[aed8be046f04a89cfa707bb5c1c52e6f374393507edfb090fbd409810b325bee/wip/hadess/add-inhibit-list] gnome-session-inhibit: Add --list command



commit 71ce23f4327dda368f88eee8f08d1350438322d9
Author: Bastien Nocera <hadess hadess net>
Date:   Wed Jul 8 12:22:09 2020 +0200

    gnome-session-inhibit: Add --list command
    
    Add --list command to make it easy to list the applications currenty
    inhibiting the session, rather than having to poke at introspection
    output.

 doc/man/gnome-session-inhibit.xml |   7 ++
 tools/gnome-session-inhibit.c     | 144 ++++++++++++++++++++++++++++++++++++++
 2 files changed, 151 insertions(+)
---
diff --git a/doc/man/gnome-session-inhibit.xml b/doc/man/gnome-session-inhibit.xml
index d7ed4c8f..1cc03b11 100644
--- a/doc/man/gnome-session-inhibit.xml
+++ b/doc/man/gnome-session-inhibit.xml
@@ -88,6 +88,13 @@ Do not launch COMMAND and wait forever instead
 </para></listitem>
 </varlistentry>
 
+<varlistentry>
+<term><option>-l</option>, <option>--list</option></term>
+<listitem><para>
+list the existing inhibitions and exit
+</para></listitem>
+</varlistentry>
+
 </variablelist>
 </refsect1>
 
diff --git a/tools/gnome-session-inhibit.c b/tools/gnome-session-inhibit.c
index d1d3bc60..a4af0bf9 100644
--- a/tools/gnome-session-inhibit.c
+++ b/tools/gnome-session-inhibit.c
@@ -117,6 +117,7 @@ static void usage (void)
              "  --inhibit ARG     Things to inhibit, colon-separated list of:\n"
              "                    logout, switch-user, suspend, idle, automount\n"
              "  --inhibit-only    Do not launch COMMAND and wait forever instead\n"
+             "  -l, --list        List the existing inhibitions, and exit\n"
              "\n"
              "If no --inhibit option is specified, idle is assumed.\n"),
            g_get_prgname ());
@@ -127,6 +128,139 @@ static void version (void)
   g_print ("%s %s\n", g_get_prgname (), PACKAGE_VERSION);
 }
 
