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