]> Pileus Git - ~andy/gtk/blob - gdk/x11/gdkevents-x11.c
89f3176d12c0a3a7f1811e7b54c7e2acdc756eff
[~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 #include "gdkscreen-x11.h"
32 #include "gdkdisplay-x11.h"
33
34 #include "gdkkeysyms.h"
35
36 #include "xsettings-client.h"
37
38 #if HAVE_CONFIG_H
39 #  include <config.h>
40 #  if STDC_HEADERS
41 #    include <string.h>
42 #  endif
43 #endif
44
45 #include "gdkinputprivate.h"
46
47 #ifdef HAVE_XKB
48 #include <X11/XKBlib.h>
49 #endif
50
51 #include <X11/Xatom.h>
52
53 typedef struct _GdkIOClosure GdkIOClosure;
54 typedef struct _GdkEventPrivate GdkEventPrivate;
55 typedef struct _GdkDisplaySource GdkDisplaySource;
56
57 #define DOUBLE_CLICK_TIME      250
58 #define TRIPLE_CLICK_TIME      500
59 #define DOUBLE_CLICK_DIST      5
60 #define TRIPLE_CLICK_DIST      5
61
62 typedef enum
63 {
64   /* Following flag is set for events on the event queue during
65    * translation and cleared afterwards.
66    */
67   GDK_EVENT_PENDING = 1 << 0
68 } GdkEventFlags;
69
70 struct _GdkIOClosure
71 {
72   GdkInputFunction function;
73   GdkInputCondition condition;
74   GdkDestroyNotify notify;
75   gpointer data;
76 };
77
78 struct _GdkEventPrivate
79 {
80   GdkEvent event;
81   guint    flags;
82 };
83
84 struct _GdkDisplaySource
85 {
86   GSource source;
87   
88   GdkDisplay *display;
89   GPollFD event_poll_fd;
90 };
91
92 /* 
93  * Private function declarations
94  */
95
96 static gint      gdk_event_apply_filters (XEvent   *xevent,
97                                           GdkEvent *event,
98                                           GList    *filters);
99 static gboolean  gdk_event_translate     (GdkDisplay *display,
100                                           GdkEvent   *event, 
101                                           XEvent     *xevent,
102                                           gboolean    return_exposes);
103
104 static gboolean gdk_event_prepare  (GSource     *source,
105                                     gint        *timeout);
106 static gboolean gdk_event_check    (GSource     *source);
107 static gboolean gdk_event_dispatch (GSource     *source,
108                                     GSourceFunc  callback,
109                                     gpointer     user_data);
110
111 static GdkFilterReturn gdk_wm_protocols_filter (GdkXEvent *xev,
112                                                 GdkEvent  *event,
113                                                 gpointer   data);
114
115 static GSource *gdk_display_source_new (GdkDisplay *display);
116 static gboolean gdk_check_xpending     (GdkDisplay *display);
117
118 static void gdk_xsettings_watch_cb  (Window            window,
119                                      Bool              is_start,
120                                      long              mask,
121                                      void             *cb_data);
122 static void gdk_xsettings_notify_cb (const char       *name,
123                                      XSettingsAction   action,
124                                      XSettingsSetting *setting,
125                                      void             *data);
126
127 /* Private variable declarations
128  */
129
130 static GList *display_sources;
131
132 static GSourceFuncs event_funcs = {
133   gdk_event_prepare,
134   gdk_event_check,
135   gdk_event_dispatch,
136   NULL
137 };
138
139 static GSource *
140 gdk_display_source_new (GdkDisplay *display)
141 {
142   GSource *source = g_source_new (&event_funcs, sizeof (GdkDisplaySource));
143   GdkDisplaySource *display_source = (GdkDisplaySource *)source;
144   
145   display_source->display = display;
146   
147   return source;
148 }
149
150 static gboolean
151 gdk_check_xpending (GdkDisplay *display)
152 {
153   return XPending (GDK_DISPLAY_XDISPLAY (display));
154 }
155
156 /*********************************************
157  * Functions for maintaining the event queue *
158  *********************************************/
159
160 void
161 _gdk_x11_events_init_screen (GdkScreen *screen)
162 {
163   GdkScreenX11 *screen_x11 = GDK_SCREEN_X11 (screen);
164   
165   screen_x11->xsettings_client = xsettings_client_new (screen_x11->xdisplay,
166                                                        screen_x11->screen_num,
167                                                        gdk_xsettings_notify_cb,
168                                                        gdk_xsettings_watch_cb,
169                                                        screen);
170 }
171
172 void
173 _gdk_x11_events_uninit_screen (GdkScreen *screen)
174 {
175   GdkScreenX11 *screen_x11 = GDK_SCREEN_X11 (screen);
176
177   xsettings_client_destroy (screen_x11->xsettings_client);
178   screen_x11->xsettings_client = NULL;
179 }
180
181 void 
182 _gdk_events_init (GdkDisplay *display)
183 {
184   GSource *source;
185   GdkDisplaySource *display_source;
186   GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
187   
188   int connection_number = ConnectionNumber (display_x11->xdisplay);
189   GDK_NOTE (MISC, g_message ("connection number: %d", connection_number));
190
191
192   source = display_x11->event_source = gdk_display_source_new (display);
193   display_source = (GdkDisplaySource*) source;
194   g_source_set_priority (source, GDK_PRIORITY_EVENTS);
195   
196   display_source->event_poll_fd.fd = connection_number;
197   display_source->event_poll_fd.events = G_IO_IN;
198   
199   g_source_add_poll (source, &display_source->event_poll_fd);
200   g_source_set_can_recurse (source, TRUE);
201   g_source_attach (source, NULL);
202
203   display_sources = g_list_prepend (display_sources,display_source);
204
205   gdk_display_add_client_message_filter (
206         display,
207         gdk_atom_intern ("WM_PROTOCOLS", FALSE), 
208         gdk_wm_protocols_filter,   
209         NULL);
210 }
211
212
213 /**
214  * gdk_events_pending:
215  * 
216  * Checks if any events are ready to be processed for any display.
217  * 
218  * Return value:  %TRUE if any events are pending.
219  **/
220 gboolean
221 gdk_events_pending (void)
222 {
223   GList *tmp_list;
224
225   for (tmp_list = display_sources; tmp_list; tmp_list = tmp_list->next)
226     {
227       GdkDisplaySource *tmp_source = tmp_list->data;
228       GdkDisplay *display = tmp_source->display;
229       
230       if (_gdk_event_queue_find_first (display))
231         return TRUE;
232     }
233
234   for (tmp_list = display_sources; tmp_list; tmp_list = tmp_list->next)
235     {
236       GdkDisplaySource *tmp_source = tmp_list->data;
237       GdkDisplay *display = tmp_source->display;
238       
239       if (gdk_check_xpending (display))
240         return TRUE;
241     }
242   
243   return FALSE;
244 }
245
246 static Bool
247 graphics_expose_predicate (Display  *display,
248                            XEvent   *xevent,
249                            XPointer  arg)
250 {
251   if (xevent->xany.window == GDK_DRAWABLE_XID ((GdkDrawable *)arg) &&
252       (xevent->xany.type == GraphicsExpose ||
253        xevent->xany.type == NoExpose))
254     return True;
255   else
256     return False;
257 }
258
259 /**
260  * gdk_event_get_graphics_expose:
261  * @window: the #GdkWindow to wait for the events for.
262  * 
263  * Waits for a GraphicsExpose or NoExpose event from the X server.
264  * This is used in the #GtkText and #GtkCList widgets in GTK+ to make sure any
265  * GraphicsExpose events are handled before the widget is scrolled.
266  *
267  * Return value:  a #GdkEventExpose if a GraphicsExpose was received, or %NULL if a
268  * NoExpose event was received.
269  **/
270 GdkEvent*
271 gdk_event_get_graphics_expose (GdkWindow *window)
272 {
273   XEvent xevent;
274   GdkEvent *event;
275   
276   g_return_val_if_fail (window != NULL, NULL);
277   
278   XIfEvent (GDK_WINDOW_XDISPLAY (window), &xevent, 
279             graphics_expose_predicate, (XPointer) window);
280   
281   if (xevent.xany.type == GraphicsExpose)
282     {
283       event = _gdk_event_new ();
284       
285       if (gdk_event_translate (GDK_WINDOW_DISPLAY (window), event,
286                                &xevent, TRUE))
287         return event;
288       else
289         gdk_event_free (event);
290     }
291   
292   return NULL;  
293 }
294
295 static gint
296 gdk_event_apply_filters (XEvent *xevent,
297                          GdkEvent *event,
298                          GList *filters)
299 {
300   GList *tmp_list;
301   GdkFilterReturn result;
302   
303   tmp_list = filters;
304   
305   while (tmp_list)
306     {
307       GdkEventFilter *filter = (GdkEventFilter*) tmp_list->data;
308       
309       tmp_list = tmp_list->next;
310       result = filter->function (xevent, event, filter->data);
311       if (result !=  GDK_FILTER_CONTINUE)
312         return result;
313     }
314   
315   return GDK_FILTER_CONTINUE;
316 }
317
318 /**
319  * gdk_display_add_client_message_filter:
320  * @display: a #GdkDisplay for which this message filter applies
321  * @message_type: the type of ClientMessage events to receive.
322  *   This will be checked against the @message_type field 
323  *   of the XClientMessage event struct.
324  * @func: the function to call to process the event.
325  * @data: user data to pass to @func.
326  *
327  * Adds a filter to be called when X ClientMessage events are received.
328  *
329  **/ 
330 void 
331 gdk_display_add_client_message_filter (GdkDisplay   *display,
332                                        GdkAtom       message_type,
333                                        GdkFilterFunc func,
334                                        gpointer      data)
335 {
336   GdkClientFilter *filter;
337   g_return_if_fail (GDK_IS_DISPLAY (display));
338   filter = g_new (GdkClientFilter, 1);
339
340   filter->type = message_type;
341   filter->function = func;
342   filter->data = data;
343   
344   GDK_DISPLAY_X11(display)->client_filters = 
345     g_list_prepend (GDK_DISPLAY_X11 (display)->client_filters,
346                     filter);
347 }
348
349 /**
350  * gdk_add_client_message_filter:
351  * @message_type: the type of ClientMessage events to receive. This will be
352  *     checked against the <structfield>message_type</structfield> field of the
353  *     XClientMessage event struct.
354  * @func: the function to call to process the event.
355  * @data: user data to pass to @func. 
356  * 
357  * Adds a filter to the default display to be called when X ClientMessage events
358  * are received. See gdk_display_add_client_message_filter().
359  **/
360 void 
361 gdk_add_client_message_filter (GdkAtom       message_type,
362                                GdkFilterFunc func,
363                                gpointer      data)
364 {
365   gdk_display_add_client_message_filter (gdk_display_get_default (),
366                                          message_type, func, data);
367 }
368
369 static void
370 gdk_check_wm_state_changed (GdkWindow *window)
371 {  
372   Atom type;
373   gint format;
374   gulong nitems;
375   gulong bytes_after;
376   Atom *atoms = NULL;
377   gulong i;
378   gboolean found_sticky, found_maxvert, found_maxhorz;
379   GdkWindowState old_state;
380   GdkDisplay *display = GDK_WINDOW_DISPLAY (window);
381   
382   if (GDK_WINDOW_DESTROYED (window) ||
383       gdk_window_get_window_type (window) != GDK_WINDOW_TOPLEVEL)
384     return;
385   
386   found_sticky = FALSE;
387   found_maxvert = FALSE;
388   found_maxhorz = FALSE;
389   
390   XGetWindowProperty (GDK_WINDOW_XDISPLAY (window), GDK_WINDOW_XID (window),
391                       gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_STATE"),
392                       0, G_MAXLONG, False, XA_ATOM, &type, &format, &nitems,
393                       &bytes_after, (guchar **)&atoms);
394
395   if (type != None)
396     {
397       Atom sticky_atom = gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_STATE_STICKY");
398       Atom maxvert_atom = gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_STATE_MAXIMIZED_VERT");
399       Atom maxhorz_atom = gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_STATE_MAXIMIZED_HORZ");
400
401       i = 0;
402       while (i < nitems)
403         {
404           if (atoms[i] == sticky_atom)
405             found_sticky = TRUE;
406           else if (atoms[i] == maxvert_atom)
407             found_maxvert = TRUE;
408           else if (atoms[i] == maxhorz_atom)
409             found_maxhorz = TRUE;
410
411           ++i;
412         }
413
414       XFree (atoms);
415     }
416
417   /* For found_sticky to remain TRUE, we have to also be on desktop
418    * 0xFFFFFFFF
419    */
420
421   if (found_sticky)
422     {
423       gulong *desktop;
424       
425       XGetWindowProperty (GDK_WINDOW_XDISPLAY (window), 
426                           GDK_WINDOW_XID (window),
427                           gdk_x11_get_xatom_by_name_for_display 
428                           (display, "_NET_WM_DESKTOP"),
429                           0, G_MAXLONG, False, XA_CARDINAL, &type, 
430                           &format, &nitems,
431                           &bytes_after, (guchar **)&desktop);
432
433       if (type != None)
434         {
435           if (*desktop != 0xFFFFFFFF)
436             found_sticky = FALSE;
437           XFree (desktop);
438         }
439     }
440           
441   old_state = gdk_window_get_state (window);
442
443   if (old_state & GDK_WINDOW_STATE_STICKY)
444     {
445       if (!found_sticky)
446         gdk_synthesize_window_state (window,
447                                      GDK_WINDOW_STATE_STICKY,
448                                      0);
449     }
450   else
451     {
452       if (found_sticky)
453         gdk_synthesize_window_state (window,
454                                      0,
455                                      GDK_WINDOW_STATE_STICKY);
456     }
457
458   /* Our "maximized" means both vertical and horizontal; if only one,
459    * we don't expose that via GDK
460    */
461   if (old_state & GDK_WINDOW_STATE_MAXIMIZED)
462     {
463       if (!(found_maxvert && found_maxhorz))
464         gdk_synthesize_window_state (window,
465                                      GDK_WINDOW_STATE_MAXIMIZED,
466                                      0);
467     }
468   else
469     {
470       if (found_maxvert && found_maxhorz)
471         gdk_synthesize_window_state (window,
472                                      0,
473                                      GDK_WINDOW_STATE_MAXIMIZED);
474     }
475 }
476
477 #define HAS_FOCUS(window_impl)                           \
478   ((window_impl)->has_focus || (window_impl)->has_pointer_focus)
479
480 static void
481 generate_focus_event (GdkWindow *window,
482                       gboolean   in)
483 {
484   GdkEvent event;
485   
486   event.type = GDK_FOCUS_CHANGE;
487   event.focus_change.window = window;
488   event.focus_change.send_event = FALSE;
489   event.focus_change.in = in;
490   
491   gdk_event_put (&event);
492 }
493
494 static gboolean
495 gdk_event_translate (GdkDisplay *display,
496                      GdkEvent   *event,
497                      XEvent     *xevent,
498                      gboolean    return_exposes)
499 {
500   
501   GdkWindow *window;
502   GdkWindowObject *window_private;
503   GdkWindowImplX11 *window_impl = NULL;
504   static XComposeStatus compose;
505   KeySym keysym;
506   int charcount;
507   char buf[16];
508   gint return_val;
509   gint xoffset, yoffset;
510   GdkScreen *screen = NULL;
511   GdkScreenX11 *screen_x11 = NULL;
512   GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
513   
514   return_val = FALSE;
515
516   /* init these, since the done: block uses them */
517   window = NULL;
518   window_private = NULL;
519   event->any.window = NULL;
520
521   if (_gdk_default_filters)
522     {
523       /* Apply global filters */
524       GdkFilterReturn result;
525       result = gdk_event_apply_filters (xevent, event,
526                                         _gdk_default_filters);
527       
528       if (result != GDK_FILTER_CONTINUE)
529         {
530           return_val = (result == GDK_FILTER_TRANSLATE) ? TRUE : FALSE;
531           goto done;
532         }
533     }  
534
535    /* We handle events with window=None
536     *  specially - they are generated by XFree86's XInput under
537     *  some circumstances. This handling for obvious reasons
538     * goes before we bother to lookup the event window.
539     */
540   
541   if (xevent->xany.window == None)
542     {
543       return_val = _gdk_input_window_none_event (event, xevent);
544       
545       if (return_val >= 0)      /* was handled */
546         return return_val;
547       else
548         return_val = FALSE;
549     }
550
551   /* Find the GdkWindow that this event occurred in. */
552   
553   window = gdk_window_lookup_for_display (display, xevent->xany.window);
554   window_private = (GdkWindowObject *) window;
555
556   if (window)
557     {
558       screen = GDK_WINDOW_SCREEN (window);
559       screen_x11 = GDK_SCREEN_X11 (screen);
560     }
561     
562   if (window != NULL)
563     {
564       /* Window may be a pixmap, so check its type.
565        * (This check is probably too expensive unless
566        *  GLib short-circuits an exact type match,
567        *  which has been proposed)
568        */
569       if (GDK_IS_WINDOW (window))
570         {
571           window_impl = GDK_WINDOW_IMPL_X11 (window_private->impl);
572           
573           if (xevent->xany.window != GDK_WINDOW_XID (window))
574             {
575               g_assert (xevent->xany.window == window_impl->focus_window);
576               
577               switch (xevent->type)
578                 {
579                 case KeyPress:
580                 case KeyRelease:
581                   xevent->xany.window = GDK_WINDOW_XID (window);
582                   break;
583                 default:
584                   return FALSE;
585                 }
586             }
587         }
588
589       g_object_ref (G_OBJECT (window));
590     }
591
592   event->any.window = window;
593   event->any.send_event = xevent->xany.send_event ? TRUE : FALSE;
594   
595   if (window_private && GDK_WINDOW_DESTROYED (window))
596     {
597       if (xevent->type != DestroyNotify)
598         {
599           return_val = FALSE;
600           goto done;
601         }
602     }
603   else if (window_private)
604     {
605       /* Apply per-window filters */
606       GdkFilterReturn result;
607       result = gdk_event_apply_filters (xevent, event,
608                                         window_private->filters);
609       
610       if (result != GDK_FILTER_CONTINUE)
611         {
612           return_val = (result == GDK_FILTER_TRANSLATE) ? TRUE : FALSE;
613           goto done;
614         }
615     }
616       
617   if (screen_x11 && screen_x11->wmspec_check_window != None &&
618       xevent->xany.window == screen_x11->wmspec_check_window)
619     {
620       if (xevent->type == DestroyNotify)
621         screen_x11->wmspec_check_window = None;
622       
623       /* Eat events on this window unless someone had wrapped
624        * it as a foreign window
625        */
626       if (window == NULL)
627         {
628           return_val = FALSE;
629           goto done;
630         }
631     }
632
633   if (window &&
634       (xevent->xany.type == MotionNotify ||
635        xevent->xany.type == ButtonRelease))
636     {
637       if (_gdk_moveresize_handle_event (xevent))
638         {
639           return_val = FALSE;
640           goto done;
641         }
642     }
643   
644   /* We do a "manual" conversion of the XEvent to a
645    *  GdkEvent. The structures are mostly the same so
646    *  the conversion is fairly straightforward. We also
647    *  optionally print debugging info regarding events
648    *  received.
649    */
650
651   return_val = TRUE;
652
653   if (window)
654     {
655       _gdk_windowing_window_get_offsets (window, &xoffset, &yoffset);
656     }
657   else
658     {
659       xoffset = 0;
660       yoffset = 0;
661     }
662
663   switch (xevent->type)
664     {
665     case KeyPress:
666       if (window_private == NULL)
667         {
668           return_val = FALSE;
669           break;
670         }
671       
672       /* Lookup the string corresponding to the given keysym.
673        */
674
675       charcount = XLookupString (&xevent->xkey, buf, 16,
676                                  &keysym, &compose);
677       event->key.keyval = keysym;
678       event->key.hardware_keycode = xevent->xkey.keycode;
679       
680       if (charcount > 0 && buf[charcount-1] == '\0')
681         charcount --;
682       else
683         buf[charcount] = '\0';
684       
685 #ifdef G_ENABLE_DEBUG
686       if (_gdk_debug_flags & GDK_DEBUG_EVENTS)
687         {
688           g_message ("key press:\twindow: %ld  key: %12s  %d",
689                      xevent->xkey.window,
690                      event->key.keyval ? XKeysymToString (event->key.keyval) : "(none)",
691                      event->key.keyval);
692           if (charcount > 0)
693             g_message ("\t\tlength: %4d string: \"%s\"",
694                        charcount, buf);
695         }
696 #endif /* G_ENABLE_DEBUG */
697
698       event->key.type = GDK_KEY_PRESS;
699       event->key.window = window;
700       event->key.time = xevent->xkey.time;
701       event->key.state = (GdkModifierType) xevent->xkey.state;
702       event->key.string = g_strdup (buf);
703       event->key.length = charcount;
704
705       /* bits 13 and 14 in the "state" field are the keyboard group */
706 #define KEYBOARD_GROUP_SHIFT 13
707 #define KEYBOARD_GROUP_MASK ((1 << 13) | (1 << 14))
708       
709       event->key.group = _gdk_x11_get_group_for_state (display, xevent->xkey.state);
710       
711       break;
712       
713     case KeyRelease:
714       if (window_private == NULL)
715         {
716           return_val = FALSE;
717           break;
718         }
719       
720       /* Lookup the string corresponding to the given keysym.
721        */
722
723       /* Emulate detectable auto-repeat by checking to see
724        * if the next event is a key press with the same
725        * keycode and timestamp, and if so, ignoring the event.
726        */
727
728       if (!display_x11->have_xkb_autorepeat && XPending (xevent->xkey.display))
729         {
730           XEvent next_event;
731
732           XPeekEvent (xevent->xkey.display, &next_event);
733
734           if (next_event.type == KeyPress &&
735               next_event.xkey.keycode == xevent->xkey.keycode &&
736               next_event.xkey.time == xevent->xkey.time)
737             break;
738         }
739       
740       keysym = GDK_VoidSymbol;
741       charcount = XLookupString (&xevent->xkey, buf, 16,
742                                  &keysym, &compose);
743       event->key.keyval = keysym;      
744       event->key.hardware_keycode = xevent->xkey.keycode;
745       
746       GDK_NOTE (EVENTS, 
747                 g_message ("key release:\t\twindow: %ld  key: %12s  %d",
748                            xevent->xkey.window,
749                            XKeysymToString (event->key.keyval),
750                            event->key.keyval));
751       
752       event->key.type = GDK_KEY_RELEASE;
753       event->key.window = window;
754       event->key.time = xevent->xkey.time;
755       event->key.state = (GdkModifierType) xevent->xkey.state;
756       event->key.length = 0;
757       event->key.string = NULL;
758       
759       event->key.group = (xevent->xkey.state & KEYBOARD_GROUP_MASK) >> KEYBOARD_GROUP_SHIFT;
760
761       break;
762       
763     case ButtonPress:
764       GDK_NOTE (EVENTS, 
765                 g_message ("button press:\t\twindow: %ld  x,y: %d %d  button: %d",
766                            xevent->xbutton.window,
767                            xevent->xbutton.x, xevent->xbutton.y,
768                            xevent->xbutton.button));
769       
770       if (window_private == NULL || 
771           ((window_private->extension_events != 0) &&
772            display_x11->input_ignore_core))
773         {
774           return_val = FALSE;
775           break;
776         }
777       
778       /* If we get a ButtonPress event where the button is 4 or 5,
779          it's a Scroll event */
780       switch (xevent->xbutton.button)
781         {
782         case 4: /* up */
783         case 5: /* down */
784         case 6: /* left */
785         case 7: /* right */
786           event->scroll.type = GDK_SCROLL;
787
788           if (xevent->xbutton.button == 4)
789             event->scroll.direction = GDK_SCROLL_UP;
790           else if (xevent->xbutton.button == 5)
791             event->scroll.direction = GDK_SCROLL_DOWN;
792           else if (xevent->xbutton.button == 6)
793             event->scroll.direction = GDK_SCROLL_LEFT;
794           else
795             event->scroll.direction = GDK_SCROLL_RIGHT;
796
797           event->scroll.window = window;
798           event->scroll.time = xevent->xbutton.time;
799           event->scroll.x = xevent->xbutton.x + xoffset;
800           event->scroll.y = xevent->xbutton.y + yoffset;
801           event->scroll.x_root = (gfloat)xevent->xbutton.x_root;
802           event->scroll.y_root = (gfloat)xevent->xbutton.y_root;
803           event->scroll.state = (GdkModifierType) xevent->xbutton.state;
804           event->scroll.device = display->core_pointer;
805           break;
806           
807         default:
808           event->button.type = GDK_BUTTON_PRESS;
809           event->button.window = window;
810           event->button.time = xevent->xbutton.time;
811           event->button.x = xevent->xbutton.x + xoffset;
812           event->button.y = xevent->xbutton.y + yoffset;
813           event->button.x_root = (gfloat)xevent->xbutton.x_root;
814           event->button.y_root = (gfloat)xevent->xbutton.y_root;
815           event->button.axes = NULL;
816           event->button.state = (GdkModifierType) xevent->xbutton.state;
817           event->button.button = xevent->xbutton.button;
818           event->button.device = display->core_pointer;
819           
820           _gdk_event_button_generate (display, event);
821           break;
822         }
823
824       break;
825       
826     case ButtonRelease:
827       GDK_NOTE (EVENTS, 
828                 g_message ("button release:\twindow: %ld  x,y: %d %d  button: %d",
829                            xevent->xbutton.window,
830                            xevent->xbutton.x, xevent->xbutton.y,
831                            xevent->xbutton.button));
832       
833       if (window_private == NULL ||
834           ((window_private->extension_events != 0) &&
835            display_x11->input_ignore_core))
836         {
837           return_val = FALSE;
838           break;
839         }
840       
841       /* We treat button presses as scroll wheel events, so ignore the release */
842       if (xevent->xbutton.button == 4 || xevent->xbutton.button == 5 ||
843           xevent->xbutton.button == 6 || xevent->xbutton.button ==7)
844         {
845           return_val = FALSE;
846           break;
847         }
848
849       event->button.type = GDK_BUTTON_RELEASE;
850       event->button.window = window;
851       event->button.time = xevent->xbutton.time;
852       event->button.x = xevent->xbutton.x + xoffset;
853       event->button.y = xevent->xbutton.y + yoffset;
854       event->button.x_root = (gfloat)xevent->xbutton.x_root;
855       event->button.y_root = (gfloat)xevent->xbutton.y_root;
856       event->button.axes = NULL;
857       event->button.state = (GdkModifierType) xevent->xbutton.state;
858       event->button.button = xevent->xbutton.button;
859       event->button.device = display->core_pointer;
860       
861       break;
862       
863     case MotionNotify:
864       GDK_NOTE (EVENTS,
865                 g_message ("motion notify:\t\twindow: %ld  x,y: %d %d  hint: %s", 
866                            xevent->xmotion.window,
867                            xevent->xmotion.x, xevent->xmotion.y,
868                            (xevent->xmotion.is_hint) ? "true" : "false"));
869       
870       if (window_private == NULL ||
871           ((window_private->extension_events != 0) &&
872            display_x11->input_ignore_core))
873         {
874           return_val = FALSE;
875           break;
876         }
877       
878       event->motion.type = GDK_MOTION_NOTIFY;
879       event->motion.window = window;
880       event->motion.time = xevent->xmotion.time;
881       event->motion.x = xevent->xmotion.x + xoffset;
882       event->motion.y = xevent->xmotion.y + yoffset;
883       event->motion.x_root = (gfloat)xevent->xmotion.x_root;
884       event->motion.y_root = (gfloat)xevent->xmotion.y_root;
885       event->motion.axes = NULL;
886       event->motion.state = (GdkModifierType) xevent->xmotion.state;
887       event->motion.is_hint = xevent->xmotion.is_hint;
888       event->motion.device = display->core_pointer;
889       
890       break;
891       
892     case EnterNotify:
893       GDK_NOTE (EVENTS,
894                 g_message ("enter notify:\t\twindow: %ld  detail: %d subwin: %ld",
895                            xevent->xcrossing.window,
896                            xevent->xcrossing.detail,
897                            xevent->xcrossing.subwindow));
898  
899       if (window_private == NULL)
900         {
901           return_val = FALSE;
902           break;
903         }
904       
905       /* Handle focusing (in the case where no window manager is running */
906       if (window &&
907           GDK_WINDOW_TYPE (window) != GDK_WINDOW_CHILD &&
908           xevent->xcrossing.detail != NotifyInferior &&
909           xevent->xcrossing.focus && !window_impl->has_focus)
910         {
911           gboolean had_focus = HAS_FOCUS (window_impl);
912           
913           window_impl->has_pointer_focus = TRUE;
914
915           if (HAS_FOCUS (window_impl) != had_focus)
916             generate_focus_event (window, TRUE);
917         }
918
919       /* Tell XInput stuff about it if appropriate */
920       if (window_private &&
921           !GDK_WINDOW_DESTROYED (window) &&
922           window_private->extension_events != 0)
923         _gdk_input_enter_event (&xevent->xcrossing, window);
924       
925       event->crossing.type = GDK_ENTER_NOTIFY;
926       event->crossing.window = window;
927       
928       /* If the subwindow field of the XEvent is non-NULL, then
929        *  lookup the corresponding GdkWindow.
930        */
931       if (xevent->xcrossing.subwindow != None)
932         event->crossing.subwindow = gdk_window_lookup_for_display (display, xevent->xcrossing.subwindow);
933       else
934         event->crossing.subwindow = NULL;
935       
936       event->crossing.time = xevent->xcrossing.time;
937       event->crossing.x = xevent->xcrossing.x + xoffset;
938       event->crossing.y = xevent->xcrossing.y + yoffset;
939       event->crossing.x_root = xevent->xcrossing.x_root;
940       event->crossing.y_root = xevent->xcrossing.y_root;
941       
942       /* Translate the crossing mode into Gdk terms.
943        */
944       switch (xevent->xcrossing.mode)
945         {
946         case NotifyNormal:
947           event->crossing.mode = GDK_CROSSING_NORMAL;
948           break;
949         case NotifyGrab:
950           event->crossing.mode = GDK_CROSSING_GRAB;
951           break;
952         case NotifyUngrab:
953           event->crossing.mode = GDK_CROSSING_UNGRAB;
954           break;
955         };
956       
957       /* Translate the crossing detail into Gdk terms.
958        */
959       switch (xevent->xcrossing.detail)
960         {
961         case NotifyInferior:
962           event->crossing.detail = GDK_NOTIFY_INFERIOR;
963           break;
964         case NotifyAncestor:
965           event->crossing.detail = GDK_NOTIFY_ANCESTOR;
966           break;
967         case NotifyVirtual:
968           event->crossing.detail = GDK_NOTIFY_VIRTUAL;
969           break;
970         case NotifyNonlinear:
971           event->crossing.detail = GDK_NOTIFY_NONLINEAR;
972           break;
973         case NotifyNonlinearVirtual:
974           event->crossing.detail = GDK_NOTIFY_NONLINEAR_VIRTUAL;
975           break;
976         default:
977           event->crossing.detail = GDK_NOTIFY_UNKNOWN;
978           break;
979         }
980       
981       event->crossing.focus = xevent->xcrossing.focus;
982       event->crossing.state = xevent->xcrossing.state;
983   
984       break;
985       
986     case LeaveNotify:
987       GDK_NOTE (EVENTS, 
988                 g_message ("leave notify:\t\twindow: %ld  detail: %d subwin: %ld",
989                            xevent->xcrossing.window,
990                            xevent->xcrossing.detail, xevent->xcrossing.subwindow));
991
992       if (window_private == NULL)
993         {
994           return_val = FALSE;
995           break;
996         }
997       
998       /* Handle focusing (in the case where no window manager is running */
999       if (window &&
1000           GDK_WINDOW_TYPE (window) != GDK_WINDOW_CHILD &&
1001           xevent->xcrossing.detail != NotifyInferior &&
1002           xevent->xcrossing.focus && !window_impl->has_focus)
1003         {
1004           gboolean had_focus = HAS_FOCUS (window_impl);
1005           
1006           window_impl->has_pointer_focus = FALSE;
1007
1008           if (HAS_FOCUS (window_impl) != had_focus)
1009             generate_focus_event (window, FALSE);
1010         }
1011
1012       event->crossing.type = GDK_LEAVE_NOTIFY;
1013       event->crossing.window = window;
1014       
1015       /* If the subwindow field of the XEvent is non-NULL, then
1016        *  lookup the corresponding GdkWindow.
1017        */
1018       if (xevent->xcrossing.subwindow != None)
1019         event->crossing.subwindow = gdk_window_lookup_for_display (display, xevent->xcrossing.subwindow);
1020       else
1021         event->crossing.subwindow = NULL;
1022       
1023       event->crossing.time = xevent->xcrossing.time;
1024       event->crossing.x = xevent->xcrossing.x + xoffset;
1025       event->crossing.y = xevent->xcrossing.y + yoffset;
1026       event->crossing.x_root = xevent->xcrossing.x_root;
1027       event->crossing.y_root = xevent->xcrossing.y_root;
1028       
1029       /* Translate the crossing mode into Gdk terms.
1030        */
1031       switch (xevent->xcrossing.mode)
1032         {
1033         case NotifyNormal:
1034           event->crossing.mode = GDK_CROSSING_NORMAL;
1035           break;
1036         case NotifyGrab:
1037           event->crossing.mode = GDK_CROSSING_GRAB;
1038           break;
1039         case NotifyUngrab:
1040           event->crossing.mode = GDK_CROSSING_UNGRAB;
1041           break;
1042         };
1043       
1044       /* Translate the crossing detail into Gdk terms.
1045        */
1046       switch (xevent->xcrossing.detail)
1047         {
1048         case NotifyInferior:
1049           event->crossing.detail = GDK_NOTIFY_INFERIOR;
1050           break;
1051         case NotifyAncestor:
1052           event->crossing.detail = GDK_NOTIFY_ANCESTOR;
1053           break;
1054         case NotifyVirtual:
1055           event->crossing.detail = GDK_NOTIFY_VIRTUAL;
1056           break;
1057         case NotifyNonlinear:
1058           event->crossing.detail = GDK_NOTIFY_NONLINEAR;
1059           break;
1060         case NotifyNonlinearVirtual:
1061           event->crossing.detail = GDK_NOTIFY_NONLINEAR_VIRTUAL;
1062           break;
1063         default:
1064           event->crossing.detail = GDK_NOTIFY_UNKNOWN;
1065           break;
1066         }
1067       
1068       event->crossing.focus = xevent->xcrossing.focus;
1069       event->crossing.state = xevent->xcrossing.state;
1070       
1071       break;
1072       
1073       /* We only care about focus events that indicate that _this_
1074        * window (not a ancestor or child) got or lost the focus
1075        */
1076     case FocusIn:
1077       GDK_NOTE (EVENTS,
1078                 g_message ("focus in:\t\twindow: %ld", xevent->xfocus.window));
1079       
1080       if (window && GDK_WINDOW_TYPE (window) != GDK_WINDOW_CHILD)
1081         {
1082           gboolean had_focus = HAS_FOCUS (window_impl);
1083           
1084           switch (xevent->xfocus.detail)
1085             {
1086             case NotifyAncestor:
1087             case NotifyNonlinear:
1088             case NotifyVirtual:
1089             case NotifyNonlinearVirtual:
1090               window_impl->has_focus = TRUE;
1091               break;
1092             case NotifyPointer:
1093               window_impl->has_pointer_focus = TRUE;
1094               break;
1095             case NotifyInferior:
1096             case NotifyPointerRoot:
1097             case NotifyDetailNone:
1098               break;
1099             }
1100
1101           if (HAS_FOCUS (window_impl) != had_focus)
1102             generate_focus_event (window, TRUE);
1103         }
1104       break;
1105     case FocusOut:
1106       GDK_NOTE (EVENTS,
1107                 g_message ("focus out:\t\twindow: %ld", xevent->xfocus.window));
1108
1109       if (window && GDK_WINDOW_TYPE (window) != GDK_WINDOW_CHILD)
1110         {
1111           gboolean had_focus = HAS_FOCUS (window_impl);
1112             
1113           switch (xevent->xfocus.detail)
1114             {
1115             case NotifyAncestor:
1116             case NotifyNonlinear:
1117             case NotifyVirtual:
1118             case NotifyNonlinearVirtual:
1119               window_impl->has_focus = FALSE;
1120               break;
1121             case NotifyPointer:
1122               window_impl->has_pointer_focus = FALSE;
1123             break;
1124             case NotifyInferior:
1125             case NotifyPointerRoot:
1126             case NotifyDetailNone:
1127               break;
1128             }
1129
1130           if (HAS_FOCUS (window_impl) != had_focus)
1131             generate_focus_event (window, FALSE);
1132         }
1133       break;
1134
1135 #if 0      
1136           /* gdk_keyboard_grab() causes following events. These events confuse
1137            * the XIM focus, so ignore them.
1138            */
1139           if (xevent->xfocus.mode == NotifyGrab ||
1140               xevent->xfocus.mode == NotifyUngrab)
1141             break;
1142 #endif
1143
1144     case KeymapNotify:
1145       GDK_NOTE (EVENTS,
1146                 g_message ("keymap notify"));
1147
1148       /* Not currently handled */
1149       return_val = FALSE;
1150       break;
1151       
1152     case Expose:
1153       GDK_NOTE (EVENTS,
1154                 g_message ("expose:\t\twindow: %ld  %d  x,y: %d %d  w,h: %d %d%s",
1155                            xevent->xexpose.window, xevent->xexpose.count,
1156                            xevent->xexpose.x, xevent->xexpose.y,
1157                            xevent->xexpose.width, xevent->xexpose.height,
1158                            event->any.send_event ? " (send)" : ""));
1159       
1160       if (window_private == NULL)
1161         {
1162           return_val = FALSE;
1163           break;
1164         }
1165       
1166       {
1167         GdkRectangle expose_rect;
1168
1169         expose_rect.x = xevent->xexpose.x + xoffset;
1170         expose_rect.y = xevent->xexpose.y + yoffset;
1171         expose_rect.width = xevent->xexpose.width;
1172         expose_rect.height = xevent->xexpose.height;
1173
1174         if (return_exposes)
1175           {
1176             event->expose.type = GDK_EXPOSE;
1177             event->expose.area = expose_rect;
1178             event->expose.region = gdk_region_rectangle (&expose_rect);
1179             event->expose.window = window;
1180             event->expose.count = xevent->xexpose.count;
1181
1182             return_val = TRUE;
1183           }
1184         else
1185           {
1186             _gdk_window_process_expose (window, xevent->xexpose.serial, &expose_rect);
1187             return_val = FALSE;
1188           }
1189         
1190         return_val = FALSE;
1191       }
1192         
1193       break;
1194       
1195     case GraphicsExpose:
1196       {
1197         GdkRectangle expose_rect;
1198
1199         GDK_NOTE (EVENTS,
1200                   g_message ("graphics expose:\tdrawable: %ld",
1201                              xevent->xgraphicsexpose.drawable));
1202  
1203         if (window_private == NULL)
1204           {
1205             return_val = FALSE;
1206             break;
1207           }
1208         
1209         expose_rect.x = xevent->xgraphicsexpose.x + xoffset;
1210         expose_rect.y = xevent->xgraphicsexpose.y + yoffset;
1211         expose_rect.width = xevent->xgraphicsexpose.width;
1212         expose_rect.height = xevent->xgraphicsexpose.height;
1213             
1214         if (return_exposes)
1215           {
1216             event->expose.type = GDK_EXPOSE;
1217             event->expose.area = expose_rect;
1218             event->expose.region = gdk_region_rectangle (&expose_rect);
1219             event->expose.window = window;
1220             event->expose.count = xevent->xgraphicsexpose.count;
1221
1222             return_val = TRUE;
1223           }
1224         else
1225           {
1226             _gdk_window_process_expose (window, xevent->xgraphicsexpose.serial, &expose_rect);
1227             
1228             return_val = FALSE;
1229           }
1230         
1231       }
1232       break;
1233       
1234     case NoExpose:
1235       GDK_NOTE (EVENTS,
1236                 g_message ("no expose:\t\tdrawable: %ld",
1237                            xevent->xnoexpose.drawable));
1238       
1239       event->no_expose.type = GDK_NO_EXPOSE;
1240       event->no_expose.window = window;
1241       
1242       break;
1243       
1244     case VisibilityNotify:
1245 #ifdef G_ENABLE_DEBUG
1246       if (_gdk_debug_flags & GDK_DEBUG_EVENTS)
1247         switch (xevent->xvisibility.state)
1248           {
1249           case VisibilityFullyObscured:
1250             g_message ("visibility notify:\twindow: %ld  none",
1251                        xevent->xvisibility.window);
1252             break;
1253           case VisibilityPartiallyObscured:
1254             g_message ("visibility notify:\twindow: %ld  partial",
1255                        xevent->xvisibility.window);
1256             break;
1257           case VisibilityUnobscured:
1258             g_message ("visibility notify:\twindow: %ld  full",
1259                        xevent->xvisibility.window);
1260             break;
1261           }
1262 #endif /* G_ENABLE_DEBUG */
1263       
1264       if (window_private == NULL)
1265         {
1266           return_val = FALSE;
1267           break;
1268         }
1269       
1270       event->visibility.type = GDK_VISIBILITY_NOTIFY;
1271       event->visibility.window = window;
1272       
1273       switch (xevent->xvisibility.state)
1274         {
1275         case VisibilityFullyObscured:
1276           event->visibility.state = GDK_VISIBILITY_FULLY_OBSCURED;
1277           break;
1278           
1279         case VisibilityPartiallyObscured:
1280           event->visibility.state = GDK_VISIBILITY_PARTIAL;
1281           break;
1282           
1283         case VisibilityUnobscured:
1284           event->visibility.state = GDK_VISIBILITY_UNOBSCURED;
1285           break;
1286         }
1287       
1288       break;
1289       
1290     case CreateNotify:
1291       GDK_NOTE (EVENTS,
1292                 g_message ("create notify:\twindow: %ld  x,y: %d %d     w,h: %d %d  b-w: %d  parent: %ld         ovr: %d",
1293                            xevent->xcreatewindow.window,
1294                            xevent->xcreatewindow.x,
1295                            xevent->xcreatewindow.y,
1296                            xevent->xcreatewindow.width,
1297                            xevent->xcreatewindow.height,
1298                            xevent->xcreatewindow.border_width,
1299                            xevent->xcreatewindow.parent,
1300                            xevent->xcreatewindow.override_redirect));
1301       /* not really handled */
1302       break;
1303       
1304     case DestroyNotify:
1305       GDK_NOTE (EVENTS,
1306                 g_message ("destroy notify:\twindow: %ld",
1307                            xevent->xdestroywindow.window));
1308
1309       /* Ignore DestroyNotify from SubstructureNotifyMask */
1310       if (xevent->xdestroywindow.window == xevent->xdestroywindow.event)
1311         {
1312           event->any.type = GDK_DESTROY;
1313           event->any.window = window;
1314           
1315           return_val = window_private && !GDK_WINDOW_DESTROYED (window);
1316           
1317           if (window && GDK_WINDOW_XID (window) != screen_x11->xroot_window)
1318             gdk_window_destroy_notify (window);
1319         }
1320       else
1321         return_val = FALSE;
1322       
1323       break;
1324       
1325     case UnmapNotify:
1326       GDK_NOTE (EVENTS,
1327                 g_message ("unmap notify:\t\twindow: %ld",
1328                            xevent->xmap.window));
1329       
1330       event->any.type = GDK_UNMAP;
1331       event->any.window = window;      
1332
1333       /* If we are shown (not withdrawn) and get an unmap, it means we
1334        * were iconified in the X sense. If we are withdrawn, and get
1335        * an unmap, it means we hid the window ourselves, so we
1336        * will have already flipped the iconified bit off.
1337        */
1338       if (window)
1339         {
1340           if (GDK_WINDOW_IS_MAPPED (window))
1341             gdk_synthesize_window_state (window,
1342                                          0,
1343                                          GDK_WINDOW_STATE_ICONIFIED);
1344
1345           _gdk_xgrab_check_unmap (window, xevent->xany.serial);
1346         }
1347       
1348       break;
1349       
1350     case MapNotify:
1351       GDK_NOTE (EVENTS,
1352                 g_message ("map notify:\t\twindow: %ld",
1353                            xevent->xmap.window));
1354       
1355       event->any.type = GDK_MAP;
1356       event->any.window = window;
1357
1358       /* Unset iconified if it was set */
1359       if (window && (((GdkWindowObject*)window)->state & GDK_WINDOW_STATE_ICONIFIED))
1360         gdk_synthesize_window_state (window,
1361                                      GDK_WINDOW_STATE_ICONIFIED,
1362                                      0);
1363       
1364       break;
1365       
1366     case ReparentNotify:
1367       GDK_NOTE (EVENTS,
1368                 g_message ("reparent notify:\twindow: %ld  x,y: %d %d  parent: %ld      ovr: %d",
1369                            xevent->xreparent.window,
1370                            xevent->xreparent.x,
1371                            xevent->xreparent.y,
1372                            xevent->xreparent.parent,
1373                            xevent->xreparent.override_redirect));
1374
1375       /* Not currently handled */
1376       return_val = FALSE;
1377       break;
1378       
1379     case ConfigureNotify:
1380       GDK_NOTE (EVENTS,
1381                 g_message ("configure notify:\twindow: %ld  x,y: %d %d  w,h: %d %d  b-w: %d  above: %ld  ovr: %d%s",
1382                            xevent->xconfigure.window,
1383                            xevent->xconfigure.x,
1384                            xevent->xconfigure.y,
1385                            xevent->xconfigure.width,
1386                            xevent->xconfigure.height,
1387                            xevent->xconfigure.border_width,
1388                            xevent->xconfigure.above,
1389                            xevent->xconfigure.override_redirect,
1390                            !window
1391                            ? " (discarding)"
1392                            : GDK_WINDOW_TYPE (window) == GDK_WINDOW_CHILD
1393                            ? " (discarding child)"
1394                            : xevent->xconfigure.event != xevent->xconfigure.window
1395                            ? " (discarding substructure)"
1396                            : ""));
1397       if (window &&
1398           xevent->xconfigure.event == xevent->xconfigure.window &&
1399           !GDK_WINDOW_DESTROYED (window) &&
1400           (window_private->extension_events != 0))
1401         _gdk_input_configure_event (&xevent->xconfigure, window);
1402
1403       if (!window ||
1404           xevent->xconfigure.event != xevent->xconfigure.window ||
1405           GDK_WINDOW_TYPE (window) == GDK_WINDOW_CHILD ||
1406           GDK_WINDOW_TYPE (window) == GDK_WINDOW_ROOT)
1407         return_val = FALSE;
1408       else
1409         {
1410           event->configure.type = GDK_CONFIGURE;
1411           event->configure.window = window;
1412           event->configure.width = xevent->xconfigure.width;
1413           event->configure.height = xevent->xconfigure.height;
1414           
1415           if (!xevent->xconfigure.send_event && 
1416               !GDK_WINDOW_DESTROYED (window))
1417             {
1418               gint tx = 0;
1419               gint ty = 0;
1420               Window child_window = 0;
1421
1422               gdk_error_trap_push ();
1423               if (XTranslateCoordinates (GDK_DRAWABLE_XDISPLAY (window),
1424                                          GDK_DRAWABLE_XID (window),
1425                                          screen_x11->xroot_window,
1426                                          0, 0,
1427                                          &tx, &ty,
1428                                          &child_window))
1429                 {
1430                   if (!gdk_error_trap_pop ())
1431                     {
1432                       event->configure.x = tx;
1433                       event->configure.y = ty;
1434                     }
1435                 }
1436               else
1437                 gdk_error_trap_pop ();
1438             }
1439           else
1440             {
1441               event->configure.x = xevent->xconfigure.x;
1442               event->configure.y = xevent->xconfigure.y;
1443             }
1444           window_private->x = event->configure.x;
1445           window_private->y = event->configure.y;
1446           GDK_WINDOW_IMPL_X11 (window_private->impl)->width = xevent->xconfigure.width;
1447           GDK_WINDOW_IMPL_X11 (window_private->impl)->height = xevent->xconfigure.height;
1448           if (window_private->resize_count >= 1)
1449             {
1450               window_private->resize_count -= 1;
1451
1452               if (window_private->resize_count == 0)
1453                 _gdk_moveresize_configure_done (display, window);
1454             }
1455         }
1456       break;
1457       
1458     case PropertyNotify:
1459       GDK_NOTE (EVENTS,
1460                 g_message ("property notify:\twindow: %ld, atom(%ld): %s%s%s",
1461                            xevent->xproperty.window,
1462                            xevent->xproperty.atom,
1463                            "\"",
1464                            gdk_x11_get_xatom_name_for_display (display, xevent->xproperty.atom),
1465                            "\""));
1466
1467       if (window_private == NULL)
1468         {
1469           return_val = FALSE;
1470           break;
1471         }
1472       
1473       if (xevent->xproperty.atom == gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_STATE") ||
1474           xevent->xproperty.atom == gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_DESKTOP"))
1475         {
1476           /* If window state changed, then synthesize those events. */
1477           gdk_check_wm_state_changed (window);
1478         }
1479       
1480       if (window_private->event_mask & GDK_PROPERTY_CHANGE_MASK) 
1481         {
1482           event->property.type = GDK_PROPERTY_NOTIFY;
1483           event->property.window = window;
1484           event->property.atom = gdk_x11_xatom_to_atom_for_display (display, xevent->xproperty.atom);
1485           event->property.time = xevent->xproperty.time;
1486           event->property.state = xevent->xproperty.state;
1487         }
1488       else
1489         return_val = FALSE;
1490
1491       break;
1492       
1493     case SelectionClear:
1494       GDK_NOTE (EVENTS,
1495                 g_message ("selection clear:\twindow: %ld",
1496                            xevent->xproperty.window));
1497
1498       if (_gdk_selection_filter_clear_event (&xevent->xselectionclear))
1499         {
1500           event->selection.type = GDK_SELECTION_CLEAR;
1501           event->selection.window = window;
1502           event->selection.selection = gdk_x11_xatom_to_atom_for_display (display, xevent->xselectionclear.selection);
1503           event->selection.time = xevent->xselectionclear.time;
1504         }
1505       else
1506         return_val = FALSE;
1507           
1508       break;
1509       
1510     case SelectionRequest:
1511       GDK_NOTE (EVENTS,
1512                 g_message ("selection request:\twindow: %ld",
1513                            xevent->xproperty.window));
1514       
1515       event->selection.type = GDK_SELECTION_REQUEST;
1516       event->selection.window = window;
1517       event->selection.selection = gdk_x11_xatom_to_atom_for_display (display, xevent->xselectionrequest.selection);
1518       event->selection.target = gdk_x11_xatom_to_atom_for_display (display, xevent->xselectionrequest.target);
1519       event->selection.property = gdk_x11_xatom_to_atom_for_display (display, xevent->xselectionrequest.property);
1520       event->selection.requestor = xevent->xselectionrequest.requestor;
1521       event->selection.time = xevent->xselectionrequest.time;
1522       
1523       break;
1524       
1525     case SelectionNotify:
1526       GDK_NOTE (EVENTS,
1527                 g_message ("selection notify:\twindow: %ld",
1528                            xevent->xproperty.window));
1529       
1530       
1531       event->selection.type = GDK_SELECTION_NOTIFY;
1532       event->selection.window = window;
1533       event->selection.selection = gdk_x11_xatom_to_atom_for_display (display, xevent->xselection.selection);
1534       event->selection.target = gdk_x11_xatom_to_atom_for_display (display, xevent->xselection.target);
1535       event->selection.property = gdk_x11_xatom_to_atom_for_display (display, xevent->xselection.property);
1536       event->selection.time = xevent->xselection.time;
1537       
1538       break;
1539       
1540     case ColormapNotify:
1541       GDK_NOTE (EVENTS,
1542                 g_message ("colormap notify:\twindow: %ld",
1543                            xevent->xcolormap.window));
1544       
1545       /* Not currently handled */
1546       return_val = FALSE;
1547       break;
1548       
1549     case ClientMessage:
1550       {
1551         GList *tmp_list;
1552         GdkFilterReturn result = GDK_FILTER_CONTINUE;
1553         GdkAtom message_type = gdk_x11_xatom_to_atom_for_display (display, xevent->xclient.message_type);
1554
1555         GDK_NOTE (EVENTS,
1556                   g_message ("client message:\twindow: %ld",
1557                              xevent->xclient.window));
1558         
1559         tmp_list = display_x11->client_filters;
1560         while (tmp_list)
1561           {
1562             GdkClientFilter *filter = tmp_list->data;
1563             if (filter->type == message_type)
1564               {
1565                 result = (*filter->function) (xevent, event, filter->data);
1566                 break;
1567               }
1568             
1569             tmp_list = tmp_list->next;
1570           }
1571
1572         switch (result)
1573           {
1574           case GDK_FILTER_REMOVE:
1575             return_val = FALSE;
1576             break;
1577           case GDK_FILTER_TRANSLATE:
1578             return_val = TRUE;
1579             break;
1580           case GDK_FILTER_CONTINUE:
1581             /* Send unknown ClientMessage's on to Gtk for it to use */
1582             if (window_private == NULL)
1583               {
1584                 return_val = FALSE;
1585               }
1586             else
1587               {
1588                 event->client.type = GDK_CLIENT_EVENT;
1589                 event->client.window = window;
1590                 event->client.message_type = message_type;
1591                 event->client.data_format = xevent->xclient.format;
1592                 memcpy(&event->client.data, &xevent->xclient.data,
1593                        sizeof(event->client.data));
1594               }
1595             break;
1596           }
1597       }
1598       
1599       break;
1600       
1601     case MappingNotify:
1602       GDK_NOTE (EVENTS,
1603                 g_message ("mapping notify"));
1604       
1605       /* Let XLib know that there is a new keyboard mapping.
1606        */
1607       XRefreshKeyboardMapping (&xevent->xmapping);
1608       ++display_x11->keymap_serial;
1609       return_val = FALSE;
1610       break;
1611
1612     default:
1613 #ifdef HAVE_XKB
1614       if (xevent->type == display_x11->xkb_event_type)
1615         {
1616           XkbEvent *xkb_event = (XkbEvent *)xevent;
1617
1618           switch (xkb_event->any.xkb_type)
1619             {
1620             case XkbMapNotify:
1621               ++display_x11->keymap_serial;
1622
1623               return_val = FALSE;
1624               break;
1625               
1626             case XkbStateNotify:
1627               _gdk_keymap_state_changed (display);
1628               break;
1629             }
1630         }
1631       else
1632 #endif
1633         {
1634           /* something else - (e.g., a Xinput event) */
1635           
1636           if (window_private &&
1637               !GDK_WINDOW_DESTROYED (window_private) &&
1638               (window_private->extension_events != 0))
1639             return_val = _gdk_input_other_event(event, xevent, window);
1640           else
1641             return_val = FALSE;
1642           
1643           break;
1644         }
1645     }
1646
1647  done:
1648   if (return_val)
1649     {
1650       if (event->any.window)
1651         gdk_window_ref (event->any.window);
1652       if (((event->any.type == GDK_ENTER_NOTIFY) ||
1653            (event->any.type == GDK_LEAVE_NOTIFY)) &&
1654           (event->crossing.subwindow != NULL))
1655         gdk_window_ref (event->crossing.subwindow);
1656     }
1657   else
1658     {
1659       /* Mark this event as having no resources to be freed */
1660       event->any.window = NULL;
1661       event->any.type = GDK_NOTHING;
1662     }
1663   
1664   if (window)
1665     gdk_window_unref (window);
1666   
1667   return return_val;
1668 }
1669
1670 static GdkFilterReturn
1671 gdk_wm_protocols_filter (GdkXEvent *xev,
1672                          GdkEvent  *event,
1673                          gpointer data)
1674 {
1675   XEvent *xevent = (XEvent *)xev;
1676   GdkWindow *win = event->any.window;
1677   GdkDisplay *display = GDK_WINDOW_DISPLAY (win);
1678
1679   if ((Atom) xevent->xclient.data.l[0] == gdk_x11_get_xatom_by_name_for_display (display, "WM_DELETE_WINDOW"))
1680     {
1681   /* The delete window request specifies a window
1682    *  to delete. We don't actually destroy the
1683    *  window because "it is only a request". (The
1684    *  window might contain vital data that the
1685    *  program does not want destroyed). Instead
1686    *  the event is passed along to the program,
1687    *  which should then destroy the window.
1688    */
1689       GDK_NOTE (EVENTS,
1690                 g_message ("delete window:\t\twindow: %ld",
1691                            xevent->xclient.window));
1692       
1693       event->any.type = GDK_DELETE;
1694
1695       return GDK_FILTER_TRANSLATE;
1696     }
1697   else if ((Atom) xevent->xclient.data.l[0] == gdk_x11_get_xatom_by_name_for_display (display, "WM_TAKE_FOCUS"))
1698     {
1699       GdkWindow *win = event->any.window;
1700       Window focus_win = GDK_WINDOW_IMPL_X11(((GdkWindowObject *)win)->impl)->focus_window;
1701
1702       /* There is no way of knowing reliably whether we are viewable so we need
1703        * to trap errors so we don't cause a BadMatch.
1704        */
1705       gdk_error_trap_push ();
1706       XSetInputFocus (GDK_WINDOW_XDISPLAY (win),
1707                       focus_win,
1708                       RevertToParent,
1709                       xevent->xclient.data.l[1]);
1710       XSync (GDK_WINDOW_XDISPLAY (win), False);
1711       gdk_error_trap_pop ();
1712     }
1713   else if ((Atom) xevent->xclient.data.l[0] == gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_PING"))
1714     {
1715       XEvent xev = *xevent;
1716       
1717       xev.xclient.window = GDK_WINDOW_XROOTWIN (win);
1718       XSendEvent (GDK_WINDOW_XDISPLAY (win), 
1719                   xev.xclient.window,
1720                   False, 
1721                   SubstructureRedirectMask | SubstructureNotifyMask, &xev);
1722     }
1723
1724   return GDK_FILTER_REMOVE;
1725 }
1726
1727 void
1728 _gdk_events_queue (GdkDisplay *display)
1729 {
1730   GList *node;
1731   GdkEvent *event;
1732   XEvent xevent;
1733   Display *xdisplay = GDK_DISPLAY_XDISPLAY (display);
1734
1735   while (!_gdk_event_queue_find_first(display) && XPending (xdisplay))
1736     {
1737       XNextEvent (xdisplay, &xevent);
1738
1739       switch (xevent.type)
1740         {
1741         case KeyPress:
1742         case KeyRelease:
1743           break;
1744         default:
1745           if (XFilterEvent (&xevent, None))
1746             continue;
1747         }
1748       
1749       event = _gdk_event_new ();
1750       
1751       event->any.type = GDK_NOTHING;
1752       event->any.window = NULL;
1753       event->any.send_event = xevent.xany.send_event ? TRUE : FALSE;
1754
1755       ((GdkEventPrivate *)event)->flags |= GDK_EVENT_PENDING;
1756
1757       node = _gdk_event_queue_append (display, event);
1758
1759       if (gdk_event_translate (display, event, &xevent, FALSE))
1760         {
1761           ((GdkEventPrivate *)event)->flags &= ~GDK_EVENT_PENDING;
1762         }
1763       else
1764         {
1765           _gdk_event_queue_remove_link (display, node);
1766           g_list_free_1 (node);
1767           gdk_event_free (event);
1768         }
1769     }
1770 }
1771
1772 static gboolean  
1773 gdk_event_prepare (GSource  *source,
1774                    gint     *timeout)
1775 {
1776   GdkDisplay *display = ((GdkDisplaySource*)source)->display;
1777   gboolean retval;
1778   
1779   GDK_THREADS_ENTER ();
1780
1781   *timeout = -1;
1782   retval = (_gdk_event_queue_find_first (display) != NULL || 
1783             gdk_check_xpending (display));
1784   
1785   GDK_THREADS_LEAVE ();
1786
1787   return retval;
1788 }
1789
1790 static gboolean  
1791 gdk_event_check (GSource *source) 
1792 {
1793   GdkDisplaySource *display_source = (GdkDisplaySource*)source;
1794   gboolean retval;
1795
1796   GDK_THREADS_ENTER ();
1797
1798   if (display_source->event_poll_fd.revents & G_IO_IN)
1799     retval = (_gdk_event_queue_find_first (display_source->display) != NULL || 
1800               gdk_check_xpending (display_source->display));
1801   else
1802     retval = FALSE;
1803
1804   GDK_THREADS_LEAVE ();
1805
1806   return retval;
1807 }
1808
1809 static gboolean  
1810 gdk_event_dispatch (GSource    *source,
1811                     GSourceFunc callback,
1812                     gpointer    user_data)
1813 {
1814   GdkDisplay *display = ((GdkDisplaySource*)source)->display;
1815   GdkEvent *event;
1816  
1817   GDK_THREADS_ENTER ();
1818
1819   _gdk_events_queue (display);
1820   event = _gdk_event_unqueue (display);
1821
1822   if (event)
1823     {
1824       if (_gdk_event_func)
1825         (*_gdk_event_func) (event, _gdk_event_data);
1826       
1827       gdk_event_free (event);
1828     }
1829   
1830   GDK_THREADS_LEAVE ();
1831
1832   return TRUE;
1833 }
1834
1835 /**
1836  * gdk_event_send_client_message_for_display :
1837  * @display : the #GdkDisplay for the window where the message is to be sent.
1838  * @event : the #GdkEvent to send, which should be a #GdkEventClient.
1839  * @xid : the X window to send the X ClientMessage event to.
1840  *
1841  * Sends an X ClientMessage event to a given window.
1842  *
1843  * This could be used for communicating between different applications,
1844  * though the amount of data is limited to 20 bytes.
1845  *
1846  * Returns : non-zero on success.
1847  */
1848 gboolean
1849 gdk_event_send_client_message_for_display (GdkDisplay *display,
1850                                            GdkEvent *event,
1851                                            guint32 xid)
1852 {
1853   XEvent sev;
1854   
1855   g_return_val_if_fail(event != NULL, FALSE);
1856
1857   /* Set up our event to send, with the exception of its target window */
1858   sev.xclient.type = ClientMessage;
1859   sev.xclient.display = GDK_DISPLAY_XDISPLAY (display);
1860   sev.xclient.format = event->client.data_format;
1861   sev.xclient.window = xid;
1862   memcpy(&sev.xclient.data, &event->client.data, sizeof (sev.xclient.data));
1863   sev.xclient.message_type = gdk_x11_atom_to_xatom_for_display (display, event->client.message_type);
1864   
1865   return _gdk_send_xevent (display, xid, False, NoEventMask, &sev);
1866 }
1867
1868
1869
1870 /* Sends a ClientMessage to all toplevel client windows */
1871 gboolean
1872 gdk_event_send_client_message_to_all_recurse (GdkDisplay *display,
1873                                               XEvent     *xev, 
1874                                               guint32     xid,
1875                                               guint       level)
1876 {
1877   Atom type = None;
1878   int format;
1879   unsigned long nitems, after;
1880   unsigned char *data;
1881   Window *ret_children, ret_root, ret_parent;
1882   unsigned int ret_nchildren;
1883   gboolean send = FALSE;
1884   gboolean found = FALSE;
1885   gboolean result = FALSE;
1886   int i;
1887   
1888   gdk_error_trap_push ();
1889   
1890   if (XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), xid, 
1891                           gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_STATE"),
1892                           0, 0, False, AnyPropertyType,
1893                           &type, &format, &nitems, &after, &data) != Success)
1894     goto out;
1895   
1896   if (type)
1897     {
1898       send = TRUE;
1899       XFree (data);
1900     }
1901   else
1902     {
1903       /* OK, we're all set, now let's find some windows to send this to */
1904       if (!XQueryTree (GDK_DISPLAY_XDISPLAY (display), xid,
1905                       &ret_root, &ret_parent,
1906                       &ret_children, &ret_nchildren))   
1907         goto out;
1908
1909       for(i = 0; i < ret_nchildren; i++)
1910         if (gdk_event_send_client_message_to_all_recurse (display, xev, ret_children[i], level + 1))
1911           found = TRUE;
1912
1913       XFree (ret_children);
1914     }
1915
1916   if (send || (!found && (level == 1)))
1917     {
1918       xev->xclient.window = xid;
1919       _gdk_send_xevent (display, xid, False, NoEventMask, xev);
1920     }
1921
1922   result = send || found;
1923
1924  out:
1925   gdk_error_trap_pop ();
1926
1927   return result;
1928 }
1929
1930 /**
1931  * gdk_screen_broadcast_client_message:
1932  * @screen : the #GdkScreen where the event will be broadcasted.
1933  * @event : the #GdkEvent.
1934  *
1935  * Sends an X ClientMessage event to all toplevel windows on @screen.
1936  *
1937  * Toplevel windows are determined by checking for the WM_STATE property, 
1938  * as described in the Inter-Client Communication Conventions Manual (ICCCM).
1939  * If no windows are found with the WM_STATE property set, the message is 
1940  * sent to all children of the root window.
1941  */
1942
1943 void
1944 gdk_screen_broadcast_client_message (GdkScreen *screen, 
1945                                      GdkEvent  *event)
1946 {
1947   XEvent sev;
1948   GdkWindow *root_window;
1949
1950   g_return_if_fail (event != NULL);
1951   
1952   root_window = gdk_screen_get_root_window (screen);
1953   
1954   /* Set up our event to send, with the exception of its target window */
1955   sev.xclient.type = ClientMessage;
1956   sev.xclient.display = GDK_WINDOW_XDISPLAY (root_window);
1957   sev.xclient.format = event->client.data_format;
1958   memcpy(&sev.xclient.data, &event->client.data, sizeof (sev.xclient.data));
1959   sev.xclient.message_type = 
1960     gdk_x11_atom_to_xatom_for_display (GDK_WINDOW_DISPLAY (root_window),
1961                                        event->client.message_type);
1962
1963   gdk_event_send_client_message_to_all_recurse (gdk_screen_get_display (screen),
1964                                                 &sev, 
1965                                                 GDK_WINDOW_XID (root_window), 
1966                                                 0);
1967 }
1968
1969 /*
1970  *--------------------------------------------------------------
1971  * gdk_flush
1972  *
1973  *   Flushes the Xlib output buffer and then waits
1974  *   until all requests have been received and processed
1975  *   by the X server. The only real use for this function
1976  *   is in dealing with XShm.
1977  *
1978  * Arguments:
1979  *
1980  * Results:
1981  *
1982  * Side effects:
1983  *
1984  *--------------------------------------------------------------
1985  */
1986
1987 void
1988 gdk_flush (void)
1989 {
1990   GSList *tmp_list = _gdk_displays;
1991   
1992   while (tmp_list)
1993     {
1994       XSync (GDK_DISPLAY_XDISPLAY (tmp_list->data), False);
1995       tmp_list = tmp_list->next;
1996     }
1997 }
1998
1999 static Bool
2000 timestamp_predicate (Display *display,
2001                      XEvent  *xevent,
2002                      XPointer arg)
2003 {
2004   Window xwindow = GPOINTER_TO_UINT (arg);
2005   GdkDisplay *gdk_display = gdk_x11_lookup_xdisplay (display);
2006
2007   if (xevent->type == PropertyNotify &&
2008       xevent->xproperty.window == xwindow &&
2009       xevent->xproperty.atom == gdk_x11_get_xatom_by_name_for_display 
2010       (gdk_display, "GDK_TIMESTAMP_PROP"))
2011     return True;
2012
2013   return False;
2014 }
2015
2016 /**
2017  * gdk_x11_get_server_time:
2018  * @window: a #GdkWindow, used for communication with the server.
2019  *          The window must have GDK_PROPERTY_CHANGE_MASK in its
2020  *          events mask or a hang will result.
2021  * 
2022  * Routine to get the current X server time stamp. 
2023  * 
2024  * Return value: the time stamp.
2025  **/
2026 guint32
2027 gdk_x11_get_server_time (GdkWindow *window)
2028 {
2029   Display *xdisplay;
2030   Window   xwindow;
2031   guchar c = 'a';
2032   XEvent xevent;
2033   Atom timestamp_prop_atom;
2034
2035   g_return_val_if_fail (GDK_IS_WINDOW (window), 0);
2036   g_return_val_if_fail (!GDK_WINDOW_DESTROYED (window), 0);
2037
2038   xdisplay = GDK_WINDOW_XDISPLAY (window);
2039   xwindow = GDK_WINDOW_XWINDOW (window);
2040   timestamp_prop_atom = 
2041     gdk_x11_get_xatom_by_name_for_display (GDK_WINDOW_DISPLAY (window),
2042                                            "GDK_TIMESTAMP_PROP");
2043   
2044   XChangeProperty (xdisplay, xwindow, timestamp_prop_atom,
2045                    timestamp_prop_atom,
2046                    8, PropModeReplace, &c, 1);
2047
2048   XIfEvent (xdisplay, &xevent,
2049             timestamp_predicate, GUINT_TO_POINTER(xwindow));
2050
2051   return xevent.xproperty.time;
2052 }
2053
2054 typedef struct _NetWmSupportedAtoms NetWmSupportedAtoms;
2055
2056 struct _NetWmSupportedAtoms
2057 {
2058   Atom *atoms;
2059   gulong n_atoms;
2060 };
2061
2062 /**
2063  * gdk_x11_screen_supports_net_wm_hint:
2064  * @screen : the relevant #GdkScreen.
2065  * @property: a property atom.
2066  * 
2067  * This function is specific to the X11 backend of GDK, and indicates
2068  * whether the window manager supports a certain hint from the
2069  * Extended Window Manager Hints Specification. You can find this
2070  * specification on http://www.freedesktop.org.
2071  *
2072  * When using this function, keep in mind that the window manager
2073  * can change over time; so you shouldn't use this function in
2074  * a way that impacts persistent application state. A common bug
2075  * is that your application can start up before the window manager
2076  * does when the user logs in, and before the window manager starts
2077  * gdk_x11_screen_supports_net_wm_hint() will return %FALSE for every property.
2078  * 
2079  * Return value: %TRUE if the window manager supports @property
2080  **/
2081 gboolean
2082 gdk_x11_screen_supports_net_wm_hint (GdkScreen *screen,
2083                                      GdkAtom    property)
2084 {
2085   Atom type;
2086   gint format;
2087   gulong nitems;
2088   gulong bytes_after;
2089   Window *xwindow;
2090   gulong i;
2091   GdkScreenX11 *screen_x11;
2092   NetWmSupportedAtoms *supported_atoms;
2093   GdkDisplay *display;
2094
2095   g_return_val_if_fail (GDK_IS_SCREEN (screen), FALSE);
2096   
2097   screen_x11 = GDK_SCREEN_X11 (screen);
2098   display = screen_x11->display;
2099
2100   supported_atoms = g_object_get_data (G_OBJECT (screen), "gdk-net-wm-supported-atoms");
2101   if (!supported_atoms)
2102     {
2103       supported_atoms = g_new0 (NetWmSupportedAtoms, 1);
2104       g_object_set_data (G_OBJECT (screen), "gdk-net-wm-supported-atoms", supported_atoms);
2105     }
2106
2107   if (screen_x11->wmspec_check_window != None)
2108     {
2109       if (supported_atoms->atoms == NULL)
2110         return FALSE;
2111       
2112       i = 0;
2113       while (i < supported_atoms->n_atoms)
2114         {
2115           if (supported_atoms->atoms[i] == gdk_x11_atom_to_xatom_for_display (display, property))
2116             return TRUE;
2117           
2118           ++i;
2119         }
2120       
2121       return FALSE;
2122     }
2123
2124   if (supported_atoms->atoms)
2125     XFree (supported_atoms->atoms);
2126
2127   supported_atoms->atoms = NULL;
2128   supported_atoms->n_atoms = 0;
2129   
2130   /* This function is very slow on every call if you are not running a
2131    * spec-supporting WM. For now not optimized, because it isn't in
2132    * any critical code paths, but if you used it somewhere that had to
2133    * be fast you want to avoid "GTK is slow with old WMs" complaints.
2134    * Probably at that point the function should be changed to query
2135    * _NET_SUPPORTING_WM_CHECK only once every 10 seconds or something.
2136    */
2137   
2138   XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), screen_x11->xroot_window,
2139                       gdk_x11_get_xatom_by_name_for_display (display, "_NET_SUPPORTING_WM_CHECK"),
2140                       0, G_MAXLONG, False, XA_WINDOW, &type, &format, 
2141                       &nitems, &bytes_after, (guchar **) & xwindow);
2142   
2143   if (type != XA_WINDOW)
2144     return FALSE;
2145
2146   gdk_error_trap_push ();
2147
2148   /* Find out if this WM goes away, so we can reset everything. */
2149   XSelectInput (screen_x11->xdisplay, *xwindow, StructureNotifyMask);
2150
2151   gdk_display_sync (screen_x11->display);
2152   
2153   if (gdk_error_trap_pop ())
2154     {
2155       XFree (xwindow);
2156       return FALSE;
2157     }
2158   
2159   XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), screen_x11->xroot_window,
2160                       gdk_x11_get_xatom_by_name_for_display (display, "_NET_SUPPORTED"),
2161                       0, G_MAXLONG, False, XA_ATOM, &type, &format, 
2162                       &supported_atoms->n_atoms, &bytes_after,
2163                       (guchar **)&supported_atoms->atoms);
2164   
2165   if (type != XA_ATOM)
2166     return FALSE;
2167   
2168   screen_x11->wmspec_check_window = *xwindow;
2169   XFree (xwindow);
2170   
2171   /* since wmspec_check_window != None this isn't infinite. ;-) */
2172   return gdk_x11_screen_supports_net_wm_hint (screen, property);
2173 }
2174
2175 /**
2176  * gdk_net_wm_supports:
2177  * @property: a property atom.
2178  * 
2179  * This function is specific to the X11 backend of GDK, and indicates
2180  * whether the window manager for the default screen supports a certain
2181  * hint from the Extended Window Manager Hints Specification. See
2182  * gdk_x11_screen_supports_net_wm_hint() for complete details.
2183  * 
2184  * Return value: %TRUE if the window manager supports @property
2185  **/
2186 gboolean
2187 gdk_net_wm_supports (GdkAtom property)
2188 {
2189   return gdk_x11_screen_supports_net_wm_hint (gdk_screen_get_default (), property);
2190 }
2191
2192 static struct
2193 {
2194   const char *xsettings_name;
2195   const char *gdk_name;
2196 } settings_map[] = {
2197   { "Net/DoubleClickTime", "gtk-double-click-time" },
2198   { "Net/DndDragThreshold", "gtk-dnd-drag-threshold" },
2199   { "Gtk/CanChangeAccels", "gtk-can-change-accels" },
2200   { "Gtk/ColorPalette", "gtk-color-palette" },
2201   { "Gtk/FontName", "gtk-font-name" },
2202   { "Gtk/KeyThemeName", "gtk-key-theme-name" },
2203   { "Gtk/ToolbarStyle", "gtk-toolbar-style" },
2204   { "Gtk/ToolbarIconSize", "gtk-toolbar-icon-size" },
2205   { "Net/CursorBlink", "gtk-cursor-blink" },
2206   { "Net/CursorBlinkTime", "gtk-cursor-blink-time" },
2207   { "Net/ThemeName", "gtk-theme-name" }
2208 };
2209
2210 static void
2211 gdk_xsettings_notify_cb (const char       *name,
2212                          XSettingsAction   action,
2213                          XSettingsSetting *setting,
2214                          void             *data)
2215 {
2216   GdkEvent new_event;
2217   GdkScreen *screen = data;
2218   int i;
2219
2220   new_event.type = GDK_SETTING;
2221   new_event.setting.window = gdk_screen_get_root_window (screen);
2222   new_event.setting.send_event = FALSE;
2223   new_event.setting.name = NULL;
2224
2225   for (i = 0; i < G_N_ELEMENTS (settings_map) ; i++)
2226     if (strcmp (settings_map[i].xsettings_name, name) == 0)
2227       {
2228         new_event.setting.name = (char *)settings_map[i].gdk_name;
2229         break;
2230       }
2231
2232   if (!new_event.setting.name)
2233     return;
2234   
2235   switch (action)
2236     {
2237     case XSETTINGS_ACTION_NEW:
2238       new_event.setting.action = GDK_SETTING_ACTION_NEW;
2239       break;
2240     case XSETTINGS_ACTION_CHANGED:
2241       new_event.setting.action = GDK_SETTING_ACTION_CHANGED;
2242       break;
2243     case XSETTINGS_ACTION_DELETED:
2244       new_event.setting.action = GDK_SETTING_ACTION_DELETED;
2245       break;
2246     }
2247
2248   gdk_event_put (&new_event);
2249 }
2250
2251 static gboolean
2252 check_transform (const gchar *xsettings_name,
2253                  GType        src_type,
2254                  GType        dest_type)
2255 {
2256   if (!g_value_type_transformable (src_type, dest_type))
2257     {
2258       g_warning ("Cannot tranform xsetting %s of type %s to type %s\n",
2259                  xsettings_name,
2260                  g_type_name (src_type),
2261                  g_type_name (dest_type));
2262       return FALSE;
2263     }
2264   else
2265     return TRUE;
2266 }
2267
2268 /**
2269  * gdk_screen_get_setting:
2270  * @screen: the #GdkScreen where the setting is located
2271  * @name: the name of the setting
2272  * @value: location to store the value of the setting
2273  *
2274  * Retrieves a desktop-wide setting such as double-click time
2275  * for the #GdkScreen @screen. 
2276  *
2277  * FIXME needs a list of valid settings here, or a link to 
2278  * more information.
2279  * 
2280  * Returns : %TRUE if the setting existed and a value was stored
2281  *   in @value, %FALSE otherwise.
2282  **/
2283 gboolean
2284 gdk_screen_get_setting (GdkScreen   *screen,
2285                         const gchar *name,
2286                         GValue      *value)
2287 {
2288
2289   const char *xsettings_name = NULL;
2290   XSettingsResult result;
2291   XSettingsSetting *setting;
2292   GdkScreenX11 *screen_x11;
2293   gboolean success = FALSE;
2294   gint i;
2295   GValue tmp_val = { 0, };
2296   
2297   g_return_val_if_fail (GDK_IS_SCREEN (screen), FALSE);
2298   
2299   screen_x11 = GDK_SCREEN_X11 (screen);
2300
2301   for (i = 0; i < G_N_ELEMENTS (settings_map) ; i++)
2302     if (strcmp (settings_map[i].gdk_name, name) == 0)
2303       {
2304         xsettings_name = settings_map[i].xsettings_name;
2305         break;
2306       }
2307
2308   if (!xsettings_name)
2309     return FALSE;
2310
2311   result = xsettings_client_get_setting (screen_x11->xsettings_client, 
2312                                          xsettings_name, &setting);
2313   if (result != XSETTINGS_SUCCESS)
2314     return FALSE;
2315
2316   switch (setting->type)
2317     {
2318     case XSETTINGS_TYPE_INT:
2319       if (check_transform (xsettings_name, G_TYPE_INT, G_VALUE_TYPE (value)))
2320         {
2321           g_value_init (&tmp_val, G_TYPE_INT);
2322           g_value_set_int (&tmp_val, setting->data.v_int);
2323           g_value_transform (&tmp_val, value);
2324
2325           success = TRUE;
2326         }
2327       break;
2328     case XSETTINGS_TYPE_STRING:
2329       if (check_transform (xsettings_name, G_TYPE_STRING, G_VALUE_TYPE (value)))
2330         {
2331           g_value_init (&tmp_val, G_TYPE_STRING);
2332           g_value_set_string (&tmp_val, setting->data.v_string);
2333           g_value_transform (&tmp_val, value);
2334
2335           success = TRUE;
2336         }
2337       break;
2338     case XSETTINGS_TYPE_COLOR:
2339       if (!check_transform (xsettings_name, GDK_TYPE_COLOR, G_VALUE_TYPE (value)))
2340         {
2341           GdkColor color;
2342           
2343           g_value_init (&tmp_val, GDK_TYPE_COLOR);
2344
2345           color.pixel = 0;
2346           color.red = setting->data.v_color.red;
2347           color.green = setting->data.v_color.green;
2348           color.blue = setting->data.v_color.blue;
2349           
2350           g_value_set_boxed (&tmp_val, &color);
2351           
2352           g_value_transform (&tmp_val, value);
2353           
2354           success = TRUE;
2355         }
2356       break;
2357     }
2358   
2359   g_value_unset (&tmp_val);
2360
2361   xsettings_setting_free (setting);
2362
2363   return success;
2364 }
2365
2366 static GdkFilterReturn 
2367 gdk_xsettings_client_event_filter (GdkXEvent *xevent,
2368                                    GdkEvent  *event,
2369                                    gpointer   data)
2370 {
2371   GdkScreenX11 *screen = data;
2372   
2373   if (xsettings_client_process_event (screen->xsettings_client, (XEvent *)xevent))
2374     return GDK_FILTER_REMOVE;
2375   else
2376     return GDK_FILTER_CONTINUE;
2377 }
2378
2379 static void 
2380 gdk_xsettings_watch_cb (Window   window,
2381                         Bool     is_start,
2382                         long     mask,
2383                         void    *cb_data)
2384 {
2385   GdkWindow *gdkwin;
2386   GdkScreen *screen = cb_data;
2387   GdkScreenX11 *screen_x11 = GDK_SCREEN_X11 (screen);
2388
2389   gdkwin = gdk_window_lookup_for_display (gdk_screen_get_display (screen), window);
2390
2391   if (is_start)
2392     {
2393       if (!gdkwin)
2394         gdkwin = gdk_window_foreign_new_for_display (gdk_screen_get_display (screen), window);
2395       else
2396         g_object_ref (gdkwin);
2397       
2398       gdk_window_add_filter (gdkwin, gdk_xsettings_client_event_filter, screen);
2399     }
2400   else
2401     {
2402       g_assert (gdkwin);
2403       gdk_window_remove_filter (gdkwin, gdk_xsettings_client_event_filter, screen);
2404       g_object_unref (gdkwin);
2405     }
2406 }