]> Pileus Git - ~andy/gtk/blob - gdk/x11/gdkdisplay-x11.c
Precache the _NET_VIRTUAL_ROOTS atom.
[~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 <unistd.h>
30
31 #include <glib.h>
32 #include "gdkalias.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
42 #include <X11/Xatom.h>
43
44 #ifdef HAVE_XKB
45 #include <X11/XKBlib.h>
46 #endif
47
48 #ifdef HAVE_XFIXES
49 #include <X11/extensions/Xfixes.h>
50 #endif
51
52 static void                 gdk_display_x11_class_init         (GdkDisplayX11Class *class);
53 static void                 gdk_display_x11_dispose            (GObject            *object);
54 static void                 gdk_display_x11_finalize           (GObject            *object);
55
56 #ifdef HAVE_X11R6
57 static void gdk_internal_connection_watch (Display  *display,
58                                            XPointer  arg,
59                                            gint      fd,
60                                            gboolean  opening,
61                                            XPointer *watch_data);
62 #endif /* HAVE_X11R6 */
63
64 static gpointer parent_class = NULL;
65
66 /* Note that we never *directly* use WM_LOCALE_NAME, WM_PROTOCOLS,
67  * but including them here has the side-effect of getting them
68  * into the internal Xlib cache
69  */
70 static const char *const precache_atoms[] = {
71   "UTF8_STRING",
72   "WM_CLIENT_LEADER",
73   "WM_DELETE_WINDOW",
74   "WM_LOCALE_NAME",
75   "WM_PROTOCOLS",
76   "WM_TAKE_FOCUS",
77   "_NET_WM_DESKTOP",
78   "_NET_WM_ICON",
79   "_NET_WM_ICON_NAME",
80   "_NET_WM_NAME",
81   "_NET_WM_PID",
82   "_NET_WM_PING",
83   "_NET_WM_STATE",
84   "_NET_WM_STATE_STICKY",
85   "_NET_WM_STATE_MAXIMIZED_VERT",
86   "_NET_WM_STATE_MAXIMIZED_HORZ",
87   "_NET_WM_STATE_FULLSCREEN",
88   "_NET_WM_SYNC_REQUEST",
89   "_NET_WM_SYNC_REQUEST_COUNTER",
90   "_NET_WM_WINDOW_TYPE",
91   "_NET_WM_WINDOW_TYPE_NORMAL",
92   "_NET_WM_USER_TIME",
93   "_NET_VIRTUAL_ROOTS"
94 };
95
96 GType
97 _gdk_display_x11_get_type (void)
98 {
99   static GType object_type = 0;
100
101   if (!object_type)
102     {
103       static const GTypeInfo object_info =
104         {
105           sizeof (GdkDisplayX11Class),
106           (GBaseInitFunc) NULL,
107           (GBaseFinalizeFunc) NULL,
108           (GClassInitFunc) gdk_display_x11_class_init,
109           NULL,                 /* class_finalize */
110           NULL,                 /* class_data */
111           sizeof (GdkDisplayX11),
112           0,                    /* n_preallocs */
113           (GInstanceInitFunc) NULL,
114         };
115       
116       object_type = g_type_register_static (GDK_TYPE_DISPLAY,
117                                             "GdkDisplayX11",
118                                             &object_info, 0);
119     }
120   
121   return object_type;
122 }
123
124 static void
125 gdk_display_x11_class_init (GdkDisplayX11Class * class)
126 {
127   GObjectClass *object_class = G_OBJECT_CLASS (class);
128   
129   object_class->dispose = gdk_display_x11_dispose;
130   object_class->finalize = gdk_display_x11_finalize;
131   
132   parent_class = g_type_class_peek_parent (class);
133 }
134
135
136 /**
137  * gdk_display_open:
138  * @display_name: the name of the display to open
139  * @returns: a #GdkDisplay, or %NULL if the display
140  *  could not be opened.
141  *
142  * Opens a display.
143  *
144  * Since: 2.2
145  */
146 GdkDisplay *
147 gdk_display_open (const gchar *display_name)
148 {
149   Display *xdisplay;
150   GdkDisplay *display;
151   GdkDisplayX11 *display_x11;
152   GdkWindowAttr attr;
153   gint argc;
154   gchar *argv[1];
155   const char *sm_client_id;
156   
157   XClassHint *class_hint;
158   gulong pid;
159   gint i;
160 #ifdef HAVE_XFIXES
161   gint ignore;
162 #endif
163
164   xdisplay = XOpenDisplay (display_name);
165   if (!xdisplay)
166     return NULL;
167   
168   display = g_object_new (GDK_TYPE_DISPLAY_X11, NULL);
169   display_x11 = GDK_DISPLAY_X11 (display);
170
171   display_x11->use_xshm = TRUE;
172   display_x11->xdisplay = xdisplay;
173
174 #ifdef HAVE_X11R6  
175   /* Set up handlers for Xlib internal connections */
176   XAddConnectionWatch (xdisplay, gdk_internal_connection_watch, NULL);
177 #endif /* HAVE_X11R6 */
178   
179   /* initialize the display's screens */ 
180   display_x11->screens = g_new (GdkScreen *, ScreenCount (display_x11->xdisplay));
181   for (i = 0; i < ScreenCount (display_x11->xdisplay); i++)
182     display_x11->screens[i] = _gdk_x11_screen_new (display, i);
183
184   /* We need to initialize events after we have the screen
185    * structures in places
186    */
187   for (i = 0; i < ScreenCount (display_x11->xdisplay); i++)
188     _gdk_x11_events_init_screen (display_x11->screens[i]);
189   
190   /*set the default screen */
191   display_x11->default_screen = display_x11->screens[DefaultScreen (display_x11->xdisplay)];
192
193   attr.window_type = GDK_WINDOW_TOPLEVEL;
194   attr.wclass = GDK_INPUT_OUTPUT;
195   attr.x = 10;
196   attr.y = 10;
197   attr.width = 10;
198   attr.height = 10;
199   attr.event_mask = 0;
200
201   display_x11->leader_gdk_window = gdk_window_new (GDK_SCREEN_X11 (display_x11->default_screen)->root_window, 
202                                                    &attr, GDK_WA_X | GDK_WA_Y);
203   (_gdk_x11_window_get_toplevel (display_x11->leader_gdk_window))->is_leader = TRUE;
204
205   display_x11->leader_window = GDK_WINDOW_XID (display_x11->leader_gdk_window);
206
207   display_x11->leader_window_title_set = FALSE;
208
209   display_x11->have_render = GDK_UNKNOWN;
210   display_x11->have_render_with_trapezoids = 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 /**
492  * gdk_display_pointer_ungrab:
493  * @display: a #GdkDisplay.
494  * @time_: a timestap (e.g. GDK_CURRENT_TIME).
495  *
496  * Release any pointer grab.
497  *
498  * Since: 2.2
499  */
500 void
501 gdk_display_pointer_ungrab (GdkDisplay *display,
502                             guint32     time)
503 {
504   Display *xdisplay;
505   
506   g_return_if_fail (GDK_IS_DISPLAY (display));
507
508   xdisplay = GDK_DISPLAY_XDISPLAY (display);
509   
510   _gdk_input_ungrab_pointer (display, time);
511   XUngrabPointer (xdisplay, time);
512   XFlush (xdisplay);
513   
514   GDK_DISPLAY_X11 (display)->pointer_xgrab_window = NULL;
515 }
516
517 /**
518  * gdk_display_pointer_is_grabbed:
519  * @display: a #GdkDisplay
520  *
521  * Test if the pointer is grabbed.
522  *
523  * Returns: %TRUE if an active X pointer grab is in effect
524  *
525  * Since: 2.2
526  */
527 gboolean
528 gdk_display_pointer_is_grabbed (GdkDisplay * display)
529 {
530   g_return_val_if_fail (GDK_IS_DISPLAY (display), TRUE);
531   
532   return (GDK_DISPLAY_X11 (display)->pointer_xgrab_window != NULL);
533 }
534
535 /**
536  * gdk_display_keyboard_ungrab:
537  * @display: a #GdkDisplay.
538  * @time_: a timestap (e.g #GDK_CURRENT_TIME).
539  *
540  * Release any keyboard grab
541  *
542  * Since: 2.2
543  */
544 void
545 gdk_display_keyboard_ungrab (GdkDisplay *display,
546                              guint32     time)
547 {
548   Display *xdisplay;
549   
550   g_return_if_fail (GDK_IS_DISPLAY (display));
551
552   xdisplay = GDK_DISPLAY_XDISPLAY (display);
553   
554   XUngrabKeyboard (xdisplay, time);
555   XFlush (xdisplay);
556   
557   GDK_DISPLAY_X11 (display)->keyboard_xgrab_window = NULL;
558 }
559
560 /**
561  * gdk_display_beep:
562  * @display: a #GdkDisplay
563  *
564  * Emits a short beep on @display
565  *
566  * Since: 2.2
567  */
568 void
569 gdk_display_beep (GdkDisplay * display)
570 {
571   g_return_if_fail (GDK_IS_DISPLAY (display));
572   
573   XBell (GDK_DISPLAY_XDISPLAY (display), 0);
574 }
575
576 /**
577  * gdk_display_sync:
578  * @display: a #GdkDisplay
579  *
580  * Flushes any requests queued for the windowing system and waits until all
581  * requests have been handled. This is often used for making sure that the
582  * display is synchronized with the current state of the program. Calling
583  * gdk_display_sync() before gdk_error_trap_pop() makes sure that any errors
584  * generated from earlier requests are handled before the error trap is 
585  * removed.
586  *
587  * This is most useful for X11. On windowing systems where requests are
588  * handled synchronously, this function will do nothing.
589  *
590  * Since: 2.2
591  */
592 void
593 gdk_display_sync (GdkDisplay * display)
594 {
595   g_return_if_fail (GDK_IS_DISPLAY (display));
596   
597   XSync (GDK_DISPLAY_XDISPLAY (display), False);
598 }
599
600 /**
601  * gdk_display_flush:
602  * @display: a #GdkDisplay
603  *
604  * Flushes any requests queued for the windowing system; this happens automatically
605  * when the main loop blocks waiting for new events, but if your application
606  * is drawing without returning control to the main loop, you may need
607  * to call this function explicitely. A common case where this function
608  * needs to be called is when an application is executing drawing commands
609  * from a thread other than the thread where the main loop is running.
610  *
611  * This is most useful for X11. On windowing systems where requests are
612  * handled synchronously, this function will do nothing.
613  *
614  * Since: 2.4
615  */
616 void 
617 gdk_display_flush (GdkDisplay *display)
618 {
619   g_return_if_fail (GDK_IS_DISPLAY (display));
620
621   if (!display->closed)
622     XFlush (GDK_DISPLAY_XDISPLAY (display));
623 }
624
625 /**
626  * gdk_display_get_default_group:
627  * @display: a #GdkDisplay
628  * 
629  * Returns the default group leader window for all toplevel windows
630  * on @display. This window is implicitly created by GDK. 
631  * See gdk_window_set_group().
632  * 
633  * Return value: The default group leader window for @display
634  *
635  * Since: 2.4
636  **/
637 GdkWindow *gdk_display_get_default_group (GdkDisplay *display)
638 {
639   g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
640
641   return GDK_DISPLAY_X11 (display)->leader_gdk_window;
642 }
643
644 /**
645  * gdk_x11_display_grab:
646  * @display: a #GdkDisplay 
647  * 
648  * Call XGrabServer() on @display. 
649  * To ungrab the display again, use gdk_x11_display_ungrab(). 
650  *
651  * gdk_x11_display_grab()/gdk_x11_display_ungrab() calls can be nested.
652  *
653  * Since: 2.2
654  **/
655 void
656 gdk_x11_display_grab (GdkDisplay * display)
657 {
658   GdkDisplayX11 *display_x11;
659   
660   g_return_if_fail (GDK_IS_DISPLAY (display));
661   
662   display_x11 = GDK_DISPLAY_X11 (display);
663   
664   if (display_x11->grab_count == 0)
665     XGrabServer (display_x11->xdisplay);
666   display_x11->grab_count++;
667 }
668
669 /**
670  * gdk_x11_display_ungrab:
671  * @display: a #GdkDisplay
672  * 
673  * Ungrab @display after it has been grabbed with 
674  * gdk_x11_display_grab(). 
675  *
676  * Since: 2.2
677  **/
678 void
679 gdk_x11_display_ungrab (GdkDisplay * display)
680 {
681   GdkDisplayX11 *display_x11;
682   
683   g_return_if_fail (GDK_IS_DISPLAY (display));
684   
685   display_x11 = GDK_DISPLAY_X11 (display);;
686   g_return_if_fail (display_x11->grab_count > 0);
687   
688   display_x11->grab_count--;
689   if (display_x11->grab_count == 0)
690     {
691       XUngrabServer (display_x11->xdisplay);
692       XFlush (display_x11->xdisplay);
693     }
694 }
695
696 static void
697 gdk_display_x11_dispose (GObject *object)
698 {
699   GdkDisplayX11 *display_x11;
700   gint i;
701   
702   display_x11 = GDK_DISPLAY_X11 (object);
703   
704   for (i = 0; i < ScreenCount (display_x11->xdisplay); i++)
705     _gdk_screen_close (display_x11->screens[i]);
706
707   g_source_destroy (display_x11->event_source);
708
709   XCloseDisplay (display_x11->xdisplay);
710   display_x11->xdisplay = NULL;
711
712   G_OBJECT_CLASS (parent_class)->dispose (object);
713 }
714
715 static void
716 gdk_display_x11_finalize (GObject *object)
717 {
718   GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (object);
719   int i;
720   GList *tmp;
721   /* FIXME need to write GdkKeymap finalize fct 
722      g_object_unref (display_x11->keymap);
723    */
724   /* Free motif Dnd */
725   if (display_x11->motif_target_lists)
726     {
727       for (i = 0; i < display_x11->motif_n_target_lists; i++)
728         g_list_free (display_x11->motif_target_lists[i]);
729       g_free (display_x11->motif_target_lists);
730     }
731
732   /* Atom Hashtable */
733   g_hash_table_destroy (display_x11->atom_from_virtual);
734   g_hash_table_destroy (display_x11->atom_to_virtual);
735   /* Leader Window */
736   XDestroyWindow (display_x11->xdisplay, display_x11->leader_window);
737   /* list of filters for client messages */
738   g_list_free (display_x11->client_filters);
739   /* List of event window extraction functions */
740   g_slist_foreach (display_x11->event_types, (GFunc)g_free, NULL);
741   g_slist_free (display_x11->event_types);
742   /* X ID hashtable */
743   g_hash_table_destroy (display_x11->xid_ht);
744   /* input GdkDevice list */
745   /* FIXME need to write finalize fct */
746   for (tmp = display_x11->input_devices; tmp; tmp = tmp->next)
747     g_object_unref (tmp->data);
748   g_list_free (display_x11->input_devices);
749   /* input GdkWindow list */
750   for (tmp = display_x11->input_windows; tmp; tmp = tmp->next)
751     g_object_unref (tmp->data);
752   g_list_free (display_x11->input_windows);
753   /* Free all GdkScreens */
754   for (i = 0; i < ScreenCount (display_x11->xdisplay); i++)
755     g_object_unref (display_x11->screens[i]);
756   g_free (display_x11->screens);
757   g_free (display_x11->startup_notification_id);
758   
759   G_OBJECT_CLASS (parent_class)->finalize (object);
760 }
761
762 /**
763  * gdk_x11_lookup_xdisplay:
764  * @xdisplay: a pointer to an X Display
765  * 
766  * Find the #GdkDisplay corresponding to @display, if any exists.
767  * 
768  * Return value: the #GdkDisplay, if found, otherwise %NULL.
769  *
770  * Since: 2.2
771  **/
772 GdkDisplay *
773 gdk_x11_lookup_xdisplay (Display *xdisplay)
774 {
775   GSList *tmp_list;
776
777   for (tmp_list = _gdk_displays; tmp_list; tmp_list = tmp_list->next)
778     {
779       if (GDK_DISPLAY_XDISPLAY (tmp_list->data) == xdisplay)
780         return tmp_list->data;
781     }
782   
783   return NULL;
784 }
785
786 /**
787  * _gdk_x11_display_screen_for_xrootwin:
788  * @display: a #Display
789  * @xrootwin: window ID for one of of the screen's of the display.
790  * 
791  * Given the root window ID of one of the screen's of a #GdkDisplay,
792  * finds the screen.
793  * 
794  * Return value: the #GdkScreen corresponding to @xrootwin, or %NULL.
795  **/
796 GdkScreen *
797 _gdk_x11_display_screen_for_xrootwin (GdkDisplay *display,
798                                       Window      xrootwin)
799 {
800   gint n_screens, i;
801
802   n_screens = gdk_display_get_n_screens (display);
803   for (i = 0; i < n_screens; i++)
804     {
805       GdkScreen *screen = gdk_display_get_screen (display, i);
806       if (GDK_SCREEN_XROOTWIN (screen) == xrootwin)
807         return screen;
808     }
809
810   return NULL;
811 }
812
813 /**
814  * gdk_x11_display_get_xdisplay:
815  * @display: a #GdkDisplay
816  * @returns: an X display.
817  *
818  * Returns the X display of a #GdkDisplay.
819  *
820  * Since: 2.2
821  */
822 Display *
823 gdk_x11_display_get_xdisplay (GdkDisplay  *display)
824 {
825   return GDK_DISPLAY_X11 (display)->xdisplay;
826 }
827
828 void
829 _gdk_windowing_set_default_display (GdkDisplay *display)
830 {
831   GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
832   const gchar *startup_id;
833   
834   if (display)
835     gdk_display = GDK_DISPLAY_XDISPLAY (display);
836   else
837     gdk_display = NULL;
838
839   g_free (display_x11->startup_notification_id);
840   display_x11->startup_notification_id = NULL;
841   
842   startup_id = g_getenv ("DESKTOP_STARTUP_ID");
843   if (startup_id && *startup_id != '\0')
844     {
845       if (!g_utf8_validate (startup_id, -1, NULL))
846         g_warning ("DESKTOP_STARTUP_ID contains invalid UTF-8");
847       else
848         display_x11->startup_notification_id = g_strdup (startup_id);
849       
850       /* Clear the environment variable so it won't be inherited by
851        * child processes and confuse things.  
852        */
853       g_unsetenv ("DESKTOP_STARTUP_ID");
854
855       /* Set the startup id on the leader window so it
856        * applies to all windows we create on this display
857        */
858       XChangeProperty (display_x11->xdisplay,
859                        display_x11->leader_window,
860                        gdk_x11_get_xatom_by_name_for_display (display, "_NET_STARTUP_ID"),
861                        gdk_x11_get_xatom_by_name_for_display (display, "UTF8_STRING"), 8,
862                        PropModeReplace,
863                        startup_id, strlen (startup_id));
864     }
865 }
866
867 static char*
868 escape_for_xmessage (const char *str)
869 {
870   GString *retval;
871   const char *p;
872   
873   retval = g_string_new (NULL);
874
875   p = str;
876   while (*p)
877     {
878       switch (*p)
879         {
880         case ' ':
881         case '"':
882         case '\\':
883           g_string_append_c (retval, '\\');
884           break;
885         }
886
887       g_string_append_c (retval, *p);
888       ++p;
889     }
890
891   return g_string_free (retval, FALSE);
892 }
893
894 static void
895 broadcast_xmessage   (GdkDisplay   *display,
896                       const char   *message_type,
897                       const char   *message_type_begin,
898                       const char   *message)
899 {
900   Display *xdisplay = GDK_DISPLAY_XDISPLAY (display);
901   GdkScreen *screen = gdk_display_get_default_screen (display);
902   GdkWindow *root_window = gdk_screen_get_root_window (screen);
903   Window xroot_window = GDK_WINDOW_XID (root_window);
904   
905   Atom type_atom;
906   Atom type_atom_begin;
907   Window xwindow;
908
909   {
910     XSetWindowAttributes attrs;
911
912     attrs.override_redirect = True;
913     attrs.event_mask = PropertyChangeMask | StructureNotifyMask;
914
915     xwindow =
916       XCreateWindow (xdisplay,
917                      xroot_window,
918                      -100, -100, 1, 1,
919                      0,
920                      CopyFromParent,
921                      CopyFromParent,
922                      (Visual *)CopyFromParent,
923                      CWOverrideRedirect | CWEventMask,
924                      &attrs);
925   }
926
927   type_atom = gdk_x11_get_xatom_by_name_for_display (display,
928                                                      message_type);
929   type_atom_begin = gdk_x11_get_xatom_by_name_for_display (display,
930                                                            message_type_begin);
931   
932   {
933     XEvent xevent;
934     const char *src;
935     const char *src_end;
936     char *dest;
937     char *dest_end;
938     
939     xevent.xclient.type = ClientMessage;
940     xevent.xclient.message_type = type_atom_begin;
941     xevent.xclient.display =xdisplay;
942     xevent.xclient.window = xwindow;
943     xevent.xclient.format = 8;
944
945     src = message;
946     src_end = message + strlen (message) + 1; /* +1 to include nul byte */
947     
948     while (src != src_end)
949       {
950         dest = &xevent.xclient.data.b[0];
951         dest_end = dest + 20;        
952         
953         while (dest != dest_end &&
954                src != src_end)
955           {
956             *dest = *src;
957             ++dest;
958             ++src;
959           }
960
961         while (dest != dest_end)
962           {
963             *dest = 0;
964             ++dest;
965           }
966         
967         XSendEvent (xdisplay,
968                     xroot_window,
969                     False,
970                     PropertyChangeMask,
971                     &xevent);
972
973         xevent.xclient.message_type = type_atom;
974       }
975   }
976
977   XDestroyWindow (xdisplay, xwindow);
978   XFlush (xdisplay);
979 }
980
981 /**
982  * gdk_notify_startup_complete:
983  * 
984  * Indicates to the GUI environment that the application has finished
985  * loading. If the applications opens windows, this function is
986  * normally called after opening the application's initial set of
987  * windows.
988  * 
989  * GTK+ will call this function automatically after opening the first
990  * #GtkWindow unless gtk_window_set_auto_startup_notification() is called 
991  * to disable that feature.
992  *
993  * Since: 2.2
994  **/
995 void
996 gdk_notify_startup_complete (void)
997 {
998   GdkDisplay *display;
999   GdkDisplayX11 *display_x11;
1000   gchar *escaped_id;
1001   gchar *message;
1002
1003   display = gdk_display_get_default ();
1004   if (!display)
1005     return;
1006   
1007   display_x11 = GDK_DISPLAY_X11 (display);
1008
1009   if (display_x11->startup_notification_id == NULL)
1010     return;
1011
1012   escaped_id = escape_for_xmessage (display_x11->startup_notification_id);
1013   message = g_strdup_printf ("remove: ID=%s", escaped_id);
1014   g_free (escaped_id);
1015
1016   broadcast_xmessage (display,
1017                       "_NET_STARTUP_INFO",
1018                       "_NET_STARTUP_INFO_BEGIN",
1019                       message);
1020
1021   g_free (message);
1022 }
1023
1024
1025 /**
1026  * gdk_display_supports_selection_notification:
1027  * @display: a #GdkDisplay
1028  * 
1029  * Returns whether #GdkEventOwnerChange events will be 
1030  * sent when the owner of a selection changes.
1031  * 
1032  * Return value: whether #GdkEventOwnerChange events will 
1033  *               be sent.
1034  *
1035  * Since: 2.6
1036  **/
1037 gboolean 
1038 gdk_display_supports_selection_notification (GdkDisplay *display)
1039 {
1040   GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
1041
1042   return display_x11->have_xfixes;
1043 }
1044
1045 /**
1046  * gdk_display_request_selection_notification:
1047  * @display: a #GdkDisplay
1048  * @selection: the #GdkAtom naming the selection for which
1049  *             ownership change notification is requested
1050  * 
1051  * Request #GdkEventOwnerChange events for ownership changes
1052  * of the selection named by the given atom.
1053  * 
1054  * Return value: whether #GdkEventOwnerChange events will 
1055  *               be sent.
1056  *
1057  * Since: 2.6
1058  **/
1059 gboolean gdk_display_request_selection_notification  (GdkDisplay *display,
1060                                                       GdkAtom     selection)
1061
1062 {
1063 #ifdef HAVE_XFIXES
1064   GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
1065   Atom atom;
1066
1067   if (display_x11->have_xfixes)
1068     {
1069       atom = gdk_x11_atom_to_xatom_for_display (display, 
1070                                                 selection);
1071       XFixesSelectSelectionInput (display_x11->xdisplay, 
1072                                   display_x11->leader_window,
1073                                   atom,
1074                                   XFixesSetSelectionOwnerNotifyMask |
1075                                   XFixesSelectionWindowDestroyNotifyMask |
1076                                   XFixesSelectionClientCloseNotifyMask);
1077       return TRUE;
1078     }
1079   else
1080 #endif
1081     return FALSE;
1082 }
1083
1084 /**
1085  * gdk_display_supports_clipboard_persistence
1086  * @display: a #GdkDisplay
1087  *
1088  * Returns whether the speicifed display supports clipboard
1089  * persistance; i.e. if it's possible to store the clipboard data after an
1090  * application has quit. On X11 this checks if a clipboard daemon is
1091  * running.
1092  *
1093  * Returns: %TRUE if the display supports clipboard persistance.
1094  *
1095  * Since: 2.6
1096  */
1097 gboolean
1098 gdk_display_supports_clipboard_persistence (GdkDisplay *display)
1099 {
1100   /* It might make sense to cache this */
1101   return XGetSelectionOwner (GDK_DISPLAY_X11 (display)->xdisplay,
1102                              gdk_x11_get_xatom_by_name_for_display (display, "CLIPBOARD_MANAGER")) != None;
1103 }
1104
1105 /**
1106  * gdk_display_store_clipboard
1107  * @display:          a #GdkDisplay
1108  * @clipboard_window: a #GdkWindow belonging to the clipboard owner
1109  * @time_:            a timestamp
1110  * @targets:          an array of targets that should be saved, or %NULL 
1111  *                    if all available targets should be saved.
1112  * @n_targets:        length of the @targets array
1113  *
1114  * Issues a request to the the clipboard manager to store the
1115  * clipboard data. On X11, this is a special program that works
1116  * according to the freedesktop clipboard specification, available at
1117  * <ulink url="http://www.freedesktop.org/Standards/clipboard-manager-spec">
1118  * http://www.freedesktop.org/Standards/clipboard-manager-spec</ulink>.
1119  *
1120  * Since: 2.6
1121  */
1122 void
1123 gdk_display_store_clipboard (GdkDisplay *display,
1124                              GdkWindow  *clipboard_window,
1125                              guint32     time_,
1126                              GdkAtom    *targets,
1127                              gint        n_targets)
1128 {
1129   GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
1130   Atom clipboard_manager, save_targets;
1131   
1132   clipboard_manager = gdk_x11_get_xatom_by_name_for_display (display, "CLIPBOARD_MANAGER");
1133   save_targets = gdk_x11_get_xatom_by_name_for_display (display, "SAVE_TARGETS");
1134
1135   gdk_error_trap_push ();
1136
1137   if (XGetSelectionOwner (display_x11->xdisplay, clipboard_manager) != None)
1138     {
1139       Atom property_name = None;
1140       Atom *xatoms;
1141       int i;
1142       
1143       if (n_targets > 0)
1144         {
1145           property_name = gdk_x11_atom_to_xatom_for_display (display, _gdk_selection_property);
1146
1147           xatoms = g_new (Atom, n_targets);
1148           for (i = 0; i < n_targets; i++)
1149             xatoms[i] = gdk_x11_atom_to_xatom_for_display (display, targets[i]);
1150
1151           XChangeProperty (display_x11->xdisplay, GDK_WINDOW_XID (clipboard_window),
1152                            property_name, XA_ATOM,
1153                            32, PropModeReplace, (guchar *)xatoms, n_targets);
1154           g_free (xatoms);
1155
1156         }
1157       
1158       XConvertSelection (display_x11->xdisplay,
1159                          clipboard_manager, save_targets, property_name,
1160                          GDK_WINDOW_XID (clipboard_window), time_);
1161       
1162     }
1163   gdk_error_trap_pop ();
1164
1165 }
1166
1167 /**
1168  * gdk_x11_display_get_user_time:
1169  * @display: a #GdkDisplay
1170  *
1171  * Returns the timestamp of the last user interaction on 
1172  * @display. The timestamp is taken from events caused
1173  * by user interaction such as key presses or pointer 
1174  * movements. See gdk_x11_window_set_user_time().
1175  *
1176  * Returns: the timestamp of the last user interaction 
1177  *
1178  * Since: 2.8
1179  */
1180 guint32
1181 gdk_x11_display_get_user_time (GdkDisplay *display)
1182 {
1183   return GDK_DISPLAY_X11 (display)->user_time;
1184 }