From: Kirill Moizik <kmoizik@xxxxxxxxxx> This will be useful when adding usbdk support as usbdk may not be available, so we'll need to switch between the usbdk and usbclerk code paths at runtime (only at init-time). For now, this is used to replace the #ifdef G_OS_WIN32/G_OS_LINUX blocks with a runtime test (on linux, use_usbclerk will be FALSE, and it will be TRUE on Windows). This commit is much more readable with git show -w Signed-off-by: Kirill Moizik <kirillm@xxxxxxxxxx> --- src/usb-device-manager.c | 189 ++++++++++++++++++++++++++--------------------- 1 file changed, 103 insertions(+), 86 deletions(-) diff --git a/src/usb-device-manager.c b/src/usb-device-manager.c index adee0fc..0c67acc 100644 --- a/src/usb-device-manager.c +++ b/src/usb-device-manager.c @@ -123,6 +123,7 @@ struct _SpiceUsbDeviceManagerPrivate { #ifdef G_OS_WIN32 SpiceWinUsbDriver *installer; #endif + gboolean use_usbclerk; #endif GPtrArray *devices; GPtrArray *channels; @@ -225,6 +226,9 @@ static void spice_usb_device_manager_init(SpiceUsbDeviceManager *self) priv->devices = g_ptr_array_new_with_free_func((GDestroyNotify) spice_usb_device_unref); #endif +#ifdef G_OS_WIN32 + priv->use_usbclerk = TRUE; +#endif } static gboolean spice_usb_device_manager_initable_init(GInitable *initable, @@ -352,8 +356,10 @@ static void spice_usb_device_manager_finalize(GObject *gobject) free(priv->auto_conn_filter_rules); free(priv->redirect_on_connect_rules); #ifdef G_OS_WIN32 - if (priv->installer) + if (priv->installer) { + g_warn_if_fail(priv->use_usbclerk); g_object_unref(priv->installer); + } #endif #endif @@ -647,13 +653,15 @@ static gboolean spice_usb_device_manager_get_udev_bus_n_address( *bus = *address = 0; -#ifndef G_OS_WIN32 - bus_str = g_udev_device_get_property(udev, "BUSNUM"); - address_str = g_udev_device_get_property(udev, "DEVNUM"); -#else /* Windows -- request vid:pid instead */ - bus_str = g_udev_device_get_property(udev, "VID"); - address_str = g_udev_device_get_property(udev, "PID"); -#endif + if (manager->priv->use_usbclerk) { + /* Windows WinUsb/UsbClerk -- request vid:pid instead */ + bus_str = g_udev_device_get_property(udev, "VID"); + address_str = g_udev_device_get_property(udev, "PID"); + } else { + /* 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) @@ -790,13 +798,18 @@ static void spice_usb_device_manager_auto_connect_cb(GObject *gobject, spice_usb_device_unref(device); } -#ifndef G_OS_WIN32 /* match functions for Linux -- match by bus.addr */ static gboolean spice_usb_device_manager_device_match(SpiceUsbDeviceManager *self, SpiceUsbDevice *device, const int bus, const int address) { - return (spice_usb_device_get_busnum(device) == bus && - spice_usb_device_get_devaddr(device) == address); + if (self->priv->use_usbclerk) { + return (spice_usb_device_get_vid(device) == bus && + spice_usb_device_get_pid(device) == address); + } else { + return (spice_usb_device_get_busnum(device) == bus && + spice_usb_device_get_devaddr(device) == address); + } + g_return_val_if_reached(FALSE); } #ifdef USE_GUDEV @@ -804,32 +817,22 @@ static gboolean spice_usb_device_manager_libdev_match(SpiceUsbDeviceManager *self, libusb_device *libdev, const int bus, const int address) { - return (libusb_get_bus_number(libdev) == bus && - libusb_get_device_address(libdev) == address); -} -#endif - -#else /* Win32 -- match functions for Windows -- match by vid:pid */ -static gboolean -spice_usb_device_manager_device_match(SpiceUsbDeviceManager *self, SpiceUsbDevice *device, - const int vid, const int pid) -{ - return (spice_usb_device_get_vid(device) == vid && - spice_usb_device_get_pid(device) == pid); -} - -static gboolean -spice_usb_device_manager_libdev_match(SpiceUsbDeviceManager *self, libusb_device *libdev, - const int vid, const int pid) -{ - int vid2, pid2; + if (self->priv->use_usbclerk) { + /* WinUSB -- match functions for Windows -- match by vid:pid */ + int vid, pid; - if (!spice_usb_device_manager_get_libdev_vid_pid(libdev, &vid2, &pid2)) { - return FALSE; + if (!spice_usb_device_manager_get_libdev_vid_pid(libdev, &vid, &pid)) { + return FALSE; + } + return (bus == vid && address == pid); + } else { + /* match functions for Linux/UsbDk -- match by bus.addr */ + return (libusb_get_bus_number(libdev) == bus && + libusb_get_device_address(libdev) == address); } - return (vid == vid2 && pid == pid2); + g_return_val_if_reached(FALSE); } -#endif /* of Win32 -- match functions */ +#endif static SpiceUsbDevice* spice_usb_device_manager_find_device(SpiceUsbDeviceManager *self, @@ -905,12 +908,14 @@ static void spice_usb_device_manager_remove_dev(SpiceUsbDeviceManager *self, } #ifdef G_OS_WIN32 - const guint8 state = spice_usb_device_get_state(device); - if ((state == SPICE_USB_DEVICE_STATE_INSTALLING) || - (state == SPICE_USB_DEVICE_STATE_UNINSTALLING)) { - SPICE_DEBUG("skipping " DEV_ID_FMT ". It is un/installing its driver", - bus, address); - return; + if (priv->use_usbclerk) { + const guint8 state = spice_usb_device_get_state(device); + if ((state == SPICE_USB_DEVICE_STATE_INSTALLING) || + (state == SPICE_USB_DEVICE_STATE_UNINSTALLING)) { + SPICE_DEBUG("skipping " DEV_ID_FMT ". It is un/installing its driver", + bus, address); + return; + } } #endif @@ -1108,6 +1113,7 @@ static void spice_usb_device_manager_drv_install_cb(GObject *gobject, g_free(cbinfo); g_return_if_fail(SPICE_IS_USB_DEVICE_MANAGER(self)); + g_return_if_fail(self->priv->use_usbclerk); g_return_if_fail(SPICE_IS_WIN_USB_DRIVER(installer)); g_return_if_fail(device!= NULL); @@ -1140,6 +1146,7 @@ static void spice_usb_device_manager_drv_uninstall_cb(GObject *gobject, SPICE_DEBUG("Win USB driver uninstall finished"); g_return_if_fail(SPICE_IS_USB_DEVICE_MANAGER(self)); + g_return_if_fail(self->priv->use_usbclerk); if (!spice_win_usb_driver_uninstall_finish(cbinfo->installer, res, &err)) { g_warning("win usb driver uninstall failed -- %s", err->message); @@ -1470,34 +1477,37 @@ void spice_usb_device_manager_connect_device_async(SpiceUsbDeviceManager *self, GAsyncReadyCallback callback, gpointer user_data) { - + if (self->priv->use_usbclerk) { #if defined(USE_USBREDIR) && defined(G_OS_WIN32) - SpiceWinUsbDriver *installer; - UsbInstallCbInfo *cbinfo; + SpiceWinUsbDriver *installer; + UsbInstallCbInfo *cbinfo; - g_return_if_fail(self->priv->installer); + g_return_if_fail(self->priv->installer); - spice_usb_device_set_state(device, SPICE_USB_DEVICE_STATE_INSTALLING); + spice_usb_device_set_state(device, SPICE_USB_DEVICE_STATE_INSTALLING); - installer = self->priv->installer; - cbinfo = g_new0(UsbInstallCbInfo, 1); - cbinfo->manager = self; - cbinfo->device = spice_usb_device_ref(device); - cbinfo->installer = installer; - cbinfo->cancellable = cancellable; - cbinfo->callback = callback; - cbinfo->user_data = user_data; + installer = self->priv->installer; + cbinfo = g_new0(UsbInstallCbInfo, 1); + cbinfo->manager = self; + cbinfo->device = spice_usb_device_ref(device); + cbinfo->installer = installer; + cbinfo->cancellable = cancellable; + cbinfo->callback = callback; + cbinfo->user_data = user_data; - spice_win_usb_driver_install_async(installer, device, cancellable, - spice_usb_device_manager_drv_install_cb, - cbinfo); + spice_win_usb_driver_install_async(installer, device, cancellable, + spice_usb_device_manager_drv_install_cb, + cbinfo); #else - _spice_usb_device_manager_connect_device_async(self, - device, - cancellable, - callback, - user_data); + g_assert_not_reached(); #endif + } else { + _spice_usb_device_manager_connect_device_async(self, + device, + cancellable, + callback, + user_data); + } } gboolean spice_usb_device_manager_connect_device_finish( @@ -1538,30 +1548,32 @@ void spice_usb_device_manager_disconnect_device(SpiceUsbDeviceManager *self, spice_usbredir_channel_disconnect_device(channel); #ifdef G_OS_WIN32 - SpiceWinUsbDriver *installer; - UsbInstallCbInfo *cbinfo; - guint8 state; - - g_warn_if_fail(device != NULL); - g_return_if_fail(self->priv->installer); - - state = spice_usb_device_get_state(device); - if ((state != SPICE_USB_DEVICE_STATE_INSTALLED) && - (state != SPICE_USB_DEVICE_STATE_CONNECTED)) { - return; - } + if (self->priv->use_usbclerk) { + SpiceWinUsbDriver *installer; + UsbInstallCbInfo *cbinfo; + guint8 state; + + g_warn_if_fail(device != NULL); + g_return_if_fail(self->priv->installer); + + state = spice_usb_device_get_state(device); + if ((state != SPICE_USB_DEVICE_STATE_INSTALLED) && + (state != SPICE_USB_DEVICE_STATE_CONNECTED)) { + return; + } - spice_usb_device_set_state(device, SPICE_USB_DEVICE_STATE_UNINSTALLING); + spice_usb_device_set_state(device, SPICE_USB_DEVICE_STATE_UNINSTALLING); - installer = self->priv->installer; - cbinfo = g_new0(UsbInstallCbInfo, 1); - cbinfo->manager = self; - cbinfo->device = spice_usb_device_ref(device); - cbinfo->installer = installer; + installer = self->priv->installer; + cbinfo = g_new0(UsbInstallCbInfo, 1); + cbinfo->manager = self; + cbinfo->device = spice_usb_device_ref(device); + cbinfo->installer = installer; - spice_win_usb_driver_uninstall_async(installer, device, NULL, - spice_usb_device_manager_drv_uninstall_cb, - cbinfo); + spice_win_usb_driver_uninstall_async (installer, device, NULL, + spice_usb_device_manager_drv_uninstall_cb, + cbinfo); + } #endif #endif @@ -1841,13 +1853,18 @@ spice_usb_manager_device_equal_libdev(SpiceUsbDeviceManager *manager, SpiceUsbDevice *device, libusb_device *libdev) { - int vid, pid; + int vid, pid; if ((device == NULL) || (libdev == NULL)) - return FALSE; + return FALSE; + + if (manager->priv->use_usbclerk) { + vid = spice_usb_device_get_vid(device); + pid = spice_usb_device_get_pid(device); + } else { + g_return_val_if_reached(FALSE); + } - vid = spice_usb_device_get_vid(device); - pid = spice_usb_device_get_pid(device); return spice_usb_device_manager_libdev_match(manager, libdev, vid, pid); } #endif -- 2.1.0 _______________________________________________ Spice-devel mailing list Spice-devel@xxxxxxxxxxxxxxxxxxxxx http://lists.freedesktop.org/mailman/listinfo/spice-devel