feat(input): a pen moves the pointer while hovering, clicks with its tip and right-clicks with its side button (#2625)

* feat(compositor): pen samples drive the pointer like a mouse

Input record 13 carries the pen's state (in proximity, tip, right and
middle button) and its position in input space as 24.8 fixed point.
Hover moves the pointer, the tip holds the left button, and a record
without the in-proximity bit releases every button. Under a pointer
lock only the buttons pass. A button pressed or released mid-stroke
keeps the implicit grab on the window the stroke started in, unless
that window closed.

* feat(input): a pen moves the pointer while hovering, clicks with its tip and right-clicks with its side button

In Mouse mode (the default) a pen's hover moves the pointer, the tip
holds the left button and the side button right-clicks; Touch keeps it a
finger. The side button can send a middle click or nothing instead, and
a Bluetooth pen key counts as the side button while the pen is near.
With glasses or a program's pointer lock the pen drives the touchpad.
A hover exit followed within 50 ms by more hover or a touch keeps the
pen near, so a pen at the edge of its hover range does not flicker out,
and a hover that sends nothing for 2 s leaves proximity. On the desktop
a pen that comes near releases KWin's grab of a hardware mouse, so the
pen moves KWin's pointer. Whatever cancels touches also ends the pen,
and stylus devices no longer count as hardware mice.

* feat(input): pen mode and side button settings in the Controls tab and the session drawer

A Pen group follows the touch settings once the phone has a stylus
device or a pen event was seen, which is remembered as penSeen and kept
by a settings reset. It holds Pen (Mouse, Touch) and Side button (Right
click, Middle click, Off); Side button is hidden in Touch. Before
Android 14 a touchscreen reports the stylus source without a pen, so
there it counts only with its first pen event. The strings are
translated into all 32 languages.

* docs: describe pen input and its limits

---------

Co-authored-by: alien2003 <11610652+alien2003@users.noreply.github.com>
This commit is contained in:
alien2003andalien2003 authored and GitHub committed 2026-11-15 09:38:01 -05:00
1 parent de70e37f30
commit decab6ea90
63 files changed
+2352 -38

No files matched your search

