Touch: fingers reach apps as wl_touch, so a thumb scrolls
Deploy to the handset / build-and-deploy (push) Successful in 2m20s
CI / test (push) Failing after 2m56s

Every touch was turned into a mouse drag in the seat code, which selects text
instead of scrolling. The seat now reports raw touches and the compositor
routes them: to the app as wl_touch on its own content, and to the pointer
(tap, drag, hold for right click, as before) everywhere else and for apps that
don't listen to touches. The cursor is hidden while fingers are in use.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
nandiandClaude Opus 5.5 committed 2026-11-29 22:22:14 -08:00
1 parent 98a99aaa36
commit e30d9cc71f
17 files changed
+786 -126

No files matched your search

+48 -114
View File
@@ -17,7 +17,6 @@
#include <string.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <sys/timerfd.h>
#include <unistd.h>
#include <libseat.h>
@@ -32,10 +31,6 @@
#define KEY_F10_CODE 68
#define KEY_F11_CODE 87
#define KEY_F12_CODE 88
#define BTN_LEFT_CODE 0x110
#define BTN_RIGHT_CODE 0x111
#define TOUCH_LONG_MS 500
#define TOUCH_DRAG_PX 18
/* ---- damage ---- */
@@ -114,7 +109,8 @@ static const struct libinput_interface g_li_iface = {
/* ---- input event queue --------------------------------------------------- */
enum { EV_MOVE = 1, EV_BUTTON = 2, EV_KEY = 3, EV_MODS = 4, EV_AXIS = 5 };
enum { EV_MOVE = 1, EV_BUTTON = 2, EV_KEY = 3, EV_MODS = 4, EV_AXIS = 5, EV_TOUCH = 6 };
enum { TP_DOWN = 0, TP_MOTION = 1, TP_UP = 2, TP_CANCEL = 3, TP_PRESENT = 4 };
#define QCAP 512
static int32_t g_q[QCAP][5];
@@ -249,11 +245,7 @@ static int g_cur_ready;
static int g_cur_shape = -1;
static int g_px, g_py; /* pointer position (hotspot) */
static int g_hotx, g_hoty;
enum { TOUCH_IDLE = 0, TOUCH_PENDING = 1, TOUCH_DRAG = 2, TOUCH_LONG = 3 };
static int g_touch_state;
static int32_t g_touch_slot;
static int g_touch_x0, g_touch_y0;
static int g_touch_timer = -1;
static int g_cur_hidden; /* fingers are in use: no cursor until a pointer moves */
static const char *ART_ARROW[] = {
"X...........", "XX..........", "XoX.........", "XooX........", "XoooX.......",
@@ -316,7 +308,7 @@ static void cursor_draw(int shape) {
}
static void cursor_apply(void) {
if (!g_cur_ready || !g_seat_enabled) return;
if (!g_cur_ready || !g_seat_enabled || g_cur_hidden) return;
int ux = g_px, uy = g_py;
if (g_rot != 0 && g_base_w > 0 && g_base_h > 0) {
logical_to_unrot(g_px, g_py, &ux, &uy);
@@ -325,28 +317,35 @@ static void cursor_apply(void) {
drmModeMoveCursor(g_drm, g_crtc, ux * g_scale - g_hotx, uy * g_scale - g_hoty);
}
/* ---- touch long-press timer ---------------------------------------------- */
static void touch_timer_arm(int ms) {
if (g_touch_timer < 0) return;
struct itimerspec its = {0};
its.it_value.tv_sec = ms / 1000;
its.it_value.tv_nsec = (long)(ms % 1000) * 1000000L;
timerfd_settime(g_touch_timer, 0, &its, NULL);
static void cursor_hide(void) {
if (g_cur_hidden) return;
g_cur_hidden = 1;
if (g_cur_ready && g_seat_enabled) drmModeSetCursor(g_drm, g_crtc, 0, 0, 0);
}
static void touch_timer_disarm(void) {
if (g_touch_timer < 0) return;
struct itimerspec its = {0};
timerfd_settime(g_touch_timer, 0, &its, NULL);
}
/* ---- touch --------------------------------------------------------------- */
static void touch_cancel(void) {
touch_timer_disarm();
if (g_touch_state == TOUCH_DRAG) {
q_push(EV_BUTTON, BTN_LEFT_CODE, 0, 0, 0);
/* A finger's position in the rotated output, to 1/256 of a logical pixel. */
static void touch_push(int phase, struct libinput_event_touch *t) {
double rx = libinput_event_touch_get_x_transformed(t, (uint32_t)g_base_w);
double ry = libinput_event_touch_get_y_transformed(t, (uint32_t)g_base_h);
double x = rx, y = ry;
switch (g_rot) {
case 1: x = ry; y = g_base_w - rx; break;
case 2: x = g_base_w - rx; y = g_base_h - ry; break;
case 3: x = g_base_h - ry; y = rx; break;
default: break;
}
g_touch_state = TOUCH_IDLE;
if (x < 0) x = 0;
if (y < 0) y = 0;
if (x > g_width - 1) x = g_width - 1;
if (y > g_height - 1) y = g_height - 1;
q_push(EV_TOUCH, phase, libinput_event_touch_get_seat_slot(t), (int32_t)(x * 256.0), (int32_t)(y * 256.0));
}
/* Every finger's touch is over (the screen turned off or rotated, the VT switched away). */
static void touch_cancel(void) {
q_push(EV_TOUCH, TP_CANCEL, 0, 0, 0);
}
/* ---- xkb + libinput ------------------------------------------------------ */
@@ -401,6 +400,7 @@ static void handle_li_event(struct libinput_event *ev) {
}
case LIBINPUT_EVENT_POINTER_MOTION: {
struct libinput_event_pointer *p = libinput_event_get_pointer_event(ev);
g_cur_hidden = 0;
g_px += (int)libinput_event_pointer_get_dx(p);
g_py += (int)libinput_event_pointer_get_dy(p);
clamp_pointer();
@@ -410,6 +410,7 @@ static void handle_li_event(struct libinput_event *ev) {
}
case LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE: {
struct libinput_event_pointer *p = libinput_event_get_pointer_event(ev);
g_cur_hidden = 0;
int rx = (int)libinput_event_pointer_get_absolute_x_transformed(p, (uint32_t)g_base_w);
int ry = (int)libinput_event_pointer_get_absolute_y_transformed(p, (uint32_t)g_base_h);
unrot_to_logical(rx, ry, &g_px, &g_py);
@@ -441,74 +442,25 @@ static void handle_li_event(struct libinput_event *ev) {
}
break;
}
case LIBINPUT_EVENT_TOUCH_DOWN: {
struct libinput_event_touch *t = libinput_event_get_touch_event(ev);
if (g_touch_state == TOUCH_IDLE) {
g_touch_slot = libinput_event_touch_get_seat_slot(t);
int rx = (int)libinput_event_touch_get_x_transformed(t, (uint32_t)g_base_w);
int ry = (int)libinput_event_touch_get_y_transformed(t, (uint32_t)g_base_h);
unrot_to_logical(rx, ry, &g_px, &g_py);
clamp_pointer();
g_touch_x0 = g_px;
g_touch_y0 = g_py;
g_touch_state = TOUCH_PENDING;
q_push(EV_MOVE, g_px, g_py, 0, 0);
cursor_apply();
touch_timer_arm(TOUCH_LONG_MS);
}
case LIBINPUT_EVENT_DEVICE_ADDED:
if (libinput_device_has_capability(libinput_event_get_device(ev), LIBINPUT_DEVICE_CAP_TOUCH))
q_push(EV_TOUCH, TP_PRESENT, 0, 0, 0);
break;
}
case LIBINPUT_EVENT_TOUCH_MOTION: {
struct libinput_event_touch *t = libinput_event_get_touch_event(ev);
if (g_touch_state != TOUCH_IDLE && libinput_event_touch_get_seat_slot(t) == g_touch_slot) {
int rx = (int)libinput_event_touch_get_x_transformed(t, (uint32_t)g_base_w);
int ry = (int)libinput_event_touch_get_y_transformed(t, (uint32_t)g_base_h);
unrot_to_logical(rx, ry, &g_px, &g_py);
clamp_pointer();
if (g_touch_state == TOUCH_PENDING) {
int dx = g_px - g_touch_x0;
int dy = g_py - g_touch_y0;
if (dx * dx + dy * dy > TOUCH_DRAG_PX * TOUCH_DRAG_PX) {
touch_timer_disarm();
g_touch_state = TOUCH_DRAG;
q_push(EV_MOVE, g_touch_x0, g_touch_y0, 0, 0);
q_push(EV_BUTTON, BTN_LEFT_CODE, 1, 0, 0);
q_push(EV_MOVE, g_px, g_py, 0, 0);
cursor_apply();
}
} else if (g_touch_state == TOUCH_DRAG) {
q_push(EV_MOVE, g_px, g_py, 0, 0);
cursor_apply();
}
}
/* Fingers go to the compositor as they are: it decides which are an app's and which work the
* shell like a mouse (compositor/host/touch.mbt). */
case LIBINPUT_EVENT_TOUCH_DOWN:
cursor_hide();
touch_push(TP_DOWN, libinput_event_get_touch_event(ev));
break;
}
case LIBINPUT_EVENT_TOUCH_UP: {
struct libinput_event_touch *t = libinput_event_get_touch_event(ev);
if (g_touch_state != TOUCH_IDLE && libinput_event_touch_get_seat_slot(t) == g_touch_slot) {
int st = g_touch_state;
touch_timer_disarm();
g_touch_state = TOUCH_IDLE;
if (st == TOUCH_PENDING) {
g_px = g_touch_x0;
g_py = g_touch_y0;
q_push(EV_MOVE, g_px, g_py, 0, 0);
q_push(EV_BUTTON, BTN_LEFT_CODE, 1, 0, 0);
q_push(EV_BUTTON, BTN_LEFT_CODE, 0, 0, 0);
cursor_apply();
} else if (st == TOUCH_DRAG) {
q_push(EV_BUTTON, BTN_LEFT_CODE, 0, 0, 0);
}
}
case LIBINPUT_EVENT_TOUCH_MOTION:
touch_push(TP_MOTION, libinput_event_get_touch_event(ev));
break;
}
case LIBINPUT_EVENT_TOUCH_CANCEL: {
struct libinput_event_touch *t = libinput_event_get_touch_event(ev);
if (g_touch_state != TOUCH_IDLE && libinput_event_touch_get_seat_slot(t) == g_touch_slot) {
touch_cancel();
}
case LIBINPUT_EVENT_TOUCH_UP:
q_push(EV_TOUCH, TP_UP, libinput_event_touch_get_seat_slot(libinput_event_get_touch_event(ev)), 0, 0);
break;
case LIBINPUT_EVENT_TOUCH_CANCEL:
touch_cancel();
break;
}
default:
break;
}
@@ -668,7 +620,6 @@ int32_t mp_kms_open(void) {
fprintf(stderr, "kms: cannot start libinput on seat %s\n", g_seat ? libseat_seat_name(g_seat) : "?");
return -6;
}
g_touch_timer = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK | TFD_CLOEXEC);
dispatch_input();
return 0;
}
@@ -697,13 +648,12 @@ int32_t mp_kms_set_rotation(int32_t rot) {
MOONBIT_FFI_EXPORT
int32_t mp_kms_poll(void) {
if (!g_seat) return 0;
struct pollfd pfd[4];
struct pollfd pfd[3];
int n = 0;
pfd[n].fd = libseat_get_fd(g_seat); pfd[n].events = POLLIN; n++;
pfd[n].fd = g_drm; pfd[n].events = POLLIN; n++;
int lifd = g_li ? libinput_get_fd(g_li) : -1;
pfd[n].fd = lifd; pfd[n].events = POLLIN; n++;
pfd[n].fd = g_touch_timer; pfd[n].events = POLLIN; n++;
poll(pfd, (nfds_t)n, 0);
libseat_dispatch(g_seat, 0);
if (pfd[1].revents & POLLIN) {
@@ -727,24 +677,10 @@ int32_t mp_kms_poll(void) {
if (g_blank) set_dpms(0);
}
if (g_li) dispatch_input();
if (g_touch_timer >= 0 && (pfd[3].revents & POLLIN)) {
uint64_t exp = 0;
while (read(g_touch_timer, &exp, sizeof exp) > 0) {}
if (g_touch_state == TOUCH_PENDING) {
g_touch_state = TOUCH_LONG;
g_px = g_touch_x0;
g_py = g_touch_y0;
q_push(EV_MOVE, g_px, g_py, 0, 0);
q_push(EV_BUTTON, BTN_RIGHT_CODE, 1, 0, 0);
q_push(EV_BUTTON, BTN_RIGHT_CODE, 0, 0, 0);
cursor_apply();
}
}
return g_seat_enabled;
}
/* The fds mp_kms_poll services (0 seat, 1 DRM, 2 libinput, 3 touch timer), for an idle loop to
block on; -1 when not open. */
/* The fds mp_kms_poll services (0 seat, 1 DRM, 2 libinput), for an idle loop to block on; -1 when not open. */
MOONBIT_FFI_EXPORT
int32_t mp_kms_fd(int32_t i) {
if (!g_seat) return -1;
@@ -752,7 +688,6 @@ int32_t mp_kms_fd(int32_t i) {
case 0: return libseat_get_fd(g_seat);
case 1: return g_drm;
case 2: return g_li ? libinput_get_fd(g_li) : -1;
case 3: return g_touch_timer;
default: return -1;
}
}
@@ -945,7 +880,6 @@ int32_t mp_kms_keymap_copy(moonbit_bytes_t out) {
MOONBIT_FFI_EXPORT
void mp_kms_close(void) {
touch_cancel();
if (g_touch_timer >= 0) { close(g_touch_timer); g_touch_timer = -1; }
g_phys_w = 0; g_phys_h = 0; g_base_w = 0; g_base_h = 0; g_rot = 0; g_mm_w = 0; g_mm_h = 0; g_scale = 1;
if (g_li) { libinput_unref(g_li); g_li = NULL; }
if (g_udev) { udev_unref(g_udev); g_udev = NULL; }