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

Marc-André Lureau marcandre.lureau at gmail.com
Sun Feb 19 06:07:01 PST 2012


ack

On Sun, Feb 19, 2012 at 12:50 AM, Hans de Goede <hdegoede at redhat.com> wrote:
> Signed-off-by: Hans de Goede <hdegoede at redhat.com>
> ---
>  gtk/usb-device-manager.c |   32 ++++++++------------------------
>  gtk/usbutil.c            |   38 +++++++++++++++++++++++++++++++++++++-
>  gtk/usbutil.h            |    8 +++++---
>  3 files changed, 50 insertions(+), 28 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 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
> 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.5
>
> _______________________________________________
> Spice-devel mailing list
> Spice-devel at lists.freedesktop.org
> http://lists.freedesktop.org/mailman/listinfo/spice-devel



-- 
Marc-André Lureau


More information about the Spice-devel mailing list