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