From ed2e45ee29099cb6656782067f28376f65f94289 Mon Sep 17 00:00:00 2001 From: Romain Vimont Date: Sat, 1 Jan 2022 20:00:33 +0100 Subject: [PATCH] Refactor AOA/HID keyboard initialization This paves the way to add support for HID mouse initialization. --- app/src/scrcpy.c | 43 +++++++++++++++++++++++++++++-------------- 1 file changed, 29 insertions(+), 14 deletions(-) diff --git a/app/src/scrcpy.c b/app/src/scrcpy.c index 6b8074e9..5907c91e 100644 --- a/app/src/scrcpy.c +++ b/app/src/scrcpy.c @@ -329,6 +329,7 @@ scrcpy(struct scrcpy_options *options) { bool stream_started = false; #ifdef HAVE_AOA_HID bool aoa_hid_initialized = false; + bool hid_keyboard_initialized = false; #endif bool controller_initialized = false; bool controller_started = false; @@ -448,40 +449,52 @@ scrcpy(struct scrcpy_options *options) { if (options->control) { #ifdef HAVE_AOA_HID - if (options->keyboard_input_mode == SC_KEYBOARD_INPUT_MODE_HID) { + bool use_hid_keyboard = + options->keyboard_input_mode == SC_KEYBOARD_INPUT_MODE_HID; + if (use_hid_keyboard) { bool ok = sc_acksync_init(&s->acksync); if (!ok) { goto end; } - ok = sc_aoa_init(&s->aoa, serial, acksync); + ok = sc_aoa_init(&s->aoa, serial, &s->acksync); if (!ok) { + LOGE("Failed to enable HID over AOA"); sc_acksync_destroy(&s->acksync); goto aoa_hid_end; } - if (!sc_hid_keyboard_init(&s->keyboard_hid, &s->aoa)) { + if (use_hid_keyboard) { + if (sc_hid_keyboard_init(&s->keyboard_hid, &s->aoa)) { + hid_keyboard_initialized = true; + kp = &s->keyboard_hid.key_processor; + } else { + LOGE("Could not initialize HID keyboard"); + } + } + + bool need_aoa = hid_keyboard_initialized; + + if (!need_aoa || !sc_aoa_start(&s->aoa)) { sc_acksync_destroy(&s->acksync); sc_aoa_destroy(&s->aoa); goto aoa_hid_end; } - if (!sc_aoa_start(&s->aoa)) { - sc_acksync_destroy(&s->acksync); - sc_hid_keyboard_destroy(&s->keyboard_hid); - sc_aoa_destroy(&s->aoa); - goto aoa_hid_end; - } - - kp = &s->keyboard_hid.key_processor; acksync = &s->acksync; aoa_hid_initialized = true; aoa_hid_end: if (!aoa_hid_initialized) { - LOGE("Failed to enable HID over AOA, " - "fallback to default keyboard injection method " + if (hid_keyboard_initialized) { + sc_hid_keyboard_destroy(&s->keyboard_hid); + hid_keyboard_initialized = false; + } + } + + if (use_hid_keyboard && !hid_keyboard_initialized) { + LOGE("Fallback to default keyboard injection method " "(-K/--hid-keyboard ignored)"); options->keyboard_input_mode = SC_KEYBOARD_INPUT_MODE_INJECT; } @@ -590,7 +603,9 @@ end: // end-of-stream #ifdef HAVE_AOA_HID if (aoa_hid_initialized) { - sc_hid_keyboard_destroy(&s->keyboard_hid); + if (hid_keyboard_initialized) { + sc_hid_keyboard_destroy(&s->keyboard_hid); + } sc_aoa_stop(&s->aoa); } if (acksync) {