From 9077f7e0a73bc8dcbd861223b6c3ef088a03f893 Mon Sep 17 00:00:00 2001 From: Ari Sosnovsky Date: Sat, 11 Jul 2026 13:29:00 -0400 Subject: [PATCH] initial work on touchscreen adapter --- configs/niri/shared/general.kdl | 6 + flake.nix | 7 + modules/core/default.nix | 2 + modules/lib.nix | 1 + pkgs/ABOUTME.md | 1 + pkgs/niri-touchscreen-gestures/.gitignore | 1 + pkgs/niri-touchscreen-gestures/README.md | 39 +++++ pkgs/niri-touchscreen-gestures/default.nix | 32 +++++ .../example-config.toml | 28 ++++ .../niri_touchscreen_gestures/__init__.py | 29 ++++ .../niri_touchscreen_gestures/cli.py | 76 ++++++++++ .../niri_touchscreen_gestures/config.py | 27 ++++ .../niri_touchscreen_gestures/detector.py | 136 ++++++++++++++++++ .../niri_touchscreen_gestures/main.py | 4 + pkgs/niri-touchscreen-gestures/pyproject.toml | 23 +++ 15 files changed, 412 insertions(+) create mode 100644 pkgs/niri-touchscreen-gestures/.gitignore create mode 100644 pkgs/niri-touchscreen-gestures/README.md create mode 100644 pkgs/niri-touchscreen-gestures/default.nix create mode 100644 pkgs/niri-touchscreen-gestures/example-config.toml create mode 100644 pkgs/niri-touchscreen-gestures/niri_touchscreen_gestures/__init__.py create mode 100644 pkgs/niri-touchscreen-gestures/niri_touchscreen_gestures/cli.py create mode 100644 pkgs/niri-touchscreen-gestures/niri_touchscreen_gestures/config.py create mode 100644 pkgs/niri-touchscreen-gestures/niri_touchscreen_gestures/detector.py create mode 100644 pkgs/niri-touchscreen-gestures/niri_touchscreen_gestures/main.py create mode 100644 pkgs/niri-touchscreen-gestures/pyproject.toml diff --git a/configs/niri/shared/general.kdl b/configs/niri/shared/general.kdl index 0769f26..bd5817d 100644 --- a/configs/niri/shared/general.kdl +++ b/configs/niri/shared/general.kdl @@ -8,6 +8,12 @@ input { tap natural-scroll } + + // Absolute map for the built-in panel (scale 1.5). Without this, touch + // can miss windows when logical/physical sizes diverge. + touch { + map-to-output "eDP-1" + } } overview { diff --git a/flake.nix b/flake.nix index 53165fd..4a8e7bb 100644 --- a/flake.nix +++ b/flake.nix @@ -134,6 +134,13 @@ cargo rust-analyzer nushell + # Python tooling for niri-touchscreen-gestures script + python3 + python3Packages.evdev + python3Packages.tomli + python3Packages.pydantic + python3Packages.pydantic-settings + libinput ]; shellHook = '' export PATH=$PATH:$(pwd)/bin diff --git a/modules/core/default.nix b/modules/core/default.nix index dc55b16..9f0c800 100644 --- a/modules/core/default.nix +++ b/modules/core/default.nix @@ -19,6 +19,8 @@ ds4 = final.callPackage ../../pkgs/ds4 { }; ds4-rocm = final.callPackage ../../pkgs/ds4 { backend = "rocm"; }; ds4-cuda = final.callPackage ../../pkgs/ds4 { backend = "cuda"; }; + # Touchscreen gesture bridge for niri, see pkgs/niri-touchscreen-gestures. + niri-touchscreen-gestures = final.callPackage ../../pkgs/niri-touchscreen-gestures { }; }) (final: prev: { pythonPackagesExtensions = prev.pythonPackagesExtensions ++ [ diff --git a/modules/lib.nix b/modules/lib.nix index f2f2819..65abaf5 100644 --- a/modules/lib.nix +++ b/modules/lib.nix @@ -29,6 +29,7 @@ let # CPU variant only here (buildable on any system); the GPU variants # need their toolchains and are wired in modules/core/default.nix. ds4 = final.callPackage ../pkgs/ds4 { }; + niri-touchscreen-gestures = final.callPackage ../pkgs/niri-touchscreen-gestures { }; }) ]; } diff --git a/pkgs/ABOUTME.md b/pkgs/ABOUTME.md index 930e1cf..b8d62a1 100644 --- a/pkgs/ABOUTME.md +++ b/pkgs/ABOUTME.md @@ -9,6 +9,7 @@ Packages here are exposed via an overlay defined in `modules/core/default.nix`. |---|---| | `grok-cli/` | xAI Grok CLI — prebuilt static binary, fetched per-platform. Exposed as `pkgs.grok-cli`. | | `ds4/` | DwarfStar (`antirez/ds4`) DeepSeek V4 local inference engine, built from source. Backend-parameterized (`backend = "cpu" \| "rocm" \| "cuda"`); tracks `main` and builds per-host. Exposed as `pkgs.ds4` (cpu, default), `pkgs.ds4-rocm` (Strix Halo / gfx1151), `pkgs.ds4-cuda`. Installs the `ds4`, `ds4-server`, `ds4-bench`, `ds4-eval`, `ds4-agent` binaries plus a `ds4-download-model` helper for fetching GGUF weights. Model weights are a runtime concern and not packaged. | +| `niri-touchscreen-gestures/` | Python daemon that reads multi-touch events from a touchscreen via `evdev`, detects 2/3/4-finger swipes (up/down/left/right), and dispatches configurable `niri msg action` commands. Configured via TOML; exposed as `pkgs.niri-touchscreen-gestures`. | ## Adding a New Package diff --git a/pkgs/niri-touchscreen-gestures/.gitignore b/pkgs/niri-touchscreen-gestures/.gitignore new file mode 100644 index 0000000..bee8a64 --- /dev/null +++ b/pkgs/niri-touchscreen-gestures/.gitignore @@ -0,0 +1 @@ +__pycache__ diff --git a/pkgs/niri-touchscreen-gestures/README.md b/pkgs/niri-touchscreen-gestures/README.md new file mode 100644 index 0000000..09ba154 --- /dev/null +++ b/pkgs/niri-touchscreen-gestures/README.md @@ -0,0 +1,39 @@ +# niri-touchscreen-gestures + +Detect 2/3/4-finger (or more) touchscreen swipes and dispatch arbitrary `niri msg action` commands (or any shell command). + +## Usage + +```bash +niri-touchscreen-gestures --config ~/.config/niri/gestures.toml +``` + +Optional flags: + +- `--threshold N` — minimum pixels of movement to register a swipe (default 60) +- `--device /dev/input/eventN` — explicit evdev device (auto-detection usually works on Framework laptops) + +## Config (TOML) + +Keys are `-finger-` where `N` is the finger count and `direction` is one of `up|down|left|right`. + +Values are arrays of command arrays; each inner array is passed verbatim to `subprocess.run`. + +Example (`~/.config/niri/gestures.toml`): + +```toml +"2-finger-up" = [["niri", "msg", "action", "focus-workspace-up"]] +"2-finger-down" = [["niri", "msg", "action", "focus-workspace-down"]] +"3-finger-left" = [["niri", "msg", "action", "focus-column-left"]] +"3-finger-right" = [["niri", "msg", "action", "focus-column-right"]] +"4-finger-up" = [["niri", "msg", "action", "toggle-overview"]] +``` + +## Requirements + +- User must be in the `input` group (or otherwise have read access to the touchscreen evdev node). +- Python ≥ 3.10, `evdev`, `pydantic`/`pydantic-settings`. + +## License + +MIT diff --git a/pkgs/niri-touchscreen-gestures/default.nix b/pkgs/niri-touchscreen-gestures/default.nix new file mode 100644 index 0000000..d0cbbef --- /dev/null +++ b/pkgs/niri-touchscreen-gestures/default.nix @@ -0,0 +1,32 @@ +{ lib +, python3 +, ... +}: + +python3.pkgs.buildPythonApplication { + pname = "niri-touchscreen-gestures"; + version = "0.1.0"; + + src = ./.; + + pyproject = true; + + build-system = with python3.pkgs; [ + hatchling + ]; + + dependencies = with python3.pkgs; [ + evdev + pydantic + pydantic-settings + ]; + + meta = { + description = "Touchscreen gesture detector for niri (2/3/4-finger swipes → niri actions)"; + homepage = "https://github.com/skykanin/nixos-setup"; + license = lib.licenses.mit; + sourceProvenance = [ lib.sourceTypes.fromSource ]; + platforms = lib.platforms.linux; + mainProgram = "niri-touchscreen-gestures"; + }; +} diff --git a/pkgs/niri-touchscreen-gestures/example-config.toml b/pkgs/niri-touchscreen-gestures/example-config.toml new file mode 100644 index 0000000..f36ce49 --- /dev/null +++ b/pkgs/niri-touchscreen-gestures/example-config.toml @@ -0,0 +1,28 @@ +# Example config for niri-touchscreen-gestures +# +# Keys are "-finger-" where: +# 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. + +"2-finger-up" = [ + ["niri", "msg", "action", "focus-workspace-up"], +] + +"2-finger-down" = [ + ["niri", "msg", "action", "focus-workspace-down"], +] + +"3-finger-left" = [ + ["niri", "msg", "action", "focus-column-left"], +] + +"3-finger-right" = [ + ["niri", "msg", "action", "focus-column-right"], +] + +"4-finger-up" = [ + ["niri", "msg", "action", "toggle-overview"], +] diff --git a/pkgs/niri-touchscreen-gestures/niri_touchscreen_gestures/__init__.py b/pkgs/niri-touchscreen-gestures/niri_touchscreen_gestures/__init__.py new file mode 100644 index 0000000..58d5186 --- /dev/null +++ b/pkgs/niri-touchscreen-gestures/niri_touchscreen_gestures/__init__.py @@ -0,0 +1,29 @@ +"""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/cli.py new file mode 100644 index 0000000..e2e5727 --- /dev/null +++ b/pkgs/niri-touchscreen-gestures/niri_touchscreen_gestures/cli.py @@ -0,0 +1,76 @@ +"""Command-line interface and main loop.""" + +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 + +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" + ) + parser.add_argument( + "--config", + required=True, + help="Path to TOML config file", + ) + parser.add_argument( + "--threshold", + type=int, + default=60, + help="Minimum pixels of movement to register a swipe (default: 60)", + ) + parser.add_argument( + "--device", + help="Explicit evdev device path (default: auto-detect first touchscreen)", + ) + return parser + + +def process_args(args: argparse.Namespace): + 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}") + + logger.info(f"niri-touchscreen-gestures: listening on {device_path}") + return config, dev, args.threshold diff --git a/pkgs/niri-touchscreen-gestures/niri_touchscreen_gestures/config.py b/pkgs/niri-touchscreen-gestures/niri_touchscreen_gestures/config.py new file mode 100644 index 0000000..1373ece --- /dev/null +++ b/pkgs/niri-touchscreen-gestures/niri_touchscreen_gestures/config.py @@ -0,0 +1,27 @@ +"""Pydantic configuration model for gesture mappings.""" + +from __future__ import annotations + +from typing import Any, Dict, List + +import tomllib +from pydantic import BaseModel, Field + + +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) + + 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) + return cls(gestures=data) + + def get(self, key: str) -> List[List[str]] | 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 new file mode 100644 index 0000000..a1d6528 --- /dev/null +++ b/pkgs/niri-touchscreen-gestures/niri_touchscreen_gestures/detector.py @@ -0,0 +1,136 @@ +"""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/main.py b/pkgs/niri-touchscreen-gestures/niri_touchscreen_gestures/main.py new file mode 100644 index 0000000..47f428f --- /dev/null +++ b/pkgs/niri-touchscreen-gestures/niri_touchscreen_gestures/main.py @@ -0,0 +1,4 @@ +from niri_touchscreen_gestures import run + +if __name__ == "__main__": + run() diff --git a/pkgs/niri-touchscreen-gestures/pyproject.toml b/pkgs/niri-touchscreen-gestures/pyproject.toml new file mode 100644 index 0000000..0a0b0df --- /dev/null +++ b/pkgs/niri-touchscreen-gestures/pyproject.toml @@ -0,0 +1,23 @@ +[build-system] +requires = ["hatchling"] +build-backend = "hatchling.build" + +[project] +name = "niri-touchscreen-gestures" +version = "0.1.0" +description = "Touchscreen gesture detector for niri (2/3/4-finger swipes → niri actions)" +readme = "README.md" +license = { text = "MIT" } +requires-python = ">=3.10" +authors = [{ name = "Ari Sosnovsky", email = "ariel@sosnovsky.ca" }] +dependencies = [ + "evdev>=1.7", + "pydantic>=2.0", + "pydantic-settings>=2.0", +] + +[project.scripts] +niri-touchscreen-gestures = "niri_touchscreen_gestures.cli:main" + +[tool.hatch.build.targets.wheel] +packages = ["niri_touchscreen_gestures"]