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