]> Pileus Git - ~andy/gtk/blob - gdk/x11/gdkevents-x11.c
gdk/gdkevents.c gdk/gdkinternals.h gdk/x11/gdkevents-x11.c Move the
[~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               window_impl->has_focus = TRUE;
1076               break;
1077             case NotifyPointer:
1078               window_impl->has_pointer_focus = TRUE;
1079               break;
1080             case NotifyInferior:
1081             case NotifyPointerRoot:
1082             case NotifyDetailNone:
1083               break;
1084             }
1085
1086           if (HAS_FOCUS (window_impl) != had_focus)
1087             generate_focus_event (window, TRUE);
1088         }
1089       break;
1090     case FocusOut:
1091       GDK_NOTE (EVENTS,
1092                 g_message ("focus out:\t\twindow: %ld", xevent->xfocus.window));
1093
1094       if (window && GDK_WINDOW_TYPE (window) != GDK_WINDOW_CHILD)
1095         {
1096           gboolean had_focus = HAS_FOCUS (window_impl);
1097             
1098           switch (xevent->xfocus.detail)
1099             {
1100             case NotifyAncestor:
1101             case NotifyNonlinear:
1102             case NotifyVirtual:
1103             case NotifyNonlinearVirtual:
1104               window_impl->has_focus = FALSE;
1105               break;
1106             case NotifyPointer:
1107               window_impl->has_pointer_focus = FALSE;
1108             break;
1109             case NotifyInferior:
1110             case NotifyPointerRoot:
1111             case NotifyDetailNone:
1112               break;
1113             }
1114
1115           if (HAS_FOCUS (window_impl) != had_focus)
1116             generate_focus_event (window, FALSE);
1117         }
1118       break;
1119
1120 #if 0      
1121           /* gdk_keyboard_grab() causes following events. These events confuse
1122            * the XIM focus, so ignore them.
1123            */
1124           if (xevent->xfocus.mode == NotifyGrab ||
1125               xevent->xfocus.mode == NotifyUngrab)
1126             break;
1127 #endif
1128
1129     case KeymapNotify:
1130       GDK_NOTE (EVENTS,
1131                 g_message ("keymap notify"));
1132
1133       /* Not currently handled */
1134       return_val = FALSE;
1135       break;
1136       
1137     case Expose:
1138       GDK_NOTE (EVENTS,
1139                 g_message ("expose:\t\twindow: %ld  %d  x,y: %d %d  w,h: %d %d%s",
1140                            xevent->xexpose.window, xevent->xexpose.count,
1141                            xevent->xexpose.x, xevent->xexpose.y,
1142                            xevent->xexpose.width, xevent->xexpose.height,
1143                            event->any.send_event ? " (send)" : ""));
1144       
1145       if (window_private == NULL)
1146         {
1147           return_val = FALSE;
1148           break;
1149         }
1150       
1151       {
1152         GdkRectangle expose_rect;
1153
1154         expose_rect.x = xevent->xexpose.x + xoffset;
1155         expose_rect.y = xevent->xexpose.y + yoffset;
1156         expose_rect.width = xevent->xexpose.width;
1157         expose_rect.height = xevent->xexpose.height;
1158
1159         if (return_exposes)
1160           {
1161             event->expose.type = GDK_EXPOSE;
1162             event->expose.area = expose_rect;
1163             event->expose.region = gdk_region_rectangle (&expose_rect);
1164             event->expose.window = window;
1165             event->expose.count = xevent->xexpose.count;
1166
1167             return_val = TRUE;
1168           }
1169         else
1170           {
1171             _gdk_window_process_expose (window, xevent->xexpose.serial, &expose_rect);
1172             return_val = FALSE;
1173           }
1174         
1175         return_val = FALSE;
1176       }
1177         
1178       break;
1179       
1180     case GraphicsExpose:
1181       {
1182         GdkRectangle expose_rect;
1183
1184         GDK_NOTE (EVENTS,
1185                   g_message ("graphics expose:\tdrawable: %ld",
1186                              xevent->xgraphicsexpose.drawable));
1187  
1188         if (window_private == NULL)
1189           {
1190             return_val = FALSE;
1191             break;
1192           }
1193         
1194         expose_rect.x = xevent->xgraphicsexpose.x + xoffset;
1195         expose_rect.y = xevent->xgraphicsexpose.y + yoffset;
1196         expose_rect.width = xevent->xgraphicsexpose.width;
1197         expose_rect.height = xevent->xgraphicsexpose.height;
1198             
1199         if (return_exposes)
1200           {
1201             event->expose.type = GDK_EXPOSE;
1202             event->expose.area = expose_rect;
1203             event->expose.region = gdk_region_rectangle (&expose_rect);
1204             event->expose.window = window;
1205             event->expose.count = xevent->xgraphicsexpose.count;
1206
1207             return_val = TRUE;
1208           }
1209         else
1210           {
1211             _gdk_window_process_expose (window, xevent->xgraphicsexpose.serial, &expose_rect);
1212             
1213             return_val = FALSE;
1214           }
1215         
1216       }
1217       break;
1218       
1219     case NoExpose:
1220       GDK_NOTE (EVENTS,
1221                 g_message ("no expose:\t\tdrawable: %ld",
1222                            xevent->xnoexpose.drawable));
1223       
1224       event->no_expose.type = GDK_NO_EXPOSE;
1225       event->no_expose.window = window;
1226       
1227       break;
1228       
1229     case VisibilityNotify:
1230 #ifdef G_ENABLE_DEBUG
1231       if (_gdk_debug_flags & GDK_DEBUG_EVENTS)
1232         switch (xevent->xvisibility.state)
1233           {
1234           case VisibilityFullyObscured:
1235             g_message ("visibility notify:\twindow: %ld  none",
1236                        xevent->xvisibility.window);
1237             break;
1238           case VisibilityPartiallyObscured:
1239             g_message ("visibility notify:\twindow: %ld  partial",
1240                        xevent->xvisibility.window);
1241             break;
1242           case VisibilityUnobscured:
1243             g_message ("visibility notify:\twindow: %ld  full",
1244                        xevent->xvisibility.window);
1245             break;
1246           }
1247 #endif /* G_ENABLE_DEBUG */
1248       
1249       if (window_private == NULL)
1250         {
1251           return_val = FALSE;
1252           break;
1253         }
1254       
1255       event->visibility.type = GDK_VISIBILITY_NOTIFY;
1256       event->visibility.window = window;
1257       
1258       switch (xevent->xvisibility.state)
1259         {
1260         case VisibilityFullyObscured:
1261           event->visibility.state = GDK_VISIBILITY_FULLY_OBSCURED;
1262           break;
1263           
1264         case VisibilityPartiallyObscured:
1265           event->visibility.state = GDK_VISIBILITY_PARTIAL;
1266           break;
1267           
1268         case VisibilityUnobscured:
1269           event->visibility.state = GDK_VISIBILITY_UNOBSCURED;
1270           break;
1271         }
1272       
1273       break;
1274       
1275     case CreateNotify:
1276       GDK_NOTE (EVENTS,
1277                 g_message ("create notify:\twindow: %ld  x,y: %d %d     w,h: %d %d  b-w: %d  parent: %ld         ovr: %d",
1278                            xevent->xcreatewindow.window,
1279                            xevent->xcreatewindow.x,
1280                            xevent->xcreatewindow.y,
1281                            xevent->xcreatewindow.width,
1282                            xevent->xcreatewindow.height,
1283                            xevent->xcreatewindow.border_width,
1284                            xevent->xcreatewindow.parent,
1285                            xevent->xcreatewindow.override_redirect));
1286       /* not really handled */
1287       break;
1288       
1289     case DestroyNotify:
1290       GDK_NOTE (EVENTS,
1291                 g_message ("destroy notify:\twindow: %ld",
1292                            xevent->xdestroywindow.window));
1293
1294       /* Ignore DestroyNotify from SubstructureNotifyMask */
1295       if (xevent->xdestroywindow.window == xevent->xdestroywindow.event)
1296         {
1297           event->any.type = GDK_DESTROY;
1298           event->any.window = window;
1299           
1300           return_val = window_private && !GDK_WINDOW_DESTROYED (window);
1301           
1302           if (window && GDK_WINDOW_XID (window) != screen_x11->xroot_window)
1303             gdk_window_destroy_notify (window);
1304         }
1305       else
1306         return_val = FALSE;
1307       
1308       break;
1309       
1310     case UnmapNotify:
1311       GDK_NOTE (EVENTS,
1312                 g_message ("unmap notify:\t\twindow: %ld",
1313                            xevent->xmap.window));
1314       
1315       event->any.type = GDK_UNMAP;
1316       event->any.window = window;      
1317
1318       /* If we are shown (not withdrawn) and get an unmap, it means we
1319        * were iconified in the X sense. If we are withdrawn, and get
1320        * an unmap, it means we hid the window ourselves, so we
1321        * will have already flipped the iconified bit off.
1322        */
1323       if (window)
1324         {
1325           if (GDK_WINDOW_IS_MAPPED (window))
1326             gdk_synthesize_window_state (window,
1327                                          0,
1328                                          GDK_WINDOW_STATE_ICONIFIED);
1329
1330           _gdk_xgrab_check_unmap (window, xevent->xany.serial);
1331         }
1332       
1333       break;
1334       
1335     case MapNotify:
1336       GDK_NOTE (EVENTS,
1337                 g_message ("map notify:\t\twindow: %ld",
1338                            xevent->xmap.window));
1339       
1340       event->any.type = GDK_MAP;
1341       event->any.window = window;
1342
1343       /* Unset iconified if it was set */
1344       if (window && (((GdkWindowObject*)window)->state & GDK_WINDOW_STATE_ICONIFIED))
1345         gdk_synthesize_window_state (window,
1346                                      GDK_WINDOW_STATE_ICONIFIED,
1347                                      0);
1348       
1349       break;
1350       
1351     case ReparentNotify:
1352       GDK_NOTE (EVENTS,
1353                 g_message ("reparent notify:\twindow: %ld  x,y: %d %d  parent: %ld      ovr: %d",
1354                            xevent->xreparent.window,
1355                            xevent->xreparent.x,
1356                            xevent->xreparent.y,
1357                            xevent->xreparent.parent,
1358                            xevent->xreparent.override_redirect));
1359
1360       /* Not currently handled */
1361       return_val = FALSE;
1362       break;
1363       
1364     case ConfigureNotify:
1365       GDK_NOTE (EVENTS,
1366                 g_message ("configure notify:\twindow: %ld  x,y: %d %d  w,h: %d %d  b-w: %d  above: %ld  ovr: %d%s",
1367                            xevent->xconfigure.window,
1368                            xevent->xconfigure.x,
1369                            xevent->xconfigure.y,
1370                            xevent->xconfigure.width,
1371                            xevent->xconfigure.height,
1372                            xevent->xconfigure.border_width,
1373                            xevent->xconfigure.above,
1374                            xevent->xconfigure.override_redirect,
1375                            !window
1376                            ? " (discarding)"
1377                            : GDK_WINDOW_TYPE (window) == GDK_WINDOW_CHILD
1378                            ? " (discarding child)"
1379                            : xevent->xconfigure.event != xevent->xconfigure.window
1380                            ? " (discarding substructure)"
1381                            : ""));
1382       if (window &&
1383           xevent->xconfigure.event == xevent->xconfigure.window &&
1384           !GDK_WINDOW_DESTROYED (window) &&
1385           (window_private->extension_events != 0))
1386         _gdk_input_configure_event (&xevent->xconfigure, window);
1387
1388       if (!window ||
1389           xevent->xconfigure.event != xevent->xconfigure.window ||
1390           GDK_WINDOW_TYPE (window) == GDK_WINDOW_CHILD ||
1391           GDK_WINDOW_TYPE (window) == GDK_WINDOW_ROOT)
1392         return_val = FALSE;
1393       else
1394         {
1395           event->configure.type = GDK_CONFIGURE;
1396           event->configure.window = window;
1397           event->configure.width = xevent->xconfigure.width;
1398           event->configure.height = xevent->xconfigure.height;
1399           
1400           if (!xevent->xconfigure.send_event && 
1401               !GDK_WINDOW_DESTROYED (window))
1402             {
1403               gint tx = 0;
1404               gint ty = 0;
1405               Window child_window = 0;
1406
1407               gdk_error_trap_push ();
1408               if (XTranslateCoordinates (GDK_DRAWABLE_XDISPLAY (window),
1409                                          GDK_DRAWABLE_XID (window),
1410                                          screen_x11->xroot_window,
1411                                          0, 0,
1412                                          &tx, &ty,
1413                                          &child_window))
1414                 {
1415                   if (!gdk_error_trap_pop ())
1416                     {
1417                       event->configure.x = tx;
1418                       event->configure.y = ty;
1419                     }
1420                 }
1421               else
1422                 gdk_error_trap_pop ();
1423             }
1424           else
1425             {
1426               event->configure.x = xevent->xconfigure.x;
1427               event->configure.y = xevent->xconfigure.y;
1428             }
1429           window_private->x = event->configure.x;
1430           window_private->y = event->configure.y;
1431           GDK_WINDOW_IMPL_X11 (window_private->impl)->width = xevent->xconfigure.width;
1432           GDK_WINDOW_IMPL_X11 (window_private->impl)->height = xevent->xconfigure.height;
1433           if (window_private->resize_count >= 1)
1434             {
1435               window_private->resize_count -= 1;
1436
1437               if (window_private->resize_count == 0)
1438                 _gdk_moveresize_configure_done (display, window);
1439             }
1440         }
1441       break;
1442       
1443     case PropertyNotify:
1444       GDK_NOTE (EVENTS,
1445                 g_message ("property notify:\twindow: %ld, atom(%ld): %s%s%s",
1446                            xevent->xproperty.window,
1447                            xevent->xproperty.atom,
1448                            "\"",
1449                            gdk_x11_get_xatom_name_for_display (display, xevent->xproperty.atom),
1450                            "\""));
1451
1452       if (window_private == NULL)
1453         {
1454           return_val = FALSE;
1455           break;
1456         }
1457       
1458       if (xevent->xproperty.atom == gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_STATE") ||
1459           xevent->xproperty.atom == gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_DESKTOP"))
1460         {
1461           /* If window state changed, then synthesize those events. */
1462           gdk_check_wm_state_changed (window);
1463         }
1464       
1465       if (window_private->event_mask & GDK_PROPERTY_CHANGE_MASK) 
1466         {
1467           event->property.type = GDK_PROPERTY_NOTIFY;
1468           event->property.window = window;
1469           event->property.atom = gdk_x11_xatom_to_atom_for_display (display, xevent->xproperty.atom);
1470           event->property.time = xevent->xproperty.time;
1471           event->property.state = xevent->xproperty.state;
1472         }
1473       else
1474         return_val = FALSE;
1475
1476       break;
1477       
1478     case SelectionClear:
1479       GDK_NOTE (EVENTS,
1480                 g_message ("selection clear:\twindow: %ld",
1481                            xevent->xproperty.window));
1482
1483       if (_gdk_selection_filter_clear_event (&xevent->xselectionclear))
1484         {
1485           event->selection.type = GDK_SELECTION_CLEAR;
1486           event->selection.window = window;
1487           event->selection.selection = gdk_x11_xatom_to_atom_for_display (display, xevent->xselectionclear.selection);
1488           event->selection.time = xevent->xselectionclear.time;
1489         }
1490       else
1491         return_val = FALSE;
1492           
1493       break;
1494       
1495     case SelectionRequest:
1496       GDK_NOTE (EVENTS,
1497                 g_message ("selection request:\twindow: %ld",
1498                            xevent->xproperty.window));
1499       
1500       event->selection.type = GDK_SELECTION_REQUEST;
1501       event->selection.window = window;
1502       event->selection.selection = gdk_x11_xatom_to_atom_for_display (display, xevent->xselectionrequest.selection);
1503       event->selection.target = gdk_x11_xatom_to_atom_for_display (display, xevent->xselectionrequest.target);
1504       event->selection.property = gdk_x11_xatom_to_atom_for_display (display, xevent->xselectionrequest.property);
1505       event->selection.requestor = xevent->xselectionrequest.requestor;
1506       event->selection.time = xevent->xselectionrequest.time;
1507       
1508       break;
1509       
1510     case SelectionNotify:
1511       GDK_NOTE (EVENTS,
1512                 g_message ("selection notify:\twindow: %ld",
1513                            xevent->xproperty.window));
1514       
1515       
1516       event->selection.type = GDK_SELECTION_NOTIFY;
1517       event->selection.window = window;
1518       event->selection.selection = gdk_x11_xatom_to_atom_for_display (display, xevent->xselection.selection);
1519       event->selection.target = gdk_x11_xatom_to_atom_for_display (display, xevent->xselection.target);
1520       event->selection.property = gdk_x11_xatom_to_atom_for_display (display, xevent->xselection.property);
1521       event->selection.time = xevent->xselection.time;
1522       
1523       break;
1524       
1525     case ColormapNotify:
1526       GDK_NOTE (EVENTS,
1527                 g_message ("colormap notify:\twindow: %ld",
1528                            xevent->xcolormap.window));
1529       
1530       /* Not currently handled */
1531       return_val = FALSE;
1532       break;
1533       
1534     case ClientMessage:
1535       {
1536         GList *tmp_list;
1537         GdkFilterReturn result = GDK_FILTER_CONTINUE;
1538         GdkAtom message_type = gdk_x11_xatom_to_atom_for_display (display, xevent->xclient.message_type);
1539
1540         GDK_NOTE (EVENTS,
1541                   g_message ("client message:\twindow: %ld",
1542                              xevent->xclient.window));
1543         
1544         tmp_list = display_x11->client_filters;
1545         while (tmp_list)
1546           {
1547             GdkClientFilter *filter = tmp_list->data;
1548             if (filter->type == message_type)
1549               {
1550                 result = (*filter->function) (xevent, event, filter->data);
1551                 break;
1552               }
1553             
1554             tmp_list = tmp_list->next;
1555           }
1556
1557         switch (result)
1558           {
1559           case GDK_FILTER_REMOVE:
1560             return_val = FALSE;
1561             break;
1562           case GDK_FILTER_TRANSLATE:
1563             return_val = TRUE;
1564             break;
1565           case GDK_FILTER_CONTINUE:
1566             /* Send unknown ClientMessage's on to Gtk for it to use */
1567             if (window_private == NULL)
1568               {
1569                 return_val = FALSE;
1570               }
1571             else
1572               {
1573                 event->client.type = GDK_CLIENT_EVENT;
1574                 event->client.window = window;
1575                 event->client.message_type = message_type;
1576                 event->client.data_format = xevent->xclient.format;
1577                 memcpy(&event->client.data, &xevent->xclient.data,
1578                        sizeof(event->client.data));
1579               }
1580             break;
1581           }
1582       }
1583       
1584       break;
1585       
1586     case MappingNotify:
1587       GDK_NOTE (EVENTS,
1588                 g_message ("mapping notify"));
1589       
1590       /* Let XLib know that there is a new keyboard mapping.
1591        */
1592       XRefreshKeyboardMapping (&xevent->xmapping);
1593       ++display_x11->keymap_serial;
1594       return_val = FALSE;
1595       break;
1596
1597     default:
1598 #ifdef HAVE_XKB
1599       if (xevent->type == display_x11->xkb_event_type)
1600         {
1601           XkbEvent *xkb_event = (XkbEvent *)xevent;
1602
1603           switch (xkb_event->any.xkb_type)
1604             {
1605             case XkbMapNotify:
1606               ++display_x11->keymap_serial;
1607
1608               return_val = FALSE;
1609               break;
1610               
1611             case XkbStateNotify:
1612               _gdk_keymap_state_changed (display);
1613               break;
1614             }
1615         }
1616       else
1617 #endif
1618         {
1619           /* something else - (e.g., a Xinput event) */
1620           
1621           if (window_private &&
1622               !GDK_WINDOW_DESTROYED (window_private) &&
1623               (window_private->extension_events != 0))
1624             return_val = _gdk_input_other_event(event, xevent, window);
1625           else
1626             return_val = FALSE;
1627           
1628           break;
1629         }
1630     }
1631
1632  done:
1633   if (return_val)
1634     {
1635       if (event->any.window)
1636         gdk_window_ref (event->any.window);
1637       if (((event->any.type == GDK_ENTER_NOTIFY) ||
1638            (event->any.type == GDK_LEAVE_NOTIFY)) &&
1639           (event->crossing.subwindow != NULL))
1640         gdk_window_ref (event->crossing.subwindow);
1641     }
1642   else
1643     {
1644       /* Mark this event as having no resources to be freed */
1645       event->any.window = NULL;
1646       event->any.type = GDK_NOTHING;
1647     }
1648   
1649   if (window)
1650     gdk_window_unref (window);
1651   
1652   return return_val;
1653 }
1654
1655 static GdkFilterReturn
1656 gdk_wm_protocols_filter (GdkXEvent *xev,
1657                          GdkEvent  *event,
1658                          gpointer data)
1659 {
1660   XEvent *xevent = (XEvent *)xev;
1661   GdkWindow *win = event->any.window;
1662   GdkDisplay *display = GDK_WINDOW_DISPLAY (win);
1663
1664   if ((Atom) xevent->xclient.data.l[0] == gdk_x11_get_xatom_by_name_for_display (display, "WM_DELETE_WINDOW"))
1665     {
1666   /* The delete window request specifies a window
1667    *  to delete. We don't actually destroy the
1668    *  window because "it is only a request". (The
1669    *  window might contain vital data that the
1670    *  program does not want destroyed). Instead
1671    *  the event is passed along to the program,
1672    *  which should then destroy the window.
1673    */
1674       GDK_NOTE (EVENTS,
1675                 g_message ("delete window:\t\twindow: %ld",
1676                            xevent->xclient.window));
1677       
1678       event->any.type = GDK_DELETE;
1679
1680       return GDK_FILTER_TRANSLATE;
1681     }
1682   else if ((Atom) xevent->xclient.data.l[0] == gdk_x11_get_xatom_by_name_for_display (display, "WM_TAKE_FOCUS"))
1683     {
1684       GdkWindow *win = event->any.window;
1685       Window focus_win = GDK_WINDOW_IMPL_X11(((GdkWindowObject *)win)->impl)->focus_window;
1686
1687       /* There is no way of knowing reliably whether we are viewable so we need
1688        * to trap errors so we don't cause a BadMatch.
1689        */
1690       gdk_error_trap_push ();
1691       XSetInputFocus (GDK_WINDOW_XDISPLAY (win),
1692                       focus_win,
1693                       RevertToParent,
1694                       xevent->xclient.data.l[1]);
1695       XSync (GDK_WINDOW_XDISPLAY (win), False);
1696       gdk_error_trap_pop ();
1697     }
1698   else if ((Atom) xevent->xclient.data.l[0] == gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_PING"))
1699     {
1700       XEvent xev = *xevent;
1701       
1702       xev.xclient.window = GDK_WINDOW_XROOTWIN (win);
1703       XSendEvent (GDK_WINDOW_XDISPLAY (win), 
1704                   xev.xclient.window,
1705                   False, 
1706                   SubstructureRedirectMask | SubstructureNotifyMask, &xev);
1707     }
1708
1709   return GDK_FILTER_REMOVE;
1710 }
1711
1712 void
1713 _gdk_events_queue (GdkDisplay *display)
1714 {
1715   GList *node;
1716   GdkEvent *event;
1717   XEvent xevent;
1718   Display *xdisplay = GDK_DISPLAY_XDISPLAY (display);
1719
1720   while (!_gdk_event_queue_find_first(display) && XPending (xdisplay))
1721     {
1722       XNextEvent (xdisplay, &xevent);
1723
1724       switch (xevent.type)
1725         {
1726         case KeyPress:
1727         case KeyRelease:
1728           break;
1729         default:
1730           if (XFilterEvent (&xevent, None))
1731             continue;
1732         }
1733       
1734       event = _gdk_event_new ();
1735       
1736       event->any.type = GDK_NOTHING;
1737       event->any.window = NULL;
1738       event->any.send_event = xevent.xany.send_event ? TRUE : FALSE;
1739
1740       ((GdkEventPrivate *)event)->flags |= GDK_EVENT_PENDING;
1741
1742       node = _gdk_event_queue_append (display, event);
1743
1744       if (gdk_event_translate (display, event, &xevent, FALSE))
1745         {
1746           ((GdkEventPrivate *)event)->flags &= ~GDK_EVENT_PENDING;
1747         }
1748       else
1749         {
1750           _gdk_event_queue_remove_link (display, node);
1751           g_list_free_1 (node);
1752           gdk_event_free (event);
1753         }
1754     }
1755 }
1756
1757 static gboolean  
1758 gdk_event_prepare (GSource  *source,
1759                    gint     *timeout)
1760 {
1761   GdkDisplay *display = ((GdkDisplaySource*)source)->display;
1762   gboolean retval;
1763   
1764   GDK_THREADS_ENTER ();
1765
1766   *timeout = -1;
1767   retval = (_gdk_event_queue_find_first (display) != NULL || 
1768             gdk_check_xpending (display));
1769   
1770   GDK_THREADS_LEAVE ();
1771
1772   return retval;
1773 }
1774
1775 static gboolean  
1776 gdk_event_check (GSource *source) 
1777 {
1778   GdkDisplaySource *display_source = (GdkDisplaySource*)source;
1779   gboolean retval;
1780
1781   GDK_THREADS_ENTER ();
1782
1783   if (display_source->event_poll_fd.revents & G_IO_IN)
1784     retval = (_gdk_event_queue_find_first (display_source->display) != NULL || 
1785               gdk_check_xpending (display_source->display));
1786   else
1787     retval = FALSE;
1788
1789   GDK_THREADS_LEAVE ();
1790
1791   return retval;
1792 }
1793
1794 static gboolean  
1795 gdk_event_dispatch (GSource    *source,
1796                     GSourceFunc callback,
1797                     gpointer    user_data)
1798 {
1799   GdkDisplay *display = ((GdkDisplaySource*)source)->display;
1800   GdkEvent *event;
1801  
1802   GDK_THREADS_ENTER ();
1803
1804   _gdk_events_queue (display);
1805   event = _gdk_event_unqueue (display);
1806
1807   if (event)
1808     {
1809       if (_gdk_event_func)
1810         (*_gdk_event_func) (event, _gdk_event_data);
1811       
1812       gdk_event_free (event);
1813     }
1814   
1815   GDK_THREADS_LEAVE ();
1816
1817   return TRUE;
1818 }
1819
1820 /**
1821  * gdk_event_send_client_message_for_display :
1822  * @display : the #GdkDisplay for the window where the message is to be sent.
1823  * @event : the #GdkEvent to send, which should be a #GdkEventClient.
1824  * @xid : the X window to send the X ClientMessage event to.
1825  *
1826  * Sends an X ClientMessage event to a given window.
1827  *
1828  * This could be used for communicating between different applications,
1829  * though the amount of data is limited to 20 bytes.
1830  *
1831  * Returns : non-zero on success.
1832  */
1833 gboolean
1834 gdk_event_send_client_message_for_display (GdkDisplay *display,
1835                                            GdkEvent *event,
1836                                            guint32 xid)
1837 {
1838   XEvent sev;
1839   
1840   g_return_val_if_fail(event != NULL, FALSE);
1841
1842   /* Set up our event to send, with the exception of its target window */
1843   sev.xclient.type = ClientMessage;
1844   sev.xclient.display = GDK_DISPLAY_XDISPLAY (display);
1845   sev.xclient.format = event->client.data_format;
1846   sev.xclient.window = xid;
1847   memcpy(&sev.xclient.data, &event->client.data, sizeof (sev.xclient.data));
1848   sev.xclient.message_type = gdk_x11_atom_to_xatom_for_display (display, event->client.message_type);
1849   
1850   return _gdk_send_xevent (display, xid, False, NoEventMask, &sev);
1851 }
1852
1853
1854
1855 /* Sends a ClientMessage to all toplevel client windows */
1856 gboolean
1857 gdk_event_send_client_message_to_all_recurse (GdkDisplay *display,
1858                                               XEvent     *xev, 
1859                                               guint32     xid,
1860                                               guint       level)
1861 {
1862   Atom type = None;
1863   int format;
1864   unsigned long nitems, after;
1865   unsigned char *data;
1866   Window *ret_children, ret_root, ret_parent;
1867   unsigned int ret_nchildren;
1868   gboolean send = FALSE;
1869   gboolean found = FALSE;
1870   gboolean result = FALSE;
1871   int i;
1872   
1873   gdk_error_trap_push ();
1874   
1875   if (XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), xid, 
1876                           gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_STATE"),
1877                           0, 0, False, AnyPropertyType,
1878                           &type, &format, &nitems, &after, &data) != Success)
1879     goto out;
1880   
1881   if (type)
1882     {
1883       send = TRUE;
1884       XFree (data);
1885     }
1886   else
1887     {
1888       /* OK, we're all set, now let's find some windows to send this to */
1889       if (!XQueryTree (GDK_DISPLAY_XDISPLAY (display), xid,
1890                       &ret_root, &ret_parent,
1891                       &ret_children, &ret_nchildren))   
1892         goto out;
1893
1894       for(i = 0; i < ret_nchildren; i++)
1895         if (gdk_event_send_client_message_to_all_recurse (display, xev, ret_children[i], level + 1))
1896           found = TRUE;
1897
1898       XFree (ret_children);
1899     }
1900
1901   if (send || (!found && (level == 1)))
1902     {
1903       xev->xclient.window = xid;
1904       _gdk_send_xevent (display, xid, False, NoEventMask, xev);
1905     }
1906
1907   result = send || found;
1908
1909  out:
1910   gdk_error_trap_pop ();
1911
1912   return result;
1913 }
1914
1915 /**
1916  * gdk_screen_broadcast_client_message:
1917  * @screen : the #GdkScreen where the event will be broadcasted.
1918  * @event : the #GdkEvent.
1919  *
1920  * Sends an X ClientMessage event to all toplevel windows on @screen.
1921  *
1922  * Toplevel windows are determined by checking for the WM_STATE property, 
1923  * as described in the Inter-Client Communication Conventions Manual (ICCCM).
1924  * If no windows are found with the WM_STATE property set, the message is 
1925  * sent to all children of the root window.
1926  */
1927
1928 void
1929 gdk_screen_broadcast_client_message (GdkScreen *screen, 
1930                                      GdkEvent  *event)
1931 {
1932   XEvent sev;
1933   GdkWindow *root_window;
1934
1935   g_return_if_fail (event != NULL);
1936   
1937   root_window = gdk_screen_get_root_window (screen);
1938   
1939   /* Set up our event to send, with the exception of its target window */
1940   sev.xclient.type = ClientMessage;
1941   sev.xclient.display = GDK_WINDOW_XDISPLAY (root_window);
1942   sev.xclient.format = event->client.data_format;
1943   memcpy(&sev.xclient.data, &event->client.data, sizeof (sev.xclient.data));
1944   sev.xclient.message_type = 
1945     gdk_x11_atom_to_xatom_for_display (GDK_WINDOW_DISPLAY (root_window),
1946                                        event->client.message_type);
1947
1948   gdk_event_send_client_message_to_all_recurse (gdk_screen_get_display (screen),
1949                                                 &sev, 
1950                                                 GDK_WINDOW_XID (root_window), 
1951                                                 0);
1952 }
1953
1954 /*
1955  *--------------------------------------------------------------
1956  * gdk_flush
1957  *
1958  *   Flushes the Xlib output buffer and then waits
1959  *   until all requests have been received and processed
1960  *   by the X server. The only real use for this function
1961  *   is in dealing with XShm.
1962  *
1963  * Arguments:
1964  *
1965  * Results:
1966  *
1967  * Side effects:
1968  *
1969  *--------------------------------------------------------------
1970  */
1971
1972 void
1973 gdk_flush (void)
1974 {
1975   GSList *tmp_list = _gdk_displays;
1976   
1977   while (tmp_list)
1978     {
1979       XSync (GDK_DISPLAY_XDISPLAY (tmp_list->data), False);
1980       tmp_list = tmp_list->next;
1981     }
1982 }
1983
1984 static Bool
1985 timestamp_predicate (Display *display,
1986                      XEvent  *xevent,
1987                      XPointer arg)
1988 {
1989   Window xwindow = GPOINTER_TO_UINT (arg);
1990   GdkDisplay *gdk_display = gdk_x11_lookup_xdisplay (display);
1991
1992   if (xevent->type == PropertyNotify &&
1993       xevent->xproperty.window == xwindow &&
1994       xevent->xproperty.atom == gdk_x11_get_xatom_by_name_for_display 
1995       (gdk_display, "GDK_TIMESTAMP_PROP"))
1996     return True;
1997
1998   return False;
1999 }
2000
2001 /**
2002  * gdk_x11_get_server_time:
2003  * @window: a #GdkWindow, used for communication with the server.
2004  *          The window must have GDK_PROPERTY_CHANGE_MASK in its
2005  *          events mask or a hang will result.
2006  * 
2007  * Routine to get the current X server time stamp. 
2008  * 
2009  * Return value: the time stamp.
2010  **/
2011 guint32
2012 gdk_x11_get_server_time (GdkWindow *window)
2013 {
2014   Display *xdisplay;
2015   Window   xwindow;
2016   guchar c = 'a';
2017   XEvent xevent;
2018   Atom timestamp_prop_atom;
2019
2020   g_return_val_if_fail (GDK_IS_WINDOW (window), 0);
2021   g_return_val_if_fail (!GDK_WINDOW_DESTROYED (window), 0);
2022
2023   xdisplay = GDK_WINDOW_XDISPLAY (window);
2024   xwindow = GDK_WINDOW_XWINDOW (window);
2025   timestamp_prop_atom = 
2026     gdk_x11_get_xatom_by_name_for_display (GDK_WINDOW_DISPLAY (window),
2027                                            "GDK_TIMESTAMP_PROP");
2028   
2029   XChangeProperty (xdisplay, xwindow, timestamp_prop_atom,
2030                    timestamp_prop_atom,
2031                    8, PropModeReplace, &c, 1);
2032
2033   XIfEvent (xdisplay, &xevent,
2034             timestamp_predicate, GUINT_TO_POINTER(xwindow));
2035
2036   return xevent.xproperty.time;
2037 }
2038
2039 typedef struct _NetWmSupportedAtoms NetWmSupportedAtoms;
2040
2041 struct _NetWmSupportedAtoms
2042 {
2043   Atom *atoms;
2044   gulong n_atoms;
2045 };
2046
2047 /**
2048  * gdk_x11_screen_supports_net_wm_hint:
2049  * @screen : the relevant #GdkScreen.
2050  * @property: a property atom.
2051  * 
2052  * This function is specific to the X11 backend of GDK, and indicates
2053  * whether the window manager supports a certain hint from the
2054  * Extended Window Manager Hints Specification. You can find this
2055  * specification on http://www.freedesktop.org.
2056  *
2057  * When using this function, keep in mind that the window manager
2058  * can change over time; so you shouldn't use this function in
2059  * a way that impacts persistent application state. A common bug
2060  * is that your application can start up before the window manager
2061  * does when the user logs in, and before the window manager starts
2062  * gdk_x11_screen_supports_net_wm_hint() will return %FALSE for every property.
2063  * 
2064  * Return value: %TRUE if the window manager supports @property
2065  **/
2066 gboolean
2067 gdk_x11_screen_supports_net_wm_hint (GdkScreen *screen,
2068                                      GdkAtom    property)
2069 {
2070   Atom type;
2071   gint format;
2072   gulong nitems;
2073   gulong bytes_after;
2074   Window *xwindow;
2075   gulong i;
2076   GdkScreenX11 *screen_x11;
2077   NetWmSupportedAtoms *supported_atoms;
2078   GdkDisplay *display;
2079
2080   g_return_val_if_fail (GDK_IS_SCREEN (screen), FALSE);
2081   
2082   screen_x11 = GDK_SCREEN_X11 (screen);
2083   display = screen_x11->display;
2084
2085   supported_atoms = g_object_get_data (G_OBJECT (screen), "gdk-net-wm-supported-atoms");
2086   if (!supported_atoms)
2087     {
2088       supported_atoms = g_new0 (NetWmSupportedAtoms, 1);
2089       g_object_set_data (G_OBJECT (screen), "gdk-net-wm-supported-atoms", supported_atoms);
2090     }
2091
2092   if (screen_x11->wmspec_check_window != None)
2093     {
2094       if (supported_atoms->atoms == NULL)
2095         return FALSE;
2096       
2097       i = 0;
2098       while (i < supported_atoms->n_atoms)
2099         {
2100           if (supported_atoms->atoms[i] == gdk_x11_atom_to_xatom_for_display (display, property))
2101             return TRUE;
2102           
2103           ++i;
2104         }
2105       
2106       return FALSE;
2107     }
2108
2109   if (supported_atoms->atoms)
2110     XFree (supported_atoms->atoms);
2111
2112   supported_atoms->atoms = NULL;
2113   supported_atoms->n_atoms = 0;
2114   
2115   /* This function is very slow on every call if you are not running a
2116    * spec-supporting WM. For now not optimized, because it isn't in
2117    * any critical code paths, but if you used it somewhere that had to
2118    * be fast you want to avoid "GTK is slow with old WMs" complaints.
2119    * Probably at that point the function should be changed to query
2120    * _NET_SUPPORTING_WM_CHECK only once every 10 seconds or something.
2121    */
2122   
2123   XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), screen_x11->xroot_window,
2124                       gdk_x11_get_xatom_by_name_for_display (display, "_NET_SUPPORTING_WM_CHECK"),
2125                       0, G_MAXLONG, False, XA_WINDOW, &type, &format, 
2126                       &nitems, &bytes_after, (guchar **) & xwindow);
2127   
2128   if (type != XA_WINDOW)
2129     return FALSE;
2130
2131   gdk_error_trap_push ();
2132
2133   /* Find out if this WM goes away, so we can reset everything. */
2134   XSelectInput (screen_x11->xdisplay, *xwindow, StructureNotifyMask);
2135
2136   gdk_display_sync (screen_x11->display);
2137   
2138   if (gdk_error_trap_pop ())
2139     {
2140       XFree (xwindow);
2141       return FALSE;
2142     }
2143   
2144   XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), screen_x11->xroot_window,
2145                       gdk_x11_get_xatom_by_name_for_display (display, "_NET_SUPPORTED"),
2146                       0, G_MAXLONG, False, XA_ATOM, &type, &format, 
2147                       &supported_atoms->n_atoms, &bytes_after,
2148                       (guchar **)&supported_atoms->atoms);
2149   
2150   if (type != XA_ATOM)
2151     return FALSE;
2152   
2153   screen_x11->wmspec_check_window = *xwindow;
2154   XFree (xwindow);
2155   
2156   /* since wmspec_check_window != None this isn't infinite. ;-) */
2157   return gdk_x11_screen_supports_net_wm_hint (screen, property);
2158 }
2159
2160 /**
2161  * gdk_net_wm_supports:
2162  * @property: a property atom.
2163  * 
2164  * This function is specific to the X11 backend of GDK, and indicates
2165  * whether the window manager for the default screen supports a certain
2166  * hint from the Extended Window Manager Hints Specification. See
2167  * gdk_x11_screen_supports_net_wm_hint() for complete details.
2168  * 
2169  * Return value: %TRUE if the window manager supports @property
2170  **/
2171 gboolean
2172 gdk_net_wm_supports (GdkAtom property)
2173 {
2174   return gdk_x11_screen_supports_net_wm_hint (gdk_screen_get_default (), property);
2175 }
2176
2177 static struct
2178 {
2179   const char *xsettings_name;
2180   const char *gdk_name;
2181 } settings_map[] = {
2182   { "Net/DoubleClickTime", "gtk-double-click-time" },
2183   { "Net/DndDragThreshold", "gtk-dnd-drag-threshold" },
2184   { "Gtk/CanChangeAccels", "gtk-can-change-accels" },
2185   { "Gtk/ColorPalette", "gtk-color-palette" },
2186   { "Gtk/FontName", "gtk-font-name" },
2187   { "Gtk/KeyThemeName", "gtk-key-theme-name" },
2188   { "Gtk/ToolbarStyle", "gtk-toolbar-style" },
2189   { "Gtk/ToolbarIconSize", "gtk-toolbar-icon-size" },
2190   { "Net/CursorBlink", "gtk-cursor-blink" },
2191   { "Net/CursorBlinkTime", "gtk-cursor-blink-time" },
2192   { "Net/ThemeName", "gtk-theme-name" }
2193 };
2194
2195 static void
2196 gdk_xsettings_notify_cb (const char       *name,
2197                          XSettingsAction   action,
2198                          XSettingsSetting *setting,
2199                          void             *data)
2200 {
2201   GdkEvent new_event;
2202   GdkScreen *screen = data;
2203   int i;
2204
2205   new_event.type = GDK_SETTING;
2206   new_event.setting.window = gdk_screen_get_root_window (screen);
2207   new_event.setting.send_event = FALSE;
2208   new_event.setting.name = NULL;
2209
2210   for (i = 0; i < G_N_ELEMENTS (settings_map) ; i++)
2211     if (strcmp (settings_map[i].xsettings_name, name) == 0)
2212       {
2213         new_event.setting.name = (char *)settings_map[i].gdk_name;
2214         break;
2215       }
2216
2217   if (!new_event.setting.name)
2218     return;
2219   
2220   switch (action)
2221     {
2222     case XSETTINGS_ACTION_NEW:
2223       new_event.setting.action = GDK_SETTING_ACTION_NEW;
2224       break;
2225     case XSETTINGS_ACTION_CHANGED:
2226       new_event.setting.action = GDK_SETTING_ACTION_CHANGED;
2227       break;
2228     case XSETTINGS_ACTION_DELETED:
2229       new_event.setting.action = GDK_SETTING_ACTION_DELETED;
2230       break;
2231     }
2232
2233   gdk_event_put (&new_event);
2234 }
2235
2236 static gboolean
2237 check_transform (const gchar *xsettings_name,
2238                  GType        src_type,
2239                  GType        dest_type)
2240 {
2241   if (!g_value_type_transformable (src_type, dest_type))
2242     {
2243       g_warning ("Cannot tranform xsetting %s of type %s to type %s\n",
2244                  xsettings_name,
2245                  g_type_name (src_type),
2246                  g_type_name (dest_type));
2247       return FALSE;
2248     }
2249   else
2250     return TRUE;
2251 }
2252
2253 /**
2254  * gdk_screen_get_setting:
2255  * @screen: the #GdkScreen where the setting is located
2256  * @name: the name of the setting
2257  * @value: location to store the value of the setting
2258  *
2259  * Retrieves a desktop-wide setting such as double-click time
2260  * for the #GdkScreen @screen. 
2261  *
2262  * FIXME needs a list of valid settings here, or a link to 
2263  * more information.
2264  * 
2265  * Returns : %TRUE if the setting existed and a value was stored
2266  *   in @value, %FALSE otherwise.
2267  **/
2268 gboolean
2269 gdk_screen_get_setting (GdkScreen   *screen,
2270                         const gchar *name,
2271                         GValue      *value)
2272 {
2273
2274   const char *xsettings_name = NULL;
2275   XSettingsResult result;
2276   XSettingsSetting *setting;
2277   GdkScreenX11 *screen_x11;
2278   gboolean success = FALSE;
2279   gint i;
2280   GValue tmp_val = { 0, };
2281   
2282   g_return_val_if_fail (GDK_IS_SCREEN (screen), FALSE);
2283   
2284   screen_x11 = GDK_SCREEN_X11 (screen);
2285
2286   for (i = 0; i < G_N_ELEMENTS (settings_map) ; i++)
2287     if (strcmp (settings_map[i].gdk_name, name) == 0)
2288       {
2289         xsettings_name = settings_map[i].xsettings_name;
2290         break;
2291       }
2292
2293   if (!xsettings_name)
2294     return FALSE;
2295
2296   result = xsettings_client_get_setting (screen_x11->xsettings_client, 
2297                                          xsettings_name, &setting);
2298   if (result != XSETTINGS_SUCCESS)
2299     return FALSE;
2300
2301   switch (setting->type)
2302     {
2303     case XSETTINGS_TYPE_INT:
2304       if (check_transform (xsettings_name, G_TYPE_INT, G_VALUE_TYPE (value)))
2305         {
2306           g_value_init (&tmp_val, G_TYPE_INT);
2307           g_value_set_int (&tmp_val, setting->data.v_int);
2308           g_value_transform (&tmp_val, value);
2309
2310           success = TRUE;
2311         }
2312       break;
2313     case XSETTINGS_TYPE_STRING:
2314       if (check_transform (xsettings_name, G_TYPE_STRING, G_VALUE_TYPE (value)))
2315         {
2316           g_value_init (&tmp_val, G_TYPE_STRING);
2317           g_value_set_string (&tmp_val, setting->data.v_string);
2318           g_value_transform (&tmp_val, value);
2319
2320           success = TRUE;
2321         }
2322       break;
2323     case XSETTINGS_TYPE_COLOR:
2324       if (!check_transform (xsettings_name, GDK_TYPE_COLOR, G_VALUE_TYPE (value)))
2325         {
2326           GdkColor color;
2327           
2328           g_value_init (&tmp_val, GDK_TYPE_COLOR);
2329
2330           color.pixel = 0;
2331           color.red = setting->data.v_color.red;
2332           color.green = setting->data.v_color.green;
2333           color.blue = setting->data.v_color.blue;
2334           
2335           g_value_set_boxed (&tmp_val, &color);
2336           
2337           g_value_transform (&tmp_val, value);
2338           
2339           success = TRUE;
2340         }
2341       break;
2342     }
2343   
2344   g_value_unset (&tmp_val);
2345
2346   xsettings_setting_free (setting);
2347
2348   return success;
2349 }
2350
2351 static GdkFilterReturn 
2352 gdk_xsettings_client_event_filter (GdkXEvent *xevent,
2353                                    GdkEvent  *event,
2354                                    gpointer   data)
2355 {
2356   GdkScreenX11 *screen = data;
2357   
2358   if (xsettings_client_process_event (screen->xsettings_client, (XEvent *)xevent))
2359     return GDK_FILTER_REMOVE;
2360   else
2361     return GDK_FILTER_CONTINUE;
2362 }
2363
2364 static void 
2365 gdk_xsettings_watch_cb (Window   window,
2366                         Bool     is_start,
2367                         long     mask,
2368                         void    *cb_data)
2369 {
2370   GdkWindow *gdkwin;
2371   GdkScreen *screen = cb_data;
2372   GdkScreenX11 *screen_x11 = GDK_SCREEN_X11 (screen);
2373
2374   gdkwin = gdk_window_lookup_for_display (gdk_screen_get_display (screen), window);
2375
2376   if (is_start)
2377     {
2378       if (!gdkwin)
2379         gdkwin = gdk_window_foreign_new_for_display (gdk_screen_get_display (screen), window);
2380       else
2381         g_object_ref (gdkwin);
2382       
2383       gdk_window_add_filter (gdkwin, gdk_xsettings_client_event_filter, screen);
2384     }
2385   else
2386     {
2387       g_assert (gdkwin);
2388       gdk_window_remove_filter (gdkwin, gdk_xsettings_client_event_filter, screen);
2389       g_object_unref (gdkwin);
2390     }
2391 }