2008-08-01 13:15:40 +01:00
|
|
|
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
|
|
|
|
|
*
|
2008-11-11 10:13:24 -05:00
|
|
|
* Copyright (C) 2007 David Zeuthen <davidz@redhat.com>
|
2008-08-01 16:48:33 +01:00
|
|
|
* Copyright (C) 2008 Richard Hughes <richard@hughsie.com>
|
2008-08-01 13:15:40 +01:00
|
|
|
*
|
|
|
|
|
* 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 <glib.h>
|
|
|
|
|
#include <glib/gi18n-lib.h>
|
|
|
|
|
|
|
|
|
|
#include "dkp-marshal.h"
|
2008-08-05 17:26:35 +01:00
|
|
|
#include "dkp-client.h"
|
2009-01-23 17:14:02 +00:00
|
|
|
#include "dkp-device.h"
|
2009-01-30 13:55:42 +00:00
|
|
|
#include "dkp-wakeups.h"
|
2009-01-23 17:14:02 +00:00
|
|
|
|
|
|
|
|
#include "egg-debug.h"
|
2008-08-05 17:26:35 +01:00
|
|
|
|
2008-08-01 13:15:40 +01:00
|
|
|
static GMainLoop *loop;
|
|
|
|
|
static gboolean opt_monitor_detail = FALSE;
|
|
|
|
|
|
|
|
|
|
/**
|
2008-08-01 16:48:33 +01:00
|
|
|
* dkp_tool_device_added_cb:
|
2008-08-01 13:15:40 +01:00
|
|
|
**/
|
|
|
|
|
static void
|
2009-01-23 17:14:02 +00:00
|
|
|
dkp_tool_device_added_cb (DkpClient *client, const DkpDevice *device, gpointer user_data)
|
2008-08-01 13:15:40 +01:00
|
|
|
{
|
2009-01-23 17:14:02 +00:00
|
|
|
g_print ("device added: %s\n", dkp_device_get_object_path (device));
|
2008-08-01 13:15:40 +01:00
|
|
|
if (opt_monitor_detail) {
|
2009-01-23 17:14:02 +00:00
|
|
|
dkp_device_print (device);
|
2008-11-11 13:58:26 -05:00
|
|
|
g_print ("\n");
|
2008-08-01 13:15:40 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2008-08-01 16:48:33 +01:00
|
|
|
* dkp_tool_device_changed_cb:
|
2008-08-01 13:15:40 +01:00
|
|
|
**/
|
|
|
|
|
static void
|
2009-01-23 17:14:02 +00:00
|
|
|
dkp_tool_device_changed_cb (DkpClient *client, const DkpDevice *device, gpointer user_data)
|
2008-08-01 13:15:40 +01:00
|
|
|
{
|
2009-01-23 17:14:02 +00:00
|
|
|
g_print ("device changed: %s\n", dkp_device_get_object_path (device));
|
2008-08-01 13:15:40 +01:00
|
|
|
if (opt_monitor_detail) {
|
|
|
|
|
/* TODO: would be nice to just show the diff */
|
2009-01-23 17:14:02 +00:00
|
|
|
dkp_device_print (device);
|
2008-11-11 13:58:26 -05:00
|
|
|
g_print ("\n");
|
2008-08-01 13:15:40 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2008-08-01 16:48:33 +01:00
|
|
|
* dkp_tool_device_removed_cb:
|
2008-08-01 13:15:40 +01:00
|
|
|
**/
|
|
|
|
|
static void
|
2009-01-23 17:14:02 +00:00
|
|
|
dkp_tool_device_removed_cb (DkpClient *client, const DkpDevice *device, gpointer user_data)
|
2008-08-01 13:15:40 +01:00
|
|
|
{
|
2009-01-23 17:14:02 +00:00
|
|
|
g_print ("device removed: %s\n", dkp_device_get_object_path (device));
|
2008-11-11 13:58:26 -05:00
|
|
|
if (opt_monitor_detail)
|
|
|
|
|
g_print ("\n");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
dkp_client_print (DkpClient *client)
|
|
|
|
|
{
|
|
|
|
|
g_print (" daemon-version: %s\n", dkp_client_get_daemon_version (client));
|
|
|
|
|
g_print (" can-suspend: %s\n", dkp_client_can_suspend (client) ? "yes" : "no");
|
|
|
|
|
g_print (" can-hibernate %s\n", dkp_client_can_hibernate (client) ? "yes" : "no");
|
|
|
|
|
g_print (" on-battery: %s\n", dkp_client_on_battery (client) ? "yes" : "no");
|
|
|
|
|
g_print (" on-low-battery: %s\n", dkp_client_on_low_battery (client) ? "yes" : "no");
|
2009-05-05 12:51:26 +01:00
|
|
|
g_print (" lid-is-closed: %s\n", dkp_client_lid_is_closed (client) ? "yes" : "no");
|
2008-11-11 13:58:26 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* dkp_tool_changed_cb:
|
|
|
|
|
**/
|
|
|
|
|
static void
|
|
|
|
|
dkp_tool_changed_cb (DkpClient *client, gpointer user_data)
|
|
|
|
|
{
|
|
|
|
|
g_print ("daemon changed:\n");
|
|
|
|
|
if (opt_monitor_detail) {
|
|
|
|
|
dkp_client_print (client);
|
|
|
|
|
g_print ("\n");
|
|
|
|
|
}
|
2008-08-01 13:15:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2008-08-01 16:48:33 +01:00
|
|
|
* dkp_tool_do_monitor:
|
2008-08-01 13:15:40 +01:00
|
|
|
**/
|
|
|
|
|
static gboolean
|
2008-08-05 17:26:35 +01:00
|
|
|
dkp_tool_do_monitor (DkpClient *client)
|
2008-08-01 13:15:40 +01:00
|
|
|
{
|
|
|
|
|
g_print ("Monitoring activity from the power daemon. Press Ctrl+C to cancel.\n");
|
|
|
|
|
|
2008-11-11 13:58:26 -05:00
|
|
|
g_signal_connect (client, "device-added", G_CALLBACK (dkp_tool_device_added_cb), NULL);
|
|
|
|
|
g_signal_connect (client, "device-removed", G_CALLBACK (dkp_tool_device_removed_cb), NULL);
|
|
|
|
|
g_signal_connect (client, "device-changed", G_CALLBACK (dkp_tool_device_changed_cb), NULL);
|
|
|
|
|
g_signal_connect (client, "changed", G_CALLBACK (dkp_tool_changed_cb), NULL);
|
2008-08-05 17:26:35 +01:00
|
|
|
|
2008-08-01 13:15:40 +01:00
|
|
|
g_main_loop_run (loop);
|
|
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
|
2009-04-22 16:55:24 +01:00
|
|
|
/**
|
|
|
|
|
* dkp_tool_show_wakeups:
|
|
|
|
|
**/
|
|
|
|
|
static gboolean
|
|
|
|
|
dkp_tool_show_wakeups (void)
|
|
|
|
|
{
|
|
|
|
|
guint i;
|
|
|
|
|
gboolean ret;
|
|
|
|
|
DkpWakeups *wakeups;
|
|
|
|
|
DkpWakeupsObj *obj;
|
|
|
|
|
guint total;
|
|
|
|
|
GPtrArray *array;
|
|
|
|
|
|
|
|
|
|
/* create new object */
|
|
|
|
|
wakeups = dkp_wakeups_new ();
|
|
|
|
|
|
|
|
|
|
/* do we have support? */
|
|
|
|
|
ret = dkp_wakeups_has_capability (wakeups);
|
|
|
|
|
if (!ret) {
|
|
|
|
|
g_print ("No wakeup capability\n");
|
|
|
|
|
goto out;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* get total */
|
|
|
|
|
total = dkp_wakeups_get_total (wakeups, NULL);
|
|
|
|
|
g_print ("Total wakeups per minute: %i\n", total);
|
|
|
|
|
|
|
|
|
|
/* get data */
|
|
|
|
|
array = dkp_wakeups_get_data (wakeups, NULL);
|
|
|
|
|
if (array == NULL)
|
|
|
|
|
goto out;
|
|
|
|
|
g_print ("Wakeup sources:\n");
|
|
|
|
|
for (i=0; i<array->len; i++) {
|
|
|
|
|
obj = g_ptr_array_index (array, i);
|
|
|
|
|
dkp_wakeups_obj_print (obj);
|
|
|
|
|
}
|
|
|
|
|
g_ptr_array_foreach (array, (GFunc) dkp_wakeups_obj_free, NULL);
|
|
|
|
|
g_ptr_array_free (array, TRUE);
|
|
|
|
|
out:
|
|
|
|
|
g_object_unref (wakeups);
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2008-08-01 13:15:40 +01:00
|
|
|
/**
|
|
|
|
|
* main:
|
|
|
|
|
**/
|
|
|
|
|
int
|
|
|
|
|
main (int argc, char **argv)
|
|
|
|
|
{
|
2009-02-27 13:18:57 +00:00
|
|
|
gint retval = 1;
|
2009-01-30 13:55:42 +00:00
|
|
|
guint i;
|
2008-08-01 13:15:40 +01:00
|
|
|
GOptionContext *context;
|
|
|
|
|
gboolean verbose = FALSE;
|
2008-08-04 11:54:15 +01:00
|
|
|
gboolean opt_dump = FALSE;
|
2009-01-30 13:55:42 +00:00
|
|
|
gboolean opt_wakeups = FALSE;
|
2008-08-06 06:58:45 +01:00
|
|
|
gboolean opt_enumerate = FALSE;
|
|
|
|
|
gboolean opt_monitor = FALSE;
|
|
|
|
|
gchar *opt_show_info = FALSE;
|
2008-11-11 13:58:26 -05:00
|
|
|
gboolean opt_version = FALSE;
|
2009-02-27 13:18:57 +00:00
|
|
|
gboolean ret;
|
|
|
|
|
GError *error = NULL;
|
2008-08-01 13:15:40 +01:00
|
|
|
|
2008-08-05 17:26:35 +01:00
|
|
|
DkpClient *client;
|
2009-01-23 17:14:02 +00:00
|
|
|
DkpDevice *device;
|
2008-08-05 17:26:35 +01:00
|
|
|
|
2008-08-01 13:15:40 +01:00
|
|
|
const GOptionEntry entries[] = {
|
|
|
|
|
{ "verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose, _("Show extra debugging information"), NULL },
|
2008-08-05 15:39:05 +01:00
|
|
|
{ "enumerate", 'e', 0, G_OPTION_ARG_NONE, &opt_enumerate, _("Enumerate objects paths for devices"), NULL },
|
2008-08-05 17:26:35 +01:00
|
|
|
{ "dump", 'd', 0, G_OPTION_ARG_NONE, &opt_dump, _("Dump all parameters for all objects"), NULL },
|
2009-01-30 13:55:42 +00:00
|
|
|
{ "wakeups", 'w', 0, G_OPTION_ARG_NONE, &opt_wakeups, _("Get the wakeup data"), NULL },
|
2008-08-05 17:26:35 +01:00
|
|
|
{ "monitor", 'm', 0, G_OPTION_ARG_NONE, &opt_monitor, _("Monitor activity from the power daemon"), NULL },
|
2008-08-01 13:15:40 +01:00
|
|
|
{ "monitor-detail", 0, 0, G_OPTION_ARG_NONE, &opt_monitor_detail, _("Monitor with detail"), NULL },
|
2008-08-05 17:26:35 +01:00
|
|
|
{ "show-info", 'i', 0, G_OPTION_ARG_STRING, &opt_show_info, _("Show information about object path"), NULL },
|
2008-11-12 09:58:36 +00:00
|
|
|
{ "version", 'v', 0, G_OPTION_ARG_NONE, &opt_version, "Print version of client and daemon", NULL },
|
2008-08-01 13:15:40 +01:00
|
|
|
{ NULL }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
g_type_init ();
|
|
|
|
|
|
|
|
|
|
context = g_option_context_new ("DeviceKit-power tool");
|
|
|
|
|
g_option_context_add_main_entries (context, entries, NULL);
|
|
|
|
|
g_option_context_parse (context, &argc, &argv, NULL);
|
|
|
|
|
g_option_context_free (context);
|
2008-08-28 15:49:19 +01:00
|
|
|
egg_debug_init (verbose);
|
2008-08-01 13:15:40 +01:00
|
|
|
|
|
|
|
|
loop = g_main_loop_new (NULL, FALSE);
|
2008-08-05 17:26:35 +01:00
|
|
|
client = dkp_client_new ();
|
2008-08-01 13:15:40 +01:00
|
|
|
|
2008-11-11 13:58:26 -05:00
|
|
|
if (opt_version) {
|
2008-11-12 09:58:36 +00:00
|
|
|
g_print ("DeviceKit-power client version %s\n"
|
|
|
|
|
"DeviceKit-power daemon version %s\n",
|
|
|
|
|
PACKAGE_VERSION,
|
|
|
|
|
dkp_client_get_daemon_version (client));
|
2009-02-27 13:18:57 +00:00
|
|
|
retval = 0;
|
2008-11-11 13:58:26 -05:00
|
|
|
goto out;
|
|
|
|
|
}
|
|
|
|
|
|
2009-01-30 13:55:42 +00:00
|
|
|
if (opt_wakeups) {
|
2009-04-22 16:55:24 +01:00
|
|
|
dkp_tool_show_wakeups ();
|
2009-01-30 13:55:42 +00:00
|
|
|
} else if (opt_enumerate || opt_dump) {
|
2008-08-01 13:15:40 +01:00
|
|
|
GPtrArray *devices;
|
2009-04-01 09:15:04 +01:00
|
|
|
devices = dkp_client_enumerate_devices (client, &error);
|
|
|
|
|
if (devices == NULL) {
|
|
|
|
|
egg_warning ("failed to enumerate: %s", error->message);
|
2008-08-01 13:15:40 +01:00
|
|
|
goto out;
|
2009-04-01 09:15:04 +01:00
|
|
|
}
|
2009-01-30 13:55:42 +00:00
|
|
|
for (i=0; i < devices->len; i++) {
|
|
|
|
|
device = (DkpDevice*) g_ptr_array_index (devices, i);
|
2008-11-11 13:58:26 -05:00
|
|
|
if (opt_enumerate) {
|
2009-01-23 17:14:02 +00:00
|
|
|
g_print ("%s\n", dkp_device_get_object_path (device));
|
2008-11-11 13:58:26 -05:00
|
|
|
} else {
|
2009-01-23 17:14:02 +00:00
|
|
|
g_print ("Device: %s\n", dkp_device_get_object_path (device));
|
|
|
|
|
dkp_device_print (device);
|
2008-11-11 13:58:26 -05:00
|
|
|
g_print ("\n");
|
2008-08-04 16:41:58 +01:00
|
|
|
}
|
2008-08-01 13:15:40 +01:00
|
|
|
}
|
2009-01-23 17:14:02 +00:00
|
|
|
g_ptr_array_foreach (devices, (GFunc) g_object_unref, NULL);
|
2008-08-01 13:15:40 +01:00
|
|
|
g_ptr_array_free (devices, TRUE);
|
2008-11-11 13:58:26 -05:00
|
|
|
if (opt_dump) {
|
|
|
|
|
g_print ("Daemon:\n");
|
|
|
|
|
dkp_client_print (client);
|
|
|
|
|
}
|
2008-08-01 13:15:40 +01:00
|
|
|
} else if (opt_monitor || opt_monitor_detail) {
|
2008-08-05 17:26:35 +01:00
|
|
|
if (!dkp_tool_do_monitor (client))
|
2008-08-01 13:15:40 +01:00
|
|
|
goto out;
|
|
|
|
|
} else if (opt_show_info != NULL) {
|
2009-01-23 17:14:02 +00:00
|
|
|
device = dkp_device_new ();
|
2009-02-27 13:18:57 +00:00
|
|
|
ret = dkp_device_set_object_path (device, opt_show_info, &error);
|
|
|
|
|
if (!ret) {
|
|
|
|
|
g_print ("failed to set path: %s\n", error->message);
|
|
|
|
|
g_error_free (error);
|
|
|
|
|
} else {
|
|
|
|
|
dkp_device_print (device);
|
|
|
|
|
retval = 0;
|
|
|
|
|
}
|
2008-08-05 17:26:35 +01:00
|
|
|
g_object_unref (device);
|
2008-08-01 13:15:40 +01:00
|
|
|
}
|
|
|
|
|
|
2009-02-27 13:18:57 +00:00
|
|
|
retval = 0;
|
2008-08-01 13:15:40 +01:00
|
|
|
out:
|
2008-08-05 17:26:35 +01:00
|
|
|
g_object_unref (client);
|
2009-02-27 13:18:57 +00:00
|
|
|
return retval;
|
2008-08-01 13:15:40 +01:00
|
|
|
}
|