Files
DroidDeck/tools/tests/pen_emulation_harness.c
T
alien2003andalien2003 decab6ea90 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>
2026-11-15 09:38:01 -05:00

159 lines
5.7 KiB
C

#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");
}