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