From: Iain Lane Date: Thu, 18 Apr 2013 10:54:55 +0000 (+0100) Subject: Use logind for suspend/hibernate too, if available. X-Git-Url: https://rtime.felk.cvut.cz/gitweb/sojka/lightdm.git/commitdiff_plain/d09c680103bd19ee03dfe807fe7ef9674f90369c Use logind for suspend/hibernate too, if available. --- diff --git a/liblightdm-gobject/power.c b/liblightdm-gobject/power.c index 628dc2ad..1871ccca 100644 --- a/liblightdm-gobject/power.c +++ b/liblightdm-gobject/power.c @@ -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); diff --git a/liblightdm-qt/power.cpp b/liblightdm-qt/power.cpp index 47bd5a05..245e15c7 100644 --- a/liblightdm-qt/power.cpp +++ b/liblightdm-qt/power.cpp @@ -51,6 +51,17 @@ PowerInterface::~PowerInterface() bool PowerInterface::canSuspend() { + if (d->login1Interface->isValid()) + { + QDBusReply reply = d->login1Interface->call("CanSuspend"); + if (reply.isValid()) + { + return reply.value() == "yes"; + } + } + + qWarning() << d->login1Interface->lastError(); + QDBusReply reply = d->powerManagementInterface->call("SuspendAllowed"); if (reply.isValid()) { return reply.value(); @@ -62,12 +73,28 @@ bool PowerInterface::canSuspend() bool PowerInterface::suspend() { - QDBusReply reply = d->powerManagementInterface->call("Suspend"); + QDBusReply 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 reply = d->login1Interface->call("CanHibernate"); + if (reply.isValid()) + { + return reply.value() == "yes"; + } + } + + qWarning() << d->login1Interface->lastError(); + QDBusReply reply = d->powerManagementInterface->call("HibernateAllowed"); if (reply.isValid()) { return reply.value(); @@ -79,7 +106,12 @@ bool PowerInterface::canHibernate() bool PowerInterface::hibernate() { - QDBusReply reply = d->powerManagementInterface->call("Hibernate"); + QDBusReply reply; + if (d->login1Interface->isValid()) + reply = d->login1Interface->call("Hibernate", false); + else + reply = d->powerManagementInterface->call("Hibernate"); + return reply.isValid (); }