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