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>
This commit is contained in:
Romain Vimont 2021-11-26 22:05:28 +01:00
parent 0c0f62e4ab
commit 5e918ac0c3
6 changed files with 24 additions and 13 deletions

View file

@ -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)) {

View file

@ -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;

View file

@ -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;
};

View file

@ -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,

View file

@ -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;

View file

@ -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);