Use scrcpy input events for key processors
Pass scrcpy input events instead of SDL input events to key processors. This makes the source code of key processors independent of the SDL API.
This commit is contained in:
parent
e4396e34c2
commit
b4b638e8fe
4 changed files with 194 additions and 163 deletions
|
@ -1,8 +1,8 @@
|
||||||
#include "hid_keyboard.h"
|
#include "hid_keyboard.h"
|
||||||
|
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
#include <SDL2/SDL_events.h>
|
|
||||||
|
|
||||||
|
#include "input_events.h"
|
||||||
#include "util/log.h"
|
#include "util/log.h"
|
||||||
|
|
||||||
/** Downcast key processor to hid_keyboard */
|
/** Downcast key processor to hid_keyboard */
|
||||||
|
@ -201,30 +201,30 @@ static const unsigned char keyboard_report_desc[] = {
|
||||||
*/
|
*/
|
||||||
|
|
||||||
static unsigned char
|
static unsigned char
|
||||||
sdl_keymod_to_hid_modifiers(SDL_Keymod mod) {
|
sdl_keymod_to_hid_modifiers(uint16_t mod) {
|
||||||
unsigned char modifiers = HID_MODIFIER_NONE;
|
unsigned char modifiers = HID_MODIFIER_NONE;
|
||||||
if (mod & KMOD_LCTRL) {
|
if (mod & SC_MOD_LCTRL) {
|
||||||
modifiers |= HID_MODIFIER_LEFT_CONTROL;
|
modifiers |= HID_MODIFIER_LEFT_CONTROL;
|
||||||
}
|
}
|
||||||
if (mod & KMOD_LSHIFT) {
|
if (mod & SC_MOD_LSHIFT) {
|
||||||
modifiers |= HID_MODIFIER_LEFT_SHIFT;
|
modifiers |= HID_MODIFIER_LEFT_SHIFT;
|
||||||
}
|
}
|
||||||
if (mod & KMOD_LALT) {
|
if (mod & SC_MOD_LALT) {
|
||||||
modifiers |= HID_MODIFIER_LEFT_ALT;
|
modifiers |= HID_MODIFIER_LEFT_ALT;
|
||||||
}
|
}
|
||||||
if (mod & KMOD_LGUI) {
|
if (mod & SC_MOD_LGUI) {
|
||||||
modifiers |= HID_MODIFIER_LEFT_GUI;
|
modifiers |= HID_MODIFIER_LEFT_GUI;
|
||||||
}
|
}
|
||||||
if (mod & KMOD_RCTRL) {
|
if (mod & SC_MOD_RCTRL) {
|
||||||
modifiers |= HID_MODIFIER_RIGHT_CONTROL;
|
modifiers |= HID_MODIFIER_RIGHT_CONTROL;
|
||||||
}
|
}
|
||||||
if (mod & KMOD_RSHIFT) {
|
if (mod & SC_MOD_RSHIFT) {
|
||||||
modifiers |= HID_MODIFIER_RIGHT_SHIFT;
|
modifiers |= HID_MODIFIER_RIGHT_SHIFT;
|
||||||
}
|
}
|
||||||
if (mod & KMOD_RALT) {
|
if (mod & SC_MOD_RALT) {
|
||||||
modifiers |= HID_MODIFIER_RIGHT_ALT;
|
modifiers |= HID_MODIFIER_RIGHT_ALT;
|
||||||
}
|
}
|
||||||
if (mod & KMOD_RGUI) {
|
if (mod & SC_MOD_RGUI) {
|
||||||
modifiers |= HID_MODIFIER_RIGHT_GUI;
|
modifiers |= HID_MODIFIER_RIGHT_GUI;
|
||||||
}
|
}
|
||||||
return modifiers;
|
return modifiers;
|
||||||
|
@ -248,15 +248,15 @@ sc_hid_keyboard_event_init(struct sc_hid_event *hid_event) {
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline bool
|
static inline bool
|
||||||
scancode_is_modifier(SDL_Scancode scancode) {
|
scancode_is_modifier(enum sc_scancode scancode) {
|
||||||
return scancode >= SDL_SCANCODE_LCTRL && scancode <= SDL_SCANCODE_RGUI;
|
return scancode >= SC_SCANCODE_LCTRL && scancode <= SC_SCANCODE_RGUI;
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool
|
static bool
|
||||||
convert_hid_keyboard_event(struct sc_hid_keyboard *kb,
|
convert_hid_keyboard_event(struct sc_hid_keyboard *kb,
|
||||||
struct sc_hid_event *hid_event,
|
struct sc_hid_event *hid_event,
|
||||||
const SDL_KeyboardEvent *event) {
|
const struct sc_key_event *event) {
|
||||||
SDL_Scancode scancode = event->keysym.scancode;
|
enum sc_scancode scancode = event->scancode;
|
||||||
assert(scancode >= 0);
|
assert(scancode >= 0);
|
||||||
|
|
||||||
// SDL also generates events when only modifiers are pressed, we cannot
|
// SDL also generates events when only modifiers are pressed, we cannot
|
||||||
|
@ -272,11 +272,11 @@ convert_hid_keyboard_event(struct sc_hid_keyboard *kb,
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned char modifiers = sdl_keymod_to_hid_modifiers(event->keysym.mod);
|
unsigned char modifiers = sdl_keymod_to_hid_modifiers(event->mods_state);
|
||||||
|
|
||||||
if (scancode < SC_HID_KEYBOARD_KEYS) {
|
if (scancode < SC_HID_KEYBOARD_KEYS) {
|
||||||
// Pressed is true and released is false
|
// Pressed is true and released is false
|
||||||
kb->keys[scancode] = (event->type == SDL_KEYDOWN);
|
kb->keys[scancode] = (event->action == SC_ACTION_DOWN);
|
||||||
LOGV("keys[%02x] = %s", scancode,
|
LOGV("keys[%02x] = %s", scancode,
|
||||||
kb->keys[scancode] ? "true" : "false");
|
kb->keys[scancode] ? "true" : "false");
|
||||||
}
|
}
|
||||||
|
@ -306,17 +306,17 @@ convert_hid_keyboard_event(struct sc_hid_keyboard *kb,
|
||||||
|
|
||||||
end:
|
end:
|
||||||
LOGV("hid keyboard: key %-4s scancode=%02x (%u) mod=%02x",
|
LOGV("hid keyboard: key %-4s scancode=%02x (%u) mod=%02x",
|
||||||
event->type == SDL_KEYDOWN ? "down" : "up", event->keysym.scancode,
|
event->action == SC_ACTION_DOWN ? "down" : "up", event->scancode,
|
||||||
event->keysym.scancode, modifiers);
|
event->scancode, modifiers);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static bool
|
static bool
|
||||||
push_mod_lock_state(struct sc_hid_keyboard *kb, uint16_t sdl_mod) {
|
push_mod_lock_state(struct sc_hid_keyboard *kb, uint16_t mods_state) {
|
||||||
bool capslock = sdl_mod & KMOD_CAPS;
|
bool capslock = mods_state & SC_MOD_CAPS;
|
||||||
bool numlock = sdl_mod & KMOD_NUM;
|
bool numlock = mods_state & SC_MOD_NUM;
|
||||||
if (!capslock && !numlock) {
|
if (!capslock && !numlock) {
|
||||||
// Nothing to do
|
// Nothing to do
|
||||||
return true;
|
return true;
|
||||||
|
@ -328,8 +328,6 @@ push_mod_lock_state(struct sc_hid_keyboard *kb, uint16_t sdl_mod) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
#define SC_SCANCODE_CAPSLOCK SDL_SCANCODE_CAPSLOCK
|
|
||||||
#define SC_SCANCODE_NUMLOCK SDL_SCANCODE_NUMLOCKCLEAR
|
|
||||||
unsigned i = 0;
|
unsigned i = 0;
|
||||||
if (capslock) {
|
if (capslock) {
|
||||||
hid_event.buffer[HID_KEYBOARD_INDEX_KEYS + i] = SC_SCANCODE_CAPSLOCK;
|
hid_event.buffer[HID_KEYBOARD_INDEX_KEYS + i] = SC_SCANCODE_CAPSLOCK;
|
||||||
|
@ -353,7 +351,7 @@ push_mod_lock_state(struct sc_hid_keyboard *kb, uint16_t sdl_mod) {
|
||||||
|
|
||||||
static void
|
static void
|
||||||
sc_key_processor_process_key(struct sc_key_processor *kp,
|
sc_key_processor_process_key(struct sc_key_processor *kp,
|
||||||
const SDL_KeyboardEvent *event,
|
const struct sc_key_event *event,
|
||||||
uint64_t ack_to_wait) {
|
uint64_t ack_to_wait) {
|
||||||
if (event->repeat) {
|
if (event->repeat) {
|
||||||
// In USB HID protocol, key repeat is handled by the host (Android), so
|
// In USB HID protocol, key repeat is handled by the host (Android), so
|
||||||
|
@ -369,7 +367,7 @@ sc_key_processor_process_key(struct sc_key_processor *kp,
|
||||||
if (!kb->mod_lock_synchronized) {
|
if (!kb->mod_lock_synchronized) {
|
||||||
// Inject CAPSLOCK and/or NUMLOCK if necessary to synchronize
|
// Inject CAPSLOCK and/or NUMLOCK if necessary to synchronize
|
||||||
// keyboard state
|
// keyboard state
|
||||||
if (push_mod_lock_state(kb, event->keysym.mod)) {
|
if (push_mod_lock_state(kb, event->mods_state)) {
|
||||||
kb->mod_lock_synchronized = true;
|
kb->mod_lock_synchronized = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -391,7 +389,7 @@ sc_key_processor_process_key(struct sc_key_processor *kp,
|
||||||
|
|
||||||
static void
|
static void
|
||||||
sc_key_processor_process_text(struct sc_key_processor *kp,
|
sc_key_processor_process_text(struct sc_key_processor *kp,
|
||||||
const SDL_TextInputEvent *event) {
|
const struct sc_text_event *event) {
|
||||||
(void) kp;
|
(void) kp;
|
||||||
(void) event;
|
(void) event;
|
||||||
|
|
||||||
|
|
|
@ -6,6 +6,30 @@
|
||||||
#include "input_events.h"
|
#include "input_events.h"
|
||||||
#include "util/log.h"
|
#include "util/log.h"
|
||||||
|
|
||||||
|
static inline uint16_t
|
||||||
|
sc_mods_state_from_sdl(uint16_t mods_state) {
|
||||||
|
return mods_state;
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline enum sc_keycode
|
||||||
|
sc_keycode_from_sdl(SDL_Keycode keycode) {
|
||||||
|
return (enum sc_keycode) keycode;
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline enum sc_scancode
|
||||||
|
sc_scancode_from_sdl(SDL_Scancode scancode) {
|
||||||
|
return (enum sc_scancode) scancode;
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline enum sc_action
|
||||||
|
sc_action_from_sdl_keyboard_type(uint32_t type) {
|
||||||
|
assert(type == SDL_KEYDOWN || type == SDL_KEYUP);
|
||||||
|
if (type == SDL_KEYDOWN) {
|
||||||
|
return SC_ACTION_DOWN;
|
||||||
|
}
|
||||||
|
return SC_ACTION_UP;
|
||||||
|
}
|
||||||
|
|
||||||
#define SC_SDL_SHORTCUT_MODS_MASK (KMOD_CTRL | KMOD_ALT | KMOD_GUI)
|
#define SC_SDL_SHORTCUT_MODS_MASK (KMOD_CTRL | KMOD_ALT | KMOD_GUI)
|
||||||
|
|
||||||
static inline uint16_t
|
static inline uint16_t
|
||||||
|
@ -317,7 +341,11 @@ input_manager_process_text_input(struct input_manager *im,
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
im->kp->ops->process_text(im->kp, event);
|
struct sc_text_event evt = {
|
||||||
|
.text = event->text,
|
||||||
|
};
|
||||||
|
|
||||||
|
im->kp->ops->process_text(im->kp, &evt);
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool
|
static bool
|
||||||
|
@ -536,7 +564,15 @@ input_manager_process_key(struct input_manager *im,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
im->kp->ops->process_key(im->kp, event, ack_to_wait);
|
struct sc_key_event evt = {
|
||||||
|
.action = sc_action_from_sdl_keyboard_type(event->type),
|
||||||
|
.keycode = sc_keycode_from_sdl(event->keysym.sym),
|
||||||
|
.scancode = sc_scancode_from_sdl(event->keysym.scancode),
|
||||||
|
.repeat = event->repeat,
|
||||||
|
.mods_state = sc_mods_state_from_sdl(event->keysym.mod),
|
||||||
|
};
|
||||||
|
|
||||||
|
im->kp->ops->process_key(im->kp, &evt, ack_to_wait);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
|
|
|
@ -1,11 +1,11 @@
|
||||||
#include "keyboard_inject.h"
|
#include "keyboard_inject.h"
|
||||||
|
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
#include <SDL2/SDL_events.h>
|
|
||||||
|
|
||||||
#include "android/input.h"
|
#include "android/input.h"
|
||||||
#include "control_msg.h"
|
#include "control_msg.h"
|
||||||
#include "controller.h"
|
#include "controller.h"
|
||||||
|
#include "input_events.h"
|
||||||
#include "util/intmap.h"
|
#include "util/intmap.h"
|
||||||
#include "util/log.h"
|
#include "util/log.h"
|
||||||
|
|
||||||
|
@ -13,10 +13,10 @@
|
||||||
#define DOWNCAST(KP) container_of(KP, struct sc_keyboard_inject, key_processor)
|
#define DOWNCAST(KP) container_of(KP, struct sc_keyboard_inject, key_processor)
|
||||||
|
|
||||||
static bool
|
static bool
|
||||||
convert_keycode_action(SDL_EventType from, enum android_keyevent_action *to) {
|
convert_keycode_action(enum sc_action from, enum android_keyevent_action *to) {
|
||||||
static const struct sc_intmap_entry actions[] = {
|
static const struct sc_intmap_entry actions[] = {
|
||||||
{SDL_KEYDOWN, AKEY_EVENT_ACTION_DOWN},
|
{SC_ACTION_DOWN, AKEY_EVENT_ACTION_DOWN},
|
||||||
{SDL_KEYUP, AKEY_EVENT_ACTION_UP},
|
{SC_ACTION_UP, AKEY_EVENT_ACTION_UP},
|
||||||
};
|
};
|
||||||
|
|
||||||
const struct sc_intmap_entry *entry = SC_INTMAP_FIND_ENTRY(actions, from);
|
const struct sc_intmap_entry *entry = SC_INTMAP_FIND_ENTRY(actions, from);
|
||||||
|
@ -29,125 +29,125 @@ 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(enum sc_keycode from, enum android_keycode *to, uint16_t mod,
|
||||||
enum sc_key_inject_mode key_inject_mode) {
|
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[] = {
|
||||||
{SDLK_RETURN, AKEYCODE_ENTER},
|
{SC_KEYCODE_RETURN, AKEYCODE_ENTER},
|
||||||
{SDLK_KP_ENTER, AKEYCODE_NUMPAD_ENTER},
|
{SC_KEYCODE_KP_ENTER, AKEYCODE_NUMPAD_ENTER},
|
||||||
{SDLK_ESCAPE, AKEYCODE_ESCAPE},
|
{SC_KEYCODE_ESCAPE, AKEYCODE_ESCAPE},
|
||||||
{SDLK_BACKSPACE, AKEYCODE_DEL},
|
{SC_KEYCODE_BACKSPACE, AKEYCODE_DEL},
|
||||||
{SDLK_TAB, AKEYCODE_TAB},
|
{SC_KEYCODE_TAB, AKEYCODE_TAB},
|
||||||
{SDLK_PAGEUP, AKEYCODE_PAGE_UP},
|
{SC_KEYCODE_PAGEUP, AKEYCODE_PAGE_UP},
|
||||||
{SDLK_DELETE, AKEYCODE_FORWARD_DEL},
|
{SC_KEYCODE_DELETE, AKEYCODE_FORWARD_DEL},
|
||||||
{SDLK_HOME, AKEYCODE_MOVE_HOME},
|
{SC_KEYCODE_HOME, AKEYCODE_MOVE_HOME},
|
||||||
{SDLK_END, AKEYCODE_MOVE_END},
|
{SC_KEYCODE_END, AKEYCODE_MOVE_END},
|
||||||
{SDLK_PAGEDOWN, AKEYCODE_PAGE_DOWN},
|
{SC_KEYCODE_PAGEDOWN, AKEYCODE_PAGE_DOWN},
|
||||||
{SDLK_RIGHT, AKEYCODE_DPAD_RIGHT},
|
{SC_KEYCODE_RIGHT, AKEYCODE_DPAD_RIGHT},
|
||||||
{SDLK_LEFT, AKEYCODE_DPAD_LEFT},
|
{SC_KEYCODE_LEFT, AKEYCODE_DPAD_LEFT},
|
||||||
{SDLK_DOWN, AKEYCODE_DPAD_DOWN},
|
{SC_KEYCODE_DOWN, AKEYCODE_DPAD_DOWN},
|
||||||
{SDLK_UP, AKEYCODE_DPAD_UP},
|
{SC_KEYCODE_UP, AKEYCODE_DPAD_UP},
|
||||||
{SDLK_LCTRL, AKEYCODE_CTRL_LEFT},
|
{SC_KEYCODE_LCTRL, AKEYCODE_CTRL_LEFT},
|
||||||
{SDLK_RCTRL, AKEYCODE_CTRL_RIGHT},
|
{SC_KEYCODE_RCTRL, AKEYCODE_CTRL_RIGHT},
|
||||||
{SDLK_LSHIFT, AKEYCODE_SHIFT_LEFT},
|
{SC_KEYCODE_LSHIFT, AKEYCODE_SHIFT_LEFT},
|
||||||
{SDLK_RSHIFT, AKEYCODE_SHIFT_RIGHT},
|
{SC_KEYCODE_RSHIFT, AKEYCODE_SHIFT_RIGHT},
|
||||||
};
|
};
|
||||||
|
|
||||||
// Numpad navigation keys.
|
// Numpad navigation keys.
|
||||||
// Used in all modes, when NumLock and Shift are disabled.
|
// Used in all modes, when NumLock and Shift are disabled.
|
||||||
static const struct sc_intmap_entry kp_nav_keys[] = {
|
static const struct sc_intmap_entry kp_nav_keys[] = {
|
||||||
{SDLK_KP_0, AKEYCODE_INSERT},
|
{SC_KEYCODE_KP_0, AKEYCODE_INSERT},
|
||||||
{SDLK_KP_1, AKEYCODE_MOVE_END},
|
{SC_KEYCODE_KP_1, AKEYCODE_MOVE_END},
|
||||||
{SDLK_KP_2, AKEYCODE_DPAD_DOWN},
|
{SC_KEYCODE_KP_2, AKEYCODE_DPAD_DOWN},
|
||||||
{SDLK_KP_3, AKEYCODE_PAGE_DOWN},
|
{SC_KEYCODE_KP_3, AKEYCODE_PAGE_DOWN},
|
||||||
{SDLK_KP_4, AKEYCODE_DPAD_LEFT},
|
{SC_KEYCODE_KP_4, AKEYCODE_DPAD_LEFT},
|
||||||
{SDLK_KP_6, AKEYCODE_DPAD_RIGHT},
|
{SC_KEYCODE_KP_6, AKEYCODE_DPAD_RIGHT},
|
||||||
{SDLK_KP_7, AKEYCODE_MOVE_HOME},
|
{SC_KEYCODE_KP_7, AKEYCODE_MOVE_HOME},
|
||||||
{SDLK_KP_8, AKEYCODE_DPAD_UP},
|
{SC_KEYCODE_KP_8, AKEYCODE_DPAD_UP},
|
||||||
{SDLK_KP_9, AKEYCODE_PAGE_UP},
|
{SC_KEYCODE_KP_9, AKEYCODE_PAGE_UP},
|
||||||
{SDLK_KP_PERIOD, AKEYCODE_FORWARD_DEL},
|
{SC_KEYCODE_KP_PERIOD, AKEYCODE_FORWARD_DEL},
|
||||||
};
|
};
|
||||||
|
|
||||||
// Letters and space.
|
// Letters and space.
|
||||||
// Used in non-text mode.
|
// Used in non-text mode.
|
||||||
static const struct sc_intmap_entry alphaspace_keys[] = {
|
static const struct sc_intmap_entry alphaspace_keys[] = {
|
||||||
{SDLK_a, AKEYCODE_A},
|
{SC_KEYCODE_a, AKEYCODE_A},
|
||||||
{SDLK_b, AKEYCODE_B},
|
{SC_KEYCODE_b, AKEYCODE_B},
|
||||||
{SDLK_c, AKEYCODE_C},
|
{SC_KEYCODE_c, AKEYCODE_C},
|
||||||
{SDLK_d, AKEYCODE_D},
|
{SC_KEYCODE_d, AKEYCODE_D},
|
||||||
{SDLK_e, AKEYCODE_E},
|
{SC_KEYCODE_e, AKEYCODE_E},
|
||||||
{SDLK_f, AKEYCODE_F},
|
{SC_KEYCODE_f, AKEYCODE_F},
|
||||||
{SDLK_g, AKEYCODE_G},
|
{SC_KEYCODE_g, AKEYCODE_G},
|
||||||
{SDLK_h, AKEYCODE_H},
|
{SC_KEYCODE_h, AKEYCODE_H},
|
||||||
{SDLK_i, AKEYCODE_I},
|
{SC_KEYCODE_i, AKEYCODE_I},
|
||||||
{SDLK_j, AKEYCODE_J},
|
{SC_KEYCODE_j, AKEYCODE_J},
|
||||||
{SDLK_k, AKEYCODE_K},
|
{SC_KEYCODE_k, AKEYCODE_K},
|
||||||
{SDLK_l, AKEYCODE_L},
|
{SC_KEYCODE_l, AKEYCODE_L},
|
||||||
{SDLK_m, AKEYCODE_M},
|
{SC_KEYCODE_m, AKEYCODE_M},
|
||||||
{SDLK_n, AKEYCODE_N},
|
{SC_KEYCODE_n, AKEYCODE_N},
|
||||||
{SDLK_o, AKEYCODE_O},
|
{SC_KEYCODE_o, AKEYCODE_O},
|
||||||
{SDLK_p, AKEYCODE_P},
|
{SC_KEYCODE_p, AKEYCODE_P},
|
||||||
{SDLK_q, AKEYCODE_Q},
|
{SC_KEYCODE_q, AKEYCODE_Q},
|
||||||
{SDLK_r, AKEYCODE_R},
|
{SC_KEYCODE_r, AKEYCODE_R},
|
||||||
{SDLK_s, AKEYCODE_S},
|
{SC_KEYCODE_s, AKEYCODE_S},
|
||||||
{SDLK_t, AKEYCODE_T},
|
{SC_KEYCODE_t, AKEYCODE_T},
|
||||||
{SDLK_u, AKEYCODE_U},
|
{SC_KEYCODE_u, AKEYCODE_U},
|
||||||
{SDLK_v, AKEYCODE_V},
|
{SC_KEYCODE_v, AKEYCODE_V},
|
||||||
{SDLK_w, AKEYCODE_W},
|
{SC_KEYCODE_w, AKEYCODE_W},
|
||||||
{SDLK_x, AKEYCODE_X},
|
{SC_KEYCODE_x, AKEYCODE_X},
|
||||||
{SDLK_y, AKEYCODE_Y},
|
{SC_KEYCODE_y, AKEYCODE_Y},
|
||||||
{SDLK_z, AKEYCODE_Z},
|
{SC_KEYCODE_z, AKEYCODE_Z},
|
||||||
{SDLK_SPACE, AKEYCODE_SPACE},
|
{SC_KEYCODE_SPACE, AKEYCODE_SPACE},
|
||||||
};
|
};
|
||||||
|
|
||||||
// Numbers and punctuation keys.
|
// Numbers and punctuation keys.
|
||||||
// Used in raw mode only.
|
// Used in raw mode only.
|
||||||
static const struct sc_intmap_entry numbers_punct_keys[] = {
|
static const struct sc_intmap_entry numbers_punct_keys[] = {
|
||||||
{SDLK_HASH, AKEYCODE_POUND},
|
{SC_KEYCODE_HASH, AKEYCODE_POUND},
|
||||||
{SDLK_PERCENT, AKEYCODE_PERIOD},
|
{SC_KEYCODE_PERCENT, AKEYCODE_PERIOD},
|
||||||
{SDLK_QUOTE, AKEYCODE_APOSTROPHE},
|
{SC_KEYCODE_QUOTE, AKEYCODE_APOSTROPHE},
|
||||||
{SDLK_ASTERISK, AKEYCODE_STAR},
|
{SC_KEYCODE_ASTERISK, AKEYCODE_STAR},
|
||||||
{SDLK_PLUS, AKEYCODE_PLUS},
|
{SC_KEYCODE_PLUS, AKEYCODE_PLUS},
|
||||||
{SDLK_COMMA, AKEYCODE_COMMA},
|
{SC_KEYCODE_COMMA, AKEYCODE_COMMA},
|
||||||
{SDLK_MINUS, AKEYCODE_MINUS},
|
{SC_KEYCODE_MINUS, AKEYCODE_MINUS},
|
||||||
{SDLK_PERIOD, AKEYCODE_PERIOD},
|
{SC_KEYCODE_PERIOD, AKEYCODE_PERIOD},
|
||||||
{SDLK_SLASH, AKEYCODE_SLASH},
|
{SC_KEYCODE_SLASH, AKEYCODE_SLASH},
|
||||||
{SDLK_0, AKEYCODE_0},
|
{SC_KEYCODE_0, AKEYCODE_0},
|
||||||
{SDLK_1, AKEYCODE_1},
|
{SC_KEYCODE_1, AKEYCODE_1},
|
||||||
{SDLK_2, AKEYCODE_2},
|
{SC_KEYCODE_2, AKEYCODE_2},
|
||||||
{SDLK_3, AKEYCODE_3},
|
{SC_KEYCODE_3, AKEYCODE_3},
|
||||||
{SDLK_4, AKEYCODE_4},
|
{SC_KEYCODE_4, AKEYCODE_4},
|
||||||
{SDLK_5, AKEYCODE_5},
|
{SC_KEYCODE_5, AKEYCODE_5},
|
||||||
{SDLK_6, AKEYCODE_6},
|
{SC_KEYCODE_6, AKEYCODE_6},
|
||||||
{SDLK_7, AKEYCODE_7},
|
{SC_KEYCODE_7, AKEYCODE_7},
|
||||||
{SDLK_8, AKEYCODE_8},
|
{SC_KEYCODE_8, AKEYCODE_8},
|
||||||
{SDLK_9, AKEYCODE_9},
|
{SC_KEYCODE_9, AKEYCODE_9},
|
||||||
{SDLK_SEMICOLON, AKEYCODE_SEMICOLON},
|
{SC_KEYCODE_SEMICOLON, AKEYCODE_SEMICOLON},
|
||||||
{SDLK_EQUALS, AKEYCODE_EQUALS},
|
{SC_KEYCODE_EQUALS, AKEYCODE_EQUALS},
|
||||||
{SDLK_AT, AKEYCODE_AT},
|
{SC_KEYCODE_AT, AKEYCODE_AT},
|
||||||
{SDLK_LEFTBRACKET, AKEYCODE_LEFT_BRACKET},
|
{SC_KEYCODE_LEFTBRACKET, AKEYCODE_LEFT_BRACKET},
|
||||||
{SDLK_BACKSLASH, AKEYCODE_BACKSLASH},
|
{SC_KEYCODE_BACKSLASH, AKEYCODE_BACKSLASH},
|
||||||
{SDLK_RIGHTBRACKET, AKEYCODE_RIGHT_BRACKET},
|
{SC_KEYCODE_RIGHTBRACKET, AKEYCODE_RIGHT_BRACKET},
|
||||||
{SDLK_BACKQUOTE, AKEYCODE_GRAVE},
|
{SC_KEYCODE_BACKQUOTE, AKEYCODE_GRAVE},
|
||||||
{SDLK_KP_1, AKEYCODE_NUMPAD_1},
|
{SC_KEYCODE_KP_1, AKEYCODE_NUMPAD_1},
|
||||||
{SDLK_KP_2, AKEYCODE_NUMPAD_2},
|
{SC_KEYCODE_KP_2, AKEYCODE_NUMPAD_2},
|
||||||
{SDLK_KP_3, AKEYCODE_NUMPAD_3},
|
{SC_KEYCODE_KP_3, AKEYCODE_NUMPAD_3},
|
||||||
{SDLK_KP_4, AKEYCODE_NUMPAD_4},
|
{SC_KEYCODE_KP_4, AKEYCODE_NUMPAD_4},
|
||||||
{SDLK_KP_5, AKEYCODE_NUMPAD_5},
|
{SC_KEYCODE_KP_5, AKEYCODE_NUMPAD_5},
|
||||||
{SDLK_KP_6, AKEYCODE_NUMPAD_6},
|
{SC_KEYCODE_KP_6, AKEYCODE_NUMPAD_6},
|
||||||
{SDLK_KP_7, AKEYCODE_NUMPAD_7},
|
{SC_KEYCODE_KP_7, AKEYCODE_NUMPAD_7},
|
||||||
{SDLK_KP_8, AKEYCODE_NUMPAD_8},
|
{SC_KEYCODE_KP_8, AKEYCODE_NUMPAD_8},
|
||||||
{SDLK_KP_9, AKEYCODE_NUMPAD_9},
|
{SC_KEYCODE_KP_9, AKEYCODE_NUMPAD_9},
|
||||||
{SDLK_KP_0, AKEYCODE_NUMPAD_0},
|
{SC_KEYCODE_KP_0, AKEYCODE_NUMPAD_0},
|
||||||
{SDLK_KP_DIVIDE, AKEYCODE_NUMPAD_DIVIDE},
|
{SC_KEYCODE_KP_DIVIDE, AKEYCODE_NUMPAD_DIVIDE},
|
||||||
{SDLK_KP_MULTIPLY, AKEYCODE_NUMPAD_MULTIPLY},
|
{SC_KEYCODE_KP_MULTIPLY, AKEYCODE_NUMPAD_MULTIPLY},
|
||||||
{SDLK_KP_MINUS, AKEYCODE_NUMPAD_SUBTRACT},
|
{SC_KEYCODE_KP_MINUS, AKEYCODE_NUMPAD_SUBTRACT},
|
||||||
{SDLK_KP_PLUS, AKEYCODE_NUMPAD_ADD},
|
{SC_KEYCODE_KP_PLUS, AKEYCODE_NUMPAD_ADD},
|
||||||
{SDLK_KP_PERIOD, AKEYCODE_NUMPAD_DOT},
|
{SC_KEYCODE_KP_PERIOD, AKEYCODE_NUMPAD_DOT},
|
||||||
{SDLK_KP_EQUALS, AKEYCODE_NUMPAD_EQUALS},
|
{SC_KEYCODE_KP_EQUALS, AKEYCODE_NUMPAD_EQUALS},
|
||||||
{SDLK_KP_LEFTPAREN, AKEYCODE_NUMPAD_LEFT_PAREN},
|
{SC_KEYCODE_KP_LEFTPAREN, AKEYCODE_NUMPAD_LEFT_PAREN},
|
||||||
{SDLK_KP_RIGHTPAREN, AKEYCODE_NUMPAD_RIGHT_PAREN},
|
{SC_KEYCODE_KP_RIGHTPAREN, AKEYCODE_NUMPAD_RIGHT_PAREN},
|
||||||
};
|
};
|
||||||
|
|
||||||
const struct sc_intmap_entry *entry =
|
const struct sc_intmap_entry *entry =
|
||||||
|
@ -157,7 +157,7 @@ convert_keycode(SDL_Keycode from, enum android_keycode *to, uint16_t mod,
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!(mod & (KMOD_NUM | KMOD_SHIFT))) {
|
if (!(mod & (SC_MOD_NUM | SC_MOD_LSHIFT | SC_MOD_RSHIFT))) {
|
||||||
// Handle Numpad events when Num Lock is disabled
|
// Handle Numpad events when Num Lock is disabled
|
||||||
// If SHIFT is pressed, a text event will be sent instead
|
// If SHIFT is pressed, a text event will be sent instead
|
||||||
entry = SC_INTMAP_FIND_ENTRY(kp_nav_keys, from);
|
entry = SC_INTMAP_FIND_ENTRY(kp_nav_keys, from);
|
||||||
|
@ -167,12 +167,13 @@ convert_keycode(SDL_Keycode from, enum android_keycode *to, uint16_t mod,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (key_inject_mode == SC_KEY_INJECT_MODE_TEXT && !(mod & KMOD_CTRL)) {
|
if (key_inject_mode == SC_KEY_INJECT_MODE_TEXT &&
|
||||||
|
!(mod & (SC_MOD_LCTRL | SC_MOD_RCTRL))) {
|
||||||
// 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;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (mod & (KMOD_LALT | KMOD_RALT | KMOD_LGUI | KMOD_RGUI)) {
|
if (mod & (SC_MOD_LALT | SC_MOD_RALT | SC_MOD_LGUI | SC_MOD_RGUI)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -214,70 +215,66 @@ autocomplete_metastate(enum android_metastate metastate) {
|
||||||
}
|
}
|
||||||
|
|
||||||
static enum android_metastate
|
static enum android_metastate
|
||||||
convert_meta_state(SDL_Keymod mod) {
|
convert_meta_state(uint16_t mod) {
|
||||||
enum android_metastate metastate = 0;
|
enum android_metastate metastate = 0;
|
||||||
if (mod & KMOD_LSHIFT) {
|
if (mod & SC_MOD_LSHIFT) {
|
||||||
metastate |= AMETA_SHIFT_LEFT_ON;
|
metastate |= AMETA_SHIFT_LEFT_ON;
|
||||||
}
|
}
|
||||||
if (mod & KMOD_RSHIFT) {
|
if (mod & SC_MOD_RSHIFT) {
|
||||||
metastate |= AMETA_SHIFT_RIGHT_ON;
|
metastate |= AMETA_SHIFT_RIGHT_ON;
|
||||||
}
|
}
|
||||||
if (mod & KMOD_LCTRL) {
|
if (mod & SC_MOD_LCTRL) {
|
||||||
metastate |= AMETA_CTRL_LEFT_ON;
|
metastate |= AMETA_CTRL_LEFT_ON;
|
||||||
}
|
}
|
||||||
if (mod & KMOD_RCTRL) {
|
if (mod & SC_MOD_RCTRL) {
|
||||||
metastate |= AMETA_CTRL_RIGHT_ON;
|
metastate |= AMETA_CTRL_RIGHT_ON;
|
||||||
}
|
}
|
||||||
if (mod & KMOD_LALT) {
|
if (mod & SC_MOD_LALT) {
|
||||||
metastate |= AMETA_ALT_LEFT_ON;
|
metastate |= AMETA_ALT_LEFT_ON;
|
||||||
}
|
}
|
||||||
if (mod & KMOD_RALT) {
|
if (mod & SC_MOD_RALT) {
|
||||||
metastate |= AMETA_ALT_RIGHT_ON;
|
metastate |= AMETA_ALT_RIGHT_ON;
|
||||||
}
|
}
|
||||||
if (mod & KMOD_LGUI) { // Windows key
|
if (mod & SC_MOD_LGUI) { // Windows key
|
||||||
metastate |= AMETA_META_LEFT_ON;
|
metastate |= AMETA_META_LEFT_ON;
|
||||||
}
|
}
|
||||||
if (mod & KMOD_RGUI) { // Windows key
|
if (mod & SC_MOD_RGUI) { // Windows key
|
||||||
metastate |= AMETA_META_RIGHT_ON;
|
metastate |= AMETA_META_RIGHT_ON;
|
||||||
}
|
}
|
||||||
if (mod & KMOD_NUM) {
|
if (mod & SC_MOD_NUM) {
|
||||||
metastate |= AMETA_NUM_LOCK_ON;
|
metastate |= AMETA_NUM_LOCK_ON;
|
||||||
}
|
}
|
||||||
if (mod & KMOD_CAPS) {
|
if (mod & SC_MOD_CAPS) {
|
||||||
metastate |= AMETA_CAPS_LOCK_ON;
|
metastate |= AMETA_CAPS_LOCK_ON;
|
||||||
}
|
}
|
||||||
if (mod & KMOD_MODE) { // Alt Gr
|
|
||||||
// no mapping?
|
|
||||||
}
|
|
||||||
|
|
||||||
// fill the dependent fields
|
// fill the dependent fields
|
||||||
return autocomplete_metastate(metastate);
|
return autocomplete_metastate(metastate);
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool
|
static bool
|
||||||
convert_input_key(const SDL_KeyboardEvent *from, struct control_msg *to,
|
convert_input_key(const struct sc_key_event *event, struct control_msg *msg,
|
||||||
enum sc_key_inject_mode key_inject_mode, uint32_t repeat) {
|
enum sc_key_inject_mode key_inject_mode, uint32_t repeat) {
|
||||||
to->type = CONTROL_MSG_TYPE_INJECT_KEYCODE;
|
msg->type = CONTROL_MSG_TYPE_INJECT_KEYCODE;
|
||||||
|
|
||||||
if (!convert_keycode_action(from->type, &to->inject_keycode.action)) {
|
if (!convert_keycode_action(event->action, &msg->inject_keycode.action)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint16_t mod = from->keysym.mod;
|
if (!convert_keycode(event->keycode, &msg->inject_keycode.keycode,
|
||||||
if (!convert_keycode(from->keysym.sym, &to->inject_keycode.keycode, mod,
|
event->mods_state, key_inject_mode)) {
|
||||||
key_inject_mode)) {
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
to->inject_keycode.repeat = repeat;
|
msg->inject_keycode.repeat = repeat;
|
||||||
to->inject_keycode.metastate = convert_meta_state(mod);
|
msg->inject_keycode.metastate = convert_meta_state(event->mods_state);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
sc_key_processor_process_key(struct sc_key_processor *kp,
|
sc_key_processor_process_key(struct sc_key_processor *kp,
|
||||||
const SDL_KeyboardEvent *event,
|
const struct sc_key_event *event,
|
||||||
uint64_t ack_to_wait) {
|
uint64_t ack_to_wait) {
|
||||||
// The device clipboard synchronization and the key event messages are
|
// The device clipboard synchronization and the key event messages are
|
||||||
// serialized, there is nothing special to do to ensure that the clipboard
|
// serialized, there is nothing special to do to ensure that the clipboard
|
||||||
|
@ -305,7 +302,7 @@ sc_key_processor_process_key(struct sc_key_processor *kp,
|
||||||
|
|
||||||
static void
|
static void
|
||||||
sc_key_processor_process_text(struct sc_key_processor *kp,
|
sc_key_processor_process_text(struct sc_key_processor *kp,
|
||||||
const SDL_TextInputEvent *event) {
|
const struct sc_text_event *event) {
|
||||||
struct sc_keyboard_inject *ki = DOWNCAST(kp);
|
struct sc_keyboard_inject *ki = DOWNCAST(kp);
|
||||||
|
|
||||||
if (ki->key_inject_mode == SC_KEY_INJECT_MODE_RAW) {
|
if (ki->key_inject_mode == SC_KEY_INJECT_MODE_RAW) {
|
||||||
|
|
|
@ -6,7 +6,7 @@
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
|
||||||
#include <SDL2/SDL_events.h>
|
#include "input_events.h"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Key processor trait.
|
* Key processor trait.
|
||||||
|
@ -37,12 +37,12 @@ struct sc_key_processor_ops {
|
||||||
* Ctrl+v on the device.
|
* Ctrl+v on the device.
|
||||||
*/
|
*/
|
||||||
void
|
void
|
||||||
(*process_key)(struct sc_key_processor *kp, const SDL_KeyboardEvent *event,
|
(*process_key)(struct sc_key_processor *kp,
|
||||||
uint64_t ack_to_wait);
|
const struct sc_key_event *event, uint64_t ack_to_wait);
|
||||||
|
|
||||||
void
|
void
|
||||||
(*process_text)(struct sc_key_processor *kp,
|
(*process_text)(struct sc_key_processor *kp,
|
||||||
const SDL_TextInputEvent *event);
|
const struct sc_text_event *event);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Reference in a new issue