]> Pileus Git - ~andy/gtk/blob - gdk/x11/gdkevents-x11.c
Another attempt at getting Xrandr screen size changes right. Actually
[~andy/gtk] / gdk / x11 / gdkevents-x11.c
1 /* GDK - The GIMP Drawing Kit
2  * Copyright (C) 1995-2007 Peter Mattis, Spencer Kimball,
3  * Josh MacDonald, Ryan Lortie
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20
21 /*
22  * Modified by the GTK+ Team and others 1997-2007.  See the AUTHORS
23  * file for a list of people on the GTK+ Team.  See the ChangeLog
24  * files for a list of changes.  These files are distributed with
25  * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
26  */
27
28 #include "config.h"
29
30 #include "gdk.h"
31 #include "gdkprivate-x11.h"
32 #include "gdkinternals.h"
33 #include "gdkx.h"
34 #include "gdkscreen-x11.h"
35 #include "gdkdisplay-x11.h"
36 #include "gdkasync.h"
37
38 #include "gdkkeysyms.h"
39
40 #include "xsettings-client.h"
41
42 #include <string.h>
43
44 #include "gdkinputprivate.h"
45 #include "gdksettings.c"
46 #include "gdkalias.h"
47
48
49 #ifdef HAVE_XKB
50 #include <X11/XKBlib.h>
51 #endif
52
53 #ifdef HAVE_XSYNC
54 #include <X11/extensions/sync.h>
55 #endif
56
57 #ifdef HAVE_XFIXES
58 #include <X11/extensions/Xfixes.h>
59 #endif
60
61 #ifdef HAVE_RANDR
62 #include <X11/extensions/Xrandr.h>
63 #endif
64
65 #include <X11/Xatom.h>
66
67 typedef struct _GdkIOClosure GdkIOClosure;
68 typedef struct _GdkDisplaySource GdkDisplaySource;
69 typedef struct _GdkEventTypeX11 GdkEventTypeX11;
70
71 struct _GdkIOClosure
72 {
73   GdkInputFunction function;
74   GdkInputCondition condition;
75   GDestroyNotify notify;
76   gpointer data;
77 };
78
79 struct _GdkDisplaySource
80 {
81   GSource source;
82   
83   GdkDisplay *display;
84   GPollFD event_poll_fd;
85 };
86
87 struct _GdkEventTypeX11
88 {
89   gint base;
90   gint n_events;
91 };
92
93 /* 
94  * Private function declarations
95  */
96
97 static gint      gdk_event_apply_filters (XEvent   *xevent,
98                                           GdkEvent *event,
99                                           GList    *filters);
100 static gboolean  gdk_event_translate     (GdkDisplay *display,
101                                           GdkEvent   *event, 
102                                           XEvent     *xevent,
103                                           gboolean    return_exposes);
104
105 static gboolean gdk_event_prepare  (GSource     *source,
106                                     gint        *timeout);
107 static gboolean gdk_event_check    (GSource     *source);
108 static gboolean gdk_event_dispatch (GSource     *source,
109                                     GSourceFunc  callback,
110                                     gpointer     user_data);
111
112 static GdkFilterReturn gdk_wm_protocols_filter (GdkXEvent *xev,
113                                                 GdkEvent  *event,
114                                                 gpointer   data);
115
116 static GSource *gdk_display_source_new (GdkDisplay *display);
117 static gboolean gdk_check_xpending     (GdkDisplay *display);
118
119 static Bool gdk_xsettings_watch_cb  (Window            window,
120                                      Bool              is_start,
121                                      long              mask,
122                                      void             *cb_data);
123 static void gdk_xsettings_notify_cb (const char       *name,
124                                      XSettingsAction   action,
125                                      XSettingsSetting *setting,
126                                      void             *data);
127
128 /* Private variable declarations
129  */
130
131 static GList *display_sources;
132
133 static GSourceFuncs event_funcs = {
134   gdk_event_prepare,
135   gdk_event_check,
136   gdk_event_dispatch,
137   NULL
138 };
139
140 static GSource *
141 gdk_display_source_new (GdkDisplay *display)
142 {
143   GSource *source = g_source_new (&event_funcs, sizeof (GdkDisplaySource));
144   GdkDisplaySource *display_source = (GdkDisplaySource *)source;
145   
146   display_source->display = display;
147   
148   return source;
149 }
150
151 static gboolean
152 gdk_check_xpending (GdkDisplay *display)
153 {
154   return XPending (GDK_DISPLAY_XDISPLAY (display));
155 }
156
157 /*********************************************
158  * Functions for maintaining the event queue *
159  *********************************************/
160
161 static void
162 refcounted_grab_server (Display *xdisplay)
163 {
164   GdkDisplay *display = gdk_x11_lookup_xdisplay (xdisplay);
165
166   gdk_x11_display_grab (display);
167 }
168
169 static void
170 refcounted_ungrab_server (Display *xdisplay)
171 {
172   GdkDisplay *display = gdk_x11_lookup_xdisplay (xdisplay);
173   
174   gdk_x11_display_ungrab (display);
175 }
176
177 void
178 _gdk_x11_events_init_screen (GdkScreen *screen)
179 {
180   GdkScreenX11 *screen_x11 = GDK_SCREEN_X11 (screen);
181
182   /* Keep a flag to avoid extra notifies that we don't need
183    */
184   screen_x11->xsettings_in_init = TRUE;
185   screen_x11->xsettings_client = xsettings_client_new_with_grab_funcs (screen_x11->xdisplay,
186                                                                        screen_x11->screen_num,
187                                                                        gdk_xsettings_notify_cb,
188                                                                        gdk_xsettings_watch_cb,
189                                                                        screen,
190                                                                        refcounted_grab_server,
191                                                                        refcounted_ungrab_server);
192   screen_x11->xsettings_in_init = FALSE;
193 }
194
195 void
196 _gdk_x11_events_uninit_screen (GdkScreen *screen)
197 {
198   GdkScreenX11 *screen_x11 = GDK_SCREEN_X11 (screen);
199
200   if (screen_x11->xsettings_client)
201     {
202       xsettings_client_destroy (screen_x11->xsettings_client);
203       screen_x11->xsettings_client = NULL;
204     }
205 }
206
207 void 
208 _gdk_events_init (GdkDisplay *display)
209 {
210   GSource *source;
211   GdkDisplaySource *display_source;
212   GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
213   
214   int connection_number = ConnectionNumber (display_x11->xdisplay);
215   GDK_NOTE (MISC, g_message ("connection number: %d", connection_number));
216
217
218   source = display_x11->event_source = gdk_display_source_new (display);
219   display_source = (GdkDisplaySource*) source;
220   g_source_set_priority (source, GDK_PRIORITY_EVENTS);
221   
222   display_source->event_poll_fd.fd = connection_number;
223   display_source->event_poll_fd.events = G_IO_IN;
224   
225   g_source_add_poll (source, &display_source->event_poll_fd);
226   g_source_set_can_recurse (source, TRUE);
227   g_source_attach (source, NULL);
228
229   display_sources = g_list_prepend (display_sources,display_source);
230
231   gdk_display_add_client_message_filter (display,
232                                          gdk_atom_intern_static_string ("WM_PROTOCOLS"), 
233                                          gdk_wm_protocols_filter,   
234                                          NULL);
235 }
236
237 void
238 _gdk_events_uninit (GdkDisplay *display)
239 {
240   GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
241
242   if (display_x11->event_source)
243     {
244       display_sources = g_list_remove (display_sources,
245                                        display_x11->event_source);
246       g_source_destroy (display_x11->event_source);
247       g_source_unref (display_x11->event_source);
248       display_x11->event_source = NULL;
249     }
250 }
251
252 /**
253  * gdk_events_pending:
254  * 
255  * Checks if any events are ready to be processed for any display.
256  * 
257  * Return value:  %TRUE if any events are pending.
258  **/
259 gboolean
260 gdk_events_pending (void)
261 {
262   GList *tmp_list;
263
264   for (tmp_list = display_sources; tmp_list; tmp_list = tmp_list->next)
265     {
266       GdkDisplaySource *tmp_source = tmp_list->data;
267       GdkDisplay *display = tmp_source->display;
268       
269       if (_gdk_event_queue_find_first (display))
270         return TRUE;
271     }
272
273   for (tmp_list = display_sources; tmp_list; tmp_list = tmp_list->next)
274     {
275       GdkDisplaySource *tmp_source = tmp_list->data;
276       GdkDisplay *display = tmp_source->display;
277       
278       if (gdk_check_xpending (display))
279         return TRUE;
280     }
281   
282   return FALSE;
283 }
284
285 static Bool
286 graphics_expose_predicate (Display  *display,
287                            XEvent   *xevent,
288                            XPointer  arg)
289 {
290   if (xevent->xany.window == GDK_DRAWABLE_XID ((GdkDrawable *)arg) &&
291       (xevent->xany.type == GraphicsExpose ||
292        xevent->xany.type == NoExpose))
293     return True;
294   else
295     return False;
296 }
297
298 /**
299  * gdk_event_get_graphics_expose:
300  * @window: the #GdkWindow to wait for the events for.
301  * 
302  * Waits for a GraphicsExpose or NoExpose event from the X server.
303  * This is used in the #GtkText and #GtkCList widgets in GTK+ to make sure any
304  * GraphicsExpose events are handled before the widget is scrolled.
305  *
306  * Return value:  a #GdkEventExpose if a GraphicsExpose was received, or %NULL if a
307  * NoExpose event was received.
308  **/
309 GdkEvent*
310 gdk_event_get_graphics_expose (GdkWindow *window)
311 {
312   XEvent xevent;
313   GdkEvent *event;
314   
315   g_return_val_if_fail (window != NULL, NULL);
316   
317   XIfEvent (GDK_WINDOW_XDISPLAY (window), &xevent, 
318             graphics_expose_predicate, (XPointer) window);
319   
320   if (xevent.xany.type == GraphicsExpose)
321     {
322       event = gdk_event_new (GDK_NOTHING);
323       
324       if (gdk_event_translate (GDK_WINDOW_DISPLAY (window), event,
325                                &xevent, TRUE))
326         return event;
327       else
328         gdk_event_free (event);
329     }
330   
331   return NULL;  
332 }
333
334 static gint
335 gdk_event_apply_filters (XEvent *xevent,
336                          GdkEvent *event,
337                          GList *filters)
338 {
339   GList *tmp_list;
340   GdkFilterReturn result;
341   
342   tmp_list = filters;
343   
344   while (tmp_list)
345     {
346       GdkEventFilter *filter = (GdkEventFilter*) tmp_list->data;
347       
348       tmp_list = tmp_list->next;
349       result = filter->function (xevent, event, filter->data);
350       if (result !=  GDK_FILTER_CONTINUE)
351         return result;
352     }
353   
354   return GDK_FILTER_CONTINUE;
355 }
356
357 /**
358  * gdk_display_add_client_message_filter:
359  * @display: a #GdkDisplay for which this message filter applies
360  * @message_type: the type of ClientMessage events to receive.
361  *   This will be checked against the @message_type field 
362  *   of the XClientMessage event struct.
363  * @func: the function to call to process the event.
364  * @data: user data to pass to @func.
365  *
366  * Adds a filter to be called when X ClientMessage events are received.
367  * See gdk_window_add_filter() if you are interested in filtering other
368  * types of events.
369  *
370  * Since: 2.2
371  **/ 
372 void 
373 gdk_display_add_client_message_filter (GdkDisplay   *display,
374                                        GdkAtom       message_type,
375                                        GdkFilterFunc func,
376                                        gpointer      data)
377 {
378   GdkClientFilter *filter;
379   g_return_if_fail (GDK_IS_DISPLAY (display));
380   filter = g_new (GdkClientFilter, 1);
381
382   filter->type = message_type;
383   filter->function = func;
384   filter->data = data;
385   
386   GDK_DISPLAY_X11(display)->client_filters = 
387     g_list_append (GDK_DISPLAY_X11 (display)->client_filters,
388                    filter);
389 }
390
391 /**
392  * gdk_add_client_message_filter:
393  * @message_type: the type of ClientMessage events to receive. This will be
394  *     checked against the <structfield>message_type</structfield> field of the
395  *     XClientMessage event struct.
396  * @func: the function to call to process the event.
397  * @data: user data to pass to @func. 
398  * 
399  * Adds a filter to the default display to be called when X ClientMessage events
400  * are received. See gdk_display_add_client_message_filter().
401  **/
402 void 
403 gdk_add_client_message_filter (GdkAtom       message_type,
404                                GdkFilterFunc func,
405                                gpointer      data)
406 {
407   gdk_display_add_client_message_filter (gdk_display_get_default (),
408                                          message_type, func, data);
409 }
410
411 static void
412 do_net_wm_state_changes (GdkWindow *window)
413 {
414   GdkToplevelX11 *toplevel = _gdk_x11_window_get_toplevel (window);
415   GdkWindowState old_state;
416   
417   if (GDK_WINDOW_DESTROYED (window) ||
418       gdk_window_get_window_type (window) != GDK_WINDOW_TOPLEVEL)
419     return;
420   
421   old_state = gdk_window_get_state (window);
422
423   /* For found_sticky to remain TRUE, we have to also be on desktop
424    * 0xFFFFFFFF
425    */
426   if (old_state & GDK_WINDOW_STATE_STICKY)
427     {
428       if (!(toplevel->have_sticky && toplevel->on_all_desktops))
429         gdk_synthesize_window_state (window,
430                                      GDK_WINDOW_STATE_STICKY,
431                                      0);
432     }
433   else
434     {
435       if (toplevel->have_sticky || toplevel->on_all_desktops)
436         gdk_synthesize_window_state (window,
437                                      0,
438                                      GDK_WINDOW_STATE_STICKY);
439     }
440
441   if (old_state & GDK_WINDOW_STATE_FULLSCREEN)
442     {
443       if (!toplevel->have_fullscreen)
444         gdk_synthesize_window_state (window,
445                                      GDK_WINDOW_STATE_FULLSCREEN,
446                                      0);
447     }
448   else
449     {
450       if (toplevel->have_fullscreen)
451         gdk_synthesize_window_state (window,
452                                      0,
453                                      GDK_WINDOW_STATE_FULLSCREEN);
454     }
455   
456   /* Our "maximized" means both vertical and horizontal; if only one,
457    * we don't expose that via GDK
458    */
459   if (old_state & GDK_WINDOW_STATE_MAXIMIZED)
460     {
461       if (!(toplevel->have_maxvert && toplevel->have_maxhorz))
462         gdk_synthesize_window_state (window,
463                                      GDK_WINDOW_STATE_MAXIMIZED,
464                                      0);
465     }
466   else
467     {
468       if (toplevel->have_maxvert && toplevel->have_maxhorz)
469         gdk_synthesize_window_state (window,
470                                      0,
471                                      GDK_WINDOW_STATE_MAXIMIZED);
472     }
473 }
474
475 static void
476 gdk_check_wm_desktop_changed (GdkWindow *window)
477 {
478   GdkToplevelX11 *toplevel = _gdk_x11_window_get_toplevel (window);
479   GdkDisplay *display = GDK_WINDOW_DISPLAY (window);
480
481   Atom type;
482   gint format;
483   gulong nitems;
484   gulong bytes_after;
485   guchar *data;
486   gulong *desktop;
487
488   type = None;
489   gdk_error_trap_push ();
490   XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), 
491                       GDK_WINDOW_XID (window),
492                       gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_DESKTOP"),
493                       0, G_MAXLONG, False, XA_CARDINAL, &type, 
494                       &format, &nitems,
495                       &bytes_after, &data);
496   gdk_error_trap_pop ();
497
498   if (type != None)
499     {
500       desktop = (gulong *)data;
501       toplevel->on_all_desktops = (*desktop == 0xFFFFFFFF);
502       XFree (desktop);
503     }
504   else
505     toplevel->on_all_desktops = FALSE;
506       
507   do_net_wm_state_changes (window);
508 }
509
510 static void
511 gdk_check_wm_state_changed (GdkWindow *window)
512 {
513   GdkToplevelX11 *toplevel = _gdk_x11_window_get_toplevel (window);
514   GdkDisplay *display = GDK_WINDOW_DISPLAY (window);
515   
516   Atom type;
517   gint format;
518   gulong nitems;
519   gulong bytes_after;
520   guchar *data;
521   Atom *atoms = NULL;
522   gulong i;
523
524   gboolean had_sticky = toplevel->have_sticky;
525
526   toplevel->have_sticky = FALSE;
527   toplevel->have_maxvert = FALSE;
528   toplevel->have_maxhorz = FALSE;
529   toplevel->have_fullscreen = FALSE;
530
531   type = None;
532   gdk_error_trap_push ();
533   XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), GDK_WINDOW_XID (window),
534                       gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_STATE"),
535                       0, G_MAXLONG, False, XA_ATOM, &type, &format, &nitems,
536                       &bytes_after, &data);
537   gdk_error_trap_pop ();
538
539   if (type != None)
540     {
541       Atom sticky_atom = gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_STATE_STICKY");
542       Atom maxvert_atom = gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_STATE_MAXIMIZED_VERT");
543       Atom maxhorz_atom = gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_STATE_MAXIMIZED_HORZ");
544       Atom fullscreen_atom = gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_STATE_FULLSCREEN");
545
546       atoms = (Atom *)data;
547
548       i = 0;
549       while (i < nitems)
550         {
551           if (atoms[i] == sticky_atom)
552             toplevel->have_sticky = TRUE;
553           else if (atoms[i] == maxvert_atom)
554             toplevel->have_maxvert = TRUE;
555           else if (atoms[i] == maxhorz_atom)
556             toplevel->have_maxhorz = TRUE;
557           else if (atoms[i] == fullscreen_atom)
558             toplevel->have_fullscreen = TRUE;
559           
560           ++i;
561         }
562
563       XFree (atoms);
564     }
565
566   /* When have_sticky is turned on, we have to check the DESKTOP property
567    * as well.
568    */
569   if (toplevel->have_sticky && !had_sticky)
570     gdk_check_wm_desktop_changed (window);
571   else
572     do_net_wm_state_changes (window);
573 }
574
575 #define HAS_FOCUS(toplevel)                           \
576   ((toplevel)->has_focus || (toplevel)->has_pointer_focus)
577
578 static void
579 generate_focus_event (GdkWindow *window,
580                       gboolean   in)
581 {
582   GdkEvent event;
583   
584   event.type = GDK_FOCUS_CHANGE;
585   event.focus_change.window = window;
586   event.focus_change.send_event = FALSE;
587   event.focus_change.in = in;
588   
589   gdk_event_put (&event);
590 }
591
592 static gboolean
593 set_screen_from_root (GdkDisplay *display,
594                       GdkEvent   *event,
595                       Window      xrootwin)
596 {
597   GdkScreen *screen;
598
599   screen = _gdk_x11_display_screen_for_xrootwin (display, xrootwin);
600
601   if (screen)
602     {
603       gdk_event_set_screen (event, screen);
604
605       return TRUE;
606     }
607   
608   return FALSE;
609 }
610
611 static void
612 translate_key_event (GdkDisplay *display,
613                      GdkEvent   *event,
614                      XEvent     *xevent)
615 {
616   GdkKeymap *keymap = gdk_keymap_get_for_display (display);
617   gunichar c = 0;
618   gchar buf[7];
619
620   event->key.type = xevent->xany.type == KeyPress ? GDK_KEY_PRESS : GDK_KEY_RELEASE;
621   event->key.time = xevent->xkey.time;
622
623   event->key.state = (GdkModifierType) xevent->xkey.state;
624   event->key.group = _gdk_x11_get_group_for_state (display, xevent->xkey.state);
625   event->key.hardware_keycode = xevent->xkey.keycode;
626
627   event->key.keyval = GDK_VoidSymbol;
628
629   gdk_keymap_translate_keyboard_state (keymap,
630                                        event->key.hardware_keycode,
631                                        event->key.state,
632                                        event->key.group,
633                                        &event->key.keyval,
634                                        NULL, NULL, NULL);
635
636   _gdk_keymap_add_virtual_modifiers (keymap, &event->key.state);
637   event->key.is_modifier = _gdk_keymap_key_is_modifier (keymap, event->key.hardware_keycode);
638
639   /* Fill in event->string crudely, since various programs
640    * depend on it.
641    */
642   event->key.string = NULL;
643   
644   if (event->key.keyval != GDK_VoidSymbol)
645     c = gdk_keyval_to_unicode (event->key.keyval);
646
647   if (c)
648     {
649       gsize bytes_written;
650       gint len;
651
652       /* Apply the control key - Taken from Xlib
653        */
654       if (event->key.state & GDK_CONTROL_MASK)
655         {
656           if ((c >= '@' && c < '\177') || c == ' ') c &= 0x1F;
657           else if (c == '2')
658             {
659               event->key.string = g_memdup ("\0\0", 2);
660               event->key.length = 1;
661               buf[0] = '\0';
662               goto out;
663             }
664           else if (c >= '3' && c <= '7') c -= ('3' - '\033');
665           else if (c == '8') c = '\177';
666           else if (c == '/') c = '_' & 0x1F;
667         }
668       
669       len = g_unichar_to_utf8 (c, buf);
670       buf[len] = '\0';
671       
672       event->key.string = g_locale_from_utf8 (buf, len,
673                                               NULL, &bytes_written,
674                                               NULL);
675       if (event->key.string)
676         event->key.length = bytes_written;
677     }
678   else if (event->key.keyval == GDK_Escape)
679     {
680       event->key.length = 1;
681       event->key.string = g_strdup ("\033");
682     }
683   else if (event->key.keyval == GDK_Return ||
684           event->key.keyval == GDK_KP_Enter)
685     {
686       event->key.length = 1;
687       event->key.string = g_strdup ("\r");
688     }
689
690   if (!event->key.string)
691     {
692       event->key.length = 0;
693       event->key.string = g_strdup ("");
694     }
695   
696  out:
697 #ifdef G_ENABLE_DEBUG
698   if (_gdk_debug_flags & GDK_DEBUG_EVENTS)
699     {
700       g_message ("%s:\t\twindow: %ld     key: %12s  %d",
701                  event->type == GDK_KEY_PRESS ? "key press  " : "key release",
702                  xevent->xkey.window,
703                  event->key.keyval ? gdk_keyval_name (event->key.keyval) : "(none)",
704                  event->key.keyval);
705   
706       if (event->key.length > 0)
707         g_message ("\t\tlength: %4d string: \"%s\"",
708                    event->key.length, buf);
709     }
710 #endif /* G_ENABLE_DEBUG */  
711   return;
712 }
713
714 /**
715  * gdk_x11_register_standard_event_type:
716  * @display: a #GdkDisplay
717  * @event_base: first event type code to register
718  * @n_events: number of event type codes to register
719  * 
720  * Registers interest in receiving extension events with type codes
721  * between @event_base and <literal>event_base + n_events - 1</literal>.
722  * The registered events must have the window field in the same place
723  * as core X events (this is not the case for e.g. XKB extension events).
724  *
725  * If an event type is registered, events of this type will go through
726  * global and window-specific filters (see gdk_window_add_filter()). 
727  * Unregistered events will only go through global filters.
728  * GDK may register the events of some X extensions on its own.
729  * 
730  * This function should only be needed in unusual circumstances, e.g.
731  * when filtering XInput extension events on the root window.
732  *
733  * Since: 2.4
734  **/
735 void
736 gdk_x11_register_standard_event_type (GdkDisplay          *display,
737                                       gint                 event_base,
738                                       gint                 n_events)
739 {
740   GdkEventTypeX11 *event_type;
741   GdkDisplayX11 *display_x11;
742
743   display_x11 = GDK_DISPLAY_X11 (display);
744   event_type = g_new (GdkEventTypeX11, 1);
745
746   event_type->base = event_base;
747   event_type->n_events = n_events;
748
749   display_x11->event_types = g_slist_prepend (display_x11->event_types, event_type);
750 }
751
752 /* Return the window this has to do with, if any, rather
753  * than the frame or root window that was selecting
754  * for substructure
755  */
756 static void
757 get_real_window (GdkDisplay *display,
758                  XEvent     *event,
759                  Window     *event_window,
760                  Window     *filter_window)
761 {
762   /* Core events all have an event->xany.window field, but that's
763    * not true for extension events
764    */
765   if (event->type >= KeyPress &&
766       event->type <= MappingNotify)
767     {
768       *filter_window = event->xany.window;
769       switch (event->type)
770         {      
771         case CreateNotify:
772           *event_window = event->xcreatewindow.window;
773           break;
774         case DestroyNotify:
775           *event_window = event->xdestroywindow.window;
776           break;
777         case UnmapNotify:
778           *event_window = event->xunmap.window;
779           break;
780         case MapNotify:
781           *event_window = event->xmap.window;
782           break;
783         case MapRequest:
784           *event_window = event->xmaprequest.window;
785           break;
786         case ReparentNotify:
787           *event_window = event->xreparent.window;
788           break;
789         case ConfigureNotify:
790           *event_window = event->xconfigure.window;
791           break;
792         case ConfigureRequest:
793           *event_window = event->xconfigurerequest.window;
794           break;
795         case GravityNotify:
796           *event_window = event->xgravity.window;
797           break;
798         case CirculateNotify:
799           *event_window = event->xcirculate.window;
800           break;
801         case CirculateRequest:
802           *event_window = event->xcirculaterequest.window;
803           break;
804         default:
805           *event_window = event->xany.window;
806         }
807     }
808   else
809     {
810       GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
811       GSList *tmp_list;
812
813       for (tmp_list = display_x11->event_types;
814            tmp_list;
815            tmp_list = tmp_list->next)
816         {
817           GdkEventTypeX11 *event_type = tmp_list->data;
818
819           if (event->type >= event_type->base &&
820               event->type < event_type->base + event_type->n_events)
821             {
822               *event_window = event->xany.window;
823               *filter_window = event->xany.window;
824               return;
825             }
826         }
827            
828       *event_window = None;
829       *filter_window = None;
830     }
831 }
832
833 #ifdef G_ENABLE_DEBUG
834 static const char notify_modes[][19] = {
835   "NotifyNormal",
836   "NotifyGrab",
837   "NotifyUngrab",
838   "NotifyWhileGrabbed"
839 };
840
841 static const char notify_details[][23] = {
842   "NotifyAncestor",
843   "NotifyVirtual",
844   "NotifyInferior",
845   "NotifyNonlinear",
846   "NotifyNonlinearVirtual",
847   "NotifyPointer",
848   "NotifyPointerRoot",
849   "NotifyDetailNone"
850 };
851 #endif
852
853 static void
854 set_user_time (GdkWindow *window,
855                GdkEvent  *event)
856 {
857   g_return_if_fail (event != NULL);
858
859   window = gdk_window_get_toplevel (event->client.window);
860   g_return_if_fail (GDK_IS_WINDOW (window));
861
862   /* If an event doesn't have a valid timestamp, we shouldn't use it
863    * to update the latest user interaction time.
864    */
865   if (gdk_event_get_time (event) != GDK_CURRENT_TIME)
866     gdk_x11_window_set_user_time (gdk_window_get_toplevel (window),
867                                   gdk_event_get_time (event));
868 }
869
870 static gboolean
871 gdk_event_translate (GdkDisplay *display,
872                      GdkEvent   *event,
873                      XEvent     *xevent,
874                      gboolean    return_exposes)
875 {
876   
877   GdkWindow *window;
878   GdkWindowObject *window_private;
879   GdkWindow *filter_window;
880   GdkWindowImplX11 *window_impl = NULL;
881   gboolean return_val;
882   gint xoffset, yoffset;
883   GdkScreen *screen = NULL;
884   GdkScreenX11 *screen_x11 = NULL;
885   GdkToplevelX11 *toplevel = NULL;
886   GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
887   Window xwindow, filter_xwindow;
888   
889   return_val = FALSE;
890
891   /* init these, since the done: block uses them */
892   window = NULL;
893   window_private = NULL;
894   event->any.window = NULL;
895
896   if (_gdk_default_filters)
897     {
898       /* Apply global filters */
899       GdkFilterReturn result;
900       result = gdk_event_apply_filters (xevent, event,
901                                         _gdk_default_filters);
902       
903       if (result != GDK_FILTER_CONTINUE)
904         {
905           return_val = (result == GDK_FILTER_TRANSLATE) ? TRUE : FALSE;
906           goto done;
907         }
908     }  
909
910   /* Find the GdkWindow that this event relates to.
911    * Basically this means substructure events
912    * are reported same as structure events
913    */
914   get_real_window (display, xevent, &xwindow, &filter_xwindow);
915   
916   window = gdk_window_lookup_for_display (display, xwindow);
917   /* We may receive events such as NoExpose/GraphicsExpose
918    * and ShmCompletion for pixmaps
919    */
920   if (window && !GDK_IS_WINDOW (window))
921     window = NULL;
922   window_private = (GdkWindowObject *) window;
923
924   /* We always run the filters for the window where the event
925    * is delivered, not the window that it relates to
926    */
927   if (filter_xwindow == xwindow)
928     filter_window = window;
929   else
930     {
931       filter_window = gdk_window_lookup_for_display (display, filter_xwindow);
932       if (filter_window && !GDK_IS_WINDOW (filter_window))
933         filter_window = NULL;
934     }
935
936   if (window)
937     {
938       screen = GDK_WINDOW_SCREEN (window);
939       screen_x11 = GDK_SCREEN_X11 (screen);
940       toplevel = _gdk_x11_window_get_toplevel (window);
941     }
942     
943   if (window != NULL)
944     {
945       window_impl = GDK_WINDOW_IMPL_X11 (window_private->impl);
946       
947       /* Move key events on focus window to the real toplevel, and
948        * filter out all other events on focus window
949        */          
950       if (toplevel && xwindow == toplevel->focus_window)
951         {
952           switch (xevent->type)
953             {
954             case KeyPress:
955             case KeyRelease:
956               xwindow = GDK_WINDOW_XID (window);
957               xevent->xany.window = xwindow;
958               break;
959             default:
960               return FALSE;
961             }
962         }
963
964       g_object_ref (window);
965     }
966
967   event->any.window = window;
968   event->any.send_event = xevent->xany.send_event ? TRUE : FALSE;
969   
970   if (window_private && GDK_WINDOW_DESTROYED (window))
971     {
972       if (xevent->type != DestroyNotify)
973         {
974           return_val = FALSE;
975           goto done;
976         }
977     }
978   else if (filter_window)
979     {
980       /* Apply per-window filters */
981       GdkWindowObject *filter_private = (GdkWindowObject *) filter_window;
982       GdkFilterReturn result;
983
984       if (filter_private->filters)
985         {
986           g_object_ref (filter_window);
987           
988           result = gdk_event_apply_filters (xevent, event,
989                                             filter_private->filters);
990           
991           g_object_unref (filter_window);
992       
993           if (result != GDK_FILTER_CONTINUE)
994             {
995               return_val = (result == GDK_FILTER_TRANSLATE) ? TRUE : FALSE;
996               goto done;
997             }
998         }
999     }
1000       
1001   if (screen_x11 && screen_x11->wmspec_check_window != None &&
1002       xwindow == screen_x11->wmspec_check_window)
1003     {
1004       if (xevent->type == DestroyNotify)
1005         {
1006           screen_x11->wmspec_check_window = None;
1007           g_free (screen_x11->window_manager_name);
1008           screen_x11->window_manager_name = g_strdup ("unknown");
1009
1010           /* careful, reentrancy */
1011           _gdk_x11_screen_window_manager_changed (GDK_SCREEN (screen_x11));
1012         }
1013       
1014       /* Eat events on this window unless someone had wrapped
1015        * it as a foreign window
1016        */
1017       if (window == NULL)
1018         {
1019           return_val = FALSE;
1020           goto done;
1021         }
1022     }
1023
1024   if (window &&
1025       (xevent->xany.type == MotionNotify ||
1026        xevent->xany.type == ButtonRelease))
1027     {
1028       if (_gdk_moveresize_handle_event (xevent))
1029         {
1030           return_val = FALSE;
1031           goto done;
1032         }
1033     }
1034   
1035   /* We do a "manual" conversion of the XEvent to a
1036    *  GdkEvent. The structures are mostly the same so
1037    *  the conversion is fairly straightforward. We also
1038    *  optionally print debugging info regarding events
1039    *  received.
1040    */
1041
1042   return_val = TRUE;
1043
1044   if (window)
1045     {
1046       _gdk_x11_window_get_offsets (window, &xoffset, &yoffset);
1047     }
1048   else
1049     {
1050       xoffset = 0;
1051       yoffset = 0;
1052     }
1053
1054   switch (xevent->type)
1055     {
1056     case KeyPress:
1057       if (window_private == NULL)
1058         {
1059           return_val = FALSE;
1060           break;
1061         }
1062       translate_key_event (display, event, xevent);
1063       set_user_time (window, event);
1064       break;
1065
1066     case KeyRelease:
1067       if (window_private == NULL)
1068         {
1069           return_val = FALSE;
1070           break;
1071         }
1072       
1073       /* Emulate detectable auto-repeat by checking to see
1074        * if the next event is a key press with the same
1075        * keycode and timestamp, and if so, ignoring the event.
1076        */
1077
1078       if (!display_x11->have_xkb_autorepeat && XPending (xevent->xkey.display))
1079         {
1080           XEvent next_event;
1081
1082           XPeekEvent (xevent->xkey.display, &next_event);
1083
1084           if (next_event.type == KeyPress &&
1085               next_event.xkey.keycode == xevent->xkey.keycode &&
1086               next_event.xkey.time == xevent->xkey.time)
1087             {
1088               return_val = FALSE;
1089               break;
1090             }
1091         }
1092
1093       translate_key_event (display, event, xevent);
1094       break;
1095       
1096     case ButtonPress:
1097       GDK_NOTE (EVENTS, 
1098                 g_message ("button press:\t\twindow: %ld  x,y: %d %d  button: %d",
1099                            xevent->xbutton.window,
1100                            xevent->xbutton.x, xevent->xbutton.y,
1101                            xevent->xbutton.button));
1102       
1103       if (window_private == NULL || 
1104           ((window_private->extension_events != 0) &&
1105            display_x11->input_ignore_core))
1106         {
1107           return_val = FALSE;
1108           break;
1109         }
1110       
1111       /* If we get a ButtonPress event where the button is 4 or 5,
1112          it's a Scroll event */
1113       switch (xevent->xbutton.button)
1114         {
1115         case 4: /* up */
1116         case 5: /* down */
1117         case 6: /* left */
1118         case 7: /* right */
1119           event->scroll.type = GDK_SCROLL;
1120
1121           if (xevent->xbutton.button == 4)
1122             event->scroll.direction = GDK_SCROLL_UP;
1123           else if (xevent->xbutton.button == 5)
1124             event->scroll.direction = GDK_SCROLL_DOWN;
1125           else if (xevent->xbutton.button == 6)
1126             event->scroll.direction = GDK_SCROLL_LEFT;
1127           else
1128             event->scroll.direction = GDK_SCROLL_RIGHT;
1129
1130           event->scroll.window = window;
1131           event->scroll.time = xevent->xbutton.time;
1132           event->scroll.x = xevent->xbutton.x + xoffset;
1133           event->scroll.y = xevent->xbutton.y + yoffset;
1134           event->scroll.x_root = (gfloat)xevent->xbutton.x_root;
1135           event->scroll.y_root = (gfloat)xevent->xbutton.y_root;
1136           event->scroll.state = (GdkModifierType) xevent->xbutton.state;
1137           event->scroll.device = display->core_pointer;
1138           
1139           if (!set_screen_from_root (display, event, xevent->xbutton.root))
1140             {
1141               return_val = FALSE;
1142               break;
1143             }
1144           
1145           break;
1146           
1147         default:
1148           event->button.type = GDK_BUTTON_PRESS;
1149           event->button.window = window;
1150           event->button.time = xevent->xbutton.time;
1151           event->button.x = xevent->xbutton.x + xoffset;
1152           event->button.y = xevent->xbutton.y + yoffset;
1153           event->button.x_root = (gfloat)xevent->xbutton.x_root;
1154           event->button.y_root = (gfloat)xevent->xbutton.y_root;
1155           event->button.axes = NULL;
1156           event->button.state = (GdkModifierType) xevent->xbutton.state;
1157           event->button.button = xevent->xbutton.button;
1158           event->button.device = display->core_pointer;
1159           
1160           if (!set_screen_from_root (display, event, xevent->xbutton.root))
1161             {
1162               return_val = FALSE;
1163               break;
1164             }
1165
1166           _gdk_event_button_generate (display, event);
1167           break;
1168         }
1169
1170       set_user_time (window, event);
1171
1172       _gdk_xgrab_check_button_event (window, xevent);
1173       break;
1174       
1175     case ButtonRelease:
1176       GDK_NOTE (EVENTS, 
1177                 g_message ("button release:\twindow: %ld  x,y: %d %d  button: %d",
1178                            xevent->xbutton.window,
1179                            xevent->xbutton.x, xevent->xbutton.y,
1180                            xevent->xbutton.button));
1181       
1182       if (window_private == NULL ||
1183           ((window_private->extension_events != 0) &&
1184            display_x11->input_ignore_core))
1185         {
1186           return_val = FALSE;
1187           break;
1188         }
1189       
1190       /* We treat button presses as scroll wheel events, so ignore the release */
1191       if (xevent->xbutton.button == 4 || xevent->xbutton.button == 5 ||
1192           xevent->xbutton.button == 6 || xevent->xbutton.button ==7)
1193         {
1194           return_val = FALSE;
1195           break;
1196         }
1197
1198       event->button.type = GDK_BUTTON_RELEASE;
1199       event->button.window = window;
1200       event->button.time = xevent->xbutton.time;
1201       event->button.x = xevent->xbutton.x + xoffset;
1202       event->button.y = xevent->xbutton.y + yoffset;
1203       event->button.x_root = (gfloat)xevent->xbutton.x_root;
1204       event->button.y_root = (gfloat)xevent->xbutton.y_root;
1205       event->button.axes = NULL;
1206       event->button.state = (GdkModifierType) xevent->xbutton.state;
1207       event->button.button = xevent->xbutton.button;
1208       event->button.device = display->core_pointer;
1209
1210       if (!set_screen_from_root (display, event, xevent->xbutton.root))
1211         {
1212           return_val = FALSE;
1213           break;
1214         }
1215
1216       _gdk_xgrab_check_button_event (window, xevent);
1217       break;
1218       
1219     case MotionNotify:
1220       GDK_NOTE (EVENTS,
1221                 g_message ("motion notify:\t\twindow: %ld  x,y: %d %d  hint: %s", 
1222                            xevent->xmotion.window,
1223                            xevent->xmotion.x, xevent->xmotion.y,
1224                            (xevent->xmotion.is_hint) ? "true" : "false"));
1225       
1226       if (window_private == NULL ||
1227           ((window_private->extension_events != 0) &&
1228            display_x11->input_ignore_core))
1229         {
1230           return_val = FALSE;
1231           break;
1232         }
1233
1234       event->motion.type = GDK_MOTION_NOTIFY;
1235       event->motion.window = window;
1236       event->motion.time = xevent->xmotion.time;
1237       event->motion.x = xevent->xmotion.x + xoffset;
1238       event->motion.y = xevent->xmotion.y + yoffset;
1239       event->motion.x_root = (gfloat)xevent->xmotion.x_root;
1240       event->motion.y_root = (gfloat)xevent->xmotion.y_root;
1241       event->motion.axes = NULL;
1242       event->motion.state = (GdkModifierType) xevent->xmotion.state;
1243       event->motion.is_hint = xevent->xmotion.is_hint;
1244       event->motion.device = display->core_pointer;
1245       
1246       if (!set_screen_from_root (display, event, xevent->xbutton.root))
1247         {
1248           return_val = FALSE;
1249           break;
1250         }
1251             
1252       break;
1253       
1254     case EnterNotify:
1255       GDK_NOTE (EVENTS,
1256                 g_message ("enter notify:\t\twindow: %ld  detail: %d subwin: %ld",
1257                            xevent->xcrossing.window,
1258                            xevent->xcrossing.detail,
1259                            xevent->xcrossing.subwindow));
1260  
1261       if (window_private == NULL)
1262         {
1263           return_val = FALSE;
1264           break;
1265         }
1266       
1267       if (!set_screen_from_root (display, event, xevent->xbutton.root))
1268         {
1269           return_val = FALSE;
1270           break;
1271         }
1272       
1273       /* Handle focusing (in the case where no window manager is running */
1274       if (toplevel && xevent->xcrossing.detail != NotifyInferior)
1275         {
1276           toplevel->has_pointer = TRUE;
1277
1278           if (xevent->xcrossing.focus && !toplevel->has_focus_window)
1279             {
1280               gboolean had_focus = HAS_FOCUS (toplevel);
1281               
1282               toplevel->has_pointer_focus = TRUE;
1283               
1284               if (HAS_FOCUS (toplevel) != had_focus)
1285                 generate_focus_event (window, TRUE);
1286             }
1287         }
1288
1289       /* Tell XInput stuff about it if appropriate */
1290       if (window_private &&
1291           !GDK_WINDOW_DESTROYED (window) &&
1292           window_private->extension_events != 0)
1293         _gdk_input_enter_event (&xevent->xcrossing, window);
1294       
1295       event->crossing.type = GDK_ENTER_NOTIFY;
1296       event->crossing.window = window;
1297       
1298       /* If the subwindow field of the XEvent is non-NULL, then
1299        *  lookup the corresponding GdkWindow.
1300        */
1301       if (xevent->xcrossing.subwindow != None)
1302         event->crossing.subwindow = gdk_window_lookup_for_display (display, xevent->xcrossing.subwindow);
1303       else
1304         event->crossing.subwindow = NULL;
1305       
1306       event->crossing.time = xevent->xcrossing.time;
1307       event->crossing.x = xevent->xcrossing.x + xoffset;
1308       event->crossing.y = xevent->xcrossing.y + yoffset;
1309       event->crossing.x_root = xevent->xcrossing.x_root;
1310       event->crossing.y_root = xevent->xcrossing.y_root;
1311       
1312       /* Translate the crossing mode into Gdk terms.
1313        */
1314       switch (xevent->xcrossing.mode)
1315         {
1316         case NotifyNormal:
1317           event->crossing.mode = GDK_CROSSING_NORMAL;
1318           break;
1319         case NotifyGrab:
1320           event->crossing.mode = GDK_CROSSING_GRAB;
1321           break;
1322         case NotifyUngrab:
1323           event->crossing.mode = GDK_CROSSING_UNGRAB;
1324           break;
1325         };
1326       
1327       /* Translate the crossing detail into Gdk terms.
1328        */
1329       switch (xevent->xcrossing.detail)
1330         {
1331         case NotifyInferior:
1332           event->crossing.detail = GDK_NOTIFY_INFERIOR;
1333           break;
1334         case NotifyAncestor:
1335           event->crossing.detail = GDK_NOTIFY_ANCESTOR;
1336           break;
1337         case NotifyVirtual:
1338           event->crossing.detail = GDK_NOTIFY_VIRTUAL;
1339           break;
1340         case NotifyNonlinear:
1341           event->crossing.detail = GDK_NOTIFY_NONLINEAR;
1342           break;
1343         case NotifyNonlinearVirtual:
1344           event->crossing.detail = GDK_NOTIFY_NONLINEAR_VIRTUAL;
1345           break;
1346         default:
1347           event->crossing.detail = GDK_NOTIFY_UNKNOWN;
1348           break;
1349         }
1350       
1351       event->crossing.focus = xevent->xcrossing.focus;
1352       event->crossing.state = xevent->xcrossing.state;
1353   
1354       break;
1355       
1356     case LeaveNotify:
1357       GDK_NOTE (EVENTS, 
1358                 g_message ("leave notify:\t\twindow: %ld  detail: %d subwin: %ld",
1359                            xevent->xcrossing.window,
1360                            xevent->xcrossing.detail, xevent->xcrossing.subwindow));
1361
1362       if (window_private == NULL)
1363         {
1364           return_val = FALSE;
1365           break;
1366         }
1367
1368       if (!set_screen_from_root (display, event, xevent->xbutton.root))
1369         {
1370           return_val = FALSE;
1371           break;
1372         }
1373                   
1374       /* Handle focusing (in the case where no window manager is running */
1375       if (toplevel && xevent->xcrossing.detail != NotifyInferior)
1376         {
1377           toplevel->has_pointer = FALSE;
1378
1379           if (xevent->xcrossing.focus && !toplevel->has_focus_window)
1380             {
1381               gboolean had_focus = HAS_FOCUS (toplevel);
1382               
1383               toplevel->has_pointer_focus = FALSE;
1384               
1385               if (HAS_FOCUS (toplevel) != had_focus)
1386                 generate_focus_event (window, FALSE);
1387             }
1388         }
1389
1390       event->crossing.type = GDK_LEAVE_NOTIFY;
1391       event->crossing.window = window;
1392       
1393       /* If the subwindow field of the XEvent is non-NULL, then
1394        *  lookup the corresponding GdkWindow.
1395        */
1396       if (xevent->xcrossing.subwindow != None)
1397         event->crossing.subwindow = gdk_window_lookup_for_display (display, xevent->xcrossing.subwindow);
1398       else
1399         event->crossing.subwindow = NULL;
1400       
1401       event->crossing.time = xevent->xcrossing.time;
1402       event->crossing.x = xevent->xcrossing.x + xoffset;
1403       event->crossing.y = xevent->xcrossing.y + yoffset;
1404       event->crossing.x_root = xevent->xcrossing.x_root;
1405       event->crossing.y_root = xevent->xcrossing.y_root;
1406       
1407       /* Translate the crossing mode into Gdk terms.
1408        */
1409       switch (xevent->xcrossing.mode)
1410         {
1411         case NotifyNormal:
1412           event->crossing.mode = GDK_CROSSING_NORMAL;
1413           break;
1414         case NotifyGrab:
1415           event->crossing.mode = GDK_CROSSING_GRAB;
1416           break;
1417         case NotifyUngrab:
1418           event->crossing.mode = GDK_CROSSING_UNGRAB;
1419           break;
1420         };
1421       
1422       /* Translate the crossing detail into Gdk terms.
1423        */
1424       switch (xevent->xcrossing.detail)
1425         {
1426         case NotifyInferior:
1427           event->crossing.detail = GDK_NOTIFY_INFERIOR;
1428           break;
1429         case NotifyAncestor:
1430           event->crossing.detail = GDK_NOTIFY_ANCESTOR;
1431           break;
1432         case NotifyVirtual:
1433           event->crossing.detail = GDK_NOTIFY_VIRTUAL;
1434           break;
1435         case NotifyNonlinear:
1436           event->crossing.detail = GDK_NOTIFY_NONLINEAR;
1437           break;
1438         case NotifyNonlinearVirtual:
1439           event->crossing.detail = GDK_NOTIFY_NONLINEAR_VIRTUAL;
1440           break;
1441         default:
1442           event->crossing.detail = GDK_NOTIFY_UNKNOWN;
1443           break;
1444         }
1445       
1446       event->crossing.focus = xevent->xcrossing.focus;
1447       event->crossing.state = xevent->xcrossing.state;
1448       
1449       break;
1450       
1451       /* We only care about focus events that indicate that _this_
1452        * window (not a ancestor or child) got or lost the focus
1453        */
1454     case FocusIn:
1455       GDK_NOTE (EVENTS,
1456                 g_message ("focus in:\t\twindow: %ld, detail: %s, mode: %s",
1457                            xevent->xfocus.window,
1458                            notify_details[xevent->xfocus.detail],
1459                            notify_modes[xevent->xfocus.mode]));
1460       
1461       if (toplevel)
1462         {
1463           gboolean had_focus = HAS_FOCUS (toplevel);
1464           
1465           switch (xevent->xfocus.detail)
1466             {
1467             case NotifyAncestor:
1468             case NotifyVirtual:
1469               /* When the focus moves from an ancestor of the window to
1470                * the window or a descendent of the window, *and* the
1471                * pointer is inside the window, then we were previously
1472                * receiving keystroke events in the has_pointer_focus
1473                * case and are now receiving them in the
1474                * has_focus_window case.
1475                */
1476               if (toplevel->has_pointer &&
1477                   xevent->xfocus.mode != NotifyGrab &&
1478                   xevent->xfocus.mode != NotifyUngrab)
1479                 toplevel->has_pointer_focus = FALSE;
1480               
1481               /* fall through */
1482             case NotifyNonlinear:
1483             case NotifyNonlinearVirtual:
1484               if (xevent->xfocus.mode != NotifyGrab &&
1485                   xevent->xfocus.mode != NotifyUngrab)
1486                 toplevel->has_focus_window = TRUE;
1487               /* We pretend that the focus moves to the grab
1488                * window, so we pay attention to NotifyGrab
1489                * NotifyUngrab, and ignore NotifyWhileGrabbed
1490                */
1491               if (xevent->xfocus.mode != NotifyWhileGrabbed)
1492                 toplevel->has_focus = TRUE;
1493               break;
1494             case NotifyPointer:
1495               /* The X server sends NotifyPointer/NotifyGrab,
1496                * but the pointer focus is ignored while a
1497                * grab is in effect
1498                */
1499               if (xevent->xfocus.mode != NotifyGrab &&
1500                   xevent->xfocus.mode != NotifyUngrab)
1501                 toplevel->has_pointer_focus = TRUE;
1502               break;
1503             case NotifyInferior:
1504             case NotifyPointerRoot:
1505             case NotifyDetailNone:
1506               break;
1507             }
1508
1509           if (HAS_FOCUS (toplevel) != had_focus)
1510             generate_focus_event (window, TRUE);
1511         }
1512       break;
1513     case FocusOut:
1514       GDK_NOTE (EVENTS,
1515                 g_message ("focus out:\t\twindow: %ld, detail: %s, mode: %s",
1516                            xevent->xfocus.window,
1517                            notify_details[xevent->xfocus.detail],
1518                            notify_modes[xevent->xfocus.mode]));
1519       
1520       if (toplevel)
1521         {
1522           gboolean had_focus = HAS_FOCUS (toplevel);
1523             
1524           switch (xevent->xfocus.detail)
1525             {
1526             case NotifyAncestor:
1527             case NotifyVirtual:
1528               /* When the focus moves from the window or a descendent
1529                * of the window to an ancestor of the window, *and* the
1530                * pointer is inside the window, then we were previously
1531                * receiving keystroke events in the has_focus_window
1532                * case and are now receiving them in the
1533                * has_pointer_focus case.
1534                */
1535               if (toplevel->has_pointer &&
1536                   xevent->xfocus.mode != NotifyGrab &&
1537                   xevent->xfocus.mode != NotifyUngrab)
1538                 toplevel->has_pointer_focus = TRUE;
1539
1540               /* fall through */
1541             case NotifyNonlinear:
1542             case NotifyNonlinearVirtual:
1543               if (xevent->xfocus.mode != NotifyGrab &&
1544                   xevent->xfocus.mode != NotifyUngrab)
1545                 toplevel->has_focus_window = FALSE;
1546               if (xevent->xfocus.mode != NotifyWhileGrabbed)
1547                 toplevel->has_focus = FALSE;
1548               break;
1549             case NotifyPointer:
1550               if (xevent->xfocus.mode != NotifyGrab &&
1551                   xevent->xfocus.mode != NotifyUngrab)
1552                 toplevel->has_pointer_focus = FALSE;
1553             break;
1554             case NotifyInferior:
1555             case NotifyPointerRoot:
1556             case NotifyDetailNone:
1557               break;
1558             }
1559
1560           if (HAS_FOCUS (toplevel) != had_focus)
1561             generate_focus_event (window, FALSE);
1562         }
1563       break;
1564
1565 #if 0      
1566           /* gdk_keyboard_grab() causes following events. These events confuse
1567            * the XIM focus, so ignore them.
1568            */
1569           if (xevent->xfocus.mode == NotifyGrab ||
1570               xevent->xfocus.mode == NotifyUngrab)
1571             break;
1572 #endif
1573
1574     case KeymapNotify:
1575       GDK_NOTE (EVENTS,
1576                 g_message ("keymap notify"));
1577
1578       /* Not currently handled */
1579       return_val = FALSE;
1580       break;
1581       
1582     case Expose:
1583       GDK_NOTE (EVENTS,
1584                 g_message ("expose:\t\twindow: %ld  %d  x,y: %d %d  w,h: %d %d%s",
1585                            xevent->xexpose.window, xevent->xexpose.count,
1586                            xevent->xexpose.x, xevent->xexpose.y,
1587                            xevent->xexpose.width, xevent->xexpose.height,
1588                            event->any.send_event ? " (send)" : ""));
1589       
1590       if (window_private == NULL)
1591         {
1592           return_val = FALSE;
1593           break;
1594         }
1595       
1596       {
1597         GdkRectangle expose_rect;
1598
1599         expose_rect.x = xevent->xexpose.x + xoffset;
1600         expose_rect.y = xevent->xexpose.y + yoffset;
1601         expose_rect.width = xevent->xexpose.width;
1602         expose_rect.height = xevent->xexpose.height;
1603
1604         if (return_exposes)
1605           {
1606             event->expose.type = GDK_EXPOSE;
1607             event->expose.area = expose_rect;
1608             event->expose.region = gdk_region_rectangle (&expose_rect);
1609             event->expose.window = window;
1610             event->expose.count = xevent->xexpose.count;
1611
1612             return_val = TRUE;
1613           }
1614         else
1615           {
1616             _gdk_window_process_expose (window, xevent->xexpose.serial, &expose_rect);
1617             return_val = FALSE;
1618           }
1619         
1620         return_val = FALSE;
1621       }
1622         
1623       break;
1624       
1625     case GraphicsExpose:
1626       {
1627         GdkRectangle expose_rect;
1628
1629         GDK_NOTE (EVENTS,
1630                   g_message ("graphics expose:\tdrawable: %ld",
1631                              xevent->xgraphicsexpose.drawable));
1632  
1633         if (window_private == NULL)
1634           {
1635             return_val = FALSE;
1636             break;
1637           }
1638         
1639         expose_rect.x = xevent->xgraphicsexpose.x + xoffset;
1640         expose_rect.y = xevent->xgraphicsexpose.y + yoffset;
1641         expose_rect.width = xevent->xgraphicsexpose.width;
1642         expose_rect.height = xevent->xgraphicsexpose.height;
1643             
1644         if (return_exposes)
1645           {
1646             event->expose.type = GDK_EXPOSE;
1647             event->expose.area = expose_rect;
1648             event->expose.region = gdk_region_rectangle (&expose_rect);
1649             event->expose.window = window;
1650             event->expose.count = xevent->xgraphicsexpose.count;
1651
1652             return_val = TRUE;
1653           }
1654         else
1655           {
1656             _gdk_window_process_expose (window, xevent->xgraphicsexpose.serial, &expose_rect);
1657             
1658             return_val = FALSE;
1659           }
1660         
1661       }
1662       break;
1663       
1664     case NoExpose:
1665       GDK_NOTE (EVENTS,
1666                 g_message ("no expose:\t\tdrawable: %ld",
1667                            xevent->xnoexpose.drawable));
1668       
1669       event->no_expose.type = GDK_NO_EXPOSE;
1670       event->no_expose.window = window;
1671       
1672       break;
1673       
1674     case VisibilityNotify:
1675 #ifdef G_ENABLE_DEBUG
1676       if (_gdk_debug_flags & GDK_DEBUG_EVENTS)
1677         switch (xevent->xvisibility.state)
1678           {
1679           case VisibilityFullyObscured:
1680             g_message ("visibility notify:\twindow: %ld  none",
1681                        xevent->xvisibility.window);
1682             break;
1683           case VisibilityPartiallyObscured:
1684             g_message ("visibility notify:\twindow: %ld  partial",
1685                        xevent->xvisibility.window);
1686             break;
1687           case VisibilityUnobscured:
1688             g_message ("visibility notify:\twindow: %ld  full",
1689                        xevent->xvisibility.window);
1690             break;
1691           }
1692 #endif /* G_ENABLE_DEBUG */
1693       
1694       if (window_private == NULL)
1695         {
1696           return_val = FALSE;
1697           break;
1698         }
1699       
1700       event->visibility.type = GDK_VISIBILITY_NOTIFY;
1701       event->visibility.window = window;
1702       
1703       switch (xevent->xvisibility.state)
1704         {
1705         case VisibilityFullyObscured:
1706           event->visibility.state = GDK_VISIBILITY_FULLY_OBSCURED;
1707           break;
1708           
1709         case VisibilityPartiallyObscured:
1710           event->visibility.state = GDK_VISIBILITY_PARTIAL;
1711           break;
1712           
1713         case VisibilityUnobscured:
1714           event->visibility.state = GDK_VISIBILITY_UNOBSCURED;
1715           break;
1716         }
1717       
1718       break;
1719       
1720     case CreateNotify:
1721       GDK_NOTE (EVENTS,
1722                 g_message ("create notify:\twindow: %ld  x,y: %d %d     w,h: %d %d  b-w: %d  parent: %ld         ovr: %d",
1723                            xevent->xcreatewindow.window,
1724                            xevent->xcreatewindow.x,
1725                            xevent->xcreatewindow.y,
1726                            xevent->xcreatewindow.width,
1727                            xevent->xcreatewindow.height,
1728                            xevent->xcreatewindow.border_width,
1729                            xevent->xcreatewindow.parent,
1730                            xevent->xcreatewindow.override_redirect));
1731       /* not really handled */
1732       break;
1733       
1734     case DestroyNotify:
1735       GDK_NOTE (EVENTS,
1736                 g_message ("destroy notify:\twindow: %ld",
1737                            xevent->xdestroywindow.window));
1738
1739       /* Ignore DestroyNotify from SubstructureNotifyMask */
1740       if (xevent->xdestroywindow.window == xevent->xdestroywindow.event)
1741         {
1742           event->any.type = GDK_DESTROY;
1743           event->any.window = window;
1744           
1745           return_val = window_private && !GDK_WINDOW_DESTROYED (window);
1746           
1747           if (window && GDK_WINDOW_XID (window) != screen_x11->xroot_window)
1748             gdk_window_destroy_notify (window);
1749         }
1750       else
1751         return_val = FALSE;
1752       
1753       break;
1754       
1755     case UnmapNotify:
1756       GDK_NOTE (EVENTS,
1757                 g_message ("unmap notify:\t\twindow: %ld",
1758                            xevent->xmap.window));
1759       
1760       event->any.type = GDK_UNMAP;
1761       event->any.window = window;      
1762
1763       /* If we are shown (not withdrawn) and get an unmap, it means we
1764        * were iconified in the X sense. If we are withdrawn, and get
1765        * an unmap, it means we hid the window ourselves, so we
1766        * will have already flipped the iconified bit off.
1767        */
1768       if (window)
1769         {
1770           if (GDK_WINDOW_IS_MAPPED (window))
1771             gdk_synthesize_window_state (window,
1772                                          0,
1773                                          GDK_WINDOW_STATE_ICONIFIED);
1774
1775           _gdk_xgrab_check_unmap (window, xevent->xany.serial);
1776         }
1777       
1778       break;
1779       
1780     case MapNotify:
1781       GDK_NOTE (EVENTS,
1782                 g_message ("map notify:\t\twindow: %ld",
1783                            xevent->xmap.window));
1784       
1785       event->any.type = GDK_MAP;
1786       event->any.window = window;
1787
1788       /* Unset iconified if it was set */
1789       if (window && (((GdkWindowObject*)window)->state & GDK_WINDOW_STATE_ICONIFIED))
1790         gdk_synthesize_window_state (window,
1791                                      GDK_WINDOW_STATE_ICONIFIED,
1792                                      0);
1793       
1794       break;
1795       
1796     case ReparentNotify:
1797       GDK_NOTE (EVENTS,
1798                 g_message ("reparent notify:\twindow: %ld  x,y: %d %d  parent: %ld      ovr: %d",
1799                            xevent->xreparent.window,
1800                            xevent->xreparent.x,
1801                            xevent->xreparent.y,
1802                            xevent->xreparent.parent,
1803                            xevent->xreparent.override_redirect));
1804
1805       /* Not currently handled */
1806       return_val = FALSE;
1807       break;
1808       
1809     case ConfigureNotify:
1810       GDK_NOTE (EVENTS,
1811                 g_message ("configure notify:\twindow: %ld  x,y: %d %d  w,h: %d %d  b-w: %d  above: %ld  ovr: %d%s",
1812                            xevent->xconfigure.window,
1813                            xevent->xconfigure.x,
1814                            xevent->xconfigure.y,
1815                            xevent->xconfigure.width,
1816                            xevent->xconfigure.height,
1817                            xevent->xconfigure.border_width,
1818                            xevent->xconfigure.above,
1819                            xevent->xconfigure.override_redirect,
1820                            !window
1821                            ? " (discarding)"
1822                            : GDK_WINDOW_TYPE (window) == GDK_WINDOW_CHILD
1823                            ? " (discarding child)"
1824                            : xevent->xconfigure.event != xevent->xconfigure.window
1825                            ? " (discarding substructure)"
1826                            : ""));
1827       if (window && GDK_WINDOW_TYPE (window) == GDK_WINDOW_ROOT)
1828         { 
1829           window_impl->width = xevent->xconfigure.width;
1830           window_impl->height = xevent->xconfigure.height;
1831           
1832           _gdk_x11_drawable_update_size (window_private->impl);
1833           _gdk_x11_screen_size_changed (screen, xevent);
1834         }
1835
1836       if (window &&
1837           xevent->xconfigure.event == xevent->xconfigure.window &&
1838           !GDK_WINDOW_DESTROYED (window) &&
1839           (window_private->extension_events != 0))
1840         _gdk_input_configure_event (&xevent->xconfigure, window);
1841       
1842 #ifdef HAVE_XSYNC
1843       if (toplevel && display_x11->use_sync && !XSyncValueIsZero (toplevel->pending_counter_value))
1844         {
1845           toplevel->current_counter_value = toplevel->pending_counter_value;
1846           XSyncIntToValue (&toplevel->pending_counter_value, 0);
1847         }
1848 #endif
1849
1850     if (!window ||
1851           xevent->xconfigure.event != xevent->xconfigure.window ||
1852           GDK_WINDOW_TYPE (window) == GDK_WINDOW_CHILD ||
1853           GDK_WINDOW_TYPE (window) == GDK_WINDOW_ROOT)
1854         return_val = FALSE;
1855       else
1856         {
1857           event->configure.type = GDK_CONFIGURE;
1858           event->configure.window = window;
1859           event->configure.width = xevent->xconfigure.width;
1860           event->configure.height = xevent->xconfigure.height;
1861           
1862           if (!xevent->xconfigure.send_event &&
1863               !xevent->xconfigure.override_redirect &&
1864               !GDK_WINDOW_DESTROYED (window))
1865             {
1866               gint tx = 0;
1867               gint ty = 0;
1868               Window child_window = 0;
1869
1870               gdk_error_trap_push ();
1871               if (XTranslateCoordinates (GDK_DRAWABLE_XDISPLAY (window),
1872                                          GDK_DRAWABLE_XID (window),
1873                                          screen_x11->xroot_window,
1874                                          0, 0,
1875                                          &tx, &ty,
1876                                          &child_window))
1877                 {
1878                   event->configure.x = tx;
1879                   event->configure.y = ty;
1880                 }
1881               gdk_error_trap_pop ();
1882             }
1883           else
1884             {
1885               event->configure.x = xevent->xconfigure.x;
1886               event->configure.y = xevent->xconfigure.y;
1887             }
1888           window_private->x = event->configure.x;
1889           window_private->y = event->configure.y;
1890           window_impl->width = xevent->xconfigure.width;
1891           window_impl->height = xevent->xconfigure.height;
1892           
1893           _gdk_x11_drawable_update_size (window_private->impl);
1894           
1895           if (window_private->resize_count >= 1)
1896             {
1897               window_private->resize_count -= 1;
1898
1899               if (window_private->resize_count == 0)
1900                 _gdk_moveresize_configure_done (display, window);
1901             }
1902         }
1903       break;
1904       
1905     case PropertyNotify:
1906       GDK_NOTE (EVENTS,
1907                 g_message ("property notify:\twindow: %ld, atom(%ld): %s%s%s",
1908                            xevent->xproperty.window,
1909                            xevent->xproperty.atom,
1910                            "\"",
1911                            gdk_x11_get_xatom_name_for_display (display, xevent->xproperty.atom),
1912                            "\""));
1913
1914       if (window_private == NULL)
1915         {
1916           return_val = FALSE;
1917           break;
1918         }
1919
1920       /* We compare with the serial of the last time we mapped the
1921        * window to avoid refetching properties that we set ourselves
1922        */
1923       if (toplevel &&
1924           xevent->xproperty.serial >= toplevel->map_serial)
1925         {
1926           if (xevent->xproperty.atom == gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_STATE"))
1927             gdk_check_wm_state_changed (window);
1928           
1929           if (xevent->xproperty.atom == gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_DESKTOP"))
1930             gdk_check_wm_desktop_changed (window);
1931         }
1932       
1933       if (window_private->event_mask & GDK_PROPERTY_CHANGE_MASK) 
1934         {
1935           event->property.type = GDK_PROPERTY_NOTIFY;
1936           event->property.window = window;
1937           event->property.atom = gdk_x11_xatom_to_atom_for_display (display, xevent->xproperty.atom);
1938           event->property.time = xevent->xproperty.time;
1939           event->property.state = xevent->xproperty.state;
1940         }
1941       else
1942         return_val = FALSE;
1943
1944       break;
1945       
1946     case SelectionClear:
1947       GDK_NOTE (EVENTS,
1948                 g_message ("selection clear:\twindow: %ld",
1949                            xevent->xproperty.window));
1950
1951       if (_gdk_selection_filter_clear_event (&xevent->xselectionclear))
1952         {
1953           event->selection.type = GDK_SELECTION_CLEAR;
1954           event->selection.window = window;
1955           event->selection.selection = gdk_x11_xatom_to_atom_for_display (display, xevent->xselectionclear.selection);
1956           event->selection.time = xevent->xselectionclear.time;
1957         }
1958       else
1959         return_val = FALSE;
1960           
1961       break;
1962       
1963     case SelectionRequest:
1964       GDK_NOTE (EVENTS,
1965                 g_message ("selection request:\twindow: %ld",
1966                            xevent->xproperty.window));
1967       
1968       event->selection.type = GDK_SELECTION_REQUEST;
1969       event->selection.window = window;
1970       event->selection.selection = gdk_x11_xatom_to_atom_for_display (display, xevent->xselectionrequest.selection);
1971       event->selection.target = gdk_x11_xatom_to_atom_for_display (display, xevent->xselectionrequest.target);
1972       event->selection.property = gdk_x11_xatom_to_atom_for_display (display, xevent->xselectionrequest.property);
1973       event->selection.requestor = xevent->xselectionrequest.requestor;
1974       event->selection.time = xevent->xselectionrequest.time;
1975       
1976       break;
1977       
1978     case SelectionNotify:
1979       GDK_NOTE (EVENTS,
1980                 g_message ("selection notify:\twindow: %ld",
1981                            xevent->xproperty.window));
1982       
1983       
1984       event->selection.type = GDK_SELECTION_NOTIFY;
1985       event->selection.window = window;
1986       event->selection.selection = gdk_x11_xatom_to_atom_for_display (display, xevent->xselection.selection);
1987       event->selection.target = gdk_x11_xatom_to_atom_for_display (display, xevent->xselection.target);
1988       event->selection.property = gdk_x11_xatom_to_atom_for_display (display, xevent->xselection.property);
1989       event->selection.time = xevent->xselection.time;
1990       
1991       break;
1992       
1993     case ColormapNotify:
1994       GDK_NOTE (EVENTS,
1995                 g_message ("colormap notify:\twindow: %ld",
1996                            xevent->xcolormap.window));
1997       
1998       /* Not currently handled */
1999       return_val = FALSE;
2000       break;
2001       
2002     case ClientMessage:
2003       {
2004         GList *tmp_list;
2005         GdkFilterReturn result = GDK_FILTER_CONTINUE;
2006         GdkAtom message_type = gdk_x11_xatom_to_atom_for_display (display, xevent->xclient.message_type);
2007
2008         GDK_NOTE (EVENTS,
2009                   g_message ("client message:\twindow: %ld",
2010                              xevent->xclient.window));
2011         
2012         tmp_list = display_x11->client_filters;
2013         while (tmp_list)
2014           {
2015             GdkClientFilter *filter = tmp_list->data;
2016             tmp_list = tmp_list->next;
2017             
2018             if (filter->type == message_type)
2019               {
2020                 result = (*filter->function) (xevent, event, filter->data);
2021                 if (result != GDK_FILTER_CONTINUE)
2022                   break;
2023               }
2024           }
2025
2026         switch (result)
2027           {
2028           case GDK_FILTER_REMOVE:
2029             return_val = FALSE;
2030             break;
2031           case GDK_FILTER_TRANSLATE:
2032             return_val = TRUE;
2033             break;
2034           case GDK_FILTER_CONTINUE:
2035             /* Send unknown ClientMessage's on to Gtk for it to use */
2036             if (window_private == NULL)
2037               {
2038                 return_val = FALSE;
2039               }
2040             else
2041               {
2042                 event->client.type = GDK_CLIENT_EVENT;
2043                 event->client.window = window;
2044                 event->client.message_type = message_type;
2045                 event->client.data_format = xevent->xclient.format;
2046                 memcpy(&event->client.data, &xevent->xclient.data,
2047                        sizeof(event->client.data));
2048               }
2049             break;
2050           }
2051       }
2052       
2053       break;
2054       
2055     case MappingNotify:
2056       GDK_NOTE (EVENTS,
2057                 g_message ("mapping notify"));
2058       
2059       /* Let XLib know that there is a new keyboard mapping.
2060        */
2061       XRefreshKeyboardMapping (&xevent->xmapping);
2062       _gdk_keymap_keys_changed (display);
2063       return_val = FALSE;
2064       break;
2065
2066     default:
2067 #ifdef HAVE_XKB
2068       if (xevent->type == display_x11->xkb_event_type)
2069         {
2070           XkbEvent *xkb_event = (XkbEvent *)xevent;
2071
2072           switch (xkb_event->any.xkb_type)
2073             {
2074             case XkbNewKeyboardNotify:
2075             case XkbMapNotify:
2076               _gdk_keymap_keys_changed (display);
2077
2078               return_val = FALSE;
2079               break;
2080               
2081             case XkbStateNotify:
2082               _gdk_keymap_state_changed (display, xevent);
2083               break;
2084             }
2085         }
2086       else
2087 #endif
2088 #ifdef HAVE_XFIXES
2089       if (xevent->type - display_x11->xfixes_event_base == XFixesSelectionNotify)
2090         {
2091           XFixesSelectionNotifyEvent *selection_notify = (XFixesSelectionNotifyEvent *)xevent;
2092
2093           _gdk_x11_screen_process_owner_change (screen, xevent);
2094           
2095           event->owner_change.type = GDK_OWNER_CHANGE;
2096           event->owner_change.window = window;
2097           event->owner_change.owner = selection_notify->owner;
2098           event->owner_change.reason = selection_notify->subtype;
2099           event->owner_change.selection = 
2100             gdk_x11_xatom_to_atom_for_display (display, 
2101                                                selection_notify->selection);
2102           event->owner_change.time = selection_notify->timestamp;
2103           event->owner_change.selection_time = selection_notify->selection_timestamp;
2104           
2105           return_val = TRUE;
2106         }
2107       else
2108 #endif
2109 #ifdef HAVE_RANDR
2110       if (xevent->type - display_x11->xrandr_event_base == RRScreenChangeNotify ||
2111           xevent->type - display_x11->xrandr_event_base == RRNotify)
2112         {
2113           if (screen)
2114             _gdk_x11_screen_size_changed (screen, xevent);
2115         }
2116       else
2117 #endif
2118 #if defined(HAVE_XCOMPOSITE) && defined (HAVE_XDAMAGE) && defined (HAVE_XFIXES)
2119       if (display_x11->have_xdamage && window_private && window_private->composited &&
2120           xevent->type == display_x11->xdamage_event_base + XDamageNotify &&
2121           ((XDamageNotifyEvent *) xevent)->damage == window_impl->damage)
2122         {
2123           XDamageNotifyEvent *damage_event = (XDamageNotifyEvent *) xevent;
2124           XserverRegion repair;
2125           GdkRectangle rect;
2126
2127           rect.x = window_private->x + damage_event->area.x;
2128           rect.y = window_private->y + damage_event->area.y;
2129           rect.width = damage_event->area.width;
2130           rect.height = damage_event->area.height;
2131
2132           repair = XFixesCreateRegion (display_x11->xdisplay,
2133                                        &damage_event->area, 1);
2134           XDamageSubtract (display_x11->xdisplay,
2135                            window_impl->damage,
2136                            repair, None);
2137           XFixesDestroyRegion (display_x11->xdisplay, repair);
2138
2139           if (window_private->parent != NULL)
2140             _gdk_window_process_expose (GDK_WINDOW (window_private->parent),
2141                                         damage_event->serial, &rect);
2142
2143           return_val = TRUE;
2144         }
2145       else
2146 #endif
2147         {
2148           /* something else - (e.g., a Xinput event) */
2149           
2150           if (window_private &&
2151               !GDK_WINDOW_DESTROYED (window_private) &&
2152               (window_private->extension_events != 0))
2153             return_val = _gdk_input_other_event(event, xevent, window);
2154           else
2155             return_val = FALSE;
2156           
2157           break;
2158         }
2159     }
2160
2161  done:
2162   if (return_val)
2163     {
2164       if (event->any.window)
2165         g_object_ref (event->any.window);
2166       if (((event->any.type == GDK_ENTER_NOTIFY) ||
2167            (event->any.type == GDK_LEAVE_NOTIFY)) &&
2168           (event->crossing.subwindow != NULL))
2169         g_object_ref (event->crossing.subwindow);
2170     }
2171   else
2172     {
2173       /* Mark this event as having no resources to be freed */
2174       event->any.window = NULL;
2175       event->any.type = GDK_NOTHING;
2176     }
2177   
2178   if (window)
2179     g_object_unref (window);
2180   
2181   return return_val;
2182 }
2183
2184 static GdkFilterReturn
2185 gdk_wm_protocols_filter (GdkXEvent *xev,
2186                          GdkEvent  *event,
2187                          gpointer data)
2188 {
2189   XEvent *xevent = (XEvent *)xev;
2190   GdkWindow *win = event->any.window;
2191   GdkDisplay *display;
2192   Atom atom;
2193
2194   if (!win)
2195       return GDK_FILTER_REMOVE;    
2196
2197   display = GDK_WINDOW_DISPLAY (win);
2198   atom = (Atom)xevent->xclient.data.l[0];
2199
2200   if (atom == gdk_x11_get_xatom_by_name_for_display (display, "WM_DELETE_WINDOW"))
2201     {
2202   /* The delete window request specifies a window
2203    *  to delete. We don't actually destroy the
2204    *  window because "it is only a request". (The
2205    *  window might contain vital data that the
2206    *  program does not want destroyed). Instead
2207    *  the event is passed along to the program,
2208    *  which should then destroy the window.
2209    */
2210       GDK_NOTE (EVENTS,
2211                 g_message ("delete window:\t\twindow: %ld",
2212                            xevent->xclient.window));
2213       
2214       event->any.type = GDK_DELETE;
2215
2216       gdk_x11_window_set_user_time (win, xevent->xclient.data.l[1]);
2217
2218       return GDK_FILTER_TRANSLATE;
2219     }
2220   else if (atom == gdk_x11_get_xatom_by_name_for_display (display, "WM_TAKE_FOCUS"))
2221     {
2222       GdkToplevelX11 *toplevel = _gdk_x11_window_get_toplevel (event->any.window);
2223       GdkWindowObject *private = (GdkWindowObject *)win;
2224
2225       /* There is no way of knowing reliably whether we are viewable;
2226        * _gdk_x11_set_input_focus_safe() traps errors asynchronously.
2227        */
2228       if (toplevel && private->accept_focus)
2229         _gdk_x11_set_input_focus_safe (display, toplevel->focus_window,
2230                                        RevertToParent,
2231                                        xevent->xclient.data.l[1]);
2232
2233       return GDK_FILTER_REMOVE;
2234     }
2235   else if (atom == gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_PING") &&
2236            !_gdk_x11_display_is_root_window (display,
2237                                              xevent->xclient.window))
2238     {
2239       XClientMessageEvent xclient = xevent->xclient;
2240       
2241       xclient.window = GDK_WINDOW_XROOTWIN (win);
2242       XSendEvent (GDK_WINDOW_XDISPLAY (win), 
2243                   xclient.window,
2244                   False, 
2245                   SubstructureRedirectMask | SubstructureNotifyMask, (XEvent *)&xclient);
2246
2247       return GDK_FILTER_REMOVE;
2248     }
2249   else if (atom == gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_SYNC_REQUEST") &&
2250            GDK_DISPLAY_X11 (display)->use_sync)
2251     {
2252       GdkToplevelX11 *toplevel = _gdk_x11_window_get_toplevel (event->any.window);
2253       if (toplevel)
2254         {
2255 #ifdef HAVE_XSYNC
2256           XSyncIntsToValue (&toplevel->pending_counter_value, 
2257                             xevent->xclient.data.l[2], 
2258                             xevent->xclient.data.l[3]);
2259 #endif
2260         }
2261       return GDK_FILTER_REMOVE;
2262     }
2263   
2264   return GDK_FILTER_CONTINUE;
2265 }
2266
2267 void
2268 _gdk_events_queue (GdkDisplay *display)
2269 {
2270   GList *node;
2271   GdkEvent *event;
2272   XEvent xevent;
2273   Display *xdisplay = GDK_DISPLAY_XDISPLAY (display);
2274
2275   while (!_gdk_event_queue_find_first(display) && XPending (xdisplay))
2276     {
2277       XNextEvent (xdisplay, &xevent);
2278
2279       switch (xevent.type)
2280         {
2281         case KeyPress:
2282         case KeyRelease:
2283           break;
2284         default:
2285           if (XFilterEvent (&xevent, None))
2286             continue;
2287         }
2288       
2289       event = gdk_event_new (GDK_NOTHING);
2290       
2291       event->any.window = NULL;
2292       event->any.send_event = xevent.xany.send_event ? TRUE : FALSE;
2293
2294       ((GdkEventPrivate *)event)->flags |= GDK_EVENT_PENDING;
2295
2296       node = _gdk_event_queue_append (display, event);
2297
2298       if (gdk_event_translate (display, event, &xevent, FALSE))
2299         {
2300           ((GdkEventPrivate *)event)->flags &= ~GDK_EVENT_PENDING;
2301         }
2302       else
2303         {
2304           _gdk_event_queue_remove_link (display, node);
2305           g_list_free_1 (node);
2306           gdk_event_free (event);
2307         }
2308     }
2309 }
2310
2311 static gboolean  
2312 gdk_event_prepare (GSource  *source,
2313                    gint     *timeout)
2314 {
2315   GdkDisplay *display = ((GdkDisplaySource*)source)->display;
2316   gboolean retval;
2317   
2318   GDK_THREADS_ENTER ();
2319
2320   *timeout = -1;
2321   retval = (_gdk_event_queue_find_first (display) != NULL || 
2322             gdk_check_xpending (display));
2323   
2324   GDK_THREADS_LEAVE ();
2325
2326   return retval;
2327 }
2328
2329 static gboolean  
2330 gdk_event_check (GSource *source) 
2331 {
2332   GdkDisplaySource *display_source = (GdkDisplaySource*)source;
2333   gboolean retval;
2334
2335   GDK_THREADS_ENTER ();
2336
2337   if (display_source->event_poll_fd.revents & G_IO_IN)
2338     retval = (_gdk_event_queue_find_first (display_source->display) != NULL || 
2339               gdk_check_xpending (display_source->display));
2340   else
2341     retval = FALSE;
2342
2343   GDK_THREADS_LEAVE ();
2344
2345   return retval;
2346 }
2347
2348 static gboolean  
2349 gdk_event_dispatch (GSource    *source,
2350                     GSourceFunc callback,
2351                     gpointer    user_data)
2352 {
2353   GdkDisplay *display = ((GdkDisplaySource*)source)->display;
2354   GdkEvent *event;
2355  
2356   GDK_THREADS_ENTER ();
2357
2358   _gdk_events_queue (display);
2359   event = _gdk_event_unqueue (display);
2360
2361   if (event)
2362     {
2363       if (_gdk_event_func)
2364         (*_gdk_event_func) (event, _gdk_event_data);
2365       
2366       gdk_event_free (event);
2367     }
2368   
2369   GDK_THREADS_LEAVE ();
2370
2371   return TRUE;
2372 }
2373
2374 /**
2375  * gdk_event_send_client_message_for_display:
2376  * @display: the #GdkDisplay for the window where the message is to be sent.
2377  * @event: the #GdkEvent to send, which should be a #GdkEventClient.
2378  * @winid: the window to send the client message to.
2379  *
2380  * On X11, sends an X ClientMessage event to a given window. On
2381  * Windows, sends a message registered with the name
2382  * GDK_WIN32_CLIENT_MESSAGE.
2383  *
2384  * This could be used for communicating between different
2385  * applications, though the amount of data is limited to 20 bytes on
2386  * X11, and to just four bytes on Windows.
2387  *
2388  * Returns: non-zero on success.
2389  *
2390  * Since: 2.2
2391  */
2392 gboolean
2393 gdk_event_send_client_message_for_display (GdkDisplay     *display,
2394                                            GdkEvent       *event,
2395                                            GdkNativeWindow winid)
2396 {
2397   XEvent sev;
2398   
2399   g_return_val_if_fail(event != NULL, FALSE);
2400
2401   /* Set up our event to send, with the exception of its target window */
2402   sev.xclient.type = ClientMessage;
2403   sev.xclient.display = GDK_DISPLAY_XDISPLAY (display);
2404   sev.xclient.format = event->client.data_format;
2405   sev.xclient.window = winid;
2406   memcpy(&sev.xclient.data, &event->client.data, sizeof (sev.xclient.data));
2407   sev.xclient.message_type = gdk_x11_atom_to_xatom_for_display (display, event->client.message_type);
2408   
2409   return _gdk_send_xevent (display, winid, False, NoEventMask, &sev);
2410 }
2411
2412
2413
2414 /* Sends a ClientMessage to all toplevel client windows */
2415 static gboolean
2416 gdk_event_send_client_message_to_all_recurse (GdkDisplay *display,
2417                                               XEvent     *xev, 
2418                                               guint32     xid,
2419                                               guint       level)
2420 {
2421   Atom type = None;
2422   int format;
2423   unsigned long nitems, after;
2424   unsigned char *data;
2425   Window *ret_children, ret_root, ret_parent;
2426   unsigned int ret_nchildren;
2427   gboolean send = FALSE;
2428   gboolean found = FALSE;
2429   gboolean result = FALSE;
2430   int i;
2431   
2432   gdk_error_trap_push ();
2433   
2434   if (XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), xid, 
2435                           gdk_x11_get_xatom_by_name_for_display (display, "WM_STATE"),
2436                           0, 0, False, AnyPropertyType,
2437                           &type, &format, &nitems, &after, &data) != Success)
2438     goto out;
2439   
2440   if (type)
2441     {
2442       send = TRUE;
2443       XFree (data);
2444     }
2445   else
2446     {
2447       /* OK, we're all set, now let's find some windows to send this to */
2448       if (!XQueryTree (GDK_DISPLAY_XDISPLAY (display), xid,
2449                       &ret_root, &ret_parent,
2450                       &ret_children, &ret_nchildren))   
2451         goto out;
2452
2453       for(i = 0; i < ret_nchildren; i++)
2454         if (gdk_event_send_client_message_to_all_recurse (display, xev, ret_children[i], level + 1))
2455           found = TRUE;
2456
2457       XFree (ret_children);
2458     }
2459
2460   if (send || (!found && (level == 1)))
2461     {
2462       xev->xclient.window = xid;
2463       _gdk_send_xevent (display, xid, False, NoEventMask, xev);
2464     }
2465
2466   result = send || found;
2467
2468  out:
2469   gdk_error_trap_pop ();
2470
2471   return result;
2472 }
2473
2474 /**
2475  * gdk_screen_broadcast_client_message:
2476  * @screen: the #GdkScreen where the event will be broadcasted.
2477  * @event: the #GdkEvent.
2478  *
2479  * On X11, sends an X ClientMessage event to all toplevel windows on
2480  * @screen. 
2481  *
2482  * Toplevel windows are determined by checking for the WM_STATE property, 
2483  * as described in the Inter-Client Communication Conventions Manual (ICCCM).
2484  * If no windows are found with the WM_STATE property set, the message is 
2485  * sent to all children of the root window.
2486  *
2487  * On Windows, broadcasts a message registered with the name
2488  * GDK_WIN32_CLIENT_MESSAGE to all top-level windows. The amount of
2489  * data is limited to one long, i.e. four bytes.
2490  *
2491  * Since: 2.2
2492  */
2493
2494 void
2495 gdk_screen_broadcast_client_message (GdkScreen *screen, 
2496                                      GdkEvent  *event)
2497 {
2498   XEvent sev;
2499   GdkWindow *root_window;
2500
2501   g_return_if_fail (event != NULL);
2502   
2503   root_window = gdk_screen_get_root_window (screen);
2504   
2505   /* Set up our event to send, with the exception of its target window */
2506   sev.xclient.type = ClientMessage;
2507   sev.xclient.display = GDK_WINDOW_XDISPLAY (root_window);
2508   sev.xclient.format = event->client.data_format;
2509   memcpy(&sev.xclient.data, &event->client.data, sizeof (sev.xclient.data));
2510   sev.xclient.message_type = 
2511     gdk_x11_atom_to_xatom_for_display (GDK_WINDOW_DISPLAY (root_window),
2512                                        event->client.message_type);
2513
2514   gdk_event_send_client_message_to_all_recurse (gdk_screen_get_display (screen),
2515                                                 &sev, 
2516                                                 GDK_WINDOW_XID (root_window), 
2517                                                 0);
2518 }
2519
2520 /*
2521  *--------------------------------------------------------------
2522  * gdk_flush
2523  *
2524  *   Flushes the Xlib output buffer and then waits
2525  *   until all requests have been received and processed
2526  *   by the X server. The only real use for this function
2527  *   is in dealing with XShm.
2528  *
2529  * Arguments:
2530  *
2531  * Results:
2532  *
2533  * Side effects:
2534  *
2535  *--------------------------------------------------------------
2536  */
2537
2538 void
2539 gdk_flush (void)
2540 {
2541   GSList *tmp_list = _gdk_displays;
2542   
2543   while (tmp_list)
2544     {
2545       XSync (GDK_DISPLAY_XDISPLAY (tmp_list->data), False);
2546       tmp_list = tmp_list->next;
2547     }
2548 }
2549
2550 static Bool
2551 timestamp_predicate (Display *display,
2552                      XEvent  *xevent,
2553                      XPointer arg)
2554 {
2555   Window xwindow = GPOINTER_TO_UINT (arg);
2556   GdkDisplay *gdk_display = gdk_x11_lookup_xdisplay (display);
2557
2558   if (xevent->type == PropertyNotify &&
2559       xevent->xproperty.window == xwindow &&
2560       xevent->xproperty.atom == gdk_x11_get_xatom_by_name_for_display (gdk_display,
2561                                                                        "GDK_TIMESTAMP_PROP"))
2562     return True;
2563
2564   return False;
2565 }
2566
2567 /**
2568  * gdk_x11_get_server_time:
2569  * @window: a #GdkWindow, used for communication with the server.
2570  *          The window must have GDK_PROPERTY_CHANGE_MASK in its
2571  *          events mask or a hang will result.
2572  * 
2573  * Routine to get the current X server time stamp. 
2574  * 
2575  * Return value: the time stamp.
2576  **/
2577 guint32
2578 gdk_x11_get_server_time (GdkWindow *window)
2579 {
2580   Display *xdisplay;
2581   Window   xwindow;
2582   guchar c = 'a';
2583   XEvent xevent;
2584   Atom timestamp_prop_atom;
2585
2586   g_return_val_if_fail (GDK_IS_WINDOW (window), 0);
2587   g_return_val_if_fail (!GDK_WINDOW_DESTROYED (window), 0);
2588
2589   xdisplay = GDK_WINDOW_XDISPLAY (window);
2590   xwindow = GDK_WINDOW_XWINDOW (window);
2591   timestamp_prop_atom = 
2592     gdk_x11_get_xatom_by_name_for_display (GDK_WINDOW_DISPLAY (window),
2593                                            "GDK_TIMESTAMP_PROP");
2594   
2595   XChangeProperty (xdisplay, xwindow, timestamp_prop_atom,
2596                    timestamp_prop_atom,
2597                    8, PropModeReplace, &c, 1);
2598
2599   XIfEvent (xdisplay, &xevent,
2600             timestamp_predicate, GUINT_TO_POINTER(xwindow));
2601
2602   return xevent.xproperty.time;
2603 }
2604
2605 static void
2606 fetch_net_wm_check_window (GdkScreen *screen)
2607 {
2608   GdkScreenX11 *screen_x11;
2609   GdkDisplay *display;
2610   Atom type;
2611   gint format;
2612   gulong n_items;
2613   gulong bytes_after;
2614   guchar *data;
2615   Window *xwindow;
2616   GTimeVal tv;
2617   
2618   screen_x11 = GDK_SCREEN_X11 (screen);
2619   display = screen_x11->display;
2620
2621   g_return_if_fail (GDK_DISPLAY_X11 (display)->trusted_client);
2622   
2623   g_get_current_time (&tv);
2624       
2625   if (ABS  (tv.tv_sec - screen_x11->last_wmspec_check_time) < 15)
2626     return; /* we've checked recently */
2627
2628   screen_x11->last_wmspec_check_time = tv.tv_sec;
2629
2630   data = NULL;
2631   XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), screen_x11->xroot_window,
2632                       gdk_x11_get_xatom_by_name_for_display (display, "_NET_SUPPORTING_WM_CHECK"),
2633                       0, G_MAXLONG, False, XA_WINDOW, &type, &format, 
2634                       &n_items, &bytes_after, &data);
2635   
2636   if (type != XA_WINDOW)
2637     {
2638       if (data)
2639         XFree (data);
2640       return;
2641     }
2642
2643   xwindow = (Window *)data;
2644
2645   if (screen_x11->wmspec_check_window == *xwindow)
2646     {
2647       XFree (xwindow);
2648       return;
2649     }
2650
2651   gdk_error_trap_push ();
2652   
2653   /* Find out if this WM goes away, so we can reset everything. */
2654   XSelectInput (screen_x11->xdisplay, *xwindow, StructureNotifyMask);
2655   gdk_display_sync (display);
2656
2657   if (gdk_error_trap_pop () == Success)
2658     {
2659       screen_x11->wmspec_check_window = *xwindow;
2660       screen_x11->need_refetch_net_supported = TRUE;
2661       screen_x11->need_refetch_wm_name = TRUE;
2662       
2663       /* Careful, reentrancy */
2664       _gdk_x11_screen_window_manager_changed (GDK_SCREEN (screen_x11));
2665     }
2666
2667   XFree (xwindow);    
2668 }
2669
2670 /**
2671  * gdk_x11_screen_get_window_manager_name:
2672  * @screen: a #GdkScreen 
2673  * 
2674  * Returns the name of the window manager for @screen. 
2675  * 
2676  * Return value: the name of the window manager screen @screen, or 
2677  * "unknown" if the window manager is unknown. The string is owned by GDK
2678  * and should not be freed.
2679  *
2680  * Since: 2.2
2681  **/
2682 const char*
2683 gdk_x11_screen_get_window_manager_name (GdkScreen *screen)
2684 {
2685   GdkScreenX11 *screen_x11;
2686
2687   screen_x11 = GDK_SCREEN_X11 (screen);
2688   
2689   if (!G_LIKELY (GDK_DISPLAY_X11 (screen_x11->display)->trusted_client))
2690     return screen_x11->window_manager_name;
2691
2692   fetch_net_wm_check_window (screen);
2693
2694   if (screen_x11->need_refetch_wm_name)
2695     {
2696       /* Get the name of the window manager */
2697       screen_x11->need_refetch_wm_name = FALSE;
2698
2699       g_free (screen_x11->window_manager_name);
2700       screen_x11->window_manager_name = g_strdup ("unknown");
2701       
2702       if (screen_x11->wmspec_check_window != None)
2703         {
2704           Atom type;
2705           gint format;
2706           gulong n_items;
2707           gulong bytes_after;
2708           gchar *name;
2709           
2710           name = NULL;
2711
2712           gdk_error_trap_push ();
2713           
2714           XGetWindowProperty (GDK_DISPLAY_XDISPLAY (screen_x11->display),
2715                               screen_x11->wmspec_check_window,
2716                               gdk_x11_get_xatom_by_name_for_display (screen_x11->display,
2717                                                                      "_NET_WM_NAME"),
2718                               0, G_MAXLONG, False,
2719                               gdk_x11_get_xatom_by_name_for_display (screen_x11->display,
2720                                                                      "UTF8_STRING"),
2721                               &type, &format, 
2722                               &n_items, &bytes_after,
2723                               (guchar **)&name);
2724           
2725           gdk_display_sync (screen_x11->display);
2726           
2727           gdk_error_trap_pop ();
2728           
2729           if (name != NULL)
2730             {
2731               g_free (screen_x11->window_manager_name);
2732               screen_x11->window_manager_name = g_strdup (name);
2733               XFree (name);
2734             }
2735         }
2736     }
2737   
2738   return GDK_SCREEN_X11 (screen)->window_manager_name;
2739 }
2740
2741 typedef struct _NetWmSupportedAtoms NetWmSupportedAtoms;
2742
2743 struct _NetWmSupportedAtoms
2744 {
2745   Atom *atoms;
2746   gulong n_atoms;
2747 };
2748
2749 static void
2750 cleanup_atoms(gpointer data)
2751 {
2752   NetWmSupportedAtoms *supported_atoms = data;
2753   if (supported_atoms->atoms)
2754       XFree (supported_atoms->atoms);
2755   g_free (supported_atoms);
2756 }
2757
2758 /**
2759  * gdk_x11_screen_supports_net_wm_hint:
2760  * @screen: the relevant #GdkScreen.
2761  * @property: a property atom.
2762  * 
2763  * This function is specific to the X11 backend of GDK, and indicates
2764  * whether the window manager supports a certain hint from the
2765  * Extended Window Manager Hints Specification. You can find this
2766  * specification on 
2767  * <ulink url="http://www.freedesktop.org">http://www.freedesktop.org</ulink>.
2768  *
2769  * When using this function, keep in mind that the window manager
2770  * can change over time; so you shouldn't use this function in
2771  * a way that impacts persistent application state. A common bug
2772  * is that your application can start up before the window manager
2773  * does when the user logs in, and before the window manager starts
2774  * gdk_x11_screen_supports_net_wm_hint() will return %FALSE for every property.
2775  * You can monitor the window_manager_changed signal on #GdkScreen to detect
2776  * a window manager change.
2777  * 
2778  * Return value: %TRUE if the window manager supports @property
2779  *
2780  * Since: 2.2
2781  **/
2782 gboolean
2783 gdk_x11_screen_supports_net_wm_hint (GdkScreen *screen,
2784                                      GdkAtom    property)
2785 {
2786   gulong i;
2787   GdkScreenX11 *screen_x11;
2788   NetWmSupportedAtoms *supported_atoms;
2789   GdkDisplay *display;
2790
2791   g_return_val_if_fail (GDK_IS_SCREEN (screen), FALSE);
2792   
2793   screen_x11 = GDK_SCREEN_X11 (screen);
2794   display = screen_x11->display;
2795
2796   if (!G_LIKELY (GDK_DISPLAY_X11 (display)->trusted_client))
2797     return FALSE;
2798
2799   supported_atoms = g_object_get_data (G_OBJECT (screen), "gdk-net-wm-supported-atoms");
2800   if (!supported_atoms)
2801     {
2802       supported_atoms = g_new0 (NetWmSupportedAtoms, 1);
2803       g_object_set_data_full (G_OBJECT (screen), "gdk-net-wm-supported-atoms", supported_atoms, cleanup_atoms);
2804     }
2805
2806   fetch_net_wm_check_window (screen);
2807
2808   if (screen_x11->wmspec_check_window == None)
2809     return FALSE;
2810   
2811   if (screen_x11->need_refetch_net_supported)
2812     {
2813       /* WM has changed since we last got the supported list,
2814        * refetch it.
2815        */
2816       Atom type;
2817       gint format;
2818       gulong bytes_after;
2819
2820       screen_x11->need_refetch_net_supported = FALSE;
2821       
2822       if (supported_atoms->atoms)
2823         XFree (supported_atoms->atoms);
2824       
2825       supported_atoms->atoms = NULL;
2826       supported_atoms->n_atoms = 0;
2827       
2828       XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), screen_x11->xroot_window,
2829                           gdk_x11_get_xatom_by_name_for_display (display, "_NET_SUPPORTED"),
2830                           0, G_MAXLONG, False, XA_ATOM, &type, &format, 
2831                           &supported_atoms->n_atoms, &bytes_after,
2832                           (guchar **)&supported_atoms->atoms);
2833       
2834       if (type != XA_ATOM)
2835         return FALSE;
2836     }
2837   
2838   if (supported_atoms->atoms == NULL)
2839     return FALSE;
2840   
2841   i = 0;
2842   while (i < supported_atoms->n_atoms)
2843     {
2844       if (supported_atoms->atoms[i] == gdk_x11_atom_to_xatom_for_display (display, property))
2845         return TRUE;
2846       
2847       ++i;
2848     }
2849   
2850   return FALSE;
2851 }
2852
2853 /**
2854  * gdk_net_wm_supports:
2855  * @property: a property atom.
2856  * 
2857  * This function is specific to the X11 backend of GDK, and indicates
2858  * whether the window manager for the default screen supports a certain
2859  * hint from the Extended Window Manager Hints Specification. See
2860  * gdk_x11_screen_supports_net_wm_hint() for complete details.
2861  * 
2862  * Return value: %TRUE if the window manager supports @property
2863  **/
2864 gboolean
2865 gdk_net_wm_supports (GdkAtom property)
2866 {
2867   return gdk_x11_screen_supports_net_wm_hint (gdk_screen_get_default (), property);
2868 }
2869
2870
2871 static void
2872 gdk_xsettings_notify_cb (const char       *name,
2873                          XSettingsAction   action,
2874                          XSettingsSetting *setting,
2875                          void             *data)
2876 {
2877   GdkEvent new_event;
2878   GdkScreen *screen = data;
2879   GdkScreenX11 *screen_x11 = data;
2880   int i;
2881
2882   if (screen_x11->xsettings_in_init)
2883     return;
2884   
2885   new_event.type = GDK_SETTING;
2886   new_event.setting.window = gdk_screen_get_root_window (screen);
2887   new_event.setting.send_event = FALSE;
2888   new_event.setting.name = NULL;
2889
2890   for (i = 0; i < GDK_SETTINGS_N_ELEMENTS() ; i++)
2891     if (strcmp (GDK_SETTINGS_X_NAME (i), name) == 0)
2892       {
2893         new_event.setting.name = (char*) GDK_SETTINGS_GDK_NAME (i);
2894         break;
2895       }
2896   
2897   if (!new_event.setting.name)
2898     return;
2899   
2900   switch (action)
2901     {
2902     case XSETTINGS_ACTION_NEW:
2903       new_event.setting.action = GDK_SETTING_ACTION_NEW;
2904       break;
2905     case XSETTINGS_ACTION_CHANGED:
2906       new_event.setting.action = GDK_SETTING_ACTION_CHANGED;
2907       break;
2908     case XSETTINGS_ACTION_DELETED:
2909       new_event.setting.action = GDK_SETTING_ACTION_DELETED;
2910       break;
2911     }
2912
2913   gdk_event_put (&new_event);
2914 }
2915
2916 static gboolean
2917 check_transform (const gchar *xsettings_name,
2918                  GType        src_type,
2919                  GType        dest_type)
2920 {
2921   if (!g_value_type_transformable (src_type, dest_type))
2922     {
2923       g_warning ("Cannot transform xsetting %s of type %s to type %s\n",
2924                  xsettings_name,
2925                  g_type_name (src_type),
2926                  g_type_name (dest_type));
2927       return FALSE;
2928     }
2929   else
2930     return TRUE;
2931 }
2932
2933 /**
2934  * gdk_screen_get_setting:
2935  * @screen: the #GdkScreen where the setting is located
2936  * @name: the name of the setting
2937  * @value: location to store the value of the setting
2938  *
2939  * Retrieves a desktop-wide setting such as double-click time
2940  * for the #GdkScreen @screen. 
2941  *
2942  * FIXME needs a list of valid settings here, or a link to 
2943  * more information.
2944  * 
2945  * Returns: %TRUE if the setting existed and a value was stored
2946  *   in @value, %FALSE otherwise.
2947  *
2948  * Since: 2.2
2949  **/
2950 gboolean
2951 gdk_screen_get_setting (GdkScreen   *screen,
2952                         const gchar *name,
2953                         GValue      *value)
2954 {
2955
2956   const char *xsettings_name = NULL;
2957   XSettingsResult result;
2958   XSettingsSetting *setting = NULL;
2959   GdkScreenX11 *screen_x11;
2960   gboolean success = FALSE;
2961   gint i;
2962   GValue tmp_val = { 0, };
2963   
2964   g_return_val_if_fail (GDK_IS_SCREEN (screen), FALSE);
2965   
2966   screen_x11 = GDK_SCREEN_X11 (screen);
2967
2968   for (i = 0; i < GDK_SETTINGS_N_ELEMENTS(); i++)
2969     if (strcmp (GDK_SETTINGS_GDK_NAME (i), name) == 0)
2970       {
2971         xsettings_name = GDK_SETTINGS_X_NAME (i);
2972         break;
2973       }
2974
2975   if (!xsettings_name)
2976     goto out;
2977
2978   result = xsettings_client_get_setting (screen_x11->xsettings_client, 
2979                                          xsettings_name, &setting);
2980   if (result != XSETTINGS_SUCCESS)
2981     goto out;
2982
2983   switch (setting->type)
2984     {
2985     case XSETTINGS_TYPE_INT:
2986       if (check_transform (xsettings_name, G_TYPE_INT, G_VALUE_TYPE (value)))
2987         {
2988           g_value_init (&tmp_val, G_TYPE_INT);
2989           g_value_set_int (&tmp_val, setting->data.v_int);
2990           g_value_transform (&tmp_val, value);
2991
2992           success = TRUE;
2993         }
2994       break;
2995     case XSETTINGS_TYPE_STRING:
2996       if (check_transform (xsettings_name, G_TYPE_STRING, G_VALUE_TYPE (value)))
2997         {
2998           g_value_init (&tmp_val, G_TYPE_STRING);
2999           g_value_set_string (&tmp_val, setting->data.v_string);
3000           g_value_transform (&tmp_val, value);
3001
3002           success = TRUE;
3003         }
3004       break;
3005     case XSETTINGS_TYPE_COLOR:
3006       if (!check_transform (xsettings_name, GDK_TYPE_COLOR, G_VALUE_TYPE (value)))
3007         {
3008           GdkColor color;
3009           
3010           g_value_init (&tmp_val, GDK_TYPE_COLOR);
3011
3012           color.pixel = 0;
3013           color.red = setting->data.v_color.red;
3014           color.green = setting->data.v_color.green;
3015           color.blue = setting->data.v_color.blue;
3016           
3017           g_value_set_boxed (&tmp_val, &color);
3018           
3019           g_value_transform (&tmp_val, value);
3020           
3021           success = TRUE;
3022         }
3023       break;
3024     }
3025   
3026   g_value_unset (&tmp_val);
3027
3028  out:
3029   if (setting)
3030     xsettings_setting_free (setting);
3031
3032   if (success)
3033     return TRUE;
3034   else
3035     return _gdk_x11_get_xft_setting (screen, name, value);
3036 }
3037
3038 static GdkFilterReturn 
3039 gdk_xsettings_client_event_filter (GdkXEvent *xevent,
3040                                    GdkEvent  *event,
3041                                    gpointer   data)
3042 {
3043   GdkScreenX11 *screen = data;
3044   
3045   if (xsettings_client_process_event (screen->xsettings_client, (XEvent *)xevent))
3046     return GDK_FILTER_REMOVE;
3047   else
3048     return GDK_FILTER_CONTINUE;
3049 }
3050
3051 static Bool
3052 gdk_xsettings_watch_cb (Window   window,
3053                         Bool     is_start,
3054                         long     mask,
3055                         void    *cb_data)
3056 {
3057   GdkWindow *gdkwin;
3058   GdkScreen *screen = cb_data;
3059
3060   gdkwin = gdk_window_lookup_for_display (gdk_screen_get_display (screen), window);
3061
3062   if (is_start)
3063     {
3064       if (gdkwin)
3065         g_object_ref (gdkwin);
3066       else
3067         {
3068           gdkwin = gdk_window_foreign_new_for_display (gdk_screen_get_display (screen), window);
3069           
3070           /* gdk_window_foreign_new_for_display() can fail and return NULL if the
3071            * window has already been destroyed.
3072            */
3073           if (!gdkwin)
3074             return False;
3075         }
3076
3077       gdk_window_add_filter (gdkwin, gdk_xsettings_client_event_filter, screen);
3078     }
3079   else
3080     {
3081       if (!gdkwin)
3082         {
3083           /* gdkwin should not be NULL here, since if starting the watch succeeded
3084            * we have a reference on the window. It might mean that the caller didn't
3085            * remove the watch when it got a DestroyNotify event. Or maybe the
3086            * caller ignored the return value when starting the watch failed.
3087            */
3088           g_warning ("gdk_xsettings_watch_cb(): Couldn't find window to unwatch");
3089           return False;
3090         }
3091       
3092       gdk_window_remove_filter (gdkwin, gdk_xsettings_client_event_filter, screen);
3093       g_object_unref (gdkwin);
3094     }
3095
3096   return True;
3097 }
3098
3099 void
3100 _gdk_windowing_event_data_copy (const GdkEvent *src,
3101                                 GdkEvent       *dst)
3102 {
3103 }
3104
3105 void
3106 _gdk_windowing_event_data_free (GdkEvent *event)
3107 {
3108 }
3109
3110 #define __GDK_EVENTS_X11_C__
3111 #include "gdkaliasdef.c"