summaryrefslogtreecommitdiffstats
path: root/gtk
diff options
context:
space:
mode:
authorHans de Goede <hdegoede@redhat.com>2011-12-19 11:37:53 +0100
committerHans de Goede <hdegoede@redhat.com>2012-01-03 10:16:32 +0100
commit63069cec885491ad4ad8dd46d4aef91379425386 (patch)
tree109d3fc4371f7750db808826a2cb5bef9d651de1 /gtk
parentcebf1091c1df916846216919297af46ef36c0fa6 (diff)
downloadspice-gtk-63069cec885491ad4ad8dd46d4aef91379425386.tar.gz
spice-gtk-63069cec885491ad4ad8dd46d4aef91379425386.tar.xz
spice-gtk-63069cec885491ad4ad8dd46d4aef91379425386.zip
usbredir: Give devices a user friendly description
Before this patch devices were described like this to the user: USB device at 2-14 After this patch the description is: SanDisk Cruzer Blade [0781:5567] at 2-14 Signed-off-by: Hans de Goede <hdegoede@redhat.com>
Diffstat (limited to 'gtk')
-rw-r--r--gtk/usb-device-manager.c64
1 files changed, 62 insertions, 2 deletions
diff --git a/gtk/usb-device-manager.c b/gtk/usb-device-manager.c
index e8587bb..e55caae 100644
--- a/gtk/usb-device-manager.c
+++ b/gtk/usb-device-manager.c
@@ -22,10 +22,17 @@
#include "config.h"
#include <glib-object.h>
+#include <glib/gi18n.h>
#include "glib-compat.h"
#ifdef USE_USBREDIR
+#ifdef __linux__
+#include <stdio.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#endif
#include <libusb.h>
#include <gudev/gudev.h>
#include "channel-usbredir-priv.h"
@@ -422,6 +429,33 @@ const char *spice_usb_device_manager_libusb_strerror(enum libusb_error error_cod
}
return "Unknown error";
}
+
+#ifdef __linux__
+/* <Sigh> libusb does not allow getting the manufacturer and product strings
+ without opening the device, so grab them directly from sysfs */
+static gchar *spice_usb_device_manager_get_sysfs_attribute(
+ int bus, int address, const char *attribute)
+{
+ struct stat stat_buf;
+ char filename[256];
+ gchar *contents;
+
+ snprintf(filename, sizeof(filename), "/dev/bus/usb/%03d/%03d",
+ bus, address);
+ if (stat(filename, &stat_buf) != 0)
+ return NULL;
+
+ snprintf(filename, sizeof(filename), "/sys/dev/char/%d:%d/%s",
+ major(stat_buf.st_rdev), minor(stat_buf.st_rdev), attribute);
+ if (!g_file_get_contents(filename, &contents, NULL, NULL))
+ return NULL;
+
+ /* Remove the newline at the end */
+ contents[strlen(contents) - 1] = '\0';
+
+ return contents;
+}
+#endif
#endif
/* ------------------------------------------------------------------ */
@@ -848,14 +882,40 @@ gchar *spice_usb_device_get_description(SpiceUsbDevice *_device)
{
#ifdef USE_USBREDIR
libusb_device *device = (libusb_device *)_device;
- int bus, address;
+ struct libusb_device_descriptor desc;
+ int rc, bus, address;
+ gchar *description, *manufacturer = NULL, *product = NULL;
g_return_val_if_fail(device != NULL, NULL);
bus = libusb_get_bus_number(device);
address = libusb_get_device_address(device);
- return g_strdup_printf("USB device at %d-%d", bus, address);
+#if __linux__
+ manufacturer = spice_usb_device_manager_get_sysfs_attribute(bus, address,
+ "manufacturer");
+ product = spice_usb_device_manager_get_sysfs_attribute(bus, address,
+ "product");
+#endif
+ if (!manufacturer)
+ manufacturer = g_strdup(_("USB"));
+ if (!product)
+ product = g_strdup(_("Device"));
+
+ rc = libusb_get_device_descriptor(device, &desc);
+ if (rc == LIBUSB_SUCCESS) {
+ description = g_strdup_printf(_("%s %s [%04x:%04x] at %d-%d"),
+ manufacturer, product, desc.idVendor,
+ desc.idProduct, bus, address);
+ } else {
+ description = g_strdup_printf(_("%s %s at %d-%d"), manufacturer,
+ product, bus, address);
+ }
+
+ g_free(manufacturer);
+ g_free(product);
+
+ return description;
#else
return NULL;
#endif