src/video/SDL_video.c
author Sam Lantinga <slouken@libsdl.org>
Sat, 21 Aug 2004 13:10:58 +0000
changeset 944 cdea7cbc3e23
parent 916 46916168361d
child 989 475166d13b44
permissions -rw-r--r--
Date: Wed, 28 Jul 2004 14:56:57 +0800
From: Aaron Perez
Subject: [SDL] Fwd: SDL not checking malloc returning NULL

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