[Spice-devel] [PATCH spice-gtk 2/3] usbutil: Add a spice_usb_util_get_device_strings helper function

Hans de Goede hdegoede at redhat.com
Thu Feb 9 05:30:01 PST 2012


Signed-off-by: Hans de Goede <hdegoede at redhat.com>
---
 gtk/usb-device-manager.c |   32 ++++++++------------------------
 gtk/usbutil.c            |   31 +++++++++++++++++++++++++++++++
 gtk/usbutil.h            |    8 +++++---
 3 files changed, 44 insertions(+), 27 deletions(-)

diff --git a/gtk/usb-device-manager.c b/gtk/usb-device-manager.c
index a20c18c..495a15b 100644
--- a/gtk/usb-device-manager.c
+++ b/gtk/usb-device-manager.c
@@ -907,32 +907,16 @@ gchar *spice_usb_device_get_description(SpiceUsbDevice *_device, const gchar *fo
     bus     = libusb_get_bus_number(device);
     address = libusb_get_device_address(device);
 
-#if __linux__
-    manufacturer = spice_usbutil_get_sysfs_attribute(bus, address, "manufacturer");
-    product = spice_usbutil_get_sysfs_attribute(bus, address, "product");
-#endif
-    if (!manufacturer)
-        manufacturer = g_strdup(_("USB"));
-    if (!product)
-        product = g_strdup(_("Device"));
-
-    /* Some devices have unwanted whitespace in their strings */
-    g_strstrip(manufacturer);
-    g_strstrip(product);
-
-    /* Some devices repeat the manufacturer at the beginning of product */
-    if (g_str_has_prefix(product, manufacturer) &&
-            strlen(product) > strlen(manufacturer)) {
-        gchar *tmp = g_strdup(product + strlen(manufacturer));
-        g_free(product);
-        product = tmp;
-        g_strstrip(product);
-    }
-
-    if (libusb_get_device_descriptor(device, &desc) == LIBUSB_SUCCESS)
+    if (libusb_get_device_descriptor(device, &desc) == LIBUSB_SUCCESS) {
+        spice_usb_util_get_device_strings(bus, address,
+                                          desc.idVendor, desc.idProduct,
+                                          &manufacturer, &product);
         descriptor = g_strdup_printf("[%04x:%04x]", desc.idVendor, desc.idProduct);
-    else
+    } else {
+        spice_usb_util_get_device_strings(bus, address, -1, -1,
+                                          &manufacturer, &product);
         descriptor = g_strdup("");
+    }
 
     if (!format)
         format = _("%s %s %s at %d-%d");
diff --git a/gtk/usbutil.c b/gtk/usbutil.c
index a089a58..025b85f 100644
--- a/gtk/usbutil.c
+++ b/gtk/usbutil.c
@@ -95,4 +95,35 @@ gchar *spice_usbutil_get_sysfs_attribute(int bus, int address, const char *attri
     return contents;
 }
 #endif
+
+void spice_usb_util_get_device_strings(int bus, int address,
+                                       int vendor_id, int product_id,
+                                       gchar **manufacturer, gchar **product)
+{
+    *manufacturer = NULL;
+    *product = NULL;
+
+#if __linux__
+    *manufacturer = spice_usbutil_get_sysfs_attribute(bus, address, "manufacturer");
+    *product = spice_usbutil_get_sysfs_attribute(bus, address, "product");
+#endif
+    if (!*manufacturer)
+        *manufacturer = g_strdup(_("USB"));
+    if (!*product)
+        *product = g_strdup(_("Device"));
+
+    /* Some devices have unwanted whitespace in their strings */
+    g_strstrip(*manufacturer);
+    g_strstrip(*product);
+
+    /* Some devices repeat the manufacturer at the beginning of product */
+    if (g_str_has_prefix(*product, *manufacturer) &&
+            strlen(*product) > strlen(*manufacturer)) {
+        gchar *tmp = g_strdup(*product + strlen(*manufacturer));
+        g_free(*product);
+        *product = tmp;
+        g_strstrip(*product);
+    }
+}
+
 #endif
diff --git a/gtk/usbutil.h b/gtk/usbutil.h
index a74f1aa..de5e92a 100644
--- a/gtk/usbutil.h
+++ b/gtk/usbutil.h
@@ -21,15 +21,17 @@
 #ifndef __SPICE_USBUTIL_H__
 #define __SPICE_USBUTIL_H__
 
+#include <glib.h>
+
 #ifdef USE_USBREDIR
 #include <libusb.h>
 
 G_BEGIN_DECLS
 
 const char *spice_usbutil_libusb_strerror(enum libusb_error error_code);
-#ifdef __linux__
-gchar *spice_usbutil_get_sysfs_attribute(int bus, int address, const char *attribute);
-#endif
+void spice_usb_util_get_device_strings(int bus, int address,
+                                       int vendor_id, int product_id,
+                                       gchar **manufacturer, gchar **product);
 
 G_END_DECLS
 
-- 
1.7.7.6



More information about the Spice-devel mailing list