src/video/x11/SDL_x11video.c
author Ryan C. Gordon
Tue, 29 Sep 2009 13:50:33 +0000
branchSDL-1.2
changeset 4268 d48035d857d3
parent 4266 62849663f20a
child 4278 010efca83dfc
permissions -rw-r--r--
Make SDL_GL_GetAttribute(SDL_GL_SWAP_CONTROL) work with GLX_SGI_swap_control.

Fixes Bugzilla #697.
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@4159
     3
    Copyright (C) 1997-2009 Sam Lantinga
slouken@0
     4
slouken@0
     5
    This library is free software; you can redistribute it and/or
slouken@1312
     6
    modify it under the terms of the GNU Lesser General Public
slouken@0
     7
    License as published by the Free Software Foundation; either
slouken@1312
     8
    version 2.1 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@1312
    13
    Lesser General Public License for more details.
slouken@0
    14
slouken@1312
    15
    You should have received a copy of the GNU Lesser General Public
slouken@1312
    16
    License along with this library; if not, write to the Free Software
slouken@1312
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@0
    18
slouken@0
    19
    Sam Lantinga
slouken@252
    20
    slouken@libsdl.org
slouken@0
    21
*/
slouken@1402
    22
#include "SDL_config.h"
slouken@0
    23
slouken@0
    24
/* X11 based SDL video driver implementation.
slouken@0
    25
   Note:  This implementation does not currently need X11 thread locking,
slouken@0
    26
          since the event thread uses a separate X connection and any
slouken@0
    27
          additional locking necessary is handled internally.  However,
slouken@0
    28
          if full locking is neccessary, take a look at XInitThreads().
slouken@0
    29
*/
slouken@0
    30
slouken@0
    31
#include <unistd.h>
slouken@0
    32
#include <sys/ioctl.h>
slouken@0
    33
#ifdef MTRR_SUPPORT
slouken@0
    34
#include <asm/mtrr.h>
slouken@0
    35
#include <sys/fcntl.h>
slouken@0
    36
#endif
slouken@0
    37
slouken@1358
    38
#include "SDL_endian.h"
slouken@0
    39
#include "SDL_timer.h"
slouken@0
    40
#include "SDL_thread.h"
slouken@0
    41
#include "SDL_video.h"
slouken@0
    42
#include "SDL_mouse.h"
slouken@1361
    43
#include "../SDL_sysvideo.h"
slouken@1361
    44
#include "../SDL_pixels_c.h"
slouken@1361
    45
#include "../../events/SDL_events_c.h"
slouken@0
    46
#include "SDL_x11video.h"
slouken@0
    47
#include "SDL_x11wm_c.h"
slouken@0
    48
#include "SDL_x11mouse_c.h"
slouken@0
    49
#include "SDL_x11events_c.h"
slouken@0
    50
#include "SDL_x11modes_c.h"
slouken@0
    51
#include "SDL_x11image_c.h"
slouken@0
    52
#include "SDL_x11yuv_c.h"
slouken@0
    53
#include "SDL_x11gl_c.h"
slouken@0
    54
#include "SDL_x11gamma_c.h"
slouken@1361
    55
#include "../blank_cursor.h"
slouken@0
    56
icculus@3978
    57
#ifdef X_HAVE_UTF8_STRING
icculus@3978
    58
#include <locale.h>
icculus@3978
    59
#endif
icculus@3978
    60
slouken@0
    61
/* Initialization/Query functions */
slouken@0
    62
static int X11_VideoInit(_THIS, SDL_PixelFormat *vformat);
slouken@0
    63
static SDL_Surface *X11_SetVideoMode(_THIS, SDL_Surface *current, int width, int height, int bpp, Uint32 flags);
slouken@0
    64
static int X11_ToggleFullScreen(_THIS, int on);
slouken@0
    65
static void X11_UpdateMouse(_THIS);
slouken@0
    66
static int X11_SetColors(_THIS, int firstcolor, int ncolors,
slouken@0
    67
			 SDL_Color *colors);
slouken@0
    68
static int X11_SetGammaRamp(_THIS, Uint16 *ramp);
slouken@0
    69
static void X11_VideoQuit(_THIS);
slouken@0
    70
icculus@1168
    71
slouken@0
    72
/* X11 driver bootstrap functions */
slouken@0
    73
slouken@0
    74
static int X11_Available(void)
slouken@0
    75
{
icculus@1168
    76
	Display *display = NULL;
icculus@1168
    77
	if ( SDL_X11_LoadSymbols() ) {
icculus@1575
    78
		display = XOpenDisplay(NULL);
icculus@1168
    79
		if ( display != NULL ) {
icculus@1575
    80
			XCloseDisplay(display);
icculus@1168
    81
		}
icculus@1168
    82
		SDL_X11_UnloadSymbols();
slouken@0
    83
	}
slouken@0
    84
	return(display != NULL);
slouken@0
    85
}
slouken@0
    86
slouken@0
    87
static void X11_DeleteDevice(SDL_VideoDevice *device)
slouken@0
    88
{
slouken@0
    89
	if ( device ) {
slouken@0
    90
		if ( device->hidden ) {
slouken@1336
    91
			SDL_free(device->hidden);
slouken@0
    92
		}
slouken@0
    93
		if ( device->gl_data ) {
slouken@1336
    94
			SDL_free(device->gl_data);
slouken@0
    95
		}
slouken@1336
    96
		SDL_free(device);
icculus@1168
    97
		SDL_X11_UnloadSymbols();
slouken@0
    98
	}
slouken@0
    99
}
slouken@0
   100
slouken@0
   101
static SDL_VideoDevice *X11_CreateDevice(int devindex)
slouken@0
   102
{
icculus@1168
   103
	SDL_VideoDevice *device = NULL;
slouken@0
   104
icculus@1168
   105
	if ( SDL_X11_LoadSymbols() ) {
icculus@1168
   106
		/* Initialize all variables that we clean on shutdown */
slouken@1336
   107
		device = (SDL_VideoDevice *)SDL_malloc(sizeof(SDL_VideoDevice));
icculus@1168
   108
		if ( device ) {
slouken@1336
   109
			SDL_memset(device, 0, (sizeof *device));
icculus@1168
   110
			device->hidden = (struct SDL_PrivateVideoData *)
slouken@1336
   111
					SDL_malloc((sizeof *device->hidden));
icculus@1168
   112
			device->gl_data = (struct SDL_PrivateGLData *)
slouken@1336
   113
					SDL_malloc((sizeof *device->gl_data));
icculus@1168
   114
		}
icculus@1168
   115
		if ( (device == NULL) || (device->hidden == NULL) ||
icculus@1168
   116
		                         (device->gl_data == NULL) ) {
icculus@1168
   117
			SDL_OutOfMemory();
icculus@1168
   118
			X11_DeleteDevice(device); /* calls SDL_X11_UnloadSymbols(). */
icculus@1168
   119
			return(0);
icculus@1168
   120
		}
slouken@1336
   121
		SDL_memset(device->hidden, 0, (sizeof *device->hidden));
slouken@1336
   122
		SDL_memset(device->gl_data, 0, (sizeof *device->gl_data));
slouken@0
   123
icculus@4268
   124
		device->gl_data->sgi_swap_interval = 1;
icculus@4268
   125
icculus@1168
   126
		/* Set the driver flags */
icculus@1168
   127
		device->handles_any_size = 1;
slouken@0
   128
icculus@1168
   129
		/* Set the function pointers */
icculus@1168
   130
		device->VideoInit = X11_VideoInit;
icculus@1168
   131
		device->ListModes = X11_ListModes;
icculus@1168
   132
		device->SetVideoMode = X11_SetVideoMode;
icculus@1168
   133
		device->ToggleFullScreen = X11_ToggleFullScreen;
icculus@1168
   134
		device->UpdateMouse = X11_UpdateMouse;
slouken@1361
   135
#if SDL_VIDEO_DRIVER_X11_XV
icculus@1168
   136
		device->CreateYUVOverlay = X11_CreateYUVOverlay;
slouken@0
   137
#endif
icculus@1168
   138
		device->SetColors = X11_SetColors;
icculus@1168
   139
		device->UpdateRects = NULL;
icculus@1168
   140
		device->VideoQuit = X11_VideoQuit;
icculus@1168
   141
		device->AllocHWSurface = X11_AllocHWSurface;
icculus@1168
   142
		device->CheckHWBlit = NULL;
icculus@1168
   143
		device->FillHWRect = NULL;
icculus@1168
   144
		device->SetHWColorKey = NULL;
icculus@1168
   145
		device->SetHWAlpha = NULL;
icculus@1168
   146
		device->LockHWSurface = X11_LockHWSurface;
icculus@1168
   147
		device->UnlockHWSurface = X11_UnlockHWSurface;
icculus@1168
   148
		device->FlipHWSurface = X11_FlipHWSurface;
icculus@1168
   149
		device->FreeHWSurface = X11_FreeHWSurface;
icculus@1168
   150
		device->SetGamma = X11_SetVidModeGamma;
icculus@1168
   151
		device->GetGamma = X11_GetVidModeGamma;
icculus@1168
   152
		device->SetGammaRamp = X11_SetGammaRamp;
icculus@1168
   153
		device->GetGammaRamp = NULL;
slouken@1361
   154
#if SDL_VIDEO_OPENGL_GLX
icculus@1168
   155
		device->GL_LoadLibrary = X11_GL_LoadLibrary;
icculus@1168
   156
		device->GL_GetProcAddress = X11_GL_GetProcAddress;
icculus@1168
   157
		device->GL_GetAttribute = X11_GL_GetAttribute;
icculus@1168
   158
		device->GL_MakeCurrent = X11_GL_MakeCurrent;
icculus@1168
   159
		device->GL_SwapBuffers = X11_GL_SwapBuffers;
slouken@0
   160
#endif
icculus@1168
   161
		device->SetCaption = X11_SetCaption;
icculus@1168
   162
		device->SetIcon = X11_SetIcon;
icculus@1168
   163
		device->IconifyWindow = X11_IconifyWindow;
icculus@1168
   164
		device->GrabInput = X11_GrabInput;
icculus@1168
   165
		device->GetWMInfo = X11_GetWMInfo;
icculus@1168
   166
		device->FreeWMCursor = X11_FreeWMCursor;
icculus@1168
   167
		device->CreateWMCursor = X11_CreateWMCursor;
icculus@1168
   168
		device->ShowWMCursor = X11_ShowWMCursor;
icculus@1168
   169
		device->WarpWMCursor = X11_WarpWMCursor;
icculus@1168
   170
		device->CheckMouseMode = X11_CheckMouseMode;
icculus@1168
   171
		device->InitOSKeymap = X11_InitOSKeymap;
icculus@1168
   172
		device->PumpEvents = X11_PumpEvents;
slouken@0
   173
icculus@1168
   174
		device->free = X11_DeleteDevice;
icculus@1168
   175
	}
slouken@0
   176
slouken@0
   177
	return device;
slouken@0
   178
}
slouken@0
   179
slouken@0
   180
VideoBootStrap X11_bootstrap = {
slouken@0
   181
	"x11", "X Window System",
slouken@0
   182
	X11_Available, X11_CreateDevice
slouken@0
   183
};
slouken@0
   184
slouken@0
   185
/* Normal X11 error handler routine */
slouken@0
   186
static int (*X_handler)(Display *, XErrorEvent *) = NULL;
slouken@0
   187
