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