From 67c36ceff0486d473b58b4e02fa89557f5826396 Mon Sep 17 00:00:00 2001 From: Richard Hughes Date: Wed, 22 Apr 2009 16:55:24 +0100 Subject: Add a has-capability to the org.freedesktop.DeviceKit.Power.Wakeups interface so clients can tell if the wakeups functionality can be supported --- devkit-power-gobject/dkp-wakeups.c | 71 +++++++++++++++++++++++++ devkit-power-gobject/dkp-wakeups.h | 1 + src/dkp-wakeups.c | 56 +++++++++++++++++++ src/org.freedesktop.DeviceKit.Power.Wakeups.xml | 10 ++++ tools/dkp-tool.c | 61 +++++++++++++++------ 5 files changed, 182 insertions(+), 17 deletions(-) diff --git a/devkit-power-gobject/dkp-wakeups.c b/devkit-power-gobject/dkp-wakeups.c index 4c39c25..7b379f4 100644 --- a/devkit-power-gobject/dkp-wakeups.c +++ b/devkit-power-gobject/dkp-wakeups.c @@ -38,6 +38,9 @@ struct DkpWakeupsPrivate { DBusGConnection *bus; DBusGProxy *proxy; + DBusGProxy *prop_proxy; + gboolean has_capability; + gboolean have_properties; }; enum { @@ -164,6 +167,60 @@ out: return array; } +/** + * dkp_wakeups_ensure_properties: + **/ +static void +dkp_wakeups_ensure_properties (DkpWakeups *wakeups) +{ + gboolean ret; + GError *error; + GHashTable *props; + GValue *value; + + props = NULL; + + if (wakeups->priv->have_properties) + goto out; + + error = NULL; + ret = dbus_g_proxy_call (wakeups->priv->prop_proxy, "GetAll", &error, + G_TYPE_STRING, "org.freedesktop.DeviceKit.Power.Wakeups", + G_TYPE_INVALID, + dbus_g_type_get_map ("GHashTable", G_TYPE_STRING, G_TYPE_VALUE), &props, + G_TYPE_INVALID); + if (!ret) { + g_warning ("Error invoking GetAll() to get properties: %s", error->message); + g_error_free (error); + goto out; + } + + value = g_hash_table_lookup (props, "has-capability"); + if (value == NULL) { + g_warning ("No 'has-capability' property"); + goto out; + } + wakeups->priv->has_capability = g_value_get_boolean (value); + + /* cached */ + wakeups->priv->have_properties = TRUE; + +out: + if (props != NULL) + g_hash_table_unref (props); +} + +/** + * dkp_wakeups_has_capability: + **/ +gboolean +dkp_wakeups_has_capability (DkpWakeups *wakeups) +{ + g_return_val_if_fail (DKP_IS_WAKEUPS (wakeups), FALSE); + dkp_wakeups_ensure_properties (wakeups); + return wakeups->priv->has_capability; +} + /** * dkp_wakeups_total_changed_cb: **/ @@ -218,6 +275,8 @@ dkp_wakeups_init (DkpWakeups *wakeups) GError *error = NULL; wakeups->priv = DKP_WAKEUPS_GET_PRIVATE (wakeups); + wakeups->priv->has_capability = FALSE; + wakeups->priv->have_properties = FALSE; /* get on the bus */ wakeups->priv->bus = dbus_g_bus_get (DBUS_BUS_SYSTEM, &error); @@ -227,6 +286,16 @@ dkp_wakeups_init (DkpWakeups *wakeups) goto out; } + /* connect to properties interface */ + wakeups->priv->prop_proxy = dbus_g_proxy_new_for_name (wakeups->priv->bus, + "org.freedesktop.DeviceKit.Power", + "/org/freedesktop/DeviceKit/Power/Wakeups", + "org.freedesktop.DBus.Properties"); + if (wakeups->priv->prop_proxy == NULL) { + g_warning ("Couldn't connect to proxy"); + goto out; + } + /* connect to main interface */ wakeups->priv->proxy = dbus_g_proxy_new_for_name (wakeups->priv->bus, "org.freedesktop.DeviceKit.Power", @@ -262,6 +331,8 @@ dkp_wakeups_finalize (GObject *object) wakeups = DKP_WAKEUPS (object); if (wakeups->priv->proxy != NULL) g_object_unref (wakeups->priv->proxy); + if (wakeups->priv->prop_proxy != NULL) + g_object_unref (wakeups->priv->prop_proxy); G_OBJECT_CLASS (dkp_wakeups_parent_class)->finalize (object); } diff --git a/devkit-power-gobject/dkp-wakeups.h b/devkit-power-gobject/dkp-wakeups.h index f03fc40..7ff10e3 100644 --- a/devkit-power-gobject/dkp-wakeups.h +++ b/devkit-power-gobject/dkp-wakeups.h @@ -64,6 +64,7 @@ guint dkp_wakeups_get_total (DkpWakeups *wakeups, GError **error); GPtrArray *dkp_wakeups_get_data (DkpWakeups *wakeups, GError **error); +gboolean dkp_wakeups_has_capability (DkpWakeups *wakeups); G_END_DECLS diff --git a/src/dkp-wakeups.c b/src/dkp-wakeups.c index 019c136..c081ae3 100644 --- a/src/dkp-wakeups.c +++ b/src/dkp-wakeups.c @@ -68,6 +68,12 @@ struct DkpWakeupsPrivate guint poll_kernel_id; guint disable_id; gboolean polling_enabled; + gboolean has_capability; +}; + +enum { + PROP_0, + PROP_HAS_CAPABILITY, }; enum { @@ -165,6 +171,12 @@ dkp_wakeups_get_total (DkpWakeups *wakeups, guint *value, GError **error) { gboolean ret; + /* no capability */ + if (!wakeups->priv->has_capability) { + *error = g_error_new (DKP_DAEMON_ERROR, DKP_DAEMON_ERROR_GENERAL, "no hardware support"); + return FALSE; + } + /* start if not already started */ ret = dkp_wakeups_timerstats_enable (wakeups); @@ -189,6 +201,12 @@ dkp_wakeups_get_data (DkpWakeups *wakeups, GPtrArray **data, GError **error) GPtrArray *array; DkpWakeupsObj *obj; + /* no capability */ + if (!wakeups->priv->has_capability) { + *error = g_error_new (DKP_DAEMON_ERROR, DKP_DAEMON_ERROR_GENERAL, "no hardware support"); + return FALSE; + } + /* start if not already started */ dkp_wakeups_timerstats_enable (wakeups); @@ -636,6 +654,28 @@ dkp_wakeups_timerstats_enable (DkpWakeups *wakeups) return TRUE; } +/** + * dkp_wakeups_get_property: + **/ +static void +dkp_wakeups_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec) +{ + DkpWakeups *wakeups; + + wakeups = DKP_WAKEUPS (object); + + switch (prop_id) { + + case PROP_HAS_CAPABILITY: + g_value_set_boolean (value, wakeups->priv->has_capability); + break; + + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + break; + } +} + /** * dkp_wakeups_class_init: **/ @@ -644,6 +684,7 @@ dkp_wakeups_class_init (DkpWakeupsClass *klass) { GObjectClass *object_class = G_OBJECT_CLASS (klass); object_class->finalize = dkp_wakeups_finalize; + object_class->get_property = dkp_wakeups_get_property; signals [TOTAL_CHANGED] = g_signal_new ("total-changed", @@ -658,6 +699,14 @@ dkp_wakeups_class_init (DkpWakeupsClass *klass) NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0); + g_object_class_install_property (object_class, + PROP_HAS_CAPABILITY, + g_param_spec_boolean ("has-capability", + "Has capability", + "If wakeups functionality is available", + FALSE, + G_PARAM_READABLE)); + /* introspection */ dbus_g_object_type_install_info (DKP_TYPE_WAKEUPS, &dbus_glib_dkp_wakeups_object_info); @@ -678,6 +727,7 @@ dkp_wakeups_init (DkpWakeups *wakeups) wakeups->priv->total_ave = 0; wakeups->priv->poll_userspace_id = 0; wakeups->priv->poll_kernel_id = 0; + wakeups->priv->has_capability = FALSE; wakeups->priv->polling_enabled = FALSE; wakeups->priv->connection = dbus_g_bus_get (DBUS_BUS_SYSTEM, &error); @@ -687,6 +737,12 @@ dkp_wakeups_init (DkpWakeups *wakeups) return; } + /* test if we have an interface */ + if (g_file_test (DKP_WAKEUPS_SOURCE_KERNEL, G_FILE_TEST_EXISTS) || + g_file_test (DKP_WAKEUPS_SOURCE_KERNEL, G_FILE_TEST_EXISTS)) { + wakeups->priv->has_capability = TRUE; + } + /* register on the bus */ dbus_g_connection_register_g_object (wakeups->priv->connection, "/org/freedesktop/DeviceKit/Power/Wakeups", G_OBJECT (wakeups)); } diff --git a/src/org.freedesktop.DeviceKit.Power.Wakeups.xml b/src/org.freedesktop.DeviceKit.Power.Wakeups.xml index d31ab7c..d3e0342 100644 --- a/src/org.freedesktop.DeviceKit.Power.Wakeups.xml +++ b/src/org.freedesktop.DeviceKit.Power.Wakeups.xml @@ -16,6 +16,16 @@ + + + + + If the system has the ability to profile wakeups. + + + + + diff --git a/tools/dkp-tool.c b/tools/dkp-tool.c index 753354a..15723f0 100644 --- a/tools/dkp-tool.c +++ b/tools/dkp-tool.c @@ -119,6 +119,49 @@ dkp_tool_do_monitor (DkpClient *client) return FALSE; } +/** + * 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; ilen; 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; +} + /** * main: **/ @@ -174,23 +217,7 @@ main (int argc, char **argv) } if (opt_wakeups) { - DkpWakeups *wakeups; - DkpWakeupsObj *obj; - guint total; - GPtrArray *array; - wakeups = dkp_wakeups_new (); - total = dkp_wakeups_get_total (wakeups, NULL); - g_print ("Total wakeups per minute: %i\n", total); - array = dkp_wakeups_get_data (wakeups, NULL); - if (array == NULL) - goto out; - g_print ("Wakeup sources:\n"); - for (i=0; ilen; 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); + dkp_tool_show_wakeups (); } else if (opt_enumerate || opt_dump) { GPtrArray *devices; devices = dkp_client_enumerate_devices (client, &error); -- cgit v1.2.1