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
|
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
*
* Copyright (C) 2007 David Zeuthen <davidz@redhat.com>
* Copyright (C) 2008 Richard Hughes <richard@hughsie.com>
*
* 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 St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
#include <sys/time.h>
#include <glib.h>
#include <glib/gi18n-lib.h>
#include <locale.h>
#include "up-client.h"
#include "up-device.h"
#include "up-wakeups.h"
static GMainLoop *loop;
static gboolean opt_monitor_detail = FALSE;
/**
* up_tool_get_timestamp:
**/
static gchar *
up_tool_get_timestamp (void)
{
gchar *str_time;
gchar *timestamp;
time_t the_time;
struct timeval time_val;
time (&the_time);
gettimeofday (&time_val, NULL);
str_time = g_new0 (gchar, 255);
strftime (str_time, 254, "%H:%M:%S", localtime (&the_time));
/* generate header text */
timestamp = g_strdup_printf ("%s.%03i", str_time, (gint) time_val.tv_usec / 1000);
g_free (str_time);
return timestamp;
}
/**
* up_tool_device_added_cb:
**/
static void
up_tool_device_added_cb (UpClient *client, UpDevice *device, gpointer user_data)
{
gchar *timestamp;
gchar *text = NULL;
timestamp = up_tool_get_timestamp ();
g_print ("[%s]\tdevice added: %s\n", timestamp, up_device_get_object_path (device));
if (opt_monitor_detail) {
text = up_device_to_text (device);
g_print ("%s\n", text);
}
g_free (timestamp);
g_free (text);
}
/**
* up_tool_device_changed_cb:
**/
static void
up_tool_device_changed_cb (UpDevice *device, GParamSpec *pspec, gpointer user_data)
{
gchar *timestamp;
gchar *text = NULL;
timestamp = up_tool_get_timestamp ();
g_print ("[%s]\tdevice changed: %s\n", timestamp, up_device_get_object_path (device));
if (opt_monitor_detail) {
/* TODO: would be nice to just show the diff */
text = up_device_to_text (device);
g_print ("%s\n", text);
}
g_free (timestamp);
g_free (text);
}
/**
* up_tool_device_removed_cb:
**/
static void
up_tool_device_removed_cb (UpClient *client, const char *object_path, gpointer user_data)
{
gchar *timestamp;
timestamp = up_tool_get_timestamp ();
g_print ("[%s]\tdevice removed: %s\n", timestamp, object_path);
if (opt_monitor_detail)
g_print ("\n");
g_free (timestamp);
}
/**
* up_client_print:
**/
static void
up_client_print (UpClient *client)
{
gchar *daemon_version;
gboolean on_battery;
gboolean lid_is_closed;
gboolean lid_is_present;
char *action;
g_object_get (client,
"daemon-version", &daemon_version,
"on-battery", &on_battery,
"lid-is-closed", &lid_is_closed,
"lid-is-present", &lid_is_present,
NULL);
g_print (" daemon-version: %s\n", daemon_version);
g_print (" on-battery: %s\n", on_battery ? "yes" : "no");
g_print (" lid-is-closed: %s\n", lid_is_closed ? "yes" : "no");
g_print (" lid-is-present: %s\n", lid_is_present ? "yes" : "no");
action = up_client_get_critical_action (client);
g_print (" critical-action: %s\n", action);
g_free (action);
g_free (daemon_version);
}
/**
* up_tool_changed_cb:
**/
static void
up_tool_changed_cb (UpClient *client, GParamSpec *pspec, gpointer user_data)
{
gchar *timestamp;
timestamp = up_tool_get_timestamp ();
g_print ("[%s]\tdaemon changed:\n", timestamp);
if (opt_monitor_detail) {
up_client_print (client);
g_print ("\n");
}
g_free (timestamp);
}
/**
* up_tool_do_monitor:
**/
static gboolean
up_tool_do_monitor (UpClient *client)
{
GPtrArray *devices;
guint i;
g_print ("Monitoring activity from the power daemon. Press Ctrl+C to cancel.\n");
g_signal_connect (client, "device-added", G_CALLBACK (up_tool_device_added_cb), NULL);
g_signal_connect (client, "device-removed", G_CALLBACK (up_tool_device_removed_cb), NULL);
g_signal_connect (client, "notify", G_CALLBACK (up_tool_changed_cb), NULL);
devices = up_client_get_devices (client);
for (i=0; i < devices->len; i++) {
UpDevice *device;
device = g_ptr_array_index (devices, i);
g_signal_connect (device, "notify", G_CALLBACK (up_tool_device_changed_cb), NULL);
}
g_main_loop_run (loop);
return FALSE;
}
/**
* up_tool_print_wakeup_item:
**/
static void
up_tool_print_wakeup_item (UpWakeupItem *item)
{
g_print ("userspace:%i id:%i, interrupts:%.1f, cmdline:%s, details:%s\n",
up_wakeup_item_get_is_userspace (item),
up_wakeup_item_get_id (item),
up_wakeup_item_get_value (item),
up_wakeup_item_get_cmdline (item),
up_wakeup_item_get_details (item));
}
/**
* up_tool_show_wakeups:
**/
static gboolean
up_tool_show_wakeups (void)
{
guint i;
gboolean ret;
UpWakeups *wakeups;
UpWakeupItem *item;
guint total;
GPtrArray *array;
/* create new object */
wakeups = up_wakeups_new ();
/* do we have support? */
ret = up_wakeups_get_has_capability (wakeups);
if (!ret) {
g_print ("No wakeup capability\n");
goto out;
}
/* get total */
total = up_wakeups_get_total_sync (wakeups, NULL, NULL);
g_print ("Total wakeups per minute: %i\n", total);
/* get data */
array = up_wakeups_get_data_sync (wakeups, NULL, NULL);
if (array == NULL)
goto out;
g_print ("Wakeup sources:\n");
for (i=0; i<array->len; i++) {
item = g_ptr_array_index (array, i);
up_tool_print_wakeup_item (item);
}
g_ptr_array_unref (array);
out:
g_object_unref (wakeups);
return ret;
}
/**
* main:
**/
int
main (int argc, char **argv)
{
gint retval = EXIT_FAILURE;
guint i;
GOptionContext *context;
gboolean opt_dump = FALSE;
gboolean opt_wakeups = FALSE;
gboolean opt_enumerate = FALSE;
gboolean opt_monitor = FALSE;
gchar *opt_show_info = FALSE;
gboolean opt_version = FALSE;
gboolean ret;
GError *error = NULL;
gchar *text = NULL;
UpClient *client;
UpDevice *device;
const GOptionEntry entries[] = {
{ "enumerate", 'e', 0, G_OPTION_ARG_NONE, &opt_enumerate, _("Enumerate objects paths for devices"), NULL },
{ "dump", 'd', 0, G_OPTION_ARG_NONE, &opt_dump, _("Dump all parameters for all objects"), NULL },
{ "wakeups", 'w', 0, G_OPTION_ARG_NONE, &opt_wakeups, _("Get the wakeup data"), NULL },
{ "monitor", 'm', 0, G_OPTION_ARG_NONE, &opt_monitor, _("Monitor activity from the power daemon"), NULL },
{ "monitor-detail", 0, 0, G_OPTION_ARG_NONE, &opt_monitor_detail, _("Monitor with detail"), NULL },
{ "show-info", 'i', 0, G_OPTION_ARG_STRING, &opt_show_info, _("Show information about object path"), NULL },
{ "version", 'v', 0, G_OPTION_ARG_NONE, &opt_version, "Print version of client and daemon", NULL },
{ NULL }
};
#if !defined(GLIB_VERSION_2_36)
g_type_init ();
#endif
setlocale(LC_ALL, "");
context = g_option_context_new ("UPower tool");
g_option_context_add_main_entries (context, entries, NULL);
g_option_context_parse (context, &argc, &argv, NULL);
g_option_context_free (context);
loop = g_main_loop_new (NULL, FALSE);
client = up_client_new_full (NULL, &error);
if (client == NULL) {
g_warning ("Cannot connect to upowerd: %s", error->message);
g_error_free (error);
return EXIT_FAILURE;
}
if (opt_version) {
gchar *daemon_version;
g_object_get (client,
"daemon-version", &daemon_version,
NULL);
g_print ("UPower client version %s\n"
"UPower daemon version %s\n",
PACKAGE_VERSION, daemon_version);
g_free (daemon_version);
retval = 0;
goto out;
}
/* wakeups */
if (opt_wakeups) {
up_tool_show_wakeups ();
retval = EXIT_SUCCESS;
goto out;
}
if (opt_enumerate || opt_dump) {
GPtrArray *devices;
devices = up_client_get_devices (client);
for (i=0; i < devices->len; i++) {
device = (UpDevice*) g_ptr_array_index (devices, i);
if (opt_enumerate) {
g_print ("%s\n", up_device_get_object_path (device));
} else {
g_print ("Device: %s\n", up_device_get_object_path (device));
text = up_device_to_text (device);
g_print ("%s\n", text);
g_free (text);
}
}
g_ptr_array_unref (devices);
device = up_client_get_display_device (client);
if (opt_enumerate) {
g_print ("%s\n", up_device_get_object_path (device));
} else {
g_print ("Device: %s\n", up_device_get_object_path (device));
text = up_device_to_text (device);
g_print ("%s\n", text);
g_free (text);
}
g_object_unref (device);
if (opt_dump) {
g_print ("Daemon:\n");
up_client_print (client);
}
retval = EXIT_SUCCESS;
goto out;
}
if (opt_monitor || opt_monitor_detail) {
if (!up_tool_do_monitor (client))
goto out;
retval = EXIT_SUCCESS;
goto out;
}
if (opt_show_info != NULL) {
device = up_device_new ();
ret = up_device_set_object_path_sync (device, opt_show_info, NULL, &error);
if (!ret) {
g_print ("failed to set path: %s\n", error->message);
g_error_free (error);
} else {
text = up_device_to_text (device);
g_print ("%s\n", text);
g_free (text);
}
g_object_unref (device);
retval = EXIT_SUCCESS;
goto out;
}
out:
g_object_unref (client);
return retval;
}
|