Debian Codegraph @

src/x/xdisplay.c

raw
1 #include "allegro5/allegro.h"
2 #include "allegro5/allegro_opengl.h"
3 #include "allegro5/internal/aintern_bitmap.h"
4 #include "allegro5/internal/aintern_opengl.h"
5 #include "allegro5/internal/aintern_x.h"
6 #include "allegro5/internal/aintern_xcursor.h"
7 #include "allegro5/internal/aintern_xclipboard.h"
8 #include "allegro5/internal/aintern_xdisplay.h"
9 #include "allegro5/internal/aintern_xfullscreen.h"
10 #include "allegro5/internal/aintern_xglx_config.h"
11 #include "allegro5/internal/aintern_xsystem.h"
12 #include "allegro5/internal/aintern_xtouch.h"
13 #include "allegro5/internal/aintern_xwindow.h"
14 #include "allegro5/internal/aintern_xdnd.h"
15 #include "allegro5/platform/aintxglx.h"
16
17 #include <X11/Xatom.h>
18 #ifdef ALLEGRO_XWINDOWS_WITH_XINPUT2
19 #include <X11/extensions/XInput2.h>
20 #endif
21
22 #include "xicon.h"
23 #include "icon.inc"
24
25 ALLEGRO_DEBUG_CHANNEL("display")
26
27 static ALLEGRO_DISPLAY_INTERFACE xdpy_vt;
28 static const ALLEGRO_XWIN_DISPLAY_OVERRIDABLE_INTERFACE default_overridable_vt;
29 static const ALLEGRO_XWIN_DISPLAY_OVERRIDABLE_INTERFACE *gtk_override_vt = NULL;
30
31 static void xdpy_destroy_display(ALLEGRO_DISPLAY *d);
32 static bool xdpy_acknowledge_resize(ALLEGRO_DISPLAY *d);
33
34
35 /* XXX where does this belong? */
36 static void _al_xglx_use_adapter(ALLEGRO_SYSTEM_XGLX *s, int adapter)
37 {
38 ALLEGRO_DEBUG("use adapter %i\n", adapter);
39 s->adapter_use_count++;
40 s->adapter_map[adapter]++;
41 }
42
43
44 static void _al_xglx_unuse_adapter(ALLEGRO_SYSTEM_XGLX *s, int adapter)
45 {
46 ALLEGRO_DEBUG("unuse adapter %i\n", adapter);
47 s->adapter_use_count--;
48 s->adapter_map[adapter]--;
49 }
50
51
52 static bool check_adapter_use_count(ALLEGRO_SYSTEM_XGLX *system)
53 {
54 /* If we're in multi-head X mode, bail out if we try to use more than one
55 * display as there are bugs in X/glX that cause us to hang in X if we try
56 * to use more than one.
57 * If we're in real xinerama mode, also bail out, X makes mouse use evil.
58 */
59 bool true_xinerama_active = false;
60 #ifdef ALLEGRO_XWINDOWS_WITH_XINERAMA
61 bool xrandr_active = false;
62 #ifdef ALLEGRO_XWINDOWS_WITH_XRANDR
63 xrandr_active = system->xrandr_available;
64 #endif
65 true_xinerama_active = !xrandr_active && system->xinerama_available;
66 #endif
67
68 if ((true_xinerama_active || ScreenCount(system->x11display) > 1)
69 && system->adapter_use_count > 0)
70 {
71 int i, adapter_use_count = 0;
72
73 /* XXX magic constant */
74 for (i = 0; i < 32; i++) {
75 if (system->adapter_map[i])
76 adapter_use_count++;
77 }
78
79 if (adapter_use_count > 1) {
80 ALLEGRO_ERROR("Use of more than one adapter at once in "
81 "multi-head X or X with true Xinerama active is not possible.\n");
82 return false;
83 }
84 }
85
86 return true;
87 }
88
89
90 static int query_glx_version(ALLEGRO_SYSTEM_XGLX *system)
91 {
92 int major, minor;
93 int version;
94
95 glXQueryVersion(system->x11display, &major, &minor);
96 version = major * 100 + minor * 10;
97 ALLEGRO_INFO("GLX %.1f.\n", version / 100.f);
98 return version;
99 }
100
101
102 static int xdpy_swap_control(ALLEGRO_DISPLAY *display, int vsync_setting)
103 {
104 /* We set the swap interval to 0 if vsync is forced off, and to 1
105 * if it is forced on.
106 * http://www.opengl.org/registry/specs/SGI/swap_control.txt
107 * If the option is set to 0, we simply use the system default. The
108 * above extension specifies vsync on as default though, so in the
109 * end with GLX we can't force vsync on, just off.
110 */
111 ALLEGRO_DEBUG("requested vsync=%d.\n", vsync_setting);
112
113 if (vsync_setting) {
114 if (display->ogl_extras->extension_list->ALLEGRO_GLX_SGI_swap_control) {
115 int x = (vsync_setting == 2) ? 0 : 1;
116 if (glXSwapIntervalSGI(x)) {
117 ALLEGRO_WARN("glXSwapIntervalSGI(%d) failed.\n", x);
118 }
119 }
120 else {
121 ALLEGRO_WARN("no vsync, GLX_SGI_swap_control missing.\n");
122 /* According to the specification that means it's on, but
123 * the driver might have disabled it. So we do not know.
124 */
125 vsync_setting = 0;
126 }
127 }
128
129 return vsync_setting;
130 }
131
132 static bool should_bypass_compositor(int flags)
133 {
134 const char* value = al_get_config_value(al_get_system_config(), "x11", "bypass_compositor");
135 if (value && strcmp(value, "always") == 0) {
136 return true;
137 }
138 if (value && strcmp(value, "never") == 0) {
139 return false;
140 }
141 // default to "fullscreen_only"
142 return (flags & ALLEGRO_FULLSCREEN) || (flags & ALLEGRO_FULLSCREEN_WINDOW);
143 }
144
145 static void set_compositor_bypass_flag(ALLEGRO_DISPLAY *display)
146 {
147 ALLEGRO_SYSTEM_XGLX *system = (ALLEGRO_SYSTEM_XGLX *)al_get_system_driver();
148 ALLEGRO_DISPLAY_XGLX *glx = (ALLEGRO_DISPLAY_XGLX *)display;
149 const long _NET_WM_BYPASS_COMPOSITOR_HINT_ON = should_bypass_compositor(display->flags);
150 Atom _NET_WM_BYPASS_COMPOSITOR;
151
152 _NET_WM_BYPASS_COMPOSITOR = XInternAtom(system->x11display,
153 "_NET_WM_BYPASS_COMPOSITOR",
154 False);
155 XChangeProperty(system->x11display, glx->window, _NET_WM_BYPASS_COMPOSITOR,
156 XA_CARDINAL, 32, PropModeReplace,
157 (unsigned char *)&_NET_WM_BYPASS_COMPOSITOR_HINT_ON, 1);
158 }
159
160
161 static bool xdpy_create_display_window(ALLEGRO_SYSTEM_XGLX *system,
162 ALLEGRO_DISPLAY_XGLX *d, int w, int h, int adapter)
163 {
164 ALLEGRO_DISPLAY *display = (ALLEGRO_DISPLAY *)d;
165
166 /* Create a colormap. */
167 Colormap cmap = XCreateColormap(system->x11display,
168 RootWindow(system->x11display, d->xvinfo->screen),
169 d->xvinfo->visual, AllocNone);
170
171 /* Create an X11 window */
172 XSetWindowAttributes swa;
173 int mask = CWBorderPixel | CWColormap | CWEventMask;
174 swa.colormap = cmap;
175 swa.border_pixel = 0;
176 swa.event_mask =
177 KeyPressMask |
178 KeyReleaseMask |
179 StructureNotifyMask |
180 EnterWindowMask |
181 LeaveWindowMask |
182 ExposureMask |
183 PropertyChangeMask |
184 ButtonPressMask |
185 ButtonReleaseMask |
186 PointerMotionMask;
187
188 /* We handle these events via GTK-sent XEmbed events. */
189 if (!(display->flags & ALLEGRO_GTK_TOPLEVEL_INTERNAL)) {
190 swa.event_mask |= FocusChangeMask;
191 }
192
193 /* For a non-compositing window manager, a black background can look
194 * less broken if the application doesn't react to expose events fast
195 * enough. However in some cases like resizing, the black background
196 * causes horrible flicker.
197 */
198 if (!(display->flags & ALLEGRO_RESIZABLE)) {
199 mask |= CWBackPixel;
200 swa.background_pixel = BlackPixel(system->x11display, d->xvinfo->screen);
201 }
202
203 int x_off = INT_MAX;
204 int y_off = INT_MAX;
205 if (display->flags & ALLEGRO_FULLSCREEN) {
206 _al_xglx_get_display_offset(system, d->adapter, &x_off, &y_off);
207 }
208 else {
209 /* We want new_display_adapter's offset to add to the
210 * new_window_position.
211 */
212 int xscr_x = 0;
213 int xscr_y = 0;
214 al_get_new_window_position(&x_off, &y_off);
215 if (x_off != INT_MAX && y_off != INT_MAX) {
216 d->need_initial_position_adjust = true;
217 ALLEGRO_DEBUG("Will adjust window position later to %d/%d\n", x_off, y_off);
218 }
219
220 if (adapter >= 0) {
221 /* Non default adapter. I'm assuming this means the user wants the
222 * window to be placed on the adapter offset by new display pos.
223 */
224 _al_xglx_get_display_offset(system, d->adapter, &xscr_x, &xscr_y);
225 if (x_off != INT_MAX)
226 x_off += xscr_x;
227 if (y_off != INT_MAX)
228 y_off += xscr_y;
229 }
230 }
231
232 d->window = XCreateWindow(system->x11display,
233 RootWindow(system->x11display, d->xvinfo->screen),
234 x_off != INT_MAX ? x_off : 0,
235 y_off != INT_MAX ? y_off : 0,
236 w, h, 0, d->xvinfo->depth,
237 InputOutput, d->xvinfo->visual, mask, &swa);
238
239 ALLEGRO_DEBUG("Window ID: %ld\n", (long)d->window);
240
241 /* Try to set full screen mode if requested, fail if we can't. */
242 if (display->flags & ALLEGRO_FULLSCREEN) {
243 /* According to the spec, the window manager is supposed to disable
244 * window decorations when _NET_WM_STATE_FULLSCREEN is in effect.
245 * However, some WMs may not be fully compliant, e.g. Fluxbox.
246 */
247 _al_xwin_set_frame(display, false);
248 _al_xwin_set_above(display, 1);
249 if (!_al_xglx_fullscreen_set_mode(system, d, w, h, 0,
250 display->refresh_rate)) {
251 ALLEGRO_DEBUG("xdpy: failed to set fullscreen mode.\n");
252 return false;
253 }
254 }
255
256 ALLEGRO_DEBUG("X11 window created.\n");
257
258 /* Set the PID related to the window. */
259 Atom _NET_WM_PID = XInternAtom(system->x11display, "_NET_WM_PID", False);
260 int pid = getpid();
261 XChangeProperty(system->x11display, d->window, _NET_WM_PID, XA_CARDINAL,
262 32, PropModeReplace, (unsigned char *)&pid, 1);
263
264 _al_xwin_set_size_hints(display, x_off, y_off);
265
266 /* Let the window manager know we're a "normal" window */
267 Atom _NET_WM_WINDOW_TYPE;
268 Atom _NET_WM_WINDOW_TYPE_NORMAL;
269
270 _NET_WM_WINDOW_TYPE = XInternAtom(system->x11display, "_NET_WM_WINDOW_TYPE",
271 False);
272 _NET_WM_WINDOW_TYPE_NORMAL = XInternAtom(system->x11display,
273 "_NET_WM_WINDOW_TYPE_NORMAL",
274 False);
275 XChangeProperty(system->x11display, d->window, _NET_WM_WINDOW_TYPE, XA_ATOM,
276 32, PropModeReplace,
277 (unsigned char *)&_NET_WM_WINDOW_TYPE_NORMAL, 1);
278
279 /* This seems like a good idea */
280 set_compositor_bypass_flag(display);
281
282 #ifdef ALLEGRO_XWINDOWS_WITH_XINPUT2
283 /* listen for touchscreen events */
284 XIEventMask event_mask;
285 event_mask.deviceid = XIAllMasterDevices;
286 event_mask.mask_len = XIMaskLen(XI_TouchEnd);
287 event_mask.mask = (unsigned char*)al_calloc(3, sizeof(char));
288 XISetMask(event_mask.mask, XI_TouchBegin);
289 XISetMask(event_mask.mask, XI_TouchUpdate);
290 XISetMask(event_mask.mask, XI_TouchEnd);
291
292 XISelectEvents(system->x11display, d->window, &event_mask, 1);
293
294 al_free(event_mask.mask);
295 #endif
296
297 return true;
298 }
299
300
301 static ALLEGRO_DISPLAY_XGLX *xdpy_create_display_locked(
302 ALLEGRO_SYSTEM_XGLX *system, int flags, int w, int h, int adapter)
303 {
304 ALLEGRO_DISPLAY_XGLX *d = al_calloc(1, sizeof *d);
305 ALLEGRO_DISPLAY *display = (ALLEGRO_DISPLAY *)d;
306 ALLEGRO_OGL_EXTRAS *ogl = al_calloc(1, sizeof *ogl);
307 display->ogl_extras = ogl;
308
309 d->glx_version = query_glx_version(system);
310
311 display->w = w;
312 display->h = h;
313 display->vt = _al_display_xglx_driver();
314 display->refresh_rate = al_get_new_display_refresh_rate();
315 display->flags = flags;
316 // FIXME: default? Is this the right place to set this?
317 display->flags |= ALLEGRO_OPENGL;
318 #ifdef ALLEGRO_CFG_OPENGLES2
319 display->flags |= ALLEGRO_PROGRAMMABLE_PIPELINE;
320 #endif
321 #ifdef ALLEGRO_CFG_OPENGLES
322 display->flags |= ALLEGRO_OPENGL_ES_PROFILE;
323 #endif
324
325 /* Store our initial virtual adapter, used by fullscreen and positioning
326 * code.
327 */
328 ALLEGRO_DEBUG("selected adapter %i\n", adapter);
329 if (adapter < 0)
330 d->adapter = _al_xglx_get_default_adapter(system);
331 else
332 d->adapter = adapter;
333
334 ALLEGRO_DEBUG("xdpy: selected adapter %i\n", d->adapter);
335 _al_xglx_use_adapter(system, d->adapter);
336 if (!check_adapter_use_count(system)) {
337 goto EarlyError;
338 }
339
340 /* Store our initial X Screen, used by window creation, fullscreen, and glx
341 * visual code.
342 */
343 d->xscreen = _al_xglx_get_xscreen(system, d->adapter);
344 ALLEGRO_DEBUG("xdpy: selected xscreen %i\n", d->xscreen);
345
346 d->wm_delete_window_atom = None;
347
348 d->is_mapped = false;
349 _al_cond_init(&d->mapped);
350
351 d->is_selectioned = false;
352 _al_cond_init(&d->selectioned);
353
354
355 d->resize_count = 0;
356 d->programmatic_resize = false;
357
358 _al_xglx_config_select_visual(d);
359
360 if (!d->xvinfo) {
361 ALLEGRO_ERROR("FIXME: Need better visual selection.\n");
362 ALLEGRO_ERROR("No matching visual found.\n");
363 goto EarlyError;
364 }
365
366 ALLEGRO_INFO("Selected X11 visual %lu.\n", d->xvinfo->visualid);
367
368 /* Add ourself to the list of displays. */
369 ALLEGRO_DISPLAY_XGLX **add;
370 add = _al_vector_alloc_back(&system->system.displays);
371 *add = d;
372
373 /* Each display is an event source. */
374 _al_event_source_init(&display->es);
375
376 if (!xdpy_create_display_window(system, d, w, h, adapter)) {
377 goto LateError;
378 }
379
380 /* Send any pending requests to the X server.
381 * This is necessary to make the window ID immediately valid
382 * for a GtkSocket.
383 */
384 XSync(system->x11display, False);
385
386 if (display->flags & ALLEGRO_GTK_TOPLEVEL_INTERNAL) {
387 ASSERT(gtk_override_vt);
388 if (!gtk_override_vt->create_display_hook(display, w, h)) {
389 goto LateError;
390 }
391 }
392 else {
393 default_overridable_vt.set_window_title(display, al_get_new_window_title());
394 if (!default_overridable_vt.create_display_hook(display, w, h)) {
395 goto LateError;
396 }
397 }
398
399 /* overridable_vt should be set by the create_display_hook. */
400 ASSERT(d->overridable_vt);
401
402 /* Send any pending requests to the X server. */
403 XSync(system->x11display, False);
404
405 /* To avoid race conditions where some X11 functions fail before the window
406 * is mapped, we wait here until it is mapped. Note that the thread is
407 * locked, so the event could not possibly have been processed yet in the
408 * events thread. So as long as no other map events occur, the condition
409 * should only be signalled when our window gets mapped.
410 */
411 while (!d->is_mapped) {
412 _al_cond_wait(&d->mapped, &system->lock);
413 }
414 /* In tiling WMs, we might get resize events pretty much immediately after
415 * Window creation. This location seems to catch them reliably, tested with
416 * dwm, awesome, xmonad and i3. */
417 if ((display->flags & ALLEGRO_RESIZABLE) && d->resize_count > 0) {
418 xdpy_acknowledge_resize(display);
419 }
420
421 /* We can do this at any time, but if we already have a mapped
422 * window when switching to fullscreen it will use the same
423 * monitor (with the MetaCity version I'm using here right now).
424 */
425 if ((display->flags & ALLEGRO_FULLSCREEN_WINDOW)) {
426 ALLEGRO_INFO("Toggling fullscreen flag for %d x %d window.\n",
427 display->w, display->h);
428 _al_xwin_reset_size_hints(display);
429 _al_xwin_set_fullscreen_window(display, 2);
430 _al_xwin_set_size_hints(display, INT_MAX, INT_MAX);
431
432 XWindowAttributes xwa;
433 XGetWindowAttributes(system->x11display, d->window, &xwa);
434 display->w = xwa.width;
435 display->h = xwa.height;
436 ALLEGRO_INFO("Using ALLEGRO_FULLSCREEN_WINDOW of %d x %d\n",
437 display->w, display->h);
438 }
439
440 if (display->flags & ALLEGRO_FULLSCREEN) {
441 /* kwin wants these here */
442 /* metacity wants these here too */
443 /* XXX compiz is quiky, can't seem to find a combination of hints that
444 * make sure we are layerd over panels, and are positioned properly */
445
446 //_al_xwin_set_fullscreen_window(display, 1);
447 _al_xwin_set_above(display, 1);
448
449 _al_xglx_fullscreen_to_display(system, d);
450
451 /* Grab mouse if we only have one display, ungrab it if we have more than
452 * one.
453 */
454 if (_al_vector_size(&system->system.displays) == 1) {
455 al_grab_mouse(display);
456 }
457 else if (_al_vector_size(&system->system.displays) > 1) {
458 al_ungrab_mouse();
459 }
460 }
461
462 if (display->flags & ALLEGRO_FRAMELESS) {
463 /* set_display_flag works by comparing to current flag value, so we need
464 * to start with an unset state. */
465 display->flags &= ~ALLEGRO_FRAMELESS;
466 /* Call the vt directly to bypass system lock.
467 */
468 d->overridable_vt->set_display_flag(display, ALLEGRO_FRAMELESS, true);
469 }
470
471 if (display->flags & ALLEGRO_DRAG_AND_DROP) {
472 _al_xwin_accept_drag_and_drop(display, true);
473 }
474
475 if (flags & ALLEGRO_MAXIMIZED) {
476 /* set_display_flag works by comparing to current flag value, so we need
477 * to start with an unset state. */
478 display->flags &= ~ALLEGRO_MAXIMIZED;
479 /* Call the vt directly to bypass system lock.
480 */
481 d->overridable_vt->set_display_flag(display, ALLEGRO_MAXIMIZED, true);
482 }
483
484 if (!_al_xglx_config_create_context(d)) {
485 goto LateError;
486 }
487
488 /* Make our GLX context current for reading and writing in the current
489 * thread.
490 */
491 if (d->fbc) {
492 if (!glXMakeContextCurrent(system->gfxdisplay, d->glxwindow,
493 d->glxwindow, d->context)) {
494 ALLEGRO_ERROR("glXMakeContextCurrent failed\n");
495 }
496 }
497 else {
498 if (!glXMakeCurrent(system->gfxdisplay, d->glxwindow, d->context)) {
499 ALLEGRO_ERROR("glXMakeCurrent failed\n");
500 }
501 }
502
503 _al_ogl_manage_extensions(display);
504 _al_ogl_set_extensions(ogl->extension_api);
505
506 /* Print out OpenGL version info */
507 ALLEGRO_INFO("OpenGL Version: %s\n", (const char*)glGetString(GL_VERSION));
508 ALLEGRO_INFO("Vendor: %s\n", (const char*)glGetString(GL_VENDOR));
509 ALLEGRO_INFO("Renderer: %s\n", (const char*)glGetString(GL_RENDERER));
510
511 /* Fill in opengl version */
512 const int v = display->ogl_extras->ogl_info.version;
513 display->extra_settings.settings[ALLEGRO_OPENGL_MAJOR_VERSION] = (v >> 24) & 0xFF;
514 display->extra_settings.settings[ALLEGRO_OPENGL_MINOR_VERSION] = (v >> 16) & 0xFF;
515
516 if (display->ogl_extras->ogl_info.version < _ALLEGRO_OPENGL_VERSION_1_2) {
517 ALLEGRO_EXTRA_DISPLAY_SETTINGS *eds = _al_get_new_display_settings();
518 if (eds->required & (1<<ALLEGRO_COMPATIBLE_DISPLAY)) {
519 ALLEGRO_ERROR("Allegro requires at least OpenGL version 1.2 to work.\n");
520 goto LateError;
521 }
522 display->extra_settings.settings[ALLEGRO_COMPATIBLE_DISPLAY] = 0;
523 }
524
525 if (display->extra_settings.settings[ALLEGRO_COMPATIBLE_DISPLAY])
526 _al_ogl_setup_gl(display);
527
528 /* vsync */
529 int vsync_setting = _al_get_new_display_settings()->settings[ALLEGRO_VSYNC];
530 vsync_setting = xdpy_swap_control(display, vsync_setting);
531 display->extra_settings.settings[ALLEGRO_VSYNC] = vsync_setting;
532
533 d->invisible_cursor = None; /* Will be created on demand. */
534 d->current_cursor = None; /* Initially, we use the root cursor. */
535 d->is_system_cursor = true;
536 d->cursor_hidden = false;
537
538 d->icon = None;
539 d->icon_mask = None;
540
541 return d;
542
543 EarlyError:
544 al_free(d);
545 al_free(ogl);
546 return NULL;
547
548 LateError:
549 xdpy_destroy_display(display);
550 return NULL;
551 }
552
553
554 static bool xdpy_create_display_hook_default(ALLEGRO_DISPLAY *display,
555 int w, int h)
556 {
557 ALLEGRO_SYSTEM_XGLX *system = (ALLEGRO_SYSTEM_XGLX *)al_get_system_driver();
558 ALLEGRO_DISPLAY_XGLX *d = (ALLEGRO_DISPLAY_XGLX *)display;
559 (void)w;
560 (void)h;
561
562 if (_al_xwin_initial_icon) {
563 ALLEGRO_BITMAP *bitmaps[] = {_al_xwin_initial_icon};
564 _al_xwin_set_icons(display, 1, bitmaps);
565 } else {
566 ALLEGRO_STATE state;
567 al_store_state(&state, ALLEGRO_STATE_NEW_BITMAP_PARAMETERS);
568 al_set_new_bitmap_flags(ALLEGRO_MEMORY_BITMAP);
569 ALLEGRO_BITMAP *bitmap = al_create_bitmap(ICON_WIDTH, ICON_HEIGHT);
570 al_restore_state(&state);
571
572 ALLEGRO_LOCKED_REGION *lr = al_lock_bitmap(bitmap, ALLEGRO_PIXEL_FORMAT_RGBA_8888, ALLEGRO_LOCK_WRITEONLY);
573 for (int y = 0; y < ICON_HEIGHT; y++) {
574 memcpy((char*)lr->data + lr->pitch * y, &icon_data[ICON_WIDTH * y], ICON_WIDTH * 4);
575 }
576 al_unlock_bitmap(bitmap);
577
578 ALLEGRO_BITMAP *bitmaps[] = {bitmap};
579 _al_xwin_set_icons(display, 1, bitmaps);
580 al_destroy_bitmap(bitmap);
581 }
582
583 XLockDisplay(system->x11display);
584
585 XMapWindow(system->x11display, d->window);
586 ALLEGRO_DEBUG("X11 window mapped.\n");
587
588 d->wm_delete_window_atom = XInternAtom(system->x11display,
589 "WM_DELETE_WINDOW", False);
590 XSetWMProtocols(system->x11display, d->window, &d->wm_delete_window_atom, 1);
591
592 XUnlockDisplay(system->x11display);
593
594 d->overridable_vt = &default_overridable_vt;
595
596 return true;
597 }
598
599
600 /* Create a new X11 display, which maps directly to a GLX window. */
601 static ALLEGRO_DISPLAY *xdpy_create_display(int w, int h)
602 {
603 ALLEGRO_SYSTEM_XGLX *system = (ALLEGRO_SYSTEM_XGLX *)al_get_system_driver();
604 ALLEGRO_DISPLAY_XGLX *display;
605 int flags;
606 int adapter;
607
608 if (system->x11display == NULL) {
609 ALLEGRO_WARN("Not connected to X server.\n");
610 return NULL;
611 }
612
613 if (w <= 0 || h <= 0) {
614 ALLEGRO_ERROR("Invalid window size %dx%d\n", w, h);
615 return NULL;
616 }
617
618 flags = al_get_new_display_flags();
619 if (flags & ALLEGRO_GTK_TOPLEVEL_INTERNAL) {
620 if (gtk_override_vt == NULL) {
621 ALLEGRO_ERROR("GTK requested but unavailable\n");
622 return NULL;
623 }
624 if (flags & ALLEGRO_FULLSCREEN) {
625 ALLEGRO_ERROR("GTK incompatible with fullscreen\n");
626 return NULL;
627 }
628 }
629
630 _al_mutex_lock(&system->lock);
631
632 adapter = al_get_new_display_adapter();
633 display = xdpy_create_display_locked(system, flags, w, h, adapter);
634
635 _al_mutex_unlock(&system->lock);
636
637 return (ALLEGRO_DISPLAY *)display;
638 }
639
640
641 static void convert_display_bitmaps_to_memory_bitmap(ALLEGRO_DISPLAY *d)
642 {
643 ALLEGRO_DEBUG("converting display bitmaps to memory bitmaps.\n");
644
645 while (d->bitmaps._size > 0) {
646 ALLEGRO_BITMAP **bptr = _al_vector_ref_back(&d->bitmaps);
647 ALLEGRO_BITMAP *b = *bptr;
648 _al_convert_to_memory_bitmap(b);
649 }
650 }
651
652
653 static void transfer_display_bitmaps_to_any_other_display(
654 ALLEGRO_SYSTEM_XGLX *s, ALLEGRO_DISPLAY *d)
655 {
656 size_t i;
657 ALLEGRO_DISPLAY *living = NULL;
658 ASSERT(s->system.displays._size > 1);
659
660 for (i = 0; i < s->system.displays._size; i++) {
661 ALLEGRO_DISPLAY **slot = _al_vector_ref(&s->system.displays, i);
662 living = *slot;
663 if (living != d)
664 break;
665 }
666
667 ALLEGRO_DEBUG("transferring display bitmaps to other display.\n");
668
669 for (i = 0; i < d->bitmaps._size; i++) {
670 ALLEGRO_BITMAP **add = _al_vector_alloc_back(&(living->bitmaps));
671 ALLEGRO_BITMAP **ref = _al_vector_ref(&d->bitmaps, i);
672 *add = *ref;
673 (*add)->_display = living;
674 }
675 }
676
677
678 static void restore_mode_if_last_fullscreen_display(ALLEGRO_SYSTEM_XGLX *s,
679 ALLEGRO_DISPLAY_XGLX *d)
680 {
681 bool last_fullscreen = true;
682 size_t i;
683
684 /* If any other fullscreen display is still active on the same adapter,
685 * we must not touch the video mode.
686 */
687 for (i = 0; i < s->system.displays._size; i++) {
688 ALLEGRO_DISPLAY_XGLX **slot = _al_vector_ref(&s->system.displays, i);
689 ALLEGRO_DISPLAY_XGLX *living = *slot;
690
691 if (living == d)
692 continue;
693
694 /* Check for fullscreen displays on the same adapter. */
695 if (living->adapter == d->adapter
696 && (living->display.flags & ALLEGRO_FULLSCREEN)) {
697 last_fullscreen = false;
698 }
699 }
700
701 if (last_fullscreen) {
702 ALLEGRO_DEBUG("restore mode.\n");
703 _al_xglx_restore_video_mode(s, d->adapter);
704 }
705 else {
706 ALLEGRO_DEBUG("*not* restoring mode.\n");
707 }
708 }
709
710
711 static void xdpy_destroy_display_hook_default(ALLEGRO_DISPLAY *d, bool is_last)
712 {
713 ALLEGRO_SYSTEM_XGLX *s = (ALLEGRO_SYSTEM_XGLX *)al_get_system_driver();
714 ALLEGRO_DISPLAY_XGLX *glx = (ALLEGRO_DISPLAY_XGLX *)d;
715 (void)is_last;
716
717 if (glx->context) {
718 glXDestroyContext(s->gfxdisplay, glx->context);
719 glx->context = NULL;
720 ALLEGRO_DEBUG("destroy context.\n");
721 }
722
723 if (glx->fbc) {
724 al_free(glx->fbc);
725 glx->fbc = NULL;
726 XFree(glx->xvinfo);
727 glx->xvinfo = NULL;
728 }
729 else if (glx->xvinfo) {
730 al_free(glx->xvinfo);
731 glx->xvinfo = NULL;
732 }
733
734 if ((glx->glxwindow) && (glx->glxwindow != glx->window)) {
735 glXDestroyWindow(s->x11display, glx->glxwindow);
736 glx->glxwindow = 0;
737 ALLEGRO_DEBUG("destroy glx window\n");
738 }
739
740 _al_cond_destroy(&glx->mapped);
741 _al_cond_destroy(&glx->selectioned);
742
743 ALLEGRO_DEBUG("destroy window.\n");
744 XDestroyWindow(s->x11display, glx->window);
745
746 _al_xglx_unuse_adapter(s, glx->adapter);
747
748 if (d->flags & ALLEGRO_FULLSCREEN) {
749 restore_mode_if_last_fullscreen_display(s, glx);
750 }
751 }
752
753
754 static void xdpy_destroy_display(ALLEGRO_DISPLAY *d)
755 {
756 ALLEGRO_SYSTEM_XGLX *s = (ALLEGRO_SYSTEM_XGLX *)al_get_system_driver();
757 ALLEGRO_DISPLAY_XGLX *glx = (ALLEGRO_DISPLAY_XGLX *)d;
758 ALLEGRO_OGL_EXTRAS *ogl = d->ogl_extras;
759 bool is_last;
760
761 ALLEGRO_DEBUG("destroying display.\n");
762
763 /* If we're the last display, convert all bitmaps to display independent
764 * (memory) bitmaps. Otherwise, pass all bitmaps to any other living
765 * display. We assume all displays are compatible.)
766 */
767 is_last = (s->system.displays._size == 1);
768 if (is_last)
769 convert_display_bitmaps_to_memory_bitmap(d);
770 else
771 transfer_display_bitmaps_to_any_other_display(s, d);
772
773 _al_ogl_unmanage_extensions(d);
774 ALLEGRO_DEBUG("unmanaged extensions.\n");
775
776 _al_mutex_lock(&s->lock);
777 _al_vector_find_and_delete(&s->system.displays, &d);
778
779 if (ogl->backbuffer) {
780 _al_ogl_destroy_backbuffer(ogl->backbuffer);
781 ogl->backbuffer = NULL;
782 ALLEGRO_DEBUG("destroy backbuffer.\n");
783 }
784
785 if (glx->overridable_vt) {
786 glx->overridable_vt->destroy_display_hook(d, is_last);
787 }
788
789 if (s->mouse_grab_display == d) {
790 s->mouse_grab_display = NULL;
791 }
792
793 _al_vector_free(&d->bitmaps);
794 _al_event_source_free(&d->es);
795
796 al_free(d->ogl_extras);
797 al_free(d->vertex_cache);
798 al_free(d);
799
800 _al_mutex_unlock(&s->lock);
801
802 ALLEGRO_DEBUG("destroy display finished.\n");
803 }
804
805
806 static bool xdpy_make_current(ALLEGRO_DISPLAY *d)
807 {
808 ALLEGRO_SYSTEM_XGLX *system = (ALLEGRO_SYSTEM_XGLX *)al_get_system_driver();
809 ALLEGRO_DISPLAY_XGLX *glx = (ALLEGRO_DISPLAY_XGLX *)d;
810
811 /* Make our GLX context current for reading and writing in the current
812 * thread.
813 */
814 if (glx->fbc) {
815 return glXMakeContextCurrent(system->gfxdisplay, glx->glxwindow,
816 glx->glxwindow, glx->context);
817 }
818 else {
819 return glXMakeCurrent(system->gfxdisplay, glx->glxwindow, glx->context);
820 }
821 }
822
823
824 static bool xdpy_set_current_display(ALLEGRO_DISPLAY *d)
825 {
826 bool rc;
827
828 rc = xdpy_make_current(d);
829 if (rc) {
830 ALLEGRO_OGL_EXTRAS *ogl = d->ogl_extras;
831 _al_ogl_set_extensions(ogl->extension_api);
832 _al_ogl_update_render_state(d);
833 }
834
835 return rc;
836 }
837
838
839 static void xdpy_unset_current_display(ALLEGRO_DISPLAY *d)
840 {
841 ALLEGRO_SYSTEM_XGLX *system = (ALLEGRO_SYSTEM_XGLX *)al_get_system_driver();
842 glXMakeContextCurrent(system->gfxdisplay, None, None, NULL);
843 (void)d;
844 }
845
846
847 static void xdpy_flip_display(ALLEGRO_DISPLAY *d)
848 {
849 ALLEGRO_SYSTEM_XGLX *system = (ALLEGRO_SYSTEM_XGLX *)al_get_system_driver();
850 ALLEGRO_DISPLAY_XGLX *glx = (ALLEGRO_DISPLAY_XGLX *)d;
851
852 int e = glGetError();
853 if (e) {
854 ALLEGRO_ERROR("OpenGL error was not 0: %s\n", _al_gl_error_string(e));
855 }
856
857 if (d->extra_settings.settings[ALLEGRO_SINGLE_BUFFER])
858 glFlush();
859 else
860 glXSwapBuffers(system->gfxdisplay, glx->glxwindow);
861 }
862
863
864 static void xdpy_update_display_region(ALLEGRO_DISPLAY *d, int x, int y,
865 int w, int h)
866 {
867 (void)x;
868 (void)y;
869 (void)w;
870 (void)h;
871 xdpy_flip_display(d);
872 }
873
874
875 static bool xdpy_acknowledge_resize(ALLEGRO_DISPLAY *d)
876 {
877 ALLEGRO_SYSTEM_XGLX *system = (ALLEGRO_SYSTEM_XGLX *)al_get_system_driver();
878 ALLEGRO_DISPLAY_XGLX *glx = (ALLEGRO_DISPLAY_XGLX *)d;
879 XWindowAttributes xwa;
880 unsigned int w, h;
881
882 _al_mutex_lock(&system->lock);
883
884 /* glXQueryDrawable is GLX 1.3+. */
885 /*
886 glXQueryDrawable(system->x11display, glx->glxwindow, GLX_WIDTH, &w);
887 glXQueryDrawable(system->x11display, glx->glxwindow, GLX_HEIGHT, &h);
888 */
889
890 XGetWindowAttributes(system->x11display, glx->window, &xwa);
891 w = xwa.width;
892 h = xwa.height;
893
894 if ((int)w != d->w || (int)h != d->h) {
895 d->w = w;
896 d->h = h;
897
898 ALLEGRO_DEBUG("xdpy: acknowledge_resize (%d, %d)\n", d->w, d->h);
899
900 /* No context yet means this is a stray call happening during
901 * initialization.
902 */
903 if (glx->context) {
904 _al_ogl_setup_gl(d);
905 }
906
907 glx->overridable_vt->check_maximized(d);
908 }
909
910 _al_mutex_unlock(&system->lock);
911
912 return true;
913 }
914
915
916 /* Note: The system mutex must be locked (exactly once) so when we
917 * wait for the condition variable it gets auto-unlocked. For a
918 * nested lock that would not be the case.
919 */
920 void _al_display_xglx_await_resize(ALLEGRO_DISPLAY *d, int old_resize_count,
921 bool delay_hack)
922 {
923 ALLEGRO_SYSTEM_XGLX *system = (void *)al_get_system_driver();
924 ALLEGRO_DISPLAY_XGLX *glx = (ALLEGRO_DISPLAY_XGLX *)d;
925 ALLEGRO_TIMEOUT timeout;
926
927 ALLEGRO_DEBUG("Awaiting resize event\n");
928
929 XSync(system->x11display, False);
930
931 /* Wait until we are actually resized.
932 * Don't wait forever if an event never comes.
933 */
934 al_init_timeout(&timeout, 1.0);
935 while (old_resize_count == glx->resize_count) {
936 if (_al_cond_timedwait(&system->resized, &system->lock, &timeout) == -1) {
937 ALLEGRO_ERROR("Timeout while waiting for resize event.\n");
938 return;
939 }
940 }
941
942 /* XXX: This hack helps when toggling between fullscreen windows and not,
943 * on various window managers.
944 */
945 if (delay_hack) {
946 al_rest(0.2);
947 }
948
949 xdpy_acknowledge_resize(d);
950 }
951
952
953 static bool xdpy_resize_display_default(ALLEGRO_DISPLAY *d, int w, int h)
954 {
955 ALLEGRO_SYSTEM_XGLX *system = (ALLEGRO_SYSTEM_XGLX *)al_get_system_driver();
956 ALLEGRO_DISPLAY_XGLX *glx = (ALLEGRO_DISPLAY_XGLX *)d;
957 XWindowAttributes xwa;
958 int attempts;
959 bool ret = false;
960
961 _al_mutex_lock(&system->lock);
962
963 /* It seems some X servers will treat the resize as a no-op if the window is
964 * already the right size, so check for it to avoid a deadlock later.
965 */
966 XGetWindowAttributes(system->x11display, glx->window, &xwa);
967 if (xwa.width == w && xwa.height == h) {
968 _al_mutex_unlock(&system->lock);
969 return false;
970 }
971
972 if (d->flags & ALLEGRO_FULLSCREEN) {
973 _al_xwin_set_fullscreen_window(d, 0);
974 if (!_al_xglx_fullscreen_set_mode(system, glx, w, h, 0, 0)) {
975 ret = false;
976 goto skip_resize;
977 }
978 attempts = 3;
979 }
980 else {
981 attempts = 1;
982 }
983
984 /* Hack: try multiple times to resize the window, with delays. KDE reacts
985 * slowly to the video mode change, and won't resize our window until a
986 * while after. It would be better to wait for some sort of event rather
987 * than just waiting some amount of time, but I didn't manage to find that
988 * event. --pw
989 */
990 for (; attempts >= 0; attempts--) {
991 const int old_resize_count = glx->resize_count;
992 ALLEGRO_DEBUG("calling XResizeWindow, attempts=%d\n", attempts);
993 _al_xwin_reset_size_hints(d);
994 glx->programmatic_resize = true;
995 XResizeWindow(system->x11display, glx->window, w, h);
996 _al_display_xglx_await_resize(d, old_resize_count,
997 (d->flags & ALLEGRO_FULLSCREEN));
998 glx->programmatic_resize = false;
999 _al_xwin_set_size_hints(d, INT_MAX, INT_MAX);
1000
1001 if (d->w == w && d->h == h) {
1002 ret = true;
1003 break;
1004 }
1005
1006 /* Wait before trying again. */
1007 al_rest(0.333);
1008 }
1009
1010 if (attempts == 0) {
1011 ALLEGRO_ERROR("XResizeWindow didn't work; giving up\n");
1012 }
1013
1014 skip_resize:
1015
1016 if (d->flags & ALLEGRO_FULLSCREEN) {
1017 _al_xwin_set_fullscreen_window(d, 1);
1018 _al_xwin_set_above(d, 1);
1019 _al_xglx_fullscreen_to_display(system, glx);
1020 ALLEGRO_DEBUG("xdpy: resize fullscreen?\n");
1021 }
1022
1023 _al_mutex_unlock(&system->lock);
1024 return ret;
1025 }
1026
1027
1028 static bool xdpy_resize_display(ALLEGRO_DISPLAY *d, int w, int h)
1029 {
1030 ALLEGRO_DISPLAY_XGLX *glx = (ALLEGRO_DISPLAY_XGLX *)d;
1031
1032 /* A fullscreen-window can't be resized. */
1033 if (d->flags & ALLEGRO_FULLSCREEN_WINDOW)
1034 return false;
1035
1036 return glx->overridable_vt->resize_display(d, w, h);
1037 }
1038
1039
1040 void _al_xglx_display_configure(ALLEGRO_DISPLAY *d, int x, int y,
1041 int width, int height, bool setglxy)
1042 {
1043 ALLEGRO_DISPLAY_XGLX *glx = (ALLEGRO_DISPLAY_XGLX *)d;
1044
1045 ALLEGRO_EVENT_SOURCE *es = &glx->display.es;
1046 _al_event_source_lock(es);
1047
1048 _al_xwin_get_borders(d);
1049
1050 /* Generate a resize event if the size has changed non-programmatically.
1051 * We cannot asynchronously change the display size here yet, since the user
1052 * will only know about a changed size after receiving the resize event.
1053 * Here we merely add the event to the queue.
1054 */
1055 if (!glx->programmatic_resize &&
1056 (d->w != width ||
1057 d->h != height)) {
1058 if (_al_event_source_needs_to_generate_event(es)) {
1059 ALLEGRO_EVENT event;
1060 event.display.type = ALLEGRO_EVENT_DISPLAY_RESIZE;
1061 event.display.timestamp = al_get_time();
1062 event.display.x = x;
1063 event.display.y = y;
1064 event.display.width = width;
1065 event.display.height = height;
1066 _al_event_source_emit_event(es, &event);
1067 }
1068 }
1069
1070 if (setglxy) {
1071 // Note: XConfigure events will have our inner window position
1072 // which is what we are using for al_set/get_window_position
1073 glx->x = x;
1074 glx->y = y;
1075
1076 if (glx->need_initial_position_adjust && glx->borders_known) {
1077 glx->need_initial_position_adjust = false;
1078 // Unfortunately there doesn't seem to be a good way to know the
1079 // border size before mapping the window, so we have to adjust
1080 // the position slightly now, after the window is mapped.
1081 // The scenario is this:
1082 // 1. We create the window, passing the user position to X11, which
1083 // is interpreted as outer coordinates. Since borders are
1084 // not known yet d->x/d->y are also set to that position.
1085 // 2. The window is mapped and X11 sends us the inner coordinates
1086 // in XConfigure which we store in d->x/d->y.
1087 // 3. Now d->x/d->y is in sync with the real position, but if
1088 // we wanted a specific position it is off by the border size
1089 // and so we adjust it below.
1090 ALLEGRO_DEBUG("Adjusting initial position: %d/%d", glx->x - glx->border_left, glx->y - glx->border_top);
1091 al_set_window_position(d, glx->x - glx->border_left, glx->y - glx->border_top);
1092 }
1093 }
1094
1095 ALLEGRO_SYSTEM_XGLX *system = (ALLEGRO_SYSTEM_XGLX*)al_get_system_driver();
1096 ALLEGRO_MONITOR_INFO mi;
1097 int center_x = (glx->x + (glx->x + width)) / 2;
1098 int center_y = (glx->y + (glx->y + height)) / 2;
1099
1100 _al_xglx_get_monitor_info(system, glx->adapter, &mi);
1101
1102 ALLEGRO_DEBUG("xconfigure event! %ix%i\n", x, y);
1103
1104 /* check if we're no longer inside the stored adapter */
1105 if ((center_x < mi.x1 && center_x > mi.x2) ||
1106 (center_y < mi.y1 && center_y > mi.x2))
1107 {
1108 int new_adapter = _al_xglx_get_adapter(system, glx, true);
1109 if (new_adapter != glx->adapter) {
1110 ALLEGRO_DEBUG("xdpy: adapter change!\n");
1111 _al_xglx_unuse_adapter(system, glx->adapter);
1112 if (d->flags & ALLEGRO_FULLSCREEN)
1113 _al_xglx_restore_video_mode(system, glx->adapter);
1114 glx->adapter = new_adapter;
1115 _al_xglx_use_adapter(system, glx->adapter);
1116 }
1117
1118 }
1119
1120 glx->overridable_vt->check_maximized(d);
1121
1122 _al_event_source_unlock(es);
1123 }
1124
1125
1126 /* Handle an X11 configure event. [X11 thread]
1127 * Only called from the event handler with the system locked.
1128 */
1129 void _al_xglx_display_configure_event(ALLEGRO_DISPLAY *d, XEvent *xevent)
1130 {
1131 /* We receive two configure events when toggling the window frame.
1132 * We ignore the first one as it has bogus coordinates.
1133 * The only way to tell them apart seems to be the send_event field.
1134 * Unfortunately, we also end up ignoring the only event we receive in
1135 * response to a XMoveWindow request so we have to compensate for that.
1136 */
1137 bool setglxy = (xevent->xconfigure.send_event);
1138 _al_xglx_display_configure(d, xevent->xconfigure.x, xevent->xconfigure.y,
1139 xevent->xconfigure.width, xevent->xconfigure.height, setglxy);
1140 }
1141
1142
1143
1144 /* Handle X11 switch event. [X11 thread]
1145 */
1146 void _al_xwin_display_switch_handler(ALLEGRO_DISPLAY *display,
1147 XFocusChangeEvent *xevent)
1148 {
1149 /* Mouse click in/out tend to set NotifyNormal events. For Alt-Tab,
1150 * there are also accompanying NotifyGrab/NotifyUngrab events which we don't
1151 * care about. At this point, some WMs either send NotifyNormal (KDE) or
1152 * NotifyWhileGrabbed (GNOME).
1153 */
1154 if (xevent->mode == NotifyNormal || xevent->mode == NotifyWhileGrabbed)
1155 _al_xwin_display_switch_handler_inner(display, (xevent->type == FocusIn));
1156 }
1157
1158
1159
1160 /* Handle X11 switch event. [X11 thread]
1161 */
1162 void _al_xwin_display_switch_handler_inner(ALLEGRO_DISPLAY *display, bool focus_in)
1163 {
1164 ALLEGRO_EVENT_SOURCE *es = &display->es;
1165 _al_event_source_lock(es);
1166 if (_al_event_source_needs_to_generate_event(es)) {
1167 ALLEGRO_EVENT event;
1168 if (focus_in)
1169 event.display.type = ALLEGRO_EVENT_DISPLAY_SWITCH_IN;
1170 else
1171 event.display.type = ALLEGRO_EVENT_DISPLAY_SWITCH_OUT;
1172 event.display.timestamp = al_get_time();
1173 _al_event_source_emit_event(es, &event);
1174 }
1175 _al_event_source_unlock(es);
1176 }
1177
1178
1179
1180 void _al_xwin_display_expose(ALLEGRO_DISPLAY *display,
1181 XExposeEvent *xevent)
1182 {
1183 ALLEGRO_EVENT_SOURCE *es = &display->es;
1184 _al_event_source_lock(es);
1185 if (_al_event_source_needs_to_generate_event(es)) {
1186 ALLEGRO_EVENT event;
1187 event.display.type = ALLEGRO_EVENT_DISPLAY_EXPOSE;
1188 event.display.timestamp = al_get_time();
1189 event.display.x = xevent->x;
1190 event.display.y = xevent->y;
1191 event.display.width = xevent->width;
1192 event.display.height = xevent->height;
1193 _al_event_source_emit_event(es, &event);
1194 }
1195 _al_event_source_unlock(es);
1196 }
1197
1198
1199 static bool xdpy_is_compatible_bitmap(ALLEGRO_DISPLAY *display,
1200 ALLEGRO_BITMAP *bitmap)
1201 {
1202 /* All GLX bitmaps are compatible. */
1203 (void)display;
1204 (void)bitmap;
1205 return true;
1206 }
1207
1208
1209 static void xdpy_set_window_title_default(ALLEGRO_DISPLAY *display, const char *title)
1210 {
1211 ALLEGRO_SYSTEM_XGLX *system = (ALLEGRO_SYSTEM_XGLX *)al_get_system_driver();
1212 ALLEGRO_DISPLAY_XGLX *glx = (ALLEGRO_DISPLAY_XGLX *)display;
1213
1214 {
1215 Atom WM_NAME = XInternAtom(system->x11display, "WM_NAME", False);
1216 Atom _NET_WM_NAME = XInternAtom(system->x11display, "_NET_WM_NAME", False);
1217 char *list[1] = { (char *) title };
1218 XTextProperty property;
1219 int status = Xutf8TextListToTextProperty(system->x11display, list, 1, XUTF8StringStyle,
1220 &property);
1221
1222 if (status == Success) {
1223 XSetTextProperty(system->x11display, glx->window, &property, WM_NAME);
1224 XSetTextProperty(system->x11display, glx->window, &property, _NET_WM_NAME);
1225 XSetTextProperty(system->x11display, glx->window, &property, XA_WM_NAME);
1226 XFree(property.value);
1227 }
1228 else if (status == XLocaleNotSupported) {
1229 ALLEGRO_WARN("Couldn't set window title. Xutf8TextListToTextProperty returned XLocaleNotSupported.\n");
1230 }
1231 else {
1232 ALLEGRO_WARN("Couldn't set window title. Xutf8TextListToTextProperty returned: %d\n", status);
1233 }
1234 }
1235 {
1236 XClassHint *hint = XAllocClassHint();
1237 if (hint) {
1238 ALLEGRO_PATH *exepath = al_get_standard_path(ALLEGRO_EXENAME_PATH);
1239 // hint doesn't use a const char*, so we use strdup to create a non const string
1240 hint->res_name = strdup(al_get_path_basename(exepath));
1241 hint->res_class = strdup(al_get_path_basename(exepath));
1242 XSetClassHint(system->x11display, glx->window, hint);
1243 free(hint->res_name);
1244 free(hint->res_class);
1245 XFree(hint);
1246 al_destroy_path(exepath);
1247 }
1248 }
1249 }
1250
1251
1252 static void xdpy_set_window_title(ALLEGRO_DISPLAY *display, const char *title)
1253 {
1254 ALLEGRO_SYSTEM_XGLX *system = (ALLEGRO_SYSTEM_XGLX *)al_get_system_driver();
1255 ALLEGRO_DISPLAY_XGLX *glx = (ALLEGRO_DISPLAY_XGLX *)display;
1256
1257 _al_mutex_lock(&system->lock);
1258 glx->overridable_vt->set_window_title(display, title);
1259 _al_mutex_unlock(&system->lock);
1260 }
1261
1262
1263 // Note: we assume x/y to be the inner window position (that is drawing
1264 // to 0/0 in Allegro's drawing area will draw to x/y on the desktop)
1265 static void xdpy_set_window_position_default(ALLEGRO_DISPLAY *display,
1266 int x, int y)
1267 {
1268 ALLEGRO_DISPLAY_XGLX *glx = (ALLEGRO_DISPLAY_XGLX *)display;
1269 ALLEGRO_SYSTEM_XGLX *system = (void *)al_get_system_driver();
1270
1271 _al_mutex_lock(&system->lock);
1272
1273 // Note: a previous version of this function tried to translate the
1274 // coordinates with XTranslateCoordinates (assuming a parent window
1275 // offset by exactly the border) - but that didn't quite work in any
1276 // of the window managers I tried.
1277
1278 // XMoveWindow expects outer coordinates so we offset by the border
1279 // size.
1280 XMoveWindow(system->x11display, glx->window, x - glx->border_left,
1281 y - glx->border_top);
1282 XFlush(system->x11display);
1283
1284 /* We have to store these immediately, as we will ignore the XConfigureEvent
1285 * that we receive in response. See comments in _al_xglx_display_configure().
1286 */
1287 glx->x = x;
1288 glx->y = y;
1289
1290 _al_mutex_unlock(&system->lock);
1291 }
1292
1293
1294 static void xdpy_set_window_position(ALLEGRO_DISPLAY *display, int x, int y)
1295 {
1296 ALLEGRO_DISPLAY_XGLX *glx = (ALLEGRO_DISPLAY_XGLX *)display;
1297 glx->overridable_vt->set_window_position(display, x, y);
1298 }
1299
1300
1301 static void xdpy_get_window_position(ALLEGRO_DISPLAY *display, int *x, int *y)
1302 {
1303 ALLEGRO_DISPLAY_XGLX *glx = (ALLEGRO_DISPLAY_XGLX *)display;
1304 /* We could also query the X11 server, but it just would take longer, and
1305 * would not be synchronized to our events. The latter can be an advantage
1306 * or disadvantage.
1307 */
1308 *x = glx->x;
1309 *y = glx->y;
1310 }
1311
1312
1313 static bool xdpy_get_window_borders(ALLEGRO_DISPLAY *display, int *left, int *top, int *right, int *bottom)
1314 {
1315 ALLEGRO_DISPLAY_XGLX *glx = (ALLEGRO_DISPLAY_XGLX *)display;
1316 if (!glx->borders_known) return false;
1317 if (left) *left = glx->border_left;
1318 if (right) *right = glx->border_right;
1319 if (top) *top = glx->border_top;
1320 if (bottom) *bottom = glx->border_bottom;
1321 return true;
1322 }
1323
1324
1325 static bool xdpy_set_window_constraints_default(ALLEGRO_DISPLAY *display,
1326 int min_w, int min_h, int max_w, int max_h)
1327 {
1328 ALLEGRO_DISPLAY_XGLX *glx = (ALLEGRO_DISPLAY_XGLX *)display;
1329
1330 glx->display.min_w = min_w;
1331 glx->display.min_h = min_h;
1332 glx->display.max_w = max_w;
1333 glx->display.max_h = max_h;
1334
1335 return true;
1336 }
1337
1338
1339 static bool xdpy_set_window_constraints(ALLEGRO_DISPLAY *display,
1340 int min_w, int min_h, int max_w, int max_h)
1341 {
1342 ALLEGRO_DISPLAY_XGLX *glx = (ALLEGRO_DISPLAY_XGLX *)display;
1343 return glx->overridable_vt->set_window_constraints(display,
1344 min_w, min_h, max_w, max_h);
1345 }
1346
1347
1348 static bool xdpy_get_window_constraints(ALLEGRO_DISPLAY *display,
1349 int *min_w, int *min_h, int *max_w, int * max_h)
1350 {
1351 ALLEGRO_DISPLAY_XGLX *glx = (ALLEGRO_DISPLAY_XGLX *)display;
1352
1353 *min_w = glx->display.min_w;
1354 *min_h = glx->display.min_h;
1355 *max_w = glx->display.max_w;
1356 *max_h = glx->display.max_h;
1357
1358 return true;
1359 }
1360
1361
1362 static void xdpy_apply_window_constraints(ALLEGRO_DISPLAY *display,
1363 bool onoff)
1364 {
1365 int posX;
1366 int posY;
1367 ALLEGRO_SYSTEM_XGLX *system = (ALLEGRO_SYSTEM_XGLX *)al_get_system_driver();
1368
1369 _al_mutex_lock(&system->lock);
1370
1371 if (onoff) {
1372 al_get_window_position(display, &posX, &posY);
1373 _al_xwin_set_size_hints(display, posX, posY);
1374 }
1375 else {
1376 _al_xwin_reset_size_hints(display);
1377 }
1378
1379 _al_mutex_unlock(&system->lock);
1380 al_resize_display(display, display->w, display->h);
1381 }
1382
1383
1384 static void xdpy_set_fullscreen_window_default(ALLEGRO_DISPLAY *display, bool onoff)
1385 {
1386 if (onoff == !(display->flags & ALLEGRO_FULLSCREEN_WINDOW)) {
1387 ALLEGRO_SYSTEM_XGLX *system = (ALLEGRO_SYSTEM_XGLX *)al_get_system_driver();
1388 _al_mutex_lock(&system->lock);
1389
1390 _al_xwin_reset_size_hints(display);
1391 _al_xwin_set_fullscreen_window(display, 2);
1392 /* XXX Technically, the user may fiddle with the _NET_WM_STATE_FULLSCREEN
1393 * property outside of Allegro so this flag may not be in sync with
1394 * reality.
1395 */
1396 display->flags ^= ALLEGRO_FULLSCREEN_WINDOW;
1397 _al_xwin_set_size_hints(display, INT_MAX, INT_MAX);
1398
1399 set_compositor_bypass_flag(display);
1400
1401 _al_mutex_unlock(&system->lock);
1402 }
1403 }
1404
1405
1406 static bool xdpy_set_display_flag_default(ALLEGRO_DISPLAY *display, int flag,
1407 bool flag_onoff)
1408 {
1409 switch (flag) {
1410 case ALLEGRO_FRAMELESS:
1411 {
1412 /* The ALLEGRO_FRAMELESS flag is backwards. */
1413 _al_xwin_set_frame(display, !flag_onoff);
1414 return true;
1415 }
1416 case ALLEGRO_FULLSCREEN_WINDOW:
1417 {
1418 ALLEGRO_DISPLAY_XGLX *glx = (ALLEGRO_DISPLAY_XGLX *)display;
1419 glx->overridable_vt->set_fullscreen_window(display, flag_onoff);
1420 return true;
1421 }
1422 case ALLEGRO_MAXIMIZED:
1423 {
1424 ALLEGRO_SYSTEM_XGLX *system = (ALLEGRO_SYSTEM_XGLX *)al_get_system_driver();
1425 _al_mutex_lock(&system->lock);
1426 _al_xwin_maximize(display, flag_onoff);
1427 _al_mutex_unlock(&system->lock);
1428 return true;
1429 }
1430 }
1431 return false;
1432 }
1433
1434
1435 static bool xdpy_set_display_flag(ALLEGRO_DISPLAY *display, int flag, bool onoff)
1436 {
1437 ALLEGRO_DISPLAY_XGLX *glx = (ALLEGRO_DISPLAY_XGLX *)display;
1438 return glx->overridable_vt->set_display_flag(display, flag, onoff);
1439 }
1440
1441
1442 static bool xdpy_wait_for_vsync(ALLEGRO_DISPLAY *display)
1443 {
1444 (void) display;
1445
1446 if (al_get_opengl_extension_list()->ALLEGRO_GLX_SGI_video_sync) {
1447 unsigned int count;
1448 glXGetVideoSyncSGI(&count);
1449 glXWaitVideoSyncSGI(2, (count+1) & 1, &count);
1450 return true;
1451 }
1452
1453 return false;
1454 }
1455
1456
1457 /* Obtain a reference to this driver. */
1458 ALLEGRO_DISPLAY_INTERFACE *_al_display_xglx_driver(void)
1459 {
1460 if (xdpy_vt.create_display)
1461 return &xdpy_vt;
1462
1463 xdpy_vt.create_display = xdpy_create_display;
1464 xdpy_vt.destroy_display = xdpy_destroy_display;
1465 xdpy_vt.set_current_display = xdpy_set_current_display;
1466 xdpy_vt.unset_current_display = xdpy_unset_current_display;
1467 xdpy_vt.flip_display = xdpy_flip_display;
1468 xdpy_vt.update_display_region = xdpy_update_display_region;
1469 xdpy_vt.acknowledge_resize = xdpy_acknowledge_resize;
1470 xdpy_vt.create_bitmap = _al_ogl_create_bitmap;
1471 xdpy_vt.get_backbuffer = _al_ogl_get_backbuffer;
1472 xdpy_vt.set_target_bitmap = _al_ogl_set_target_bitmap;
1473 xdpy_vt.is_compatible_bitmap = xdpy_is_compatible_bitmap;
1474 xdpy_vt.resize_display = xdpy_resize_display;
1475 xdpy_vt.set_icons = _al_xwin_set_icons;
1476 xdpy_vt.set_window_title = xdpy_set_window_title;
1477 xdpy_vt.set_window_position = xdpy_set_window_position;
1478 xdpy_vt.get_window_position = xdpy_get_window_position;
1479 xdpy_vt.get_window_borders = xdpy_get_window_borders;
1480 xdpy_vt.set_window_constraints = xdpy_set_window_constraints;
1481 xdpy_vt.get_window_constraints = xdpy_get_window_constraints;
1482 xdpy_vt.apply_window_constraints = xdpy_apply_window_constraints;
1483 xdpy_vt.set_display_flag = xdpy_set_display_flag;
1484 xdpy_vt.wait_for_vsync = xdpy_wait_for_vsync;
1485 xdpy_vt.update_render_state = _al_ogl_update_render_state;
1486
1487 _al_xwin_add_cursor_functions(&xdpy_vt);
1488 _al_xwin_add_clipboard_functions(&xdpy_vt);
1489 _al_ogl_add_drawing_functions(&xdpy_vt);
1490
1491 return &xdpy_vt;
1492 }
1493
1494
1495 static const ALLEGRO_XWIN_DISPLAY_OVERRIDABLE_INTERFACE default_overridable_vt =
1496 {
1497 xdpy_create_display_hook_default,
1498 xdpy_destroy_display_hook_default,
1499 xdpy_resize_display_default,
1500 xdpy_set_window_title_default,
1501 xdpy_set_fullscreen_window_default,
1502 xdpy_set_window_position_default,
1503 xdpy_set_window_constraints_default,
1504 xdpy_set_display_flag_default,
1505 _al_xwin_check_maximized,
1506 };
1507
1508
1509 bool _al_xwin_set_gtk_display_overridable_interface(uint32_t check_version,
1510 const ALLEGRO_XWIN_DISPLAY_OVERRIDABLE_INTERFACE *vt)
1511 {
1512 /* The version of the native dialogs addon must exactly match the core
1513 * library version.
1514 */
1515 if (vt && check_version == ALLEGRO_VERSION_INT) {
1516 ALLEGRO_DEBUG("GTK vtable made available\n");
1517 gtk_override_vt = vt;
1518 return true;
1519 }
1520
1521 ALLEGRO_DEBUG("GTK vtable reset\n");
1522 gtk_override_vt = NULL;
1523 return (vt == NULL);
1524 }
1525
1526
1527 /* vim: set sts=3 sw=3 et: */