~lightdm-team/lightdm/1.10

« back to all changes in this revision

Viewing changes to liblightdm-gobject/power.c

  • Committer: Robert Ancell
  • Date: 2017-01-12 02:34:24 UTC
  • Revision ID: robert.ancell@canonical.com-20170112023424-sa0gtyqw7qganpk7
Use power management functions from ConsoleKit2 if available.

Suspend and hibernate functionality was removed from upower 0.99.0, so systems
not using systemd had now suspend/hibernate functionality. Support for this
was added into ConsoleKit2.

Most systems will either be systemd or ConsoleKit2 now, so we try the following:
1. Power management in logind
2. Power management in ConsoleKit
3. upower for suspend/resume (really only here to not break backwards
   compatibility)

Based on a patch for Gentoo by Fitzcarraldo.

Show diffs side-by-side

added added

removed removed

Lines of Context:
75
75
    return r;
76
76
}
77
77
 
 
78
static GVariant *
 
79
ck_call_function (const gchar *function, GVariant *parameters, GError **error)
 
80
{
 
81
    GVariant *r;
 
82
 
 
83
    if (!ck_proxy)
 
84
    {
 
85
        ck_proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SYSTEM,
 
86
                                                  G_DBUS_PROXY_FLAGS_NONE,
 
87
                                                  NULL,
 
88
                                                  "org.freedesktop.ConsoleKit",
 
89
                                                  "/org/freedesktop/ConsoleKit/Manager",
 
90
                                                  "org.freedesktop.ConsoleKit.Manager",
 
91
                                                  NULL,
 
92
                                                  error);
 
93
        if (!ck_proxy)
 
94
            return FALSE;
 
95
    }
 
96
 
 
97
    r = g_dbus_proxy_call_sync (ck_proxy,
 
98
                                function,
 
99
                                parameters,
 
100
                                G_DBUS_CALL_FLAGS_NONE,
 
101
                                -1,
 
102
                                NULL,
 
103
                                error);
 
104
 
 
105
    return r;
 
106
}
 
107
 
78
108
/**
79
109
 * lightdm_get_can_suspend:
80
110
 *
98
128
            can_suspend = g_strcmp0 (result, "yes") == 0;
99
129
        }
100
130
    }
101
 
    else
 
131
    if (!r)
 
132
    {
 
133
        r = ck_call_function ("CanSuspend", NULL, NULL);
 
134
        if (r && g_variant_is_of_type (r, G_VARIANT_TYPE ("(b)")))
 
135
            g_variant_get (r, "(b)", &can_suspend);
 
136
    }
 
137
    if (!r)
102
138
    {
103
139
        r = upower_call_function ("SuspendAllowed", NULL);
104
140
        if (r && g_variant_is_of_type (r, G_VARIANT_TYPE ("(b)")))
128
164
    if (!result)
129
165
    {
130
166
        if (error)
131
 
            g_debug ("Can't suspend using logind; falling back to UPower: %s", (*error)->message);
 
167
            g_debug ("Can't suspend using logind; falling back to ConsoleKit: %s", (*error)->message);
 
168
        g_clear_error (error);
 
169
        result = ck_call_function ("Suspend", g_variant_new ("(b)", FALSE), error);
 
170
    }
 
171
    if (!result)
 
172
    {
 
173
        if (error)
 
174
            g_debug ("Can't suspend using logind or ConsoleKit; falling back to UPower: %s", (*error)->message);
132
175
        g_clear_error (error);
133
176
        result = upower_call_function ("Suspend", error);
134
177
    }
163
206
            can_hibernate = g_strcmp0 (result, "yes") == 0;
164
207
        }
165
208
    }
166
 
    else
 
209
    if (!r)
 
210
    {
 
211
        r = ck_call_function ("CanHibernate", NULL, NULL);
 
212
        if (r && g_variant_is_of_type (r, G_VARIANT_TYPE ("(b)")))
 
213
            g_variant_get (r, "(b)", &can_hibernate);
 
214
    }
 
215
    if (!r)
167
216
    {
168
217
        r = upower_call_function ("HibernateAllowed", NULL);
169
218
        if (r && g_variant_is_of_type (r, G_VARIANT_TYPE ("(b)")))
193
242
    if (!result)
194
243
    {
195
244
        if (error)
196
 
            g_debug ("Can't hibernate using logind; falling back to UPower: %s", (*error)->message);
 
245
            g_debug ("Can't hibernate using logind; falling back to ConsoleKit: %s", (*error)->message);
 
246
        g_clear_error (error);
 
247
        result = ck_call_function ("Hibernate", g_variant_new ("(b)", FALSE), error);
 
248
    }
 
249
    if (!result)
 
250
    {
 
251
        if (error)
 
252
            g_debug ("Can't hibernate using logind or ConsoleKit; falling back to UPower: %s", (*error)->message);
197
253
        g_clear_error (error);
198
254
        result = upower_call_function ("Hibernate", error);
199
255
    }
205
261
    return hibernated;
206
262
}
207
263
 
208
 
static GVariant *
209
 
ck_call_function (const gchar *function, GError **error)
210
 
{
211
 
    GVariant *r;
212
 
 
213
 
    if (!ck_proxy)
214
 
    {
215
 
        ck_proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SYSTEM,
216
 
                                                  G_DBUS_PROXY_FLAGS_NONE,
217
 
                                                  NULL,
218
 
                                                  "org.freedesktop.ConsoleKit",
219
 
                                                  "/org/freedesktop/ConsoleKit/Manager",
220
 
                                                  "org.freedesktop.ConsoleKit.Manager",
221
 
                                                  NULL,
222
 
                                                  error);
223
 
        if (!ck_proxy)
224
 
            return FALSE;
225
 
    }
226
 
 
227
 
    r = g_dbus_proxy_call_sync (ck_proxy,
228
 
                                function,
229
 
                                NULL,
230
 
                                G_DBUS_CALL_FLAGS_NONE,
231
 
                                -1,
232
 
                                NULL,
233
 
                                error);
234
 
 
235
 
    return r;
236
 
}
237
 
 
238
264
/**
239
265
 * lightdm_get_can_restart:
240
266
 *
260
286
    }
261
287
    else
262
288
    {
263
 
        r = ck_call_function ("CanRestart", NULL);
 
289
        r = ck_call_function ("CanRestart", NULL, NULL);
264
290
        if (r && g_variant_is_of_type (r, G_VARIANT_TYPE ("(b)")))
265
291
            g_variant_get (r, "(b)", &can_restart);
266
292
    }
288
314
    if (!r)
289
315
    {
290
316
        g_clear_error (error);
291
 
        r = ck_call_function ("Restart", error);
 
317
        r = ck_call_function ("Restart", NULL, error);
292
318
    }
293
319
    restarted = r != NULL;
294
320
    if (r)
322
348
    }
323
349
    else
324
350
    {
325
 
        r = ck_call_function ("CanStop", NULL);
 
351
        r = ck_call_function ("CanStop", NULL, NULL);
326
352
        if (r && g_variant_is_of_type (r, G_VARIANT_TYPE ("(b)")))
327
353
            g_variant_get (r, "(b)", &can_shutdown);
328
354
    }
350
376
    if (!r)
351
377
    {
352
378
        g_clear_error (error);
353
 
        r = ck_call_function ("Stop", error);
 
379
        r = ck_call_function ("Stop", NULL, error);
354
380
    }
355
381
    shutdown = r != NULL;
356
382
    if (r)