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