src/video/SDL_video.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 04 Aug 2003 00:52:42 +0000
changeset 663 8bedd6d61642
parent 662 66c02f83f5bf
child 769 b8d311d90021
permissions -rw-r--r--
Date: Sat, 2 Aug 2003 16:22:51 +0300
From: "Mike Gorchak"
Subject: New patches for QNX6

Here my patches for the SDL/QNX:

QNXSDL.diff - diff to non-QNX related sources:

- updated BUGS file, I think QNX6 is now will be officially supported
- configure.in - added shared library support for QNX, and removed dependency between the ALSA and QNX6.
- SDL_audio.c - added QNX NTO sound bootstrap insted of ALSA's.
- SDL_sysaudio.h - the same.
- SDL_nto_audio.c - the same.
- SDL_video.c - right now, QNX doesn't offer any method to obtain pointers to the OpenGL functions by function name, so they must be hardcoded in library, otherwise OpenGL will not be supported.
- testsprite.c - fixed: do not draw vertical red line if we are in non-double-buffered mode.

sdlqnxph.tar.gz - archive of the ./src/video/photon/* . Too many changes in code to make diffs :) :

+ Added stub for support hide/unhide window event
+ Added full YUV overlays support.
+ Added window maximize support.
+ Added mouse wheel events.
+ Added support for some specific key codes in Unicode mode (like ESC).
+ Added more checks to the all memory allocation code.
+ Added SDL_DOUBLEBUF support in all fullscreen modes.
+ Added fallback to window mode, if desired fullscreen mode is not supported.
+ Added stub support for the GL_LoadLibrary and GL_GetProcAddress functions.
+ Added resizable window support without caption.
! Fixed bug in the Ph_EV_EXPOSE event handler, when rectangles to update is 0 and when width or height of the rectangle is 0.
! Fixed bug in the event handler code. Events has not been passed to the window widget handler.
! Fixed codes for Win keys (Super/Hyper/Menu).
! Fixed memory leak, when deallocation palette.
! Fixed palette emulation code bugs.
! Fixed fullscreen and hwsurface handling.
! Fixed CLOSE button bug. First event was passed to the handler, but second terminated the application. Now all events passed to the application correctly.
- Removed all printfs in code, now SDL_SetError used instead of them.
- Disabled ToggleFullScreen function.

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