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.
321 gdk_display_add_client_message_filter (GdkDisplay *display,
322 GdkAtom message_type,
326 GdkClientFilter *filter;
327 g_return_if_fail (GDK_IS_DISPLAY (display));
328 filter = g_new (GdkClientFilter, 1);
330 filter->type = message_type;
331 filter->function = func;
334 GDK_DISPLAY_X11(display)->client_filters =
335 g_list_prepend (GDK_DISPLAY_X11 (display)->client_filters,
340 * gdk_add_client_message_filter:
341 * @message_type: the type of ClientMessage events to receive. This will be
342 * checked against the <structfield>message_type</structfield> field of the
343 * XClientMessage event struct.
344 * @func: the function to call to process the event.
345 * @data: user data to pass to @func.
347 * Adds a filter to the default display to be called when X ClientMessage events
348 * are received. See gdk_display_add_client_message_filter().
351 gdk_add_client_message_filter (GdkAtom message_type,
355 gdk_display_add_client_message_filter (gdk_display_get_default (),
356 message_type, func, data);
360 gdk_check_wm_state_changed (GdkWindow *window)
368 gboolean found_sticky, found_maxvert, found_maxhorz, found_fullscreen;
369 GdkWindowState old_state;
370 GdkDisplay *display = GDK_WINDOW_DISPLAY (window);
372 if (GDK_WINDOW_DESTROYED (window) ||
373 gdk_window_get_window_type (window) != GDK_WINDOW_TOPLEVEL)
376 found_sticky = FALSE;
377 found_maxvert = FALSE;
378 found_maxhorz = FALSE;
379 found_fullscreen = FALSE;
381 XGetWindowProperty (GDK_WINDOW_XDISPLAY (window), GDK_WINDOW_XID (window),
382 gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_STATE"),
383 0, G_MAXLONG, False, XA_ATOM, &type, &format, &nitems,
384 &bytes_after, (guchar **)&atoms);
388 Atom sticky_atom = gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_STATE_STICKY");
389 Atom maxvert_atom = gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_STATE_MAXIMIZED_VERT");
390 Atom maxhorz_atom = gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_STATE_MAXIMIZED_HORZ");
391 Atom fullscreen_atom = gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_STATE_FULLSCREEN");
396 if (atoms[i] == sticky_atom)
398 else if (atoms[i] == maxvert_atom)
399 found_maxvert = TRUE;
400 else if (atoms[i] == maxhorz_atom)
401 found_maxhorz = TRUE;
402 else if (atoms[i] == fullscreen_atom)
403 found_fullscreen = TRUE;
411 /* For found_sticky to remain TRUE, we have to also be on desktop
419 XGetWindowProperty (GDK_WINDOW_XDISPLAY (window),
420 GDK_WINDOW_XID (window),
421 gdk_x11_get_xatom_by_name_for_display
422 (display, "_NET_WM_DESKTOP"),
423 0, G_MAXLONG, False, XA_CARDINAL, &type,
425 &bytes_after, (guchar **)&desktop);
429 if (*desktop != 0xFFFFFFFF)
430 found_sticky = FALSE;
435 old_state = gdk_window_get_state (window);
437 if (old_state & GDK_WINDOW_STATE_STICKY)
440 gdk_synthesize_window_state (window,
441 GDK_WINDOW_STATE_STICKY,
447 gdk_synthesize_window_state (window,
449 GDK_WINDOW_STATE_STICKY);
452 if (old_state & GDK_WINDOW_STATE_FULLSCREEN)
454 if (!found_fullscreen)
455 gdk_synthesize_window_state (window,
456 GDK_WINDOW_STATE_FULLSCREEN,
461 if (found_fullscreen)
462 gdk_synthesize_window_state (window,
464 GDK_WINDOW_STATE_FULLSCREEN);
467 /* Our "maximized" means both vertical and horizontal; if only one,
468 * we don't expose that via GDK
470 if (old_state & GDK_WINDOW_STATE_MAXIMIZED)
472 if (!(found_maxvert && found_maxhorz))
473 gdk_synthesize_window_state (window,
474 GDK_WINDOW_STATE_MAXIMIZED,
479 if (found_maxvert && found_maxhorz)
480 gdk_synthesize_window_state (window,
482 GDK_WINDOW_STATE_MAXIMIZED);
486 #define HAS_FOCUS(window_impl) \
487 ((window_impl)->has_focus || (window_impl)->has_pointer_focus)
490 generate_focus_event (GdkWindow *window,
495 event.type = GDK_FOCUS_CHANGE;
496 event.focus_change.window = window;
497 event.focus_change.send_event = FALSE;
498 event.focus_change.in = in;
500 gdk_event_put (&event);
504 set_screen_from_root (GdkDisplay *display,
510 screen = _gdk_x11_display_screen_for_xrootwin (display, xrootwin);
513 gdk_event_set_screen (event, screen);
517 translate_key_event (GdkDisplay *display,
521 GdkKeymap *keymap = gdk_keymap_get_for_display (display);
525 event->key.type = xevent->xany.type == KeyPress ? GDK_KEY_PRESS : GDK_KEY_RELEASE;
526 event->key.time = xevent->xkey.time;
528 event->key.state = (GdkModifierType) xevent->xkey.state;
529 event->key.group = _gdk_x11_get_group_for_state (display, xevent->xkey.state);
530 event->key.hardware_keycode = xevent->xkey.keycode;
532 event->key.keyval = GDK_VoidSymbol;
534 gdk_keymap_translate_keyboard_state (keymap,
535 event->key.hardware_keycode,
541 /* Fill in event->string crudely, since various programs
544 event->key.string = NULL;
546 if (event->key.keyval != GDK_VoidSymbol)
547 c = gdk_keyval_to_unicode (event->key.keyval);
554 /* Apply the control key - Taken from Xlib
556 if (event->key.state & GDK_CONTROL_MASK)
558 if ((c >= '@' && c < '\177') || c == ' ') c &= 0x1F;
561 event->key.string = g_memdup ("\0\0", 2);
562 event->key.length = 1;
566 else if (c >= '3' && c <= '7') c -= ('3' - '\033');
567 else if (c == '8') c = '\177';
568 else if (c == '/') c = '_' & 0x1F;
571 len = g_unichar_to_utf8 (c, buf);
574 event->key.string = g_locale_from_utf8 (buf, len,
575 NULL, &bytes_written,
577 if (event->key.string)
578 event->key.length = bytes_written;
580 else if (event->key.keyval == GDK_Escape)
582 event->key.length = 1;
583 event->key.string = g_strdup ("\033");
585 else if (event->key.keyval == GDK_Return ||
586 event->key.keyval == GDK_KP_Enter)
588 event->key.length = 1;
589 event->key.string = g_strdup ("\r");
592 if (!event->key.string)
594 event->key.length = 0;
595 event->key.string = g_strdup ("");
599 #ifdef G_ENABLE_DEBUG
600 if (_gdk_debug_flags & GDK_DEBUG_EVENTS)
602 g_message ("%s:\t\twindow: %ld key: %12s %d",
603 event->type == GDK_KEY_PRESS ? "key press " : "key release",
605 event->key.keyval ? gdk_keyval_name (event->key.keyval) : "(none)",
608 if (event->key.length > 0)
609 g_message ("\t\tlength: %4d string: \"%s\"",
610 event->key.length, buf);
612 #endif /* G_ENABLE_DEBUG */
615 /* Return the window this has to do with, if any, rather
616 * than the frame or root window that was selecting
620 get_real_window (XEvent *event)
625 return event->xcreatewindow.window;
628 return event->xdestroywindow.window;
631 return event->xunmap.window;
634 return event->xmap.window;
637 return event->xmaprequest.window;
640 return event->xreparent.window;
642 case ConfigureNotify:
643 return event->xconfigure.window;
645 case ConfigureRequest:
646 return event->xconfigurerequest.window;
649 return event->xgravity.window;
651 case CirculateNotify:
652 return event->xcirculate.window;
654 case CirculateRequest:
655 return event->xcirculaterequest.window;
658 return event->xany.window;
663 gdk_event_translate (GdkDisplay *display,
666 gboolean return_exposes)
670 GdkWindowObject *window_private;
671 GdkWindowImplX11 *window_impl = NULL;
673 gint xoffset, yoffset;
674 GdkScreen *screen = NULL;
675 GdkScreenX11 *screen_x11 = NULL;
676 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
681 /* init these, since the done: block uses them */
683 window_private = NULL;
684 event->any.window = NULL;
686 if (_gdk_default_filters)
688 /* Apply global filters */
689 GdkFilterReturn result;
690 result = gdk_event_apply_filters (xevent, event,
691 _gdk_default_filters);
693 if (result != GDK_FILTER_CONTINUE)
695 return_val = (result == GDK_FILTER_TRANSLATE) ? TRUE : FALSE;
700 /* We handle events with window=None
701 * specially - they are generated by XFree86's XInput under
702 * some circumstances. This handling for obvious reasons
703 * goes before we bother to lookup the event window.
706 if (xevent->xany.window == None)
708 return_val = _gdk_input_window_none_event (event, xevent);
710 if (return_val >= 0) /* was handled */
716 /* Find the GdkWindow that this event relates to.
717 * Basically this means substructure events
718 * are reported same as structure events
720 xwindow = get_real_window (xevent);
722 window = gdk_window_lookup_for_display (display, xwindow);
723 window_private = (GdkWindowObject *) window;
727 screen = GDK_WINDOW_SCREEN (window);
728 screen_x11 = GDK_SCREEN_X11 (screen);
733 /* Window may be a pixmap, so check its type.
734 * (This check is probably too expensive unless
735 * GLib short-circuits an exact type match,
736 * which has been proposed)
738 if (GDK_IS_WINDOW (window))
740 window_impl = GDK_WINDOW_IMPL_X11 (window_private->impl);
742 /* Move key events on focus window to the real toplevel, and
743 * filter out all other events on focus window
745 if (xwindow == window_impl->focus_window)
747 switch (xevent->type)
751 xwindow = GDK_WINDOW_XID (window);
752 xevent->xany.window = xwindow;
760 g_object_ref (window);
763 event->any.window = window;
764 event->any.send_event = xevent->xany.send_event ? TRUE : FALSE;
766 if (window_private && GDK_WINDOW_DESTROYED (window))
768 if (xevent->type != DestroyNotify)
774 else if (window_private)
776 /* Apply per-window filters */
777 GdkFilterReturn result;
778 result = gdk_event_apply_filters (xevent, event,
779 window_private->filters);
781 if (result != GDK_FILTER_CONTINUE)
783 return_val = (result == GDK_FILTER_TRANSLATE) ? TRUE : FALSE;
788 if (screen_x11 && screen_x11->wmspec_check_window != None &&
789 xwindow == screen_x11->wmspec_check_window)
791 if (xevent->type == DestroyNotify)
793 screen_x11->wmspec_check_window = None;
794 g_free (screen_x11->window_manager_name);
795 screen_x11->window_manager_name = g_strdup ("unknown");
797 /* careful, reentrancy */
798 _gdk_x11_screen_window_manager_changed (GDK_SCREEN (screen_x11));
801 /* Eat events on this window unless someone had wrapped
802 * it as a foreign window
812 (xevent->xany.type == MotionNotify ||
813 xevent->xany.type == ButtonRelease))
815 if (_gdk_moveresize_handle_event (xevent))
822 /* We do a "manual" conversion of the XEvent to a
823 * GdkEvent. The structures are mostly the same so
824 * the conversion is fairly straightforward. We also
825 * optionally print debugging info regarding events
833 _gdk_windowing_window_get_offsets (window, &xoffset, &yoffset);
841 switch (xevent->type)
844 if (window_private == NULL)
849 translate_key_event (display, event, xevent);
853 if (window_private == NULL)
859 /* Emulate detectable auto-repeat by checking to see
860 * if the next event is a key press with the same
861 * keycode and timestamp, and if so, ignoring the event.
864 if (!display_x11->have_xkb_autorepeat && XPending (xevent->xkey.display))
868 XPeekEvent (xevent->xkey.display, &next_event);
870 if (next_event.type == KeyPress &&
871 next_event.xkey.keycode == xevent->xkey.keycode &&
872 next_event.xkey.time == xevent->xkey.time)
879 translate_key_event (display, event, xevent);
884 g_message ("button press:\t\twindow: %ld x,y: %d %d button: %d",
885 xevent->xbutton.window,
886 xevent->xbutton.x, xevent->xbutton.y,
887 xevent->xbutton.button));
889 if (window_private == NULL ||
890 ((window_private->extension_events != 0) &&
891 display_x11->input_ignore_core))
897 /* If we get a ButtonPress event where the button is 4 or 5,
898 it's a Scroll event */
899 switch (xevent->xbutton.button)
905 event->scroll.type = GDK_SCROLL;
907 if (xevent->xbutton.button == 4)
908 event->scroll.direction = GDK_SCROLL_UP;
909 else if (xevent->xbutton.button == 5)
910 event->scroll.direction = GDK_SCROLL_DOWN;
911 else if (xevent->xbutton.button == 6)
912 event->scroll.direction = GDK_SCROLL_LEFT;
914 event->scroll.direction = GDK_SCROLL_RIGHT;
916 event->scroll.window = window;
917 event->scroll.time = xevent->xbutton.time;
918 event->scroll.x = xevent->xbutton.x + xoffset;
919 event->scroll.y = xevent->xbutton.y + yoffset;
920 event->scroll.x_root = (gfloat)xevent->xbutton.x_root;
921 event->scroll.y_root = (gfloat)xevent->xbutton.y_root;
922 event->scroll.state = (GdkModifierType) xevent->xbutton.state;
923 event->scroll.device = display->core_pointer;
925 set_screen_from_root (display, event, xevent->xbutton.root);
930 event->button.type = GDK_BUTTON_PRESS;
931 event->button.window = window;
932 event->button.time = xevent->xbutton.time;
933 event->button.x = xevent->xbutton.x + xoffset;
934 event->button.y = xevent->xbutton.y + yoffset;
935 event->button.x_root = (gfloat)xevent->xbutton.x_root;
936 event->button.y_root = (gfloat)xevent->xbutton.y_root;
937 event->button.axes = NULL;
938 event->button.state = (GdkModifierType) xevent->xbutton.state;
939 event->button.button = xevent->xbutton.button;
940 event->button.device = display->core_pointer;
942 set_screen_from_root (display, event, xevent->xbutton.root);
944 _gdk_event_button_generate (display, event);
952 g_message ("button release:\twindow: %ld x,y: %d %d button: %d",
953 xevent->xbutton.window,
954 xevent->xbutton.x, xevent->xbutton.y,
955 xevent->xbutton.button));
957 if (window_private == NULL ||
958 ((window_private->extension_events != 0) &&
959 display_x11->input_ignore_core))
965 /* We treat button presses as scroll wheel events, so ignore the release */
966 if (xevent->xbutton.button == 4 || xevent->xbutton.button == 5 ||
967 xevent->xbutton.button == 6 || xevent->xbutton.button ==7)
973 event->button.type = GDK_BUTTON_RELEASE;
974 event->button.window = window;
975 event->button.time = xevent->xbutton.time;
976 event->button.x = xevent->xbutton.x + xoffset;
977 event->button.y = xevent->xbutton.y + yoffset;
978 event->button.x_root = (gfloat)xevent->xbutton.x_root;
979 event->button.y_root = (gfloat)xevent->xbutton.y_root;
980 event->button.axes = NULL;
981 event->button.state = (GdkModifierType) xevent->xbutton.state;
982 event->button.button = xevent->xbutton.button;
983 event->button.device = display->core_pointer;
985 set_screen_from_root (display, event, xevent->xbutton.root);
991 g_message ("motion notify:\t\twindow: %ld x,y: %d %d hint: %s",
992 xevent->xmotion.window,
993 xevent->xmotion.x, xevent->xmotion.y,
994 (xevent->xmotion.is_hint) ? "true" : "false"));
996 if (window_private == NULL ||
997 ((window_private->extension_events != 0) &&
998 display_x11->input_ignore_core))
1004 event->motion.type = GDK_MOTION_NOTIFY;
1005 event->motion.window = window;
1006 event->motion.time = xevent->xmotion.time;
1007 event->motion.x = xevent->xmotion.x + xoffset;
1008 event->motion.y = xevent->xmotion.y + yoffset;
1009 event->motion.x_root = (gfloat)xevent->xmotion.x_root;
1010 event->motion.y_root = (gfloat)xevent->xmotion.y_root;
1011 event->motion.axes = NULL;
1012 event->motion.state = (GdkModifierType) xevent->xmotion.state;
1013 event->motion.is_hint = xevent->xmotion.is_hint;
1014 event->motion.device = display->core_pointer;
1016 set_screen_from_root (display, event, xevent->xmotion.root);
1022 g_message ("enter notify:\t\twindow: %ld detail: %d subwin: %ld",
1023 xevent->xcrossing.window,
1024 xevent->xcrossing.detail,
1025 xevent->xcrossing.subwindow));
1027 if (window_private == NULL)
1033 /* Handle focusing (in the case where no window manager is running */
1035 GDK_WINDOW_TYPE (window) != GDK_WINDOW_CHILD &&
1036 xevent->xcrossing.detail != NotifyInferior &&
1037 xevent->xcrossing.focus && !window_impl->has_focus)
1039 gboolean had_focus = HAS_FOCUS (window_impl);
1041 window_impl->has_pointer_focus = TRUE;
1043 if (HAS_FOCUS (window_impl) != had_focus)
1044 generate_focus_event (window, TRUE);
1047 /* Tell XInput stuff about it if appropriate */
1048 if (window_private &&
1049 !GDK_WINDOW_DESTROYED (window) &&
1050 window_private->extension_events != 0)
1051 _gdk_input_enter_event (&xevent->xcrossing, window);
1053 event->crossing.type = GDK_ENTER_NOTIFY;
1054 event->crossing.window = window;
1056 /* If the subwindow field of the XEvent is non-NULL, then
1057 * lookup the corresponding GdkWindow.
1059 if (xevent->xcrossing.subwindow != None)
1060 event->crossing.subwindow = gdk_window_lookup_for_display (display, xevent->xcrossing.subwindow);
1062 event->crossing.subwindow = NULL;
1064 event->crossing.time = xevent->xcrossing.time;
1065 event->crossing.x = xevent->xcrossing.x + xoffset;
1066 event->crossing.y = xevent->xcrossing.y + yoffset;
1067 event->crossing.x_root = xevent->xcrossing.x_root;
1068 event->crossing.y_root = xevent->xcrossing.y_root;
1070 set_screen_from_root (display, event, xevent->xcrossing.root);
1072 /* Translate the crossing mode into Gdk terms.
1074 switch (xevent->xcrossing.mode)
1077 event->crossing.mode = GDK_CROSSING_NORMAL;
1080 event->crossing.mode = GDK_CROSSING_GRAB;
1083 event->crossing.mode = GDK_CROSSING_UNGRAB;
1087 /* Translate the crossing detail into Gdk terms.
1089 switch (xevent->xcrossing.detail)
1091 case NotifyInferior:
1092 event->crossing.detail = GDK_NOTIFY_INFERIOR;
1094 case NotifyAncestor:
1095 event->crossing.detail = GDK_NOTIFY_ANCESTOR;
1098 event->crossing.detail = GDK_NOTIFY_VIRTUAL;
1100 case NotifyNonlinear:
1101 event->crossing.detail = GDK_NOTIFY_NONLINEAR;
1103 case NotifyNonlinearVirtual:
1104 event->crossing.detail = GDK_NOTIFY_NONLINEAR_VIRTUAL;
1107 event->crossing.detail = GDK_NOTIFY_UNKNOWN;
1111 event->crossing.focus = xevent->xcrossing.focus;
1112 event->crossing.state = xevent->xcrossing.state;
1118 g_message ("leave notify:\t\twindow: %ld detail: %d subwin: %ld",
1119 xevent->xcrossing.window,
1120 xevent->xcrossing.detail, xevent->xcrossing.subwindow));
1122 if (window_private == NULL)
1128 /* Handle focusing (in the case where no window manager is running */
1130 GDK_WINDOW_TYPE (window) != GDK_WINDOW_CHILD &&
1131 xevent->xcrossing.detail != NotifyInferior &&
1132 xevent->xcrossing.focus && !window_impl->has_focus)
1134 gboolean had_focus = HAS_FOCUS (window_impl);
1136 window_impl->has_pointer_focus = FALSE;
1138 if (HAS_FOCUS (window_impl) != had_focus)
1139 generate_focus_event (window, FALSE);
1142 event->crossing.type = GDK_LEAVE_NOTIFY;
1143 event->crossing.window = window;
1145 /* If the subwindow field of the XEvent is non-NULL, then
1146 * lookup the corresponding GdkWindow.
1148 if (xevent->xcrossing.subwindow != None)
1149 event->crossing.subwindow = gdk_window_lookup_for_display (display, xevent->xcrossing.subwindow);
1151 event->crossing.subwindow = NULL;
1153 event->crossing.time = xevent->xcrossing.time;
1154 event->crossing.x = xevent->xcrossing.x + xoffset;
1155 event->crossing.y = xevent->xcrossing.y + yoffset;
1156 event->crossing.x_root = xevent->xcrossing.x_root;
1157 event->crossing.y_root = xevent->xcrossing.y_root;
1159 set_screen_from_root (display, event, xevent->xcrossing.root);
1161 /* Translate the crossing mode into Gdk terms.
1163 switch (xevent->xcrossing.mode)
1166 event->crossing.mode = GDK_CROSSING_NORMAL;
1169 event->crossing.mode = GDK_CROSSING_GRAB;
1172 event->crossing.mode = GDK_CROSSING_UNGRAB;
1176 /* Translate the crossing detail into Gdk terms.
1178 switch (xevent->xcrossing.detail)
1180 case NotifyInferior:
1181 event->crossing.detail = GDK_NOTIFY_INFERIOR;
1183 case NotifyAncestor:
1184 event->crossing.detail = GDK_NOTIFY_ANCESTOR;
1187 event->crossing.detail = GDK_NOTIFY_VIRTUAL;
1189 case NotifyNonlinear:
1190 event->crossing.detail = GDK_NOTIFY_NONLINEAR;
1192 case NotifyNonlinearVirtual:
1193 event->crossing.detail = GDK_NOTIFY_NONLINEAR_VIRTUAL;
1196 event->crossing.detail = GDK_NOTIFY_UNKNOWN;
1200 event->crossing.focus = xevent->xcrossing.focus;
1201 event->crossing.state = xevent->xcrossing.state;
1205 /* We only care about focus events that indicate that _this_
1206 * window (not a ancestor or child) got or lost the focus
1210 g_message ("focus in:\t\twindow: %ld", xevent->xfocus.window));
1212 if (window && GDK_WINDOW_TYPE (window) != GDK_WINDOW_CHILD)
1214 gboolean had_focus = HAS_FOCUS (window_impl);
1216 switch (xevent->xfocus.detail)
1218 case NotifyAncestor:
1219 case NotifyNonlinear:
1221 case NotifyNonlinearVirtual:
1222 /* We pretend that the focus moves to the grab
1223 * window, so we pay attention to NotifyGrab
1224 * NotifyUngrab, and ignore NotifyWhileGrabbed
1226 if (xevent->xfocus.mode != NotifyWhileGrabbed)
1227 window_impl->has_focus = TRUE;
1230 /* The X server sends NotifyPointer/NotifyGrab,
1231 * but the pointer focus is ignored while a
1234 if (xevent->xfocus.mode != NotifyGrab)
1235 window_impl->has_pointer_focus = TRUE;
1237 case NotifyInferior:
1238 case NotifyPointerRoot:
1239 case NotifyDetailNone:
1243 if (HAS_FOCUS (window_impl) != had_focus)
1244 generate_focus_event (window, TRUE);
1249 g_message ("focus out:\t\twindow: %ld", xevent->xfocus.window));
1251 if (window && GDK_WINDOW_TYPE (window) != GDK_WINDOW_CHILD)
1253 gboolean had_focus = HAS_FOCUS (window_impl);
1255 switch (xevent->xfocus.detail)
1257 case NotifyAncestor:
1258 case NotifyNonlinear:
1260 case NotifyNonlinearVirtual:
1261 if (xevent->xfocus.mode != NotifyWhileGrabbed)
1262 window_impl->has_focus = FALSE;
1265 if (xevent->xfocus.mode != NotifyUngrab)
1266 window_impl->has_pointer_focus = FALSE;
1268 case NotifyInferior:
1269 case NotifyPointerRoot:
1270 case NotifyDetailNone:
1274 if (HAS_FOCUS (window_impl) != had_focus)
1275 generate_focus_event (window, FALSE);
1280 /* gdk_keyboard_grab() causes following events. These events confuse
1281 * the XIM focus, so ignore them.
1283 if (xevent->xfocus.mode == NotifyGrab ||
1284 xevent->xfocus.mode == NotifyUngrab)
1290 g_message ("keymap notify"));
1292 /* Not currently handled */
1298 g_message ("expose:\t\twindow: %ld %d x,y: %d %d w,h: %d %d%s",
1299 xevent->xexpose.window, xevent->xexpose.count,
1300 xevent->xexpose.x, xevent->xexpose.y,
1301 xevent->xexpose.width, xevent->xexpose.height,
1302 event->any.send_event ? " (send)" : ""));
1304 if (window_private == NULL)
1311 GdkRectangle expose_rect;
1313 expose_rect.x = xevent->xexpose.x + xoffset;
1314 expose_rect.y = xevent->xexpose.y + yoffset;
1315 expose_rect.width = xevent->xexpose.width;
1316 expose_rect.height = xevent->xexpose.height;
1320 event->expose.type = GDK_EXPOSE;
1321 event->expose.area = expose_rect;
1322 event->expose.region = gdk_region_rectangle (&expose_rect);
1323 event->expose.window = window;
1324 event->expose.count = xevent->xexpose.count;
1330 _gdk_window_process_expose (window, xevent->xexpose.serial, &expose_rect);
1339 case GraphicsExpose:
1341 GdkRectangle expose_rect;
1344 g_message ("graphics expose:\tdrawable: %ld",
1345 xevent->xgraphicsexpose.drawable));
1347 if (window_private == NULL)
1353 expose_rect.x = xevent->xgraphicsexpose.x + xoffset;
1354 expose_rect.y = xevent->xgraphicsexpose.y + yoffset;
1355 expose_rect.width = xevent->xgraphicsexpose.width;
1356 expose_rect.height = xevent->xgraphicsexpose.height;
1360 event->expose.type = GDK_EXPOSE;
1361 event->expose.area = expose_rect;
1362 event->expose.region = gdk_region_rectangle (&expose_rect);
1363 event->expose.window = window;
1364 event->expose.count = xevent->xgraphicsexpose.count;
1370 _gdk_window_process_expose (window, xevent->xgraphicsexpose.serial, &expose_rect);
1380 g_message ("no expose:\t\tdrawable: %ld",
1381 xevent->xnoexpose.drawable));
1383 event->no_expose.type = GDK_NO_EXPOSE;
1384 event->no_expose.window = window;
1388 case VisibilityNotify:
1389 #ifdef G_ENABLE_DEBUG
1390 if (_gdk_debug_flags & GDK_DEBUG_EVENTS)
1391 switch (xevent->xvisibility.state)
1393 case VisibilityFullyObscured:
1394 g_message ("visibility notify:\twindow: %ld none",
1395 xevent->xvisibility.window);
1397 case VisibilityPartiallyObscured:
1398 g_message ("visibility notify:\twindow: %ld partial",
1399 xevent->xvisibility.window);
1401 case VisibilityUnobscured:
1402 g_message ("visibility notify:\twindow: %ld full",
1403 xevent->xvisibility.window);
1406 #endif /* G_ENABLE_DEBUG */
1408 if (window_private == NULL)
1414 event->visibility.type = GDK_VISIBILITY_NOTIFY;
1415 event->visibility.window = window;
1417 switch (xevent->xvisibility.state)
1419 case VisibilityFullyObscured:
1420 event->visibility.state = GDK_VISIBILITY_FULLY_OBSCURED;
1423 case VisibilityPartiallyObscured:
1424 event->visibility.state = GDK_VISIBILITY_PARTIAL;
1427 case VisibilityUnobscured:
1428 event->visibility.state = GDK_VISIBILITY_UNOBSCURED;
1436 g_message ("create notify:\twindow: %ld x,y: %d %d w,h: %d %d b-w: %d parent: %ld ovr: %d",
1437 xevent->xcreatewindow.window,
1438 xevent->xcreatewindow.x,
1439 xevent->xcreatewindow.y,
1440 xevent->xcreatewindow.width,
1441 xevent->xcreatewindow.height,
1442 xevent->xcreatewindow.border_width,
1443 xevent->xcreatewindow.parent,
1444 xevent->xcreatewindow.override_redirect));
1445 /* not really handled */
1450 g_message ("destroy notify:\twindow: %ld",
1451 xevent->xdestroywindow.window));
1453 /* Ignore DestroyNotify from SubstructureNotifyMask */
1454 if (xevent->xdestroywindow.window == xevent->xdestroywindow.event)
1456 event->any.type = GDK_DESTROY;
1457 event->any.window = window;
1459 return_val = window_private && !GDK_WINDOW_DESTROYED (window);
1461 if (window && GDK_WINDOW_XID (window) != screen_x11->xroot_window)
1462 gdk_window_destroy_notify (window);
1471 g_message ("unmap notify:\t\twindow: %ld",
1472 xevent->xmap.window));
1474 event->any.type = GDK_UNMAP;
1475 event->any.window = window;
1477 /* If we are shown (not withdrawn) and get an unmap, it means we
1478 * were iconified in the X sense. If we are withdrawn, and get
1479 * an unmap, it means we hid the window ourselves, so we
1480 * will have already flipped the iconified bit off.
1484 if (GDK_WINDOW_IS_MAPPED (window))
1485 gdk_synthesize_window_state (window,
1487 GDK_WINDOW_STATE_ICONIFIED);
1489 _gdk_xgrab_check_unmap (window, xevent->xany.serial);
1496 g_message ("map notify:\t\twindow: %ld",
1497 xevent->xmap.window));
1499 event->any.type = GDK_MAP;
1500 event->any.window = window;
1502 /* Unset iconified if it was set */
1503 if (window && (((GdkWindowObject*)window)->state & GDK_WINDOW_STATE_ICONIFIED))
1504 gdk_synthesize_window_state (window,
1505 GDK_WINDOW_STATE_ICONIFIED,
1510 case ReparentNotify:
1512 g_message ("reparent notify:\twindow: %ld x,y: %d %d parent: %ld ovr: %d",
1513 xevent->xreparent.window,
1514 xevent->xreparent.x,
1515 xevent->xreparent.y,
1516 xevent->xreparent.parent,
1517 xevent->xreparent.override_redirect));
1519 /* Not currently handled */
1523 case ConfigureNotify:
1525 g_message ("configure notify:\twindow: %ld x,y: %d %d w,h: %d %d b-w: %d above: %ld ovr: %d%s",
1526 xevent->xconfigure.window,
1527 xevent->xconfigure.x,
1528 xevent->xconfigure.y,
1529 xevent->xconfigure.width,
1530 xevent->xconfigure.height,
1531 xevent->xconfigure.border_width,
1532 xevent->xconfigure.above,
1533 xevent->xconfigure.override_redirect,
1536 : GDK_WINDOW_TYPE (window) == GDK_WINDOW_CHILD
1537 ? " (discarding child)"
1538 : xevent->xconfigure.event != xevent->xconfigure.window
1539 ? " (discarding substructure)"
1541 if (window && GDK_WINDOW_TYPE (window) == GDK_WINDOW_ROOT)
1542 _gdk_x11_screen_size_changed (screen, xevent);
1545 xevent->xconfigure.event == xevent->xconfigure.window &&
1546 !GDK_WINDOW_DESTROYED (window) &&
1547 (window_private->extension_events != 0))
1548 _gdk_input_configure_event (&xevent->xconfigure, window);
1551 xevent->xconfigure.event != xevent->xconfigure.window ||
1552 GDK_WINDOW_TYPE (window) == GDK_WINDOW_CHILD ||
1553 GDK_WINDOW_TYPE (window) == GDK_WINDOW_ROOT)
1557 event->configure.type = GDK_CONFIGURE;
1558 event->configure.window = window;
1559 event->configure.width = xevent->xconfigure.width;
1560 event->configure.height = xevent->xconfigure.height;
1562 if (!xevent->xconfigure.send_event &&
1563 !GDK_WINDOW_DESTROYED (window))
1567 Window child_window = 0;
1569 gdk_error_trap_push ();
1570 if (XTranslateCoordinates (GDK_DRAWABLE_XDISPLAY (window),
1571 GDK_DRAWABLE_XID (window),
1572 screen_x11->xroot_window,
1577 if (!gdk_error_trap_pop ())
1579 event->configure.x = tx;
1580 event->configure.y = ty;
1584 gdk_error_trap_pop ();
1588 event->configure.x = xevent->xconfigure.x;
1589 event->configure.y = xevent->xconfigure.y;
1591 window_private->x = event->configure.x;
1592 window_private->y = event->configure.y;
1593 GDK_WINDOW_IMPL_X11 (window_private->impl)->width = xevent->xconfigure.width;
1594 GDK_WINDOW_IMPL_X11 (window_private->impl)->height = xevent->xconfigure.height;
1595 if (window_private->resize_count >= 1)
1597 window_private->resize_count -= 1;
1599 if (window_private->resize_count == 0)
1600 _gdk_moveresize_configure_done (display, window);
1605 case PropertyNotify:
1607 g_message ("property notify:\twindow: %ld, atom(%ld): %s%s%s",
1608 xevent->xproperty.window,
1609 xevent->xproperty.atom,
1611 gdk_x11_get_xatom_name_for_display (display, xevent->xproperty.atom),
1614 if (window_private == NULL)
1620 if (xevent->xproperty.atom == gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_STATE") ||
1621 xevent->xproperty.atom == gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_DESKTOP"))
1623 /* If window state changed, then synthesize those events. */
1624 gdk_check_wm_state_changed (window);
1627 if (window_private->event_mask & GDK_PROPERTY_CHANGE_MASK)
1629 event->property.type = GDK_PROPERTY_NOTIFY;
1630 event->property.window = window;
1631 event->property.atom = gdk_x11_xatom_to_atom_for_display (display, xevent->xproperty.atom);
1632 event->property.time = xevent->xproperty.time;
1633 event->property.state = xevent->xproperty.state;
1640 case SelectionClear:
1642 g_message ("selection clear:\twindow: %ld",
1643 xevent->xproperty.window));
1645 if (_gdk_selection_filter_clear_event (&xevent->xselectionclear))
1647 event->selection.type = GDK_SELECTION_CLEAR;
1648 event->selection.window = window;
1649 event->selection.selection = gdk_x11_xatom_to_atom_for_display (display, xevent->xselectionclear.selection);
1650 event->selection.time = xevent->xselectionclear.time;
1657 case SelectionRequest:
1659 g_message ("selection request:\twindow: %ld",
1660 xevent->xproperty.window));
1662 event->selection.type = GDK_SELECTION_REQUEST;
1663 event->selection.window = window;
1664 event->selection.selection = gdk_x11_xatom_to_atom_for_display (display, xevent->xselectionrequest.selection);
1665 event->selection.target = gdk_x11_xatom_to_atom_for_display (display, xevent->xselectionrequest.target);
1666 event->selection.property = gdk_x11_xatom_to_atom_for_display (display, xevent->xselectionrequest.property);
1667 event->selection.requestor = xevent->xselectionrequest.requestor;
1668 event->selection.time = xevent->xselectionrequest.time;
1672 case SelectionNotify:
1674 g_message ("selection notify:\twindow: %ld",
1675 xevent->xproperty.window));
1678 event->selection.type = GDK_SELECTION_NOTIFY;
1679 event->selection.window = window;
1680 event->selection.selection = gdk_x11_xatom_to_atom_for_display (display, xevent->xselection.selection);
1681 event->selection.target = gdk_x11_xatom_to_atom_for_display (display, xevent->xselection.target);
1682 event->selection.property = gdk_x11_xatom_to_atom_for_display (display, xevent->xselection.property);
1683 event->selection.time = xevent->xselection.time;
1687 case ColormapNotify:
1689 g_message ("colormap notify:\twindow: %ld",
1690 xevent->xcolormap.window));
1692 /* Not currently handled */
1699 GdkFilterReturn result = GDK_FILTER_CONTINUE;
1700 GdkAtom message_type = gdk_x11_xatom_to_atom_for_display (display, xevent->xclient.message_type);
1703 g_message ("client message:\twindow: %ld",
1704 xevent->xclient.window));
1706 tmp_list = display_x11->client_filters;
1709 GdkClientFilter *filter = tmp_list->data;
1710 if (filter->type == message_type)
1712 result = (*filter->function) (xevent, event, filter->data);
1716 tmp_list = tmp_list->next;
1721 case GDK_FILTER_REMOVE:
1724 case GDK_FILTER_TRANSLATE:
1727 case GDK_FILTER_CONTINUE:
1728 /* Send unknown ClientMessage's on to Gtk for it to use */
1729 if (window_private == NULL)
1735 event->client.type = GDK_CLIENT_EVENT;
1736 event->client.window = window;
1737 event->client.message_type = message_type;
1738 event->client.data_format = xevent->xclient.format;
1739 memcpy(&event->client.data, &xevent->xclient.data,
1740 sizeof(event->client.data));
1750 g_message ("mapping notify"));
1752 /* Let XLib know that there is a new keyboard mapping.
1754 XRefreshKeyboardMapping (&xevent->xmapping);
1755 _gdk_keymap_keys_changed (display);
1761 if (xevent->type == display_x11->xkb_event_type)
1763 XkbEvent *xkb_event = (XkbEvent *)xevent;
1765 switch (xkb_event->any.xkb_type)
1768 _gdk_keymap_keys_changed (display);
1773 case XkbStateNotify:
1774 _gdk_keymap_state_changed (display);
1781 /* something else - (e.g., a Xinput event) */
1783 if (window_private &&
1784 !GDK_WINDOW_DESTROYED (window_private) &&
1785 (window_private->extension_events != 0))
1786 return_val = _gdk_input_other_event(event, xevent, window);
1797 if (event->any.window)
1798 g_object_ref (event->any.window);
1799 if (((event->any.type == GDK_ENTER_NOTIFY) ||
1800 (event->any.type == GDK_LEAVE_NOTIFY)) &&
1801 (event->crossing.subwindow != NULL))
1802 g_object_ref (event->crossing.subwindow);
1806 /* Mark this event as having no resources to be freed */
1807 event->any.window = NULL;
1808 event->any.type = GDK_NOTHING;
1812 g_object_unref (window);
1817 static GdkFilterReturn
1818 gdk_wm_protocols_filter (GdkXEvent *xev,
1822 XEvent *xevent = (XEvent *)xev;
1823 GdkWindow *win = event->any.window;
1824 GdkDisplay *display = GDK_WINDOW_DISPLAY (win);
1826 if ((Atom) xevent->xclient.data.l[0] == gdk_x11_get_xatom_by_name_for_display (display, "WM_DELETE_WINDOW"))
1828 /* The delete window request specifies a window
1829 * to delete. We don't actually destroy the
1830 * window because "it is only a request". (The
1831 * window might contain vital data that the
1832 * program does not want destroyed). Instead
1833 * the event is passed along to the program,
1834 * which should then destroy the window.
1837 g_message ("delete window:\t\twindow: %ld",
1838 xevent->xclient.window));
1840 event->any.type = GDK_DELETE;
1842 return GDK_FILTER_TRANSLATE;
1844 else if ((Atom) xevent->xclient.data.l[0] == gdk_x11_get_xatom_by_name_for_display (display, "WM_TAKE_FOCUS"))
1846 GdkWindow *win = event->any.window;
1847 Window focus_win = GDK_WINDOW_IMPL_X11(((GdkWindowObject *)win)->impl)->focus_window;
1849 /* There is no way of knowing reliably whether we are viewable so we need
1850 * to trap errors so we don't cause a BadMatch.
1852 gdk_error_trap_push ();
1853 XSetInputFocus (GDK_WINDOW_XDISPLAY (win),
1856 xevent->xclient.data.l[1]);
1857 XSync (GDK_WINDOW_XDISPLAY (win), False);
1858 gdk_error_trap_pop ();
1860 else if ((Atom) xevent->xclient.data.l[0] == gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_PING"))
1862 XEvent xev = *xevent;
1864 xev.xclient.window = GDK_WINDOW_XROOTWIN (win);
1865 XSendEvent (GDK_WINDOW_XDISPLAY (win),
1868 SubstructureRedirectMask | SubstructureNotifyMask, &xev);
1871 return GDK_FILTER_REMOVE;
1875 _gdk_events_queue (GdkDisplay *display)
1880 Display *xdisplay = GDK_DISPLAY_XDISPLAY (display);
1882 while (!_gdk_event_queue_find_first(display) && XPending (xdisplay))
1884 XNextEvent (xdisplay, &xevent);
1886 switch (xevent.type)
1892 if (XFilterEvent (&xevent, None))
1896 event = gdk_event_new (GDK_NOTHING);
1898 event->any.window = NULL;
1899 event->any.send_event = xevent.xany.send_event ? TRUE : FALSE;
1901 ((GdkEventPrivate *)event)->flags |= GDK_EVENT_PENDING;
1903 node = _gdk_event_queue_append (display, event);
1905 if (gdk_event_translate (display, event, &xevent, FALSE))
1907 ((GdkEventPrivate *)event)->flags &= ~GDK_EVENT_PENDING;
1911 _gdk_event_queue_remove_link (display, node);
1912 g_list_free_1 (node);
1913 gdk_event_free (event);
1919 gdk_event_prepare (GSource *source,
1922 GdkDisplay *display = ((GdkDisplaySource*)source)->display;
1925 GDK_THREADS_ENTER ();
1928 retval = (_gdk_event_queue_find_first (display) != NULL ||
1929 gdk_check_xpending (display));
1931 GDK_THREADS_LEAVE ();
1937 gdk_event_check (GSource *source)
1939 GdkDisplaySource *display_source = (GdkDisplaySource*)source;
1942 GDK_THREADS_ENTER ();
1944 if (display_source->event_poll_fd.revents & G_IO_IN)
1945 retval = (_gdk_event_queue_find_first (display_source->display) != NULL ||
1946 gdk_check_xpending (display_source->display));
1950 GDK_THREADS_LEAVE ();
1956 gdk_event_dispatch (GSource *source,
1957 GSourceFunc callback,
1960 GdkDisplay *display = ((GdkDisplaySource*)source)->display;
1963 GDK_THREADS_ENTER ();
1965 _gdk_events_queue (display);
1966 event = _gdk_event_unqueue (display);
1970 if (_gdk_event_func)
1971 (*_gdk_event_func) (event, _gdk_event_data);
1973 gdk_event_free (event);
1976 GDK_THREADS_LEAVE ();
1982 * gdk_event_send_client_message_for_display:
1983 * @display: the #GdkDisplay for the window where the message is to be sent.
1984 * @event: the #GdkEvent to send, which should be a #GdkEventClient.
1985 * @winid: the window to send the X ClientMessage event to.
1987 * Sends an X ClientMessage event to a given window.
1989 * This could be used for communicating between different applications,
1990 * though the amount of data is limited to 20 bytes.
1992 * Returns: non-zero on success.
1997 gdk_event_send_client_message_for_display (GdkDisplay *display,
1999 GdkNativeWindow winid)
2003 g_return_val_if_fail(event != NULL, FALSE);
2005 /* Set up our event to send, with the exception of its target window */
2006 sev.xclient.type = ClientMessage;
2007 sev.xclient.display = GDK_DISPLAY_XDISPLAY (display);
2008 sev.xclient.format = event->client.data_format;
2009 sev.xclient.window = winid;
2010 memcpy(&sev.xclient.data, &event->client.data, sizeof (sev.xclient.data));
2011 sev.xclient.message_type = gdk_x11_atom_to_xatom_for_display (display, event->client.message_type);
2013 return _gdk_send_xevent (display, winid, False, NoEventMask, &sev);
2018 /* Sends a ClientMessage to all toplevel client windows */
2020 gdk_event_send_client_message_to_all_recurse (GdkDisplay *display,
2027 unsigned long nitems, after;
2028 unsigned char *data;
2029 Window *ret_children, ret_root, ret_parent;
2030 unsigned int ret_nchildren;
2031 gboolean send = FALSE;
2032 gboolean found = FALSE;
2033 gboolean result = FALSE;
2036 gdk_error_trap_push ();
2038 if (XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), xid,
2039 gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_STATE"),
2040 0, 0, False, AnyPropertyType,
2041 &type, &format, &nitems, &after, &data) != Success)
2051 /* OK, we're all set, now let's find some windows to send this to */
2052 if (!XQueryTree (GDK_DISPLAY_XDISPLAY (display), xid,
2053 &ret_root, &ret_parent,
2054 &ret_children, &ret_nchildren))
2057 for(i = 0; i < ret_nchildren; i++)
2058 if (gdk_event_send_client_message_to_all_recurse (display, xev, ret_children[i], level + 1))
2061 XFree (ret_children);
2064 if (send || (!found && (level == 1)))
2066 xev->xclient.window = xid;
2067 _gdk_send_xevent (display, xid, False, NoEventMask, xev);
2070 result = send || found;
2073 gdk_error_trap_pop ();
2079 * gdk_screen_broadcast_client_message:
2080 * @screen: the #GdkScreen where the event will be broadcasted.
2081 * @event: the #GdkEvent.
2083 * Sends an X ClientMessage event to all toplevel windows on @screen.
2085 * Toplevel windows are determined by checking for the WM_STATE property,
2086 * as described in the Inter-Client Communication Conventions Manual (ICCCM).
2087 * If no windows are found with the WM_STATE property set, the message is
2088 * sent to all children of the root window.
2094 gdk_screen_broadcast_client_message (GdkScreen *screen,
2098 GdkWindow *root_window;
2100 g_return_if_fail (event != NULL);
2102 root_window = gdk_screen_get_root_window (screen);
2104 /* Set up our event to send, with the exception of its target window */
2105 sev.xclient.type = ClientMessage;
2106 sev.xclient.display = GDK_WINDOW_XDISPLAY (root_window);
2107 sev.xclient.format = event->client.data_format;
2108 memcpy(&sev.xclient.data, &event->client.data, sizeof (sev.xclient.data));
2109 sev.xclient.message_type =
2110 gdk_x11_atom_to_xatom_for_display (GDK_WINDOW_DISPLAY (root_window),
2111 event->client.message_type);
2113 gdk_event_send_client_message_to_all_recurse (gdk_screen_get_display (screen),
2115 GDK_WINDOW_XID (root_window),
2120 *--------------------------------------------------------------
2123 * Flushes the Xlib output buffer and then waits
2124 * until all requests have been received and processed
2125 * by the X server. The only real use for this function
2126 * is in dealing with XShm.
2134 *--------------------------------------------------------------
2140 GSList *tmp_list = _gdk_displays;
2144 XSync (GDK_DISPLAY_XDISPLAY (tmp_list->data), False);
2145 tmp_list = tmp_list->next;
2150 timestamp_predicate (Display *display,
2154 Window xwindow = GPOINTER_TO_UINT (arg);
2155 GdkDisplay *gdk_display = gdk_x11_lookup_xdisplay (display);
2157 if (xevent->type == PropertyNotify &&
2158 xevent->xproperty.window == xwindow &&
2159 xevent->xproperty.atom == gdk_x11_get_xatom_by_name_for_display
2160 (gdk_display, "GDK_TIMESTAMP_PROP"))
2167 * gdk_x11_get_server_time:
2168 * @window: a #GdkWindow, used for communication with the server.
2169 * The window must have GDK_PROPERTY_CHANGE_MASK in its
2170 * events mask or a hang will result.
2172 * Routine to get the current X server time stamp.
2174 * Return value: the time stamp.
2177 gdk_x11_get_server_time (GdkWindow *window)
2183 Atom timestamp_prop_atom;
2185 g_return_val_if_fail (GDK_IS_WINDOW (window), 0);
2186 g_return_val_if_fail (!GDK_WINDOW_DESTROYED (window), 0);
2188 xdisplay = GDK_WINDOW_XDISPLAY (window);
2189 xwindow = GDK_WINDOW_XWINDOW (window);
2190 timestamp_prop_atom =
2191 gdk_x11_get_xatom_by_name_for_display (GDK_WINDOW_DISPLAY (window),
2192 "GDK_TIMESTAMP_PROP");
2194 XChangeProperty (xdisplay, xwindow, timestamp_prop_atom,
2195 timestamp_prop_atom,
2196 8, PropModeReplace, &c, 1);
2198 XIfEvent (xdisplay, &xevent,
2199 timestamp_predicate, GUINT_TO_POINTER(xwindow));
2201 return xevent.xproperty.time;
2205 fetch_net_wm_check_window (GdkScreen *screen)
2207 GdkScreenX11 *screen_x11;
2208 GdkDisplay *display;
2215 /* This function is very slow on every call if you are not running a
2216 * spec-supporting WM. For now not optimized, because it isn't in
2217 * any critical code paths, but if you used it somewhere that had to
2218 * be fast you want to avoid "GTK is slow with old WMs" complaints.
2219 * Probably at that point the function should be changed to query
2220 * _NET_SUPPORTING_WM_CHECK only once every 10 seconds or something.
2223 screen_x11 = GDK_SCREEN_X11 (screen);
2224 display = screen_x11->display;
2226 if (screen_x11->wmspec_check_window != None)
2227 return; /* already have it */
2229 XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), screen_x11->xroot_window,
2230 gdk_x11_get_xatom_by_name_for_display (display, "_NET_SUPPORTING_WM_CHECK"),
2231 0, G_MAXLONG, False, XA_WINDOW, &type, &format,
2232 &n_items, &bytes_after, (guchar **) & xwindow);
2234 if (type != XA_WINDOW)
2237 gdk_error_trap_push ();
2239 /* Find out if this WM goes away, so we can reset everything. */
2240 XSelectInput (screen_x11->xdisplay, *xwindow, StructureNotifyMask);
2242 screen_x11->wmspec_check_window = *xwindow;
2245 screen_x11->need_refetch_net_supported = TRUE;
2246 screen_x11->need_refetch_wm_name = TRUE;
2248 /* Careful, reentrancy */
2249 _gdk_x11_screen_window_manager_changed (GDK_SCREEN (screen_x11));
2253 * gdk_x11_screen_get_window_manager_name:
2254 * @screen: a #GdkScreen
2256 * Returns the name of the window manager for @screen.
2258 * Return value: the name of the window manager screen @screen, or
2259 * "unknown" if the window manager is unknown. The string is owned by GDK
2260 * and should not be freed.
2265 gdk_x11_screen_get_window_manager_name (GdkScreen *screen)
2267 GdkScreenX11 *screen_x11;
2269 screen_x11 = GDK_SCREEN_X11 (screen);
2271 fetch_net_wm_check_window (screen);
2273 if (screen_x11->need_refetch_wm_name)
2275 /* Get the name of the window manager */
2276 screen_x11->need_refetch_wm_name = FALSE;
2278 g_free (screen_x11->window_manager_name);
2279 screen_x11->window_manager_name = g_strdup ("unknown");
2281 if (screen_x11->wmspec_check_window != None)
2291 XGetWindowProperty (GDK_DISPLAY_XDISPLAY (screen_x11->display),
2292 screen_x11->wmspec_check_window,
2293 gdk_x11_get_xatom_by_name_for_display (screen_x11->display,
2295 0, G_MAXLONG, False,
2296 gdk_x11_get_xatom_by_name_for_display (screen_x11->display,
2299 &n_items, &bytes_after,
2302 gdk_display_sync (screen_x11->display);
2304 gdk_error_trap_pop ();
2308 g_free (screen_x11->window_manager_name);
2309 screen_x11->window_manager_name = g_strdup (name);
2315 return GDK_SCREEN_X11 (screen)->window_manager_name;
2318 typedef struct _NetWmSupportedAtoms NetWmSupportedAtoms;
2320 struct _NetWmSupportedAtoms
2327 * gdk_x11_screen_supports_net_wm_hint:
2328 * @screen: the relevant #GdkScreen.
2329 * @property: a property atom.
2331 * This function is specific to the X11 backend of GDK, and indicates
2332 * whether the window manager supports a certain hint from the
2333 * Extended Window Manager Hints Specification. You can find this
2335 * <ulink url="http://www.freedesktop.org">http://www.freedesktop.org</ulink>.
2337 * When using this function, keep in mind that the window manager
2338 * can change over time; so you shouldn't use this function in
2339 * a way that impacts persistent application state. A common bug
2340 * is that your application can start up before the window manager
2341 * does when the user logs in, and before the window manager starts
2342 * gdk_x11_screen_supports_net_wm_hint() will return %FALSE for every property.
2343 * You can monitor the window_manager_changed signal on #GdkScreen to detect
2344 * a window manager change.
2346 * Return value: %TRUE if the window manager supports @property
2351 gdk_x11_screen_supports_net_wm_hint (GdkScreen *screen,
2355 GdkScreenX11 *screen_x11;
2356 NetWmSupportedAtoms *supported_atoms;
2357 GdkDisplay *display;
2359 g_return_val_if_fail (GDK_IS_SCREEN (screen), FALSE);
2361 screen_x11 = GDK_SCREEN_X11 (screen);
2362 display = screen_x11->display;
2364 supported_atoms = g_object_get_data (G_OBJECT (screen), "gdk-net-wm-supported-atoms");
2365 if (!supported_atoms)
2367 supported_atoms = g_new0 (NetWmSupportedAtoms, 1);
2368 g_object_set_data (G_OBJECT (screen), "gdk-net-wm-supported-atoms", supported_atoms);
2371 fetch_net_wm_check_window (screen);
2373 if (screen_x11->wmspec_check_window == None)
2376 if (screen_x11->need_refetch_net_supported)
2378 /* WM has changed since we last got the supported list,
2385 screen_x11->need_refetch_net_supported = FALSE;
2387 if (supported_atoms->atoms)
2388 XFree (supported_atoms->atoms);
2390 supported_atoms->atoms = NULL;
2391 supported_atoms->n_atoms = 0;
2393 XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), screen_x11->xroot_window,
2394 gdk_x11_get_xatom_by_name_for_display (display, "_NET_SUPPORTED"),
2395 0, G_MAXLONG, False, XA_ATOM, &type, &format,
2396 &supported_atoms->n_atoms, &bytes_after,
2397 (guchar **)&supported_atoms->atoms);
2399 if (type != XA_ATOM)
2403 if (supported_atoms->atoms == NULL)
2407 while (i < supported_atoms->n_atoms)
2409 if (supported_atoms->atoms[i] == gdk_x11_atom_to_xatom_for_display (display, property))
2419 * gdk_net_wm_supports:
2420 * @property: a property atom.
2422 * This function is specific to the X11 backend of GDK, and indicates
2423 * whether the window manager for the default screen supports a certain
2424 * hint from the Extended Window Manager Hints Specification. See
2425 * gdk_x11_screen_supports_net_wm_hint() for complete details.
2427 * Return value: %TRUE if the window manager supports @property
2430 gdk_net_wm_supports (GdkAtom property)
2432 return gdk_x11_screen_supports_net_wm_hint (gdk_screen_get_default (), property);
2437 const char *xsettings_name;
2438 const char *gdk_name;
2439 } settings_map[] = {
2440 { "Net/DoubleClickTime", "gtk-double-click-time" },
2441 { "Net/DndDragThreshold", "gtk-dnd-drag-threshold" },
2442 { "Gtk/CanChangeAccels", "gtk-can-change-accels" },
2443 { "Gtk/ColorPalette", "gtk-color-palette" },
2444 { "Gtk/FontName", "gtk-font-name" },
2445 { "Gtk/IconSizes", "gtk-icon-sizes" },
2446 { "Gtk/KeyThemeName", "gtk-key-theme-name" },
2447 { "Gtk/ToolbarStyle", "gtk-toolbar-style" },
2448 { "Gtk/ToolbarIconSize", "gtk-toolbar-icon-size" },
2449 { "Gtk/IMPreeditStyle", "gtk-im-preedit-style" },
2450 { "Gtk/IMStatusStyle", "gtk-im-status-style" },
2451 { "Net/CursorBlink", "gtk-cursor-blink" },
2452 { "Net/CursorBlinkTime", "gtk-cursor-blink-time" },
2453 { "Net/ThemeName", "gtk-theme-name" }
2457 gdk_xsettings_notify_cb (const char *name,
2458 XSettingsAction action,
2459 XSettingsSetting *setting,
2463 GdkScreen *screen = data;
2464 GdkScreenX11 *screen_x11 = data;
2467 if (screen_x11->xsettings_in_init)
2470 new_event.type = GDK_SETTING;
2471 new_event.setting.window = gdk_screen_get_root_window (screen);
2472 new_event.setting.send_event = FALSE;
2473 new_event.setting.name = NULL;
2475 for (i = 0; i < G_N_ELEMENTS (settings_map) ; i++)
2476 if (strcmp (settings_map[i].xsettings_name, name) == 0)
2478 new_event.setting.name = (char *)settings_map[i].gdk_name;
2483 if (!new_event.setting.name)
2488 case XSETTINGS_ACTION_NEW:
2489 new_event.setting.action = GDK_SETTING_ACTION_NEW;
2491 case XSETTINGS_ACTION_CHANGED:
2492 new_event.setting.action = GDK_SETTING_ACTION_CHANGED;
2494 case XSETTINGS_ACTION_DELETED:
2495 new_event.setting.action = GDK_SETTING_ACTION_DELETED;
2499 gdk_event_put (&new_event);
2503 check_transform (const gchar *xsettings_name,
2507 if (!g_value_type_transformable (src_type, dest_type))
2509 g_warning ("Cannot tranform xsetting %s of type %s to type %s\n",
2511 g_type_name (src_type),
2512 g_type_name (dest_type));
2520 * gdk_screen_get_setting:
2521 * @screen: the #GdkScreen where the setting is located
2522 * @name: the name of the setting
2523 * @value: location to store the value of the setting
2525 * Retrieves a desktop-wide setting such as double-click time
2526 * for the #GdkScreen @screen.
2528 * FIXME needs a list of valid settings here, or a link to
2531 * Returns: %TRUE if the setting existed and a value was stored
2532 * in @value, %FALSE otherwise.
2537 gdk_screen_get_setting (GdkScreen *screen,
2542 const char *xsettings_name = NULL;
2543 XSettingsResult result;
2544 XSettingsSetting *setting;
2545 GdkScreenX11 *screen_x11;
2546 gboolean success = FALSE;
2548 GValue tmp_val = { 0, };
2550 g_return_val_if_fail (GDK_IS_SCREEN (screen), FALSE);
2552 screen_x11 = GDK_SCREEN_X11 (screen);
2554 for (i = 0; i < G_N_ELEMENTS (settings_map) ; i++)
2555 if (strcmp (settings_map[i].gdk_name, name) == 0)
2557 xsettings_name = settings_map[i].xsettings_name;
2561 if (!xsettings_name)
2564 result = xsettings_client_get_setting (screen_x11->xsettings_client,
2565 xsettings_name, &setting);
2566 if (result != XSETTINGS_SUCCESS)
2569 switch (setting->type)
2571 case XSETTINGS_TYPE_INT:
2572 if (check_transform (xsettings_name, G_TYPE_INT, G_VALUE_TYPE (value)))
2574 g_value_init (&tmp_val, G_TYPE_INT);
2575 g_value_set_int (&tmp_val, setting->data.v_int);
2576 g_value_transform (&tmp_val, value);
2581 case XSETTINGS_TYPE_STRING:
2582 if (check_transform (xsettings_name, G_TYPE_STRING, G_VALUE_TYPE (value)))
2584 g_value_init (&tmp_val, G_TYPE_STRING);
2585 g_value_set_string (&tmp_val, setting->data.v_string);
2586 g_value_transform (&tmp_val, value);
2591 case XSETTINGS_TYPE_COLOR:
2592 if (!check_transform (xsettings_name, GDK_TYPE_COLOR, G_VALUE_TYPE (value)))
2596 g_value_init (&tmp_val, GDK_TYPE_COLOR);
2599 color.red = setting->data.v_color.red;
2600 color.green = setting->data.v_color.green;
2601 color.blue = setting->data.v_color.blue;
2603 g_value_set_boxed (&tmp_val, &color);
2605 g_value_transform (&tmp_val, value);
2612 g_value_unset (&tmp_val);
2614 xsettings_setting_free (setting);
2619 static GdkFilterReturn
2620 gdk_xsettings_client_event_filter (GdkXEvent *xevent,
2624 GdkScreenX11 *screen = data;
2626 if (xsettings_client_process_event (screen->xsettings_client, (XEvent *)xevent))
2627 return GDK_FILTER_REMOVE;
2629 return GDK_FILTER_CONTINUE;
2633 gdk_xsettings_watch_cb (Window window,
2639 GdkScreen *screen = cb_data;
2641 gdkwin = gdk_window_lookup_for_display (gdk_screen_get_display (screen), window);
2646 gdkwin = gdk_window_foreign_new_for_display (gdk_screen_get_display (screen), window);
2648 g_object_ref (gdkwin);
2650 gdk_window_add_filter (gdkwin, gdk_xsettings_client_event_filter, screen);
2655 gdk_window_remove_filter (gdkwin, gdk_xsettings_client_event_filter, screen);
2656 g_object_unref (gdkwin);