From 5e918ac0c39cf7004c7f6843f2d28d4a06fd216e Mon Sep 17 00:00:00 2001 From: Romain Vimont Date: Fri, 26 Nov 2021 22:05:28 +0100 Subject: [PATCH] Use enum for key injection mode There was only two key injection modes: - the default one - the mode with --prefer-text enabled To prepare the addition of another mode (--raw-key-events), use an enum instead of a bool. PR #2831 --- app/src/cli.c | 2 +- app/src/keyboard_inject.c | 16 ++++++++-------- app/src/keyboard_inject.h | 2 +- app/src/options.c | 2 +- app/src/options.h | 13 ++++++++++++- app/tests/test_cli.c | 2 +- 6 files changed, 24 insertions(+), 13 deletions(-) diff --git a/app/src/cli.c b/app/src/cli.c index 410ce25a..67a7a89f 100644 --- a/app/src/cli.c +++ b/app/src/cli.c @@ -1350,7 +1350,7 @@ parse_args_with_getopt(struct scrcpy_cli_args *args, int argc, char *argv[], opts->push_target = optarg; break; case OPT_PREFER_TEXT: - opts->prefer_text = true; + opts->key_inject_mode = SC_KEY_INJECT_MODE_TEXT; break; case OPT_ROTATION: if (!parse_rotation(optarg, &opts->rotation)) { diff --git a/app/src/keyboard_inject.c b/app/src/keyboard_inject.c index bb3bb953..e6212193 100644 --- a/app/src/keyboard_inject.c +++ b/app/src/keyboard_inject.c @@ -30,7 +30,7 @@ convert_keycode_action(SDL_EventType from, enum android_keyevent_action *to) { static bool convert_keycode(SDL_Keycode from, enum android_keycode *to, uint16_t mod, - bool prefer_text) { + enum sc_key_inject_mode key_inject_mode) { // Navigation keys and ENTER. // Used in all modes. static const struct sc_intmap_entry special_keys[] = { @@ -118,7 +118,7 @@ convert_keycode(SDL_Keycode from, enum android_keycode *to, uint16_t mod, } } - if (prefer_text && !(mod & KMOD_CTRL)) { + if (key_inject_mode == SC_KEY_INJECT_MODE_TEXT && !(mod & KMOD_CTRL)) { // do not forward alpha and space key events (unless Ctrl is pressed) return false; } @@ -199,7 +199,7 @@ convert_meta_state(SDL_Keymod mod) { static bool convert_input_key(const SDL_KeyboardEvent *from, struct control_msg *to, - bool prefer_text, uint32_t repeat) { + enum sc_key_inject_mode key_inject_mode, uint32_t repeat) { to->type = CONTROL_MSG_TYPE_INJECT_KEYCODE; if (!convert_keycode_action(from->type, &to->inject_keycode.action)) { @@ -208,7 +208,7 @@ convert_input_key(const SDL_KeyboardEvent *from, struct control_msg *to, uint16_t mod = from->keysym.mod; if (!convert_keycode(from->keysym.sym, &to->inject_keycode.keycode, mod, - prefer_text)) { + key_inject_mode)) { return false; } @@ -239,7 +239,7 @@ sc_key_processor_process_key(struct sc_key_processor *kp, } struct control_msg msg; - if (convert_input_key(event, &msg, ki->prefer_text, ki->repeat)) { + if (convert_input_key(event, &msg, ki->key_inject_mode, ki->repeat)) { if (!controller_push_msg(ki->controller, &msg)) { LOGW("Could not request 'inject keycode'"); } @@ -251,11 +251,11 @@ sc_key_processor_process_text(struct sc_key_processor *kp, const SDL_TextInputEvent *event) { struct sc_keyboard_inject *ki = DOWNCAST(kp); - if (!ki->prefer_text) { + if (ki->key_inject_mode == SC_KEY_INJECT_MODE_MIXED) { char c = event->text[0]; if (isalpha(c) || c == ' ') { assert(event->text[1] == '\0'); - // letters and space are handled as raw key event + // Letters and space are handled as raw key events return; } } @@ -278,7 +278,7 @@ sc_keyboard_inject_init(struct sc_keyboard_inject *ki, struct controller *controller, const struct scrcpy_options *options) { ki->controller = controller; - ki->prefer_text = options->prefer_text; + ki->key_inject_mode = options->key_inject_mode; ki->forward_key_repeat = options->forward_key_repeat; ki->repeat = 0; diff --git a/app/src/keyboard_inject.h b/app/src/keyboard_inject.h index f4ebe40e..edd5b1ba 100644 --- a/app/src/keyboard_inject.h +++ b/app/src/keyboard_inject.h @@ -18,7 +18,7 @@ struct sc_keyboard_inject { // number of repetitions. This variable keeps track of the count. unsigned repeat; - bool prefer_text; + enum sc_key_inject_mode key_inject_mode; bool forward_key_repeat; }; diff --git a/app/src/options.c b/app/src/options.c index a14bda9a..4860fa07 100644 --- a/app/src/options.c +++ b/app/src/options.c @@ -43,7 +43,7 @@ const struct scrcpy_options scrcpy_options_default = { .control = true, .display = true, .turn_screen_off = false, - .prefer_text = false, + .key_inject_mode = SC_KEY_INJECT_MODE_MIXED, .window_borderless = false, .mipmaps = true, .stay_awake = false, diff --git a/app/src/options.h b/app/src/options.h index f183bd73..84a80fbe 100644 --- a/app/src/options.h +++ b/app/src/options.h @@ -38,6 +38,17 @@ enum sc_keyboard_input_mode { SC_KEYBOARD_INPUT_MODE_HID, }; +enum sc_key_inject_mode { + // Inject special keys, letters and space as key events. + // Inject numbers and punctuation as text events. + // This is the default mode. + SC_KEY_INJECT_MODE_MIXED, + + // Inject special keys as key events. + // Inject letters and space, numbers and punctuation as text events. + SC_KEY_INJECT_MODE_TEXT, +}; + #define SC_MAX_SHORTCUT_MODS 8 enum sc_shortcut_mod { @@ -98,7 +109,7 @@ struct scrcpy_options { bool control; bool display; bool turn_screen_off; - bool prefer_text; + enum sc_key_inject_mode key_inject_mode; bool window_borderless; bool mipmaps; bool stay_awake; diff --git a/app/tests/test_cli.c b/app/tests/test_cli.c index 05bacbf8..5bc1cc07 100644 --- a/app/tests/test_cli.c +++ b/app/tests/test_cli.c @@ -89,7 +89,7 @@ static void test_options(void) { assert(!strcmp(opts->serial, "0123456789abcdef")); assert(opts->show_touches); assert(opts->turn_screen_off); - assert(opts->prefer_text); + assert(opts->key_inject_mode == SC_KEY_INJECT_MODE_TEXT); assert(!strcmp(opts->window_title, "my device")); assert(opts->window_x == 100); assert(opts->window_y == -1);