Skip to content

Display and control your Android device

Send touch events from the client

Romain Vimontb5a2d99bc24b
diff --git a/app/src/event_converter.c b/app/src/event_converter.c
index da4b2e30..e9fbe13b 100644
--- a/app/src/event_converter.c
+++ b/app/src/event_converter.c
@@ -207,6 +207,34 @@ convert_mouse_motion(const SDL_MouseMotionEvent *from, struct size screen_size,
return true;
}
+static bool
+convert_touch_action(SDL_EventType from, enum android_motionevent_action *to) {
+ switch (from) {
+ MAP(SDL_FINGERMOTION, AMOTION_EVENT_ACTION_MOVE);
+ MAP(SDL_FINGERDOWN, AMOTION_EVENT_ACTION_DOWN);
+ MAP(SDL_FINGERUP, AMOTION_EVENT_ACTION_UP);
+ FAIL;
+ }
+}
+
+bool
+convert_touch(const SDL_TouchFingerEvent *from, struct size screen_size,
+ struct control_msg *to) {
+ to->type = CONTROL_MSG_TYPE_INJECT_TOUCH_EVENT;
+
+ if (!convert_touch_action(from->type, &to->inject_touch_event.action)) {
+ return false;
+ }
+
+ to->inject_touch_event.pointer_id = from->fingerId;
+ to->inject_touch_event.position.screen_size = screen_size;
+ // SDL touch event coordinates are normalized in the range [0; 1]
+ to->inject_touch_event.position.point.x = from->x * screen_size.width;
+ to->inject_touch_event.position.point.y = from->y * screen_size.height;
+ to->inject_touch_event.pressure = from->pressure;
+ return true;
+}
+
bool
convert_mouse_wheel(const SDL_MouseWheelEvent *from, struct position position,
struct control_msg *to) {
diff --git a/app/src/event_converter.h b/app/src/event_converter.h
index e0b24c15..f6f136a3 100644
--- a/app/src/event_converter.h
+++ b/app/src/event_converter.h
@@ -30,6 +30,10 @@ bool
convert_mouse_motion(const SDL_MouseMotionEvent *from, struct size screen_size,
struct control_msg *to);
+bool
+convert_touch(const SDL_TouchFingerEvent *from, struct size screen_size,
+ struct control_msg *to);
+
// on Android, a scroll event requires the current mouse position
bool
convert_mouse_wheel(const SDL_MouseWheelEvent *from, struct position position,
diff --git a/app/src/input_manager.c b/app/src/input_manager.c
index cf2a7519..2123f241 100644
--- a/app/src/input_manager.c
+++ b/app/src/input_manager.c
@@ -397,6 +397,17 @@ input_manager_process_mouse_motion(struct input_manager *input_manager,
}
}
+void
+input_manager_process_touch(struct input_manager *input_manager,
+ const SDL_TouchFingerEvent *event) {
+ struct control_msg msg;
+ if (convert_touch(event, input_manager->screen->frame_size, &msg)) {
+ if (!controller_push_msg(input_manager->controller, &msg)) {
+ LOGW("Could not request 'inject touch event'");
+ }
+ }
+}
+
static bool
is_outside_device_screen(struct input_manager *input_manager, int x, int y)
{
diff --git a/app/src/input_manager.h b/app/src/input_manager.h
index 61a0447f..0009cb81 100644
--- a/app/src/input_manager.h
+++ b/app/src/input_manager.h
@@ -29,6 +29,10 @@ void
input_manager_process_mouse_motion(struct input_manager *input_manager,
const SDL_MouseMotionEvent *event);
+void
+input_manager_process_touch(struct input_manager *input_manager,
+ const SDL_TouchFingerEvent *event);
+
void
input_manager_process_mouse_button(struct input_manager *input_manager,
const SDL_MouseButtonEvent *event,
diff --git a/app/src/scrcpy.c b/app/src/scrcpy.c
index defcb751..c219c9e5 100644
--- a/app/src/scrcpy.c
+++ b/app/src/scrcpy.c
@@ -181,6 +181,11 @@ handle_event(SDL_Event *event, bool control) {
input_manager_process_mouse_button(&input_manager, &event->button,
control);
break;
+ case SDL_FINGERMOTION:
+ case SDL_FINGERDOWN:
+ case SDL_FINGERUP:
+ input_manager_process_touch(&input_manager, &event->tfinger);
+ break;
case SDL_DROPFILE: {
if (!control) {
break;