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.
35 #include "gdkdisplay.h"
36 #include "gdkdisplay-x11.h"
37 #include "gdkeventsource.h"
38 #include "gdkeventtranslator.h"
39 #include "gdkscreen.h"
40 #include "gdkscreen-x11.h"
41 #include "gdkinternals.h"
42 #include "gdkdeviceprivate.h"
43 #include "gdkdevicemanager.h"
44 #include "xsettings-client.h"
46 #include <X11/Xatom.h>
49 #include <X11/XKBlib.h>
53 #include <X11/extensions/Xfixes.h>
56 #include <X11/extensions/shape.h>
58 #ifdef HAVE_XCOMPOSITE
59 #include <X11/extensions/Xcomposite.h>
63 #include <X11/extensions/Xdamage.h>
67 #include <X11/extensions/Xrandr.h>
71 static void gdk_display_x11_dispose (GObject *object);
72 static void gdk_display_x11_finalize (GObject *object);
74 static void gdk_display_x11_event_translator_init (GdkEventTranslatorIface *iface);
76 static gboolean gdk_display_x11_translate_event (GdkEventTranslator *translator,
82 static void gdk_internal_connection_watch (Display *display,
86 XPointer *watch_data);
87 #endif /* HAVE_X11R6 */
89 typedef struct _GdkEventTypeX11 GdkEventTypeX11;
91 struct _GdkEventTypeX11
97 /* Note that we never *directly* use WM_LOCALE_NAME, WM_PROTOCOLS,
98 * but including them here has the side-effect of getting them
99 * into the internal Xlib cache
101 static const char *const precache_atoms[] = {
111 "_NET_ACTIVE_WINDOW",
112 "_NET_CURRENT_DESKTOP",
113 "_NET_FRAME_EXTENTS",
123 "_NET_WM_STATE_ABOVE",
124 "_NET_WM_STATE_BELOW",
125 "_NET_WM_STATE_FULLSCREEN",
126 "_NET_WM_STATE_MODAL",
127 "_NET_WM_STATE_MAXIMIZED_VERT",
128 "_NET_WM_STATE_MAXIMIZED_HORZ",
129 "_NET_WM_STATE_SKIP_TASKBAR",
130 "_NET_WM_STATE_SKIP_PAGER",
131 "_NET_WM_STATE_STICKY",
132 "_NET_WM_SYNC_REQUEST",
133 "_NET_WM_SYNC_REQUEST_COUNTER",
134 "_NET_WM_WINDOW_TYPE",
135 "_NET_WM_WINDOW_TYPE_NORMAL",
140 G_DEFINE_TYPE_WITH_CODE (GdkDisplayX11, _gdk_display_x11, GDK_TYPE_DISPLAY,
141 G_IMPLEMENT_INTERFACE (GDK_TYPE_EVENT_TRANSLATOR,
142 gdk_display_x11_event_translator_init))
146 _gdk_display_x11_class_init (GdkDisplayX11Class * class)
148 GObjectClass *object_class = G_OBJECT_CLASS (class);
150 object_class->dispose = gdk_display_x11_dispose;
151 object_class->finalize = gdk_display_x11_finalize;
155 _gdk_display_x11_init (GdkDisplayX11 *display)
160 gdk_display_x11_event_translator_init (GdkEventTranslatorIface *iface)
162 iface->translate_event = gdk_display_x11_translate_event;
166 do_net_wm_state_changes (GdkWindow *window)
168 GdkToplevelX11 *toplevel = _gdk_x11_window_get_toplevel (window);
169 GdkWindowState old_state;
171 if (GDK_WINDOW_DESTROYED (window) ||
172 gdk_window_get_window_type (window) != GDK_WINDOW_TOPLEVEL)
175 old_state = gdk_window_get_state (window);
177 /* For found_sticky to remain TRUE, we have to also be on desktop
180 if (old_state & GDK_WINDOW_STATE_STICKY)
182 if (!(toplevel->have_sticky && toplevel->on_all_desktops))
183 gdk_synthesize_window_state (window,
184 GDK_WINDOW_STATE_STICKY,
189 if (toplevel->have_sticky || toplevel->on_all_desktops)
190 gdk_synthesize_window_state (window,
192 GDK_WINDOW_STATE_STICKY);
195 if (old_state & GDK_WINDOW_STATE_FULLSCREEN)
197 if (!toplevel->have_fullscreen)
198 gdk_synthesize_window_state (window,
199 GDK_WINDOW_STATE_FULLSCREEN,
204 if (toplevel->have_fullscreen)
205 gdk_synthesize_window_state (window,
207 GDK_WINDOW_STATE_FULLSCREEN);
210 /* Our "maximized" means both vertical and horizontal; if only one,
211 * we don't expose that via GDK
213 if (old_state & GDK_WINDOW_STATE_MAXIMIZED)
215 if (!(toplevel->have_maxvert && toplevel->have_maxhorz))
216 gdk_synthesize_window_state (window,
217 GDK_WINDOW_STATE_MAXIMIZED,
222 if (toplevel->have_maxvert && toplevel->have_maxhorz)
223 gdk_synthesize_window_state (window,
225 GDK_WINDOW_STATE_MAXIMIZED);
230 gdk_check_wm_desktop_changed (GdkWindow *window)
232 GdkToplevelX11 *toplevel = _gdk_x11_window_get_toplevel (window);
233 GdkDisplay *display = GDK_WINDOW_DISPLAY (window);
243 gdk_error_trap_push ();
244 XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display),
245 GDK_WINDOW_XID (window),
246 gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_DESKTOP"),
247 0, G_MAXLONG, False, XA_CARDINAL, &type,
249 &bytes_after, &data);
250 gdk_error_trap_pop ();
254 desktop = (gulong *)data;
255 toplevel->on_all_desktops = (*desktop == 0xFFFFFFFF);
259 toplevel->on_all_desktops = FALSE;
261 do_net_wm_state_changes (window);
265 gdk_check_wm_state_changed (GdkWindow *window)
267 GdkToplevelX11 *toplevel = _gdk_x11_window_get_toplevel (window);
268 GdkDisplay *display = GDK_WINDOW_DISPLAY (window);
278 gboolean had_sticky = toplevel->have_sticky;
280 toplevel->have_sticky = FALSE;
281 toplevel->have_maxvert = FALSE;
282 toplevel->have_maxhorz = FALSE;
283 toplevel->have_fullscreen = FALSE;
286 gdk_error_trap_push ();
287 XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), GDK_WINDOW_XID (window),
288 gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_STATE"),
289 0, G_MAXLONG, False, XA_ATOM, &type, &format, &nitems,
290 &bytes_after, &data);
291 gdk_error_trap_pop ();
295 Atom sticky_atom = gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_STATE_STICKY");
296 Atom maxvert_atom = gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_STATE_MAXIMIZED_VERT");
297 Atom maxhorz_atom = gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_STATE_MAXIMIZED_HORZ");
298 Atom fullscreen_atom = gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_STATE_FULLSCREEN");
300 atoms = (Atom *)data;
305 if (atoms[i] == sticky_atom)
306 toplevel->have_sticky = TRUE;
307 else if (atoms[i] == maxvert_atom)
308 toplevel->have_maxvert = TRUE;
309 else if (atoms[i] == maxhorz_atom)
310 toplevel->have_maxhorz = TRUE;
311 else if (atoms[i] == fullscreen_atom)
312 toplevel->have_fullscreen = TRUE;
320 /* When have_sticky is turned on, we have to check the DESKTOP property
323 if (toplevel->have_sticky && !had_sticky)
324 gdk_check_wm_desktop_changed (window);
326 do_net_wm_state_changes (window);
330 get_event_window (GdkEventTranslator *translator,
336 display = (GdkDisplay *) translator;
338 switch (xevent->type)
341 xwindow = xevent->xdestroywindow.window;
344 xwindow = xevent->xunmap.window;
347 xwindow = xevent->xmap.window;
349 case ConfigureNotify:
350 xwindow = xevent->xconfigure.window;
353 xwindow = xevent->xany.window;
356 return gdk_window_lookup_for_display (display, xwindow);
360 gdk_display_x11_translate_event (GdkEventTranslator *translator,
366 GdkWindowObject *window_private;
367 GdkWindowImplX11 *window_impl = NULL;
368 GdkScreen *screen = NULL;
369 GdkScreenX11 *screen_x11 = NULL;
370 GdkToplevelX11 *toplevel = NULL;
371 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
373 Window xwindow = None;
375 /* Find the GdkWindow that this event relates to.
376 * Basically this means substructure events
377 * are reported same as structure events
379 window = get_event_window (translator, xevent);
380 window_private = (GdkWindowObject *) window;
384 /* We may receive events such as NoExpose/GraphicsExpose
385 * and ShmCompletion for pixmaps
387 if (!GDK_IS_WINDOW (window))
390 screen = GDK_WINDOW_SCREEN (window);
391 screen_x11 = GDK_SCREEN_X11 (screen);
392 toplevel = _gdk_x11_window_get_toplevel (window);
393 window_impl = GDK_WINDOW_IMPL_X11 (window_private->impl);
394 xwindow = GDK_WINDOW_XID (window);
396 g_object_ref (window);
399 event->any.window = window;
400 event->any.send_event = xevent->xany.send_event ? TRUE : FALSE;
402 if (window_private && GDK_WINDOW_DESTROYED (window))
404 if (xevent->type != DestroyNotify)
411 if (xevent->type == DestroyNotify)
415 n = gdk_display_get_n_screens (display);
416 for (i = 0; i < n; i++)
418 screen = gdk_display_get_screen (display, i);
419 screen_x11 = GDK_SCREEN_X11 (screen);
421 if (screen_x11->wmspec_check_window == xwindow)
423 screen_x11->wmspec_check_window = None;
424 screen_x11->last_wmspec_check_time = 0;
425 g_free (screen_x11->window_manager_name);
426 screen_x11->window_manager_name = g_strdup ("unknown");
428 /* careful, reentrancy */
429 _gdk_x11_screen_window_manager_changed (screen);
437 /* We do a "manual" conversion of the XEvent to a
438 * GdkEvent. The structures are mostly the same so
439 * the conversion is fairly straightforward. We also
440 * optionally print debugging info regarding events
446 switch (xevent->type)
450 g_message ("keymap notify"));
452 /* Not currently handled */
458 g_message ("expose:\t\twindow: %ld %d x,y: %d %d w,h: %d %d%s",
459 xevent->xexpose.window, xevent->xexpose.count,
460 xevent->xexpose.x, xevent->xexpose.y,
461 xevent->xexpose.width, xevent->xexpose.height,
462 event->any.send_event ? " (send)" : ""));
464 if (window_private == NULL)
471 GdkRectangle expose_rect;
473 expose_rect.x = xevent->xexpose.x;
474 expose_rect.y = xevent->xexpose.y;
475 expose_rect.width = xevent->xexpose.width;
476 expose_rect.height = xevent->xexpose.height;
478 _gdk_window_process_expose (window, xevent->xexpose.serial, &expose_rect);
486 GdkRectangle expose_rect;
489 g_message ("graphics expose:\tdrawable: %ld",
490 xevent->xgraphicsexpose.drawable));
492 if (window_private == NULL)
498 expose_rect.x = xevent->xgraphicsexpose.x;
499 expose_rect.y = xevent->xgraphicsexpose.y;
500 expose_rect.width = xevent->xgraphicsexpose.width;
501 expose_rect.height = xevent->xgraphicsexpose.height;
503 _gdk_window_process_expose (window, xevent->xgraphicsexpose.serial, &expose_rect);
510 g_message ("no expose:\t\tdrawable: %ld",
511 xevent->xnoexpose.drawable));
513 event->no_expose.type = GDK_NO_EXPOSE;
514 event->no_expose.window = window;
518 case VisibilityNotify:
519 #ifdef G_ENABLE_DEBUG
520 if (_gdk_debug_flags & GDK_DEBUG_EVENTS)
521 switch (xevent->xvisibility.state)
523 case VisibilityFullyObscured:
524 g_message ("visibility notify:\twindow: %ld none",
525 xevent->xvisibility.window);
527 case VisibilityPartiallyObscured:
528 g_message ("visibility notify:\twindow: %ld partial",
529 xevent->xvisibility.window);
531 case VisibilityUnobscured:
532 g_message ("visibility notify:\twindow: %ld full",
533 xevent->xvisibility.window);
536 #endif /* G_ENABLE_DEBUG */
538 if (window_private == NULL)
544 event->visibility.type = GDK_VISIBILITY_NOTIFY;
545 event->visibility.window = window;
547 switch (xevent->xvisibility.state)
549 case VisibilityFullyObscured:
550 event->visibility.state = GDK_VISIBILITY_FULLY_OBSCURED;
553 case VisibilityPartiallyObscured:
554 event->visibility.state = GDK_VISIBILITY_PARTIAL;
557 case VisibilityUnobscured:
558 event->visibility.state = GDK_VISIBILITY_UNOBSCURED;
566 g_message ("create notify:\twindow: %ld x,y: %d %d w,h: %d %d b-w: %d parent: %ld ovr: %d",
567 xevent->xcreatewindow.window,
568 xevent->xcreatewindow.x,
569 xevent->xcreatewindow.y,
570 xevent->xcreatewindow.width,
571 xevent->xcreatewindow.height,
572 xevent->xcreatewindow.border_width,
573 xevent->xcreatewindow.parent,
574 xevent->xcreatewindow.override_redirect));
575 /* not really handled */
580 g_message ("destroy notify:\twindow: %ld",
581 xevent->xdestroywindow.window));
583 /* Ignore DestroyNotify from SubstructureNotifyMask */
584 if (xevent->xdestroywindow.window == xevent->xdestroywindow.event)
586 event->any.type = GDK_DESTROY;
587 event->any.window = window;
589 return_val = window_private && !GDK_WINDOW_DESTROYED (window);
591 if (window && GDK_WINDOW_XID (window) != screen_x11->xroot_window)
592 gdk_window_destroy_notify (window);
601 g_message ("unmap notify:\t\twindow: %ld",
602 xevent->xmap.window));
604 event->any.type = GDK_UNMAP;
605 event->any.window = window;
607 /* If we are shown (not withdrawn) and get an unmap, it means we
608 * were iconified in the X sense. If we are withdrawn, and get
609 * an unmap, it means we hid the window ourselves, so we
610 * will have already flipped the iconified bit off.
614 if (GDK_WINDOW_IS_MAPPED (window))
615 gdk_synthesize_window_state (window,
617 GDK_WINDOW_STATE_ICONIFIED);
619 _gdk_xgrab_check_unmap (window, xevent->xany.serial);
626 g_message ("map notify:\t\twindow: %ld",
627 xevent->xmap.window));
629 event->any.type = GDK_MAP;
630 event->any.window = window;
632 /* Unset iconified if it was set */
633 if (window && (((GdkWindowObject*)window)->state & GDK_WINDOW_STATE_ICONIFIED))
634 gdk_synthesize_window_state (window,
635 GDK_WINDOW_STATE_ICONIFIED,
642 g_message ("reparent notify:\twindow: %ld x,y: %d %d parent: %ld ovr: %d",
643 xevent->xreparent.window,
646 xevent->xreparent.parent,
647 xevent->xreparent.override_redirect));
649 /* Not currently handled */
653 case ConfigureNotify:
655 g_message ("configure notify:\twindow: %ld x,y: %d %d w,h: %d %d b-w: %d above: %ld ovr: %d%s",
656 xevent->xconfigure.window,
657 xevent->xconfigure.x,
658 xevent->xconfigure.y,
659 xevent->xconfigure.width,
660 xevent->xconfigure.height,
661 xevent->xconfigure.border_width,
662 xevent->xconfigure.above,
663 xevent->xconfigure.override_redirect,
666 : GDK_WINDOW_TYPE (window) == GDK_WINDOW_CHILD
667 ? " (discarding child)"
668 : xevent->xconfigure.event != xevent->xconfigure.window
669 ? " (discarding substructure)"
671 if (window && GDK_WINDOW_TYPE (window) == GDK_WINDOW_ROOT)
673 window_private->width = xevent->xconfigure.width;
674 window_private->height = xevent->xconfigure.height;
676 _gdk_window_update_size (window);
677 _gdk_x11_drawable_update_size (window_private->impl);
678 _gdk_x11_screen_size_changed (screen, xevent);
682 if (toplevel && display_x11->use_sync && !XSyncValueIsZero (toplevel->pending_counter_value))
684 toplevel->current_counter_value = toplevel->pending_counter_value;
685 XSyncIntToValue (&toplevel->pending_counter_value, 0);
690 xevent->xconfigure.event != xevent->xconfigure.window ||
691 GDK_WINDOW_TYPE (window) == GDK_WINDOW_CHILD ||
692 GDK_WINDOW_TYPE (window) == GDK_WINDOW_ROOT)
696 event->configure.type = GDK_CONFIGURE;
697 event->configure.window = window;
698 event->configure.width = xevent->xconfigure.width;
699 event->configure.height = xevent->xconfigure.height;
701 if (!xevent->xconfigure.send_event &&
702 !xevent->xconfigure.override_redirect &&
703 !GDK_WINDOW_DESTROYED (window))
707 Window child_window = 0;
709 gdk_error_trap_push ();
710 if (XTranslateCoordinates (GDK_DRAWABLE_XDISPLAY (window),
711 GDK_DRAWABLE_XID (window),
712 screen_x11->xroot_window,
717 event->configure.x = tx;
718 event->configure.y = ty;
720 gdk_error_trap_pop ();
724 event->configure.x = xevent->xconfigure.x;
725 event->configure.y = xevent->xconfigure.y;
727 window_private->x = event->configure.x;
728 window_private->y = event->configure.y;
729 window_private->width = xevent->xconfigure.width;
730 window_private->height = xevent->xconfigure.height;
732 _gdk_window_update_size (window);
733 _gdk_x11_drawable_update_size (window_private->impl);
735 if (window_private->resize_count >= 1)
737 window_private->resize_count -= 1;
739 if (window_private->resize_count == 0)
740 _gdk_moveresize_configure_done (display, window);
747 g_message ("property notify:\twindow: %ld, atom(%ld): %s%s%s",
748 xevent->xproperty.window,
749 xevent->xproperty.atom,
751 gdk_x11_get_xatom_name_for_display (display, xevent->xproperty.atom),
754 if (window_private == NULL)
760 /* We compare with the serial of the last time we mapped the
761 * window to avoid refetching properties that we set ourselves
764 xevent->xproperty.serial >= toplevel->map_serial)
766 if (xevent->xproperty.atom == gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_STATE"))
767 gdk_check_wm_state_changed (window);
769 if (xevent->xproperty.atom == gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_DESKTOP"))
770 gdk_check_wm_desktop_changed (window);
773 if (window_private->event_mask & GDK_PROPERTY_CHANGE_MASK)
775 event->property.type = GDK_PROPERTY_NOTIFY;
776 event->property.window = window;
777 event->property.atom = gdk_x11_xatom_to_atom_for_display (display, xevent->xproperty.atom);
778 event->property.time = xevent->xproperty.time;
779 event->property.state = xevent->xproperty.state;
788 g_message ("selection clear:\twindow: %ld",
789 xevent->xproperty.window));
791 if (_gdk_selection_filter_clear_event (&xevent->xselectionclear))
793 event->selection.type = GDK_SELECTION_CLEAR;
794 event->selection.window = window;
795 event->selection.selection = gdk_x11_xatom_to_atom_for_display (display, xevent->xselectionclear.selection);
796 event->selection.time = xevent->xselectionclear.time;
803 case SelectionRequest:
805 g_message ("selection request:\twindow: %ld",
806 xevent->xproperty.window));
808 event->selection.type = GDK_SELECTION_REQUEST;
809 event->selection.window = window;
810 event->selection.selection = gdk_x11_xatom_to_atom_for_display (display, xevent->xselectionrequest.selection);
811 event->selection.target = gdk_x11_xatom_to_atom_for_display (display, xevent->xselectionrequest.target);
812 event->selection.property = gdk_x11_xatom_to_atom_for_display (display, xevent->xselectionrequest.property);
813 event->selection.requestor = xevent->xselectionrequest.requestor;
814 event->selection.time = xevent->xselectionrequest.time;
818 case SelectionNotify:
820 g_message ("selection notify:\twindow: %ld",
821 xevent->xproperty.window));
823 event->selection.type = GDK_SELECTION_NOTIFY;
824 event->selection.window = window;
825 event->selection.selection = gdk_x11_xatom_to_atom_for_display (display, xevent->xselection.selection);
826 event->selection.target = gdk_x11_xatom_to_atom_for_display (display, xevent->xselection.target);
827 if (xevent->xselection.property == None)
828 event->selection.property = GDK_NONE;
830 event->selection.property = gdk_x11_xatom_to_atom_for_display (display, xevent->xselection.property);
831 event->selection.time = xevent->xselection.time;
837 g_message ("colormap notify:\twindow: %ld",
838 xevent->xcolormap.window));
840 /* Not currently handled */
847 GdkFilterReturn result = GDK_FILTER_CONTINUE;
848 GdkAtom message_type = gdk_x11_xatom_to_atom_for_display (display, xevent->xclient.message_type);
851 g_message ("client message:\twindow: %ld",
852 xevent->xclient.window));
854 tmp_list = display_x11->client_filters;
857 GdkClientFilter *filter = tmp_list->data;
858 tmp_list = tmp_list->next;
860 if (filter->type == message_type)
862 result = (*filter->function) (xevent, event, filter->data);
863 if (result != GDK_FILTER_CONTINUE)
870 case GDK_FILTER_REMOVE:
873 case GDK_FILTER_TRANSLATE:
876 case GDK_FILTER_CONTINUE:
877 /* Send unknown ClientMessage's on to Gtk for it to use */
878 if (window_private == NULL)
884 event->client.type = GDK_CLIENT_EVENT;
885 event->client.window = window;
886 event->client.message_type = message_type;
887 event->client.data_format = xevent->xclient.format;
888 memcpy(&event->client.data, &xevent->xclient.data,
889 sizeof(event->client.data));
899 g_message ("mapping notify"));
901 /* Let XLib know that there is a new keyboard mapping.
903 XRefreshKeyboardMapping (&xevent->xmapping);
904 _gdk_keymap_keys_changed (display);
910 if (xevent->type - display_x11->xfixes_event_base == XFixesSelectionNotify)
912 XFixesSelectionNotifyEvent *selection_notify = (XFixesSelectionNotifyEvent *)xevent;
914 _gdk_x11_screen_process_owner_change (screen, xevent);
916 event->owner_change.type = GDK_OWNER_CHANGE;
917 event->owner_change.window = window;
918 event->owner_change.owner = selection_notify->owner;
919 event->owner_change.reason = selection_notify->subtype;
920 event->owner_change.selection =
921 gdk_x11_xatom_to_atom_for_display (display,
922 selection_notify->selection);
923 event->owner_change.time = selection_notify->timestamp;
924 event->owner_change.selection_time = selection_notify->selection_timestamp;
931 if (xevent->type - display_x11->xrandr_event_base == RRScreenChangeNotify ||
932 xevent->type - display_x11->xrandr_event_base == RRNotify)
935 _gdk_x11_screen_size_changed (screen, xevent);
939 #if defined(HAVE_XCOMPOSITE) && defined (HAVE_XDAMAGE) && defined (HAVE_XFIXES)
940 if (display_x11->have_xdamage && window_private && window_private->composited &&
941 xevent->type == display_x11->xdamage_event_base + XDamageNotify &&
942 ((XDamageNotifyEvent *) xevent)->damage == window_impl->damage)
944 XDamageNotifyEvent *damage_event = (XDamageNotifyEvent *) xevent;
945 XserverRegion repair;
948 rect.x = window_private->x + damage_event->area.x;
949 rect.y = window_private->y + damage_event->area.y;
950 rect.width = damage_event->area.width;
951 rect.height = damage_event->area.height;
953 repair = XFixesCreateRegion (display_x11->xdisplay,
954 &damage_event->area, 1);
955 XDamageSubtract (display_x11->xdisplay,
958 XFixesDestroyRegion (display_x11->xdisplay, repair);
960 if (window_private->parent != NULL)
961 _gdk_window_process_expose (GDK_WINDOW (window_private->parent),
962 damage_event->serial, &rect);
969 if (xevent->type == display_x11->xkb_event_type)
971 XkbEvent *xkb_event = (XkbEvent *) xevent;
973 switch (xkb_event->any.xkb_type)
975 case XkbNewKeyboardNotify:
977 _gdk_keymap_keys_changed (display);
983 _gdk_keymap_state_changed (display, xevent);
995 if (event->any.window)
996 g_object_ref (event->any.window);
1000 /* Mark this event as having no resources to be freed */
1001 event->any.window = NULL;
1002 event->any.type = GDK_NOTHING;
1006 g_object_unref (window);
1011 static GdkFilterReturn
1012 gdk_wm_protocols_filter (GdkXEvent *xev,
1016 XEvent *xevent = (XEvent *)xev;
1017 GdkWindow *win = event->any.window;
1018 GdkDisplay *display;
1022 return GDK_FILTER_REMOVE;
1024 display = GDK_WINDOW_DISPLAY (win);
1025 atom = (Atom)xevent->xclient.data.l[0];
1027 if (atom == gdk_x11_get_xatom_by_name_for_display (display, "WM_DELETE_WINDOW"))
1029 /* The delete window request specifies a window
1030 * to delete. We don't actually destroy the
1031 * window because "it is only a request". (The
1032 * window might contain vital data that the
1033 * program does not want destroyed). Instead
1034 * the event is passed along to the program,
1035 * which should then destroy the window.
1038 g_message ("delete window:\t\twindow: %ld",
1039 xevent->xclient.window));
1041 event->any.type = GDK_DELETE;
1043 gdk_x11_window_set_user_time (win, xevent->xclient.data.l[1]);
1045 return GDK_FILTER_TRANSLATE;
1047 else if (atom == gdk_x11_get_xatom_by_name_for_display (display, "WM_TAKE_FOCUS"))
1049 GdkToplevelX11 *toplevel = _gdk_x11_window_get_toplevel (event->any.window);
1050 GdkWindowObject *private = (GdkWindowObject *)win;
1052 /* There is no way of knowing reliably whether we are viewable;
1053 * _gdk_x11_set_input_focus_safe() traps errors asynchronously.
1055 if (toplevel && private->accept_focus)
1056 _gdk_x11_set_input_focus_safe (display, toplevel->focus_window,
1058 xevent->xclient.data.l[1]);
1060 return GDK_FILTER_REMOVE;
1062 else if (atom == gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_PING") &&
1063 !_gdk_x11_display_is_root_window (display,
1064 xevent->xclient.window))
1066 XClientMessageEvent xclient = xevent->xclient;
1068 xclient.window = GDK_WINDOW_XROOTWIN (win);
1069 XSendEvent (GDK_WINDOW_XDISPLAY (win),
1072 SubstructureRedirectMask | SubstructureNotifyMask, (XEvent *)&xclient);
1074 return GDK_FILTER_REMOVE;
1076 else if (atom == gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_SYNC_REQUEST") &&
1077 GDK_DISPLAY_X11 (display)->use_sync)
1079 GdkToplevelX11 *toplevel = _gdk_x11_window_get_toplevel (event->any.window);
1083 XSyncIntsToValue (&toplevel->pending_counter_value,
1084 xevent->xclient.data.l[2],
1085 xevent->xclient.data.l[3]);
1088 return GDK_FILTER_REMOVE;
1091 return GDK_FILTER_CONTINUE;
1095 _gdk_event_init (GdkDisplay *display)
1097 GdkDisplayX11 *display_x11;
1098 GdkDeviceManager *device_manager;
1100 display_x11 = GDK_DISPLAY_X11 (display);
1101 display_x11->event_source = gdk_event_source_new (display);
1103 gdk_event_source_add_translator ((GdkEventSource *) display_x11->event_source,
1104 GDK_EVENT_TRANSLATOR (display));
1106 device_manager = gdk_display_get_device_manager (display);
1107 gdk_event_source_add_translator ((GdkEventSource *) display_x11->event_source,
1108 GDK_EVENT_TRANSLATOR (device_manager));
1110 gdk_display_add_client_message_filter (display,
1111 gdk_atom_intern_static_string ("WM_PROTOCOLS"),
1112 gdk_wm_protocols_filter,
1117 _gdk_input_init (GdkDisplay *display)
1119 GdkDisplayX11 *display_x11;
1120 GdkDeviceManager *device_manager;
1124 display_x11 = GDK_DISPLAY_X11 (display);
1125 device_manager = gdk_display_get_device_manager (display);
1127 /* For backwards compatibility, just add
1128 * floating devices that are not keyboards.
1130 list = gdk_device_manager_list_devices (device_manager, GDK_DEVICE_TYPE_FLOATING);
1132 for (l = list; l; l = l->next)
1136 if (device->source == GDK_SOURCE_KEYBOARD)
1139 display_x11->input_devices = g_list_prepend (display_x11->input_devices, l->data);
1144 /* Now set "core" pointer to the first
1145 * master device that is a pointer.
1147 list = gdk_device_manager_list_devices (device_manager, GDK_DEVICE_TYPE_MASTER);
1149 for (l = list; l; l = l->next)
1151 device = list->data;
1153 if (device->source != GDK_SOURCE_MOUSE)
1156 display->core_pointer = device;
1160 /* Add the core pointer to the devices list */
1161 display_x11->input_devices = g_list_prepend (display_x11->input_devices, display->core_pointer);
1168 * @display_name: the name of the display to open
1169 * @returns: a #GdkDisplay, or %NULL if the display
1170 * could not be opened.
1177 gdk_display_open (const gchar *display_name)
1180 GdkDisplay *display;
1181 GdkDisplayX11 *display_x11;
1185 const char *sm_client_id;
1187 XClassHint *class_hint;
1193 xdisplay = XOpenDisplay (display_name);
1197 display = g_object_new (GDK_TYPE_DISPLAY_X11, NULL);
1198 display_x11 = GDK_DISPLAY_X11 (display);
1200 display_x11->xdisplay = xdisplay;
1203 /* Set up handlers for Xlib internal connections */
1204 XAddConnectionWatch (xdisplay, gdk_internal_connection_watch, NULL);
1205 #endif /* HAVE_X11R6 */
1207 _gdk_x11_precache_atoms (display, precache_atoms, G_N_ELEMENTS (precache_atoms));
1209 /* RandR must be initialized before we initialize the screens */
1210 display_x11->have_randr13 = FALSE;
1212 if (XRRQueryExtension (display_x11->xdisplay,
1213 &display_x11->xrandr_event_base, &ignore))
1217 XRRQueryVersion (display_x11->xdisplay, &major, &minor);
1219 if ((major == 1 && minor >= 3) || major > 1)
1220 display_x11->have_randr13 = TRUE;
1222 gdk_x11_register_standard_event_type (display, display_x11->xrandr_event_base, RRNumberEvents);
1226 /* initialize the display's screens */
1227 display_x11->screens = g_new (GdkScreen *, ScreenCount (display_x11->xdisplay));
1228 for (i = 0; i < ScreenCount (display_x11->xdisplay); i++)
1229 display_x11->screens[i] = _gdk_x11_screen_new (display, i);
1231 /* We need to initialize events after we have the screen
1232 * structures in places
1234 for (i = 0; i < ScreenCount (display_x11->xdisplay); i++)
1235 _gdk_screen_x11_events_init (display_x11->screens[i]);
1237 /*set the default screen */
1238 display_x11->default_screen = display_x11->screens[DefaultScreen (display_x11->xdisplay)];
1240 display->device_manager = _gdk_device_manager_new (display);
1242 _gdk_event_init (display);
1244 attr.window_type = GDK_WINDOW_TOPLEVEL;
1245 attr.wclass = GDK_INPUT_OUTPUT;
1250 attr.event_mask = 0;
1252 display_x11->leader_gdk_window = gdk_window_new (GDK_SCREEN_X11 (display_x11->default_screen)->root_window,
1253 &attr, GDK_WA_X | GDK_WA_Y);
1254 (_gdk_x11_window_get_toplevel (display_x11->leader_gdk_window))->is_leader = TRUE;
1256 display_x11->leader_window = GDK_WINDOW_XID (display_x11->leader_gdk_window);
1258 display_x11->leader_window_title_set = FALSE;
1261 if (XFixesQueryExtension (display_x11->xdisplay,
1262 &display_x11->xfixes_event_base,
1265 display_x11->have_xfixes = TRUE;
1267 gdk_x11_register_standard_event_type (display,
1268 display_x11->xfixes_event_base,
1269 XFixesNumberEvents);
1273 display_x11->have_xfixes = FALSE;
1275 #ifdef HAVE_XCOMPOSITE
1276 if (XCompositeQueryExtension (display_x11->xdisplay,
1281 XCompositeQueryVersion (display_x11->xdisplay, &major, &minor);
1283 /* Prior to Composite version 0.4, composited windows clipped their
1284 * parents, so you had to use IncludeInferiors to draw to the parent
1285 * This isn't useful for our purposes, so require 0.4
1287 display_x11->have_xcomposite = major > 0 || (major == 0 && minor >= 4);
1291 display_x11->have_xcomposite = FALSE;
1294 if (XDamageQueryExtension (display_x11->xdisplay,
1295 &display_x11->xdamage_event_base,
1298 display_x11->have_xdamage = TRUE;
1300 gdk_x11_register_standard_event_type (display,
1301 display_x11->xdamage_event_base,
1302 XDamageNumberEvents);
1306 display_x11->have_xdamage = FALSE;
1308 display_x11->have_shapes = FALSE;
1309 display_x11->have_input_shapes = FALSE;
1311 if (XShapeQueryExtension (GDK_DISPLAY_XDISPLAY (display), &display_x11->shape_event_base, &ignore))
1313 display_x11->have_shapes = TRUE;
1315 if (XShapeQueryVersion (GDK_DISPLAY_XDISPLAY (display), &maj, &min))
1316 display_x11->have_input_shapes = (maj == 1 && min >= 1);
1320 display_x11->trusted_client = TRUE;
1323 int rootx, rooty, winx, winy;
1326 gdk_error_trap_push ();
1327 XQueryPointer (display_x11->xdisplay,
1328 GDK_SCREEN_X11 (display_x11->default_screen)->xroot_window,
1329 &root, &child, &rootx, &rooty, &winx, &winy, &xmask);
1331 if (G_UNLIKELY (gdk_error_trap_pop () == BadWindow))
1333 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));
1334 display_x11->trusted_client = FALSE;
1338 if (_gdk_synchronize)
1339 XSynchronize (display_x11->xdisplay, True);
1341 class_hint = XAllocClassHint();
1342 class_hint->res_name = g_get_prgname ();
1344 class_hint->res_class = (char *)gdk_get_program_class ();
1346 /* XmbSetWMProperties sets the RESOURCE_NAME environment variable
1347 * from argv[0], so we just synthesize an argument array here.
1350 argv[0] = g_get_prgname ();
1352 XmbSetWMProperties (display_x11->xdisplay,
1353 display_x11->leader_window,
1354 NULL, NULL, argv, argc, NULL, NULL,
1358 sm_client_id = _gdk_get_sm_client_id ();
1360 _gdk_windowing_display_set_sm_client_id (display, sm_client_id);
1363 XChangeProperty (display_x11->xdisplay,
1364 display_x11->leader_window,
1365 gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_PID"),
1366 XA_CARDINAL, 32, PropModeReplace, (guchar *) & pid, 1);
1368 /* We don't yet know a valid time. */
1369 display_x11->user_time = 0;
1373 gint xkb_major = XkbMajorVersion;
1374 gint xkb_minor = XkbMinorVersion;
1375 if (XkbLibraryVersion (&xkb_major, &xkb_minor))
1377 xkb_major = XkbMajorVersion;
1378 xkb_minor = XkbMinorVersion;
1380 if (XkbQueryExtension (display_x11->xdisplay,
1381 NULL, &display_x11->xkb_event_type, NULL,
1382 &xkb_major, &xkb_minor))
1384 Bool detectable_autorepeat_supported;
1386 display_x11->use_xkb = TRUE;
1388 XkbSelectEvents (display_x11->xdisplay,
1390 XkbNewKeyboardNotifyMask | XkbMapNotifyMask | XkbStateNotifyMask,
1391 XkbNewKeyboardNotifyMask | XkbMapNotifyMask | XkbStateNotifyMask);
1393 /* keep this in sync with _gdk_keymap_state_changed() */
1394 XkbSelectEventDetails (display_x11->xdisplay,
1395 XkbUseCoreKbd, XkbStateNotify,
1396 XkbAllStateComponentsMask,
1397 XkbGroupLockMask|XkbModifierLockMask);
1399 XkbSetDetectableAutoRepeat (display_x11->xdisplay,
1401 &detectable_autorepeat_supported);
1403 GDK_NOTE (MISC, g_message ("Detectable autorepeat %s.",
1404 detectable_autorepeat_supported ?
1405 "supported" : "not supported"));
1407 display_x11->have_xkb_autorepeat = detectable_autorepeat_supported;
1413 display_x11->use_sync = FALSE;
1417 int error_base, event_base;
1419 if (XSyncQueryExtension (display_x11->xdisplay,
1420 &event_base, &error_base) &&
1421 XSyncInitialize (display_x11->xdisplay,
1423 display_x11->use_sync = TRUE;
1427 _gdk_input_init (display);
1428 _gdk_dnd_init (display);
1430 for (i = 0; i < ScreenCount (display_x11->xdisplay); i++)
1431 _gdk_x11_screen_setup (display_x11->screens[i]);
1433 g_signal_emit_by_name (display, "opened");
1434 g_signal_emit_by_name (gdk_display_manager_get(),
1435 "display_opened", display);
1442 * XLib internal connection handling
1444 typedef struct _GdkInternalConnection GdkInternalConnection;
1446 struct _GdkInternalConnection
1454 process_internal_connection (GIOChannel *gioc,
1458 GdkInternalConnection *connection = (GdkInternalConnection *)data;
1460 GDK_THREADS_ENTER ();
1462 XProcessInternalConnection ((Display*)connection->display, connection->fd);
1464 GDK_THREADS_LEAVE ();
1470 _gdk_windowing_window_get_next_serial (GdkDisplay *display)
1472 return NextRequest (GDK_DISPLAY_XDISPLAY (display));
1476 static GdkInternalConnection *
1477 gdk_add_connection_handler (Display *display,
1480 GIOChannel *io_channel;
1481 GdkInternalConnection *connection;
1483 connection = g_new (GdkInternalConnection, 1);
1485 connection->fd = fd;
1486 connection->display = display;
1488 io_channel = g_io_channel_unix_new (fd);
1490 connection->source = g_io_create_watch (io_channel, G_IO_IN);
1491 g_source_set_callback (connection->source,
1492 (GSourceFunc)process_internal_connection, connection, NULL);
1493 g_source_attach (connection->source, NULL);
1495 g_io_channel_unref (io_channel);
1501 gdk_remove_connection_handler (GdkInternalConnection *connection)
1503 g_source_destroy (connection->source);
1504 g_free (connection);
1508 gdk_internal_connection_watch (Display *display,
1512 XPointer *watch_data)
1515 *watch_data = (XPointer)gdk_add_connection_handler (display, fd);
1517 gdk_remove_connection_handler ((GdkInternalConnection *)*watch_data);
1519 #endif /* HAVE_X11R6 */
1522 * gdk_display_get_name:
1523 * @display: a #GdkDisplay
1525 * Gets the name of the display.
1527 * Returns: a string representing the display name. This string is owned
1528 * by GDK and should not be modified or freed.
1532 G_CONST_RETURN gchar *
1533 gdk_display_get_name (GdkDisplay *display)
1535 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
1537 return (gchar *) DisplayString (GDK_DISPLAY_X11 (display)->xdisplay);
1541 * gdk_display_get_n_screens:
1542 * @display: a #GdkDisplay
1544 * Gets the number of screen managed by the @display.
1546 * Returns: number of screens.
1551 gdk_display_get_n_screens (GdkDisplay *display)
1553 g_return_val_if_fail (GDK_IS_DISPLAY (display), 0);
1555 return ScreenCount (GDK_DISPLAY_X11 (display)->xdisplay);
1559 * gdk_display_get_screen:
1560 * @display: a #GdkDisplay
1561 * @screen_num: the screen number
1563 * Returns a screen object for one of the screens of the display.
1565 * Returns: the #GdkScreen object
1570 gdk_display_get_screen (GdkDisplay *display,
1573 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
1574 g_return_val_if_fail (ScreenCount (GDK_DISPLAY_X11 (display)->xdisplay) > screen_num, NULL);
1576 return GDK_DISPLAY_X11 (display)->screens[screen_num];
1580 * gdk_display_get_default_screen:
1581 * @display: a #GdkDisplay
1583 * Get the default #GdkScreen for @display.
1585 * Returns: the default #GdkScreen object for @display
1590 gdk_display_get_default_screen (GdkDisplay *display)
1592 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
1594 return GDK_DISPLAY_X11 (display)->default_screen;
1598 _gdk_x11_display_is_root_window (GdkDisplay *display,
1599 Window xroot_window)
1601 GdkDisplayX11 *display_x11;
1604 g_return_val_if_fail (GDK_IS_DISPLAY (display), FALSE);
1606 display_x11 = GDK_DISPLAY_X11 (display);
1608 for (i = 0; i < ScreenCount (display_x11->xdisplay); i++)
1610 if (GDK_SCREEN_XROOTWIN (display_x11->screens[i]) == xroot_window)
1616 struct XPointerUngrabInfo {
1617 GdkDisplay *display;
1622 device_ungrab_callback (GdkDisplay *display,
1626 GdkDevice *device = data;
1628 _gdk_display_device_grab_update (display, device, serial);
1632 #define XSERVER_TIME_IS_LATER(time1, time2) \
1633 ( (( time1 > time2 ) && ( time1 - time2 < ((guint32)-1)/2 )) || \
1634 (( time1 < time2 ) && ( time2 - time1 > ((guint32)-1)/2 )) \
1638 * gdk_device_ungrab:
1639 * @device: a #GdkDevice
1640 * @time_: a timestap (e.g. %GDK_CURRENT_TIME).
1642 * Release any grab on @device.
1647 gdk_device_ungrab (GdkDevice *device,
1650 GdkDisplay *display;
1652 GdkDeviceGrabInfo *grab;
1653 unsigned long serial;
1655 g_return_if_fail (GDK_IS_DEVICE (device));
1657 display = gdk_device_get_display (device);
1658 xdisplay = GDK_DISPLAY_XDISPLAY (display);
1660 serial = NextRequest (xdisplay);
1662 GDK_DEVICE_GET_CLASS (device)->ungrab (device, time_);
1665 grab = _gdk_display_get_last_device_grab (display, device);
1667 (time_ == GDK_CURRENT_TIME ||
1668 grab->time == GDK_CURRENT_TIME ||
1669 !XSERVER_TIME_IS_LATER (grab->time, time_)))
1671 grab->serial_end = serial;
1672 _gdk_x11_roundtrip_async (display,
1673 device_ungrab_callback,
1680 * @display: a #GdkDisplay
1682 * Emits a short beep on @display
1687 gdk_display_beep (GdkDisplay *display)
1689 g_return_if_fail (GDK_IS_DISPLAY (display));
1692 XkbBell (GDK_DISPLAY_XDISPLAY (display), None, 0, None);
1694 XBell (GDK_DISPLAY_XDISPLAY (display), 0);
1700 * @display: a #GdkDisplay
1702 * Flushes any requests queued for the windowing system and waits until all
1703 * requests have been handled. This is often used for making sure that the
1704 * display is synchronized with the current state of the program. Calling
1705 * gdk_display_sync() before gdk_error_trap_pop() makes sure that any errors
1706 * generated from earlier requests are handled before the error trap is
1709 * This is most useful for X11. On windowing systems where requests are
1710 * handled synchronously, this function will do nothing.
1715 gdk_display_sync (GdkDisplay *display)
1717 g_return_if_fail (GDK_IS_DISPLAY (display));
1719 XSync (GDK_DISPLAY_XDISPLAY (display), False);
1723 * gdk_display_flush:
1724 * @display: a #GdkDisplay
1726 * Flushes any requests queued for the windowing system; this happens automatically
1727 * when the main loop blocks waiting for new events, but if your application
1728 * is drawing without returning control to the main loop, you may need
1729 * to call this function explicitely. A common case where this function
1730 * needs to be called is when an application is executing drawing commands
1731 * from a thread other than the thread where the main loop is running.
1733 * This is most useful for X11. On windowing systems where requests are
1734 * handled synchronously, this function will do nothing.
1739 gdk_display_flush (GdkDisplay *display)
1741 g_return_if_fail (GDK_IS_DISPLAY (display));
1743 if (!display->closed)
1744 XFlush (GDK_DISPLAY_XDISPLAY (display));
1748 * gdk_display_get_default_group:
1749 * @display: a #GdkDisplay
1751 * Returns the default group leader window for all toplevel windows
1752 * on @display. This window is implicitly created by GDK.
1753 * See gdk_window_set_group().
1755 * Return value: The default group leader window for @display
1760 gdk_display_get_default_group (GdkDisplay *display)
1762 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
1764 return GDK_DISPLAY_X11 (display)->leader_gdk_window;
1768 * gdk_x11_display_grab:
1769 * @display: a #GdkDisplay
1771 * Call XGrabServer() on @display.
1772 * To ungrab the display again, use gdk_x11_display_ungrab().
1774 * gdk_x11_display_grab()/gdk_x11_display_ungrab() calls can be nested.
1779 gdk_x11_display_grab (GdkDisplay *display)
1781 GdkDisplayX11 *display_x11;
1783 g_return_if_fail (GDK_IS_DISPLAY (display));
1785 display_x11 = GDK_DISPLAY_X11 (display);
1787 if (display_x11->grab_count == 0)
1788 XGrabServer (display_x11->xdisplay);
1789 display_x11->grab_count++;
1793 * gdk_x11_display_ungrab:
1794 * @display: a #GdkDisplay
1796 * Ungrab @display after it has been grabbed with
1797 * gdk_x11_display_grab().
1802 gdk_x11_display_ungrab (GdkDisplay *display)
1804 GdkDisplayX11 *display_x11;
1806 g_return_if_fail (GDK_IS_DISPLAY (display));
1808 display_x11 = GDK_DISPLAY_X11 (display);;
1809 g_return_if_fail (display_x11->grab_count > 0);
1811 display_x11->grab_count--;
1812 if (display_x11->grab_count == 0)
1814 XUngrabServer (display_x11->xdisplay);
1815 XFlush (display_x11->xdisplay);
1820 gdk_display_x11_dispose (GObject *object)
1822 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (object);
1825 g_list_foreach (display_x11->input_devices, (GFunc) g_object_run_dispose, NULL);
1827 for (i = 0; i < ScreenCount (display_x11->xdisplay); i++)
1828 _gdk_screen_close (display_x11->screens[i]);
1830 if (display_x11->event_source)
1832 g_source_destroy (display_x11->event_source);
1833 g_source_unref (display_x11->event_source);
1834 display_x11->event_source = NULL;
1837 G_OBJECT_CLASS (_gdk_display_x11_parent_class)->dispose (object);
1841 gdk_display_x11_finalize (GObject *object)
1843 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (object);
1847 if (display_x11->keymap)
1848 g_object_unref (display_x11->keymap);
1850 /* Free motif Dnd */
1851 if (display_x11->motif_target_lists)
1853 for (i = 0; i < display_x11->motif_n_target_lists; i++)
1854 g_list_free (display_x11->motif_target_lists[i]);
1855 g_free (display_x11->motif_target_lists);
1858 _gdk_x11_cursor_display_finalize (GDK_DISPLAY_OBJECT(display_x11));
1860 /* Atom Hashtable */
1861 g_hash_table_destroy (display_x11->atom_from_virtual);
1862 g_hash_table_destroy (display_x11->atom_to_virtual);
1865 XDestroyWindow (display_x11->xdisplay, display_x11->leader_window);
1867 /* list of filters for client messages */
1868 g_list_foreach (display_x11->client_filters, (GFunc) g_free, NULL);
1869 g_list_free (display_x11->client_filters);
1871 /* List of event window extraction functions */
1872 g_slist_foreach (display_x11->event_types, (GFunc)g_free, NULL);
1873 g_slist_free (display_x11->event_types);
1875 /* input GdkDevice list */
1876 g_list_foreach (display_x11->input_devices, (GFunc) g_object_unref, NULL);
1877 g_list_free (display_x11->input_devices);
1879 /* input GdkWindow list */
1880 g_list_foreach (display_x11->input_windows, (GFunc) g_free, NULL);
1881 g_list_free (display_x11->input_windows);
1883 /* Free all GdkScreens */
1884 for (i = 0; i < ScreenCount (display_x11->xdisplay); i++)
1885 g_object_unref (display_x11->screens[i]);
1886 g_free (display_x11->screens);
1888 g_free (display_x11->startup_notification_id);
1890 /* X ID hashtable */
1891 g_hash_table_destroy (display_x11->xid_ht);
1893 XCloseDisplay (display_x11->xdisplay);
1895 G_OBJECT_CLASS (_gdk_display_x11_parent_class)->finalize (object);
1899 * gdk_x11_lookup_xdisplay:
1900 * @xdisplay: a pointer to an X Display
1902 * Find the #GdkDisplay corresponding to @display, if any exists.
1904 * Return value: the #GdkDisplay, if found, otherwise %NULL.
1909 gdk_x11_lookup_xdisplay (Display *xdisplay)
1913 for (tmp_list = _gdk_displays; tmp_list; tmp_list = tmp_list->next)
1915 if (GDK_DISPLAY_XDISPLAY (tmp_list->data) == xdisplay)
1916 return tmp_list->data;
1923 * _gdk_x11_display_screen_for_xrootwin:
1924 * @display: a #GdkDisplay
1925 * @xrootwin: window ID for one of of the screen's of the display.
1927 * Given the root window ID of one of the screen's of a #GdkDisplay,
1930 * Return value: the #GdkScreen corresponding to @xrootwin, or %NULL.
1933 _gdk_x11_display_screen_for_xrootwin (GdkDisplay *display,
1938 for (i = 0; i < ScreenCount (GDK_DISPLAY_X11 (display)->xdisplay); i++)
1940 GdkScreen *screen = gdk_display_get_screen (display, i);
1941 if (GDK_SCREEN_XROOTWIN (screen) == xrootwin)
1949 * gdk_x11_display_get_xdisplay:
1950 * @display: a #GdkDisplay
1951 * @returns: an X display.
1953 * Returns the X display of a #GdkDisplay.
1958 gdk_x11_display_get_xdisplay (GdkDisplay *display)
1960 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
1961 return GDK_DISPLAY_X11 (display)->xdisplay;
1965 _gdk_windowing_set_default_display (GdkDisplay *display)
1967 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
1968 const gchar *startup_id;
1976 gdk_display = GDK_DISPLAY_XDISPLAY (display);
1978 g_free (display_x11->startup_notification_id);
1979 display_x11->startup_notification_id = NULL;
1981 startup_id = g_getenv ("DESKTOP_STARTUP_ID");
1982 if (startup_id && *startup_id != '\0')
1986 if (!g_utf8_validate (startup_id, -1, NULL))
1987 g_warning ("DESKTOP_STARTUP_ID contains invalid UTF-8");
1989 display_x11->startup_notification_id = g_strdup (startup_id);
1991 /* Find the launch time from the startup_id, if it's there. Newer spec
1992 * states that the startup_id is of the form <unique>_TIME<timestamp>
1994 time_str = g_strrstr (startup_id, "_TIME");
1995 if (time_str != NULL)
2001 /* Skip past the "_TIME" part */
2004 retval = strtoul (time_str, &end, 0);
2005 if (end != time_str && errno == 0)
2006 display_x11->user_time = retval;
2009 /* Clear the environment variable so it won't be inherited by
2010 * child processes and confuse things.
2012 g_unsetenv ("DESKTOP_STARTUP_ID");
2014 /* Set the startup id on the leader window so it
2015 * applies to all windows we create on this display
2017 XChangeProperty (display_x11->xdisplay,
2018 display_x11->leader_window,
2019 gdk_x11_get_xatom_by_name_for_display (display, "_NET_STARTUP_ID"),
2020 gdk_x11_get_xatom_by_name_for_display (display, "UTF8_STRING"), 8,
2022 (guchar *)startup_id, strlen (startup_id));
2027 broadcast_xmessage (GdkDisplay *display,
2028 const char *message_type,
2029 const char *message_type_begin,
2030 const char *message)
2032 Display *xdisplay = GDK_DISPLAY_XDISPLAY (display);
2033 GdkScreen *screen = gdk_display_get_default_screen (display);
2034 GdkWindow *root_window = gdk_screen_get_root_window (screen);
2035 Window xroot_window = GDK_WINDOW_XID (root_window);
2038 Atom type_atom_begin;
2041 if (!G_LIKELY (GDK_DISPLAY_X11 (display)->trusted_client))
2045 XSetWindowAttributes attrs;
2047 attrs.override_redirect = True;
2048 attrs.event_mask = PropertyChangeMask | StructureNotifyMask;
2051 XCreateWindow (xdisplay,
2057 (Visual *)CopyFromParent,
2058 CWOverrideRedirect | CWEventMask,
2062 type_atom = gdk_x11_get_xatom_by_name_for_display (display,
2064 type_atom_begin = gdk_x11_get_xatom_by_name_for_display (display,
2065 message_type_begin);
2068 XClientMessageEvent xclient;
2070 const char *src_end;
2074 memset(&xclient, 0, sizeof (xclient));
2075 xclient.type = ClientMessage;
2076 xclient.message_type = type_atom_begin;
2077 xclient.display =xdisplay;
2078 xclient.window = xwindow;
2082 src_end = message + strlen (message) + 1; /* +1 to include nul byte */
2084 while (src != src_end)
2086 dest = &xclient.data.b[0];
2087 dest_end = dest + 20;
2089 while (dest != dest_end &&
2097 while (dest != dest_end)
2103 XSendEvent (xdisplay,
2107 (XEvent *)&xclient);
2109 xclient.message_type = type_atom;
2113 XDestroyWindow (xdisplay, xwindow);
2118 * gdk_x11_display_broadcast_startup_message:
2119 * @display: a #GdkDisplay
2120 * @message_type: startup notification message type ("new", "change",
2122 * @...: a list of key/value pairs (as strings), terminated by a
2123 * %NULL key. (A %NULL value for a key will cause that key to be
2124 * skipped in the output.)
2126 * Sends a startup notification message of type @message_type to
2129 * This is a convenience function for use by code that implements the
2130 * freedesktop startup notification specification. Applications should
2131 * not normally need to call it directly. See the <ulink
2132 * url="http://standards.freedesktop.org/startup-notification-spec/startup-notification-latest.txt">Startup
2133 * Notification Protocol specification</ulink> for
2134 * definitions of the message types and keys that can be used.
2139 gdk_x11_display_broadcast_startup_message (GdkDisplay *display,
2140 const char *message_type,
2145 const char *key, *value, *p;
2147 message = g_string_new (message_type);
2148 g_string_append_c (message, ':');
2150 va_start (ap, message_type);
2151 while ((key = va_arg (ap, const char *)))
2153 value = va_arg (ap, const char *);
2157 g_string_append_printf (message, " %s=\"", key);
2158 for (p = value; *p; p++)
2165 g_string_append_c (message, '\\');
2169 g_string_append_c (message, *p);
2171 g_string_append_c (message, '\"');
2175 broadcast_xmessage (display,
2176 "_NET_STARTUP_INFO",
2177 "_NET_STARTUP_INFO_BEGIN",
2180 g_string_free (message, TRUE);
2184 * gdk_notify_startup_complete:
2186 * Indicates to the GUI environment that the application has finished
2187 * loading. If the applications opens windows, this function is
2188 * normally called after opening the application's initial set of
2191 * GTK+ will call this function automatically after opening the first
2192 * #GtkWindow unless gtk_window_set_auto_startup_notification() is called
2193 * to disable that feature.
2198 gdk_notify_startup_complete (void)
2200 GdkDisplay *display;
2201 GdkDisplayX11 *display_x11;
2203 display = gdk_display_get_default ();
2207 display_x11 = GDK_DISPLAY_X11 (display);
2209 if (display_x11->startup_notification_id == NULL)
2212 gdk_notify_startup_complete_with_id (display_x11->startup_notification_id);
2216 * gdk_notify_startup_complete_with_id:
2217 * @startup_id: a startup-notification identifier, for which notification
2218 * process should be completed
2220 * Indicates to the GUI environment that the application has finished
2221 * loading, using a given identifier.
2223 * GTK+ will call this function automatically for #GtkWindow with custom
2224 * startup-notification identifier unless
2225 * gtk_window_set_auto_startup_notification() is called to disable
2231 gdk_notify_startup_complete_with_id (const gchar* startup_id)
2233 GdkDisplay *display;
2235 display = gdk_display_get_default ();
2239 gdk_x11_display_broadcast_startup_message (display, "remove",
2245 * gdk_display_supports_selection_notification:
2246 * @display: a #GdkDisplay
2248 * Returns whether #GdkEventOwnerChange events will be
2249 * sent when the owner of a selection changes.
2251 * Return value: whether #GdkEventOwnerChange events will
2257 gdk_display_supports_selection_notification (GdkDisplay *display)
2259 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
2261 return display_x11->have_xfixes;
2265 * gdk_display_request_selection_notification:
2266 * @display: a #GdkDisplay
2267 * @selection: the #GdkAtom naming the selection for which
2268 * ownership change notification is requested
2270 * Request #GdkEventOwnerChange events for ownership changes
2271 * of the selection named by the given atom.
2273 * Return value: whether #GdkEventOwnerChange events will
2279 gdk_display_request_selection_notification (GdkDisplay *display,
2284 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
2287 if (display_x11->have_xfixes)
2289 atom = gdk_x11_atom_to_xatom_for_display (display,
2291 XFixesSelectSelectionInput (display_x11->xdisplay,
2292 display_x11->leader_window,
2294 XFixesSetSelectionOwnerNotifyMask |
2295 XFixesSelectionWindowDestroyNotifyMask |
2296 XFixesSelectionClientCloseNotifyMask);
2305 * gdk_display_supports_clipboard_persistence
2306 * @display: a #GdkDisplay
2308 * Returns whether the speicifed display supports clipboard
2309 * persistance; i.e. if it's possible to store the clipboard data after an
2310 * application has quit. On X11 this checks if a clipboard daemon is
2313 * Returns: %TRUE if the display supports clipboard persistance.
2318 gdk_display_supports_clipboard_persistence (GdkDisplay *display)
2320 Atom clipboard_manager;
2322 /* It might make sense to cache this */
2323 clipboard_manager = gdk_x11_get_xatom_by_name_for_display (display, "CLIPBOARD_MANAGER");
2324 return XGetSelectionOwner (GDK_DISPLAY_X11 (display)->xdisplay, clipboard_manager) != None;
2328 * gdk_display_store_clipboard
2329 * @display: a #GdkDisplay
2330 * @clipboard_window: a #GdkWindow belonging to the clipboard owner
2331 * @time_: a timestamp
2332 * @targets: an array of targets that should be saved, or %NULL
2333 * if all available targets should be saved.
2334 * @n_targets: length of the @targets array
2336 * Issues a request to the clipboard manager to store the
2337 * clipboard data. On X11, this is a special program that works
2338 * according to the freedesktop clipboard specification, available at
2339 * <ulink url="http://www.freedesktop.org/Standards/clipboard-manager-spec">
2340 * http://www.freedesktop.org/Standards/clipboard-manager-spec</ulink>.
2345 gdk_display_store_clipboard (GdkDisplay *display,
2346 GdkWindow *clipboard_window,
2348 const GdkAtom *targets,
2351 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
2352 Atom clipboard_manager, save_targets;
2354 g_return_if_fail (GDK_WINDOW_IS_X11 (clipboard_window));
2356 clipboard_manager = gdk_x11_get_xatom_by_name_for_display (display, "CLIPBOARD_MANAGER");
2357 save_targets = gdk_x11_get_xatom_by_name_for_display (display, "SAVE_TARGETS");
2359 gdk_error_trap_push ();
2361 if (XGetSelectionOwner (display_x11->xdisplay, clipboard_manager) != None)
2363 Atom property_name = None;
2369 property_name = gdk_x11_atom_to_xatom_for_display (display, _gdk_selection_property);
2371 xatoms = g_new (Atom, n_targets);
2372 for (i = 0; i < n_targets; i++)
2373 xatoms[i] = gdk_x11_atom_to_xatom_for_display (display, targets[i]);
2375 XChangeProperty (display_x11->xdisplay, GDK_WINDOW_XID (clipboard_window),
2376 property_name, XA_ATOM,
2377 32, PropModeReplace, (guchar *)xatoms, n_targets);
2382 XConvertSelection (display_x11->xdisplay,
2383 clipboard_manager, save_targets, property_name,
2384 GDK_WINDOW_XID (clipboard_window), time_);
2387 gdk_error_trap_pop ();
2392 * gdk_x11_display_get_user_time:
2393 * @display: a #GdkDisplay
2395 * Returns the timestamp of the last user interaction on
2396 * @display. The timestamp is taken from events caused
2397 * by user interaction such as key presses or pointer
2398 * movements. See gdk_x11_window_set_user_time().
2400 * Returns: the timestamp of the last user interaction
2405 gdk_x11_display_get_user_time (GdkDisplay *display)
2407 return GDK_DISPLAY_X11 (display)->user_time;
2411 * gdk_display_supports_shapes:
2412 * @display: a #GdkDisplay
2414 * Returns %TRUE if gdk_window_shape_combine_mask() can
2415 * be used to create shaped windows on @display.
2417 * Returns: %TRUE if shaped windows are supported
2422 gdk_display_supports_shapes (GdkDisplay *display)
2424 return GDK_DISPLAY_X11 (display)->have_shapes;
2428 * gdk_display_supports_input_shapes:
2429 * @display: a #GdkDisplay
2431 * Returns %TRUE if gdk_window_input_shape_combine_mask() can
2432 * be used to modify the input shape of windows on @display.
2434 * Returns: %TRUE if windows with modified input shape are supported
2439 gdk_display_supports_input_shapes (GdkDisplay *display)
2441 return GDK_DISPLAY_X11 (display)->have_input_shapes;
2446 * gdk_x11_display_get_startup_notification_id:
2447 * @display: a #GdkDisplay
2449 * Gets the startup notification ID for a display.
2451 * Returns: the startup notification ID for @display
2455 G_CONST_RETURN gchar *
2456 gdk_x11_display_get_startup_notification_id (GdkDisplay *display)
2458 return GDK_DISPLAY_X11 (display)->startup_notification_id;
2462 * gdk_display_supports_composite:
2463 * @display: a #GdkDisplay
2465 * Returns %TRUE if gdk_window_set_composited() can be used
2466 * to redirect drawing on the window using compositing.
2468 * Currently this only works on X11 with XComposite and
2469 * XDamage extensions available.
2471 * Returns: %TRUE if windows may be composited.
2476 gdk_display_supports_composite (GdkDisplay *display)
2478 GdkDisplayX11 *x11_display = GDK_DISPLAY_X11 (display);
2480 return x11_display->have_xcomposite &&
2481 x11_display->have_xdamage &&
2482 x11_display->have_xfixes;
2486 * gdk_display_list_devices:
2487 * @display: a #GdkDisplay
2489 * Returns the list of available input devices attached to @display.
2490 * The list is statically allocated and should not be freed.
2492 * Return value: a list of #GdkDevice
2496 * Deprecated: 3.0: Use gdk_device_manager_list_devices() instead.
2499 gdk_display_list_devices (GdkDisplay *display)
2501 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
2503 return GDK_DISPLAY_X11 (display)->input_devices;
2507 * gdk_event_send_client_message_for_display:
2508 * @display: the #GdkDisplay for the window where the message is to be sent.
2509 * @event: the #GdkEvent to send, which should be a #GdkEventClient.
2510 * @winid: the window to send the client message to.
2512 * On X11, sends an X ClientMessage event to a given window. On
2513 * Windows, sends a message registered with the name
2514 * GDK_WIN32_CLIENT_MESSAGE.
2516 * This could be used for communicating between different
2517 * applications, though the amount of data is limited to 20 bytes on
2518 * X11, and to just four bytes on Windows.
2520 * Returns: non-zero on success.
2525 gdk_event_send_client_message_for_display (GdkDisplay *display,
2527 GdkNativeWindow winid)
2531 g_return_val_if_fail(event != NULL, FALSE);
2533 /* Set up our event to send, with the exception of its target window */
2534 sev.xclient.type = ClientMessage;
2535 sev.xclient.display = GDK_DISPLAY_XDISPLAY (display);
2536 sev.xclient.format = event->client.data_format;
2537 sev.xclient.window = winid;
2538 memcpy(&sev.xclient.data, &event->client.data, sizeof (sev.xclient.data));
2539 sev.xclient.message_type = gdk_x11_atom_to_xatom_for_display (display, event->client.message_type);
2541 return _gdk_send_xevent (display, winid, False, NoEventMask, &sev);
2545 * gdk_display_add_client_message_filter:
2546 * @display: a #GdkDisplay for which this message filter applies
2547 * @message_type: the type of ClientMessage events to receive.
2548 * This will be checked against the @message_type field
2549 * of the XClientMessage event struct.
2550 * @func: the function to call to process the event.
2551 * @data: user data to pass to @func.
2553 * Adds a filter to be called when X ClientMessage events are received.
2554 * See gdk_window_add_filter() if you are interested in filtering other
2560 gdk_display_add_client_message_filter (GdkDisplay *display,
2561 GdkAtom message_type,
2565 GdkClientFilter *filter;
2566 g_return_if_fail (GDK_IS_DISPLAY (display));
2567 filter = g_new (GdkClientFilter, 1);
2569 filter->type = message_type;
2570 filter->function = func;
2571 filter->data = data;
2573 GDK_DISPLAY_X11(display)->client_filters =
2574 g_list_append (GDK_DISPLAY_X11 (display)->client_filters,
2579 * gdk_add_client_message_filter:
2580 * @message_type: the type of ClientMessage events to receive. This will be
2581 * checked against the <structfield>message_type</structfield> field of the
2582 * XClientMessage event struct.
2583 * @func: the function to call to process the event.
2584 * @data: user data to pass to @func.
2586 * Adds a filter to the default display to be called when X ClientMessage events
2587 * are received. See gdk_display_add_client_message_filter().
2590 gdk_add_client_message_filter (GdkAtom message_type,
2594 gdk_display_add_client_message_filter (gdk_display_get_default (),
2595 message_type, func, data);
2599 *--------------------------------------------------------------
2602 * Flushes the Xlib output buffer and then waits
2603 * until all requests have been received and processed
2604 * by the X server. The only real use for this function
2605 * is in dealing with XShm.
2613 *--------------------------------------------------------------
2618 GSList *tmp_list = _gdk_displays;
2622 XSync (GDK_DISPLAY_XDISPLAY (tmp_list->data), False);
2623 tmp_list = tmp_list->next;
2628 * gdk_x11_register_standard_event_type:
2629 * @display: a #GdkDisplay
2630 * @event_base: first event type code to register
2631 * @n_events: number of event type codes to register
2633 * Registers interest in receiving extension events with type codes
2634 * between @event_base and <literal>event_base + n_events - 1</literal>.
2635 * The registered events must have the window field in the same place
2636 * as core X events (this is not the case for e.g. XKB extension events).
2638 * If an event type is registered, events of this type will go through
2639 * global and window-specific filters (see gdk_window_add_filter()).
2640 * Unregistered events will only go through global filters.
2641 * GDK may register the events of some X extensions on its own.
2643 * This function should only be needed in unusual circumstances, e.g.
2644 * when filtering XInput extension events on the root window.
2649 gdk_x11_register_standard_event_type (GdkDisplay *display,
2653 GdkEventTypeX11 *event_type;
2654 GdkDisplayX11 *display_x11;
2656 display_x11 = GDK_DISPLAY_X11 (display);
2657 event_type = g_new (GdkEventTypeX11, 1);
2659 event_type->base = event_base;
2660 event_type->n_events = n_events;
2662 display_x11->event_types = g_slist_prepend (display_x11->event_types, event_type);