[Xfce4-commits] [xfce/xfce4-power-manager] 01/01: Add support for ConsoleKit2

noreply at xfce.org noreply at xfce.org
Sat Feb 14 14:37:22 CET 2015


This is an automated email from the git hooks/post-receive script.

eric pushed a commit to branch master
in repository xfce/xfce4-power-manager.

commit 555268f2c2f7fb94774c863dc3108f9e4010f6a2
Author: Eric Koegel <eric.koegel at gmail.com>
Date:   Mon Nov 3 07:54:41 2014 +0300

    Add support for ConsoleKit2
    
    ConsoleKit2 implements the suspend/hibernate DBUS API. This patch
    uses it if ConsoleKit2 is running and reports the system as capable
    of suspend or hibernate.
---
 common/xfpm-power-common.h |    2 +
 src/xfpm-console-kit.c     |  109 +++++++++++++++++++++++++++++++++++++++-----
 src/xfpm-console-kit.h     |    6 +++
 src/xfpm-power.c           |   90 ++++++++++++++++++++++++++++++++++--
 4 files changed, 191 insertions(+), 16 deletions(-)

diff --git a/common/xfpm-power-common.h b/common/xfpm-power-common.h
index a18f486..58b1333 100644
--- a/common/xfpm-power-common.h
+++ b/common/xfpm-power-common.h
@@ -42,6 +42,8 @@
 #define POLKIT_AUTH_SUSPEND_XFPM	"org.xfce.power.xfce4-pm-helper"
 #define POLKIT_AUTH_HIBERNATE_XFPM	"org.xfce.power.xfce4-pm-helper"
 
+#define POLKIT_AUTH_SUSPEND_CONSOLEKIT2   "org.freedesktop.consolekit.system.suspend"
+#define POLKIT_AUTH_HIBERNATE_CONSOLEKIT2 "org.freedesktop.consolekit.system.hibernate"
 
 const gchar *xfpm_power_translate_device_type (guint type);
 
diff --git a/src/xfpm-console-kit.c b/src/xfpm-console-kit.c
index 1796949..cc0e4da 100644
--- a/src/xfpm-console-kit.c
+++ b/src/xfpm-console-kit.c
@@ -55,13 +55,17 @@ struct XfpmConsoleKitPrivate
     
     gboolean	     can_shutdown;
     gboolean	     can_restart;
+    gboolean         can_suspend;
+    gboolean         can_hibernate;
 };
 
 enum
 {
     PROP_0,
     PROP_CAN_RESTART,
-    PROP_CAN_SHUTDOWN
+    PROP_CAN_SHUTDOWN,
+    PROP_CAN_SUSPEND,
+    PROP_CAN_HIBERNATE
 };
 
 G_DEFINE_TYPE (XfpmConsoleKit, xfpm_console_kit, G_TYPE_OBJECT)
@@ -70,7 +74,8 @@ static void
 xfpm_console_kit_get_info (XfpmConsoleKit *console)
 {
     GError *error = NULL;
-    
+    gchar *tmp = NULL;
+
     dbus_g_proxy_call (console->priv->proxy, "CanStop", &error,
 		       G_TYPE_INVALID,
 		       G_TYPE_BOOLEAN, &console->priv->can_shutdown,
@@ -79,8 +84,7 @@ xfpm_console_kit_get_info (XfpmConsoleKit *console)
     if ( error )
     {
 	g_warning ("'CanStop' method failed : %s", error->message);
-	g_error_free (error);
-	error = NULL;
+	g_clear_error (&error);
     }
     
     dbus_g_proxy_call (console->priv->proxy, "CanRestart", &error,
@@ -91,10 +95,50 @@ xfpm_console_kit_get_info (XfpmConsoleKit *console)
     if ( error )
     {
 	g_warning ("'CanRestart' method failed : %s", error->message);
-	g_error_free (error);
-	error = NULL;
+	g_clear_error (&error);
+    }
+
+    /* start with FALSE */
+    console->priv->can_suspend = FALSE;
+
+    dbus_g_proxy_call (console->priv->proxy, "CanSuspend", &error,
+		       G_TYPE_INVALID,
+		       G_TYPE_STRING, &tmp,
+		       G_TYPE_INVALID);
+
+    if ( error )
+    {
+	g_debug ("'CanSuspend' method failed : %s", error->message);
+	g_clear_error (&error);
+    }
+    else
+    {
+	if (g_strcmp0 (tmp, "yes") == 0 || g_strcmp0 (tmp, "challenge") == 0)
+	{
+	    console->priv->can_suspend = TRUE;
+	}
+    }
+
+    /* start with FALSE */
+    console->priv->can_hibernate = FALSE;
+
+    dbus_g_proxy_call (console->priv->proxy, "CanHibernate", &error,
+		       G_TYPE_INVALID,
+		       G_TYPE_STRING, &tmp,
+		       G_TYPE_INVALID);
+
+    if ( error )
+    {
+	g_debug ("'CanHibernate' method failed : %s", error->message);
+	g_clear_error (&error);
+    }
+    else
+    {
+	if (g_strcmp0 (tmp, "yes") == 0 || g_strcmp0 (tmp, "challenge") == 0)
+	{
+	    console->priv->can_hibernate = TRUE;
+	}
     }
-    
 }
 
 static void
@@ -120,6 +164,20 @@ xfpm_console_kit_class_init (XfpmConsoleKitClass *klass)
                                                            FALSE,
                                                            G_PARAM_READABLE));
 
