1 /* GDK - The GIMP Drawing Kit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
22 * file for a list of people on the GTK+ Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
28 #include "gdkprivate-x11.h"
29 #include "gdkinternals.h"
31 #include "gdkscreen-x11.h"
32 #include "gdkdisplay-x11.h"
34 #include "gdkkeysyms.h"
36 #include "xsettings-client.h"
45 #include "gdkinputprivate.h"
48 #include <X11/XKBlib.h>
51 #include <X11/Xatom.h>
53 typedef struct _GdkIOClosure GdkIOClosure;
54 typedef struct _GdkDisplaySource GdkDisplaySource;
56 #define DOUBLE_CLICK_TIME 250
57 #define TRIPLE_CLICK_TIME 500
58 #define DOUBLE_CLICK_DIST 5
59 #define TRIPLE_CLICK_DIST 5
63 GdkInputFunction function;
64 GdkInputCondition condition;
65 GdkDestroyNotify notify;
69 struct _GdkDisplaySource
74 GPollFD event_poll_fd;
78 * Private function declarations
81 static gint gdk_event_apply_filters (XEvent *xevent,
84 static gboolean gdk_event_translate (GdkDisplay *display,
87 gboolean return_exposes);
89 static gboolean gdk_event_prepare (GSource *source,
91 static gboolean gdk_event_check (GSource *source);
92 static gboolean gdk_event_dispatch (GSource *source,
96 static GdkFilterReturn gdk_wm_protocols_filter (GdkXEvent *xev,
100 static GSource *gdk_display_source_new (GdkDisplay *display);
101 static gboolean gdk_check_xpending (GdkDisplay *display);
103 static void gdk_xsettings_watch_cb (Window window,
107 static void gdk_xsettings_notify_cb (const char *name,
108 XSettingsAction action,
109 XSettingsSetting *setting,
112 /* Private variable declarations
115 static GList *display_sources;
117 static GSourceFuncs event_funcs = {
125 gdk_display_source_new (GdkDisplay *display)
127 GSource *source = g_source_new (&event_funcs, sizeof (GdkDisplaySource));
128 GdkDisplaySource *display_source = (GdkDisplaySource *)source;
130 display_source->display = display;
136 gdk_check_xpending (GdkDisplay *display)
138 return XPending (GDK_DISPLAY_XDISPLAY (display));
141 /*********************************************
142 * Functions for maintaining the event queue *
143 *********************************************/
146 _gdk_x11_events_init_screen (GdkScreen *screen)
148 GdkScreenX11 *screen_x11 = GDK_SCREEN_X11 (screen);
150 /* Keep a flag to avoid extra notifies that we don't need
152 screen_x11->xsettings_in_init = TRUE;
153 screen_x11->xsettings_client = xsettings_client_new (screen_x11->xdisplay,
154 screen_x11->screen_num,
155 gdk_xsettings_notify_cb,
156 gdk_xsettings_watch_cb,
158 screen_x11->xsettings_in_init = FALSE;
162 _gdk_x11_events_uninit_screen (GdkScreen *screen)
164 GdkScreenX11 *screen_x11 = GDK_SCREEN_X11 (screen);
166 xsettings_client_destroy (screen_x11->xsettings_client);
167 screen_x11->xsettings_client = NULL;
171 _gdk_events_init (GdkDisplay *display)
174 GdkDisplaySource *display_source;
175 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
177 int connection_number = ConnectionNumber (display_x11->xdisplay);
178 GDK_NOTE (MISC, g_message ("connection number: %d", connection_number));
181 source = display_x11->event_source = gdk_display_source_new (display);
182 display_source = (GdkDisplaySource*) source;
183 g_source_set_priority (source, GDK_PRIORITY_EVENTS);
185 display_source->event_poll_fd.fd = connection_number;
186 display_source->event_poll_fd.events = G_IO_IN;
188 g_source_add_poll (source, &display_source->event_poll_fd);
189 g_source_set_can_recurse (source, TRUE);
190 g_source_attach (source, NULL);
192 display_sources = g_list_prepend (display_sources,display_source);
194 gdk_display_add_client_message_filter (
196 gdk_atom_intern ("WM_PROTOCOLS", FALSE),
197 gdk_wm_protocols_filter,
203 * gdk_events_pending:
205 * Checks if any events are ready to be processed for any display.
207 * Return value: %TRUE if any events are pending.
210 gdk_events_pending (void)
214 for (tmp_list = display_sources; tmp_list; tmp_list = tmp_list->next)
216 GdkDisplaySource *tmp_source = tmp_list->data;
217 GdkDisplay *display = tmp_source->display;
219 if (_gdk_event_queue_find_first (display))
223 for (tmp_list = display_sources; tmp_list; tmp_list = tmp_list->next)
225 GdkDisplaySource *tmp_source = tmp_list->data;
226 GdkDisplay *display = tmp_source->display;
228 if (gdk_check_xpending (display))
236 graphics_expose_predicate (Display *display,
240 if (xevent->xany.window == GDK_DRAWABLE_XID ((GdkDrawable *)arg) &&
241 (xevent->xany.type == GraphicsExpose ||
242 xevent->xany.type == NoExpose))
249 * gdk_event_get_graphics_expose:
250 * @window: the #GdkWindow to wait for the events for.
252 * Waits for a GraphicsExpose or NoExpose event from the X server.
253 * This is used in the #GtkText and #GtkCList widgets in GTK+ to make sure any
254 * GraphicsExpose events are handled before the widget is scrolled.
256 * Return value: a #GdkEventExpose if a GraphicsExpose was received, or %NULL if a
257 * NoExpose event was received.
260 gdk_event_get_graphics_expose (GdkWindow *window)
265 g_return_val_if_fail (window != NULL, NULL);
267 XIfEvent (GDK_WINDOW_XDISPLAY (window), &xevent,
268 graphics_expose_predicate, (XPointer) window);
270 if (xevent.xany.type == GraphicsExpose)
272 event = _gdk_event_new ();
274 if (gdk_event_translate (GDK_WINDOW_DISPLAY (window), event,
278 gdk_event_free (event);
285 gdk_event_apply_filters (XEvent *xevent,
290 GdkFilterReturn result;
296 GdkEventFilter *filter = (GdkEventFilter*) tmp_list->data;
298 tmp_list = tmp_list->next;
299 result = filter->function (xevent, event, filter->data);
300 if (result != GDK_FILTER_CONTINUE)
304 return GDK_FILTER_CONTINUE;
308 * gdk_display_add_client_message_filter:
309 * @display: a #GdkDisplay for which this message filter applies
310 * @message_type: the type of ClientMessage events to receive.
311 * This will be checked against the @message_type field
312 * of the XClientMessage event struct.
313 * @func: the function to call to process the event.
314 * @data: user data to pass to @func.
316 * Adds a filter to be called when X ClientMessage events are received.
320 gdk_display_add_client_message_filter (GdkDisplay *display,
321 GdkAtom message_type,
325 GdkClientFilter *filter;
326 g_return_if_fail (GDK_IS_DISPLAY (display));
327 filter = g_new (GdkClientFilter, 1);
329 filter->type = message_type;
330 filter->function = func;
333 GDK_DISPLAY_X11(display)->client_filters =
334 g_list_prepend (GDK_DISPLAY_X11 (display)->client_filters,
339 * gdk_add_client_message_filter:
340 * @message_type: the type of ClientMessage events to receive. This will be
341 * checked against the <structfield>message_type</structfield> field of the
342 * XClientMessage event struct.
343 * @func: the function to call to process the event.
344 * @data: user data to pass to @func.
346 * Adds a filter to the default display to be called when X ClientMessage events
347 * are received. See gdk_display_add_client_message_filter().
350 gdk_add_client_message_filter (GdkAtom message_type,
354 gdk_display_add_client_message_filter (gdk_display_get_default (),
355 message_type, func, data);
359 gdk_check_wm_state_changed (GdkWindow *window)
367 gboolean found_sticky, found_maxvert, found_maxhorz;
368 GdkWindowState old_state;
369 GdkDisplay *display = GDK_WINDOW_DISPLAY (window);
371 if (GDK_WINDOW_DESTROYED (window) ||
372 gdk_window_get_window_type (window) != GDK_WINDOW_TOPLEVEL)
375 found_sticky = FALSE;
376 found_maxvert = FALSE;
377 found_maxhorz = FALSE;
379 XGetWindowProperty (GDK_WINDOW_XDISPLAY (window), GDK_WINDOW_XID (window),
380 gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_STATE"),
381 0, G_MAXLONG, False, XA_ATOM, &type, &format, &nitems,
382 &bytes_after, (guchar **)&atoms);
386 Atom sticky_atom = gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_STATE_STICKY");
387 Atom maxvert_atom = gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_STATE_MAXIMIZED_VERT");
388 Atom maxhorz_atom = gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_STATE_MAXIMIZED_HORZ");
393 if (atoms[i] == sticky_atom)
395 else if (atoms[i] == maxvert_atom)
396 found_maxvert = TRUE;
397 else if (atoms[i] == maxhorz_atom)
398 found_maxhorz = TRUE;
406 /* For found_sticky to remain TRUE, we have to also be on desktop
414 XGetWindowProperty (GDK_WINDOW_XDISPLAY (window),
415 GDK_WINDOW_XID (window),
416 gdk_x11_get_xatom_by_name_for_display
417 (display, "_NET_WM_DESKTOP"),
418 0, G_MAXLONG, False, XA_CARDINAL, &type,
420 &bytes_after, (guchar **)&desktop);
424 if (*desktop != 0xFFFFFFFF)
425 found_sticky = FALSE;
430 old_state = gdk_window_get_state (window);
432 if (old_state & GDK_WINDOW_STATE_STICKY)
435 gdk_synthesize_window_state (window,
436 GDK_WINDOW_STATE_STICKY,
442 gdk_synthesize_window_state (window,
444 GDK_WINDOW_STATE_STICKY);
447 /* Our "maximized" means both vertical and horizontal; if only one,
448 * we don't expose that via GDK
450 if (old_state & GDK_WINDOW_STATE_MAXIMIZED)
452 if (!(found_maxvert && found_maxhorz))
453 gdk_synthesize_window_state (window,
454 GDK_WINDOW_STATE_MAXIMIZED,
459 if (found_maxvert && found_maxhorz)
460 gdk_synthesize_window_state (window,
462 GDK_WINDOW_STATE_MAXIMIZED);
466 #define HAS_FOCUS(window_impl) \
467 ((window_impl)->has_focus || (window_impl)->has_pointer_focus)
470 generate_focus_event (GdkWindow *window,
475 event.type = GDK_FOCUS_CHANGE;
476 event.focus_change.window = window;
477 event.focus_change.send_event = FALSE;
478 event.focus_change.in = in;
480 gdk_event_put (&event);
484 gdk_event_translate (GdkDisplay *display,
487 gboolean return_exposes)
491 GdkWindowObject *window_private;
492 GdkWindowImplX11 *window_impl = NULL;
493 static XComposeStatus compose;
498 gint xoffset, yoffset;
499 GdkScreen *screen = NULL;
500 GdkScreenX11 *screen_x11 = NULL;
501 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
505 /* init these, since the done: block uses them */
507 window_private = NULL;
508 event->any.window = NULL;
510 if (_gdk_default_filters)
512 /* Apply global filters */
513 GdkFilterReturn result;
514 result = gdk_event_apply_filters (xevent, event,
515 _gdk_default_filters);
517 if (result != GDK_FILTER_CONTINUE)
519 return_val = (result == GDK_FILTER_TRANSLATE) ? TRUE : FALSE;
524 /* We handle events with window=None
525 * specially - they are generated by XFree86's XInput under
526 * some circumstances. This handling for obvious reasons
527 * goes before we bother to lookup the event window.
530 if (xevent->xany.window == None)
532 return_val = _gdk_input_window_none_event (event, xevent);
534 if (return_val >= 0) /* was handled */
540 /* Find the GdkWindow that this event occurred in. */
542 window = gdk_window_lookup_for_display (display, xevent->xany.window);
543 window_private = (GdkWindowObject *) window;
547 screen = GDK_WINDOW_SCREEN (window);
548 screen_x11 = GDK_SCREEN_X11 (screen);
553 /* Window may be a pixmap, so check its type.
554 * (This check is probably too expensive unless
555 * GLib short-circuits an exact type match,
556 * which has been proposed)
558 if (GDK_IS_WINDOW (window))
560 window_impl = GDK_WINDOW_IMPL_X11 (window_private->impl);
562 if (xevent->xany.window != GDK_WINDOW_XID (window))
564 g_assert (xevent->xany.window == window_impl->focus_window);
566 switch (xevent->type)
570 xevent->xany.window = GDK_WINDOW_XID (window);
578 g_object_ref (G_OBJECT (window));
581 event->any.window = window;
582 event->any.send_event = xevent->xany.send_event ? TRUE : FALSE;
584 if (window_private && GDK_WINDOW_DESTROYED (window))
586 if (xevent->type != DestroyNotify)
592 else if (window_private)
594 /* Apply per-window filters */
595 GdkFilterReturn result;
596 result = gdk_event_apply_filters (xevent, event,
597 window_private->filters);
599 if (result != GDK_FILTER_CONTINUE)
601 return_val = (result == GDK_FILTER_TRANSLATE) ? TRUE : FALSE;
606 if (screen_x11 && screen_x11->wmspec_check_window != None &&
607 xevent->xany.window == screen_x11->wmspec_check_window)
609 if (xevent->type == DestroyNotify)
610 screen_x11->wmspec_check_window = None;
612 /* Eat events on this window unless someone had wrapped
613 * it as a foreign window
623 (xevent->xany.type == MotionNotify ||
624 xevent->xany.type == ButtonRelease))
626 if (_gdk_moveresize_handle_event (xevent))
633 /* We do a "manual" conversion of the XEvent to a
634 * GdkEvent. The structures are mostly the same so
635 * the conversion is fairly straightforward. We also
636 * optionally print debugging info regarding events
644 _gdk_windowing_window_get_offsets (window, &xoffset, &yoffset);
652 switch (xevent->type)
655 if (window_private == NULL)
661 /* Lookup the string corresponding to the given keysym.
664 charcount = XLookupString (&xevent->xkey, buf, 16,
666 event->key.keyval = keysym;
667 event->key.hardware_keycode = xevent->xkey.keycode;
669 if (charcount > 0 && buf[charcount-1] == '\0')
672 buf[charcount] = '\0';
674 #ifdef G_ENABLE_DEBUG
675 if (_gdk_debug_flags & GDK_DEBUG_EVENTS)
677 g_message ("key press:\twindow: %ld key: %12s %d",
679 event->key.keyval ? XKeysymToString (event->key.keyval) : "(none)",
682 g_message ("\t\tlength: %4d string: \"%s\"",
685 #endif /* G_ENABLE_DEBUG */
687 event->key.type = GDK_KEY_PRESS;
688 event->key.window = window;
689 event->key.time = xevent->xkey.time;
690 event->key.state = (GdkModifierType) xevent->xkey.state;
691 event->key.string = g_strdup (buf);
692 event->key.length = charcount;
694 /* bits 13 and 14 in the "state" field are the keyboard group */
695 #define KEYBOARD_GROUP_SHIFT 13
696 #define KEYBOARD_GROUP_MASK ((1 << 13) | (1 << 14))
698 event->key.group = _gdk_x11_get_group_for_state (display, xevent->xkey.state);
703 if (window_private == NULL)
709 /* Lookup the string corresponding to the given keysym.
712 /* Emulate detectable auto-repeat by checking to see
713 * if the next event is a key press with the same
714 * keycode and timestamp, and if so, ignoring the event.
717 if (!display_x11->have_xkb_autorepeat && XPending (xevent->xkey.display))
721 XPeekEvent (xevent->xkey.display, &next_event);
723 if (next_event.type == KeyPress &&
724 next_event.xkey.keycode == xevent->xkey.keycode &&
725 next_event.xkey.time == xevent->xkey.time)
729 keysym = GDK_VoidSymbol;
730 charcount = XLookupString (&xevent->xkey, buf, 16,
732 event->key.keyval = keysym;
733 event->key.hardware_keycode = xevent->xkey.keycode;
736 g_message ("key release:\t\twindow: %ld key: %12s %d",
738 XKeysymToString (event->key.keyval),
741 event->key.type = GDK_KEY_RELEASE;
742 event->key.window = window;
743 event->key.time = xevent->xkey.time;
744 event->key.state = (GdkModifierType) xevent->xkey.state;
745 event->key.length = 0;
746 event->key.string = NULL;
748 event->key.group = (xevent->xkey.state & KEYBOARD_GROUP_MASK) >> KEYBOARD_GROUP_SHIFT;
754 g_message ("button press:\t\twindow: %ld x,y: %d %d button: %d",
755 xevent->xbutton.window,
756 xevent->xbutton.x, xevent->xbutton.y,
757 xevent->xbutton.button));
759 if (window_private == NULL ||
760 ((window_private->extension_events != 0) &&
761 display_x11->input_ignore_core))
767 /* If we get a ButtonPress event where the button is 4 or 5,
768 it's a Scroll event */
769 switch (xevent->xbutton.button)
775 event->scroll.type = GDK_SCROLL;
777 if (xevent->xbutton.button == 4)
778 event->scroll.direction = GDK_SCROLL_UP;
779 else if (xevent->xbutton.button == 5)
780 event->scroll.direction = GDK_SCROLL_DOWN;
781 else if (xevent->xbutton.button == 6)
782 event->scroll.direction = GDK_SCROLL_LEFT;
784 event->scroll.direction = GDK_SCROLL_RIGHT;
786 event->scroll.window = window;
787 event->scroll.time = xevent->xbutton.time;
788 event->scroll.x = xevent->xbutton.x + xoffset;
789 event->scroll.y = xevent->xbutton.y + yoffset;
790 event->scroll.x_root = (gfloat)xevent->xbutton.x_root;
791 event->scroll.y_root = (gfloat)xevent->xbutton.y_root;
792 event->scroll.state = (GdkModifierType) xevent->xbutton.state;
793 event->scroll.device = display->core_pointer;
797 event->button.type = GDK_BUTTON_PRESS;
798 event->button.window = window;
799 event->button.time = xevent->xbutton.time;
800 event->button.x = xevent->xbutton.x + xoffset;
801 event->button.y = xevent->xbutton.y + yoffset;
802 event->button.x_root = (gfloat)xevent->xbutton.x_root;
803 event->button.y_root = (gfloat)xevent->xbutton.y_root;
804 event->button.axes = NULL;
805 event->button.state = (GdkModifierType) xevent->xbutton.state;
806 event->button.button = xevent->xbutton.button;
807 event->button.device = display->core_pointer;
809 _gdk_event_button_generate (display, event);
817 g_message ("button release:\twindow: %ld x,y: %d %d button: %d",
818 xevent->xbutton.window,
819 xevent->xbutton.x, xevent->xbutton.y,
820 xevent->xbutton.button));
822 if (window_private == NULL ||
823 ((window_private->extension_events != 0) &&
824 display_x11->input_ignore_core))
830 /* We treat button presses as scroll wheel events, so ignore the release */
831 if (xevent->xbutton.button == 4 || xevent->xbutton.button == 5 ||
832 xevent->xbutton.button == 6 || xevent->xbutton.button ==7)
838 event->button.type = GDK_BUTTON_RELEASE;
839 event->button.window = window;
840 event->button.time = xevent->xbutton.time;
841 event->button.x = xevent->xbutton.x + xoffset;
842 event->button.y = xevent->xbutton.y + yoffset;
843 event->button.x_root = (gfloat)xevent->xbutton.x_root;
844 event->button.y_root = (gfloat)xevent->xbutton.y_root;
845 event->button.axes = NULL;
846 event->button.state = (GdkModifierType) xevent->xbutton.state;
847 event->button.button = xevent->xbutton.button;
848 event->button.device = display->core_pointer;
854 g_message ("motion notify:\t\twindow: %ld x,y: %d %d hint: %s",
855 xevent->xmotion.window,
856 xevent->xmotion.x, xevent->xmotion.y,
857 (xevent->xmotion.is_hint) ? "true" : "false"));
859 if (window_private == NULL ||
860 ((window_private->extension_events != 0) &&
861 display_x11->input_ignore_core))
867 event->motion.type = GDK_MOTION_NOTIFY;
868 event->motion.window = window;
869 event->motion.time = xevent->xmotion.time;
870 event->motion.x = xevent->xmotion.x + xoffset;
871 event->motion.y = xevent->xmotion.y + yoffset;
872 event->motion.x_root = (gfloat)xevent->xmotion.x_root;
873 event->motion.y_root = (gfloat)xevent->xmotion.y_root;
874 event->motion.axes = NULL;
875 event->motion.state = (GdkModifierType) xevent->xmotion.state;
876 event->motion.is_hint = xevent->xmotion.is_hint;
877 event->motion.device = display->core_pointer;
883 g_message ("enter notify:\t\twindow: %ld detail: %d subwin: %ld",
884 xevent->xcrossing.window,
885 xevent->xcrossing.detail,
886 xevent->xcrossing.subwindow));
888 if (window_private == NULL)
894 /* Handle focusing (in the case where no window manager is running */
896 GDK_WINDOW_TYPE (window) != GDK_WINDOW_CHILD &&
897 xevent->xcrossing.detail != NotifyInferior &&
898 xevent->xcrossing.focus && !window_impl->has_focus)
900 gboolean had_focus = HAS_FOCUS (window_impl);
902 window_impl->has_pointer_focus = TRUE;
904 if (HAS_FOCUS (window_impl) != had_focus)
905 generate_focus_event (window, TRUE);
908 /* Tell XInput stuff about it if appropriate */
909 if (window_private &&
910 !GDK_WINDOW_DESTROYED (window) &&
911 window_private->extension_events != 0)
912 _gdk_input_enter_event (&xevent->xcrossing, window);
914 event->crossing.type = GDK_ENTER_NOTIFY;
915 event->crossing.window = window;
917 /* If the subwindow field of the XEvent is non-NULL, then
918 * lookup the corresponding GdkWindow.
920 if (xevent->xcrossing.subwindow != None)
921 event->crossing.subwindow = gdk_window_lookup_for_display (display, xevent->xcrossing.subwindow);
923 event->crossing.subwindow = NULL;
925 event->crossing.time = xevent->xcrossing.time;
926 event->crossing.x = xevent->xcrossing.x + xoffset;
927 event->crossing.y = xevent->xcrossing.y + yoffset;
928 event->crossing.x_root = xevent->xcrossing.x_root;
929 event->crossing.y_root = xevent->xcrossing.y_root;
931 /* Translate the crossing mode into Gdk terms.
933 switch (xevent->xcrossing.mode)
936 event->crossing.mode = GDK_CROSSING_NORMAL;
939 event->crossing.mode = GDK_CROSSING_GRAB;
942 event->crossing.mode = GDK_CROSSING_UNGRAB;
946 /* Translate the crossing detail into Gdk terms.
948 switch (xevent->xcrossing.detail)
951 event->crossing.detail = GDK_NOTIFY_INFERIOR;
954 event->crossing.detail = GDK_NOTIFY_ANCESTOR;
957 event->crossing.detail = GDK_NOTIFY_VIRTUAL;
959 case NotifyNonlinear:
960 event->crossing.detail = GDK_NOTIFY_NONLINEAR;
962 case NotifyNonlinearVirtual:
963 event->crossing.detail = GDK_NOTIFY_NONLINEAR_VIRTUAL;
966 event->crossing.detail = GDK_NOTIFY_UNKNOWN;
970 event->crossing.focus = xevent->xcrossing.focus;
971 event->crossing.state = xevent->xcrossing.state;
977 g_message ("leave notify:\t\twindow: %ld detail: %d subwin: %ld",
978 xevent->xcrossing.window,
979 xevent->xcrossing.detail, xevent->xcrossing.subwindow));
981 if (window_private == NULL)
987 /* Handle focusing (in the case where no window manager is running */
989 GDK_WINDOW_TYPE (window) != GDK_WINDOW_CHILD &&
990 xevent->xcrossing.detail != NotifyInferior &&
991 xevent->xcrossing.focus && !window_impl->has_focus)
993 gboolean had_focus = HAS_FOCUS (window_impl);
995 window_impl->has_pointer_focus = FALSE;
997 if (HAS_FOCUS (window_impl) != had_focus)
998 generate_focus_event (window, FALSE);
1001 event->crossing.type = GDK_LEAVE_NOTIFY;
1002 event->crossing.window = window;
1004 /* If the subwindow field of the XEvent is non-NULL, then
1005 * lookup the corresponding GdkWindow.
1007 if (xevent->xcrossing.subwindow != None)
1008 event->crossing.subwindow = gdk_window_lookup_for_display (display, xevent->xcrossing.subwindow);
1010 event->crossing.subwindow = NULL;
1012 event->crossing.time = xevent->xcrossing.time;
1013 event->crossing.x = xevent->xcrossing.x + xoffset;
1014 event->crossing.y = xevent->xcrossing.y + yoffset;
1015 event->crossing.x_root = xevent->xcrossing.x_root;
1016 event->crossing.y_root = xevent->xcrossing.y_root;
1018 /* Translate the crossing mode into Gdk terms.
1020 switch (xevent->xcrossing.mode)
1023 event->crossing.mode = GDK_CROSSING_NORMAL;
1026 event->crossing.mode = GDK_CROSSING_GRAB;
1029 event->crossing.mode = GDK_CROSSING_UNGRAB;
1033 /* Translate the crossing detail into Gdk terms.
1035 switch (xevent->xcrossing.detail)
1037 case NotifyInferior:
1038 event->crossing.detail = GDK_NOTIFY_INFERIOR;
1040 case NotifyAncestor:
1041 event->crossing.detail = GDK_NOTIFY_ANCESTOR;
1044 event->crossing.detail = GDK_NOTIFY_VIRTUAL;
1046 case NotifyNonlinear:
1047 event->crossing.detail = GDK_NOTIFY_NONLINEAR;
1049 case NotifyNonlinearVirtual:
1050 event->crossing.detail = GDK_NOTIFY_NONLINEAR_VIRTUAL;
1053 event->crossing.detail = GDK_NOTIFY_UNKNOWN;
1057 event->crossing.focus = xevent->xcrossing.focus;
1058 event->crossing.state = xevent->xcrossing.state;
1062 /* We only care about focus events that indicate that _this_
1063 * window (not a ancestor or child) got or lost the focus
1067 g_message ("focus in:\t\twindow: %ld", xevent->xfocus.window));
1069 if (window && GDK_WINDOW_TYPE (window) != GDK_WINDOW_CHILD)
1071 gboolean had_focus = HAS_FOCUS (window_impl);
1073 switch (xevent->xfocus.detail)
1075 case NotifyAncestor:
1076 case NotifyNonlinear:
1078 case NotifyNonlinearVirtual:
1079 /* We pretend that the focus moves to the grab
1080 * window, so we pay attention to NotifyGrab
1081 * NotifyUngrab, and ignore NotifyWhileGrabbed
1083 if (xevent->xfocus.mode != NotifyWhileGrabbed)
1084 window_impl->has_focus = TRUE;
1087 /* The X server sends NotifyPointer/NotifyGrab,
1088 * but the pointer focus is ignored while a
1091 if (xevent->xfocus.mode != NotifyGrab)
1092 window_impl->has_pointer_focus = TRUE;
1094 case NotifyInferior:
1095 case NotifyPointerRoot:
1096 case NotifyDetailNone:
1100 if (HAS_FOCUS (window_impl) != had_focus)
1101 generate_focus_event (window, TRUE);
1106 g_message ("focus out:\t\twindow: %ld", xevent->xfocus.window));
1108 if (window && GDK_WINDOW_TYPE (window) != GDK_WINDOW_CHILD)
1110 gboolean had_focus = HAS_FOCUS (window_impl);
1112 switch (xevent->xfocus.detail)
1114 case NotifyAncestor:
1115 case NotifyNonlinear:
1117 case NotifyNonlinearVirtual:
1118 if (xevent->xfocus.mode != NotifyWhileGrabbed)
1119 window_impl->has_focus = FALSE;
1122 if (xevent->xfocus.mode != NotifyUngrab)
1123 window_impl->has_pointer_focus = FALSE;
1125 case NotifyInferior:
1126 case NotifyPointerRoot:
1127 case NotifyDetailNone:
1131 if (HAS_FOCUS (window_impl) != had_focus)
1132 generate_focus_event (window, FALSE);
1137 /* gdk_keyboard_grab() causes following events. These events confuse
1138 * the XIM focus, so ignore them.
1140 if (xevent->xfocus.mode == NotifyGrab ||
1141 xevent->xfocus.mode == NotifyUngrab)
1147 g_message ("keymap notify"));
1149 /* Not currently handled */
1155 g_message ("expose:\t\twindow: %ld %d x,y: %d %d w,h: %d %d%s",
1156 xevent->xexpose.window, xevent->xexpose.count,
1157 xevent->xexpose.x, xevent->xexpose.y,
1158 xevent->xexpose.width, xevent->xexpose.height,
1159 event->any.send_event ? " (send)" : ""));
1161 if (window_private == NULL)
1168 GdkRectangle expose_rect;
1170 expose_rect.x = xevent->xexpose.x + xoffset;
1171 expose_rect.y = xevent->xexpose.y + yoffset;
1172 expose_rect.width = xevent->xexpose.width;
1173 expose_rect.height = xevent->xexpose.height;
1177 event->expose.type = GDK_EXPOSE;
1178 event->expose.area = expose_rect;
1179 event->expose.region = gdk_region_rectangle (&expose_rect);
1180 event->expose.window = window;
1181 event->expose.count = xevent->xexpose.count;
1187 _gdk_window_process_expose (window, xevent->xexpose.serial, &expose_rect);
1196 case GraphicsExpose:
1198 GdkRectangle expose_rect;
1201 g_message ("graphics expose:\tdrawable: %ld",
1202 xevent->xgraphicsexpose.drawable));
1204 if (window_private == NULL)
1210 expose_rect.x = xevent->xgraphicsexpose.x + xoffset;
1211 expose_rect.y = xevent->xgraphicsexpose.y + yoffset;
1212 expose_rect.width = xevent->xgraphicsexpose.width;
1213 expose_rect.height = xevent->xgraphicsexpose.height;
1217 event->expose.type = GDK_EXPOSE;
1218 event->expose.area = expose_rect;
1219 event->expose.region = gdk_region_rectangle (&expose_rect);
1220 event->expose.window = window;
1221 event->expose.count = xevent->xgraphicsexpose.count;
1227 _gdk_window_process_expose (window, xevent->xgraphicsexpose.serial, &expose_rect);
1237 g_message ("no expose:\t\tdrawable: %ld",
1238 xevent->xnoexpose.drawable));
1240 event->no_expose.type = GDK_NO_EXPOSE;
1241 event->no_expose.window = window;
1245 case VisibilityNotify:
1246 #ifdef G_ENABLE_DEBUG
1247 if (_gdk_debug_flags & GDK_DEBUG_EVENTS)
1248 switch (xevent->xvisibility.state)
1250 case VisibilityFullyObscured:
1251 g_message ("visibility notify:\twindow: %ld none",
1252 xevent->xvisibility.window);
1254 case VisibilityPartiallyObscured:
1255 g_message ("visibility notify:\twindow: %ld partial",
1256 xevent->xvisibility.window);
1258 case VisibilityUnobscured:
1259 g_message ("visibility notify:\twindow: %ld full",
1260 xevent->xvisibility.window);
1263 #endif /* G_ENABLE_DEBUG */
1265 if (window_private == NULL)
1271 event->visibility.type = GDK_VISIBILITY_NOTIFY;
1272 event->visibility.window = window;
1274 switch (xevent->xvisibility.state)
1276 case VisibilityFullyObscured:
1277 event->visibility.state = GDK_VISIBILITY_FULLY_OBSCURED;
1280 case VisibilityPartiallyObscured:
1281 event->visibility.state = GDK_VISIBILITY_PARTIAL;
1284 case VisibilityUnobscured:
1285 event->visibility.state = GDK_VISIBILITY_UNOBSCURED;
1293 g_message ("create notify:\twindow: %ld x,y: %d %d w,h: %d %d b-w: %d parent: %ld ovr: %d",
1294 xevent->xcreatewindow.window,
1295 xevent->xcreatewindow.x,
1296 xevent->xcreatewindow.y,
1297 xevent->xcreatewindow.width,
1298 xevent->xcreatewindow.height,
1299 xevent->xcreatewindow.border_width,
1300 xevent->xcreatewindow.parent,
1301 xevent->xcreatewindow.override_redirect));
1302 /* not really handled */
1307 g_message ("destroy notify:\twindow: %ld",
1308 xevent->xdestroywindow.window));
1310 /* Ignore DestroyNotify from SubstructureNotifyMask */
1311 if (xevent->xdestroywindow.window == xevent->xdestroywindow.event)
1313 event->any.type = GDK_DESTROY;
1314 event->any.window = window;
1316 return_val = window_private && !GDK_WINDOW_DESTROYED (window);
1318 if (window && GDK_WINDOW_XID (window) != screen_x11->xroot_window)
1319 gdk_window_destroy_notify (window);
1328 g_message ("unmap notify:\t\twindow: %ld",
1329 xevent->xmap.window));
1331 event->any.type = GDK_UNMAP;
1332 event->any.window = window;
1334 /* If we are shown (not withdrawn) and get an unmap, it means we
1335 * were iconified in the X sense. If we are withdrawn, and get
1336 * an unmap, it means we hid the window ourselves, so we
1337 * will have already flipped the iconified bit off.
1341 if (GDK_WINDOW_IS_MAPPED (window))
1342 gdk_synthesize_window_state (window,
1344 GDK_WINDOW_STATE_ICONIFIED);
1346 _gdk_xgrab_check_unmap (window, xevent->xany.serial);
1353 g_message ("map notify:\t\twindow: %ld",
1354 xevent->xmap.window));
1356 event->any.type = GDK_MAP;
1357 event->any.window = window;
1359 /* Unset iconified if it was set */
1360 if (window && (((GdkWindowObject*)window)->state & GDK_WINDOW_STATE_ICONIFIED))
1361 gdk_synthesize_window_state (window,
1362 GDK_WINDOW_STATE_ICONIFIED,
1367 case ReparentNotify:
1369 g_message ("reparent notify:\twindow: %ld x,y: %d %d parent: %ld ovr: %d",
1370 xevent->xreparent.window,
1371 xevent->xreparent.x,
1372 xevent->xreparent.y,
1373 xevent->xreparent.parent,
1374 xevent->xreparent.override_redirect));
1376 /* Not currently handled */
1380 case ConfigureNotify:
1382 g_message ("configure notify:\twindow: %ld x,y: %d %d w,h: %d %d b-w: %d above: %ld ovr: %d%s",
1383 xevent->xconfigure.window,
1384 xevent->xconfigure.x,
1385 xevent->xconfigure.y,
1386 xevent->xconfigure.width,
1387 xevent->xconfigure.height,
1388 xevent->xconfigure.border_width,
1389 xevent->xconfigure.above,
1390 xevent->xconfigure.override_redirect,
1393 : GDK_WINDOW_TYPE (window) == GDK_WINDOW_CHILD
1394 ? " (discarding child)"
1395 : xevent->xconfigure.event != xevent->xconfigure.window
1396 ? " (discarding substructure)"
1399 xevent->xconfigure.event == xevent->xconfigure.window &&
1400 !GDK_WINDOW_DESTROYED (window) &&
1401 (window_private->extension_events != 0))
1402 _gdk_input_configure_event (&xevent->xconfigure, window);
1405 xevent->xconfigure.event != xevent->xconfigure.window ||
1406 GDK_WINDOW_TYPE (window) == GDK_WINDOW_CHILD ||
1407 GDK_WINDOW_TYPE (window) == GDK_WINDOW_ROOT)
1411 event->configure.type = GDK_CONFIGURE;
1412 event->configure.window = window;
1413 event->configure.width = xevent->xconfigure.width;
1414 event->configure.height = xevent->xconfigure.height;
1416 if (!xevent->xconfigure.send_event &&
1417 !GDK_WINDOW_DESTROYED (window))
1421 Window child_window = 0;
1423 gdk_error_trap_push ();
1424 if (XTranslateCoordinates (GDK_DRAWABLE_XDISPLAY (window),
1425 GDK_DRAWABLE_XID (window),
1426 screen_x11->xroot_window,
1431 if (!gdk_error_trap_pop ())
1433 event->configure.x = tx;
1434 event->configure.y = ty;
1438 gdk_error_trap_pop ();
1442 event->configure.x = xevent->xconfigure.x;
1443 event->configure.y = xevent->xconfigure.y;
1445 window_private->x = event->configure.x;
1446 window_private->y = event->configure.y;
1447 GDK_WINDOW_IMPL_X11 (window_private->impl)->width = xevent->xconfigure.width;
1448 GDK_WINDOW_IMPL_X11 (window_private->impl)->height = xevent->xconfigure.height;
1449 if (window_private->resize_count >= 1)
1451 window_private->resize_count -= 1;
1453 if (window_private->resize_count == 0)
1454 _gdk_moveresize_configure_done (display, window);
1459 case PropertyNotify:
1461 g_message ("property notify:\twindow: %ld, atom(%ld): %s%s%s",
1462 xevent->xproperty.window,
1463 xevent->xproperty.atom,
1465 gdk_x11_get_xatom_name_for_display (display, xevent->xproperty.atom),
1468 if (window_private == NULL)
1474 if (xevent->xproperty.atom == gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_STATE") ||
1475 xevent->xproperty.atom == gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_DESKTOP"))
1477 /* If window state changed, then synthesize those events. */
1478 gdk_check_wm_state_changed (window);
1481 if (window_private->event_mask & GDK_PROPERTY_CHANGE_MASK)
1483 event->property.type = GDK_PROPERTY_NOTIFY;
1484 event->property.window = window;
1485 event->property.atom = gdk_x11_xatom_to_atom_for_display (display, xevent->xproperty.atom);
1486 event->property.time = xevent->xproperty.time;
1487 event->property.state = xevent->xproperty.state;
1494 case SelectionClear:
1496 g_message ("selection clear:\twindow: %ld",
1497 xevent->xproperty.window));
1499 if (_gdk_selection_filter_clear_event (&xevent->xselectionclear))
1501 event->selection.type = GDK_SELECTION_CLEAR;
1502 event->selection.window = window;
1503 event->selection.selection = gdk_x11_xatom_to_atom_for_display (display, xevent->xselectionclear.selection);
1504 event->selection.time = xevent->xselectionclear.time;
1511 case SelectionRequest:
1513 g_message ("selection request:\twindow: %ld",
1514 xevent->xproperty.window));
1516 event->selection.type = GDK_SELECTION_REQUEST;
1517 event->selection.window = window;
1518 event->selection.selection = gdk_x11_xatom_to_atom_for_display (display, xevent->xselectionrequest.selection);
1519 event->selection.target = gdk_x11_xatom_to_atom_for_display (display, xevent->xselectionrequest.target);
1520 event->selection.property = gdk_x11_xatom_to_atom_for_display (display, xevent->xselectionrequest.property);
1521 event->selection.requestor = xevent->xselectionrequest.requestor;
1522 event->selection.time = xevent->xselectionrequest.time;
1526 case SelectionNotify:
1528 g_message ("selection notify:\twindow: %ld",
1529 xevent->xproperty.window));
1532 event->selection.type = GDK_SELECTION_NOTIFY;
1533 event->selection.window = window;
1534 event->selection.selection = gdk_x11_xatom_to_atom_for_display (display, xevent->xselection.selection);
1535 event->selection.target = gdk_x11_xatom_to_atom_for_display (display, xevent->xselection.target);
1536 event->selection.property = gdk_x11_xatom_to_atom_for_display (display, xevent->xselection.property);
1537 event->selection.time = xevent->xselection.time;
1541 case ColormapNotify:
1543 g_message ("colormap notify:\twindow: %ld",
1544 xevent->xcolormap.window));
1546 /* Not currently handled */
1553 GdkFilterReturn result = GDK_FILTER_CONTINUE;
1554 GdkAtom message_type = gdk_x11_xatom_to_atom_for_display (display, xevent->xclient.message_type);
1557 g_message ("client message:\twindow: %ld",
1558 xevent->xclient.window));
1560 tmp_list = display_x11->client_filters;
1563 GdkClientFilter *filter = tmp_list->data;
1564 if (filter->type == message_type)
1566 result = (*filter->function) (xevent, event, filter->data);
1570 tmp_list = tmp_list->next;
1575 case GDK_FILTER_REMOVE:
1578 case GDK_FILTER_TRANSLATE:
1581 case GDK_FILTER_CONTINUE:
1582 /* Send unknown ClientMessage's on to Gtk for it to use */
1583 if (window_private == NULL)
1589 event->client.type = GDK_CLIENT_EVENT;
1590 event->client.window = window;
1591 event->client.message_type = message_type;
1592 event->client.data_format = xevent->xclient.format;
1593 memcpy(&event->client.data, &xevent->xclient.data,
1594 sizeof(event->client.data));
1604 g_message ("mapping notify"));
1606 /* Let XLib know that there is a new keyboard mapping.
1608 XRefreshKeyboardMapping (&xevent->xmapping);
1609 _gdk_keymap_keys_changed (display);
1615 if (xevent->type == display_x11->xkb_event_type)
1617 XkbEvent *xkb_event = (XkbEvent *)xevent;
1619 switch (xkb_event->any.xkb_type)
1622 _gdk_keymap_keys_changed (display);
1627 case XkbStateNotify:
1628 _gdk_keymap_state_changed (display);
1635 /* something else - (e.g., a Xinput event) */
1637 if (window_private &&
1638 !GDK_WINDOW_DESTROYED (window_private) &&
1639 (window_private->extension_events != 0))
1640 return_val = _gdk_input_other_event(event, xevent, window);
1651 if (event->any.window)
1652 gdk_window_ref (event->any.window);
1653 if (((event->any.type == GDK_ENTER_NOTIFY) ||
1654 (event->any.type == GDK_LEAVE_NOTIFY)) &&
1655 (event->crossing.subwindow != NULL))
1656 gdk_window_ref (event->crossing.subwindow);
1660 /* Mark this event as having no resources to be freed */
1661 event->any.window = NULL;
1662 event->any.type = GDK_NOTHING;
1666 gdk_window_unref (window);
1671 static GdkFilterReturn
1672 gdk_wm_protocols_filter (GdkXEvent *xev,
1676 XEvent *xevent = (XEvent *)xev;
1677 GdkWindow *win = event->any.window;
1678 GdkDisplay *display = GDK_WINDOW_DISPLAY (win);
1680 if ((Atom) xevent->xclient.data.l[0] == gdk_x11_get_xatom_by_name_for_display (display, "WM_DELETE_WINDOW"))
1682 /* The delete window request specifies a window
1683 * to delete. We don't actually destroy the
1684 * window because "it is only a request". (The
1685 * window might contain vital data that the
1686 * program does not want destroyed). Instead
1687 * the event is passed along to the program,
1688 * which should then destroy the window.
1691 g_message ("delete window:\t\twindow: %ld",
1692 xevent->xclient.window));
1694 event->any.type = GDK_DELETE;
1696 return GDK_FILTER_TRANSLATE;
1698 else if ((Atom) xevent->xclient.data.l[0] == gdk_x11_get_xatom_by_name_for_display (display, "WM_TAKE_FOCUS"))
1700 GdkWindow *win = event->any.window;
1701 Window focus_win = GDK_WINDOW_IMPL_X11(((GdkWindowObject *)win)->impl)->focus_window;
1703 /* There is no way of knowing reliably whether we are viewable so we need
1704 * to trap errors so we don't cause a BadMatch.
1706 gdk_error_trap_push ();
1707 XSetInputFocus (GDK_WINDOW_XDISPLAY (win),
1710 xevent->xclient.data.l[1]);
1711 XSync (GDK_WINDOW_XDISPLAY (win), False);
1712 gdk_error_trap_pop ();
1714 else if ((Atom) xevent->xclient.data.l[0] == gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_PING"))
1716 XEvent xev = *xevent;
1718 xev.xclient.window = GDK_WINDOW_XROOTWIN (win);
1719 XSendEvent (GDK_WINDOW_XDISPLAY (win),
1722 SubstructureRedirectMask | SubstructureNotifyMask, &xev);
1725 return GDK_FILTER_REMOVE;
1729 _gdk_events_queue (GdkDisplay *display)
1734 Display *xdisplay = GDK_DISPLAY_XDISPLAY (display);
1736 while (!_gdk_event_queue_find_first(display) && XPending (xdisplay))
1738 XNextEvent (xdisplay, &xevent);
1740 switch (xevent.type)
1746 if (XFilterEvent (&xevent, None))
1750 event = _gdk_event_new ();
1752 event->any.type = GDK_NOTHING;
1753 event->any.window = NULL;
1754 event->any.send_event = xevent.xany.send_event ? TRUE : FALSE;
1756 ((GdkEventPrivate *)event)->flags |= GDK_EVENT_PENDING;
1758 node = _gdk_event_queue_append (display, event);
1760 if (gdk_event_translate (display, event, &xevent, FALSE))
1762 ((GdkEventPrivate *)event)->flags &= ~GDK_EVENT_PENDING;
1766 _gdk_event_queue_remove_link (display, node);
1767 g_list_free_1 (node);
1768 gdk_event_free (event);
1774 gdk_event_prepare (GSource *source,
1777 GdkDisplay *display = ((GdkDisplaySource*)source)->display;
1780 GDK_THREADS_ENTER ();
1783 retval = (_gdk_event_queue_find_first (display) != NULL ||
1784 gdk_check_xpending (display));
1786 GDK_THREADS_LEAVE ();
1792 gdk_event_check (GSource *source)
1794 GdkDisplaySource *display_source = (GdkDisplaySource*)source;
1797 GDK_THREADS_ENTER ();
1799 if (display_source->event_poll_fd.revents & G_IO_IN)
1800 retval = (_gdk_event_queue_find_first (display_source->display) != NULL ||
1801 gdk_check_xpending (display_source->display));
1805 GDK_THREADS_LEAVE ();
1811 gdk_event_dispatch (GSource *source,
1812 GSourceFunc callback,
1815 GdkDisplay *display = ((GdkDisplaySource*)source)->display;
1818 GDK_THREADS_ENTER ();
1820 _gdk_events_queue (display);
1821 event = _gdk_event_unqueue (display);
1825 if (_gdk_event_func)
1826 (*_gdk_event_func) (event, _gdk_event_data);
1828 gdk_event_free (event);
1831 GDK_THREADS_LEAVE ();
1837 * gdk_event_send_client_message_for_display :
1838 * @display : the #GdkDisplay for the window where the message is to be sent.
1839 * @event : the #GdkEvent to send, which should be a #GdkEventClient.
1840 * @winid : the window to send the X ClientMessage event to.
1842 * Sends an X ClientMessage event to a given window.
1844 * This could be used for communicating between different applications,
1845 * though the amount of data is limited to 20 bytes.
1847 * Returns : non-zero on success.
1850 gdk_event_send_client_message_for_display (GdkDisplay *display,
1852 GdkNativeWindow winid)
1856 g_return_val_if_fail(event != NULL, FALSE);
1858 /* Set up our event to send, with the exception of its target window */
1859 sev.xclient.type = ClientMessage;
1860 sev.xclient.display = GDK_DISPLAY_XDISPLAY (display);
1861 sev.xclient.format = event->client.data_format;
1862 sev.xclient.window = winid;
1863 memcpy(&sev.xclient.data, &event->client.data, sizeof (sev.xclient.data));
1864 sev.xclient.message_type = gdk_x11_atom_to_xatom_for_display (display, event->client.message_type);
1866 return _gdk_send_xevent (display, winid, False, NoEventMask, &sev);
1871 /* Sends a ClientMessage to all toplevel client windows */
1873 gdk_event_send_client_message_to_all_recurse (GdkDisplay *display,
1880 unsigned long nitems, after;
1881 unsigned char *data;
1882 Window *ret_children, ret_root, ret_parent;
1883 unsigned int ret_nchildren;
1884 gboolean send = FALSE;
1885 gboolean found = FALSE;
1886 gboolean result = FALSE;
1889 gdk_error_trap_push ();
1891 if (XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), xid,
1892 gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_STATE"),
1893 0, 0, False, AnyPropertyType,
1894 &type, &format, &nitems, &after, &data) != Success)
1904 /* OK, we're all set, now let's find some windows to send this to */
1905 if (!XQueryTree (GDK_DISPLAY_XDISPLAY (display), xid,
1906 &ret_root, &ret_parent,
1907 &ret_children, &ret_nchildren))
1910 for(i = 0; i < ret_nchildren; i++)
1911 if (gdk_event_send_client_message_to_all_recurse (display, xev, ret_children[i], level + 1))
1914 XFree (ret_children);
1917 if (send || (!found && (level == 1)))
1919 xev->xclient.window = xid;
1920 _gdk_send_xevent (display, xid, False, NoEventMask, xev);
1923 result = send || found;
1926 gdk_error_trap_pop ();
1932 * gdk_screen_broadcast_client_message:
1933 * @screen : the #GdkScreen where the event will be broadcasted.
1934 * @event : the #GdkEvent.
1936 * Sends an X ClientMessage event to all toplevel windows on @screen.
1938 * Toplevel windows are determined by checking for the WM_STATE property,
1939 * as described in the Inter-Client Communication Conventions Manual (ICCCM).
1940 * If no windows are found with the WM_STATE property set, the message is
1941 * sent to all children of the root window.
1945 gdk_screen_broadcast_client_message (GdkScreen *screen,
1949 GdkWindow *root_window;
1951 g_return_if_fail (event != NULL);
1953 root_window = gdk_screen_get_root_window (screen);
1955 /* Set up our event to send, with the exception of its target window */
1956 sev.xclient.type = ClientMessage;
1957 sev.xclient.display = GDK_WINDOW_XDISPLAY (root_window);
1958 sev.xclient.format = event->client.data_format;
1959 memcpy(&sev.xclient.data, &event->client.data, sizeof (sev.xclient.data));
1960 sev.xclient.message_type =
1961 gdk_x11_atom_to_xatom_for_display (GDK_WINDOW_DISPLAY (root_window),
1962 event->client.message_type);
1964 gdk_event_send_client_message_to_all_recurse (gdk_screen_get_display (screen),
1966 GDK_WINDOW_XID (root_window),
1971 *--------------------------------------------------------------
1974 * Flushes the Xlib output buffer and then waits
1975 * until all requests have been received and processed
1976 * by the X server. The only real use for this function
1977 * is in dealing with XShm.
1985 *--------------------------------------------------------------
1991 GSList *tmp_list = _gdk_displays;
1995 XSync (GDK_DISPLAY_XDISPLAY (tmp_list->data), False);
1996 tmp_list = tmp_list->next;
2001 timestamp_predicate (Display *display,
2005 Window xwindow = GPOINTER_TO_UINT (arg);
2006 GdkDisplay *gdk_display = gdk_x11_lookup_xdisplay (display);
2008 if (xevent->type == PropertyNotify &&
2009 xevent->xproperty.window == xwindow &&
2010 xevent->xproperty.atom == gdk_x11_get_xatom_by_name_for_display
2011 (gdk_display, "GDK_TIMESTAMP_PROP"))
2018 * gdk_x11_get_server_time:
2019 * @window: a #GdkWindow, used for communication with the server.
2020 * The window must have GDK_PROPERTY_CHANGE_MASK in its
2021 * events mask or a hang will result.
2023 * Routine to get the current X server time stamp.
2025 * Return value: the time stamp.
2028 gdk_x11_get_server_time (GdkWindow *window)
2034 Atom timestamp_prop_atom;
2036 g_return_val_if_fail (GDK_IS_WINDOW (window), 0);
2037 g_return_val_if_fail (!GDK_WINDOW_DESTROYED (window), 0);
2039 xdisplay = GDK_WINDOW_XDISPLAY (window);
2040 xwindow = GDK_WINDOW_XWINDOW (window);
2041 timestamp_prop_atom =
2042 gdk_x11_get_xatom_by_name_for_display (GDK_WINDOW_DISPLAY (window),
2043 "GDK_TIMESTAMP_PROP");
2045 XChangeProperty (xdisplay, xwindow, timestamp_prop_atom,
2046 timestamp_prop_atom,
2047 8, PropModeReplace, &c, 1);
2049 XIfEvent (xdisplay, &xevent,
2050 timestamp_predicate, GUINT_TO_POINTER(xwindow));
2052 return xevent.xproperty.time;
2055 typedef struct _NetWmSupportedAtoms NetWmSupportedAtoms;
2057 struct _NetWmSupportedAtoms
2064 * gdk_x11_screen_supports_net_wm_hint:
2065 * @screen : the relevant #GdkScreen.
2066 * @property: a property atom.
2068 * This function is specific to the X11 backend of GDK, and indicates
2069 * whether the window manager supports a certain hint from the
2070 * Extended Window Manager Hints Specification. You can find this
2071 * specification on http://www.freedesktop.org.
2073 * When using this function, keep in mind that the window manager
2074 * can change over time; so you shouldn't use this function in
2075 * a way that impacts persistent application state. A common bug
2076 * is that your application can start up before the window manager
2077 * does when the user logs in, and before the window manager starts
2078 * gdk_x11_screen_supports_net_wm_hint() will return %FALSE for every property.
2080 * Return value: %TRUE if the window manager supports @property
2083 gdk_x11_screen_supports_net_wm_hint (GdkScreen *screen,
2092 GdkScreenX11 *screen_x11;
2093 NetWmSupportedAtoms *supported_atoms;
2094 GdkDisplay *display;
2096 g_return_val_if_fail (GDK_IS_SCREEN (screen), FALSE);
2098 screen_x11 = GDK_SCREEN_X11 (screen);
2099 display = screen_x11->display;
2101 supported_atoms = g_object_get_data (G_OBJECT (screen), "gdk-net-wm-supported-atoms");
2102 if (!supported_atoms)
2104 supported_atoms = g_new0 (NetWmSupportedAtoms, 1);
2105 g_object_set_data (G_OBJECT (screen), "gdk-net-wm-supported-atoms", supported_atoms);
2108 if (screen_x11->wmspec_check_window != None)
2110 if (supported_atoms->atoms == NULL)
2114 while (i < supported_atoms->n_atoms)
2116 if (supported_atoms->atoms[i] == gdk_x11_atom_to_xatom_for_display (display, property))
2125 if (supported_atoms->atoms)
2126 XFree (supported_atoms->atoms);
2128 supported_atoms->atoms = NULL;
2129 supported_atoms->n_atoms = 0;
2131 /* This function is very slow on every call if you are not running a
2132 * spec-supporting WM. For now not optimized, because it isn't in
2133 * any critical code paths, but if you used it somewhere that had to
2134 * be fast you want to avoid "GTK is slow with old WMs" complaints.
2135 * Probably at that point the function should be changed to query
2136 * _NET_SUPPORTING_WM_CHECK only once every 10 seconds or something.
2139 XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), screen_x11->xroot_window,
2140 gdk_x11_get_xatom_by_name_for_display (display, "_NET_SUPPORTING_WM_CHECK"),
2141 0, G_MAXLONG, False, XA_WINDOW, &type, &format,
2142 &nitems, &bytes_after, (guchar **) & xwindow);
2144 if (type != XA_WINDOW)
2147 gdk_error_trap_push ();
2149 /* Find out if this WM goes away, so we can reset everything. */
2150 XSelectInput (screen_x11->xdisplay, *xwindow, StructureNotifyMask);
2152 gdk_display_sync (screen_x11->display);
2154 if (gdk_error_trap_pop ())
2160 XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), screen_x11->xroot_window,
2161 gdk_x11_get_xatom_by_name_for_display (display, "_NET_SUPPORTED"),
2162 0, G_MAXLONG, False, XA_ATOM, &type, &format,
2163 &supported_atoms->n_atoms, &bytes_after,
2164 (guchar **)&supported_atoms->atoms);
2166 if (type != XA_ATOM)
2169 screen_x11->wmspec_check_window = *xwindow;
2172 /* since wmspec_check_window != None this isn't infinite. ;-) */
2173 return gdk_x11_screen_supports_net_wm_hint (screen, property);
2177 * gdk_net_wm_supports:
2178 * @property: a property atom.
2180 * This function is specific to the X11 backend of GDK, and indicates
2181 * whether the window manager for the default screen supports a certain
2182 * hint from the Extended Window Manager Hints Specification. See
2183 * gdk_x11_screen_supports_net_wm_hint() for complete details.
2185 * Return value: %TRUE if the window manager supports @property
2188 gdk_net_wm_supports (GdkAtom property)
2190 return gdk_x11_screen_supports_net_wm_hint (gdk_screen_get_default (), property);
2195 const char *xsettings_name;
2196 const char *gdk_name;
2197 } settings_map[] = {
2198 { "Net/DoubleClickTime", "gtk-double-click-time" },
2199 { "Net/DndDragThreshold", "gtk-dnd-drag-threshold" },
2200 { "Gtk/CanChangeAccels", "gtk-can-change-accels" },
2201 { "Gtk/ColorPalette", "gtk-color-palette" },
2202 { "Gtk/FontName", "gtk-font-name" },
2203 { "Gtk/KeyThemeName", "gtk-key-theme-name" },
2204 { "Gtk/ToolbarStyle", "gtk-toolbar-style" },
2205 { "Gtk/ToolbarIconSize", "gtk-toolbar-icon-size" },
2206 { "Net/CursorBlink", "gtk-cursor-blink" },
2207 { "Net/CursorBlinkTime", "gtk-cursor-blink-time" },
2208 { "Net/ThemeName", "gtk-theme-name" }
2212 gdk_xsettings_notify_cb (const char *name,
2213 XSettingsAction action,
2214 XSettingsSetting *setting,
2218 GdkScreen *screen = data;
2219 GdkScreenX11 *screen_x11 = data;
2222 if (screen_x11->xsettings_in_init)
2225 new_event.type = GDK_SETTING;
2226 new_event.setting.window = gdk_screen_get_root_window (screen);
2227 new_event.setting.send_event = FALSE;
2228 new_event.setting.name = NULL;
2230 for (i = 0; i < G_N_ELEMENTS (settings_map) ; i++)
2231 if (strcmp (settings_map[i].xsettings_name, name) == 0)
2233 new_event.setting.name = (char *)settings_map[i].gdk_name;
2238 if (!new_event.setting.name)
2243 case XSETTINGS_ACTION_NEW:
2244 new_event.setting.action = GDK_SETTING_ACTION_NEW;
2246 case XSETTINGS_ACTION_CHANGED:
2247 new_event.setting.action = GDK_SETTING_ACTION_CHANGED;
2249 case XSETTINGS_ACTION_DELETED:
2250 new_event.setting.action = GDK_SETTING_ACTION_DELETED;
2254 gdk_event_put (&new_event);
2258 check_transform (const gchar *xsettings_name,
2262 if (!g_value_type_transformable (src_type, dest_type))
2264 g_warning ("Cannot tranform xsetting %s of type %s to type %s\n",
2266 g_type_name (src_type),
2267 g_type_name (dest_type));
2275 * gdk_screen_get_setting:
2276 * @screen: the #GdkScreen where the setting is located
2277 * @name: the name of the setting
2278 * @value: location to store the value of the setting
2280 * Retrieves a desktop-wide setting such as double-click time
2281 * for the #GdkScreen @screen.
2283 * FIXME needs a list of valid settings here, or a link to
2286 * Returns : %TRUE if the setting existed and a value was stored
2287 * in @value, %FALSE otherwise.
2290 gdk_screen_get_setting (GdkScreen *screen,
2295 const char *xsettings_name = NULL;
2296 XSettingsResult result;
2297 XSettingsSetting *setting;
2298 GdkScreenX11 *screen_x11;
2299 gboolean success = FALSE;
2301 GValue tmp_val = { 0, };
2303 g_return_val_if_fail (GDK_IS_SCREEN (screen), FALSE);
2305 screen_x11 = GDK_SCREEN_X11 (screen);
2307 for (i = 0; i < G_N_ELEMENTS (settings_map) ; i++)
2308 if (strcmp (settings_map[i].gdk_name, name) == 0)
2310 xsettings_name = settings_map[i].xsettings_name;
2314 if (!xsettings_name)
2317 result = xsettings_client_get_setting (screen_x11->xsettings_client,
2318 xsettings_name, &setting);
2319 if (result != XSETTINGS_SUCCESS)
2322 switch (setting->type)
2324 case XSETTINGS_TYPE_INT:
2325 if (check_transform (xsettings_name, G_TYPE_INT, G_VALUE_TYPE (value)))
2327 g_value_init (&tmp_val, G_TYPE_INT);
2328 g_value_set_int (&tmp_val, setting->data.v_int);
2329 g_value_transform (&tmp_val, value);
2334 case XSETTINGS_TYPE_STRING:
2335 if (check_transform (xsettings_name, G_TYPE_STRING, G_VALUE_TYPE (value)))
2337 g_value_init (&tmp_val, G_TYPE_STRING);
2338 g_value_set_string (&tmp_val, setting->data.v_string);
2339 g_value_transform (&tmp_val, value);
2344 case XSETTINGS_TYPE_COLOR:
2345 if (!check_transform (xsettings_name, GDK_TYPE_COLOR, G_VALUE_TYPE (value)))
2349 g_value_init (&tmp_val, GDK_TYPE_COLOR);
2352 color.red = setting->data.v_color.red;
2353 color.green = setting->data.v_color.green;
2354 color.blue = setting->data.v_color.blue;
2356 g_value_set_boxed (&tmp_val, &color);
2358 g_value_transform (&tmp_val, value);
2365 g_value_unset (&tmp_val);
2367 xsettings_setting_free (setting);
2372 static GdkFilterReturn
2373 gdk_xsettings_client_event_filter (GdkXEvent *xevent,
2377 GdkScreenX11 *screen = data;
2379 if (xsettings_client_process_event (screen->xsettings_client, (XEvent *)xevent))
2380 return GDK_FILTER_REMOVE;
2382 return GDK_FILTER_CONTINUE;
2386 gdk_xsettings_watch_cb (Window window,
2392 GdkScreen *screen = cb_data;
2393 GdkScreenX11 *screen_x11 = GDK_SCREEN_X11 (screen);
2395 gdkwin = gdk_window_lookup_for_display (gdk_screen_get_display (screen), window);
2400 gdkwin = gdk_window_foreign_new_for_display (gdk_screen_get_display (screen), window);
2402 g_object_ref (gdkwin);
2404 gdk_window_add_filter (gdkwin, gdk_xsettings_client_event_filter, screen);
2409 gdk_window_remove_filter (gdkwin, gdk_xsettings_client_event_filter, screen);
2410 g_object_unref (gdkwin);