[Spice-devel] [PATCH spice-gtk 1/7] usb: Move various helper functions into usbutil.[c, h]

Hans de Goede hdegoede at redhat.com
Sat Feb 18 15:50:18 PST 2012


Signed-off-by: Hans de Goede <hdegoede at redhat.com>
---
 gtk/Makefile.am               |    2 +
 gtk/channel-usbredir.c        |    5 +-
 gtk/usb-device-manager-priv.h |    6 +--
 gtk/usb-device-manager.c      |   81 +++-------------------------------
 gtk/usb-device-manager.h      |    2 +-
 gtk/usbutil.c                 |   99 +++++++++++++++++++++++++++++++++++++++++
 gtk/usbutil.h                 |   37 +++++++++++++++
 7 files changed, 149 insertions(+), 83 deletions(-)
 create mode 100644 gtk/usbutil.c
 create mode 100644 gtk/usbutil.h

diff --git a/gtk/Makefile.am b/gtk/Makefile.am
index 788a145..2ad4494 100644
--- a/gtk/Makefile.am
+++ b/gtk/Makefile.am
@@ -224,6 +224,8 @@ libspice_client_glib_2_0_la_SOURCES =	\
 	smartcard-manager-priv.h	\
 	usb-device-manager.c		\
 	usb-device-manager-priv.h	\
+	usbutil.c			\
+	usbutil.h			\
 	$(USB_ACL_HELPER_SRCS)		\
 	\
 	decode.h			\
diff --git a/gtk/channel-usbredir.c b/gtk/channel-usbredir.c
index 10d266c..ac23256 100644
--- a/gtk/channel-usbredir.c
+++ b/gtk/channel-usbredir.c
@@ -1,6 +1,6 @@
 /* -*- Mode: C; c-basic-offset: 4; indent-tabs-mode: nil -*- */
 /*
-   Copyright 2010-2011 Red Hat, Inc.
+   Copyright 2010-2012 Red Hat, Inc.
 
    Red Hat Authors:
    Hans de Goede <hdegoede at redhat.com>
@@ -28,6 +28,7 @@
 #endif
 #include "channel-usbredir-priv.h"
 #include "usb-device-manager-priv.h"
+#include "usbutil.h"
 #endif
 
 #include "spice-client.h"
@@ -186,7 +187,7 @@ static gboolean spice_usbredir_channel_open_device(
     if (rc != 0) {
         g_set_error(err, SPICE_CLIENT_ERROR, SPICE_CLIENT_ERROR_FAILED,
                     "Could not open usb device: %s [%i]",
-                    spice_usb_device_manager_libusb_strerror(rc), rc);
+                    spice_usbutil_libusb_strerror(rc), rc);
         return FALSE;
     }
 
diff --git a/gtk/usb-device-manager-priv.h b/gtk/usb-device-manager-priv.h
index 7e1aea3..b0b2f74 100644
--- a/gtk/usb-device-manager-priv.h
+++ b/gtk/usb-device-manager-priv.h
@@ -1,6 +1,6 @@
 /* -*- Mode: C; c-basic-offset: 4; indent-tabs-mode: nil -*- */
 /*
-   Copyright (C) 2011 Red Hat, Inc.
+   Copyright (C) 2011,2012 Red Hat, Inc.
 
    Red Hat Authors:
    Hans de Goede <hdegoede at redhat.com>
@@ -25,10 +25,6 @@
 
 G_BEGIN_DECLS
 
-#ifdef USE_USBREDIR
-const char *spice_usb_device_manager_libusb_strerror(enum libusb_error error_code);
-#endif
-
 gboolean spice_usb_device_manager_start_event_listening(
     SpiceUsbDeviceManager *manager, GError **err);
 
diff --git a/gtk/usb-device-manager.c b/gtk/usb-device-manager.c
index c5062b7..a20c18c 100644
--- a/gtk/usb-device-manager.c
+++ b/gtk/usb-device-manager.c
@@ -1,6 +1,6 @@
 /* -*- Mode: C; c-basic-offset: 4; indent-tabs-mode: nil -*- */
 /*
-   Copyright (C) 2011 Red Hat, Inc.
+   Copyright (C) 2011, 2012 Red Hat, Inc.
 
    Red Hat Authors:
    Hans de Goede <hdegoede at redhat.com>
@@ -27,17 +27,12 @@
 #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 <errno.h>
 #include <libusb.h>
 #include <gudev/gudev.h>
 #include "channel-usbredir-priv.h"
 #include "usbredirhost.h"
+#include "usbutil.h"
 #endif
 
 #include "spice-session-priv.h"
@@ -191,7 +186,7 @@ static gboolean spice_usb_device_manager_initable_init(GInitable  *initable,
 #ifdef USE_USBREDIR
     rc = libusb_init(&priv->context);
     if (rc < 0) {
-        const char *desc = spice_usb_device_manager_libusb_strerror(rc);
+        const char *desc = spice_usbutil_libusb_strerror(rc);
         g_warning("Error initializing USB support: %s [%i]", desc, rc);
         g_set_error(err, SPICE_CLIENT_ERROR, SPICE_CLIENT_ERROR_FAILED,
                     "Error initializing USB support: %s [%i]", desc, rc);
@@ -461,68 +456,6 @@ static gboolean spice_usb_device_manager_get_udev_bus_n_address(
 
     return *bus && *address;
 }
-
-const char *spice_usb_device_manager_libusb_strerror(enum libusb_error error_code)
-{
-    switch (error_code) {
-    case LIBUSB_SUCCESS:
-        return "Success";
-    case LIBUSB_ERROR_IO:
-        return "Input/output error";
-    case LIBUSB_ERROR_INVALID_PARAM:
-        return "Invalid parameter";
-    case LIBUSB_ERROR_ACCESS:
-        return "Access denied (insufficient permissions)";
-    case LIBUSB_ERROR_NO_DEVICE:
-        return "No such device (it may have been disconnected)";
-    case LIBUSB_ERROR_NOT_FOUND:
-        return "Entity not found";
-    case LIBUSB_ERROR_BUSY:
-        return "Resource busy";
-    case LIBUSB_ERROR_TIMEOUT:
-        return "Operation timed out";
-    case LIBUSB_ERROR_OVERFLOW:
-        return "Overflow";
-    case LIBUSB_ERROR_PIPE:
-        return "Pipe error";
-    case LIBUSB_ERROR_INTERRUPTED:
-        return "System call interrupted (perhaps due to signal)";
-    case LIBUSB_ERROR_NO_MEM:
-        return "Insufficient memory";
-    case LIBUSB_ERROR_NOT_SUPPORTED:
-        return "Operation not supported or unimplemented on this platform";
-    case LIBUSB_ERROR_OTHER:
-        return "Other error";
-    }
-    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
 
 /* ------------------------------------------------------------------ */
