]> rtime.felk.cvut.cz Git - sojka/lightdm.git/commitdiff
Use logind for suspend/hibernate too, if available.
authorIain Lane <iain.lane@canonical.com>
Thu, 18 Apr 2013 10:54:55 +0000 (11:54 +0100)
committerIain Lane <iain.lane@canonical.com>
Thu, 18 Apr 2013 10:54:55 +0000 (11:54 +0100)
liblightdm-gobject/power.c
liblightdm-qt/power.cpp

index 628dc2ad93757232e6e22780024a4fac56ea660b..1871ccca8efb15695b1f735b5ff6ef4184bc93a0 100644 (file)
@@ -47,6 +47,37 @@ upower_call_function (const gchar *function, GError **error)
                                    error);
 }
 
+static GVariant *
+login1_call_function (const gchar *function, GVariant *parameters, GError **error)
+{
+    GVariant *r;
+    gchar *str_result;
+
+    if (!login1_proxy)
+    {
+        login1_proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SYSTEM,
+                                                      G_DBUS_PROXY_FLAGS_NONE,
+                                                      NULL,
+                                                      "org.freedesktop.login1",
+                                                      "/org/freedesktop/login1",
+                                                      "org.freedesktop.login1.Manager",
+                                                      NULL,
+                                                      error);
+        if (!login1_proxy)
+            return NULL;
+    }
+
+    r = g_dbus_proxy_call_sync (login1_proxy,
+                                function,
+                                parameters,
+                                G_DBUS_CALL_FLAGS_NONE,
+                                -1,
+                                NULL,
+                                error);
+
+    return r;
+}
+
 /**
  * lightdm_get_can_suspend:
  *
@@ -57,16 +88,27 @@ upower_call_function (const gchar *function, GError **error)
 gboolean
 lightdm_get_can_suspend (void)
 {
-    GVariant *result;
     gboolean can_suspend = FALSE;
+    GVariant *r;
 
-    result = upower_call_function ("SuspendAllowed", NULL);
-    if (!result)
-        return FALSE;
-
-    if (g_variant_is_of_type (result, G_VARIANT_TYPE ("(b)")))
-        g_variant_get (result, "(b)", &can_suspend);
-    g_variant_unref (result);
+    r = login1_call_function ("CanSuspend", NULL, NULL);
+    if (r)
+    {
+        gchar *result;
+        if (g_variant_is_of_type (r, G_VARIANT_TYPE ("(s)")))
+        {
+            g_variant_get (r, "(&s)", &result);
+            can_suspend = g_strcmp0 (result, "yes") == 0;
+        }
+    }
+    else
+    {
+        r = upower_call_function ("CanSuspend", NULL);
+        if (r && g_variant_is_of_type (r, G_VARIANT_TYPE ("(b)")))
+            g_variant_get (r, "(b)", &can_suspend);
+    }
+    if (r)
+        g_variant_unref (r);
 
     return can_suspend;
 }
@@ -83,14 +125,25 @@ gboolean
 lightdm_suspend (GError **error)
 {
     GVariant *result;
-  
-    result = upower_call_function ("Suspend", error);
+    gboolean suspended;
+
+    result = login1_call_function ("Suspend", g_variant_new("(b)", FALSE), error);
+
     if (!result)
-        return FALSE;
+    {
+      if (error)
+       g_printerr ("Can't suspend using lightdm; falling back to UPower: %s",
+                   (*error)->message);
+      g_clear_error (error);
+      result = upower_call_function ("Suspend", error);
+    }
+
+    suspended = result != NULL;
+    if (result)
+      g_variant_unref (result);
 
-    g_variant_unref (result);
+    return suspended;
 
-    return TRUE;
 }
 
 /**
@@ -103,16 +156,27 @@ lightdm_suspend (GError **error)
 gboolean
 lightdm_get_can_hibernate (void)
 {
-    GVariant *result;
     gboolean can_hibernate = FALSE;
-  
-    result = upower_call_function ("HibernateAllowed", NULL);
-    if (!result)
-        return FALSE;
+    GVariant *r;
 
-    if (g_variant_is_of_type (result, G_VARIANT_TYPE ("(b)")))
-        g_variant_get (result, "(b)", &can_hibernate);
-    g_variant_unref (result);
+    r = login1_call_function ("CanHibernate", NULL, NULL);
+    if (r)
+    {
+        gchar *result;
+        if (g_variant_is_of_type (r, G_VARIANT_TYPE ("(s)")))
+        {
+            g_variant_get (r, "(&s)", &result);
+            can_hibernate = g_strcmp0 (result, "yes") == 0;
+        }
+    }
+    else
+    {
+        r = upower_call_function ("CanHibernate", NULL);
+        if (r && g_variant_is_of_type (r, G_VARIANT_TYPE ("(b)")))
+            g_variant_get (r, "(b)", &can_hibernate);
+    }
+    if (r)
+        g_variant_unref (r);
 
     return can_hibernate;
 }
@@ -129,14 +193,24 @@ gboolean
 lightdm_hibernate (GError **error)
 {
     GVariant *result;
-  
-    result = upower_call_function ("Hibernate", error);
+    gboolean hibernated;
+
+    result = login1_call_function ("Hibernate", g_variant_new("(b)", FALSE), error);
+
     if (!result)
-        return FALSE;
+    {
+      if (error)
+       g_printerr ("Can't hibernate using lightdm; falling back to UPower: %s",
+                   (*error)->message);
+      g_clear_error (error);
+      result = upower_call_function ("Hibernate", error);
+    }
 
-    g_variant_unref (result);
+    hibernated = result != NULL;
+    if (result)
+      g_variant_unref (result);
 
-    return TRUE;
+    return hibernated;
 }
 
 static GVariant *
@@ -169,37 +243,6 @@ ck_call_function (const gchar *function, GError **error)
     return r;
 }
 
-static GVariant *
-login1_call_function (const gchar *function, GVariant *parameters, GError **error)
-{
-    GVariant *r;
-    gchar *str_result;
-
-    if (!login1_proxy)
-    {
-        login1_proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SYSTEM,
-                                                      G_DBUS_PROXY_FLAGS_NONE,
-                                                      NULL,
-                                                      "org.freedesktop.login1",
-                                                      "/org/freedesktop/login1",
-                                                      "org.freedesktop.login1.Manager",
-                                                      NULL,
-                                                      error);
-        if (!login1_proxy)
-            return NULL;
-    }
-
-    r = g_dbus_proxy_call_sync (login1_proxy,
-                                function,
-                                parameters,
-                                G_DBUS_CALL_FLAGS_NONE,
-                                -1,
-                                NULL,
-                                error);
-
-    return r;
-}
-
 /**
  * lightdm_get_can_restart:
  *
@@ -250,6 +293,7 @@ lightdm_restart (GError **error)
     gboolean restarted;
 
     r = login1_call_function ("Reboot", g_variant_new("(b)", FALSE), error);
+
     if (!r)
     {
         g_clear_error (error);
index 47bd5a05ec25c76b15de36f486cd395ef2e405aa..245e15c7bbdaf513d4dd2e0ce1eec9bc40d578c5 100644 (file)
@@ -51,6 +51,17 @@ PowerInterface::~PowerInterface()
 
 bool PowerInterface::canSuspend()
 {
+    if (d->login1Interface->isValid())
+    {
+       QDBusReply<QString> reply = d->login1Interface->call("CanSuspend");
+       if (reply.isValid())
+       {
+           return reply.value() == "yes";
+       }
+    }
+
+    qWarning() << d->login1Interface->lastError();
+
     QDBusReply<bool> reply = d->powerManagementInterface->call("SuspendAllowed");
     if (reply.isValid()) {
         return reply.value();
@@ -62,12 +73,28 @@ bool PowerInterface::canSuspend()
 
 bool PowerInterface::suspend()
 {
-    QDBusReply<void> reply = d->powerManagementInterface->call("Suspend");
+    QDBusReply<void> reply;
+    if (d->login1Interface->isValid())
+       reply = d->login1Interface->call("Hibernate", false);
+    else
+       reply = d->powerManagementInterface->call("Hibernate");
+
     return reply.isValid ();
 }
 
 bool PowerInterface::canHibernate()
 {
+    if (d->login1Interface->isValid())
+    {
+       QDBusReply<QString> reply = d->login1Interface->call("CanHibernate");
+       if (reply.isValid())
+       {
+           return reply.value() == "yes";
+       }
+    }
+
+    qWarning() << d->login1Interface->lastError();
+
     QDBusReply<bool> reply = d->powerManagementInterface->call("HibernateAllowed");
     if (reply.isValid()) {
         return reply.value();
@@ -79,7 +106,12 @@ bool PowerInterface::canHibernate()
 
 bool PowerInterface::hibernate()
 {
-    QDBusReply<void> reply = d->powerManagementInterface->call("Hibernate");
+    QDBusReply<void> reply;
+    if (d->login1Interface->isValid())
+       reply = d->login1Interface->call("Hibernate", false);
+    else
+       reply = d->powerManagementInterface->call("Hibernate");
+
     return reply.isValid ();
 }