static int x_errhandler(Display *d, XErrorEvent *e)
slouken@0
   188
{
slouken@1361
   189
#if SDL_VIDEO_DRIVER_X11_VIDMODE
slouken@0
   190
	extern int vm_error;
slouken@0
   191
#endif
slouken@1361
   192
#if SDL_VIDEO_DRIVER_X11_DGAMOUSE
slouken@0
   193
	extern int dga_error;
slouken@0
   194
#endif
slouken@0
   195
slouken@1361
   196
#if SDL_VIDEO_DRIVER_X11_VIDMODE
slouken@0
   197
	/* VidMode errors are non-fatal. :) */
slouken@0
   198
	/* Are the errors offset by one from the error base?
slouken@0
   199
	   e.g. the error base is 143, the code is 148, and the
slouken@0
   200
	        actual error is XF86VidModeExtensionDisabled (4) ?
slouken@0
   201
	 */
slouken@0
   202
        if ( (vm_error >= 0) &&
slouken@0
   203
	     (((e->error_code == BadRequest)&&(e->request_code == vm_error)) ||
slouken@0
   204
	      ((e->error_code > vm_error) &&
slouken@0
   205
	       (e->error_code <= (vm_error+XF86VidModeNumberErrors)))) ) {
slouken@1765
   206
#ifdef X11_DEBUG
slouken@0
   207
{ char errmsg[1024];
icculus@1575
   208
  XGetErrorText(d, e->error_code, errmsg, sizeof(errmsg));
slouken@0
   209
printf("VidMode error: %s\n", errmsg);
slouken@0
   210
}
slouken@0
   211
#endif
slouken@0
   212
        	return(0);
slouken@0
   213
        }
slouken@1361
   214
#endif /* SDL_VIDEO_DRIVER_X11_VIDMODE */
slouken@0
   215
slouken@1361
   216
#if SDL_VIDEO_DRIVER_X11_DGAMOUSE
slouken@0
   217
	/* DGA errors can be non-fatal. :) */
slouken@0
   218
        if ( (dga_error >= 0) &&
slouken@0
   219
	     ((e->error_code > dga_error) &&
slouken@0
   220
	      (e->error_code <= (dga_error+XF86DGANumberErrors))) ) {
slouken@1765
   221
#ifdef X11_DEBUG
slouken@0
   222
{ char errmsg[1024];
icculus@1575
   223
  XGetErrorText(d, e->error_code, errmsg, sizeof(errmsg));
slouken@0
   224
printf("DGA error: %s\n", errmsg);
slouken@0
   225
}
slouken@0
   226
#endif
slouken@0
   227
        	return(0);
slouken@0
   228
        }
slouken@1361
   229
#endif /* SDL_VIDEO_DRIVER_X11_DGAMOUSE */
slouken@0
   230
slouken@0
   231
	return(X_handler(d,e));
slouken@0
   232
}
slouken@0
   233
slouken@0
   234
/* X11 I/O error handler routine */
slouken@0
   235
static int (*XIO_handler)(Display *) = NULL;
slouken@0
   236
static int xio_errhandler(Display *d)
slouken@0
   237
{
slouken@0
   238
	/* Ack!  Lost X11 connection! */
slouken@0
   239
slouken@0
   240
	/* We will crash if we try to clean up our display */
slouken@4210
   241
	if ( SDL_VideoSurface && current_video->hidden->Ximage ) {
slouken@0
   242
		SDL_VideoSurface->pixels = NULL;
slouken@0
   243
	}
slouken@0
   244
	current_video->hidden->X11_Display = NULL;
slouken@0
   245
slouken@0
   246
	/* Continue with the standard X11 error handler */
slouken@0
   247
	return(XIO_handler(d));
slouken@0
   248
}
slouken@0
   249
icculus@1609
   250
static int (*Xext_handler)(Display *, _Xconst char *, _Xconst char *) = NULL;
icculus@1609
   251
static int xext_errhandler(Display *d, _Xconst char *ext, _Xconst char *reason)
icculus@1248
   252
{
slouken@1765
   253
#ifdef X11_DEBUG
icculus@1248
   254
	printf("Xext error inside SDL (may be harmless):\n");
icculus@1248
   255
	printf("  Extension \"%s\" %s on display \"%s\".\n",
icculus@1609
   256
	       ext, reason, XDisplayString(d));
icculus@1248
   257
#endif
icculus@1248
   258
slouken@1336
   259
	if (SDL_strcmp(reason, "missing") == 0) {
icculus@1248
   260
		/*
icculus@1248
   261
		 * Since the query itself, elsewhere, can handle a missing extension
icculus@1248
   262
		 *  and the default behaviour in Xlib is to write to stderr, which
icculus@1248
   263
		 *  generates unnecessary bug reports, we just ignore these.
icculus@1248
   264
		 */
icculus@1248
   265
		return 0;
icculus@1248
   266
	}
icculus@1248
   267
icculus@1248
   268
	/* Everything else goes to the default handler... */
icculus@1609
   269
	return Xext_handler(d, ext, reason);
icculus@1248
   270
}
icculus@1248
   271
slouken@1325
   272
/* Find out what class name we should use */
slouken@1325
   273
static char *get_classname(char *classname, int maxlen)
slouken@1325
   274
{
slouken@1325
   275
	char *spot;
slouken@1402
   276
#if defined(__LINUX__) || defined(__FREEBSD__)
slouken@1325
   277
	char procfile[1024];
slouken@1325
   278
	char linkfile[1024];
slouken@1325
   279
	int linksize;
slouken@1325
   280
#endif
slouken@1325
   281
slouken@1325
   282
	/* First allow environment variable override */
slouken@1336
   283
	spot = SDL_getenv("SDL_VIDEO_X11_WMCLASS");
slouken@1325
   284
	if ( spot ) {
slouken@1379
   285
		SDL_strlcpy(classname, spot, maxlen);
slouken@1325
   286
		return classname;
slouken@1325
   287
	}
slouken@1325
   288
slouken@1325
   289
	/* Next look at the application's executable name */
slouken@1402
   290
#if defined(__LINUX__) || defined(__FREEBSD__)
slouken@1402
   291
#if defined(__LINUX__)
slouken@1338
   292
	SDL_snprintf(procfile, SDL_arraysize(procfile), "/proc/%d/exe", getpid());
slouken@1402
   293
#elif defined(__FREEBSD__)
slouken@1338
   294
	SDL_snprintf(procfile, SDL_arraysize(procfile), "/proc/%d/file", getpid());
slouken@1325
   295
#else
slouken@1325
   296
#error Where can we find the executable name?
slouken@1325
   297
#endif
slouken@1325
   298
	linksize = readlink(procfile, linkfile, sizeof(linkfile)-1);
slouken@1325
   299
	if ( linksize > 0 ) {
slouken@1325
   300
		linkfile[linksize] = '\0';
slouken@1336
   301
		spot = SDL_strrchr(linkfile, '/');
slouken@1325
   302
		if ( spot ) {
slouken@1379
   303
			SDL_strlcpy(classname, spot+1, maxlen);
slouken@1325
   304
		} else {
slouken@1379
   305
			SDL_strlcpy(classname, linkfile, maxlen);
slouken@1325
   306
		}
slouken@1325
   307
		return classname;
slouken@1325
   308
	}
slouken@1402
   309
#endif /* __LINUX__ */
slouken@1325
   310
slouken@1325
   311
	/* Finally use the default we've used forever */
slouken@1379
   312
	SDL_strlcpy(classname, "SDL_App", maxlen);
slouken@1325
   313
	return classname;
slouken@1325
   314
}
slouken@1325
   315
slouken@0
   316
/* Create auxiliary (toplevel) windows with the current visual */
slouken@0
   317
