From 0af108211c4f5d3511b085313587e8d5541e51bb Mon Sep 17 00:00:00 2001 From: Colin Walters Date: Sat, 3 Sep 2011 10:32:06 -0400 Subject: [PATCH] apps: Ensure running apps override new .desktop file data This patch fixes the "apps vanish from alt-TAB bug". If a "package system" rips away and possibly replaces .desktop files at some random time, we have historically used inotify to detect this and reread state (in a racy way, but...). In GNOME 2, this was generally not too problematic because the menu widget was totally separate from the list of windows - and the data they operate on was disjoint as well. In GNOME 3 we unify these, and this creates architectural problems because the windows are tied to the app. What this patch tries to do is, when rereading the application state, if we have a running application, we keep that app around instead of making a new instance. This ensures we preserve any state such as the set of open windows. This requires moving the running state into ShellAppSystem. Adjust callers as necessary, and while we're at it drop the unused "contexts" stuff. This is just a somewhat quick band-aid; a REAL fix would require us having low-level control over application installation. As long as we're on top of random broken tar+wget wrappers, it will be gross. A slight future improvement to this patch would add an explicit "merge" between the old and new data. I think probably we always keep around the ShellApp corresponding to a given ID, but replace its GMenuTreeEntry. https://bugzilla.gnome.org/show_bug.cgi?id=657990 --- js/ui/altTab.js | 4 +- js/ui/dash.js | 7 +-- js/ui/panel.js | 5 +- src/Makefile.am | 1 + src/shell-app-system-private.h | 9 ++++ src/shell-app-system.c | 85 +++++++++++++++++++++++++++++- src/shell-app-system.h | 2 + src/shell-app.c | 3 +- src/shell-window-tracker-private.h | 2 - src/shell-window-tracker.c | 75 +------------------------- src/shell-window-tracker.h | 3 -- 11 files changed, 105 insertions(+), 91 deletions(-) create mode 100644 src/shell-app-system-private.h diff --git a/js/ui/altTab.js b/js/ui/altTab.js index b1a12eb15..0d2ae3960 100644 --- a/js/ui/altTab.js +++ b/js/ui/altTab.js @@ -122,8 +122,8 @@ AltTabPopup.prototype = { }, show : function(backward, binding) { - let tracker = Shell.WindowTracker.get_default(); - let apps = tracker.get_running_apps (''); + let appSys = Shell.AppSystem.get_default(); + let apps = appSys.get_running (); if (!apps.length) return false; diff --git a/js/ui/dash.js b/js/ui/dash.js index 77d4eb4a0..e96ae9eeb 100644 --- a/js/ui/dash.js +++ b/js/ui/dash.js @@ -275,7 +275,7 @@ Dash.prototype = { this._appSystem.connect('installed-changed', Lang.bind(this, this._queueRedisplay)); AppFavorites.getAppFavorites().connect('changed', Lang.bind(this, this._queueRedisplay)); - this._tracker.connect('app-state-changed', Lang.bind(this, this._queueRedisplay)); + this._appSystem.connect('app-state-changed', Lang.bind(this, this._queueRedisplay)); Main.overview.connect('item-drag-begin', Lang.bind(this, this._onDragBegin)); @@ -471,10 +471,7 @@ Dash.prototype = { _redisplay: function () { let favorites = AppFavorites.getAppFavorites().getFavoriteMap(); - /* hardcode here pending some design about how exactly desktop contexts behave */ - let contextId = ''; - - let running = this._tracker.get_running_apps(contextId); + let running = this._appSystem.get_running(); let children = this._box.get_children().filter(function(actor) { return actor._delegate.child && diff --git a/js/ui/panel.js b/js/ui/panel.js index 987ca2b8c..57c5df4aa 100644 --- a/js/ui/panel.js +++ b/js/ui/panel.js @@ -292,8 +292,9 @@ AppMenuButton.prototype = { this._spinner.actor.lower_bottom(); let tracker = Shell.WindowTracker.get_default(); + let appSys = Shell.AppSystem.get_default(); tracker.connect('notify::focus-app', Lang.bind(this, this._sync)); - tracker.connect('app-state-changed', Lang.bind(this, this._onAppStateChanged)); + appSys.connect('app-state-changed', Lang.bind(this, this._onAppStateChanged)); global.window_manager.connect('switch-workspace', Lang.bind(this, this._sync)); @@ -457,7 +458,7 @@ AppMenuButton.prototype = { this._targetApp.request_quit(); }, - _onAppStateChanged: function(tracker, app) { + _onAppStateChanged: function(appSys, app) { let state = app.state; if (state != Shell.AppState.STARTING) { this._startingApps = this._startingApps.filter(function(a) { diff --git a/src/Makefile.am b/src/Makefile.am index 3ff043f61..11449eac5 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -126,6 +126,7 @@ libgnome_shell_la_SOURCES = \ $(shell_built_sources) \ $(shell_public_headers_h) \ shell-app-private.h \ + shell-app-system-private.h \ shell-embedded-window-private.h \ shell-global-private.h \ shell-jsapi-compat-private.h \ diff --git a/src/shell-app-system-private.h b/src/shell-app-system-private.h new file mode 100644 index 000000000..975d563de --- /dev/null +++ b/src/shell-app-system-private.h @@ -0,0 +1,9 @@ +/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */ +#ifndef __SHELL_APP_SYSTEM_PRIVATE_H__ +#define __SHELL_APP_SYSTEM_PRIVATE_H__ + +#include "shell-app-system.h" + +void _shell_app_system_notify_app_state_changed (ShellAppSystem *self, ShellApp *app); + +#endif diff --git a/src/shell-app-system.c b/src/shell-app-system.c index 4bdfc801c..4d3d76ebf 100644 --- a/src/shell-app-system.c +++ b/src/shell-app-system.c @@ -14,6 +14,7 @@ #include "shell-app-private.h" #include "shell-window-tracker-private.h" +#include "shell-app-system-private.h" #include "shell-global.h" #include "shell-util.h" #include "st.h" @@ -32,6 +33,7 @@ enum { }; enum { + APP_STATE_CHANGED, INSTALLED_CHANGED, LAST_SIGNAL }; @@ -43,6 +45,8 @@ struct _ShellAppSystemPrivate { GHashTable *entry_to_app; + GHashTable *running_apps; + GSList *known_vendor_prefixes; GMenuTree *settings_tree; @@ -61,6 +65,14 @@ static void shell_app_system_class_init(ShellAppSystemClass *klass) gobject_class->finalize = shell_app_system_finalize; + signals[APP_STATE_CHANGED] = g_signal_new ("app-state-changed", + SHELL_TYPE_APP_SYSTEM, + G_SIGNAL_RUN_LAST, + 0, + NULL, NULL, + g_cclosure_marshal_VOID__OBJECT, + G_TYPE_NONE, 1, + SHELL_TYPE_APP); signals[INSTALLED_CHANGED] = g_signal_new ("installed-changed", SHELL_TYPE_APP_SYSTEM, @@ -82,6 +94,9 @@ shell_app_system_init (ShellAppSystem *self) SHELL_TYPE_APP_SYSTEM, ShellAppSystemPrivate); + priv->running_apps = g_hash_table_new_full (g_str_hash, g_str_equal, + NULL, (GDestroyNotify) g_object_unref); + priv->entry_to_app = g_hash_table_new_full (NULL, NULL, (GDestroyNotify)gmenu_tree_item_unref, (GDestroyNotify)g_object_unref); @@ -112,6 +127,7 @@ shell_app_system_finalize (GObject *object) g_object_unref (priv->apps_tree); g_object_unref (priv->settings_tree); + g_hash_table_destroy (priv->running_apps); g_hash_table_destroy (priv->entry_to_app); g_hash_table_destroy (priv->setting_entry_to_app); @@ -248,7 +264,14 @@ load_app_entry (ShellAppSystem *self, else g_free (prefix); - app = _shell_app_new (entry); + /* Here we check to see whether the app is still running; if so, we + * keep the old data around. + */ + app = g_hash_table_lookup (self->priv->running_apps, gmenu_tree_entry_get_desktop_file_id (entry)); + if (app != NULL) + app = g_object_ref (app); + else + app = _shell_app_new (entry); g_hash_table_insert (self->priv->entry_to_app, gmenu_tree_item_ref (entry), app); } @@ -492,7 +515,10 @@ ShellApp * shell_app_system_lookup_app_by_tree_entry (ShellAppSystem *self, GMenuTreeEntry *entry) { - return g_hash_table_lookup (self->priv->entry_to_app, entry); + /* If we looked up directly in ->entry_to_app, we'd lose the + * override of running apps. Thus, indirect through the id. + */ + return shell_app_system_lookup_app (self, gmenu_tree_entry_get_desktop_file_id (entry)); } /** @@ -582,12 +608,67 @@ shell_app_system_get_all (ShellAppSystem *self) while (g_hash_table_iter_next (&iter, &key, &value)) { ShellApp *app = value; + if (!g_desktop_app_info_get_nodisplay (shell_app_get_app_info (app))) result = g_slist_prepend (result, app); } return result; } +void +_shell_app_system_notify_app_state_changed (ShellAppSystem *self, + ShellApp *app) +{ + ShellAppState state = shell_app_get_state (app); + + switch (state) + { + case SHELL_APP_STATE_RUNNING: + /* key is owned by the app */ + g_hash_table_insert (self->priv->running_apps, (char*)shell_app_get_id (app), g_object_ref (app)); + break; + case SHELL_APP_STATE_STARTING: + break; + case SHELL_APP_STATE_STOPPED: + g_hash_table_remove (self->priv->running_apps, shell_app_get_id (app)); + break; + } + g_signal_emit (self, signals[APP_STATE_CHANGED], 0, app); +} + +/** + * shell_app_system_get_running: + * @self: A #ShellAppSystem + * + * Returns the set of applications which currently have at least one + * open window in the given context. The returned list will be sorted + * by shell_app_compare(). + * + * Returns: (element-type ShellApp) (transfer container): Active applications + */ +GSList * +shell_app_system_get_running (ShellAppSystem *self) +{ + gpointer key, value; + GSList *ret; + GHashTableIter iter; + + g_hash_table_iter_init (&iter, self->priv->running_apps); + + ret = NULL; + while (g_hash_table_iter_next (&iter, &key, &value)) + { + ShellApp *app = value; + + ret = g_slist_prepend (ret, app); + } + + ret = g_slist_sort (ret, (GCompareFunc)shell_app_compare); + + return ret; +} + + static gint compare_apps_by_name (gconstpointer a, gconstpointer b, diff --git a/src/shell-app-system.h b/src/shell-app-system.h index a2a55f646..c26b72859 100644 --- a/src/shell-app-system.h +++ b/src/shell-app-system.h @@ -53,6 +53,8 @@ ShellApp *shell_app_system_lookup_heuristic_basename (ShellAppSystem * GSList *shell_app_system_get_all (ShellAppSystem *system); +GSList *shell_app_system_get_running (ShellAppSystem *self); + GSList *shell_app_system_initial_search (ShellAppSystem *system, GSList *terms); GSList *shell_app_system_subsearch (ShellAppSystem *system, diff --git a/src/shell-app.c b/src/shell-app.c index 0455f42b4..7535a8199 100644 --- a/src/shell-app.c +++ b/src/shell-app.c @@ -12,6 +12,7 @@ #include "shell-enum-types.h" #include "shell-global.h" #include "shell-util.h" +#include "shell-app-system-private.h" #include "shell-window-tracker-private.h" #include "st.h" @@ -840,7 +841,7 @@ shell_app_state_transition (ShellApp *app, app->running_state = NULL; } - _shell_window_tracker_notify_app_state_changed (shell_window_tracker_get_default (), app); + _shell_app_system_notify_app_state_changed (shell_app_system_get_default (), app); g_object_notify (G_OBJECT (app), "state"); } diff --git a/src/shell-window-tracker-private.h b/src/shell-window-tracker-private.h index 0b60a8896..4307d153a 100644 --- a/src/shell-window-tracker-private.h +++ b/src/shell-window-tracker-private.h @@ -4,8 +4,6 @@ #include "shell-window-tracker.h" -void _shell_window_tracker_notify_app_state_changed (ShellWindowTracker *tracker, ShellApp *self); - void _shell_window_tracker_add_child_process_app (ShellWindowTracker *tracker, GPid pid, ShellApp *app); diff --git a/src/shell-window-tracker.c b/src/shell-window-tracker.c index 3ad7dd4ab..b0251d270 100644 --- a/src/shell-window-tracker.c +++ b/src/shell-window-tracker.c @@ -49,9 +49,6 @@ struct _ShellWindowTracker /* */ GHashTable *window_to_app; - /* */ - GHashTable *running_apps; - /* */ GHashTable *launched_pid_to_app; }; @@ -64,7 +61,6 @@ enum { }; enum { - APP_STATE_CHANGED, STARTUP_SEQUENCE_CHANGED, TRACKED_WINDOWS_CHANGED, @@ -117,14 +113,6 @@ shell_window_tracker_class_init (ShellWindowTrackerClass *klass) SHELL_TYPE_APP, G_PARAM_READABLE)); - signals[APP_STATE_CHANGED] = g_signal_new ("app-state-changed", - SHELL_TYPE_WINDOW_TRACKER, - G_SIGNAL_RUN_LAST, - 0, - NULL, NULL, - g_cclosure_marshal_VOID__OBJECT, - G_TYPE_NONE, 1, - SHELL_TYPE_APP); signals[STARTUP_SEQUENCE_CHANGED] = g_signal_new ("startup-sequence-changed", SHELL_TYPE_WINDOW_TRACKER, G_SIGNAL_RUN_LAST, @@ -579,27 +567,6 @@ init_window_tracking (ShellWindowTracker *self) shell_window_tracker_on_n_workspaces_changed (screen, NULL, self); } -void -_shell_window_tracker_notify_app_state_changed (ShellWindowTracker *self, - ShellApp *app) -{ - ShellAppState state = shell_app_get_state (app); - - switch (state) - { - case SHELL_APP_STATE_RUNNING: - /* key is owned by the app */ - g_hash_table_insert (self->running_apps, (char*)shell_app_get_id (app), app); - break; - case SHELL_APP_STATE_STARTING: - break; - case SHELL_APP_STATE_STOPPED: - g_hash_table_remove (self->running_apps, shell_app_get_id (app)); - break; - } - g_signal_emit (self, signals[APP_STATE_CHANGED], 0, app); -} - static void on_startup_sequence_changed (MetaScreen *screen, SnStartupSequence *sequence, @@ -622,8 +589,6 @@ shell_window_tracker_init (ShellWindowTracker *self) self->window_to_app = g_hash_table_new_full (g_direct_hash, g_direct_equal, NULL, (GDestroyNotify) g_object_unref); - self->running_apps = g_hash_table_new (g_str_hash, g_str_equal); - self->launched_pid_to_app = g_hash_table_new_full (NULL, NULL, NULL, (GDestroyNotify) g_object_unref); screen = shell_global_get_screen (shell_global_get ()); @@ -640,7 +605,6 @@ shell_window_tracker_finalize (GObject *object) { ShellWindowTracker *self = SHELL_WINDOW_TRACKER (object); - g_hash_table_destroy (self->running_apps); g_hash_table_destroy (self->window_to_app); g_hash_table_destroy (self->launched_pid_to_app); @@ -686,7 +650,7 @@ ShellApp * shell_window_tracker_get_app_from_pid (ShellWindowTracker *self, int pid) { - GSList *running = shell_window_tracker_get_running_apps (self, ""); + GSList *running = shell_app_system_get_running (shell_app_system_get_default()); GSList *iter; ShellApp *result = NULL; @@ -716,43 +680,6 @@ shell_window_tracker_get_app_from_pid (ShellWindowTracker *self, return result; } -/** - * shell_window_tracker_get_running_apps: - * @tracker: An app monitor instance - * @context: Activity identifier - * - * Returns the set of applications which currently have at least one open - * window in the given context. The returned list will be sorted - * by shell_app_compare(). - * - * Returns: (element-type ShellApp) (transfer full): Active applications - */ -GSList * -shell_window_tracker_get_running_apps (ShellWindowTracker *tracker, - const char *context) -{ - gpointer key, value; - GSList *ret; - GHashTableIter iter; - - g_hash_table_iter_init (&iter, tracker->running_apps); - - ret = NULL; - while (g_hash_table_iter_next (&iter, &key, &value)) - { - ShellApp *app = value; - - if (strcmp (context, _shell_window_tracker_get_app_context (tracker, app)) != 0) - continue; - - ret = g_slist_prepend (ret, g_object_ref (app)); - } - - ret = g_slist_sort (ret, (GCompareFunc)shell_app_compare); - - return ret; -} - static void on_child_exited (GPid pid, gint status, diff --git a/src/shell-window-tracker.h b/src/shell-window-tracker.h index 62bb5b238..99c4cf618 100644 --- a/src/shell-window-tracker.h +++ b/src/shell-window-tracker.h @@ -31,9 +31,6 @@ GType shell_window_tracker_get_type (void) G_GNUC_CONST; ShellWindowTracker* shell_window_tracker_get_default(void); -GSList * shell_window_tracker_get_running_apps (ShellWindowTracker *tracker, - const char *context); - ShellApp *shell_window_tracker_get_window_app (ShellWindowTracker *tracker, MetaWindow *metawin); ShellApp *shell_window_tracker_get_app_from_pid (ShellWindowTracker *tracker, int pid);