+static GVariant *get_inhibitor_prop (GDBusProxy *inhibitor,
+                                     const char *method_name)
+{
+  g_autoptr(GVariant) variant = NULL;
+  g_autoptr(GError) error = NULL;
+  variant = g_dbus_proxy_call_sync (inhibitor,
+                                    method_name,
+                                    NULL,
+                                    G_DBUS_CALL_FLAGS_NONE,
+                                    -1,
+                                    NULL,
+                                    &error);
+  if (variant == NULL)
+    g_debug ("Failed to get property via '%s': %s",
+             method_name, error->message);
+  return g_variant_get_child_value (variant, 0);
+}
+
+static void
+string_append_comma (GString *s,
+                     const char *value)
+{
+  if (s->len != 0)
+    g_string_append (s, ", ");
+  g_string_append (s, value);
+}
+
+static char *
+flags_to_str (guint32 flags)
+{
+  GString *s;
+
+  s = g_string_new (NULL);
+  if (flags & GSM_INHIBITOR_FLAG_LOGOUT)
+    string_append_comma (s, "logout");
+  if (flags & GSM_INHIBITOR_FLAG_SWITCH_USER)
+    string_append_comma (s, "switch-user");
+  if (flags & GSM_INHIBITOR_FLAG_SUSPEND)
+    string_append_comma (s, "suspend");
+  if (flags & GSM_INHIBITOR_FLAG_IDLE)
+    string_append_comma (s, "idle");
+  if (flags & GSM_INHIBITOR_FLAG_AUTOMOUNT)
+    string_append_comma (s, "automount");
+
+  return g_string_free (s, FALSE);
+}
+
+static void list (void)
+{
+  g_autoptr(GDBusConnection) bus = NULL;
+  g_autoptr(GVariant) ret = NULL;
+  g_autoptr(GVariant) array = NULL;
+  g_autoptr(GError) error = NULL;
+  guint num_children;
+  g_autoptr(GVariantIter) iter = NULL;
+  const char *inhibitor_path;
+
+  bus = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
+
+  if (bus == NULL)
+    {
+      g_warning ("Failed to connect to session bus: %s", error->message);
+      g_error_free (error);
+      return;
+    }
+
+  ret = g_dbus_connection_call_sync (bus,
+                               "org.gnome.SessionManager",
+                               "/org/gnome/SessionManager",
+                               "org.gnome.SessionManager",
+                               "GetInhibitors",
+                               NULL,
+                               NULL,
+                               0,
+                               G_MAXINT,
+                               NULL,
+                               &error);
+
+  if (ret == NULL)
+    {
+      g_warning ("Failed to call GetInhibitors: %s\n", error->message);
+      g_error_free (error);
+      return;
+    }
+
+  array = g_variant_get_child_value (ret, 0);
+
+  num_children = g_variant_n_children (array);
+  if (num_children == 0)
+    {
+      g_print ("No inhibitors\n");
+      return;
+    }
+
+  g_variant_get (array, "ao", &iter);
+  while (g_variant_iter_loop (iter, "&o", &inhibitor_path))
+    {
+      g_autoptr(GDBusProxy) inhibitor = NULL;
+      g_autoptr(GVariant) app_id = NULL;
+      g_autoptr(GVariant) reason = NULL;
+      g_autoptr(GVariant) flags = NULL;
+      g_autofree char *flags_str = NULL;
+
+      inhibitor = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SESSION,
+                                                 G_DBUS_PROXY_FLAGS_NONE,
+                                                 NULL,
+                                                 "org.gnome.SessionManager",
+                                                 inhibitor_path,
+                                                 "org.gnome.SessionManager.Inhibitor",
+                                                 NULL,
+                                                 &error);
+
+      /* Skip inhibitors that might have disappeared */
+      if (!inhibitor)
+        {
+          g_debug ("Failed to get proxy for %s: %s",
+                   inhibitor_path,
+                   error->message);
+          continue;
+        }
+
+      app_id = get_inhibitor_prop (inhibitor, "GetAppId");
+      reason = get_inhibitor_prop (inhibitor, "GetReason");
+      flags = get_inhibitor_prop (inhibitor, "GetFlags");
+      flags_str = flags_to_str (g_variant_get_uint32 (flags));
+
+      g_print ("%s: %s (%s)\n",
+               g_variant_get_string (app_id, NULL),
+               g_variant_get_string (reason, NULL),
+               flags_str);
+    }
+}
+
 static void
 wait_for_child_app (char **argv)
 {
@@ -175,6 +309,7 @@ int main (int argc, char *argv[])
   GsmInhibitorFlag inhibit_flags = 0;
   gboolean show_help = FALSE;
   gboolean show_version = FALSE;
+  gboolean show_list = FALSE;
   gboolean no_launch = FALSE;
   gint i;
   const gchar *app_id = "unknown";
@@ -194,6 +329,9 @@ int main (int argc, char *argv[])
       if (strcmp (argv[i], "--help") == 0 ||
           strcmp (argv[i], "-h") == 0)
         show_help = TRUE;
+      if (strcmp (argv[i], "--list") == 0 ||
+          strcmp (argv[i], "-l") == 0)
+        show_list = TRUE;
       else if (strcmp (argv[i], "--version") == 0)
         show_version = TRUE;
       else if (strcmp (argv[i], "--inhibit-only") == 0)
@@ -238,6 +376,12 @@ int main (int argc, char *argv[])
       return 0;
     }
 
+  if (show_list)
+    {
+      list ();
+      return 0;
+    }
+
   if (show_help || (i == argc && !no_launch))
     {
       usage ();


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