static void create_aux_windows(_THIS)
slouken@0
   318
{
slouken@1765
   319
    int x = 0, y = 0;
slouken@1325
   320
    char classname[1024];
slouken@0
   321
    XSetWindowAttributes xattr;
slouken@0
   322
    XWMHints *hints;
icculus@3978
   323
    unsigned long app_event_mask;
slouken@0
   324
    int def_vis = (SDL_Visual == DefaultVisual(SDL_Display, SDL_Screen));
slouken@0
   325
slouken@1558
   326
    /* Look up some useful Atoms */
icculus@1575
   327
    WM_DELETE_WINDOW = XInternAtom(SDL_Display, "WM_DELETE_WINDOW", False);
slouken@1558
   328
slouken@0
   329
    /* Don't create any extra windows if we are being managed */
slouken@0
   330
    if ( SDL_windowid ) {
slouken@0
   331
	FSwindow = 0;
slouken@1336
   332
	WMwindow = SDL_strtol(SDL_windowid, NULL, 0);
slouken@0
   333
        return;
slouken@0
   334
    }
slouken@0
   335
slouken@0
   336
    if(FSwindow)
icculus@1575
   337
	XDestroyWindow(SDL_Display, FSwindow);
slouken@0
   338
slouken@1863
   339
#if SDL_VIDEO_DRIVER_X11_XINERAMA
slouken@1765
   340
    if ( use_xinerama ) {
slouken@1765
   341
        x = xinerama_info.x_org;
slouken@1765
   342
        y = xinerama_info.y_org;
slouken@1765
   343
    }
slouken@1765
   344
#endif
slouken@0
   345
    xattr.override_redirect = True;
slouken@0
   346
    xattr.background_pixel = def_vis ? BlackPixel(SDL_Display, SDL_Screen) : 0;
slouken@0
   347
    xattr.border_pixel = 0;
slouken@0
   348
    xattr.colormap = SDL_XColorMap;
slouken@0
   349
icculus@1575
   350
    FSwindow = XCreateWindow(SDL_Display, SDL_Root,
slouken@1765
   351
                             x, y, 32, 32, 0,
slouken@0
   352
			     this->hidden->depth, InputOutput, SDL_Visual,
slouken@0
   353
			     CWOverrideRedirect | CWBackPixel | CWBorderPixel
slouken@0
   354
			     | CWColormap,
slouken@0
   355
			     &xattr);
slouken@0
   356
icculus@1575
   357
    XSelectInput(SDL_Display, FSwindow, StructureNotifyMask);
slouken@0
   358
slouken@0
   359
    /* Tell KDE to keep the fullscreen window on top */
slouken@0
   360
    {
slouken@0
   361
	XEvent ev;
slouken@0
   362
	long mask;
slouken@0
   363
slouken@1336
   364
	SDL_memset(&ev, 0, sizeof(ev));
slouken@0
   365
	ev.xclient.type = ClientMessage;
slouken@0
   366
	ev.xclient.window = SDL_Root;
icculus@1575
   367
	ev.xclient.message_type = XInternAtom(SDL_Display,
slouken@0
   368
					      "KWM_KEEP_ON_TOP", False);
slouken@0
   369
	ev.xclient.format = 32;
slouken@0
   370
	ev.xclient.data.l[0] = FSwindow;
slouken@0
   371
	ev.xclient.data.l[1] = CurrentTime;
slouken@0
   372
	mask = SubstructureRedirectMask;
icculus@1575
   373
	XSendEvent(SDL_Display, SDL_Root, False, mask, &ev);
slouken@0
   374
    }
slouken@0
   375
slouken@0
   376
    hints = NULL;
slouken@0
   377
    if(WMwindow) {
slouken@0
   378
	/* All window attributes must survive the recreation */
icculus@1575
   379
	hints = XGetWMHints(SDL_Display, WMwindow);
icculus@1575
   380
	XDestroyWindow(SDL_Display, WMwindow);
slouken@0
   381
    }
slouken@0
   382
slouken@0
   383
    /* Create the window for windowed management */
slouken@0
   384
    /* (reusing the xattr structure above) */
slouken@1765
   385
    WMwindow = XCreateWindow(SDL_Display, SDL_Root,
slouken@1765
   386
                             x, y, 32, 32, 0,
slouken@0
   387
			     this->hidden->depth, InputOutput, SDL_Visual,
slouken@0
   388
			     CWBackPixel | CWBorderPixel | CWColormap,
slouken@0
   389
			     &xattr);
slouken@0
   390
slouken@0
   391
    /* Set the input hints so we get keyboard input */
slouken@0
   392
    if(!hints) {
icculus@1575
   393
	hints = XAllocWMHints();
slouken@0
   394
	hints->input = True;
slouken@0
   395
	hints->flags = InputHint;
slouken@0
   396
    }
icculus@1575
   397
    XSetWMHints(SDL_Display, WMwindow, hints);
icculus@1575
   398
    XFree(hints);
slouken@1767
   399
    X11_SetCaptionNoLock(this, this->wm_title, this->wm_icon);
slouken@0
   400
icculus@3978
   401
    app_event_mask = FocusChangeMask | KeyPressMask | KeyReleaseMask
icculus@3978
   402
	| PropertyChangeMask | StructureNotifyMask | KeymapStateMask;
icculus@3978
   403
    XSelectInput(SDL_Display, WMwindow, app_event_mask);
slouken@0
   404
slouken@0
   405
    /* Set the class hints so we can get an icon (AfterStep) */
slouken@1325
   406
    get_classname(classname, sizeof(classname));
slouken@0
   407
    {
slouken@0
   408
	XClassHint *classhints;
icculus@1575
   409
	classhints = XAllocClassHint();
slouken@0
   410
	if(classhints != NULL) {
slouken@449
   411
	    classhints->res_name = classname;
slouken@449
   412
	    classhints->res_class = classname;
icculus@1575
   413
	    XSetClassHint(SDL_Display, WMwindow, classhints);
icculus@1575
   414
	    XFree(classhints);
slouken@0
   415
	}
slouken@0
   416
    }
slouken@0
   417
icculus@3964
   418
	/* Setup the communication with the IM server */
icculus@3978
   419
	/* create_aux_windows may be called several times against the same
icculus@3978
   420
	   Display.  We should reuse the SDL_IM if one has been opened for
icculus@3978
   421
	   the Display, so we should not simply reset SDL_IM here.  */
icculus@1178
   422
icculus@1575
   423
	#ifdef X_HAVE_UTF8_STRING
icculus@1575
   424
	if (SDL_X11_HAVE_UTF8) {
icculus@3978
   425
		/* Discard obsolete resources if any.  */
icculus@3978
   426
		if (SDL_IM != NULL && SDL_Display != XDisplayOfIM(SDL_IM)) {
icculus@3978
   427
			/* Just a double check. I don't think this
icculus@3978
   428
		           code is ever executed. */
icculus@3978
   429
			SDL_SetError("display has changed while an IM is kept");
icculus@3978
   430
			if (SDL_IC) {
icculus@3978
   431
				XUnsetICFocus(SDL_IC);
icculus@3978
   432
				XDestroyIC(SDL_IC);
icculus@3978
   433
				SDL_IC = NULL;
icculus@3978
   434
			}
icculus@3978
   435
			XCloseIM(SDL_IM);
icculus@3978
   436
			SDL_IM = NULL;
icculus@3978
   437
		}
icculus@3978
   438
icculus@3978
   439
		/* Open an input method.  */
icculus@3978
   440
		if (SDL_IM == NULL) {
slouken@4067
   441
			char *old_locale = NULL, *old_modifiers = NULL;
slouken@4067
   442
			const char *p;
slouken@4067
   443
			size_t n;
icculus@3978
   444
			/* I'm not comfortable to do locale setup
icculus@3978
   445
			   here.  However, we need C library locale
icculus@3978
   446
			   (and xlib modifiers) to be set based on the
icculus@3978
   447
			   user's preference to use XIM, and many
icculus@3978
   448
			   existing game programs doesn't take care of
icculus@3978
   449
			   users' locale preferences, so someone other
icculus@3978
   450
			   than the game program should do it.
icculus@3978
   451
			   Moreover, ones say that some game programs
icculus@3978
   452
			   heavily rely on the C locale behaviour,
icculus@3978
   453
			   e.g., strcol()'s, and we can't change the C
icculus@3978
   454
			   library locale.  Given the situation, I
icculus@3978
   455
			   couldn't find better place to do the
icculus@3978
   456
			   job... */
icculus@3978
   457
icculus@3978
   458
			/* Save the current (application program's)
icculus@3978
   459
			   locale settings.  */
slouken@4067
   460
			p = setlocale(LC_ALL, NULL);
slouken@4067
   461
			if ( p ) {
slouken@4067
   462
				n = SDL_strlen(p)+1;
slouken@4067
   463
				old_locale = SDL_stack_alloc(char, n);
slouken@4067
   464
				if ( old_locale ) {
slouken@4067
   465
					SDL_strlcpy(old_locale, p, n);
slouken@4067
   466
				}
slouken@4067
   467
			}
slouken@4067
   468
			p = XSetLocaleModifiers(NULL);
slouken@4067
   469
			if ( p ) {
slouken@4067
   470
				n = SDL_strlen(p)+1;
slouken@4067
   471
				old_modifiers = SDL_stack_alloc(char, n);
slouken@4067
   472
				if ( old_modifiers ) {
slouken@4067
   473
					SDL_strlcpy(old_modifiers, p, n);
slouken@4067
   474
				}
icculus@3978
   475
			}
icculus@3978
   476
icculus@3978
   477
			/* Fetch the user's preferences and open the
icculus@3978
   478
			   input method with them.  */
icculus@3978
   479
			setlocale(LC_ALL, "");
icculus@3978
   480
			XSetLocaleModifiers("");
icculus@3978
   481
			SDL_IM = XOpenIM(SDL_Display, NULL, classname, classname);
icculus@3978
   482
icculus@3978
   483
			/* Restore the application's locale settings
icculus@3978
   484
			   so that we don't break the application's
icculus@3978
   485
			   expected behaviour.  */
slouken@4067
   486
			if ( old_locale ) {
icculus@3978
   487
				/* We need to restore the C library
icculus@3978
   488
				   locale first, since the
icculus@3978
   489
				   interpretation of the X modifier
icculus@3978
   490
				   may depend on it.  */
icculus@3978
   491
				setlocale(LC_ALL, old_locale);
slouken@4067
   492
				SDL_stack_free(old_locale);
slouken@4067
   493
			}
slouken@4067
   494
			if ( old_modifiers ) {
icculus@3978
   495
				XSetLocaleModifiers(old_modifiers);
slouken@4067
   496
				SDL_stack_free(old_modifiers);
icculus@3978
   497
			}
icculus@3978
   498
		}
icculus@3978
   499
icculus@3978
   500
		/* Create a new input context for the new window just created.  */
icculus@1575
   501
		if (SDL_IM == NULL) {
icculus@1575
   502
			SDL_SetError("no input method could be opened");
icculus@1575
   503
		} else {
icculus@3978
   504
			if (SDL_IC != NULL) {
icculus@3978
   505
				/* Discard the old IC before creating new one.  */
icculus@3978
   506
			    XUnsetICFocus(SDL_IC);
icculus@3978
   507
			    XDestroyIC(SDL_IC);
icculus@3978
   508
			}
icculus@3978
   509
			/* Theoretically we should check the current IM supports
icculus@3978
   510
			   PreeditNothing+StatusNothing style (i.e., root window method)
icculus@3978
   511
			   before creating the IC.  However, it is the bottom line method,
icculus@3978
   512
			   and we supports any other options.  If the IM didn't support
icculus@3978
   513
			   root window method, the following call fails, and SDL falls
icculus@3978
   514
			   back to pre-XIM keyboard handling.  */
icculus@1575
   515
			SDL_IC = pXCreateIC(SDL_IM,
icculus@1575
   516
					XNClientWindow, WMwindow,
icculus@1575
   517
					XNFocusWindow, WMwindow,
icculus@3978
   518
					XNInputStyle, XIMPreeditNothing | XIMStatusNothing,
icculus@1575
   519
					XNResourceName, classname,
icculus@1575
   520
					XNResourceClass, classname,
icculus@1575
   521
					NULL);
icculus@1575
   522
icculus@1575
   523
			if (SDL_IC == NULL) {
icculus@1575
   524
				SDL_SetError("no input context could be created");
icculus@1575
   525
				XCloseIM(SDL_IM);
icculus@1575
   526
				SDL_IM = NULL;
icculus@3978
   527
			} else {
icculus@3978
   528
				/* We need to receive X events that an IM wants and to pass
icculus@3978
   529
				   them to the IM through XFilterEvent. The set of events may
icculus@3978
   530
				   vary depending on the IM implementation and the options
icculus@3978
   531
				   specified through various routes. Although unlikely, the
icculus@3978
   532
				   xlib specification allows IM to change the event requirement
icculus@3978
   533
				   with its own circumstances, it is safe to call SelectInput
icculus@3978
   534
				   whenever we re-create an IC.  */
icculus@3978
   535
				unsigned long mask = 0;
icculus@3978
   536
				char *ret = pXGetICValues(SDL_IC, XNFilterEvents, &mask, NULL);
icculus@4014
   537
				if (ret != NULL) {
icculus@4014
   538
					XUnsetICFocus(SDL_IC);
icculus@4014
   539
					XDestroyIC(SDL_IC);
icculus@4014
   540
					SDL_IC = NULL;
icculus@4014
   541
					SDL_SetError("no input context could be created");
icculus@4014
   542
					XCloseIM(SDL_IM);
icculus@4014
   543
					SDL_IM = NULL;
icculus@4014
   544
				} else {
icculus@4014
   545
					XSelectInput(SDL_Display, WMwindow, app_event_mask | mask);
icculus@4014
   546
					XSetICFocus(SDL_IC);
icculus@4014
   547
				}
icculus@1575
   548
			}
icculus@1575
   549
		}
icculus@1178
   550
	}
icculus@1575
   551
	#endif
icculus@1178
   552
icculus@1575
   553
	/* Allow the window to be deleted by the window manager */
icculus@1575
   554
	XSetWMProtocols(SDL_Display, WMwindow, &WM_DELETE_WINDOW, 1);
slouken@0
   555
}
slouken@0
   556
slouken@0
   557
