]> Pileus Git - ~andy/gtk/blob - gdk/x11/gdkdisplay-x11.c
gdk/: fully remove gdkalias hacks
[~andy/gtk] / gdk / x11 / gdkdisplay-x11.c
1 /* GDK - The GIMP Drawing Kit
2  * gdkdisplay-x11.c
3  * 
4  * Copyright 2001 Sun Microsystems Inc.
5  * Copyright (C) 2004 Nokia Corporation
6  *
7  * Erwann Chenede <erwann.chenede@sun.com>
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Library General Public
11  * License as published by the Free Software Foundation; either
12  * version 2 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Library General Public License for more details.
18  *
19  * You should have received a copy of the GNU Library General Public
20  * License along with this library; if not, write to the
21  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22  * Boston, MA 02111-1307, USA.
23  */
24
25 #include "config.h"
26
27 #include <stdlib.h>
28 #include <string.h>
29 #include <errno.h>
30 #include <unistd.h>
31
32 #include <glib.h>
33 #include "gdkx.h"
34 #include "gdkasync.h"
35 #include "gdkdisplay.h"
36 #include "gdkdisplay-x11.h"
37 #include "gdkeventsource.h"
38 #include "gdkeventtranslator.h"
39 #include "gdkscreen.h"
40 #include "gdkscreen-x11.h"
41 #include "gdkinternals.h"
42 #include "gdkdeviceprivate.h"
43 #include "gdkdevicemanager.h"
44 #include "xsettings-client.h"
45
46 #include <X11/Xatom.h>
47
48 #ifdef HAVE_XKB
49 #include <X11/XKBlib.h>
50 #endif
51
52 #ifdef HAVE_XFIXES
53 #include <X11/extensions/Xfixes.h>
54 #endif
55
56 #include <X11/extensions/shape.h>
57
58 #ifdef HAVE_XCOMPOSITE
59 #include <X11/extensions/Xcomposite.h>
60 #endif
61
62 #ifdef HAVE_XDAMAGE
63 #include <X11/extensions/Xdamage.h>
64 #endif
65
66 #ifdef HAVE_RANDR
67 #include <X11/extensions/Xrandr.h>
68 #endif
69
70
71 static void   gdk_display_x11_dispose            (GObject            *object);
72 static void   gdk_display_x11_finalize           (GObject            *object);
73
74 static void     gdk_display_x11_event_translator_init (GdkEventTranslatorIface *iface);
75
76 static gboolean gdk_display_x11_translate_event (GdkEventTranslator *translator,
77                                                  GdkDisplay         *display,
78                                                  GdkEvent           *event,
79                                                  XEvent             *xevent);
80
81 #ifdef HAVE_X11R6
82 static void gdk_internal_connection_watch (Display  *display,
83                                            XPointer  arg,
84                                            gint      fd,
85                                            gboolean  opening,
86                                            XPointer *watch_data);
87 #endif /* HAVE_X11R6 */
88
89 typedef struct _GdkEventTypeX11 GdkEventTypeX11;
90
91 struct _GdkEventTypeX11
92 {
93   gint base;
94   gint n_events;
95 };
96
97 /* Note that we never *directly* use WM_LOCALE_NAME, WM_PROTOCOLS,
98  * but including them here has the side-effect of getting them
99  * into the internal Xlib cache
100  */
101 static const char *const precache_atoms[] = {
102   "UTF8_STRING",
103   "WM_CLIENT_LEADER",
104   "WM_DELETE_WINDOW",
105   "WM_ICON_NAME",
106   "WM_LOCALE_NAME",
107   "WM_NAME",
108   "WM_PROTOCOLS",
109   "WM_TAKE_FOCUS",
110   "WM_WINDOW_ROLE",
111   "_NET_ACTIVE_WINDOW",
112   "_NET_CURRENT_DESKTOP",
113   "_NET_FRAME_EXTENTS",
114   "_NET_STARTUP_ID",
115   "_NET_WM_CM_S0",
116   "_NET_WM_DESKTOP",
117   "_NET_WM_ICON",
118   "_NET_WM_ICON_NAME",
119   "_NET_WM_NAME",
120   "_NET_WM_PID",
121   "_NET_WM_PING",
122   "_NET_WM_STATE",
123   "_NET_WM_STATE_ABOVE",
124   "_NET_WM_STATE_BELOW",
125   "_NET_WM_STATE_FULLSCREEN",
126   "_NET_WM_STATE_MODAL",
127   "_NET_WM_STATE_MAXIMIZED_VERT",
128   "_NET_WM_STATE_MAXIMIZED_HORZ",
129   "_NET_WM_STATE_SKIP_TASKBAR",
130   "_NET_WM_STATE_SKIP_PAGER",
131   "_NET_WM_STATE_STICKY",
132   "_NET_WM_SYNC_REQUEST",
133   "_NET_WM_SYNC_REQUEST_COUNTER",
134   "_NET_WM_WINDOW_TYPE",
135   "_NET_WM_WINDOW_TYPE_NORMAL",
136   "_NET_WM_USER_TIME",
137   "_NET_VIRTUAL_ROOTS"
138 };
139
140 G_DEFINE_TYPE_WITH_CODE (GdkDisplayX11, _gdk_display_x11, GDK_TYPE_DISPLAY,
141                          G_IMPLEMENT_INTERFACE (GDK_TYPE_EVENT_TRANSLATOR,
142                                                 gdk_display_x11_event_translator_init))
143
144
145 static void
146 _gdk_display_x11_class_init (GdkDisplayX11Class * class)
147 {
148   GObjectClass *object_class = G_OBJECT_CLASS (class);
149   
150   object_class->dispose = gdk_display_x11_dispose;
151   object_class->finalize = gdk_display_x11_finalize;
152 }
153
154 static void
155 _gdk_display_x11_init (GdkDisplayX11 *display)
156 {
157 }
158
159 static void
160 gdk_display_x11_event_translator_init (GdkEventTranslatorIface *iface)
161 {
162   iface->translate_event = gdk_display_x11_translate_event;
163 }
164
165 static void
166 do_net_wm_state_changes (GdkWindow *window)
167 {
168   GdkToplevelX11 *toplevel = _gdk_x11_window_get_toplevel (window);
169   GdkWindowState old_state;
170
171   if (GDK_WINDOW_DESTROYED (window) ||
172       gdk_window_get_window_type (window) != GDK_WINDOW_TOPLEVEL)
173     return;
174
175   old_state = gdk_window_get_state (window);
176
177   /* For found_sticky to remain TRUE, we have to also be on desktop
178    * 0xFFFFFFFF
179    */
180   if (old_state & GDK_WINDOW_STATE_STICKY)
181     {
182       if (!(toplevel->have_sticky && toplevel->on_all_desktops))
183         gdk_synthesize_window_state (window,
184                                      GDK_WINDOW_STATE_STICKY,
185                                      0);
186     }
187   else
188     {
189       if (toplevel->have_sticky || toplevel->on_all_desktops)
190         gdk_synthesize_window_state (window,
191                                      0,
192                                      GDK_WINDOW_STATE_STICKY);
193     }
194
195   if (old_state & GDK_WINDOW_STATE_FULLSCREEN)
196     {
197       if (!toplevel->have_fullscreen)
198         gdk_synthesize_window_state (window,
199                                      GDK_WINDOW_STATE_FULLSCREEN,
200                                      0);
201     }
202   else
203     {
204       if (toplevel->have_fullscreen)
205         gdk_synthesize_window_state (window,
206                                      0,
207                                      GDK_WINDOW_STATE_FULLSCREEN);
208     }
209
210   /* Our "maximized" means both vertical and horizontal; if only one,
211    * we don't expose that via GDK
212    */
213   if (old_state & GDK_WINDOW_STATE_MAXIMIZED)
214     {
215       if (!(toplevel->have_maxvert && toplevel->have_maxhorz))
216         gdk_synthesize_window_state (window,
217                                      GDK_WINDOW_STATE_MAXIMIZED,
218                                      0);
219     }
220   else
221     {
222       if (toplevel->have_maxvert && toplevel->have_maxhorz)
223         gdk_synthesize_window_state (window,
224                                      0,
225                                      GDK_WINDOW_STATE_MAXIMIZED);
226     }
227 }
228
229 static void
230 gdk_check_wm_desktop_changed (GdkWindow *window)
231 {
232   GdkToplevelX11 *toplevel = _gdk_x11_window_get_toplevel (window);
233   GdkDisplay *display = GDK_WINDOW_DISPLAY (window);
234
235   Atom type;
236   gint format;
237   gulong nitems;
238   gulong bytes_after;
239   guchar *data;
240   gulong *desktop;
241
242   type = None;
243   gdk_error_trap_push ();
244   XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display),
245                       GDK_WINDOW_XID (window),
246                       gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_DESKTOP"),
247                       0, G_MAXLONG, False, XA_CARDINAL, &type,
248                       &format, &nitems,
249                       &bytes_after, &data);
250   gdk_error_trap_pop ();
251
252   if (type != None)
253     {
254       desktop = (gulong *)data;
255       toplevel->on_all_desktops = (*desktop == 0xFFFFFFFF);
256       XFree (desktop);
257     }
258   else
259     toplevel->on_all_desktops = FALSE;
260
261   do_net_wm_state_changes (window);
262 }
263
264 static void
265 gdk_check_wm_state_changed (GdkWindow *window)
266 {
267   GdkToplevelX11 *toplevel = _gdk_x11_window_get_toplevel (window);
268   GdkDisplay *display = GDK_WINDOW_DISPLAY (window);
269
270   Atom type;
271   gint format;
272   gulong nitems;
273   gulong bytes_after;
274   guchar *data;
275   Atom *atoms = NULL;
276   gulong i;
277
278   gboolean had_sticky = toplevel->have_sticky;
279
280   toplevel->have_sticky = FALSE;
281   toplevel->have_maxvert = FALSE;
282   toplevel->have_maxhorz = FALSE;
283   toplevel->have_fullscreen = FALSE;
284
285   type = None;
286   gdk_error_trap_push ();
287   XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), GDK_WINDOW_XID (window),
288                       gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_STATE"),
289                       0, G_MAXLONG, False, XA_ATOM, &type, &format, &nitems,
290                       &bytes_after, &data);
291   gdk_error_trap_pop ();
292
293   if (type != None)
294     {
295       Atom sticky_atom = gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_STATE_STICKY");
296       Atom maxvert_atom = gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_STATE_MAXIMIZED_VERT");
297       Atom maxhorz_atom = gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_STATE_MAXIMIZED_HORZ");
298       Atom fullscreen_atom = gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_STATE_FULLSCREEN");
299
300       atoms = (Atom *)data;
301
302       i = 0;
303       while (i < nitems)
304         {
305           if (atoms[i] == sticky_atom)
306             toplevel->have_sticky = TRUE;
307           else if (atoms[i] == maxvert_atom)
308             toplevel->have_maxvert = TRUE;
309           else if (atoms[i] == maxhorz_atom)
310             toplevel->have_maxhorz = TRUE;
311           else if (atoms[i] == fullscreen_atom)
312             toplevel->have_fullscreen = TRUE;
313
314           ++i;
315         }
316
317       XFree (atoms);
318     }
319
320   /* When have_sticky is turned on, we have to check the DESKTOP property
321    * as well.
322    */
323   if (toplevel->have_sticky && !had_sticky)
324     gdk_check_wm_desktop_changed (window);
325   else
326     do_net_wm_state_changes (window);
327 }
328
329 static GdkWindow *
330 get_event_window (GdkEventTranslator *translator,
331                   XEvent             *xevent)
332 {
333   GdkDisplay *display;
334   Window xwindow;
335
336   display = (GdkDisplay *) translator;
337
338   switch (xevent->type)
339     {
340     case DestroyNotify:
341       xwindow = xevent->xdestroywindow.window;
342       break;
343     case UnmapNotify:
344       xwindow = xevent->xunmap.window;
345       break;
346     case MapNotify:
347       xwindow = xevent->xmap.window;
348       break;
349     case ConfigureNotify:
350       xwindow = xevent->xconfigure.window;
351       break;
352     default:
353       xwindow = xevent->xany.window;
354     }
355
356   return gdk_window_lookup_for_display (display, xwindow);
357 }
358
359 static gboolean
360 gdk_display_x11_translate_event (GdkEventTranslator *translator,
361                                  GdkDisplay         *display,
362                                  GdkEvent           *event,
363                                  XEvent             *xevent)
364 {
365   GdkWindow *window;
366   GdkWindowObject *window_private;
367   GdkWindowImplX11 *window_impl = NULL;
368   GdkScreen *screen = NULL;
369   GdkScreenX11 *screen_x11 = NULL;
370   GdkToplevelX11 *toplevel = NULL;
371   GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
372   gboolean return_val;
373   Window xwindow = None;
374
375   /* Find the GdkWindow that this event relates to.
376    * Basically this means substructure events
377    * are reported same as structure events
378    */
379   window = get_event_window (translator, xevent);
380   window_private = (GdkWindowObject *) window;
381
382   if (window)
383     {
384       /* We may receive events such as NoExpose/GraphicsExpose
385        * and ShmCompletion for pixmaps
386        */
387       if (!GDK_IS_WINDOW (window))
388         return FALSE;
389
390       screen = GDK_WINDOW_SCREEN (window);
391       screen_x11 = GDK_SCREEN_X11 (screen);
392       toplevel = _gdk_x11_window_get_toplevel (window);
393       window_impl = GDK_WINDOW_IMPL_X11 (window_private->impl);
394       xwindow = GDK_WINDOW_XID (window);
395
396       g_object_ref (window);
397     }
398
399   event->any.window = window;
400   event->any.send_event = xevent->xany.send_event ? TRUE : FALSE;
401
402   if (window_private && GDK_WINDOW_DESTROYED (window))
403     {
404       if (xevent->type != DestroyNotify)
405         {
406           return_val = FALSE;
407           goto done;
408         }
409     }
410
411   if (xevent->type == DestroyNotify)
412     {
413       int i, n;
414
415       n = gdk_display_get_n_screens (display);
416       for (i = 0; i < n; i++)
417         {
418           screen = gdk_display_get_screen (display, i);
419           screen_x11 = GDK_SCREEN_X11 (screen);
420
421           if (screen_x11->wmspec_check_window == xwindow)
422             {
423               screen_x11->wmspec_check_window = None;
424               screen_x11->last_wmspec_check_time = 0;
425               g_free (screen_x11->window_manager_name);
426               screen_x11->window_manager_name = g_strdup ("unknown");
427
428               /* careful, reentrancy */
429               _gdk_x11_screen_window_manager_changed (screen);
430
431               return_val = FALSE;
432               goto done;
433             }
434         }
435     }
436
437   /* We do a "manual" conversion of the XEvent to a
438    *  GdkEvent. The structures are mostly the same so
439    *  the conversion is fairly straightforward. We also
440    *  optionally print debugging info regarding events
441    *  received.
442    */
443
444   return_val = TRUE;
445
446   switch (xevent->type)
447     {
448     case KeymapNotify:
449       GDK_NOTE (EVENTS,
450                 g_message ("keymap notify"));
451
452       /* Not currently handled */
453       return_val = FALSE;
454       break;
455
456     case Expose:
457       GDK_NOTE (EVENTS,
458                 g_message ("expose:\t\twindow: %ld  %d  x,y: %d %d  w,h: %d %d%s",
459                            xevent->xexpose.window, xevent->xexpose.count,
460                            xevent->xexpose.x, xevent->xexpose.y,
461                            xevent->xexpose.width, xevent->xexpose.height,
462                            event->any.send_event ? " (send)" : ""));
463
464       if (window_private == NULL)
465         {
466           return_val = FALSE;
467           break;
468         }
469
470       {
471         GdkRectangle expose_rect;
472
473         expose_rect.x = xevent->xexpose.x;
474         expose_rect.y = xevent->xexpose.y;
475         expose_rect.width = xevent->xexpose.width;
476         expose_rect.height = xevent->xexpose.height;
477
478         _gdk_window_process_expose (window, xevent->xexpose.serial, &expose_rect);
479         return_val = FALSE;
480       }
481
482       break;
483
484     case GraphicsExpose:
485       {
486         GdkRectangle expose_rect;
487
488         GDK_NOTE (EVENTS,
489                   g_message ("graphics expose:\tdrawable: %ld",
490                              xevent->xgraphicsexpose.drawable));
491
492         if (window_private == NULL)
493           {
494             return_val = FALSE;
495             break;
496           }
497
498         expose_rect.x = xevent->xgraphicsexpose.x;
499         expose_rect.y = xevent->xgraphicsexpose.y;
500         expose_rect.width = xevent->xgraphicsexpose.width;
501         expose_rect.height = xevent->xgraphicsexpose.height;
502
503         _gdk_window_process_expose (window, xevent->xgraphicsexpose.serial, &expose_rect);
504         return_val = FALSE;
505       }
506       break;
507
508     case NoExpose:
509       GDK_NOTE (EVENTS,
510                 g_message ("no expose:\t\tdrawable: %ld",
511                            xevent->xnoexpose.drawable));
512
513       event->no_expose.type = GDK_NO_EXPOSE;
514       event->no_expose.window = window;
515
516       break;
517
518     case VisibilityNotify:
519 #ifdef G_ENABLE_DEBUG
520       if (_gdk_debug_flags & GDK_DEBUG_EVENTS)
521         switch (xevent->xvisibility.state)
522           {
523           case VisibilityFullyObscured:
524             g_message ("visibility notify:\twindow: %ld  none",
525                        xevent->xvisibility.window);
526             break;
527           case VisibilityPartiallyObscured:
528             g_message ("visibility notify:\twindow: %ld  partial",
529                        xevent->xvisibility.window);
530             break;
531           case VisibilityUnobscured:
532             g_message ("visibility notify:\twindow: %ld  full",
533                        xevent->xvisibility.window);
534             break;
535           }
536 #endif /* G_ENABLE_DEBUG */
537
538       if (window_private == NULL)
539         {
540           return_val = FALSE;
541           break;
542         }
543
544       event->visibility.type = GDK_VISIBILITY_NOTIFY;
545       event->visibility.window = window;
546
547       switch (xevent->xvisibility.state)
548         {
549         case VisibilityFullyObscured:
550           event->visibility.state = GDK_VISIBILITY_FULLY_OBSCURED;
551           break;
552
553         case VisibilityPartiallyObscured:
554           event->visibility.state = GDK_VISIBILITY_PARTIAL;
555           break;
556
557         case VisibilityUnobscured:
558           event->visibility.state = GDK_VISIBILITY_UNOBSCURED;
559           break;
560         }
561
562       break;
563
564     case CreateNotify:
565       GDK_NOTE (EVENTS,
566                 g_message ("create notify:\twindow: %ld  x,y: %d %d     w,h: %d %d  b-w: %d  parent: %ld         ovr: %d",
567                            xevent->xcreatewindow.window,
568                            xevent->xcreatewindow.x,
569                            xevent->xcreatewindow.y,
570                            xevent->xcreatewindow.width,
571                            xevent->xcreatewindow.height,
572                            xevent->xcreatewindow.border_width,
573                            xevent->xcreatewindow.parent,
574                            xevent->xcreatewindow.override_redirect));
575       /* not really handled */
576       break;
577
578     case DestroyNotify:
579       GDK_NOTE (EVENTS,
580                 g_message ("destroy notify:\twindow: %ld",
581                            xevent->xdestroywindow.window));
582
583       /* Ignore DestroyNotify from SubstructureNotifyMask */
584       if (xevent->xdestroywindow.window == xevent->xdestroywindow.event)
585         {
586           event->any.type = GDK_DESTROY;
587           event->any.window = window;
588
589           return_val = window_private && !GDK_WINDOW_DESTROYED (window);
590
591           if (window && GDK_WINDOW_XID (window) != screen_x11->xroot_window)
592             gdk_window_destroy_notify (window);
593         }
594       else
595         return_val = FALSE;
596
597       break;
598
599     case UnmapNotify:
600       GDK_NOTE (EVENTS,
601                 g_message ("unmap notify:\t\twindow: %ld",
602                            xevent->xmap.window));
603
604       event->any.type = GDK_UNMAP;
605       event->any.window = window;
606
607       /* If we are shown (not withdrawn) and get an unmap, it means we
608        * were iconified in the X sense. If we are withdrawn, and get
609        * an unmap, it means we hid the window ourselves, so we
610        * will have already flipped the iconified bit off.
611        */
612       if (window)
613         {
614           if (GDK_WINDOW_IS_MAPPED (window))
615             gdk_synthesize_window_state (window,
616                                          0,
617                                          GDK_WINDOW_STATE_ICONIFIED);
618
619           _gdk_xgrab_check_unmap (window, xevent->xany.serial);
620         }
621
622       break;
623
624     case MapNotify:
625       GDK_NOTE (EVENTS,
626                 g_message ("map notify:\t\twindow: %ld",
627                            xevent->xmap.window));
628
629       event->any.type = GDK_MAP;
630       event->any.window = window;
631
632       /* Unset iconified if it was set */
633       if (window && (((GdkWindowObject*)window)->state & GDK_WINDOW_STATE_ICONIFIED))
634         gdk_synthesize_window_state (window,
635                                      GDK_WINDOW_STATE_ICONIFIED,
636                                      0);
637
638       break;
639
640     case ReparentNotify:
641       GDK_NOTE (EVENTS,
642                 g_message ("reparent notify:\twindow: %ld  x,y: %d %d  parent: %ld      ovr: %d",
643                            xevent->xreparent.window,
644                            xevent->xreparent.x,
645                            xevent->xreparent.y,
646                            xevent->xreparent.parent,
647                            xevent->xreparent.override_redirect));
648
649       /* Not currently handled */
650       return_val = FALSE;
651       break;
652
653     case ConfigureNotify:
654       GDK_NOTE (EVENTS,
655                 g_message ("configure notify:\twindow: %ld  x,y: %d %d  w,h: %d %d  b-w: %d  above: %ld  ovr: %d%s",
656                            xevent->xconfigure.window,
657                            xevent->xconfigure.x,
658                            xevent->xconfigure.y,
659                            xevent->xconfigure.width,
660                            xevent->xconfigure.height,
661                            xevent->xconfigure.border_width,
662                            xevent->xconfigure.above,
663                            xevent->xconfigure.override_redirect,
664                            !window
665                            ? " (discarding)"
666                            : GDK_WINDOW_TYPE (window) == GDK_WINDOW_CHILD
667                            ? " (discarding child)"
668                            : xevent->xconfigure.event != xevent->xconfigure.window
669                            ? " (discarding substructure)"
670                            : ""));
671       if (window && GDK_WINDOW_TYPE (window) == GDK_WINDOW_ROOT)
672         {
673           window_private->width = xevent->xconfigure.width;
674           window_private->height = xevent->xconfigure.height;
675
676           _gdk_window_update_size (window);
677           _gdk_x11_drawable_update_size (window_private->impl);
678           _gdk_x11_screen_size_changed (screen, xevent);
679         }
680
681 #ifdef HAVE_XSYNC
682       if (toplevel && display_x11->use_sync && !XSyncValueIsZero (toplevel->pending_counter_value))
683         {
684           toplevel->current_counter_value = toplevel->pending_counter_value;
685           XSyncIntToValue (&toplevel->pending_counter_value, 0);
686         }
687 #endif
688
689     if (!window ||
690           xevent->xconfigure.event != xevent->xconfigure.window ||
691           GDK_WINDOW_TYPE (window) == GDK_WINDOW_CHILD ||
692           GDK_WINDOW_TYPE (window) == GDK_WINDOW_ROOT)
693         return_val = FALSE;
694       else
695         {
696           event->configure.type = GDK_CONFIGURE;
697           event->configure.window = window;
698           event->configure.width = xevent->xconfigure.width;
699           event->configure.height = xevent->xconfigure.height;
700
701           if (!xevent->xconfigure.send_event &&
702               !xevent->xconfigure.override_redirect &&
703               !GDK_WINDOW_DESTROYED (window))
704             {
705               gint tx = 0;
706               gint ty = 0;
707               Window child_window = 0;
708
709               gdk_error_trap_push ();
710               if (XTranslateCoordinates (GDK_DRAWABLE_XDISPLAY (window),
711                                          GDK_DRAWABLE_XID (window),
712                                          screen_x11->xroot_window,
713                                          0, 0,
714                                          &tx, &ty,
715                                          &child_window))
716                 {
717                   event->configure.x = tx;
718                   event->configure.y = ty;
719                 }
720               gdk_error_trap_pop ();
721             }
722           else
723             {
724               event->configure.x = xevent->xconfigure.x;
725               event->configure.y = xevent->xconfigure.y;
726             }
727           window_private->x = event->configure.x;
728           window_private->y = event->configure.y;
729           window_private->width = xevent->xconfigure.width;
730           window_private->height = xevent->xconfigure.height;
731
732           _gdk_window_update_size (window);
733           _gdk_x11_drawable_update_size (window_private->impl);
734
735           if (window_private->resize_count >= 1)
736             {
737               window_private->resize_count -= 1;
738
739               if (window_private->resize_count == 0)
740                 _gdk_moveresize_configure_done (display, window);
741             }
742         }
743       break;
744
745     case PropertyNotify:
746       GDK_NOTE (EVENTS,
747                 g_message ("property notify:\twindow: %ld, atom(%ld): %s%s%s",
748                            xevent->xproperty.window,
749                            xevent->xproperty.atom,
750                            "\"",
751                            gdk_x11_get_xatom_name_for_display (display, xevent->xproperty.atom),
752                            "\""));
753
754       if (window_private == NULL)
755         {
756           return_val = FALSE;
757           break;
758         }
759
760       /* We compare with the serial of the last time we mapped the
761        * window to avoid refetching properties that we set ourselves
762        */
763       if (toplevel &&
764           xevent->xproperty.serial >= toplevel->map_serial)
765         {
766           if (xevent->xproperty.atom == gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_STATE"))
767             gdk_check_wm_state_changed (window);
768
769           if (xevent->xproperty.atom == gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_DESKTOP"))
770             gdk_check_wm_desktop_changed (window);
771         }
772
773       if (window_private->event_mask & GDK_PROPERTY_CHANGE_MASK)
774         {
775           event->property.type = GDK_PROPERTY_NOTIFY;
776           event->property.window = window;
777           event->property.atom = gdk_x11_xatom_to_atom_for_display (display, xevent->xproperty.atom);
778           event->property.time = xevent->xproperty.time;
779           event->property.state = xevent->xproperty.state;
780         }
781       else
782         return_val = FALSE;
783
784       break;
785
786     case SelectionClear:
787       GDK_NOTE (EVENTS,
788                 g_message ("selection clear:\twindow: %ld",
789                            xevent->xproperty.window));
790
791       if (_gdk_selection_filter_clear_event (&xevent->xselectionclear))
792         {
793           event->selection.type = GDK_SELECTION_CLEAR;
794           event->selection.window = window;
795           event->selection.selection = gdk_x11_xatom_to_atom_for_display (display, xevent->xselectionclear.selection);
796           event->selection.time = xevent->xselectionclear.time;
797         }
798       else
799         return_val = FALSE;
800
801       break;
802
803     case SelectionRequest:
804       GDK_NOTE (EVENTS,
805                 g_message ("selection request:\twindow: %ld",
806                            xevent->xproperty.window));
807
808       event->selection.type = GDK_SELECTION_REQUEST;
809       event->selection.window = window;
810       event->selection.selection = gdk_x11_xatom_to_atom_for_display (display, xevent->xselectionrequest.selection);
811       event->selection.target = gdk_x11_xatom_to_atom_for_display (display, xevent->xselectionrequest.target);
812       event->selection.property = gdk_x11_xatom_to_atom_for_display (display, xevent->xselectionrequest.property);
813       event->selection.requestor = xevent->xselectionrequest.requestor;
814       event->selection.time = xevent->xselectionrequest.time;
815
816       break;
817
818     case SelectionNotify:
819       GDK_NOTE (EVENTS,
820                 g_message ("selection notify:\twindow: %ld",
821                            xevent->xproperty.window));
822
823       event->selection.type = GDK_SELECTION_NOTIFY;
824       event->selection.window = window;
825       event->selection.selection = gdk_x11_xatom_to_atom_for_display (display, xevent->xselection.selection);
826       event->selection.target = gdk_x11_xatom_to_atom_for_display (display, xevent->xselection.target);
827       if (xevent->xselection.property == None)
828         event->selection.property = GDK_NONE;
829       else
830         event->selection.property = gdk_x11_xatom_to_atom_for_display (display, xevent->xselection.property);
831       event->selection.time = xevent->xselection.time;
832
833       break;
834
835     case ColormapNotify:
836       GDK_NOTE (EVENTS,
837                 g_message ("colormap notify:\twindow: %ld",
838                            xevent->xcolormap.window));
839
840       /* Not currently handled */
841       return_val = FALSE;
842       break;
843
844     case ClientMessage:
845       {
846         GList *tmp_list;
847         GdkFilterReturn result = GDK_FILTER_CONTINUE;
848         GdkAtom message_type = gdk_x11_xatom_to_atom_for_display (display, xevent->xclient.message_type);
849
850         GDK_NOTE (EVENTS,
851                   g_message ("client message:\twindow: %ld",
852                              xevent->xclient.window));
853
854         tmp_list = display_x11->client_filters;
855         while (tmp_list)
856           {
857             GdkClientFilter *filter = tmp_list->data;
858             tmp_list = tmp_list->next;
859
860             if (filter->type == message_type)
861               {
862                 result = (*filter->function) (xevent, event, filter->data);
863                 if (result != GDK_FILTER_CONTINUE)
864                   break;
865               }
866           }
867
868         switch (result)
869           {
870           case GDK_FILTER_REMOVE:
871             return_val = FALSE;
872             break;
873           case GDK_FILTER_TRANSLATE:
874             return_val = TRUE;
875             break;
876           case GDK_FILTER_CONTINUE:
877             /* Send unknown ClientMessage's on to Gtk for it to use */
878             if (window_private == NULL)
879               {
880                 return_val = FALSE;
881               }
882             else
883               {
884                 event->client.type = GDK_CLIENT_EVENT;
885                 event->client.window = window;
886                 event->client.message_type = message_type;
887                 event->client.data_format = xevent->xclient.format;
888                 memcpy(&event->client.data, &xevent->xclient.data,
889                        sizeof(event->client.data));
890               }
891             break;
892           }
893       }
894
895       break;
896
897     case MappingNotify:
898       GDK_NOTE (EVENTS,
899                 g_message ("mapping notify"));
900
901       /* Let XLib know that there is a new keyboard mapping.
902        */
903       XRefreshKeyboardMapping (&xevent->xmapping);
904       _gdk_keymap_keys_changed (display);
905       return_val = FALSE;
906       break;
907
908     default:
909 #ifdef HAVE_XFIXES
910       if (xevent->type - display_x11->xfixes_event_base == XFixesSelectionNotify)
911         {
912           XFixesSelectionNotifyEvent *selection_notify = (XFixesSelectionNotifyEvent *)xevent;
913
914           _gdk_x11_screen_process_owner_change (screen, xevent);
915           
916           event->owner_change.type = GDK_OWNER_CHANGE;
917           event->owner_change.window = window;
918           event->owner_change.owner = selection_notify->owner;
919           event->owner_change.reason = selection_notify->subtype;
920           event->owner_change.selection = 
921             gdk_x11_xatom_to_atom_for_display (display, 
922                                                selection_notify->selection);
923           event->owner_change.time = selection_notify->timestamp;
924           event->owner_change.selection_time = selection_notify->selection_timestamp;
925           
926           return_val = TRUE;
927         }
928       else
929 #endif
930 #ifdef HAVE_RANDR
931       if (xevent->type - display_x11->xrandr_event_base == RRScreenChangeNotify ||
932           xevent->type - display_x11->xrandr_event_base == RRNotify)
933         {
934           if (screen)
935             _gdk_x11_screen_size_changed (screen, xevent);
936         }
937       else
938 #endif
939 #if defined(HAVE_XCOMPOSITE) && defined (HAVE_XDAMAGE) && defined (HAVE_XFIXES)
940       if (display_x11->have_xdamage && window_private && window_private->composited &&
941           xevent->type == display_x11->xdamage_event_base + XDamageNotify &&
942           ((XDamageNotifyEvent *) xevent)->damage == window_impl->damage)
943         {
944           XDamageNotifyEvent *damage_event = (XDamageNotifyEvent *) xevent;
945           XserverRegion repair;
946           GdkRectangle rect;
947
948           rect.x = window_private->x + damage_event->area.x;
949           rect.y = window_private->y + damage_event->area.y;
950           rect.width = damage_event->area.width;
951           rect.height = damage_event->area.height;
952
953           repair = XFixesCreateRegion (display_x11->xdisplay,
954                                        &damage_event->area, 1);
955           XDamageSubtract (display_x11->xdisplay,
956                            window_impl->damage,
957                            repair, None);
958           XFixesDestroyRegion (display_x11->xdisplay, repair);
959
960           if (window_private->parent != NULL)
961             _gdk_window_process_expose (GDK_WINDOW (window_private->parent),
962                                         damage_event->serial, &rect);
963
964           return_val = TRUE;
965         }
966       else
967 #endif
968 #ifdef HAVE_XKB
969       if (xevent->type == display_x11->xkb_event_type)
970         {
971           XkbEvent *xkb_event = (XkbEvent *) xevent;
972
973           switch (xkb_event->any.xkb_type)
974             {
975             case XkbNewKeyboardNotify:
976             case XkbMapNotify:
977               _gdk_keymap_keys_changed (display);
978
979               return_val = FALSE;
980               break;
981
982             case XkbStateNotify:
983               _gdk_keymap_state_changed (display, xevent);
984               break;
985             }
986         }
987       else
988 #endif
989         return_val = FALSE;
990     }
991
992  done:
993   if (return_val)
994     {
995       if (event->any.window)
996         g_object_ref (event->any.window);
997     }
998   else
999     {
1000       /* Mark this event as having no resources to be freed */
1001       event->any.window = NULL;
1002       event->any.type = GDK_NOTHING;
1003     }
1004
1005   if (window)
1006     g_object_unref (window);
1007
1008   return return_val;
1009 }
1010
1011 static GdkFilterReturn
1012 gdk_wm_protocols_filter (GdkXEvent *xev,
1013                          GdkEvent  *event,
1014                          gpointer data)
1015 {
1016   XEvent *xevent = (XEvent *)xev;
1017   GdkWindow *win = event->any.window;
1018   GdkDisplay *display;
1019   Atom atom;
1020
1021   if (!win)
1022       return GDK_FILTER_REMOVE;
1023
1024   display = GDK_WINDOW_DISPLAY (win);
1025   atom = (Atom)xevent->xclient.data.l[0];
1026
1027   if (atom == gdk_x11_get_xatom_by_name_for_display (display, "WM_DELETE_WINDOW"))
1028     {
1029   /* The delete window request specifies a window
1030    *  to delete. We don't actually destroy the
1031    *  window because "it is only a request". (The
1032    *  window might contain vital data that the
1033    *  program does not want destroyed). Instead
1034    *  the event is passed along to the program,
1035    *  which should then destroy the window.
1036    */
1037       GDK_NOTE (EVENTS,
1038                 g_message ("delete window:\t\twindow: %ld",
1039                            xevent->xclient.window));
1040
1041       event->any.type = GDK_DELETE;
1042
1043       gdk_x11_window_set_user_time (win, xevent->xclient.data.l[1]);
1044
1045       return GDK_FILTER_TRANSLATE;
1046     }
1047   else if (atom == gdk_x11_get_xatom_by_name_for_display (display, "WM_TAKE_FOCUS"))
1048     {
1049       GdkToplevelX11 *toplevel = _gdk_x11_window_get_toplevel (event->any.window);
1050       GdkWindowObject *private = (GdkWindowObject *)win;
1051
1052       /* There is no way of knowing reliably whether we are viewable;
1053        * _gdk_x11_set_input_focus_safe() traps errors asynchronously.
1054        */
1055       if (toplevel && private->accept_focus)
1056         _gdk_x11_set_input_focus_safe (display, toplevel->focus_window,
1057                                        RevertToParent,
1058                                        xevent->xclient.data.l[1]);
1059
1060       return GDK_FILTER_REMOVE;
1061     }
1062   else if (atom == gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_PING") &&
1063            !_gdk_x11_display_is_root_window (display,
1064                                              xevent->xclient.window))
1065     {
1066       XClientMessageEvent xclient = xevent->xclient;
1067
1068       xclient.window = GDK_WINDOW_XROOTWIN (win);
1069       XSendEvent (GDK_WINDOW_XDISPLAY (win),
1070                   xclient.window,
1071                   False,
1072                   SubstructureRedirectMask | SubstructureNotifyMask, (XEvent *)&xclient);
1073
1074       return GDK_FILTER_REMOVE;
1075     }
1076   else if (atom == gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_SYNC_REQUEST") &&
1077            GDK_DISPLAY_X11 (display)->use_sync)
1078     {
1079       GdkToplevelX11 *toplevel = _gdk_x11_window_get_toplevel (event->any.window);
1080       if (toplevel)
1081         {
1082 #ifdef HAVE_XSYNC
1083           XSyncIntsToValue (&toplevel->pending_counter_value,
1084                             xevent->xclient.data.l[2],
1085                             xevent->xclient.data.l[3]);
1086 #endif
1087         }
1088       return GDK_FILTER_REMOVE;
1089     }
1090
1091   return GDK_FILTER_CONTINUE;
1092 }
1093
1094 static void
1095 _gdk_event_init (GdkDisplay *display)
1096 {
1097   GdkDisplayX11 *display_x11;
1098   GdkDeviceManager *device_manager;
1099
1100   display_x11 = GDK_DISPLAY_X11 (display);
1101   display_x11->event_source = gdk_event_source_new (display);
1102
1103   gdk_event_source_add_translator ((GdkEventSource *) display_x11->event_source,
1104                                    GDK_EVENT_TRANSLATOR (display));
1105
1106   device_manager = gdk_display_get_device_manager (display);
1107   gdk_event_source_add_translator ((GdkEventSource *) display_x11->event_source,
1108                                    GDK_EVENT_TRANSLATOR (device_manager));
1109
1110   gdk_display_add_client_message_filter (display,
1111                                          gdk_atom_intern_static_string ("WM_PROTOCOLS"),
1112                                          gdk_wm_protocols_filter,
1113                                          NULL);
1114 }
1115
1116 static void
1117 _gdk_input_init (GdkDisplay *display)
1118 {
1119   GdkDisplayX11 *display_x11;
1120   GdkDeviceManager *device_manager;
1121   GdkDevice *device;
1122   GList *list, *l;
1123
1124   display_x11 = GDK_DISPLAY_X11 (display);
1125   device_manager = gdk_display_get_device_manager (display);
1126
1127   /* For backwards compatibility, just add
1128    * floating devices that are not keyboards.
1129    */
1130   list = gdk_device_manager_list_devices (device_manager, GDK_DEVICE_TYPE_FLOATING);
1131
1132   for (l = list; l; l = l->next)
1133     {
1134       device = l->data;
1135
1136       if (device->source == GDK_SOURCE_KEYBOARD)
1137         continue;
1138
1139       display_x11->input_devices = g_list_prepend (display_x11->input_devices, l->data);
1140     }
1141
1142   g_list_free (list);
1143
1144   /* Now set "core" pointer to the first
1145    * master device that is a pointer.
1146    */
1147   list = gdk_device_manager_list_devices (device_manager, GDK_DEVICE_TYPE_MASTER);
1148
1149   for (l = list; l; l = l->next)
1150     {
1151       device = list->data;
1152
1153       if (device->source != GDK_SOURCE_MOUSE)
1154         continue;
1155
1156       display->core_pointer = device;
1157       break;
1158     }
1159
1160   /* Add the core pointer to the devices list */
1161   display_x11->input_devices = g_list_prepend (display_x11->input_devices, display->core_pointer);
1162
1163   g_list_free (list);
1164 }
1165
1166 /**
1167  * gdk_display_open:
1168  * @display_name: the name of the display to open
1169  * @returns: a #GdkDisplay, or %NULL if the display
1170  *  could not be opened.
1171  *
1172  * Opens a display.
1173  *
1174  * Since: 2.2
1175  */
1176 GdkDisplay *
1177 gdk_display_open (const gchar *display_name)
1178 {
1179   Display *xdisplay;
1180   GdkDisplay *display;
1181   GdkDisplayX11 *display_x11;
1182   GdkWindowAttr attr;
1183   gint argc;
1184   gchar *argv[1];
1185   const char *sm_client_id;
1186   
1187   XClassHint *class_hint;
1188   gulong pid;
1189   gint i;
1190   gint ignore;
1191   gint maj, min;
1192
1193   xdisplay = XOpenDisplay (display_name);
1194   if (!xdisplay)
1195     return NULL;
1196   
1197   display = g_object_new (GDK_TYPE_DISPLAY_X11, NULL);
1198   display_x11 = GDK_DISPLAY_X11 (display);
1199
1200   display_x11->use_xshm = TRUE;
1201   display_x11->xdisplay = xdisplay;
1202
1203 #ifdef HAVE_X11R6  
1204   /* Set up handlers for Xlib internal connections */
1205   XAddConnectionWatch (xdisplay, gdk_internal_connection_watch, NULL);
1206 #endif /* HAVE_X11R6 */
1207   
1208   _gdk_x11_precache_atoms (display, precache_atoms, G_N_ELEMENTS (precache_atoms));
1209
1210   /* RandR must be initialized before we initialize the screens */
1211   display_x11->have_randr13 = FALSE;
1212 #ifdef HAVE_RANDR
1213   if (XRRQueryExtension (display_x11->xdisplay,
1214                          &display_x11->xrandr_event_base, &ignore))
1215   {
1216       int major, minor;
1217       
1218       XRRQueryVersion (display_x11->xdisplay, &major, &minor);
1219
1220       if ((major == 1 && minor >= 3) || major > 1)
1221           display_x11->have_randr13 = TRUE;
1222
1223        gdk_x11_register_standard_event_type (display, display_x11->xrandr_event_base, RRNumberEvents);
1224   }
1225 #endif
1226   
1227   /* initialize the display's screens */ 
1228   display_x11->screens = g_new (GdkScreen *, ScreenCount (display_x11->xdisplay));
1229   for (i = 0; i < ScreenCount (display_x11->xdisplay); i++)
1230     display_x11->screens[i] = _gdk_x11_screen_new (display, i);
1231
1232   /* We need to initialize events after we have the screen
1233    * structures in places
1234    */
1235   for (i = 0; i < ScreenCount (display_x11->xdisplay); i++)
1236     _gdk_screen_x11_events_init (display_x11->screens[i]);
1237
1238   /*set the default screen */
1239   display_x11->default_screen = display_x11->screens[DefaultScreen (display_x11->xdisplay)];
1240
1241   display->device_manager = _gdk_device_manager_new (display);
1242
1243   _gdk_event_init (display);
1244
1245   attr.window_type = GDK_WINDOW_TOPLEVEL;
1246   attr.wclass = GDK_INPUT_OUTPUT;
1247   attr.x = 10;
1248   attr.y = 10;
1249   attr.width = 10;
1250   attr.height = 10;
1251   attr.event_mask = 0;
1252
1253   display_x11->leader_gdk_window = gdk_window_new (GDK_SCREEN_X11 (display_x11->default_screen)->root_window, 
1254                                                    &attr, GDK_WA_X | GDK_WA_Y);
1255   (_gdk_x11_window_get_toplevel (display_x11->leader_gdk_window))->is_leader = TRUE;
1256
1257   display_x11->leader_window = GDK_WINDOW_XID (display_x11->leader_gdk_window);
1258
1259   display_x11->leader_window_title_set = FALSE;
1260
1261   display_x11->have_render = GDK_UNKNOWN;
1262
1263 #ifdef HAVE_XFIXES
1264   if (XFixesQueryExtension (display_x11->xdisplay, 
1265                             &display_x11->xfixes_event_base, 
1266                             &ignore))
1267     {
1268       display_x11->have_xfixes = TRUE;
1269
1270       gdk_x11_register_standard_event_type (display,
1271                                             display_x11->xfixes_event_base, 
1272                                             XFixesNumberEvents);
1273     }
1274   else
1275 #endif
1276     display_x11->have_xfixes = FALSE;
1277
1278 #ifdef HAVE_XCOMPOSITE
1279   if (XCompositeQueryExtension (display_x11->xdisplay,
1280                                 &ignore, &ignore))
1281     {
1282       int major, minor;
1283
1284       XCompositeQueryVersion (display_x11->xdisplay, &major, &minor);
1285
1286       /* Prior to Composite version 0.4, composited windows clipped their
1287        * parents, so you had to use IncludeInferiors to draw to the parent
1288        * This isn't useful for our purposes, so require 0.4
1289        */
1290       display_x11->have_xcomposite = major > 0 || (major == 0 && minor >= 4);
1291     }
1292   else
1293 #endif
1294     display_x11->have_xcomposite = FALSE;
1295
1296 #ifdef HAVE_XDAMAGE
1297   if (XDamageQueryExtension (display_x11->xdisplay,
1298                              &display_x11->xdamage_event_base,
1299                              &ignore))
1300     {
1301       display_x11->have_xdamage = TRUE;
1302
1303       gdk_x11_register_standard_event_type (display,
1304                                             display_x11->xdamage_event_base,
1305                                             XDamageNumberEvents);
1306     }
1307   else
1308 #endif
1309     display_x11->have_xdamage = FALSE;
1310
1311   display_x11->have_shapes = FALSE;
1312   display_x11->have_input_shapes = FALSE;
1313
1314   if (XShapeQueryExtension (GDK_DISPLAY_XDISPLAY (display), &display_x11->shape_event_base, &ignore))
1315     {
1316       display_x11->have_shapes = TRUE;
1317 #ifdef ShapeInput
1318       if (XShapeQueryVersion (GDK_DISPLAY_XDISPLAY (display), &maj, &min))
1319         display_x11->have_input_shapes = (maj == 1 && min >= 1);
1320 #endif
1321     }
1322
1323   display_x11->trusted_client = TRUE;
1324   {
1325     Window root, child;
1326     int rootx, rooty, winx, winy;
1327     unsigned int xmask;
1328
1329     gdk_error_trap_push ();
1330     XQueryPointer (display_x11->xdisplay, 
1331                    GDK_SCREEN_X11 (display_x11->default_screen)->xroot_window,
1332                    &root, &child, &rootx, &rooty, &winx, &winy, &xmask);
1333     gdk_flush ();
1334     if (G_UNLIKELY (gdk_error_trap_pop () == BadWindow)) 
1335       {
1336         g_warning ("Connection to display %s appears to be untrusted. Pointer and keyboard grabs and inter-client communication may not work as expected.", gdk_display_get_name (display));
1337         display_x11->trusted_client = FALSE;
1338       }
1339   }
1340
1341   if (_gdk_synchronize)
1342     XSynchronize (display_x11->xdisplay, True);
1343   
1344   class_hint = XAllocClassHint();
1345   class_hint->res_name = g_get_prgname ();
1346   
1347   class_hint->res_class = (char *)gdk_get_program_class ();
1348
1349   /* XmbSetWMProperties sets the RESOURCE_NAME environment variable
1350    * from argv[0], so we just synthesize an argument array here.
1351    */
1352   argc = 1;
1353   argv[0] = g_get_prgname ();
1354   
1355   XmbSetWMProperties (display_x11->xdisplay,
1356                       display_x11->leader_window,
1357                       NULL, NULL, argv, argc, NULL, NULL,
1358                       class_hint);
1359   XFree (class_hint);
1360
1361   sm_client_id = _gdk_get_sm_client_id ();
1362   if (sm_client_id)
1363     _gdk_windowing_display_set_sm_client_id (display, sm_client_id);
1364
1365   pid = getpid ();
1366   XChangeProperty (display_x11->xdisplay,
1367                    display_x11->leader_window,
1368                    gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_PID"),
1369                    XA_CARDINAL, 32, PropModeReplace, (guchar *) & pid, 1);
1370
1371   /* We don't yet know a valid time. */
1372   display_x11->user_time = 0;
1373   
1374 #ifdef HAVE_XKB
1375   {
1376     gint xkb_major = XkbMajorVersion;
1377     gint xkb_minor = XkbMinorVersion;
1378     if (XkbLibraryVersion (&xkb_major, &xkb_minor))
1379       {
1380         xkb_major = XkbMajorVersion;
1381         xkb_minor = XkbMinorVersion;
1382             
1383         if (XkbQueryExtension (display_x11->xdisplay, 
1384                                NULL, &display_x11->xkb_event_type, NULL,
1385                                &xkb_major, &xkb_minor))
1386           {
1387             Bool detectable_autorepeat_supported;
1388             
1389             display_x11->use_xkb = TRUE;
1390
1391             XkbSelectEvents (display_x11->xdisplay,
1392                              XkbUseCoreKbd,
1393                              XkbNewKeyboardNotifyMask | XkbMapNotifyMask | XkbStateNotifyMask,
1394                              XkbNewKeyboardNotifyMask | XkbMapNotifyMask | XkbStateNotifyMask);
1395
1396             /* keep this in sync with _gdk_keymap_state_changed() */ 
1397             XkbSelectEventDetails (display_x11->xdisplay,
1398                                    XkbUseCoreKbd, XkbStateNotify,
1399                                    XkbAllStateComponentsMask,
1400                                    XkbGroupLockMask|XkbModifierLockMask);
1401
1402             XkbSetDetectableAutoRepeat (display_x11->xdisplay,
1403                                         True,
1404                                         &detectable_autorepeat_supported);
1405
1406             GDK_NOTE (MISC, g_message ("Detectable autorepeat %s.",
1407                                        detectable_autorepeat_supported ? 
1408                                        "supported" : "not supported"));
1409             
1410             display_x11->have_xkb_autorepeat = detectable_autorepeat_supported;
1411           }
1412       }
1413   }
1414 #endif
1415
1416   display_x11->use_sync = FALSE;
1417 #ifdef HAVE_XSYNC
1418   {
1419     int major, minor;
1420     int error_base, event_base;
1421     
1422     if (XSyncQueryExtension (display_x11->xdisplay,
1423                              &event_base, &error_base) &&
1424         XSyncInitialize (display_x11->xdisplay,
1425                          &major, &minor))
1426       display_x11->use_sync = TRUE;
1427   }
1428 #endif
1429
1430   _gdk_windowing_image_init (display);
1431   _gdk_input_init (display);
1432   _gdk_dnd_init (display);
1433
1434   for (i = 0; i < ScreenCount (display_x11->xdisplay); i++)
1435     _gdk_x11_screen_setup (display_x11->screens[i]);
1436
1437   g_signal_emit_by_name (display, "opened");
1438   g_signal_emit_by_name (gdk_display_manager_get(),
1439                          "display_opened", display);
1440
1441   return display;
1442 }
1443
1444 #ifdef HAVE_X11R6
1445 /*
1446  * XLib internal connection handling
1447  */
1448 typedef struct _GdkInternalConnection GdkInternalConnection;
1449
1450 struct _GdkInternalConnection
1451 {
1452   gint           fd;
1453   GSource       *source;
1454   Display       *display;
1455 };
1456
1457 static gboolean
1458 process_internal_connection (GIOChannel  *gioc,
1459                              GIOCondition cond,
1460                              gpointer     data)
1461 {
1462   GdkInternalConnection *connection = (GdkInternalConnection *)data;
1463
1464   GDK_THREADS_ENTER ();
1465
1466   XProcessInternalConnection ((Display*)connection->display, connection->fd);
1467
1468   GDK_THREADS_LEAVE ();
1469
1470   return TRUE;
1471 }
1472
1473 gulong
1474 _gdk_windowing_window_get_next_serial (GdkDisplay *display)
1475 {
1476   return NextRequest (GDK_DISPLAY_XDISPLAY (display));
1477 }
1478
1479
1480 static GdkInternalConnection *
1481 gdk_add_connection_handler (Display *display,
1482                             guint    fd)
1483 {
1484   GIOChannel *io_channel;
1485   GdkInternalConnection *connection;
1486
1487   connection = g_new (GdkInternalConnection, 1);
1488
1489   connection->fd = fd;
1490   connection->display = display;
1491   
1492   io_channel = g_io_channel_unix_new (fd);
1493   
1494   connection->source = g_io_create_watch (io_channel, G_IO_IN);
1495   g_source_set_callback (connection->source,
1496                          (GSourceFunc)process_internal_connection, connection, NULL);
1497   g_source_attach (connection->source, NULL);
1498   
1499   g_io_channel_unref (io_channel);
1500   
1501   return connection;
1502 }
1503
1504 static void
1505 gdk_remove_connection_handler (GdkInternalConnection *connection)
1506 {
1507   g_source_destroy (connection->source);
1508   g_free (connection);
1509 }
1510
1511 static void
1512 gdk_internal_connection_watch (Display  *display,
1513                                XPointer  arg,
1514                                gint      fd,
1515                                gboolean  opening,
1516                                XPointer *watch_data)
1517 {
1518   if (opening)
1519     *watch_data = (XPointer)gdk_add_connection_handler (display, fd);
1520   else
1521     gdk_remove_connection_handler ((GdkInternalConnection *)*watch_data);
1522 }
1523 #endif /* HAVE_X11R6 */
1524
1525 /**
1526  * gdk_display_get_name:
1527  * @display: a #GdkDisplay
1528  *
1529  * Gets the name of the display.
1530  * 
1531  * Returns: a string representing the display name. This string is owned
1532  * by GDK and should not be modified or freed.
1533  * 
1534  * Since: 2.2
1535  */
1536 G_CONST_RETURN gchar *
1537 gdk_display_get_name (GdkDisplay *display)
1538 {
1539   g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
1540   
1541   return (gchar *) DisplayString (GDK_DISPLAY_X11 (display)->xdisplay);
1542 }
1543
1544 /**
1545  * gdk_display_get_n_screens:
1546  * @display: a #GdkDisplay
1547  *
1548  * Gets the number of screen managed by the @display.
1549  * 
1550  * Returns: number of screens.
1551  * 
1552  * Since: 2.2
1553  */
1554 gint
1555 gdk_display_get_n_screens (GdkDisplay *display)
1556 {
1557   g_return_val_if_fail (GDK_IS_DISPLAY (display), 0);
1558   
1559   return ScreenCount (GDK_DISPLAY_X11 (display)->xdisplay);
1560 }
1561
1562 /**
1563  * gdk_display_get_screen:
1564  * @display: a #GdkDisplay
1565  * @screen_num: the screen number
1566  *
1567  * Returns a screen object for one of the screens of the display.
1568  *
1569  * Returns: the #GdkScreen object
1570  *
1571  * Since: 2.2
1572  */
1573 GdkScreen *
1574 gdk_display_get_screen (GdkDisplay *display, 
1575                         gint        screen_num)
1576 {
1577   g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
1578   g_return_val_if_fail (ScreenCount (GDK_DISPLAY_X11 (display)->xdisplay) > screen_num, NULL);
1579   
1580   return GDK_DISPLAY_X11 (display)->screens[screen_num];
1581 }
1582
1583 /**
1584  * gdk_display_get_default_screen:
1585  * @display: a #GdkDisplay
1586  *
1587  * Get the default #GdkScreen for @display.
1588  * 
1589  * Returns: the default #GdkScreen object for @display
1590  *
1591  * Since: 2.2
1592  */
1593 GdkScreen *
1594 gdk_display_get_default_screen (GdkDisplay *display)
1595 {
1596   g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
1597   
1598   return GDK_DISPLAY_X11 (display)->default_screen;
1599 }
1600
1601 gboolean
1602 _gdk_x11_display_is_root_window (GdkDisplay *display,
1603                                  Window      xroot_window)
1604 {
1605   GdkDisplayX11 *display_x11;
1606   gint i;
1607   
1608   g_return_val_if_fail (GDK_IS_DISPLAY (display), FALSE);
1609   
1610   display_x11 = GDK_DISPLAY_X11 (display);
1611   
1612   for (i = 0; i < ScreenCount (display_x11->xdisplay); i++)
1613     {
1614       if (GDK_SCREEN_XROOTWIN (display_x11->screens[i]) == xroot_window)
1615         return TRUE;
1616     }
1617   return FALSE;
1618 }
1619
1620 struct XPointerUngrabInfo {
1621   GdkDisplay *display;
1622   guint32 time;
1623 };
1624
1625 static void
1626 device_ungrab_callback (GdkDisplay *display,
1627                         gpointer    data,
1628                         gulong      serial)
1629 {
1630   GdkDevice *device = data;
1631
1632   _gdk_display_device_grab_update (display, device, serial);
1633 }
1634
1635
1636 #define XSERVER_TIME_IS_LATER(time1, time2)                        \
1637   ( (( time1 > time2 ) && ( time1 - time2 < ((guint32)-1)/2 )) ||  \
1638     (( time1 < time2 ) && ( time2 - time1 > ((guint32)-1)/2 ))     \
1639   )
1640
1641 /**
1642  * gdk_device_ungrab:
1643  * @device: a #GdkDevice
1644  * @time_: a timestap (e.g. %GDK_CURRENT_TIME).
1645  *
1646  * Release any grab on @device.
1647  *
1648  * Since: 3.0
1649  */
1650 void
1651 gdk_device_ungrab (GdkDevice  *device,
1652                    guint32     time_)
1653 {
1654   GdkDisplay *display;
1655   Display *xdisplay;
1656   GdkDeviceGrabInfo *grab;
1657   unsigned long serial;
1658
1659   g_return_if_fail (GDK_IS_DEVICE (device));
1660
1661   display = gdk_device_get_display (device);
1662   xdisplay = GDK_DISPLAY_XDISPLAY (display);
1663
1664   serial = NextRequest (xdisplay);
1665
1666   GDK_DEVICE_GET_CLASS (device)->ungrab (device, time_);
1667   XFlush (xdisplay);
1668
1669   grab = _gdk_display_get_last_device_grab (display, device);
1670   if (grab &&
1671       (time_ == GDK_CURRENT_TIME ||
1672        grab->time == GDK_CURRENT_TIME ||
1673        !XSERVER_TIME_IS_LATER (grab->time, time_)))
1674     {
1675       grab->serial_end = serial;
1676       _gdk_x11_roundtrip_async (display,
1677                                 device_ungrab_callback,
1678                                 device);
1679     }
1680 }
1681
1682 /**
1683  * gdk_display_beep:
1684  * @display: a #GdkDisplay
1685  *
1686  * Emits a short beep on @display
1687  *
1688  * Since: 2.2
1689  */
1690 void
1691 gdk_display_beep (GdkDisplay *display)
1692 {
1693   g_return_if_fail (GDK_IS_DISPLAY (display));
1694
1695 #ifdef HAVE_XKB
1696   XkbBell (GDK_DISPLAY_XDISPLAY (display), None, 0, None);
1697 #else
1698   XBell (GDK_DISPLAY_XDISPLAY (display), 0);
1699 #endif
1700 }
1701
1702 /**
1703  * gdk_display_sync:
1704  * @display: a #GdkDisplay
1705  *
1706  * Flushes any requests queued for the windowing system and waits until all
1707  * requests have been handled. This is often used for making sure that the
1708  * display is synchronized with the current state of the program. Calling
1709  * gdk_display_sync() before gdk_error_trap_pop() makes sure that any errors
1710  * generated from earlier requests are handled before the error trap is 
1711  * removed.
1712  *
1713  * This is most useful for X11. On windowing systems where requests are
1714  * handled synchronously, this function will do nothing.
1715  *
1716  * Since: 2.2
1717  */
1718 void
1719 gdk_display_sync (GdkDisplay *display)
1720 {
1721   g_return_if_fail (GDK_IS_DISPLAY (display));
1722   
1723   XSync (GDK_DISPLAY_XDISPLAY (display), False);
1724 }
1725
1726 /**
1727  * gdk_display_flush:
1728  * @display: a #GdkDisplay
1729  *
1730  * Flushes any requests queued for the windowing system; this happens automatically
1731  * when the main loop blocks waiting for new events, but if your application
1732  * is drawing without returning control to the main loop, you may need
1733  * to call this function explicitely. A common case where this function
1734  * needs to be called is when an application is executing drawing commands
1735  * from a thread other than the thread where the main loop is running.
1736  *
1737  * This is most useful for X11. On windowing systems where requests are
1738  * handled synchronously, this function will do nothing.
1739  *
1740  * Since: 2.4
1741  */
1742 void 
1743 gdk_display_flush (GdkDisplay *display)
1744 {
1745   g_return_if_fail (GDK_IS_DISPLAY (display));
1746
1747   if (!display->closed)
1748     XFlush (GDK_DISPLAY_XDISPLAY (display));
1749 }
1750
1751 /**
1752  * gdk_display_get_default_group:
1753  * @display: a #GdkDisplay
1754  * 
1755  * Returns the default group leader window for all toplevel windows
1756  * on @display. This window is implicitly created by GDK. 
1757  * See gdk_window_set_group().
1758  * 
1759  * Return value: The default group leader window for @display
1760  *
1761  * Since: 2.4
1762  **/
1763 GdkWindow *
1764 gdk_display_get_default_group (GdkDisplay *display)
1765 {
1766   g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
1767
1768   return GDK_DISPLAY_X11 (display)->leader_gdk_window;
1769 }
1770
1771 /**
1772  * gdk_x11_display_grab:
1773  * @display: a #GdkDisplay 
1774  * 
1775  * Call XGrabServer() on @display. 
1776  * To ungrab the display again, use gdk_x11_display_ungrab(). 
1777  *
1778  * gdk_x11_display_grab()/gdk_x11_display_ungrab() calls can be nested.
1779  *
1780  * Since: 2.2
1781  **/
1782 void
1783 gdk_x11_display_grab (GdkDisplay *display)
1784 {
1785   GdkDisplayX11 *display_x11;
1786   
1787   g_return_if_fail (GDK_IS_DISPLAY (display));
1788   
1789   display_x11 = GDK_DISPLAY_X11 (display);
1790   
1791   if (display_x11->grab_count == 0)
1792     XGrabServer (display_x11->xdisplay);
1793   display_x11->grab_count++;
1794 }
1795
1796 /**
1797  * gdk_x11_display_ungrab:
1798  * @display: a #GdkDisplay
1799  * 
1800  * Ungrab @display after it has been grabbed with 
1801  * gdk_x11_display_grab(). 
1802  *
1803  * Since: 2.2
1804  **/
1805 void
1806 gdk_x11_display_ungrab (GdkDisplay *display)
1807 {
1808   GdkDisplayX11 *display_x11;
1809   
1810   g_return_if_fail (GDK_IS_DISPLAY (display));
1811   
1812   display_x11 = GDK_DISPLAY_X11 (display);;
1813   g_return_if_fail (display_x11->grab_count > 0);
1814   
1815   display_x11->grab_count--;
1816   if (display_x11->grab_count == 0)
1817     {
1818       XUngrabServer (display_x11->xdisplay);
1819       XFlush (display_x11->xdisplay);
1820     }
1821 }
1822
1823 static void
1824 gdk_display_x11_dispose (GObject *object)
1825 {
1826   GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (object);
1827   gint           i;
1828
1829   g_list_foreach (display_x11->input_devices, (GFunc) g_object_run_dispose, NULL);
1830
1831   for (i = 0; i < ScreenCount (display_x11->xdisplay); i++)
1832     _gdk_screen_close (display_x11->screens[i]);
1833
1834   if (display_x11->event_source)
1835     {
1836       g_source_destroy (display_x11->event_source);
1837       g_source_unref (display_x11->event_source);
1838       display_x11->event_source = NULL;
1839     }
1840
1841   G_OBJECT_CLASS (_gdk_display_x11_parent_class)->dispose (object);
1842 }
1843
1844 static void
1845 gdk_display_x11_finalize (GObject *object)
1846 {
1847   GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (object);
1848   gint           i;
1849
1850   /* Keymap */
1851   if (display_x11->keymap)
1852     g_object_unref (display_x11->keymap);
1853
1854   /* Free motif Dnd */
1855   if (display_x11->motif_target_lists)
1856     {
1857       for (i = 0; i < display_x11->motif_n_target_lists; i++)
1858         g_list_free (display_x11->motif_target_lists[i]);
1859       g_free (display_x11->motif_target_lists);
1860     }
1861
1862   _gdk_x11_cursor_display_finalize (GDK_DISPLAY_OBJECT(display_x11));
1863
1864   /* Atom Hashtable */
1865   g_hash_table_destroy (display_x11->atom_from_virtual);
1866   g_hash_table_destroy (display_x11->atom_to_virtual);
1867
1868   /* Leader Window */
1869   XDestroyWindow (display_x11->xdisplay, display_x11->leader_window);
1870
1871   /* list of filters for client messages */
1872   g_list_foreach (display_x11->client_filters, (GFunc) g_free, NULL);
1873   g_list_free (display_x11->client_filters);
1874
1875   /* List of event window extraction functions */
1876   g_slist_foreach (display_x11->event_types, (GFunc)g_free, NULL);
1877   g_slist_free (display_x11->event_types);
1878
1879   /* input GdkDevice list */
1880   g_list_foreach (display_x11->input_devices, (GFunc) g_object_unref, NULL);
1881   g_list_free (display_x11->input_devices);
1882
1883   /* input GdkWindow list */
1884   g_list_foreach (display_x11->input_windows, (GFunc) g_free, NULL);
1885   g_list_free (display_x11->input_windows);
1886
1887   /* Free all GdkScreens */
1888   for (i = 0; i < ScreenCount (display_x11->xdisplay); i++)
1889     g_object_unref (display_x11->screens[i]);
1890   g_free (display_x11->screens);
1891
1892   g_free (display_x11->startup_notification_id);
1893
1894   /* X ID hashtable */
1895   g_hash_table_destroy (display_x11->xid_ht);
1896
1897   XCloseDisplay (display_x11->xdisplay);
1898
1899   G_OBJECT_CLASS (_gdk_display_x11_parent_class)->finalize (object);
1900 }
1901
1902 /**
1903  * gdk_x11_lookup_xdisplay:
1904  * @xdisplay: a pointer to an X Display
1905  * 
1906  * Find the #GdkDisplay corresponding to @display, if any exists.
1907  * 
1908  * Return value: the #GdkDisplay, if found, otherwise %NULL.
1909  *
1910  * Since: 2.2
1911  **/
1912 GdkDisplay *
1913 gdk_x11_lookup_xdisplay (Display *xdisplay)
1914 {
1915   GSList *tmp_list;
1916
1917   for (tmp_list = _gdk_displays; tmp_list; tmp_list = tmp_list->next)
1918     {
1919       if (GDK_DISPLAY_XDISPLAY (tmp_list->data) == xdisplay)
1920         return tmp_list->data;
1921     }
1922   
1923   return NULL;
1924 }
1925
1926 /**
1927  * _gdk_x11_display_screen_for_xrootwin:
1928  * @display: a #GdkDisplay
1929  * @xrootwin: window ID for one of of the screen's of the display.
1930  * 
1931  * Given the root window ID of one of the screen's of a #GdkDisplay,
1932  * finds the screen.
1933  * 
1934  * Return value: the #GdkScreen corresponding to @xrootwin, or %NULL.
1935  **/
1936 GdkScreen *
1937 _gdk_x11_display_screen_for_xrootwin (GdkDisplay *display,
1938                                       Window      xrootwin)
1939 {
1940   gint i;
1941
1942   for (i = 0; i < ScreenCount (GDK_DISPLAY_X11 (display)->xdisplay); i++)
1943     {
1944       GdkScreen *screen = gdk_display_get_screen (display, i);
1945       if (GDK_SCREEN_XROOTWIN (screen) == xrootwin)
1946         return screen;
1947     }
1948
1949   return NULL;
1950 }
1951
1952 /**
1953  * gdk_x11_display_get_xdisplay:
1954  * @display: a #GdkDisplay
1955  * @returns: an X display.
1956  *
1957  * Returns the X display of a #GdkDisplay.
1958  *
1959  * Since: 2.2
1960  */
1961 Display *
1962 gdk_x11_display_get_xdisplay (GdkDisplay *display)
1963 {
1964   g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
1965   return GDK_DISPLAY_X11 (display)->xdisplay;
1966 }
1967
1968 void
1969 _gdk_windowing_set_default_display (GdkDisplay *display)
1970 {
1971   GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
1972   const gchar *startup_id;
1973   
1974   if (!display)
1975     {
1976       gdk_display = NULL;
1977       return;
1978     }
1979
1980   gdk_display = GDK_DISPLAY_XDISPLAY (display);
1981
1982   g_free (display_x11->startup_notification_id);
1983   display_x11->startup_notification_id = NULL;
1984   
1985   startup_id = g_getenv ("DESKTOP_STARTUP_ID");
1986   if (startup_id && *startup_id != '\0')
1987     {
1988       gchar *time_str;
1989
1990       if (!g_utf8_validate (startup_id, -1, NULL))
1991         g_warning ("DESKTOP_STARTUP_ID contains invalid UTF-8");
1992       else
1993         display_x11->startup_notification_id = g_strdup (startup_id);
1994
1995       /* Find the launch time from the startup_id, if it's there.  Newer spec
1996        * states that the startup_id is of the form <unique>_TIME<timestamp>
1997        */
1998       time_str = g_strrstr (startup_id, "_TIME");
1999       if (time_str != NULL)
2000         {
2001           gulong retval;
2002           gchar *end;
2003           errno = 0;
2004
2005           /* Skip past the "_TIME" part */
2006           time_str += 5;
2007
2008           retval = strtoul (time_str, &end, 0);
2009           if (end != time_str && errno == 0)
2010             display_x11->user_time = retval;
2011         }
2012       
2013       /* Clear the environment variable so it won't be inherited by
2014        * child processes and confuse things.  
2015        */
2016       g_unsetenv ("DESKTOP_STARTUP_ID");
2017
2018       /* Set the startup id on the leader window so it
2019        * applies to all windows we create on this display
2020        */
2021       XChangeProperty (display_x11->xdisplay,
2022                        display_x11->leader_window,
2023                        gdk_x11_get_xatom_by_name_for_display (display, "_NET_STARTUP_ID"),
2024                        gdk_x11_get_xatom_by_name_for_display (display, "UTF8_STRING"), 8,
2025                        PropModeReplace,
2026                        (guchar *)startup_id, strlen (startup_id));
2027     }
2028 }
2029
2030 static void
2031 broadcast_xmessage (GdkDisplay *display,
2032                     const char *message_type,
2033                     const char *message_type_begin,
2034                     const char *message)
2035 {
2036   Display *xdisplay = GDK_DISPLAY_XDISPLAY (display);
2037   GdkScreen *screen = gdk_display_get_default_screen (display);
2038   GdkWindow *root_window = gdk_screen_get_root_window (screen);
2039   Window xroot_window = GDK_WINDOW_XID (root_window);
2040   
2041   Atom type_atom;
2042   Atom type_atom_begin;
2043   Window xwindow;
2044
2045   if (!G_LIKELY (GDK_DISPLAY_X11 (display)->trusted_client))
2046     return;
2047
2048   {
2049     XSetWindowAttributes attrs;
2050
2051     attrs.override_redirect = True;
2052     attrs.event_mask = PropertyChangeMask | StructureNotifyMask;
2053
2054     xwindow =
2055       XCreateWindow (xdisplay,
2056                      xroot_window,
2057                      -100, -100, 1, 1,
2058                      0,
2059                      CopyFromParent,
2060                      CopyFromParent,
2061                      (Visual *)CopyFromParent,
2062                      CWOverrideRedirect | CWEventMask,
2063                      &attrs);
2064   }
2065
2066   type_atom = gdk_x11_get_xatom_by_name_for_display (display,
2067                                                      message_type);
2068   type_atom_begin = gdk_x11_get_xatom_by_name_for_display (display,
2069                                                            message_type_begin);
2070   
2071   {
2072     XClientMessageEvent xclient;
2073     const char *src;
2074     const char *src_end;
2075     char *dest;
2076     char *dest_end;
2077     
2078                 memset(&xclient, 0, sizeof (xclient));
2079     xclient.type = ClientMessage;
2080     xclient.message_type = type_atom_begin;
2081     xclient.display =xdisplay;
2082     xclient.window = xwindow;
2083     xclient.format = 8;
2084
2085     src = message;
2086     src_end = message + strlen (message) + 1; /* +1 to include nul byte */
2087     
2088     while (src != src_end)
2089       {
2090         dest = &xclient.data.b[0];
2091         dest_end = dest + 20;        
2092         
2093         while (dest != dest_end &&
2094                src != src_end)
2095           {
2096             *dest = *src;
2097             ++dest;
2098             ++src;
2099           }
2100
2101         while (dest != dest_end)
2102           {
2103             *dest = 0;
2104             ++dest;
2105           }
2106         
2107         XSendEvent (xdisplay,
2108                     xroot_window,
2109                     False,
2110                     PropertyChangeMask,
2111                     (XEvent *)&xclient);
2112
2113         xclient.message_type = type_atom;
2114       }
2115   }
2116
2117   XDestroyWindow (xdisplay, xwindow);
2118   XFlush (xdisplay);
2119 }
2120
2121 /**
2122  * gdk_x11_display_broadcast_startup_message:
2123  * @display: a #GdkDisplay
2124  * @message_type: startup notification message type ("new", "change",
2125  * or "remove")
2126  * @...: a list of key/value pairs (as strings), terminated by a
2127  * %NULL key. (A %NULL value for a key will cause that key to be
2128  * skipped in the output.)
2129  *
2130  * Sends a startup notification message of type @message_type to
2131  * @display. 
2132  *
2133  * This is a convenience function for use by code that implements the
2134  * freedesktop startup notification specification. Applications should
2135  * not normally need to call it directly. See the <ulink
2136  * url="http://standards.freedesktop.org/startup-notification-spec/startup-notification-latest.txt">Startup
2137  * Notification Protocol specification</ulink> for
2138  * definitions of the message types and keys that can be used.
2139  *
2140  * Since: 2.12
2141  **/
2142 void
2143 gdk_x11_display_broadcast_startup_message (GdkDisplay *display,
2144                                            const char *message_type,
2145                                            ...)
2146 {
2147   GString *message;
2148   va_list ap;
2149   const char *key, *value, *p;
2150
2151   message = g_string_new (message_type);
2152   g_string_append_c (message, ':');
2153
2154   va_start (ap, message_type);
2155   while ((key = va_arg (ap, const char *)))
2156     {
2157       value = va_arg (ap, const char *);
2158       if (!value)
2159         continue;
2160
2161       g_string_append_printf (message, " %s=\"", key);
2162       for (p = value; *p; p++)
2163         {
2164           switch (*p)
2165             {
2166             case ' ':
2167             case '"':
2168             case '\\':
2169               g_string_append_c (message, '\\');
2170               break;
2171             }
2172
2173           g_string_append_c (message, *p);
2174         }
2175       g_string_append_c (message, '\"');
2176     }
2177   va_end (ap);
2178
2179   broadcast_xmessage (display,
2180                       "_NET_STARTUP_INFO",
2181                       "_NET_STARTUP_INFO_BEGIN",
2182                       message->str);
2183
2184   g_string_free (message, TRUE);
2185 }
2186
2187 /**
2188  * gdk_notify_startup_complete:
2189  * 
2190  * Indicates to the GUI environment that the application has finished
2191  * loading. If the applications opens windows, this function is
2192  * normally called after opening the application's initial set of
2193  * windows.
2194  * 
2195  * GTK+ will call this function automatically after opening the first
2196  * #GtkWindow unless gtk_window_set_auto_startup_notification() is called 
2197  * to disable that feature.
2198  *
2199  * Since: 2.2
2200  **/
2201 void
2202 gdk_notify_startup_complete (void)
2203 {
2204   GdkDisplay *display;
2205   GdkDisplayX11 *display_x11;
2206
2207   display = gdk_display_get_default ();
2208   if (!display)
2209     return;
2210   
2211   display_x11 = GDK_DISPLAY_X11 (display);
2212
2213   if (display_x11->startup_notification_id == NULL)
2214     return;
2215
2216   gdk_notify_startup_complete_with_id (display_x11->startup_notification_id);
2217 }
2218
2219 /**
2220  * gdk_notify_startup_complete_with_id:
2221  * @startup_id: a startup-notification identifier, for which notification
2222  *              process should be completed
2223  * 
2224  * Indicates to the GUI environment that the application has finished
2225  * loading, using a given identifier.
2226  * 
2227  * GTK+ will call this function automatically for #GtkWindow with custom
2228  * startup-notification identifier unless
2229  * gtk_window_set_auto_startup_notification() is called to disable
2230  * that feature.
2231  *
2232  * Since: 2.12
2233  **/
2234 void
2235 gdk_notify_startup_complete_with_id (const gchar* startup_id)
2236 {
2237   GdkDisplay *display;
2238
2239   display = gdk_display_get_default ();
2240   if (!display)
2241     return;
2242
2243   gdk_x11_display_broadcast_startup_message (display, "remove",
2244                                              "ID", startup_id,
2245                                              NULL);
2246 }
2247
2248 /**
2249  * gdk_display_supports_selection_notification:
2250  * @display: a #GdkDisplay
2251  * 
2252  * Returns whether #GdkEventOwnerChange events will be 
2253  * sent when the owner of a selection changes.
2254  * 
2255  * Return value: whether #GdkEventOwnerChange events will 
2256  *               be sent.
2257  *
2258  * Since: 2.6
2259  **/
2260 gboolean 
2261 gdk_display_supports_selection_notification (GdkDisplay *display)
2262 {
2263   GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
2264
2265   return display_x11->have_xfixes;
2266 }
2267
2268 /**
2269  * gdk_display_request_selection_notification:
2270  * @display: a #GdkDisplay
2271  * @selection: the #GdkAtom naming the selection for which
2272  *             ownership change notification is requested
2273  * 
2274  * Request #GdkEventOwnerChange events for ownership changes
2275  * of the selection named by the given atom.
2276  * 
2277  * Return value: whether #GdkEventOwnerChange events will 
2278  *               be sent.
2279  *
2280  * Since: 2.6
2281  **/
2282 gboolean
2283 gdk_display_request_selection_notification (GdkDisplay *display,
2284                                             GdkAtom     selection)
2285
2286 {
2287 #ifdef HAVE_XFIXES
2288   GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
2289   Atom atom;
2290
2291   if (display_x11->have_xfixes)
2292     {
2293       atom = gdk_x11_atom_to_xatom_for_display (display, 
2294                                                 selection);
2295       XFixesSelectSelectionInput (display_x11->xdisplay, 
2296                                   display_x11->leader_window,
2297                                   atom,
2298                                   XFixesSetSelectionOwnerNotifyMask |
2299                                   XFixesSelectionWindowDestroyNotifyMask |
2300                                   XFixesSelectionClientCloseNotifyMask);
2301       return TRUE;
2302     }
2303   else
2304 #endif
2305     return FALSE;
2306 }
2307
2308 /**
2309  * gdk_display_supports_clipboard_persistence
2310  * @display: a #GdkDisplay
2311  *
2312  * Returns whether the speicifed display supports clipboard
2313  * persistance; i.e. if it's possible to store the clipboard data after an
2314  * application has quit. On X11 this checks if a clipboard daemon is
2315  * running.
2316  *
2317  * Returns: %TRUE if the display supports clipboard persistance.
2318  *
2319  * Since: 2.6
2320  */
2321 gboolean
2322 gdk_display_supports_clipboard_persistence (GdkDisplay *display)
2323 {
2324   Atom clipboard_manager;
2325
2326   /* It might make sense to cache this */
2327   clipboard_manager = gdk_x11_get_xatom_by_name_for_display (display, "CLIPBOARD_MANAGER");
2328   return XGetSelectionOwner (GDK_DISPLAY_X11 (display)->xdisplay, clipboard_manager) != None;
2329 }
2330
2331 /**
2332  * gdk_display_store_clipboard
2333  * @display:          a #GdkDisplay
2334  * @clipboard_window: a #GdkWindow belonging to the clipboard owner
2335  * @time_:            a timestamp
2336  * @targets:          an array of targets that should be saved, or %NULL 
2337  *                    if all available targets should be saved.
2338  * @n_targets:        length of the @targets array
2339  *
2340  * Issues a request to the clipboard manager to store the
2341  * clipboard data. On X11, this is a special program that works
2342  * according to the freedesktop clipboard specification, available at
2343  * <ulink url="http://www.freedesktop.org/Standards/clipboard-manager-spec">
2344  * http://www.freedesktop.org/Standards/clipboard-manager-spec</ulink>.
2345  *
2346  * Since: 2.6
2347  */
2348 void
2349 gdk_display_store_clipboard (GdkDisplay    *display,
2350                              GdkWindow     *clipboard_window,
2351                              guint32        time_,
2352                              const GdkAtom *targets,
2353                              gint           n_targets)
2354 {
2355   GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
2356   Atom clipboard_manager, save_targets;
2357
2358   g_return_if_fail (GDK_WINDOW_IS_X11 (clipboard_window));
2359
2360   clipboard_manager = gdk_x11_get_xatom_by_name_for_display (display, "CLIPBOARD_MANAGER");
2361   save_targets = gdk_x11_get_xatom_by_name_for_display (display, "SAVE_TARGETS");
2362
2363   gdk_error_trap_push ();
2364
2365   if (XGetSelectionOwner (display_x11->xdisplay, clipboard_manager) != None)
2366     {
2367       Atom property_name = None;
2368       Atom *xatoms;
2369       int i;
2370       
2371       if (n_targets > 0)
2372         {
2373           property_name = gdk_x11_atom_to_xatom_for_display (display, _gdk_selection_property);
2374
2375           xatoms = g_new (Atom, n_targets);
2376           for (i = 0; i < n_targets; i++)
2377             xatoms[i] = gdk_x11_atom_to_xatom_for_display (display, targets[i]);
2378
2379           XChangeProperty (display_x11->xdisplay, GDK_WINDOW_XID (clipboard_window),
2380                            property_name, XA_ATOM,
2381                            32, PropModeReplace, (guchar *)xatoms, n_targets);
2382           g_free (xatoms);
2383
2384         }
2385       
2386       XConvertSelection (display_x11->xdisplay,
2387                          clipboard_manager, save_targets, property_name,
2388                          GDK_WINDOW_XID (clipboard_window), time_);
2389       
2390     }
2391   gdk_error_trap_pop ();
2392
2393 }
2394
2395 /**
2396  * gdk_x11_display_get_user_time:
2397  * @display: a #GdkDisplay
2398  *
2399  * Returns the timestamp of the last user interaction on 
2400  * @display. The timestamp is taken from events caused
2401  * by user interaction such as key presses or pointer 
2402  * movements. See gdk_x11_window_set_user_time().
2403  *
2404  * Returns: the timestamp of the last user interaction 
2405  *
2406  * Since: 2.8
2407  */
2408 guint32
2409 gdk_x11_display_get_user_time (GdkDisplay *display)
2410 {
2411   return GDK_DISPLAY_X11 (display)->user_time;
2412 }
2413
2414 /**
2415  * gdk_display_supports_shapes:
2416  * @display: a #GdkDisplay
2417  *
2418  * Returns %TRUE if gdk_window_shape_combine_mask() can
2419  * be used to create shaped windows on @display.
2420  *
2421  * Returns: %TRUE if shaped windows are supported 
2422  *
2423  * Since: 2.10
2424  */
2425 gboolean 
2426 gdk_display_supports_shapes (GdkDisplay *display)
2427 {
2428   return GDK_DISPLAY_X11 (display)->have_shapes;
2429 }
2430
2431 /**
2432  * gdk_display_supports_input_shapes:
2433  * @display: a #GdkDisplay
2434  *
2435  * Returns %TRUE if gdk_window_input_shape_combine_mask() can
2436  * be used to modify the input shape of windows on @display.
2437  *
2438  * Returns: %TRUE if windows with modified input shape are supported 
2439  *
2440  * Since: 2.10
2441  */
2442 gboolean 
2443 gdk_display_supports_input_shapes (GdkDisplay *display)
2444 {
2445   return GDK_DISPLAY_X11 (display)->have_input_shapes;
2446 }
2447
2448
2449 /**
2450  * gdk_x11_display_get_startup_notification_id:
2451  * @display: a #GdkDisplay
2452  *
2453  * Gets the startup notification ID for a display.
2454  * 
2455  * Returns: the startup notification ID for @display
2456  *
2457  * Since: 2.12
2458  */
2459 G_CONST_RETURN gchar *
2460 gdk_x11_display_get_startup_notification_id (GdkDisplay *display)
2461 {
2462   return GDK_DISPLAY_X11 (display)->startup_notification_id;
2463 }
2464
2465 /**
2466  * gdk_display_supports_composite:
2467  * @display: a #GdkDisplay
2468  *
2469  * Returns %TRUE if gdk_window_set_composited() can be used
2470  * to redirect drawing on the window using compositing.
2471  *
2472  * Currently this only works on X11 with XComposite and
2473  * XDamage extensions available.
2474  *
2475  * Returns: %TRUE if windows may be composited.
2476  *
2477  * Since: 2.12
2478  */
2479 gboolean
2480 gdk_display_supports_composite (GdkDisplay *display)
2481 {
2482   GdkDisplayX11 *x11_display = GDK_DISPLAY_X11 (display);
2483
2484   return x11_display->have_xcomposite &&
2485          x11_display->have_xdamage &&
2486          x11_display->have_xfixes;
2487 }
2488
2489 /**
2490  * gdk_display_list_devices:
2491  * @display: a #GdkDisplay
2492  *
2493  * Returns the list of available input devices attached to @display.
2494  * The list is statically allocated and should not be freed.
2495  *
2496  * Return value: a list of #GdkDevice
2497  *
2498  * Since: 2.2
2499  *
2500  * Deprecated: 3.0: Use gdk_device_manager_list_devices() instead.
2501  **/
2502 GList *
2503 gdk_display_list_devices (GdkDisplay *display)
2504 {
2505   g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
2506
2507   return GDK_DISPLAY_X11 (display)->input_devices;
2508 }
2509
2510 /**
2511  * gdk_event_send_client_message_for_display:
2512  * @display: the #GdkDisplay for the window where the message is to be sent.
2513  * @event: the #GdkEvent to send, which should be a #GdkEventClient.
2514  * @winid: the window to send the client message to.
2515  *
2516  * On X11, sends an X ClientMessage event to a given window. On
2517  * Windows, sends a message registered with the name
2518  * GDK_WIN32_CLIENT_MESSAGE.
2519  *
2520  * This could be used for communicating between different
2521  * applications, though the amount of data is limited to 20 bytes on
2522  * X11, and to just four bytes on Windows.
2523  *
2524  * Returns: non-zero on success.
2525  *
2526  * Since: 2.2
2527  */
2528 gboolean
2529 gdk_event_send_client_message_for_display (GdkDisplay     *display,
2530                                            GdkEvent       *event,
2531                                            GdkNativeWindow winid)
2532 {
2533   XEvent sev;
2534
2535   g_return_val_if_fail(event != NULL, FALSE);
2536
2537   /* Set up our event to send, with the exception of its target window */
2538   sev.xclient.type = ClientMessage;
2539   sev.xclient.display = GDK_DISPLAY_XDISPLAY (display);
2540   sev.xclient.format = event->client.data_format;
2541   sev.xclient.window = winid;
2542   memcpy(&sev.xclient.data, &event->client.data, sizeof (sev.xclient.data));
2543   sev.xclient.message_type = gdk_x11_atom_to_xatom_for_display (display, event->client.message_type);
2544
2545   return _gdk_send_xevent (display, winid, False, NoEventMask, &sev);
2546 }
2547
2548 /**
2549  * gdk_display_add_client_message_filter:
2550  * @display: a #GdkDisplay for which this message filter applies
2551  * @message_type: the type of ClientMessage events to receive.
2552  *   This will be checked against the @message_type field
2553  *   of the XClientMessage event struct.
2554  * @func: the function to call to process the event.
2555  * @data: user data to pass to @func.
2556  *
2557  * Adds a filter to be called when X ClientMessage events are received.
2558  * See gdk_window_add_filter() if you are interested in filtering other
2559  * types of events.
2560  *
2561  * Since: 2.2
2562  **/
2563 void
2564 gdk_display_add_client_message_filter (GdkDisplay   *display,
2565                                        GdkAtom       message_type,
2566                                        GdkFilterFunc func,
2567                                        gpointer      data)
2568 {
2569   GdkClientFilter *filter;
2570   g_return_if_fail (GDK_IS_DISPLAY (display));
2571   filter = g_new (GdkClientFilter, 1);
2572
2573   filter->type = message_type;
2574   filter->function = func;
2575   filter->data = data;
2576
2577   GDK_DISPLAY_X11(display)->client_filters =
2578     g_list_append (GDK_DISPLAY_X11 (display)->client_filters,
2579                    filter);
2580 }
2581
2582 /**
2583  * gdk_add_client_message_filter:
2584  * @message_type: the type of ClientMessage events to receive. This will be
2585  *     checked against the <structfield>message_type</structfield> field of the
2586  *     XClientMessage event struct.
2587  * @func: the function to call to process the event.
2588  * @data: user data to pass to @func.
2589  *
2590  * Adds a filter to the default display to be called when X ClientMessage events
2591  * are received. See gdk_display_add_client_message_filter().
2592  **/
2593 void
2594 gdk_add_client_message_filter (GdkAtom       message_type,
2595                                GdkFilterFunc func,
2596                                gpointer      data)
2597 {
2598   gdk_display_add_client_message_filter (gdk_display_get_default (),
2599                                          message_type, func, data);
2600 }
2601
2602 /*
2603  *--------------------------------------------------------------
2604  * gdk_flush
2605  *
2606  *   Flushes the Xlib output buffer and then waits
2607  *   until all requests have been received and processed
2608  *   by the X server. The only real use for this function
2609  *   is in dealing with XShm.
2610  *
2611  * Arguments:
2612  *
2613  * Results:
2614  *
2615  * Side effects:
2616  *
2617  *--------------------------------------------------------------
2618  */
2619 void
2620 gdk_flush (void)
2621 {
2622   GSList *tmp_list = _gdk_displays;
2623
2624   while (tmp_list)
2625     {
2626       XSync (GDK_DISPLAY_XDISPLAY (tmp_list->data), False);
2627       tmp_list = tmp_list->next;
2628     }
2629 }
2630
2631 /**
2632  * gdk_x11_register_standard_event_type:
2633  * @display: a #GdkDisplay
2634  * @event_base: first event type code to register
2635  * @n_events: number of event type codes to register
2636  *
2637  * Registers interest in receiving extension events with type codes
2638  * between @event_base and <literal>event_base + n_events - 1</literal>.
2639  * The registered events must have the window field in the same place
2640  * as core X events (this is not the case for e.g. XKB extension events).
2641  *
2642  * If an event type is registered, events of this type will go through
2643  * global and window-specific filters (see gdk_window_add_filter()).
2644  * Unregistered events will only go through global filters.
2645  * GDK may register the events of some X extensions on its own.
2646  *
2647  * This function should only be needed in unusual circumstances, e.g.
2648  * when filtering XInput extension events on the root window.
2649  *
2650  * Since: 2.4
2651  **/
2652 void
2653 gdk_x11_register_standard_event_type (GdkDisplay *display,
2654                                       gint        event_base,
2655                                       gint        n_events)
2656 {
2657   GdkEventTypeX11 *event_type;
2658   GdkDisplayX11 *display_x11;
2659
2660   display_x11 = GDK_DISPLAY_X11 (display);
2661   event_type = g_new (GdkEventTypeX11, 1);
2662
2663   event_type->base = event_base;
2664   event_type->n_events = n_events;
2665
2666   display_x11->event_types = g_slist_prepend (display_x11->event_types, event_type);
2667 }