2 * Copyright © 2010 Codethink Limited
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the licence, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
19 * Author: Ryan Lortie <desrt@desrt.ca>
24 #include "gtkapplication.h"
32 #include "gtkapplicationprivate.h"
33 #include "gtkmarshalers.h"
35 #include "gtkaccelmapprivate.h"
36 #include "gactionmuxer.h"
39 #ifdef GDK_WINDOWING_QUARTZ
40 #include "gtkquartz-menu.h"
41 #import <Cocoa/Cocoa.h>
42 #include <Carbon/Carbon.h>
43 #include <CoreServices/CoreServices.h>
47 #ifdef GDK_WINDOWING_X11
48 #include <gdk/x11/gdkx.h>
52 * SECTION:gtkapplication
53 * @title: GtkApplication
54 * @short_description: Application class
56 * #GtkApplication is a class that handles many important aspects
57 * of a GTK+ application in a convenient fashion, without enforcing
58 * a one-size-fits-all application model.
60 * Currently, GtkApplication handles GTK+ initialization, application
61 * uniqueness, session management, provides some basic scriptability and
62 * desktop shell integration by exporting actions and menus and manages a
63 * list of toplevel windows whose life-cycle is automatically tied to the
64 * life-cycle of your application.
66 * While GtkApplication works fine with plain #GtkWindows, it is recommended
67 * to use it together with #GtkApplicationWindow.
69 * When GDK threads are enabled, GtkApplication will acquire the GDK
70 * lock when invoking actions that arrive from other processes. The GDK
71 * lock is not touched for local action invocations. In order to have
72 * actions invoked in a predictable context it is therefore recommended
73 * that the GDK lock be held while invoking actions locally with
74 * g_action_group_activate_action(). The same applies to actions
75 * associated with #GtkApplicationWindow and to the 'activate' and
76 * 'open' #GApplication methods.
78 * To set an application menu on a GtkApplication, use
79 * gtk_application_set_app_menu(). The #GMenuModel that this function
80 * expects is usually constructed using #GtkBuilder, as seen in the
81 * following example. To set a menubar that will be automatically picked
82 * up by #GApplicationWindows, use gtk_application_set_menubar(). GTK+
83 * makes these menus appear as expected, depending on the platform
84 * the application is running on.
86 * <figure label="Menu integration in OS X">
87 * <graphic fileref="bloatpad-osx.png" format="PNG"/>
90 * <figure label="Menu integration in GNOME">
91 * <graphic fileref="bloatpad-gnome.png" format="PNG"/>
94 * <figure label="Menu integration in Xfce">
95 * <graphic fileref="bloatpad-xfce.png" format="PNG"/>
98 * <example id="gtkapplication"><title>A simple application</title>
100 * <xi:include xmlns:xi="http://www.w3.org/2001/XInclude" parse="text" href="../../../../examples/bloatpad.c">
101 * <xi:fallback>FIXME: MISSING XINCLUDE CONTENT</xi:fallback>
106 * GtkApplication optionally registers with a session manager
107 * of the users session (if you set the #GtkApplication::register-session
108 * property) and offers various functionality related to the session
111 * An application can be informed when the session is about to end
112 * by connecting to the #GtkApplication::quit signal.
114 * An application can request the session to be ended by calling
115 * gtk_application_end_session().
117 * An application can block various ways to end the session with
118 * the gtk_application_inhibit() function. Typical use cases for
119 * this kind of inhibiting are long-running, uninterruptible operations,
120 * such as burning a CD or performing a disk backup. The session
121 * manager may not honor the inhibitor, but it can be expected to
122 * inform the user about the negative consequences of ending the
123 * session while inhibitors are present.
133 static guint gtk_application_signals[LAST_SIGNAL];
137 PROP_REGISTER_SESSION
140 G_DEFINE_TYPE (GtkApplication, gtk_application, G_TYPE_APPLICATION)
142 struct _GtkApplicationPrivate
146 gboolean register_session;
148 #ifdef GDK_WINDOWING_X11
149 GDBusConnection *session_bus;
150 gchar *window_prefix;
153 GDBusProxy *sm_proxy;
154 GDBusProxy *client_proxy;
159 #ifdef GDK_WINDOWING_QUARTZ
163 GHashTable *inhibitors;
166 AppleEvent quit_event, quit_reply;
171 #ifdef GDK_WINDOWING_X11
173 gtk_application_window_added_x11 (GtkApplication *application,
176 if (application->priv->session_bus == NULL)
179 if (GTK_IS_APPLICATION_WINDOW (window))
181 GtkApplicationWindow *app_window = GTK_APPLICATION_WINDOW (window);
184 /* GtkApplicationWindow associates with us when it is first created,
185 * so surely it's not realized yet...
187 g_assert (!gtk_widget_get_realized (GTK_WIDGET (window)));
194 window_id = application->priv->next_id++;
195 window_path = g_strdup_printf ("%s%d", application->priv->window_prefix, window_id);
196 success = gtk_application_window_publish (app_window, application->priv->session_bus, window_path);
197 g_free (window_path);
204 gtk_application_window_removed_x11 (GtkApplication *application,
207 if (application->priv->session_bus == NULL)
210 if (GTK_IS_APPLICATION_WINDOW (window))
211 gtk_application_window_unpublish (GTK_APPLICATION_WINDOW (window));
215 window_prefix_from_appid (const gchar *appid)
217 gchar *appid_path, *iter;
219 appid_path = g_strconcat ("/", appid, "/windows/", NULL);
220 for (iter = appid_path; *iter; iter++)
232 static void gtk_application_startup_session_dbus (GtkApplication *app);
235 gtk_application_startup_x11 (GtkApplication *application)
237 const gchar *application_id;
239 application_id = g_application_get_application_id (G_APPLICATION (application));
240 application->priv->session_bus = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
241 application->priv->window_prefix = window_prefix_from_appid (application_id);
243 gtk_application_startup_session_dbus (GTK_APPLICATION (application));
247 gtk_application_shutdown_x11 (GtkApplication *application)
249 g_free (application->priv->window_prefix);
250 application->priv->window_prefix = NULL;
251 g_clear_object (&application->priv->session_bus);
253 g_clear_object (&application->priv->sm_proxy);
254 g_clear_object (&application->priv->client_proxy);
255 g_free (application->priv->app_id);
256 g_free (application->priv->client_path);
260 #ifdef GDK_WINDOWING_QUARTZ
262 gtk_application_menu_changed_quartz (GObject *object,
266 GtkApplication *application = GTK_APPLICATION (object);
269 combined = g_menu_new ();
270 g_menu_append_submenu (combined, "Application", g_application_get_app_menu (application));
271 g_menu_append_section (combined, NULL, gtk_application_get_menubar (application));
273 gtk_quartz_set_main_menu (G_MENU_MODEL (combined), G_ACTION_OBSERVABLE (application->priv->muxer));
276 static void gtk_application_startup_session_quartz (GtkApplication *app);
279 gtk_application_startup_quartz (GtkApplication *application)
281 [NSApp finishLaunching];
283 application->priv->muxer = g_action_muxer_new ();
284 g_action_muxer_insert (application->priv->muxer, "app", G_ACTION_GROUP (application));
286 g_signal_connect (application, "notify::app-menu", G_CALLBACK (gtk_application_menu_changed_quartz), NULL);
287 g_signal_connect (application, "notify::menubar", G_CALLBACK (gtk_application_menu_changed_quartz), NULL);
288 gtk_application_menu_changed_quartz (G_OBJECT (application), NULL, NULL);
290 gtk_application_startup_session_quartz (application);
294 gtk_application_shutdown_quartz (GtkApplication *application)
296 g_signal_handlers_disconnect_by_func (application, gtk_application_menu_changed_quartz, NULL);
298 g_object_unref (application->priv->muxer);
299 application->priv->muxer = NULL;
301 g_hash_table_unref (application->priv->inhibitors);
305 gtk_application_focus_changed (GtkApplication *application,
308 if (G_IS_ACTION_GROUP (window))
309 g_action_muxer_insert (application->priv->muxer, "win", G_ACTION_GROUP (window));
311 g_action_muxer_remove (application->priv->muxer, "win");
316 gtk_application_focus_in_event_cb (GtkWindow *window,
317 GdkEventFocus *event,
318 GtkApplication *application)
320 GtkApplicationPrivate *priv = application->priv;
323 /* Keep the window list sorted by most-recently-focused. */
324 link = g_list_find (priv->windows, window);
325 if (link != NULL && link != priv->windows)
327 priv->windows = g_list_remove_link (priv->windows, link);
328 priv->windows = g_list_concat (link, priv->windows);
331 #ifdef GDK_WINDOWING_QUARTZ
332 gtk_application_focus_changed (application, window);
339 gtk_application_startup (GApplication *application)
341 G_APPLICATION_CLASS (gtk_application_parent_class)
342 ->startup (application);
346 #ifdef GDK_WINDOWING_X11
347 gtk_application_startup_x11 (GTK_APPLICATION (application));
350 #ifdef GDK_WINDOWING_QUARTZ
351 gtk_application_startup_quartz (GTK_APPLICATION (application));
356 gtk_application_shutdown (GApplication *application)
358 #ifdef GDK_WINDOWING_X11
359 gtk_application_shutdown_x11 (GTK_APPLICATION (application));
362 #ifdef GDK_WINDOWING_QUARTZ
363 gtk_application_shutdown_quartz (GTK_APPLICATION (application));
366 G_APPLICATION_CLASS (gtk_application_parent_class)
367 ->shutdown (application);
371 gtk_application_add_platform_data (GApplication *application,
372 GVariantBuilder *builder)
374 const gchar *startup_id;
376 startup_id = getenv ("DESKTOP_STARTUP_ID");
378 if (startup_id && g_utf8_validate (startup_id, -1, NULL))
379 g_variant_builder_add (builder, "{sv}", "desktop-startup-id",
380 g_variant_new_string (startup_id));
384 gtk_application_before_emit (GApplication *application,
385 GVariant *platform_data)
391 gdk_threads_enter ();
393 g_variant_iter_init (&iter, platform_data);
394 while (g_variant_iter_loop (&iter, "{&sv}", &key, &value))
396 #ifdef GDK_WINDOWING_X11
397 if (strcmp (key, "desktop-startup-id") == 0)
402 display = gdk_display_get_default ();
403 id = g_variant_get_string (value, NULL);
404 if (GDK_IS_X11_DISPLAY (display))
405 gdk_x11_display_set_startup_notification_id (display, id);
412 gtk_application_after_emit (GApplication *application,
413 GVariant *platform_data)
415 gdk_notify_startup_complete ();
417 gdk_threads_leave ();
421 gtk_application_init (GtkApplication *application)
423 application->priv = G_TYPE_INSTANCE_GET_PRIVATE (application,
424 GTK_TYPE_APPLICATION,
425 GtkApplicationPrivate);
429 gtk_application_window_added (GtkApplication *application,
432 GtkApplicationPrivate *priv = application->priv;
434 priv->windows = g_list_prepend (priv->windows, window);
435 gtk_window_set_application (window, application);
436 g_application_hold (G_APPLICATION (application));
438 g_signal_connect (window, "focus-in-event",
439 G_CALLBACK (gtk_application_focus_in_event_cb),
442 #ifdef GDK_WINDOWING_X11
443 gtk_application_window_added_x11 (application, window);
448 gtk_application_window_removed (GtkApplication *application,
451 GtkApplicationPrivate *priv = application->priv;
453 #ifdef GDK_WINDOWING_X11
454 gtk_application_window_removed_x11 (application, window);
457 g_signal_handlers_disconnect_by_func (window,
458 gtk_application_focus_in_event_cb,
461 g_application_release (G_APPLICATION (application));
462 priv->windows = g_list_remove (priv->windows, window);
463 gtk_window_set_application (window, NULL);
467 extract_accel_from_menu_item (GMenuModel *model,
471 GMenuAttributeIter *iter;
474 const gchar *accel = NULL;
475 const gchar *action = NULL;
476 GVariant *target = NULL;
478 iter = g_menu_model_iterate_item_attributes (model, item);
479 while (g_menu_attribute_iter_get_next (iter, &key, &value))
481 if (g_str_equal (key, "action") && g_variant_is_of_type (value, G_VARIANT_TYPE_STRING))
482 action = g_variant_get_string (value, NULL);
483 else if (g_str_equal (key, "accel") && g_variant_is_of_type (value, G_VARIANT_TYPE_STRING))
484 accel = g_variant_get_string (value, NULL);
485 else if (g_str_equal (key, "target"))
486 target = g_variant_ref (value);
487 g_variant_unref (value);
489 g_object_unref (iter);
492 gtk_application_add_accelerator (app, accel, action, target);
495 g_variant_unref (target);
499 extract_accels_from_menu (GMenuModel *model,
507 for (i = 0; i < g_menu_model_get_n_items (model); i++)
509 extract_accel_from_menu_item (model, i, app);
511 iter = g_menu_model_iterate_item_links (model, i);
512 while (g_menu_link_iter_get_next (iter, &key, &m))
514 extract_accels_from_menu (m, app);
517 g_object_unref (iter);
522 gtk_application_notify (GObject *object,
525 if (strcmp (pspec->name, "app-menu") == 0 ||
526 strcmp (pspec->name, "menubar") == 0)
529 g_object_get (object, pspec->name, &model, NULL);
532 extract_accels_from_menu (model, GTK_APPLICATION (object));
533 g_object_unref (model);
537 if (G_OBJECT_CLASS (gtk_application_parent_class)->notify)
538 G_OBJECT_CLASS (gtk_application_parent_class)->notify (object, pspec);
542 gtk_application_get_property (GObject *object,
547 GtkApplication *application = GTK_APPLICATION (object);
551 case PROP_REGISTER_SESSION:
552 g_value_set_boolean (value, application->priv->register_session);
556 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
562 gtk_application_set_property (GObject *object,
567 GtkApplication *application = GTK_APPLICATION (object);
571 case PROP_REGISTER_SESSION:
572 application->priv->register_session = g_value_get_boolean (value);
576 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
582 gtk_application_quit (GtkApplication *app)
584 /* we are asked to quit, so don't linger */
585 g_application_set_inactivity_timeout (G_APPLICATION (app), 0);
589 gtk_application_class_init (GtkApplicationClass *class)
591 GObjectClass *object_class = G_OBJECT_CLASS (class);
592 GApplicationClass *application_class = G_APPLICATION_CLASS (class);
594 object_class->get_property = gtk_application_get_property;
595 object_class->set_property = gtk_application_set_property;
596 object_class->notify = gtk_application_notify;
598 application_class->add_platform_data = gtk_application_add_platform_data;
599 application_class->before_emit = gtk_application_before_emit;
600 application_class->after_emit = gtk_application_after_emit;
601 application_class->startup = gtk_application_startup;
602 application_class->shutdown = gtk_application_shutdown;
604 class->window_added = gtk_application_window_added;
605 class->window_removed = gtk_application_window_removed;
606 class->quit = gtk_application_quit;
608 g_type_class_add_private (class, sizeof (GtkApplicationPrivate));
611 * GtkApplication::window-added:
612 * @application: the #GtkApplication which emitted the signal
613 * @window: the newly-added #GtkWindow
615 * Emitted when a #GtkWindow is added to @application through
616 * gtk_application_add_window().
620 gtk_application_signals[WINDOW_ADDED] =
621 g_signal_new ("window-added", GTK_TYPE_APPLICATION, G_SIGNAL_RUN_FIRST,
622 G_STRUCT_OFFSET (GtkApplicationClass, window_added),
624 g_cclosure_marshal_VOID__OBJECT,
625 G_TYPE_NONE, 1, GTK_TYPE_WINDOW);
628 * GtkApplication::window-removed:
629 * @application: the #GtkApplication which emitted the signal
630 * @window: the #GtkWindow that is being removed
632 * Emitted when a #GtkWindow is removed from @application,
633 * either as a side-effect of being destroyed or explicitly
634 * through gtk_application_remove_window().
638 gtk_application_signals[WINDOW_REMOVED] =
639 g_signal_new ("window-removed", GTK_TYPE_APPLICATION, G_SIGNAL_RUN_FIRST,
640 G_STRUCT_OFFSET (GtkApplicationClass, window_removed),
642 g_cclosure_marshal_VOID__OBJECT,
643 G_TYPE_NONE, 1, GTK_TYPE_WINDOW);
646 * GtkApplication::quit:
647 * @application: the #GtkApplication
649 * Emitted when the session manager wants the application to quit
650 * (generally because the user is logging out). The application
651 * should exit as soon as possible after receiving this signal; if
652 * it does not, the session manager may choose to forcibly kill it.
654 * Normally, an application would only be sent a ::quit if there
655 * are no inhibitors (see gtk_application_inhibit()).
656 * However, this is not guaranteed; in some situations the
657 * session manager may decide to end the session without giving
658 * applications a chance to object.
660 * To receive this signal, you need to set the
661 * #GtkApplication::register-session property
662 * when creating the application object.
666 gtk_application_signals[QUIT] =
667 g_signal_new ("quit", GTK_TYPE_APPLICATION, G_SIGNAL_RUN_FIRST,
668 G_STRUCT_OFFSET (GtkApplicationClass, quit),
669 NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
672 * GtkApplication::register-session:
674 * Set this property to %TRUE to register with the session manager
675 * and receive the #GtkApplication::quit signal when the session
680 g_object_class_install_property (object_class, PROP_REGISTER_SESSION,
681 g_param_spec_boolean ("register-session",
682 P_("Register session"),
683 P_("Register with the session manager"),
684 FALSE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
688 * gtk_application_new:
689 * @application_id: the application id
690 * @flags: the application flags
692 * Creates a new #GtkApplication instance.
694 * This function calls g_type_init() for you. gtk_init() is called
695 * as soon as the application gets registered as the primary instance.
697 * Note that commandline arguments are not passed to gtk_init().
698 * All GTK+ functionality that is available via commandline arguments
699 * can also be achieved by setting suitable environment variables
700 * such as <envvar>G_DEBUG</envvar>, so this should not be a big
701 * problem. If you absolutely must support GTK+ commandline arguments,
702 * you can explicitly call gtk_init() before creating the application
705 * The application id must be valid. See g_application_id_is_valid().
707 * Returns: a new #GtkApplication instance
710 gtk_application_new (const gchar *application_id,
711 GApplicationFlags flags)
713 g_return_val_if_fail (g_application_id_is_valid (application_id), NULL);
717 return g_object_new (GTK_TYPE_APPLICATION,
718 "application-id", application_id,
724 * gtk_application_add_window:
725 * @application: a #GtkApplication
726 * @window: a #GtkWindow
728 * Adds a window from @application.
730 * This call is equivalent to setting the #GtkWindow:application
731 * property of @window to @application.
733 * Normally, the connection between the application and the window
734 * will remain until the window is destroyed, but you can explicitly
735 * remove it with gtk_application_remove_window().
737 * GTK+ will keep the application running as long as it has
743 gtk_application_add_window (GtkApplication *application,
746 g_return_if_fail (GTK_IS_APPLICATION (application));
748 if (!g_list_find (application->priv->windows, window))
749 g_signal_emit (application,
750 gtk_application_signals[WINDOW_ADDED], 0, window);
754 * gtk_application_remove_window:
755 * @application: a #GtkApplication
756 * @window: a #GtkWindow
758 * Remove a window from @application.
760 * If @window belongs to @application then this call is equivalent to
761 * setting the #GtkWindow:application property of @window to
764 * The application may stop running as a result of a call to this
770 gtk_application_remove_window (GtkApplication *application,
773 g_return_if_fail (GTK_IS_APPLICATION (application));
775 if (g_list_find (application->priv->windows, window))
776 g_signal_emit (application,
777 gtk_application_signals[WINDOW_REMOVED], 0, window);
781 * gtk_application_get_windows:
782 * @application: a #GtkApplication
784 * Gets a list of the #GtkWindows associated with @application.
786 * The list is sorted by most recently focused window, such that the first
787 * element is the currently focused window. (Useful for choosing a parent
788 * for a transient window.)
790 * The list that is returned should not be modified in any way. It will
791 * only remain valid until the next focus change or window creation or
794 * Returns: (element-type GtkWindow) (transfer none): a #GList of #GtkWindow
799 gtk_application_get_windows (GtkApplication *application)
801 g_return_val_if_fail (GTK_IS_APPLICATION (application), NULL);
803 return application->priv->windows;
807 * gtk_application_add_accelerator:
808 * @application: a #GtkApplication
809 * @accelerator: accelerator string
810 * @action_name: the name of the action to activate
811 * @parameter: (allow-none): parameter to pass when activating the action,
812 * or %NULL if the action does not accept an activation parameter
814 * Installs an accelerator that will cause the named action
815 * to be activated when the key combination specificed by @accelerator
818 * @accelerator must be a string that can be parsed by
819 * gtk_accelerator_parse(), e.g. "<Primary>q" or "<Control><Alt>p".
821 * @action_name must be the name of an action as it would be used
822 * in the app menu, i.e. actions that have been added to the application
823 * are referred to with an "app." prefix, and window-specific actions
824 * with a "win." prefix.
826 * GtkApplication also extracts accelerators out of 'accel' attributes
827 * in the #GMenuModels passed to gtk_application_set_app_menu() and
828 * gtk_application_set_menubar(), which is usually more convenient
829 * than calling this function for each accelerator.
834 gtk_application_add_accelerator (GtkApplication *application,
835 const gchar *accelerator,
836 const gchar *action_name,
841 GdkModifierType accel_mods;
843 g_return_if_fail (GTK_IS_APPLICATION (application));
845 /* Call this here, since gtk_init() is only getting called in startup() */
846 _gtk_accel_map_init ();
848 gtk_accelerator_parse (accelerator, &accel_key, &accel_mods);
852 g_warning ("Failed to parse accelerator: '%s'\n", accelerator);
856 accel_path = _gtk_accel_path_for_action (action_name, parameter);
858 if (gtk_accel_map_lookup_entry (accel_path, NULL))
859 gtk_accel_map_change_entry (accel_path, accel_key, accel_mods, TRUE);
861 gtk_accel_map_add_entry (accel_path, accel_key, accel_mods);
867 * gtk_application_remove_accelerator:
868 * @application: a #GtkApplication
869 * @action_name: the name of the action to activate
870 * @parameter: (allow-none): parameter to pass when activating the action,
871 * or %NULL if the action does not accept an activation parameter
873 * Removes an accelerator that has been previously added
874 * with gtk_application_add_accelerator().
879 gtk_application_remove_accelerator (GtkApplication *application,
880 const gchar *action_name,
885 g_return_if_fail (GTK_IS_APPLICATION (application));
887 accel_path = _gtk_accel_path_for_action (action_name, parameter);
889 if (!gtk_accel_map_lookup_entry (accel_path, NULL))
891 g_warning ("No accelerator found for '%s'\n", accel_path);
896 gtk_accel_map_change_entry (accel_path, 0, 0, FALSE);
901 * gtk_application_set_app_menu:
902 * @application: a #GtkApplication
903 * @model: (allow-none): a #GMenuModel, or %NULL
905 * Sets or unsets the application menu for @application.
907 * The application menu is a single menu containing items that typically
908 * impact the application as a whole, rather than acting on a specific
909 * window or document. For example, you would expect to see
910 * "Preferences" or "Quit" in an application menu, but not "Save" or
913 * If supported, the application menu will be rendered by the desktop
919 gtk_application_set_app_menu (GtkApplication *application,
922 g_object_set (application, "app-menu", model, NULL);
926 * gtk_application_get_app_menu:
927 * @application: a #GtkApplication
929 * Returns the menu model that has been set with
930 * gtk_application_set_app_menu().
932 * Returns: (transfer none): the application menu of @application
937 gtk_application_get_app_menu (GtkApplication *application)
939 GMenuModel *app_menu = NULL;
941 g_object_get (application, "app-menu", &app_menu, NULL);
944 g_object_unref (app_menu);
950 * gtk_application_set_menubar:
951 * @application: a #GtkApplication
952 * @model: (allow-none): a #GMenuModel, or %NULL
954 * Sets or unsets the menubar for windows of @application.
956 * This is a menubar in the traditional sense.
958 * Depending on the desktop environment, this may appear at the top of
959 * each window, or at the top of the screen. In some environments, if
960 * both the application menu and the menubar are set, the application
961 * menu will be presented as if it were the first item of the menubar.
962 * Other environments treat the two as completely separate -- for
963 * example, the application menu may be rendered by the desktop shell
964 * while the menubar (if set) remains in each individual window.
969 gtk_application_set_menubar (GtkApplication *application,
972 g_object_set (application, "menubar", model, NULL);
976 * gtk_application_get_menubar:
977 * @application: a #GtkApplication
979 * Returns the menu model that has been set with
980 * gtk_application_set_menubar().
982 * Returns: (transfer none): the menubar for windows of @application
987 gtk_application_get_menubar (GtkApplication *application)
989 GMenuModel *menubar = NULL;
991 g_object_get (application, "menubar", &menubar, NULL);
994 g_object_unref (menubar);
999 #if defined(GDK_WINDOWING_X11)
1001 /* D-Bus Session Management
1003 * The protocol and the D-Bus API are described here:
1004 * http://live.gnome.org/SessionManagement/GnomeSession
1005 * http://people.gnome.org/~mccann/gnome-session/docs/gnome-session.html
1009 unregister_client (GtkApplication *app)
1011 GError *error = NULL;
1013 g_debug ("Unregistering client");
1015 g_dbus_proxy_call_sync (app->priv->sm_proxy,
1017 g_variant_new ("(o)", app->priv->client_path),
1018 G_DBUS_CALL_FLAGS_NONE,
1025 g_warning ("Failed to unregister client: %s", error->message);
1026 g_error_free (error);
1029 g_clear_object (&app->priv->client_proxy);
1031 g_free (app->priv->client_path);
1032 app->priv->client_path = NULL;
1036 gtk_application_quit_response (GtkApplication *application,
1038 const gchar *reason)
1040 g_debug ("Calling EndSessionResponse %d '%s'", will_quit, reason);
1042 g_dbus_proxy_call (application->priv->client_proxy,
1043 "EndSessionResponse",
1044 g_variant_new ("(bs)", will_quit, reason ? reason : ""),
1045 G_DBUS_CALL_FLAGS_NONE,
1050 client_proxy_signal (GDBusProxy *proxy,
1051 const gchar *sender_name,
1052 const gchar *signal_name,
1053 GVariant *parameters,
1054 GtkApplication *app)
1056 if (strcmp (signal_name, "QueryEndSession") == 0)
1058 g_debug ("Received QueryEndSession");
1059 gtk_application_quit_response (app, TRUE, NULL);
1061 else if (strcmp (signal_name, "CancelEndSession") == 0)
1063 g_debug ("Received CancelEndSession");
1065 else if (strcmp (signal_name, "EndSession") == 0)
1067 g_debug ("Received EndSession");
1068 gtk_application_quit_response (app, TRUE, NULL);
1069 unregister_client (app);
1070 g_signal_emit (app, gtk_application_signals[QUIT], 0);
1072 else if (strcmp (signal_name, "Stop") == 0)
1074 g_debug ("Received Stop");
1075 unregister_client (app);
1076 g_signal_emit (app, gtk_application_signals[QUIT], 0);
1081 gtk_application_startup_session_dbus (GtkApplication *app)
1083 static gchar *client_id;
1084 GError *error = NULL;
1087 if (app->priv->session_bus == NULL)
1090 if (client_id == NULL)
1092 const gchar *desktop_autostart_id;
1094 desktop_autostart_id = g_getenv ("DESKTOP_AUTOSTART_ID");
1095 /* Unset DESKTOP_AUTOSTART_ID in order to avoid child processes to
1096 * use the same client id.
1098 g_unsetenv ("DESKTOP_AUTOSTART_ID");
1099 client_id = g_strdup (desktop_autostart_id ? desktop_autostart_id : "");
1102 g_debug ("Connecting to session manager");
1104 app->priv->sm_proxy = g_dbus_proxy_new_sync (app->priv->session_bus,
1105 G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES |
1106 G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS,
1108 "org.gnome.SessionManager",
1109 "/org/gnome/SessionManager",
1110 "org.gnome.SessionManager",
1115 g_warning ("Failed to get a session proxy: %s", error->message);
1116 g_error_free (error);
1120 /* FIXME: should we reuse the D-Bus application id here ? */
1121 app->priv->app_id = g_strdup (g_get_prgname ());
1123 if (!app->priv->register_session)
1126 g_debug ("Registering client '%s' '%s'", app->priv->app_id, client_id);
1128 res = g_dbus_proxy_call_sync (app->priv->sm_proxy,
1130 g_variant_new ("(ss)", app->priv->app_id, client_id),
1131 G_DBUS_CALL_FLAGS_NONE,
1138 g_warning ("Failed to register client: %s", error->message);
1139 g_error_free (error);
1140 g_clear_object (&app->priv->sm_proxy);
1144 g_variant_get (res, "(o)", &app->priv->client_path);
1145 g_variant_unref (res);
1147 g_debug ("Registered client at '%s'", app->priv->client_path);
1149 app->priv->client_proxy = g_dbus_proxy_new_sync (app->priv->session_bus, 0,
1151 "org.gnome.SessionManager",
1152 app->priv->client_path,
1153 "org.gnome.SessionManager.ClientPrivate",
1158 g_warning ("Failed to get client proxy: %s", error->message);
1159 g_error_free (error);
1160 g_clear_object (&app->priv->sm_proxy);
1161 g_free (app->priv->client_path);
1162 app->priv->client_path = NULL;
1166 g_signal_connect (app->priv->client_proxy, "g-signal", G_CALLBACK (client_proxy_signal), app);
1172 * GtkApplicationInhibitFlags:
1173 * @GTK_APPLICATION_INHIBIT_LOGOUT: Inhibit ending the user session
1174 * by logging out or by shutting down the computer
1175 * @GTK_APPLICATION_INHIBIT_SWITCH: Inhibit user switching
1176 * @GTK_APPLICATION_INHIBIT_SUSPEND: Inhibit suspending the
1177 * session or computer
1178 * @GTK_APPLICATION_INHIBIT_IDLE: Inhibit the session being
1179 * marked as idle (and possibly locked)
1181 * Types of user actions that may be blocked by gtk_application_inhibit().
1187 * gtk_application_inhibit:
1188 * @application: the #GApplication
1189 * @window: (allow-none): a #GtkWindow, or %NULL
1190 * @flags: what types of actions should be inhibited
1191 * @reason: (allow-none): a short, human-readable string that explains
1192 * why these operations are inhibited
1194 * Inform the session manager that certain types of actions should be
1195 * inhibited. This is not guaranteed to work on all platforms and for
1196 * all types of actions.
1198 * Applications should invoke this method when they begin an operation
1199 * that should not be interrupted, such as creating a CD or DVD. The
1200 * types of actions that may be blocked are specified by the @flags
1201 * parameter. When the application completes the operation it should
1202 * call g_application_uninhibit() to remove the inhibitor. Note that
1203 * an application can have multiple inhibitors, and all of the must
1204 * be individually removed. Inhibitors are also cleared when the
1205 * application exits.
1207 * Applications should not expect that they will always be able to block
1208 * the action. In most cases, users will be given the option to force
1209 * the action to take place.
1211 * Reasons should be short and to the point.
1213 * If @window is given, the session manager may point the user to
1214 * this window to find out more about why the action is inhibited.
1216 * Returns: A non-zero cookie that is used to uniquely identify this
1217 * request. It should be used as an argument to g_application_uninhibit()
1218 * in order to remove the request. If the platform does not support
1219 * inhibiting or the request failed for some reason, 0 is returned.
1224 gtk_application_inhibit (GtkApplication *application,
1226 GtkApplicationInhibitFlags flags,
1227 const gchar *reason)
1230 GError *error = NULL;
1234 g_return_val_if_fail (GTK_IS_APPLICATION (application), 0);
1235 g_return_val_if_fail (!g_application_get_is_remote (G_APPLICATION (application)), 0);
1236 g_return_val_if_fail (application->priv->sm_proxy != NULL, 0);
1239 xid = GDK_WINDOW_XID (gtk_widget_get_window (GTK_WIDGET (window)));
1243 res = g_dbus_proxy_call_sync (application->priv->sm_proxy,
1245 g_variant_new ("(susu)",
1246 application->priv->app_id,
1250 G_DBUS_CALL_FLAGS_NONE,
1256 g_warning ("Calling Inhibit failed: %s", error->message);
1257 g_error_free (error);
1261 g_variant_get (res, "(u)", &cookie);
1262 g_variant_unref (res);
1268 * gtk_application_uninhibit:
1269 * @application: the #GApplication
1270 * @cookie: a cookie that was returned by g_application_inhibit()
1272 * Removes an inhibitor that has been established with g_application_inhibit().
1273 * Inhibitors are also cleared when the application exits.
1278 gtk_application_uninhibit (GtkApplication *application,
1281 g_return_if_fail (GTK_IS_APPLICATION (application));
1282 g_return_if_fail (!g_application_get_is_remote (G_APPLICATION (application)));
1283 g_return_if_fail (application->priv->sm_proxy != NULL);
1285 g_dbus_proxy_call (application->priv->sm_proxy,
1287 g_variant_new ("(u)", cookie),
1288 G_DBUS_CALL_FLAGS_NONE,
1294 * gtk_application_is_inhibited:
1295 * @application: the #GApplication
1296 * @flags: what types of actions should be queried
1298 * Determines if any of the actions specified in @flags are
1299 * currently inhibited (possibly by another application).
1301 * Returns: %TRUE if any of the actions specified in @flags are inhibited
1306 gtk_application_is_inhibited (GtkApplication *application,
1307 GtkApplicationInhibitFlags flags)
1310 GError *error = NULL;
1313 g_return_val_if_fail (GTK_IS_APPLICATION (application), FALSE);
1314 g_return_val_if_fail (!g_application_get_is_remote (G_APPLICATION (application)), FALSE);
1315 g_return_val_if_fail (application->priv->sm_proxy != NULL, FALSE);
1317 res = g_dbus_proxy_call_sync (application->priv->sm_proxy,
1319 g_variant_new ("(u)", flags),
1320 G_DBUS_CALL_FLAGS_NONE,
1326 g_warning ("Calling IsInhibited failed: %s", error->message);
1327 g_error_free (error);
1331 g_variant_get (res, "(b)", &inhibited);
1332 g_variant_unref (res);
1338 * GtkApplicationEndSessionStyle:
1339 * @GTK_APPLICATION_LOGOUT: End the session by logging out
1340 * @GTK_APPLICATION_REBOOT: Restart the computer
1341 * @GTK_APPLICATION_SHUTDOWN: Shut the computer down
1343 * Different ways to end a user session, for use with
1344 * gtk_application_end_session().
1348 * gtk_application_end_session:
1349 * @application: the #GtkApplication
1350 * @style: the desired kind of session end
1351 * @request_confirmation: whether or not the user should get a chance
1352 * to confirm the action
1354 * Requests that the session manager end the current session.
1355 * @style indicates how the session should be ended, and
1356 * @request_confirmation indicates whether or not the user should be
1357 * given a chance to confirm the action. Both of these parameters are
1358 * merely hints though; the session manager may choose to ignore them.
1360 * Return value: %TRUE if the request was sent; %FALSE if it could not
1361 * be sent (eg, because it could not connect to the session manager)
1366 gtk_application_end_session (GtkApplication *application,
1367 GtkApplicationEndSessionStyle style,
1368 gboolean request_confirmation)
1370 g_return_val_if_fail (GTK_IS_APPLICATION (application), FALSE);
1371 g_return_val_if_fail (!g_application_get_is_remote (G_APPLICATION (application)), FALSE);
1372 g_return_val_if_fail (application->priv->sm_proxy != NULL, FALSE);
1376 case GTK_APPLICATION_LOGOUT:
1377 g_dbus_proxy_call (application->priv->sm_proxy,
1379 g_variant_new ("(u)", request_confirmation ? 0 : 1),
1380 G_DBUS_CALL_FLAGS_NONE,
1384 case GTK_APPLICATION_REBOOT:
1385 case GTK_APPLICATION_SHUTDOWN:
1386 g_dbus_proxy_call (application->priv->sm_proxy,
1389 G_DBUS_CALL_FLAGS_NONE,
1398 #elif defined(GDK_WINDOWING_QUARTZ)
1400 /* OS X implementation copied from EggSMClient */
1403 quit_requested_resumed (const AppleEvent *aevt,
1407 GtkApplication *app = GSIZE_TO_POINTER ((gsize)refcon);
1409 return app->priv->quit_inhibit == 0 ? noErr : userCanceledErr;
1413 idle_will_quit (gpointer data)
1415 GtkApplication *app = data;
1417 /* Resume the event with a new handler that will return
1418 * a value to the system
1420 AEResumeTheCurrentEvent (&app->priv->quit_event, &app->priv->quit_reply,
1421 NewAEEventHandlerUPP (quit_requested_resumed),
1422 (long)GPOINTER_TO_SIZE (app));
1424 AEDisposeDesc (&app->quit->quit_event);
1425 AEDisposeDesc (&app->quit->quit_reply);
1427 if (app->priv->quit_inhibit == 0)
1428 g_signal_emit (app, gtk_application_signals[QUIT], 0);
1434 quit_requested (const AppleEvent *aevt,
1438 GtkApplication *app = GSIZE_TO_POINTER ((gsize)refcon);
1440 /* FIXME AEInteractWithUser? */
1441 AEDuplicateDesc (aevt, &app->priv->quit_event);
1442 AEDuplicateDesc (reply, &app->priv->quit_reply);
1443 AESuspendTheCurrentEvent (aevt);
1445 /* Don't emit the "quit" signal immediately, since we're
1446 * called from a weird point in the guts of gdkeventloop-quartz.c
1448 g_idle_add (idle_will_quit, app);
1454 gtk_application_startup_session_quartz (GtkApplication *app)
1456 if (app->priv->register_session)
1457 AEInstallEventHandler (kCoreEventClass, kAEQuitApplication,
1458 NewAEEventHandlerUPP (quit_requested),
1459 (long)GPOINTER_TO_SIZE (app), false);
1461 app->priv->inhibitors = g_hash_table_new (NULL, NULL);
1465 gtk_application_inhibit (GtkApplication *application,
1467 GtkApplicationInhibitFlags flags,
1468 const gchar *reason)
1472 g_return_val_if_fail (GTK_IS_APPLICATION (application), 0);
1473 g_return_val_if_fail (flags != 0, 0);
1475 application->priv->next_cookie++;
1476 cookie = application->priv->next_cookie;
1478 g_hash_table_insert (application->priv->inhibitors,
1479 GUINT_TO_POINTER (cookie),
1480 GUINT_TO_POINTER (flags));
1482 if (flags & GTK_APPLICATION_INHIBIT_LOGOUT)
1483 application->priv->quit_inhibit++;
1489 gtk_application_uninhibit (GtkApplication *application,
1492 GApplicationInhibitFlags flags;
1494 flags = GPOINTER_TO_UINT (g_hash_table_lookup (application->priv->inhibitors, GUINT_TO_POINTER (cookie)));
1498 g_warning ("Invalid inhibitor cookie");
1502 g_hash_table_remove (application->priv->inhibitors, GUINT_TO_POINTER (cookie));
1504 if (flags & GTK_APPLICATION_INHIBIT_LOGOUT)
1505 application->priv->quit_inhibit--;
1509 gtk_application_is_inhibited (GtkApplication *application,
1510 GtkApplicationInhibitFlags flags)
1512 if (flags & GTK_APPLICATION_INHIBIT_LOGOUT)
1513 return application->priv->quit_inhibit > 0;
1519 gtk_application_end_session (GtkApplication *application,
1520 GtkApplicationEndStyle *style,
1521 gboolean request_confirmation)
1523 static const ProcessSerialNumber loginwindow_psn = { 0, kSystemProcess };
1524 AppleEvent event = { typeNull, NULL };
1525 AppleEvent reply = { typeNull, NULL };
1526 AEAddressDesc target;
1532 case GTK_APPLICATION_LOGOUT:
1533 id = request_confirmation ? kAELogOut : kAEReallyLogOut;
1535 case GTK_APPLICATION_REBOOT:
1536 id = request_confirmation ? kAEShowRestartDialog : kAERestart;
1538 case GTK_APPLICATION_SHUTDOWN:
1539 id = request_confirmation ? kAEShowShutdownDialog : kAEShutDown;
1543 err = AECreateDesc (typeProcessSerialNumber, &loginwindow_psn,
1544 sizeof (loginwindow_psn), &target);
1547 g_warning ("Could not create descriptor for loginwindow: %d", err);
1551 err = AECreateAppleEvent (kCoreEventClass, id, &target,
1552 kAutoGenerateReturnID, kAnyTransactionID,
1554 AEDisposeDesc (&target);
1557 g_warning ("Could not create logout AppleEvent: %d", err);
1561 err = AESend (&event, &reply, kAENoReply, kAENormalPriority,
1562 kAEDefaultTimeout, NULL, NULL);
1563 AEDisposeDesc (&event);
1565 AEDisposeDesc (&reply);
1567 return err == noErr;
1572 /* Trivial implementation.
1574 * For the inhibit API on Windows, see
1575 * http://msdn.microsoft.com/en-us/library/ms700677%28VS.85%29.aspx
1579 gtk_application_inhibit (GtkApplication *application,
1581 GtkApplicationInhibitFlags flags,
1582 const gchar *reason)
1588 gtk_application_uninhibit (GtkApplication *application,
1594 gtk_application_is_inhibited (GtkApplication *application,
1595 GtkApplicationInhibitFlags flags)
1601 gtk_application_end_session (GtkApplication *application,
1602 GtkApplicationEndStyle style,
1603 gboolean request_confirmation)