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