Submitted By:            Pierre Labastie <pierre dot labastie at neuf dot fr>
Date:                    2016-04-19
Initial Package Version: 0.5.2
Upstream Status:         Pushed to git
Origin:                  Self
Description:             Adds Hibernate and Suspend through ConsoleKit2 API

diff -Naur lxsession-0.5.2-old/lxlock/lxlock lxsession-0.5.2-new/lxlock/lxlock
--- lxsession-0.5.2-old/lxlock/lxlock	2014-11-29 15:43:14.000000000 +0100
+++ lxsession-0.5.2-new/lxlock/lxlock	2016-04-09 19:11:24.317005824 +0200
@@ -29,11 +29,11 @@
     xscreensaver-command -lock >/dev/null 2>&1
 elif pidof gnome-screensaver >/dev/null; then
     gnome-screensaver-command --lock
-elif which slock >/dev/null; then
+elif which slock >/dev/null 2>&1; then
     slock &
-elif which xlock 2>/dev/null; then
+elif which xlock >/dev/null 2>&1; then
     xlock $*
-elif which i3lock >/dev/null; then
+elif which i3lock >/dev/null 2>&1; then
     i3lock -d
 else
     # In the end, try to fallback to xscreensaver
diff -Naur lxsession-0.5.2-old/lxsession-logout/lxsession-logout.c lxsession-0.5.2-new/lxsession-logout/lxsession-logout.c
--- lxsession-0.5.2-old/lxsession-logout/lxsession-logout.c	2014-11-29 15:43:14.000000000 +0100
+++ lxsession-0.5.2-new/lxsession-logout/lxsession-logout.c	2016-04-09 21:02:25.035056912 +0200
@@ -69,6 +69,8 @@
     int hibernate_systemd : 1;		/* Hibernate is available via systemd */
     int shutdown_ConsoleKit : 1;	/* Shutdown is available via ConsoleKit */
     int reboot_ConsoleKit : 1;		/* Reboot is available via ConsoleKit */
+    int suspend_ConsoleKit : 1;		/* Suspend is available via ConsoleKit */
+    int hibernate_ConsoleKit : 1;	/* Hibernate is available via ConsoleKit */
     int suspend_UPower : 1;		/* Suspend is available via UPower */
     int hibernate_UPower : 1;		/* Hibernate is available via UPower */
     int switch_user_GDM : 1;		/* Switch User is available via GDM */
@@ -222,7 +224,7 @@
         }
     }
     else if (handler_context->shutdown_ConsoleKit)
-        dbus_ConsoleKit_Stop(&err);
+        dbus_ConsoleKit_PowerOff(&err);
     else if (handler_context->shutdown_systemd)
         dbus_systemd_PowerOff(&err);
 
@@ -252,7 +254,7 @@
         }
     }
     else if (handler_context->reboot_ConsoleKit)
-        dbus_ConsoleKit_Restart(&err);
+        dbus_ConsoleKit_Reboot(&err);
     else if (handler_context->reboot_systemd)
         dbus_systemd_Reboot(&err);
 
@@ -276,6 +278,8 @@
     lock_screen();
     if (handler_context->suspend_UPower)
         dbus_UPower_Suspend(&err);
+    else if (handler_context->suspend_ConsoleKit)
+        dbus_ConsoleKit_Suspend(&err);
     else if (handler_context->suspend_systemd)
         dbus_systemd_Suspend(&err);
 
@@ -299,6 +303,8 @@
     lock_screen();
     if (handler_context->hibernate_UPower)
         dbus_UPower_Hibernate(&err);
+    else if (handler_context->hibernate_ConsoleKit)
+        dbus_ConsoleKit_Hibernate(&err);
     else if (handler_context->hibernate_systemd)
         dbus_systemd_Hibernate(&err);
 
@@ -515,16 +521,26 @@
     }
 
     /* Initialize capabilities of the ConsoleKit mechanism. */
-    if (!handler_context.shutdown_available && dbus_ConsoleKit_CanStop())
+    if (!handler_context.shutdown_available && dbus_ConsoleKit_CanPowerOff())
     {
         handler_context.shutdown_available = TRUE;
         handler_context.shutdown_ConsoleKit = TRUE;
     }
-    if (!handler_context.reboot_available && dbus_ConsoleKit_CanRestart())
+    if (!handler_context.reboot_available && dbus_ConsoleKit_CanReboot())
     {
         handler_context.reboot_available = TRUE;
         handler_context.reboot_ConsoleKit = TRUE;
     }
+    if (!handler_context.suspend_available && dbus_ConsoleKit_CanSuspend())
+    {
+        handler_context.suspend_available = TRUE;
+        handler_context.suspend_ConsoleKit = TRUE;
+    }
+    if (!handler_context.hibernate_available && dbus_ConsoleKit_CanHibernate())
+    {
+        handler_context.hibernate_available = TRUE;
+        handler_context.hibernate_ConsoleKit = TRUE;
+    }
 
     /* Initialize capabilities of the UPower mechanism. */
     if (!handler_context.suspend_available && dbus_UPower_CanSuspend())
diff -Naur lxsession-0.5.2-old/lxsession-logout/lxsession-logout-dbus-interface.c lxsession-0.5.2-new/lxsession-logout/lxsession-logout-dbus-interface.c
--- lxsession-0.5.2-old/lxsession-logout/lxsession-logout-dbus-interface.c	2014-11-29 15:43:14.000000000 +0100
+++ lxsession-0.5.2-new/lxsession-logout/lxsession-logout-dbus-interface.c	2016-04-09 19:25:46.704012439 +0200
@@ -93,10 +93,11 @@
 /*** ConsoleKit mechanism ***/
 
 static gboolean
