]> rtime.felk.cvut.cz Git - sojka/lightdm.git/blob - liblightdm-gobject/power.c
Remove ConsoleKit support since upstream is long dead. Please migrate to logind or...
[sojka/lightdm.git] / liblightdm-gobject / power.c
1 /*
2  * Copyright (C) 2010-2011 Robert Ancell.
3  * Author: Robert Ancell <robert.ancell@canonical.com>
4  *
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.
9  */
10
11 #include <config.h>
12
13 #include <string.h>
14 #include <gio/gio.h>
15
16 #include "lightdm/power.h"
17
18 static GDBusProxy *upower_proxy = NULL;
19 static GDBusProxy *login1_proxy = NULL;
20
21 static GVariant *
22 upower_call_function (const gchar *function, GError **error)
23 {
24     if (!upower_proxy)
25     {
26         upower_proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SYSTEM,
27                                                       G_DBUS_PROXY_FLAGS_NONE,
28                                                       NULL,
29                                                       "org.freedesktop.UPower",
30                                                       "/org/freedesktop/UPower",
31                                                       "org.freedesktop.UPower",
32                                                       NULL,
33                                                       error);
34         if (!upower_proxy)
35             return NULL;
36     }
37
38     return g_dbus_proxy_call_sync (upower_proxy,
39                                    function,
40                                    NULL,
41                                    G_DBUS_CALL_FLAGS_NONE,
42                                    -1,
43                                    NULL,
44                                    error);
45 }
46
47 static GVariant *
48 login1_call_function (const gchar *function, GVariant *parameters, GError **error)
49 {
50     GVariant *r;
51
52     if (!login1_proxy)
53     {
54         login1_proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SYSTEM,
55                                                       G_DBUS_PROXY_FLAGS_NONE,
56                                                       NULL,
57                                                       "org.freedesktop.login1",
58                                                       "/org/freedesktop/login1",
59                                                       "org.freedesktop.login1.Manager",
60                                                       NULL,
61                                                       error);
62         if (!login1_proxy)
63             return NULL;
64     }
65
66     r = g_dbus_proxy_call_sync (login1_proxy,
67                                 function,
68                                 parameters,
69                                 G_DBUS_CALL_FLAGS_NONE,
70                                 -1,
71                                 NULL,
72                                 error);
73
74     return r;
75 }
76
77 /**
78  * lightdm_get_can_suspend:
79  *
80  * Checks if authorized to do a system suspend.
81  *
82  * Return value: #TRUE if can suspend the system
83  **/
84 gboolean
85 lightdm_get_can_suspend (void)
86 {
87     gboolean can_suspend = FALSE;
88     GVariant *r;
89
90     r = login1_call_function ("CanSuspend", NULL, NULL);
91     if (r)
92     {
93         gchar *result;
94         if (g_variant_is_of_type (r, G_VARIANT_TYPE ("(s)")))
95         {
96             g_variant_get (r, "(&s)", &result);
97             can_suspend = g_strcmp0 (result, "yes") == 0;
98         }
99     }
100     else
101     {
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);
105     }
106     if (r)
107         g_variant_unref (r);
108
109     return can_suspend;
110 }
111
112 /**
113  * lightdm_suspend:
114  * @error: return location for a #GError, or %NULL
115  *
116  * Triggers a system suspend.
117  *
118  * Return value: #TRUE if suspend initiated.
119  **/
120 gboolean
121 lightdm_suspend (GError **error)
122 {
123     GVariant *result;
124     gboolean suspended;
125
126     result = login1_call_function ("Suspend", g_variant_new("(b)", FALSE), error);
127     if (!result)
128     {
129         if (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);
133     }
134
135     suspended = result != NULL;
136     if (result)
137         g_variant_unref (result);
138
139     return suspended;
140 }
141
142 /**
143  * lightdm_get_can_hibernate:
144  *
145  * Checks if is authorized to do a system hibernate.
146  *
147  * Return value: #TRUE if can hibernate the system
148  **/
149 gboolean
150 lightdm_get_can_hibernate (void)
151 {
152     gboolean can_hibernate = FALSE;
153     GVariant *r;
154
155     r = login1_call_function ("CanHibernate", NULL, NULL);
156     if (r)
157     {
158         gchar *result;
159         if (g_variant_is_of_type (r, G_VARIANT_TYPE ("(s)")))
160         {
161             g_variant_get (r, "(&s)", &result);
162             can_hibernate = g_strcmp0 (result, "yes") == 0;
163         }
164     }
165     else
166     {
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);
170     }
171     if (r)
172         g_variant_unref (r);
173
174     return can_hibernate;
175 }
176
177 /**
178  * lightdm_hibernate:
179  * @error: return location for a #GError, or %NULL
180  *
181  * Triggers a system hibernate.
182  *
183  * Return value: #TRUE if hibernate initiated.
184  **/
185 gboolean
186 lightdm_hibernate (GError **error)
187 {
188     GVariant *result;
189     gboolean hibernated;
190
191     result = login1_call_function ("Hibernate", g_variant_new("(b)", FALSE), error);
192     if (!result)
193     {
194         if (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);
198     }
199
200     hibernated = result != NULL;
201     if (result)
202         g_variant_unref (result);
203
204     return hibernated;
205 }
206
207 /**
208  * lightdm_get_can_restart:
209  *
210  * Checks if is authorized to do a system restart.
211  *
212  * Return value: #TRUE if can restart the system
213  **/
214 gboolean
215 lightdm_get_can_restart (void)
216 {
217     gboolean can_restart = FALSE;
218     GVariant *r;
219
220     r = login1_call_function ("CanReboot", NULL, NULL);
221     if (r)
222     {
223         gchar *result;
224         if (g_variant_is_of_type (r, G_VARIANT_TYPE ("(s)")))
225         {
226             g_variant_get (r, "(&s)", &result);
227             can_restart = g_strcmp0 (result, "yes") == 0;
228         }
229         g_variant_unref (r);
230     }  
231
232     return can_restart;
233 }
234
235 /**
236  * lightdm_restart:
237  * @error: return location for a #GError, or %NULL
238  *
239  * Triggers a system restart.
240  *
241  * Return value: #TRUE if restart initiated.
242  **/
243 gboolean
244 lightdm_restart (GError **error)
245 {
246     GVariant *r;
247
248     r = login1_call_function ("Reboot", g_variant_new("(b)", FALSE), error);
249     if (r)
250     {
251         g_variant_unref (r);
252         return TRUE;
253     }
254
255     return FALSE;
256 }
257
258 /**
259  * lightdm_get_can_shutdown:
260  *
261  * Checks if is authorized to do a system shutdown.
262  *
263  * Return value: #TRUE if can shutdown the system
264  **/
265 gboolean
266 lightdm_get_can_shutdown (void)
267 {
268     gboolean can_shutdown = FALSE;
269     GVariant *r;
270
271     r = login1_call_function ("CanPowerOff", NULL, NULL);
272     if (r)
273     {
274         gchar *result;
275         if (g_variant_is_of_type (r, G_VARIANT_TYPE ("(s)")))
276         {
277             g_variant_get (r, "(&s)", &result);
278             can_shutdown = g_strcmp0 (result, "yes") == 0;
279         }
280         g_variant_unref (r);
281     }
282
283     return can_shutdown;
284 }
285
286 /**
287  * lightdm_shutdown:
288  * @error: return location for a #GError, or %NULL
289  *
290  * Triggers a system shutdown.
291  *
292  * Return value: #TRUE if shutdown initiated.
293  **/
294 gboolean
295 lightdm_shutdown (GError **error)
296 {
297     GVariant *r;
298
299     r = login1_call_function ("PowerOff", g_variant_new("(b)", FALSE), error);
300     if (r)
301     {
302         g_variant_unref (r);
303         return TRUE;
304     }
305
306     return FALSE;
307 }