static int X11_VideoInit(_THIS, SDL_PixelFormat *vformat)
slouken@0
   558
{
slouken@4139
   559
	const char *env;
slouken@0
   560
	char *display;
slouken@0
   561
	int i;
slouken@0
   562
slouken@0
   563
	/* Open the X11 display */
slouken@0
   564
	display = NULL;		/* Get it from DISPLAY environment variable */
slouken@0
   565
icculus@1575
   566
	if ( (SDL_strncmp(XDisplayName(display), ":", 1) == 0) ||
icculus@1575
   567
	     (SDL_strncmp(XDisplayName(display), "unix:", 5) == 0) ) {
slouken@0
   568
		local_X11 = 1;
slouken@0
   569
	} else {
slouken@0
   570
		local_X11 = 0;
slouken@0
   571
	}
icculus@1575
   572
	SDL_Display = XOpenDisplay(display);
slouken@1361
   573
#if defined(__osf__) && defined(SDL_VIDEO_DRIVER_X11_DYNAMIC)
icculus@1299
   574
	/* On Tru64 if linking without -lX11, it fails and you get following message.
icculus@1299
   575
	 * Xlib: connection to ":0.0" refused by server
icculus@1299
   576
	 * Xlib: XDM authorization key matches an existing client!
icculus@1299
   577
	 *
icculus@1299
   578
	 * It succeeds if retrying 1 second later
icculus@1299
   579
	 * or if running xhost +localhost on shell.
icculus@1299
   580
	 *
icculus@1299
   581
	 */
icculus@1299
   582
	if ( SDL_Display == NULL ) {
icculus@1299
   583
		SDL_Delay(1000);
icculus@1575
   584
		SDL_Display = XOpenDisplay(display);
icculus@1299
   585
	}
icculus@1299
   586
#endif
slouken@0
   587
	if ( SDL_Display == NULL ) {
slouken@0
   588
		SDL_SetError("Couldn't open X11 display");
slouken@0
   589
		return(-1);
slouken@0
   590
	}
slouken@0
   591
#ifdef X11_DEBUG
icculus@1575
   592
	XSynchronize(SDL_Display, True);
slouken@0
   593
#endif
slouken@0
   594
slouken@0
   595
	/* Create an alternate X display for graphics updates -- allows us
slouken@0
   596
	   to do graphics updates in a separate thread from event handling.
slouken@0
   597
	   Thread-safe X11 doesn't seem to exist.
slouken@0
   598
	 */
icculus@1575
   599
	GFX_Display = XOpenDisplay(display);
slouken@0
   600
	if ( GFX_Display == NULL ) {
icculus@3964
   601
		XCloseDisplay(SDL_Display);
icculus@3964
   602
		SDL_Display = NULL;
slouken@0
   603
		SDL_SetError("Couldn't open X11 display");
slouken@0
   604
		return(-1);
slouken@0
   605
	}
slouken@0
   606
slouken@0
   607
	/* Set the normal X error handler */
icculus@1575
   608
	X_handler = XSetErrorHandler(x_errhandler);
slouken@0
   609
slouken@0
   610
	/* Set the error handler if we lose the X display */
icculus@1575
   611
	XIO_handler = XSetIOErrorHandler(xio_errhandler);
slouken@0
   612
icculus@1248
   613
	/* Set the X extension error handler */
icculus@1575
   614
	Xext_handler = XSetExtensionErrorHandler(xext_errhandler);
icculus@1248
   615
slouken@0
   616
	/* use default screen (from $DISPLAY) */
slouken@0
   617
	SDL_Screen = DefaultScreen(SDL_Display);
slouken@0
   618
slouken@0
   619
#ifndef NO_SHARED_MEMORY
slouken@0
   620
	/* Check for MIT shared memory extension */
slouken@556
   621
	use_mitshm = 0;
slouken@0
   622
	if ( local_X11 ) {
icculus@1575
   623
		use_mitshm = XShmQueryExtension(SDL_Display);
slouken@0
   624
	}
slouken@0
   625
#endif /* NO_SHARED_MEMORY */
slouken@0
   626
slouken@0
   627
	/* Get the available video modes */
icculus@3964
   628
	if(X11_GetVideoModes(this) < 0) {
icculus@3964
   629
		XCloseDisplay(GFX_Display);
icculus@3964
   630
		GFX_Display = NULL;
icculus@3964
   631
		XCloseDisplay(SDL_Display);
icculus@3964
   632
		SDL_Display = NULL;
slouken@0
   633
	    return -1;
icculus@3964
   634
	}
slouken@0
   635
slouken@1545
   636
	/* Determine the current screen size */
slouken@1545
   637
	this->info.current_w = DisplayWidth(SDL_Display, SDL_Screen);
slouken@1545
   638
	this->info.current_h = DisplayHeight(SDL_Display, SDL_Screen);
slouken@1545
   639
slouken@0
   640
	/* Determine the default screen depth:
slouken@0
   641
	   Use the default visual (or at least one with the same depth) */
slouken@0
   642
	SDL_DisplayColormap = DefaultColormap(SDL_Display, SDL_Screen);
slouken@0
   643
	for(i = 0; i < this->hidden->nvisuals; i++)
slouken@0
   644
	    if(this->hidden->visuals[i].depth == DefaultDepth(SDL_Display,
slouken@0
   645
							      SDL_Screen))
slouken@0
   646
		break;
slouken@0
   647
	if(i == this->hidden->nvisuals) {
slouken@0
   648
	    /* default visual was useless, take the deepest one instead */
slouken@0
   649
	    i = 0;
slouken@0
   650
	}
slouken@0
   651
	SDL_Visual = this->hidden->visuals[i].visual;
slouken@0
   652
	if ( SDL_Visual == DefaultVisual(SDL_Display, SDL_Screen) ) {
slouken@0
   653
	    SDL_XColorMap = SDL_DisplayColormap;
slouken@0
   654
	} else {
icculus@1575
   655
	    SDL_XColorMap = XCreateColormap(SDL_Display, SDL_Root,
slouken@0
   656
					    SDL_Visual, AllocNone);
slouken@0
   657
	}
slouken@0
   658
	this->hidden->depth = this->hidden->visuals[i].depth;
slouken@0
   659
	vformat->BitsPerPixel = this->hidden->visuals[i].bpp;
slouken@0
   660
	if ( vformat->BitsPerPixel > 8 ) {
slouken@0
   661
		vformat->Rmask = SDL_Visual->red_mask;
slouken@0
   662
	  	vformat->Gmask = SDL_Visual->green_mask;
slouken@0
   663
	  	vformat->Bmask = SDL_Visual->blue_mask;
slouken@0
   664
	}
slouken@1641
   665
	if ( this->hidden->depth == 32 ) {
slouken@1641
   666
		vformat->Amask = (0xFFFFFFFF & ~(vformat->Rmask|vformat->Gmask|vformat->Bmask));
slouken@1641
   667
	}
slouken@0
   668
	X11_SaveVidModeGamma(this);
slouken@0
   669
slouken@4124
   670
	/* Allow environment override of screensaver disable. */
slouken@4124
   671
	env = SDL_getenv("SDL_VIDEO_ALLOW_SCREENSAVER");
slouken@4139
   672
	if ( env ) {
slouken@4139
   673
		allow_screensaver = SDL_atoi(env);
slouken@4139
   674
	} else {
slouken@4139
   675
#ifdef SDL_VIDEO_DISABLE_SCREENSAVER
slouken@4139
   676
		allow_screensaver = 0;
slouken@4139
   677
#else
slouken@4139
   678
		allow_screensaver = 1;
slouken@4139
   679
#endif
slouken@4139
   680
	}
slouken@1783
   681
slouken@0
   682
	/* See if we have been passed a window to use */
slouken@1336
   683
	SDL_windowid = SDL_getenv("SDL_WINDOWID");
slouken@0
   684
slouken@0
   685
	/* Create the fullscreen and managed windows */
slouken@0
   686
	create_aux_windows(this);
slouken@0
   687
slouken@0
   688
	/* Create the blank cursor */
slouken@0
   689
	SDL_BlankCursor = this->CreateWMCursor(this, blank_cdata, blank_cmask,
slouken@0
   690
					BLANK_CWIDTH, BLANK_CHEIGHT,
slouken@0
   691
						BLANK_CHOTX, BLANK_CHOTY);
slouken@0
   692
slouken@0
   693
	/* Fill in some window manager capabilities */
slouken@0
   694
	this->info.wm_available = 1;
slouken@0
   695
slouken@0
   696
	/* We're done! */
icculus@1575
   697
	XFlush(SDL_Display);
slouken@0
   698
	return(0);
slouken@0
   699
}
slouken@0
   700
slouken@0
   701
static void X11_DestroyWindow(_THIS, SDL_Surface *screen)
slouken@0
   702
{
slouken@0
   703
	/* Clean up OpenGL */
slouken@0
   704
	if ( screen ) {
slouken@0
   705
		screen->flags &= ~(SDL_OPENGL|SDL_OPENGLBLIT);
slouken@0
   706
	}
slouken@0
   707
	X11_GL_Shutdown(this);
slouken@0
   708
slouken@0
   709
	if ( ! SDL_windowid ) {
slouken@0
   710
		/* Hide the managed window */
slouken@0
   711
		if ( WMwindow ) {
icculus@1575
   712
			XUnmapWindow(SDL_Display, WMwindow);
slouken@0
   713
		}
slouken@0
   714
		if ( screen && (screen->flags & SDL_FULLSCREEN) ) {
slouken@0
   715
			screen->flags &= ~SDL_FULLSCREEN;
slouken@0
   716
			X11_LeaveFullScreen(this);
slouken@0
   717
		}
slouken@0
   718
slouken@0
   719
		/* Destroy the output window */
slouken@0
   720
		if ( SDL_Window ) {
icculus@1575
   721
			XDestroyWindow(SDL_Display, SDL_Window);
slouken@0
   722
		}
slouken@0
   723
slouken@0
   724
		/* Free the colormap entries */
slouken@0
   725
		if ( SDL_XPixels ) {
slouken@0
   726
			int numcolors;
slouken@0
   727
			unsigned long pixel;
slouken@0
   728
			numcolors = SDL_Visual->map_entries;
slouken@0
   729
			for ( pixel=0; pixel<numcolors; ++pixel ) {
slouken@0
   730
				while ( SDL_XPixels[pixel] > 0 ) {
icculus@1575
   731
					XFreeColors(GFX_Display,
slouken@0
   732
						SDL_DisplayColormap,&pixel,1,0);
slouken@0
   733
					--SDL_XPixels[pixel];
slouken@0
   734
				}
slouken@0
   735
			}
slouken@1336
   736
			SDL_free(SDL_XPixels);
slouken@0
   737
			SDL_XPixels = NULL;
slouken@0
   738
		} 
slouken@0
   739
slouken@0
   740
		/* Free the graphics context */
slouken@0
   741
		if ( SDL_GC ) {
icculus@1575
   742
			XFreeGC(SDL_Display, SDL_GC);
slouken@0
   743
			SDL_GC = 0;
slouken@0
   744
		}
slouken@0
   745
	}
slouken@0
   746
}
slouken@0
   747
slouken@497
   748
static SDL_bool X11_WindowPosition(_THIS, int *x, int *y, int w, int h)
slouken@497
   749
{
slouken@1336
   750
	const char *window = SDL_getenv("SDL_VIDEO_WINDOW_POS");
slouken@1336
   751
	const char *center = SDL_getenv("SDL_VIDEO_CENTERED");
slouken@497
   752
	if ( window ) {
slouken@1336
   753
		if ( SDL_sscanf(window, "%d,%d", x, y) == 2 ) {
slouken@497
   754
			return SDL_TRUE;
slouken@497
   755
		}
slouken@1336
   756
		if ( SDL_strcmp(window, "center") == 0 ) {
slouken@497
   757
			center = window;
slouken@497
   758
		}
slouken@497
   759
	}
slouken@497
   760
	if ( center ) {
slouken@497
   761
		*x = (DisplayWidth(SDL_Display, SDL_Screen) - w)/2;
slouken@497
   762
		*y = (DisplayHeight(SDL_Display, SDL_Screen) - h)/2;
slouken@497
   763
		return SDL_TRUE;
slouken@497
   764
	}
slouken@497
   765
	return SDL_FALSE;
slouken@497
   766
}
slouken@497
   767
slouken@0
   768
