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