src/video/x11/SDL_x11video.c
author Ryan C. Gordon
Thu, 26 Feb 2004 15:12:51 +0000
changeset 860 2bac79e27868
parent 769 b8d311d90021
child 867 5c7859610bc4
permissions -rw-r--r--
Create a 2D window and then manually focus a different window on your desktop,
call SDL_PumpEvents() so the X11 driver sets its this->hidden->switch_waiting,
then set a fullscreen OpenGL window (which makes the X11 driver tear down and
create a new window instead of just resizing the existing one), poll for
events, and the newly-created window will think it needs to pop back to a
window. Fixed by resetting switch_waiting to zero if X11_CreateWindow() had
to tear down a previous window.
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997-2004 Sam Lantinga
     4 
     5     This library is free software; you can redistribute it and/or
     6     modify it under the terms of the GNU Library General Public
     7     License as published by the Free Software Foundation; either
     8     version 2 of the License, or (at your option) any later version.
     9 
    10     This library is distributed in the hope that it will be useful,
    11     but WITHOUT ANY WARRANTY; without even the implied warranty of
    12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    13     Library General Public License for more details.
    14 
    15     You should have received a copy of the GNU Library General Public
    16     License along with this library; if not, write to the Free
    17     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    18 
    19     Sam Lantinga
    20     slouken@libsdl.org
    21 */
    22 
    23 #ifdef SAVE_RCSID
    24 static char rcsid =
    25  "@(#) $Id$";
    26 #endif
    27 
    28 /* X11 based SDL video driver implementation.
    29    Note:  This implementation does not currently need X11 thread locking,
    30           since the event thread uses a separate X connection and any
    31           additional locking necessary is handled internally.  However,
    32           if full locking is neccessary, take a look at XInitThreads().
    33 */
    34 
    35 #include <stdlib.h>
    36 #include <stdio.h>
    37 #include <unistd.h>
    38 #include <string.h>
    39 #include <sys/ioctl.h>
    40 #ifdef MTRR_SUPPORT
    41 #include <asm/mtrr.h>
    42 #include <sys/fcntl.h>
    43 #endif
    44 
    45 #ifdef HAVE_ALLOCA_H
    46 #include <alloca.h>
    47 #endif
    48 
    49 #ifdef HAVE_ALLOCA
    50 #define ALLOCA(n) ((void*)alloca(n))
    51 #define FREEA(p)
    52 #else
    53 #define ALLOCA(n) malloc(n)
    54 #define FREEA(p) free(p)
    55 #endif
    56 
    57 #include "SDL.h"
    58 #include "SDL_error.h"
    59 #include "SDL_timer.h"
    60 #include "SDL_thread.h"
    61 #include "SDL_video.h"
    62 #include "SDL_mouse.h"
    63 #include "SDL_endian.h"
    64 #include "SDL_sysvideo.h"
    65 #include "SDL_pixels_c.h"
    66 #include "SDL_events_c.h"
    67 #include "SDL_x11video.h"
    68 #include "SDL_x11wm_c.h"
    69 #include "SDL_x11mouse_c.h"
    70 #include "SDL_x11events_c.h"
    71 #include "SDL_x11modes_c.h"
    72 #include "SDL_x11image_c.h"
    73 #include "SDL_x11yuv_c.h"
    74 #include "SDL_x11gl_c.h"
    75 #include "SDL_x11gamma_c.h"
    76 #include "blank_cursor.h"
    77 
    78 /* Initialization/Query functions */
    79 static int X11_VideoInit(_THIS, SDL_PixelFormat *vformat);
    80 static SDL_Surface *X11_SetVideoMode(_THIS, SDL_Surface *current, int width, int height, int bpp, Uint32 flags);
    81 static int X11_ToggleFullScreen(_THIS, int on);
    82 static void X11_UpdateMouse(_THIS);
    83 static int X11_SetColors(_THIS, int firstcolor, int ncolors,
    84 			 SDL_Color *colors);
    85 static int X11_SetGammaRamp(_THIS, Uint16 *ramp);
    86 static void X11_VideoQuit(_THIS);
    87 
    88 /* X11 driver bootstrap functions */
    89 
    90 static int X11_Available(void)
    91 {
    92 	Display *display;
    93 
    94 	display = XOpenDisplay(NULL);
    95 	if ( display != NULL ) {
    96 		XCloseDisplay(display);
    97 	}
    98 	return(display != NULL);
    99 }
   100 
   101 static void X11_DeleteDevice(SDL_VideoDevice *device)
   102 {
   103 	if ( device ) {
   104 		if ( device->hidden ) {
   105 			free(device->hidden);
   106 		}
   107 		if ( device->gl_data ) {
   108 			free(device->gl_data);
   109 		}
   110 		free(device);
   111 	}
   112 }
   113 
   114 static SDL_VideoDevice *X11_CreateDevice(int devindex)
   115 {
   116 	SDL_VideoDevice *device;
   117 
   118 	/* Initialize all variables that we clean on shutdown */
   119 	device = (SDL_VideoDevice *)malloc(sizeof(SDL_VideoDevice));
   120 	if ( device ) {
   121 		memset(device, 0, (sizeof *device));
   122 		device->hidden = (struct SDL_PrivateVideoData *)
   123 				malloc((sizeof *device->hidden));
   124 		device->gl_data = (struct SDL_PrivateGLData *)
   125 				malloc((sizeof *device->gl_data));
   126 	}
   127 	if ( (device == NULL) || (device->hidden == NULL) ||
   128 	                         (device->gl_data == NULL) ) {
   129 		SDL_OutOfMemory();
   130 		X11_DeleteDevice(device);
   131 		return(0);
   132 	}
   133 	memset(device->hidden, 0, (sizeof *device->hidden));
   134 	memset(device->gl_data, 0, (sizeof *device->gl_data));
   135 
   136 	/* Set the driver flags */
   137 	device->handles_any_size = 1;
   138 
   139 	/* Set the function pointers */
   140 	device->VideoInit = X11_VideoInit;
   141 	device->ListModes = X11_ListModes;
   142 	device->SetVideoMode = X11_SetVideoMode;
   143 	device->ToggleFullScreen = X11_ToggleFullScreen;
   144 	device->UpdateMouse = X11_UpdateMouse;
   145 #ifdef XFREE86_XV
   146 	device->CreateYUVOverlay = X11_CreateYUVOverlay;
   147 #endif
   148 	device->SetColors = X11_SetColors;
   149 	device->UpdateRects = NULL;
   150 	device->VideoQuit = X11_VideoQuit;
   151 	device->AllocHWSurface = X11_AllocHWSurface;
   152 	device->CheckHWBlit = NULL;
   153 	device->FillHWRect = NULL;
   154 	device->SetHWColorKey = NULL;
   155 	device->SetHWAlpha = NULL;
   156 	device->LockHWSurface = X11_LockHWSurface;
   157 	device->UnlockHWSurface = X11_UnlockHWSurface;
   158 	device->FlipHWSurface = X11_FlipHWSurface;
   159 	device->FreeHWSurface = X11_FreeHWSurface;
   160 	device->SetGamma = X11_SetVidModeGamma;
   161 	device->GetGamma = X11_GetVidModeGamma;
   162 	device->SetGammaRamp = X11_SetGammaRamp;
   163 	device->GetGammaRamp = NULL;
   164 #ifdef HAVE_OPENGL
   165 	device->GL_LoadLibrary = X11_GL_LoadLibrary;
   166 	device->GL_GetProcAddress = X11_GL_GetProcAddress;
   167 	device->GL_GetAttribute = X11_GL_GetAttribute;
   168 	device->GL_MakeCurrent = X11_GL_MakeCurrent;
   169 	device->GL_SwapBuffers = X11_GL_SwapBuffers;
   170 #endif
   171 	device->SetCaption = X11_SetCaption;
   172 	device->SetIcon = X11_SetIcon;
   173 	device->IconifyWindow = X11_IconifyWindow;
   174 	device->GrabInput = X11_GrabInput;
   175 	device->GetWMInfo = X11_GetWMInfo;
   176 	device->FreeWMCursor = X11_FreeWMCursor;
   177 	device->CreateWMCursor = X11_CreateWMCursor;
   178 	device->ShowWMCursor = X11_ShowWMCursor;
   179 	device->WarpWMCursor = X11_WarpWMCursor;
   180 	device->CheckMouseMode = X11_CheckMouseMode;
   181 	device->InitOSKeymap = X11_InitOSKeymap;
   182 	device->PumpEvents = X11_PumpEvents;
   183 
   184 	device->free = X11_DeleteDevice;
   185 
   186 	return device;
   187 }
   188 
   189 VideoBootStrap X11_bootstrap = {
   190 	"x11", "X Window System",
   191 	X11_Available, X11_CreateDevice
   192 };
   193 
   194 /* Shared memory information */
   195 extern int XShmQueryExtension(Display *dpy);	/* Not in X11 headers */
   196 
   197 /* Normal X11 error handler routine */
   198 static int (*X_handler)(Display *, XErrorEvent *) = NULL;
   199 static int x_errhandler(Display *d, XErrorEvent *e)
   200 {
   201 #ifdef XFREE86_VM
   202 	extern int vm_error;
   203 #endif
   204 #ifdef XFREE86_DGAMOUSE
   205 	extern int dga_error;
   206 #endif
   207 
   208 #ifdef XFREE86_VM
   209 	/* VidMode errors are non-fatal. :) */
   210 	/* Are the errors offset by one from the error base?
   211 	   e.g. the error base is 143, the code is 148, and the
   212 	        actual error is XF86VidModeExtensionDisabled (4) ?
   213 	 */
   214         if ( (vm_error >= 0) &&
   215 	     (((e->error_code == BadRequest)&&(e->request_code == vm_error)) ||
   216 	      ((e->error_code > vm_error) &&
   217 	       (e->error_code <= (vm_error+XF86VidModeNumberErrors)))) ) {
   218 #ifdef XFREE86_DEBUG
   219 { char errmsg[1024];
   220   XGetErrorText(d, e->error_code, errmsg, sizeof(errmsg));
   221 printf("VidMode error: %s\n", errmsg);
   222 }
   223 #endif
   224         	return(0);
   225         }
   226 #endif /* XFREE86_VM */
   227 
   228 #ifdef XFREE86_DGAMOUSE
   229 	/* DGA errors can be non-fatal. :) */
   230         if ( (dga_error >= 0) &&
   231 	     ((e->error_code > dga_error) &&
   232 	      (e->error_code <= (dga_error+XF86DGANumberErrors))) ) {
   233 #ifdef XFREE86_DEBUG
   234 { char errmsg[1024];
   235   XGetErrorText(d, e->error_code, errmsg, sizeof(errmsg));
   236 printf("DGA error: %s\n", errmsg);
   237 }
   238 #endif
   239         	return(0);
   240         }
   241 #endif /* XFREE86_DGAMOUSE */
   242 
   243 	return(X_handler(d,e));
   244 }
   245 
   246 /* X11 I/O error handler routine */
   247 static int (*XIO_handler)(Display *) = NULL;
   248 static int xio_errhandler(Display *d)
   249 {
   250 	/* Ack!  Lost X11 connection! */
   251 
   252 	/* We will crash if we try to clean up our display */
   253 	if ( current_video->hidden->Ximage ) {
   254 		SDL_VideoSurface->pixels = NULL;
   255 	}
   256 	current_video->hidden->X11_Display = NULL;
   257 
   258 	/* Continue with the standard X11 error handler */
   259 	return(XIO_handler(d));
   260 }
   261 
   262 /* Create auxiliary (toplevel) windows with the current visual */
   263 static void create_aux_windows(_THIS)
   264 {
   265     XSetWindowAttributes xattr;
   266     XWMHints *hints;
   267     XTextProperty titleprop, iconprop;
   268     int def_vis = (SDL_Visual == DefaultVisual(SDL_Display, SDL_Screen));
   269 
   270     /* Don't create any extra windows if we are being managed */
   271     if ( SDL_windowid ) {
   272 	FSwindow = 0;
   273 	WMwindow = strtol(SDL_windowid, NULL, 0);
   274         return;
   275     }
   276 
   277     if(FSwindow)
   278 	XDestroyWindow(SDL_Display, FSwindow);
   279 
   280     xattr.override_redirect = True;
   281     xattr.background_pixel = def_vis ? BlackPixel(SDL_Display, SDL_Screen) : 0;
   282     xattr.border_pixel = 0;
   283     xattr.colormap = SDL_XColorMap;
   284 
   285     FSwindow = XCreateWindow(SDL_Display, SDL_Root,
   286                              xinerama_x, xinerama_y, 32, 32, 0,
   287 			     this->hidden->depth, InputOutput, SDL_Visual,
   288 			     CWOverrideRedirect | CWBackPixel | CWBorderPixel
   289 			     | CWColormap,
   290 			     &xattr);
   291 
   292     XSelectInput(SDL_Display, FSwindow, StructureNotifyMask);
   293 
   294     /* Tell KDE to keep the fullscreen window on top */
   295     {
   296 	XEvent ev;
   297 	long mask;
   298 
   299 	memset(&ev, 0, sizeof(ev));
   300 	ev.xclient.type = ClientMessage;
   301 	ev.xclient.window = SDL_Root;
   302 	ev.xclient.message_type = XInternAtom(SDL_Display,
   303 					      "KWM_KEEP_ON_TOP", False);
   304 	ev.xclient.format = 32;
   305 	ev.xclient.data.l[0] = FSwindow;
   306 	ev.xclient.data.l[1] = CurrentTime;
   307 	mask = SubstructureRedirectMask;
   308 	XSendEvent(SDL_Display, SDL_Root, False, mask, &ev);
   309     }
   310 
   311     hints = NULL;
   312     titleprop.value = iconprop.value = NULL;
   313     if(WMwindow) {
   314 	/* All window attributes must survive the recreation */
   315 	hints = XGetWMHints(SDL_Display, WMwindow);
   316 	XGetWMName(SDL_Display, WMwindow, &titleprop);
   317 	XGetWMIconName(SDL_Display, WMwindow, &iconprop);
   318 	XDestroyWindow(SDL_Display, WMwindow);
   319     }
   320 
   321     /* Create the window for windowed management */
   322     /* (reusing the xattr structure above) */
   323     WMwindow = XCreateWindow(SDL_Display, SDL_Root, 0, 0, 32, 32, 0,
   324 			     this->hidden->depth, InputOutput, SDL_Visual,
   325 			     CWBackPixel | CWBorderPixel | CWColormap,
   326 			     &xattr);
   327 
   328     /* Set the input hints so we get keyboard input */
   329     if(!hints) {
   330 	hints = XAllocWMHints();
   331 	hints->input = True;
   332 	hints->flags = InputHint;
   333     }
   334     XSetWMHints(SDL_Display, WMwindow, hints);
   335     XFree(hints);
   336     if(titleprop.value) {
   337 	XSetWMName(SDL_Display, WMwindow, &titleprop);
   338 	XFree(titleprop.value);
   339     }
   340     if(iconprop.value) {
   341 	XSetWMIconName(SDL_Display, WMwindow, &iconprop);
   342 	XFree(iconprop.value);
   343     }
   344 
   345     XSelectInput(SDL_Display, WMwindow,
   346 		 FocusChangeMask | KeyPressMask | KeyReleaseMask
   347 		 | PropertyChangeMask | StructureNotifyMask | KeymapStateMask);
   348 
   349     /* Set the class hints so we can get an icon (AfterStep) */
   350     {
   351 	XClassHint *classhints;
   352 	classhints = XAllocClassHint();
   353 	if(classhints != NULL) {
   354             char *classname = getenv("SDL_VIDEO_X11_WMCLASS");
   355             if ( ! classname ) {
   356                 classname = "SDL_App";
   357             }
   358 	    classhints->res_name = classname;
   359 	    classhints->res_class = classname;
   360 	    XSetClassHint(SDL_Display, WMwindow, classhints);
   361 	    XFree(classhints);
   362 	}
   363     }
   364 
   365     /* Allow the window to be deleted by the window manager */
   366     WM_DELETE_WINDOW = XInternAtom(SDL_Display, "WM_DELETE_WINDOW", False);
   367     XSetWMProtocols(SDL_Display, WMwindow, &WM_DELETE_WINDOW, 1);
   368 }
   369 
   370 static int X11_VideoInit(_THIS, SDL_PixelFormat *vformat)
   371 {
   372 	char *display;
   373 	int i;
   374 
   375 	/* Open the X11 display */
   376 	display = NULL;		/* Get it from DISPLAY environment variable */
   377 
   378 	if ( (strncmp(XDisplayName(display), ":", 1) == 0) ||
   379 	     (strncmp(XDisplayName(display), "unix:", 5) == 0) ) {
   380 		local_X11 = 1;
   381 	} else {
   382 		local_X11 = 0;
   383 	}
   384 	SDL_Display = XOpenDisplay(display);
   385 	if ( SDL_Display == NULL ) {
   386 		SDL_SetError("Couldn't open X11 display");
   387 		return(-1);
   388 	}
   389 #ifdef X11_DEBUG
   390 	XSynchronize(SDL_Display, True);
   391 #endif
   392 
   393 	/* Create an alternate X display for graphics updates -- allows us
   394 	   to do graphics updates in a separate thread from event handling.
   395 	   Thread-safe X11 doesn't seem to exist.
   396 	 */
   397 	GFX_Display = XOpenDisplay(display);
   398 	if ( GFX_Display == NULL ) {
   399 		SDL_SetError("Couldn't open X11 display");
   400 		return(-1);
   401 	}
   402 
   403 	/* Set the normal X error handler */
   404 	X_handler = XSetErrorHandler(x_errhandler);
   405 
   406 	/* Set the error handler if we lose the X display */
   407 	XIO_handler = XSetIOErrorHandler(xio_errhandler);
   408 
   409 	/* use default screen (from $DISPLAY) */
   410 	SDL_Screen = DefaultScreen(SDL_Display);
   411 
   412 #ifndef NO_SHARED_MEMORY
   413 	/* Check for MIT shared memory extension */
   414 	use_mitshm = 0;
   415 	if ( local_X11 ) {
   416 		use_mitshm = XShmQueryExtension(SDL_Display);
   417 	}
   418 #endif /* NO_SHARED_MEMORY */
   419 
   420 	/* Get the available video modes */
   421 	if(X11_GetVideoModes(this) < 0)
   422 	    return -1;
   423 
   424 	/* Determine the default screen depth:
   425 	   Use the default visual (or at least one with the same depth) */
   426 	SDL_DisplayColormap = DefaultColormap(SDL_Display, SDL_Screen);
   427 	for(i = 0; i < this->hidden->nvisuals; i++)
   428 	    if(this->hidden->visuals[i].depth == DefaultDepth(SDL_Display,
   429 							      SDL_Screen))
   430 		break;
   431 	if(i == this->hidden->nvisuals) {
   432 	    /* default visual was useless, take the deepest one instead */
   433 	    i = 0;
   434 	}
   435 	SDL_Visual = this->hidden->visuals[i].visual;
   436 	if ( SDL_Visual == DefaultVisual(SDL_Display, SDL_Screen) ) {
   437 	    SDL_XColorMap = SDL_DisplayColormap;
   438 	} else {
   439 	    SDL_XColorMap = XCreateColormap(SDL_Display, SDL_Root,
   440 					    SDL_Visual, AllocNone);
   441 	}
   442 	this->hidden->depth = this->hidden->visuals[i].depth;
   443 	vformat->BitsPerPixel = this->hidden->visuals[i].bpp;
   444 	if ( vformat->BitsPerPixel > 8 ) {
   445 		vformat->Rmask = SDL_Visual->red_mask;
   446 	  	vformat->Gmask = SDL_Visual->green_mask;
   447 	  	vformat->Bmask = SDL_Visual->blue_mask;
   448 	}
   449 	X11_SaveVidModeGamma(this);
   450 
   451 	/* See if we have been passed a window to use */
   452 	SDL_windowid = getenv("SDL_WINDOWID");
   453 
   454 	/* Create the fullscreen and managed windows */
   455 	create_aux_windows(this);
   456 
   457 	/* Create the blank cursor */
   458 	SDL_BlankCursor = this->CreateWMCursor(this, blank_cdata, blank_cmask,
   459 					BLANK_CWIDTH, BLANK_CHEIGHT,
   460 						BLANK_CHOTX, BLANK_CHOTY);
   461 
   462 	/* Fill in some window manager capabilities */
   463 	this->info.wm_available = 1;
   464 
   465 	/* We're done! */
   466 	XFlush(SDL_Display);
   467 	return(0);
   468 }
   469 
   470 static void X11_DestroyWindow(_THIS, SDL_Surface *screen)
   471 {
   472 	/* Clean up OpenGL */
   473 	if ( screen ) {
   474 		screen->flags &= ~(SDL_OPENGL|SDL_OPENGLBLIT);
   475 	}
   476 	X11_GL_Shutdown(this);
   477 
   478 	if ( ! SDL_windowid ) {
   479 		/* Hide the managed window */
   480 		if ( WMwindow ) {
   481 			XUnmapWindow(SDL_Display, WMwindow);
   482 		}
   483 		if ( screen && (screen->flags & SDL_FULLSCREEN) ) {
   484 			screen->flags &= ~SDL_FULLSCREEN;
   485 			X11_LeaveFullScreen(this);
   486 		}
   487 
   488 		/* Destroy the output window */
   489 		if ( SDL_Window ) {
   490 			XDestroyWindow(SDL_Display, SDL_Window);
   491 		}
   492 
   493 		/* Free the colormap entries */
   494 		if ( SDL_XPixels ) {
   495 			int numcolors;
   496 			unsigned long pixel;
   497 			numcolors = SDL_Visual->map_entries;
   498 			for ( pixel=0; pixel<numcolors; ++pixel ) {
   499 				while ( SDL_XPixels[pixel] > 0 ) {
   500 					XFreeColors(GFX_Display,
   501 						SDL_DisplayColormap,&pixel,1,0);
   502 					--SDL_XPixels[pixel];
   503 				}
   504 			}
   505 			free(SDL_XPixels);
   506 			SDL_XPixels = NULL;
   507 		} 
   508 
   509 		/* Free the graphics context */
   510 		if ( SDL_GC ) {
   511 			XFreeGC(SDL_Display, SDL_GC);
   512 			SDL_GC = 0;
   513 		}
   514 	}
   515 }
   516 
   517 static SDL_bool X11_WindowPosition(_THIS, int *x, int *y, int w, int h)
   518 {
   519 	const char *window = getenv("SDL_VIDEO_WINDOW_POS");
   520 	const char *center = getenv("SDL_VIDEO_CENTERED");
   521 	if ( window ) {
   522 		if ( sscanf(window, "%d,%d", x, y) == 2 ) {
   523 			return SDL_TRUE;
   524 		}
   525 		if ( strcmp(window, "center") == 0 ) {
   526 			center = window;
   527 		}
   528 	}
   529 	if ( center ) {
   530 		*x = (DisplayWidth(SDL_Display, SDL_Screen) - w)/2;
   531 		*y = (DisplayHeight(SDL_Display, SDL_Screen) - h)/2;
   532 		return SDL_TRUE;
   533 	}
   534 	return SDL_FALSE;
   535 }
   536 
   537 static void X11_SetSizeHints(_THIS, int w, int h, Uint32 flags)
   538 {
   539 	XSizeHints *hints;
   540 
   541 	hints = XAllocSizeHints();
   542 	if ( hints ) {
   543 		if ( flags & SDL_RESIZABLE ) {
   544 			hints->min_width = 32;
   545 			hints->min_height = 32;
   546 			hints->max_height = 4096;
   547 			hints->max_width = 4096;
   548 		} else {
   549 			hints->min_width = hints->max_width = w;
   550 			hints->min_height = hints->max_height = h;
   551 		}
   552 		hints->flags = PMaxSize | PMinSize;
   553 		if ( flags & SDL_FULLSCREEN ) {
   554 			hints->x = 0;
   555 			hints->y = 0;
   556 			hints->flags |= USPosition;
   557 		} else
   558 		/* Center it, if desired */
   559 		if ( X11_WindowPosition(this, &hints->x, &hints->y, w, h) ) {
   560 			hints->flags |= USPosition;
   561 			XMoveWindow(SDL_Display, WMwindow, hints->x, hints->y);
   562 
   563 			/* Flush the resize event so we don't catch it later */
   564 			XSync(SDL_Display, True);
   565 		}
   566 		XSetWMNormalHints(SDL_Display, WMwindow, hints);
   567 		XFree(hints);
   568 	}
   569 
   570 	/* Respect the window caption style */
   571 	if ( flags & SDL_NOFRAME ) {
   572 		SDL_bool set;
   573 		Atom WM_HINTS;
   574 
   575 		/* We haven't modified the window manager hints yet */
   576 		set = SDL_FALSE;
   577 
   578 		/* First try to set MWM hints */
   579 		WM_HINTS = XInternAtom(SDL_Display, "_MOTIF_WM_HINTS", True);
   580 		if ( WM_HINTS != None ) {
   581 			/* Hints used by Motif compliant window managers */
   582 			struct {
   583 				unsigned long flags;
   584 				unsigned long functions;
   585 				unsigned long decorations;
   586 				long input_mode;
   587 				unsigned long status;
   588 			} MWMHints = { (1L << 1), 0, 0, 0, 0 };
   589 
   590 			XChangeProperty(SDL_Display, WMwindow,
   591 			                WM_HINTS, WM_HINTS, 32,
   592 			                PropModeReplace,
   593 					(unsigned char *)&MWMHints,
   594 					sizeof(MWMHints)/sizeof(long));
   595 			set = SDL_TRUE;
   596 		}
   597 		/* Now try to set KWM hints */
   598 		WM_HINTS = XInternAtom(SDL_Display, "KWM_WIN_DECORATION", True);
   599 		if ( WM_HINTS != None ) {
   600 			long KWMHints = 0;
   601 
   602 			XChangeProperty(SDL_Display, WMwindow,
   603 			                WM_HINTS, WM_HINTS, 32,
   604 			                PropModeReplace,
   605 					(unsigned char *)&KWMHints,
   606 					sizeof(KWMHints)/sizeof(long));
   607 			set = SDL_TRUE;
   608 		}
   609 		/* Now try to set GNOME hints */
   610 		WM_HINTS = XInternAtom(SDL_Display, "_WIN_HINTS", True);
   611 		if ( WM_HINTS != None ) {
   612 			long GNOMEHints = 0;
   613 
   614 			XChangeProperty(SDL_Display, WMwindow,
   615 			                WM_HINTS, WM_HINTS, 32,
   616 			                PropModeReplace,
   617 					(unsigned char *)&GNOMEHints,
   618 					sizeof(GNOMEHints)/sizeof(long));
   619 			set = SDL_TRUE;
   620 		}
   621 		/* Finally set the transient hints if necessary */
   622 		if ( ! set ) {
   623 			XSetTransientForHint(SDL_Display, WMwindow, SDL_Root);
   624 		}
   625 	} else {
   626 		SDL_bool set;
   627 		Atom WM_HINTS;
   628 
   629 		/* We haven't modified the window manager hints yet */
   630 		set = SDL_FALSE;
   631 
   632 		/* First try to unset MWM hints */
   633 		WM_HINTS = XInternAtom(SDL_Display, "_MOTIF_WM_HINTS", True);
   634 		if ( WM_HINTS != None ) {
   635 			XDeleteProperty(SDL_Display, WMwindow, WM_HINTS);
   636 			set = SDL_TRUE;
   637 		}
   638 		/* Now try to unset KWM hints */
   639 		WM_HINTS = XInternAtom(SDL_Display, "KWM_WIN_DECORATION", True);
   640 		if ( WM_HINTS != None ) {
   641 			XDeleteProperty(SDL_Display, WMwindow, WM_HINTS);
   642 			set = SDL_TRUE;
   643 		}
   644 		/* Now try to unset GNOME hints */
   645 		WM_HINTS = XInternAtom(SDL_Display, "_WIN_HINTS", True);
   646 		if ( WM_HINTS != None ) {
   647 			XDeleteProperty(SDL_Display, WMwindow, WM_HINTS);
   648 			set = SDL_TRUE;
   649 		}
   650 		/* Finally unset the transient hints if necessary */
   651 		if ( ! set ) {
   652 			/* NOTE: Does this work? */
   653 			XSetTransientForHint(SDL_Display, WMwindow, None);
   654 		}
   655 	}
   656 }
   657 
   658 static int X11_CreateWindow(_THIS, SDL_Surface *screen,
   659 			    int w, int h, int bpp, Uint32 flags)
   660 {
   661 	int i, depth;
   662 	Visual *vis;
   663 	int vis_change;
   664 
   665 	/* If a window is already present, destroy it and start fresh */
   666 	if ( SDL_Window ) {
   667 		X11_DestroyWindow(this, screen);
   668 		switch_waiting = 0; /* Prevent jump back to now-meaningless state. */
   669 	}
   670 
   671 	/* See if we have been given a window id */
   672 	if ( SDL_windowid ) {
   673 		SDL_Window = strtol(SDL_windowid, NULL, 0);
   674 	} else {
   675 		SDL_Window = 0;
   676 	}
   677 
   678 	/* find out which visual we are going to use */
   679 	if ( flags & SDL_OPENGL ) {
   680 		XVisualInfo *vi;
   681 
   682 		vi = X11_GL_GetVisual(this);
   683 		if( !vi ) {
   684 			return -1;
   685 		}
   686 		vis = vi->visual;
   687 		depth = vi->depth;
   688 	} else if ( SDL_windowid ) {
   689 		XWindowAttributes a;
   690 
   691 		XGetWindowAttributes(SDL_Display, SDL_Window, &a);
   692 		vis = a.visual;
   693 		depth = a.depth;
   694 	} else {
   695 		for ( i = 0; i < this->hidden->nvisuals; i++ ) {
   696 			if ( this->hidden->visuals[i].bpp == bpp )
   697 				break;
   698 		}
   699 		if ( i == this->hidden->nvisuals ) {
   700 			SDL_SetError("No matching visual for requested depth");
   701 			return -1;	/* should never happen */
   702 		}
   703 		vis = this->hidden->visuals[i].visual;
   704 		depth = this->hidden->visuals[i].depth;
   705 	}
   706 #ifdef X11_DEBUG
   707         printf("Choosing %s visual at %d bpp - %d colormap entries\n", vis->class == PseudoColor ? "PseudoColor" : (vis->class == TrueColor ? "TrueColor" : (vis->class == DirectColor ? "DirectColor" : "Unknown")), depth, vis->map_entries);
   708 #endif
   709 	vis_change = (vis != SDL_Visual);
   710 	SDL_Visual = vis;
   711 	this->hidden->depth = depth;
   712 
   713 	/* Allocate the new pixel format for this video mode */
   714 	if ( ! SDL_ReallocFormat(screen, bpp,
   715 			vis->red_mask, vis->green_mask, vis->blue_mask, 0) )
   716 		return -1;
   717 
   718 	/* Create the appropriate colormap */
   719 	if ( SDL_XColorMap != SDL_DisplayColormap ) {
   720 		XFreeColormap(SDL_Display, SDL_XColorMap);
   721 	}
   722 	if ( SDL_Visual->class == PseudoColor ) {
   723 	    int ncolors;
   724 
   725 	    /* Allocate the pixel flags */
   726 	    ncolors = SDL_Visual->map_entries;
   727 	    SDL_XPixels = malloc(ncolors * sizeof(int));
   728 	    if(SDL_XPixels == NULL) {
   729 		SDL_OutOfMemory();
   730 		return -1;
   731 	    }
   732 	    memset(SDL_XPixels, 0, ncolors * sizeof(*SDL_XPixels));
   733 
   734 	    /* always allocate a private colormap on non-default visuals */
   735 	    if ( SDL_Visual != DefaultVisual(SDL_Display, SDL_Screen) ) {
   736 		flags |= SDL_HWPALETTE;
   737 	    }
   738 	    if ( flags & SDL_HWPALETTE ) {
   739 		screen->flags |= SDL_HWPALETTE;
   740 		SDL_XColorMap = XCreateColormap(SDL_Display, SDL_Root,
   741 		                                SDL_Visual, AllocAll);
   742 	    } else {
   743 		SDL_XColorMap = SDL_DisplayColormap;
   744 	    }
   745 	} else if ( SDL_Visual->class == DirectColor ) {
   746 
   747 	    /* Create a colormap which we can manipulate for gamma */
   748 	    SDL_XColorMap = XCreateColormap(SDL_Display, SDL_Root,
   749 		                            SDL_Visual, AllocAll);
   750             XSync(SDL_Display, False);
   751 
   752 	    /* Initialize the colormap to the identity mapping */
   753 	    SDL_GetGammaRamp(0, 0, 0);
   754 	    this->screen = screen;
   755 	    X11_SetGammaRamp(this, this->gamma);
   756 	    this->screen = NULL;
   757 	} else {
   758 	    /* Create a read-only colormap for our window */
   759 	    SDL_XColorMap = XCreateColormap(SDL_Display, SDL_Root,
   760 	                                    SDL_Visual, AllocNone);
   761 	}
   762 
   763 	/* Recreate the auxiliary windows, if needed (required for GL) */
   764 	if ( vis_change )
   765 	    create_aux_windows(this);
   766 
   767 	if(screen->flags & SDL_HWPALETTE) {
   768 	    /* Since the full-screen window might have got a nonzero background
   769 	       colour (0 is white on some displays), we should reset the
   770 	       background to 0 here since that is what the user expects
   771 	       with a private colormap */
   772 	    XSetWindowBackground(SDL_Display, FSwindow, 0);
   773 	    XClearWindow(SDL_Display, FSwindow);
   774 	}
   775 
   776 	/* resize the (possibly new) window manager window */
   777 	if( !SDL_windowid ) {
   778 	        X11_SetSizeHints(this, w, h, flags);
   779 		current_w = w;
   780 		current_h = h;
   781 		XResizeWindow(SDL_Display, WMwindow, w, h);
   782 	}
   783 
   784 	/* Create (or use) the X11 display window */
   785 	if ( !SDL_windowid ) {
   786 		if ( flags & SDL_OPENGL ) {
   787 			if ( X11_GL_CreateWindow(this, w, h) < 0 ) {
   788 				return(-1);
   789 			}
   790 		} else {
   791 			XSetWindowAttributes swa;
   792 
   793 			swa.background_pixel = 0;
   794 			swa.border_pixel = 0;
   795 			swa.colormap = SDL_XColorMap;
   796 			SDL_Window = XCreateWindow(SDL_Display, WMwindow,
   797 		                           	0, 0, w, h, 0, depth,
   798 		                           	InputOutput, SDL_Visual,
   799 		                           	CWBackPixel | CWBorderPixel
   800 		                           	| CWColormap, &swa);
   801 		}
   802 		/* Only manage our input if we own the window */
   803 		XSelectInput(SDL_Display, SDL_Window,
   804 					( EnterWindowMask | LeaveWindowMask
   805 					| ButtonPressMask | ButtonReleaseMask
   806 					| PointerMotionMask | ExposureMask ));
   807 	}
   808 
   809 	/* Create the graphics context here, once we have a window */
   810 	if ( flags & SDL_OPENGL ) {
   811 		if ( X11_GL_CreateContext(this) < 0 ) {
   812 			return(-1);
   813 		} else {
   814 			screen->flags |= SDL_OPENGL;
   815 		}
   816 	} else {
   817 		XGCValues gcv;
   818 
   819 		gcv.graphics_exposures = False;
   820 		SDL_GC = XCreateGC(SDL_Display, SDL_Window,
   821 		                   GCGraphicsExposures, &gcv);
   822 		if ( ! SDL_GC ) {
   823 			SDL_SetError("Couldn't create graphics context");
   824 			return(-1);
   825 		}
   826 	}
   827 
   828 	/* Set our colormaps when not setting a GL mode */
   829 	if ( ! (flags & SDL_OPENGL) ) {
   830 		XSetWindowColormap(SDL_Display, SDL_Window, SDL_XColorMap);
   831 		if( !SDL_windowid ) {
   832 		    XSetWindowColormap(SDL_Display, FSwindow, SDL_XColorMap);
   833 		    XSetWindowColormap(SDL_Display, WMwindow, SDL_XColorMap);
   834 		}
   835 	}
   836 
   837 #if 0 /* This is an experiment - are the graphics faster now? - nope. */
   838 	if ( getenv("SDL_VIDEO_X11_BACKINGSTORE") )
   839 #endif
   840 	/* Cache the window in the server, when possible */
   841 	{
   842 		Screen *xscreen;
   843 		XSetWindowAttributes a;
   844 
   845 		xscreen = ScreenOfDisplay(SDL_Display, SDL_Screen);
   846 		a.backing_store = DoesBackingStore(xscreen);
   847 		if ( a.backing_store != NotUseful ) {
   848 			XChangeWindowAttributes(SDL_Display, SDL_Window,
   849 			                        CWBackingStore, &a);
   850 		}
   851 	}
   852 
   853 	/* Update the internal keyboard state */
   854 	X11_SetKeyboardState(SDL_Display, NULL);
   855 
   856 	/* When the window is first mapped, ignore non-modifier keys */
   857 	{
   858 		Uint8 *keys = SDL_GetKeyState(NULL);
   859 		for ( i = 0; i < SDLK_LAST; ++i ) {
   860 			switch (i) {
   861 			    case SDLK_NUMLOCK:
   862 			    case SDLK_CAPSLOCK:
   863 			    case SDLK_LCTRL:
   864 			    case SDLK_RCTRL:
   865 			    case SDLK_LSHIFT:
   866 			    case SDLK_RSHIFT:
   867 			    case SDLK_LALT:
   868 			    case SDLK_RALT:
   869 			    case SDLK_LMETA:
   870 			    case SDLK_RMETA:
   871 			    case SDLK_MODE:
   872 				break;
   873 			    default:
   874 				keys[i] = SDL_RELEASED;
   875 				break;
   876 			}
   877 		}
   878 	}
   879 
   880 	/* Map them both and go fullscreen, if requested */
   881 	if ( ! SDL_windowid ) {
   882 		XMapWindow(SDL_Display, SDL_Window);
   883 		XMapWindow(SDL_Display, WMwindow);
   884 		X11_WaitMapped(this, WMwindow);
   885 		if ( flags & SDL_FULLSCREEN ) {
   886 			screen->flags |= SDL_FULLSCREEN;
   887 			X11_EnterFullScreen(this);
   888 		} else {
   889 			screen->flags &= ~SDL_FULLSCREEN;
   890 		}
   891 	}
   892 	return(0);
   893 }
   894 
   895 static int X11_ResizeWindow(_THIS,
   896 			SDL_Surface *screen, int w, int h, Uint32 flags)
   897 {
   898 	if ( ! SDL_windowid ) {
   899 		/* Resize the window manager window */
   900 		X11_SetSizeHints(this, w, h, flags);
   901 		current_w = w;
   902 		current_h = h;
   903 		XResizeWindow(SDL_Display, WMwindow, w, h);
   904 
   905 		/* Resize the fullscreen and display windows */
   906 		if ( flags & SDL_FULLSCREEN ) {
   907 			if ( screen->flags & SDL_FULLSCREEN ) {
   908 				X11_ResizeFullScreen(this);
   909 			} else {
   910 				screen->flags |= SDL_FULLSCREEN;
   911 				X11_EnterFullScreen(this);
   912 			}
   913 		} else {
   914 			if ( screen->flags & SDL_FULLSCREEN ) {
   915 				screen->flags &= ~SDL_FULLSCREEN;
   916 				X11_LeaveFullScreen(this);
   917 			}
   918 		}
   919 		XResizeWindow(SDL_Display, SDL_Window, w, h);
   920 	}
   921 	return(0);
   922 }
   923 
   924 SDL_Surface *X11_SetVideoMode(_THIS, SDL_Surface *current,
   925 				int width, int height, int bpp, Uint32 flags)
   926 {
   927 	Uint32 saved_flags;
   928 
   929 	/* Lock the event thread, in multi-threading environments */
   930 	SDL_Lock_EventThread();
   931 
   932 	/* Check the combination of flags we were passed */
   933 	if ( flags & SDL_FULLSCREEN ) {
   934 		/* Clear fullscreen flag if not supported */
   935 		if ( SDL_windowid ) {
   936 			flags &= ~SDL_FULLSCREEN;
   937 		}
   938 	}
   939 
   940 	/* Flush any delayed updates */
   941 	XSync(GFX_Display, False);
   942 
   943 	/* Set up the X11 window */
   944 	saved_flags = current->flags;
   945 	if (SDL_Window && (saved_flags&SDL_OPENGL) == (flags&SDL_OPENGL)
   946 	    && bpp == current->format->BitsPerPixel) {
   947 		if (X11_ResizeWindow(this, current, width, height, flags) < 0) {
   948 			current = NULL;
   949 			goto done;
   950 		}
   951 	} else {
   952 		if (X11_CreateWindow(this,current,width,height,bpp,flags) < 0) {
   953 			current = NULL;
   954 			goto done;
   955 		}
   956 	}
   957 
   958 	/* Set up the new mode framebuffer */
   959 	if ( ((current->w != width) || (current->h != height)) ||
   960              ((saved_flags&SDL_OPENGL) != (flags&SDL_OPENGL)) ) {
   961 		current->w = width;
   962 		current->h = height;
   963 		current->pitch = SDL_CalculatePitch(current);
   964 		X11_ResizeImage(this, current, flags);
   965 	}
   966 	current->flags |= (flags&(SDL_RESIZABLE|SDL_NOFRAME));
   967 
   968   done:
   969 	/* Release the event thread */
   970 	XSync(SDL_Display, False);
   971 	SDL_Unlock_EventThread();
   972 
   973 	/* We're done! */
   974 	return(current);
   975 }
   976 
   977 static int X11_ToggleFullScreen(_THIS, int on)
   978 {
   979 	Uint32 event_thread;
   980 
   981 	/* Don't switch if we don't own the window */
   982 	if ( SDL_windowid ) {
   983 		return(0);
   984 	}
   985 
   986 	/* Don't lock if we are the event thread */
   987 	event_thread = SDL_EventThreadID();
   988 	if ( event_thread && (SDL_ThreadID() == event_thread) ) {
   989 		event_thread = 0;
   990 	}
   991 	if ( event_thread ) {
   992 		SDL_Lock_EventThread();
   993 	}
   994 	if ( on ) {
   995 		this->screen->flags |= SDL_FULLSCREEN;
   996 		X11_EnterFullScreen(this);
   997 	} else {
   998 		this->screen->flags &= ~SDL_FULLSCREEN;
   999 		X11_LeaveFullScreen(this);
  1000 	}
  1001 	X11_RefreshDisplay(this);
  1002 	if ( event_thread ) {
  1003 		SDL_Unlock_EventThread();
  1004 	}
  1005 	SDL_ResetKeyboard();
  1006 	return(1);
  1007 }
  1008 
  1009 /* Update the current mouse state and position */
  1010 static void X11_UpdateMouse(_THIS)
  1011 {
  1012 	Window u1; int u2;
  1013 	Window current_win;
  1014 	int x, y;
  1015 	unsigned int mask;
  1016 
  1017 	/* Lock the event thread, in multi-threading environments */
  1018 	SDL_Lock_EventThread();
  1019 	if ( XQueryPointer(SDL_Display, SDL_Window, &u1, &current_win,
  1020 	                   &u2, &u2, &x, &y, &mask) ) {
  1021 		if ( (x >= 0) && (x < SDL_VideoSurface->w) &&
  1022 		     (y >= 0) && (y < SDL_VideoSurface->h) ) {
  1023 			SDL_PrivateAppActive(1, SDL_APPMOUSEFOCUS);
  1024 			SDL_PrivateMouseMotion(0, 0, x, y);
  1025 		} else {
  1026 			SDL_PrivateAppActive(0, SDL_APPMOUSEFOCUS);
  1027 		}
  1028 	}
  1029 	SDL_Unlock_EventThread();
  1030 }
  1031 
  1032 /* simple colour distance metric. Supposed to be better than a plain
  1033    Euclidian distance anyway. */
  1034 #define COLOUR_FACTOR 3
  1035 #define LIGHT_FACTOR 1
  1036 #define COLOUR_DIST(r1, g1, b1, r2, g2, b2)				\
  1037 	(COLOUR_FACTOR * (abs(r1 - r2) + abs(g1 - g2) + abs(b1 - b2))	\
  1038 	 + LIGHT_FACTOR * abs(r1 + g1 + b1 - (r2 + g2 + b2)))
  1039 
  1040 static void allocate_nearest(_THIS, SDL_Color *colors,
  1041 			     SDL_Color *want, int nwant)
  1042 {
  1043 	/*
  1044 	 * There is no way to know which ones to choose from, so we retrieve
  1045 	 * the entire colormap and try the nearest possible, until we find one
  1046 	 * that is shared.
  1047 	 */
  1048 	XColor all[256];
  1049 	int i;
  1050 	for(i = 0; i < 256; i++)
  1051 		all[i].pixel = i;
  1052 	/* 
  1053 	 * XQueryColors sets the flags in the XColor struct, so we use
  1054 	 * that to keep track of which colours are available
  1055 	 */
  1056 	XQueryColors(GFX_Display, SDL_XColorMap, all, 256);
  1057 
  1058 	for(i = 0; i < nwant; i++) {
  1059 		XColor *c;
  1060 		int j;
  1061 		int best = 0;
  1062 		int mindist = 0x7fffffff;
  1063 		int ri = want[i].r;
  1064 		int gi = want[i].g;
  1065 		int bi = want[i].b;
  1066 		for(j = 0; j < 256; j++) {
  1067 			int rj, gj, bj, d2;
  1068 			if(!all[j].flags)
  1069 				continue;	/* unavailable colour cell */
  1070 			rj = all[j].red >> 8;
  1071 			gj = all[j].green >> 8;
  1072 			bj = all[j].blue >> 8;
  1073 			d2 = COLOUR_DIST(ri, gi, bi, rj, gj, bj);
  1074 			if(d2 < mindist) {
  1075 				mindist = d2;
  1076 				best = j;
  1077 			}
  1078 		}
  1079 		if(SDL_XPixels[best])
  1080 			continue; /* already allocated, waste no more time */
  1081 		c = all + best;
  1082 		if(XAllocColor(GFX_Display, SDL_XColorMap, c)) {
  1083 			/* got it */
  1084 			colors[c->pixel].r = c->red >> 8;
  1085 			colors[c->pixel].g = c->green >> 8;
  1086 			colors[c->pixel].b = c->blue >> 8;
  1087 			++SDL_XPixels[c->pixel];
  1088 		} else {
  1089 			/* 
  1090 			 * The colour couldn't be allocated, probably being
  1091 			 * owned as a r/w cell by another client. Flag it as
  1092 			 * unavailable and try again. The termination of the
  1093 			 * loop is guaranteed since at least black and white
  1094 			 * are always there.
  1095 			 */
  1096 			c->flags = 0;
  1097 			i--;
  1098 		}
  1099 	}
  1100 }
  1101 
  1102 int X11_SetColors(_THIS, int firstcolor, int ncolors, SDL_Color *colors)
  1103 {
  1104 	int nrej = 0;
  1105 
  1106 	/* Check to make sure we have a colormap allocated */
  1107 	if ( SDL_XPixels == NULL ) {
  1108 		return(0);
  1109 	}
  1110 	if ( (this->screen->flags & SDL_HWPALETTE) == SDL_HWPALETTE ) {
  1111 	        /* private writable colormap: just set the colours we need */
  1112 	        XColor  *xcmap;
  1113 		int i;
  1114 	        xcmap = ALLOCA(ncolors*sizeof(*xcmap));
  1115 		if(xcmap == NULL)
  1116 		        return 0;
  1117 		for ( i=0; i<ncolors; ++i ) {
  1118 			xcmap[i].pixel = i + firstcolor;
  1119 			xcmap[i].red   = (colors[i].r<<8)|colors[i].r;
  1120 			xcmap[i].green = (colors[i].g<<8)|colors[i].g;
  1121 			xcmap[i].blue  = (colors[i].b<<8)|colors[i].b;
  1122 			xcmap[i].flags = (DoRed|DoGreen|DoBlue);
  1123 		}
  1124 		XStoreColors(GFX_Display, SDL_XColorMap, xcmap, ncolors);
  1125 		XSync(GFX_Display, False);
  1126 		FREEA(xcmap);
  1127 	} else {
  1128 	        /*
  1129 		 * Shared colormap: We only allocate read-only cells, which
  1130 		 * increases the likelyhood of colour sharing with other
  1131 		 * clients. The pixel values will almost certainly be
  1132 		 * different from the requested ones, so the user has to
  1133 		 * walk the colormap and see which index got what colour.
  1134 		 *
  1135 		 * We can work directly with the logical palette since it
  1136 		 * has already been set when we get here.
  1137 		 */
  1138 		SDL_Color *want, *reject;
  1139 	        unsigned long *freelist;
  1140 		int i;
  1141 		int nfree = 0;
  1142 		int nc = this->screen->format->palette->ncolors;
  1143 	        colors = this->screen->format->palette->colors;
  1144 		freelist = ALLOCA(nc * sizeof(*freelist));
  1145 		/* make sure multiple allocations of the same cell are freed */
  1146 	        for(i = 0; i < ncolors; i++) {
  1147 		        int pixel = firstcolor + i;
  1148 		        while(SDL_XPixels[pixel]) {
  1149 			        freelist[nfree++] = pixel;
  1150 				--SDL_XPixels[pixel];
  1151 			}
  1152 		}
  1153 		XFreeColors(GFX_Display, SDL_XColorMap, freelist, nfree, 0);
  1154 		FREEA(freelist);
  1155 
  1156 		want = ALLOCA(ncolors * sizeof(SDL_Color));
  1157 		reject = ALLOCA(ncolors * sizeof(SDL_Color));
  1158 		memcpy(want, colors + firstcolor, ncolors * sizeof(SDL_Color));
  1159 		/* make sure the user isn't fooled by her own wishes
  1160 		   (black is safe, always available in the default colormap) */
  1161 		memset(colors + firstcolor, 0, ncolors * sizeof(SDL_Color));
  1162 
  1163 		/* now try to allocate the colours */
  1164 		for(i = 0; i < ncolors; i++) {
  1165 		        XColor col;
  1166 			col.red = want[i].r << 8;
  1167 			col.green = want[i].g << 8;
  1168 			col.blue = want[i].b << 8;
  1169 			col.flags = DoRed | DoGreen | DoBlue;
  1170 			if(XAllocColor(GFX_Display, SDL_XColorMap, &col)) {
  1171 			        /* We got the colour, or at least the nearest
  1172 				   the hardware could get. */
  1173 			        colors[col.pixel].r = col.red >> 8;
  1174 				colors[col.pixel].g = col.green >> 8;
  1175 				colors[col.pixel].b = col.blue >> 8;
  1176 				++SDL_XPixels[col.pixel];
  1177 			} else {
  1178 				/*
  1179 				 * no more free cells, add it to the list
  1180 				 * of rejected colours
  1181 				 */
  1182 				reject[nrej++] = want[i];
  1183 			}
  1184 		}
  1185 		if(nrej)
  1186 			allocate_nearest(this, colors, reject, nrej);
  1187 		FREEA(reject);
  1188 		FREEA(want);
  1189 	}
  1190 	return nrej == 0;
  1191 }
  1192 
  1193 int X11_SetGammaRamp(_THIS, Uint16 *ramp)
  1194 {
  1195 	int i, ncolors;
  1196 	XColor xcmap[256];
  1197 
  1198 	/* See if actually setting the gamma is supported */
  1199 	if ( SDL_Visual->class != DirectColor ) {
  1200 	    SDL_SetError("Gamma correction not supported on this visual");
  1201 	    return(-1);
  1202 	}
  1203 
  1204 	/* Calculate the appropriate palette for the given gamma ramp */
  1205 	ncolors = SDL_Visual->map_entries;
  1206 	for ( i=0; i<ncolors; ++i ) {
  1207 		Uint8 c = (256 * i / ncolors);
  1208 		xcmap[i].pixel = SDL_MapRGB(this->screen->format, c, c, c);
  1209 		xcmap[i].red   = ramp[0*256+c];
  1210 		xcmap[i].green = ramp[1*256+c];
  1211 		xcmap[i].blue  = ramp[2*256+c];
  1212 		xcmap[i].flags = (DoRed|DoGreen|DoBlue);
  1213 	}
  1214 	XStoreColors(GFX_Display, SDL_XColorMap, xcmap, ncolors);
  1215 	XSync(GFX_Display, False);
  1216 	return(0);
  1217 }
  1218 
  1219 /* Note:  If we are terminated, this could be called in the middle of
  1220    another SDL video routine -- notably UpdateRects.
  1221 */
  1222 void X11_VideoQuit(_THIS)
  1223 {
  1224 	/* Shutdown everything that's still up */
  1225 	/* The event thread should be done, so we can touch SDL_Display */
  1226 	if ( SDL_Display != NULL ) {
  1227 		/* Flush any delayed updates */
  1228 		XSync(GFX_Display, False);
  1229 
  1230 		/* Start shutting down the windows */
  1231 		X11_DestroyImage(this, this->screen);
  1232 		X11_DestroyWindow(this, this->screen);
  1233 		X11_FreeVideoModes(this);
  1234 		if ( SDL_XColorMap != SDL_DisplayColormap ) {
  1235 			XFreeColormap(SDL_Display, SDL_XColorMap);
  1236 		}
  1237 		if ( SDL_iconcolors ) {
  1238 			unsigned long pixel;
  1239 			Colormap dcmap = DefaultColormap(SDL_Display,
  1240 							 SDL_Screen);
  1241 			for(pixel = 0; pixel < 256; ++pixel) {
  1242 				while(SDL_iconcolors[pixel] > 0) {
  1243 					XFreeColors(GFX_Display,
  1244 						    dcmap, &pixel, 1, 0);
  1245 					--SDL_iconcolors[pixel];
  1246 				}
  1247 			}
  1248 			free(SDL_iconcolors);
  1249 			SDL_iconcolors = NULL;
  1250 		} 
  1251 		/* Restore gamma settings if they've changed */
  1252 		if ( SDL_GetAppState() & SDL_APPACTIVE ) {
  1253 			X11_SwapVidModeGamma(this);
  1254 		}
  1255 
  1256 		/* Free that blank cursor */
  1257 		if ( SDL_BlankCursor != NULL ) {
  1258 			this->FreeWMCursor(this, SDL_BlankCursor);
  1259 			SDL_BlankCursor = NULL;
  1260 		}
  1261 
  1262 		/* Close the X11 graphics connection */
  1263 		if ( GFX_Display != NULL ) {
  1264 			XCloseDisplay(GFX_Display);
  1265 			GFX_Display = NULL;
  1266 		}
  1267 
  1268 		/* Close the X11 display connection */
  1269 		XCloseDisplay(SDL_Display);
  1270 		SDL_Display = NULL;
  1271 
  1272 		/* Reset the X11 error handlers */
  1273 		if ( XIO_handler ) {
  1274 			XSetIOErrorHandler(XIO_handler);
  1275 		}
  1276 		if ( X_handler ) {
  1277 			XSetErrorHandler(X_handler);
  1278 		}
  1279 
  1280 		/* Unload GL library after X11 shuts down */
  1281 		X11_GL_UnloadLibrary(this);
  1282 	}
  1283 	if ( this->screen && (this->screen->flags & SDL_HWSURFACE) ) {
  1284 		/* Direct screen access, no memory buffer */
  1285 		this->screen->pixels = NULL;
  1286 	}
  1287 }
  1288