static void X11_SetSizeHints(_THIS, int w, int h, Uint32 flags)
slouken@0
   769
{
slouken@0
   770
	XSizeHints *hints;
slouken@0
   771
icculus@1575
   772
	hints = XAllocSizeHints();
slouken@0
   773
	if ( hints ) {
slouken@4218
   774
		if (!(flags & SDL_RESIZABLE)) {
slouken@0
   775
			hints->min_width = hints->max_width = w;
slouken@0
   776
			hints->min_height = hints->max_height = h;
slouken@4218
   777
			hints->flags = PMaxSize | PMinSize;
slouken@0
   778
		}
slouken@0
   779
		if ( flags & SDL_FULLSCREEN ) {
slouken@0
   780
			hints->x = 0;
slouken@0
   781
			hints->y = 0;
slouken@0
   782
			hints->flags |= USPosition;
slouken@0
   783
		} else
slouken@0
   784
		/* Center it, if desired */
slouken@497
   785
		if ( X11_WindowPosition(this, &hints->x, &hints->y, w, h) ) {
slouken@0
   786
			hints->flags |= USPosition;
icculus@1575
   787
			XMoveWindow(SDL_Display, WMwindow, hints->x, hints->y);
slouken@0
   788
slouken@0
   789
			/* Flush the resize event so we don't catch it later */
icculus@1575
   790
			XSync(SDL_Display, True);
slouken@0
   791
		}
icculus@1575
   792
		XSetWMNormalHints(SDL_Display, WMwindow, hints);
icculus@1575
   793
		XFree(hints);
slouken@0
   794
	}
slouken@0
   795
slouken@0
   796
	/* Respect the window caption style */
slouken@0
   797
	if ( flags & SDL_NOFRAME ) {
slouken@0
   798
		SDL_bool set;
slouken@0
   799
		Atom WM_HINTS;
slouken@0
   800
slouken@0
   801
		/* We haven't modified the window manager hints yet */
slouken@0
   802
		set = SDL_FALSE;
slouken@0
   803
slouken@0
   804
		/* First try to set MWM hints */
icculus@1575
   805
		WM_HINTS = XInternAtom(SDL_Display, "_MOTIF_WM_HINTS", True);
slouken@0
   806
		if ( WM_HINTS != None ) {
slouken@0
   807
			/* Hints used by Motif compliant window managers */
slouken@0
   808
			struct {
slouken@0
   809
				unsigned long flags;
slouken@0
   810
				unsigned long functions;
slouken@0
   811
				unsigned long decorations;
slouken@0
   812
				long input_mode;
slouken@0
   813
				unsigned long status;
slouken@0
   814
			} MWMHints = { (1L << 1), 0, 0, 0, 0 };
slouken@0
   815
icculus@1575
   816
			XChangeProperty(SDL_Display, WMwindow,
slouken@0
   817
			                WM_HINTS, WM_HINTS, 32,
slouken@0
   818
			                PropModeReplace,
slouken@0
   819
					(unsigned char *)&MWMHints,
slouken@0
   820
					sizeof(MWMHints)/sizeof(long));
slouken@0
   821
			set = SDL_TRUE;
slouken@0
   822
		}
slouken@0
   823
		/* Now try to set KWM hints */
icculus@1575
   824
		WM_HINTS = XInternAtom(SDL_Display, "KWM_WIN_DECORATION", True);
slouken@0
   825
		if ( WM_HINTS != None ) {
slouken@0
   826
			long KWMHints = 0;
slouken@0
   827
icculus@1575
   828
			XChangeProperty(SDL_Display, WMwindow,
slouken@0
   829
			                WM_HINTS, WM_HINTS, 32,
slouken@0
   830
			                PropModeReplace,
slouken@0
   831
					(unsigned char *)&KWMHints,
slouken@0
   832
					sizeof(KWMHints)/sizeof(long));
slouken@0
   833
			set = SDL_TRUE;
slouken@0
   834
		}
slouken@0
   835
		/* Now try to set GNOME hints */
icculus@1575
   836
		WM_HINTS = XInternAtom(SDL_Display, "_WIN_HINTS", True);
slouken@0
   837
		if ( WM_HINTS != None ) {
slouken@0
   838
			long GNOMEHints = 0;
slouken@0
   839
icculus@1575
   840
			XChangeProperty(SDL_Display, WMwindow,
slouken@0
   841
			                WM_HINTS, WM_HINTS, 32,
slouken@0
   842
			                PropModeReplace,
slouken@0
   843
					(unsigned char *)&GNOMEHints,
slouken@0
   844
					sizeof(GNOMEHints)/sizeof(long));
slouken@0
   845
			set = SDL_TRUE;
slouken@0
   846
		}
slouken@0
   847
		/* Finally set the transient hints if necessary */
slouken@0
   848
		if ( ! set ) {
icculus@1575
   849
			XSetTransientForHint(SDL_Display, WMwindow, SDL_Root);
slouken@0
   850
		}
slouken@0
   851
	} else {
slouken@0
   852
		SDL_bool set;
slouken@0
   853
		Atom WM_HINTS;
slouken@0
   854
slouken@0
   855
		/* We haven't modified the window manager hints yet */
slouken@0
   856
		set = SDL_FALSE;
slouken@0
   857
slouken@0
   858
		/* First try to unset MWM hints */
icculus@1575
   859
		WM_HINTS = XInternAtom(SDL_Display, "_MOTIF_WM_HINTS", True);
slouken@0
   860
		if ( WM_HINTS != None ) {
icculus@1575
   861
			XDeleteProperty(SDL_Display, WMwindow, WM_HINTS);
slouken@0
   862
			set = SDL_TRUE;
slouken@0
   863
		}
slouken@0
   864
		/* Now try to unset KWM hints */
icculus@1575
   865
		WM_HINTS = XInternAtom(SDL_Display, "KWM_WIN_DECORATION", True);
slouken@0
   866
		if ( WM_HINTS != None ) {
icculus@1575
   867
			XDeleteProperty(SDL_Display, WMwindow, WM_HINTS);
slouken@0
   868
			set = SDL_TRUE;
slouken@0
   869
		}
slouken@0
   870
		/* Now try to unset GNOME hints */
icculus@1575
   871
		WM_HINTS = XInternAtom(SDL_Display, "_WIN_HINTS", True);
slouken@0
   872
		if ( WM_HINTS != None ) {
icculus@1575
   873
			XDeleteProperty(SDL_Display, WMwindow, WM_HINTS);
slouken@0
   874
			set = SDL_TRUE;
slouken@0
   875
		}
slouken@0
   876
		/* Finally unset the transient hints if necessary */
slouken@0
   877
		if ( ! set ) {
slouken@0
   878
			/* NOTE: Does this work? */
icculus@1575
   879
			XSetTransientForHint(SDL_Display, WMwindow, None);
slouken@0
   880
		}
slouken@0
   881
	}
slouken@0
   882
}
slouken@0
   883
slouken@0
   884
static int X11_CreateWindow(_THIS, SDL_Surface *screen,
slouken@0
   885
			    int w, int h, int bpp, Uint32 flags)
