Make some event conversions infallible

When the implementation handles all possible input values, it may never
fail.
This commit is contained in:
Romain Vimont 2021-12-29 16:55:01 +01:00
parent 9460bdd87b
commit a1f2f5fbd3
2 changed files with 42 additions and 82 deletions

View file

@ -12,20 +12,13 @@
/** Downcast key processor to sc_keyboard_inject */
#define DOWNCAST(KP) container_of(KP, struct sc_keyboard_inject, key_processor)
static bool
convert_keycode_action(enum sc_action from, enum android_keyevent_action *to) {
static const struct sc_intmap_entry actions[] = {
{SC_ACTION_DOWN, AKEY_EVENT_ACTION_DOWN},
{SC_ACTION_UP, AKEY_EVENT_ACTION_UP},
};
const struct sc_intmap_entry *entry = SC_INTMAP_FIND_ENTRY(actions, from);
if (entry) {
*to = entry->value;
return true;
static enum android_keyevent_action
convert_keycode_action(enum sc_action action) {
if (action == SC_ACTION_DOWN) {
return AKEY_EVENT_ACTION_DOWN;
}
return false;
assert(action == SC_ACTION_UP);
return AKEY_EVENT_ACTION_UP;
}
static bool
@ -257,15 +250,12 @@ convert_input_key(const struct sc_key_event *event, struct control_msg *msg,
enum sc_key_inject_mode key_inject_mode, uint32_t repeat) {
msg->type = CONTROL_MSG_TYPE_INJECT_KEYCODE;
if (!convert_keycode_action(event->action, &msg->inject_keycode.action)) {
return false;
}
if (!convert_keycode(event->keycode, &msg->inject_keycode.keycode,
event->mods_state, key_inject_mode)) {
return false;
}
msg->inject_keycode.action = convert_keycode_action(event->action);
msg->inject_keycode.repeat = repeat;
msg->inject_keycode.metastate = convert_meta_state(event->mods_state);

View file

@ -33,41 +33,29 @@ convert_mouse_buttons(uint32_t state) {
return buttons;
}
static bool
convert_mouse_action(enum sc_action from, enum android_motionevent_action *to) {
static const struct sc_intmap_entry actions[] = {
{SC_ACTION_DOWN, AMOTION_EVENT_ACTION_DOWN},
{SC_ACTION_UP, AMOTION_EVENT_ACTION_UP},
};
const struct sc_intmap_entry *entry = SC_INTMAP_FIND_ENTRY(actions, from);
if (entry) {
*to = entry->value;
return true;
static enum android_motionevent_action
convert_mouse_action(enum sc_action action) {
if (action == SC_ACTION_DOWN) {
return AMOTION_EVENT_ACTION_DOWN;
}
return false;
assert(action == SC_ACTION_UP);
return AMOTION_EVENT_ACTION_UP;
}
static bool
convert_touch_action(enum sc_touch_action from,
enum android_motionevent_action *to) {
static const struct sc_intmap_entry actions[] = {
{SC_TOUCH_ACTION_MOVE, AMOTION_EVENT_ACTION_MOVE},
{SC_TOUCH_ACTION_DOWN, AMOTION_EVENT_ACTION_DOWN},
{SC_TOUCH_ACTION_UP, AMOTION_EVENT_ACTION_UP},
};
const struct sc_intmap_entry *entry = SC_INTMAP_FIND_ENTRY(actions, from);
if (entry) {
*to = entry->value;
return true;
static enum android_motionevent_action
convert_touch_action(enum sc_touch_action action) {
switch (action) {
case SC_TOUCH_ACTION_MOVE:
return AMOTION_EVENT_ACTION_MOVE;
case SC_TOUCH_ACTION_DOWN:
return AMOTION_EVENT_ACTION_DOWN;
default:
assert(action == SC_TOUCH_ACTION_UP);
return AMOTION_EVENT_ACTION_UP;
}
return false;
}
static bool
static void
convert_mouse_motion(const struct sc_mouse_motion_event *event,
struct control_msg *msg) {
msg->type = CONTROL_MSG_TYPE_INJECT_TOUCH_EVENT;
@ -77,55 +65,39 @@ convert_mouse_motion(const struct sc_mouse_motion_event *event,
msg->inject_touch_event.pressure = 1.f;
msg->inject_touch_event.buttons =
convert_mouse_buttons(event->buttons_state);
return true;
}
static bool
static void
convert_touch(const struct sc_touch_event *event,
struct control_msg *msg) {
msg->type = CONTROL_MSG_TYPE_INJECT_TOUCH_EVENT;
if (!convert_touch_action(event->action, &msg->inject_touch_event.action)) {
return false;
}
msg->inject_touch_event.action = convert_touch_action(event->action);
msg->inject_touch_event.pointer_id = event->pointer_id;
msg->inject_touch_event.position = event->position;
msg->inject_touch_event.pressure = event->pressure;
msg->inject_touch_event.buttons = 0;
return true;
}
static bool
static void
convert_mouse_click(const struct sc_mouse_click_event *event,
struct control_msg *msg) {
msg->type = CONTROL_MSG_TYPE_INJECT_TOUCH_EVENT;
if (!convert_mouse_action(event->action, &msg->inject_touch_event.action)) {
return false;
}
msg->inject_touch_event.action = convert_mouse_action(event->action);
msg->inject_touch_event.pointer_id = POINTER_ID_MOUSE;
msg->inject_touch_event.position = event->position;
msg->inject_touch_event.pressure = event->action == SC_ACTION_DOWN
? 1.f : 0.f;
msg->inject_touch_event.buttons =
convert_mouse_buttons(event->buttons_state);
return true;
}
static bool
static void
convert_mouse_scroll(const struct sc_mouse_scroll_event *event,
struct control_msg *msg) {
msg->type = CONTROL_MSG_TYPE_INJECT_SCROLL_EVENT;
msg->inject_scroll_event.position = event->position;
msg->inject_scroll_event.hscroll = event->hscroll;
msg->inject_scroll_event.vscroll = event->vscroll;
return true;
}
static void
@ -134,9 +106,7 @@ sc_mouse_processor_process_mouse_motion(struct sc_mouse_processor *mp,
struct sc_mouse_inject *mi = DOWNCAST(mp);
struct control_msg msg;
if (!convert_mouse_motion(event, &msg)) {
return;
}
convert_mouse_motion(event, &msg);
if (!controller_push_msg(mi->controller, &msg)) {
LOGW("Could not request 'inject mouse motion event'");
@ -149,10 +119,10 @@ sc_mouse_processor_process_touch(struct sc_mouse_processor *mp,
struct sc_mouse_inject *mi = DOWNCAST(mp);
struct control_msg msg;
if (convert_touch(event, &msg)) {
if (!controller_push_msg(mi->controller, &msg)) {
LOGW("Could not request 'inject touch event'");
}
convert_touch(event, &msg);
if (!controller_push_msg(mi->controller, &msg)) {
LOGW("Could not request 'inject touch event'");
}
}
@ -162,10 +132,10 @@ sc_mouse_processor_process_mouse_click(struct sc_mouse_processor *mp,
struct sc_mouse_inject *mi = DOWNCAST(mp);
struct control_msg msg;
if (convert_mouse_click(event, &msg)) {
if (!controller_push_msg(mi->controller, &msg)) {
LOGW("Could not request 'inject mouse click event'");
}
convert_mouse_click(event, &msg);
if (!controller_push_msg(mi->controller, &msg)) {
LOGW("Could not request 'inject mouse click event'");
}
}
@ -175,10 +145,10 @@ sc_mouse_processor_process_mouse_scroll(struct sc_mouse_processor *mp,
struct sc_mouse_inject *mi = DOWNCAST(mp);
struct control_msg msg;
if (convert_mouse_scroll(event, &msg)) {
if (!controller_push_msg(mi->controller, &msg)) {
LOGW("Could not request 'inject mouse scroll event'");
}
convert_mouse_scroll(event, &msg);
if (!controller_push_msg(mi->controller, &msg)) {
LOGW("Could not request 'inject mouse scroll event'");
}
}