]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/libsdl/lib/src/src/video/SDL_video.c
update
[l4.git] / l4 / pkg / libsdl / lib / src / src / video / SDL_video.c
1 /*
2     SDL - Simple DirectMedia Layer
3     Copyright (C) 1997-2012 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_PS3
67         &PS3_bootstrap,
68 #endif
69 #if SDL_VIDEO_DRIVER_GGI
70         &GGI_bootstrap,
71 #endif
72 #if SDL_VIDEO_DRIVER_VGL
73         &VGL_bootstrap,
74 #endif
75 #if SDL_VIDEO_DRIVER_SVGALIB
76         &SVGALIB_bootstrap,
77 #endif
78 #if SDL_VIDEO_DRIVER_GAPI
79         &GAPI_bootstrap,
80 #endif
81 #if SDL_VIDEO_DRIVER_WINDIB
82         &WINDIB_bootstrap,
83 #endif
84 #if SDL_VIDEO_DRIVER_DDRAW
85         &DIRECTX_bootstrap,
86 #endif
87 #if SDL_VIDEO_DRIVER_BWINDOW
88         &BWINDOW_bootstrap,
89 #endif
90 #if SDL_VIDEO_DRIVER_TOOLBOX
91         &TOOLBOX_bootstrap,
92 #endif
93 #if SDL_VIDEO_DRIVER_DRAWSPROCKET
94         &DSp_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_NDS
115         &NDS_bootstrap,
116 #endif
117 #if SDL_VIDEO_DRIVER_RISCOS
118         &RISCOS_bootstrap,
119 #endif
120 #if SDL_VIDEO_DRIVER_OS2FS
121         &OS2FSLib_bootstrap,
122 #endif
123 #if SDL_VIDEO_DRIVER_AALIB
124         &AALIB_bootstrap,
125 #endif
126 #if SDL_VIDEO_DRIVER_CACA
127         &CACA_bootstrap,
128 #endif
129 #if SDL_VIDEO_DRIVER_DUMMY
130         &DUMMY_bootstrap,
131 #endif
132 #ifdef ENABLE_OVLWMVIDEO
133         &OVLWM_bootstrap,
134 #endif
135 #ifdef ENABLE_DOPEVIDEO
136         &DOPE_bootstrap,
137 #endif
138 #ifdef ENABLE_L4FBVIDEO
139         &L4FB_bootstrap,
140 #endif
141
142         NULL
143 };
144
145 SDL_VideoDevice *current_video = NULL;
146
147 /* Various local functions */
148 int SDL_VideoInit(const char *driver_name, Uint32 flags);
149 void SDL_VideoQuit(void);
150 void SDL_GL_UpdateRectsLock(SDL_VideoDevice* this, int numrects, SDL_Rect* rects);
151
152 static SDL_GrabMode SDL_WM_GrabInputOff(void);
153 #if SDL_VIDEO_OPENGL
154 static int lock_count = 0;
155 #endif
156
157
158 /*
159  * Initialize the video and event subsystems -- determine native pixel format
160  */
161 int SDL_VideoInit (const char *driver_name, Uint32 flags)
162 {
163         SDL_VideoDevice *video;
164         int index;
165         int i;
166         SDL_PixelFormat vformat;
167         Uint32 video_flags;
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         /* Check to make sure we don't overwrite 'current_video' */
180         if ( current_video != NULL ) {
181                 SDL_VideoQuit();
182         }
183
184         /* Select the proper video driver */
185         index = 0;
186         video = NULL;
187         if ( driver_name != NULL ) {
188 #if 0   /* This will be replaced with a better driver selection API */
189                 if ( SDL_strrchr(driver_name, ':') != NULL ) {
190                         index = atoi(SDL_strrchr(driver_name, ':')+1);
191                 }
192 #endif
193                 for ( i=0; bootstrap[i]; ++i ) {
194                         if ( SDL_strcasecmp(bootstrap[i]->name, driver_name) == 0) {
195                                 if ( bootstrap[i]->available() ) {
196                                         video = bootstrap[i]->create(index);
197                                         break;
198                                 }
199                         }
200                 }
201         } else {
202                 for ( i=0; bootstrap[i]; ++i ) {
203                         if ( bootstrap[i]->available() ) {
204                                 video = bootstrap[i]->create(index);
205                                 if ( video != NULL ) {
206                                         break;
207                                 }
208                         }
209                 }
210         }
211         if ( video == NULL ) {
212                 SDL_SetError("No available video device");
213                 return(-1);
214         }
215         current_video = video;
216         current_video->name = bootstrap[i]->name;
217
218         /* Do some basic variable initialization */
219         video->screen = NULL;
220         video->shadow = NULL;
221         video->visible = NULL;
222         video->physpal = NULL;
223         video->gammacols = NULL;
224         video->gamma = NULL;
225         video->wm_title = NULL;
226         video->wm_icon  = NULL;
227         video->offset_x = 0;
228         video->offset_y = 0;
229         SDL_memset(&video->info, 0, (sizeof video->info));
230         
231         video->displayformatalphapixel = NULL;
232
233         /* Set some very sane GL defaults */
234         video->gl_config.driver_loaded = 0;
235         video->gl_config.dll_handle = NULL;
236         video->gl_config.red_size = 3;
237         video->gl_config.green_size = 3;
238         video->gl_config.blue_size = 2;
239         video->gl_config.alpha_size = 0;
240         video->gl_config.buffer_size = 0;
241         video->gl_config.depth_size = 16;
242         video->gl_config.stencil_size = 0;
243         video->gl_config.double_buffer = 1;
244         video->gl_config.accum_red_size = 0;
245         video->gl_config.accum_green_size = 0;
246         video->gl_config.accum_blue_size = 0;
247         video->gl_config.accum_alpha_size = 0;
248         video->gl_config.stereo = 0;
249         video->gl_config.multisamplebuffers = 0;
250         video->gl_config.multisamplesamples = 0;
251         video->gl_config.accelerated = -1; /* not known, don't set */
252         video->gl_config.swap_control = -1; /* not known, don't set */
253         
254         /* Initialize the video subsystem */
255         SDL_memset(&vformat, 0, sizeof(vformat));
256         if ( video->VideoInit(video, &vformat) < 0 ) {
257                 SDL_VideoQuit();
258                 return(-1);
259         }
260
261         /* Create a zero sized video surface of the appropriate format */
262         video_flags = SDL_SWSURFACE;
263         SDL_VideoSurface = SDL_CreateRGBSurface(video_flags, 0, 0,
264                                 vformat.BitsPerPixel,
265                                 vformat.Rmask, vformat.Gmask, vformat.Bmask, 0);
266         if ( SDL_VideoSurface == NULL ) {
267                 SDL_VideoQuit();
268                 return(-1);
269         }
270         SDL_PublicSurface = NULL;       /* Until SDL_SetVideoMode() */
271
272 #if 0 /* Don't change the current palette - may be used by other programs.
273        * The application can't do anything with the display surface until
274        * a video mode has been set anyway. :)
275        */
276         /* If we have a palettized surface, create a default palette */
277         if ( SDL_VideoSurface->format->palette ) {
278                 SDL_PixelFormat *vf = SDL_VideoSurface->format;
279                 SDL_DitherColors(vf->palette->colors, vf->BitsPerPixel);
280                 video->SetColors(video,
281                                  0, vf->palette->ncolors, vf->palette->colors);
282         }
283 #endif
284         video->info.vfmt = SDL_VideoSurface->format;
285
286         /* Start the event loop */
287         if ( SDL_StartEventLoop(flags) < 0 ) {
288                 SDL_VideoQuit();
289                 return(-1);
290         }
291         SDL_CursorInit(flags & SDL_INIT_EVENTTHREAD);
292
293         /* We're ready to go! */
294         return(0);
295 }
296
297 char *SDL_VideoDriverName(char *namebuf, int maxlen)
298 {
299         if ( current_video != NULL ) {
300                 SDL_strlcpy(namebuf, current_video->name, maxlen);
301                 return(namebuf);
302         }
303         return(NULL);
304 }
305
306 /*
307  * Get the current display surface
308  */
309 SDL_Surface *SDL_GetVideoSurface(void)
310 {
311         SDL_Surface *visible;
312
313         visible = NULL;
314         if ( current_video ) {
315                 visible = current_video->visible;
316         }
317         return(visible);
318 }
319
320 /*
321  * Get the current information about the video hardware
322  */
323 const SDL_VideoInfo *SDL_GetVideoInfo(void)
324 {
325         const SDL_VideoInfo *info;
326
327         info = NULL;
328         if ( current_video ) {
329                 info = &current_video->info;
330         }
331         return(info);
332 }
333
334 /*
335  * Return a pointer to an array of available screen dimensions for the
336  * given format, sorted largest to smallest.  Returns NULL if there are
337  * no dimensions available for a particular format, or (SDL_Rect **)-1
338  * if any dimension is okay for the given format.  If 'format' is NULL,
339  * the mode list will be for the format given by SDL_GetVideoInfo()->vfmt
340  */
341 SDL_Rect ** SDL_ListModes (SDL_PixelFormat *format, Uint32 flags)
342 {
343         SDL_VideoDevice *video = current_video;
344         SDL_VideoDevice *this  = current_video;
345         SDL_Rect **modes;
346
347         modes = NULL;
348         if ( SDL_VideoSurface ) {
349                 if ( format == NULL ) {
350                         format = SDL_VideoSurface->format;
351                 }
352                 modes = video->ListModes(this, format, flags);
353         }
354         return(modes);
355 }
356
357 /*
358  * Check to see if a particular video mode is supported.
359  * It returns 0 if the requested mode is not supported under any bit depth,
360  * or returns the bits-per-pixel of the closest available mode with the
361  * given width and height.  If this bits-per-pixel is different from the
362  * one used when setting the video mode, SDL_SetVideoMode() will succeed,
363  * but will emulate the requested bits-per-pixel with a shadow surface.
364  */
365 static Uint8 SDL_closest_depths[4][8] = {
366         /* 8 bit closest depth ordering */
367         { 0, 8, 16, 15, 32, 24, 0, 0 },
368         /* 15,16 bit closest depth ordering */
369         { 0, 16, 15, 32, 24, 8, 0, 0 },
370         /* 24 bit closest depth ordering */
371         { 0, 24, 32, 16, 15, 8, 0, 0 },
372         /* 32 bit closest depth ordering */
373         { 0, 32, 16, 15, 24, 8, 0, 0 }
374 };
375
376
377 #ifdef __MACOS__ /* MPW optimization bug? */
378 #define NEGATIVE_ONE 0xFFFFFFFF
379 #else
380 #define NEGATIVE_ONE -1
381 #endif
382
383 int SDL_VideoModeOK (int width, int height, int bpp, Uint32 flags)
384 {
385         int table, b, i;
386         int supported;
387         SDL_PixelFormat format;
388         SDL_Rect **sizes;
389
390         /* Currently 1 and 4 bpp are not supported */
391         if ( bpp < 8 || bpp > 32 ) {
392                 return(0);
393         }
394         if ( (width <= 0) || (height <= 0) ) {
395                 return(0);
396         }
397
398         /* Search through the list valid of modes */
399         SDL_memset(&format, 0, sizeof(format));
400         supported = 0;
401         table = ((bpp+7)/8)-1;
402         SDL_closest_depths[table][0] = bpp;
403         SDL_closest_depths[table][7] = 0;
404         for ( b = 0; !supported && SDL_closest_depths[table][b]; ++b ) {
405                 format.BitsPerPixel = SDL_closest_depths[table][b];
406                 sizes = SDL_ListModes(&format, flags);
407                 if ( sizes == (SDL_Rect **)0 ) {
408                         /* No sizes supported at this bit-depth */
409                         continue;
410                 } else 
411                 if (sizes == (SDL_Rect **)NEGATIVE_ONE) {
412                         /* Any size supported at this bit-depth */
413                         supported = 1;
414                         continue;
415                 } else if (current_video->handles_any_size) {
416                         /* Driver can center a smaller surface to simulate fullscreen */
417                         for ( i=0; sizes[i]; ++i ) {
418                                 if ((sizes[i]->w >= width) && (sizes[i]->h >= height)) {
419                                         supported = 1; /* this mode can fit the centered window. */
420                                         break;
421                                 }
422                         }
423                 } else
424                 for ( i=0; sizes[i]; ++i ) {
425                         if ((sizes[i]->w == width) && (sizes[i]->h == height)) {
426                                 supported = 1;
427                                 break;
428                         }
429                 }
430         }
431         if ( supported ) {
432                 --b;
433                 return(SDL_closest_depths[table][b]);
434         } else {
435                 return(0);
436         }
437 }
438
439 /*
440  * Get the closest non-emulated video mode to the one requested
441  */
442 static int SDL_GetVideoMode (int *w, int *h, int *BitsPerPixel, Uint32 flags)
443 {
444         int table, b, i;
445         int supported;
446         int native_bpp;
447         SDL_PixelFormat format;
448         SDL_Rect **sizes;
449
450         /* Check parameters */
451         if ( *BitsPerPixel < 8 || *BitsPerPixel > 32 ) {
452                 SDL_SetError("Invalid bits per pixel (range is {8...32})");
453                 return(0);
454         }
455         if ((*w <= 0) || (*h <= 0)) {
456                 SDL_SetError("Invalid width or height");
457                 return(0);
458         }
459
460         /* Try the original video mode, get the closest depth */
461         native_bpp = SDL_VideoModeOK(*w, *h, *BitsPerPixel, flags);
462         if ( native_bpp == *BitsPerPixel ) {
463                 return(1);
464         }
465         if ( native_bpp > 0 ) {
466                 *BitsPerPixel = native_bpp;
467                 return(1);
468         }
469
470         /* No exact size match at any depth, look for closest match */
471         SDL_memset(&format, 0, sizeof(format));
472         supported = 0;
473         table = ((*BitsPerPixel+7)/8)-1;
474         SDL_closest_depths[table][0] = *BitsPerPixel;
475         SDL_closest_depths[table][7] = SDL_VideoSurface->format->BitsPerPixel;
476         for ( b = 0; !supported && SDL_closest_depths[table][b]; ++b ) {
477                 int best;
478
479                 format.BitsPerPixel = SDL_closest_depths[table][b];
480                 sizes = SDL_ListModes(&format, flags);
481                 if ( sizes == (SDL_Rect **)0 ) {
482                         /* No sizes supported at this bit-depth */
483                         continue;
484                 }
485                 best=0;
486                 for ( i=0; sizes[i]; ++i ) {
487                         /* Mode with both dimensions bigger or equal than asked ? */
488                         if ((sizes[i]->w >= *w) && (sizes[i]->h >= *h)) {
489                                 /* Mode with any dimension smaller or equal than current best ? */
490                                 if ((sizes[i]->w <= sizes[best]->w) || (sizes[i]->h <= sizes[best]->h)) {
491                                         /* Now choose the mode that has less pixels */
492                                         if ((sizes[i]->w * sizes[i]->h) <= (sizes[best]->w * sizes[best]->h)) {
493                                                 best=i;
494                                                 supported = 1;
495                                         }
496                                 }
497                         }
498                 }
499                 if (supported) {
500                         *w=sizes[best]->w;
501                         *h=sizes[best]->h;
502                         *BitsPerPixel = SDL_closest_depths[table][b];
503                 }
504         }
505         if ( ! supported ) {
506                 SDL_SetError("No video mode large enough for %dx%d", *w, *h);
507         }
508         return(supported);
509 }
510
511 /* This should probably go somewhere else -- like SDL_surface.c */
512 static void SDL_ClearSurface(SDL_Surface *surface)
513 {
514         Uint32 black;
515
516         black = SDL_MapRGB(surface->format, 0, 0, 0);
517         SDL_FillRect(surface, NULL, black);
518         if ((surface->flags&SDL_HWSURFACE) && (surface->flags&SDL_DOUBLEBUF)) {
519                 SDL_Flip(surface);
520                 SDL_FillRect(surface, NULL, black);
521         }
522         if (surface->flags&SDL_FULLSCREEN) {
523                 SDL_Flip(surface);
524         }
525 }
526
527 /*
528  * Create a shadow surface suitable for fooling the app. :-)
529  */
530 static void SDL_CreateShadowSurface(int depth)
531 {
532         Uint32 Rmask, Gmask, Bmask;
533
534         /* Allocate the shadow surface */
535         if ( depth == (SDL_VideoSurface->format)->BitsPerPixel ) {
536                 Rmask = (SDL_VideoSurface->format)->Rmask;
537                 Gmask = (SDL_VideoSurface->format)->Gmask;
538                 Bmask = (SDL_VideoSurface->format)->Bmask;
539         } else {
540                 Rmask = Gmask = Bmask = 0;
541         }
542         SDL_ShadowSurface = SDL_CreateRGBSurface(SDL_SWSURFACE,
543                                 SDL_VideoSurface->w, SDL_VideoSurface->h,
544                                                 depth, Rmask, Gmask, Bmask, 0);
545         if ( SDL_ShadowSurface == NULL ) {
546                 return;
547         }
548
549         /* 8-bit shadow surfaces report that they have exclusive palette */
550         if ( SDL_ShadowSurface->format->palette ) {
551                 SDL_ShadowSurface->flags |= SDL_HWPALETTE;
552                 if ( depth == (SDL_VideoSurface->format)->BitsPerPixel ) {
553                         SDL_memcpy(SDL_ShadowSurface->format->palette->colors,
554                                 SDL_VideoSurface->format->palette->colors,
555                                 SDL_VideoSurface->format->palette->ncolors*
556                                                         sizeof(SDL_Color));
557                 } else {
558                         SDL_DitherColors(
559                         SDL_ShadowSurface->format->palette->colors, depth);
560                 }
561         }
562
563         /* If the video surface is resizable, the shadow should say so */
564         if ( (SDL_VideoSurface->flags & SDL_RESIZABLE) == SDL_RESIZABLE ) {
565                 SDL_ShadowSurface->flags |= SDL_RESIZABLE;
566         }
567         /* If the video surface has no frame, the shadow should say so */
568         if ( (SDL_VideoSurface->flags & SDL_NOFRAME) == SDL_NOFRAME ) {
569                 SDL_ShadowSurface->flags |= SDL_NOFRAME;
570         }
571         /* If the video surface is fullscreen, the shadow should say so */
572         if ( (SDL_VideoSurface->flags & SDL_FULLSCREEN) == SDL_FULLSCREEN ) {
573                 SDL_ShadowSurface->flags |= SDL_FULLSCREEN;
574         }
575         /* If the video surface is flippable, the shadow should say so */
576         if ( (SDL_VideoSurface->flags & SDL_DOUBLEBUF) == SDL_DOUBLEBUF ) {
577                 SDL_ShadowSurface->flags |= SDL_DOUBLEBUF;
578         }
579         return;
580 }
581
582 #ifdef __QNXNTO__
583     #include <sys/neutrino.h>
584 #endif /* __QNXNTO__ */
585
586 #ifdef WIN32
587         extern int sysevents_mouse_pressed;
588 #endif
589
590 /*
591  * Set the requested video mode, allocating a shadow buffer if necessary.
592  */
593 SDL_Surface * SDL_SetVideoMode (int width, int height, int bpp, Uint32 flags)
594 {
595         SDL_VideoDevice *video, *this;
596         SDL_Surface *prev_mode, *mode;
597         int video_w;
598         int video_h;
599         int video_bpp;
600         int is_opengl;
601         SDL_GrabMode saved_grab;
602
603         #ifdef WIN32
604                 sysevents_mouse_pressed = 0;
605         #endif
606
607         /* Start up the video driver, if necessary..
608            WARNING: This is the only function protected this way!
609          */
610         if ( ! current_video ) {
611                 if ( SDL_Init(SDL_INIT_VIDEO|SDL_INIT_NOPARACHUTE) < 0 ) {
612                         return(NULL);
613                 }
614         }
615         this = video = current_video;
616
617         /* Default to the current width and height */
618         if ( width == 0 ) {
619                 width = video->info.current_w;
620         }
621         if ( height == 0 ) {
622                 height = video->info.current_h;
623         }
624         /* Default to the current video bpp */
625         if ( bpp == 0 ) {
626                 flags |= SDL_ANYFORMAT;
627                 bpp = SDL_VideoSurface->format->BitsPerPixel;
628         }
629
630         /* Get a good video mode, the closest one possible */
631         video_w = width;
632         video_h = height;
633         video_bpp = bpp;
634         if ( ! SDL_GetVideoMode(&video_w, &video_h, &video_bpp, flags) ) {
635                 return(NULL);
636         }
637
638         /* Check the requested flags */
639         /* There's no palette in > 8 bits-per-pixel mode */
640         if ( video_bpp > 8 ) {
641                 flags &= ~SDL_HWPALETTE;
642         }
643 #if 0
644         if ( (flags&SDL_FULLSCREEN) != SDL_FULLSCREEN ) {
645                 /* There's no windowed double-buffering */
646                 flags &= ~SDL_DOUBLEBUF;
647         }
648 #endif
649         if ( (flags&SDL_DOUBLEBUF) == SDL_DOUBLEBUF ) {
650                 /* Use hardware surfaces when double-buffering */
651                 flags |= SDL_HWSURFACE;
652         }
653
654         is_opengl = ( ( flags & SDL_OPENGL ) == SDL_OPENGL );
655         if ( is_opengl ) {
656                 /* These flags are for 2D video modes only */
657                 flags &= ~(SDL_HWSURFACE|SDL_DOUBLEBUF);
658         }
659
660         /* Reset the keyboard here so event callbacks can run */
661         SDL_ResetKeyboard();
662         SDL_ResetMouse();
663         SDL_SetMouseRange(width, height);
664         SDL_cursorstate &= ~CURSOR_USINGSW;
665
666         /* Clean up any previous video mode */
667         if ( SDL_PublicSurface != NULL ) {
668                 SDL_PublicSurface = NULL;
669         }
670         if ( SDL_ShadowSurface != NULL ) {
671                 SDL_Surface *ready_to_go;
672                 ready_to_go = SDL_ShadowSurface;
673                 SDL_ShadowSurface = NULL;
674                 SDL_FreeSurface(ready_to_go);
675         }
676         if ( video->physpal ) {
677                 SDL_free(video->physpal->colors);
678                 SDL_free(video->physpal);
679                 video->physpal = NULL;
680         }
681         if( video->gammacols) {
682                 SDL_free(video->gammacols);
683                 video->gammacols = NULL;
684         }
685
686         /* Save the previous grab state and turn off grab for mode switch */
687         saved_grab = SDL_WM_GrabInputOff();
688
689         /* Try to set the video mode, along with offset and clipping */
690         prev_mode = SDL_VideoSurface;
691         SDL_LockCursor();
692         SDL_VideoSurface = NULL;        /* In case it's freed by driver */
693         mode = video->SetVideoMode(this, prev_mode,video_w,video_h,video_bpp,flags);
694         if ( mode ) { /* Prevent resize events from mode change */
695           /* But not on OS/2 */
696 #ifndef __OS2__
697             SDL_PrivateResize(mode->w, mode->h);
698 #endif
699
700             /* Sam - If we asked for OpenGL mode, and didn't get it, fail */
701             if ( is_opengl && !(mode->flags & SDL_OPENGL) ) {
702                 mode = NULL;
703                 SDL_SetError("OpenGL not available");
704             }
705         }
706         /*
707          * rcg11292000
708          * If you try to set an SDL_OPENGL surface, and fail to find a
709          * matching  visual, then the next call to SDL_SetVideoMode()
710          * will segfault, since  we no longer point to a dummy surface,
711          * but rather NULL.
712          * Sam 11/29/00
713          * WARNING, we need to make sure that the previous mode hasn't
714          * already been freed by the video driver.  What do we do in
715          * that case?  Should we call SDL_VideoInit() again?
716          */
717         SDL_VideoSurface = (mode != NULL) ? mode : prev_mode;
718
719         if ( (mode != NULL) && (!is_opengl) ) {
720                 /* Sanity check */
721                 if ( (mode->w < width) || (mode->h < height) ) {
722                         SDL_SetError("Video mode smaller than requested");
723                         return(NULL);
724                 }
725
726                 /* If we have a palettized surface, create a default palette */
727                 if ( mode->format->palette ) {
728                         SDL_PixelFormat *vf = mode->format;
729                         SDL_DitherColors(vf->palette->colors, vf->BitsPerPixel);
730                         video->SetColors(this, 0, vf->palette->ncolors,
731                                                    vf->palette->colors);
732                 }
733
734                 /* Clear the surface to black */
735                 video->offset_x = 0;
736                 video->offset_y = 0;
737                 mode->offset = 0;
738                 SDL_SetClipRect(mode, NULL);
739                 SDL_ClearSurface(mode);
740
741                 /* Now adjust the offsets to match the desired mode */
742                 video->offset_x = (mode->w-width)/2;
743                 video->offset_y = (mode->h-height)/2;
744                 mode->offset = video->offset_y*mode->pitch +
745                                 video->offset_x*mode->format->BytesPerPixel;
746 #ifdef DEBUG_VIDEO
747   fprintf(stderr,
748         "Requested mode: %dx%dx%d, obtained mode %dx%dx%d (offset %d)\n",
749                 width, height, bpp,
750                 mode->w, mode->h, mode->format->BitsPerPixel, mode->offset);
751 #endif
752                 mode->w = width;
753                 mode->h = height;
754                 SDL_SetClipRect(mode, NULL);
755         }
756         SDL_ResetCursor();
757         SDL_UnlockCursor();
758
759         /* If we failed setting a video mode, return NULL... (Uh Oh!) */
760         if ( mode == NULL ) {
761                 return(NULL);
762         }
763
764         /* If there is no window manager, set the SDL_NOFRAME flag */
765         if ( ! video->info.wm_available ) {
766                 mode->flags |= SDL_NOFRAME;
767         }
768
769         /* Reset the mouse cursor and grab for new video mode */
770         SDL_SetCursor(NULL);
771         if ( video->UpdateMouse ) {
772                 video->UpdateMouse(this);
773         }
774         SDL_WM_GrabInput(saved_grab);
775         SDL_GetRelativeMouseState(NULL, NULL); /* Clear first large delta */
776
777 #if SDL_VIDEO_OPENGL
778         /* Load GL symbols (before MakeCurrent, where we need glGetString). */
779         if ( flags & (SDL_OPENGL | SDL_OPENGLBLIT) ) {
780
781 #if defined(__QNXNTO__) && (_NTO_VERSION < 630)
782 #define __SDL_NOGETPROCADDR__
783 #elif defined(__MINT__)
784 #define __SDL_NOGETPROCADDR__
785 #endif
786 #ifdef __SDL_NOGETPROCADDR__
787     #define SDL_PROC(ret,func,params) video->func=func;
788 #else
789     #define SDL_PROC(ret,func,params) \
790     do { \
791         video->func = SDL_GL_GetProcAddress(#func); \
792         if ( ! video->func ) { \
793             SDL_SetError("Couldn't load GL function %s: %s\n", #func, SDL_GetError()); \
794         return(NULL); \
795         } \
796     } while ( 0 );
797
798 #endif /* __SDL_NOGETPROCADDR__ */
799
800 #include "SDL_glfuncs.h"
801 #undef SDL_PROC 
802         }
803 #endif /* SDL_VIDEO_OPENGL */
804
805         /* If we're running OpenGL, make the context current */
806         if ( (video->screen->flags & SDL_OPENGL) &&
807               video->GL_MakeCurrent ) {
808                 if ( video->GL_MakeCurrent(this) < 0 ) {
809                         return(NULL);
810                 }
811         }
812
813         /* Set up a fake SDL surface for OpenGL "blitting" */
814         if ( (flags & SDL_OPENGLBLIT) == SDL_OPENGLBLIT ) {
815                 /* Load GL functions for performing the texture updates */
816 #if SDL_VIDEO_OPENGL
817
818                 /* Create a software surface for blitting */
819 #ifdef GL_VERSION_1_2
820                 /* If the implementation either supports the packed pixels
821                    extension, or implements the core OpenGL 1.2 API, it will
822                    support the GL_UNSIGNED_SHORT_5_6_5 texture format.
823                  */
824                 if ( (bpp == 16) &&
825                      (SDL_strstr((const char *)video->glGetString(GL_EXTENSIONS), "GL_EXT_packed_pixels") ||
826                      (SDL_atof((const char *)video->glGetString(GL_VERSION)) >= 1.2f))
827                    ) {
828                         video->is_32bit = 0;
829                         SDL_VideoSurface = SDL_CreateRGBSurface(
830                                 flags, 
831                                 width, 
832                                 height,  
833                                 16,
834                                 31 << 11,
835                                 63 << 5,
836                                 31,
837                                 0
838                                 );
839                 }
840                 else
841 #endif /* OpenGL 1.2 */
842                 {
843                         video->is_32bit = 1;
844                         SDL_VideoSurface = SDL_CreateRGBSurface(
845                                 flags, 
846                                 width, 
847                                 height, 
848                                 32, 
849 #if SDL_BYTEORDER == SDL_LIL_ENDIAN
850                                 0x000000FF,
851                                 0x0000FF00,
852                                 0x00FF0000,
853                                 0xFF000000
854 #else
855                                 0xFF000000,
856                                 0x00FF0000,
857                                 0x0000FF00,
858                                 0x000000FF
859 #endif
860                                 );
861                 }
862                 if ( ! SDL_VideoSurface ) {
863                         return(NULL);
864                 }
865                 SDL_VideoSurface->flags = mode->flags | SDL_OPENGLBLIT;
866
867                 /* Free the original video mode surface (is this safe?) */
868                 SDL_FreeSurface(mode);
869
870                 /* Set the surface completely opaque & white by default */
871                 SDL_memset( SDL_VideoSurface->pixels, 255, SDL_VideoSurface->h * SDL_VideoSurface->pitch );
872                 video->glGenTextures( 1, &video->texture );
873                 video->glBindTexture( GL_TEXTURE_2D, video->texture );
874                 video->glTexImage2D(
875                         GL_TEXTURE_2D,
876                         0,
877                         video->is_32bit ? GL_RGBA : GL_RGB,
878                         256,
879                         256,
880                         0,
881                         video->is_32bit ? GL_RGBA : GL_RGB,
882 #ifdef GL_VERSION_1_2
883                         video->is_32bit ? GL_UNSIGNED_BYTE : GL_UNSIGNED_SHORT_5_6_5,
884 #else
885                         GL_UNSIGNED_BYTE,
886 #endif
887                         NULL);
888
889                 video->UpdateRects = SDL_GL_UpdateRectsLock;
890 #else
891                 SDL_SetError("Somebody forgot to #define SDL_VIDEO_OPENGL");
892                 return(NULL);
893 #endif
894         }
895
896         /* Create a shadow surface if necessary */
897         /* There are three conditions under which we create a shadow surface:
898                 1.  We need a particular bits-per-pixel that we didn't get.
899                 2.  We need a hardware palette and didn't get one.
900                 3.  We need a software surface and got a hardware surface.
901         */
902         if ( !(SDL_VideoSurface->flags & SDL_OPENGL) &&
903              (
904              (  !(flags&SDL_ANYFORMAT) &&
905                         (SDL_VideoSurface->format->BitsPerPixel != bpp)) ||
906              (   (flags&SDL_HWPALETTE) && 
907                                 !(SDL_VideoSurface->flags&SDL_HWPALETTE)) ||
908                 /* If the surface is in hardware, video writes are visible
909                    as soon as they are performed, so we need to buffer them
910                  */
911              (   ((flags&SDL_HWSURFACE) == SDL_SWSURFACE) &&
912                                 (SDL_VideoSurface->flags&SDL_HWSURFACE)) ||
913              (   (flags&SDL_DOUBLEBUF) &&
914                                 (SDL_VideoSurface->flags&SDL_HWSURFACE) &&
915                                 !(SDL_VideoSurface->flags&SDL_DOUBLEBUF))
916              ) ) {
917                 SDL_CreateShadowSurface(bpp);
918                 if ( SDL_ShadowSurface == NULL ) {
919                         SDL_SetError("Couldn't create shadow surface");
920                         return(NULL);
921                 }
922                 SDL_PublicSurface = SDL_ShadowSurface;
923         } else {
924                 SDL_PublicSurface = SDL_VideoSurface;
925         }
926         video->info.vfmt = SDL_VideoSurface->format;
927         video->info.current_w = SDL_VideoSurface->w;
928         video->info.current_h = SDL_VideoSurface->h;
929
930         /* We're done! */
931         return(SDL_PublicSurface);
932 }
933
934 /* 
935  * Convert a surface into the video pixel format.
936  */
937 SDL_Surface * SDL_DisplayFormat (SDL_Surface *surface)
938 {
939         Uint32 flags;
940
941         if ( ! SDL_PublicSurface ) {
942                 SDL_SetError("No video mode has been set");
943                 return(NULL);
944         }
945         /* Set the flags appropriate for copying to display surface */
946         if (((SDL_PublicSurface->flags&SDL_HWSURFACE) == SDL_HWSURFACE) && current_video->info.blit_hw)
947                 flags = SDL_HWSURFACE;
948         else 
949                 flags = SDL_SWSURFACE;
950 #ifdef AUTORLE_DISPLAYFORMAT
951         flags |= (surface->flags & (SDL_SRCCOLORKEY|SDL_SRCALPHA));
952         flags |= SDL_RLEACCELOK;
953 #else
954         flags |= surface->flags & (SDL_SRCCOLORKEY|SDL_SRCALPHA|SDL_RLEACCELOK);
955 #endif
956         return(SDL_ConvertSurface(surface, SDL_PublicSurface->format, flags));
957 }
958
959 /*
960  * Convert a surface into a format that's suitable for blitting to
961  * the screen, but including an alpha channel.
962  */
963 SDL_Surface *SDL_DisplayFormatAlpha(SDL_Surface *surface)
964 {
965         SDL_PixelFormat *vf;
966         SDL_PixelFormat *format;
967         SDL_Surface *converted;
968         Uint32 flags;
969         /* default to ARGB8888 */
970         Uint32 amask = 0xff000000;
971         Uint32 rmask = 0x00ff0000;
972         Uint32 gmask = 0x0000ff00;
973         Uint32 bmask = 0x000000ff;
974
975         if ( ! SDL_PublicSurface ) {
976                 SDL_SetError("No video mode has been set");
977                 return(NULL);
978         }
979         vf = SDL_PublicSurface->format;
980
981         switch(vf->BytesPerPixel) {
982             case 2:
983                 /* For XGY5[56]5, use, AXGY8888, where {X, Y} = {R, B}.
984                    For anything else (like ARGB4444) it doesn't matter
985                    since we have no special code for it anyway */
986                 if ( (vf->Rmask == 0x1f) &&
987                      (vf->Bmask == 0xf800 || vf->Bmask == 0x7c00)) {
988                         rmask = 0xff;
989                         bmask = 0xff0000;
990                 }
991                 break;
992
993             case 3:
994             case 4:
995                 /* Keep the video format, as long as the high 8 bits are
996                    unused or alpha */
997                 if ( (vf->Rmask == 0xff) && (vf->Bmask == 0xff0000) ) {
998                         rmask = 0xff;
999                         bmask = 0xff0000;
1000                 } else if ( vf->Rmask == 0xFF00 && (vf->Bmask == 0xFF000000) ) {
1001                         amask = 0x000000FF;
1002                         rmask = 0x0000FF00;
1003                         gmask = 0x00FF0000;
1004                         bmask = 0xFF000000;
1005                 }
1006                 break;
1007
1008             default:
1009                 /* We have no other optimised formats right now. When/if a new
1010                    optimised alpha format is written, add the converter here */
1011                 break;
1012         }
1013         format = SDL_AllocFormat(32, rmask, gmask, bmask, amask);
1014         flags = SDL_PublicSurface->flags & SDL_HWSURFACE;
1015         flags |= surface->flags & (SDL_SRCALPHA | SDL_RLEACCELOK);
1016         converted = SDL_ConvertSurface(surface, format, flags);
1017         SDL_FreeFormat(format);
1018         return(converted);
1019 }
1020
1021 /*
1022  * Update a specific portion of the physical screen
1023  */
1024 void SDL_UpdateRect(SDL_Surface *screen, Sint32 x, Sint32 y, Uint32 w, Uint32 h)
1025 {
1026         if ( screen ) {
1027                 SDL_Rect rect;
1028
1029                 /* Perform some checking */
1030                 if ( w == 0 )
1031                         w = screen->w;
1032                 if ( h == 0 )
1033                         h = screen->h;
1034                 if ( (int)(x+w) > screen->w )
1035                         return;
1036                 if ( (int)(y+h) > screen->h )
1037                         return;
1038
1039                 /* Fill the rectangle */
1040                 rect.x = (Sint16)x;
1041                 rect.y = (Sint16)y;
1042                 rect.w = (Uint16)w;
1043                 rect.h = (Uint16)h;
1044                 SDL_UpdateRects(screen, 1, &rect);
1045         }
1046 }
1047 void SDL_UpdateRects (SDL_Surface *screen, int numrects, SDL_Rect *rects)
1048 {
1049         int i;
1050         SDL_VideoDevice *video = current_video;
1051         SDL_VideoDevice *this = current_video;
1052
1053         if ( (screen->flags & (SDL_OPENGL | SDL_OPENGLBLIT)) == SDL_OPENGL ) {
1054                 SDL_SetError("OpenGL active, use SDL_GL_SwapBuffers()");
1055                 return;
1056         }
1057         if ( screen == SDL_ShadowSurface ) {
1058                 /* Blit the shadow surface using saved mapping */
1059                 SDL_Palette *pal = screen->format->palette;
1060                 SDL_Color *saved_colors = NULL;
1061                 if ( pal && !(SDL_VideoSurface->flags & SDL_HWPALETTE) ) {
1062                         /* simulated 8bpp, use correct physical palette */
1063                         saved_colors = pal->colors;
1064                         if ( video->gammacols ) {
1065                                 /* gamma-corrected palette */
1066                                 pal->colors = video->gammacols;
1067                         } else if ( video->physpal ) {
1068                                 /* physical palette different from logical */
1069                                 pal->colors = video->physpal->colors;
1070                         }
1071                 }
1072                 if ( SHOULD_DRAWCURSOR(SDL_cursorstate) ) {
1073                         SDL_LockCursor();
1074                         SDL_DrawCursor(SDL_ShadowSurface);
1075                         for ( i=0; i<numrects; ++i ) {
1076                                 SDL_LowerBlit(SDL_ShadowSurface, &rects[i], 
1077                                                 SDL_VideoSurface, &rects[i]);
1078                         }
1079                         SDL_EraseCursor(SDL_ShadowSurface);
1080                         SDL_UnlockCursor();
1081                 } else {
1082                         for ( i=0; i<numrects; ++i ) {
1083                                 SDL_LowerBlit(SDL_ShadowSurface, &rects[i], 
1084                                                 SDL_VideoSurface, &rects[i]);
1085                         }
1086                 }
1087                 if ( saved_colors ) {
1088                         pal->colors = saved_colors;
1089                 }
1090
1091                 /* Fall through to video surface update */
1092                 screen = SDL_VideoSurface;
1093         }
1094         if ( screen == SDL_VideoSurface ) {
1095                 /* Update the video surface */
1096                 if ( screen->offset ) {
1097                         for ( i=0; i<numrects; ++i ) {
1098                                 rects[i].x += video->offset_x;
1099                                 rects[i].y += video->offset_y;
1100                         }
1101                         video->UpdateRects(this, numrects, rects);
1102                         for ( i=0; i<numrects; ++i ) {
1103                                 rects[i].x -= video->offset_x;
1104                                 rects[i].y -= video->offset_y;
1105                         }
1106                 } else {
1107                         video->UpdateRects(this, numrects, rects);
1108                 }
1109         }
1110 }
1111
1112 /*
1113  * Performs hardware double buffering, if possible, or a full update if not.
1114  */
1115 int SDL_Flip(SDL_Surface *screen)
1116 {
1117         SDL_VideoDevice *video = current_video;
1118         /* Copy the shadow surface to the video surface */
1119         if ( screen == SDL_ShadowSurface ) {
1120                 SDL_Rect rect;
1121                 SDL_Palette *pal = screen->format->palette;
1122                 SDL_Color *saved_colors = NULL;
1123                 if ( pal && !(SDL_VideoSurface->flags & SDL_HWPALETTE) ) {
1124                         /* simulated 8bpp, use correct physical palette */
1125                         saved_colors = pal->colors;
1126                         if ( video->gammacols ) {
1127                                 /* gamma-corrected palette */
1128                                 pal->colors = video->gammacols;
1129                         } else if ( video->physpal ) {
1130                                 /* physical palette different from logical */
1131                                 pal->colors = video->physpal->colors;
1132                         }
1133                 }
1134
1135                 rect.x = 0;
1136                 rect.y = 0;
1137                 rect.w = screen->w;
1138                 rect.h = screen->h;
1139                 if ( SHOULD_DRAWCURSOR(SDL_cursorstate) ) {
1140                         SDL_LockCursor();
1141                         SDL_DrawCursor(SDL_ShadowSurface);
1142                         SDL_LowerBlit(SDL_ShadowSurface, &rect,
1143                                         SDL_VideoSurface, &rect);
1144                         SDL_EraseCursor(SDL_ShadowSurface);
1145                         SDL_UnlockCursor();
1146                 } else {
1147                         SDL_LowerBlit(SDL_ShadowSurface, &rect,
1148                                         SDL_VideoSurface, &rect);
1149                 }
1150                 if ( saved_colors ) {
1151                         pal->colors = saved_colors;
1152                 }
1153
1154                 /* Fall through to video surface update */
1155                 screen = SDL_VideoSurface;
1156         }
1157         if ( (screen->flags & SDL_DOUBLEBUF) == SDL_DOUBLEBUF ) {
1158                 SDL_VideoDevice *this  = current_video;
1159                 return(video->FlipHWSurface(this, SDL_VideoSurface));
1160         } else {
1161                 SDL_UpdateRect(screen, 0, 0, 0, 0);
1162         }
1163         return(0);
1164 }
1165
1166 static void SetPalette_logical(SDL_Surface *screen, SDL_Color *colors,
1167                                int firstcolor, int ncolors)
1168 {
1169         SDL_Palette *pal = screen->format->palette;
1170         SDL_Palette *vidpal;
1171
1172         if ( colors != (pal->colors + firstcolor) ) {
1173                 SDL_memcpy(pal->colors + firstcolor, colors,
1174                        ncolors * sizeof(*colors));
1175         }
1176
1177         if ( current_video && SDL_VideoSurface ) {
1178                 vidpal = SDL_VideoSurface->format->palette;
1179                 if ( (screen == SDL_ShadowSurface) && vidpal ) {
1180                         /*
1181                          * This is a shadow surface, and the physical
1182                          * framebuffer is also indexed. Propagate the
1183                          * changes to its logical palette so that
1184                          * updates are always identity blits
1185                          */
1186                         SDL_memcpy(vidpal->colors + firstcolor, colors,
1187                                ncolors * sizeof(*colors));
1188                 }
1189         }
1190         SDL_FormatChanged(screen);
1191 }
1192
1193 static int SetPalette_physical(SDL_Surface *screen,
1194                                SDL_Color *colors, int firstcolor, int ncolors)
1195 {
1196         SDL_VideoDevice *video = current_video;
1197         int gotall = 1;
1198
1199         if ( video->physpal ) {
1200                 /* We need to copy the new colors, since we haven't
1201                  * already done the copy in the logical set above.
1202                  */
1203                 SDL_memcpy(video->physpal->colors + firstcolor,
1204                        colors, ncolors * sizeof(*colors));
1205         }
1206         if ( screen == SDL_ShadowSurface ) {
1207                 if ( SDL_VideoSurface->flags & SDL_HWPALETTE ) {
1208                         /*
1209                          * The real screen is also indexed - set its physical
1210                          * palette. The physical palette does not include the
1211                          * gamma modification, we apply it directly instead,
1212                          * but this only happens if we have hardware palette.
1213                          */
1214                         screen = SDL_VideoSurface;
1215                 } else {
1216                         /*
1217                          * The video surface is not indexed - invalidate any
1218                          * active shadow-to-video blit mappings.
1219                          */
1220                         if ( screen->map->dst == SDL_VideoSurface ) {
1221                                 SDL_InvalidateMap(screen->map);
1222                         }
1223                         if ( video->gamma ) {
1224                                 if( ! video->gammacols ) {
1225                                         SDL_Palette *pp = video->physpal;
1226                                         if(!pp)
1227                                                 pp = screen->format->palette;
1228                                         video->gammacols = SDL_malloc(pp->ncolors
1229                                                           * sizeof(SDL_Color));
1230                                         SDL_ApplyGamma(video->gamma,
1231                                                        pp->colors,
1232                                                        video->gammacols,
1233                                                        pp->ncolors);
1234                                 } else {
1235                                         SDL_ApplyGamma(video->gamma, colors,
1236                                                        video->gammacols
1237                                                        + firstcolor,
1238                                                        ncolors);
1239                                 }
1240                         }
1241                         SDL_UpdateRect(screen, 0, 0, 0, 0);
1242                 }
1243         }
1244
1245         if ( screen == SDL_VideoSurface ) {
1246                 SDL_Color gcolors[256];
1247
1248                 if ( video->gamma ) {
1249                         SDL_ApplyGamma(video->gamma, colors, gcolors, ncolors);
1250                         colors = gcolors;
1251                 }
1252                 gotall = video->SetColors(video, firstcolor, ncolors, colors);
1253                 if ( ! gotall ) {
1254                         /* The video flags shouldn't have SDL_HWPALETTE, and
1255                            the video driver is responsible for copying back the
1256                            correct colors into the video surface palette.
1257                         */
1258                         ;
1259                 }
1260                 SDL_CursorPaletteChanged();
1261         }
1262         return gotall;
1263 }
1264
1265 /*
1266  * Set the physical and/or logical colormap of a surface:
1267  * Only the screen has a physical colormap. It determines what is actually
1268  * sent to the display.
1269  * The logical colormap is used to map blits to/from the surface.
1270  * 'which' is one or both of SDL_LOGPAL, SDL_PHYSPAL
1271  *
1272  * Return nonzero if all colours were set as requested, or 0 otherwise.
1273  */
1274 int SDL_SetPalette(SDL_Surface *screen, int which,
1275                    SDL_Color *colors, int firstcolor, int ncolors)
1276 {
1277         SDL_Palette *pal;
1278         int gotall;
1279         int palsize;
1280
1281         if ( !screen ) {
1282                 return 0;
1283         }
1284         if ( !current_video || screen != SDL_PublicSurface ) {
1285                 /* only screens have physical palettes */
1286                 which &= ~SDL_PHYSPAL;
1287         } else if ( (screen->flags & SDL_HWPALETTE) != SDL_HWPALETTE ) {
1288                 /* hardware palettes required for split colormaps */
1289                 which |= SDL_PHYSPAL | SDL_LOGPAL;
1290         }
1291
1292         /* Verify the parameters */
1293         pal = screen->format->palette;
1294         if( !pal ) {
1295                 return 0;       /* not a palettized surface */
1296         }
1297         gotall = 1;
1298         palsize = 1 << screen->format->BitsPerPixel;
1299         if ( ncolors > (palsize - firstcolor) ) {
1300                 ncolors = (palsize - firstcolor);
1301                 gotall = 0;
1302         }
1303
1304         if ( which & SDL_LOGPAL ) {
1305                 /*
1306                  * Logical palette change: The actual screen isn't affected,
1307                  * but the internal colormap is altered so that the
1308                  * interpretation of the pixel values (for blits etc) is
1309                  * changed.
1310                  */
1311                 SetPalette_logical(screen, colors, firstcolor, ncolors);
1312         }
1313         if ( which & SDL_PHYSPAL ) {
1314                 SDL_VideoDevice *video = current_video;
1315                 /*
1316                  * Physical palette change: This doesn't affect the
1317                  * program's idea of what the screen looks like, but changes
1318                  * its actual appearance.
1319                  */
1320                 if ( !video->physpal && !(which & SDL_LOGPAL) ) {
1321                         /* Lazy physical palette allocation */
1322                         int size;
1323                         SDL_Palette *pp = SDL_malloc(sizeof(*pp));
1324                         if ( !pp ) {
1325                                 return 0;
1326                         }
1327                         video->physpal = pp;
1328                         pp->ncolors = pal->ncolors;
1329                         size = pp->ncolors * sizeof(SDL_Color);
1330                         pp->colors = SDL_malloc(size);
1331                         if ( !pp->colors ) {
1332                                 return 0;
1333                         }
1334                         SDL_memcpy(pp->colors, pal->colors, size);
1335                 }
1336                 if ( ! SetPalette_physical(screen,
1337                                            colors, firstcolor, ncolors) ) {
1338                         gotall = 0;
1339                 }
1340         }
1341         return gotall;
1342 }
1343
1344 int SDL_SetColors(SDL_Surface *screen, SDL_Color *colors, int firstcolor,
1345                   int ncolors)
1346 {
1347         return SDL_SetPalette(screen, SDL_LOGPAL | SDL_PHYSPAL,
1348                               colors, firstcolor, ncolors);
1349 }
1350
1351 /*
1352  * Clean up the video subsystem
1353  */
1354 void SDL_VideoQuit (void)
1355 {
1356         SDL_Surface *ready_to_go;
1357
1358         if ( current_video ) {
1359                 SDL_VideoDevice *video = current_video;
1360                 SDL_VideoDevice *this  = current_video;
1361
1362                 /* Halt event processing before doing anything else */
1363                 SDL_StopEventLoop();
1364
1365                 /* Clean up allocated window manager items */
1366                 if ( SDL_PublicSurface ) {
1367                         SDL_PublicSurface = NULL;
1368                 }
1369                 SDL_CursorQuit();
1370
1371                 /* Just in case... */
1372                 SDL_WM_GrabInputOff();
1373
1374                 /* Clean up the system video */
1375                 video->VideoQuit(this);
1376
1377                 /* Free any lingering surfaces */
1378                 ready_to_go = SDL_ShadowSurface;
1379                 SDL_ShadowSurface = NULL;
1380                 SDL_FreeSurface(ready_to_go);
1381                 if ( SDL_VideoSurface != NULL ) {
1382                         ready_to_go = SDL_VideoSurface;
1383                         SDL_VideoSurface = NULL;
1384                         SDL_FreeSurface(ready_to_go);
1385                 }
1386                 SDL_PublicSurface = NULL;
1387
1388                 /* Clean up miscellaneous memory */
1389                 if ( video->physpal ) {
1390                         SDL_free(video->physpal->colors);
1391                         SDL_free(video->physpal);
1392                         video->physpal = NULL;
1393                 }
1394                 if ( video->gammacols ) {
1395                         SDL_free(video->gammacols);
1396                         video->gammacols = NULL;
1397                 }
1398                 if ( video->gamma ) {
1399                         SDL_free(video->gamma);
1400                         video->gamma = NULL;
1401                 }
1402                 if ( video->wm_title != NULL ) {
1403                         SDL_free(video->wm_title);
1404                         video->wm_title = NULL;
1405                 }
1406                 if ( video->wm_icon != NULL ) {
1407                         SDL_free(video->wm_icon);
1408                         video->wm_icon = NULL;
1409                 }
1410
1411                 /* Finish cleaning up video subsystem */
1412                 video->free(this);
1413                 current_video = NULL;
1414         }
1415         return;
1416 }
1417
1418 /* Load the GL driver library */
1419 int SDL_GL_LoadLibrary(const char *path)
1420 {
1421         SDL_VideoDevice *video = current_video;
1422         SDL_VideoDevice *this = current_video;
1423         int retval;
1424
1425         retval = -1;
1426         if ( video == NULL ) {
1427                 SDL_SetError("Video subsystem has not been initialized");
1428         } else {
1429                 if ( video->GL_LoadLibrary ) {
1430                         retval = video->GL_LoadLibrary(this, path);
1431                 } else {
1432                         SDL_SetError("No dynamic GL support in video driver");
1433                 }
1434         }
1435         return(retval);
1436 }
1437
1438 void *SDL_GL_GetProcAddress(const char* proc)
1439 {
1440         SDL_VideoDevice *video = current_video;
1441         SDL_VideoDevice *this = current_video;
1442         void *func;
1443
1444         func = NULL;
1445         if ( video->GL_GetProcAddress ) {
1446                 if ( video->gl_config.driver_loaded ) {
1447                         func = video->GL_GetProcAddress(this, proc);
1448                 } else {
1449                         SDL_SetError("No GL driver has been loaded");
1450                 }
1451         } else {
1452                 SDL_SetError("No dynamic GL support in video driver");
1453         }
1454         return func;
1455 }
1456
1457 /* Set the specified GL attribute for setting up a GL video mode */
1458 int SDL_GL_SetAttribute( SDL_GLattr attr, int value )
1459 {
1460         int retval;
1461         SDL_VideoDevice *video = current_video;
1462
1463         retval = 0;
1464         switch (attr) {
1465                 case SDL_GL_RED_SIZE:
1466                         video->gl_config.red_size = value;
1467                         break;
1468                 case SDL_GL_GREEN_SIZE:
1469                         video->gl_config.green_size = value;
1470                         break;
1471                 case SDL_GL_BLUE_SIZE:
1472                         video->gl_config.blue_size = value;
1473                         break;
1474                 case SDL_GL_ALPHA_SIZE:
1475                         video->gl_config.alpha_size = value;
1476                         break;
1477                 case SDL_GL_DOUBLEBUFFER:
1478                         video->gl_config.double_buffer = value;
1479                         break;
1480                 case SDL_GL_BUFFER_SIZE:
1481                         video->gl_config.buffer_size = value;
1482                         break;
1483                 case SDL_GL_DEPTH_SIZE:
1484                         video->gl_config.depth_size = value;
1485                         break;
1486                 case SDL_GL_STENCIL_SIZE:
1487                         video->gl_config.stencil_size = value;
1488                         break;
1489                 case SDL_GL_ACCUM_RED_SIZE:
1490                         video->gl_config.accum_red_size = value;
1491                         break;
1492                 case SDL_GL_ACCUM_GREEN_SIZE:
1493                         video->gl_config.accum_green_size = value;
1494                         break;
1495                 case SDL_GL_ACCUM_BLUE_SIZE:
1496                         video->gl_config.accum_blue_size = value;
1497                         break;
1498                 case SDL_GL_ACCUM_ALPHA_SIZE:
1499                         video->gl_config.accum_alpha_size = value;
1500                         break;
1501                 case SDL_GL_STEREO:
1502                         video->gl_config.stereo = value;
1503                         break;
1504                 case SDL_GL_MULTISAMPLEBUFFERS:
1505                         video->gl_config.multisamplebuffers = value;
1506                         break;
1507                 case SDL_GL_MULTISAMPLESAMPLES:
1508                         video->gl_config.multisamplesamples = value;
1509                         break;
1510                 case SDL_GL_ACCELERATED_VISUAL:
1511                         video->gl_config.accelerated = value;
1512                         break;
1513                 case SDL_GL_SWAP_CONTROL:
1514                         video->gl_config.swap_control = value;
1515                         break;
1516                 default:
1517                         SDL_SetError("Unknown OpenGL attribute");
1518                         retval = -1;
1519                         break;
1520         }
1521         return(retval);
1522 }
1523
1524 /* Retrieve an attribute value from the windowing system. */
1525 int SDL_GL_GetAttribute(SDL_GLattr attr, int* value)
1526 {
1527         int retval = -1;
1528         SDL_VideoDevice* video = current_video;
1529         SDL_VideoDevice* this = current_video;
1530
1531         if ( video->GL_GetAttribute ) {
1532                 retval = this->GL_GetAttribute(this, attr, value);
1533         } else {
1534                 *value = 0;
1535                 SDL_SetError("GL_GetAttribute not supported");
1536         }
1537         return retval;
1538 }
1539
1540 /* Perform a GL buffer swap on the current GL context */
1541 void SDL_GL_SwapBuffers(void)
1542 {
1543         SDL_VideoDevice *video = current_video;
1544         SDL_VideoDevice *this = current_video;
1545
1546         if ( video->screen->flags & SDL_OPENGL ) {
1547                 video->GL_SwapBuffers(this);
1548         } else {
1549                 SDL_SetError("OpenGL video mode has not been set");
1550         }
1551 }
1552
1553 /* Update rects with locking */
1554 void SDL_GL_UpdateRectsLock(SDL_VideoDevice* this, int numrects, SDL_Rect *rects)
1555 {
1556         SDL_GL_Lock();
1557         SDL_GL_UpdateRects(numrects, rects);
1558         SDL_GL_Unlock();
1559 }
1560
1561 /* Update rects without state setting and changing (the caller is responsible for it) */
1562 void SDL_GL_UpdateRects(int numrects, SDL_Rect *rects)
1563 {
1564 #if SDL_VIDEO_OPENGL
1565         SDL_VideoDevice *this = current_video;
1566         SDL_Rect update, tmp;
1567         int x, y, i;
1568
1569         for ( i = 0; i < numrects; i++ )
1570         {
1571                 tmp.y = rects[i].y;
1572                 tmp.h = rects[i].h;
1573                 for ( y = 0; y <= rects[i].h / 256; y++ )
1574                 {
1575                         tmp.x = rects[i].x;
1576                         tmp.w = rects[i].w;
1577                         for ( x = 0; x <= rects[i].w / 256; x++ )
1578                         {
1579                                 update.x = tmp.x;
1580                                 update.y = tmp.y;
1581                                 update.w = tmp.w;
1582                                 update.h = tmp.h;
1583
1584                                 if ( update.w > 256 )
1585                                         update.w = 256;
1586
1587                                 if ( update.h > 256 )
1588                                         update.h = 256;
1589                         
1590                                 this->glFlush();
1591                                 this->glTexSubImage2D( 
1592                                         GL_TEXTURE_2D, 
1593                                         0, 
1594                                         0, 
1595                                         0, 
1596                                         update.w, 
1597                                         update.h, 
1598                                         this->is_32bit? GL_RGBA : GL_RGB,
1599 #ifdef GL_VERSION_1_2
1600                                         this->is_32bit ? GL_UNSIGNED_BYTE : GL_UNSIGNED_SHORT_5_6_5,
1601 #else
1602                                         GL_UNSIGNED_BYTE,
1603 #endif
1604                                         (Uint8 *)this->screen->pixels + 
1605                                                 this->screen->format->BytesPerPixel * update.x + 
1606                                                 update.y * this->screen->pitch );
1607         
1608                                 this->glFlush();
1609                                 /*
1610                                 * Note the parens around the function name:
1611                                 * This is because some OpenGL implementations define glTexCoord etc 
1612                                 * as macros, and we don't want them expanded here.
1613                                 */
1614                                 this->glBegin(GL_TRIANGLE_STRIP);
1615                                         (this->glTexCoord2f)( 0.0, 0.0 );       
1616                                         (this->glVertex2i)( update.x, update.y );
1617                                         (this->glTexCoord2f)( (float)(update.w / 256.0), 0.0 ); 
1618                                         (this->glVertex2i)( update.x + update.w, update.y );
1619                                         (this->glTexCoord2f)( 0.0, (float)(update.h / 256.0) );
1620                                         (this->glVertex2i)( update.x, update.y + update.h );
1621                                         (this->glTexCoord2f)( (float)(update.w / 256.0), (float)(update.h / 256.0) );   
1622                                         (this->glVertex2i)( update.x + update.w , update.y + update.h );
1623                                 this->glEnd();  
1624                         
1625                                 tmp.x += 256;
1626                                 tmp.w -= 256;
1627                         }
1628                         tmp.y += 256;
1629                         tmp.h -= 256;
1630                 }
1631         }
1632 #endif
1633 }
1634
1635 /* Lock == save current state */
1636 void SDL_GL_Lock()
1637 {
1638 #if SDL_VIDEO_OPENGL
1639         lock_count--;
1640         if (lock_count==-1)
1641         {
1642                 SDL_VideoDevice *this = current_video;
1643
1644                 this->glPushAttrib( GL_ALL_ATTRIB_BITS );       /* TODO: narrow range of what is saved */
1645 #ifdef GL_CLIENT_PIXEL_STORE_BIT
1646                 this->glPushClientAttrib( GL_CLIENT_PIXEL_STORE_BIT );
1647 #endif
1648
1649                 this->glEnable(GL_TEXTURE_2D);
1650                 this->glEnable(GL_BLEND);
1651                 this->glDisable(GL_FOG);
1652                 this->glDisable(GL_ALPHA_TEST);
1653                 this->glDisable(GL_DEPTH_TEST);
1654                 this->glDisable(GL_SCISSOR_TEST);       
1655                 this->glDisable(GL_STENCIL_TEST);
1656                 this->glDisable(GL_CULL_FACE);
1657
1658                 this->glBindTexture( GL_TEXTURE_2D, this->texture );
1659                 this->glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
1660                 this->glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
1661                 this->glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
1662                 this->glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
1663                 this->glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
1664
1665                 this->glPixelStorei( GL_UNPACK_ROW_LENGTH, this->screen->pitch / this->screen->format->BytesPerPixel );
1666                 this->glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1667                 (this->glColor4f)(1.0, 1.0, 1.0, 1.0);          /* Solaris workaround */
1668
1669                 this->glViewport(0, 0, this->screen->w, this->screen->h);
1670                 this->glMatrixMode(GL_PROJECTION);
1671                 this->glPushMatrix();
1672                 this->glLoadIdentity();
1673
1674                 this->glOrtho(0.0, (GLdouble) this->screen->w, (GLdouble) this->screen->h, 0.0, 0.0, 1.0);
1675
1676                 this->glMatrixMode(GL_MODELVIEW);
1677                 this->glPushMatrix();
1678                 this->glLoadIdentity();
1679         }
1680 #endif
1681 }
1682
1683 /* Unlock == restore saved state */
1684 void SDL_GL_Unlock()
1685 {
1686 #if SDL_VIDEO_OPENGL
1687         lock_count++;
1688         if (lock_count==0)
1689         {
1690                 SDL_VideoDevice *this = current_video;
1691
1692                 this->glPopMatrix();
1693                 this->glMatrixMode(GL_PROJECTION);
1694                 this->glPopMatrix();
1695
1696                 this->glPopClientAttrib();
1697                 this->glPopAttrib();
1698         }
1699 #endif
1700 }
1701
1702
1703 void SDL_Audio_SetCaption(const char *caption);
1704
1705 /*
1706  * Sets/Gets the title and icon text of the display window, if any.
1707  */
1708 void SDL_WM_SetCaption (const char *title, const char *icon)
1709 {
1710         SDL_VideoDevice *video = current_video;
1711         SDL_VideoDevice *this  = current_video;
1712
1713         if ( video ) {
1714                 if ( title ) {
1715                         if ( video->wm_title ) {
1716                                 SDL_free(video->wm_title);
1717                         }
1718                         video->wm_title = SDL_strdup(title);
1719                 }
1720                 if ( icon ) {
1721                         if ( video->wm_icon ) {
1722                                 SDL_free(video->wm_icon);
1723                         }
1724                         video->wm_icon = SDL_strdup(icon);
1725                 }
1726                 if ( (title || icon) && (video->SetCaption != NULL) ) {
1727                         video->SetCaption(this, video->wm_title,video->wm_icon);
1728                 }
1729         }
1730
1731         /* PulseAudio can make use of this information. */
1732         SDL_Audio_SetCaption(title);
1733 }
1734
1735 void SDL_WM_GetCaption (char **title, char **icon)
1736 {
1737         SDL_VideoDevice *video = current_video;
1738
1739         if ( video ) {
1740                 if ( title ) {
1741                         *title = video->wm_title;
1742                 }
1743                 if ( icon ) {
1744                         *icon = video->wm_icon;
1745                 }
1746         }
1747 }
1748
1749 /* Utility function used by SDL_WM_SetIcon();
1750  * flags & 1 for color key, flags & 2 for alpha channel. */
1751 static void CreateMaskFromColorKeyOrAlpha(SDL_Surface *icon, Uint8 *mask, int flags)
1752 {
1753         int x, y;
1754         Uint32 colorkey;
1755 #define SET_MASKBIT(icon, x, y, mask) \
1756         mask[(y*((icon->w+7)/8))+(x/8)] &= ~(0x01<<(7-(x%8)))
1757
1758         colorkey = icon->format->colorkey;
1759         switch (icon->format->BytesPerPixel) {
1760                 case 1: { Uint8 *pixels;
1761                         for ( y=0; y<icon->h; ++y ) {
1762                                 pixels = (Uint8 *)icon->pixels + y*icon->pitch;
1763                                 for ( x=0; x<icon->w; ++x ) {
1764                                         if ( *pixels++ == colorkey ) {
1765                                                 SET_MASKBIT(icon, x, y, mask);
1766                                         }
1767                                 }
1768                         }
1769                 }
1770                 break;
1771
1772                 case 2: { Uint16 *pixels;
1773                         for ( y=0; y<icon->h; ++y ) {
1774                                 pixels = (Uint16 *)icon->pixels +
1775                                                    y*icon->pitch/2;
1776                                 for ( x=0; x<icon->w; ++x ) {
1777                                         if ( (flags & 1) && *pixels == colorkey ) {
1778                                                 SET_MASKBIT(icon, x, y, mask);
1779                                         } else if((flags & 2) && (*pixels & icon->format->Amask) == 0) {
1780                                                 SET_MASKBIT(icon, x, y, mask);
1781                                         }
1782                                         pixels++;
1783                                 }
1784                         }
1785                 }
1786                 break;
1787
1788                 case 4: { Uint32 *pixels;
1789                         for ( y=0; y<icon->h; ++y ) {
1790                                 pixels = (Uint32 *)icon->pixels +
1791                                                    y*icon->pitch/4;
1792                                 for ( x=0; x<icon->w; ++x ) {
1793                                         if ( (flags & 1) && *pixels == colorkey ) {
1794                                                 SET_MASKBIT(icon, x, y, mask);
1795                                         } else if((flags & 2) && (*pixels & icon->format->Amask) == 0) {
1796                                                 SET_MASKBIT(icon, x, y, mask);
1797                                         }
1798                                         pixels++;
1799                                 }
1800                         }
1801                 }
1802                 break;
1803         }
1804 }
1805
1806 /*
1807  * Sets the window manager icon for the display window.
1808  */
1809 void SDL_WM_SetIcon (SDL_Surface *icon, Uint8 *mask)
1810 {
1811         SDL_VideoDevice *video = current_video;
1812         SDL_VideoDevice *this  = current_video;
1813
1814         if ( icon && video->SetIcon ) {
1815                 /* Generate a mask if necessary, and create the icon! */
1816                 if ( mask == NULL ) {
1817                         int mask_len = icon->h*(icon->w+7)/8;
1818                         int flags = 0;
1819                         mask = (Uint8 *)SDL_malloc(mask_len);
1820                         if ( mask == NULL ) {
1821                                 return;
1822                         }
1823                         SDL_memset(mask, ~0, mask_len);
1824                         if ( icon->flags & SDL_SRCCOLORKEY ) flags |= 1;
1825                         if ( icon->flags & SDL_SRCALPHA ) flags |= 2;
1826                         if( flags ) {
1827                                 CreateMaskFromColorKeyOrAlpha(icon, mask, flags);
1828                         }
1829                         video->SetIcon(video, icon, mask);
1830                         SDL_free(mask);
1831                 } else {
1832                         video->SetIcon(this, icon, mask);
1833                 }
1834         }
1835 }
1836
1837 /*
1838  * Grab or ungrab the keyboard and mouse input.
1839  * This function returns the final grab mode after calling the
1840  * driver dependent function.
1841  */
1842 static SDL_GrabMode SDL_WM_GrabInputRaw(SDL_GrabMode mode)
1843 {
1844         SDL_VideoDevice *video = current_video;
1845         SDL_VideoDevice *this  = current_video;
1846
1847         /* Only do something if we have support for grabs */
1848         if ( video->GrabInput == NULL ) {
1849                 return(video->input_grab);
1850         }
1851
1852         /* If the final grab mode if off, only then do we actually grab */
1853 #ifdef DEBUG_GRAB
1854   printf("SDL_WM_GrabInputRaw(%d) ... ", mode);
1855 #endif
1856         if ( mode == SDL_GRAB_OFF ) {
1857                 if ( video->input_grab != SDL_GRAB_OFF ) {
1858                         mode = video->GrabInput(this, mode);
1859                 }
1860         } else {
1861                 if ( video->input_grab == SDL_GRAB_OFF ) {
1862                         mode = video->GrabInput(this, mode);
1863                 }
1864         }
1865         if ( mode != video->input_grab ) {
1866                 video->input_grab = mode;
1867                 if ( video->CheckMouseMode ) {
1868                         video->CheckMouseMode(this);
1869                 }
1870         }
1871 #ifdef DEBUG_GRAB
1872   printf("Final mode %d\n", video->input_grab);
1873 #endif
1874
1875         /* Return the final grab state */
1876         if ( mode >= SDL_GRAB_FULLSCREEN ) {
1877                 mode -= SDL_GRAB_FULLSCREEN;
1878         }
1879         return(mode);
1880 }
1881 SDL_GrabMode SDL_WM_GrabInput(SDL_GrabMode mode)
1882 {
1883         SDL_VideoDevice *video = current_video;
1884
1885         /* If the video isn't initialized yet, we can't do anything */
1886         if ( ! video ) {
1887                 return SDL_GRAB_OFF;
1888         }
1889
1890         /* Return the current mode on query */
1891         if ( mode == SDL_GRAB_QUERY ) {
1892                 mode = video->input_grab;
1893                 if ( mode >= SDL_GRAB_FULLSCREEN ) {
1894                         mode -= SDL_GRAB_FULLSCREEN;
1895                 }
1896                 return(mode);
1897         }
1898
1899 #ifdef DEBUG_GRAB
1900   printf("SDL_WM_GrabInput(%d) ... ", mode);
1901 #endif
1902         /* If the video surface is fullscreen, we always grab */
1903         if ( mode >= SDL_GRAB_FULLSCREEN ) {
1904                 mode -= SDL_GRAB_FULLSCREEN;
1905         }
1906         if ( SDL_VideoSurface && (SDL_VideoSurface->flags & SDL_FULLSCREEN) ) {
1907                 mode += SDL_GRAB_FULLSCREEN;
1908         }
1909         return(SDL_WM_GrabInputRaw(mode));
1910 }
1911 static SDL_GrabMode SDL_WM_GrabInputOff(void)
1912 {
1913         SDL_GrabMode mode;
1914
1915         /* First query the current grab state */
1916         mode = SDL_WM_GrabInput(SDL_GRAB_QUERY);
1917
1918         /* Now explicitly turn off input grab */
1919         SDL_WM_GrabInputRaw(SDL_GRAB_OFF);
1920
1921         /* Return the old state */
1922         return(mode);
1923 }
1924
1925 /*
1926  * Iconify the window in window managed environments.
1927  * A successful iconification will result in an SDL_APPACTIVE loss event.
1928  */
1929 int SDL_WM_IconifyWindow(void)
1930 {
1931         SDL_VideoDevice *video = current_video;
1932         SDL_VideoDevice *this  = current_video;
1933         int retval;
1934
1935         retval = 0;
1936         if ( video->IconifyWindow ) {
1937                 retval = video->IconifyWindow(this);
1938         }
1939         return(retval);
1940 }
1941
1942 /*
1943  * Toggle fullscreen mode
1944  */
1945 int SDL_WM_ToggleFullScreen(SDL_Surface *surface)
1946 {
1947         SDL_VideoDevice *video = current_video;
1948         SDL_VideoDevice *this  = current_video;
1949         int toggled;
1950
1951         toggled = 0;
1952         if ( SDL_PublicSurface && (surface == SDL_PublicSurface) &&
1953              video->ToggleFullScreen ) {
1954                 if ( surface->flags & SDL_FULLSCREEN ) {
1955                         toggled = video->ToggleFullScreen(this, 0);
1956                         if ( toggled ) {
1957                                 SDL_VideoSurface->flags &= ~SDL_FULLSCREEN;
1958                                 SDL_PublicSurface->flags &= ~SDL_FULLSCREEN;
1959                         }
1960                 } else {
1961                         toggled = video->ToggleFullScreen(this, 1);
1962                         if ( toggled ) {
1963                                 SDL_VideoSurface->flags |= SDL_FULLSCREEN;
1964                                 SDL_PublicSurface->flags |= SDL_FULLSCREEN;
1965                         }
1966                 }
1967                 /* Double-check the grab state inside SDL_WM_GrabInput() */
1968                 if ( toggled ) {
1969                         SDL_WM_GrabInput(video->input_grab);
1970                 }
1971         }
1972         return(toggled);
1973 }
1974
1975 /*
1976  * Get some platform dependent window manager information
1977  */
1978 int SDL_GetWMInfo (SDL_SysWMinfo *info)
1979 {
1980         SDL_VideoDevice *video = current_video;
1981         SDL_VideoDevice *this  = current_video;
1982
1983         if ( video && video->GetWMInfo ) {
1984                 return(video->GetWMInfo(this, info));
1985         } else {
1986                 return(0);
1987         }
1988 }