summaryrefslogtreecommitdiff
path: root/src/gpm-control.c
blob: 5f9d528f3d79ed4e447ec51bf0cdb91fca2bcd85 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
 *
 * Copyright (C) 2006-2007 Richard Hughes <richard@hughsie.com>
 *
 * Licensed under the GNU General Public License Version 2
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <errno.h>

#include <string.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/wait.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif /* HAVE_UNISTD_H */

#include <gio/gio.h>
#include <glib/gi18n.h>

#ifdef WITH_KEYRING
#include <gnome-keyring.h>
#endif /* WITH_KEYRING */

#include "egg-console-kit.h"

#include "gpm-screensaver.h"
#include "gpm-common.h"
#include "gpm-control.h"
#include "gpm-networkmanager.h"

struct GpmControlPrivate
{
	GSettings		*settings;
};

enum {
	RESUME,
	SLEEP,
	LAST_SIGNAL
};

static guint signals [LAST_SIGNAL] = { 0 };
static gpointer gpm_control_object = NULL;

G_DEFINE_TYPE_WITH_PRIVATE (GpmControl, gpm_control, G_TYPE_OBJECT)

/**
 * gpm_control_error_quark:
 * Return value: Our personal error quark.
 **/
GQuark
gpm_control_error_quark (void)
{
	static GQuark quark = 0;
	if (!quark)
		quark = g_quark_from_static_string ("gpm_control_error");
	return quark;
}

/**
 * gpm_manager_systemd_shutdown:
 *
 * Shutdown the system using systemd-logind.
 *
 * Return value: fd, -1 on error
 **/
static gboolean
gpm_control_systemd_shutdown (void) {
	GError *error = NULL;
	GDBusProxy *proxy;
	GVariant *res = NULL;

	g_debug ("Requesting systemd to shutdown");
	proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SYSTEM,
					       G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES,
					       NULL,
					       "org.freedesktop.login1",
					       "/org/freedesktop/login1",
					       "org.freedesktop.login1.Manager",
					       NULL,
					       &error );
	//append all our arguments
	if (proxy == NULL) {
		g_error("Error connecting to dbus - %s", error->message);
		g_error_free (error);
		return FALSE;
	}

	res = g_dbus_proxy_call_sync (proxy, "PowerOff",
				      g_variant_new( "(b)", FALSE),
				      G_DBUS_CALL_FLAGS_NONE,
				      -1,
				      NULL,
				      &error
				      );
	if (error != NULL) {
		g_error ("Error in dbus - %s", error->message);
		g_error_free (error);
		return FALSE;
	}

	g_variant_unref(res);
	return TRUE;
}

/**
 * gpm_control_shutdown:
 * @control: This class instance
 *
 * Shuts down the computer
 **/
gboolean
gpm_control_shutdown (GpmControl *control, GError **error)
{
	gboolean ret;
	EggConsoleKit *console;

	if (LOGIND_RUNNING()) {
		ret = gpm_control_systemd_shutdown ();
	} else {
		console = egg_console_kit_new ();
		ret = egg_console_kit_stop (console, error);
		g_object_unref (console);
	}
	return ret;
}

/**
 * gpm_control_get_lock_policy:
 * @control: This class instance
 * @policy: The policy string.
 *
 * This function finds out if we should lock the screen when we do an
 * action. It is required as we can either use the mate-screensaver policy
 * or the custom policy. See the yelp file for more information.
 *
 * Return value: TRUE if we should lock.
 **/
