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