/
SDL_x11video.c
1579 lines (1418 loc) · 44.6 KB
1
2
/*
SDL - Simple DirectMedia Layer
3
Copyright (C) 1997-2012 Sam Lantinga
4
5
This library is free software; you can redistribute it and/or
6
modify it under the terms of the GNU Lesser General Public
7
License as published by the Free Software Foundation; either
8
version 2.1 of the License, or (at your option) any later version.
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
13
Lesser General Public License for more details.
14
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
18
19
Sam Lantinga
20
slouken@libsdl.org
21
*/
22
#include "SDL_config.h"
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/* 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 <unistd.h>
#include <sys/ioctl.h>
#ifdef MTRR_SUPPORT
#include <asm/mtrr.h>
#include <sys/fcntl.h>
#endif
38
#include "SDL_endian.h"
39
40
41
42
#include "SDL_timer.h"
#include "SDL_thread.h"
#include "SDL_video.h"
#include "SDL_mouse.h"
43
44
45
#include "../SDL_sysvideo.h"
#include "../SDL_pixels_c.h"
#include "../../events/SDL_events_c.h"
46
47
48
49
50
51
52
53
54
#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"
55
#include "../blank_cursor.h"
56
57
58
59
60
#ifdef X_HAVE_UTF8_STRING
#include <locale.h>
#endif
61
62
63
64
65
66
67
68
69
70
/* 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);
71
72
73
74
75
/* X11 driver bootstrap functions */
static int X11_Available(void)
{
76
77
Display *display = NULL;
if ( SDL_X11_LoadSymbols() ) {
78
display = XOpenDisplay(NULL);
79
if ( display != NULL ) {
80
XCloseDisplay(display);
81
82
}
SDL_X11_UnloadSymbols();
83
84
85
86
87
88
89
90
}
return(display != NULL);
}
static void X11_DeleteDevice(SDL_VideoDevice *device)
{
if ( device ) {
if ( device->hidden ) {
91
SDL_free(device->hidden);
92
93
}
if ( device->gl_data ) {
94
SDL_free(device->gl_data);
95
}
96
SDL_free(device);
97
SDL_X11_UnloadSymbols();
98
99
100
101
102
}
}
static SDL_VideoDevice *X11_CreateDevice(int devindex)
{
103
104
105
106
SDL_VideoDevice *device = NULL;
if ( SDL_X11_LoadSymbols() ) {
/* Initialize all variables that we clean on shutdown */
107
device = (SDL_VideoDevice *)SDL_malloc(sizeof(SDL_VideoDevice));
108
if ( device ) {
109
SDL_memset(device, 0, (sizeof *device));
110
device->hidden = (struct SDL_PrivateVideoData *)
111
SDL_malloc((sizeof *device->hidden));
112
device->gl_data = (struct SDL_PrivateGLData *)
113
SDL_malloc((sizeof *device->gl_data));
114
115
116
117
118
119
120
}
if ( (device == NULL) || (device->hidden == NULL) ||
(device->gl_data == NULL) ) {
SDL_OutOfMemory();
X11_DeleteDevice(device); /* calls SDL_X11_UnloadSymbols(). */
return(0);
}
121
122
SDL_memset(device->hidden, 0, (sizeof *device->hidden));
SDL_memset(device->gl_data, 0, (sizeof *device->gl_data));
123
124
#if SDL_VIDEO_OPENGL_GLX
125
device->gl_data->swap_interval = -1;
126
#endif
127
128
129
130
131
132
133
134
135
136
/* 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;
137
#if SDL_VIDEO_DRIVER_X11_XV
138
device->CreateYUVOverlay = X11_CreateYUVOverlay;
139
#endif
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
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;
156
#if SDL_VIDEO_OPENGL_GLX
157
158
159
160
161
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;
162
#endif
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
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;
}
178
179
180
181
182
183
184
185
186
187
188
189
190
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)
{
191
#if SDL_VIDEO_DRIVER_X11_VIDMODE
192
193
extern int vm_error;
#endif
194
#if SDL_VIDEO_DRIVER_X11_DGAMOUSE
195
196
197
extern int dga_error;
#endif
198
#if SDL_VIDEO_DRIVER_X11_VIDMODE
199
200
201
202
203
204
205
206
207
/* 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)))) ) {
208
#ifdef X11_DEBUG
209
{ char errmsg[1024];
210
XGetErrorText(d, e->error_code, errmsg, sizeof(errmsg));
211
212
213
214
215
printf("VidMode error: %s\n", errmsg);
}
#endif
return(0);
}
216
#endif /* SDL_VIDEO_DRIVER_X11_VIDMODE */
217
218
#if SDL_VIDEO_DRIVER_X11_DGAMOUSE
219
220
221
222
/* DGA errors can be non-fatal. :) */
if ( (dga_error >= 0) &&
((e->error_code > dga_error) &&
(e->error_code <= (dga_error+XF86DGANumberErrors))) ) {
223
#ifdef X11_DEBUG
224
{ char errmsg[1024];
225
XGetErrorText(d, e->error_code, errmsg, sizeof(errmsg));
226
227
228
229
230
printf("DGA error: %s\n", errmsg);
}
#endif
return(0);
}
231
#endif /* SDL_VIDEO_DRIVER_X11_DGAMOUSE */
232
233
234
235
236
237
238
239
240
241
242
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 */
243
if ( SDL_VideoSurface && current_video->hidden->Ximage ) {
244
245
246
247
248
249
250
251
SDL_VideoSurface->pixels = NULL;
}
current_video->hidden->X11_Display = NULL;
/* Continue with the standard X11 error handler */
return(XIO_handler(d));
}
252
253
static int (*Xext_handler)(Display *, _Xconst char *, _Xconst char *) = NULL;
static int xext_errhandler(Display *d, _Xconst char *ext, _Xconst char *reason)
254
{
255
#ifdef X11_DEBUG
256
257
printf("Xext error inside SDL (may be harmless):\n");
printf(" Extension \"%s\" %s on display \"%s\".\n",
258
ext, reason, XDisplayString(d));
259
260
#endif
261
if (SDL_strcmp(reason, "missing") == 0) {
262
263
264
265
266
267
268
269
270
/*
* 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... */
271
return Xext_handler(d, ext, reason);
272
273
}
274
275
276
277
/* Find out what class name we should use */
static char *get_classname(char *classname, int maxlen)
{
char *spot;
278
#if defined(__LINUX__) || defined(__FREEBSD__)
279
280
281
282
283
284
char procfile[1024];
char linkfile[1024];
int linksize;
#endif
/* First allow environment variable override */
285
spot = SDL_getenv("SDL_VIDEO_X11_WMCLASS");
286
if ( spot ) {
287
SDL_strlcpy(classname, spot, maxlen);
288
289
290
291
return classname;
}
/* Next look at the application's executable name */
292
293
#if defined(__LINUX__) || defined(__FREEBSD__)
#if defined(__LINUX__)
294
SDL_snprintf(procfile, SDL_arraysize(procfile), "/proc/%d/exe", getpid());
295
#elif defined(__FREEBSD__)
296
SDL_snprintf(procfile, SDL_arraysize(procfile), "/proc/%d/file", getpid());
297
298
299
300
301
302
#else
#error Where can we find the executable name?
#endif
linksize = readlink(procfile, linkfile, sizeof(linkfile)-1);
if ( linksize > 0 ) {
linkfile[linksize] = '\0';
303
spot = SDL_strrchr(linkfile, '/');
304
if ( spot ) {
305
SDL_strlcpy(classname, spot+1, maxlen);
306
} else {
307
SDL_strlcpy(classname, linkfile, maxlen);
308
309
310
}
return classname;
}
311
#endif /* __LINUX__ */
312
313
/* Finally use the default we've used forever */
314
SDL_strlcpy(classname, "SDL_App", maxlen);
315
316
317
return classname;
}
318
319
320
/* Create auxiliary (toplevel) windows with the current visual */
static void create_aux_windows(_THIS)
{
321
int x = 0, y = 0;
322
char classname[1024];
323
324
XSetWindowAttributes xattr;
XWMHints *hints;
325
unsigned long app_event_mask;
326
327
int def_vis = (SDL_Visual == DefaultVisual(SDL_Display, SDL_Screen));
328
/* Look up some useful Atoms */
329
WM_DELETE_WINDOW = XInternAtom(SDL_Display, "WM_DELETE_WINDOW", False);
331
332
333
/* Don't create any extra windows if we are being managed */
if ( SDL_windowid ) {
FSwindow = 0;
334
WMwindow = SDL_strtol(SDL_windowid, NULL, 0);
335
336
337
338
return;
}
if(FSwindow)
339
XDestroyWindow(SDL_Display, FSwindow);
340
341
#if SDL_VIDEO_DRIVER_X11_XINERAMA
342
343
344
345
346
if ( use_xinerama ) {
x = xinerama_info.x_org;
y = xinerama_info.y_org;
}
#endif
347
348
349
350
351
xattr.override_redirect = True;
xattr.background_pixel = def_vis ? BlackPixel(SDL_Display, SDL_Screen) : 0;
xattr.border_pixel = 0;
xattr.colormap = SDL_XColorMap;
352
FSwindow = XCreateWindow(SDL_Display, SDL_Root,
353
x, y, 32, 32, 0,
354
355
356
357
358
this->hidden->depth, InputOutput, SDL_Visual,
CWOverrideRedirect | CWBackPixel | CWBorderPixel
| CWColormap,
&xattr);
359
XSelectInput(SDL_Display, FSwindow, StructureNotifyMask);
360
361
362
363
364
365
/* Tell KDE to keep the fullscreen window on top */
{
XEvent ev;
long mask;
366
SDL_memset(&ev, 0, sizeof(ev));
367
368
ev.xclient.type = ClientMessage;
ev.xclient.window = SDL_Root;
369
ev.xclient.message_type = XInternAtom(SDL_Display,
370
371
372
373
374
"KWM_KEEP_ON_TOP", False);
ev.xclient.format = 32;
ev.xclient.data.l[0] = FSwindow;
ev.xclient.data.l[1] = CurrentTime;
mask = SubstructureRedirectMask;
375
XSendEvent(SDL_Display, SDL_Root, False, mask, &ev);
376
377
378
379
380
}
hints = NULL;
if(WMwindow) {
/* All window attributes must survive the recreation */
381
382
hints = XGetWMHints(SDL_Display, WMwindow);
XDestroyWindow(SDL_Display, WMwindow);
383
384
385
386
}
/* Create the window for windowed management */
/* (reusing the xattr structure above) */
387
388
WMwindow = XCreateWindow(SDL_Display, SDL_Root,
x, y, 32, 32, 0,
389
390
391
392
393
394
this->hidden->depth, InputOutput, SDL_Visual,
CWBackPixel | CWBorderPixel | CWColormap,
&xattr);
/* Set the input hints so we get keyboard input */
if(!hints) {
395
hints = XAllocWMHints();
396
397
398
hints->input = True;
hints->flags = InputHint;
}
399
400
XSetWMHints(SDL_Display, WMwindow, hints);
XFree(hints);
401
X11_SetCaptionNoLock(this, this->wm_title, this->wm_icon);
402
403
404
405
app_event_mask = FocusChangeMask | KeyPressMask | KeyReleaseMask
| PropertyChangeMask | StructureNotifyMask | KeymapStateMask;
XSelectInput(SDL_Display, WMwindow, app_event_mask);
406
407
/* Set the class hints so we can get an icon (AfterStep) */
408
get_classname(classname, sizeof(classname));
409
410
{
XClassHint *classhints;
411
classhints = XAllocClassHint();
412
if(classhints != NULL) {
413
414
classhints->res_name = classname;
classhints->res_class = classname;
415
416
XSetClassHint(SDL_Display, WMwindow, classhints);
XFree(classhints);
417
418
419
}
}
420
{
421
422
423
424
union align_pid {
pid_t pid;
long dummy;
} a_pid;
425
char hostname[256];
426
427
a_pid.pid = getpid();
428
429
if (a_pid.pid > 0 && gethostname(hostname, sizeof(hostname)) > -1) {
430
431
432
Atom _NET_WM_PID = XInternAtom(SDL_Display, "_NET_WM_PID", False);
Atom WM_CLIENT_MACHINE = XInternAtom(SDL_Display, "WM_CLIENT_MACHINE", False);
433
hostname[sizeof(hostname)-1] = '\0';
434
XChangeProperty(SDL_Display, WMwindow, _NET_WM_PID, XA_CARDINAL, 32,
435
PropModeReplace, (unsigned char *)&(a_pid.pid), 1);
436
437
XChangeProperty(SDL_Display, WMwindow, WM_CLIENT_MACHINE, XA_STRING, 8,
PropModeReplace, (unsigned char *)hostname, SDL_strlen(hostname));
438
439
440
}
}
441
/* Setup the communication with the IM server */
442
443
444
/* create_aux_windows may be called several times against the same
Display. We should reuse the SDL_IM if one has been opened for
the Display, so we should not simply reset SDL_IM here. */
445
446
447
#ifdef X_HAVE_UTF8_STRING
if (SDL_X11_HAVE_UTF8) {
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
/* Discard obsolete resources if any. */
if (SDL_IM != NULL && SDL_Display != XDisplayOfIM(SDL_IM)) {
/* Just a double check. I don't think this
code is ever executed. */
SDL_SetError("display has changed while an IM is kept");
if (SDL_IC) {
XUnsetICFocus(SDL_IC);
XDestroyIC(SDL_IC);
SDL_IC = NULL;
}
XCloseIM(SDL_IM);
SDL_IM = NULL;
}
/* Open an input method. */
if (SDL_IM == NULL) {
464
465
466
char *old_locale = NULL, *old_modifiers = NULL;
const char *p;
size_t n;
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
/* I'm not comfortable to do locale setup
here. However, we need C library locale
(and xlib modifiers) to be set based on the
user's preference to use XIM, and many
existing game programs doesn't take care of
users' locale preferences, so someone other
than the game program should do it.
Moreover, ones say that some game programs
heavily rely on the C locale behaviour,
e.g., strcol()'s, and we can't change the C
library locale. Given the situation, I
couldn't find better place to do the
job... */
/* Save the current (application program's)
locale settings. */
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
p = setlocale(LC_ALL, NULL);
if ( p ) {
n = SDL_strlen(p)+1;
old_locale = SDL_stack_alloc(char, n);
if ( old_locale ) {
SDL_strlcpy(old_locale, p, n);
}
}
p = XSetLocaleModifiers(NULL);
if ( p ) {
n = SDL_strlen(p)+1;
old_modifiers = SDL_stack_alloc(char, n);
if ( old_modifiers ) {
SDL_strlcpy(old_modifiers, p, n);
}
498
499
500
501
502
503
504
505
506
507
508
}
/* Fetch the user's preferences and open the
input method with them. */
setlocale(LC_ALL, "");
XSetLocaleModifiers("");
SDL_IM = XOpenIM(SDL_Display, NULL, classname, classname);
/* Restore the application's locale settings
so that we don't break the application's
expected behaviour. */
509
if ( old_locale ) {
510
511
512
513
514
/* We need to restore the C library
locale first, since the
interpretation of the X modifier
may depend on it. */
setlocale(LC_ALL, old_locale);
515
516
517
SDL_stack_free(old_locale);
}
if ( old_modifiers ) {
518
XSetLocaleModifiers(old_modifiers);
519
SDL_stack_free(old_modifiers);
520
521
522
523
}
}
/* Create a new input context for the new window just created. */
524
525
526
if (SDL_IM == NULL) {
SDL_SetError("no input method could be opened");
} else {
527
528
529
530
531
532
533
534
535
536
537
if (SDL_IC != NULL) {
/* Discard the old IC before creating new one. */
XUnsetICFocus(SDL_IC);
XDestroyIC(SDL_IC);
}
/* Theoretically we should check the current IM supports
PreeditNothing+StatusNothing style (i.e., root window method)
before creating the IC. However, it is the bottom line method,
and we supports any other options. If the IM didn't support
root window method, the following call fails, and SDL falls
back to pre-XIM keyboard handling. */
538
539
540
SDL_IC = pXCreateIC(SDL_IM,
XNClientWindow, WMwindow,
XNFocusWindow, WMwindow,
541
XNInputStyle, XIMPreeditNothing | XIMStatusNothing,
542
543
544
545
546
547
548
549
XNResourceName, classname,
XNResourceClass, classname,
NULL);
if (SDL_IC == NULL) {
SDL_SetError("no input context could be created");
XCloseIM(SDL_IM);
SDL_IM = NULL;
550
551
552
553
554
555
556
557
558
559
} else {
/* We need to receive X events that an IM wants and to pass
them to the IM through XFilterEvent. The set of events may
vary depending on the IM implementation and the options
specified through various routes. Although unlikely, the
xlib specification allows IM to change the event requirement
with its own circumstances, it is safe to call SelectInput
whenever we re-create an IC. */
unsigned long mask = 0;
char *ret = pXGetICValues(SDL_IC, XNFilterEvents, &mask, NULL);
560
561
562
563
564
565
566
567
568
569
570
if (ret != NULL) {
XUnsetICFocus(SDL_IC);
XDestroyIC(SDL_IC);
SDL_IC = NULL;
SDL_SetError("no input context could be created");
XCloseIM(SDL_IM);
SDL_IM = NULL;
} else {
XSelectInput(SDL_Display, WMwindow, app_event_mask | mask);
XSetICFocus(SDL_IC);
}
571
572
}
}
573
}
574
#endif
575
576
577
/* Allow the window to be deleted by the window manager */
XSetWMProtocols(SDL_Display, WMwindow, &WM_DELETE_WINDOW, 1);
578
579
580
581
}
static int X11_VideoInit(_THIS, SDL_PixelFormat *vformat)
{
582
const char *env;
583
584
585
586
587
588
char *display;
int i;
/* Open the X11 display */
display = NULL; /* Get it from DISPLAY environment variable */
589
590
if ( (SDL_strncmp(XDisplayName(display), ":", 1) == 0) ||
(SDL_strncmp(XDisplayName(display), "unix:", 5) == 0) ) {
591
592
593
594
local_X11 = 1;
} else {
local_X11 = 0;
}
595
SDL_Display = XOpenDisplay(display);
596
#if defined(__osf__) && defined(SDL_VIDEO_DRIVER_X11_DYNAMIC)
597
598
599
600
601
602
603
604
605
606
/* 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);
607
SDL_Display = XOpenDisplay(display);
608
609
}
#endif
610
611
612
613
614
if ( SDL_Display == NULL ) {
SDL_SetError("Couldn't open X11 display");
return(-1);
}
#ifdef X11_DEBUG
615
XSynchronize(SDL_Display, True);
616
617
618
619
620
621
#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.
*/
622
GFX_Display = XOpenDisplay(display);
623
if ( GFX_Display == NULL ) {
624
625
XCloseDisplay(SDL_Display);
SDL_Display = NULL;
626
627
628
629
630
SDL_SetError("Couldn't open X11 display");
return(-1);
}
/* Set the normal X error handler */
631
X_handler = XSetErrorHandler(x_errhandler);
632
633
/* Set the error handler if we lose the X display */
634
XIO_handler = XSetIOErrorHandler(xio_errhandler);
635
636
/* Set the X extension error handler */
637
Xext_handler = XSetExtensionErrorHandler(xext_errhandler);
638
639
640
641
642
643
/* use default screen (from $DISPLAY) */
SDL_Screen = DefaultScreen(SDL_Display);
#ifndef NO_SHARED_MEMORY
/* Check for MIT shared memory extension */
644
use_mitshm = 0;
645
if ( local_X11 ) {
646
use_mitshm = XShmQueryExtension(SDL_Display);
647
648
649
650
}
#endif /* NO_SHARED_MEMORY */
/* Get the available video modes */
651
652
653
654
655
if(X11_GetVideoModes(this) < 0) {
XCloseDisplay(GFX_Display);
GFX_Display = NULL;
XCloseDisplay(SDL_Display);
SDL_Display = NULL;
656
return -1;
657
}
658
659
660
661
662
/* Determine the current screen size */
this->info.current_w = DisplayWidth(SDL_Display, SDL_Screen);
this->info.current_h = DisplayHeight(SDL_Display, SDL_Screen);
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
/* 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 {
678
SDL_XColorMap = XCreateColormap(SDL_Display, SDL_Root,
679
680
681
682
683
684
685
686
687
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;
}
688
689
690
if ( this->hidden->depth == 32 ) {
vformat->Amask = (0xFFFFFFFF & ~(vformat->Rmask|vformat->Gmask|vformat->Bmask));
}
691
692
X11_SaveVidModeGamma(this);
693
694
/* Allow environment override of screensaver disable. */
env = SDL_getenv("SDL_VIDEO_ALLOW_SCREENSAVER");
695
696
697
698
699
700
701
702
703
if ( env ) {
allow_screensaver = SDL_atoi(env);
} else {
#ifdef SDL_VIDEO_DISABLE_SCREENSAVER
allow_screensaver = 0;
#else
allow_screensaver = 1;
#endif
}
705
/* See if we have been passed a window to use */
706
SDL_windowid = SDL_getenv("SDL_WINDOWID");
707
708
709
710
711
712
713
714
715
716
717
718
719
/* 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! */
720
XFlush(SDL_Display);
721
722
723
724
725
726
727
728
729
730
731
732
733
734
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 ) {
735
XUnmapWindow(SDL_Display, WMwindow);
736
737
738
739
740
741
742
743
}
if ( screen && (screen->flags & SDL_FULLSCREEN) ) {
screen->flags &= ~SDL_FULLSCREEN;
X11_LeaveFullScreen(this);
}
/* Destroy the output window */
if ( SDL_Window ) {
744
XDestroyWindow(SDL_Display, SDL_Window);
745
746
747
748
749
750
751
752
753
}
/* 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 ) {
754
XFreeColors(GFX_Display,
755
756
757
758
SDL_DisplayColormap,&pixel,1,0);
--SDL_XPixels[pixel];
}
}
759
SDL_free(SDL_XPixels);
760
761
762
763
764
SDL_XPixels = NULL;
}
/* Free the graphics context */
if ( SDL_GC ) {
765
XFreeGC(SDL_Display, SDL_GC);
766
767
768
769
770
SDL_GC = 0;
}
}
}
771
772
static SDL_bool X11_WindowPosition(_THIS, int *x, int *y, int w, int h)
{
773
774
const char *window = SDL_getenv("SDL_VIDEO_WINDOW_POS");
const char *center = SDL_getenv("SDL_VIDEO_CENTERED");
775
if ( window ) {
776
if ( SDL_sscanf(window, "%d,%d", x, y) == 2 ) {
777
778
return SDL_TRUE;
}
779
if ( SDL_strcmp(window, "center") == 0 ) {
780
781
782
783
784
785
786
787
788
789
790
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;
}
791
792
793
794
static void X11_SetSizeHints(_THIS, int w, int h, Uint32 flags)
{
XSizeHints *hints;
795
hints = XAllocSizeHints();
796
if ( hints ) {
797
if (!(flags & SDL_RESIZABLE)) {
798
799
hints->min_width = hints->max_width = w;
hints->min_height = hints->max_height = h;
800
hints->flags = PMaxSize | PMinSize;
801
802
803
804
805
806
807
}
if ( flags & SDL_FULLSCREEN ) {
hints->x = 0;
hints->y = 0;
hints->flags |= USPosition;
} else
/* Center it, if desired */
808
if ( X11_WindowPosition(this, &hints->x, &hints->y, w, h) ) {
809
hints->flags |= USPosition;
810
811
812
813
814
/* Hints must be set before moving the window, otherwise an
unwanted ConfigureNotify event will be issued */
XSetWMNormalHints(SDL_Display, WMwindow, hints);
815
XMoveWindow(SDL_Display, WMwindow, hints->x, hints->y);
816
817
/* Flush the resize event so we don't catch it later */
818
XSync(SDL_Display, True);
819
}
820
821
XSetWMNormalHints(SDL_Display, WMwindow, hints);
XFree(hints);
822
823
824
825
826
827
828
829
830
831
832
}
/* 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 */
833
WM_HINTS = XInternAtom(SDL_Display, "_MOTIF_WM_HINTS", True);
834
835
836
837
838
839
840
841
842
843
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 };
844
XChangeProperty(SDL_Display, WMwindow,
845
846
847
848
849
850
851
WM_HINTS, WM_HINTS, 32,
PropModeReplace,
(unsigned char *)&MWMHints,
sizeof(MWMHints)/sizeof(long));
set = SDL_TRUE;
}
/* Now try to set KWM hints */
852
WM_HINTS = XInternAtom(SDL_Display, "KWM_WIN_DECORATION", True);
853
854
855
if ( WM_HINTS != None ) {
long KWMHints = 0;
856
XChangeProperty(SDL_Display, WMwindow,
857
858
859
860
861
862
863
WM_HINTS, WM_HINTS, 32,
PropModeReplace,
(unsigned char *)&KWMHints,
sizeof(KWMHints)/sizeof(long));
set = SDL_TRUE;
}
/* Now try to set GNOME hints */
864
WM_HINTS = XInternAtom(SDL_Display, "_WIN_HINTS", True);
865
866
867
if ( WM_HINTS != None ) {
long GNOMEHints = 0;
868
XChangeProperty(SDL_Display, WMwindow,
869
870
871
872
873
874
875
876
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 ) {
877
XSetTransientForHint(SDL_Display, WMwindow, SDL_Root);
878
879
880
881
882
883
884
885
886
}
} 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 */
887
WM_HINTS = XInternAtom(SDL_Display, "_MOTIF_WM_HINTS", True);
888
if ( WM_HINTS != None ) {
889
XDeleteProperty(SDL_Display, WMwindow, WM_HINTS);
890
891
892
set = SDL_TRUE;
}
/* Now try to unset KWM hints */
893
WM_HINTS = XInternAtom(SDL_Display, "KWM_WIN_DECORATION", True);
894
if ( WM_HINTS != None ) {
895
XDeleteProperty(SDL_Display, WMwindow, WM_HINTS);
896
897
898
set = SDL_TRUE;
}
/* Now try to unset GNOME hints */
899
WM_HINTS = XInternAtom(SDL_Display, "_WIN_HINTS", True);
900
if ( WM_HINTS != None ) {
901
XDeleteProperty(SDL_Display, WMwindow, WM_HINTS);
902
903
904
905
set = SDL_TRUE;
}
/* Finally unset the transient hints if necessary */
if ( ! set ) {
906
XDeleteProperty(SDL_Display, WMwindow, XA_WM_TRANSIENT_FOR);
907
908
909
910
911
912
913
914
915
916
}
}
}
static int X11_CreateWindow(_THIS, SDL_Surface *screen,
int w, int h, int bpp, Uint32 flags)
{
int i, depth;
Visual *vis;
int vis_change;
917
Uint32 Amask;
918
919
920
921
/* If a window is already present, destroy it and start fresh */
if ( SDL_Window ) {
X11_DestroyWindow(this, screen);
922
switch_waiting = 0; /* Prevent jump back to now-meaningless state. */
923
924
925
926
}
/* See if we have been given a window id */
if ( SDL_windowid ) {
927
SDL_Window = SDL_strtol(SDL_windowid, NULL, 0);
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
} 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;
945
XGetWindowAttributes(SDL_Display, SDL_Window, &a);
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
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 */
968
969
970
971
972
if ( this->hidden->depth == 32 ) {
Amask = (0xFFFFFFFF & ~(vis->red_mask|vis->green_mask|vis->blue_mask));
} else {
Amask = 0;
}
973
if ( ! SDL_ReallocFormat(screen, bpp,
974
vis->red_mask, vis->green_mask, vis->blue_mask, Amask) ) {
975
return -1;
977
978
979
/* Create the appropriate colormap */
if ( SDL_XColorMap != SDL_DisplayColormap ) {
980
XFreeColormap(SDL_Display, SDL_XColorMap);
981
982
983
984
985
986
}
if ( SDL_Visual->class == PseudoColor ) {
int ncolors;
/* Allocate the pixel flags */
ncolors = SDL_Visual->map_entries;
987
SDL_XPixels = SDL_malloc(ncolors * sizeof(int));
988
989
990
991
if(SDL_XPixels == NULL) {
SDL_OutOfMemory();
return -1;
}
992
SDL_memset(SDL_XPixels, 0, ncolors * sizeof(*SDL_XPixels));
993
994
995
996
997
998
999
/* 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;
1000
SDL_XColorMap = XCreateColormap(SDL_Display, SDL_Root,