+    g_object_class_install_property (object_class,
+                                     PROP_CAN_SUSPEND,
+                                     g_param_spec_boolean ("can-suspend",
+                                                           NULL, NULL,
+                                                           FALSE,
+                                                           G_PARAM_READABLE));
+
+    g_object_class_install_property (object_class,
+                                     PROP_CAN_HIBERNATE,
+                                     g_param_spec_boolean ("can-hibernate",
+                                                           NULL, NULL,
+                                                           FALSE,
+                                                           G_PARAM_READABLE));
+
     g_type_class_add_private (klass, sizeof (XfpmConsoleKitPrivate));
 }
 
@@ -141,7 +199,7 @@ xfpm_console_kit_init (XfpmConsoleKit *console)
     {
 	g_critical ("Unable to get system bus connection : %s", error->message);
 	g_error_free (error);
-	goto out;
+	return;
     }
     
     console->priv->proxy = dbus_g_proxy_new_for_name_owner (console->priv->bus,
@@ -153,13 +211,10 @@ xfpm_console_kit_init (XfpmConsoleKit *console)
     if ( !console->priv->proxy )
     {
 	g_warning ("Unable to create proxy for 'org.freedesktop.ConsoleKit'");
-	goto out;
+	return;
     }
     
     xfpm_console_kit_get_info (console);
-    
-out:
-    ;
 }
 
 static void xfpm_console_kit_get_property (GObject *object,
@@ -178,6 +233,12 @@ static void xfpm_console_kit_get_property (GObject *object,
 	case PROP_CAN_RESTART:
 	    g_value_set_boolean (value, console->priv->can_restart);
 	    break;
+	case PROP_CAN_SUSPEND:
+	    g_value_set_boolean (value, console->priv->can_suspend);
+	    break;
+	case PROP_CAN_HIBERNATE:
+	    g_value_set_boolean (value, console->priv->can_hibernate);
+	    break;
         default:
             G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
             break;
@@ -238,3 +299,27 @@ void xfpm_console_kit_reboot (XfpmConsoleKit *console, GError **error)
 		       G_TYPE_INVALID);
     
 }
