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