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