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"
32 #include "gdkkeysyms.h"
41 #include "gdkinputprivate.h"
44 #include <X11/XKBlib.h>
47 #include <X11/Xatom.h>
49 typedef struct _GdkIOClosure GdkIOClosure;
50 typedef struct _GdkEventPrivate GdkEventPrivate;
52 #define DOUBLE_CLICK_TIME 250
53 #define TRIPLE_CLICK_TIME 500
54 #define DOUBLE_CLICK_DIST 5
55 #define TRIPLE_CLICK_DIST 5
59 /* Following flag is set for events on the event queue during
60 * translation and cleared afterwards.
62 GDK_EVENT_PENDING = 1 << 0
67 GdkInputFunction function;
68 GdkInputCondition condition;
69 GdkDestroyNotify notify;
73 struct _GdkEventPrivate
80 * Private function declarations
83 static gint gdk_event_apply_filters (XEvent *xevent,
86 static gint gdk_event_translate (GdkEvent *event,
88 gboolean return_exposes);
90 static Bool gdk_event_get_type (Display *display,
95 static gboolean gdk_event_prepare (GSource *source,
97 static gboolean gdk_event_check (GSource *source);
98 static gboolean gdk_event_dispatch (GSource *source,
102 GdkFilterReturn gdk_wm_protocols_filter (GdkXEvent *xev,
106 /* Private variable declarations
109 static int connection_number = 0; /* The file descriptor number of our
110 * connection to the X server. This
111 * is used so that we may determine
112 * when events are pending by using
113 * the "select" system call.
115 static GList *client_filters; /* Filters for client messages */
117 static GSourceFuncs event_funcs = {
124 static GPollFD event_poll_fd;
126 static Window wmspec_check_window = None;
128 /*********************************************
129 * Functions for maintaining the event queue *
130 *********************************************/
133 gdk_events_init (void)
137 connection_number = ConnectionNumber (gdk_display);
139 g_message ("connection number: %d", connection_number));
142 source = g_source_new (&event_funcs, sizeof (GSource));
143 g_source_set_priority (source, GDK_PRIORITY_EVENTS);
145 event_poll_fd.fd = connection_number;
146 event_poll_fd.events = G_IO_IN;
148 g_source_add_poll (source, &event_poll_fd);
149 g_source_set_can_recurse (source, TRUE);
150 g_source_attach (source, NULL);
152 gdk_add_client_message_filter (gdk_wm_protocols,
153 gdk_wm_protocols_filter, NULL);
157 *--------------------------------------------------------------
160 * Returns if events are pending on the queue.
165 * Returns TRUE if events are pending
169 *--------------------------------------------------------------
173 gdk_events_pending (void)
175 return (gdk_event_queue_find_first() || XPending (gdk_display));
179 *--------------------------------------------------------------
180 * gdk_event_get_graphics_expose
182 * Waits for a GraphicsExpose or NoExpose event
187 * For GraphicsExpose events, returns a pointer to the event
188 * converted into a GdkEvent Otherwise, returns NULL.
192 *-------------------------------------------------------------- */
195 graphics_expose_predicate (Display *display,
199 if (xevent->xany.window == GDK_DRAWABLE_XID (arg) &&
200 (xevent->xany.type == GraphicsExpose ||
201 xevent->xany.type == NoExpose))
208 gdk_event_get_graphics_expose (GdkWindow *window)
213 g_return_val_if_fail (window != NULL, NULL);
215 XIfEvent (gdk_display, &xevent, graphics_expose_predicate, (XPointer) window);
217 if (xevent.xany.type == GraphicsExpose)
219 event = gdk_event_new ();
221 if (gdk_event_translate (event, &xevent, TRUE))
224 gdk_event_free (event);
231 gdk_event_apply_filters (XEvent *xevent,
236 GdkFilterReturn result;
242 GdkEventFilter *filter = (GdkEventFilter*) tmp_list->data;
244 tmp_list = tmp_list->next;
245 result = filter->function (xevent, event, filter->data);
246 if (result != GDK_FILTER_CONTINUE)
250 return GDK_FILTER_CONTINUE;
254 gdk_add_client_message_filter (GdkAtom message_type,
258 GdkClientFilter *filter = g_new (GdkClientFilter, 1);
260 filter->type = message_type;
261 filter->function = func;
264 client_filters = g_list_prepend (client_filters, filter);
267 static GdkAtom wm_state_atom = 0;
268 static GdkAtom wm_desktop_atom = 0;
271 gdk_check_wm_state_changed (GdkWindow *window)
277 GdkAtom *atoms = NULL;
280 GdkAtom maxvert_atom;
281 GdkAtom maxhorz_atom;
282 gboolean found_sticky, found_maxvert, found_maxhorz;
283 GdkWindowState old_state;
285 if (GDK_WINDOW_DESTROYED (window))
288 if (wm_state_atom == 0)
289 wm_state_atom = gdk_atom_intern ("_NET_WM_STATE", FALSE);
291 if (wm_desktop_atom == 0)
292 wm_desktop_atom = gdk_atom_intern ("_NET_WM_DESKTOP", FALSE);
294 XGetWindowProperty (GDK_WINDOW_XDISPLAY (window), GDK_WINDOW_XID (window),
295 wm_state_atom, 0, G_MAXLONG,
296 False, XA_ATOM, &type, &format, &nitems,
297 &bytes_after, (guchar **)&atoms);
302 sticky_atom = gdk_atom_intern ("_NET_WM_STATE_STICKY", FALSE);
303 maxvert_atom = gdk_atom_intern ("_NET_WM_STATE_MAXIMIZED_VERT", FALSE);
304 maxhorz_atom = gdk_atom_intern ("_NET_WM_STATE_MAXIMIZED_HORZ", FALSE);
306 found_sticky = FALSE;
307 found_maxvert = FALSE;
308 found_maxhorz = FALSE;
313 if (atoms[i] == sticky_atom)
315 else if (atoms[i] == maxvert_atom)
316 found_maxvert = TRUE;
317 else if (atoms[i] == maxhorz_atom)
318 found_maxhorz = TRUE;
327 found_sticky = FALSE;
328 found_maxvert = FALSE;
329 found_maxhorz = FALSE;
332 /* For found_sticky to remain TRUE, we have to also be on desktop
340 XGetWindowProperty (GDK_WINDOW_XDISPLAY (window), GDK_WINDOW_XID (window),
341 wm_desktop_atom, 0, G_MAXLONG,
342 False, XA_CARDINAL, &type, &format, &nitems,
343 &bytes_after, (guchar **)&desktop);
347 if (*desktop != 0xFFFFFFFF)
348 found_sticky = FALSE;
353 old_state = gdk_window_get_state (window);
355 if (old_state & GDK_WINDOW_STATE_STICKY)
358 gdk_synthesize_window_state (window,
359 GDK_WINDOW_STATE_STICKY,
365 gdk_synthesize_window_state (window,
367 GDK_WINDOW_STATE_STICKY);
370 /* Our "maximized" means both vertical and horizontal; if only one,
371 * we don't expose that via GDK
373 if (old_state & GDK_WINDOW_STATE_MAXIMIZED)
375 if (!(found_maxvert && found_maxhorz))
376 gdk_synthesize_window_state (window,
377 GDK_WINDOW_STATE_MAXIMIZED,
382 if (found_maxvert && found_maxhorz)
383 gdk_synthesize_window_state (window,
385 GDK_WINDOW_STATE_MAXIMIZED);
390 gdk_event_translate (GdkEvent *event,
392 gboolean return_exposes)
396 GdkWindowObject *window_private;
397 static XComposeStatus compose;
401 static gchar* buf = NULL;
402 static gint buf_len= 0;
407 gint xoffset, yoffset;
411 /* Find the GdkWindow that this event occurred in.
413 * We handle events with window=None
414 * specially - they are generated by XFree86's XInput under
415 * some circumstances.
418 if (xevent->xany.window == None)
420 return_val = _gdk_input_window_none_event (event, xevent);
422 if (return_val >= 0) /* was handled */
428 window = gdk_window_lookup (xevent->xany.window);
429 /* FIXME: window might be a GdkPixmap!!! */
431 window_private = (GdkWindowObject *) window;
434 gdk_window_ref (window);
436 if (wmspec_check_window != None &&
437 xevent->xany.window == wmspec_check_window)
439 if (xevent->type == DestroyNotify)
440 wmspec_check_window = None;
442 /* Eat events on this window unless someone had wrapped
443 * it as a foreign window
449 event->any.window = window;
450 event->any.send_event = xevent->xany.send_event ? TRUE : FALSE;
452 if (window_private && GDK_WINDOW_DESTROYED (window))
454 if (xevent->type != DestroyNotify)
459 /* Check for filters for this window
461 GdkFilterReturn result;
462 result = gdk_event_apply_filters (xevent, event,
464 ?window_private->filters
465 :gdk_default_filters);
467 if (result != GDK_FILTER_CONTINUE)
469 return_val = (result == GDK_FILTER_TRANSLATE) ? TRUE : FALSE;
475 if (window == NULL && gdk_xim_window && xevent->type == KeyPress &&
476 !GDK_WINDOW_DESTROYED (gdk_xim_window))
479 * If user presses a key in Preedit or Status window, keypress event
480 * is sometimes sent to these windows. These windows are not managed
481 * by GDK, so we redirect KeyPress event to xim_window.
483 * If someone want to use the window whitch is not managed by GDK
484 * and want to get KeyPress event, he/she must register the filter
485 * function to gdk_default_filters to intercept the event.
488 GdkFilterReturn result;
490 window = gdk_xim_window;
491 window_private = (GdkWindowObject *) window;
492 gdk_window_ref (window);
493 event->any.window = window;
496 g_message ("KeyPress event is redirected to xim_window: %#lx",
497 xevent->xany.window));
499 result = gdk_event_apply_filters (xevent, event,
500 window_private->filters);
501 if (result != GDK_FILTER_CONTINUE)
503 return_val = (result == GDK_FILTER_TRANSLATE) ? TRUE : FALSE;
509 /* We do a "manual" conversion of the XEvent to a
510 * GdkEvent. The structures are mostly the same so
511 * the conversion is fairly straightforward. We also
512 * optionally print debugging info regarding events
520 _gdk_windowing_window_get_offsets (window, &xoffset, &yoffset);
528 switch (xevent->type)
531 /* Lookup the string corresponding to the given keysym.
538 buf = g_new (gchar, buf_len);
540 keysym = GDK_VoidSymbol;
542 if (gdk_xim_ic && gdk_xim_ic->xic)
546 /* Clear keyval. Depending on status, may not be set */
547 charcount = XmbLookupString(gdk_xim_ic->xic,
548 &xevent->xkey, buf, buf_len-1,
550 if (status == XBufferOverflow)
552 /* alloc adequate size of buffer */
554 g_message("XIM: overflow (required %i)", charcount));
556 while (buf_len <= charcount)
558 buf = (gchar *) g_realloc (buf, buf_len);
560 charcount = XmbLookupString (gdk_xim_ic->xic,
561 &xevent->xkey, buf, buf_len-1,
564 if (status == XLookupNone)
571 charcount = XLookupString (&xevent->xkey, buf, buf_len,
574 charcount = XLookupString (&xevent->xkey, buf, 16,
577 event->key.keyval = keysym;
578 event->key.hardware_keycode = xevent->xkey.keycode;
580 if (charcount > 0 && buf[charcount-1] == '\0')
583 buf[charcount] = '\0';
585 #ifdef G_ENABLE_DEBUG
586 if (gdk_debug_flags & GDK_DEBUG_EVENTS)
588 g_message ("key press:\twindow: %ld key: %12s %d",
590 event->key.keyval ? XKeysymToString (event->key.keyval) : "(none)",
593 g_message ("\t\tlength: %4d string: \"%s\"",
596 #endif /* G_ENABLE_DEBUG */
598 /* bits 13 and 14 in the "state" field are the keyboard group */
599 #define KEYBOARD_GROUP_SHIFT 13
600 #define KEYBOARD_GROUP_MASK ((1 << 13) | (1 << 14))
602 event->key.type = GDK_KEY_PRESS;
603 event->key.window = window;
604 event->key.time = xevent->xkey.time;
605 event->key.state = (GdkModifierType) xevent->xkey.state;
606 event->key.string = g_strdup (buf);
607 event->key.length = charcount;
609 event->key.group = (xevent->xkey.state & KEYBOARD_GROUP_MASK) >> KEYBOARD_GROUP_SHIFT;
614 /* Lookup the string corresponding to the given keysym.
620 buf = g_new (gchar, buf_len);
623 keysym = GDK_VoidSymbol;
624 charcount = XLookupString (&xevent->xkey, buf, 16,
626 event->key.keyval = keysym;
629 g_message ("key release:\t\twindow: %ld key: %12s %d",
631 XKeysymToString (event->key.keyval),
634 event->key.type = GDK_KEY_RELEASE;
635 event->key.window = window;
636 event->key.time = xevent->xkey.time;
637 event->key.state = (GdkModifierType) xevent->xkey.state;
638 event->key.length = 0;
639 event->key.string = NULL;
645 g_message ("button press:\t\twindow: %ld x,y: %d %d button: %d",
646 xevent->xbutton.window,
647 xevent->xbutton.x, xevent->xbutton.y,
648 xevent->xbutton.button));
650 if (window_private &&
651 (window_private->extension_events != 0) &&
652 gdk_input_ignore_core)
658 /* If we get a ButtonPress event where the button is 4 or 5,
659 it's a Scroll event */
660 if (xevent->xbutton.button == 4 || xevent->xbutton.button == 5)
662 event->scroll.type = GDK_SCROLL;
663 event->scroll.direction = (xevent->xbutton.button == 4) ?
664 GDK_SCROLL_UP : GDK_SCROLL_DOWN;
665 event->scroll.window = window;
666 event->scroll.time = xevent->xbutton.x;
667 event->scroll.x = xevent->xbutton.x + xoffset;
668 event->scroll.y = xevent->xbutton.y + yoffset;
669 event->scroll.x_root = (gfloat)xevent->xbutton.x_root;
670 event->scroll.y_root = (gfloat)xevent->xbutton.y_root;
671 event->scroll.state = (GdkModifierType) xevent->xbutton.state;
672 event->scroll.device = gdk_core_pointer;
676 event->button.type = GDK_BUTTON_PRESS;
677 event->button.window = window;
678 event->button.time = xevent->xbutton.time;
679 event->button.x = xevent->xbutton.x + xoffset;
680 event->button.y = xevent->xbutton.y + yoffset;
681 event->button.x_root = (gfloat)xevent->xbutton.x_root;
682 event->button.y_root = (gfloat)xevent->xbutton.y_root;
683 event->button.axes = NULL;
684 event->button.state = (GdkModifierType) xevent->xbutton.state;
685 event->button.button = xevent->xbutton.button;
686 event->button.device = gdk_core_pointer;
688 gdk_event_button_generate (event);
695 g_message ("button release:\twindow: %ld x,y: %d %d button: %d",
696 xevent->xbutton.window,
697 xevent->xbutton.x, xevent->xbutton.y,
698 xevent->xbutton.button));
700 if (window_private &&
701 (window_private->extension_events != 0) &&
702 gdk_input_ignore_core)
708 /* We treat button presses as scroll wheel events, so ignore the release */
709 if (xevent->xbutton.button == 4 || xevent->xbutton.button == 5)
715 event->button.type = GDK_BUTTON_RELEASE;
716 event->button.window = window;
717 event->button.time = xevent->xbutton.time;
718 event->button.x = xevent->xbutton.x + xoffset;
719 event->button.y = xevent->xbutton.y + yoffset;
720 event->button.x_root = (gfloat)xevent->xbutton.x_root;
721 event->button.y_root = (gfloat)xevent->xbutton.y_root;
722 event->button.axes = NULL;
723 event->button.state = (GdkModifierType) xevent->xbutton.state;
724 event->button.button = xevent->xbutton.button;
725 event->button.device = gdk_core_pointer;
731 g_message ("motion notify:\t\twindow: %ld x,y: %d %d hint: %s",
732 xevent->xmotion.window,
733 xevent->xmotion.x, xevent->xmotion.y,
734 (xevent->xmotion.is_hint) ? "true" : "false"));
736 if (window_private &&
737 (window_private->extension_events != 0) &&
738 gdk_input_ignore_core)
744 event->motion.type = GDK_MOTION_NOTIFY;
745 event->motion.window = window;
746 event->motion.time = xevent->xmotion.time;
747 event->motion.x = xevent->xmotion.x + xoffset;
748 event->motion.y = xevent->xmotion.y + yoffset;
749 event->motion.x_root = (gfloat)xevent->xmotion.x_root;
750 event->motion.y_root = (gfloat)xevent->xmotion.y_root;
751 event->motion.axes = NULL;
752 event->motion.state = (GdkModifierType) xevent->xmotion.state;
753 event->motion.is_hint = xevent->xmotion.is_hint;
754 event->motion.device = gdk_core_pointer;
760 g_message ("enter notify:\t\twindow: %ld detail: %d subwin: %ld",
761 xevent->xcrossing.window,
762 xevent->xcrossing.detail,
763 xevent->xcrossing.subwindow));
765 /* Tell XInput stuff about it if appropriate */
766 if (window_private &&
767 !GDK_WINDOW_DESTROYED (window) &&
768 window_private->extension_events != 0)
769 _gdk_input_enter_event (&xevent->xcrossing, window);
771 event->crossing.type = GDK_ENTER_NOTIFY;
772 event->crossing.window = window;
774 /* If the subwindow field of the XEvent is non-NULL, then
775 * lookup the corresponding GdkWindow.
777 if (xevent->xcrossing.subwindow != None)
778 event->crossing.subwindow = gdk_window_lookup (xevent->xcrossing.subwindow);
780 event->crossing.subwindow = NULL;
782 event->crossing.time = xevent->xcrossing.time;
783 event->crossing.x = xevent->xcrossing.x + xoffset;
784 event->crossing.y = xevent->xcrossing.y + yoffset;
785 event->crossing.x_root = xevent->xcrossing.x_root;
786 event->crossing.y_root = xevent->xcrossing.y_root;
788 /* Translate the crossing mode into Gdk terms.
790 switch (xevent->xcrossing.mode)
793 event->crossing.mode = GDK_CROSSING_NORMAL;
796 event->crossing.mode = GDK_CROSSING_GRAB;
799 event->crossing.mode = GDK_CROSSING_UNGRAB;
803 /* Translate the crossing detail into Gdk terms.
805 switch (xevent->xcrossing.detail)
808 event->crossing.detail = GDK_NOTIFY_INFERIOR;
811 event->crossing.detail = GDK_NOTIFY_ANCESTOR;
814 event->crossing.detail = GDK_NOTIFY_VIRTUAL;
816 case NotifyNonlinear:
817 event->crossing.detail = GDK_NOTIFY_NONLINEAR;
819 case NotifyNonlinearVirtual:
820 event->crossing.detail = GDK_NOTIFY_NONLINEAR_VIRTUAL;
823 event->crossing.detail = GDK_NOTIFY_UNKNOWN;
827 event->crossing.focus = xevent->xcrossing.focus;
828 event->crossing.state = xevent->xcrossing.state;
834 g_message ("leave notify:\t\twindow: %ld detail: %d subwin: %ld",
835 xevent->xcrossing.window,
836 xevent->xcrossing.detail, xevent->xcrossing.subwindow));
838 event->crossing.type = GDK_LEAVE_NOTIFY;
839 event->crossing.window = window;
841 /* If the subwindow field of the XEvent is non-NULL, then
842 * lookup the corresponding GdkWindow.
844 if (xevent->xcrossing.subwindow != None)
845 event->crossing.subwindow = gdk_window_lookup (xevent->xcrossing.subwindow);
847 event->crossing.subwindow = NULL;
849 event->crossing.time = xevent->xcrossing.time;
850 event->crossing.x = xevent->xcrossing.x + xoffset;
851 event->crossing.y = xevent->xcrossing.y + yoffset;
852 event->crossing.x_root = xevent->xcrossing.x_root;
853 event->crossing.y_root = xevent->xcrossing.y_root;
855 /* Translate the crossing mode into Gdk terms.
857 switch (xevent->xcrossing.mode)
860 event->crossing.mode = GDK_CROSSING_NORMAL;
863 event->crossing.mode = GDK_CROSSING_GRAB;
866 event->crossing.mode = GDK_CROSSING_UNGRAB;
870 /* Translate the crossing detail into Gdk terms.
872 switch (xevent->xcrossing.detail)
875 event->crossing.detail = GDK_NOTIFY_INFERIOR;
878 event->crossing.detail = GDK_NOTIFY_ANCESTOR;
881 event->crossing.detail = GDK_NOTIFY_VIRTUAL;
883 case NotifyNonlinear:
884 event->crossing.detail = GDK_NOTIFY_NONLINEAR;
886 case NotifyNonlinearVirtual:
887 event->crossing.detail = GDK_NOTIFY_NONLINEAR_VIRTUAL;
890 event->crossing.detail = GDK_NOTIFY_UNKNOWN;
894 event->crossing.focus = xevent->xcrossing.focus;
895 event->crossing.state = xevent->xcrossing.state;
901 /* We only care about focus events that indicate that _this_
902 * window (not a ancestor or child) got or lost the focus
904 switch (xevent->xfocus.detail)
908 case NotifyNonlinear:
910 g_message ("focus %s:\t\twindow: %ld",
911 (xevent->xany.type == FocusIn) ? "in" : "out",
912 xevent->xfocus.window));
914 /* gdk_keyboard_grab() causes following events. These events confuse
915 * the XIM focus, so ignore them.
917 if (xevent->xfocus.mode == NotifyGrab ||
918 xevent->xfocus.mode == NotifyUngrab)
921 event->focus_change.type = GDK_FOCUS_CHANGE;
922 event->focus_change.window = window;
923 event->focus_change.in = (xevent->xany.type == FocusIn);
933 g_message ("keymap notify"));
935 /* Not currently handled */
941 g_message ("expose:\t\twindow: %ld %d x,y: %d %d w,h: %d %d%s",
942 xevent->xexpose.window, xevent->xexpose.count,
943 xevent->xexpose.x, xevent->xexpose.y,
944 xevent->xexpose.width, xevent->xexpose.height,
945 event->any.send_event ? " (send)" : ""));
947 GdkRectangle expose_rect;
949 expose_rect.x = xevent->xexpose.x + xoffset;
950 expose_rect.y = xevent->xexpose.y + yoffset;
951 expose_rect.width = xevent->xexpose.width;
952 expose_rect.height = xevent->xexpose.height;
956 event->expose.type = GDK_EXPOSE;
957 event->expose.area = expose_rect;
958 event->expose.window = window;
959 event->expose.count = xevent->xexpose.count;
965 _gdk_window_process_expose (window, xevent->xexpose.serial, &expose_rect);
977 GdkRectangle expose_rect;
980 g_message ("graphics expose:\tdrawable: %ld",
981 xevent->xgraphicsexpose.drawable));
983 expose_rect.x = xevent->xgraphicsexpose.x + xoffset;
984 expose_rect.y = xevent->xgraphicsexpose.y + yoffset;
985 expose_rect.width = xevent->xgraphicsexpose.width;
986 expose_rect.height = xevent->xgraphicsexpose.height;
990 event->expose.type = GDK_EXPOSE;
991 event->expose.area = expose_rect;
992 event->expose.window = window;
993 event->expose.count = xevent->xgraphicsexpose.count;
999 _gdk_window_process_expose (window, xevent->xgraphicsexpose.serial, &expose_rect);
1009 g_message ("no expose:\t\tdrawable: %ld",
1010 xevent->xnoexpose.drawable));
1012 event->no_expose.type = GDK_NO_EXPOSE;
1013 event->no_expose.window = window;
1017 case VisibilityNotify:
1018 #ifdef G_ENABLE_DEBUG
1019 if (gdk_debug_flags & GDK_DEBUG_EVENTS)
1020 switch (xevent->xvisibility.state)
1022 case VisibilityFullyObscured:
1023 g_message ("visibility notify:\twindow: %ld none",
1024 xevent->xvisibility.window);
1026 case VisibilityPartiallyObscured:
1027 g_message ("visibility notify:\twindow: %ld partial",
1028 xevent->xvisibility.window);
1030 case VisibilityUnobscured:
1031 g_message ("visibility notify:\twindow: %ld full",
1032 xevent->xvisibility.window);
1035 #endif /* G_ENABLE_DEBUG */
1037 event->visibility.type = GDK_VISIBILITY_NOTIFY;
1038 event->visibility.window = window;
1040 switch (xevent->xvisibility.state)
1042 case VisibilityFullyObscured:
1043 event->visibility.state = GDK_VISIBILITY_FULLY_OBSCURED;
1046 case VisibilityPartiallyObscured:
1047 event->visibility.state = GDK_VISIBILITY_PARTIAL;
1050 case VisibilityUnobscured:
1051 event->visibility.state = GDK_VISIBILITY_UNOBSCURED;
1059 g_message ("create notify:\twindow: %ld x,y: %d %d w,h: %d %d b-w: %d parent: %ld ovr: %d",
1060 xevent->xcreatewindow.window,
1061 xevent->xcreatewindow.x,
1062 xevent->xcreatewindow.y,
1063 xevent->xcreatewindow.width,
1064 xevent->xcreatewindow.height,
1065 xevent->xcreatewindow.border_width,
1066 xevent->xcreatewindow.parent,
1067 xevent->xcreatewindow.override_redirect));
1068 /* not really handled */
1073 g_message ("destroy notify:\twindow: %ld",
1074 xevent->xdestroywindow.window));
1076 event->any.type = GDK_DESTROY;
1077 event->any.window = window;
1079 return_val = window_private && !GDK_WINDOW_DESTROYED (window);
1081 if (window && GDK_WINDOW_XID (window) != GDK_ROOT_WINDOW())
1082 gdk_window_destroy_notify (window);
1087 g_message ("unmap notify:\t\twindow: %ld",
1088 xevent->xmap.window));
1090 event->any.type = GDK_UNMAP;
1091 event->any.window = window;
1093 /* If we are shown (not withdrawn) and get an unmap, it means we
1094 * were iconified in the X sense. If we are withdrawn, and get
1095 * an unmap, it means we hid the window ourselves, so we
1096 * will have already flipped the iconified bit off.
1098 if (GDK_WINDOW_IS_MAPPED (window))
1099 gdk_synthesize_window_state (window,
1101 GDK_WINDOW_STATE_ICONIFIED);
1103 if (gdk_xgrab_window == window_private)
1104 gdk_xgrab_window = NULL;
1110 g_message ("map notify:\t\twindow: %ld",
1111 xevent->xmap.window));
1113 event->any.type = GDK_MAP;
1114 event->any.window = window;
1116 /* Unset iconified if it was set */
1117 if (((GdkWindowObject*)window)->state & GDK_WINDOW_STATE_ICONIFIED)
1118 gdk_synthesize_window_state (window,
1119 GDK_WINDOW_STATE_ICONIFIED,
1124 case ReparentNotify:
1126 g_message ("reparent notify:\twindow: %ld x,y: %d %d parent: %ld ovr: %d",
1127 xevent->xreparent.window,
1128 xevent->xreparent.x,
1129 xevent->xreparent.y,
1130 xevent->xreparent.parent,
1131 xevent->xreparent.override_redirect));
1133 /* Not currently handled */
1137 case ConfigureNotify:
1139 g_message ("configure notify:\twindow: %ld x,y: %d %d w,h: %d %d b-w: %d above: %ld ovr: %d%s",
1140 xevent->xconfigure.window,
1141 xevent->xconfigure.x,
1142 xevent->xconfigure.y,
1143 xevent->xconfigure.width,
1144 xevent->xconfigure.height,
1145 xevent->xconfigure.border_width,
1146 xevent->xconfigure.above,
1147 xevent->xconfigure.override_redirect,
1150 : GDK_WINDOW_TYPE (window) == GDK_WINDOW_CHILD
1151 ? " (discarding child)"
1154 !GDK_WINDOW_DESTROYED (window) &&
1155 (window_private->extension_events != 0))
1156 _gdk_input_configure_event (&xevent->xconfigure, window);
1158 if (!window || GDK_WINDOW_TYPE (window) == GDK_WINDOW_CHILD)
1162 event->configure.type = GDK_CONFIGURE;
1163 event->configure.window = window;
1164 event->configure.width = xevent->xconfigure.width;
1165 event->configure.height = xevent->xconfigure.height;
1167 if (!xevent->xconfigure.x &&
1168 !xevent->xconfigure.y &&
1169 !GDK_WINDOW_DESTROYED (window))
1173 Window child_window = 0;
1175 gdk_error_trap_push ();
1176 if (XTranslateCoordinates (GDK_DRAWABLE_XDISPLAY (window),
1177 GDK_DRAWABLE_XID (window),
1183 if (!gdk_error_trap_pop ())
1185 event->configure.x = tx;
1186 event->configure.y = ty;
1190 gdk_error_trap_pop ();
1194 event->configure.x = xevent->xconfigure.x;
1195 event->configure.y = xevent->xconfigure.y;
1197 window_private->x = event->configure.x;
1198 window_private->y = event->configure.y;
1199 GDK_WINDOW_IMPL_X11 (window_private->impl)->width = xevent->xconfigure.width;
1200 GDK_WINDOW_IMPL_X11 (window_private->impl)->height = xevent->xconfigure.height;
1201 if (window_private->resize_count > 1)
1202 window_private->resize_count -= 1;
1206 case PropertyNotify:
1208 gchar *atom = gdk_atom_name (xevent->xproperty.atom);
1209 g_message ("property notify:\twindow: %ld, atom(%ld): %s%s%s",
1210 xevent->xproperty.window,
1211 xevent->xproperty.atom,
1213 atom ? atom : "unknown",
1218 event->property.type = GDK_PROPERTY_NOTIFY;
1219 event->property.window = window;
1220 event->property.atom = xevent->xproperty.atom;
1221 event->property.time = xevent->xproperty.time;
1222 event->property.state = xevent->xproperty.state;
1224 if (wm_state_atom == 0)
1225 wm_state_atom = gdk_atom_intern ("_NET_WM_STATE", FALSE);
1227 if (wm_desktop_atom == 0)
1228 wm_desktop_atom = gdk_atom_intern ("_NET_WM_DESKTOP", FALSE);
1230 if (event->property.atom == wm_state_atom ||
1231 event->property.atom == wm_desktop_atom)
1233 /* If window state changed, then synthesize those events. */
1234 gdk_check_wm_state_changed (event->property.window);
1239 case SelectionClear:
1241 g_message ("selection clear:\twindow: %ld",
1242 xevent->xproperty.window));
1244 if (_gdk_selection_filter_clear_event (&xevent->xselectionclear))
1246 event->selection.type = GDK_SELECTION_CLEAR;
1247 event->selection.window = window;
1248 event->selection.selection = xevent->xselectionclear.selection;
1249 event->selection.time = xevent->xselectionclear.time;
1256 case SelectionRequest:
1258 g_message ("selection request:\twindow: %ld",
1259 xevent->xproperty.window));
1261 event->selection.type = GDK_SELECTION_REQUEST;
1262 event->selection.window = window;
1263 event->selection.selection = xevent->xselectionrequest.selection;
1264 event->selection.target = xevent->xselectionrequest.target;
1265 event->selection.property = xevent->xselectionrequest.property;
1266 event->selection.requestor = xevent->xselectionrequest.requestor;
1267 event->selection.time = xevent->xselectionrequest.time;
1271 case SelectionNotify:
1273 g_message ("selection notify:\twindow: %ld",
1274 xevent->xproperty.window));
1277 event->selection.type = GDK_SELECTION_NOTIFY;
1278 event->selection.window = window;
1279 event->selection.selection = xevent->xselection.selection;
1280 event->selection.target = xevent->xselection.target;
1281 event->selection.property = xevent->xselection.property;
1282 event->selection.time = xevent->xselection.time;
1286 case ColormapNotify:
1288 g_message ("colormap notify:\twindow: %ld",
1289 xevent->xcolormap.window));
1291 /* Not currently handled */
1298 GdkFilterReturn result = GDK_FILTER_CONTINUE;
1301 g_message ("client message:\twindow: %ld",
1302 xevent->xclient.window));
1304 tmp_list = client_filters;
1307 GdkClientFilter *filter = tmp_list->data;
1308 if (filter->type == xevent->xclient.message_type)
1310 result = (*filter->function) (xevent, event, filter->data);
1314 tmp_list = tmp_list->next;
1319 case GDK_FILTER_REMOVE:
1322 case GDK_FILTER_TRANSLATE:
1325 case GDK_FILTER_CONTINUE:
1326 /* Send unknown ClientMessage's on to Gtk for it to use */
1327 event->client.type = GDK_CLIENT_EVENT;
1328 event->client.window = window;
1329 event->client.message_type = xevent->xclient.message_type;
1330 event->client.data_format = xevent->xclient.format;
1331 memcpy(&event->client.data, &xevent->xclient.data,
1332 sizeof(event->client.data));
1340 g_message ("mapping notify"));
1342 /* Let XLib know that there is a new keyboard mapping.
1344 XRefreshKeyboardMapping (&xevent->xmapping);
1345 ++_gdk_keymap_serial;
1351 ++_gdk_keymap_serial;
1357 /* something else - (e.g., a Xinput event) */
1359 if (window_private &&
1360 !GDK_WINDOW_DESTROYED (window_private) &&
1361 (window_private->extension_events != 0))
1362 return_val = _gdk_input_other_event(event, xevent, window);
1372 if (event->any.window)
1373 gdk_window_ref (event->any.window);
1374 if (((event->any.type == GDK_ENTER_NOTIFY) ||
1375 (event->any.type == GDK_LEAVE_NOTIFY)) &&
1376 (event->crossing.subwindow != NULL))
1377 gdk_window_ref (event->crossing.subwindow);
1381 /* Mark this event as having no resources to be freed */
1382 event->any.window = NULL;
1383 event->any.type = GDK_NOTHING;
1387 gdk_window_unref (window);
1393 gdk_wm_protocols_filter (GdkXEvent *xev,
1397 XEvent *xevent = (XEvent *)xev;
1399 if ((Atom) xevent->xclient.data.l[0] == gdk_wm_delete_window)
1401 /* The delete window request specifies a window
1402 * to delete. We don't actually destroy the
1403 * window because "it is only a request". (The
1404 * window might contain vital data that the
1405 * program does not want destroyed). Instead
1406 * the event is passed along to the program,
1407 * which should then destroy the window.
1410 g_message ("delete window:\t\twindow: %ld",
1411 xevent->xclient.window));
1413 event->any.type = GDK_DELETE;
1415 return GDK_FILTER_TRANSLATE;
1417 else if ((Atom) xevent->xclient.data.l[0] == gdk_wm_take_focus)
1420 else if ((Atom) xevent->xclient.data.l[0] == gdk_atom_intern ("_NET_WM_PING", FALSE))
1422 XEvent xev = *xevent;
1424 xev.xclient.window = gdk_root_window;
1425 XSendEvent (gdk_display, gdk_root_window, False, SubstructureRedirectMask | SubstructureNotifyMask, &xev);
1428 return GDK_FILTER_REMOVE;
1433 gdk_event_get_type (Display *display,
1440 if (gdk_event_translate (&event, xevent, FALSE))
1442 pred = (GdkPredicate*) arg;
1443 return (* pred->func) (&event, pred->data);
1451 gdk_events_queue (void)
1457 while (!gdk_event_queue_find_first() && XPending (gdk_display))
1462 XNextEvent (gdk_display, &xevent);
1464 switch (xevent.type)
1470 w = GDK_WINDOW_XWINDOW (gdk_xim_window);
1474 if (XFilterEvent (&xevent, w))
1477 XNextEvent (gdk_display, &xevent);
1480 switch (xevent.type)
1486 if (XFilterEvent (&xevent, None))
1490 event = gdk_event_new ();
1492 event->any.type = GDK_NOTHING;
1493 event->any.window = NULL;
1494 event->any.send_event = xevent.xany.send_event ? TRUE : FALSE;
1496 ((GdkEventPrivate *)event)->flags |= GDK_EVENT_PENDING;
1498 gdk_event_queue_append (event);
1499 node = gdk_queued_tail;
1501 if (gdk_event_translate (event, &xevent, FALSE))
1503 ((GdkEventPrivate *)event)->flags &= ~GDK_EVENT_PENDING;
1507 gdk_event_queue_remove_link (node);
1508 g_list_free_1 (node);
1509 gdk_event_free (event);
1515 gdk_event_prepare (GSource *source,
1520 GDK_THREADS_ENTER ();
1524 retval = (gdk_event_queue_find_first () != NULL) || XPending (gdk_display);
1526 GDK_THREADS_LEAVE ();
1532 gdk_event_check (GSource *source)
1536 GDK_THREADS_ENTER ();
1538 if (event_poll_fd.revents & G_IO_IN)
1539 retval = (gdk_event_queue_find_first () != NULL) || XPending (gdk_display);
1543 GDK_THREADS_LEAVE ();
1549 gdk_event_dispatch (GSource *source,
1550 GSourceFunc callback,
1555 GDK_THREADS_ENTER ();
1558 event = gdk_event_unqueue();
1563 (*gdk_event_func) (event, gdk_event_data);
1565 gdk_event_free (event);
1568 GDK_THREADS_LEAVE ();
1573 /* Sends a ClientMessage to all toplevel client windows */
1575 gdk_event_send_client_message (GdkEvent *event, guint32 xid)
1579 g_return_val_if_fail(event != NULL, FALSE);
1581 /* Set up our event to send, with the exception of its target window */
1582 sev.xclient.type = ClientMessage;
1583 sev.xclient.display = gdk_display;
1584 sev.xclient.format = event->client.data_format;
1585 sev.xclient.window = xid;
1586 memcpy(&sev.xclient.data, &event->client.data, sizeof(sev.xclient.data));
1587 sev.xclient.message_type = event->client.message_type;
1589 return gdk_send_xevent (xid, False, NoEventMask, &sev);
1592 /* Sends a ClientMessage to all toplevel client windows */
1594 gdk_event_send_client_message_to_all_recurse (XEvent *xev,
1598 static GdkAtom wm_state_atom = GDK_NONE;
1601 unsigned long nitems, after;
1602 unsigned char *data;
1603 Window *ret_children, ret_root, ret_parent;
1604 unsigned int ret_nchildren;
1605 gint old_warnings = gdk_error_warnings;
1606 gboolean send = FALSE;
1607 gboolean found = FALSE;
1611 wm_state_atom = gdk_atom_intern ("WM_STATE", FALSE);
1613 gdk_error_warnings = FALSE;
1615 XGetWindowProperty (gdk_display, xid, wm_state_atom, 0, 0, False, AnyPropertyType,
1616 &type, &format, &nitems, &after, &data);
1620 gdk_error_warnings = old_warnings;
1632 /* OK, we're all set, now let's find some windows to send this to */
1633 if (XQueryTree (gdk_display, xid, &ret_root, &ret_parent,
1634 &ret_children, &ret_nchildren) != True ||
1637 gdk_error_warnings = old_warnings;
1642 for(i = 0; i < ret_nchildren; i++)
1643 if (gdk_event_send_client_message_to_all_recurse (xev, ret_children[i], level + 1))
1646 XFree (ret_children);
1649 if (send || (!found && (level == 1)))
1651 xev->xclient.window = xid;
1652 gdk_send_xevent (xid, False, NoEventMask, xev);
1655 gdk_error_warnings = old_warnings;
1657 return (send || found);
1661 gdk_event_send_clientmessage_toall (GdkEvent *event)
1664 gint old_warnings = gdk_error_warnings;
1666 g_return_if_fail(event != NULL);
1668 /* Set up our event to send, with the exception of its target window */
1669 sev.xclient.type = ClientMessage;
1670 sev.xclient.display = gdk_display;
1671 sev.xclient.format = event->client.data_format;
1672 memcpy(&sev.xclient.data, &event->client.data, sizeof(sev.xclient.data));
1673 sev.xclient.message_type = event->client.message_type;
1675 gdk_event_send_client_message_to_all_recurse(&sev, gdk_root_window, 0);
1677 gdk_error_warnings = old_warnings;
1681 *--------------------------------------------------------------
1684 * Flushes the Xlib output buffer and then waits
1685 * until all requests have been received and processed
1686 * by the X server. The only real use for this function
1687 * is in dealing with XShm.
1695 *--------------------------------------------------------------
1701 XSync (gdk_display, False);
1704 static GdkAtom timestamp_prop_atom = 0;
1707 timestamp_predicate (Display *display,
1711 Window xwindow = GPOINTER_TO_UINT (arg);
1713 if (xevent->type == PropertyNotify &&
1714 xevent->xproperty.window == xwindow &&
1715 xevent->xproperty.atom == timestamp_prop_atom)
1722 * gdk_x11_get_server_time:
1723 * @window: a #GdkWindow, used for communication with the server.
1724 * The window must have GDK_PROPERTY_CHANGE_MASK in its
1725 * events mask or a hang will result.
1727 * Routine to get the current X server time stamp.
1729 * Return value: the time stamp.
1732 gdk_x11_get_server_time (GdkWindow *window)
1739 g_return_val_if_fail (GDK_IS_WINDOW (window), 0);
1740 g_return_val_if_fail (!GDK_WINDOW_DESTROYED (window), 0);
1742 if (!timestamp_prop_atom)
1743 timestamp_prop_atom = gdk_atom_intern ("GDK_TIMESTAMP_PROP", FALSE);
1745 xdisplay = GDK_WINDOW_XDISPLAY (window);
1746 xwindow = GDK_WINDOW_XWINDOW (window);
1748 XChangeProperty (xdisplay, xwindow,
1749 timestamp_prop_atom, timestamp_prop_atom,
1750 8, PropModeReplace, &c, 1);
1752 XIfEvent (xdisplay, &xevent,
1753 timestamp_predicate, GUINT_TO_POINTER(xwindow));
1755 return xevent.xproperty.time;
1760 gdk_net_wm_supports (GdkAtom property)
1762 static GdkAtom wmspec_check_atom = 0;
1763 static GdkAtom wmspec_supported_atom = 0;
1764 static GdkAtom *atoms = NULL;
1765 static gulong n_atoms = 0;
1773 if (wmspec_check_window != None)
1781 if (atoms[i] == property)
1796 /* This function is very slow on every call if you are not running a
1797 * spec-supporting WM. For now not optimized, because it isn't in
1798 * any critical code paths, but if you used it somewhere that had to
1799 * be fast you want to avoid "GTK is slow with old WMs" complaints.
1800 * Probably at that point the function should be changed to query
1801 * _NET_SUPPORTING_WM_CHECK only once every 10 seconds or something.
1804 if (wmspec_check_atom == 0)
1805 wmspec_check_atom = gdk_atom_intern ("_NET_SUPPORTING_WM_CHECK", FALSE);
1807 if (wmspec_supported_atom == 0)
1808 wmspec_supported_atom = gdk_atom_intern ("_NET_SUPPORTED", FALSE);
1810 XGetWindowProperty (gdk_display, gdk_root_window,
1811 wmspec_check_atom, 0, G_MAXLONG,
1812 False, XA_WINDOW, &type, &format, &nitems,
1813 &bytes_after, (guchar **)&xwindow);
1815 if (type != XA_WINDOW)
1818 gdk_error_trap_push ();
1820 /* Find out if this WM goes away, so we can reset everything. */
1821 XSelectInput (gdk_display, *xwindow,
1822 StructureNotifyMask);
1826 if (gdk_error_trap_pop ())
1832 XGetWindowProperty (gdk_display, gdk_root_window,
1833 wmspec_supported_atom, 0, G_MAXLONG,
1834 False, XA_ATOM, &type, &format, &n_atoms,
1835 &bytes_after, (guchar **)&atoms);
1837 if (type != XA_ATOM)
1840 wmspec_check_window = *xwindow;
1843 /* since wmspec_check_window != None this isn't infinite. ;-) */
1844 return gdk_net_wm_supports (property);