diff --git a/pkgs/niri-touchscreen-gestures/default.nix b/pkgs/niri-touchscreen-gestures/default.nix index d0cbbef..90281b2 100644 --- a/pkgs/niri-touchscreen-gestures/default.nix +++ b/pkgs/niri-touchscreen-gestures/default.nix @@ -19,6 +19,8 @@ python3.pkgs.buildPythonApplication { evdev pydantic pydantic-settings + typing-extensions + ], ]; meta = { diff --git a/pkgs/niri-touchscreen-gestures/example-config.toml b/pkgs/niri-touchscreen-gestures/example-config.toml index f36ce49..3f4a363 100644 --- a/pkgs/niri-touchscreen-gestures/example-config.toml +++ b/pkgs/niri-touchscreen-gestures/example-config.toml @@ -4,25 +4,16 @@ # N = 2, 3, 4 (or more) # direction = up | down | left | right # -# Values are arrays of command arrays. Each inner array is passed to -# subprocess.run as-is. +# Values are strings representing the niri action to perform. -"2-finger-up" = [ - ["niri", "msg", "action", "focus-workspace-up"], -] +"3-finger-up" = "FocusWorkspaceDown" -"2-finger-down" = [ - ["niri", "msg", "action", "focus-workspace-down"], -] +"3-finger-down" = "FocusWorkspaceUp" -"3-finger-left" = [ - ["niri", "msg", "action", "focus-column-left"], -] +"3-finger-left" = "FocusColumnRight" -"3-finger-right" = [ - ["niri", "msg", "action", "focus-column-right"], -] +"3-finger-right" = "FocusColumnLeft" -"4-finger-up" = [ - ["niri", "msg", "action", "toggle-overview"], -] +"4-finger-up" = "ToggleOverview" + +"4-finger-down" = "ToggleOverview" diff --git a/pkgs/niri-touchscreen-gestures/niri_touchscreen_gestures/__init__.py b/pkgs/niri-touchscreen-gestures/niri_touchscreen_gestures/__init__.py index 58d5186..3dc1f76 100644 --- a/pkgs/niri-touchscreen-gestures/niri_touchscreen_gestures/__init__.py +++ b/pkgs/niri-touchscreen-gestures/niri_touchscreen_gestures/__init__.py @@ -1,29 +1 @@ -"""niri-touchscreen-gestures — detect multi-finger touchscreen swipes and dispatch niri actions.""" - __version__ = "0.1.0" - - -import logging - -from niri_touchscreen_gestures.cli import get_parser, process_args, run_commands -from niri_touchscreen_gestures.detector import GestureDetector - -logging.basicConfig(level=logging.INFO) - -logger = logging.getLogger(__name__) - - -def run() -> None: - logger.info("niri-touchscreen-gestures: starting") - config, dev, threshold = process_args(get_parser().parse_args()) - - detector = GestureDetector( - config, - threshold=threshold, - ) - - for ev in dev.read_loop(): - result = detector.handle_event(ev) - if result: - logger.info(f"niri-touchscreen-gestures: executing {result}") - run_commands(result) diff --git a/pkgs/niri-touchscreen-gestures/niri_touchscreen_gestures/cli.py b/pkgs/niri-touchscreen-gestures/niri_touchscreen_gestures/argparser.py similarity index 51% rename from pkgs/niri-touchscreen-gestures/niri_touchscreen_gestures/cli.py rename to pkgs/niri-touchscreen-gestures/niri_touchscreen_gestures/argparser.py index e2e5727..94a01be 100644 --- a/pkgs/niri-touchscreen-gestures/niri_touchscreen_gestures/cli.py +++ b/pkgs/niri-touchscreen-gestures/niri_touchscreen_gestures/argparser.py @@ -5,30 +5,15 @@ from __future__ import annotations import argparse import logging import os -import subprocess -import sys -from cmath import log import evdev from .config import GestureConfig -from .detector import find_touchscreen +from .detector.touchscreen_identifier import identify_touchscreen_via_evdev logger = logging.getLogger(__name__) -def run_commands(cmds: list[list[str]]) -> None: - for cmd in cmds: - if not cmd: - continue - try: - subprocess.run(cmd, check=False) - except Exception as exc: - logger.info( - f"niri-touchscreen-gestures: failed to run {cmd}: {exc}", - ) - - def get_parser() -> argparse.ArgumentParser: parser = argparse.ArgumentParser( description="Touchscreen gesture detector for niri" @@ -51,26 +36,27 @@ def get_parser() -> argparse.ArgumentParser: return parser -def process_args(args: argparse.Namespace): +def process_args( + args: argparse.Namespace, +) -> tuple[GestureConfig, evdev.InputDevice, int]: if not os.path.exists(args.config): raise FileNotFoundError(f"Config not found: {args.config}") logger.info(f"niri-touchscreen-gestures: using config {args.config}") config = GestureConfig.from_toml(args.config) - device_path = args.device or find_touchscreen() - if not device_path: - raise RuntimeError( - "No touchscreen found. Pass --device /dev/input/eventN explicitly.", - ) - logger.info(f"niri-touchscreen-gestures: using device {device_path}") - try: - dev = evdev.InputDevice(device_path) - except PermissionError: - raise PermissionError( - f"Permission denied opening {device_path}.\n" - "Add your user to the 'input' group.", - ) - logger.info(f"niri-touchscreen-gestures: opened device {device_path}") + if args.device is None: + devices = list(identify_touchscreen_via_evdev()) + if len(devices) > 1: + raise RuntimeError( + f"Multiple touchscreens found ({devices=}). Pass --device /dev/input/eventN explicitly.", + ) + if len(devices) == 0: + raise RuntimeError( + "No touchscreen found. Pass --device /dev/input/eventN explicitly.", + ) + device = devices[0] + else: + device = evdev.InputDevice(args.device) - logger.info(f"niri-touchscreen-gestures: listening on {device_path}") - return config, dev, args.threshold + logger.info(f"niri-touchscreen-gestures: using device {device.path}") + return config, device, args.threshold diff --git a/pkgs/niri-touchscreen-gestures/niri_touchscreen_gestures/config.py b/pkgs/niri-touchscreen-gestures/niri_touchscreen_gestures/config.py index 1373ece..5940eab 100644 --- a/pkgs/niri-touchscreen-gestures/niri_touchscreen_gestures/config.py +++ b/pkgs/niri-touchscreen-gestures/niri_touchscreen_gestures/config.py @@ -2,26 +2,28 @@ from __future__ import annotations -from typing import Any, Dict, List +from typing import Any import tomllib from pydantic import BaseModel, Field +from niri_touchscreen_gestures.nirictl import SupportedActions + class GestureConfig(BaseModel): """Configuration mapping gesture keys (e.g. "3-finger-up") to lists of command arrays. Any key is accepted; values must be arrays of string arrays. """ - gestures: Dict[str, List[List[str]]] = Field(default_factory=dict) + gestures: dict[str, SupportedActions] = Field(default_factory=dict) model_config = {"extra": "allow"} @classmethod def from_toml(cls, path: str) -> GestureConfig: with open(path, "rb") as f: - data: Dict[str, Any] = tomllib.load(f) + data: dict[str, Any] = tomllib.load(f) return cls(gestures=data) - def get(self, key: str) -> List[List[str]] | None: + def get(self, key: str) -> SupportedActions | None: return self.gestures.get(key) diff --git a/pkgs/niri-touchscreen-gestures/niri_touchscreen_gestures/detector.py b/pkgs/niri-touchscreen-gestures/niri_touchscreen_gestures/detector.py deleted file mode 100644 index a1d6528..0000000 --- a/pkgs/niri-touchscreen-gestures/niri_touchscreen_gestures/detector.py +++ /dev/null @@ -1,136 +0,0 @@ -"""Gesture detection logic using evdev multitouch events.""" - -from __future__ import annotations - -import json -import logging -import subprocess -from typing import Any, Optional - -import evdev -from evdev import ecodes as e - -from .config import GestureConfig - -logger = logging.getLogger(__name__) - - -class GestureDetector: - """Detect multi-finger swipes from a touchscreen evdev device.""" - - def __init__( - self, config: GestureConfig | dict[str, Any], threshold: int = 60 - ) -> None: - if isinstance(config, GestureConfig): - self.config = config.gestures - else: - self.config = config - self.threshold = threshold - self.slots: dict[int, dict[str, Any]] = {} - self.current_slot = 0 - for i in range(10): - self.slots[i] = { - "tracking_id": -1, - "start_x": 0, - "start_y": 0, - "last_x": 0, - "last_y": 0, - } - - def _classify(self, dx: int, dy: int) -> Optional[str]: - ax, ay = abs(dx), abs(dy) - if max(ax, ay) < self.threshold: - return None - if ax > ay: - return "right" if dx > 0 else "left" - return "down" if dy > 0 else "up" - - def handle_event(self, ev: evdev.InputEvent) -> Optional[list[list[str]]]: - """Return a list of command arrays to execute, or None.""" - if ev.type == e.EV_ABS: - slot = self.slots.get(self.current_slot) - if slot is None: - return None - - if ev.code == e.ABS_MT_SLOT: - self.current_slot = ev.value - elif ev.code == e.ABS_MT_TRACKING_ID: - if ev.value == -1: - # Finger release — check for gesture - if slot["tracking_id"] != -1: - dx = slot["last_x"] - slot["start_x"] - dy = slot["last_y"] - slot["start_y"] - direction = self._classify(dx, dy) - if direction: - # Count how many fingers were active (this one is releasing, plus any still tracked) - finger_count = 1 + sum( - 1 for s in self.slots.values() if s["tracking_id"] != -1 - ) - key = f"{finger_count}-finger-{direction}" - logger.info(f"Detected {key=}") - cmds = self.config.get(key) - if cmds: - slot.update( - { - "tracking_id": -1, - "start_x": 0, - "start_y": 0, - "last_x": 0, - "last_y": 0, - } - ) - return cmds - slot["tracking_id"] = -1 - else: - slot["tracking_id"] = ev.value - slot["start_x"] = slot["last_x"] = 0 - slot["start_y"] = slot["last_y"] = 0 - elif ev.code == e.ABS_MT_POSITION_X: - if slot["start_x"] == 0: - slot["start_x"] = ev.value - slot["last_x"] = ev.value - elif ev.code == e.ABS_MT_POSITION_Y: - if slot["start_y"] == 0: - slot["start_y"] = ev.value - slot["last_y"] = ev.value - - return None - - -def find_touchscreen() -> Optional[str]: - """Return the first evdev device node that looks like a touchscreen. - - Checks for multitouch slot/tracking id in evdev capabilities, and falls back - to udev property ID_INPUT_TOUCHSCREEN=1 for devices that report touch via - ABS_MT_TRACKING_ID without an explicit SLOT axis (common on some Framework - laptop touchscreens). - """ - # First pass: strict evdev MT check - for path in evdev.list_devices(): - try: - dev = evdev.InputDevice(path) - caps = dev.capabilities() - abs_caps = caps.get(e.EV_ABS, []) - codes = [c[0] if isinstance(c, tuple) else c for c in abs_caps] - if e.ABS_MT_SLOT in codes or e.ABS_MT_TRACKING_ID in codes: - return path - except Exception: - continue - - # Second pass: udev property fallback via udevadm (no extra Python deps) - try: - out = subprocess.run( - ["udevadm", "info", "--export-db", "--json=short"], - capture_output=True, - text=True, - check=False, - ) - if out.returncode == 0: - for rec in json.loads(out.stdout or "[]"): - if rec.get("ID_INPUT_TOUCHSCREEN") == "1": - node = rec.get("DEVNAME") or rec.get("DEVPATH") - if node and node.startswith("/dev/input/event"): - return node - except Exception: - pass - return None diff --git a/pkgs/niri-touchscreen-gestures/niri_touchscreen_gestures/detector/__init__.py b/pkgs/niri-touchscreen-gestures/niri_touchscreen_gestures/detector/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/pkgs/niri-touchscreen-gestures/niri_touchscreen_gestures/detector/base_types.py b/pkgs/niri-touchscreen-gestures/niri_touchscreen_gestures/detector/base_types.py new file mode 100644 index 0000000..9284b84 --- /dev/null +++ b/pkgs/niri-touchscreen-gestures/niri_touchscreen_gestures/detector/base_types.py @@ -0,0 +1,26 @@ +from typing import Literal, TypedDict + +from pydantic import BaseModel, Field + +FingerNumber = int + +SwipeDirection = Literal["up", "down", "left", "right"] + + +class Slot(TypedDict): + """Per-slot tracking information.""" + + tracking_id: int + start_x: int + start_y: int + last_x: int + last_y: int + + +class DetectorState(BaseModel): + """Immutable snapshot of detector state.""" + + slots: dict[FingerNumber, Slot] = Field(default_factory=dict) + current_slot: FingerNumber = FingerNumber(0) + + model_config = {"frozen": True} diff --git a/pkgs/niri-touchscreen-gestures/niri_touchscreen_gestures/detector/classifiers.py b/pkgs/niri-touchscreen-gestures/niri_touchscreen_gestures/detector/classifiers.py new file mode 100644 index 0000000..c46b88d --- /dev/null +++ b/pkgs/niri-touchscreen-gestures/niri_touchscreen_gestures/detector/classifiers.py @@ -0,0 +1,19 @@ +from niri_touchscreen_gestures.detector.base_types import ( + FingerNumber, + Slot, + SwipeDirection, +) + + +def count_active_fingers(slots: dict[FingerNumber, Slot]) -> int: + return sum(1 for s in slots.values() if s["tracking_id"] != -1) + + +def classify_direction(dx: int, dy: int, threshold: int) -> SwipeDirection | None: + """Pure direction classifier.""" + ax, ay = abs(dx), abs(dy) + if max(ax, ay) < threshold: + return None + if ax > ay: + return "right" if dx > 0 else "left" + return "down" if dy > 0 else "up" diff --git a/pkgs/niri-touchscreen-gestures/niri_touchscreen_gestures/detector/gestures.py b/pkgs/niri-touchscreen-gestures/niri_touchscreen_gestures/detector/gestures.py new file mode 100644 index 0000000..8b8e11a --- /dev/null +++ b/pkgs/niri-touchscreen-gestures/niri_touchscreen_gestures/detector/gestures.py @@ -0,0 +1,96 @@ +import logging + +import evdev + +from niri_touchscreen_gestures.config import GestureConfig +from niri_touchscreen_gestures.detector.base_types import ( + DetectorState, + FingerNumber, + Slot, +) +from niri_touchscreen_gestures.detector.classifiers import ( + classify_direction, + count_active_fingers, +) +from niri_touchscreen_gestures.detector.inits import empty_slot, initial_state +from niri_touchscreen_gestures.nirictl import SupportedActions + +logger = logging.getLogger(__name__) + + +class GestureDetector: + def __init__(self, config: GestureConfig, threshold: int = 60) -> None: + self.config = config + self.threshold = threshold + self._state = initial_state() + + def handle_event(self, ev: evdev.InputEvent) -> SupportedActions | None: + """Return action or None. Mutates internal state.""" + self._state, action = _handle_event( + self._state, ev, self.config, self.threshold + ) + return action + + +def _handle_event( + state: DetectorState, + ev: evdev.InputEvent, + config: GestureConfig, + threshold: int, +) -> tuple[DetectorState, SupportedActions | None]: + """Pure event handler. Returns (new_state, action_or_None).""" + if ev.type != evdev.ecodes.EV_ABS: + return state, None + + slot: Slot = state.slots.get(state.current_slot, empty_slot()).copy() + new_slots: dict[FingerNumber, Slot] = {k: v.copy() for k, v in state.slots.items()} + + if ev.code == evdev.ecodes.ABS_MT_SLOT: + return DetectorState(slots=new_slots, current_slot=FingerNumber(ev.value)), None + + if ev.code == evdev.ecodes.ABS_MT_TRACKING_ID: + if ev.value == -1: + if slot["tracking_id"] != -1: + dx = slot["last_x"] - slot["start_x"] + dy = slot["last_y"] - slot["start_y"] + direction = classify_direction(dx, dy, threshold) + if direction: + finger_count = count_active_fingers(state.slots) + key = f"{finger_count}-finger-{direction}" + logger.info(f"Detected {key=}") + action = config.get(key) + if action: + new_slots[state.current_slot] = empty_slot() + return ( + DetectorState( + slots=new_slots, current_slot=state.current_slot + ), + action, + ) + slot = empty_slot() + else: + slot = Slot( + tracking_id=ev.value, + start_x=0, + start_y=0, + last_x=0, + last_y=0, + ) + new_slots[state.current_slot] = slot + return DetectorState(slots=new_slots, current_slot=state.current_slot), None + + if ev.code == evdev.ecodes.ABS_MT_POSITION_X: + if slot["start_x"] == 0: + slot["start_x"] = ev.value + slot["last_x"] = ev.value + new_slots[state.current_slot] = slot + return DetectorState(slots=new_slots, current_slot=state.current_slot), None + + if ev.code == evdev.ecodes.ABS_MT_POSITION_Y: + if slot["start_y"] == 0: + slot["start_y"] = ev.value + slot["last_y"] = ev.value + new_slots[state.current_slot] = slot + return DetectorState(slots=new_slots, current_slot=state.current_slot), None + + return state, None diff --git a/pkgs/niri-touchscreen-gestures/niri_touchscreen_gestures/detector/inits.py b/pkgs/niri-touchscreen-gestures/niri_touchscreen_gestures/detector/inits.py new file mode 100644 index 0000000..c5a4fb4 --- /dev/null +++ b/pkgs/niri-touchscreen-gestures/niri_touchscreen_gestures/detector/inits.py @@ -0,0 +1,22 @@ +from niri_touchscreen_gestures.detector.base_types import ( + DetectorState, + FingerNumber, + Slot, +) + + +def empty_slot() -> Slot: + return Slot( + tracking_id=-1, + start_x=0, + start_y=0, + last_x=0, + last_y=0, + ) + + +def initial_state() -> DetectorState: + return DetectorState( + slots={FingerNumber(i): empty_slot() for i in range(10)}, + current_slot=FingerNumber(0), + ) diff --git a/pkgs/niri-touchscreen-gestures/niri_touchscreen_gestures/detector/touchscreen_identifier.py b/pkgs/niri-touchscreen-gestures/niri_touchscreen_gestures/detector/touchscreen_identifier.py new file mode 100644 index 0000000..74b4ca5 --- /dev/null +++ b/pkgs/niri-touchscreen-gestures/niri_touchscreen_gestures/detector/touchscreen_identifier.py @@ -0,0 +1,15 @@ +from collections.abc import Iterable + +import evdev + + +def identify_touchscreen_via_evdev() -> Iterable[evdev.InputDevice]: + for path in evdev.list_devices(): + dev = evdev.InputDevice(path) + name_lower = dev.name.lower() + if "touchscreen" in name_lower or "touch panel" in name_lower: + yield dev + if caps := dev.capabilities(absinfo=False).get(evdev.ecodes.EV_ABS): + if evdev.ecodes.ABS_X in caps and evdev.ecodes.ABS_Y in caps: + if ("mouse" not in name_lower) and ("touchpad" not in name_lower): + yield dev diff --git a/pkgs/niri-touchscreen-gestures/niri_touchscreen_gestures/main.py b/pkgs/niri-touchscreen-gestures/niri_touchscreen_gestures/main.py index 47f428f..3a03e73 100644 --- a/pkgs/niri-touchscreen-gestures/niri_touchscreen_gestures/main.py +++ b/pkgs/niri-touchscreen-gestures/niri_touchscreen_gestures/main.py @@ -1,4 +1,29 @@ -from niri_touchscreen_gestures import run +import logging + +from niri_touchscreen_gestures.argparser import get_parser, process_args +from niri_touchscreen_gestures.detector.gestures import GestureDetector +from niri_touchscreen_gestures.nirictl import send_niri_action + +logging.basicConfig(level=logging.INFO) + +logger = logging.getLogger(__name__) + + +def main_runtime() -> None: + logger.info("niri-touchscreen-gestures: starting") + config, dev, threshold = process_args(get_parser().parse_args()) + + detector = GestureDetector( + config, + threshold=threshold, + ) + + for ev in dev.read_loop(): + result = detector.handle_event(ev) + if result: + logger.info(f"niri-touchscreen-gestures: event {ev}, result {result}") + send_niri_action(result) + if __name__ == "__main__": - run() + main_runtime() diff --git a/pkgs/niri-touchscreen-gestures/niri_touchscreen_gestures/nirictl.py b/pkgs/niri-touchscreen-gestures/niri_touchscreen_gestures/nirictl.py new file mode 100644 index 0000000..d1aca3f --- /dev/null +++ b/pkgs/niri-touchscreen-gestures/niri_touchscreen_gestures/nirictl.py @@ -0,0 +1,68 @@ +import json +import os +import select +import socket +from contextlib import contextmanager +from typing import Any, Literal, TypeAlias + +SupportedActions: TypeAlias = Literal[ + "FocusWorkspaceUp", + "FocusWorkspaceDown", + "FocusColumnLeft", + "FocusColumnRight", + "ToggleOverview", +] + + +def get_socket_path(): + """Get the Niri socket path from the environment, raising if not set.""" + socket_path = os.environ.get("NIRI_SOCKET") + if not socket_path: + raise RuntimeError("Niri is not running or NIRI_SOCKET is not set.") + + return socket_path + + +@contextmanager +def niri_socket(): + """Context manager for the Niri socket.""" + socket_path = get_socket_path() + s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) + s.connect(socket_path) + s.setblocking(False) + try: + yield s + finally: + s.close() + + +def send_niri_command(command: dict[str, Any]) -> dict[str, Any]: + """Send a raw JSON command to Niri and return the parsed reply.""" + with niri_socket() as s: + s.sendall((json.dumps(command) + "\n").encode("utf-8")) + + # wait for data (up to 5 s) + ready, _, _ = select.select([s], [], [], 5) + if not ready: + raise TimeoutError("No response from Niri") + + # read everything available + data = bytearray() + while True: + try: + chunk = s.recv(4096) + if chunk: + data += chunk + else: + break + except BlockingIOError: + break + + return json.loads(data.decode("utf-8")) + + +def send_niri_action(action: SupportedActions) -> bool: + """Send an Action command""" + request = {"Action": {action: {}}} + reply = send_niri_command(request) + return reply.get("Ok", "Handled").lower() == "handled" diff --git a/pkgs/niri-touchscreen-gestures/pyproject.toml b/pkgs/niri-touchscreen-gestures/pyproject.toml index 0a0b0df..ffc55b7 100644 --- a/pkgs/niri-touchscreen-gestures/pyproject.toml +++ b/pkgs/niri-touchscreen-gestures/pyproject.toml @@ -14,6 +14,7 @@ dependencies = [ "evdev>=1.7", "pydantic>=2.0", "pydantic-settings>=2.0", + "typing-extensions>=4.0", ] [project.scripts] diff --git a/pkgs/niri-touchscreen-gestures/tests/detector/test_gestures.py b/pkgs/niri-touchscreen-gestures/tests/detector/test_gestures.py new file mode 100644 index 0000000..3d52578 --- /dev/null +++ b/pkgs/niri-touchscreen-gestures/tests/detector/test_gestures.py @@ -0,0 +1,82 @@ +from niri_touchscreen_gestures.detector.classifiers import count_active_fingers + + +class TestClassifiers: + def test_count_fingers(self): + assert ( + count_active_fingers( + { + 0: { + "tracking_id": 167, + "start_x": 2404, + "start_y": 776, + "last_x": 1946, + "last_y": 717, + }, + 1: { + "tracking_id": -1, + "start_x": 0, + "start_y": 0, + "last_x": 0, + "last_y": 0, + }, + 2: { + "tracking_id": -1, + "start_x": 0, + "start_y": 0, + "last_x": 0, + "last_y": 0, + }, + 3: { + "tracking_id": -1, + "start_x": 0, + "start_y": 0, + "last_x": 0, + "last_y": 0, + }, + 4: { + "tracking_id": -1, + "start_x": 0, + "start_y": 0, + "last_x": 0, + "last_y": 0, + }, + 5: { + "tracking_id": -1, + "start_x": 0, + "start_y": 0, + "last_x": 0, + "last_y": 0, + }, + 6: { + "tracking_id": -1, + "start_x": 0, + "start_y": 0, + "last_x": 0, + "last_y": 0, + }, + 7: { + "tracking_id": -1, + "start_x": 0, + "start_y": 0, + "last_x": 0, + "last_y": 0, + }, + 8: { + "tracking_id": -1, + "start_x": 0, + "start_y": 0, + "last_x": 0, + "last_y": 0, + }, + 9: { + "tracking_id": -1, + "start_x": 0, + "start_y": 0, + "last_x": 0, + "last_y": 0, + }, + } + ) + == 1 + )