slouken@0
   886
{
slouken@0
   887
	int i, depth;
slouken@0
   888
	Visual *vis;
slouken@0
   889
	int vis_change;
slouken@1641
   890
	Uint32 Amask;
slouken@0
   891
slouken@0
   892
	/* If a window is already present, destroy it and start fresh */
slouken@0
   893
	if ( SDL_Window ) {
slouken@0
   894
		X11_DestroyWindow(this, screen);
icculus@860
   895
		switch_waiting = 0; /* Prevent jump back to now-meaningless state. */
slouken@0
   896
	}
slouken@0
   897
slouken@0
   898
	/* See if we have been given a window id */
slouken@0
   899
	if ( SDL_windowid ) {
slouken@1336
   900
		SDL_Window = SDL_strtol(SDL_windowid, NULL, 0);
slouken@0
   901
	} else {
slouken@0
   902
		SDL_Window = 0;
slouken@0
   903
	}
slouken@0
   904
slouken@0
   905
	/* find out which visual we are going to use */
slouken@0
   906
	if ( flags & SDL_OPENGL ) {
slouken@0
   907
		XVisualInfo *vi;
slouken@0
   908
slouken@0
   909
		vi = X11_GL_GetVisual(this);
slouken@0
   910
		if( !vi ) {
slouken@0
   911
			return -1;
slouken@0
   912
		}
slouken@0
   913
		vis = vi->visual;
slouken@0
   914
		depth = vi->depth;
slouken@0
   915
	} else if ( SDL_windowid ) {
slouken@0
   916
		XWindowAttributes a;
slouken@0
   917
icculus@1575
   918
		XGetWindowAttributes(SDL_Display, SDL_Window, &a);
slouken@0
   919
		vis = a.visual;
slouken@0
   920
		depth = a.depth;
slouken@0
   921
	} else {
slouken@0
   922
		for ( i = 0; i < this->hidden->nvisuals; i++ ) {
slouken@0
   923
			if ( this->hidden->visuals[i].bpp == bpp )
slouken@0
   924
				break;
slouken@0
   925
		}
slouken@0
   926
		if ( i == this->hidden->nvisuals ) {
slouken@0
   927
			SDL_SetError("No matching visual for requested depth");
slouken@0
   928
			return -1;	/* should never happen */
slouken@0
   929
		}
slouken@0
   930
		vis = this->hidden->visuals[i].visual;
slouken@0
   931
		depth = this->hidden->visuals[i].depth;
slouken@0
   932
	}
slouken@0
   933
#ifdef X11_DEBUG
slouken@0
   934
        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
   935
#endif
slouken@0
   936
	vis_change = (vis != SDL_Visual);
slouken@0
   937
	SDL_Visual = vis;
slouken@0
   938
	this->hidden->depth = depth;
slouken@0
   939
slouken@0
   940
	/* Allocate the new pixel format for this video mode */
slouken@1641
   941
	if ( this->hidden->depth == 32 ) {
slouken@1641
   942
		Amask = (0xFFFFFFFF & ~(vis->red_mask|vis->green_mask|vis->blue_mask));
slouken@1641
   943
	} else {
slouken@1641
   944
		Amask = 0;
slouken@1641
   945
	}
slouken@0
   946
	if ( ! SDL_ReallocFormat(screen, bpp,
slouken@1641
   947
			vis->red_mask, vis->green_mask, vis->blue_mask, Amask) ) {
slouken@0
   948
		return -1;
slouken@1641
   949
	}
slouken@0
   950
slouken@0
   951
	/* Create the appropriate colormap */
slouken@0
   952
	if ( SDL_XColorMap != SDL_DisplayColormap ) {
icculus@1575
   953
		XFreeColormap(SDL_Display, SDL_XColorMap);
slouken@0
   954
	}
slouken@0
   955
	if ( SDL_Visual->class == PseudoColor ) {
slouken@0
   956
	    int ncolors;
slouken@0
   957
slouken@0
   958
	    /* Allocate the pixel flags */
slouken@0
   959
	    ncolors = SDL_Visual->map_entries;
slouken@1336
   960
	    SDL_XPixels = SDL_malloc(ncolors * sizeof(int));
slouken@0
   961
	    if(SDL_XPixels == NULL) {
slouken@0
   962
		SDL_OutOfMemory();
slouken@0
   963
		return -1;
slouken@0
   964
	    }
slouken@1336
   965
	    SDL_memset(SDL_XPixels, 0, ncolors * sizeof(*SDL_XPixels));
slouken@0
   966
slouken@0
   967
	    /* always allocate a private colormap on non-default visuals */
slouken@0
   968
	    if ( SDL_Visual != DefaultVisual(SDL_Display, SDL_Screen) ) {
slouken@0
   969
		flags |= SDL_HWPALETTE;
slouken@0
   970
	    }
slouken@0
   971
	    if ( flags & SDL_HWPALETTE ) {
slouken@0
   972
		screen->flags |= SDL_HWPALETTE;
icculus@1575
   973
		SDL_XColorMap = XCreateColormap(SDL_Display, SDL_Root,
slouken@0
   974
		                                SDL_Visual, AllocAll);
slouken@0
   975
	    } else {
slouken@0
   976
		SDL_XColorMap = SDL_DisplayColormap;
slouken@0
   977
	    }
slouken@0
   978
	} else if ( SDL_Visual->class == DirectColor ) {
slouken@0
   979
slouken@0
   980
	    /* Create a colormap which we can manipulate for gamma */
icculus@1575
   981
	    SDL_XColorMap = XCreateColormap(SDL_Display, SDL_Root,
slouken@0
   982
		                            SDL_Visual, AllocAll);
icculus@1575
   983
            XSync(SDL_Display, False);
slouken@0
   984
slouken@0
   985
	    /* Initialize the colormap to the identity mapping */
slouken@0
   986
	    SDL_GetGammaRamp(0, 0, 0);
slouken@0
   987
	    this->screen = screen;
slouken@0
   988
	    X11_SetGammaRamp(this, this->gamma);
slouken@0
   989
	    this->screen = NULL;
slouken@0
   990
	} else {
slouken@0
   991
	    /* Create a read-only colormap for our window */
icculus@1575
   992
	    SDL_XColorMap = XCreateColormap(SDL_Display, SDL_Root,
slouken@0
   993
	                                    SDL_Visual, AllocNone);
slouken@0
   994
	}
slouken@0
   995
slouken@0
   996
	/* Recreate the auxiliary windows, if needed (required for GL) */
slouken@0
   997
	if ( vis_change )
slouken@0
   998
	    create_aux_windows(this);
slouken@0
   999
slouken@0
  1000
	if(screen->flags & SDL_HWPALETTE) {
slouken@0
  1001
	    /* Since the full-screen window might have got a nonzero background
slouken@0
  1002
	       colour (0 is white on some displays), we should reset the
slouken@0
  1003
	       background to 0 here since that is what the user expects
slouken@0
  1004
	       with a private colormap */
icculus@1575
  1005
	    XSetWindowBackground(SDL_Display, FSwindow, 0);
icculus@1575
  1006
	    XClearWindow(SDL_Display, FSwindow);
slouken@0
  1007
	}
slouken@0
  1008
slouken@0
  1009
	/* resize the (possibly new) window manager window */
slouken@0
  1010
	if( !SDL_windowid ) {
slouken@0
  1011
	        X11_SetSizeHints(this, w, h, flags);
slouken@1545
  1012
		window_w = w;
slouken@1545
  1013
		window_h = h;
icculus@1575
  1014
		XResizeWindow(SDL_Display, WMwindow, w, h);
slouken@0
  1015
	}
slouken@0
  1016
slouken@0
  1017
	/* Create (or use) the X11 display window */
slouken@0
  1018
	if ( !SDL_windowid ) {
slouken@0
  1019
		if ( flags & SDL_OPENGL ) {
slouken@0
  1020
			if ( X11_GL_CreateWindow(this, w, h) < 0 ) {
slouken@0
  1021
				return(-1);
slouken@0
  1022
			}
slouken@0
  1023
		} else {
slouken@0
  1024
			XSetWindowAttributes swa;
slouken@0
  1025
slouken@0
  1026
			swa.background_pixel = 0;
slouken@0
  1027
			swa.border_pixel = 0;
slouken@0
  1028
			swa.colormap = SDL_XColorMap;
icculus@1575
  1029
			SDL_Window = XCreateWindow(SDL_Display, WMwindow,
slouken@0
  1030
		                           	0, 0, w, h, 0, depth,
slouken@0
  1031
		                           	InputOutput, SDL_Visual,
slouken@0
  1032
		                           	CWBackPixel | CWBorderPixel
slouken@0
  1033
		                           	| CWColormap, &swa);
slouken@0
  1034
		}
slouken@0
  1035
		/* Only manage our input if we own the window */
icculus@1575
  1036
		XSelectInput(SDL_Display, SDL_Window,
slouken@0
  1037
					( EnterWindowMask | LeaveWindowMask
slouken@0
  1038
					| ButtonPressMask | ButtonReleaseMask
slouken@0
  1039
					| PointerMotionMask | ExposureMask ));
slouken@0
  1040
	}
slouken@0
  1041
	/* Create the graphics context here, once we have a window */
slouken@0
  1042
	if ( flags & SDL_OPENGL ) {
slouken@0
  1043
		if ( X11_GL_CreateContext(this) < 0 ) {
slouken@0
  1044
			return(-1);
slouken@0
  1045
		} else {
slouken@0
  1046
			screen->flags |= SDL_OPENGL;
slouken@0
  1047
		}
slouken@0
  1048
	} else {
slouken@0
  1049
		XGCValues gcv;
slouken@0
  1050
slouken@0
  1051
		gcv.graphics_exposures = False;
icculus@1575
  1052
		SDL_GC = XCreateGC(SDL_Display, SDL_Window,
slouken@0
  1053
		                   GCGraphicsExposures, &gcv);
slouken@0
  1054
		if ( ! SDL_GC ) {
slouken@0
  1055
			SDL_SetError("Couldn't create graphics context");
slouken@0
  1056
			return(-1);
slouken@0
  1057
		}
slouken@0
  1058
	}
slouken@0
  1059
slouken@0
  1060
	/* Set our colormaps when not setting a GL mode */
slouken@0
  1061
	if ( ! (flags & SDL_OPENGL) ) {
icculus@1575
  1062
		XSetWindowColormap(SDL_Display, SDL_Window, SDL_XColorMap);
slouken@0
  1063
		if( !SDL_windowid ) {
icculus@1575
  1064
		    XSetWindowColormap(SDL_Display, FSwindow, SDL_XColorMap);
icculus@1575
  1065
		    XSetWindowColormap(SDL_Display, WMwindow, SDL_XColorMap);
slouken@0
  1066
		}
slouken@0
  1067
	}
slouken@0
  1068
slouken@0
  1069
#if 0 /* This is an experiment - are the graphics faster now? - nope. */
slouken@1336
  1070
	if ( SDL_getenv("SDL_VIDEO_X11_BACKINGSTORE") )
slouken@0
  1071
#endif
slouken@0
  1072
	/* Cache the window in the server, when possible */
slouken@0
  1073
	{
slouken@0
  1074
		Screen *xscreen;
slouken@0
  1075
		XSetWindowAttributes a;
slouken@0
  1076
slouken@0
  1077
		xscreen = ScreenOfDisplay(SDL_Display, SDL_Screen);
slouken@0
  1078
		a.backing_store = DoesBackingStore(xscreen);
slouken@0
  1079
		if ( a.backing_store != NotUseful ) {
icculus@1575
  1080
			XChangeWindowAttributes(SDL_Display, SDL_Window,
slouken@0
  1081
			                        CWBackingStore, &a);
slouken@0
  1082
		}
slouken@0
  1083
	}
slouken@0
  1084
slouken@0
  1085
	/* Map them both and go fullscreen, if requested */
slouken@0
  1086
	if ( ! SDL_windowid ) {
icculus@1575
  1087
		XMapWindow(SDL_Display, SDL_Window);
icculus@1575
  1088
		XMapWindow(SDL_Display, WMwindow);
slouken@160
  1089
		X11_WaitMapped(this, WMwindow);
slouken@0
  1090
		if ( flags & SDL_FULLSCREEN ) {
slouken@0
  1091
			screen->flags |= SDL_FULLSCREEN;
slouken@0
  1092
			X11_EnterFullScreen(this);
slouken@0
  1093
		} else {
slouken@0
  1094
			screen->flags &= ~SDL_FULLSCREEN;
slouken@0
  1095
		}
slouken@0
  1096
	}
icculus@1178
  1097
	
slouken@0
  1098
	return(0);
slouken@0
  1099
}
slouken@0
  1100
slouken@0
  1101
static int X11_ResizeWindow(_THIS,
slouken@0
  1102
			SDL_Surface *screen, int w, int h, Uint32 flags)
slouken@0
  1103
{
slouken@0
  1104
	if ( ! SDL_windowid ) {
slouken@0
  1105
		/* Resize the window manager window */
slouken@0
  1106
		X11_SetSizeHints(this, w, h, flags);
slouken@1545
  1107
		window_w = w;
slouken@1545
  1108
		window_h = h;
icculus@1575
  1109
		XResizeWindow(SDL_Display, WMwindow, w, h);
slouken@0
  1110
slouken@0
  1111
		/* Resize the fullscreen and display windows */
slouken@0
  1112
		if ( flags & SDL_FULLSCREEN ) {
slouken@0
  1113
			if ( screen->flags & SDL_FULLSCREEN ) {
slouken@0
  1114
				X11_ResizeFullScreen(this);
slouken@0
  1115
			} else {
slouken@0
  1116
				screen->flags |= SDL_FULLSCREEN;
slouken@0
  1117
				X11_EnterFullScreen(this);
slouken@0
  1118
			}
slouken@0
  1119
		} else {
slouken@0
  1120
			if ( screen->flags & SDL_FULLSCREEN ) {
slouken@0
  1121
				screen->flags &= ~SDL_FULLSCREEN;
slouken@0
  1122
				X11_LeaveFullScreen(this);
slouken@0
  1123
			}
slouken@0
  1124
		}
icculus@1575
  1125
		XResizeWindow(SDL_Display, SDL_Window, w, h);
slouken@0
  1126
	}
slouken@0
  1127
	return(0);
slouken@0
  1128
}
slouken@0
  1129
slouken@0
  1130
SDL_Surface *X11_SetVideoMode(_THIS, SDL_Surface *current,
slouken@0
  1131
				int width, int height, int bpp, Uint32 flags)
