/
SDL_x11wm.c
394 lines (359 loc) · 10.6 KB
1
2
/*
SDL - Simple DirectMedia Layer
3
Copyright (C) 1997-2004 Sam Lantinga
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
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
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Sam Lantinga
20
slouken@libsdl.org
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
*/
#ifdef SAVE_RCSID
static char rcsid =
"@(#) $Id$";
#endif
#include <stdlib.h>
#include <string.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include "SDL_version.h"
#include "SDL_error.h"
#include "SDL_timer.h"
#include "SDL_video.h"
#include "SDL_syswm.h"
#include "SDL_events_c.h"
#include "SDL_pixels_c.h"
#include "SDL_x11modes_c.h"
#include "SDL_x11wm_c.h"
43
44
45
46
47
48
49
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;
}
50
51
52
53
54
55
56
57
void X11_SetIcon(_THIS, SDL_Surface *icon, Uint8 *mask)
{
SDL_Surface *sicon;
XWMHints *wmhints;
XImage *icon_image;
Pixmap icon_pixmap;
Pixmap mask_pixmap;
58
59
Window icon_window = None;
GC gc;
60
XGCValues GCvalues;
61
int i, dbpp;
62
SDL_Rect bounds;
63
Uint8 *LSBmask;
64
Visual *dvis;
65
66
char *p;
int masksize;
67
68
69
70
71
SDL_Lock_EventThread();
/* The icon must use the default visual, depth and colormap of the
screen, so it might need a conversion */
72
dvis = DefaultVisual(SDL_Display, SDL_Screen);
73
dbpp = DefaultDepth(SDL_Display, SDL_Screen);
74
75
76
77
78
for(i = 0; i < this->hidden->nvisuals; i++) {
if(this->hidden->visuals[i].visual == dvis) {
dbpp = this->hidden->visuals[i].bpp;
break;
}
79
80
81
82
83
84
85
}
/* 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);
86
if ( sicon == NULL )
87
88
goto done;
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
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;
memcpy(want, icon->format->palette->colors,
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;
115
}
116
117
118
119
120
121
122
123
124
125
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]--;
}
}
126
pXFreeColors(GFX_Display, dcmap, freelist, nfree, 0);
127
}
128
129
130
if(!SDL_iconcolors)
SDL_iconcolors = malloc(256 * sizeof *SDL_iconcolors);
memset(SDL_iconcolors, 0, 256 * sizeof *SDL_iconcolors);
131
132
133
134
135
136
137
138
139
140
/* try to allocate the colours */
memset(got, 0, sizeof got);
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;
141
if(pXAllocColor(GFX_Display, dcmap, &c)) {
142
143
144
145
146
147
148
149
150
151
152
153
154
/* 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;
155
pXQueryColors(GFX_Display, dcmap, cols, 256);
156
157
158
159
160
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]) {
161
if(pXAllocColor(GFX_Display, dcmap,
162
163
164
165
166
167
168
169
cols + i)) {
SDL_iconcolors[i] = 1;
} else {
/* index not available */
got[i].r = 0;
got[i].g = 0;
got[i].b = 0;
}
170
171
172
}
}
}
173
174
SDL_SetColors(sicon, got, 0, 256);
175
176
}
177
178
179
180
181
182
183
184
185
186
187
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;
LSBmask = malloc(masksize);
188
189
190
if ( LSBmask == NULL ) {
goto done;
}
191
192
193
memset(LSBmask, 0, masksize);
for(i = 0; i < masksize; i++)
LSBmask[i] = reverse_byte(mask[i]);
194
mask_pixmap = pXCreatePixmapFromBitmapData(SDL_Display, WMwindow,
195
196
197
(char *)LSBmask,
sicon->w, sicon->h,
1L, 0L, 1);
198
199
/* Transfer the image to an X11 pixmap */
200
icon_image = pXCreateImage(SDL_Display,
201
202
203
204
205
DefaultVisual(SDL_Display, SDL_Screen),
DefaultDepth(SDL_Display, SDL_Screen),
ZPixmap, 0, sicon->pixels,
sicon->w, sicon->h,
32, 0);
206
207
icon_image->byte_order = (SDL_BYTEORDER == SDL_BIG_ENDIAN)
? MSBFirst : LSBFirst;
208
icon_pixmap = pXCreatePixmap(SDL_Display, SDL_Root, sicon->w, sicon->h,
209
DefaultDepth(SDL_Display, SDL_Screen));
210
211
gc = pXCreateGC(SDL_Display, icon_pixmap, 0, &GCvalues);
pXPutImage(SDL_Display, icon_pixmap, gc, icon_image,
212
0, 0, 0, 0, sicon->w, sicon->h);
213
214
pXFreeGC(SDL_Display, gc);
pXDestroyImage(icon_image);
215
216
217
free(LSBmask);
sicon->pixels = NULL;
218
219
220
221
222
/* 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. */
p = getenv("SDL_VIDEO_X11_ICONWIN");
if(p && *p) {
223
icon_window = pXCreateSimpleWindow(SDL_Display, SDL_Root,
224
225
226
0, 0, sicon->w, sicon->h, 0,
CopyFromParent,
CopyFromParent);
227
pXSetWindowBackgroundPixmap(SDL_Display, icon_window,
228
icon_pixmap);
229
pXClearWindow(SDL_Display, icon_window);
230
}
231
232
/* Set the window icon to the icon pixmap (and icon window) */
233
wmhints = pXAllocWMHints();
234
235
236
wmhints->flags = (IconPixmapHint | IconMaskHint);
wmhints->icon_pixmap = icon_pixmap;
wmhints->icon_mask = mask_pixmap;
237
238
239
240
if(icon_window != None) {
wmhints->flags |= IconWindowHint;
wmhints->icon_window = icon_window;
}
241
242
243
pXSetWMHints(SDL_Display, WMwindow, wmhints);
pXFree(wmhints);
pXSync(SDL_Display, False);
244
245
246
done:
SDL_Unlock_EventThread();
247
SDL_FreeSurface(sicon);
248
249
250
251
252
253
254
255
256
257
}
void X11_SetCaption(_THIS, const char *title, const char *icon)
{
XTextProperty titleprop, iconprop;
/* Lock the event thread, in multi-threading environments */
SDL_Lock_EventThread();
if ( title != NULL ) {
258
int error = XLocaleNotSupported;
259
#ifdef X_HAVE_UTF8_STRING
260
error = pXutf8TextListToTextProperty(SDL_Display,
261
262
(char **)&title, 1, XUTF8StringStyle,
&titleprop);
263
#endif
264
if ( error != Success ) {
265
pXStringListToTextProperty((char **)&title, 1,
266
267
&titleprop);
}
268
269
pXSetWMName(SDL_Display, WMwindow, &titleprop);
pXFree(titleprop.value);
270
271
}
if ( icon != NULL ) {
272
int error = XLocaleNotSupported;
273
#ifdef X_HAVE_UTF8_STRING
274
error = pXutf8TextListToTextProperty(SDL_Display,
275
(char **)&icon, 1, XUTF8StringStyle, &iconprop);
276
#endif
277
if ( error != Success ) {
278
pXStringListToTextProperty((char **)&icon, 1, &iconprop);
279
}
280
281
pXSetWMIconName(SDL_Display, WMwindow, &iconprop);
pXFree(iconprop.value);
282
}
283
pXSync(SDL_Display, False);
284
285
286
287
288
289
290
291
292
293
SDL_Unlock_EventThread();
}
/* Iconify the window */
int X11_IconifyWindow(_THIS)
{
int result;
SDL_Lock_EventThread();
294
295
result = pXIconifyWindow(SDL_Display, WMwindow, SDL_Screen);
pXSync(SDL_Display, False);
296
297
298
299
300
301
SDL_Unlock_EventThread();
return(result);
}
SDL_GrabMode X11_GrabInputNoLock(_THIS, SDL_GrabMode mode)
{
302
int result;
303
304
305
306
307
308
309
310
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 ) {
311
312
pXUngrabPointer(SDL_Display, CurrentTime);
pXUngrabKeyboard(SDL_Display, CurrentTime);
313
314
315
} else {
if ( this->screen->flags & SDL_FULLSCREEN ) {
/* Unbind the mouse from the fullscreen window */
316
pXUngrabPointer(SDL_Display, CurrentTime);
317
318
}
/* Try to grab the mouse */
319
#if 0 /* We'll wait here until we actually grab, otherwise behavior undefined */
320
for ( numtries = 0; numtries < 10; ++numtries ) {
321
322
323
#else
while ( 1 ) {
#endif
324
result = pXGrabPointer(SDL_Display, SDL_Window, True, 0,
325
326
GrabModeAsync, GrabModeAsync,
SDL_Window, None, CurrentTime);
327
if ( result == GrabSuccess ) {
328
329
330
331
break;
}
SDL_Delay(100);
}
332
333
334
if ( result != GrabSuccess ) {
/* Uh, oh, what do we do here? */ ;
}
335
/* Now grab the keyboard */
336
pXGrabKeyboard(SDL_Display, WMwindow, True,
337
GrabModeAsync, GrabModeAsync, CurrentTime);
338
339
340
/* Raise the window if we grab the mouse */
if ( !(this->screen->flags & SDL_FULLSCREEN) )
341
pXRaiseWindow(SDL_Display, WMwindow);
342
343
344
/* Make sure we register input focus */
SDL_PrivateAppActive(1, SDL_APPINPUTFOCUS);
345
}
346
pXSync(SDL_Display, False);
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
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;
371
pXSync(SDL_Display, False);
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
SDL_Unlock_EventThread();
}
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;
}
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);
}
}