]> Pileus Git - ~andy/gtk/blob - gdk/x11/gdkdisplay-x11.c
Avoid a segfault. (#309054)
[~andy/gtk] / gdk / x11 / gdkdisplay-x11.c
1 /* GDK - The GIMP Drawing Kit
2  * gdkdisplay-x11.c
3  * 
4  * Copyright 2001 Sun Microsystems Inc.
5  * Copyright (C) 2004 Nokia Corporation
6  *
7  * Erwann Chenede <erwann.chenede@sun.com>
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Library General Public
11  * License as published by the Free Software Foundation; either
12  * version 2 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Library General Public License for more details.
18  *
19  * You should have received a copy of the GNU Library General Public
20  * License along with this library; if not, write to the
21  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22  * Boston, MA 02111-1307, USA.
23  */
24
25 #include <config.h>
26
27 #include <stdlib.h>
28 #include <string.h>
29 #include <errno.h>
30 #include <unistd.h>
31
32 #include <glib.h>
33 #include "gdkx.h"
34 #include "gdkdisplay.h"
35 #include "gdkdisplay-x11.h"
36 #include "gdkscreen.h"
37 #include "gdkscreen-x11.h"
38 #include "gdkinternals.h"
39 #include "gdkinputprivate.h"
40 #include "xsettings-client.h"
41 #include "gdkalias.h"
42
43 #include <X11/Xatom.h>
44
45 #ifdef HAVE_XKB
46 #include <X11/XKBlib.h>
47 #endif
48
49 #ifdef HAVE_XFIXES
50 #include <X11/extensions/Xfixes.h>
51 #endif
52
53 static void                 gdk_display_x11_class_init         (GdkDisplayX11Class *class);
54 static void                 gdk_display_x11_dispose            (GObject            *object);
55 static void                 gdk_display_x11_finalize           (GObject            *object);
56
57 #ifdef HAVE_X11R6
58 static void gdk_internal_connection_watch (Display  *display,
59                                            XPointer  arg,
60                                            gint      fd,
61                                            gboolean  opening,
62                                            XPointer *watch_data);
63 #endif /* HAVE_X11R6 */
64
65 static gpointer parent_class = NULL;
66
67 /* Note that we never *directly* use WM_LOCALE_NAME, WM_PROTOCOLS,
68  * but including them here has the side-effect of getting them
69  * into the internal Xlib cache
70  */
71 static const char *const precache_atoms[] = {
72   "UTF8_STRING",
73   "WM_CLIENT_LEADER",
74   "WM_DELETE_WINDOW",
75   "WM_LOCALE_NAME",
76   "WM_PROTOCOLS",
77   "WM_TAKE_FOCUS",
78   "_NET_WM_DESKTOP",
79   "_NET_WM_ICON",
80   "_NET_WM_ICON_NAME",
81   "_NET_WM_NAME",
82   "_NET_WM_PID",
83   "_NET_WM_PING",
84   "_NET_WM_STATE",
85   "_NET_WM_STATE_STICKY",
86   "_NET_WM_STATE_MAXIMIZED_VERT",
87   "_NET_WM_STATE_MAXIMIZED_HORZ",
88   "_NET_WM_STATE_FULLSCREEN",
89   "_NET_WM_SYNC_REQUEST",
90   "_NET_WM_SYNC_REQUEST_COUNTER",
91   "_NET_WM_WINDOW_TYPE",
92   "_NET_WM_WINDOW_TYPE_NORMAL",
93   "_NET_WM_USER_TIME",
94   "_NET_VIRTUAL_ROOTS"
95 };
96
97 GType
98 _gdk_display_x11_get_type (void)
99 {
100   static GType object_type = 0;
101
102   if (!object_type)
103     {
104       static const GTypeInfo object_info =
105         {
106           sizeof (GdkDisplayX11Class),
107           (GBaseInitFunc) NULL,
108           (GBaseFinalizeFunc) NULL,
109           (GClassInitFunc) gdk_display_x11_class_init,
110           NULL,                 /* class_finalize */
111           NULL,                 /* class_data */
112           sizeof (GdkDisplayX11),
113           0,                    /* n_preallocs */
114           (GInstanceInitFunc) NULL,
115         };
116       
117       object_type = g_type_register_static (GDK_TYPE_DISPLAY,
118                                             "GdkDisplayX11",
119                                             &object_info, 0);
120     }
121   
122   return object_type;
123 }
124
125 static void
126 gdk_display_x11_class_init (GdkDisplayX11Class * class)
127 {
128   GObjectClass *object_class = G_OBJECT_CLASS (class);
129   
130   object_class->dispose = gdk_display_x11_dispose;
131   object_class->finalize = gdk_display_x11_finalize;
132   
133   parent_class = g_type_class_peek_parent (class);
134 }
135
136
137 /**
138  * gdk_display_open:
139  * @display_name: the name of the display to open
140  * @returns: a #GdkDisplay, or %NULL if the display
141  *  could not be opened.
142  *
143  * Opens a display.
144  *
145  * Since: 2.2
146  */
147 GdkDisplay *
148 gdk_display_open (const gchar *display_name)
149 {
150   Display *xdisplay;
151   GdkDisplay *display;
152   GdkDisplayX11 *display_x11;
153   GdkWindowAttr attr;
154   gint argc;
155   gchar *argv[1];
156   const char *sm_client_id;
157   
158   XClassHint *class_hint;
159   gulong pid;
160   gint i;
161 #ifdef HAVE_XFIXES
162   gint ignore;
163 #endif
164
165   xdisplay = XOpenDisplay (display_name);
166   if (!xdisplay)
167     return NULL;
168   
169   display = g_object_new (GDK_TYPE_DISPLAY_X11, NULL);
170   display_x11 = GDK_DISPLAY_X11 (display);
171
172   display_x11->use_xshm = TRUE;
173   display_x11->xdisplay = xdisplay;
174
175 #ifdef HAVE_X11R6  
176   /* Set up handlers for Xlib internal connections */
177   XAddConnectionWatch (xdisplay, gdk_internal_connection_watch, NULL);
178 #endif /* HAVE_X11R6 */
179   
180   /* initialize the display's screens */ 
181   display_x11->screens = g_new (GdkScreen *, ScreenCount (display_x11->xdisplay));
182   for (i = 0; i < ScreenCount (display_x11->xdisplay); i++)
183     display_x11->screens[i] = _gdk_x11_screen_new (display, i);
184
185   /* We need to initialize events after we have the screen
186    * structures in places
187    */
188   for (i = 0; i < ScreenCount (display_x11->xdisplay); i++)
189     _gdk_x11_events_init_screen (display_x11->screens[i]);
190   
191   /*set the default screen */
192   display_x11->default_screen = display_x11->screens[DefaultScreen (display_x11->xdisplay)];
193
194   attr.window_type = GDK_WINDOW_TOPLEVEL;
195   attr.wclass = GDK_INPUT_OUTPUT;
196   attr.x = 10;
197   attr.y = 10;
198   attr.width = 10;
199   attr.height = 10;
200   attr.event_mask = 0;
201
202   display_x11->leader_gdk_window = gdk_window_new (GDK_SCREEN_X11 (display_x11->default_screen)->root_window, 
203                                                    &attr, GDK_WA_X | GDK_WA_Y);
204   (_gdk_x11_window_get_toplevel (display_x11->leader_gdk_window))->is_leader = TRUE;
205
206   display_x11->leader_window = GDK_WINDOW_XID (display_x11->leader_gdk_window);
207
208   display_x11->leader_window_title_set = FALSE;
209
210   display_x11->have_render = GDK_UNKNOWN;
211
212 #ifdef HAVE_XFIXES
213   if (XFixesQueryExtension (display_x11->xdisplay, 
214                             &display_x11->xfixes_event_base, 
215                             &ignore))
216     {
217       display_x11->have_xfixes = TRUE;
218
219       gdk_x11_register_standard_event_type (display,
220                                             display_x11->xfixes_event_base, 
221                                             XFixesNumberEvents);
222     }
223   else
224 #endif
225   display_x11->have_xfixes = FALSE;
226
227   if (_gdk_synchronize)
228     XSynchronize (display_x11->xdisplay, True);
229   
230   _gdk_x11_precache_atoms (display, precache_atoms, G_N_ELEMENTS (precache_atoms));
231
232   class_hint = XAllocClassHint();
233   class_hint->res_name = g_get_prgname ();
234   
235   class_hint->res_class = (char *)gdk_get_program_class ();
236
237   /* XmbSetWMProperties sets the RESOURCE_NAME environment variable
238    * from argv[0], so we just synthesize an argument array here.
239    */
240   argc = 1;
241   argv[0] = g_get_prgname ();
242   
243   XmbSetWMProperties (display_x11->xdisplay,
244                       display_x11->leader_window,
245                       NULL, NULL, argv, argc, NULL, NULL,
246                       class_hint);
247   XFree (class_hint);
248
249   sm_client_id = _gdk_get_sm_client_id ();
250   if (sm_client_id)
251     _gdk_windowing_display_set_sm_client_id (display, sm_client_id);
252
253   pid = getpid ();
254   XChangeProperty (display_x11->xdisplay,
255                    display_x11->leader_window,
256                    gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_PID"),
257                    XA_CARDINAL, 32, PropModeReplace, (guchar *) & pid, 1);
258
259   /* We don't yet know a valid time. */
260   display_x11->user_time = 0;
261   
262 #ifdef HAVE_XKB
263   {
264     gint xkb_major = XkbMajorVersion;
265     gint xkb_minor = XkbMinorVersion;
266     if (XkbLibraryVersion (&xkb_major, &xkb_minor))
267       {
268         xkb_major = XkbMajorVersion;
269         xkb_minor = XkbMinorVersion;
270             
271         if (XkbQueryExtension (display_x11->xdisplay, 
272                                NULL, &display_x11->xkb_event_type, NULL,
273                                &xkb_major, &xkb_minor))
274           {
275             Bool detectable_autorepeat_supported;
276             
277             display_x11->use_xkb = TRUE;
278
279             XkbSelectEvents (display_x11->xdisplay,
280                              XkbUseCoreKbd,
281                              XkbNewKeyboardNotifyMask | XkbMapNotifyMask | XkbStateNotifyMask,
282                              XkbNewKeyboardNotifyMask | XkbMapNotifyMask | XkbStateNotifyMask);
283
284             XkbSetDetectableAutoRepeat (display_x11->xdisplay,
285                                         True,
286                                         &detectable_autorepeat_supported);
287
288             GDK_NOTE (MISC, g_message ("Detectable autorepeat %s.",
289                                        detectable_autorepeat_supported ? 
290                                        "supported" : "not supported"));
291             
292             display_x11->have_xkb_autorepeat = detectable_autorepeat_supported;
293           }
294       }
295   }
296 #endif
297
298   display_x11->use_sync = FALSE;
299 #ifdef HAVE_XSYNC
300   {
301     int major, minor;
302     int error_base, event_base;
303     
304     if (XSyncQueryExtension (display_x11->xdisplay,
305                              &event_base, &error_base) &&
306         XSyncInitialize (display_x11->xdisplay,
307                          &major, &minor))
308       display_x11->use_sync = TRUE;
309   }
310 #endif
311   
312   _gdk_windowing_image_init (display);
313   _gdk_events_init (display);
314   _gdk_input_init (display);
315   _gdk_dnd_init (display);
316
317   g_signal_emit_by_name (gdk_display_manager_get(),
318                          "display_opened", display);
319
320   return display;
321 }
322
323 #ifdef HAVE_X11R6
324 /*
325  * XLib internal connection handling
326  */
327 typedef struct _GdkInternalConnection GdkInternalConnection;
328
329 struct _GdkInternalConnection
330 {
331   gint           fd;
332   GSource       *source;
333   Display       *display;
334 };
335
336 static gboolean
337 process_internal_connection (GIOChannel  *gioc,
338                              GIOCondition cond,
339                              gpointer     data)
340 {
341   GdkInternalConnection *connection = (GdkInternalConnection *)data;
342
343   GDK_THREADS_ENTER ();
344
345   XProcessInternalConnection ((Display*)connection->display, connection->fd);
346
347   GDK_THREADS_LEAVE ();
348
349   return TRUE;
350 }
351
352 static GdkInternalConnection *
353 gdk_add_connection_handler (Display *display,
354                             guint    fd)
355 {
356   GIOChannel *io_channel;
357   GdkInternalConnection *connection;
358
359   connection = g_new (GdkInternalConnection, 1);
360
361   connection->fd = fd;
362   connection->display = display;
363   
364   io_channel = g_io_channel_unix_new (fd);
365   
366   connection->source = g_io_create_watch (io_channel, G_IO_IN);
367   g_source_set_callback (connection->source,
368                          (GSourceFunc)process_internal_connection, connection, NULL);
369   g_source_attach (connection->source, NULL);
370   
371   g_io_channel_unref (io_channel);
372   
373   return connection;
374 }
375
376 static void
377 gdk_remove_connection_handler (GdkInternalConnection *connection)
378 {
379   g_source_destroy (connection->source);
380   g_free (connection);
381 }
382
383 static void
384 gdk_internal_connection_watch (Display  *display,
385                                XPointer  arg,
386                                gint      fd,
387                                gboolean  opening,
388                                XPointer *watch_data)
389 {
390   if (opening)
391     *watch_data = (XPointer)gdk_add_connection_handler (display, fd);
392   else
393     gdk_remove_connection_handler ((GdkInternalConnection *)*watch_data);
394 }
395 #endif /* HAVE_X11R6 */
396
397 /**
398  * gdk_display_get_name:
399  * @display: a #GdkDisplay
400  *
401  * Gets the name of the display.
402  * 
403  * Returns: a string representing the display name. This string is owned
404  * by GDK and should not be modified or freed.
405  * 
406  * Since: 2.2
407  */
408 G_CONST_RETURN gchar *
409 gdk_display_get_name (GdkDisplay * display)
410 {
411   g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
412   
413   return (gchar *) DisplayString (GDK_DISPLAY_X11 (display)->xdisplay);
414 }
415
416 /**
417  * gdk_display_get_n_screens:
418  * @display: a #GdkDisplay
419  *
420  * Gets the number of screen managed by the @display.
421  * 
422  * Returns: number of screens.
423  * 
424  * Since: 2.2
425  */
426 gint
427 gdk_display_get_n_screens (GdkDisplay * display)
428 {
429   g_return_val_if_fail (GDK_IS_DISPLAY (display), 0);
430   
431   return ScreenCount (GDK_DISPLAY_X11 (display)->xdisplay);
432 }
433
434 /**
435  * gdk_display_get_screen:
436  * @display: a #GdkDisplay
437  * @screen_num: the screen number
438  *
439  * Returns a screen object for one of the screens of the display.
440  *
441  * Returns: the #GdkScreen object
442  *
443  * Since: 2.2
444  */
445 GdkScreen *
446 gdk_display_get_screen (GdkDisplay * display, gint screen_num)
447 {
448   g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
449   g_return_val_if_fail (ScreenCount (GDK_DISPLAY_X11 (display)->xdisplay) > screen_num, NULL);
450   
451   return GDK_DISPLAY_X11 (display)->screens[screen_num];
452 }
453
454 /**
455  * gdk_display_get_default_screen:
456  * @display: a #GdkDisplay
457  *
458  * Get the default #GdkScreen for @display.
459  * 
460  * Returns: the default #GdkScreen object for @display
461  *
462  * Since: 2.2
463  */
464 GdkScreen *
465 gdk_display_get_default_screen (GdkDisplay * display)
466 {
467   g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
468   
469   return GDK_DISPLAY_X11 (display)->default_screen;
470 }
471
472 gboolean
473 _gdk_x11_display_is_root_window (GdkDisplay *display,
474                                  Window      xroot_window)
475 {
476   GdkDisplayX11 *display_x11;
477   gint i;
478   
479   g_return_val_if_fail (GDK_IS_DISPLAY (display), FALSE);
480   
481   display_x11 = GDK_DISPLAY_X11 (display);
482   
483   for (i = 0; i < ScreenCount (display_x11->xdisplay); i++)
484     {
485       if (GDK_SCREEN_XROOTWIN (display_x11->screens[i]) == xroot_window)
486         return TRUE;
487     }
488   return FALSE;
489 }
490
491 #define XSERVER_TIME_IS_LATER(time1, time2)                        \
492   ( (( time1 > time2 ) && ( time1 - time2 < ((guint32)-1)/2 )) ||  \
493     (( time1 < time2 ) && ( time2 - time1 > ((guint32)-1)/2 ))     \
494   )
495
496 /**
497  * gdk_display_pointer_ungrab:
498  * @display: a #GdkDisplay.
499  * @time_: a timestap (e.g. GDK_CURRENT_TIME).
500  *
501  * Release any pointer grab.
502  *
503  * Since: 2.2
504  */
505 void
506 gdk_display_pointer_ungrab (GdkDisplay *display,
507                             guint32     time)
508 {
509   Display *xdisplay;
510   GdkDisplayX11 *display_x11;
511
512   g_return_if_fail (GDK_IS_DISPLAY (display));
513
514   display_x11 = GDK_DISPLAY_X11 (display);
515   xdisplay = GDK_DISPLAY_XDISPLAY (display);
516   
517   _gdk_input_ungrab_pointer (display, time);
518   XUngrabPointer (xdisplay, time);
519   XFlush (xdisplay);
520
521   if (time == GDK_CURRENT_TIME || 
522       display_x11->pointer_xgrab_time == GDK_CURRENT_TIME ||
523       !XSERVER_TIME_IS_LATER (display_x11->pointer_xgrab_time, time))
524     display_x11->pointer_xgrab_window = NULL;
525 }
526
527 /**
528  * gdk_display_pointer_is_grabbed:
529  * @display: a #GdkDisplay
530  *
531  * Test if the pointer is grabbed.
532  *
533  * Returns: %TRUE if an active X pointer grab is in effect
534  *
535  * Since: 2.2
536  */
537 gboolean
538 gdk_display_pointer_is_grabbed (GdkDisplay * display)
539 {
540   g_return_val_if_fail (GDK_IS_DISPLAY (display), TRUE);
541   
542   return (GDK_DISPLAY_X11 (display)->pointer_xgrab_window != NULL);
543 }
544
545 /**
546  * gdk_display_keyboard_ungrab:
547  * @display: a #GdkDisplay.
548  * @time_: a timestap (e.g #GDK_CURRENT_TIME).
549  *
550  * Release any keyboard grab
551  *
552  * Since: 2.2
553  */
554 void
555 gdk_display_keyboard_ungrab (GdkDisplay *display,
556                              guint32     time)
557 {
558   Display *xdisplay;
559   GdkDisplayX11 *display_x11;
560   
561   g_return_if_fail (GDK_IS_DISPLAY (display));
562
563   display_x11 = GDK_DISPLAY_X11 (display);
564   xdisplay = GDK_DISPLAY_XDISPLAY (display);
565   
566   XUngrabKeyboard (xdisplay, time);
567   XFlush (xdisplay);
568   
569   if (time == GDK_CURRENT_TIME || 
570       display_x11->keyboard_xgrab_time == GDK_CURRENT_TIME ||
571       !XSERVER_TIME_IS_LATER (display_x11->keyboard_xgrab_time, time))
572     display_x11->keyboard_xgrab_window = NULL;
573 }
574
575 /**
576  * gdk_display_beep:
577  * @display: a #GdkDisplay
578  *
579  * Emits a short beep on @display
580  *
581  * Since: 2.2
582  */
583 void
584 gdk_display_beep (GdkDisplay * display)
585 {
586   g_return_if_fail (GDK_IS_DISPLAY (display));
587   
588   XBell (GDK_DISPLAY_XDISPLAY (display), 0);
589 }
590
591 /**
592  * gdk_display_sync:
593  * @display: a #GdkDisplay
594  *
595  * Flushes any requests queued for the windowing system and waits until all
596  * requests have been handled. This is often used for making sure that the
597  * display is synchronized with the current state of the program. Calling
598  * gdk_display_sync() before gdk_error_trap_pop() makes sure that any errors
599  * generated from earlier requests are handled before the error trap is 
600  * removed.
601  *
602  * This is most useful for X11. On windowing systems where requests are
603  * handled synchronously, this function will do nothing.
604  *
605  * Since: 2.2
606  */
607 void
608 gdk_display_sync (GdkDisplay * display)
609 {
610   g_return_if_fail (GDK_IS_DISPLAY (display));
611   
612   XSync (GDK_DISPLAY_XDISPLAY (display), False);
613 }
614
615 /**
616  * gdk_display_flush:
617  * @display: a #GdkDisplay
618  *
619  * Flushes any requests queued for the windowing system; this happens automatically
620  * when the main loop blocks waiting for new events, but if your application
621  * is drawing without returning control to the main loop, you may need
622  * to call this function explicitely. A common case where this function
623  * needs to be called is when an application is executing drawing commands
624  * from a thread other than the thread where the main loop is running.
625  *
626  * This is most useful for X11. On windowing systems where requests are
627  * handled synchronously, this function will do nothing.
628  *
629  * Since: 2.4
630  */
631 void 
632 gdk_display_flush (GdkDisplay *display)
633 {
634   g_return_if_fail (GDK_IS_DISPLAY (display));
635
636   if (!display->closed)
637     XFlush (GDK_DISPLAY_XDISPLAY (display));
638 }
639
640 /**
641  * gdk_display_get_default_group:
642  * @display: a #GdkDisplay
643  * 
644  * Returns the default group leader window for all toplevel windows
645  * on @display. This window is implicitly created by GDK. 
646  * See gdk_window_set_group().
647  * 
648  * Return value: The default group leader window for @display
649  *
650  * Since: 2.4
651  **/
652 GdkWindow *gdk_display_get_default_group (GdkDisplay *display)
653 {
654   g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
655
656   return GDK_DISPLAY_X11 (display)->leader_gdk_window;
657 }
658
659 /**
660  * gdk_x11_display_grab:
661  * @display: a #GdkDisplay 
662  * 
663  * Call XGrabServer() on @display. 
664  * To ungrab the display again, use gdk_x11_display_ungrab(). 
665  *
666  * gdk_x11_display_grab()/gdk_x11_display_ungrab() calls can be nested.
667  *
668  * Since: 2.2
669  **/
670 void
671 gdk_x11_display_grab (GdkDisplay * display)
672 {
673   GdkDisplayX11 *display_x11;
674   
675   g_return_if_fail (GDK_IS_DISPLAY (display));
676   
677   display_x11 = GDK_DISPLAY_X11 (display);
678   
679   if (display_x11->grab_count == 0)
680     XGrabServer (display_x11->xdisplay);
681   display_x11->grab_count++;
682 }
683
684 /**
685  * gdk_x11_display_ungrab:
686  * @display: a #GdkDisplay
687  * 
688  * Ungrab @display after it has been grabbed with 
689  * gdk_x11_display_grab(). 
690  *
691  * Since: 2.2
692  **/
693 void
694 gdk_x11_display_ungrab (GdkDisplay * display)
695 {
696   GdkDisplayX11 *display_x11;
697   
698   g_return_if_fail (GDK_IS_DISPLAY (display));
699   
700   display_x11 = GDK_DISPLAY_X11 (display);;
701   g_return_if_fail (display_x11->grab_count > 0);
702   
703   display_x11->grab_count--;
704   if (display_x11->grab_count == 0)
705     {
706       XUngrabServer (display_x11->xdisplay);
707       XFlush (display_x11->xdisplay);
708     }
709 }
710
711 static void
712 gdk_display_x11_dispose (GObject *object)
713 {
714   GdkDisplayX11 *display_x11;
715   gint i;
716   
717   display_x11 = GDK_DISPLAY_X11 (object);
718   
719   for (i = 0; i < ScreenCount (display_x11->xdisplay); i++)
720     _gdk_screen_close (display_x11->screens[i]);
721
722   g_source_destroy (display_x11->event_source);
723
724   XCloseDisplay (display_x11->xdisplay);
725   display_x11->xdisplay = NULL;
726
727   G_OBJECT_CLASS (parent_class)->dispose (object);
728 }
729
730 static void
731 gdk_display_x11_finalize (GObject *object)
732 {
733   GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (object);
734   int i;
735   GList *tmp;
736   /* FIXME need to write GdkKeymap finalize fct 
737      g_object_unref (display_x11->keymap);
738    */
739   /* Free motif Dnd */
740   if (display_x11->motif_target_lists)
741     {
742       for (i = 0; i < display_x11->motif_n_target_lists; i++)
743         g_list_free (display_x11->motif_target_lists[i]);
744       g_free (display_x11->motif_target_lists);
745     }
746
747   /* Atom Hashtable */
748   g_hash_table_destroy (display_x11->atom_from_virtual);
749   g_hash_table_destroy (display_x11->atom_to_virtual);
750   /* Leader Window */
751   XDestroyWindow (display_x11->xdisplay, display_x11->leader_window);
752   /* list of filters for client messages */
753   g_list_free (display_x11->client_filters);
754   /* List of event window extraction functions */
755   g_slist_foreach (display_x11->event_types, (GFunc)g_free, NULL);
756   g_slist_free (display_x11->event_types);
757   /* X ID hashtable */
758   g_hash_table_destroy (display_x11->xid_ht);
759   /* input GdkDevice list */
760   /* FIXME need to write finalize fct */
761   for (tmp = display_x11->input_devices; tmp; tmp = tmp->next)
762     g_object_unref (tmp->data);
763   g_list_free (display_x11->input_devices);
764   /* input GdkWindow list */
765   for (tmp = display_x11->input_windows; tmp; tmp = tmp->next)
766     g_object_unref (tmp->data);
767   g_list_free (display_x11->input_windows);
768   /* Free all GdkScreens */
769   for (i = 0; i < ScreenCount (display_x11->xdisplay); i++)
770     g_object_unref (display_x11->screens[i]);
771   g_free (display_x11->screens);
772   g_free (display_x11->startup_notification_id);
773   
774   G_OBJECT_CLASS (parent_class)->finalize (object);
775 }
776
777 /**
778  * gdk_x11_lookup_xdisplay:
779  * @xdisplay: a pointer to an X Display
780  * 
781  * Find the #GdkDisplay corresponding to @display, if any exists.
782  * 
783  * Return value: the #GdkDisplay, if found, otherwise %NULL.
784  *
785  * Since: 2.2
786  **/
787 GdkDisplay *
788 gdk_x11_lookup_xdisplay (Display *xdisplay)
789 {
790   GSList *tmp_list;
791
792   for (tmp_list = _gdk_displays; tmp_list; tmp_list = tmp_list->next)
793     {
794       if (GDK_DISPLAY_XDISPLAY (tmp_list->data) == xdisplay)
795         return tmp_list->data;
796     }
797   
798   return NULL;
799 }
800
801 /**
802  * _gdk_x11_display_screen_for_xrootwin:
803  * @display: a #Display
804  * @xrootwin: window ID for one of of the screen's of the display.
805  * 
806  * Given the root window ID of one of the screen's of a #GdkDisplay,
807  * finds the screen.
808  * 
809  * Return value: the #GdkScreen corresponding to @xrootwin, or %NULL.
810  **/
811 GdkScreen *
812 _gdk_x11_display_screen_for_xrootwin (GdkDisplay *display,
813                                       Window      xrootwin)
814 {
815   gint n_screens, i;
816
817   n_screens = gdk_display_get_n_screens (display);
818   for (i = 0; i < n_screens; i++)
819     {
820       GdkScreen *screen = gdk_display_get_screen (display, i);
821       if (GDK_SCREEN_XROOTWIN (screen) == xrootwin)
822         return screen;
823     }
824
825   return NULL;
826 }
827
828 /**
829  * gdk_x11_display_get_xdisplay:
830  * @display: a #GdkDisplay
831  * @returns: an X display.
832  *
833  * Returns the X display of a #GdkDisplay.
834  *
835  * Since: 2.2
836  */
837 Display *
838 gdk_x11_display_get_xdisplay (GdkDisplay  *display)
839 {
840   return GDK_DISPLAY_X11 (display)->xdisplay;
841 }
842
843 void
844 _gdk_windowing_set_default_display (GdkDisplay *display)
845 {
846   GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
847   const gchar *startup_id;
848   
849   if (display)
850     gdk_display = GDK_DISPLAY_XDISPLAY (display);
851   else
852     gdk_display = NULL;
853
854   g_free (display_x11->startup_notification_id);
855   display_x11->startup_notification_id = NULL;
856   
857   startup_id = g_getenv ("DESKTOP_STARTUP_ID");
858   if (startup_id && *startup_id != '\0')
859     {
860       gchar *time_str;
861
862       if (!g_utf8_validate (startup_id, -1, NULL))
863         g_warning ("DESKTOP_STARTUP_ID contains invalid UTF-8");
864       else
865         display_x11->startup_notification_id = g_strdup (startup_id);
866
867       /* Find the launch time from the startup_id, if it's there.  Newer spec
868        * states that the startup_id is of the form <unique>_TIME<timestamp>
869        */
870       time_str = g_strrstr (startup_id, "_TIME");
871       if (time_str != NULL)
872         {
873           gulong retval;
874           gchar *end;
875           errno = 0;
876
877           /* Skip past the "_TIME" part */
878           time_str += 5;
879
880           retval = strtoul (time_str, &end, 0);
881           if (end != time_str && errno == 0)
882             display_x11->user_time = retval;
883         }
884       
885       /* Clear the environment variable so it won't be inherited by
886        * child processes and confuse things.  
887        */
888       g_unsetenv ("DESKTOP_STARTUP_ID");
889
890       /* Set the startup id on the leader window so it
891        * applies to all windows we create on this display
892        */
893       XChangeProperty (display_x11->xdisplay,
894                        display_x11->leader_window,
895                        gdk_x11_get_xatom_by_name_for_display (display, "_NET_STARTUP_ID"),
896                        gdk_x11_get_xatom_by_name_for_display (display, "UTF8_STRING"), 8,
897                        PropModeReplace,
898                        startup_id, strlen (startup_id));
899     }
900 }
901
902 static char*
903 escape_for_xmessage (const char *str)
904 {
905   GString *retval;
906   const char *p;
907   
908   retval = g_string_new (NULL);
909
910   p = str;
911   while (*p)
912     {
913       switch (*p)
914         {
915         case ' ':
916         case '"':
917         case '\\':
918           g_string_append_c (retval, '\\');
919           break;
920         }
921
922       g_string_append_c (retval, *p);
923       ++p;
924     }
925
926   return g_string_free (retval, FALSE);
927 }
928
929 static void
930 broadcast_xmessage   (GdkDisplay   *display,
931                       const char   *message_type,
932                       const char   *message_type_begin,
933                       const char   *message)
934 {
935   Display *xdisplay = GDK_DISPLAY_XDISPLAY (display);
936   GdkScreen *screen = gdk_display_get_default_screen (display);
937   GdkWindow *root_window = gdk_screen_get_root_window (screen);
938   Window xroot_window = GDK_WINDOW_XID (root_window);
939   
940   Atom type_atom;
941   Atom type_atom_begin;
942   Window xwindow;
943
944   {
945     XSetWindowAttributes attrs;
946
947     attrs.override_redirect = True;
948     attrs.event_mask = PropertyChangeMask | StructureNotifyMask;
949
950     xwindow =
951       XCreateWindow (xdisplay,
952                      xroot_window,
953                      -100, -100, 1, 1,
954                      0,
955                      CopyFromParent,
956                      CopyFromParent,
957                      (Visual *)CopyFromParent,
958                      CWOverrideRedirect | CWEventMask,
959                      &attrs);
960   }
961
962   type_atom = gdk_x11_get_xatom_by_name_for_display (display,
963                                                      message_type);
964   type_atom_begin = gdk_x11_get_xatom_by_name_for_display (display,
965                                                            message_type_begin);
966   
967   {
968     XEvent xevent;
969     const char *src;
970     const char *src_end;
971     char *dest;
972     char *dest_end;
973     
974     xevent.xclient.type = ClientMessage;
975     xevent.xclient.message_type = type_atom_begin;
976     xevent.xclient.display =xdisplay;
977     xevent.xclient.window = xwindow;
978     xevent.xclient.format = 8;
979
980     src = message;
981     src_end = message + strlen (message) + 1; /* +1 to include nul byte */
982     
983     while (src != src_end)
984       {
985         dest = &xevent.xclient.data.b[0];
986         dest_end = dest + 20;        
987         
988         while (dest != dest_end &&
989                src != src_end)
990           {
991             *dest = *src;
992             ++dest;
993             ++src;
994           }
995
996         while (dest != dest_end)
997           {
998             *dest = 0;
999             ++dest;
1000           }
1001         
1002         XSendEvent (xdisplay,
1003                     xroot_window,
1004                     False,
1005                     PropertyChangeMask,
1006                     &xevent);
1007
1008         xevent.xclient.message_type = type_atom;
1009       }
1010   }
1011
1012   XDestroyWindow (xdisplay, xwindow);
1013   XFlush (xdisplay);
1014 }
1015
1016 /**
1017  * gdk_notify_startup_complete:
1018  * 
1019  * Indicates to the GUI environment that the application has finished
1020  * loading. If the applications opens windows, this function is
1021  * normally called after opening the application's initial set of
1022  * windows.
1023  * 
1024  * GTK+ will call this function automatically after opening the first
1025  * #GtkWindow unless gtk_window_set_auto_startup_notification() is called 
1026  * to disable that feature.
1027  *
1028  * Since: 2.2
1029  **/
1030 void
1031 gdk_notify_startup_complete (void)
1032 {
1033   GdkDisplay *display;
1034   GdkDisplayX11 *display_x11;
1035   gchar *escaped_id;
1036   gchar *message;
1037
1038   display = gdk_display_get_default ();
1039   if (!display)
1040     return;
1041   
1042   display_x11 = GDK_DISPLAY_X11 (display);
1043
1044   if (display_x11->startup_notification_id == NULL)
1045     return;
1046
1047   escaped_id = escape_for_xmessage (display_x11->startup_notification_id);
1048   message = g_strdup_printf ("remove: ID=%s", escaped_id);
1049   g_free (escaped_id);
1050
1051   broadcast_xmessage (display,
1052                       "_NET_STARTUP_INFO",
1053                       "_NET_STARTUP_INFO_BEGIN",
1054                       message);
1055
1056   g_free (message);
1057 }
1058
1059
1060 /**
1061  * gdk_display_supports_selection_notification:
1062  * @display: a #GdkDisplay
1063  * 
1064  * Returns whether #GdkEventOwnerChange events will be 
1065  * sent when the owner of a selection changes.
1066  * 
1067  * Return value: whether #GdkEventOwnerChange events will 
1068  *               be sent.
1069  *
1070  * Since: 2.6
1071  **/
1072 gboolean 
1073 gdk_display_supports_selection_notification (GdkDisplay *display)
1074 {
1075   GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
1076
1077   return display_x11->have_xfixes;
1078 }
1079
1080 /**
1081  * gdk_display_request_selection_notification:
1082  * @display: a #GdkDisplay
1083  * @selection: the #GdkAtom naming the selection for which
1084  *             ownership change notification is requested
1085  * 
1086  * Request #GdkEventOwnerChange events for ownership changes
1087  * of the selection named by the given atom.
1088  * 
1089  * Return value: whether #GdkEventOwnerChange events will 
1090  *               be sent.
1091  *
1092  * Since: 2.6
1093  **/
1094 gboolean gdk_display_request_selection_notification  (GdkDisplay *display,
1095                                                       GdkAtom     selection)
1096
1097 {
1098 #ifdef HAVE_XFIXES
1099   GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
1100   Atom atom;
1101
1102   if (display_x11->have_xfixes)
1103     {
1104       atom = gdk_x11_atom_to_xatom_for_display (display, 
1105                                                 selection);
1106       XFixesSelectSelectionInput (display_x11->xdisplay, 
1107                                   display_x11->leader_window,
1108                                   atom,
1109                                   XFixesSetSelectionOwnerNotifyMask |
1110                                   XFixesSelectionWindowDestroyNotifyMask |
1111                                   XFixesSelectionClientCloseNotifyMask);
1112       return TRUE;
1113     }
1114   else
1115 #endif
1116     return FALSE;
1117 }
1118
1119 /**
1120  * gdk_display_supports_clipboard_persistence
1121  * @display: a #GdkDisplay
1122  *
1123  * Returns whether the speicifed display supports clipboard
1124  * persistance; i.e. if it's possible to store the clipboard data after an
1125  * application has quit. On X11 this checks if a clipboard daemon is
1126  * running.
1127  *
1128  * Returns: %TRUE if the display supports clipboard persistance.
1129  *
1130  * Since: 2.6
1131  */
1132 gboolean
1133 gdk_display_supports_clipboard_persistence (GdkDisplay *display)
1134 {
1135   /* It might make sense to cache this */
1136   return XGetSelectionOwner (GDK_DISPLAY_X11 (display)->xdisplay,
1137                              gdk_x11_get_xatom_by_name_for_display (display, "CLIPBOARD_MANAGER")) != None;
1138 }
1139
1140 /**
1141  * gdk_display_store_clipboard
1142  * @display:          a #GdkDisplay
1143  * @clipboard_window: a #GdkWindow belonging to the clipboard owner
1144  * @time_:            a timestamp
1145  * @targets:          an array of targets that should be saved, or %NULL 
1146  *                    if all available targets should be saved.
1147  * @n_targets:        length of the @targets array
1148  *
1149  * Issues a request to the clipboard manager to store the
1150  * clipboard data. On X11, this is a special program that works
1151  * according to the freedesktop clipboard specification, available at
1152  * <ulink url="http://www.freedesktop.org/Standards/clipboard-manager-spec">
1153  * http://www.freedesktop.org/Standards/clipboard-manager-spec</ulink>.
1154  *
1155  * Since: 2.6
1156  */
1157 void
1158 gdk_display_store_clipboard (GdkDisplay *display,
1159                              GdkWindow  *clipboard_window,
1160                              guint32     time_,
1161                              GdkAtom    *targets,
1162                              gint        n_targets)
1163 {
1164   GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
1165   Atom clipboard_manager, save_targets;
1166   
1167   clipboard_manager = gdk_x11_get_xatom_by_name_for_display (display, "CLIPBOARD_MANAGER");
1168   save_targets = gdk_x11_get_xatom_by_name_for_display (display, "SAVE_TARGETS");
1169
1170   gdk_error_trap_push ();
1171
1172   if (XGetSelectionOwner (display_x11->xdisplay, clipboard_manager) != None)
1173     {
1174       Atom property_name = None;
1175       Atom *xatoms;
1176       int i;
1177       
1178       if (n_targets > 0)
1179         {
1180           property_name = gdk_x11_atom_to_xatom_for_display (display, _gdk_selection_property);
1181
1182           xatoms = g_new (Atom, n_targets);
1183           for (i = 0; i < n_targets; i++)
1184             xatoms[i] = gdk_x11_atom_to_xatom_for_display (display, targets[i]);
1185
1186           XChangeProperty (display_x11->xdisplay, GDK_WINDOW_XID (clipboard_window),
1187                            property_name, XA_ATOM,
1188                            32, PropModeReplace, (guchar *)xatoms, n_targets);
1189           g_free (xatoms);
1190
1191         }
1192       
1193       XConvertSelection (display_x11->xdisplay,
1194                          clipboard_manager, save_targets, property_name,
1195                          GDK_WINDOW_XID (clipboard_window), time_);
1196       
1197     }
1198   gdk_error_trap_pop ();
1199
1200 }
1201
1202 /**
1203  * gdk_x11_display_get_user_time:
1204  * @display: a #GdkDisplay
1205  *
1206  * Returns the timestamp of the last user interaction on 
1207  * @display. The timestamp is taken from events caused
1208  * by user interaction such as key presses or pointer 
1209  * movements. See gdk_x11_window_set_user_time().
1210  *
1211  * Returns: the timestamp of the last user interaction 
1212  *
1213  * Since: 2.8
1214  */
1215 guint32
1216 gdk_x11_display_get_user_time (GdkDisplay *display)
1217 {
1218   return GDK_DISPLAY_X11 (display)->user_time;
1219 }
1220
1221 #define __GDK_DISPLAY_X11_C__
1222 #include "gdkaliasdef.c"