src/video/SDL_video.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 11 Dec 2009 09:13:51 +0000
changeset 3540 3ad09fdbfcb0
parent 3536 0267b8b1595c
child 3570 7812d3e9564e
permissions -rw-r--r--
Fixed constness in RenderRects() parameter
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997-2009 Sam Lantinga
     4 
     5     This library is free software; you can redistribute it and/or
     6     modify it under the terms of the GNU Lesser General Public
     7     License as published by the Free Software Foundation; either
     8     version 2.1 of the License, or (at your option) any later version.
     9 
    10     This library is distributed in the hope that it will be useful,
    11     but WITHOUT ANY WARRANTY; without even the implied warranty of
    12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    13     Lesser General Public License for more details.
    14 
    15     You should have received a copy of the GNU Lesser General Public
    16     License along with this library; if not, write to the Free Software
    17     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    18 
    19     Sam Lantinga
    20     slouken@libsdl.org
    21 */
    22 #include "SDL_config.h"
    23 
    24 /* The high-level video driver subsystem */
    25 
    26 #include "SDL.h"
    27 #include "SDL_video.h"
    28 #include "SDL_sysvideo.h"
    29 #include "SDL_blit.h"
    30 #include "SDL_pixels_c.h"
    31 #include "SDL_renderer_gl.h"
    32 #include "SDL_renderer_gles.h"
    33 #include "SDL_renderer_sw.h"
    34 #include "../events/SDL_sysevents.h"
    35 #include "../events/SDL_events_c.h"
    36 
    37 #if SDL_VIDEO_OPENGL_ES
    38 #include "SDL_opengles.h"
    39 #endif /* SDL_VIDEO_OPENGL_ES */
    40 
    41 #if SDL_VIDEO_OPENGL
    42 #include "SDL_opengl.h"
    43 
    44 /* On Windows, windows.h defines CreateWindow */
    45 #ifdef CreateWindow
    46 #undef CreateWindow
    47 #endif
    48 #endif /* SDL_VIDEO_OPENGL */
    49 
    50 /* Available video drivers */
    51 static VideoBootStrap *bootstrap[] = {
    52 #if SDL_VIDEO_DRIVER_COCOA
    53     &COCOA_bootstrap,
    54 #endif
    55 #if SDL_VIDEO_DRIVER_X11
    56     &X11_bootstrap,
    57 #endif
    58 #if SDL_VIDEO_DRIVER_FBCON
    59     &FBCON_bootstrap,
    60 #endif
    61 #if SDL_VIDEO_DRIVER_DIRECTFB
    62     &DirectFB_bootstrap,
    63 #endif
    64 #if SDL_VIDEO_DRIVER_PS2GS
    65     &PS2GS_bootstrap,
    66 #endif
    67 #if SDL_VIDEO_DRIVER_PS3
    68     &PS3_bootstrap,
    69 #endif
    70 #if SDL_VIDEO_DRIVER_SVGALIB
    71     &SVGALIB_bootstrap,
    72 #endif
    73 #if SDL_VIDEO_DRIVER_GAPI
    74     &GAPI_bootstrap,
    75 #endif
    76 #if SDL_VIDEO_DRIVER_WIN32
    77     &WIN32_bootstrap,
    78 #endif
    79 #if SDL_VIDEO_DRIVER_BWINDOW
    80     &BWINDOW_bootstrap,
    81 #endif
    82 #if SDL_VIDEO_DRIVER_PHOTON
    83     &photon_bootstrap,
    84 #endif
    85 #if SDL_VIDEO_DRIVER_QNXGF
    86     &qnxgf_bootstrap,
    87 #endif
    88 #if SDL_VIDEO_DRIVER_EPOC
    89     &EPOC_bootstrap,
    90 #endif
    91 #if SDL_VIDEO_DRIVER_RISCOS
    92     &RISCOS_bootstrap,
    93 #endif
    94 #if SDL_VIDEO_DRIVER_NDS
    95     &NDS_bootstrap,
    96 #endif
    97 #if SDL_VIDEO_DRIVER_UIKIT
    98     &UIKIT_bootstrap,
    99 #endif
   100 #if SDL_VIDEO_DRIVER_DUMMY
   101     &DUMMY_bootstrap,
   102 #endif
   103 #if SDL_VIDEO_DRIVER_PANDORA
   104     &PND_bootstrap,
   105 #endif
   106     NULL
   107 };
   108 
   109 static SDL_VideoDevice *_this = NULL;
   110 
   111 /* Various local functions */
   112 static void SDL_UpdateWindowGrab(SDL_Window * window);
   113 
   114 static int
   115 cmpmodes(const void *A, const void *B)
   116 {
   117     SDL_DisplayMode a = *(const SDL_DisplayMode *) A;
   118     SDL_DisplayMode b = *(const SDL_DisplayMode *) B;
   119 
   120     if (a.w != b.w) {
   121         return b.w - a.w;
   122     }
   123     if (a.h != b.h) {
   124         return b.h - a.h;
   125     }
   126     if (SDL_BITSPERPIXEL(a.format) != SDL_BITSPERPIXEL(b.format)) {
   127         return SDL_BITSPERPIXEL(b.format) - SDL_BITSPERPIXEL(a.format);
   128     }
   129     if (SDL_PIXELLAYOUT(a.format) != SDL_PIXELLAYOUT(b.format)) {
   130         return SDL_PIXELLAYOUT(b.format) - SDL_PIXELLAYOUT(a.format);
   131     }
   132     if (a.refresh_rate != b.refresh_rate) {
   133         return b.refresh_rate - a.refresh_rate;
   134     }
   135     return 0;
   136 }
   137 
   138 static void
   139 SDL_UninitializedVideo()
   140 {
   141     SDL_SetError("Video subsystem has not been initialized");
   142 }
   143 
   144 int
   145 SDL_GetNumVideoDrivers(void)
   146 {
   147     return SDL_arraysize(bootstrap) - 1;
   148 }
   149 
   150 const char *
   151 SDL_GetVideoDriver(int index)
   152 {
   153     if (index >= 0 && index < SDL_GetNumVideoDrivers()) {
   154         return bootstrap[index]->name;
   155     }
   156     return NULL;
   157 }
   158 
   159 /*
   160  * Initialize the video and event subsystems -- determine native pixel format
   161  */
   162 int
   163 SDL_VideoInit(const char *driver_name, Uint32 flags)
   164 {
   165     SDL_VideoDevice *video;
   166     int index;
   167     int i;
   168 
   169     /* Toggle the event thread flags, based on OS requirements */
   170 #if defined(MUST_THREAD_EVENTS)
   171     flags |= SDL_INIT_EVENTTHREAD;
   172 #elif defined(CANT_THREAD_EVENTS)
   173     if ((flags & SDL_INIT_EVENTTHREAD) == SDL_INIT_EVENTTHREAD) {
   174         SDL_SetError("OS doesn't support threaded events");
   175         return -1;
   176     }
   177 #endif
   178 
   179     /* Start the event loop */
   180     if (SDL_StartEventLoop(flags) < 0) {
   181         return -1;
   182     }
   183     /* Check to make sure we don't overwrite '_this' */
   184     if (_this != NULL) {
   185         SDL_VideoQuit();
   186     }
   187     /* Select the proper video driver */
   188     index = 0;
   189     video = NULL;
   190     if (driver_name == NULL) {
   191         driver_name = SDL_getenv("SDL_VIDEODRIVER");
   192     }
   193     if (driver_name != NULL) {
   194         for (i = 0; bootstrap[i]; ++i) {
   195             if (SDL_strcasecmp(bootstrap[i]->name, driver_name) == 0) {
   196                 video = bootstrap[i]->create(index);
   197                 break;
   198             }
   199         }
   200     } else {
   201         for (i = 0; bootstrap[i]; ++i) {
   202             if (bootstrap[i]->available()) {
   203                 video = bootstrap[i]->create(index);
   204                 if (video != NULL) {
   205                     break;
   206                 }
   207             }
   208         }
   209     }
   210     if (video == NULL) {
   211         if (driver_name) {
   212             SDL_SetError("%s not available", driver_name);
   213         } else {
   214             SDL_SetError("No available video device");
   215         }
   216         return -1;
   217     }
   218     _this = video;
   219     _this->name = bootstrap[i]->name;
   220     _this->next_object_id = 1;
   221 
   222 
   223     /* Set some very sane GL defaults */
   224     _this->gl_config.driver_loaded = 0;
   225     _this->gl_config.dll_handle = NULL;
   226     _this->gl_config.red_size = 3;
   227     _this->gl_config.green_size = 3;
   228     _this->gl_config.blue_size = 2;
   229     _this->gl_config.alpha_size = 0;
   230     _this->gl_config.buffer_size = 0;
   231     _this->gl_config.depth_size = 16;
   232     _this->gl_config.stencil_size = 0;
   233     _this->gl_config.double_buffer = 1;
   234     _this->gl_config.accum_red_size = 0;
   235     _this->gl_config.accum_green_size = 0;
   236     _this->gl_config.accum_blue_size = 0;
   237     _this->gl_config.accum_alpha_size = 0;
   238     _this->gl_config.stereo = 0;
   239     _this->gl_config.multisamplebuffers = 0;
   240     _this->gl_config.multisamplesamples = 0;
   241     _this->gl_config.retained_backing = 1;
   242     _this->gl_config.accelerated = -1;  /* not known, don't set */
   243     _this->gl_config.major_version = 2;
   244     _this->gl_config.minor_version = 1;
   245 
   246     /* Initialize the video subsystem */
   247     if (_this->VideoInit(_this) < 0) {
   248         SDL_VideoQuit();
   249         return -1;
   250     }
   251     /* Make sure some displays were added */
   252     if (_this->num_displays == 0) {
   253         SDL_SetError("The video driver did not add any displays");
   254         SDL_VideoQuit();
   255         return (-1);
   256     }
   257     /* The software renderer is always available */
   258     for (i = 0; i < _this->num_displays; ++i) {
   259         SDL_VideoDisplay *display = &_this->displays[i];
   260         if (_this->GL_CreateContext) {
   261 #if SDL_VIDEO_RENDER_OGL
   262             SDL_AddRenderDriver(display, &GL_RenderDriver);
   263 #endif
   264 #if SDL_VIDEO_RENDER_OGL_ES
   265             SDL_AddRenderDriver(display, &GL_ES_RenderDriver);
   266 #endif
   267         }
   268         if (display->num_render_drivers > 0) {
   269             SDL_AddRenderDriver(display, &SW_RenderDriver);
   270         }
   271     }
   272 
   273     /* We're ready to go! */
   274     return 0;
   275 }
   276 
   277 const char *
   278 SDL_GetCurrentVideoDriver()
   279 {
   280     if (!_this) {
   281         SDL_UninitializedVideo();
   282         return NULL;
   283     }
   284     return _this->name;
   285 }
   286 
   287 SDL_VideoDevice *
   288 SDL_GetVideoDevice()
   289 {
   290     return _this;
   291 }
   292 
   293 int
   294 SDL_AddBasicVideoDisplay(const SDL_DisplayMode * desktop_mode)
   295 {
   296     SDL_VideoDisplay display;
   297 
   298     SDL_zero(display);
   299     if (desktop_mode) {
   300         display.desktop_mode = *desktop_mode;
   301     }
   302     display.current_mode = display.desktop_mode;
   303 
   304     return SDL_AddVideoDisplay(&display);
   305 }
   306 
   307 int
   308 SDL_AddVideoDisplay(const SDL_VideoDisplay * display)
   309 {
   310     SDL_VideoDisplay *displays;
   311     int index = -1;
   312 
   313     displays =
   314         SDL_realloc(_this->displays,
   315                     (_this->num_displays + 1) * sizeof(*displays));
   316     if (displays) {
   317         index = _this->num_displays++;
   318         displays[index] = *display;
   319         displays[index].device = _this;
   320         _this->displays = displays;
   321     } else {
   322         SDL_OutOfMemory();
   323     }
   324     return index;
   325 }
   326 
   327 int
   328 SDL_GetNumVideoDisplays(void)
   329 {
   330     if (!_this) {
   331         SDL_UninitializedVideo();
   332         return 0;
   333     }
   334     return _this->num_displays;
   335 }
   336 
   337 int
   338 SDL_GetDisplayBounds(int index, SDL_Rect * rect)
   339 {
   340     if (!_this) {
   341         SDL_UninitializedVideo();
   342         return -1;
   343     }
   344     if (index < 0 || index >= _this->num_displays) {
   345         SDL_SetError("index must be in the range 0 - %d",
   346                      _this->num_displays - 1);
   347         return -1;
   348     }
   349     if (rect) {
   350         SDL_VideoDisplay *display = &_this->displays[index];
   351 
   352         if (_this->GetDisplayBounds) {
   353             if (_this->GetDisplayBounds(_this, display, rect) < 0) {
   354                 return -1;
   355             }
   356         } else {
   357             /* Assume that the displays are left to right */
   358             if (index == 0) {
   359                 rect->x = 0;
   360                 rect->y = 0;
   361             } else {
   362                 SDL_GetDisplayBounds(index-1, rect);
   363                 rect->x += rect->w;
   364             }
   365             rect->w = display->desktop_mode.w;
   366             rect->h = display->desktop_mode.h;
   367         }
   368     }
   369     return 0;
   370 }
   371 
   372 int
   373 SDL_SelectVideoDisplay(int index)
   374 {
   375     if (!_this) {
   376         SDL_UninitializedVideo();
   377         return (-1);
   378     }
   379     if (index < 0 || index >= _this->num_displays) {
   380         SDL_SetError("index must be in the range 0 - %d",
   381                      _this->num_displays - 1);
   382         return -1;
   383     }
   384     _this->current_display = index;
   385     return 0;
   386 }
   387 
   388 int
   389 SDL_GetCurrentVideoDisplay(void)
   390 {
   391     if (!_this) {
   392         SDL_UninitializedVideo();
   393         return (-1);
   394     }
   395     return _this->current_display;
   396 }
   397 
   398 SDL_bool
   399 SDL_AddDisplayMode(SDL_VideoDisplay * display,  const SDL_DisplayMode * mode)
   400 {
   401     SDL_DisplayMode *modes;
   402     int i, nmodes;
   403 
   404     /* Make sure we don't already have the mode in the list */
   405     modes = display->display_modes;
   406     nmodes = display->num_display_modes;
   407     for (i = nmodes; i--;) {
   408         if (SDL_memcmp(mode, &modes[i], sizeof(*mode)) == 0) {
   409             return SDL_FALSE;
   410         }
   411     }
   412 
   413     /* Go ahead and add the new mode */
   414     if (nmodes == display->max_display_modes) {
   415         modes =
   416             SDL_realloc(modes,
   417                         (display->max_display_modes + 32) * sizeof(*modes));
   418         if (!modes) {
   419             return SDL_FALSE;
   420         }
   421         display->display_modes = modes;
   422         display->max_display_modes += 32;
   423     }
   424     modes[nmodes] = *mode;
   425     display->num_display_modes++;
   426 
   427     /* Re-sort video modes */
   428     SDL_qsort(display->display_modes, display->num_display_modes,
   429               sizeof(SDL_DisplayMode), cmpmodes);
   430 
   431     return SDL_TRUE;
   432 }
   433 
   434 int
   435 SDL_GetNumDisplayModesForDisplay(SDL_VideoDisplay * display)
   436 {
   437     if (!display->num_display_modes && _this->GetDisplayModes) {
   438         _this->GetDisplayModes(_this, display);
   439         SDL_qsort(display->display_modes, display->num_display_modes,
   440                   sizeof(SDL_DisplayMode), cmpmodes);
   441     }
   442     return display->num_display_modes;
   443 }
   444 
   445 int
   446 SDL_GetNumDisplayModes()
   447 {
   448     if (_this) {
   449         return SDL_GetNumDisplayModesForDisplay(&SDL_CurrentDisplay);
   450     }
   451     return 0;
   452 }
   453 
   454 int
   455 SDL_GetDisplayModeForDisplay(SDL_VideoDisplay * display, int index, SDL_DisplayMode * mode)
   456 {
   457     if (index < 0 || index >= SDL_GetNumDisplayModesForDisplay(display)) {
   458         SDL_SetError("index must be in the range of 0 - %d",
   459                      SDL_GetNumDisplayModesForDisplay(display) - 1);
   460         return -1;
   461     }
   462     if (mode) {
   463         *mode = display->display_modes[index];
   464     }
   465     return 0;
   466 }
   467 
   468 int
   469 SDL_GetDisplayMode(int index, SDL_DisplayMode * mode)
   470 {
   471     return SDL_GetDisplayModeForDisplay(&SDL_CurrentDisplay, index, mode);
   472 }
   473 
   474 int
   475 SDL_GetDesktopDisplayModeForDisplay(SDL_VideoDisplay * display, SDL_DisplayMode * mode)
   476 {
   477     if (mode) {
   478         *mode = display->desktop_mode;
   479     }
   480     return 0;
   481 }
   482 
   483 int
   484 SDL_GetDesktopDisplayMode(SDL_DisplayMode * mode)
   485 {
   486     if (!_this) {
   487         SDL_UninitializedVideo();
   488         return -1;
   489     }
   490     return SDL_GetDesktopDisplayModeForDisplay(&SDL_CurrentDisplay, mode);
   491 }
   492 
   493 int
   494 SDL_GetCurrentDisplayModeForDisplay(SDL_VideoDisplay * display, SDL_DisplayMode * mode)
   495 {
   496     if (mode) {
   497         *mode = display->current_mode;
   498     }
   499     return 0;
   500 }
   501 
   502 int
   503 SDL_GetCurrentDisplayMode(SDL_DisplayMode * mode)
   504 {
   505     if (!_this) {
   506         SDL_UninitializedVideo();
   507         return -1;
   508     }
   509     return SDL_GetCurrentDisplayModeForDisplay(&SDL_CurrentDisplay, mode);
   510 }
   511 
   512 SDL_DisplayMode *
   513 SDL_GetClosestDisplayModeForDisplay(SDL_VideoDisplay * display,
   514                                     const SDL_DisplayMode * mode,
   515                                     SDL_DisplayMode * closest)
   516 {
   517     Uint32 target_format;
   518     int target_refresh_rate;
   519     int i;
   520     SDL_DisplayMode *current, *match;
   521 
   522     if (!mode || !closest) {
   523         SDL_SetError("Missing desired mode or closest mode parameter");
   524         return NULL;
   525     }
   526 
   527     /* Default to the desktop format */
   528     if (mode->format) {
   529         target_format = mode->format;
   530     } else {
   531         target_format = display->desktop_mode.format;
   532     }
   533 
   534     /* Default to the desktop refresh rate */
   535     if (mode->refresh_rate) {
   536         target_refresh_rate = mode->refresh_rate;
   537     } else {
   538         target_refresh_rate = display->desktop_mode.refresh_rate;
   539     }
   540 
   541     match = NULL;
   542     for (i = 0; i < SDL_GetNumDisplayModesForDisplay(display); ++i) {
   543         current = &display->display_modes[i];
   544 
   545         if (current->w && (current->w < mode->w)) {
   546             /* Out of sorted modes large enough here */
   547             break;
   548         }
   549         if (current->h && (current->h < mode->h)) {
   550             if (current->w && (current->w == mode->w)) {
   551                 /* Out of sorted modes large enough here */
   552                 break;
   553             }
   554             /* Wider, but not tall enough, due to a different
   555                aspect ratio. This mode must be skipped, but closer
   556                modes may still follow. */
   557             continue;
   558         }
   559         if (!match || current->w < match->w || current->h < match->h) {
   560             match = current;
   561             continue;
   562         }
   563         if (current->format != match->format) {
   564             /* Sorted highest depth to lowest */
   565             if (current->format == target_format ||
   566                 (SDL_BITSPERPIXEL(current->format) >=
   567                  SDL_BITSPERPIXEL(target_format)
   568                  && SDL_PIXELTYPE(current->format) ==
   569                  SDL_PIXELTYPE(target_format))) {
   570                 match = current;
   571             }
   572             continue;
   573         }
   574         if (current->refresh_rate != match->refresh_rate) {
   575             /* Sorted highest refresh to lowest */
   576             if (current->refresh_rate >= target_refresh_rate) {
   577                 match = current;
   578             }
   579         }
   580     }
   581     if (match) {
   582         if (match->format) {
   583             closest->format = match->format;
   584         } else {
   585             closest->format = mode->format;
   586         }
   587         if (match->w && match->h) {
   588             closest->w = match->w;
   589             closest->h = match->h;
   590         } else {
   591             closest->w = mode->w;
   592             closest->h = mode->h;
   593         }
   594         if (match->refresh_rate) {
   595             closest->refresh_rate = match->refresh_rate;
   596         } else {
   597             closest->refresh_rate = mode->refresh_rate;
   598         }
   599         closest->driverdata = match->driverdata;
   600 
   601         /*
   602          * Pick some reasonable defaults if the app and driver don't
   603          * care
   604          */
   605         if (!closest->format) {
   606             closest->format = SDL_PIXELFORMAT_RGB888;
   607         }
   608         if (!closest->w) {
   609             closest->w = 640;
   610         }
   611         if (!closest->h) {
   612             closest->h = 480;
   613         }
   614         return closest;
   615     }
   616     return NULL;
   617 }
   618 
   619 SDL_DisplayMode *
   620 SDL_GetClosestDisplayMode(const SDL_DisplayMode * mode,
   621                           SDL_DisplayMode * closest)
   622 {
   623     if (!_this) {
   624         SDL_UninitializedVideo();
   625         return NULL;
   626     }
   627     return SDL_GetClosestDisplayModeForDisplay(&SDL_CurrentDisplay, mode, closest);
   628 }
   629 
   630 int
   631 SDL_SetDisplayModeForDisplay(SDL_VideoDisplay * display, const SDL_DisplayMode * mode)
   632 {
   633     SDL_DisplayMode display_mode;
   634     SDL_DisplayMode current_mode;
   635     int ncolors;
   636 
   637     if (mode) {
   638         display_mode = *mode;
   639 
   640         /* Default to the current mode */
   641         if (!display_mode.format) {
   642             display_mode.format = display->current_mode.format;
   643         }
   644         if (!display_mode.w) {
   645             display_mode.w = display->current_mode.w;
   646         }
   647         if (!display_mode.h) {
   648             display_mode.h = display->current_mode.h;
   649         }
   650         if (!display_mode.refresh_rate) {
   651             display_mode.refresh_rate = display->current_mode.refresh_rate;
   652         }
   653 
   654         /* Get a good video mode, the closest one possible */
   655         if (!SDL_GetClosestDisplayModeForDisplay(display, &display_mode, &display_mode)) {
   656             SDL_SetError("No video mode large enough for %dx%d",
   657                          display_mode.w, display_mode.h);
   658             return -1;
   659         }
   660     } else {
   661         display_mode = display->desktop_mode;
   662     }
   663 
   664     /* See if there's anything left to do */
   665     SDL_GetCurrentDisplayModeForDisplay(display, &current_mode);
   666     if (SDL_memcmp(&display_mode, &current_mode, sizeof(display_mode)) == 0) {
   667         return 0;
   668     }
   669 
   670     /* Actually change the display mode */
   671     if (_this->SetDisplayMode(_this, display, &display_mode) < 0) {
   672         return -1;
   673     }
   674     display->current_mode = display_mode;
   675 
   676     /* Set up a palette, if necessary */
   677     if (SDL_ISPIXELFORMAT_INDEXED(display_mode.format)) {
   678         ncolors = (1 << SDL_BITSPERPIXEL(display_mode.format));
   679     } else {
   680         ncolors = 0;
   681     }
   682     if ((!ncolors && display->palette) || (ncolors && !display->palette)
   683         || (ncolors && ncolors != display->palette->ncolors)) {
   684         if (display->palette) {
   685             SDL_FreePalette(display->palette);
   686             display->palette = NULL;
   687         }
   688         if (ncolors) {
   689             display->palette = SDL_AllocPalette(ncolors);
   690             if (!display->palette) {
   691                 return -1;
   692             }
   693             SDL_DitherColors(display->palette->colors,
   694                              SDL_BITSPERPIXEL(display_mode.format));
   695         }
   696     }
   697 
   698     return 0;
   699 }
   700 
   701 int
   702 SDL_SetDisplayMode(const SDL_DisplayMode * mode)
   703 {
   704     if (!_this) {
   705         SDL_UninitializedVideo();
   706         return -1;
   707     }
   708     return SDL_SetDisplayModeForDisplay(&SDL_CurrentDisplay, mode);
   709 }
   710 
   711 int
   712 SDL_SetWindowDisplayMode(SDL_WindowID windowID, const SDL_DisplayMode * mode)
   713 {
   714     SDL_Window *window = SDL_GetWindowFromID(windowID);
   715 
   716     if (!window) {
   717         return -1;
   718     }
   719 
   720     if (mode) {
   721         window->fullscreen_mode = *mode;
   722     } else {
   723         SDL_zero(window->fullscreen_mode);
   724     }
   725     return 0;
   726 }
   727 
   728 int
   729 SDL_GetWindowDisplayMode(SDL_WindowID windowID, SDL_DisplayMode * mode)
   730 {
   731     SDL_Window *window = SDL_GetWindowFromID(windowID);
   732     SDL_DisplayMode fullscreen_mode;
   733 
   734     if (!window) {
   735         return -1;
   736     }
   737 
   738     fullscreen_mode = window->fullscreen_mode;
   739     if (!fullscreen_mode.w) {
   740         fullscreen_mode.w = window->w;
   741     }
   742     if (!fullscreen_mode.h) {
   743         fullscreen_mode.h = window->h;
   744     }
   745 
   746     if (!SDL_GetClosestDisplayModeForDisplay(SDL_GetDisplayFromWindow(window),
   747                                              &fullscreen_mode,
   748                                              &fullscreen_mode)) {
   749         SDL_SetError("Couldn't find display mode match");
   750         return -1;
   751     }
   752 
   753     if (mode) {
   754         *mode = fullscreen_mode;
   755     }
   756     return 0;
   757 }
   758 
   759 static void
   760 SDL_UpdateFullscreenMode(SDL_Window * window, SDL_bool attempt)
   761 {
   762     SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);
   763     int i;
   764 
   765     /* See if we're already processing a window */
   766     if (display->updating_fullscreen) {
   767         return;
   768     }
   769 
   770     display->updating_fullscreen = SDL_TRUE;
   771 
   772     /* See if we even want to do anything here */
   773     if ((window->flags & SDL_WINDOW_FULLSCREEN) &&
   774         (window->flags & SDL_WINDOW_SHOWN)) {
   775         if (attempt) {
   776             /* We just gained some state, try to gain all states */
   777             if (window->flags & SDL_WINDOW_MINIMIZED) {
   778                 SDL_RestoreWindow(window->id);
   779             } else {
   780                 SDL_RaiseWindow(window->id);
   781             }
   782         } else {
   783             /* We just lost some state, try to release all states */
   784             SDL_MinimizeWindow(window->id);
   785         }
   786     }
   787 
   788     if (FULLSCREEN_VISIBLE(window)) {
   789         /* Hide any other fullscreen windows */
   790         for (i = 0; i < display->num_windows; ++i) {
   791             SDL_Window *other = &display->windows[i];
   792             if (other != window && FULLSCREEN_VISIBLE(other)) {
   793                 SDL_MinimizeWindow(other->id);
   794             }
   795         }
   796     }
   797 
   798     display->updating_fullscreen = SDL_FALSE;
   799 
   800     /* See if there are any fullscreen windows */
   801     for (i = 0; i < display->num_windows; ++i) {
   802         window = &display->windows[i];
   803         if (FULLSCREEN_VISIBLE(window)) {
   804             SDL_DisplayMode fullscreen_mode;
   805             if (SDL_GetWindowDisplayMode(window->id, &fullscreen_mode) == 0) {
   806                 SDL_SetDisplayModeForDisplay(display, &fullscreen_mode);
   807                 display->fullscreen_window = window;
   808                 return;
   809             }
   810         }
   811     }
   812 
   813     /* Nope, restore the desktop mode */
   814     SDL_SetDisplayModeForDisplay(display, NULL);
   815     display->fullscreen_window = NULL;
   816 }
   817 
   818 int
   819 SDL_SetPaletteForDisplay(SDL_VideoDisplay * display, const SDL_Color * colors, int firstcolor, int ncolors)
   820 {
   821     SDL_Palette *palette;
   822     int status = 0;
   823 
   824     palette = display->palette;
   825     if (!palette) {
   826         SDL_SetError("Display mode does not have a palette");
   827         return -1;
   828     }
   829     status = SDL_SetPaletteColors(palette, colors, firstcolor, ncolors);
   830 
   831     if (_this->SetDisplayPalette) {
   832         if (_this->SetDisplayPalette(_this, display, palette) < 0) {
   833             status = -1;
   834         }
   835     }
   836     return status;
   837 }
   838 
   839 int
   840 SDL_SetDisplayPalette(const SDL_Color * colors, int firstcolor, int ncolors)
   841 {
   842     if (!_this) {
   843         SDL_UninitializedVideo();
   844         return -1;
   845     }
   846     return SDL_SetPaletteForDisplay(&SDL_CurrentDisplay, colors, firstcolor, ncolors);
   847 }
   848 
   849 int
   850 SDL_GetPaletteForDisplay(SDL_VideoDisplay * display, SDL_Color * colors, int firstcolor, int ncolors)
   851 {
   852     SDL_Palette *palette;
   853 
   854     palette = display->palette;
   855     if (!palette || !palette->ncolors) {
   856         SDL_SetError("Display mode does not have a palette");
   857         return -1;
   858     }
   859     if (firstcolor < 0 || (firstcolor + ncolors) > palette->ncolors) {
   860         SDL_SetError("Palette indices are out of range");
   861         return -1;
   862     }
   863     SDL_memcpy(colors, &palette->colors[firstcolor],
   864                ncolors * sizeof(*colors));
   865     return 0;
   866 }
   867 
   868 int
   869 SDL_GetDisplayPalette(SDL_Color * colors, int firstcolor, int ncolors)
   870 {
   871     if (!_this) {
   872         SDL_UninitializedVideo();
   873         return -1;
   874     }
   875     return SDL_GetPaletteForDisplay(&SDL_CurrentDisplay, colors, firstcolor, ncolors);
   876 }
   877 
   878 SDL_WindowID
   879 SDL_CreateWindow(const char *title, int x, int y, int w, int h, Uint32 flags)
   880 {
   881     const Uint32 allowed_flags = (SDL_WINDOW_FULLSCREEN |
   882                                   SDL_WINDOW_OPENGL |
   883                                   SDL_WINDOW_BORDERLESS |
   884                                   SDL_WINDOW_RESIZABLE |
   885                                   SDL_WINDOW_INPUT_GRABBED);
   886     SDL_VideoDisplay *display;
   887     SDL_Window window;
   888     int num_windows;
   889     SDL_Window *windows;
   890 
   891     if (!_this) {
   892         /* Initialize the video system if needed */
   893         if (SDL_VideoInit(NULL, 0) < 0) {
   894             return 0;
   895         }
   896     }
   897     if (flags & SDL_WINDOW_OPENGL) {
   898         if (!_this->GL_CreateContext) {
   899             SDL_SetError("No OpenGL support in video driver");
   900             return 0;
   901         }
   902         SDL_GL_LoadLibrary(NULL);
   903     }
   904     SDL_zero(window);
   905     window.id = _this->next_object_id++;
   906     window.x = x;
   907     window.y = y;
   908     window.w = w;
   909     window.h = h;
   910     window.flags = (flags & allowed_flags);
   911     window.display = _this->current_display;
   912 
   913     if (_this->CreateWindow && _this->CreateWindow(_this, &window) < 0) {
   914         if (flags & SDL_WINDOW_OPENGL) {
   915             SDL_GL_UnloadLibrary();
   916         }
   917         return 0;
   918     }
   919     display = &SDL_CurrentDisplay;
   920     num_windows = display->num_windows;
   921     windows =
   922         SDL_realloc(display->windows, (num_windows + 1) * sizeof(*windows));
   923     if (!windows) {
   924         if (_this->DestroyWindow) {
   925             _this->DestroyWindow(_this, &window);
   926         }
   927         if (flags & SDL_WINDOW_OPENGL) {
   928             SDL_GL_UnloadLibrary();
   929         }
   930         return 0;
   931     }
   932     windows[num_windows] = window;
   933     display->windows = windows;
   934     display->num_windows++;
   935 
   936     if (title) {
   937         SDL_SetWindowTitle(window.id, title);
   938     }
   939     if (flags & SDL_WINDOW_MAXIMIZED) {
   940         SDL_MaximizeWindow(window.id);
   941     }
   942     if (flags & SDL_WINDOW_MINIMIZED) {
   943         SDL_MinimizeWindow(window.id);
   944     }
   945     if (flags & SDL_WINDOW_SHOWN) {
   946         SDL_ShowWindow(window.id);
   947     }
   948     SDL_UpdateWindowGrab(&window);
   949 
   950     return window.id;
   951 }
   952 
   953 SDL_WindowID
   954 SDL_CreateWindowFrom(const void *data)
   955 {
   956     SDL_VideoDisplay *display;
   957     SDL_Window window;
   958     int num_windows;
   959     SDL_Window *windows;
   960 
   961     if (!_this) {
   962         SDL_UninitializedVideo();
   963         return (0);
   964     }
   965     SDL_zero(window);
   966     window.id = _this->next_object_id++;
   967     window.display = _this->current_display;
   968     window.flags = SDL_WINDOW_FOREIGN;
   969 
   970     if (!_this->CreateWindowFrom ||
   971         _this->CreateWindowFrom(_this, &window, data) < 0) {
   972         return 0;
   973     }
   974     /* FIXME: Find out what display this window is actually on... */
   975     display = &SDL_CurrentDisplay;
   976     num_windows = display->num_windows;
   977     windows =
   978         SDL_realloc(display->windows, (num_windows + 1) * sizeof(*windows));
   979     if (!windows) {
   980         if (_this->DestroyWindow) {
   981             _this->DestroyWindow(_this, &window);
   982         }
   983         if (window.title) {
   984             SDL_free(window.title);
   985         }
   986         return 0;
   987     }
   988     windows[num_windows] = window;
   989     display->windows = windows;
   990     display->num_windows++;
   991 
   992     return window.id;
   993 }
   994 
   995 int
   996 SDL_RecreateWindow(SDL_Window * window, Uint32 flags)
   997 {
   998     const Uint32 allowed_flags = (SDL_WINDOW_FULLSCREEN |
   999                                   SDL_WINDOW_OPENGL |
  1000                                   SDL_WINDOW_BORDERLESS |
  1001                                   SDL_WINDOW_RESIZABLE |
  1002                                   SDL_WINDOW_INPUT_GRABBED |
  1003                                   SDL_WINDOW_FOREIGN);
  1004     char *title = window->title;
  1005 
  1006     if ((flags & SDL_WINDOW_OPENGL) && !_this->GL_CreateContext) {
  1007         SDL_SetError("No OpenGL support in video driver");
  1008         return -1;
  1009     }
  1010     if ((window->flags & SDL_WINDOW_OPENGL) != (flags & SDL_WINDOW_OPENGL)) {
  1011         if (flags & SDL_WINDOW_OPENGL) {
  1012             SDL_GL_LoadLibrary(NULL);
  1013         } else {
  1014             SDL_GL_UnloadLibrary();
  1015         }
  1016     }
  1017 
  1018     if (window->flags & SDL_WINDOW_FOREIGN) {
  1019         /* Can't destroy and re-create foreign windows, hrm */
  1020         flags |= SDL_WINDOW_FOREIGN;
  1021     } else {
  1022         flags &= ~SDL_WINDOW_FOREIGN;
  1023     }
  1024 
  1025     if (_this->DestroyWindow && !(flags & SDL_WINDOW_FOREIGN)) {
  1026         _this->DestroyWindow(_this, window);
  1027     }
  1028 
  1029     window->title = NULL;
  1030     window->flags = (flags & allowed_flags);
  1031 
  1032     if (_this->CreateWindow && !(flags & SDL_WINDOW_FOREIGN)) {
  1033         if (_this->CreateWindow(_this, window) < 0) {
  1034             if (flags & SDL_WINDOW_OPENGL) {
  1035                 SDL_GL_UnloadLibrary();
  1036             }
  1037             return -1;
  1038         }
  1039     }
  1040 
  1041     if (title) {
  1042         SDL_SetWindowTitle(window->id, title);
  1043         SDL_free(title);
  1044     }
  1045     if (flags & SDL_WINDOW_MAXIMIZED) {
  1046         SDL_MaximizeWindow(window->id);
  1047     }
  1048     if (flags & SDL_WINDOW_MINIMIZED) {
  1049         SDL_MinimizeWindow(window->id);
  1050     }
  1051     if (flags & SDL_WINDOW_SHOWN) {
  1052         SDL_ShowWindow(window->id);
  1053     }
  1054     SDL_UpdateWindowGrab(window);
  1055 
  1056     return 0;
  1057 }
  1058 
  1059 SDL_Window *
  1060 SDL_GetWindowFromID(SDL_WindowID windowID)
  1061 {
  1062     int i, j;
  1063 
  1064     if (!_this) {
  1065         SDL_UninitializedVideo();
  1066         return NULL;
  1067     }
  1068     if (windowID) {
  1069         for (i = 0; i < _this->num_displays; ++i) {
  1070             SDL_VideoDisplay *display = &_this->displays[i];
  1071             for (j = 0; j < display->num_windows; ++j) {
  1072                 SDL_Window *window = &display->windows[j];
  1073                 if (window->id == windowID) {
  1074                     return window;
  1075                 }
  1076             }
  1077         }
  1078     } else {
  1079         /* Just return the first active window */
  1080         for (i = 0; i < _this->num_displays; ++i) {
  1081             SDL_VideoDisplay *display = &_this->displays[i];
  1082             for (j = 0; j < display->num_windows; ++j) {
  1083                 SDL_Window *window = &display->windows[j];
  1084                 return window;
  1085             }
  1086         }
  1087     }
  1088     /* Couldn't find the window with the requested ID */
  1089     SDL_SetError("Invalid window ID");
  1090     return NULL;
  1091 }
  1092 
  1093 SDL_VideoDisplay *
  1094 SDL_GetDisplayFromWindow(SDL_Window * window)
  1095 {
  1096     if (!_this) {
  1097         SDL_UninitializedVideo();
  1098         return NULL;
  1099     }
  1100     if (!window) {
  1101         return NULL;
  1102     }
  1103     return &_this->displays[window->display];
  1104 }
  1105 
  1106 static __inline__ SDL_Renderer *
  1107 SDL_GetCurrentRenderer(SDL_bool create)
  1108 {
  1109     if (!_this) {
  1110         SDL_UninitializedVideo();
  1111         return NULL;
  1112     }
  1113     if (!SDL_CurrentRenderer) {
  1114         if (!create) {
  1115             SDL_SetError("Use SDL_CreateRenderer() to create a renderer");
  1116             return NULL;
  1117         }
  1118         if (SDL_CreateRenderer(0, -1, 0) < 0) {
  1119             return NULL;
  1120         }
  1121     }
  1122     return SDL_CurrentRenderer;
  1123 }
  1124 
  1125 Uint32
  1126 SDL_GetWindowFlags(SDL_WindowID windowID)
  1127 {
  1128     SDL_Window *window = SDL_GetWindowFromID(windowID);
  1129 
  1130     if (!window) {
  1131         return 0;
  1132     }
  1133     return window->flags;
  1134 }
  1135 
  1136 void
  1137 SDL_SetWindowTitle(SDL_WindowID windowID, const char *title)
  1138 {
  1139     SDL_Window *window = SDL_GetWindowFromID(windowID);
  1140 
  1141     if (!window || title == window->title) {
  1142         return;
  1143     }
  1144     if (window->title) {
  1145         SDL_free(window->title);
  1146     }
  1147     if (title) {
  1148         window->title = SDL_strdup(title);
  1149     } else {
  1150         window->title = NULL;
  1151     }
  1152 
  1153     if (_this->SetWindowTitle) {
  1154         _this->SetWindowTitle(_this, window);
  1155     }
  1156 }
  1157 
  1158 const char *
  1159 SDL_GetWindowTitle(SDL_WindowID windowID)
  1160 {
  1161     SDL_Window *window = SDL_GetWindowFromID(windowID);
  1162 
  1163     if (!window) {
  1164         return NULL;
  1165     }
  1166     return window->title;
  1167 }
  1168 
  1169 void
  1170 SDL_SetWindowIcon(SDL_WindowID windowID, SDL_Surface * icon)
  1171 {
  1172     SDL_Window *window = SDL_GetWindowFromID(windowID);
  1173 
  1174     if (!window) {
  1175         return;
  1176     }
  1177     if (_this->SetWindowIcon) {
  1178         _this->SetWindowIcon(_this, window, icon);
  1179     }
  1180 }
  1181 
  1182 void
  1183 SDL_SetWindowData(SDL_WindowID windowID, void *userdata)
  1184 {
  1185     SDL_Window *window = SDL_GetWindowFromID(windowID);
  1186 
  1187     if (!window) {
  1188         return;
  1189     }
  1190     window->userdata = userdata;
  1191 }
  1192 
  1193 void *
  1194 SDL_GetWindowData(SDL_WindowID windowID)
  1195 {
  1196     SDL_Window *window = SDL_GetWindowFromID(windowID);
  1197 
  1198     if (!window) {
  1199         return NULL;
  1200     }
  1201     return window->userdata;
  1202 }
  1203 
  1204 void
  1205 SDL_SetWindowPosition(SDL_WindowID windowID, int x, int y)
  1206 {
  1207     SDL_Window *window = SDL_GetWindowFromID(windowID);
  1208     SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);
  1209 
  1210     if (!window) {
  1211         return;
  1212     }
  1213     if (x != SDL_WINDOWPOS_UNDEFINED) {
  1214         window->x = x;
  1215     }
  1216     if (y != SDL_WINDOWPOS_UNDEFINED) {
  1217         window->y = y;
  1218     }
  1219     if (_this->SetWindowPosition) {
  1220         _this->SetWindowPosition(_this, window);
  1221     }
  1222     SDL_SendWindowEvent(window->id, SDL_WINDOWEVENT_MOVED, x, y);
  1223 }
  1224 
  1225 void
  1226 SDL_GetWindowPosition(SDL_WindowID windowID, int *x, int *y)
  1227 {
  1228     SDL_Window *window = SDL_GetWindowFromID(windowID);
  1229 
  1230     if (!window) {
  1231         return;
  1232     }
  1233     if (x) {
  1234         *x = window->x;
  1235     }
  1236     if (y) {
  1237         *y = window->y;
  1238     }
  1239 }
  1240 
  1241 void
  1242 SDL_SetWindowSize(SDL_WindowID windowID, int w, int h)
  1243 {
  1244     SDL_Window *window = SDL_GetWindowFromID(windowID);
  1245 
  1246     if (!window) {
  1247         return;
  1248     }
  1249     window->w = w;
  1250     window->h = h;
  1251 
  1252     if (_this->SetWindowSize) {
  1253         _this->SetWindowSize(_this, window);
  1254     }
  1255     SDL_OnWindowResized(window);
  1256 }
  1257 
  1258 void
  1259 SDL_GetWindowSize(SDL_WindowID windowID, int *w, int *h)
  1260 {
  1261     SDL_Window *window = SDL_GetWindowFromID(windowID);
  1262 
  1263     if (window) {
  1264         if (w) {
  1265             *w = window->w;
  1266         }
  1267         if (h) {
  1268             *h = window->h;
  1269         }
  1270     } else {
  1271         if (w) {
  1272             *w = 0;
  1273         }
  1274         if (h) {
  1275             *h = 0;
  1276         }
  1277     }
  1278 }
  1279 
  1280 void
  1281 SDL_ShowWindow(SDL_WindowID windowID)
  1282 {
  1283     SDL_Window *window = SDL_GetWindowFromID(windowID);
  1284 
  1285     if (!window || (window->flags & SDL_WINDOW_SHOWN)) {
  1286         return;
  1287     }
  1288 
  1289     if (_this->ShowWindow) {
  1290         _this->ShowWindow(_this, window);
  1291     }
  1292     SDL_SendWindowEvent(window->id, SDL_WINDOWEVENT_SHOWN, 0, 0);
  1293 }
  1294 
  1295 void
  1296 SDL_HideWindow(SDL_WindowID windowID)
  1297 {
  1298     SDL_Window *window = SDL_GetWindowFromID(windowID);
  1299 
  1300     if (!window || !(window->flags & SDL_WINDOW_SHOWN)) {
  1301         return;
  1302     }
  1303 
  1304     if (_this->HideWindow) {
  1305         _this->HideWindow(_this, window);
  1306     }
  1307     SDL_SendWindowEvent(window->id, SDL_WINDOWEVENT_HIDDEN, 0, 0);
  1308 }
  1309 
  1310 void
  1311 SDL_RaiseWindow(SDL_WindowID windowID)
  1312 {
  1313     SDL_Window *window = SDL_GetWindowFromID(windowID);
  1314 
  1315     if (!window || !(window->flags & SDL_WINDOW_SHOWN)) {
  1316         return;
  1317     }
  1318     if (_this->RaiseWindow) {
  1319         _this->RaiseWindow(_this, window);
  1320     } else {
  1321         /* FIXME: What we really want is a way to request focus */
  1322         SDL_SendWindowEvent(window->id, SDL_WINDOWEVENT_FOCUS_GAINED, 0, 0);
  1323     }
  1324 }
  1325 
  1326 void
  1327 SDL_MaximizeWindow(SDL_WindowID windowID)
  1328 {
  1329     SDL_Window *window = SDL_GetWindowFromID(windowID);
  1330 
  1331     if (!window || (window->flags & SDL_WINDOW_MAXIMIZED)) {
  1332         return;
  1333     }
  1334 
  1335     if (_this->MaximizeWindow) {
  1336         _this->MaximizeWindow(_this, window);
  1337     }
  1338     SDL_SendWindowEvent(window->id, SDL_WINDOWEVENT_MAXIMIZED, 0, 0);
  1339 }
  1340 
  1341 void
  1342 SDL_MinimizeWindow(SDL_WindowID windowID)
  1343 {
  1344     SDL_Window *window = SDL_GetWindowFromID(windowID);
  1345 
  1346     if (!window || (window->flags & SDL_WINDOW_MINIMIZED)) {
  1347         return;
  1348     }
  1349 
  1350     if (_this->MinimizeWindow) {
  1351         _this->MinimizeWindow(_this, window);
  1352     }
  1353     SDL_SendWindowEvent(window->id, SDL_WINDOWEVENT_MINIMIZED, 0, 0);
  1354 }
  1355 
  1356 void
  1357 SDL_RestoreWindow(SDL_WindowID windowID)
  1358 {
  1359     SDL_Window *window = SDL_GetWindowFromID(windowID);
  1360 
  1361     if (!window
  1362         || !(window->flags & (SDL_WINDOW_MAXIMIZED | SDL_WINDOW_MINIMIZED))) {
  1363         return;
  1364     }
  1365 
  1366     if (_this->RestoreWindow) {
  1367         _this->RestoreWindow(_this, window);
  1368     }
  1369     SDL_SendWindowEvent(window->id, SDL_WINDOWEVENT_RESTORED, 0, 0);
  1370 }
  1371 
  1372 int
  1373 SDL_SetWindowFullscreen(SDL_WindowID windowID, int fullscreen)
  1374 {
  1375     SDL_Window *window = SDL_GetWindowFromID(windowID);
  1376 
  1377     if (!window) {
  1378         return -1;
  1379     }
  1380     if (fullscreen) {
  1381         fullscreen = SDL_WINDOW_FULLSCREEN;
  1382     }
  1383     if ((window->flags & SDL_WINDOW_FULLSCREEN) == fullscreen) {
  1384         return 0;
  1385     }
  1386     if (fullscreen) {
  1387         window->flags |= SDL_WINDOW_FULLSCREEN;
  1388 
  1389         SDL_UpdateFullscreenMode(window, SDL_TRUE);
  1390     } else {
  1391         window->flags &= ~SDL_WINDOW_FULLSCREEN;
  1392 
  1393         SDL_UpdateFullscreenMode(window, SDL_FALSE);
  1394     }
  1395     return 0;
  1396 }
  1397 
  1398 void
  1399 SDL_SetWindowGrab(SDL_WindowID windowID, int mode)
  1400 {
  1401     SDL_Window *window = SDL_GetWindowFromID(windowID);
  1402 
  1403     if (!window || (!!mode == !!(window->flags & SDL_WINDOW_INPUT_GRABBED))) {
  1404         return;
  1405     }
  1406     if (mode) {
  1407         window->flags |= SDL_WINDOW_INPUT_GRABBED;
  1408     } else {
  1409         window->flags &= ~SDL_WINDOW_INPUT_GRABBED;
  1410     }
  1411     SDL_UpdateWindowGrab(window);
  1412 }
  1413 
  1414 static void
  1415 SDL_UpdateWindowGrab(SDL_Window * window)
  1416 {
  1417     if ((window->flags & SDL_WINDOW_INPUT_FOCUS) && _this->SetWindowGrab) {
  1418         _this->SetWindowGrab(_this, window);
  1419     }
  1420 }
  1421 
  1422 int
  1423 SDL_GetWindowGrab(SDL_WindowID windowID)
  1424 {
  1425     SDL_Window *window = SDL_GetWindowFromID(windowID);
  1426 
  1427     if (!window) {
  1428         return 0;
  1429     }
  1430     return ((window->flags & SDL_WINDOW_INPUT_GRABBED) != 0);
  1431 }
  1432 
  1433 void
  1434 SDL_OnWindowShown(SDL_Window * window)
  1435 {
  1436     SDL_RaiseWindow(window->id);
  1437     SDL_UpdateFullscreenMode(window, SDL_TRUE);
  1438 }
  1439 
  1440 void
  1441 SDL_OnWindowHidden(SDL_Window * window)
  1442 {
  1443     SDL_UpdateFullscreenMode(window, SDL_FALSE);
  1444 }
  1445 
  1446 void
  1447 SDL_OnWindowResized(SDL_Window * window)
  1448 {
  1449     SDL_Renderer *renderer = window->renderer;
  1450 
  1451     if (renderer && renderer->DisplayModeChanged) {
  1452         renderer->DisplayModeChanged(renderer);
  1453     }
  1454 }
  1455 
  1456 void
  1457 SDL_OnWindowMinimized(SDL_Window * window)
  1458 {
  1459     SDL_UpdateFullscreenMode(window, SDL_FALSE);
  1460 }
  1461 
  1462 void
  1463 SDL_OnWindowRestored(SDL_Window * window)
  1464 {
  1465     SDL_RaiseWindow(window->id);
  1466     SDL_UpdateFullscreenMode(window, SDL_TRUE);
  1467 }
  1468 
  1469 void
  1470 SDL_OnWindowFocusGained(SDL_Window * window)
  1471 {
  1472     SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);
  1473 
  1474     if (display->gamma && _this->SetDisplayGammaRamp) {
  1475         _this->SetDisplayGammaRamp(_this, display, display->gamma);
  1476     }
  1477     if ((window->flags & (SDL_WINDOW_INPUT_GRABBED | SDL_WINDOW_FULLSCREEN))
  1478         && _this->SetWindowGrab) {
  1479         _this->SetWindowGrab(_this, window);
  1480     }
  1481 }
  1482 
  1483 void
  1484 SDL_OnWindowFocusLost(SDL_Window * window)
  1485 {
  1486     SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);
  1487 
  1488     /* If we're fullscreen on a single-head system and lose focus, minimize */
  1489     if ((window->flags & SDL_WINDOW_FULLSCREEN) &&
  1490         _this->num_displays == 1) {
  1491         SDL_MinimizeWindow(window->id);
  1492     }
  1493 
  1494     if (display->gamma && _this->SetDisplayGammaRamp) {
  1495         _this->SetDisplayGammaRamp(_this, display, display->saved_gamma);
  1496     }
  1497     if ((window->flags & (SDL_WINDOW_INPUT_GRABBED | SDL_WINDOW_FULLSCREEN))
  1498         && _this->SetWindowGrab) {
  1499         _this->SetWindowGrab(_this, window);
  1500     }
  1501 }
  1502 
  1503 SDL_WindowID
  1504 SDL_GetFocusWindow(void)
  1505 {
  1506     SDL_VideoDisplay *display;
  1507     int i;
  1508 
  1509     if (!_this) {
  1510         return 0;
  1511     }
  1512     display = &SDL_CurrentDisplay;
  1513     for (i = 0; i < display->num_windows; ++i) {
  1514         SDL_Window *window = &display->windows[i];
  1515 
  1516         if (window->flags & SDL_WINDOW_INPUT_FOCUS) {
  1517             return window->id;
  1518         }
  1519     }
  1520     return 0;
  1521 }
  1522 
  1523 void
  1524 SDL_DestroyWindow(SDL_WindowID windowID)
  1525 {
  1526     int i, j;
  1527 
  1528     if (!_this) {
  1529         return;
  1530     }
  1531 
  1532     for (i = 0; i < _this->num_displays; ++i) {
  1533         SDL_VideoDisplay *display = &_this->displays[i];
  1534         for (j = 0; j < display->num_windows; ++j) {
  1535             SDL_Window *window = &display->windows[j];
  1536             if (window->id != windowID) {
  1537                 continue;
  1538             }
  1539             if (window->title) {
  1540                 SDL_free(window->title);
  1541                 window->title = NULL;
  1542             }
  1543             if (window->renderer) {
  1544                 SDL_DestroyRenderer(window->id);
  1545                 window->renderer = NULL;
  1546             }
  1547 
  1548             /* Restore video mode, etc. */
  1549             SDL_UpdateFullscreenMode(window, SDL_FALSE);
  1550 
  1551             if (_this->DestroyWindow) {
  1552                 _this->DestroyWindow(_this, window);
  1553             }
  1554             if (window->flags & SDL_WINDOW_OPENGL) {
  1555                 SDL_GL_UnloadLibrary();
  1556             }
  1557             if (j != display->num_windows - 1) {
  1558                 SDL_memcpy(&display->windows[i],
  1559                            &display->windows[i + 1],
  1560                            (display->num_windows - i - 1) * sizeof(*window));
  1561             }
  1562             --display->num_windows;
  1563             return;
  1564         }
  1565     }
  1566 }
  1567 
  1568 void
  1569 SDL_AddRenderDriver(SDL_VideoDisplay * display, const SDL_RenderDriver * driver)
  1570 {
  1571     SDL_RenderDriver *render_drivers;
  1572 
  1573     render_drivers =
  1574         SDL_realloc(display->render_drivers,
  1575                     (display->num_render_drivers +
  1576                      1) * sizeof(*render_drivers));
  1577     if (render_drivers) {
  1578         render_drivers[display->num_render_drivers] = *driver;
  1579         display->render_drivers = render_drivers;
  1580         display->num_render_drivers++;
  1581     }
  1582 }
  1583 
  1584 int
  1585 SDL_GetNumRenderDrivers(void)
  1586 {
  1587     if (_this) {
  1588         return SDL_CurrentDisplay.num_render_drivers;
  1589     }
  1590     return 0;
  1591 }
  1592 
  1593 int
  1594 SDL_GetRenderDriverInfo(int index, SDL_RendererInfo * info)
  1595 {
  1596     if (!_this) {
  1597         SDL_UninitializedVideo();
  1598         return -1;
  1599     }
  1600     if (index < 0 || index >= SDL_GetNumRenderDrivers()) {
  1601         SDL_SetError("index must be in the range of 0 - %d",
  1602                      SDL_GetNumRenderDrivers() - 1);
  1603         return -1;
  1604     }
  1605     *info = SDL_CurrentDisplay.render_drivers[index].info;
  1606     return 0;
  1607 }
  1608 
  1609 int
  1610 SDL_CreateRenderer(SDL_WindowID windowID, int index, Uint32 flags)
  1611 {
  1612     SDL_Window *window = SDL_GetWindowFromID(windowID);
  1613 
  1614     if (!window) {
  1615         SDL_SetError("Invalid window ID");
  1616         return -1;
  1617     }
  1618 
  1619     /* Free any existing renderer */
  1620     SDL_DestroyRenderer(windowID);
  1621 
  1622     if (index < 0) {
  1623         char *override = SDL_getenv("SDL_VIDEO_RENDERER");
  1624         int n = SDL_GetNumRenderDrivers();
  1625 
  1626 #if SDL_VIDEO_RENDER_OGL
  1627         if (!override && (window->flags & SDL_WINDOW_OPENGL)) {
  1628             override = "opengl";
  1629         }
  1630 #endif /* SDL_VIDEO_RENDER_OGL */
  1631 #if SDL_VIDEO_RENDER_OGL_ES
  1632         if (!override && (window->flags & SDL_WINDOW_OPENGL)) {
  1633             override = "opengl_es";
  1634         }
  1635 #endif /* SDL_VIDEO_RENDER_OGL_ES */
  1636         if (override) {
  1637             for (index = 0; index < n; ++index) {
  1638                 SDL_RenderDriver *driver =
  1639                     &SDL_CurrentDisplay.render_drivers[index];
  1640 
  1641                 if (SDL_strcasecmp(override, driver->info.name) == 0) {
  1642                     /* Create a new renderer instance */
  1643                     window->renderer = driver->CreateRenderer(window, flags);
  1644                     break;
  1645                 }
  1646             }
  1647         } else {
  1648             for (index = 0; index < n; ++index) {
  1649                 SDL_RenderDriver *driver =
  1650                     &SDL_CurrentDisplay.render_drivers[index];
  1651 
  1652                 if ((driver->info.flags & flags) == flags) {
  1653                     /* Create a new renderer instance */
  1654                     window->renderer = driver->CreateRenderer(window, flags);
  1655                     if (window->renderer) {
  1656                         /* Yay, we got one! */
  1657                         break;
  1658                     }
  1659                 }
  1660             }
  1661         }
  1662         if (index == n) {
  1663             SDL_SetError("Couldn't find matching render driver");
  1664             return -1;
  1665         }
  1666     } else {
  1667         if (index >= SDL_GetNumRenderDrivers()) {
  1668             SDL_SetError("index must be -1 or in the range of 0 - %d",
  1669                          SDL_GetNumRenderDrivers() - 1);
  1670             return -1;
  1671         }
  1672         /* Create a new renderer instance */
  1673         window->renderer = SDL_CurrentDisplay.render_drivers[index].CreateRenderer(window, flags);
  1674     }
  1675 
  1676     if (window->renderer == NULL) {
  1677         /* Assuming renderer set its error */
  1678         return -1;
  1679     }
  1680 
  1681     SDL_SelectRenderer(window->id);
  1682 
  1683     return 0;
  1684 }
  1685 
  1686 int
  1687 SDL_SelectRenderer(SDL_WindowID windowID)
  1688 {
  1689     SDL_Window *window = SDL_GetWindowFromID(windowID);
  1690     SDL_Renderer *renderer;
  1691 
  1692     if (!window) {
  1693         SDL_SetError("Invalid window ID");
  1694         return -1;
  1695     }
  1696     renderer = window->renderer;
  1697     if (!renderer) {
  1698         SDL_SetError("Use SDL_CreateRenderer() to create a renderer");
  1699         return -1;
  1700     }
  1701     if (renderer->ActivateRenderer) {
  1702         if (renderer->ActivateRenderer(renderer) < 0) {
  1703             return -1;
  1704         }
  1705     }
  1706     SDL_CurrentDisplay.current_renderer = renderer;
  1707     return 0;
  1708 }
  1709 
  1710 int
  1711 SDL_GetRendererInfo(SDL_RendererInfo * info)
  1712 {
  1713     SDL_Renderer *renderer = SDL_GetCurrentRenderer(SDL_FALSE);
  1714     if (!renderer) {
  1715         return -1;
  1716     }
  1717     *info = renderer->info;
  1718     return 0;
  1719 }
  1720 
  1721 SDL_TextureID
  1722 SDL_CreateTexture(Uint32 format, int access, int w, int h)
  1723 {
  1724     int hash;
  1725     SDL_Renderer *renderer;
  1726     SDL_Texture *texture;
  1727 
  1728     renderer = SDL_GetCurrentRenderer(SDL_TRUE);
  1729     if (!renderer) {
  1730         return 0;
  1731     }
  1732     if (!renderer->CreateTexture) {
  1733         SDL_Unsupported();
  1734         return 0;
  1735     }
  1736     texture = (SDL_Texture *) SDL_calloc(1, sizeof(*texture));
  1737     if (!texture) {
  1738         SDL_OutOfMemory();
  1739         return 0;
  1740     }
  1741     texture->id = _this->next_object_id++;
  1742     texture->format = format;
  1743     texture->access = access;
  1744     texture->w = w;
  1745     texture->h = h;
  1746     texture->r = 255;
  1747     texture->g = 255;
  1748     texture->b = 255;
  1749     texture->a = 255;
  1750     texture->renderer = renderer;
  1751 
  1752     if (renderer->CreateTexture(renderer, texture) < 0) {
  1753         if (renderer->DestroyTexture) {
  1754             renderer->DestroyTexture(renderer, texture);
  1755         }
  1756         SDL_free(texture);
  1757         return 0;
  1758     }
  1759     hash = (texture->id % SDL_arraysize(SDL_CurrentDisplay.textures));
  1760     texture->next = SDL_CurrentDisplay.textures[hash];
  1761     SDL_CurrentDisplay.textures[hash] = texture;
  1762 
  1763     return texture->id;
  1764 }
  1765 
  1766 SDL_TextureID
  1767 SDL_CreateTextureFromSurface(Uint32 format, SDL_Surface * surface)
  1768 {
  1769     SDL_TextureID textureID;
  1770     Uint32 requested_format = format;
  1771     SDL_PixelFormat *fmt;
  1772     SDL_Renderer *renderer;
  1773     int bpp;
  1774     Uint32 Rmask, Gmask, Bmask, Amask;
  1775 
  1776     if (!surface) {
  1777         SDL_SetError("SDL_CreateTextureFromSurface() passed NULL surface");
  1778         return 0;
  1779     }
  1780     fmt = surface->format;
  1781 
  1782     renderer = SDL_GetCurrentRenderer(SDL_TRUE);
  1783     if (!renderer) {
  1784         return 0;
  1785     }
  1786 
  1787     if (format) {
  1788         if (!SDL_PixelFormatEnumToMasks
  1789             (format, &bpp, &Rmask, &Gmask, &Bmask, &Amask)) {
  1790             SDL_SetError("Unknown pixel format");
  1791             return 0;
  1792         }
  1793     } else {
  1794         if (surface->format->Amask
  1795             || !(surface->map->info.flags &
  1796                  (SDL_COPY_COLORKEY | SDL_COPY_MASK | SDL_COPY_BLEND))) {
  1797             Uint32 it;
  1798             int pfmt;
  1799 
  1800             /* Pixel formats, sorted by best first */
  1801             static const Uint32 sdl_pformats[] = {
  1802                 SDL_PIXELFORMAT_ARGB8888,
  1803                 SDL_PIXELFORMAT_RGBA8888,
  1804                 SDL_PIXELFORMAT_ABGR8888,
  1805                 SDL_PIXELFORMAT_BGRA8888,
  1806                 SDL_PIXELFORMAT_RGB888,
  1807                 SDL_PIXELFORMAT_BGR888,
  1808                 SDL_PIXELFORMAT_RGB24,
  1809                 SDL_PIXELFORMAT_BGR24,
  1810                 SDL_PIXELFORMAT_RGB565,
  1811                 SDL_PIXELFORMAT_BGR565,
  1812                 SDL_PIXELFORMAT_ARGB1555,
  1813                 SDL_PIXELFORMAT_ABGR1555,
  1814                 SDL_PIXELFORMAT_RGB555,
  1815                 SDL_PIXELFORMAT_BGR555,
  1816                 SDL_PIXELFORMAT_ARGB4444,
  1817                 SDL_PIXELFORMAT_ABGR4444,
  1818                 SDL_PIXELFORMAT_RGB444,
  1819                 SDL_PIXELFORMAT_ARGB2101010,
  1820                 SDL_PIXELFORMAT_INDEX8,
  1821                 SDL_PIXELFORMAT_INDEX4LSB,
  1822                 SDL_PIXELFORMAT_INDEX4MSB,
  1823                 SDL_PIXELFORMAT_RGB332,
  1824                 SDL_PIXELFORMAT_INDEX1LSB,
  1825                 SDL_PIXELFORMAT_INDEX1MSB,
  1826                 SDL_PIXELFORMAT_UNKNOWN
  1827             };
  1828 
  1829             bpp = fmt->BitsPerPixel;
  1830             Rmask = fmt->Rmask;
  1831             Gmask = fmt->Gmask;
  1832             Bmask = fmt->Bmask;
  1833             Amask = fmt->Amask;
  1834 
  1835             format =
  1836                 SDL_MasksToPixelFormatEnum(bpp, Rmask, Gmask, Bmask, Amask);
  1837             if (!format) {
  1838                 SDL_SetError("Unknown pixel format");
  1839                 return 0;
  1840             }
  1841 
  1842             /* Search requested format in the supported texture */
  1843             /* formats by current renderer                      */
  1844             for (it = 0; it < renderer->info.num_texture_formats; it++) {
  1845                 if (renderer->info.texture_formats[it] == format) {
  1846                     break;
  1847                 }
  1848             }
  1849 
  1850             /* If requested format can't be found, search any best */
  1851             /* format which renderer provides                      */
  1852             if (it == renderer->info.num_texture_formats) {
  1853                 pfmt = 0;
  1854                 for (;;) {
  1855                     if (sdl_pformats[pfmt] == SDL_PIXELFORMAT_UNKNOWN) {
  1856                         break;
  1857                     }
  1858 
  1859                     for (it = 0; it < renderer->info.num_texture_formats;
  1860                          it++) {
  1861                         if (renderer->info.texture_formats[it] ==
  1862                             sdl_pformats[pfmt]) {
  1863                             break;
  1864                         }
  1865                     }
  1866 
  1867                     if (it != renderer->info.num_texture_formats) {
  1868                         /* The best format has been found */
  1869                         break;
  1870                     }
  1871                     pfmt++;
  1872                 }
  1873 
  1874                 /* If any format can't be found, then return an error */
  1875                 if (it == renderer->info.num_texture_formats) {
  1876                     SDL_SetError
  1877                         ("Any of the supported pixel formats can't be found");
  1878                     return 0;
  1879                 }
  1880 
  1881                 /* Convert found pixel format back to color masks */
  1882                 if (SDL_PixelFormatEnumToMasks
  1883                     (renderer->info.texture_formats[it], &bpp, &Rmask, &Gmask,
  1884                      &Bmask, &Amask) != SDL_TRUE) {
  1885                     SDL_SetError("Unknown pixel format");
  1886                     return 0;
  1887                 }
  1888             }
  1889         } else {
  1890             /* Need a format with alpha */
  1891             Uint32 it;
  1892             int apfmt;
  1893 
  1894             /* Pixel formats with alpha, sorted by best first */
  1895             static const Uint32 sdl_alpha_pformats[] = {
  1896                 SDL_PIXELFORMAT_ARGB8888,
  1897                 SDL_PIXELFORMAT_RGBA8888,
  1898                 SDL_PIXELFORMAT_ABGR8888,
  1899                 SDL_PIXELFORMAT_BGRA8888,
  1900                 SDL_PIXELFORMAT_ARGB1555,
  1901                 SDL_PIXELFORMAT_ABGR1555,
  1902                 SDL_PIXELFORMAT_ARGB4444,
  1903                 SDL_PIXELFORMAT_ABGR4444,
  1904                 SDL_PIXELFORMAT_ARGB2101010,
  1905                 SDL_PIXELFORMAT_UNKNOWN
  1906             };
  1907 
  1908             if (surface->format->Amask) {
  1909                 /* If surface already has alpha, then try an original */
  1910                 /* surface format first                               */
  1911                 bpp = fmt->BitsPerPixel;
  1912                 Rmask = fmt->Rmask;
  1913                 Gmask = fmt->Gmask;
  1914                 Bmask = fmt->Bmask;
  1915                 Amask = fmt->Amask;
  1916             } else {
  1917                 bpp = 32;
  1918                 Rmask = 0x00FF0000;
  1919                 Gmask = 0x0000FF00;
  1920                 Bmask = 0x000000FF;
  1921                 Amask = 0xFF000000;
  1922             }
  1923 
  1924             format =
  1925                 SDL_MasksToPixelFormatEnum(bpp, Rmask, Gmask, Bmask, Amask);
  1926             if (!format) {
  1927                 SDL_SetError("Unknown pixel format");
  1928                 return 0;
  1929             }
  1930 
  1931             /* Search this format in the supported texture formats */
  1932             /* by current renderer                                 */
  1933             for (it = 0; it < renderer->info.num_texture_formats; it++) {
  1934                 if (renderer->info.texture_formats[it] == format) {
  1935                     break;
  1936                 }
  1937             }
  1938 
  1939             /* If this format can't be found, search any best       */
  1940             /* compatible format with alpha which renderer provides */
  1941             if (it == renderer->info.num_texture_formats) {
  1942                 apfmt = 0;
  1943                 for (;;) {
  1944                     if (sdl_alpha_pformats[apfmt] == SDL_PIXELFORMAT_UNKNOWN) {
  1945                         break;
  1946                     }
  1947 
  1948                     for (it = 0; it < renderer->info.num_texture_formats;
  1949                          it++) {
  1950                         if (renderer->info.texture_formats[it] ==
  1951                             sdl_alpha_pformats[apfmt]) {
  1952                             break;
  1953                         }
  1954                     }
  1955 
  1956                     if (it != renderer->info.num_texture_formats) {
  1957                         /* Compatible format has been found */
  1958                         break;
  1959                     }
  1960                     apfmt++;
  1961                 }
  1962 
  1963                 /* If compatible format can't be found, then return an error */
  1964                 if (it == renderer->info.num_texture_formats) {
  1965                     SDL_SetError("Compatible pixel format can't be found");
  1966                     return 0;
  1967                 }
  1968 
  1969                 /* Convert found pixel format back to color masks */
  1970                 if (SDL_PixelFormatEnumToMasks
  1971                     (renderer->info.texture_formats[it], &bpp, &Rmask, &Gmask,
  1972                      &Bmask, &Amask) != SDL_TRUE) {
  1973                     SDL_SetError("Unknown pixel format");
  1974                     return 0;
  1975                 }
  1976             }
  1977         }
  1978 
  1979         format = SDL_MasksToPixelFormatEnum(bpp, Rmask, Gmask, Bmask, Amask);
  1980         if (!format) {
  1981             SDL_SetError("Unknown pixel format");
  1982             return 0;
  1983         }
  1984     }
  1985 
  1986     textureID =
  1987         SDL_CreateTexture(format, SDL_TEXTUREACCESS_STATIC, surface->w,
  1988                           surface->h);
  1989     if (!textureID && !requested_format) {
  1990         SDL_DisplayMode desktop_mode;
  1991         SDL_GetDesktopDisplayMode(&desktop_mode);
  1992         format = desktop_mode.format;
  1993         textureID =
  1994             SDL_CreateTexture(format, SDL_TEXTUREACCESS_STATIC, surface->w,
  1995                               surface->h);
  1996     }
  1997     if (!textureID) {
  1998         return 0;
  1999     }
  2000     if (bpp == fmt->BitsPerPixel && Rmask == fmt->Rmask && Gmask == fmt->Gmask
  2001         && Bmask == fmt->Bmask && Amask == fmt->Amask) {
  2002         if (SDL_MUSTLOCK(surface)) {
  2003             SDL_LockSurface(surface);
  2004             SDL_UpdateTexture(textureID, NULL, surface->pixels,
  2005                               surface->pitch);
  2006             SDL_UnlockSurface(surface);
  2007         } else {
  2008             SDL_UpdateTexture(textureID, NULL, surface->pixels,
  2009                               surface->pitch);
  2010         }
  2011     } else {
  2012         SDL_PixelFormat dst_fmt;
  2013         SDL_Surface *dst = NULL;
  2014 
  2015         /* Set up a destination surface for the texture update */
  2016         SDL_InitFormat(&dst_fmt, bpp, Rmask, Gmask, Bmask, Amask);
  2017         if (SDL_ISPIXELFORMAT_INDEXED(format)) {
  2018             dst_fmt.palette =
  2019                 SDL_AllocPalette((1 << SDL_BITSPERPIXEL(format)));
  2020             if (dst_fmt.palette) {
  2021                 /*
  2022                  * FIXME: Should we try to copy
  2023                  * fmt->palette?
  2024                  */
  2025                 SDL_DitherColors(dst_fmt.palette->colors,
  2026                                  SDL_BITSPERPIXEL(format));
  2027             }
  2028         }
  2029         dst = SDL_ConvertSurface(surface, &dst_fmt, 0);
  2030         if (dst) {
  2031             SDL_UpdateTexture(textureID, NULL, dst->pixels, dst->pitch);
  2032             SDL_FreeSurface(dst);
  2033         }
  2034         if (dst_fmt.palette) {
  2035             SDL_FreePalette(dst_fmt.palette);
  2036         }
  2037         if (!dst) {
  2038             SDL_DestroyTexture(textureID);
  2039             return 0;
  2040         }
  2041     }
  2042 
  2043     {
  2044         Uint8 r, g, b, a;
  2045         int blendMode;
  2046         int scaleMode;
  2047 
  2048         SDL_GetSurfaceColorMod(surface, &r, &g, &b);
  2049         SDL_SetTextureColorMod(textureID, r, g, b);
  2050 
  2051         SDL_GetSurfaceAlphaMod(surface, &a);
  2052         SDL_SetTextureAlphaMod(textureID, a);
  2053 
  2054         SDL_GetSurfaceBlendMode(surface, &blendMode);
  2055         SDL_SetTextureBlendMode(textureID, blendMode);
  2056 
  2057         SDL_GetSurfaceScaleMode(surface, &scaleMode);
  2058         SDL_SetTextureScaleMode(textureID, scaleMode);
  2059     }
  2060 
  2061     if (SDL_ISPIXELFORMAT_INDEXED(format) && fmt->palette) {
  2062         SDL_SetTexturePalette(textureID, fmt->palette->colors, 0,
  2063                               fmt->palette->ncolors);
  2064     }
  2065     return textureID;
  2066 }
  2067 
  2068 static __inline__ SDL_Texture *
  2069 SDL_GetTextureFromID(SDL_TextureID textureID)
  2070 {
  2071     int hash;
  2072     SDL_Texture *texture;
  2073 
  2074     if (!_this) {
  2075         return NULL;
  2076     }
  2077     hash = (textureID % SDL_arraysize(SDL_CurrentDisplay.textures));
  2078     for (texture = SDL_CurrentDisplay.textures[hash]; texture;
  2079          texture = texture->next) {
  2080         if (texture->id == textureID) {
  2081             return texture;
  2082         }
  2083     }
  2084     return NULL;
  2085 }
  2086 
  2087 int
  2088 SDL_QueryTexture(SDL_TextureID textureID, Uint32 * format, int *access,
  2089                  int *w, int *h)
  2090 {
  2091     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  2092 
  2093     if (!texture) {
  2094         return -1;
  2095     }
  2096     if (format) {
  2097         *format = texture->format;
  2098     }
  2099     if (access) {
  2100         *access = texture->access;
  2101     }
  2102     if (w) {
  2103         *w = texture->w;
  2104     }
  2105     if (h) {
  2106         *h = texture->h;
  2107     }
  2108     return 0;
  2109 }
  2110 
  2111 int
  2112 SDL_QueryTexturePixels(SDL_TextureID textureID, void **pixels, int *pitch)
  2113 {
  2114     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  2115     SDL_Renderer *renderer;
  2116 
  2117     if (!texture) {
  2118         return -1;
  2119     }
  2120     renderer = texture->renderer;
  2121     if (!renderer->QueryTexturePixels) {
  2122         SDL_Unsupported();
  2123         return -1;
  2124     }
  2125     return renderer->QueryTexturePixels(renderer, texture, pixels, pitch);
  2126 }
  2127 
  2128 int
  2129 SDL_SetTexturePalette(SDL_TextureID textureID, const SDL_Color * colors,
  2130                       int firstcolor, int ncolors)
  2131 {
  2132     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  2133     SDL_Renderer *renderer;
  2134 
  2135     if (!texture) {
  2136         return -1;
  2137     }
  2138     renderer = texture->renderer;
  2139     if (!renderer->SetTexturePalette) {
  2140         SDL_Unsupported();
  2141         return -1;
  2142     }
  2143     return renderer->SetTexturePalette(renderer, texture, colors, firstcolor,
  2144                                        ncolors);
  2145 }
  2146 
  2147 int
  2148 SDL_GetTexturePalette(SDL_TextureID textureID, SDL_Color * colors,
  2149                       int firstcolor, int ncolors)
  2150 {
  2151     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  2152     SDL_Renderer *renderer;
  2153 
  2154     if (!texture) {
  2155         return -1;
  2156     }
  2157     renderer = texture->renderer;
  2158     if (!renderer->GetTexturePalette) {
  2159         SDL_Unsupported();
  2160         return -1;
  2161     }
  2162     return renderer->GetTexturePalette(renderer, texture, colors, firstcolor,
  2163                                        ncolors);
  2164 }
  2165 
  2166 int
  2167 SDL_SetTextureColorMod(SDL_TextureID textureID, Uint8 r, Uint8 g, Uint8 b)
  2168 {
  2169     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  2170     SDL_Renderer *renderer;
  2171 
  2172     if (!texture) {
  2173         return -1;
  2174     }
  2175     renderer = texture->renderer;
  2176     if (!renderer->SetTextureColorMod) {
  2177         SDL_Unsupported();
  2178         return -1;
  2179     }
  2180     if (r < 255 || g < 255 || b < 255) {
  2181         texture->modMode |= SDL_TEXTUREMODULATE_COLOR;
  2182     } else {
  2183         texture->modMode &= ~SDL_TEXTUREMODULATE_COLOR;
  2184     }
  2185     texture->r = r;
  2186     texture->g = g;
  2187     texture->b = b;
  2188     return renderer->SetTextureColorMod(renderer, texture);
  2189 }
  2190 
  2191 int
  2192 SDL_GetTextureColorMod(SDL_TextureID textureID, Uint8 * r, Uint8 * g,
  2193                        Uint8 * b)
  2194 {
  2195     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  2196     SDL_Renderer *renderer;
  2197 
  2198     if (!texture) {
  2199         return -1;
  2200     }
  2201     renderer = texture->renderer;
  2202     if (r) {
  2203         *r = texture->r;
  2204     }
  2205     if (g) {
  2206         *g = texture->g;
  2207     }
  2208     if (b) {
  2209         *b = texture->b;
  2210     }
  2211     return 0;
  2212 }
  2213 
  2214 int
  2215 SDL_SetTextureAlphaMod(SDL_TextureID textureID, Uint8 alpha)
  2216 {
  2217     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  2218     SDL_Renderer *renderer;
  2219 
  2220     if (!texture) {
  2221         return -1;
  2222     }
  2223     renderer = texture->renderer;
  2224     if (!renderer->SetTextureAlphaMod) {
  2225         SDL_Unsupported();
  2226         return -1;
  2227     }
  2228     if (alpha < 255) {
  2229         texture->modMode |= SDL_TEXTUREMODULATE_ALPHA;
  2230     } else {
  2231         texture->modMode &= ~SDL_TEXTUREMODULATE_ALPHA;
  2232     }
  2233     texture->a = alpha;
  2234     return renderer->SetTextureAlphaMod(renderer, texture);
  2235 }
  2236 
  2237 int
  2238 SDL_GetTextureAlphaMod(SDL_TextureID textureID, Uint8 * alpha)
  2239 {
  2240     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  2241 
  2242     if (!texture) {
  2243         return -1;
  2244     }
  2245     if (alpha) {
  2246         *alpha = texture->a;
  2247     }
  2248     return 0;
  2249 }
  2250 
  2251 int
  2252 SDL_SetTextureBlendMode(SDL_TextureID textureID, int blendMode)
  2253 {
  2254     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  2255     SDL_Renderer *renderer;
  2256 
  2257     if (!texture) {
  2258         return -1;
  2259     }
  2260     renderer = texture->renderer;
  2261     if (!renderer->SetTextureBlendMode) {
  2262         SDL_Unsupported();
  2263         return -1;
  2264     }
  2265     texture->blendMode = blendMode;
  2266     return renderer->SetTextureBlendMode(renderer, texture);
  2267 }
  2268 
  2269 int
  2270 SDL_GetTextureBlendMode(SDL_TextureID textureID, int *blendMode)
  2271 {
  2272     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  2273 
  2274     if (!texture) {
  2275         return -1;
  2276     }
  2277     if (blendMode) {
  2278         *blendMode = texture->blendMode;
  2279     }
  2280     return 0;
  2281 }
  2282 
  2283 int
  2284 SDL_SetTextureScaleMode(SDL_TextureID textureID, int scaleMode)
  2285 {
  2286     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  2287     SDL_Renderer *renderer;
  2288 
  2289     if (!texture) {
  2290         return -1;
  2291     }
  2292     renderer = texture->renderer;
  2293     if (!renderer->SetTextureScaleMode) {
  2294         SDL_Unsupported();
  2295         return -1;
  2296     }
  2297     texture->scaleMode = scaleMode;
  2298     return renderer->SetTextureScaleMode(renderer, texture);
  2299 }
  2300 
  2301 int
  2302 SDL_GetTextureScaleMode(SDL_TextureID textureID, int *scaleMode)
  2303 {
  2304     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  2305 
  2306     if (!texture) {
  2307         return -1;
  2308     }
  2309     if (scaleMode) {
  2310         *scaleMode = texture->scaleMode;
  2311     }
  2312     return 0;
  2313 }
  2314 
  2315 int
  2316 SDL_UpdateTexture(SDL_TextureID textureID, const SDL_Rect * rect,
  2317                   const void *pixels, int pitch)
  2318 {
  2319     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  2320     SDL_Renderer *renderer;
  2321     SDL_Rect full_rect;
  2322 
  2323     if (!texture) {
  2324         return -1;
  2325     }
  2326     renderer = texture->renderer;
  2327     if (!renderer->UpdateTexture) {
  2328         SDL_Unsupported();
  2329         return -1;
  2330     }
  2331     if (!rect) {
  2332         full_rect.x = 0;
  2333         full_rect.y = 0;
  2334         full_rect.w = texture->w;
  2335         full_rect.h = texture->h;
  2336         rect = &full_rect;
  2337     }
  2338     return renderer->UpdateTexture(renderer, texture, rect, pixels, pitch);
  2339 }
  2340 
  2341 int
  2342 SDL_LockTexture(SDL_TextureID textureID, const SDL_Rect * rect, int markDirty,
  2343                 void **pixels, int *pitch)
  2344 {
  2345     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  2346     SDL_Renderer *renderer;
  2347     SDL_Rect full_rect;
  2348 
  2349     if (!texture) {
  2350         return -1;
  2351     }
  2352     if (texture->access != SDL_TEXTUREACCESS_STREAMING) {
  2353         SDL_SetError("SDL_LockTexture(): texture must be streaming");
  2354         return -1;
  2355     }
  2356     renderer = texture->renderer;
  2357     if (!renderer->LockTexture) {
  2358         SDL_Unsupported();
  2359         return -1;
  2360     }
  2361     if (!rect) {
  2362         full_rect.x = 0;
  2363         full_rect.y = 0;
  2364         full_rect.w = texture->w;
  2365         full_rect.h = texture->h;
  2366         rect = &full_rect;
  2367     }
  2368     return renderer->LockTexture(renderer, texture, rect, markDirty, pixels,
  2369                                  pitch);
  2370 }
  2371 
  2372 void
  2373 SDL_UnlockTexture(SDL_TextureID textureID)
  2374 {
  2375     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  2376     SDL_Renderer *renderer;
  2377 
  2378     if (!texture) {
  2379         return;
  2380     }
  2381     if (texture->access != SDL_TEXTUREACCESS_STREAMING) {
  2382         return;
  2383     }
  2384     renderer = texture->renderer;
  2385     if (!renderer->UnlockTexture) {
  2386         return;
  2387     }
  2388     renderer->UnlockTexture(renderer, texture);
  2389 }
  2390 
  2391 void
  2392 SDL_DirtyTexture(SDL_TextureID textureID, int numrects,
  2393                  const SDL_Rect * rects)
  2394 {
  2395     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  2396     SDL_Renderer *renderer;
  2397 
  2398     if (!texture) {
  2399         return;
  2400     }
  2401     if (texture->access != SDL_TEXTUREACCESS_STREAMING) {
  2402         return;
  2403     }
  2404     renderer = texture->renderer;
  2405     if (!renderer->DirtyTexture) {
  2406         return;
  2407     }
  2408     renderer->DirtyTexture(renderer, texture, numrects, rects);
  2409 }
  2410 
  2411 int
  2412 SDL_SetRenderDrawColor(Uint8 r, Uint8 g, Uint8 b, Uint8 a)
  2413 {
  2414     SDL_Renderer *renderer;
  2415 
  2416     renderer = SDL_GetCurrentRenderer(SDL_TRUE);
  2417     if (!renderer) {
  2418         return -1;
  2419     }
  2420     renderer->r = r;
  2421     renderer->g = g;
  2422     renderer->b = b;
  2423     renderer->a = a;
  2424     if (renderer->SetDrawColor) {
  2425         return renderer->SetDrawColor(renderer);
  2426     } else {
  2427         return 0;
  2428     }
  2429 }
  2430 
  2431 int
  2432 SDL_GetRenderDrawColor(Uint8 * r, Uint8 * g, Uint8 * b, Uint8 * a)
  2433 {
  2434     SDL_Renderer *renderer;
  2435 
  2436     renderer = SDL_GetCurrentRenderer(SDL_TRUE);
  2437     if (!renderer) {
  2438         return -1;
  2439     }
  2440     if (r) {
  2441         *r = renderer->r;
  2442     }
  2443     if (g) {
  2444         *g = renderer->g;
  2445     }
  2446     if (b) {
  2447         *b = renderer->b;
  2448     }
  2449     if (a) {
  2450         *a = renderer->a;
  2451     }
  2452     return 0;
  2453 }
  2454 
  2455 int
  2456 SDL_SetRenderDrawBlendMode(int blendMode)
  2457 {
  2458     SDL_Renderer *renderer;
  2459 
  2460     renderer = SDL_GetCurrentRenderer(SDL_TRUE);
  2461     if (!renderer) {
  2462         return -1;
  2463     }
  2464     renderer->blendMode = blendMode;
  2465     if (renderer->SetDrawBlendMode) {
  2466         return renderer->SetDrawBlendMode(renderer);
  2467     } else {
  2468         return 0;
  2469     }
  2470 }
  2471 
  2472 int
  2473 SDL_GetRenderDrawBlendMode(int *blendMode)
  2474 {
  2475     SDL_Renderer *renderer;
  2476 
  2477     renderer = SDL_GetCurrentRenderer(SDL_TRUE);
  2478     if (!renderer) {
  2479         return -1;
  2480     }
  2481     *blendMode = renderer->blendMode;
  2482     return 0;
  2483 }
  2484 
  2485 int
  2486 SDL_RenderPoint(int x, int y)
  2487 {
  2488     SDL_Point point;
  2489 
  2490     point.x = x;
  2491     point.y = y;
  2492     return SDL_RenderPoints(&point, 1);
  2493 }
  2494 
  2495 int
  2496 SDL_RenderPoints(const SDL_Point * points, int count)
  2497 {
  2498     SDL_Renderer *renderer;
  2499 
  2500     if (!points) {
  2501         SDL_SetError("SDL_RenderPoints(): Passed NULL points");
  2502         return -1;
  2503     }
  2504 
  2505     renderer = SDL_GetCurrentRenderer(SDL_TRUE);
  2506     if (!renderer) {
  2507         return -1;
  2508     }
  2509     if (!renderer->RenderPoints) {
  2510         SDL_Unsupported();
  2511         return -1;
  2512     }
  2513     if (count < 1) {
  2514         return 0;
  2515     }
  2516     return renderer->RenderPoints(renderer, points, count);
  2517 }
  2518 
  2519 int
  2520 SDL_RenderLine(int x1, int y1, int x2, int y2)
  2521 {
  2522     SDL_Point points[2];
  2523 
  2524     points[0].x = x1;
  2525     points[0].y = y1;
  2526     points[1].x = x2;
  2527     points[1].y = y2;
  2528     return SDL_RenderLines(points, 2);
  2529 }
  2530 
  2531 int
  2532 SDL_RenderLines(const SDL_Point * points, int count)
  2533 {
  2534     SDL_Renderer *renderer;
  2535 
  2536     if (!points) {
  2537         SDL_SetError("SDL_RenderLines(): Passed NULL points");
  2538         return -1;
  2539     }
  2540 
  2541     renderer = SDL_GetCurrentRenderer(SDL_TRUE);
  2542     if (!renderer) {
  2543         return -1;
  2544     }
  2545     if (!renderer->RenderLines) {
  2546         SDL_Unsupported();
  2547         return -1;
  2548     }
  2549     if (count < 2) {
  2550         return 0;
  2551     }
  2552     return renderer->RenderLines(renderer, points, count);
  2553 }
  2554 
  2555 int
  2556 SDL_RenderRect(const SDL_Rect * rect)
  2557 {
  2558     return SDL_RenderRects(&rect, 1);
  2559 }
  2560 
  2561 int
  2562 SDL_RenderRects(const SDL_Rect ** rects, int count)
  2563 {
  2564     SDL_Renderer *renderer;
  2565     int i;
  2566 
  2567     if (!rects) {
  2568         SDL_SetError("SDL_RenderRects(): Passed NULL rects");
  2569         return -1;
  2570     }
  2571 
  2572     renderer = SDL_GetCurrentRenderer(SDL_TRUE);
  2573     if (!renderer) {
  2574         return -1;
  2575     }
  2576     if (!renderer->RenderRects) {
  2577         SDL_Unsupported();
  2578         return -1;
  2579     }
  2580     if (count < 1) {
  2581         return 0;
  2582     }
  2583     /* Check for NULL rect, which means fill entire window */
  2584     for (i = 0; i < count; ++i) {
  2585         if (rects[i] == NULL) {
  2586             SDL_Window *window;
  2587             SDL_Rect full_rect;
  2588             const SDL_Rect *rect;
  2589 
  2590             window = SDL_GetWindowFromID(renderer->window);
  2591             full_rect.x = 0;
  2592             full_rect.y = 0;
  2593             full_rect.w = window->w;
  2594             full_rect.h = window->h;
  2595             rect = &full_rect;
  2596             return renderer->RenderRects(renderer, &rect, 1);
  2597         }
  2598     }
  2599     return renderer->RenderRects(renderer, rects, count);
  2600 }
  2601 
  2602 int
  2603 SDL_RenderCopy(SDL_TextureID textureID, const SDL_Rect * srcrect,
  2604                const SDL_Rect * dstrect)
  2605 {
  2606     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  2607     SDL_Renderer *renderer;
  2608     SDL_Window *window;
  2609     SDL_Rect real_srcrect;
  2610     SDL_Rect real_dstrect;
  2611 
  2612     renderer = SDL_GetCurrentRenderer(SDL_TRUE);
  2613     if (!renderer) {
  2614         return -1;
  2615     }
  2616     if (!texture) {
  2617         SDL_SetError("Texture not found");
  2618         return -1;
  2619     }
  2620     if (texture->renderer != renderer) {
  2621         SDL_SetError("Texture was not created with this renderer");
  2622         return -1;
  2623     }
  2624     if (!renderer->RenderCopy) {
  2625         SDL_Unsupported();
  2626         return -1;
  2627     }
  2628     window = SDL_GetWindowFromID(renderer->window);
  2629 
  2630     real_srcrect.x = 0;
  2631     real_srcrect.y = 0;
  2632     real_srcrect.w = texture->w;
  2633     real_srcrect.h = texture->h;
  2634     if (srcrect) {
  2635         if (!SDL_IntersectRect(srcrect, &real_srcrect, &real_srcrect)) {
  2636             return 0;
  2637         }
  2638     }
  2639 
  2640     real_dstrect.x = 0;
  2641     real_dstrect.y = 0;
  2642     real_dstrect.w = window->w;
  2643     real_dstrect.h = window->h;
  2644     if (dstrect) {
  2645         if (!SDL_IntersectRect(dstrect, &real_dstrect, &real_dstrect)) {
  2646             return 0;
  2647         }
  2648         /* Clip srcrect by the same amount as dstrect was clipped */
  2649         if (dstrect->w != real_dstrect.w) {
  2650             int deltax = (real_dstrect.x - dstrect->x);
  2651             int deltaw = (real_dstrect.w - dstrect->w);
  2652             real_srcrect.x += (deltax * real_srcrect.w) / dstrect->w;
  2653             real_srcrect.w += (deltaw * real_srcrect.w) / dstrect->w;
  2654         }
  2655         if (dstrect->h != real_dstrect.h) {
  2656             int deltay = (real_dstrect.y - dstrect->y);
  2657             int deltah = (real_dstrect.h - dstrect->h);
  2658             real_srcrect.y += (deltay * real_srcrect.h) / dstrect->h;
  2659             real_srcrect.h += (deltah * real_srcrect.h) / dstrect->h;
  2660         }
  2661     }
  2662 
  2663     return renderer->RenderCopy(renderer, texture, &real_srcrect,
  2664                                 &real_dstrect);
  2665 }
  2666 
  2667 int
  2668 SDL_RenderReadPixels(const SDL_Rect * rect, Uint32 format,
  2669                      void * pixels, int pitch)
  2670 {
  2671     SDL_Renderer *renderer;
  2672     SDL_Window *window;
  2673     SDL_Rect real_rect;
  2674 
  2675     renderer = SDL_GetCurrentRenderer(SDL_TRUE);
  2676     if (!renderer) {
  2677         return -1;
  2678     }
  2679     if (!renderer->RenderReadPixels) {
  2680         SDL_Unsupported();
  2681         return -1;
  2682     }
  2683     window = SDL_GetWindowFromID(renderer->window);
  2684 
  2685     if (!format) {
  2686         format = SDL_GetDisplayFromWindow(window)->current_mode.format;
  2687     }
  2688 
  2689     real_rect.x = 0;
  2690     real_rect.y = 0;
  2691     real_rect.w = window->w;
  2692     real_rect.h = window->h;
  2693     if (rect) {
  2694         if (!SDL_IntersectRect(rect, &real_rect, &real_rect)) {
  2695             return 0;
  2696         }
  2697         if (real_rect.y > rect->y) {
  2698             pixels = (Uint8 *)pixels + pitch * (real_rect.y - rect->y);
  2699         }
  2700         if (real_rect.x > rect->x) {
  2701             Uint32 format = SDL_CurrentDisplay.current_mode.format;
  2702             int bpp = SDL_BYTESPERPIXEL(format);
  2703             pixels = (Uint8 *)pixels + bpp * (real_rect.x - rect->x);
  2704         }
  2705     }
  2706 
  2707     return renderer->RenderReadPixels(renderer, &real_rect,
  2708                                       format, pixels, pitch);
  2709 }
  2710 
  2711 int
  2712 SDL_RenderWritePixels(const SDL_Rect * rect, Uint32 format,
  2713                       const void * pixels, int pitch)
  2714 {
  2715     SDL_Renderer *renderer;
  2716     SDL_Window *window;
  2717     SDL_Rect real_rect;
  2718 
  2719     renderer = SDL_GetCurrentRenderer(SDL_TRUE);
  2720     if (!renderer) {
  2721         return -1;
  2722     }
  2723     if (!renderer->RenderWritePixels) {
  2724         SDL_Unsupported();
  2725         return -1;
  2726     }
  2727     window = SDL_GetWindowFromID(renderer->window);
  2728 
  2729     if (!format) {
  2730         format = SDL_GetDisplayFromWindow(window)->current_mode.format;
  2731     }
  2732 
  2733     real_rect.x = 0;
  2734     real_rect.y = 0;
  2735     real_rect.w = window->w;
  2736     real_rect.h = window->h;
  2737     if (rect) {
  2738         if (!SDL_IntersectRect(rect, &real_rect, &real_rect)) {
  2739             return 0;
  2740         }
  2741         if (real_rect.y > rect->y) {
  2742             pixels = (const Uint8 *)pixels + pitch * (real_rect.y - rect->y);
  2743         }
  2744         if (real_rect.x > rect->x) {
  2745             Uint32 format = SDL_CurrentDisplay.current_mode.format;
  2746             int bpp = SDL_BYTESPERPIXEL(format);
  2747             pixels = (const Uint8 *)pixels + bpp * (real_rect.x - rect->x);
  2748         }
  2749     }
  2750 
  2751     return renderer->RenderWritePixels(renderer, &real_rect,
  2752                                        format, pixels, pitch);
  2753 }
  2754 
  2755 void
  2756 SDL_RenderPresent(void)
  2757 {
  2758     SDL_Renderer *renderer;
  2759 
  2760     renderer = SDL_GetCurrentRenderer(SDL_TRUE);
  2761     if (!renderer || !renderer->RenderPresent) {
  2762         return;
  2763     }
  2764     renderer->RenderPresent(renderer);
  2765 }
  2766 
  2767 void
  2768 SDL_DestroyTexture(SDL_TextureID textureID)
  2769 {
  2770     int hash;
  2771     SDL_Texture *prev, *texture;
  2772     SDL_Renderer *renderer;
  2773 
  2774     if (!_this) {
  2775         SDL_UninitializedVideo();
  2776         return;
  2777     }
  2778     /* Look up the texture in the hash table */
  2779     hash = (textureID % SDL_arraysize(SDL_CurrentDisplay.textures));
  2780     prev = NULL;
  2781     for (texture = SDL_CurrentDisplay.textures[hash]; texture;
  2782          prev = texture, texture = texture->next) {
  2783         if (texture->id == textureID) {
  2784             break;
  2785         }
  2786     }
  2787     if (!texture) {
  2788         return;
  2789     }
  2790     /* Unlink the texture from the list */
  2791     if (prev) {
  2792         prev->next = texture->next;
  2793     } else {
  2794         SDL_CurrentDisplay.textures[hash] = texture->next;
  2795     }
  2796 
  2797     /* Free the texture */
  2798     renderer = texture->renderer;
  2799     renderer->DestroyTexture(renderer, texture);
  2800     SDL_free(texture);
  2801 }
  2802 
  2803 void
  2804 SDL_DestroyRenderer(SDL_WindowID windowID)
  2805 {
  2806     SDL_Window *window = SDL_GetWindowFromID(windowID);
  2807     SDL_Renderer *renderer;
  2808     int i;
  2809 
  2810     if (!window) {
  2811         return;
  2812     }
  2813     renderer = window->renderer;
  2814     if (!renderer) {
  2815         return;
  2816     }
  2817     /* Free existing textures for this renderer */
  2818     for (i = 0; i < SDL_arraysize(SDL_CurrentDisplay.textures); ++i) {
  2819         SDL_Texture *texture;
  2820         SDL_Texture *prev = NULL;
  2821         SDL_Texture *next;
  2822         for (texture = SDL_CurrentDisplay.textures[i]; texture;
  2823              texture = next) {
  2824             next = texture->next;
  2825             if (texture->renderer == renderer) {
  2826                 if (prev) {
  2827                     prev->next = next;
  2828                 } else {
  2829                     SDL_CurrentDisplay.textures[i] = next;
  2830                 }
  2831                 renderer->DestroyTexture(renderer, texture);
  2832                 SDL_free(texture);
  2833             } else {
  2834                 prev = texture;
  2835             }
  2836         }
  2837     }
  2838 
  2839     /* Free the renderer instance */
  2840     renderer->DestroyRenderer(renderer);
  2841 
  2842     /* Clear references */
  2843     window->renderer = NULL;
  2844     if (SDL_CurrentDisplay.current_renderer == renderer) {
  2845         SDL_CurrentDisplay.current_renderer = NULL;
  2846     }
  2847 }
  2848 
  2849 SDL_bool
  2850 SDL_IsScreenSaverEnabled()
  2851 {
  2852     if (!_this) {
  2853         return SDL_TRUE;
  2854     }
  2855     return _this->suspend_screensaver ? SDL_FALSE : SDL_TRUE;
  2856 }
  2857 
  2858 void
  2859 SDL_EnableScreenSaver()
  2860 {
  2861     if (!_this) {
  2862         return;
  2863     }
  2864     if (!_this->suspend_screensaver) {
  2865         return;
  2866     }
  2867     _this->suspend_screensaver = SDL_FALSE;
  2868     if (_this->SuspendScreenSaver) {
  2869         _this->SuspendScreenSaver(_this);
  2870     }
  2871 }
  2872 
  2873 void
  2874 SDL_DisableScreenSaver()
  2875 {
  2876     if (!_this) {
  2877         return;
  2878     }
  2879     if (_this->suspend_screensaver) {
  2880         return;
  2881     }
  2882     _this->suspend_screensaver = SDL_TRUE;
  2883     if (_this->SuspendScreenSaver) {
  2884         _this->SuspendScreenSaver(_this);
  2885     }
  2886 }
  2887 
  2888 void
  2889 SDL_VideoQuit(void)
  2890 {
  2891     int i, j;
  2892 
  2893     if (!_this) {
  2894         return;
  2895     }
  2896     /* Halt event processing before doing anything else */
  2897     SDL_StopEventLoop();
  2898     SDL_EnableScreenSaver();
  2899 
  2900     /* Clean up the system video */
  2901     for (i = _this->num_displays; i--;) {
  2902         SDL_VideoDisplay *display = &_this->displays[i];
  2903         for (j = display->num_windows; j--;) {
  2904             SDL_DestroyWindow(display->windows[i].id);
  2905         }
  2906         if (display->windows) {
  2907             SDL_free(display->windows);
  2908             display->windows = NULL;
  2909         }
  2910         display->num_windows = 0;
  2911         if (display->render_drivers) {
  2912             SDL_free(display->render_drivers);
  2913             display->render_drivers = NULL;
  2914         }
  2915         display->num_render_drivers = 0;
  2916     }
  2917     _this->VideoQuit(_this);
  2918 
  2919     for (i = _this->num_displays; i--;) {
  2920         SDL_VideoDisplay *display = &_this->displays[i];
  2921         for (j = display->num_display_modes; j--;) {
  2922             if (display->display_modes[j].driverdata) {
  2923                 SDL_free(display->display_modes[j].driverdata);
  2924                 display->display_modes[j].driverdata = NULL;
  2925             }
  2926         }
  2927         if (display->display_modes) {
  2928             SDL_free(display->display_modes);
  2929             display->display_modes = NULL;
  2930         }
  2931         if (display->desktop_mode.driverdata) {
  2932             SDL_free(display->desktop_mode.driverdata);
  2933             display->desktop_mode.driverdata = NULL;
  2934         }
  2935         if (display->palette) {
  2936             SDL_FreePalette(display->palette);
  2937             display->palette = NULL;
  2938         }
  2939         if (display->gamma) {
  2940             SDL_free(display->gamma);
  2941             display->gamma = NULL;
  2942         }
  2943         if (display->driverdata) {
  2944             SDL_free(display->driverdata);
  2945             display->driverdata = NULL;
  2946         }
  2947     }
  2948     if (_this->displays) {
  2949         SDL_free(_this->displays);
  2950         _this->displays = NULL;
  2951     }
  2952     _this->free(_this);
  2953     _this = NULL;
  2954 }
  2955 
  2956 int
  2957 SDL_GL_LoadLibrary(const char *path)
  2958 {
  2959     int retval;
  2960 
  2961     if (!_this) {
  2962         SDL_UninitializedVideo();
  2963         return -1;
  2964     }
  2965     if (_this->gl_config.driver_loaded) {
  2966         if (path && SDL_strcmp(path, _this->gl_config.driver_path) != 0) {
  2967             SDL_SetError("OpenGL library already loaded");
  2968             return -1;
  2969         }
  2970         retval = 0;
  2971     } else {
  2972         if (!_this->GL_LoadLibrary) {
  2973             SDL_SetError("No dynamic GL support in video driver");
  2974             return -1;
  2975         }
  2976         retval = _this->GL_LoadLibrary(_this, path);
  2977     }
  2978     if (retval == 0) {
  2979         ++_this->gl_config.driver_loaded;
  2980     }
  2981     return (retval);
  2982 }
  2983 
  2984 void *
  2985 SDL_GL_GetProcAddress(const char *proc)
  2986 {
  2987     void *func;
  2988 
  2989     if (!_this) {
  2990         SDL_UninitializedVideo();
  2991         return NULL;
  2992     }
  2993     func = NULL;
  2994     if (_this->GL_GetProcAddress) {
  2995         if (_this->gl_config.driver_loaded) {
  2996             func = _this->GL_GetProcAddress(_this, proc);
  2997         } else {
  2998             SDL_SetError("No GL driver has been loaded");
  2999         }
  3000     } else {
  3001         SDL_SetError("No dynamic GL support in video driver");
  3002     }
  3003     return func;
  3004 }
  3005 
  3006 void
  3007 SDL_GL_UnloadLibrary(void)
  3008 {
  3009     if (!_this) {
  3010         SDL_UninitializedVideo();
  3011         return;
  3012     }
  3013     if (_this->gl_config.driver_loaded > 0) {
  3014         if (--_this->gl_config.driver_loaded > 0) {
  3015             return;
  3016         }
  3017         if (_this->GL_UnloadLibrary) {
  3018             _this->GL_UnloadLibrary(_this);
  3019         }
  3020     }
  3021 }
  3022 
  3023 SDL_bool
  3024 SDL_GL_ExtensionSupported(const char *extension)
  3025 {
  3026 #if SDL_VIDEO_OPENGL || SDL_VIDEO_OPENGL_ES
  3027     const GLubyte *(APIENTRY * glGetStringFunc) (GLenum);
  3028     const char *extensions;
  3029     const char *start;
  3030     const char *where, *terminator;
  3031 
  3032     /* Extension names should not have spaces. */
  3033     where = SDL_strchr(extension, ' ');
  3034     if (where || *extension == '\0') {
  3035         return SDL_FALSE;
  3036     }
  3037     /* See if there's an environment variable override */
  3038     start = SDL_getenv(extension);
  3039     if (start && *start == '0') {
  3040         return SDL_FALSE;
  3041     }
  3042     /* Lookup the available extensions */
  3043     glGetStringFunc = SDL_GL_GetProcAddress("glGetString");
  3044     if (glGetStringFunc) {
  3045         extensions = (const char *) glGetStringFunc(GL_EXTENSIONS);
  3046     } else {
  3047         extensions = NULL;
  3048     }
  3049     if (!extensions) {
  3050         return SDL_FALSE;
  3051     }
  3052     /*
  3053      * It takes a bit of care to be fool-proof about parsing the OpenGL
  3054      * extensions string. Don't be fooled by sub-strings, etc.
  3055      */
  3056 
  3057     start = extensions;
  3058 
  3059     for (;;) {
  3060         where = SDL_strstr(start, extension);
  3061         if (!where)
  3062             break;
  3063 
  3064         terminator = where + SDL_strlen(extension);
  3065         if (where == start || *(where - 1) == ' ')
  3066             if (*terminator == ' ' || *terminator == '\0')
  3067                 return SDL_TRUE;
  3068 
  3069         start = terminator;
  3070     }
  3071     return SDL_FALSE;
  3072 #else
  3073     return SDL_FALSE;
  3074 #endif
  3075 }
  3076 
  3077 int
  3078 SDL_GL_SetAttribute(SDL_GLattr attr, int value)
  3079 {
  3080 #if SDL_VIDEO_OPENGL || SDL_VIDEO_OPENGL_ES
  3081     int retval;
  3082 
  3083     if (!_this) {
  3084         SDL_UninitializedVideo();
  3085         return -1;
  3086     }
  3087     retval = 0;
  3088     switch (attr) {
  3089     case SDL_GL_RED_SIZE:
  3090         _this->gl_config.red_size = value;
  3091         break;
  3092     case SDL_GL_GREEN_SIZE:
  3093         _this->gl_config.green_size = value;
  3094         break;
  3095     case SDL_GL_BLUE_SIZE:
  3096         _this->gl_config.blue_size = value;
  3097         break;
  3098     case SDL_GL_ALPHA_SIZE:
  3099         _this->gl_config.alpha_size = value;
  3100         break;
  3101     case SDL_GL_DOUBLEBUFFER:
  3102         _this->gl_config.double_buffer = value;
  3103         break;
  3104     case SDL_GL_BUFFER_SIZE:
  3105         _this->gl_config.buffer_size = value;
  3106         break;
  3107     case SDL_GL_DEPTH_SIZE:
  3108         _this->gl_config.depth_size = value;
  3109         break;
  3110     case SDL_GL_STENCIL_SIZE:
  3111         _this->gl_config.stencil_size = value;
  3112         break;
  3113     case SDL_GL_ACCUM_RED_SIZE:
  3114         _this->gl_config.accum_red_size = value;
  3115         break;
  3116     case SDL_GL_ACCUM_GREEN_SIZE:
  3117         _this->gl_config.accum_green_size = value;
  3118         break;
  3119     case SDL_GL_ACCUM_BLUE_SIZE:
  3120         _this->gl_config.accum_blue_size = value;
  3121         break;
  3122     case SDL_GL_ACCUM_ALPHA_SIZE:
  3123         _this->gl_config.accum_alpha_size = value;
  3124         break;
  3125     case SDL_GL_STEREO:
  3126         _this->gl_config.stereo = value;
  3127         break;
  3128     case SDL_GL_MULTISAMPLEBUFFERS:
  3129         _this->gl_config.multisamplebuffers = value;
  3130         break;
  3131     case SDL_GL_MULTISAMPLESAMPLES:
  3132         _this->gl_config.multisamplesamples = value;
  3133         break;
  3134     case SDL_GL_ACCELERATED_VISUAL:
  3135         _this->gl_config.accelerated = value;
  3136         break;
  3137     case SDL_GL_RETAINED_BACKING:
  3138         _this->gl_config.retained_backing = value;
  3139         break;
  3140     case SDL_GL_CONTEXT_MAJOR_VERSION:
  3141         _this->gl_config.major_version = value;
  3142         break;
  3143     case SDL_GL_CONTEXT_MINOR_VERSION:
  3144         _this->gl_config.minor_version = value;
  3145         break;
  3146     default:
  3147         SDL_SetError("Unknown OpenGL attribute");
  3148         retval = -1;
  3149         break;
  3150     }
  3151     return retval;
  3152 #else
  3153     SDL_Unsupported();
  3154     return -1;
  3155 #endif /* SDL_VIDEO_OPENGL */
  3156 }
  3157 
  3158 int
  3159 SDL_GL_GetAttribute(SDL_GLattr attr, int *value)
  3160 {
  3161 #if SDL_VIDEO_OPENGL || SDL_VIDEO_OPENGL_ES
  3162     void (APIENTRY * glGetIntegervFunc) (GLenum pname, GLint * params);
  3163     GLenum(APIENTRY * glGetErrorFunc) (void);
  3164     GLenum attrib = 0;
  3165     GLenum error = 0;
  3166 
  3167     glGetIntegervFunc = SDL_GL_GetProcAddress("glGetIntegerv");
  3168     if (!glGetIntegervFunc) {
  3169         return -1;
  3170     }
  3171 
  3172     glGetErrorFunc = SDL_GL_GetProcAddress("glGetError");
  3173     if (!glGetErrorFunc) {
  3174         return -1;
  3175     }
  3176 
  3177     /* Clear value in any case */
  3178     *value = 0;
  3179 
  3180     switch (attr) {
  3181     case SDL_GL_RETAINED_BACKING:
  3182         *value = _this->gl_config.retained_backing;
  3183         return 0;
  3184     case SDL_GL_RED_SIZE:
  3185         attrib = GL_RED_BITS;
  3186         break;
  3187     case SDL_GL_BLUE_SIZE:
  3188         attrib = GL_BLUE_BITS;
  3189         break;
  3190     case SDL_GL_GREEN_SIZE:
  3191         attrib = GL_GREEN_BITS;
  3192         break;
  3193     case SDL_GL_ALPHA_SIZE:
  3194         attrib = GL_ALPHA_BITS;
  3195         break;
  3196     case SDL_GL_DOUBLEBUFFER:
  3197 #ifndef SDL_VIDEO_OPENGL_ES
  3198         attrib = GL_DOUBLEBUFFER;
  3199         break;
  3200 #else
  3201         /* OpenGL ES 1.0 and above specifications have EGL_SINGLE_BUFFER      */
  3202         /* parameter which switches double buffer to single buffer. OpenGL ES */
  3203         /* SDL driver must set proper value after initialization              */
  3204         *value = _this->gl_config.double_buffer;
  3205         return 0;
  3206 #endif
  3207     case SDL_GL_DEPTH_SIZE:
  3208         attrib = GL_DEPTH_BITS;
  3209         break;
  3210     case SDL_GL_STENCIL_SIZE:
  3211         attrib = GL_STENCIL_BITS;
  3212         break;
  3213 #ifndef SDL_VIDEO_OPENGL_ES
  3214     case SDL_GL_ACCUM_RED_SIZE:
  3215         attrib = GL_ACCUM_RED_BITS;
  3216         break;
  3217     case SDL_GL_ACCUM_GREEN_SIZE:
  3218         attrib = GL_ACCUM_GREEN_BITS;
  3219         break;
  3220     case SDL_GL_ACCUM_BLUE_SIZE:
  3221         attrib = GL_ACCUM_BLUE_BITS;
  3222         break;
  3223     case SDL_GL_ACCUM_ALPHA_SIZE:
  3224         attrib = GL_ACCUM_ALPHA_BITS;
  3225         break;
  3226     case SDL_GL_STEREO:
  3227         attrib = GL_STEREO;
  3228         break;
  3229 #else
  3230     case SDL_GL_ACCUM_RED_SIZE:
  3231     case SDL_GL_ACCUM_GREEN_SIZE:
  3232     case SDL_GL_ACCUM_BLUE_SIZE:
  3233     case SDL_GL_ACCUM_ALPHA_SIZE:
  3234     case SDL_GL_STEREO:
  3235         /* none of these are supported in OpenGL ES */
  3236         *value = 0;
  3237         return 0;
  3238 #endif
  3239     case SDL_GL_MULTISAMPLEBUFFERS:
  3240 #ifndef SDL_VIDEO_OPENGL_ES
  3241         attrib = GL_SAMPLE_BUFFERS_ARB;
  3242 #else
  3243         attrib = GL_SAMPLE_BUFFERS;
  3244 #endif
  3245         break;
  3246     case SDL_GL_MULTISAMPLESAMPLES:
  3247 #ifndef SDL_VIDEO_OPENGL_ES
  3248         attrib = GL_SAMPLES_ARB;
  3249 #else
  3250         attrib = GL_SAMPLES;
  3251 #endif
  3252         break;
  3253     case SDL_GL_BUFFER_SIZE:
  3254         {
  3255             GLint bits = 0;
  3256             GLint component;
  3257 
  3258             /*
  3259              * there doesn't seem to be a single flag in OpenGL
  3260              * for this!
  3261              */
  3262             glGetIntegervFunc(GL_RED_BITS, &component);
  3263             bits += component;
  3264             glGetIntegervFunc(GL_GREEN_BITS, &component);
  3265             bits += component;
  3266             glGetIntegervFunc(GL_BLUE_BITS, &component);
  3267             bits += component;
  3268             glGetIntegervFunc(GL_ALPHA_BITS, &component);
  3269             bits += component;
  3270 
  3271             *value = bits;
  3272             return 0;
  3273         }
  3274     case SDL_GL_ACCELERATED_VISUAL:
  3275         {
  3276             /* FIXME: How do we get this information? */
  3277             *value = (_this->gl_config.accelerated != 0);
  3278             return 0;
  3279         }
  3280     default:
  3281         SDL_SetError("Unknown OpenGL attribute");
  3282         return -1;
  3283     }
  3284 
  3285     glGetIntegervFunc(attrib, (GLint *) value);
  3286     error = glGetErrorFunc();
  3287     if (error != GL_NO_ERROR) {
  3288         switch (error) {
  3289         case GL_INVALID_ENUM:
  3290             {
  3291                 SDL_SetError("OpenGL error: GL_INVALID_ENUM");
  3292             }
  3293             break;
  3294         case GL_INVALID_VALUE:
  3295             {
  3296                 SDL_SetError("OpenGL error: GL_INVALID_VALUE");
  3297             }
  3298             break;
  3299         default:
  3300             {
  3301                 SDL_SetError("OpenGL error: %08X", error);
  3302             }
  3303             break;
  3304         }
  3305         return -1;
  3306     }
  3307     return 0;
  3308 #else
  3309     SDL_Unsupported();
  3310     return -1;
  3311 #endif /* SDL_VIDEO_OPENGL */
  3312 }
  3313 
  3314 SDL_GLContext
  3315 SDL_GL_CreateContext(SDL_WindowID windowID)
  3316 {
  3317     SDL_Window *window = SDL_GetWindowFromID(windowID);
  3318 
  3319     if (!window) {
  3320         return NULL;
  3321     }
  3322     if (!(window->flags & SDL_WINDOW_OPENGL)) {
  3323         SDL_SetError("The specified window isn't an OpenGL window");
  3324         return NULL;
  3325     }
  3326     return _this->GL_CreateContext(_this, window);
  3327 }
  3328 
  3329 int
  3330 SDL_GL_MakeCurrent(SDL_WindowID windowID, SDL_GLContext context)
  3331 {
  3332     SDL_Window *window = SDL_GetWindowFromID(windowID);
  3333 
  3334     if (window && !(window->flags & SDL_WINDOW_OPENGL)) {
  3335         SDL_SetError("The specified window isn't an OpenGL window");
  3336         return -1;
  3337     }
  3338     if (!context) {
  3339         window = NULL;
  3340     }
  3341     return _this->GL_MakeCurrent(_this, window, context);
  3342 }
  3343 
  3344 int
  3345 SDL_GL_SetSwapInterval(int interval)
  3346 {
  3347     if (!_this) {
  3348         SDL_UninitializedVideo();
  3349         return -1;
  3350     }
  3351     if (_this->GL_SetSwapInterval) {
  3352         return _this->GL_SetSwapInterval(_this, interval);
  3353     } else {
  3354         SDL_SetError("Setting the swap interval is not supported");
  3355         return -1;
  3356     }
  3357 }
  3358 
  3359 int
  3360 SDL_GL_GetSwapInterval(void)
  3361 {
  3362     if (!_this) {
  3363         SDL_UninitializedVideo();
  3364         return -1;
  3365     }
  3366     if (_this->GL_GetSwapInterval) {
  3367         return _this->GL_GetSwapInterval(_this);
  3368     } else {
  3369         SDL_SetError("Getting the swap interval is not supported");
  3370         return -1;
  3371     }
  3372 }
  3373 
  3374 void
  3375 SDL_GL_SwapWindow(SDL_WindowID windowID)
  3376 {
  3377     SDL_Window *window = SDL_GetWindowFromID(windowID);
  3378 
  3379     if (!window) {
  3380         return;
  3381     }
  3382     if (!(window->flags & SDL_WINDOW_OPENGL)) {
  3383         SDL_SetError("The specified window isn't an OpenGL window");
  3384         return;
  3385     }
  3386     _this->GL_SwapWindow(_this, window);
  3387 }
  3388 
  3389 void
  3390 SDL_GL_DeleteContext(SDL_GLContext context)
  3391 {
  3392     if (!_this || !context) {
  3393         return;
  3394     }
  3395     _this->GL_MakeCurrent(_this, NULL, NULL);
  3396     _this->GL_DeleteContext(_this, context);
  3397 }
  3398 
  3399 #if 0                           // FIXME
  3400 /*
  3401  * Utility function used by SDL_WM_SetIcon(); flags & 1 for color key, flags
  3402  * & 2 for alpha channel.
  3403  */
  3404 static void
  3405 CreateMaskFromColorKeyOrAlpha(SDL_Surface * icon, Uint8 * mask, int flags)
  3406 {
  3407     int x, y;
  3408     Uint32 colorkey;
  3409 #define SET_MASKBIT(icon, x, y, mask) \
  3410 	mask[(y*((icon->w+7)/8))+(x/8)] &= ~(0x01<<(7-(x%8)))
  3411 
  3412     colorkey = icon->format->colorkey;
  3413     switch (icon->format->BytesPerPixel) {
  3414     case 1:
  3415         {
  3416             Uint8 *pixels;
  3417             for (y = 0; y < icon->h; ++y) {
  3418                 pixels = (Uint8 *) icon->pixels + y * icon->pitch;
  3419                 for (x = 0; x < icon->w; ++x) {
  3420                     if (*pixels++ == colorkey) {
  3421                         SET_MASKBIT(icon, x, y, mask);
  3422                     }
  3423                 }
  3424             }
  3425         }
  3426         break;
  3427 
  3428     case 2:
  3429         {
  3430             Uint16 *pixels;
  3431             for (y = 0; y < icon->h; ++y) {
  3432                 pixels = (Uint16 *) icon->pixels + y * icon->pitch / 2;
  3433                 for (x = 0; x < icon->w; ++x) {
  3434                     if ((flags & 1) && *pixels == colorkey) {
  3435                         SET_MASKBIT(icon, x, y, mask);
  3436                     } else if ((flags & 2)
  3437                                && (*pixels & icon->format->Amask) == 0) {
  3438                         SET_MASKBIT(icon, x, y, mask);
  3439                     }
  3440                     pixels++;
  3441                 }
  3442             }
  3443         }
  3444         break;
  3445 
  3446     case 4:
  3447         {
  3448             Uint32 *pixels;
  3449             for (y = 0; y < icon->h; ++y) {
  3450                 pixels = (Uint32 *) icon->pixels + y * icon->pitch / 4;
  3451                 for (x = 0; x < icon->w; ++x) {
  3452                     if ((flags & 1) && *pixels == colorkey) {
  3453                         SET_MASKBIT(icon, x, y, mask);
  3454                     } else if ((flags & 2)
  3455                                && (*pixels & icon->format->Amask) == 0) {
  3456                         SET_MASKBIT(icon, x, y, mask);
  3457                     }
  3458                     pixels++;
  3459                 }
  3460             }
  3461         }
  3462         break;
  3463     }
  3464 }
  3465 
  3466 /*
  3467  * Sets the window manager icon for the display window.
  3468  */
  3469 void
  3470 SDL_WM_SetIcon(SDL_Surface * icon, Uint8 * mask)
  3471 {
  3472     if (icon && _this->SetIcon) {
  3473         /* Generate a mask if necessary, and create the icon! */
  3474         if (mask == NULL) {
  3475             int mask_len = icon->h * (icon->w + 7) / 8;
  3476             int flags = 0;
  3477             mask = (Uint8 *) SDL_malloc(mask_len);
  3478             if (mask == NULL) {
  3479                 return;
  3480             }
  3481             SDL_memset(mask, ~0, mask_len);
  3482             if (icon->flags & SDL_SRCCOLORKEY)
  3483                 flags |= 1;
  3484             if (icon->flags & SDL_SRCALPHA)
  3485                 flags |= 2;
  3486             if (flags) {
  3487                 CreateMaskFromColorKeyOrAlpha(icon, mask, flags);
  3488             }
  3489             _this->SetIcon(_this, icon, mask);
  3490             SDL_free(mask);
  3491         } else {
  3492             _this->SetIcon(_this, icon, mask);
  3493         }
  3494     }
  3495 }
  3496 #endif
  3497 
  3498 SDL_bool
  3499 SDL_GetWindowWMInfo(SDL_WindowID windowID, struct SDL_SysWMinfo *info)
  3500 {
  3501     SDL_Window *window = SDL_GetWindowFromID(windowID);
  3502 
  3503     if (!window || !_this->GetWindowWMInfo) {
  3504         return SDL_FALSE;
  3505     }
  3506     return (_this->GetWindowWMInfo(_this, window, info));
  3507 }
  3508 
  3509 void
  3510 SDL_StartTextInput(void)
  3511 {
  3512     if (_this->StartTextInput) {
  3513         _this->StartTextInput(_this);
  3514     }
  3515     SDL_EventState(SDL_TEXTINPUT, SDL_ENABLE);
  3516     SDL_EventState(SDL_TEXTEDITING, SDL_ENABLE);
  3517 }
  3518 
  3519 void
  3520 SDL_StopTextInput(void)
  3521 {
  3522     if (_this->StopTextInput) {
  3523         _this->StopTextInput(_this);
  3524     }
  3525     SDL_EventState(SDL_TEXTINPUT, SDL_DISABLE);
  3526     SDL_EventState(SDL_TEXTEDITING, SDL_DISABLE);
  3527 }
  3528 
  3529 void
  3530 SDL_SetTextInputRect(SDL_Rect *rect)
  3531 {
  3532     if (_this->SetTextInputRect) {
  3533         _this->SetTextInputRect(_this, rect);
  3534     }
  3535 }
  3536 
  3537 /* vi: set ts=4 sw=4 expandtab: */