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 * g_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 g_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-requested 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.
135 static guint gtk_application_signals[LAST_SIGNAL];
139 PROP_REGISTER_SESSION
142 G_DEFINE_TYPE (GtkApplication, gtk_application, G_TYPE_APPLICATION)
144 struct _GtkApplicationPrivate
148 gboolean register_session;
149 gboolean quit_requested;
151 #ifdef GDK_WINDOWING_X11
152 GDBusConnection *session_bus;
153 gchar *window_prefix;
156 GDBusProxy *sm_proxy;
157 GDBusProxy *client_proxy;
162 #ifdef GDK_WINDOWING_QUARTZ
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;
303 gtk_application_focus_changed (GtkApplication *application,
306 if (G_IS_ACTION_GROUP (window))
307 g_action_muxer_insert (application->priv->muxer, "win", G_ACTION_GROUP (window));
309 g_action_muxer_remove (application->priv->muxer, "win");
314 gtk_application_focus_in_event_cb (GtkWindow *window,
315 GdkEventFocus *event,
316 GtkApplication *application)
318 GtkApplicationPrivate *priv = application->priv;
321 /* Keep the window list sorted by most-recently-focused. */
322 link = g_list_find (priv->windows, window);
323 if (link != NULL && link != priv->windows)
325 priv->windows = g_list_remove_link (priv->windows, link);
326 priv->windows = g_list_concat (link, priv->windows);
329 #ifdef GDK_WINDOWING_QUARTZ
330 gtk_application_focus_changed (application, window);
337 gtk_application_startup (GApplication *application)
339 G_APPLICATION_CLASS (gtk_application_parent_class)
340 ->startup (application);
344 #ifdef GDK_WINDOWING_X11
345 gtk_application_startup_x11 (GTK_APPLICATION (application));
348 #ifdef GDK_WINDOWING_QUARTZ
349 gtk_application_startup_quartz (GTK_APPLICATION (application));
354 gtk_application_shutdown (GApplication *application)
356 #ifdef GDK_WINDOWING_X11
357 gtk_application_shutdown_x11 (GTK_APPLICATION (application));
360 #ifdef GDK_WINDOWING_QUARTZ
361 gtk_application_shutdown_quartz (GTK_APPLICATION (application));
364 G_APPLICATION_CLASS (gtk_application_parent_class)
365 ->shutdown (application);
369 gtk_application_add_platform_data (GApplication *application,
370 GVariantBuilder *builder)
372 const gchar *startup_id;
374 startup_id = getenv ("DESKTOP_STARTUP_ID");
376 if (startup_id && g_utf8_validate (startup_id, -1, NULL))
377 g_variant_builder_add (builder, "{sv}", "desktop-startup-id",
378 g_variant_new_string (startup_id));
382 gtk_application_before_emit (GApplication *application,
383 GVariant *platform_data)
389 gdk_threads_enter ();
391 g_variant_iter_init (&iter, platform_data);
392 while (g_variant_iter_loop (&iter, "{&sv}", &key, &value))
394 #ifdef GDK_WINDOWING_X11
395 if (strcmp (key, "desktop-startup-id") == 0)
400 display = gdk_display_get_default ();
401 id = g_variant_get_string (value, NULL);
402 if (GDK_IS_X11_DISPLAY (display))
403 gdk_x11_display_set_startup_notification_id (display, id);
410 gtk_application_after_emit (GApplication *application,
411 GVariant *platform_data)
413 gdk_notify_startup_complete ();
415 gdk_threads_leave ();
419 gtk_application_init (GtkApplication *application)
421 application->priv = G_TYPE_INSTANCE_GET_PRIVATE (application,
422 GTK_TYPE_APPLICATION,
423 GtkApplicationPrivate);
427 gtk_application_window_added (GtkApplication *application,
430 GtkApplicationPrivate *priv = application->priv;
432 priv->windows = g_list_prepend (priv->windows, window);
433 gtk_window_set_application (window, application);
434 g_application_hold (G_APPLICATION (application));
436 g_signal_connect (window, "focus-in-event",
437 G_CALLBACK (gtk_application_focus_in_event_cb),
440 #ifdef GDK_WINDOWING_X11
441 gtk_application_window_added_x11 (application, window);
446 gtk_application_window_removed (GtkApplication *application,
449 GtkApplicationPrivate *priv = application->priv;
451 #ifdef GDK_WINDOWING_X11
452 gtk_application_window_removed_x11 (application, window);
455 g_signal_handlers_disconnect_by_func (window,
456 gtk_application_focus_in_event_cb,
459 g_application_release (G_APPLICATION (application));
460 priv->windows = g_list_remove (priv->windows, window);
461 gtk_window_set_application (window, NULL);
465 extract_accel_from_menu_item (GMenuModel *model,
469 GMenuAttributeIter *iter;
472 const gchar *accel = NULL;
473 const gchar *action = NULL;
474 GVariant *target = NULL;
476 iter = g_menu_model_iterate_item_attributes (model, item);
477 while (g_menu_attribute_iter_get_next (iter, &key, &value))
479 if (g_str_equal (key, "action") && g_variant_is_of_type (value, G_VARIANT_TYPE_STRING))
480 action = g_variant_get_string (value, NULL);
481 else if (g_str_equal (key, "accel") && g_variant_is_of_type (value, G_VARIANT_TYPE_STRING))
482 accel = g_variant_get_string (value, NULL);
483 else if (g_str_equal (key, "target"))
484 target = g_variant_ref (value);
485 g_variant_unref (value);
487 g_object_unref (iter);
490 gtk_application_add_accelerator (app, accel, action, target);
493 g_variant_unref (target);
497 extract_accels_from_menu (GMenuModel *model,
505 for (i = 0; i < g_menu_model_get_n_items (model); i++)
507 extract_accel_from_menu_item (model, i, app);
509 iter = g_menu_model_iterate_item_links (model, i);
510 while (g_menu_link_iter_get_next (iter, &key, &m))
512 extract_accels_from_menu (m, app);
515 g_object_unref (iter);
520 gtk_application_notify (GObject *object,
523 if (strcmp (pspec->name, "app-menu") == 0 ||
524 strcmp (pspec->name, "menubar") == 0)
527 g_object_get (object, pspec->name, &model, NULL);
530 extract_accels_from_menu (model, GTK_APPLICATION (object));
531 g_object_unref (model);
535 if (G_OBJECT_CLASS (gtk_application_parent_class)->notify)
536 G_OBJECT_CLASS (gtk_application_parent_class)->notify (object, pspec);
540 gtk_application_get_property (GObject *object,
545 GtkApplication *application = GTK_APPLICATION (object);
549 case PROP_REGISTER_SESSION:
550 g_value_set_boolean (value, application->priv->register_session);
554 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
560 gtk_application_set_property (GObject *object,
565 GtkApplication *application = GTK_APPLICATION (object);
569 case PROP_REGISTER_SESSION:
570 application->priv->register_session = g_value_get_boolean (value);
574 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
580 gtk_application_class_init (GtkApplicationClass *class)
582 GObjectClass *object_class = G_OBJECT_CLASS (class);
583 GApplicationClass *application_class = G_APPLICATION_CLASS (class);
585 object_class->get_property = gtk_application_get_property;
586 object_class->set_property = gtk_application_set_property;
587 object_class->notify = gtk_application_notify;
589 application_class->add_platform_data = gtk_application_add_platform_data;
590 application_class->before_emit = gtk_application_before_emit;
591 application_class->after_emit = gtk_application_after_emit;
592 application_class->startup = gtk_application_startup;
593 application_class->shutdown = gtk_application_shutdown;
595 class->window_added = gtk_application_window_added;
596 class->window_removed = gtk_application_window_removed;
598 g_type_class_add_private (class, sizeof (GtkApplicationPrivate));
601 * GtkApplication::window-added:
602 * @application: the #GtkApplication which emitted the signal
603 * @window: the newly-added #GtkWindow
605 * Emitted when a #GtkWindow is added to @application through
606 * gtk_application_add_window().
610 gtk_application_signals[WINDOW_ADDED] =
611 g_signal_new ("window-added", GTK_TYPE_APPLICATION, G_SIGNAL_RUN_FIRST,
612 G_STRUCT_OFFSET (GtkApplicationClass, window_added),
614 g_cclosure_marshal_VOID__OBJECT,
615 G_TYPE_NONE, 1, GTK_TYPE_WINDOW);
618 * GtkApplication::window-removed:
619 * @application: the #GtkApplication which emitted the signal
620 * @window: the #GtkWindow that is being removed
622 * Emitted when a #GtkWindow is removed from @application,
623 * either as a side-effect of being destroyed or explicitly
624 * through gtk_application_remove_window().
628 gtk_application_signals[WINDOW_REMOVED] =
629 g_signal_new ("window-removed", GTK_TYPE_APPLICATION, G_SIGNAL_RUN_FIRST,
630 G_STRUCT_OFFSET (GtkApplicationClass, window_removed),
632 g_cclosure_marshal_VOID__OBJECT,
633 G_TYPE_NONE, 1, GTK_TYPE_WINDOW);
636 * GtkApplication::quit-requested:
637 * @application: the #GtkApplication
639 * Emitted when the session manager requests that the application
640 * exit (generally because the user is logging out). The application
641 * should decide whether or not it is willing to quit and then call
642 * g_application_quit_response(), passing %TRUE or %FALSE to give its
643 * answer to the session manager. It does not need to give an answer
644 * before returning from the signal handler; the answer can be given
645 * later on, but <emphasis>the application must not attempt to perform
646 * any actions or interact with the user</emphasis> in response to
647 * this signal. Any actions required for a clean shutdown should take
648 * place in response to the #GtkApplication::quit signal.
650 * The application should limit its operations until either the
651 * #GApplication::quit or #GtkApplication::quit-cancelled signals is
654 * To receive this signal, you need to set the
655 * #GtkApplication::register-session property
656 * when creating the application object.
660 gtk_application_signals[QUIT_REQUESTED] =
661 g_signal_new ("quit-requested", GTK_TYPE_APPLICATION, G_SIGNAL_RUN_LAST,
662 G_STRUCT_OFFSET (GtkApplicationClass, quit_requested),
663 NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
666 * GtkApplication::quit-cancelled:
667 * @application: the #GtkApplication
669 * Emitted when the session manager decides to cancel a logout after
670 * the application has already agreed to quit. After receiving this
671 * signal, the application can go back to what it was doing before
672 * receiving the #GtkApplication::quit-requested signal.
674 * To receive this signal, you need to set the
675 * #GtkApplication::register-session property
676 * when creating the application object.
680 gtk_application_signals[QUIT_CANCELLED] =
681 g_signal_new ("quit-cancelled", GTK_TYPE_APPLICATION, G_SIGNAL_RUN_LAST,
682 G_STRUCT_OFFSET (GtkApplicationClass, quit_cancelled),
683 NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
686 * GtkApplication::quit:
687 * @application: the #GtkApplication
689 * Emitted when the session manager wants the application to quit
690 * (generally because the user is logging out). The application
691 * should exit as soon as possible after receiving this signal; if
692 * it does not, the session manager may choose to forcibly kill it.
694 * Normally, an application would only be sent a ::quit if it
695 * agreed to quit in response to a #GtkApplication::quit-requested
696 * signal. However, this is not guaranteed; in some situations the
697 * session manager may decide to end the session without giving
698 * applications a chance to object.
700 * To receive this signal, you need to set the
701 * #GtkApplication::register-session property
702 * when creating the application object.
706 gtk_application_signals[QUIT] =
707 g_signal_new ("quit", GTK_TYPE_APPLICATION, G_SIGNAL_RUN_LAST,
708 G_STRUCT_OFFSET (GtkApplicationClass, quit),
709 NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
711 g_object_class_install_property (object_class, PROP_REGISTER_SESSION,
712 g_param_spec_boolean ("register-session",
713 P_("Register session"),
714 P_("Register with the session manager"),
715 FALSE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
719 * gtk_application_new:
720 * @application_id: the application id
721 * @flags: the application flags
723 * Creates a new #GtkApplication instance.
725 * This function calls g_type_init() for you. gtk_init() is called
726 * as soon as the application gets registered as the primary instance.
728 * Note that commandline arguments are not passed to gtk_init().
729 * All GTK+ functionality that is available via commandline arguments
730 * can also be achieved by setting suitable environment variables
731 * such as <envvar>G_DEBUG</envvar>, so this should not be a big
732 * problem. If you absolutely must support GTK+ commandline arguments,
733 * you can explicitly call gtk_init() before creating the application
736 * The application id must be valid. See g_application_id_is_valid().
738 * Returns: a new #GtkApplication instance
741 gtk_application_new (const gchar *application_id,
742 GApplicationFlags flags)
744 g_return_val_if_fail (g_application_id_is_valid (application_id), NULL);
748 return g_object_new (GTK_TYPE_APPLICATION,
749 "application-id", application_id,
755 * gtk_application_add_window:
756 * @application: a #GtkApplication
757 * @window: a #GtkWindow
759 * Adds a window from @application.
761 * This call is equivalent to setting the #GtkWindow:application
762 * property of @window to @application.
764 * Normally, the connection between the application and the window
765 * will remain until the window is destroyed, but you can explicitly
766 * remove it with gtk_application_remove_window().
768 * GTK+ will keep the application running as long as it has
774 gtk_application_add_window (GtkApplication *application,
777 g_return_if_fail (GTK_IS_APPLICATION (application));
779 if (!g_list_find (application->priv->windows, window))
780 g_signal_emit (application,
781 gtk_application_signals[WINDOW_ADDED], 0, window);
785 * gtk_application_remove_window:
786 * @application: a #GtkApplication
787 * @window: a #GtkWindow
789 * Remove a window from @application.
791 * If @window belongs to @application then this call is equivalent to
792 * setting the #GtkWindow:application property of @window to
795 * The application may stop running as a result of a call to this
801 gtk_application_remove_window (GtkApplication *application,
804 g_return_if_fail (GTK_IS_APPLICATION (application));
806 if (g_list_find (application->priv->windows, window))
807 g_signal_emit (application,
808 gtk_application_signals[WINDOW_REMOVED], 0, window);
812 * gtk_application_get_windows:
813 * @application: a #GtkApplication
815 * Gets a list of the #GtkWindows associated with @application.
817 * The list is sorted by most recently focused window, such that the first
818 * element is the currently focused window. (Useful for choosing a parent
819 * for a transient window.)
821 * The list that is returned should not be modified in any way. It will
822 * only remain valid until the next focus change or window creation or
825 * Returns: (element-type GtkWindow) (transfer none): a #GList of #GtkWindow
830 gtk_application_get_windows (GtkApplication *application)
832 g_return_val_if_fail (GTK_IS_APPLICATION (application), NULL);
834 return application->priv->windows;
838 * gtk_application_add_accelerator:
839 * @application: a #GtkApplication
840 * @accelerator: accelerator string
841 * @action_name: the name of the action to activate
842 * @parameter: (allow-none): parameter to pass when activating the action,
843 * or %NULL if the action does not accept an activation parameter
845 * Installs an accelerator that will cause the named action
846 * to be activated when the key combination specificed by @accelerator
849 * @accelerator must be a string that can be parsed by
850 * gtk_accelerator_parse(), e.g. "<Primary>q" or "<Control><Alt>p".
852 * @action_name must be the name of an action as it would be used
853 * in the app menu, i.e. actions that have been added to the application
854 * are referred to with an "app." prefix, and window-specific actions
855 * with a "win." prefix.
857 * GtkApplication also extracts accelerators out of 'accel' attributes
858 * in the #GMenuModels passed to g_application_set_app_menu() and
859 * g_application_set_menubar(), which is usually more convenient
860 * than calling this function for each accelerator.
865 gtk_application_add_accelerator (GtkApplication *application,
866 const gchar *accelerator,
867 const gchar *action_name,
872 GdkModifierType accel_mods;
874 g_return_if_fail (GTK_IS_APPLICATION (application));
876 /* Call this here, since gtk_init() is only getting called in startup() */
877 _gtk_accel_map_init ();
879 gtk_accelerator_parse (accelerator, &accel_key, &accel_mods);
883 g_warning ("Failed to parse accelerator: '%s'\n", accelerator);
887 accel_path = _gtk_accel_path_for_action (action_name, parameter);
889 if (gtk_accel_map_lookup_entry (accel_path, NULL))
890 gtk_accel_map_change_entry (accel_path, accel_key, accel_mods, TRUE);
892 gtk_accel_map_add_entry (accel_path, accel_key, accel_mods);
898 * gtk_application_remove_accelerator:
899 * @application: a #GtkApplication
900 * @action_name: the name of the action to activate
901 * @parameter: (allow-none): parameter to pass when activating the action,
902 * or %NULL if the action does not accept an activation parameter
904 * Removes an accelerator that has been previously added
905 * with gtk_application_add_accelerator().
910 gtk_application_remove_accelerator (GtkApplication *application,
911 const gchar *action_name,
916 g_return_if_fail (GTK_IS_APPLICATION (application));
918 accel_path = _gtk_accel_path_for_action (action_name, parameter);
920 if (!gtk_accel_map_lookup_entry (accel_path, NULL))
922 g_warning ("No accelerator found for '%s'\n", accel_path);
927 gtk_accel_map_change_entry (accel_path, 0, 0, FALSE);
932 * gtk_application_set_app_menu:
933 * @application: a #GtkApplication
934 * @model: (allow-none): a #GMenuModel, or %NULL
936 * Sets or unsets the application menu for @application.
938 * The application menu is a single menu containing items that typically
939 * impact the application as a whole, rather than acting on a specific
940 * window or document. For example, you would expect to see
941 * "Preferences" or "Quit" in an application menu, but not "Save" or
944 * If supported, the application menu will be rendered by the desktop
950 gtk_application_set_app_menu (GtkApplication *application,
953 g_object_set (application, "app-menu", model, NULL);
957 * gtk_application_get_app_menu:
958 * @application: a #GtkApplication
960 * Returns the menu model that has been set with
961 * g_application_set_app_menu().
963 * Returns: (transfer none): the application menu of @application
968 gtk_application_get_app_menu (GtkApplication *application)
970 GMenuModel *app_menu = NULL;
972 g_object_get (application, "app-menu", &app_menu, NULL);
975 g_object_unref (app_menu);
981 * gtk_application_set_menubar:
982 * @application: a #GtkApplication
983 * @model: (allow-none): a #GMenuModel, or %NULL
985 * Sets or unsets the menubar for windows of @application.
987 * This is a menubar in the traditional sense.
989 * Depending on the desktop environment, this may appear at the top of
990 * each window, or at the top of the screen. In some environments, if
991 * both the application menu and the menubar are set, the application
992 * menu will be presented as if it were the first item of the menubar.
993 * Other environments treat the two as completely separate -- for
994 * example, the application menu may be rendered by the desktop shell
995 * while the menubar (if set) remains in each individual window.
1000 gtk_application_set_menubar (GtkApplication *application,
1003 g_object_set (application, "menubar", model, NULL);
1007 * gtk_application_get_menubar:
1008 * @application: a #GtkApplication
1010 * Returns the menu model that has been set with
1011 * g_application_set_menubar().
1013 * Returns: (transfer none): the menubar for windows of @application
1018 gtk_application_get_menubar (GtkApplication *application)
1020 GMenuModel *menubar = NULL;
1022 g_object_get (application, "menubar", &menubar, NULL);
1025 g_object_unref (menubar);
1030 #if defined(GDK_WINDOWING_X11)
1032 /* D-Bus Session Management
1034 * The protocol and the D-Bus API are described here:
1035 * http://live.gnome.org/SessionManagement/GnomeSession
1036 * http://people.gnome.org/~mccann/gnome-session/docs/gnome-session.html
1040 unregister_client (GtkApplication *app)
1042 GError *error = NULL;
1044 g_debug ("Unregistering client");
1046 g_dbus_proxy_call_sync (app->priv->sm_proxy,
1048 g_variant_new ("(o)", app->priv->client_path),
1049 G_DBUS_CALL_FLAGS_NONE,
1056 g_warning ("Failed to unregister client: %s", error->message);
1057 g_error_free (error);
1060 g_clear_object (&app->priv->client_proxy);
1062 g_free (app->priv->client_path);
1063 app->priv->client_path = NULL;
1067 client_proxy_signal (GDBusProxy *proxy,
1068 const gchar *sender_name,
1069 const gchar *signal_name,
1070 GVariant *parameters,
1071 GtkApplication *app)
1073 if (strcmp (signal_name, "QueryEndSession") == 0)
1075 g_debug ("Received QueryEndSession");
1076 app->priv->quit_requested = TRUE;
1077 g_signal_emit (app, gtk_application_signals[QUIT_REQUESTED], 0);
1079 else if (strcmp (signal_name, "EndSession") == 0)
1081 g_debug ("Received EndSession");
1082 app->priv->quit_requested = TRUE;
1083 gtk_application_quit_response (app, TRUE, NULL);
1084 unregister_client (app);
1085 g_signal_emit (app, gtk_application_signals[QUIT], 0);
1087 else if (strcmp (signal_name, "CancelEndSession") == 0)
1089 g_debug ("Received CancelEndSession");
1090 g_signal_emit (app, gtk_application_signals[QUIT_CANCELLED], 0);
1092 else if (strcmp (signal_name, "Stop") == 0)
1094 g_debug ("Received Stop");
1095 unregister_client (app);
1096 g_signal_emit (app, gtk_application_signals[QUIT], 0);
1101 gtk_application_startup_session_dbus (GtkApplication *app)
1103 static gchar *client_id;
1104 GError *error = NULL;
1107 if (app->priv->session_bus == NULL)
1110 if (client_id == NULL)
1112 const gchar *desktop_autostart_id;
1114 desktop_autostart_id = g_getenv ("DESKTOP_AUTOSTART_ID");
1115 /* Unset DESKTOP_AUTOSTART_ID in order to avoid child processes to
1116 * use the same client id.
1118 g_unsetenv ("DESKTOP_AUTOSTART_ID");
1119 client_id = g_strdup (desktop_autostart_id ? desktop_autostart_id : "");
1122 g_debug ("Connecting to session manager");
1124 app->priv->sm_proxy = g_dbus_proxy_new_sync (app->priv->session_bus,
1125 G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES |
1126 G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS,
1128 "org.gnome.SessionManager",
1129 "/org/gnome/SessionManager",
1130 "org.gnome.SessionManager",
1135 g_warning ("Failed to get a session proxy: %s", error->message);
1136 g_error_free (error);
1140 /* FIXME: should we reuse the D-Bus application id here ? */
1141 app->priv->app_id = g_strdup (g_get_prgname ());
1143 if (!app->priv->register_session)
1146 g_debug ("Registering client '%s' '%s'", app->priv->app_id, client_id);
1148 res = g_dbus_proxy_call_sync (app->priv->sm_proxy,
1150 g_variant_new ("(ss)", app->priv->app_id, client_id),
1151 G_DBUS_CALL_FLAGS_NONE,
1158 g_warning ("Failed to register client: %s", error->message);
1159 g_error_free (error);
1160 g_clear_object (&app->priv->sm_proxy);
1164 g_variant_get (res, "(o)", &app->priv->client_path);
1165 g_variant_unref (res);
1167 g_debug ("Registered client at '%s'", app->priv->client_path);
1169 app->priv->client_proxy = g_dbus_proxy_new_sync (app->priv->session_bus, 0,
1171 "org.gnome.SessionManager",
1172 app->priv->client_path,
1173 "org.gnome.SessionManager.ClientPrivate",
1178 g_warning ("Failed to get client proxy: %s", error->message);
1179 g_error_free (error);
1180 g_clear_object (&app->priv->sm_proxy);
1181 g_free (app->priv->client_path);
1182 app->priv->client_path = NULL;
1186 g_signal_connect (app->priv->client_proxy, "g-signal", G_CALLBACK (client_proxy_signal), app);
1190 * gtk_application_quit_response:
1191 * @application: the #GtkApplication
1192 * @will_quit: whether the application agrees to quit
1193 * @reason: (allow-none): a short human-readable string that explains
1194 * why quitting is not possible
1196 * This function <emphasis>must</emphasis> be called in response to the
1197 * #GtkApplication::quit-requested signal, to indicate whether or
1198 * not the application is willing to quit. The application may call
1199 * it either directly from the signal handler, or at some later point.
1201 * It should be stressed that <emphasis>applications should not assume
1202 * that they have the ability to block logout or shutdown</emphasis>,
1203 * even when %FALSE is passed for @will_quit.
1205 * After calling this method, the application should wait to receive
1206 * either #GtkApplication::quit-cancelled or #GtkApplication::quit.
1208 * If the application does not connect to #GtkApplication::quit-requested,
1209 * #GtkApplication will call this method on its behalf (passing %TRUE
1215 gtk_application_quit_response (GtkApplication *application,
1217 const gchar *reason)
1219 g_return_if_fail (GTK_IS_APPLICATION (application));
1220 g_return_if_fail (!g_application_get_is_remote (G_APPLICATION (application)));
1221 g_return_if_fail (application->priv->client_proxy != NULL);
1222 g_return_if_fail (application->priv->quit_requested);
1224 application->priv->quit_requested = FALSE;
1226 g_debug ("Calling EndSessionResponse %d '%s'", will_quit, reason);
1228 g_dbus_proxy_call (application->priv->client_proxy,
1229 "EndSessionResponse",
1230 g_variant_new ("(bs)", will_quit, reason ? reason : ""),
1231 G_DBUS_CALL_FLAGS_NONE,
1237 * GtkApplicationInhibitFlags:
1238 * @GTK_APPLICATION_INHIBIT_LOGOUT: Inhibit logging out (including shutdown
1240 * @GTK_APPLICATION_INHIBIT_SWITCH: Inhibit user switching
1241 * @GTK_APPLICATION_INHIBIT_SUSPEND: Inhibit suspending the
1242 * session or computer
1243 * @GTK_APPLICATION_INHIBIT_IDLE: Inhibit the session being
1244 * marked as idle (and possibly locked)
1246 * Types of user actions that may be blocked by gtk_application_inhibit().
1252 * gtk_application_inhibit:
1253 * @application: the #GApplication
1254 * @window: (allow-none): a #GtkWindow, or %NULL
1255 * @flags: what types of actions should be inhibited
1256 * @reason: (allow-none): a short, human-readable string that explains
1257 * why these operations are inhibited
1259 * Inform the session manager that certain types of actions should be
1260 * inhibited. This is not guaranteed to work on all platforms and for
1261 * all types of actions.
1263 * Applications should invoke this method when they begin an operation
1264 * that should not be interrupted, such as creating a CD or DVD. The
1265 * types of actions that may be blocked are specified by the @flags
1266 * parameter. When the application completes the operation it should
1267 * call g_application_uninhibit() to remove the inhibitor.
1268 * Inhibitors are also cleared when the application exits.
1270 * Applications should not expect that they will always be able to block
1271 * the action. In most cases, users will be given the option to force
1272 * the action to take place.
1274 * Reasons should be short and to the point.
1276 * If a window is passed, the session manager may point the user to
1277 * this window to find out more about why the action is inhibited.
1279 * Returns: A non-zero cookie that is used to uniquely identify this
1280 * request. It should be used as an argument to g_application_uninhibit()
1281 * in order to remove the request. If the platform does not support
1282 * inhibiting or the request failed for some reason, 0 is returned.
1287 gtk_application_inhibit (GtkApplication *application,
1289 GtkApplicationInhibitFlags flags,
1290 const gchar *reason)
1293 GError *error = NULL;
1297 g_return_val_if_fail (GTK_IS_APPLICATION (application), 0);
1298 g_return_val_if_fail (!g_application_get_is_remote (G_APPLICATION (application)), 0);
1299 g_return_val_if_fail (application->priv->sm_proxy != NULL, 0);
1302 xid = GDK_WINDOW_XID (gtk_widget_get_window (GTK_WIDGET (window)));
1306 res = g_dbus_proxy_call_sync (application->priv->sm_proxy,
1308 g_variant_new ("(susu)",
1309 application->priv->app_id,
1313 G_DBUS_CALL_FLAGS_NONE,
1319 g_warning ("Calling Inhibit failed: %s\n", error->message);
1320 g_error_free (error);
1324 g_variant_get (res, "(u)", &cookie);
1325 g_variant_unref (res);
1331 * gtk_application_uninhibit:
1332 * @application: the #GApplication
1333 * @cookie: a cookie that was returned by g_application_inhibit()
1335 * Removes an inhibitor that has been established with g_application_inhibit().
1336 * Inhibitors are also cleared when the application exits.
1341 gtk_application_uninhibit (GtkApplication *application,
1344 g_return_if_fail (GTK_IS_APPLICATION (application));
1345 g_return_if_fail (!g_application_get_is_remote (G_APPLICATION (application)));
1346 g_return_if_fail (application->priv->sm_proxy != NULL);
1348 g_dbus_proxy_call (application->priv->sm_proxy,
1350 g_variant_new ("(u)", cookie),
1351 G_DBUS_CALL_FLAGS_NONE,
1357 * gtk_application_is_inhibited:
1358 * @application: the #GApplication
1359 * @flags: what types of actions should be queried
1361 * Determines if any of the actions specified in @flags are
1362 * currently inhibited (possibly by another application).
1364 * Returns: %TRUE if any of the actions specified in @flags are inhibited
1369 gtk_application_is_inhibited (GtkApplication *application,
1370 GtkApplicationInhibitFlags flags)
1373 GError *error = NULL;
1376 g_return_val_if_fail (GTK_IS_APPLICATION (application), FALSE);
1377 g_return_val_if_fail (!g_application_get_is_remote (G_APPLICATION (application)), FALSE);
1378 g_return_val_if_fail (application->priv->sm_proxy != NULL, FALSE);
1380 res = g_dbus_proxy_call_sync (application->priv->sm_proxy,
1382 g_variant_new ("(u)", flags),
1383 G_DBUS_CALL_FLAGS_NONE,
1389 g_warning ("Calling IsInhibited failed: %s\n", error->message);
1390 g_error_free (error);
1394 g_variant_get (res, "(b)", &inhibited);
1395 g_variant_unref (res);
1401 * GtkApplicationEndStyle:
1402 * @GTK_APPLICATION_LOGOUT: End the session by logging out.
1403 * @GTK_APPLICATION_REBOOT: Restart the computer.
1404 * @GTK_APPLICATION_SHUTDOWN: Shut the computer down.
1406 * Different ways to end a user session, for use with
1407 * gtk_application_end_session().
1411 * gtk_application_end_session:
1412 * @application: the #GtkApplication
1413 * @style: the desired kind of session end
1414 * @request_confirmation: whether or not the user should get a chance
1415 * to confirm the action
1417 * Requests that the session manager end the current session.
1418 * @style indicates how the session should be ended, and
1419 * @request_confirmation indicates whether or not the user should be
1420 * given a chance to confirm the action. Both of these flags are merely
1421 * hints though; the session manager may choose to ignore them.
1423 * Return value: %TRUE if the request was sent; %FALSE if it could not
1424 * be sent (eg, because it could not connect to the session manager)
1429 gtk_application_end_session (GtkApplication *application,
1430 GtkApplicationEndStyle style,
1431 gboolean request_confirmation)
1433 g_return_val_if_fail (GTK_IS_APPLICATION (application), FALSE);
1434 g_return_val_if_fail (!g_application_get_is_remote (G_APPLICATION (application)), FALSE);
1435 g_return_val_if_fail (application->priv->sm_proxy != NULL, FALSE);
1439 case GTK_APPLICATION_LOGOUT:
1440 g_dbus_proxy_call (application->priv->sm_proxy,
1442 g_variant_new ("(u)", request_confirmation ? 0 : 1),
1443 G_DBUS_CALL_FLAGS_NONE,
1447 case GTK_APPLICATION_REBOOT:
1448 case GTK_APPLICATION_SHUTDOWN:
1449 g_dbus_proxy_call (application->priv->sm_proxy,
1452 G_DBUS_CALL_FLAGS_NONE,
1461 #elif defined(GDK_WINDOWING_QUARTZ)
1463 /* OS X implementation copied from EggSMClient */
1466 idle_quit_requested (gpointer client)
1468 g_signal_emit (client, gtk_application_signals[QUIT_REQUESTED], 0);
1474 quit_requested (const AppleEvent *aevt,
1478 GtkApplication *app = GSIZE_TO_POINTER ((gsize)refcon);
1480 g_return_val_if_fail (!app->priv->quit_requested, userCanceledErr);
1482 /* FIXME AEInteractWithUser? */
1483 osx->quit_requested = TRUE;
1484 AEDuplicateDesc (aevt, &app->priv->quit_event);
1485 AEDuplicateDesc (reply, &app->priv->quit_reply);
1486 AESuspendTheCurrentEvent (aevt);
1488 /* Don't emit the "quit_requested" signal immediately, since we're
1489 * called from a weird point in the guts of gdkeventloop-quartz.c
1491 g_idle_add (idle_quit_requested, app);
1497 gtk_application_startup_session_quartz (GtkApplication *app)
1499 if (app->priv->register_session)
1500 AEInstallEventHandler (kCoreEventClass, kAEQuitApplication,
1501 NewAEEventHandlerUPP (quit_requested),
1502 (long)GPOINTER_TO_SIZE (app), false);
1506 quit_requested_resumed (const AppleEvent *aevt,
1510 GtkApplication *app = GSIZE_TO_POINTER ((gsize)refcon);
1512 app->priv->quit_requested = FALSE;
1514 return app->priv->quitting ? noErr : userCanceledErr;
1518 idle_will_quit (gpointer data)
1520 GtkApplication *app = data;
1522 /* Resume the event with a new handler that will return
1523 * a value to the system
1525 AEResumeTheCurrentEvent (&app->priv->quit_event, &app->priv->quit_reply,
1526 NewAEEventHandlerUPP (quit_requested_resumed),
1527 (long)GPOINTER_TO_SIZE (app));
1529 AEDisposeDesc (&app->quit->quit_event);
1530 AEDisposeDesc (&app->quit->quit_reply);
1532 if (app->priv->quitting)
1533 g_signal_emit (app, gtk_application_signals[QUIT], 0);
1539 gtk_application_quit_response (GtkApplication *application,
1541 const gchar *reason)
1543 g_return_if_fail (GTK_IS_APPLICATION (application));
1544 g_return_if_fail (!g_application_get_is_remote (G_APPLICATION (application)));
1545 g_return_if_fail (application->priv->quit_requested);
1547 application->priv->quitting = will_quit;
1549 /* Finish in an idle handler since the caller might have called
1550 * gtk_application_quit_response() from inside the ::quit-requested
1551 * signal handler, but may not expect the ::quit signal to arrive
1552 * during the gtk_application_quit_response() call.
1554 g_idle_add (idle_will_quit, application);
1558 gtk_application_inhibit (GtkApplication *application,
1560 GtkApplicationInhibitFlags flags,
1561 const gchar *reason)
1567 gtk_application_uninhibit (GtkApplication *application,
1573 gtk_application_is_inhibited (GtkApplication *application,
1574 GtkApplicationInhibitFlags flags)
1580 gtk_application_end_session (GtkApplication *application,
1581 GtkApplicationEndStyle *style,
1582 gboolean request_confirmation)
1584 static const ProcessSerialNumber loginwindow_psn = { 0, kSystemProcess };
1585 AppleEvent event = { typeNull, NULL };
1586 AppleEvent reply = { typeNull, NULL };
1587 AEAddressDesc target;
1593 case GTK_APPLICATION_LOGOUT:
1594 id = request_confirmation ? kAELogOut : kAEReallyLogOut;
1596 case GTK_APPLICATION_REBOOT:
1597 id = request_confirmation ? kAEShowRestartDialog : kAERestart;
1599 case GTK_APPLICATION_SHUTDOWN:
1600 id = request_confirmation ? kAEShowShutdownDialog : kAEShutDown;
1604 err = AECreateDesc (typeProcessSerialNumber, &loginwindow_psn,
1605 sizeof (loginwindow_psn), &target);
1608 g_warning ("Could not create descriptor for loginwindow: %d", err);
1612 err = AECreateAppleEvent (kCoreEventClass, id, &target,
1613 kAutoGenerateReturnID, kAnyTransactionID,
1615 AEDisposeDesc (&target);
1618 g_warning ("Could not create logout AppleEvent: %d", err);
1622 err = AESend (&event, &reply, kAENoReply, kAENormalPriority,
1623 kAEDefaultTimeout, NULL, NULL);
1624 AEDisposeDesc (&event);
1626 AEDisposeDesc (&reply);
1628 return err == noErr;
1633 /* Trivial implementation.
1635 * For the inhibit API on Windows, see
1636 * http://msdn.microsoft.com/en-us/library/ms700677%28VS.85%29.aspx
1640 gtk_application_quit_response (GtkApplication *application,
1642 const gchar *reason)
1647 gtk_application_inhibit (GtkApplication *application,
1649 GtkApplicationInhibitFlags flags,
1650 const gchar *reason)
1656 gtk_application_uninhibit (GtkApplication *application,
1662 gtk_application_is_inhibited (GtkApplication *application,
1663 GtkApplicationInhibitFlags flags)
1669 gtk_application_end_session (GtkApplication *application,
1670 GtkApplicationEndStyle *style,
1671 gboolean request_confirmation)