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