There is one cursor on this compositor build and every virtual pointer writes to it, so aiming at a session is a matter of serialising input, not of binding harder. Two pieces, and the split between them is the point. `hhstreamd/pointer.py` is the lease: one holder, an idle TTL and a hard cap on an injected clock, tracked buttons, and the window-local-to-normalised arithmetic that makes "click 40px into this window" mean something on an output that is not at the layout origin. Pure, so the expiry tests move time instead of sleeping. `hhstream_pointer/` is the driver: its own process because a Wayland client needs its own connection and a blocked one must not stall the broker. It holds no lease state -- it is told what to do and does it. The manager is bound at version 2 and pointers are created with `create_virtual_pointer_with_output`; a version-1-only manager is reported and refused rather than quietly aiming at the global layout. The wlr protocol module is vendored scanner output under `_protocol/`, so a checkout builds without a code generator; `_protocol/regenerate.py` says how to refresh it and why pywayland's own scanner entry point cannot be used. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011txKimbdkqPFpq6m8KDELu
94 lines
3.6 KiB
Python
94 lines
3.6 KiB
Python
"""Aiming arithmetic (CONTRACT-sessions.md §3.3).
|
|
|
|
An agent says "click 40 pixels into this window"; the virtual pointer wants a
|
|
fraction of its output. Everything between is this one function, and the case
|
|
that breaks in the field is the last one: a window on an output that is not at
|
|
the layout origin.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import pytest
|
|
|
|
from hhstreamd.pointer import window_to_absolute
|
|
|
|
#: A 1280x720 output at the origin, and a 640x480 window centred in it.
|
|
ORIGIN_MONITOR = (0, 0, 1280, 720)
|
|
CENTRED_WINDOW = (320, 120, 640, 480)
|
|
|
|
|
|
def test_window_centre_is_the_window_centre_not_the_monitor_centre():
|
|
# (320 + 320) / 1280 = 0.5 and (120 + 240) / 720 = 0.5 — the window happens
|
|
# to be centred, so this also pins down the easy reading of the maths.
|
|
assert window_to_absolute((320, 240), CENTRED_WINDOW, ORIGIN_MONITOR) == (0.5, 0.5)
|
|
|
|
|
|
def test_an_off_centre_window_centre_is_not_the_monitor_centre():
|
|
window = (0, 0, 640, 480)
|
|
assert window_to_absolute((320, 240), window, ORIGIN_MONITOR) == (0.25, 1 / 3)
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"corner, expected",
|
|
[
|
|
((0, 0), (320 / 1280, 120 / 720)), # top-left
|
|
((640, 0), (960 / 1280, 120 / 720)), # top-right
|
|
((0, 480), (320 / 1280, 600 / 720)), # bottom-left
|
|
((640, 480), (960 / 1280, 600 / 720)), # bottom-right
|
|
],
|
|
)
|
|
def test_all_four_window_corners(corner, expected):
|
|
assert window_to_absolute(corner, CENTRED_WINDOW, ORIGIN_MONITOR) == expected
|
|
|
|
|
|
def test_the_far_edge_is_inside():
|
|
"""``(w, h)`` is the bottom-right corner; rejecting it makes it unaimable."""
|
|
|
|
assert window_to_absolute((640, 480), CENTRED_WINDOW, ORIGIN_MONITOR) == (0.75, 600 / 720)
|
|
|
|
|
|
@pytest.mark.parametrize("point", [(-1, 0), (0, -1), (641, 0), (0, 481), (641, 481)])
|
|
def test_a_point_outside_the_window_raises(point):
|
|
with pytest.raises(ValueError, match="outside window"):
|
|
window_to_absolute(point, CENTRED_WINDOW, ORIGIN_MONITOR)
|
|
|
|
|
|
def test_a_window_on_the_second_monitor_uses_that_monitors_frame():
|
|
"""The offset case.
|
|
|
|
Two 1280x720 outputs side by side; the window is on the right-hand one, so
|
|
its global x starts at 1280. Without subtracting the monitor origin every
|
|
coordinate would exceed 1.0, clamp, and quietly aim at the far right edge —
|
|
a click that happens, in the wrong place.
|
|
"""
|
|
|
|
monitor = (1280, 0, 1280, 720)
|
|
window = (1280 + 320, 120, 640, 480)
|
|
assert window_to_absolute((320, 240), window, monitor) == (0.5, 0.5)
|
|
assert window_to_absolute((0, 0), window, monitor) == (320 / 1280, 120 / 720)
|
|
assert window_to_absolute((640, 480), window, monitor) == (960 / 1280, 600 / 720)
|
|
|
|
|
|
def test_a_vertically_stacked_monitor_uses_its_own_origin():
|
|
monitor = (0, 720, 1280, 720)
|
|
window = (0, 720, 1280, 720)
|
|
assert window_to_absolute((640, 360), window, monitor) == (0.5, 0.5)
|
|
assert window_to_absolute((0, 0), window, monitor) == (0.0, 0.0)
|
|
|
|
|
|
def test_a_window_hanging_off_the_monitor_clamps_rather_than_overflowing():
|
|
"""Hyprland will place a window partly off-screen; 1.0 is the honest answer
|
|
for "as far as this output goes"."""
|
|
|
|
window = (1000, 600, 640, 480)
|
|
assert window_to_absolute((640, 480), window, ORIGIN_MONITOR) == (1.0, 1.0)
|
|
# Negative global coordinates clamp at the other end.
|
|
assert window_to_absolute((0, 0), (-100, -50, 640, 480), ORIGIN_MONITOR) == (0.0, 0.0)
|
|
|
|
|
|
def test_a_degenerate_window_or_monitor_raises():
|
|
with pytest.raises(ValueError, match="window has no area"):
|
|
window_to_absolute((0, 0), (0, 0, 0, 480), ORIGIN_MONITOR)
|
|
with pytest.raises(ValueError, match="monitor has no area"):
|
|
window_to_absolute((0, 0), CENTRED_WINDOW, (0, 0, 1280, 0))
|