src/video/SDL_video.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 06 Dec 2009 08:03:38 +0000
changeset 3528 59ff7a2beb57
parent 3526 e6f2f312780f
child 3536 0267b8b1595c
permissions -rw-r--r--
Added an API function to query geometry of multiple monitors:
SDL_GetDisplayBounds()

Implemented multi-monitor window positions on Windows
     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_Renderer *renderer;
  2489     SDL_Window *window;
  2490 
  2491     renderer = SDL_GetCurrentRenderer(SDL_TRUE);
  2492     if (!renderer) {
  2493         return -1;
  2494     }
  2495     if (!renderer->RenderPoint) {
  2496         SDL_Unsupported();
  2497         return -1;
  2498     }
  2499     window = SDL_GetWindowFromID(renderer->window);
  2500     if (x < 0 || y < 0 || x >= window->w || y >= window->h) {
  2501         return 0;
  2502     }
  2503     return renderer->RenderPoint(renderer, x, y);
  2504 }
  2505 
  2506 int
  2507 SDL_RenderLine(int x1, int y1, int x2, int y2)
  2508 {
  2509     SDL_Renderer *renderer;
  2510     SDL_Window *window;
  2511     SDL_Rect real_rect;
  2512 
  2513     if (x1 == x2 && y1 == y2) {
  2514         return SDL_RenderPoint(x1, y1);
  2515     }
  2516 
  2517     renderer = SDL_GetCurrentRenderer(SDL_TRUE);
  2518     if (!renderer) {
  2519         return -1;
  2520     }
  2521     if (!renderer->RenderLine) {
  2522         SDL_Unsupported();
  2523         return -1;
  2524     }
  2525     window = SDL_GetWindowFromID(renderer->window);
  2526 
  2527     real_rect.x = 0;
  2528     real_rect.y = 0;
  2529     real_rect.w = window->w;
  2530     real_rect.h = window->h;
  2531     if (!SDL_IntersectRectAndLine(&real_rect, &x1, &y1, &x2, &y2)) {
  2532         return (0);
  2533     }
  2534     return renderer->RenderLine(renderer, x1, y1, x2, y2);
  2535 }
  2536 
  2537 int
  2538 SDL_RenderFill(const SDL_Rect * rect)
  2539 {
  2540     SDL_Renderer *renderer;
  2541     SDL_Window *window;
  2542     SDL_Rect real_rect;
  2543 
  2544     renderer = SDL_GetCurrentRenderer(SDL_TRUE);
  2545     if (!renderer) {
  2546         return -1;
  2547     }
  2548     if (!renderer->RenderFill) {
  2549         SDL_Unsupported();
  2550         return -1;
  2551     }
  2552     window = SDL_GetWindowFromID(renderer->window);
  2553 
  2554     real_rect.x = 0;
  2555     real_rect.y = 0;
  2556     real_rect.w = window->w;
  2557     real_rect.h = window->h;
  2558     if (rect) {
  2559         if (!SDL_IntersectRect(rect, &real_rect, &real_rect)) {
  2560             return 0;
  2561         }
  2562     }
  2563     return renderer->RenderFill(renderer, &real_rect);
  2564 }
  2565 
  2566 int
  2567 SDL_RenderCopy(SDL_TextureID textureID, const SDL_Rect * srcrect,
  2568                const SDL_Rect * dstrect)
  2569 {
  2570     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  2571     SDL_Renderer *renderer;
  2572     SDL_Window *window;
  2573     SDL_Rect real_srcrect;
  2574     SDL_Rect real_dstrect;
  2575 
  2576     renderer = SDL_GetCurrentRenderer(SDL_TRUE);
  2577     if (!renderer) {
  2578         return -1;
  2579     }
  2580     if (!texture) {
  2581         SDL_SetError("Texture not found");
  2582         return -1;
  2583     }
  2584     if (texture->renderer != renderer) {
  2585         SDL_SetError("Texture was not created with this renderer");
  2586         return -1;
  2587     }
  2588     if (!renderer->RenderCopy) {
  2589         SDL_Unsupported();
  2590         return -1;
  2591     }
  2592     window = SDL_GetWindowFromID(renderer->window);
  2593 
  2594     real_srcrect.x = 0;
  2595     real_srcrect.y = 0;
  2596     real_srcrect.w = texture->w;
  2597     real_srcrect.h = texture->h;
  2598     if (srcrect) {
  2599         if (!SDL_IntersectRect(srcrect, &real_srcrect, &real_srcrect)) {
  2600             return 0;
  2601         }
  2602     }
  2603 
  2604     real_dstrect.x = 0;
  2605     real_dstrect.y = 0;
  2606     real_dstrect.w = window->w;
  2607     real_dstrect.h = window->h;
  2608     if (dstrect) {
  2609         if (!SDL_IntersectRect(dstrect, &real_dstrect, &real_dstrect)) {
  2610             return 0;
  2611         }
  2612         /* Clip srcrect by the same amount as dstrect was clipped */
  2613         if (dstrect->w != real_dstrect.w) {
  2614             int deltax = (real_dstrect.x - dstrect->x);
  2615             int deltaw = (real_dstrect.w - dstrect->w);
  2616             real_srcrect.x += (deltax * real_srcrect.w) / dstrect->w;
  2617             real_srcrect.w += (deltaw * real_srcrect.w) / dstrect->w;
  2618         }
  2619         if (dstrect->h != real_dstrect.h) {
  2620             int deltay = (real_dstrect.y - dstrect->y);
  2621             int deltah = (real_dstrect.h - dstrect->h);
  2622             real_srcrect.y += (deltay * real_srcrect.h) / dstrect->h;
  2623             real_srcrect.h += (deltah * real_srcrect.h) / dstrect->h;
  2624         }
  2625     }
  2626 
  2627     return renderer->RenderCopy(renderer, texture, &real_srcrect,
  2628                                 &real_dstrect);
  2629 }
  2630 
  2631 int
  2632 SDL_RenderReadPixels(const SDL_Rect * rect, Uint32 format,
  2633                      void * pixels, int pitch)
  2634 {
  2635     SDL_Renderer *renderer;
  2636     SDL_Window *window;
  2637     SDL_Rect real_rect;
  2638 
  2639     renderer = SDL_GetCurrentRenderer(SDL_TRUE);
  2640     if (!renderer) {
  2641         return -1;
  2642     }
  2643     if (!renderer->RenderReadPixels) {
  2644         SDL_Unsupported();
  2645         return -1;
  2646     }
  2647     window = SDL_GetWindowFromID(renderer->window);
  2648 
  2649     if (!format) {
  2650         format = SDL_GetDisplayFromWindow(window)->current_mode.format;
  2651     }
  2652 
  2653     real_rect.x = 0;
  2654     real_rect.y = 0;
  2655     real_rect.w = window->w;
  2656     real_rect.h = window->h;
  2657     if (rect) {
  2658         if (!SDL_IntersectRect(rect, &real_rect, &real_rect)) {
  2659             return 0;
  2660         }
  2661         if (real_rect.y > rect->y) {
  2662             pixels = (Uint8 *)pixels + pitch * (real_rect.y - rect->y);
  2663         }
  2664         if (real_rect.x > rect->x) {
  2665             Uint32 format = SDL_CurrentDisplay.current_mode.format;
  2666             int bpp = SDL_BYTESPERPIXEL(format);
  2667             pixels = (Uint8 *)pixels + bpp * (real_rect.x - rect->x);
  2668         }
  2669     }
  2670 
  2671     return renderer->RenderReadPixels(renderer, &real_rect,
  2672                                       format, pixels, pitch);
  2673 }
  2674 
  2675 int
  2676 SDL_RenderWritePixels(const SDL_Rect * rect, Uint32 format,
  2677                       const void * pixels, int pitch)
  2678 {
  2679     SDL_Renderer *renderer;
  2680     SDL_Window *window;
  2681     SDL_Rect real_rect;
  2682 
  2683     renderer = SDL_GetCurrentRenderer(SDL_TRUE);
  2684     if (!renderer) {
  2685         return -1;
  2686     }
  2687     if (!renderer->RenderWritePixels) {
  2688         SDL_Unsupported();
  2689         return -1;
  2690     }
  2691     window = SDL_GetWindowFromID(renderer->window);
  2692 
  2693     if (!format) {
  2694         format = SDL_GetDisplayFromWindow(window)->current_mode.format;
  2695     }
  2696 
  2697     real_rect.x = 0;
  2698     real_rect.y = 0;
  2699     real_rect.w = window->w;
  2700     real_rect.h = window->h;
  2701     if (rect) {
  2702         if (!SDL_IntersectRect(rect, &real_rect, &real_rect)) {
  2703             return 0;
  2704         }
  2705         if (real_rect.y > rect->y) {
  2706             pixels = (const Uint8 *)pixels + pitch * (real_rect.y - rect->y);
  2707         }
  2708         if (real_rect.x > rect->x) {
  2709             Uint32 format = SDL_CurrentDisplay.current_mode.format;
  2710             int bpp = SDL_BYTESPERPIXEL(format);
  2711             pixels = (const Uint8 *)pixels + bpp * (real_rect.x - rect->x);
  2712         }
  2713     }
  2714 
  2715     return renderer->RenderWritePixels(renderer, &real_rect,
  2716                                        format, pixels, pitch);
  2717 }
  2718 
  2719 void
  2720 SDL_RenderPresent(void)
  2721 {
  2722     SDL_Renderer *renderer;
  2723 
  2724     renderer = SDL_GetCurrentRenderer(SDL_TRUE);
  2725     if (!renderer || !renderer->RenderPresent) {
  2726         return;
  2727     }
  2728     renderer->RenderPresent(renderer);
  2729 }
  2730 
  2731 void
  2732 SDL_DestroyTexture(SDL_TextureID textureID)
  2733 {
  2734     int hash;
  2735     SDL_Texture *prev, *texture;
  2736     SDL_Renderer *renderer;
  2737 
  2738     if (!_this) {
  2739         SDL_UninitializedVideo();
  2740         return;
  2741     }
  2742     /* Look up the texture in the hash table */
  2743     hash = (textureID % SDL_arraysize(SDL_CurrentDisplay.textures));
  2744     prev = NULL;
  2745     for (texture = SDL_CurrentDisplay.textures[hash]; texture;
  2746          prev = texture, texture = texture->next) {
  2747         if (texture->id == textureID) {
  2748             break;
  2749         }
  2750     }
  2751     if (!texture) {
  2752         return;
  2753     }
  2754     /* Unlink the texture from the list */
  2755     if (prev) {
  2756         prev->next = texture->next;
  2757     } else {
  2758         SDL_CurrentDisplay.textures[hash] = texture->next;
  2759     }
  2760 
  2761     /* Free the texture */
  2762     renderer = texture->renderer;
  2763     renderer->DestroyTexture(renderer, texture);
  2764     SDL_free(texture);
  2765 }
  2766 
  2767 void
  2768 SDL_DestroyRenderer(SDL_WindowID windowID)
  2769 {
  2770     SDL_Window *window = SDL_GetWindowFromID(windowID);
  2771     SDL_Renderer *renderer;
  2772     int i;
  2773 
  2774     if (!window) {
  2775         return;
  2776     }
  2777     renderer = window->renderer;
  2778     if (!renderer) {
  2779         return;
  2780     }
  2781     /* Free existing textures for this renderer */
  2782     for (i = 0; i < SDL_arraysize(SDL_CurrentDisplay.textures); ++i) {
  2783         SDL_Texture *texture;
  2784         SDL_Texture *prev = NULL;
  2785         SDL_Texture *next;
  2786         for (texture = SDL_CurrentDisplay.textures[i]; texture;
  2787              texture = next) {
  2788             next = texture->next;
  2789             if (texture->renderer == renderer) {
  2790                 if (prev) {
  2791                     prev->next = next;
  2792                 } else {
  2793                     SDL_CurrentDisplay.textures[i] = next;
  2794                 }
  2795                 renderer->DestroyTexture(renderer, texture);
  2796                 SDL_free(texture);
  2797             } else {
  2798                 prev = texture;
  2799             }
  2800         }
  2801     }
  2802 
  2803     /* Free the renderer instance */
  2804     renderer->DestroyRenderer(renderer);
  2805 
  2806     /* Clear references */
  2807     window->renderer = NULL;
  2808     if (SDL_CurrentDisplay.current_renderer == renderer) {
  2809         SDL_CurrentDisplay.current_renderer = NULL;
  2810     }
  2811 }
  2812 
  2813 SDL_bool
  2814 SDL_IsScreenSaverEnabled()
  2815 {
  2816     if (!_this) {
  2817         return SDL_TRUE;
  2818     }
  2819     return _this->suspend_screensaver ? SDL_FALSE : SDL_TRUE;
  2820 }
  2821 
  2822 void
  2823 SDL_EnableScreenSaver()
  2824 {
  2825     if (!_this) {
  2826         return;
  2827     }
  2828     if (!_this->suspend_screensaver) {
  2829         return;
  2830     }
  2831     _this->suspend_screensaver = SDL_FALSE;
  2832     if (_this->SuspendScreenSaver) {
  2833         _this->SuspendScreenSaver(_this);
  2834     }
  2835 }
  2836 
  2837 void
  2838 SDL_DisableScreenSaver()
  2839 {
  2840     if (!_this) {
  2841         return;
  2842     }
  2843     if (_this->suspend_screensaver) {
  2844         return;
  2845     }
  2846     _this->suspend_screensaver = SDL_TRUE;
  2847     if (_this->SuspendScreenSaver) {
  2848         _this->SuspendScreenSaver(_this);
  2849     }
  2850 }
  2851 
  2852 void
  2853 SDL_VideoQuit(void)
  2854 {
  2855     int i, j;
  2856 
  2857     if (!_this) {
  2858         return;
  2859     }
  2860     /* Halt event processing before doing anything else */
  2861     SDL_StopEventLoop();
  2862     SDL_EnableScreenSaver();
  2863 
  2864     /* Clean up the system video */
  2865     for (i = _this->num_displays; i--;) {
  2866         SDL_VideoDisplay *display = &_this->displays[i];
  2867         for (j = display->num_windows; j--;) {
  2868             SDL_DestroyWindow(display->windows[i].id);
  2869         }
  2870         if (display->windows) {
  2871             SDL_free(display->windows);
  2872             display->windows = NULL;
  2873         }
  2874         display->num_windows = 0;
  2875         if (display->render_drivers) {
  2876             SDL_free(display->render_drivers);
  2877             display->render_drivers = NULL;
  2878         }
  2879         display->num_render_drivers = 0;
  2880     }
  2881     _this->VideoQuit(_this);
  2882 
  2883     for (i = _this->num_displays; i--;) {
  2884         SDL_VideoDisplay *display = &_this->displays[i];
  2885         for (j = display->num_display_modes; j--;) {
  2886             if (display->display_modes[j].driverdata) {
  2887                 SDL_free(display->display_modes[j].driverdata);
  2888                 display->display_modes[j].driverdata = NULL;
  2889             }
  2890         }
  2891         if (display->display_modes) {
  2892             SDL_free(display->display_modes);
  2893             display->display_modes = NULL;
  2894         }
  2895         if (display->desktop_mode.driverdata) {
  2896             SDL_free(display->desktop_mode.driverdata);
  2897             display->desktop_mode.driverdata = NULL;
  2898         }
  2899         if (display->palette) {
  2900             SDL_FreePalette(display->palette);
  2901             display->palette = NULL;
  2902         }
  2903         if (display->gamma) {
  2904             SDL_free(display->gamma);
  2905             display->gamma = NULL;
  2906         }
  2907         if (display->driverdata) {
  2908             SDL_free(display->driverdata);
  2909             display->driverdata = NULL;
  2910         }
  2911     }
  2912     if (_this->displays) {
  2913         SDL_free(_this->displays);
  2914         _this->displays = NULL;
  2915     }
  2916     _this->free(_this);
  2917     _this = NULL;
  2918 }
  2919 
  2920 int
  2921 SDL_GL_LoadLibrary(const char *path)
  2922 {
  2923     int retval;
  2924 
  2925     if (!_this) {
  2926         SDL_UninitializedVideo();
  2927         return -1;
  2928     }
  2929     if (_this->gl_config.driver_loaded) {
  2930         if (path && SDL_strcmp(path, _this->gl_config.driver_path) != 0) {
  2931             SDL_SetError("OpenGL library already loaded");
  2932             return -1;
  2933         }
  2934         retval = 0;
  2935     } else {
  2936         if (!_this->GL_LoadLibrary) {
  2937             SDL_SetError("No dynamic GL support in video driver");
  2938             return -1;
  2939         }
  2940         retval = _this->GL_LoadLibrary(_this, path);
  2941     }
  2942     if (retval == 0) {
  2943         ++_this->gl_config.driver_loaded;
  2944     }
  2945     return (retval);
  2946 }
  2947 
  2948 void *
  2949 SDL_GL_GetProcAddress(const char *proc)
  2950 {
  2951     void *func;
  2952 
  2953     if (!_this) {
  2954         SDL_UninitializedVideo();
  2955         return NULL;
  2956     }
  2957     func = NULL;
  2958     if (_this->GL_GetProcAddress) {
  2959         if (_this->gl_config.driver_loaded) {
  2960             func = _this->GL_GetProcAddress(_this, proc);
  2961         } else {
  2962             SDL_SetError("No GL driver has been loaded");
  2963         }
  2964     } else {
  2965         SDL_SetError("No dynamic GL support in video driver");
  2966     }
  2967     return func;
  2968 }
  2969 
  2970 void
  2971 SDL_GL_UnloadLibrary(void)
  2972 {
  2973     if (!_this) {
  2974         SDL_UninitializedVideo();
  2975         return;
  2976     }
  2977     if (_this->gl_config.driver_loaded > 0) {
  2978         if (--_this->gl_config.driver_loaded > 0) {
  2979             return;
  2980         }
  2981         if (_this->GL_UnloadLibrary) {
  2982             _this->GL_UnloadLibrary(_this);
  2983         }
  2984     }
  2985 }
  2986 
  2987 SDL_bool
  2988 SDL_GL_ExtensionSupported(const char *extension)
  2989 {
  2990 #if SDL_VIDEO_OPENGL || SDL_VIDEO_OPENGL_ES
  2991     const GLubyte *(APIENTRY * glGetStringFunc) (GLenum);
  2992     const char *extensions;
  2993     const char *start;
  2994     const char *where, *terminator;
  2995 
  2996     /* Extension names should not have spaces. */
  2997     where = SDL_strchr(extension, ' ');
  2998     if (where || *extension == '\0') {
  2999         return SDL_FALSE;
  3000     }
  3001     /* See if there's an environment variable override */
  3002     start = SDL_getenv(extension);
  3003     if (start && *start == '0') {
  3004         return SDL_FALSE;
  3005     }
  3006     /* Lookup the available extensions */
  3007     glGetStringFunc = SDL_GL_GetProcAddress("glGetString");
  3008     if (glGetStringFunc) {
  3009         extensions = (const char *) glGetStringFunc(GL_EXTENSIONS);
  3010     } else {
  3011         extensions = NULL;
  3012     }
  3013     if (!extensions) {
  3014         return SDL_FALSE;
  3015     }
  3016     /*
  3017      * It takes a bit of care to be fool-proof about parsing the OpenGL
  3018      * extensions string. Don't be fooled by sub-strings, etc.
  3019      */
  3020 
  3021     start = extensions;
  3022 
  3023     for (;;) {
  3024         where = SDL_strstr(start, extension);
  3025         if (!where)
  3026             break;
  3027 
  3028         terminator = where + SDL_strlen(extension);
  3029         if (where == start || *(where - 1) == ' ')
  3030             if (*terminator == ' ' || *terminator == '\0')
  3031                 return SDL_TRUE;
  3032 
  3033         start = terminator;
  3034     }
  3035     return SDL_FALSE;
  3036 #else
  3037     return SDL_FALSE;
  3038 #endif
  3039 }
  3040 
  3041 int
  3042 SDL_GL_SetAttribute(SDL_GLattr attr, int value)
  3043 {
  3044 #if SDL_VIDEO_OPENGL || SDL_VIDEO_OPENGL_ES
  3045     int retval;
  3046 
  3047     if (!_this) {
  3048         SDL_UninitializedVideo();
  3049         return -1;
  3050     }
  3051     retval = 0;
  3052     switch (attr) {
  3053     case SDL_GL_RED_SIZE:
  3054         _this->gl_config.red_size = value;
  3055         break;
  3056     case SDL_GL_GREEN_SIZE:
  3057         _this->gl_config.green_size = value;
  3058         break;
  3059     case SDL_GL_BLUE_SIZE:
  3060         _this->gl_config.blue_size = value;
  3061         break;
  3062     case SDL_GL_ALPHA_SIZE:
  3063         _this->gl_config.alpha_size = value;
  3064         break;
  3065     case SDL_GL_DOUBLEBUFFER:
  3066         _this->gl_config.double_buffer = value;
  3067         break;
  3068     case SDL_GL_BUFFER_SIZE:
  3069         _this->gl_config.buffer_size = value;
  3070         break;
  3071     case SDL_GL_DEPTH_SIZE:
  3072         _this->gl_config.depth_size = value;
  3073         break;
  3074     case SDL_GL_STENCIL_SIZE:
  3075         _this->gl_config.stencil_size = value;
  3076         break;
  3077     case SDL_GL_ACCUM_RED_SIZE:
  3078         _this->gl_config.accum_red_size = value;
  3079         break;
  3080     case SDL_GL_ACCUM_GREEN_SIZE:
  3081         _this->gl_config.accum_green_size = value;
  3082         break;
  3083     case SDL_GL_ACCUM_BLUE_SIZE:
  3084         _this->gl_config.accum_blue_size = value;
  3085         break;
  3086     case SDL_GL_ACCUM_ALPHA_SIZE:
  3087         _this->gl_config.accum_alpha_size = value;
  3088         break;
  3089     case SDL_GL_STEREO:
  3090         _this->gl_config.stereo = value;
  3091         break;
  3092     case SDL_GL_MULTISAMPLEBUFFERS:
  3093         _this->gl_config.multisamplebuffers = value;
  3094         break;
  3095     case SDL_GL_MULTISAMPLESAMPLES:
  3096         _this->gl_config.multisamplesamples = value;
  3097         break;
  3098     case SDL_GL_ACCELERATED_VISUAL:
  3099         _this->gl_config.accelerated = value;
  3100         break;
  3101     case SDL_GL_RETAINED_BACKING:
  3102         _this->gl_config.retained_backing = value;
  3103         break;
  3104     case SDL_GL_CONTEXT_MAJOR_VERSION:
  3105         _this->gl_config.major_version = value;
  3106         break;
  3107     case SDL_GL_CONTEXT_MINOR_VERSION:
  3108         _this->gl_config.minor_version = value;
  3109         break;
  3110     default:
  3111         SDL_SetError("Unknown OpenGL attribute");
  3112         retval = -1;
  3113         break;
  3114     }
  3115     return retval;
  3116 #else
  3117     SDL_Unsupported();
  3118     return -1;
  3119 #endif /* SDL_VIDEO_OPENGL */
  3120 }
  3121 
  3122 int
  3123 SDL_GL_GetAttribute(SDL_GLattr attr, int *value)
  3124 {
  3125 #if SDL_VIDEO_OPENGL || SDL_VIDEO_OPENGL_ES
  3126     void (APIENTRY * glGetIntegervFunc) (GLenum pname, GLint * params);
  3127     GLenum(APIENTRY * glGetErrorFunc) (void);
  3128     GLenum attrib = 0;
  3129     GLenum error = 0;
  3130 
  3131     glGetIntegervFunc = SDL_GL_GetProcAddress("glGetIntegerv");
  3132     if (!glGetIntegervFunc) {
  3133         return -1;
  3134     }
  3135 
  3136     glGetErrorFunc = SDL_GL_GetProcAddress("glGetError");
  3137     if (!glGetErrorFunc) {
  3138         return -1;
  3139     }
  3140 
  3141     /* Clear value in any case */
  3142     *value = 0;
  3143 
  3144     switch (attr) {
  3145     case SDL_GL_RETAINED_BACKING:
  3146         *value = _this->gl_config.retained_backing;
  3147         return 0;
  3148     case SDL_GL_RED_SIZE:
  3149         attrib = GL_RED_BITS;
  3150         break;
  3151     case SDL_GL_BLUE_SIZE:
  3152         attrib = GL_BLUE_BITS;
  3153         break;
  3154     case SDL_GL_GREEN_SIZE:
  3155         attrib = GL_GREEN_BITS;
  3156         break;
  3157     case SDL_GL_ALPHA_SIZE:
  3158         attrib = GL_ALPHA_BITS;
  3159         break;
  3160     case SDL_GL_DOUBLEBUFFER:
  3161 #ifndef SDL_VIDEO_OPENGL_ES
  3162         attrib = GL_DOUBLEBUFFER;
  3163         break;
  3164 #else
  3165         /* OpenGL ES 1.0 and above specifications have EGL_SINGLE_BUFFER      */
  3166         /* parameter which switches double buffer to single buffer. OpenGL ES */
  3167         /* SDL driver must set proper value after initialization              */
  3168         *value = _this->gl_config.double_buffer;
  3169         return 0;
  3170 #endif
  3171     case SDL_GL_DEPTH_SIZE:
  3172         attrib = GL_DEPTH_BITS;
  3173         break;
  3174     case SDL_GL_STENCIL_SIZE:
  3175         attrib = GL_STENCIL_BITS;
  3176         break;
  3177 #ifndef SDL_VIDEO_OPENGL_ES
  3178     case SDL_GL_ACCUM_RED_SIZE:
  3179         attrib = GL_ACCUM_RED_BITS;
  3180         break;
  3181     case SDL_GL_ACCUM_GREEN_SIZE:
  3182         attrib = GL_ACCUM_GREEN_BITS;
  3183         break;
  3184     case SDL_GL_ACCUM_BLUE_SIZE:
  3185         attrib = GL_ACCUM_BLUE_BITS;
  3186         break;
  3187     case SDL_GL_ACCUM_ALPHA_SIZE:
  3188         attrib = GL_ACCUM_ALPHA_BITS;
  3189         break;
  3190     case SDL_GL_STEREO:
  3191         attrib = GL_STEREO;
  3192         break;
  3193 #else
  3194     case SDL_GL_ACCUM_RED_SIZE:
  3195     case SDL_GL_ACCUM_GREEN_SIZE:
  3196     case SDL_GL_ACCUM_BLUE_SIZE:
  3197     case SDL_GL_ACCUM_ALPHA_SIZE:
  3198     case SDL_GL_STEREO:
  3199         /* none of these are supported in OpenGL ES */
  3200         *value = 0;
  3201         return 0;
  3202 #endif
  3203     case SDL_GL_MULTISAMPLEBUFFERS:
  3204 #ifndef SDL_VIDEO_OPENGL_ES
  3205         attrib = GL_SAMPLE_BUFFERS_ARB;
  3206 #else
  3207         attrib = GL_SAMPLE_BUFFERS;
  3208 #endif
  3209         break;
  3210     case SDL_GL_MULTISAMPLESAMPLES:
  3211 #ifndef SDL_VIDEO_OPENGL_ES
  3212         attrib = GL_SAMPLES_ARB;
  3213 #else
  3214         attrib = GL_SAMPLES;
  3215 #endif
  3216         break;
  3217     case SDL_GL_BUFFER_SIZE:
  3218         {
  3219             GLint bits = 0;
  3220             GLint component;
  3221 
  3222             /*
  3223              * there doesn't seem to be a single flag in OpenGL
  3224              * for this!
  3225              */
  3226             glGetIntegervFunc(GL_RED_BITS, &component);
  3227             bits += component;
  3228             glGetIntegervFunc(GL_GREEN_BITS, &component);
  3229             bits += component;
  3230             glGetIntegervFunc(GL_BLUE_BITS, &component);
  3231             bits += component;
  3232             glGetIntegervFunc(GL_ALPHA_BITS, &component);
  3233             bits += component;
  3234 
  3235             *value = bits;
  3236             return 0;
  3237         }
  3238     case SDL_GL_ACCELERATED_VISUAL:
  3239         {
  3240             /* FIXME: How do we get this information? */
  3241             *value = (_this->gl_config.accelerated != 0);
  3242             return 0;
  3243         }
  3244     default:
  3245         SDL_SetError("Unknown OpenGL attribute");
  3246         return -1;
  3247     }
  3248 
  3249     glGetIntegervFunc(attrib, (GLint *) value);
  3250     error = glGetErrorFunc();
  3251     if (error != GL_NO_ERROR) {
  3252         switch (error) {
  3253         case GL_INVALID_ENUM:
  3254             {
  3255                 SDL_SetError("OpenGL error: GL_INVALID_ENUM");
  3256             }
  3257             break;
  3258         case GL_INVALID_VALUE:
  3259             {
  3260                 SDL_SetError("OpenGL error: GL_INVALID_VALUE");
  3261             }
  3262             break;
  3263         default:
  3264             {
  3265                 SDL_SetError("OpenGL error: %08X", error);
  3266             }
  3267             break;
  3268         }
  3269         return -1;
  3270     }
  3271     return 0;
  3272 #else
  3273     SDL_Unsupported();
  3274     return -1;
  3275 #endif /* SDL_VIDEO_OPENGL */
  3276 }
  3277 
  3278 SDL_GLContext
  3279 SDL_GL_CreateContext(SDL_WindowID windowID)
  3280 {
  3281     SDL_Window *window = SDL_GetWindowFromID(windowID);
  3282 
  3283     if (!window) {
  3284         return NULL;
  3285     }
  3286     if (!(window->flags & SDL_WINDOW_OPENGL)) {
  3287         SDL_SetError("The specified window isn't an OpenGL window");
  3288         return NULL;
  3289     }
  3290     return _this->GL_CreateContext(_this, window);
  3291 }
  3292 
  3293 int
  3294 SDL_GL_MakeCurrent(SDL_WindowID windowID, SDL_GLContext context)
  3295 {
  3296     SDL_Window *window = SDL_GetWindowFromID(windowID);
  3297 
  3298     if (window && !(window->flags & SDL_WINDOW_OPENGL)) {
  3299         SDL_SetError("The specified window isn't an OpenGL window");
  3300         return -1;
  3301     }
  3302     if (!context) {
  3303         window = NULL;
  3304     }
  3305     return _this->GL_MakeCurrent(_this, window, context);
  3306 }
  3307 
  3308 int
  3309 SDL_GL_SetSwapInterval(int interval)
  3310 {
  3311     if (!_this) {
  3312         SDL_UninitializedVideo();
  3313         return -1;
  3314     }
  3315     if (_this->GL_SetSwapInterval) {
  3316         return _this->GL_SetSwapInterval(_this, interval);
  3317     } else {
  3318         SDL_SetError("Setting the swap interval is not supported");
  3319         return -1;
  3320     }
  3321 }
  3322 
  3323 int
  3324 SDL_GL_GetSwapInterval(void)
  3325 {
  3326     if (!_this) {
  3327         SDL_UninitializedVideo();
  3328         return -1;
  3329     }
  3330     if (_this->GL_GetSwapInterval) {
  3331         return _this->GL_GetSwapInterval(_this);
  3332     } else {
  3333         SDL_SetError("Getting the swap interval is not supported");
  3334         return -1;
  3335     }
  3336 }
  3337 
  3338 void
  3339 SDL_GL_SwapWindow(SDL_WindowID windowID)
  3340 {
  3341     SDL_Window *window = SDL_GetWindowFromID(windowID);
  3342 
  3343     if (!window) {
  3344         return;
  3345     }
  3346     if (!(window->flags & SDL_WINDOW_OPENGL)) {
  3347         SDL_SetError("The specified window isn't an OpenGL window");
  3348         return;
  3349     }
  3350     _this->GL_SwapWindow(_this, window);
  3351 }
  3352 
  3353 void
  3354 SDL_GL_DeleteContext(SDL_GLContext context)
  3355 {
  3356     if (!_this || !context) {
  3357         return;
  3358     }
  3359     _this->GL_MakeCurrent(_this, NULL, NULL);
  3360     _this->GL_DeleteContext(_this, context);
  3361 }
  3362 
  3363 #if 0                           // FIXME
  3364 /*
  3365  * Utility function used by SDL_WM_SetIcon(); flags & 1 for color key, flags
  3366  * & 2 for alpha channel.
  3367  */
  3368 static void
  3369 CreateMaskFromColorKeyOrAlpha(SDL_Surface * icon, Uint8 * mask, int flags)
  3370 {
  3371     int x, y;
  3372     Uint32 colorkey;
  3373 #define SET_MASKBIT(icon, x, y, mask) \
  3374 	mask[(y*((icon->w+7)/8))+(x/8)] &= ~(0x01<<(7-(x%8)))
  3375 
  3376     colorkey = icon->format->colorkey;
  3377     switch (icon->format->BytesPerPixel) {
  3378     case 1:
  3379         {
  3380             Uint8 *pixels;
  3381             for (y = 0; y < icon->h; ++y) {
  3382                 pixels = (Uint8 *) icon->pixels + y * icon->pitch;
  3383                 for (x = 0; x < icon->w; ++x) {
  3384                     if (*pixels++ == colorkey) {
  3385                         SET_MASKBIT(icon, x, y, mask);
  3386                     }
  3387                 }
  3388             }
  3389         }
  3390         break;
  3391 
  3392     case 2:
  3393         {
  3394             Uint16 *pixels;
  3395             for (y = 0; y < icon->h; ++y) {
  3396                 pixels = (Uint16 *) icon->pixels + y * icon->pitch / 2;
  3397                 for (x = 0; x < icon->w; ++x) {
  3398                     if ((flags & 1) && *pixels == colorkey) {
  3399                         SET_MASKBIT(icon, x, y, mask);
  3400                     } else if ((flags & 2)
  3401                                && (*pixels & icon->format->Amask) == 0) {
  3402                         SET_MASKBIT(icon, x, y, mask);
  3403                     }
  3404                     pixels++;
  3405                 }
  3406             }
  3407         }
  3408         break;
  3409 
  3410     case 4:
  3411         {
  3412             Uint32 *pixels;
  3413             for (y = 0; y < icon->h; ++y) {
  3414                 pixels = (Uint32 *) icon->pixels + y * icon->pitch / 4;
  3415                 for (x = 0; x < icon->w; ++x) {
  3416                     if ((flags & 1) && *pixels == colorkey) {
  3417                         SET_MASKBIT(icon, x, y, mask);
  3418                     } else if ((flags & 2)
  3419                                && (*pixels & icon->format->Amask) == 0) {
  3420                         SET_MASKBIT(icon, x, y, mask);
  3421                     }
  3422                     pixels++;
  3423                 }
  3424             }
  3425         }
  3426         break;
  3427     }
  3428 }
  3429 
  3430 /*
  3431  * Sets the window manager icon for the display window.
  3432  */
  3433 void
  3434 SDL_WM_SetIcon(SDL_Surface * icon, Uint8 * mask)
  3435 {
  3436     if (icon && _this->SetIcon) {
  3437         /* Generate a mask if necessary, and create the icon! */
  3438         if (mask == NULL) {
  3439             int mask_len = icon->h * (icon->w + 7) / 8;
  3440             int flags = 0;
  3441             mask = (Uint8 *) SDL_malloc(mask_len);
  3442             if (mask == NULL) {
  3443                 return;
  3444             }
  3445             SDL_memset(mask, ~0, mask_len);
  3446             if (icon->flags & SDL_SRCCOLORKEY)
  3447                 flags |= 1;
  3448             if (icon->flags & SDL_SRCALPHA)
  3449                 flags |= 2;
  3450             if (flags) {
  3451                 CreateMaskFromColorKeyOrAlpha(icon, mask, flags);
  3452             }
  3453             _this->SetIcon(_this, icon, mask);
  3454             SDL_free(mask);
  3455         } else {
  3456             _this->SetIcon(_this, icon, mask);
  3457         }
  3458     }
  3459 }
  3460 #endif
  3461 
  3462 SDL_bool
  3463 SDL_GetWindowWMInfo(SDL_WindowID windowID, struct SDL_SysWMinfo *info)
  3464 {
  3465     SDL_Window *window = SDL_GetWindowFromID(windowID);
  3466 
  3467     if (!window || !_this->GetWindowWMInfo) {
  3468         return SDL_FALSE;
  3469     }
  3470     return (_this->GetWindowWMInfo(_this, window, info));
  3471 }
  3472 
  3473 void
  3474 SDL_StartTextInput(void)
  3475 {
  3476     if (_this->StartTextInput) {
  3477         _this->StartTextInput(_this);
  3478     }
  3479     SDL_EventState(SDL_TEXTINPUT, SDL_ENABLE);
  3480     SDL_EventState(SDL_TEXTEDITING, SDL_ENABLE);
  3481 }
  3482 
  3483 void
  3484 SDL_StopTextInput(void)
  3485 {
  3486     if (_this->StopTextInput) {
  3487         _this->StopTextInput(_this);
  3488     }
  3489     SDL_EventState(SDL_TEXTINPUT, SDL_DISABLE);
  3490     SDL_EventState(SDL_TEXTEDITING, SDL_DISABLE);
  3491 }
  3492 
  3493 void
  3494 SDL_SetTextInputRect(SDL_Rect *rect)
  3495 {
  3496     if (_this->SetTextInputRect) {
  3497         _this->SetTextInputRect(_this, rect);
  3498     }
  3499 }
  3500 
  3501 /* vi: set ts=4 sw=4 expandtab: */