]> Pileus Git - ~andy/gtk/blob - gdk/x11/gdkevents-x11.c
Remove_gdk_pixmap_set_as_backing etc, as this is not used anymore
[~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       /* We treat button presses as scroll wheel events, so don't expose
1196        * this grab to gtk, as it will be immediately released. If we do
1197        * expose it there is a short time before we receive the Release
1198        * where a client-side generated pointer motion event could be handled
1199        * as if the grab was effect.
1200        */
1201       if (!(xevent->xbutton.button == 4 || xevent->xbutton.button == 5 ||
1202             xevent->xbutton.button == 6 || xevent->xbutton.button == 7))
1203         _gdk_xgrab_check_button_event (window, xevent);
1204       break;
1205       
1206     case ButtonRelease:
1207       GDK_NOTE (EVENTS, 
1208                 g_message ("button release:\twindow: %ld  x,y: %d %d  button: %d",
1209                            xevent->xbutton.window,
1210                            xevent->xbutton.x, xevent->xbutton.y,
1211                            xevent->xbutton.button));
1212       
1213       if (window_private == NULL ||
1214           ((window_private->extension_events != 0) &&
1215            display_x11->input_ignore_core))
1216         {
1217           return_val = FALSE;
1218           break;
1219         }
1220       
1221       /* We treat button presses as scroll wheel events, so ignore the release */
1222       if (xevent->xbutton.button == 4 || xevent->xbutton.button == 5 ||
1223           xevent->xbutton.button == 6 || xevent->xbutton.button ==7)
1224         {
1225           return_val = FALSE;
1226           break;
1227         }
1228
1229       event->button.type = GDK_BUTTON_RELEASE;
1230       event->button.window = window;
1231       event->button.time = xevent->xbutton.time;
1232       event->button.x = xevent->xbutton.x;
1233       event->button.y = xevent->xbutton.y;
1234       event->button.x_root = (gfloat)xevent->xbutton.x_root;
1235       event->button.y_root = (gfloat)xevent->xbutton.y_root;
1236       event->button.axes = NULL;
1237       event->button.state = (GdkModifierType) xevent->xbutton.state;
1238       event->button.button = xevent->xbutton.button;
1239       event->button.device = display->core_pointer;
1240
1241       if (!set_screen_from_root (display, event, xevent->xbutton.root))
1242         return_val = FALSE;
1243       
1244       _gdk_xgrab_check_button_event (window, xevent);
1245       
1246       break;
1247       
1248     case MotionNotify:
1249       GDK_NOTE (EVENTS,
1250                 g_message ("motion notify:\t\twindow: %ld  x,y: %d %d  hint: %s", 
1251                            xevent->xmotion.window,
1252                            xevent->xmotion.x, xevent->xmotion.y,
1253                            (xevent->xmotion.is_hint) ? "true" : "false"));
1254       
1255       if (window_private == NULL ||
1256           ((window_private->extension_events != 0) &&
1257            display_x11->input_ignore_core))
1258         {
1259           return_val = FALSE;
1260           break;
1261         }
1262
1263       event->motion.type = GDK_MOTION_NOTIFY;
1264       event->motion.window = window;
1265       event->motion.time = xevent->xmotion.time;
1266       event->motion.x = xevent->xmotion.x;
1267       event->motion.y = xevent->xmotion.y;
1268       event->motion.x_root = (gfloat)xevent->xmotion.x_root;
1269       event->motion.y_root = (gfloat)xevent->xmotion.y_root;
1270       event->motion.axes = NULL;
1271       event->motion.state = (GdkModifierType) xevent->xmotion.state;
1272       event->motion.is_hint = xevent->xmotion.is_hint;
1273       event->motion.device = display->core_pointer;
1274       
1275       if (!set_screen_from_root (display, event, xevent->xbutton.root))
1276         {
1277           return_val = FALSE;
1278           break;
1279         }
1280             
1281       break;
1282       
1283     case EnterNotify:
1284       GDK_NOTE (EVENTS,
1285                 g_message ("enter notify:\t\twindow: %ld  detail: %d subwin: %ld",
1286                            xevent->xcrossing.window,
1287                            xevent->xcrossing.detail,
1288                            xevent->xcrossing.subwindow));
1289  
1290       if (window_private == NULL)
1291         {
1292           return_val = FALSE;
1293           break;
1294         }
1295       
1296       if (!set_screen_from_root (display, event, xevent->xbutton.root))
1297         {
1298           return_val = FALSE;
1299           break;
1300         }
1301       
1302       /* Handle focusing (in the case where no window manager is running */
1303       if (toplevel && xevent->xcrossing.detail != NotifyInferior)
1304         {
1305           toplevel->has_pointer = TRUE;
1306
1307           if (xevent->xcrossing.focus && !toplevel->has_focus_window)
1308             {
1309               gboolean had_focus = HAS_FOCUS (toplevel);
1310               
1311               toplevel->has_pointer_focus = TRUE;
1312               
1313               if (HAS_FOCUS (toplevel) != had_focus)
1314                 generate_focus_event (window, TRUE);
1315             }
1316         }
1317
1318       /* Tell XInput stuff about it if appropriate */
1319       if (window_private &&
1320           !GDK_WINDOW_DESTROYED (window) &&
1321           window_private->extension_events != 0)
1322         _gdk_input_enter_event (&xevent->xcrossing, window);
1323       
1324       event->crossing.type = GDK_ENTER_NOTIFY;
1325       event->crossing.window = window;
1326       
1327       /* If the subwindow field of the XEvent is non-NULL, then
1328        *  lookup the corresponding GdkWindow.
1329        */
1330       if (xevent->xcrossing.subwindow != None)
1331         event->crossing.subwindow = gdk_window_lookup_for_display (display, xevent->xcrossing.subwindow);
1332       else
1333         event->crossing.subwindow = NULL;
1334       
1335       event->crossing.time = xevent->xcrossing.time;
1336       event->crossing.x = xevent->xcrossing.x;
1337       event->crossing.y = xevent->xcrossing.y;
1338       event->crossing.x_root = xevent->xcrossing.x_root;
1339       event->crossing.y_root = xevent->xcrossing.y_root;
1340       
1341       /* Translate the crossing mode into Gdk terms.
1342        */
1343       switch (xevent->xcrossing.mode)
1344         {
1345         case NotifyNormal:
1346           event->crossing.mode = GDK_CROSSING_NORMAL;
1347           break;
1348         case NotifyGrab:
1349           event->crossing.mode = GDK_CROSSING_GRAB;
1350           break;
1351         case NotifyUngrab:
1352           event->crossing.mode = GDK_CROSSING_UNGRAB;
1353           break;
1354         };
1355       
1356       /* Translate the crossing detail into Gdk terms.
1357        */
1358       switch (xevent->xcrossing.detail)
1359         {
1360         case NotifyInferior:
1361           event->crossing.detail = GDK_NOTIFY_INFERIOR;
1362           break;
1363         case NotifyAncestor:
1364           event->crossing.detail = GDK_NOTIFY_ANCESTOR;
1365           break;
1366         case NotifyVirtual:
1367           event->crossing.detail = GDK_NOTIFY_VIRTUAL;
1368           break;
1369         case NotifyNonlinear:
1370           event->crossing.detail = GDK_NOTIFY_NONLINEAR;
1371           break;
1372         case NotifyNonlinearVirtual:
1373           event->crossing.detail = GDK_NOTIFY_NONLINEAR_VIRTUAL;
1374           break;
1375         default:
1376           event->crossing.detail = GDK_NOTIFY_UNKNOWN;
1377           break;
1378         }
1379       
1380       event->crossing.focus = xevent->xcrossing.focus;
1381       event->crossing.state = xevent->xcrossing.state;
1382   
1383       break;
1384       
1385     case LeaveNotify:
1386       GDK_NOTE (EVENTS, 
1387                 g_message ("leave notify:\t\twindow: %ld  detail: %d subwin: %ld",
1388                            xevent->xcrossing.window,
1389                            xevent->xcrossing.detail, xevent->xcrossing.subwindow));
1390
1391       if (window_private == NULL)
1392         {
1393           return_val = FALSE;
1394           break;
1395         }
1396
1397       if (!set_screen_from_root (display, event, xevent->xbutton.root))
1398         {
1399           return_val = FALSE;
1400           break;
1401         }
1402                   
1403       /* Handle focusing (in the case where no window manager is running */
1404       if (toplevel && xevent->xcrossing.detail != NotifyInferior)
1405         {
1406           toplevel->has_pointer = FALSE;
1407
1408           if (xevent->xcrossing.focus && !toplevel->has_focus_window)
1409             {
1410               gboolean had_focus = HAS_FOCUS (toplevel);
1411               
1412               toplevel->has_pointer_focus = FALSE;
1413               
1414               if (HAS_FOCUS (toplevel) != had_focus)
1415                 generate_focus_event (window, FALSE);
1416             }
1417         }
1418
1419       event->crossing.type = GDK_LEAVE_NOTIFY;
1420       event->crossing.window = window;
1421       
1422       /* If the subwindow field of the XEvent is non-NULL, then
1423        *  lookup the corresponding GdkWindow.
1424        */
1425       if (xevent->xcrossing.subwindow != None)
1426         event->crossing.subwindow = gdk_window_lookup_for_display (display, xevent->xcrossing.subwindow);
1427       else
1428         event->crossing.subwindow = NULL;
1429       
1430       event->crossing.time = xevent->xcrossing.time;
1431       event->crossing.x = xevent->xcrossing.x;
1432       event->crossing.y = xevent->xcrossing.y;
1433       event->crossing.x_root = xevent->xcrossing.x_root;
1434       event->crossing.y_root = xevent->xcrossing.y_root;
1435       
1436       /* Translate the crossing mode into Gdk terms.
1437        */
1438       switch (xevent->xcrossing.mode)
1439         {
1440         case NotifyNormal:
1441           event->crossing.mode = GDK_CROSSING_NORMAL;
1442           break;
1443         case NotifyGrab:
1444           event->crossing.mode = GDK_CROSSING_GRAB;
1445           break;
1446         case NotifyUngrab:
1447           event->crossing.mode = GDK_CROSSING_UNGRAB;
1448           break;
1449         };
1450       
1451       /* Translate the crossing detail into Gdk terms.
1452        */
1453       switch (xevent->xcrossing.detail)
1454         {
1455         case NotifyInferior:
1456           event->crossing.detail = GDK_NOTIFY_INFERIOR;
1457           break;
1458         case NotifyAncestor:
1459           event->crossing.detail = GDK_NOTIFY_ANCESTOR;
1460           break;
1461         case NotifyVirtual:
1462           event->crossing.detail = GDK_NOTIFY_VIRTUAL;
1463           break;
1464         case NotifyNonlinear:
1465           event->crossing.detail = GDK_NOTIFY_NONLINEAR;
1466           break;
1467         case NotifyNonlinearVirtual:
1468           event->crossing.detail = GDK_NOTIFY_NONLINEAR_VIRTUAL;
1469           break;
1470         default:
1471           event->crossing.detail = GDK_NOTIFY_UNKNOWN;
1472           break;
1473         }
1474       
1475       event->crossing.focus = xevent->xcrossing.focus;
1476       event->crossing.state = xevent->xcrossing.state;
1477       
1478       break;
1479       
1480       /* We only care about focus events that indicate that _this_
1481        * window (not a ancestor or child) got or lost the focus
1482        */
1483     case FocusIn:
1484       GDK_NOTE (EVENTS,
1485                 g_message ("focus in:\t\twindow: %ld, detail: %s, mode: %s",
1486                            xevent->xfocus.window,
1487                            notify_details[xevent->xfocus.detail],
1488                            notify_modes[xevent->xfocus.mode]));
1489       
1490       if (toplevel)
1491         {
1492           gboolean had_focus = HAS_FOCUS (toplevel);
1493           
1494           switch (xevent->xfocus.detail)
1495             {
1496             case NotifyAncestor:
1497             case NotifyVirtual:
1498               /* When the focus moves from an ancestor of the window to
1499                * the window or a descendent of the window, *and* the
1500                * pointer is inside the window, then we were previously
1501                * receiving keystroke events in the has_pointer_focus
1502                * case and are now receiving them in the
1503                * has_focus_window case.
1504                */
1505               if (toplevel->has_pointer &&
1506                   xevent->xfocus.mode != NotifyGrab &&
1507                   xevent->xfocus.mode != NotifyUngrab)
1508                 toplevel->has_pointer_focus = FALSE;
1509               
1510               /* fall through */
1511             case NotifyNonlinear:
1512             case NotifyNonlinearVirtual:
1513               if (xevent->xfocus.mode != NotifyGrab &&
1514                   xevent->xfocus.mode != NotifyUngrab)
1515                 toplevel->has_focus_window = TRUE;
1516               /* We pretend that the focus moves to the grab
1517                * window, so we pay attention to NotifyGrab
1518                * NotifyUngrab, and ignore NotifyWhileGrabbed
1519                */
1520               if (xevent->xfocus.mode != NotifyWhileGrabbed)
1521                 toplevel->has_focus = TRUE;
1522               break;
1523             case NotifyPointer:
1524               /* The X server sends NotifyPointer/NotifyGrab,
1525                * but the pointer focus is ignored while a
1526                * grab is in effect
1527                */
1528               if (xevent->xfocus.mode != NotifyGrab &&
1529                   xevent->xfocus.mode != NotifyUngrab)
1530                 toplevel->has_pointer_focus = TRUE;
1531               break;
1532             case NotifyInferior:
1533             case NotifyPointerRoot:
1534             case NotifyDetailNone:
1535               break;
1536             }
1537
1538           if (HAS_FOCUS (toplevel) != had_focus)
1539             generate_focus_event (window, TRUE);
1540         }
1541       break;
1542     case FocusOut:
1543       GDK_NOTE (EVENTS,
1544                 g_message ("focus out:\t\twindow: %ld, detail: %s, mode: %s",
1545                            xevent->xfocus.window,
1546                            notify_details[xevent->xfocus.detail],
1547                            notify_modes[xevent->xfocus.mode]));
1548       
1549       if (toplevel)
1550         {
1551           gboolean had_focus = HAS_FOCUS (toplevel);
1552             
1553           switch (xevent->xfocus.detail)
1554             {
1555             case NotifyAncestor:
1556             case NotifyVirtual:
1557               /* When the focus moves from the window or a descendent
1558                * of the window to an ancestor of the window, *and* the
1559                * pointer is inside the window, then we were previously
1560                * receiving keystroke events in the has_focus_window
1561                * case and are now receiving them in the
1562                * has_pointer_focus case.
1563                */
1564               if (toplevel->has_pointer &&
1565                   xevent->xfocus.mode != NotifyGrab &&
1566                   xevent->xfocus.mode != NotifyUngrab)
1567                 toplevel->has_pointer_focus = TRUE;
1568
1569               /* fall through */
1570             case NotifyNonlinear:
1571             case NotifyNonlinearVirtual:
1572               if (xevent->xfocus.mode != NotifyGrab &&
1573                   xevent->xfocus.mode != NotifyUngrab)
1574                 toplevel->has_focus_window = FALSE;
1575               if (xevent->xfocus.mode != NotifyWhileGrabbed)
1576                 toplevel->has_focus = FALSE;
1577               break;
1578             case NotifyPointer:
1579               if (xevent->xfocus.mode != NotifyGrab &&
1580                   xevent->xfocus.mode != NotifyUngrab)
1581                 toplevel->has_pointer_focus = FALSE;
1582             break;
1583             case NotifyInferior:
1584             case NotifyPointerRoot:
1585             case NotifyDetailNone:
1586               break;
1587             }
1588
1589           if (HAS_FOCUS (toplevel) != had_focus)
1590             generate_focus_event (window, FALSE);
1591         }
1592       break;
1593
1594 #if 0      
1595           /* gdk_keyboard_grab() causes following events. These events confuse
1596            * the XIM focus, so ignore them.
1597            */
1598           if (xevent->xfocus.mode == NotifyGrab ||
1599               xevent->xfocus.mode == NotifyUngrab)
1600             break;
1601 #endif
1602
1603     case KeymapNotify:
1604       GDK_NOTE (EVENTS,
1605                 g_message ("keymap notify"));
1606
1607       /* Not currently handled */
1608       return_val = FALSE;
1609       break;
1610       
1611     case Expose:
1612       GDK_NOTE (EVENTS,
1613                 g_message ("expose:\t\twindow: %ld  %d  x,y: %d %d  w,h: %d %d%s",
1614                            xevent->xexpose.window, xevent->xexpose.count,
1615                            xevent->xexpose.x, xevent->xexpose.y,
1616                            xevent->xexpose.width, xevent->xexpose.height,
1617                            event->any.send_event ? " (send)" : ""));
1618       
1619       if (window_private == NULL)
1620         {
1621           return_val = FALSE;
1622           break;
1623         }
1624       
1625       {
1626         GdkRectangle expose_rect;
1627
1628         expose_rect.x = xevent->xexpose.x;
1629         expose_rect.y = xevent->xexpose.y;
1630         expose_rect.width = xevent->xexpose.width;
1631         expose_rect.height = xevent->xexpose.height;
1632
1633         if (return_exposes)
1634           {
1635             event->expose.type = GDK_EXPOSE;
1636             event->expose.area = expose_rect;
1637             event->expose.region = gdk_region_rectangle (&expose_rect);
1638             event->expose.window = window;
1639             event->expose.count = xevent->xexpose.count;
1640
1641             return_val = TRUE;
1642           }
1643         else
1644           {
1645             _gdk_window_process_expose (window, xevent->xexpose.serial, &expose_rect);
1646             return_val = FALSE;
1647           }
1648         
1649         return_val = FALSE;
1650       }
1651         
1652       break;
1653       
1654     case GraphicsExpose:
1655       {
1656         GdkRectangle expose_rect;
1657
1658         GDK_NOTE (EVENTS,
1659                   g_message ("graphics expose:\tdrawable: %ld",
1660                              xevent->xgraphicsexpose.drawable));
1661  
1662         if (window_private == NULL)
1663           {
1664             return_val = FALSE;
1665             break;
1666           }
1667         
1668         expose_rect.x = xevent->xgraphicsexpose.x;
1669         expose_rect.y = xevent->xgraphicsexpose.y;
1670         expose_rect.width = xevent->xgraphicsexpose.width;
1671         expose_rect.height = xevent->xgraphicsexpose.height;
1672             
1673         if (return_exposes)
1674           {
1675             event->expose.type = GDK_EXPOSE;
1676             event->expose.area = expose_rect;
1677             event->expose.region = gdk_region_rectangle (&expose_rect);
1678             event->expose.window = window;
1679             event->expose.count = xevent->xgraphicsexpose.count;
1680
1681             return_val = TRUE;
1682           }
1683         else
1684           {
1685             _gdk_window_process_expose (window, xevent->xgraphicsexpose.serial, &expose_rect);
1686             
1687             return_val = FALSE;
1688           }
1689         
1690       }
1691       break;
1692       
1693     case NoExpose:
1694       GDK_NOTE (EVENTS,
1695                 g_message ("no expose:\t\tdrawable: %ld",
1696                            xevent->xnoexpose.drawable));
1697       
1698       event->no_expose.type = GDK_NO_EXPOSE;
1699       event->no_expose.window = window;
1700       
1701       break;
1702       
1703     case VisibilityNotify:
1704 #ifdef G_ENABLE_DEBUG
1705       if (_gdk_debug_flags & GDK_DEBUG_EVENTS)
1706         switch (xevent->xvisibility.state)
1707           {
1708           case VisibilityFullyObscured:
1709             g_message ("visibility notify:\twindow: %ld  none",
1710                        xevent->xvisibility.window);
1711             break;
1712           case VisibilityPartiallyObscured:
1713             g_message ("visibility notify:\twindow: %ld  partial",
1714                        xevent->xvisibility.window);
1715             break;
1716           case VisibilityUnobscured:
1717             g_message ("visibility notify:\twindow: %ld  full",
1718                        xevent->xvisibility.window);
1719             break;
1720           }
1721 #endif /* G_ENABLE_DEBUG */
1722       
1723       if (window_private == NULL)
1724         {
1725           return_val = FALSE;
1726           break;
1727         }
1728       
1729       event->visibility.type = GDK_VISIBILITY_NOTIFY;
1730       event->visibility.window = window;
1731       
1732       switch (xevent->xvisibility.state)
1733         {
1734         case VisibilityFullyObscured:
1735           event->visibility.state = GDK_VISIBILITY_FULLY_OBSCURED;
1736           break;
1737           
1738         case VisibilityPartiallyObscured:
1739           event->visibility.state = GDK_VISIBILITY_PARTIAL;
1740           break;
1741           
1742         case VisibilityUnobscured:
1743           event->visibility.state = GDK_VISIBILITY_UNOBSCURED;
1744           break;
1745         }
1746       
1747       break;
1748       
1749     case CreateNotify:
1750       GDK_NOTE (EVENTS,
1751                 g_message ("create notify:\twindow: %ld  x,y: %d %d     w,h: %d %d  b-w: %d  parent: %ld         ovr: %d",
1752                            xevent->xcreatewindow.window,
1753                            xevent->xcreatewindow.x,
1754                            xevent->xcreatewindow.y,
1755                            xevent->xcreatewindow.width,
1756                            xevent->xcreatewindow.height,
1757                            xevent->xcreatewindow.border_width,
1758                            xevent->xcreatewindow.parent,
1759                            xevent->xcreatewindow.override_redirect));
1760       /* not really handled */
1761       break;
1762       
1763     case DestroyNotify:
1764       GDK_NOTE (EVENTS,
1765                 g_message ("destroy notify:\twindow: %ld",
1766                            xevent->xdestroywindow.window));
1767
1768       /* Ignore DestroyNotify from SubstructureNotifyMask */
1769       if (xevent->xdestroywindow.window == xevent->xdestroywindow.event)
1770         {
1771           event->any.type = GDK_DESTROY;
1772           event->any.window = window;
1773           
1774           return_val = window_private && !GDK_WINDOW_DESTROYED (window);
1775           
1776           if (window && GDK_WINDOW_XID (window) != screen_x11->xroot_window)
1777             gdk_window_destroy_notify (window);
1778         }
1779       else
1780         return_val = FALSE;
1781       
1782       break;
1783       
1784     case UnmapNotify:
1785       GDK_NOTE (EVENTS,
1786                 g_message ("unmap notify:\t\twindow: %ld",
1787                            xevent->xmap.window));
1788       
1789       event->any.type = GDK_UNMAP;
1790       event->any.window = window;      
1791
1792       /* If we are shown (not withdrawn) and get an unmap, it means we
1793        * were iconified in the X sense. If we are withdrawn, and get
1794        * an unmap, it means we hid the window ourselves, so we
1795        * will have already flipped the iconified bit off.
1796        */
1797       if (window)
1798         {
1799           if (GDK_WINDOW_IS_MAPPED (window))
1800             gdk_synthesize_window_state (window,
1801                                          0,
1802                                          GDK_WINDOW_STATE_ICONIFIED);
1803
1804           _gdk_xgrab_check_unmap (window, xevent->xany.serial);
1805         }
1806       
1807       break;
1808       
1809     case MapNotify:
1810       GDK_NOTE (EVENTS,
1811                 g_message ("map notify:\t\twindow: %ld",
1812                            xevent->xmap.window));
1813       
1814       event->any.type = GDK_MAP;
1815       event->any.window = window;
1816
1817       /* Unset iconified if it was set */
1818       if (window && (((GdkWindowObject*)window)->state & GDK_WINDOW_STATE_ICONIFIED))
1819         gdk_synthesize_window_state (window,
1820                                      GDK_WINDOW_STATE_ICONIFIED,
1821                                      0);
1822       
1823       break;
1824       
1825     case ReparentNotify:
1826       GDK_NOTE (EVENTS,
1827                 g_message ("reparent notify:\twindow: %ld  x,y: %d %d  parent: %ld      ovr: %d",
1828                            xevent->xreparent.window,
1829                            xevent->xreparent.x,
1830                            xevent->xreparent.y,
1831                            xevent->xreparent.parent,
1832                            xevent->xreparent.override_redirect));
1833
1834       /* Not currently handled */
1835       return_val = FALSE;
1836       break;
1837       
1838     case ConfigureNotify:
1839       GDK_NOTE (EVENTS,
1840                 g_message ("configure notify:\twindow: %ld  x,y: %d %d  w,h: %d %d  b-w: %d  above: %ld  ovr: %d%s",
1841                            xevent->xconfigure.window,
1842                            xevent->xconfigure.x,
1843                            xevent->xconfigure.y,
1844                            xevent->xconfigure.width,
1845                            xevent->xconfigure.height,
1846                            xevent->xconfigure.border_width,
1847                            xevent->xconfigure.above,
1848                            xevent->xconfigure.override_redirect,
1849                            !window
1850                            ? " (discarding)"
1851                            : GDK_WINDOW_TYPE (window) == GDK_WINDOW_CHILD
1852                            ? " (discarding child)"
1853                            : xevent->xconfigure.event != xevent->xconfigure.window
1854                            ? " (discarding substructure)"
1855                            : ""));
1856       if (window && GDK_WINDOW_TYPE (window) == GDK_WINDOW_ROOT)
1857         { 
1858           window_private->width = xevent->xconfigure.width;
1859           window_private->height = xevent->xconfigure.height;
1860
1861           _gdk_x11_drawable_update_size (window_private->impl);
1862           _gdk_x11_screen_size_changed (screen, xevent);
1863         }
1864
1865       if (window &&
1866           xevent->xconfigure.event == xevent->xconfigure.window &&
1867           !GDK_WINDOW_DESTROYED (window) &&
1868           (window_private->extension_events != 0))
1869         _gdk_input_configure_event (&xevent->xconfigure, window);
1870       
1871 #ifdef HAVE_XSYNC
1872       if (toplevel && display_x11->use_sync && !XSyncValueIsZero (toplevel->pending_counter_value))
1873         {
1874           toplevel->current_counter_value = toplevel->pending_counter_value;
1875           XSyncIntToValue (&toplevel->pending_counter_value, 0);
1876         }
1877 #endif
1878
1879     if (!window ||
1880           xevent->xconfigure.event != xevent->xconfigure.window ||
1881           GDK_WINDOW_TYPE (window) == GDK_WINDOW_CHILD ||
1882           GDK_WINDOW_TYPE (window) == GDK_WINDOW_ROOT)
1883         return_val = FALSE;
1884       else
1885         {
1886           event->configure.type = GDK_CONFIGURE;
1887           event->configure.window = window;
1888           event->configure.width = xevent->xconfigure.width;
1889           event->configure.height = xevent->xconfigure.height;
1890           
1891           if (!xevent->xconfigure.send_event &&
1892               !xevent->xconfigure.override_redirect &&
1893               !GDK_WINDOW_DESTROYED (window))
1894             {
1895               gint tx = 0;
1896               gint ty = 0;
1897               Window child_window = 0;
1898
1899               gdk_error_trap_push ();
1900               if (XTranslateCoordinates (GDK_DRAWABLE_XDISPLAY (window),
1901                                          GDK_DRAWABLE_XID (window),
1902                                          screen_x11->xroot_window,
1903                                          0, 0,
1904                                          &tx, &ty,
1905                                          &child_window))
1906                 {
1907                   event->configure.x = tx;
1908                   event->configure.y = ty;
1909                 }
1910               gdk_error_trap_pop ();
1911             }
1912           else
1913             {
1914               event->configure.x = xevent->xconfigure.x;
1915               event->configure.y = xevent->xconfigure.y;
1916             }
1917           window_private->x = event->configure.x;
1918           window_private->y = event->configure.y;
1919           window_private->width = xevent->xconfigure.width;
1920           window_private->height = xevent->xconfigure.height;
1921           
1922           _gdk_window_update_size (window);
1923           _gdk_x11_drawable_update_size (window_private->impl);
1924           
1925           if (window_private->resize_count >= 1)
1926             {
1927               window_private->resize_count -= 1;
1928
1929               if (window_private->resize_count == 0)
1930                 _gdk_moveresize_configure_done (display, window);
1931             }
1932         }
1933       break;
1934       
1935     case PropertyNotify:
1936       GDK_NOTE (EVENTS,
1937                 g_message ("property notify:\twindow: %ld, atom(%ld): %s%s%s",
1938                            xevent->xproperty.window,
1939                            xevent->xproperty.atom,
1940                            "\"",
1941                            gdk_x11_get_xatom_name_for_display (display, xevent->xproperty.atom),
1942                            "\""));
1943
1944       if (window_private == NULL)
1945         {
1946           return_val = FALSE;
1947           break;
1948         }
1949
1950       /* We compare with the serial of the last time we mapped the
1951        * window to avoid refetching properties that we set ourselves
1952        */
1953       if (toplevel &&
1954           xevent->xproperty.serial >= toplevel->map_serial)
1955         {
1956           if (xevent->xproperty.atom == gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_STATE"))
1957             gdk_check_wm_state_changed (window);
1958           
1959           if (xevent->xproperty.atom == gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_DESKTOP"))
1960             gdk_check_wm_desktop_changed (window);
1961         }
1962       
1963       if (window_private->event_mask & GDK_PROPERTY_CHANGE_MASK) 
1964         {
1965           event->property.type = GDK_PROPERTY_NOTIFY;
1966           event->property.window = window;
1967           event->property.atom = gdk_x11_xatom_to_atom_for_display (display, xevent->xproperty.atom);
1968           event->property.time = xevent->xproperty.time;
1969           event->property.state = xevent->xproperty.state;
1970         }
1971       else
1972         return_val = FALSE;
1973
1974       break;
1975       
1976     case SelectionClear:
1977       GDK_NOTE (EVENTS,
1978                 g_message ("selection clear:\twindow: %ld",
1979                            xevent->xproperty.window));
1980
1981       if (_gdk_selection_filter_clear_event (&xevent->xselectionclear))
1982         {
1983           event->selection.type = GDK_SELECTION_CLEAR;
1984           event->selection.window = window;
1985           event->selection.selection = gdk_x11_xatom_to_atom_for_display (display, xevent->xselectionclear.selection);
1986           event->selection.time = xevent->xselectionclear.time;
1987         }
1988       else
1989         return_val = FALSE;
1990           
1991       break;
1992       
1993     case SelectionRequest:
1994       GDK_NOTE (EVENTS,
1995                 g_message ("selection request:\twindow: %ld",
1996                            xevent->xproperty.window));
1997       
1998       event->selection.type = GDK_SELECTION_REQUEST;
1999       event->selection.window = window;
2000       event->selection.selection = gdk_x11_xatom_to_atom_for_display (display, xevent->xselectionrequest.selection);
2001       event->selection.target = gdk_x11_xatom_to_atom_for_display (display, xevent->xselectionrequest.target);
2002       event->selection.property = gdk_x11_xatom_to_atom_for_display (display, xevent->xselectionrequest.property);
2003       event->selection.requestor = xevent->xselectionrequest.requestor;
2004       event->selection.time = xevent->xselectionrequest.time;
2005       
2006       break;
2007       
2008     case SelectionNotify:
2009       GDK_NOTE (EVENTS,
2010                 g_message ("selection notify:\twindow: %ld",
2011                            xevent->xproperty.window));
2012       
2013       
2014       event->selection.type = GDK_SELECTION_NOTIFY;
2015       event->selection.window = window;
2016       event->selection.selection = gdk_x11_xatom_to_atom_for_display (display, xevent->xselection.selection);
2017       event->selection.target = gdk_x11_xatom_to_atom_for_display (display, xevent->xselection.target);
2018       event->selection.property = gdk_x11_xatom_to_atom_for_display (display, xevent->xselection.property);
2019       event->selection.time = xevent->xselection.time;
2020       
2021       break;
2022       
2023     case ColormapNotify:
2024       GDK_NOTE (EVENTS,
2025                 g_message ("colormap notify:\twindow: %ld",
2026                            xevent->xcolormap.window));
2027       
2028       /* Not currently handled */
2029       return_val = FALSE;
2030       break;
2031       
2032     case ClientMessage:
2033       {
2034         GList *tmp_list;
2035         GdkFilterReturn result = GDK_FILTER_CONTINUE;
2036         GdkAtom message_type = gdk_x11_xatom_to_atom_for_display (display, xevent->xclient.message_type);
2037
2038         GDK_NOTE (EVENTS,
2039                   g_message ("client message:\twindow: %ld",
2040                              xevent->xclient.window));
2041         
2042         tmp_list = display_x11->client_filters;
2043         while (tmp_list)
2044           {
2045             GdkClientFilter *filter = tmp_list->data;
2046             tmp_list = tmp_list->next;
2047             
2048             if (filter->type == message_type)
2049               {
2050                 result = (*filter->function) (xevent, event, filter->data);
2051                 if (result != GDK_FILTER_CONTINUE)
2052                   break;
2053               }
2054           }
2055
2056         switch (result)
2057           {
2058           case GDK_FILTER_REMOVE:
2059             return_val = FALSE;
2060             break;
2061           case GDK_FILTER_TRANSLATE:
2062             return_val = TRUE;
2063             break;
2064           case GDK_FILTER_CONTINUE:
2065             /* Send unknown ClientMessage's on to Gtk for it to use */
2066             if (window_private == NULL)
2067               {
2068                 return_val = FALSE;
2069               }
2070             else
2071               {
2072                 event->client.type = GDK_CLIENT_EVENT;
2073                 event->client.window = window;
2074                 event->client.message_type = message_type;
2075                 event->client.data_format = xevent->xclient.format;
2076                 memcpy(&event->client.data, &xevent->xclient.data,
2077                        sizeof(event->client.data));
2078               }
2079             break;
2080           }
2081       }
2082       
2083       break;
2084       
2085     case MappingNotify:
2086       GDK_NOTE (EVENTS,
2087                 g_message ("mapping notify"));
2088       
2089       /* Let XLib know that there is a new keyboard mapping.
2090        */
2091       XRefreshKeyboardMapping (&xevent->xmapping);
2092       _gdk_keymap_keys_changed (display);
2093       return_val = FALSE;
2094       break;
2095
2096     default:
2097 #ifdef HAVE_XKB
2098       if (xevent->type == display_x11->xkb_event_type)
2099         {
2100           XkbEvent *xkb_event = (XkbEvent *)xevent;
2101
2102           switch (xkb_event->any.xkb_type)
2103             {
2104             case XkbNewKeyboardNotify:
2105             case XkbMapNotify:
2106               _gdk_keymap_keys_changed (display);
2107
2108               return_val = FALSE;
2109               break;
2110               
2111             case XkbStateNotify:
2112               _gdk_keymap_state_changed (display, xevent);
2113               break;
2114             }
2115         }
2116       else
2117 #endif
2118 #ifdef HAVE_XFIXES
2119       if (xevent->type - display_x11->xfixes_event_base == XFixesSelectionNotify)
2120         {
2121           XFixesSelectionNotifyEvent *selection_notify = (XFixesSelectionNotifyEvent *)xevent;
2122
2123           _gdk_x11_screen_process_owner_change (screen, xevent);
2124           
2125           event->owner_change.type = GDK_OWNER_CHANGE;
2126           event->owner_change.window = window;
2127           event->owner_change.owner = selection_notify->owner;
2128           event->owner_change.reason = selection_notify->subtype;
2129           event->owner_change.selection = 
2130             gdk_x11_xatom_to_atom_for_display (display, 
2131                                                selection_notify->selection);
2132           event->owner_change.time = selection_notify->timestamp;
2133           event->owner_change.selection_time = selection_notify->selection_timestamp;
2134           
2135           return_val = TRUE;
2136         }
2137       else
2138 #endif
2139 #ifdef HAVE_RANDR
2140       if (xevent->type - display_x11->xrandr_event_base == RRScreenChangeNotify ||
2141           xevent->type - display_x11->xrandr_event_base == RRNotify)
2142         {
2143           if (screen)
2144             _gdk_x11_screen_size_changed (screen, xevent);
2145         }
2146       else
2147 #endif
2148 #if defined(HAVE_XCOMPOSITE) && defined (HAVE_XDAMAGE) && defined (HAVE_XFIXES)
2149       if (display_x11->have_xdamage && window_private && window_private->composited &&
2150           xevent->type == display_x11->xdamage_event_base + XDamageNotify &&
2151           ((XDamageNotifyEvent *) xevent)->damage == window_impl->damage)
2152         {
2153           XDamageNotifyEvent *damage_event = (XDamageNotifyEvent *) xevent;
2154           XserverRegion repair;
2155           GdkRectangle rect;
2156
2157           rect.x = window_private->x + damage_event->area.x;
2158           rect.y = window_private->y + damage_event->area.y;
2159           rect.width = damage_event->area.width;
2160           rect.height = damage_event->area.height;
2161
2162           repair = XFixesCreateRegion (display_x11->xdisplay,
2163                                        &damage_event->area, 1);
2164           XDamageSubtract (display_x11->xdisplay,
2165                            window_impl->damage,
2166                            repair, None);
2167           XFixesDestroyRegion (display_x11->xdisplay, repair);
2168
2169           if (window_private->parent != NULL)
2170             _gdk_window_process_expose (GDK_WINDOW (window_private->parent),
2171                                         damage_event->serial, &rect);
2172
2173           return_val = TRUE;
2174         }
2175       else
2176 #endif
2177         {
2178           /* something else - (e.g., a Xinput event) */
2179           
2180           if (window_private &&
2181               !GDK_WINDOW_DESTROYED (window_private) &&
2182               (window_private->extension_events != 0))
2183             return_val = _gdk_input_other_event(event, xevent, window);
2184           else
2185             return_val = FALSE;
2186           
2187           break;
2188         }
2189     }
2190
2191  done:
2192   if (return_val)
2193     {
2194       if (event->any.window)
2195         g_object_ref (event->any.window);
2196       if (((event->any.type == GDK_ENTER_NOTIFY) ||
2197            (event->any.type == GDK_LEAVE_NOTIFY)) &&
2198           (event->crossing.subwindow != NULL))
2199         g_object_ref (event->crossing.subwindow);
2200     }
2201   else
2202     {
2203       /* Mark this event as having no resources to be freed */
2204       event->any.window = NULL;
2205       event->any.type = GDK_NOTHING;
2206     }
2207   
2208   if (window)
2209     g_object_unref (window);
2210   
2211   return return_val;
2212 }
2213
2214 static GdkFilterReturn
2215 gdk_wm_protocols_filter (GdkXEvent *xev,
2216                          GdkEvent  *event,
2217                          gpointer data)
2218 {
2219   XEvent *xevent = (XEvent *)xev;
2220   GdkWindow *win = event->any.window;
2221   GdkDisplay *display;
2222   Atom atom;
2223
2224   if (!win)
2225       return GDK_FILTER_REMOVE;    
2226
2227   display = GDK_WINDOW_DISPLAY (win);
2228   atom = (Atom)xevent->xclient.data.l[0];
2229
2230   if (atom == gdk_x11_get_xatom_by_name_for_display (display, "WM_DELETE_WINDOW"))
2231     {
2232   /* The delete window request specifies a window
2233    *  to delete. We don't actually destroy the
2234    *  window because "it is only a request". (The
2235    *  window might contain vital data that the
2236    *  program does not want destroyed). Instead
2237    *  the event is passed along to the program,
2238    *  which should then destroy the window.
2239    */
2240       GDK_NOTE (EVENTS,
2241                 g_message ("delete window:\t\twindow: %ld",
2242                            xevent->xclient.window));
2243       
2244       event->any.type = GDK_DELETE;
2245
2246       gdk_x11_window_set_user_time (win, xevent->xclient.data.l[1]);
2247
2248       return GDK_FILTER_TRANSLATE;
2249     }
2250   else if (atom == gdk_x11_get_xatom_by_name_for_display (display, "WM_TAKE_FOCUS"))
2251     {
2252       GdkToplevelX11 *toplevel = _gdk_x11_window_get_toplevel (event->any.window);
2253       GdkWindowObject *private = (GdkWindowObject *)win;
2254
2255       /* There is no way of knowing reliably whether we are viewable;
2256        * _gdk_x11_set_input_focus_safe() traps errors asynchronously.
2257        */
2258       if (toplevel && private->accept_focus)
2259         _gdk_x11_set_input_focus_safe (display, toplevel->focus_window,
2260                                        RevertToParent,
2261                                        xevent->xclient.data.l[1]);
2262
2263       return GDK_FILTER_REMOVE;
2264     }
2265   else if (atom == gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_PING") &&
2266            !_gdk_x11_display_is_root_window (display,
2267                                              xevent->xclient.window))
2268     {
2269       XClientMessageEvent xclient = xevent->xclient;
2270       
2271       xclient.window = GDK_WINDOW_XROOTWIN (win);
2272       XSendEvent (GDK_WINDOW_XDISPLAY (win), 
2273                   xclient.window,
2274                   False, 
2275                   SubstructureRedirectMask | SubstructureNotifyMask, (XEvent *)&xclient);
2276
2277       return GDK_FILTER_REMOVE;
2278     }
2279   else if (atom == gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_SYNC_REQUEST") &&
2280            GDK_DISPLAY_X11 (display)->use_sync)
2281     {
2282       GdkToplevelX11 *toplevel = _gdk_x11_window_get_toplevel (event->any.window);
2283       if (toplevel)
2284         {
2285 #ifdef HAVE_XSYNC
2286           XSyncIntsToValue (&toplevel->pending_counter_value, 
2287                             xevent->xclient.data.l[2], 
2288                             xevent->xclient.data.l[3]);
2289 #endif
2290         }
2291       return GDK_FILTER_REMOVE;
2292     }
2293   
2294   return GDK_FILTER_CONTINUE;
2295 }
2296
2297 void
2298 _gdk_events_queue (GdkDisplay *display)
2299 {
2300   GList *node;
2301   GdkEvent *event;
2302   XEvent xevent;
2303   Display *xdisplay = GDK_DISPLAY_XDISPLAY (display);
2304
2305   while (!_gdk_event_queue_find_first(display) && XPending (xdisplay))
2306     {
2307       XNextEvent (xdisplay, &xevent);
2308
2309       switch (xevent.type)
2310         {
2311         case KeyPress:
2312         case KeyRelease:
2313           break;
2314         default:
2315           if (XFilterEvent (&xevent, None))
2316             continue;
2317         }
2318       
2319       event = gdk_event_new (GDK_NOTHING);
2320       
2321       event->any.window = NULL;
2322       event->any.send_event = xevent.xany.send_event ? TRUE : FALSE;
2323
2324       ((GdkEventPrivate *)event)->flags |= GDK_EVENT_PENDING;
2325
2326       node = _gdk_event_queue_append (display, event);
2327
2328       if (gdk_event_translate (display, event, &xevent, FALSE))
2329         {
2330           ((GdkEventPrivate *)event)->flags &= ~GDK_EVENT_PENDING;
2331           _gdk_windowing_got_event (display, node, event, xevent.xany.serial);
2332         }
2333       else
2334         {
2335           _gdk_event_queue_remove_link (display, node);
2336           g_list_free_1 (node);
2337           gdk_event_free (event);
2338         }
2339     }
2340 }
2341
2342 static gboolean  
2343 gdk_event_prepare (GSource  *source,
2344                    gint     *timeout)
2345 {
2346   GdkDisplay *display = ((GdkDisplaySource*)source)->display;
2347   gboolean retval;
2348   
2349   GDK_THREADS_ENTER ();
2350
2351   *timeout = -1;
2352   retval = (_gdk_event_queue_find_first (display) != NULL || 
2353             gdk_check_xpending (display));
2354   
2355   GDK_THREADS_LEAVE ();
2356
2357   return retval;
2358 }
2359
2360 static gboolean  
2361 gdk_event_check (GSource *source) 
2362 {
2363   GdkDisplaySource *display_source = (GdkDisplaySource*)source;
2364   gboolean retval;
2365
2366   GDK_THREADS_ENTER ();
2367
2368   if (display_source->event_poll_fd.revents & G_IO_IN)
2369     retval = (_gdk_event_queue_find_first (display_source->display) != NULL || 
2370               gdk_check_xpending (display_source->display));
2371   else
2372     retval = FALSE;
2373
2374   GDK_THREADS_LEAVE ();
2375
2376   return retval;
2377 }
2378
2379 static gboolean  
2380 gdk_event_dispatch (GSource    *source,
2381                     GSourceFunc callback,
2382                     gpointer    user_data)
2383 {
2384   GdkDisplay *display = ((GdkDisplaySource*)source)->display;
2385   GdkEvent *event;
2386  
2387   GDK_THREADS_ENTER ();
2388
2389   _gdk_events_queue (display);
2390   event = _gdk_event_unqueue (display);
2391
2392   if (event)
2393     {
2394       if (_gdk_event_func)
2395         (*_gdk_event_func) (event, _gdk_event_data);
2396       
2397       gdk_event_free (event);
2398     }
2399   
2400   GDK_THREADS_LEAVE ();
2401
2402   return TRUE;
2403 }
2404
2405 /**
2406  * gdk_event_send_client_message_for_display:
2407  * @display: the #GdkDisplay for the window where the message is to be sent.
2408  * @event: the #GdkEvent to send, which should be a #GdkEventClient.
2409  * @winid: the window to send the client message to.
2410  *
2411  * On X11, sends an X ClientMessage event to a given window. On
2412  * Windows, sends a message registered with the name
2413  * GDK_WIN32_CLIENT_MESSAGE.
2414  *
2415  * This could be used for communicating between different
2416  * applications, though the amount of data is limited to 20 bytes on
2417  * X11, and to just four bytes on Windows.
2418  *
2419  * Returns: non-zero on success.
2420  *
2421  * Since: 2.2
2422  */
2423 gboolean
2424 gdk_event_send_client_message_for_display (GdkDisplay     *display,
2425                                            GdkEvent       *event,
2426                                            GdkNativeWindow winid)
2427 {
2428   XEvent sev;
2429   
2430   g_return_val_if_fail(event != NULL, FALSE);
2431
2432   /* Set up our event to send, with the exception of its target window */
2433   sev.xclient.type = ClientMessage;
2434   sev.xclient.display = GDK_DISPLAY_XDISPLAY (display);
2435   sev.xclient.format = event->client.data_format;
2436   sev.xclient.window = winid;
2437   memcpy(&sev.xclient.data, &event->client.data, sizeof (sev.xclient.data));
2438   sev.xclient.message_type = gdk_x11_atom_to_xatom_for_display (display, event->client.message_type);
2439   
2440   return _gdk_send_xevent (display, winid, False, NoEventMask, &sev);
2441 }
2442
2443
2444
2445 /* Sends a ClientMessage to all toplevel client windows */
2446 static gboolean
2447 gdk_event_send_client_message_to_all_recurse (GdkDisplay *display,
2448                                               XEvent     *xev, 
2449                                               guint32     xid,
2450                                               guint       level)
2451 {
2452   Atom type = None;
2453   int format;
2454   unsigned long nitems, after;
2455   unsigned char *data;
2456   Window *ret_children, ret_root, ret_parent;
2457   unsigned int ret_nchildren;
2458   gboolean send = FALSE;
2459   gboolean found = FALSE;
2460   gboolean result = FALSE;
2461   int i;
2462   
2463   gdk_error_trap_push ();
2464   
2465   if (XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), xid, 
2466                           gdk_x11_get_xatom_by_name_for_display (display, "WM_STATE"),
2467                           0, 0, False, AnyPropertyType,
2468                           &type, &format, &nitems, &after, &data) != Success)
2469     goto out;
2470   
2471   if (type)
2472     {
2473       send = TRUE;
2474       XFree (data);
2475     }
2476   else
2477     {
2478       /* OK, we're all set, now let's find some windows to send this to */
2479       if (!XQueryTree (GDK_DISPLAY_XDISPLAY (display), xid,
2480                       &ret_root, &ret_parent,
2481                       &ret_children, &ret_nchildren))   
2482         goto out;
2483
2484       for(i = 0; i < ret_nchildren; i++)
2485         if (gdk_event_send_client_message_to_all_recurse (display, xev, ret_children[i], level + 1))
2486           found = TRUE;
2487
2488       XFree (ret_children);
2489     }
2490
2491   if (send || (!found && (level == 1)))
2492     {
2493       xev->xclient.window = xid;
2494       _gdk_send_xevent (display, xid, False, NoEventMask, xev);
2495     }
2496
2497   result = send || found;
2498
2499  out:
2500   gdk_error_trap_pop ();
2501
2502   return result;
2503 }
2504
2505 /**
2506  * gdk_screen_broadcast_client_message:
2507  * @screen: the #GdkScreen where the event will be broadcasted.
2508  * @event: the #GdkEvent.
2509  *
2510  * On X11, sends an X ClientMessage event to all toplevel windows on
2511  * @screen. 
2512  *
2513  * Toplevel windows are determined by checking for the WM_STATE property, 
2514  * as described in the Inter-Client Communication Conventions Manual (ICCCM).
2515  * If no windows are found with the WM_STATE property set, the message is 
2516  * sent to all children of the root window.
2517  *
2518  * On Windows, broadcasts a message registered with the name
2519  * GDK_WIN32_CLIENT_MESSAGE to all top-level windows. The amount of
2520  * data is limited to one long, i.e. four bytes.
2521  *
2522  * Since: 2.2
2523  */
2524
2525 void
2526 gdk_screen_broadcast_client_message (GdkScreen *screen, 
2527                                      GdkEvent  *event)
2528 {
2529   XEvent sev;
2530   GdkWindow *root_window;
2531
2532   g_return_if_fail (event != NULL);
2533   
2534   root_window = gdk_screen_get_root_window (screen);
2535   
2536   /* Set up our event to send, with the exception of its target window */
2537   sev.xclient.type = ClientMessage;
2538   sev.xclient.display = GDK_WINDOW_XDISPLAY (root_window);
2539   sev.xclient.format = event->client.data_format;
2540   memcpy(&sev.xclient.data, &event->client.data, sizeof (sev.xclient.data));
2541   sev.xclient.message_type = 
2542     gdk_x11_atom_to_xatom_for_display (GDK_WINDOW_DISPLAY (root_window),
2543                                        event->client.message_type);
2544
2545   gdk_event_send_client_message_to_all_recurse (gdk_screen_get_display (screen),
2546                                                 &sev, 
2547                                                 GDK_WINDOW_XID (root_window), 
2548                                                 0);
2549 }
2550
2551 /*
2552  *--------------------------------------------------------------
2553  * gdk_flush
2554  *
2555  *   Flushes the Xlib output buffer and then waits
2556  *   until all requests have been received and processed
2557  *   by the X server. The only real use for this function
2558  *   is in dealing with XShm.
2559  *
2560  * Arguments:
2561  *
2562  * Results:
2563  *
2564  * Side effects:
2565  *
2566  *--------------------------------------------------------------
2567  */
2568
2569 void
2570 gdk_flush (void)
2571 {
2572   GSList *tmp_list = _gdk_displays;
2573   
2574   while (tmp_list)
2575     {
2576       XSync (GDK_DISPLAY_XDISPLAY (tmp_list->data), False);
2577       tmp_list = tmp_list->next;
2578     }
2579 }
2580
2581 static Bool
2582 timestamp_predicate (Display *display,
2583                      XEvent  *xevent,
2584                      XPointer arg)
2585 {
2586   Window xwindow = GPOINTER_TO_UINT (arg);
2587   GdkDisplay *gdk_display = gdk_x11_lookup_xdisplay (display);
2588
2589   if (xevent->type == PropertyNotify &&
2590       xevent->xproperty.window == xwindow &&
2591       xevent->xproperty.atom == gdk_x11_get_xatom_by_name_for_display (gdk_display,
2592                                                                        "GDK_TIMESTAMP_PROP"))
2593     return True;
2594
2595   return False;
2596 }
2597
2598 /**
2599  * gdk_x11_get_server_time:
2600  * @window: a #GdkWindow, used for communication with the server.
2601  *          The window must have GDK_PROPERTY_CHANGE_MASK in its
2602  *          events mask or a hang will result.
2603  * 
2604  * Routine to get the current X server time stamp. 
2605  * 
2606  * Return value: the time stamp.
2607  **/
2608 guint32
2609 gdk_x11_get_server_time (GdkWindow *window)
2610 {
2611   Display *xdisplay;
2612   Window   xwindow;
2613   guchar c = 'a';
2614   XEvent xevent;
2615   Atom timestamp_prop_atom;
2616
2617   g_return_val_if_fail (GDK_IS_WINDOW (window), 0);
2618   g_return_val_if_fail (!GDK_WINDOW_DESTROYED (window), 0);
2619
2620   xdisplay = GDK_WINDOW_XDISPLAY (window);
2621   xwindow = GDK_WINDOW_XWINDOW (window);
2622   timestamp_prop_atom = 
2623     gdk_x11_get_xatom_by_name_for_display (GDK_WINDOW_DISPLAY (window),
2624                                            "GDK_TIMESTAMP_PROP");
2625   
2626   XChangeProperty (xdisplay, xwindow, timestamp_prop_atom,
2627                    timestamp_prop_atom,
2628                    8, PropModeReplace, &c, 1);
2629
2630   XIfEvent (xdisplay, &xevent,
2631             timestamp_predicate, GUINT_TO_POINTER(xwindow));
2632
2633   return xevent.xproperty.time;
2634 }
2635
2636 static void
2637 fetch_net_wm_check_window (GdkScreen *screen)
2638 {
2639   GdkScreenX11 *screen_x11;
2640   GdkDisplay *display;
2641   Atom type;
2642   gint format;
2643   gulong n_items;
2644   gulong bytes_after;
2645   guchar *data;
2646   Window *xwindow;
2647   GTimeVal tv;
2648   
2649   screen_x11 = GDK_SCREEN_X11 (screen);
2650   display = screen_x11->display;
2651
2652   g_return_if_fail (GDK_DISPLAY_X11 (display)->trusted_client);
2653   
2654   g_get_current_time (&tv);
2655       
2656   if (ABS  (tv.tv_sec - screen_x11->last_wmspec_check_time) < 15)
2657     return; /* we've checked recently */
2658
2659   screen_x11->last_wmspec_check_time = tv.tv_sec;
2660
2661   data = NULL;
2662   XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), screen_x11->xroot_window,
2663                       gdk_x11_get_xatom_by_name_for_display (display, "_NET_SUPPORTING_WM_CHECK"),
2664                       0, G_MAXLONG, False, XA_WINDOW, &type, &format, 
2665                       &n_items, &bytes_after, &data);
2666   
2667   if (type != XA_WINDOW)
2668     {
2669       if (data)
2670         XFree (data);
2671       return;
2672     }
2673
2674   xwindow = (Window *)data;
2675
2676   if (screen_x11->wmspec_check_window == *xwindow)
2677     {
2678       XFree (xwindow);
2679       return;
2680     }
2681
2682   gdk_error_trap_push ();
2683   
2684   /* Find out if this WM goes away, so we can reset everything. */
2685   XSelectInput (screen_x11->xdisplay, *xwindow, StructureNotifyMask);
2686   gdk_display_sync (display);
2687
2688   if (gdk_error_trap_pop () == Success)
2689     {
2690       screen_x11->wmspec_check_window = *xwindow;
2691       screen_x11->need_refetch_net_supported = TRUE;
2692       screen_x11->need_refetch_wm_name = TRUE;
2693       
2694       /* Careful, reentrancy */
2695       _gdk_x11_screen_window_manager_changed (GDK_SCREEN (screen_x11));
2696     }
2697
2698   XFree (xwindow);    
2699 }
2700
2701 /**
2702  * gdk_x11_screen_get_window_manager_name:
2703  * @screen: a #GdkScreen 
2704  * 
2705  * Returns the name of the window manager for @screen. 
2706  * 
2707  * Return value: the name of the window manager screen @screen, or 
2708  * "unknown" if the window manager is unknown. The string is owned by GDK
2709  * and should not be freed.
2710  *
2711  * Since: 2.2
2712  **/
2713 const char*
2714 gdk_x11_screen_get_window_manager_name (GdkScreen *screen)
2715 {
2716   GdkScreenX11 *screen_x11;
2717
2718   screen_x11 = GDK_SCREEN_X11 (screen);
2719   
2720   if (!G_LIKELY (GDK_DISPLAY_X11 (screen_x11->display)->trusted_client))
2721     return screen_x11->window_manager_name;
2722
2723   fetch_net_wm_check_window (screen);
2724
2725   if (screen_x11->need_refetch_wm_name)
2726     {
2727       /* Get the name of the window manager */
2728       screen_x11->need_refetch_wm_name = FALSE;
2729
2730       g_free (screen_x11->window_manager_name);
2731       screen_x11->window_manager_name = g_strdup ("unknown");
2732       
2733       if (screen_x11->wmspec_check_window != None)
2734         {
2735           Atom type;
2736           gint format;
2737           gulong n_items;
2738           gulong bytes_after;
2739           gchar *name;
2740           
2741           name = NULL;
2742
2743           gdk_error_trap_push ();
2744           
2745           XGetWindowProperty (GDK_DISPLAY_XDISPLAY (screen_x11->display),
2746                               screen_x11->wmspec_check_window,
2747                               gdk_x11_get_xatom_by_name_for_display (screen_x11->display,
2748                                                                      "_NET_WM_NAME"),
2749                               0, G_MAXLONG, False,
2750                               gdk_x11_get_xatom_by_name_for_display (screen_x11->display,
2751                                                                      "UTF8_STRING"),
2752                               &type, &format, 
2753                               &n_items, &bytes_after,
2754                               (guchar **)&name);
2755           
2756           gdk_display_sync (screen_x11->display);
2757           
2758           gdk_error_trap_pop ();
2759           
2760           if (name != NULL)
2761             {
2762               g_free (screen_x11->window_manager_name);
2763               screen_x11->window_manager_name = g_strdup (name);
2764               XFree (name);
2765             }
2766         }
2767     }
2768   
2769   return GDK_SCREEN_X11 (screen)->window_manager_name;
2770 }
2771
2772 typedef struct _NetWmSupportedAtoms NetWmSupportedAtoms;
2773
2774 struct _NetWmSupportedAtoms
2775 {
2776   Atom *atoms;
2777   gulong n_atoms;
2778 };
2779
2780 static void
2781 cleanup_atoms(gpointer data)
2782 {
2783   NetWmSupportedAtoms *supported_atoms = data;
2784   if (supported_atoms->atoms)
2785       XFree (supported_atoms->atoms);
2786   g_free (supported_atoms);
2787 }
2788
2789 /**
2790  * gdk_x11_screen_supports_net_wm_hint:
2791  * @screen: the relevant #GdkScreen.
2792  * @property: a property atom.
2793  * 
2794  * This function is specific to the X11 backend of GDK, and indicates
2795  * whether the window manager supports a certain hint from the
2796  * Extended Window Manager Hints Specification. You can find this
2797  * specification on 
2798  * <ulink url="http://www.freedesktop.org">http://www.freedesktop.org</ulink>.
2799  *
2800  * When using this function, keep in mind that the window manager
2801  * can change over time; so you shouldn't use this function in
2802  * a way that impacts persistent application state. A common bug
2803  * is that your application can start up before the window manager
2804  * does when the user logs in, and before the window manager starts
2805  * gdk_x11_screen_supports_net_wm_hint() will return %FALSE for every property.
2806  * You can monitor the window_manager_changed signal on #GdkScreen to detect
2807  * a window manager change.
2808  * 
2809  * Return value: %TRUE if the window manager supports @property
2810  *
2811  * Since: 2.2
2812  **/
2813 gboolean
2814 gdk_x11_screen_supports_net_wm_hint (GdkScreen *screen,
2815                                      GdkAtom    property)
2816 {
2817   gulong i;
2818   GdkScreenX11 *screen_x11;
2819   NetWmSupportedAtoms *supported_atoms;
2820   GdkDisplay *display;
2821
2822   g_return_val_if_fail (GDK_IS_SCREEN (screen), FALSE);
2823   
2824   screen_x11 = GDK_SCREEN_X11 (screen);
2825   display = screen_x11->display;
2826
2827   if (!G_LIKELY (GDK_DISPLAY_X11 (display)->trusted_client))
2828     return FALSE;
2829
2830   supported_atoms = g_object_get_data (G_OBJECT (screen), "gdk-net-wm-supported-atoms");
2831   if (!supported_atoms)
2832     {
2833       supported_atoms = g_new0 (NetWmSupportedAtoms, 1);
2834       g_object_set_data_full (G_OBJECT (screen), "gdk-net-wm-supported-atoms", supported_atoms, cleanup_atoms);
2835     }
2836
2837   fetch_net_wm_check_window (screen);
2838
2839   if (screen_x11->wmspec_check_window == None)
2840     return FALSE;
2841   
2842   if (screen_x11->need_refetch_net_supported)
2843     {
2844       /* WM has changed since we last got the supported list,
2845        * refetch it.
2846        */
2847       Atom type;
2848       gint format;
2849       gulong bytes_after;
2850
2851       screen_x11->need_refetch_net_supported = FALSE;
2852       
2853       if (supported_atoms->atoms)
2854         XFree (supported_atoms->atoms);
2855       
2856       supported_atoms->atoms = NULL;
2857       supported_atoms->n_atoms = 0;
2858       
2859       XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), screen_x11->xroot_window,
2860                           gdk_x11_get_xatom_by_name_for_display (display, "_NET_SUPPORTED"),
2861                           0, G_MAXLONG, False, XA_ATOM, &type, &format, 
2862                           &supported_atoms->n_atoms, &bytes_after,
2863                           (guchar **)&supported_atoms->atoms);
2864       
2865       if (type != XA_ATOM)
2866         return FALSE;
2867     }
2868   
2869   if (supported_atoms->atoms == NULL)
2870     return FALSE;
2871   
2872   i = 0;
2873   while (i < supported_atoms->n_atoms)
2874     {
2875       if (supported_atoms->atoms[i] == gdk_x11_atom_to_xatom_for_display (display, property))
2876         return TRUE;
2877       
2878       ++i;
2879     }
2880   
2881   return FALSE;
2882 }
2883
2884 /**
2885  * gdk_net_wm_supports:
2886  * @property: a property atom.
2887  * 
2888  * This function is specific to the X11 backend of GDK, and indicates
2889  * whether the window manager for the default screen supports a certain
2890  * hint from the Extended Window Manager Hints Specification. See
2891  * gdk_x11_screen_supports_net_wm_hint() for complete details.
2892  * 
2893  * Return value: %TRUE if the window manager supports @property
2894  **/
2895 gboolean
2896 gdk_net_wm_supports (GdkAtom property)
2897 {
2898   return gdk_x11_screen_supports_net_wm_hint (gdk_screen_get_default (), property);
2899 }
2900
2901
2902 static void
2903 gdk_xsettings_notify_cb (const char       *name,
2904                          XSettingsAction   action,
2905                          XSettingsSetting *setting,
2906                          void             *data)
2907 {
2908   GdkEvent new_event;
2909   GdkScreen *screen = data;
2910   GdkScreenX11 *screen_x11 = data;
2911   int i;
2912
2913   if (screen_x11->xsettings_in_init)
2914     return;
2915   
2916   new_event.type = GDK_SETTING;
2917   new_event.setting.window = gdk_screen_get_root_window (screen);
2918   new_event.setting.send_event = FALSE;
2919   new_event.setting.name = NULL;
2920
2921   for (i = 0; i < GDK_SETTINGS_N_ELEMENTS() ; i++)
2922     if (strcmp (GDK_SETTINGS_X_NAME (i), name) == 0)
2923       {
2924         new_event.setting.name = (char*) GDK_SETTINGS_GDK_NAME (i);
2925         break;
2926       }
2927   
2928   if (!new_event.setting.name)
2929     return;
2930   
2931   switch (action)
2932     {
2933     case XSETTINGS_ACTION_NEW:
2934       new_event.setting.action = GDK_SETTING_ACTION_NEW;
2935       break;
2936     case XSETTINGS_ACTION_CHANGED:
2937       new_event.setting.action = GDK_SETTING_ACTION_CHANGED;
2938       break;
2939     case XSETTINGS_ACTION_DELETED:
2940       new_event.setting.action = GDK_SETTING_ACTION_DELETED;
2941       break;
2942     }
2943
2944   gdk_event_put (&new_event);
2945 }
2946
2947 static gboolean
2948 check_transform (const gchar *xsettings_name,
2949                  GType        src_type,
2950                  GType        dest_type)
2951 {
2952   if (!g_value_type_transformable (src_type, dest_type))
2953     {
2954       g_warning ("Cannot transform xsetting %s of type %s to type %s\n",
2955                  xsettings_name,
2956                  g_type_name (src_type),
2957                  g_type_name (dest_type));
2958       return FALSE;
2959     }
2960   else
2961     return TRUE;
2962 }
2963
2964 /**
2965  * gdk_screen_get_setting:
2966  * @screen: the #GdkScreen where the setting is located
2967  * @name: the name of the setting
2968  * @value: location to store the value of the setting
2969  *
2970  * Retrieves a desktop-wide setting such as double-click time
2971  * for the #GdkScreen @screen. 
2972  *
2973  * FIXME needs a list of valid settings here, or a link to 
2974  * more information.
2975  * 
2976  * Returns: %TRUE if the setting existed and a value was stored
2977  *   in @value, %FALSE otherwise.
2978  *
2979  * Since: 2.2
2980  **/
2981 gboolean
2982 gdk_screen_get_setting (GdkScreen   *screen,
2983                         const gchar *name,
2984                         GValue      *value)
2985 {
2986
2987   const char *xsettings_name = NULL;
2988   XSettingsResult result;
2989   XSettingsSetting *setting = NULL;
2990   GdkScreenX11 *screen_x11;
2991   gboolean success = FALSE;
2992   gint i;
2993   GValue tmp_val = { 0, };
2994   
2995   g_return_val_if_fail (GDK_IS_SCREEN (screen), FALSE);
2996   
2997   screen_x11 = GDK_SCREEN_X11 (screen);
2998
2999   for (i = 0; i < GDK_SETTINGS_N_ELEMENTS(); i++)
3000     if (strcmp (GDK_SETTINGS_GDK_NAME (i), name) == 0)
3001       {
3002         xsettings_name = GDK_SETTINGS_X_NAME (i);
3003         break;
3004       }
3005
3006   if (!xsettings_name)
3007     goto out;
3008
3009   result = xsettings_client_get_setting (screen_x11->xsettings_client, 
3010                                          xsettings_name, &setting);
3011   if (result != XSETTINGS_SUCCESS)
3012     goto out;
3013
3014   switch (setting->type)
3015     {
3016     case XSETTINGS_TYPE_INT:
3017       if (check_transform (xsettings_name, G_TYPE_INT, G_VALUE_TYPE (value)))
3018         {
3019           g_value_init (&tmp_val, G_TYPE_INT);
3020           g_value_set_int (&tmp_val, setting->data.v_int);
3021           g_value_transform (&tmp_val, value);
3022
3023           success = TRUE;
3024         }
3025       break;
3026     case XSETTINGS_TYPE_STRING:
3027       if (check_transform (xsettings_name, G_TYPE_STRING, G_VALUE_TYPE (value)))
3028         {
3029           g_value_init (&tmp_val, G_TYPE_STRING);
3030           g_value_set_string (&tmp_val, setting->data.v_string);
3031           g_value_transform (&tmp_val, value);
3032
3033           success = TRUE;
3034         }
3035       break;
3036     case XSETTINGS_TYPE_COLOR:
3037       if (!check_transform (xsettings_name, GDK_TYPE_COLOR, G_VALUE_TYPE (value)))
3038         {
3039           GdkColor color;
3040           
3041           g_value_init (&tmp_val, GDK_TYPE_COLOR);
3042
3043           color.pixel = 0;
3044           color.red = setting->data.v_color.red;
3045           color.green = setting->data.v_color.green;
3046           color.blue = setting->data.v_color.blue;
3047           
3048           g_value_set_boxed (&tmp_val, &color);
3049           
3050           g_value_transform (&tmp_val, value);
3051           
3052           success = TRUE;
3053         }
3054       break;
3055     }
3056   
3057   g_value_unset (&tmp_val);
3058
3059  out:
3060   if (setting)
3061     xsettings_setting_free (setting);
3062
3063   if (success)
3064     return TRUE;
3065   else
3066     return _gdk_x11_get_xft_setting (screen, name, value);
3067 }
3068
3069 static GdkFilterReturn 
3070 gdk_xsettings_client_event_filter (GdkXEvent *xevent,
3071                                    GdkEvent  *event,
3072                                    gpointer   data)
3073 {
3074   GdkScreenX11 *screen = data;
3075   
3076   if (xsettings_client_process_event (screen->xsettings_client, (XEvent *)xevent))
3077     return GDK_FILTER_REMOVE;
3078   else
3079     return GDK_FILTER_CONTINUE;
3080 }
3081
3082 static Bool
3083 gdk_xsettings_watch_cb (Window   window,
3084                         Bool     is_start,
3085                         long     mask,
3086                         void    *cb_data)
3087 {
3088   GdkWindow *gdkwin;
3089   GdkScreen *screen = cb_data;
3090
3091   gdkwin = gdk_window_lookup_for_display (gdk_screen_get_display (screen), window);
3092
3093   if (is_start)
3094     {
3095       if (gdkwin)
3096         g_object_ref (gdkwin);
3097       else
3098         {
3099           gdkwin = gdk_window_foreign_new_for_display (gdk_screen_get_display (screen), window);
3100           
3101           /* gdk_window_foreign_new_for_display() can fail and return NULL if the
3102            * window has already been destroyed.
3103            */
3104           if (!gdkwin)
3105             return False;
3106         }
3107
3108       gdk_window_add_filter (gdkwin, gdk_xsettings_client_event_filter, screen);
3109     }
3110   else
3111     {
3112       if (!gdkwin)
3113         {
3114           /* gdkwin should not be NULL here, since if starting the watch succeeded
3115            * we have a reference on the window. It might mean that the caller didn't
3116            * remove the watch when it got a DestroyNotify event. Or maybe the
3117            * caller ignored the return value when starting the watch failed.
3118            */
3119           g_warning ("gdk_xsettings_watch_cb(): Couldn't find window to unwatch");
3120           return False;
3121         }
3122       
3123       gdk_window_remove_filter (gdkwin, gdk_xsettings_client_event_filter, screen);
3124       g_object_unref (gdkwin);
3125     }
3126
3127   return True;
3128 }
3129
3130 void
3131 _gdk_windowing_event_data_copy (const GdkEvent *src,
3132                                 GdkEvent       *dst)
3133 {
3134 }
3135
3136 void
3137 _gdk_windowing_event_data_free (GdkEvent *event)
3138 {
3139 }
3140
3141 #define __GDK_EVENTS_X11_C__
3142 #include "gdkaliasdef.c"