summaryrefslogtreecommitdiff
path: root/plugins/media-keys/libhal-glib/hal-device-power.c
blob: 69c8061297cc6314d25b35dcd2cfddcec93b5421 (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
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
 *
 * Copyright (C) 2005-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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

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

#include <string.h>
#include <glib.h>
#include <glib/gi18n.h>
#include <dbus/dbus-glib.h>
#include <time.h>

#include "egg-debug.h"
#include "egg-dbus-proxy.h"

#include "hal-marshal.h"
#include "hal-device-power.h"
#include "hal-device.h"
#include "hal-manager.h"

static void     hal_device_power_class_init (HalDevicePowerClass *klass);
static void     hal_device_power_init       (HalDevicePower      *power);
static void     hal_device_power_finalize   (GObject	      *object);

#define HAL_DEVICE_POWER_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), HAL_TYPE_DEVICE_POWER, HalDevicePowerPrivate))

struct HalDevicePowerPrivate
{
	HalDevice		*computer;
	EggDbusProxy		*gproxy;
};

static gpointer hal_device_power_object = NULL;
G_DEFINE_TYPE (HalDevicePower, hal_device_power, G_TYPE_OBJECT)

/**
 * hal_device_power_class_init:
 * @klass: This class instance
 **/
static void
hal_device_power_class_init (HalDevicePowerClass *klass)
{
	GObjectClass *object_class = G_OBJECT_CLASS (klass);
	object_class->finalize = hal_device_power_finalize;
	g_type_class_add_private (klass, sizeof (HalDevicePowerPrivate));
}

/**
 * hal_device_power_init:
 *
 * @power: This class instance
 **/
static void
hal_device_power_init (HalDevicePower *power)
{
	DBusGConnection *connection;
	power->priv = HAL_DEVICE_POWER_GET_PRIVATE (power);

	/* get the power connection */
	power->priv->gproxy = egg_dbus_proxy_new ();
	connection = dbus_g_bus_get (DBUS_BUS_SYSTEM, NULL);
	egg_dbus_proxy_assign (power->priv->gproxy, connection, HAL_DBUS_SERVICE,
			       HAL_ROOT_COMPUTER, HAL_DBUS_INTERFACE_POWER);
	if (power->priv->gproxy == NULL)
		egg_warning ("HAL does not support power management!");

	power->priv->computer = hal_device_new ();
	hal_device_set_udi (power->priv->computer, HAL_ROOT_COMPUTER);
}

/**
 * hal_device_power_is_laptop:
 *
 * @power: This class instance
 * Return value: TRUE is computer is identified as a laptop
 *
 * Returns true if system.formfactor is "laptop"
 **/
gboolean
hal_device_power_is_laptop (HalDevicePower *power)
{
	gboolean ret = TRUE;
	gchar *formfactor = NULL;

	g_return_val_if_fail (HAL_IS_DEVICE_POWER (power), FALSE);

	/* always present */
	hal_device_get_string (power->priv->computer, "system.formfactor", &formfactor, NULL);
	if (formfactor == NULL) {
		/* no need to free */
		return FALSE;
	}
	if (strcmp (formfactor, "laptop") != 0) {
		egg_debug ("This machine is not identified as a laptop."
			   "system.formfactor is %s.", formfactor);
		ret = FALSE;
	}
	g_free (formfactor);
	return ret;
}

/**
 * hal_device_power_has_support:
 *
 * @power: This class instance
 * Return value: TRUE if haldaemon has power management capability
 *
 * Finds out if power management functions are running (only ACPI, PMU, APM)
 **/
gboolean
hal_device_power_has_support (HalDevicePower *power)
{
	gchar *type = NULL;

	g_return_val_if_fail (HAL_IS_DEVICE_POWER (power), FALSE);

	hal_device_get_string (power->priv->computer, "power_management.type", &type, NULL);
	/* this key only has to exist to be pm okay */
	if (type != NULL) {
		g_free (type);
		return TRUE;
	}
	return FALSE;
}

/**
 * hal_device_power_can_suspend:
 *
 * @power: This class instance
 * Return value: TRUE if kernel suspend support is compiled in
 *
 * Finds out if HAL indicates that we can suspend
 **/
gboolean
hal_device_power_can_suspend (HalDevicePower *power)
{
	gboolean exists;
	gboolean can_suspend;

	g_return_val_if_fail (HAL_IS_DEVICE_POWER (power), FALSE);

	/* TODO: Change to can_suspend when rely on newer HAL */
	exists = hal_device_get_bool (power->priv->computer,
					  "power_management.can_suspend",
					  &can_suspend, NULL);
	if (exists == FALSE) {
		egg_warning ("Key can_suspend missing");
		return FALSE;
	}
	return can_suspend;
}

/**
 * hal_device_power_can_hibernate:
 *
 * @power: This class instance
 * Return value: TRUE if kernel hibernation support is compiled in
 *
 * Finds out if HAL indicates that we can hibernate
 **/
gboolean
hal_device_power_can_hibernate (HalDevicePower *power)
{
	gboolean exists;
	gboolean can_hibernate;

	g_return_val_if_fail (HAL_IS_DEVICE_POWER (power), FALSE);

	/* TODO: Change to can_hibernate when rely on newer HAL */
	exists = hal_device_get_bool (power->priv->computer,
					  "power_management.can_hibernate",
					  &can_hibernate, NULL);
	if (exists == FALSE) {
		egg_warning ("Key can_hibernate missing");
		return FALSE;
	}
	return can_hibernate;
}

/**
 * hal_device_power_filter_error:
 *
 * We have to ignore dbus timeouts sometimes
 **/
static gboolean
hal_device_power_filter_error (GError **error)
{
	/* short cut for speed, no error */
	if (error == NULL || *error == NULL)
		return FALSE;

	/* DBUS might time out, which is okay. We can remove this code
	   when the dbus glib bindings are fixed. See #332888 */
	if (g_error_matches (*error, DBUS_GERROR, DBUS_GERROR_NO_REPLY)) {
		egg_warning ("DBUS timed out, but recovering");
		g_error_free (*error);
		*error = NULL;
		return TRUE;
	}
	egg_warning ("Method failed\n(%s)",  (*error)->message);
	return FALSE;
}

/**
 * hal_device_power_suspend:
 *
 * @power: This class instance
 * @wakeup: Seconds to wakeup, currently unsupported
 * Return value: Success, true if we suspended OK
 *
 * Uses org.freedesktop.Hal.Device.SystemPowerManagement.Suspend ()
 **/
gboolean
hal_device_power_suspend (HalDevicePower *power, guint wakeup, GError **error)
{
	time_t start;
	time_t end;
	gint retval = 0;
	gboolean ret;
	DBusGProxy *proxy;

	g_return_val_if_fail (HAL_IS_DEVICE_POWER (power), FALSE);

	proxy = egg_dbus_proxy_get_proxy (power->priv->gproxy);
	if (DBUS_IS_G_PROXY (proxy) == FALSE) {
		egg_warning ("proxy NULL!!");
		return FALSE;
	}

	time (&start);
	ret = dbus_g_proxy_call (proxy, "Suspend", error,
				 G_TYPE_INT, wakeup,
				 G_TYPE_INVALID,
				 G_TYPE_INT, &retval,
				 G_TYPE_INVALID);
	/* we might have to ignore the error */
	if (error != NULL && hal_device_power_filter_error (error))
		return TRUE;
	if (retval != 0)
		egg_warning ("Suspend failed without error message");

	/* compare the amount of time that has passed - if it's more than 6 hours
	 * then the dbus call timed out (dbus-pending-call.c) */
	if (ret != 0) {
		time (&end);
		if (difftime (start, end) >= 6*60*60*1000)
			return TRUE;
	}

	return ret;
}

/**
 * hal_device_power_pm_method_void:
 *
 * @power: This class instance
 * @method: The method name, e.g. "Hibernate"
 * Return value: Success, true if we did OK
 *
 * Do a method on org.freedesktop.Hal.Device.SystemPowerManagement.*
 * with no arguments.
 **/
static gboolean
hal_device_power_pm_method_void (HalDevicePower *power, const gchar *method, GError **error)
{
	time_t start;
	time_t end;
	guint retval = 0;
	gboolean ret;
	DBusGProxy *proxy;

	g_return_val_if_fail (HAL_IS_DEVICE_POWER (power), FALSE);
	g_return_val_if_fail (method != NULL, FALSE);

	proxy = egg_dbus_proxy_get_proxy (power->priv->gproxy);
	if (DBUS_IS_G_PROXY (proxy) == FALSE) {
		egg_warning ("proxy NULL!!");
		return FALSE;
	}
	if (DBUS_IS_G_PROXY (proxy) == FALSE) {
		egg_warning ("not connected");
		return FALSE;
	}

	time (&start);
	ret = dbus_g_proxy_call (proxy, method, error,
				 G_TYPE_INVALID,
				 G_TYPE_INT, &retval,
				 G_TYPE_INVALID);
	/* we might have to ignore the error */
	if (error != NULL && hal_device_power_filter_error (error))
		return TRUE;
	if (retval != 0)
		egg_warning ("%s failed in a horrible way!", method);

	/* compare the amount of time that has passed - if it's more than 6 hours
	 * then the dbus call timed out (dbus-pending-call.c) */
	if (ret != 0) {
		time (&end);
		if (difftime (start,end) >= 6*60*60*1000)
			return TRUE;
	}

	return ret;
}

/**
 * hal_device_power_hibernate:
 *
 * @power: This class instance
 * Return value: Success, true if we hibernated OK
 *
 * Uses org.freedesktop.Hal.Device.SystemPowerManagement.Hibernate ()
 **/
gboolean
hal_device_power_hibernate (HalDevicePower *power, GError **error)
{
	g_return_val_if_fail (HAL_IS_DEVICE_POWER (power), FALSE);
	return hal_device_power_pm_method_void (power, "Hibernate", error);
}

/**
 * hal_device_power_shutdown:
 *
 * Return value: Success, true if we shutdown OK
 *
 * Uses org.freedesktop.Hal.Device.SystemPowerManagement.Shutdown ()
 **/
gboolean
hal_device_power_shutdown (HalDevicePower *power, GError **error)
{
	g_return_val_if_fail (HAL_IS_DEVICE_POWER (power), FALSE);
	return hal_device_power_pm_method_void (power, "Shutdown", error);
}

/**
 * hal_device_power_reboot:
 *
 * @power: This class instance
 * Return value: Success, true if we shutdown OK
 *
 * Uses org.freedesktop.Hal.Device.SystemPowerManagement.Reboot ()
 **/
gboolean
hal_device_power_reboot (HalDevicePower *power, GError **error)
{
	g_return_val_if_fail (HAL_IS_DEVICE_POWER (power), FALSE);
	return hal_device_power_pm_method_void (power, "Reboot", error);
}

/**
 * hal_device_power_enable_power_save:
 *
 * @power: This class instance
 * @enable: True to enable low power mode
 * Return value: Success, true if we set the mode
 *
 * Uses org.freedesktop.Hal.Device.SystemPowerManagement.SetPowerSave ()
 **/
gboolean
hal_device_power_enable_power_save (HalDevicePower *power, gboolean enable)
{
	gint retval = 0;
	GError *error = NULL;
	gboolean ret;
	DBusGProxy *proxy;

	g_return_val_if_fail (power != NULL, FALSE);
	g_return_val_if_fail (HAL_IS_DEVICE_POWER (power), FALSE);

	proxy = egg_dbus_proxy_get_proxy (power->priv->gproxy);
	if (DBUS_IS_G_PROXY (proxy) == FALSE) {
		egg_warning ("proxy NULL!!");
		return FALSE;
	}

	/* abort if we are not a "qualified" laptop */
	if (hal_device_power_is_laptop (power) == FALSE) {
		egg_debug ("We are not a laptop, so not even trying");
		return FALSE;
	}

	ret = dbus_g_proxy_call (proxy, "SetPowerSave", &error,
				 G_TYPE_BOOLEAN, enable,
				 G_TYPE_INVALID,
				 G_TYPE_INT, &retval,
				 G_TYPE_INVALID);
	if (retval != 0)
		egg_warning ("SetPowerSave failed in a horrible way!");
	return ret;
}

/**
 * hal_device_power_has_suspend_error:
 *
 * @power: This class instance
 * @enable: Return true if there was a suspend error
 * Return value: Success
 *
 * TODO: should call a method on HAL and also return the ouput of the file
 **/
gboolean
hal_device_power_has_suspend_error (HalDevicePower *power, gboolean *state)
{
	g_return_val_if_fail (power != NULL, FALSE);
	g_return_val_if_fail (state != NULL, FALSE);
	g_return_val_if_fail (HAL_IS_DEVICE_POWER (power), FALSE);
	*state = g_file_test ("/var/lib/hal/system-power-suspend-output", G_FILE_TEST_EXISTS);
	return TRUE;
}

/**
 * hal_device_power_has_hibernate_error:
 *
 * @power: This class instance
 * @enable: Return true if there was a hibernate error
 * Return value: Success
 *
 * TODO: should call a method on HAL and also return the ouput of the file
 **/
gboolean
hal_device_power_has_hibernate_error (HalDevicePower *power, gboolean *state)
{
	g_return_val_if_fail (power != NULL, FALSE);
	g_return_val_if_fail (state != NULL, FALSE);
	g_return_val_if_fail (HAL_IS_DEVICE_POWER (power), FALSE);
	*state = g_file_test ("/var/lib/hal/system-power-hibernate-output", G_FILE_TEST_EXISTS);
	return TRUE;
}

/**
 * hal_device_power_clear_suspend_error:
 *
 * @power: This class instance
 * Return value: Success
 *
 * Tells HAL to try and clear the suspend error as we appear to be okay
 **/
gboolean
hal_device_power_clear_suspend_error (HalDevicePower *power, GError **error)
{
	gboolean ret;
	DBusGProxy *proxy;

	g_return_val_if_fail (power != NULL, FALSE);
	g_return_val_if_fail (HAL_IS_DEVICE_POWER (power), FALSE);

	proxy = egg_dbus_proxy_get_proxy (power->priv->gproxy);
	if (DBUS_IS_G_PROXY (proxy) == FALSE) {
		egg_warning ("proxy NULL!!");
		return FALSE;
	}

	ret = dbus_g_proxy_call (proxy, "SuspendClearError", error,
				 G_TYPE_INVALID, G_TYPE_INVALID);
	return ret;
}

/**
 * hal_device_power_clear_hibernate_error:
 *
 * @power: This class instance
 * Return value: Success
 *
 * Tells HAL to try and clear the hibernate error as we appear to be okay
 **/
gboolean
hal_device_power_clear_hibernate_error (HalDevicePower *power, GError **error)
{
	gboolean ret;
	DBusGProxy *proxy;

	g_return_val_if_fail (power != NULL, FALSE);
	g_return_val_if_fail (HAL_IS_DEVICE_POWER (power), FALSE);

	proxy = egg_dbus_proxy_get_proxy (power->priv->gproxy);
	if (DBUS_IS_G_PROXY (proxy) == FALSE) {
		egg_warning ("proxy NULL!!");
		return FALSE;
	}

	ret = dbus_g_proxy_call (proxy, "HibernateClearError", error,
				 G_TYPE_INVALID, G_TYPE_INVALID);
	return ret;
}

/**
 * hal_device_power_finalize:
 * @object: This class instance
 **/
static void
hal_device_power_finalize (GObject *object)
{
	HalDevicePower *power;
	g_return_if_fail (object != NULL);
	g_return_if_fail (HAL_IS_DEVICE_POWER (object));

	power = HAL_DEVICE_POWER (object);
	power->priv = HAL_DEVICE_POWER_GET_PRIVATE (power);

	g_object_unref (power->priv->gproxy);
	g_object_unref (power->priv->computer);

	G_OBJECT_CLASS (hal_device_power_parent_class)->finalize (object);
}

/**
 * hal_device_power_new:
 * Return value: new HalDevicePower instance.
 **/
HalDevicePower *
hal_device_power_new (void)
{
	if (hal_device_power_object != NULL) {
		g_object_ref (hal_device_power_object);
	} else {
		hal_device_power_object = g_object_new (HAL_TYPE_DEVICE_POWER, NULL);
		g_object_add_weak_pointer (hal_device_power_object, &hal_device_power_object);
	}
	return HAL_DEVICE_POWER (hal_device_power_object);
}