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