From bbef426a4bb69dc96fbd8c1075c0e569d71fdff4 Mon Sep 17 00:00:00 2001 From: Romain Vimont Date: Tue, 25 Jan 2022 19:10:23 +0100 Subject: [PATCH] Split USB initialization and connection This will allow to execute other USB calls (retrieving the device list for example) before connecting to the selected device. PR #2974 --- app/src/scrcpy.c | 15 +++++++++++++-- app/src/usb/usb.c | 23 +++++++++++++---------- app/src/usb/usb.h | 8 +++++++- 3 files changed, 33 insertions(+), 13 deletions(-) diff --git a/app/src/scrcpy.c b/app/src/scrcpy.c index 0c7966ac..3de683db 100644 --- a/app/src/scrcpy.c +++ b/app/src/scrcpy.c @@ -424,9 +424,17 @@ scrcpy(struct scrcpy_options *options) { goto end; } - ok = sc_usb_init(&s->usb, serial); + ok = sc_usb_init(&s->usb); if (!ok) { - LOGE("Failed to initialized USB device"); + LOGE("Failed to initialize USB"); + sc_acksync_destroy(&s->acksync); + goto aoa_hid_end; + } + + ok = sc_usb_connect(&s->usb, serial); + if (!ok) { + LOGE("Failed to connect to USB device %s", serial); + sc_usb_destroy(&s->usb); sc_acksync_destroy(&s->acksync); goto aoa_hid_end; } @@ -434,6 +442,7 @@ scrcpy(struct scrcpy_options *options) { ok = sc_aoa_init(&s->aoa, &s->usb, &s->acksync); if (!ok) { LOGE("Failed to enable HID over AOA"); + sc_usb_disconnect(&s->usb); sc_usb_destroy(&s->usb); sc_acksync_destroy(&s->acksync); goto aoa_hid_end; @@ -461,6 +470,7 @@ scrcpy(struct scrcpy_options *options) { if (!need_aoa || !sc_aoa_start(&s->aoa)) { sc_acksync_destroy(&s->acksync); + sc_usb_disconnect(&s->usb); sc_usb_destroy(&s->usb); sc_aoa_destroy(&s->aoa); goto aoa_hid_end; @@ -650,6 +660,7 @@ end: if (aoa_hid_initialized) { sc_aoa_join(&s->aoa); sc_aoa_destroy(&s->aoa); + sc_usb_disconnect(&s->usb); sc_usb_destroy(&s->usb); } #endif diff --git a/app/src/usb/usb.c b/app/src/usb/usb.c index 321d745c..64f30353 100644 --- a/app/src/usb/usb.c +++ b/app/src/usb/usb.c @@ -76,18 +76,23 @@ sc_usb_open_handle(libusb_device *device) { } bool -sc_usb_init(struct sc_usb *usb, const char *serial) { - assert(serial); +sc_usb_init(struct sc_usb *usb) { + usb->handle = NULL; + return libusb_init(&usb->context) == LIBUSB_SUCCESS; +} - // There is only one device, initialize the context here - if (libusb_init(&usb->context) != LIBUSB_SUCCESS) { - return false; - } +void +sc_usb_destroy(struct sc_usb *usb) { + libusb_exit(usb->context); +} + +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); - libusb_exit(usb->context); return false; } @@ -95,7 +100,6 @@ sc_usb_init(struct sc_usb *usb, const char *serial) { libusb_unref_device(device); if (!usb->handle) { LOGW("Could not open USB device %s", serial); - libusb_exit(usb->context); return false; } @@ -103,7 +107,6 @@ sc_usb_init(struct sc_usb *usb, const char *serial) { } void -sc_usb_destroy(struct sc_usb *usb) { +sc_usb_disconnect(struct sc_usb *usb) { libusb_close(usb->handle); - libusb_exit(usb->context); } diff --git a/app/src/usb/usb.h b/app/src/usb/usb.h index 8ee3eb9f..e487a32c 100644 --- a/app/src/usb/usb.h +++ b/app/src/usb/usb.h @@ -12,9 +12,15 @@ struct sc_usb { }; bool -sc_usb_init(struct sc_usb *usb, const char *serial); +sc_usb_init(struct sc_usb *usb); void sc_usb_destroy(struct sc_usb *usb); +bool +sc_usb_connect(struct sc_usb *usb, const char *serial); + +void +sc_usb_disconnect(struct sc_usb *usb); + #endif