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