]> Pileus Git - ~andy/gtk/blob - gdk/x11/gdkdisplay-x11.c
Use asynchronously _gdk_x11_set_input_focus_safe to avoid having to trap
[~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  *
6  * Erwann Chenede <erwann.chenede@sun.com>
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the
20  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  */
23
24 #include <config.h>
25
26 #include <stdlib.h>
27 #include <string.h>
28 #include <unistd.h>
29
30 #include <glib.h>
31 #include "gdkx.h"
32 #include "gdkdisplay.h"
33 #include "gdkdisplay-x11.h"
34 #include "gdkscreen.h"
35 #include "gdkscreen-x11.h"
36 #include "gdkinternals.h"
37 #include "gdkinputprivate.h"
38 #include "xsettings-client.h"
39
40 #include <X11/Xatom.h>
41
42 #ifdef HAVE_XKB
43 #include <X11/XKBlib.h>
44 #endif
45
46 static void                 gdk_display_x11_class_init         (GdkDisplayX11Class *class);
47 static void                 gdk_display_x11_dispose            (GObject            *object);
48 static void                 gdk_display_x11_finalize           (GObject            *object);
49
50 #ifdef HAVE_X11R6
51 static void gdk_internal_connection_watch (Display  *display,
52                                            XPointer  arg,
53                                            gint      fd,
54                                            gboolean  opening,
55                                            XPointer *watch_data);
56 #endif /* HAVE_X11R6 */
57
58 static gpointer parent_class = NULL;
59
60 /* Note that we never *directly* use WM_LOCALE_NAME, WM_PROTOCOLS,
61  * but including them here has the side-effect of getting them
62  * into the internal Xlib cache
63  */
64 static const char *const precache_atoms[] = {
65   "UTF8_STRING",
66   "WM_CLIENT_LEADER",
67   "WM_DELETE_WINDOW",
68   "WM_LOCALE_NAME",
69   "WM_PROTOCOLS",
70   "WM_TAKE_FOCUS",
71   "_NET_WM_DESKTOP",
72   "_NET_WM_ICON",
73   "_NET_WM_ICON_NAME",
74   "_NET_WM_NAME",
75   "_NET_WM_PID",
76   "_NET_WM_PING",
77   "_NET_WM_STATE",
78   "_NET_WM_STATE_STICKY",
79   "_NET_WM_STATE_MAXIMIZED_VERT",
80   "_NET_WM_STATE_MAXIMIZED_HORZ",
81   "_NET_WM_STATE_FULLSCREEN",
82   "_NET_WM_WINDOW_TYPE",
83   "_NET_WM_WINDOW_TYPE_NORMAL",
84 };
85
86 GType
87 _gdk_display_x11_get_type (void)
88 {
89   static GType object_type = 0;
90
91   if (!object_type)
92     {
93       static const GTypeInfo object_info =
94         {
95           sizeof (GdkDisplayX11Class),
96           (GBaseInitFunc) NULL,
97           (GBaseFinalizeFunc) NULL,
98           (GClassInitFunc) gdk_display_x11_class_init,
99           NULL,                 /* class_finalize */
100           NULL,                 /* class_data */
101           sizeof (GdkDisplayX11),
102           0,                    /* n_preallocs */
103           (GInstanceInitFunc) NULL,
104         };
105       
106       object_type = g_type_register_static (GDK_TYPE_DISPLAY,
107                                             "GdkDisplayX11",
108                                             &object_info, 0);
109     }
110   
111   return object_type;
112 }
113
114 static void
115 gdk_display_x11_class_init (GdkDisplayX11Class * class)
116 {
117   GObjectClass *object_class = G_OBJECT_CLASS (class);
118   
119   object_class->dispose = gdk_display_x11_dispose;
120   object_class->finalize = gdk_display_x11_finalize;
121   
122   parent_class = g_type_class_peek_parent (class);
123 }
124
125 /**
126  * gdk_display_open:
127  * @display_name: the name of the display to open
128  * @returns: a #GdkDisplay, or %NULL if the display
129  *  could not be opened.
130  *
131  * Opens a display.
132  *
133  * Since: 2.2
134  */
135 GdkDisplay *
136 gdk_display_open (const gchar *display_name)
137 {
138   Display *xdisplay;
139   GdkDisplay *display;
140   GdkDisplayX11 *display_x11;
141   gint argc;
142   gchar **argv;
143   const char *sm_client_id;
144   
145   XClassHint *class_hint;
146   gulong pid;
147   gint i;
148
149   xdisplay = XOpenDisplay (display_name);
150   if (!xdisplay)
151     return NULL;
152   
153   display = g_object_new (GDK_TYPE_DISPLAY_X11, NULL);
154   display_x11 = GDK_DISPLAY_X11 (display);
155
156   display_x11->use_xft = -1;
157   display_x11->use_xshm = TRUE;
158   display_x11->xdisplay = xdisplay;
159
160 #ifdef HAVE_X11R6  
161   /* Set up handlers for Xlib internal connections */
162   XAddConnectionWatch (xdisplay, gdk_internal_connection_watch, NULL);
163 #endif /* HAVE_X11R6 */
164   
165   /* initialize the display's screens */ 
166   display_x11->screens = g_new (GdkScreen *, ScreenCount (display_x11->xdisplay));
167   for (i = 0; i < ScreenCount (display_x11->xdisplay); i++)
168     display_x11->screens[i] = _gdk_x11_screen_new (display, i);
169
170   /* We need to initialize events after we have the screen
171    * structures in places
172    */
173   for (i = 0; i < ScreenCount (display_x11->xdisplay); i++)
174     _gdk_x11_events_init_screen (display_x11->screens[i]);
175   
176   /*set the default screen */
177   display_x11->default_screen = display_x11->screens[DefaultScreen (display_x11->xdisplay)];
178   display_x11->leader_window = XCreateSimpleWindow (display_x11->xdisplay,
179                                                     GDK_SCREEN_X11 (display_x11->default_screen)->xroot_window,
180                                                     10, 10, 10, 10, 0, 0, 0);
181   display_x11->leader_window_title_set = FALSE;
182
183   display_x11->have_shape = GDK_UNKNOWN;
184   display_x11->gravity_works = GDK_UNKNOWN;
185   display_x11->have_render = GDK_UNKNOWN;
186
187   if (_gdk_synchronize)
188     XSynchronize (display_x11->xdisplay, True);
189   
190   _gdk_x11_precache_atoms (display, precache_atoms, G_N_ELEMENTS (precache_atoms));
191
192   class_hint = XAllocClassHint();
193   class_hint->res_name = g_get_prgname ();
194   
195   class_hint->res_class = (char *)gdk_get_program_class ();
196   _gdk_get_command_line_args (&argc, &argv);
197   XmbSetWMProperties (display_x11->xdisplay,
198                       display_x11->leader_window,
199                       NULL, NULL, argv, argc, NULL, NULL,
200                       class_hint);
201   XFree (class_hint);
202
203   sm_client_id = _gdk_get_sm_client_id ();
204   if (sm_client_id)
205     _gdk_windowing_display_set_sm_client_id (display, sm_client_id);
206
207   pid = getpid ();
208   XChangeProperty (display_x11->xdisplay,
209                    display_x11->leader_window,
210                    gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_PID"),
211                    XA_CARDINAL, 32, PropModeReplace, (guchar *) & pid, 1);
212   
213 #ifdef HAVE_XKB
214   {
215     gint xkb_major = XkbMajorVersion;
216     gint xkb_minor = XkbMinorVersion;
217     if (XkbLibraryVersion (&xkb_major, &xkb_minor))
218       {
219         xkb_major = XkbMajorVersion;
220         xkb_minor = XkbMinorVersion;
221             
222         if (XkbQueryExtension (display_x11->xdisplay, 
223                                NULL, &display_x11->xkb_event_type, NULL,
224                                &xkb_major, &xkb_minor))
225           {
226             Bool detectable_autorepeat_supported;
227             
228             display_x11->use_xkb = TRUE;
229
230             XkbSelectEvents (display_x11->xdisplay,
231                              XkbUseCoreKbd,
232                              XkbNewKeyboardNotifyMask | XkbMapNotifyMask | XkbStateNotifyMask,
233                              XkbNewKeyboardNotifyMask | XkbMapNotifyMask | XkbStateNotifyMask);
234
235             XkbSetDetectableAutoRepeat (display_x11->xdisplay,
236                                         True,
237                                         &detectable_autorepeat_supported);
238
239             GDK_NOTE (MISC, g_message ("Detectable autorepeat %s.",
240                                        detectable_autorepeat_supported ? 
241                                        "supported" : "not supported"));
242             
243             display_x11->have_xkb_autorepeat = detectable_autorepeat_supported;
244           }
245       }
246   }
247 #endif
248
249   _gdk_windowing_image_init (display);
250   _gdk_events_init (display);
251   _gdk_input_init (display);
252   _gdk_dnd_init (display);
253
254   g_signal_emit_by_name (gdk_display_manager_get(),
255                          "display_opened", display);
256
257   return display;
258 }
259
260 #ifdef HAVE_X11R6
261 /*
262  * XLib internal connection handling
263  */
264 typedef struct _GdkInternalConnection GdkInternalConnection;
265
266 struct _GdkInternalConnection
267 {
268   gint           fd;
269   GSource       *source;
270   Display       *display;
271 };
272
273 static gboolean
274 process_internal_connection (GIOChannel  *gioc,
275                              GIOCondition cond,
276                              gpointer     data)
277 {
278   GdkInternalConnection *connection = (GdkInternalConnection *)data;
279
280   GDK_THREADS_ENTER ();
281
282   XProcessInternalConnection ((Display*)connection->display, connection->fd);
283
284   GDK_THREADS_LEAVE ();
285
286   return TRUE;
287 }
288
289 static GdkInternalConnection *
290 gdk_add_connection_handler (Display *display,
291                             guint    fd)
292 {
293   GIOChannel *io_channel;
294   GdkInternalConnection *connection;
295
296   connection = g_new (GdkInternalConnection, 1);
297
298   connection->fd = fd;
299   connection->display = display;
300   
301   io_channel = g_io_channel_unix_new (fd);
302   
303   connection->source = g_io_create_watch (io_channel, G_IO_IN);
304   g_source_set_callback (connection->source,
305                          (GSourceFunc)process_internal_connection, connection, NULL);
306   g_source_attach (connection->source, NULL);
307   
308   g_io_channel_unref (io_channel);
309   
310   return connection;
311 }
312
313 static void
314 gdk_remove_connection_handler (GdkInternalConnection *connection)
315 {
316   g_source_destroy (connection->source);
317   g_free (connection);
318 }
319
320 static void
321 gdk_internal_connection_watch (Display  *display,
322                                XPointer  arg,
323                                gint      fd,
324                                gboolean  opening,
325                                XPointer *watch_data)
326 {
327   if (opening)
328     *watch_data = (XPointer)gdk_add_connection_handler (display, fd);
329   else
330     gdk_remove_connection_handler ((GdkInternalConnection *)*watch_data);
331 }
332 #endif /* HAVE_X11R6 */
333
334 /**
335  * gdk_display_get_name:
336  * @display: a #GdkDisplay
337  *
338  * Gets the name of the display.
339  * 
340  * Returns: a string representing the display name. This string is owned
341  * by GDK and should not be modified or freed.
342  * 
343  * Since: 2.2
344  */
345 G_CONST_RETURN gchar *
346 gdk_display_get_name (GdkDisplay * display)
347 {
348   g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
349   
350   return (gchar *) DisplayString (GDK_DISPLAY_X11 (display)->xdisplay);
351 }
352
353 /**
354  * gdk_display_get_n_screens:
355  * @display: a #GdkDisplay
356  *
357  * Gets the number of screen managed by the @display.
358  * 
359  * Returns: number of screens.
360  * 
361  * Since: 2.2
362  */
363 gint
364 gdk_display_get_n_screens (GdkDisplay * display)
365 {
366   g_return_val_if_fail (GDK_IS_DISPLAY (display), 0);
367   
368   return ScreenCount (GDK_DISPLAY_X11 (display)->xdisplay);
369 }
370
371 /**
372  * gdk_display_get_screen:
373  * @display: a #GdkDisplay
374  * @screen_num: the screen number
375  *
376  * Returns a screen object for one of the screens of the display.
377  *
378  * Returns: the #GdkScreen object
379  *
380  * Since: 2.2
381  */
382 GdkScreen *
383 gdk_display_get_screen (GdkDisplay * display, gint screen_num)
384 {
385   g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
386   g_return_val_if_fail (ScreenCount (GDK_DISPLAY_X11 (display)->xdisplay) > screen_num, NULL);
387   
388   return GDK_DISPLAY_X11 (display)->screens[screen_num];
389 }
390
391 /**
392  * gdk_display_get_default_screen:
393  * @display: a #GdkDisplay
394  *
395  * Get the default #GdkScreen for @display.
396  * 
397  * Returns: the default #GdkScreen object for @display
398  *
399  * Since: 2.2
400  */
401 GdkScreen *
402 gdk_display_get_default_screen (GdkDisplay * display)
403 {
404   g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
405   
406   return GDK_DISPLAY_X11 (display)->default_screen;
407 }
408
409 gboolean
410 _gdk_x11_display_is_root_window (GdkDisplay *display,
411                                  Window      xroot_window)
412 {
413   GdkDisplayX11 *display_x11;
414   gint i;
415   
416   g_return_val_if_fail (GDK_IS_DISPLAY (display), FALSE);
417   
418   display_x11 = GDK_DISPLAY_X11 (display);
419   
420   for (i = 0; i < ScreenCount (display_x11->xdisplay); i++)
421     {
422       if (GDK_SCREEN_XROOTWIN (display_x11->screens[i]) == xroot_window)
423         return TRUE;
424     }
425   return FALSE;
426 }
427
428 /**
429  * gdk_display_pointer_ungrab:
430  * @display: a #GdkDisplay.
431  * @time_: a timestap (e.g. GDK_CURRENT_TIME).
432  *
433  * Release any pointer grab.
434  *
435  * Since: 2.2
436  */
437 void
438 gdk_display_pointer_ungrab (GdkDisplay *display,
439                             guint32     time)
440 {
441   Display *xdisplay;
442   
443   g_return_if_fail (GDK_IS_DISPLAY (display));
444
445   xdisplay = GDK_DISPLAY_XDISPLAY (display);
446   
447   _gdk_input_ungrab_pointer (display, time);
448   XUngrabPointer (xdisplay, time);
449   XFlush (xdisplay);
450   
451   GDK_DISPLAY_X11 (display)->pointer_xgrab_window = NULL;
452 }
453
454 /**
455  * gdk_display_pointer_is_grabbed:
456  * @display: a #GdkDisplay
457  *
458  * Test if the pointer is grabbed.
459  *
460  * Returns: %TRUE if an active X pointer grab is in effect
461  *
462  * Since: 2.2
463  */
464 gboolean
465 gdk_display_pointer_is_grabbed (GdkDisplay * display)
466 {
467   g_return_val_if_fail (GDK_IS_DISPLAY (display), TRUE);
468   
469   return (GDK_DISPLAY_X11 (display)->pointer_xgrab_window != NULL);
470 }
471
472 /**
473  * gdk_display_keyboard_ungrab:
474  * @display: a #GdkDisplay.
475  * @time_: a timestap (e.g #GDK_CURRENT_TIME).
476  *
477  * Release any keyboard grab
478  *
479  * Since: 2.2
480  */
481 void
482 gdk_display_keyboard_ungrab (GdkDisplay *display,
483                              guint32     time)
484 {
485   Display *xdisplay;
486   
487   g_return_if_fail (GDK_IS_DISPLAY (display));
488
489   xdisplay = GDK_DISPLAY_XDISPLAY (display);
490   
491   XUngrabKeyboard (xdisplay, time);
492   XFlush (xdisplay);
493   
494   GDK_DISPLAY_X11 (display)->keyboard_xgrab_window = NULL;
495 }
496
497 /**
498  * gdk_display_beep:
499  * @display: a #GdkDisplay
500  *
501  * Emits a short beep on @display
502  *
503  * Since: 2.2
504  */
505 void
506 gdk_display_beep (GdkDisplay * display)
507 {
508   g_return_if_fail (GDK_IS_DISPLAY (display));
509   
510   XBell (GDK_DISPLAY_XDISPLAY (display), 0);
511 }
512
513 /**
514  * gdk_display_sync:
515  * @display: a #GdkDisplay
516  *
517  * Flushes any requests queued for the windowing system and waits until all
518  * requests have been handled. This is often used for making sure that the
519  * display is synchronized with the current state of the program. Calling
520  * gdk_display_sync() before gdk_error_trap_pop() makes sure that any errors
521  * generated from earlier requests are handled before the error trap is 
522  * removed.
523  *
524  * This is most useful for X11. On windowing systems where requests are
525  * handled synchronously, this function will do nothing.
526  *
527  * Since: 2.2
528  */
529 void
530 gdk_display_sync (GdkDisplay * display)
531 {
532   g_return_if_fail (GDK_IS_DISPLAY (display));
533   
534   XSync (GDK_DISPLAY_XDISPLAY (display), False);
535 }
536
537 /**
538  * gdk_display_flush:
539  * @display: a #GdkDisplay
540  *
541  * Flushes any requests queued for the windowing system; this happens automatically
542  * when the main loop blocks waiting for new events, but if your application
543  * is drawing without returning control to the main loop, you may need
544  * to call this function explicitely. A common case where this function
545  * needs to be called is when an application is executing drawing commands
546  * from a thread other than the thread where the main loop is running.
547  *
548  * This is most useful for X11. On windowing systems where requests are
549  * handled synchronously, this function will do nothing.
550  *
551  * Since: 2.4
552  */
553 void 
554 gdk_display_flush (GdkDisplay *display)
555 {
556   g_return_if_fail (GDK_IS_DISPLAY (display));
557
558   if (!display->closed)
559     XFlush (GDK_DISPLAY_XDISPLAY (display));
560 }
561
562 /**
563  * gdk_x11_display_grab:
564  * @display: a #GdkDisplay 
565  * 
566  * Call XGrabServer() on @display. 
567  * To ungrab the display again, use gdk_x11_display_ungrab(). 
568  *
569  * gdk_x11_display_grab()/gdk_x11_display_ungrab() calls can be nested.
570  *
571  * Since: 2.2
572  **/
573 void
574 gdk_x11_display_grab (GdkDisplay * display)
575 {
576   GdkDisplayX11 *display_x11;
577   
578   g_return_if_fail (GDK_IS_DISPLAY (display));
579   
580   display_x11 = GDK_DISPLAY_X11 (display);
581   
582   if (display_x11->grab_count == 0)
583     XGrabServer (display_x11->xdisplay);
584   display_x11->grab_count++;
585 }
586
587 /**
588  * gdk_x11_display_ungrab:
589  * @display: a #GdkDisplay
590  * 
591  * Ungrab @display after it has been grabbed with 
592  * gdk_x11_display_grab(). 
593  *
594  * Since: 2.2
595  **/
596 void
597 gdk_x11_display_ungrab (GdkDisplay * display)
598 {
599   GdkDisplayX11 *display_x11;
600   
601   g_return_if_fail (GDK_IS_DISPLAY (display));
602   
603   display_x11 = GDK_DISPLAY_X11 (display);;
604   g_return_if_fail (display_x11->grab_count > 0);
605   
606   display_x11->grab_count--;
607   if (display_x11->grab_count == 0)
608     {
609       XUngrabServer (display_x11->xdisplay);
610       XFlush (display_x11->xdisplay);
611     }
612 }
613
614 static void
615 gdk_display_x11_dispose (GObject *object)
616 {
617   GdkDisplayX11 *display_x11;
618   gint i;
619   
620   display_x11 = GDK_DISPLAY_X11 (object);
621   
622   for (i = 0; i < ScreenCount (display_x11->xdisplay); i++)
623     _gdk_screen_close (display_x11->screens[i]);
624
625   g_source_destroy (display_x11->event_source);
626
627   XCloseDisplay (display_x11->xdisplay);
628   display_x11->xdisplay = NULL;
629
630   G_OBJECT_CLASS (parent_class)->dispose (object);
631 }
632
633 static void
634 gdk_display_x11_finalize (GObject *object)
635 {
636   GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (object);
637   int i;
638   GList *tmp;
639   /* FIXME need to write GdkKeymap finalize fct 
640      g_object_unref (display_x11->keymap);
641    */
642   /* Free motif Dnd */
643   if (display_x11->motif_target_lists)
644     {
645       for (i = 0; i < display_x11->motif_n_target_lists; i++)
646         g_list_free (display_x11->motif_target_lists[i]);
647       g_free (display_x11->motif_target_lists);
648     }
649
650   /* Atom Hashtable */
651   g_hash_table_destroy (display_x11->atom_from_virtual);
652   g_hash_table_destroy (display_x11->atom_to_virtual);
653   /* Leader Window */
654   XDestroyWindow (display_x11->xdisplay, display_x11->leader_window);
655   /* list of filters for client messages */
656   g_list_free (display_x11->client_filters);
657   /* X ID hashtable */
658   g_hash_table_destroy (display_x11->xid_ht);
659   /* input GdkDevice list */
660   /* FIXME need to write finalize fct */
661   for (tmp = display_x11->input_devices; tmp; tmp = tmp->next)
662     g_object_unref (tmp->data);
663   g_list_free (display_x11->input_devices);
664   /* input GdkWindow list */
665   for (tmp = display_x11->input_windows; tmp; tmp = tmp->next)
666     g_object_unref (tmp->data);
667   g_list_free (display_x11->input_windows);
668   /* Free all GdkScreens */
669   for (i = 0; i < ScreenCount (display_x11->xdisplay); i++)
670     g_object_unref (display_x11->screens[i]);
671   g_free (display_x11->screens);
672   g_free (display_x11->startup_notification_id);
673   
674   G_OBJECT_CLASS (parent_class)->finalize (object);
675 }
676
677 /**
678  * gdk_x11_lookup_xdisplay:
679  * @xdisplay: a pointer to an X Display
680  * 
681  * Find the #GdkDisplay corresponding to @display, if any exists.
682  * 
683  * Return value: the #GdkDisplay, if found, otherwise %NULL.
684  *
685  * Since: 2.2
686  **/
687 GdkDisplay *
688 gdk_x11_lookup_xdisplay (Display *xdisplay)
689 {
690   GSList *tmp_list;
691
692   for (tmp_list = _gdk_displays; tmp_list; tmp_list = tmp_list->next)
693     {
694       if (GDK_DISPLAY_XDISPLAY (tmp_list->data) == xdisplay)
695         return tmp_list->data;
696     }
697   
698   return NULL;
699 }
700
701 /**
702  * _gdk_x11_display_screen_for_xrootwin:
703  * @display: a #Display
704  * @xrootwin: window ID for one of of the screen's of the display.
705  * 
706  * Given the root window ID of one of the screen's of a #GdkDisplay,
707  * finds the screen.
708  * 
709  * Return value: the #GdkScreen corresponding to @xrootwin, or %NULL.
710  **/
711 GdkScreen *
712 _gdk_x11_display_screen_for_xrootwin (GdkDisplay *display,
713                                       Window      xrootwin)
714 {
715   gint n_screens, i;
716
717   n_screens = gdk_display_get_n_screens (display);
718   for (i = 0; i < n_screens; i++)
719     {
720       GdkScreen *screen = gdk_display_get_screen (display, i);
721       if (GDK_SCREEN_XROOTWIN (screen) == xrootwin)
722         return screen;
723     }
724
725   return NULL;
726 }
727
728 /**
729  * gdk_x11_display_get_xdisplay:
730  * @display: a #GdkDisplay
731  * @returns: an X display.
732  *
733  * Returns the X display of a #GdkDisplay.
734  *
735  * Since: 2.2
736  */
737 Display *
738 gdk_x11_display_get_xdisplay (GdkDisplay  *display)
739 {
740   return GDK_DISPLAY_X11 (display)->xdisplay;
741 }
742
743 void
744 _gdk_windowing_set_default_display (GdkDisplay *display)
745 {
746   GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
747   const gchar *startup_id;
748   
749   if (display)
750     gdk_display = GDK_DISPLAY_XDISPLAY (display);
751   else
752     gdk_display = NULL;
753
754   g_free (display_x11->startup_notification_id);
755   display_x11->startup_notification_id = NULL;
756   
757   startup_id = g_getenv ("DESKTOP_STARTUP_ID");
758   if (startup_id && *startup_id != '\0')
759     {
760       if (!g_utf8_validate (startup_id, -1, NULL))
761         g_warning ("DESKTOP_STARTUP_ID contains invalid UTF-8");
762       else
763         display_x11->startup_notification_id = g_strdup (startup_id);
764       
765       /* Clear the environment variable so it won't be inherited by
766        * child processes and confuse things.  unsetenv isn't portable,
767        * right...
768        */
769       putenv ("DESKTOP_STARTUP_ID=");
770
771       /* Set the startup id on the leader window so it
772        * applies to all windows we create on this display
773        */
774       XChangeProperty (display_x11->xdisplay,
775                        display_x11->leader_window,
776                        gdk_x11_get_xatom_by_name_for_display (display, "_NET_STARTUP_ID"),
777                        gdk_x11_get_xatom_by_name_for_display (display, "UTF8_STRING"), 8,
778                        PropModeReplace,
779                        startup_id, strlen (startup_id));
780     }
781 }
782
783 char*
784 escape_for_xmessage (const char *str)
785 {
786   GString *retval;
787   const char *p;
788   
789   retval = g_string_new (NULL);
790
791   p = str;
792   while (*p)
793     {
794       switch (*p)
795         {
796         case ' ':
797         case '"':
798         case '\\':
799           g_string_append_c (retval, '\\');
800           break;
801         }
802
803       g_string_append_c (retval, *p);
804       ++p;
805     }
806
807   return g_string_free (retval, FALSE);
808 }
809
810 static void
811 broadcast_xmessage   (GdkDisplay   *display,
812                       const char   *message_type,
813                       const char   *message_type_begin,
814                       const char   *message)
815 {
816   Display *xdisplay = GDK_DISPLAY_XDISPLAY (display);
817   GdkScreen *screen = gdk_display_get_default_screen (display);
818   GdkWindow *root_window = gdk_screen_get_root_window (screen);
819   Window xroot_window = GDK_WINDOW_XID (root_window);
820   
821   Atom type_atom;
822   Atom type_atom_begin;
823   Window xwindow;
824
825   {
826     XSetWindowAttributes attrs;
827
828     attrs.override_redirect = True;
829     attrs.event_mask = PropertyChangeMask | StructureNotifyMask;
830
831     xwindow =
832       XCreateWindow (xdisplay,
833                      xroot_window,
834                      -100, -100, 1, 1,
835                      0,
836                      CopyFromParent,
837                      CopyFromParent,
838                      CopyFromParent,
839                      CWOverrideRedirect | CWEventMask,
840                      &attrs);
841   }
842
843   type_atom = gdk_x11_get_xatom_by_name_for_display (display,
844                                                      message_type);
845   type_atom_begin = gdk_x11_get_xatom_by_name_for_display (display,
846                                                            message_type_begin);
847   
848   {
849     XEvent xevent;
850     const char *src;
851     const char *src_end;
852     char *dest;
853     char *dest_end;
854     
855     xevent.xclient.type = ClientMessage;
856     xevent.xclient.message_type = type_atom_begin;
857     xevent.xclient.display =xdisplay;
858     xevent.xclient.window = xwindow;
859     xevent.xclient.format = 8;
860
861     src = message;
862     src_end = message + strlen (message) + 1; /* +1 to include nul byte */
863     
864     while (src != src_end)
865       {
866         dest = &xevent.xclient.data.b[0];
867         dest_end = dest + 20;        
868         
869         while (dest != dest_end &&
870                src != src_end)
871           {
872             *dest = *src;
873             ++dest;
874             ++src;
875           }
876
877         while (dest != dest_end)
878           {
879             *dest = 0;
880             ++dest;
881           }
882         
883         XSendEvent (xdisplay,
884                     xroot_window,
885                     False,
886                     PropertyChangeMask,
887                     &xevent);
888
889         xevent.xclient.message_type = type_atom;
890       }
891   }
892
893   XDestroyWindow (xdisplay, xwindow);
894   XFlush (xdisplay);
895 }
896
897 /**
898  * gdk_notify_startup_complete:
899  * 
900  * Indicates to the GUI environment that the application has finished
901  * loading. If the applications opens windows, this function is
902  * normally called after opening the application's initial set of
903  * windows.
904  * 
905  * GTK+ will call this function automatically after opening the first
906  * #GtkWindow unless gtk_window_set_auto_startup_notification() is called 
907  * to disable that feature.
908  *
909  * Since: 2.2
910  **/
911 void
912 gdk_notify_startup_complete (void)
913 {
914   GdkDisplay *display;
915   GdkDisplayX11 *display_x11;
916   gchar *escaped_id;
917   gchar *message;
918
919   display = gdk_display_get_default ();
920   if (!display)
921     return;
922   
923   display_x11 = GDK_DISPLAY_X11 (display);
924
925   if (display_x11->startup_notification_id == NULL)
926     return;
927
928   escaped_id = escape_for_xmessage (display_x11->startup_notification_id);
929   message = g_strdup_printf ("remove: ID=%s", escaped_id);
930   g_free (escaped_id);
931
932   broadcast_xmessage (display,
933                       "_NET_STARTUP_INFO",
934                       "_NET_STARTUP_INFO_BEGIN",
935                       message);
936
937   g_free (message);
938 }