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