1 /* GDK - The GIMP Drawing Kit
4 * Copyright 2001 Sun Microsystems Inc.
5 * Copyright (C) 2004 Nokia Corporation
7 * Erwann Chenede <erwann.chenede@sun.com>
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.
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.
19 * You should have received a copy of the GNU Library General Public
20 * License along with this library; if not, write to the
21 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 * Boston, MA 02111-1307, USA.
27 #include "gdkdisplay-x11.h"
31 #include "gdkdisplay.h"
32 #include "gdkeventsource.h"
33 #include "gdkeventtranslator.h"
34 #include "gdkscreen.h"
35 #include "gdkscreen-x11.h"
36 #include "gdkinternals.h"
37 #include "gdkdeviceprivate.h"
38 #include "gdkdevicemanager.h"
39 #include "xsettings-client.h"
42 #include <glib/gprintf.h>
48 #include <X11/Xatom.h>
51 #include <X11/XKBlib.h>
55 #include <X11/extensions/Xfixes.h>
58 #include <X11/extensions/shape.h>
60 #ifdef HAVE_XCOMPOSITE
61 #include <X11/extensions/Xcomposite.h>
65 #include <X11/extensions/Xdamage.h>
69 #include <X11/extensions/Xrandr.h>
72 typedef struct _GdkErrorTrap GdkErrorTrap;
76 /* Next sequence when trap was pushed, i.e. first sequence to
79 gulong start_sequence;
81 /* Next sequence when trap was popped, i.e. first sequence
82 * to not ignore. 0 if trap is still active.
86 /* Most recent error code within the sequence */
90 static void gdk_display_x11_dispose (GObject *object);
91 static void gdk_display_x11_finalize (GObject *object);
93 static void gdk_display_x11_event_translator_init (GdkEventTranslatorIface *iface);
95 static gboolean gdk_display_x11_translate_event (GdkEventTranslator *translator,
101 static void gdk_internal_connection_watch (Display *display,
105 XPointer *watch_data);
106 #endif /* HAVE_X11R6 */
108 typedef struct _GdkEventTypeX11 GdkEventTypeX11;
110 struct _GdkEventTypeX11
116 /* Note that we never *directly* use WM_LOCALE_NAME, WM_PROTOCOLS,
117 * but including them here has the side-effect of getting them
118 * into the internal Xlib cache
120 static const char *const precache_atoms[] = {
130 "_NET_ACTIVE_WINDOW",
131 "_NET_CURRENT_DESKTOP",
132 "_NET_FRAME_EXTENTS",
142 "_NET_WM_STATE_ABOVE",
143 "_NET_WM_STATE_BELOW",
144 "_NET_WM_STATE_FULLSCREEN",
145 "_NET_WM_STATE_MODAL",
146 "_NET_WM_STATE_MAXIMIZED_VERT",
147 "_NET_WM_STATE_MAXIMIZED_HORZ",
148 "_NET_WM_STATE_SKIP_TASKBAR",
149 "_NET_WM_STATE_SKIP_PAGER",
150 "_NET_WM_STATE_STICKY",
151 "_NET_WM_SYNC_REQUEST",
152 "_NET_WM_SYNC_REQUEST_COUNTER",
153 "_NET_WM_WINDOW_TYPE",
154 "_NET_WM_WINDOW_TYPE_NORMAL",
159 G_DEFINE_TYPE_WITH_CODE (GdkDisplayX11, _gdk_display_x11, GDK_TYPE_DISPLAY,
160 G_IMPLEMENT_INTERFACE (GDK_TYPE_EVENT_TRANSLATOR,
161 gdk_display_x11_event_translator_init))
165 _gdk_display_x11_class_init (GdkDisplayX11Class * class)
167 GObjectClass *object_class = G_OBJECT_CLASS (class);
169 object_class->dispose = gdk_display_x11_dispose;
170 object_class->finalize = gdk_display_x11_finalize;
174 _gdk_display_x11_init (GdkDisplayX11 *display)
179 gdk_display_x11_event_translator_init (GdkEventTranslatorIface *iface)
181 iface->translate_event = gdk_display_x11_translate_event;
185 do_net_wm_state_changes (GdkWindow *window)
187 GdkToplevelX11 *toplevel = _gdk_x11_window_get_toplevel (window);
188 GdkWindowState old_state;
190 if (GDK_WINDOW_DESTROYED (window) ||
191 gdk_window_get_window_type (window) != GDK_WINDOW_TOPLEVEL)
194 old_state = gdk_window_get_state (window);
196 /* For found_sticky to remain TRUE, we have to also be on desktop
199 if (old_state & GDK_WINDOW_STATE_STICKY)
201 if (!(toplevel->have_sticky && toplevel->on_all_desktops))
202 gdk_synthesize_window_state (window,
203 GDK_WINDOW_STATE_STICKY,
208 if (toplevel->have_sticky || toplevel->on_all_desktops)
209 gdk_synthesize_window_state (window,
211 GDK_WINDOW_STATE_STICKY);
214 if (old_state & GDK_WINDOW_STATE_FULLSCREEN)
216 if (!toplevel->have_fullscreen)
217 gdk_synthesize_window_state (window,
218 GDK_WINDOW_STATE_FULLSCREEN,
223 if (toplevel->have_fullscreen)
224 gdk_synthesize_window_state (window,
226 GDK_WINDOW_STATE_FULLSCREEN);
229 /* Our "maximized" means both vertical and horizontal; if only one,
230 * we don't expose that via GDK
232 if (old_state & GDK_WINDOW_STATE_MAXIMIZED)
234 if (!(toplevel->have_maxvert && toplevel->have_maxhorz))
235 gdk_synthesize_window_state (window,
236 GDK_WINDOW_STATE_MAXIMIZED,
241 if (toplevel->have_maxvert && toplevel->have_maxhorz)
242 gdk_synthesize_window_state (window,
244 GDK_WINDOW_STATE_MAXIMIZED);
249 gdk_check_wm_desktop_changed (GdkWindow *window)
251 GdkToplevelX11 *toplevel = _gdk_x11_window_get_toplevel (window);
252 GdkDisplay *display = GDK_WINDOW_DISPLAY (window);
262 gdk_error_trap_push ();
263 XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display),
264 GDK_WINDOW_XID (window),
265 gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_DESKTOP"),
266 0, G_MAXLONG, False, XA_CARDINAL, &type,
268 &bytes_after, &data);
269 gdk_error_trap_pop_ignored ();
273 desktop = (gulong *)data;
274 toplevel->on_all_desktops = (*desktop == 0xFFFFFFFF);
278 toplevel->on_all_desktops = FALSE;
280 do_net_wm_state_changes (window);
284 gdk_check_wm_state_changed (GdkWindow *window)
286 GdkToplevelX11 *toplevel = _gdk_x11_window_get_toplevel (window);
287 GdkDisplay *display = GDK_WINDOW_DISPLAY (window);
297 gboolean had_sticky = toplevel->have_sticky;
299 toplevel->have_sticky = FALSE;
300 toplevel->have_maxvert = FALSE;
301 toplevel->have_maxhorz = FALSE;
302 toplevel->have_fullscreen = FALSE;
305 gdk_error_trap_push ();
306 XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), GDK_WINDOW_XID (window),
307 gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_STATE"),
308 0, G_MAXLONG, False, XA_ATOM, &type, &format, &nitems,
309 &bytes_after, &data);
310 gdk_error_trap_pop_ignored ();
314 Atom sticky_atom = gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_STATE_STICKY");
315 Atom maxvert_atom = gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_STATE_MAXIMIZED_VERT");
316 Atom maxhorz_atom = gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_STATE_MAXIMIZED_HORZ");
317 Atom fullscreen_atom = gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_STATE_FULLSCREEN");
319 atoms = (Atom *)data;
324 if (atoms[i] == sticky_atom)
325 toplevel->have_sticky = TRUE;
326 else if (atoms[i] == maxvert_atom)
327 toplevel->have_maxvert = TRUE;
328 else if (atoms[i] == maxhorz_atom)
329 toplevel->have_maxhorz = TRUE;
330 else if (atoms[i] == fullscreen_atom)
331 toplevel->have_fullscreen = TRUE;
339 /* When have_sticky is turned on, we have to check the DESKTOP property
342 if (toplevel->have_sticky && !had_sticky)
343 gdk_check_wm_desktop_changed (window);
345 do_net_wm_state_changes (window);
349 get_event_window (GdkEventTranslator *translator,
355 display = (GdkDisplay *) translator;
357 switch (xevent->type)
360 xwindow = xevent->xdestroywindow.window;
363 xwindow = xevent->xunmap.window;
366 xwindow = xevent->xmap.window;
368 case ConfigureNotify:
369 xwindow = xevent->xconfigure.window;
372 xwindow = xevent->xany.window;
375 return gdk_window_lookup_for_display (display, xwindow);
379 gdk_display_x11_translate_event (GdkEventTranslator *translator,
385 GdkWindowObject *window_private;
386 GdkWindowImplX11 *window_impl = NULL;
387 GdkScreen *screen = NULL;
388 GdkScreenX11 *screen_x11 = NULL;
389 GdkToplevelX11 *toplevel = NULL;
390 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
392 Window xwindow = None;
394 /* Find the GdkWindow that this event relates to.
395 * Basically this means substructure events
396 * are reported same as structure events
398 window = get_event_window (translator, xevent);
399 window_private = (GdkWindowObject *) window;
403 /* We may receive events such as NoExpose/GraphicsExpose
404 * and ShmCompletion for pixmaps
406 if (!GDK_IS_WINDOW (window))
409 screen = GDK_WINDOW_SCREEN (window);
410 screen_x11 = GDK_SCREEN_X11 (screen);
411 toplevel = _gdk_x11_window_get_toplevel (window);
412 window_impl = GDK_WINDOW_IMPL_X11 (window_private->impl);
413 xwindow = GDK_WINDOW_XID (window);
415 g_object_ref (window);
418 event->any.window = window;
419 event->any.send_event = xevent->xany.send_event ? TRUE : FALSE;
421 if (window_private && GDK_WINDOW_DESTROYED (window))
423 if (xevent->type != DestroyNotify)
430 if (xevent->type == DestroyNotify)
434 n = gdk_display_get_n_screens (display);
435 for (i = 0; i < n; i++)
437 screen = gdk_display_get_screen (display, i);
438 screen_x11 = GDK_SCREEN_X11 (screen);
440 if (screen_x11->wmspec_check_window == xwindow)
442 screen_x11->wmspec_check_window = None;
443 screen_x11->last_wmspec_check_time = 0;
444 g_free (screen_x11->window_manager_name);
445 screen_x11->window_manager_name = g_strdup ("unknown");
447 /* careful, reentrancy */
448 _gdk_x11_screen_window_manager_changed (screen);
456 /* We do a "manual" conversion of the XEvent to a
457 * GdkEvent. The structures are mostly the same so
458 * the conversion is fairly straightforward. We also
459 * optionally print debugging info regarding events
465 switch (xevent->type)
469 g_message ("keymap notify"));
471 /* Not currently handled */
477 g_message ("expose:\t\twindow: %ld %d x,y: %d %d w,h: %d %d%s",
478 xevent->xexpose.window, xevent->xexpose.count,
479 xevent->xexpose.x, xevent->xexpose.y,
480 xevent->xexpose.width, xevent->xexpose.height,
481 event->any.send_event ? " (send)" : ""));
483 if (window_private == NULL)
490 GdkRectangle expose_rect;
492 expose_rect.x = xevent->xexpose.x;
493 expose_rect.y = xevent->xexpose.y;
494 expose_rect.width = xevent->xexpose.width;
495 expose_rect.height = xevent->xexpose.height;
497 _gdk_window_process_expose (window, xevent->xexpose.serial, &expose_rect);
505 GdkRectangle expose_rect;
508 g_message ("graphics expose:\tdrawable: %ld",
509 xevent->xgraphicsexpose.drawable));
511 if (window_private == NULL)
517 expose_rect.x = xevent->xgraphicsexpose.x;
518 expose_rect.y = xevent->xgraphicsexpose.y;
519 expose_rect.width = xevent->xgraphicsexpose.width;
520 expose_rect.height = xevent->xgraphicsexpose.height;
522 _gdk_window_process_expose (window, xevent->xgraphicsexpose.serial, &expose_rect);
529 g_message ("no expose:\t\tdrawable: %ld",
530 xevent->xnoexpose.drawable));
532 event->no_expose.type = GDK_NO_EXPOSE;
533 event->no_expose.window = window;
537 case VisibilityNotify:
538 #ifdef G_ENABLE_DEBUG
539 if (_gdk_debug_flags & GDK_DEBUG_EVENTS)
540 switch (xevent->xvisibility.state)
542 case VisibilityFullyObscured:
543 g_message ("visibility notify:\twindow: %ld none",
544 xevent->xvisibility.window);
546 case VisibilityPartiallyObscured:
547 g_message ("visibility notify:\twindow: %ld partial",
548 xevent->xvisibility.window);
550 case VisibilityUnobscured:
551 g_message ("visibility notify:\twindow: %ld full",
552 xevent->xvisibility.window);
555 #endif /* G_ENABLE_DEBUG */
557 if (window_private == NULL)
563 event->visibility.type = GDK_VISIBILITY_NOTIFY;
564 event->visibility.window = window;
566 switch (xevent->xvisibility.state)
568 case VisibilityFullyObscured:
569 event->visibility.state = GDK_VISIBILITY_FULLY_OBSCURED;
572 case VisibilityPartiallyObscured:
573 event->visibility.state = GDK_VISIBILITY_PARTIAL;
576 case VisibilityUnobscured:
577 event->visibility.state = GDK_VISIBILITY_UNOBSCURED;
585 g_message ("create notify:\twindow: %ld x,y: %d %d w,h: %d %d b-w: %d parent: %ld ovr: %d",
586 xevent->xcreatewindow.window,
587 xevent->xcreatewindow.x,
588 xevent->xcreatewindow.y,
589 xevent->xcreatewindow.width,
590 xevent->xcreatewindow.height,
591 xevent->xcreatewindow.border_width,
592 xevent->xcreatewindow.parent,
593 xevent->xcreatewindow.override_redirect));
594 /* not really handled */
599 g_message ("destroy notify:\twindow: %ld",
600 xevent->xdestroywindow.window));
602 /* Ignore DestroyNotify from SubstructureNotifyMask */
603 if (xevent->xdestroywindow.window == xevent->xdestroywindow.event)
605 event->any.type = GDK_DESTROY;
606 event->any.window = window;
608 return_val = window_private && !GDK_WINDOW_DESTROYED (window);
610 if (window && GDK_WINDOW_XID (window) != screen_x11->xroot_window)
611 gdk_window_destroy_notify (window);
620 g_message ("unmap notify:\t\twindow: %ld",
621 xevent->xmap.window));
623 event->any.type = GDK_UNMAP;
624 event->any.window = window;
626 /* If we are shown (not withdrawn) and get an unmap, it means we
627 * were iconified in the X sense. If we are withdrawn, and get
628 * an unmap, it means we hid the window ourselves, so we
629 * will have already flipped the iconified bit off.
633 if (GDK_WINDOW_IS_MAPPED (window))
634 gdk_synthesize_window_state (window,
636 GDK_WINDOW_STATE_ICONIFIED);
638 _gdk_xgrab_check_unmap (window, xevent->xany.serial);
645 g_message ("map notify:\t\twindow: %ld",
646 xevent->xmap.window));
648 event->any.type = GDK_MAP;
649 event->any.window = window;
651 /* Unset iconified if it was set */
652 if (window && (((GdkWindowObject*)window)->state & GDK_WINDOW_STATE_ICONIFIED))
653 gdk_synthesize_window_state (window,
654 GDK_WINDOW_STATE_ICONIFIED,
661 g_message ("reparent notify:\twindow: %ld x,y: %d %d parent: %ld ovr: %d",
662 xevent->xreparent.window,
665 xevent->xreparent.parent,
666 xevent->xreparent.override_redirect));
668 /* Not currently handled */
672 case ConfigureNotify:
674 g_message ("configure notify:\twindow: %ld x,y: %d %d w,h: %d %d b-w: %d above: %ld ovr: %d%s",
675 xevent->xconfigure.window,
676 xevent->xconfigure.x,
677 xevent->xconfigure.y,
678 xevent->xconfigure.width,
679 xevent->xconfigure.height,
680 xevent->xconfigure.border_width,
681 xevent->xconfigure.above,
682 xevent->xconfigure.override_redirect,
685 : GDK_WINDOW_TYPE (window) == GDK_WINDOW_CHILD
686 ? " (discarding child)"
687 : xevent->xconfigure.event != xevent->xconfigure.window
688 ? " (discarding substructure)"
690 if (window && GDK_WINDOW_TYPE (window) == GDK_WINDOW_ROOT)
692 window_private->width = xevent->xconfigure.width;
693 window_private->height = xevent->xconfigure.height;
695 _gdk_window_update_size (window);
696 _gdk_x11_window_update_size (GDK_WINDOW_IMPL_X11 (window_private->impl));
697 _gdk_x11_screen_size_changed (screen, xevent);
701 if (toplevel && display_x11->use_sync && !XSyncValueIsZero (toplevel->pending_counter_value))
703 toplevel->current_counter_value = toplevel->pending_counter_value;
704 XSyncIntToValue (&toplevel->pending_counter_value, 0);
709 xevent->xconfigure.event != xevent->xconfigure.window ||
710 GDK_WINDOW_TYPE (window) == GDK_WINDOW_CHILD ||
711 GDK_WINDOW_TYPE (window) == GDK_WINDOW_ROOT)
715 event->configure.type = GDK_CONFIGURE;
716 event->configure.window = window;
717 event->configure.width = xevent->xconfigure.width;
718 event->configure.height = xevent->xconfigure.height;
720 if (!xevent->xconfigure.send_event &&
721 !xevent->xconfigure.override_redirect &&
722 !GDK_WINDOW_DESTROYED (window))
726 Window child_window = 0;
728 gdk_error_trap_push ();
729 if (XTranslateCoordinates (GDK_WINDOW_XDISPLAY (window),
730 GDK_WINDOW_XID (window),
731 screen_x11->xroot_window,
736 event->configure.x = tx;
737 event->configure.y = ty;
739 gdk_error_trap_pop_ignored ();
743 event->configure.x = xevent->xconfigure.x;
744 event->configure.y = xevent->xconfigure.y;
746 window_private->x = event->configure.x;
747 window_private->y = event->configure.y;
748 window_private->width = xevent->xconfigure.width;
749 window_private->height = xevent->xconfigure.height;
751 _gdk_window_update_size (window);
752 _gdk_x11_window_update_size (GDK_WINDOW_IMPL_X11 (window_private->impl));
754 if (window_private->resize_count >= 1)
756 window_private->resize_count -= 1;
758 if (window_private->resize_count == 0)
759 _gdk_moveresize_configure_done (display, window);
766 g_message ("property notify:\twindow: %ld, atom(%ld): %s%s%s",
767 xevent->xproperty.window,
768 xevent->xproperty.atom,
770 gdk_x11_get_xatom_name_for_display (display, xevent->xproperty.atom),
773 if (window_private == NULL)
779 /* We compare with the serial of the last time we mapped the
780 * window to avoid refetching properties that we set ourselves
783 xevent->xproperty.serial >= toplevel->map_serial)
785 if (xevent->xproperty.atom == gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_STATE"))
786 gdk_check_wm_state_changed (window);
788 if (xevent->xproperty.atom == gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_DESKTOP"))
789 gdk_check_wm_desktop_changed (window);
792 if (window_private->event_mask & GDK_PROPERTY_CHANGE_MASK)
794 event->property.type = GDK_PROPERTY_NOTIFY;
795 event->property.window = window;
796 event->property.atom = gdk_x11_xatom_to_atom_for_display (display, xevent->xproperty.atom);
797 event->property.time = xevent->xproperty.time;
798 event->property.state = xevent->xproperty.state;
807 g_message ("selection clear:\twindow: %ld",
808 xevent->xproperty.window));
810 if (_gdk_selection_filter_clear_event (&xevent->xselectionclear))
812 event->selection.type = GDK_SELECTION_CLEAR;
813 event->selection.window = window;
814 event->selection.selection = gdk_x11_xatom_to_atom_for_display (display, xevent->xselectionclear.selection);
815 event->selection.time = xevent->xselectionclear.time;
822 case SelectionRequest:
824 g_message ("selection request:\twindow: %ld",
825 xevent->xproperty.window));
827 event->selection.type = GDK_SELECTION_REQUEST;
828 event->selection.window = window;
829 event->selection.selection = gdk_x11_xatom_to_atom_for_display (display, xevent->xselectionrequest.selection);
830 event->selection.target = gdk_x11_xatom_to_atom_for_display (display, xevent->xselectionrequest.target);
831 event->selection.property = gdk_x11_xatom_to_atom_for_display (display, xevent->xselectionrequest.property);
832 event->selection.requestor = xevent->xselectionrequest.requestor;
833 event->selection.time = xevent->xselectionrequest.time;
837 case SelectionNotify:
839 g_message ("selection notify:\twindow: %ld",
840 xevent->xproperty.window));
842 event->selection.type = GDK_SELECTION_NOTIFY;
843 event->selection.window = window;
844 event->selection.selection = gdk_x11_xatom_to_atom_for_display (display, xevent->xselection.selection);
845 event->selection.target = gdk_x11_xatom_to_atom_for_display (display, xevent->xselection.target);
846 if (xevent->xselection.property == None)
847 event->selection.property = GDK_NONE;
849 event->selection.property = gdk_x11_xatom_to_atom_for_display (display, xevent->xselection.property);
850 event->selection.time = xevent->xselection.time;
856 g_message ("colormap notify:\twindow: %ld",
857 xevent->xcolormap.window));
859 /* Not currently handled */
866 GdkFilterReturn result = GDK_FILTER_CONTINUE;
867 GdkAtom message_type = gdk_x11_xatom_to_atom_for_display (display, xevent->xclient.message_type);
870 g_message ("client message:\twindow: %ld",
871 xevent->xclient.window));
873 tmp_list = display_x11->client_filters;
876 GdkClientFilter *filter = tmp_list->data;
877 tmp_list = tmp_list->next;
879 if (filter->type == message_type)
881 result = (*filter->function) (xevent, event, filter->data);
882 if (result != GDK_FILTER_CONTINUE)
889 case GDK_FILTER_REMOVE:
892 case GDK_FILTER_TRANSLATE:
895 case GDK_FILTER_CONTINUE:
896 /* Send unknown ClientMessage's on to Gtk for it to use */
897 if (window_private == NULL)
903 event->client.type = GDK_CLIENT_EVENT;
904 event->client.window = window;
905 event->client.message_type = message_type;
906 event->client.data_format = xevent->xclient.format;
907 memcpy(&event->client.data, &xevent->xclient.data,
908 sizeof(event->client.data));
918 g_message ("mapping notify"));
920 /* Let XLib know that there is a new keyboard mapping.
922 XRefreshKeyboardMapping (&xevent->xmapping);
923 _gdk_keymap_keys_changed (display);
929 if (xevent->type - display_x11->xfixes_event_base == XFixesSelectionNotify)
931 XFixesSelectionNotifyEvent *selection_notify = (XFixesSelectionNotifyEvent *)xevent;
933 _gdk_x11_screen_process_owner_change (screen, xevent);
935 event->owner_change.type = GDK_OWNER_CHANGE;
936 event->owner_change.window = window;
937 event->owner_change.owner = selection_notify->owner;
938 event->owner_change.reason = selection_notify->subtype;
939 event->owner_change.selection =
940 gdk_x11_xatom_to_atom_for_display (display,
941 selection_notify->selection);
942 event->owner_change.time = selection_notify->timestamp;
943 event->owner_change.selection_time = selection_notify->selection_timestamp;
950 if (xevent->type - display_x11->xrandr_event_base == RRScreenChangeNotify ||
951 xevent->type - display_x11->xrandr_event_base == RRNotify)
954 _gdk_x11_screen_size_changed (screen, xevent);
958 #if defined(HAVE_XCOMPOSITE) && defined (HAVE_XDAMAGE) && defined (HAVE_XFIXES)
959 if (display_x11->have_xdamage && window_private && window_private->composited &&
960 xevent->type == display_x11->xdamage_event_base + XDamageNotify &&
961 ((XDamageNotifyEvent *) xevent)->damage == window_impl->damage)
963 XDamageNotifyEvent *damage_event = (XDamageNotifyEvent *) xevent;
964 XserverRegion repair;
967 rect.x = window_private->x + damage_event->area.x;
968 rect.y = window_private->y + damage_event->area.y;
969 rect.width = damage_event->area.width;
970 rect.height = damage_event->area.height;
972 repair = XFixesCreateRegion (display_x11->xdisplay,
973 &damage_event->area, 1);
974 XDamageSubtract (display_x11->xdisplay,
977 XFixesDestroyRegion (display_x11->xdisplay, repair);
979 if (window_private->parent != NULL)
980 _gdk_window_process_expose (GDK_WINDOW (window_private->parent),
981 damage_event->serial, &rect);
988 if (xevent->type == display_x11->xkb_event_type)
990 XkbEvent *xkb_event = (XkbEvent *) xevent;
992 switch (xkb_event->any.xkb_type)
994 case XkbNewKeyboardNotify:
996 _gdk_keymap_keys_changed (display);
1001 case XkbStateNotify:
1002 _gdk_keymap_state_changed (display, xevent);
1014 if (event->any.window)
1015 g_object_ref (event->any.window);
1019 /* Mark this event as having no resources to be freed */
1020 event->any.window = NULL;
1021 event->any.type = GDK_NOTHING;
1025 g_object_unref (window);
1030 static GdkFilterReturn
1031 gdk_wm_protocols_filter (GdkXEvent *xev,
1035 XEvent *xevent = (XEvent *)xev;
1036 GdkWindow *win = event->any.window;
1037 GdkDisplay *display;
1041 return GDK_FILTER_REMOVE;
1043 display = GDK_WINDOW_DISPLAY (win);
1044 atom = (Atom)xevent->xclient.data.l[0];
1046 if (atom == gdk_x11_get_xatom_by_name_for_display (display, "WM_DELETE_WINDOW"))
1048 /* The delete window request specifies a window
1049 * to delete. We don't actually destroy the
1050 * window because "it is only a request". (The
1051 * window might contain vital data that the
1052 * program does not want destroyed). Instead
1053 * the event is passed along to the program,
1054 * which should then destroy the window.
1057 g_message ("delete window:\t\twindow: %ld",
1058 xevent->xclient.window));
1060 event->any.type = GDK_DELETE;
1062 gdk_x11_window_set_user_time (win, xevent->xclient.data.l[1]);
1064 return GDK_FILTER_TRANSLATE;
1066 else if (atom == gdk_x11_get_xatom_by_name_for_display (display, "WM_TAKE_FOCUS"))
1068 GdkToplevelX11 *toplevel = _gdk_x11_window_get_toplevel (event->any.window);
1069 GdkWindowObject *private = (GdkWindowObject *)win;
1071 /* There is no way of knowing reliably whether we are viewable;
1072 * _gdk_x11_set_input_focus_safe() traps errors asynchronously.
1074 if (toplevel && private->accept_focus)
1075 _gdk_x11_set_input_focus_safe (display, toplevel->focus_window,
1077 xevent->xclient.data.l[1]);
1079 return GDK_FILTER_REMOVE;
1081 else if (atom == gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_PING") &&
1082 !_gdk_x11_display_is_root_window (display,
1083 xevent->xclient.window))
1085 XClientMessageEvent xclient = xevent->xclient;
1087 xclient.window = GDK_WINDOW_XROOTWIN (win);
1088 XSendEvent (GDK_WINDOW_XDISPLAY (win),
1091 SubstructureRedirectMask | SubstructureNotifyMask, (XEvent *)&xclient);
1093 return GDK_FILTER_REMOVE;
1095 else if (atom == gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_SYNC_REQUEST") &&
1096 GDK_DISPLAY_X11 (display)->use_sync)
1098 GdkToplevelX11 *toplevel = _gdk_x11_window_get_toplevel (event->any.window);
1102 XSyncIntsToValue (&toplevel->pending_counter_value,
1103 xevent->xclient.data.l[2],
1104 xevent->xclient.data.l[3]);
1107 return GDK_FILTER_REMOVE;
1110 return GDK_FILTER_CONTINUE;
1114 _gdk_event_init (GdkDisplay *display)
1116 GdkDisplayX11 *display_x11;
1117 GdkDeviceManager *device_manager;
1119 display_x11 = GDK_DISPLAY_X11 (display);
1120 display_x11->event_source = gdk_event_source_new (display);
1122 gdk_event_source_add_translator ((GdkEventSource *) display_x11->event_source,
1123 GDK_EVENT_TRANSLATOR (display));
1125 device_manager = gdk_display_get_device_manager (display);
1126 gdk_event_source_add_translator ((GdkEventSource *) display_x11->event_source,
1127 GDK_EVENT_TRANSLATOR (device_manager));
1129 gdk_display_add_client_message_filter (display,
1130 gdk_atom_intern_static_string ("WM_PROTOCOLS"),
1131 gdk_wm_protocols_filter,
1136 _gdk_input_init (GdkDisplay *display)
1138 GdkDisplayX11 *display_x11;
1139 GdkDeviceManager *device_manager;
1143 display_x11 = GDK_DISPLAY_X11 (display);
1144 device_manager = gdk_display_get_device_manager (display);
1146 /* For backwards compatibility, just add
1147 * floating devices that are not keyboards.
1149 list = gdk_device_manager_list_devices (device_manager, GDK_DEVICE_TYPE_FLOATING);
1151 for (l = list; l; l = l->next)
1155 if (gdk_device_get_source (device) == GDK_SOURCE_KEYBOARD)
1158 display_x11->input_devices = g_list_prepend (display_x11->input_devices,
1159 g_object_ref (l->data));
1164 /* Now set "core" pointer to the first
1165 * master device that is a pointer.
1167 list = gdk_device_manager_list_devices (device_manager, GDK_DEVICE_TYPE_MASTER);
1169 for (l = list; l; l = l->next)
1171 device = list->data;
1173 if (gdk_device_get_source (device) != GDK_SOURCE_MOUSE)
1176 display->core_pointer = device;
1180 /* Add the core pointer to the devices list */
1181 display_x11->input_devices = g_list_prepend (display_x11->input_devices,
1182 g_object_ref (display->core_pointer));
1189 * @display_name: the name of the display to open
1193 * Return value: (transfer none): a #GdkDisplay, or %NULL if the display
1194 * could not be opened.
1199 gdk_display_open (const gchar *display_name)
1202 GdkDisplay *display;
1203 GdkDisplayX11 *display_x11;
1207 const char *sm_client_id;
1209 XClassHint *class_hint;
1215 xdisplay = XOpenDisplay (display_name);
1219 display = g_object_new (GDK_TYPE_DISPLAY_X11, NULL);
1220 display_x11 = GDK_DISPLAY_X11 (display);
1222 display_x11->xdisplay = xdisplay;
1225 /* Set up handlers for Xlib internal connections */
1226 XAddConnectionWatch (xdisplay, gdk_internal_connection_watch, NULL);
1227 #endif /* HAVE_X11R6 */
1229 _gdk_x11_precache_atoms (display, precache_atoms, G_N_ELEMENTS (precache_atoms));
1231 /* RandR must be initialized before we initialize the screens */
1232 display_x11->have_randr13 = FALSE;
1234 if (XRRQueryExtension (display_x11->xdisplay,
1235 &display_x11->xrandr_event_base, &ignore))
1239 XRRQueryVersion (display_x11->xdisplay, &major, &minor);
1241 if ((major == 1 && minor >= 3) || major > 1)
1242 display_x11->have_randr13 = TRUE;
1244 gdk_x11_register_standard_event_type (display, display_x11->xrandr_event_base, RRNumberEvents);
1248 /* initialize the display's screens */
1249 display_x11->screens = g_new (GdkScreen *, ScreenCount (display_x11->xdisplay));
1250 for (i = 0; i < ScreenCount (display_x11->xdisplay); i++)
1251 display_x11->screens[i] = _gdk_x11_screen_new (display, i);
1253 /* We need to initialize events after we have the screen
1254 * structures in places
1256 for (i = 0; i < ScreenCount (display_x11->xdisplay); i++)
1257 _gdk_screen_x11_events_init (display_x11->screens[i]);
1259 /*set the default screen */
1260 display_x11->default_screen = display_x11->screens[DefaultScreen (display_x11->xdisplay)];
1262 display->device_manager = _gdk_device_manager_new (display);
1264 _gdk_event_init (display);
1266 attr.window_type = GDK_WINDOW_TOPLEVEL;
1267 attr.wclass = GDK_INPUT_OUTPUT;
1272 attr.event_mask = 0;
1274 display_x11->leader_gdk_window = gdk_window_new (GDK_SCREEN_X11 (display_x11->default_screen)->root_window,
1275 &attr, GDK_WA_X | GDK_WA_Y);
1276 (_gdk_x11_window_get_toplevel (display_x11->leader_gdk_window))->is_leader = TRUE;
1278 display_x11->leader_window = GDK_WINDOW_XID (display_x11->leader_gdk_window);
1280 display_x11->leader_window_title_set = FALSE;
1283 if (XFixesQueryExtension (display_x11->xdisplay,
1284 &display_x11->xfixes_event_base,
1287 display_x11->have_xfixes = TRUE;
1289 gdk_x11_register_standard_event_type (display,
1290 display_x11->xfixes_event_base,
1291 XFixesNumberEvents);
1295 display_x11->have_xfixes = FALSE;
1297 #ifdef HAVE_XCOMPOSITE
1298 if (XCompositeQueryExtension (display_x11->xdisplay,
1303 XCompositeQueryVersion (display_x11->xdisplay, &major, &minor);
1305 /* Prior to Composite version 0.4, composited windows clipped their
1306 * parents, so you had to use IncludeInferiors to draw to the parent
1307 * This isn't useful for our purposes, so require 0.4
1309 display_x11->have_xcomposite = major > 0 || (major == 0 && minor >= 4);
1313 display_x11->have_xcomposite = FALSE;
1316 if (XDamageQueryExtension (display_x11->xdisplay,
1317 &display_x11->xdamage_event_base,
1320 display_x11->have_xdamage = TRUE;
1322 gdk_x11_register_standard_event_type (display,
1323 display_x11->xdamage_event_base,
1324 XDamageNumberEvents);
1328 display_x11->have_xdamage = FALSE;
1330 display_x11->have_shapes = FALSE;
1331 display_x11->have_input_shapes = FALSE;
1333 if (XShapeQueryExtension (GDK_DISPLAY_XDISPLAY (display), &display_x11->shape_event_base, &ignore))
1335 display_x11->have_shapes = TRUE;
1337 if (XShapeQueryVersion (GDK_DISPLAY_XDISPLAY (display), &maj, &min))
1338 display_x11->have_input_shapes = (maj == 1 && min >= 1);
1342 display_x11->trusted_client = TRUE;
1345 int rootx, rooty, winx, winy;
1348 gdk_error_trap_push ();
1349 XQueryPointer (display_x11->xdisplay,
1350 GDK_SCREEN_X11 (display_x11->default_screen)->xroot_window,
1351 &root, &child, &rootx, &rooty, &winx, &winy, &xmask);
1352 if (G_UNLIKELY (gdk_error_trap_pop () == BadWindow))
1354 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));
1355 display_x11->trusted_client = FALSE;
1359 if (_gdk_synchronize)
1360 XSynchronize (display_x11->xdisplay, True);
1362 class_hint = XAllocClassHint();
1363 class_hint->res_name = g_get_prgname ();
1365 class_hint->res_class = (char *)gdk_get_program_class ();
1367 /* XmbSetWMProperties sets the RESOURCE_NAME environment variable
1368 * from argv[0], so we just synthesize an argument array here.
1371 argv[0] = g_get_prgname ();
1373 XmbSetWMProperties (display_x11->xdisplay,
1374 display_x11->leader_window,
1375 NULL, NULL, argv, argc, NULL, NULL,
1379 sm_client_id = _gdk_get_sm_client_id ();
1381 _gdk_windowing_display_set_sm_client_id (display, sm_client_id);
1384 XChangeProperty (display_x11->xdisplay,
1385 display_x11->leader_window,
1386 gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_PID"),
1387 XA_CARDINAL, 32, PropModeReplace, (guchar *) & pid, 1);
1389 /* We don't yet know a valid time. */
1390 display_x11->user_time = 0;
1394 gint xkb_major = XkbMajorVersion;
1395 gint xkb_minor = XkbMinorVersion;
1396 if (XkbLibraryVersion (&xkb_major, &xkb_minor))
1398 xkb_major = XkbMajorVersion;
1399 xkb_minor = XkbMinorVersion;
1401 if (XkbQueryExtension (display_x11->xdisplay,
1402 NULL, &display_x11->xkb_event_type, NULL,
1403 &xkb_major, &xkb_minor))
1405 Bool detectable_autorepeat_supported;
1407 display_x11->use_xkb = TRUE;
1409 XkbSelectEvents (display_x11->xdisplay,
1411 XkbNewKeyboardNotifyMask | XkbMapNotifyMask | XkbStateNotifyMask,
1412 XkbNewKeyboardNotifyMask | XkbMapNotifyMask | XkbStateNotifyMask);
1414 /* keep this in sync with _gdk_keymap_state_changed() */
1415 XkbSelectEventDetails (display_x11->xdisplay,
1416 XkbUseCoreKbd, XkbStateNotify,
1417 XkbAllStateComponentsMask,
1418 XkbGroupLockMask|XkbModifierLockMask);
1420 XkbSetDetectableAutoRepeat (display_x11->xdisplay,
1422 &detectable_autorepeat_supported);
1424 GDK_NOTE (MISC, g_message ("Detectable autorepeat %s.",
1425 detectable_autorepeat_supported ?
1426 "supported" : "not supported"));
1428 display_x11->have_xkb_autorepeat = detectable_autorepeat_supported;
1434 display_x11->use_sync = FALSE;
1438 int error_base, event_base;
1440 if (XSyncQueryExtension (display_x11->xdisplay,
1441 &event_base, &error_base) &&
1442 XSyncInitialize (display_x11->xdisplay,
1444 display_x11->use_sync = TRUE;
1448 _gdk_input_init (display);
1449 _gdk_dnd_init (display);
1451 for (i = 0; i < ScreenCount (display_x11->xdisplay); i++)
1452 _gdk_x11_screen_setup (display_x11->screens[i]);
1454 g_signal_emit_by_name (display, "opened");
1455 g_signal_emit_by_name (gdk_display_manager_get (), "display-opened", display);
1462 * XLib internal connection handling
1464 typedef struct _GdkInternalConnection GdkInternalConnection;
1466 struct _GdkInternalConnection
1474 process_internal_connection (GIOChannel *gioc,
1478 GdkInternalConnection *connection = (GdkInternalConnection *)data;
1480 GDK_THREADS_ENTER ();
1482 XProcessInternalConnection ((Display*)connection->display, connection->fd);
1484 GDK_THREADS_LEAVE ();
1490 _gdk_windowing_window_get_next_serial (GdkDisplay *display)
1492 return NextRequest (GDK_DISPLAY_XDISPLAY (display));
1496 static GdkInternalConnection *
1497 gdk_add_connection_handler (Display *display,
1500 GIOChannel *io_channel;
1501 GdkInternalConnection *connection;
1503 connection = g_new (GdkInternalConnection, 1);
1505 connection->fd = fd;
1506 connection->display = display;
1508 io_channel = g_io_channel_unix_new (fd);
1510 connection->source = g_io_create_watch (io_channel, G_IO_IN);
1511 g_source_set_callback (connection->source,
1512 (GSourceFunc)process_internal_connection, connection, NULL);
1513 g_source_attach (connection->source, NULL);
1515 g_io_channel_unref (io_channel);
1521 gdk_remove_connection_handler (GdkInternalConnection *connection)
1523 g_source_destroy (connection->source);
1524 g_free (connection);
1528 gdk_internal_connection_watch (Display *display,
1532 XPointer *watch_data)
1535 *watch_data = (XPointer)gdk_add_connection_handler (display, fd);
1537 gdk_remove_connection_handler ((GdkInternalConnection *)*watch_data);
1539 #endif /* HAVE_X11R6 */
1542 * gdk_display_get_name:
1543 * @display: a #GdkDisplay
1545 * Gets the name of the display.
1547 * Returns: a string representing the display name. This string is owned
1548 * by GDK and should not be modified or freed.
1552 G_CONST_RETURN gchar *
1553 gdk_display_get_name (GdkDisplay *display)
1555 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
1557 return (gchar *) DisplayString (GDK_DISPLAY_X11 (display)->xdisplay);
1561 * gdk_display_get_n_screens:
1562 * @display: a #GdkDisplay
1564 * Gets the number of screen managed by the @display.
1566 * Returns: number of screens.
1571 gdk_display_get_n_screens (GdkDisplay *display)
1573 g_return_val_if_fail (GDK_IS_DISPLAY (display), 0);
1575 return ScreenCount (GDK_DISPLAY_X11 (display)->xdisplay);
1579 * gdk_display_get_screen:
1580 * @display: a #GdkDisplay
1581 * @screen_num: the screen number
1583 * Returns a screen object for one of the screens of the display.
1585 * Returns: (transfer none): the #GdkScreen object
1590 gdk_display_get_screen (GdkDisplay *display,
1593 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
1594 g_return_val_if_fail (ScreenCount (GDK_DISPLAY_X11 (display)->xdisplay) > screen_num, NULL);
1596 return GDK_DISPLAY_X11 (display)->screens[screen_num];
1600 * gdk_display_get_default_screen:
1601 * @display: a #GdkDisplay
1603 * Get the default #GdkScreen for @display.
1605 * Returns: (transfer none): the default #GdkScreen object for @display
1610 gdk_display_get_default_screen (GdkDisplay *display)
1612 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
1614 return GDK_DISPLAY_X11 (display)->default_screen;
1618 _gdk_x11_display_is_root_window (GdkDisplay *display,
1619 Window xroot_window)
1621 GdkDisplayX11 *display_x11;
1624 g_return_val_if_fail (GDK_IS_DISPLAY (display), FALSE);
1626 display_x11 = GDK_DISPLAY_X11 (display);
1628 for (i = 0; i < ScreenCount (display_x11->xdisplay); i++)
1630 if (GDK_SCREEN_XROOTWIN (display_x11->screens[i]) == xroot_window)
1636 struct XPointerUngrabInfo {
1637 GdkDisplay *display;
1642 device_ungrab_callback (GdkDisplay *display,
1646 GdkDevice *device = data;
1648 _gdk_display_device_grab_update (display, device, serial);
1652 #define XSERVER_TIME_IS_LATER(time1, time2) \
1653 ( (( time1 > time2 ) && ( time1 - time2 < ((guint32)-1)/2 )) || \
1654 (( time1 < time2 ) && ( time2 - time1 > ((guint32)-1)/2 )) \
1658 * gdk_device_ungrab:
1659 * @device: a #GdkDevice
1660 * @time_: a timestap (e.g. %GDK_CURRENT_TIME).
1662 * Release any grab on @device.
1667 gdk_device_ungrab (GdkDevice *device,
1670 GdkDisplay *display;
1672 GdkDeviceGrabInfo *grab;
1673 unsigned long serial;
1675 g_return_if_fail (GDK_IS_DEVICE (device));
1677 display = gdk_device_get_display (device);
1678 xdisplay = GDK_DISPLAY_XDISPLAY (display);
1680 serial = NextRequest (xdisplay);
1682 GDK_DEVICE_GET_CLASS (device)->ungrab (device, time_);
1685 grab = _gdk_display_get_last_device_grab (display, device);
1687 (time_ == GDK_CURRENT_TIME ||
1688 grab->time == GDK_CURRENT_TIME ||
1689 !XSERVER_TIME_IS_LATER (grab->time, time_)))
1691 grab->serial_end = serial;
1692 _gdk_x11_roundtrip_async (display,
1693 device_ungrab_callback,
1700 * @display: a #GdkDisplay
1702 * Emits a short beep on @display
1707 gdk_display_beep (GdkDisplay *display)
1709 g_return_if_fail (GDK_IS_DISPLAY (display));
1712 XkbBell (GDK_DISPLAY_XDISPLAY (display), None, 0, None);
1714 XBell (GDK_DISPLAY_XDISPLAY (display), 0);
1720 * @display: a #GdkDisplay
1722 * Flushes any requests queued for the windowing system and waits until all
1723 * requests have been handled. This is often used for making sure that the
1724 * display is synchronized with the current state of the program. Calling
1725 * gdk_display_sync() before gdk_error_trap_pop() makes sure that any errors
1726 * generated from earlier requests are handled before the error trap is
1729 * This is most useful for X11. On windowing systems where requests are
1730 * handled synchronously, this function will do nothing.
1735 gdk_display_sync (GdkDisplay *display)
1737 g_return_if_fail (GDK_IS_DISPLAY (display));
1739 XSync (GDK_DISPLAY_XDISPLAY (display), False);
1743 * gdk_display_flush:
1744 * @display: a #GdkDisplay
1746 * Flushes any requests queued for the windowing system; this happens automatically
1747 * when the main loop blocks waiting for new events, but if your application
1748 * is drawing without returning control to the main loop, you may need
1749 * to call this function explicitely. A common case where this function
1750 * needs to be called is when an application is executing drawing commands
1751 * from a thread other than the thread where the main loop is running.
1753 * This is most useful for X11. On windowing systems where requests are
1754 * handled synchronously, this function will do nothing.
1759 gdk_display_flush (GdkDisplay *display)
1761 g_return_if_fail (GDK_IS_DISPLAY (display));
1763 if (!display->closed)
1764 XFlush (GDK_DISPLAY_XDISPLAY (display));
1768 * gdk_display_get_default_group:
1769 * @display: a #GdkDisplay
1771 * Returns the default group leader window for all toplevel windows
1772 * on @display. This window is implicitly created by GDK.
1773 * See gdk_window_set_group().
1775 * Return value: (transfer none): The default group leader window
1781 gdk_display_get_default_group (GdkDisplay *display)
1783 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
1785 return GDK_DISPLAY_X11 (display)->leader_gdk_window;
1789 * gdk_x11_display_grab:
1790 * @display: a #GdkDisplay
1792 * Call XGrabServer() on @display.
1793 * To ungrab the display again, use gdk_x11_display_ungrab().
1795 * gdk_x11_display_grab()/gdk_x11_display_ungrab() calls can be nested.
1800 gdk_x11_display_grab (GdkDisplay *display)
1802 GdkDisplayX11 *display_x11;
1804 g_return_if_fail (GDK_IS_DISPLAY (display));
1806 display_x11 = GDK_DISPLAY_X11 (display);
1808 if (display_x11->grab_count == 0)
1809 XGrabServer (display_x11->xdisplay);
1810 display_x11->grab_count++;
1814 * gdk_x11_display_ungrab:
1815 * @display: a #GdkDisplay
1817 * Ungrab @display after it has been grabbed with
1818 * gdk_x11_display_grab().
1823 gdk_x11_display_ungrab (GdkDisplay *display)
1825 GdkDisplayX11 *display_x11;
1827 g_return_if_fail (GDK_IS_DISPLAY (display));
1829 display_x11 = GDK_DISPLAY_X11 (display);;
1830 g_return_if_fail (display_x11->grab_count > 0);
1832 display_x11->grab_count--;
1833 if (display_x11->grab_count == 0)
1835 XUngrabServer (display_x11->xdisplay);
1836 XFlush (display_x11->xdisplay);
1841 gdk_display_x11_dispose (GObject *object)
1843 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (object);
1846 g_list_foreach (display_x11->input_devices, (GFunc) g_object_run_dispose, NULL);
1848 for (i = 0; i < ScreenCount (display_x11->xdisplay); i++)
1849 _gdk_screen_close (display_x11->screens[i]);
1851 if (display_x11->event_source)
1853 g_source_destroy (display_x11->event_source);
1854 g_source_unref (display_x11->event_source);
1855 display_x11->event_source = NULL;
1858 G_OBJECT_CLASS (_gdk_display_x11_parent_class)->dispose (object);
1862 gdk_display_x11_finalize (GObject *object)
1864 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (object);
1868 if (display_x11->keymap)
1869 g_object_unref (display_x11->keymap);
1871 /* Free motif Dnd */
1872 if (display_x11->motif_target_lists)
1874 for (i = 0; i < display_x11->motif_n_target_lists; i++)
1875 g_list_free (display_x11->motif_target_lists[i]);
1876 g_free (display_x11->motif_target_lists);
1879 _gdk_x11_cursor_display_finalize (GDK_DISPLAY_OBJECT(display_x11));
1881 /* Atom Hashtable */
1882 g_hash_table_destroy (display_x11->atom_from_virtual);
1883 g_hash_table_destroy (display_x11->atom_to_virtual);
1886 XDestroyWindow (display_x11->xdisplay, display_x11->leader_window);
1888 /* list of filters for client messages */
1889 g_list_foreach (display_x11->client_filters, (GFunc) g_free, NULL);
1890 g_list_free (display_x11->client_filters);
1892 /* List of event window extraction functions */
1893 g_slist_foreach (display_x11->event_types, (GFunc)g_free, NULL);
1894 g_slist_free (display_x11->event_types);
1896 /* input GdkDevice list */
1897 g_list_foreach (display_x11->input_devices, (GFunc) g_object_unref, NULL);
1898 g_list_free (display_x11->input_devices);
1900 /* input GdkWindow list */
1901 g_list_foreach (display_x11->input_windows, (GFunc) g_free, NULL);
1902 g_list_free (display_x11->input_windows);
1904 /* Free all GdkScreens */
1905 for (i = 0; i < ScreenCount (display_x11->xdisplay); i++)
1906 g_object_unref (display_x11->screens[i]);
1907 g_free (display_x11->screens);
1909 g_free (display_x11->startup_notification_id);
1911 /* X ID hashtable */
1912 g_hash_table_destroy (display_x11->xid_ht);
1914 XCloseDisplay (display_x11->xdisplay);
1917 while (display_x11->error_traps != NULL)
1919 GdkErrorTrap *trap = display_x11->error_traps->data;
1921 display_x11->error_traps =
1922 g_slist_delete_link (display_x11->error_traps,
1923 display_x11->error_traps);
1925 if (trap->end_sequence == 0)
1926 g_warning ("Display finalized with an unpopped error trap");
1928 g_slice_free (GdkErrorTrap, trap);
1931 G_OBJECT_CLASS (_gdk_display_x11_parent_class)->finalize (object);
1935 * gdk_x11_lookup_xdisplay:
1936 * @xdisplay: a pointer to an X Display
1938 * Find the #GdkDisplay corresponding to @display, if any exists.
1940 * Return value: (transfer none): the #GdkDisplay, if found, otherwise %NULL.
1945 gdk_x11_lookup_xdisplay (Display *xdisplay)
1949 for (tmp_list = _gdk_displays; tmp_list; tmp_list = tmp_list->next)
1951 if (GDK_DISPLAY_XDISPLAY (tmp_list->data) == xdisplay)
1952 return tmp_list->data;
1959 * _gdk_x11_display_screen_for_xrootwin:
1960 * @display: a #GdkDisplay
1961 * @xrootwin: window ID for one of of the screen's of the display.
1963 * Given the root window ID of one of the screen's of a #GdkDisplay,
1966 * Return value: the #GdkScreen corresponding to @xrootwin, or %NULL.
1969 _gdk_x11_display_screen_for_xrootwin (GdkDisplay *display,
1974 for (i = 0; i < ScreenCount (GDK_DISPLAY_X11 (display)->xdisplay); i++)
1976 GdkScreen *screen = gdk_display_get_screen (display, i);
1977 if (GDK_SCREEN_XROOTWIN (screen) == xrootwin)
1985 * gdk_x11_display_get_xdisplay:
1986 * @display: a #GdkDisplay
1987 * @returns: an X display.
1989 * Returns the X display of a #GdkDisplay.
1994 gdk_x11_display_get_xdisplay (GdkDisplay *display)
1996 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
1997 return GDK_DISPLAY_X11 (display)->xdisplay;
2001 _gdk_windowing_set_default_display (GdkDisplay *display)
2003 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
2004 const gchar *startup_id;
2009 g_free (display_x11->startup_notification_id);
2010 display_x11->startup_notification_id = NULL;
2012 startup_id = g_getenv ("DESKTOP_STARTUP_ID");
2013 if (startup_id && *startup_id != '\0')
2015 if (!g_utf8_validate (startup_id, -1, NULL))
2016 g_warning ("DESKTOP_STARTUP_ID contains invalid UTF-8");
2018 gdk_x11_display_set_startup_notification_id (display, startup_id);
2020 /* Clear the environment variable so it won't be inherited by
2021 * child processes and confuse things.
2023 g_unsetenv ("DESKTOP_STARTUP_ID");
2028 broadcast_xmessage (GdkDisplay *display,
2029 const char *message_type,
2030 const char *message_type_begin,
2031 const char *message)
2033 Display *xdisplay = GDK_DISPLAY_XDISPLAY (display);
2034 GdkScreen *screen = gdk_display_get_default_screen (display);
2035 GdkWindow *root_window = gdk_screen_get_root_window (screen);
2036 Window xroot_window = GDK_WINDOW_XID (root_window);
2039 Atom type_atom_begin;
2042 if (!G_LIKELY (GDK_DISPLAY_X11 (display)->trusted_client))
2046 XSetWindowAttributes attrs;
2048 attrs.override_redirect = True;
2049 attrs.event_mask = PropertyChangeMask | StructureNotifyMask;
2052 XCreateWindow (xdisplay,
2058 (Visual *)CopyFromParent,
2059 CWOverrideRedirect | CWEventMask,
2063 type_atom = gdk_x11_get_xatom_by_name_for_display (display,
2065 type_atom_begin = gdk_x11_get_xatom_by_name_for_display (display,
2066 message_type_begin);
2069 XClientMessageEvent xclient;
2071 const char *src_end;
2075 memset(&xclient, 0, sizeof (xclient));
2076 xclient.type = ClientMessage;
2077 xclient.message_type = type_atom_begin;
2078 xclient.display =xdisplay;
2079 xclient.window = xwindow;
2083 src_end = message + strlen (message) + 1; /* +1 to include nul byte */
2085 while (src != src_end)
2087 dest = &xclient.data.b[0];
2088 dest_end = dest + 20;
2090 while (dest != dest_end &&
2098 while (dest != dest_end)
2104 XSendEvent (xdisplay,
2108 (XEvent *)&xclient);
2110 xclient.message_type = type_atom;
2114 XDestroyWindow (xdisplay, xwindow);
2119 * gdk_x11_display_broadcast_startup_message:
2120 * @display: a #GdkDisplay
2121 * @message_type: startup notification message type ("new", "change",
2123 * @...: a list of key/value pairs (as strings), terminated by a
2124 * %NULL key. (A %NULL value for a key will cause that key to be
2125 * skipped in the output.)
2127 * Sends a startup notification message of type @message_type to
2130 * This is a convenience function for use by code that implements the
2131 * freedesktop startup notification specification. Applications should
2132 * not normally need to call it directly. See the <ulink
2133 * url="http://standards.freedesktop.org/startup-notification-spec/startup-notification-latest.txt">Startup
2134 * Notification Protocol specification</ulink> for
2135 * definitions of the message types and keys that can be used.
2140 gdk_x11_display_broadcast_startup_message (GdkDisplay *display,
2141 const char *message_type,
2146 const char *key, *value, *p;
2148 message = g_string_new (message_type);
2149 g_string_append_c (message, ':');
2151 va_start (ap, message_type);
2152 while ((key = va_arg (ap, const char *)))
2154 value = va_arg (ap, const char *);
2158 g_string_append_printf (message, " %s=\"", key);
2159 for (p = value; *p; p++)
2166 g_string_append_c (message, '\\');
2170 g_string_append_c (message, *p);
2172 g_string_append_c (message, '\"');
2176 broadcast_xmessage (display,
2177 "_NET_STARTUP_INFO",
2178 "_NET_STARTUP_INFO_BEGIN",
2181 g_string_free (message, TRUE);
2185 * gdk_notify_startup_complete:
2187 * Indicates to the GUI environment that the application has finished
2188 * loading. If the applications opens windows, this function is
2189 * normally called after opening the application's initial set of
2192 * GTK+ will call this function automatically after opening the first
2193 * #GtkWindow unless gtk_window_set_auto_startup_notification() is called
2194 * to disable that feature.
2199 gdk_notify_startup_complete (void)
2201 GdkDisplay *display;
2202 GdkDisplayX11 *display_x11;
2204 display = gdk_display_get_default ();
2208 display_x11 = GDK_DISPLAY_X11 (display);
2210 if (display_x11->startup_notification_id == NULL)
2213 gdk_notify_startup_complete_with_id (display_x11->startup_notification_id);
2217 * gdk_notify_startup_complete_with_id:
2218 * @startup_id: a startup-notification identifier, for which notification
2219 * process should be completed
2221 * Indicates to the GUI environment that the application has finished
2222 * loading, using a given identifier.
2224 * GTK+ will call this function automatically for #GtkWindow with custom
2225 * startup-notification identifier unless
2226 * gtk_window_set_auto_startup_notification() is called to disable
2232 gdk_notify_startup_complete_with_id (const gchar* startup_id)
2234 GdkDisplay *display;
2236 display = gdk_display_get_default ();
2240 gdk_x11_display_broadcast_startup_message (display, "remove",
2246 * gdk_display_supports_selection_notification:
2247 * @display: a #GdkDisplay
2249 * Returns whether #GdkEventOwnerChange events will be
2250 * sent when the owner of a selection changes.
2252 * Return value: whether #GdkEventOwnerChange events will
2258 gdk_display_supports_selection_notification (GdkDisplay *display)
2260 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
2262 return display_x11->have_xfixes;
2266 * gdk_display_request_selection_notification:
2267 * @display: a #GdkDisplay
2268 * @selection: the #GdkAtom naming the selection for which
2269 * ownership change notification is requested
2271 * Request #GdkEventOwnerChange events for ownership changes
2272 * of the selection named by the given atom.
2274 * Return value: whether #GdkEventOwnerChange events will
2280 gdk_display_request_selection_notification (GdkDisplay *display,
2285 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
2288 if (display_x11->have_xfixes)
2290 atom = gdk_x11_atom_to_xatom_for_display (display,
2292 XFixesSelectSelectionInput (display_x11->xdisplay,
2293 display_x11->leader_window,
2295 XFixesSetSelectionOwnerNotifyMask |
2296 XFixesSelectionWindowDestroyNotifyMask |
2297 XFixesSelectionClientCloseNotifyMask);
2306 * gdk_display_supports_clipboard_persistence
2307 * @display: a #GdkDisplay
2309 * Returns whether the speicifed display supports clipboard
2310 * persistance; i.e. if it's possible to store the clipboard data after an
2311 * application has quit. On X11 this checks if a clipboard daemon is
2314 * Returns: %TRUE if the display supports clipboard persistance.
2319 gdk_display_supports_clipboard_persistence (GdkDisplay *display)
2321 Atom clipboard_manager;
2323 /* It might make sense to cache this */
2324 clipboard_manager = gdk_x11_get_xatom_by_name_for_display (display, "CLIPBOARD_MANAGER");
2325 return XGetSelectionOwner (GDK_DISPLAY_X11 (display)->xdisplay, clipboard_manager) != None;
2329 * gdk_display_store_clipboard
2330 * @display: a #GdkDisplay
2331 * @clipboard_window: a #GdkWindow belonging to the clipboard owner
2332 * @time_: a timestamp
2333 * @targets: an array of targets that should be saved, or %NULL
2334 * if all available targets should be saved.
2335 * @n_targets: length of the @targets array
2337 * Issues a request to the clipboard manager to store the
2338 * clipboard data. On X11, this is a special program that works
2339 * according to the freedesktop clipboard specification, available at
2340 * <ulink url="http://www.freedesktop.org/Standards/clipboard-manager-spec">
2341 * http://www.freedesktop.org/Standards/clipboard-manager-spec</ulink>.
2346 gdk_display_store_clipboard (GdkDisplay *display,
2347 GdkWindow *clipboard_window,
2349 const GdkAtom *targets,
2352 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
2353 Atom clipboard_manager, save_targets;
2355 g_return_if_fail (GDK_WINDOW_IS_X11 (clipboard_window));
2357 clipboard_manager = gdk_x11_get_xatom_by_name_for_display (display, "CLIPBOARD_MANAGER");
2358 save_targets = gdk_x11_get_xatom_by_name_for_display (display, "SAVE_TARGETS");
2360 gdk_error_trap_push ();
2362 if (XGetSelectionOwner (display_x11->xdisplay, clipboard_manager) != None)
2364 Atom property_name = None;
2370 property_name = gdk_x11_atom_to_xatom_for_display (display, _gdk_selection_property);
2372 xatoms = g_new (Atom, n_targets);
2373 for (i = 0; i < n_targets; i++)
2374 xatoms[i] = gdk_x11_atom_to_xatom_for_display (display, targets[i]);
2376 XChangeProperty (display_x11->xdisplay, GDK_WINDOW_XID (clipboard_window),
2377 property_name, XA_ATOM,
2378 32, PropModeReplace, (guchar *)xatoms, n_targets);
2383 XConvertSelection (display_x11->xdisplay,
2384 clipboard_manager, save_targets, property_name,
2385 GDK_WINDOW_XID (clipboard_window), time_);
2388 gdk_error_trap_pop_ignored ();
2393 * gdk_x11_display_get_user_time:
2394 * @display: a #GdkDisplay
2396 * Returns the timestamp of the last user interaction on
2397 * @display. The timestamp is taken from events caused
2398 * by user interaction such as key presses or pointer
2399 * movements. See gdk_x11_window_set_user_time().
2401 * Returns: the timestamp of the last user interaction
2406 gdk_x11_display_get_user_time (GdkDisplay *display)
2408 return GDK_DISPLAY_X11 (display)->user_time;
2412 * gdk_display_supports_shapes:
2413 * @display: a #GdkDisplay
2415 * Returns %TRUE if gdk_window_shape_combine_mask() can
2416 * be used to create shaped windows on @display.
2418 * Returns: %TRUE if shaped windows are supported
2423 gdk_display_supports_shapes (GdkDisplay *display)
2425 return GDK_DISPLAY_X11 (display)->have_shapes;
2429 * gdk_display_supports_input_shapes:
2430 * @display: a #GdkDisplay
2432 * Returns %TRUE if gdk_window_input_shape_combine_mask() can
2433 * be used to modify the input shape of windows on @display.
2435 * Returns: %TRUE if windows with modified input shape are supported
2440 gdk_display_supports_input_shapes (GdkDisplay *display)
2442 return GDK_DISPLAY_X11 (display)->have_input_shapes;
2447 * gdk_x11_display_get_startup_notification_id:
2448 * @display: a #GdkDisplay
2450 * Gets the startup notification ID for a display.
2452 * Returns: the startup notification ID for @display
2456 G_CONST_RETURN gchar *
2457 gdk_x11_display_get_startup_notification_id (GdkDisplay *display)
2459 return GDK_DISPLAY_X11 (display)->startup_notification_id;
2463 * gdk_x11_display_set_startup_notification_id:
2464 * @display: a #GdkDisplay
2465 * @startup_id: the startup notification ID (must be valid utf8)
2467 * Sets the startup notification ID for a display.
2469 * This is usually taken from the value of the DESKTOP_STARTUP_ID
2470 * environment variable, but in some cases (such as the application not
2471 * being launched using exec()) it can come from other sources.
2473 * If the ID contains the string "_TIME" then the portion following that
2474 * string is taken to be the X11 timestamp of the event that triggered
2475 * the application to be launched and the GDK current event time is set
2478 * The startup ID is also what is used to signal that the startup is
2479 * complete (for example, when opening a window or when calling
2480 * gdk_notify_startup_complete()).
2485 gdk_x11_display_set_startup_notification_id (GdkDisplay *display,
2486 const gchar *startup_id)
2488 GdkDisplayX11 *display_x11;
2491 display_x11 = GDK_DISPLAY_X11 (display);
2493 g_free (display_x11->startup_notification_id);
2494 display_x11->startup_notification_id = g_strdup (startup_id);
2496 /* Find the launch time from the startup_id, if it's there. Newer spec
2497 * states that the startup_id is of the form <unique>_TIME<timestamp>
2499 time_str = g_strrstr (startup_id, "_TIME");
2500 if (time_str != NULL)
2506 /* Skip past the "_TIME" part */
2509 retval = strtoul (time_str, &end, 0);
2510 if (end != time_str && errno == 0)
2511 display_x11->user_time = retval;
2514 /* Set the startup id on the leader window so it
2515 * applies to all windows we create on this display
2517 XChangeProperty (display_x11->xdisplay,
2518 display_x11->leader_window,
2519 gdk_x11_get_xatom_by_name_for_display (display, "_NET_STARTUP_ID"),
2520 gdk_x11_get_xatom_by_name_for_display (display, "UTF8_STRING"), 8,
2522 (guchar *)startup_id, strlen (startup_id));
2526 * gdk_display_supports_composite:
2527 * @display: a #GdkDisplay
2529 * Returns %TRUE if gdk_window_set_composited() can be used
2530 * to redirect drawing on the window using compositing.
2532 * Currently this only works on X11 with XComposite and
2533 * XDamage extensions available.
2535 * Returns: %TRUE if windows may be composited.
2540 gdk_display_supports_composite (GdkDisplay *display)
2542 GdkDisplayX11 *x11_display = GDK_DISPLAY_X11 (display);
2544 return x11_display->have_xcomposite &&
2545 x11_display->have_xdamage &&
2546 x11_display->have_xfixes;
2550 * gdk_display_list_devices:
2551 * @display: a #GdkDisplay
2553 * Returns the list of available input devices attached to @display.
2554 * The list is statically allocated and should not be freed.
2556 * Return value: (transfer none) (element-type GdkDevice):
2557 * a list of #GdkDevice
2561 * Deprecated: 3.0: Use gdk_device_manager_list_devices() instead.
2564 gdk_display_list_devices (GdkDisplay *display)
2566 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
2568 return GDK_DISPLAY_X11 (display)->input_devices;
2572 * gdk_event_send_client_message_for_display:
2573 * @display: the #GdkDisplay for the window where the message is to be sent.
2574 * @event: the #GdkEvent to send, which should be a #GdkEventClient.
2575 * @winid: the window to send the client message to.
2577 * On X11, sends an X ClientMessage event to a given window. On
2578 * Windows, sends a message registered with the name
2579 * GDK_WIN32_CLIENT_MESSAGE.
2581 * This could be used for communicating between different
2582 * applications, though the amount of data is limited to 20 bytes on
2583 * X11, and to just four bytes on Windows.
2585 * Returns: non-zero on success.
2590 gdk_event_send_client_message_for_display (GdkDisplay *display,
2592 GdkNativeWindow winid)
2596 g_return_val_if_fail(event != NULL, FALSE);
2598 /* Set up our event to send, with the exception of its target window */
2599 sev.xclient.type = ClientMessage;
2600 sev.xclient.display = GDK_DISPLAY_XDISPLAY (display);
2601 sev.xclient.format = event->client.data_format;
2602 sev.xclient.window = winid;
2603 memcpy(&sev.xclient.data, &event->client.data, sizeof (sev.xclient.data));
2604 sev.xclient.message_type = gdk_x11_atom_to_xatom_for_display (display, event->client.message_type);
2606 return _gdk_send_xevent (display, winid, False, NoEventMask, &sev);
2610 * gdk_display_add_client_message_filter:
2611 * @display: a #GdkDisplay for which this message filter applies
2612 * @message_type: the type of ClientMessage events to receive.
2613 * This will be checked against the @message_type field
2614 * of the XClientMessage event struct.
2615 * @func: the function to call to process the event.
2616 * @data: user data to pass to @func.
2618 * Adds a filter to be called when X ClientMessage events are received.
2619 * See gdk_window_add_filter() if you are interested in filtering other
2625 gdk_display_add_client_message_filter (GdkDisplay *display,
2626 GdkAtom message_type,
2630 GdkClientFilter *filter;
2631 g_return_if_fail (GDK_IS_DISPLAY (display));
2632 filter = g_new (GdkClientFilter, 1);
2634 filter->type = message_type;
2635 filter->function = func;
2636 filter->data = data;
2638 GDK_DISPLAY_X11(display)->client_filters =
2639 g_list_append (GDK_DISPLAY_X11 (display)->client_filters,
2644 * gdk_add_client_message_filter:
2645 * @message_type: the type of ClientMessage events to receive. This will be
2646 * checked against the <structfield>message_type</structfield> field of the
2647 * XClientMessage event struct.
2648 * @func: the function to call to process the event.
2649 * @data: user data to pass to @func.
2651 * Adds a filter to the default display to be called when X ClientMessage events
2652 * are received. See gdk_display_add_client_message_filter().
2655 gdk_add_client_message_filter (GdkAtom message_type,
2659 gdk_display_add_client_message_filter (gdk_display_get_default (),
2660 message_type, func, data);
2664 *--------------------------------------------------------------
2667 * Flushes the Xlib output buffer and then waits
2668 * until all requests have been received and processed
2669 * by the X server. The only real use for this function
2670 * is in dealing with XShm.
2678 *--------------------------------------------------------------
2683 GSList *tmp_list = _gdk_displays;
2687 XSync (GDK_DISPLAY_XDISPLAY (tmp_list->data), False);
2688 tmp_list = tmp_list->next;
2693 * gdk_x11_register_standard_event_type:
2694 * @display: a #GdkDisplay
2695 * @event_base: first event type code to register
2696 * @n_events: number of event type codes to register
2698 * Registers interest in receiving extension events with type codes
2699 * between @event_base and <literal>event_base + n_events - 1</literal>.
2700 * The registered events must have the window field in the same place
2701 * as core X events (this is not the case for e.g. XKB extension events).
2703 * If an event type is registered, events of this type will go through
2704 * global and window-specific filters (see gdk_window_add_filter()).
2705 * Unregistered events will only go through global filters.
2706 * GDK may register the events of some X extensions on its own.
2708 * This function should only be needed in unusual circumstances, e.g.
2709 * when filtering XInput extension events on the root window.
2714 gdk_x11_register_standard_event_type (GdkDisplay *display,
2718 GdkEventTypeX11 *event_type;
2719 GdkDisplayX11 *display_x11;
2721 display_x11 = GDK_DISPLAY_X11 (display);
2722 event_type = g_new (GdkEventTypeX11, 1);
2724 event_type->base = event_base;
2725 event_type->n_events = n_events;
2727 display_x11->event_types = g_slist_prepend (display_x11->event_types, event_type);
2730 /* compare X sequence numbers handling wraparound */
2731 #define SEQUENCE_COMPARE(a,op,b) (((long) (a) - (long) (b)) op 0)
2733 /* delivers an error event from the error handler in gdkmain-x11.c */
2735 _gdk_x11_display_error_event (GdkDisplay *display,
2738 GdkDisplayX11 *display_x11;
2742 display_x11 = GDK_DISPLAY_X11 (display);
2745 for (tmp_list = display_x11->error_traps;
2747 tmp_list = tmp_list->next)
2751 trap = tmp_list->data;
2753 if (SEQUENCE_COMPARE (trap->start_sequence, <=, error->serial) &&
2754 (trap->end_sequence == 0 ||
2755 SEQUENCE_COMPARE (trap->end_sequence, >, error->serial)))
2758 trap->error_code = error->error_code;
2759 break; /* only innermost trap gets the error code */
2768 XGetErrorText (display_x11->xdisplay, error->error_code, buf, 63);
2771 g_strdup_printf ("The program '%s' received an X Window System error.\n"
2772 "This probably reflects a bug in the program.\n"
2773 "The error was '%s'.\n"
2774 " (Details: serial %ld error_code %d request_code %d minor_code %d)\n"
2775 " (Note to programmers: normally, X errors are reported asynchronously;\n"
2776 " that is, you will receive the error a while after causing it.\n"
2777 " To debug your program, run it with the --sync command line\n"
2778 " option to change this behavior. You can then get a meaningful\n"
2779 " backtrace from your debugger if you break on the gdk_x_error() function.)",
2784 error->request_code,
2787 #ifdef G_ENABLE_DEBUG
2788 g_error ("%s", msg);
2789 #else /* !G_ENABLE_DEBUG */
2790 g_warning ("%s\n", msg);
2793 #endif /* G_ENABLE_DEBUG */
2798 delete_outdated_error_traps (GdkDisplayX11 *display_x11)
2801 gulong processed_sequence;
2803 processed_sequence = XLastKnownRequestProcessed (display_x11->xdisplay);
2805 tmp_list = display_x11->error_traps;
2806 while (tmp_list != NULL)
2808 GdkErrorTrap *trap = tmp_list->data;
2810 if (trap->end_sequence != 0 &&
2811 SEQUENCE_COMPARE (trap->end_sequence, <=, processed_sequence))
2813 GSList *free_me = tmp_list;
2815 tmp_list = tmp_list->next;
2816 display_x11->error_traps =
2817 g_slist_delete_link (display_x11->error_traps, free_me);
2818 g_slice_free (GdkErrorTrap, trap);
2822 tmp_list = tmp_list->next;
2828 * gdk_x11_display_error_trap_push:
2829 * @display: a #GdkDisplay
2831 * Begins a range of X requests on @display for which X error events
2832 * will be ignored. Unignored errors (when no trap is pushed) will abort
2833 * the application. Use gdk_x11_display_error_trap_pop() or
2834 * gdk_x11_display_error_trap_pop_ignored()to lift a trap pushed
2835 * with this function.
2837 * See also gdk_error_trap_push() to push a trap on all displays.
2842 gdk_x11_display_error_trap_push (GdkDisplay *display)
2844 GdkDisplayX11 *display_x11;
2847 display_x11 = GDK_DISPLAY_X11 (display);
2849 delete_outdated_error_traps (display_x11);
2851 /* set up the Xlib callback to tell us about errors */
2852 _gdk_x11_error_handler_push ();
2854 trap = g_slice_new0 (GdkErrorTrap);
2856 trap->start_sequence = XNextRequest (display_x11->xdisplay);
2857 trap->error_code = Success;
2859 display_x11->error_traps =
2860 g_slist_prepend (display_x11->error_traps, trap);
2864 gdk_x11_display_error_trap_pop_internal (GdkDisplay *display,
2867 GdkDisplayX11 *display_x11;
2872 display_x11 = GDK_DISPLAY_X11 (display);
2874 g_return_val_if_fail (display_x11->error_traps != NULL, Success);
2876 /* Find the first trap that hasn't been popped already */
2877 trap = NULL; /* quiet gcc */
2878 for (tmp_list = display_x11->error_traps;
2880 tmp_list = tmp_list->next)
2882 trap = tmp_list->data;
2884 if (trap->end_sequence == 0)
2888 g_return_val_if_fail (trap != NULL, Success);
2889 g_assert (trap->end_sequence == 0);
2891 /* May need to sync to fill in trap->error_code if we care about
2892 * getting an error code.
2896 gulong processed_sequence;
2897 gulong next_sequence;
2899 next_sequence = XNextRequest (display_x11->xdisplay);
2900 processed_sequence = XLastKnownRequestProcessed (display_x11->xdisplay);
2902 /* If our last request was already processed, there is no point
2903 * in syncing. i.e. if last request was a round trip (or even if
2904 * we got an event with the serial of a non-round-trip)
2906 if ((next_sequence - 1) != processed_sequence)
2908 XSync (display_x11->xdisplay, False);
2911 result = trap->error_code;
2918 /* record end of trap, giving us a range of
2919 * error sequences we'll ignore.
2921 trap->end_sequence = XNextRequest (display_x11->xdisplay);
2923 /* remove the Xlib callback */
2924 _gdk_x11_error_handler_pop ();
2926 /* we may already be outdated */
2927 delete_outdated_error_traps (display_x11);
2933 * gdk_x11_display_error_trap_pop:
2934 * @display: the display
2936 * Pops the error trap pushed by gdk_x11_display_error_trap_push().
2937 * Will XSync() if necessary and will always block until
2938 * the error is known to have occurred or not occurred,
2939 * so the error code can be returned.
2941 * If you don't need to use the return value,
2942 * gdk_x11_display_error_trap_pop_ignored() would be more efficient.
2944 * See gdk_error_trap_pop() for the all-displays-at-once
2949 * Return value: X error code or 0 on success
2952 gdk_x11_display_error_trap_pop (GdkDisplay *display)
2954 g_return_val_if_fail (GDK_IS_DISPLAY_X11 (display), Success);
2956 return gdk_x11_display_error_trap_pop_internal (display, TRUE);
2960 * gdk_x11_display_error_trap_pop_ignored:
2961 * @display: the display
2963 * Pops the error trap pushed by gdk_x11_display_error_trap_push().
2964 * Does not block to see if an error occurred; merely records the
2965 * range of requests to ignore errors for, and ignores those errors
2966 * if they arrive asynchronously.
2968 * See gdk_error_trap_pop_ignored() for the all-displays-at-once
2974 gdk_x11_display_error_trap_pop_ignored (GdkDisplay *display)
2976 g_return_if_fail (GDK_IS_DISPLAY_X11 (display));
2978 gdk_x11_display_error_trap_pop_internal (display, FALSE);