src/video/SDL_video.c
author Sam Lantinga <slouken@libsdl.org>
Tue, 01 Dec 2009 09:04:28 +0000
changeset 3503 7931094e4c48
parent 3502 98a819296cdc
child 3511 42cfa69e351b
permissions -rw-r--r--
Explicitly clear fullscreen status rather than relying on the window focus behavior.
     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     SDL_UpdateFullscreenMode(window, SDL_TRUE);
  1439     if (display->gamma && _this->SetDisplayGammaRamp) {
  1440         _this->SetDisplayGammaRamp(_this, display, display->gamma);
  1441     }
  1442     if ((window->flags & (SDL_WINDOW_INPUT_GRABBED | SDL_WINDOW_FULLSCREEN))
  1443         && _this->SetWindowGrab) {
  1444         _this->SetWindowGrab(_this, window);
  1445     }
  1446 }
  1447 
  1448 void
  1449 SDL_OnWindowFocusLost(SDL_Window * window)
  1450 {
  1451     SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);
  1452 
  1453     SDL_UpdateFullscreenMode(window, SDL_FALSE);
  1454     if (display->gamma && _this->SetDisplayGammaRamp) {
  1455         _this->SetDisplayGammaRamp(_this, display, display->saved_gamma);
  1456     }
  1457     if ((window->flags & (SDL_WINDOW_INPUT_GRABBED | SDL_WINDOW_FULLSCREEN))
  1458         && _this->SetWindowGrab) {
  1459         _this->SetWindowGrab(_this, window);
  1460     }
  1461 }
  1462 
  1463 SDL_WindowID
  1464 SDL_GetFocusWindow(void)
  1465 {
  1466     SDL_VideoDisplay *display;
  1467     int i;
  1468 
  1469     if (!_this) {
  1470         return 0;
  1471     }
  1472     display = &SDL_CurrentDisplay;
  1473     for (i = 0; i < display->num_windows; ++i) {
  1474         SDL_Window *window = &display->windows[i];
  1475 
  1476         if (window->flags & SDL_WINDOW_INPUT_FOCUS) {
  1477             return window->id;
  1478         }
  1479     }
  1480     return 0;
  1481 }
  1482 
  1483 void
  1484 SDL_DestroyWindow(SDL_WindowID windowID)
  1485 {
  1486     int i, j;
  1487 
  1488     if (!_this) {
  1489         return;
  1490     }
  1491 
  1492     /* Restore video mode, etc. */
  1493     SDL_SetWindowFullscreen(windowID, 0);
  1494 
  1495     for (i = 0; i < _this->num_displays; ++i) {
  1496         SDL_VideoDisplay *display = &_this->displays[i];
  1497         for (j = 0; j < display->num_windows; ++j) {
  1498             SDL_Window *window = &display->windows[j];
  1499             if (window->id != windowID) {
  1500                 continue;
  1501             }
  1502             if (window->title) {
  1503                 SDL_free(window->title);
  1504                 window->title = NULL;
  1505             }
  1506             if (window->renderer) {
  1507                 SDL_DestroyRenderer(window->id);
  1508                 window->renderer = NULL;
  1509             }
  1510             if (_this->DestroyWindow) {
  1511                 _this->DestroyWindow(_this, window);
  1512             }
  1513             if (window->flags & SDL_WINDOW_OPENGL) {
  1514                 SDL_GL_UnloadLibrary();
  1515             }
  1516             if (j != display->num_windows - 1) {
  1517                 SDL_memcpy(&display->windows[i],
  1518                            &display->windows[i + 1],
  1519                            (display->num_windows - i - 1) * sizeof(*window));
  1520             }
  1521             --display->num_windows;
  1522             return;
  1523         }
  1524     }
  1525 }
  1526 
  1527 void
  1528 SDL_AddRenderDriver(SDL_VideoDisplay * display, const SDL_RenderDriver * driver)
  1529 {
  1530     SDL_RenderDriver *render_drivers;
  1531 
  1532     render_drivers =
  1533         SDL_realloc(display->render_drivers,
  1534                     (display->num_render_drivers +
  1535                      1) * sizeof(*render_drivers));
  1536     if (render_drivers) {
  1537         render_drivers[display->num_render_drivers] = *driver;
  1538         display->render_drivers = render_drivers;
  1539         display->num_render_drivers++;
  1540     }
  1541 }
  1542 
  1543 int
  1544 SDL_GetNumRenderDrivers(void)
  1545 {
  1546     if (_this) {
  1547         return SDL_CurrentDisplay.num_render_drivers;
  1548     }
  1549     return 0;
  1550 }
  1551 
  1552 int
  1553 SDL_GetRenderDriverInfo(int index, SDL_RendererInfo * info)
  1554 {
  1555     if (!_this) {
  1556         SDL_UninitializedVideo();
  1557         return -1;
  1558     }
  1559     if (index < 0 || index >= SDL_GetNumRenderDrivers()) {
  1560         SDL_SetError("index must be in the range of 0 - %d",
  1561                      SDL_GetNumRenderDrivers() - 1);
  1562         return -1;
  1563     }
  1564     *info = SDL_CurrentDisplay.render_drivers[index].info;
  1565     return 0;
  1566 }
  1567 
  1568 int
  1569 SDL_CreateRenderer(SDL_WindowID windowID, int index, Uint32 flags)
  1570 {
  1571     SDL_Window *window = SDL_GetWindowFromID(windowID);
  1572 
  1573     if (!window) {
  1574         SDL_SetError("Invalid window ID");
  1575         return -1;
  1576     }
  1577 
  1578     /* Free any existing renderer */
  1579     SDL_DestroyRenderer(windowID);
  1580 
  1581     if (index < 0) {
  1582         char *override = SDL_getenv("SDL_VIDEO_RENDERER");
  1583         int n = SDL_GetNumRenderDrivers();
  1584 
  1585 #if SDL_VIDEO_RENDER_OGL
  1586         if (!override && (window->flags & SDL_WINDOW_OPENGL)) {
  1587             override = "opengl";
  1588         }
  1589 #endif /* SDL_VIDEO_RENDER_OGL */
  1590 #if SDL_VIDEO_RENDER_OGL_ES
  1591         if (!override && (window->flags & SDL_WINDOW_OPENGL)) {
  1592             override = "opengl_es";
  1593         }
  1594 #endif /* SDL_VIDEO_RENDER_OGL_ES */
  1595         if (override) {
  1596             for (index = 0; index < n; ++index) {
  1597                 SDL_RenderDriver *driver =
  1598                     &SDL_CurrentDisplay.render_drivers[index];
  1599 
  1600                 if (SDL_strcasecmp(override, driver->info.name) == 0) {
  1601                     /* Create a new renderer instance */
  1602                     window->renderer = driver->CreateRenderer(window, flags);
  1603                     break;
  1604                 }
  1605             }
  1606         } else {
  1607             for (index = 0; index < n; ++index) {
  1608                 SDL_RenderDriver *driver =
  1609                     &SDL_CurrentDisplay.render_drivers[index];
  1610 
  1611                 if ((driver->info.flags & flags) == flags) {
  1612                     /* Create a new renderer instance */
  1613                     window->renderer = driver->CreateRenderer(window, flags);
  1614                     if (window->renderer) {
  1615                         /* Yay, we got one! */
  1616                         break;
  1617                     }
  1618                 }
  1619             }
  1620         }
  1621         if (index == n) {
  1622             SDL_SetError("Couldn't find matching render driver");
  1623             return -1;
  1624         }
  1625     } else {
  1626         if (index >= SDL_GetNumRenderDrivers()) {
  1627             SDL_SetError("index must be -1 or in the range of 0 - %d",
  1628                          SDL_GetNumRenderDrivers() - 1);
  1629             return -1;
  1630         }
  1631         /* Create a new renderer instance */
  1632         window->renderer = SDL_CurrentDisplay.render_drivers[index].CreateRenderer(window, flags);
  1633     }
  1634 
  1635     if (window->renderer == NULL) {
  1636         /* Assuming renderer set its error */
  1637         return -1;
  1638     }
  1639 
  1640     SDL_SelectRenderer(window->id);
  1641 
  1642     return 0;
  1643 }
  1644 
  1645 int
  1646 SDL_SelectRenderer(SDL_WindowID windowID)
  1647 {
  1648     SDL_Window *window = SDL_GetWindowFromID(windowID);
  1649     SDL_Renderer *renderer;
  1650 
  1651     if (!window) {
  1652         SDL_SetError("Invalid window ID");
  1653         return -1;
  1654     }
  1655     renderer = window->renderer;
  1656     if (!renderer) {
  1657         SDL_SetError("Use SDL_CreateRenderer() to create a renderer");
  1658         return -1;
  1659     }
  1660     if (renderer->ActivateRenderer) {
  1661         if (renderer->ActivateRenderer(renderer) < 0) {
  1662             return -1;
  1663         }
  1664     }
  1665     SDL_CurrentDisplay.current_renderer = renderer;
  1666     return 0;
  1667 }
  1668 
  1669 int
  1670 SDL_GetRendererInfo(SDL_RendererInfo * info)
  1671 {
  1672     SDL_Renderer *renderer = SDL_GetCurrentRenderer(SDL_FALSE);
  1673     if (!renderer) {
  1674         return -1;
  1675     }
  1676     *info = renderer->info;
  1677     return 0;
  1678 }
  1679 
  1680 SDL_TextureID
  1681 SDL_CreateTexture(Uint32 format, int access, int w, int h)
  1682 {
  1683     int hash;
  1684     SDL_Renderer *renderer;
  1685     SDL_Texture *texture;
  1686 
  1687     renderer = SDL_GetCurrentRenderer(SDL_TRUE);
  1688     if (!renderer) {
  1689         return 0;
  1690     }
  1691     if (!renderer->CreateTexture) {
  1692         SDL_Unsupported();
  1693         return 0;
  1694     }
  1695     texture = (SDL_Texture *) SDL_calloc(1, sizeof(*texture));
  1696     if (!texture) {
  1697         SDL_OutOfMemory();
  1698         return 0;
  1699     }
  1700     texture->id = _this->next_object_id++;
  1701     texture->format = format;
  1702     texture->access = access;
  1703     texture->w = w;
  1704     texture->h = h;
  1705     texture->r = 255;
  1706     texture->g = 255;
  1707     texture->b = 255;
  1708     texture->a = 255;
  1709     texture->renderer = renderer;
  1710 
  1711     if (renderer->CreateTexture(renderer, texture) < 0) {
  1712         if (renderer->DestroyTexture) {
  1713             renderer->DestroyTexture(renderer, texture);
  1714         }
  1715         SDL_free(texture);
  1716         return 0;
  1717     }
  1718     hash = (texture->id % SDL_arraysize(SDL_CurrentDisplay.textures));
  1719     texture->next = SDL_CurrentDisplay.textures[hash];
  1720     SDL_CurrentDisplay.textures[hash] = texture;
  1721 
  1722     return texture->id;
  1723 }
  1724 
  1725 SDL_TextureID
  1726 SDL_CreateTextureFromSurface(Uint32 format, SDL_Surface * surface)
  1727 {
  1728     SDL_TextureID textureID;
  1729     Uint32 requested_format = format;
  1730     SDL_PixelFormat *fmt;
  1731     SDL_Renderer *renderer;
  1732     int bpp;
  1733     Uint32 Rmask, Gmask, Bmask, Amask;
  1734 
  1735     if (!surface) {
  1736         SDL_SetError("SDL_CreateTextureFromSurface() passed NULL surface");
  1737         return 0;
  1738     }
  1739     fmt = surface->format;
  1740 
  1741     renderer = SDL_GetCurrentRenderer(SDL_TRUE);
  1742     if (!renderer) {
  1743         return 0;
  1744     }
  1745 
  1746     if (format) {
  1747         if (!SDL_PixelFormatEnumToMasks
  1748             (format, &bpp, &Rmask, &Gmask, &Bmask, &Amask)) {
  1749             SDL_SetError("Unknown pixel format");
  1750             return 0;
  1751         }
  1752     } else {
  1753         if (surface->format->Amask
  1754             || !(surface->map->info.flags &
  1755                  (SDL_COPY_COLORKEY | SDL_COPY_MASK | SDL_COPY_BLEND))) {
  1756             Uint32 it;
  1757             int pfmt;
  1758 
  1759             /* Pixel formats, sorted by best first */
  1760             static const Uint32 sdl_pformats[] = {
  1761                 SDL_PIXELFORMAT_ARGB8888,
  1762                 SDL_PIXELFORMAT_RGBA8888,
  1763                 SDL_PIXELFORMAT_ABGR8888,
  1764                 SDL_PIXELFORMAT_BGRA8888,
  1765                 SDL_PIXELFORMAT_RGB888,
  1766                 SDL_PIXELFORMAT_BGR888,
  1767                 SDL_PIXELFORMAT_RGB24,
  1768                 SDL_PIXELFORMAT_BGR24,
  1769                 SDL_PIXELFORMAT_RGB565,
  1770                 SDL_PIXELFORMAT_BGR565,
  1771                 SDL_PIXELFORMAT_ARGB1555,
  1772                 SDL_PIXELFORMAT_ABGR1555,
  1773                 SDL_PIXELFORMAT_RGB555,
  1774                 SDL_PIXELFORMAT_BGR555,
  1775                 SDL_PIXELFORMAT_ARGB4444,
  1776                 SDL_PIXELFORMAT_ABGR4444,
  1777                 SDL_PIXELFORMAT_RGB444,
  1778                 SDL_PIXELFORMAT_ARGB2101010,
  1779                 SDL_PIXELFORMAT_INDEX8,
  1780                 SDL_PIXELFORMAT_INDEX4LSB,
  1781                 SDL_PIXELFORMAT_INDEX4MSB,
  1782                 SDL_PIXELFORMAT_RGB332,
  1783                 SDL_PIXELFORMAT_INDEX1LSB,
  1784                 SDL_PIXELFORMAT_INDEX1MSB,
  1785                 SDL_PIXELFORMAT_UNKNOWN
  1786             };
  1787 
  1788             bpp = fmt->BitsPerPixel;
  1789             Rmask = fmt->Rmask;
  1790             Gmask = fmt->Gmask;
  1791             Bmask = fmt->Bmask;
  1792             Amask = fmt->Amask;
  1793 
  1794             format =
  1795                 SDL_MasksToPixelFormatEnum(bpp, Rmask, Gmask, Bmask, Amask);
  1796             if (!format) {
  1797                 SDL_SetError("Unknown pixel format");
  1798                 return 0;
  1799             }
  1800 
  1801             /* Search requested format in the supported texture */
  1802             /* formats by current renderer                      */
  1803             for (it = 0; it < renderer->info.num_texture_formats; it++) {
  1804                 if (renderer->info.texture_formats[it] == format) {
  1805                     break;
  1806                 }
  1807             }
  1808 
  1809             /* If requested format can't be found, search any best */
  1810             /* format which renderer provides                      */
  1811             if (it == renderer->info.num_texture_formats) {
  1812                 pfmt = 0;
  1813                 for (;;) {
  1814                     if (sdl_pformats[pfmt] == SDL_PIXELFORMAT_UNKNOWN) {
  1815                         break;
  1816                     }
  1817 
  1818                     for (it = 0; it < renderer->info.num_texture_formats;
  1819                          it++) {
  1820                         if (renderer->info.texture_formats[it] ==
  1821                             sdl_pformats[pfmt]) {
  1822                             break;
  1823                         }
  1824                     }
  1825 
  1826                     if (it != renderer->info.num_texture_formats) {
  1827                         /* The best format has been found */
  1828                         break;
  1829                     }
  1830                     pfmt++;
  1831                 }
  1832 
  1833                 /* If any format can't be found, then return an error */
  1834                 if (it == renderer->info.num_texture_formats) {
  1835                     SDL_SetError
  1836                         ("Any of the supported pixel formats can't be found");
  1837                     return 0;
  1838                 }
  1839 
  1840                 /* Convert found pixel format back to color masks */
  1841                 if (SDL_PixelFormatEnumToMasks
  1842                     (renderer->info.texture_formats[it], &bpp, &Rmask, &Gmask,
  1843                      &Bmask, &Amask) != SDL_TRUE) {
  1844                     SDL_SetError("Unknown pixel format");
  1845                     return 0;
  1846                 }
  1847             }
  1848         } else {
  1849             /* Need a format with alpha */
  1850             Uint32 it;
  1851             int apfmt;
  1852 
  1853             /* Pixel formats with alpha, sorted by best first */
  1854             static const Uint32 sdl_alpha_pformats[] = {
  1855                 SDL_PIXELFORMAT_ARGB8888,
  1856                 SDL_PIXELFORMAT_RGBA8888,
  1857                 SDL_PIXELFORMAT_ABGR8888,
  1858                 SDL_PIXELFORMAT_BGRA8888,
  1859                 SDL_PIXELFORMAT_ARGB1555,
  1860                 SDL_PIXELFORMAT_ABGR1555,
  1861                 SDL_PIXELFORMAT_ARGB4444,
  1862                 SDL_PIXELFORMAT_ABGR4444,
  1863                 SDL_PIXELFORMAT_ARGB2101010,
  1864                 SDL_PIXELFORMAT_UNKNOWN
  1865             };
  1866 
  1867             if (surface->format->Amask) {
  1868                 /* If surface already has alpha, then try an original */
  1869                 /* surface format first                               */
  1870                 bpp = fmt->BitsPerPixel;
  1871                 Rmask = fmt->Rmask;
  1872                 Gmask = fmt->Gmask;
  1873                 Bmask = fmt->Bmask;
  1874                 Amask = fmt->Amask;
  1875             } else {
  1876                 bpp = 32;
  1877                 Rmask = 0x00FF0000;
  1878                 Gmask = 0x0000FF00;
  1879                 Bmask = 0x000000FF;
  1880                 Amask = 0xFF000000;
  1881             }
  1882 
  1883             format =
  1884                 SDL_MasksToPixelFormatEnum(bpp, Rmask, Gmask, Bmask, Amask);
  1885             if (!format) {
  1886                 SDL_SetError("Unknown pixel format");
  1887                 return 0;
  1888             }
  1889 
  1890             /* Search this format in the supported texture formats */
  1891             /* by current renderer                                 */
  1892             for (it = 0; it < renderer->info.num_texture_formats; it++) {
  1893                 if (renderer->info.texture_formats[it] == format) {
  1894                     break;
  1895                 }
  1896             }
  1897 
  1898             /* If this format can't be found, search any best       */
  1899             /* compatible format with alpha which renderer provides */
  1900             if (it == renderer->info.num_texture_formats) {
  1901                 apfmt = 0;
  1902                 for (;;) {
  1903                     if (sdl_alpha_pformats[apfmt] == SDL_PIXELFORMAT_UNKNOWN) {
  1904                         break;
  1905                     }
  1906 
  1907                     for (it = 0; it < renderer->info.num_texture_formats;
  1908                          it++) {
  1909                         if (renderer->info.texture_formats[it] ==
  1910                             sdl_alpha_pformats[apfmt]) {
  1911                             break;
  1912                         }
  1913                     }
  1914 
  1915                     if (it != renderer->info.num_texture_formats) {
  1916                         /* Compatible format has been found */
  1917                         break;
  1918                     }
  1919                     apfmt++;
  1920                 }
  1921 
  1922                 /* If compatible format can't be found, then return an error */
  1923                 if (it == renderer->info.num_texture_formats) {
  1924                     SDL_SetError("Compatible pixel format can't be found");
  1925                     return 0;
  1926                 }
  1927 
  1928                 /* Convert found pixel format back to color masks */
  1929                 if (SDL_PixelFormatEnumToMasks
  1930                     (renderer->info.texture_formats[it], &bpp, &Rmask, &Gmask,
  1931                      &Bmask, &Amask) != SDL_TRUE) {
  1932                     SDL_SetError("Unknown pixel format");
  1933                     return 0;
  1934                 }
  1935             }
  1936         }
  1937 
  1938         format = SDL_MasksToPixelFormatEnum(bpp, Rmask, Gmask, Bmask, Amask);
  1939         if (!format) {
  1940             SDL_SetError("Unknown pixel format");
  1941             return 0;
  1942         }
  1943     }
  1944 
  1945     textureID =
  1946         SDL_CreateTexture(format, SDL_TEXTUREACCESS_STATIC, surface->w,
  1947                           surface->h);
  1948     if (!textureID && !requested_format) {
  1949         SDL_DisplayMode desktop_mode;
  1950         SDL_GetDesktopDisplayMode(&desktop_mode);
  1951         format = desktop_mode.format;
  1952         textureID =
  1953             SDL_CreateTexture(format, SDL_TEXTUREACCESS_STATIC, surface->w,
  1954                               surface->h);
  1955     }
  1956     if (!textureID) {
  1957         return 0;
  1958     }
  1959     if (bpp == fmt->BitsPerPixel && Rmask == fmt->Rmask && Gmask == fmt->Gmask
  1960         && Bmask == fmt->Bmask && Amask == fmt->Amask) {
  1961         if (SDL_MUSTLOCK(surface)) {
  1962             SDL_LockSurface(surface);
  1963             SDL_UpdateTexture(textureID, NULL, surface->pixels,
  1964                               surface->pitch);
  1965             SDL_UnlockSurface(surface);
  1966         } else {
  1967             SDL_UpdateTexture(textureID, NULL, surface->pixels,
  1968                               surface->pitch);
  1969         }
  1970     } else {
  1971         SDL_PixelFormat dst_fmt;
  1972         SDL_Surface *dst = NULL;
  1973 
  1974         /* Set up a destination surface for the texture update */
  1975         SDL_InitFormat(&dst_fmt, bpp, Rmask, Gmask, Bmask, Amask);
  1976         if (SDL_ISPIXELFORMAT_INDEXED(format)) {
  1977             dst_fmt.palette =
  1978                 SDL_AllocPalette((1 << SDL_BITSPERPIXEL(format)));
  1979             if (dst_fmt.palette) {
  1980                 /*
  1981                  * FIXME: Should we try to copy
  1982                  * fmt->palette?
  1983                  */
  1984                 SDL_DitherColors(dst_fmt.palette->colors,
  1985                                  SDL_BITSPERPIXEL(format));
  1986             }
  1987         }
  1988         dst = SDL_ConvertSurface(surface, &dst_fmt, 0);
  1989         if (dst) {
  1990             SDL_UpdateTexture(textureID, NULL, dst->pixels, dst->pitch);
  1991             SDL_FreeSurface(dst);
  1992         }
  1993         if (dst_fmt.palette) {
  1994             SDL_FreePalette(dst_fmt.palette);
  1995         }
  1996         if (!dst) {
  1997             SDL_DestroyTexture(textureID);
  1998             return 0;
  1999         }
  2000     }
  2001 
  2002     {
  2003         Uint8 r, g, b, a;
  2004         int blendMode;
  2005         int scaleMode;
  2006 
  2007         SDL_GetSurfaceColorMod(surface, &r, &g, &b);
  2008         SDL_SetTextureColorMod(textureID, r, g, b);
  2009 
  2010         SDL_GetSurfaceAlphaMod(surface, &a);
  2011         SDL_SetTextureAlphaMod(textureID, a);
  2012 
  2013         SDL_GetSurfaceBlendMode(surface, &blendMode);
  2014         SDL_SetTextureBlendMode(textureID, blendMode);
  2015 
  2016         SDL_GetSurfaceScaleMode(surface, &scaleMode);
  2017         SDL_SetTextureScaleMode(textureID, scaleMode);
  2018     }
  2019 
  2020     if (SDL_ISPIXELFORMAT_INDEXED(format) && fmt->palette) {
  2021         SDL_SetTexturePalette(textureID, fmt->palette->colors, 0,
  2022                               fmt->palette->ncolors);
  2023     }
  2024     return textureID;
  2025 }
  2026 
  2027 static __inline__ SDL_Texture *
  2028 SDL_GetTextureFromID(SDL_TextureID textureID)
  2029 {
  2030     int hash;
  2031     SDL_Texture *texture;
  2032 
  2033     if (!_this) {
  2034         return NULL;
  2035     }
  2036     hash = (textureID % SDL_arraysize(SDL_CurrentDisplay.textures));
  2037     for (texture = SDL_CurrentDisplay.textures[hash]; texture;
  2038          texture = texture->next) {
  2039         if (texture->id == textureID) {
  2040             return texture;
  2041         }
  2042     }
  2043     return NULL;
  2044 }
  2045 
  2046 int
  2047 SDL_QueryTexture(SDL_TextureID textureID, Uint32 * format, int *access,
  2048                  int *w, int *h)
  2049 {
  2050     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  2051 
  2052     if (!texture) {
  2053         return -1;
  2054     }
  2055     if (format) {
  2056         *format = texture->format;
  2057     }
  2058     if (access) {
  2059         *access = texture->access;
  2060     }
  2061     if (w) {
  2062         *w = texture->w;
  2063     }
  2064     if (h) {
  2065         *h = texture->h;
  2066     }
  2067     return 0;
  2068 }
  2069 
  2070 int
  2071 SDL_QueryTexturePixels(SDL_TextureID textureID, void **pixels, int *pitch)
  2072 {
  2073     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  2074     SDL_Renderer *renderer;
  2075 
  2076     if (!texture) {
  2077         return -1;
  2078     }
  2079     renderer = texture->renderer;
  2080     if (!renderer->QueryTexturePixels) {
  2081         SDL_Unsupported();
  2082         return -1;
  2083     }
  2084     return renderer->QueryTexturePixels(renderer, texture, pixels, pitch);
  2085 }
  2086 
  2087 int
  2088 SDL_SetTexturePalette(SDL_TextureID textureID, const SDL_Color * colors,
  2089                       int firstcolor, int ncolors)
  2090 {
  2091     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  2092     SDL_Renderer *renderer;
  2093 
  2094     if (!texture) {
  2095         return -1;
  2096     }
  2097     renderer = texture->renderer;
  2098     if (!renderer->SetTexturePalette) {
  2099         SDL_Unsupported();
  2100         return -1;
  2101     }
  2102     return renderer->SetTexturePalette(renderer, texture, colors, firstcolor,
  2103                                        ncolors);
  2104 }
  2105 
  2106 int
  2107 SDL_GetTexturePalette(SDL_TextureID textureID, SDL_Color * colors,
  2108                       int firstcolor, int ncolors)
  2109 {
  2110     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  2111     SDL_Renderer *renderer;
  2112 
  2113     if (!texture) {
  2114         return -1;
  2115     }
  2116     renderer = texture->renderer;
  2117     if (!renderer->GetTexturePalette) {
  2118         SDL_Unsupported();
  2119         return -1;
  2120     }
  2121     return renderer->GetTexturePalette(renderer, texture, colors, firstcolor,
  2122                                        ncolors);
  2123 }
  2124 
  2125 int
  2126 SDL_SetTextureColorMod(SDL_TextureID textureID, Uint8 r, Uint8 g, Uint8 b)
  2127 {
  2128     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  2129     SDL_Renderer *renderer;
  2130 
  2131     if (!texture) {
  2132         return -1;
  2133     }
  2134     renderer = texture->renderer;
  2135     if (!renderer->SetTextureColorMod) {
  2136         SDL_Unsupported();
  2137         return -1;
  2138     }
  2139     if (r < 255 || g < 255 || b < 255) {
  2140         texture->modMode |= SDL_TEXTUREMODULATE_COLOR;
  2141     } else {
  2142         texture->modMode &= ~SDL_TEXTUREMODULATE_COLOR;
  2143     }
  2144     texture->r = r;
  2145     texture->g = g;
  2146     texture->b = b;
  2147     return renderer->SetTextureColorMod(renderer, texture);
  2148 }
  2149 
  2150 int
  2151 SDL_GetTextureColorMod(SDL_TextureID textureID, Uint8 * r, Uint8 * g,
  2152                        Uint8 * b)
  2153 {
  2154     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  2155     SDL_Renderer *renderer;
  2156 
  2157     if (!texture) {
  2158         return -1;
  2159     }
  2160     renderer = texture->renderer;
  2161     if (r) {
  2162         *r = texture->r;
  2163     }
  2164     if (g) {
  2165         *g = texture->g;
  2166     }
  2167     if (b) {
  2168         *b = texture->b;
  2169     }
  2170     return 0;
  2171 }
  2172 
  2173 int
  2174 SDL_SetTextureAlphaMod(SDL_TextureID textureID, Uint8 alpha)
  2175 {
  2176     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  2177     SDL_Renderer *renderer;
  2178 
  2179     if (!texture) {
  2180         return -1;
  2181     }
  2182     renderer = texture->renderer;
  2183     if (!renderer->SetTextureAlphaMod) {
  2184         SDL_Unsupported();
  2185         return -1;
  2186     }
  2187     if (alpha < 255) {
  2188         texture->modMode |= SDL_TEXTUREMODULATE_ALPHA;
  2189     } else {
  2190         texture->modMode &= ~SDL_TEXTUREMODULATE_ALPHA;
  2191     }
  2192     texture->a = alpha;
  2193     return renderer->SetTextureAlphaMod(renderer, texture);
  2194 }
  2195 
  2196 int
  2197 SDL_GetTextureAlphaMod(SDL_TextureID textureID, Uint8 * alpha)
  2198 {
  2199     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  2200 
  2201     if (!texture) {
  2202         return -1;
  2203     }
  2204     if (alpha) {
  2205         *alpha = texture->a;
  2206     }
  2207     return 0;
  2208 }
  2209 
  2210 int
  2211 SDL_SetTextureBlendMode(SDL_TextureID textureID, int blendMode)
  2212 {
  2213     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  2214     SDL_Renderer *renderer;
  2215 
  2216     if (!texture) {
  2217         return -1;
  2218     }
  2219     renderer = texture->renderer;
  2220     if (!renderer->SetTextureBlendMode) {
  2221         SDL_Unsupported();
  2222         return -1;
  2223     }
  2224     texture->blendMode = blendMode;
  2225     return renderer->SetTextureBlendMode(renderer, texture);
  2226 }
  2227 
  2228 int
  2229 SDL_GetTextureBlendMode(SDL_TextureID textureID, int *blendMode)
  2230 {
  2231     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  2232 
  2233     if (!texture) {
  2234         return -1;
  2235     }
  2236     if (blendMode) {
  2237         *blendMode = texture->blendMode;
  2238     }
  2239     return 0;
  2240 }
  2241 
  2242 int
  2243 SDL_SetTextureScaleMode(SDL_TextureID textureID, int scaleMode)
  2244 {
  2245     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  2246     SDL_Renderer *renderer;
  2247 
  2248     if (!texture) {
  2249         return -1;
  2250     }
  2251     renderer = texture->renderer;
  2252     if (!renderer->SetTextureScaleMode) {
  2253         SDL_Unsupported();
  2254         return -1;
  2255     }
  2256     texture->scaleMode = scaleMode;
  2257     return renderer->SetTextureScaleMode(renderer, texture);
  2258 }
  2259 
  2260 int
  2261 SDL_GetTextureScaleMode(SDL_TextureID textureID, int *scaleMode)
  2262 {
  2263     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  2264 
  2265     if (!texture) {
  2266         return -1;
  2267     }
  2268     if (scaleMode) {
  2269         *scaleMode = texture->scaleMode;
  2270     }
  2271     return 0;
  2272 }
  2273 
  2274 int
  2275 SDL_UpdateTexture(SDL_TextureID textureID, const SDL_Rect * rect,
  2276                   const void *pixels, int pitch)
  2277 {
  2278     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  2279     SDL_Renderer *renderer;
  2280     SDL_Rect full_rect;
  2281 
  2282     if (!texture) {
  2283         return -1;
  2284     }
  2285     renderer = texture->renderer;
  2286     if (!renderer->UpdateTexture) {
  2287         SDL_Unsupported();
  2288         return -1;
  2289     }
  2290     if (!rect) {
  2291         full_rect.x = 0;
  2292         full_rect.y = 0;
  2293         full_rect.w = texture->w;
  2294         full_rect.h = texture->h;
  2295         rect = &full_rect;
  2296     }
  2297     return renderer->UpdateTexture(renderer, texture, rect, pixels, pitch);
  2298 }
  2299 
  2300 int
  2301 SDL_LockTexture(SDL_TextureID textureID, const SDL_Rect * rect, int markDirty,
  2302                 void **pixels, int *pitch)
  2303 {
  2304     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  2305     SDL_Renderer *renderer;
  2306     SDL_Rect full_rect;
  2307 
  2308     if (!texture) {
  2309         return -1;
  2310     }
  2311     if (texture->access != SDL_TEXTUREACCESS_STREAMING) {
  2312         SDL_SetError("SDL_LockTexture(): texture must be streaming");
  2313         return -1;
  2314     }
  2315     renderer = texture->renderer;
  2316     if (!renderer->LockTexture) {
  2317         SDL_Unsupported();
  2318         return -1;
  2319     }
  2320     if (!rect) {
  2321         full_rect.x = 0;
  2322         full_rect.y = 0;
  2323         full_rect.w = texture->w;
  2324         full_rect.h = texture->h;
  2325         rect = &full_rect;
  2326     }
  2327     return renderer->LockTexture(renderer, texture, rect, markDirty, pixels,
  2328                                  pitch);
  2329 }
  2330 
  2331 void
  2332 SDL_UnlockTexture(SDL_TextureID textureID)
  2333 {
  2334     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  2335     SDL_Renderer *renderer;
  2336 
  2337     if (!texture) {
  2338         return;
  2339     }
  2340     if (texture->access != SDL_TEXTUREACCESS_STREAMING) {
  2341         return;
  2342     }
  2343     renderer = texture->renderer;
  2344     if (!renderer->UnlockTexture) {
  2345         return;
  2346     }
  2347     renderer->UnlockTexture(renderer, texture);
  2348 }
  2349 
  2350 void
  2351 SDL_DirtyTexture(SDL_TextureID textureID, int numrects,
  2352                  const SDL_Rect * rects)
  2353 {
  2354     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  2355     SDL_Renderer *renderer;
  2356 
  2357     if (!texture) {
  2358         return;
  2359     }
  2360     if (texture->access != SDL_TEXTUREACCESS_STREAMING) {
  2361         return;
  2362     }
  2363     renderer = texture->renderer;
  2364     if (!renderer->DirtyTexture) {
  2365         return;
  2366     }
  2367     renderer->DirtyTexture(renderer, texture, numrects, rects);
  2368 }
  2369 
  2370 int
  2371 SDL_SetRenderDrawColor(Uint8 r, Uint8 g, Uint8 b, Uint8 a)
  2372 {
  2373     SDL_Renderer *renderer;
  2374 
  2375     renderer = SDL_GetCurrentRenderer(SDL_TRUE);
  2376     if (!renderer) {
  2377         return -1;
  2378     }
  2379     renderer->r = r;
  2380     renderer->g = g;
  2381     renderer->b = b;
  2382     renderer->a = a;
  2383     if (renderer->SetDrawColor) {
  2384         return renderer->SetDrawColor(renderer);
  2385     } else {
  2386         return 0;
  2387     }
  2388 }
  2389 
  2390 int
  2391 SDL_GetRenderDrawColor(Uint8 * r, Uint8 * g, Uint8 * b, Uint8 * a)
  2392 {
  2393     SDL_Renderer *renderer;
  2394 
  2395     renderer = SDL_GetCurrentRenderer(SDL_TRUE);
  2396     if (!renderer) {
  2397         return -1;
  2398     }
  2399     if (r) {
  2400         *r = renderer->r;
  2401     }
  2402     if (g) {
  2403         *g = renderer->g;
  2404     }
  2405     if (b) {
  2406         *b = renderer->b;
  2407     }
  2408     if (a) {
  2409         *a = renderer->a;
  2410     }
  2411     return 0;
  2412 }
  2413 
  2414 int
  2415 SDL_SetRenderDrawBlendMode(int blendMode)
  2416 {
  2417     SDL_Renderer *renderer;
  2418 
  2419     renderer = SDL_GetCurrentRenderer(SDL_TRUE);
  2420     if (!renderer) {
  2421         return -1;
  2422     }
  2423     renderer->blendMode = blendMode;
  2424     if (renderer->SetDrawBlendMode) {
  2425         return renderer->SetDrawBlendMode(renderer);
  2426     } else {
  2427         return 0;
  2428     }
  2429 }
  2430 
  2431 int
  2432 SDL_GetRenderDrawBlendMode(int *blendMode)
  2433 {
  2434     SDL_Renderer *renderer;
  2435 
  2436     renderer = SDL_GetCurrentRenderer(SDL_TRUE);
  2437     if (!renderer) {
  2438         return -1;
  2439     }
  2440     *blendMode = renderer->blendMode;
  2441     return 0;
  2442 }
  2443 
  2444 int
  2445 SDL_RenderPoint(int x, int y)
  2446 {
  2447     SDL_Renderer *renderer;
  2448     SDL_Window *window;
  2449 
  2450     renderer = SDL_GetCurrentRenderer(SDL_TRUE);
  2451     if (!renderer) {
  2452         return -1;
  2453     }
  2454     if (!renderer->RenderPoint) {
  2455         SDL_Unsupported();
  2456         return -1;
  2457     }
  2458     window = SDL_GetWindowFromID(renderer->window);
  2459     if (x < 0 || y < 0 || x >= window->w || y >= window->h) {
  2460         return 0;
  2461     }
  2462     return renderer->RenderPoint(renderer, x, y);
  2463 }
  2464 
  2465 int
  2466 SDL_RenderLine(int x1, int y1, int x2, int y2)
  2467 {
  2468     SDL_Renderer *renderer;
  2469     SDL_Window *window;
  2470     SDL_Rect real_rect;
  2471 
  2472     if (x1 == x2 && y1 == y2) {
  2473         return SDL_RenderPoint(x1, y1);
  2474     }
  2475 
  2476     renderer = SDL_GetCurrentRenderer(SDL_TRUE);
  2477     if (!renderer) {
  2478         return -1;
  2479     }
  2480     if (!renderer->RenderLine) {
  2481         SDL_Unsupported();
  2482         return -1;
  2483     }
  2484     window = SDL_GetWindowFromID(renderer->window);
  2485 
  2486     real_rect.x = 0;
  2487     real_rect.y = 0;
  2488     real_rect.w = window->w;
  2489     real_rect.h = window->h;
  2490     if (!SDL_IntersectRectAndLine(&real_rect, &x1, &y1, &x2, &y2)) {
  2491         return (0);
  2492     }
  2493     return renderer->RenderLine(renderer, x1, y1, x2, y2);
  2494 }
  2495 
  2496 int
  2497 SDL_RenderFill(const SDL_Rect * rect)
  2498 {
  2499     SDL_Renderer *renderer;
  2500     SDL_Window *window;
  2501     SDL_Rect real_rect;
  2502 
  2503     renderer = SDL_GetCurrentRenderer(SDL_TRUE);
  2504     if (!renderer) {
  2505         return -1;
  2506     }
  2507     if (!renderer->RenderFill) {
  2508         SDL_Unsupported();
  2509         return -1;
  2510     }
  2511     window = SDL_GetWindowFromID(renderer->window);
  2512 
  2513     real_rect.x = 0;
  2514     real_rect.y = 0;
  2515     real_rect.w = window->w;
  2516     real_rect.h = window->h;
  2517     if (rect) {
  2518         if (!SDL_IntersectRect(rect, &real_rect, &real_rect)) {
  2519             return 0;
  2520         }
  2521     }
  2522     return renderer->RenderFill(renderer, &real_rect);
  2523 }
  2524 
  2525 int
  2526 SDL_RenderCopy(SDL_TextureID textureID, const SDL_Rect * srcrect,
  2527                const SDL_Rect * dstrect)
  2528 {
  2529     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  2530     SDL_Renderer *renderer;
  2531     SDL_Window *window;
  2532     SDL_Rect real_srcrect;
  2533     SDL_Rect real_dstrect;
  2534 
  2535     renderer = SDL_GetCurrentRenderer(SDL_TRUE);
  2536     if (!renderer) {
  2537         return -1;
  2538     }
  2539     if (!texture) {
  2540         SDL_SetError("Texture not found");
  2541         return -1;
  2542     }
  2543     if (texture->renderer != renderer) {
  2544         SDL_SetError("Texture was not created with this renderer");
  2545         return -1;
  2546     }
  2547     if (!renderer->RenderCopy) {
  2548         SDL_Unsupported();
  2549         return -1;
  2550     }
  2551     window = SDL_GetWindowFromID(renderer->window);
  2552 
  2553     real_srcrect.x = 0;
  2554     real_srcrect.y = 0;
  2555     real_srcrect.w = texture->w;
  2556     real_srcrect.h = texture->h;
  2557     if (srcrect) {
  2558         if (!SDL_IntersectRect(srcrect, &real_srcrect, &real_srcrect)) {
  2559             return 0;
  2560         }
  2561     }
  2562 
  2563     real_dstrect.x = 0;
  2564     real_dstrect.y = 0;
  2565     real_dstrect.w = window->w;
  2566     real_dstrect.h = window->h;
  2567     if (dstrect) {
  2568         if (!SDL_IntersectRect(dstrect, &real_dstrect, &real_dstrect)) {
  2569             return 0;
  2570         }
  2571         /* Clip srcrect by the same amount as dstrect was clipped */
  2572         if (dstrect->w != real_dstrect.w) {
  2573             int deltax = (real_dstrect.x - dstrect->x);
  2574             int deltaw = (real_dstrect.w - dstrect->w);
  2575             real_srcrect.x += (deltax * real_srcrect.w) / dstrect->w;
  2576             real_srcrect.w += (deltaw * real_srcrect.w) / dstrect->w;
  2577         }
  2578         if (dstrect->h != real_dstrect.h) {
  2579             int deltay = (real_dstrect.y - dstrect->y);
  2580             int deltah = (real_dstrect.h - dstrect->h);
  2581             real_srcrect.y += (deltay * real_srcrect.h) / dstrect->h;
  2582             real_srcrect.h += (deltah * real_srcrect.h) / dstrect->h;
  2583         }
  2584     }
  2585 
  2586     return renderer->RenderCopy(renderer, texture, &real_srcrect,
  2587                                 &real_dstrect);
  2588 }
  2589 
  2590 int
  2591 SDL_RenderReadPixels(const SDL_Rect * rect, Uint32 format,
  2592                      void * pixels, int pitch)
  2593 {
  2594     SDL_Renderer *renderer;
  2595     SDL_Window *window;
  2596     SDL_Rect real_rect;
  2597 
  2598     renderer = SDL_GetCurrentRenderer(SDL_TRUE);
  2599     if (!renderer) {
  2600         return -1;
  2601     }
  2602     if (!renderer->RenderReadPixels) {
  2603         SDL_Unsupported();
  2604         return -1;
  2605     }
  2606     window = SDL_GetWindowFromID(renderer->window);
  2607 
  2608     if (!format) {
  2609         format = SDL_GetDisplayFromWindow(window)->current_mode.format;
  2610     }
  2611 
  2612     real_rect.x = 0;
  2613     real_rect.y = 0;
  2614     real_rect.w = window->w;
  2615     real_rect.h = window->h;
  2616     if (rect) {
  2617         if (!SDL_IntersectRect(rect, &real_rect, &real_rect)) {
  2618             return 0;
  2619         }
  2620         if (real_rect.y > rect->y) {
  2621             pixels = (Uint8 *)pixels + pitch * (real_rect.y - rect->y);
  2622         }
  2623         if (real_rect.x > rect->x) {
  2624             Uint32 format = SDL_CurrentDisplay.current_mode.format;
  2625             int bpp = SDL_BYTESPERPIXEL(format);
  2626             pixels = (Uint8 *)pixels + bpp * (real_rect.x - rect->x);
  2627         }
  2628     }
  2629 
  2630     return renderer->RenderReadPixels(renderer, &real_rect,
  2631                                       format, pixels, pitch);
  2632 }
  2633 
  2634 int
  2635 SDL_RenderWritePixels(const SDL_Rect * rect, Uint32 format,
  2636                       const void * pixels, int pitch)
  2637 {
  2638     SDL_Renderer *renderer;
  2639     SDL_Window *window;
  2640     SDL_Rect real_rect;
  2641 
  2642     renderer = SDL_GetCurrentRenderer(SDL_TRUE);
  2643     if (!renderer) {
  2644         return -1;
  2645     }
  2646     if (!renderer->RenderWritePixels) {
  2647         SDL_Unsupported();
  2648         return -1;
  2649     }
  2650     window = SDL_GetWindowFromID(renderer->window);
  2651 
  2652     if (!format) {
  2653         format = SDL_GetDisplayFromWindow(window)->current_mode.format;
  2654     }
  2655 
  2656     real_rect.x = 0;
  2657     real_rect.y = 0;
  2658     real_rect.w = window->w;
  2659     real_rect.h = window->h;
  2660     if (rect) {
  2661         if (!SDL_IntersectRect(rect, &real_rect, &real_rect)) {
  2662             return 0;
  2663         }
  2664         if (real_rect.y > rect->y) {
  2665             pixels = (const Uint8 *)pixels + pitch * (real_rect.y - rect->y);
  2666         }
  2667         if (real_rect.x > rect->x) {
  2668             Uint32 format = SDL_CurrentDisplay.current_mode.format;
  2669             int bpp = SDL_BYTESPERPIXEL(format);
  2670             pixels = (const Uint8 *)pixels + bpp * (real_rect.x - rect->x);
  2671         }
  2672     }
  2673 
  2674     return renderer->RenderWritePixels(renderer, &real_rect,
  2675                                        format, pixels, pitch);
  2676 }
  2677 
  2678 void
  2679 SDL_RenderPresent(void)
  2680 {
  2681     SDL_Renderer *renderer;
  2682 
  2683     renderer = SDL_GetCurrentRenderer(SDL_TRUE);
  2684     if (!renderer || !renderer->RenderPresent) {
  2685         return;
  2686     }
  2687     renderer->RenderPresent(renderer);
  2688 }
  2689 
  2690 void
  2691 SDL_DestroyTexture(SDL_TextureID textureID)
  2692 {
  2693     int hash;
  2694     SDL_Texture *prev, *texture;
  2695     SDL_Renderer *renderer;
  2696 
  2697     if (!_this) {
  2698         SDL_UninitializedVideo();
  2699         return;
  2700     }
  2701     /* Look up the texture in the hash table */
  2702     hash = (textureID % SDL_arraysize(SDL_CurrentDisplay.textures));
  2703     prev = NULL;
  2704     for (texture = SDL_CurrentDisplay.textures[hash]; texture;
  2705          prev = texture, texture = texture->next) {
  2706         if (texture->id == textureID) {
  2707             break;
  2708         }
  2709     }
  2710     if (!texture) {
  2711         return;
  2712     }
  2713     /* Unlink the texture from the list */
  2714     if (prev) {
  2715         prev->next = texture->next;
  2716     } else {
  2717         SDL_CurrentDisplay.textures[hash] = texture->next;
  2718     }
  2719 
  2720     /* Free the texture */
  2721     renderer = texture->renderer;
  2722     renderer->DestroyTexture(renderer, texture);
  2723     SDL_free(texture);
  2724 }
  2725 
  2726 void
  2727 SDL_DestroyRenderer(SDL_WindowID windowID)
  2728 {
  2729     SDL_Window *window = SDL_GetWindowFromID(windowID);
  2730     SDL_Renderer *renderer;
  2731     int i;
  2732 
  2733     if (!window) {
  2734         return;
  2735     }
  2736     renderer = window->renderer;
  2737     if (!renderer) {
  2738         return;
  2739     }
  2740     /* Free existing textures for this renderer */
  2741     for (i = 0; i < SDL_arraysize(SDL_CurrentDisplay.textures); ++i) {
  2742         SDL_Texture *texture;
  2743         SDL_Texture *prev = NULL;
  2744         SDL_Texture *next;
  2745         for (texture = SDL_CurrentDisplay.textures[i]; texture;
  2746              texture = next) {
  2747             next = texture->next;
  2748             if (texture->renderer == renderer) {
  2749                 if (prev) {
  2750                     prev->next = next;
  2751                 } else {
  2752                     SDL_CurrentDisplay.textures[i] = next;
  2753                 }
  2754                 renderer->DestroyTexture(renderer, texture);
  2755                 SDL_free(texture);
  2756             } else {
  2757                 prev = texture;
  2758             }
  2759         }
  2760     }
  2761 
  2762     /* Free the renderer instance */
  2763     renderer->DestroyRenderer(renderer);
  2764 
  2765     /* Clear references */
  2766     window->renderer = NULL;
  2767     if (SDL_CurrentDisplay.current_renderer == renderer) {
  2768         SDL_CurrentDisplay.current_renderer = NULL;
  2769     }
  2770 }
  2771 
  2772 SDL_bool
  2773 SDL_IsScreenSaverEnabled()
  2774 {
  2775     if (!_this) {
  2776         return SDL_TRUE;
  2777     }
  2778     return _this->suspend_screensaver ? SDL_FALSE : SDL_TRUE;
  2779 }
  2780 
  2781 void
  2782 SDL_EnableScreenSaver()
  2783 {
  2784     if (!_this) {
  2785         return;
  2786     }
  2787     if (!_this->suspend_screensaver) {
  2788         return;
  2789     }
  2790     _this->suspend_screensaver = SDL_FALSE;
  2791     if (_this->SuspendScreenSaver) {
  2792         _this->SuspendScreenSaver(_this);
  2793     }
  2794 }
  2795 
  2796 void
  2797 SDL_DisableScreenSaver()
  2798 {
  2799     if (!_this) {
  2800         return;
  2801     }
  2802     if (_this->suspend_screensaver) {
  2803         return;
  2804     }
  2805     _this->suspend_screensaver = SDL_TRUE;
  2806     if (_this->SuspendScreenSaver) {
  2807         _this->SuspendScreenSaver(_this);
  2808     }
  2809 }
  2810 
  2811 void
  2812 SDL_VideoQuit(void)
  2813 {
  2814     int i, j;
  2815 
  2816     if (!_this) {
  2817         return;
  2818     }
  2819     /* Halt event processing before doing anything else */
  2820     SDL_StopEventLoop();
  2821     SDL_EnableScreenSaver();
  2822 
  2823     /* Clean up the system video */
  2824     for (i = _this->num_displays; i--;) {
  2825         SDL_VideoDisplay *display = &_this->displays[i];
  2826         for (j = display->num_windows; j--;) {
  2827             SDL_DestroyWindow(display->windows[i].id);
  2828         }
  2829         if (display->windows) {
  2830             SDL_free(display->windows);
  2831             display->windows = NULL;
  2832         }
  2833         display->num_windows = 0;
  2834         if (display->render_drivers) {
  2835             SDL_free(display->render_drivers);
  2836             display->render_drivers = NULL;
  2837         }
  2838         display->num_render_drivers = 0;
  2839     }
  2840     _this->VideoQuit(_this);
  2841 
  2842     for (i = _this->num_displays; i--;) {
  2843         SDL_VideoDisplay *display = &_this->displays[i];
  2844         for (j = display->num_display_modes; j--;) {
  2845             if (display->display_modes[j].driverdata) {
  2846                 SDL_free(display->display_modes[j].driverdata);
  2847                 display->display_modes[j].driverdata = NULL;
  2848             }
  2849         }
  2850         if (display->display_modes) {
  2851             SDL_free(display->display_modes);
  2852             display->display_modes = NULL;
  2853         }
  2854         if (display->desktop_mode.driverdata) {
  2855             SDL_free(display->desktop_mode.driverdata);
  2856             display->desktop_mode.driverdata = NULL;
  2857         }
  2858         if (display->palette) {
  2859             SDL_FreePalette(display->palette);
  2860             display->palette = NULL;
  2861         }
  2862         if (display->gamma) {
  2863             SDL_free(display->gamma);
  2864             display->gamma = NULL;
  2865         }
  2866         if (display->driverdata) {
  2867             SDL_free(display->driverdata);
  2868             display->driverdata = NULL;
  2869         }
  2870     }
  2871     if (_this->displays) {
  2872         SDL_free(_this->displays);
  2873         _this->displays = NULL;
  2874     }
  2875     _this->free(_this);
  2876     _this = NULL;
  2877 }
  2878 
  2879 int
  2880 SDL_GL_LoadLibrary(const char *path)
  2881 {
  2882     int retval;
  2883 
  2884     if (!_this) {
  2885         SDL_UninitializedVideo();
  2886         return -1;
  2887     }
  2888     if (_this->gl_config.driver_loaded) {
  2889         if (path && SDL_strcmp(path, _this->gl_config.driver_path) != 0) {
  2890             SDL_SetError("OpenGL library already loaded");
  2891             return -1;
  2892         }
  2893         retval = 0;
  2894     } else {
  2895         if (!_this->GL_LoadLibrary) {
  2896             SDL_SetError("No dynamic GL support in video driver");
  2897             return -1;
  2898         }
  2899         retval = _this->GL_LoadLibrary(_this, path);
  2900     }
  2901     if (retval == 0) {
  2902         ++_this->gl_config.driver_loaded;
  2903     }
  2904     return (retval);
  2905 }
  2906 
  2907 void *
  2908 SDL_GL_GetProcAddress(const char *proc)
  2909 {
  2910     void *func;
  2911 
  2912     if (!_this) {
  2913         SDL_UninitializedVideo();
  2914         return NULL;
  2915     }
  2916     func = NULL;
  2917     if (_this->GL_GetProcAddress) {
  2918         if (_this->gl_config.driver_loaded) {
  2919             func = _this->GL_GetProcAddress(_this, proc);
  2920         } else {
  2921             SDL_SetError("No GL driver has been loaded");
  2922         }
  2923     } else {
  2924         SDL_SetError("No dynamic GL support in video driver");
  2925     }
  2926     return func;
  2927 }
  2928 
  2929 void
  2930 SDL_GL_UnloadLibrary(void)
  2931 {
  2932     if (!_this) {
  2933         SDL_UninitializedVideo();
  2934         return;
  2935     }
  2936     if (_this->gl_config.driver_loaded > 0) {
  2937         if (--_this->gl_config.driver_loaded > 0) {
  2938             return;
  2939         }
  2940         if (_this->GL_UnloadLibrary) {
  2941             _this->GL_UnloadLibrary(_this);
  2942         }
  2943     }
  2944 }
  2945 
  2946 SDL_bool
  2947 SDL_GL_ExtensionSupported(const char *extension)
  2948 {
  2949 #if SDL_VIDEO_OPENGL || SDL_VIDEO_OPENGL_ES
  2950     const GLubyte *(APIENTRY * glGetStringFunc) (GLenum);
  2951     const char *extensions;
  2952     const char *start;
  2953     const char *where, *terminator;
  2954 
  2955     /* Extension names should not have spaces. */
  2956     where = SDL_strchr(extension, ' ');
  2957     if (where || *extension == '\0') {
  2958         return SDL_FALSE;
  2959     }
  2960     /* See if there's an environment variable override */
  2961     start = SDL_getenv(extension);
  2962     if (start && *start == '0') {
  2963         return SDL_FALSE;
  2964     }
  2965     /* Lookup the available extensions */
  2966     glGetStringFunc = SDL_GL_GetProcAddress("glGetString");
  2967     if (glGetStringFunc) {
  2968         extensions = (const char *) glGetStringFunc(GL_EXTENSIONS);
  2969     } else {
  2970         extensions = NULL;
  2971     }
  2972     if (!extensions) {
  2973         return SDL_FALSE;
  2974     }
  2975     /*
  2976      * It takes a bit of care to be fool-proof about parsing the OpenGL
  2977      * extensions string. Don't be fooled by sub-strings, etc.
  2978      */
  2979 
  2980     start = extensions;
  2981 
  2982     for (;;) {
  2983         where = SDL_strstr(start, extension);
  2984         if (!where)
  2985             break;
  2986 
  2987         terminator = where + SDL_strlen(extension);
  2988         if (where == start || *(where - 1) == ' ')
  2989             if (*terminator == ' ' || *terminator == '\0')
  2990                 return SDL_TRUE;
  2991 
  2992         start = terminator;
  2993     }
  2994     return SDL_FALSE;
  2995 #else
  2996     return SDL_FALSE;
  2997 #endif
  2998 }
  2999 
  3000 int
  3001 SDL_GL_SetAttribute(SDL_GLattr attr, int value)
  3002 {
  3003 #if SDL_VIDEO_OPENGL || SDL_VIDEO_OPENGL_ES
  3004     int retval;
  3005 
  3006     if (!_this) {
  3007         SDL_UninitializedVideo();
  3008         return -1;
  3009     }
  3010     retval = 0;
  3011     switch (attr) {
  3012     case SDL_GL_RED_SIZE:
  3013         _this->gl_config.red_size = value;
  3014         break;
  3015     case SDL_GL_GREEN_SIZE:
  3016         _this->gl_config.green_size = value;
  3017         break;
  3018     case SDL_GL_BLUE_SIZE:
  3019         _this->gl_config.blue_size = value;
  3020         break;
  3021     case SDL_GL_ALPHA_SIZE:
  3022         _this->gl_config.alpha_size = value;
  3023         break;
  3024     case SDL_GL_DOUBLEBUFFER:
  3025         _this->gl_config.double_buffer = value;
  3026         break;
  3027     case SDL_GL_BUFFER_SIZE:
  3028         _this->gl_config.buffer_size = value;
  3029         break;
  3030     case SDL_GL_DEPTH_SIZE:
  3031         _this->gl_config.depth_size = value;
  3032         break;
  3033     case SDL_GL_STENCIL_SIZE:
  3034         _this->gl_config.stencil_size = value;
  3035         break;
  3036     case SDL_GL_ACCUM_RED_SIZE:
  3037         _this->gl_config.accum_red_size = value;
  3038         break;
  3039     case SDL_GL_ACCUM_GREEN_SIZE:
  3040         _this->gl_config.accum_green_size = value;
  3041         break;
  3042     case SDL_GL_ACCUM_BLUE_SIZE:
  3043         _this->gl_config.accum_blue_size = value;
  3044         break;
  3045     case SDL_GL_ACCUM_ALPHA_SIZE:
  3046         _this->gl_config.accum_alpha_size = value;
  3047         break;
  3048     case SDL_GL_STEREO:
  3049         _this->gl_config.stereo = value;
  3050         break;
  3051     case SDL_GL_MULTISAMPLEBUFFERS:
  3052         _this->gl_config.multisamplebuffers = value;
  3053         break;
  3054     case SDL_GL_MULTISAMPLESAMPLES:
  3055         _this->gl_config.multisamplesamples = value;
  3056         break;
  3057     case SDL_GL_ACCELERATED_VISUAL:
  3058         _this->gl_config.accelerated = value;
  3059         break;
  3060     case SDL_GL_RETAINED_BACKING:
  3061         _this->gl_config.retained_backing = value;
  3062         break;
  3063     case SDL_GL_CONTEXT_MAJOR_VERSION:
  3064         _this->gl_config.major_version = value;
  3065         break;
  3066     case SDL_GL_CONTEXT_MINOR_VERSION:
  3067         _this->gl_config.minor_version = value;
  3068         break;
  3069     default:
  3070         SDL_SetError("Unknown OpenGL attribute");
  3071         retval = -1;
  3072         break;
  3073     }
  3074     return retval;
  3075 #else
  3076     SDL_Unsupported();
  3077     return -1;
  3078 #endif /* SDL_VIDEO_OPENGL */
  3079 }
  3080 
  3081 int
  3082 SDL_GL_GetAttribute(SDL_GLattr attr, int *value)
  3083 {
  3084 #if SDL_VIDEO_OPENGL || SDL_VIDEO_OPENGL_ES
  3085     void (APIENTRY * glGetIntegervFunc) (GLenum pname, GLint * params);
  3086     GLenum(APIENTRY * glGetErrorFunc) (void);
  3087     GLenum attrib = 0;
  3088     GLenum error = 0;
  3089 
  3090     glGetIntegervFunc = SDL_GL_GetProcAddress("glGetIntegerv");
  3091     if (!glGetIntegervFunc) {
  3092         return -1;
  3093     }
  3094 
  3095     glGetErrorFunc = SDL_GL_GetProcAddress("glGetError");
  3096     if (!glGetErrorFunc) {
  3097         return -1;
  3098     }
  3099 
  3100     /* Clear value in any case */
  3101     *value = 0;
  3102 
  3103     switch (attr) {
  3104     case SDL_GL_RETAINED_BACKING:
  3105         *value = _this->gl_config.retained_backing;
  3106         return 0;
  3107     case SDL_GL_RED_SIZE:
  3108         attrib = GL_RED_BITS;
  3109         break;
  3110     case SDL_GL_BLUE_SIZE:
  3111         attrib = GL_BLUE_BITS;
  3112         break;
  3113     case SDL_GL_GREEN_SIZE:
  3114         attrib = GL_GREEN_BITS;
  3115         break;
  3116     case SDL_GL_ALPHA_SIZE:
  3117         attrib = GL_ALPHA_BITS;
  3118         break;
  3119     case SDL_GL_DOUBLEBUFFER:
  3120 #ifndef SDL_VIDEO_OPENGL_ES
  3121         attrib = GL_DOUBLEBUFFER;
  3122         break;
  3123 #else
  3124         /* OpenGL ES 1.0 and above specifications have EGL_SINGLE_BUFFER      */
  3125         /* parameter which switches double buffer to single buffer. OpenGL ES */
  3126         /* SDL driver must set proper value after initialization              */
  3127         *value = _this->gl_config.double_buffer;
  3128         return 0;
  3129 #endif
  3130     case SDL_GL_DEPTH_SIZE:
  3131         attrib = GL_DEPTH_BITS;
  3132         break;
  3133     case SDL_GL_STENCIL_SIZE:
  3134         attrib = GL_STENCIL_BITS;
  3135         break;
  3136 #ifndef SDL_VIDEO_OPENGL_ES
  3137     case SDL_GL_ACCUM_RED_SIZE:
  3138         attrib = GL_ACCUM_RED_BITS;
  3139         break;
  3140     case SDL_GL_ACCUM_GREEN_SIZE:
  3141         attrib = GL_ACCUM_GREEN_BITS;
  3142         break;
  3143     case SDL_GL_ACCUM_BLUE_SIZE:
  3144         attrib = GL_ACCUM_BLUE_BITS;
  3145         break;
  3146     case SDL_GL_ACCUM_ALPHA_SIZE:
  3147         attrib = GL_ACCUM_ALPHA_BITS;
  3148         break;
  3149     case SDL_GL_STEREO:
  3150         attrib = GL_STEREO;
  3151         break;
  3152 #else
  3153     case SDL_GL_ACCUM_RED_SIZE:
  3154     case SDL_GL_ACCUM_GREEN_SIZE:
  3155     case SDL_GL_ACCUM_BLUE_SIZE:
  3156     case SDL_GL_ACCUM_ALPHA_SIZE:
  3157     case SDL_GL_STEREO:
  3158         /* none of these are supported in OpenGL ES */
  3159         *value = 0;
  3160         return 0;
  3161 #endif
  3162     case SDL_GL_MULTISAMPLEBUFFERS:
  3163 #ifndef SDL_VIDEO_OPENGL_ES
  3164         attrib = GL_SAMPLE_BUFFERS_ARB;
  3165 #else
  3166         attrib = GL_SAMPLE_BUFFERS;
  3167 #endif
  3168         break;
  3169     case SDL_GL_MULTISAMPLESAMPLES:
  3170 #ifndef SDL_VIDEO_OPENGL_ES
  3171         attrib = GL_SAMPLES_ARB;
  3172 #else
  3173         attrib = GL_SAMPLES;
  3174 #endif
  3175         break;
  3176     case SDL_GL_BUFFER_SIZE:
  3177         {
  3178             GLint bits = 0;
  3179             GLint component;
  3180 
  3181             /*
  3182              * there doesn't seem to be a single flag in OpenGL
  3183              * for this!
  3184              */
  3185             glGetIntegervFunc(GL_RED_BITS, &component);
  3186             bits += component;
  3187             glGetIntegervFunc(GL_GREEN_BITS, &component);
  3188             bits += component;
  3189             glGetIntegervFunc(GL_BLUE_BITS, &component);
  3190             bits += component;
  3191             glGetIntegervFunc(GL_ALPHA_BITS, &component);
  3192             bits += component;
  3193 
  3194             *value = bits;
  3195             return 0;
  3196         }
  3197     case SDL_GL_ACCELERATED_VISUAL:
  3198         {
  3199             /* FIXME: How do we get this information? */
  3200             *value = (_this->gl_config.accelerated != 0);
  3201             return 0;
  3202         }
  3203     default:
  3204         SDL_SetError("Unknown OpenGL attribute");
  3205         return -1;
  3206     }
  3207 
  3208     glGetIntegervFunc(attrib, (GLint *) value);
  3209     error = glGetErrorFunc();
  3210     if (error != GL_NO_ERROR) {
  3211         switch (error) {
  3212         case GL_INVALID_ENUM:
  3213             {
  3214                 SDL_SetError("OpenGL error: GL_INVALID_ENUM");
  3215             }
  3216             break;
  3217         case GL_INVALID_VALUE:
  3218             {
  3219                 SDL_SetError("OpenGL error: GL_INVALID_VALUE");
  3220             }
  3221             break;
  3222         default:
  3223             {
  3224                 SDL_SetError("OpenGL error: %08X", error);
  3225             }
  3226             break;
  3227         }
  3228         return -1;
  3229     }
  3230     return 0;
  3231 #else
  3232     SDL_Unsupported();
  3233     return -1;
  3234 #endif /* SDL_VIDEO_OPENGL */
  3235 }
  3236 
  3237 SDL_GLContext
  3238 SDL_GL_CreateContext(SDL_WindowID windowID)
  3239 {
  3240     SDL_Window *window = SDL_GetWindowFromID(windowID);
  3241 
  3242     if (!window) {
  3243         return NULL;
  3244     }
  3245     if (!(window->flags & SDL_WINDOW_OPENGL)) {
  3246         SDL_SetError("The specified window isn't an OpenGL window");
  3247         return NULL;
  3248     }
  3249     return _this->GL_CreateContext(_this, window);
  3250 }
  3251 
  3252 int
  3253 SDL_GL_MakeCurrent(SDL_WindowID windowID, SDL_GLContext context)
  3254 {
  3255     SDL_Window *window = SDL_GetWindowFromID(windowID);
  3256 
  3257     if (window && !(window->flags & SDL_WINDOW_OPENGL)) {
  3258         SDL_SetError("The specified window isn't an OpenGL window");
  3259         return -1;
  3260     }
  3261     if (!context) {
  3262         window = NULL;
  3263     }
  3264     return _this->GL_MakeCurrent(_this, window, context);
  3265 }
  3266 
  3267 int
  3268 SDL_GL_SetSwapInterval(int interval)
  3269 {
  3270     if (!_this) {
  3271         SDL_UninitializedVideo();
  3272         return -1;
  3273     }
  3274     if (_this->GL_SetSwapInterval) {
  3275         return _this->GL_SetSwapInterval(_this, interval);
  3276     } else {
  3277         SDL_SetError("Setting the swap interval is not supported");
  3278         return -1;
  3279     }
  3280 }
  3281 
  3282 int
  3283 SDL_GL_GetSwapInterval(void)
  3284 {
  3285     if (!_this) {
  3286         SDL_UninitializedVideo();
  3287         return -1;
  3288     }
  3289     if (_this->GL_GetSwapInterval) {
  3290         return _this->GL_GetSwapInterval(_this);
  3291     } else {
  3292         SDL_SetError("Getting the swap interval is not supported");
  3293         return -1;
  3294     }
  3295 }
  3296 
  3297 void
  3298 SDL_GL_SwapWindow(SDL_WindowID windowID)
  3299 {
  3300     SDL_Window *window = SDL_GetWindowFromID(windowID);
  3301 
  3302     if (!window) {
  3303         return;
  3304     }
  3305     if (!(window->flags & SDL_WINDOW_OPENGL)) {
  3306         SDL_SetError("The specified window isn't an OpenGL window");
  3307         return;
  3308     }
  3309     _this->GL_SwapWindow(_this, window);
  3310 }
  3311 
  3312 void
  3313 SDL_GL_DeleteContext(SDL_GLContext context)
  3314 {
  3315     if (!_this || !context) {
  3316         return;
  3317     }
  3318     _this->GL_MakeCurrent(_this, NULL, NULL);
  3319     _this->GL_DeleteContext(_this, context);
  3320 }
  3321 
  3322 #if 0                           // FIXME
  3323 /*
  3324  * Utility function used by SDL_WM_SetIcon(); flags & 1 for color key, flags
  3325  * & 2 for alpha channel.
  3326  */
  3327 static void
  3328 CreateMaskFromColorKeyOrAlpha(SDL_Surface * icon, Uint8 * mask, int flags)
  3329 {
  3330     int x, y;
  3331     Uint32 colorkey;
  3332 #define SET_MASKBIT(icon, x, y, mask) \
  3333 	mask[(y*((icon->w+7)/8))+(x/8)] &= ~(0x01<<(7-(x%8)))
  3334 
  3335     colorkey = icon->format->colorkey;
  3336     switch (icon->format->BytesPerPixel) {
  3337     case 1:
  3338         {
  3339             Uint8 *pixels;
  3340             for (y = 0; y < icon->h; ++y) {
  3341                 pixels = (Uint8 *) icon->pixels + y * icon->pitch;
  3342                 for (x = 0; x < icon->w; ++x) {
  3343                     if (*pixels++ == colorkey) {
  3344                         SET_MASKBIT(icon, x, y, mask);
  3345                     }
  3346                 }
  3347             }
  3348         }
  3349         break;
  3350 
  3351     case 2:
  3352         {
  3353             Uint16 *pixels;
  3354             for (y = 0; y < icon->h; ++y) {
  3355                 pixels = (Uint16 *) icon->pixels + y * icon->pitch / 2;
  3356                 for (x = 0; x < icon->w; ++x) {
  3357                     if ((flags & 1) && *pixels == colorkey) {
  3358                         SET_MASKBIT(icon, x, y, mask);
  3359                     } else if ((flags & 2)
  3360                                && (*pixels & icon->format->Amask) == 0) {
  3361                         SET_MASKBIT(icon, x, y, mask);
  3362                     }
  3363                     pixels++;
  3364                 }
  3365             }
  3366         }
  3367         break;
  3368 
  3369     case 4:
  3370         {
  3371             Uint32 *pixels;
  3372             for (y = 0; y < icon->h; ++y) {
  3373                 pixels = (Uint32 *) icon->pixels + y * icon->pitch / 4;
  3374                 for (x = 0; x < icon->w; ++x) {
  3375                     if ((flags & 1) && *pixels == colorkey) {
  3376                         SET_MASKBIT(icon, x, y, mask);
  3377                     } else if ((flags & 2)
  3378                                && (*pixels & icon->format->Amask) == 0) {
  3379                         SET_MASKBIT(icon, x, y, mask);
  3380                     }
  3381                     pixels++;
  3382                 }
  3383             }
  3384         }
  3385         break;
  3386     }
  3387 }
  3388 
  3389 /*
  3390  * Sets the window manager icon for the display window.
  3391  */
  3392 void
  3393 SDL_WM_SetIcon(SDL_Surface * icon, Uint8 * mask)
  3394 {
  3395     if (icon && _this->SetIcon) {
  3396         /* Generate a mask if necessary, and create the icon! */
  3397         if (mask == NULL) {
  3398             int mask_len = icon->h * (icon->w + 7) / 8;
  3399             int flags = 0;
  3400             mask = (Uint8 *) SDL_malloc(mask_len);
  3401             if (mask == NULL) {
  3402                 return;
  3403             }
  3404             SDL_memset(mask, ~0, mask_len);
  3405             if (icon->flags & SDL_SRCCOLORKEY)
  3406                 flags |= 1;
  3407             if (icon->flags & SDL_SRCALPHA)
  3408                 flags |= 2;
  3409             if (flags) {
  3410                 CreateMaskFromColorKeyOrAlpha(icon, mask, flags);
  3411             }
  3412             _this->SetIcon(_this, icon, mask);
  3413             SDL_free(mask);
  3414         } else {
  3415             _this->SetIcon(_this, icon, mask);
  3416         }
  3417     }
  3418 }
  3419 #endif
  3420 
  3421 SDL_bool
  3422 SDL_GetWindowWMInfo(SDL_WindowID windowID, struct SDL_SysWMinfo *info)
  3423 {
  3424     SDL_Window *window = SDL_GetWindowFromID(windowID);
  3425 
  3426     if (!window || !_this->GetWindowWMInfo) {
  3427         return SDL_FALSE;
  3428     }
  3429     return (_this->GetWindowWMInfo(_this, window, info));
  3430 }
  3431 
  3432 void
  3433 SDL_StartTextInput(void)
  3434 {
  3435     if (_this->StartTextInput) {
  3436         _this->StartTextInput(_this);
  3437     }
  3438     SDL_EventState(SDL_TEXTINPUT, SDL_ENABLE);
  3439     SDL_EventState(SDL_TEXTEDITING, SDL_ENABLE);
  3440 }
  3441 
  3442 void
  3443 SDL_StopTextInput(void)
  3444 {
  3445     if (_this->StopTextInput) {
  3446         _this->StopTextInput(_this);
  3447     }
  3448     SDL_EventState(SDL_TEXTINPUT, SDL_DISABLE);
  3449     SDL_EventState(SDL_TEXTEDITING, SDL_DISABLE);
  3450 }
  3451 
  3452 void
  3453 SDL_SetTextInputRect(SDL_Rect *rect)
  3454 {
  3455     if (_this->SetTextInputRect) {
  3456         _this->SetTextInputRect(_this, rect);
  3457     }
  3458 }
  3459 
  3460 /* vi: set ts=4 sw=4 expandtab: */