gboolean
gpm_control_get_lock_policy (GpmControl *control, const gchar *policy)
{
	gboolean do_lock;
	gboolean use_ss_setting;
	gchar **schemas = NULL;
	gboolean schema_exists;
	gint i;

	/* Check if the mate-screensaver schema exists before trying to read
	   the lock setting to prevent crashing. See GNOME bug #651225. */
	g_settings_schema_source_list_schemas (g_settings_schema_source_get_default (), TRUE, &schemas, NULL);
	schema_exists = FALSE;
	for (i = 0; schemas[i] != NULL; i++) {
		if (g_strcmp0 (schemas[i], GS_SETTINGS_SCHEMA) == 0) {
			schema_exists = TRUE;
			break;
		}
	}

	g_strfreev (schemas);

	/* This allows us to over-ride the custom lock settings set
	   with a system default set in mate-screensaver.
	   See bug #331164 for all the juicy details. :-) */
	use_ss_setting = g_settings_get_boolean (control->priv->settings, GPM_SETTINGS_LOCK_USE_SCREENSAVER);
	if (use_ss_setting && schema_exists) {
		GSettings *settings_ss;
		settings_ss = g_settings_new (GS_SETTINGS_SCHEMA);
		do_lock = g_settings_get_boolean (settings_ss, GS_SETTINGS_PREF_LOCK_ENABLED);
		g_debug ("Using ScreenSaver settings (%i)", do_lock);
		g_object_unref (settings_ss);
	} else {
		do_lock = g_settings_get_boolean (control->priv->settings, policy);
		g_debug ("Using custom locking settings (%i)", do_lock);
	}
	return do_lock;
}

/**
 * gpm_control_suspend:
 **/
gboolean
gpm_control_suspend (GpmControl *control, GError **error)
{
	gboolean allowed = FALSE;
	gboolean ret = FALSE;
	gboolean do_lock;
	gboolean nm_sleep;
	EggConsoleKit *console;
	GpmScreensaver *screensaver;
	guint32 throttle_cookie = 0;
#ifdef WITH_KEYRING
	gboolean lock_gnome_keyring;
	GnomeKeyringResult keyres;
#endif /* WITH_KEYRING */

	GError *dbus_error = NULL;
	GDBusProxy *proxy;
	GVariant *res = NULL;

	screensaver = gpm_screensaver_new ();

	if (!LOGIND_RUNNING()) {
		console = egg_console_kit_new ();
		egg_console_kit_can_suspend (console, &allowed, NULL);
		g_object_unref (console);

		if (!allowed) {
			g_debug ("cannot suspend as not allowed from policy");
			g_set_error_literal (error, GPM_CONTROL_ERROR, GPM_CONTROL_ERROR_GENERAL, "Cannot suspend");
			goto out;
		}
	}

#ifdef WITH_KEYRING
	/* we should perhaps lock keyrings when sleeping #375681 */
	lock_gnome_keyring = g_settings_get_boolean (control->priv->settings, GPM_SETTINGS_LOCK_KEYRING_SUSPEND);
	if (lock_gnome_keyring) {
		keyres = gnome_keyring_lock_all_sync ();
		if (keyres != GNOME_KEYRING_RESULT_OK)
			g_warning ("could not lock keyring");
	}
#endif /* WITH_KEYRING */

	do_lock = gpm_control_get_lock_policy (control, GPM_SETTINGS_LOCK_ON_SUSPEND);
	if (do_lock) {
		throttle_cookie = gpm_screensaver_add_throttle (screensaver, "suspend");
		gpm_screensaver_lock (screensaver);
	}

	nm_sleep = g_settings_get_boolean (control->priv->settings, GPM_SETTINGS_NETWORKMANAGER_SLEEP);
	if (nm_sleep)
		gpm_networkmanager_sleep ();

	/* Do the suspend */
	g_debug ("emitting sleep");
	g_signal_emit (control, signals [SLEEP], 0, GPM_CONTROL_ACTION_SUSPEND);

	if (LOGIND_RUNNING()) {
		/* sleep via logind */
		proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SYSTEM,
						       G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES,
						       NULL,
						       "org.freedesktop.login1",
						       "/org/freedesktop/login1",
						       "org.freedesktop.login1.Manager",
						       NULL,
						       &dbus_error );
		if (proxy == NULL) {
			g_error ("Error connecting to dbus - %s", dbus_error->message);
			g_error_free (dbus_error);
			ret = FALSE;
			goto out;
		}
		res = g_dbus_proxy_call_sync (proxy, "Suspend",
					      g_variant_new( "(b)",FALSE),
					      G_DBUS_CALL_FLAGS_NONE,
					      -1,
					      NULL,
					      &dbus_error
					      );
		if (dbus_error != NULL ) {
			g_debug ("Error in dbus - %s", dbus_error->message);
			g_error_free (dbus_error);
			ret = TRUE;
		}
		else {
			g_variant_unref(res);
			ret = TRUE;
		}
		g_object_unref(proxy);
	}
	else {
		console = egg_console_kit_new ();
		ret = egg_console_kit_suspend (console, error);
		g_object_unref (console);
	}

	g_debug ("emitting resume");
	g_signal_emit (control, signals [RESUME], 0, GPM_CONTROL_ACTION_SUSPEND);

	if (do_lock) {
		gpm_screensaver_poke (screensaver);
		if (throttle_cookie)
			gpm_screensaver_remove_throttle (screensaver, throttle_cookie);
	}

	nm_sleep = g_settings_get_boolean (control->priv->settings, GPM_SETTINGS_NETWORKMANAGER_SLEEP);
	if (nm_sleep)
		gpm_networkmanager_wake ();

