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
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
|
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
*
* Copyright (C) 2008-2010 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.
*/
/**
* SECTION:up-client
* @short_description: Main client object for accessing UPower data
* @see_also: #UpDevice
*
* A helper GObject to use for accessing UPower information, whether on the
* system bus (from the UPower daemon) or from the session (from session services),
* and to be notified when it is changed.
*/
#include "config.h"
#include <stdlib.h>
#include <stdio.h>
#include <glib-object.h>
#include "up-client.h"
#include "up-daemon-generated.h"
#include "up-device.h"
static void up_client_class_init (UpClientClass *klass);
static void up_client_initable_iface_init (GInitableIface *iface);
static void up_client_init (UpClient *client);
static void up_client_finalize (GObject *object);
/**
* UpClientPrivate:
*
* Private #UpClient data
**/
struct _UpClientPrivate
{
UpExportedDaemon *proxy;
GBusType bus_type;
};
enum {
UP_CLIENT_DEVICE_ADDED,
UP_CLIENT_DEVICE_REMOVED,
UP_CLIENT_LAST_SIGNAL
};
enum {
PROP_0,
PROP_DAEMON_VERSION,
PROP_ON_BATTERY,
PROP_LID_IS_CLOSED,
PROP_LID_IS_PRESENT,
PROP_BUS_TYPE,
PROP_LAST
};
static guint signals [UP_CLIENT_LAST_SIGNAL] = { 0 };
G_DEFINE_TYPE_WITH_CODE (UpClient, up_client, G_TYPE_OBJECT,
G_ADD_PRIVATE(UpClient)
G_IMPLEMENT_INTERFACE(G_TYPE_INITABLE, up_client_initable_iface_init))
static inline gboolean
is_system (UpClient *client)
{
return client->priv->bus_type == G_BUS_TYPE_SYSTEM;
}
/**
* up_client_get_devices:
* @client: a #UpClient instance.
*
* Get a copy of the device objects. This function does not set the free
* function for the #GPtrArray so you need use g_object_unref() on all
* elements when you are finished with the array.
*
* Return value: (element-type UpDevice) (transfer full): an array of #UpDevice objects, free with g_ptr_array_unref()
*
* Since: 0.9.0
* Deprecated: 0.99.8
**/
GPtrArray *
up_client_get_devices (UpClient *client)
{
GPtrArray *array;
g_return_val_if_fail (UP_IS_CLIENT (client), NULL);
array = up_client_get_devices2 (client);
g_ptr_array_set_free_func (array, NULL);
return array;
}
/**
* up_client_get_devices2:
* @client: a #UpClient instance.
*
* Get a copy of the device objects.
*
* Return value: (element-type UpDevice) (transfer full): an array of #UpDevice objects, free with g_ptr_array_unref()
*
* Since: 0.99.8
**/
GPtrArray *
up_client_get_devices2 (UpClient *client)
{
GError *error = NULL;
char **devices;
GPtrArray *array;
guint i;
g_return_val_if_fail (UP_IS_CLIENT (client), NULL);
if (up_exported_daemon_call_enumerate_devices_sync (client->priv->proxy,
&devices,
NULL,
&error) == FALSE) {
g_warning ("up_client_get_devices failed: %s", error->message);
g_error_free (error);
return NULL;
}
array = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref);
for (i = 0; devices[i] != NULL; i++) {
UpDevice *device;
const char *object_path = devices[i];
gboolean ret;
device = up_device_new ();
ret = up_device_set_object_path_sync (device, object_path, NULL, NULL);
if (!ret) {
g_object_unref (device);
continue;
}
g_ptr_array_add (array, device);
}
g_strfreev (devices);
return array;
}
/**
* up_client_get_display_device:
* @client: a #UpClient instance.
*
* Get the composite display device. Not available
* for a session #UpClient.
*
* Return value: (transfer full): a #UpDevice object, or %NULL on error.
*
* Since: 1.0
**/
UpDevice *
up_client_get_display_device (UpClient *client)
{
gboolean ret;
UpDevice *device;
g_return_val_if_fail (UP_IS_CLIENT (client), NULL);
g_return_val_if_fail (is_system (client), NULL);
device = up_device_new ();
ret = up_device_set_object_path_sync (device, "/org/freedesktop/UPower/devices/DisplayDevice", NULL, NULL);
if (!ret) {
g_object_unref (G_OBJECT (device));
return NULL;
}
return device;
}
/**
* up_client_get_critical_action:
* @client: a #UpClient instance.
*
* Gets a string representing the configured critical action,
* depending on availability. Not available for a
* session #UpClient.
*
* Return value: the action name, or %NULL on error.
*
* Since: 1.0
**/
char *
up_client_get_critical_action (UpClient *client)
{
char *action;
g_return_val_if_fail (UP_IS_CLIENT (client), NULL);
g_return_val_if_fail (is_system (client), NULL);
if (!up_exported_daemon_call_get_critical_action_sync (client->priv->proxy,
&action,
NULL, NULL)) {
return NULL;
}
return action;
}
/**
* up_client_get_daemon_version:
* @client: a #UpClient instance.
*
* Get UPower daemon version, or the library version in the case
* of a session #UpClient.
*
* Return value: string containing the daemon version, e.g. 008
*
* Since: 0.9.0
**/
const gchar *
up_client_get_daemon_version (UpClient *client)
{
g_return_val_if_fail (UP_IS_CLIENT (client), NULL);
if (!is_system (client))
return VERSION;
return up_exported_daemon_get_daemon_version (client->priv->proxy);
}
/**
* up_client_get_lid_is_closed:
* @client: a #UpClient instance.
*
* Get whether the laptop lid is closed. Not available for a
* session #UpClient.
*
* Return value: %TRUE if lid is closed or %FALSE otherwise.
*
* Since: 0.9.0
*/
gboolean
up_client_get_lid_is_closed (UpClient *client)
{
g_return_val_if_fail (UP_IS_CLIENT (client), FALSE);
g_return_val_if_fail (is_system (client), FALSE);
return up_exported_daemon_get_lid_is_closed (client->priv->proxy);
}
/**
* up_client_get_lid_is_present:
* @client: a #UpClient instance.
*
* Get whether a laptop lid is present on this machine. Not available for
* a session #UpClient.
*
* Return value: %TRUE if the machine has a laptop lid
*
* Since: 0.9.2
*/
gboolean
up_client_get_lid_is_present (UpClient *client)
{
g_return_val_if_fail (UP_IS_CLIENT (client), FALSE);
g_return_val_if_fail (is_system (client), FALSE);
return up_exported_daemon_get_lid_is_present (client->priv->proxy);
}
/**
* up_client_get_on_battery:
* @client: a #UpClient instance.
*
* Get whether the system is running on battery power. Not available
* for a session #UpClient.
*
* Return value: %TRUE if the system is currently running on battery, %FALSE otherwise.
*
* Since: 0.9.0
**/
gboolean
up_client_get_on_battery (UpClient *client)
{
g_return_val_if_fail (UP_IS_CLIENT (client), FALSE);
g_return_val_if_fail (is_system (client), FALSE);
return up_exported_daemon_get_on_battery (client->priv->proxy);
}
static void
up_client_add (UpClient *client, const gchar *object_path)
{
UpDevice *device = NULL;
gboolean ret;
/* create new device */
device = up_device_new ();
ret = up_device_set_object_path_sync (device, object_path, NULL, NULL);
if (!ret)
goto out;
/* add to array */
g_signal_emit (client, signals [UP_CLIENT_DEVICE_ADDED], 0, device);
out:
g_clear_object (&device);
}
static void
up_client_notify_cb (GObject *gobject,
GParamSpec *pspec,
UpClient *client)
{
/* Proxy the notification from the D-Bus glue object
* to the real one, but only if the property exists
* for UpClient */
if (!g_object_class_find_property (G_OBJECT_GET_CLASS (client),
pspec->name))
return;
g_object_notify (G_OBJECT (client), pspec->name);
}
static void
up_device_added_cb (UpExportedDaemon *proxy, const gchar *object_path, UpClient *client)
{
up_client_add (client, object_path);
}
static void
up_device_removed_cb (UpExportedDaemon *proxy, const gchar *object_path, UpClient *client)
{
g_signal_emit (client, signals [UP_CLIENT_DEVICE_REMOVED], 0, object_path);
}
static void
up_client_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
UpClient *client;
GBusType bus_type;
client = UP_CLIENT (object);
switch (prop_id) {
case PROP_BUS_TYPE:
bus_type = g_value_get_enum (value);
if (bus_type == G_BUS_TYPE_SYSTEM ||
bus_type == G_BUS_TYPE_SESSION) {
client->priv->bus_type = bus_type;
} else {
g_warning ("Invalid value '%d' property for \'bus-type\'", bus_type);
}
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
up_client_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
UpClient *client;
client = UP_CLIENT (object);
if (is_system (client) && client->priv->proxy == NULL)
return;
switch (prop_id) {
case PROP_DAEMON_VERSION:
g_value_set_string (value, is_system (client) ?
up_exported_daemon_get_daemon_version (client->priv->proxy) :
VERSION);
break;
case PROP_ON_BATTERY:
if (is_system (client))
g_value_set_boolean (value, up_exported_daemon_get_on_battery (client->priv->proxy));
else
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
case PROP_LID_IS_CLOSED:
if (is_system (client))
g_value_set_boolean (value, up_exported_daemon_get_lid_is_closed (client->priv->proxy));
else
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
case PROP_LID_IS_PRESENT:
if (is_system (client))
g_value_set_boolean (value, up_exported_daemon_get_lid_is_present (client->priv->proxy));
else
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
case PROP_BUS_TYPE:
g_value_set_enum (value, client->priv->bus_type);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
up_client_class_init (UpClientClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
object_class->set_property = up_client_set_property;
object_class->get_property = up_client_get_property;
object_class->finalize = up_client_finalize;
/**
* UpClient:daemon-version:
*
* The daemon version.
*
* Since: 0.9.0
*/
g_object_class_install_property (object_class,
PROP_DAEMON_VERSION,
g_param_spec_string ("daemon-version",
"Daemon version",
NULL,
NULL,
G_PARAM_READABLE));
/**
* UpClient:on-battery:
*
* If the computer is on battery power.
*
* Since: 0.9.0
*/
g_object_class_install_property (object_class,
PROP_ON_BATTERY,
g_param_spec_boolean ("on-battery",
"If the computer is on battery power",
NULL,
FALSE,
G_PARAM_READABLE));
/**
* UpClient:lid-is-closed:
*
* If the laptop lid is closed.
*
* Since: 0.9.0
*/
g_object_class_install_property (object_class,
PROP_LID_IS_CLOSED,
g_param_spec_boolean ("lid-is-closed",
"If the laptop lid is closed",
NULL,
FALSE,
G_PARAM_READABLE));
/**
* UpClient:lid-is-present:
*
* If a laptop lid is present.
*
* Since: 0.9.0
*/
g_object_class_install_property (object_class,
PROP_LID_IS_PRESENT,
g_param_spec_boolean ("lid-is-present",
"If a laptop lid is present",
NULL,
FALSE,
G_PARAM_READABLE));
/**
* UpClient:bus-type:
*
* Whether the #UpClient will gather data from the session or the system bus.
* Note that only %G_BUS_TYPE_SYSTEM and %G_BUS_TYPE_SESSION are valid values.
*
* Since: 0.9.11
*/
g_object_class_install_property (object_class,
PROP_BUS_TYPE,
g_param_spec_enum ("bus-type",
"The D-Bus bus on which to gather data",
NULL,
G_TYPE_BUS_TYPE,
G_BUS_TYPE_SYSTEM,
G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
/**
* UpClient::device-added:
* @client: the #UpClient instance that emitted the signal
* @device: the #UpDevice that was added.
*
* The ::device-added signal is emitted when a power device is added.
*
* Since: 0.9.0
**/
signals [UP_CLIENT_DEVICE_ADDED] =
g_signal_new ("device-added",
G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET (UpClientClass, device_added),
NULL, NULL, g_cclosure_marshal_VOID__OBJECT,
G_TYPE_NONE, 1, UP_TYPE_DEVICE);
/**
* UpClient::device-removed:
* @client: the #UpClient instance that emitted the signal
* @object_path: the object path of the #UpDevice that was removed.
*
* The ::device-removed signal is emitted when a power device is removed.
*
* Since: 1.0
**/
signals [UP_CLIENT_DEVICE_REMOVED] =
g_signal_new ("device-removed",
G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET (UpClientClass, device_removed),
NULL, NULL, g_cclosure_marshal_VOID__STRING,
G_TYPE_NONE, 1, G_TYPE_STRING);
}
static gboolean
up_client_initable_init (GInitable *initable, GCancellable *cancellable, GError **error)
{
UpClient *client = UP_CLIENT (initable);
if (!is_system (client)) {
g_set_error_literal (error, G_FILE_ERROR, G_FILE_ERROR_NOSYS,
"Not implemented yet.");
return FALSE;
}
/* connect to main interface */
client->priv->proxy = up_exported_daemon_proxy_new_for_bus_sync (G_BUS_TYPE_SYSTEM,
G_DBUS_PROXY_FLAGS_NONE,
"org.freedesktop.UPower",
"/org/freedesktop/UPower",
cancellable,
error);
if (client->priv->proxy == NULL)
return FALSE;
/* all callbacks */
g_signal_connect (client->priv->proxy, "device-added",
G_CALLBACK (up_device_added_cb), client);
g_signal_connect (client->priv->proxy, "device-removed",
G_CALLBACK (up_device_removed_cb), client);
g_signal_connect (client->priv->proxy, "notify",
G_CALLBACK (up_client_notify_cb), client);
return TRUE;
}
static void
up_client_initable_iface_init (GInitableIface *iface)
{
iface->init = up_client_initable_init;
}
static void
up_client_init (UpClient *client)
{
client->priv = up_client_get_instance_private (client);
}
static void
up_client_finalize (GObject *object)
{
UpClient *client;
g_return_if_fail (UP_IS_CLIENT (object));
client = UP_CLIENT (object);
g_clear_object (&client->priv->proxy);
G_OBJECT_CLASS (up_client_parent_class)->finalize (object);
}
/**
* up_client_new_full:
* @cancellable: (allow-none): A #GCancellable or %NULL.
* @error: Return location for error or %NULL.
*
* Creates a new #UpClient object. If connecting to upowerd on D-Bus fails,
* this returns %NULL and sets @error.
*
* Return value: a new UpClient object, or %NULL on failure.
*
* Since: 0.99.5
**/
UpClient *
up_client_new_full (GCancellable *cancellable, GError **error)
{
return g_initable_new (UP_TYPE_CLIENT, cancellable, error, NULL);
}
/**
* up_client_new:
*
* Creates a new #UpClient object. If connecting to upowerd on D-Bus fails,
* this returns %NULL and prints out a warning with the error message.
* Consider using up_client_new_full() instead which allows you to handle errors
* and cancelling long operations yourself.
*
* Return value: a new UpClient object, or %NULL on failure.
*
* Since: 0.9.0
**/
UpClient *
up_client_new (void)
{
GError *error = NULL;
UpClient *client;
client = up_client_new_full (NULL, &error);
if (client == NULL) {
g_warning ("Couldn't connect to proxy: %s", error->message);
g_error_free (error);
}
return client;
}
/**
* up_client_new_session:
*
* Creates a new #UpClient object that will listen to UPower data on the session
* bus rather than on the system bus.
*
* Return value: a new UpClient object, or %NULL on failure.
*
* Since: 0.9.11
**/
UpClient *
up_client_new_session (GCancellable *cancellable, GError **error)
{
return g_initable_new (UP_TYPE_CLIENT, cancellable, error,
"bus-type", G_BUS_TYPE_SESSION,
NULL);
}
|