]> Pileus Git - ~andy/gtk/blob - gdk/x11/gdkdisplay-x11.c
1cb5f67b790e0122110392aaba2af77fc6570a18
[~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 #ifdef HAVE_SHAPE_EXT
54 #include <X11/extensions/shape.h>
55 #endif
56
57 #ifdef HAVE_XCOMPOSITE
58 #include <X11/extensions/Xcomposite.h>
59 #endif
60
61 #ifdef HAVE_XDAMAGE
62 #include <X11/extensions/Xdamage.h>
63 #endif
64
65 #ifdef HAVE_RANDR
66 #include <X11/extensions/Xrandr.h>
67 #endif
68
69
70 static void   gdk_display_x11_dispose            (GObject            *object);
71 static void   gdk_display_x11_finalize           (GObject            *object);
72
73 #ifdef HAVE_X11R6
74 static void gdk_internal_connection_watch (Display  *display,
75                                            XPointer  arg,
76                                            gint      fd,
77                                            gboolean  opening,
78                                            XPointer *watch_data);
79 #endif /* HAVE_X11R6 */
80
81 /* Note that we never *directly* use WM_LOCALE_NAME, WM_PROTOCOLS,
82  * but including them here has the side-effect of getting them
83  * into the internal Xlib cache
84  */
85 static const char *const precache_atoms[] = {
86   "UTF8_STRING",
87   "WM_CLIENT_LEADER",
88   "WM_DELETE_WINDOW",
89   "WM_LOCALE_NAME",
90   "WM_PROTOCOLS",
91   "WM_TAKE_FOCUS",
92   "_NET_WM_CM_S0",
93   "_NET_WM_DESKTOP",
94   "_NET_WM_ICON",
95   "_NET_WM_ICON_NAME",
96   "_NET_WM_NAME",
97   "_NET_WM_PID",
98   "_NET_WM_PING",
99   "_NET_WM_STATE",
100   "_NET_WM_STATE_STICKY",
101   "_NET_WM_STATE_MAXIMIZED_VERT",
102   "_NET_WM_STATE_MAXIMIZED_HORZ",
103   "_NET_WM_STATE_FULLSCREEN",
104   "_NET_WM_SYNC_REQUEST",
105   "_NET_WM_SYNC_REQUEST_COUNTER",
106   "_NET_WM_WINDOW_TYPE",
107   "_NET_WM_WINDOW_TYPE_NORMAL",
108   "_NET_WM_USER_TIME",
109   "_NET_VIRTUAL_ROOTS"
110 };
111
112 G_DEFINE_TYPE (GdkDisplayX11, _gdk_display_x11, GDK_TYPE_DISPLAY)
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
123 static void
124 _gdk_display_x11_init (GdkDisplayX11 *display)
125 {
126 }
127
128 /**
129  * gdk_display_open:
130  * @display_name: the name of the display to open
131  * @returns: a #GdkDisplay, or %NULL if the display
132  *  could not be opened.
133  *
134  * Opens a display.
135  *
136  * Since: 2.2
137  */
138 GdkDisplay *
139 gdk_display_open (const gchar *display_name)
140 {
141   Display *xdisplay;
142   GdkDisplay *display;
143   GdkDisplayX11 *display_x11;
144   GdkWindowAttr attr;
145   gint argc;
146   gchar *argv[1];
147   const char *sm_client_id;
148   
149   XClassHint *class_hint;
150   gulong pid;
151   gint i;
152 #if defined(HAVE_XFIXES) || defined(HAVE_SHAPE_EXT)
153   gint ignore;
154   gint maj, min;
155 #endif
156
157   xdisplay = XOpenDisplay (display_name);
158   if (!xdisplay)
159     return NULL;
160   
161   display = g_object_new (GDK_TYPE_DISPLAY_X11, NULL);
162   display_x11 = GDK_DISPLAY_X11 (display);
163
164   display_x11->use_xshm = TRUE;
165   display_x11->xdisplay = xdisplay;
166
167 #ifdef HAVE_X11R6  
168   /* Set up handlers for Xlib internal connections */
169   XAddConnectionWatch (xdisplay, gdk_internal_connection_watch, NULL);
170 #endif /* HAVE_X11R6 */
171   
172   /* RandR must be initialized before we initialize the screens */
173   display_x11->have_randr12 = FALSE;
174 #ifdef HAVE_RANDR
175   if (XRRQueryExtension (display_x11->xdisplay,
176                          &display_x11->xrandr_event_base, &ignore))
177   {
178       int major, minor;
179       
180       XRRQueryVersion (display_x11->xdisplay, &major, &minor);
181
182       if ((major == 1 && minor >= 2) || major > 1)
183           display_x11->have_randr12 = TRUE;
184   }
185 #endif
186   
187   /* initialize the display's screens */ 
188   display_x11->screens = g_new (GdkScreen *, ScreenCount (display_x11->xdisplay));
189   for (i = 0; i < ScreenCount (display_x11->xdisplay); i++)
190     display_x11->screens[i] = _gdk_x11_screen_new (display, i);
191
192   /* We need to initialize events after we have the screen
193    * structures in places
194    */
195   for (i = 0; i < ScreenCount (display_x11->xdisplay); i++)
196     _gdk_x11_events_init_screen (display_x11->screens[i]);
197   
198   /*set the default screen */
199   display_x11->default_screen = display_x11->screens[DefaultScreen (display_x11->xdisplay)];
200
201   attr.window_type = GDK_WINDOW_TOPLEVEL;
202   attr.wclass = GDK_INPUT_OUTPUT;
203   attr.x = 10;
204   attr.y = 10;
205   attr.width = 10;
206   attr.height = 10;
207   attr.event_mask = 0;
208
209   _gdk_x11_precache_atoms (display, precache_atoms, G_N_ELEMENTS (precache_atoms));
210
211   display_x11->leader_gdk_window = gdk_window_new (GDK_SCREEN_X11 (display_x11->default_screen)->root_window, 
212                                                    &attr, GDK_WA_X | GDK_WA_Y);
213   (_gdk_x11_window_get_toplevel (display_x11->leader_gdk_window))->is_leader = TRUE;
214
215   display_x11->leader_window = GDK_WINDOW_XID (display_x11->leader_gdk_window);
216
217   display_x11->leader_window_title_set = FALSE;
218
219   display_x11->have_render = GDK_UNKNOWN;
220
221 #ifdef HAVE_XFIXES
222   if (XFixesQueryExtension (display_x11->xdisplay, 
223                             &display_x11->xfixes_event_base, 
224                             &ignore))
225     {
226       display_x11->have_xfixes = TRUE;
227
228       gdk_x11_register_standard_event_type (display,
229                                             display_x11->xfixes_event_base, 
230                                             XFixesNumberEvents);
231     }
232   else
233 #endif
234     display_x11->have_xfixes = FALSE;
235
236 #ifdef HAVE_XCOMPOSITE
237   if (XCompositeQueryExtension (display_x11->xdisplay,
238                                 &ignore, &ignore))
239       display_x11->have_xcomposite = TRUE;
240   else
241 #endif
242     display_x11->have_xcomposite = FALSE;
243
244 #ifdef HAVE_XDAMAGE
245   if (XDamageQueryExtension (display_x11->xdisplay,
246                              &display_x11->xdamage_event_base,
247                              &ignore))
248     {
249       display_x11->have_xdamage = TRUE;
250
251       gdk_x11_register_standard_event_type (display,
252                                             display_x11->xdamage_event_base,
253                                             XDamageNumberEvents);
254     }
255   else
256 #endif
257     display_x11->have_xdamage = FALSE;
258
259   display_x11->have_shapes = FALSE;
260   display_x11->have_input_shapes = FALSE;
261 #ifdef HAVE_SHAPE_EXT
262   if (XShapeQueryExtension (GDK_DISPLAY_XDISPLAY (display), &ignore, &ignore))
263     {
264       display_x11->have_shapes = TRUE;
265 #ifdef ShapeInput             
266       if (XShapeQueryVersion (GDK_DISPLAY_XDISPLAY (display), &maj, &min))
267         display_x11->have_input_shapes = (maj == 1 && min >= 1);
268 #endif
269     }
270 #endif
271
272   display_x11->trusted_client = TRUE;
273   {
274     Window root, child;
275     int rootx, rooty, winx, winy;
276     unsigned int xmask;
277
278     gdk_error_trap_push ();
279     XQueryPointer (display_x11->xdisplay, 
280                    GDK_SCREEN_X11 (display_x11->default_screen)->xroot_window,
281                    &root, &child, &rootx, &rooty, &winx, &winy, &xmask);
282     gdk_flush ();
283     if (G_UNLIKELY (gdk_error_trap_pop () == BadWindow)) 
284       {
285         g_warning ("Connection to display %s appears to be untrusted. Pointer and keyboard grabs and inter-client communication may not work as expected.", gdk_display_get_name (display));
286         display_x11->trusted_client = FALSE;
287       }
288   }
289
290   if (_gdk_synchronize)
291     XSynchronize (display_x11->xdisplay, True);
292   
293   class_hint = XAllocClassHint();
294   class_hint->res_name = g_get_prgname ();
295   
296   class_hint->res_class = (char *)gdk_get_program_class ();
297
298   /* XmbSetWMProperties sets the RESOURCE_NAME environment variable
299    * from argv[0], so we just synthesize an argument array here.
300    */
301   argc = 1;
302   argv[0] = g_get_prgname ();
303   
304   XmbSetWMProperties (display_x11->xdisplay,
305                       display_x11->leader_window,
306                       NULL, NULL, argv, argc, NULL, NULL,
307                       class_hint);
308   XFree (class_hint);
309
310   sm_client_id = _gdk_get_sm_client_id ();
311   if (sm_client_id)
312     _gdk_windowing_display_set_sm_client_id (display, sm_client_id);
313
314   pid = getpid ();
315   XChangeProperty (display_x11->xdisplay,
316                    display_x11->leader_window,
317                    gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_PID"),
318                    XA_CARDINAL, 32, PropModeReplace, (guchar *) & pid, 1);
319
320   /* We don't yet know a valid time. */
321   display_x11->user_time = 0;
322   
323 #ifdef HAVE_XKB
324   {
325     gint xkb_major = XkbMajorVersion;
326     gint xkb_minor = XkbMinorVersion;
327     if (XkbLibraryVersion (&xkb_major, &xkb_minor))
328       {
329         xkb_major = XkbMajorVersion;
330         xkb_minor = XkbMinorVersion;
331             
332         if (XkbQueryExtension (display_x11->xdisplay, 
333                                NULL, &display_x11->xkb_event_type, NULL,
334                                &xkb_major, &xkb_minor))
335           {
336             Bool detectable_autorepeat_supported;
337             
338             display_x11->use_xkb = TRUE;
339
340             XkbSelectEvents (display_x11->xdisplay,
341                              XkbUseCoreKbd,
342                              XkbNewKeyboardNotifyMask | XkbMapNotifyMask | XkbStateNotifyMask,
343                              XkbNewKeyboardNotifyMask | XkbMapNotifyMask | XkbStateNotifyMask);
344
345             /* keep this in sync with _gdk_keymap_state_changed() */ 
346             XkbSelectEventDetails (display_x11->xdisplay,
347                                    XkbUseCoreKbd, XkbStateNotify,
348                                    XkbAllStateComponentsMask,
349                                    XkbGroupLockMask);
350
351             XkbSetDetectableAutoRepeat (display_x11->xdisplay,
352                                         True,
353                                         &detectable_autorepeat_supported);
354
355             GDK_NOTE (MISC, g_message ("Detectable autorepeat %s.",
356                                        detectable_autorepeat_supported ? 
357                                        "supported" : "not supported"));
358             
359             display_x11->have_xkb_autorepeat = detectable_autorepeat_supported;
360           }
361       }
362   }
363 #endif
364
365   display_x11->use_sync = FALSE;
366 #ifdef HAVE_XSYNC
367   {
368     int major, minor;
369     int error_base, event_base;
370     
371     if (XSyncQueryExtension (display_x11->xdisplay,
372                              &event_base, &error_base) &&
373         XSyncInitialize (display_x11->xdisplay,
374                          &major, &minor))
375       display_x11->use_sync = TRUE;
376   }
377 #endif
378   
379   _gdk_windowing_image_init (display);
380   _gdk_events_init (display);
381   _gdk_input_init (display);
382   _gdk_dnd_init (display);
383
384   for (i = 0; i < ScreenCount (display_x11->xdisplay); i++)
385     gdk_display_request_selection_notification (display, 
386                                                 GDK_SCREEN_X11 (display_x11->screens[i])->cm_selection_atom);
387
388   g_signal_emit_by_name (gdk_display_manager_get(),
389                          "display_opened", display);
390
391   return display;
392 }
393
394 #ifdef HAVE_X11R6
395 /*
396  * XLib internal connection handling
397  */
398 typedef struct _GdkInternalConnection GdkInternalConnection;
399
400 struct _GdkInternalConnection
401 {
402   gint           fd;
403   GSource       *source;
404   Display       *display;
405 };
406
407 static gboolean
408 process_internal_connection (GIOChannel  *gioc,
409                              GIOCondition cond,
410                              gpointer     data)
411 {
412   GdkInternalConnection *connection = (GdkInternalConnection *)data;
413
414   GDK_THREADS_ENTER ();
415
416   XProcessInternalConnection ((Display*)connection->display, connection->fd);
417
418   GDK_THREADS_LEAVE ();
419
420   return TRUE;
421 }
422
423 static GdkInternalConnection *
424 gdk_add_connection_handler (Display *display,
425                             guint    fd)
426 {
427   GIOChannel *io_channel;
428   GdkInternalConnection *connection;
429
430   connection = g_new (GdkInternalConnection, 1);
431
432   connection->fd = fd;
433   connection->display = display;
434   
435   io_channel = g_io_channel_unix_new (fd);
436   
437   connection->source = g_io_create_watch (io_channel, G_IO_IN);
438   g_source_set_callback (connection->source,
439                          (GSourceFunc)process_internal_connection, connection, NULL);
440   g_source_attach (connection->source, NULL);
441   
442   g_io_channel_unref (io_channel);
443   
444   return connection;
445 }
446
447 static void
448 gdk_remove_connection_handler (GdkInternalConnection *connection)
449 {
450   g_source_destroy (connection->source);
451   g_free (connection);
452 }
453
454 static void
455 gdk_internal_connection_watch (Display  *display,
456                                XPointer  arg,
457                                gint      fd,
458                                gboolean  opening,
459                                XPointer *watch_data)
460 {
461   if (opening)
462     *watch_data = (XPointer)gdk_add_connection_handler (display, fd);
463   else
464     gdk_remove_connection_handler ((GdkInternalConnection *)*watch_data);
465 }
466 #endif /* HAVE_X11R6 */
467
468 /**
469  * gdk_display_get_name:
470  * @display: a #GdkDisplay
471  *
472  * Gets the name of the display.
473  * 
474  * Returns: a string representing the display name. This string is owned
475  * by GDK and should not be modified or freed.
476  * 
477  * Since: 2.2
478  */
479 G_CONST_RETURN gchar *
480 gdk_display_get_name (GdkDisplay *display)
481 {
482   g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
483   
484   return (gchar *) DisplayString (GDK_DISPLAY_X11 (display)->xdisplay);
485 }
486
487 /**
488  * gdk_display_get_n_screens:
489  * @display: a #GdkDisplay
490  *
491  * Gets the number of screen managed by the @display.
492  * 
493  * Returns: number of screens.
494  * 
495  * Since: 2.2
496  */
497 gint
498 gdk_display_get_n_screens (GdkDisplay *display)
499 {
500   g_return_val_if_fail (GDK_IS_DISPLAY (display), 0);
501   
502   return ScreenCount (GDK_DISPLAY_X11 (display)->xdisplay);
503 }
504
505 /**
506  * gdk_display_get_screen:
507  * @display: a #GdkDisplay
508  * @screen_num: the screen number
509  *
510  * Returns a screen object for one of the screens of the display.
511  *
512  * Returns: the #GdkScreen object
513  *
514  * Since: 2.2
515  */
516 GdkScreen *
517 gdk_display_get_screen (GdkDisplay *display, 
518                         gint        screen_num)
519 {
520   g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
521   g_return_val_if_fail (ScreenCount (GDK_DISPLAY_X11 (display)->xdisplay) > screen_num, NULL);
522   
523   return GDK_DISPLAY_X11 (display)->screens[screen_num];
524 }
525
526 /**
527  * gdk_display_get_default_screen:
528  * @display: a #GdkDisplay
529  *
530  * Get the default #GdkScreen for @display.
531  * 
532  * Returns: the default #GdkScreen object for @display
533  *
534  * Since: 2.2
535  */
536 GdkScreen *
537 gdk_display_get_default_screen (GdkDisplay *display)
538 {
539   g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
540   
541   return GDK_DISPLAY_X11 (display)->default_screen;
542 }
543
544 gboolean
545 _gdk_x11_display_is_root_window (GdkDisplay *display,
546                                  Window      xroot_window)
547 {
548   GdkDisplayX11 *display_x11;
549   gint i;
550   
551   g_return_val_if_fail (GDK_IS_DISPLAY (display), FALSE);
552   
553   display_x11 = GDK_DISPLAY_X11 (display);
554   
555   for (i = 0; i < ScreenCount (display_x11->xdisplay); i++)
556     {
557       if (GDK_SCREEN_XROOTWIN (display_x11->screens[i]) == xroot_window)
558         return TRUE;
559     }
560   return FALSE;
561 }
562
563 #define XSERVER_TIME_IS_LATER(time1, time2)                        \
564   ( (( time1 > time2 ) && ( time1 - time2 < ((guint32)-1)/2 )) ||  \
565     (( time1 < time2 ) && ( time2 - time1 > ((guint32)-1)/2 ))     \
566   )
567
568 /**
569  * gdk_display_pointer_ungrab:
570  * @display: a #GdkDisplay.
571  * @time_: a timestap (e.g. %GDK_CURRENT_TIME).
572  *
573  * Release any pointer grab.
574  *
575  * Since: 2.2
576  */
577 void
578 gdk_display_pointer_ungrab (GdkDisplay *display,
579                             guint32     time)
580 {
581   Display *xdisplay;
582   GdkDisplayX11 *display_x11;
583
584   g_return_if_fail (GDK_IS_DISPLAY (display));
585
586   display_x11 = GDK_DISPLAY_X11 (display);
587   xdisplay = GDK_DISPLAY_XDISPLAY (display);
588   
589   _gdk_input_ungrab_pointer (display, time);
590   XUngrabPointer (xdisplay, time);
591   XFlush (xdisplay);
592
593   if (time == GDK_CURRENT_TIME || 
594       display_x11->pointer_xgrab_time == GDK_CURRENT_TIME ||
595       !XSERVER_TIME_IS_LATER (display_x11->pointer_xgrab_time, time))
596     display_x11->pointer_xgrab_window = NULL;
597 }
598
599 /**
600  * gdk_display_pointer_is_grabbed:
601  * @display: a #GdkDisplay
602  *
603  * Test if the pointer is grabbed.
604  *
605  * Returns: %TRUE if an active X pointer grab is in effect
606  *
607  * Since: 2.2
608  */
609 gboolean
610 gdk_display_pointer_is_grabbed (GdkDisplay *display)
611 {
612   g_return_val_if_fail (GDK_IS_DISPLAY (display), TRUE);
613   
614   return (GDK_DISPLAY_X11 (display)->pointer_xgrab_window != NULL &&
615           !GDK_DISPLAY_X11 (display)->pointer_xgrab_implicit);
616 }
617
618 /**
619  * gdk_display_keyboard_ungrab:
620  * @display: a #GdkDisplay.
621  * @time_: a timestap (e.g #GDK_CURRENT_TIME).
622  *
623  * Release any keyboard grab
624  *
625  * Since: 2.2
626  */
627 void
628 gdk_display_keyboard_ungrab (GdkDisplay *display,
629                              guint32     time)
630 {
631   Display *xdisplay;
632   GdkDisplayX11 *display_x11;
633   
634   g_return_if_fail (GDK_IS_DISPLAY (display));
635
636   display_x11 = GDK_DISPLAY_X11 (display);
637   xdisplay = GDK_DISPLAY_XDISPLAY (display);
638   
639   XUngrabKeyboard (xdisplay, time);
640   XFlush (xdisplay);
641   
642   if (time == GDK_CURRENT_TIME || 
643       display_x11->keyboard_xgrab_time == GDK_CURRENT_TIME ||
644       !XSERVER_TIME_IS_LATER (display_x11->keyboard_xgrab_time, time))
645     display_x11->keyboard_xgrab_window = NULL;
646 }
647
648 /**
649  * gdk_display_beep:
650  * @display: a #GdkDisplay
651  *
652  * Emits a short beep on @display
653  *
654  * Since: 2.2
655  */
656 void
657 gdk_display_beep (GdkDisplay *display)
658 {
659   g_return_if_fail (GDK_IS_DISPLAY (display));
660   
661   XBell (GDK_DISPLAY_XDISPLAY (display), 0);
662 }
663
664 /**
665  * gdk_display_sync:
666  * @display: a #GdkDisplay
667  *
668  * Flushes any requests queued for the windowing system and waits until all
669  * requests have been handled. This is often used for making sure that the
670  * display is synchronized with the current state of the program. Calling
671  * gdk_display_sync() before gdk_error_trap_pop() makes sure that any errors
672  * generated from earlier requests are handled before the error trap is 
673  * removed.
674  *
675  * This is most useful for X11. On windowing systems where requests are
676  * handled synchronously, this function will do nothing.
677  *
678  * Since: 2.2
679  */
680 void
681 gdk_display_sync (GdkDisplay *display)
682 {
683   g_return_if_fail (GDK_IS_DISPLAY (display));
684   
685   XSync (GDK_DISPLAY_XDISPLAY (display), False);
686 }
687
688 /**
689  * gdk_display_flush:
690  * @display: a #GdkDisplay
691  *
692  * Flushes any requests queued for the windowing system; this happens automatically
693  * when the main loop blocks waiting for new events, but if your application
694  * is drawing without returning control to the main loop, you may need
695  * to call this function explicitely. A common case where this function
696  * needs to be called is when an application is executing drawing commands
697  * from a thread other than the thread where the main loop is running.
698  *
699  * This is most useful for X11. On windowing systems where requests are
700  * handled synchronously, this function will do nothing.
701  *
702  * Since: 2.4
703  */
704 void 
705 gdk_display_flush (GdkDisplay *display)
706 {
707   g_return_if_fail (GDK_IS_DISPLAY (display));
708
709   if (!display->closed)
710     XFlush (GDK_DISPLAY_XDISPLAY (display));
711 }
712
713 /**
714  * gdk_display_get_default_group:
715  * @display: a #GdkDisplay
716  * 
717  * Returns the default group leader window for all toplevel windows
718  * on @display. This window is implicitly created by GDK. 
719  * See gdk_window_set_group().
720  * 
721  * Return value: The default group leader window for @display
722  *
723  * Since: 2.4
724  **/
725 GdkWindow *
726 gdk_display_get_default_group (GdkDisplay *display)
727 {
728   g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
729
730   return GDK_DISPLAY_X11 (display)->leader_gdk_window;
731 }
732
733 /**
734  * gdk_x11_display_grab:
735  * @display: a #GdkDisplay 
736  * 
737  * Call XGrabServer() on @display. 
738  * To ungrab the display again, use gdk_x11_display_ungrab(). 
739  *
740  * gdk_x11_display_grab()/gdk_x11_display_ungrab() calls can be nested.
741  *
742  * Since: 2.2
743  **/
744 void
745 gdk_x11_display_grab (GdkDisplay *display)
746 {
747   GdkDisplayX11 *display_x11;
748   
749   g_return_if_fail (GDK_IS_DISPLAY (display));
750   
751   display_x11 = GDK_DISPLAY_X11 (display);
752   
753   if (display_x11->grab_count == 0)
754     XGrabServer (display_x11->xdisplay);
755   display_x11->grab_count++;
756 }
757
758 /**
759  * gdk_x11_display_ungrab:
760  * @display: a #GdkDisplay
761  * 
762  * Ungrab @display after it has been grabbed with 
763  * gdk_x11_display_grab(). 
764  *
765  * Since: 2.2
766  **/
767 void
768 gdk_x11_display_ungrab (GdkDisplay *display)
769 {
770   GdkDisplayX11 *display_x11;
771   
772   g_return_if_fail (GDK_IS_DISPLAY (display));
773   
774   display_x11 = GDK_DISPLAY_X11 (display);;
775   g_return_if_fail (display_x11->grab_count > 0);
776   
777   display_x11->grab_count--;
778   if (display_x11->grab_count == 0)
779     {
780       XUngrabServer (display_x11->xdisplay);
781       XFlush (display_x11->xdisplay);
782     }
783 }
784
785 static void
786 gdk_display_x11_dispose (GObject *object)
787 {
788   GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (object);
789   gint           i;
790
791   for (i = 0; i < ScreenCount (display_x11->xdisplay); i++)
792     _gdk_screen_close (display_x11->screens[i]);
793
794   _gdk_events_uninit (GDK_DISPLAY_OBJECT (object));
795
796   G_OBJECT_CLASS (_gdk_display_x11_parent_class)->dispose (object);
797 }
798
799 static void
800 gdk_display_x11_finalize (GObject *object)
801 {
802   GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (object);
803   gint           i;
804
805   /* Keymap */
806   if (display_x11->keymap)
807     g_object_unref (display_x11->keymap);
808
809   /* Free motif Dnd */
810   if (display_x11->motif_target_lists)
811     {
812       for (i = 0; i < display_x11->motif_n_target_lists; i++)
813         g_list_free (display_x11->motif_target_lists[i]);
814       g_free (display_x11->motif_target_lists);
815     }
816
817   /* Atom Hashtable */
818   g_hash_table_destroy (display_x11->atom_from_virtual);
819   g_hash_table_destroy (display_x11->atom_to_virtual);
820
821   /* Leader Window */
822   XDestroyWindow (display_x11->xdisplay, display_x11->leader_window);
823
824   /* list of filters for client messages */
825   g_list_foreach (display_x11->client_filters, (GFunc) g_free, NULL);
826   g_list_free (display_x11->client_filters);
827
828   /* List of event window extraction functions */
829   g_slist_foreach (display_x11->event_types, (GFunc)g_free, NULL);
830   g_slist_free (display_x11->event_types);
831
832   /* input GdkDevice list */
833   /* FIXME need to write finalize fct */
834   g_list_foreach (display_x11->input_devices, (GFunc) g_object_unref, NULL);
835   g_list_free (display_x11->input_devices);
836
837   /* input GdkWindow list */
838   g_list_foreach (display_x11->input_windows, (GFunc) g_free, NULL);
839   g_list_free (display_x11->input_windows);
840
841   /* Free all GdkScreens */
842   for (i = 0; i < ScreenCount (display_x11->xdisplay); i++)
843     g_object_unref (display_x11->screens[i]);
844   g_free (display_x11->screens);
845
846   g_free (display_x11->startup_notification_id);
847
848   /* X ID hashtable */
849   g_hash_table_destroy (display_x11->xid_ht);
850
851   XCloseDisplay (display_x11->xdisplay);
852
853   G_OBJECT_CLASS (_gdk_display_x11_parent_class)->finalize (object);
854 }
855
856 /**
857  * gdk_x11_lookup_xdisplay:
858  * @xdisplay: a pointer to an X Display
859  * 
860  * Find the #GdkDisplay corresponding to @display, if any exists.
861  * 
862  * Return value: the #GdkDisplay, if found, otherwise %NULL.
863  *
864  * Since: 2.2
865  **/
866 GdkDisplay *
867 gdk_x11_lookup_xdisplay (Display *xdisplay)
868 {
869   GSList *tmp_list;
870
871   for (tmp_list = _gdk_displays; tmp_list; tmp_list = tmp_list->next)
872     {
873       if (GDK_DISPLAY_XDISPLAY (tmp_list->data) == xdisplay)
874         return tmp_list->data;
875     }
876   
877   return NULL;
878 }
879
880 /**
881  * _gdk_x11_display_screen_for_xrootwin:
882  * @display: a #GdkDisplay
883  * @xrootwin: window ID for one of of the screen's of the display.
884  * 
885  * Given the root window ID of one of the screen's of a #GdkDisplay,
886  * finds the screen.
887  * 
888  * Return value: the #GdkScreen corresponding to @xrootwin, or %NULL.
889  **/
890 GdkScreen *
891 _gdk_x11_display_screen_for_xrootwin (GdkDisplay *display,
892                                       Window      xrootwin)
893 {
894   gint i;
895
896   for (i = 0; i < ScreenCount (GDK_DISPLAY_X11 (display)->xdisplay); i++)
897     {
898       GdkScreen *screen = gdk_display_get_screen (display, i);
899       if (GDK_SCREEN_XROOTWIN (screen) == xrootwin)
900         return screen;
901     }
902
903   return NULL;
904 }
905
906 /**
907  * gdk_x11_display_get_xdisplay:
908  * @display: a #GdkDisplay
909  * @returns: an X display.
910  *
911  * Returns the X display of a #GdkDisplay.
912  *
913  * Since: 2.2
914  */
915 Display *
916 gdk_x11_display_get_xdisplay (GdkDisplay *display)
917 {
918   g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
919   return GDK_DISPLAY_X11 (display)->xdisplay;
920 }
921
922 void
923 _gdk_windowing_set_default_display (GdkDisplay *display)
924 {
925   GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
926   const gchar *startup_id;
927   
928   if (!display)
929     {
930       gdk_display = NULL;
931       return;
932     }
933
934   gdk_display = GDK_DISPLAY_XDISPLAY (display);
935
936   g_free (display_x11->startup_notification_id);
937   display_x11->startup_notification_id = NULL;
938   
939   startup_id = g_getenv ("DESKTOP_STARTUP_ID");
940   if (startup_id && *startup_id != '\0')
941     {
942       gchar *time_str;
943
944       if (!g_utf8_validate (startup_id, -1, NULL))
945         g_warning ("DESKTOP_STARTUP_ID contains invalid UTF-8");
946       else
947         display_x11->startup_notification_id = g_strdup (startup_id);
948
949       /* Find the launch time from the startup_id, if it's there.  Newer spec
950        * states that the startup_id is of the form <unique>_TIME<timestamp>
951        */
952       time_str = g_strrstr (startup_id, "_TIME");
953       if (time_str != NULL)
954         {
955           gulong retval;
956           gchar *end;
957           errno = 0;
958
959           /* Skip past the "_TIME" part */
960           time_str += 5;
961
962           retval = strtoul (time_str, &end, 0);
963           if (end != time_str && errno == 0)
964             display_x11->user_time = retval;
965         }
966       
967       /* Clear the environment variable so it won't be inherited by
968        * child processes and confuse things.  
969        */
970       g_unsetenv ("DESKTOP_STARTUP_ID");
971
972       /* Set the startup id on the leader window so it
973        * applies to all windows we create on this display
974        */
975       XChangeProperty (display_x11->xdisplay,
976                        display_x11->leader_window,
977                        gdk_x11_get_xatom_by_name_for_display (display, "_NET_STARTUP_ID"),
978                        gdk_x11_get_xatom_by_name_for_display (display, "UTF8_STRING"), 8,
979                        PropModeReplace,
980                        (guchar *)startup_id, strlen (startup_id));
981     }
982 }
983
984 static void
985 broadcast_xmessage (GdkDisplay *display,
986                     const char *message_type,
987                     const char *message_type_begin,
988                     const char *message)
989 {
990   Display *xdisplay = GDK_DISPLAY_XDISPLAY (display);
991   GdkScreen *screen = gdk_display_get_default_screen (display);
992   GdkWindow *root_window = gdk_screen_get_root_window (screen);
993   Window xroot_window = GDK_WINDOW_XID (root_window);
994   
995   Atom type_atom;
996   Atom type_atom_begin;
997   Window xwindow;
998
999   if (!G_LIKELY (GDK_DISPLAY_X11 (display)->trusted_client))
1000     return;
1001
1002   {
1003     XSetWindowAttributes attrs;
1004
1005     attrs.override_redirect = True;
1006     attrs.event_mask = PropertyChangeMask | StructureNotifyMask;
1007
1008     xwindow =
1009       XCreateWindow (xdisplay,
1010                      xroot_window,
1011                      -100, -100, 1, 1,
1012                      0,
1013                      CopyFromParent,
1014                      CopyFromParent,
1015                      (Visual *)CopyFromParent,
1016                      CWOverrideRedirect | CWEventMask,
1017                      &attrs);
1018   }
1019
1020   type_atom = gdk_x11_get_xatom_by_name_for_display (display,
1021                                                      message_type);
1022   type_atom_begin = gdk_x11_get_xatom_by_name_for_display (display,
1023                                                            message_type_begin);
1024   
1025   {
1026     XClientMessageEvent xclient;
1027     const char *src;
1028     const char *src_end;
1029     char *dest;
1030     char *dest_end;
1031     
1032                 memset(&xclient, 0, sizeof (xclient));
1033     xclient.type = ClientMessage;
1034     xclient.message_type = type_atom_begin;
1035     xclient.display =xdisplay;
1036     xclient.window = xwindow;
1037     xclient.format = 8;
1038
1039     src = message;
1040     src_end = message + strlen (message) + 1; /* +1 to include nul byte */
1041     
1042     while (src != src_end)
1043       {
1044         dest = &xclient.data.b[0];
1045         dest_end = dest + 20;        
1046         
1047         while (dest != dest_end &&
1048                src != src_end)
1049           {
1050             *dest = *src;
1051             ++dest;
1052             ++src;
1053           }
1054
1055         while (dest != dest_end)
1056           {
1057             *dest = 0;
1058             ++dest;
1059           }
1060         
1061         XSendEvent (xdisplay,
1062                     xroot_window,
1063                     False,
1064                     PropertyChangeMask,
1065                     (XEvent *)&xclient);
1066
1067         xclient.message_type = type_atom;
1068       }
1069   }
1070
1071   XDestroyWindow (xdisplay, xwindow);
1072   XFlush (xdisplay);
1073 }
1074
1075 /**
1076  * gdk_x11_display_broadcast_startup_message:
1077  * @display: a #GdkDisplay
1078  * @message_type: startup notification message type ("new", "change",
1079  * or "remove")
1080  * @...: a list of key/value pairs (as strings), terminated by a
1081  * %NULL key. (A %NULL value for a key will cause that key to be
1082  * skipped in the output.)
1083  *
1084  * Sends a startup notification message of type @message_type to
1085  * @display. 
1086  *
1087  * This is a convenience function for use by code that implements the
1088  * freedesktop startup notification specification. Applications should
1089  * not normally need to call it directly. See the <ulink
1090  * url="http://standards.freedesktop.org/startup-notification-spec/startup-notification-latest.txt">Startup
1091  * Notification Protocol specification</ulink> for
1092  * definitions of the message types and keys that can be used.
1093  *
1094  * Since: 2.12
1095  **/
1096 void
1097 gdk_x11_display_broadcast_startup_message (GdkDisplay *display,
1098                                            const char *message_type,
1099                                            ...)
1100 {
1101   GString *message;
1102   va_list ap;
1103   const char *key, *value, *p;
1104
1105   message = g_string_new (message_type);
1106   g_string_append_c (message, ':');
1107
1108   va_start (ap, message_type);
1109   while ((key = va_arg (ap, const char *)))
1110     {
1111       value = va_arg (ap, const char *);
1112       if (!value)
1113         continue;
1114
1115       g_string_append_printf (message, " %s=\"", key);
1116       for (p = value; *p; p++)
1117         {
1118           switch (*p)
1119             {
1120             case ' ':
1121             case '"':
1122             case '\\':
1123               g_string_append_c (message, '\\');
1124               break;
1125             }
1126
1127           g_string_append_c (message, *p);
1128         }
1129       g_string_append_c (message, '\"');
1130     }
1131   va_end (ap);
1132
1133   broadcast_xmessage (display,
1134                       "_NET_STARTUP_INFO",
1135                       "_NET_STARTUP_INFO_BEGIN",
1136                       message->str);
1137
1138   g_string_free (message, TRUE);
1139 }
1140
1141 /**
1142  * gdk_notify_startup_complete:
1143  * 
1144  * Indicates to the GUI environment that the application has finished
1145  * loading. If the applications opens windows, this function is
1146  * normally called after opening the application's initial set of
1147  * windows.
1148  * 
1149  * GTK+ will call this function automatically after opening the first
1150  * #GtkWindow unless gtk_window_set_auto_startup_notification() is called 
1151  * to disable that feature.
1152  *
1153  * Since: 2.2
1154  **/
1155 void
1156 gdk_notify_startup_complete (void)
1157 {
1158   GdkDisplay *display;
1159   GdkDisplayX11 *display_x11;
1160
1161   display = gdk_display_get_default ();
1162   if (!display)
1163     return;
1164   
1165   display_x11 = GDK_DISPLAY_X11 (display);
1166
1167   if (display_x11->startup_notification_id == NULL)
1168     return;
1169
1170   gdk_notify_startup_complete_with_id (display_x11->startup_notification_id);
1171 }
1172
1173 /**
1174  * gdk_notify_startup_complete_with_id:
1175  * @startup_id: a startup-notification identifier, for which notification
1176  *              process should be completed
1177  * 
1178  * Indicates to the GUI environment that the application has finished
1179  * loading, using a given identifier.
1180  * 
1181  * GTK+ will call this function automatically for #GtkWindow with custom
1182  * startup-notification identifier unless
1183  * gtk_window_set_auto_startup_notification() is called to disable
1184  * that feature.
1185  *
1186  * Since: 2.12
1187  **/
1188 void
1189 gdk_notify_startup_complete_with_id (const gchar* startup_id)
1190 {
1191   GdkDisplay *display;
1192
1193   display = gdk_display_get_default ();
1194   if (!display)
1195     return;
1196
1197   gdk_x11_display_broadcast_startup_message (display, "remove",
1198                                              "ID", startup_id,
1199                                              NULL);
1200 }
1201
1202 /**
1203  * gdk_display_supports_selection_notification:
1204  * @display: a #GdkDisplay
1205  * 
1206  * Returns whether #GdkEventOwnerChange events will be 
1207  * sent when the owner of a selection changes.
1208  * 
1209  * Return value: whether #GdkEventOwnerChange events will 
1210  *               be sent.
1211  *
1212  * Since: 2.6
1213  **/
1214 gboolean 
1215 gdk_display_supports_selection_notification (GdkDisplay *display)
1216 {
1217   GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
1218
1219   return display_x11->have_xfixes;
1220 }
1221
1222 /**
1223  * gdk_display_request_selection_notification:
1224  * @display: a #GdkDisplay
1225  * @selection: the #GdkAtom naming the selection for which
1226  *             ownership change notification is requested
1227  * 
1228  * Request #GdkEventOwnerChange events for ownership changes
1229  * of the selection named by the given atom.
1230  * 
1231  * Return value: whether #GdkEventOwnerChange events will 
1232  *               be sent.
1233  *
1234  * Since: 2.6
1235  **/
1236 gboolean
1237 gdk_display_request_selection_notification (GdkDisplay *display,
1238                                             GdkAtom     selection)
1239
1240 {
1241 #ifdef HAVE_XFIXES
1242   GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
1243   Atom atom;
1244
1245   if (display_x11->have_xfixes)
1246     {
1247       atom = gdk_x11_atom_to_xatom_for_display (display, 
1248                                                 selection);
1249       XFixesSelectSelectionInput (display_x11->xdisplay, 
1250                                   display_x11->leader_window,
1251                                   atom,
1252                                   XFixesSetSelectionOwnerNotifyMask |
1253                                   XFixesSelectionWindowDestroyNotifyMask |
1254                                   XFixesSelectionClientCloseNotifyMask);
1255       return TRUE;
1256     }
1257   else
1258 #endif
1259     return FALSE;
1260 }
1261
1262 /**
1263  * gdk_display_supports_clipboard_persistence
1264  * @display: a #GdkDisplay
1265  *
1266  * Returns whether the speicifed display supports clipboard
1267  * persistance; i.e. if it's possible to store the clipboard data after an
1268  * application has quit. On X11 this checks if a clipboard daemon is
1269  * running.
1270  *
1271  * Returns: %TRUE if the display supports clipboard persistance.
1272  *
1273  * Since: 2.6
1274  */
1275 gboolean
1276 gdk_display_supports_clipboard_persistence (GdkDisplay *display)
1277 {
1278   Atom clipboard_manager;
1279
1280   /* It might make sense to cache this */
1281   clipboard_manager = gdk_x11_get_xatom_by_name_for_display (display, "CLIPBOARD_MANAGER");
1282   return XGetSelectionOwner (GDK_DISPLAY_X11 (display)->xdisplay, clipboard_manager) != None;
1283 }
1284
1285 /**
1286  * gdk_display_store_clipboard
1287  * @display:          a #GdkDisplay
1288  * @clipboard_window: a #GdkWindow belonging to the clipboard owner
1289  * @time_:            a timestamp
1290  * @targets:          an array of targets that should be saved, or %NULL 
1291  *                    if all available targets should be saved.
1292  * @n_targets:        length of the @targets array
1293  *
1294  * Issues a request to the clipboard manager to store the
1295  * clipboard data. On X11, this is a special program that works
1296  * according to the freedesktop clipboard specification, available at
1297  * <ulink url="http://www.freedesktop.org/Standards/clipboard-manager-spec">
1298  * http://www.freedesktop.org/Standards/clipboard-manager-spec</ulink>.
1299  *
1300  * Since: 2.6
1301  */
1302 void
1303 gdk_display_store_clipboard (GdkDisplay    *display,
1304                              GdkWindow     *clipboard_window,
1305                              guint32        time_,
1306                              const GdkAtom *targets,
1307                              gint           n_targets)
1308 {
1309   GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
1310   Atom clipboard_manager, save_targets;
1311   
1312   clipboard_manager = gdk_x11_get_xatom_by_name_for_display (display, "CLIPBOARD_MANAGER");
1313   save_targets = gdk_x11_get_xatom_by_name_for_display (display, "SAVE_TARGETS");
1314
1315   gdk_error_trap_push ();
1316
1317   if (XGetSelectionOwner (display_x11->xdisplay, clipboard_manager) != None)
1318     {
1319       Atom property_name = None;
1320       Atom *xatoms;
1321       int i;
1322       
1323       if (n_targets > 0)
1324         {
1325           property_name = gdk_x11_atom_to_xatom_for_display (display, _gdk_selection_property);
1326
1327           xatoms = g_new (Atom, n_targets);
1328           for (i = 0; i < n_targets; i++)
1329             xatoms[i] = gdk_x11_atom_to_xatom_for_display (display, targets[i]);
1330
1331           XChangeProperty (display_x11->xdisplay, GDK_WINDOW_XID (clipboard_window),
1332                            property_name, XA_ATOM,
1333                            32, PropModeReplace, (guchar *)xatoms, n_targets);
1334           g_free (xatoms);
1335
1336         }
1337       
1338       XConvertSelection (display_x11->xdisplay,
1339                          clipboard_manager, save_targets, property_name,
1340                          GDK_WINDOW_XID (clipboard_window), time_);
1341       
1342     }
1343   gdk_error_trap_pop ();
1344
1345 }
1346
1347 /**
1348  * gdk_x11_display_get_user_time:
1349  * @display: a #GdkDisplay
1350  *
1351  * Returns the timestamp of the last user interaction on 
1352  * @display. The timestamp is taken from events caused
1353  * by user interaction such as key presses or pointer 
1354  * movements. See gdk_x11_window_set_user_time().
1355  *
1356  * Returns: the timestamp of the last user interaction 
1357  *
1358  * Since: 2.8
1359  */
1360 guint32
1361 gdk_x11_display_get_user_time (GdkDisplay *display)
1362 {
1363   return GDK_DISPLAY_X11 (display)->user_time;
1364 }
1365
1366 /**
1367  * gdk_display_supports_shapes:
1368  * @display: a #GdkDisplay
1369  *
1370  * Returns %TRUE if gdk_window_shape_combine_mask() can
1371  * be used to create shaped windows on @display.
1372  *
1373  * Returns: %TRUE if shaped windows are supported 
1374  *
1375  * Since: 2.10
1376  */
1377 gboolean 
1378 gdk_display_supports_shapes (GdkDisplay *display)
1379 {
1380   return GDK_DISPLAY_X11 (display)->have_shapes;
1381 }
1382
1383 /**
1384  * gdk_display_supports_input_shapes:
1385  * @display: a #GdkDisplay
1386  *
1387  * Returns %TRUE if gdk_window_input_shape_combine_mask() can
1388  * be used to modify the input shape of windows on @display.
1389  *
1390  * Returns: %TRUE if windows with modified input shape are supported 
1391  *
1392  * Since: 2.10
1393  */
1394 gboolean 
1395 gdk_display_supports_input_shapes (GdkDisplay *display)
1396 {
1397   return GDK_DISPLAY_X11 (display)->have_input_shapes;
1398 }
1399
1400
1401 /**
1402  * gdk_x11_display_get_startup_notification_id:
1403  * @display: a #GdkDisplay
1404  *
1405  * Gets the startup notification ID for a display.
1406  * 
1407  * Returns: the startup notification ID for @display
1408  *
1409  * Since: 2.12
1410  */
1411 G_CONST_RETURN gchar *
1412 gdk_x11_display_get_startup_notification_id (GdkDisplay *display)
1413 {
1414   return GDK_DISPLAY_X11 (display)->startup_notification_id;
1415 }
1416
1417 /**
1418  * gdk_display_supports_composite:
1419  * @display: a #GdkDisplay
1420  *
1421  * Returns %TRUE if gdk_window_set_composited() can be used
1422  * to redirect drawing on the window using compositing.
1423  *
1424  * Currently this only works on X11 with XComposite and
1425  * XDamage extensions available.
1426  *
1427  * Returns: %TRUE if windows may be composited.
1428  *
1429  * Since: 2.12
1430  */
1431 gboolean
1432 gdk_display_supports_composite (GdkDisplay *display)
1433 {
1434   GdkDisplayX11 *x11_display = GDK_DISPLAY_X11 (display);
1435
1436   return x11_display->have_xcomposite &&
1437          x11_display->have_xdamage &&
1438          x11_display->have_xfixes;
1439 }
1440
1441
1442 #define __GDK_DISPLAY_X11_C__
1443 #include "gdkaliasdef.c"