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 (GDK_NOTHING);
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, found_fullscreen;
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;
378 found_fullscreen = FALSE;
380 XGetWindowProperty (GDK_WINDOW_XDISPLAY (window), GDK_WINDOW_XID (window),
381 gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_STATE"),
382 0, G_MAXLONG, False, XA_ATOM, &type, &format, &nitems,
383 &bytes_after, (guchar **)&atoms);
387 Atom sticky_atom = gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_STATE_STICKY");
388 Atom maxvert_atom = gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_STATE_MAXIMIZED_VERT");
389 Atom maxhorz_atom = gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_STATE_MAXIMIZED_HORZ");
390 Atom fullscreen_atom = gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_STATE_FULLSCREEN");
395 if (atoms[i] == sticky_atom)
397 else if (atoms[i] == maxvert_atom)
398 found_maxvert = TRUE;
399 else if (atoms[i] == maxhorz_atom)
400 found_maxhorz = TRUE;
401 else if (atoms[i] == fullscreen_atom)
402 found_fullscreen = TRUE;
410 /* For found_sticky to remain TRUE, we have to also be on desktop
418 XGetWindowProperty (GDK_WINDOW_XDISPLAY (window),
419 GDK_WINDOW_XID (window),
420 gdk_x11_get_xatom_by_name_for_display
421 (display, "_NET_WM_DESKTOP"),
422 0, G_MAXLONG, False, XA_CARDINAL, &type,
424 &bytes_after, (guchar **)&desktop);
428 if (*desktop != 0xFFFFFFFF)
429 found_sticky = FALSE;
434 old_state = gdk_window_get_state (window);
436 if (old_state & GDK_WINDOW_STATE_STICKY)
439 gdk_synthesize_window_state (window,
440 GDK_WINDOW_STATE_STICKY,
446 gdk_synthesize_window_state (window,
448 GDK_WINDOW_STATE_STICKY);
451 if (old_state & GDK_WINDOW_STATE_FULLSCREEN)
453 if (!found_fullscreen)
454 gdk_synthesize_window_state (window,
455 GDK_WINDOW_STATE_FULLSCREEN,
460 if (found_fullscreen)
461 gdk_synthesize_window_state (window,
463 GDK_WINDOW_STATE_FULLSCREEN);
466 /* Our "maximized" means both vertical and horizontal; if only one,
467 * we don't expose that via GDK
469 if (old_state & GDK_WINDOW_STATE_MAXIMIZED)
471 if (!(found_maxvert && found_maxhorz))
472 gdk_synthesize_window_state (window,
473 GDK_WINDOW_STATE_MAXIMIZED,
478 if (found_maxvert && found_maxhorz)
479 gdk_synthesize_window_state (window,
481 GDK_WINDOW_STATE_MAXIMIZED);
485 #define HAS_FOCUS(window_impl) \
486 ((window_impl)->has_focus || (window_impl)->has_pointer_focus)
489 generate_focus_event (GdkWindow *window,
494 event.type = GDK_FOCUS_CHANGE;
495 event.focus_change.window = window;
496 event.focus_change.send_event = FALSE;
497 event.focus_change.in = in;
499 gdk_event_put (&event);
503 set_screen_from_root (GdkDisplay *display,
509 screen = _gdk_x11_display_screen_for_xrootwin (display, xrootwin);
512 gdk_event_set_screen (event, screen);
516 translate_key_event (GdkDisplay *display,
520 GdkKeymap *keymap = gdk_keymap_get_for_display (display);
524 event->key.type = xevent->xany.type == KeyPress ? GDK_KEY_PRESS : GDK_KEY_RELEASE;
525 event->key.time = xevent->xkey.time;
527 event->key.state = (GdkModifierType) xevent->xkey.state;
528 event->key.group = _gdk_x11_get_group_for_state (display, xevent->xkey.state);
529 event->key.hardware_keycode = xevent->xkey.keycode;
531 event->key.keyval = GDK_VoidSymbol;
533 gdk_keymap_translate_keyboard_state (keymap,
534 event->key.hardware_keycode,
540 /* Fill in event->string crudely, since various programs
543 event->key.string = NULL;
545 if (event->key.keyval != GDK_VoidSymbol)
546 c = gdk_keyval_to_unicode (event->key.keyval);
553 /* Apply the control key - Taken from Xlib
555 if (event->key.state & GDK_CONTROL_MASK)
557 if ((c >= '@' && c < '\177') || c == ' ') c &= 0x1F;
560 event->key.string = g_memdup ("\0\0", 2);
561 event->key.length = 1;
565 else if (c >= '3' && c <= '7') c -= ('3' - '\033');
566 else if (c == '8') c = '\177';
567 else if (c == '/') c = '_' & 0x1F;
570 len = g_unichar_to_utf8 (c, buf);
573 event->key.string = g_locale_from_utf8 (buf, len,
574 NULL, &bytes_written,
576 if (event->key.string)
577 event->key.length = bytes_written;
580 if (!event->key.string)
582 event->key.length = 0;
583 event->key.string = g_strdup ("");
587 #ifdef G_ENABLE_DEBUG
588 if (_gdk_debug_flags & GDK_DEBUG_EVENTS)
590 g_message ("%s:\t\twindow: %ld key: %12s %d",
591 event->type == GDK_KEY_PRESS ? "key press " : "key release",
593 event->key.keyval ? gdk_keyval_name (event->key.keyval) : "(none)",
596 if (event->key.length > 0)
597 g_message ("\t\tlength: %4d string: \"%s\"",
598 event->key.length, buf);
600 #endif /* G_ENABLE_DEBUG */
603 /* Return the window this has to do with, if any, rather
604 * than the frame or root window that was selecting
608 get_real_window (XEvent *event)
613 return event->xcreatewindow.window;
616 return event->xdestroywindow.window;
619 return event->xunmap.window;
622 return event->xmap.window;
625 return event->xmaprequest.window;
628 return event->xreparent.window;
630 case ConfigureNotify:
631 return event->xconfigure.window;
633 case ConfigureRequest:
634 return event->xconfigurerequest.window;
637 return event->xgravity.window;
639 case CirculateNotify:
640 return event->xcirculate.window;
642 case CirculateRequest:
643 return event->xcirculaterequest.window;
646 return event->xany.window;
651 gdk_event_translate (GdkDisplay *display,
654 gboolean return_exposes)
658 GdkWindowObject *window_private;
659 GdkWindowImplX11 *window_impl = NULL;
661 gint xoffset, yoffset;
662 GdkScreen *screen = NULL;
663 GdkScreenX11 *screen_x11 = NULL;
664 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
669 /* init these, since the done: block uses them */
671 window_private = NULL;
672 event->any.window = NULL;
674 if (_gdk_default_filters)
676 /* Apply global filters */
677 GdkFilterReturn result;
678 result = gdk_event_apply_filters (xevent, event,
679 _gdk_default_filters);
681 if (result != GDK_FILTER_CONTINUE)
683 return_val = (result == GDK_FILTER_TRANSLATE) ? TRUE : FALSE;
688 /* We handle events with window=None
689 * specially - they are generated by XFree86's XInput under
690 * some circumstances. This handling for obvious reasons
691 * goes before we bother to lookup the event window.
694 if (xevent->xany.window == None)
696 return_val = _gdk_input_window_none_event (event, xevent);
698 if (return_val >= 0) /* was handled */
704 /* Find the GdkWindow that this event relates to.
705 * Basically this means substructure events
706 * are reported same as structure events
708 xwindow = get_real_window (xevent);
710 window = gdk_window_lookup_for_display (display, xwindow);
711 window_private = (GdkWindowObject *) window;
715 screen = GDK_WINDOW_SCREEN (window);
716 screen_x11 = GDK_SCREEN_X11 (screen);
721 /* Window may be a pixmap, so check its type.
722 * (This check is probably too expensive unless
723 * GLib short-circuits an exact type match,
724 * which has been proposed)
726 if (GDK_IS_WINDOW (window))
728 window_impl = GDK_WINDOW_IMPL_X11 (window_private->impl);
730 /* Move key events on focus window to the real toplevel, and
731 * filter out all other events on focus window
733 if (xwindow == window_impl->focus_window)
735 switch (xevent->type)
739 xwindow = GDK_WINDOW_XID (window);
740 xevent->xany.window = xwindow;
748 g_object_ref (window);
751 event->any.window = window;
752 event->any.send_event = xevent->xany.send_event ? TRUE : FALSE;
754 if (window_private && GDK_WINDOW_DESTROYED (window))
756 if (xevent->type != DestroyNotify)
762 else if (window_private)
764 /* Apply per-window filters */
765 GdkFilterReturn result;
766 result = gdk_event_apply_filters (xevent, event,
767 window_private->filters);
769 if (result != GDK_FILTER_CONTINUE)
771 return_val = (result == GDK_FILTER_TRANSLATE) ? TRUE : FALSE;
776 if (screen_x11 && screen_x11->wmspec_check_window != None &&
777 xwindow == screen_x11->wmspec_check_window)
779 if (xevent->type == DestroyNotify)
781 screen_x11->wmspec_check_window = None;
782 g_free (screen_x11->window_manager_name);
783 screen_x11->window_manager_name = g_strdup ("unknown");
785 /* careful, reentrancy */
786 _gdk_x11_screen_window_manager_changed (GDK_SCREEN (screen_x11));
789 /* Eat events on this window unless someone had wrapped
790 * it as a foreign window
800 (xevent->xany.type == MotionNotify ||
801 xevent->xany.type == ButtonRelease))
803 if (_gdk_moveresize_handle_event (xevent))
810 /* We do a "manual" conversion of the XEvent to a
811 * GdkEvent. The structures are mostly the same so
812 * the conversion is fairly straightforward. We also
813 * optionally print debugging info regarding events
821 _gdk_windowing_window_get_offsets (window, &xoffset, &yoffset);
829 switch (xevent->type)
832 if (window_private == NULL)
837 translate_key_event (display, event, xevent);
841 if (window_private == NULL)
847 /* Emulate detectable auto-repeat by checking to see
848 * if the next event is a key press with the same
849 * keycode and timestamp, and if so, ignoring the event.
852 if (!display_x11->have_xkb_autorepeat && XPending (xevent->xkey.display))
856 XPeekEvent (xevent->xkey.display, &next_event);
858 if (next_event.type == KeyPress &&
859 next_event.xkey.keycode == xevent->xkey.keycode &&
860 next_event.xkey.time == xevent->xkey.time)
867 translate_key_event (display, event, xevent);
872 g_message ("button press:\t\twindow: %ld x,y: %d %d button: %d",
873 xevent->xbutton.window,
874 xevent->xbutton.x, xevent->xbutton.y,
875 xevent->xbutton.button));
877 if (window_private == NULL ||
878 ((window_private->extension_events != 0) &&
879 display_x11->input_ignore_core))
885 /* If we get a ButtonPress event where the button is 4 or 5,
886 it's a Scroll event */
887 switch (xevent->xbutton.button)
893 event->scroll.type = GDK_SCROLL;
895 if (xevent->xbutton.button == 4)
896 event->scroll.direction = GDK_SCROLL_UP;
897 else if (xevent->xbutton.button == 5)
898 event->scroll.direction = GDK_SCROLL_DOWN;
899 else if (xevent->xbutton.button == 6)
900 event->scroll.direction = GDK_SCROLL_LEFT;
902 event->scroll.direction = GDK_SCROLL_RIGHT;
904 event->scroll.window = window;
905 event->scroll.time = xevent->xbutton.time;
906 event->scroll.x = xevent->xbutton.x + xoffset;
907 event->scroll.y = xevent->xbutton.y + yoffset;
908 event->scroll.x_root = (gfloat)xevent->xbutton.x_root;
909 event->scroll.y_root = (gfloat)xevent->xbutton.y_root;
910 event->scroll.state = (GdkModifierType) xevent->xbutton.state;
911 event->scroll.device = display->core_pointer;
913 set_screen_from_root (display, event, xevent->xbutton.root);
918 event->button.type = GDK_BUTTON_PRESS;
919 event->button.window = window;
920 event->button.time = xevent->xbutton.time;
921 event->button.x = xevent->xbutton.x + xoffset;
922 event->button.y = xevent->xbutton.y + yoffset;
923 event->button.x_root = (gfloat)xevent->xbutton.x_root;
924 event->button.y_root = (gfloat)xevent->xbutton.y_root;
925 event->button.axes = NULL;
926 event->button.state = (GdkModifierType) xevent->xbutton.state;
927 event->button.button = xevent->xbutton.button;
928 event->button.device = display->core_pointer;
930 set_screen_from_root (display, event, xevent->xbutton.root);
932 _gdk_event_button_generate (display, event);
940 g_message ("button release:\twindow: %ld x,y: %d %d button: %d",
941 xevent->xbutton.window,
942 xevent->xbutton.x, xevent->xbutton.y,
943 xevent->xbutton.button));
945 if (window_private == NULL ||
946 ((window_private->extension_events != 0) &&
947 display_x11->input_ignore_core))
953 /* We treat button presses as scroll wheel events, so ignore the release */
954 if (xevent->xbutton.button == 4 || xevent->xbutton.button == 5 ||
955 xevent->xbutton.button == 6 || xevent->xbutton.button ==7)
961 event->button.type = GDK_BUTTON_RELEASE;
962 event->button.window = window;
963 event->button.time = xevent->xbutton.time;
964 event->button.x = xevent->xbutton.x + xoffset;
965 event->button.y = xevent->xbutton.y + yoffset;
966 event->button.x_root = (gfloat)xevent->xbutton.x_root;
967 event->button.y_root = (gfloat)xevent->xbutton.y_root;
968 event->button.axes = NULL;
969 event->button.state = (GdkModifierType) xevent->xbutton.state;
970 event->button.button = xevent->xbutton.button;
971 event->button.device = display->core_pointer;
973 set_screen_from_root (display, event, xevent->xbutton.root);
979 g_message ("motion notify:\t\twindow: %ld x,y: %d %d hint: %s",
980 xevent->xmotion.window,
981 xevent->xmotion.x, xevent->xmotion.y,
982 (xevent->xmotion.is_hint) ? "true" : "false"));
984 if (window_private == NULL ||
985 ((window_private->extension_events != 0) &&
986 display_x11->input_ignore_core))
992 event->motion.type = GDK_MOTION_NOTIFY;
993 event->motion.window = window;
994 event->motion.time = xevent->xmotion.time;
995 event->motion.x = xevent->xmotion.x + xoffset;
996 event->motion.y = xevent->xmotion.y + yoffset;
997 event->motion.x_root = (gfloat)xevent->xmotion.x_root;
998 event->motion.y_root = (gfloat)xevent->xmotion.y_root;
999 event->motion.axes = NULL;
1000 event->motion.state = (GdkModifierType) xevent->xmotion.state;
1001 event->motion.is_hint = xevent->xmotion.is_hint;
1002 event->motion.device = display->core_pointer;
1004 set_screen_from_root (display, event, xevent->xmotion.root);
1010 g_message ("enter notify:\t\twindow: %ld detail: %d subwin: %ld",
1011 xevent->xcrossing.window,
1012 xevent->xcrossing.detail,
1013 xevent->xcrossing.subwindow));
1015 if (window_private == NULL)
1021 /* Handle focusing (in the case where no window manager is running */
1023 GDK_WINDOW_TYPE (window) != GDK_WINDOW_CHILD &&
1024 xevent->xcrossing.detail != NotifyInferior &&
1025 xevent->xcrossing.focus && !window_impl->has_focus)
1027 gboolean had_focus = HAS_FOCUS (window_impl);
1029 window_impl->has_pointer_focus = TRUE;
1031 if (HAS_FOCUS (window_impl) != had_focus)
1032 generate_focus_event (window, TRUE);
1035 /* Tell XInput stuff about it if appropriate */
1036 if (window_private &&
1037 !GDK_WINDOW_DESTROYED (window) &&
1038 window_private->extension_events != 0)
1039 _gdk_input_enter_event (&xevent->xcrossing, window);
1041 event->crossing.type = GDK_ENTER_NOTIFY;
1042 event->crossing.window = window;
1044 /* If the subwindow field of the XEvent is non-NULL, then
1045 * lookup the corresponding GdkWindow.
1047 if (xevent->xcrossing.subwindow != None)
1048 event->crossing.subwindow = gdk_window_lookup_for_display (display, xevent->xcrossing.subwindow);
1050 event->crossing.subwindow = NULL;
1052 event->crossing.time = xevent->xcrossing.time;
1053 event->crossing.x = xevent->xcrossing.x + xoffset;
1054 event->crossing.y = xevent->xcrossing.y + yoffset;
1055 event->crossing.x_root = xevent->xcrossing.x_root;
1056 event->crossing.y_root = xevent->xcrossing.y_root;
1058 set_screen_from_root (display, event, xevent->xcrossing.root);
1060 /* Translate the crossing mode into Gdk terms.
1062 switch (xevent->xcrossing.mode)
1065 event->crossing.mode = GDK_CROSSING_NORMAL;
1068 event->crossing.mode = GDK_CROSSING_GRAB;
1071 event->crossing.mode = GDK_CROSSING_UNGRAB;
1075 /* Translate the crossing detail into Gdk terms.
1077 switch (xevent->xcrossing.detail)
1079 case NotifyInferior:
1080 event->crossing.detail = GDK_NOTIFY_INFERIOR;
1082 case NotifyAncestor:
1083 event->crossing.detail = GDK_NOTIFY_ANCESTOR;
1086 event->crossing.detail = GDK_NOTIFY_VIRTUAL;
1088 case NotifyNonlinear:
1089 event->crossing.detail = GDK_NOTIFY_NONLINEAR;
1091 case NotifyNonlinearVirtual:
1092 event->crossing.detail = GDK_NOTIFY_NONLINEAR_VIRTUAL;
1095 event->crossing.detail = GDK_NOTIFY_UNKNOWN;
1099 event->crossing.focus = xevent->xcrossing.focus;
1100 event->crossing.state = xevent->xcrossing.state;
1106 g_message ("leave notify:\t\twindow: %ld detail: %d subwin: %ld",
1107 xevent->xcrossing.window,
1108 xevent->xcrossing.detail, xevent->xcrossing.subwindow));
1110 if (window_private == NULL)
1116 /* Handle focusing (in the case where no window manager is running */
1118 GDK_WINDOW_TYPE (window) != GDK_WINDOW_CHILD &&
1119 xevent->xcrossing.detail != NotifyInferior &&
1120 xevent->xcrossing.focus && !window_impl->has_focus)
1122 gboolean had_focus = HAS_FOCUS (window_impl);
1124 window_impl->has_pointer_focus = FALSE;
1126 if (HAS_FOCUS (window_impl) != had_focus)
1127 generate_focus_event (window, FALSE);
1130 event->crossing.type = GDK_LEAVE_NOTIFY;
1131 event->crossing.window = window;
1133 /* If the subwindow field of the XEvent is non-NULL, then
1134 * lookup the corresponding GdkWindow.
1136 if (xevent->xcrossing.subwindow != None)
1137 event->crossing.subwindow = gdk_window_lookup_for_display (display, xevent->xcrossing.subwindow);
1139 event->crossing.subwindow = NULL;
1141 event->crossing.time = xevent->xcrossing.time;
1142 event->crossing.x = xevent->xcrossing.x + xoffset;
1143 event->crossing.y = xevent->xcrossing.y + yoffset;
1144 event->crossing.x_root = xevent->xcrossing.x_root;
1145 event->crossing.y_root = xevent->xcrossing.y_root;
1147 set_screen_from_root (display, event, xevent->xcrossing.root);
1149 /* Translate the crossing mode into Gdk terms.
1151 switch (xevent->xcrossing.mode)
1154 event->crossing.mode = GDK_CROSSING_NORMAL;
1157 event->crossing.mode = GDK_CROSSING_GRAB;
1160 event->crossing.mode = GDK_CROSSING_UNGRAB;
1164 /* Translate the crossing detail into Gdk terms.
1166 switch (xevent->xcrossing.detail)
1168 case NotifyInferior:
1169 event->crossing.detail = GDK_NOTIFY_INFERIOR;
1171 case NotifyAncestor:
1172 event->crossing.detail = GDK_NOTIFY_ANCESTOR;
1175 event->crossing.detail = GDK_NOTIFY_VIRTUAL;
1177 case NotifyNonlinear:
1178 event->crossing.detail = GDK_NOTIFY_NONLINEAR;
1180 case NotifyNonlinearVirtual:
1181 event->crossing.detail = GDK_NOTIFY_NONLINEAR_VIRTUAL;
1184 event->crossing.detail = GDK_NOTIFY_UNKNOWN;
1188 event->crossing.focus = xevent->xcrossing.focus;
1189 event->crossing.state = xevent->xcrossing.state;
1193 /* We only care about focus events that indicate that _this_
1194 * window (not a ancestor or child) got or lost the focus
1198 g_message ("focus in:\t\twindow: %ld", xevent->xfocus.window));
1200 if (window && GDK_WINDOW_TYPE (window) != GDK_WINDOW_CHILD)
1202 gboolean had_focus = HAS_FOCUS (window_impl);
1204 switch (xevent->xfocus.detail)
1206 case NotifyAncestor:
1207 case NotifyNonlinear:
1209 case NotifyNonlinearVirtual:
1210 /* We pretend that the focus moves to the grab
1211 * window, so we pay attention to NotifyGrab
1212 * NotifyUngrab, and ignore NotifyWhileGrabbed
1214 if (xevent->xfocus.mode != NotifyWhileGrabbed)
1215 window_impl->has_focus = TRUE;
1218 /* The X server sends NotifyPointer/NotifyGrab,
1219 * but the pointer focus is ignored while a
1222 if (xevent->xfocus.mode != NotifyGrab)
1223 window_impl->has_pointer_focus = TRUE;
1225 case NotifyInferior:
1226 case NotifyPointerRoot:
1227 case NotifyDetailNone:
1231 if (HAS_FOCUS (window_impl) != had_focus)
1232 generate_focus_event (window, TRUE);
1237 g_message ("focus out:\t\twindow: %ld", xevent->xfocus.window));
1239 if (window && GDK_WINDOW_TYPE (window) != GDK_WINDOW_CHILD)
1241 gboolean had_focus = HAS_FOCUS (window_impl);
1243 switch (xevent->xfocus.detail)
1245 case NotifyAncestor:
1246 case NotifyNonlinear:
1248 case NotifyNonlinearVirtual:
1249 if (xevent->xfocus.mode != NotifyWhileGrabbed)
1250 window_impl->has_focus = FALSE;
1253 if (xevent->xfocus.mode != NotifyUngrab)
1254 window_impl->has_pointer_focus = FALSE;
1256 case NotifyInferior:
1257 case NotifyPointerRoot:
1258 case NotifyDetailNone:
1262 if (HAS_FOCUS (window_impl) != had_focus)
1263 generate_focus_event (window, FALSE);
1268 /* gdk_keyboard_grab() causes following events. These events confuse
1269 * the XIM focus, so ignore them.
1271 if (xevent->xfocus.mode == NotifyGrab ||
1272 xevent->xfocus.mode == NotifyUngrab)
1278 g_message ("keymap notify"));
1280 /* Not currently handled */
1286 g_message ("expose:\t\twindow: %ld %d x,y: %d %d w,h: %d %d%s",
1287 xevent->xexpose.window, xevent->xexpose.count,
1288 xevent->xexpose.x, xevent->xexpose.y,
1289 xevent->xexpose.width, xevent->xexpose.height,
1290 event->any.send_event ? " (send)" : ""));
1292 if (window_private == NULL)
1299 GdkRectangle expose_rect;
1301 expose_rect.x = xevent->xexpose.x + xoffset;
1302 expose_rect.y = xevent->xexpose.y + yoffset;
1303 expose_rect.width = xevent->xexpose.width;
1304 expose_rect.height = xevent->xexpose.height;
1308 event->expose.type = GDK_EXPOSE;
1309 event->expose.area = expose_rect;
1310 event->expose.region = gdk_region_rectangle (&expose_rect);
1311 event->expose.window = window;
1312 event->expose.count = xevent->xexpose.count;
1318 _gdk_window_process_expose (window, xevent->xexpose.serial, &expose_rect);
1327 case GraphicsExpose:
1329 GdkRectangle expose_rect;
1332 g_message ("graphics expose:\tdrawable: %ld",
1333 xevent->xgraphicsexpose.drawable));
1335 if (window_private == NULL)
1341 expose_rect.x = xevent->xgraphicsexpose.x + xoffset;
1342 expose_rect.y = xevent->xgraphicsexpose.y + yoffset;
1343 expose_rect.width = xevent->xgraphicsexpose.width;
1344 expose_rect.height = xevent->xgraphicsexpose.height;
1348 event->expose.type = GDK_EXPOSE;
1349 event->expose.area = expose_rect;
1350 event->expose.region = gdk_region_rectangle (&expose_rect);
1351 event->expose.window = window;
1352 event->expose.count = xevent->xgraphicsexpose.count;
1358 _gdk_window_process_expose (window, xevent->xgraphicsexpose.serial, &expose_rect);
1368 g_message ("no expose:\t\tdrawable: %ld",
1369 xevent->xnoexpose.drawable));
1371 event->no_expose.type = GDK_NO_EXPOSE;
1372 event->no_expose.window = window;
1376 case VisibilityNotify:
1377 #ifdef G_ENABLE_DEBUG
1378 if (_gdk_debug_flags & GDK_DEBUG_EVENTS)
1379 switch (xevent->xvisibility.state)
1381 case VisibilityFullyObscured:
1382 g_message ("visibility notify:\twindow: %ld none",
1383 xevent->xvisibility.window);
1385 case VisibilityPartiallyObscured:
1386 g_message ("visibility notify:\twindow: %ld partial",
1387 xevent->xvisibility.window);
1389 case VisibilityUnobscured:
1390 g_message ("visibility notify:\twindow: %ld full",
1391 xevent->xvisibility.window);
1394 #endif /* G_ENABLE_DEBUG */
1396 if (window_private == NULL)
1402 event->visibility.type = GDK_VISIBILITY_NOTIFY;
1403 event->visibility.window = window;
1405 switch (xevent->xvisibility.state)
1407 case VisibilityFullyObscured:
1408 event->visibility.state = GDK_VISIBILITY_FULLY_OBSCURED;
1411 case VisibilityPartiallyObscured:
1412 event->visibility.state = GDK_VISIBILITY_PARTIAL;
1415 case VisibilityUnobscured:
1416 event->visibility.state = GDK_VISIBILITY_UNOBSCURED;
1424 g_message ("create notify:\twindow: %ld x,y: %d %d w,h: %d %d b-w: %d parent: %ld ovr: %d",
1425 xevent->xcreatewindow.window,
1426 xevent->xcreatewindow.x,
1427 xevent->xcreatewindow.y,
1428 xevent->xcreatewindow.width,
1429 xevent->xcreatewindow.height,
1430 xevent->xcreatewindow.border_width,
1431 xevent->xcreatewindow.parent,
1432 xevent->xcreatewindow.override_redirect));
1433 /* not really handled */
1438 g_message ("destroy notify:\twindow: %ld",
1439 xevent->xdestroywindow.window));
1441 /* Ignore DestroyNotify from SubstructureNotifyMask */
1442 if (xevent->xdestroywindow.window == xevent->xdestroywindow.event)
1444 event->any.type = GDK_DESTROY;
1445 event->any.window = window;
1447 return_val = window_private && !GDK_WINDOW_DESTROYED (window);
1449 if (window && GDK_WINDOW_XID (window) != screen_x11->xroot_window)
1450 gdk_window_destroy_notify (window);
1459 g_message ("unmap notify:\t\twindow: %ld",
1460 xevent->xmap.window));
1462 event->any.type = GDK_UNMAP;
1463 event->any.window = window;
1465 /* If we are shown (not withdrawn) and get an unmap, it means we
1466 * were iconified in the X sense. If we are withdrawn, and get
1467 * an unmap, it means we hid the window ourselves, so we
1468 * will have already flipped the iconified bit off.
1472 if (GDK_WINDOW_IS_MAPPED (window))
1473 gdk_synthesize_window_state (window,
1475 GDK_WINDOW_STATE_ICONIFIED);
1477 _gdk_xgrab_check_unmap (window, xevent->xany.serial);
1484 g_message ("map notify:\t\twindow: %ld",
1485 xevent->xmap.window));
1487 event->any.type = GDK_MAP;
1488 event->any.window = window;
1490 /* Unset iconified if it was set */
1491 if (window && (((GdkWindowObject*)window)->state & GDK_WINDOW_STATE_ICONIFIED))
1492 gdk_synthesize_window_state (window,
1493 GDK_WINDOW_STATE_ICONIFIED,
1498 case ReparentNotify:
1500 g_message ("reparent notify:\twindow: %ld x,y: %d %d parent: %ld ovr: %d",
1501 xevent->xreparent.window,
1502 xevent->xreparent.x,
1503 xevent->xreparent.y,
1504 xevent->xreparent.parent,
1505 xevent->xreparent.override_redirect));
1507 /* Not currently handled */
1511 case ConfigureNotify:
1513 g_message ("configure notify:\twindow: %ld x,y: %d %d w,h: %d %d b-w: %d above: %ld ovr: %d%s",
1514 xevent->xconfigure.window,
1515 xevent->xconfigure.x,
1516 xevent->xconfigure.y,
1517 xevent->xconfigure.width,
1518 xevent->xconfigure.height,
1519 xevent->xconfigure.border_width,
1520 xevent->xconfigure.above,
1521 xevent->xconfigure.override_redirect,
1524 : GDK_WINDOW_TYPE (window) == GDK_WINDOW_CHILD
1525 ? " (discarding child)"
1526 : xevent->xconfigure.event != xevent->xconfigure.window
1527 ? " (discarding substructure)"
1529 if (window && GDK_WINDOW_TYPE (window) == GDK_WINDOW_ROOT)
1530 _gdk_x11_screen_size_changed (screen, xevent);
1533 xevent->xconfigure.event == xevent->xconfigure.window &&
1534 !GDK_WINDOW_DESTROYED (window) &&
1535 (window_private->extension_events != 0))
1536 _gdk_input_configure_event (&xevent->xconfigure, window);
1539 xevent->xconfigure.event != xevent->xconfigure.window ||
1540 GDK_WINDOW_TYPE (window) == GDK_WINDOW_CHILD ||
1541 GDK_WINDOW_TYPE (window) == GDK_WINDOW_ROOT)
1545 event->configure.type = GDK_CONFIGURE;
1546 event->configure.window = window;
1547 event->configure.width = xevent->xconfigure.width;
1548 event->configure.height = xevent->xconfigure.height;
1550 if (!xevent->xconfigure.send_event &&
1551 !GDK_WINDOW_DESTROYED (window))
1555 Window child_window = 0;
1557 gdk_error_trap_push ();
1558 if (XTranslateCoordinates (GDK_DRAWABLE_XDISPLAY (window),
1559 GDK_DRAWABLE_XID (window),
1560 screen_x11->xroot_window,
1565 if (!gdk_error_trap_pop ())
1567 event->configure.x = tx;
1568 event->configure.y = ty;
1572 gdk_error_trap_pop ();
1576 event->configure.x = xevent->xconfigure.x;
1577 event->configure.y = xevent->xconfigure.y;
1579 window_private->x = event->configure.x;
1580 window_private->y = event->configure.y;
1581 GDK_WINDOW_IMPL_X11 (window_private->impl)->width = xevent->xconfigure.width;
1582 GDK_WINDOW_IMPL_X11 (window_private->impl)->height = xevent->xconfigure.height;
1583 if (window_private->resize_count >= 1)
1585 window_private->resize_count -= 1;
1587 if (window_private->resize_count == 0)
1588 _gdk_moveresize_configure_done (display, window);
1593 case PropertyNotify:
1595 g_message ("property notify:\twindow: %ld, atom(%ld): %s%s%s",
1596 xevent->xproperty.window,
1597 xevent->xproperty.atom,
1599 gdk_x11_get_xatom_name_for_display (display, xevent->xproperty.atom),
1602 if (window_private == NULL)
1608 if (xevent->xproperty.atom == gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_STATE") ||
1609 xevent->xproperty.atom == gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_DESKTOP"))
1611 /* If window state changed, then synthesize those events. */
1612 gdk_check_wm_state_changed (window);
1615 if (window_private->event_mask & GDK_PROPERTY_CHANGE_MASK)
1617 event->property.type = GDK_PROPERTY_NOTIFY;
1618 event->property.window = window;
1619 event->property.atom = gdk_x11_xatom_to_atom_for_display (display, xevent->xproperty.atom);
1620 event->property.time = xevent->xproperty.time;
1621 event->property.state = xevent->xproperty.state;
1628 case SelectionClear:
1630 g_message ("selection clear:\twindow: %ld",
1631 xevent->xproperty.window));
1633 if (_gdk_selection_filter_clear_event (&xevent->xselectionclear))
1635 event->selection.type = GDK_SELECTION_CLEAR;
1636 event->selection.window = window;
1637 event->selection.selection = gdk_x11_xatom_to_atom_for_display (display, xevent->xselectionclear.selection);
1638 event->selection.time = xevent->xselectionclear.time;
1645 case SelectionRequest:
1647 g_message ("selection request:\twindow: %ld",
1648 xevent->xproperty.window));
1650 event->selection.type = GDK_SELECTION_REQUEST;
1651 event->selection.window = window;
1652 event->selection.selection = gdk_x11_xatom_to_atom_for_display (display, xevent->xselectionrequest.selection);
1653 event->selection.target = gdk_x11_xatom_to_atom_for_display (display, xevent->xselectionrequest.target);
1654 event->selection.property = gdk_x11_xatom_to_atom_for_display (display, xevent->xselectionrequest.property);
1655 event->selection.requestor = xevent->xselectionrequest.requestor;
1656 event->selection.time = xevent->xselectionrequest.time;
1660 case SelectionNotify:
1662 g_message ("selection notify:\twindow: %ld",
1663 xevent->xproperty.window));
1666 event->selection.type = GDK_SELECTION_NOTIFY;
1667 event->selection.window = window;
1668 event->selection.selection = gdk_x11_xatom_to_atom_for_display (display, xevent->xselection.selection);
1669 event->selection.target = gdk_x11_xatom_to_atom_for_display (display, xevent->xselection.target);
1670 event->selection.property = gdk_x11_xatom_to_atom_for_display (display, xevent->xselection.property);
1671 event->selection.time = xevent->xselection.time;
1675 case ColormapNotify:
1677 g_message ("colormap notify:\twindow: %ld",
1678 xevent->xcolormap.window));
1680 /* Not currently handled */
1687 GdkFilterReturn result = GDK_FILTER_CONTINUE;
1688 GdkAtom message_type = gdk_x11_xatom_to_atom_for_display (display, xevent->xclient.message_type);
1691 g_message ("client message:\twindow: %ld",
1692 xevent->xclient.window));
1694 tmp_list = display_x11->client_filters;
1697 GdkClientFilter *filter = tmp_list->data;
1698 if (filter->type == message_type)
1700 result = (*filter->function) (xevent, event, filter->data);
1704 tmp_list = tmp_list->next;
1709 case GDK_FILTER_REMOVE:
1712 case GDK_FILTER_TRANSLATE:
1715 case GDK_FILTER_CONTINUE:
1716 /* Send unknown ClientMessage's on to Gtk for it to use */
1717 if (window_private == NULL)
1723 event->client.type = GDK_CLIENT_EVENT;
1724 event->client.window = window;
1725 event->client.message_type = message_type;
1726 event->client.data_format = xevent->xclient.format;
1727 memcpy(&event->client.data, &xevent->xclient.data,
1728 sizeof(event->client.data));
1738 g_message ("mapping notify"));
1740 /* Let XLib know that there is a new keyboard mapping.
1742 XRefreshKeyboardMapping (&xevent->xmapping);
1743 _gdk_keymap_keys_changed (display);
1749 if (xevent->type == display_x11->xkb_event_type)
1751 XkbEvent *xkb_event = (XkbEvent *)xevent;
1753 switch (xkb_event->any.xkb_type)
1756 _gdk_keymap_keys_changed (display);
1761 case XkbStateNotify:
1762 _gdk_keymap_state_changed (display);
1769 /* something else - (e.g., a Xinput event) */
1771 if (window_private &&
1772 !GDK_WINDOW_DESTROYED (window_private) &&
1773 (window_private->extension_events != 0))
1774 return_val = _gdk_input_other_event(event, xevent, window);
1785 if (event->any.window)
1786 g_object_ref (event->any.window);
1787 if (((event->any.type == GDK_ENTER_NOTIFY) ||
1788 (event->any.type == GDK_LEAVE_NOTIFY)) &&
1789 (event->crossing.subwindow != NULL))
1790 g_object_ref (event->crossing.subwindow);
1794 /* Mark this event as having no resources to be freed */
1795 event->any.window = NULL;
1796 event->any.type = GDK_NOTHING;
1800 g_object_unref (window);
1805 static GdkFilterReturn
1806 gdk_wm_protocols_filter (GdkXEvent *xev,
1810 XEvent *xevent = (XEvent *)xev;
1811 GdkWindow *win = event->any.window;
1812 GdkDisplay *display = GDK_WINDOW_DISPLAY (win);
1814 if ((Atom) xevent->xclient.data.l[0] == gdk_x11_get_xatom_by_name_for_display (display, "WM_DELETE_WINDOW"))
1816 /* The delete window request specifies a window
1817 * to delete. We don't actually destroy the
1818 * window because "it is only a request". (The
1819 * window might contain vital data that the
1820 * program does not want destroyed). Instead
1821 * the event is passed along to the program,
1822 * which should then destroy the window.
1825 g_message ("delete window:\t\twindow: %ld",
1826 xevent->xclient.window));
1828 event->any.type = GDK_DELETE;
1830 return GDK_FILTER_TRANSLATE;
1832 else if ((Atom) xevent->xclient.data.l[0] == gdk_x11_get_xatom_by_name_for_display (display, "WM_TAKE_FOCUS"))
1834 GdkWindow *win = event->any.window;
1835 Window focus_win = GDK_WINDOW_IMPL_X11(((GdkWindowObject *)win)->impl)->focus_window;
1837 /* There is no way of knowing reliably whether we are viewable so we need
1838 * to trap errors so we don't cause a BadMatch.
1840 gdk_error_trap_push ();
1841 XSetInputFocus (GDK_WINDOW_XDISPLAY (win),
1844 xevent->xclient.data.l[1]);
1845 XSync (GDK_WINDOW_XDISPLAY (win), False);
1846 gdk_error_trap_pop ();
1848 else if ((Atom) xevent->xclient.data.l[0] == gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_PING"))
1850 XEvent xev = *xevent;
1852 xev.xclient.window = GDK_WINDOW_XROOTWIN (win);
1853 XSendEvent (GDK_WINDOW_XDISPLAY (win),
1856 SubstructureRedirectMask | SubstructureNotifyMask, &xev);
1859 return GDK_FILTER_REMOVE;
1863 _gdk_events_queue (GdkDisplay *display)
1868 Display *xdisplay = GDK_DISPLAY_XDISPLAY (display);
1870 while (!_gdk_event_queue_find_first(display) && XPending (xdisplay))
1872 XNextEvent (xdisplay, &xevent);
1874 switch (xevent.type)
1880 if (XFilterEvent (&xevent, None))
1884 event = gdk_event_new (GDK_NOTHING);
1886 event->any.window = NULL;
1887 event->any.send_event = xevent.xany.send_event ? TRUE : FALSE;
1889 ((GdkEventPrivate *)event)->flags |= GDK_EVENT_PENDING;
1891 node = _gdk_event_queue_append (display, event);
1893 if (gdk_event_translate (display, event, &xevent, FALSE))
1895 ((GdkEventPrivate *)event)->flags &= ~GDK_EVENT_PENDING;
1899 _gdk_event_queue_remove_link (display, node);
1900 g_list_free_1 (node);
1901 gdk_event_free (event);
1907 gdk_event_prepare (GSource *source,
1910 GdkDisplay *display = ((GdkDisplaySource*)source)->display;
1913 GDK_THREADS_ENTER ();
1916 retval = (_gdk_event_queue_find_first (display) != NULL ||
1917 gdk_check_xpending (display));
1919 GDK_THREADS_LEAVE ();
1925 gdk_event_check (GSource *source)
1927 GdkDisplaySource *display_source = (GdkDisplaySource*)source;
1930 GDK_THREADS_ENTER ();
1932 if (display_source->event_poll_fd.revents & G_IO_IN)
1933 retval = (_gdk_event_queue_find_first (display_source->display) != NULL ||
1934 gdk_check_xpending (display_source->display));
1938 GDK_THREADS_LEAVE ();
1944 gdk_event_dispatch (GSource *source,
1945 GSourceFunc callback,
1948 GdkDisplay *display = ((GdkDisplaySource*)source)->display;
1951 GDK_THREADS_ENTER ();
1953 _gdk_events_queue (display);
1954 event = _gdk_event_unqueue (display);
1958 if (_gdk_event_func)
1959 (*_gdk_event_func) (event, _gdk_event_data);
1961 gdk_event_free (event);
1964 GDK_THREADS_LEAVE ();
1970 * gdk_event_send_client_message_for_display :
1971 * @display : the #GdkDisplay for the window where the message is to be sent.
1972 * @event : the #GdkEvent to send, which should be a #GdkEventClient.
1973 * @winid : the window to send the X ClientMessage event to.
1975 * Sends an X ClientMessage event to a given window.
1977 * This could be used for communicating between different applications,
1978 * though the amount of data is limited to 20 bytes.
1980 * Returns : non-zero on success.
1983 gdk_event_send_client_message_for_display (GdkDisplay *display,
1985 GdkNativeWindow winid)
1989 g_return_val_if_fail(event != NULL, FALSE);
1991 /* Set up our event to send, with the exception of its target window */
1992 sev.xclient.type = ClientMessage;
1993 sev.xclient.display = GDK_DISPLAY_XDISPLAY (display);
1994 sev.xclient.format = event->client.data_format;
1995 sev.xclient.window = winid;
1996 memcpy(&sev.xclient.data, &event->client.data, sizeof (sev.xclient.data));
1997 sev.xclient.message_type = gdk_x11_atom_to_xatom_for_display (display, event->client.message_type);
1999 return _gdk_send_xevent (display, winid, False, NoEventMask, &sev);
2004 /* Sends a ClientMessage to all toplevel client windows */
2006 gdk_event_send_client_message_to_all_recurse (GdkDisplay *display,
2013 unsigned long nitems, after;
2014 unsigned char *data;
2015 Window *ret_children, ret_root, ret_parent;
2016 unsigned int ret_nchildren;
2017 gboolean send = FALSE;
2018 gboolean found = FALSE;
2019 gboolean result = FALSE;
2022 gdk_error_trap_push ();
2024 if (XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), xid,
2025 gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_STATE"),
2026 0, 0, False, AnyPropertyType,
2027 &type, &format, &nitems, &after, &data) != Success)
2037 /* OK, we're all set, now let's find some windows to send this to */
2038 if (!XQueryTree (GDK_DISPLAY_XDISPLAY (display), xid,
2039 &ret_root, &ret_parent,
2040 &ret_children, &ret_nchildren))
2043 for(i = 0; i < ret_nchildren; i++)
2044 if (gdk_event_send_client_message_to_all_recurse (display, xev, ret_children[i], level + 1))
2047 XFree (ret_children);
2050 if (send || (!found && (level == 1)))
2052 xev->xclient.window = xid;
2053 _gdk_send_xevent (display, xid, False, NoEventMask, xev);
2056 result = send || found;
2059 gdk_error_trap_pop ();
2065 * gdk_screen_broadcast_client_message:
2066 * @screen : the #GdkScreen where the event will be broadcasted.
2067 * @event : the #GdkEvent.
2069 * Sends an X ClientMessage event to all toplevel windows on @screen.
2071 * Toplevel windows are determined by checking for the WM_STATE property,
2072 * as described in the Inter-Client Communication Conventions Manual (ICCCM).
2073 * If no windows are found with the WM_STATE property set, the message is
2074 * sent to all children of the root window.
2078 gdk_screen_broadcast_client_message (GdkScreen *screen,
2082 GdkWindow *root_window;
2084 g_return_if_fail (event != NULL);
2086 root_window = gdk_screen_get_root_window (screen);
2088 /* Set up our event to send, with the exception of its target window */
2089 sev.xclient.type = ClientMessage;
2090 sev.xclient.display = GDK_WINDOW_XDISPLAY (root_window);
2091 sev.xclient.format = event->client.data_format;
2092 memcpy(&sev.xclient.data, &event->client.data, sizeof (sev.xclient.data));
2093 sev.xclient.message_type =
2094 gdk_x11_atom_to_xatom_for_display (GDK_WINDOW_DISPLAY (root_window),
2095 event->client.message_type);
2097 gdk_event_send_client_message_to_all_recurse (gdk_screen_get_display (screen),
2099 GDK_WINDOW_XID (root_window),
2104 *--------------------------------------------------------------
2107 * Flushes the Xlib output buffer and then waits
2108 * until all requests have been received and processed
2109 * by the X server. The only real use for this function
2110 * is in dealing with XShm.
2118 *--------------------------------------------------------------
2124 GSList *tmp_list = _gdk_displays;
2128 XSync (GDK_DISPLAY_XDISPLAY (tmp_list->data), False);
2129 tmp_list = tmp_list->next;
2134 timestamp_predicate (Display *display,
2138 Window xwindow = GPOINTER_TO_UINT (arg);
2139 GdkDisplay *gdk_display = gdk_x11_lookup_xdisplay (display);
2141 if (xevent->type == PropertyNotify &&
2142 xevent->xproperty.window == xwindow &&
2143 xevent->xproperty.atom == gdk_x11_get_xatom_by_name_for_display
2144 (gdk_display, "GDK_TIMESTAMP_PROP"))
2151 * gdk_x11_get_server_time:
2152 * @window: a #GdkWindow, used for communication with the server.
2153 * The window must have GDK_PROPERTY_CHANGE_MASK in its
2154 * events mask or a hang will result.
2156 * Routine to get the current X server time stamp.
2158 * Return value: the time stamp.
2161 gdk_x11_get_server_time (GdkWindow *window)
2167 Atom timestamp_prop_atom;
2169 g_return_val_if_fail (GDK_IS_WINDOW (window), 0);
2170 g_return_val_if_fail (!GDK_WINDOW_DESTROYED (window), 0);
2172 xdisplay = GDK_WINDOW_XDISPLAY (window);
2173 xwindow = GDK_WINDOW_XWINDOW (window);
2174 timestamp_prop_atom =
2175 gdk_x11_get_xatom_by_name_for_display (GDK_WINDOW_DISPLAY (window),
2176 "GDK_TIMESTAMP_PROP");
2178 XChangeProperty (xdisplay, xwindow, timestamp_prop_atom,
2179 timestamp_prop_atom,
2180 8, PropModeReplace, &c, 1);
2182 XIfEvent (xdisplay, &xevent,
2183 timestamp_predicate, GUINT_TO_POINTER(xwindow));
2185 return xevent.xproperty.time;
2189 fetch_net_wm_check_window (GdkScreen *screen)
2191 GdkScreenX11 *screen_x11;
2192 GdkDisplay *display;
2199 /* This function is very slow on every call if you are not running a
2200 * spec-supporting WM. For now not optimized, because it isn't in
2201 * any critical code paths, but if you used it somewhere that had to
2202 * be fast you want to avoid "GTK is slow with old WMs" complaints.
2203 * Probably at that point the function should be changed to query
2204 * _NET_SUPPORTING_WM_CHECK only once every 10 seconds or something.
2207 screen_x11 = GDK_SCREEN_X11 (screen);
2208 display = screen_x11->display;
2210 if (screen_x11->wmspec_check_window != None)
2211 return; /* already have it */
2213 XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), screen_x11->xroot_window,
2214 gdk_x11_get_xatom_by_name_for_display (display, "_NET_SUPPORTING_WM_CHECK"),
2215 0, G_MAXLONG, False, XA_WINDOW, &type, &format,
2216 &n_items, &bytes_after, (guchar **) & xwindow);
2218 if (type != XA_WINDOW)
2221 gdk_error_trap_push ();
2223 /* Find out if this WM goes away, so we can reset everything. */
2224 XSelectInput (screen_x11->xdisplay, *xwindow, StructureNotifyMask);
2226 screen_x11->wmspec_check_window = *xwindow;
2229 screen_x11->need_refetch_net_supported = TRUE;
2230 screen_x11->need_refetch_wm_name = TRUE;
2232 /* Careful, reentrancy */
2233 _gdk_x11_screen_window_manager_changed (GDK_SCREEN (screen_x11));
2237 gdk_x11_screen_get_window_manager_name (GdkScreen *screen)
2239 GdkScreenX11 *screen_x11;
2241 screen_x11 = GDK_SCREEN_X11 (screen);
2243 fetch_net_wm_check_window (screen);
2245 if (screen_x11->need_refetch_wm_name)
2247 /* Get the name of the window manager */
2248 screen_x11->need_refetch_wm_name = FALSE;
2250 g_free (screen_x11->window_manager_name);
2251 screen_x11->window_manager_name = g_strdup ("unknown");
2253 if (screen_x11->wmspec_check_window != None)
2263 XGetWindowProperty (GDK_DISPLAY_XDISPLAY (screen_x11->display),
2264 screen_x11->wmspec_check_window,
2265 gdk_x11_get_xatom_by_name_for_display (screen_x11->display,
2267 0, G_MAXLONG, False,
2268 gdk_x11_get_xatom_by_name_for_display (screen_x11->display,
2271 &n_items, &bytes_after,
2274 gdk_display_sync (screen_x11->display);
2276 gdk_error_trap_pop ();
2280 g_free (screen_x11->window_manager_name);
2281 screen_x11->window_manager_name = g_strdup (name);
2287 return GDK_SCREEN_X11 (screen)->window_manager_name;
2290 typedef struct _NetWmSupportedAtoms NetWmSupportedAtoms;
2292 struct _NetWmSupportedAtoms
2299 * gdk_x11_screen_supports_net_wm_hint:
2300 * @screen : the relevant #GdkScreen.
2301 * @property: a property atom.
2303 * This function is specific to the X11 backend of GDK, and indicates
2304 * whether the window manager supports a certain hint from the
2305 * Extended Window Manager Hints Specification. You can find this
2306 * specification on http://www.freedesktop.org.
2308 * When using this function, keep in mind that the window manager
2309 * can change over time; so you shouldn't use this function in
2310 * a way that impacts persistent application state. A common bug
2311 * is that your application can start up before the window manager
2312 * does when the user logs in, and before the window manager starts
2313 * gdk_x11_screen_supports_net_wm_hint() will return %FALSE for every property.
2314 * You can monitor the window_manager_changed signal on #GdkScreen to detect
2315 * a window manager change.
2317 * Return value: %TRUE if the window manager supports @property
2320 gdk_x11_screen_supports_net_wm_hint (GdkScreen *screen,
2324 GdkScreenX11 *screen_x11;
2325 NetWmSupportedAtoms *supported_atoms;
2326 GdkDisplay *display;
2328 g_return_val_if_fail (GDK_IS_SCREEN (screen), FALSE);
2330 screen_x11 = GDK_SCREEN_X11 (screen);
2331 display = screen_x11->display;
2333 supported_atoms = g_object_get_data (G_OBJECT (screen), "gdk-net-wm-supported-atoms");
2334 if (!supported_atoms)
2336 supported_atoms = g_new0 (NetWmSupportedAtoms, 1);
2337 g_object_set_data (G_OBJECT (screen), "gdk-net-wm-supported-atoms", supported_atoms);
2340 fetch_net_wm_check_window (screen);
2342 if (screen_x11->wmspec_check_window == None)
2345 if (screen_x11->need_refetch_net_supported)
2347 /* WM has changed since we last got the supported list,
2354 screen_x11->need_refetch_net_supported = FALSE;
2356 if (supported_atoms->atoms)
2357 XFree (supported_atoms->atoms);
2359 supported_atoms->atoms = NULL;
2360 supported_atoms->n_atoms = 0;
2362 XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), screen_x11->xroot_window,
2363 gdk_x11_get_xatom_by_name_for_display (display, "_NET_SUPPORTED"),
2364 0, G_MAXLONG, False, XA_ATOM, &type, &format,
2365 &supported_atoms->n_atoms, &bytes_after,
2366 (guchar **)&supported_atoms->atoms);
2368 if (type != XA_ATOM)
2372 if (supported_atoms->atoms == NULL)
2376 while (i < supported_atoms->n_atoms)
2378 if (supported_atoms->atoms[i] == gdk_x11_atom_to_xatom_for_display (display, property))
2388 * gdk_net_wm_supports:
2389 * @property: a property atom.
2391 * This function is specific to the X11 backend of GDK, and indicates
2392 * whether the window manager for the default screen supports a certain
2393 * hint from the Extended Window Manager Hints Specification. See
2394 * gdk_x11_screen_supports_net_wm_hint() for complete details.
2396 * Return value: %TRUE if the window manager supports @property
2399 gdk_net_wm_supports (GdkAtom property)
2401 return gdk_x11_screen_supports_net_wm_hint (gdk_screen_get_default (), property);
2406 const char *xsettings_name;
2407 const char *gdk_name;
2408 } settings_map[] = {
2409 { "Net/DoubleClickTime", "gtk-double-click-time" },
2410 { "Net/DndDragThreshold", "gtk-dnd-drag-threshold" },
2411 { "Gtk/CanChangeAccels", "gtk-can-change-accels" },
2412 { "Gtk/ColorPalette", "gtk-color-palette" },
2413 { "Gtk/FontName", "gtk-font-name" },
2414 { "Gtk/IconSizes", "gtk-icon-sizes" },
2415 { "Gtk/KeyThemeName", "gtk-key-theme-name" },
2416 { "Gtk/ToolbarStyle", "gtk-toolbar-style" },
2417 { "Gtk/ToolbarIconSize", "gtk-toolbar-icon-size" },
2418 { "Gtk/IMPreeditStyle", "gtk-im-preedit-style" },
2419 { "Gtk/IMStatusStyle", "gtk-im-status-style" },
2420 { "Net/CursorBlink", "gtk-cursor-blink" },
2421 { "Net/CursorBlinkTime", "gtk-cursor-blink-time" },
2422 { "Net/ThemeName", "gtk-theme-name" }
2426 gdk_xsettings_notify_cb (const char *name,
2427 XSettingsAction action,
2428 XSettingsSetting *setting,
2432 GdkScreen *screen = data;
2433 GdkScreenX11 *screen_x11 = data;
2436 if (screen_x11->xsettings_in_init)
2439 new_event.type = GDK_SETTING;
2440 new_event.setting.window = gdk_screen_get_root_window (screen);
2441 new_event.setting.send_event = FALSE;
2442 new_event.setting.name = NULL;
2444 for (i = 0; i < G_N_ELEMENTS (settings_map) ; i++)
2445 if (strcmp (settings_map[i].xsettings_name, name) == 0)
2447 new_event.setting.name = (char *)settings_map[i].gdk_name;
2452 if (!new_event.setting.name)
2457 case XSETTINGS_ACTION_NEW:
2458 new_event.setting.action = GDK_SETTING_ACTION_NEW;
2460 case XSETTINGS_ACTION_CHANGED:
2461 new_event.setting.action = GDK_SETTING_ACTION_CHANGED;
2463 case XSETTINGS_ACTION_DELETED:
2464 new_event.setting.action = GDK_SETTING_ACTION_DELETED;
2468 gdk_event_put (&new_event);
2472 check_transform (const gchar *xsettings_name,
2476 if (!g_value_type_transformable (src_type, dest_type))
2478 g_warning ("Cannot tranform xsetting %s of type %s to type %s\n",
2480 g_type_name (src_type),
2481 g_type_name (dest_type));
2489 * gdk_screen_get_setting:
2490 * @screen: the #GdkScreen where the setting is located
2491 * @name: the name of the setting
2492 * @value: location to store the value of the setting
2494 * Retrieves a desktop-wide setting such as double-click time
2495 * for the #GdkScreen @screen.
2497 * FIXME needs a list of valid settings here, or a link to
2500 * Returns : %TRUE if the setting existed and a value was stored
2501 * in @value, %FALSE otherwise.
2504 gdk_screen_get_setting (GdkScreen *screen,
2509 const char *xsettings_name = NULL;
2510 XSettingsResult result;
2511 XSettingsSetting *setting;
2512 GdkScreenX11 *screen_x11;
2513 gboolean success = FALSE;
2515 GValue tmp_val = { 0, };
2517 g_return_val_if_fail (GDK_IS_SCREEN (screen), FALSE);
2519 screen_x11 = GDK_SCREEN_X11 (screen);
2521 for (i = 0; i < G_N_ELEMENTS (settings_map) ; i++)
2522 if (strcmp (settings_map[i].gdk_name, name) == 0)
2524 xsettings_name = settings_map[i].xsettings_name;
2528 if (!xsettings_name)
2531 result = xsettings_client_get_setting (screen_x11->xsettings_client,
2532 xsettings_name, &setting);
2533 if (result != XSETTINGS_SUCCESS)
2536 switch (setting->type)
2538 case XSETTINGS_TYPE_INT:
2539 if (check_transform (xsettings_name, G_TYPE_INT, G_VALUE_TYPE (value)))
2541 g_value_init (&tmp_val, G_TYPE_INT);
2542 g_value_set_int (&tmp_val, setting->data.v_int);
2543 g_value_transform (&tmp_val, value);
2548 case XSETTINGS_TYPE_STRING:
2549 if (check_transform (xsettings_name, G_TYPE_STRING, G_VALUE_TYPE (value)))
2551 g_value_init (&tmp_val, G_TYPE_STRING);
2552 g_value_set_string (&tmp_val, setting->data.v_string);
2553 g_value_transform (&tmp_val, value);
2558 case XSETTINGS_TYPE_COLOR:
2559 if (!check_transform (xsettings_name, GDK_TYPE_COLOR, G_VALUE_TYPE (value)))
2563 g_value_init (&tmp_val, GDK_TYPE_COLOR);
2566 color.red = setting->data.v_color.red;
2567 color.green = setting->data.v_color.green;
2568 color.blue = setting->data.v_color.blue;
2570 g_value_set_boxed (&tmp_val, &color);
2572 g_value_transform (&tmp_val, value);
2579 g_value_unset (&tmp_val);
2581 xsettings_setting_free (setting);
2586 static GdkFilterReturn
2587 gdk_xsettings_client_event_filter (GdkXEvent *xevent,
2591 GdkScreenX11 *screen = data;
2593 if (xsettings_client_process_event (screen->xsettings_client, (XEvent *)xevent))
2594 return GDK_FILTER_REMOVE;
2596 return GDK_FILTER_CONTINUE;
2600 gdk_xsettings_watch_cb (Window window,
2606 GdkScreen *screen = cb_data;
2608 gdkwin = gdk_window_lookup_for_display (gdk_screen_get_display (screen), window);
2613 gdkwin = gdk_window_foreign_new_for_display (gdk_screen_get_display (screen), window);
2615 g_object_ref (gdkwin);
2617 gdk_window_add_filter (gdkwin, gdk_xsettings_client_event_filter, screen);
2622 gdk_window_remove_filter (gdkwin, gdk_xsettings_client_event_filter, screen);
2623 g_object_unref (gdkwin);