]> Pileus Git - ~andy/gtk/blob - gdk/x11/gdkdisplay-x11.c
Switch set_cairo_target() virtual function to ref_cairo_surface()
[~andy/gtk] / gdk / x11 / gdkdisplay-x11.c
1 /* GDK - The GIMP Drawing Kit
2  * gdkdisplay-x11.c
3  * 
4  * Copyright 2001 Sun Microsystems Inc.
5  * Copyright (C) 2004 Nokia Corporation
6  *
7  * Erwann Chenede <erwann.chenede@sun.com>
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Library General Public
11  * License as published by the Free Software Foundation; either
12  * version 2 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Library General Public License for more details.
18  *
19  * You should have received a copy of the GNU Library General Public
20  * License along with this library; if not, write to the
21  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22  * Boston, MA 02111-1307, USA.
23  */
24
25 #include <config.h>
26
27 #include <stdlib.h>
28 #include <string.h>
29 #include <errno.h>
30 #include <unistd.h>
31
32 #include <glib.h>
33 #include "gdkx.h"
34 #include "gdkdisplay.h"
35 #include "gdkdisplay-x11.h"
36 #include "gdkscreen.h"
37 #include "gdkscreen-x11.h"
38 #include "gdkinternals.h"
39 #include "gdkinputprivate.h"
40 #include "xsettings-client.h"
41 #include "gdkalias.h"
42
43 #include <X11/Xatom.h>
44
45 #ifdef HAVE_XKB
46 #include <X11/XKBlib.h>
47 #endif
48
49 #ifdef HAVE_XFIXES
50 #include <X11/extensions/Xfixes.h>
51 #endif
52
53 static void                 gdk_display_x11_class_init         (GdkDisplayX11Class *class);
54 static void                 gdk_display_x11_dispose            (GObject            *object);
55 static void                 gdk_display_x11_finalize           (GObject            *object);
56
57 #ifdef HAVE_X11R6
58 static void gdk_internal_connection_watch (Display  *display,
59                                            XPointer  arg,
60                                            gint      fd,
61                                            gboolean  opening,
62                                            XPointer *watch_data);
63 #endif /* HAVE_X11R6 */
64
65 static gpointer parent_class = NULL;
66
67 /* Note that we never *directly* use WM_LOCALE_NAME, WM_PROTOCOLS,
68  * but including them here has the side-effect of getting them
69  * into the internal Xlib cache
70  */
71 static const char *const precache_atoms[] = {
72   "UTF8_STRING",
73   "WM_CLIENT_LEADER",
74   "WM_DELETE_WINDOW",
75   "WM_LOCALE_NAME",
76   "WM_PROTOCOLS",
77   "WM_TAKE_FOCUS",
78   "_NET_WM_DESKTOP",
79   "_NET_WM_ICON",
80   "_NET_WM_ICON_NAME",
81   "_NET_WM_NAME",
82   "_NET_WM_PID",
83   "_NET_WM_PING",
84   "_NET_WM_STATE",
85   "_NET_WM_STATE_STICKY",
86   "_NET_WM_STATE_MAXIMIZED_VERT",
87   "_NET_WM_STATE_MAXIMIZED_HORZ",
88   "_NET_WM_STATE_FULLSCREEN",
89   "_NET_WM_SYNC_REQUEST",
90   "_NET_WM_SYNC_REQUEST_COUNTER",
91   "_NET_WM_WINDOW_TYPE",
92   "_NET_WM_WINDOW_TYPE_NORMAL",
93   "_NET_WM_USER_TIME",
94   "_NET_VIRTUAL_ROOTS"
95 };
96
97 GType
98 _gdk_display_x11_get_type (void)
99 {
100   static GType object_type = 0;
101
102   if (!object_type)
103     {
104       static const GTypeInfo object_info =
105         {
106           sizeof (GdkDisplayX11Class),
107           (GBaseInitFunc) NULL,
108           (GBaseFinalizeFunc) NULL,
109           (GClassInitFunc) gdk_display_x11_class_init,
110           NULL,                 /* class_finalize */
111           NULL,                 /* class_data */
112           sizeof (GdkDisplayX11),
113           0,                    /* n_preallocs */
114           (GInstanceInitFunc) NULL,
115         };
116       
117       object_type = g_type_register_static (GDK_TYPE_DISPLAY,
118                                             "GdkDisplayX11",
119                                             &object_info, 0);
120     }
121   
122   return object_type;
123 }
124
125 static void
126 gdk_display_x11_class_init (GdkDisplayX11Class * class)
127 {
128   GObjectClass *object_class = G_OBJECT_CLASS (class);
129   
130   object_class->dispose = gdk_display_x11_dispose;
131   object_class->finalize = gdk_display_x11_finalize;
132   
133   parent_class = g_type_class_peek_parent (class);
134 }
135
136
137 /**
138  * gdk_display_open:
139  * @display_name: the name of the display to open
140  * @returns: a #GdkDisplay, or %NULL if the display
141  *  could not be opened.
142  *
143  * Opens a display.
144  *
145  * Since: 2.2
146  */
147 GdkDisplay *
148 gdk_display_open (const gchar *display_name)
149 {
150   Display *xdisplay;
151   GdkDisplay *display;
152   GdkDisplayX11 *display_x11;
153   GdkWindowAttr attr;
154   gint argc;
155   gchar *argv[1];
156   const char *sm_client_id;
157   
158   XClassHint *class_hint;
159   gulong pid;
160   gint i;
161 #ifdef HAVE_XFIXES
162   gint ignore;
163 #endif
164
165   xdisplay = XOpenDisplay (display_name);
166   if (!xdisplay)
167     return NULL;
168   
169   display = g_object_new (GDK_TYPE_DISPLAY_X11, NULL);
170   display_x11 = GDK_DISPLAY_X11 (display);
171
172   display_x11->use_xshm = TRUE;
173   display_x11->xdisplay = xdisplay;
174
175 #ifdef HAVE_X11R6  
176   /* Set up handlers for Xlib internal connections */
177   XAddConnectionWatch (xdisplay, gdk_internal_connection_watch, NULL);
178 #endif /* HAVE_X11R6 */
179   
180   /* initialize the display's screens */ 
181   display_x11->screens = g_new (GdkScreen *, ScreenCount (display_x11->xdisplay));
182   for (i = 0; i < ScreenCount (display_x11->xdisplay); i++)
183     display_x11->screens[i] = _gdk_x11_screen_new (display, i);
184
185   /* We need to initialize events after we have the screen
186    * structures in places
187    */
188   for (i = 0; i < ScreenCount (display_x11->xdisplay); i++)
189     _gdk_x11_events_init_screen (display_x11->screens[i]);
190   
191   /*set the default screen */
192   display_x11->default_screen = display_x11->screens[DefaultScreen (display_x11->xdisplay)];
193
194   attr.window_type = GDK_WINDOW_TOPLEVEL;
195   attr.wclass = GDK_INPUT_OUTPUT;
196   attr.x = 10;
197   attr.y = 10;
198   attr.width = 10;
199   attr.height = 10;
200   attr.event_mask = 0;
201
202   display_x11->leader_gdk_window = gdk_window_new (GDK_SCREEN_X11 (display_x11->default_screen)->root_window, 
203                                                    &attr, GDK_WA_X | GDK_WA_Y);
204   (_gdk_x11_window_get_toplevel (display_x11->leader_gdk_window))->is_leader = TRUE;
205
206   display_x11->leader_window = GDK_WINDOW_XID (display_x11->leader_gdk_window);
207
208   display_x11->leader_window_title_set = FALSE;
209
210   display_x11->have_render = GDK_UNKNOWN;
211
212 #ifdef HAVE_XFIXES
213   if (XFixesQueryExtension (display_x11->xdisplay, 
214                             &display_x11->xfixes_event_base, 
215                             &ignore))
216     {
217       display_x11->have_xfixes = TRUE;
218
219       gdk_x11_register_standard_event_type (display,
220                                             display_x11->xfixes_event_base, 
221                                             XFixesNumberEvents);
222     }
223   else
224 #endif
225   display_x11->have_xfixes = FALSE;
226
227   if (_gdk_synchronize)
228     XSynchronize (display_x11->xdisplay, True);
229   
230   _gdk_x11_precache_atoms (display, precache_atoms, G_N_ELEMENTS (precache_atoms));
231
232   class_hint = XAllocClassHint();
233   class_hint->res_name = g_get_prgname ();
234   
235   class_hint->res_class = (char *)gdk_get_program_class ();
236
237   /* XmbSetWMProperties sets the RESOURCE_NAME environment variable
238    * from argv[0], so we just synthesize an argument array here.
239    */
240   argc = 1;
241   argv[0] = g_get_prgname ();
242   
243   XmbSetWMProperties (display_x11->xdisplay,
244                       display_x11->leader_window,
245                       NULL, NULL, argv, argc, NULL, NULL,
246                       class_hint);
247   XFree (class_hint);
248
249   sm_client_id = _gdk_get_sm_client_id ();
250   if (sm_client_id)
251     _gdk_windowing_display_set_sm_client_id (display, sm_client_id);
252
253   pid = getpid ();
254   XChangeProperty (display_x11->xdisplay,
255                    display_x11->leader_window,
256                    gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_PID"),
257                    XA_CARDINAL, 32, PropModeReplace, (guchar *) & pid, 1);
258
259   /* We don't yet know a valid time. */
260   display_x11->user_time = 0;
261   
262 #ifdef HAVE_XKB
263   {
264     gint xkb_major = XkbMajorVersion;
265     gint xkb_minor = XkbMinorVersion;
266     if (XkbLibraryVersion (&xkb_major, &xkb_minor))
267       {
268         xkb_major = XkbMajorVersion;
269         xkb_minor = XkbMinorVersion;
270             
271         if (XkbQueryExtension (display_x11->xdisplay, 
272                                NULL, &display_x11->xkb_event_type, NULL,
273                                &xkb_major, &xkb_minor))
274           {
275             Bool detectable_autorepeat_supported;
276             
277             display_x11->use_xkb = TRUE;
278
279             XkbSelectEvents (display_x11->xdisplay,
280                              XkbUseCoreKbd,
281                              XkbNewKeyboardNotifyMask | XkbMapNotifyMask | XkbStateNotifyMask,
282                              XkbNewKeyboardNotifyMask | XkbMapNotifyMask | XkbStateNotifyMask);
283
284             XkbSetDetectableAutoRepeat (display_x11->xdisplay,
285                                         True,
286                                         &detectable_autorepeat_supported);
287
288             GDK_NOTE (MISC, g_message ("Detectable autorepeat %s.",
289                                        detectable_autorepeat_supported ? 
290                                        "supported" : "not supported"));
291             
292             display_x11->have_xkb_autorepeat = detectable_autorepeat_supported;
293           }
294       }
295   }
296 #endif
297
298   display_x11->use_sync = FALSE;
299 #ifdef HAVE_XSYNC
300   {
301     int major, minor;
302     int error_base, event_base;
303     
304     if (XSyncQueryExtension (display_x11->xdisplay,
305                              &event_base, &error_base) &&
306         XSyncInitialize (display_x11->xdisplay,
307                          &major, &minor))
308       display_x11->use_sync = TRUE;
309   }
310 #endif
311   
312   _gdk_windowing_image_init (display);
313   _gdk_events_init (display);
314   _gdk_input_init (display);
315   _gdk_dnd_init (display);
316
317   g_signal_emit_by_name (gdk_display_manager_get(),
318                          "display_opened", display);
319
320   return display;
321 }
322
323 #ifdef HAVE_X11R6
324 /*
325  * XLib internal connection handling
326  */
327 typedef struct _GdkInternalConnection GdkInternalConnection;
328
329 struct _GdkInternalConnection
330 {
331   gint           fd;
332   GSource       *source;
333   Display       *display;
334 };
335
336 static gboolean
337 process_internal_connection (GIOChannel  *gioc,
338                              GIOCondition cond,
339                              gpointer     data)
340 {
341   GdkInternalConnection *connection = (GdkInternalConnection *)data;
342
343   GDK_THREADS_ENTER ();
344
345   XProcessInternalConnection ((Display*)connection->display, connection->fd);
346
347   GDK_THREADS_LEAVE ();
348
349   return TRUE;
350 }
351
352 static GdkInternalConnection *
353 gdk_add_connection_handler (Display *display,
354                             guint    fd)
355 {
356   GIOChannel *io_channel;
357   GdkInternalConnection *connection;
358
359   connection = g_new (GdkInternalConnection, 1);
360
361   connection->fd = fd;
362   connection->display = display;
363   
364   io_channel = g_io_channel_unix_new (fd);
365   
366   connection->source = g_io_create_watch (io_channel, G_IO_IN);
367   g_source_set_callback (connection->source,
368                          (GSourceFunc)process_internal_connection, connection, NULL);
369   g_source_attach (connection->source, NULL);
370   
371   g_io_channel_unref (io_channel);
372   
373   return connection;
374 }
375
376 static void
377 gdk_remove_connection_handler (GdkInternalConnection *connection)
378 {
379   g_source_destroy (connection->source);
380   g_free (connection);
381 }
382
383 static void
384 gdk_internal_connection_watch (Display  *display,
385                                XPointer  arg,
386                                gint      fd,
387                                gboolean  opening,
388                                XPointer *watch_data)
389 {
390   if (opening)
391     *watch_data = (XPointer)gdk_add_connection_handler (display, fd);
392   else
393     gdk_remove_connection_handler ((GdkInternalConnection *)*watch_data);
394 }
395 #endif /* HAVE_X11R6 */
396
397 /**
398  * gdk_display_get_name:
399  * @display: a #GdkDisplay
400  *
401  * Gets the name of the display.
402  * 
403  * Returns: a string representing the display name. This string is owned
404  * by GDK and should not be modified or freed.
405  * 
406  * Since: 2.2
407  */
408 G_CONST_RETURN gchar *
409 gdk_display_get_name (GdkDisplay * display)
410 {
411   g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
412   
413   return (gchar *) DisplayString (GDK_DISPLAY_X11 (display)->xdisplay);
414 }
415
416 /**
417  * gdk_display_get_n_screens:
418  * @display: a #GdkDisplay
419  *
420  * Gets the number of screen managed by the @display.
421  * 
422  * Returns: number of screens.
423  * 
424  * Since: 2.2
425  */
426 gint
427 gdk_display_get_n_screens (GdkDisplay * display)
428 {
429   g_return_val_if_fail (GDK_IS_DISPLAY (display), 0);
430   
431   return ScreenCount (GDK_DISPLAY_X11 (display)->xdisplay);
432 }
433
434 /**
435  * gdk_display_get_screen:
436  * @display: a #GdkDisplay
437  * @screen_num: the screen number
438  *
439  * Returns a screen object for one of the screens of the display.
440  *
441  * Returns: the #GdkScreen object
442  *
443  * Since: 2.2
444  */
445 GdkScreen *
446 gdk_display_get_screen (GdkDisplay * display, gint screen_num)
447 {
448   g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
449   g_return_val_if_fail (ScreenCount (GDK_DISPLAY_X11 (display)->xdisplay) > screen_num, NULL);
450   
451   return GDK_DISPLAY_X11 (display)->screens[screen_num];
452 }
453
454 /**
455  * gdk_display_get_default_screen:
456  * @display: a #GdkDisplay
457  *
458  * Get the default #GdkScreen for @display.
459  * 
460  * Returns: the default #GdkScreen object for @display
461  *
462  * Since: 2.2
463  */
464 GdkScreen *
465 gdk_display_get_default_screen (GdkDisplay * display)
466 {
467   g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
468   
469   return GDK_DISPLAY_X11 (display)->default_screen;
470 }
471
472 gboolean
473 _gdk_x11_display_is_root_window (GdkDisplay *display,
474                                  Window      xroot_window)
475 {
476   GdkDisplayX11 *display_x11;
477   gint i;
478   
479   g_return_val_if_fail (GDK_IS_DISPLAY (display), FALSE);
480   
481   display_x11 = GDK_DISPLAY_X11 (display);
482   
483   for (i = 0; i < ScreenCount (display_x11->xdisplay); i++)
484     {
485       if (GDK_SCREEN_XROOTWIN (display_x11->screens[i]) == xroot_window)
486         return TRUE;
487     }
488   return FALSE;
489 }
490
491 /**
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       gchar *time_str;
846
847       if (!g_utf8_validate (startup_id, -1, NULL))
848         g_warning ("DESKTOP_STARTUP_ID contains invalid UTF-8");
849       else
850         display_x11->startup_notification_id = g_strdup (startup_id);
851
852       /* Find the launch time from the startup_id, if it's there.  Newer spec
853        * states that the startup_id is of the form <unique>_TIME<timestamp>
854        */
855       time_str = g_strrstr (startup_id, "_TIME");
856       if (time_str != NULL)
857         {
858           gulong retval;
859           gchar *end;
860           errno = 0;
861
862           /* Skip past the "_TIME" part */
863           time_str += 5;
864
865           retval = strtoul (time_str, &end, 0);
866           if (end != time_str && errno == 0)
867             display_x11->user_time = retval;
868         }
869       
870       /* Clear the environment variable so it won't be inherited by
871        * child processes and confuse things.  
872        */
873       g_unsetenv ("DESKTOP_STARTUP_ID");
874
875       /* Set the startup id on the leader window so it
876        * applies to all windows we create on this display
877        */
878       XChangeProperty (display_x11->xdisplay,
879                        display_x11->leader_window,
880                        gdk_x11_get_xatom_by_name_for_display (display, "_NET_STARTUP_ID"),
881                        gdk_x11_get_xatom_by_name_for_display (display, "UTF8_STRING"), 8,
882                        PropModeReplace,
883                        startup_id, strlen (startup_id));
884     }
885 }
886
887 static char*
888 escape_for_xmessage (const char *str)
889 {
890   GString *retval;
891   const char *p;
892   
893   retval = g_string_new (NULL);
894
895   p = str;
896   while (*p)
897     {
898       switch (*p)
899         {
900         case ' ':
901         case '"':
902         case '\\':
903           g_string_append_c (retval, '\\');
904           break;
905         }
906
907       g_string_append_c (retval, *p);
908       ++p;
909     }
910
911   return g_string_free (retval, FALSE);
912 }
913
914 static void
915 broadcast_xmessage   (GdkDisplay   *display,
916                       const char   *message_type,
917                       const char   *message_type_begin,
918                       const char   *message)
919 {
920   Display *xdisplay = GDK_DISPLAY_XDISPLAY (display);
921   GdkScreen *screen = gdk_display_get_default_screen (display);
922   GdkWindow *root_window = gdk_screen_get_root_window (screen);
923   Window xroot_window = GDK_WINDOW_XID (root_window);
924   
925   Atom type_atom;
926   Atom type_atom_begin;
927   Window xwindow;
928
929   {
930     XSetWindowAttributes attrs;
931
932     attrs.override_redirect = True;
933     attrs.event_mask = PropertyChangeMask | StructureNotifyMask;
934
935     xwindow =
936       XCreateWindow (xdisplay,
937                      xroot_window,
938                      -100, -100, 1, 1,
939                      0,
940                      CopyFromParent,
941                      CopyFromParent,
942                      (Visual *)CopyFromParent,
943                      CWOverrideRedirect | CWEventMask,
944                      &attrs);
945   }
946
947   type_atom = gdk_x11_get_xatom_by_name_for_display (display,
948                                                      message_type);
949   type_atom_begin = gdk_x11_get_xatom_by_name_for_display (display,
950                                                            message_type_begin);
951   
952   {
953     XEvent xevent;
954     const char *src;
955     const char *src_end;
956     char *dest;
957     char *dest_end;
958     
959     xevent.xclient.type = ClientMessage;
960     xevent.xclient.message_type = type_atom_begin;
961     xevent.xclient.display =xdisplay;
962     xevent.xclient.window = xwindow;
963     xevent.xclient.format = 8;
964
965     src = message;
966     src_end = message + strlen (message) + 1; /* +1 to include nul byte */
967     
968     while (src != src_end)
969       {
970         dest = &xevent.xclient.data.b[0];
971         dest_end = dest + 20;        
972         
973         while (dest != dest_end &&
974                src != src_end)
975           {
976             *dest = *src;
977             ++dest;
978             ++src;
979           }
980
981         while (dest != dest_end)
982           {
983             *dest = 0;
984             ++dest;
985           }
986         
987         XSendEvent (xdisplay,
988                     xroot_window,
989                     False,
990                     PropertyChangeMask,
991                     &xevent);
992
993         xevent.xclient.message_type = type_atom;
994       }
995   }
996
997   XDestroyWindow (xdisplay, xwindow);
998   XFlush (xdisplay);
999 }
1000
1001 /**
1002  * gdk_notify_startup_complete:
1003  * 
1004  * Indicates to the GUI environment that the application has finished
1005  * loading. If the applications opens windows, this function is
1006  * normally called after opening the application's initial set of
1007  * windows.
1008  * 
1009  * GTK+ will call this function automatically after opening the first
1010  * #GtkWindow unless gtk_window_set_auto_startup_notification() is called 
1011  * to disable that feature.
1012  *
1013  * Since: 2.2
1014  **/
1015 void
1016 gdk_notify_startup_complete (void)
1017 {
1018   GdkDisplay *display;
1019   GdkDisplayX11 *display_x11;
1020   gchar *escaped_id;
1021   gchar *message;
1022
1023   display = gdk_display_get_default ();
1024   if (!display)
1025     return;
1026   
1027   display_x11 = GDK_DISPLAY_X11 (display);
1028
1029   if (display_x11->startup_notification_id == NULL)
1030     return;
1031
1032   escaped_id = escape_for_xmessage (display_x11->startup_notification_id);
1033   message = g_strdup_printf ("remove: ID=%s", escaped_id);
1034   g_free (escaped_id);
1035
1036   broadcast_xmessage (display,
1037                       "_NET_STARTUP_INFO",
1038                       "_NET_STARTUP_INFO_BEGIN",
1039                       message);
1040
1041   g_free (message);
1042 }
1043
1044
1045 /**
1046  * gdk_display_supports_selection_notification:
1047  * @display: a #GdkDisplay
1048  * 
1049  * Returns whether #GdkEventOwnerChange events will be 
1050  * sent when the owner of a selection changes.
1051  * 
1052  * Return value: whether #GdkEventOwnerChange events will 
1053  *               be sent.
1054  *
1055  * Since: 2.6
1056  **/
1057 gboolean 
1058 gdk_display_supports_selection_notification (GdkDisplay *display)
1059 {
1060   GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
1061
1062   return display_x11->have_xfixes;
1063 }
1064
1065 /**
1066  * gdk_display_request_selection_notification:
1067  * @display: a #GdkDisplay
1068  * @selection: the #GdkAtom naming the selection for which
1069  *             ownership change notification is requested
1070  * 
1071  * Request #GdkEventOwnerChange events for ownership changes
1072  * of the selection named by the given atom.
1073  * 
1074  * Return value: whether #GdkEventOwnerChange events will 
1075  *               be sent.
1076  *
1077  * Since: 2.6
1078  **/
1079 gboolean gdk_display_request_selection_notification  (GdkDisplay *display,
1080                                                       GdkAtom     selection)
1081
1082 {
1083 #ifdef HAVE_XFIXES
1084   GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
1085   Atom atom;
1086
1087   if (display_x11->have_xfixes)
1088     {
1089       atom = gdk_x11_atom_to_xatom_for_display (display, 
1090                                                 selection);
1091       XFixesSelectSelectionInput (display_x11->xdisplay, 
1092                                   display_x11->leader_window,
1093                                   atom,
1094                                   XFixesSetSelectionOwnerNotifyMask |
1095                                   XFixesSelectionWindowDestroyNotifyMask |
1096                                   XFixesSelectionClientCloseNotifyMask);
1097       return TRUE;
1098     }
1099   else
1100 #endif
1101     return FALSE;
1102 }
1103
1104 /**
1105  * gdk_display_supports_clipboard_persistence
1106  * @display: a #GdkDisplay
1107  *
1108  * Returns whether the speicifed display supports clipboard
1109  * persistance; i.e. if it's possible to store the clipboard data after an
1110  * application has quit. On X11 this checks if a clipboard daemon is
1111  * running.
1112  *
1113  * Returns: %TRUE if the display supports clipboard persistance.
1114  *
1115  * Since: 2.6
1116  */
1117 gboolean
1118 gdk_display_supports_clipboard_persistence (GdkDisplay *display)
1119 {
1120   /* It might make sense to cache this */
1121   return XGetSelectionOwner (GDK_DISPLAY_X11 (display)->xdisplay,
1122                              gdk_x11_get_xatom_by_name_for_display (display, "CLIPBOARD_MANAGER")) != None;
1123 }
1124
1125 /**
1126  * gdk_display_store_clipboard
1127  * @display:          a #GdkDisplay
1128  * @clipboard_window: a #GdkWindow belonging to the clipboard owner
1129  * @time_:            a timestamp
1130  * @targets:          an array of targets that should be saved, or %NULL 
1131  *                    if all available targets should be saved.
1132  * @n_targets:        length of the @targets array
1133  *
1134  * Issues a request to the clipboard manager to store the
1135  * clipboard data. On X11, this is a special program that works
1136  * according to the freedesktop clipboard specification, available at
1137  * <ulink url="http://www.freedesktop.org/Standards/clipboard-manager-spec">
1138  * http://www.freedesktop.org/Standards/clipboard-manager-spec</ulink>.
1139  *
1140  * Since: 2.6
1141  */
1142 void
1143 gdk_display_store_clipboard (GdkDisplay *display,
1144                              GdkWindow  *clipboard_window,
1145                              guint32     time_,
1146                              GdkAtom    *targets,
1147                              gint        n_targets)
1148 {
1149   GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
1150   Atom clipboard_manager, save_targets;
1151   
1152   clipboard_manager = gdk_x11_get_xatom_by_name_for_display (display, "CLIPBOARD_MANAGER");
1153   save_targets = gdk_x11_get_xatom_by_name_for_display (display, "SAVE_TARGETS");
1154
1155   gdk_error_trap_push ();
1156
1157   if (XGetSelectionOwner (display_x11->xdisplay, clipboard_manager) != None)
1158     {
1159       Atom property_name = None;
1160       Atom *xatoms;
1161       int i;
1162       
1163       if (n_targets > 0)
1164         {
1165           property_name = gdk_x11_atom_to_xatom_for_display (display, _gdk_selection_property);
1166
1167           xatoms = g_new (Atom, n_targets);
1168           for (i = 0; i < n_targets; i++)
1169             xatoms[i] = gdk_x11_atom_to_xatom_for_display (display, targets[i]);
1170
1171           XChangeProperty (display_x11->xdisplay, GDK_WINDOW_XID (clipboard_window),
1172                            property_name, XA_ATOM,
1173                            32, PropModeReplace, (guchar *)xatoms, n_targets);
1174           g_free (xatoms);
1175
1176         }
1177       
1178       XConvertSelection (display_x11->xdisplay,
1179                          clipboard_manager, save_targets, property_name,
1180                          GDK_WINDOW_XID (clipboard_window), time_);
1181       
1182     }
1183   gdk_error_trap_pop ();
1184
1185 }
1186
1187 /**
1188  * gdk_x11_display_get_user_time:
1189  * @display: a #GdkDisplay
1190  *
1191  * Returns the timestamp of the last user interaction on 
1192  * @display. The timestamp is taken from events caused
1193  * by user interaction such as key presses or pointer 
1194  * movements. See gdk_x11_window_set_user_time().
1195  *
1196  * Returns: the timestamp of the last user interaction 
1197  *
1198  * Since: 2.8
1199  */
1200 guint32
1201 gdk_x11_display_get_user_time (GdkDisplay *display)
1202 {
1203   return GDK_DISPLAY_X11 (display)->user_time;
1204 }
1205
1206 #define __GDK_DISPLAY_X11_C__
1207 #include "gdkaliasdef.c"