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