src/video/SDL_video.c
author Sam Lantinga <slouken@libsdl.org>
Tue, 19 Jan 2010 07:28:51 +0000
changeset 3683 4c5ab6841fdc
parent 3678 8e961ef35d4b
child 3685 64ce267332c6
permissions -rw-r--r--
From: Jjgod Jiang
Date: Mon, 18 Jan 2010 17:46:35 +0800
Subject: [PATCH] Polish text input handling for Mac OS X

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