]> Pileus Git - ~andy/gtk/blob - gdk/x11/gdkevents-x11.c
Changes multihead reorganizing code for win32 support, mostly from a patch
[~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                            : xevent->xconfigure.event != xevent->xconfigure.window
1386                            ? " (discarding substructure)"
1387                            : ""));
1388       if (window &&
1389           xevent->xconfigure.event == xevent->xconfigure.window &&
1390           !GDK_WINDOW_DESTROYED (window) &&
1391           (window_private->extension_events != 0))
1392         _gdk_input_configure_event (&xevent->xconfigure, window);
1393
1394       if (!window ||
1395           xevent->xconfigure.event != xevent->xconfigure.window ||
1396           GDK_WINDOW_TYPE (window) == GDK_WINDOW_CHILD ||
1397           GDK_WINDOW_TYPE (window) == GDK_WINDOW_ROOT)
1398         return_val = FALSE;
1399       else
1400         {
1401           event->configure.type = GDK_CONFIGURE;
1402           event->configure.window = window;
1403           event->configure.width = xevent->xconfigure.width;
1404           event->configure.height = xevent->xconfigure.height;
1405           
1406           if (!xevent->xconfigure.send_event && 
1407               !GDK_WINDOW_DESTROYED (window))
1408             {
1409               gint tx = 0;
1410               gint ty = 0;
1411               Window child_window = 0;
1412
1413               gdk_error_trap_push ();
1414               if (XTranslateCoordinates (GDK_DRAWABLE_XDISPLAY (window),
1415                                          GDK_DRAWABLE_XID (window),
1416                                          screen_x11->xroot_window,
1417                                          0, 0,
1418                                          &tx, &ty,
1419                                          &child_window))
1420                 {
1421                   if (!gdk_error_trap_pop ())
1422                     {
1423                       event->configure.x = tx;
1424                       event->configure.y = ty;
1425                     }
1426                 }
1427               else
1428                 gdk_error_trap_pop ();
1429             }
1430           else
1431             {
1432               event->configure.x = xevent->xconfigure.x;
1433               event->configure.y = xevent->xconfigure.y;
1434             }
1435           window_private->x = event->configure.x;
1436           window_private->y = event->configure.y;
1437           GDK_WINDOW_IMPL_X11 (window_private->impl)->width = xevent->xconfigure.width;
1438           GDK_WINDOW_IMPL_X11 (window_private->impl)->height = xevent->xconfigure.height;
1439           if (window_private->resize_count >= 1)
1440             {
1441               window_private->resize_count -= 1;
1442
1443               if (window_private->resize_count == 0)
1444                 _gdk_moveresize_configure_done (display, window);
1445             }
1446         }
1447       break;
1448       
1449     case PropertyNotify:
1450       GDK_NOTE (EVENTS,
1451                 g_message ("property notify:\twindow: %ld, atom(%ld): %s%s%s",
1452                            xevent->xproperty.window,
1453                            xevent->xproperty.atom,
1454                            "\"",
1455                            gdk_x11_get_xatom_name_for_display (display, xevent->xproperty.atom),
1456                            "\""));
1457
1458       if (window_private == NULL)
1459         {
1460           return_val = FALSE;
1461           break;
1462         }
1463       
1464       if (xevent->xproperty.atom == gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_STATE") ||
1465           xevent->xproperty.atom == gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_DESKTOP"))
1466         {
1467           /* If window state changed, then synthesize those events. */
1468           gdk_check_wm_state_changed (window);
1469         }
1470       
1471       if (window_private->event_mask & GDK_PROPERTY_CHANGE_MASK) 
1472         {
1473           event->property.type = GDK_PROPERTY_NOTIFY;
1474           event->property.window = window;
1475           event->property.atom = gdk_x11_xatom_to_atom_for_display (display, xevent->xproperty.atom);
1476           event->property.time = xevent->xproperty.time;
1477           event->property.state = xevent->xproperty.state;
1478         }
1479       else
1480         return_val = FALSE;
1481
1482       break;
1483       
1484     case SelectionClear:
1485       GDK_NOTE (EVENTS,
1486                 g_message ("selection clear:\twindow: %ld",
1487                            xevent->xproperty.window));
1488
1489       if (_gdk_selection_filter_clear_event (&xevent->xselectionclear))
1490         {
1491           event->selection.type = GDK_SELECTION_CLEAR;
1492           event->selection.window = window;
1493           event->selection.selection = gdk_x11_xatom_to_atom_for_display (display, xevent->xselectionclear.selection);
1494           event->selection.time = xevent->xselectionclear.time;
1495         }
1496       else
1497         return_val = FALSE;
1498           
1499       break;
1500       
1501     case SelectionRequest:
1502       GDK_NOTE (EVENTS,
1503                 g_message ("selection request:\twindow: %ld",
1504                            xevent->xproperty.window));
1505       
1506       event->selection.type = GDK_SELECTION_REQUEST;
1507       event->selection.window = window;
1508       event->selection.selection = gdk_x11_xatom_to_atom_for_display (display, xevent->xselectionrequest.selection);
1509       event->selection.target = gdk_x11_xatom_to_atom_for_display (display, xevent->xselectionrequest.target);
1510       event->selection.property = gdk_x11_xatom_to_atom_for_display (display, xevent->xselectionrequest.property);
1511       event->selection.requestor = xevent->xselectionrequest.requestor;
1512       event->selection.time = xevent->xselectionrequest.time;
1513       
1514       break;
1515       
1516     case SelectionNotify:
1517       GDK_NOTE (EVENTS,
1518                 g_message ("selection notify:\twindow: %ld",
1519                            xevent->xproperty.window));
1520       
1521       
1522       event->selection.type = GDK_SELECTION_NOTIFY;
1523       event->selection.window = window;
1524       event->selection.selection = gdk_x11_xatom_to_atom_for_display (display, xevent->xselection.selection);
1525       event->selection.target = gdk_x11_xatom_to_atom_for_display (display, xevent->xselection.target);
1526       event->selection.property = gdk_x11_xatom_to_atom_for_display (display, xevent->xselection.property);
1527       event->selection.time = xevent->xselection.time;
1528       
1529       break;
1530       
1531     case ColormapNotify:
1532       GDK_NOTE (EVENTS,
1533                 g_message ("colormap notify:\twindow: %ld",
1534                            xevent->xcolormap.window));
1535       
1536       /* Not currently handled */
1537       return_val = FALSE;
1538       break;
1539       
1540     case ClientMessage:
1541       {
1542         GList *tmp_list;
1543         GdkFilterReturn result = GDK_FILTER_CONTINUE;
1544         GdkAtom message_type = gdk_x11_xatom_to_atom_for_display (display, xevent->xclient.message_type);
1545
1546         GDK_NOTE (EVENTS,
1547                   g_message ("client message:\twindow: %ld",
1548                              xevent->xclient.window));
1549         
1550         tmp_list = display_x11->client_filters;
1551         while (tmp_list)
1552           {
1553             GdkClientFilter *filter = tmp_list->data;
1554             if (filter->type == message_type)
1555               {
1556                 result = (*filter->function) (xevent, event, filter->data);
1557                 break;
1558               }
1559             
1560             tmp_list = tmp_list->next;
1561           }
1562
1563         switch (result)
1564           {
1565           case GDK_FILTER_REMOVE:
1566             return_val = FALSE;
1567             break;
1568           case GDK_FILTER_TRANSLATE:
1569             return_val = TRUE;
1570             break;
1571           case GDK_FILTER_CONTINUE:
1572             /* Send unknown ClientMessage's on to Gtk for it to use */
1573             if (window_private == NULL)
1574               {
1575                 return_val = FALSE;
1576               }
1577             else
1578               {
1579                 event->client.type = GDK_CLIENT_EVENT;
1580                 event->client.window = window;
1581                 event->client.message_type = message_type;
1582                 event->client.data_format = xevent->xclient.format;
1583                 memcpy(&event->client.data, &xevent->xclient.data,
1584                        sizeof(event->client.data));
1585               }
1586             break;
1587           }
1588       }
1589       
1590       break;
1591       
1592     case MappingNotify:
1593       GDK_NOTE (EVENTS,
1594                 g_message ("mapping notify"));
1595       
1596       /* Let XLib know that there is a new keyboard mapping.
1597        */
1598       XRefreshKeyboardMapping (&xevent->xmapping);
1599       ++display_x11->keymap_serial;
1600       return_val = FALSE;
1601       break;
1602
1603     default:
1604 #ifdef HAVE_XKB
1605       if (xevent->type == display_x11->xkb_event_type)
1606         {
1607           XkbEvent *xkb_event = (XkbEvent *)xevent;
1608
1609           switch (xkb_event->any.xkb_type)
1610             {
1611             case XkbMapNotify:
1612               ++display_x11->keymap_serial;
1613
1614               return_val = FALSE;
1615               break;
1616               
1617             case XkbStateNotify:
1618               _gdk_keymap_state_changed (display);
1619               break;
1620             }
1621         }
1622       else
1623 #endif
1624         {
1625           /* something else - (e.g., a Xinput event) */
1626           
1627           if (window_private &&
1628               !GDK_WINDOW_DESTROYED (window_private) &&
1629               (window_private->extension_events != 0))
1630             return_val = _gdk_input_other_event(event, xevent, window);
1631           else
1632             return_val = FALSE;
1633           
1634           break;
1635         }
1636     }
1637
1638  done:
1639   if (return_val)
1640     {
1641       if (event->any.window)
1642         gdk_window_ref (event->any.window);
1643       if (((event->any.type == GDK_ENTER_NOTIFY) ||
1644            (event->any.type == GDK_LEAVE_NOTIFY)) &&
1645           (event->crossing.subwindow != NULL))
1646         gdk_window_ref (event->crossing.subwindow);
1647     }
1648   else
1649     {
1650       /* Mark this event as having no resources to be freed */
1651       event->any.window = NULL;
1652       event->any.type = GDK_NOTHING;
1653     }
1654   
1655   if (window)
1656     gdk_window_unref (window);
1657   
1658   return return_val;
1659 }
1660
1661 static GdkFilterReturn
1662 gdk_wm_protocols_filter (GdkXEvent *xev,
1663                          GdkEvent  *event,
1664                          gpointer data)
1665 {
1666   XEvent *xevent = (XEvent *)xev;
1667   GdkWindow *win = event->any.window;
1668   GdkDisplay *display = GDK_WINDOW_DISPLAY (win);
1669
1670   if ((Atom) xevent->xclient.data.l[0] == gdk_x11_get_xatom_by_name_for_display (display, "WM_DELETE_WINDOW"))
1671     {
1672   /* The delete window request specifies a window
1673    *  to delete. We don't actually destroy the
1674    *  window because "it is only a request". (The
1675    *  window might contain vital data that the
1676    *  program does not want destroyed). Instead
1677    *  the event is passed along to the program,
1678    *  which should then destroy the window.
1679    */
1680       GDK_NOTE (EVENTS,
1681                 g_message ("delete window:\t\twindow: %ld",
1682                            xevent->xclient.window));
1683       
1684       event->any.type = GDK_DELETE;
1685
1686       return GDK_FILTER_TRANSLATE;
1687     }
1688   else if ((Atom) xevent->xclient.data.l[0] == gdk_x11_get_xatom_by_name_for_display (display, "WM_TAKE_FOCUS"))
1689     {
1690       GdkWindow *win = event->any.window;
1691       Window focus_win = GDK_WINDOW_IMPL_X11(((GdkWindowObject *)win)->impl)->focus_window;
1692
1693       /* There is no way of knowing reliably whether we are viewable so we need
1694        * to trap errors so we don't cause a BadMatch.
1695        */
1696       gdk_error_trap_push ();
1697       XSetInputFocus (GDK_WINDOW_XDISPLAY (win),
1698                       focus_win,
1699                       RevertToParent,
1700                       xevent->xclient.data.l[1]);
1701       XSync (GDK_WINDOW_XDISPLAY (win), False);
1702       gdk_error_trap_pop ();
1703     }
1704   else if ((Atom) xevent->xclient.data.l[0] == gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_PING"))
1705     {
1706       XEvent xev = *xevent;
1707       
1708       xev.xclient.window = GDK_WINDOW_XROOTWIN (win);
1709       XSendEvent (GDK_WINDOW_XDISPLAY (win), 
1710                   xev.xclient.window,
1711                   False, 
1712                   SubstructureRedirectMask | SubstructureNotifyMask, &xev);
1713     }
1714
1715   return GDK_FILTER_REMOVE;
1716 }
1717
1718 void
1719 _gdk_events_queue (GdkDisplay *display)
1720 {
1721   GList *node;
1722   GdkEvent *event;
1723   XEvent xevent;
1724   Display *xdisplay = GDK_DISPLAY_XDISPLAY (display);
1725
1726   while (!_gdk_event_queue_find_first(display) && XPending (xdisplay))
1727     {
1728       XNextEvent (xdisplay, &xevent);
1729
1730       switch (xevent.type)
1731         {
1732         case KeyPress:
1733         case KeyRelease:
1734           break;
1735         default:
1736           if (XFilterEvent (&xevent, None))
1737             continue;
1738         }
1739       
1740       event = _gdk_event_new ();
1741       
1742       event->any.type = GDK_NOTHING;
1743       event->any.window = NULL;
1744       event->any.send_event = xevent.xany.send_event ? TRUE : FALSE;
1745
1746       ((GdkEventPrivate *)event)->flags |= GDK_EVENT_PENDING;
1747
1748       node = _gdk_event_queue_append (display, event);
1749
1750       if (gdk_event_translate (display, event, &xevent, FALSE))
1751         {
1752           ((GdkEventPrivate *)event)->flags &= ~GDK_EVENT_PENDING;
1753         }
1754       else
1755         {
1756           _gdk_event_queue_remove_link (display, node);
1757           g_list_free_1 (node);
1758           gdk_event_free (event);
1759         }
1760     }
1761 }
1762
1763 static gboolean  
1764 gdk_event_prepare (GSource  *source,
1765                    gint     *timeout)
1766 {
1767   GdkDisplay *display = ((GdkDisplaySource*)source)->display;
1768   gboolean retval;
1769   
1770   GDK_THREADS_ENTER ();
1771
1772   *timeout = -1;
1773   retval = (_gdk_event_queue_find_first (display) != NULL || 
1774             gdk_check_xpending (display));
1775   
1776   GDK_THREADS_LEAVE ();
1777
1778   return retval;
1779 }
1780
1781 static gboolean  
1782 gdk_event_check (GSource *source) 
1783 {
1784   GdkDisplaySource *display_source = (GdkDisplaySource*)source;
1785   gboolean retval;
1786
1787   GDK_THREADS_ENTER ();
1788
1789   if (display_source->event_poll_fd.revents & G_IO_IN)
1790     retval = (_gdk_event_queue_find_first (display_source->display) != NULL || 
1791               gdk_check_xpending (display_source->display));
1792   else
1793     retval = FALSE;
1794
1795   GDK_THREADS_LEAVE ();
1796
1797   return retval;
1798 }
1799
1800 static gboolean  
1801 gdk_event_dispatch (GSource    *source,
1802                     GSourceFunc callback,
1803                     gpointer    user_data)
1804 {
1805   GdkDisplay *display = ((GdkDisplaySource*)source)->display;
1806   GdkEvent *event;
1807  
1808   GDK_THREADS_ENTER ();
1809
1810   _gdk_events_queue (display);
1811   event = _gdk_event_unqueue (display);
1812
1813   if (event)
1814     {
1815       if (_gdk_event_func)
1816         (*_gdk_event_func) (event, _gdk_event_data);
1817       
1818       gdk_event_free (event);
1819     }
1820   
1821   GDK_THREADS_LEAVE ();
1822
1823   return TRUE;
1824 }
1825
1826 /**
1827  * gdk_event_send_client_message_for_display :
1828  * @display : the #GdkDisplay for the window where the message is to be sent.
1829  * @event : the #GdkEvent to send, which should be a #GdkEventClient.
1830  * @xid : the X window to send the X ClientMessage event to.
1831  *
1832  * Sends an X ClientMessage event to a given window.
1833  *
1834  * This could be used for communicating between different applications,
1835  * though the amount of data is limited to 20 bytes.
1836  *
1837  * Returns : non-zero on success.
1838  */
1839 gboolean
1840 gdk_event_send_client_message_for_display (GdkDisplay *display,
1841                                            GdkEvent *event,
1842                                            guint32 xid)
1843 {
1844   XEvent sev;
1845   
1846   g_return_val_if_fail(event != NULL, FALSE);
1847
1848   /* Set up our event to send, with the exception of its target window */
1849   sev.xclient.type = ClientMessage;
1850   sev.xclient.display = GDK_DISPLAY_XDISPLAY (display);
1851   sev.xclient.format = event->client.data_format;
1852   sev.xclient.window = xid;
1853   memcpy(&sev.xclient.data, &event->client.data, sizeof (sev.xclient.data));
1854   sev.xclient.message_type = gdk_x11_atom_to_xatom_for_display (display, event->client.message_type);
1855   
1856   return _gdk_send_xevent (display, xid, False, NoEventMask, &sev);
1857 }
1858
1859
1860
1861 /* Sends a ClientMessage to all toplevel client windows */
1862 gboolean
1863 gdk_event_send_client_message_to_all_recurse (GdkDisplay *display,
1864                                               XEvent     *xev, 
1865                                               guint32     xid,
1866                                               guint       level)
1867 {
1868   Atom type = None;
1869   int format;
1870   unsigned long nitems, after;
1871   unsigned char *data;
1872   Window *ret_children, ret_root, ret_parent;
1873   unsigned int ret_nchildren;
1874   gboolean send = FALSE;
1875   gboolean found = FALSE;
1876   gboolean result = FALSE;
1877   int i;
1878   
1879   gdk_error_trap_push ();
1880   
1881   if (XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), xid, 
1882                           gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_STATE"),
1883                           0, 0, False, AnyPropertyType,
1884                           &type, &format, &nitems, &after, &data) != Success)
1885     goto out;
1886   
1887   if (type)
1888     {
1889       send = TRUE;
1890       XFree (data);
1891     }
1892   else
1893     {
1894       /* OK, we're all set, now let's find some windows to send this to */
1895       if (!XQueryTree (GDK_DISPLAY_XDISPLAY (display), xid,
1896                       &ret_root, &ret_parent,
1897                       &ret_children, &ret_nchildren))   
1898         goto out;
1899
1900       for(i = 0; i < ret_nchildren; i++)
1901         if (gdk_event_send_client_message_to_all_recurse (display, xev, ret_children[i], level + 1))
1902           found = TRUE;
1903
1904       XFree (ret_children);
1905     }
1906
1907   if (send || (!found && (level == 1)))
1908     {
1909       xev->xclient.window = xid;
1910       _gdk_send_xevent (display, xid, False, NoEventMask, xev);
1911     }
1912
1913   result = send || found;
1914
1915  out:
1916   gdk_error_trap_pop ();
1917
1918   return result;
1919 }
1920
1921 /**
1922  * gdk_screen_broadcast_client_message:
1923  * @screen : the #GdkScreen where the event will be broadcasted.
1924  * @event : the #GdkEvent.
1925  *
1926  * Sends an X ClientMessage event to all toplevel windows on @screen.
1927  *
1928  * Toplevel windows are determined by checking for the WM_STATE property, 
1929  * as described in the Inter-Client Communication Conventions Manual (ICCCM).
1930  * If no windows are found with the WM_STATE property set, the message is 
1931  * sent to all children of the root window.
1932  */
1933
1934 void
1935 gdk_screen_broadcast_client_message (GdkScreen *screen, 
1936                                      GdkEvent  *event)
1937 {
1938   XEvent sev;
1939   GdkWindow *root_window;
1940
1941   g_return_if_fail (event != NULL);
1942   
1943   root_window = gdk_screen_get_root_window (screen);
1944   
1945   /* Set up our event to send, with the exception of its target window */
1946   sev.xclient.type = ClientMessage;
1947   sev.xclient.display = GDK_WINDOW_XDISPLAY (root_window);
1948   sev.xclient.format = event->client.data_format;
1949   memcpy(&sev.xclient.data, &event->client.data, sizeof (sev.xclient.data));
1950   sev.xclient.message_type = 
1951     gdk_x11_atom_to_xatom_for_display (GDK_WINDOW_DISPLAY (root_window),
1952                                        event->client.message_type);
1953
1954   gdk_event_send_client_message_to_all_recurse (gdk_screen_get_display (screen),
1955                                                 &sev, 
1956                                                 GDK_WINDOW_XID (root_window), 
1957                                                 0);
1958 }
1959
1960 /*
1961  *--------------------------------------------------------------
1962  * gdk_flush
1963  *
1964  *   Flushes the Xlib output buffer and then waits
1965  *   until all requests have been received and processed
1966  *   by the X server. The only real use for this function
1967  *   is in dealing with XShm.
1968  *
1969  * Arguments:
1970  *
1971  * Results:
1972  *
1973  * Side effects:
1974  *
1975  *--------------------------------------------------------------
1976  */
1977
1978 void
1979 gdk_flush (void)
1980 {
1981   GSList *tmp_list = _gdk_displays;
1982   
1983   while (tmp_list)
1984     {
1985       XSync (GDK_DISPLAY_XDISPLAY (tmp_list->data), False);
1986       tmp_list = tmp_list->next;
1987     }
1988 }
1989
1990 static Bool
1991 timestamp_predicate (Display *display,
1992                      XEvent  *xevent,
1993                      XPointer arg)
1994 {
1995   Window xwindow = GPOINTER_TO_UINT (arg);
1996   GdkDisplay *gdk_display = gdk_x11_lookup_xdisplay (display);
1997
1998   if (xevent->type == PropertyNotify &&
1999       xevent->xproperty.window == xwindow &&
2000       xevent->xproperty.atom == gdk_x11_get_xatom_by_name_for_display 
2001       (gdk_display, "GDK_TIMESTAMP_PROP"))
2002     return True;
2003
2004   return False;
2005 }
2006
2007 /**
2008  * gdk_x11_get_server_time:
2009  * @window: a #GdkWindow, used for communication with the server.
2010  *          The window must have GDK_PROPERTY_CHANGE_MASK in its
2011  *          events mask or a hang will result.
2012  * 
2013  * Routine to get the current X server time stamp. 
2014  * 
2015  * Return value: the time stamp.
2016  **/
2017 guint32
2018 gdk_x11_get_server_time (GdkWindow *window)
2019 {
2020   Display *xdisplay;
2021   Window   xwindow;
2022   guchar c = 'a';
2023   XEvent xevent;
2024   Atom timestamp_prop_atom;
2025
2026   g_return_val_if_fail (GDK_IS_WINDOW (window), 0);
2027   g_return_val_if_fail (!GDK_WINDOW_DESTROYED (window), 0);
2028
2029   xdisplay = GDK_WINDOW_XDISPLAY (window);
2030   xwindow = GDK_WINDOW_XWINDOW (window);
2031   timestamp_prop_atom = 
2032     gdk_x11_get_xatom_by_name_for_display (GDK_WINDOW_DISPLAY (window),
2033                                            "GDK_TIMESTAMP_PROP");
2034   
2035   XChangeProperty (xdisplay, xwindow, timestamp_prop_atom,
2036                    timestamp_prop_atom,
2037                    8, PropModeReplace, &c, 1);
2038
2039   XIfEvent (xdisplay, &xevent,
2040             timestamp_predicate, GUINT_TO_POINTER(xwindow));
2041
2042   return xevent.xproperty.time;
2043 }
2044
2045 typedef struct _NetWmSupportedAtoms NetWmSupportedAtoms;
2046
2047 struct _NetWmSupportedAtoms
2048 {
2049   Atom *atoms;
2050   gulong n_atoms;
2051 };
2052
2053 /**
2054  * gdk_x11_screen_supports_net_wm_hint:
2055  * @screen : the relevant #GdkScreen.
2056  * @property: a property atom.
2057  * 
2058  * This function is specific to the X11 backend of GDK, and indicates
2059  * whether the window manager supports a certain hint from the
2060  * Extended Window Manager Hints Specification. You can find this
2061  * specification on http://www.freedesktop.org.
2062  *
2063  * When using this function, keep in mind that the window manager
2064  * can change over time; so you shouldn't use this function in
2065  * a way that impacts persistent application state. A common bug
2066  * is that your application can start up before the window manager
2067  * does when the user logs in, and before the window manager starts
2068  * gdk_x11_screen_supports_net_wm_hint() will return %FALSE for every property.
2069  * 
2070  * Return value: %TRUE if the window manager supports @property
2071  **/
2072 gboolean
2073 gdk_x11_screen_supports_net_wm_hint (GdkScreen *screen,
2074                                      GdkAtom    property)
2075 {
2076   Atom type;
2077   gint format;
2078   gulong nitems;
2079   gulong bytes_after;
2080   Window *xwindow;
2081   gulong i;
2082   GdkScreenX11 *screen_x11;
2083   NetWmSupportedAtoms *supported_atoms;
2084   GdkDisplay *display;
2085
2086   g_return_val_if_fail (GDK_IS_SCREEN (screen), FALSE);
2087   
2088   screen_x11 = GDK_SCREEN_X11 (screen);
2089   display = screen_x11->display;
2090
2091   supported_atoms = g_object_get_data (G_OBJECT (screen), "gdk-net-wm-supported-atoms");
2092   if (!supported_atoms)
2093     {
2094       supported_atoms = g_new0 (NetWmSupportedAtoms, 1);
2095       g_object_set_data (G_OBJECT (screen), "gdk-net-wm-supported-atoms", supported_atoms);
2096     }
2097
2098   if (screen_x11->wmspec_check_window != None)
2099     {
2100       if (supported_atoms->atoms == NULL)
2101         return FALSE;
2102       
2103       i = 0;
2104       while (i < supported_atoms->n_atoms)
2105         {
2106           if (supported_atoms->atoms[i] == gdk_x11_atom_to_xatom_for_display (display, property))
2107             return TRUE;
2108           
2109           ++i;
2110         }
2111       
2112       return FALSE;
2113     }
2114
2115   if (supported_atoms->atoms)
2116     XFree (supported_atoms->atoms);
2117
2118   supported_atoms->atoms = NULL;
2119   supported_atoms->n_atoms = 0;
2120   
2121   /* This function is very slow on every call if you are not running a
2122    * spec-supporting WM. For now not optimized, because it isn't in
2123    * any critical code paths, but if you used it somewhere that had to
2124    * be fast you want to avoid "GTK is slow with old WMs" complaints.
2125    * Probably at that point the function should be changed to query
2126    * _NET_SUPPORTING_WM_CHECK only once every 10 seconds or something.
2127    */
2128   
2129   XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), screen_x11->xroot_window,
2130                       gdk_x11_get_xatom_by_name_for_display (display, "_NET_SUPPORTING_WM_CHECK"),
2131                       0, G_MAXLONG, False, XA_WINDOW, &type, &format, 
2132                       &nitems, &bytes_after, (guchar **) & xwindow);
2133   
2134   if (type != XA_WINDOW)
2135     return FALSE;
2136
2137   gdk_error_trap_push ();
2138
2139   /* Find out if this WM goes away, so we can reset everything. */
2140   XSelectInput (screen_x11->xdisplay, *xwindow, StructureNotifyMask);
2141
2142   gdk_display_sync (screen_x11->display);
2143   
2144   if (gdk_error_trap_pop ())
2145     {
2146       XFree (xwindow);
2147       return FALSE;
2148     }
2149   
2150   XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), screen_x11->xroot_window,
2151                       gdk_x11_get_xatom_by_name_for_display (display, "_NET_SUPPORTED"),
2152                       0, G_MAXLONG, False, XA_ATOM, &type, &format, 
2153                       &supported_atoms->n_atoms, &bytes_after,
2154                       (guchar **)&supported_atoms->atoms);
2155   
2156   if (type != XA_ATOM)
2157     return FALSE;
2158   
2159   screen_x11->wmspec_check_window = *xwindow;
2160   XFree (xwindow);
2161   
2162   /* since wmspec_check_window != None this isn't infinite. ;-) */
2163   return gdk_x11_screen_supports_net_wm_hint (screen, property);
2164 }
2165
2166 /**
2167  * gdk_net_wm_supports:
2168  * @property: a property atom.
2169  * 
2170  * This function is specific to the X11 backend of GDK, and indicates
2171  * whether the window manager for the default screen supports a certain
2172  * hint from the Extended Window Manager Hints Specification. See
2173  * gdk_x11_screen_supports_net_wm_hint() for complete details.
2174  * 
2175  * Return value: %TRUE if the window manager supports @property
2176  **/
2177 gboolean
2178 gdk_net_wm_supports (GdkAtom property)
2179 {
2180   return gdk_x11_screen_supports_net_wm_hint (gdk_get_default_screen (), property);
2181 }
2182
2183 static struct
2184 {
2185   const char *xsettings_name;
2186   const char *gdk_name;
2187 } settings_map[] = {
2188   { "Net/DoubleClickTime", "gtk-double-click-time" },
2189   { "Net/DndDragThreshold", "gtk-dnd-drag-threshold" },
2190   { "Gtk/CanChangeAccels", "gtk-can-change-accels" },
2191   { "Gtk/ColorPalette", "gtk-color-palette" },
2192   { "Gtk/FontName", "gtk-font-name" },
2193   { "Gtk/KeyThemeName", "gtk-key-theme-name" },
2194   { "Gtk/ToolbarStyle", "gtk-toolbar-style" },
2195   { "Gtk/ToolbarIconSize", "gtk-toolbar-icon-size" },
2196   { "Net/CursorBlink", "gtk-cursor-blink" },
2197   { "Net/CursorBlinkTime", "gtk-cursor-blink-time" },
2198   { "Net/ThemeName", "gtk-theme-name" }
2199 };
2200
2201 static void
2202 gdk_xsettings_notify_cb (const char       *name,
2203                          XSettingsAction   action,
2204                          XSettingsSetting *setting,
2205                          void             *data)
2206 {
2207   GdkEvent new_event;
2208   GdkScreen *screen = data;
2209   int i;
2210
2211   new_event.type = GDK_SETTING;
2212   new_event.setting.window = gdk_screen_get_root_window (screen);
2213   new_event.setting.send_event = FALSE;
2214   new_event.setting.name = NULL;
2215
2216   for (i = 0; i < G_N_ELEMENTS (settings_map) ; i++)
2217     if (strcmp (settings_map[i].xsettings_name, name) == 0)
2218       {
2219         new_event.setting.name = (char *)settings_map[i].gdk_name;
2220         break;
2221       }
2222
2223   if (!new_event.setting.name)
2224     return;
2225   
2226   switch (action)
2227     {
2228     case XSETTINGS_ACTION_NEW:
2229       new_event.setting.action = GDK_SETTING_ACTION_NEW;
2230       break;
2231     case XSETTINGS_ACTION_CHANGED:
2232       new_event.setting.action = GDK_SETTING_ACTION_CHANGED;
2233       break;
2234     case XSETTINGS_ACTION_DELETED:
2235       new_event.setting.action = GDK_SETTING_ACTION_DELETED;
2236       break;
2237     }
2238
2239   gdk_event_put (&new_event);
2240 }
2241
2242 static gboolean
2243 check_transform (const gchar *xsettings_name,
2244                  GType        src_type,
2245                  GType        dest_type)
2246 {
2247   if (!g_value_type_transformable (src_type, dest_type))
2248     {
2249       g_warning ("Cannot tranform xsetting %s of type %s to type %s\n",
2250                  xsettings_name,
2251                  g_type_name (src_type),
2252                  g_type_name (dest_type));
2253       return FALSE;
2254     }
2255   else
2256     return TRUE;
2257 }
2258
2259 /**
2260  * gdk_screen_get_setting:
2261  * @screen: the #GdkScreen where the setting is located
2262  * @name: the name of the setting
2263  * @value: location to store the value of the setting
2264  *
2265  * Retrieves a desktop-wide setting such as double-click time
2266  * for the #GdkScreen @screen. 
2267  *
2268  * FIXME needs a list of valid settings here, or a link to 
2269  * more information.
2270  * 
2271  * Returns : %TRUE if the setting existed and a value was stored
2272  *   in @value, %FALSE otherwise.
2273  **/
2274 gboolean
2275 gdk_screen_get_setting (GdkScreen   *screen,
2276                         const gchar *name,
2277                         GValue      *value)
2278 {
2279
2280   const char *xsettings_name = NULL;
2281   XSettingsResult result;
2282   XSettingsSetting *setting;
2283   GdkScreenX11 *screen_x11;
2284   gboolean success = FALSE;
2285   gint i;
2286   GValue tmp_val = { 0, };
2287   
2288   g_return_val_if_fail (GDK_IS_SCREEN (screen), FALSE);
2289   
2290   screen_x11 = GDK_SCREEN_X11 (screen);
2291
2292   for (i = 0; i < G_N_ELEMENTS (settings_map) ; i++)
2293     if (strcmp (settings_map[i].gdk_name, name) == 0)
2294       {
2295         xsettings_name = settings_map[i].xsettings_name;
2296         break;
2297       }
2298
2299   if (!xsettings_name)
2300     return FALSE;
2301
2302   result = xsettings_client_get_setting (screen_x11->xsettings_client, 
2303                                          xsettings_name, &setting);
2304   if (result != XSETTINGS_SUCCESS)
2305     return FALSE;
2306
2307   switch (setting->type)
2308     {
2309     case XSETTINGS_TYPE_INT:
2310       if (check_transform (xsettings_name, G_TYPE_INT, G_VALUE_TYPE (value)))
2311         {
2312           g_value_init (&tmp_val, G_TYPE_INT);
2313           g_value_set_int (&tmp_val, setting->data.v_int);
2314           g_value_transform (&tmp_val, value);
2315
2316           success = TRUE;
2317         }
2318       break;
2319     case XSETTINGS_TYPE_STRING:
2320       if (check_transform (xsettings_name, G_TYPE_STRING, G_VALUE_TYPE (value)))
2321         {
2322           g_value_init (&tmp_val, G_TYPE_STRING);
2323           g_value_set_string (&tmp_val, setting->data.v_string);
2324           g_value_transform (&tmp_val, value);
2325
2326           success = TRUE;
2327         }
2328       break;
2329     case XSETTINGS_TYPE_COLOR:
2330       if (!check_transform (xsettings_name, GDK_TYPE_COLOR, G_VALUE_TYPE (value)))
2331         {
2332           GdkColor color;
2333           
2334           g_value_init (&tmp_val, GDK_TYPE_COLOR);
2335
2336           color.pixel = 0;
2337           color.red = setting->data.v_color.red;
2338           color.green = setting->data.v_color.green;
2339           color.blue = setting->data.v_color.blue;
2340           
2341           g_value_set_boxed (&tmp_val, &color);
2342           
2343           g_value_transform (&tmp_val, value);
2344           
2345           success = TRUE;
2346         }
2347       break;
2348     }
2349   
2350   g_value_unset (&tmp_val);
2351
2352   xsettings_setting_free (setting);
2353
2354   return success;
2355 }
2356
2357 static GdkFilterReturn 
2358 gdk_xsettings_client_event_filter (GdkXEvent *xevent,
2359                                    GdkEvent  *event,
2360                                    gpointer   data)
2361 {
2362   GdkScreenX11 *screen = data;
2363   
2364   if (xsettings_client_process_event (screen->xsettings_client, (XEvent *)xevent))
2365     return GDK_FILTER_REMOVE;
2366   else
2367     return GDK_FILTER_CONTINUE;
2368 }
2369
2370 static void 
2371 gdk_xsettings_watch_cb (Window   window,
2372                          Bool     is_start,
2373                          long     mask,
2374                          void    *cb_data)
2375 {
2376   GdkWindow *gdkwin;
2377   GdkScreen *screen = cb_data;
2378
2379   gdkwin = gdk_window_lookup_for_display (gdk_screen_get_display (screen), window);
2380   
2381   if (is_start)
2382     gdk_window_add_filter (gdkwin, gdk_xsettings_client_event_filter, screen);
2383   else
2384     gdk_window_remove_filter (gdkwin, gdk_xsettings_client_event_filter, screen);
2385 }