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 Library 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 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library 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-1999. 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.h"
31 #include "gdkkeysyms.h"
40 #include "gdkinputprivate.h"
42 typedef struct _GdkIOClosure GdkIOClosure;
43 typedef struct _GdkEventPrivate GdkEventPrivate;
45 #define DOUBLE_CLICK_TIME 250
46 #define TRIPLE_CLICK_TIME 500
47 #define DOUBLE_CLICK_DIST 5
48 #define TRIPLE_CLICK_DIST 5
52 /* Following flag is set for events on the event queue during
53 * translation and cleared afterwards.
55 GDK_EVENT_PENDING = 1 << 0
60 GdkInputFunction function;
61 GdkInputCondition condition;
62 GdkDestroyNotify notify;
66 struct _GdkEventPrivate
73 * Private function declarations
76 static gint gdk_event_apply_filters (XEvent *xevent,
79 static gint gdk_event_translate (GdkEvent *event,
82 static Bool gdk_event_get_type (Display *display,
87 static gboolean gdk_event_prepare (gpointer source_data,
88 GTimeVal *current_time,
91 static gboolean gdk_event_check (gpointer source_data,
92 GTimeVal *current_time,
94 static gboolean gdk_event_dispatch (gpointer source_data,
95 GTimeVal *current_time,
98 GdkFilterReturn gdk_wm_protocols_filter (GdkXEvent *xev,
102 /* Private variable declarations
105 static int connection_number = 0; /* The file descriptor number of our
106 * connection to the X server. This
107 * is used so that we may determine
108 * when events are pending by using
109 * the "select" system call.
111 static GList *client_filters; /* Filters for client messages */
113 static GSourceFuncs event_funcs = {
117 (GDestroyNotify)g_free
120 GPollFD event_poll_fd;
122 /*********************************************
123 * Functions for maintaining the event queue *
124 *********************************************/
127 gdk_events_init (void)
129 connection_number = ConnectionNumber (gdk_display);
131 g_message ("connection number: %d", connection_number));
133 g_source_add (GDK_PRIORITY_EVENTS, TRUE, &event_funcs, NULL, NULL, NULL);
135 event_poll_fd.fd = connection_number;
136 event_poll_fd.events = G_IO_IN;
138 g_main_add_poll (&event_poll_fd, GDK_PRIORITY_EVENTS);
140 gdk_add_client_message_filter (gdk_wm_protocols,
141 gdk_wm_protocols_filter, NULL);
145 *--------------------------------------------------------------
148 * Returns if events are pending on the queue.
153 * Returns TRUE if events are pending
157 *--------------------------------------------------------------
161 gdk_events_pending (void)
163 return (gdk_event_queue_find_first() || XPending (gdk_display));
167 *--------------------------------------------------------------
168 * gdk_event_get_graphics_expose
170 * Waits for a GraphicsExpose or NoExpose event
175 * For GraphicsExpose events, returns a pointer to the event
176 * converted into a GdkEvent Otherwise, returns NULL.
180 *-------------------------------------------------------------- */
183 graphics_expose_predicate (Display *display,
187 if (xevent->xany.window == GDK_DRAWABLE_XID (arg) &&
188 (xevent->xany.type == GraphicsExpose ||
189 xevent->xany.type == NoExpose))
196 gdk_event_get_graphics_expose (GdkWindow *window)
201 g_return_val_if_fail (window != NULL, NULL);
203 XIfEvent (gdk_display, &xevent, graphics_expose_predicate, (XPointer) window);
205 if (xevent.xany.type == GraphicsExpose)
207 event = gdk_event_new ();
209 if (gdk_event_translate (event, &xevent))
212 gdk_event_free (event);
218 /************************
219 * Exposure compression *
220 ************************/
223 * The following implements simple exposure compression. It is
224 * modelled after the way Xt does exposure compression - in
225 * particular compress_expose = XtExposeCompressMultiple.
226 * It compress consecutive sequences of exposure events,
227 * but not sequences that cross other events. (This is because
228 * if it crosses a ConfigureNotify, we could screw up and
229 * mistakenly compress the exposures generated for the new
230 * size - could we just check for ConfigureNotify?)
232 * Xt compresses to a region / bounding rectangle, we compress
233 * to two rectangles, and try find the two rectangles of minimal
234 * area for this - this is supposed to handle the typical
235 * L-shaped regions generated by OpaqueMove.
238 /* Given three rectangles, find the two rectangles that cover
239 * them with the smallest area.
242 gdk_add_rect_to_rects (GdkRectangle *rect1,
244 GdkRectangle *new_rect)
246 GdkRectangle t1, t2, t3;
247 gint size1, size2, size3;
249 gdk_rectangle_union (rect1, rect2, &t1);
250 gdk_rectangle_union (rect1, new_rect, &t2);
251 gdk_rectangle_union (rect2, new_rect, &t3);
253 size1 = t1.width * t1.height + new_rect->width * new_rect->height;
254 size2 = t2.width * t2.height + rect2->width * rect2->height;
255 size3 = t1.width * t1.height + rect1->width * rect1->height;
276 typedef struct _GdkExposeInfo GdkExposeInfo;
278 struct _GdkExposeInfo
281 gboolean seen_nonmatching;
285 expose_predicate (Display *display,
289 GdkExposeInfo *info = (GdkExposeInfo*) arg;
291 /* Compressing across GravityNotify events is safe, because
292 * we completely ignore them, so they can't change what
293 * we are going to draw. Compressing across GravityNotify
294 * events is necessay because during window-unshading animation
295 * we'll get a whole bunch of them interspersed with
298 if (xevent->xany.type != Expose &&
299 xevent->xany.type != GravityNotify)
301 info->seen_nonmatching = TRUE;
304 if (info->seen_nonmatching ||
305 xevent->xany.type != Expose ||
306 xevent->xany.window != info->window)
313 gdk_compress_exposures (XEvent *xevent,
320 GdkRectangle tmp_rect;
322 GdkFilterReturn result;
326 info.window = xevent->xany.window;
327 info.seen_nonmatching = FALSE;
329 rect1.x = xevent->xexpose.x;
330 rect1.y = xevent->xexpose.y;
331 rect1.width = xevent->xexpose.width;
332 rect1.height = xevent->xexpose.height;
334 event.any.type = GDK_EXPOSE;
335 event.any.window = None;
336 event.any.send_event = FALSE;
342 if (!XCheckIfEvent (gdk_display,
349 XIfEvent (gdk_display,
354 event.any.window = window;
356 /* We apply filters here, and if it was filtered, completely
359 result = gdk_event_apply_filters (xevent, &event,
361 ((GdkWindowPrivate *)window)->filters
362 : gdk_default_filters);
364 if (result != GDK_FILTER_CONTINUE)
366 if (result == GDK_FILTER_TRANSLATE)
367 gdk_event_put (&event);
373 rect2.x = tmp_event.xexpose.x;
374 rect2.y = tmp_event.xexpose.y;
375 rect2.width = tmp_event.xexpose.width;
376 rect2.height = tmp_event.xexpose.height;
382 tmp_rect.x = tmp_event.xexpose.x;
383 tmp_rect.y = tmp_event.xexpose.y;
384 tmp_rect.width = tmp_event.xexpose.width;
385 tmp_rect.height = tmp_event.xexpose.height;
387 gdk_add_rect_to_rects (&rect1, &rect2, &tmp_rect);
390 count = tmp_event.xexpose.count;
395 gdk_rectangle_union (&rect1, &rect2, &tmp_rect);
397 if ((tmp_rect.width * tmp_rect.height) <
398 2 * (rect1.height * rect1.width +
399 rect2.height * rect2.width))
408 event.expose.type = GDK_EXPOSE;
409 event.expose.window = window;
410 event.expose.area.x = rect2.x;
411 event.expose.area.y = rect2.y;
412 event.expose.area.width = rect2.width;
413 event.expose.area.height = rect2.height;
414 event.expose.count = 0;
416 gdk_event_put (&event);
419 xevent->xexpose.count = nrects - 1;
420 xevent->xexpose.x = rect1.x;
421 xevent->xexpose.y = rect1.y;
422 xevent->xexpose.width = rect1.width;
423 xevent->xexpose.height = rect1.height;
427 gdk_event_apply_filters (XEvent *xevent,
432 GdkFilterReturn result;
438 GdkEventFilter *filter = (GdkEventFilter*) tmp_list->data;
440 tmp_list = tmp_list->next;
441 result = filter->function (xevent, event, filter->data);
442 if (result != GDK_FILTER_CONTINUE)
446 return GDK_FILTER_CONTINUE;
450 gdk_add_client_message_filter (GdkAtom message_type,
454 GdkClientFilter *filter = g_new (GdkClientFilter, 1);
456 filter->type = message_type;
457 filter->function = func;
460 client_filters = g_list_prepend (client_filters, filter);
464 gdk_event_translate (GdkEvent *event,
469 GdkWindowPrivate *window_private;
470 static XComposeStatus compose;
474 static gchar* buf = NULL;
475 static gint buf_len= 0;
483 /* Find the GdkWindow that this event occurred in.
485 * We handle events with window=None
486 * specially - they are generated by XFree86's XInput under
487 * some circumstances.
490 if ((xevent->xany.window == None) &&
491 gdk_input_vtable.window_none_event)
493 return_val = gdk_input_vtable.window_none_event (event,xevent);
495 if (return_val >= 0) /* was handled */
501 window = gdk_window_lookup (xevent->xany.window);
502 /* FIXME: window might be a GdkPixmap!!! */
504 window_private = (GdkWindowPrivate *) window;
507 gdk_window_ref (window);
509 event->any.window = window;
510 event->any.send_event = xevent->xany.send_event ? TRUE : FALSE;
512 if (window_private && GDK_DRAWABLE_DESTROYED (window))
514 if (xevent->type != DestroyNotify)
519 /* Check for filters for this window
521 GdkFilterReturn result;
522 result = gdk_event_apply_filters (xevent, event,
524 ?window_private->filters
525 :gdk_default_filters);
527 if (result != GDK_FILTER_CONTINUE)
529 return (result == GDK_FILTER_TRANSLATE) ? TRUE : FALSE;
534 if (window == NULL && gdk_xim_window && xevent->type == KeyPress &&
535 !GDK_DRAWABLE_DESTROYED (gdk_xim_window))
538 * If user presses a key in Preedit or Status window, keypress event
539 * is sometimes sent to these windows. These windows are not managed
540 * by GDK, so we redirect KeyPress event to xim_window.
542 * If someone want to use the window whitch is not managed by GDK
543 * and want to get KeyPress event, he/she must register the filter
544 * function to gdk_default_filters to intercept the event.
547 GdkFilterReturn result;
549 window = gdk_xim_window;
550 window_private = (GdkWindowPrivate *) window;
551 gdk_window_ref (window);
552 event->any.window = window;
555 g_message ("KeyPress event is redirected to xim_window: %#lx",
556 xevent->xany.window));
558 result = gdk_event_apply_filters (xevent, event,
559 window_private->filters);
560 if (result != GDK_FILTER_CONTINUE)
561 return (result == GDK_FILTER_TRANSLATE) ? TRUE : FALSE;
565 /* We do a "manual" conversion of the XEvent to a
566 * GdkEvent. The structures are mostly the same so
567 * the conversion is fairly straightforward. We also
568 * optionally print debugging info regarding events
574 switch (xevent->type)
577 /* Lookup the string corresponding to the given keysym.
584 buf = g_new (gchar, buf_len);
586 keysym = GDK_VoidSymbol;
588 if (gdk_xim_ic && gdk_xim_ic->xic)
592 /* Clear keyval. Depending on status, may not be set */
593 charcount = XmbLookupString(gdk_xim_ic->xic,
594 &xevent->xkey, buf, buf_len-1,
596 if (status == XBufferOverflow)
598 /* alloc adequate size of buffer */
600 g_message("XIM: overflow (required %i)", charcount));
602 while (buf_len <= charcount)
604 buf = (gchar *) g_realloc (buf, buf_len);
606 charcount = XmbLookupString (gdk_xim_ic->xic,
607 &xevent->xkey, buf, buf_len-1,
610 if (status == XLookupNone)
617 charcount = XLookupString (&xevent->xkey, buf, buf_len,
620 charcount = XLookupString (&xevent->xkey, buf, 16,
623 event->key.keyval = keysym;
625 if (charcount > 0 && buf[charcount-1] == '\0')
628 buf[charcount] = '\0';
630 /* Print debugging info. */
632 #ifdef G_ENABLE_DEBUG
633 if (gdk_debug_flags & GDK_DEBUG_EVENTS)
635 g_message ("key press:\twindow: %ld key: %12s %d",
637 event->key.keyval ? XKeysymToString (event->key.keyval) : "(none)",
640 g_message ("\t\tlength: %4d string: \"%s\"",
643 #endif /* G_ENABLE_DEBUG */
645 event->key.type = GDK_KEY_PRESS;
646 event->key.window = window;
647 event->key.time = xevent->xkey.time;
648 event->key.state = (GdkModifierType) xevent->xkey.state;
649 event->key.string = g_strdup (buf);
650 event->key.length = charcount;
655 /* Lookup the string corresponding to the given keysym.
661 buf = g_new (gchar, buf_len);
664 keysym = GDK_VoidSymbol;
665 charcount = XLookupString (&xevent->xkey, buf, 16,
667 event->key.keyval = keysym;
669 /* Print debugging info.
672 g_message ("key release:\t\twindow: %ld key: %12s %d",
674 XKeysymToString (event->key.keyval),
677 event->key.type = GDK_KEY_RELEASE;
678 event->key.window = window;
679 event->key.time = xevent->xkey.time;
680 event->key.state = (GdkModifierType) xevent->xkey.state;
681 event->key.length = 0;
682 event->key.string = NULL;
687 /* Print debugging info.
690 g_message ("button press:\t\twindow: %ld x,y: %d %d button: %d",
691 xevent->xbutton.window,
692 xevent->xbutton.x, xevent->xbutton.y,
693 xevent->xbutton.button));
695 if (window_private &&
696 (window_private->extension_events != 0) &&
697 gdk_input_ignore_core)
703 event->button.type = GDK_BUTTON_PRESS;
704 event->button.window = window;
705 event->button.time = xevent->xbutton.time;
706 event->button.x = xevent->xbutton.x;
707 event->button.y = xevent->xbutton.y;
708 event->button.x_root = (gfloat)xevent->xbutton.x_root;
709 event->button.y_root = (gfloat)xevent->xbutton.y_root;
710 event->button.pressure = 0.5;
711 event->button.xtilt = 0;
712 event->button.ytilt = 0;
713 event->button.state = (GdkModifierType) xevent->xbutton.state;
714 event->button.button = xevent->xbutton.button;
715 event->button.source = GDK_SOURCE_MOUSE;
716 event->button.deviceid = GDK_CORE_POINTER;
718 gdk_event_button_generate (event);
723 /* Print debugging info.
726 g_message ("button release:\twindow: %ld x,y: %d %d button: %d",
727 xevent->xbutton.window,
728 xevent->xbutton.x, xevent->xbutton.y,
729 xevent->xbutton.button));
731 if (window_private &&
732 (window_private->extension_events != 0) &&
733 gdk_input_ignore_core)
739 event->button.type = GDK_BUTTON_RELEASE;
740 event->button.window = window;
741 event->button.time = xevent->xbutton.time;
742 event->button.x = xevent->xbutton.x;
743 event->button.y = xevent->xbutton.y;
744 event->button.x_root = (gfloat)xevent->xbutton.x_root;
745 event->button.y_root = (gfloat)xevent->xbutton.y_root;
746 event->button.pressure = 0.5;
747 event->button.xtilt = 0;
748 event->button.ytilt = 0;
749 event->button.state = (GdkModifierType) xevent->xbutton.state;
750 event->button.button = xevent->xbutton.button;
751 event->button.source = GDK_SOURCE_MOUSE;
752 event->button.deviceid = GDK_CORE_POINTER;
757 /* Print debugging info.
760 g_message ("motion notify:\t\twindow: %ld x,y: %d %d hint: %s",
761 xevent->xmotion.window,
762 xevent->xmotion.x, xevent->xmotion.y,
763 (xevent->xmotion.is_hint) ? "true" : "false"));
765 if (window_private &&
766 (window_private->extension_events != 0) &&
767 gdk_input_ignore_core)
773 event->motion.type = GDK_MOTION_NOTIFY;
774 event->motion.window = window;
775 event->motion.time = xevent->xmotion.time;
776 event->motion.x = xevent->xmotion.x;
777 event->motion.y = xevent->xmotion.y;
778 event->motion.x_root = (gfloat)xevent->xmotion.x_root;
779 event->motion.y_root = (gfloat)xevent->xmotion.y_root;
780 event->motion.pressure = 0.5;
781 event->motion.xtilt = 0;
782 event->motion.ytilt = 0;
783 event->motion.state = (GdkModifierType) xevent->xmotion.state;
784 event->motion.is_hint = xevent->xmotion.is_hint;
785 event->motion.source = GDK_SOURCE_MOUSE;
786 event->motion.deviceid = GDK_CORE_POINTER;
791 /* Print debugging info.
794 g_message ("enter notify:\t\twindow: %ld detail: %d subwin: %ld",
795 xevent->xcrossing.window,
796 xevent->xcrossing.detail,
797 xevent->xcrossing.subwindow));
799 /* Tell XInput stuff about it if appropriate */
800 if (window_private &&
801 !GDK_DRAWABLE_DESTROYED (window) &&
802 (window_private->extension_events != 0) &&
803 gdk_input_vtable.enter_event)
804 gdk_input_vtable.enter_event (&xevent->xcrossing, window);
806 event->crossing.type = GDK_ENTER_NOTIFY;
807 event->crossing.window = window;
809 /* If the subwindow field of the XEvent is non-NULL, then
810 * lookup the corresponding GdkWindow.
812 if (xevent->xcrossing.subwindow != None)
813 event->crossing.subwindow = gdk_window_lookup (xevent->xcrossing.subwindow);
815 event->crossing.subwindow = NULL;
817 event->crossing.time = xevent->xcrossing.time;
818 event->crossing.x = xevent->xcrossing.x;
819 event->crossing.y = xevent->xcrossing.y;
820 event->crossing.x_root = xevent->xcrossing.x_root;
821 event->crossing.y_root = xevent->xcrossing.y_root;
823 /* Translate the crossing mode into Gdk terms.
825 switch (xevent->xcrossing.mode)
828 event->crossing.mode = GDK_CROSSING_NORMAL;
831 event->crossing.mode = GDK_CROSSING_GRAB;
834 event->crossing.mode = GDK_CROSSING_UNGRAB;
838 /* Translate the crossing detail into Gdk terms.
840 switch (xevent->xcrossing.detail)
843 event->crossing.detail = GDK_NOTIFY_INFERIOR;
846 event->crossing.detail = GDK_NOTIFY_ANCESTOR;
849 event->crossing.detail = GDK_NOTIFY_VIRTUAL;
851 case NotifyNonlinear:
852 event->crossing.detail = GDK_NOTIFY_NONLINEAR;
854 case NotifyNonlinearVirtual:
855 event->crossing.detail = GDK_NOTIFY_NONLINEAR_VIRTUAL;
858 event->crossing.detail = GDK_NOTIFY_UNKNOWN;
862 event->crossing.focus = xevent->xcrossing.focus;
863 event->crossing.state = xevent->xcrossing.state;
868 /* Print debugging info.
871 g_message ("leave notify:\t\twindow: %ld detail: %d subwin: %ld",
872 xevent->xcrossing.window,
873 xevent->xcrossing.detail, xevent->xcrossing.subwindow));
875 event->crossing.type = GDK_LEAVE_NOTIFY;
876 event->crossing.window = window;
878 /* If the subwindow field of the XEvent is non-NULL, then
879 * lookup the corresponding GdkWindow.
881 if (xevent->xcrossing.subwindow != None)
882 event->crossing.subwindow = gdk_window_lookup (xevent->xcrossing.subwindow);
884 event->crossing.subwindow = NULL;
886 event->crossing.time = xevent->xcrossing.time;
887 event->crossing.x = xevent->xcrossing.x;
888 event->crossing.y = xevent->xcrossing.y;
889 event->crossing.x_root = xevent->xcrossing.x_root;
890 event->crossing.y_root = xevent->xcrossing.y_root;
892 /* Translate the crossing mode into Gdk terms.
894 switch (xevent->xcrossing.mode)
897 event->crossing.mode = GDK_CROSSING_NORMAL;
900 event->crossing.mode = GDK_CROSSING_GRAB;
903 event->crossing.mode = GDK_CROSSING_UNGRAB;
907 /* Translate the crossing detail into Gdk terms.
909 switch (xevent->xcrossing.detail)
912 event->crossing.detail = GDK_NOTIFY_INFERIOR;
915 event->crossing.detail = GDK_NOTIFY_ANCESTOR;
918 event->crossing.detail = GDK_NOTIFY_VIRTUAL;
920 case NotifyNonlinear:
921 event->crossing.detail = GDK_NOTIFY_NONLINEAR;
923 case NotifyNonlinearVirtual:
924 event->crossing.detail = GDK_NOTIFY_NONLINEAR_VIRTUAL;
927 event->crossing.detail = GDK_NOTIFY_UNKNOWN;
931 event->crossing.focus = xevent->xcrossing.focus;
932 event->crossing.state = xevent->xcrossing.state;
938 /* We only care about focus events that indicate that _this_
939 * window (not a ancestor or child) got or lost the focus
941 switch (xevent->xfocus.detail)
945 case NotifyNonlinear:
946 /* Print debugging info.
949 g_message ("focus %s:\t\twindow: %ld",
950 (xevent->xany.type == FocusIn) ? "in" : "out",
951 xevent->xfocus.window));
953 /* gdk_keyboard_grab() causes following events. These events confuse
954 * the XIM focus, so ignore them.
956 if (xevent->xfocus.mode == NotifyGrab ||
957 xevent->xfocus.mode == NotifyUngrab)
960 event->focus_change.type = GDK_FOCUS_CHANGE;
961 event->focus_change.window = window;
962 event->focus_change.in = (xevent->xany.type == FocusIn);
971 /* Print debugging info.
974 g_message ("keymap notify"));
976 /* Not currently handled */
981 /* Print debugging info.
984 g_message ("expose:\t\twindow: %ld %d x,y: %d %d w,h: %d %d%s",
985 xevent->xexpose.window, xevent->xexpose.count,
986 xevent->xexpose.x, xevent->xexpose.y,
987 xevent->xexpose.width, xevent->xexpose.height,
988 event->any.send_event ? " (send)" : ""));
989 gdk_compress_exposures (xevent, window);
991 event->expose.type = GDK_EXPOSE;
992 event->expose.window = window;
993 event->expose.area.x = xevent->xexpose.x;
994 event->expose.area.y = xevent->xexpose.y;
995 event->expose.area.width = xevent->xexpose.width;
996 event->expose.area.height = xevent->xexpose.height;
997 event->expose.count = xevent->xexpose.count;
1001 case GraphicsExpose:
1002 /* Print debugging info.
1005 g_message ("graphics expose:\tdrawable: %ld",
1006 xevent->xgraphicsexpose.drawable));
1008 event->expose.type = GDK_EXPOSE;
1009 event->expose.window = window;
1010 event->expose.area.x = xevent->xgraphicsexpose.x;
1011 event->expose.area.y = xevent->xgraphicsexpose.y;
1012 event->expose.area.width = xevent->xgraphicsexpose.width;
1013 event->expose.area.height = xevent->xgraphicsexpose.height;
1014 event->expose.count = xevent->xexpose.count;
1019 /* Print debugging info.
1022 g_message ("no expose:\t\tdrawable: %ld",
1023 xevent->xnoexpose.drawable));
1025 event->no_expose.type = GDK_NO_EXPOSE;
1026 event->no_expose.window = window;
1030 case VisibilityNotify:
1031 /* Print debugging info.
1033 #ifdef G_ENABLE_DEBUG
1034 if (gdk_debug_flags & GDK_DEBUG_EVENTS)
1035 switch (xevent->xvisibility.state)
1037 case VisibilityFullyObscured:
1038 g_message ("visibility notify:\twindow: %ld none",
1039 xevent->xvisibility.window);
1041 case VisibilityPartiallyObscured:
1042 g_message ("visibility notify:\twindow: %ld partial",
1043 xevent->xvisibility.window);
1045 case VisibilityUnobscured:
1046 g_message ("visibility notify:\twindow: %ld full",
1047 xevent->xvisibility.window);
1050 #endif /* G_ENABLE_DEBUG */
1052 event->visibility.type = GDK_VISIBILITY_NOTIFY;
1053 event->visibility.window = window;
1055 switch (xevent->xvisibility.state)
1057 case VisibilityFullyObscured:
1058 event->visibility.state = GDK_VISIBILITY_FULLY_OBSCURED;
1061 case VisibilityPartiallyObscured:
1062 event->visibility.state = GDK_VISIBILITY_PARTIAL;
1065 case VisibilityUnobscured:
1066 event->visibility.state = GDK_VISIBILITY_UNOBSCURED;
1074 g_message ("create notify:\twindow: %ld x,y: %d %d w,h: %d %d b-w: %d parent: %ld ovr: %d",
1075 xevent->xcreatewindow.window,
1076 xevent->xcreatewindow.x,
1077 xevent->xcreatewindow.y,
1078 xevent->xcreatewindow.width,
1079 xevent->xcreatewindow.height,
1080 xevent->xcreatewindow.border_width,
1081 xevent->xcreatewindow.parent,
1082 xevent->xcreatewindow.override_redirect));
1083 /* not really handled */
1087 /* Print debugging info.
1090 g_message ("destroy notify:\twindow: %ld",
1091 xevent->xdestroywindow.window));
1093 event->any.type = GDK_DESTROY;
1094 event->any.window = window;
1096 return_val = window_private && !GDK_DRAWABLE_DESTROYED (window);
1098 if (window && GDK_DRAWABLE_XID (window) != GDK_ROOT_WINDOW())
1099 gdk_window_destroy_notify (window);
1103 /* Print debugging info.
1106 g_message ("unmap notify:\t\twindow: %ld",
1107 xevent->xmap.window));
1109 event->any.type = GDK_UNMAP;
1110 event->any.window = window;
1112 if (gdk_xgrab_window == window_private)
1113 gdk_xgrab_window = NULL;
1118 /* Print debugging info.
1121 g_message ("map notify:\t\twindow: %ld",
1122 xevent->xmap.window));
1124 event->any.type = GDK_MAP;
1125 event->any.window = window;
1129 case ReparentNotify:
1130 /* Print debugging info.
1133 g_message ("reparent notify:\twindow: %ld x,y: %d %d parent: %ld ovr: %d",
1134 xevent->xreparent.window,
1135 xevent->xreparent.x,
1136 xevent->xreparent.y,
1137 xevent->xreparent.parent,
1138 xevent->xreparent.override_redirect));
1140 /* Not currently handled */
1144 case ConfigureNotify:
1145 /* Print debugging info.
1148 g_message ("configure notify:\twindow: %ld x,y: %d %d w,h: %d %d b-w: %d above: %ld ovr: %d%s",
1149 xevent->xconfigure.window,
1150 xevent->xconfigure.x,
1151 xevent->xconfigure.y,
1152 xevent->xconfigure.width,
1153 xevent->xconfigure.height,
1154 xevent->xconfigure.border_width,
1155 xevent->xconfigure.above,
1156 xevent->xconfigure.override_redirect,
1159 : GDK_DRAWABLE_TYPE (window) == GDK_WINDOW_CHILD
1160 ? " (discarding child)"
1163 !GDK_DRAWABLE_DESTROYED (window) &&
1164 (window_private->extension_events != 0) &&
1165 gdk_input_vtable.configure_event)
1166 gdk_input_vtable.configure_event (&xevent->xconfigure, window);
1168 if (!window || GDK_DRAWABLE_TYPE (window) == GDK_WINDOW_CHILD)
1172 event->configure.type = GDK_CONFIGURE;
1173 event->configure.window = window;
1174 event->configure.width = xevent->xconfigure.width;
1175 event->configure.height = xevent->xconfigure.height;
1177 if (!xevent->xconfigure.x &&
1178 !xevent->xconfigure.y &&
1179 !GDK_DRAWABLE_DESTROYED (window))
1183 Window child_window = 0;
1185 gdk_error_trap_push ();
1186 if (XTranslateCoordinates (GDK_DRAWABLE_XDISPLAY (window),
1187 GDK_DRAWABLE_XID (window),
1193 if (!gdk_error_trap_pop ())
1195 event->configure.x = tx;
1196 event->configure.y = ty;
1200 gdk_error_trap_pop ();
1204 event->configure.x = xevent->xconfigure.x;
1205 event->configure.y = xevent->xconfigure.y;
1207 window_private->x = event->configure.x;
1208 window_private->y = event->configure.y;
1209 window_private->drawable.width = xevent->xconfigure.width;
1210 window_private->drawable.height = xevent->xconfigure.height;
1211 if (window_private->resize_count > 1)
1212 window_private->resize_count -= 1;
1216 case PropertyNotify:
1217 /* Print debugging info.
1220 gchar *atom = gdk_atom_name (xevent->xproperty.atom);
1221 g_message ("property notify:\twindow: %ld, atom(%ld): %s%s%s",
1222 xevent->xproperty.window,
1223 xevent->xproperty.atom,
1225 atom ? atom : "unknown",
1230 event->property.type = GDK_PROPERTY_NOTIFY;
1231 event->property.window = window;
1232 event->property.atom = xevent->xproperty.atom;
1233 event->property.time = xevent->xproperty.time;
1234 event->property.state = xevent->xproperty.state;
1238 case SelectionClear:
1240 g_message ("selection clear:\twindow: %ld",
1241 xevent->xproperty.window));
1243 event->selection.type = GDK_SELECTION_CLEAR;
1244 event->selection.window = window;
1245 event->selection.selection = xevent->xselectionclear.selection;
1246 event->selection.time = xevent->xselectionclear.time;
1250 case SelectionRequest:
1252 g_message ("selection request:\twindow: %ld",
1253 xevent->xproperty.window));
1255 event->selection.type = GDK_SELECTION_REQUEST;
1256 event->selection.window = window;
1257 event->selection.selection = xevent->xselectionrequest.selection;
1258 event->selection.target = xevent->xselectionrequest.target;
1259 event->selection.property = xevent->xselectionrequest.property;
1260 event->selection.requestor = xevent->xselectionrequest.requestor;
1261 event->selection.time = xevent->xselectionrequest.time;
1265 case SelectionNotify:
1267 g_message ("selection notify:\twindow: %ld",
1268 xevent->xproperty.window));
1271 event->selection.type = GDK_SELECTION_NOTIFY;
1272 event->selection.window = window;
1273 event->selection.selection = xevent->xselection.selection;
1274 event->selection.target = xevent->xselection.target;
1275 event->selection.property = xevent->xselection.property;
1276 event->selection.time = xevent->xselection.time;
1280 case ColormapNotify:
1281 /* Print debugging info.
1284 g_message ("colormap notify:\twindow: %ld",
1285 xevent->xcolormap.window));
1287 /* Not currently handled */
1294 GdkFilterReturn result = GDK_FILTER_CONTINUE;
1296 /* Print debugging info.
1299 g_message ("client message:\twindow: %ld",
1300 xevent->xclient.window));
1302 tmp_list = client_filters;
1305 GdkClientFilter *filter = tmp_list->data;
1306 if (filter->type == xevent->xclient.message_type)
1308 result = (*filter->function) (xevent, event, filter->data);
1312 tmp_list = tmp_list->next;
1317 case GDK_FILTER_REMOVE:
1320 case GDK_FILTER_TRANSLATE:
1323 case GDK_FILTER_CONTINUE:
1324 /* Send unknown ClientMessage's on to Gtk for it to use */
1325 event->client.type = GDK_CLIENT_EVENT;
1326 event->client.window = window;
1327 event->client.message_type = xevent->xclient.message_type;
1328 event->client.data_format = xevent->xclient.format;
1329 memcpy(&event->client.data, &xevent->xclient.data,
1330 sizeof(event->client.data));
1337 /* Print debugging info.
1340 g_message ("mapping notify"));
1342 /* Let XLib know that there is a new keyboard mapping.
1344 XRefreshKeyboardMapping (&xevent->xmapping);
1349 /* something else - (e.g., a Xinput event) */
1351 if (window_private &&
1352 !window_private->drawable.destroyed &&
1353 (window_private->extension_events != 0) &&
1354 gdk_input_vtable.other_event)
1355 return_val = gdk_input_vtable.other_event(event, xevent, window);
1364 if (event->any.window)
1365 gdk_window_ref (event->any.window);
1366 if (((event->any.type == GDK_ENTER_NOTIFY) ||
1367 (event->any.type == GDK_LEAVE_NOTIFY)) &&
1368 (event->crossing.subwindow != NULL))
1369 gdk_window_ref (event->crossing.subwindow);
1373 /* Mark this event as having no resources to be freed */
1374 event->any.window = NULL;
1375 event->any.type = GDK_NOTHING;
1379 gdk_window_unref (window);
1385 gdk_wm_protocols_filter (GdkXEvent *xev,
1389 XEvent *xevent = (XEvent *)xev;
1391 if ((Atom) xevent->xclient.data.l[0] == gdk_wm_delete_window)
1393 /* The delete window request specifies a window
1394 * to delete. We don't actually destroy the
1395 * window because "it is only a request". (The
1396 * window might contain vital data that the
1397 * program does not want destroyed). Instead
1398 * the event is passed along to the program,
1399 * which should then destroy the window.
1402 g_message ("delete window:\t\twindow: %ld",
1403 xevent->xclient.window));
1405 event->any.type = GDK_DELETE;
1407 return GDK_FILTER_TRANSLATE;
1409 else if ((Atom) xevent->xclient.data.l[0] == gdk_wm_take_focus)
1413 return GDK_FILTER_REMOVE;
1418 gdk_event_get_type (Display *display,
1425 if (gdk_event_translate (&event, xevent))
1427 pred = (GdkPredicate*) arg;
1428 return (* pred->func) (&event, pred->data);
1436 gdk_events_queue (void)
1442 while (!gdk_event_queue_find_first() && XPending (gdk_display))
1447 XNextEvent (gdk_display, &xevent);
1449 switch (xevent.type)
1455 w = GDK_WINDOW_XWINDOW (gdk_xim_window);
1459 if (XFilterEvent (&xevent, w))
1462 XNextEvent (gdk_display, &xevent);
1465 event = gdk_event_new ();
1467 event->any.type = GDK_NOTHING;
1468 event->any.window = NULL;
1469 event->any.send_event = xevent.xany.send_event ? TRUE : FALSE;
1471 ((GdkEventPrivate *)event)->flags |= GDK_EVENT_PENDING;
1473 gdk_event_queue_append (event);
1474 node = gdk_queued_tail;
1476 if (gdk_event_translate (event, &xevent))
1478 ((GdkEventPrivate *)event)->flags &= ~GDK_EVENT_PENDING;
1482 gdk_event_queue_remove_link (node);
1483 g_list_free_1 (node);
1484 gdk_event_free (event);
1490 gdk_event_prepare (gpointer source_data,
1491 GTimeVal *current_time,
1497 GDK_THREADS_ENTER ();
1501 retval = (gdk_event_queue_find_first () != NULL) || XPending (gdk_display);
1503 GDK_THREADS_LEAVE ();
1509 gdk_event_check (gpointer source_data,
1510 GTimeVal *current_time,
1515 GDK_THREADS_ENTER ();
1517 if (event_poll_fd.revents & G_IO_IN)
1518 retval = (gdk_event_queue_find_first () != NULL) || XPending (gdk_display);
1522 GDK_THREADS_LEAVE ();
1528 gdk_event_dispatch (gpointer source_data,
1529 GTimeVal *current_time,
1534 GDK_THREADS_ENTER ();
1537 event = gdk_event_unqueue();
1542 (*gdk_event_func) (event, gdk_event_data);
1544 gdk_event_free (event);
1547 GDK_THREADS_LEAVE ();
1552 /* Sends a ClientMessage to all toplevel client windows */
1554 gdk_event_send_client_message (GdkEvent *event, guint32 xid)
1558 g_return_val_if_fail(event != NULL, FALSE);
1560 /* Set up our event to send, with the exception of its target window */
1561 sev.xclient.type = ClientMessage;
1562 sev.xclient.display = gdk_display;
1563 sev.xclient.format = event->client.data_format;
1564 sev.xclient.window = xid;
1565 memcpy(&sev.xclient.data, &event->client.data, sizeof(sev.xclient.data));
1566 sev.xclient.message_type = event->client.message_type;
1568 return gdk_send_xevent (xid, False, NoEventMask, &sev);
1571 /* Sends a ClientMessage to all toplevel client windows */
1573 gdk_event_send_client_message_to_all_recurse (XEvent *xev,
1577 static GdkAtom wm_state_atom = GDK_NONE;
1580 unsigned long nitems, after;
1581 unsigned char *data;
1582 Window *ret_children, ret_root, ret_parent;
1583 unsigned int ret_nchildren;
1584 gint old_warnings = gdk_error_warnings;
1585 gboolean send = FALSE;
1586 gboolean found = FALSE;
1590 wm_state_atom = gdk_atom_intern ("WM_STATE", FALSE);
1592 gdk_error_warnings = FALSE;
1594 XGetWindowProperty (gdk_display, xid, wm_state_atom, 0, 0, False, AnyPropertyType,
1595 &type, &format, &nitems, &after, &data);
1599 gdk_error_warnings = old_warnings;
1611 /* OK, we're all set, now let's find some windows to send this to */
1612 if (XQueryTree (gdk_display, xid, &ret_root, &ret_parent,
1613 &ret_children, &ret_nchildren) != True ||
1616 gdk_error_warnings = old_warnings;
1621 for(i = 0; i < ret_nchildren; i++)
1622 if (gdk_event_send_client_message_to_all_recurse (xev, ret_children[i], level + 1))
1625 XFree (ret_children);
1628 if (send || (!found && (level == 1)))
1630 xev->xclient.window = xid;
1631 gdk_send_xevent (xid, False, NoEventMask, xev);
1634 gdk_error_warnings = old_warnings;
1636 return (send || found);
1640 gdk_event_send_clientmessage_toall (GdkEvent *event)
1643 gint old_warnings = gdk_error_warnings;
1645 g_return_if_fail(event != NULL);
1647 /* Set up our event to send, with the exception of its target window */
1648 sev.xclient.type = ClientMessage;
1649 sev.xclient.display = gdk_display;
1650 sev.xclient.format = event->client.data_format;
1651 memcpy(&sev.xclient.data, &event->client.data, sizeof(sev.xclient.data));
1652 sev.xclient.message_type = event->client.message_type;
1654 gdk_event_send_client_message_to_all_recurse(&sev, gdk_root_window, 0);
1656 gdk_error_warnings = old_warnings;
1660 *--------------------------------------------------------------
1663 * Flushes the Xlib output buffer and then waits
1664 * until all requests have been received and processed
1665 * by the X server. The only real use for this function
1666 * is in dealing with XShm.
1674 *--------------------------------------------------------------
1680 XSync (gdk_display, False);