+
+void
+xfpm_console_kit_suspend (XfpmConsoleKit *console,
+                          GError        **error)
+{
+    g_return_if_fail (console->priv->proxy != NULL );
+
+    dbus_g_proxy_call (console->priv->proxy, "Suspend", error,
+		       G_TYPE_INVALID,
+		       G_TYPE_BOOLEAN, TRUE,
+		       G_TYPE_INVALID);
+}
+
+void
+xfpm_console_kit_hibernate (XfpmConsoleKit *console,
+                            GError        **error)
+{
+    g_return_if_fail (console->priv->proxy != NULL );
+
+    dbus_g_proxy_call (console->priv->proxy, "Hibernate", error,
+		       G_TYPE_INVALID,
+		       G_TYPE_BOOLEAN, TRUE,
+		       G_TYPE_INVALID);
+}
diff --git a/src/xfpm-console-kit.h b/src/xfpm-console-kit.h
index 42bf420..c48bf8c 100644
--- a/src/xfpm-console-kit.h
+++ b/src/xfpm-console-kit.h
@@ -54,6 +54,12 @@ void				xfpm_console_kit_shutdown	 (XfpmConsoleKit *console,
 void				xfpm_console_kit_reboot		 (XfpmConsoleKit *console,
 								  GError **error);
 
+void                xfpm_console_kit_suspend     (XfpmConsoleKit *console,
+                                                  GError        **error);
+
+void                xfpm_console_kit_hibernate   (XfpmConsoleKit *console,
+                                                  GError        **error);
+
 G_END_DECLS
 
 #endif /* __XFPM_CONSOLE_KIT_H */
diff --git a/src/xfpm-power.c b/src/xfpm-power.c
index b89d343..07085ff 100644
--- a/src/xfpm-power.c
+++ b/src/xfpm-power.c
@@ -169,6 +169,40 @@ static guint signals [LAST_SIGNAL] = { 0 };
 
 G_DEFINE_TYPE (XfpmPower, xfpm_power, G_TYPE_OBJECT)
 
+
+/* This checks if consolekit returns TRUE for either suspend or
+ * hibernate showing support. This means that ConsoleKit2 is running
+ * (and the system is capable of those actions).
+ */
+static gboolean
+check_for_consolekit2 (XfpmPower *power)
+{
+    XfpmConsoleKit *console;
+    gboolean can_suspend, can_hibernate;
+
+    g_return_val_if_fail (XFPM_IS_POWER (power), FALSE);
+
+    if (power->priv->console == NULL)
+	return FALSE;
+
+    console = power->priv->console;
+
+    g_object_get (G_OBJECT (console),
+		  "can-suspend", &can_suspend,
+		  NULL);
+    g_object_get (G_OBJECT (console),
+		  "can-hibernate", &can_hibernate,
+		  NULL);
+
+    /* ConsoleKit2 supports suspend and hibernate */
+    if (can_suspend || can_hibernate)
+    {
+	return TRUE;
+    }
+
+    return FALSE;
+}
+
 #ifdef ENABLE_POLKIT
 static void
 xfpm_power_check_polkit_auth (XfpmPower *power)
@@ -176,17 +210,33 @@ xfpm_power_check_polkit_auth (XfpmPower *power)
     const char *suspend, *hibernate;
     if (LOGIND_RUNNING())
     {
+	XFPM_DEBUG ("using logind suspend backend");
 	suspend   = POLKIT_AUTH_SUSPEND_LOGIND;
 	hibernate = POLKIT_AUTH_HIBERNATE_LOGIND;
     }
     else
     {
 #if !UP_CHECK_VERSION(0, 99, 0)
+	XFPM_DEBUG ("using upower suspend backend");
 	suspend   = POLKIT_AUTH_SUSPEND_UPOWER;
 	hibernate = POLKIT_AUTH_HIBERNATE_UPOWER;
 #else
-	suspend   = POLKIT_AUTH_SUSPEND_XFPM;
-	hibernate = POLKIT_AUTH_HIBERNATE_XFPM;
+	if (power->priv->console != NULL)
+	{
+	    /* ConsoleKit2 supports suspend and hibernate */
+	    if (check_for_consolekit2 (power))
+	    {
+		XFPM_DEBUG ("using consolekit2 suspend backend");
+		suspend   = POLKIT_AUTH_SUSPEND_CONSOLEKIT2;
+		hibernate = POLKIT_AUTH_HIBERNATE_CONSOLEKIT2;
+	    }
+	    else
+	    {
+		XFPM_DEBUG ("using xfpm internal suspend backend");
+		suspend   = POLKIT_AUTH_SUSPEND_XFPM;
+		hibernate = POLKIT_AUTH_HIBERNATE_XFPM;
+	    }
+	}
 #endif
     }
     power->priv->auth_suspend = xfpm_polkit_check_auth (power->priv->polkit,
@@ -269,8 +319,20 @@ xfpm_power_get_properties (XfpmPower *power)
     }
     else
     {
-	power->priv->can_suspend   = xfpm_suspend_can_suspend ();
-        power->priv->can_hibernate = xfpm_suspend_can_hibernate ();
+	if (check_for_consolekit2 (power))
+	{
+	    g_object_get (G_OBJECT (power->priv->console),
+			  "can-suspend", &power->priv->can_suspend,
+			  NULL);
+	    g_object_get (G_OBJECT (power->priv->console),
+			  "can-hibernate", &power->priv->can_hibernate,
+			  NULL);
+	}
+	else
+	{
+	    power->priv->can_suspend   = xfpm_suspend_can_suspend ();
+	    power->priv->can_hibernate = xfpm_suspend_can_hibernate ();
+	}
     }
 #endif
     g_object_get (power->priv->upower,
@@ -385,6 +447,12 @@ xfpm_power_sleep (XfpmPower *power, const gchar *sleep_time, gboolean force)
         }
     }
 
+    /* This is fun, here's the order of operations:
+     * - if the Logind is running then use it
+     * - if UPower < 0.99.0 then use it (don't make changes on the user unless forced)
+     * - if ConsoleKit2 is running then use it
+     * - if everything else fails use our built-in fallback
+     */
     if ( LOGIND_RUNNING () )
     {
 	xfpm_systemd_sleep (power->priv->systemd, sleep_time, &error);
@@ -403,11 +471,25 @@ xfpm_power_sleep (XfpmPower *power, const gchar *sleep_time, gboolean force)
 #else
 	if (!g_strcmp0 (sleep_time, "Hibernate"))
         {
+	    if (check_for_consolekit2 (power))
+	    {
+		xfpm_console_kit_hibernate (power->priv->console, &error);
+	    }
+	    else
+	    {
                 xfpm_suspend_try_action (XFPM_HIBERNATE);
+	    }
         }
         else
         {
+	    if (check_for_consolekit2 (power))
+	    {
+		xfpm_console_kit_suspend (power->priv->console, &error);
+	    }
+	    else
+	    {
                 xfpm_suspend_try_action (XFPM_SUSPEND);
+	    }
         }
 #endif
     }

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.


More information about the Xfce4-commits mailing list