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