Debian Codegraph @

gdk/x11/gdkdisplay-x11.c

raw
1 /* GDK - The GIMP Drawing Kit
2 * gdkdisplay-x11.c
3 *
4 * Copyright 2001 Sun Microsystems Inc.
5 * Copyright (C) 2004 Nokia Corporation
6 *
7 * Erwann Chenede <[email protected]>
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Library General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Library General Public License for more details.
18 *
19 * You should have received a copy of the GNU Library General Public
20 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
21 */
22
23 #include "config.h"
24
25 #include "gdkasync.h"
26 #include "gdkdisplay.h"
27 #include "gdkeventsource.h"
28 #include "gdkeventtranslator.h"
29 #include "gdkframeclockprivate.h"
30 #include "gdkinternals.h"
31 #include "gdkscreen.h"
32 #include "gdkinternals.h"
33 #include "gdkdeviceprivate.h"
34 #include "gdkkeysprivate.h"
35 #include "gdkdevicemanager.h"
36 #include "xsettings-client.h"
37 #include "gdkdisplay-x11.h"
38 #include "gdkprivate-x11.h"
39 #include "gdkscreen-x11.h"
40 #include "gdkglcontext-x11.h"
41 #include "gdk-private.h"
42 #include "gdkprofilerprivate.h"
43
44 #include <glib.h>
45 #include <glib/gprintf.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include <errno.h>
49 #include <unistd.h>
50
51 #include <X11/Xatom.h>
52 #include <X11/Xlibint.h>
53
54 #ifdef HAVE_XKB
55 #include <X11/XKBlib.h>
56 #endif
57
58 #ifdef HAVE_XFIXES
59 #include <X11/extensions/Xfixes.h>
60 #endif
61
62 #include <X11/extensions/shape.h>
63
64 #ifdef HAVE_XCOMPOSITE
65 #include <X11/extensions/Xcomposite.h>
66 #endif
67
68 #ifdef HAVE_XDAMAGE
69 #include <X11/extensions/Xdamage.h>
70 #endif
71
72 #ifdef HAVE_RANDR
73 #include <X11/extensions/Xrandr.h>
74 #endif
75
76 typedef struct _GdkErrorTrap GdkErrorTrap;
77
78 struct _GdkErrorTrap
79 {
80 /* Next sequence when trap was pushed, i.e. first sequence to
81 * ignore
82 */
83 gulong start_sequence;
84
85 /* Next sequence when trap was popped, i.e. first sequence
86 * to not ignore. 0 if trap is still active.
87 */
88 gulong end_sequence;
89
90 /* Most recent error code within the sequence */
91 int error_code;
92 };
93
94 static void gdk_x11_display_dispose (GObject *object);
95 static void gdk_x11_display_finalize (GObject *object);
96
97 static void gdk_x11_display_event_translator_init (GdkEventTranslatorIface *iface);
98
99 static gboolean gdk_x11_display_translate_event (GdkEventTranslator *translator,
100 GdkDisplay *display,
101 GdkEvent *event,
102 XEvent *xevent);
103
104 static void gdk_internal_connection_watch (Display *display,
105 XPointer arg,
106 gint fd,
107 gboolean opening,
108 XPointer *watch_data);
109
110 typedef struct _GdkEventTypeX11 GdkEventTypeX11;
111
112 struct _GdkEventTypeX11
113 {
114 gint base;
115 gint n_events;
116 };
117
118 /* Note that we never *directly* use WM_LOCALE_NAME, WM_PROTOCOLS,
119 * but including them here has the side-effect of getting them
120 * into the internal Xlib cache
121 */
122 static const char *const precache_atoms[] = {
123 "UTF8_STRING",
124 "WM_CLIENT_LEADER",
125 "WM_DELETE_WINDOW",
126 "WM_ICON_NAME",
127 "WM_LOCALE_NAME",
128 "WM_NAME",
129 "WM_PROTOCOLS",
130 "WM_TAKE_FOCUS",
131 "WM_WINDOW_ROLE",
132 "_NET_ACTIVE_WINDOW",
133 "_NET_CURRENT_DESKTOP",
134 "_NET_FRAME_EXTENTS",
135 "_NET_STARTUP_ID",
136 "_NET_WM_CM_S0",
137 "_NET_WM_DESKTOP",
138 "_NET_WM_ICON",
139 "_NET_WM_ICON_NAME",
140 "_NET_WM_NAME",
141 "_NET_WM_PID",
142 "_NET_WM_PING",
143 "_NET_WM_STATE",
144 "_NET_WM_STATE_ABOVE",
145 "_NET_WM_STATE_BELOW",
146 "_NET_WM_STATE_FULLSCREEN",
147 "_NET_WM_STATE_HIDDEN",
148 "_NET_WM_STATE_MODAL",
149 "_NET_WM_STATE_MAXIMIZED_VERT",
150 "_NET_WM_STATE_MAXIMIZED_HORZ",
151 "_NET_WM_STATE_SKIP_TASKBAR",
152 "_NET_WM_STATE_SKIP_PAGER",
153 "_NET_WM_STATE_STICKY",
154 "_NET_WM_SYNC_REQUEST",
155 "_NET_WM_SYNC_REQUEST_COUNTER",
156 "_NET_WM_WINDOW_TYPE",
157 "_NET_WM_WINDOW_TYPE_COMBO",
158 "_NET_WM_WINDOW_TYPE_DIALOG",
159 "_NET_WM_WINDOW_TYPE_DND",
160 "_NET_WM_WINDOW_TYPE_DROPDOWN_MENU",
161 "_NET_WM_WINDOW_TYPE_MENU",
162 "_NET_WM_WINDOW_TYPE_NORMAL",
163 "_NET_WM_WINDOW_TYPE_POPUP_MENU",
164 "_NET_WM_WINDOW_TYPE_TOOLTIP",
165 "_NET_WM_WINDOW_TYPE_UTILITY",
166 "_NET_WM_USER_TIME",
167 "_NET_WM_USER_TIME_WINDOW",
168 "_NET_VIRTUAL_ROOTS",
169 "GDK_SELECTION",
170 "_NET_WM_STATE_FOCUSED",
171 "GDK_VISUALS"
172 };
173
174 static char *gdk_sm_client_id;
175
176 G_DEFINE_TYPE_WITH_CODE (GdkX11Display, gdk_x11_display, GDK_TYPE_DISPLAY,
177 G_IMPLEMENT_INTERFACE (GDK_TYPE_EVENT_TRANSLATOR,
178 gdk_x11_display_event_translator_init))
179
180
181 static void
182 gdk_x11_display_init (GdkX11Display *display)
183 {
184 display->monitors = g_ptr_array_new_with_free_func (g_object_unref);
185 }
186
187 static void
188 gdk_x11_display_event_translator_init (GdkEventTranslatorIface *iface)
189 {
190 iface->translate_event = gdk_x11_display_translate_event;
191 }
192
193 #define ANY_EDGE_TILED (GDK_WINDOW_STATE_LEFT_TILED | \
194 GDK_WINDOW_STATE_RIGHT_TILED | \
195 GDK_WINDOW_STATE_TOP_TILED | \
196 GDK_WINDOW_STATE_BOTTOM_TILED)
197
198 static void
199 do_edge_constraint_state_check (GdkWindow *window,
200 GdkWindowState old_state,
201 GdkWindowState *set,
202 GdkWindowState *unset)
203 {
204 GdkToplevelX11 *toplevel = _gdk_x11_window_get_toplevel (window);
205 GdkWindowState local_set, local_unset;
206 GdkScreen *screen = GDK_WINDOW_SCREEN (window);
207 guint edge_constraints;
208
209 local_set = *set;
210 local_unset = *unset;
211 edge_constraints = toplevel->edge_constraints;
212
213 /* If the WM doesn't support _GTK_EDGE_CONSTRAINTS, rely on the fallback
214 * implementation. If it supports _GTK_EDGE_CONSTRAINTS, arrange for
215 * GDK_WINDOW_STATE_TILED to be set if any edge is tiled, and cleared
216 * if no edge is tiled.
217 */
218 if (!gdk_x11_screen_supports_net_wm_hint (screen,
219 gdk_atom_intern_static_string ("_GTK_EDGE_CONSTRAINTS")))
220 {
221 /* FIXME: we rely on implementation details of mutter here:
222 * mutter only tiles horizontally, and sets maxvert when it does
223 * and if it tiles, it always affects all edges
224 */
225 if (old_state & GDK_WINDOW_STATE_TILED)
226 {
227 if (!toplevel->have_maxvert)
228 local_unset |= GDK_WINDOW_STATE_TILED;
229 }
230 else
231 {
232 if (toplevel->have_maxvert && !toplevel->have_maxhorz)
233 local_set |= GDK_WINDOW_STATE_TILED;
234 }
235 }
236 else
237 {
238 if (old_state & GDK_WINDOW_STATE_TILED)
239 {
240 if (!(edge_constraints & ANY_EDGE_TILED))
241 local_unset |= GDK_WINDOW_STATE_TILED;
242 }
243 else
244 {
245 if (edge_constraints & ANY_EDGE_TILED)
246 local_set |= GDK_WINDOW_STATE_TILED;
247 }
248 }
249
250 /* Top edge */
251 if (old_state & GDK_WINDOW_STATE_TOP_TILED)
252 {
253 if ((edge_constraints & GDK_WINDOW_STATE_TOP_TILED) == 0)
254 local_unset |= GDK_WINDOW_STATE_TOP_TILED;
255 }
256 else
257 {
258 if (edge_constraints & GDK_WINDOW_STATE_TOP_TILED)
259 local_set |= GDK_WINDOW_STATE_TOP_TILED;
260 }
261
262 if (old_state & GDK_WINDOW_STATE_TOP_RESIZABLE)
263 {
264 if ((edge_constraints & GDK_WINDOW_STATE_TOP_RESIZABLE) == 0)
265 local_unset |= GDK_WINDOW_STATE_TOP_RESIZABLE;
266 }
267 else
268 {
269 if (edge_constraints & GDK_WINDOW_STATE_TOP_RESIZABLE)
270 local_set |= GDK_WINDOW_STATE_TOP_RESIZABLE;
271 }
272
273 /* Right edge */
274 if (old_state & GDK_WINDOW_STATE_RIGHT_TILED)
275 {
276 if ((edge_constraints & GDK_WINDOW_STATE_RIGHT_TILED) == 0)
277 local_unset |= GDK_WINDOW_STATE_RIGHT_TILED;
278 }
279 else
280 {
281 if (edge_constraints & GDK_WINDOW_STATE_RIGHT_TILED)
282 local_set |= GDK_WINDOW_STATE_RIGHT_TILED;
283 }
284
285 if (old_state & GDK_WINDOW_STATE_RIGHT_RESIZABLE)
286 {
287 if ((edge_constraints & GDK_WINDOW_STATE_RIGHT_RESIZABLE) == 0)
288 local_unset |= GDK_WINDOW_STATE_RIGHT_RESIZABLE;
289 }
290 else
291 {
292 if (edge_constraints & GDK_WINDOW_STATE_RIGHT_RESIZABLE)
293 local_set |= GDK_WINDOW_STATE_RIGHT_RESIZABLE;
294 }
295
296 /* Bottom edge */
297 if (old_state & GDK_WINDOW_STATE_BOTTOM_TILED)
298 {
299 if ((edge_constraints & GDK_WINDOW_STATE_BOTTOM_TILED) == 0)
300 local_unset |= GDK_WINDOW_STATE_BOTTOM_TILED;
301 }
302 else
303 {
304 if (edge_constraints & GDK_WINDOW_STATE_BOTTOM_TILED)
305 local_set |= GDK_WINDOW_STATE_BOTTOM_TILED;
306 }
307
308 if (old_state & GDK_WINDOW_STATE_BOTTOM_RESIZABLE)
309 {
310 if ((edge_constraints & GDK_WINDOW_STATE_BOTTOM_RESIZABLE) == 0)
311 local_unset |= GDK_WINDOW_STATE_BOTTOM_RESIZABLE;
312 }
313 else
314 {
315 if (edge_constraints & GDK_WINDOW_STATE_BOTTOM_RESIZABLE)
316 local_set |= GDK_WINDOW_STATE_BOTTOM_RESIZABLE;
317 }
318
319 /* Left edge */
320 if (old_state & GDK_WINDOW_STATE_LEFT_TILED)
321 {
322 if ((edge_constraints & GDK_WINDOW_STATE_LEFT_TILED) == 0)
323 local_unset |= GDK_WINDOW_STATE_LEFT_TILED;
324 }
325 else
326 {
327 if (edge_constraints & GDK_WINDOW_STATE_LEFT_TILED)
328 local_set |= GDK_WINDOW_STATE_LEFT_TILED;
329 }
330
331 if (old_state & GDK_WINDOW_STATE_LEFT_RESIZABLE)
332 {
333 if ((edge_constraints & GDK_WINDOW_STATE_LEFT_RESIZABLE) == 0)
334 local_unset |= GDK_WINDOW_STATE_LEFT_RESIZABLE;
335 }
336 else
337 {
338 if (edge_constraints & GDK_WINDOW_STATE_LEFT_RESIZABLE)
339 local_set |= GDK_WINDOW_STATE_LEFT_RESIZABLE;
340 }
341
342 *set = local_set;
343 *unset = local_unset;
344 }
345
346 static void
347 do_net_wm_state_changes (GdkWindow *window)
348 {
349 GdkToplevelX11 *toplevel = _gdk_x11_window_get_toplevel (window);
350 GdkWindowState old_state, set, unset;
351
352 if (GDK_WINDOW_DESTROYED (window) ||
353 gdk_window_get_window_type (window) != GDK_WINDOW_TOPLEVEL)
354 return;
355
356 old_state = gdk_window_get_state (window);
357
358 set = unset = 0;
359
360 /* For found_sticky to remain TRUE, we have to also be on desktop
361 * 0xFFFFFFFF
362 */
363 if (old_state & GDK_WINDOW_STATE_STICKY)
364 {
365 if (!(toplevel->have_sticky && toplevel->on_all_desktops))
366 unset |= GDK_WINDOW_STATE_STICKY;
367 }
368 else
369 {
370 if (toplevel->have_sticky && toplevel->on_all_desktops)
371 set |= GDK_WINDOW_STATE_STICKY;
372 }
373
374 if (old_state & GDK_WINDOW_STATE_FULLSCREEN)
375 {
376 if (!toplevel->have_fullscreen)
377 unset |= GDK_WINDOW_STATE_FULLSCREEN;
378 }
379 else
380 {
381 if (toplevel->have_fullscreen)
382 set |= GDK_WINDOW_STATE_FULLSCREEN;
383 }
384
385 /* Our "maximized" means both vertical and horizontal; if only one,
386 * we don't expose that via GDK
387 */
388 if (old_state & GDK_WINDOW_STATE_MAXIMIZED)
389 {
390 if (!(toplevel->have_maxvert && toplevel->have_maxhorz))
391 unset |= GDK_WINDOW_STATE_MAXIMIZED;
392 }
393 else
394 {
395 if (toplevel->have_maxvert && toplevel->have_maxhorz)
396 set |= GDK_WINDOW_STATE_MAXIMIZED;
397 }
398
399 if (old_state & GDK_WINDOW_STATE_FOCUSED)
400 {
401 if (!toplevel->have_focused)
402 unset |= GDK_WINDOW_STATE_FOCUSED;
403 }
404 else
405 {
406 if (toplevel->have_focused)
407 set |= GDK_WINDOW_STATE_FOCUSED;
408 }
409
410 if (old_state & GDK_WINDOW_STATE_ICONIFIED)
411 {
412 if (!toplevel->have_hidden)
413 unset |= GDK_WINDOW_STATE_ICONIFIED;
414 }
415 else
416 {
417 if (toplevel->have_hidden)
418 set |= GDK_WINDOW_STATE_ICONIFIED;
419 }
420
421 /* Update edge constraints and tiling */
422 do_edge_constraint_state_check (window, old_state, &set, &unset);
423
424 gdk_synthesize_window_state (window, unset, set);
425 }
426
427 static void
428 gdk_check_wm_desktop_changed (GdkWindow *window)
429 {
430 GdkToplevelX11 *toplevel = _gdk_x11_window_get_toplevel (window);
431 GdkDisplay *display = GDK_WINDOW_DISPLAY (window);
432
433 Atom type;
434 gint format;
435 gulong nitems;
436 gulong bytes_after;
437 guchar *data;
438 gulong *desktop;
439
440 type = None;
441 gdk_x11_display_error_trap_push (display);
442 XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display),
443 GDK_WINDOW_XID (window),
444 gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_DESKTOP"),
445 0, G_MAXLONG, False, XA_CARDINAL, &type,
446 &format, &nitems,
447 &bytes_after, &data);
448 gdk_x11_display_error_trap_pop_ignored (display);
449
450 if (type != None)
451 {
452 desktop = (gulong *)data;
453 toplevel->on_all_desktops = ((*desktop & 0xFFFFFFFF) == 0xFFFFFFFF);
454 XFree (desktop);
455 }
456 else
457 toplevel->on_all_desktops = FALSE;
458
459 do_net_wm_state_changes (window);
460 }
461
462 static void
463 gdk_check_wm_state_changed (GdkWindow *window)
464 {
465 GdkToplevelX11 *toplevel = _gdk_x11_window_get_toplevel (window);
466 GdkDisplay *display = GDK_WINDOW_DISPLAY (window);
467 GdkScreen *screen = GDK_WINDOW_SCREEN (window);
468
469 Atom type;
470 gint format;
471 gulong nitems;
472 gulong bytes_after;
473 guchar *data;
474 Atom *atoms = NULL;
475 gulong i;
476
477 gboolean had_sticky = toplevel->have_sticky;
478
479 toplevel->have_sticky = FALSE;
480 toplevel->have_maxvert = FALSE;
481 toplevel->have_maxhorz = FALSE;
482 toplevel->have_fullscreen = FALSE;
483 toplevel->have_focused = FALSE;
484 toplevel->have_hidden = FALSE;
485
486 type = None;
487 gdk_x11_display_error_trap_push (display);
488 XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), GDK_WINDOW_XID (window),
489 gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_STATE"),
490 0, G_MAXLONG, False, XA_ATOM, &type, &format, &nitems,
491 &bytes_after, &data);
492 gdk_x11_display_error_trap_pop_ignored (display);
493
494 if (type != None)
495 {
496 Atom sticky_atom = gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_STATE_STICKY");
497 Atom maxvert_atom = gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_STATE_MAXIMIZED_VERT");
498 Atom maxhorz_atom = gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_STATE_MAXIMIZED_HORZ");
499 Atom fullscreen_atom = gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_STATE_FULLSCREEN");
500 Atom focused_atom = gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_STATE_FOCUSED");
501 Atom hidden_atom = gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_STATE_HIDDEN");
502
503 atoms = (Atom *)data;
504
505 i = 0;
506 while (i < nitems)
507 {
508 if (atoms[i] == sticky_atom)
509 toplevel->have_sticky = TRUE;
510 else if (atoms[i] == maxvert_atom)
511 toplevel->have_maxvert = TRUE;
512 else if (atoms[i] == maxhorz_atom)
513 toplevel->have_maxhorz = TRUE;
514 else if (atoms[i] == fullscreen_atom)
515 toplevel->have_fullscreen = TRUE;
516 else if (atoms[i] == focused_atom)
517 toplevel->have_focused = TRUE;
518 else if (atoms[i] == hidden_atom)
519 toplevel->have_hidden = TRUE;
520
521 ++i;
522 }
523
524 XFree (atoms);
525 }
526
527 if (!gdk_x11_screen_supports_net_wm_hint (screen,
528 gdk_atom_intern_static_string ("_NET_WM_STATE_FOCUSED")))
529 toplevel->have_focused = TRUE;
530
531 /* When have_sticky is turned on, we have to check the DESKTOP property
532 * as well.
533 */
534 if (toplevel->have_sticky && !had_sticky)
535 gdk_check_wm_desktop_changed (window);
536 else
537 do_net_wm_state_changes (window);
538 }
539
540 static void
541 gdk_check_edge_constraints_changed (GdkWindow *window)
542 {
543 GdkToplevelX11 *toplevel = _gdk_x11_window_get_toplevel (window);
544 GdkDisplay *display = GDK_WINDOW_DISPLAY (window);
545
546 Atom type;
547 gint format;
548 gulong nitems;
549 gulong bytes_after;
550 guchar *data;
551 gulong *constraints;
552
553 type = None;
554 gdk_x11_display_error_trap_push (display);
555 XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display),
556 GDK_WINDOW_XID (window),
557 gdk_x11_get_xatom_by_name_for_display (display, "_GTK_EDGE_CONSTRAINTS"),
558 0, G_MAXLONG, False, XA_CARDINAL, &type,
559 &format, &nitems,
560 &bytes_after, &data);
561 gdk_x11_display_error_trap_pop_ignored (display);
562
563 if (type != None)
564 {
565 constraints = (gulong *)data;
566
567 /* The GDK enum for these states does not begin at zero so, to avoid
568 * messing around with shifts, just make the passed value and GDK's
569 * enum values match by shifting to the first tiled state.
570 */
571 toplevel->edge_constraints = constraints[0] << 9;
572
573 XFree (constraints);
574 }
575 else
576 {
577 toplevel->edge_constraints = 0;
578 }
579
580 do_net_wm_state_changes (window);
581 }
582
583 static Window
584 get_event_xwindow (XEvent *xevent)
585 {
586 Window xwindow;
587
588 switch (xevent->type)
589 {
590 case DestroyNotify:
591 xwindow = xevent->xdestroywindow.window;
592 break;
593 case UnmapNotify:
594 xwindow = xevent->xunmap.window;
595 break;
596 case MapNotify:
597 xwindow = xevent->xmap.window;
598 break;
599 case ConfigureNotify:
600 xwindow = xevent->xconfigure.window;
601 break;
602 case ReparentNotify:
603 xwindow = xevent->xreparent.window;
604 break;
605 case GravityNotify:
606 xwindow = xevent->xgravity.window;
607 break;
608 case CirculateNotify:
609 xwindow = xevent->xcirculate.window;
610 break;
611 default:
612 xwindow = xevent->xany.window;
613 }
614
615 return xwindow;
616 }
617
618 static gboolean
619 gdk_x11_display_translate_event (GdkEventTranslator *translator,
620 GdkDisplay *display,
621 GdkEvent *event,
622 XEvent *xevent)
623 {
624 Window xwindow;
625 GdkWindow *window;
626 gboolean is_substructure;
627 GdkWindowImplX11 *window_impl = NULL;
628 GdkScreen *screen = NULL;
629 GdkX11Screen *x11_screen = NULL;
630 GdkToplevelX11 *toplevel = NULL;
631 GdkX11Display *display_x11 = GDK_X11_DISPLAY (display);
632 gboolean return_val;
633
634 /* Find the GdkWindow that this event relates to. If that's
635 * not the same as the window that the event was sent to,
636 * we are getting an event from SubstructureNotifyMask.
637 * We ignore such events for internal operation, but we
638 * need to report them to the application because of
639 * GDK_SUBSTRUCTURE_MASK (which should be removed at next
640 * opportunity.) The most likely reason for getting these
641 * events is when we are used in the Metacity or Mutter
642 * window managers.
643 */
644 xwindow = get_event_xwindow (xevent);
645 is_substructure = xwindow != xevent->xany.window;
646
647 window = gdk_x11_window_lookup_for_display (display, xwindow);
648 if (window)
649 {
650 /* We may receive events such as NoExpose/GraphicsExpose
651 * and ShmCompletion for pixmaps
652 */
653 if (!GDK_IS_WINDOW (window))
654 return FALSE;
655
656 screen = GDK_WINDOW_SCREEN (window);
657 x11_screen = GDK_X11_SCREEN (screen);
658 toplevel = _gdk_x11_window_get_toplevel (window);
659 window_impl = GDK_WINDOW_IMPL_X11 (window->impl);
660
661 g_object_ref (window);
662 }
663
664 event->any.window = window;
665 event->any.send_event = xevent->xany.send_event ? TRUE : FALSE;
666
667 if (window && GDK_WINDOW_DESTROYED (window))
668 {
669 if (xevent->type != DestroyNotify)
670 {
671 return_val = FALSE;
672 goto done;
673 }
674 }
675
676 if (xevent->type == DestroyNotify && !is_substructure)
677 {
678 screen = GDK_X11_DISPLAY (display)->screen;
679 x11_screen = GDK_X11_SCREEN (screen);
680
681 if (x11_screen->wmspec_check_window == xevent->xdestroywindow.window)
682 {
683 x11_screen->wmspec_check_window = None;
684 x11_screen->last_wmspec_check_time = 0;
685 g_free (x11_screen->window_manager_name);
686 x11_screen->window_manager_name = g_strdup ("unknown");
687
688 /* careful, reentrancy */
689 _gdk_x11_screen_window_manager_changed (screen);
690
691 return_val = FALSE;
692 goto done;
693 }
694 }
695
696 /* We do a "manual" conversion of the XEvent to a
697 * GdkEvent. The structures are mostly the same so
698 * the conversion is fairly straightforward. We also
699 * optionally print debugging info regarding events
700 * received.
701 */
702
703 return_val = TRUE;
704
705 switch (xevent->type)
706 {
707 case KeymapNotify:
708 GDK_NOTE (EVENTS,
709 g_message ("keymap notify"));
710
711 /* Not currently handled */
712 return_val = FALSE;
713 break;
714
715 case Expose:
716 GDK_NOTE (EVENTS,
717 g_message ("expose:\t\twindow: %ld %d x,y: %d %d w,h: %d %d%s",
718 xevent->xexpose.window, xevent->xexpose.count,
719 xevent->xexpose.x, xevent->xexpose.y,
720 xevent->xexpose.width, xevent->xexpose.height,
721 event->any.send_event ? " (send)" : ""));
722
723 if (window == NULL)
724 {
725 return_val = FALSE;
726 break;
727 }
728
729 {
730 GdkRectangle expose_rect;
731 int x2, y2;
732
733 expose_rect.x = xevent->xexpose.x / window_impl->window_scale;
734 expose_rect.y = xevent->xexpose.y / window_impl->window_scale;
735
736 x2 = (xevent->xexpose.x + xevent->xexpose.width + window_impl->window_scale -1) / window_impl->window_scale;
737 expose_rect.width = x2 - expose_rect.x;
738
739 y2 = (xevent->xexpose.y + xevent->xexpose.height + window_impl->window_scale -1) / window_impl->window_scale;
740 expose_rect.height = y2 - expose_rect.y;
741
742 _gdk_x11_window_process_expose (window, xevent->xexpose.serial, &expose_rect);
743 return_val = FALSE;
744 }
745
746 break;
747
748 case GraphicsExpose:
749 {
750 GdkRectangle expose_rect;
751 int x2, y2;
752
753 GDK_NOTE (EVENTS,
754 g_message ("graphics expose:\tdrawable: %ld",
755 xevent->xgraphicsexpose.drawable));
756
757 if (window == NULL)
758 {
759 return_val = FALSE;
760 break;
761 }
762
763 expose_rect.x = xevent->xgraphicsexpose.x / window_impl->window_scale;
764 expose_rect.y = xevent->xgraphicsexpose.y / window_impl->window_scale;
765
766 x2 = (xevent->xgraphicsexpose.x + xevent->xgraphicsexpose.width + window_impl->window_scale -1) / window_impl->window_scale;
767 expose_rect.width = x2 - expose_rect.x;
768
769 y2 = (xevent->xgraphicsexpose.y + xevent->xgraphicsexpose.height + window_impl->window_scale -1) / window_impl->window_scale;
770 expose_rect.height = y2 - expose_rect.y;
771
772 _gdk_x11_window_process_expose (window, xevent->xgraphicsexpose.serial, &expose_rect);
773 return_val = FALSE;
774 }
775 break;
776
777 case VisibilityNotify:
778 #ifdef G_ENABLE_DEBUG
779 if (GDK_DEBUG_CHECK (EVENTS))
780 switch (xevent->xvisibility.state)
781 {
782 case VisibilityFullyObscured:
783 g_message ("visibility notify:\twindow: %ld none",
784 xevent->xvisibility.window);
785 break;
786 case VisibilityPartiallyObscured:
787 g_message ("visibility notify:\twindow: %ld partial",
788 xevent->xvisibility.window);
789 break;
790 case VisibilityUnobscured:
791 g_message ("visibility notify:\twindow: %ld full",
792 xevent->xvisibility.window);
793 break;
794 }
795 #endif /* G_ENABLE_DEBUG */
796
797 if (window == NULL)
798 {
799 return_val = FALSE;
800 break;
801 }
802
803 event->visibility.type = GDK_VISIBILITY_NOTIFY;
804 event->visibility.window = window;
805
806 switch (xevent->xvisibility.state)
807 {
808 case VisibilityFullyObscured:
809 event->visibility.state = GDK_VISIBILITY_FULLY_OBSCURED;
810 break;
811
812 case VisibilityPartiallyObscured:
813 event->visibility.state = GDK_VISIBILITY_PARTIAL;
814 break;
815
816 case VisibilityUnobscured:
817 event->visibility.state = GDK_VISIBILITY_UNOBSCURED;
818 break;
819 }
820
821 break;
822
823 case CreateNotify:
824 GDK_NOTE (EVENTS,
825 g_message ("create notify:\twindow: %ld x,y: %d %d w,h: %d %d b-w: %d parent: %ld ovr: %d",
826 xevent->xcreatewindow.window,
827 xevent->xcreatewindow.x,
828 xevent->xcreatewindow.y,
829 xevent->xcreatewindow.width,
830 xevent->xcreatewindow.height,
831 xevent->xcreatewindow.border_width,
832 xevent->xcreatewindow.parent,
833 xevent->xcreatewindow.override_redirect));
834 /* not really handled */
835 break;
836
837 case DestroyNotify:
838 GDK_NOTE (EVENTS,
839 g_message ("destroy notify:\twindow: %ld",
840 xevent->xdestroywindow.window));
841
842 if (!is_substructure)
843 {
844 event->any.type = GDK_DESTROY;
845 event->any.window = window;
846
847 return_val = window && !GDK_WINDOW_DESTROYED (window);
848
849 if (window && GDK_WINDOW_XID (window) != x11_screen->xroot_window)
850 gdk_window_destroy_notify (window);
851 }
852 else
853 return_val = FALSE;
854
855 break;
856
857 case UnmapNotify:
858 GDK_NOTE (EVENTS,
859 g_message ("unmap notify:\t\twindow: %ld",
860 xevent->xmap.window));
861
862 event->any.type = GDK_UNMAP;
863 event->any.window = window;
864
865 if (window && !is_substructure)
866 {
867 /* If the WM supports the _NET_WM_STATE_HIDDEN hint, we do not want to
868 * interpret UnmapNotify events as implying iconic state.
869 * http://bugzilla.gnome.org/show_bug.cgi?id=590726.
870 */
871 if (screen &&
872 !gdk_x11_screen_supports_net_wm_hint (screen,
873 gdk_atom_intern_static_string ("_NET_WM_STATE_HIDDEN")))
874 {
875 /* If we are shown (not withdrawn) and get an unmap, it means we were
876 * iconified in the X sense. If we are withdrawn, and get an unmap, it
877 * means we hid the window ourselves, so we will have already flipped
878 * the iconified bit off.
879 */
880 if (GDK_WINDOW_IS_MAPPED (window))
881 gdk_synthesize_window_state (window,
882 0,
883 GDK_WINDOW_STATE_ICONIFIED);
884 }
885
886 if (window_impl->toplevel &&
887 window_impl->toplevel->frame_pending)
888 {
889 window_impl->toplevel->frame_pending = FALSE;
890 _gdk_frame_clock_thaw (gdk_window_get_frame_clock (event->any.window));
891 }
892
893 if (toplevel)
894 gdk_window_freeze_toplevel_updates (window);
895
896 _gdk_x11_window_grab_check_unmap (window, xevent->xany.serial);
897 }
898
899 break;
900
901 case MapNotify:
902 GDK_NOTE (EVENTS,
903 g_message ("map notify:\t\twindow: %ld",
904 xevent->xmap.window));
905
906 event->any.type = GDK_MAP;
907 event->any.window = window;
908
909 if (window && !is_substructure)
910 {
911 /* Unset iconified if it was set */
912 if (window->state & GDK_WINDOW_STATE_ICONIFIED)
913 gdk_synthesize_window_state (window,
914 GDK_WINDOW_STATE_ICONIFIED,
915 0);
916
917 if (toplevel)
918 gdk_window_thaw_toplevel_updates (window);
919 }
920
921 break;
922
923 case ReparentNotify:
924 GDK_NOTE (EVENTS,
925 g_message ("reparent notify:\twindow: %ld x,y: %d %d parent: %ld ovr: %d",
926 xevent->xreparent.window,
927 xevent->xreparent.x,
928 xevent->xreparent.y,
929 xevent->xreparent.parent,
930 xevent->xreparent.override_redirect));
931
932 /* Not currently handled */
933 return_val = FALSE;
934 break;
935
936 case ConfigureNotify:
937 GDK_NOTE (EVENTS,
938 g_message ("configure notify:\twindow: %ld x,y: %d %d w,h: %d %d b-w: %d above: %ld ovr: %d%s",
939 xevent->xconfigure.window,
940 xevent->xconfigure.x,
941 xevent->xconfigure.y,
942 xevent->xconfigure.width,
943 xevent->xconfigure.height,
944 xevent->xconfigure.border_width,
945 xevent->xconfigure.above,
946 xevent->xconfigure.override_redirect,
947 !window
948 ? " (discarding)"
949 : window->window_type == GDK_WINDOW_CHILD
950 ? " (discarding child)"
951 : xevent->xconfigure.event != xevent->xconfigure.window
952 ? " (discarding substructure)"
953 : ""));
954 if (window && GDK_WINDOW_TYPE (window) == GDK_WINDOW_ROOT)
955 {
956 window_impl->unscaled_width = xevent->xconfigure.width;
957 window_impl->unscaled_height = xevent->xconfigure.height;
958 window->width = (xevent->xconfigure.width + window_impl->window_scale - 1) / window_impl->window_scale;
959 window->height = (xevent->xconfigure.height + window_impl->window_scale - 1) / window_impl->window_scale;
960
961 _gdk_window_update_size (window);
962 _gdk_x11_window_update_size (window_impl);
963 _gdk_x11_screen_size_changed (screen, xevent);
964 }
965
966 #ifdef HAVE_XSYNC
967 if (!is_substructure && toplevel && display_x11->use_sync && toplevel->pending_counter_value != 0)
968 {
969 toplevel->configure_counter_value = toplevel->pending_counter_value;
970 toplevel->configure_counter_value_is_extended = toplevel->pending_counter_value_is_extended;
971 toplevel->pending_counter_value = 0;
972 }
973 #endif
974
975 if (!window ||
976 xevent->xconfigure.event != xevent->xconfigure.window ||
977 GDK_WINDOW_TYPE (window) == GDK_WINDOW_CHILD ||
978 GDK_WINDOW_TYPE (window) == GDK_WINDOW_ROOT)
979 return_val = FALSE;
980 else
981 {
982 event->configure.type = GDK_CONFIGURE;
983 event->configure.window = window;
984 event->configure.width = (xevent->xconfigure.width + window_impl->window_scale - 1) / window_impl->window_scale;
985 event->configure.height = (xevent->xconfigure.height + window_impl->window_scale - 1) / window_impl->window_scale;
986
987 if (!xevent->xconfigure.send_event &&
988 !xevent->xconfigure.override_redirect &&
989 !GDK_WINDOW_DESTROYED (window))
990 {
991 gint tx = 0;
992 gint ty = 0;
993 Window child_window = 0;
994
995 gdk_x11_display_error_trap_push (display);
996 if (XTranslateCoordinates (GDK_WINDOW_XDISPLAY (window),
997 GDK_WINDOW_XID (window),
998 x11_screen->xroot_window,
999 0, 0,
1000 &tx, &ty,
1001 &child_window))
1002 {
1003 event->configure.x = tx / window_impl->window_scale;
1004 event->configure.y = ty / window_impl->window_scale;
1005 }
1006 gdk_x11_display_error_trap_pop_ignored (display);
1007 }
1008 else
1009 {
1010 event->configure.x = xevent->xconfigure.x / window_impl->window_scale;
1011 event->configure.y = xevent->xconfigure.y / window_impl->window_scale;
1012 }
1013 if (!is_substructure)
1014 {
1015 window->x = event->configure.x;
1016 window->y = event->configure.y;
1017
1018 if (window_impl->unscaled_width != xevent->xconfigure.width ||
1019 window_impl->unscaled_height != xevent->xconfigure.height)
1020 {
1021 window_impl->unscaled_width = xevent->xconfigure.width;
1022 window_impl->unscaled_height = xevent->xconfigure.height;
1023 window->width = event->configure.width;
1024 window->height = event->configure.height;
1025
1026 _gdk_window_update_size (window);
1027 _gdk_x11_window_update_size (window_impl);
1028 }
1029
1030 if (window->resize_count >= 1)
1031 {
1032 window->resize_count -= 1;
1033
1034 if (window->resize_count == 0)
1035 _gdk_x11_moveresize_configure_done (display, window);
1036 }
1037 }
1038 }
1039 break;
1040
1041 case PropertyNotify:
1042 GDK_NOTE (EVENTS,
1043 g_message ("property notify:\twindow: %ld, atom(%ld): %s%s%s",
1044 xevent->xproperty.window,
1045 xevent->xproperty.atom,
1046 "\"",
1047 gdk_x11_get_xatom_name_for_display (display, xevent->xproperty.atom),
1048 "\""));
1049
1050 if (window == NULL)
1051 {
1052 return_val = FALSE;
1053 break;
1054 }
1055
1056 /* We compare with the serial of the last time we mapped the
1057 * window to avoid refetching properties that we set ourselves
1058 */
1059 if (toplevel &&
1060 xevent->xproperty.serial >= toplevel->map_serial)
1061 {
1062 if (xevent->xproperty.atom == gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_STATE"))
1063 gdk_check_wm_state_changed (window);
1064
1065 if (xevent->xproperty.atom == gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_DESKTOP"))
1066 gdk_check_wm_desktop_changed (window);
1067
1068 if (xevent->xproperty.atom == gdk_x11_get_xatom_by_name_for_display (display, "_GTK_EDGE_CONSTRAINTS"))
1069 gdk_check_edge_constraints_changed (window);
1070 }
1071
1072 if (window->event_mask & GDK_PROPERTY_CHANGE_MASK)
1073 {
1074 event->property.type = GDK_PROPERTY_NOTIFY;
1075 event->property.window = window;
1076 event->property.atom = gdk_x11_xatom_to_atom_for_display (display, xevent->xproperty.atom);
1077 event->property.time = xevent->xproperty.time;
1078 event->property.state = xevent->xproperty.state;
1079 }
1080 else
1081 return_val = FALSE;
1082
1083 break;
1084
1085 case SelectionClear:
1086 GDK_NOTE (EVENTS,
1087 g_message ("selection clear:\twindow: %ld",
1088 xevent->xproperty.window));
1089
1090 if (_gdk_x11_selection_filter_clear_event (&xevent->xselectionclear))
1091 {
1092 event->selection.type = GDK_SELECTION_CLEAR;
1093 event->selection.window = window;
1094 event->selection.selection = gdk_x11_xatom_to_atom_for_display (display, xevent->xselectionclear.selection);
1095 event->selection.time = xevent->xselectionclear.time;
1096 }
1097 else
1098 return_val = FALSE;
1099
1100 break;
1101
1102 case SelectionRequest:
1103 GDK_NOTE (EVENTS,
1104 g_message ("selection request:\twindow: %ld",
1105 xevent->xproperty.window));
1106
1107 event->selection.type = GDK_SELECTION_REQUEST;
1108 event->selection.window = window;
1109 event->selection.selection = gdk_x11_xatom_to_atom_for_display (display, xevent->xselectionrequest.selection);
1110 event->selection.target = gdk_x11_xatom_to_atom_for_display (display, xevent->xselectionrequest.target);
1111 if (xevent->xselectionrequest.property == None)
1112 event->selection.property = event->selection.target;
1113 else
1114 event->selection.property = gdk_x11_xatom_to_atom_for_display (display, xevent->xselectionrequest.property);
1115 if (xevent->xselectionrequest.requestor != None)
1116 event->selection.requestor = gdk_x11_window_foreign_new_for_display (display,
1117 xevent->xselectionrequest.requestor);
1118 else
1119 event->selection.requestor = NULL;
1120 event->selection.time = xevent->xselectionrequest.time;
1121
1122 break;
1123
1124 case SelectionNotify:
1125 GDK_NOTE (EVENTS,
1126 g_message ("selection notify:\twindow: %ld",
1127 xevent->xproperty.window));
1128
1129 event->selection.type = GDK_SELECTION_NOTIFY;
1130 event->selection.window = window;
1131 event->selection.selection = gdk_x11_xatom_to_atom_for_display (display, xevent->xselection.selection);
1132 event->selection.target = gdk_x11_xatom_to_atom_for_display (display, xevent->xselection.target);
1133 if (xevent->xselection.property == None)
1134 event->selection.property = event->selection.target;
1135 else
1136 event->selection.property = gdk_x11_xatom_to_atom_for_display (display, xevent->xselection.property);
1137 event->selection.time = xevent->xselection.time;
1138
1139 break;
1140
1141 case ColormapNotify:
1142 GDK_NOTE (EVENTS,
1143 g_message ("colormap notify:\twindow: %ld",
1144 xevent->xcolormap.window));
1145
1146 /* Not currently handled */
1147 return_val = FALSE;
1148 break;
1149
1150 case ClientMessage:
1151 GDK_NOTE (EVENTS,
1152 g_message ("client message:\twindow: %ld",
1153 xevent->xclient.window));
1154
1155 /* Not currently handled */
1156 return_val = FALSE;
1157 break;
1158
1159 case MappingNotify:
1160 GDK_NOTE (EVENTS,
1161 g_message ("mapping notify"));
1162
1163 /* Let XLib know that there is a new keyboard mapping.
1164 */
1165 XRefreshKeyboardMapping (&xevent->xmapping);
1166 _gdk_x11_keymap_keys_changed (display);
1167 return_val = FALSE;
1168 break;
1169
1170 default:
1171 #ifdef HAVE_XFIXES
1172 if (xevent->type - display_x11->xfixes_event_base == XFixesSelectionNotify)
1173 {
1174 XFixesSelectionNotifyEvent *selection_notify = (XFixesSelectionNotifyEvent *)xevent;
1175
1176 _gdk_x11_screen_process_owner_change (screen, xevent);
1177
1178 event->owner_change.type = GDK_OWNER_CHANGE;
1179 event->owner_change.window = window;
1180 if (selection_notify->owner != None)
1181 event->owner_change.owner = gdk_x11_window_foreign_new_for_display (display,
1182 selection_notify->owner);
1183 else
1184 event->owner_change.owner = NULL;
1185 event->owner_change.reason = selection_notify->subtype;
1186 event->owner_change.selection =
1187 gdk_x11_xatom_to_atom_for_display (display,
1188 selection_notify->selection);
1189 event->owner_change.time = selection_notify->timestamp;
1190 event->owner_change.selection_time = selection_notify->selection_timestamp;
1191
1192 return_val = TRUE;
1193 }
1194 else
1195 #endif
1196 #ifdef HAVE_RANDR
1197 if (xevent->type - display_x11->xrandr_event_base == RRScreenChangeNotify ||
1198 xevent->type - display_x11->xrandr_event_base == RRNotify)
1199 {
1200 if (screen)
1201 _gdk_x11_screen_size_changed (screen, xevent);
1202 }
1203 else
1204 #endif
1205 #if defined(HAVE_XCOMPOSITE) && defined (HAVE_XDAMAGE) && defined (HAVE_XFIXES)
1206 if (display_x11->have_xdamage && window && window->composited &&
1207 xevent->type == display_x11->xdamage_event_base + XDamageNotify &&
1208 ((XDamageNotifyEvent *) xevent)->damage == window_impl->damage)
1209 {
1210 XDamageNotifyEvent *damage_event = (XDamageNotifyEvent *) xevent;
1211 XserverRegion repair;
1212 GdkRectangle rect;
1213 int x2, y2;
1214
1215 rect.x = window->x + damage_event->area.x / window_impl->window_scale;
1216 rect.y = window->y + damage_event->area.y / window_impl->window_scale;
1217
1218 x2 = (rect.x * window_impl->window_scale + damage_event->area.width + window_impl->window_scale -1) / window_impl->window_scale;
1219 y2 = (rect.y * window_impl->window_scale + damage_event->area.height + window_impl->window_scale -1) / window_impl->window_scale;
1220 rect.width = x2 - rect.x;
1221 rect.height = y2 - rect.y;
1222
1223 repair = XFixesCreateRegion (display_x11->xdisplay,
1224 &damage_event->area, 1);
1225 XDamageSubtract (display_x11->xdisplay,
1226 window_impl->damage,
1227 repair, None);
1228 XFixesDestroyRegion (display_x11->xdisplay, repair);
1229
1230 if (window->parent != NULL)
1231 _gdk_x11_window_process_expose (window->parent,
1232 damage_event->serial, &rect);
1233
1234 return_val = TRUE;
1235 }
1236 else
1237 #endif
1238 #ifdef HAVE_XKB
1239 if (xevent->type == display_x11->xkb_event_type)
1240 {
1241 XkbEvent *xkb_event = (XkbEvent *) xevent;
1242
1243 switch (xkb_event->any.xkb_type)
1244 {
1245 case XkbNewKeyboardNotify:
1246 case XkbMapNotify:
1247 _gdk_x11_keymap_keys_changed (display);
1248
1249 return_val = FALSE;
1250 break;
1251
1252 case XkbStateNotify:
1253 _gdk_x11_keymap_state_changed (display, xevent);
1254 break;
1255 }
1256 }
1257 else
1258 #endif
1259 return_val = FALSE;
1260 }
1261
1262 done:
1263 if (return_val)
1264 {
1265 if (event->any.window)
1266 g_object_ref (event->any.window);
1267 }
1268 else
1269 {
1270 /* Mark this event as having no resources to be freed */
1271 event->any.window = NULL;
1272 event->any.type = GDK_NOTHING;
1273 }
1274
1275 if (window)
1276 g_object_unref (window);
1277
1278 return return_val;
1279 }
1280
1281 static GdkFrameTimings *
1282 find_frame_timings (GdkFrameClock *clock,
1283 guint64 serial)
1284 {
1285 gint64 start_frame, end_frame, i;
1286
1287 start_frame = gdk_frame_clock_get_history_start (clock);
1288 end_frame = gdk_frame_clock_get_frame_counter (clock);
1289 for (i = end_frame; i >= start_frame; i--)
1290 {
1291 GdkFrameTimings *timings = gdk_frame_clock_get_timings (clock, i);
1292
1293 if (timings->cookie == serial)
1294 return timings;
1295 }
1296
1297 return NULL;
1298 }
1299
1300 /* _NET_WM_FRAME_DRAWN and _NET_WM_FRAME_TIMINGS messages represent time
1301 * as a "high resolution server time" - this is the server time interpolated
1302 * to microsecond resolution. The advantage of this time representation
1303 * is that if X server is running on the same computer as a client, and
1304 * the Xserver uses 'clock_gettime(CLOCK_MONOTONIC, ...)' for the server
1305 * time, the client can detect this, and all such clients will share a
1306 * a time representation with high accuracy. If there is not a common
1307 * time source, then the time synchronization will be less accurate.
1308 */
1309 gint64
1310 server_time_to_monotonic_time (GdkX11Display *display_x11,
1311 gint64 server_time)
1312 {
1313 if (display_x11->server_time_query_time == 0 ||
1314 (!display_x11->server_time_is_monotonic_time &&
1315 server_time > display_x11->server_time_query_time + 10*1000*1000)) /* 10 seconds */
1316 {
1317 gint64 current_server_time = gdk_x11_get_server_time (display_x11->leader_gdk_window);
1318 gint64 current_server_time_usec = (gint64)current_server_time * 1000;
1319 gint64 current_monotonic_time = g_get_monotonic_time ();
1320 display_x11->server_time_query_time = current_monotonic_time;
1321
1322 /* If the server time is within a second of the monotonic time,
1323 * we assume that they are identical. This seems like a big margin,
1324 * but we want to be as robust as possible even if the system
1325 * is under load and our processing of the server response is
1326 * delayed.
1327 */
1328 if (current_server_time_usec > current_monotonic_time - 1000*1000 &&
1329 current_server_time_usec < current_monotonic_time + 1000*1000)
1330 display_x11->server_time_is_monotonic_time = TRUE;
1331
1332 display_x11->server_time_offset = current_server_time_usec - current_monotonic_time;
1333 }
1334
1335 if (display_x11->server_time_is_monotonic_time)
1336 return server_time;
1337 else
1338 return server_time - display_x11->server_time_offset;
1339 }
1340
1341 GdkFilterReturn
1342 _gdk_wm_protocols_filter (GdkXEvent *xev,
1343 GdkEvent *event,
1344 gpointer data)
1345 {
1346 XEvent *xevent = (XEvent *)xev;
1347 GdkWindow *win = event->any.window;
1348 GdkDisplay *display;
1349 Atom atom;
1350
1351 if (!GDK_IS_X11_WINDOW (win) || GDK_WINDOW_DESTROYED (win))
1352 return GDK_FILTER_CONTINUE;
1353
1354 if (xevent->type != ClientMessage)
1355 return GDK_FILTER_CONTINUE;
1356
1357 display = GDK_WINDOW_DISPLAY (win);
1358
1359 /* This isn't actually WM_PROTOCOLS because that wouldn't leave enough space
1360 * in the message for everything that gets stuffed in */
1361 if (xevent->xclient.message_type == gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_FRAME_DRAWN"))
1362 {
1363 GdkWindowImplX11 *window_impl;
1364 window_impl = GDK_WINDOW_IMPL_X11 (win->impl);
1365 if (window_impl->toplevel)
1366 {
1367 guint32 d0 = xevent->xclient.data.l[0];
1368 guint32 d1 = xevent->xclient.data.l[1];
1369 guint32 d2 = xevent->xclient.data.l[2];
1370 guint32 d3 = xevent->xclient.data.l[3];
1371
1372 guint64 serial = ((guint64)d1 << 32) | d0;
1373 gint64 frame_drawn_time = server_time_to_monotonic_time (GDK_X11_DISPLAY (display), ((guint64)d3 << 32) | d2);
1374 gint64 refresh_interval, presentation_time;
1375
1376 GdkFrameClock *clock = gdk_window_get_frame_clock (win);
1377 GdkFrameTimings *timings = find_frame_timings (clock, serial);
1378
1379 if (timings)
1380 timings->drawn_time = frame_drawn_time;
1381
1382 if (window_impl->toplevel->frame_pending)
1383 {
1384 window_impl->toplevel->frame_pending = FALSE;
1385 _gdk_frame_clock_thaw (clock);
1386 }
1387
1388 gdk_frame_clock_get_refresh_info (clock,
1389 frame_drawn_time,
1390 &refresh_interval,
1391 &presentation_time);
1392 if (presentation_time != 0)
1393 window_impl->toplevel->throttled_presentation_time = presentation_time + refresh_interval;
1394 }
1395
1396 return GDK_FILTER_REMOVE;
1397 }
1398
1399 if (xevent->xclient.message_type == gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_FRAME_TIMINGS"))
1400 {
1401 GdkWindowImplX11 *window_impl;
1402 window_impl = GDK_WINDOW_IMPL_X11 (win->impl);
1403 if (window_impl->toplevel)
1404 {
1405 guint32 d0 = xevent->xclient.data.l[0];
1406 guint32 d1 = xevent->xclient.data.l[1];
1407 guint32 d2 = xevent->xclient.data.l[2];
1408 guint32 d3 = xevent->xclient.data.l[3];
1409
1410 guint64 serial = ((guint64)d1 << 32) | d0;
1411
1412 GdkFrameClock *clock = gdk_window_get_frame_clock (win);
1413 GdkFrameTimings *timings = find_frame_timings (clock, serial);
1414
1415 if (timings)
1416 {
1417 gint32 presentation_time_offset = (gint32)d2;
1418 gint32 refresh_interval = d3;
1419
1420 if (timings->drawn_time && presentation_time_offset)
1421 timings->presentation_time = timings->drawn_time + presentation_time_offset;
1422
1423 if (refresh_interval)
1424 timings->refresh_interval = refresh_interval;
1425
1426 timings->complete = TRUE;
1427 #ifdef G_ENABLE_DEBUG
1428 if (GDK_DEBUG_CHECK (FRAMES))
1429 _gdk_frame_clock_debug_print_timings (clock, timings);
1430
1431 if (gdk_profiler_is_running ())
1432 _gdk_frame_clock_add_timings_to_profiler (clock, timings);
1433 #endif /* G_ENABLE_DEBUG */
1434 }
1435 }
1436 }
1437
1438 if (xevent->xclient.message_type != gdk_x11_get_xatom_by_name_for_display (display, "WM_PROTOCOLS"))
1439 return GDK_FILTER_CONTINUE;
1440
1441 atom = (Atom) xevent->xclient.data.l[0];
1442
1443 if (atom == gdk_x11_get_xatom_by_name_for_display (display, "WM_DELETE_WINDOW"))
1444 {
1445 /* The delete window request specifies a window
1446 * to delete. We don't actually destroy the
1447 * window because "it is only a request". (The
1448 * window might contain vital data that the
1449 * program does not want destroyed). Instead
1450 * the event is passed along to the program,
1451 * which should then destroy the window.
1452 */
1453 GDK_NOTE (EVENTS,
1454 g_message ("delete window:\t\twindow: %ld",
1455 xevent->xclient.window));
1456
1457 event->any.type = GDK_DELETE;
1458
1459 gdk_x11_window_set_user_time (win, xevent->xclient.data.l[1]);
1460
1461 return GDK_FILTER_TRANSLATE;
1462 }
1463 else if (atom == gdk_x11_get_xatom_by_name_for_display (display, "WM_TAKE_FOCUS"))
1464 {
1465 GdkToplevelX11 *toplevel = _gdk_x11_window_get_toplevel (win);
1466
1467 /* There is no way of knowing reliably whether we are viewable;
1468 * so trap errors asynchronously around the XSetInputFocus call
1469 */
1470 if (toplevel && win->accept_focus)
1471 {
1472 gdk_x11_display_error_trap_push (display);
1473 XSetInputFocus (GDK_DISPLAY_XDISPLAY (display),
1474 toplevel->focus_window,
1475 RevertToParent,
1476 xevent->xclient.data.l[1]);
1477 gdk_x11_display_error_trap_pop_ignored (display);
1478 }
1479
1480 return GDK_FILTER_REMOVE;
1481 }
1482 else if (atom == gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_PING") &&
1483 !_gdk_x11_display_is_root_window (display,
1484 xevent->xclient.window))
1485 {
1486 XClientMessageEvent xclient = xevent->xclient;
1487
1488 xclient.window = GDK_WINDOW_XROOTWIN (win);
1489 XSendEvent (GDK_WINDOW_XDISPLAY (win),
1490 xclient.window,
1491 False,
1492 SubstructureRedirectMask | SubstructureNotifyMask, (XEvent *)&xclient);
1493
1494 return GDK_FILTER_REMOVE;
1495 }
1496 else if (atom == gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_SYNC_REQUEST") &&
1497 GDK_X11_DISPLAY (display)->use_sync)
1498 {
1499 GdkToplevelX11 *toplevel = _gdk_x11_window_get_toplevel (win);
1500 if (toplevel)
1501 {
1502 #ifdef HAVE_XSYNC
1503 toplevel->pending_counter_value = xevent->xclient.data.l[2] + ((gint64)xevent->xclient.data.l[3] << 32);
1504 toplevel->pending_counter_value_is_extended = xevent->xclient.data.l[4] != 0;
1505 #endif
1506 }
1507 return GDK_FILTER_REMOVE;
1508 }
1509
1510 return GDK_FILTER_CONTINUE;
1511 }
1512
1513 static void
1514 gdk_event_init (GdkDisplay *display)
1515 {
1516 GdkX11Display *display_x11;
1517 GdkDeviceManager *device_manager;
1518
1519 display_x11 = GDK_X11_DISPLAY (display);
1520 display_x11->event_source = gdk_x11_event_source_new (display);
1521
1522 gdk_x11_event_source_add_translator ((GdkEventSource *) display_x11->event_source,
1523 GDK_EVENT_TRANSLATOR (display));
1524
1525 G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
1526 device_manager = gdk_display_get_device_manager (display);
1527 gdk_x11_event_source_add_translator ((GdkEventSource *) display_x11->event_source,
1528 GDK_EVENT_TRANSLATOR (device_manager));
1529 G_GNUC_END_IGNORE_DEPRECATIONS;
1530 }
1531
1532 static void
1533 set_sm_client_id (GdkDisplay *display,
1534 const gchar *sm_client_id)
1535 {
1536 GdkX11Display *display_x11 = GDK_X11_DISPLAY (display);
1537
1538 if (gdk_display_is_closed (display))
1539 return;
1540
1541 if (sm_client_id && strcmp (sm_client_id, ""))
1542 XChangeProperty (display_x11->xdisplay, display_x11->leader_window,
1543 gdk_x11_get_xatom_by_name_for_display (display, "SM_CLIENT_ID"),
1544 XA_STRING, 8, PropModeReplace, (guchar *)sm_client_id,
1545 strlen (sm_client_id));
1546 else
1547 XDeleteProperty (display_x11->xdisplay, display_x11->leader_window,
1548 gdk_x11_get_xatom_by_name_for_display (display, "SM_CLIENT_ID"));
1549 }
1550
1551 GdkDisplay *
1552 _gdk_x11_display_open (const gchar *display_name)
1553 {
1554 Display *xdisplay;
1555 GdkDisplay *display;
1556 GdkX11Display *display_x11;
1557 GdkWindowAttr attr;
1558 gint argc;
1559 gchar *argv[1];
1560
1561 XClassHint *class_hint;
1562 gint ignore;
1563 gint maj, min;
1564
1565 xdisplay = XOpenDisplay (display_name);
1566 if (!xdisplay)
1567 return NULL;
1568
1569 display = g_object_new (GDK_TYPE_X11_DISPLAY, NULL);
1570 display_x11 = GDK_X11_DISPLAY (display);
1571
1572 display_x11->xdisplay = xdisplay;
1573
1574 /* Set up handlers for Xlib internal connections */
1575 XAddConnectionWatch (xdisplay, gdk_internal_connection_watch, NULL);
1576
1577 _gdk_x11_precache_atoms (display, precache_atoms, G_N_ELEMENTS (precache_atoms));
1578
1579 /* RandR must be initialized before we initialize the screens */
1580 display_x11->have_randr12 = FALSE;
1581 display_x11->have_randr13 = FALSE;
1582 display_x11->have_randr15 = FALSE;
1583 #ifdef HAVE_RANDR
1584 if (XRRQueryExtension (display_x11->xdisplay,
1585 &display_x11->xrandr_event_base, &ignore))
1586 {
1587 int major, minor;
1588
1589 XRRQueryVersion (display_x11->xdisplay, &major, &minor);
1590
1591 if ((major == 1 && minor >= 2) || major > 1) {
1592 display_x11->have_randr12 = TRUE;
1593 if (minor >= 3 || major > 1)
1594 display_x11->have_randr13 = TRUE;
1595 #ifdef HAVE_RANDR15
1596 if (minor >= 5 || major > 1)
1597 display_x11->have_randr15 = TRUE;
1598 #endif
1599 }
1600
1601 gdk_x11_register_standard_event_type (display, display_x11->xrandr_event_base, RRNumberEvents);
1602 }
1603 #endif
1604
1605 /* initialize the display's screens */
1606 display_x11->screen = _gdk_x11_screen_new (display, DefaultScreen (display_x11->xdisplay));
1607
1608 /* We need to initialize events after we have the screen
1609 * structures in places
1610 */
1611 _gdk_x11_xsettings_init (GDK_X11_SCREEN (display_x11->screen));
1612
1613 display->device_manager = _gdk_x11_device_manager_new (display);
1614
1615 gdk_event_init (display);
1616
1617 attr.window_type = GDK_WINDOW_TOPLEVEL;
1618 attr.wclass = GDK_INPUT_ONLY;
1619 attr.x = 10;
1620 attr.y = 10;
1621 attr.width = 10;
1622 attr.height = 10;
1623 attr.event_mask = 0;
1624
1625 display_x11->leader_gdk_window = gdk_window_new (GDK_X11_SCREEN (display_x11->screen)->root_window,
1626 &attr, GDK_WA_X | GDK_WA_Y);
1627 (_gdk_x11_window_get_toplevel (display_x11->leader_gdk_window))->is_leader = TRUE;
1628
1629 display_x11->leader_window = GDK_WINDOW_XID (display_x11->leader_gdk_window);
1630
1631 display_x11->leader_window_title_set = FALSE;
1632
1633 #ifdef HAVE_XFIXES
1634 if (XFixesQueryExtension (display_x11->xdisplay,
1635 &display_x11->xfixes_event_base,
1636 &ignore))
1637 {
1638 display_x11->have_xfixes = TRUE;
1639
1640 gdk_x11_register_standard_event_type (display,
1641 display_x11->xfixes_event_base,
1642 XFixesNumberEvents);
1643 }
1644 else
1645 #endif
1646 display_x11->have_xfixes = FALSE;
1647
1648 #ifdef HAVE_XCOMPOSITE
1649 if (XCompositeQueryExtension (display_x11->xdisplay,
1650 &ignore, &ignore))
1651 {
1652 int major, minor;
1653
1654 XCompositeQueryVersion (display_x11->xdisplay, &major, &minor);
1655
1656 /* Prior to Composite version 0.4, composited windows clipped their
1657 * parents, so you had to use IncludeInferiors to draw to the parent
1658 * This isn't useful for our purposes, so require 0.4
1659 */
1660 display_x11->have_xcomposite = major > 0 || (major == 0 && minor >= 4);
1661 }
1662 else
1663 #endif
1664 display_x11->have_xcomposite = FALSE;
1665
1666 #ifdef HAVE_XDAMAGE
1667 if (XDamageQueryExtension (display_x11->xdisplay,
1668 &display_x11->xdamage_event_base,
1669 &ignore))
1670 {
1671 display_x11->have_xdamage = TRUE;
1672
1673 gdk_x11_register_standard_event_type (display,
1674 display_x11->xdamage_event_base,
1675 XDamageNumberEvents);
1676 }
1677 else
1678 #endif
1679 display_x11->have_xdamage = FALSE;
1680
1681 display_x11->have_shapes = FALSE;
1682 display_x11->have_input_shapes = FALSE;
1683
1684 if (XShapeQueryExtension (GDK_DISPLAY_XDISPLAY (display), &display_x11->shape_event_base, &ignore))
1685 {
1686 display_x11->have_shapes = TRUE;
1687 #ifdef ShapeInput
1688 if (XShapeQueryVersion (GDK_DISPLAY_XDISPLAY (display), &maj, &min))
1689 display_x11->have_input_shapes = (maj == 1 && min >= 1);
1690 #endif
1691 }
1692
1693 display_x11->trusted_client = TRUE;
1694 {
1695 Window root, child;
1696 int rootx, rooty, winx, winy;
1697 unsigned int xmask;
1698
1699 gdk_x11_display_error_trap_push (display);
1700 XQueryPointer (display_x11->xdisplay,
1701 GDK_X11_SCREEN (display_x11->screen)->xroot_window,
1702 &root, &child, &rootx, &rooty, &winx, &winy, &xmask);
1703 if (G_UNLIKELY (gdk_x11_display_error_trap_pop (display) == BadWindow))
1704 {
1705 g_warning ("Connection to display %s appears to be untrusted. Pointer and keyboard grabs and inter-client communication may not work as expected.", gdk_display_get_name (display));
1706 display_x11->trusted_client = FALSE;
1707 }
1708 }
1709
1710 if (g_getenv ("GDK_SYNCHRONIZE"))
1711 XSynchronize (display_x11->xdisplay, True);
1712
1713 class_hint = XAllocClassHint();
1714 class_hint->res_name = (char *) g_get_prgname ();
1715 class_hint->res_class = (char *)gdk_get_program_class ();
1716
1717 /* XmbSetWMProperties sets the RESOURCE_NAME environment variable
1718 * from argv[0], so we just synthesize an argument array here.
1719 */
1720 argc = 1;
1721 argv[0] = (char *) g_get_prgname ();
1722
1723 XmbSetWMProperties (display_x11->xdisplay,
1724 display_x11->leader_window,
1725 NULL, NULL, argv, argc, NULL, NULL,
1726 class_hint);
1727 XFree (class_hint);
1728
1729 if (gdk_sm_client_id)
1730 set_sm_client_id (display, gdk_sm_client_id);
1731
1732 if (!gdk_running_in_sandbox ())
1733 {
1734 /* if sandboxed, we're likely in a pid namespace and would only confuse the wm with this */
1735 long pid = getpid ();
1736 XChangeProperty (display_x11->xdisplay,
1737 display_x11->leader_window,
1738 gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_PID"),
1739 XA_CARDINAL, 32, PropModeReplace, (guchar *) & pid, 1);
1740 }
1741
1742 /* We don't yet know a valid time. */
1743 display_x11->user_time = 0;
1744
1745 #ifdef HAVE_XKB
1746 {
1747 gint xkb_major = XkbMajorVersion;
1748 gint xkb_minor = XkbMinorVersion;
1749 if (XkbLibraryVersion (&xkb_major, &xkb_minor))
1750 {
1751 xkb_major = XkbMajorVersion;
1752 xkb_minor = XkbMinorVersion;
1753
1754 if (XkbQueryExtension (display_x11->xdisplay,
1755 NULL, &display_x11->xkb_event_type, NULL,
1756 &xkb_major, &xkb_minor))
1757 {
1758 Bool detectable_autorepeat_supported;
1759
1760 display_x11->use_xkb = TRUE;
1761
1762 XkbSelectEvents (display_x11->xdisplay,
1763 XkbUseCoreKbd,
1764 XkbNewKeyboardNotifyMask | XkbMapNotifyMask | XkbStateNotifyMask,
1765 XkbNewKeyboardNotifyMask | XkbMapNotifyMask | XkbStateNotifyMask);
1766
1767 /* keep this in sync with _gdk_x11_keymap_state_changed() */
1768 XkbSelectEventDetails (display_x11->xdisplay,
1769 XkbUseCoreKbd, XkbStateNotify,
1770 XkbAllStateComponentsMask,
1771 XkbModifierStateMask|XkbGroupStateMask);
1772
1773 XkbSetDetectableAutoRepeat (display_x11->xdisplay,
1774 True,
1775 &detectable_autorepeat_supported);
1776
1777 GDK_NOTE (MISC, g_message ("Detectable autorepeat %s.",
1778 detectable_autorepeat_supported ?
1779 "supported" : "not supported"));
1780
1781 display_x11->have_xkb_autorepeat = detectable_autorepeat_supported;
1782 }
1783 }
1784 }
1785 #endif
1786
1787 display_x11->use_sync = FALSE;
1788 #ifdef HAVE_XSYNC
1789 {
1790 int major, minor;
1791 int error_base, event_base;
1792
1793 if (XSyncQueryExtension (display_x11->xdisplay,
1794 &event_base, &error_base) &&
1795 XSyncInitialize (display_x11->xdisplay,
1796 &major, &minor))
1797 display_x11->use_sync = TRUE;
1798 }
1799 #endif
1800
1801 _gdk_x11_screen_setup (display_x11->screen);
1802
1803 g_signal_emit_by_name (display, "opened");
1804
1805 return display;
1806 }
1807
1808 /*
1809 * XLib internal connection handling
1810 */
1811 typedef struct _GdkInternalConnection GdkInternalConnection;
1812
1813 struct _GdkInternalConnection
1814 {
1815 gint fd;
1816 GSource *source;
1817 Display *display;
1818 };
1819
1820 static gboolean
1821 process_internal_connection (GIOChannel *gioc,
1822 GIOCondition cond,
1823 gpointer data)
1824 {
1825 GdkInternalConnection *connection = (GdkInternalConnection *)data;
1826
1827 gdk_threads_enter ();
1828
1829 XProcessInternalConnection ((Display*)connection->display, connection->fd);
1830
1831 gdk_threads_leave ();
1832
1833 return TRUE;
1834 }
1835
1836 static gulong
1837 gdk_x11_display_get_next_serial (GdkDisplay *display)
1838 {
1839 return NextRequest (GDK_DISPLAY_XDISPLAY (display));
1840 }
1841
1842
1843 static GdkInternalConnection *
1844 gdk_add_connection_handler (Display *display,
1845 guint fd)
1846 {
1847 GIOChannel *io_channel;
1848 GdkInternalConnection *connection;
1849
1850 connection = g_new (GdkInternalConnection, 1);
1851
1852 connection->fd = fd;
1853 connection->display = display;
1854
1855 io_channel = g_io_channel_unix_new (fd);
1856
1857 connection->source = g_io_create_watch (io_channel, G_IO_IN);
1858 g_source_set_callback (connection->source,
1859 (GSourceFunc)process_internal_connection, connection, NULL);
1860 g_source_attach (connection->source, NULL);
1861
1862 g_io_channel_unref (io_channel);
1863
1864 return connection;
1865 }
1866
1867 static void
1868 gdk_remove_connection_handler (GdkInternalConnection *connection)
1869 {
1870 g_source_destroy (connection->source);
1871 g_free (connection);
1872 }
1873
1874 static void
1875 gdk_internal_connection_watch (Display *display,
1876 XPointer arg,
1877 gint fd,
1878 gboolean opening,
1879 XPointer *watch_data)
1880 {
1881 if (opening)
1882 *watch_data = (XPointer)gdk_add_connection_handler (display, fd);
1883 else
1884 gdk_remove_connection_handler ((GdkInternalConnection *)*watch_data);
1885 }
1886
1887 static const gchar *
1888 gdk_x11_display_get_name (GdkDisplay *display)
1889 {
1890 return (gchar *) DisplayString (GDK_X11_DISPLAY (display)->xdisplay);
1891 }
1892
1893 static GdkScreen *
1894 gdk_x11_display_get_default_screen (GdkDisplay *display)
1895 {
1896 return GDK_X11_DISPLAY (display)->screen;
1897 }
1898
1899 gboolean
1900 _gdk_x11_display_is_root_window (GdkDisplay *display,
1901 Window xroot_window)
1902 {
1903 GdkX11Display *display_x11;
1904
1905 display_x11 = GDK_X11_DISPLAY (display);
1906
1907 return GDK_SCREEN_XROOTWIN (display_x11->screen) == xroot_window;
1908 }
1909
1910 struct XPointerUngrabInfo {
1911 GdkDisplay *display;
1912 guint32 time;
1913 };
1914
1915 static void
1916 device_grab_update_callback (GdkDisplay *display,
1917 gpointer data,
1918 gulong serial)
1919 {
1920 GdkPointerWindowInfo *pointer_info;
1921 GdkDevice *device = data;
1922
1923 pointer_info = _gdk_display_get_pointer_info (display, device);
1924 _gdk_display_device_grab_update (display, device,
1925 pointer_info->last_slave ? pointer_info->last_slave : device,
1926 serial);
1927 }
1928
1929 #define XSERVER_TIME_IS_LATER(time1, time2) \
1930 ( (( time1 > time2 ) && ( time1 - time2 < ((guint32)-1)/2 )) || \
1931 (( time1 < time2 ) && ( time2 - time1 > ((guint32)-1)/2 )) \
1932 )
1933
1934 void
1935 _gdk_x11_display_update_grab_info (GdkDisplay *display,
1936 GdkDevice *device,
1937 gint status)
1938 {
1939 if (status == GrabSuccess)
1940 _gdk_x11_roundtrip_async (display, device_grab_update_callback, device);
1941 }
1942
1943 void
1944 _gdk_x11_display_update_grab_info_ungrab (GdkDisplay *display,
1945 GdkDevice *device,
1946 guint32 time,
1947 gulong serial)
1948 {
1949 GdkDeviceGrabInfo *grab;
1950
1951 XFlush (GDK_DISPLAY_XDISPLAY (display));
1952
1953 grab = _gdk_display_get_last_device_grab (display, device);
1954 if (grab &&
1955 (time == GDK_CURRENT_TIME ||
1956 grab->time == GDK_CURRENT_TIME ||
1957 !XSERVER_TIME_IS_LATER (grab->time, time)))
1958 {
1959 grab->serial_end = serial;
1960 _gdk_x11_roundtrip_async (display, device_grab_update_callback, device);
1961 }
1962 }
1963
1964 static void
1965 gdk_x11_display_beep (GdkDisplay *display)
1966 {
1967 if (!GDK_X11_DISPLAY (display)->trusted_client)
1968 return;
1969
1970 #ifdef HAVE_XKB
1971 XkbBell (GDK_DISPLAY_XDISPLAY (display), None, 0, None);
1972 #else
1973 XBell (GDK_DISPLAY_XDISPLAY (display), 0);
1974 #endif
1975 }
1976
1977 static void
1978 gdk_x11_display_sync (GdkDisplay *display)
1979 {
1980 XSync (GDK_DISPLAY_XDISPLAY (display), False);
1981 }
1982
1983 static void
1984 gdk_x11_display_flush (GdkDisplay *display)
1985 {
1986 if (!display->closed)
1987 XFlush (GDK_DISPLAY_XDISPLAY (display));
1988 }
1989
1990 static gboolean
1991 gdk_x11_display_has_pending (GdkDisplay *display)
1992 {
1993 return XPending (GDK_DISPLAY_XDISPLAY (display));
1994 }
1995
1996 static GdkWindow *
1997 gdk_x11_display_get_default_group (GdkDisplay *display)
1998 {
1999 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
2000
2001 return GDK_X11_DISPLAY (display)->leader_gdk_window;
2002 }
2003
2004 /**
2005 * gdk_x11_display_grab:
2006 * @display: (type GdkX11Display): a #GdkDisplay
2007 *
2008 * Call XGrabServer() on @display.
2009 * To ungrab the display again, use gdk_x11_display_ungrab().
2010 *
2011 * gdk_x11_display_grab()/gdk_x11_display_ungrab() calls can be nested.
2012 *
2013 * Since: 2.2
2014 **/
2015 void
2016 gdk_x11_display_grab (GdkDisplay *display)
2017 {
2018 GdkX11Display *display_x11;
2019
2020 g_return_if_fail (GDK_IS_DISPLAY (display));
2021
2022 display_x11 = GDK_X11_DISPLAY (display);
2023
2024 if (display_x11->grab_count == 0)
2025 XGrabServer (display_x11->xdisplay);
2026 display_x11->grab_count++;
2027 }
2028
2029 /**
2030 * gdk_x11_display_ungrab:
2031 * @display: (type GdkX11Display): a #GdkDisplay
2032 *
2033 * Ungrab @display after it has been grabbed with
2034 * gdk_x11_display_grab().
2035 *
2036 * Since: 2.2
2037 **/
2038 void
2039 gdk_x11_display_ungrab (GdkDisplay *display)
2040 {
2041 GdkX11Display *display_x11;
2042
2043 g_return_if_fail (GDK_IS_DISPLAY (display));
2044
2045 display_x11 = GDK_X11_DISPLAY (display);;
2046 g_return_if_fail (display_x11->grab_count > 0);
2047
2048 display_x11->grab_count--;
2049 if (display_x11->grab_count == 0)
2050 {
2051 XUngrabServer (display_x11->xdisplay);
2052 XFlush (display_x11->xdisplay);
2053 }
2054 }
2055
2056 static void
2057 gdk_x11_display_dispose (GObject *object)
2058 {
2059 GdkX11Display *display_x11 = GDK_X11_DISPLAY (object);
2060
2061 _gdk_screen_close (display_x11->screen);
2062
2063 if (display_x11->event_source)
2064 {
2065 g_source_destroy (display_x11->event_source);
2066 g_source_unref (display_x11->event_source);
2067 display_x11->event_source = NULL;
2068 }
2069
2070 G_OBJECT_CLASS (gdk_x11_display_parent_class)->dispose (object);
2071 }
2072
2073 static void
2074 gdk_x11_display_finalize (GObject *object)
2075 {
2076 GdkX11Display *display_x11 = GDK_X11_DISPLAY (object);
2077
2078 /* Keymap */
2079 if (display_x11->keymap)
2080 g_object_unref (display_x11->keymap);
2081
2082 _gdk_x11_cursor_display_finalize (GDK_DISPLAY (display_x11));
2083
2084 /* Empty the event queue */
2085 _gdk_x11_display_free_translate_queue (GDK_DISPLAY (display_x11));
2086
2087 /* Atom Hashtable */
2088 g_hash_table_destroy (display_x11->atom_from_virtual);
2089 g_hash_table_destroy (display_x11->atom_to_virtual);
2090
2091 /* Leader Window */
2092 XDestroyWindow (display_x11->xdisplay, display_x11->leader_window);
2093
2094 /* List of event window extraction functions */
2095 g_slist_free_full (display_x11->event_types, g_free);
2096
2097 /* input GdkWindow list */
2098 g_list_free_full (display_x11->input_windows, g_free);
2099
2100 /* Free all GdkScreens */
2101 g_object_unref (display_x11->screen);
2102 g_list_free_full (display_x11->screens, g_object_unref);
2103
2104 g_ptr_array_free (display_x11->monitors, TRUE);
2105
2106 g_free (display_x11->startup_notification_id);
2107
2108 /* X ID hashtable */
2109 g_hash_table_destroy (display_x11->xid_ht);
2110
2111 XCloseDisplay (display_x11->xdisplay);
2112
2113 /* error traps */
2114 while (display_x11->error_traps != NULL)
2115 {
2116 GdkErrorTrap *trap = display_x11->error_traps->data;
2117
2118 display_x11->error_traps =
2119 g_slist_delete_link (display_x11->error_traps,
2120 display_x11->error_traps);
2121
2122 if (trap->end_sequence == 0)
2123 g_warning ("Display finalized with an unpopped error trap");
2124
2125 g_slice_free (GdkErrorTrap, trap);
2126 }
2127
2128 G_OBJECT_CLASS (gdk_x11_display_parent_class)->finalize (object);
2129 }
2130
2131 /**
2132 * gdk_x11_lookup_xdisplay:
2133 * @xdisplay: a pointer to an X Display
2134 *
2135 * Find the #GdkDisplay corresponding to @xdisplay, if any exists.
2136 *
2137 * Returns: (transfer none) (type GdkX11Display): the #GdkDisplay, if found, otherwise %NULL.
2138 *
2139 * Since: 2.2
2140 **/
2141 GdkDisplay *
2142 gdk_x11_lookup_xdisplay (Display *xdisplay)
2143 {
2144 GSList *list, *l;
2145 GdkDisplay *display;
2146
2147 display = NULL;
2148
2149 list = gdk_display_manager_list_displays (gdk_display_manager_get ());
2150
2151 for (l = list; l; l = l->next)
2152 {
2153 if (GDK_IS_X11_DISPLAY (l->data) &&
2154 GDK_DISPLAY_XDISPLAY (l->data) == xdisplay)
2155 {
2156 display = l->data;
2157 break;
2158 }
2159 }
2160
2161 g_slist_free (list);
2162
2163 return display;
2164 }
2165
2166 /**
2167 * _gdk_x11_display_screen_for_xrootwin:
2168 * @display: a #GdkDisplay
2169 * @xrootwin: window ID for one of of the screen’s of the display.
2170 *
2171 * Given the root window ID of one of the screen’s of a #GdkDisplay,
2172 * finds the screen.
2173 *
2174 * Returns: (transfer none): the #GdkScreen corresponding to
2175 * @xrootwin, or %NULL.
2176 **/
2177 GdkScreen *
2178 _gdk_x11_display_screen_for_xrootwin (GdkDisplay *display,
2179 Window xrootwin)
2180 {
2181 GdkScreen *screen;
2182 XWindowAttributes attrs;
2183 gboolean result;
2184 GdkX11Display *display_x11;
2185 GList *l;
2186
2187 screen = gdk_display_get_default_screen (display);
2188
2189 if (GDK_SCREEN_XROOTWIN (screen) == xrootwin)
2190 return screen;
2191
2192 display_x11 = GDK_X11_DISPLAY (display);
2193
2194 for (l = display_x11->screens; l; l = l->next)
2195 {
2196 screen = l->data;
2197 if (GDK_SCREEN_XROOTWIN (screen) == xrootwin)
2198 return screen;
2199 }
2200
2201 gdk_x11_display_error_trap_push (display);
2202 result = XGetWindowAttributes (display_x11->xdisplay, xrootwin, &attrs);
2203 if (gdk_x11_display_error_trap_pop (display) || !result)
2204 return NULL;
2205
2206 screen = _gdk_x11_screen_new (display, XScreenNumberOfScreen (attrs.screen));
2207
2208 display_x11->screens = g_list_prepend (display_x11->screens, screen);
2209
2210 return screen;
2211 }
2212
2213 /**
2214 * gdk_x11_display_get_xdisplay:
2215 * @display: (type GdkX11Display): a #GdkDisplay
2216 *
2217 * Returns the X display of a #GdkDisplay.
2218 *
2219 * Returns: (transfer none): an X display
2220 *
2221 * Since: 2.2
2222 */
2223 Display *
2224 gdk_x11_display_get_xdisplay (GdkDisplay *display)
2225 {
2226 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
2227 return GDK_X11_DISPLAY (display)->xdisplay;
2228 }
2229
2230 static void
2231 gdk_x11_display_make_default (GdkDisplay *display)
2232 {
2233 GdkX11Display *display_x11 = GDK_X11_DISPLAY (display);
2234 const gchar *startup_id;
2235
2236 g_free (display_x11->startup_notification_id);
2237 display_x11->startup_notification_id = NULL;
2238
2239 startup_id = gdk_get_desktop_startup_id ();
2240 if (startup_id)
2241 gdk_x11_display_set_startup_notification_id (display, startup_id);
2242 }
2243
2244 static void
2245 broadcast_xmessage (GdkDisplay *display,
2246 const char *message_type,
2247 const char *message_type_begin,
2248 const char *message)
2249 {
2250 Display *xdisplay = GDK_DISPLAY_XDISPLAY (display);
2251 GdkScreen *screen = gdk_display_get_default_screen (display);
2252 GdkWindow *root_window = gdk_screen_get_root_window (screen);
2253 Window xroot_window = GDK_WINDOW_XID (root_window);
2254
2255 Atom type_atom;
2256 Atom type_atom_begin;
2257 Window xwindow;
2258
2259 if (!G_LIKELY (GDK_X11_DISPLAY (display)->trusted_client))
2260 return;
2261
2262 {
2263 XSetWindowAttributes attrs;
2264
2265 attrs.override_redirect = True;
2266 attrs.event_mask = PropertyChangeMask | StructureNotifyMask;
2267
2268 xwindow =
2269 XCreateWindow (xdisplay,
2270 xroot_window,
2271 -100, -100, 1, 1,
2272 0,
2273 CopyFromParent,
2274 CopyFromParent,
2275 (Visual *)CopyFromParent,
2276 CWOverrideRedirect | CWEventMask,
2277 &attrs);
2278 }
2279
2280 type_atom = gdk_x11_get_xatom_by_name_for_display (display,
2281 message_type);
2282 type_atom_begin = gdk_x11_get_xatom_by_name_for_display (display,
2283 message_type_begin);
2284
2285 {
2286 XClientMessageEvent xclient;
2287 const char *src;
2288 const char *src_end;
2289 char *dest;
2290 char *dest_end;
2291
2292 memset(&xclient, 0, sizeof (xclient));
2293 xclient.type = ClientMessage;
2294 xclient.message_type = type_atom_begin;
2295 xclient.display =xdisplay;
2296 xclient.window = xwindow;
2297 xclient.format = 8;
2298
2299 src = message;
2300 src_end = message + strlen (message) + 1; /* +1 to include nul byte */
2301
2302 while (src != src_end)
2303 {
2304 dest = &xclient.data.b[0];
2305 dest_end = dest + 20;
2306
2307 while (dest != dest_end &&
2308 src != src_end)
2309 {
2310 *dest = *src;
2311 ++dest;
2312 ++src;
2313 }
2314
2315 while (dest != dest_end)
2316 {
2317 *dest = 0;
2318 ++dest;
2319 }
2320
2321 XSendEvent (xdisplay,
2322 xroot_window,
2323 False,
2324 PropertyChangeMask,
2325 (XEvent *)&xclient);
2326
2327 xclient.message_type = type_atom;
2328 }
2329 }
2330
2331 XDestroyWindow (xdisplay, xwindow);
2332 XFlush (xdisplay);
2333 }
2334
2335 /**
2336 * gdk_x11_display_broadcast_startup_message:
2337 * @display: (type GdkX11Display): a #GdkDisplay
2338 * @message_type: startup notification message type ("new", "change",
2339 * or "remove")
2340 * @...: a list of key/value pairs (as strings), terminated by a
2341 * %NULL key. (A %NULL value for a key will cause that key to be
2342 * skipped in the output.)
2343 *
2344 * Sends a startup notification message of type @message_type to
2345 * @display.
2346 *
2347 * This is a convenience function for use by code that implements the
2348 * freedesktop startup notification specification. Applications should
2349 * not normally need to call it directly. See the
2350 * [Startup Notification Protocol specification](http://standards.freedesktop.org/startup-notification-spec/startup-notification-latest.txt)
2351 * for definitions of the message types and keys that can be used.
2352 *
2353 * Since: 2.12
2354 **/
2355 void
2356 gdk_x11_display_broadcast_startup_message (GdkDisplay *display,
2357 const char *message_type,
2358 ...)
2359 {
2360 GString *message;
2361 va_list ap;
2362 const char *key, *value, *p;
2363
2364 message = g_string_new (message_type);
2365 g_string_append_c (message, ':');
2366
2367 va_start (ap, message_type);
2368 while ((key = va_arg (ap, const char *)))
2369 {
2370 value = va_arg (ap, const char *);
2371 if (!value)
2372 continue;
2373
2374 g_string_append_printf (message, " %s=\"", key);
2375 for (p = value; *p; p++)
2376 {
2377 switch (*p)
2378 {
2379 case ' ':
2380 case '"':
2381 case '\\':
2382 g_string_append_c (message, '\\');
2383 break;
2384 }
2385
2386 g_string_append_c (message, *p);
2387 }
2388 g_string_append_c (message, '\"');
2389 }
2390 va_end (ap);
2391
2392 broadcast_xmessage (display,
2393 "_NET_STARTUP_INFO",
2394 "_NET_STARTUP_INFO_BEGIN",
2395 message->str);
2396
2397 g_string_free (message, TRUE);
2398 }
2399
2400 static void
2401 gdk_x11_display_notify_startup_complete (GdkDisplay *display,
2402 const gchar *startup_id)
2403 {
2404 gchar *free_this = NULL;
2405
2406 if (startup_id == NULL)
2407 {
2408 GdkX11Display *display_x11 = GDK_X11_DISPLAY (display);
2409
2410 startup_id = free_this = display_x11->startup_notification_id;
2411 display_x11->startup_notification_id = NULL;
2412
2413 if (startup_id == NULL)
2414 return;
2415 }
2416
2417 gdk_x11_display_broadcast_startup_message (display, "remove",
2418 "ID", startup_id,
2419 NULL);
2420
2421 g_free (free_this);
2422 }
2423
2424 static gboolean
2425 gdk_x11_display_supports_selection_notification (GdkDisplay *display)
2426 {
2427 GdkX11Display *display_x11 = GDK_X11_DISPLAY (display);
2428
2429 return display_x11->have_xfixes;
2430 }
2431
2432 static gboolean
2433 gdk_x11_display_request_selection_notification (GdkDisplay *display,
2434 GdkAtom selection)
2435
2436 {
2437 #ifdef HAVE_XFIXES
2438 GdkX11Display *display_x11 = GDK_X11_DISPLAY (display);
2439 Atom atom;
2440
2441 if (display_x11->have_xfixes)
2442 {
2443 atom = gdk_x11_atom_to_xatom_for_display (display,
2444 selection);
2445 XFixesSelectSelectionInput (display_x11->xdisplay,
2446 display_x11->leader_window,
2447 atom,
2448 XFixesSetSelectionOwnerNotifyMask |
2449 XFixesSelectionWindowDestroyNotifyMask |
2450 XFixesSelectionClientCloseNotifyMask);
2451 return TRUE;
2452 }
2453 else
2454 #endif
2455 return FALSE;
2456 }
2457
2458 static gboolean
2459 gdk_x11_display_supports_clipboard_persistence (GdkDisplay *display)
2460 {
2461 Atom clipboard_manager;
2462
2463 /* It might make sense to cache this */
2464 clipboard_manager = gdk_x11_get_xatom_by_name_for_display (display, "CLIPBOARD_MANAGER");
2465 return XGetSelectionOwner (GDK_X11_DISPLAY (display)->xdisplay, clipboard_manager) != None;
2466 }
2467
2468 static void
2469 gdk_x11_display_store_clipboard (GdkDisplay *display,
2470 GdkWindow *clipboard_window,
2471 guint32 time_,
2472 const GdkAtom *targets,
2473 gint n_targets)
2474 {
2475 GdkX11Display *display_x11 = GDK_X11_DISPLAY (display);
2476 Atom clipboard_manager, save_targets;
2477
2478 g_return_if_fail (GDK_WINDOW_IS_X11 (clipboard_window));
2479
2480 clipboard_manager = gdk_x11_get_xatom_by_name_for_display (display, "CLIPBOARD_MANAGER");
2481 save_targets = gdk_x11_get_xatom_by_name_for_display (display, "SAVE_TARGETS");
2482
2483 gdk_x11_display_error_trap_push (display);
2484
2485 if (XGetSelectionOwner (display_x11->xdisplay, clipboard_manager) != None)
2486 {
2487 Atom property_name = None;
2488 Atom *xatoms;
2489 int i;
2490
2491 if (n_targets > 0)
2492 {
2493 property_name = gdk_x11_get_xatom_by_name_for_display (display, "GDK_SELECTION");
2494
2495 xatoms = g_new (Atom, n_targets);
2496 for (i = 0; i < n_targets; i++)
2497 xatoms[i] = gdk_x11_atom_to_xatom_for_display (display, targets[i]);
2498
2499 XChangeProperty (display_x11->xdisplay, GDK_WINDOW_XID (clipboard_window),
2500 property_name, XA_ATOM,
2501 32, PropModeReplace, (guchar *)xatoms, n_targets);
2502 g_free (xatoms);
2503
2504 }
2505
2506 XConvertSelection (display_x11->xdisplay,
2507 clipboard_manager, save_targets, property_name,
2508 GDK_WINDOW_XID (clipboard_window), time_);
2509
2510 }
2511 gdk_x11_display_error_trap_pop_ignored (display);
2512
2513 }
2514
2515 /**
2516 * gdk_x11_display_get_user_time:
2517 * @display: (type GdkX11Display): a #GdkDisplay
2518 *
2519 * Returns the timestamp of the last user interaction on
2520 * @display. The timestamp is taken from events caused
2521 * by user interaction such as key presses or pointer
2522 * movements. See gdk_x11_window_set_user_time().
2523 *
2524 * Returns: the timestamp of the last user interaction
2525 *
2526 * Since: 2.8
2527 */
2528 guint32
2529 gdk_x11_display_get_user_time (GdkDisplay *display)
2530 {
2531 return GDK_X11_DISPLAY (display)->user_time;
2532 }
2533
2534 static gboolean
2535 gdk_x11_display_supports_shapes (GdkDisplay *display)
2536 {
2537 return GDK_X11_DISPLAY (display)->have_shapes;
2538 }
2539
2540 static gboolean
2541 gdk_x11_display_supports_input_shapes (GdkDisplay *display)
2542 {
2543 return GDK_X11_DISPLAY (display)->have_input_shapes;
2544 }
2545
2546
2547 /**
2548 * gdk_x11_display_get_startup_notification_id:
2549 * @display: (type GdkX11Display): a #GdkDisplay
2550 *
2551 * Gets the startup notification ID for a display.
2552 *
2553 * Returns: the startup notification ID for @display
2554 *
2555 * Since: 2.12
2556 */
2557 const gchar *
2558 gdk_x11_display_get_startup_notification_id (GdkDisplay *display)
2559 {
2560 return GDK_X11_DISPLAY (display)->startup_notification_id;
2561 }
2562
2563 /**
2564 * gdk_x11_display_set_startup_notification_id:
2565 * @display: (type GdkX11Display): a #GdkDisplay
2566 * @startup_id: the startup notification ID (must be valid utf8)
2567 *
2568 * Sets the startup notification ID for a display.
2569 *
2570 * This is usually taken from the value of the DESKTOP_STARTUP_ID
2571 * environment variable, but in some cases (such as the application not
2572 * being launched using exec()) it can come from other sources.
2573 *
2574 * If the ID contains the string "_TIME" then the portion following that
2575 * string is taken to be the X11 timestamp of the event that triggered
2576 * the application to be launched and the GDK current event time is set
2577 * accordingly.
2578 *
2579 * The startup ID is also what is used to signal that the startup is
2580 * complete (for example, when opening a window or when calling
2581 * gdk_notify_startup_complete()).
2582 *
2583 * Since: 3.0
2584 **/
2585 void
2586 gdk_x11_display_set_startup_notification_id (GdkDisplay *display,
2587 const gchar *startup_id)
2588 {
2589 GdkX11Display *display_x11;
2590 gchar *time_str;
2591
2592 display_x11 = GDK_X11_DISPLAY (display);
2593
2594 g_free (display_x11->startup_notification_id);
2595 display_x11->startup_notification_id = g_strdup (startup_id);
2596
2597 if (startup_id != NULL)
2598 {
2599 /* Find the launch time from the startup_id, if it's there. Newer spec
2600 * states that the startup_id is of the form <unique>_TIME<timestamp>
2601 */
2602 time_str = g_strrstr (startup_id, "_TIME");
2603 if (time_str != NULL)
2604 {
2605 gulong retval;
2606 gchar *end;
2607 errno = 0;
2608
2609 /* Skip past the "_TIME" part */
2610 time_str += 5;
2611
2612 retval = strtoul (time_str, &end, 0);
2613 if (end != time_str && errno == 0)
2614 display_x11->user_time = retval;
2615 }
2616 else
2617 display_x11->user_time = 0;
2618
2619 /* Set the startup id on the leader window so it
2620 * applies to all windows we create on this display
2621 */
2622 XChangeProperty (display_x11->xdisplay,
2623 display_x11->leader_window,
2624 gdk_x11_get_xatom_by_name_for_display (display, "_NET_STARTUP_ID"),
2625 gdk_x11_get_xatom_by_name_for_display (display, "UTF8_STRING"), 8,
2626 PropModeReplace,
2627 (guchar *)startup_id, strlen (startup_id));
2628 }
2629 else
2630 {
2631 XDeleteProperty (display_x11->xdisplay, display_x11->leader_window,
2632 gdk_x11_get_xatom_by_name_for_display (display, "_NET_STARTUP_ID"));
2633 display_x11->user_time = 0;
2634 }
2635 }
2636
2637 static gboolean
2638 gdk_x11_display_supports_composite (GdkDisplay *display)
2639 {
2640 GdkX11Display *x11_display = GDK_X11_DISPLAY (display);
2641
2642 return x11_display->have_xcomposite &&
2643 x11_display->have_xdamage &&
2644 x11_display->have_xfixes;
2645 }
2646
2647 /**
2648 * gdk_x11_register_standard_event_type:
2649 * @display: (type GdkX11Display): a #GdkDisplay
2650 * @event_base: first event type code to register
2651 * @n_events: number of event type codes to register
2652 *
2653 * Registers interest in receiving extension events with type codes
2654 * between @event_base and `event_base + n_events - 1`.
2655 * The registered events must have the window field in the same place
2656 * as core X events (this is not the case for e.g. XKB extension events).
2657 *
2658 * If an event type is registered, events of this type will go through
2659 * global and window-specific filters (see gdk_window_add_filter()).
2660 * Unregistered events will only go through global filters.
2661 * GDK may register the events of some X extensions on its own.
2662 *
2663 * This function should only be needed in unusual circumstances, e.g.
2664 * when filtering XInput extension events on the root window.
2665 *
2666 * Since: 2.4
2667 **/
2668 void
2669 gdk_x11_register_standard_event_type (GdkDisplay *display,
2670 gint event_base,
2671 gint n_events)
2672 {
2673 GdkEventTypeX11 *event_type;
2674 GdkX11Display *display_x11;
2675
2676 display_x11 = GDK_X11_DISPLAY (display);
2677 event_type = g_new (GdkEventTypeX11, 1);
2678
2679 event_type->base = event_base;
2680 event_type->n_events = n_events;
2681
2682 display_x11->event_types = g_slist_prepend (display_x11->event_types, event_type);
2683 }
2684
2685 /* look up the extension name for a given major opcode. grubs around in
2686 * xlib to do it since a) it’s already cached there b) XQueryExtension
2687 * emits protocol so we can’t use it in an error handler.
2688 */
2689 static const char *
2690 _gdk_x11_decode_request_code(Display *dpy, int code)
2691 {
2692 _XExtension *ext;
2693
2694 if (code < 128)
2695 return "core protocol";
2696
2697 for (ext = dpy->ext_procs; ext; ext = ext->next)
2698 {
2699 if (ext->codes.major_opcode == code)
2700 return ext->name;
2701 }
2702
2703 return "unknown";
2704 }
2705
2706 /* compare X sequence numbers handling wraparound */
2707 #define SEQUENCE_COMPARE(a,op,b) (((long) (a) - (long) (b)) op 0)
2708
2709 /* delivers an error event from the error handler in gdkmain-x11.c */
2710 void
2711 _gdk_x11_display_error_event (GdkDisplay *display,
2712 XErrorEvent *error)
2713 {
2714 GdkX11Display *display_x11;
2715 GSList *tmp_list;
2716 gboolean ignore;
2717
2718 display_x11 = GDK_X11_DISPLAY (display);
2719
2720 ignore = FALSE;
2721 for (tmp_list = display_x11->error_traps;
2722 tmp_list != NULL;
2723 tmp_list = tmp_list->next)
2724 {
2725 GdkErrorTrap *trap;
2726
2727 trap = tmp_list->data;
2728
2729 if (SEQUENCE_COMPARE (trap->start_sequence, <=, error->serial) &&
2730 (trap->end_sequence == 0 ||
2731 SEQUENCE_COMPARE (trap->end_sequence, >, error->serial)))
2732 {
2733 ignore = TRUE;
2734 trap->error_code = error->error_code;
2735 break; /* only innermost trap gets the error code */
2736 }
2737 }
2738
2739 if (!ignore)
2740 {
2741 gchar buf[64];
2742 gchar *msg;
2743
2744 XGetErrorText (display_x11->xdisplay, error->error_code, buf, 63);
2745
2746 msg =
2747 g_strdup_printf ("The program '%s' received an X Window System error.\n"
2748 "This probably reflects a bug in the program.\n"
2749 "The error was '%s'.\n"
2750 " (Details: serial %ld error_code %d request_code %d (%s) minor_code %d)\n"
2751 " (Note to programmers: normally, X errors are reported asynchronously;\n"
2752 " that is, you will receive the error a while after causing it.\n"
2753 " To debug your program, run it with the GDK_SYNCHRONIZE environment\n"
2754 " variable to change this behavior. You can then get a meaningful\n"
2755 " backtrace from your debugger if you break on the gdk_x_error() function.)",
2756 g_get_prgname (),
2757 buf,
2758 error->serial,
2759 error->error_code,
2760 error->request_code,
2761 _gdk_x11_decode_request_code(display_x11->xdisplay,
2762 error->request_code),
2763 error->minor_code);
2764
2765 #ifdef G_ENABLE_DEBUG
2766 g_error ("%s", msg);
2767 #else /* !G_ENABLE_DEBUG */
2768 g_warning ("%s", msg);
2769
2770 _exit (1);
2771 #endif /* G_ENABLE_DEBUG */
2772 }
2773 }
2774
2775 static void
2776 delete_outdated_error_traps (GdkX11Display *display_x11)
2777 {
2778 GSList *tmp_list;
2779 gulong processed_sequence;
2780
2781 processed_sequence = XLastKnownRequestProcessed (display_x11->xdisplay);
2782
2783 tmp_list = display_x11->error_traps;
2784 while (tmp_list != NULL)
2785 {
2786 GdkErrorTrap *trap = tmp_list->data;
2787
2788 if (trap->end_sequence != 0 &&
2789 SEQUENCE_COMPARE (trap->end_sequence, <=, processed_sequence))
2790 {
2791 GSList *free_me = tmp_list;
2792
2793 tmp_list = tmp_list->next;
2794 display_x11->error_traps =
2795 g_slist_delete_link (display_x11->error_traps, free_me);
2796 g_slice_free (GdkErrorTrap, trap);
2797 }
2798 else
2799 {
2800 tmp_list = tmp_list->next;
2801 }
2802 }
2803 }
2804
2805 /**
2806 * gdk_x11_display_error_trap_push:
2807 * @display: (type GdkX11Display): a #GdkDisplay
2808 *
2809 * Begins a range of X requests on @display for which X error events
2810 * will be ignored. Unignored errors (when no trap is pushed) will abort
2811 * the application. Use gdk_x11_display_error_trap_pop() or
2812 * gdk_x11_display_error_trap_pop_ignored()to lift a trap pushed
2813 * with this function.
2814 *
2815 * See also gdk_error_trap_push() to push a trap on all displays.
2816 *
2817 * Since: 3.0
2818 */
2819 void
2820 gdk_x11_display_error_trap_push (GdkDisplay *display)
2821 {
2822 GdkX11Display *display_x11;
2823 GdkErrorTrap *trap;
2824
2825 display_x11 = GDK_X11_DISPLAY (display);
2826
2827 delete_outdated_error_traps (display_x11);
2828
2829 /* set up the Xlib callback to tell us about errors */
2830 _gdk_x11_error_handler_push ();
2831
2832 trap = g_slice_new0 (GdkErrorTrap);
2833
2834 trap->start_sequence = XNextRequest (display_x11->xdisplay);
2835 trap->error_code = Success;
2836
2837 display_x11->error_traps =
2838 g_slist_prepend (display_x11->error_traps, trap);
2839 }
2840
2841 static gint
2842 gdk_x11_display_error_trap_pop_internal (GdkDisplay *display,
2843 gboolean need_code)
2844 {
2845 GdkX11Display *display_x11;
2846 GdkErrorTrap *trap;
2847 GSList *tmp_list;
2848 int result;
2849
2850 display_x11 = GDK_X11_DISPLAY (display);
2851
2852 g_return_val_if_fail (display_x11->error_traps != NULL, Success);
2853
2854 /* Find the first trap that hasn't been popped already */
2855 trap = NULL; /* quiet gcc */
2856 for (tmp_list = display_x11->error_traps;
2857 tmp_list != NULL;
2858 tmp_list = tmp_list->next)
2859 {
2860 trap = tmp_list->data;
2861
2862 if (trap->end_sequence == 0)
2863 break;
2864 }
2865
2866 g_return_val_if_fail (trap != NULL, Success);
2867 g_assert (trap->end_sequence == 0);
2868
2869 /* May need to sync to fill in trap->error_code if we care about
2870 * getting an error code.
2871 */
2872 if (need_code)
2873 {
2874 gulong processed_sequence;
2875 gulong next_sequence;
2876
2877 next_sequence = XNextRequest (display_x11->xdisplay);
2878 processed_sequence = XLastKnownRequestProcessed (display_x11->xdisplay);
2879
2880 /* If our last request was already processed, there is no point
2881 * in syncing. i.e. if last request was a round trip (or even if
2882 * we got an event with the serial of a non-round-trip)
2883 */
2884 if ((next_sequence - 1) != processed_sequence)
2885 {
2886 XSync (display_x11->xdisplay, False);
2887 }
2888
2889 result = trap->error_code;
2890 }
2891 else
2892 {
2893 result = Success;
2894 }
2895
2896 /* record end of trap, giving us a range of
2897 * error sequences we'll ignore.
2898 */
2899 trap->end_sequence = XNextRequest (display_x11->xdisplay);
2900
2901 /* remove the Xlib callback */
2902 _gdk_x11_error_handler_pop ();
2903
2904 /* we may already be outdated */
2905 delete_outdated_error_traps (display_x11);
2906
2907 return result;
2908 }
2909
2910 /**
2911 * gdk_x11_display_set_window_scale:
2912 * @display: (type GdkX11Display): the display
2913 * @scale: The new scale value
2914 *
2915 * Forces a specific window scale for all windows on this display,
2916 * instead of using the default or user configured scale. This
2917 * is can be used to disable scaling support by setting @scale to
2918 * 1, or to programmatically set the window scale.
2919 *
2920 * Once the scale is set by this call it will not change in response
2921 * to later user configuration changes.
2922 *
2923 * Since: 3.10
2924 */
2925 void
2926 gdk_x11_display_set_window_scale (GdkDisplay *display,
2927 gint scale)
2928 {
2929 GdkX11Screen *x11_screen;
2930 gboolean need_reread_settings = FALSE;
2931
2932 g_return_if_fail (GDK_IS_X11_DISPLAY (display));
2933
2934 scale = MAX (scale, 1);
2935
2936 x11_screen = GDK_X11_SCREEN (GDK_X11_DISPLAY (display)->screen);
2937
2938 if (!x11_screen->fixed_window_scale)
2939 {
2940 x11_screen->fixed_window_scale = TRUE;
2941
2942 /* We treat screens with a window scale set differently when
2943 * reading xsettings, so we need to reread
2944 */
2945 need_reread_settings = TRUE;
2946 }
2947
2948 _gdk_x11_screen_set_window_scale (x11_screen, scale);
2949
2950 if (need_reread_settings)
2951 _gdk_x11_settings_force_reread (x11_screen);
2952 }
2953
2954
2955 /**
2956 * gdk_x11_display_error_trap_pop:
2957 * @display: (type GdkX11Display): the display
2958 *
2959 * Pops the error trap pushed by gdk_x11_display_error_trap_push().
2960 * Will XSync() if necessary and will always block until
2961 * the error is known to have occurred or not occurred,
2962 * so the error code can be returned.
2963 *
2964 * If you don’t need to use the return value,
2965 * gdk_x11_display_error_trap_pop_ignored() would be more efficient.
2966 *
2967 * See gdk_error_trap_pop() for the all-displays-at-once
2968 * equivalent.
2969 *
2970 * Since: 3.0
2971 *
2972 * Returns: X error code or 0 on success
2973 */
2974 gint
2975 gdk_x11_display_error_trap_pop (GdkDisplay *display)
2976 {
2977 g_return_val_if_fail (GDK_IS_X11_DISPLAY (display), Success);
2978
2979 return gdk_x11_display_error_trap_pop_internal (display, TRUE);
2980 }
2981
2982 /**
2983 * gdk_x11_display_error_trap_pop_ignored:
2984 * @display: (type GdkX11Display): the display
2985 *
2986 * Pops the error trap pushed by gdk_x11_display_error_trap_push().
2987 * Does not block to see if an error occurred; merely records the
2988 * range of requests to ignore errors for, and ignores those errors
2989 * if they arrive asynchronously.
2990 *
2991 * See gdk_error_trap_pop_ignored() for the all-displays-at-once
2992 * equivalent.
2993 *
2994 * Since: 3.0
2995 */
2996 void
2997 gdk_x11_display_error_trap_pop_ignored (GdkDisplay *display)
2998 {
2999 g_return_if_fail (GDK_IS_X11_DISPLAY (display));
3000
3001 gdk_x11_display_error_trap_pop_internal (display, FALSE);
3002 }
3003
3004 /**
3005 * gdk_x11_set_sm_client_id:
3006 * @sm_client_id: (nullable): the client id assigned by the session manager
3007 * when the connection was opened, or %NULL to remove the property.
3008 *
3009 * Sets the `SM_CLIENT_ID` property on the application’s leader window so that
3010 * the window manager can save the application’s state using the X11R6 ICCCM
3011 * session management protocol.
3012 *
3013 * See the X Session Management Library documentation for more information on
3014 * session management and the Inter-Client Communication Conventions Manual
3015 *
3016 * Since: 2.24
3017 */
3018 void
3019 gdk_x11_set_sm_client_id (const gchar *sm_client_id)
3020 {
3021 GSList *displays, *l;
3022
3023 g_free (gdk_sm_client_id);
3024 gdk_sm_client_id = g_strdup (sm_client_id);
3025
3026 displays = gdk_display_manager_list_displays (gdk_display_manager_get ());
3027 for (l = displays; l; l = l->next)
3028 {
3029 if (GDK_IS_X11_DISPLAY (l->data))
3030 set_sm_client_id (l->data, sm_client_id);
3031 }
3032
3033 g_slist_free (displays);
3034 }
3035
3036 static gint
3037 pop_error_trap (GdkDisplay *display,
3038 gboolean ignored)
3039 {
3040 if (ignored)
3041 {
3042 gdk_x11_display_error_trap_pop_ignored (display);
3043 return Success;
3044 }
3045 else
3046 {
3047 return gdk_x11_display_error_trap_pop (display);
3048 }
3049 }
3050
3051 static GdkKeymap *
3052 gdk_x11_display_get_keymap (GdkDisplay *display)
3053 {
3054 GdkX11Display *display_x11;
3055 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
3056 display_x11 = GDK_X11_DISPLAY (display);
3057
3058 if (!display_x11->keymap)
3059 display_x11->keymap = g_object_new (GDK_TYPE_X11_KEYMAP, NULL);
3060
3061 display_x11->keymap->display = display;
3062
3063 return display_x11->keymap;
3064 }
3065
3066 static GdkSeat *
3067 gdk_x11_display_get_default_seat (GdkDisplay *display)
3068 {
3069 GList *seats, *l;
3070 int device_id;
3071 gboolean result = FALSE;
3072
3073 seats = gdk_display_list_seats (display);
3074
3075 /* Shortcut only one seat being available.
3076 * This path always triggers for core events, so we can freely use XInput below. */
3077 if (g_list_length (seats) == 1)
3078 {
3079 GdkSeat *seat = seats->data;
3080
3081 g_list_free (seats);
3082 return seat;
3083 }
3084
3085 gdk_x11_display_error_trap_push (display);
3086 result = XIGetClientPointer (GDK_DISPLAY_XDISPLAY (display),
3087 None, &device_id);
3088 gdk_x11_display_error_trap_pop_ignored (display);
3089
3090 for (l = seats; l; l = l->next)
3091 {
3092 GdkDevice *pointer;
3093
3094 pointer = gdk_seat_get_pointer (l->data);
3095
3096 if (gdk_x11_device_get_id (pointer) == device_id || !result)
3097 {
3098 GdkSeat *seat = l->data;
3099 g_list_free (seats);
3100
3101 return seat;
3102 }
3103 }
3104
3105 g_list_free (seats);
3106
3107 return NULL;
3108 }
3109
3110 static int
3111 gdk_x11_display_get_n_monitors (GdkDisplay *display)
3112 {
3113 GdkX11Display *x11_display = GDK_X11_DISPLAY (display);
3114
3115 return x11_display->monitors->len;
3116 }
3117
3118
3119 static GdkMonitor *
3120 gdk_x11_display_get_monitor (GdkDisplay *display,
3121 int monitor_num)
3122 {
3123 GdkX11Display *x11_display = GDK_X11_DISPLAY (display);
3124
3125 if (0 <= monitor_num && monitor_num < x11_display->monitors->len)
3126 return (GdkMonitor *)x11_display->monitors->pdata[monitor_num];
3127
3128 return NULL;
3129 }
3130
3131 static GdkMonitor *
3132 gdk_x11_display_get_primary_monitor (GdkDisplay *display)
3133 {
3134 GdkX11Display *x11_display = GDK_X11_DISPLAY (display);
3135
3136 if (0 <= x11_display->primary_monitor && x11_display->primary_monitor < x11_display->monitors->len)
3137 return x11_display->monitors->pdata[x11_display->primary_monitor];
3138
3139 return NULL;
3140 }
3141
3142 static void
3143 gdk_x11_display_class_init (GdkX11DisplayClass * class)
3144 {
3145 GObjectClass *object_class = G_OBJECT_CLASS (class);
3146 GdkDisplayClass *display_class = GDK_DISPLAY_CLASS (class);
3147
3148 object_class->dispose = gdk_x11_display_dispose;
3149 object_class->finalize = gdk_x11_display_finalize;
3150
3151 display_class->window_type = GDK_TYPE_X11_WINDOW;
3152
3153 display_class->get_name = gdk_x11_display_get_name;
3154 display_class->get_default_screen = gdk_x11_display_get_default_screen;
3155 display_class->beep = gdk_x11_display_beep;
3156 display_class->sync = gdk_x11_display_sync;
3157 display_class->flush = gdk_x11_display_flush;
3158 display_class->make_default = gdk_x11_display_make_default;
3159 display_class->has_pending = gdk_x11_display_has_pending;
3160 display_class->queue_events = _gdk_x11_display_queue_events;
3161 display_class->get_default_group = gdk_x11_display_get_default_group;
3162 display_class->supports_selection_notification = gdk_x11_display_supports_selection_notification;
3163 display_class->request_selection_notification = gdk_x11_display_request_selection_notification;
3164 display_class->supports_clipboard_persistence = gdk_x11_display_supports_clipboard_persistence;
3165 display_class->store_clipboard = gdk_x11_display_store_clipboard;
3166 display_class->supports_shapes = gdk_x11_display_supports_shapes;
3167 display_class->supports_input_shapes = gdk_x11_display_supports_input_shapes;
3168 display_class->supports_composite = gdk_x11_display_supports_composite;
3169 display_class->get_app_launch_context = _gdk_x11_display_get_app_launch_context;
3170 display_class->get_cursor_for_type = _gdk_x11_display_get_cursor_for_type;
3171 display_class->get_cursor_for_name = _gdk_x11_display_get_cursor_for_name;
3172 display_class->get_cursor_for_surface = _gdk_x11_display_get_cursor_for_surface;
3173 display_class->get_default_cursor_size = _gdk_x11_display_get_default_cursor_size;
3174 display_class->get_maximal_cursor_size = _gdk_x11_display_get_maximal_cursor_size;
3175 display_class->supports_cursor_alpha = _gdk_x11_display_supports_cursor_alpha;
3176 display_class->supports_cursor_color = _gdk_x11_display_supports_cursor_color;
3177
3178 display_class->before_process_all_updates = _gdk_x11_display_before_process_all_updates;
3179 display_class->after_process_all_updates = _gdk_x11_display_after_process_all_updates;
3180 display_class->get_next_serial = gdk_x11_display_get_next_serial;
3181 display_class->notify_startup_complete = gdk_x11_display_notify_startup_complete;
3182 display_class->create_window_impl = _gdk_x11_display_create_window_impl;
3183 display_class->get_keymap = gdk_x11_display_get_keymap;
3184 display_class->push_error_trap = gdk_x11_display_error_trap_push;
3185 display_class->pop_error_trap = pop_error_trap;
3186 display_class->get_selection_owner = _gdk_x11_display_get_selection_owner;
3187 display_class->set_selection_owner = _gdk_x11_display_set_selection_owner;
3188 display_class->send_selection_notify = _gdk_x11_display_send_selection_notify;
3189 display_class->get_selection_property = _gdk_x11_display_get_selection_property;
3190 display_class->convert_selection = _gdk_x11_display_convert_selection;
3191 display_class->text_property_to_utf8_list = _gdk_x11_display_text_property_to_utf8_list;
3192 display_class->utf8_to_string_target = _gdk_x11_display_utf8_to_string_target;
3193
3194 display_class->is_gl_context_current = gdk_x11_display_is_gl_context_current;
3195 display_class->make_gl_context_current = gdk_x11_display_make_gl_context_current;
3196
3197 display_class->get_default_seat = gdk_x11_display_get_default_seat;
3198
3199 display_class->get_n_monitors = gdk_x11_display_get_n_monitors;
3200 display_class->get_monitor = gdk_x11_display_get_monitor;
3201 display_class->get_primary_monitor = gdk_x11_display_get_primary_monitor;
3202
3203 _gdk_x11_windowing_init ();
3204 }