-ck_call_function (const gchar *function, gboolean default_result, GError **error)
+ck_query (const gchar *function, gboolean default_result, GError **error)
 {
     GVariant *result;
     gboolean function_result = FALSE;
+    const gchar *str;
 
     if (!ck_proxy)
     {
@@ -122,35 +123,95 @@
     if (!result)
         return default_result;
 
-    if (g_variant_is_of_type (result, G_VARIANT_TYPE ("(b)")))
-        g_variant_get (result, "(b)", &function_result);
+    if (g_variant_is_of_type (result, G_VARIANT_TYPE ("(s)")))
+    {
+			g_variant_get (result, "(s)", &str);
+			if ( g_strcmp0 (str, "yes") == 0 || g_strcmp0 (str, "challenge") == 0 )
+				function_result = TRUE;
+			else
+				function_result = default_result;
+		}
 
     g_variant_unref (result);
     return function_result;
 }
 
+static void
+ck_call_function (const gchar *function, gboolean value, GError **error)
+{
+    GVariant *result;
+
+    if (!ck_proxy)
+    {
+        ck_proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SYSTEM,
+                                                      G_DBUS_PROXY_FLAGS_NONE,
+                                                      NULL,
+                                                      "org.freedesktop.ConsoleKit",
+                                                      "/org/freedesktop/ConsoleKit",
+                                                      "org.freedesktop.ConsoleKit.Manager",
+                                                      NULL,
+                                                      error);
+        if (!ck_proxy)
+            return;
+    }
+
+    result = g_dbus_proxy_call_sync (ck_proxy,
+                                     function,
+                                     g_variant_new ("(b)", value),
+                                     G_DBUS_CALL_FLAGS_NONE,
+                                     -1,
+                                     NULL,
+                                     error);
+    g_variant_unref (result);
+    return;
+}
+
 gboolean
-dbus_ConsoleKit_CanRestart (void)
+dbus_ConsoleKit_CanPowerOff (void)
+{
+    return ck_query ("CanPowerOff", FALSE, NULL);
+}
+
+void
+dbus_ConsoleKit_PowerOff (GError **error)
 {
-    return ck_call_function ("CanRestart", FALSE, NULL);
+    ck_call_function ("PowerOff", TRUE, error);
 }
 
 gboolean
-dbus_ConsoleKit_Restart (GError **error)
+dbus_ConsoleKit_CanReboot (void)
 {
-    return ck_call_function ("Restart", TRUE, error);
+    return ck_query ("CanReboot", FALSE, NULL);
+}
+
+void
+dbus_ConsoleKit_Reboot (GError **error)
+{
+    ck_call_function ("Reboot", TRUE, error);
 }
 
 gboolean
-dbus_ConsoleKit_CanStop (void)
+dbus_ConsoleKit_CanSuspend (void)
 {
-    return ck_call_function ("CanStop", FALSE, NULL);
+    return ck_query ("CanSuspend", FALSE, NULL);
+}
+
+void
+dbus_ConsoleKit_Suspend (GError **error)
+{
+    ck_call_function ("Suspend", TRUE, error);
 }
 
 gboolean
-dbus_ConsoleKit_Stop (GError **error)
+dbus_ConsoleKit_CanHibernate (void)
+{
+    return ck_query ("CanHibernate", FALSE, NULL);
+}
+
+void
+dbus_ConsoleKit_Hibernate (GError **error)
 {
-    return ck_call_function ("Stop", TRUE, error);
+    ck_call_function ("Hibernate", TRUE, error);
 }
 
 /*** Systemd mechanism ***/
diff -Naur lxsession-0.5.2-old/lxsession-logout/lxsession-logout-dbus-interface.h lxsession-0.5.2-new/lxsession-logout/lxsession-logout-dbus-interface.h
--- lxsession-0.5.2-old/lxsession-logout/lxsession-logout-dbus-interface.h	2014-10-05 01:49:49.000000000 +0200
+++ lxsession-0.5.2-new/lxsession-logout/lxsession-logout-dbus-interface.h	2016-04-09 21:05:04.524058135 +0200
@@ -21,11 +21,15 @@
 
 #include <glib.h>
 
-/* Interface to ConsoleKit for shutdown and reboot. */
-extern gboolean dbus_ConsoleKit_CanStop(void);
-extern gboolean dbus_ConsoleKit_CanRestart(void);
-extern gboolean dbus_ConsoleKit_Stop(GError **);
-extern gboolean dbus_ConsoleKit_Restart(GError **);
+/* Interface to ConsoleKit for suspend, hibernate, shutdown and reboot. */
+extern gboolean dbus_ConsoleKit_CanPowerOff(void);
+extern gboolean dbus_ConsoleKit_CanReboot(void);
+extern gboolean dbus_ConsoleKit_CanSuspend(void);
+extern gboolean dbus_ConsoleKit_CanHibernate(void);
+extern void dbus_ConsoleKit_PowerOff(GError **);
+extern void dbus_ConsoleKit_Reboot(GError **);
+extern void dbus_ConsoleKit_Suspend(GError **);
+extern void dbus_ConsoleKit_Hibernate(GError **);
 
 /* Interface to UPower for suspend and hibernate. */
 extern gboolean dbus_UPower_CanSuspend(void);
