src/video/x11/SDL_x11video.c
author Ryan C. Gordon <icculus@icculus.org>
Mon, 21 Nov 2005 00:16:34 +0000
changeset 1178 9867f3d86e44
parent 1168 045f186426e1
child 1191 2bd4cec0de63
permissions -rw-r--r--
Real Unicode support for X11. Based on updated version of this patch:
http://lists.arabeyes.org/archives/developer/2004/June/msg00160.html

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