[Spice-devel] [PATCH spice-gtk 08/44] fixup! usb-redir: define interfaces to support emulated devices

Frediano Ziglio fziglio at redhat.com
Tue Jul 30 12:02:57 UTC 2019


Unify spice_usb_device_get_description and spice_usb_backend_device_description
They were doing the same thing.
This make spice_usb_backend_device_description more consistent
supporting any device, not only emulated
---
 src/usb-backend.c        | 42 +++++++++++++++++++++++++++++++---------
 src/usb-backend.h        |  2 +-
 src/usb-device-manager.c | 29 +--------------------------
 3 files changed, 35 insertions(+), 38 deletions(-)

diff --git a/src/usb-backend.c b/src/usb-backend.c
index 0a830dfb..67fd63c3 100644
--- a/src/usb-backend.c
+++ b/src/usb-backend.c
@@ -42,6 +42,7 @@
 #include "usb-emulation.h"
 #include "channel-usbredir-priv.h"
 #include "spice-channel-priv.h"
+#include "usbutil.h"
 
 #define LOUD_DEBUG(x, ...)
 
@@ -845,19 +846,42 @@ spice_usb_backend_channel_get_guest_filter(SpiceUsbBackendChannel *ch,
     }
 }
 
-gchar * spice_usb_backend_device_description(SpiceUsbBackendDevice *dev,
-                                             const gchar *format)
+gchar * spice_usb_backend_device_get_description(SpiceUsbBackendDevice *dev,
+                                                 const gchar *format)
 {
-    if (!dev->edev) {
-        return g_strdup("Not available for libusb devices");
+    guint16 bus, address, vid, pid;
+    gchar *description, *descriptor, *manufacturer = NULL, *product = NULL;
+
+    g_return_val_if_fail(dev != NULL, NULL);
+
+    bus     = dev->device_info.bus;
+    address = dev->device_info.address;
+    vid     = dev->device_info.vid;
+    pid     = dev->device_info.pid;
+
+    if ((vid > 0) && (pid > 0)) {
+        descriptor = g_strdup_printf("[%04x:%04x]", vid, pid);
+    } else {
+        descriptor = g_strdup("");
     }
-    gchar *description, *descriptor, *product;
-    descriptor = g_strdup_printf("[%04x:%04x]", dev->device_info.vid, dev->device_info.pid);
-    product = device_ops(dev->edev)->get_product_description(dev->edev);
-    description = g_strdup_printf(format, "", product, descriptor,
-                                  dev->device_info.bus, dev->device_info.address);
+
+    if (dev->libusb_device) {
+        spice_usb_util_get_device_strings(bus, address, vid, pid,
+                                          &manufacturer, &product);
+    } else {
+        product = device_ops(dev->edev)->get_product_description(dev->edev);
+    }
+
+    if (!format)
+        format = _("%s %s %s at %d-%d");
+
+    description = g_strdup_printf(format, manufacturer ? manufacturer : "",
+                                  product, descriptor, bus, address);
+
+    g_free(manufacturer);
     g_free(descriptor);
     g_free(product);
+
     return description;
 }
 
diff --git a/src/usb-backend.h b/src/usb-backend.h
index e898d68a..df24fa56 100644
--- a/src/usb-backend.h
+++ b/src/usb-backend.h
@@ -97,6 +97,7 @@ void spice_usb_backend_channel_get_guest_filter(SpiceUsbBackendChannel *ch,
                                                 const struct usbredirfilter_rule  **rules,
                                                 int *count);
 void spice_usb_backend_return_write_data(SpiceUsbBackendChannel *ch, void *data);
+gchar *spice_usb_backend_device_get_description(SpiceUsbBackendDevice *dev, const gchar *format);
 
 typedef struct UsbCreateDeviceParameters
 {
@@ -116,7 +117,6 @@ typedef struct UsbCreateDeviceParameters
 gboolean spice_usb_backend_create_device(SpiceUsbBackend *be,
                                          UsbEmulatedDeviceType dev_type,
                                          UsbCreateDeviceParameters *param);
-gchar *spice_usb_backend_device_description(SpiceUsbBackendDevice *dev, const gchar *format);
 void spice_usb_backend_device_eject(SpiceUsbBackend *be, SpiceUsbBackendDevice *device);
 void spice_usb_backend_device_report_change(SpiceUsbBackend *be, SpiceUsbBackendDevice *device);
 
diff --git a/src/usb-device-manager.c b/src/usb-device-manager.c
index 544e5687..743db9b7 100644
--- a/src/usb-device-manager.c
+++ b/src/usb-device-manager.c
@@ -32,7 +32,6 @@
 #endif
 
 #include "channel-usbredir-priv.h"
-#include "usbutil.h"
 #endif
 
 #include "spice-session-priv.h"
@@ -1435,35 +1434,9 @@ spice_usb_device_manager_can_redirect_device(SpiceUsbDeviceManager  *self,
 gchar *spice_usb_device_get_description(SpiceUsbDevice *device, const gchar *format)
 {
 #ifdef USE_USBREDIR
-    guint16 bus, address, vid, pid;
-    gchar *description, *descriptor, *manufacturer = NULL, *product = NULL;
-
     g_return_val_if_fail(device != NULL, NULL);
 
-    bus     = spice_usb_device_get_busnum(device);
-    address = spice_usb_device_get_devaddr(device);
-    vid     = spice_usb_device_get_vid(device);
-    pid     = spice_usb_device_get_pid(device);
-
-    if ((vid > 0) && (pid > 0)) {
-        descriptor = g_strdup_printf("[%04x:%04x]", vid, pid);
-    } else {
-        descriptor = g_strdup("");
-    }
-
-    spice_usb_util_get_device_strings(bus, address, vid, pid,
-                                      &manufacturer, &product);
-
-    if (!format)
-        format = _("%s %s %s at %d-%d");
-
-    description = g_strdup_printf(format, manufacturer, product, descriptor, bus, address);
-
-    g_free(manufacturer);
-    g_free(descriptor);
-    g_free(product);
-
-    return description;
+    return spice_usb_backend_device_get_description(device->bdev, format);
 #else
     return NULL;
 #endif
-- 
2.20.1



More information about the Spice-devel mailing list