2021-10-03 15:44:14 +00:00
|
|
|
#include "mouse_inject.h"
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
#include "android/input.h"
|
|
|
|
#include "control_msg.h"
|
|
|
|
#include "controller.h"
|
2021-12-29 15:24:20 +00:00
|
|
|
#include "input_events.h"
|
2021-11-26 20:58:56 +00:00
|
|
|
#include "util/intmap.h"
|
2021-10-03 15:44:14 +00: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 15:24:20 +00:00
|
|
|
if (state & SC_MOUSE_BUTTON_LEFT) {
|
2021-10-03 15:44:14 +00:00
|
|
|
buttons |= AMOTION_EVENT_BUTTON_PRIMARY;
|
|
|
|
}
|
2021-12-29 15:24:20 +00:00
|
|
|
if (state & SC_MOUSE_BUTTON_RIGHT) {
|
2021-10-03 15:44:14 +00:00
|
|
|
buttons |= AMOTION_EVENT_BUTTON_SECONDARY;
|
|
|
|
}
|
2021-12-29 15:24:20 +00:00
|
|
|
if (state & SC_MOUSE_BUTTON_MIDDLE) {
|
2021-10-03 15:44:14 +00:00
|
|
|
buttons |= AMOTION_EVENT_BUTTON_TERTIARY;
|
|
|
|
}
|
2021-12-29 15:24:20 +00:00
|
|
|
if (state & SC_MOUSE_BUTTON_X1) {
|
2021-10-03 15:44:14 +00:00
|
|
|
buttons |= AMOTION_EVENT_BUTTON_BACK;
|
|
|
|
}
|
2021-12-29 15:24:20 +00:00
|
|
|
if (state & SC_MOUSE_BUTTON_X2) {
|
2021-10-03 15:44:14 +00:00
|
|
|
buttons |= AMOTION_EVENT_BUTTON_FORWARD;
|
|
|
|
}
|
|
|
|
return buttons;
|
|
|
|
}
|
|
|
|
|
2021-12-29 15:55:01 +00:00
|
|
|
static enum android_motionevent_action
|
|
|
|
convert_mouse_action(enum sc_action action) {
|
|
|
|
if (action == SC_ACTION_DOWN) {
|
|
|
|
return AMOTION_EVENT_ACTION_DOWN;
|
2021-10-03 15:44:14 +00:00
|
|
|
}
|
2021-12-29 15:55:01 +00:00
|
|
|
assert(action == SC_ACTION_UP);
|
|
|
|
return AMOTION_EVENT_ACTION_UP;
|
2021-10-03 15:44:14 +00:00
|
|
|
}
|
|
|
|
|
2021-12-29 15:55:01 +00:00
|
|
|
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;
|
2021-10-03 15:44:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
sc_mouse_processor_process_mouse_motion(struct sc_mouse_processor *mp,
|
2021-12-29 15:24:20 +00:00
|
|
|
const struct sc_mouse_motion_event *event) {
|
2022-01-01 23:11:00 +00:00
|
|
|
if (!event->buttons_state) {
|
|
|
|
// Do not send motion events when no click is pressed
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-10-03 15:44:14 +00:00
|
|
|
struct sc_mouse_inject *mi = DOWNCAST(mp);
|
|
|
|
|
2022-01-14 21:17:30 +00:00
|
|
|
struct sc_control_msg msg = {
|
2022-01-26 20:31:30 +00:00
|
|
|
.type = SC_CONTROL_MSG_TYPE_INJECT_TOUCH_EVENT,
|
2021-12-29 16:10:12 +00:00
|
|
|
.inject_touch_event = {
|
|
|
|
.action = AMOTION_EVENT_ACTION_MOVE,
|
|
|
|
.pointer_id = POINTER_ID_MOUSE,
|
|
|
|
.position = event->position,
|
|
|
|
.pressure = 1.f,
|
|
|
|
.buttons = convert_mouse_buttons(event->buttons_state),
|
|
|
|
},
|
|
|
|
};
|
2021-10-03 15:44:14 +00:00
|
|
|
|
2022-01-14 21:17:30 +00:00
|
|
|
if (!sc_controller_push_msg(mi->controller, &msg)) {
|
2021-10-03 15:44:14 +00:00
|
|
|
LOGW("Could not request 'inject mouse motion event'");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2021-12-29 15:24:20 +00:00
|
|
|
sc_mouse_processor_process_mouse_click(struct sc_mouse_processor *mp,
|
|
|
|
const struct sc_mouse_click_event *event) {
|
2021-10-03 15:44:14 +00:00
|
|
|
struct sc_mouse_inject *mi = DOWNCAST(mp);
|
|
|
|
|
2022-01-14 21:17:30 +00:00
|
|
|
struct sc_control_msg msg = {
|
2022-01-26 20:31:30 +00:00
|
|
|
.type = SC_CONTROL_MSG_TYPE_INJECT_TOUCH_EVENT,
|
2021-12-29 16:10:12 +00:00
|
|
|
.inject_touch_event = {
|
|
|
|
.action = convert_mouse_action(event->action),
|
|
|
|
.pointer_id = POINTER_ID_MOUSE,
|
|
|
|
.position = event->position,
|
|
|
|
.pressure = event->action == SC_ACTION_DOWN ? 1.f : 0.f,
|
|
|
|
.buttons = convert_mouse_buttons(event->buttons_state),
|
|
|
|
},
|
|
|
|
};
|
2021-12-29 15:55:01 +00:00
|
|
|
|
2022-01-14 21:17:30 +00:00
|
|
|
if (!sc_controller_push_msg(mi->controller, &msg)) {
|
2021-12-29 15:55:01 +00:00
|
|
|
LOGW("Could not request 'inject mouse click event'");
|
2021-10-03 15:44:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2021-12-29 15:24:20 +00:00
|
|
|
sc_mouse_processor_process_mouse_scroll(struct sc_mouse_processor *mp,
|
|
|
|
const struct sc_mouse_scroll_event *event) {
|
2021-10-03 15:44:14 +00:00
|
|
|
struct sc_mouse_inject *mi = DOWNCAST(mp);
|
|
|
|
|
2022-01-14 21:17:30 +00:00
|
|
|
struct sc_control_msg msg = {
|
2022-01-26 20:31:30 +00:00
|
|
|
.type = SC_CONTROL_MSG_TYPE_INJECT_SCROLL_EVENT,
|
2021-12-29 16:10:12 +00:00
|
|
|
.inject_scroll_event = {
|
|
|
|
.position = event->position,
|
|
|
|
.hscroll = event->hscroll,
|
|
|
|
.vscroll = event->vscroll,
|
2021-12-31 09:38:05 +00:00
|
|
|
.buttons = convert_mouse_buttons(event->buttons_state),
|
2021-12-29 16:10:12 +00:00
|
|
|
},
|
|
|
|
};
|
2021-12-29 15:55:01 +00:00
|
|
|
|
2022-01-14 21:17:30 +00:00
|
|
|
if (!sc_controller_push_msg(mi->controller, &msg)) {
|
2021-12-29 15:55:01 +00:00
|
|
|
LOGW("Could not request 'inject mouse scroll event'");
|
2021-10-03 15:44:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-29 22:20:35 +00:00
|
|
|
static void
|
|
|
|
sc_mouse_processor_process_touch(struct sc_mouse_processor *mp,
|
|
|
|
const struct sc_touch_event *event) {
|
|
|
|
struct sc_mouse_inject *mi = DOWNCAST(mp);
|
|
|
|
|
2022-01-14 21:17:30 +00:00
|
|
|
struct sc_control_msg msg = {
|
2022-01-26 20:31:30 +00:00
|
|
|
.type = SC_CONTROL_MSG_TYPE_INJECT_TOUCH_EVENT,
|
2021-12-29 22:20:35 +00:00
|
|
|
.inject_touch_event = {
|
|
|
|
.action = convert_touch_action(event->action),
|
|
|
|
.pointer_id = event->pointer_id,
|
|
|
|
.position = event->position,
|
|
|
|
.pressure = event->pressure,
|
|
|
|
.buttons = 0,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2022-01-14 21:17:30 +00:00
|
|
|
if (!sc_controller_push_msg(mi->controller, &msg)) {
|
2021-12-29 22:20:35 +00:00
|
|
|
LOGW("Could not request 'inject touch event'");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-03 15:44:14 +00:00
|
|
|
void
|
2021-12-29 15:24:20 +00:00
|
|
|
sc_mouse_inject_init(struct sc_mouse_inject *mi,
|
2022-01-14 21:17:30 +00:00
|
|
|
struct sc_controller *controller) {
|
2021-10-03 15:44:14 +00:00
|
|
|
mi->controller = controller;
|
|
|
|
|
|
|
|
static const struct sc_mouse_processor_ops ops = {
|
|
|
|
.process_mouse_motion = sc_mouse_processor_process_mouse_motion,
|
2021-12-29 15:24:20 +00:00
|
|
|
.process_mouse_click = sc_mouse_processor_process_mouse_click,
|
|
|
|
.process_mouse_scroll = sc_mouse_processor_process_mouse_scroll,
|
2021-12-29 22:20:35 +00:00
|
|
|
.process_touch = sc_mouse_processor_process_touch,
|
2021-10-03 15:44:14 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
mi->mouse_processor.ops = &ops;
|
2021-12-30 14:46:00 +00:00
|
|
|
|
|
|
|
mi->mouse_processor.relative_mode = false;
|
2021-10-03 15:44:14 +00:00
|
|
|
}
|