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