From 229c9b0322a88245cad966aa527458e291bae8d2 Mon Sep 17 00:00:00 2001 From: Richard Hughes Date: Mon, 5 Jan 2015 20:23:48 +0000 Subject: Add g_usb_device_get_children() This is slow, but it's not expected to be called 1000's of times. --- gusb/gusb-device.c | 29 +++++++++++++++++++++++++++++ gusb/gusb-device.h | 1 + 2 files changed, 30 insertions(+) diff --git a/gusb/gusb-device.c b/gusb/gusb-device.c index aa1fd36..5ddf687 100644 --- a/gusb/gusb-device.c +++ b/gusb/gusb-device.c @@ -33,6 +33,7 @@ #include +#include "gusb-cleanup.h" #include "gusb-context.h" #include "gusb-context-private.h" #include "gusb-util.h" @@ -92,6 +93,34 @@ g_usb_device_get_parent (GUsbDevice *device) NULL); } +/** + * g_usb_device_get_children: + * @device: a #GUsbDevice instance + * + * Gets the device children if any exist. + * + * Return value: (transfer full): an array of #GUsbDevice + **/ +GPtrArray * +g_usb_device_get_children (GUsbDevice *device) +{ + GPtrArray *children; + GUsbDevice *device_tmp; + GUsbDevicePrivate *priv = device->priv; + guint i; + _cleanup_ptrarray_unref_ GPtrArray *devices = NULL; + + /* find any devices that have @device as a parent */ + children = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref); + devices = g_usb_context_get_devices (priv->context); + for (i = 0; i < devices->len; i++) { + device_tmp = g_ptr_array_index (devices, i); + if (priv->device == libusb_get_parent (device_tmp->priv->device)) + g_ptr_array_add (children, g_object_ref (device_tmp)); + } + return children; +} + /** * g_usb_device_error_quark: * diff --git a/gusb/gusb-device.h b/gusb/gusb-device.h index 7ac3377..dc1c368 100644 --- a/gusb/gusb-device.h +++ b/gusb/gusb-device.h @@ -122,6 +122,7 @@ GQuark g_usb_device_error_quark (void); const gchar *g_usb_device_get_platform_id (GUsbDevice *device); GUsbDevice *g_usb_device_get_parent (GUsbDevice *device); +GPtrArray *g_usb_device_get_children (GUsbDevice *device); guint8 g_usb_device_get_bus (GUsbDevice *device); guint8 g_usb_device_get_address (GUsbDevice *device); -- cgit v1.2.1