Move sc_usb out of sc_aoa

This will allow to initialize a USB device separately and pass it to
sc_aoa.

PR #2974 <https://github.com/Genymobile/scrcpy/pull/2974>
This commit is contained in:
Romain Vimont 2022-01-24 23:11:42 +01:00
parent 48e3ff284f
commit adda47b0f7
3 changed files with 27 additions and 25 deletions

View File

@ -27,6 +27,7 @@
# include "usb/aoa_hid.h"
# include "usb/hid_keyboard.h"
# include "usb/hid_mouse.h"
# include "usb/usb.h"
#endif
#include "util/acksync.h"
#include "util/log.h"
@ -47,6 +48,7 @@ struct scrcpy {
struct sc_controller controller;
struct sc_file_pusher file_pusher;
#ifdef HAVE_USB
struct sc_usb usb;
struct sc_aoa aoa;
// sequence/ack helper to synchronize clipboard and Ctrl+v via HID
struct sc_acksync acksync;
@ -422,9 +424,17 @@ scrcpy(struct scrcpy_options *options) {
goto end;
}
ok = sc_aoa_init(&s->aoa, serial, &s->acksync);
ok = sc_usb_init(&s->usb, serial);
if (!ok) {
LOGE("Failed to initialized USB device");
sc_acksync_destroy(&s->acksync);
goto aoa_hid_end;
}
ok = sc_aoa_init(&s->aoa, &s->usb, &s->acksync);
if (!ok) {
LOGE("Failed to enable HID over AOA");
sc_usb_destroy(&s->usb);
sc_acksync_destroy(&s->acksync);
goto aoa_hid_end;
}
@ -451,6 +461,7 @@ scrcpy(struct scrcpy_options *options) {
if (!need_aoa || !sc_aoa_start(&s->aoa)) {
sc_acksync_destroy(&s->acksync);
sc_usb_destroy(&s->usb);
sc_aoa_destroy(&s->aoa);
goto aoa_hid_end;
}
@ -639,6 +650,7 @@ end:
if (aoa_hid_initialized) {
sc_aoa_join(&s->aoa);
sc_aoa_destroy(&s->aoa);
sc_usb_destroy(&s->usb);
}
#endif

View File

@ -51,7 +51,7 @@ log_libusb_error(enum libusb_error errcode) {
}
bool
sc_aoa_init(struct sc_aoa *aoa, const char *serial,
sc_aoa_init(struct sc_aoa *aoa, struct sc_usb *usb,
struct sc_acksync *acksync) {
assert(acksync);
@ -62,24 +62,15 @@ sc_aoa_init(struct sc_aoa *aoa, const char *serial,
}
if (!sc_cond_init(&aoa->event_cond)) {
goto error_destroy_mutex;
}
bool ok = sc_usb_init(&aoa->usb, serial);
if (!ok) {
goto error_destroy_cond;
sc_mutex_destroy(&aoa->mutex);
return false;
}
aoa->stopped = false;
aoa->acksync = acksync;
aoa->usb = usb;
return true;
error_destroy_cond:
sc_cond_destroy(&aoa->event_cond);
error_destroy_mutex:
sc_mutex_destroy(&aoa->mutex);
return false;
}
void
@ -90,7 +81,6 @@ sc_aoa_destroy(struct sc_aoa *aoa) {
sc_hid_event_destroy(&event);
}
sc_usb_destroy(&aoa->usb);
sc_cond_destroy(&aoa->event_cond);
sc_mutex_destroy(&aoa->mutex);
}
@ -107,8 +97,8 @@ sc_aoa_register_hid(struct sc_aoa *aoa, uint16_t accessory_id,
uint16_t index = report_desc_size;
unsigned char *buffer = NULL;
uint16_t length = 0;
int result = libusb_control_transfer(aoa->usb.handle, request_type, request,
value, index, buffer, length,
int result = libusb_control_transfer(aoa->usb->handle, request_type,
request, value, index, buffer, length,
DEFAULT_TIMEOUT);
if (result < 0) {
log_libusb_error((enum libusb_error) result);
@ -143,8 +133,8 @@ sc_aoa_set_hid_report_desc(struct sc_aoa *aoa, uint16_t accessory_id,
// libusb_control_transfer expects a pointer to non-const
unsigned char *buffer = (unsigned char *) report_desc;
uint16_t length = report_desc_size;
int result = libusb_control_transfer(aoa->usb.handle, request_type, request,
value, index, buffer, length,
int result = libusb_control_transfer(aoa->usb->handle, request_type,
request, value, index, buffer, length,
DEFAULT_TIMEOUT);
if (result < 0) {
log_libusb_error((enum libusb_error) result);
@ -185,8 +175,8 @@ sc_aoa_send_hid_event(struct sc_aoa *aoa, const struct sc_hid_event *event) {
uint16_t index = 0;
unsigned char *buffer = event->buffer;
uint16_t length = event->size;
int result = libusb_control_transfer(aoa->usb.handle, request_type, request,
value, index, buffer, length,
int result = libusb_control_transfer(aoa->usb->handle, request_type,
request, value, index, buffer, length,
DEFAULT_TIMEOUT);
if (result < 0) {
log_libusb_error((enum libusb_error) result);
@ -207,8 +197,8 @@ sc_aoa_unregister_hid(struct sc_aoa *aoa, const uint16_t accessory_id) {
uint16_t index = 0;
unsigned char *buffer = NULL;
uint16_t length = 0;
int result = libusb_control_transfer(aoa->usb.handle, request_type, request,
value, index, buffer, length,
int result = libusb_control_transfer(aoa->usb->handle, request_type,
request, value, index, buffer, length,
DEFAULT_TIMEOUT);
if (result < 0) {
log_libusb_error((enum libusb_error) result);

View File

@ -30,7 +30,7 @@ sc_hid_event_destroy(struct sc_hid_event *hid_event);
struct sc_hid_event_queue CBUF(struct sc_hid_event, 64);
struct sc_aoa {
struct sc_usb usb;
struct sc_usb *usb;
sc_thread thread;
sc_mutex mutex;
sc_cond event_cond;
@ -41,7 +41,7 @@ struct sc_aoa {
};
bool
sc_aoa_init(struct sc_aoa *aoa, const char *serial, struct sc_acksync *acksync);
sc_aoa_init(struct sc_aoa *aoa, struct sc_usb *usb, struct sc_acksync *acksync);
void
sc_aoa_destroy(struct sc_aoa *aoa);