[gnome-shell] Associate process identifiers with applications



commit bf6d0dc8082ca7b2b657714b8cde8a8a9c04363e
Author: Colin Walters <walters verbum org>
Date:   Mon May 24 10:59:52 2010 -0400

    Associate process identifiers with applications
    
    Cache the set of pids for an application, in preparation for
    landing an XSMP patch which requires this information.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=619542

 src/shell-app.c            |   26 ++++++++++++++++++++++++
 src/shell-app.h            |    2 +
 src/shell-window-tracker.c |   47 +++++++++++++++++++++++++------------------
 3 files changed, 55 insertions(+), 20 deletions(-)
---
diff --git a/src/shell-app.c b/src/shell-app.c
index dca21a2..9022b27 100644
--- a/src/shell-app.c
+++ b/src/shell-app.c
@@ -739,6 +739,32 @@ _shell_app_remove_window (ShellApp   *app,
     }
 }
 
+/**
+ * shell_app_get_pids:
+ * @app: a #ShellApp
+ *
+ * Returns: (transfer container) (element-type int): An unordered list of process identifers associated with this application.
+ */
+GSList *
+shell_app_get_pids (ShellApp *app)
+{
+  GSList *result;
+  GSList *iter;
+
+  result = NULL;
+  for (iter = shell_app_get_windows (app); iter; iter = iter->next)
+    {
+      MetaWindow *window = iter->data;
+      int pid = meta_window_get_pid (window);
+      /* Note in the (by far) common case, app will only have one pid, so
+       * we'll hit the first element, so don't worry about O(N^2) here.
+       */
+      if (!g_slist_find (result, GINT_TO_POINTER (pid)))
+        result = g_slist_prepend (result, GINT_TO_POINTER (pid));
+    }
+  return result;
+}
+
 void
 _shell_app_set_starting (ShellApp        *app,
                          gboolean         starting)
diff --git a/src/shell-app.h b/src/shell-app.h
index 22f612f..ce22237 100644
--- a/src/shell-app.h
+++ b/src/shell-app.h
@@ -56,6 +56,8 @@ guint shell_app_get_n_windows (ShellApp *app);
 
 GSList *shell_app_get_windows (ShellApp *app);
 
+GSList *shell_app_get_pids (ShellApp *app);
+
 gboolean shell_app_is_on_workspace (ShellApp *app, MetaWorkspace *workspace);
 
 int shell_app_compare (ShellApp *app, ShellApp *other);
diff --git a/src/shell-window-tracker.c b/src/shell-window-tracker.c
index e2957d9..e9eda24 100644
--- a/src/shell-window-tracker.c
+++ b/src/shell-window-tracker.c
@@ -746,33 +746,40 @@ shell_window_tracker_get_window_app (ShellWindowTracker *monitor,
  *
  * Look up the application corresponding to a process.
  *
- * Returns: (transfer full): A #ShellApp, or %NULL if none
+ * Returns: (transfer none): A #ShellApp, or %NULL if none
  */
 ShellApp *
 shell_window_tracker_get_app_from_pid (ShellWindowTracker *self, 
                                        int                 pid)
 {
-  ShellGlobal *global = shell_global_get ();
-  GList *windows, *iter;
-  
-  windows = shell_global_get_windows (global);
-  for (iter = windows; iter; iter = iter->next)
+  GSList *running = shell_window_tracker_get_running_apps (self, "");
+  GSList *iter;
+  ShellApp *result = NULL;
+
+  for (iter = running; iter; iter = iter->next)
     {
-      MutterWindow *win = iter->data;
-      MetaWindow *metawin;
-      int windowpid;
-      ShellApp *app;
-      
-      metawin = mutter_window_get_meta_window (win);
-      windowpid = meta_window_get_pid (metawin);
-      if (windowpid != pid)
-        continue;
-      
-      app = shell_window_tracker_get_window_app (self, metawin);
-      if (app)
-        return app;
+      ShellApp *app = iter->data;
+      GSList *pids = shell_app_get_pids (app);
+      GSList *pids_iter;
+
+      for (pids_iter = pids; pids_iter; pids_iter = pids_iter->next)
+        {
+          int app_pid = GPOINTER_TO_INT (pids_iter->data);
+          if (app_pid == pid)
+            {
+              result = app;
+              break;
+            }
+        }
+      g_slist_free (pids);
+
+      if (result != NULL)
+        break;
     }
-  return NULL;
+
+  g_slist_free (running);
+
+  return result;
 }
 
 /**



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]