Files
hyprland-headless-stream-pl…/tests/pointer/test_driver_flags.py
T
HermenixandClaude Opus 5 f931858f30 Stage 1: the driver learns to type
`zwp_virtual_keyboard_v1`, spoken by the process that already speaks
`zwlr_virtual_pointer_v1`. One device for the seat, one keymap uploaded
once, keysym names on our socket and evdev codes only on the Wayland
request -- the division buttons.py already draws.

keymap.py is the new seam, and it holds the three decisions that have a
wrong version which looks right:

  * A capital is the MASK, not a synthetic Shift keycode. Hyprland's
    virtual keyboard never runs updateXkbStateWithKey, so a virtual
    KEY_LEFTSHIFT changes no modifier state at all -- and sending both
    is how a toolkit applies Shift twice.
  * A modifier is whatever the keymap says is one, measured by pressing
    each key in a throwaway xkb state. Not a name list: `@` on `de` is a
    level-three symbol, and a Shift-only special case types the wrong
    character. (The measured set is also smaller than the canonical one,
    because no keycode on us/pc105 carries Meta or Hyper.)
  * A lock is not a modifier. Caps_Lock reports a *locked* mask, so
    `type` refuses while it is held rather than guessing the case.

The whole string resolves before anything is emitted, and every refusal
names the scalar INDEX, never the character: these messages reach the
driver's log and the string is a password as often as not.

Folding in the review's changes 1, 2, 3 and 6:

  1 a Rejected now carries a `kind`. A bad keysym or an unmappable
    character is `invalid_argument`; a missing keyboard global is
    `refused`. Without the token the broker would have to tell exit 2
    from exit 10 by reading English, and _drive maps every `ok: false`
    to driver_refused today.
  2 the keymap `size` on the wire includes the trailing NUL, and the fd
    outlives a roundtrip rather than a flush. Hyprland mmaps exactly
    that many bytes and hands the pointer to xkb_keymap_new_from_string.
  3 release_all lifts buttons, then keys in press order, then zeroes the
    mask -- and the mask only if something set it, so a compositor with
    no keyboard global still gets its buttons lifted. One release_all is
    the whole of expiry; an exception in the key half would abandon it.
  6 `key` and `type` never call _aimed(). A keyboard has no output, and
    one test deliberately never warps so that stays true.

Also: `--xkb-layout/-model/-variant/-options`, compiled BEFORE any
Wayland call so a bad layout is exit 2 rather than exit 10; a comma in
the layout refused outright, because v1 always sends group 0 and
xkbcommon would hand back a second group nothing could reach; the
keymap uploaded after connect and before listen, with one
modifiers(0,0,0,0) after it; and xkbcommon as a hard, non-optional
dependency -- an optional one would let test_keymap.py skip, and the
unmappable-character path is the one that must not rot.

  nix develop -c python -m pytest tests/pointer tests/sessions/unit -q
  549 passed in 25.70s

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01GhNPM6rGcuzxocM1nATnuM
2026-10-04 03:39:00 +00:00

145 lines
4.9 KiB
Python

"""The driver's XKB flags — the only path a layout has to the uploaded keymap.
This file exists where a `tests/sessions/unit/test_settings.py` case would have
gone, and that is the point. The broker's TOML is read by `hhstreamd` for its
lease numbers; `hhstream-pointer` is a different unit and never opens that
file. So a `[keyboard]` table there would be a setting an operator could change
with no effect at all, and a test asserting its default would be green while
every injected key stayed on `us`.
The layout therefore arrives as `--xkb-layout`, and these are the assertions
about that flag: what it defaults to, that a bad value is exit **2**, and that
a bad value is exit 2 **without the compositor being touched** — because a
layout xkbcommon will not compile is the operator's typo, and reporting it as
exit 10 sends them to the wrong unit.
"""
from __future__ import annotations
import pytest
from hhstream_pointer import __main__ as entry
class _Unreached(Exception):
"""Raised if the Wayland side is reached when it should not have been."""
class _RefusingSink:
"""A sink that fails the test if anything asks it to talk to a compositor.
The review asked for an injected connect that can be asserted *not* to have
been called; this is that, as a type rather than a flag, so a future code
path that connects earlier fails loudly instead of silently reordering
start-up.
"""
instances = 0
def __init__(self, runtime_dir, display_name=None) -> None:
type(self).instances += 1
def connect(self) -> None:
raise _Unreached("connect")
def upload_keymap(self, text: str) -> None:
raise _Unreached("upload_keymap")
def close(self) -> None:
pass
@pytest.fixture
def no_compositor(monkeypatch):
_RefusingSink.instances = 0
monkeypatch.setattr(entry, "WaylandSink", _RefusingSink)
return _RefusingSink
# -- defaults ---------------------------------------------------------------
def test_the_xkb_flags_default_to_the_frozen_values():
args = entry.build_parser().parse_args([])
assert args.xkb_layout == "us"
assert args.xkb_model == "pc105"
assert (args.xkb_variant, args.xkb_options) == ("", "")
def test_the_defaults_are_the_ones_the_module_option_defaults_to():
# Stated as constants so the NixOS option and the hand-run binary cannot
# drift into typing different characters.
assert (entry.DEFAULT_LAYOUT, entry.DEFAULT_MODEL) == ("us", "pc105")
def test_a_layout_is_taken_from_the_flag():
assert entry.build_parser().parse_args(["--xkb-layout", "de"]).xkb_layout == "de"
def test_the_contract_command_line_still_parses():
"""CONTRACT-sessions.md §3.4's exact line, plus the keymap flags."""
args = entry.build_parser().parse_args(
[
"--socket",
"/run/hhstream/pointer.sock",
"--compositor-runtime-dir",
"/run/hypr-stream",
"--xkb-layout",
"us",
"--xkb-model",
"pc105",
"--xkb-variant",
"",
"--xkb-options",
"",
]
)
assert str(args.socket) == "/run/hhstream/pointer.sock"
assert str(args.compositor_runtime_dir) == "/run/hypr-stream"
assert args.xkb_layout == "us"
# -- refusals, before any Wayland call --------------------------------------
def test_a_comma_separated_layout_exits_2_without_connecting(no_compositor, caplog):
"""v1 sends group 0 in every `modifiers`, so a second group is unreachable.
xkbcommon accepts `us,ru` and hands back a two-group keymap, so nothing
downstream would notice: a caller asking for Cyrillic would get Latin and
no error. Refused here, and the NixOS option's type refuses it too — this
one is for the binary run by hand, which that type cannot reach.
"""
code = entry.main(["--xkb-layout", "us,ru"])
assert code == entry.EXIT_USAGE
assert no_compositor.instances == 0, "the compositor must not be touched"
def test_a_layout_xkbcommon_rejects_exits_2_without_connecting(no_compositor):
code = entry.main(["--xkb-layout", "zzz-no-such-layout"])
assert code == entry.EXIT_USAGE
assert no_compositor.instances == 0
def test_a_usable_layout_gets_as_far_as_the_compositor(no_compositor):
"""The other half: exit 2 must not be how a *good* layout fails.
A test that only asserted the refusals would pass against a `main` that
returned 2 for everything, so this one proves the good path reaches the
Wayland side and fails there instead.
"""
with pytest.raises(_Unreached, match="connect"):
entry.main(["--xkb-layout", "us"])
assert no_compositor.instances == 1
def test_the_exit_codes_match_the_brokers_table():
# Mirrored, not imported: the driver is a separate package on purpose
# (see the module docstring in __main__).
assert (entry.EXIT_OK, entry.EXIT_USAGE, entry.EXIT_COMPOSITOR) == (0, 2, 10)