+158
View File
@@ -0,0 +1,158 @@
#include <assert.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#define BTN_LEFT 0x110
#define BTN_RIGHT 0x111
#define BTN_MIDDLE 0x112
#define INPUT_SPACE_W 1920
#define INPUT_SPACE_H 1080
#include "input_msg.inc"
#include "pen_state.inc"
enum { MOTION, PRESS, RELEASE };
struct event { int kind; uint32_t button; double x, y; int raw_valid; };
struct constraint { int is_lock; };
static struct constraint *g_active_constraint;
static struct surface *g_grab;
static int g_raw_valid;
static struct event events[16];
static int nevents, logs;
static uint32_t held;
static void scene_size(int *w, int *h) { *w = 3840; *h = 2160; }
static void vkp_output_size(int *w, int *h) { *w = 0; *h = 0; }
static int vkp_output_to_scene(double ox, double oy, double *x, double *y) {
(void)ox; (void)oy; (void)x; (void)y;
return 0;
}
static void pointer_event(double x, double y, uint32_t button, int pressed) {
assert(!button && !pressed && nevents < 16);
events[nevents++] = (struct event){MOTION, 0, x, y, g_raw_valid};
g_raw_valid = 1;
}
static void pointer_button(uint32_t button, int pressed) {
assert(button >= BTN_LEFT && button <= BTN_MIDDLE && nevents < 16);
uint32_t bit = 1u << (button - BTN_LEFT);
if (pressed) assert(!(held & bit));
else assert(held & bit);
held = pressed ? held | bit : held & ~bit;
events[nevents++] = (struct event){pressed ? PRESS : RELEASE, button, 0, 0, g_raw_valid};
}
static void pen_log_target(void) { logs++; }
#include "pen.inc"
static void pen(int flags, double x, double y) {
struct input_msg m = {13, flags, (int)(x * 256), (int)(y * 256), 0};
nevents = 0;
deliver_pen(&m);
}
static void motion(int i, double x, double y) {
assert(events[i].kind == MOTION && events[i].x == x && events[i].y == y);
}
static void button(int i, int kind, uint32_t code) {
assert(events[i].kind == kind && events[i].button == code);
}
static void expect(int count, uint32_t buttons) {
assert(nevents == count && held == buttons);
assert(g_pen.buttons == (int)buttons);
}
#define L (1u << 0)
#define R (1u << 1)
#define M (1u << 2)
int main(int argc, char **argv) {
assert(argc == 2);
const char *c = argv[1];
if (!strcmp(c, "hover")) {
pen(PEN_IN, 100.5, 50.25);
expect(1, 0); motion(0, 201, 100.5);
pen(PEN_IN, 2000, -4);
expect(1, 0); motion(0, 3839, 0);
assert(g_pen.in && logs == 2);
pen(0, 2000, -4);
expect(0, 0);
assert(!g_pen.in && logs == 2);
} else if (!strcmp(c, "tip")) {
pen(PEN_IN, 10, 10);
expect(1, 0);
pen(PEN_IN | PEN_TIP, 10, 10);
expect(2, L); motion(0, 20, 20); button(1, PRESS, BTN_LEFT);
pen(PEN_IN | PEN_TIP, 11, 10);
expect(1, L); motion(0, 22, 20);
pen(PEN_IN, 12, 10);
expect(2, 0); motion(0, 24, 20); button(1, RELEASE, BTN_LEFT);
pen(PEN_IN | PEN_TIP, 12, 10);
pen(PEN_IN, 12, 10);
pen(0, 12, 10);
expect(0, 0);
} else if (!strcmp(c, "buttons")) {
pen(PEN_IN | PEN_RIGHT, 10, 10);
expect(2, R); motion(0, 20, 20); button(1, PRESS, BTN_RIGHT);
pen(PEN_IN | PEN_RIGHT | PEN_MIDDLE, 10, 10);
expect(2, R | M); button(1, PRESS, BTN_MIDDLE);
pen(PEN_IN | PEN_MIDDLE | PEN_TIP, 11, 10);
expect(3, L | M); motion(0, 22, 20); button(1, PRESS, BTN_LEFT); button(2, RELEASE, BTN_RIGHT);
pen(PEN_IN | PEN_RIGHT, 12, 10);
expect(4, R); motion(0, 24, 20); button(1, PRESS, BTN_RIGHT);
button(2, RELEASE, BTN_LEFT); button(3, RELEASE, BTN_MIDDLE);
pen(PEN_IN, 12, 10);
expect(2, 0); button(1, RELEASE, BTN_RIGHT);
} else if (!strcmp(c, "out")) {
pen(PEN_IN | PEN_TIP | PEN_RIGHT | PEN_MIDDLE, 10, 10);
expect(4, L | R | M); motion(0, 20, 20);
button(1, PRESS, BTN_LEFT); button(2, PRESS, BTN_RIGHT); button(3, PRESS, BTN_MIDDLE);
pen(PEN_TIP | PEN_RIGHT | PEN_MIDDLE, 30, 30);
expect(3, 0);
button(0, RELEASE, BTN_LEFT); button(1, RELEASE, BTN_RIGHT); button(2, RELEASE, BTN_MIDDLE);
assert(!g_pen.in && !g_pen.buttons);
pen(0, 30, 30);
expect(0, 0);
} else if (!strcmp(c, "lock")) {
struct constraint lock = {1}, confine = {0};
g_active_constraint = &lock;
pen(PEN_IN, 10, 10);
expect(0, 0);
assert(g_pen.in && logs == 1);
pen(PEN_IN | PEN_TIP, 11, 10);
expect(1, L); button(0, PRESS, BTN_LEFT);
pen(PEN_IN | PEN_TIP, 40, 10);
expect(0, L);
pen(PEN_IN | PEN_RIGHT, 40, 10);
expect(2, R); button(0, PRESS, BTN_RIGHT); button(1, RELEASE, BTN_LEFT);
pen(0, 40, 10);
expect(1, 0); button(0, RELEASE, BTN_RIGHT);
g_active_constraint = &confine;
pen(PEN_IN, 10, 10);
expect(1, 0); motion(0, 20, 20);
} else if (!strcmp(c, "lost")) {
pen(PEN_IN | PEN_TIP, 10, 10);
expect(2, L); motion(0, 20, 20); button(1, PRESS, BTN_LEFT);
pen(0, 10, 10);
expect(1, 0); button(0, RELEASE, BTN_LEFT);
pen(PEN_IN | PEN_TIP | PEN_MIDDLE, 10, 10);
expect(3, L | M);
pen(PEN_IN, 50, 10);
expect(3, 0); motion(0, 100, 20); button(1, RELEASE, BTN_LEFT); button(2, RELEASE, BTN_MIDDLE);
} else {
assert(!strcmp(c, "raw"));
g_raw_valid = 1;
pen(PEN_IN, 10, 10);
assert(!events[0].raw_valid && g_raw_valid);
pen(PEN_IN, 11, 10);
assert(events[0].raw_valid);
pen(PEN_IN | PEN_TIP, 12, 10);
assert(events[0].raw_valid && events[1].raw_valid);
pen(0, 12, 10);
assert(!g_raw_valid);
g_raw_valid = 1;
pen(0, 12, 10);
assert(g_raw_valid);
}
puts("PASS");
}
+182
View File
@@ -0,0 +1,182 @@
#include <assert.h>
#include <stdatomic.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#define BTN_LEFT 0x110
#define BTN_RIGHT 0x111
#define BTN_MIDDLE 0x112
#define INPUT_SPACE_W 1920
#define INPUT_SPACE_H 1080
#define WL_POINTER_FRAME_SINCE_VERSION 5
#define WL_POINTER_BUTTON_STATE_RELEASED 0
#define WL_POINTER_BUTTON_STATE_PRESSED 1
#include "input_msg.inc"
#include "pen_state.inc"
typedef int32_t wl_fixed_t;
struct wl_client { int id; };
struct wl_resource { struct wl_client *client; };
struct wl_display { int unused; };
struct surface { struct wl_resource *resource; int placed, x, y; };
struct seat_pointer { struct wl_resource *ptr, *focus; };
struct constraint { int is_lock; struct surface *surface; };
static struct wl_client client_a, client_b;
static struct wl_resource surface_a = {&client_a}, surface_b = {&client_b};
static struct wl_resource pointer_a = {&client_a}, pointer_b = {&client_b};
static struct surface a = {&surface_a, 1, 0, 0}, b = {&surface_b, 1, 1000, 0};
static struct seat_pointer g_ptrs[2] = {{&pointer_a, NULL}, {&pointer_b, NULL}};
static int g_nptrs = 2;
static struct wl_display *g_display;
static struct constraint *g_active_constraint;
static struct surface *g_desktop, *g_grab, *g_key_target, *g_ime_click;
static double g_ptr_x, g_ptr_y, g_raw_x, g_raw_y;
static int g_ptr_known, g_raw_valid;
static atomic_int g_cursor_composite;
enum { ENTER, LEAVE, PRESS, RELEASE };
struct event { int kind; struct wl_resource *pointer, *surface; uint32_t button; };
static struct event events[32];
static int nevents;
static void record(int kind, struct wl_resource *pointer, struct wl_resource *surface, uint32_t button) {
assert(nevents < 32);
events[nevents++] = (struct event){kind, pointer, surface, button};
}
static void scene_size(int *w, int *h) { *w = 2000; *h = 1000; }
static void vkp_output_size(int *w, int *h) { *w = 0; *h = 0; }
static int vkp_output_to_scene(double ox, double oy, double *x, double *y) {
(void)ox; (void)oy; (void)x; (void)y;
return 0;
}
static struct surface *toplevel_at(double x, double y) {
(void)y;
return x < 1000 ? &a : &b;
}
static void confine_clamp(struct constraint *k, double *x, double *y) { (void)k; (void)x; (void)y; }
static void schedule_render(void) {}
static void publish_query_state(void) {}
static void constraints_focus_entered(struct wl_resource *target, struct wl_client *client) {
(void)target; (void)client;
}
static void droiddeck_text_input_refocus(void) {}
static void pen_log_target(void) {}
static struct surface *dying;
static void wl_display_flush_clients(struct wl_display *display) {
(void)display;
if (!dying || !nevents || events[nevents - 1].kind != RELEASE) return;
if (g_grab == dying) g_grab = NULL;
if (g_pen.grab == dying) g_pen.grab = NULL;
dying = NULL;
}
static uint32_t now_ms(void) { return 0; }
static uint32_t wl_display_next_serial(struct wl_display *display) { (void)display; return 0; }
static int wl_resource_get_version(struct wl_resource *r) { (void)r; return 7; }
static struct wl_client *wl_resource_get_client(struct wl_resource *r) { return r->client; }
static wl_fixed_t wl_fixed_from_double(double d) { return (wl_fixed_t)(d * 256); }
static int send_relative_motion(struct wl_client *client, double dx, double dy) {
(void)client; (void)dx; (void)dy;
return 0;
}
static void wl_pointer_send_enter(struct wl_resource *p, uint32_t serial, struct wl_resource *s,
wl_fixed_t x, wl_fixed_t y) {
(void)serial; (void)x; (void)y;
record(ENTER, p, s, 0);
}
static void wl_pointer_send_leave(struct wl_resource *p, uint32_t serial, struct wl_resource *s) {
(void)serial;
record(LEAVE, p, s, 0);
}
static void wl_pointer_send_motion(struct wl_resource *p, uint32_t t, wl_fixed_t x, wl_fixed_t y) {
(void)p; (void)t; (void)x; (void)y;
}
static void wl_pointer_send_frame(struct wl_resource *p) { (void)p; }
static void wl_pointer_send_button(struct wl_resource *p, uint32_t serial, uint32_t t, uint32_t button,
uint32_t state) {
(void)serial; (void)t;
record(state == WL_POINTER_BUTTON_STATE_PRESSED ? PRESS : RELEASE, p, NULL, button);
}
#include "pointer.inc"
#include "pen.inc"
static void pen(int flags, double x) {
struct input_msg m = {13, flags, (int)(x * INPUT_SPACE_W / 2000 * 256), 100 * 256, 0};
nevents = 0;
deliver_pen(&m);
}
static void focus(int i, struct wl_resource *pointer, struct wl_resource *surface) {
assert(i < nevents && events[i].kind == ENTER && events[i].pointer == pointer && events[i].surface == surface);
}
static void left(int i, struct wl_resource *pointer, struct wl_resource *surface) {
assert(i < nevents && events[i].kind == LEAVE && events[i].pointer == pointer && events[i].surface == surface);
}
static void button(int i, int kind, uint32_t code) {
assert(i < nevents && events[i].kind == kind && events[i].pointer == &pointer_a && events[i].button == code);
}
static void hover_away(void) {
assert(!g_grab);
pen(PEN_IN, 1500);
assert(nevents == 2);
left(0, &pointer_a, &surface_a); focus(1, &pointer_b, &surface_b);
}
int main(int argc, char **argv) {
assert(argc == 2);
const char *c = argv[1];
pen(PEN_IN, 500);
assert(nevents == 1);
focus(0, &pointer_a, &surface_a);
pen(PEN_IN | PEN_TIP, 500);
assert(nevents == 1 && g_grab == &a);
button(0, PRESS, BTN_LEFT);
if (!strcmp(c, "side")) {
pen(PEN_IN | PEN_TIP | PEN_RIGHT, 600);
assert(nevents == 1 && g_grab == &a);
button(0, PRESS, BTN_RIGHT);
pen(PEN_IN | PEN_TIP, 700);
assert(nevents == 1 && g_grab == &a);
button(0, RELEASE, BTN_RIGHT);
pen(PEN_IN | PEN_TIP, 1500);
assert(nevents == 0 && g_grab == &a);
pen(PEN_IN, 1500);
assert(nevents == 1);
button(0, RELEASE, BTN_LEFT);
} else if (!strcmp(c, "swap")) {
pen(PEN_IN | PEN_RIGHT, 600);
assert(nevents == 2 && g_grab == &a);
button(0, PRESS, BTN_RIGHT); button(1, RELEASE, BTN_LEFT);
pen(PEN_IN | PEN_RIGHT, 1500);
assert(nevents == 0 && g_grab == &a);
pen(PEN_IN, 1500);
assert(nevents == 1);
button(0, RELEASE, BTN_RIGHT);
} else if (!strcmp(c, "mouse")) {
pointer_button(BTN_RIGHT, 1);
pointer_button(BTN_RIGHT, 0);
pen(PEN_IN | PEN_TIP, 1500);
assert(nevents == 0 && g_grab == &a);
pen(PEN_IN, 1500);
assert(nevents == 1);
button(0, RELEASE, BTN_LEFT);
} else {
assert(!strcmp(c, "gone"));
pen(PEN_IN | PEN_TIP | PEN_RIGHT, 600);
assert(nevents == 1 && g_grab == &a);
dying = &a;
pen(PEN_IN | PEN_TIP, 700);
assert(nevents == 1 && !dying && !g_grab && !g_pen.grab);
button(0, RELEASE, BTN_RIGHT);
pen(PEN_IN, 700);
assert(nevents == 1);
button(0, RELEASE, BTN_LEFT);
}
hover_away();
puts("PASS");
}
+1
View File
@@ -70,6 +70,7 @@ static void send_toplevel_configure(struct surface *s) { (void)s; resizes++; }
#define stream_forget_all(...) ((void)0)
#define pointer_delta(...) ((void)0)
#define deliver_touch(m) ((void)0)
#define deliver_pen(m) ((void)0)
#define deliver_pointer(m) (pointers++)
static void on_vsync(int64_t ns) { assert(ns > 0); ticks++; }
static void vk_present_set_scale_mode(int mode, int alignment) {
+102
View File
@@ -0,0 +1,102 @@
import os
from pathlib import Path
import re
import shutil
import subprocess
import tempfile
import unittest
REPO = Path(__file__).resolve().parents[2]
SOURCE = REPO / 'app/src/main/cpp/waylandcomp/src/compositor.c'
PEN_STREAM = REPO / 'app/src/main/java/com/droiddeck/launcher/input/PenStream.kt'
def function(source, signature):
start = source.index(signature)
return source[start:source.index('\n}\n', start) + 3]
class PenEmulationTest(unittest.TestCase):
@classmethod
def setUpClass(cls):
compiler = shutil.which(os.environ.get('CC', 'cc'))
if compiler is None:
raise unittest.SkipTest('A host C compiler is required')
source = SOURCE.read_text()
cls.temp = tempfile.TemporaryDirectory(prefix='droiddeck-pen-emulation-')
cls.addClassCleanup(cls.temp.cleanup)
folder = Path(cls.temp.name)
start = source.index('struct input_msg {')
(folder / 'input_msg.inc').write_text(source[start:source.index('\n', start) + 1])
start = source.index('#define PEN_IN ')
end = source.index('static struct pen_state g_pen;\n', start)
(folder / 'pen_state.inc').write_text(source[start:source.index('\n', end) + 1])
(folder / 'pen.inc').write_text(''.join(function(source, signature) for signature in (
'static int pointer_locked_for_cursor(void) {',
'static void input_output_to_scene(double px, double py, double *x, double *y) {',
'static int pen_buttons(int flags) {',
'static void pen_release(int buttons) {',
'static void deliver_pen(const struct input_msg *m) {')))
start = source.index('#define for_each_pointer_of(client, sp)')
(folder / 'pointer.inc').write_text(
function(source, 'static struct seat_pointer *pointer_for(struct wl_client *client) {')
+ source[start:source.index('#define for_each_keyboard_of', start)]
+ ''.join(function(source, signature) for signature in (
'static void pointer_focus(struct wl_resource *target, wl_fixed_t fx, wl_fixed_t fy) {',
'static void pointer_input(double x, double y, int relative, uint32_t button, int pressed) {',
'static void pointer_event(double x, double y, uint32_t button, int pressed) {',
'static void pointer_button(uint32_t button, int pressed) {')))
cls.binary = folder / 'pen-test'
cls.pointer_binary = folder / 'pen-pointer-test'
for harness, binary in (('pen_emulation_harness.c', cls.binary),
('pen_pointer_harness.c', cls.pointer_binary)):
subprocess.run([compiler, '-std=c11', '-Wall', '-Wextra', '-Werror', '-I', str(folder),
str(REPO / 'tools/tests' / harness),
'-o', str(binary)], check=True, capture_output=True, text=True)
def check_case(self, case, binary=None):
result = subprocess.run([str(binary or self.binary), case], capture_output=True, text=True)
self.assertEqual(result.returncode, 0, result.stdout + result.stderr)
def test_flags_match_the_kotlin_pen_stream(self):
native = dict(re.findall(r'^#define (PEN_[A-Z]+) \(1 << (\d+)\)$', SOURCE.read_text(), re.MULTILINE))
kotlin = dict(re.findall(r'^\s*const val (PEN_[A-Z]+) = 1 shl (\d+)$', PEN_STREAM.read_text(), re.MULTILINE))
self.assertEqual({'PEN_IN', 'PEN_TIP', 'PEN_RIGHT', 'PEN_MIDDLE'}, set(native))
self.assertEqual(native, kotlin)
def test_hover_moves_the_pointer_in_scene_coordinates(self):
self.check_case('hover')
def test_tip_presses_and_releases_the_left_button_once(self):
self.check_case('tip')
def test_buttons_press_after_motion_and_before_releases(self):
self.check_case('buttons')
def test_leaving_proximity_releases_every_button_without_motion(self):
self.check_case('out')
def test_pointer_lock_blocks_motion_but_passes_buttons(self):
self.check_case('lock')
def test_lost_records_still_release_held_buttons(self):
self.check_case('lost')
def test_relative_origin_resets_when_proximity_starts_and_ends(self):
self.check_case('raw')
def test_side_button_release_keeps_the_grab(self):
self.check_case('side', self.pointer_binary)
def test_swapping_buttons_in_one_record_keeps_the_grab(self):
self.check_case('swap', self.pointer_binary)
def test_a_button_release_from_another_pointer_keeps_the_grab(self):
self.check_case('mouse', self.pointer_binary)
def test_a_window_closed_by_a_button_release_is_not_grabbed_again(self):
self.check_case('gone', self.pointer_binary)
if __name__ == '__main__':
unittest.main()
+39
View File
@@ -0,0 +1,39 @@
from pathlib import Path
import re
import unittest
import xml.etree.ElementTree as ET
ROOT = Path(__file__).resolve().parents[2]
RES = ROOT / "app/src/main/res"
def strings(path):
return {
element.attrib["name"]: "".join(element.itertext())
for element in ET.parse(path).getroot()
if element.tag == "string" and element.get("translatable") != "false"
}
class PenLocalizationTest(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.required = [key for key in strings(RES / "values/strings.xml") if key.startswith("mode_pen")]
language_source = (ROOT / "app/src/main/java/com/droiddeck/launcher/core/AppLanguage.kt").read_text(encoding="utf-8")
tags = re.findall(r'"([a-zA-Z0-9-]+)"', re.search(r"val supported:.*?listOf\((.*?)\)", language_source, re.S).group(1))
special = {"id": "in", "es-419": "b+es+419", "pt-BR": "pt"}
cls.locales = {
tag: strings(RES / f"values-{special.get(tag, tag.replace('-', '-r'))}/strings.xml")
for tag in tags if tag != "en"
}
def test_every_supported_language_translates_the_pen_settings(self):
for locale, translated in self.locales.items():
for key in self.required:
with self.subTest(locale=locale, key=key):
self.assertTrue(translated.get(key, "").strip(), "Missing translation would fall back to English")
if __name__ == "__main__":
unittest.main()