This repository has been archived by the owner on Feb 11, 2021. It is now read-only.
/
SDL_x11video.c
1458 lines (1310 loc) · 46.8 KB
1
2
/*
SDL - Simple DirectMedia Layer
3
Copyright (C) 1997-2006 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
/* Initialization/Query functions */
58
59
60
61
static int X11_VideoInit(_THIS);
static SDL_Surface *X11_SetVideoMode(_THIS, SDL_Surface * current,
const SDL_DisplayMode * mode,
Uint32 flags);
62
63
64
static int X11_ToggleFullScreen(_THIS, int on);
static void X11_UpdateMouse(_THIS);
static int X11_SetColors(_THIS, int firstcolor, int ncolors,
65
66
SDL_Color * colors);
static int X11_SetGammaRamp(_THIS, Uint16 * ramp);
67
68
static void X11_VideoQuit(_THIS);
69
70
71
/* X11 driver bootstrap functions */
72
73
static int
X11_Available(void)
74
{
75
76
77
78
79
80
81
82
83
Display *display = NULL;
if (SDL_X11_LoadSymbols()) {
display = XOpenDisplay(NULL);
if (display != NULL) {
XCloseDisplay(display);
}
SDL_X11_UnloadSymbols();
}
return (display != NULL);
84
85
}
86
87
static void
X11_DeleteDevice(SDL_VideoDevice * device)
88
{
89
90
91
92
93
94
95
96
97
98
if (device) {
if (device->hidden) {
SDL_free(device->hidden);
}
if (device->gl_data) {
SDL_free(device->gl_data);
}
SDL_free(device);
SDL_X11_UnloadSymbols();
}
99
100
}
101
102
static SDL_VideoDevice *
X11_CreateDevice(int devindex)
103
{
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
SDL_VideoDevice *device = NULL;
if (SDL_X11_LoadSymbols()) {
/* Initialize all variables that we clean on shutdown */
device = (SDL_VideoDevice *) SDL_malloc(sizeof(SDL_VideoDevice));
if (device) {
SDL_memset(device, 0, (sizeof *device));
device->hidden = (struct SDL_PrivateVideoData *)
SDL_malloc((sizeof *device->hidden));
device->gl_data = (struct SDL_PrivateGLData *)
SDL_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);
}
SDL_memset(device->hidden, 0, (sizeof *device->hidden));
SDL_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->SetVideoMode = X11_SetVideoMode;
device->ToggleFullScreen = X11_ToggleFullScreen;
device->UpdateMouse = X11_UpdateMouse;
133
#if SDL_VIDEO_DRIVER_X11_XV
134
device->CreateYUVOverlay = X11_CreateYUVOverlay;
135
#endif
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
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;
152
#if SDL_VIDEO_OPENGL_GLX
153
154
155
156
157
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;
158
#endif
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
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;
}
return device;
176
177
178
}
VideoBootStrap X11_bootstrap = {
179
180
"x11", "X Window System",
X11_Available, X11_CreateDevice
181
182
183
};
/* Normal X11 error handler routine */
184
185
186
static int (*X_handler) (Display *, XErrorEvent *) = NULL;
static int
x_errhandler(Display * d, XErrorEvent * e)
187
{
188
#if SDL_VIDEO_DRIVER_X11_VIDMODE
189
extern int vm_error;
190
#endif
191
#if SDL_VIDEO_DRIVER_X11_DGAMOUSE
192
extern int dga_error;
193
194
#endif
195
#if SDL_VIDEO_DRIVER_X11_VIDMODE
196
197
198
199
200
201
202
203
204
/* 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))))) {
205
#ifdef X11_DEBUG
206
207
208
209
{
char errmsg[1024];
XGetErrorText(d, e->error_code, errmsg, sizeof(errmsg));
printf("VidMode error: %s\n", errmsg);
210
}
211
212
213
#endif
return (0);
}
214
#endif /* SDL_VIDEO_DRIVER_X11_VIDMODE */
215
216
#if SDL_VIDEO_DRIVER_X11_DGAMOUSE
217
218
219
220
/* DGA errors can be non-fatal. :) */
if ((dga_error >= 0) &&
((e->error_code > dga_error) &&
(e->error_code <= (dga_error + XF86DGANumberErrors)))) {
221
#ifdef X11_DEBUG
222
223
224
225
{
char errmsg[1024];
XGetErrorText(d, e->error_code, errmsg, sizeof(errmsg));
printf("DGA error: %s\n", errmsg);
226
}
227
228
229
#endif
return (0);
}
230
#endif /* SDL_VIDEO_DRIVER_X11_DGAMOUSE */
231
232
return (X_handler(d, e));
233
234
235
}
/* X11 I/O error handler routine */
236
237
238
static int (*XIO_handler) (Display *) = NULL;
static int
xio_errhandler(Display * d)
239
{
240
/* Ack! Lost X11 connection! */
241
242
243
244
245
246
/* 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;
247
248
249
/* Continue with the standard X11 error handler */
return (XIO_handler(d));
250
251
}
252
253
254
static int (*Xext_handler) (Display *, _Xconst char *, _Xconst char *) = NULL;
static int
xext_errhandler(Display * d, _Xconst char *ext, _Xconst char *reason)
255
{
256
#ifdef X11_DEBUG
257
258
259
printf("Xext error inside SDL (may be harmless):\n");
printf(" Extension \"%s\" %s on display \"%s\".\n",
ext, reason, XDisplayString(d));
260
261
#endif
262
263
264
265
266
267
268
269
270
271
272
if (SDL_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, reason);
273
274
}
275
/* Find out what class name we should use */
276
277
static char *
get_classname(char *classname, int maxlen)
278
{
279
char *spot;
280
#if defined(__LINUX__) || defined(__FREEBSD__)
281
282
283
char procfile[1024];
char linkfile[1024];
int linksize;
284
285
#endif
286
287
288
289
290
291
/* First allow environment variable override */
spot = SDL_getenv("SDL_VIDEO_X11_WMCLASS");
if (spot) {
SDL_strlcpy(classname, spot, maxlen);
return classname;
}
292
293
/* Next look at the application's executable name */
294
295
#if defined(__LINUX__) || defined(__FREEBSD__)
#if defined(__LINUX__)
296
SDL_snprintf(procfile, SDL_arraysize(procfile), "/proc/%d/exe", getpid());
297
#elif defined(__FREEBSD__)
298
299
SDL_snprintf(procfile, SDL_arraysize(procfile), "/proc/%d/file",
getpid());
300
301
302
#else
#error Where can we find the executable name?
#endif
303
304
305
306
307
308
309
310
311
312
313
linksize = readlink(procfile, linkfile, sizeof(linkfile) - 1);
if (linksize > 0) {
linkfile[linksize] = '\0';
spot = SDL_strrchr(linkfile, '/');
if (spot) {
SDL_strlcpy(classname, spot + 1, maxlen);
} else {
SDL_strlcpy(classname, linkfile, maxlen);
}
return classname;
}
314
#endif /* __LINUX__ */
315
316
317
318
/* Finally use the default we've used forever */
SDL_strlcpy(classname, "SDL_App", maxlen);
return classname;
319
320
}
321
/* Create auxiliary (toplevel) windows with the current visual */
322
323
static void
create_aux_windows(_THIS)
324
{
325
int x = 0, y = 0;
326
char classname[1024];
327
328
329
330
XSetWindowAttributes xattr;
XWMHints *hints;
int def_vis = (SDL_Visual == DefaultVisual(SDL_Display, SDL_Screen));
331
/* Look up some useful Atoms */
332
WM_DELETE_WINDOW = XInternAtom(SDL_Display, "WM_DELETE_WINDOW", False);
333
334
/* Don't create any extra windows if we are being managed */
335
336
337
if (SDL_windowid) {
FSwindow = 0;
WMwindow = SDL_strtol(SDL_windowid, NULL, 0);
338
339
340
return;
}
341
342
if (FSwindow)
XDestroyWindow(SDL_Display, FSwindow);
343
344
#if SDL_VIDEO_DRIVER_X11_XINERAMA
345
346
347
if (use_xinerama) {
x = xinerama[this->current_display].x_org;
y = xinerama[this->current_display].y_org;
348
349
}
#endif
350
xattr.override_redirect = True;
351
352
xattr.background_pixel =
def_vis ? BlackPixel(SDL_Display, SDL_Screen) : 0;
353
354
355
xattr.border_pixel = 0;
xattr.colormap = SDL_XColorMap;
356
FSwindow = XCreateWindow(SDL_Display, SDL_Root,
357
x, y, 32, 32, 0,
358
359
360
this->hidden->depth, InputOutput, SDL_Visual,
CWOverrideRedirect | CWBackPixel | CWBorderPixel
| CWColormap, &xattr);
361
362
XSelectInput(SDL_Display, FSwindow, StructureNotifyMask);
363
364
365
/* Tell KDE to keep the fullscreen window on top */
{
366
367
368
369
370
371
372
373
374
375
376
377
378
XEvent ev;
long mask;
SDL_memset(&ev, 0, sizeof(ev));
ev.xclient.type = ClientMessage;
ev.xclient.window = SDL_Root;
ev.xclient.message_type = XInternAtom(SDL_Display,
"KWM_KEEP_ON_TOP", False);
ev.xclient.format = 32;
ev.xclient.data.l[0] = FSwindow;
ev.xclient.data.l[1] = CurrentTime;
mask = SubstructureRedirectMask;
XSendEvent(SDL_Display, SDL_Root, False, mask, &ev);
379
380
381
}
hints = NULL;
382
383
384
385
if (WMwindow) {
/* All window attributes must survive the recreation */
hints = XGetWMHints(SDL_Display, WMwindow);
XDestroyWindow(SDL_Display, WMwindow);
386
387
388
389
}
/* Create the window for windowed management */
/* (reusing the xattr structure above) */
390
391
WMwindow = XCreateWindow(SDL_Display, SDL_Root,
x, y, 32, 32, 0,
392
393
394
this->hidden->depth, InputOutput, SDL_Visual,
CWBackPixel | CWBorderPixel | CWColormap,
&xattr);
395
396
/* Set the input hints so we get keyboard input */
397
398
399
400
if (!hints) {
hints = XAllocWMHints();
hints->input = True;
hints->flags = InputHint;
401
}
402
403
XSetWMHints(SDL_Display, WMwindow, hints);
XFree(hints);
404
405
X11_SetCaptionNoLock(this, SDL_CurrentWindow.wm_title,
SDL_CurrentWindow.wm_icon);
406
407
XSelectInput(SDL_Display, WMwindow,
408
409
410
FocusChangeMask | KeyPressMask | KeyReleaseMask
| PropertyChangeMask | StructureNotifyMask |
KeymapStateMask);
411
412
/* Set the class hints so we can get an icon (AfterStep) */
413
get_classname(classname, sizeof(classname));
414
{
415
416
417
418
419
420
421
422
XClassHint *classhints;
classhints = XAllocClassHint();
if (classhints != NULL) {
classhints->res_name = classname;
classhints->res_class = classname;
XSetClassHint(SDL_Display, WMwindow, classhints);
XFree(classhints);
}
423
424
}
425
/* Setup the communication with the IM server */
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
SDL_IM = NULL;
SDL_IC = NULL;
#ifdef X_HAVE_UTF8_STRING
if (SDL_X11_HAVE_UTF8) {
SDL_IM = XOpenIM(SDL_Display, NULL, classname, classname);
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, classname,
XNResourceClass, classname, NULL);
if (SDL_IC == NULL) {
SDL_SetError("no input context could be created");
XCloseIM(SDL_IM);
SDL_IM = NULL;
}
}
}
#endif
/* Allow the window to be deleted by the window manager */
XSetWMProtocols(SDL_Display, WMwindow, &WM_DELETE_WINDOW, 1);
454
455
}
456
457
static int
X11_VideoInit(_THIS)
458
{
459
460
461
462
463
464
465
466
467
468
469
470
471
472
char *display;
int i;
SDL_DisplayMode desktop_mode;
/* Open the X11 display */
display = NULL; /* Get it from DISPLAY environment variable */
if ((SDL_strncmp(XDisplayName(display), ":", 1) == 0) ||
(SDL_strncmp(XDisplayName(display), "unix:", 5) == 0)) {
local_X11 = 1;
} else {
local_X11 = 0;
}
SDL_Display = XOpenDisplay(display);
473
#if defined(__osf__) && defined(SDL_VIDEO_DRIVER_X11_DYNAMIC)
474
475
476
477
478
479
480
481
482
483
484
485
/* 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 = XOpenDisplay(display);
}
486
#endif
487
488
489
490
if (SDL_Display == NULL) {
SDL_SetError("Couldn't open X11 display");
return (-1);
}
491
#ifdef X11_DEBUG
492
XSynchronize(SDL_Display, True);
493
494
#endif
495
496
497
498
499
500
501
502
503
/* 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.
*/
GFX_Display = XOpenDisplay(display);
if (GFX_Display == NULL) {
SDL_SetError("Couldn't open X11 display");
return (-1);
}
504
505
506
/* Set the normal X error handler */
X_handler = XSetErrorHandler(x_errhandler);
507
508
509
/* Set the error handler if we lose the X display */
XIO_handler = XSetIOErrorHandler(xio_errhandler);
510
511
512
/* Set the X extension error handler */
Xext_handler = XSetExtensionErrorHandler(xext_errhandler);
513
514
515
/* use default screen (from $DISPLAY) */
SDL_Screen = DefaultScreen(SDL_Display);
516
517
#ifndef NO_SHARED_MEMORY
518
519
520
521
522
/* Check for MIT shared memory extension */
use_mitshm = 0;
if (local_X11) {
use_mitshm = XShmQueryExtension(SDL_Display);
}
523
524
#endif /* NO_SHARED_MEMORY */
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
/* Get the available visuals */
if (X11_GetVisuals(this) < 0)
return -1;
/* Determine the default screen mode:
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 {
SDL_XColorMap = XCreateColormap(SDL_Display, SDL_Root,
SDL_Visual, AllocNone);
}
desktop_mode.format =
X11_VisualToFormat(this->hidden->visuals[i].visual,
this->hidden->visuals[i].depth,
this->hidden->visuals[i].bpp);
desktop_mode.w = DisplayWidth(SDL_Display, SDL_Screen);
desktop_mode.h = DisplayHeight(SDL_Display, SDL_Screen);
desktop_mode.refresh_rate = 0;
SDL_AddVideoDisplay(&desktop_mode);
/* Get the available video modes */
if (X11_GetVideoModes(this) < 0)
return -1;
X11_SaveVidModeGamma(this);
/* Save DPMS and screensaver settings */
X11_SaveScreenSaver(SDL_Display, &screensaver_timeout, &dpms_enabled);
X11_DisableScreenSaver(SDL_Display);
/* See if we have been passed a window to use */
SDL_windowid = SDL_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! */
XFlush(SDL_Display);
return (0);
583
584
}
585
586
static void
X11_DestroyWindow(_THIS, SDL_Surface * screen)
587
{
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
/* Clean up OpenGL */
if (screen) {
screen->flags &= ~SDL_INTERNALOPENGL;
}
X11_GL_Shutdown(this);
if (!SDL_windowid) {
/* Hide the managed window */
if (WMwindow) {
XUnmapWindow(SDL_Display, WMwindow);
}
if (screen && (screen->flags & SDL_FULLSCREEN)) {
screen->flags &= ~SDL_FULLSCREEN;
X11_LeaveFullScreen(this);
}
/* Destroy the output window */
if (SDL_Window) {
XDestroyWindow(SDL_Display, SDL_Window);
}
/* 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) {
XFreeColors(GFX_Display,
SDL_DisplayColormap, &pixel, 1, 0);
--SDL_XPixels[pixel];
}
}
SDL_free(SDL_XPixels);
SDL_XPixels = NULL;
}
/* Free the graphics context */
if (SDL_GC) {
XFreeGC(SDL_Display, SDL_GC);
SDL_GC = 0;
}
}
631
632
}
633
634
static SDL_bool
X11_WindowPosition(_THIS, int *x, int *y, int w, int h)
635
{
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
const char *window = SDL_getenv("SDL_VIDEO_WINDOW_POS");
const char *center = SDL_getenv("SDL_VIDEO_CENTERED");
if (window) {
if (SDL_sscanf(window, "%d,%d", x, y) == 2) {
return SDL_TRUE;
}
if (SDL_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;
652
653
}
654
655
static void
X11_SetSizeHints(_THIS, int w, int h, Uint32 flags)
656
{
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
XSizeHints *hints;
hints = XAllocSizeHints();
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 */
if (X11_WindowPosition(this, &hints->x, &hints->y, w, h)) {
hints->flags |= USPosition;
XMoveWindow(SDL_Display, WMwindow, hints->x, hints->y);
/* Flush the resize event so we don't catch it later */
XSync(SDL_Display, True);
}
XSetWMNormalHints(SDL_Display, WMwindow, hints);
XFree(hints);
}
/* 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 */
WM_HINTS = XInternAtom(SDL_Display, "_MOTIF_WM_HINTS", True);
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};
XChangeProperty(SDL_Display, WMwindow,
WM_HINTS, WM_HINTS, 32,
PropModeReplace,
(unsigned char *) &MWMHints,
sizeof(MWMHints) / sizeof(long));
set = SDL_TRUE;
}
/* Now try to set KWM hints */
WM_HINTS = XInternAtom(SDL_Display, "KWM_WIN_DECORATION", True);
if (WM_HINTS != None) {
long KWMHints = 0;
XChangeProperty(SDL_Display, WMwindow,
WM_HINTS, WM_HINTS, 32,
PropModeReplace,
(unsigned char *) &KWMHints,
sizeof(KWMHints) / sizeof(long));
set = SDL_TRUE;
}
/* Now try to set GNOME hints */
WM_HINTS = XInternAtom(SDL_Display, "_WIN_HINTS", True);
if (WM_HINTS != None) {
long GNOMEHints = 0;
XChangeProperty(SDL_Display, WMwindow,
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) {
XSetTransientForHint(SDL_Display, WMwindow, SDL_Root);
}
} 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 */
WM_HINTS = XInternAtom(SDL_Display, "_MOTIF_WM_HINTS", True);
if (WM_HINTS != None) {
XDeleteProperty(SDL_Display, WMwindow, WM_HINTS);
set = SDL_TRUE;
}
/* Now try to unset KWM hints */
WM_HINTS = XInternAtom(SDL_Display, "KWM_WIN_DECORATION", True);
if (WM_HINTS != None) {
XDeleteProperty(SDL_Display, WMwindow, WM_HINTS);
set = SDL_TRUE;
}
/* Now try to unset GNOME hints */
WM_HINTS = XInternAtom(SDL_Display, "_WIN_HINTS", True);
if (WM_HINTS != None) {
XDeleteProperty(SDL_Display, WMwindow, WM_HINTS);
set = SDL_TRUE;
}
/* Finally unset the transient hints if necessary */
if (!set) {
/* NOTE: Does this work? */
XSetTransientForHint(SDL_Display, WMwindow, None);
}
}
776
777
}
778
779
780
static int
X11_CreateWindow(_THIS, SDL_Surface * screen,
const SDL_DisplayMode * mode, Uint32 flags)
781
{
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
int i, depth;
Visual *vis;
int vis_change;
int bpp;
Uint32 Rmask, Gmask, Bmask, Amask;
SDL_PixelFormatEnumToMasks(mode->format, &bpp, &Rmask, &Gmask, &Bmask,
&Amask);
/* If a window is already present, destroy it and start fresh */
if (SDL_Window) {
X11_DestroyWindow(this, screen);
switch_waiting = 0; /* Prevent jump back to now-meaningless state. */
}
/* See if we have been given a window id */
if (SDL_windowid) {
SDL_Window = SDL_strtol(SDL_windowid, NULL, 0);
} else {
SDL_Window = 0;
}
/* find out which visual we are going to use */
if (flags & SDL_INTERNALOPENGL) {
XVisualInfo *vi;
vi = X11_GL_GetVisual(this);
if (!vi) {
return -1;
}
vis = vi->visual;
depth = vi->depth;
} else if (SDL_windowid) {
XWindowAttributes a;
XGetWindowAttributes(SDL_Display, SDL_Window, &a);
vis = a.visual;
depth = a.depth;
} else {
for (i = 0; i < this->hidden->nvisuals; i++) {
if (this->hidden->visuals[i].bpp == bpp &&
this->hidden->visuals[i].visual->red_mask == Rmask &&
this->hidden->visuals[i].visual->green_mask == Gmask &&
this->hidden->visuals[i].visual->blue_mask == Bmask)
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;
}
835
#ifdef X11_DEBUG
836
837
838
839
840
841
842
843
844
845
846
847
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);
848
#endif
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
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, Rmask, Gmask, Bmask, Amask)) {
return -1;
}
/* Create the appropriate colormap */
if (SDL_XColorMap != SDL_DisplayColormap) {
XFreeColormap(SDL_Display, SDL_XColorMap);
}
if (SDL_Visual->class == PseudoColor) {
int ncolors;
/* Allocate the pixel flags */
ncolors = SDL_Visual->map_entries;
SDL_XPixels = SDL_malloc(ncolors * sizeof(int));
if (SDL_XPixels == NULL) {
SDL_OutOfMemory();
return -1;
}
SDL_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;
SDL_XColorMap = XCreateColormap(SDL_Display, SDL_Root,
SDL_Visual, AllocAll);
} else {
SDL_XColorMap = SDL_DisplayColormap;
}
} else if (SDL_Visual->class == DirectColor) {
/* Create a colormap which we can manipulate for gamma */
SDL_XColorMap = XCreateColormap(SDL_Display, SDL_Root,
SDL_Visual, AllocAll);
XSync(SDL_Display, False);
/* Initialize the colormap to the identity mapping */
SDL_GetGammaRamp(0, 0, 0);
SDL_VideoSurface = screen;
X11_SetGammaRamp(this, SDL_CurrentWindow.gamma);
SDL_VideoSurface = NULL;
} else {
/* Create a read-only colormap for our window */
SDL_XColorMap = XCreateColormap(SDL_Display, SDL_Root,
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 */
XSetWindowBackground(SDL_Display, FSwindow, 0);
XClearWindow(SDL_Display, FSwindow);
}
/* resize the (possibly new) window manager window */
if (!SDL_windowid) {
X11_SetSizeHints(this, mode->w, mode->h, flags);
window_w = mode->w;
window_h = mode->h;
XResizeWindow(SDL_Display, WMwindow, mode->w, mode->h);
}
/* Create (or use) the X11 display window */
if (!SDL_windowid) {
if (flags & SDL_INTERNALOPENGL) {
if (X11_GL_CreateWindow(this, mode->w, mode->h) < 0) {
return (-1);
}
} else {
XSetWindowAttributes swa;
swa.background_pixel = 0;
swa.border_pixel = 0;
swa.colormap = SDL_XColorMap;
SDL_Window = XCreateWindow(SDL_Display, WMwindow,
0, 0, mode->w, mode->h, 0, depth,
InputOutput, SDL_Visual,
CWBackPixel | CWBorderPixel
| CWColormap, &swa);
}
/* Only manage our input if we own the window */
XSelectInput(SDL_Display, SDL_Window,
(EnterWindowMask | LeaveWindowMask
| ButtonPressMask | ButtonReleaseMask
| PointerMotionMask | ExposureMask));
}
/* Create the graphics context here, once we have a window */
if (flags & SDL_INTERNALOPENGL) {
if (X11_GL_CreateContext(this) < 0) {
return (-1);
} else {
screen->flags |= SDL_INTERNALOPENGL;
}
} else {
XGCValues gcv;
gcv.graphics_exposures = False;
SDL_GC = XCreateGC(SDL_Display, SDL_Window,
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_INTERNALOPENGL)) {
XSetWindowColormap(SDL_Display, SDL_Window, SDL_XColorMap);
if (!SDL_windowid) {
XSetWindowColormap(SDL_Display, FSwindow, SDL_XColorMap);
XSetWindowColormap(SDL_Display, WMwindow, SDL_XColorMap);
}
}
#if 0 /* This is an experiment - are the graphics faster now? - nope. */
if (SDL_getenv("SDL_VIDEO_X11_BACKINGSTORE"))
977
#endif
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* 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) {
XChangeWindowAttributes(SDL_Display, SDL_Window,
CWBackingStore, &a);
}
}
/* Update the internal keyboard state */
X11_SetKeyboardState(SDL_Display, NULL);
/* 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: