1 /* GDK - The GIMP Drawing Kit
4 * Copyright 2001 Sun Microsystems Inc.
5 * Copyright (C) 2004 Nokia Corporation
7 * Erwann Chenede <erwann.chenede@sun.com>
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.
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.
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.
35 #include "gdkdisplay.h"
36 #include "gdkdisplay-x11.h"
37 #include "gdkscreen.h"
38 #include "gdkscreen-x11.h"
39 #include "gdkinternals.h"
40 #include "gdkinputprivate.h"
41 #include "xsettings-client.h"
44 #include <X11/Xatom.h>
47 #include <X11/XKBlib.h>
51 #include <X11/extensions/Xfixes.h>
54 #include <X11/extensions/shape.h>
56 #ifdef HAVE_XCOMPOSITE
57 #include <X11/extensions/Xcomposite.h>
61 #include <X11/extensions/Xdamage.h>
65 #include <X11/extensions/Xrandr.h>
69 static void gdk_display_x11_dispose (GObject *object);
70 static void gdk_display_x11_finalize (GObject *object);
73 static void gdk_internal_connection_watch (Display *display,
77 XPointer *watch_data);
78 #endif /* HAVE_X11R6 */
80 /* Note that we never *directly* use WM_LOCALE_NAME, WM_PROTOCOLS,
81 * but including them here has the side-effect of getting them
82 * into the internal Xlib cache
84 static const char *const precache_atoms[] = {
95 "_NET_CURRENT_DESKTOP",
106 "_NET_WM_STATE_ABOVE",
107 "_NET_WM_STATE_BELOW",
108 "_NET_WM_STATE_FULLSCREEN",
109 "_NET_WM_STATE_MODAL",
110 "_NET_WM_STATE_MAXIMIZED_VERT",
111 "_NET_WM_STATE_MAXIMIZED_HORZ",
112 "_NET_WM_STATE_SKIP_TASKBAR",
113 "_NET_WM_STATE_SKIP_PAGER",
114 "_NET_WM_STATE_STICKY",
115 "_NET_WM_SYNC_REQUEST",
116 "_NET_WM_SYNC_REQUEST_COUNTER",
117 "_NET_WM_WINDOW_TYPE",
118 "_NET_WM_WINDOW_TYPE_NORMAL",
123 G_DEFINE_TYPE (GdkDisplayX11, _gdk_display_x11, GDK_TYPE_DISPLAY)
126 _gdk_display_x11_class_init (GdkDisplayX11Class * class)
128 GObjectClass *object_class = G_OBJECT_CLASS (class);
130 object_class->dispose = gdk_display_x11_dispose;
131 object_class->finalize = gdk_display_x11_finalize;
135 _gdk_display_x11_init (GdkDisplayX11 *display)
141 * @display_name: the name of the display to open
142 * @returns: a #GdkDisplay, or %NULL if the display
143 * could not be opened.
150 gdk_display_open (const gchar *display_name)
154 GdkDisplayX11 *display_x11;
158 const char *sm_client_id;
160 XClassHint *class_hint;
166 xdisplay = XOpenDisplay (display_name);
170 display = g_object_new (GDK_TYPE_DISPLAY_X11, NULL);
171 display_x11 = GDK_DISPLAY_X11 (display);
173 display_x11->use_xshm = TRUE;
174 display_x11->xdisplay = xdisplay;
177 /* Set up handlers for Xlib internal connections */
178 XAddConnectionWatch (xdisplay, gdk_internal_connection_watch, NULL);
179 #endif /* HAVE_X11R6 */
181 _gdk_x11_precache_atoms (display, precache_atoms, G_N_ELEMENTS (precache_atoms));
183 /* RandR must be initialized before we initialize the screens */
184 display_x11->have_randr13 = FALSE;
186 if (XRRQueryExtension (display_x11->xdisplay,
187 &display_x11->xrandr_event_base, &ignore))
191 XRRQueryVersion (display_x11->xdisplay, &major, &minor);
193 if ((major == 1 && minor >= 3) || major > 1)
194 display_x11->have_randr13 = TRUE;
196 gdk_x11_register_standard_event_type (display, display_x11->xrandr_event_base, RRNumberEvents);
200 /* initialize the display's screens */
201 display_x11->screens = g_new (GdkScreen *, ScreenCount (display_x11->xdisplay));
202 for (i = 0; i < ScreenCount (display_x11->xdisplay); i++)
203 display_x11->screens[i] = _gdk_x11_screen_new (display, i);
205 /* We need to initialize events after we have the screen
206 * structures in places
208 for (i = 0; i < ScreenCount (display_x11->xdisplay); i++)
209 _gdk_x11_events_init_screen (display_x11->screens[i]);
211 /*set the default screen */
212 display_x11->default_screen = display_x11->screens[DefaultScreen (display_x11->xdisplay)];
214 attr.window_type = GDK_WINDOW_TOPLEVEL;
215 attr.wclass = GDK_INPUT_OUTPUT;
222 display_x11->leader_gdk_window = gdk_window_new (GDK_SCREEN_X11 (display_x11->default_screen)->root_window,
223 &attr, GDK_WA_X | GDK_WA_Y);
224 (_gdk_x11_window_get_toplevel (display_x11->leader_gdk_window))->is_leader = TRUE;
226 display_x11->leader_window = GDK_WINDOW_XID (display_x11->leader_gdk_window);
228 display_x11->leader_window_title_set = FALSE;
230 display_x11->have_render = GDK_UNKNOWN;
233 if (XFixesQueryExtension (display_x11->xdisplay,
234 &display_x11->xfixes_event_base,
237 display_x11->have_xfixes = TRUE;
239 gdk_x11_register_standard_event_type (display,
240 display_x11->xfixes_event_base,
245 display_x11->have_xfixes = FALSE;
247 #ifdef HAVE_XCOMPOSITE
248 if (XCompositeQueryExtension (display_x11->xdisplay,
253 XCompositeQueryVersion (display_x11->xdisplay, &major, &minor);
255 /* Prior to Composite version 0.4, composited windows clipped their
256 * parents, so you had to use IncludeInferiors to draw to the parent
257 * This isn't useful for our purposes, so require 0.4
259 display_x11->have_xcomposite = major > 0 || (major == 0 && minor >= 4);
263 display_x11->have_xcomposite = FALSE;
266 if (XDamageQueryExtension (display_x11->xdisplay,
267 &display_x11->xdamage_event_base,
270 display_x11->have_xdamage = TRUE;
272 gdk_x11_register_standard_event_type (display,
273 display_x11->xdamage_event_base,
274 XDamageNumberEvents);
278 display_x11->have_xdamage = FALSE;
280 display_x11->have_shapes = FALSE;
281 display_x11->have_input_shapes = FALSE;
283 if (XShapeQueryExtension (GDK_DISPLAY_XDISPLAY (display), &display_x11->shape_event_base, &ignore))
285 display_x11->have_shapes = TRUE;
287 if (XShapeQueryVersion (GDK_DISPLAY_XDISPLAY (display), &maj, &min))
288 display_x11->have_input_shapes = (maj == 1 && min >= 1);
292 display_x11->trusted_client = TRUE;
295 int rootx, rooty, winx, winy;
298 gdk_error_trap_push ();
299 XQueryPointer (display_x11->xdisplay,
300 GDK_SCREEN_X11 (display_x11->default_screen)->xroot_window,
301 &root, &child, &rootx, &rooty, &winx, &winy, &xmask);
303 if (G_UNLIKELY (gdk_error_trap_pop () == BadWindow))
305 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));
306 display_x11->trusted_client = FALSE;
310 if (_gdk_synchronize)
311 XSynchronize (display_x11->xdisplay, True);
313 class_hint = XAllocClassHint();
314 class_hint->res_name = g_get_prgname ();
316 class_hint->res_class = (char *)gdk_get_program_class ();
318 /* XmbSetWMProperties sets the RESOURCE_NAME environment variable
319 * from argv[0], so we just synthesize an argument array here.
322 argv[0] = g_get_prgname ();
324 XmbSetWMProperties (display_x11->xdisplay,
325 display_x11->leader_window,
326 NULL, NULL, argv, argc, NULL, NULL,
330 sm_client_id = _gdk_get_sm_client_id ();
332 _gdk_windowing_display_set_sm_client_id (display, sm_client_id);
335 XChangeProperty (display_x11->xdisplay,
336 display_x11->leader_window,
337 gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_PID"),
338 XA_CARDINAL, 32, PropModeReplace, (guchar *) & pid, 1);
340 /* We don't yet know a valid time. */
341 display_x11->user_time = 0;
345 gint xkb_major = XkbMajorVersion;
346 gint xkb_minor = XkbMinorVersion;
347 if (XkbLibraryVersion (&xkb_major, &xkb_minor))
349 xkb_major = XkbMajorVersion;
350 xkb_minor = XkbMinorVersion;
352 if (XkbQueryExtension (display_x11->xdisplay,
353 NULL, &display_x11->xkb_event_type, NULL,
354 &xkb_major, &xkb_minor))
356 Bool detectable_autorepeat_supported;
358 display_x11->use_xkb = TRUE;
360 XkbSelectEvents (display_x11->xdisplay,
362 XkbNewKeyboardNotifyMask | XkbMapNotifyMask | XkbStateNotifyMask,
363 XkbNewKeyboardNotifyMask | XkbMapNotifyMask | XkbStateNotifyMask);
365 /* keep this in sync with _gdk_keymap_state_changed() */
366 XkbSelectEventDetails (display_x11->xdisplay,
367 XkbUseCoreKbd, XkbStateNotify,
368 XkbAllStateComponentsMask,
369 XkbGroupLockMask|XkbModifierLockMask);
371 XkbSetDetectableAutoRepeat (display_x11->xdisplay,
373 &detectable_autorepeat_supported);
375 GDK_NOTE (MISC, g_message ("Detectable autorepeat %s.",
376 detectable_autorepeat_supported ?
377 "supported" : "not supported"));
379 display_x11->have_xkb_autorepeat = detectable_autorepeat_supported;
385 display_x11->use_sync = FALSE;
389 int error_base, event_base;
391 if (XSyncQueryExtension (display_x11->xdisplay,
392 &event_base, &error_base) &&
393 XSyncInitialize (display_x11->xdisplay,
395 display_x11->use_sync = TRUE;
399 _gdk_windowing_image_init (display);
400 _gdk_events_init (display);
401 _gdk_input_init (display);
402 _gdk_dnd_init (display);
404 for (i = 0; i < ScreenCount (display_x11->xdisplay); i++)
405 _gdk_x11_screen_setup (display_x11->screens[i]);
407 g_signal_emit_by_name (gdk_display_manager_get(),
408 "display_opened", display);
415 * XLib internal connection handling
417 typedef struct _GdkInternalConnection GdkInternalConnection;
419 struct _GdkInternalConnection
427 process_internal_connection (GIOChannel *gioc,
431 GdkInternalConnection *connection = (GdkInternalConnection *)data;
433 GDK_THREADS_ENTER ();
435 XProcessInternalConnection ((Display*)connection->display, connection->fd);
437 GDK_THREADS_LEAVE ();
443 _gdk_windowing_window_get_next_serial (GdkDisplay *display)
445 return NextRequest (GDK_DISPLAY_XDISPLAY (display));
449 static GdkInternalConnection *
450 gdk_add_connection_handler (Display *display,
453 GIOChannel *io_channel;
454 GdkInternalConnection *connection;
456 connection = g_new (GdkInternalConnection, 1);
459 connection->display = display;
461 io_channel = g_io_channel_unix_new (fd);
463 connection->source = g_io_create_watch (io_channel, G_IO_IN);
464 g_source_set_callback (connection->source,
465 (GSourceFunc)process_internal_connection, connection, NULL);
466 g_source_attach (connection->source, NULL);
468 g_io_channel_unref (io_channel);
474 gdk_remove_connection_handler (GdkInternalConnection *connection)
476 g_source_destroy (connection->source);
481 gdk_internal_connection_watch (Display *display,
485 XPointer *watch_data)
488 *watch_data = (XPointer)gdk_add_connection_handler (display, fd);
490 gdk_remove_connection_handler ((GdkInternalConnection *)*watch_data);
492 #endif /* HAVE_X11R6 */
495 * gdk_display_get_name:
496 * @display: a #GdkDisplay
498 * Gets the name of the display.
500 * Returns: a string representing the display name. This string is owned
501 * by GDK and should not be modified or freed.
505 G_CONST_RETURN gchar *
506 gdk_display_get_name (GdkDisplay *display)
508 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
510 return (gchar *) DisplayString (GDK_DISPLAY_X11 (display)->xdisplay);
514 * gdk_display_get_n_screens:
515 * @display: a #GdkDisplay
517 * Gets the number of screen managed by the @display.
519 * Returns: number of screens.
524 gdk_display_get_n_screens (GdkDisplay *display)
526 g_return_val_if_fail (GDK_IS_DISPLAY (display), 0);
528 return ScreenCount (GDK_DISPLAY_X11 (display)->xdisplay);
532 * gdk_display_get_screen:
533 * @display: a #GdkDisplay
534 * @screen_num: the screen number
536 * Returns a screen object for one of the screens of the display.
538 * Returns: the #GdkScreen object
543 gdk_display_get_screen (GdkDisplay *display,
546 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
547 g_return_val_if_fail (ScreenCount (GDK_DISPLAY_X11 (display)->xdisplay) > screen_num, NULL);
549 return GDK_DISPLAY_X11 (display)->screens[screen_num];
553 * gdk_display_get_default_screen:
554 * @display: a #GdkDisplay
556 * Get the default #GdkScreen for @display.
558 * Returns: the default #GdkScreen object for @display
563 gdk_display_get_default_screen (GdkDisplay *display)
565 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
567 return GDK_DISPLAY_X11 (display)->default_screen;
571 _gdk_x11_display_is_root_window (GdkDisplay *display,
574 GdkDisplayX11 *display_x11;
577 g_return_val_if_fail (GDK_IS_DISPLAY (display), FALSE);
579 display_x11 = GDK_DISPLAY_X11 (display);
581 for (i = 0; i < ScreenCount (display_x11->xdisplay); i++)
583 if (GDK_SCREEN_XROOTWIN (display_x11->screens[i]) == xroot_window)
589 struct XPointerUngrabInfo {
595 pointer_ungrab_callback (GdkDisplay *display,
599 _gdk_display_pointer_grab_update (display, serial);
603 #define XSERVER_TIME_IS_LATER(time1, time2) \
604 ( (( time1 > time2 ) && ( time1 - time2 < ((guint32)-1)/2 )) || \
605 (( time1 < time2 ) && ( time2 - time1 > ((guint32)-1)/2 )) \
609 * gdk_display_pointer_ungrab:
610 * @display: a #GdkDisplay.
611 * @time_: a timestap (e.g. %GDK_CURRENT_TIME).
613 * Release any pointer grab.
618 gdk_display_pointer_ungrab (GdkDisplay *display,
622 GdkDisplayX11 *display_x11;
623 GdkPointerGrabInfo *grab;
624 unsigned long serial;
626 g_return_if_fail (GDK_IS_DISPLAY (display));
628 display_x11 = GDK_DISPLAY_X11 (display);
629 xdisplay = GDK_DISPLAY_XDISPLAY (display);
631 serial = NextRequest (xdisplay);
633 _gdk_input_ungrab_pointer (display, time_);
634 XUngrabPointer (xdisplay, time_);
637 grab = _gdk_display_get_last_pointer_grab (display);
639 (time_ == GDK_CURRENT_TIME ||
640 grab->time == GDK_CURRENT_TIME ||
641 !XSERVER_TIME_IS_LATER (grab->time, time_)))
643 grab->serial_end = serial;
644 _gdk_x11_roundtrip_async (display,
645 pointer_ungrab_callback,
651 * gdk_display_keyboard_ungrab:
652 * @display: a #GdkDisplay.
653 * @time_: a timestap (e.g #GDK_CURRENT_TIME).
655 * Release any keyboard grab
660 gdk_display_keyboard_ungrab (GdkDisplay *display,
664 GdkDisplayX11 *display_x11;
666 g_return_if_fail (GDK_IS_DISPLAY (display));
668 display_x11 = GDK_DISPLAY_X11 (display);
669 xdisplay = GDK_DISPLAY_XDISPLAY (display);
671 XUngrabKeyboard (xdisplay, time);
674 if (time == GDK_CURRENT_TIME ||
675 display->keyboard_grab.time == GDK_CURRENT_TIME ||
676 !XSERVER_TIME_IS_LATER (display->keyboard_grab.time, time))
677 _gdk_display_unset_has_keyboard_grab (display, FALSE);
682 * @display: a #GdkDisplay
684 * Emits a short beep on @display
689 gdk_display_beep (GdkDisplay *display)
691 g_return_if_fail (GDK_IS_DISPLAY (display));
694 XkbBell (GDK_DISPLAY_XDISPLAY (display), None, 0, None);
696 XBell (GDK_DISPLAY_XDISPLAY (display), 0);
702 * @display: a #GdkDisplay
704 * Flushes any requests queued for the windowing system and waits until all
705 * requests have been handled. This is often used for making sure that the
706 * display is synchronized with the current state of the program. Calling
707 * gdk_display_sync() before gdk_error_trap_pop() makes sure that any errors
708 * generated from earlier requests are handled before the error trap is
711 * This is most useful for X11. On windowing systems where requests are
712 * handled synchronously, this function will do nothing.
717 gdk_display_sync (GdkDisplay *display)
719 g_return_if_fail (GDK_IS_DISPLAY (display));
721 XSync (GDK_DISPLAY_XDISPLAY (display), False);
726 * @display: a #GdkDisplay
728 * Flushes any requests queued for the windowing system; this happens automatically
729 * when the main loop blocks waiting for new events, but if your application
730 * is drawing without returning control to the main loop, you may need
731 * to call this function explicitely. A common case where this function
732 * needs to be called is when an application is executing drawing commands
733 * from a thread other than the thread where the main loop is running.
735 * This is most useful for X11. On windowing systems where requests are
736 * handled synchronously, this function will do nothing.
741 gdk_display_flush (GdkDisplay *display)
743 g_return_if_fail (GDK_IS_DISPLAY (display));
745 if (!display->closed)
746 XFlush (GDK_DISPLAY_XDISPLAY (display));
750 * gdk_display_get_default_group:
751 * @display: a #GdkDisplay
753 * Returns the default group leader window for all toplevel windows
754 * on @display. This window is implicitly created by GDK.
755 * See gdk_window_set_group().
757 * Return value: The default group leader window for @display
762 gdk_display_get_default_group (GdkDisplay *display)
764 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
766 return GDK_DISPLAY_X11 (display)->leader_gdk_window;
770 * gdk_x11_display_grab:
771 * @display: a #GdkDisplay
773 * Call XGrabServer() on @display.
774 * To ungrab the display again, use gdk_x11_display_ungrab().
776 * gdk_x11_display_grab()/gdk_x11_display_ungrab() calls can be nested.
781 gdk_x11_display_grab (GdkDisplay *display)
783 GdkDisplayX11 *display_x11;
785 g_return_if_fail (GDK_IS_DISPLAY (display));
787 display_x11 = GDK_DISPLAY_X11 (display);
789 if (display_x11->grab_count == 0)
790 XGrabServer (display_x11->xdisplay);
791 display_x11->grab_count++;
795 * gdk_x11_display_ungrab:
796 * @display: a #GdkDisplay
798 * Ungrab @display after it has been grabbed with
799 * gdk_x11_display_grab().
804 gdk_x11_display_ungrab (GdkDisplay *display)
806 GdkDisplayX11 *display_x11;
808 g_return_if_fail (GDK_IS_DISPLAY (display));
810 display_x11 = GDK_DISPLAY_X11 (display);;
811 g_return_if_fail (display_x11->grab_count > 0);
813 display_x11->grab_count--;
814 if (display_x11->grab_count == 0)
816 XUngrabServer (display_x11->xdisplay);
817 XFlush (display_x11->xdisplay);
822 gdk_display_x11_dispose (GObject *object)
824 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (object);
827 g_list_foreach (display_x11->input_devices, (GFunc) g_object_run_dispose, NULL);
829 for (i = 0; i < ScreenCount (display_x11->xdisplay); i++)
830 _gdk_screen_close (display_x11->screens[i]);
832 _gdk_events_uninit (GDK_DISPLAY_OBJECT (object));
834 G_OBJECT_CLASS (_gdk_display_x11_parent_class)->dispose (object);
838 gdk_display_x11_finalize (GObject *object)
840 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (object);
844 if (display_x11->keymap)
845 g_object_unref (display_x11->keymap);
848 if (display_x11->motif_target_lists)
850 for (i = 0; i < display_x11->motif_n_target_lists; i++)
851 g_list_free (display_x11->motif_target_lists[i]);
852 g_free (display_x11->motif_target_lists);
855 _gdk_x11_cursor_display_finalize (GDK_DISPLAY_OBJECT(display_x11));
858 g_hash_table_destroy (display_x11->atom_from_virtual);
859 g_hash_table_destroy (display_x11->atom_to_virtual);
862 XDestroyWindow (display_x11->xdisplay, display_x11->leader_window);
864 /* list of filters for client messages */
865 g_list_foreach (display_x11->client_filters, (GFunc) g_free, NULL);
866 g_list_free (display_x11->client_filters);
868 /* List of event window extraction functions */
869 g_slist_foreach (display_x11->event_types, (GFunc)g_free, NULL);
870 g_slist_free (display_x11->event_types);
872 /* input GdkDevice list */
873 g_list_foreach (display_x11->input_devices, (GFunc) g_object_unref, NULL);
874 g_list_free (display_x11->input_devices);
876 /* input GdkWindow list */
877 g_list_foreach (display_x11->input_windows, (GFunc) g_free, NULL);
878 g_list_free (display_x11->input_windows);
880 /* Free all GdkScreens */
881 for (i = 0; i < ScreenCount (display_x11->xdisplay); i++)
882 g_object_unref (display_x11->screens[i]);
883 g_free (display_x11->screens);
885 g_free (display_x11->startup_notification_id);
888 g_hash_table_destroy (display_x11->xid_ht);
890 XCloseDisplay (display_x11->xdisplay);
892 G_OBJECT_CLASS (_gdk_display_x11_parent_class)->finalize (object);
896 * gdk_x11_lookup_xdisplay:
897 * @xdisplay: a pointer to an X Display
899 * Find the #GdkDisplay corresponding to @display, if any exists.
901 * Return value: the #GdkDisplay, if found, otherwise %NULL.
906 gdk_x11_lookup_xdisplay (Display *xdisplay)
910 for (tmp_list = _gdk_displays; tmp_list; tmp_list = tmp_list->next)
912 if (GDK_DISPLAY_XDISPLAY (tmp_list->data) == xdisplay)
913 return tmp_list->data;
920 * _gdk_x11_display_screen_for_xrootwin:
921 * @display: a #GdkDisplay
922 * @xrootwin: window ID for one of of the screen's of the display.
924 * Given the root window ID of one of the screen's of a #GdkDisplay,
927 * Return value: the #GdkScreen corresponding to @xrootwin, or %NULL.
930 _gdk_x11_display_screen_for_xrootwin (GdkDisplay *display,
935 for (i = 0; i < ScreenCount (GDK_DISPLAY_X11 (display)->xdisplay); i++)
937 GdkScreen *screen = gdk_display_get_screen (display, i);
938 if (GDK_SCREEN_XROOTWIN (screen) == xrootwin)
946 * gdk_x11_display_get_xdisplay:
947 * @display: a #GdkDisplay
948 * @returns: an X display.
950 * Returns the X display of a #GdkDisplay.
955 gdk_x11_display_get_xdisplay (GdkDisplay *display)
957 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
958 return GDK_DISPLAY_X11 (display)->xdisplay;
962 _gdk_windowing_set_default_display (GdkDisplay *display)
964 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
965 const gchar *startup_id;
973 gdk_display = GDK_DISPLAY_XDISPLAY (display);
975 g_free (display_x11->startup_notification_id);
976 display_x11->startup_notification_id = NULL;
978 startup_id = g_getenv ("DESKTOP_STARTUP_ID");
979 if (startup_id && *startup_id != '\0')
983 if (!g_utf8_validate (startup_id, -1, NULL))
984 g_warning ("DESKTOP_STARTUP_ID contains invalid UTF-8");
986 display_x11->startup_notification_id = g_strdup (startup_id);
988 /* Find the launch time from the startup_id, if it's there. Newer spec
989 * states that the startup_id is of the form <unique>_TIME<timestamp>
991 time_str = g_strrstr (startup_id, "_TIME");
992 if (time_str != NULL)
998 /* Skip past the "_TIME" part */
1001 retval = strtoul (time_str, &end, 0);
1002 if (end != time_str && errno == 0)
1003 display_x11->user_time = retval;
1006 /* Clear the environment variable so it won't be inherited by
1007 * child processes and confuse things.
1009 g_unsetenv ("DESKTOP_STARTUP_ID");
1011 /* Set the startup id on the leader window so it
1012 * applies to all windows we create on this display
1014 XChangeProperty (display_x11->xdisplay,
1015 display_x11->leader_window,
1016 gdk_x11_get_xatom_by_name_for_display (display, "_NET_STARTUP_ID"),
1017 gdk_x11_get_xatom_by_name_for_display (display, "UTF8_STRING"), 8,
1019 (guchar *)startup_id, strlen (startup_id));
1024 broadcast_xmessage (GdkDisplay *display,
1025 const char *message_type,
1026 const char *message_type_begin,
1027 const char *message)
1029 Display *xdisplay = GDK_DISPLAY_XDISPLAY (display);
1030 GdkScreen *screen = gdk_display_get_default_screen (display);
1031 GdkWindow *root_window = gdk_screen_get_root_window (screen);
1032 Window xroot_window = GDK_WINDOW_XID (root_window);
1035 Atom type_atom_begin;
1038 if (!G_LIKELY (GDK_DISPLAY_X11 (display)->trusted_client))
1042 XSetWindowAttributes attrs;
1044 attrs.override_redirect = True;
1045 attrs.event_mask = PropertyChangeMask | StructureNotifyMask;
1048 XCreateWindow (xdisplay,
1054 (Visual *)CopyFromParent,
1055 CWOverrideRedirect | CWEventMask,
1059 type_atom = gdk_x11_get_xatom_by_name_for_display (display,
1061 type_atom_begin = gdk_x11_get_xatom_by_name_for_display (display,
1062 message_type_begin);
1065 XClientMessageEvent xclient;
1067 const char *src_end;
1071 memset(&xclient, 0, sizeof (xclient));
1072 xclient.type = ClientMessage;
1073 xclient.message_type = type_atom_begin;
1074 xclient.display =xdisplay;
1075 xclient.window = xwindow;
1079 src_end = message + strlen (message) + 1; /* +1 to include nul byte */
1081 while (src != src_end)
1083 dest = &xclient.data.b[0];
1084 dest_end = dest + 20;
1086 while (dest != dest_end &&
1094 while (dest != dest_end)
1100 XSendEvent (xdisplay,
1104 (XEvent *)&xclient);
1106 xclient.message_type = type_atom;
1110 XDestroyWindow (xdisplay, xwindow);
1115 * gdk_x11_display_broadcast_startup_message:
1116 * @display: a #GdkDisplay
1117 * @message_type: startup notification message type ("new", "change",
1119 * @...: a list of key/value pairs (as strings), terminated by a
1120 * %NULL key. (A %NULL value for a key will cause that key to be
1121 * skipped in the output.)
1123 * Sends a startup notification message of type @message_type to
1126 * This is a convenience function for use by code that implements the
1127 * freedesktop startup notification specification. Applications should
1128 * not normally need to call it directly. See the <ulink
1129 * url="http://standards.freedesktop.org/startup-notification-spec/startup-notification-latest.txt">Startup
1130 * Notification Protocol specification</ulink> for
1131 * definitions of the message types and keys that can be used.
1136 gdk_x11_display_broadcast_startup_message (GdkDisplay *display,
1137 const char *message_type,
1142 const char *key, *value, *p;
1144 message = g_string_new (message_type);
1145 g_string_append_c (message, ':');
1147 va_start (ap, message_type);
1148 while ((key = va_arg (ap, const char *)))
1150 value = va_arg (ap, const char *);
1154 g_string_append_printf (message, " %s=\"", key);
1155 for (p = value; *p; p++)
1162 g_string_append_c (message, '\\');
1166 g_string_append_c (message, *p);
1168 g_string_append_c (message, '\"');
1172 broadcast_xmessage (display,
1173 "_NET_STARTUP_INFO",
1174 "_NET_STARTUP_INFO_BEGIN",
1177 g_string_free (message, TRUE);
1181 * gdk_notify_startup_complete:
1183 * Indicates to the GUI environment that the application has finished
1184 * loading. If the applications opens windows, this function is
1185 * normally called after opening the application's initial set of
1188 * GTK+ will call this function automatically after opening the first
1189 * #GtkWindow unless gtk_window_set_auto_startup_notification() is called
1190 * to disable that feature.
1195 gdk_notify_startup_complete (void)
1197 GdkDisplay *display;
1198 GdkDisplayX11 *display_x11;
1200 display = gdk_display_get_default ();
1204 display_x11 = GDK_DISPLAY_X11 (display);
1206 if (display_x11->startup_notification_id == NULL)
1209 gdk_notify_startup_complete_with_id (display_x11->startup_notification_id);
1213 * gdk_notify_startup_complete_with_id:
1214 * @startup_id: a startup-notification identifier, for which notification
1215 * process should be completed
1217 * Indicates to the GUI environment that the application has finished
1218 * loading, using a given identifier.
1220 * GTK+ will call this function automatically for #GtkWindow with custom
1221 * startup-notification identifier unless
1222 * gtk_window_set_auto_startup_notification() is called to disable
1228 gdk_notify_startup_complete_with_id (const gchar* startup_id)
1230 GdkDisplay *display;
1232 display = gdk_display_get_default ();
1236 gdk_x11_display_broadcast_startup_message (display, "remove",
1242 * gdk_display_supports_selection_notification:
1243 * @display: a #GdkDisplay
1245 * Returns whether #GdkEventOwnerChange events will be
1246 * sent when the owner of a selection changes.
1248 * Return value: whether #GdkEventOwnerChange events will
1254 gdk_display_supports_selection_notification (GdkDisplay *display)
1256 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
1258 return display_x11->have_xfixes;
1262 * gdk_display_request_selection_notification:
1263 * @display: a #GdkDisplay
1264 * @selection: the #GdkAtom naming the selection for which
1265 * ownership change notification is requested
1267 * Request #GdkEventOwnerChange events for ownership changes
1268 * of the selection named by the given atom.
1270 * Return value: whether #GdkEventOwnerChange events will
1276 gdk_display_request_selection_notification (GdkDisplay *display,
1281 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
1284 if (display_x11->have_xfixes)
1286 atom = gdk_x11_atom_to_xatom_for_display (display,
1288 XFixesSelectSelectionInput (display_x11->xdisplay,
1289 display_x11->leader_window,
1291 XFixesSetSelectionOwnerNotifyMask |
1292 XFixesSelectionWindowDestroyNotifyMask |
1293 XFixesSelectionClientCloseNotifyMask);
1302 * gdk_display_supports_clipboard_persistence
1303 * @display: a #GdkDisplay
1305 * Returns whether the speicifed display supports clipboard
1306 * persistance; i.e. if it's possible to store the clipboard data after an
1307 * application has quit. On X11 this checks if a clipboard daemon is
1310 * Returns: %TRUE if the display supports clipboard persistance.
1315 gdk_display_supports_clipboard_persistence (GdkDisplay *display)
1317 Atom clipboard_manager;
1319 /* It might make sense to cache this */
1320 clipboard_manager = gdk_x11_get_xatom_by_name_for_display (display, "CLIPBOARD_MANAGER");
1321 return XGetSelectionOwner (GDK_DISPLAY_X11 (display)->xdisplay, clipboard_manager) != None;
1325 * gdk_display_store_clipboard
1326 * @display: a #GdkDisplay
1327 * @clipboard_window: a #GdkWindow belonging to the clipboard owner
1328 * @time_: a timestamp
1329 * @targets: an array of targets that should be saved, or %NULL
1330 * if all available targets should be saved.
1331 * @n_targets: length of the @targets array
1333 * Issues a request to the clipboard manager to store the
1334 * clipboard data. On X11, this is a special program that works
1335 * according to the freedesktop clipboard specification, available at
1336 * <ulink url="http://www.freedesktop.org/Standards/clipboard-manager-spec">
1337 * http://www.freedesktop.org/Standards/clipboard-manager-spec</ulink>.
1342 gdk_display_store_clipboard (GdkDisplay *display,
1343 GdkWindow *clipboard_window,
1345 const GdkAtom *targets,
1348 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
1349 Atom clipboard_manager, save_targets;
1351 g_return_if_fail (GDK_WINDOW_IS_X11 (clipboard_window));
1353 clipboard_manager = gdk_x11_get_xatom_by_name_for_display (display, "CLIPBOARD_MANAGER");
1354 save_targets = gdk_x11_get_xatom_by_name_for_display (display, "SAVE_TARGETS");
1356 gdk_error_trap_push ();
1358 if (XGetSelectionOwner (display_x11->xdisplay, clipboard_manager) != None)
1360 Atom property_name = None;
1366 property_name = gdk_x11_atom_to_xatom_for_display (display, _gdk_selection_property);
1368 xatoms = g_new (Atom, n_targets);
1369 for (i = 0; i < n_targets; i++)
1370 xatoms[i] = gdk_x11_atom_to_xatom_for_display (display, targets[i]);
1372 XChangeProperty (display_x11->xdisplay, GDK_WINDOW_XID (clipboard_window),
1373 property_name, XA_ATOM,
1374 32, PropModeReplace, (guchar *)xatoms, n_targets);
1379 XConvertSelection (display_x11->xdisplay,
1380 clipboard_manager, save_targets, property_name,
1381 GDK_WINDOW_XID (clipboard_window), time_);
1384 gdk_error_trap_pop ();
1389 * gdk_x11_display_get_user_time:
1390 * @display: a #GdkDisplay
1392 * Returns the timestamp of the last user interaction on
1393 * @display. The timestamp is taken from events caused
1394 * by user interaction such as key presses or pointer
1395 * movements. See gdk_x11_window_set_user_time().
1397 * Returns: the timestamp of the last user interaction
1402 gdk_x11_display_get_user_time (GdkDisplay *display)
1404 return GDK_DISPLAY_X11 (display)->user_time;
1408 * gdk_display_supports_shapes:
1409 * @display: a #GdkDisplay
1411 * Returns %TRUE if gdk_window_shape_combine_mask() can
1412 * be used to create shaped windows on @display.
1414 * Returns: %TRUE if shaped windows are supported
1419 gdk_display_supports_shapes (GdkDisplay *display)
1421 return GDK_DISPLAY_X11 (display)->have_shapes;
1425 * gdk_display_supports_input_shapes:
1426 * @display: a #GdkDisplay
1428 * Returns %TRUE if gdk_window_input_shape_combine_mask() can
1429 * be used to modify the input shape of windows on @display.
1431 * Returns: %TRUE if windows with modified input shape are supported
1436 gdk_display_supports_input_shapes (GdkDisplay *display)
1438 return GDK_DISPLAY_X11 (display)->have_input_shapes;
1443 * gdk_x11_display_get_startup_notification_id:
1444 * @display: a #GdkDisplay
1446 * Gets the startup notification ID for a display.
1448 * Returns: the startup notification ID for @display
1452 G_CONST_RETURN gchar *
1453 gdk_x11_display_get_startup_notification_id (GdkDisplay *display)
1455 return GDK_DISPLAY_X11 (display)->startup_notification_id;
1459 * gdk_display_supports_composite:
1460 * @display: a #GdkDisplay
1462 * Returns %TRUE if gdk_window_set_composited() can be used
1463 * to redirect drawing on the window using compositing.
1465 * Currently this only works on X11 with XComposite and
1466 * XDamage extensions available.
1468 * Returns: %TRUE if windows may be composited.
1473 gdk_display_supports_composite (GdkDisplay *display)
1475 GdkDisplayX11 *x11_display = GDK_DISPLAY_X11 (display);
1477 return x11_display->have_xcomposite &&
1478 x11_display->have_xdamage &&
1479 x11_display->have_xfixes;
1483 #define __GDK_DISPLAY_X11_C__
1484 #include "gdkaliasdef.c"