]> Pileus Git - ~andy/gtk/blob - gdk/x11/gdkevents-x11.c
a005e5a148c360010f4db17ce19e91292f819e2d
[~andy/gtk] / gdk / x11 / gdkevents-x11.c
1 /* GDK - The GIMP Drawing Kit
2  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 /*
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/. 
25  */
26
27 #include "gdk.h"
28 #include "gdkprivate-x11.h"
29 #include "gdkinternals.h"
30 #include "gdkx.h"
31
32 #include "gdkkeysyms.h"
33
34 #if HAVE_CONFIG_H
35 #  include <config.h>
36 #  if STDC_HEADERS
37 #    include <string.h>
38 #  endif
39 #endif
40
41 #include "gdkinputprivate.h"
42
43 #ifdef HAVE_XKB
44 #include <X11/XKBlib.h>
45 #endif
46
47 typedef struct _GdkIOClosure GdkIOClosure;
48 typedef struct _GdkEventPrivate GdkEventPrivate;
49
50 #define DOUBLE_CLICK_TIME      250
51 #define TRIPLE_CLICK_TIME      500
52 #define DOUBLE_CLICK_DIST      5
53 #define TRIPLE_CLICK_DIST      5
54
55 typedef enum
56 {
57   /* Following flag is set for events on the event queue during
58    * translation and cleared afterwards.
59    */
60   GDK_EVENT_PENDING = 1 << 0
61 } GdkEventFlags;
62
63 struct _GdkIOClosure
64 {
65   GdkInputFunction function;
66   GdkInputCondition condition;
67   GdkDestroyNotify notify;
68   gpointer data;
69 };
70
71 struct _GdkEventPrivate
72 {
73   GdkEvent event;
74   guint    flags;
75 };
76
77 /* 
78  * Private function declarations
79  */
80
81 static gint      gdk_event_apply_filters (XEvent   *xevent,
82                                           GdkEvent *event,
83                                           GList    *filters);
84 static gint      gdk_event_translate     (GdkEvent *event, 
85                                           XEvent   *xevent,
86                                           gboolean  return_exposes);
87 #if 0
88 static Bool      gdk_event_get_type     (Display   *display, 
89                                          XEvent    *xevent, 
90                                          XPointer   arg);
91 #endif
92
93 static gboolean gdk_event_prepare  (GSource     *source,
94                                     gint        *timeout);
95 static gboolean gdk_event_check    (GSource     *source);
96 static gboolean gdk_event_dispatch (GSource     *source,
97                                     GSourceFunc  callback,
98                                     gpointer     user_data);
99
100 GdkFilterReturn gdk_wm_protocols_filter (GdkXEvent *xev,
101                                          GdkEvent  *event,
102                                          gpointer   data);
103
104 /* Private variable declarations
105  */
106
107 static int connection_number = 0;           /* The file descriptor number of our
108                                              *  connection to the X server. This
109                                              *  is used so that we may determine
110                                              *  when events are pending by using
111                                              *  the "select" system call.
112                                              */
113 static GList *client_filters;               /* Filters for client messages */
114
115 static GSourceFuncs event_funcs = {
116   gdk_event_prepare,
117   gdk_event_check,
118   gdk_event_dispatch,
119   NULL
120 };
121
122 GPollFD event_poll_fd;
123
124 /*********************************************
125  * Functions for maintaining the event queue *
126  *********************************************/
127
128 void 
129 gdk_events_init (void)
130 {
131   GSource *source;
132   
133   connection_number = ConnectionNumber (gdk_display);
134   GDK_NOTE (MISC,
135             g_message ("connection number: %d", connection_number));
136
137
138   source = g_source_new (&event_funcs, sizeof (GSource));
139   g_source_set_priority (source, GDK_PRIORITY_EVENTS);
140   
141   event_poll_fd.fd = connection_number;
142   event_poll_fd.events = G_IO_IN;
143   
144   g_source_add_poll (source, &event_poll_fd);
145   g_source_set_can_recurse (source, TRUE);
146   g_source_attach (source, NULL);
147
148   gdk_add_client_message_filter (gdk_wm_protocols, 
149                                  gdk_wm_protocols_filter, NULL);
150 }
151
152 /*
153  *--------------------------------------------------------------
154  * gdk_events_pending
155  *
156  *   Returns if events are pending on the queue.
157  *
158  * Arguments:
159  *
160  * Results:
161  *   Returns TRUE if events are pending
162  *
163  * Side effects:
164  *
165  *--------------------------------------------------------------
166  */
167
168 gboolean
169 gdk_events_pending (void)
170 {
171   return (gdk_event_queue_find_first() || XPending (gdk_display));
172 }
173
174 /*
175  *--------------------------------------------------------------
176  * gdk_event_get_graphics_expose
177  *
178  *   Waits for a GraphicsExpose or NoExpose event
179  *
180  * Arguments:
181  *
182  * Results: 
183  *   For GraphicsExpose events, returns a pointer to the event
184  *   converted into a GdkEvent Otherwise, returns NULL.
185  *
186  * Side effects:
187  *
188  *-------------------------------------------------------------- */
189
190 static Bool
191 graphics_expose_predicate (Display  *display,
192                            XEvent   *xevent,
193                            XPointer  arg)
194 {
195   if (xevent->xany.window == GDK_DRAWABLE_XID (arg) &&
196       (xevent->xany.type == GraphicsExpose ||
197        xevent->xany.type == NoExpose))
198     return True;
199   else
200     return False;
201 }
202
203 GdkEvent*
204 gdk_event_get_graphics_expose (GdkWindow *window)
205 {
206   XEvent xevent;
207   GdkEvent *event;
208   
209   g_return_val_if_fail (window != NULL, NULL);
210   
211   XIfEvent (gdk_display, &xevent, graphics_expose_predicate, (XPointer) window);
212   
213   if (xevent.xany.type == GraphicsExpose)
214     {
215       event = gdk_event_new ();
216       
217       if (gdk_event_translate (event, &xevent, TRUE))
218         return event;
219       else
220         gdk_event_free (event);
221     }
222   
223   return NULL;  
224 }
225
226 static gint
227 gdk_event_apply_filters (XEvent *xevent,
228                          GdkEvent *event,
229                          GList *filters)
230 {
231   GList *tmp_list;
232   GdkFilterReturn result;
233   
234   tmp_list = filters;
235   
236   while (tmp_list)
237     {
238       GdkEventFilter *filter = (GdkEventFilter*) tmp_list->data;
239       
240       tmp_list = tmp_list->next;
241       result = filter->function (xevent, event, filter->data);
242       if (result !=  GDK_FILTER_CONTINUE)
243         return result;
244     }
245   
246   return GDK_FILTER_CONTINUE;
247 }
248
249 void 
250 gdk_add_client_message_filter (GdkAtom       message_type,
251                                GdkFilterFunc func,
252                                gpointer      data)
253 {
254   GdkClientFilter *filter = g_new (GdkClientFilter, 1);
255
256   filter->type = message_type;
257   filter->function = func;
258   filter->data = data;
259   
260   client_filters = g_list_prepend (client_filters, filter);
261 }
262
263 static gint
264 gdk_event_translate (GdkEvent *event,
265                      XEvent   *xevent,
266                      gboolean  return_exposes)
267 {
268   
269   GdkWindow *window;
270   GdkWindowObject *window_private;
271   static XComposeStatus compose;
272   KeySym keysym;
273   int charcount;
274 #ifdef USE_XIM
275   static gchar* buf = NULL;
276   static gint buf_len= 0;
277 #else
278   char buf[16];
279 #endif
280   gint return_val;
281   gint xoffset, yoffset;
282   
283   return_val = FALSE;
284   
285   /* Find the GdkWindow that this event occurred in.
286    * 
287    * We handle events with window=None
288    *  specially - they are generated by XFree86's XInput under
289    *  some circumstances.
290    */
291   
292   if (xevent->xany.window == None)
293     {
294       return_val = _gdk_input_window_none_event (event, xevent);
295       
296       if (return_val >= 0)      /* was handled */
297         return return_val;
298       else
299         return_val = FALSE;
300     }
301   
302   window = gdk_window_lookup (xevent->xany.window);
303   /* FIXME: window might be a GdkPixmap!!! */
304   
305   window_private = (GdkWindowObject *) window;
306   
307   if (window != NULL)
308     gdk_window_ref (window);
309   
310   event->any.window = window;
311   event->any.send_event = xevent->xany.send_event ? TRUE : FALSE;
312   
313   if (window_private && GDK_WINDOW_DESTROYED (window))
314     {
315       if (xevent->type != DestroyNotify)
316         return FALSE;
317     }
318   else
319     {
320       /* Check for filters for this window
321        */
322       GdkFilterReturn result;
323       result = gdk_event_apply_filters (xevent, event,
324                                         window_private
325                                         ?window_private->filters
326                                         :gdk_default_filters);
327       
328       if (result != GDK_FILTER_CONTINUE)
329         {
330           return_val = (result == GDK_FILTER_TRANSLATE) ? TRUE : FALSE;
331           goto done;
332         }
333     }
334
335 #ifdef USE_XIM
336   if (window == NULL && gdk_xim_window && xevent->type == KeyPress &&
337       !GDK_WINDOW_DESTROYED (gdk_xim_window))
338     {
339       /*
340        * If user presses a key in Preedit or Status window, keypress event
341        * is sometimes sent to these windows. These windows are not managed
342        * by GDK, so we redirect KeyPress event to xim_window.
343        *
344        * If someone want to use the window whitch is not managed by GDK
345        * and want to get KeyPress event, he/she must register the filter
346        * function to gdk_default_filters to intercept the event.
347        */
348
349       GdkFilterReturn result;
350
351       window = gdk_xim_window;
352       window_private = (GdkWindowObject *) window;
353       gdk_window_ref (window);
354       event->any.window = window;
355
356       GDK_NOTE (XIM,
357         g_message ("KeyPress event is redirected to xim_window: %#lx",
358                    xevent->xany.window));
359
360       result = gdk_event_apply_filters (xevent, event,
361                                         window_private->filters);
362       if (result != GDK_FILTER_CONTINUE)
363         {
364           return_val = (result == GDK_FILTER_TRANSLATE) ? TRUE : FALSE;
365           goto done;
366         }
367     }
368 #endif
369
370   /* We do a "manual" conversion of the XEvent to a
371    *  GdkEvent. The structures are mostly the same so
372    *  the conversion is fairly straightforward. We also
373    *  optionally print debugging info regarding events
374    *  received.
375    */
376
377   return_val = TRUE;
378
379   if (window)
380     {
381       _gdk_windowing_window_get_offsets (window, &xoffset, &yoffset);
382     }
383   else
384     {
385       xoffset = 0;
386       yoffset = 0;
387     }
388
389   switch (xevent->type)
390     {
391     case KeyPress:
392       /* Lookup the string corresponding to the given keysym.
393        */
394       
395 #ifdef USE_XIM
396       if (buf_len == 0) 
397         {
398           buf_len = 128;
399           buf = g_new (gchar, buf_len);
400         }
401       keysym = GDK_VoidSymbol;
402       
403       if (gdk_xim_ic && gdk_xim_ic->xic)
404         {
405           Status status;
406           
407           /* Clear keyval. Depending on status, may not be set */
408           charcount = XmbLookupString(gdk_xim_ic->xic,
409                                       &xevent->xkey, buf, buf_len-1,
410                                       &keysym, &status);
411           if (status == XBufferOverflow)
412             {                     /* retry */
413               /* alloc adequate size of buffer */
414               GDK_NOTE (XIM,
415                         g_message("XIM: overflow (required %i)", charcount));
416               
417               while (buf_len <= charcount)
418                 buf_len *= 2;
419               buf = (gchar *) g_realloc (buf, buf_len);
420               
421               charcount = XmbLookupString (gdk_xim_ic->xic,
422                                            &xevent->xkey, buf, buf_len-1,
423                                            &keysym, &status);
424             }
425           if (status == XLookupNone)
426             {
427               return_val = FALSE;
428               break;
429             }
430         }
431       else
432         charcount = XLookupString (&xevent->xkey, buf, buf_len,
433                                    &keysym, &compose);
434 #else
435       charcount = XLookupString (&xevent->xkey, buf, 16,
436                                  &keysym, &compose);
437 #endif
438       event->key.keyval = keysym;
439       event->key.hardware_keycode = xevent->xkey.keycode;
440       
441       if (charcount > 0 && buf[charcount-1] == '\0')
442         charcount --;
443       else
444         buf[charcount] = '\0';
445       
446 #ifdef G_ENABLE_DEBUG
447       if (gdk_debug_flags & GDK_DEBUG_EVENTS)
448         {
449           g_message ("key press:\twindow: %ld  key: %12s  %d",
450                      xevent->xkey.window,
451                      event->key.keyval ? XKeysymToString (event->key.keyval) : "(none)",
452                      event->key.keyval);
453           if (charcount > 0)
454             g_message ("\t\tlength: %4d string: \"%s\"",
455                        charcount, buf);
456         }
457 #endif /* G_ENABLE_DEBUG */
458
459       /* bits 13 and 14 in the "state" field are the keyboard group */
460 #define KEYBOARD_GROUP_MASK ((1 << 13) | (1 << 14))
461       
462       event->key.type = GDK_KEY_PRESS;
463       event->key.window = window;
464       event->key.time = xevent->xkey.time;
465       event->key.state = (GdkModifierType) xevent->xkey.state;
466       event->key.string = g_strdup (buf);
467       event->key.length = charcount;
468
469       event->key.group = xevent->xkey.state & KEYBOARD_GROUP_MASK;
470       
471       break;
472       
473     case KeyRelease:
474       /* Lookup the string corresponding to the given keysym.
475        */
476 #ifdef USE_XIM
477       if (buf_len == 0) 
478         {
479           buf_len = 128;
480           buf = g_new (gchar, buf_len);
481         }
482 #endif
483       keysym = GDK_VoidSymbol;
484       charcount = XLookupString (&xevent->xkey, buf, 16,
485                                  &keysym, &compose);
486       event->key.keyval = keysym;      
487       
488       GDK_NOTE (EVENTS, 
489                 g_message ("key release:\t\twindow: %ld  key: %12s  %d",
490                            xevent->xkey.window,
491                            XKeysymToString (event->key.keyval),
492                            event->key.keyval));
493       
494       event->key.type = GDK_KEY_RELEASE;
495       event->key.window = window;
496       event->key.time = xevent->xkey.time;
497       event->key.state = (GdkModifierType) xevent->xkey.state;
498       event->key.length = 0;
499       event->key.string = NULL;
500       
501       break;
502       
503     case ButtonPress:
504       GDK_NOTE (EVENTS, 
505                 g_message ("button press:\t\twindow: %ld  x,y: %d %d  button: %d",
506                            xevent->xbutton.window,
507                            xevent->xbutton.x, xevent->xbutton.y,
508                            xevent->xbutton.button));
509       
510       if (window_private &&
511           (window_private->extension_events != 0) &&
512           gdk_input_ignore_core)
513         {
514           return_val = FALSE;
515           break;
516         }
517       
518       /* If we get a ButtonPress event where the button is 4 or 5,
519          it's a Scroll event */
520       if (xevent->xbutton.button == 4 || xevent->xbutton.button == 5)
521         {
522           event->scroll.type = GDK_SCROLL;
523           event->scroll.direction = (xevent->xbutton.button == 4) ? 
524             GDK_SCROLL_UP : GDK_SCROLL_DOWN;
525           event->scroll.window = window;
526           event->scroll.time = xevent->xbutton.x;
527           event->scroll.x = xevent->xbutton.x + xoffset;
528           event->scroll.y = xevent->xbutton.y + yoffset;
529           event->scroll.x_root = (gfloat)xevent->xbutton.x_root;
530           event->scroll.y_root = (gfloat)xevent->xbutton.y_root;
531           event->scroll.state = (GdkModifierType) xevent->xbutton.state;
532           event->scroll.device = gdk_core_pointer;
533         }
534       else
535         {
536           event->button.type = GDK_BUTTON_PRESS;
537           event->button.window = window;
538           event->button.time = xevent->xbutton.time;
539           event->button.x = xevent->xbutton.x + xoffset;
540           event->button.y = xevent->xbutton.y + yoffset;
541           event->button.x_root = (gfloat)xevent->xbutton.x_root;
542           event->button.y_root = (gfloat)xevent->xbutton.y_root;
543           event->button.axes = NULL;
544           event->button.state = (GdkModifierType) xevent->xbutton.state;
545           event->button.button = xevent->xbutton.button;
546           event->button.device = gdk_core_pointer;
547           
548           gdk_event_button_generate (event);
549         }
550
551       break;
552       
553     case ButtonRelease:
554       GDK_NOTE (EVENTS, 
555                 g_message ("button release:\twindow: %ld  x,y: %d %d  button: %d",
556                            xevent->xbutton.window,
557                            xevent->xbutton.x, xevent->xbutton.y,
558                            xevent->xbutton.button));
559       
560       if (window_private &&
561           (window_private->extension_events != 0) &&
562           gdk_input_ignore_core)
563         {
564           return_val = FALSE;
565           break;
566         }
567       
568       /* We treat button presses as scroll wheel events, so ignore the release */
569       if (xevent->xbutton.button == 4 || xevent->xbutton.button == 5)
570         {
571           return_val = FALSE;
572           break;
573         }
574
575       event->button.type = GDK_BUTTON_RELEASE;
576       event->button.window = window;
577       event->button.time = xevent->xbutton.time;
578       event->button.x = xevent->xbutton.x + xoffset;
579       event->button.y = xevent->xbutton.y + yoffset;
580       event->button.x_root = (gfloat)xevent->xbutton.x_root;
581       event->button.y_root = (gfloat)xevent->xbutton.y_root;
582       event->button.axes = NULL;
583       event->button.state = (GdkModifierType) xevent->xbutton.state;
584       event->button.button = xevent->xbutton.button;
585       event->button.device = gdk_core_pointer;
586       
587       break;
588       
589     case MotionNotify:
590       GDK_NOTE (EVENTS,
591                 g_message ("motion notify:\t\twindow: %ld  x,y: %d %d  hint: %s", 
592                            xevent->xmotion.window,
593                            xevent->xmotion.x, xevent->xmotion.y,
594                            (xevent->xmotion.is_hint) ? "true" : "false"));
595       
596       if (window_private &&
597           (window_private->extension_events != 0) &&
598           gdk_input_ignore_core)
599         {
600           return_val = FALSE;
601           break;
602         }
603       
604       event->motion.type = GDK_MOTION_NOTIFY;
605       event->motion.window = window;
606       event->motion.time = xevent->xmotion.time;
607       event->motion.x = xevent->xmotion.x + xoffset;
608       event->motion.y = xevent->xmotion.y + yoffset;
609       event->motion.x_root = (gfloat)xevent->xmotion.x_root;
610       event->motion.y_root = (gfloat)xevent->xmotion.y_root;
611       event->motion.axes = NULL;
612       event->motion.state = (GdkModifierType) xevent->xmotion.state;
613       event->motion.is_hint = xevent->xmotion.is_hint;
614       event->motion.device = gdk_core_pointer;
615       
616       break;
617       
618     case EnterNotify:
619       GDK_NOTE (EVENTS,
620                 g_message ("enter notify:\t\twindow: %ld  detail: %d subwin: %ld",
621                            xevent->xcrossing.window,
622                            xevent->xcrossing.detail,
623                            xevent->xcrossing.subwindow));
624       
625       /* Tell XInput stuff about it if appropriate */
626       if (window_private &&
627           !GDK_WINDOW_DESTROYED (window) &&
628           window_private->extension_events != 0)
629         _gdk_input_enter_event (&xevent->xcrossing, window);
630       
631       event->crossing.type = GDK_ENTER_NOTIFY;
632       event->crossing.window = window;
633       
634       /* If the subwindow field of the XEvent is non-NULL, then
635        *  lookup the corresponding GdkWindow.
636        */
637       if (xevent->xcrossing.subwindow != None)
638         event->crossing.subwindow = gdk_window_lookup (xevent->xcrossing.subwindow);
639       else
640         event->crossing.subwindow = NULL;
641       
642       event->crossing.time = xevent->xcrossing.time;
643       event->crossing.x = xevent->xcrossing.x + xoffset;
644       event->crossing.y = xevent->xcrossing.y + yoffset;
645       event->crossing.x_root = xevent->xcrossing.x_root;
646       event->crossing.y_root = xevent->xcrossing.y_root;
647       
648       /* Translate the crossing mode into Gdk terms.
649        */
650       switch (xevent->xcrossing.mode)
651         {
652         case NotifyNormal:
653           event->crossing.mode = GDK_CROSSING_NORMAL;
654           break;
655         case NotifyGrab:
656           event->crossing.mode = GDK_CROSSING_GRAB;
657           break;
658         case NotifyUngrab:
659           event->crossing.mode = GDK_CROSSING_UNGRAB;
660           break;
661         };
662       
663       /* Translate the crossing detail into Gdk terms.
664        */
665       switch (xevent->xcrossing.detail)
666         {
667         case NotifyInferior:
668           event->crossing.detail = GDK_NOTIFY_INFERIOR;
669           break;
670         case NotifyAncestor:
671           event->crossing.detail = GDK_NOTIFY_ANCESTOR;
672           break;
673         case NotifyVirtual:
674           event->crossing.detail = GDK_NOTIFY_VIRTUAL;
675           break;
676         case NotifyNonlinear:
677           event->crossing.detail = GDK_NOTIFY_NONLINEAR;
678           break;
679         case NotifyNonlinearVirtual:
680           event->crossing.detail = GDK_NOTIFY_NONLINEAR_VIRTUAL;
681           break;
682         default:
683           event->crossing.detail = GDK_NOTIFY_UNKNOWN;
684           break;
685         }
686       
687       event->crossing.focus = xevent->xcrossing.focus;
688       event->crossing.state = xevent->xcrossing.state;
689   
690       break;
691       
692     case LeaveNotify:
693       GDK_NOTE (EVENTS, 
694                 g_message ("leave notify:\t\twindow: %ld  detail: %d subwin: %ld",
695                            xevent->xcrossing.window,
696                            xevent->xcrossing.detail, xevent->xcrossing.subwindow));
697       
698       event->crossing.type = GDK_LEAVE_NOTIFY;
699       event->crossing.window = window;
700       
701       /* If the subwindow field of the XEvent is non-NULL, then
702        *  lookup the corresponding GdkWindow.
703        */
704       if (xevent->xcrossing.subwindow != None)
705         event->crossing.subwindow = gdk_window_lookup (xevent->xcrossing.subwindow);
706       else
707         event->crossing.subwindow = NULL;
708       
709       event->crossing.time = xevent->xcrossing.time;
710       event->crossing.x = xevent->xcrossing.x + xoffset;
711       event->crossing.y = xevent->xcrossing.y + yoffset;
712       event->crossing.x_root = xevent->xcrossing.x_root;
713       event->crossing.y_root = xevent->xcrossing.y_root;
714       
715       /* Translate the crossing mode into Gdk terms.
716        */
717       switch (xevent->xcrossing.mode)
718         {
719         case NotifyNormal:
720           event->crossing.mode = GDK_CROSSING_NORMAL;
721           break;
722         case NotifyGrab:
723           event->crossing.mode = GDK_CROSSING_GRAB;
724           break;
725         case NotifyUngrab:
726           event->crossing.mode = GDK_CROSSING_UNGRAB;
727           break;
728         };
729       
730       /* Translate the crossing detail into Gdk terms.
731        */
732       switch (xevent->xcrossing.detail)
733         {
734         case NotifyInferior:
735           event->crossing.detail = GDK_NOTIFY_INFERIOR;
736           break;
737         case NotifyAncestor:
738           event->crossing.detail = GDK_NOTIFY_ANCESTOR;
739           break;
740         case NotifyVirtual:
741           event->crossing.detail = GDK_NOTIFY_VIRTUAL;
742           break;
743         case NotifyNonlinear:
744           event->crossing.detail = GDK_NOTIFY_NONLINEAR;
745           break;
746         case NotifyNonlinearVirtual:
747           event->crossing.detail = GDK_NOTIFY_NONLINEAR_VIRTUAL;
748           break;
749         default:
750           event->crossing.detail = GDK_NOTIFY_UNKNOWN;
751           break;
752         }
753       
754       event->crossing.focus = xevent->xcrossing.focus;
755       event->crossing.state = xevent->xcrossing.state;
756       
757       break;
758       
759     case FocusIn:
760     case FocusOut:
761       /* We only care about focus events that indicate that _this_
762        * window (not a ancestor or child) got or lost the focus
763        */
764       switch (xevent->xfocus.detail)
765         {
766         case NotifyAncestor:
767         case NotifyInferior:
768         case NotifyNonlinear:
769           GDK_NOTE (EVENTS,
770                     g_message ("focus %s:\t\twindow: %ld",
771                                (xevent->xany.type == FocusIn) ? "in" : "out",
772                                xevent->xfocus.window));
773           
774           /* gdk_keyboard_grab() causes following events. These events confuse
775            * the XIM focus, so ignore them.
776            */
777           if (xevent->xfocus.mode == NotifyGrab ||
778               xevent->xfocus.mode == NotifyUngrab)
779             break;
780           
781           event->focus_change.type = GDK_FOCUS_CHANGE;
782           event->focus_change.window = window;
783           event->focus_change.in = (xevent->xany.type == FocusIn);
784
785           break;
786         default:
787           return_val = FALSE;
788         }
789       break;
790       
791     case KeymapNotify:
792       GDK_NOTE (EVENTS,
793                 g_message ("keymap notify"));
794
795       /* Not currently handled */
796       return_val = FALSE;
797       break;
798       
799     case Expose:
800       GDK_NOTE (EVENTS,
801                 g_message ("expose:\t\twindow: %ld  %d  x,y: %d %d  w,h: %d %d%s",
802                            xevent->xexpose.window, xevent->xexpose.count,
803                            xevent->xexpose.x, xevent->xexpose.y,
804                            xevent->xexpose.width, xevent->xexpose.height,
805                            event->any.send_event ? " (send)" : ""));
806       {
807         GdkRectangle expose_rect;
808
809         expose_rect.x = xevent->xexpose.x + xoffset;
810         expose_rect.y = xevent->xexpose.y + yoffset;
811         expose_rect.width = xevent->xexpose.width;
812         expose_rect.height = xevent->xexpose.height;
813
814         if (return_exposes)
815           {
816             event->expose.type = GDK_EXPOSE;
817             event->expose.area = expose_rect;
818             event->expose.window = window;
819             event->expose.count = xevent->xexpose.count;
820
821             return_val = TRUE;
822           }
823         else
824           {
825             _gdk_window_process_expose (window, xevent->xexpose.serial, &expose_rect);
826
827             return_val = FALSE;
828           }
829         
830         return_val = FALSE;
831       }
832         
833       break;
834       
835     case GraphicsExpose:
836       {
837         GdkRectangle expose_rect;
838
839         GDK_NOTE (EVENTS,
840                   g_message ("graphics expose:\tdrawable: %ld",
841                              xevent->xgraphicsexpose.drawable));
842
843         expose_rect.x = xevent->xgraphicsexpose.x + xoffset;
844         expose_rect.y = xevent->xgraphicsexpose.y + yoffset;
845         expose_rect.width = xevent->xgraphicsexpose.width;
846         expose_rect.height = xevent->xgraphicsexpose.height;
847             
848         if (return_exposes)
849           {
850             event->expose.type = GDK_EXPOSE;
851             event->expose.area = expose_rect;
852             event->expose.window = window;
853             event->expose.count = xevent->xgraphicsexpose.count;
854
855             return_val = TRUE;
856           }
857         else
858           {
859             _gdk_window_process_expose (window, xevent->xgraphicsexpose.serial, &expose_rect);
860             
861             return_val = FALSE;
862           }
863         
864       }
865       break;
866       
867     case NoExpose:
868       GDK_NOTE (EVENTS,
869                 g_message ("no expose:\t\tdrawable: %ld",
870                            xevent->xnoexpose.drawable));
871       
872       event->no_expose.type = GDK_NO_EXPOSE;
873       event->no_expose.window = window;
874       
875       break;
876       
877     case VisibilityNotify:
878 #ifdef G_ENABLE_DEBUG
879       if (gdk_debug_flags & GDK_DEBUG_EVENTS)
880         switch (xevent->xvisibility.state)
881           {
882           case VisibilityFullyObscured:
883             g_message ("visibility notify:\twindow: %ld  none",
884                        xevent->xvisibility.window);
885             break;
886           case VisibilityPartiallyObscured:
887             g_message ("visibility notify:\twindow: %ld  partial",
888                        xevent->xvisibility.window);
889             break;
890           case VisibilityUnobscured:
891             g_message ("visibility notify:\twindow: %ld  full",
892                        xevent->xvisibility.window);
893             break;
894           }
895 #endif /* G_ENABLE_DEBUG */
896       
897       event->visibility.type = GDK_VISIBILITY_NOTIFY;
898       event->visibility.window = window;
899       
900       switch (xevent->xvisibility.state)
901         {
902         case VisibilityFullyObscured:
903           event->visibility.state = GDK_VISIBILITY_FULLY_OBSCURED;
904           break;
905           
906         case VisibilityPartiallyObscured:
907           event->visibility.state = GDK_VISIBILITY_PARTIAL;
908           break;
909           
910         case VisibilityUnobscured:
911           event->visibility.state = GDK_VISIBILITY_UNOBSCURED;
912           break;
913         }
914       
915       break;
916       
917     case CreateNotify:
918       GDK_NOTE (EVENTS,
919                 g_message ("create notify:\twindow: %ld  x,y: %d %d     w,h: %d %d  b-w: %d  parent: %ld         ovr: %d",
920                            xevent->xcreatewindow.window,
921                            xevent->xcreatewindow.x,
922                            xevent->xcreatewindow.y,
923                            xevent->xcreatewindow.width,
924                            xevent->xcreatewindow.height,
925                            xevent->xcreatewindow.border_width,
926                            xevent->xcreatewindow.parent,
927                            xevent->xcreatewindow.override_redirect));
928       /* not really handled */
929       break;
930       
931     case DestroyNotify:
932       GDK_NOTE (EVENTS,
933                 g_message ("destroy notify:\twindow: %ld",
934                            xevent->xdestroywindow.window));
935       
936       event->any.type = GDK_DESTROY;
937       event->any.window = window;
938       
939       return_val = window_private && !GDK_WINDOW_DESTROYED (window);
940       
941       if (window && GDK_WINDOW_XID (window) != GDK_ROOT_WINDOW())
942         gdk_window_destroy_notify (window);
943       break;
944       
945     case UnmapNotify:
946       GDK_NOTE (EVENTS,
947                 g_message ("unmap notify:\t\twindow: %ld",
948                            xevent->xmap.window));
949       
950       event->any.type = GDK_UNMAP;
951       event->any.window = window;
952       
953       if (gdk_xgrab_window == window_private)
954         gdk_xgrab_window = NULL;
955       
956       break;
957       
958     case MapNotify:
959       GDK_NOTE (EVENTS,
960                 g_message ("map notify:\t\twindow: %ld",
961                            xevent->xmap.window));
962       
963       event->any.type = GDK_MAP;
964       event->any.window = window;
965       
966       break;
967       
968     case ReparentNotify:
969       GDK_NOTE (EVENTS,
970                 g_message ("reparent notify:\twindow: %ld  x,y: %d %d  parent: %ld      ovr: %d",
971                            xevent->xreparent.window,
972                            xevent->xreparent.x,
973                            xevent->xreparent.y,
974                            xevent->xreparent.parent,
975                            xevent->xreparent.override_redirect));
976
977       /* Not currently handled */
978       return_val = FALSE;
979       break;
980       
981     case ConfigureNotify:
982       GDK_NOTE (EVENTS,
983                 g_message ("configure notify:\twindow: %ld  x,y: %d %d  w,h: %d %d  b-w: %d  above: %ld  ovr: %d%s",
984                            xevent->xconfigure.window,
985                            xevent->xconfigure.x,
986                            xevent->xconfigure.y,
987                            xevent->xconfigure.width,
988                            xevent->xconfigure.height,
989                            xevent->xconfigure.border_width,
990                            xevent->xconfigure.above,
991                            xevent->xconfigure.override_redirect,
992                            !window
993                            ? " (discarding)"
994                            : GDK_WINDOW_TYPE (window) == GDK_WINDOW_CHILD
995                            ? " (discarding child)"
996                            : ""));
997       if (window &&
998           !GDK_WINDOW_DESTROYED (window) &&
999           (window_private->extension_events != 0))
1000         _gdk_input_configure_event (&xevent->xconfigure, window);
1001
1002       if (!window || GDK_WINDOW_TYPE (window) == GDK_WINDOW_CHILD)
1003         return_val = FALSE;
1004       else
1005         {
1006           event->configure.type = GDK_CONFIGURE;
1007           event->configure.window = window;
1008           event->configure.width = xevent->xconfigure.width;
1009           event->configure.height = xevent->xconfigure.height;
1010           
1011           if (!xevent->xconfigure.x &&
1012               !xevent->xconfigure.y &&
1013               !GDK_WINDOW_DESTROYED (window))
1014             {
1015               gint tx = 0;
1016               gint ty = 0;
1017               Window child_window = 0;
1018
1019               gdk_error_trap_push ();
1020               if (XTranslateCoordinates (GDK_DRAWABLE_XDISPLAY (window),
1021                                          GDK_DRAWABLE_XID (window),
1022                                          gdk_root_window,
1023                                          0, 0,
1024                                          &tx, &ty,
1025                                          &child_window))
1026                 {
1027                   if (!gdk_error_trap_pop ())
1028                     {
1029                       event->configure.x = tx;
1030                       event->configure.y = ty;
1031                     }
1032                 }
1033               else
1034                 gdk_error_trap_pop ();
1035             }
1036           else
1037             {
1038               event->configure.x = xevent->xconfigure.x;
1039               event->configure.y = xevent->xconfigure.y;
1040             }
1041           window_private->x = event->configure.x;
1042           window_private->y = event->configure.y;
1043           GDK_WINDOW_IMPL_X11 (window_private->impl)->width = xevent->xconfigure.width;
1044           GDK_WINDOW_IMPL_X11 (window_private->impl)->height = xevent->xconfigure.height;
1045           if (window_private->resize_count > 1)
1046             window_private->resize_count -= 1;
1047         }
1048       break;
1049       
1050     case PropertyNotify:
1051       GDK_NOTE (EVENTS,
1052                 gchar *atom = gdk_atom_name (xevent->xproperty.atom);
1053                 g_message ("property notify:\twindow: %ld, atom(%ld): %s%s%s",
1054                            xevent->xproperty.window,
1055                            xevent->xproperty.atom,
1056                            atom ? "\"" : "",
1057                            atom ? atom : "unknown",
1058                            atom ? "\"" : "");
1059                 g_free (atom);
1060                 );
1061       
1062       event->property.type = GDK_PROPERTY_NOTIFY;
1063       event->property.window = window;
1064       event->property.atom = xevent->xproperty.atom;
1065       event->property.time = xevent->xproperty.time;
1066       event->property.state = xevent->xproperty.state;
1067       
1068       break;
1069       
1070     case SelectionClear:
1071       GDK_NOTE (EVENTS,
1072                 g_message ("selection clear:\twindow: %ld",
1073                            xevent->xproperty.window));
1074
1075       if (_gdk_selection_filter_clear_event (&xevent->xselectionclear))
1076         {
1077           event->selection.type = GDK_SELECTION_CLEAR;
1078           event->selection.window = window;
1079           event->selection.selection = xevent->xselectionclear.selection;
1080           event->selection.time = xevent->xselectionclear.time;
1081         }
1082       else
1083         return_val = FALSE;
1084           
1085       break;
1086       
1087     case SelectionRequest:
1088       GDK_NOTE (EVENTS,
1089                 g_message ("selection request:\twindow: %ld",
1090                            xevent->xproperty.window));
1091       
1092       event->selection.type = GDK_SELECTION_REQUEST;
1093       event->selection.window = window;
1094       event->selection.selection = xevent->xselectionrequest.selection;
1095       event->selection.target = xevent->xselectionrequest.target;
1096       event->selection.property = xevent->xselectionrequest.property;
1097       event->selection.requestor = xevent->xselectionrequest.requestor;
1098       event->selection.time = xevent->xselectionrequest.time;
1099       
1100       break;
1101       
1102     case SelectionNotify:
1103       GDK_NOTE (EVENTS,
1104                 g_message ("selection notify:\twindow: %ld",
1105                            xevent->xproperty.window));
1106       
1107       
1108       event->selection.type = GDK_SELECTION_NOTIFY;
1109       event->selection.window = window;
1110       event->selection.selection = xevent->xselection.selection;
1111       event->selection.target = xevent->xselection.target;
1112       event->selection.property = xevent->xselection.property;
1113       event->selection.time = xevent->xselection.time;
1114       
1115       break;
1116       
1117     case ColormapNotify:
1118       GDK_NOTE (EVENTS,
1119                 g_message ("colormap notify:\twindow: %ld",
1120                            xevent->xcolormap.window));
1121       
1122       /* Not currently handled */
1123       return_val = FALSE;
1124       break;
1125       
1126     case ClientMessage:
1127       {
1128         GList *tmp_list;
1129         GdkFilterReturn result = GDK_FILTER_CONTINUE;
1130
1131         GDK_NOTE (EVENTS,
1132                   g_message ("client message:\twindow: %ld",
1133                              xevent->xclient.window));
1134         
1135         tmp_list = client_filters;
1136         while (tmp_list)
1137           {
1138             GdkClientFilter *filter = tmp_list->data;
1139             if (filter->type == xevent->xclient.message_type)
1140               {
1141                 result = (*filter->function) (xevent, event, filter->data);
1142                 break;
1143               }
1144             
1145             tmp_list = tmp_list->next;
1146           }
1147
1148         switch (result)
1149           {
1150           case GDK_FILTER_REMOVE:
1151             return_val = FALSE;
1152             break;
1153           case GDK_FILTER_TRANSLATE:
1154             return_val = TRUE;
1155             break;
1156           case GDK_FILTER_CONTINUE:
1157             /* Send unknown ClientMessage's on to Gtk for it to use */
1158             event->client.type = GDK_CLIENT_EVENT;
1159             event->client.window = window;
1160             event->client.message_type = xevent->xclient.message_type;
1161             event->client.data_format = xevent->xclient.format;
1162             memcpy(&event->client.data, &xevent->xclient.data,
1163                    sizeof(event->client.data));
1164           }
1165       }
1166       
1167       break;
1168       
1169     case MappingNotify:
1170       GDK_NOTE (EVENTS,
1171                 g_message ("mapping notify"));
1172       
1173       /* Let XLib know that there is a new keyboard mapping.
1174        */
1175       XRefreshKeyboardMapping (&xevent->xmapping);
1176       ++_gdk_keymap_serial;
1177       return_val = FALSE;
1178       break;
1179
1180 #ifdef HAVE_XKB
1181     case XkbMapNotify:
1182       ++_gdk_keymap_serial;
1183       return_val = FALSE;
1184       break;
1185 #endif
1186       
1187     default:
1188       /* something else - (e.g., a Xinput event) */
1189       
1190       if (window_private &&
1191           !GDK_WINDOW_DESTROYED (window_private) &&
1192           (window_private->extension_events != 0))
1193         return_val = _gdk_input_other_event(event, xevent, window);
1194       else
1195         return_val = FALSE;
1196       
1197       break;
1198     }
1199
1200  done:
1201   if (return_val)
1202     {
1203       if (event->any.window)
1204         gdk_window_ref (event->any.window);
1205       if (((event->any.type == GDK_ENTER_NOTIFY) ||
1206            (event->any.type == GDK_LEAVE_NOTIFY)) &&
1207           (event->crossing.subwindow != NULL))
1208         gdk_window_ref (event->crossing.subwindow);
1209     }
1210   else
1211     {
1212       /* Mark this event as having no resources to be freed */
1213       event->any.window = NULL;
1214       event->any.type = GDK_NOTHING;
1215     }
1216   
1217   if (window)
1218     gdk_window_unref (window);
1219   
1220   return return_val;
1221 }
1222
1223 GdkFilterReturn
1224 gdk_wm_protocols_filter (GdkXEvent *xev,
1225                          GdkEvent  *event,
1226                          gpointer data)
1227 {
1228   XEvent *xevent = (XEvent *)xev;
1229
1230   if ((Atom) xevent->xclient.data.l[0] == gdk_wm_delete_window)
1231     {
1232   /* The delete window request specifies a window
1233    *  to delete. We don't actually destroy the
1234    *  window because "it is only a request". (The
1235    *  window might contain vital data that the
1236    *  program does not want destroyed). Instead
1237    *  the event is passed along to the program,
1238    *  which should then destroy the window.
1239    */
1240       GDK_NOTE (EVENTS,
1241                 g_message ("delete window:\t\twindow: %ld",
1242                            xevent->xclient.window));
1243       
1244       event->any.type = GDK_DELETE;
1245
1246       return GDK_FILTER_TRANSLATE;
1247     }
1248   else if ((Atom) xevent->xclient.data.l[0] == gdk_wm_take_focus)
1249     {
1250     }
1251   else if ((Atom) xevent->xclient.data.l[0] == gdk_atom_intern ("_NET_WM_PING", FALSE))
1252     {
1253       XEvent xev = *xevent;
1254       
1255       xev.xclient.window = gdk_root_window;
1256       XSendEvent (gdk_display, gdk_root_window, False, SubstructureRedirectMask | SubstructureNotifyMask, &xev);
1257     }
1258
1259   return GDK_FILTER_REMOVE;
1260 }
1261
1262 #if 0
1263 static Bool
1264 gdk_event_get_type (Display  *display,
1265                     XEvent   *xevent,
1266                     XPointer  arg)
1267 {
1268   GdkEvent event;
1269   GdkPredicate *pred;
1270   
1271   if (gdk_event_translate (&event, xevent, FALSE))
1272     {
1273       pred = (GdkPredicate*) arg;
1274       return (* pred->func) (&event, pred->data);
1275     }
1276   
1277   return FALSE;
1278 }
1279 #endif
1280
1281 void
1282 gdk_events_queue (void)
1283 {
1284   GList *node;
1285   GdkEvent *event;
1286   XEvent xevent;
1287
1288   while (!gdk_event_queue_find_first() && XPending (gdk_display))
1289     {
1290 #ifdef USE_XIM
1291       Window w = None;
1292       
1293       XNextEvent (gdk_display, &xevent);
1294       if (gdk_xim_window)
1295         switch (xevent.type)
1296           {
1297           case KeyPress:
1298           case KeyRelease:
1299           case ButtonPress:
1300           case ButtonRelease:
1301             w = GDK_WINDOW_XWINDOW (gdk_xim_window);
1302             break;
1303           }
1304
1305       if (XFilterEvent (&xevent, w))
1306         continue;
1307 #else
1308       XNextEvent (gdk_display, &xevent);
1309 #endif
1310
1311       switch (xevent.type)
1312         {
1313         case KeyPress:
1314         case KeyRelease:
1315           break;
1316         default:
1317           if (XFilterEvent (&xevent, None))
1318             continue;
1319         }
1320       
1321       event = gdk_event_new ();
1322       
1323       event->any.type = GDK_NOTHING;
1324       event->any.window = NULL;
1325       event->any.send_event = xevent.xany.send_event ? TRUE : FALSE;
1326
1327       ((GdkEventPrivate *)event)->flags |= GDK_EVENT_PENDING;
1328
1329       gdk_event_queue_append (event);
1330       node = gdk_queued_tail;
1331
1332       if (gdk_event_translate (event, &xevent, FALSE))
1333         {
1334           ((GdkEventPrivate *)event)->flags &= ~GDK_EVENT_PENDING;
1335         }
1336       else
1337         {
1338           gdk_event_queue_remove_link (node);
1339           g_list_free_1 (node);
1340           gdk_event_free (event);
1341         }
1342     }
1343 }
1344
1345 static gboolean  
1346 gdk_event_prepare (GSource  *source,
1347                    gint     *timeout)
1348 {
1349   gboolean retval;
1350   
1351   GDK_THREADS_ENTER ();
1352
1353   *timeout = -1;
1354
1355   retval = (gdk_event_queue_find_first () != NULL) || XPending (gdk_display);
1356
1357   GDK_THREADS_LEAVE ();
1358
1359   return retval;
1360 }
1361
1362 static gboolean  
1363 gdk_event_check (GSource  *source) 
1364 {
1365   gboolean retval;
1366   
1367   GDK_THREADS_ENTER ();
1368
1369   if (event_poll_fd.revents & G_IO_IN)
1370     retval = (gdk_event_queue_find_first () != NULL) || XPending (gdk_display);
1371   else
1372     retval = FALSE;
1373
1374   GDK_THREADS_LEAVE ();
1375
1376   return retval;
1377 }
1378
1379 static gboolean  
1380 gdk_event_dispatch (GSource    *source,
1381                     GSourceFunc callback,
1382                     gpointer    user_data)
1383 {
1384   GdkEvent *event;
1385  
1386   GDK_THREADS_ENTER ();
1387
1388   gdk_events_queue();
1389   event = gdk_event_unqueue();
1390
1391   if (event)
1392     {
1393       if (gdk_event_func)
1394         (*gdk_event_func) (event, gdk_event_data);
1395       
1396       gdk_event_free (event);
1397     }
1398   
1399   GDK_THREADS_LEAVE ();
1400
1401   return TRUE;
1402 }
1403
1404 /* Sends a ClientMessage to all toplevel client windows */
1405 gboolean
1406 gdk_event_send_client_message (GdkEvent *event, guint32 xid)
1407 {
1408   XEvent sev;
1409   
1410   g_return_val_if_fail(event != NULL, FALSE);
1411   
1412   /* Set up our event to send, with the exception of its target window */
1413   sev.xclient.type = ClientMessage;
1414   sev.xclient.display = gdk_display;
1415   sev.xclient.format = event->client.data_format;
1416   sev.xclient.window = xid;
1417   memcpy(&sev.xclient.data, &event->client.data, sizeof(sev.xclient.data));
1418   sev.xclient.message_type = event->client.message_type;
1419   
1420   return gdk_send_xevent (xid, False, NoEventMask, &sev);
1421 }
1422
1423 /* Sends a ClientMessage to all toplevel client windows */
1424 gboolean
1425 gdk_event_send_client_message_to_all_recurse (XEvent  *xev, 
1426                                               guint32  xid,
1427                                               guint    level)
1428 {
1429   static GdkAtom wm_state_atom = GDK_NONE;
1430   Atom type = None;
1431   int format;
1432   unsigned long nitems, after;
1433   unsigned char *data;
1434   Window *ret_children, ret_root, ret_parent;
1435   unsigned int ret_nchildren;
1436   gint old_warnings = gdk_error_warnings;
1437   gboolean send = FALSE;
1438   gboolean found = FALSE;
1439   int i;
1440
1441   if (!wm_state_atom)
1442     wm_state_atom = gdk_atom_intern ("WM_STATE", FALSE);
1443
1444   gdk_error_warnings = FALSE;
1445   gdk_error_code = 0;
1446   XGetWindowProperty (gdk_display, xid, wm_state_atom, 0, 0, False, AnyPropertyType,
1447                       &type, &format, &nitems, &after, &data);
1448
1449   if (gdk_error_code)
1450     {
1451       gdk_error_warnings = old_warnings;
1452
1453       return FALSE;
1454     }
1455
1456   if (type)
1457     {
1458       send = TRUE;
1459       XFree (data);
1460     }
1461   else
1462     {
1463       /* OK, we're all set, now let's find some windows to send this to */
1464       if (XQueryTree (gdk_display, xid, &ret_root, &ret_parent,
1465                       &ret_children, &ret_nchildren) != True ||
1466           gdk_error_code)
1467         {
1468           gdk_error_warnings = old_warnings;
1469
1470           return FALSE;
1471         }
1472
1473       for(i = 0; i < ret_nchildren; i++)
1474         if (gdk_event_send_client_message_to_all_recurse (xev, ret_children[i], level + 1))
1475           found = TRUE;
1476
1477       XFree (ret_children);
1478     }
1479
1480   if (send || (!found && (level == 1)))
1481     {
1482       xev->xclient.window = xid;
1483       gdk_send_xevent (xid, False, NoEventMask, xev);
1484     }
1485
1486   gdk_error_warnings = old_warnings;
1487
1488   return (send || found);
1489 }
1490
1491 void
1492 gdk_event_send_clientmessage_toall (GdkEvent *event)
1493 {
1494   XEvent sev;
1495   gint old_warnings = gdk_error_warnings;
1496
1497   g_return_if_fail(event != NULL);
1498   
1499   /* Set up our event to send, with the exception of its target window */
1500   sev.xclient.type = ClientMessage;
1501   sev.xclient.display = gdk_display;
1502   sev.xclient.format = event->client.data_format;
1503   memcpy(&sev.xclient.data, &event->client.data, sizeof(sev.xclient.data));
1504   sev.xclient.message_type = event->client.message_type;
1505
1506   gdk_event_send_client_message_to_all_recurse(&sev, gdk_root_window, 0);
1507
1508   gdk_error_warnings = old_warnings;
1509 }
1510
1511 /*
1512  *--------------------------------------------------------------
1513  * gdk_flush
1514  *
1515  *   Flushes the Xlib output buffer and then waits
1516  *   until all requests have been received and processed
1517  *   by the X server. The only real use for this function
1518  *   is in dealing with XShm.
1519  *
1520  * Arguments:
1521  *
1522  * Results:
1523  *
1524  * Side effects:
1525  *
1526  *--------------------------------------------------------------
1527  */
1528
1529 void
1530 gdk_flush (void)
1531 {
1532   XSync (gdk_display, False);
1533 }
1534
1535 static GdkAtom timestamp_prop_atom = 0;
1536
1537 static Bool
1538 timestamp_predicate (Display *display,
1539                      XEvent  *xevent,
1540                      XPointer arg)
1541 {
1542   Window xwindow = GPOINTER_TO_UINT (arg);
1543
1544   if (xevent->type == PropertyNotify &&
1545       xevent->xproperty.window == xwindow &&
1546       xevent->xproperty.atom == timestamp_prop_atom)
1547     return True;
1548
1549   return False;
1550 }
1551
1552 /**
1553  * gdk_x11_get_server_time:
1554  * @window: a #GdkWindow, used for communication with the server.
1555  *          The window must have GDK_PROPERTY_CHANGE_MASK in its
1556  *          events mask or a hang will result.
1557  * 
1558  * Routine to get the current X server time stamp. 
1559  * 
1560  * Return value: the time stamp.
1561  **/
1562 guint32
1563 gdk_x11_get_server_time (GdkWindow *window)
1564 {
1565   Display *xdisplay;
1566   Window   xwindow;
1567   guchar c = 'a';
1568   XEvent xevent;
1569
1570   g_return_val_if_fail (GDK_IS_WINDOW (window), 0);
1571   g_return_val_if_fail (!GDK_WINDOW_DESTROYED (window), 0);
1572
1573   if (!timestamp_prop_atom)
1574     timestamp_prop_atom = gdk_atom_intern ("GDK_TIMESTAMP_PROP", FALSE);
1575
1576   xdisplay = GDK_WINDOW_XDISPLAY (window);
1577   xwindow = GDK_WINDOW_XWINDOW (window);
1578   
1579   XChangeProperty (xdisplay, xwindow,
1580                    timestamp_prop_atom, timestamp_prop_atom,
1581                    8, PropModeReplace, &c, 1);
1582
1583   XIfEvent (xdisplay, &xevent,
1584             timestamp_predicate, GUINT_TO_POINTER(xwindow));
1585
1586   return xevent.xproperty.time;
1587 }
1588