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 <https://github.com/Genymobile/scrcpy/pull/2831>
rawkeyevents_2021
Romain Vimont 3 years ago
parent 0c0f62e4ab
commit 5e918ac0c3

@ -1350,7 +1350,7 @@ parse_args_with_getopt(struct scrcpy_cli_args *args, int argc, char *argv[],
opts->push_target = optarg; opts->push_target = optarg;
break; break;
case OPT_PREFER_TEXT: case OPT_PREFER_TEXT:
opts->prefer_text = true; opts->key_inject_mode = SC_KEY_INJECT_MODE_TEXT;
break; break;
case OPT_ROTATION: case OPT_ROTATION:
if (!parse_rotation(optarg, &opts->rotation)) { if (!parse_rotation(optarg, &opts->rotation)) {

@ -30,7 +30,7 @@ convert_keycode_action(SDL_EventType from, enum android_keyevent_action *to) {
static bool static bool
convert_keycode(SDL_Keycode from, enum android_keycode *to, uint16_t mod, 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. // Navigation keys and ENTER.
// Used in all modes. // Used in all modes.
static const struct sc_intmap_entry special_keys[] = { 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) // do not forward alpha and space key events (unless Ctrl is pressed)
return false; return false;
} }
@ -199,7 +199,7 @@ convert_meta_state(SDL_Keymod mod) {
static bool static bool
convert_input_key(const SDL_KeyboardEvent *from, struct control_msg *to, 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; to->type = CONTROL_MSG_TYPE_INJECT_KEYCODE;
if (!convert_keycode_action(from->type, &to->inject_keycode.action)) { 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; uint16_t mod = from->keysym.mod;
if (!convert_keycode(from->keysym.sym, &to->inject_keycode.keycode, mod, if (!convert_keycode(from->keysym.sym, &to->inject_keycode.keycode, mod,
prefer_text)) { key_inject_mode)) {
return false; return false;
} }
@ -239,7 +239,7 @@ sc_key_processor_process_key(struct sc_key_processor *kp,
} }
struct control_msg msg; 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)) { if (!controller_push_msg(ki->controller, &msg)) {
LOGW("Could not request 'inject keycode'"); LOGW("Could not request 'inject keycode'");
} }
@ -251,11 +251,11 @@ sc_key_processor_process_text(struct sc_key_processor *kp,
const SDL_TextInputEvent *event) { const SDL_TextInputEvent *event) {
struct sc_keyboard_inject *ki = DOWNCAST(kp); 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]; char c = event->text[0];
if (isalpha(c) || c == ' ') { if (isalpha(c) || c == ' ') {
assert(event->text[1] == '\0'); assert(event->text[1] == '\0');
// letters and space are handled as raw key event // Letters and space are handled as raw key events
return; return;
} }
} }
@ -278,7 +278,7 @@ sc_keyboard_inject_init(struct sc_keyboard_inject *ki,
struct controller *controller, struct controller *controller,
const struct scrcpy_options *options) { const struct scrcpy_options *options) {
ki->controller = controller; 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->forward_key_repeat = options->forward_key_repeat;
ki->repeat = 0; ki->repeat = 0;

@ -18,7 +18,7 @@ struct sc_keyboard_inject {
// number of repetitions. This variable keeps track of the count. // number of repetitions. This variable keeps track of the count.
unsigned repeat; unsigned repeat;
bool prefer_text; enum sc_key_inject_mode key_inject_mode;
bool forward_key_repeat; bool forward_key_repeat;
}; };

@ -43,7 +43,7 @@ const struct scrcpy_options scrcpy_options_default = {
.control = true, .control = true,
.display = true, .display = true,
.turn_screen_off = false, .turn_screen_off = false,
.prefer_text = false, .key_inject_mode = SC_KEY_INJECT_MODE_MIXED,
.window_borderless = false, .window_borderless = false,
.mipmaps = true, .mipmaps = true,
.stay_awake = false, .stay_awake = false,

@ -38,6 +38,17 @@ enum sc_keyboard_input_mode {
SC_KEYBOARD_INPUT_MODE_HID, 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 #define SC_MAX_SHORTCUT_MODS 8
enum sc_shortcut_mod { enum sc_shortcut_mod {
@ -98,7 +109,7 @@ struct scrcpy_options {
bool control; bool control;
bool display; bool display;
bool turn_screen_off; bool turn_screen_off;
bool prefer_text; enum sc_key_inject_mode key_inject_mode;
bool window_borderless; bool window_borderless;
bool mipmaps; bool mipmaps;
bool stay_awake; bool stay_awake;

@ -89,7 +89,7 @@ static void test_options(void) {
assert(!strcmp(opts->serial, "0123456789abcdef")); assert(!strcmp(opts->serial, "0123456789abcdef"));
assert(opts->show_touches); assert(opts->show_touches);
assert(opts->turn_screen_off); 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(!strcmp(opts->window_title, "my device"));
assert(opts->window_x == 100); assert(opts->window_x == 100);
assert(opts->window_y == -1); assert(opts->window_y == -1);

Loading…
Cancel
Save