@@ -699,7 +632,7 @@ static gpointer spice_usb_device_manager_usb_ev_thread(gpointer user_data)
     while (priv->event_thread_run) {
         rc = libusb_handle_events(priv->context);
         if (rc) {
-            const char *desc = spice_usb_device_manager_libusb_strerror(rc);
+            const char *desc = spice_usbutil_libusb_strerror(rc);
             g_warning("Error handling USB events: %s [%i]", desc, rc);
         }
     }
@@ -975,10 +908,8 @@ gchar *spice_usb_device_get_description(SpiceUsbDevice *_device, const gchar *fo
     address = libusb_get_device_address(device);
 
 #if __linux__
-    manufacturer = spice_usb_device_manager_get_sysfs_attribute(bus, address,
-                                                              "manufacturer");
-    product = spice_usb_device_manager_get_sysfs_attribute(bus, address,
-                                                           "product");
+    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"));
diff --git a/gtk/usb-device-manager.h b/gtk/usb-device-manager.h
index d01bc12..ec1a896 100644
--- a/gtk/usb-device-manager.h
+++ b/gtk/usb-device-manager.h
@@ -1,6 +1,6 @@
 /* -*- Mode: C; c-basic-offset: 4; indent-tabs-mode: nil -*- */
 /*
-   Copyright (C) 2011 Red Hat, Inc.
+   Copyright (C) 2011, 2012 Red Hat, Inc.
 
    Red Hat Authors:
    Hans de Goede <hdegoede at redhat.com>
diff --git a/gtk/usbutil.c b/gtk/usbutil.c
new file mode 100644
index 0000000..4f9502d
--- /dev/null
+++ b/gtk/usbutil.c
@@ -0,0 +1,99 @@
+/* -*- Mode: C; c-basic-offset: 4; indent-tabs-mode: nil -*- */
+/*
+   Copyright (C) 2012 Red Hat, Inc.
+
+   Red Hat Authors:
+   Hans de Goede <hdegoede at redhat.com>
+
+   This library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   This library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with this library; if not, see <http://www.gnu.org/licenses/>.
+*/
+
+#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 "usbutil.h"
+
+G_GNUC_INTERNAL
+const char *spice_usbutil_libusb_strerror(enum libusb_error error_code)
+{
+    switch (error_code) {
+    case LIBUSB_SUCCESS:
+        return "Success";
+    case LIBUSB_ERROR_IO:
+        return "Input/output error";
+    case LIBUSB_ERROR_INVALID_PARAM:
+        return "Invalid parameter";
+    case LIBUSB_ERROR_ACCESS:
+        return "Access denied (insufficient permissions)";
+    case LIBUSB_ERROR_NO_DEVICE:
+        return "No such device (it may have been disconnected)";
+    case LIBUSB_ERROR_NOT_FOUND:
+        return "Entity not found";
+    case LIBUSB_ERROR_BUSY:
+        return "Resource busy";
+    case LIBUSB_ERROR_TIMEOUT:
+        return "Operation timed out";
+    case LIBUSB_ERROR_OVERFLOW:
+        return "Overflow";
+    case LIBUSB_ERROR_PIPE:
+        return "Pipe error";
+    case LIBUSB_ERROR_INTERRUPTED:
+        return "System call interrupted (perhaps due to signal)";
+    case LIBUSB_ERROR_NO_MEM:
+        return "Insufficient memory";
+    case LIBUSB_ERROR_NOT_SUPPORTED:
+        return "Operation not supported or unimplemented on this platform";
+    case LIBUSB_ERROR_OTHER:
+        return "Other error";
+    }
+    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 */
+gchar *spice_usbutil_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
diff --git a/gtk/usbutil.h b/gtk/usbutil.h
new file mode 100644
index 0000000..a74f1aa
--- /dev/null
+++ b/gtk/usbutil.h
@@ -0,0 +1,37 @@
+/* -*- Mode: C; c-basic-offset: 4; indent-tabs-mode: nil -*- */
+/*
+   Copyright (C) 2012 Red Hat, Inc.
+
+   Red Hat Authors:
+   Hans de Goede <hdegoede at redhat.com>
+
+   This library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   This library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with this library; if not, see <http://www.gnu.org/licenses/>.
+*/
+#ifndef __SPICE_USBUTIL_H__
+#define __SPICE_USBUTIL_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
+
+G_END_DECLS
+
+#endif /* USE_USBREDIR */
+#endif /* __SPICE_USBUTIL_H__ */
-- 
1.7.7.5



More information about the Spice-devel mailing list