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