slouken@0
  1132
{
slouken@0
  1133
	Uint32 saved_flags;
slouken@0
  1134
slouken@0
  1135
	/* Lock the event thread, in multi-threading environments */
slouken@0
  1136
	SDL_Lock_EventThread();
slouken@0
  1137
slouken@0
  1138
	/* Check the combination of flags we were passed */
slouken@0
  1139
	if ( flags & SDL_FULLSCREEN ) {
slouken@0
  1140
		/* Clear fullscreen flag if not supported */
slouken@0
  1141
		if ( SDL_windowid ) {
slouken@0
  1142
			flags &= ~SDL_FULLSCREEN;
slouken@0
  1143
		}
slouken@0
  1144
	}
slouken@0
  1145
slouken@0
  1146
	/* Flush any delayed updates */
icculus@1575
  1147
	XSync(GFX_Display, False);
slouken@0
  1148
slouken@0
  1149
	/* Set up the X11 window */
slouken@0
  1150
	saved_flags = current->flags;
icculus@867
  1151
	if ( (SDL_Window) && ((saved_flags&SDL_OPENGL) == (flags&SDL_OPENGL))
icculus@867
  1152
	      && (bpp == current->format->BitsPerPixel)
icculus@867
  1153
          && ((saved_flags&SDL_NOFRAME) == (flags&SDL_NOFRAME)) ) {
slouken@0
  1154
		if (X11_ResizeWindow(this, current, width, height, flags) < 0) {
slouken@0
  1155
			current = NULL;
slouken@0
  1156
			goto done;
slouken@0
  1157
		}
slouken@0
  1158
	} else {
slouken@0
  1159
		if (X11_CreateWindow(this,current,width,height,bpp,flags) < 0) {
slouken@0
  1160
			current = NULL;
slouken@0
  1161
			goto done;
slouken@0
  1162
		}
slouken@0
  1163
	}
slouken@0
  1164
slouken@4008
  1165
	/* Update the internal keyboard state */
slouken@4008
  1166
	X11_SetKeyboardState(SDL_Display, NULL);
slouken@4008
  1167
slouken@4008
  1168
	/* When the window is first mapped, ignore non-modifier keys */
slouken@4008
  1169
	if ( !current->w && !current->h ) {
slouken@4008
  1170
		Uint8 *keys = SDL_GetKeyState(NULL);
slouken@4008
  1171
		int i;
slouken@4008
  1172
		for ( i = 0; i < SDLK_LAST; ++i ) {
slouken@4008
  1173
			switch (i) {
slouken@4008
  1174
			    case SDLK_NUMLOCK:
slouken@4008
  1175
			    case SDLK_CAPSLOCK:
slouken@4008
  1176
			    case SDLK_LCTRL:
slouken@4008
  1177
			    case SDLK_RCTRL:
slouken@4008
  1178
			    case SDLK_LSHIFT:
slouken@4008
  1179
			    case SDLK_RSHIFT:
slouken@4008
  1180
			    case SDLK_LALT:
slouken@4008
  1181
			    case SDLK_RALT:
slouken@4008
  1182
			    case SDLK_LMETA:
slouken@4008
  1183
			    case SDLK_RMETA:
slouken@4008
  1184
			    case SDLK_MODE:
slouken@4008
  1185
				break;
slouken@4008
  1186
			    default:
slouken@4008
  1187
				keys[i] = SDL_RELEASED;
slouken@4008
  1188
				break;
slouken@4008
  1189
			}
slouken@4008
  1190
		}
slouken@4008
  1191
	}
slouken@4008
  1192
slouken@0
  1193
	/* Set up the new mode framebuffer */
slouken@0
  1194
	if ( ((current->w != width) || (current->h != height)) ||
slouken@0
  1195
             ((saved_flags&SDL_OPENGL) != (flags&SDL_OPENGL)) ) {
slouken@0
  1196
		current->w = width;
slouken@0
  1197
		current->h = height;
slouken@0
  1198
		current->pitch = SDL_CalculatePitch(current);
slouken@4253
  1199
		if (X11_ResizeImage(this, current, flags) < 0) {
slouken@4253
  1200
			current = NULL;
slouken@4253
  1201
			goto done;
slouken@4253
  1202
		}
slouken@0
  1203
	}
icculus@4016
  1204
icculus@4016
  1205
	/* Clear these flags and set them only if they are in the new set. */
icculus@4016
  1206
	current->flags &= ~(SDL_RESIZABLE|SDL_NOFRAME);
slouken@0
  1207
	current->flags |= (flags&(SDL_RESIZABLE|SDL_NOFRAME));
slouken@0
  1208
slouken@0
  1209
  done:
slouken@0
  1210
	/* Release the event thread */
icculus@1575
  1211
	XSync(SDL_Display, False);
slouken@0
  1212
	SDL_Unlock_EventThread();
slouken@0
  1213
slouken@0
  1214
	/* We're done! */
slouken@0
  1215
	return(current);
slouken@0
  1216
}
slouken@0
  1217
slouken@0
  1218
static int X11_ToggleFullScreen(_THIS, int on)
slouken@0
  1219
{
slouken@0
  1220
	Uint32 event_thread;
slouken@0
  1221
slouken@0
  1222
	/* Don't switch if we don't own the window */
slouken@0
  1223
	if ( SDL_windowid ) {
slouken@0
  1224
		return(0);
slouken@0
  1225
	}
slouken@0
  1226
slouken@0
  1227
	/* Don't lock if we are the event thread */
slouken@0
  1228
	event_thread = SDL_EventThreadID();
slouken@0
  1229
	if ( event_thread && (SDL_ThreadID() == event_thread) ) {
slouken@0
  1230
		event_thread = 0;
slouken@0
  1231
	}
slouken@0
  1232
	if ( event_thread ) {
slouken@0
  1233
		SDL_Lock_EventThread();
slouken@0
  1234
	}
slouken@0
  1235
	if ( on ) {
slouken@0
  1236
		this->screen->flags |= SDL_FULLSCREEN;
slouken@0
  1237
		X11_EnterFullScreen(this);
slouken@0
  1238
	} else {
slouken@0
  1239
		this->screen->flags &= ~SDL_FULLSCREEN;
slouken@0
  1240
		X11_LeaveFullScreen(this);
slouken@0
  1241
	}
slouken@0
  1242
	X11_RefreshDisplay(this);
slouken@0
  1243
	if ( event_thread ) {
slouken@0
  1244
		SDL_Unlock_EventThread();
slouken@0
  1245
	}
slouken@0
  1246
	SDL_ResetKeyboard();
slouken@0
  1247
	return(1);
slouken@0
  1248
}
slouken@0
  1249
slouken@0
  1250
/* Update the current mouse state and position */
slouken@0
  1251
static void X11_UpdateMouse(_THIS)
slouken@0
  1252
{
slouken@0
  1253
	Window u1; int u2;
slouken@0
  1254
	Window current_win;
slouken@0
  1255
	int x, y;
slouken@0
  1256
	unsigned int mask;
slouken@0
  1257
slouken@0
  1258
	/* Lock the event thread, in multi-threading environments */
slouken@0
  1259
	SDL_Lock_EventThread();
icculus@1575
  1260
	if ( XQueryPointer(SDL_Display, SDL_Window, &u1, &current_win,
slouken@0
  1261
	                   &u2, &u2, &x, &y, &mask) ) {
slouken@0
  1262
		if ( (x >= 0) && (x < SDL_VideoSurface->w) &&
slouken@0
  1263
		     (y >= 0) && (y < SDL_VideoSurface->h) ) {
slouken@0
  1264
			SDL_PrivateAppActive(1, SDL_APPMOUSEFOCUS);
slouken@0
  1265
			SDL_PrivateMouseMotion(0, 0, x, y);
slouken@0
  1266
		} else {
slouken@0
  1267
			SDL_PrivateAppActive(0, SDL_APPMOUSEFOCUS);
slouken@0
  1268
		}
slouken@0
  1269
	}
slouken@0
  1270
	SDL_Unlock_EventThread();
slouken@0
  1271
}
slouken@0
  1272
slouken@0
  1273
/* simple colour distance metric. Supposed to be better than a plain
slouken@0
  1274
   Euclidian distance anyway. */
slouken@0
  1275
#define COLOUR_FACTOR 3
slouken@0
  1276
#define LIGHT_FACTOR 1
slouken@0
  1277
#define COLOUR_DIST(r1, g1, b1, r2, g2, b2)				\
slouken@0
  1278
	(COLOUR_FACTOR * (abs(r1 - r2) + abs(g1 - g2) + abs(b1 - b2))	\
slouken@0
  1279
	 + LIGHT_FACTOR * abs(r1 + g1 + b1 - (r2 + g2 + b2)))
slouken@0
  1280
slouken@0
  1281
static void allocate_nearest(_THIS, SDL_Color *colors,
slouken@0
  1282
			     SDL_Color *want, int nwant)
slouken@0
  1283
{
slouken@0
  1284
	/*
slouken@0
  1285
	 * There is no way to know which ones to choose from, so we retrieve
slouken@0
  1286
	 * the entire colormap and try the nearest possible, until we find one
slouken@0
  1287
	 * that is shared.
slouken@0
  1288
	 */
slouken@0
  1289
	XColor all[256];
slouken@0
  1290
	int i;
slouken@0
  1291
	for(i = 0; i < 256; i++)
slouken@0
  1292
		all[i].pixel = i;
slouken@0
  1293
	/* 
slouken@0
  1294
	 * XQueryColors sets the flags in the XColor struct, so we use
slouken@0
  1295
	 * that to keep track of which colours are available
slouken@0
  1296
	 */
icculus@1575
  1297
	XQueryColors(GFX_Display, SDL_XColorMap, all, 256);
slouken@0
  1298
slouken@0
  1299
	for(i = 0; i < nwant; i++) {
slouken@0
  1300
		XColor *c;
slouken@0
  1301
		int j;
slouken@0
  1302
		int best = 0;
slouken@0
  1303
		int mindist = 0x7fffffff;
slouken@0
  1304
		int ri = want[i].r;
slouken@0
  1305
		int gi = want[i].g;
slouken@0
  1306
		int bi = want[i].b;
slouken@0
  1307
		for(j = 0; j < 256; j++) {
slouken@0
  1308
			int rj, gj, bj, d2;
slouken@0
  1309
			if(!all[j].flags)
slouken@0
  1310
				continue;	/* unavailable colour cell */
slouken@0
  1311
			rj = all[j].red >> 8;
slouken@0
  1312
			gj = all[j].green >> 8;
slouken@0
  1313
			bj = all[j].blue >> 8;
slouken@0
  1314
			d2 = COLOUR_DIST(ri, gi, bi, rj, gj, bj);
slouken@0
  1315
			if(d2 < mindist) {
slouken@0
  1316
				mindist = d2;
slouken@0
  1317
				best = j;
slouken@0
  1318
			}
slouken@0
  1319
		}
slouken@0
  1320
		if(SDL_XPixels[best])
slouken@0
  1321
			continue; /* already allocated, waste no more time */
slouken@0
  1322
		c = all + best;
icculus@1575
  1323
		if(XAllocColor(GFX_Display, SDL_XColorMap, c)) {
slouken@0
  1324
			/* got it */
slouken@236
  1325
			colors[c->pixel].r = c->red >> 8;
slouken@236
  1326
			colors[c->pixel].g = c->green >> 8;
slouken@236
  1327
			colors[c->pixel].b = c->blue >> 8;
slouken@236
  1328
			++SDL_XPixels[c->pixel];
slouken@0
  1329
		} else {
slouken@0
  1330
			/* 
slouken@0
  1331
			 * The colour couldn't be allocated, probably being
slouken@0
  1332
			 * owned as a r/w cell by another client. Flag it as
slouken@0
  1333
			 * unavailable and try again. The termination of the
slouken@0
  1334
			 * loop is guaranteed since at least black and white
slouken@0
  1335
			 * are always there.
slouken@0
  1336
			 */
slouken@0
  1337
			c->flags = 0;
slouken@0
  1338
			i--;
slouken@0
  1339
		}
slouken@0
  1340
	}
slouken@0
  1341
}
slouken@0
  1342
slouken@0
  1343
