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