]> Pileus Git - ~andy/gtk/blob - gdk/x11/gdkdisplay-x11.c
Assume gravity works. (Anything else we have to take our chances with).
[~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   GdkWindowAttr attr;
142   gint argc;
143   gchar **argv;
144   const char *sm_client_id;
145   
146   XClassHint *class_hint;
147   gulong pid;
148   gint i;
149
150   xdisplay = XOpenDisplay (display_name);
151   if (!xdisplay)
152     return NULL;
153   
154   display = g_object_new (GDK_TYPE_DISPLAY_X11, NULL);
155   display_x11 = GDK_DISPLAY_X11 (display);
156
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
179   attr.window_type = GDK_WINDOW_TOPLEVEL;
180   attr.wclass = GDK_INPUT_OUTPUT;
181   attr.x = 10;
182   attr.y = 10;
183   attr.width = 10;
184   attr.height = 10;
185   attr.event_mask = 0;
186
187   display_x11->leader_gdk_window = gdk_window_new (GDK_SCREEN_X11 (display_x11->default_screen)->root_window, 
188                                                    &attr, GDK_WA_X | GDK_WA_Y);
189   display_x11->leader_window = GDK_WINDOW_XID (display_x11->leader_gdk_window);
190
191   display_x11->leader_window_title_set = FALSE;
192
193   display_x11->have_render = GDK_UNKNOWN;
194
195   if (_gdk_synchronize)
196     XSynchronize (display_x11->xdisplay, True);
197   
198   _gdk_x11_precache_atoms (display, precache_atoms, G_N_ELEMENTS (precache_atoms));
199
200   class_hint = XAllocClassHint();
201   class_hint->res_name = g_get_prgname ();
202   
203   class_hint->res_class = (char *)gdk_get_program_class ();
204   _gdk_get_command_line_args (&argc, &argv);
205   XmbSetWMProperties (display_x11->xdisplay,
206                       display_x11->leader_window,
207                       NULL, NULL, argv, argc, NULL, NULL,
208                       class_hint);
209   XFree (class_hint);
210
211   sm_client_id = _gdk_get_sm_client_id ();
212   if (sm_client_id)
213     _gdk_windowing_display_set_sm_client_id (display, sm_client_id);
214
215   pid = getpid ();
216   XChangeProperty (display_x11->xdisplay,
217                    display_x11->leader_window,
218                    gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_PID"),
219                    XA_CARDINAL, 32, PropModeReplace, (guchar *) & pid, 1);
220   
221 #ifdef HAVE_XKB
222   {
223     gint xkb_major = XkbMajorVersion;
224     gint xkb_minor = XkbMinorVersion;
225     if (XkbLibraryVersion (&xkb_major, &xkb_minor))
226       {
227         xkb_major = XkbMajorVersion;
228         xkb_minor = XkbMinorVersion;
229             
230         if (XkbQueryExtension (display_x11->xdisplay, 
231                                NULL, &display_x11->xkb_event_type, NULL,
232                                &xkb_major, &xkb_minor))
233           {
234             Bool detectable_autorepeat_supported;
235             
236             display_x11->use_xkb = TRUE;
237
238             XkbSelectEvents (display_x11->xdisplay,
239                              XkbUseCoreKbd,
240                              XkbNewKeyboardNotifyMask | XkbMapNotifyMask | XkbStateNotifyMask,
241                              XkbNewKeyboardNotifyMask | XkbMapNotifyMask | XkbStateNotifyMask);
242
243             XkbSetDetectableAutoRepeat (display_x11->xdisplay,
244                                         True,
245                                         &detectable_autorepeat_supported);
246
247             GDK_NOTE (MISC, g_message ("Detectable autorepeat %s.",
248                                        detectable_autorepeat_supported ? 
249                                        "supported" : "not supported"));
250             
251             display_x11->have_xkb_autorepeat = detectable_autorepeat_supported;
252           }
253       }
254   }
255 #endif
256
257   _gdk_windowing_image_init (display);
258   _gdk_events_init (display);
259   _gdk_input_init (display);
260   _gdk_dnd_init (display);
261
262   g_signal_emit_by_name (gdk_display_manager_get(),
263                          "display_opened", display);
264
265   return display;
266 }
267
268 #ifdef HAVE_X11R6
269 /*
270  * XLib internal connection handling
271  */
272 typedef struct _GdkInternalConnection GdkInternalConnection;
273
274 struct _GdkInternalConnection
275 {
276   gint           fd;
277   GSource       *source;
278   Display       *display;
279 };
280
281 static gboolean
282 process_internal_connection (GIOChannel  *gioc,
283                              GIOCondition cond,
284                              gpointer     data)
285 {
286   GdkInternalConnection *connection = (GdkInternalConnection *)data;
287
288   GDK_THREADS_ENTER ();
289
290   XProcessInternalConnection ((Display*)connection->display, connection->fd);
291
292   GDK_THREADS_LEAVE ();
293
294   return TRUE;
295 }
296
297 static GdkInternalConnection *
298 gdk_add_connection_handler (Display *display,
299                             guint    fd)
300 {
301   GIOChannel *io_channel;
302   GdkInternalConnection *connection;
303
304   connection = g_new (GdkInternalConnection, 1);
305
306   connection->fd = fd;
307   connection->display = display;
308   
309   io_channel = g_io_channel_unix_new (fd);
310   
311   connection->source = g_io_create_watch (io_channel, G_IO_IN);
312   g_source_set_callback (connection->source,
313                          (GSourceFunc)process_internal_connection, connection, NULL);
314   g_source_attach (connection->source, NULL);
315   
316   g_io_channel_unref (io_channel);
317   
318   return connection;
319 }
320
321 static void
322 gdk_remove_connection_handler (GdkInternalConnection *connection)
323 {
324   g_source_destroy (connection->source);
325   g_free (connection);
326 }
327
328 static void
329 gdk_internal_connection_watch (Display  *display,
330                                XPointer  arg,
331                                gint      fd,
332                                gboolean  opening,
333                                XPointer *watch_data)
334 {
335   if (opening)
336     *watch_data = (XPointer)gdk_add_connection_handler (display, fd);
337   else
338     gdk_remove_connection_handler ((GdkInternalConnection *)*watch_data);
339 }
340 #endif /* HAVE_X11R6 */
341
342 /**
343  * gdk_display_get_name:
344  * @display: a #GdkDisplay
345  *
346  * Gets the name of the display.
347  * 
348  * Returns: a string representing the display name. This string is owned
349  * by GDK and should not be modified or freed.
350  * 
351  * Since: 2.2
352  */
353 G_CONST_RETURN gchar *
354 gdk_display_get_name (GdkDisplay * display)
355 {
356   g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
357   
358   return (gchar *) DisplayString (GDK_DISPLAY_X11 (display)->xdisplay);
359 }
360
361 /**
362  * gdk_display_get_n_screens:
363  * @display: a #GdkDisplay
364  *
365  * Gets the number of screen managed by the @display.
366  * 
367  * Returns: number of screens.
368  * 
369  * Since: 2.2
370  */
371 gint
372 gdk_display_get_n_screens (GdkDisplay * display)
373 {
374   g_return_val_if_fail (GDK_IS_DISPLAY (display), 0);
375   
376   return ScreenCount (GDK_DISPLAY_X11 (display)->xdisplay);
377 }
378
379 /**
380  * gdk_display_get_screen:
381  * @display: a #GdkDisplay
382  * @screen_num: the screen number
383  *
384  * Returns a screen object for one of the screens of the display.
385  *
386  * Returns: the #GdkScreen object
387  *
388  * Since: 2.2
389  */
390 GdkScreen *
391 gdk_display_get_screen (GdkDisplay * display, gint screen_num)
392 {
393   g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
394   g_return_val_if_fail (ScreenCount (GDK_DISPLAY_X11 (display)->xdisplay) > screen_num, NULL);
395   
396   return GDK_DISPLAY_X11 (display)->screens[screen_num];
397 }
398
399 /**
400  * gdk_display_get_default_screen:
401  * @display: a #GdkDisplay
402  *
403  * Get the default #GdkScreen for @display.
404  * 
405  * Returns: the default #GdkScreen object for @display
406  *
407  * Since: 2.2
408  */
409 GdkScreen *
410 gdk_display_get_default_screen (GdkDisplay * display)
411 {
412   g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
413   
414   return GDK_DISPLAY_X11 (display)->default_screen;
415 }
416
417 gboolean
418 _gdk_x11_display_is_root_window (GdkDisplay *display,
419                                  Window      xroot_window)
420 {
421   GdkDisplayX11 *display_x11;
422   gint i;
423   
424   g_return_val_if_fail (GDK_IS_DISPLAY (display), FALSE);
425   
426   display_x11 = GDK_DISPLAY_X11 (display);
427   
428   for (i = 0; i < ScreenCount (display_x11->xdisplay); i++)
429     {
430       if (GDK_SCREEN_XROOTWIN (display_x11->screens[i]) == xroot_window)
431         return TRUE;
432     }
433   return FALSE;
434 }
435
436 /**
437  * gdk_display_pointer_ungrab:
438  * @display: a #GdkDisplay.
439  * @time_: a timestap (e.g. GDK_CURRENT_TIME).
440  *
441  * Release any pointer grab.
442  *
443  * Since: 2.2
444  */
445 void
446 gdk_display_pointer_ungrab (GdkDisplay *display,
447                             guint32     time)
448 {
449   Display *xdisplay;
450   
451   g_return_if_fail (GDK_IS_DISPLAY (display));
452
453   xdisplay = GDK_DISPLAY_XDISPLAY (display);
454   
455   _gdk_input_ungrab_pointer (display, time);
456   XUngrabPointer (xdisplay, time);
457   XFlush (xdisplay);
458   
459   GDK_DISPLAY_X11 (display)->pointer_xgrab_window = NULL;
460 }
461
462 /**
463  * gdk_display_pointer_is_grabbed:
464  * @display: a #GdkDisplay
465  *
466  * Test if the pointer is grabbed.
467  *
468  * Returns: %TRUE if an active X pointer grab is in effect
469  *
470  * Since: 2.2
471  */
472 gboolean
473 gdk_display_pointer_is_grabbed (GdkDisplay * display)
474 {
475   g_return_val_if_fail (GDK_IS_DISPLAY (display), TRUE);
476   
477   return (GDK_DISPLAY_X11 (display)->pointer_xgrab_window != NULL);
478 }
479
480 /**
481  * gdk_display_keyboard_ungrab:
482  * @display: a #GdkDisplay.
483  * @time_: a timestap (e.g #GDK_CURRENT_TIME).
484  *
485  * Release any keyboard grab
486  *
487  * Since: 2.2
488  */
489 void
490 gdk_display_keyboard_ungrab (GdkDisplay *display,
491                              guint32     time)
492 {
493   Display *xdisplay;
494   
495   g_return_if_fail (GDK_IS_DISPLAY (display));
496
497   xdisplay = GDK_DISPLAY_XDISPLAY (display);
498   
499   XUngrabKeyboard (xdisplay, time);
500   XFlush (xdisplay);
501   
502   GDK_DISPLAY_X11 (display)->keyboard_xgrab_window = NULL;
503 }
504
505 /**
506  * gdk_display_beep:
507  * @display: a #GdkDisplay
508  *
509  * Emits a short beep on @display
510  *
511  * Since: 2.2
512  */
513 void
514 gdk_display_beep (GdkDisplay * display)
515 {
516   g_return_if_fail (GDK_IS_DISPLAY (display));
517   
518   XBell (GDK_DISPLAY_XDISPLAY (display), 0);
519 }
520
521 /**
522  * gdk_display_sync:
523  * @display: a #GdkDisplay
524  *
525  * Flushes any requests queued for the windowing system and waits until all
526  * requests have been handled. This is often used for making sure that the
527  * display is synchronized with the current state of the program. Calling
528  * gdk_display_sync() before gdk_error_trap_pop() makes sure that any errors
529  * generated from earlier requests are handled before the error trap is 
530  * removed.
531  *
532  * This is most useful for X11. On windowing systems where requests are
533  * handled synchronously, this function will do nothing.
534  *
535  * Since: 2.2
536  */
537 void
538 gdk_display_sync (GdkDisplay * display)
539 {
540   g_return_if_fail (GDK_IS_DISPLAY (display));
541   
542   XSync (GDK_DISPLAY_XDISPLAY (display), False);
543 }
544
545 /**
546  * gdk_display_flush:
547  * @display: a #GdkDisplay
548  *
549  * Flushes any requests queued for the windowing system; this happens automatically
550  * when the main loop blocks waiting for new events, but if your application
551  * is drawing without returning control to the main loop, you may need
552  * to call this function explicitely. A common case where this function
553  * needs to be called is when an application is executing drawing commands
554  * from a thread other than the thread where the main loop is running.
555  *
556  * This is most useful for X11. On windowing systems where requests are
557  * handled synchronously, this function will do nothing.
558  *
559  * Since: 2.4
560  */
561 void 
562 gdk_display_flush (GdkDisplay *display)
563 {
564   g_return_if_fail (GDK_IS_DISPLAY (display));
565
566   if (!display->closed)
567     XFlush (GDK_DISPLAY_XDISPLAY (display));
568 }
569
570 /**
571  * gdk_display_get_default_group:
572  * @display: a #GdkDisplay
573  * 
574  * Returns the default group leader window for all toplevel windows
575  * on @display. This window is implicitly created by GDK. 
576  * See gdk_window_set_group().
577  * 
578  * Return value: The default group leader window for @display
579  *
580  * Since: 2.4
581  **/
582 GdkWindow *gdk_display_get_default_group (GdkDisplay *display)
583 {
584   g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
585
586   return GDK_DISPLAY_X11 (display)->leader_gdk_window;
587 }
588
589 /**
590  * gdk_x11_display_grab:
591  * @display: a #GdkDisplay 
592  * 
593  * Call XGrabServer() on @display. 
594  * To ungrab the display again, use gdk_x11_display_ungrab(). 
595  *
596  * gdk_x11_display_grab()/gdk_x11_display_ungrab() calls can be nested.
597  *
598  * Since: 2.2
599  **/
600 void
601 gdk_x11_display_grab (GdkDisplay * display)
602 {
603   GdkDisplayX11 *display_x11;
604   
605   g_return_if_fail (GDK_IS_DISPLAY (display));
606   
607   display_x11 = GDK_DISPLAY_X11 (display);
608   
609   if (display_x11->grab_count == 0)
610     XGrabServer (display_x11->xdisplay);
611   display_x11->grab_count++;
612 }
613
614 /**
615  * gdk_x11_display_ungrab:
616  * @display: a #GdkDisplay
617  * 
618  * Ungrab @display after it has been grabbed with 
619  * gdk_x11_display_grab(). 
620  *
621  * Since: 2.2
622  **/
623 void
624 gdk_x11_display_ungrab (GdkDisplay * display)
625 {
626   GdkDisplayX11 *display_x11;
627   
628   g_return_if_fail (GDK_IS_DISPLAY (display));
629   
630   display_x11 = GDK_DISPLAY_X11 (display);;
631   g_return_if_fail (display_x11->grab_count > 0);
632   
633   display_x11->grab_count--;
634   if (display_x11->grab_count == 0)
635     {
636       XUngrabServer (display_x11->xdisplay);
637       XFlush (display_x11->xdisplay);
638     }
639 }
640
641 static void
642 gdk_display_x11_dispose (GObject *object)
643 {
644   GdkDisplayX11 *display_x11;
645   gint i;
646   
647   display_x11 = GDK_DISPLAY_X11 (object);
648   
649   for (i = 0; i < ScreenCount (display_x11->xdisplay); i++)
650     _gdk_screen_close (display_x11->screens[i]);
651
652   g_source_destroy (display_x11->event_source);
653
654   XCloseDisplay (display_x11->xdisplay);
655   display_x11->xdisplay = NULL;
656
657   G_OBJECT_CLASS (parent_class)->dispose (object);
658 }
659
660 static void
661 gdk_display_x11_finalize (GObject *object)
662 {
663   GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (object);
664   int i;
665   GList *tmp;
666   /* FIXME need to write GdkKeymap finalize fct 
667      g_object_unref (display_x11->keymap);
668    */
669   /* Free motif Dnd */
670   if (display_x11->motif_target_lists)
671     {
672       for (i = 0; i < display_x11->motif_n_target_lists; i++)
673         g_list_free (display_x11->motif_target_lists[i]);
674       g_free (display_x11->motif_target_lists);
675     }
676
677   /* Atom Hashtable */
678   g_hash_table_destroy (display_x11->atom_from_virtual);
679   g_hash_table_destroy (display_x11->atom_to_virtual);
680   /* Leader Window */
681   XDestroyWindow (display_x11->xdisplay, display_x11->leader_window);
682   /* list of filters for client messages */
683   g_list_free (display_x11->client_filters);
684   /* List of event window extraction functions */
685   g_slist_foreach (display_x11->event_types, (GFunc)g_free, NULL);
686   g_slist_free (display_x11->event_types);
687   /* X ID hashtable */
688   g_hash_table_destroy (display_x11->xid_ht);
689   /* input GdkDevice list */
690   /* FIXME need to write finalize fct */
691   for (tmp = display_x11->input_devices; tmp; tmp = tmp->next)
692     g_object_unref (tmp->data);
693   g_list_free (display_x11->input_devices);
694   /* input GdkWindow list */
695   for (tmp = display_x11->input_windows; tmp; tmp = tmp->next)
696     g_object_unref (tmp->data);
697   g_list_free (display_x11->input_windows);
698   /* Free all GdkScreens */
699   for (i = 0; i < ScreenCount (display_x11->xdisplay); i++)
700     g_object_unref (display_x11->screens[i]);
701   g_free (display_x11->screens);
702   g_free (display_x11->startup_notification_id);
703   
704   G_OBJECT_CLASS (parent_class)->finalize (object);
705 }
706
707 /**
708  * gdk_x11_lookup_xdisplay:
709  * @xdisplay: a pointer to an X Display
710  * 
711  * Find the #GdkDisplay corresponding to @display, if any exists.
712  * 
713  * Return value: the #GdkDisplay, if found, otherwise %NULL.
714  *
715  * Since: 2.2
716  **/
717 GdkDisplay *
718 gdk_x11_lookup_xdisplay (Display *xdisplay)
719 {
720   GSList *tmp_list;
721
722   for (tmp_list = _gdk_displays; tmp_list; tmp_list = tmp_list->next)
723     {
724       if (GDK_DISPLAY_XDISPLAY (tmp_list->data) == xdisplay)
725         return tmp_list->data;
726     }
727   
728   return NULL;
729 }
730
731 /**
732  * _gdk_x11_display_screen_for_xrootwin:
733  * @display: a #Display
734  * @xrootwin: window ID for one of of the screen's of the display.
735  * 
736  * Given the root window ID of one of the screen's of a #GdkDisplay,
737  * finds the screen.
738  * 
739  * Return value: the #GdkScreen corresponding to @xrootwin, or %NULL.
740  **/
741 GdkScreen *
742 _gdk_x11_display_screen_for_xrootwin (GdkDisplay *display,
743                                       Window      xrootwin)
744 {
745   gint n_screens, i;
746
747   n_screens = gdk_display_get_n_screens (display);
748   for (i = 0; i < n_screens; i++)
749     {
750       GdkScreen *screen = gdk_display_get_screen (display, i);
751       if (GDK_SCREEN_XROOTWIN (screen) == xrootwin)
752         return screen;
753     }
754
755   return NULL;
756 }
757
758 /**
759  * gdk_x11_display_get_xdisplay:
760  * @display: a #GdkDisplay
761  * @returns: an X display.
762  *
763  * Returns the X display of a #GdkDisplay.
764  *
765  * Since: 2.2
766  */
767 Display *
768 gdk_x11_display_get_xdisplay (GdkDisplay  *display)
769 {
770   return GDK_DISPLAY_X11 (display)->xdisplay;
771 }
772
773 void
774 _gdk_windowing_set_default_display (GdkDisplay *display)
775 {
776   GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
777   const gchar *startup_id;
778   
779   if (display)
780     gdk_display = GDK_DISPLAY_XDISPLAY (display);
781   else
782     gdk_display = NULL;
783
784   g_free (display_x11->startup_notification_id);
785   display_x11->startup_notification_id = NULL;
786   
787   startup_id = g_getenv ("DESKTOP_STARTUP_ID");
788   if (startup_id && *startup_id != '\0')
789     {
790       if (!g_utf8_validate (startup_id, -1, NULL))
791         g_warning ("DESKTOP_STARTUP_ID contains invalid UTF-8");
792       else
793         display_x11->startup_notification_id = g_strdup (startup_id);
794       
795       /* Clear the environment variable so it won't be inherited by
796        * child processes and confuse things.  
797        */
798       g_unsetenv ("DESKTOP_STARTUP_ID");
799
800       /* Set the startup id on the leader window so it
801        * applies to all windows we create on this display
802        */
803       XChangeProperty (display_x11->xdisplay,
804                        display_x11->leader_window,
805                        gdk_x11_get_xatom_by_name_for_display (display, "_NET_STARTUP_ID"),
806                        gdk_x11_get_xatom_by_name_for_display (display, "UTF8_STRING"), 8,
807                        PropModeReplace,
808                        startup_id, strlen (startup_id));
809     }
810 }
811
812 char*
813 escape_for_xmessage (const char *str)
814 {
815   GString *retval;
816   const char *p;
817   
818   retval = g_string_new (NULL);
819
820   p = str;
821   while (*p)
822     {
823       switch (*p)
824         {
825         case ' ':
826         case '"':
827         case '\\':
828           g_string_append_c (retval, '\\');
829           break;
830         }
831
832       g_string_append_c (retval, *p);
833       ++p;
834     }
835
836   return g_string_free (retval, FALSE);
837 }
838
839 static void
840 broadcast_xmessage   (GdkDisplay   *display,
841                       const char   *message_type,
842                       const char   *message_type_begin,
843                       const char   *message)
844 {
845   Display *xdisplay = GDK_DISPLAY_XDISPLAY (display);
846   GdkScreen *screen = gdk_display_get_default_screen (display);
847   GdkWindow *root_window = gdk_screen_get_root_window (screen);
848   Window xroot_window = GDK_WINDOW_XID (root_window);
849   
850   Atom type_atom;
851   Atom type_atom_begin;
852   Window xwindow;
853
854   {
855     XSetWindowAttributes attrs;
856
857     attrs.override_redirect = True;
858     attrs.event_mask = PropertyChangeMask | StructureNotifyMask;
859
860     xwindow =
861       XCreateWindow (xdisplay,
862                      xroot_window,
863                      -100, -100, 1, 1,
864                      0,
865                      CopyFromParent,
866                      CopyFromParent,
867                      CopyFromParent,
868                      CWOverrideRedirect | CWEventMask,
869                      &attrs);
870   }
871
872   type_atom = gdk_x11_get_xatom_by_name_for_display (display,
873                                                      message_type);
874   type_atom_begin = gdk_x11_get_xatom_by_name_for_display (display,
875                                                            message_type_begin);
876   
877   {
878     XEvent xevent;
879     const char *src;
880     const char *src_end;
881     char *dest;
882     char *dest_end;
883     
884     xevent.xclient.type = ClientMessage;
885     xevent.xclient.message_type = type_atom_begin;
886     xevent.xclient.display =xdisplay;
887     xevent.xclient.window = xwindow;
888     xevent.xclient.format = 8;
889
890     src = message;
891     src_end = message + strlen (message) + 1; /* +1 to include nul byte */
892     
893     while (src != src_end)
894       {
895         dest = &xevent.xclient.data.b[0];
896         dest_end = dest + 20;        
897         
898         while (dest != dest_end &&
899                src != src_end)
900           {
901             *dest = *src;
902             ++dest;
903             ++src;
904           }
905
906         while (dest != dest_end)
907           {
908             *dest = 0;
909             ++dest;
910           }
911         
912         XSendEvent (xdisplay,
913                     xroot_window,
914                     False,
915                     PropertyChangeMask,
916                     &xevent);
917
918         xevent.xclient.message_type = type_atom;
919       }
920   }
921
922   XDestroyWindow (xdisplay, xwindow);
923   XFlush (xdisplay);
924 }
925
926 /**
927  * gdk_notify_startup_complete:
928  * 
929  * Indicates to the GUI environment that the application has finished
930  * loading. If the applications opens windows, this function is
931  * normally called after opening the application's initial set of
932  * windows.
933  * 
934  * GTK+ will call this function automatically after opening the first
935  * #GtkWindow unless gtk_window_set_auto_startup_notification() is called 
936  * to disable that feature.
937  *
938  * Since: 2.2
939  **/
940 void
941 gdk_notify_startup_complete (void)
942 {
943   GdkDisplay *display;
944   GdkDisplayX11 *display_x11;
945   gchar *escaped_id;
946   gchar *message;
947
948   display = gdk_display_get_default ();
949   if (!display)
950     return;
951   
952   display_x11 = GDK_DISPLAY_X11 (display);
953
954   if (display_x11->startup_notification_id == NULL)
955     return;
956
957   escaped_id = escape_for_xmessage (display_x11->startup_notification_id);
958   message = g_strdup_printf ("remove: ID=%s", escaped_id);
959   g_free (escaped_id);
960
961   broadcast_xmessage (display,
962                       "_NET_STARTUP_INFO",
963                       "_NET_STARTUP_INFO_BEGIN",
964                       message);
965
966   g_free (message);
967 }