src/video/SDL_video.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 29 May 2006 05:08:33 +0000
branchSDL-1.3
changeset 1669 9857d21967bb
parent 1668 4da1ee79c9af
child 1670 eef792d31de8
permissions -rw-r--r--
The test programs compile again.
The dummy video driver is partially functional now.
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997-2006 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_sysvideo.h"
    28 #include "SDL_blit.h"
    29 #include "SDL_pixels_c.h"
    30 #include "SDL_cursor_c.h"
    31 #include "../events/SDL_sysevents.h"
    32 #include "../events/SDL_events_c.h"
    33 
    34 /* Available video drivers */
    35 static VideoBootStrap *bootstrap[] = {
    36 #if SDL_VIDEO_DRIVER_QUARTZ
    37     &QZ_bootstrap,
    38 #endif
    39 #if SDL_VIDEO_DRIVER_X11
    40     &X11_bootstrap,
    41 #endif
    42 #if SDL_VIDEO_DRIVER_DGA
    43     &DGA_bootstrap,
    44 #endif
    45 #if SDL_VIDEO_DRIVER_NANOX
    46     &NX_bootstrap,
    47 #endif
    48 #if SDL_VIDEO_DRIVER_IPOD
    49     &iPod_bootstrap,
    50 #endif
    51 #if SDL_VIDEO_DRIVER_QTOPIA
    52     &Qtopia_bootstrap,
    53 #endif
    54 #if SDL_VIDEO_DRIVER_WSCONS
    55     &WSCONS_bootstrap,
    56 #endif
    57 #if SDL_VIDEO_DRIVER_FBCON
    58     &FBCON_bootstrap,
    59 #endif
    60 #if SDL_VIDEO_DRIVER_DIRECTFB
    61     &DirectFB_bootstrap,
    62 #endif
    63 #if SDL_VIDEO_DRIVER_PS2GS
    64     &PS2GS_bootstrap,
    65 #endif
    66 #if SDL_VIDEO_DRIVER_GGI
    67     &GGI_bootstrap,
    68 #endif
    69 #if SDL_VIDEO_DRIVER_VGL
    70     &VGL_bootstrap,
    71 #endif
    72 #if SDL_VIDEO_DRIVER_SVGALIB
    73     &SVGALIB_bootstrap,
    74 #endif
    75 #if SDL_VIDEO_DRIVER_GAPI
    76     &GAPI_bootstrap,
    77 #endif
    78 #if SDL_VIDEO_DRIVER_WINDIB
    79     &WINDIB_bootstrap,
    80 #endif
    81 #if SDL_VIDEO_DRIVER_DDRAW
    82     &DIRECTX_bootstrap,
    83 #endif
    84 #if SDL_VIDEO_DRIVER_BWINDOW
    85     &BWINDOW_bootstrap,
    86 #endif
    87 #if SDL_VIDEO_DRIVER_TOOLBOX
    88     &TOOLBOX_bootstrap,
    89 #endif
    90 #if SDL_VIDEO_DRIVER_DRAWSPROCKET
    91     &DSp_bootstrap,
    92 #endif
    93 #if SDL_VIDEO_DRIVER_CYBERGRAPHICS
    94     &CGX_bootstrap,
    95 #endif
    96 #if SDL_VIDEO_DRIVER_PHOTON
    97     &ph_bootstrap,
    98 #endif
    99 #if SDL_VIDEO_DRIVER_EPOC
   100     &EPOC_bootstrap,
   101 #endif
   102 #if SDL_VIDEO_DRIVER_XBIOS
   103     &XBIOS_bootstrap,
   104 #endif
   105 #if SDL_VIDEO_DRIVER_GEM
   106     &GEM_bootstrap,
   107 #endif
   108 #if SDL_VIDEO_DRIVER_PICOGUI
   109     &PG_bootstrap,
   110 #endif
   111 #if SDL_VIDEO_DRIVER_DC
   112     &DC_bootstrap,
   113 #endif
   114 #if SDL_VIDEO_DRIVER_RISCOS
   115     &RISCOS_bootstrap,
   116 #endif
   117 #if SDL_VIDEO_DRIVER_OS2FS
   118     &OS2FSLib_bootstrap,
   119 #endif
   120 #if SDL_VIDEO_DRIVER_AALIB
   121     &AALIB_bootstrap,
   122 #endif
   123 #if SDL_VIDEO_DRIVER_DUMMY
   124     &DUMMY_bootstrap,
   125 #endif
   126 #if SDL_VIDEO_DRIVER_GLSDL
   127     &glSDL_bootstrap,
   128 #endif
   129     NULL
   130 };
   131 
   132 static SDL_VideoDevice *_this = NULL;
   133 
   134 /* Various local functions */
   135 int SDL_VideoInit(const char *driver_name, Uint32 flags);
   136 void SDL_VideoQuit(void);
   137 
   138 static int
   139 cmpmodes(const void *A, const void *B)
   140 {
   141     SDL_DisplayMode a = *(const SDL_DisplayMode *) A;
   142     SDL_DisplayMode b = *(const SDL_DisplayMode *) B;
   143 
   144     if (a.w != b.w) {
   145         return b.w - a.w;
   146     }
   147     if (a.h != b.h) {
   148         return b.h - a.h;
   149     }
   150     if (SDL_BITSPERPIXEL(a.format) != SDL_BITSPERPIXEL(b.format)) {
   151         return SDL_BITSPERPIXEL(b.format) - SDL_BITSPERPIXEL(a.format);
   152     }
   153     if (a.refresh_rate != b.refresh_rate) {
   154         return b.refresh_rate - a.refresh_rate;
   155     }
   156     return 0;
   157 }
   158 
   159 int
   160 SDL_GetNumVideoDrivers(void)
   161 {
   162     return SDL_arraysize(bootstrap) - 1;
   163 }
   164 
   165 const char *
   166 SDL_GetVideoDriver(int index)
   167 {
   168     if (index >= 0 && index < SDL_GetNumVideoDrivers()) {
   169         return bootstrap[index]->name;
   170     }
   171     return NULL;
   172 }
   173 
   174 /*
   175  * Initialize the video and event subsystems -- determine native pixel format
   176  */
   177 int
   178 SDL_VideoInit(const char *driver_name, Uint32 flags)
   179 {
   180     SDL_VideoDevice *video;
   181     int index;
   182     int i;
   183     int bpp;
   184     Uint32 Rmask, Gmask, Bmask, Amask;
   185 
   186     /* Toggle the event thread flags, based on OS requirements */
   187 #if defined(MUST_THREAD_EVENTS)
   188     flags |= SDL_INIT_EVENTTHREAD;
   189 #elif defined(CANT_THREAD_EVENTS)
   190     if ((flags & SDL_INIT_EVENTTHREAD) == SDL_INIT_EVENTTHREAD) {
   191         SDL_SetError("OS doesn't support threaded events");
   192         return -1;
   193     }
   194 #endif
   195 
   196     /* Check to make sure we don't overwrite '_this' */
   197     if (_this != NULL) {
   198         SDL_VideoQuit();
   199     }
   200 
   201     /* Select the proper video driver */
   202     index = 0;
   203     video = NULL;
   204     if (driver_name != NULL) {
   205         for (i = 0; bootstrap[i]; ++i) {
   206             if (SDL_strncmp(bootstrap[i]->name, driver_name,
   207                             SDL_strlen(bootstrap[i]->name)) == 0) {
   208                 if (bootstrap[i]->available()) {
   209                     video = bootstrap[i]->create(index);
   210                 }
   211                 break;
   212             }
   213         }
   214     } else {
   215         for (i = 0; bootstrap[i]; ++i) {
   216             if (bootstrap[i]->available()) {
   217                 video = bootstrap[i]->create(index);
   218                 if (video != NULL) {
   219                     break;
   220                 }
   221             }
   222         }
   223     }
   224     if (video == NULL) {
   225         if (driver_name) {
   226             SDL_SetError("%s not available", driver_name);
   227         } else {
   228             SDL_SetError("No available video device");
   229         }
   230         return -1;
   231     }
   232     _this = video;
   233     _this->name = bootstrap[i]->name;
   234     _this->next_window_id = 1;
   235 
   236 
   237     /* Set some very sane GL defaults */
   238     _this->gl_config.driver_loaded = 0;
   239     _this->gl_config.dll_handle = NULL;
   240     _this->gl_config.red_size = 3;
   241     _this->gl_config.green_size = 3;
   242     _this->gl_config.blue_size = 2;
   243     _this->gl_config.alpha_size = 0;
   244     _this->gl_config.buffer_size = 0;
   245     _this->gl_config.depth_size = 16;
   246     _this->gl_config.stencil_size = 0;
   247     _this->gl_config.double_buffer = 1;
   248     _this->gl_config.accum_red_size = 0;
   249     _this->gl_config.accum_green_size = 0;
   250     _this->gl_config.accum_blue_size = 0;
   251     _this->gl_config.accum_alpha_size = 0;
   252     _this->gl_config.stereo = 0;
   253     _this->gl_config.multisamplebuffers = 0;
   254     _this->gl_config.multisamplesamples = 0;
   255     _this->gl_config.accelerated = -1;  /* not known, don't set */
   256     _this->gl_config.swap_control = -1; /* not known, don't set */
   257 
   258     /* Initialize the video subsystem */
   259     if (_this->VideoInit(_this) < 0) {
   260         SDL_VideoQuit();
   261         return -1;
   262     }
   263 
   264     /* Make sure some displays were added */
   265     if (_this->num_displays == 0) {
   266         SDL_SetError("The video driver did not add any displays");
   267         SDL_VideoQuit();
   268         return (-1);
   269     }
   270 
   271     /* Temporarily here for backwards compatibility */
   272     {
   273         int bpp;
   274         Uint32 Rmask, Gmask, Bmask, Amask;
   275 
   276         SDL_PixelFormatEnumToMasks(SDL_GetDesktopDisplayMode()->format, &bpp,
   277                                    &Rmask, &Gmask, &Bmask, &Amask);
   278         _this->info.vfmt = SDL_AllocFormat(bpp, Rmask, Gmask, Bmask, Amask);
   279     }
   280 
   281     /* Sort the video modes */
   282     for (i = 0; i < _this->num_displays; ++i) {
   283         SDL_qsort(_this->displays[i].display_modes,
   284                   _this->displays[i].num_display_modes,
   285                   sizeof(SDL_DisplayMode), cmpmodes);
   286     }
   287 
   288     /* Start the event loop */
   289     if (SDL_StartEventLoop(flags) < 0) {
   290         SDL_VideoQuit();
   291         return -1;
   292     }
   293     SDL_CursorInit(flags & SDL_INIT_EVENTTHREAD);
   294 
   295     /* We're ready to go! */
   296     return 0;
   297 }
   298 
   299 const char *
   300 SDL_GetCurrentVideoDriver()
   301 {
   302     if (!_this) {
   303         return NULL;
   304     }
   305     return _this->name;
   306 }
   307 
   308 SDL_VideoDevice *
   309 SDL_GetVideoDevice()
   310 {
   311     return _this;
   312 }
   313 
   314 const SDL_VideoInfo *
   315 SDL_GetVideoInfo(void)
   316 {
   317     if (!_this) {
   318         return NULL;
   319     }
   320     return &_this->info;
   321 }
   322 
   323 void
   324 SDL_AddBasicVideoDisplay(const SDL_DisplayMode * desktop_mode)
   325 {
   326     SDL_VideoDisplay display;
   327 
   328     SDL_zero(display);
   329     if (desktop_mode) {
   330         display.desktop_mode = *desktop_mode;
   331     }
   332     display.current_mode = display.desktop_mode;
   333     display.max_windows = 1;
   334 
   335     SDL_AddVideoDisplay(&display);
   336 }
   337 
   338 void
   339 SDL_AddVideoDisplay(const SDL_VideoDisplay * display)
   340 {
   341     SDL_VideoDisplay *displays;
   342 
   343     displays =
   344         SDL_realloc(_this->displays,
   345                     (_this->num_displays + 1) * sizeof(*displays));
   346     if (displays) {
   347         displays[_this->num_displays] = *display;
   348         _this->displays = displays;
   349         _this->num_displays++;
   350     }
   351 }
   352 
   353 int
   354 SDL_GetNumVideoDisplays(void)
   355 {
   356     if (!_this) {
   357         return 0;
   358     }
   359     return _this->num_displays;
   360 }
   361 
   362 int
   363 SDL_SelectVideoDisplay(int index)
   364 {
   365     if (!_this) {
   366         SDL_SetError("Video subsystem has not been initialized");
   367         return (-1);
   368     }
   369     if (index >= 0) {
   370         if (index >= _this->num_displays) {
   371             SDL_SetError("index must be in the range 0 - %d",
   372                          _this->num_displays - 1);
   373             return -1;
   374         }
   375         _this->current_display = index;
   376     }
   377     return _this->current_display;
   378 }
   379 
   380 void
   381 SDL_AddDisplayMode(int display, const SDL_DisplayMode * mode)
   382 {
   383     SDL_DisplayMode *modes;
   384     int i, nmodes;
   385 
   386     /* Make sure we don't already have the mode in the list */
   387     modes = SDL_CurrentDisplay.display_modes;
   388     nmodes = SDL_CurrentDisplay.num_display_modes;
   389     for (i = 0; i < nmodes; ++i) {
   390         if (SDL_memcmp(mode, &modes[i], sizeof(*mode)) == 0) {
   391             return;
   392         }
   393     }
   394 
   395     /* Go ahead and add the new mode */
   396     modes = SDL_realloc(modes, (nmodes + 1) * sizeof(*mode));
   397     if (modes) {
   398         SDL_CurrentDisplay.display_modes = modes;
   399         modes[nmodes] = *mode;
   400         SDL_CurrentDisplay.num_display_modes++;
   401     }
   402 }
   403 
   404 int
   405 SDL_GetNumDisplayModes()
   406 {
   407     if (_this) {
   408         return SDL_CurrentDisplay.num_display_modes;
   409     }
   410     return 0;
   411 }
   412 
   413 const SDL_DisplayMode *
   414 SDL_GetDisplayMode(int index)
   415 {
   416     if (index < 0 || index >= SDL_GetNumDisplayModes()) {
   417         SDL_SetError("index must be in the range of 0 - %d",
   418                      SDL_GetNumDisplayModes());
   419         return NULL;
   420     }
   421     return &SDL_CurrentDisplay.display_modes[index];
   422 }
   423 
   424 const SDL_DisplayMode *
   425 SDL_GetDesktopDisplayMode(void)
   426 {
   427     if (_this) {
   428         return &SDL_CurrentDisplay.desktop_mode;
   429     }
   430     return NULL;
   431 }
   432 
   433 const SDL_DisplayMode *
   434 SDL_GetCurrentDisplayMode(void)
   435 {
   436     if (_this) {
   437         return &SDL_CurrentDisplay.current_mode;
   438     }
   439     return NULL;
   440 }
   441 
   442 SDL_DisplayMode *
   443 SDL_GetClosestDisplayMode(const SDL_DisplayMode * mode,
   444                           SDL_DisplayMode * closest)
   445 {
   446     Uint32 target_format;
   447     int target_refresh_rate;
   448     int i;
   449     SDL_DisplayMode *current, *match;
   450 
   451     if (!_this || !mode || !closest) {
   452         return NULL;
   453     }
   454 
   455     /* Default to the desktop format */
   456     if (mode->format) {
   457         target_format = mode->format;
   458     } else {
   459         target_format = SDL_CurrentDisplay.desktop_mode.format;
   460     }
   461 
   462     /* Default to the desktop refresh rate */
   463     if (mode->refresh_rate) {
   464         target_refresh_rate = mode->refresh_rate;
   465     } else {
   466         target_refresh_rate = SDL_CurrentDisplay.desktop_mode.refresh_rate;
   467     }
   468 
   469     match = NULL;
   470     for (i = 0; i < SDL_CurrentDisplay.num_display_modes; ++i) {
   471         current = &SDL_CurrentDisplay.display_modes[i];
   472 
   473         if ((current->w && current->h) &&
   474             (current->w < mode->w || current->h < mode->h)) {
   475             /* Out of sorted modes large enough here */
   476             break;
   477         }
   478         if (!match || current->w < match->w || current->h < match->h) {
   479             match = current;
   480             continue;
   481         }
   482         if (current->format != match->format) {
   483             /* Sorted highest depth to lowest */
   484             if (current->format == target_format ||
   485                 (SDL_BITSPERPIXEL(current->format) >=
   486                  SDL_BITSPERPIXEL(target_format)
   487                  && SDL_PIXELTYPE(current->format) ==
   488                  SDL_PIXELTYPE(target_format))) {
   489                 match = current;
   490             }
   491             continue;
   492         }
   493         if (current->refresh_rate != match->refresh_rate) {
   494             /* Sorted highest refresh to lowest */
   495             if (current->refresh_rate >= target_refresh_rate) {
   496                 match = current;
   497             }
   498         }
   499     }
   500     if (match) {
   501         if (match->format) {
   502             closest->format = match->format;
   503         } else {
   504             closest->format = mode->format;
   505         }
   506         if (match->w && match->h) {
   507             closest->w = match->w;
   508             closest->h = match->h;
   509         } else {
   510             closest->w = mode->w;
   511             closest->h = mode->h;
   512         }
   513         if (match->refresh_rate) {
   514             closest->refresh_rate = match->refresh_rate;
   515         } else {
   516             closest->refresh_rate = mode->refresh_rate;
   517         }
   518         return closest;
   519     }
   520     return NULL;
   521 }
   522 
   523 int
   524 SDL_SetDisplayMode(const SDL_DisplayMode * mode)
   525 {
   526     SDL_VideoDisplay *display;
   527     SDL_DisplayMode display_mode;
   528     int i;
   529 
   530     if (!_this) {
   531         SDL_SetError("Video subsystem has not been initialized");
   532         return -1;
   533     }
   534 
   535     /* Make sure there's an actual display mode to set */
   536     if (!mode) {
   537         SDL_SetError("No mode passed to SDL_SetDisplayMode");
   538         return -1;
   539     }
   540     display = &SDL_CurrentDisplay;
   541     display_mode = *mode;
   542 
   543     /* Default to the current mode */
   544     if (!display_mode.format) {
   545         display_mode.format = display->current_mode.format;
   546     }
   547     if (!display_mode.w) {
   548         display_mode.w = display->current_mode.w;
   549     }
   550     if (!display_mode.h) {
   551         display_mode.h = display->current_mode.h;
   552     }
   553     if (!display_mode.refresh_rate) {
   554         display_mode.refresh_rate = display->current_mode.refresh_rate;
   555     }
   556 
   557     /* Get a good video mode, the closest one possible */
   558     if (!SDL_GetClosestDisplayMode(&display_mode, &display_mode)) {
   559         SDL_SetError("No video mode large enough for %dx%d",
   560                      display_mode.w, display_mode.h);
   561         return -1;
   562     }
   563 
   564     /* See if there's anything left to do */
   565     if (SDL_memcmp
   566         (&display_mode, SDL_GetCurrentDisplayMode(),
   567          sizeof(display_mode)) == 0) {
   568         return 0;
   569     }
   570 
   571     /* Free any previous window surfaces */
   572     for (i = 0; i < display->num_windows; ++i) {
   573         SDL_Window *window = &display->windows[i];
   574         if (window->shadow) {
   575             SDL_FreeSurface(window->shadow);
   576             window->shadow = NULL;
   577         }
   578         if (window->surface) {
   579             SDL_FreeSurface(window->surface);
   580             window->surface = NULL;
   581         }
   582     }
   583 
   584     return _this->SetDisplayMode(_this, &display_mode);
   585 }
   586 
   587 SDL_WindowID
   588 SDL_CreateWindow(const char *title, int x, int y, int w, int h, Uint32 flags)
   589 {
   590     const Uint32 allowed_flags = (SDL_WINDOW_FULLSCREEN |
   591                                   SDL_WINDOW_BORDERLESS |
   592                                   SDL_WINDOW_SHOWN |
   593                                   SDL_WINDOW_OPENGL |
   594                                   SDL_WINDOW_RESIZABLE |
   595                                   SDL_WINDOW_MAXIMIZED |
   596                                   SDL_WINDOW_MINIMIZED |
   597                                   SDL_WINDOW_INPUT_GRABBED);
   598     SDL_Window window;
   599     int num_windows;
   600     SDL_Window *windows;
   601 
   602     if (!_this) {
   603         SDL_SetError("Video subsystem has not been initialized");
   604         return 0;
   605     }
   606 
   607     SDL_zero(window);
   608     window.id = _this->next_window_id++;
   609     window.title = title ? SDL_strdup(title) : NULL;
   610     window.x = x;
   611     window.y = y;
   612     window.w = w;
   613     window.h = h;
   614     window.flags = (flags & allowed_flags);
   615 
   616     if (_this->CreateWindow && _this->CreateWindow(_this, &window) < 0) {
   617         if (window.title) {
   618             SDL_free(window.title);
   619         }
   620         return 0;
   621     }
   622 
   623     num_windows = SDL_CurrentDisplay.num_windows;
   624     windows =
   625         SDL_realloc(SDL_CurrentDisplay.windows,
   626                     (num_windows + 1) * sizeof(*windows));
   627     if (!windows) {
   628         if (_this->DestroyWindow) {
   629             _this->DestroyWindow(_this, &window);
   630         }
   631         if (window.title) {
   632             SDL_free(window.title);
   633         }
   634         return 0;
   635     }
   636     windows[num_windows] = window;
   637     SDL_CurrentDisplay.windows = windows;
   638     SDL_CurrentDisplay.num_windows++;
   639 
   640     return window.id;
   641 }
   642 
   643 SDL_WindowID
   644 SDL_CreateWindowFrom(void *data)
   645 {
   646     SDL_Window window;
   647     int num_windows;
   648     SDL_Window *windows;
   649 
   650     if (!_this) {
   651         SDL_SetError("Video subsystem has not been initialized");
   652         return (0);
   653     }
   654 
   655     SDL_zero(window);
   656     window.id = _this->next_window_id++;
   657 
   658     if (!_this->CreateWindowFrom ||
   659         _this->CreateWindowFrom(_this, &window, data) < 0) {
   660         return 0;
   661     }
   662 
   663     num_windows = SDL_CurrentDisplay.num_windows;
   664     windows =
   665         SDL_realloc(SDL_CurrentDisplay.windows,
   666                     (num_windows + 1) * sizeof(*windows));
   667     if (!windows) {
   668         if (_this->DestroyWindow) {
   669             _this->DestroyWindow(_this, &window);
   670         }
   671         if (window.title) {
   672             SDL_free(window.title);
   673         }
   674         return 0;
   675     }
   676     windows[num_windows] = window;
   677     SDL_CurrentDisplay.windows = windows;
   678     SDL_CurrentDisplay.num_windows++;
   679 
   680     return window.id;
   681 }
   682 
   683 static __inline__ SDL_Window *
   684 SDL_GetWindowFromID(SDL_WindowID windowID)
   685 {
   686     int i, j;
   687 
   688     if (!_this) {
   689         return NULL;
   690     }
   691 
   692     for (i = 0; i < _this->num_displays; ++i) {
   693         SDL_VideoDisplay *display = &_this->displays[i];
   694         for (j = 0; j < display->num_windows; ++j) {
   695             SDL_Window *window = &display->windows[j];
   696             if (window->id == windowID) {
   697                 return window;
   698             }
   699         }
   700     }
   701     return NULL;
   702 }
   703 
   704 SDL_Window *
   705 SDL_GetWindowFromSurface(SDL_Surface * surface)
   706 {
   707     int i, j;
   708 
   709     if (!_this || !surface) {
   710         return NULL;
   711     }
   712 
   713     for (i = 0; i < _this->num_displays; ++i) {
   714         SDL_VideoDisplay *display = &_this->displays[i];
   715         for (j = 0; j < display->num_windows; ++j) {
   716             SDL_Window *window = &display->windows[j];
   717             if (surface == window->surface || surface == window->shadow) {
   718                 return window;
   719             }
   720         }
   721     }
   722     return NULL;
   723 }
   724 
   725 
   726 Uint32
   727 SDL_GetWindowFlags(SDL_WindowID windowID)
   728 {
   729     SDL_Window *window = SDL_GetWindowFromID(windowID);
   730 
   731     if (!window) {
   732         return 0;
   733     }
   734     return window->flags;
   735 }
   736 
   737 void
   738 SDL_SetWindowTitle(SDL_WindowID windowID, const char *title)
   739 {
   740     SDL_Window *window = SDL_GetWindowFromID(windowID);
   741 
   742     if (!window) {
   743         return;
   744     }
   745     if (window->title) {
   746         SDL_free(window->title);
   747     }
   748     window->title = SDL_strdup(title);
   749 
   750     if (_this->SetWindowTitle) {
   751         _this->SetWindowTitle(_this, window);
   752     }
   753 }
   754 
   755 const char *
   756 SDL_GetWindowTitle(SDL_WindowID windowID)
   757 {
   758     SDL_Window *window = SDL_GetWindowFromID(windowID);
   759 
   760     if (!window) {
   761         return NULL;
   762     }
   763     return window->title;
   764 }
   765 
   766 void
   767 SDL_SetWindowData(SDL_WindowID windowID, void *userdata)
   768 {
   769     SDL_Window *window = SDL_GetWindowFromID(windowID);
   770 
   771     if (!window) {
   772         return;
   773     }
   774     window->userdata = userdata;
   775 }
   776 
   777 void *
   778 SDL_GetWindowData(SDL_WindowID windowID)
   779 {
   780     SDL_Window *window = SDL_GetWindowFromID(windowID);
   781 
   782     if (!window) {
   783         return NULL;
   784     }
   785     return window->userdata;
   786 }
   787 
   788 void
   789 SDL_SetWindowPosition(SDL_WindowID windowID, int x, int y)
   790 {
   791     SDL_Window *window = SDL_GetWindowFromID(windowID);
   792 
   793     if (!window) {
   794         return;
   795     }
   796 
   797     window->x = x;
   798     window->y = y;
   799 
   800     if (_this->SetWindowPosition) {
   801         _this->SetWindowPosition(_this, window);
   802     }
   803 }
   804 
   805 void
   806 SDL_GetWindowPosition(SDL_WindowID windowID, int *x, int *y)
   807 {
   808     SDL_Window *window = SDL_GetWindowFromID(windowID);
   809 
   810     if (!window) {
   811         return;
   812     }
   813     if (x) {
   814         *x = window->x;
   815     }
   816     if (y) {
   817         *y = window->y;
   818     }
   819 }
   820 
   821 void
   822 SDL_SetWindowSize(SDL_WindowID windowID, int w, int h)
   823 {
   824     SDL_Window *window = SDL_GetWindowFromID(windowID);
   825 
   826     if (!window) {
   827         return;
   828     }
   829 
   830     window->w = w;
   831     window->h = h;
   832 
   833     if (_this->SetWindowSize) {
   834         _this->SetWindowSize(_this, window);
   835     }
   836 }
   837 
   838 void
   839 SDL_GetWindowSize(SDL_WindowID windowID, int *w, int *h)
   840 {
   841     SDL_Window *window = SDL_GetWindowFromID(windowID);
   842 
   843     if (!window) {
   844         return;
   845     }
   846     if (w) {
   847         *w = window->w;
   848     }
   849     if (h) {
   850         *h = window->h;
   851     }
   852 }
   853 
   854 void
   855 SDL_ShowWindow(SDL_WindowID windowID)
   856 {
   857     SDL_Window *window = SDL_GetWindowFromID(windowID);
   858 
   859     if (!window || (window->flags & SDL_WINDOW_SHOWN)) {
   860         return;
   861     }
   862 
   863     window->flags |= SDL_WINDOW_SHOWN;
   864 
   865     if (_this->ShowWindow) {
   866         _this->ShowWindow(_this, window);
   867     }
   868 }
   869 
   870 void
   871 SDL_HideWindow(SDL_WindowID windowID)
   872 {
   873     SDL_Window *window = SDL_GetWindowFromID(windowID);
   874 
   875     if (!window || !(window->flags & SDL_WINDOW_SHOWN)) {
   876         return;
   877     }
   878 
   879     window->flags &= ~SDL_WINDOW_SHOWN;
   880 
   881     if (_this->HideWindow) {
   882         _this->HideWindow(_this, window);
   883     }
   884 }
   885 
   886 void
   887 SDL_RaiseWindow(SDL_WindowID windowID)
   888 {
   889     SDL_Window *window = SDL_GetWindowFromID(windowID);
   890 
   891     if (!window) {
   892         return;
   893     }
   894 
   895     if (_this->RaiseWindow) {
   896         _this->RaiseWindow(_this, window);
   897     }
   898 }
   899 
   900 void
   901 SDL_MaximizeWindow(SDL_WindowID windowID)
   902 {
   903     SDL_Window *window = SDL_GetWindowFromID(windowID);
   904 
   905     if (!window || (window->flags & SDL_WINDOW_MAXIMIZED)) {
   906         return;
   907     }
   908 
   909     window->flags |= SDL_WINDOW_MAXIMIZED;
   910 
   911     if (_this->MaximizeWindow) {
   912         _this->MaximizeWindow(_this, window);
   913     }
   914 }
   915 
   916 void
   917 SDL_MinimizeWindow(SDL_WindowID windowID)
   918 {
   919     SDL_Window *window = SDL_GetWindowFromID(windowID);
   920 
   921     if (!window || (window->flags & SDL_WINDOW_MINIMIZED)) {
   922         return;
   923     }
   924 
   925     window->flags |= SDL_WINDOW_MINIMIZED;
   926 
   927     if (_this->MinimizeWindow) {
   928         _this->MinimizeWindow(_this, window);
   929     }
   930 }
   931 
   932 void
   933 SDL_RestoreWindow(SDL_WindowID windowID)
   934 {
   935     SDL_Window *window = SDL_GetWindowFromID(windowID);
   936 
   937     if (!window
   938         || (window->flags & (SDL_WINDOW_MAXIMIZED | SDL_WINDOW_MINIMIZED))) {
   939         return;
   940     }
   941 
   942     window->flags &= ~(SDL_WINDOW_MAXIMIZED | SDL_WINDOW_MINIMIZED);
   943 
   944     if (_this->RestoreWindow) {
   945         _this->RestoreWindow(_this, window);
   946     }
   947 }
   948 
   949 void
   950 SDL_SetWindowGrab(SDL_WindowID windowID, int mode)
   951 {
   952     SDL_Window *window = SDL_GetWindowFromID(windowID);
   953 
   954     if (!window || (!!mode == !!(window->flags & SDL_WINDOW_INPUT_GRABBED))) {
   955         return;
   956     }
   957 
   958     if (mode) {
   959         window->flags |= SDL_WINDOW_INPUT_GRABBED;
   960     } else {
   961         window->flags &= ~SDL_WINDOW_INPUT_GRABBED;
   962     }
   963 
   964     if (_this->SetWindowGrab) {
   965         _this->SetWindowGrab(_this, window);
   966     }
   967 }
   968 
   969 int
   970 SDL_GetWindowGrab(SDL_WindowID windowID)
   971 {
   972     SDL_Window *window = SDL_GetWindowFromID(windowID);
   973 
   974     if (!window) {
   975         return 0;
   976     }
   977 
   978     return ((window->flags & SDL_WINDOW_INPUT_GRABBED) != 0);
   979 }
   980 
   981 void
   982 SDL_DestroyWindow(SDL_WindowID windowID)
   983 {
   984     int i, j;
   985 
   986     if (!_this) {
   987         return;
   988     }
   989 
   990     for (i = 0; i < _this->num_displays; ++i) {
   991         SDL_VideoDisplay *display = &_this->displays[i];
   992         for (j = 0; j < display->num_windows; ++j) {
   993             SDL_Window *window = &display->windows[j];
   994             if (window->id != windowID) {
   995                 continue;
   996             }
   997             if (window->flags & SDL_WINDOW_INPUT_GRABBED) {
   998                 window->flags &= ~SDL_WINDOW_INPUT_GRABBED;
   999                 _this->SetWindowGrab(_this, window);
  1000             }
  1001             if (window->shadow) {
  1002                 SDL_FreeSurface(window->shadow);
  1003             }
  1004             if (window->surface) {
  1005                 SDL_FreeSurface(window->surface);
  1006             }
  1007             if (_this->DestroyWindow) {
  1008                 _this->DestroyWindow(_this, window);
  1009             }
  1010             if (window->title) {
  1011                 SDL_free(window->title);
  1012             }
  1013             if (window->gamma) {
  1014                 SDL_free(window->gamma);
  1015             }
  1016             if (j != display->num_windows - 1) {
  1017                 SDL_memcpy(&display->windows[i],
  1018                            &display->windows[i + 1],
  1019                            (display->num_windows - i - 1) * sizeof(*window));
  1020             }
  1021             --display->num_windows;
  1022             return;
  1023         }
  1024     }
  1025 }
  1026 
  1027 SDL_Surface *
  1028 SDL_CreateWindowSurface(SDL_WindowID windowID, Uint32 format, Uint32 flags)
  1029 {
  1030     SDL_Window *window = SDL_GetWindowFromID(windowID);
  1031     Uint32 black;
  1032     SDL_Surface *surface;
  1033 
  1034     if (!window) {
  1035         return NULL;
  1036     }
  1037 
  1038     if (!_this->CreateWindowSurface) {
  1039         return NULL;
  1040     }
  1041 
  1042     if (!window->surface) {
  1043         _this->CreateWindowSurface(_this, window, flags);
  1044         if (!window->surface) {
  1045             return NULL;
  1046         }
  1047         window->surface->flags |= SDL_SCREEN_SURFACE;
  1048 
  1049         /* If we have a palettized surface, create a default palette */
  1050         if (window->surface->format->palette) {
  1051             SDL_Color colors[256];
  1052             SDL_PixelFormat *vf = window->surface->format;
  1053             SDL_DitherColors(colors, vf->BitsPerPixel);
  1054             SDL_SetColors(window->surface, colors, 0, vf->palette->ncolors);
  1055         }
  1056     }
  1057     surface = window->surface;
  1058 
  1059     if (window->shadow) {
  1060         SDL_FreeSurface(window->shadow);
  1061         window->shadow = NULL;
  1062     }
  1063 
  1064     /* Create a shadow surface if necessary */
  1065     if ((!(flags & SDL_ANYFORMAT)
  1066          && (format != SDL_GetCurrentDisplayMode()->format))
  1067         || ((flags & SDL_HWPALETTE)
  1068             && !(window->surface->flags & SDL_HWPALETTE))) {
  1069         int bpp;
  1070         Uint32 Rmask, Gmask, Bmask, Amask;
  1071 
  1072         SDL_PixelFormatEnumToMasks(format, &bpp, &Amask, &Gmask, &Bmask,
  1073                                    &Amask);
  1074         window->shadow =
  1075             SDL_CreateRGBSurface(SDL_SWSURFACE, surface->w, surface->h, bpp,
  1076                                  Rmask, Gmask, Bmask, Amask);
  1077         if (window->shadow == NULL) {
  1078             return NULL;
  1079         }
  1080         window->shadow->flags |= SDL_SHADOW_SURFACE;
  1081 
  1082         surface = window->shadow;
  1083 
  1084         /* 8-bit shadow surfaces report that they have exclusive palette */
  1085         if (surface->format->palette) {
  1086             surface->flags |= SDL_HWPALETTE;
  1087             if (format == SDL_GetCurrentDisplayMode()->format) {
  1088                 SDL_memcpy(surface->format->palette->colors,
  1089                            window->surface->format->palette->colors,
  1090                            window->surface->format->palette->ncolors *
  1091                            sizeof(SDL_Color));
  1092             } else {
  1093                 SDL_DitherColors(surface->format->palette->colors, bpp);
  1094             }
  1095         }
  1096     }
  1097 
  1098     /* Clear the surface for display */
  1099     {
  1100         Uint32 black = SDL_MapRGB(surface->format, 0, 0, 0);
  1101         SDL_FillRect(surface, NULL, black);
  1102         if (surface->flags & SDL_DOUBLEBUF) {
  1103             SDL_Flip(surface);
  1104             SDL_FillRect(surface, NULL, black);
  1105         }
  1106         SDL_Flip(surface);
  1107     }
  1108 
  1109     return surface;
  1110 }
  1111 
  1112 /* 
  1113  * Convert a surface into the video pixel format.
  1114  */
  1115 SDL_Surface *
  1116 SDL_DisplayFormat(SDL_Surface * surface)
  1117 {
  1118     Uint32 flags;
  1119 
  1120     if (!SDL_PublicSurface) {
  1121         SDL_SetError("No video mode has been set");
  1122         return (NULL);
  1123     }
  1124     /* Set the flags appropriate for copying to display surface */
  1125     if (((SDL_PublicSurface->flags & SDL_HWSURFACE) == SDL_HWSURFACE)
  1126         && _this->info.blit_hw)
  1127         flags = SDL_HWSURFACE;
  1128     else
  1129         flags = SDL_SWSURFACE;
  1130 #ifdef AUTORLE_DISPLAYFORMAT
  1131     flags |= (surface->flags & (SDL_SRCCOLORKEY | SDL_SRCALPHA));
  1132     flags |= SDL_RLEACCELOK;
  1133 #else
  1134     flags |=
  1135         surface->flags & (SDL_SRCCOLORKEY | SDL_SRCALPHA | SDL_RLEACCELOK);
  1136 #endif
  1137     return (SDL_ConvertSurface(surface, SDL_PublicSurface->format, flags));
  1138 }
  1139 
  1140 /*
  1141  * Convert a surface into a format that's suitable for blitting to
  1142  * the screen, but including an alpha channel.
  1143  */
  1144 SDL_Surface *
  1145 SDL_DisplayFormatAlpha(SDL_Surface * surface)
  1146 {
  1147     SDL_PixelFormat *vf;
  1148     SDL_PixelFormat *format;
  1149     SDL_Surface *converted;
  1150     Uint32 flags;
  1151     /* default to ARGB8888 */
  1152     Uint32 amask = 0xff000000;
  1153     Uint32 rmask = 0x00ff0000;
  1154     Uint32 gmask = 0x0000ff00;
  1155     Uint32 bmask = 0x000000ff;
  1156 
  1157     if (!SDL_PublicSurface) {
  1158         SDL_SetError("No video mode has been set");
  1159         return (NULL);
  1160     }
  1161     vf = SDL_PublicSurface->format;
  1162 
  1163     switch (vf->BytesPerPixel) {
  1164     case 2:
  1165         /* For XGY5[56]5, use, AXGY8888, where {X, Y} = {R, B}.
  1166            For anything else (like ARGB4444) it doesn't matter
  1167            since we have no special code for it anyway */
  1168         if ((vf->Rmask == 0x1f) &&
  1169             (vf->Bmask == 0xf800 || vf->Bmask == 0x7c00)) {
  1170             rmask = 0xff;
  1171             bmask = 0xff0000;
  1172         }
  1173         break;
  1174 
  1175     case 3:
  1176     case 4:
  1177         /* Keep the video format, as long as the high 8 bits are
  1178            unused or alpha */
  1179         if ((vf->Rmask == 0xff) && (vf->Bmask == 0xff0000)) {
  1180             rmask = 0xff;
  1181             bmask = 0xff0000;
  1182         }
  1183         break;
  1184 
  1185     default:
  1186         /* We have no other optimised formats right now. When/if a new
  1187            optimised alpha format is written, add the converter here */
  1188         break;
  1189     }
  1190     format = SDL_AllocFormat(32, rmask, gmask, bmask, amask);
  1191     flags = SDL_PublicSurface->flags & SDL_HWSURFACE;
  1192     flags |= surface->flags & (SDL_SRCALPHA | SDL_RLEACCELOK);
  1193     converted = SDL_ConvertSurface(surface, format, flags);
  1194     SDL_FreeFormat(format);
  1195     return (converted);
  1196 }
  1197 
  1198 /*
  1199  * Update a specific portion of the physical screen
  1200  */
  1201 void
  1202 SDL_UpdateRect(SDL_Surface * screen, Sint32 x, Sint32 y, Uint32 w, Uint32 h)
  1203 {
  1204     if (screen) {
  1205         SDL_Rect rect;
  1206 
  1207         /* Perform some checking */
  1208         if (w == 0)
  1209             w = screen->w;
  1210         if (h == 0)
  1211             h = screen->h;
  1212         if ((int) (x + w) > screen->w)
  1213             return;
  1214         if ((int) (y + h) > screen->h)
  1215             return;
  1216 
  1217         /* Fill the rectangle */
  1218         rect.x = (Sint16) x;
  1219         rect.y = (Sint16) y;
  1220         rect.w = (Uint16) w;
  1221         rect.h = (Uint16) h;
  1222         SDL_UpdateRects(screen, 1, &rect);
  1223     }
  1224 }
  1225 void
  1226 SDL_UpdateRects(SDL_Surface * screen, int numrects, SDL_Rect * rects)
  1227 {
  1228     int i;
  1229     SDL_Window *window;
  1230 
  1231     /* Find the window corresponding to this surface */
  1232     window = SDL_GetWindowFromSurface(screen);
  1233     if (!window) {
  1234         SDL_SetError("Couldn't find window associated with surface");
  1235         return;
  1236     }
  1237 
  1238     if (screen->flags & SDL_SHADOW_SURFACE) {
  1239         if (SHOULD_DRAWCURSOR(SDL_cursorstate)) {
  1240             SDL_LockCursor();
  1241             SDL_DrawCursor(screen);
  1242             for (i = 0; i < numrects; ++i) {
  1243                 SDL_LowerBlit(screen, &rects[i], window->surface, &rects[i]);
  1244             }
  1245             SDL_EraseCursor(screen);
  1246             SDL_UnlockCursor();
  1247         } else {
  1248             for (i = 0; i < numrects; ++i) {
  1249                 SDL_LowerBlit(screen, &rects[i], window->surface, &rects[i]);
  1250             }
  1251         }
  1252 
  1253         /* Fall through to video surface update */
  1254         screen = window->surface;
  1255     }
  1256     if ((screen->flags & SDL_SCREEN_SURFACE) && _this->UpdateWindowSurface) {
  1257         /* Update the video surface */
  1258         if (screen->offset) {
  1259             int offset_y = screen->offset / screen->pitch;
  1260             int offset_x = screen->offset % screen->pitch;
  1261             for (i = 0; i < numrects; ++i) {
  1262                 rects[i].x += offset_x;
  1263                 rects[i].y += offset_y;
  1264             }
  1265             _this->UpdateWindowSurface(_this, window, numrects, rects);
  1266             for (i = 0; i < numrects; ++i) {
  1267                 rects[i].x -= offset_x;
  1268                 rects[i].y -= offset_y;
  1269             }
  1270         } else {
  1271             _this->UpdateWindowSurface(_this, window, numrects, rects);
  1272         }
  1273     }
  1274 }
  1275 
  1276 /*
  1277  * Performs hardware double buffering, if possible, or a full update if not.
  1278  */
  1279 int
  1280 SDL_Flip(SDL_Surface * screen)
  1281 {
  1282     SDL_Window *window;
  1283 
  1284     /* Find the window corresponding to this surface */
  1285     window = SDL_GetWindowFromSurface(screen);
  1286     if (!window) {
  1287         SDL_SetError("Couldn't find window associated with surface");
  1288         return;
  1289     }
  1290 
  1291     /* Copy the shadow surface to the video surface */
  1292     if (screen->flags & SDL_SHADOW_SURFACE) {
  1293         SDL_Rect rect;
  1294 
  1295         rect.x = 0;
  1296         rect.y = 0;
  1297         rect.w = screen->w;
  1298         rect.h = screen->h;
  1299         if (SHOULD_DRAWCURSOR(SDL_cursorstate)) {
  1300             SDL_LockCursor();
  1301             SDL_DrawCursor(screen);
  1302             SDL_LowerBlit(screen, &rect, window->surface, &rect);
  1303             SDL_EraseCursor(screen);
  1304             SDL_UnlockCursor();
  1305         } else {
  1306             SDL_LowerBlit(screen, &rect, window->surface, &rect);
  1307         }
  1308 
  1309         /* Fall through to video surface update */
  1310         screen = window->surface;
  1311     }
  1312     if (screen->flags & SDL_DOUBLEBUF) {
  1313         _this->FlipWindowSurface(_this, window);
  1314     } else {
  1315         SDL_UpdateRect(screen, 0, 0, 0, 0);
  1316     }
  1317     return (0);
  1318 }
  1319 
  1320 int
  1321 SDL_SetColors(SDL_Surface * screen, SDL_Color * colors, int firstcolor,
  1322               int ncolors)
  1323 {
  1324     SDL_Window *window = NULL;
  1325     SDL_Palette *pal;
  1326     int gotall;
  1327     int palsize;
  1328 
  1329     /* Verify the parameters */
  1330     pal = screen->format->palette;
  1331     if (!pal) {
  1332         return 0;               /* not a palettized surface */
  1333     }
  1334     gotall = 1;
  1335     palsize = 1 << screen->format->BitsPerPixel;
  1336     if (ncolors > (palsize - firstcolor)) {
  1337         ncolors = (palsize - firstcolor);
  1338         gotall = 0;
  1339     }
  1340 
  1341     if (colors != (pal->colors + firstcolor)) {
  1342         SDL_memcpy(pal->colors + firstcolor, colors,
  1343                    ncolors * sizeof(*colors));
  1344     }
  1345     SDL_FormatChanged(screen);
  1346 
  1347     if (screen->flags & (SDL_SHADOW_SURFACE | SDL_SCREEN_SURFACE)) {
  1348         window = SDL_GetWindowFromSurface(screen);
  1349         if (!window) {
  1350             return 0;
  1351         }
  1352     }
  1353 
  1354     if (screen->flags & SDL_SHADOW_SURFACE) {
  1355         SDL_Palette *vidpal;
  1356 
  1357         vidpal = window->surface->format->palette;
  1358         if (vidpal && vidpal->ncolors == pal->ncolors) {
  1359             /* This is a shadow surface, and the physical
  1360              * framebuffer is also indexed. Propagate the
  1361              * changes to its logical palette so that
  1362              * updates are always identity blits
  1363              */
  1364             SDL_memcpy(vidpal->colors + firstcolor, colors,
  1365                        ncolors * sizeof(*colors));
  1366         }
  1367         if (window->surface->flags & SDL_HWPALETTE) {
  1368             /* Set the physical palette */
  1369             screen = window->surface;
  1370         } else {
  1371             SDL_UpdateRect(screen, 0, 0, 0, 0);
  1372         }
  1373     }
  1374 
  1375     if (screen->flags & SDL_SCREEN_SURFACE) {
  1376         if (_this->SetWindowColors) {
  1377             gotall =
  1378                 _this->SetWindowColors(_this, window, firstcolor, ncolors,
  1379                                        colors);
  1380             if (!gotall) {
  1381                 /* The video flags shouldn't have SDL_HWPALETTE, and
  1382                    the video driver is responsible for copying back the
  1383                    correct colors into the video surface palette.
  1384                  */
  1385                 ;
  1386             }
  1387         }
  1388         SDL_CursorPaletteChanged();
  1389     }
  1390 
  1391     return gotall;
  1392 }
  1393 
  1394 void
  1395 SDL_VideoQuit(void)
  1396 {
  1397     int i, j;
  1398 
  1399     if (!_this) {
  1400         return;
  1401     }
  1402 
  1403     /* Halt event processing before doing anything else */
  1404     SDL_StopEventLoop();
  1405 
  1406     /* Clean up allocated window manager items */
  1407     SDL_CursorQuit();
  1408 
  1409     /* Clean up the system video */
  1410     for (i = _this->num_displays; i--;) {
  1411         SDL_VideoDisplay *display = &_this->displays[i];
  1412         for (j = display->num_windows; j--;) {
  1413             SDL_DestroyWindow(display->windows[i].id);
  1414         }
  1415         if (display->windows) {
  1416             SDL_free(display->windows);
  1417             display->windows = NULL;
  1418         }
  1419     }
  1420     _this->VideoQuit(_this);
  1421     if (_this->displays) {
  1422         SDL_free(_this->displays);
  1423     }
  1424     SDL_free(_this->info.vfmt);
  1425     _this->free(_this);
  1426     _this = NULL;
  1427 }
  1428 
  1429 /* Load the GL driver library */
  1430 int
  1431 SDL_GL_LoadLibrary(const char *path)
  1432 {
  1433     int retval;
  1434 
  1435     retval = -1;
  1436     if (_this == NULL) {
  1437         SDL_SetError("Video subsystem has not been initialized");
  1438     } else {
  1439         if (_this->GL_LoadLibrary) {
  1440             retval = _this->GL_LoadLibrary(_this, path);
  1441         } else {
  1442             SDL_SetError("No dynamic GL support in video driver");
  1443         }
  1444     }
  1445     return (retval);
  1446 }
  1447 
  1448 void *
  1449 SDL_GL_GetProcAddress(const char *proc)
  1450 {
  1451     void *func;
  1452 
  1453     func = NULL;
  1454     if (_this->GL_GetProcAddress) {
  1455         if (_this->gl_config.driver_loaded) {
  1456             func = _this->GL_GetProcAddress(_this, proc);
  1457         } else {
  1458             SDL_SetError("No GL driver has been loaded");
  1459         }
  1460     } else {
  1461         SDL_SetError("No dynamic GL support in video driver");
  1462     }
  1463     return func;
  1464 }
  1465 
  1466 /* Set the specified GL attribute for setting up a GL video mode */
  1467 int
  1468 SDL_GL_SetAttribute(SDL_GLattr attr, int value)
  1469 {
  1470     int retval;
  1471 
  1472     retval = 0;
  1473     switch (attr) {
  1474     case SDL_GL_RED_SIZE:
  1475         _this->gl_config.red_size = value;
  1476         break;
  1477     case SDL_GL_GREEN_SIZE:
  1478         _this->gl_config.green_size = value;
  1479         break;
  1480     case SDL_GL_BLUE_SIZE:
  1481         _this->gl_config.blue_size = value;
  1482         break;
  1483     case SDL_GL_ALPHA_SIZE:
  1484         _this->gl_config.alpha_size = value;
  1485         break;
  1486     case SDL_GL_DOUBLEBUFFER:
  1487         _this->gl_config.double_buffer = value;
  1488         break;
  1489     case SDL_GL_BUFFER_SIZE:
  1490         _this->gl_config.buffer_size = value;
  1491         break;
  1492     case SDL_GL_DEPTH_SIZE:
  1493         _this->gl_config.depth_size = value;
  1494         break;
  1495     case SDL_GL_STENCIL_SIZE:
  1496         _this->gl_config.stencil_size = value;
  1497         break;
  1498     case SDL_GL_ACCUM_RED_SIZE:
  1499         _this->gl_config.accum_red_size = value;
  1500         break;
  1501     case SDL_GL_ACCUM_GREEN_SIZE:
  1502         _this->gl_config.accum_green_size = value;
  1503         break;
  1504     case SDL_GL_ACCUM_BLUE_SIZE:
  1505         _this->gl_config.accum_blue_size = value;
  1506         break;
  1507     case SDL_GL_ACCUM_ALPHA_SIZE:
  1508         _this->gl_config.accum_alpha_size = value;
  1509         break;
  1510     case SDL_GL_STEREO:
  1511         _this->gl_config.stereo = value;
  1512         break;
  1513     case SDL_GL_MULTISAMPLEBUFFERS:
  1514         _this->gl_config.multisamplebuffers = value;
  1515         break;
  1516     case SDL_GL_MULTISAMPLESAMPLES:
  1517         _this->gl_config.multisamplesamples = value;
  1518         break;
  1519     case SDL_GL_ACCELERATED_VISUAL:
  1520         _this->gl_config.accelerated = value;
  1521         break;
  1522     case SDL_GL_SWAP_CONTROL:
  1523         _this->gl_config.swap_control = value;
  1524         break;
  1525     default:
  1526         SDL_SetError("Unknown OpenGL attribute");
  1527         retval = -1;
  1528         break;
  1529     }
  1530     return (retval);
  1531 }
  1532 
  1533 /* Retrieve an attribute value from the windowing system. */
  1534 int
  1535 SDL_GL_GetAttribute(SDL_GLattr attr, int *value)
  1536 {
  1537     int retval = -1;
  1538 
  1539     if (_this->GL_GetAttribute) {
  1540         retval = _this->GL_GetAttribute(_this, attr, value);
  1541     } else {
  1542         *value = 0;
  1543         SDL_SetError("GL_GetAttribute not supported");
  1544     }
  1545     return retval;
  1546 }
  1547 
  1548 /* Perform a GL buffer swap on the current GL context */
  1549 void
  1550 SDL_GL_SwapBuffers(void)
  1551 {
  1552     if (SDL_VideoSurface->flags & SDL_INTERNALOPENGL) {
  1553         _this->GL_SwapBuffers(_this);
  1554     } else {
  1555         SDL_SetError("OpenGL video mode has not been set");
  1556     }
  1557 }
  1558 
  1559 #if 0                           // FIXME
  1560 /* Utility function used by SDL_WM_SetIcon();
  1561  * flags & 1 for color key, flags & 2 for alpha channel. */
  1562 static void
  1563 CreateMaskFromColorKeyOrAlpha(SDL_Surface * icon, Uint8 * mask, int flags)
  1564 {
  1565     int x, y;
  1566     Uint32 colorkey;
  1567 #define SET_MASKBIT(icon, x, y, mask) \
  1568 	mask[(y*((icon->w+7)/8))+(x/8)] &= ~(0x01<<(7-(x%8)))
  1569 
  1570     colorkey = icon->format->colorkey;
  1571     switch (icon->format->BytesPerPixel) {
  1572     case 1:
  1573         {
  1574             Uint8 *pixels;
  1575             for (y = 0; y < icon->h; ++y) {
  1576                 pixels = (Uint8 *) icon->pixels + y * icon->pitch;
  1577                 for (x = 0; x < icon->w; ++x) {
  1578                     if (*pixels++ == colorkey) {
  1579                         SET_MASKBIT(icon, x, y, mask);
  1580                     }
  1581                 }
  1582             }
  1583         }
  1584         break;
  1585 
  1586     case 2:
  1587         {
  1588             Uint16 *pixels;
  1589             for (y = 0; y < icon->h; ++y) {
  1590                 pixels = (Uint16 *) icon->pixels + y * icon->pitch / 2;
  1591                 for (x = 0; x < icon->w; ++x) {
  1592                     if ((flags & 1) && *pixels == colorkey) {
  1593                         SET_MASKBIT(icon, x, y, mask);
  1594                     } else if ((flags & 2)
  1595                                && (*pixels & icon->format->Amask) == 0) {
  1596                         SET_MASKBIT(icon, x, y, mask);
  1597                     }
  1598                     pixels++;
  1599                 }
  1600             }
  1601         }
  1602         break;
  1603 
  1604     case 4:
  1605         {
  1606             Uint32 *pixels;
  1607             for (y = 0; y < icon->h; ++y) {
  1608                 pixels = (Uint32 *) icon->pixels + y * icon->pitch / 4;
  1609                 for (x = 0; x < icon->w; ++x) {
  1610                     if ((flags & 1) && *pixels == colorkey) {
  1611                         SET_MASKBIT(icon, x, y, mask);
  1612                     } else if ((flags & 2)
  1613                                && (*pixels & icon->format->Amask) == 0) {
  1614                         SET_MASKBIT(icon, x, y, mask);
  1615                     }
  1616                     pixels++;
  1617                 }
  1618             }
  1619         }
  1620         break;
  1621     }
  1622 }
  1623 
  1624 /*
  1625  * Sets the window manager icon for the display window.
  1626  */
  1627 void
  1628 SDL_WM_SetIcon(SDL_Surface * icon, Uint8 * mask)
  1629 {
  1630     if (icon && _this->SetIcon) {
  1631         /* Generate a mask if necessary, and create the icon! */
  1632         if (mask == NULL) {
  1633             int mask_len = icon->h * (icon->w + 7) / 8;
  1634             int flags = 0;
  1635             mask = (Uint8 *) SDL_malloc(mask_len);
  1636             if (mask == NULL) {
  1637                 return;
  1638             }
  1639             SDL_memset(mask, ~0, mask_len);
  1640             if (icon->flags & SDL_SRCCOLORKEY)
  1641                 flags |= 1;
  1642             if (icon->flags & SDL_SRCALPHA)
  1643                 flags |= 2;
  1644             if (flags) {
  1645                 CreateMaskFromColorKeyOrAlpha(icon, mask, flags);
  1646             }
  1647             _this->SetIcon(_this, icon, mask);
  1648             SDL_free(mask);
  1649         } else {
  1650             _this->SetIcon(_this, icon, mask);
  1651         }
  1652     }
  1653 }
  1654 #endif
  1655 
  1656 SDL_bool
  1657 SDL_GetWindowWMInfo(SDL_WindowID windowID, SDL_SysWMinfo * info)
  1658 {
  1659     SDL_Window *window = SDL_GetWindowFromID(windowID);
  1660 
  1661     if (!window || !_this->GetWindowWMInfo) {
  1662         return SDL_FALSE;
  1663     }
  1664     return (_this->GetWindowWMInfo(_this, window, info));
  1665 }
  1666 
  1667 /* vi: set ts=4 sw=4 expandtab: */