2 * Copyright (C) 2010-2011 Robert Ancell.
3 * Author: Robert Ancell <robert.ancell@canonical.com>
5 * This library is free software; you can redistribute it and/or modify it under
6 * the terms of the GNU Lesser General Public License as published by the Free
7 * Software Foundation; either version 2 or version 3 of the License.
8 * See http://www.gnu.org/copyleft/lgpl.html the full text of the license.
16 #include "lightdm/power.h"
18 static GDBusProxy *upower_proxy = NULL;
19 static GDBusProxy *ck_proxy = NULL;
20 static GDBusProxy *login1_proxy = NULL;
23 upower_call_function (const gchar *function, GError **error)
27 upower_proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SYSTEM,
28 G_DBUS_PROXY_FLAGS_NONE,
30 "org.freedesktop.UPower",
31 "/org/freedesktop/UPower",
32 "org.freedesktop.UPower",
39 return g_dbus_proxy_call_sync (upower_proxy,
42 G_DBUS_CALL_FLAGS_NONE,
49 login1_call_function (const gchar *function, GVariant *parameters, GError **error)
55 login1_proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SYSTEM,
56 G_DBUS_PROXY_FLAGS_NONE,
58 "org.freedesktop.login1",
59 "/org/freedesktop/login1",
60 "org.freedesktop.login1.Manager",
67 r = g_dbus_proxy_call_sync (login1_proxy,
70 G_DBUS_CALL_FLAGS_NONE,
79 * lightdm_get_can_suspend:
81 * Checks if authorized to do a system suspend.
83 * Return value: #TRUE if can suspend the system
86 lightdm_get_can_suspend (void)
88 gboolean can_suspend = FALSE;
91 r = login1_call_function ("CanSuspend", NULL, NULL);
95 if (g_variant_is_of_type (r, G_VARIANT_TYPE ("(s)")))
97 g_variant_get (r, "(&s)", &result);
98 can_suspend = g_strcmp0 (result, "yes") == 0;
103 r = upower_call_function ("SuspendAllowed", NULL);
104 if (r && g_variant_is_of_type (r, G_VARIANT_TYPE ("(b)")))
105 g_variant_get (r, "(b)", &can_suspend);
115 * @error: return location for a #GError, or %NULL
117 * Triggers a system suspend.
119 * Return value: #TRUE if suspend initiated.
122 lightdm_suspend (GError **error)
127 result = login1_call_function ("Suspend", g_variant_new("(b)", FALSE), error);
131 g_debug ("Can't suspend using logind; falling back to UPower: %s", (*error)->message);
132 g_clear_error (error);
133 result = upower_call_function ("Suspend", error);
136 suspended = result != NULL;
138 g_variant_unref (result);
144 * lightdm_get_can_hibernate:
146 * Checks if is authorized to do a system hibernate.
148 * Return value: #TRUE if can hibernate the system
151 lightdm_get_can_hibernate (void)
153 gboolean can_hibernate = FALSE;
156 r = login1_call_function ("CanHibernate", NULL, NULL);
160 if (g_variant_is_of_type (r, G_VARIANT_TYPE ("(s)")))
162 g_variant_get (r, "(&s)", &result);
163 can_hibernate = g_strcmp0 (result, "yes") == 0;
168 r = upower_call_function ("HibernateAllowed", NULL);
169 if (r && g_variant_is_of_type (r, G_VARIANT_TYPE ("(b)")))
170 g_variant_get (r, "(b)", &can_hibernate);
175 return can_hibernate;
180 * @error: return location for a #GError, or %NULL
182 * Triggers a system hibernate.
184 * Return value: #TRUE if hibernate initiated.
187 lightdm_hibernate (GError **error)
192 result = login1_call_function ("Hibernate", g_variant_new("(b)", FALSE), error);
196 g_debug ("Can't hibernate using logind; falling back to UPower: %s", (*error)->message);
197 g_clear_error (error);
198 result = upower_call_function ("Hibernate", error);
201 hibernated = result != NULL;
203 g_variant_unref (result);
209 ck_call_function (const gchar *function, GError **error)
215 ck_proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SYSTEM,
216 G_DBUS_PROXY_FLAGS_NONE,
218 "org.freedesktop.ConsoleKit",
219 "/org/freedesktop/ConsoleKit/Manager",
220 "org.freedesktop.ConsoleKit.Manager",
227 r = g_dbus_proxy_call_sync (ck_proxy,
230 G_DBUS_CALL_FLAGS_NONE,
239 * lightdm_get_can_restart:
241 * Checks if is authorized to do a system restart.
243 * Return value: #TRUE if can restart the system
246 lightdm_get_can_restart (void)
248 gboolean can_restart = FALSE;
251 r = login1_call_function ("CanReboot", NULL, NULL);
255 if (g_variant_is_of_type (r, G_VARIANT_TYPE ("(s)")))
257 g_variant_get (r, "(&s)", &result);
258 can_restart = g_strcmp0 (result, "yes") == 0;
263 r = ck_call_function ("CanRestart", NULL);
264 if (r && g_variant_is_of_type (r, G_VARIANT_TYPE ("(b)")))
265 g_variant_get (r, "(b)", &can_restart);
275 * @error: return location for a #GError, or %NULL
277 * Triggers a system restart.
279 * Return value: #TRUE if restart initiated.
282 lightdm_restart (GError **error)
287 r = login1_call_function ("Reboot", g_variant_new("(b)", FALSE), error);
290 g_clear_error (error);
291 r = ck_call_function ("Restart", error);
293 restarted = r != NULL;
301 * lightdm_get_can_shutdown:
303 * Checks if is authorized to do a system shutdown.
305 * Return value: #TRUE if can shutdown the system
308 lightdm_get_can_shutdown (void)
310 gboolean can_shutdown = FALSE;
313 r = login1_call_function ("CanPowerOff", NULL, NULL);
317 if (g_variant_is_of_type (r, G_VARIANT_TYPE ("(s)")))
319 g_variant_get (r, "(&s)", &result);
320 can_shutdown = g_strcmp0 (result, "yes") == 0;
325 r = ck_call_function ("CanStop", NULL);
326 if (r && g_variant_is_of_type (r, G_VARIANT_TYPE ("(b)")))
327 g_variant_get (r, "(b)", &can_shutdown);
337 * @error: return location for a #GError, or %NULL
339 * Triggers a system shutdown.
341 * Return value: #TRUE if shutdown initiated.
344 lightdm_shutdown (GError **error)
349 r = login1_call_function ("PowerOff", g_variant_new("(b)", FALSE), error);
352 g_clear_error (error);
353 r = ck_call_function ("Stop", error);
355 shutdown = r != NULL;