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