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 */
616 /* Return the window this has to do with, if any, rather
617 * than the frame or root window that was selecting
621 get_real_window (XEvent *event)
626 return event->xcreatewindow.window;
629 return event->xdestroywindow.window;
632 return event->xunmap.window;
635 return event->xmap.window;
638 return event->xmaprequest.window;
641 return event->xreparent.window;
643 case ConfigureNotify:
644 return event->xconfigure.window;
646 case ConfigureRequest:
647 return event->xconfigurerequest.window;
650 return event->xgravity.window;
652 case CirculateNotify:
653 return event->xcirculate.window;
655 case CirculateRequest:
656 return event->xcirculaterequest.window;
659 return event->xany.window;
663 #ifdef G_ENABLE_DEBUG
664 static const char notify_modes[][18] = {
671 static const char notify_details[][22] = {
676 "NotifyNonlinearVirtual",
684 gdk_event_translate (GdkDisplay *display,
687 gboolean return_exposes)
691 GdkWindowObject *window_private;
692 GdkWindow *filter_window;
693 GdkWindowImplX11 *window_impl = NULL;
695 gint xoffset, yoffset;
696 GdkScreen *screen = NULL;
697 GdkScreenX11 *screen_x11 = NULL;
698 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
703 /* init these, since the done: block uses them */
705 window_private = NULL;
706 event->any.window = NULL;
708 if (_gdk_default_filters)
710 /* Apply global filters */
711 GdkFilterReturn result;
712 result = gdk_event_apply_filters (xevent, event,
713 _gdk_default_filters);
715 if (result != GDK_FILTER_CONTINUE)
717 return_val = (result == GDK_FILTER_TRANSLATE) ? TRUE : FALSE;
722 /* We handle events with window=None
723 * specially - they are generated by XFree86's XInput under
724 * some circumstances. This handling for obvious reasons
725 * goes before we bother to lookup the event window.
728 if (xevent->xany.window == None)
730 return_val = _gdk_input_window_none_event (event, xevent);
732 if (return_val >= 0) /* was handled */
738 /* Find the GdkWindow that this event relates to.
739 * Basically this means substructure events
740 * are reported same as structure events
742 xwindow = get_real_window (xevent);
744 window = gdk_window_lookup_for_display (display, xwindow);
745 window_private = (GdkWindowObject *) window;
747 /* We always run the filters for the window where the event
748 * is delivered, not the window that it relates to
750 if (xevent->xany.window == xwindow)
751 filter_window = window;
753 filter_window = gdk_window_lookup_for_display (display, xevent->xany.window);
757 screen = GDK_WINDOW_SCREEN (window);
758 screen_x11 = GDK_SCREEN_X11 (screen);
763 /* Window may be a pixmap, so check its type.
764 * (This check is probably too expensive unless
765 * GLib short-circuits an exact type match,
766 * which has been proposed)
768 if (GDK_IS_WINDOW (window))
770 window_impl = GDK_WINDOW_IMPL_X11 (window_private->impl);
772 /* Move key events on focus window to the real toplevel, and
773 * filter out all other events on focus window
775 if (xwindow == window_impl->focus_window)
777 switch (xevent->type)
781 xwindow = GDK_WINDOW_XID (window);
782 xevent->xany.window = xwindow;
790 g_object_ref (window);
793 event->any.window = window;
794 event->any.send_event = xevent->xany.send_event ? TRUE : FALSE;
796 if (window_private && GDK_WINDOW_DESTROYED (window))
798 if (xevent->type != DestroyNotify)
804 else if (filter_window)
806 /* Apply per-window filters */
807 GdkWindowObject *filter_private = (GdkWindowObject *) filter_window;
808 GdkFilterReturn result;
810 if (filter_private->filters)
812 g_object_ref (filter_window);
814 result = gdk_event_apply_filters (xevent, event,
815 filter_private->filters);
817 g_object_unref (filter_window);
819 if (result != GDK_FILTER_CONTINUE)
821 return_val = (result == GDK_FILTER_TRANSLATE) ? TRUE : FALSE;
827 if (screen_x11 && screen_x11->wmspec_check_window != None &&
828 xwindow == screen_x11->wmspec_check_window)
830 if (xevent->type == DestroyNotify)
832 screen_x11->wmspec_check_window = None;
833 g_free (screen_x11->window_manager_name);
834 screen_x11->window_manager_name = g_strdup ("unknown");
836 /* careful, reentrancy */
837 _gdk_x11_screen_window_manager_changed (GDK_SCREEN (screen_x11));
840 /* Eat events on this window unless someone had wrapped
841 * it as a foreign window
851 (xevent->xany.type == MotionNotify ||
852 xevent->xany.type == ButtonRelease))
854 if (_gdk_moveresize_handle_event (xevent))
861 /* We do a "manual" conversion of the XEvent to a
862 * GdkEvent. The structures are mostly the same so
863 * the conversion is fairly straightforward. We also
864 * optionally print debugging info regarding events
872 _gdk_windowing_window_get_offsets (window, &xoffset, &yoffset);
880 switch (xevent->type)
883 if (window_private == NULL)
888 translate_key_event (display, event, xevent);
892 if (window_private == NULL)
898 /* Emulate detectable auto-repeat by checking to see
899 * if the next event is a key press with the same
900 * keycode and timestamp, and if so, ignoring the event.
903 if (!display_x11->have_xkb_autorepeat && XPending (xevent->xkey.display))
907 XPeekEvent (xevent->xkey.display, &next_event);
909 if (next_event.type == KeyPress &&
910 next_event.xkey.keycode == xevent->xkey.keycode &&
911 next_event.xkey.time == xevent->xkey.time)
918 translate_key_event (display, event, xevent);
923 g_message ("button press:\t\twindow: %ld x,y: %d %d button: %d",
924 xevent->xbutton.window,
925 xevent->xbutton.x, xevent->xbutton.y,
926 xevent->xbutton.button));
928 if (window_private == NULL ||
929 ((window_private->extension_events != 0) &&
930 display_x11->input_ignore_core))
936 /* If we get a ButtonPress event where the button is 4 or 5,
937 it's a Scroll event */
938 switch (xevent->xbutton.button)
944 event->scroll.type = GDK_SCROLL;
946 if (xevent->xbutton.button == 4)
947 event->scroll.direction = GDK_SCROLL_UP;
948 else if (xevent->xbutton.button == 5)
949 event->scroll.direction = GDK_SCROLL_DOWN;
950 else if (xevent->xbutton.button == 6)
951 event->scroll.direction = GDK_SCROLL_LEFT;
953 event->scroll.direction = GDK_SCROLL_RIGHT;
955 event->scroll.window = window;
956 event->scroll.time = xevent->xbutton.time;
957 event->scroll.x = xevent->xbutton.x + xoffset;
958 event->scroll.y = xevent->xbutton.y + yoffset;
959 event->scroll.x_root = (gfloat)xevent->xbutton.x_root;
960 event->scroll.y_root = (gfloat)xevent->xbutton.y_root;
961 event->scroll.state = (GdkModifierType) xevent->xbutton.state;
962 event->scroll.device = display->core_pointer;
964 set_screen_from_root (display, event, xevent->xbutton.root);
969 event->button.type = GDK_BUTTON_PRESS;
970 event->button.window = window;
971 event->button.time = xevent->xbutton.time;
972 event->button.x = xevent->xbutton.x + xoffset;
973 event->button.y = xevent->xbutton.y + yoffset;
974 event->button.x_root = (gfloat)xevent->xbutton.x_root;
975 event->button.y_root = (gfloat)xevent->xbutton.y_root;
976 event->button.axes = NULL;
977 event->button.state = (GdkModifierType) xevent->xbutton.state;
978 event->button.button = xevent->xbutton.button;
979 event->button.device = display->core_pointer;
981 set_screen_from_root (display, event, xevent->xbutton.root);
983 _gdk_event_button_generate (display, event);
991 g_message ("button release:\twindow: %ld x,y: %d %d button: %d",
992 xevent->xbutton.window,
993 xevent->xbutton.x, xevent->xbutton.y,
994 xevent->xbutton.button));
996 if (window_private == NULL ||
997 ((window_private->extension_events != 0) &&
998 display_x11->input_ignore_core))
1004 /* We treat button presses as scroll wheel events, so ignore the release */
1005 if (xevent->xbutton.button == 4 || xevent->xbutton.button == 5 ||
1006 xevent->xbutton.button == 6 || xevent->xbutton.button ==7)
1012 event->button.type = GDK_BUTTON_RELEASE;
1013 event->button.window = window;
1014 event->button.time = xevent->xbutton.time;
1015 event->button.x = xevent->xbutton.x + xoffset;
1016 event->button.y = xevent->xbutton.y + yoffset;
1017 event->button.x_root = (gfloat)xevent->xbutton.x_root;
1018 event->button.y_root = (gfloat)xevent->xbutton.y_root;
1019 event->button.axes = NULL;
1020 event->button.state = (GdkModifierType) xevent->xbutton.state;
1021 event->button.button = xevent->xbutton.button;
1022 event->button.device = display->core_pointer;
1024 set_screen_from_root (display, event, xevent->xbutton.root);
1030 g_message ("motion notify:\t\twindow: %ld x,y: %d %d hint: %s",
1031 xevent->xmotion.window,
1032 xevent->xmotion.x, xevent->xmotion.y,
1033 (xevent->xmotion.is_hint) ? "true" : "false"));
1035 if (window_private == NULL ||
1036 ((window_private->extension_events != 0) &&
1037 display_x11->input_ignore_core))
1043 event->motion.type = GDK_MOTION_NOTIFY;
1044 event->motion.window = window;
1045 event->motion.time = xevent->xmotion.time;
1046 event->motion.x = xevent->xmotion.x + xoffset;
1047 event->motion.y = xevent->xmotion.y + yoffset;
1048 event->motion.x_root = (gfloat)xevent->xmotion.x_root;
1049 event->motion.y_root = (gfloat)xevent->xmotion.y_root;
1050 event->motion.axes = NULL;
1051 event->motion.state = (GdkModifierType) xevent->xmotion.state;
1052 event->motion.is_hint = xevent->xmotion.is_hint;
1053 event->motion.device = display->core_pointer;
1055 set_screen_from_root (display, event, xevent->xmotion.root);
1061 g_message ("enter notify:\t\twindow: %ld detail: %d subwin: %ld",
1062 xevent->xcrossing.window,
1063 xevent->xcrossing.detail,
1064 xevent->xcrossing.subwindow));
1066 if (window_private == NULL)
1072 /* Handle focusing (in the case where no window manager is running */
1074 GDK_WINDOW_TYPE (window) != GDK_WINDOW_CHILD &&
1075 xevent->xcrossing.detail != NotifyInferior &&
1076 xevent->xcrossing.focus && !window_impl->has_focus_window)
1078 gboolean had_focus = HAS_FOCUS (window_impl);
1080 window_impl->has_pointer_focus = TRUE;
1082 if (HAS_FOCUS (window_impl) != had_focus)
1083 generate_focus_event (window, TRUE);
1086 /* Tell XInput stuff about it if appropriate */
1087 if (window_private &&
1088 !GDK_WINDOW_DESTROYED (window) &&
1089 window_private->extension_events != 0)
1090 _gdk_input_enter_event (&xevent->xcrossing, window);
1092 event->crossing.type = GDK_ENTER_NOTIFY;
1093 event->crossing.window = window;
1095 /* If the subwindow field of the XEvent is non-NULL, then
1096 * lookup the corresponding GdkWindow.
1098 if (xevent->xcrossing.subwindow != None)
1099 event->crossing.subwindow = gdk_window_lookup_for_display (display, xevent->xcrossing.subwindow);
1101 event->crossing.subwindow = NULL;
1103 event->crossing.time = xevent->xcrossing.time;
1104 event->crossing.x = xevent->xcrossing.x + xoffset;
1105 event->crossing.y = xevent->xcrossing.y + yoffset;
1106 event->crossing.x_root = xevent->xcrossing.x_root;
1107 event->crossing.y_root = xevent->xcrossing.y_root;
1109 set_screen_from_root (display, event, xevent->xcrossing.root);
1111 /* Translate the crossing mode into Gdk terms.
1113 switch (xevent->xcrossing.mode)
1116 event->crossing.mode = GDK_CROSSING_NORMAL;
1119 event->crossing.mode = GDK_CROSSING_GRAB;
1122 event->crossing.mode = GDK_CROSSING_UNGRAB;
1126 /* Translate the crossing detail into Gdk terms.
1128 switch (xevent->xcrossing.detail)
1130 case NotifyInferior:
1131 event->crossing.detail = GDK_NOTIFY_INFERIOR;
1133 case NotifyAncestor:
1134 event->crossing.detail = GDK_NOTIFY_ANCESTOR;
1137 event->crossing.detail = GDK_NOTIFY_VIRTUAL;
1139 case NotifyNonlinear:
1140 event->crossing.detail = GDK_NOTIFY_NONLINEAR;
1142 case NotifyNonlinearVirtual:
1143 event->crossing.detail = GDK_NOTIFY_NONLINEAR_VIRTUAL;
1146 event->crossing.detail = GDK_NOTIFY_UNKNOWN;
1150 event->crossing.focus = xevent->xcrossing.focus;
1151 event->crossing.state = xevent->xcrossing.state;
1157 g_message ("leave notify:\t\twindow: %ld detail: %d subwin: %ld",
1158 xevent->xcrossing.window,
1159 xevent->xcrossing.detail, xevent->xcrossing.subwindow));
1161 if (window_private == NULL)
1167 /* Handle focusing (in the case where no window manager is running */
1169 GDK_WINDOW_TYPE (window) != GDK_WINDOW_CHILD &&
1170 xevent->xcrossing.detail != NotifyInferior &&
1171 xevent->xcrossing.focus && !window_impl->has_focus_window)
1173 gboolean had_focus = HAS_FOCUS (window_impl);
1175 window_impl->has_pointer_focus = FALSE;
1177 if (HAS_FOCUS (window_impl) != had_focus)
1178 generate_focus_event (window, FALSE);
1181 event->crossing.type = GDK_LEAVE_NOTIFY;
1182 event->crossing.window = window;
1184 /* If the subwindow field of the XEvent is non-NULL, then
1185 * lookup the corresponding GdkWindow.
1187 if (xevent->xcrossing.subwindow != None)
1188 event->crossing.subwindow = gdk_window_lookup_for_display (display, xevent->xcrossing.subwindow);
1190 event->crossing.subwindow = NULL;
1192 event->crossing.time = xevent->xcrossing.time;
1193 event->crossing.x = xevent->xcrossing.x + xoffset;
1194 event->crossing.y = xevent->xcrossing.y + yoffset;
1195 event->crossing.x_root = xevent->xcrossing.x_root;
1196 event->crossing.y_root = xevent->xcrossing.y_root;
1198 set_screen_from_root (display, event, xevent->xcrossing.root);
1200 /* Translate the crossing mode into Gdk terms.
1202 switch (xevent->xcrossing.mode)
1205 event->crossing.mode = GDK_CROSSING_NORMAL;
1208 event->crossing.mode = GDK_CROSSING_GRAB;
1211 event->crossing.mode = GDK_CROSSING_UNGRAB;
1215 /* Translate the crossing detail into Gdk terms.
1217 switch (xevent->xcrossing.detail)
1219 case NotifyInferior:
1220 event->crossing.detail = GDK_NOTIFY_INFERIOR;
1222 case NotifyAncestor:
1223 event->crossing.detail = GDK_NOTIFY_ANCESTOR;
1226 event->crossing.detail = GDK_NOTIFY_VIRTUAL;
1228 case NotifyNonlinear:
1229 event->crossing.detail = GDK_NOTIFY_NONLINEAR;
1231 case NotifyNonlinearVirtual:
1232 event->crossing.detail = GDK_NOTIFY_NONLINEAR_VIRTUAL;
1235 event->crossing.detail = GDK_NOTIFY_UNKNOWN;
1239 event->crossing.focus = xevent->xcrossing.focus;
1240 event->crossing.state = xevent->xcrossing.state;
1244 /* We only care about focus events that indicate that _this_
1245 * window (not a ancestor or child) got or lost the focus
1249 g_message ("focus in:\t\twindow: %ld, detail: %s, mode: %s",
1250 xevent->xfocus.window,
1251 notify_details[xevent->xfocus.detail],
1252 notify_modes[xevent->xfocus.mode]));
1254 if (window && GDK_WINDOW_TYPE (window) != GDK_WINDOW_CHILD)
1256 gboolean had_focus = HAS_FOCUS (window_impl);
1258 switch (xevent->xfocus.detail)
1260 case NotifyAncestor:
1261 case NotifyNonlinear:
1263 case NotifyNonlinearVirtual:
1264 window_impl->has_focus_window = TRUE;
1265 /* We pretend that the focus moves to the grab
1266 * window, so we pay attention to NotifyGrab
1267 * NotifyUngrab, and ignore NotifyWhileGrabbed
1269 if (xevent->xfocus.mode != NotifyWhileGrabbed)
1270 window_impl->has_focus = TRUE;
1273 /* The X server sends NotifyPointer/NotifyGrab,
1274 * but the pointer focus is ignored while a
1277 if (xevent->xfocus.mode != NotifyGrab)
1278 window_impl->has_pointer_focus = TRUE;
1280 case NotifyInferior:
1281 case NotifyPointerRoot:
1282 case NotifyDetailNone:
1286 if (HAS_FOCUS (window_impl) != had_focus)
1287 generate_focus_event (window, TRUE);
1292 g_message ("focus out:\t\twindow: %ld, detail: %s, mode: %s",
1293 xevent->xfocus.window,
1294 notify_details[xevent->xfocus.detail],
1295 notify_modes[xevent->xfocus.mode]));
1297 if (window && GDK_WINDOW_TYPE (window) != GDK_WINDOW_CHILD)
1299 gboolean had_focus = HAS_FOCUS (window_impl);
1301 switch (xevent->xfocus.detail)
1303 case NotifyAncestor:
1304 case NotifyNonlinear:
1306 case NotifyNonlinearVirtual:
1307 window_impl->has_focus_window = FALSE;
1308 if (xevent->xfocus.mode != NotifyWhileGrabbed)
1309 window_impl->has_focus = FALSE;
1312 if (xevent->xfocus.mode != NotifyUngrab)
1313 window_impl->has_pointer_focus = FALSE;
1315 case NotifyInferior:
1316 case NotifyPointerRoot:
1317 case NotifyDetailNone:
1321 if (HAS_FOCUS (window_impl) != had_focus)
1322 generate_focus_event (window, FALSE);
1327 /* gdk_keyboard_grab() causes following events. These events confuse
1328 * the XIM focus, so ignore them.
1330 if (xevent->xfocus.mode == NotifyGrab ||
1331 xevent->xfocus.mode == NotifyUngrab)
1337 g_message ("keymap notify"));
1339 /* Not currently handled */
1345 g_message ("expose:\t\twindow: %ld %d x,y: %d %d w,h: %d %d%s",
1346 xevent->xexpose.window, xevent->xexpose.count,
1347 xevent->xexpose.x, xevent->xexpose.y,
1348 xevent->xexpose.width, xevent->xexpose.height,
1349 event->any.send_event ? " (send)" : ""));
1351 if (window_private == NULL)
1358 GdkRectangle expose_rect;
1360 expose_rect.x = xevent->xexpose.x + xoffset;
1361 expose_rect.y = xevent->xexpose.y + yoffset;
1362 expose_rect.width = xevent->xexpose.width;
1363 expose_rect.height = xevent->xexpose.height;
1367 event->expose.type = GDK_EXPOSE;
1368 event->expose.area = expose_rect;
1369 event->expose.region = gdk_region_rectangle (&expose_rect);
1370 event->expose.window = window;
1371 event->expose.count = xevent->xexpose.count;
1377 _gdk_window_process_expose (window, xevent->xexpose.serial, &expose_rect);
1386 case GraphicsExpose:
1388 GdkRectangle expose_rect;
1391 g_message ("graphics expose:\tdrawable: %ld",
1392 xevent->xgraphicsexpose.drawable));
1394 if (window_private == NULL)
1400 expose_rect.x = xevent->xgraphicsexpose.x + xoffset;
1401 expose_rect.y = xevent->xgraphicsexpose.y + yoffset;
1402 expose_rect.width = xevent->xgraphicsexpose.width;
1403 expose_rect.height = xevent->xgraphicsexpose.height;
1407 event->expose.type = GDK_EXPOSE;
1408 event->expose.area = expose_rect;
1409 event->expose.region = gdk_region_rectangle (&expose_rect);
1410 event->expose.window = window;
1411 event->expose.count = xevent->xgraphicsexpose.count;
1417 _gdk_window_process_expose (window, xevent->xgraphicsexpose.serial, &expose_rect);
1427 g_message ("no expose:\t\tdrawable: %ld",
1428 xevent->xnoexpose.drawable));
1430 event->no_expose.type = GDK_NO_EXPOSE;
1431 event->no_expose.window = window;
1435 case VisibilityNotify:
1436 #ifdef G_ENABLE_DEBUG
1437 if (_gdk_debug_flags & GDK_DEBUG_EVENTS)
1438 switch (xevent->xvisibility.state)
1440 case VisibilityFullyObscured:
1441 g_message ("visibility notify:\twindow: %ld none",
1442 xevent->xvisibility.window);
1444 case VisibilityPartiallyObscured:
1445 g_message ("visibility notify:\twindow: %ld partial",
1446 xevent->xvisibility.window);
1448 case VisibilityUnobscured:
1449 g_message ("visibility notify:\twindow: %ld full",
1450 xevent->xvisibility.window);
1453 #endif /* G_ENABLE_DEBUG */
1455 if (window_private == NULL)
1461 event->visibility.type = GDK_VISIBILITY_NOTIFY;
1462 event->visibility.window = window;
1464 switch (xevent->xvisibility.state)
1466 case VisibilityFullyObscured:
1467 event->visibility.state = GDK_VISIBILITY_FULLY_OBSCURED;
1470 case VisibilityPartiallyObscured:
1471 event->visibility.state = GDK_VISIBILITY_PARTIAL;
1474 case VisibilityUnobscured:
1475 event->visibility.state = GDK_VISIBILITY_UNOBSCURED;
1483 g_message ("create notify:\twindow: %ld x,y: %d %d w,h: %d %d b-w: %d parent: %ld ovr: %d",
1484 xevent->xcreatewindow.window,
1485 xevent->xcreatewindow.x,
1486 xevent->xcreatewindow.y,
1487 xevent->xcreatewindow.width,
1488 xevent->xcreatewindow.height,
1489 xevent->xcreatewindow.border_width,
1490 xevent->xcreatewindow.parent,
1491 xevent->xcreatewindow.override_redirect));
1492 /* not really handled */
1497 g_message ("destroy notify:\twindow: %ld",
1498 xevent->xdestroywindow.window));
1500 /* Ignore DestroyNotify from SubstructureNotifyMask */
1501 if (xevent->xdestroywindow.window == xevent->xdestroywindow.event)
1503 event->any.type = GDK_DESTROY;
1504 event->any.window = window;
1506 return_val = window_private && !GDK_WINDOW_DESTROYED (window);
1508 if (window && GDK_WINDOW_XID (window) != screen_x11->xroot_window)
1509 gdk_window_destroy_notify (window);
1518 g_message ("unmap notify:\t\twindow: %ld",
1519 xevent->xmap.window));
1521 event->any.type = GDK_UNMAP;
1522 event->any.window = window;
1524 /* If we are shown (not withdrawn) and get an unmap, it means we
1525 * were iconified in the X sense. If we are withdrawn, and get
1526 * an unmap, it means we hid the window ourselves, so we
1527 * will have already flipped the iconified bit off.
1531 if (GDK_WINDOW_IS_MAPPED (window))
1532 gdk_synthesize_window_state (window,
1534 GDK_WINDOW_STATE_ICONIFIED);
1536 _gdk_xgrab_check_unmap (window, xevent->xany.serial);
1543 g_message ("map notify:\t\twindow: %ld",
1544 xevent->xmap.window));
1546 event->any.type = GDK_MAP;
1547 event->any.window = window;
1549 /* Unset iconified if it was set */
1550 if (window && (((GdkWindowObject*)window)->state & GDK_WINDOW_STATE_ICONIFIED))
1551 gdk_synthesize_window_state (window,
1552 GDK_WINDOW_STATE_ICONIFIED,
1557 case ReparentNotify:
1559 g_message ("reparent notify:\twindow: %ld x,y: %d %d parent: %ld ovr: %d",
1560 xevent->xreparent.window,
1561 xevent->xreparent.x,
1562 xevent->xreparent.y,
1563 xevent->xreparent.parent,
1564 xevent->xreparent.override_redirect));
1566 /* Not currently handled */
1570 case ConfigureNotify:
1572 g_message ("configure notify:\twindow: %ld x,y: %d %d w,h: %d %d b-w: %d above: %ld ovr: %d%s",
1573 xevent->xconfigure.window,
1574 xevent->xconfigure.x,
1575 xevent->xconfigure.y,
1576 xevent->xconfigure.width,
1577 xevent->xconfigure.height,
1578 xevent->xconfigure.border_width,
1579 xevent->xconfigure.above,
1580 xevent->xconfigure.override_redirect,
1583 : GDK_WINDOW_TYPE (window) == GDK_WINDOW_CHILD
1584 ? " (discarding child)"
1585 : xevent->xconfigure.event != xevent->xconfigure.window
1586 ? " (discarding substructure)"
1588 if (window && GDK_WINDOW_TYPE (window) == GDK_WINDOW_ROOT)
1589 _gdk_x11_screen_size_changed (screen, xevent);
1592 xevent->xconfigure.event == xevent->xconfigure.window &&
1593 !GDK_WINDOW_DESTROYED (window) &&
1594 (window_private->extension_events != 0))
1595 _gdk_input_configure_event (&xevent->xconfigure, window);
1598 xevent->xconfigure.event != xevent->xconfigure.window ||
1599 GDK_WINDOW_TYPE (window) == GDK_WINDOW_CHILD ||
1600 GDK_WINDOW_TYPE (window) == GDK_WINDOW_ROOT)
1604 event->configure.type = GDK_CONFIGURE;
1605 event->configure.window = window;
1606 event->configure.width = xevent->xconfigure.width;
1607 event->configure.height = xevent->xconfigure.height;
1609 if (!xevent->xconfigure.send_event &&
1610 !GDK_WINDOW_DESTROYED (window))
1614 Window child_window = 0;
1616 gdk_error_trap_push ();
1617 if (XTranslateCoordinates (GDK_DRAWABLE_XDISPLAY (window),
1618 GDK_DRAWABLE_XID (window),
1619 screen_x11->xroot_window,
1624 if (!gdk_error_trap_pop ())
1626 event->configure.x = tx;
1627 event->configure.y = ty;
1631 gdk_error_trap_pop ();
1635 event->configure.x = xevent->xconfigure.x;
1636 event->configure.y = xevent->xconfigure.y;
1638 window_private->x = event->configure.x;
1639 window_private->y = event->configure.y;
1640 GDK_WINDOW_IMPL_X11 (window_private->impl)->width = xevent->xconfigure.width;
1641 GDK_WINDOW_IMPL_X11 (window_private->impl)->height = xevent->xconfigure.height;
1642 if (window_private->resize_count >= 1)
1644 window_private->resize_count -= 1;
1646 if (window_private->resize_count == 0)
1647 _gdk_moveresize_configure_done (display, window);
1652 case PropertyNotify:
1654 g_message ("property notify:\twindow: %ld, atom(%ld): %s%s%s",
1655 xevent->xproperty.window,
1656 xevent->xproperty.atom,
1658 gdk_x11_get_xatom_name_for_display (display, xevent->xproperty.atom),
1661 if (window_private == NULL)
1667 if (xevent->xproperty.atom == gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_STATE") ||
1668 xevent->xproperty.atom == gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_DESKTOP"))
1670 /* If window state changed, then synthesize those events. */
1671 gdk_check_wm_state_changed (window);
1674 if (window_private->event_mask & GDK_PROPERTY_CHANGE_MASK)
1676 event->property.type = GDK_PROPERTY_NOTIFY;
1677 event->property.window = window;
1678 event->property.atom = gdk_x11_xatom_to_atom_for_display (display, xevent->xproperty.atom);
1679 event->property.time = xevent->xproperty.time;
1680 event->property.state = xevent->xproperty.state;
1687 case SelectionClear:
1689 g_message ("selection clear:\twindow: %ld",
1690 xevent->xproperty.window));
1692 if (_gdk_selection_filter_clear_event (&xevent->xselectionclear))
1694 event->selection.type = GDK_SELECTION_CLEAR;
1695 event->selection.window = window;
1696 event->selection.selection = gdk_x11_xatom_to_atom_for_display (display, xevent->xselectionclear.selection);
1697 event->selection.time = xevent->xselectionclear.time;
1704 case SelectionRequest:
1706 g_message ("selection request:\twindow: %ld",
1707 xevent->xproperty.window));
1709 event->selection.type = GDK_SELECTION_REQUEST;
1710 event->selection.window = window;
1711 event->selection.selection = gdk_x11_xatom_to_atom_for_display (display, xevent->xselectionrequest.selection);
1712 event->selection.target = gdk_x11_xatom_to_atom_for_display (display, xevent->xselectionrequest.target);
1713 event->selection.property = gdk_x11_xatom_to_atom_for_display (display, xevent->xselectionrequest.property);
1714 event->selection.requestor = xevent->xselectionrequest.requestor;
1715 event->selection.time = xevent->xselectionrequest.time;
1719 case SelectionNotify:
1721 g_message ("selection notify:\twindow: %ld",
1722 xevent->xproperty.window));
1725 event->selection.type = GDK_SELECTION_NOTIFY;
1726 event->selection.window = window;
1727 event->selection.selection = gdk_x11_xatom_to_atom_for_display (display, xevent->xselection.selection);
1728 event->selection.target = gdk_x11_xatom_to_atom_for_display (display, xevent->xselection.target);
1729 event->selection.property = gdk_x11_xatom_to_atom_for_display (display, xevent->xselection.property);
1730 event->selection.time = xevent->xselection.time;
1734 case ColormapNotify:
1736 g_message ("colormap notify:\twindow: %ld",
1737 xevent->xcolormap.window));
1739 /* Not currently handled */
1746 GdkFilterReturn result = GDK_FILTER_CONTINUE;
1747 GdkAtom message_type = gdk_x11_xatom_to_atom_for_display (display, xevent->xclient.message_type);
1750 g_message ("client message:\twindow: %ld",
1751 xevent->xclient.window));
1753 tmp_list = display_x11->client_filters;
1756 GdkClientFilter *filter = tmp_list->data;
1757 if (filter->type == message_type)
1759 result = (*filter->function) (xevent, event, filter->data);
1763 tmp_list = tmp_list->next;
1768 case GDK_FILTER_REMOVE:
1771 case GDK_FILTER_TRANSLATE:
1774 case GDK_FILTER_CONTINUE:
1775 /* Send unknown ClientMessage's on to Gtk for it to use */
1776 if (window_private == NULL)
1782 event->client.type = GDK_CLIENT_EVENT;
1783 event->client.window = window;
1784 event->client.message_type = message_type;
1785 event->client.data_format = xevent->xclient.format;
1786 memcpy(&event->client.data, &xevent->xclient.data,
1787 sizeof(event->client.data));
1797 g_message ("mapping notify"));
1799 /* Let XLib know that there is a new keyboard mapping.
1801 XRefreshKeyboardMapping (&xevent->xmapping);
1802 _gdk_keymap_keys_changed (display);
1808 if (xevent->type == display_x11->xkb_event_type)
1810 XkbEvent *xkb_event = (XkbEvent *)xevent;
1812 switch (xkb_event->any.xkb_type)
1815 _gdk_keymap_keys_changed (display);
1820 case XkbStateNotify:
1821 _gdk_keymap_state_changed (display);
1828 /* something else - (e.g., a Xinput event) */
1830 if (window_private &&
1831 !GDK_WINDOW_DESTROYED (window_private) &&
1832 (window_private->extension_events != 0))
1833 return_val = _gdk_input_other_event(event, xevent, window);
1844 if (event->any.window)
1845 g_object_ref (event->any.window);
1846 if (((event->any.type == GDK_ENTER_NOTIFY) ||
1847 (event->any.type == GDK_LEAVE_NOTIFY)) &&
1848 (event->crossing.subwindow != NULL))
1849 g_object_ref (event->crossing.subwindow);
1853 /* Mark this event as having no resources to be freed */
1854 event->any.window = NULL;
1855 event->any.type = GDK_NOTHING;
1859 g_object_unref (window);
1864 static GdkFilterReturn
1865 gdk_wm_protocols_filter (GdkXEvent *xev,
1869 XEvent *xevent = (XEvent *)xev;
1870 GdkWindow *win = event->any.window;
1871 GdkDisplay *display = GDK_WINDOW_DISPLAY (win);
1873 if ((Atom) xevent->xclient.data.l[0] == gdk_x11_get_xatom_by_name_for_display (display, "WM_DELETE_WINDOW"))
1875 /* The delete window request specifies a window
1876 * to delete. We don't actually destroy the
1877 * window because "it is only a request". (The
1878 * window might contain vital data that the
1879 * program does not want destroyed). Instead
1880 * the event is passed along to the program,
1881 * which should then destroy the window.
1884 g_message ("delete window:\t\twindow: %ld",
1885 xevent->xclient.window));
1887 event->any.type = GDK_DELETE;
1889 return GDK_FILTER_TRANSLATE;
1891 else if ((Atom) xevent->xclient.data.l[0] == gdk_x11_get_xatom_by_name_for_display (display, "WM_TAKE_FOCUS"))
1893 GdkWindow *win = event->any.window;
1894 Window focus_win = GDK_WINDOW_IMPL_X11(((GdkWindowObject *)win)->impl)->focus_window;
1896 /* There is no way of knowing reliably whether we are viewable so we need
1897 * to trap errors so we don't cause a BadMatch.
1899 gdk_error_trap_push ();
1900 XSetInputFocus (GDK_WINDOW_XDISPLAY (win),
1903 xevent->xclient.data.l[1]);
1904 XSync (GDK_WINDOW_XDISPLAY (win), False);
1905 gdk_error_trap_pop ();
1907 else if ((Atom) xevent->xclient.data.l[0] == gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_PING"))
1909 XEvent xev = *xevent;
1911 xev.xclient.window = GDK_WINDOW_XROOTWIN (win);
1912 XSendEvent (GDK_WINDOW_XDISPLAY (win),
1915 SubstructureRedirectMask | SubstructureNotifyMask, &xev);
1918 return GDK_FILTER_REMOVE;
1922 _gdk_events_queue (GdkDisplay *display)
1927 Display *xdisplay = GDK_DISPLAY_XDISPLAY (display);
1929 while (!_gdk_event_queue_find_first(display) && XPending (xdisplay))
1931 XNextEvent (xdisplay, &xevent);
1933 switch (xevent.type)
1939 if (XFilterEvent (&xevent, None))
1943 event = gdk_event_new (GDK_NOTHING);
1945 event->any.window = NULL;
1946 event->any.send_event = xevent.xany.send_event ? TRUE : FALSE;
1948 ((GdkEventPrivate *)event)->flags |= GDK_EVENT_PENDING;
1950 node = _gdk_event_queue_append (display, event);
1952 if (gdk_event_translate (display, event, &xevent, FALSE))
1954 ((GdkEventPrivate *)event)->flags &= ~GDK_EVENT_PENDING;
1958 _gdk_event_queue_remove_link (display, node);
1959 g_list_free_1 (node);
1960 gdk_event_free (event);
1966 gdk_event_prepare (GSource *source,
1969 GdkDisplay *display = ((GdkDisplaySource*)source)->display;
1972 GDK_THREADS_ENTER ();
1975 retval = (_gdk_event_queue_find_first (display) != NULL ||
1976 gdk_check_xpending (display));
1978 GDK_THREADS_LEAVE ();
1984 gdk_event_check (GSource *source)
1986 GdkDisplaySource *display_source = (GdkDisplaySource*)source;
1989 GDK_THREADS_ENTER ();
1991 if (display_source->event_poll_fd.revents & G_IO_IN)
1992 retval = (_gdk_event_queue_find_first (display_source->display) != NULL ||
1993 gdk_check_xpending (display_source->display));
1997 GDK_THREADS_LEAVE ();
2003 gdk_event_dispatch (GSource *source,
2004 GSourceFunc callback,
2007 GdkDisplay *display = ((GdkDisplaySource*)source)->display;
2010 GDK_THREADS_ENTER ();
2012 _gdk_events_queue (display);
2013 event = _gdk_event_unqueue (display);
2017 if (_gdk_event_func)
2018 (*_gdk_event_func) (event, _gdk_event_data);
2020 gdk_event_free (event);
2023 GDK_THREADS_LEAVE ();
2029 * gdk_event_send_client_message_for_display:
2030 * @display: the #GdkDisplay for the window where the message is to be sent.
2031 * @event: the #GdkEvent to send, which should be a #GdkEventClient.
2032 * @winid: the window to send the X ClientMessage event to.
2034 * Sends an X ClientMessage event to a given window.
2036 * This could be used for communicating between different applications,
2037 * though the amount of data is limited to 20 bytes.
2039 * Returns: non-zero on success.
2044 gdk_event_send_client_message_for_display (GdkDisplay *display,
2046 GdkNativeWindow winid)
2050 g_return_val_if_fail(event != NULL, FALSE);
2052 /* Set up our event to send, with the exception of its target window */
2053 sev.xclient.type = ClientMessage;
2054 sev.xclient.display = GDK_DISPLAY_XDISPLAY (display);
2055 sev.xclient.format = event->client.data_format;
2056 sev.xclient.window = winid;
2057 memcpy(&sev.xclient.data, &event->client.data, sizeof (sev.xclient.data));
2058 sev.xclient.message_type = gdk_x11_atom_to_xatom_for_display (display, event->client.message_type);
2060 return _gdk_send_xevent (display, winid, False, NoEventMask, &sev);
2065 /* Sends a ClientMessage to all toplevel client windows */
2067 gdk_event_send_client_message_to_all_recurse (GdkDisplay *display,
2074 unsigned long nitems, after;
2075 unsigned char *data;
2076 Window *ret_children, ret_root, ret_parent;
2077 unsigned int ret_nchildren;
2078 gboolean send = FALSE;
2079 gboolean found = FALSE;
2080 gboolean result = FALSE;
2083 gdk_error_trap_push ();
2085 if (XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), xid,
2086 gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_STATE"),
2087 0, 0, False, AnyPropertyType,
2088 &type, &format, &nitems, &after, &data) != Success)
2098 /* OK, we're all set, now let's find some windows to send this to */
2099 if (!XQueryTree (GDK_DISPLAY_XDISPLAY (display), xid,
2100 &ret_root, &ret_parent,
2101 &ret_children, &ret_nchildren))
2104 for(i = 0; i < ret_nchildren; i++)
2105 if (gdk_event_send_client_message_to_all_recurse (display, xev, ret_children[i], level + 1))
2108 XFree (ret_children);
2111 if (send || (!found && (level == 1)))
2113 xev->xclient.window = xid;
2114 _gdk_send_xevent (display, xid, False, NoEventMask, xev);
2117 result = send || found;
2120 gdk_error_trap_pop ();
2126 * gdk_screen_broadcast_client_message:
2127 * @screen: the #GdkScreen where the event will be broadcasted.
2128 * @event: the #GdkEvent.
2130 * Sends an X ClientMessage event to all toplevel windows on @screen.
2132 * Toplevel windows are determined by checking for the WM_STATE property,
2133 * as described in the Inter-Client Communication Conventions Manual (ICCCM).
2134 * If no windows are found with the WM_STATE property set, the message is
2135 * sent to all children of the root window.
2141 gdk_screen_broadcast_client_message (GdkScreen *screen,
2145 GdkWindow *root_window;
2147 g_return_if_fail (event != NULL);
2149 root_window = gdk_screen_get_root_window (screen);
2151 /* Set up our event to send, with the exception of its target window */
2152 sev.xclient.type = ClientMessage;
2153 sev.xclient.display = GDK_WINDOW_XDISPLAY (root_window);
2154 sev.xclient.format = event->client.data_format;
2155 memcpy(&sev.xclient.data, &event->client.data, sizeof (sev.xclient.data));
2156 sev.xclient.message_type =
2157 gdk_x11_atom_to_xatom_for_display (GDK_WINDOW_DISPLAY (root_window),
2158 event->client.message_type);
2160 gdk_event_send_client_message_to_all_recurse (gdk_screen_get_display (screen),
2162 GDK_WINDOW_XID (root_window),
2167 *--------------------------------------------------------------
2170 * Flushes the Xlib output buffer and then waits
2171 * until all requests have been received and processed
2172 * by the X server. The only real use for this function
2173 * is in dealing with XShm.
2181 *--------------------------------------------------------------
2187 GSList *tmp_list = _gdk_displays;
2191 XSync (GDK_DISPLAY_XDISPLAY (tmp_list->data), False);
2192 tmp_list = tmp_list->next;
2197 timestamp_predicate (Display *display,
2201 Window xwindow = GPOINTER_TO_UINT (arg);
2202 GdkDisplay *gdk_display = gdk_x11_lookup_xdisplay (display);
2204 if (xevent->type == PropertyNotify &&
2205 xevent->xproperty.window == xwindow &&
2206 xevent->xproperty.atom == gdk_x11_get_xatom_by_name_for_display
2207 (gdk_display, "GDK_TIMESTAMP_PROP"))
2214 * gdk_x11_get_server_time:
2215 * @window: a #GdkWindow, used for communication with the server.
2216 * The window must have GDK_PROPERTY_CHANGE_MASK in its
2217 * events mask or a hang will result.
2219 * Routine to get the current X server time stamp.
2221 * Return value: the time stamp.
2224 gdk_x11_get_server_time (GdkWindow *window)
2230 Atom timestamp_prop_atom;
2232 g_return_val_if_fail (GDK_IS_WINDOW (window), 0);
2233 g_return_val_if_fail (!GDK_WINDOW_DESTROYED (window), 0);
2235 xdisplay = GDK_WINDOW_XDISPLAY (window);
2236 xwindow = GDK_WINDOW_XWINDOW (window);
2237 timestamp_prop_atom =
2238 gdk_x11_get_xatom_by_name_for_display (GDK_WINDOW_DISPLAY (window),
2239 "GDK_TIMESTAMP_PROP");
2241 XChangeProperty (xdisplay, xwindow, timestamp_prop_atom,
2242 timestamp_prop_atom,
2243 8, PropModeReplace, &c, 1);
2245 XIfEvent (xdisplay, &xevent,
2246 timestamp_predicate, GUINT_TO_POINTER(xwindow));
2248 return xevent.xproperty.time;
2252 fetch_net_wm_check_window (GdkScreen *screen)
2254 GdkScreenX11 *screen_x11;
2255 GdkDisplay *display;
2262 /* This function is very slow on every call if you are not running a
2263 * spec-supporting WM. For now not optimized, because it isn't in
2264 * any critical code paths, but if you used it somewhere that had to
2265 * be fast you want to avoid "GTK is slow with old WMs" complaints.
2266 * Probably at that point the function should be changed to query
2267 * _NET_SUPPORTING_WM_CHECK only once every 10 seconds or something.
2270 screen_x11 = GDK_SCREEN_X11 (screen);
2271 display = screen_x11->display;
2273 if (screen_x11->wmspec_check_window != None)
2274 return; /* already have it */
2276 XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), screen_x11->xroot_window,
2277 gdk_x11_get_xatom_by_name_for_display (display, "_NET_SUPPORTING_WM_CHECK"),
2278 0, G_MAXLONG, False, XA_WINDOW, &type, &format,
2279 &n_items, &bytes_after, (guchar **) & xwindow);
2281 if (type != XA_WINDOW)
2284 gdk_error_trap_push ();
2286 /* Find out if this WM goes away, so we can reset everything. */
2287 XSelectInput (screen_x11->xdisplay, *xwindow, StructureNotifyMask);
2289 screen_x11->wmspec_check_window = *xwindow;
2292 screen_x11->need_refetch_net_supported = TRUE;
2293 screen_x11->need_refetch_wm_name = TRUE;
2295 /* Careful, reentrancy */
2296 _gdk_x11_screen_window_manager_changed (GDK_SCREEN (screen_x11));
2300 * gdk_x11_screen_get_window_manager_name:
2301 * @screen: a #GdkScreen
2303 * Returns the name of the window manager for @screen.
2305 * Return value: the name of the window manager screen @screen, or
2306 * "unknown" if the window manager is unknown. The string is owned by GDK
2307 * and should not be freed.
2312 gdk_x11_screen_get_window_manager_name (GdkScreen *screen)
2314 GdkScreenX11 *screen_x11;
2316 screen_x11 = GDK_SCREEN_X11 (screen);
2318 fetch_net_wm_check_window (screen);
2320 if (screen_x11->need_refetch_wm_name)
2322 /* Get the name of the window manager */
2323 screen_x11->need_refetch_wm_name = FALSE;
2325 g_free (screen_x11->window_manager_name);
2326 screen_x11->window_manager_name = g_strdup ("unknown");
2328 if (screen_x11->wmspec_check_window != None)
2338 XGetWindowProperty (GDK_DISPLAY_XDISPLAY (screen_x11->display),
2339 screen_x11->wmspec_check_window,
2340 gdk_x11_get_xatom_by_name_for_display (screen_x11->display,
2342 0, G_MAXLONG, False,
2343 gdk_x11_get_xatom_by_name_for_display (screen_x11->display,
2346 &n_items, &bytes_after,
2349 gdk_display_sync (screen_x11->display);
2351 gdk_error_trap_pop ();
2355 g_free (screen_x11->window_manager_name);
2356 screen_x11->window_manager_name = g_strdup (name);
2362 return GDK_SCREEN_X11 (screen)->window_manager_name;
2365 typedef struct _NetWmSupportedAtoms NetWmSupportedAtoms;
2367 struct _NetWmSupportedAtoms
2374 * gdk_x11_screen_supports_net_wm_hint:
2375 * @screen: the relevant #GdkScreen.
2376 * @property: a property atom.
2378 * This function is specific to the X11 backend of GDK, and indicates
2379 * whether the window manager supports a certain hint from the
2380 * Extended Window Manager Hints Specification. You can find this
2382 * <ulink url="http://www.freedesktop.org">http://www.freedesktop.org</ulink>.
2384 * When using this function, keep in mind that the window manager
2385 * can change over time; so you shouldn't use this function in
2386 * a way that impacts persistent application state. A common bug
2387 * is that your application can start up before the window manager
2388 * does when the user logs in, and before the window manager starts
2389 * gdk_x11_screen_supports_net_wm_hint() will return %FALSE for every property.
2390 * You can monitor the window_manager_changed signal on #GdkScreen to detect
2391 * a window manager change.
2393 * Return value: %TRUE if the window manager supports @property
2398 gdk_x11_screen_supports_net_wm_hint (GdkScreen *screen,
2402 GdkScreenX11 *screen_x11;
2403 NetWmSupportedAtoms *supported_atoms;
2404 GdkDisplay *display;
2406 g_return_val_if_fail (GDK_IS_SCREEN (screen), FALSE);
2408 screen_x11 = GDK_SCREEN_X11 (screen);
2409 display = screen_x11->display;
2411 supported_atoms = g_object_get_data (G_OBJECT (screen), "gdk-net-wm-supported-atoms");
2412 if (!supported_atoms)
2414 supported_atoms = g_new0 (NetWmSupportedAtoms, 1);
2415 g_object_set_data (G_OBJECT (screen), "gdk-net-wm-supported-atoms", supported_atoms);
2418 fetch_net_wm_check_window (screen);
2420 if (screen_x11->wmspec_check_window == None)
2423 if (screen_x11->need_refetch_net_supported)
2425 /* WM has changed since we last got the supported list,
2432 screen_x11->need_refetch_net_supported = FALSE;
2434 if (supported_atoms->atoms)
2435 XFree (supported_atoms->atoms);
2437 supported_atoms->atoms = NULL;
2438 supported_atoms->n_atoms = 0;
2440 XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), screen_x11->xroot_window,
2441 gdk_x11_get_xatom_by_name_for_display (display, "_NET_SUPPORTED"),
2442 0, G_MAXLONG, False, XA_ATOM, &type, &format,
2443 &supported_atoms->n_atoms, &bytes_after,
2444 (guchar **)&supported_atoms->atoms);
2446 if (type != XA_ATOM)
2450 if (supported_atoms->atoms == NULL)
2454 while (i < supported_atoms->n_atoms)
2456 if (supported_atoms->atoms[i] == gdk_x11_atom_to_xatom_for_display (display, property))
2466 * gdk_net_wm_supports:
2467 * @property: a property atom.
2469 * This function is specific to the X11 backend of GDK, and indicates
2470 * whether the window manager for the default screen supports a certain
2471 * hint from the Extended Window Manager Hints Specification. See
2472 * gdk_x11_screen_supports_net_wm_hint() for complete details.
2474 * Return value: %TRUE if the window manager supports @property
2477 gdk_net_wm_supports (GdkAtom property)
2479 return gdk_x11_screen_supports_net_wm_hint (gdk_screen_get_default (), property);
2484 const char *xsettings_name;
2485 const char *gdk_name;
2486 } settings_map[] = {
2487 { "Net/DoubleClickTime", "gtk-double-click-time" },
2488 { "Net/DndDragThreshold", "gtk-dnd-drag-threshold" },
2489 { "Gtk/CanChangeAccels", "gtk-can-change-accels" },
2490 { "Gtk/ColorPalette", "gtk-color-palette" },
2491 { "Gtk/FontName", "gtk-font-name" },
2492 { "Gtk/IconSizes", "gtk-icon-sizes" },
2493 { "Gtk/KeyThemeName", "gtk-key-theme-name" },
2494 { "Gtk/ToolbarStyle", "gtk-toolbar-style" },
2495 { "Gtk/ToolbarIconSize", "gtk-toolbar-icon-size" },
2496 { "Gtk/IMPreeditStyle", "gtk-im-preedit-style" },
2497 { "Gtk/IMStatusStyle", "gtk-im-status-style" },
2498 { "Net/CursorBlink", "gtk-cursor-blink" },
2499 { "Net/CursorBlinkTime", "gtk-cursor-blink-time" },
2500 { "Net/ThemeName", "gtk-theme-name" }
2504 gdk_xsettings_notify_cb (const char *name,
2505 XSettingsAction action,
2506 XSettingsSetting *setting,
2510 GdkScreen *screen = data;
2511 GdkScreenX11 *screen_x11 = data;
2514 if (screen_x11->xsettings_in_init)
2517 new_event.type = GDK_SETTING;
2518 new_event.setting.window = gdk_screen_get_root_window (screen);
2519 new_event.setting.send_event = FALSE;
2520 new_event.setting.name = NULL;
2522 for (i = 0; i < G_N_ELEMENTS (settings_map) ; i++)
2523 if (strcmp (settings_map[i].xsettings_name, name) == 0)
2525 new_event.setting.name = (char *)settings_map[i].gdk_name;
2530 if (!new_event.setting.name)
2535 case XSETTINGS_ACTION_NEW:
2536 new_event.setting.action = GDK_SETTING_ACTION_NEW;
2538 case XSETTINGS_ACTION_CHANGED:
2539 new_event.setting.action = GDK_SETTING_ACTION_CHANGED;
2541 case XSETTINGS_ACTION_DELETED:
2542 new_event.setting.action = GDK_SETTING_ACTION_DELETED;
2546 gdk_event_put (&new_event);
2550 check_transform (const gchar *xsettings_name,
2554 if (!g_value_type_transformable (src_type, dest_type))
2556 g_warning ("Cannot tranform xsetting %s of type %s to type %s\n",
2558 g_type_name (src_type),
2559 g_type_name (dest_type));
2567 * gdk_screen_get_setting:
2568 * @screen: the #GdkScreen where the setting is located
2569 * @name: the name of the setting
2570 * @value: location to store the value of the setting
2572 * Retrieves a desktop-wide setting such as double-click time
2573 * for the #GdkScreen @screen.
2575 * FIXME needs a list of valid settings here, or a link to
2578 * Returns: %TRUE if the setting existed and a value was stored
2579 * in @value, %FALSE otherwise.
2584 gdk_screen_get_setting (GdkScreen *screen,
2589 const char *xsettings_name = NULL;
2590 XSettingsResult result;
2591 XSettingsSetting *setting;
2592 GdkScreenX11 *screen_x11;
2593 gboolean success = FALSE;
2595 GValue tmp_val = { 0, };
2597 g_return_val_if_fail (GDK_IS_SCREEN (screen), FALSE);
2599 screen_x11 = GDK_SCREEN_X11 (screen);
2601 for (i = 0; i < G_N_ELEMENTS (settings_map) ; i++)
2602 if (strcmp (settings_map[i].gdk_name, name) == 0)
2604 xsettings_name = settings_map[i].xsettings_name;
2608 if (!xsettings_name)
2611 result = xsettings_client_get_setting (screen_x11->xsettings_client,
2612 xsettings_name, &setting);
2613 if (result != XSETTINGS_SUCCESS)
2616 switch (setting->type)
2618 case XSETTINGS_TYPE_INT:
2619 if (check_transform (xsettings_name, G_TYPE_INT, G_VALUE_TYPE (value)))
2621 g_value_init (&tmp_val, G_TYPE_INT);
2622 g_value_set_int (&tmp_val, setting->data.v_int);
2623 g_value_transform (&tmp_val, value);
2628 case XSETTINGS_TYPE_STRING:
2629 if (check_transform (xsettings_name, G_TYPE_STRING, G_VALUE_TYPE (value)))
2631 g_value_init (&tmp_val, G_TYPE_STRING);
2632 g_value_set_string (&tmp_val, setting->data.v_string);
2633 g_value_transform (&tmp_val, value);
2638 case XSETTINGS_TYPE_COLOR:
2639 if (!check_transform (xsettings_name, GDK_TYPE_COLOR, G_VALUE_TYPE (value)))
2643 g_value_init (&tmp_val, GDK_TYPE_COLOR);
2646 color.red = setting->data.v_color.red;
2647 color.green = setting->data.v_color.green;
2648 color.blue = setting->data.v_color.blue;
2650 g_value_set_boxed (&tmp_val, &color);
2652 g_value_transform (&tmp_val, value);
2659 g_value_unset (&tmp_val);
2661 xsettings_setting_free (setting);
2666 static GdkFilterReturn
2667 gdk_xsettings_client_event_filter (GdkXEvent *xevent,
2671 GdkScreenX11 *screen = data;
2673 if (xsettings_client_process_event (screen->xsettings_client, (XEvent *)xevent))
2674 return GDK_FILTER_REMOVE;
2676 return GDK_FILTER_CONTINUE;
2680 gdk_xsettings_watch_cb (Window window,
2686 GdkScreen *screen = cb_data;
2688 gdkwin = gdk_window_lookup_for_display (gdk_screen_get_display (screen), window);
2693 gdkwin = gdk_window_foreign_new_for_display (gdk_screen_get_display (screen), window);
2695 g_object_ref (gdkwin);
2697 gdk_window_add_filter (gdkwin, gdk_xsettings_client_event_filter, screen);
2702 gdk_window_remove_filter (gdkwin, gdk_xsettings_client_event_filter, screen);
2703 g_object_unref (gdkwin);