From 0ee9e2ff51baa4826283b521053a383550b93ea1 Mon Sep 17 00:00:00 2001 From: Romain Vimont <rom@rom1v.com> Date: Tue, 25 Jan 2022 21:11:32 +0100 Subject: [PATCH] Expose function to find a USB device The device was automatically found by sc_usb_connect(). Instead, expose a function to find a device from a serial, and let the caller connect to the device found (if any). This will allow to list all devices first, then select one device to connect to. PR #2974 <https://github.com/Genymobile/scrcpy/pull/2974> --- app/src/scrcpy.c | 12 +++++++++++- app/src/usb/usb.c | 16 ++++------------ app/src/usb/usb.h | 5 ++++- 3 files changed, 19 insertions(+), 14 deletions(-) diff --git a/app/src/scrcpy.c b/app/src/scrcpy.c index 3de683db..9039eede 100644 --- a/app/src/scrcpy.c +++ b/app/src/scrcpy.c @@ -431,7 +431,17 @@ scrcpy(struct scrcpy_options *options) { goto aoa_hid_end; } - ok = sc_usb_connect(&s->usb, serial); + assert(serial); + libusb_device *device = sc_usb_find_device(&s->usb, serial); + if (!device) { + LOGE("Could not find USB device %s", serial); + sc_usb_destroy(&s->usb); + sc_acksync_destroy(&s->acksync); + goto aoa_hid_end; + } + + ok = sc_usb_connect(&s->usb, device); + libusb_unref_device(device); if (!ok) { LOGE("Failed to connect to USB device %s", serial); sc_usb_destroy(&s->usb); diff --git a/app/src/usb/usb.c b/app/src/usb/usb.c index e2bbaee1..459aefc0 100644 --- a/app/src/usb/usb.c +++ b/app/src/usb/usb.c @@ -57,8 +57,10 @@ accept_device(libusb_device *device, const char *serial) { return matches; } -static libusb_device * +libusb_device * sc_usb_find_device(struct sc_usb *usb, const char *serial) { + assert(serial); + libusb_device **list; libusb_device *result = NULL; ssize_t count = libusb_get_device_list(usb->context, &list); @@ -103,19 +105,9 @@ sc_usb_destroy(struct sc_usb *usb) { } bool -sc_usb_connect(struct sc_usb *usb, const char *serial) { - assert(serial); - - libusb_device *device = sc_usb_find_device(usb, serial); - if (!device) { - LOGW("USB device %s not found", serial); - return false; - } - +sc_usb_connect(struct sc_usb *usb, libusb_device *device) { usb->handle = sc_usb_open_handle(device); - libusb_unref_device(device); if (!usb->handle) { - LOGW("Could not open USB device %s", serial); return false; } diff --git a/app/src/usb/usb.h b/app/src/usb/usb.h index e487a32c..0ab7ce90 100644 --- a/app/src/usb/usb.h +++ b/app/src/usb/usb.h @@ -17,8 +17,11 @@ sc_usb_init(struct sc_usb *usb); void sc_usb_destroy(struct sc_usb *usb); +libusb_device * +sc_usb_find_device(struct sc_usb *usb, const char *serial); + bool -sc_usb_connect(struct sc_usb *usb, const char *serial); +sc_usb_connect(struct sc_usb *usb, libusb_device *device); void sc_usb_disconnect(struct sc_usb *usb);