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