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 */ /** Downcast key processor to sc_keyboard_inject */
#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 enum android_keyevent_action
convert_keycode_action(enum sc_action from, enum android_keyevent_action *to) { convert_keycode_action(enum sc_action action) {
static const struct sc_intmap_entry actions[] = { if (action == SC_ACTION_DOWN) {
{SC_ACTION_DOWN, AKEY_EVENT_ACTION_DOWN}, return 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;
} }
assert(action == SC_ACTION_UP);
return false; return AKEY_EVENT_ACTION_UP;
} }
static bool 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) { enum sc_key_inject_mode key_inject_mode, uint32_t repeat) {
msg->type = CONTROL_MSG_TYPE_INJECT_KEYCODE; 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, if (!convert_keycode(event->keycode, &msg->inject_keycode.keycode,
event->mods_state, key_inject_mode)) { event->mods_state, key_inject_mode)) {
return false; return false;
} }
msg->inject_keycode.action = convert_keycode_action(event->action);
msg->inject_keycode.repeat = repeat; msg->inject_keycode.repeat = repeat;
msg->inject_keycode.metastate = convert_meta_state(event->mods_state); msg->inject_keycode.metastate = convert_meta_state(event->mods_state);

View file

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