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