[Spice-devel] [spice-gtk][PATCHv2] usb-device-manager: Add spice_usb_device_manager_get_devices_by_classes

Fabiano Fidêncio fidencio at redhat.com
Thu Jul 17 02:43:08 PDT 2014


This function allows the applications to get USB devices filtered by the
devices' classes. The applications that are going to use this function
should provide a list of USB devices class code (based on
http://www.usb.org/developers/defined_class), then the function will
create a string in the expcted format, representing the filter, and call
spice_usb_device_manager_get_devices_with_filter()

---
 doc/reference/spice-gtk-sections.txt |  1 +
 gtk/map-file                         |  1 +
 gtk/spice-glib-sym-file              |  1 +
 gtk/usb-device-manager.c             | 45 ++++++++++++++++++++++++++++++++++++
 gtk/usb-device-manager.h             |  2 ++
 5 files changed, 50 insertions(+)

diff --git a/doc/reference/spice-gtk-sections.txt b/doc/reference/spice-gtk-sections.txt
index caaa92c..03dcec3 100644
--- a/doc/reference/spice-gtk-sections.txt
+++ b/doc/reference/spice-gtk-sections.txt
@@ -298,6 +298,7 @@ SpiceUsbDeviceManagerClass
 spice_usb_device_manager_get
 spice_usb_device_manager_get_devices
 spice_usb_device_manager_get_devices_with_filter
+spice_usb_device_manager_get_devices_by_classes
 spice_usb_device_manager_is_device_connected
 spice_usb_device_manager_disconnect_device
 spice_usb_device_manager_can_redirect_device
diff --git a/gtk/map-file b/gtk/map-file
index 90f14f1..0a31dc0 100644
--- a/gtk/map-file
+++ b/gtk/map-file
@@ -110,6 +110,7 @@ spice_usb_device_manager_connect_device_finish;
 spice_usb_device_manager_disconnect_device;
 spice_usb_device_manager_get;
 spice_usb_device_manager_get_devices;
+spice_usb_device_manager_get_devices_by_classes;
 spice_usb_device_manager_get_devices_with_filter;
 spice_usb_device_manager_get_type;
 spice_usb_device_manager_is_device_connected;
diff --git a/gtk/spice-glib-sym-file b/gtk/spice-glib-sym-file
index 878dd12..3c5a785 100644
--- a/gtk/spice-glib-sym-file
+++ b/gtk/spice-glib-sym-file
@@ -85,6 +85,7 @@ spice_usb_device_manager_connect_device_finish
 spice_usb_device_manager_disconnect_device
 spice_usb_device_manager_get
 spice_usb_device_manager_get_devices
+spice_usb_device_manager_get_devices_by_classes
 spice_usb_device_manager_get_devices_with_filter
 spice_usb_device_manager_get_type
 spice_usb_device_manager_is_device_connected
diff --git a/gtk/usb-device-manager.c b/gtk/usb-device-manager.c
index 5013b6c..7e65991 100644
--- a/gtk/usb-device-manager.c
+++ b/gtk/usb-device-manager.c
@@ -1379,6 +1379,51 @@ GPtrArray* spice_usb_device_manager_get_devices(SpiceUsbDeviceManager *self)
 }
 
 /**
+ * spice_usb_device_manager_get_devices_by_classes:
+ * @manager: the #SpiceUsbDeviceManager manager
+ * @classes: (element-type gint) (allow-none): a %GList of classes for selecting
+ *           which devices to return or %NULL
+ *
+ * Returns: (element-type SpiceUsbDevice) (transfer full): a %GPtrArray array of %SpiceUsbDevice
+ *
+ * Since: 0.26
+ */
+GPtrArray* spice_usb_device_manager_get_devices_by_classes(SpiceUsbDeviceManager *self,
+                                                           GList *classes)
+{
+    GPtrArray *devices;
+    GList *l;
+    gchar *filter = NULL;
+
+    /* Builds the filter based on the USB classes received from the user. As this function
+       filters only by the device class, another fields as vendor id, product id and device
+       version bcd are automatically set to match any id/version and allow is always set to
+       "allows redir for this device" (actually, it doesn't matter when we are just querying
+       a list of devices).
+
+       A filter rule has the form of @class, at vendor, at product, at version, at allow and the rules,
+       themselves, are concatenated like @rule1|@rule2|@rule3 */
+    for (l = classes; l != NULL; l = l->next) {
+        enum libusb_class_code code = GPOINTER_TO_INT(l->data);
+        if (filter == NULL) {
+            filter = g_strdup_printf("0x%02x,-1,-1,-1,1", code);
+        } else {
+            gchar *tmp_filter = NULL;
+
+            tmp_filter = g_strdup_printf("%s|0x%02x,-1,-1,-1,1", filter, code);
+
+            g_free(filter);
+            filter = tmp_filter;
+        }
+    }
+
+    devices = spice_usb_device_manager_get_devices_with_filter(self, filter);
+    g_free(filter);
+
+    return devices;
+}
+
+/**
  * spice_usb_device_manager_is_device_connected:
  * @manager: the #SpiceUsbDeviceManager manager
  * @device: a #SpiceUsbDevice
diff --git a/gtk/usb-device-manager.h b/gtk/usb-device-manager.h
index a7e3515..e6db56f 100644
--- a/gtk/usb-device-manager.h
+++ b/gtk/usb-device-manager.h
@@ -96,6 +96,8 @@ SpiceUsbDeviceManager *spice_usb_device_manager_get(SpiceSession *session,
 GPtrArray *spice_usb_device_manager_get_devices(SpiceUsbDeviceManager *manager);
 GPtrArray* spice_usb_device_manager_get_devices_with_filter(
     SpiceUsbDeviceManager *manager, const gchar *filter);
+GPtrArray* spice_usb_device_manager_get_devices_by_classes(
+    SpiceUsbDeviceManager *manager, GList *classes);
 
 gboolean spice_usb_device_manager_is_device_connected(SpiceUsbDeviceManager *manager,
                                                       SpiceUsbDevice *device);
-- 
1.9.3



More information about the Spice-devel mailing list