adding better support for niri+zed

This commit is contained in:
ari committed 2026-07-28 14:52:30 -04:00
1 parent 318d6ed286
commit 24574f6a01
14 files changed
+497 -80

No files matched your search

+39 -19
View File
@@ -1,23 +1,31 @@
# niri-touchscreen-gestures
A lightweight Python daemon that reads multi-touch events from a touchscreen via [`evdev`](https://python-evdev.readthedocs.io/), detects **2/3/4-finger swipe gestures**, and dispatches configurable actions to the [niri](https://github.com/YaLTeR/niri) Wayland compositor.
A lightweight Python daemon that reads multi-touch events from a touchscreen via [`evdev`](https://python-evdev.readthedocs.io/), detects **swipe/tap gestures**, and either dispatches an action to the [niri](https://github.com/YaLTeR/niri) Wayland compositor or **forwards synthetic input (click/scroll) to whatever window currently has focus**.
**Now with built-in defaults** — just run `niri-touchscreen-gestures` with no arguments to get started with sensible 3- and 4-finger gesture mappings.
It works by:
- Listening to absolute multi-touch (MT) events from an evdev touchscreen device
- Tracking finger start/end positions and movement vectors
- Classifying swipes (up/down/left/right) once they exceed a configurable pixel threshold
- Looking up the gesture in a TOML config and sending an `Action` command to niri's Unix socket (`$NIRI_SOCKET`)
- Tracking finger start/end positions, timing, and movement vectors
- Classifying swipes (up/down/left/right), taps, double-taps, long-taps, and continuous
2-finger scroll drags
- Looking up the gesture in a TOML config, scoped by the currently-focused app (queried over
niri's IPC socket), and either sending an `Action` command to niri or forwarding synthetic
input via [`wlrctl`](https://git.sr.ht/~brocellous/wlrctl) (niri implements the
`wlr-virtual-pointer-unstable-v1`/`virtual-keyboard-unstable-v1` protocols natively, so no
`ydotool`/`uinput`/root daemon is needed)
## Features
- **Pure event-driven**: No polling, uses `evdev.InputDevice.read_loop()`
- **Multi-finger support**: Automatically counts active fingers (2–4+)
- **Configurable mappings**: TOML file maps gestures like `3-finger-left` to niri actions
- **Tap gestures**: single tap, double tap, and long-press, forwarded as clicks
- **Continuous scroll**: 2-finger vertical drags forward live scroll deltas, not just a one-shot action
- **Per-app rules**: gestures can be scoped to whichever app currently has focus, with a global fallback
- **Configurable mappings**: TOML file maps gestures like `3-finger-left` to niri actions or forwarded input
- **Auto device detection**: Finds the first touchscreen (or accepts explicit `--device`)
- **Robust state machine**: Pure functions for gesture classification with comprehensive tests
- **Lightweight**: Minimal dependencies (`evdev`, `pydantic`, `pydantic-settings`)
- **Lightweight**: Minimal dependencies (`evdev`, `pydantic`, `pydantic-settings`, `wlrctl`)
## Installation
@@ -80,24 +88,35 @@ The tool ships with **sensible defaults** that match the example below. You only
See [`example-config.toml`](example-config.toml):
```toml
# 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 niri actions. See https://github.com/YaLTeR/niri/blob/main/src/niri/config.rs
# for the full list of available actions.
[global]
"3-finger-up" = "FocusWorkspaceDown"
"3-finger-down" = "FocusWorkspaceUp"
"3-finger-left" = "FocusColumnRight"
"3-finger-right" = "FocusColumnLeft"
"4-finger-up" = "ToggleOverview"
"4-finger-down" = "ToggleOverview"
[apps."dev.zed.Zed"]
"1-finger-tap" = "forward:click-left"
"1-finger-double-tap" = "forward:click-right"
"1-finger-long-tap" = "forward:click-right"
"2-finger-scroll" = "forward:scroll"
```
These exact mappings are now the **built-in defaults**. You can map gestures to any niri action that accepts no parameters.
The `[global]` table's mappings are the **built-in defaults**. `[apps."<app-id>"]` tables add
rules that only apply while that app has focus (matched by its Wayland `app_id`, as reported by
niri's `FocusedWindow` IPC query) and take priority over `[global]` for the same key.
Gesture keys:
- `"<N>-finger-<direction>"` — swipe, `direction` = `up`/`down`/`left`/`right`
- `"1-finger-tap"` / `"1-finger-double-tap"` / `"1-finger-long-tap"` — tap gestures
- `"<N>-finger-scroll"` — continuous drag scroll (currently supported for `N = 2`)
Action values:
- A niri action name (see below, or
[niri's config.rs](https://github.com/YaLTeR/niri/blob/main/src/niri/config.rs))
- `"forward:click-left"` / `"forward:click-right"` / `"forward:scroll"` — synthetic input
forwarded to the focused window via `wlrctl`
**Tip**: Place a custom config at `~/.config/niri/gestures.toml` and launch the daemon from a user systemd service or your niri startup script.
@@ -117,9 +136,10 @@ See [`nirictl.py`](niri_touchscreen_gestures/nirictl.py) for the list.
1. **Device Selection**: `touchscreen_identifier.py` scans `/dev/input` for devices with `EV_ABS` capabilities that aren't mice or touchpads.
2. **Event Loop**: Reads raw `EV_ABS` events (`ABS_MT_SLOT`, `ABS_MT_TRACKING_ID`, `ABS_MT_POSITION_X/Y`).
3. **State Tracking**: Maintains per-slot (per-finger) start and current coordinates.
4. **Gesture Detection**: On finger lift (`TRACKING_ID == -1`), computes delta and classifies direction if movement exceeds threshold.
5. **Action Dispatch**: Looks up `"{finger_count}-finger-{direction}"` in config and sends JSON command to niri's IPC socket.
3. **State Tracking**: Maintains per-slot (per-finger) start/current coordinates and start time.
4. **Gesture Detection**: On finger lift (`TRACKING_ID == -1`), classifies a tap/long-tap (low movement) or a swipe (movement past threshold). A lone tap is held for a short debounce window (via a background timer) to see if a second tap follows and it becomes a double-tap. 2-finger vertical drags stream continuous scroll deltas on every position update, instead of waiting for lift.
5. **Focused-app resolution**: Queries niri's `FocusedWindow` IPC request (once per gesture, cached for its duration) and resolves the gesture key against `[apps."<app-id>"]` first, falling back to `[global]`.
6. **Dispatch**: Niri actions are sent as JSON over niri's IPC socket; `forward:*` actions are sent to the focused window via `wlrctl pointer click/scroll`.
All core logic is in `detector/gestures.py` and is unit-tested.
+12 -1
View File
@@ -1,5 +1,6 @@
{ lib
, python3
, wlrctl
, ...
}:
@@ -22,8 +23,18 @@ python3.pkgs.buildPythonApplication {
typing-extensions
];
# Gestures forwarded to the focused app (clicks/scroll) shell out to wlrctl,
# which uses niri's native wlr-virtual-pointer/virtual-keyboard protocol
# support -- no uinput/root daemon needed.
makeWrapperArgs = [
"--prefix"
"PATH"
":"
(lib.makeBinPath [ wlrctl ])
];
meta = {
description = "Touchscreen gesture detector for niri (2/3/4-finger swipes → niri actions). Just run `niri-touchscreen-gestures` — no config needed.";
description = "Touchscreen gesture detector for niri (2/3/4-finger swipes → niri actions, plus click/scroll forwarding to the focused app). Just run `niri-touchscreen-gestures` — no config needed.";
homepage = "https://github.com/skykanin/nixos-setup";
license = lib.licenses.mit;
sourceProvenance = [ lib.sourceTypes.fromSource ];
@@ -1,19 +1,33 @@
# Example config for niri-touchscreen-gestures
#
# Keys are "<N>-finger-<direction>" where:
# N = 2, 3, 4 (or more)
# direction = up | down | left | right
# Keys are one of:
# "<N>-finger-<direction>" direction = up | down | left | right
# "1-finger-tap" single short tap
# "1-finger-double-tap" two short taps in quick succession
# "1-finger-long-tap" a tap held past the long-press threshold
# "2-finger-scroll" continuous 2-finger vertical drag
#
# Values are strings representing the niri action to perform.
# Values are either:
# - a niri action (see https://github.com/YaLTeR/niri/blob/main/src/niri/config.rs)
# - "forward:click-left" / "forward:click-right" / "forward:scroll", which
# inject synthetic input (via wlrctl) into whatever window is focused
#
# Rules live under two tables:
# [global] fallback rules, used when no per-app rule matches
# [apps."<app-id>"] only applied while that app (by Wayland app-id) is focused
# App-specific rules take priority over global ones for the same key.
[global]
"3-finger-up" = "FocusWorkspaceDown"
"3-finger-down" = "FocusWorkspaceUp"
"3-finger-left" = "FocusColumnRight"
"3-finger-right" = "FocusColumnLeft"
"4-finger-up" = "ToggleOverview"
"4-finger-down" = "ToggleOverview"
# Forward click/scroll gestures to Zed while it's focused.
[apps."dev.zed.Zed"]
"1-finger-tap" = "forward:click-left"
"1-finger-double-tap" = "forward:click-right"
"1-finger-long-tap" = "forward:click-right"
"2-finger-scroll" = "forward:scroll"
@@ -0,0 +1,43 @@
"""Gesture action model: either a niri IPC action, or synthetic input forwarded
to whatever window currently has compositor focus."""
from __future__ import annotations
from typing import Literal, Union
from pydantic import BaseModel
from niri_touchscreen_gestures.nirictl import SupportedActions
class ForwardClick(BaseModel):
kind: Literal["click"] = "click"
button: Literal["left", "right"]
class ForwardScroll(BaseModel):
kind: Literal["scroll"] = "scroll"
GestureAction = Union[SupportedActions, ForwardClick, ForwardScroll]
_FORWARD_CLICK_PREFIX = "forward:click-"
_FORWARD_SCROLL = "forward:scroll"
def parse_action(raw: str) -> GestureAction:
"""Parse a config string into a GestureAction.
"forward:click-left" / "forward:click-right" -> ForwardClick
"forward:scroll" -> ForwardScroll
anything else -> treated as a niri SupportedActions string (unchanged
behavior for existing configs).
"""
if raw == _FORWARD_SCROLL:
return ForwardScroll()
if raw.startswith(_FORWARD_CLICK_PREFIX):
button = raw[len(_FORWARD_CLICK_PREFIX) :]
if button not in ("left", "right"):
raise ValueError(f"Unknown forward click button: {raw!r}")
return ForwardClick(button=button) # type: ignore[arg-type]
return raw # type: ignore[return-value]
@@ -41,13 +41,15 @@ def process_args(
) -> tuple[GestureConfig, evdev.InputDevice, int]:
if args.config is None:
config = GestureConfig(
gestures={
"3-finger-up": "FocusWorkspaceDown",
"3-finger-down": "FocusWorkspaceUp",
"3-finger-left": "FocusColumnRight",
"3-finger-right": "FocusColumnLeft",
"4-finger-up": "ToggleOverview",
"4-finger-down": "ToggleOverview",
**{
"global": {
"3-finger-up": "FocusWorkspaceDown",
"3-finger-down": "FocusWorkspaceUp",
"3-finger-left": "FocusColumnRight",
"3-finger-right": "FocusColumnLeft",
"4-finger-up": "ToggleOverview",
"4-finger-down": "ToggleOverview",
}
}
)
elif not os.path.exists(args.config):
@@ -7,23 +7,35 @@ from typing import Any
import tomllib
from pydantic import BaseModel, Field
from niri_touchscreen_gestures.nirictl import SupportedActions
from niri_touchscreen_gestures.actions import GestureAction, parse_action
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.
"""Configuration mapping gesture keys (e.g. "3-finger-up") to actions.
Rules live under two tables:
- `global`: fallback rules, used when no focused-app-specific rule matches.
- `apps."<app-id>"`: rules that only apply while that app is focused.
App-specific rules take priority over global ones for the same key.
"""
gestures: dict[str, SupportedActions] = Field(default_factory=dict)
global_: dict[str, str] = Field(default_factory=dict, alias="global")
apps: dict[str, dict[str, str]] = Field(default_factory=dict)
model_config = {"extra": "allow"}
model_config = {"populate_by_name": True}
@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)
return cls(**data)
def get(self, key: str) -> SupportedActions | None:
return self.gestures.get(key)
def resolve(self, key: str, app_id: str | None) -> GestureAction | None:
"""Resolve a gesture key to an action, preferring the focused app's
rules over the global fallback."""
if app_id and (raw := self.apps.get(app_id, {}).get(key)):
return parse_action(raw)
if raw := self.global_.get(key):
return parse_action(raw)
return None
@@ -15,6 +15,7 @@ class Slot(TypedDict):
start_y: int
last_x: int
last_y: int
start_time: float
class DetectorState(BaseModel):
@@ -1,9 +1,13 @@
from typing import Literal
from niri_touchscreen_gestures.detector.base_types import (
FingerNumber,
Slot,
SwipeDirection,
)
TapKind = Literal["tap", "long-tap"]
def count_active_fingers(slots: dict[FingerNumber, Slot]) -> int:
return sum(1 for s in slots.values() if s["tracking_id"] != -1)
@@ -17,3 +21,13 @@ def classify_direction(dx: int, dy: int, threshold: int) -> SwipeDirection | Non
if ax > ay:
return "right" if dx > 0 else "left"
return "down" if dy > 0 else "up"
def classify_tap(
dx: int, dy: int, duration: float, move_threshold: int, long_press_s: float
) -> TapKind | None:
"""Pure tap classifier. Returns None if the finger moved past
`move_threshold` (i.e. it was a swipe, not a tap)."""
if max(abs(dx), abs(dy)) >= move_threshold:
return None
return "long-tap" if duration >= long_press_s else "tap"
@@ -1,7 +1,11 @@
import logging
import threading
from dataclasses import dataclass
from typing import Callable
import evdev
from niri_touchscreen_gestures.actions import ForwardScroll, GestureAction
from niri_touchscreen_gestures.config import GestureConfig
from niri_touchscreen_gestures.detector.base_types import (
DetectorState,
@@ -10,35 +14,163 @@ from niri_touchscreen_gestures.detector.base_types import (
)
from niri_touchscreen_gestures.detector.classifiers import (
classify_direction,
classify_tap,
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__)
DOUBLE_TAP_WINDOW_S = 0.35
DOUBLE_TAP_MOVE_THRESHOLD = 40 # px, max distance between the two taps
TAP_MOVE_THRESHOLD = 15 # px, max finger movement to still count as a tap
LONG_PRESS_S = 0.5
@dataclass(frozen=True)
class _SwipeSignal:
key: str
@dataclass(frozen=True)
class _TapSignal:
kind: str # "tap" | "long-tap"
x: int
y: int
time: float
@dataclass(frozen=True)
class _ScrollSignal:
key: str
delta: float
_Signal = _SwipeSignal | _TapSignal | _ScrollSignal
@dataclass
class _PendingTap:
timer: threading.Timer
x: int
y: int
time: float
app_id: str | None
class GestureDetector:
def __init__(self, config: GestureConfig, threshold: int = 60) -> None:
self.config = config
self.threshold = threshold
self._state = initial_state()
"""Detects gestures from raw evdev events and dispatches resolved actions.
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
Discrete gestures (swipes, long-taps, the second tap of a double-tap) are
resolved and dispatched as soon as the triggering event arrives. A lone
short tap can't be resolved immediately -- it might turn out to be the
first half of a double-tap -- so it's dispatched from a background
`threading.Timer` if no second tap follows within `DOUBLE_TAP_WINDOW_S`.
"""
def __init__(
self,
config: GestureConfig,
dispatch: Callable[[GestureAction, float], None],
get_app_id: Callable[[], str | None],
threshold: int = 60,
timer_factory: Callable[..., threading.Timer] = threading.Timer,
) -> None:
self.config = config
self.dispatch = dispatch
self.get_app_id = get_app_id
self.threshold = threshold
self.timer_factory = timer_factory
self._state = initial_state()
self._lock = threading.RLock()
self._app_id_queried = False
self._cached_app_id: str | None = None
self._pending_tap: _PendingTap | None = None
self._scroll_active = False
def handle_event(self, ev: evdev.InputEvent) -> None:
"""Mutates internal state and dispatches any resolved action."""
self._state, signal = _handle_event(self._state, ev, self.threshold)
if signal is not None:
self._process_signal(signal)
if count_active_fingers(self._state.slots) == 0:
with self._lock:
self._app_id_queried = False
self._cached_app_id = None
self._scroll_active = False
def _current_app_id(self) -> str | None:
with self._lock:
if not self._app_id_queried:
self._cached_app_id = self.get_app_id()
self._app_id_queried = True
return self._cached_app_id
def _process_signal(self, signal: _Signal) -> None:
if isinstance(signal, _SwipeSignal):
if signal.key.startswith("2-finger-") and self._scroll_active:
return # already forwarded as continuous scroll -- don't double-dispatch
self._resolve_and_dispatch(signal.key, self._current_app_id())
elif isinstance(signal, _ScrollSignal):
action = self.config.resolve(signal.key, self._current_app_id())
if isinstance(action, ForwardScroll):
self._scroll_active = True
self.dispatch(action, signal.delta)
elif isinstance(signal, _TapSignal):
self._process_tap(signal)
def _process_tap(self, tap: _TapSignal) -> None:
if tap.kind == "long-tap":
self._resolve_and_dispatch("1-finger-long-tap", self._current_app_id())
return
with self._lock:
pending = self._pending_tap
is_second_tap = (
pending is not None
and (tap.time - pending.time) <= DOUBLE_TAP_WINDOW_S
and max(abs(tap.x - pending.x), abs(tap.y - pending.y))
<= DOUBLE_TAP_MOVE_THRESHOLD
)
if is_second_tap:
assert pending is not None
pending.timer.cancel()
self._pending_tap = None
app_id = pending.app_id
else:
if pending is not None:
pending.timer.cancel()
app_id = self._current_app_id()
timer = self.timer_factory(
DOUBLE_TAP_WINDOW_S, self._resolve_pending_tap, args=(app_id,)
)
self._pending_tap = _PendingTap(
timer=timer, x=tap.x, y=tap.y, time=tap.time, app_id=app_id
)
timer.daemon = True
timer.start()
if is_second_tap:
self._resolve_and_dispatch("1-finger-double-tap", app_id)
def _resolve_pending_tap(self, app_id: str | None) -> None:
with self._lock:
self._pending_tap = None
self._resolve_and_dispatch("1-finger-tap", app_id)
def _resolve_and_dispatch(self, key: str, app_id: str | None) -> None:
logger.info(f"Detected {key=} {app_id=}")
action = self.config.resolve(key, app_id)
if action is not None:
self.dispatch(action, 0.0)
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)."""
) -> tuple[DetectorState, _Signal | None]:
"""Pure event handler. Returns (new_state, signal_or_None)."""
if ev.type != evdev.ecodes.EV_ABS:
return state, None
slot: Slot = state.slots.get(state.current_slot, empty_slot()).copy()
@@ -49,24 +181,29 @@ def _handle_event(
if ev.code == evdev.ecodes.ABS_MT_TRACKING_ID:
if ev.value == -1:
signal: _Signal | None = None
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,
finger_count = count_active_fingers(state.slots)
if finger_count == 1:
tap_kind = classify_tap(
dx,
dy,
ev.timestamp() - slot["start_time"],
TAP_MOVE_THRESHOLD,
LONG_PRESS_S,
)
if tap_kind:
signal = _TapSignal(
tap_kind, slot["last_x"], slot["last_y"], ev.timestamp()
)
slot = empty_slot()
if signal is None:
direction = classify_direction(dx, dy, threshold)
if direction:
signal = _SwipeSignal(f"{finger_count}-finger-{direction}")
new_slots[state.current_slot] = empty_slot()
return DetectorState(slots=new_slots, current_slot=state.current_slot), signal
else:
slot = Slot(
tracking_id=ev.value,
@@ -74,6 +211,7 @@ def _handle_event(
start_y=0,
last_x=0,
last_y=0,
start_time=ev.timestamp(),
)
new_slots[state.current_slot] = slot
return DetectorState(slots=new_slots, current_slot=state.current_slot), None
@@ -86,10 +224,20 @@ def _handle_event(
return DetectorState(slots=new_slots, current_slot=state.current_slot), None
if ev.code == evdev.ecodes.ABS_MT_POSITION_Y:
prev_y = slot["last_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
new_state = DetectorState(slots=new_slots, current_slot=state.current_slot)
# Stream continuous scroll ticks for 2-finger vertical drags, instead of
# waiting for finger-lift like the discrete swipe classifier does.
finger_count = count_active_fingers(new_slots)
if prev_y != 0 and finger_count == 2:
delta = float(ev.value - prev_y)
if delta != 0:
return new_state, _ScrollSignal(f"{finger_count}-finger-scroll", delta)
return new_state, None
return state, None
@@ -12,6 +12,7 @@ def empty_slot() -> Slot:
start_y=0,
last_x=0,
last_y=0,
start_time=0.0,
)
@@ -0,0 +1,19 @@
"""Forwards synthetic input to whatever window currently has compositor
focus, via wlrctl (wlr-virtual-pointer-unstable-v1 / virtual-keyboard-unstable-v1).
Niri implements both protocols, so no root daemon / uinput access is needed."""
import logging
import subprocess
from typing import Literal
logger = logging.getLogger(__name__)
def send_click(button: Literal["left", "right"]) -> None:
logger.info(f"niri-touchscreen-gestures: forwarding click {button=}")
subprocess.run(["wlrctl", "pointer", "click", button], check=True)
def send_scroll(dy: float) -> None:
logger.info(f"niri-touchscreen-gestures: forwarding scroll {dy=}")
subprocess.run(["wlrctl", "pointer", "scroll", str(dy)], check=True)
@@ -1,28 +1,43 @@
import logging
from niri_touchscreen_gestures import forwarder, nirictl
from niri_touchscreen_gestures.actions import ForwardClick, ForwardScroll, GestureAction
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 dispatch(action: GestureAction, delta: float) -> None:
logger.info(f"niri-touchscreen-gestures: dispatching {action=} {delta=}")
if isinstance(action, ForwardClick):
forwarder.send_click(action.button)
elif isinstance(action, ForwardScroll):
forwarder.send_scroll(delta)
else:
nirictl.send_niri_action(action)
def get_focused_app_id() -> str | None:
window = nirictl.get_focused_window()
return window.get("app_id") if window else None
def main_runtime() -> None:
logger.info("niri-touchscreen-gestures: starting")
config, dev, threshold = process_args(get_parser().parse_args())
detector = GestureDetector(
config,
dispatch=dispatch,
get_app_id=get_focused_app_id,
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)
detector.handle_event(ev)
if __name__ == "__main__":
@@ -36,7 +36,7 @@ def niri_socket():
s.close()
def send_niri_command(command: dict[str, Any]) -> dict[str, Any]:
def send_niri_command(command: dict[str, Any] | str) -> 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"))
@@ -66,3 +66,9 @@ def send_niri_action(action: SupportedActions) -> bool:
request = {"Action": {action: {}}}
reply = send_niri_command(request)
return reply.get("Ok", "Handled").lower() == "handled"
def get_focused_window() -> dict[str, Any] | None:
"""Query niri for the currently focused window (includes its `app_id`)."""
reply = send_niri_command("FocusedWindow")
return reply.get("Ok", {}).get("FocusedWindow")
@@ -1,4 +1,115 @@
from niri_touchscreen_gestures.detector.classifiers import count_active_fingers
from niri_touchscreen_gestures.actions import ForwardClick
from niri_touchscreen_gestures.config import GestureConfig
from niri_touchscreen_gestures.detector.classifiers import classify_tap, count_active_fingers
from niri_touchscreen_gestures.detector.gestures import (
_PendingTap,
_TapSignal,
GestureDetector,
)
class FakeTimer:
"""Records the callback instead of scheduling it on a real thread, so
debounce tests can fire (or skip) it deterministically."""
def __init__(self, interval, function, args=()):
self.interval = interval
self.function = function
self.args = args
self.cancelled = False
self.started = False
def start(self):
self.started = True
def cancel(self):
self.cancelled = True
def fire(self):
if not self.cancelled:
self.function(*self.args)
class TestClassifyTap:
def test_short_move_short_duration_is_tap(self):
assert classify_tap(2, -3, 0.1, move_threshold=15, long_press_s=0.5) == "tap"
def test_short_move_long_duration_is_long_tap(self):
assert classify_tap(0, 0, 0.6, move_threshold=15, long_press_s=0.5) == "long-tap"
def test_large_move_is_not_a_tap(self):
assert classify_tap(50, 0, 0.1, move_threshold=15, long_press_s=0.5) is None
class TestTapDebounce:
def _make_detector(self, config=None):
dispatched = []
detector = GestureDetector(
config or GestureConfig(),
dispatch=lambda action, delta: dispatched.append((action, delta)),
get_app_id=lambda: None,
timer_factory=FakeTimer,
)
return detector, dispatched
def test_lone_tap_dispatches_after_debounce_window_fires(self):
config = GestureConfig(**{"global": {"1-finger-tap": "forward:click-left"}})
detector, dispatched = self._make_detector(config)
detector._process_signal(_TapSignal("tap", 100, 100, 1.0))
assert dispatched == [] # not dispatched yet -- awaiting the debounce window
pending = detector._pending_tap
assert isinstance(pending, _PendingTap)
pending.timer.fire()
assert dispatched == [(ForwardClick(button="left"), 0.0)]
def test_second_tap_within_window_cancels_timer_and_dispatches_double_tap(self):
config = GestureConfig(
**{
"global": {
"1-finger-tap": "forward:click-left",
"1-finger-double-tap": "forward:click-right",
}
}
)
detector, dispatched = self._make_detector(config)
detector._process_signal(_TapSignal("tap", 100, 100, 1.0))
first_timer = detector._pending_tap.timer
detector._process_signal(_TapSignal("tap", 105, 98, 1.1))
assert first_timer.cancelled
assert detector._pending_tap is None
assert dispatched == [(ForwardClick(button="right"), 0.0)]
def test_second_tap_outside_window_is_treated_as_a_new_lone_tap(self):
config = GestureConfig(
**{
"global": {
"1-finger-tap": "forward:click-left",
"1-finger-double-tap": "forward:click-right",
}
}
)
detector, dispatched = self._make_detector(config)
detector._process_signal(_TapSignal("tap", 100, 100, 1.0))
detector._process_signal(_TapSignal("tap", 100, 100, 2.0)) # well past the window
assert dispatched == []
detector._pending_tap.timer.fire()
assert dispatched == [(ForwardClick(button="left"), 0.0)]
def test_long_tap_dispatches_immediately(self):
config = GestureConfig(**{"global": {"1-finger-long-tap": "forward:click-right"}})
detector, dispatched = self._make_detector(config)
detector._process_signal(_TapSignal("long-tap", 100, 100, 1.0))
assert dispatched == [(ForwardClick(button="right"), 0.0)]
assert detector._pending_tap is None
class TestClassifiers: