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