Skip to content

Latest commit

 

History

History
434 lines (395 loc) · 11.8 KB

SDL_x11wm.c

File metadata and controls

434 lines (395 loc) · 11.8 KB
 
Apr 26, 2001
Apr 26, 2001
1
2
/*
SDL - Simple DirectMedia Layer
Dec 8, 2008
Dec 8, 2008
3
Copyright (C) 1997-2009 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
*/
Feb 21, 2006
Feb 21, 2006
22
#include "SDL_config.h"
Apr 26, 2001
Apr 26, 2001
23
24
25
26
27
28
29
30
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include "SDL_version.h"
#include "SDL_timer.h"
#include "SDL_video.h"
#include "SDL_syswm.h"
Feb 16, 2006
Feb 16, 2006
31
32
#include "../SDL_pixels_c.h"
#include "../../events/SDL_events_c.h"
Apr 26, 2001
Apr 26, 2001
33
34
35
#include "SDL_x11modes_c.h"
#include "SDL_x11wm_c.h"
Nov 7, 2001
Nov 7, 2001
36
37
38
39
40
41
42
static Uint8 reverse_byte(Uint8 x)
{
x = (x & 0xaa) >> 1 | (x & 0x55) << 1;
x = (x & 0xcc) >> 2 | (x & 0x33) << 2;
x = (x & 0xf0) >> 4 | (x & 0x0f) << 4;
return x;
}
Apr 26, 2001
Apr 26, 2001
43
44
45
46
47
48
49
50
void X11_SetIcon(_THIS, SDL_Surface *icon, Uint8 *mask)
{
SDL_Surface *sicon;
XWMHints *wmhints;
XImage *icon_image;
Pixmap icon_pixmap;
Pixmap mask_pixmap;
Nov 7, 2001
Nov 7, 2001
51
52
Window icon_window = None;
GC gc;
Apr 26, 2001
Apr 26, 2001
53
XGCValues GCvalues;
Nov 7, 2001
Nov 7, 2001
54
int i, dbpp;
Apr 26, 2001
Apr 26, 2001
55
SDL_Rect bounds;
Nov 7, 2001
Nov 7, 2001
56
Uint8 *LSBmask;
Apr 26, 2001
Apr 26, 2001
57
Visual *dvis;
Nov 7, 2001
Nov 7, 2001
58
59
char *p;
int masksize;
Apr 26, 2001
Apr 26, 2001
60
61
62
63
64
SDL_Lock_EventThread();
/* The icon must use the default visual, depth and colormap of the
screen, so it might need a conversion */
Nov 7, 2001
Nov 7, 2001
65
dvis = DefaultVisual(SDL_Display, SDL_Screen);
Apr 26, 2001
Apr 26, 2001
66
dbpp = DefaultDepth(SDL_Display, SDL_Screen);
Nov 7, 2001
Nov 7, 2001
67
68
69
70
71
for(i = 0; i < this->hidden->nvisuals; i++) {
if(this->hidden->visuals[i].visual == dvis) {
dbpp = this->hidden->visuals[i].bpp;
break;
}
Apr 26, 2001
Apr 26, 2001
72
73
74
75
76
77
78
}
/* The Visual struct is supposed to be opaque but we cheat a little */
sicon = SDL_CreateRGBSurface(SDL_SWSURFACE, icon->w, icon->h,
dbpp,
dvis->red_mask, dvis->green_mask,
dvis->blue_mask, 0);
Nov 7, 2001
Nov 7, 2001
79
if ( sicon == NULL )
Apr 26, 2001
Apr 26, 2001
80
81
goto done;
Nov 7, 2001
Nov 7, 2001
82
83
84
85
86
87
88
89
90
91
92
93
if(dbpp == 8) {
/* Default visual is 8bit; we need to allocate colours from
the default colormap */
SDL_Color want[256], got[256];
int nwant;
Colormap dcmap;
int missing;
dcmap = DefaultColormap(SDL_Display, SDL_Screen);
if(icon->format->palette) {
/* The icon has a palette as well - we just have to
find those colours */
nwant = icon->format->palette->ncolors;
Feb 7, 2006
Feb 7, 2006
94
SDL_memcpy(want, icon->format->palette->colors,
Nov 7, 2001
Nov 7, 2001
95
96
97
98
99
100
101
102
103
104
105
106
107
nwant * sizeof want[0]);
} else {
/* try the standard 6x6x6 cube for lack of better
ideas */
int r, g, b, i;
for(r = i = 0; r < 256; r += 0x33)
for(g = 0; g < 256; g += 0x33)
for(b = 0; b < 256; b += 0x33, i++) {
want[i].r = r;
want[i].g = g;
want[i].b = b;
}
nwant = 216;
Apr 26, 2001
Apr 26, 2001
108
}
Nov 7, 2001
Nov 7, 2001
109
110
111
112
113
114
115
116
117
118
if(SDL_iconcolors) {
/* free already allocated colours first */
unsigned long freelist[512];
int nfree = 0;
for(i = 0; i < 256; i++) {
while(SDL_iconcolors[i]) {
freelist[nfree++] = i;
SDL_iconcolors[i]--;
}
}
Mar 22, 2006
Mar 22, 2006
119
XFreeColors(GFX_Display, dcmap, freelist, nfree, 0);
Apr 26, 2001
Apr 26, 2001
120
}
Nov 7, 2001
Nov 7, 2001
121
if(!SDL_iconcolors)
Feb 7, 2006
Feb 7, 2006
122
123
SDL_iconcolors = SDL_malloc(256 * sizeof *SDL_iconcolors);
SDL_memset(SDL_iconcolors, 0, 256 * sizeof *SDL_iconcolors);
Apr 26, 2001
Apr 26, 2001
124
Nov 7, 2001
Nov 7, 2001
125
/* try to allocate the colours */
Feb 7, 2006
Feb 7, 2006
126
SDL_memset(got, 0, sizeof got);
Nov 7, 2001
Nov 7, 2001
127
128
129
130
131
132
133
missing = 0;
for(i = 0; i < nwant; i++) {
XColor c;
c.red = want[i].r << 8;
c.green = want[i].g << 8;
c.blue = want[i].b << 8;
c.flags = DoRed | DoGreen | DoBlue;
Mar 22, 2006
Mar 22, 2006
134
if(XAllocColor(GFX_Display, dcmap, &c)) {
Nov 7, 2001
Nov 7, 2001
135
136
137
138
139
140
141
142
143
144
145
146
147
/* got the colour */
SDL_iconcolors[c.pixel]++;
got[c.pixel] = want[i];
} else {
missing = 1;
}
}
if(missing) {
/* Some colours were apparently missing, so we just
allocate all the rest as well */
XColor cols[256];
for(i = 0; i < 256; i++)
cols[i].pixel = i;
Mar 22, 2006
Mar 22, 2006
148
XQueryColors(GFX_Display, dcmap, cols, 256);
Nov 7, 2001
Nov 7, 2001
149
150
151
152
153
for(i = 0; i < 256; i++) {
got[i].r = cols[i].red >> 8;
got[i].g = cols[i].green >> 8;
got[i].b = cols[i].blue >> 8;
if(!SDL_iconcolors[i]) {
Mar 22, 2006
Mar 22, 2006
154
if(XAllocColor(GFX_Display, dcmap,
Nov 7, 2001
Nov 7, 2001
155
156
157
158
159
160
161
162
cols + i)) {
SDL_iconcolors[i] = 1;
} else {
/* index not available */
got[i].r = 0;
got[i].g = 0;
got[i].b = 0;
}
Apr 26, 2001
Apr 26, 2001
163
164
165
}
}
}
Nov 7, 2001
Nov 7, 2001
166
167
SDL_SetColors(sicon, got, 0, 256);
Apr 26, 2001
Apr 26, 2001
168
169
}
Nov 7, 2001
Nov 7, 2001
170
171
172
173
174
175
176
177
178
179
bounds.x = 0;
bounds.y = 0;
bounds.w = icon->w;
bounds.h = icon->h;
if ( SDL_LowerBlit(icon, &bounds, sicon, &bounds) < 0 )
goto done;
/* We need the mask as given, except in LSBfirst format instead of
MSBfirst. Reverse the bits in each byte. */
masksize = ((sicon->w + 7) >> 3) * sicon->h;
Feb 7, 2006
Feb 7, 2006
180
LSBmask = SDL_malloc(masksize);
Apr 26, 2001
Apr 26, 2001
181
182
183
if ( LSBmask == NULL ) {
goto done;
}
Feb 7, 2006
Feb 7, 2006
184
SDL_memset(LSBmask, 0, masksize);
Nov 7, 2001
Nov 7, 2001
185
186
for(i = 0; i < masksize; i++)
LSBmask[i] = reverse_byte(mask[i]);
Mar 22, 2006
Mar 22, 2006
187
mask_pixmap = XCreatePixmapFromBitmapData(SDL_Display, WMwindow,
Nov 7, 2001
Nov 7, 2001
188
189
190
(char *)LSBmask,
sicon->w, sicon->h,
1L, 0L, 1);
Apr 26, 2001
Apr 26, 2001
191
192
/* Transfer the image to an X11 pixmap */
Mar 22, 2006
Mar 22, 2006
193
icon_image = XCreateImage(SDL_Display,
Nov 7, 2001
Nov 7, 2001
194
195
196
197
198
DefaultVisual(SDL_Display, SDL_Screen),
DefaultDepth(SDL_Display, SDL_Screen),
ZPixmap, 0, sicon->pixels,
sicon->w, sicon->h,
32, 0);
Nov 27, 2001
Nov 27, 2001
199
200
icon_image->byte_order = (SDL_BYTEORDER == SDL_BIG_ENDIAN)
? MSBFirst : LSBFirst;
Mar 22, 2006
Mar 22, 2006
201
icon_pixmap = XCreatePixmap(SDL_Display, SDL_Root, sicon->w, sicon->h,
Nov 7, 2001
Nov 7, 2001
202
DefaultDepth(SDL_Display, SDL_Screen));
Mar 22, 2006
Mar 22, 2006
203
204
gc = XCreateGC(SDL_Display, icon_pixmap, 0, &GCvalues);
XPutImage(SDL_Display, icon_pixmap, gc, icon_image,
Nov 7, 2001
Nov 7, 2001
205
0, 0, 0, 0, sicon->w, sicon->h);
Mar 22, 2006
Mar 22, 2006
206
207
XFreeGC(SDL_Display, gc);
XDestroyImage(icon_image);
Feb 7, 2006
Feb 7, 2006
208
SDL_free(LSBmask);
Apr 26, 2001
Apr 26, 2001
209
210
sicon->pixels = NULL;
Nov 7, 2001
Nov 7, 2001
211
212
213
/* Some buggy window managers (some versions of Enlightenment, it
seems) need an icon window *and* icon pixmap to work properly, while
it screws up others. The default is only to use a pixmap. */
Feb 7, 2006
Feb 7, 2006
214
p = SDL_getenv("SDL_VIDEO_X11_ICONWIN");
Nov 7, 2001
Nov 7, 2001
215
if(p && *p) {
Mar 22, 2006
Mar 22, 2006
216
icon_window = XCreateSimpleWindow(SDL_Display, SDL_Root,
Nov 7, 2001
Nov 7, 2001
217
218
219
0, 0, sicon->w, sicon->h, 0,
CopyFromParent,
CopyFromParent);
Mar 22, 2006
Mar 22, 2006
220
XSetWindowBackgroundPixmap(SDL_Display, icon_window,
Nov 7, 2001
Nov 7, 2001
221
icon_pixmap);
Mar 22, 2006
Mar 22, 2006
222
XClearWindow(SDL_Display, icon_window);
Nov 7, 2001
Nov 7, 2001
223
}
Apr 26, 2001
Apr 26, 2001
224
225
/* Set the window icon to the icon pixmap (and icon window) */
Mar 22, 2006
Mar 22, 2006
226
wmhints = XAllocWMHints();
Sep 27, 2009
Sep 27, 2009
227
wmhints->flags = (IconPixmapHint | IconMaskHint | InputHint);
Apr 26, 2001
Apr 26, 2001
228
229
wmhints->icon_pixmap = icon_pixmap;
wmhints->icon_mask = mask_pixmap;
Sep 27, 2009
Sep 27, 2009
230
wmhints->input = True;
Nov 7, 2001
Nov 7, 2001
231
232
233
234
if(icon_window != None) {
wmhints->flags |= IconWindowHint;
wmhints->icon_window = icon_window;
}
Mar 22, 2006
Mar 22, 2006
235
236
237
XSetWMHints(SDL_Display, WMwindow, wmhints);
XFree(wmhints);
XSync(SDL_Display, False);
Apr 26, 2001
Apr 26, 2001
238
239
240
done:
SDL_Unlock_EventThread();
Nov 7, 2001
Nov 7, 2001
241
SDL_FreeSurface(sicon);
Apr 26, 2001
Apr 26, 2001
242
243
}
May 5, 2006
May 5, 2006
244
void X11_SetCaptionNoLock(_THIS, const char *title, const char *icon)
Apr 26, 2001
Apr 26, 2001
245
246
{
XTextProperty titleprop, iconprop;
Mar 20, 2006
Mar 20, 2006
247
248
249
Status status;
#ifdef X_HAVE_UTF8_STRING
May 17, 2006
May 17, 2006
250
251
Atom _NET_WM_NAME = 0;
Atom _NET_WM_ICON_NAME = 0;
Mar 20, 2006
Mar 20, 2006
252
253
/* Look up some useful Atoms */
Mar 22, 2006
Mar 22, 2006
254
255
256
257
if (SDL_X11_HAVE_UTF8) {
_NET_WM_NAME = XInternAtom(SDL_Display, "_NET_WM_NAME", False);
_NET_WM_ICON_NAME = XInternAtom(SDL_Display, "_NET_WM_ICON_NAME", False);
}
Mar 20, 2006
Mar 20, 2006
258
#endif
Apr 26, 2001
Apr 26, 2001
259
260
if ( title != NULL ) {
Jul 4, 2007
Jul 4, 2007
261
262
char *title_locale = SDL_iconv_utf8_locale(title);
if ( !title_locale ) {
Mar 20, 2006
Mar 20, 2006
263
264
265
SDL_OutOfMemory();
return;
}
Jul 4, 2007
Jul 4, 2007
266
267
status = XStringListToTextProperty(&title_locale, 1, &titleprop);
SDL_free(title_locale);
Mar 20, 2006
Mar 20, 2006
268
if ( status ) {
Mar 22, 2006
Mar 22, 2006
269
270
XSetTextProperty(SDL_Display, WMwindow, &titleprop, XA_WM_NAME);
XFree(titleprop.value);
Mar 20, 2006
Mar 20, 2006
271
}
Jul 25, 2004
Jul 25, 2004
272
#ifdef X_HAVE_UTF8_STRING
Mar 22, 2006
Mar 22, 2006
273
274
275
276
277
278
279
if (SDL_X11_HAVE_UTF8) {
status = Xutf8TextListToTextProperty(SDL_Display,
(char **)&title, 1, XUTF8StringStyle, &titleprop);
if ( status == Success ) {
XSetTextProperty(SDL_Display, WMwindow, &titleprop, _NET_WM_NAME);
XFree(titleprop.value);
}
Sep 11, 2004
Sep 11, 2004
280
}
Mar 20, 2006
Mar 20, 2006
281
#endif
Apr 26, 2001
Apr 26, 2001
282
283
}
if ( icon != NULL ) {
Jul 4, 2007
Jul 4, 2007
284
285
char *icon_locale = SDL_iconv_utf8_locale(icon);
if ( !icon_locale ) {
Mar 20, 2006
Mar 20, 2006
286
287
288
SDL_OutOfMemory();
return;
}
Jul 4, 2007
Jul 4, 2007
289
290
status = XStringListToTextProperty(&icon_locale, 1, &iconprop);
SDL_free(icon_locale);
Mar 20, 2006
Mar 20, 2006
291
if ( status ) {
Mar 22, 2006
Mar 22, 2006
292
293
XSetTextProperty(SDL_Display, WMwindow, &iconprop, XA_WM_ICON_NAME);
XFree(iconprop.value);
Mar 20, 2006
Mar 20, 2006
294
}
Jul 25, 2004
Jul 25, 2004
295
#ifdef X_HAVE_UTF8_STRING
Mar 22, 2006
Mar 22, 2006
296
297
298
299
300
301
302
if (SDL_X11_HAVE_UTF8) {
status = Xutf8TextListToTextProperty(SDL_Display,
(char **)&icon, 1, XUTF8StringStyle, &iconprop);
if ( status == Success ) {
XSetTextProperty(SDL_Display, WMwindow, &iconprop, _NET_WM_ICON_NAME);
XFree(iconprop.value);
}
Sep 11, 2004
Sep 11, 2004
303
}
Mar 20, 2006
Mar 20, 2006
304
#endif
Apr 26, 2001
Apr 26, 2001
305
}
Mar 22, 2006
Mar 22, 2006
306
XSync(SDL_Display, False);
May 5, 2006
May 5, 2006
307
}
Apr 26, 2001
Apr 26, 2001
308
May 5, 2006
May 5, 2006
309
310
311
312
void X11_SetCaption(_THIS, const char *title, const char *icon)
{
SDL_Lock_EventThread();
X11_SetCaptionNoLock(this, title, icon);
Apr 26, 2001
Apr 26, 2001
313
314
315
316
317
318
319
320
321
SDL_Unlock_EventThread();
}
/* Iconify the window */
int X11_IconifyWindow(_THIS)
{
int result;
SDL_Lock_EventThread();
Mar 22, 2006
Mar 22, 2006
322
323
result = XIconifyWindow(SDL_Display, WMwindow, SDL_Screen);
XSync(SDL_Display, False);
Apr 26, 2001
Apr 26, 2001
324
325
326
327
328
329
SDL_Unlock_EventThread();
return(result);
}
SDL_GrabMode X11_GrabInputNoLock(_THIS, SDL_GrabMode mode)
{
Nov 7, 2001
Nov 7, 2001
330
int result;
Apr 26, 2001
Apr 26, 2001
331
332
333
334
335
336
337
338
if ( this->screen == NULL ) {
return(SDL_GRAB_OFF);
}
if ( ! SDL_Window ) {
return(mode); /* Will be set later on mode switch */
}
if ( mode == SDL_GRAB_OFF ) {
Mar 22, 2006
Mar 22, 2006
339
340
XUngrabPointer(SDL_Display, CurrentTime);
XUngrabKeyboard(SDL_Display, CurrentTime);
Apr 26, 2001
Apr 26, 2001
341
342
343
} else {
if ( this->screen->flags & SDL_FULLSCREEN ) {
/* Unbind the mouse from the fullscreen window */
Mar 22, 2006
Mar 22, 2006
344
XUngrabPointer(SDL_Display, CurrentTime);
Apr 26, 2001
Apr 26, 2001
345
346
}
/* Try to grab the mouse */
Jul 9, 2001
Jul 9, 2001
347
#if 0 /* We'll wait here until we actually grab, otherwise behavior undefined */
Apr 26, 2001
Apr 26, 2001
348
for ( numtries = 0; numtries < 10; ++numtries ) {
Jul 9, 2001
Jul 9, 2001
349
#else
May 5, 2006
May 5, 2006
350
for ( ; ; ) {
Jul 9, 2001
Jul 9, 2001
351
#endif
Mar 22, 2006
Mar 22, 2006
352
result = XGrabPointer(SDL_Display, SDL_Window, True, 0,
Apr 26, 2001
Apr 26, 2001
353
354
GrabModeAsync, GrabModeAsync,
SDL_Window, None, CurrentTime);
Jul 7, 2001
Jul 7, 2001
355
if ( result == GrabSuccess ) {
Apr 26, 2001
Apr 26, 2001
356
357
358
359
break;
}
SDL_Delay(100);
}
Jul 7, 2001
Jul 7, 2001
360
361
362
if ( result != GrabSuccess ) {
/* Uh, oh, what do we do here? */ ;
}
Jul 9, 2001
Jul 9, 2001
363
/* Now grab the keyboard */
Mar 22, 2006
Mar 22, 2006
364
XGrabKeyboard(SDL_Display, WMwindow, True,
Jul 7, 2001
Jul 7, 2001
365
GrabModeAsync, GrabModeAsync, CurrentTime);
Apr 26, 2001
Apr 26, 2001
366
367
368
/* Raise the window if we grab the mouse */
if ( !(this->screen->flags & SDL_FULLSCREEN) )
Mar 22, 2006
Mar 22, 2006
369
XRaiseWindow(SDL_Display, WMwindow);
Jul 9, 2001
Jul 9, 2001
370
371
372
/* Make sure we register input focus */
SDL_PrivateAppActive(1, SDL_APPINPUTFOCUS);
Nov 13, 2006
Nov 13, 2006
373
374
/* Since we grabbed the pointer, we have mouse focus, too. */
SDL_PrivateAppActive(1, SDL_APPMOUSEFOCUS);
Apr 26, 2001
Apr 26, 2001
375
}
Mar 22, 2006
Mar 22, 2006
376
XSync(SDL_Display, False);
Apr 26, 2001
Apr 26, 2001
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
return(mode);
}
SDL_GrabMode X11_GrabInput(_THIS, SDL_GrabMode mode)
{
SDL_Lock_EventThread();
mode = X11_GrabInputNoLock(this, mode);
SDL_Unlock_EventThread();
return(mode);
}
/* If 'info' is the right version, this function fills it and returns 1.
Otherwise, in case of a version mismatch, it returns -1.
*/
static void lock_display(void)
{
SDL_Lock_EventThread();
}
static void unlock_display(void)
{
/* Make sure any X11 transactions are completed */
SDL_VideoDevice *this = current_video;
Mar 22, 2006
Mar 22, 2006
401
XSync(SDL_Display, False);
Apr 26, 2001
Apr 26, 2001
402
403
SDL_Unlock_EventThread();
}
Sep 24, 2006
Sep 24, 2006
404
405
#include <stdio.h>
Apr 26, 2001
Apr 26, 2001
406
407
408
409
410
411
412
413
414
415
416
417
int X11_GetWMInfo(_THIS, SDL_SysWMinfo *info)
{
if ( info->version.major <= SDL_MAJOR_VERSION ) {
info->subsystem = SDL_SYSWM_X11;
info->info.x11.display = SDL_Display;
info->info.x11.window = SDL_Window;
if ( SDL_VERSIONNUM(info->version.major,
info->version.minor,
info->version.patch) >= 1002 ) {
info->info.x11.fswindow = FSwindow;
info->info.x11.wmwindow = WMwindow;
}
Sep 24, 2006
Sep 24, 2006
418
419
420
421
422
423
424
425
if ( SDL_VERSIONNUM(info->version.major,
info->version.minor,
info->version.patch) >= 1212 ) {
info->info.x11.gfxdisplay = GFX_Display;
}
Apr 26, 2001
Apr 26, 2001
426
427
428
429
430
431
432
433
434
info->info.x11.lock_func = lock_display;
info->info.x11.unlock_func = unlock_display;
return(1);
} else {
SDL_SetError("Application not compiled with SDL %d.%d\n",
SDL_MAJOR_VERSION, SDL_MINOR_VERSION);
return(-1);
}
}