From 1c17f57c101fc08fd54c99b5c6c947414f5f35be Mon Sep 17 00:00:00 2001 From: Romain Vimont Date: Wed, 26 Jan 2022 22:08:55 +0100 Subject: [PATCH] Find a list of devices instead of a single one Several devices may match the requested serial, but above all, this paves the way to list all devices (when no serial is provided). PR #2974 --- app/src/scrcpy.c | 27 +++++++++++++++++++-------- app/src/usb/usb.c | 25 ++++++++++++++++--------- app/src/usb/usb.h | 9 ++++++--- 3 files changed, 41 insertions(+), 20 deletions(-) diff --git a/app/src/scrcpy.c b/app/src/scrcpy.c index 7d58b0f1..1c84b428 100644 --- a/app/src/scrcpy.c +++ b/app/src/scrcpy.c @@ -432,21 +432,32 @@ scrcpy(struct scrcpy_options *options) { } assert(serial); - struct sc_usb_device usb_device; - ok = sc_usb_find_device(&s->usb, serial, &usb_device); - if (!ok) { + struct sc_usb_device usb_devices[16]; + ssize_t count = sc_usb_find_devices(&s->usb, serial, usb_devices, + ARRAY_LEN(usb_devices)); + if (count <= 0) { LOGE("Could not find USB device %s", serial); sc_usb_destroy(&s->usb); sc_acksync_destroy(&s->acksync); goto aoa_hid_end; } - LOGI("USB device: %s (%04" PRIx16 ":%04" PRIx16 ") %s %s", - usb_device.serial, usb_device.vid, usb_device.pid, - usb_device.manufacturer, usb_device.product); + if (count > 1) { + LOGE("Multiple (%d) devices with serial %s", (int) count, serial); + sc_usb_device_destroy_all(usb_devices, count); + sc_usb_destroy(&s->usb); + sc_acksync_destroy(&s->acksync); + goto aoa_hid_end; + } - ok = sc_usb_connect(&s->usb, usb_device.device); - sc_usb_device_destroy(&usb_device); + struct sc_usb_device *usb_device = &usb_devices[0]; + + LOGI("USB device: %s (%04" PRIx16 ":%04" PRIx16 ") %s %s", + usb_device->serial, usb_device->vid, usb_device->pid, + usb_device->manufacturer, usb_device->product); + + ok = sc_usb_connect(&s->usb, usb_device->device); + sc_usb_device_destroy(usb_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 9d5f35be..6add7b2e 100644 --- a/app/src/usb/usb.c +++ b/app/src/usb/usb.c @@ -80,29 +80,36 @@ sc_usb_device_destroy(struct sc_usb_device *usb_device) { free(usb_device->product); } -bool -sc_usb_find_device(struct sc_usb *usb, const char *serial, - struct sc_usb_device *out) { +void +sc_usb_device_destroy_all(struct sc_usb_device *usb_devices, size_t count) { + for (size_t i = 0; i < count; ++i) { + sc_usb_device_destroy(&usb_devices[i]); + } +} + +ssize_t +sc_usb_find_devices(struct sc_usb *usb, const char *serial, + struct sc_usb_device *devices, size_t len) { assert(serial); libusb_device **list; ssize_t count = libusb_get_device_list(usb->context, &list); if (count < 0) { log_libusb_error((enum libusb_error) count); - return false; + return -1; } - for (size_t i = 0; i < (size_t) count; ++i) { + size_t idx = 0; + for (size_t i = 0; i < (size_t) count && idx < len; ++i) { libusb_device *device = list[i]; - if (accept_device(device, serial, out)) { - libusb_free_device_list(list, 1); - return true; + if (accept_device(device, serial, &devices[idx])) { + ++idx; } } libusb_free_device_list(list, 1); - return false; + return idx; } static libusb_device_handle * diff --git a/app/src/usb/usb.h b/app/src/usb/usb.h index ea6e5514..a271d034 100644 --- a/app/src/usb/usb.h +++ b/app/src/usb/usb.h @@ -23,15 +23,18 @@ struct sc_usb_device { void sc_usb_device_destroy(struct sc_usb_device *usb_device); +void +sc_usb_device_destroy_all(struct sc_usb_device *usb_devices, size_t count); + bool sc_usb_init(struct sc_usb *usb); void sc_usb_destroy(struct sc_usb *usb); -bool -sc_usb_find_device(struct sc_usb *usb, const char *serial, - struct sc_usb_device *out); +ssize_t +sc_usb_find_devices(struct sc_usb *usb, const char *serial, + struct sc_usb_device *devices, size_t len); bool sc_usb_connect(struct sc_usb *usb, libusb_device *device);