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