src/video/SDL_video.c
author Ryan C. Gordon <icculus@icculus.org>
Thu, 08 Sep 2005 18:26:56 +0000
changeset 1141 fe1fa56dcbc6
parent 1140 af8b0f9ac2f4
child 1187 19d8949b4584
permissions -rw-r--r--
If couldn't load a needed GL symbol in SDL_SetVideoMode(), report it more
specifically. Patch by Anders F Bj´┐Żrklund <afb@algonet.se>.

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