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