[Spice-devel] [PATCH spice-gtk 3/5] usb-device-manager: Add spice_usb_device_manager_get_devices_with_filter (fdo#63807)

Hans de Goede hdegoede at redhat.com
Tue Apr 23 05:19:50 PDT 2013


Signed-off-by: Hans de Goede <hdegoede at redhat.com>
---
 doc/reference/spice-gtk-sections.txt |  1 +
 gtk/map-file                         |  1 +
 gtk/usb-device-manager.c             | 46 +++++++++++++++++++++++++++++++++---
 gtk/usb-device-manager.h             |  2 ++
 4 files changed, 47 insertions(+), 3 deletions(-)

diff --git a/doc/reference/spice-gtk-sections.txt b/doc/reference/spice-gtk-sections.txt
index d82a886..b15e4bb 100644
--- a/doc/reference/spice-gtk-sections.txt
+++ b/doc/reference/spice-gtk-sections.txt
@@ -291,6 +291,7 @@ SpiceUsbDeviceManagerClass
 <SUBSECTION>
 spice_usb_device_manager_get
 spice_usb_device_manager_get_devices
+spice_usb_device_manager_get_devices_with_filter
 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 386afb1..558a4d7 100644
--- a/gtk/map-file
+++ b/gtk/map-file
@@ -104,6 +104,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_with_filter;
 spice_usb_device_manager_get_type;
 spice_usb_device_manager_is_device_connected;
 spice_usb_device_widget_get_type;
diff --git a/gtk/usb-device-manager.c b/gtk/usb-device-manager.c
index dc334bd..75406a4 100644
--- a/gtk/usb-device-manager.c
+++ b/gtk/usb-device-manager.c
@@ -500,7 +500,7 @@ static void spice_usb_device_manager_class_init(SpiceUsbDeviceManagerClass *klas
      * Set a string specifying a filter selecting USB devices to automatically
      * redirect after a Spice connection has been established.
      *
-     * See SpiceUsbDeviceManager:auto-connect-filter: for the filter string
+     * See #SpiceUsbDeviceManager:auto-connect-filter for the filter string
      * format.
      */
     pspec = g_param_spec_string("redirect-on-connect", "Redirect on connect",
@@ -1181,12 +1181,15 @@ SpiceUsbDeviceManager *spice_usb_device_manager_get(SpiceSession *session,
 }
 
 /**
- * spice_usb_device_manager_get_devices:
+ * spice_usb_device_manager_get_devices_with_filter:
  * @manager: the #SpiceUsbDeviceManager manager
+ * @filter: filter string for selecting which devices to return, see
+ *      #SpiceUsbDeviceManager:auto-connect-filter for the filter string format
  *
  * Returns: (element-type SpiceUsbDevice) (transfer full): a %GPtrArray array of %SpiceUsbDevice
  */
-GPtrArray* spice_usb_device_manager_get_devices(SpiceUsbDeviceManager *self)
+GPtrArray* spice_usb_device_manager_get_devices_with_filter(
+    SpiceUsbDeviceManager *self, const gchar *filter)
 {
     GPtrArray *devices_copy = NULL;
 
@@ -1194,20 +1197,57 @@ GPtrArray* spice_usb_device_manager_get_devices(SpiceUsbDeviceManager *self)
 
 #ifdef USE_USBREDIR
     SpiceUsbDeviceManagerPrivate *priv = self->priv;
+    struct usbredirfilter_rule *rules = NULL;;
+    int r, count = 0;
     guint i;
 
+    if (filter) {
+        r = usbredirfilter_string_to_rules(filter, ",", "|", &rules, &count);
+        if (r) {
+            if (r == -ENOMEM)
+                g_error("Failed to allocate memory for filter");
+            g_warning("Error parsing filter, ignoring");
+            rules = NULL;
+            count = 0;
+        }
+    }
+
     devices_copy = g_ptr_array_new_with_free_func((GDestroyNotify)
                                                   spice_usb_device_unref);
     for (i = 0; i < priv->devices->len; i++) {
         SpiceUsbDevice *device = g_ptr_array_index(priv->devices, i);
+
+        if (rules) {
+            libusb_device *libdev =
+                spice_usb_device_manager_device_to_libdev(self, device);
+#ifdef G_OS_WIN32
+            if (libdev == NULL)
+                continue;
+#endif
+            if (usbredirhost_check_device_filter(rules, count, libdev, 0) != 0)
+                continue;
+        }
         g_ptr_array_add(devices_copy, spice_usb_device_ref(device));
     }
+
+    free(rules);
 #endif
 
     return devices_copy;
 }
 
 /**
+ * spice_usb_device_manager_get_devices:
+ * @manager: the #SpiceUsbDeviceManager manager
+ *
+ * Returns: (element-type SpiceUsbDevice) (transfer full): a %GPtrArray array of %SpiceUsbDevice
+ */
+GPtrArray* spice_usb_device_manager_get_devices(SpiceUsbDeviceManager *self)
+{
+    return spice_usb_device_manager_get_devices_with_filter(self, NULL);
+}
+
+/**
  * 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 df138ee..a7e3515 100644
--- a/gtk/usb-device-manager.h
+++ b/gtk/usb-device-manager.h
@@ -94,6 +94,8 @@ SpiceUsbDeviceManager *spice_usb_device_manager_get(SpiceSession *session,
                                                     GError **err);
 
 GPtrArray *spice_usb_device_manager_get_devices(SpiceUsbDeviceManager *manager);
+GPtrArray* spice_usb_device_manager_get_devices_with_filter(
+    SpiceUsbDeviceManager *manager, const gchar *filter);
 
 gboolean spice_usb_device_manager_is_device_connected(SpiceUsbDeviceManager *manager,
                                                       SpiceUsbDevice *device);
-- 
1.8.2



More information about the Spice-devel mailing list