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