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 <https://github.com/Genymobile/scrcpy/pull/2974>
This commit is contained in:
Romain Vimont 2022-01-25 19:10:23 +01:00
parent 2114f48185
commit bbef426a4b
3 changed files with 33 additions and 13 deletions

View file

@ -424,9 +424,17 @@ scrcpy(struct scrcpy_options *options) {
goto end; goto end;
} }
ok = sc_usb_init(&s->usb, serial); ok = sc_usb_init(&s->usb);
if (!ok) { 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); sc_acksync_destroy(&s->acksync);
goto aoa_hid_end; goto aoa_hid_end;
} }
@ -434,6 +442,7 @@ scrcpy(struct scrcpy_options *options) {
ok = sc_aoa_init(&s->aoa, &s->usb, &s->acksync); ok = sc_aoa_init(&s->aoa, &s->usb, &s->acksync);
if (!ok) { if (!ok) {
LOGE("Failed to enable HID over AOA"); LOGE("Failed to enable HID over AOA");
sc_usb_disconnect(&s->usb);
sc_usb_destroy(&s->usb); sc_usb_destroy(&s->usb);
sc_acksync_destroy(&s->acksync); sc_acksync_destroy(&s->acksync);
goto aoa_hid_end; goto aoa_hid_end;
@ -461,6 +470,7 @@ scrcpy(struct scrcpy_options *options) {
if (!need_aoa || !sc_aoa_start(&s->aoa)) { if (!need_aoa || !sc_aoa_start(&s->aoa)) {
sc_acksync_destroy(&s->acksync); sc_acksync_destroy(&s->acksync);
sc_usb_disconnect(&s->usb);
sc_usb_destroy(&s->usb); sc_usb_destroy(&s->usb);
sc_aoa_destroy(&s->aoa); sc_aoa_destroy(&s->aoa);
goto aoa_hid_end; goto aoa_hid_end;
@ -650,6 +660,7 @@ end:
if (aoa_hid_initialized) { if (aoa_hid_initialized) {
sc_aoa_join(&s->aoa); sc_aoa_join(&s->aoa);
sc_aoa_destroy(&s->aoa); sc_aoa_destroy(&s->aoa);
sc_usb_disconnect(&s->usb);
sc_usb_destroy(&s->usb); sc_usb_destroy(&s->usb);
} }
#endif #endif

View file

@ -76,18 +76,23 @@ sc_usb_open_handle(libusb_device *device) {
} }
bool bool
sc_usb_init(struct sc_usb *usb, const char *serial) { sc_usb_init(struct sc_usb *usb) {
assert(serial); usb->handle = NULL;
return libusb_init(&usb->context) == LIBUSB_SUCCESS;
}
// There is only one device, initialize the context here void
if (libusb_init(&usb->context) != LIBUSB_SUCCESS) { sc_usb_destroy(struct sc_usb *usb) {
return false; 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); libusb_device *device = sc_usb_find_device(usb, serial);
if (!device) { if (!device) {
LOGW("USB device %s not found", serial); LOGW("USB device %s not found", serial);
libusb_exit(usb->context);
return false; return false;
} }
@ -95,7 +100,6 @@ sc_usb_init(struct sc_usb *usb, const char *serial) {
libusb_unref_device(device); libusb_unref_device(device);
if (!usb->handle) { if (!usb->handle) {
LOGW("Could not open USB device %s", serial); LOGW("Could not open USB device %s", serial);
libusb_exit(usb->context);
return false; return false;
} }
@ -103,7 +107,6 @@ sc_usb_init(struct sc_usb *usb, const char *serial) {
} }
void void
sc_usb_destroy(struct sc_usb *usb) { sc_usb_disconnect(struct sc_usb *usb) {
libusb_close(usb->handle); libusb_close(usb->handle);
libusb_exit(usb->context);
} }

View file

@ -12,9 +12,15 @@ struct sc_usb {
}; };
bool bool
sc_usb_init(struct sc_usb *usb, const char *serial); sc_usb_init(struct sc_usb *usb);
void void
sc_usb_destroy(struct sc_usb *usb); 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 #endif