Skip to content

Latest commit

 

History

History
1367 lines (1226 loc) · 37.9 KB

SDL_x11video.c

File metadata and controls

1367 lines (1226 loc) · 37.9 KB
 
Apr 26, 2001
Apr 26, 2001
1
2
/*
SDL - Simple DirectMedia Layer
Feb 1, 2006
Feb 1, 2006
3
Copyright (C) 1997-2006 Sam Lantinga
Apr 26, 2001
Apr 26, 2001
4
5
This library is free software; you can redistribute it and/or
Feb 1, 2006
Feb 1, 2006
6
modify it under the terms of the GNU Lesser General Public
Apr 26, 2001
Apr 26, 2001
7
License as published by the Free Software Foundation; either
Feb 1, 2006
Feb 1, 2006
8
version 2.1 of the License, or (at your option) any later version.
Apr 26, 2001
Apr 26, 2001
9
10
11
12
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Feb 1, 2006
Feb 1, 2006
13
Lesser General Public License for more details.
Apr 26, 2001
Apr 26, 2001
14
Feb 1, 2006
Feb 1, 2006
15
16
17
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Apr 26, 2001
Apr 26, 2001
18
19
Sam Lantinga
Dec 14, 2001
Dec 14, 2001
20
slouken@libsdl.org
Apr 26, 2001
Apr 26, 2001
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
*/
/* X11 based SDL video driver implementation.
Note: This implementation does not currently need X11 thread locking,
since the event thread uses a separate X connection and any
additional locking necessary is handled internally. However,
if full locking is neccessary, take a look at XInitThreads().
*/
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/ioctl.h>
#ifdef MTRR_SUPPORT
#include <asm/mtrr.h>
#include <sys/fcntl.h>
#endif
#ifdef HAVE_ALLOCA_H
#include <alloca.h>
#endif
#ifdef HAVE_ALLOCA
#define ALLOCA(n) ((void*)alloca(n))
#define FREEA(p)
#else
#define ALLOCA(n) malloc(n)
#define FREEA(p) free(p)
#endif
#include "SDL.h"
#include "SDL_error.h"
#include "SDL_timer.h"
#include "SDL_thread.h"
#include "SDL_video.h"
#include "SDL_mouse.h"
#include "SDL_endian.h"
#include "SDL_sysvideo.h"
#include "SDL_pixels_c.h"
#include "SDL_events_c.h"
#include "SDL_x11video.h"
#include "SDL_x11wm_c.h"
#include "SDL_x11mouse_c.h"
#include "SDL_x11events_c.h"
#include "SDL_x11modes_c.h"
#include "SDL_x11image_c.h"
#include "SDL_x11yuv_c.h"
#include "SDL_x11gl_c.h"
#include "SDL_x11gamma_c.h"
#include "blank_cursor.h"
/* Initialization/Query functions */
static int X11_VideoInit(_THIS, SDL_PixelFormat *vformat);
static SDL_Surface *X11_SetVideoMode(_THIS, SDL_Surface *current, int width, int height, int bpp, Uint32 flags);
static int X11_ToggleFullScreen(_THIS, int on);
static void X11_UpdateMouse(_THIS);
static int X11_SetColors(_THIS, int firstcolor, int ncolors,
SDL_Color *colors);
static int X11_SetGammaRamp(_THIS, Uint16 *ramp);
static void X11_VideoQuit(_THIS);
Nov 5, 2005
Nov 5, 2005
83
Apr 26, 2001
Apr 26, 2001
84
85
86
87
/* X11 driver bootstrap functions */
static int X11_Available(void)
{
Nov 5, 2005
Nov 5, 2005
88
89
90
91
92
93
94
Display *display = NULL;
if ( SDL_X11_LoadSymbols() ) {
display = pXOpenDisplay(NULL);
if ( display != NULL ) {
pXCloseDisplay(display);
}
SDL_X11_UnloadSymbols();
Apr 26, 2001
Apr 26, 2001
95
96
97
98
99
100
101
102
103
104
105
106
107
108
}
return(display != NULL);
}
static void X11_DeleteDevice(SDL_VideoDevice *device)
{
if ( device ) {
if ( device->hidden ) {
free(device->hidden);
}
if ( device->gl_data ) {
free(device->gl_data);
}
free(device);
Nov 5, 2005
Nov 5, 2005
109
SDL_X11_UnloadSymbols();
Apr 26, 2001
Apr 26, 2001
110
111
112
113
114
}
}
static SDL_VideoDevice *X11_CreateDevice(int devindex)
{
Nov 5, 2005
Nov 5, 2005
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
SDL_VideoDevice *device = NULL;
if ( SDL_X11_LoadSymbols() ) {
/* Initialize all variables that we clean on shutdown */
device = (SDL_VideoDevice *)malloc(sizeof(SDL_VideoDevice));
if ( device ) {
memset(device, 0, (sizeof *device));
device->hidden = (struct SDL_PrivateVideoData *)
malloc((sizeof *device->hidden));
device->gl_data = (struct SDL_PrivateGLData *)
malloc((sizeof *device->gl_data));
}
if ( (device == NULL) || (device->hidden == NULL) ||
(device->gl_data == NULL) ) {
SDL_OutOfMemory();
X11_DeleteDevice(device); /* calls SDL_X11_UnloadSymbols(). */
return(0);
}
memset(device->hidden, 0, (sizeof *device->hidden));
memset(device->gl_data, 0, (sizeof *device->gl_data));
/* Set the driver flags */
device->handles_any_size = 1;
/* Set the function pointers */
device->VideoInit = X11_VideoInit;
device->ListModes = X11_ListModes;
device->SetVideoMode = X11_SetVideoMode;
device->ToggleFullScreen = X11_ToggleFullScreen;
device->UpdateMouse = X11_UpdateMouse;
Apr 26, 2001
Apr 26, 2001
145
#ifdef XFREE86_XV
Nov 5, 2005
Nov 5, 2005
146
device->CreateYUVOverlay = X11_CreateYUVOverlay;
Apr 26, 2001
Apr 26, 2001
147
#endif
Nov 5, 2005
Nov 5, 2005
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
device->SetColors = X11_SetColors;
device->UpdateRects = NULL;
device->VideoQuit = X11_VideoQuit;
device->AllocHWSurface = X11_AllocHWSurface;
device->CheckHWBlit = NULL;
device->FillHWRect = NULL;
device->SetHWColorKey = NULL;
device->SetHWAlpha = NULL;
device->LockHWSurface = X11_LockHWSurface;
device->UnlockHWSurface = X11_UnlockHWSurface;
device->FlipHWSurface = X11_FlipHWSurface;
device->FreeHWSurface = X11_FreeHWSurface;
device->SetGamma = X11_SetVidModeGamma;
device->GetGamma = X11_GetVidModeGamma;
device->SetGammaRamp = X11_SetGammaRamp;
device->GetGammaRamp = NULL;
Nov 23, 2005
Nov 23, 2005
164
#ifdef HAVE_OPENGL_X11
Nov 5, 2005
Nov 5, 2005
165
166
167
168
169
device->GL_LoadLibrary = X11_GL_LoadLibrary;
device->GL_GetProcAddress = X11_GL_GetProcAddress;
device->GL_GetAttribute = X11_GL_GetAttribute;
device->GL_MakeCurrent = X11_GL_MakeCurrent;
device->GL_SwapBuffers = X11_GL_SwapBuffers;
Apr 26, 2001
Apr 26, 2001
170
#endif
Nov 5, 2005
Nov 5, 2005
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
device->SetCaption = X11_SetCaption;
device->SetIcon = X11_SetIcon;
device->IconifyWindow = X11_IconifyWindow;
device->GrabInput = X11_GrabInput;
device->GetWMInfo = X11_GetWMInfo;
device->FreeWMCursor = X11_FreeWMCursor;
device->CreateWMCursor = X11_CreateWMCursor;
device->ShowWMCursor = X11_ShowWMCursor;
device->WarpWMCursor = X11_WarpWMCursor;
device->CheckMouseMode = X11_CheckMouseMode;
device->InitOSKeymap = X11_InitOSKeymap;
device->PumpEvents = X11_PumpEvents;
device->free = X11_DeleteDevice;
}
Apr 26, 2001
Apr 26, 2001
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
return device;
}
VideoBootStrap X11_bootstrap = {
"x11", "X Window System",
X11_Available, X11_CreateDevice
};
/* Normal X11 error handler routine */
static int (*X_handler)(Display *, XErrorEvent *) = NULL;
static int x_errhandler(Display *d, XErrorEvent *e)
{
#ifdef XFREE86_VM
extern int vm_error;
#endif
#ifdef XFREE86_DGAMOUSE
extern int dga_error;
#endif
#ifdef XFREE86_VM
/* VidMode errors are non-fatal. :) */
/* Are the errors offset by one from the error base?
e.g. the error base is 143, the code is 148, and the
actual error is XF86VidModeExtensionDisabled (4) ?
*/
if ( (vm_error >= 0) &&
(((e->error_code == BadRequest)&&(e->request_code == vm_error)) ||
((e->error_code > vm_error) &&
(e->error_code <= (vm_error+XF86VidModeNumberErrors)))) ) {
#ifdef XFREE86_DEBUG
{ char errmsg[1024];
Jan 14, 2006
Jan 14, 2006
218
pXGetErrorText(d, e->error_code, errmsg, sizeof(errmsg));
Apr 26, 2001
Apr 26, 2001
219
220
221
222
223
224
225
226
227
228
229
230
231
232
printf("VidMode error: %s\n", errmsg);
}
#endif
return(0);
}
#endif /* XFREE86_VM */
#ifdef XFREE86_DGAMOUSE
/* DGA errors can be non-fatal. :) */
if ( (dga_error >= 0) &&
((e->error_code > dga_error) &&
(e->error_code <= (dga_error+XF86DGANumberErrors))) ) {
#ifdef XFREE86_DEBUG
{ char errmsg[1024];
Jan 14, 2006
Jan 14, 2006
233
pXGetErrorText(d, e->error_code, errmsg, sizeof(errmsg));
Apr 26, 2001
Apr 26, 2001
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
printf("DGA error: %s\n", errmsg);
}
#endif
return(0);
}
#endif /* XFREE86_DGAMOUSE */
return(X_handler(d,e));
}
/* X11 I/O error handler routine */
static int (*XIO_handler)(Display *) = NULL;
static int xio_errhandler(Display *d)
{
/* Ack! Lost X11 connection! */
/* We will crash if we try to clean up our display */
if ( current_video->hidden->Ximage ) {
SDL_VideoSurface->pixels = NULL;
}
current_video->hidden->X11_Display = NULL;
/* Continue with the standard X11 error handler */
return(XIO_handler(d));
}
Jan 14, 2006
Jan 14, 2006
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
static int (*Xext_handler)(Display *,char *,char *) = NULL;
static int xext_errhandler(Display *d, char *ext_name, char *reason)
{
#ifdef XFREE86_DEBUG
printf("Xext error inside SDL (may be harmless):\n");
printf(" Extension \"%s\" %s on display \"%s\".\n",
ext_name, reason, pXDisplayString(d));
#endif
if (strcmp(reason, "missing") == 0) {
/*
* Since the query itself, elsewhere, can handle a missing extension
* and the default behaviour in Xlib is to write to stderr, which
* generates unnecessary bug reports, we just ignore these.
*/
return 0;
}
/* Everything else goes to the default handler... */
return Xext_handler(d, ext_name, reason);
}
Apr 26, 2001
Apr 26, 2001
282
283
284
/* Create auxiliary (toplevel) windows with the current visual */
static void create_aux_windows(_THIS)
{
Dec 19, 2005
Dec 19, 2005
285
char * savedclassname = NULL;
Apr 26, 2001
Apr 26, 2001
286
287
288
289
290
291
292
293
294
295
296
297
298
XSetWindowAttributes xattr;
XWMHints *hints;
XTextProperty titleprop, iconprop;
int def_vis = (SDL_Visual == DefaultVisual(SDL_Display, SDL_Screen));
/* Don't create any extra windows if we are being managed */
if ( SDL_windowid ) {
FSwindow = 0;
WMwindow = strtol(SDL_windowid, NULL, 0);
return;
}
if(FSwindow)
Nov 5, 2005
Nov 5, 2005
299
pXDestroyWindow(SDL_Display, FSwindow);
Apr 26, 2001
Apr 26, 2001
300
301
302
303
304
305
xattr.override_redirect = True;
xattr.background_pixel = def_vis ? BlackPixel(SDL_Display, SDL_Screen) : 0;
xattr.border_pixel = 0;
xattr.colormap = SDL_XColorMap;
Nov 5, 2005
Nov 5, 2005
306
FSwindow = pXCreateWindow(SDL_Display, SDL_Root,
Sep 30, 2002
Sep 30, 2002
307
xinerama_x, xinerama_y, 32, 32, 0,
Apr 26, 2001
Apr 26, 2001
308
309
310
311
312
this->hidden->depth, InputOutput, SDL_Visual,
CWOverrideRedirect | CWBackPixel | CWBorderPixel
| CWColormap,
&xattr);
Nov 5, 2005
Nov 5, 2005
313
pXSelectInput(SDL_Display, FSwindow, StructureNotifyMask);
Apr 26, 2001
Apr 26, 2001
314
315
316
317
318
319
320
321
322
/* Tell KDE to keep the fullscreen window on top */
{
XEvent ev;
long mask;
memset(&ev, 0, sizeof(ev));
ev.xclient.type = ClientMessage;
ev.xclient.window = SDL_Root;
Nov 5, 2005
Nov 5, 2005
323
ev.xclient.message_type = pXInternAtom(SDL_Display,
Apr 26, 2001
Apr 26, 2001
324
325
326
327
328
"KWM_KEEP_ON_TOP", False);
ev.xclient.format = 32;
ev.xclient.data.l[0] = FSwindow;
ev.xclient.data.l[1] = CurrentTime;
mask = SubstructureRedirectMask;
Nov 5, 2005
Nov 5, 2005
329
pXSendEvent(SDL_Display, SDL_Root, False, mask, &ev);
Apr 26, 2001
Apr 26, 2001
330
331
332
333
334
335
}
hints = NULL;
titleprop.value = iconprop.value = NULL;
if(WMwindow) {
/* All window attributes must survive the recreation */
Nov 5, 2005
Nov 5, 2005
336
337
338
339
hints = pXGetWMHints(SDL_Display, WMwindow);
pXGetWMName(SDL_Display, WMwindow, &titleprop);
pXGetWMIconName(SDL_Display, WMwindow, &iconprop);
pXDestroyWindow(SDL_Display, WMwindow);
Apr 26, 2001
Apr 26, 2001
340
341
342
343
}
/* Create the window for windowed management */
/* (reusing the xattr structure above) */
Nov 5, 2005
Nov 5, 2005
344
WMwindow = pXCreateWindow(SDL_Display, SDL_Root, 0, 0, 32, 32, 0,
Apr 26, 2001
Apr 26, 2001
345
346
347
348
349
350
this->hidden->depth, InputOutput, SDL_Visual,
CWBackPixel | CWBorderPixel | CWColormap,
&xattr);
/* Set the input hints so we get keyboard input */
if(!hints) {
Nov 5, 2005
Nov 5, 2005
351
hints = pXAllocWMHints();
Apr 26, 2001
Apr 26, 2001
352
353
354
hints->input = True;
hints->flags = InputHint;
}
Nov 5, 2005
Nov 5, 2005
355
356
pXSetWMHints(SDL_Display, WMwindow, hints);
pXFree(hints);
Apr 26, 2001
Apr 26, 2001
357
if(titleprop.value) {
Nov 5, 2005
Nov 5, 2005
358
359
pXSetWMName(SDL_Display, WMwindow, &titleprop);
pXFree(titleprop.value);
Apr 26, 2001
Apr 26, 2001
360
361
}
if(iconprop.value) {
Nov 5, 2005
Nov 5, 2005
362
363
pXSetWMIconName(SDL_Display, WMwindow, &iconprop);
pXFree(iconprop.value);
Apr 26, 2001
Apr 26, 2001
364
365
}
Nov 5, 2005
Nov 5, 2005
366
pXSelectInput(SDL_Display, WMwindow,
Apr 26, 2001
Apr 26, 2001
367
368
369
370
371
372
FocusChangeMask | KeyPressMask | KeyReleaseMask
| PropertyChangeMask | StructureNotifyMask | KeymapStateMask);
/* Set the class hints so we can get an icon (AfterStep) */
{
XClassHint *classhints;
Nov 5, 2005
Nov 5, 2005
373
classhints = pXAllocClassHint();
Apr 26, 2001
Apr 26, 2001
374
if(classhints != NULL) {
Aug 19, 2002
Aug 19, 2002
375
376
377
378
char *classname = getenv("SDL_VIDEO_X11_WMCLASS");
if ( ! classname ) {
classname = "SDL_App";
}
Nov 21, 2005
Nov 21, 2005
379
savedclassname = strdup(classname);
Aug 19, 2002
Aug 19, 2002
380
381
classhints->res_name = classname;
classhints->res_class = classname;
Nov 5, 2005
Nov 5, 2005
382
383
pXSetClassHint(SDL_Display, WMwindow, classhints);
pXFree(classhints);
Apr 26, 2001
Apr 26, 2001
384
385
386
}
}
Nov 21, 2005
Nov 21, 2005
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
/* Setup the communication with the IM server */
SDL_IM = NULL;
SDL_IC = NULL;
#ifdef X_HAVE_UTF8_STRING
SDL_IM = pXOpenIM(SDL_Display, NULL, savedclassname, savedclassname);
if (SDL_IM == NULL) {
SDL_SetError("no input method could be opened");
} else {
SDL_IC = pXCreateIC(SDL_IM,
XNClientWindow, WMwindow,
XNFocusWindow, WMwindow,
XNInputStyle, XIMPreeditNothing | XIMStatusNothing,
XNResourceName, savedclassname,
XNResourceClass, savedclassname,
NULL);
if (SDL_IC == NULL) {
SDL_SetError("no input context could be created");
pXCloseIM(SDL_IM);
SDL_IM = NULL;
}
}
#endif
free(savedclassname);
Apr 26, 2001
Apr 26, 2001
414
/* Allow the window to be deleted by the window manager */
Nov 5, 2005
Nov 5, 2005
415
416
WM_DELETE_WINDOW = pXInternAtom(SDL_Display, "WM_DELETE_WINDOW", False);
pXSetWMProtocols(SDL_Display, WMwindow, &WM_DELETE_WINDOW, 1);
Apr 26, 2001
Apr 26, 2001
417
418
419
420
421
422
423
424
425
426
}
static int X11_VideoInit(_THIS, SDL_PixelFormat *vformat)
{
char *display;
int i;
/* Open the X11 display */
display = NULL; /* Get it from DISPLAY environment variable */
Nov 5, 2005
Nov 5, 2005
427
428
if ( (strncmp(pXDisplayName(display), ":", 1) == 0) ||
(strncmp(pXDisplayName(display), "unix:", 5) == 0) ) {
Apr 26, 2001
Apr 26, 2001
429
430
431
432
local_X11 = 1;
} else {
local_X11 = 0;
}
Nov 5, 2005
Nov 5, 2005
433
SDL_Display = pXOpenDisplay(display);
Jan 30, 2006
Jan 30, 2006
434
435
436
437
438
439
440
441
442
443
444
445
446
447
#if defined(__osf__) && defined(X11_DYNAMIC)
/* On Tru64 if linking without -lX11, it fails and you get following message.
* Xlib: connection to ":0.0" refused by server
* Xlib: XDM authorization key matches an existing client!
*
* It succeeds if retrying 1 second later
* or if running xhost +localhost on shell.
*
*/
if ( SDL_Display == NULL ) {
SDL_Delay(1000);
SDL_Display = pXOpenDisplay(display);
}
#endif
Apr 26, 2001
Apr 26, 2001
448
449
450
451
452
if ( SDL_Display == NULL ) {
SDL_SetError("Couldn't open X11 display");
return(-1);
}
#ifdef X11_DEBUG
Nov 5, 2005
Nov 5, 2005
453
pXSynchronize(SDL_Display, True);
Apr 26, 2001
Apr 26, 2001
454
455
456
457
458
459
#endif
/* Create an alternate X display for graphics updates -- allows us
to do graphics updates in a separate thread from event handling.
Thread-safe X11 doesn't seem to exist.
*/
Nov 5, 2005
Nov 5, 2005
460
GFX_Display = pXOpenDisplay(display);
Apr 26, 2001
Apr 26, 2001
461
462
463
464
465
466
if ( GFX_Display == NULL ) {
SDL_SetError("Couldn't open X11 display");
return(-1);
}
/* Set the normal X error handler */
Nov 5, 2005
Nov 5, 2005
467
X_handler = pXSetErrorHandler(x_errhandler);
Apr 26, 2001
Apr 26, 2001
468
469
/* Set the error handler if we lose the X display */
Nov 5, 2005
Nov 5, 2005
470
XIO_handler = pXSetIOErrorHandler(xio_errhandler);
Apr 26, 2001
Apr 26, 2001
471
Jan 14, 2006
Jan 14, 2006
472
473
474
/* Set the X extension error handler */
Xext_handler = pXSetExtensionErrorHandler(xext_errhandler);
Apr 26, 2001
Apr 26, 2001
475
476
477
478
479
/* use default screen (from $DISPLAY) */
SDL_Screen = DefaultScreen(SDL_Display);
#ifndef NO_SHARED_MEMORY
/* Check for MIT shared memory extension */
Dec 7, 2002
Dec 7, 2002
480
use_mitshm = 0;
Apr 26, 2001
Apr 26, 2001
481
if ( local_X11 ) {
Nov 5, 2005
Nov 5, 2005
482
use_mitshm = pXShmQueryExtension(SDL_Display);
Apr 26, 2001
Apr 26, 2001
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
}
#endif /* NO_SHARED_MEMORY */
/* Get the available video modes */
if(X11_GetVideoModes(this) < 0)
return -1;
/* Determine the default screen depth:
Use the default visual (or at least one with the same depth) */
SDL_DisplayColormap = DefaultColormap(SDL_Display, SDL_Screen);
for(i = 0; i < this->hidden->nvisuals; i++)
if(this->hidden->visuals[i].depth == DefaultDepth(SDL_Display,
SDL_Screen))
break;
if(i == this->hidden->nvisuals) {
/* default visual was useless, take the deepest one instead */
i = 0;
}
SDL_Visual = this->hidden->visuals[i].visual;
if ( SDL_Visual == DefaultVisual(SDL_Display, SDL_Screen) ) {
SDL_XColorMap = SDL_DisplayColormap;
} else {
Nov 5, 2005
Nov 5, 2005
505
SDL_XColorMap = pXCreateColormap(SDL_Display, SDL_Root,
Apr 26, 2001
Apr 26, 2001
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
SDL_Visual, AllocNone);
}
this->hidden->depth = this->hidden->visuals[i].depth;
vformat->BitsPerPixel = this->hidden->visuals[i].bpp;
if ( vformat->BitsPerPixel > 8 ) {
vformat->Rmask = SDL_Visual->red_mask;
vformat->Gmask = SDL_Visual->green_mask;
vformat->Bmask = SDL_Visual->blue_mask;
}
X11_SaveVidModeGamma(this);
/* See if we have been passed a window to use */
SDL_windowid = getenv("SDL_WINDOWID");
/* Create the fullscreen and managed windows */
create_aux_windows(this);
/* Create the blank cursor */
SDL_BlankCursor = this->CreateWMCursor(this, blank_cdata, blank_cmask,
BLANK_CWIDTH, BLANK_CHEIGHT,
BLANK_CHOTX, BLANK_CHOTY);
/* Fill in some window manager capabilities */
this->info.wm_available = 1;
/* We're done! */
Nov 5, 2005
Nov 5, 2005
532
pXFlush(SDL_Display);
Apr 26, 2001
Apr 26, 2001
533
534
535
536
537
538
539
540
541
542
543
544
545
546
return(0);
}
static void X11_DestroyWindow(_THIS, SDL_Surface *screen)
{
/* Clean up OpenGL */
if ( screen ) {
screen->flags &= ~(SDL_OPENGL|SDL_OPENGLBLIT);
}
X11_GL_Shutdown(this);
if ( ! SDL_windowid ) {
/* Hide the managed window */
if ( WMwindow ) {
Nov 5, 2005
Nov 5, 2005
547
pXUnmapWindow(SDL_Display, WMwindow);
Apr 26, 2001
Apr 26, 2001
548
549
550
551
552
553
554
555
}
if ( screen && (screen->flags & SDL_FULLSCREEN) ) {
screen->flags &= ~SDL_FULLSCREEN;
X11_LeaveFullScreen(this);
}
/* Destroy the output window */
if ( SDL_Window ) {
Nov 5, 2005
Nov 5, 2005
556
pXDestroyWindow(SDL_Display, SDL_Window);
Apr 26, 2001
Apr 26, 2001
557
558
559
560
561
562
563
564
565
}
/* Free the colormap entries */
if ( SDL_XPixels ) {
int numcolors;
unsigned long pixel;
numcolors = SDL_Visual->map_entries;
for ( pixel=0; pixel<numcolors; ++pixel ) {
while ( SDL_XPixels[pixel] > 0 ) {
Nov 5, 2005
Nov 5, 2005
566
pXFreeColors(GFX_Display,
Apr 26, 2001
Apr 26, 2001
567
568
569
570
571
572
573
574
575
576
SDL_DisplayColormap,&pixel,1,0);
--SDL_XPixels[pixel];
}
}
free(SDL_XPixels);
SDL_XPixels = NULL;
}
/* Free the graphics context */
if ( SDL_GC ) {
Nov 5, 2005
Nov 5, 2005
577
pXFreeGC(SDL_Display, SDL_GC);
Apr 26, 2001
Apr 26, 2001
578
579
580
581
582
SDL_GC = 0;
}
}
}
Sep 16, 2002
Sep 16, 2002
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
static SDL_bool X11_WindowPosition(_THIS, int *x, int *y, int w, int h)
{
const char *window = getenv("SDL_VIDEO_WINDOW_POS");
const char *center = getenv("SDL_VIDEO_CENTERED");
if ( window ) {
if ( sscanf(window, "%d,%d", x, y) == 2 ) {
return SDL_TRUE;
}
if ( strcmp(window, "center") == 0 ) {
center = window;
}
}
if ( center ) {
*x = (DisplayWidth(SDL_Display, SDL_Screen) - w)/2;
*y = (DisplayHeight(SDL_Display, SDL_Screen) - h)/2;
return SDL_TRUE;
}
return SDL_FALSE;
}
Apr 26, 2001
Apr 26, 2001
603
604
605
606
static void X11_SetSizeHints(_THIS, int w, int h, Uint32 flags)
{
XSizeHints *hints;
Nov 5, 2005
Nov 5, 2005
607
hints = pXAllocSizeHints();
Apr 26, 2001
Apr 26, 2001
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
if ( hints ) {
if ( flags & SDL_RESIZABLE ) {
hints->min_width = 32;
hints->min_height = 32;
hints->max_height = 4096;
hints->max_width = 4096;
} else {
hints->min_width = hints->max_width = w;
hints->min_height = hints->max_height = h;
}
hints->flags = PMaxSize | PMinSize;
if ( flags & SDL_FULLSCREEN ) {
hints->x = 0;
hints->y = 0;
hints->flags |= USPosition;
} else
/* Center it, if desired */
Sep 16, 2002
Sep 16, 2002
625
if ( X11_WindowPosition(this, &hints->x, &hints->y, w, h) ) {
Apr 26, 2001
Apr 26, 2001
626
hints->flags |= USPosition;
Nov 5, 2005
Nov 5, 2005
627
pXMoveWindow(SDL_Display, WMwindow, hints->x, hints->y);
Apr 26, 2001
Apr 26, 2001
628
629
/* Flush the resize event so we don't catch it later */
Nov 5, 2005
Nov 5, 2005
630
pXSync(SDL_Display, True);
Apr 26, 2001
Apr 26, 2001
631
}
Nov 5, 2005
Nov 5, 2005
632
633
pXSetWMNormalHints(SDL_Display, WMwindow, hints);
pXFree(hints);
Apr 26, 2001
Apr 26, 2001
634
635
636
637
638
639
640
641
642
643
644
}
/* Respect the window caption style */
if ( flags & SDL_NOFRAME ) {
SDL_bool set;
Atom WM_HINTS;
/* We haven't modified the window manager hints yet */
set = SDL_FALSE;
/* First try to set MWM hints */
Nov 5, 2005
Nov 5, 2005
645
WM_HINTS = pXInternAtom(SDL_Display, "_MOTIF_WM_HINTS", True);
Apr 26, 2001
Apr 26, 2001
646
647
648
649
650
651
652
653
654
655
if ( WM_HINTS != None ) {
/* Hints used by Motif compliant window managers */
struct {
unsigned long flags;
unsigned long functions;
unsigned long decorations;
long input_mode;
unsigned long status;
} MWMHints = { (1L << 1), 0, 0, 0, 0 };
Nov 5, 2005
Nov 5, 2005
656
pXChangeProperty(SDL_Display, WMwindow,
Apr 26, 2001
Apr 26, 2001
657
658
659
660
661
662
663
WM_HINTS, WM_HINTS, 32,
PropModeReplace,
(unsigned char *)&MWMHints,
sizeof(MWMHints)/sizeof(long));
set = SDL_TRUE;
}
/* Now try to set KWM hints */
Nov 5, 2005
Nov 5, 2005
664
WM_HINTS = pXInternAtom(SDL_Display, "KWM_WIN_DECORATION", True);
Apr 26, 2001
Apr 26, 2001
665
666
667
if ( WM_HINTS != None ) {
long KWMHints = 0;
Nov 5, 2005
Nov 5, 2005
668
pXChangeProperty(SDL_Display, WMwindow,
Apr 26, 2001
Apr 26, 2001
669
670
671
672
673
674
675
WM_HINTS, WM_HINTS, 32,
PropModeReplace,
(unsigned char *)&KWMHints,
sizeof(KWMHints)/sizeof(long));
set = SDL_TRUE;
}
/* Now try to set GNOME hints */
Nov 5, 2005
Nov 5, 2005
676
WM_HINTS = pXInternAtom(SDL_Display, "_WIN_HINTS", True);
Apr 26, 2001
Apr 26, 2001
677
678
679
if ( WM_HINTS != None ) {
long GNOMEHints = 0;
Nov 5, 2005
Nov 5, 2005
680
pXChangeProperty(SDL_Display, WMwindow,
Apr 26, 2001
Apr 26, 2001
681
682
683
684
685
686
687
688
WM_HINTS, WM_HINTS, 32,
PropModeReplace,
(unsigned char *)&GNOMEHints,
sizeof(GNOMEHints)/sizeof(long));
set = SDL_TRUE;
}
/* Finally set the transient hints if necessary */
if ( ! set ) {
Nov 5, 2005
Nov 5, 2005
689
pXSetTransientForHint(SDL_Display, WMwindow, SDL_Root);
Apr 26, 2001
Apr 26, 2001
690
691
692
693
694
695
696
697
698
}
} else {
SDL_bool set;
Atom WM_HINTS;
/* We haven't modified the window manager hints yet */
set = SDL_FALSE;
/* First try to unset MWM hints */
Nov 5, 2005
Nov 5, 2005
699
WM_HINTS = pXInternAtom(SDL_Display, "_MOTIF_WM_HINTS", True);
Apr 26, 2001
Apr 26, 2001
700
if ( WM_HINTS != None ) {
Nov 5, 2005
Nov 5, 2005
701
pXDeleteProperty(SDL_Display, WMwindow, WM_HINTS);
Apr 26, 2001
Apr 26, 2001
702
703
704
set = SDL_TRUE;
}
/* Now try to unset KWM hints */
Nov 5, 2005
Nov 5, 2005
705
WM_HINTS = pXInternAtom(SDL_Display, "KWM_WIN_DECORATION", True);
Apr 26, 2001
Apr 26, 2001
706
if ( WM_HINTS != None ) {
Nov 5, 2005
Nov 5, 2005
707
pXDeleteProperty(SDL_Display, WMwindow, WM_HINTS);
Apr 26, 2001
Apr 26, 2001
708
709
710
set = SDL_TRUE;
}
/* Now try to unset GNOME hints */
Nov 5, 2005
Nov 5, 2005
711
WM_HINTS = pXInternAtom(SDL_Display, "_WIN_HINTS", True);
Apr 26, 2001
Apr 26, 2001
712
if ( WM_HINTS != None ) {
Nov 5, 2005
Nov 5, 2005
713
pXDeleteProperty(SDL_Display, WMwindow, WM_HINTS);
Apr 26, 2001
Apr 26, 2001
714
715
716
717
718
set = SDL_TRUE;
}
/* Finally unset the transient hints if necessary */
if ( ! set ) {
/* NOTE: Does this work? */
Nov 5, 2005
Nov 5, 2005
719
pXSetTransientForHint(SDL_Display, WMwindow, None);
Apr 26, 2001
Apr 26, 2001
720
721
722
723
724
725
726
727
728
729
730
731
732
733
}
}
}
static int X11_CreateWindow(_THIS, SDL_Surface *screen,
int w, int h, int bpp, Uint32 flags)
{
int i, depth;
Visual *vis;
int vis_change;
/* If a window is already present, destroy it and start fresh */
if ( SDL_Window ) {
X11_DestroyWindow(this, screen);
Feb 26, 2004
Feb 26, 2004
734
switch_waiting = 0; /* Prevent jump back to now-meaningless state. */
Apr 26, 2001
Apr 26, 2001
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
}
/* See if we have been given a window id */
if ( SDL_windowid ) {
SDL_Window = strtol(SDL_windowid, NULL, 0);
} else {
SDL_Window = 0;
}
/* find out which visual we are going to use */
if ( flags & SDL_OPENGL ) {
XVisualInfo *vi;
vi = X11_GL_GetVisual(this);
if( !vi ) {
return -1;
}
vis = vi->visual;
depth = vi->depth;
} else if ( SDL_windowid ) {
XWindowAttributes a;
Nov 5, 2005
Nov 5, 2005
757
pXGetWindowAttributes(SDL_Display, SDL_Window, &a);
Apr 26, 2001
Apr 26, 2001
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
vis = a.visual;
depth = a.depth;
} else {
for ( i = 0; i < this->hidden->nvisuals; i++ ) {
if ( this->hidden->visuals[i].bpp == bpp )
break;
}
if ( i == this->hidden->nvisuals ) {
SDL_SetError("No matching visual for requested depth");
return -1; /* should never happen */
}
vis = this->hidden->visuals[i].visual;
depth = this->hidden->visuals[i].depth;
}
#ifdef X11_DEBUG
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);
#endif
vis_change = (vis != SDL_Visual);
SDL_Visual = vis;
this->hidden->depth = depth;
/* Allocate the new pixel format for this video mode */
if ( ! SDL_ReallocFormat(screen, bpp,
vis->red_mask, vis->green_mask, vis->blue_mask, 0) )
return -1;
/* Create the appropriate colormap */
if ( SDL_XColorMap != SDL_DisplayColormap ) {
Nov 5, 2005
Nov 5, 2005
786
pXFreeColormap(SDL_Display, SDL_XColorMap);
Apr 26, 2001
Apr 26, 2001
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
}
if ( SDL_Visual->class == PseudoColor ) {
int ncolors;
/* Allocate the pixel flags */
ncolors = SDL_Visual->map_entries;
SDL_XPixels = malloc(ncolors * sizeof(int));
if(SDL_XPixels == NULL) {
SDL_OutOfMemory();
return -1;
}
memset(SDL_XPixels, 0, ncolors * sizeof(*SDL_XPixels));
/* always allocate a private colormap on non-default visuals */
if ( SDL_Visual != DefaultVisual(SDL_Display, SDL_Screen) ) {
flags |= SDL_HWPALETTE;
}
if ( flags & SDL_HWPALETTE ) {
screen->flags |= SDL_HWPALETTE;
Nov 5, 2005
Nov 5, 2005
806
SDL_XColorMap = pXCreateColormap(SDL_Display, SDL_Root,
Apr 26, 2001
Apr 26, 2001
807
808
809
810
811
812
813
SDL_Visual, AllocAll);
} else {
SDL_XColorMap = SDL_DisplayColormap;
}
} else if ( SDL_Visual->class == DirectColor ) {
/* Create a colormap which we can manipulate for gamma */
Nov 5, 2005
Nov 5, 2005
814
SDL_XColorMap = pXCreateColormap(SDL_Display, SDL_Root,
Apr 26, 2001
Apr 26, 2001
815
SDL_Visual, AllocAll);
Nov 5, 2005
Nov 5, 2005
816
pXSync(SDL_Display, False);
Apr 26, 2001
Apr 26, 2001
817
818
819
820
821
822
823
824
/* Initialize the colormap to the identity mapping */
SDL_GetGammaRamp(0, 0, 0);
this->screen = screen;
X11_SetGammaRamp(this, this->gamma);
this->screen = NULL;
} else {
/* Create a read-only colormap for our window */
Nov 5, 2005
Nov 5, 2005
825
SDL_XColorMap = pXCreateColormap(SDL_Display, SDL_Root,
Apr 26, 2001
Apr 26, 2001
826
827
828
829
830
831
832
833
834
835
836
837
SDL_Visual, AllocNone);
}
/* Recreate the auxiliary windows, if needed (required for GL) */
if ( vis_change )
create_aux_windows(this);
if(screen->flags & SDL_HWPALETTE) {
/* Since the full-screen window might have got a nonzero background
colour (0 is white on some displays), we should reset the
background to 0 here since that is what the user expects
with a private colormap */
Nov 5, 2005
Nov 5, 2005
838
839
pXSetWindowBackground(SDL_Display, FSwindow, 0);
pXClearWindow(SDL_Display, FSwindow);
Apr 26, 2001
Apr 26, 2001
840
841
842
843
844
845
846
}
/* resize the (possibly new) window manager window */
if( !SDL_windowid ) {
X11_SetSizeHints(this, w, h, flags);
current_w = w;
current_h = h;
Nov 5, 2005
Nov 5, 2005
847
pXResizeWindow(SDL_Display, WMwindow, w, h);
Apr 26, 2001
Apr 26, 2001
848
849
850
851
852
853
854
855
856
857
858
859
860
861
}
/* Create (or use) the X11 display window */
if ( !SDL_windowid ) {
if ( flags & SDL_OPENGL ) {
if ( X11_GL_CreateWindow(this, w, h) < 0 ) {
return(-1);
}
} else {
XSetWindowAttributes swa;
swa.background_pixel = 0;
swa.border_pixel = 0;
swa.colormap = SDL_XColorMap;
Nov 5, 2005
Nov 5, 2005
862
SDL_Window = pXCreateWindow(SDL_Display, WMwindow,
Apr 26, 2001
Apr 26, 2001
863
864
865
866
867
868
0, 0, w, h, 0, depth,
InputOutput, SDL_Visual,
CWBackPixel | CWBorderPixel
| CWColormap, &swa);
}
/* Only manage our input if we own the window */
Nov 5, 2005
Nov 5, 2005
869
pXSelectInput(SDL_Display, SDL_Window,
Apr 26, 2001
Apr 26, 2001
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
( EnterWindowMask | LeaveWindowMask
| ButtonPressMask | ButtonReleaseMask
| PointerMotionMask | ExposureMask ));
}
/* Create the graphics context here, once we have a window */
if ( flags & SDL_OPENGL ) {
if ( X11_GL_CreateContext(this) < 0 ) {
return(-1);
} else {
screen->flags |= SDL_OPENGL;
}
} else {
XGCValues gcv;
gcv.graphics_exposures = False;
Nov 5, 2005
Nov 5, 2005
885
SDL_GC = pXCreateGC(SDL_Display, SDL_Window,
Apr 26, 2001
Apr 26, 2001
886
887
888
889
890
891
892
893
894
GCGraphicsExposures, &gcv);
if ( ! SDL_GC ) {
SDL_SetError("Couldn't create graphics context");
return(-1);
}
}
/* Set our colormaps when not setting a GL mode */
if ( ! (flags & SDL_OPENGL) ) {
Nov 5, 2005
Nov 5, 2005
895
pXSetWindowColormap(SDL_Display, SDL_Window, SDL_XColorMap);
Apr 26, 2001
Apr 26, 2001
896
if( !SDL_windowid ) {
Nov 5, 2005
Nov 5, 2005
897
898
pXSetWindowColormap(SDL_Display, FSwindow, SDL_XColorMap);
pXSetWindowColormap(SDL_Display, WMwindow, SDL_XColorMap);
Apr 26, 2001
Apr 26, 2001
899
900
901
902
903
904
905
906
907
908
909
910
911
912
}
}
#if 0 /* This is an experiment - are the graphics faster now? - nope. */
if ( getenv("SDL_VIDEO_X11_BACKINGSTORE") )
#endif
/* Cache the window in the server, when possible */
{
Screen *xscreen;
XSetWindowAttributes a;
xscreen = ScreenOfDisplay(SDL_Display, SDL_Screen);
a.backing_store = DoesBackingStore(xscreen);
if ( a.backing_store != NotUseful ) {
Nov 5, 2005
Nov 5, 2005
913
pXChangeWindowAttributes(SDL_Display, SDL_Window,
Apr 26, 2001
Apr 26, 2001
914
915
916
917
918
CWBackingStore, &a);
}
}
/* Update the internal keyboard state */
Nov 21, 2005
Nov 21, 2005
919
X11_SetKeyboardState(SDL_Display, SDL_IC, NULL);
Apr 26, 2001
Apr 26, 2001
920
Aug 18, 2002
Aug 18, 2002
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
/* When the window is first mapped, ignore non-modifier keys */
{
Uint8 *keys = SDL_GetKeyState(NULL);
for ( i = 0; i < SDLK_LAST; ++i ) {
switch (i) {
case SDLK_NUMLOCK:
case SDLK_CAPSLOCK:
case SDLK_LCTRL:
case SDLK_RCTRL:
case SDLK_LSHIFT:
case SDLK_RSHIFT:
case SDLK_LALT:
case SDLK_RALT:
case SDLK_LMETA:
case SDLK_RMETA:
case SDLK_MODE:
break;
default:
keys[i] = SDL_RELEASED;
break;
}
}
}
Apr 26, 2001
Apr 26, 2001
945
946
/* Map them both and go fullscreen, if requested */
if ( ! SDL_windowid ) {
Nov 5, 2005
Nov 5, 2005
947
948
pXMapWindow(SDL_Display, SDL_Window);
pXMapWindow(SDL_Display, WMwindow);
Aug 31, 2001
Aug 31, 2001
949
X11_WaitMapped(this, WMwindow);
Apr 26, 2001
Apr 26, 2001
950
951
952
953
954
955
956
if ( flags & SDL_FULLSCREEN ) {
screen->flags |= SDL_FULLSCREEN;
X11_EnterFullScreen(this);
} else {
screen->flags &= ~SDL_FULLSCREEN;
}
}
Nov 21, 2005
Nov 21, 2005
957
Apr 26, 2001
Apr 26, 2001
958
959
960
961
962
963
964
965
966
967
968
return(0);
}
static int X11_ResizeWindow(_THIS,
SDL_Surface *screen, int w, int h, Uint32 flags)
{
if ( ! SDL_windowid ) {
/* Resize the window manager window */
X11_SetSizeHints(this, w, h, flags);
current_w = w;
current_h = h;
Nov 5, 2005
Nov 5, 2005
969
pXResizeWindow(SDL_Display, WMwindow, w, h);
Apr 26, 2001
Apr 26, 2001
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
/* Resize the fullscreen and display windows */
if ( flags & SDL_FULLSCREEN ) {
if ( screen->flags & SDL_FULLSCREEN ) {
X11_ResizeFullScreen(this);
} else {
screen->flags |= SDL_FULLSCREEN;
X11_EnterFullScreen(this);
}
} else {
if ( screen->flags & SDL_FULLSCREEN ) {
screen->flags &= ~SDL_FULLSCREEN;
X11_LeaveFullScreen(this);
}
}
Nov 5, 2005
Nov 5, 2005
985
pXResizeWindow(SDL_Display, SDL_Window, w, h);
Apr 26, 2001
Apr 26, 2001
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
}
return(0);
}
SDL_Surface *X11_SetVideoMode(_THIS, SDL_Surface *current,
int width, int height, int bpp, Uint32 flags)
{
Uint32 saved_flags;
/* Lock the event thread, in multi-threading environments */
SDL_Lock_EventThread();
/* Check the combination of flags we were passed */
if ( flags & SDL_FULLSCREEN ) {
/* Clear fullscreen flag if not supported */