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