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.
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"
43 #include <X11/Xatom.h>
46 #include <X11/XKBlib.h>
50 #include <X11/extensions/Xfixes.h>
54 #include <X11/extensions/shape.h>
57 #ifdef HAVE_XCOMPOSITE
58 #include <X11/extensions/Xcomposite.h>
62 #include <X11/extensions/Xdamage.h>
66 #include <X11/extensions/Xrandr.h>
70 static void gdk_display_x11_dispose (GObject *object);
71 static void gdk_display_x11_finalize (GObject *object);
74 static void gdk_internal_connection_watch (Display *display,
78 XPointer *watch_data);
79 #endif /* HAVE_X11R6 */
81 /* Note that we never *directly* use WM_LOCALE_NAME, WM_PROTOCOLS,
82 * but including them here has the side-effect of getting them
83 * into the internal Xlib cache
85 static const char *const precache_atoms[] = {
96 "_NET_CURRENT_DESKTOP",
107 "_NET_WM_STATE_ABOVE",
108 "_NET_WM_STATE_BELOW",
109 "_NET_WM_STATE_FULLSCREEN",
110 "_NET_WM_STATE_MODAL",
111 "_NET_WM_STATE_MAXIMIZED_VERT",
112 "_NET_WM_STATE_MAXIMIZED_HORZ",
113 "_NET_WM_STATE_SKIP_TASKBAR",
114 "_NET_WM_STATE_SKIP_PAGER",
115 "_NET_WM_STATE_STICKY",
116 "_NET_WM_SYNC_REQUEST",
117 "_NET_WM_SYNC_REQUEST_COUNTER",
118 "_NET_WM_WINDOW_TYPE",
119 "_NET_WM_WINDOW_TYPE_NORMAL",
124 G_DEFINE_TYPE (GdkDisplayX11, _gdk_display_x11, GDK_TYPE_DISPLAY)
127 _gdk_display_x11_class_init (GdkDisplayX11Class * class)
129 GObjectClass *object_class = G_OBJECT_CLASS (class);
131 object_class->dispose = gdk_display_x11_dispose;
132 object_class->finalize = gdk_display_x11_finalize;
136 _gdk_display_x11_init (GdkDisplayX11 *display)
142 * @display_name: the name of the display to open
143 * @returns: a #GdkDisplay, or %NULL if the display
144 * could not be opened.
151 gdk_display_open (const gchar *display_name)
155 GdkDisplayX11 *display_x11;
159 const char *sm_client_id;
161 XClassHint *class_hint;
164 #if defined(HAVE_XFIXES) || defined(HAVE_SHAPE_EXT)
169 xdisplay = XOpenDisplay (display_name);
173 display = g_object_new (GDK_TYPE_DISPLAY_X11, NULL);
174 display_x11 = GDK_DISPLAY_X11 (display);
176 display_x11->use_xshm = TRUE;
177 display_x11->xdisplay = xdisplay;
180 /* Set up handlers for Xlib internal connections */
181 XAddConnectionWatch (xdisplay, gdk_internal_connection_watch, NULL);
182 #endif /* HAVE_X11R6 */
184 _gdk_x11_precache_atoms (display, precache_atoms, G_N_ELEMENTS (precache_atoms));
186 /* RandR must be initialized before we initialize the screens */
187 display_x11->have_randr12 = FALSE;
189 if (XRRQueryExtension (display_x11->xdisplay,
190 &display_x11->xrandr_event_base, &ignore))
194 XRRQueryVersion (display_x11->xdisplay, &major, &minor);
196 if ((major == 1 && minor >= 2) || major > 1)
197 display_x11->have_randr12 = TRUE;
201 /* initialize the display's screens */
202 display_x11->screens = g_new (GdkScreen *, ScreenCount (display_x11->xdisplay));
203 for (i = 0; i < ScreenCount (display_x11->xdisplay); i++)
204 display_x11->screens[i] = _gdk_x11_screen_new (display, i);
206 /* We need to initialize events after we have the screen
207 * structures in places
209 for (i = 0; i < ScreenCount (display_x11->xdisplay); i++)
210 _gdk_x11_events_init_screen (display_x11->screens[i]);
212 /*set the default screen */
213 display_x11->default_screen = display_x11->screens[DefaultScreen (display_x11->xdisplay)];
215 attr.window_type = GDK_WINDOW_TOPLEVEL;
216 attr.wclass = GDK_INPUT_OUTPUT;
223 display_x11->leader_gdk_window = gdk_window_new (GDK_SCREEN_X11 (display_x11->default_screen)->root_window,
224 &attr, GDK_WA_X | GDK_WA_Y);
225 (_gdk_x11_window_get_toplevel (display_x11->leader_gdk_window))->is_leader = TRUE;
227 display_x11->leader_window = GDK_WINDOW_XID (display_x11->leader_gdk_window);
229 display_x11->leader_window_title_set = FALSE;
231 display_x11->have_render = GDK_UNKNOWN;
234 if (XFixesQueryExtension (display_x11->xdisplay,
235 &display_x11->xfixes_event_base,
238 display_x11->have_xfixes = TRUE;
240 gdk_x11_register_standard_event_type (display,
241 display_x11->xfixes_event_base,
246 display_x11->have_xfixes = FALSE;
248 #ifdef HAVE_XCOMPOSITE
249 if (XCompositeQueryExtension (display_x11->xdisplay,
254 XCompositeQueryVersion (display_x11->xdisplay, &major, &minor);
256 /* Prior to Composite version 0.4, composited windows clipped their
257 * parents, so you had to use IncludeInferiors to draw to the parent
258 * This isn't useful for our purposes, so require 0.4
260 display_x11->have_xcomposite = major > 0 || (major == 0 && minor >= 4);
264 display_x11->have_xcomposite = FALSE;
267 if (XDamageQueryExtension (display_x11->xdisplay,
268 &display_x11->xdamage_event_base,
271 display_x11->have_xdamage = TRUE;
273 gdk_x11_register_standard_event_type (display,
274 display_x11->xdamage_event_base,
275 XDamageNumberEvents);
279 display_x11->have_xdamage = FALSE;
281 display_x11->have_shapes = FALSE;
282 display_x11->have_input_shapes = FALSE;
283 #ifdef HAVE_SHAPE_EXT
284 if (XShapeQueryExtension (GDK_DISPLAY_XDISPLAY (display), &ignore, &ignore))
286 display_x11->have_shapes = TRUE;
288 if (XShapeQueryVersion (GDK_DISPLAY_XDISPLAY (display), &maj, &min))
289 display_x11->have_input_shapes = (maj == 1 && min >= 1);
294 display_x11->trusted_client = TRUE;
297 int rootx, rooty, winx, winy;
300 gdk_error_trap_push ();
301 XQueryPointer (display_x11->xdisplay,
302 GDK_SCREEN_X11 (display_x11->default_screen)->xroot_window,
303 &root, &child, &rootx, &rooty, &winx, &winy, &xmask);
305 if (G_UNLIKELY (gdk_error_trap_pop () == BadWindow))
307 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));
308 display_x11->trusted_client = FALSE;
312 if (_gdk_synchronize)
313 XSynchronize (display_x11->xdisplay, True);
315 class_hint = XAllocClassHint();
316 class_hint->res_name = g_get_prgname ();
318 class_hint->res_class = (char *)gdk_get_program_class ();
320 /* XmbSetWMProperties sets the RESOURCE_NAME environment variable
321 * from argv[0], so we just synthesize an argument array here.
324 argv[0] = g_get_prgname ();
326 XmbSetWMProperties (display_x11->xdisplay,
327 display_x11->leader_window,
328 NULL, NULL, argv, argc, NULL, NULL,
332 sm_client_id = _gdk_get_sm_client_id ();
334 _gdk_windowing_display_set_sm_client_id (display, sm_client_id);
337 XChangeProperty (display_x11->xdisplay,
338 display_x11->leader_window,
339 gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_PID"),
340 XA_CARDINAL, 32, PropModeReplace, (guchar *) & pid, 1);
342 /* We don't yet know a valid time. */
343 display_x11->user_time = 0;
347 gint xkb_major = XkbMajorVersion;
348 gint xkb_minor = XkbMinorVersion;
349 if (XkbLibraryVersion (&xkb_major, &xkb_minor))
351 xkb_major = XkbMajorVersion;
352 xkb_minor = XkbMinorVersion;
354 if (XkbQueryExtension (display_x11->xdisplay,
355 NULL, &display_x11->xkb_event_type, NULL,
356 &xkb_major, &xkb_minor))
358 Bool detectable_autorepeat_supported;
360 display_x11->use_xkb = TRUE;
362 XkbSelectEvents (display_x11->xdisplay,
364 XkbNewKeyboardNotifyMask | XkbMapNotifyMask | XkbStateNotifyMask,
365 XkbNewKeyboardNotifyMask | XkbMapNotifyMask | XkbStateNotifyMask);
367 /* keep this in sync with _gdk_keymap_state_changed() */
368 XkbSelectEventDetails (display_x11->xdisplay,
369 XkbUseCoreKbd, XkbStateNotify,
370 XkbAllStateComponentsMask,
371 XkbGroupLockMask|XkbModifierLockMask);
373 XkbSetDetectableAutoRepeat (display_x11->xdisplay,
375 &detectable_autorepeat_supported);
377 GDK_NOTE (MISC, g_message ("Detectable autorepeat %s.",
378 detectable_autorepeat_supported ?
379 "supported" : "not supported"));
381 display_x11->have_xkb_autorepeat = detectable_autorepeat_supported;
387 display_x11->use_sync = FALSE;
391 int error_base, event_base;
393 if (XSyncQueryExtension (display_x11->xdisplay,
394 &event_base, &error_base) &&
395 XSyncInitialize (display_x11->xdisplay,
397 display_x11->use_sync = TRUE;
401 _gdk_windowing_image_init (display);
402 _gdk_events_init (display);
403 _gdk_input_init (display);
404 _gdk_dnd_init (display);
406 for (i = 0; i < ScreenCount (display_x11->xdisplay); i++)
407 gdk_display_request_selection_notification (display,
408 GDK_SCREEN_X11 (display_x11->screens[i])->cm_selection_atom);
410 g_signal_emit_by_name (gdk_display_manager_get(),
411 "display_opened", display);
418 * XLib internal connection handling
420 typedef struct _GdkInternalConnection GdkInternalConnection;
422 struct _GdkInternalConnection
430 process_internal_connection (GIOChannel *gioc,
434 GdkInternalConnection *connection = (GdkInternalConnection *)data;
436 GDK_THREADS_ENTER ();
438 XProcessInternalConnection ((Display*)connection->display, connection->fd);
440 GDK_THREADS_LEAVE ();
445 static GdkInternalConnection *
446 gdk_add_connection_handler (Display *display,
449 GIOChannel *io_channel;
450 GdkInternalConnection *connection;
452 connection = g_new (GdkInternalConnection, 1);
455 connection->display = display;
457 io_channel = g_io_channel_unix_new (fd);
459 connection->source = g_io_create_watch (io_channel, G_IO_IN);
460 g_source_set_callback (connection->source,
461 (GSourceFunc)process_internal_connection, connection, NULL);
462 g_source_attach (connection->source, NULL);
464 g_io_channel_unref (io_channel);
470 gdk_remove_connection_handler (GdkInternalConnection *connection)
472 g_source_destroy (connection->source);
477 gdk_internal_connection_watch (Display *display,
481 XPointer *watch_data)
484 *watch_data = (XPointer)gdk_add_connection_handler (display, fd);
486 gdk_remove_connection_handler ((GdkInternalConnection *)*watch_data);
488 #endif /* HAVE_X11R6 */
491 * gdk_display_get_name:
492 * @display: a #GdkDisplay
494 * Gets the name of the display.
496 * Returns: a string representing the display name. This string is owned
497 * by GDK and should not be modified or freed.
501 G_CONST_RETURN gchar *
502 gdk_display_get_name (GdkDisplay *display)
504 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
506 return (gchar *) DisplayString (GDK_DISPLAY_X11 (display)->xdisplay);
510 * gdk_display_get_n_screens:
511 * @display: a #GdkDisplay
513 * Gets the number of screen managed by the @display.
515 * Returns: number of screens.
520 gdk_display_get_n_screens (GdkDisplay *display)
522 g_return_val_if_fail (GDK_IS_DISPLAY (display), 0);
524 return ScreenCount (GDK_DISPLAY_X11 (display)->xdisplay);
528 * gdk_display_get_screen:
529 * @display: a #GdkDisplay
530 * @screen_num: the screen number
532 * Returns a screen object for one of the screens of the display.
534 * Returns: the #GdkScreen object
539 gdk_display_get_screen (GdkDisplay *display,
542 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
543 g_return_val_if_fail (ScreenCount (GDK_DISPLAY_X11 (display)->xdisplay) > screen_num, NULL);
545 return GDK_DISPLAY_X11 (display)->screens[screen_num];
549 * gdk_display_get_default_screen:
550 * @display: a #GdkDisplay
552 * Get the default #GdkScreen for @display.
554 * Returns: the default #GdkScreen object for @display
559 gdk_display_get_default_screen (GdkDisplay *display)
561 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
563 return GDK_DISPLAY_X11 (display)->default_screen;
567 _gdk_x11_display_is_root_window (GdkDisplay *display,
570 GdkDisplayX11 *display_x11;
573 g_return_val_if_fail (GDK_IS_DISPLAY (display), FALSE);
575 display_x11 = GDK_DISPLAY_X11 (display);
577 for (i = 0; i < ScreenCount (display_x11->xdisplay); i++)
579 if (GDK_SCREEN_XROOTWIN (display_x11->screens[i]) == xroot_window)
585 #define XSERVER_TIME_IS_LATER(time1, time2) \
586 ( (( time1 > time2 ) && ( time1 - time2 < ((guint32)-1)/2 )) || \
587 (( time1 < time2 ) && ( time2 - time1 > ((guint32)-1)/2 )) \
591 * gdk_display_pointer_ungrab:
592 * @display: a #GdkDisplay.
593 * @time_: a timestap (e.g. %GDK_CURRENT_TIME).
595 * Release any pointer grab.
600 gdk_display_pointer_ungrab (GdkDisplay *display,
604 GdkDisplayX11 *display_x11;
606 g_return_if_fail (GDK_IS_DISPLAY (display));
608 display_x11 = GDK_DISPLAY_X11 (display);
609 xdisplay = GDK_DISPLAY_XDISPLAY (display);
611 _gdk_input_ungrab_pointer (display, time);
612 XUngrabPointer (xdisplay, time);
615 if (time == GDK_CURRENT_TIME ||
616 display_x11->pointer_xgrab_time == GDK_CURRENT_TIME ||
617 !XSERVER_TIME_IS_LATER (display_x11->pointer_xgrab_time, time))
618 display_x11->pointer_xgrab_window = NULL;
622 * gdk_display_pointer_is_grabbed:
623 * @display: a #GdkDisplay
625 * Test if the pointer is grabbed.
627 * Returns: %TRUE if an active X pointer grab is in effect
632 gdk_display_pointer_is_grabbed (GdkDisplay *display)
634 g_return_val_if_fail (GDK_IS_DISPLAY (display), TRUE);
636 return (GDK_DISPLAY_X11 (display)->pointer_xgrab_window != NULL &&
637 !GDK_DISPLAY_X11 (display)->pointer_xgrab_implicit);
641 * gdk_display_keyboard_ungrab:
642 * @display: a #GdkDisplay.
643 * @time_: a timestap (e.g #GDK_CURRENT_TIME).
645 * Release any keyboard grab
650 gdk_display_keyboard_ungrab (GdkDisplay *display,
654 GdkDisplayX11 *display_x11;
656 g_return_if_fail (GDK_IS_DISPLAY (display));
658 display_x11 = GDK_DISPLAY_X11 (display);
659 xdisplay = GDK_DISPLAY_XDISPLAY (display);
661 XUngrabKeyboard (xdisplay, time);
664 if (time == GDK_CURRENT_TIME ||
665 display_x11->keyboard_xgrab_time == GDK_CURRENT_TIME ||
666 !XSERVER_TIME_IS_LATER (display_x11->keyboard_xgrab_time, time))
667 display_x11->keyboard_xgrab_window = NULL;
672 * @display: a #GdkDisplay
674 * Emits a short beep on @display
679 gdk_display_beep (GdkDisplay *display)
681 g_return_if_fail (GDK_IS_DISPLAY (display));
683 XBell (GDK_DISPLAY_XDISPLAY (display), 0);
688 * @display: a #GdkDisplay
690 * Flushes any requests queued for the windowing system and waits until all
691 * requests have been handled. This is often used for making sure that the
692 * display is synchronized with the current state of the program. Calling
693 * gdk_display_sync() before gdk_error_trap_pop() makes sure that any errors
694 * generated from earlier requests are handled before the error trap is
697 * This is most useful for X11. On windowing systems where requests are
698 * handled synchronously, this function will do nothing.
703 gdk_display_sync (GdkDisplay *display)
705 g_return_if_fail (GDK_IS_DISPLAY (display));
707 XSync (GDK_DISPLAY_XDISPLAY (display), False);
712 * @display: a #GdkDisplay
714 * Flushes any requests queued for the windowing system; this happens automatically
715 * when the main loop blocks waiting for new events, but if your application
716 * is drawing without returning control to the main loop, you may need
717 * to call this function explicitely. A common case where this function
718 * needs to be called is when an application is executing drawing commands
719 * from a thread other than the thread where the main loop is running.
721 * This is most useful for X11. On windowing systems where requests are
722 * handled synchronously, this function will do nothing.
727 gdk_display_flush (GdkDisplay *display)
729 g_return_if_fail (GDK_IS_DISPLAY (display));
731 if (!display->closed)
732 XFlush (GDK_DISPLAY_XDISPLAY (display));
736 * gdk_display_get_default_group:
737 * @display: a #GdkDisplay
739 * Returns the default group leader window for all toplevel windows
740 * on @display. This window is implicitly created by GDK.
741 * See gdk_window_set_group().
743 * Return value: The default group leader window for @display
748 gdk_display_get_default_group (GdkDisplay *display)
750 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
752 return GDK_DISPLAY_X11 (display)->leader_gdk_window;
756 * gdk_x11_display_grab:
757 * @display: a #GdkDisplay
759 * Call XGrabServer() on @display.
760 * To ungrab the display again, use gdk_x11_display_ungrab().
762 * gdk_x11_display_grab()/gdk_x11_display_ungrab() calls can be nested.
767 gdk_x11_display_grab (GdkDisplay *display)
769 GdkDisplayX11 *display_x11;
771 g_return_if_fail (GDK_IS_DISPLAY (display));
773 display_x11 = GDK_DISPLAY_X11 (display);
775 if (display_x11->grab_count == 0)
776 XGrabServer (display_x11->xdisplay);
777 display_x11->grab_count++;
781 * gdk_x11_display_ungrab:
782 * @display: a #GdkDisplay
784 * Ungrab @display after it has been grabbed with
785 * gdk_x11_display_grab().
790 gdk_x11_display_ungrab (GdkDisplay *display)
792 GdkDisplayX11 *display_x11;
794 g_return_if_fail (GDK_IS_DISPLAY (display));
796 display_x11 = GDK_DISPLAY_X11 (display);;
797 g_return_if_fail (display_x11->grab_count > 0);
799 display_x11->grab_count--;
800 if (display_x11->grab_count == 0)
802 XUngrabServer (display_x11->xdisplay);
803 XFlush (display_x11->xdisplay);
808 gdk_display_x11_dispose (GObject *object)
810 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (object);
813 g_list_foreach (display_x11->input_devices, (GFunc) g_object_run_dispose, NULL);
815 for (i = 0; i < ScreenCount (display_x11->xdisplay); i++)
816 _gdk_screen_close (display_x11->screens[i]);
818 _gdk_events_uninit (GDK_DISPLAY_OBJECT (object));
820 G_OBJECT_CLASS (_gdk_display_x11_parent_class)->dispose (object);
824 gdk_display_x11_finalize (GObject *object)
826 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (object);
830 if (display_x11->keymap)
831 g_object_unref (display_x11->keymap);
834 if (display_x11->motif_target_lists)
836 for (i = 0; i < display_x11->motif_n_target_lists; i++)
837 g_list_free (display_x11->motif_target_lists[i]);
838 g_free (display_x11->motif_target_lists);
841 _gdk_x11_cursor_display_finalize (GDK_DISPLAY_OBJECT(display_x11));
844 g_hash_table_destroy (display_x11->atom_from_virtual);
845 g_hash_table_destroy (display_x11->atom_to_virtual);
848 XDestroyWindow (display_x11->xdisplay, display_x11->leader_window);
850 /* list of filters for client messages */
851 g_list_foreach (display_x11->client_filters, (GFunc) g_free, NULL);
852 g_list_free (display_x11->client_filters);
854 /* List of event window extraction functions */
855 g_slist_foreach (display_x11->event_types, (GFunc)g_free, NULL);
856 g_slist_free (display_x11->event_types);
858 /* input GdkDevice list */
859 g_list_foreach (display_x11->input_devices, (GFunc) g_object_unref, NULL);
860 g_list_free (display_x11->input_devices);
862 /* input GdkWindow list */
863 g_list_foreach (display_x11->input_windows, (GFunc) g_free, NULL);
864 g_list_free (display_x11->input_windows);
866 /* Free all GdkScreens */
867 for (i = 0; i < ScreenCount (display_x11->xdisplay); i++)
868 g_object_unref (display_x11->screens[i]);
869 g_free (display_x11->screens);
871 g_free (display_x11->startup_notification_id);
874 g_hash_table_destroy (display_x11->xid_ht);
876 XCloseDisplay (display_x11->xdisplay);
878 G_OBJECT_CLASS (_gdk_display_x11_parent_class)->finalize (object);
882 * gdk_x11_lookup_xdisplay:
883 * @xdisplay: a pointer to an X Display
885 * Find the #GdkDisplay corresponding to @display, if any exists.
887 * Return value: the #GdkDisplay, if found, otherwise %NULL.
892 gdk_x11_lookup_xdisplay (Display *xdisplay)
896 for (tmp_list = _gdk_displays; tmp_list; tmp_list = tmp_list->next)
898 if (GDK_DISPLAY_XDISPLAY (tmp_list->data) == xdisplay)
899 return tmp_list->data;
906 * _gdk_x11_display_screen_for_xrootwin:
907 * @display: a #GdkDisplay
908 * @xrootwin: window ID for one of of the screen's of the display.
910 * Given the root window ID of one of the screen's of a #GdkDisplay,
913 * Return value: the #GdkScreen corresponding to @xrootwin, or %NULL.
916 _gdk_x11_display_screen_for_xrootwin (GdkDisplay *display,
921 for (i = 0; i < ScreenCount (GDK_DISPLAY_X11 (display)->xdisplay); i++)
923 GdkScreen *screen = gdk_display_get_screen (display, i);
924 if (GDK_SCREEN_XROOTWIN (screen) == xrootwin)
932 * gdk_x11_display_get_xdisplay:
933 * @display: a #GdkDisplay
934 * @returns: an X display.
936 * Returns the X display of a #GdkDisplay.
941 gdk_x11_display_get_xdisplay (GdkDisplay *display)
943 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
944 return GDK_DISPLAY_X11 (display)->xdisplay;
948 _gdk_windowing_set_default_display (GdkDisplay *display)
950 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
951 const gchar *startup_id;
959 gdk_display = GDK_DISPLAY_XDISPLAY (display);
961 g_free (display_x11->startup_notification_id);
962 display_x11->startup_notification_id = NULL;
964 startup_id = g_getenv ("DESKTOP_STARTUP_ID");
965 if (startup_id && *startup_id != '\0')
969 if (!g_utf8_validate (startup_id, -1, NULL))
970 g_warning ("DESKTOP_STARTUP_ID contains invalid UTF-8");
972 display_x11->startup_notification_id = g_strdup (startup_id);
974 /* Find the launch time from the startup_id, if it's there. Newer spec
975 * states that the startup_id is of the form <unique>_TIME<timestamp>
977 time_str = g_strrstr (startup_id, "_TIME");
978 if (time_str != NULL)
984 /* Skip past the "_TIME" part */
987 retval = strtoul (time_str, &end, 0);
988 if (end != time_str && errno == 0)
989 display_x11->user_time = retval;
992 /* Clear the environment variable so it won't be inherited by
993 * child processes and confuse things.
995 g_unsetenv ("DESKTOP_STARTUP_ID");
997 /* Set the startup id on the leader window so it
998 * applies to all windows we create on this display
1000 XChangeProperty (display_x11->xdisplay,
1001 display_x11->leader_window,
1002 gdk_x11_get_xatom_by_name_for_display (display, "_NET_STARTUP_ID"),
1003 gdk_x11_get_xatom_by_name_for_display (display, "UTF8_STRING"), 8,
1005 (guchar *)startup_id, strlen (startup_id));
1010 broadcast_xmessage (GdkDisplay *display,
1011 const char *message_type,
1012 const char *message_type_begin,
1013 const char *message)
1015 Display *xdisplay = GDK_DISPLAY_XDISPLAY (display);
1016 GdkScreen *screen = gdk_display_get_default_screen (display);
1017 GdkWindow *root_window = gdk_screen_get_root_window (screen);
1018 Window xroot_window = GDK_WINDOW_XID (root_window);
1021 Atom type_atom_begin;
1024 if (!G_LIKELY (GDK_DISPLAY_X11 (display)->trusted_client))
1028 XSetWindowAttributes attrs;
1030 attrs.override_redirect = True;
1031 attrs.event_mask = PropertyChangeMask | StructureNotifyMask;
1034 XCreateWindow (xdisplay,
1040 (Visual *)CopyFromParent,
1041 CWOverrideRedirect | CWEventMask,
1045 type_atom = gdk_x11_get_xatom_by_name_for_display (display,
1047 type_atom_begin = gdk_x11_get_xatom_by_name_for_display (display,
1048 message_type_begin);
1051 XClientMessageEvent xclient;
1053 const char *src_end;
1057 memset(&xclient, 0, sizeof (xclient));
1058 xclient.type = ClientMessage;
1059 xclient.message_type = type_atom_begin;
1060 xclient.display =xdisplay;
1061 xclient.window = xwindow;
1065 src_end = message + strlen (message) + 1; /* +1 to include nul byte */
1067 while (src != src_end)
1069 dest = &xclient.data.b[0];
1070 dest_end = dest + 20;
1072 while (dest != dest_end &&
1080 while (dest != dest_end)
1086 XSendEvent (xdisplay,
1090 (XEvent *)&xclient);
1092 xclient.message_type = type_atom;
1096 XDestroyWindow (xdisplay, xwindow);
1101 * gdk_x11_display_broadcast_startup_message:
1102 * @display: a #GdkDisplay
1103 * @message_type: startup notification message type ("new", "change",
1105 * @...: a list of key/value pairs (as strings), terminated by a
1106 * %NULL key. (A %NULL value for a key will cause that key to be
1107 * skipped in the output.)
1109 * Sends a startup notification message of type @message_type to
1112 * This is a convenience function for use by code that implements the
1113 * freedesktop startup notification specification. Applications should
1114 * not normally need to call it directly. See the <ulink
1115 * url="http://standards.freedesktop.org/startup-notification-spec/startup-notification-latest.txt">Startup
1116 * Notification Protocol specification</ulink> for
1117 * definitions of the message types and keys that can be used.
1122 gdk_x11_display_broadcast_startup_message (GdkDisplay *display,
1123 const char *message_type,
1128 const char *key, *value, *p;
1130 message = g_string_new (message_type);
1131 g_string_append_c (message, ':');
1133 va_start (ap, message_type);
1134 while ((key = va_arg (ap, const char *)))
1136 value = va_arg (ap, const char *);
1140 g_string_append_printf (message, " %s=\"", key);
1141 for (p = value; *p; p++)
1148 g_string_append_c (message, '\\');
1152 g_string_append_c (message, *p);
1154 g_string_append_c (message, '\"');
1158 broadcast_xmessage (display,
1159 "_NET_STARTUP_INFO",
1160 "_NET_STARTUP_INFO_BEGIN",
1163 g_string_free (message, TRUE);
1167 * gdk_notify_startup_complete:
1169 * Indicates to the GUI environment that the application has finished
1170 * loading. If the applications opens windows, this function is
1171 * normally called after opening the application's initial set of
1174 * GTK+ will call this function automatically after opening the first
1175 * #GtkWindow unless gtk_window_set_auto_startup_notification() is called
1176 * to disable that feature.
1181 gdk_notify_startup_complete (void)
1183 GdkDisplay *display;
1184 GdkDisplayX11 *display_x11;
1186 display = gdk_display_get_default ();
1190 display_x11 = GDK_DISPLAY_X11 (display);
1192 if (display_x11->startup_notification_id == NULL)
1195 gdk_notify_startup_complete_with_id (display_x11->startup_notification_id);
1199 * gdk_notify_startup_complete_with_id:
1200 * @startup_id: a startup-notification identifier, for which notification
1201 * process should be completed
1203 * Indicates to the GUI environment that the application has finished
1204 * loading, using a given identifier.
1206 * GTK+ will call this function automatically for #GtkWindow with custom
1207 * startup-notification identifier unless
1208 * gtk_window_set_auto_startup_notification() is called to disable
1214 gdk_notify_startup_complete_with_id (const gchar* startup_id)
1216 GdkDisplay *display;
1218 display = gdk_display_get_default ();
1222 gdk_x11_display_broadcast_startup_message (display, "remove",
1228 * gdk_display_supports_selection_notification:
1229 * @display: a #GdkDisplay
1231 * Returns whether #GdkEventOwnerChange events will be
1232 * sent when the owner of a selection changes.
1234 * Return value: whether #GdkEventOwnerChange events will
1240 gdk_display_supports_selection_notification (GdkDisplay *display)
1242 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
1244 return display_x11->have_xfixes;
1248 * gdk_display_request_selection_notification:
1249 * @display: a #GdkDisplay
1250 * @selection: the #GdkAtom naming the selection for which
1251 * ownership change notification is requested
1253 * Request #GdkEventOwnerChange events for ownership changes
1254 * of the selection named by the given atom.
1256 * Return value: whether #GdkEventOwnerChange events will
1262 gdk_display_request_selection_notification (GdkDisplay *display,
1267 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
1270 if (display_x11->have_xfixes)
1272 atom = gdk_x11_atom_to_xatom_for_display (display,
1274 XFixesSelectSelectionInput (display_x11->xdisplay,
1275 display_x11->leader_window,
1277 XFixesSetSelectionOwnerNotifyMask |
1278 XFixesSelectionWindowDestroyNotifyMask |
1279 XFixesSelectionClientCloseNotifyMask);
1288 * gdk_display_supports_clipboard_persistence
1289 * @display: a #GdkDisplay
1291 * Returns whether the speicifed display supports clipboard
1292 * persistance; i.e. if it's possible to store the clipboard data after an
1293 * application has quit. On X11 this checks if a clipboard daemon is
1296 * Returns: %TRUE if the display supports clipboard persistance.
1301 gdk_display_supports_clipboard_persistence (GdkDisplay *display)
1303 Atom clipboard_manager;
1305 /* It might make sense to cache this */
1306 clipboard_manager = gdk_x11_get_xatom_by_name_for_display (display, "CLIPBOARD_MANAGER");
1307 return XGetSelectionOwner (GDK_DISPLAY_X11 (display)->xdisplay, clipboard_manager) != None;
1311 * gdk_display_store_clipboard
1312 * @display: a #GdkDisplay
1313 * @clipboard_window: a #GdkWindow belonging to the clipboard owner
1314 * @time_: a timestamp
1315 * @targets: an array of targets that should be saved, or %NULL
1316 * if all available targets should be saved.
1317 * @n_targets: length of the @targets array
1319 * Issues a request to the clipboard manager to store the
1320 * clipboard data. On X11, this is a special program that works
1321 * according to the freedesktop clipboard specification, available at
1322 * <ulink url="http://www.freedesktop.org/Standards/clipboard-manager-spec">
1323 * http://www.freedesktop.org/Standards/clipboard-manager-spec</ulink>.
1328 gdk_display_store_clipboard (GdkDisplay *display,
1329 GdkWindow *clipboard_window,
1331 const GdkAtom *targets,
1334 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
1335 Atom clipboard_manager, save_targets;
1337 clipboard_manager = gdk_x11_get_xatom_by_name_for_display (display, "CLIPBOARD_MANAGER");
1338 save_targets = gdk_x11_get_xatom_by_name_for_display (display, "SAVE_TARGETS");
1340 gdk_error_trap_push ();
1342 if (XGetSelectionOwner (display_x11->xdisplay, clipboard_manager) != None)
1344 Atom property_name = None;
1350 property_name = gdk_x11_atom_to_xatom_for_display (display, _gdk_selection_property);
1352 xatoms = g_new (Atom, n_targets);
1353 for (i = 0; i < n_targets; i++)
1354 xatoms[i] = gdk_x11_atom_to_xatom_for_display (display, targets[i]);
1356 XChangeProperty (display_x11->xdisplay, GDK_WINDOW_XID (clipboard_window),
1357 property_name, XA_ATOM,
1358 32, PropModeReplace, (guchar *)xatoms, n_targets);
1363 XConvertSelection (display_x11->xdisplay,
1364 clipboard_manager, save_targets, property_name,
1365 GDK_WINDOW_XID (clipboard_window), time_);
1368 gdk_error_trap_pop ();
1373 * gdk_x11_display_get_user_time:
1374 * @display: a #GdkDisplay
1376 * Returns the timestamp of the last user interaction on
1377 * @display. The timestamp is taken from events caused
1378 * by user interaction such as key presses or pointer
1379 * movements. See gdk_x11_window_set_user_time().
1381 * Returns: the timestamp of the last user interaction
1386 gdk_x11_display_get_user_time (GdkDisplay *display)
1388 return GDK_DISPLAY_X11 (display)->user_time;
1392 * gdk_display_supports_shapes:
1393 * @display: a #GdkDisplay
1395 * Returns %TRUE if gdk_window_shape_combine_mask() can
1396 * be used to create shaped windows on @display.
1398 * Returns: %TRUE if shaped windows are supported
1403 gdk_display_supports_shapes (GdkDisplay *display)
1405 return GDK_DISPLAY_X11 (display)->have_shapes;
1409 * gdk_display_supports_input_shapes:
1410 * @display: a #GdkDisplay
1412 * Returns %TRUE if gdk_window_input_shape_combine_mask() can
1413 * be used to modify the input shape of windows on @display.
1415 * Returns: %TRUE if windows with modified input shape are supported
1420 gdk_display_supports_input_shapes (GdkDisplay *display)
1422 return GDK_DISPLAY_X11 (display)->have_input_shapes;
1427 * gdk_x11_display_get_startup_notification_id:
1428 * @display: a #GdkDisplay
1430 * Gets the startup notification ID for a display.
1432 * Returns: the startup notification ID for @display
1436 G_CONST_RETURN gchar *
1437 gdk_x11_display_get_startup_notification_id (GdkDisplay *display)
1439 return GDK_DISPLAY_X11 (display)->startup_notification_id;
1443 * gdk_display_supports_composite:
1444 * @display: a #GdkDisplay
1446 * Returns %TRUE if gdk_window_set_composited() can be used
1447 * to redirect drawing on the window using compositing.
1449 * Currently this only works on X11 with XComposite and
1450 * XDamage extensions available.
1452 * Returns: %TRUE if windows may be composited.
1457 gdk_display_supports_composite (GdkDisplay *display)
1459 GdkDisplayX11 *x11_display = GDK_DISPLAY_X11 (display);
1461 return x11_display->have_xcomposite &&
1462 x11_display->have_xdamage &&
1463 x11_display->have_xfixes;
1467 #define __GDK_DISPLAY_X11_C__
1468 #include "gdkaliasdef.c"