diff options
author | Hans de Goede <hdegoede@redhat.com> | 2012-02-09 12:49:57 +0100 |
---|---|---|
committer | Hans de Goede <hdegoede@redhat.com> | 2012-02-20 16:14:49 +0100 |
commit | 97648e4d0769f01800af21e43aeffee50aca503d (patch) | |
tree | d7d60aec1056751054c7ae0471d96a17cc56f414 /gtk/usbutil.c | |
parent | 05a1bf60e9805300c1ec54a4c3ea9aebf5d2f488 (diff) |
usbutil: Add a spice_usb_util_get_device_strings helper function
Signed-off-by: Hans de Goede <hdegoede@redhat.com>
Diffstat (limited to 'gtk/usbutil.c')
-rw-r--r-- | gtk/usbutil.c | 38 |
1 files changed, 37 insertions, 1 deletions
diff --git a/gtk/usbutil.c b/gtk/usbutil.c index 4f9502d..0d1361e 100644 --- a/gtk/usbutil.c +++ b/gtk/usbutil.c @@ -74,7 +74,8 @@ const char *spice_usbutil_libusb_strerror(enum libusb_error error_code) #ifdef __linux__ /* <Sigh> libusb does not allow getting the manufacturer and product strings without opening the device, so grab them directly from sysfs */ -gchar *spice_usbutil_get_sysfs_attribute(int bus, int address, const char *attribute) +static gchar *spice_usbutil_get_sysfs_attribute(int bus, int address, + const char *attribute) { struct stat stat_buf; char filename[256]; @@ -96,4 +97,39 @@ gchar *spice_usbutil_get_sysfs_attribute(int bus, int address, const char *attri return contents; } #endif + +G_GNUC_INTERNAL +void spice_usb_util_get_device_strings(int bus, int address, + int vendor_id, int product_id, + gchar **manufacturer, gchar **product) +{ + g_return_if_fail(manufacturer != NULL); + g_return_if_fail(product != NULL); + + *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 |