]> Pileus Git - ~andy/gtk/blob - gdk/x11/gdkdisplay-x11.c
Add annotations
[~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->xdisplay = xdisplay;
1201
1202 #ifdef HAVE_X11R6  
1203   /* Set up handlers for Xlib internal connections */
1204   XAddConnectionWatch (xdisplay, gdk_internal_connection_watch, NULL);
1205 #endif /* HAVE_X11R6 */
1206   
1207   _gdk_x11_precache_atoms (display, precache_atoms, G_N_ELEMENTS (precache_atoms));
1208
1209   /* RandR must be initialized before we initialize the screens */
1210   display_x11->have_randr13 = FALSE;
1211 #ifdef HAVE_RANDR
1212   if (XRRQueryExtension (display_x11->xdisplay,
1213                          &display_x11->xrandr_event_base, &ignore))
1214   {
1215       int major, minor;
1216       
1217       XRRQueryVersion (display_x11->xdisplay, &major, &minor);
1218
1219       if ((major == 1 && minor >= 3) || major > 1)
1220           display_x11->have_randr13 = TRUE;
1221
1222        gdk_x11_register_standard_event_type (display, display_x11->xrandr_event_base, RRNumberEvents);
1223   }
1224 #endif
1225   
1226   /* initialize the display's screens */ 
1227   display_x11->screens = g_new (GdkScreen *, ScreenCount (display_x11->xdisplay));
1228   for (i = 0; i < ScreenCount (display_x11->xdisplay); i++)
1229     display_x11->screens[i] = _gdk_x11_screen_new (display, i);
1230
1231   /* We need to initialize events after we have the screen
1232    * structures in places
1233    */
1234   for (i = 0; i < ScreenCount (display_x11->xdisplay); i++)
1235     _gdk_screen_x11_events_init (display_x11->screens[i]);
1236
1237   /*set the default screen */
1238   display_x11->default_screen = display_x11->screens[DefaultScreen (display_x11->xdisplay)];
1239
1240   display->device_manager = _gdk_device_manager_new (display);
1241
1242   _gdk_event_init (display);
1243
1244   attr.window_type = GDK_WINDOW_TOPLEVEL;
1245   attr.wclass = GDK_INPUT_OUTPUT;
1246   attr.x = 10;
1247   attr.y = 10;
1248   attr.width = 10;
1249   attr.height = 10;
1250   attr.event_mask = 0;
1251
1252   display_x11->leader_gdk_window = gdk_window_new (GDK_SCREEN_X11 (display_x11->default_screen)->root_window, 
1253                                                    &attr, GDK_WA_X | GDK_WA_Y);
1254   (_gdk_x11_window_get_toplevel (display_x11->leader_gdk_window))->is_leader = TRUE;
1255
1256   display_x11->leader_window = GDK_WINDOW_XID (display_x11->leader_gdk_window);
1257
1258   display_x11->leader_window_title_set = FALSE;
1259
1260 #ifdef HAVE_XFIXES
1261   if (XFixesQueryExtension (display_x11->xdisplay, 
1262                             &display_x11->xfixes_event_base, 
1263                             &ignore))
1264     {
1265       display_x11->have_xfixes = TRUE;
1266
1267       gdk_x11_register_standard_event_type (display,
1268                                             display_x11->xfixes_event_base, 
1269                                             XFixesNumberEvents);
1270     }
1271   else
1272 #endif
1273     display_x11->have_xfixes = FALSE;
1274
1275 #ifdef HAVE_XCOMPOSITE
1276   if (XCompositeQueryExtension (display_x11->xdisplay,
1277                                 &ignore, &ignore))
1278     {
1279       int major, minor;
1280
1281       XCompositeQueryVersion (display_x11->xdisplay, &major, &minor);
1282
1283       /* Prior to Composite version 0.4, composited windows clipped their
1284        * parents, so you had to use IncludeInferiors to draw to the parent
1285        * This isn't useful for our purposes, so require 0.4
1286        */
1287       display_x11->have_xcomposite = major > 0 || (major == 0 && minor >= 4);
1288     }
1289   else
1290 #endif
1291     display_x11->have_xcomposite = FALSE;
1292
1293 #ifdef HAVE_XDAMAGE
1294   if (XDamageQueryExtension (display_x11->xdisplay,
1295                              &display_x11->xdamage_event_base,
1296                              &ignore))
1297     {
1298       display_x11->have_xdamage = TRUE;
1299
1300       gdk_x11_register_standard_event_type (display,
1301                                             display_x11->xdamage_event_base,
1302                                             XDamageNumberEvents);
1303     }
1304   else
1305 #endif
1306     display_x11->have_xdamage = FALSE;
1307
1308   display_x11->have_shapes = FALSE;
1309   display_x11->have_input_shapes = FALSE;
1310
1311   if (XShapeQueryExtension (GDK_DISPLAY_XDISPLAY (display), &display_x11->shape_event_base, &ignore))
1312     {
1313       display_x11->have_shapes = TRUE;
1314 #ifdef ShapeInput
1315       if (XShapeQueryVersion (GDK_DISPLAY_XDISPLAY (display), &maj, &min))
1316         display_x11->have_input_shapes = (maj == 1 && min >= 1);
1317 #endif
1318     }
1319
1320   display_x11->trusted_client = TRUE;
1321   {
1322     Window root, child;
1323     int rootx, rooty, winx, winy;
1324     unsigned int xmask;
1325
1326     gdk_error_trap_push ();
1327     XQueryPointer (display_x11->xdisplay, 
1328                    GDK_SCREEN_X11 (display_x11->default_screen)->xroot_window,
1329                    &root, &child, &rootx, &rooty, &winx, &winy, &xmask);
1330     gdk_flush ();
1331     if (G_UNLIKELY (gdk_error_trap_pop () == BadWindow)) 
1332       {
1333         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));
1334         display_x11->trusted_client = FALSE;
1335       }
1336   }
1337
1338   if (_gdk_synchronize)
1339     XSynchronize (display_x11->xdisplay, True);
1340   
1341   class_hint = XAllocClassHint();
1342   class_hint->res_name = g_get_prgname ();
1343   
1344   class_hint->res_class = (char *)gdk_get_program_class ();
1345
1346   /* XmbSetWMProperties sets the RESOURCE_NAME environment variable
1347    * from argv[0], so we just synthesize an argument array here.
1348    */
1349   argc = 1;
1350   argv[0] = g_get_prgname ();
1351   
1352   XmbSetWMProperties (display_x11->xdisplay,
1353                       display_x11->leader_window,
1354                       NULL, NULL, argv, argc, NULL, NULL,
1355                       class_hint);
1356   XFree (class_hint);
1357
1358   sm_client_id = _gdk_get_sm_client_id ();
1359   if (sm_client_id)
1360     _gdk_windowing_display_set_sm_client_id (display, sm_client_id);
1361
1362   pid = getpid ();
1363   XChangeProperty (display_x11->xdisplay,
1364                    display_x11->leader_window,
1365                    gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_PID"),
1366                    XA_CARDINAL, 32, PropModeReplace, (guchar *) & pid, 1);
1367
1368   /* We don't yet know a valid time. */
1369   display_x11->user_time = 0;
1370   
1371 #ifdef HAVE_XKB
1372   {
1373     gint xkb_major = XkbMajorVersion;
1374     gint xkb_minor = XkbMinorVersion;
1375     if (XkbLibraryVersion (&xkb_major, &xkb_minor))
1376       {
1377         xkb_major = XkbMajorVersion;
1378         xkb_minor = XkbMinorVersion;
1379             
1380         if (XkbQueryExtension (display_x11->xdisplay, 
1381                                NULL, &display_x11->xkb_event_type, NULL,
1382                                &xkb_major, &xkb_minor))
1383           {
1384             Bool detectable_autorepeat_supported;
1385             
1386             display_x11->use_xkb = TRUE;
1387
1388             XkbSelectEvents (display_x11->xdisplay,
1389                              XkbUseCoreKbd,
1390                              XkbNewKeyboardNotifyMask | XkbMapNotifyMask | XkbStateNotifyMask,
1391                              XkbNewKeyboardNotifyMask | XkbMapNotifyMask | XkbStateNotifyMask);
1392
1393             /* keep this in sync with _gdk_keymap_state_changed() */ 
1394             XkbSelectEventDetails (display_x11->xdisplay,
1395                                    XkbUseCoreKbd, XkbStateNotify,
1396                                    XkbAllStateComponentsMask,
1397                                    XkbGroupLockMask|XkbModifierLockMask);
1398
1399             XkbSetDetectableAutoRepeat (display_x11->xdisplay,
1400                                         True,
1401                                         &detectable_autorepeat_supported);
1402
1403             GDK_NOTE (MISC, g_message ("Detectable autorepeat %s.",
1404                                        detectable_autorepeat_supported ? 
1405                                        "supported" : "not supported"));
1406             
1407             display_x11->have_xkb_autorepeat = detectable_autorepeat_supported;
1408           }
1409       }
1410   }
1411 #endif
1412
1413   display_x11->use_sync = FALSE;
1414 #ifdef HAVE_XSYNC
1415   {
1416     int major, minor;
1417     int error_base, event_base;
1418     
1419     if (XSyncQueryExtension (display_x11->xdisplay,
1420                              &event_base, &error_base) &&
1421         XSyncInitialize (display_x11->xdisplay,
1422                          &major, &minor))
1423       display_x11->use_sync = TRUE;
1424   }
1425 #endif
1426
1427   _gdk_input_init (display);
1428   _gdk_dnd_init (display);
1429
1430   for (i = 0; i < ScreenCount (display_x11->xdisplay); i++)
1431     _gdk_x11_screen_setup (display_x11->screens[i]);
1432
1433   g_signal_emit_by_name (display, "opened");
1434   g_signal_emit_by_name (gdk_display_manager_get(),
1435                          "display_opened", display);
1436
1437   return display;
1438 }
1439
1440 #ifdef HAVE_X11R6
1441 /*
1442  * XLib internal connection handling
1443  */
1444 typedef struct _GdkInternalConnection GdkInternalConnection;
1445
1446 struct _GdkInternalConnection
1447 {
1448   gint           fd;
1449   GSource       *source;
1450   Display       *display;
1451 };
1452
1453 static gboolean
1454 process_internal_connection (GIOChannel  *gioc,
1455                              GIOCondition cond,
1456                              gpointer     data)
1457 {
1458   GdkInternalConnection *connection = (GdkInternalConnection *)data;
1459
1460   GDK_THREADS_ENTER ();
1461
1462   XProcessInternalConnection ((Display*)connection->display, connection->fd);
1463
1464   GDK_THREADS_LEAVE ();
1465
1466   return TRUE;
1467 }
1468
1469 gulong
1470 _gdk_windowing_window_get_next_serial (GdkDisplay *display)
1471 {
1472   return NextRequest (GDK_DISPLAY_XDISPLAY (display));
1473 }
1474
1475
1476 static GdkInternalConnection *
1477 gdk_add_connection_handler (Display *display,
1478                             guint    fd)
1479 {
1480   GIOChannel *io_channel;
1481   GdkInternalConnection *connection;
1482
1483   connection = g_new (GdkInternalConnection, 1);
1484
1485   connection->fd = fd;
1486   connection->display = display;
1487   
1488   io_channel = g_io_channel_unix_new (fd);
1489   
1490   connection->source = g_io_create_watch (io_channel, G_IO_IN);
1491   g_source_set_callback (connection->source,
1492                          (GSourceFunc)process_internal_connection, connection, NULL);
1493   g_source_attach (connection->source, NULL);
1494   
1495   g_io_channel_unref (io_channel);
1496   
1497   return connection;
1498 }
1499
1500 static void
1501 gdk_remove_connection_handler (GdkInternalConnection *connection)
1502 {
1503   g_source_destroy (connection->source);
1504   g_free (connection);
1505 }
1506
1507 static void
1508 gdk_internal_connection_watch (Display  *display,
1509                                XPointer  arg,
1510                                gint      fd,
1511                                gboolean  opening,
1512                                XPointer *watch_data)
1513 {
1514   if (opening)
1515     *watch_data = (XPointer)gdk_add_connection_handler (display, fd);
1516   else
1517     gdk_remove_connection_handler ((GdkInternalConnection *)*watch_data);
1518 }
1519 #endif /* HAVE_X11R6 */
1520
1521 /**
1522  * gdk_display_get_name:
1523  * @display: a #GdkDisplay
1524  *
1525  * Gets the name of the display.
1526  * 
1527  * Returns: a string representing the display name. This string is owned
1528  * by GDK and should not be modified or freed.
1529  * 
1530  * Since: 2.2
1531  */
1532 G_CONST_RETURN gchar *
1533 gdk_display_get_name (GdkDisplay *display)
1534 {
1535   g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
1536   
1537   return (gchar *) DisplayString (GDK_DISPLAY_X11 (display)->xdisplay);
1538 }
1539
1540 /**
1541  * gdk_display_get_n_screens:
1542  * @display: a #GdkDisplay
1543  *
1544  * Gets the number of screen managed by the @display.
1545  * 
1546  * Returns: number of screens.
1547  * 
1548  * Since: 2.2
1549  */
1550 gint
1551 gdk_display_get_n_screens (GdkDisplay *display)
1552 {
1553   g_return_val_if_fail (GDK_IS_DISPLAY (display), 0);
1554   
1555   return ScreenCount (GDK_DISPLAY_X11 (display)->xdisplay);
1556 }
1557
1558 /**
1559  * gdk_display_get_screen:
1560  * @display: a #GdkDisplay
1561  * @screen_num: the screen number
1562  *
1563  * Returns a screen object for one of the screens of the display.
1564  *
1565  * Returns: the #GdkScreen object
1566  *
1567  * Since: 2.2
1568  */
1569 GdkScreen *
1570 gdk_display_get_screen (GdkDisplay *display, 
1571                         gint        screen_num)
1572 {
1573   g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
1574   g_return_val_if_fail (ScreenCount (GDK_DISPLAY_X11 (display)->xdisplay) > screen_num, NULL);
1575   
1576   return GDK_DISPLAY_X11 (display)->screens[screen_num];
1577 }
1578
1579 /**
1580  * gdk_display_get_default_screen:
1581  * @display: a #GdkDisplay
1582  *
1583  * Get the default #GdkScreen for @display.
1584  * 
1585  * Returns: the default #GdkScreen object for @display
1586  *
1587  * Since: 2.2
1588  */
1589 GdkScreen *
1590 gdk_display_get_default_screen (GdkDisplay *display)
1591 {
1592   g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
1593   
1594   return GDK_DISPLAY_X11 (display)->default_screen;
1595 }
1596
1597 gboolean
1598 _gdk_x11_display_is_root_window (GdkDisplay *display,
1599                                  Window      xroot_window)
1600 {
1601   GdkDisplayX11 *display_x11;
1602   gint i;
1603   
1604   g_return_val_if_fail (GDK_IS_DISPLAY (display), FALSE);
1605   
1606   display_x11 = GDK_DISPLAY_X11 (display);
1607   
1608   for (i = 0; i < ScreenCount (display_x11->xdisplay); i++)
1609     {
1610       if (GDK_SCREEN_XROOTWIN (display_x11->screens[i]) == xroot_window)
1611         return TRUE;
1612     }
1613   return FALSE;
1614 }
1615
1616 struct XPointerUngrabInfo {
1617   GdkDisplay *display;
1618   guint32 time;
1619 };
1620
1621 static void
1622 device_ungrab_callback (GdkDisplay *display,
1623                         gpointer    data,
1624                         gulong      serial)
1625 {
1626   GdkDevice *device = data;
1627
1628   _gdk_display_device_grab_update (display, device, serial);
1629 }
1630
1631
1632 #define XSERVER_TIME_IS_LATER(time1, time2)                        \
1633   ( (( time1 > time2 ) && ( time1 - time2 < ((guint32)-1)/2 )) ||  \
1634     (( time1 < time2 ) && ( time2 - time1 > ((guint32)-1)/2 ))     \
1635   )
1636
1637 /**
1638  * gdk_device_ungrab:
1639  * @device: a #GdkDevice
1640  * @time_: a timestap (e.g. %GDK_CURRENT_TIME).
1641  *
1642  * Release any grab on @device.
1643  *
1644  * Since: 3.0
1645  */
1646 void
1647 gdk_device_ungrab (GdkDevice  *device,
1648                    guint32     time_)
1649 {
1650   GdkDisplay *display;
1651   Display *xdisplay;
1652   GdkDeviceGrabInfo *grab;
1653   unsigned long serial;
1654
1655   g_return_if_fail (GDK_IS_DEVICE (device));
1656
1657   display = gdk_device_get_display (device);
1658   xdisplay = GDK_DISPLAY_XDISPLAY (display);
1659
1660   serial = NextRequest (xdisplay);
1661
1662   GDK_DEVICE_GET_CLASS (device)->ungrab (device, time_);
1663   XFlush (xdisplay);
1664
1665   grab = _gdk_display_get_last_device_grab (display, device);
1666   if (grab &&
1667       (time_ == GDK_CURRENT_TIME ||
1668        grab->time == GDK_CURRENT_TIME ||
1669        !XSERVER_TIME_IS_LATER (grab->time, time_)))
1670     {
1671       grab->serial_end = serial;
1672       _gdk_x11_roundtrip_async (display,
1673                                 device_ungrab_callback,
1674                                 device);
1675     }
1676 }
1677
1678 /**
1679  * gdk_display_beep:
1680  * @display: a #GdkDisplay
1681  *
1682  * Emits a short beep on @display
1683  *
1684  * Since: 2.2
1685  */
1686 void
1687 gdk_display_beep (GdkDisplay *display)
1688 {
1689   g_return_if_fail (GDK_IS_DISPLAY (display));
1690
1691 #ifdef HAVE_XKB
1692   XkbBell (GDK_DISPLAY_XDISPLAY (display), None, 0, None);
1693 #else
1694   XBell (GDK_DISPLAY_XDISPLAY (display), 0);
1695 #endif
1696 }
1697
1698 /**
1699  * gdk_display_sync:
1700  * @display: a #GdkDisplay
1701  *
1702  * Flushes any requests queued for the windowing system and waits until all
1703  * requests have been handled. This is often used for making sure that the
1704  * display is synchronized with the current state of the program. Calling
1705  * gdk_display_sync() before gdk_error_trap_pop() makes sure that any errors
1706  * generated from earlier requests are handled before the error trap is 
1707  * removed.
1708  *
1709  * This is most useful for X11. On windowing systems where requests are
1710  * handled synchronously, this function will do nothing.
1711  *
1712  * Since: 2.2
1713  */
1714 void
1715 gdk_display_sync (GdkDisplay *display)
1716 {
1717   g_return_if_fail (GDK_IS_DISPLAY (display));
1718   
1719   XSync (GDK_DISPLAY_XDISPLAY (display), False);
1720 }
1721
1722 /**
1723  * gdk_display_flush:
1724  * @display: a #GdkDisplay
1725  *
1726  * Flushes any requests queued for the windowing system; this happens automatically
1727  * when the main loop blocks waiting for new events, but if your application
1728  * is drawing without returning control to the main loop, you may need
1729  * to call this function explicitely. A common case where this function
1730  * needs to be called is when an application is executing drawing commands
1731  * from a thread other than the thread where the main loop is running.
1732  *
1733  * This is most useful for X11. On windowing systems where requests are
1734  * handled synchronously, this function will do nothing.
1735  *
1736  * Since: 2.4
1737  */
1738 void 
1739 gdk_display_flush (GdkDisplay *display)
1740 {
1741   g_return_if_fail (GDK_IS_DISPLAY (display));
1742
1743   if (!display->closed)
1744     XFlush (GDK_DISPLAY_XDISPLAY (display));
1745 }
1746
1747 /**
1748  * gdk_display_get_default_group:
1749  * @display: a #GdkDisplay
1750  * 
1751  * Returns the default group leader window for all toplevel windows
1752  * on @display. This window is implicitly created by GDK. 
1753  * See gdk_window_set_group().
1754  * 
1755  * Return value: The default group leader window for @display
1756  *
1757  * Since: 2.4
1758  **/
1759 GdkWindow *
1760 gdk_display_get_default_group (GdkDisplay *display)
1761 {
1762   g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
1763
1764   return GDK_DISPLAY_X11 (display)->leader_gdk_window;
1765 }
1766
1767 /**
1768  * gdk_x11_display_grab:
1769  * @display: a #GdkDisplay 
1770  * 
1771  * Call XGrabServer() on @display. 
1772  * To ungrab the display again, use gdk_x11_display_ungrab(). 
1773  *
1774  * gdk_x11_display_grab()/gdk_x11_display_ungrab() calls can be nested.
1775  *
1776  * Since: 2.2
1777  **/
1778 void
1779 gdk_x11_display_grab (GdkDisplay *display)
1780 {
1781   GdkDisplayX11 *display_x11;
1782   
1783   g_return_if_fail (GDK_IS_DISPLAY (display));
1784   
1785   display_x11 = GDK_DISPLAY_X11 (display);
1786   
1787   if (display_x11->grab_count == 0)
1788     XGrabServer (display_x11->xdisplay);
1789   display_x11->grab_count++;
1790 }
1791
1792 /**
1793  * gdk_x11_display_ungrab:
1794  * @display: a #GdkDisplay
1795  * 
1796  * Ungrab @display after it has been grabbed with 
1797  * gdk_x11_display_grab(). 
1798  *
1799  * Since: 2.2
1800  **/
1801 void
1802 gdk_x11_display_ungrab (GdkDisplay *display)
1803 {
1804   GdkDisplayX11 *display_x11;
1805   
1806   g_return_if_fail (GDK_IS_DISPLAY (display));
1807   
1808   display_x11 = GDK_DISPLAY_X11 (display);;
1809   g_return_if_fail (display_x11->grab_count > 0);
1810   
1811   display_x11->grab_count--;
1812   if (display_x11->grab_count == 0)
1813     {
1814       XUngrabServer (display_x11->xdisplay);
1815       XFlush (display_x11->xdisplay);
1816     }
1817 }
1818
1819 static void
1820 gdk_display_x11_dispose (GObject *object)
1821 {
1822   GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (object);
1823   gint           i;
1824
1825   g_list_foreach (display_x11->input_devices, (GFunc) g_object_run_dispose, NULL);
1826
1827   for (i = 0; i < ScreenCount (display_x11->xdisplay); i++)
1828     _gdk_screen_close (display_x11->screens[i]);
1829
1830   if (display_x11->event_source)
1831     {
1832       g_source_destroy (display_x11->event_source);
1833       g_source_unref (display_x11->event_source);
1834       display_x11->event_source = NULL;
1835     }
1836
1837   G_OBJECT_CLASS (_gdk_display_x11_parent_class)->dispose (object);
1838 }
1839
1840 static void
1841 gdk_display_x11_finalize (GObject *object)
1842 {
1843   GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (object);
1844   gint           i;
1845
1846   /* Keymap */
1847   if (display_x11->keymap)
1848     g_object_unref (display_x11->keymap);
1849
1850   /* Free motif Dnd */
1851   if (display_x11->motif_target_lists)
1852     {
1853       for (i = 0; i < display_x11->motif_n_target_lists; i++)
1854         g_list_free (display_x11->motif_target_lists[i]);
1855       g_free (display_x11->motif_target_lists);
1856     }
1857
1858   _gdk_x11_cursor_display_finalize (GDK_DISPLAY_OBJECT(display_x11));
1859
1860   /* Atom Hashtable */
1861   g_hash_table_destroy (display_x11->atom_from_virtual);
1862   g_hash_table_destroy (display_x11->atom_to_virtual);
1863
1864   /* Leader Window */
1865   XDestroyWindow (display_x11->xdisplay, display_x11->leader_window);
1866
1867   /* list of filters for client messages */
1868   g_list_foreach (display_x11->client_filters, (GFunc) g_free, NULL);
1869   g_list_free (display_x11->client_filters);
1870
1871   /* List of event window extraction functions */
1872   g_slist_foreach (display_x11->event_types, (GFunc)g_free, NULL);
1873   g_slist_free (display_x11->event_types);
1874
1875   /* input GdkDevice list */
1876   g_list_foreach (display_x11->input_devices, (GFunc) g_object_unref, NULL);
1877   g_list_free (display_x11->input_devices);
1878
1879   /* input GdkWindow list */
1880   g_list_foreach (display_x11->input_windows, (GFunc) g_free, NULL);
1881   g_list_free (display_x11->input_windows);
1882
1883   /* Free all GdkScreens */
1884   for (i = 0; i < ScreenCount (display_x11->xdisplay); i++)
1885     g_object_unref (display_x11->screens[i]);
1886   g_free (display_x11->screens);
1887
1888   g_free (display_x11->startup_notification_id);
1889
1890   /* X ID hashtable */
1891   g_hash_table_destroy (display_x11->xid_ht);
1892
1893   XCloseDisplay (display_x11->xdisplay);
1894
1895   G_OBJECT_CLASS (_gdk_display_x11_parent_class)->finalize (object);
1896 }
1897
1898 /**
1899  * gdk_x11_lookup_xdisplay:
1900  * @xdisplay: a pointer to an X Display
1901  * 
1902  * Find the #GdkDisplay corresponding to @display, if any exists.
1903  * 
1904  * Return value: the #GdkDisplay, if found, otherwise %NULL.
1905  *
1906  * Since: 2.2
1907  **/
1908 GdkDisplay *
1909 gdk_x11_lookup_xdisplay (Display *xdisplay)
1910 {
1911   GSList *tmp_list;
1912
1913   for (tmp_list = _gdk_displays; tmp_list; tmp_list = tmp_list->next)
1914     {
1915       if (GDK_DISPLAY_XDISPLAY (tmp_list->data) == xdisplay)
1916         return tmp_list->data;
1917     }
1918   
1919   return NULL;
1920 }
1921
1922 /**
1923  * _gdk_x11_display_screen_for_xrootwin:
1924  * @display: a #GdkDisplay
1925  * @xrootwin: window ID for one of of the screen's of the display.
1926  * 
1927  * Given the root window ID of one of the screen's of a #GdkDisplay,
1928  * finds the screen.
1929  * 
1930  * Return value: the #GdkScreen corresponding to @xrootwin, or %NULL.
1931  **/
1932 GdkScreen *
1933 _gdk_x11_display_screen_for_xrootwin (GdkDisplay *display,
1934                                       Window      xrootwin)
1935 {
1936   gint i;
1937
1938   for (i = 0; i < ScreenCount (GDK_DISPLAY_X11 (display)->xdisplay); i++)
1939     {
1940       GdkScreen *screen = gdk_display_get_screen (display, i);
1941       if (GDK_SCREEN_XROOTWIN (screen) == xrootwin)
1942         return screen;
1943     }
1944
1945   return NULL;
1946 }
1947
1948 /**
1949  * gdk_x11_display_get_xdisplay:
1950  * @display: a #GdkDisplay
1951  * @returns: an X display.
1952  *
1953  * Returns the X display of a #GdkDisplay.
1954  *
1955  * Since: 2.2
1956  */
1957 Display *
1958 gdk_x11_display_get_xdisplay (GdkDisplay *display)
1959 {
1960   g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
1961   return GDK_DISPLAY_X11 (display)->xdisplay;
1962 }
1963
1964 void
1965 _gdk_windowing_set_default_display (GdkDisplay *display)
1966 {
1967   GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
1968   const gchar *startup_id;
1969   
1970   if (!display)
1971     return;
1972
1973   g_free (display_x11->startup_notification_id);
1974   display_x11->startup_notification_id = NULL;
1975   
1976   startup_id = g_getenv ("DESKTOP_STARTUP_ID");
1977   if (startup_id && *startup_id != '\0')
1978     {
1979       gchar *time_str;
1980
1981       if (!g_utf8_validate (startup_id, -1, NULL))
1982         g_warning ("DESKTOP_STARTUP_ID contains invalid UTF-8");
1983       else
1984         display_x11->startup_notification_id = g_strdup (startup_id);
1985
1986       /* Find the launch time from the startup_id, if it's there.  Newer spec
1987        * states that the startup_id is of the form <unique>_TIME<timestamp>
1988        */
1989       time_str = g_strrstr (startup_id, "_TIME");
1990       if (time_str != NULL)
1991         {
1992           gulong retval;
1993           gchar *end;
1994           errno = 0;
1995
1996           /* Skip past the "_TIME" part */
1997           time_str += 5;
1998
1999           retval = strtoul (time_str, &end, 0);
2000           if (end != time_str && errno == 0)
2001             display_x11->user_time = retval;
2002         }
2003       
2004       /* Clear the environment variable so it won't be inherited by
2005        * child processes and confuse things.  
2006        */
2007       g_unsetenv ("DESKTOP_STARTUP_ID");
2008
2009       /* Set the startup id on the leader window so it
2010        * applies to all windows we create on this display
2011        */
2012       XChangeProperty (display_x11->xdisplay,
2013                        display_x11->leader_window,
2014                        gdk_x11_get_xatom_by_name_for_display (display, "_NET_STARTUP_ID"),
2015                        gdk_x11_get_xatom_by_name_for_display (display, "UTF8_STRING"), 8,
2016                        PropModeReplace,
2017                        (guchar *)startup_id, strlen (startup_id));
2018     }
2019 }
2020
2021 static void
2022 broadcast_xmessage (GdkDisplay *display,
2023                     const char *message_type,
2024                     const char *message_type_begin,
2025                     const char *message)
2026 {
2027   Display *xdisplay = GDK_DISPLAY_XDISPLAY (display);
2028   GdkScreen *screen = gdk_display_get_default_screen (display);
2029   GdkWindow *root_window = gdk_screen_get_root_window (screen);
2030   Window xroot_window = GDK_WINDOW_XID (root_window);
2031   
2032   Atom type_atom;
2033   Atom type_atom_begin;
2034   Window xwindow;
2035
2036   if (!G_LIKELY (GDK_DISPLAY_X11 (display)->trusted_client))
2037     return;
2038
2039   {
2040     XSetWindowAttributes attrs;
2041
2042     attrs.override_redirect = True;
2043     attrs.event_mask = PropertyChangeMask | StructureNotifyMask;
2044
2045     xwindow =
2046       XCreateWindow (xdisplay,
2047                      xroot_window,
2048                      -100, -100, 1, 1,
2049                      0,
2050                      CopyFromParent,
2051                      CopyFromParent,
2052                      (Visual *)CopyFromParent,
2053                      CWOverrideRedirect | CWEventMask,
2054                      &attrs);
2055   }
2056
2057   type_atom = gdk_x11_get_xatom_by_name_for_display (display,
2058                                                      message_type);
2059   type_atom_begin = gdk_x11_get_xatom_by_name_for_display (display,
2060                                                            message_type_begin);
2061   
2062   {
2063     XClientMessageEvent xclient;
2064     const char *src;
2065     const char *src_end;
2066     char *dest;
2067     char *dest_end;
2068     
2069                 memset(&xclient, 0, sizeof (xclient));
2070     xclient.type = ClientMessage;
2071     xclient.message_type = type_atom_begin;
2072     xclient.display =xdisplay;
2073     xclient.window = xwindow;
2074     xclient.format = 8;
2075
2076     src = message;
2077     src_end = message + strlen (message) + 1; /* +1 to include nul byte */
2078     
2079     while (src != src_end)
2080       {
2081         dest = &xclient.data.b[0];
2082         dest_end = dest + 20;        
2083         
2084         while (dest != dest_end &&
2085                src != src_end)
2086           {
2087             *dest = *src;
2088             ++dest;
2089             ++src;
2090           }
2091
2092         while (dest != dest_end)
2093           {
2094             *dest = 0;
2095             ++dest;
2096           }
2097         
2098         XSendEvent (xdisplay,
2099                     xroot_window,
2100                     False,
2101                     PropertyChangeMask,
2102                     (XEvent *)&xclient);
2103
2104         xclient.message_type = type_atom;
2105       }
2106   }
2107
2108   XDestroyWindow (xdisplay, xwindow);
2109   XFlush (xdisplay);
2110 }
2111
2112 /**
2113  * gdk_x11_display_broadcast_startup_message:
2114  * @display: a #GdkDisplay
2115  * @message_type: startup notification message type ("new", "change",
2116  * or "remove")
2117  * @...: a list of key/value pairs (as strings), terminated by a
2118  * %NULL key. (A %NULL value for a key will cause that key to be
2119  * skipped in the output.)
2120  *
2121  * Sends a startup notification message of type @message_type to
2122  * @display. 
2123  *
2124  * This is a convenience function for use by code that implements the
2125  * freedesktop startup notification specification. Applications should
2126  * not normally need to call it directly. See the <ulink
2127  * url="http://standards.freedesktop.org/startup-notification-spec/startup-notification-latest.txt">Startup
2128  * Notification Protocol specification</ulink> for
2129  * definitions of the message types and keys that can be used.
2130  *
2131  * Since: 2.12
2132  **/
2133 void
2134 gdk_x11_display_broadcast_startup_message (GdkDisplay *display,
2135                                            const char *message_type,
2136                                            ...)
2137 {
2138   GString *message;
2139   va_list ap;
2140   const char *key, *value, *p;
2141
2142   message = g_string_new (message_type);
2143   g_string_append_c (message, ':');
2144
2145   va_start (ap, message_type);
2146   while ((key = va_arg (ap, const char *)))
2147     {
2148       value = va_arg (ap, const char *);
2149       if (!value)
2150         continue;
2151
2152       g_string_append_printf (message, " %s=\"", key);
2153       for (p = value; *p; p++)
2154         {
2155           switch (*p)
2156             {
2157             case ' ':
2158             case '"':
2159             case '\\':
2160               g_string_append_c (message, '\\');
2161               break;
2162             }
2163
2164           g_string_append_c (message, *p);
2165         }
2166       g_string_append_c (message, '\"');
2167     }
2168   va_end (ap);
2169
2170   broadcast_xmessage (display,
2171                       "_NET_STARTUP_INFO",
2172                       "_NET_STARTUP_INFO_BEGIN",
2173                       message->str);
2174
2175   g_string_free (message, TRUE);
2176 }
2177
2178 /**
2179  * gdk_notify_startup_complete:
2180  * 
2181  * Indicates to the GUI environment that the application has finished
2182  * loading. If the applications opens windows, this function is
2183  * normally called after opening the application's initial set of
2184  * windows.
2185  * 
2186  * GTK+ will call this function automatically after opening the first
2187  * #GtkWindow unless gtk_window_set_auto_startup_notification() is called 
2188  * to disable that feature.
2189  *
2190  * Since: 2.2
2191  **/
2192 void
2193 gdk_notify_startup_complete (void)
2194 {
2195   GdkDisplay *display;
2196   GdkDisplayX11 *display_x11;
2197
2198   display = gdk_display_get_default ();
2199   if (!display)
2200     return;
2201   
2202   display_x11 = GDK_DISPLAY_X11 (display);
2203
2204   if (display_x11->startup_notification_id == NULL)
2205     return;
2206
2207   gdk_notify_startup_complete_with_id (display_x11->startup_notification_id);
2208 }
2209
2210 /**
2211  * gdk_notify_startup_complete_with_id:
2212  * @startup_id: a startup-notification identifier, for which notification
2213  *              process should be completed
2214  * 
2215  * Indicates to the GUI environment that the application has finished
2216  * loading, using a given identifier.
2217  * 
2218  * GTK+ will call this function automatically for #GtkWindow with custom
2219  * startup-notification identifier unless
2220  * gtk_window_set_auto_startup_notification() is called to disable
2221  * that feature.
2222  *
2223  * Since: 2.12
2224  **/
2225 void
2226 gdk_notify_startup_complete_with_id (const gchar* startup_id)
2227 {
2228   GdkDisplay *display;
2229
2230   display = gdk_display_get_default ();
2231   if (!display)
2232     return;
2233
2234   gdk_x11_display_broadcast_startup_message (display, "remove",
2235                                              "ID", startup_id,
2236                                              NULL);
2237 }
2238
2239 /**
2240  * gdk_display_supports_selection_notification:
2241  * @display: a #GdkDisplay
2242  * 
2243  * Returns whether #GdkEventOwnerChange events will be 
2244  * sent when the owner of a selection changes.
2245  * 
2246  * Return value: whether #GdkEventOwnerChange events will 
2247  *               be sent.
2248  *
2249  * Since: 2.6
2250  **/
2251 gboolean 
2252 gdk_display_supports_selection_notification (GdkDisplay *display)
2253 {
2254   GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
2255
2256   return display_x11->have_xfixes;
2257 }
2258
2259 /**
2260  * gdk_display_request_selection_notification:
2261  * @display: a #GdkDisplay
2262  * @selection: the #GdkAtom naming the selection for which
2263  *             ownership change notification is requested
2264  * 
2265  * Request #GdkEventOwnerChange events for ownership changes
2266  * of the selection named by the given atom.
2267  * 
2268  * Return value: whether #GdkEventOwnerChange events will 
2269  *               be sent.
2270  *
2271  * Since: 2.6
2272  **/
2273 gboolean
2274 gdk_display_request_selection_notification (GdkDisplay *display,
2275                                             GdkAtom     selection)
2276
2277 {
2278 #ifdef HAVE_XFIXES
2279   GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
2280   Atom atom;
2281
2282   if (display_x11->have_xfixes)
2283     {
2284       atom = gdk_x11_atom_to_xatom_for_display (display, 
2285                                                 selection);
2286       XFixesSelectSelectionInput (display_x11->xdisplay, 
2287                                   display_x11->leader_window,
2288                                   atom,
2289                                   XFixesSetSelectionOwnerNotifyMask |
2290                                   XFixesSelectionWindowDestroyNotifyMask |
2291                                   XFixesSelectionClientCloseNotifyMask);
2292       return TRUE;
2293     }
2294   else
2295 #endif
2296     return FALSE;
2297 }
2298
2299 /**
2300  * gdk_display_supports_clipboard_persistence
2301  * @display: a #GdkDisplay
2302  *
2303  * Returns whether the speicifed display supports clipboard
2304  * persistance; i.e. if it's possible to store the clipboard data after an
2305  * application has quit. On X11 this checks if a clipboard daemon is
2306  * running.
2307  *
2308  * Returns: %TRUE if the display supports clipboard persistance.
2309  *
2310  * Since: 2.6
2311  */
2312 gboolean
2313 gdk_display_supports_clipboard_persistence (GdkDisplay *display)
2314 {
2315   Atom clipboard_manager;
2316
2317   /* It might make sense to cache this */
2318   clipboard_manager = gdk_x11_get_xatom_by_name_for_display (display, "CLIPBOARD_MANAGER");
2319   return XGetSelectionOwner (GDK_DISPLAY_X11 (display)->xdisplay, clipboard_manager) != None;
2320 }
2321
2322 /**
2323  * gdk_display_store_clipboard
2324  * @display:          a #GdkDisplay
2325  * @clipboard_window: a #GdkWindow belonging to the clipboard owner
2326  * @time_:            a timestamp
2327  * @targets:          an array of targets that should be saved, or %NULL 
2328  *                    if all available targets should be saved.
2329  * @n_targets:        length of the @targets array
2330  *
2331  * Issues a request to the clipboard manager to store the
2332  * clipboard data. On X11, this is a special program that works
2333  * according to the freedesktop clipboard specification, available at
2334  * <ulink url="http://www.freedesktop.org/Standards/clipboard-manager-spec">
2335  * http://www.freedesktop.org/Standards/clipboard-manager-spec</ulink>.
2336  *
2337  * Since: 2.6
2338  */
2339 void
2340 gdk_display_store_clipboard (GdkDisplay    *display,
2341                              GdkWindow     *clipboard_window,
2342                              guint32        time_,
2343                              const GdkAtom *targets,
2344                              gint           n_targets)
2345 {
2346   GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
2347   Atom clipboard_manager, save_targets;
2348
2349   g_return_if_fail (GDK_WINDOW_IS_X11 (clipboard_window));
2350
2351   clipboard_manager = gdk_x11_get_xatom_by_name_for_display (display, "CLIPBOARD_MANAGER");
2352   save_targets = gdk_x11_get_xatom_by_name_for_display (display, "SAVE_TARGETS");
2353
2354   gdk_error_trap_push ();
2355
2356   if (XGetSelectionOwner (display_x11->xdisplay, clipboard_manager) != None)
2357     {
2358       Atom property_name = None;
2359       Atom *xatoms;
2360       int i;
2361       
2362       if (n_targets > 0)
2363         {
2364           property_name = gdk_x11_atom_to_xatom_for_display (display, _gdk_selection_property);
2365
2366           xatoms = g_new (Atom, n_targets);
2367           for (i = 0; i < n_targets; i++)
2368             xatoms[i] = gdk_x11_atom_to_xatom_for_display (display, targets[i]);
2369
2370           XChangeProperty (display_x11->xdisplay, GDK_WINDOW_XID (clipboard_window),
2371                            property_name, XA_ATOM,
2372                            32, PropModeReplace, (guchar *)xatoms, n_targets);
2373           g_free (xatoms);
2374
2375         }
2376       
2377       XConvertSelection (display_x11->xdisplay,
2378                          clipboard_manager, save_targets, property_name,
2379                          GDK_WINDOW_XID (clipboard_window), time_);
2380       
2381     }
2382   gdk_error_trap_pop ();
2383
2384 }
2385
2386 /**
2387  * gdk_x11_display_get_user_time:
2388  * @display: a #GdkDisplay
2389  *
2390  * Returns the timestamp of the last user interaction on 
2391  * @display. The timestamp is taken from events caused
2392  * by user interaction such as key presses or pointer 
2393  * movements. See gdk_x11_window_set_user_time().
2394  *
2395  * Returns: the timestamp of the last user interaction 
2396  *
2397  * Since: 2.8
2398  */
2399 guint32
2400 gdk_x11_display_get_user_time (GdkDisplay *display)
2401 {
2402   return GDK_DISPLAY_X11 (display)->user_time;
2403 }
2404
2405 /**
2406  * gdk_display_supports_shapes:
2407  * @display: a #GdkDisplay
2408  *
2409  * Returns %TRUE if gdk_window_shape_combine_mask() can
2410  * be used to create shaped windows on @display.
2411  *
2412  * Returns: %TRUE if shaped windows are supported 
2413  *
2414  * Since: 2.10
2415  */
2416 gboolean 
2417 gdk_display_supports_shapes (GdkDisplay *display)
2418 {
2419   return GDK_DISPLAY_X11 (display)->have_shapes;
2420 }
2421
2422 /**
2423  * gdk_display_supports_input_shapes:
2424  * @display: a #GdkDisplay
2425  *
2426  * Returns %TRUE if gdk_window_input_shape_combine_mask() can
2427  * be used to modify the input shape of windows on @display.
2428  *
2429  * Returns: %TRUE if windows with modified input shape are supported 
2430  *
2431  * Since: 2.10
2432  */
2433 gboolean 
2434 gdk_display_supports_input_shapes (GdkDisplay *display)
2435 {
2436   return GDK_DISPLAY_X11 (display)->have_input_shapes;
2437 }
2438
2439
2440 /**
2441  * gdk_x11_display_get_startup_notification_id:
2442  * @display: a #GdkDisplay
2443  *
2444  * Gets the startup notification ID for a display.
2445  * 
2446  * Returns: the startup notification ID for @display
2447  *
2448  * Since: 2.12
2449  */
2450 G_CONST_RETURN gchar *
2451 gdk_x11_display_get_startup_notification_id (GdkDisplay *display)
2452 {
2453   return GDK_DISPLAY_X11 (display)->startup_notification_id;
2454 }
2455
2456 /**
2457  * gdk_display_supports_composite:
2458  * @display: a #GdkDisplay
2459  *
2460  * Returns %TRUE if gdk_window_set_composited() can be used
2461  * to redirect drawing on the window using compositing.
2462  *
2463  * Currently this only works on X11 with XComposite and
2464  * XDamage extensions available.
2465  *
2466  * Returns: %TRUE if windows may be composited.
2467  *
2468  * Since: 2.12
2469  */
2470 gboolean
2471 gdk_display_supports_composite (GdkDisplay *display)
2472 {
2473   GdkDisplayX11 *x11_display = GDK_DISPLAY_X11 (display);
2474
2475   return x11_display->have_xcomposite &&
2476          x11_display->have_xdamage &&
2477          x11_display->have_xfixes;
2478 }
2479
2480 /**
2481  * gdk_display_list_devices:
2482  * @display: a #GdkDisplay
2483  *
2484  * Returns the list of available input devices attached to @display.
2485  * The list is statically allocated and should not be freed.
2486  *
2487  * Return value: (transfer none) (element-type GdkDevice):
2488  *     a list of #GdkDevice
2489  *
2490  * Since: 2.2
2491  *
2492  * Deprecated: 3.0: Use gdk_device_manager_list_devices() instead.
2493  **/
2494 GList *
2495 gdk_display_list_devices (GdkDisplay *display)
2496 {
2497   g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
2498
2499   return GDK_DISPLAY_X11 (display)->input_devices;
2500 }
2501
2502 /**
2503  * gdk_event_send_client_message_for_display:
2504  * @display: the #GdkDisplay for the window where the message is to be sent.
2505  * @event: the #GdkEvent to send, which should be a #GdkEventClient.
2506  * @winid: the window to send the client message to.
2507  *
2508  * On X11, sends an X ClientMessage event to a given window. On
2509  * Windows, sends a message registered with the name
2510  * GDK_WIN32_CLIENT_MESSAGE.
2511  *
2512  * This could be used for communicating between different
2513  * applications, though the amount of data is limited to 20 bytes on
2514  * X11, and to just four bytes on Windows.
2515  *
2516  * Returns: non-zero on success.
2517  *
2518  * Since: 2.2
2519  */
2520 gboolean
2521 gdk_event_send_client_message_for_display (GdkDisplay     *display,
2522                                            GdkEvent       *event,
2523                                            GdkNativeWindow winid)
2524 {
2525   XEvent sev;
2526
2527   g_return_val_if_fail(event != NULL, FALSE);
2528
2529   /* Set up our event to send, with the exception of its target window */
2530   sev.xclient.type = ClientMessage;
2531   sev.xclient.display = GDK_DISPLAY_XDISPLAY (display);
2532   sev.xclient.format = event->client.data_format;
2533   sev.xclient.window = winid;
2534   memcpy(&sev.xclient.data, &event->client.data, sizeof (sev.xclient.data));
2535   sev.xclient.message_type = gdk_x11_atom_to_xatom_for_display (display, event->client.message_type);
2536
2537   return _gdk_send_xevent (display, winid, False, NoEventMask, &sev);
2538 }
2539
2540 /**
2541  * gdk_display_add_client_message_filter:
2542  * @display: a #GdkDisplay for which this message filter applies
2543  * @message_type: the type of ClientMessage events to receive.
2544  *   This will be checked against the @message_type field
2545  *   of the XClientMessage event struct.
2546  * @func: the function to call to process the event.
2547  * @data: user data to pass to @func.
2548  *
2549  * Adds a filter to be called when X ClientMessage events are received.
2550  * See gdk_window_add_filter() if you are interested in filtering other
2551  * types of events.
2552  *
2553  * Since: 2.2
2554  **/
2555 void
2556 gdk_display_add_client_message_filter (GdkDisplay   *display,
2557                                        GdkAtom       message_type,
2558                                        GdkFilterFunc func,
2559                                        gpointer      data)
2560 {
2561   GdkClientFilter *filter;
2562   g_return_if_fail (GDK_IS_DISPLAY (display));
2563   filter = g_new (GdkClientFilter, 1);
2564
2565   filter->type = message_type;
2566   filter->function = func;
2567   filter->data = data;
2568
2569   GDK_DISPLAY_X11(display)->client_filters =
2570     g_list_append (GDK_DISPLAY_X11 (display)->client_filters,
2571                    filter);
2572 }
2573
2574 /**
2575  * gdk_add_client_message_filter:
2576  * @message_type: the type of ClientMessage events to receive. This will be
2577  *     checked against the <structfield>message_type</structfield> field of the
2578  *     XClientMessage event struct.
2579  * @func: the function to call to process the event.
2580  * @data: user data to pass to @func.
2581  *
2582  * Adds a filter to the default display to be called when X ClientMessage events
2583  * are received. See gdk_display_add_client_message_filter().
2584  **/
2585 void
2586 gdk_add_client_message_filter (GdkAtom       message_type,
2587                                GdkFilterFunc func,
2588                                gpointer      data)
2589 {
2590   gdk_display_add_client_message_filter (gdk_display_get_default (),
2591                                          message_type, func, data);
2592 }
2593
2594 /*
2595  *--------------------------------------------------------------
2596  * gdk_flush
2597  *
2598  *   Flushes the Xlib output buffer and then waits
2599  *   until all requests have been received and processed
2600  *   by the X server. The only real use for this function
2601  *   is in dealing with XShm.
2602  *
2603  * Arguments:
2604  *
2605  * Results:
2606  *
2607  * Side effects:
2608  *
2609  *--------------------------------------------------------------
2610  */
2611 void
2612 gdk_flush (void)
2613 {
2614   GSList *tmp_list = _gdk_displays;
2615
2616   while (tmp_list)
2617     {
2618       XSync (GDK_DISPLAY_XDISPLAY (tmp_list->data), False);
2619       tmp_list = tmp_list->next;
2620     }
2621 }
2622
2623 /**
2624  * gdk_x11_register_standard_event_type:
2625  * @display: a #GdkDisplay
2626  * @event_base: first event type code to register
2627  * @n_events: number of event type codes to register
2628  *
2629  * Registers interest in receiving extension events with type codes
2630  * between @event_base and <literal>event_base + n_events - 1</literal>.
2631  * The registered events must have the window field in the same place
2632  * as core X events (this is not the case for e.g. XKB extension events).
2633  *
2634  * If an event type is registered, events of this type will go through
2635  * global and window-specific filters (see gdk_window_add_filter()).
2636  * Unregistered events will only go through global filters.
2637  * GDK may register the events of some X extensions on its own.
2638  *
2639  * This function should only be needed in unusual circumstances, e.g.
2640  * when filtering XInput extension events on the root window.
2641  *
2642  * Since: 2.4
2643  **/
2644 void
2645 gdk_x11_register_standard_event_type (GdkDisplay *display,
2646                                       gint        event_base,
2647                                       gint        n_events)
2648 {
2649   GdkEventTypeX11 *event_type;
2650   GdkDisplayX11 *display_x11;
2651
2652   display_x11 = GDK_DISPLAY_X11 (display);
2653   event_type = g_new (GdkEventTypeX11, 1);
2654
2655   event_type->base = event_base;
2656   event_type->n_events = n_events;
2657
2658   display_x11->event_types = g_slist_prepend (display_x11->event_types, event_type);
2659 }