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