[Spice-devel] [spice-gtk v2 11/13] win-usb-dev: report libusb_device via signal

Yuri Benditovich yuri.benditovich at daynix.com
Tue Mar 19 05:56:06 UTC 2019


Change the signal to report libusb_device instead of GUdevDevice
object. This simplifies the callback procedure in usb-dev-manager
and avoids additional reenumeration immediately after reenumeration
that already done by win-usb-dev.

Signed-off-by: Yuri Benditovich <yuri.benditovich at daynix.com>
---
 src/usb-device-manager.c | 81 +++-------------------------------------
 src/win-usb-dev.c        | 55 +--------------------------
 src/win-usb-dev.h        |  2 +-
 3 files changed, 9 insertions(+), 129 deletions(-)

diff --git a/src/usb-device-manager.c b/src/usb-device-manager.c
index c99d359..a163501 100644
--- a/src/usb-device-manager.c
+++ b/src/usb-device-manager.c
@@ -153,11 +153,9 @@ static void channel_event(SpiceChannel *channel, SpiceChannelEvent event,
                           gpointer user_data);
 #ifdef G_OS_WIN32
 static void spice_usb_device_manager_uevent_cb(GUdevClient     *client,
-                                               GUdevDevice     *udevice,
+                                               libusb_device   *udevice,
                                                gboolean         add,
                                                gpointer         user_data);
-static void spice_usb_device_manager_add_udev(SpiceUsbDeviceManager  *self,
-                                              GUdevDevice            *udev);
 #else
 static int spice_usb_device_manager_hotplug_cb(libusb_context       *ctx,
                                                libusb_device        *device,
@@ -720,28 +718,6 @@ static void spice_usb_device_manager_class_init(SpiceUsbDeviceManagerClass *klas
 /* ------------------------------------------------------------------ */
 /* gudev / libusb Helper functions                                    */
 
-#ifdef G_OS_WIN32
-static gboolean spice_usb_device_manager_get_udev_bus_n_address(
-    SpiceUsbDeviceManager *manager, GUdevDevice *udev,
-    int *bus, int *address)
-{
-    const gchar *bus_str, *address_str;
-
-    *bus = *address = 0;
-
-   /* Linux or UsbDk backend on Windows*/
-    bus_str = g_udev_device_get_property(udev, "BUSNUM");
-    address_str = g_udev_device_get_property(udev, "DEVNUM");
-
-    if (bus_str)
-        *bus = atoi(bus_str);
-    if (address_str)
-        *address = atoi(address_str);
-
-    return *bus && *address;
-}
-#endif
-
 static gboolean spice_usb_device_manager_get_device_descriptor(
     libusb_device *libdev,
     struct libusb_device_descriptor *desc)
@@ -1022,64 +998,19 @@ static void spice_usb_device_manager_remove_dev(SpiceUsbDeviceManager *self,
 }
 
 #ifdef G_OS_WIN32
-static void spice_usb_device_manager_add_udev(SpiceUsbDeviceManager  *self,
-                                              GUdevDevice            *udev)
-{
-    SpiceUsbDeviceManagerPrivate *priv = self->priv;
-    libusb_device *libdev = NULL, **dev_list = NULL;
-    const gchar *devtype;
-    int i, bus, address;
-
-    devtype = g_udev_device_get_property(udev, "DEVTYPE");
-    /* Check if this is a usb device (and not an interface) */
-    if (!devtype || strcmp(devtype, "usb_device"))
-        return;
-
-    if (!spice_usb_device_manager_get_udev_bus_n_address(self, udev, &bus, &address)) {
-        g_warning("USB device without bus number or device address");
-        return;
-    }
-
-    libusb_get_device_list(priv->context, &dev_list);
-
-    for (i = 0; dev_list && dev_list[i]; i++) {
-        if (spice_usb_device_manager_libdev_match(self, dev_list[i], bus, address)) {
-            libdev = dev_list[i];
-            break;
-        }
-    }
-
-    if (libdev)
-        spice_usb_device_manager_add_dev(self, libdev);
-    else
-        g_warning("Could not find USB device to add " DEV_ID_FMT,
-                  (guint) bus, (guint) address);
-
-    libusb_free_device_list(dev_list, 1);
-}
-
-static void spice_usb_device_manager_remove_udev(SpiceUsbDeviceManager  *self,
-                                                 GUdevDevice            *udev)
-{
-    int bus, address;
-
-    if (!spice_usb_device_manager_get_udev_bus_n_address(self, udev, &bus, &address))
-        return;
-
-    spice_usb_device_manager_remove_dev(self, bus, address);
-}
-
 static void spice_usb_device_manager_uevent_cb(GUdevClient     *client,
-                                               GUdevDevice     *udevice,
+                                               libusb_device   *dev,
                                                gboolean         add,
                                                gpointer         user_data)
 {
     SpiceUsbDeviceManager *self = SPICE_USB_DEVICE_MANAGER(user_data);
 
     if (add)
-        spice_usb_device_manager_add_udev(self, udevice);
+        spice_usb_device_manager_add_dev(self, dev);
     else
-        spice_usb_device_manager_remove_udev(self, udevice);
+        spice_usb_device_manager_remove_dev(self,
+                                            libusb_get_bus_number(dev),
+                                            libusb_get_device_address(dev));
 }
 #else
 struct hotplug_idle_cb_args {
diff --git a/src/win-usb-dev.c b/src/win-usb-dev.c
index 0b87e75..09bd031 100644
--- a/src/win-usb-dev.c
+++ b/src/win-usb-dev.c
@@ -83,9 +83,7 @@ enum
 static guint signals[LAST_SIGNAL] = { 0, };
 static GUdevClient *singleton = NULL;
 
-static GUdevDevice *g_udev_device_new(GUdevDeviceInfo *udevinfo);
 static LRESULT CALLBACK wnd_proc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam);
-static gboolean get_usb_dev_info(libusb_device *dev, GUdevDeviceInfo *udevinfo);
 
 //uncomment to debug gudev device lists.
 //#define DEBUG_GUDEV_DEVICE_LISTS
@@ -247,22 +245,9 @@ static void g_udev_client_initable_iface_init(GInitableIface *iface)
     iface->init = g_udev_client_initable_init;
 }
 
-static void g_udev_notify_device(GUdevClient *self, libusb_device *dev, gboolean add)
-{
-    GUdevDeviceInfo *udevinfo;
-    GUdevDevice *udevice;
-    udevinfo = g_new0(GUdevDeviceInfo, 1);
-    if (get_usb_dev_info(dev, udevinfo)) {
-        udevice = g_udev_device_new(udevinfo);
-        g_signal_emit(self, signals[UEVENT_SIGNAL], 0, udevice, add);
-    } else {
-        g_free(udevinfo);
-    }
-}
-
 static void report_one_device(gpointer data, gpointer self)
 {
-    g_udev_notify_device(self, data, TRUE);
+    g_signal_emit(self, signals[UEVENT_SIGNAL], 0, data, TRUE);
 }
 
 void g_udev_client_report_devices(GUdevClient *self)
@@ -376,31 +361,6 @@ static void g_udev_client_class_init(GUdevClientClass *klass)
     g_object_class_install_property(gobject_class, PROP_REDIRECTING, pspec);
 }
 
