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