initial work on touchscreen adapter

This commit is contained in:
ari committed 2026-07-11 13:29:00 -04:00
1 parent a0b4f8ec43
commit 9077f7e0a7
15 files changed
+412

No files matched your search

+6
View File
@@ -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 {
+7
View File
@@ -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
+2
View File
@@ -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 ++ [
+1
View File
@@ -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 { };
})
];
}
+1
View File
@@ -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
@@ -0,0 +1 @@
__pycache__
+39
View File
@@ -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 `<N>-finger-<direction>` 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
@@ -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";
};
}
@@ -0,0 +1,28 @@
# Example config for niri-touchscreen-gestures
#
# Keys are "<N>-finger-<direction>" 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"],
]
@@ -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)
@@ -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
@@ -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)
@@ -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
@@ -0,0 +1,4 @@
from niri_touchscreen_gestures import run
if __name__ == "__main__":
run()
@@ -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"]