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