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