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