int X11_SetColors(_THIS, int firstcolor, int ncolors, SDL_Color *colors)
slouken@0
  1344
{
slouken@0
  1345
	int nrej = 0;
slouken@0
  1346
slouken@0
  1347
	/* Check to make sure we have a colormap allocated */
slouken@0
  1348
	if ( SDL_XPixels == NULL ) {
slouken@0
  1349
		return(0);
slouken@0
  1350
	}
slouken@0
  1351
	if ( (this->screen->flags & SDL_HWPALETTE) == SDL_HWPALETTE ) {
slouken@0
  1352
	        /* private writable colormap: just set the colours we need */
slouken@0
  1353
	        XColor  *xcmap;
slouken@0
  1354
		int i;
slouken@1338
  1355
	        xcmap = SDL_stack_alloc(XColor, ncolors);
slouken@0
  1356
		if(xcmap == NULL)
slouken@0
  1357
		        return 0;
slouken@0
  1358
		for ( i=0; i<ncolors; ++i ) {
slouken@0
  1359
			xcmap[i].pixel = i + firstcolor;
slouken@0
  1360
			xcmap[i].red   = (colors[i].r<<8)|colors[i].r;
slouken@0
  1361
			xcmap[i].green = (colors[i].g<<8)|colors[i].g;
slouken@0
  1362
			xcmap[i].blue  = (colors[i].b<<8)|colors[i].b;
slouken@0
  1363
			xcmap[i].flags = (DoRed|DoGreen|DoBlue);
slouken@0
  1364
		}
icculus@1575
  1365
		XStoreColors(GFX_Display, SDL_XColorMap, xcmap, ncolors);
icculus@1575
  1366
		XSync(GFX_Display, False);
slouken@1338
  1367
		SDL_stack_free(xcmap);
slouken@0
  1368
	} else {
slouken@0
  1369
	        /*
slouken@0
  1370
		 * Shared colormap: We only allocate read-only cells, which
slouken@0
  1371
		 * increases the likelyhood of colour sharing with other
slouken@0
  1372
		 * clients. The pixel values will almost certainly be
slouken@0
  1373
		 * different from the requested ones, so the user has to
slouken@0
  1374
		 * walk the colormap and see which index got what colour.
slouken@0
  1375
		 *
slouken@0
  1376
		 * We can work directly with the logical palette since it
slouken@0
  1377
		 * has already been set when we get here.
slouken@0
  1378
		 */
slouken@0
  1379
		SDL_Color *want, *reject;
slouken@0
  1380
	        unsigned long *freelist;
slouken@0
  1381
		int i;
slouken@0
  1382
		int nfree = 0;
slouken@0
  1383
		int nc = this->screen->format->palette->ncolors;
slouken@0
  1384
	        colors = this->screen->format->palette->colors;
slouken@1338
  1385
		freelist = SDL_stack_alloc(unsigned long, nc);
slouken@0
  1386
		/* make sure multiple allocations of the same cell are freed */
slouken@0
  1387
	        for(i = 0; i < ncolors; i++) {
slouken@0
  1388
		        int pixel = firstcolor + i;
slouken@0
  1389
		        while(SDL_XPixels[pixel]) {
slouken@0
  1390
			        freelist[nfree++] = pixel;
slouken@0
  1391
				--SDL_XPixels[pixel];
slouken@0
  1392
			}
slouken@0
  1393
		}
icculus@1575
  1394
		XFreeColors(GFX_Display, SDL_XColorMap, freelist, nfree, 0);
slouken@1338
  1395
		SDL_stack_free(freelist);
slouken@0
  1396
slouken@1338
  1397
		want = SDL_stack_alloc(SDL_Color, ncolors);
slouken@1338
  1398
		reject = SDL_stack_alloc(SDL_Color, ncolors);
slouken@1336
  1399
		SDL_memcpy(want, colors + firstcolor, ncolors * sizeof(SDL_Color));
slouken@0
  1400
		/* make sure the user isn't fooled by her own wishes
slouken@0
  1401
		   (black is safe, always available in the default colormap) */
slouken@1336
  1402
		SDL_memset(colors + firstcolor, 0, ncolors * sizeof(SDL_Color));
slouken@0
  1403
slouken@0
  1404
		/* now try to allocate the colours */
slouken@0
  1405
		for(i = 0; i < ncolors; i++) {
slouken@0
  1406
		        XColor col;
slouken@0
  1407
			col.red = want[i].r << 8;
slouken@0
  1408
			col.green = want[i].g << 8;
slouken@0
  1409
			col.blue = want[i].b << 8;
slouken@0
  1410
			col.flags = DoRed | DoGreen | DoBlue;
icculus@1575
  1411
			if(XAllocColor(GFX_Display, SDL_XColorMap, &col)) {
slouken@0
  1412
			        /* We got the colour, or at least the nearest
slouken@0
  1413
				   the hardware could get. */
slouken@0
  1414
			        colors[col.pixel].r = col.red >> 8;
slouken@0
  1415
				colors[col.pixel].g = col.green >> 8;
slouken@0
  1416
				colors[col.pixel].b = col.blue >> 8;
slouken@0
  1417
				++SDL_XPixels[col.pixel];
slouken@0
  1418
			} else {
slouken@0
  1419
				/*
slouken@0
  1420
				 * no more free cells, add it to the list
slouken@0
  1421
				 * of rejected colours
slouken@0
  1422
				 */
slouken@0
  1423
				reject[nrej++] = want[i];
slouken@0
  1424
			}
slouken@0
  1425
		}
slouken@0
  1426
		if(nrej)
slouken@0
  1427
			allocate_nearest(this, colors, reject, nrej);
slouken@1338
  1428
		SDL_stack_free(reject);
slouken@1338
  1429
		SDL_stack_free(want);
slouken@0
  1430
	}
slouken@0
  1431
	return nrej == 0;
slouken@0
  1432
}
slouken@0
  1433
slouken@0
  1434
int X11_SetGammaRamp(_THIS, Uint16 *ramp)
slouken@0
  1435
{
slouken@0
  1436
	int i, ncolors;
slouken@0
  1437
	XColor xcmap[256];
slouken@0
  1438
slouken@0
  1439
	/* See if actually setting the gamma is supported */
slouken@0
  1440
	if ( SDL_Visual->class != DirectColor ) {
slouken@0
  1441
	    SDL_SetError("Gamma correction not supported on this visual");
slouken@0
  1442
	    return(-1);
slouken@0
  1443
	}
slouken@0
  1444
slouken@0
  1445
	/* Calculate the appropriate palette for the given gamma ramp */
slouken@0
  1446
	ncolors = SDL_Visual->map_entries;
slouken@0
  1447
	for ( i=0; i<ncolors; ++i ) {
slouken@0
  1448
		Uint8 c = (256 * i / ncolors);
slouken@0
  1449
		xcmap[i].pixel = SDL_MapRGB(this->screen->format, c, c, c);
slouken@0
  1450
		xcmap[i].red   = ramp[0*256+c];
slouken@0
  1451
		xcmap[i].green = ramp[1*256+c];
slouken@0
  1452
		xcmap[i].blue  = ramp[2*256+c];
slouken@0
  1453
		xcmap[i].flags = (DoRed|DoGreen|DoBlue);
slouken@0
  1454
	}
icculus@1575
  1455
	XStoreColors(GFX_Display, SDL_XColorMap, xcmap, ncolors);
icculus@1575
  1456
	XSync(GFX_Display, False);
slouken@0
  1457
	return(0);
slouken@0
  1458
}
slouken@0
  1459
slouken@0
  1460
/* Note:  If we are terminated, this could be called in the middle of
slouken@0
  1461
   another SDL video routine -- notably UpdateRects.
slouken@0
  1462
*/
slouken@0
  1463
void X11_VideoQuit(_THIS)
slouken@0
  1464
{
slouken@0
  1465
	/* Shutdown everything that's still up */
slouken@0
  1466
	/* The event thread should be done, so we can touch SDL_Display */
slouken@0
  1467
	if ( SDL_Display != NULL ) {
slouken@0
  1468
		/* Flush any delayed updates */
icculus@1575
  1469
		XSync(GFX_Display, False);
slouken@0
  1470
icculus@1178
  1471
		/* Close the connection with the IM server */
icculus@1178
  1472
		#ifdef X_HAVE_UTF8_STRING
icculus@1319
  1473
		if (SDL_IC != NULL) {
icculus@3978
  1474
			XUnsetICFocus(SDL_IC);
icculus@1575
  1475
			XDestroyIC(SDL_IC);
icculus@1178
  1476
			SDL_IC = NULL;
icculus@1178
  1477
		}
icculus@1319
  1478
		if (SDL_IM != NULL) {
icculus@1575
  1479
			XCloseIM(SDL_IM);
icculus@1178
  1480
			SDL_IM = NULL;
icculus@1178
  1481
		}
icculus@1178
  1482
		#endif
icculus@1178
  1483
slouken@0
  1484
		/* Start shutting down the windows */
slouken@0
  1485
		X11_DestroyImage(this, this->screen);
slouken@0
  1486
		X11_DestroyWindow(this, this->screen);
slouken@0
  1487
		X11_FreeVideoModes(this);
slouken@0
  1488
		if ( SDL_XColorMap != SDL_DisplayColormap ) {
icculus@1575
  1489
			XFreeColormap(SDL_Display, SDL_XColorMap);
slouken@0
  1490
		}
slouken@0
  1491
		if ( SDL_iconcolors ) {
slouken@0
  1492
			unsigned long pixel;
slouken@236
  1493
			Colormap dcmap = DefaultColormap(SDL_Display,
slouken@236
  1494
							 SDL_Screen);
slouken@236
  1495
			for(pixel = 0; pixel < 256; ++pixel) {
slouken@236
  1496
				while(SDL_iconcolors[pixel] > 0) {
icculus@1575
  1497
					XFreeColors(GFX_Display,
slouken@236
  1498
						    dcmap, &pixel, 1, 0);
slouken@0
  1499
					--SDL_iconcolors[pixel];
slouken@0
  1500
				}
slouken@0
  1501
			}
slouken@1336
  1502
			SDL_free(SDL_iconcolors);
slouken@0
  1503
			SDL_iconcolors = NULL;
slouken@0
  1504
		} 
slouken@1783
  1505
slouken@0
  1506
		/* Restore gamma settings if they've changed */
slouken@0
  1507
		if ( SDL_GetAppState() & SDL_APPACTIVE ) {
slouken@0
  1508
			X11_SwapVidModeGamma(this);
slouken@0
  1509
		}
slouken@0
  1510
slouken@0
  1511
		/* Free that blank cursor */
slouken@0
  1512
		if ( SDL_BlankCursor != NULL ) {
slouken@0
  1513
			this->FreeWMCursor(this, SDL_BlankCursor);
slouken@0
  1514
			SDL_BlankCursor = NULL;
slouken@0
  1515
		}
slouken@0
  1516
slouken@0
  1517
		/* Close the X11 graphics connection */
slouken@0
  1518
		if ( GFX_Display != NULL ) {
icculus@1575
  1519
			XCloseDisplay(GFX_Display);
slouken@0
  1520
			GFX_Display = NULL;
slouken@0
  1521
		}
slouken@0
  1522
slouken@0
  1523
		/* Close the X11 display connection */
icculus@1575
  1524
		XCloseDisplay(SDL_Display);
slouken@0
  1525
		SDL_Display = NULL;
slouken@0
  1526
slouken@0
  1527
		/* Reset the X11 error handlers */
slouken@0
  1528
		if ( XIO_handler ) {
icculus@1575
  1529
			XSetIOErrorHandler(XIO_handler);
slouken@0
  1530
		}
slouken@0
  1531
		if ( X_handler ) {
icculus@1575
  1532
			XSetErrorHandler(X_handler);
slouken@0
  1533
		}
slouken@0
  1534
slouken@0
  1535
		/* Unload GL library after X11 shuts down */
slouken@0
  1536
		X11_GL_UnloadLibrary(this);
slouken@0
  1537
	}
slouken@0
  1538
	if ( this->screen && (this->screen->flags & SDL_HWSURFACE) ) {
slouken@0
  1539
		/* Direct screen access, no memory buffer */
slouken@0
  1540
		this->screen->pixels = NULL;
slouken@0
  1541
	}
icculus@3885
  1542
icculus@3885
  1543
#if SDL_VIDEO_DRIVER_X11_XME
icculus@3885
  1544
    XiGMiscDestroy();
icculus@3885
  1545
#endif
slouken@0
  1546
}
slouken@0
  1547