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