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