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