1 /* GDK - The GIMP Drawing Kit
4 * Copyright 2001 Sun Microsystems Inc.
6 * Erwann Chenede <erwann.chenede@sun.com>
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
18 * You should have received a copy of the GNU Library General Public
19 * License along with this library; if not, write to the
20 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, USA.
32 #include "gdkdisplay.h"
33 #include "gdkdisplay-x11.h"
34 #include "gdkscreen.h"
35 #include "gdkscreen-x11.h"
36 #include "gdkinternals.h"
37 #include "gdkinputprivate.h"
38 #include "xsettings-client.h"
40 #include <X11/Xatom.h>
43 #include <X11/XKBlib.h>
46 static void gdk_display_x11_class_init (GdkDisplayX11Class *class);
47 static void gdk_display_x11_dispose (GObject *object);
48 static void gdk_display_x11_finalize (GObject *object);
51 static void gdk_internal_connection_watch (Display *display,
55 XPointer *watch_data);
56 #endif /* HAVE_X11R6 */
58 static gpointer parent_class = NULL;
60 /* Note that we never *directly* use WM_LOCALE_NAME, WM_PROTOCOLS,
61 * but including them here has the side-effect of getting them
62 * into the internal Xlib cache
64 static const char *const precache_atoms[] = {
78 "_NET_WM_STATE_STICKY",
79 "_NET_WM_STATE_MAXIMIZED_VERT",
80 "_NET_WM_STATE_MAXIMIZED_HORZ",
81 "_NET_WM_STATE_FULLSCREEN",
82 "_NET_WM_WINDOW_TYPE",
83 "_NET_WM_WINDOW_TYPE_NORMAL",
88 _gdk_display_x11_get_type (void)
90 static GType object_type = 0;
94 static const GTypeInfo object_info =
96 sizeof (GdkDisplayX11Class),
98 (GBaseFinalizeFunc) NULL,
99 (GClassInitFunc) gdk_display_x11_class_init,
100 NULL, /* class_finalize */
101 NULL, /* class_data */
102 sizeof (GdkDisplayX11),
104 (GInstanceInitFunc) NULL,
107 object_type = g_type_register_static (GDK_TYPE_DISPLAY,
116 gdk_display_x11_class_init (GdkDisplayX11Class * class)
118 GObjectClass *object_class = G_OBJECT_CLASS (class);
120 object_class->dispose = gdk_display_x11_dispose;
121 object_class->finalize = gdk_display_x11_finalize;
123 parent_class = g_type_class_peek_parent (class);
128 * @display_name: the name of the display to open
129 * @returns: a #GdkDisplay, or %NULL if the display
130 * could not be opened.
137 gdk_display_open (const gchar *display_name)
141 GdkDisplayX11 *display_x11;
145 const char *sm_client_id;
147 XClassHint *class_hint;
151 xdisplay = XOpenDisplay (display_name);
155 display = g_object_new (GDK_TYPE_DISPLAY_X11, NULL);
156 display_x11 = GDK_DISPLAY_X11 (display);
158 display_x11->use_xshm = TRUE;
159 display_x11->xdisplay = xdisplay;
162 /* Set up handlers for Xlib internal connections */
163 XAddConnectionWatch (xdisplay, gdk_internal_connection_watch, NULL);
164 #endif /* HAVE_X11R6 */
166 /* initialize the display's screens */
167 display_x11->screens = g_new (GdkScreen *, ScreenCount (display_x11->xdisplay));
168 for (i = 0; i < ScreenCount (display_x11->xdisplay); i++)
169 display_x11->screens[i] = _gdk_x11_screen_new (display, i);
171 /* We need to initialize events after we have the screen
172 * structures in places
174 for (i = 0; i < ScreenCount (display_x11->xdisplay); i++)
175 _gdk_x11_events_init_screen (display_x11->screens[i]);
177 /*set the default screen */
178 display_x11->default_screen = display_x11->screens[DefaultScreen (display_x11->xdisplay)];
180 attr.window_type = GDK_WINDOW_TOPLEVEL;
181 attr.wclass = GDK_INPUT_OUTPUT;
188 display_x11->leader_gdk_window = gdk_window_new (GDK_SCREEN_X11 (display_x11->default_screen)->root_window,
189 &attr, GDK_WA_X | GDK_WA_Y);
190 display_x11->leader_window = GDK_WINDOW_XID (display_x11->leader_gdk_window);
192 display_x11->leader_window_title_set = FALSE;
194 display_x11->have_render = GDK_UNKNOWN;
196 if (_gdk_synchronize)
197 XSynchronize (display_x11->xdisplay, True);
199 _gdk_x11_precache_atoms (display, precache_atoms, G_N_ELEMENTS (precache_atoms));
201 class_hint = XAllocClassHint();
202 class_hint->res_name = g_get_prgname ();
204 class_hint->res_class = (char *)gdk_get_program_class ();
205 _gdk_get_command_line_args (&argc, &argv);
206 XmbSetWMProperties (display_x11->xdisplay,
207 display_x11->leader_window,
208 NULL, NULL, argv, argc, NULL, NULL,
212 sm_client_id = _gdk_get_sm_client_id ();
214 _gdk_windowing_display_set_sm_client_id (display, sm_client_id);
217 XChangeProperty (display_x11->xdisplay,
218 display_x11->leader_window,
219 gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_PID"),
220 XA_CARDINAL, 32, PropModeReplace, (guchar *) & pid, 1);
222 /* We don't yet know a valid time. */
223 display_x11->user_time = 0;
227 gint xkb_major = XkbMajorVersion;
228 gint xkb_minor = XkbMinorVersion;
229 if (XkbLibraryVersion (&xkb_major, &xkb_minor))
231 xkb_major = XkbMajorVersion;
232 xkb_minor = XkbMinorVersion;
234 if (XkbQueryExtension (display_x11->xdisplay,
235 NULL, &display_x11->xkb_event_type, NULL,
236 &xkb_major, &xkb_minor))
238 Bool detectable_autorepeat_supported;
240 display_x11->use_xkb = TRUE;
242 XkbSelectEvents (display_x11->xdisplay,
244 XkbNewKeyboardNotifyMask | XkbMapNotifyMask | XkbStateNotifyMask,
245 XkbNewKeyboardNotifyMask | XkbMapNotifyMask | XkbStateNotifyMask);
247 XkbSetDetectableAutoRepeat (display_x11->xdisplay,
249 &detectable_autorepeat_supported);
251 GDK_NOTE (MISC, g_message ("Detectable autorepeat %s.",
252 detectable_autorepeat_supported ?
253 "supported" : "not supported"));
255 display_x11->have_xkb_autorepeat = detectable_autorepeat_supported;
261 _gdk_windowing_image_init (display);
262 _gdk_events_init (display);
263 _gdk_input_init (display);
264 _gdk_dnd_init (display);
266 g_signal_emit_by_name (gdk_display_manager_get(),
267 "display_opened", display);
274 * XLib internal connection handling
276 typedef struct _GdkInternalConnection GdkInternalConnection;
278 struct _GdkInternalConnection
286 process_internal_connection (GIOChannel *gioc,
290 GdkInternalConnection *connection = (GdkInternalConnection *)data;
292 GDK_THREADS_ENTER ();
294 XProcessInternalConnection ((Display*)connection->display, connection->fd);
296 GDK_THREADS_LEAVE ();
301 static GdkInternalConnection *
302 gdk_add_connection_handler (Display *display,
305 GIOChannel *io_channel;
306 GdkInternalConnection *connection;
308 connection = g_new (GdkInternalConnection, 1);
311 connection->display = display;
313 io_channel = g_io_channel_unix_new (fd);
315 connection->source = g_io_create_watch (io_channel, G_IO_IN);
316 g_source_set_callback (connection->source,
317 (GSourceFunc)process_internal_connection, connection, NULL);
318 g_source_attach (connection->source, NULL);
320 g_io_channel_unref (io_channel);
326 gdk_remove_connection_handler (GdkInternalConnection *connection)
328 g_source_destroy (connection->source);
333 gdk_internal_connection_watch (Display *display,
337 XPointer *watch_data)
340 *watch_data = (XPointer)gdk_add_connection_handler (display, fd);
342 gdk_remove_connection_handler ((GdkInternalConnection *)*watch_data);
344 #endif /* HAVE_X11R6 */
347 * gdk_display_get_name:
348 * @display: a #GdkDisplay
350 * Gets the name of the display.
352 * Returns: a string representing the display name. This string is owned
353 * by GDK and should not be modified or freed.
357 G_CONST_RETURN gchar *
358 gdk_display_get_name (GdkDisplay * display)
360 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
362 return (gchar *) DisplayString (GDK_DISPLAY_X11 (display)->xdisplay);
366 * gdk_display_get_n_screens:
367 * @display: a #GdkDisplay
369 * Gets the number of screen managed by the @display.
371 * Returns: number of screens.
376 gdk_display_get_n_screens (GdkDisplay * display)
378 g_return_val_if_fail (GDK_IS_DISPLAY (display), 0);
380 return ScreenCount (GDK_DISPLAY_X11 (display)->xdisplay);
384 * gdk_display_get_screen:
385 * @display: a #GdkDisplay
386 * @screen_num: the screen number
388 * Returns a screen object for one of the screens of the display.
390 * Returns: the #GdkScreen object
395 gdk_display_get_screen (GdkDisplay * display, gint screen_num)
397 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
398 g_return_val_if_fail (ScreenCount (GDK_DISPLAY_X11 (display)->xdisplay) > screen_num, NULL);
400 return GDK_DISPLAY_X11 (display)->screens[screen_num];
404 * gdk_display_get_default_screen:
405 * @display: a #GdkDisplay
407 * Get the default #GdkScreen for @display.
409 * Returns: the default #GdkScreen object for @display
414 gdk_display_get_default_screen (GdkDisplay * display)
416 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
418 return GDK_DISPLAY_X11 (display)->default_screen;
422 _gdk_x11_display_is_root_window (GdkDisplay *display,
425 GdkDisplayX11 *display_x11;
428 g_return_val_if_fail (GDK_IS_DISPLAY (display), FALSE);
430 display_x11 = GDK_DISPLAY_X11 (display);
432 for (i = 0; i < ScreenCount (display_x11->xdisplay); i++)
434 if (GDK_SCREEN_XROOTWIN (display_x11->screens[i]) == xroot_window)
441 * gdk_display_pointer_ungrab:
442 * @display: a #GdkDisplay.
443 * @time_: a timestap (e.g. GDK_CURRENT_TIME).
445 * Release any pointer grab.
450 gdk_display_pointer_ungrab (GdkDisplay *display,
455 g_return_if_fail (GDK_IS_DISPLAY (display));
457 xdisplay = GDK_DISPLAY_XDISPLAY (display);
459 _gdk_input_ungrab_pointer (display, time);
460 XUngrabPointer (xdisplay, time);
463 GDK_DISPLAY_X11 (display)->pointer_xgrab_window = NULL;
467 * gdk_display_pointer_is_grabbed:
468 * @display: a #GdkDisplay
470 * Test if the pointer is grabbed.
472 * Returns: %TRUE if an active X pointer grab is in effect
477 gdk_display_pointer_is_grabbed (GdkDisplay * display)
479 g_return_val_if_fail (GDK_IS_DISPLAY (display), TRUE);
481 return (GDK_DISPLAY_X11 (display)->pointer_xgrab_window != NULL);
485 * gdk_display_keyboard_ungrab:
486 * @display: a #GdkDisplay.
487 * @time_: a timestap (e.g #GDK_CURRENT_TIME).
489 * Release any keyboard grab
494 gdk_display_keyboard_ungrab (GdkDisplay *display,
499 g_return_if_fail (GDK_IS_DISPLAY (display));
501 xdisplay = GDK_DISPLAY_XDISPLAY (display);
503 XUngrabKeyboard (xdisplay, time);
506 GDK_DISPLAY_X11 (display)->keyboard_xgrab_window = NULL;
511 * @display: a #GdkDisplay
513 * Emits a short beep on @display
518 gdk_display_beep (GdkDisplay * display)
520 g_return_if_fail (GDK_IS_DISPLAY (display));
522 XBell (GDK_DISPLAY_XDISPLAY (display), 0);
527 * @display: a #GdkDisplay
529 * Flushes any requests queued for the windowing system and waits until all
530 * requests have been handled. This is often used for making sure that the
531 * display is synchronized with the current state of the program. Calling
532 * gdk_display_sync() before gdk_error_trap_pop() makes sure that any errors
533 * generated from earlier requests are handled before the error trap is
536 * This is most useful for X11. On windowing systems where requests are
537 * handled synchronously, this function will do nothing.
542 gdk_display_sync (GdkDisplay * display)
544 g_return_if_fail (GDK_IS_DISPLAY (display));
546 XSync (GDK_DISPLAY_XDISPLAY (display), False);
551 * @display: a #GdkDisplay
553 * Flushes any requests queued for the windowing system; this happens automatically
554 * when the main loop blocks waiting for new events, but if your application
555 * is drawing without returning control to the main loop, you may need
556 * to call this function explicitely. A common case where this function
557 * needs to be called is when an application is executing drawing commands
558 * from a thread other than the thread where the main loop is running.
560 * This is most useful for X11. On windowing systems where requests are
561 * handled synchronously, this function will do nothing.
566 gdk_display_flush (GdkDisplay *display)
568 g_return_if_fail (GDK_IS_DISPLAY (display));
570 if (!display->closed)
571 XFlush (GDK_DISPLAY_XDISPLAY (display));
575 * gdk_display_get_default_group:
576 * @display: a #GdkDisplay
578 * Returns the default group leader window for all toplevel windows
579 * on @display. This window is implicitly created by GDK.
580 * See gdk_window_set_group().
582 * Return value: The default group leader window for @display
586 GdkWindow *gdk_display_get_default_group (GdkDisplay *display)
588 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
590 return GDK_DISPLAY_X11 (display)->leader_gdk_window;
594 * gdk_x11_display_grab:
595 * @display: a #GdkDisplay
597 * Call XGrabServer() on @display.
598 * To ungrab the display again, use gdk_x11_display_ungrab().
600 * gdk_x11_display_grab()/gdk_x11_display_ungrab() calls can be nested.
605 gdk_x11_display_grab (GdkDisplay * display)
607 GdkDisplayX11 *display_x11;
609 g_return_if_fail (GDK_IS_DISPLAY (display));
611 display_x11 = GDK_DISPLAY_X11 (display);
613 if (display_x11->grab_count == 0)
614 XGrabServer (display_x11->xdisplay);
615 display_x11->grab_count++;
619 * gdk_x11_display_ungrab:
620 * @display: a #GdkDisplay
622 * Ungrab @display after it has been grabbed with
623 * gdk_x11_display_grab().
628 gdk_x11_display_ungrab (GdkDisplay * display)
630 GdkDisplayX11 *display_x11;
632 g_return_if_fail (GDK_IS_DISPLAY (display));
634 display_x11 = GDK_DISPLAY_X11 (display);;
635 g_return_if_fail (display_x11->grab_count > 0);
637 display_x11->grab_count--;
638 if (display_x11->grab_count == 0)
640 XUngrabServer (display_x11->xdisplay);
641 XFlush (display_x11->xdisplay);
646 gdk_display_x11_dispose (GObject *object)
648 GdkDisplayX11 *display_x11;
651 display_x11 = GDK_DISPLAY_X11 (object);
653 for (i = 0; i < ScreenCount (display_x11->xdisplay); i++)
654 _gdk_screen_close (display_x11->screens[i]);
656 g_source_destroy (display_x11->event_source);
658 XCloseDisplay (display_x11->xdisplay);
659 display_x11->xdisplay = NULL;
661 G_OBJECT_CLASS (parent_class)->dispose (object);
665 gdk_display_x11_finalize (GObject *object)
667 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (object);
670 /* FIXME need to write GdkKeymap finalize fct
671 g_object_unref (display_x11->keymap);
674 if (display_x11->motif_target_lists)
676 for (i = 0; i < display_x11->motif_n_target_lists; i++)
677 g_list_free (display_x11->motif_target_lists[i]);
678 g_free (display_x11->motif_target_lists);
682 g_hash_table_destroy (display_x11->atom_from_virtual);
683 g_hash_table_destroy (display_x11->atom_to_virtual);
685 XDestroyWindow (display_x11->xdisplay, display_x11->leader_window);
686 /* list of filters for client messages */
687 g_list_free (display_x11->client_filters);
688 /* List of event window extraction functions */
689 g_slist_foreach (display_x11->event_types, (GFunc)g_free, NULL);
690 g_slist_free (display_x11->event_types);
692 g_hash_table_destroy (display_x11->xid_ht);
693 /* input GdkDevice list */
694 /* FIXME need to write finalize fct */
695 for (tmp = display_x11->input_devices; tmp; tmp = tmp->next)
696 g_object_unref (tmp->data);
697 g_list_free (display_x11->input_devices);
698 /* input GdkWindow list */
699 for (tmp = display_x11->input_windows; tmp; tmp = tmp->next)
700 g_object_unref (tmp->data);
701 g_list_free (display_x11->input_windows);
702 /* Free all GdkScreens */
703 for (i = 0; i < ScreenCount (display_x11->xdisplay); i++)
704 g_object_unref (display_x11->screens[i]);
705 g_free (display_x11->screens);
706 g_free (display_x11->startup_notification_id);
708 G_OBJECT_CLASS (parent_class)->finalize (object);
712 * gdk_x11_lookup_xdisplay:
713 * @xdisplay: a pointer to an X Display
715 * Find the #GdkDisplay corresponding to @display, if any exists.
717 * Return value: the #GdkDisplay, if found, otherwise %NULL.
722 gdk_x11_lookup_xdisplay (Display *xdisplay)
726 for (tmp_list = _gdk_displays; tmp_list; tmp_list = tmp_list->next)
728 if (GDK_DISPLAY_XDISPLAY (tmp_list->data) == xdisplay)
729 return tmp_list->data;
736 * _gdk_x11_display_screen_for_xrootwin:
737 * @display: a #Display
738 * @xrootwin: window ID for one of of the screen's of the display.
740 * Given the root window ID of one of the screen's of a #GdkDisplay,
743 * Return value: the #GdkScreen corresponding to @xrootwin, or %NULL.
746 _gdk_x11_display_screen_for_xrootwin (GdkDisplay *display,
751 n_screens = gdk_display_get_n_screens (display);
752 for (i = 0; i < n_screens; i++)
754 GdkScreen *screen = gdk_display_get_screen (display, i);
755 if (GDK_SCREEN_XROOTWIN (screen) == xrootwin)
763 * gdk_x11_display_get_xdisplay:
764 * @display: a #GdkDisplay
765 * @returns: an X display.
767 * Returns the X display of a #GdkDisplay.
772 gdk_x11_display_get_xdisplay (GdkDisplay *display)
774 return GDK_DISPLAY_X11 (display)->xdisplay;
778 _gdk_windowing_set_default_display (GdkDisplay *display)
780 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
781 const gchar *startup_id;
784 gdk_display = GDK_DISPLAY_XDISPLAY (display);
788 g_free (display_x11->startup_notification_id);
789 display_x11->startup_notification_id = NULL;
791 startup_id = g_getenv ("DESKTOP_STARTUP_ID");
792 if (startup_id && *startup_id != '\0')
794 if (!g_utf8_validate (startup_id, -1, NULL))
795 g_warning ("DESKTOP_STARTUP_ID contains invalid UTF-8");
797 display_x11->startup_notification_id = g_strdup (startup_id);
799 /* Clear the environment variable so it won't be inherited by
800 * child processes and confuse things.
802 g_unsetenv ("DESKTOP_STARTUP_ID");
804 /* Set the startup id on the leader window so it
805 * applies to all windows we create on this display
807 XChangeProperty (display_x11->xdisplay,
808 display_x11->leader_window,
809 gdk_x11_get_xatom_by_name_for_display (display, "_NET_STARTUP_ID"),
810 gdk_x11_get_xatom_by_name_for_display (display, "UTF8_STRING"), 8,
812 startup_id, strlen (startup_id));
817 escape_for_xmessage (const char *str)
822 retval = g_string_new (NULL);
832 g_string_append_c (retval, '\\');
836 g_string_append_c (retval, *p);
840 return g_string_free (retval, FALSE);
844 broadcast_xmessage (GdkDisplay *display,
845 const char *message_type,
846 const char *message_type_begin,
849 Display *xdisplay = GDK_DISPLAY_XDISPLAY (display);
850 GdkScreen *screen = gdk_display_get_default_screen (display);
851 GdkWindow *root_window = gdk_screen_get_root_window (screen);
852 Window xroot_window = GDK_WINDOW_XID (root_window);
855 Atom type_atom_begin;
859 XSetWindowAttributes attrs;
861 attrs.override_redirect = True;
862 attrs.event_mask = PropertyChangeMask | StructureNotifyMask;
865 XCreateWindow (xdisplay,
872 CWOverrideRedirect | CWEventMask,
876 type_atom = gdk_x11_get_xatom_by_name_for_display (display,
878 type_atom_begin = gdk_x11_get_xatom_by_name_for_display (display,
888 xevent.xclient.type = ClientMessage;
889 xevent.xclient.message_type = type_atom_begin;
890 xevent.xclient.display =xdisplay;
891 xevent.xclient.window = xwindow;
892 xevent.xclient.format = 8;
895 src_end = message + strlen (message) + 1; /* +1 to include nul byte */
897 while (src != src_end)
899 dest = &xevent.xclient.data.b[0];
900 dest_end = dest + 20;
902 while (dest != dest_end &&
910 while (dest != dest_end)
916 XSendEvent (xdisplay,
922 xevent.xclient.message_type = type_atom;
926 XDestroyWindow (xdisplay, xwindow);
931 * gdk_notify_startup_complete:
933 * Indicates to the GUI environment that the application has finished
934 * loading. If the applications opens windows, this function is
935 * normally called after opening the application's initial set of
938 * GTK+ will call this function automatically after opening the first
939 * #GtkWindow unless gtk_window_set_auto_startup_notification() is called
940 * to disable that feature.
945 gdk_notify_startup_complete (void)
948 GdkDisplayX11 *display_x11;
952 display = gdk_display_get_default ();
956 display_x11 = GDK_DISPLAY_X11 (display);
958 if (display_x11->startup_notification_id == NULL)
961 escaped_id = escape_for_xmessage (display_x11->startup_notification_id);
962 message = g_strdup_printf ("remove: ID=%s", escaped_id);
965 broadcast_xmessage (display,
967 "_NET_STARTUP_INFO_BEGIN",