Debian Codegraph @

src/x/xwindow.c

raw
1 #include <X11/Xlib.h>
2 #include <X11/Xutil.h>
3 #include <X11/Xatom.h>
4
5 #include "allegro5/allegro.h"
6 #include "allegro5/allegro_x.h"
7 #include "allegro5/internal/aintern_x.h"
8 #include "allegro5/internal/aintern_xdisplay.h"
9 #include "allegro5/internal/aintern_xsystem.h"
10 #include "allegro5/internal/aintern_xwindow.h"
11
12 #ifdef ALLEGRO_RASPBERRYPI
13 #include "allegro5/internal/aintern_raspberrypi.h"
14 #define ALLEGRO_SYSTEM_XGLX ALLEGRO_SYSTEM_RASPBERRYPI
15 #define ALLEGRO_DISPLAY_XGLX ALLEGRO_DISPLAY_RASPBERRYPI
16 #endif
17
18 ALLEGRO_DEBUG_CHANNEL("xwindow")
19
20 #define X11_ATOM(x) XInternAtom(x11, #x, False);
21
22 void _al_xwin_set_size_hints(ALLEGRO_DISPLAY *d, int x_off, int y_off)
23 {
24 ALLEGRO_SYSTEM_XGLX *system = (void *)al_get_system_driver();
25 ALLEGRO_DISPLAY_XGLX *glx = (void *)d;
26 XSizeHints *sizehints;
27 XWMHints *wmhints;
28 XClassHint *classhints;
29 int w, h;
30
31 sizehints = XAllocSizeHints();
32 sizehints->flags = 0;
33
34 #ifdef ALLEGRO_RASPBERRYPI
35 int x, y;
36 _al_raspberrypi_get_screen_info(&x, &y, &w, &h);
37 #else
38 w = d->w;
39 h = d->h;
40 #endif
41
42 /* Do not force the size of the window on resizeable or fullscreen windows */
43 /* on fullscreen windows, it confuses most X Window Managers */
44 if (!(d->flags & ALLEGRO_RESIZABLE) && !(d->flags & ALLEGRO_FULLSCREEN)) {
45 sizehints->flags |= PMinSize | PMaxSize | PBaseSize;
46 sizehints->min_width = sizehints->max_width = sizehints->base_width = w;
47 sizehints->min_height = sizehints->max_height = sizehints->base_height = h;
48 }
49
50 /* Constrain the window if needed. */
51 if (d->use_constraints && (d->flags & ALLEGRO_RESIZABLE) &&
52 (d->min_w > 0 || d->min_h > 0 || d->max_w > 0 || d->max_h > 0))
53 {
54 sizehints->flags |= PMinSize | PMaxSize | PBaseSize;
55 sizehints->min_width = (d->min_w > 0) ? d->min_w : 0;
56 sizehints->min_height = (d->min_h > 0) ? d->min_h : 0;
57 sizehints->max_width = (d->max_w > 0) ? d->max_w : INT_MAX;
58 sizehints->max_height = (d->max_h > 0) ? d->max_h : INT_MAX;
59 sizehints->base_width = w;
60 sizehints->base_height = h;
61 }
62
63 // Tell WMs to respect our chosen position, otherwise the x_off/y_off
64 // positions passed to XCreateWindow will be ignored by most WMs.
65 if (x_off != INT_MAX && y_off != INT_MAX) {
66 ALLEGRO_DEBUG("Force window position to %d, %d.\n", x_off, y_off);
67 sizehints->flags |= PPosition;
68 sizehints->x = x_off;
69 sizehints->y = y_off;
70 }
71
72 if (d->flags & ALLEGRO_FULLSCREEN) {
73 /* kwin will improperly layer a panel over our window on a second display without this.
74 * some other Size flags may cause glitches with various WMs, but this seems to be ok
75 * with metacity and kwin. As noted in xdpy_create_display, compiz is just broken.
76 */
77 sizehints->flags |= PBaseSize;
78 sizehints->base_width = w;
79 sizehints->base_height = h;
80 }
81
82 /* Setup the input hints so we get keyboard input */
83 wmhints = XAllocWMHints();
84 wmhints->input = True;
85 wmhints->flags = InputHint;
86
87 ALLEGRO_PATH *exepath = al_get_standard_path(ALLEGRO_EXENAME_PATH);
88
89 /* Setup the class hints so we can get an icon (AfterStep)
90 * We must use the executable filename here.
91 */
92 classhints = XAllocClassHint();
93 classhints->res_name = strdup(al_get_path_basename(exepath));
94 classhints->res_class = strdup(al_get_path_basename(exepath));
95
96 /* Set the size, input and class hints, and define WM_CLIENT_MACHINE and WM_LOCALE_NAME */
97 XSetWMProperties(system->x11display, glx->window, NULL, NULL, NULL, 0,
98 sizehints, wmhints, classhints);
99
100 free(classhints->res_name);
101 free(classhints->res_class);
102 XFree(sizehints);
103 XFree(wmhints);
104 XFree(classhints);
105
106 al_destroy_path(exepath);
107 }
108
109
110 void _al_xwin_reset_size_hints(ALLEGRO_DISPLAY *d)
111 {
112 ALLEGRO_SYSTEM_XGLX *system = (void *)al_get_system_driver();
113 ALLEGRO_DISPLAY_XGLX *glx = (void *)d;
114 XSizeHints *hints;
115
116 hints = XAllocSizeHints();
117 hints->flags = PMinSize | PMaxSize;
118 hints->min_width = 0;
119 hints->min_height = 0;
120 // FIXME: Is there a way to remove/reset max dimensions?
121 hints->max_width = 32768;
122 hints->max_height = 32768;
123 XSetWMNormalHints(system->x11display, glx->window, hints);
124
125 XFree(hints);
126 }
127
128
129 /* Note: The system mutex must be locked (exactly once) before
130 * calling this as we call _al_display_xglx_await_resize.
131 */
132 void _al_xwin_set_fullscreen_window(ALLEGRO_DISPLAY *display, int value)
133 {
134 ALLEGRO_SYSTEM_XGLX *system = (void *)al_get_system_driver();
135 ALLEGRO_DISPLAY_XGLX *glx = (ALLEGRO_DISPLAY_XGLX *)display;
136 Display *x11 = system->x11display;
137 #ifndef ALLEGRO_RASPBERRYPI
138 int old_resize_count = glx->resize_count;
139 #endif
140
141 ALLEGRO_DEBUG("Toggling _NET_WM_STATE_FULLSCREEN hint: %d\n", value);
142
143 XEvent xev;
144 xev.xclient.type = ClientMessage;
145 xev.xclient.serial = 0;
146 xev.xclient.send_event = True;
147 xev.xclient.message_type = X11_ATOM(_NET_WM_STATE);
148 xev.xclient.window = glx->window;
149 xev.xclient.format = 32;
150
151 // Note: It seems 0 is not reliable except when mapping a window -
152 // 2 is all we need though.
153 xev.xclient.data.l[0] = value; /* 0 = off, 1 = on, 2 = toggle */
154
155 xev.xclient.data.l[1] = X11_ATOM(_NET_WM_STATE_FULLSCREEN);
156 xev.xclient.data.l[2] = 0;
157 xev.xclient.data.l[3] = 1; /* 1 = normal application source */
158 xev.xclient.data.l[4] = 0;
159
160 XSendEvent(
161 x11,
162 #if !defined ALLEGRO_RASPBERRYPI
163 RootWindowOfScreen(ScreenOfDisplay(x11, glx->xscreen)),
164 #else
165 RootWindowOfScreen(ScreenOfDisplay(x11, DefaultScreen(x11))),
166 #endif
167 False,
168 SubstructureRedirectMask | SubstructureNotifyMask, &xev);
169
170 #if !defined ALLEGRO_RASPBERRYPI
171 if (value == 2) {
172 /* Only wait for a resize if toggling. */
173 _al_display_xglx_await_resize(display, old_resize_count, true);
174 }
175 #endif
176 }
177
178
179 void _al_xwin_set_above(ALLEGRO_DISPLAY *display, int value)
180 {
181 ALLEGRO_SYSTEM_XGLX *system = (void *)al_get_system_driver();
182 ALLEGRO_DISPLAY_XGLX *glx = (ALLEGRO_DISPLAY_XGLX *)display;
183 Display *x11 = system->x11display;
184
185 ALLEGRO_DEBUG("Toggling _NET_WM_STATE_ABOVE hint: %d\n", value);
186
187 XEvent xev;
188 xev.xclient.type = ClientMessage;
189 xev.xclient.serial = 0;
190 xev.xclient.send_event = True;
191 xev.xclient.message_type = X11_ATOM(_NET_WM_STATE);
192 xev.xclient.window = glx->window;
193 xev.xclient.format = 32;
194
195 // Note: It seems 0 is not reliable except when mapping a window -
196 // 2 is all we need though.
197 xev.xclient.data.l[0] = value; /* 0 = off, 1 = on, 2 = toggle */
198
199 xev.xclient.data.l[1] = X11_ATOM(_NET_WM_STATE_ABOVE);
200 xev.xclient.data.l[2] = 0;
201 xev.xclient.data.l[3] = 0;
202 xev.xclient.data.l[4] = 1;
203
204 XSendEvent(x11, DefaultRootWindow(x11), False,
205 SubstructureRedirectMask | SubstructureNotifyMask, &xev);
206 }
207
208
209 void _al_xwin_set_frame(ALLEGRO_DISPLAY *display, bool frame_on)
210 {
211 ALLEGRO_SYSTEM_XGLX *system = (void *)al_get_system_driver();
212 ALLEGRO_DISPLAY_XGLX *glx = (ALLEGRO_DISPLAY_XGLX *)display;
213 Display *x11 = system->x11display;
214 Atom hints;
215
216 #if 1
217 /* This code is taken from the GDK sources. So it works perfectly in Gnome,
218 * no idea if it will work anywhere else. X11 documentation itself only
219 * describes a way how to make the window completely unmanaged, but that
220 * would also require special care in the event handler.
221 */
222 hints = XInternAtom(x11, "_MOTIF_WM_HINTS", True);
223 if (hints) {
224 struct {
225 unsigned long flags;
226 unsigned long functions;
227 unsigned long decorations;
228 long input_mode;
229 unsigned long status;
230 } motif = {2, 0, frame_on, 0, 0};
231 XChangeProperty(x11, glx->window, hints, hints, 32, PropModeReplace,
232 (void *)&motif, sizeof motif / 4);
233
234 if (frame_on)
235 display->flags &= ~ALLEGRO_FRAMELESS;
236 else
237 display->flags |= ALLEGRO_FRAMELESS;
238 }
239 #endif
240 }
241
242
243 /* Helper to set a window icon. We use the _NET_WM_ICON property which is
244 * supported by modern window managers.
245 *
246 * The old method is XSetWMHints but the (antiquated) ICCCM talks about 1-bit
247 * pixmaps. For colour icons, perhaps you're supposed use the icon_window,
248 * and draw the window yourself?
249 */
250 static bool xdpy_set_icon_inner(Display *x11display, Window window,
251 ALLEGRO_BITMAP *bitmap, int prop_mode)
252 {
253 int w, h;
254 int data_size;
255 unsigned long *data; /* Yes, unsigned long, even on 64-bit platforms! */
256 ALLEGRO_LOCKED_REGION *lr;
257 bool ret;
258
259 w = al_get_bitmap_width(bitmap);
260 h = al_get_bitmap_height(bitmap);
261 data_size = 2 + w * h;
262 data = al_malloc(data_size * sizeof(data[0]));
263 if (!data)
264 return false;
265
266 lr = al_lock_bitmap(bitmap, ALLEGRO_PIXEL_FORMAT_ANY_WITH_ALPHA,
267 ALLEGRO_LOCK_READONLY);
268 if (lr) {
269 int x, y;
270 ALLEGRO_COLOR c;
271 unsigned char r, g, b, a;
272 Atom _NET_WM_ICON;
273
274 data[0] = w;
275 data[1] = h;
276 for (y = 0; y < h; y++) {
277 for (x = 0; x < w; x++) {
278 c = al_get_pixel(bitmap, x, y);
279 al_unmap_rgba(c, &r, &g, &b, &a);
280 data[2 + y*w + x] = ((unsigned long)a << 24) | ((unsigned long)r << 16) |
281 ((unsigned long)g << 8) | (unsigned long)b;
282 }
283 }
284
285 _NET_WM_ICON = XInternAtom(x11display, "_NET_WM_ICON", False);
286 XChangeProperty(x11display, window, _NET_WM_ICON, XA_CARDINAL, 32,
287 prop_mode, (unsigned char *)data, data_size);
288
289 al_unlock_bitmap(bitmap);
290 ret = true;
291 }
292 else {
293 ret = false;
294 }
295
296 al_free(data);
297
298 return ret;
299 }
300
301
302 void _al_xwin_set_icons(ALLEGRO_DISPLAY *d,
303 int num_icons, ALLEGRO_BITMAP *bitmaps[])
304 {
305 ALLEGRO_SYSTEM_XGLX *system = (ALLEGRO_SYSTEM_XGLX *)al_get_system_driver();
306 ALLEGRO_DISPLAY_XGLX *glx = (ALLEGRO_DISPLAY_XGLX *)d;
307 int prop_mode = PropModeReplace;
308 int i;
309
310 _al_mutex_lock(&system->lock);
311
312 for (i = 0; i < num_icons; i++) {
313 if (xdpy_set_icon_inner(system->x11display, glx->window, bitmaps[i],
314 prop_mode)) {
315 prop_mode = PropModeAppend;
316 }
317 }
318
319 _al_mutex_unlock(&system->lock);
320 }
321
322
323 /* Note: The system mutex must be locked (exactly once) before
324 * calling this as we call _al_display_xglx_await_resize.
325 */
326 void _al_xwin_maximize(ALLEGRO_DISPLAY *display, bool maximized)
327 {
328 #ifndef ALLEGRO_RASPBERRYPI
329 if (!!(display->flags & ALLEGRO_MAXIMIZED) == maximized)
330 return;
331 ALLEGRO_SYSTEM_XGLX *system = (void *)al_get_system_driver();
332 ALLEGRO_DISPLAY_XGLX *glx = (ALLEGRO_DISPLAY_XGLX *)display;
333 Display *x11 = system->x11display;
334 int old_resize_count = glx->resize_count;
335
336 XEvent xev;
337 xev.xclient.type = ClientMessage;
338 xev.xclient.serial = 0;
339 xev.xclient.send_event = True;
340 xev.xclient.message_type = X11_ATOM(_NET_WM_STATE);
341 xev.xclient.window = glx->window;
342 xev.xclient.format = 32;
343
344 xev.xclient.data.l[0] = maximized ? 1 : 0;
345 xev.xclient.data.l[1] = X11_ATOM(_NET_WM_STATE_MAXIMIZED_HORZ);
346 xev.xclient.data.l[2] = X11_ATOM(_NET_WM_STATE_MAXIMIZED_VERT);
347 xev.xclient.data.l[3] = 0;
348
349 XSendEvent(
350 x11,
351 RootWindowOfScreen(ScreenOfDisplay(x11, glx->xscreen)),
352 False,
353 SubstructureRedirectMask | SubstructureNotifyMask, &xev);
354
355 _al_display_xglx_await_resize(display, old_resize_count, true);
356 #endif
357 }
358
359
360 void _al_xwin_check_maximized(ALLEGRO_DISPLAY *display)
361 {
362 #ifndef ALLEGRO_RASPBERRYPI
363 ALLEGRO_SYSTEM_XGLX *system = (void *)al_get_system_driver();
364 ALLEGRO_DISPLAY_XGLX *glx = (ALLEGRO_DISPLAY_XGLX *)display;
365 Display *x11 = system->x11display;
366 Atom type;
367 Atom horz = X11_ATOM(_NET_WM_STATE_MAXIMIZED_HORZ);
368 Atom vert = X11_ATOM(_NET_WM_STATE_MAXIMIZED_VERT);
369 Atom property = X11_ATOM(_NET_WM_STATE);
370 int format;
371 int maximized = 0;
372 unsigned long n, remaining, i, *p32;
373 unsigned char *p8 = NULL;
374 if (XGetWindowProperty(x11, glx->window, property, 0, INT_MAX,
375 False, AnyPropertyType, &type, &format, &n, &remaining, &p8)
376 != Success) {
377 return;
378 }
379 p32 = (unsigned long *)p8;
380 for (i = 0; i < n; i++) {
381 if (p32[i] == horz)
382 maximized |= 1;
383 if (p32[i] == vert)
384 maximized |= 2;
385 }
386 XFree(p8);
387 display->flags &= ~ALLEGRO_MAXIMIZED;
388 if (maximized == 3)
389 display->flags |= ALLEGRO_MAXIMIZED;
390 #endif
391 }
392
393
394 /* Function: al_get_x_window_id
395 */
396 XID al_get_x_window_id(ALLEGRO_DISPLAY *display)
397 {
398 ASSERT(display != NULL);
399 return ((ALLEGRO_DISPLAY_XGLX*)display)->window;
400 }
401
402
403 // Note: this only seems to work after the window has been mapped
404 void _al_xwin_get_borders(ALLEGRO_DISPLAY *display) {
405 ALLEGRO_DISPLAY_XGLX *glx = (ALLEGRO_DISPLAY_XGLX *)display;
406 ALLEGRO_SYSTEM_XGLX *system = (void *)al_get_system_driver();
407 Display *x11 = system->x11display;
408
409 Atom type;
410 int format;
411 unsigned long nitems, bytes_after;
412 unsigned char *property;
413 Atom frame_extents = X11_ATOM(_NET_FRAME_EXTENTS);
414 if (XGetWindowProperty(x11, glx->window,
415 frame_extents, 0, 16, 0, XA_CARDINAL,
416 &type, &format, &nitems, &bytes_after, &property) == Success) {
417 if (type != None && nitems == 4) {
418 glx->border_left = (int)((long *)property)[0];
419 glx->border_right = (int)((long *)property)[1];
420 glx->border_top = (int)((long *)property)[2];
421 glx->border_bottom = (int)((long *)property)[3];
422 glx->borders_known = true;
423 ALLEGRO_DEBUG("_NET_FRAME_EXTENTS: %d %d %d %d\n", glx->border_left, glx->border_top,
424 glx->border_right, glx->border_bottom);
425 }
426 else {
427 ALLEGRO_DEBUG("Unexpected _NET_FRAME_EXTENTS format: nitems=%lu\n", nitems);
428 }
429 XFree(property);
430 }
431 else {
432 ALLEGRO_DEBUG("Could not read _NET_FRAME_EXTENTS\n");
433 }
434 }
435
436 /* vim: set sts=3 sw=3 et: */