[Spice-devel] [spice-gtk 03/13] usb-redir: reuse libusb context under Windows

Yuri Benditovich yuri.benditovich at daynix.com
Sun Mar 10 14:46:02 UTC 2019


Do not create own libusb context in usb-device-manager.
Reuse existing context created by win-usb-dev instead.

Signed-off-by: Yuri Benditovich <yuri.benditovich at daynix.com>
---
 src/usb-device-manager.c | 11 +++++------
 src/win-usb-dev.c        |  4 ++++
 src/win-usb-dev.h        |  1 +
 3 files changed, 10 insertions(+), 6 deletions(-)

diff --git a/src/usb-device-manager.c b/src/usb-device-manager.c
index 6435be8..debba4d 100644
--- a/src/usb-device-manager.c
+++ b/src/usb-device-manager.c
@@ -282,8 +282,9 @@ static gboolean spice_usb_device_manager_initable_init(GInitable  *initable,
     SpiceUsbDeviceManagerPrivate *priv = self->priv;
     GList *list;
     GList *it;
-    int rc;
 
+#ifndef G_OS_WIN32
+    int rc;
     /* Initialize libusb */
     rc = libusb_init(&priv->context);
     if (rc < 0) {
@@ -293,11 +294,6 @@ static gboolean spice_usb_device_manager_initable_init(GInitable  *initable,
                     "Error initializing USB support: %s [%i]", desc, rc);
         return FALSE;
     }
-
-#ifdef G_OS_WIN32
-#if LIBUSB_API_VERSION >= 0x01000106
-    libusb_set_option(priv->context, LIBUSB_OPTION_USE_USBDK);
-#endif
 #endif
 
     /* Start listening for usb devices plug / unplug */
@@ -307,6 +303,7 @@ static gboolean spice_usb_device_manager_initable_init(GInitable  *initable,
         g_warning("Error initializing GUdevClient");
         return FALSE;
     }
+    priv->context = g_udev_client_get_context(priv->udev);
     g_signal_connect(G_OBJECT(priv->udev), "uevent",
                      G_CALLBACK(spice_usb_device_manager_uevent_cb), self);
     /* Do coldplug (detection of already connected devices) */
@@ -402,8 +399,10 @@ static void spice_usb_device_manager_finalize(GObject *gobject)
     g_clear_object(&priv->udev);
 #endif
     g_return_if_fail(priv->event_thread == NULL);
+#ifndef G_OS_WIN32
     if (priv->context)
         libusb_exit(priv->context);
+#endif
     free(priv->auto_conn_filter_rules);
     free(priv->redirect_on_connect_rules);
 #ifdef G_OS_WIN32
diff --git a/src/win-usb-dev.c b/src/win-usb-dev.c
index d0eae06..a8d922f 100644
--- a/src/win-usb-dev.c
+++ b/src/win-usb-dev.c
@@ -113,6 +113,10 @@ GUdevClient *g_udev_client_new(void)
     return singleton;
 }
 
+libusb_context *g_udev_client_get_context(GUdevClient *client)
+{
+    return client->priv->ctx;
+}
 
 /*
  * devs [in,out] an empty devs list in, full devs list out
diff --git a/src/win-usb-dev.h b/src/win-usb-dev.h
index 0f34a01..f3c7466 100644
--- a/src/win-usb-dev.h
+++ b/src/win-usb-dev.h
@@ -80,6 +80,7 @@ struct _GUdevClientClass
 
 GType g_udev_client_get_type(void) G_GNUC_CONST;
 GUdevClient *g_udev_client_new(void);
+libusb_context *g_udev_client_get_context(GUdevClient *client);
 GList *g_udev_client_query_by_subsystem(GUdevClient *client, const gchar *subsystem);
 
 GType g_udev_device_get_type(void) G_GNUC_CONST;
-- 
2.17.1



More information about the Spice-devel mailing list