From 5f43bb78f7a089ff5079082d92e8488114786711 Mon Sep 17 00:00:00 2001 From: Bastien Nocera Date: Thu, 17 Oct 2013 14:36:35 +0200 Subject: lib: Remove up_client_enumerate_devices_sync() And make device-removed send an object path, not a UpDevice. This means that we don't keep all the remote devices as local UpDevices object, getting woken up any time any of them have a property changed, without anybody listening. This should greatly cut down on the wakeups on the client side, especially for applications that only use a small portion of the API like the "on-battery" or "warning-level" properties. --- libupower-glib/up-client.c | 131 ++++++++++++++------------------------------- libupower-glib/up-client.h | 5 +- 2 files changed, 41 insertions(+), 95 deletions(-) diff --git a/libupower-glib/up-client.c b/libupower-glib/up-client.c index 8ff81a5..35d7b5d 100644 --- a/libupower-glib/up-client.c +++ b/libupower-glib/up-client.c @@ -53,8 +53,6 @@ static void up_client_finalize (GObject *object); struct _UpClientPrivate { UpClientGlue *proxy; - GPtrArray *array; - gboolean done_enumerate; }; enum { @@ -78,33 +76,11 @@ static gpointer up_client_object = NULL; G_DEFINE_TYPE (UpClient, up_client, G_TYPE_OBJECT) -/* - * up_client_get_device: - */ -static UpDevice * -up_client_get_device (UpClient *client, const gchar *object_path) -{ - guint i; - const gchar *object_path_tmp; - UpDevice *device; - UpClientPrivate *priv = client->priv; - - for (i=0; iarray->len; i++) { - device = g_ptr_array_index (priv->array, i); - object_path_tmp = up_device_get_object_path (device); - if (g_strcmp0 (object_path_tmp, object_path) == 0) - return device; - } - return NULL; -} - /** * up_client_get_devices: * @client: a #UpClient instance. * * Get a copy of the device objects. - * You must have called up_client_enumerate_devices_sync() before calling this - * function. * * Return value: (element-type UpDevice) (transfer full): an array of #UpDevice objects, free with g_ptr_array_unref() * @@ -113,9 +89,41 @@ up_client_get_device (UpClient *client, const gchar *object_path) GPtrArray * up_client_get_devices (UpClient *client) { + GError *error = NULL; + char **devices; + GPtrArray *array; + guint i; + g_return_val_if_fail (UP_IS_CLIENT (client), NULL); - g_return_val_if_fail (client->priv->done_enumerate, NULL); - return g_ptr_array_ref (client->priv->array); + + array = g_ptr_array_new (); + + if (up_client_glue_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; + } + + 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; } /** @@ -259,16 +267,8 @@ static void up_client_add (UpClient *client, const gchar *object_path) { UpDevice *device = NULL; - UpDevice *device_tmp; gboolean ret; - /* check existing list for this object path */ - device_tmp = up_client_get_device (client, object_path); - if (device_tmp != NULL) { - g_warning ("already added: %s", object_path); - goto out; - } - /* create new device */ device = up_device_new (); ret = up_device_set_object_path_sync (device, object_path, NULL, NULL); @@ -276,7 +276,6 @@ up_client_add (UpClient *client, const gchar *object_path) goto out; /* add to array */ - g_ptr_array_add (client->priv->array, g_object_ref (device)); g_signal_emit (client, signals [UP_CLIENT_DEVICE_ADDED], 0, device); out: if (device != NULL) @@ -311,12 +310,7 @@ up_device_added_cb (UpClientGlue *proxy, const gchar *object_path, UpClient *cli static void up_device_removed_cb (UpClientGlue *proxy, const gchar *object_path, UpClient *client) { - UpDevice *device; - device = up_client_get_device (client, object_path); - if (device != NULL) { - g_signal_emit (client, signals [UP_CLIENT_DEVICE_REMOVED], 0, device); - g_ptr_array_remove (client->priv->array, device); - } + g_signal_emit (client, signals [UP_CLIENT_DEVICE_REMOVED], 0, object_path); } static void @@ -453,64 +447,22 @@ up_client_class_init (UpClientClass *klass) /** * UpClient::device-removed: * @client: the #UpClient instance that emitted the signal - * @device: the #UpDevice that was removed. + * @object_path: the object path of the #UpDevice that was removed. * - * The ::device-added signal is emitted when a power device is removed. + * The ::device-removed signal is emitted when a power device is removed. * - * Since: 0.9.0 + * 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__OBJECT, - G_TYPE_NONE, 1, UP_TYPE_DEVICE); + NULL, NULL, g_cclosure_marshal_VOID__STRING, + G_TYPE_NONE, 1, G_TYPE_STRING); g_type_class_add_private (klass, sizeof (UpClientPrivate)); } -/** - * up_client_enumerate_devices_sync: - * @client: a #UpClient instance. - * @error: a #GError, or %NULL. - * - * Enumerates all the devices from the daemon. - * - * Return value: %TRUE for success, else %FALSE. - * - * Since: 0.9.0 - **/ -gboolean -up_client_enumerate_devices_sync (UpClient *client, GCancellable *cancellable, GError **error) -{ - const gchar *object_path; - char **devices; - guint i; - gboolean ret = TRUE; - - /* already done */ - if (client->priv->done_enumerate) - goto out; - - /* coldplug */ - if (up_client_glue_call_enumerate_devices_sync (client->priv->proxy, - &devices, - NULL, - error) == FALSE) { - ret = FALSE; - goto out; - } - for (i = 0; devices[i] != NULL; i++) { - object_path = (const gchar *) devices[i]; - up_client_add (client, object_path); - } - - /* only do this once per instance */ - client->priv->done_enumerate = TRUE; -out: - return ret; -} - /* * up_client_init: * @client: This class instance @@ -521,7 +473,6 @@ up_client_init (UpClient *client) GError *error = NULL; client->priv = UP_CLIENT_GET_PRIVATE (client); - client->priv->array = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref); /* connect to main interface */ client->priv->proxy = up_client_glue_proxy_new_for_bus_sync (G_BUS_TYPE_SYSTEM, @@ -557,8 +508,6 @@ up_client_finalize (GObject *object) client = UP_CLIENT (object); - g_ptr_array_unref (client->priv->array); - if (client->priv->proxy != NULL) g_object_unref (client->priv->proxy); diff --git a/libupower-glib/up-client.h b/libupower-glib/up-client.h index 935a651..60bfb15 100644 --- a/libupower-glib/up-client.h +++ b/libupower-glib/up-client.h @@ -56,7 +56,7 @@ typedef struct void (*device_added) (UpClient *client, UpDevice *device); void (*device_removed) (UpClient *client, - UpDevice *device); + const gchar *object_path); /*< private >*/ /* Padding for future expansion */ void (*_up_client_reserved1) (void); @@ -74,9 +74,6 @@ GType up_client_get_type (void); UpClient *up_client_new (void); /* sync versions */ -gboolean up_client_enumerate_devices_sync (UpClient *client, - GCancellable *cancellable, - GError **error); UpDevice * up_client_get_display_device (UpClient *client); char * up_client_get_critical_action (UpClient *client); -- cgit v1.2.1