2021-10-03 23:44:14 +08:00
|
|
|
#include "mouse_inject.h"
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
#include "android/input.h"
|
|
|
|
#include "control_msg.h"
|
|
|
|
#include "controller.h"
|
2021-12-29 23:24:20 +08:00
|
|
|
#include "input_events.h"
|
2021-11-27 04:58:56 +08:00
|
|
|
#include "util/intmap.h"
|
2021-10-03 23:44:14 +08:00
|
|
|
#include "util/log.h"
|
|
|
|
|
|
|
|
/** Downcast mouse processor to sc_mouse_inject */
|
|
|
|
#define DOWNCAST(MP) container_of(MP, struct sc_mouse_inject, mouse_processor)
|
|
|
|
|
|
|
|
static enum android_motionevent_buttons
|
|
|
|
convert_mouse_buttons(uint32_t state) {
|
|
|
|
enum android_motionevent_buttons buttons = 0;
|
2021-12-29 23:24:20 +08:00
|
|
|
if (state & SC_MOUSE_BUTTON_LEFT) {
|
2021-10-03 23:44:14 +08:00
|
|
|
buttons |= AMOTION_EVENT_BUTTON_PRIMARY;
|
|
|
|
}
|
2021-12-29 23:24:20 +08:00
|
|
|
if (state & SC_MOUSE_BUTTON_RIGHT) {
|
2021-10-03 23:44:14 +08:00
|
|
|
buttons |= AMOTION_EVENT_BUTTON_SECONDARY;
|
|
|
|
}
|
2021-12-29 23:24:20 +08:00
|
|
|
if (state & SC_MOUSE_BUTTON_MIDDLE) {
|
2021-10-03 23:44:14 +08:00
|
|
|
buttons |= AMOTION_EVENT_BUTTON_TERTIARY;
|
|
|
|
}
|
2021-12-29 23:24:20 +08:00
|
|
|
if (state & SC_MOUSE_BUTTON_X1) {
|
2021-10-03 23:44:14 +08:00
|
|
|
buttons |= AMOTION_EVENT_BUTTON_BACK;
|
|
|
|
}
|
2021-12-29 23:24:20 +08:00
|
|
|
if (state & SC_MOUSE_BUTTON_X2) {
|
2021-10-03 23:44:14 +08:00
|
|
|
buttons |= AMOTION_EVENT_BUTTON_FORWARD;
|
|
|
|
}
|
|
|
|
return buttons;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2021-12-29 23:24:20 +08:00
|
|
|
convert_mouse_action(enum sc_action from, enum android_motionevent_action *to) {
|
2021-11-27 04:58:56 +08:00
|
|
|
static const struct sc_intmap_entry actions[] = {
|
2021-12-29 23:24:20 +08:00
|
|
|
{SC_ACTION_DOWN, AMOTION_EVENT_ACTION_DOWN},
|
|
|
|
{SC_ACTION_UP, AMOTION_EVENT_ACTION_UP},
|
2021-11-27 04:58:56 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
const struct sc_intmap_entry *entry = SC_INTMAP_FIND_ENTRY(actions, from);
|
|
|
|
if (entry) {
|
|
|
|
*to = entry->value;
|
|
|
|
return true;
|
2021-10-03 23:44:14 +08:00
|
|
|
}
|
2021-11-27 04:58:56 +08:00
|
|
|
|
|
|
|
return false;
|
2021-10-03 23:44:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2021-12-29 23:24:20 +08:00
|
|
|
convert_touch_action(enum sc_touch_action from,
|
|
|
|
enum android_motionevent_action *to) {
|
2021-11-27 04:58:56 +08:00
|
|
|
static const struct sc_intmap_entry actions[] = {
|
2021-12-29 23:24:20 +08:00
|
|
|
{SC_TOUCH_ACTION_MOVE, AMOTION_EVENT_ACTION_MOVE},
|
|
|
|
{SC_TOUCH_ACTION_DOWN, AMOTION_EVENT_ACTION_DOWN},
|
|
|
|
{SC_TOUCH_ACTION_UP, AMOTION_EVENT_ACTION_UP},
|
2021-11-27 04:58:56 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
const struct sc_intmap_entry *entry = SC_INTMAP_FIND_ENTRY(actions, from);
|
|
|
|
if (entry) {
|
|
|
|
*to = entry->value;
|
|
|
|
return true;
|
2021-10-03 23:44:14 +08:00
|
|
|
}
|
2021-11-27 04:58:56 +08:00
|
|
|
|
|
|
|
return false;
|
2021-10-03 23:44:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2021-12-29 23:24:20 +08:00
|
|
|
convert_mouse_motion(const struct sc_mouse_motion_event *event,
|
|
|
|
struct control_msg *msg) {
|
|
|
|
msg->type = CONTROL_MSG_TYPE_INJECT_TOUCH_EVENT;
|
|
|
|
msg->inject_touch_event.action = AMOTION_EVENT_ACTION_MOVE;
|
|
|
|
msg->inject_touch_event.pointer_id = POINTER_ID_MOUSE;
|
|
|
|
msg->inject_touch_event.position = event->position;
|
|
|
|
msg->inject_touch_event.pressure = 1.f;
|
|
|
|
msg->inject_touch_event.buttons =
|
|
|
|
convert_mouse_buttons(event->buttons_state);
|
2021-10-03 23:44:14 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2021-12-29 23:24:20 +08:00
|
|
|
convert_touch(const struct sc_touch_event *event,
|
|
|
|
struct control_msg *msg) {
|
|
|
|
msg->type = CONTROL_MSG_TYPE_INJECT_TOUCH_EVENT;
|
2021-10-03 23:44:14 +08:00
|
|
|
|
2021-12-29 23:24:20 +08:00
|
|
|
if (!convert_touch_action(event->action, &msg->inject_touch_event.action)) {
|
2021-10-03 23:44:14 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-12-29 23:24:20 +08:00
|
|
|
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;
|
2021-10-03 23:44:14 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2021-12-29 23:24:20 +08:00
|
|
|
convert_mouse_click(const struct sc_mouse_click_event *event,
|
|
|
|
struct control_msg *msg) {
|
|
|
|
msg->type = CONTROL_MSG_TYPE_INJECT_TOUCH_EVENT;
|
2021-10-03 23:44:14 +08:00
|
|
|
|
2021-12-29 23:24:20 +08:00
|
|
|
if (!convert_mouse_action(event->action, &msg->inject_touch_event.action)) {
|
2021-10-03 23:44:14 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-12-29 23:24:20 +08:00
|
|
|
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);
|
2021-10-03 23:44:14 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2021-12-29 23:24:20 +08:00
|
|
|
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;
|
2021-10-03 23:44:14 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
sc_mouse_processor_process_mouse_motion(struct sc_mouse_processor *mp,
|
2021-12-29 23:24:20 +08:00
|
|
|
const struct sc_mouse_motion_event *event) {
|
2021-10-03 23:44:14 +08:00
|
|
|
struct sc_mouse_inject *mi = DOWNCAST(mp);
|
|
|
|
|
|
|
|
struct control_msg msg;
|
2021-12-29 23:24:20 +08:00
|
|
|
if (!convert_mouse_motion(event, &msg)) {
|
2021-10-03 23:44:14 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!controller_push_msg(mi->controller, &msg)) {
|
|
|
|
LOGW("Could not request 'inject mouse motion event'");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
sc_mouse_processor_process_touch(struct sc_mouse_processor *mp,
|
2021-12-29 23:24:20 +08:00
|
|
|
const struct sc_touch_event *event) {
|
2021-10-03 23:44:14 +08:00
|
|
|
struct sc_mouse_inject *mi = DOWNCAST(mp);
|
|
|
|
|
|
|
|
struct control_msg msg;
|
2021-12-29 23:24:20 +08:00
|
|
|
if (convert_touch(event, &msg)) {
|
2021-10-03 23:44:14 +08:00
|
|
|
if (!controller_push_msg(mi->controller, &msg)) {
|
|
|
|
LOGW("Could not request 'inject touch event'");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2021-12-29 23:24:20 +08:00
|
|
|
sc_mouse_processor_process_mouse_click(struct sc_mouse_processor *mp,
|
|
|
|
const struct sc_mouse_click_event *event) {
|
2021-10-03 23:44:14 +08:00
|
|
|
struct sc_mouse_inject *mi = DOWNCAST(mp);
|
|
|
|
|
|
|
|
struct control_msg msg;
|
2021-12-29 23:24:20 +08:00
|
|
|
if (convert_mouse_click(event, &msg)) {
|
2021-10-03 23:44:14 +08:00
|
|
|
if (!controller_push_msg(mi->controller, &msg)) {
|
2021-12-29 23:24:20 +08:00
|
|
|
LOGW("Could not request 'inject mouse click event'");
|
2021-10-03 23:44:14 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2021-12-29 23:24:20 +08:00
|
|
|
sc_mouse_processor_process_mouse_scroll(struct sc_mouse_processor *mp,
|
|
|
|
const struct sc_mouse_scroll_event *event) {
|
2021-10-03 23:44:14 +08:00
|
|
|
struct sc_mouse_inject *mi = DOWNCAST(mp);
|
|
|
|
|
|
|
|
struct control_msg msg;
|
2021-12-29 23:24:20 +08:00
|
|
|
if (convert_mouse_scroll(event, &msg)) {
|
2021-10-03 23:44:14 +08:00
|
|
|
if (!controller_push_msg(mi->controller, &msg)) {
|
2021-12-29 23:24:20 +08:00
|
|
|
LOGW("Could not request 'inject mouse scroll event'");
|
2021-10-03 23:44:14 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2021-12-29 23:24:20 +08:00
|
|
|
sc_mouse_inject_init(struct sc_mouse_inject *mi,
|
|
|
|
struct controller *controller) {
|
2021-10-03 23:44:14 +08:00
|
|
|
mi->controller = controller;
|
|
|
|
|
|
|
|
static const struct sc_mouse_processor_ops ops = {
|
|
|
|
.process_mouse_motion = sc_mouse_processor_process_mouse_motion,
|
|
|
|
.process_touch = sc_mouse_processor_process_touch,
|
2021-12-29 23:24:20 +08:00
|
|
|
.process_mouse_click = sc_mouse_processor_process_mouse_click,
|
|
|
|
.process_mouse_scroll = sc_mouse_processor_process_mouse_scroll,
|
2021-10-03 23:44:14 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
mi->mouse_processor.ops = &ops;
|
|
|
|
}
|