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