src/video/SDL_video.c
author Sam Lantinga <slouken@libsdl.org>
Tue, 24 Nov 2009 04:59:50 +0000
changeset 3486 c87dbbde2bc2
parent 3485 e77a69aae239
child 3488 3d2f42580488
permissions -rw-r--r--
Fixed bug #891

Mason Wheeler 2009-11-23 06:59:48 PST

There's code in SDL_RecreateWindow specifically to handle SDL_WINDOW_FOREIGN,
but it appears to have been overlooked in the allowed_flags constant. This
causes the line

window->flags = (flags & allowed_flags);

to strip SDL_WINDOW_FOREIGN from the window's flags, which breaks some code in
WIN_WindowProc in SDL_win32Events.c that treats foreign windows differently.
This can be trivially fixed by defining allowed_flags as

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