-static gboolean get_usb_dev_info(libusb_device *dev, GUdevDeviceInfo *udevinfo)
-{
-    struct libusb_device_descriptor desc;
-
-    g_return_val_if_fail(dev, FALSE);
-    g_return_val_if_fail(udevinfo, FALSE);
-
-    if (libusb_get_device_descriptor(dev, &desc) < 0) {
-        g_warning("cannot get device descriptor %p", dev);
-        return FALSE;
-    }
-
-    udevinfo->bus   = libusb_get_bus_number(dev);
-    udevinfo->addr  = libusb_get_device_address(dev);
-    udevinfo->class = desc.bDeviceClass;
-    udevinfo->vid   = desc.idVendor;
-    udevinfo->pid   = desc.idProduct;
-    g_snprintf(udevinfo->sclass, sizeof(udevinfo->sclass), "%d", udevinfo->class);
-    g_snprintf(udevinfo->sbus,   sizeof(udevinfo->sbus),   "%d", udevinfo->bus);
-    g_snprintf(udevinfo->saddr,  sizeof(udevinfo->saddr),  "%d", udevinfo->addr);
-    g_snprintf(udevinfo->svid,   sizeof(udevinfo->svid),   "%d", udevinfo->vid);
-    g_snprintf(udevinfo->spid,   sizeof(udevinfo->spid),   "%d", udevinfo->pid);
-    return TRUE;
-}
-
 /* comparing bus:addr and vid:pid */
 static gint compare_libusb_devices(gconstpointer a, gconstpointer b)
 {
@@ -431,7 +391,7 @@ static void notify_dev_state_change(GUdevClient *self,
         GList *found = g_list_find_custom(new_list, dev->data, compare_libusb_devices);
         if (found == NULL) {
             g_udev_device_print(dev->data, add ? "add" : "remove");
-            g_udev_notify_device(self, dev->data, add);
+            g_signal_emit(self, signals[UEVENT_SIGNAL], 0, dev->data, add);
         } else if (add) {
             /* keep old existing libusb_device in the new list, when
                usb-dev-manager will maintain its own list of libusb_device,
@@ -510,17 +470,6 @@ static void g_udev_device_init(GUdevDevice *device)
     device->priv = g_udev_device_get_instance_private(device);
 }
 
-static GUdevDevice *g_udev_device_new(GUdevDeviceInfo *udevinfo)
-{
-    GUdevDevice *device;
-
-    g_return_val_if_fail(udevinfo != NULL, NULL);
-
-    device =  G_UDEV_DEVICE(g_object_new(G_UDEV_TYPE_DEVICE, NULL));
-    device->priv->udevinfo = udevinfo;
-    return device;
-}
-
 const gchar *g_udev_device_get_property(GUdevDevice *udev, const gchar *property)
 {
     GUdevDeviceInfo* udevinfo;
diff --git a/src/win-usb-dev.h b/src/win-usb-dev.h
index 64ebd67..b960dfc 100644
--- a/src/win-usb-dev.h
+++ b/src/win-usb-dev.h
@@ -75,7 +75,7 @@ struct _GUdevClientClass
     GObjectClass parent_class;
 
     /* signals */
-    void (*uevent)(GUdevClient *client, GUdevDevice  *device, gboolean add);
+    void (*uevent)(GUdevClient *client, libusb_device *device, gboolean add);
 };
 
 GType g_udev_client_get_type(void) G_GNUC_CONST;
-- 
2.17.1



More information about the Spice-devel mailing list