Files
DroidDeck/tools/tests/test_pen_emulation.py
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

103 lines
4.7 KiB
Python

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()