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