out:
	g_object_unref (screensaver);
	return ret;
}

/**
 * gpm_control_hibernate:
 **/
gboolean
gpm_control_hibernate (GpmControl *control, GError **error)
{
	gboolean allowed = FALSE;
	gboolean ret = FALSE;
	gboolean do_lock;
	gboolean nm_sleep;
	EggConsoleKit *console;
	GpmScreensaver *screensaver;
	guint32 throttle_cookie = 0;
#ifdef WITH_KEYRING
	gboolean lock_gnome_keyring;
	GnomeKeyringResult keyres;
#endif /* WITH_KEYRING */

	GError *dbus_error = NULL;
	GDBusProxy *proxy;
	GVariant *res = NULL;

	screensaver = gpm_screensaver_new ();

	if (!LOGIND_RUNNING()) {
		console = egg_console_kit_new ();
		egg_console_kit_can_hibernate (console, &allowed, NULL);
		g_object_unref (console);

		if (!allowed) {
			g_debug ("cannot hibernate as not allowed from policy");
			g_set_error_literal (error, GPM_CONTROL_ERROR, GPM_CONTROL_ERROR_GENERAL, "Cannot hibernate");
			goto out;
		}
	}

#ifdef WITH_KEYRING
	/* we should perhaps lock keyrings when sleeping #375681 */
	lock_gnome_keyring = g_settings_get_boolean (control->priv->settings, GPM_SETTINGS_LOCK_KEYRING_HIBERNATE);
	if (lock_gnome_keyring) {
		keyres = gnome_keyring_lock_all_sync ();
		if (keyres != GNOME_KEYRING_RESULT_OK) {
			g_warning ("could not lock keyring");
		}
	}
#endif /* WITH_KEYRING */

	do_lock = gpm_control_get_lock_policy (control, GPM_SETTINGS_LOCK_ON_HIBERNATE);
	if (do_lock) {
		throttle_cookie = gpm_screensaver_add_throttle (screensaver, "hibernate");
		gpm_screensaver_lock (screensaver);
	}

	nm_sleep = g_settings_get_boolean (control->priv->settings, GPM_SETTINGS_NETWORKMANAGER_SLEEP);
	if (nm_sleep)
		gpm_networkmanager_sleep ();

	g_debug ("emitting sleep");
	g_signal_emit (control, signals [SLEEP], 0, GPM_CONTROL_ACTION_HIBERNATE);

	if (LOGIND_RUNNING()) {
		/* sleep via logind */
		proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SYSTEM,
						       G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES,
						       NULL,
						       "org.freedesktop.login1",
						       "/org/freedesktop/login1",
						       "org.freedesktop.login1.Manager",
						       NULL,
						       &dbus_error );
		if (proxy == NULL) {
			g_error ("Error connecting to dbus - %s", dbus_error->message);
			g_error_free (dbus_error);
			ret = FALSE;
			goto out;
		}
		res = g_dbus_proxy_call_sync (proxy, "Hibernate",
					      g_variant_new( "(b)",FALSE),
					      G_DBUS_CALL_FLAGS_NONE,
					      -1,
					      NULL,
					      &dbus_error
					      );
		if (dbus_error != NULL ) {
			g_debug ("Error in dbus - %s", dbus_error->message);
			g_error_free (dbus_error);
			ret = TRUE;
		}
		else {
			g_variant_unref(res);
			ret = TRUE;
		}
	}
	else {
		console = egg_console_kit_new ();
		ret = egg_console_kit_hibernate (console, error);
		g_object_unref (console);
	}

	g_debug ("emitting resume");
	g_signal_emit (control, signals [RESUME], 0, GPM_CONTROL_ACTION_HIBERNATE);

	if (do_lock) {
		gpm_screensaver_poke (screensaver);
		if (throttle_cookie)
			gpm_screensaver_remove_throttle (screensaver, throttle_cookie);
	}

	nm_sleep = g_settings_get_boolean (control->priv->settings, GPM_SETTINGS_NETWORKMANAGER_SLEEP);
	if (nm_sleep)
		gpm_networkmanager_wake ();

