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 *login1_proxy = NULL;
22 upower_call_function (const gchar *function, GError **error)
26 upower_proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SYSTEM,
27 G_DBUS_PROXY_FLAGS_NONE,
29 "org.freedesktop.UPower",
30 "/org/freedesktop/UPower",
31 "org.freedesktop.UPower",
38 return g_dbus_proxy_call_sync (upower_proxy,
41 G_DBUS_CALL_FLAGS_NONE,
48 login1_call_function (const gchar *function, GVariant *parameters, GError **error)
54 login1_proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SYSTEM,
55 G_DBUS_PROXY_FLAGS_NONE,
57 "org.freedesktop.login1",
58 "/org/freedesktop/login1",
59 "org.freedesktop.login1.Manager",
66 r = g_dbus_proxy_call_sync (login1_proxy,
69 G_DBUS_CALL_FLAGS_NONE,
78 * lightdm_get_can_suspend:
80 * Checks if authorized to do a system suspend.
82 * Return value: #TRUE if can suspend the system
85 lightdm_get_can_suspend (void)
87 gboolean can_suspend = FALSE;
90 r = login1_call_function ("CanSuspend", NULL, NULL);
94 if (g_variant_is_of_type (r, G_VARIANT_TYPE ("(s)")))
96 g_variant_get (r, "(&s)", &result);
97 can_suspend = g_strcmp0 (result, "yes") == 0;
102 r = upower_call_function ("SuspendAllowed", NULL);
103 if (r && g_variant_is_of_type (r, G_VARIANT_TYPE ("(b)")))
104 g_variant_get (r, "(b)", &can_suspend);
114 * @error: return location for a #GError, or %NULL
116 * Triggers a system suspend.
118 * Return value: #TRUE if suspend initiated.
121 lightdm_suspend (GError **error)
126 result = login1_call_function ("Suspend", g_variant_new("(b)", FALSE), error);
130 g_debug ("Can't suspend using logind; falling back to UPower: %s", (*error)->message);
131 g_clear_error (error);
132 result = upower_call_function ("Suspend", error);
135 suspended = result != NULL;
137 g_variant_unref (result);
143 * lightdm_get_can_hibernate:
145 * Checks if is authorized to do a system hibernate.
147 * Return value: #TRUE if can hibernate the system
150 lightdm_get_can_hibernate (void)
152 gboolean can_hibernate = FALSE;
155 r = login1_call_function ("CanHibernate", NULL, NULL);
159 if (g_variant_is_of_type (r, G_VARIANT_TYPE ("(s)")))
161 g_variant_get (r, "(&s)", &result);
162 can_hibernate = g_strcmp0 (result, "yes") == 0;
167 r = upower_call_function ("HibernateAllowed", NULL);
168 if (r && g_variant_is_of_type (r, G_VARIANT_TYPE ("(b)")))
169 g_variant_get (r, "(b)", &can_hibernate);
174 return can_hibernate;
179 * @error: return location for a #GError, or %NULL
181 * Triggers a system hibernate.
183 * Return value: #TRUE if hibernate initiated.
186 lightdm_hibernate (GError **error)
191 result = login1_call_function ("Hibernate", g_variant_new("(b)", FALSE), error);
195 g_debug ("Can't hibernate using logind; falling back to UPower: %s", (*error)->message);
196 g_clear_error (error);
197 result = upower_call_function ("Hibernate", error);
200 hibernated = result != NULL;
202 g_variant_unref (result);
208 * lightdm_get_can_restart:
210 * Checks if is authorized to do a system restart.
212 * Return value: #TRUE if can restart the system
215 lightdm_get_can_restart (void)
217 gboolean can_restart = FALSE;
220 r = login1_call_function ("CanReboot", NULL, NULL);
224 if (g_variant_is_of_type (r, G_VARIANT_TYPE ("(s)")))
226 g_variant_get (r, "(&s)", &result);
227 can_restart = g_strcmp0 (result, "yes") == 0;
237 * @error: return location for a #GError, or %NULL
239 * Triggers a system restart.
241 * Return value: #TRUE if restart initiated.
244 lightdm_restart (GError **error)
248 r = login1_call_function ("Reboot", g_variant_new("(b)", FALSE), error);
259 * lightdm_get_can_shutdown:
261 * Checks if is authorized to do a system shutdown.
263 * Return value: #TRUE if can shutdown the system
266 lightdm_get_can_shutdown (void)
268 gboolean can_shutdown = FALSE;
271 r = login1_call_function ("CanPowerOff", NULL, NULL);
275 if (g_variant_is_of_type (r, G_VARIANT_TYPE ("(s)")))
277 g_variant_get (r, "(&s)", &result);
278 can_shutdown = g_strcmp0 (result, "yes") == 0;
288 * @error: return location for a #GError, or %NULL
290 * Triggers a system shutdown.
292 * Return value: #TRUE if shutdown initiated.
295 lightdm_shutdown (GError **error)
299 r = login1_call_function ("PowerOff", g_variant_new("(b)", FALSE), error);