out:
	g_object_unref (screensaver);
	return ret;
}

/**
 * gpm_control_finalize:
 **/
static void
gpm_control_finalize (GObject *object)
{
	GpmControl *control;

	g_return_if_fail (object != NULL);
	g_return_if_fail (GPM_IS_CONTROL (object));
	control = GPM_CONTROL (object);

	g_object_unref (control->priv->settings);

	g_return_if_fail (control->priv != NULL);
	G_OBJECT_CLASS (gpm_control_parent_class)->finalize (object);
}

/**
 * gpm_control_class_init:
 **/
static void
gpm_control_class_init (GpmControlClass *klass)
{
	GObjectClass *object_class = G_OBJECT_CLASS (klass);
	object_class->finalize = gpm_control_finalize;

	signals [RESUME] =
		g_signal_new ("resume",
			      G_TYPE_FROM_CLASS (object_class),
			      G_SIGNAL_RUN_LAST,
			      G_STRUCT_OFFSET (GpmControlClass, resume),
			      NULL,
			      NULL,
			      g_cclosure_marshal_VOID__INT,
			      G_TYPE_NONE, 1, G_TYPE_INT);
	signals [SLEEP] =
		g_signal_new ("sleep",
			      G_TYPE_FROM_CLASS (object_class),
			      G_SIGNAL_RUN_LAST,
			      G_STRUCT_OFFSET (GpmControlClass, sleep),
			      NULL,
			      NULL,
			      g_cclosure_marshal_VOID__INT,
			      G_TYPE_NONE, 1, G_TYPE_INT);
}

/**
 * gpm_control_init:
 * @control: This control class instance
 **/
static void
gpm_control_init (GpmControl *control)
{
	control->priv = gpm_control_get_instance_private (control);

	control->priv->settings = g_settings_new (GPM_SETTINGS_SCHEMA);
}

/**
 * gpm_control_new:
 * Return value: A new control class instance.
 **/
GpmControl *
gpm_control_new (void)
{
	if (gpm_control_object != NULL) {
		g_object_ref (gpm_control_object);
	} else {
		gpm_control_object = g_object_new (GPM_TYPE_CONTROL, NULL);
		g_object_add_weak_pointer (gpm_control_object, &gpm_control_object);
	}
	return GPM_CONTROL (gpm_control_object);
}