#126 feat: virtual pointer for agents and remote control

state
merged · e13ddbd · merge commit
branch
feat/virtual-pointer → main
opened
bnema ·
updated

Lets agents and tools such as wlrctl or wayvnc drive the session pointer.

  • zwlr_virtual_pointer_manager_v1, hidden from sandboxed clients; bounded and ordered frames, bounded held buttons.
  • Input bound to an unavailable or isolated output is dropped; releases only follow accepted presses, per device.
  • neferwl pointer CLI; docs for controlling a session from agents.
  • Tagged purego-libwayland v0.14.0 and purego-pipewire v0.6.0.

Reviews: go-reviewer approved. Tests: race, mocks, staticcheck, headless pointer CLI and wayvnc e2e.

CommitSubjectAuthorDateSignatureStatus
d035120Merge remote-tracking branch 'origin/main' into feat/virtual-pointerbrice GPG Verified
0a5e6bfchore(merge): merge feat/server-mode into feat/virtual-pointerbrice GPG Verified
fa203bafix(core): track virtual pointer button admission per devicebrice GPG Verified
0f1431dfix(core): drop virtual pointer input for unavailable bound outputsbrice GPG Verified
43acda3build(deps): use tagged purego-libwayland v0.14.0 and purego-pipewire v0.6.0brice GPG Verified
22b3525fix(wayland): report oversized virtual pointer frames as implementation errorsbrice GPG Verified
a5b34c4fix(wayland): bound virtual pointer held buttonsbrice GPG Verified
98522fddocs(server): use neferwl pointer for agentsbrice GPG Verified
17f326bfeat(cli): neferwl pointer drives the session pointerbrice GPG Verified
f498570fix(wayland): bound and order virtual pointer framesbrice GPG Verified
a2ea963test(app): wayvnc remote viewing and control end to endbrice GPG Verified
5145934docs(server): controlling a session from agentsbrice GPG Verified
844650efeat(wayland): wlr virtual pointer for agents and wlrctlbrice GPG Verified

Checks 1

28 files · +2299 -14
1/1
M Makefile +5 -1
@@ -1,4 +1,4 @@
1-.PHONY: build test vet race e2e-xwayland mocks mocks-check spv-check fakes-check log-check arch adapter-imports check perf-check color-check bin tty logs pkg install
1+.PHONY: build test vet race e2e-xwayland e2e-wayvnc mocks mocks-check spv-check fakes-check log-check arch adapter-imports check perf-check color-check bin tty logs pkg install
22
33 # 0 runs until quit; set e.g. TTY_TIMEOUT=60s for a safety net.
44 TTY_TIMEOUT ?= 0
@@ -27,6 +27,7 @@ test:
2727 vet:
2828 CGO_ENABLED=0 go vet ./...
2929 CGO_ENABLED=0 go vet -tags xwayland ./internal/app
30+ CGO_ENABLED=0 go vet -tags wayvnc ./internal/app
3031 race:
3132 # cgo is enabled only for the race test binary.
3233 CGO_ENABLED=1 go test -race ./...
@@ -81,6 +82,9 @@ color-check:
8182 e2e-xwayland:
8283 CGO_ENABLED=1 go test -tags xwayland -race ./internal/app -run TestXwayland -count=1 -v
8384
85+e2e-wayvnc:
86+ CGO_ENABLED=1 go test -tags wayvnc -race ./internal/app -run TestWayVNC -count=1 -v
87+
8488 # Arch package of the committed HEAD (packaging/arch/PKGBUILD). Go modules
8589 # come from the module proxy in prepare(); the build itself runs offline.
8690 # pacman-ordered version: 0.0.0.r<commits>.g<hash>; a tag replaces 0.0.0.
@@ -1,4 +1,4 @@
1-.PHONY: build test vet race e2e-xwayland mocks mocks-check spv-check fakes-check log-check arch adapter-imports check perf-check color-check bin tty logs pkg install1+.PHONY: build test vet race e2e-xwayland e2e-wayvnc mocks mocks-check spv-check fakes-check log-check arch adapter-imports check perf-check color-check bin tty logs pkg install
2 2
3 # 0 runs until quit; set e.g. TTY_TIMEOUT=60s for a safety net.3 # 0 runs until quit; set e.g. TTY_TIMEOUT=60s for a safety net.
4 TTY_TIMEOUT ?= 04 TTY_TIMEOUT ?= 0
@@ -27,6 +27,7 @@ test:
27 vet:27 vet:
28 CGO_ENABLED=0 go vet ./...28 CGO_ENABLED=0 go vet ./...
29 CGO_ENABLED=0 go vet -tags xwayland ./internal/app29 CGO_ENABLED=0 go vet -tags xwayland ./internal/app
30+ CGO_ENABLED=0 go vet -tags wayvnc ./internal/app
30 race:31 race:
31 # cgo is enabled only for the race test binary.32 # cgo is enabled only for the race test binary.
32 CGO_ENABLED=1 go test -race ./...33 CGO_ENABLED=1 go test -race ./...
@@ -81,6 +82,9 @@ color-check:
81 e2e-xwayland:82 e2e-xwayland:
82 CGO_ENABLED=1 go test -tags xwayland -race ./internal/app -run TestXwayland -count=1 -v83 CGO_ENABLED=1 go test -tags xwayland -race ./internal/app -run TestXwayland -count=1 -v
83 84
85+e2e-wayvnc:
86+ CGO_ENABLED=1 go test -tags wayvnc -race ./internal/app -run TestWayVNC -count=1 -v
87+
84 # Arch package of the committed HEAD (packaging/arch/PKGBUILD). Go modules88 # Arch package of the committed HEAD (packaging/arch/PKGBUILD). Go modules
85 # come from the module proxy in prepare(); the build itself runs offline.89 # come from the module proxy in prepare(); the build itself runs offline.
86 # pacman-ordered version: 0.0.0.r<commits>.g<hash>; a tag replaces 0.0.0.90 # pacman-ordered version: 0.0.0.r<commits>.g<hash>; a tag replaces 0.0.0.
M docs/dev/headless.md +16 -0
@@ -64,6 +64,22 @@ WAYLAND_DISPLAY=<name> go run ./examples/testime -preedit nihon -commit 日本
6464
6565 `-count <n>` exits after n commits; `-delay` sets the time between preedit and commit.
6666
67+## Testing the virtual pointer
68+
69+`neferwl pointer` drives the pointer of the headless session (same display resolution as `neferwl state`); run it with the headless `WAYLAND_DISPLAY`:
70+
71+```sh
72+WAYLAND_DISPLAY=<name> neferwl pointer abs 0.1 0.5 click
73+WAYLAND_DISPLAY=<name> neferwl pointer drag 100 0 scroll down 3
74+WAYLAND_DISPLAY=<name> neferwl pointer --output HEADLESS-2 abs 0.5 0.5
75+```
76+
77+Check the result with `neferwl state` (`.window.id` is the focused window). Headless screenshots do not show the cursor. `TestPointerCLI` (`internal/app`, in `go test ./...`) runs the built binary against a headless NeferWL and checks a click, a drag, a scroll and the release at exit on a test client.
78+
79+## wayvnc check
80+
81+`make e2e-wayvnc` runs `TestWayVNC` (build tag `wayvnc`; skips without `wayvnc` on `PATH`). A headless NeferWL with a 320x240 output allowlists wayvnc in a throwaway capture-allow file and maps a client that shows a red square; then `wayvnc -o HEADLESS-1` runs on a free localhost port. A minimal RFB client in the test checks that the framebuffer has the output's size and shows the square, that a pointer event over the square enters the client and a click reaches it, and that a key event types `a` (KEY_A) into the focused client.
82+
6783 ## X11 check
6884
6985 `make e2e-xwayland` runs the end-to-end test of the built-in window manager (`xwayland = builtin`). It is behind the `xwayland` build tag, so `go test ./...` and `make check` only compile it (`go vet -tags xwayland`). It needs `Xwayland` on `PATH` and a GPU, and skips without Xwayland. The clipboard test also needs `wl-copy` and `wl-paste` (wl-clipboard) and skips without them; they run with the headless `WAYLAND_DISPLAY` and need no extra focus step, since the focused probe window is enough for `wl-copy --foreground` to set the selection.
@@ -64,6 +64,22 @@ WAYLAND_DISPLAY=<name> go run ./examples/testime -preedit nihon -commit 日本
64 64
65 `-count <n>` exits after n commits; `-delay` sets the time between preedit and commit.65 `-count <n>` exits after n commits; `-delay` sets the time between preedit and commit.
66 66
67+## Testing the virtual pointer
68+
69+`neferwl pointer` drives the pointer of the headless session (same display resolution as `neferwl state`); run it with the headless `WAYLAND_DISPLAY`:
70+
71+```sh
72+WAYLAND_DISPLAY=<name> neferwl pointer abs 0.1 0.5 click
73+WAYLAND_DISPLAY=<name> neferwl pointer drag 100 0 scroll down 3
74+WAYLAND_DISPLAY=<name> neferwl pointer --output HEADLESS-2 abs 0.5 0.5
75+```
76+
77+Check the result with `neferwl state` (`.window.id` is the focused window). Headless screenshots do not show the cursor. `TestPointerCLI` (`internal/app`, in `go test ./...`) runs the built binary against a headless NeferWL and checks a click, a drag, a scroll and the release at exit on a test client.
78+
79+## wayvnc check
80+
81+`make e2e-wayvnc` runs `TestWayVNC` (build tag `wayvnc`; skips without `wayvnc` on `PATH`). A headless NeferWL with a 320x240 output allowlists wayvnc in a throwaway capture-allow file and maps a client that shows a red square; then `wayvnc -o HEADLESS-1` runs on a free localhost port. A minimal RFB client in the test checks that the framebuffer has the output's size and shows the square, that a pointer event over the square enters the client and a click reaches it, and that a key event types `a` (KEY_A) into the focused client.
82+
67 ## X11 check83 ## X11 check
68 84
69 `make e2e-xwayland` runs the end-to-end test of the built-in window manager (`xwayland = builtin`). It is behind the `xwayland` build tag, so `go test ./...` and `make check` only compile it (`go vet -tags xwayland`). It needs `Xwayland` on `PATH` and a GPU, and skips without Xwayland. The clipboard test also needs `wl-copy` and `wl-paste` (wl-clipboard) and skips without them; they run with the headless `WAYLAND_DISPLAY` and need no extra focus step, since the focused probe window is enough for `wl-copy --foreground` to set the selection.85 `make e2e-xwayland` runs the end-to-end test of the built-in window manager (`xwayland = builtin`). It is behind the `xwayland` build tag, so `go test ./...` and `make check` only compile it (`go vet -tags xwayland`). It needs `Xwayland` on `PATH` and a GPU, and skips without Xwayland. The clipboard test also needs `wl-copy` and `wl-paste` (wl-clipboard) and skips without them; they run with the headless `WAYLAND_DISPLAY` and need no extra focus step, since the focused probe window is enough for `wl-copy --foreground` to set the selection.
M docs/server.md +29 -0
@@ -93,6 +93,35 @@ Moonlight's keyboard, mouse and gamepads reach the host through Sunshine's virtu
9393
9494 Steam's controller rules grant access through `uaccess`, which needs a logind seat. Server mode has none, so the `input` group is what gives Steam its gamepads.
9595
96+## Controlling a NeferWL session (agents)
97+
98+An agent or script on the host can watch the session and drive it with standard Wayland tools. Run them as the session user with the session's display:
99+
100+```sh
101+export $(systemctl --user show-environment | grep -E '^(WAYLAND_DISPLAY|XDG_RUNTIME_DIR)=')
102+grim -o tv /tmp/screen.png # capture one output (see Screen capture)
103+neferwl pointer abs 0.5 0.5 click # click the middle of the layout
104+neferwl pointer move 200 -50 click right # actions run in order
105+wtype 'hello' # type into the focused window
106+neferwl state # windows, focus, outputs as JSON
107+```
108+
109+- `neferwl pointer` actions: `abs X Y` (fractions 0..1 of the layout, or of one output with `--output <name>`), `move DX DY`, `press`/`release`/`click [BTN]`, `hold [BTN] [DURATION]`, `drag DX DY [BTN]` and `scroll up|down|left|right [N]`. BTN is `left` (default), `right`, `middle`, `side`, `extra`, `forward`, `back`, a `BTN_*` name or an evdev code. `--click-duration` (20ms), `--hold-duration` (500ms), `--drag-steps` (10) and `--drag-step-delay` (10ms) tune the timing. Buttons still pressed at exit are released. See `neferwl pointer --help`.
110+- [wlrctl](https://git.sr.ht/~brocellous/wlrctl) (AUR: `wlrctl`) works too: `wlrctl pointer move 200 -50`, `wlrctl pointer click left`.
111+- Both use `zwlr_virtual_pointer_v1`. NeferWL routes their events like a real mouse, so focus, hover and drags behave as they would for a person. Absolute motion maps into the whole layout, or into one output when the tool binds it (`create_virtual_pointer_with_output`).
112+- `ydotool` writes to `/dev/uinput` through `ydotoold` and does not use this protocol; prefer `neferwl pointer`.
113+- Nothing is injected while the session is locked. Buttons a tool still held when it disconnects are released.
114+- Flatpak and other sandboxed clients (those on a `wp_security_context_v1` socket) do not see the virtual pointer global.
115+- `grim` needs the capture allowlist to admit it; see [the allowlist](capture.md#the-allowlist).
116+
117+[wayvnc](https://github.com/any1/wayvnc) works for remote viewing and control: it captures an output with wlr-screencopy and injects input through the virtual pointer and keyboard. Add its executable (`/usr/bin/wayvnc`) to `/etc/neferwl/capture-allow`, then run it in the session:
118+
119+```sh
120+wayvnc -o tv 127.0.0.1 5900
121+```
122+
123+Keep it on localhost and reach it through an SSH tunnel, or configure wayvnc's own authentication and TLS.
124+
96125 ## Hardening
97126
98127 On a dedicated box, a local keyboard should not reboot or reach a console:
@@ -93,6 +93,35 @@ Moonlight's keyboard, mouse and gamepads reach the host through Sunshine's virtu
93 93
94 Steam's controller rules grant access through `uaccess`, which needs a logind seat. Server mode has none, so the `input` group is what gives Steam its gamepads.94 Steam's controller rules grant access through `uaccess`, which needs a logind seat. Server mode has none, so the `input` group is what gives Steam its gamepads.
95 95
96+## Controlling a NeferWL session (agents)
97+
98+An agent or script on the host can watch the session and drive it with standard Wayland tools. Run them as the session user with the session's display:
99+
100+```sh
101+export $(systemctl --user show-environment | grep -E '^(WAYLAND_DISPLAY|XDG_RUNTIME_DIR)=')
102+grim -o tv /tmp/screen.png # capture one output (see Screen capture)
103+neferwl pointer abs 0.5 0.5 click # click the middle of the layout
104+neferwl pointer move 200 -50 click right # actions run in order
105+wtype 'hello' # type into the focused window
106+neferwl state # windows, focus, outputs as JSON
107+```
108+
109+- `neferwl pointer` actions: `abs X Y` (fractions 0..1 of the layout, or of one output with `--output <name>`), `move DX DY`, `press`/`release`/`click [BTN]`, `hold [BTN] [DURATION]`, `drag DX DY [BTN]` and `scroll up|down|left|right [N]`. BTN is `left` (default), `right`, `middle`, `side`, `extra`, `forward`, `back`, a `BTN_*` name or an evdev code. `--click-duration` (20ms), `--hold-duration` (500ms), `--drag-steps` (10) and `--drag-step-delay` (10ms) tune the timing. Buttons still pressed at exit are released. See `neferwl pointer --help`.
110+- [wlrctl](https://git.sr.ht/~brocellous/wlrctl) (AUR: `wlrctl`) works too: `wlrctl pointer move 200 -50`, `wlrctl pointer click left`.
111+- Both use `zwlr_virtual_pointer_v1`. NeferWL routes their events like a real mouse, so focus, hover and drags behave as they would for a person. Absolute motion maps into the whole layout, or into one output when the tool binds it (`create_virtual_pointer_with_output`).
112+- `ydotool` writes to `/dev/uinput` through `ydotoold` and does not use this protocol; prefer `neferwl pointer`.
113+- Nothing is injected while the session is locked. Buttons a tool still held when it disconnects are released.
114+- Flatpak and other sandboxed clients (those on a `wp_security_context_v1` socket) do not see the virtual pointer global.
115+- `grim` needs the capture allowlist to admit it; see [the allowlist](capture.md#the-allowlist).
116+
117+[wayvnc](https://github.com/any1/wayvnc) works for remote viewing and control: it captures an output with wlr-screencopy and injects input through the virtual pointer and keyboard. Add its executable (`/usr/bin/wayvnc`) to `/etc/neferwl/capture-allow`, then run it in the session:
118+
119+```sh
120+wayvnc -o tv 127.0.0.1 5900
121+```
122+
123+Keep it on localhost and reach it through an SSH tunnel, or configure wayvnc's own authentication and TLS.
124+
96 ## Hardening125 ## Hardening
97 126
98 On a dedicated box, a local keyboard should not reboot or reach a console:127 On a dedicated box, a local keyboard should not reboot or reach a console:
M go.mod +1 -1
@@ -5,7 +5,7 @@ go 1.27
55 require (
66 bnema.dev/kvconf v0.2.0
77 bnema.dev/purego v0.13.0-bnema.3
8- bnema.dev/purego-libwayland v0.13.0
8+ bnema.dev/purego-libwayland v0.14.0
99 bnema.dev/purego-pipewire v0.6.0
1010 bnema.dev/purego-vulkan v0.7.0
1111 bnema.dev/wlturbo v0.7.0
@@ -5,7 +5,7 @@ go 1.27
5 require (5 require (
6 bnema.dev/kvconf v0.2.06 bnema.dev/kvconf v0.2.0
7 bnema.dev/purego v0.13.0-bnema.37 bnema.dev/purego v0.13.0-bnema.3
8- bnema.dev/purego-libwayland v0.13.08+ bnema.dev/purego-libwayland v0.14.0
9 bnema.dev/purego-pipewire v0.6.09 bnema.dev/purego-pipewire v0.6.0
10 bnema.dev/purego-vulkan v0.7.010 bnema.dev/purego-vulkan v0.7.0
11 bnema.dev/wlturbo v0.7.011 bnema.dev/wlturbo v0.7.0
M go.sum +2 -2 generated file: collapsed
M internal/adapters/headlessinput/input.go +18 -2
@@ -24,12 +24,12 @@ const scriptDeviceID domain.InputDeviceID = 1
2424 // software cursor of the output under them (physical position on it), using
2525 // the latest layout from layouts.
2626 func Run(ctx context.Context, km *xkb.Keymap, keymaps <-chan *xkb.Keymap, script <-chan string, input chan<- ports.InputEvent, layouts <-chan domain.Layout, moveCursor func(output string, x, y float64), log zerowrap.Logger) error {
27- return RunSecure(ctx, km, keymaps, script, input, layouts, moveCursor, log, nil)
27+ return RunSecure(ctx, km, keymaps, script, input, layouts, nil, moveCursor, log, nil)
2828 }
2929
3030 // RunSecure snapshots the protection epoch before each event enters the input
3131 // queue. Scripts are a headless test transport, never an unlock authority.
32-func RunSecure(ctx context.Context, km *xkb.Keymap, keymaps <-chan *xkb.Keymap, script <-chan string, input chan<- ports.InputEvent, layouts <-chan domain.Layout, moveCursor func(output string, x, y float64), log zerowrap.Logger, security ports.SessionSecurity) error {
32+func RunSecure(ctx context.Context, km *xkb.Keymap, keymaps <-chan *xkb.Keymap, script <-chan string, input chan<- ports.InputEvent, layouts <-chan domain.Layout, constraints <-chan domain.PointerConstraint, moveCursor func(output string, x, y float64), log zerowrap.Logger, security ports.SessionSecurity) error {
3333 defer func() { km.Close() }()
3434 activeDevice := scriptDeviceID
3535 announcedDevices := map[domain.InputDeviceID]bool{scriptDeviceID: true}
@@ -172,6 +172,22 @@ func RunSecure(ctx context.Context, km *xkb.Keymap, keymaps <-chan *xkb.Keymap,
172172 km.Close()
173173 km = next
174174 log.Info().Msg("keymap replaced")
175+ case g := <-constraints:
176+ // Core moved the pointer (a warp or a virtual pointer): the
177+ // script continues from there and the cursor follows.
178+ if !g.Warp {
179+ continue
180+ }
181+ key, targets := pointerLayout()
182+ x, y := targets.Clamp(g.X, g.Y, g.X, g.Y)
183+ positions[key] = [2]float64{x, y}
184+ delete(dirtyPositions, key)
185+ if moveCursor != nil {
186+ if o, ok := targets.At(x, y); ok {
187+ tx, ty := o.ToTarget(x, y)
188+ moveCursor(o.Info.Name, tx, ty)
189+ }
190+ }
175191 case next := <-layouts:
176192 if !slices.EqualFunc(layout, next, domain.OutputPlacement.Equal) {
177193 for key := range positions {
@@ -24,12 +24,12 @@ const scriptDeviceID domain.InputDeviceID = 1
24 // software cursor of the output under them (physical position on it), using24 // software cursor of the output under them (physical position on it), using
25 // the latest layout from layouts.25 // the latest layout from layouts.
26 func Run(ctx context.Context, km *xkb.Keymap, keymaps <-chan *xkb.Keymap, script <-chan string, input chan<- ports.InputEvent, layouts <-chan domain.Layout, moveCursor func(output string, x, y float64), log zerowrap.Logger) error {26 func Run(ctx context.Context, km *xkb.Keymap, keymaps <-chan *xkb.Keymap, script <-chan string, input chan<- ports.InputEvent, layouts <-chan domain.Layout, moveCursor func(output string, x, y float64), log zerowrap.Logger) error {
27- return RunSecure(ctx, km, keymaps, script, input, layouts, moveCursor, log, nil)27+ return RunSecure(ctx, km, keymaps, script, input, layouts, nil, moveCursor, log, nil)
28 }28 }
29 29
30 // RunSecure snapshots the protection epoch before each event enters the input30 // RunSecure snapshots the protection epoch before each event enters the input
31 // queue. Scripts are a headless test transport, never an unlock authority.31 // queue. Scripts are a headless test transport, never an unlock authority.
32-func RunSecure(ctx context.Context, km *xkb.Keymap, keymaps <-chan *xkb.Keymap, script <-chan string, input chan<- ports.InputEvent, layouts <-chan domain.Layout, moveCursor func(output string, x, y float64), log zerowrap.Logger, security ports.SessionSecurity) error {32+func RunSecure(ctx context.Context, km *xkb.Keymap, keymaps <-chan *xkb.Keymap, script <-chan string, input chan<- ports.InputEvent, layouts <-chan domain.Layout, constraints <-chan domain.PointerConstraint, moveCursor func(output string, x, y float64), log zerowrap.Logger, security ports.SessionSecurity) error {
33 defer func() { km.Close() }()33 defer func() { km.Close() }()
34 activeDevice := scriptDeviceID34 activeDevice := scriptDeviceID
35 announcedDevices := map[domain.InputDeviceID]bool{scriptDeviceID: true}35 announcedDevices := map[domain.InputDeviceID]bool{scriptDeviceID: true}
@@ -172,6 +172,22 @@ func RunSecure(ctx context.Context, km *xkb.Keymap, keymaps <-chan *xkb.Keymap,
172 km.Close()172 km.Close()
173 km = next173 km = next
174 log.Info().Msg("keymap replaced")174 log.Info().Msg("keymap replaced")
175+ case g := <-constraints:
176+ // Core moved the pointer (a warp or a virtual pointer): the
177+ // script continues from there and the cursor follows.
178+ if !g.Warp {
179+ continue
180+ }
181+ key, targets := pointerLayout()
182+ x, y := targets.Clamp(g.X, g.Y, g.X, g.Y)
183+ positions[key] = [2]float64{x, y}
184+ delete(dirtyPositions, key)
185+ if moveCursor != nil {
186+ if o, ok := targets.At(x, y); ok {
187+ tx, ty := o.ToTarget(x, y)
188+ moveCursor(o.Info.Name, tx, ty)
189+ }
190+ }
175 case next := <-layouts:191 case next := <-layouts:
176 if !slices.EqualFunc(layout, next, domain.OutputPlacement.Equal) {192 if !slices.EqualFunc(layout, next, domain.OutputPlacement.Equal) {
177 for key := range positions {193 for key := range positions {
M internal/adapters/headlessinput/security_test.go +7 -5
@@ -34,7 +34,7 @@ func TestSecureScriptSnapshotsEveryInputKind(t *testing.T) {
3434 security := portsmocks.NewMockSessionSecurity(t)
3535 state := domain.SecurityState{Generation: 7, Protected: true}
3636 security.EXPECT().Snapshot().Return(state)
37- if err := RunSecure(context.Background(), km, nil, script, input, nil, nil, logging.For(context.Background(), "input"), security); err != nil {
37+ if err := RunSecure(context.Background(), km, nil, script, input, nil, nil, nil, logging.For(context.Background(), "input"), security); err != nil {
3838 t.Fatal(err)
3939 }
4040 announcement := (<-input).(ports.SecurityInput)
@@ -116,7 +116,7 @@ func TestSecureScriptDiagnosticsRedactProtectedPayloads(t *testing.T) {
116116 script <- line
117117 }
118118 close(script)
119- if err := RunSecure(context.Background(), km, nil, script, make(chan ports.InputEvent, 32), nil, nil, log, security); err != nil {
119+ if err := RunSecure(context.Background(), km, nil, script, make(chan ports.InputEvent, 32), nil, nil, nil, log, security); err != nil {
120120 t.Fatal(err)
121121 }
122122 output := buf.String()
@@ -175,7 +175,7 @@ func TestSecureScriptSnapshotsBeforeKeyTranslation(t *testing.T) {
175175 script <- "key Shift+A"
176176 close(script)
177177 input := make(chan ports.InputEvent, 5)
178- if err := RunSecure(context.Background(), km, nil, script, input, nil, nil, logging.For(context.Background(), "input"), security); err != nil {
178+ if err := RunSecure(context.Background(), km, nil, script, input, nil, nil, nil, logging.For(context.Background(), "input"), security); err != nil {
179179 t.Fatal(err)
180180 }
181181 <-input // device announcement
@@ -218,7 +218,9 @@ func TestSecureScriptBlockedSendPreservesProductionSnapshot(t *testing.T) {
218218 ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
219219 defer cancel()
220220 done := make(chan error, 1)
221- go func() { done <- RunSecure(ctx, km, nil, script, input, nil, nil, logging.For(ctx, "input"), security) }()
221+ go func() {
222+ done <- RunSecure(ctx, km, nil, script, input, nil, nil, nil, logging.For(ctx, "input"), security)
223+ }()
222224 <-input // initial device announcement
223225 select {
224226 case <-produced:
@@ -264,7 +266,7 @@ func TestSecureScriptQuarantinesModifierAcrossTransition(t *testing.T) {
264266 script <- "key q"
265267 close(script)
266268 input := make(chan ports.InputEvent, 8)
267- if err := RunSecure(context.Background(), km, nil, script, input, nil, nil, logging.For(context.Background(), "input"), security); err != nil {
269+ if err := RunSecure(context.Background(), km, nil, script, input, nil, nil, nil, logging.For(context.Background(), "input"), security); err != nil {
268270 t.Fatal(err)
269271 }
270272 <-input // device announcement
@@ -34,7 +34,7 @@ func TestSecureScriptSnapshotsEveryInputKind(t *testing.T) {
34 security := portsmocks.NewMockSessionSecurity(t)34 security := portsmocks.NewMockSessionSecurity(t)
35 state := domain.SecurityState{Generation: 7, Protected: true}35 state := domain.SecurityState{Generation: 7, Protected: true}
36 security.EXPECT().Snapshot().Return(state)36 security.EXPECT().Snapshot().Return(state)
37- if err := RunSecure(context.Background(), km, nil, script, input, nil, nil, logging.For(context.Background(), "input"), security); err != nil {37+ if err := RunSecure(context.Background(), km, nil, script, input, nil, nil, nil, logging.For(context.Background(), "input"), security); err != nil {
38 t.Fatal(err)38 t.Fatal(err)
39 }39 }
40 announcement := (<-input).(ports.SecurityInput)40 announcement := (<-input).(ports.SecurityInput)
@@ -116,7 +116,7 @@ func TestSecureScriptDiagnosticsRedactProtectedPayloads(t *testing.T) {
116 script <- line116 script <- line
117 }117 }
118 close(script)118 close(script)
119- if err := RunSecure(context.Background(), km, nil, script, make(chan ports.InputEvent, 32), nil, nil, log, security); err != nil {119+ if err := RunSecure(context.Background(), km, nil, script, make(chan ports.InputEvent, 32), nil, nil, nil, log, security); err != nil {
120 t.Fatal(err)120 t.Fatal(err)
121 }121 }
122 output := buf.String()122 output := buf.String()
@@ -175,7 +175,7 @@ func TestSecureScriptSnapshotsBeforeKeyTranslation(t *testing.T) {
175 script <- "key Shift+A"175 script <- "key Shift+A"
176 close(script)176 close(script)
177 input := make(chan ports.InputEvent, 5)177 input := make(chan ports.InputEvent, 5)
178- if err := RunSecure(context.Background(), km, nil, script, input, nil, nil, logging.For(context.Background(), "input"), security); err != nil {178+ if err := RunSecure(context.Background(), km, nil, script, input, nil, nil, nil, logging.For(context.Background(), "input"), security); err != nil {
179 t.Fatal(err)179 t.Fatal(err)
180 }180 }
181 <-input // device announcement181 <-input // device announcement
@@ -218,7 +218,9 @@ func TestSecureScriptBlockedSendPreservesProductionSnapshot(t *testing.T) {
218 ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)218 ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
219 defer cancel()219 defer cancel()
220 done := make(chan error, 1)220 done := make(chan error, 1)
221- go func() { done <- RunSecure(ctx, km, nil, script, input, nil, nil, logging.For(ctx, "input"), security) }()221+ go func() {
222+ done <- RunSecure(ctx, km, nil, script, input, nil, nil, nil, logging.For(ctx, "input"), security)
223+ }()
222 <-input // initial device announcement224 <-input // initial device announcement
223 select {225 select {
224 case <-produced:226 case <-produced:
@@ -264,7 +266,7 @@ func TestSecureScriptQuarantinesModifierAcrossTransition(t *testing.T) {
264 script <- "key q"266 script <- "key q"
265 close(script)267 close(script)
266 input := make(chan ports.InputEvent, 8)268 input := make(chan ports.InputEvent, 8)
267- if err := RunSecure(context.Background(), km, nil, script, input, nil, nil, logging.For(context.Background(), "input"), security); err != nil {269+ if err := RunSecure(context.Background(), km, nil, script, input, nil, nil, nil, logging.For(context.Background(), "input"), security); err != nil {
268 t.Fatal(err)270 t.Fatal(err)
269 }271 }
270 <-input // device announcement272 <-input // device announcement
M internal/adapters/wayland/globals.go +1 -0
@@ -52,6 +52,7 @@ func registerGlobals(d *server.Display, o Options, s *Server) error {
5252 func() error { return registerActivation(d, s) },
5353 func() error { return registerForeign(d, s) },
5454 func() error { return registerVirtualKeyboard(d, s) },
55+ func() error { return registerVirtualPointer(d, s) },
5556 func() error { return registerTextInput(d, s) },
5657 func() error { return registerInputMethod(d, s) },
5758 func() error {
@@ -52,6 +52,7 @@ func registerGlobals(d *server.Display, o Options, s *Server) error {
52 func() error { return registerActivation(d, s) },52 func() error { return registerActivation(d, s) },
53 func() error { return registerForeign(d, s) },53 func() error { return registerForeign(d, s) },
54 func() error { return registerVirtualKeyboard(d, s) },54 func() error { return registerVirtualKeyboard(d, s) },
55+ func() error { return registerVirtualPointer(d, s) },
55 func() error { return registerTextInput(d, s) },56 func() error { return registerTextInput(d, s) },
56 func() error { return registerInputMethod(d, s) },57 func() error { return registerInputMethod(d, s) },
57 func() error {58 func() error {
M internal/adapters/wayland/server.go +3 -0
@@ -175,6 +175,9 @@ type Server struct {
175175 securityStop int
176176 securityWG sync.WaitGroup
177177 securityLive atomic.Int32
178+
179+ // nextVirtualPointer numbers virtual pointer devices (virtualpointer.go).
180+ nextVirtualPointer domain.InputDeviceID
178181 // Test seams, nil in production, set and read on the display goroutine:
179182 // securityCreated receives the client whose security context was created;
180183 // securityDone, captured by each listener at commit, is signalled when that
@@ -175,6 +175,9 @@ type Server struct {
175 securityStop int175 securityStop int
176 securityWG sync.WaitGroup176 securityWG sync.WaitGroup
177 securityLive atomic.Int32177 securityLive atomic.Int32
178+
179+ // nextVirtualPointer numbers virtual pointer devices (virtualpointer.go).
180+ nextVirtualPointer domain.InputDeviceID
178 // Test seams, nil in production, set and read on the display goroutine:181 // Test seams, nil in production, set and read on the display goroutine:
179 // securityCreated receives the client whose security context was created;182 // securityCreated receives the client whose security context was created;
180 // securityDone, captured by each listener at commit, is signalled when that183 // securityDone, captured by each listener at commit, is signalled when that
A internal/adapters/wayland/virtualpointer.go +335 -0
@@ -0,0 +1,335 @@
1+package wayland
2+
3+import (
4+ "time"
5+
6+ "bnema.dev/purego-libwayland/server"
7+ "github.com/bnema/go-wayland-bindings/server/wayland"
8+ "github.com/bnema/go-wayland-bindings/server/wlrvirtualpointer"
9+ "github.com/bnema/neferwl/internal/domain"
10+ "github.com/bnema/neferwl/internal/ports"
11+ "golang.org/x/sys/unix"
12+)
13+
14+// virtualPointerDevices is the first device identity of virtual pointers;
15+// real producers count up from 1 and never reach it.
16+const virtualPointerDevices domain.InputDeviceID = 1 << 62
17+
18+// virtualPointerInterface is the manager global, hidden from sandboxed clients.
19+const virtualPointerInterface = "zwlr_virtual_pointer_manager_v1"
20+
21+// maxVirtualFrame bounds the requests one frame may hold: a client that
22+// never sends frame cannot grow compositor memory. Exceeding it is a
23+// wl_display implementation error and disconnects the client.
24+const maxVirtualFrame = 256
25+
26+// maxVirtualHeld bounds the buttons one virtual pointer holds at once.
27+const maxVirtualHeld = 16
28+
29+// virtualButtonValid reports whether b is an evdev button code
30+// (BTN_MISC 0x100 to BTN_GEAR_UP 0x151).
31+func virtualButtonValid(b uint32) bool { return b >= 0x100 && b <= 0x151 }
32+
33+// virtualFrameKeep is the largest pending capacity kept across frames.
34+const virtualFrameKeep = 32
35+
36+// virtualPointer is a zwlr_virtual_pointer_v1 (wlrctl, agents).
37+// Requests accumulate, in order, until frame; only then do they go to core
38+// as ports.VirtualPointerInput. Consecutive motions of one kind merge, as do
39+// consecutive axis requests. Core places the pointer and routes the events
40+// as real input; the adapter only translates.
41+//
42+// forwarded are the buttons whose press reached core in this epoch: when
43+// the resource dies they are released, and pending (unframed) requests are
44+// discarded.
45+type virtualPointer struct {
46+ server *Server
47+ res *wlrvirtualpointer.ZwlrVirtualPointerV1
48+ device domain.InputDeviceID
49+ output string // create_virtual_pointer_with_output, "" for the layout
50+ pending []ports.VirtualPointerInput
51+ source domain.AxisSource // axis_source of the frame, sticky
52+ forwarded map[uint32]bool
53+ generation domain.LockGeneration
54+ dead bool // protocol error sent: requests are ignored
55+ warned bool // a dropped button was logged
56+}
57+
58+// registerVirtualPointer exposes the global to unsandboxed clients
59+// (globalVisible); a sandboxed bind is refused anyway.
60+func registerVirtualPointer(d *server.Display, s *Server) error {
61+ return wlrvirtualpointer.NewZwlrVirtualPointerManagerV1Global(d, 2, func(c server.Client, v, id uint32) {
62+ _, _ = wlrvirtualpointer.NewZwlrVirtualPointerManagerV1(c, int32(v), id, virtualPointerManager{s})
63+ })
64+}
65+
66+type virtualPointerManager struct{ server *Server }
67+
68+func (virtualPointerManager) Destroy(*wlrvirtualpointer.ZwlrVirtualPointerManagerV1) {}
69+
70+func (m virtualPointerManager) CreateVirtualPointer(r *wlrvirtualpointer.ZwlrVirtualPointerManagerV1, seat *wayland.Seat, id uint32) {
71+ m.CreateVirtualPointerWithOutput(r, seat, nil, id)
72+}
73+
74+func (m virtualPointerManager) CreateVirtualPointerWithOutput(r *wlrvirtualpointer.ZwlrVirtualPointerManagerV1, _ *wayland.Seat, output *wayland.Output, id uint32) {
75+ s := m.server
76+ p := &virtualPointer{server: s, forwarded: map[uint32]bool{}}
77+ if o := s.outputOf(output); o != nil {
78+ p.output = o.name()
79+ }
80+ res, err := wlrvirtualpointer.NewZwlrVirtualPointerV1(r.Client(), r.Version(), id, p)
81+ if err != nil {
82+ return
83+ }
84+ if s.sandboxed[r.Client()] != nil {
85+ // Inert: a sandboxed client never drives the seat.
86+ p.server = nil
87+ s.log.Info().Int("pid", r.Client().PID()).Msg("virtual pointer refused: sandboxed client")
88+ return
89+ }
90+ p.res = res
91+ s.nextVirtualPointer++
92+ p.device = virtualPointerDevices + s.nextVirtualPointer
93+ p.generation = s.securityState().Generation
94+ res.OnDestroy = p.release
95+ s.log.Info().Int("pid", r.Client().PID()).Str("output", p.output).Msg("virtual pointer created")
96+}
97+
98+func (s *Server) securityState() domain.SecurityState {
99+ if s.security == nil {
100+ return domain.SecurityState{}
101+ }
102+ return s.security.Snapshot()
103+}
104+
105+// admitted drops the pending frame and forwarded buttons when protection
106+// began or passed since the last request: nothing from before a lock is
107+// replayed or released into it.
108+func (p *virtualPointer) admitted() bool {
109+ if p.server == nil || p.dead {
110+ return false
111+ }
112+ state := p.server.securityState()
113+ if state.Protected || state.Generation != p.generation {
114+ p.reset()
115+ clear(p.forwarded)
116+ p.generation = state.Generation
117+ }
118+ return !state.Protected
119+}
120+
121+// reset drops the pending frame; an oversized buffer is not kept.
122+func (p *virtualPointer) reset() {
123+ if cap(p.pending) > virtualFrameKeep {
124+ p.pending = nil
125+ return
126+ }
127+ clear(p.pending)
128+ p.pending = p.pending[:0]
129+}
130+
131+func msec(t uint32) time.Duration { return time.Duration(t) * time.Millisecond }
132+
133+// add appends one request to the frame, or reports false after a protocol
134+// error for a frame over maxVirtualFrame.
135+func (p *virtualPointer) add(v ports.VirtualPointerInput) bool {
136+ if len(p.pending) >= maxVirtualFrame {
137+ p.reset()
138+ p.pending = nil
139+ p.dead = true
140+ // The protocol has no error for it: a compositor limit is an
141+ // implementation error on wl_display.
142+ p.res.Client().PostImplementationError("zwlr_virtual_pointer_v1: frame holds more than 256 requests")
143+ return false
144+ }
145+ p.pending = append(p.pending, v)
146+ return true
147+}
148+
149+// last is the newest pending request, or nil.
150+func (p *virtualPointer) last() *ports.VirtualPointerInput {
151+ if len(p.pending) == 0 {
152+ return nil
153+ }
154+ return &p.pending[len(p.pending)-1]
155+}
156+
157+func (p *virtualPointer) Motion(_ *wlrvirtualpointer.ZwlrVirtualPointerV1, t uint32, dx, dy server.Fixed) {
158+ if !p.admitted() {
159+ return
160+ }
161+ if l := p.last(); l != nil {
162+ if m, ok := l.Event.(ports.PointerMotion); ok && !l.Absolute {
163+ m.DX += dx.Float()
164+ m.DY += dy.Float()
165+ m.UnaccelDX, m.UnaccelDY, m.Time = m.DX, m.DY, msec(t)
166+ l.Event = m
167+ return
168+ }
169+ }
170+ p.add(ports.VirtualPointerInput{Event: ports.PointerMotion{Device: p.device, DX: dx.Float(), DY: dy.Float(), UnaccelDX: dx.Float(), UnaccelDY: dy.Float(), Time: msec(t)}})
171+}
172+
173+// MotionAbsolute maps x/x_extent into [0, 1]; x may equal the extent.
174+// Core clamps the point into the output or layout.
175+func (p *virtualPointer) MotionAbsolute(_ *wlrvirtualpointer.ZwlrVirtualPointerV1, t, x, y, xExtent, yExtent uint32) {
176+ if !p.admitted() || xExtent == 0 || yExtent == 0 {
177+ return
178+ }
179+ v := ports.VirtualPointerInput{
180+ Absolute: true, Output: p.output,
181+ FX: float64(min(x, xExtent)) / float64(xExtent),
182+ FY: float64(min(y, yExtent)) / float64(yExtent),
183+ Event: ports.PointerMotion{Device: p.device, Time: msec(t)},
184+ }
185+ if l := p.last(); l != nil && l.Absolute {
186+ *l = v
187+ return
188+ }
189+ p.add(v)
190+}
191+
192+func (p *virtualPointer) Button(_ *wlrvirtualpointer.ZwlrVirtualPointerV1, t, button, state uint32) {
193+ if !p.admitted() {
194+ return
195+ }
196+ pressed := state == uint32(wayland.PointerButtonStatePressed)
197+ if !virtualButtonValid(button) {
198+ p.dropButton(button, "not an evdev button code")
199+ return
200+ }
201+ p.add(ports.VirtualPointerInput{Event: ports.PointerButton{Device: p.device, Button: button, Pressed: pressed, Time: msec(t)}})
202+}
203+
204+// editAxis applies edit to the frame's axis request on axis, appending one
205+// unless the newest request is already a scroll. Bad axes are an
206+// invalid_axis error.
207+func (p *virtualPointer) editAxis(r *wlrvirtualpointer.ZwlrVirtualPointerV1, axis, t uint32, edit func(*domain.ScrollAxis)) {
208+ vertical := axis == uint32(wayland.PointerAxisVerticalScroll)
209+ if !vertical && axis != uint32(wayland.PointerAxisHorizontalScroll) {
210+ r.PostError(uint32(wlrvirtualpointer.ZwlrVirtualPointerV1ErrorInvalidAxis), "invalid axis")
211+ return
212+ }
213+ if !p.admitted() {
214+ return
215+ }
216+ l := p.last()
217+ if l == nil || !isAxis(l.Event) {
218+ if !p.add(ports.VirtualPointerInput{Event: ports.PointerAxis{Device: p.device}}) {
219+ return
220+ }
221+ l = p.last()
222+ }
223+ a := l.Event.(ports.PointerAxis)
224+ a.Time, a.Source = msec(t), p.source
225+ if vertical {
226+ edit(&a.Vertical)
227+ } else {
228+ edit(&a.Horizontal)
229+ }
230+ l.Event = a
231+}
232+
233+func isAxis(ev ports.InputEvent) bool { _, ok := ev.(ports.PointerAxis); return ok }
234+
235+func (p *virtualPointer) Axis(r *wlrvirtualpointer.ZwlrVirtualPointerV1, t, axis uint32, value server.Fixed) {
236+ p.editAxis(r, axis, t, func(a *domain.ScrollAxis) {
237+ a.Set = true
238+ a.Value += value.Float()
239+ })
240+}
241+
242+func (p *virtualPointer) AxisDiscrete(r *wlrvirtualpointer.ZwlrVirtualPointerV1, t, axis uint32, value server.Fixed, discrete int32) {
243+ p.editAxis(r, axis, t, func(a *domain.ScrollAxis) {
244+ a.Set = true
245+ a.Value += value.Float()
246+ a.V120 += discrete * 120
247+ })
248+}
249+
250+func (p *virtualPointer) AxisStop(r *wlrvirtualpointer.ZwlrVirtualPointerV1, t, axis uint32) {
251+ p.editAxis(r, axis, t, func(a *domain.ScrollAxis) { a.Set, a.Stop = true, true })
252+}
253+
254+func (p *virtualPointer) AxisSource(r *wlrvirtualpointer.ZwlrVirtualPointerV1, source uint32) {
255+ switch source {
256+ case uint32(wayland.PointerAxisSourceWheel), uint32(wayland.PointerAxisSourceWheelTilt):
257+ p.source = domain.AxisWheel
258+ case uint32(wayland.PointerAxisSourceFinger):
259+ p.source = domain.AxisFinger
260+ case uint32(wayland.PointerAxisSourceContinuous):
261+ p.source = domain.AxisContinuous
262+ default:
263+ r.PostError(uint32(wlrvirtualpointer.ZwlrVirtualPointerV1ErrorInvalidAxisSource), "invalid axis source")
264+ return
265+ }
266+ if l := p.last(); l != nil && isAxis(l.Event) {
267+ a := l.Event.(ports.PointerAxis)
268+ a.Source = p.source
269+ l.Event = a
270+ }
271+}
272+
273+// Frame hands the pending requests to core, in order. A release whose press
274+// was not forwarded in this epoch is dropped, as is a repeated press.
275+func (p *virtualPointer) Frame(*wlrvirtualpointer.ZwlrVirtualPointerV1) {
276+ if !p.admitted() {
277+ return
278+ }
279+ state := p.server.securityState()
280+ for _, v := range p.pending {
281+ if b, ok := v.Event.(ports.PointerButton); ok {
282+ if b.Pressed == p.forwarded[b.Button] {
283+ continue
284+ }
285+ if b.Pressed && len(p.forwarded) >= maxVirtualHeld {
286+ p.dropButton(b.Button, "too many held buttons")
287+ continue
288+ }
289+ if b.Pressed {
290+ p.forwarded[b.Button] = true
291+ } else {
292+ delete(p.forwarded, b.Button)
293+ }
294+ }
295+ v.State = state
296+ v.Output = p.output
297+ p.server.emit(v)
298+ }
299+ p.reset()
300+}
301+
302+// dropButton ignores a press the pointer may not forward. The protocol has
303+// no error for it, and dropping is harmless: nothing is held that would
304+// need a release. Logged once per pointer.
305+func (p *virtualPointer) dropButton(b uint32, why string) {
306+ if !p.warned {
307+ p.warned = true
308+ p.server.log.Warn().Uint32("button", b).Str("reason", why).Msg("virtual pointer button dropped")
309+ }
310+}
311+
312+func (*virtualPointer) Destroy(*wlrvirtualpointer.ZwlrVirtualPointerV1) {}
313+
314+// release runs when the resource dies, by request or disconnect: unframed
315+// requests are discarded, and every button whose press was forwarded in
316+// this epoch is released so no client keeps a stuck drag.
317+func (p *virtualPointer) release() {
318+ p.dead = false // a protocol error still releases what was forwarded
319+ if !p.admitted() {
320+ return
321+ }
322+ p.reset()
323+ if len(p.forwarded) == 0 {
324+ return
325+ }
326+ var ts unix.Timespec
327+ _ = unix.ClockGettime(unix.CLOCK_MONOTONIC, &ts)
328+ now := time.Duration(ts.Nano())
329+ state := p.server.securityState()
330+ for b := range p.forwarded {
331+ p.server.emit(ports.VirtualPointerInput{State: state, Output: p.output, Event: ports.PointerButton{Device: p.device, Button: b, Time: now}})
332+ }
333+ clear(p.forwarded)
334+ p.dead = true
335+}
@@ -0,0 +1,335 @@
1+package wayland
2+
3+import (
4+ "time"
5+
6+ "bnema.dev/purego-libwayland/server"
7+ "github.com/bnema/go-wayland-bindings/server/wayland"
8+ "github.com/bnema/go-wayland-bindings/server/wlrvirtualpointer"
9+ "github.com/bnema/neferwl/internal/domain"
10+ "github.com/bnema/neferwl/internal/ports"
11+ "golang.org/x/sys/unix"
12+)
13+
14+// virtualPointerDevices is the first device identity of virtual pointers;
15+// real producers count up from 1 and never reach it.
16+const virtualPointerDevices domain.InputDeviceID = 1 << 62
17+
18+// virtualPointerInterface is the manager global, hidden from sandboxed clients.
19+const virtualPointerInterface = "zwlr_virtual_pointer_manager_v1"
20+
21+// maxVirtualFrame bounds the requests one frame may hold: a client that
22+// never sends frame cannot grow compositor memory. Exceeding it is a
23+// wl_display implementation error and disconnects the client.
24+const maxVirtualFrame = 256
25+
26+// maxVirtualHeld bounds the buttons one virtual pointer holds at once.
27+const maxVirtualHeld = 16
28+
29+// virtualButtonValid reports whether b is an evdev button code
30+// (BTN_MISC 0x100 to BTN_GEAR_UP 0x151).
31+func virtualButtonValid(b uint32) bool { return b >= 0x100 && b <= 0x151 }
32+
33+// virtualFrameKeep is the largest pending capacity kept across frames.
34+const virtualFrameKeep = 32
35+
36+// virtualPointer is a zwlr_virtual_pointer_v1 (wlrctl, agents).
37+// Requests accumulate, in order, until frame; only then do they go to core
38+// as ports.VirtualPointerInput. Consecutive motions of one kind merge, as do
39+// consecutive axis requests. Core places the pointer and routes the events
40+// as real input; the adapter only translates.
41+//
42+// forwarded are the buttons whose press reached core in this epoch: when
43+// the resource dies they are released, and pending (unframed) requests are
44+// discarded.
45+type virtualPointer struct {
46+ server *Server
47+ res *wlrvirtualpointer.ZwlrVirtualPointerV1
48+ device domain.InputDeviceID
49+ output string // create_virtual_pointer_with_output, "" for the layout
50+ pending []ports.VirtualPointerInput
51+ source domain.AxisSource // axis_source of the frame, sticky
52+ forwarded map[uint32]bool
53+ generation domain.LockGeneration
54+ dead bool // protocol error sent: requests are ignored
55+ warned bool // a dropped button was logged
56+}
57+
58+// registerVirtualPointer exposes the global to unsandboxed clients
59+// (globalVisible); a sandboxed bind is refused anyway.
60+func registerVirtualPointer(d *server.Display, s *Server) error {
61+ return wlrvirtualpointer.NewZwlrVirtualPointerManagerV1Global(d, 2, func(c server.Client, v, id uint32) {
62+ _, _ = wlrvirtualpointer.NewZwlrVirtualPointerManagerV1(c, int32(v), id, virtualPointerManager{s})
63+ })
64+}
65+
66+type virtualPointerManager struct{ server *Server }
67+
68+func (virtualPointerManager) Destroy(*wlrvirtualpointer.ZwlrVirtualPointerManagerV1) {}
69+
70+func (m virtualPointerManager) CreateVirtualPointer(r *wlrvirtualpointer.ZwlrVirtualPointerManagerV1, seat *wayland.Seat, id uint32) {
71+ m.CreateVirtualPointerWithOutput(r, seat, nil, id)
72+}
73+
74+func (m virtualPointerManager) CreateVirtualPointerWithOutput(r *wlrvirtualpointer.ZwlrVirtualPointerManagerV1, _ *wayland.Seat, output *wayland.Output, id uint32) {
75+ s := m.server
76+ p := &virtualPointer{server: s, forwarded: map[uint32]bool{}}
77+ if o := s.outputOf(output); o != nil {
78+ p.output = o.name()
79+ }
80+ res, err := wlrvirtualpointer.NewZwlrVirtualPointerV1(r.Client(), r.Version(), id, p)
81+ if err != nil {
82+ return
83+ }
84+ if s.sandboxed[r.Client()] != nil {
85+ // Inert: a sandboxed client never drives the seat.
86+ p.server = nil
87+ s.log.Info().Int("pid", r.Client().PID()).Msg("virtual pointer refused: sandboxed client")
88+ return
89+ }
90+ p.res = res
91+ s.nextVirtualPointer++
92+ p.device = virtualPointerDevices + s.nextVirtualPointer
93+ p.generation = s.securityState().Generation
94+ res.OnDestroy = p.release
95+ s.log.Info().Int("pid", r.Client().PID()).Str("output", p.output).Msg("virtual pointer created")
96+}
97+
98+func (s *Server) securityState() domain.SecurityState {
99+ if s.security == nil {
100+ return domain.SecurityState{}
101+ }
102+ return s.security.Snapshot()
103+}
104+
105+// admitted drops the pending frame and forwarded buttons when protection
106+// began or passed since the last request: nothing from before a lock is
107+// replayed or released into it.
108+func (p *virtualPointer) admitted() bool {
109+ if p.server == nil || p.dead {
110+ return false
111+ }
112+ state := p.server.securityState()
113+ if state.Protected || state.Generation != p.generation {
114+ p.reset()
115+ clear(p.forwarded)
116+ p.generation = state.Generation
117+ }
118+ return !state.Protected
119+}
120+
121+// reset drops the pending frame; an oversized buffer is not kept.
122+func (p *virtualPointer) reset() {
123+ if cap(p.pending) > virtualFrameKeep {
124+ p.pending = nil
125+ return
126+ }
127+ clear(p.pending)
128+ p.pending = p.pending[:0]
129+}
130+
131+func msec(t uint32) time.Duration { return time.Duration(t) * time.Millisecond }
132+
133+// add appends one request to the frame, or reports false after a protocol
134+// error for a frame over maxVirtualFrame.
135+func (p *virtualPointer) add(v ports.VirtualPointerInput) bool {
136+ if len(p.pending) >= maxVirtualFrame {
137+ p.reset()
138+ p.pending = nil
139+ p.dead = true
140+ // The protocol has no error for it: a compositor limit is an
141+ // implementation error on wl_display.
142+ p.res.Client().PostImplementationError("zwlr_virtual_pointer_v1: frame holds more than 256 requests")
143+ return false
144+ }
145+ p.pending = append(p.pending, v)
146+ return true
147+}
148+
149+// last is the newest pending request, or nil.
150+func (p *virtualPointer) last() *ports.VirtualPointerInput {
151+ if len(p.pending) == 0 {
152+ return nil
153+ }
154+ return &p.pending[len(p.pending)-1]
155+}
156+
157+func (p *virtualPointer) Motion(_ *wlrvirtualpointer.ZwlrVirtualPointerV1, t uint32, dx, dy server.Fixed) {
158+ if !p.admitted() {
159+ return
160+ }
161+ if l := p.last(); l != nil {
162+ if m, ok := l.Event.(ports.PointerMotion); ok && !l.Absolute {
163+ m.DX += dx.Float()
164+ m.DY += dy.Float()
165+ m.UnaccelDX, m.UnaccelDY, m.Time = m.DX, m.DY, msec(t)
166+ l.Event = m
167+ return
168+ }
169+ }
170+ p.add(ports.VirtualPointerInput{Event: ports.PointerMotion{Device: p.device, DX: dx.Float(), DY: dy.Float(), UnaccelDX: dx.Float(), UnaccelDY: dy.Float(), Time: msec(t)}})
171+}
172+
173+// MotionAbsolute maps x/x_extent into [0, 1]; x may equal the extent.
174+// Core clamps the point into the output or layout.
175+func (p *virtualPointer) MotionAbsolute(_ *wlrvirtualpointer.ZwlrVirtualPointerV1, t, x, y, xExtent, yExtent uint32) {
176+ if !p.admitted() || xExtent == 0 || yExtent == 0 {
177+ return
178+ }
179+ v := ports.VirtualPointerInput{
180+ Absolute: true, Output: p.output,
181+ FX: float64(min(x, xExtent)) / float64(xExtent),
182+ FY: float64(min(y, yExtent)) / float64(yExtent),
183+ Event: ports.PointerMotion{Device: p.device, Time: msec(t)},
184+ }
185+ if l := p.last(); l != nil && l.Absolute {
186+ *l = v
187+ return
188+ }
189+ p.add(v)
190+}
191+
192+func (p *virtualPointer) Button(_ *wlrvirtualpointer.ZwlrVirtualPointerV1, t, button, state uint32) {
193+ if !p.admitted() {
194+ return
195+ }
196+ pressed := state == uint32(wayland.PointerButtonStatePressed)
197+ if !virtualButtonValid(button) {
198+ p.dropButton(button, "not an evdev button code")
199+ return
200+ }
201+ p.add(ports.VirtualPointerInput{Event: ports.PointerButton{Device: p.device, Button: button, Pressed: pressed, Time: msec(t)}})
202+}
203+
204+// editAxis applies edit to the frame's axis request on axis, appending one
205+// unless the newest request is already a scroll. Bad axes are an
206+// invalid_axis error.
207+func (p *virtualPointer) editAxis(r *wlrvirtualpointer.ZwlrVirtualPointerV1, axis, t uint32, edit func(*domain.ScrollAxis)) {
208+ vertical := axis == uint32(wayland.PointerAxisVerticalScroll)
209+ if !vertical && axis != uint32(wayland.PointerAxisHorizontalScroll) {
210+ r.PostError(uint32(wlrvirtualpointer.ZwlrVirtualPointerV1ErrorInvalidAxis), "invalid axis")
211+ return
212+ }
213+ if !p.admitted() {
214+ return
215+ }
216+ l := p.last()
217+ if l == nil || !isAxis(l.Event) {
218+ if !p.add(ports.VirtualPointerInput{Event: ports.PointerAxis{Device: p.device}}) {
219+ return
220+ }
221+ l = p.last()
222+ }
223+ a := l.Event.(ports.PointerAxis)
224+ a.Time, a.Source = msec(t), p.source
225+ if vertical {
226+ edit(&a.Vertical)
227+ } else {
228+ edit(&a.Horizontal)
229+ }
230+ l.Event = a
231+}
232+
233+func isAxis(ev ports.InputEvent) bool { _, ok := ev.(ports.PointerAxis); return ok }
234+
235+func (p *virtualPointer) Axis(r *wlrvirtualpointer.ZwlrVirtualPointerV1, t, axis uint32, value server.Fixed) {
236+ p.editAxis(r, axis, t, func(a *domain.ScrollAxis) {
237+ a.Set = true
238+ a.Value += value.Float()
239+ })
240+}
241+
242+func (p *virtualPointer) AxisDiscrete(r *wlrvirtualpointer.ZwlrVirtualPointerV1, t, axis uint32, value server.Fixed, discrete int32) {
243+ p.editAxis(r, axis, t, func(a *domain.ScrollAxis) {
244+ a.Set = true
245+ a.Value += value.Float()
246+ a.V120 += discrete * 120
247+ })
248+}
249+
250+func (p *virtualPointer) AxisStop(r *wlrvirtualpointer.ZwlrVirtualPointerV1, t, axis uint32) {
251+ p.editAxis(r, axis, t, func(a *domain.ScrollAxis) { a.Set, a.Stop = true, true })
252+}
253+
254+func (p *virtualPointer) AxisSource(r *wlrvirtualpointer.ZwlrVirtualPointerV1, source uint32) {
255+ switch source {
256+ case uint32(wayland.PointerAxisSourceWheel), uint32(wayland.PointerAxisSourceWheelTilt):
257+ p.source = domain.AxisWheel
258+ case uint32(wayland.PointerAxisSourceFinger):
259+ p.source = domain.AxisFinger
260+ case uint32(wayland.PointerAxisSourceContinuous):
261+ p.source = domain.AxisContinuous
262+ default:
263+ r.PostError(uint32(wlrvirtualpointer.ZwlrVirtualPointerV1ErrorInvalidAxisSource), "invalid axis source")
264+ return
265+ }
266+ if l := p.last(); l != nil && isAxis(l.Event) {
267+ a := l.Event.(ports.PointerAxis)
268+ a.Source = p.source
269+ l.Event = a
270+ }
271+}
272+
273+// Frame hands the pending requests to core, in order. A release whose press
274+// was not forwarded in this epoch is dropped, as is a repeated press.
275+func (p *virtualPointer) Frame(*wlrvirtualpointer.ZwlrVirtualPointerV1) {
276+ if !p.admitted() {
277+ return
278+ }
279+ state := p.server.securityState()
280+ for _, v := range p.pending {
281+ if b, ok := v.Event.(ports.PointerButton); ok {
282+ if b.Pressed == p.forwarded[b.Button] {
283+ continue
284+ }
285+ if b.Pressed && len(p.forwarded) >= maxVirtualHeld {
286+ p.dropButton(b.Button, "too many held buttons")
287+ continue
288+ }
289+ if b.Pressed {
290+ p.forwarded[b.Button] = true
291+ } else {
292+ delete(p.forwarded, b.Button)
293+ }
294+ }
295+ v.State = state
296+ v.Output = p.output
297+ p.server.emit(v)
298+ }
299+ p.reset()
300+}
301+
302+// dropButton ignores a press the pointer may not forward. The protocol has
303+// no error for it, and dropping is harmless: nothing is held that would
304+// need a release. Logged once per pointer.
305+func (p *virtualPointer) dropButton(b uint32, why string) {
306+ if !p.warned {
307+ p.warned = true
308+ p.server.log.Warn().Uint32("button", b).Str("reason", why).Msg("virtual pointer button dropped")
309+ }
310+}
311+
312+func (*virtualPointer) Destroy(*wlrvirtualpointer.ZwlrVirtualPointerV1) {}
313+
314+// release runs when the resource dies, by request or disconnect: unframed
315+// requests are discarded, and every button whose press was forwarded in
316+// this epoch is released so no client keeps a stuck drag.
317+func (p *virtualPointer) release() {
318+ p.dead = false // a protocol error still releases what was forwarded
319+ if !p.admitted() {
320+ return
321+ }
322+ p.reset()
323+ if len(p.forwarded) == 0 {
324+ return
325+ }
326+ var ts unix.Timespec
327+ _ = unix.ClockGettime(unix.CLOCK_MONOTONIC, &ts)
328+ now := time.Duration(ts.Nano())
329+ state := p.server.securityState()
330+ for b := range p.forwarded {
331+ p.server.emit(ports.VirtualPointerInput{State: state, Output: p.output, Event: ports.PointerButton{Device: p.device, Button: b, Time: now}})
332+ }
333+ clear(p.forwarded)
334+ p.dead = true
335+}
A internal/adapters/wayland/virtualpointer_test.go +374 -0
@@ -0,0 +1,374 @@
1+package wayland
2+
3+import (
4+ "testing"
5+
6+ "bnema.dev/wlturbo"
7+ "github.com/bnema/go-wayland-bindings/server/wayland"
8+ "github.com/bnema/go-wayland-bindings/server/wlrvirtualpointer"
9+ "github.com/bnema/neferwl/internal/domain"
10+ "github.com/bnema/neferwl/internal/ports"
11+)
12+
13+// virtualMover binds a virtual pointer on its own connection, as wlrctl
14+// does; withOutput uses create_virtual_pointer_with_output on wl_output.
15+func virtualMover(t *testing.T, s *Server, dir string, withOutput bool) (*wlturbo.Display, uint32) {
16+ t.Helper()
17+ c := protocolClient(t, s, dir)
18+ seat := bindProtocol(t, c, "wl_seat")
19+ registerProtocol(t, c, seat)
20+ manager := bindVersion(t, c, virtualPointerInterface, 2)
21+ registerProtocol(t, c, manager)
22+ if withOutput {
23+ out := bindVersion(t, c, "wl_output", 4)
24+ registerProtocol(t, c, out)
25+ p := c.AllocateID()
26+ registerProtocol(t, c, p)
27+ requestProtocol(t, c, manager, wlrvirtualpointer.ZwlrVirtualPointerManagerV1RequestCreateVirtualPointerWithOutput, seat, out, p)
28+ return c, p
29+ }
30+ p := c.AllocateID()
31+ registerProtocol(t, c, p)
32+ requestProtocol(t, c, manager, wlrvirtualpointer.ZwlrVirtualPointerManagerV1RequestCreateVirtualPointer, seat, p)
33+ return c, p
34+}
35+
36+func fixed(v float64) wlturbo.Fixed { return wlturbo.Fixed(int32(v * 256)) }
37+
38+// virtualEvents waits for n VirtualPointerInput events after a roundtrip
39+// and fails on any further one already forwarded.
40+func virtualEvents(t *testing.T, c *wlturbo.Display, events chan ports.ClientEvent, n int) []ports.VirtualPointerInput {
41+ t.Helper()
42+ roundtrip(t, c)
43+ var out []ports.VirtualPointerInput
44+ for len(out) < n {
45+ out = append(out, nextEvent[ports.VirtualPointerInput](t, events))
46+ }
47+ for {
48+ select {
49+ case ev := <-events:
50+ if v, ok := ev.(ports.VirtualPointerInput); ok {
51+ t.Fatalf("unexpected virtual event %+v", v)
52+ }
53+ default:
54+ return out
55+ }
56+ }
57+}
58+
59+// Requests are held until frame and reach core in one batch: motion, then
60+// buttons, then scroll, under the virtual device identity.
61+func TestVirtualPointerFrameBatch(t *testing.T) {
62+ s, events, _, dir := keyboardServer(t)
63+ c, p := virtualMover(t, s, dir, false)
64+ requestProtocol(t, c, p, wlrvirtualpointer.ZwlrVirtualPointerV1RequestMotion, uint32(5), fixed(10), fixed(-4))
65+ requestProtocol(t, c, p, wlrvirtualpointer.ZwlrVirtualPointerV1RequestMotion, uint32(6), fixed(2.5), fixed(1))
66+ requestProtocol(t, c, p, wlrvirtualpointer.ZwlrVirtualPointerV1RequestButton, uint32(7), uint32(0x110), uint32(1))
67+ requestProtocol(t, c, p, wlrvirtualpointer.ZwlrVirtualPointerV1RequestAxisSource, uint32(0))
68+ requestProtocol(t, c, p, wlrvirtualpointer.ZwlrVirtualPointerV1RequestAxisDiscrete, uint32(8), uint32(0), fixed(15), int32(1))
69+ roundtrip(t, c)
70+ select {
71+ case ev := <-events:
72+ if _, ok := ev.(ports.VirtualPointerInput); ok {
73+ t.Fatal("virtual event before frame")
74+ }
75+ default:
76+ }
77+ requestProtocol(t, c, p, wlrvirtualpointer.ZwlrVirtualPointerV1RequestFrame)
78+ got := virtualEvents(t, c, events, 3)
79+ m, ok := got[0].Event.(ports.PointerMotion)
80+ if !ok || got[0].Absolute || m.DX != 12.5 || m.DY != -3 || m.Device < virtualPointerDevices {
81+ t.Fatalf("motion %+v", got[0])
82+ }
83+ b, ok := got[1].Event.(ports.PointerButton)
84+ if !ok || b.Button != 0x110 || !b.Pressed || b.Device != m.Device {
85+ t.Fatalf("button %+v", got[1])
86+ }
87+ a, ok := got[2].Event.(ports.PointerAxis)
88+ if !ok || a.Source != domain.AxisWheel || !a.Vertical.Set || a.Vertical.Value != 15 || a.Vertical.V120 != 120 || a.Horizontal.Set {
89+ t.Fatalf("axis %+v", got[2])
90+ }
91+}
92+
93+// Absolute motion becomes a fraction of the layout, or of the bound output.
94+func TestVirtualPointerAbsolute(t *testing.T) {
95+ for _, withOutput := range []bool{false, true} {
96+ t.Run(map[bool]string{false: "layout", true: "with-output"}[withOutput], func(t *testing.T) {
97+ s, events, _, dir := keyboardServer(t)
98+ c, p := virtualMover(t, s, dir, withOutput)
99+ requestProtocol(t, c, p, wlrvirtualpointer.ZwlrVirtualPointerV1RequestMotionAbsolute, uint32(1), uint32(480), uint32(270), uint32(1920), uint32(1080))
100+ requestProtocol(t, c, p, wlrvirtualpointer.ZwlrVirtualPointerV1RequestFrame)
101+ got := virtualEvents(t, c, events, 1)[0]
102+ want := ""
103+ if withOutput {
104+ want = "HEADLESS-1"
105+ }
106+ if !got.Absolute || got.FX != 0.25 || got.FY != 0.25 || got.Output != want {
107+ t.Fatalf("absolute %+v", got)
108+ }
109+ requestProtocol(t, c, p, wlrvirtualpointer.ZwlrVirtualPointerV1RequestMotion, uint32(2), fixed(5), fixed(3))
110+ requestProtocol(t, c, p, wlrvirtualpointer.ZwlrVirtualPointerV1RequestButton, uint32(2), uint32(0x110), uint32(1))
111+ requestProtocol(t, c, p, wlrvirtualpointer.ZwlrVirtualPointerV1RequestFrame)
112+ for _, event := range virtualEvents(t, c, events, 2) {
113+ if event.Output != want {
114+ t.Fatalf("lost output association: %+v", event)
115+ }
116+ }
117+ })
118+ }
119+}
120+
121+// Losing the pointer mid-click releases its buttons; an unpaired release
122+// never goes out.
123+func TestVirtualPointerDisconnectReleases(t *testing.T) {
124+ s, events, _, dir := keyboardServer(t)
125+ c, p := virtualMover(t, s, dir, false)
126+ requestProtocol(t, c, p, wlrvirtualpointer.ZwlrVirtualPointerV1RequestButton, uint32(1), uint32(0x111), uint32(0))
127+ requestProtocol(t, c, p, wlrvirtualpointer.ZwlrVirtualPointerV1RequestButton, uint32(1), uint32(0x110), uint32(1))
128+ requestProtocol(t, c, p, wlrvirtualpointer.ZwlrVirtualPointerV1RequestFrame)
129+ if b := virtualEvents(t, c, events, 1)[0].Event.(ports.PointerButton); b.Button != 0x110 || !b.Pressed {
130+ t.Fatalf("press %+v", b)
131+ }
132+ _ = c.Close()
133+ b, ok := nextEvent[ports.VirtualPointerInput](t, events).Event.(ports.PointerButton)
134+ if !ok || b.Button != 0x110 || b.Pressed {
135+ t.Fatalf("release on disconnect %+v", b)
136+ }
137+}
138+
139+// Bad axis values are protocol errors.
140+func TestVirtualPointerInvalidAxis(t *testing.T) {
141+ s, _, _, dir := keyboardServer(t)
142+ c, p := virtualMover(t, s, dir, false)
143+ requestProtocol(t, c, p, wlrvirtualpointer.ZwlrVirtualPointerV1RequestAxis, uint32(1), uint32(7), fixed(1))
144+ expectProtocolError(t, c, p, uint32(wlrvirtualpointer.ZwlrVirtualPointerV1ErrorInvalidAxis))
145+}
146+
147+// While protected nothing is injected, the frame in progress is dropped and
148+// a press from before the lock is not released into the locked session.
149+func TestProtectedVirtualPointer(t *testing.T) {
150+ s, events, _, dir := keyboardServer(t)
151+ state := installSecurity(t, s)
152+ c, p := virtualMover(t, s, dir, false)
153+ requestProtocol(t, c, p, wlrvirtualpointer.ZwlrVirtualPointerV1RequestButton, uint32(1), uint32(0x110), uint32(1))
154+ requestProtocol(t, c, p, wlrvirtualpointer.ZwlrVirtualPointerV1RequestFrame)
155+ virtualEvents(t, c, events, 1)
156+ requestProtocol(t, c, p, wlrvirtualpointer.ZwlrVirtualPointerV1RequestMotion, uint32(2), fixed(5), fixed(5))
157+ roundtrip(t, c)
158+ protect(t, s, state, 1, true)
159+ requestProtocol(t, c, p, wlrvirtualpointer.ZwlrVirtualPointerV1RequestMotion, uint32(3), fixed(5), fixed(5))
160+ requestProtocol(t, c, p, wlrvirtualpointer.ZwlrVirtualPointerV1RequestButton, uint32(3), uint32(0x110), uint32(0))
161+ requestProtocol(t, c, p, wlrvirtualpointer.ZwlrVirtualPointerV1RequestFrame)
162+ virtualEvents(t, c, events, 0)
163+ protect(t, s, state, 2, false)
164+ // The old press died with the epoch: its release is not paired.
165+ requestProtocol(t, c, p, wlrvirtualpointer.ZwlrVirtualPointerV1RequestButton, uint32(4), uint32(0x110), uint32(0))
166+ requestProtocol(t, c, p, wlrvirtualpointer.ZwlrVirtualPointerV1RequestFrame)
167+ virtualEvents(t, c, events, 0)
168+ requestProtocol(t, c, p, wlrvirtualpointer.ZwlrVirtualPointerV1RequestMotion, uint32(5), fixed(1), fixed(0))
169+ requestProtocol(t, c, p, wlrvirtualpointer.ZwlrVirtualPointerV1RequestFrame)
170+ if got := virtualEvents(t, c, events, 1)[0]; got.State.Generation != 2 || got.State.Protected {
171+ t.Fatalf("post-unlock state %+v", got.State)
172+ }
173+}
174+
175+// Sandboxed clients do not see the manager global.
176+func TestVirtualPointerHiddenFromSandbox(t *testing.T) {
177+ h := newAdmissionHarness(t, 4)
178+ plain := protocolClient(t, h.s, h.dir)
179+ if _, ok := plain.Registry().FindGlobal(virtualPointerInterface); !ok {
180+ t.Fatal("plain client lacks the virtual pointer global")
181+ }
182+ l := newSandboxListener(t)
183+ cc := newContextClient(t, h)
184+ cc.commit(t, l, "org.flatpak", "org.example.App")
185+ sandboxed := l.connect(t)
186+ if _, ok := sandboxed.Registry().FindGlobal(virtualPointerInterface); ok {
187+ t.Fatal("sandboxed client sees the virtual pointer global")
188+ }
189+}
190+
191+const (
192+ vpMotion = wlrvirtualpointer.ZwlrVirtualPointerV1RequestMotion
193+ vpAbsolute = wlrvirtualpointer.ZwlrVirtualPointerV1RequestMotionAbsolute
194+ vpButton = wlrvirtualpointer.ZwlrVirtualPointerV1RequestButton
195+ vpFrame = wlrvirtualpointer.ZwlrVirtualPointerV1RequestFrame
196+ btnLeftCode = uint32(0x110)
197+)
198+
199+// An unframed release is discarded on disconnect: the forwarded press is
200+// still released exactly once.
201+func TestVirtualPointerUnframedReleaseThenDisconnect(t *testing.T) {
202+ s, events, _, dir := keyboardServer(t)
203+ c, p := virtualMover(t, s, dir, false)
204+ requestProtocol(t, c, p, vpButton, uint32(1), btnLeftCode, uint32(1))
205+ requestProtocol(t, c, p, vpFrame)
206+ virtualEvents(t, c, events, 1)
207+ requestProtocol(t, c, p, vpButton, uint32(2), btnLeftCode, uint32(0))
208+ virtualEvents(t, c, events, 0)
209+ _ = c.Close()
210+ b := nextEvent[ports.VirtualPointerInput](t, events).Event.(ports.PointerButton)
211+ if b.Button != btnLeftCode || b.Pressed {
212+ t.Fatalf("release %+v", b)
213+ }
214+ if !s.display.Do(func() {}) {
215+ t.Fatal("display stopped")
216+ }
217+ select {
218+ case ev := <-events:
219+ if v, ok := ev.(ports.VirtualPointerInput); ok {
220+ t.Fatalf("extra event %+v", v)
221+ }
222+ default:
223+ }
224+}
225+
226+// Every forwarded held button is released at destroy; a button pressed
227+// only in an unframed request is not.
228+func TestVirtualPointerReleasesAllHeld(t *testing.T) {
229+ s, events, _, dir := keyboardServer(t)
230+ c, p := virtualMover(t, s, dir, false)
231+ for _, b := range []uint32{0x110, 0x111, 0x112} {
232+ requestProtocol(t, c, p, vpButton, uint32(1), b, uint32(1))
233+ }
234+ requestProtocol(t, c, p, vpFrame)
235+ virtualEvents(t, c, events, 3)
236+ requestProtocol(t, c, p, vpButton, uint32(2), uint32(0x113), uint32(1))
237+ requestProtocol(t, c, p, wlrvirtualpointer.ZwlrVirtualPointerV1RequestDestroy)
238+ got := map[uint32]bool{}
239+ for _, v := range virtualEvents(t, c, events, 3) {
240+ b := v.Event.(ports.PointerButton)
241+ if b.Pressed {
242+ t.Fatalf("press on destroy %+v", b)
243+ }
244+ got[b.Button] = true
245+ }
246+ if !got[0x110] || !got[0x111] || !got[0x112] {
247+ t.Fatalf("released %v", got)
248+ }
249+}
250+
251+// Repeated presses in one frame forward once; a frame over the limit is a
252+// wl_display implementation error (object 1, code 3) and forwards nothing.
253+func TestVirtualPointerFrameLimit(t *testing.T) {
254+ s, events, _, dir := keyboardServer(t)
255+ c, p := virtualMover(t, s, dir, false)
256+ for i := 0; i < 10; i++ {
257+ requestProtocol(t, c, p, vpButton, uint32(i), btnLeftCode, uint32(1))
258+ }
259+ requestProtocol(t, c, p, vpFrame)
260+ if got := virtualEvents(t, c, events, 1); len(got) != 1 {
261+ t.Fatalf("repeated presses %v", got)
262+ }
263+ c2, p2 := virtualMover(t, s, dir, false)
264+ for i := 0; i <= maxVirtualFrame; i++ {
265+ requestProtocol(t, c2, p2, vpButton, uint32(i), btnLeftCode, uint32(i%2))
266+ }
267+ expectProtocolError(t, c2, 1, uint32(wayland.DisplayErrorImplementation))
268+ if !s.display.Do(func() {}) {
269+ t.Fatal("display stopped")
270+ }
271+ select {
272+ case ev := <-events:
273+ if v, ok := ev.(ports.VirtualPointerInput); ok {
274+ t.Fatalf("oversized frame forwarded %+v", v)
275+ }
276+ default:
277+ }
278+}
279+
280+// An oversized frame buffer is not kept after the frame.
281+func TestVirtualPointerResetDropsLargeBuffer(t *testing.T) {
282+ p := &virtualPointer{pending: make([]ports.VirtualPointerInput, 100)}
283+ p.reset()
284+ if p.pending != nil {
285+ t.Fatalf("kept cap %d", cap(p.pending))
286+ }
287+ p.pending = make([]ports.VirtualPointerInput, 3, virtualFrameKeep)
288+ p.reset()
289+ if len(p.pending) != 0 || cap(p.pending) != virtualFrameKeep {
290+ t.Fatalf("small buffer len %d cap %d", len(p.pending), cap(p.pending))
291+ }
292+}
293+
294+// x == extent and an extent of 1 normalize into [0, 1].
295+func TestVirtualPointerAbsoluteEdges(t *testing.T) {
296+ s, events, _, dir := keyboardServer(t)
297+ c, p := virtualMover(t, s, dir, false)
298+ for _, tc := range []struct {
299+ x, ext uint32
300+ want float64
301+ }{{1, 1, 1}, {0, 1, 0}, {1920, 1920, 1}, {5000, 1920, 1}} {
302+ requestProtocol(t, c, p, vpAbsolute, uint32(1), tc.x, tc.x, tc.ext, tc.ext)
303+ requestProtocol(t, c, p, vpFrame)
304+ if got := virtualEvents(t, c, events, 1)[0]; got.FX != tc.want || got.FY != tc.want {
305+ t.Fatalf("x %d extent %d: %v,%v want %v", tc.x, tc.ext, got.FX, got.FY, tc.want)
306+ }
307+ }
308+}
309+
310+// Mixed absolute and relative motion keeps its order and goes out only at
311+// frame, in both directions.
312+func TestVirtualPointerMixedMotionWaitsForFrame(t *testing.T) {
313+ for _, absFirst := range []bool{true, false} {
314+ t.Run(map[bool]string{true: "abs-rel", false: "rel-abs"}[absFirst], func(t *testing.T) {
315+ s, events, _, dir := keyboardServer(t)
316+ c, p := virtualMover(t, s, dir, false)
317+ abs := func() { requestProtocol(t, c, p, vpAbsolute, uint32(1), uint32(1), uint32(1), uint32(4), uint32(4)) }
318+ rel := func() { requestProtocol(t, c, p, vpMotion, uint32(1), fixed(3), fixed(4)) }
319+ if absFirst {
320+ abs()
321+ rel()
322+ rel()
323+ } else {
324+ rel()
325+ abs()
326+ }
327+ virtualEvents(t, c, events, 0)
328+ requestProtocol(t, c, p, vpFrame)
329+ got := virtualEvents(t, c, events, 2)
330+ if got[0].Absolute != absFirst || got[1].Absolute == absFirst {
331+ t.Fatalf("order %+v", got)
332+ }
333+ rv := got[1]
334+ if !absFirst {
335+ rv = got[0]
336+ }
337+ m := rv.Event.(ports.PointerMotion)
338+ want := 3.0
339+ if absFirst {
340+ want = 6
341+ }
342+ if m.DX != want {
343+ t.Fatalf("relative %+v want dx %v", m, want)
344+ }
345+ })
346+ }
347+}
348+
349+// Held buttons are bounded across valid frames: presses past the limit and
350+// codes outside the evdev button range are dropped, the pointer stays usable.
351+func TestVirtualPointerHeldLimit(t *testing.T) {
352+ s, events, _, dir := keyboardServer(t)
353+ c, p := virtualMover(t, s, dir, false)
354+ for i := uint32(0); i < maxVirtualHeld+4; i++ {
355+ requestProtocol(t, c, p, vpButton, i, 0x100+i, uint32(1))
356+ requestProtocol(t, c, p, vpFrame)
357+ }
358+ if got := virtualEvents(t, c, events, maxVirtualHeld); len(got) != maxVirtualHeld {
359+ t.Fatalf("forwarded %d presses", len(got))
360+ }
361+ for _, b := range []uint32{0, 0xff, 0x152, 0xffffffff} {
362+ requestProtocol(t, c, p, vpButton, uint32(1), b, uint32(1))
363+ }
364+ requestProtocol(t, c, p, vpFrame)
365+ virtualEvents(t, c, events, 0)
366+ // A release frees a slot for a new press.
367+ requestProtocol(t, c, p, vpButton, uint32(2), uint32(0x100), uint32(0))
368+ requestProtocol(t, c, p, vpButton, uint32(2), uint32(0x151), uint32(1))
369+ requestProtocol(t, c, p, vpFrame)
370+ got := virtualEvents(t, c, events, 2)
371+ if b := got[1].Event.(ports.PointerButton); b.Button != 0x151 || !b.Pressed {
372+ t.Fatalf("press after release %+v", b)
373+ }
374+}
@@ -0,0 +1,374 @@
1+package wayland
2+
3+import (
4+ "testing"
5+
6+ "bnema.dev/wlturbo"
7+ "github.com/bnema/go-wayland-bindings/server/wayland"
8+ "github.com/bnema/go-wayland-bindings/server/wlrvirtualpointer"
9+ "github.com/bnema/neferwl/internal/domain"
10+ "github.com/bnema/neferwl/internal/ports"
11+)
12+
13+// virtualMover binds a virtual pointer on its own connection, as wlrctl
14+// does; withOutput uses create_virtual_pointer_with_output on wl_output.
15+func virtualMover(t *testing.T, s *Server, dir string, withOutput bool) (*wlturbo.Display, uint32) {
16+ t.Helper()
17+ c := protocolClient(t, s, dir)
18+ seat := bindProtocol(t, c, "wl_seat")
19+ registerProtocol(t, c, seat)
20+ manager := bindVersion(t, c, virtualPointerInterface, 2)
21+ registerProtocol(t, c, manager)
22+ if withOutput {
23+ out := bindVersion(t, c, "wl_output", 4)
24+ registerProtocol(t, c, out)
25+ p := c.AllocateID()
26+ registerProtocol(t, c, p)
27+ requestProtocol(t, c, manager, wlrvirtualpointer.ZwlrVirtualPointerManagerV1RequestCreateVirtualPointerWithOutput, seat, out, p)
28+ return c, p
29+ }
30+ p := c.AllocateID()
31+ registerProtocol(t, c, p)
32+ requestProtocol(t, c, manager, wlrvirtualpointer.ZwlrVirtualPointerManagerV1RequestCreateVirtualPointer, seat, p)
33+ return c, p
34+}
35+
36+func fixed(v float64) wlturbo.Fixed { return wlturbo.Fixed(int32(v * 256)) }
37+
38+// virtualEvents waits for n VirtualPointerInput events after a roundtrip
39+// and fails on any further one already forwarded.
40+func virtualEvents(t *testing.T, c *wlturbo.Display, events chan ports.ClientEvent, n int) []ports.VirtualPointerInput {
41+ t.Helper()
42+ roundtrip(t, c)
43+ var out []ports.VirtualPointerInput
44+ for len(out) < n {
45+ out = append(out, nextEvent[ports.VirtualPointerInput](t, events))
46+ }
47+ for {
48+ select {
49+ case ev := <-events:
50+ if v, ok := ev.(ports.VirtualPointerInput); ok {
51+ t.Fatalf("unexpected virtual event %+v", v)
52+ }
53+ default:
54+ return out
55+ }
56+ }
57+}
58+
59+// Requests are held until frame and reach core in one batch: motion, then
60+// buttons, then scroll, under the virtual device identity.
61+func TestVirtualPointerFrameBatch(t *testing.T) {
62+ s, events, _, dir := keyboardServer(t)
63+ c, p := virtualMover(t, s, dir, false)
64+ requestProtocol(t, c, p, wlrvirtualpointer.ZwlrVirtualPointerV1RequestMotion, uint32(5), fixed(10), fixed(-4))
65+ requestProtocol(t, c, p, wlrvirtualpointer.ZwlrVirtualPointerV1RequestMotion, uint32(6), fixed(2.5), fixed(1))
66+ requestProtocol(t, c, p, wlrvirtualpointer.ZwlrVirtualPointerV1RequestButton, uint32(7), uint32(0x110), uint32(1))
67+ requestProtocol(t, c, p, wlrvirtualpointer.ZwlrVirtualPointerV1RequestAxisSource, uint32(0))
68+ requestProtocol(t, c, p, wlrvirtualpointer.ZwlrVirtualPointerV1RequestAxisDiscrete, uint32(8), uint32(0), fixed(15), int32(1))
69+ roundtrip(t, c)
70+ select {
71+ case ev := <-events:
72+ if _, ok := ev.(ports.VirtualPointerInput); ok {
73+ t.Fatal("virtual event before frame")
74+ }
75+ default:
76+ }
77+ requestProtocol(t, c, p, wlrvirtualpointer.ZwlrVirtualPointerV1RequestFrame)
78+ got := virtualEvents(t, c, events, 3)
79+ m, ok := got[0].Event.(ports.PointerMotion)
80+ if !ok || got[0].Absolute || m.DX != 12.5 || m.DY != -3 || m.Device < virtualPointerDevices {
81+ t.Fatalf("motion %+v", got[0])
82+ }
83+ b, ok := got[1].Event.(ports.PointerButton)
84+ if !ok || b.Button != 0x110 || !b.Pressed || b.Device != m.Device {
85+ t.Fatalf("button %+v", got[1])
86+ }
87+ a, ok := got[2].Event.(ports.PointerAxis)
88+ if !ok || a.Source != domain.AxisWheel || !a.Vertical.Set || a.Vertical.Value != 15 || a.Vertical.V120 != 120 || a.Horizontal.Set {
89+ t.Fatalf("axis %+v", got[2])
90+ }
91+}
92+
93+// Absolute motion becomes a fraction of the layout, or of the bound output.
94+func TestVirtualPointerAbsolute(t *testing.T) {
95+ for _, withOutput := range []bool{false, true} {
96+ t.Run(map[bool]string{false: "layout", true: "with-output"}[withOutput], func(t *testing.T) {
97+ s, events, _, dir := keyboardServer(t)
98+ c, p := virtualMover(t, s, dir, withOutput)
99+ requestProtocol(t, c, p, wlrvirtualpointer.ZwlrVirtualPointerV1RequestMotionAbsolute, uint32(1), uint32(480), uint32(270), uint32(1920), uint32(1080))
100+ requestProtocol(t, c, p, wlrvirtualpointer.ZwlrVirtualPointerV1RequestFrame)
101+ got := virtualEvents(t, c, events, 1)[0]
102+ want := ""
103+ if withOutput {
104+ want = "HEADLESS-1"
105+ }
106+ if !got.Absolute || got.FX != 0.25 || got.FY != 0.25 || got.Output != want {
107+ t.Fatalf("absolute %+v", got)
108+ }
109+ requestProtocol(t, c, p, wlrvirtualpointer.ZwlrVirtualPointerV1RequestMotion, uint32(2), fixed(5), fixed(3))
110+ requestProtocol(t, c, p, wlrvirtualpointer.ZwlrVirtualPointerV1RequestButton, uint32(2), uint32(0x110), uint32(1))
111+ requestProtocol(t, c, p, wlrvirtualpointer.ZwlrVirtualPointerV1RequestFrame)
112+ for _, event := range virtualEvents(t, c, events, 2) {
113+ if event.Output != want {
114+ t.Fatalf("lost output association: %+v", event)
115+ }
116+ }
117+ })
118+ }
119+}
120+
121+// Losing the pointer mid-click releases its buttons; an unpaired release
122+// never goes out.
123+func TestVirtualPointerDisconnectReleases(t *testing.T) {
124+ s, events, _, dir := keyboardServer(t)
125+ c, p := virtualMover(t, s, dir, false)
126+ requestProtocol(t, c, p, wlrvirtualpointer.ZwlrVirtualPointerV1RequestButton, uint32(1), uint32(0x111), uint32(0))
127+ requestProtocol(t, c, p, wlrvirtualpointer.ZwlrVirtualPointerV1RequestButton, uint32(1), uint32(0x110), uint32(1))
128+ requestProtocol(t, c, p, wlrvirtualpointer.ZwlrVirtualPointerV1RequestFrame)
129+ if b := virtualEvents(t, c, events, 1)[0].Event.(ports.PointerButton); b.Button != 0x110 || !b.Pressed {
130+ t.Fatalf("press %+v", b)
131+ }
132+ _ = c.Close()
133+ b, ok := nextEvent[ports.VirtualPointerInput](t, events).Event.(ports.PointerButton)
134+ if !ok || b.Button != 0x110 || b.Pressed {
135+ t.Fatalf("release on disconnect %+v", b)
136+ }
137+}
138+
139+// Bad axis values are protocol errors.
140+func TestVirtualPointerInvalidAxis(t *testing.T) {
141+ s, _, _, dir := keyboardServer(t)
142+ c, p := virtualMover(t, s, dir, false)
143+ requestProtocol(t, c, p, wlrvirtualpointer.ZwlrVirtualPointerV1RequestAxis, uint32(1), uint32(7), fixed(1))
144+ expectProtocolError(t, c, p, uint32(wlrvirtualpointer.ZwlrVirtualPointerV1ErrorInvalidAxis))
145+}
146+
147+// While protected nothing is injected, the frame in progress is dropped and
148+// a press from before the lock is not released into the locked session.
149+func TestProtectedVirtualPointer(t *testing.T) {
150+ s, events, _, dir := keyboardServer(t)
151+ state := installSecurity(t, s)
152+ c, p := virtualMover(t, s, dir, false)
153+ requestProtocol(t, c, p, wlrvirtualpointer.ZwlrVirtualPointerV1RequestButton, uint32(1), uint32(0x110), uint32(1))
154+ requestProtocol(t, c, p, wlrvirtualpointer.ZwlrVirtualPointerV1RequestFrame)
155+ virtualEvents(t, c, events, 1)
156+ requestProtocol(t, c, p, wlrvirtualpointer.ZwlrVirtualPointerV1RequestMotion, uint32(2), fixed(5), fixed(5))
157+ roundtrip(t, c)
158+ protect(t, s, state, 1, true)
159+ requestProtocol(t, c, p, wlrvirtualpointer.ZwlrVirtualPointerV1RequestMotion, uint32(3), fixed(5), fixed(5))
160+ requestProtocol(t, c, p, wlrvirtualpointer.ZwlrVirtualPointerV1RequestButton, uint32(3), uint32(0x110), uint32(0))
161+ requestProtocol(t, c, p, wlrvirtualpointer.ZwlrVirtualPointerV1RequestFrame)
162+ virtualEvents(t, c, events, 0)
163+ protect(t, s, state, 2, false)
164+ // The old press died with the epoch: its release is not paired.
165+ requestProtocol(t, c, p, wlrvirtualpointer.ZwlrVirtualPointerV1RequestButton, uint32(4), uint32(0x110), uint32(0))
166+ requestProtocol(t, c, p, wlrvirtualpointer.ZwlrVirtualPointerV1RequestFrame)
167+ virtualEvents(t, c, events, 0)
168+ requestProtocol(t, c, p, wlrvirtualpointer.ZwlrVirtualPointerV1RequestMotion, uint32(5), fixed(1), fixed(0))
169+ requestProtocol(t, c, p, wlrvirtualpointer.ZwlrVirtualPointerV1RequestFrame)
170+ if got := virtualEvents(t, c, events, 1)[0]; got.State.Generation != 2 || got.State.Protected {
171+ t.Fatalf("post-unlock state %+v", got.State)
172+ }
173+}
174+
175+// Sandboxed clients do not see the manager global.
176+func TestVirtualPointerHiddenFromSandbox(t *testing.T) {
177+ h := newAdmissionHarness(t, 4)
178+ plain := protocolClient(t, h.s, h.dir)
179+ if _, ok := plain.Registry().FindGlobal(virtualPointerInterface); !ok {
180+ t.Fatal("plain client lacks the virtual pointer global")
181+ }
182+ l := newSandboxListener(t)
183+ cc := newContextClient(t, h)
184+ cc.commit(t, l, "org.flatpak", "org.example.App")
185+ sandboxed := l.connect(t)
186+ if _, ok := sandboxed.Registry().FindGlobal(virtualPointerInterface); ok {
187+ t.Fatal("sandboxed client sees the virtual pointer global")
188+ }
189+}
190+
191+const (
192+ vpMotion = wlrvirtualpointer.ZwlrVirtualPointerV1RequestMotion
193+ vpAbsolute = wlrvirtualpointer.ZwlrVirtualPointerV1RequestMotionAbsolute
194+ vpButton = wlrvirtualpointer.ZwlrVirtualPointerV1RequestButton
195+ vpFrame = wlrvirtualpointer.ZwlrVirtualPointerV1RequestFrame
196+ btnLeftCode = uint32(0x110)
197+)
198+
199+// An unframed release is discarded on disconnect: the forwarded press is
200+// still released exactly once.
201+func TestVirtualPointerUnframedReleaseThenDisconnect(t *testing.T) {
202+ s, events, _, dir := keyboardServer(t)
203+ c, p := virtualMover(t, s, dir, false)
204+ requestProtocol(t, c, p, vpButton, uint32(1), btnLeftCode, uint32(1))
205+ requestProtocol(t, c, p, vpFrame)
206+ virtualEvents(t, c, events, 1)
207+ requestProtocol(t, c, p, vpButton, uint32(2), btnLeftCode, uint32(0))
208+ virtualEvents(t, c, events, 0)
209+ _ = c.Close()
210+ b := nextEvent[ports.VirtualPointerInput](t, events).Event.(ports.PointerButton)
211+ if b.Button != btnLeftCode || b.Pressed {
212+ t.Fatalf("release %+v", b)
213+ }
214+ if !s.display.Do(func() {}) {
215+ t.Fatal("display stopped")
216+ }
217+ select {
218+ case ev := <-events:
219+ if v, ok := ev.(ports.VirtualPointerInput); ok {
220+ t.Fatalf("extra event %+v", v)
221+ }
222+ default:
223+ }
224+}
225+
226+// Every forwarded held button is released at destroy; a button pressed
227+// only in an unframed request is not.
228+func TestVirtualPointerReleasesAllHeld(t *testing.T) {
229+ s, events, _, dir := keyboardServer(t)
230+ c, p := virtualMover(t, s, dir, false)
231+ for _, b := range []uint32{0x110, 0x111, 0x112} {
232+ requestProtocol(t, c, p, vpButton, uint32(1), b, uint32(1))
233+ }
234+ requestProtocol(t, c, p, vpFrame)
235+ virtualEvents(t, c, events, 3)
236+ requestProtocol(t, c, p, vpButton, uint32(2), uint32(0x113), uint32(1))
237+ requestProtocol(t, c, p, wlrvirtualpointer.ZwlrVirtualPointerV1RequestDestroy)
238+ got := map[uint32]bool{}
239+ for _, v := range virtualEvents(t, c, events, 3) {
240+ b := v.Event.(ports.PointerButton)
241+ if b.Pressed {
242+ t.Fatalf("press on destroy %+v", b)
243+ }
244+ got[b.Button] = true
245+ }
246+ if !got[0x110] || !got[0x111] || !got[0x112] {
247+ t.Fatalf("released %v", got)
248+ }
249+}
250+
251+// Repeated presses in one frame forward once; a frame over the limit is a
252+// wl_display implementation error (object 1, code 3) and forwards nothing.
253+func TestVirtualPointerFrameLimit(t *testing.T) {
254+ s, events, _, dir := keyboardServer(t)
255+ c, p := virtualMover(t, s, dir, false)
256+ for i := 0; i < 10; i++ {
257+ requestProtocol(t, c, p, vpButton, uint32(i), btnLeftCode, uint32(1))
258+ }
259+ requestProtocol(t, c, p, vpFrame)
260+ if got := virtualEvents(t, c, events, 1); len(got) != 1 {
261+ t.Fatalf("repeated presses %v", got)
262+ }
263+ c2, p2 := virtualMover(t, s, dir, false)
264+ for i := 0; i <= maxVirtualFrame; i++ {
265+ requestProtocol(t, c2, p2, vpButton, uint32(i), btnLeftCode, uint32(i%2))
266+ }
267+ expectProtocolError(t, c2, 1, uint32(wayland.DisplayErrorImplementation))
268+ if !s.display.Do(func() {}) {
269+ t.Fatal("display stopped")
270+ }
271+ select {
272+ case ev := <-events:
273+ if v, ok := ev.(ports.VirtualPointerInput); ok {
274+ t.Fatalf("oversized frame forwarded %+v", v)
275+ }
276+ default:
277+ }
278+}
279+
280+// An oversized frame buffer is not kept after the frame.
281+func TestVirtualPointerResetDropsLargeBuffer(t *testing.T) {
282+ p := &virtualPointer{pending: make([]ports.VirtualPointerInput, 100)}
283+ p.reset()
284+ if p.pending != nil {
285+ t.Fatalf("kept cap %d", cap(p.pending))
286+ }
287+ p.pending = make([]ports.VirtualPointerInput, 3, virtualFrameKeep)
288+ p.reset()
289+ if len(p.pending) != 0 || cap(p.pending) != virtualFrameKeep {
290+ t.Fatalf("small buffer len %d cap %d", len(p.pending), cap(p.pending))
291+ }
292+}
293+
294+// x == extent and an extent of 1 normalize into [0, 1].
295+func TestVirtualPointerAbsoluteEdges(t *testing.T) {
296+ s, events, _, dir := keyboardServer(t)
297+ c, p := virtualMover(t, s, dir, false)
298+ for _, tc := range []struct {
299+ x, ext uint32
300+ want float64
301+ }{{1, 1, 1}, {0, 1, 0}, {1920, 1920, 1}, {5000, 1920, 1}} {
302+ requestProtocol(t, c, p, vpAbsolute, uint32(1), tc.x, tc.x, tc.ext, tc.ext)
303+ requestProtocol(t, c, p, vpFrame)
304+ if got := virtualEvents(t, c, events, 1)[0]; got.FX != tc.want || got.FY != tc.want {
305+ t.Fatalf("x %d extent %d: %v,%v want %v", tc.x, tc.ext, got.FX, got.FY, tc.want)
306+ }
307+ }
308+}
309+
310+// Mixed absolute and relative motion keeps its order and goes out only at
311+// frame, in both directions.
312+func TestVirtualPointerMixedMotionWaitsForFrame(t *testing.T) {
313+ for _, absFirst := range []bool{true, false} {
314+ t.Run(map[bool]string{true: "abs-rel", false: "rel-abs"}[absFirst], func(t *testing.T) {
315+ s, events, _, dir := keyboardServer(t)
316+ c, p := virtualMover(t, s, dir, false)
317+ abs := func() { requestProtocol(t, c, p, vpAbsolute, uint32(1), uint32(1), uint32(1), uint32(4), uint32(4)) }
318+ rel := func() { requestProtocol(t, c, p, vpMotion, uint32(1), fixed(3), fixed(4)) }
319+ if absFirst {
320+ abs()
321+ rel()
322+ rel()
323+ } else {
324+ rel()
325+ abs()
326+ }
327+ virtualEvents(t, c, events, 0)
328+ requestProtocol(t, c, p, vpFrame)
329+ got := virtualEvents(t, c, events, 2)
330+ if got[0].Absolute != absFirst || got[1].Absolute == absFirst {
331+ t.Fatalf("order %+v", got)
332+ }
333+ rv := got[1]
334+ if !absFirst {
335+ rv = got[0]
336+ }
337+ m := rv.Event.(ports.PointerMotion)
338+ want := 3.0
339+ if absFirst {
340+ want = 6
341+ }
342+ if m.DX != want {
343+ t.Fatalf("relative %+v want dx %v", m, want)
344+ }
345+ })
346+ }
347+}
348+
349+// Held buttons are bounded across valid frames: presses past the limit and
350+// codes outside the evdev button range are dropped, the pointer stays usable.
351+func TestVirtualPointerHeldLimit(t *testing.T) {
352+ s, events, _, dir := keyboardServer(t)
353+ c, p := virtualMover(t, s, dir, false)
354+ for i := uint32(0); i < maxVirtualHeld+4; i++ {
355+ requestProtocol(t, c, p, vpButton, i, 0x100+i, uint32(1))
356+ requestProtocol(t, c, p, vpFrame)
357+ }
358+ if got := virtualEvents(t, c, events, maxVirtualHeld); len(got) != maxVirtualHeld {
359+ t.Fatalf("forwarded %d presses", len(got))
360+ }
361+ for _, b := range []uint32{0, 0xff, 0x152, 0xffffffff} {
362+ requestProtocol(t, c, p, vpButton, uint32(1), b, uint32(1))
363+ }
364+ requestProtocol(t, c, p, vpFrame)
365+ virtualEvents(t, c, events, 0)
366+ // A release frees a slot for a new press.
367+ requestProtocol(t, c, p, vpButton, uint32(2), uint32(0x100), uint32(0))
368+ requestProtocol(t, c, p, vpButton, uint32(2), uint32(0x151), uint32(1))
369+ requestProtocol(t, c, p, vpFrame)
370+ got := virtualEvents(t, c, events, 2)
371+ if b := got[1].Event.(ports.PointerButton); b.Button != 0x151 || !b.Pressed {
372+ t.Fatalf("press after release %+v", b)
373+ }
374+}
M internal/adapters/wayland/wire_client_test.go +4 -0
@@ -49,6 +49,7 @@ import (
4949 client_outputmanagement "github.com/bnema/go-wayland-bindings/client/wlroutputmanagement"
5050 client_outputpower "github.com/bnema/go-wayland-bindings/client/wlroutputpowermanagement"
5151 client_screencopy "github.com/bnema/go-wayland-bindings/client/wlrscreencopy"
52+ client_wlrvirtualpointer "github.com/bnema/go-wayland-bindings/client/wlrvirtualpointer"
5253 client_xdgactivation "github.com/bnema/go-wayland-bindings/client/xdgactivation"
5354 client_xdgoutput "github.com/bnema/go-wayland-bindings/client/xdgoutput"
5455 client_xdgshell "github.com/bnema/go-wayland-bindings/client/xdgshell"
@@ -93,6 +94,7 @@ import (
9394 wire_wlroutputmanagement "github.com/bnema/go-wayland-bindings/server/wlroutputmanagement"
9495 wire_wlroutputpower "github.com/bnema/go-wayland-bindings/server/wlroutputpowermanagement"
9596 wire_wlrscreencopy "github.com/bnema/go-wayland-bindings/server/wlrscreencopy"
97+ wire_wlrvirtualpointer "github.com/bnema/go-wayland-bindings/server/wlrvirtualpointer"
9698 wire_xdgactivation "github.com/bnema/go-wayland-bindings/server/xdgactivation"
9799 wire_xdgdialog "github.com/bnema/go-wayland-bindings/server/xdgdialog"
98100 wire_xdgforeign "github.com/bnema/go-wayland-bindings/server/xdgforeign"
@@ -259,6 +261,8 @@ var wireSchemas = map[string]wireSchema{
259261 "zwp_text_input_v3": {wire_textinput.ZwpTextInputV3Interface, &client_textinput.TextInputV3{}, map[uint16]bool{0: true}, nil},
260262 "zwp_virtual_keyboard_manager_v1": {wire_virtualkeyboard.ZwpVirtualKeyboardManagerV1Interface, &client_virtualkeyboard.VirtualKeyboardManager{}, nil, nil},
261263 "zwp_virtual_keyboard_v1": {wire_virtualkeyboard.ZwpVirtualKeyboardV1Interface, &client_virtualkeyboard.VirtualKeyboard{}, map[uint16]bool{3: true}, nil},
264+ "zwlr_virtual_pointer_manager_v1": {wire_wlrvirtualpointer.ZwlrVirtualPointerManagerV1Interface, &client_wlrvirtualpointer.VirtualPointerManager{}, map[uint16]bool{1: true}, nil},
265+ "zwlr_virtual_pointer_v1": {wire_wlrvirtualpointer.ZwlrVirtualPointerV1Interface, &client_wlrvirtualpointer.VirtualPointer{}, map[uint16]bool{8: true}, nil},
262266 "zwp_xwayland_keyboard_grab_manager_v1": {wire_xwaylandkeyboardgrab.ZwpXwaylandKeyboardGrabManagerV1Interface, nil, map[uint16]bool{0: true}, nil},
263267 "zwp_xwayland_keyboard_grab_v1": {wire_xwaylandkeyboardgrab.ZwpXwaylandKeyboardGrabV1Interface, nil, map[uint16]bool{0: true}, nil},
264268 "xwayland_shell_v1": {wire_xwaylandshell.XwaylandShellV1Interface, nil, map[uint16]bool{0: true}, nil},
@@ -49,6 +49,7 @@ import (
49 client_outputmanagement "github.com/bnema/go-wayland-bindings/client/wlroutputmanagement"49 client_outputmanagement "github.com/bnema/go-wayland-bindings/client/wlroutputmanagement"
50 client_outputpower "github.com/bnema/go-wayland-bindings/client/wlroutputpowermanagement"50 client_outputpower "github.com/bnema/go-wayland-bindings/client/wlroutputpowermanagement"
51 client_screencopy "github.com/bnema/go-wayland-bindings/client/wlrscreencopy"51 client_screencopy "github.com/bnema/go-wayland-bindings/client/wlrscreencopy"
52+ client_wlrvirtualpointer "github.com/bnema/go-wayland-bindings/client/wlrvirtualpointer"
52 client_xdgactivation "github.com/bnema/go-wayland-bindings/client/xdgactivation"53 client_xdgactivation "github.com/bnema/go-wayland-bindings/client/xdgactivation"
53 client_xdgoutput "github.com/bnema/go-wayland-bindings/client/xdgoutput"54 client_xdgoutput "github.com/bnema/go-wayland-bindings/client/xdgoutput"
54 client_xdgshell "github.com/bnema/go-wayland-bindings/client/xdgshell"55 client_xdgshell "github.com/bnema/go-wayland-bindings/client/xdgshell"
@@ -93,6 +94,7 @@ import (
93 wire_wlroutputmanagement "github.com/bnema/go-wayland-bindings/server/wlroutputmanagement"94 wire_wlroutputmanagement "github.com/bnema/go-wayland-bindings/server/wlroutputmanagement"
94 wire_wlroutputpower "github.com/bnema/go-wayland-bindings/server/wlroutputpowermanagement"95 wire_wlroutputpower "github.com/bnema/go-wayland-bindings/server/wlroutputpowermanagement"
95 wire_wlrscreencopy "github.com/bnema/go-wayland-bindings/server/wlrscreencopy"96 wire_wlrscreencopy "github.com/bnema/go-wayland-bindings/server/wlrscreencopy"
97+ wire_wlrvirtualpointer "github.com/bnema/go-wayland-bindings/server/wlrvirtualpointer"
96 wire_xdgactivation "github.com/bnema/go-wayland-bindings/server/xdgactivation"98 wire_xdgactivation "github.com/bnema/go-wayland-bindings/server/xdgactivation"
97 wire_xdgdialog "github.com/bnema/go-wayland-bindings/server/xdgdialog"99 wire_xdgdialog "github.com/bnema/go-wayland-bindings/server/xdgdialog"
98 wire_xdgforeign "github.com/bnema/go-wayland-bindings/server/xdgforeign"100 wire_xdgforeign "github.com/bnema/go-wayland-bindings/server/xdgforeign"
@@ -259,6 +261,8 @@ var wireSchemas = map[string]wireSchema{
259 "zwp_text_input_v3": {wire_textinput.ZwpTextInputV3Interface, &client_textinput.TextInputV3{}, map[uint16]bool{0: true}, nil},261 "zwp_text_input_v3": {wire_textinput.ZwpTextInputV3Interface, &client_textinput.TextInputV3{}, map[uint16]bool{0: true}, nil},
260 "zwp_virtual_keyboard_manager_v1": {wire_virtualkeyboard.ZwpVirtualKeyboardManagerV1Interface, &client_virtualkeyboard.VirtualKeyboardManager{}, nil, nil},262 "zwp_virtual_keyboard_manager_v1": {wire_virtualkeyboard.ZwpVirtualKeyboardManagerV1Interface, &client_virtualkeyboard.VirtualKeyboardManager{}, nil, nil},
261 "zwp_virtual_keyboard_v1": {wire_virtualkeyboard.ZwpVirtualKeyboardV1Interface, &client_virtualkeyboard.VirtualKeyboard{}, map[uint16]bool{3: true}, nil},263 "zwp_virtual_keyboard_v1": {wire_virtualkeyboard.ZwpVirtualKeyboardV1Interface, &client_virtualkeyboard.VirtualKeyboard{}, map[uint16]bool{3: true}, nil},
264+ "zwlr_virtual_pointer_manager_v1": {wire_wlrvirtualpointer.ZwlrVirtualPointerManagerV1Interface, &client_wlrvirtualpointer.VirtualPointerManager{}, map[uint16]bool{1: true}, nil},
265+ "zwlr_virtual_pointer_v1": {wire_wlrvirtualpointer.ZwlrVirtualPointerV1Interface, &client_wlrvirtualpointer.VirtualPointer{}, map[uint16]bool{8: true}, nil},
262 "zwp_xwayland_keyboard_grab_manager_v1": {wire_xwaylandkeyboardgrab.ZwpXwaylandKeyboardGrabManagerV1Interface, nil, map[uint16]bool{0: true}, nil},266 "zwp_xwayland_keyboard_grab_manager_v1": {wire_xwaylandkeyboardgrab.ZwpXwaylandKeyboardGrabManagerV1Interface, nil, map[uint16]bool{0: true}, nil},
263 "zwp_xwayland_keyboard_grab_v1": {wire_xwaylandkeyboardgrab.ZwpXwaylandKeyboardGrabV1Interface, nil, map[uint16]bool{0: true}, nil},267 "zwp_xwayland_keyboard_grab_v1": {wire_xwaylandkeyboardgrab.ZwpXwaylandKeyboardGrabV1Interface, nil, map[uint16]bool{0: true}, nil},
264 "xwayland_shell_v1": {wire_xwaylandshell.XwaylandShellV1Interface, nil, map[uint16]bool{0: true}, nil},268 "xwayland_shell_v1": {wire_xwaylandshell.XwaylandShellV1Interface, nil, map[uint16]bool{0: true}, nil},
M internal/adapters/wayland/x11window.go +4 -0
@@ -60,6 +60,10 @@ func (s *Server) globalVisible(c server.Client, g *server.Global) bool {
6060 if g == s.xdgPublic {
6161 return c != s.xwayland.client
6262 }
63+ if g.Interface() == virtualPointerInterface {
64+ // Sandboxed clients never drive the seat pointer.
65+ return s.sandboxed[c] == nil
66+ }
6367 if xwaylandRestricted(g.Interface()) {
6468 return s.xwayland.client != (server.Client{}) && c == s.xwayland.client
6569 }
@@ -60,6 +60,10 @@ func (s *Server) globalVisible(c server.Client, g *server.Global) bool {
60 if g == s.xdgPublic {60 if g == s.xdgPublic {
61 return c != s.xwayland.client61 return c != s.xwayland.client
62 }62 }
63+ if g.Interface() == virtualPointerInterface {
64+ // Sandboxed clients never drive the seat pointer.
65+ return s.sandboxed[c] == nil
66+ }
63 if xwaylandRestricted(g.Interface()) {67 if xwaylandRestricted(g.Interface()) {
64 return s.xwayland.client != (server.Client{}) && c == s.xwayland.client68 return s.xwayland.client != (server.Client{}) && c == s.xwayland.client
65 }69 }
A internal/app/pointer_target_test.go +115 -0
@@ -0,0 +1,115 @@
1+package app
2+
3+import (
4+ "encoding/binary"
5+ "testing"
6+ "time"
7+
8+ "bnema.dev/wlturbo"
9+ clientcore "github.com/bnema/go-wayland-bindings/client/wayland"
10+ clientxdg "github.com/bnema/go-wayland-bindings/client/xdgshell"
11+ "golang.org/x/sys/unix"
12+)
13+
14+// pointerTarget is a mapped toplevel showing an opaque red square, with what
15+// its pointer and keyboard received.
16+type pointerTarget struct {
17+ entered, focused bool
18+ pressed, release bool
19+ keys []uint32
20+ buttons []uint32 // button codes pressed, in order
21+ motions int // motions while the left button is down
22+ x, y float64 // latest surface-local position
23+ scrollV, scrollH int32 // wheel detents (axis_discrete), summed
24+}
25+
26+// mapPointerTarget maps a red toplevel (64x64 buffer; tiled, it fills its tile) on c with a pointer and keyboard.
27+func mapPointerTarget(t *testing.T, c *wlturbo.Display) *pointerTarget {
28+ t.Helper()
29+ check := func(err error) {
30+ t.Helper()
31+ if err != nil {
32+ t.Fatal(err)
33+ }
34+ }
35+ check(c.Roundtrip())
36+ comp := clientcore.NewCompositor(c.Context())
37+ shm := clientcore.NewShm(c.Context())
38+ seat := clientcore.NewSeat(c.Context())
39+ wm := clientxdg.NewXdgWmBase(c.Context())
40+ wm.OnPing(func(serial uint32) { check(wm.Pong(serial)) })
41+ bindTestClient(t, c, clientcore.CompositorInterface, 1, comp)
42+ bindTestClient(t, c, clientcore.ShmInterface, 1, shm)
43+ bindTestClient(t, c, clientcore.SeatInterface, 5, seat)
44+ bindTestClient(t, c, clientxdg.XdgWmBaseInterface, 1, wm)
45+ v := &pointerTarget{}
46+ ptr, err := seat.GetPointer()
47+ check(err)
48+ ptr.OnEnter(func(_, _ uint32, x, y wlturbo.Fixed) { v.entered, v.x, v.y = true, x.Float64(), y.Float64() })
49+ ptr.OnMotion(func(_ uint32, x, y wlturbo.Fixed) {
50+ v.x, v.y = x.Float64(), y.Float64()
51+ if v.pressed && !v.release {
52+ v.motions++
53+ }
54+ })
55+ ptr.OnAxisDiscrete(func(axis uint32, n int32) {
56+ if axis == 0 {
57+ v.scrollV += n
58+ } else {
59+ v.scrollH += n
60+ }
61+ })
62+ ptr.OnButton(func(_, _, button, state uint32) {
63+ if state == 1 {
64+ v.buttons = append(v.buttons, button)
65+ }
66+ if button == 0x110 && state == 1 {
67+ v.pressed = true
68+ }
69+ if button == 0x110 && state == 0 && v.pressed {
70+ v.release = true
71+ }
72+ })
73+ kb, err := seat.GetKeyboard()
74+ check(err)
75+ kb.OnKeymap(func(_ uint32, fd *wlturbo.OwnedFD, _ uint32) { _ = fd.Close() })
76+ kb.OnEnter(func(uint32, uint32, []byte) { v.focused = true })
77+ kb.OnKey(func(_, _, key, state uint32) {
78+ if state == 1 {
79+ v.keys = append(v.keys, key)
80+ }
81+ })
82+ const side = 64
83+ fd, err := unix.MemfdCreate("pointer-target", 0)
84+ check(err)
85+ px := make([]byte, side*side*4)
86+ for i := 0; i < len(px); i += 4 {
87+ binary.LittleEndian.PutUint32(px[i:], 0xffff0000) // ARGB red
88+ }
89+ if _, err := unix.Write(fd, px); err != nil {
90+ _ = unix.Close(fd)
91+ t.Fatal(err)
92+ }
93+ pool, err := shm.CreatePool(fd, int32(len(px)))
94+ if err != nil {
95+ _ = unix.Close(fd)
96+ t.Fatal(err)
97+ }
98+ buffer, err := pool.CreateBuffer(0, side, side, side*4, clientcore.FORMAT_XRGB8888)
99+ check(err)
100+ surf, err := comp.CreateSurface()
101+ check(err)
102+ xdg, err := wm.GetXdgSurface(surf)
103+ check(err)
104+ var serial uint32
105+ xdg.OnConfigure(func(value uint32) { serial = value })
106+ _, err = xdg.GetToplevel()
107+ check(err)
108+ check(surf.Commit())
109+ dispatchUntil(t, c, 5*time.Second, func() string { return "no configure" }, func() bool { return serial != 0 })
110+ check(xdg.AckConfigure(serial))
111+ check(surf.Attach(buffer, 0, 0))
112+ check(surf.Damage(0, 0, side, side))
113+ check(surf.Commit())
114+ return v
115+}
@@ -0,0 +1,115 @@
1+package app
2+
3+import (
4+ "encoding/binary"
5+ "testing"
6+ "time"
7+
8+ "bnema.dev/wlturbo"
9+ clientcore "github.com/bnema/go-wayland-bindings/client/wayland"
10+ clientxdg "github.com/bnema/go-wayland-bindings/client/xdgshell"
11+ "golang.org/x/sys/unix"
12+)
13+
14+// pointerTarget is a mapped toplevel showing an opaque red square, with what
15+// its pointer and keyboard received.
16+type pointerTarget struct {
17+ entered, focused bool
18+ pressed, release bool
19+ keys []uint32
20+ buttons []uint32 // button codes pressed, in order
21+ motions int // motions while the left button is down
22+ x, y float64 // latest surface-local position
23+ scrollV, scrollH int32 // wheel detents (axis_discrete), summed
24+}
25+
26+// mapPointerTarget maps a red toplevel (64x64 buffer; tiled, it fills its tile) on c with a pointer and keyboard.
27+func mapPointerTarget(t *testing.T, c *wlturbo.Display) *pointerTarget {
28+ t.Helper()
29+ check := func(err error) {
30+ t.Helper()
31+ if err != nil {
32+ t.Fatal(err)
33+ }
34+ }
35+ check(c.Roundtrip())
36+ comp := clientcore.NewCompositor(c.Context())
37+ shm := clientcore.NewShm(c.Context())
38+ seat := clientcore.NewSeat(c.Context())
39+ wm := clientxdg.NewXdgWmBase(c.Context())
40+ wm.OnPing(func(serial uint32) { check(wm.Pong(serial)) })
41+ bindTestClient(t, c, clientcore.CompositorInterface, 1, comp)
42+ bindTestClient(t, c, clientcore.ShmInterface, 1, shm)
43+ bindTestClient(t, c, clientcore.SeatInterface, 5, seat)
44+ bindTestClient(t, c, clientxdg.XdgWmBaseInterface, 1, wm)
45+ v := &pointerTarget{}
46+ ptr, err := seat.GetPointer()
47+ check(err)
48+ ptr.OnEnter(func(_, _ uint32, x, y wlturbo.Fixed) { v.entered, v.x, v.y = true, x.Float64(), y.Float64() })
49+ ptr.OnMotion(func(_ uint32, x, y wlturbo.Fixed) {
50+ v.x, v.y = x.Float64(), y.Float64()
51+ if v.pressed && !v.release {
52+ v.motions++
53+ }
54+ })
55+ ptr.OnAxisDiscrete(func(axis uint32, n int32) {
56+ if axis == 0 {
57+ v.scrollV += n
58+ } else {
59+ v.scrollH += n
60+ }
61+ })
62+ ptr.OnButton(func(_, _, button, state uint32) {
63+ if state == 1 {
64+ v.buttons = append(v.buttons, button)
65+ }
66+ if button == 0x110 && state == 1 {
67+ v.pressed = true
68+ }
69+ if button == 0x110 && state == 0 && v.pressed {
70+ v.release = true
71+ }
72+ })
73+ kb, err := seat.GetKeyboard()
74+ check(err)
75+ kb.OnKeymap(func(_ uint32, fd *wlturbo.OwnedFD, _ uint32) { _ = fd.Close() })
76+ kb.OnEnter(func(uint32, uint32, []byte) { v.focused = true })
77+ kb.OnKey(func(_, _, key, state uint32) {
78+ if state == 1 {
79+ v.keys = append(v.keys, key)
80+ }
81+ })
82+ const side = 64
83+ fd, err := unix.MemfdCreate("pointer-target", 0)
84+ check(err)
85+ px := make([]byte, side*side*4)
86+ for i := 0; i < len(px); i += 4 {
87+ binary.LittleEndian.PutUint32(px[i:], 0xffff0000) // ARGB red
88+ }
89+ if _, err := unix.Write(fd, px); err != nil {
90+ _ = unix.Close(fd)
91+ t.Fatal(err)
92+ }
93+ pool, err := shm.CreatePool(fd, int32(len(px)))
94+ if err != nil {
95+ _ = unix.Close(fd)
96+ t.Fatal(err)
97+ }
98+ buffer, err := pool.CreateBuffer(0, side, side, side*4, clientcore.FORMAT_XRGB8888)
99+ check(err)
100+ surf, err := comp.CreateSurface()
101+ check(err)
102+ xdg, err := wm.GetXdgSurface(surf)
103+ check(err)
104+ var serial uint32
105+ xdg.OnConfigure(func(value uint32) { serial = value })
106+ _, err = xdg.GetToplevel()
107+ check(err)
108+ check(surf.Commit())
109+ dispatchUntil(t, c, 5*time.Second, func() string { return "no configure" }, func() bool { return serial != 0 })
110+ check(xdg.AckConfigure(serial))
111+ check(surf.Attach(buffer, 0, 0))
112+ check(surf.Damage(0, 0, side, side))
113+ check(surf.Commit())
114+ return v
115+}
A internal/app/pointercli_test.go +139 -0
@@ -0,0 +1,139 @@
1+package app
2+
3+import (
4+ "context"
5+ "fmt"
6+ "os"
7+ "os/exec"
8+ "path/filepath"
9+ "strings"
10+ "testing"
11+ "time"
12+
13+ "bnema.dev/wlturbo"
14+ "github.com/bnema/neferwl/internal/adapters/config"
15+)
16+
17+// startPointerHeadless runs a headless NeferWL with one w x h output in an
18+// isolated runtime and returns its socket path.
19+//
20+//testpolicy:realtime real headless compositor startup and teardown
21+func startPointerHeadless(t *testing.T, w, h int) string {
22+ t.Helper()
23+ for _, k := range []string{"NEFERWL_STATE", "NEFERWL_SLOT", "WAYLAND_DISPLAY"} {
24+ if v, ok := os.LookupEnv(k); ok {
25+ os.Unsetenv(k)
26+ t.Cleanup(func() { os.Setenv(k, v) })
27+ }
28+ }
29+ runtime := t.TempDir()
30+ t.Setenv("XDG_RUNTIME_DIR", runtime)
31+ t.Setenv("XDG_STATE_HOME", t.TempDir())
32+ cfg := config.Defaults()
33+ cfg.Animations.On = false
34+ ctx, cancel := context.WithCancel(context.Background())
35+ done := make(chan error, 1)
36+ go func() {
37+ done <- Run(ctx, Options{Backend: "headless", NoXwayland: true, NoTerminal: true, Config: cfg, Sizes: [][2]int{{w, h}}})
38+ }()
39+ stopped := false
40+ t.Cleanup(func() {
41+ cancel()
42+ if stopped {
43+ return
44+ }
45+ select {
46+ case err := <-done:
47+ if err != nil {
48+ t.Errorf("Run: %v", err)
49+ }
50+ case <-time.After(5 * time.Second):
51+ t.Error("Run did not stop")
52+ }
53+ })
54+ socket := filepath.Join(runtime, "wayland-1")
55+ waitFile := fileChangesWithin(t, runtime, 5*time.Second)
56+ for {
57+ if _, err := os.Stat(socket); err == nil {
58+ return socket
59+ }
60+ select {
61+ case err := <-done:
62+ stopped = true
63+ if err != nil && strings.Contains(strings.ToLower(err.Error()), "vulkan") {
64+ t.Skipf("Vulkan unavailable: %v", err)
65+ }
66+ t.Fatalf("neferwl exited: %v", err)
67+ default:
68+ }
69+ if !waitFile() {
70+ t.Fatal("no wayland socket")
71+ }
72+ }
73+}
74+
75+func buildNeferwl(t *testing.T) string {
76+ t.Helper()
77+ bin := filepath.Join(t.TempDir(), "neferwl")
78+ build := exec.Command("go", "build", "-o", bin, "../../cmd/neferwl")
79+ build.Env = append(os.Environ(), "CGO_ENABLED=0")
80+ if out, err := build.CombinedOutput(); err != nil {
81+ t.Fatalf("build neferwl: %v: %s", err, out)
82+ }
83+ return bin
84+}
85+
86+// pointerCLI runs `neferwl pointer args...` against the headless display.
87+//
88+//testpolicy:realtime child process against the real headless compositor
89+func pointerCLI(t *testing.T, bin string, args ...string) {
90+ t.Helper()
91+ ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
92+ defer cancel()
93+ cmd := exec.CommandContext(ctx, bin, append([]string{"pointer"}, args...)...)
94+ cmd.Env = append(os.Environ(), "WAYLAND_DISPLAY=wayland-1")
95+ if out, err := cmd.CombinedOutput(); err != nil {
96+ t.Fatalf("neferwl pointer %v: %v: %s", args, err, out)
97+ }
98+}
99+
100+// `neferwl pointer` clicks, drags and scrolls on a real client of a
101+// headless NeferWL; buttons it leaves pressed are released at exit.
102+func TestPointerCLI(t *testing.T) {
103+ bin := buildNeferwl(t)
104+ socket := startPointerHeadless(t, 320, 240)
105+ c, err := wlturbo.Connect(socket)
106+ if err != nil {
107+ t.Fatal(err)
108+ }
109+ defer c.Close()
110+ target := mapPointerTarget(t, c)
111+ dispatchUntil(t, c, 5*time.Second, func() string { return "no keyboard focus" }, func() bool { return target.focused })
112+ wait := func(what string, ok func() bool) {
113+ t.Helper()
114+ dispatchUntil(t, c, 5*time.Second, func() string { return fmt.Sprintf("%s: %+v", what, *target) }, ok)
115+ }
116+
117+ // The only window fills the output: its centre is the output centre.
118+ pointerCLI(t, bin, "abs", "0.5", "0.5", "click")
119+ wait("click", func() bool { return target.entered && target.release })
120+ x0, y0 := target.x, target.y
121+
122+ target.pressed, target.release = false, false
123+ pointerCLI(t, bin, "--drag-steps", "4", "--drag-step-delay", "1ms", "drag", "40", "-20")
124+ wait("drag", func() bool { return target.release })
125+ if target.motions < 4 || target.x != x0+40 || target.y != y0-20 {
126+ t.Fatalf("drag: %d motions to %v,%v from %v,%v", target.motions, target.x, target.y, x0, y0)
127+ }
128+
129+ pointerCLI(t, bin, "scroll", "down", "3", "scroll", "left")
130+ wait("scroll", func() bool { return target.scrollV == 3 && target.scrollH == -1 })
131+
132+ // A press left at exit is released when the pointer goes away.
133+ target.pressed, target.release = false, false
134+ pointerCLI(t, bin, "move", "-10", "0", "press", "right", "press")
135+ wait("release at exit", func() bool { return target.release })
136+ if got := target.buttons[len(target.buttons)-2:]; got[0] != 0x111 || got[1] != 0x110 {
137+ t.Fatalf("buttons %#x", target.buttons)
138+ }
139+}
@@ -0,0 +1,139 @@
1+package app
2+
3+import (
4+ "context"
5+ "fmt"
6+ "os"
7+ "os/exec"
8+ "path/filepath"
9+ "strings"
10+ "testing"
11+ "time"
12+
13+ "bnema.dev/wlturbo"
14+ "github.com/bnema/neferwl/internal/adapters/config"
15+)
16+
17+// startPointerHeadless runs a headless NeferWL with one w x h output in an
18+// isolated runtime and returns its socket path.
19+//
20+//testpolicy:realtime real headless compositor startup and teardown
21+func startPointerHeadless(t *testing.T, w, h int) string {
22+ t.Helper()
23+ for _, k := range []string{"NEFERWL_STATE", "NEFERWL_SLOT", "WAYLAND_DISPLAY"} {
24+ if v, ok := os.LookupEnv(k); ok {
25+ os.Unsetenv(k)
26+ t.Cleanup(func() { os.Setenv(k, v) })
27+ }
28+ }
29+ runtime := t.TempDir()
30+ t.Setenv("XDG_RUNTIME_DIR", runtime)
31+ t.Setenv("XDG_STATE_HOME", t.TempDir())
32+ cfg := config.Defaults()
33+ cfg.Animations.On = false
34+ ctx, cancel := context.WithCancel(context.Background())
35+ done := make(chan error, 1)
36+ go func() {
37+ done <- Run(ctx, Options{Backend: "headless", NoXwayland: true, NoTerminal: true, Config: cfg, Sizes: [][2]int{{w, h}}})
38+ }()
39+ stopped := false
40+ t.Cleanup(func() {
41+ cancel()
42+ if stopped {
43+ return
44+ }
45+ select {
46+ case err := <-done:
47+ if err != nil {
48+ t.Errorf("Run: %v", err)
49+ }
50+ case <-time.After(5 * time.Second):
51+ t.Error("Run did not stop")
52+ }
53+ })
54+ socket := filepath.Join(runtime, "wayland-1")
55+ waitFile := fileChangesWithin(t, runtime, 5*time.Second)
56+ for {
57+ if _, err := os.Stat(socket); err == nil {
58+ return socket
59+ }
60+ select {
61+ case err := <-done:
62+ stopped = true
63+ if err != nil && strings.Contains(strings.ToLower(err.Error()), "vulkan") {
64+ t.Skipf("Vulkan unavailable: %v", err)
65+ }
66+ t.Fatalf("neferwl exited: %v", err)
67+ default:
68+ }
69+ if !waitFile() {
70+ t.Fatal("no wayland socket")
71+ }
72+ }
73+}
74+
75+func buildNeferwl(t *testing.T) string {
76+ t.Helper()
77+ bin := filepath.Join(t.TempDir(), "neferwl")
78+ build := exec.Command("go", "build", "-o", bin, "../../cmd/neferwl")
79+ build.Env = append(os.Environ(), "CGO_ENABLED=0")
80+ if out, err := build.CombinedOutput(); err != nil {
81+ t.Fatalf("build neferwl: %v: %s", err, out)
82+ }
83+ return bin
84+}
85+
86+// pointerCLI runs `neferwl pointer args...` against the headless display.
87+//
88+//testpolicy:realtime child process against the real headless compositor
89+func pointerCLI(t *testing.T, bin string, args ...string) {
90+ t.Helper()
91+ ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
92+ defer cancel()
93+ cmd := exec.CommandContext(ctx, bin, append([]string{"pointer"}, args...)...)
94+ cmd.Env = append(os.Environ(), "WAYLAND_DISPLAY=wayland-1")
95+ if out, err := cmd.CombinedOutput(); err != nil {
96+ t.Fatalf("neferwl pointer %v: %v: %s", args, err, out)
97+ }
98+}
99+
100+// `neferwl pointer` clicks, drags and scrolls on a real client of a
101+// headless NeferWL; buttons it leaves pressed are released at exit.
102+func TestPointerCLI(t *testing.T) {
103+ bin := buildNeferwl(t)
104+ socket := startPointerHeadless(t, 320, 240)
105+ c, err := wlturbo.Connect(socket)
106+ if err != nil {
107+ t.Fatal(err)
108+ }
109+ defer c.Close()
110+ target := mapPointerTarget(t, c)
111+ dispatchUntil(t, c, 5*time.Second, func() string { return "no keyboard focus" }, func() bool { return target.focused })
112+ wait := func(what string, ok func() bool) {
113+ t.Helper()
114+ dispatchUntil(t, c, 5*time.Second, func() string { return fmt.Sprintf("%s: %+v", what, *target) }, ok)
115+ }
116+
117+ // The only window fills the output: its centre is the output centre.
118+ pointerCLI(t, bin, "abs", "0.5", "0.5", "click")
119+ wait("click", func() bool { return target.entered && target.release })
120+ x0, y0 := target.x, target.y
121+
122+ target.pressed, target.release = false, false
123+ pointerCLI(t, bin, "--drag-steps", "4", "--drag-step-delay", "1ms", "drag", "40", "-20")
124+ wait("drag", func() bool { return target.release })
125+ if target.motions < 4 || target.x != x0+40 || target.y != y0-20 {
126+ t.Fatalf("drag: %d motions to %v,%v from %v,%v", target.motions, target.x, target.y, x0, y0)
127+ }
128+
129+ pointerCLI(t, bin, "scroll", "down", "3", "scroll", "left")
130+ wait("scroll", func() bool { return target.scrollV == 3 && target.scrollH == -1 })
131+
132+ // A press left at exit is released when the pointer goes away.
133+ target.pressed, target.release = false, false
134+ pointerCLI(t, bin, "move", "-10", "0", "press", "right", "press")
135+ wait("release at exit", func() bool { return target.release })
136+ if got := target.buttons[len(target.buttons)-2:]; got[0] != 0x111 || got[1] != 0x110 {
137+ t.Fatalf("buttons %#x", target.buttons)
138+ }
139+}
M internal/app/session.go +1 -1
@@ -336,7 +336,7 @@ func (s *session) run(ctx context.Context, inject func(chan<- ports.InputEvent))
336336 })
337337 return
338338 }
339- if err := headlessinput.RunSecure(ctx, km, p.keymaps, script, p.input, p.layouts, func(o string, x, y float64) { curs.Move(o, x, y, true) }, logging.For(ctx, "input"), security); err != nil && !errors.Is(err, context.Canceled) {
339+ if err := headlessinput.RunSecure(ctx, km, p.keymaps, script, p.input, p.layouts, p.constraints, func(o string, x, y float64) { curs.Move(o, x, y, true) }, logging.For(ctx, "input"), security); err != nil && !errors.Is(err, context.Canceled) {
340340 select {
341341 case done <- err:
342342 case <-ctx.Done():
@@ -336,7 +336,7 @@ func (s *session) run(ctx context.Context, inject func(chan<- ports.InputEvent))
336 })336 })
337 return337 return
338 }338 }
339- if err := headlessinput.RunSecure(ctx, km, p.keymaps, script, p.input, p.layouts, func(o string, x, y float64) { curs.Move(o, x, y, true) }, logging.For(ctx, "input"), security); err != nil && !errors.Is(err, context.Canceled) {339+ if err := headlessinput.RunSecure(ctx, km, p.keymaps, script, p.input, p.layouts, p.constraints, func(o string, x, y float64) { curs.Move(o, x, y, true) }, logging.For(ctx, "input"), security); err != nil && !errors.Is(err, context.Canceled) {
340 select {340 select {
341 case done <- err:341 case done <- err:
342 case <-ctx.Done():342 case <-ctx.Done():
A internal/app/wayvnc_test.go +322 -0
@@ -0,0 +1,322 @@
1+//go:build wayvnc
2+
3+// End-to-end test of remote viewing and control with wayvnc: a headless
4+// NeferWL, a real wayvnc (wlr-screencopy, virtual pointer, virtual
5+// keyboard) and a minimal RFB client in the test. It needs wayvnc on PATH
6+// and a GPU, so it is excluded from `go test ./...`. Run it with
7+// `make e2e-wayvnc`.
8+package app
9+
10+import (
11+ "bufio"
12+ "context"
13+ "encoding/binary"
14+ "fmt"
15+ "io"
16+ "net"
17+ "os"
18+ "os/exec"
19+ "path/filepath"
20+ "strconv"
21+ "strings"
22+ "testing"
23+ "time"
24+
25+ "bnema.dev/wlturbo"
26+ "github.com/bnema/neferwl/internal/adapters/config"
27+ "golang.org/x/sys/unix"
28+)
29+
30+const vncW, vncH = 320, 240
31+
32+// rfb is a minimal RFB 3.8 client: no authentication, raw encoding, 32-bit
33+// little-endian true colour (blue, green, red, pad).
34+type rfb struct {
35+ conn net.Conn
36+ r *bufio.Reader
37+ w, h int
38+}
39+
40+// dialRFB connects and initialises a shared session; a socket deadline
41+// bounds every later read and write.
42+func dialRFB(t *testing.T, addr string) *rfb {
43+ t.Helper()
44+ conn, err := net.DialTimeout("tcp", addr, 5*time.Second)
45+ if err != nil {
46+ t.Fatal(err)
47+ }
48+ t.Cleanup(func() { _ = conn.Close() })
49+ _ = conn.SetDeadline(time.Now().Add(20 * time.Second))
50+ c := &rfb{conn: conn, r: bufio.NewReader(conn)}
51+ version := make([]byte, 12)
52+ c.read(t, version)
53+ if !strings.HasPrefix(string(version), "RFB 003.") {
54+ t.Fatalf("server version %q", version)
55+ }
56+ c.write(t, []byte("RFB 003.008\n"))
57+ n := c.u8(t)
58+ if n == 0 {
59+ reason := make([]byte, c.u32(t))
60+ c.read(t, reason)
61+ t.Fatalf("connection refused: %s", reason)
62+ }
63+ types := make([]byte, n)
64+ c.read(t, types)
65+ if !strings.ContainsRune(string(types), 1) {
66+ t.Fatalf("no None security type in %v (wayvnc must run without auth)", types)
67+ }
68+ c.write(t, []byte{1})
69+ if res := c.u32(t); res != 0 {
70+ t.Fatalf("security result %d", res)
71+ }
72+ c.write(t, []byte{1}) // shared
73+ c.w, c.h = int(c.u16(t)), int(c.u16(t))
74+ c.read(t, make([]byte, 16)) // server pixel format
75+ c.read(t, make([]byte, c.u32(t)))
76+ // SetPixelFormat: 32 bpp, depth 24, little endian, true colour,
77+ // max 255 each, shifts red 16, green 8, blue 0.
78+ pf := []byte{0, 0, 0, 0, 32, 24, 0, 1, 0, 255, 0, 255, 0, 255, 16, 8, 0, 0, 0, 0}
79+ c.write(t, pf)
80+ c.write(t, []byte{2, 0, 0, 1, 0, 0, 0, 0}) // SetEncodings: raw only
81+ return c
82+}
83+
84+func (c *rfb) read(t *testing.T, b []byte) {
85+ t.Helper()
86+ if _, err := io.ReadFull(c.r, b); err != nil {
87+ t.Fatal(err)
88+ }
89+}
90+
91+func (c *rfb) write(t *testing.T, b []byte) {
92+ t.Helper()
93+ if _, err := c.conn.Write(b); err != nil {
94+ t.Fatal(err)
95+ }
96+}
97+
98+func (c *rfb) u8(t *testing.T) byte { b := make([]byte, 1); c.read(t, b); return b[0] }
99+func (c *rfb) u16(t *testing.T) uint16 {
100+ b := make([]byte, 2)
101+ c.read(t, b)
102+ return binary.BigEndian.Uint16(b)
103+}
104+func (c *rfb) u32(t *testing.T) uint32 {
105+ b := make([]byte, 4)
106+ c.read(t, b)
107+ return binary.BigEndian.Uint32(b)
108+}
109+
110+// frame requests a full update and returns the framebuffer as 0xRRGGBB.
111+func (c *rfb) frame(t *testing.T) []uint32 {
112+ t.Helper()
113+ req := []byte{3, 0, 0, 0, 0, 0, 0, 0, 0, 0}
114+ binary.BigEndian.PutUint16(req[6:], uint16(c.w))
115+ binary.BigEndian.PutUint16(req[8:], uint16(c.h))
116+ c.write(t, req)
117+ fb := make([]uint32, c.w*c.h)
118+ for {
119+ switch typ := c.u8(t); typ {
120+ case 0:
121+ c.read(t, make([]byte, 1))
122+ for n := c.u16(t); n > 0; n-- {
123+ x, y, w, h := int(c.u16(t)), int(c.u16(t)), int(c.u16(t)), int(c.u16(t))
124+ if enc := int32(c.u32(t)); enc != 0 {
125+ // Pseudo-encodings carry no pixels we asked for.
126+ if w*h != 0 {
127+ t.Fatalf("unexpected encoding %d", enc)
128+ }
129+ continue
130+ }
131+ row := make([]byte, w*4)
132+ for j := 0; j < h; j++ {
133+ c.read(t, row)
134+ for i := 0; i < w; i++ {
135+ if x+i < c.w && y+j < c.h {
136+ fb[(y+j)*c.w+x+i] = binary.LittleEndian.Uint32(row[i*4:]) & 0xffffff
137+ }
138+ }
139+ }
140+ }
141+ return fb
142+ case 2: // bell
143+ case 3: // server cut text
144+ c.read(t, make([]byte, 3))
145+ c.read(t, make([]byte, c.u32(t)))
146+ default:
147+ t.Fatalf("unexpected server message %d", typ)
148+ }
149+ }
150+}
151+
152+func (c *rfb) pointer(t *testing.T, mask byte, x, y int) {
153+ t.Helper()
154+ b := []byte{5, mask, 0, 0, 0, 0}
155+ binary.BigEndian.PutUint16(b[2:], uint16(x))
156+ binary.BigEndian.PutUint16(b[4:], uint16(y))
157+ c.write(t, b)
158+}
159+
160+func (c *rfb) key(t *testing.T, keysym uint32, down bool) {
161+ t.Helper()
162+ b := []byte{4, 0, 0, 0, 0, 0, 0, 0}
163+ if down {
164+ b[1] = 1
165+ }
166+ binary.BigEndian.PutUint32(b[4:], keysym)
167+ c.write(t, b)
168+}
169+
170+func freePort(t *testing.T) int {
171+ t.Helper()
172+ l, err := net.Listen("tcp", "127.0.0.1:0")
173+ if err != nil {
174+ t.Fatal(err)
175+ }
176+ defer l.Close()
177+ return l.Addr().(*net.TCPAddr).Port
178+}
179+
180+// TestWayVNC: wayvnc captures the headless output and its remote pointer and
181+// keyboard drive the session through the virtual pointer and keyboard.
182+//
183+//testpolicy:realtime real headless compositor, wayvnc child process and sockets
184+func TestWayVNC(t *testing.T) {
185+ wayvnc, err := exec.LookPath("wayvnc")
186+ if err != nil {
187+ t.Skip("wayvnc not on PATH")
188+ }
189+ if real, err := filepath.EvalSymlinks(wayvnc); err == nil {
190+ wayvnc = real
191+ }
192+ for _, k := range []string{"NEFERWL_STATE", "NEFERWL_SLOT", "WAYLAND_DISPLAY"} {
193+ if v, ok := os.LookupEnv(k); ok {
194+ os.Unsetenv(k)
195+ t.Cleanup(func() { os.Setenv(k, v) })
196+ }
197+ }
198+ runtime := t.TempDir()
199+ t.Setenv("XDG_RUNTIME_DIR", runtime)
200+ t.Setenv("XDG_STATE_HOME", t.TempDir())
201+ t.Setenv("XDG_CONFIG_HOME", t.TempDir()) // no user wayvnc config (auth)
202+ allowPath := filepath.Join(t.TempDir(), "capture-allow")
203+ if err := os.WriteFile(allowPath, []byte(wayvnc+"\n"), 0o644); err != nil {
204+ t.Fatal(err)
205+ }
206+ cfg := config.Defaults()
207+ cfg.Animations.On = false
208+ ctx, cancel := context.WithCancel(context.Background())
209+ done := make(chan error, 1)
210+ go func() {
211+ done <- Run(ctx, Options{Backend: "headless", NoXwayland: true, NoTerminal: true, Config: cfg, Sizes: [][2]int{{vncW, vncH}}, captureAllowPath: allowPath, captureAllowOwner: uint32(os.Getuid())})
212+ }()
213+ stopped := false
214+ defer func() {
215+ cancel()
216+ if stopped {
217+ return
218+ }
219+ select {
220+ case err := <-done:
221+ if err != nil {
222+ t.Errorf("Run: %v", err)
223+ }
224+ case <-time.After(5 * time.Second):
225+ t.Error("Run did not stop")
226+ }
227+ }()
228+ socket := filepath.Join(runtime, "wayland-1")
229+ waitFile := fileChangesWithin(t, runtime, 5*time.Second)
230+ for {
231+ if _, err := os.Stat(socket); err == nil {
232+ break
233+ }
234+ select {
235+ case err := <-done:
236+ stopped = true
237+ if err != nil && strings.Contains(strings.ToLower(err.Error()), "vulkan") {
238+ t.Skipf("Vulkan unavailable: %v", err)
239+ }
240+ t.Fatalf("neferwl exited: %v", err)
241+ default:
242+ }
243+ if !waitFile() {
244+ t.Fatal("no wayland socket")
245+ }
246+ }
247+
248+ c, err := wlturbo.Connect(socket)
249+ if err != nil {
250+ t.Fatal(err)
251+ }
252+ defer c.Close()
253+ target := mapPointerTarget(t, c)
254+ dispatchUntil(t, c, 5*time.Second, func() string { return "no keyboard focus" }, func() bool { return target.focused })
255+
256+ port := freePort(t)
257+ cmd := exec.Command(wayvnc, "-L", "info", "-S", filepath.Join(runtime, "wayvncctl"), "-o", "HEADLESS-1", "127.0.0.1", strconv.Itoa(port))
258+ cmd.Env = append(os.Environ(), "WAYLAND_DISPLAY=wayland-1")
259+ out := &lockedBuffer{changed: make(chan struct{}, 1)}
260+ cmd.Stdout, cmd.Stderr = out, out
261+ if err := cmd.Start(); err != nil {
262+ t.Fatal(err)
263+ }
264+ exited := make(chan error, 1)
265+ go func() { exited <- cmd.Wait() }()
266+ defer func() {
267+ _ = cmd.Process.Signal(unix.SIGTERM)
268+ select {
269+ case <-exited:
270+ case <-time.After(5 * time.Second):
271+ _ = cmd.Process.Kill()
272+ <-exited
273+ }
274+ }()
275+ ready := time.After(5 * time.Second)
276+ for !strings.Contains(out.String(), "Listening") {
277+ select {
278+ case err := <-exited:
279+ exited <- err
280+ t.Fatalf("wayvnc exited: %v: %s", err, out.String())
281+ default:
282+ }
283+ if !waitProcessOutputUntil(out, ready) {
284+ t.Fatalf("wayvnc not listening: %s", out.String())
285+ }
286+ }
287+
288+ vnc := dialRFB(t, fmt.Sprintf("127.0.0.1:%d", port))
289+ if vnc.w != vncW || vnc.h != vncH {
290+ t.Fatalf("framebuffer %dx%d, want %dx%d", vnc.w, vnc.h, vncW, vncH)
291+ }
292+ // The red square shows in the remote framebuffer; aim at its centre.
293+ var fb []uint32
294+ rx, ry, n := 0, 0, 0
295+ for attempt := 0; attempt < 10 && n == 0; attempt++ {
296+ fb = vnc.frame(t)
297+ for i, p := range fb {
298+ if p == 0xff0000 {
299+ rx, ry, n = rx+i%vnc.w, ry+i/vnc.w, n+1
300+ }
301+ }
302+ }
303+ if n == 0 {
304+ t.Fatalf("client content missing from the framebuffer: %s", out.String())
305+ }
306+ rx, ry = rx/n, ry/n
307+ t.Logf("red square centre %d,%d (%d px)", rx, ry, n)
308+
309+ vnc.pointer(t, 0, rx, ry)
310+ vnc.pointer(t, 1, rx, ry)
311+ vnc.pointer(t, 0, rx, ry)
312+ dispatchUntil(t, c, 5*time.Second, func() string {
313+ return fmt.Sprintf("entered %v pressed %v released %v; wayvnc: %s", target.entered, target.pressed, target.release, out.String())
314+ }, func() bool { return target.entered && target.release })
315+
316+ vnc.key(t, 'a', true)
317+ vnc.key(t, 'a', false)
318+ dispatchUntil(t, c, 5*time.Second, func() string { return fmt.Sprintf("keys %v; wayvnc: %s", target.keys, out.String()) }, func() bool { return len(target.keys) > 0 })
319+ if target.keys[0] != 30 { // KEY_A in wayvnc's default us layout
320+ t.Fatalf("key %d, want 30 (KEY_A)", target.keys[0])
321+ }
322+}
@@ -0,0 +1,322 @@
1+//go:build wayvnc
2+
3+// End-to-end test of remote viewing and control with wayvnc: a headless
4+// NeferWL, a real wayvnc (wlr-screencopy, virtual pointer, virtual
5+// keyboard) and a minimal RFB client in the test. It needs wayvnc on PATH
6+// and a GPU, so it is excluded from `go test ./...`. Run it with
7+// `make e2e-wayvnc`.
8+package app
9+
10+import (
11+ "bufio"
12+ "context"
13+ "encoding/binary"
14+ "fmt"
15+ "io"
16+ "net"
17+ "os"
18+ "os/exec"
19+ "path/filepath"
20+ "strconv"
21+ "strings"
22+ "testing"
23+ "time"
24+
25+ "bnema.dev/wlturbo"
26+ "github.com/bnema/neferwl/internal/adapters/config"
27+ "golang.org/x/sys/unix"
28+)
29+
30+const vncW, vncH = 320, 240
31+
32+// rfb is a minimal RFB 3.8 client: no authentication, raw encoding, 32-bit
33+// little-endian true colour (blue, green, red, pad).
34+type rfb struct {
35+ conn net.Conn
36+ r *bufio.Reader
37+ w, h int
38+}
39+
40+// dialRFB connects and initialises a shared session; a socket deadline
41+// bounds every later read and write.
42+func dialRFB(t *testing.T, addr string) *rfb {
43+ t.Helper()
44+ conn, err := net.DialTimeout("tcp", addr, 5*time.Second)
45+ if err != nil {
46+ t.Fatal(err)
47+ }
48+ t.Cleanup(func() { _ = conn.Close() })
49+ _ = conn.SetDeadline(time.Now().Add(20 * time.Second))
50+ c := &rfb{conn: conn, r: bufio.NewReader(conn)}
51+ version := make([]byte, 12)
52+ c.read(t, version)
53+ if !strings.HasPrefix(string(version), "RFB 003.") {
54+ t.Fatalf("server version %q", version)
55+ }
56+ c.write(t, []byte("RFB 003.008\n"))
57+ n := c.u8(t)
58+ if n == 0 {
59+ reason := make([]byte, c.u32(t))
60+ c.read(t, reason)
61+ t.Fatalf("connection refused: %s", reason)
62+ }
63+ types := make([]byte, n)
64+ c.read(t, types)
65+ if !strings.ContainsRune(string(types), 1) {
66+ t.Fatalf("no None security type in %v (wayvnc must run without auth)", types)
67+ }
68+ c.write(t, []byte{1})
69+ if res := c.u32(t); res != 0 {
70+ t.Fatalf("security result %d", res)
71+ }
72+ c.write(t, []byte{1}) // shared
73+ c.w, c.h = int(c.u16(t)), int(c.u16(t))
74+ c.read(t, make([]byte, 16)) // server pixel format
75+ c.read(t, make([]byte, c.u32(t)))
76+ // SetPixelFormat: 32 bpp, depth 24, little endian, true colour,
77+ // max 255 each, shifts red 16, green 8, blue 0.
78+ pf := []byte{0, 0, 0, 0, 32, 24, 0, 1, 0, 255, 0, 255, 0, 255, 16, 8, 0, 0, 0, 0}
79+ c.write(t, pf)
80+ c.write(t, []byte{2, 0, 0, 1, 0, 0, 0, 0}) // SetEncodings: raw only
81+ return c
82+}
83+
84+func (c *rfb) read(t *testing.T, b []byte) {
85+ t.Helper()
86+ if _, err := io.ReadFull(c.r, b); err != nil {
87+ t.Fatal(err)
88+ }
89+}
90+
91+func (c *rfb) write(t *testing.T, b []byte) {
92+ t.Helper()
93+ if _, err := c.conn.Write(b); err != nil {
94+ t.Fatal(err)
95+ }
96+}
97+
98+func (c *rfb) u8(t *testing.T) byte { b := make([]byte, 1); c.read(t, b); return b[0] }
99+func (c *rfb) u16(t *testing.T) uint16 {
100+ b := make([]byte, 2)
101+ c.read(t, b)
102+ return binary.BigEndian.Uint16(b)
103+}
104+func (c *rfb) u32(t *testing.T) uint32 {
105+ b := make([]byte, 4)
106+ c.read(t, b)
107+ return binary.BigEndian.Uint32(b)
108+}
109+
110+// frame requests a full update and returns the framebuffer as 0xRRGGBB.
111+func (c *rfb) frame(t *testing.T) []uint32 {
112+ t.Helper()
113+ req := []byte{3, 0, 0, 0, 0, 0, 0, 0, 0, 0}
114+ binary.BigEndian.PutUint16(req[6:], uint16(c.w))
115+ binary.BigEndian.PutUint16(req[8:], uint16(c.h))
116+ c.write(t, req)
117+ fb := make([]uint32, c.w*c.h)
118+ for {
119+ switch typ := c.u8(t); typ {
120+ case 0:
121+ c.read(t, make([]byte, 1))
122+ for n := c.u16(t); n > 0; n-- {
123+ x, y, w, h := int(c.u16(t)), int(c.u16(t)), int(c.u16(t)), int(c.u16(t))
124+ if enc := int32(c.u32(t)); enc != 0 {
125+ // Pseudo-encodings carry no pixels we asked for.
126+ if w*h != 0 {
127+ t.Fatalf("unexpected encoding %d", enc)
128+ }
129+ continue
130+ }
131+ row := make([]byte, w*4)
132+ for j := 0; j < h; j++ {
133+ c.read(t, row)
134+ for i := 0; i < w; i++ {
135+ if x+i < c.w && y+j < c.h {
136+ fb[(y+j)*c.w+x+i] = binary.LittleEndian.Uint32(row[i*4:]) & 0xffffff
137+ }
138+ }
139+ }
140+ }
141+ return fb
142+ case 2: // bell
143+ case 3: // server cut text
144+ c.read(t, make([]byte, 3))
145+ c.read(t, make([]byte, c.u32(t)))
146+ default:
147+ t.Fatalf("unexpected server message %d", typ)
148+ }
149+ }
150+}
151+
152+func (c *rfb) pointer(t *testing.T, mask byte, x, y int) {
153+ t.Helper()
154+ b := []byte{5, mask, 0, 0, 0, 0}
155+ binary.BigEndian.PutUint16(b[2:], uint16(x))
156+ binary.BigEndian.PutUint16(b[4:], uint16(y))
157+ c.write(t, b)
158+}
159+
160+func (c *rfb) key(t *testing.T, keysym uint32, down bool) {
161+ t.Helper()
162+ b := []byte{4, 0, 0, 0, 0, 0, 0, 0}
163+ if down {
164+ b[1] = 1
165+ }
166+ binary.BigEndian.PutUint32(b[4:], keysym)
167+ c.write(t, b)
168+}
169+
170+func freePort(t *testing.T) int {
171+ t.Helper()
172+ l, err := net.Listen("tcp", "127.0.0.1:0")
173+ if err != nil {
174+ t.Fatal(err)
175+ }
176+ defer l.Close()
177+ return l.Addr().(*net.TCPAddr).Port
178+}
179+
180+// TestWayVNC: wayvnc captures the headless output and its remote pointer and
181+// keyboard drive the session through the virtual pointer and keyboard.
182+//
183+//testpolicy:realtime real headless compositor, wayvnc child process and sockets
184+func TestWayVNC(t *testing.T) {
185+ wayvnc, err := exec.LookPath("wayvnc")
186+ if err != nil {
187+ t.Skip("wayvnc not on PATH")
188+ }
189+ if real, err := filepath.EvalSymlinks(wayvnc); err == nil {
190+ wayvnc = real
191+ }
192+ for _, k := range []string{"NEFERWL_STATE", "NEFERWL_SLOT", "WAYLAND_DISPLAY"} {
193+ if v, ok := os.LookupEnv(k); ok {
194+ os.Unsetenv(k)
195+ t.Cleanup(func() { os.Setenv(k, v) })
196+ }
197+ }
198+ runtime := t.TempDir()
199+ t.Setenv("XDG_RUNTIME_DIR", runtime)
200+ t.Setenv("XDG_STATE_HOME", t.TempDir())
201+ t.Setenv("XDG_CONFIG_HOME", t.TempDir()) // no user wayvnc config (auth)
202+ allowPath := filepath.Join(t.TempDir(), "capture-allow")
203+ if err := os.WriteFile(allowPath, []byte(wayvnc+"\n"), 0o644); err != nil {
204+ t.Fatal(err)
205+ }
206+ cfg := config.Defaults()
207+ cfg.Animations.On = false
208+ ctx, cancel := context.WithCancel(context.Background())
209+ done := make(chan error, 1)
210+ go func() {
211+ done <- Run(ctx, Options{Backend: "headless", NoXwayland: true, NoTerminal: true, Config: cfg, Sizes: [][2]int{{vncW, vncH}}, captureAllowPath: allowPath, captureAllowOwner: uint32(os.Getuid())})
212+ }()
213+ stopped := false
214+ defer func() {
215+ cancel()
216+ if stopped {
217+ return
218+ }
219+ select {
220+ case err := <-done:
221+ if err != nil {
222+ t.Errorf("Run: %v", err)
223+ }
224+ case <-time.After(5 * time.Second):
225+ t.Error("Run did not stop")
226+ }
227+ }()
228+ socket := filepath.Join(runtime, "wayland-1")
229+ waitFile := fileChangesWithin(t, runtime, 5*time.Second)
230+ for {
231+ if _, err := os.Stat(socket); err == nil {
232+ break
233+ }
234+ select {
235+ case err := <-done:
236+ stopped = true
237+ if err != nil && strings.Contains(strings.ToLower(err.Error()), "vulkan") {
238+ t.Skipf("Vulkan unavailable: %v", err)
239+ }
240+ t.Fatalf("neferwl exited: %v", err)
241+ default:
242+ }
243+ if !waitFile() {
244+ t.Fatal("no wayland socket")
245+ }
246+ }
247+
248+ c, err := wlturbo.Connect(socket)
249+ if err != nil {
250+ t.Fatal(err)
251+ }
252+ defer c.Close()
253+ target := mapPointerTarget(t, c)
254+ dispatchUntil(t, c, 5*time.Second, func() string { return "no keyboard focus" }, func() bool { return target.focused })
255+
256+ port := freePort(t)
257+ cmd := exec.Command(wayvnc, "-L", "info", "-S", filepath.Join(runtime, "wayvncctl"), "-o", "HEADLESS-1", "127.0.0.1", strconv.Itoa(port))
258+ cmd.Env = append(os.Environ(), "WAYLAND_DISPLAY=wayland-1")
259+ out := &lockedBuffer{changed: make(chan struct{}, 1)}
260+ cmd.Stdout, cmd.Stderr = out, out
261+ if err := cmd.Start(); err != nil {
262+ t.Fatal(err)
263+ }
264+ exited := make(chan error, 1)
265+ go func() { exited <- cmd.Wait() }()
266+ defer func() {
267+ _ = cmd.Process.Signal(unix.SIGTERM)
268+ select {
269+ case <-exited:
270+ case <-time.After(5 * time.Second):
271+ _ = cmd.Process.Kill()
272+ <-exited
273+ }
274+ }()
275+ ready := time.After(5 * time.Second)
276+ for !strings.Contains(out.String(), "Listening") {
277+ select {
278+ case err := <-exited:
279+ exited <- err
280+ t.Fatalf("wayvnc exited: %v: %s", err, out.String())
281+ default:
282+ }
283+ if !waitProcessOutputUntil(out, ready) {
284+ t.Fatalf("wayvnc not listening: %s", out.String())
285+ }
286+ }
287+
288+ vnc := dialRFB(t, fmt.Sprintf("127.0.0.1:%d", port))
289+ if vnc.w != vncW || vnc.h != vncH {
290+ t.Fatalf("framebuffer %dx%d, want %dx%d", vnc.w, vnc.h, vncW, vncH)
291+ }
292+ // The red square shows in the remote framebuffer; aim at its centre.
293+ var fb []uint32
294+ rx, ry, n := 0, 0, 0
295+ for attempt := 0; attempt < 10 && n == 0; attempt++ {
296+ fb = vnc.frame(t)
297+ for i, p := range fb {
298+ if p == 0xff0000 {
299+ rx, ry, n = rx+i%vnc.w, ry+i/vnc.w, n+1
300+ }
301+ }
302+ }
303+ if n == 0 {
304+ t.Fatalf("client content missing from the framebuffer: %s", out.String())
305+ }
306+ rx, ry = rx/n, ry/n
307+ t.Logf("red square centre %d,%d (%d px)", rx, ry, n)
308+
309+ vnc.pointer(t, 0, rx, ry)
310+ vnc.pointer(t, 1, rx, ry)
311+ vnc.pointer(t, 0, rx, ry)
312+ dispatchUntil(t, c, 5*time.Second, func() string {
313+ return fmt.Sprintf("entered %v pressed %v released %v; wayvnc: %s", target.entered, target.pressed, target.release, out.String())
314+ }, func() bool { return target.entered && target.release })
315+
316+ vnc.key(t, 'a', true)
317+ vnc.key(t, 'a', false)
318+ dispatchUntil(t, c, 5*time.Second, func() string { return fmt.Sprintf("keys %v; wayvnc: %s", target.keys, out.String()) }, func() bool { return len(target.keys) > 0 })
319+ if target.keys[0] != 30 { // KEY_A in wayvnc's default us layout
320+ t.Fatalf("key %d, want 30 (KEY_A)", target.keys[0])
321+ }
322+}
M internal/cli/cli.go +1 -1
@@ -32,7 +32,7 @@ type Command struct {
3232 // root runs the compositor; commands are the subcommands.
3333 var (
3434 root = Command{Args: "[flags]", Summary: "Run the compositor.", Setup: setupRun}
35- commands = []Command{validateConfigCmd, stateCmd, versionCmd}
35+ commands = []Command{validateConfigCmd, stateCmd, pointerCmd, versionCmd}
3636 )
3737
3838 // Main runs the command line args (without the program name) and returns
@@ -32,7 +32,7 @@ type Command struct {
32 // root runs the compositor; commands are the subcommands.32 // root runs the compositor; commands are the subcommands.
33 var (33 var (
34 root = Command{Args: "[flags]", Summary: "Run the compositor.", Setup: setupRun}34 root = Command{Args: "[flags]", Summary: "Run the compositor.", Setup: setupRun}
35- commands = []Command{validateConfigCmd, stateCmd, versionCmd}35+ commands = []Command{validateConfigCmd, stateCmd, pointerCmd, versionCmd}
36 )36 )
37 37
38 // Main runs the command line args (without the program name) and returns38 // Main runs the command line args (without the program name) and returns
A internal/cli/pointer.go +195 -0
@@ -0,0 +1,195 @@
1+package cli
2+
3+import (
4+ "context"
5+ "flag"
6+ "strconv"
7+ "strings"
8+ "time"
9+)
10+
11+// pointerAction is one parsed `neferwl pointer` action.
12+type pointerAction struct {
13+ kind string // abs, move, press, release, click, hold, drag, scroll
14+ x, y float64
15+ button uint32
16+ duration time.Duration // hold; 0 means --hold-duration
17+ steps int // scroll steps
18+ axis uint32 // scroll: 0 vertical, 1 horizontal
19+}
20+
21+// pointerOptions are the tunable timings of `neferwl pointer`.
22+type pointerOptions struct {
23+ output string
24+ clickDuration time.Duration
25+ holdDuration time.Duration
26+ dragSteps int
27+ dragStepDelay time.Duration
28+}
29+
30+// evdev button codes (linux/input-event-codes.h).
31+var pointerButtons = map[string]uint32{
32+ "left": 0x110, "right": 0x111, "middle": 0x112, "side": 0x113,
33+ "extra": 0x114, "forward": 0x115, "back": 0x116, "task": 0x117,
34+ "btn_left": 0x110, "btn_right": 0x111, "btn_middle": 0x112, "btn_side": 0x113,
35+ "btn_extra": 0x114, "btn_forward": 0x115, "btn_back": 0x116, "btn_task": 0x117,
36+}
37+
38+const btnLeft = 0x110
39+
40+// parseButton reads a button name, BTN_* name or evdev code (decimal or
41+// 0x hex) in the mouse range.
42+func parseButton(s string) (uint32, bool) {
43+ if b, ok := pointerButtons[strings.ToLower(s)]; ok {
44+ return b, true
45+ }
46+ n, err := strconv.ParseUint(s, 0, 32)
47+ if err != nil || n < 0x110 || n > 0x11f {
48+ return 0, false
49+ }
50+ return uint32(n), true
51+}
52+
53+func isButton(s string) bool { _, ok := parseButton(s); return ok }
54+
55+func parseFloat(s string) (float64, bool) {
56+ v, err := strconv.ParseFloat(s, 64)
57+ return v, err == nil && v == v && v-v == 0 // finite
58+}
59+
60+// parsePointerActions parses the action list, each action with its
61+// arguments, in order.
62+func parsePointerActions(args []string) ([]pointerAction, error) {
63+ if len(args) == 0 {
64+ return nil, usagef("no action")
65+ }
66+ var out []pointerAction
67+ for i := 0; i < len(args); i++ {
68+ name := args[i]
69+ rest := args[i+1:]
70+ a := pointerAction{kind: name, button: btnLeft}
71+ need := func(n int, what string) error {
72+ if len(rest) < n {
73+ return usagef("%s: expected %s", name, what)
74+ }
75+ return nil
76+ }
77+ optButton := func(at int) int {
78+ if len(rest) > at && isButton(rest[at]) {
79+ a.button, _ = parseButton(rest[at])
80+ return 1
81+ }
82+ return 0
83+ }
84+ switch name {
85+ case "abs", "move", "drag":
86+ if err := need(2, "two numbers"); err != nil {
87+ return nil, err
88+ }
89+ x, okx := parseFloat(rest[0])
90+ y, oky := parseFloat(rest[1])
91+ if !okx || !oky {
92+ return nil, usagef("%s: invalid numbers %q %q", name, rest[0], rest[1])
93+ }
94+ if name == "abs" && (x < 0 || x > 1 || y < 0 || y > 1) {
95+ return nil, usagef("abs: coordinates are fractions in 0..1, got %v %v", x, y)
96+ }
97+ a.x, a.y = x, y
98+ i += 2
99+ if name == "drag" {
100+ i += optButton(2)
101+ }
102+ case "press", "release", "click":
103+ i += optButton(0)
104+ case "hold":
105+ n := optButton(0)
106+ if len(rest) > n {
107+ if d, err := time.ParseDuration(rest[n]); err == nil {
108+ if d < 0 {
109+ return nil, usagef("hold: negative duration %s", rest[n])
110+ }
111+ a.duration = d
112+ n++
113+ }
114+ }
115+ i += n
116+ case "scroll":
117+ if err := need(1, "up, down, left or right"); err != nil {
118+ return nil, err
119+ }
120+ sign := 1
121+ switch rest[0] {
122+ case "up":
123+ sign = -1
124+ case "down":
125+ case "left":
126+ sign, a.axis = -1, 1
127+ case "right":
128+ a.axis = 1
129+ default:
130+ return nil, usagef("scroll: direction %q is not up, down, left or right", rest[0])
131+ }
132+ a.steps = sign
133+ i++
134+ if len(rest) > 1 {
135+ if n, err := strconv.Atoi(rest[1]); err == nil {
136+ if n <= 0 || n > 1000 {
137+ return nil, usagef("scroll: step count %d out of 1..1000", n)
138+ }
139+ a.steps = sign * n
140+ i++
141+ }
142+ }
143+ default:
144+ return nil, usagef("unknown action %q", name)
145+ }
146+ out = append(out, a)
147+ }
148+ return out, nil
149+}
150+
151+func (o pointerOptions) validate() error {
152+ switch {
153+ case o.clickDuration < 0, o.holdDuration < 0, o.dragStepDelay < 0:
154+ return usagef("durations must not be negative")
155+ case o.dragSteps < 1 || o.dragSteps > 10000:
156+ return usagef("--drag-steps must be in 1..10000, got %d", o.dragSteps)
157+ }
158+ return nil
159+}
160+
161+var pointerCmd = Command{
162+ Name: "pointer",
163+ Args: "[flags] <action> [args] [<action> [args]...]",
164+ Summary: "Drive the session pointer (zwlr_virtual_pointer_v1). Actions run in order:\n" +
165+ " abs X Y move to fractions 0..1 of the layout (or --output)\n" +
166+ " move DX DY move by logical pixels\n" +
167+ " press [BTN] press a button release [BTN] release it\n" +
168+ " click [BTN] press and release after --click-duration\n" +
169+ " hold [BTN] [DURATION] press, wait (default --hold-duration), release\n" +
170+ " drag DX DY [BTN] press, move in --drag-steps, release\n" +
171+ " scroll up|down|left|right [N] N wheel steps (default 1)\n" +
172+ "BTN is left (default), right, middle, side, extra, forward, back, a BTN_* name\n" +
173+ "or an evdev code. Buttons still pressed at exit are released.",
174+ Setup: func(fs *flag.FlagSet) Runner {
175+ var o pointerOptions
176+ fs.StringVar(&o.output, "output", "", "map abs into this output (create_virtual_pointer_with_output)")
177+ fs.DurationVar(&o.clickDuration, "click-duration", 20*time.Millisecond, "time a click holds the button")
178+ fs.DurationVar(&o.holdDuration, "hold-duration", 500*time.Millisecond, "default time of hold")
179+ fs.IntVar(&o.dragSteps, "drag-steps", 10, "motion steps of a drag")
180+ fs.DurationVar(&o.dragStepDelay, "drag-step-delay", 10*time.Millisecond, "time between drag steps")
181+ return func(ctx context.Context, env Env, args []string) error {
182+ if err := o.validate(); err != nil {
183+ return err
184+ }
185+ actions, err := parsePointerActions(args)
186+ if err != nil {
187+ return err
188+ }
189+ return runPointer(ctx, o, actions)
190+ }
191+ },
192+}
193+
194+// String names an action for errors.
195+func (a pointerAction) String() string { return a.kind }
@@ -0,0 +1,195 @@
1+package cli
2+
3+import (
4+ "context"
5+ "flag"
6+ "strconv"
7+ "strings"
8+ "time"
9+)
10+
11+// pointerAction is one parsed `neferwl pointer` action.
12+type pointerAction struct {
13+ kind string // abs, move, press, release, click, hold, drag, scroll
14+ x, y float64
15+ button uint32
16+ duration time.Duration // hold; 0 means --hold-duration
17+ steps int // scroll steps
18+ axis uint32 // scroll: 0 vertical, 1 horizontal
19+}
20+
21+// pointerOptions are the tunable timings of `neferwl pointer`.
22+type pointerOptions struct {
23+ output string
24+ clickDuration time.Duration
25+ holdDuration time.Duration
26+ dragSteps int
27+ dragStepDelay time.Duration
28+}
29+
30+// evdev button codes (linux/input-event-codes.h).
31+var pointerButtons = map[string]uint32{
32+ "left": 0x110, "right": 0x111, "middle": 0x112, "side": 0x113,
33+ "extra": 0x114, "forward": 0x115, "back": 0x116, "task": 0x117,
34+ "btn_left": 0x110, "btn_right": 0x111, "btn_middle": 0x112, "btn_side": 0x113,
35+ "btn_extra": 0x114, "btn_forward": 0x115, "btn_back": 0x116, "btn_task": 0x117,
36+}
37+
38+const btnLeft = 0x110
39+
40+// parseButton reads a button name, BTN_* name or evdev code (decimal or
41+// 0x hex) in the mouse range.
42+func parseButton(s string) (uint32, bool) {
43+ if b, ok := pointerButtons[strings.ToLower(s)]; ok {
44+ return b, true
45+ }
46+ n, err := strconv.ParseUint(s, 0, 32)
47+ if err != nil || n < 0x110 || n > 0x11f {
48+ return 0, false
49+ }
50+ return uint32(n), true
51+}
52+
53+func isButton(s string) bool { _, ok := parseButton(s); return ok }
54+
55+func parseFloat(s string) (float64, bool) {
56+ v, err := strconv.ParseFloat(s, 64)
57+ return v, err == nil && v == v && v-v == 0 // finite
58+}
59+
60+// parsePointerActions parses the action list, each action with its
61+// arguments, in order.
62+func parsePointerActions(args []string) ([]pointerAction, error) {
63+ if len(args) == 0 {
64+ return nil, usagef("no action")
65+ }
66+ var out []pointerAction
67+ for i := 0; i < len(args); i++ {
68+ name := args[i]
69+ rest := args[i+1:]
70+ a := pointerAction{kind: name, button: btnLeft}
71+ need := func(n int, what string) error {
72+ if len(rest) < n {
73+ return usagef("%s: expected %s", name, what)
74+ }
75+ return nil
76+ }
77+ optButton := func(at int) int {
78+ if len(rest) > at && isButton(rest[at]) {
79+ a.button, _ = parseButton(rest[at])
80+ return 1
81+ }
82+ return 0
83+ }
84+ switch name {
85+ case "abs", "move", "drag":
86+ if err := need(2, "two numbers"); err != nil {
87+ return nil, err
88+ }
89+ x, okx := parseFloat(rest[0])
90+ y, oky := parseFloat(rest[1])
91+ if !okx || !oky {
92+ return nil, usagef("%s: invalid numbers %q %q", name, rest[0], rest[1])
93+ }
94+ if name == "abs" && (x < 0 || x > 1 || y < 0 || y > 1) {
95+ return nil, usagef("abs: coordinates are fractions in 0..1, got %v %v", x, y)
96+ }
97+ a.x, a.y = x, y
98+ i += 2
99+ if name == "drag" {
100+ i += optButton(2)
101+ }
102+ case "press", "release", "click":
103+ i += optButton(0)
104+ case "hold":
105+ n := optButton(0)
106+ if len(rest) > n {
107+ if d, err := time.ParseDuration(rest[n]); err == nil {
108+ if d < 0 {
109+ return nil, usagef("hold: negative duration %s", rest[n])
110+ }
111+ a.duration = d
112+ n++
113+ }
114+ }
115+ i += n
116+ case "scroll":
117+ if err := need(1, "up, down, left or right"); err != nil {
118+ return nil, err
119+ }
120+ sign := 1
121+ switch rest[0] {
122+ case "up":
123+ sign = -1
124+ case "down":
125+ case "left":
126+ sign, a.axis = -1, 1
127+ case "right":
128+ a.axis = 1
129+ default:
130+ return nil, usagef("scroll: direction %q is not up, down, left or right", rest[0])
131+ }
132+ a.steps = sign
133+ i++
134+ if len(rest) > 1 {
135+ if n, err := strconv.Atoi(rest[1]); err == nil {
136+ if n <= 0 || n > 1000 {
137+ return nil, usagef("scroll: step count %d out of 1..1000", n)
138+ }
139+ a.steps = sign * n
140+ i++
141+ }
142+ }
143+ default:
144+ return nil, usagef("unknown action %q", name)
145+ }
146+ out = append(out, a)
147+ }
148+ return out, nil
149+}
150+
151+func (o pointerOptions) validate() error {
152+ switch {
153+ case o.clickDuration < 0, o.holdDuration < 0, o.dragStepDelay < 0:
154+ return usagef("durations must not be negative")
155+ case o.dragSteps < 1 || o.dragSteps > 10000:
156+ return usagef("--drag-steps must be in 1..10000, got %d", o.dragSteps)
157+ }
158+ return nil
159+}
160+
161+var pointerCmd = Command{
162+ Name: "pointer",
163+ Args: "[flags] <action> [args] [<action> [args]...]",
164+ Summary: "Drive the session pointer (zwlr_virtual_pointer_v1). Actions run in order:\n" +
165+ " abs X Y move to fractions 0..1 of the layout (or --output)\n" +
166+ " move DX DY move by logical pixels\n" +
167+ " press [BTN] press a button release [BTN] release it\n" +
168+ " click [BTN] press and release after --click-duration\n" +
169+ " hold [BTN] [DURATION] press, wait (default --hold-duration), release\n" +
170+ " drag DX DY [BTN] press, move in --drag-steps, release\n" +
171+ " scroll up|down|left|right [N] N wheel steps (default 1)\n" +
172+ "BTN is left (default), right, middle, side, extra, forward, back, a BTN_* name\n" +
173+ "or an evdev code. Buttons still pressed at exit are released.",
174+ Setup: func(fs *flag.FlagSet) Runner {
175+ var o pointerOptions
176+ fs.StringVar(&o.output, "output", "", "map abs into this output (create_virtual_pointer_with_output)")
177+ fs.DurationVar(&o.clickDuration, "click-duration", 20*time.Millisecond, "time a click holds the button")
178+ fs.DurationVar(&o.holdDuration, "hold-duration", 500*time.Millisecond, "default time of hold")
179+ fs.IntVar(&o.dragSteps, "drag-steps", 10, "motion steps of a drag")
180+ fs.DurationVar(&o.dragStepDelay, "drag-step-delay", 10*time.Millisecond, "time between drag steps")
181+ return func(ctx context.Context, env Env, args []string) error {
182+ if err := o.validate(); err != nil {
183+ return err
184+ }
185+ actions, err := parsePointerActions(args)
186+ if err != nil {
187+ return err
188+ }
189+ return runPointer(ctx, o, actions)
190+ }
191+ },
192+}
193+
194+// String names an action for errors.
195+func (a pointerAction) String() string { return a.kind }
A internal/cli/pointer_test.go +89 -0
@@ -0,0 +1,89 @@
1+package cli
2+
3+import (
4+ "errors"
5+ "reflect"
6+ "strings"
7+ "testing"
8+ "time"
9+)
10+
11+func TestParseButton(t *testing.T) {
12+ for in, want := range map[string]uint32{
13+ "left": 0x110, "RIGHT": 0x111, "middle": 0x112, "side": 0x113, "extra": 0x114,
14+ "forward": 0x115, "back": 0x116, "BTN_LEFT": 0x110, "btn_back": 0x116,
15+ "272": 0x110, "0x111": 0x111,
16+ } {
17+ if got, ok := parseButton(in); !ok || got != want {
18+ t.Errorf("%q = %#x %v, want %#x", in, got, ok, want)
19+ }
20+ }
21+ for _, in := range []string{"", "up", "1", "0x200", "-1", "BTN_JOYSTICK"} {
22+ if _, ok := parseButton(in); ok {
23+ t.Errorf("%q accepted", in)
24+ }
25+ }
26+}
27+
28+func TestParsePointerActions(t *testing.T) {
29+ got, err := parsePointerActions(strings.Fields("abs 0.5 1 move -10 2.5 press right release right click click middle hold hold side 2s hold 1s drag 30 0 drag 5 5 back scroll down scroll left 3 click"))
30+ if err != nil {
31+ t.Fatal(err)
32+ }
33+ want := []pointerAction{
34+ {kind: "abs", x: 0.5, y: 1, button: btnLeft},
35+ {kind: "move", x: -10, y: 2.5, button: btnLeft},
36+ {kind: "press", button: 0x111},
37+ {kind: "release", button: 0x111},
38+ {kind: "click", button: btnLeft},
39+ {kind: "click", button: 0x112},
40+ {kind: "hold", button: btnLeft},
41+ {kind: "hold", button: 0x113, duration: 2 * time.Second},
42+ {kind: "hold", button: btnLeft, duration: time.Second},
43+ {kind: "drag", x: 30, button: btnLeft},
44+ {kind: "drag", x: 5, y: 5, button: 0x116},
45+ {kind: "scroll", steps: 1, button: btnLeft},
46+ {kind: "scroll", steps: -3, axis: 1, button: btnLeft},
47+ {kind: "click", button: btnLeft},
48+ }
49+ if !reflect.DeepEqual(got, want) {
50+ t.Fatalf("got %+v\nwant %+v", got, want)
51+ }
52+}
53+
54+func TestParsePointerActionErrors(t *testing.T) {
55+ for _, in := range []string{
56+ "", "jump", "abs 0.5", "abs 2 0", "abs -0.1 0", "abs x y", "move 1", "move NaN 0",
57+ "move Inf 0", "drag 1", "scroll", "scroll sideways", "scroll up 0", "hold -1s",
58+ } {
59+ _, err := parsePointerActions(strings.Fields(in))
60+ var u usageError
61+ if !errors.As(err, &u) {
62+ t.Errorf("%q: %v, want a usage error", in, err)
63+ }
64+ }
65+}
66+
67+// Bad flags and actions exit 2 with the usage line, before connecting.
68+func TestPointerCommandUsage(t *testing.T) {
69+ t.Setenv("WAYLAND_DISPLAY", "")
70+ for _, args := range [][]string{
71+ {"pointer"},
72+ {"pointer", "fly"},
73+ {"pointer", "--drag-steps", "0", "click"},
74+ {"pointer", "--click-duration", "-1s", "click"},
75+ {"pointer", "--bogus", "click"},
76+ } {
77+ code, _, stderr := execute(t, args...)
78+ if code != 2 || !strings.Contains(stderr, "usage: neferwl pointer") {
79+ t.Errorf("%v: code %d stderr %q", args, code, stderr)
80+ }
81+ }
82+ code, stdout, _ := execute(t, "pointer", "-h")
83+ if code != 0 || !strings.Contains(stdout, "drag DX DY [BTN]") || !strings.Contains(stdout, "-drag-step-delay") {
84+ t.Fatalf("help %d %q", code, stdout)
85+ }
86+ if code, _, stderr := execute(t, "pointer", "click"); code != 1 || !strings.Contains(stderr, "WAYLAND_DISPLAY") {
87+ t.Fatalf("no display: %d %q", code, stderr)
88+ }
89+}
@@ -0,0 +1,89 @@
1+package cli
2+
3+import (
4+ "errors"
5+ "reflect"
6+ "strings"
7+ "testing"
8+ "time"
9+)
10+
11+func TestParseButton(t *testing.T) {
12+ for in, want := range map[string]uint32{
13+ "left": 0x110, "RIGHT": 0x111, "middle": 0x112, "side": 0x113, "extra": 0x114,
14+ "forward": 0x115, "back": 0x116, "BTN_LEFT": 0x110, "btn_back": 0x116,
15+ "272": 0x110, "0x111": 0x111,
16+ } {
17+ if got, ok := parseButton(in); !ok || got != want {
18+ t.Errorf("%q = %#x %v, want %#x", in, got, ok, want)
19+ }
20+ }
21+ for _, in := range []string{"", "up", "1", "0x200", "-1", "BTN_JOYSTICK"} {
22+ if _, ok := parseButton(in); ok {
23+ t.Errorf("%q accepted", in)
24+ }
25+ }
26+}
27+
28+func TestParsePointerActions(t *testing.T) {
29+ got, err := parsePointerActions(strings.Fields("abs 0.5 1 move -10 2.5 press right release right click click middle hold hold side 2s hold 1s drag 30 0 drag 5 5 back scroll down scroll left 3 click"))
30+ if err != nil {
31+ t.Fatal(err)
32+ }
33+ want := []pointerAction{
34+ {kind: "abs", x: 0.5, y: 1, button: btnLeft},
35+ {kind: "move", x: -10, y: 2.5, button: btnLeft},
36+ {kind: "press", button: 0x111},
37+ {kind: "release", button: 0x111},
38+ {kind: "click", button: btnLeft},
39+ {kind: "click", button: 0x112},
40+ {kind: "hold", button: btnLeft},
41+ {kind: "hold", button: 0x113, duration: 2 * time.Second},
42+ {kind: "hold", button: btnLeft, duration: time.Second},
43+ {kind: "drag", x: 30, button: btnLeft},
44+ {kind: "drag", x: 5, y: 5, button: 0x116},
45+ {kind: "scroll", steps: 1, button: btnLeft},
46+ {kind: "scroll", steps: -3, axis: 1, button: btnLeft},
47+ {kind: "click", button: btnLeft},
48+ }
49+ if !reflect.DeepEqual(got, want) {
50+ t.Fatalf("got %+v\nwant %+v", got, want)
51+ }
52+}
53+
54+func TestParsePointerActionErrors(t *testing.T) {
55+ for _, in := range []string{
56+ "", "jump", "abs 0.5", "abs 2 0", "abs -0.1 0", "abs x y", "move 1", "move NaN 0",
57+ "move Inf 0", "drag 1", "scroll", "scroll sideways", "scroll up 0", "hold -1s",
58+ } {
59+ _, err := parsePointerActions(strings.Fields(in))
60+ var u usageError
61+ if !errors.As(err, &u) {
62+ t.Errorf("%q: %v, want a usage error", in, err)
63+ }
64+ }
65+}
66+
67+// Bad flags and actions exit 2 with the usage line, before connecting.
68+func TestPointerCommandUsage(t *testing.T) {
69+ t.Setenv("WAYLAND_DISPLAY", "")
70+ for _, args := range [][]string{
71+ {"pointer"},
72+ {"pointer", "fly"},
73+ {"pointer", "--drag-steps", "0", "click"},
74+ {"pointer", "--click-duration", "-1s", "click"},
75+ {"pointer", "--bogus", "click"},
76+ } {
77+ code, _, stderr := execute(t, args...)
78+ if code != 2 || !strings.Contains(stderr, "usage: neferwl pointer") {
79+ t.Errorf("%v: code %d stderr %q", args, code, stderr)
80+ }
81+ }
82+ code, stdout, _ := execute(t, "pointer", "-h")
83+ if code != 0 || !strings.Contains(stdout, "drag DX DY [BTN]") || !strings.Contains(stdout, "-drag-step-delay") {
84+ t.Fatalf("help %d %q", code, stdout)
85+ }
86+ if code, _, stderr := execute(t, "pointer", "click"); code != 1 || !strings.Contains(stderr, "WAYLAND_DISPLAY") {
87+ t.Fatalf("no display: %d %q", code, stderr)
88+ }
89+}
A internal/cli/pointer_wayland.go +233 -0
@@ -0,0 +1,233 @@
1+package cli
2+
3+import (
4+ "context"
5+ "errors"
6+ "fmt"
7+ "math"
8+ "os"
9+ "time"
10+
11+ "bnema.dev/wlturbo"
12+ clientcore "github.com/bnema/go-wayland-bindings/client/wayland"
13+ clientvp "github.com/bnema/go-wayland-bindings/client/wlrvirtualpointer"
14+)
15+
16+// pointerSession is a connected virtual pointer.
17+type pointerSession struct {
18+ c *wlturbo.Display
19+ vp *clientvp.VirtualPointer
20+ start time.Time
21+}
22+
23+// dialPointer connects to the session display, resolved as for `neferwl
24+// state` ($WAYLAND_DISPLAY in $XDG_RUNTIME_DIR), and creates a virtual
25+// pointer, bound to the named output when output is set.
26+func dialPointer(output string) (*pointerSession, error) {
27+ if os.Getenv("XDG_RUNTIME_DIR") == "" || os.Getenv("WAYLAND_DISPLAY") == "" {
28+ return nil, errors.New("XDG_RUNTIME_DIR and WAYLAND_DISPLAY must be set")
29+ }
30+ c, err := wlturbo.Connect("")
31+ if err != nil {
32+ return nil, err
33+ }
34+ fail := func(err error) (*pointerSession, error) {
35+ _ = c.Close()
36+ return nil, err
37+ }
38+ if err := c.Roundtrip(); err != nil {
39+ return fail(err)
40+ }
41+ reg := c.Registry()
42+ manager := clientvp.NewVirtualPointerManager(c.Context())
43+ version, err := reg.BindNegotiated(clientvp.VirtualPointerManagerInterface, 2, manager)
44+ if err != nil {
45+ return fail(fmt.Errorf("the compositor offers no virtual pointer: %w", err))
46+ }
47+ seat := clientcore.NewSeat(c.Context())
48+ if _, err := reg.BindNegotiated(clientcore.SeatInterface, 1, seat); err != nil {
49+ return fail(err)
50+ }
51+ var vp *clientvp.VirtualPointer
52+ if output == "" {
53+ vp, err = manager.CreateVirtualPointer(seat)
54+ } else {
55+ if version < 2 {
56+ return fail(errors.New("--output needs zwlr_virtual_pointer_manager_v1 version 2"))
57+ }
58+ var out *clientcore.Output
59+ if out, err = findOutput(c, output); err != nil {
60+ return fail(err)
61+ }
62+ vp, err = manager.CreateVirtualPointerWithOutput(seat, out)
63+ }
64+ if err != nil {
65+ return fail(err)
66+ }
67+ return &pointerSession{c: c, vp: vp, start: time.Now()}, nil
68+}
69+
70+// findOutput binds every wl_output (v4, for names) and returns the named one.
71+func findOutput(c *wlturbo.Display, name string) (*clientcore.Output, error) {
72+ var found *clientcore.Output
73+ var names []string
74+ for gname, g := range c.Registry().GetGlobals() {
75+ if g.Interface != clientcore.OutputInterface || g.Version < 4 {
76+ continue
77+ }
78+ o := clientcore.NewOutput(c.Context())
79+ if err := c.Registry().Bind(gname, g.Interface, 4, o); err != nil {
80+ return nil, err
81+ }
82+ o.OnName(func(n string) {
83+ names = append(names, n)
84+ if n == name {
85+ found = o
86+ }
87+ })
88+ }
89+ if err := c.Roundtrip(); err != nil {
90+ return nil, err
91+ }
92+ if found == nil {
93+ return nil, usagef("no output %q (outputs: %v)", name, names)
94+ }
95+ return found, nil
96+}
97+
98+func (p *pointerSession) now() uint32 { return uint32(time.Since(p.start).Milliseconds()) }
99+
100+func fixed(v float64) wlturbo.Fixed { return wlturbo.NewFixed(v) }
101+
102+func (p *pointerSession) frame() error { return p.vp.Frame() }
103+
104+func (p *pointerSession) button(b uint32, down bool) error {
105+ state := uint32(0)
106+ if down {
107+ state = 1
108+ }
109+ if err := p.vp.Button(p.now(), b, state); err != nil {
110+ return err
111+ }
112+ return p.frame()
113+}
114+
115+func (p *pointerSession) move(dx, dy float64) error {
116+ if err := p.vp.Motion(p.now(), fixed(dx), fixed(dy)); err != nil {
117+ return err
118+ }
119+ return p.frame()
120+}
121+
122+// sync waits until the compositor has handled every request so far.
123+func (p *pointerSession) sync() error { return p.c.Roundtrip() }
124+
125+// sleep waits d, or until ctx ends.
126+func sleep(ctx context.Context, d time.Duration) error {
127+ if d <= 0 {
128+ return ctx.Err()
129+ }
130+ t := time.NewTimer(d)
131+ defer t.Stop()
132+ select {
133+ case <-ctx.Done():
134+ return ctx.Err()
135+ case <-t.C:
136+ return nil
137+ }
138+}
139+
140+const absExtent = 1 << 16
141+
142+func (p *pointerSession) run(ctx context.Context, o pointerOptions, a pointerAction) error {
143+ switch a.kind {
144+ case "abs":
145+ if err := p.vp.MotionAbsolute(p.now(), uint32(math.Round(a.x*absExtent)), uint32(math.Round(a.y*absExtent)), absExtent, absExtent); err != nil {
146+ return err
147+ }
148+ return p.frame()
149+ case "move":
150+ return p.move(a.x, a.y)
151+ case "press":
152+ return p.button(a.button, true)
153+ case "release":
154+ return p.button(a.button, false)
155+ case "click", "hold":
156+ d := o.clickDuration
157+ if a.kind == "hold" {
158+ d = o.holdDuration
159+ if a.duration > 0 {
160+ d = a.duration
161+ }
162+ }
163+ if err := p.button(a.button, true); err != nil {
164+ return err
165+ }
166+ if err := p.sync(); err != nil {
167+ return err
168+ }
169+ if err := sleep(ctx, d); err != nil {
170+ return err
171+ }
172+ return p.button(a.button, false)
173+ case "drag":
174+ if err := p.button(a.button, true); err != nil {
175+ return err
176+ }
177+ sx, sy := a.x/float64(o.dragSteps), a.y/float64(o.dragSteps)
178+ for i := 0; i < o.dragSteps; i++ {
179+ if err := p.sync(); err != nil {
180+ return err
181+ }
182+ if err := sleep(ctx, o.dragStepDelay); err != nil {
183+ return err
184+ }
185+ if err := p.move(sx, sy); err != nil {
186+ return err
187+ }
188+ }
189+ if err := p.sync(); err != nil {
190+ return err
191+ }
192+ return p.button(a.button, false)
193+ case "scroll":
194+ if err := p.vp.AxisSource(uint32(clientcore.AXIS_SOURCE_WHEEL)); err != nil {
195+ return err
196+ }
197+ n := a.steps
198+ if err := p.vp.AxisDiscrete(p.now(), a.axis, fixed(15*float64(n)), int32(n)); err != nil {
199+ return err
200+ }
201+ return p.frame()
202+ }
203+ return fmt.Errorf("unknown action %q", a.kind)
204+}
205+
206+// runPointer runs actions in order. Buttons still pressed at the end are
207+// released by the compositor when the virtual pointer goes away.
208+func runPointer(ctx context.Context, o pointerOptions, actions []pointerAction) (err error) {
209+ p, err := dialPointer(o.output)
210+ if err != nil {
211+ return err
212+ }
213+ defer func() {
214+ // Destroying the pointer releases held buttons (protocol release on
215+ // disconnect); the roundtrip makes sure the compositor saw it all.
216+ derr := p.vp.Destroy()
217+ if derr == nil {
218+ derr = p.sync()
219+ }
220+ if cerr := p.c.Close(); derr == nil {
221+ derr = cerr
222+ }
223+ if err == nil {
224+ err = derr
225+ }
226+ }()
227+ for _, a := range actions {
228+ if err := p.run(ctx, o, a); err != nil {
229+ return fmt.Errorf("%s: %w", a, err)
230+ }
231+ }
232+ return p.sync()
233+}
@@ -0,0 +1,233 @@
1+package cli
2+
3+import (
4+ "context"
5+ "errors"
6+ "fmt"
7+ "math"
8+ "os"
9+ "time"
10+
11+ "bnema.dev/wlturbo"
12+ clientcore "github.com/bnema/go-wayland-bindings/client/wayland"
13+ clientvp "github.com/bnema/go-wayland-bindings/client/wlrvirtualpointer"
14+)
15+
16+// pointerSession is a connected virtual pointer.
17+type pointerSession struct {
18+ c *wlturbo.Display
19+ vp *clientvp.VirtualPointer
20+ start time.Time
21+}
22+
23+// dialPointer connects to the session display, resolved as for `neferwl
24+// state` ($WAYLAND_DISPLAY in $XDG_RUNTIME_DIR), and creates a virtual
25+// pointer, bound to the named output when output is set.
26+func dialPointer(output string) (*pointerSession, error) {
27+ if os.Getenv("XDG_RUNTIME_DIR") == "" || os.Getenv("WAYLAND_DISPLAY") == "" {
28+ return nil, errors.New("XDG_RUNTIME_DIR and WAYLAND_DISPLAY must be set")
29+ }
30+ c, err := wlturbo.Connect("")
31+ if err != nil {
32+ return nil, err
33+ }
34+ fail := func(err error) (*pointerSession, error) {
35+ _ = c.Close()
36+ return nil, err
37+ }
38+ if err := c.Roundtrip(); err != nil {
39+ return fail(err)
40+ }
41+ reg := c.Registry()
42+ manager := clientvp.NewVirtualPointerManager(c.Context())
43+ version, err := reg.BindNegotiated(clientvp.VirtualPointerManagerInterface, 2, manager)
44+ if err != nil {
45+ return fail(fmt.Errorf("the compositor offers no virtual pointer: %w", err))
46+ }
47+ seat := clientcore.NewSeat(c.Context())
48+ if _, err := reg.BindNegotiated(clientcore.SeatInterface, 1, seat); err != nil {
49+ return fail(err)
50+ }
51+ var vp *clientvp.VirtualPointer
52+ if output == "" {
53+ vp, err = manager.CreateVirtualPointer(seat)
54+ } else {
55+ if version < 2 {
56+ return fail(errors.New("--output needs zwlr_virtual_pointer_manager_v1 version 2"))
57+ }
58+ var out *clientcore.Output
59+ if out, err = findOutput(c, output); err != nil {
60+ return fail(err)
61+ }
62+ vp, err = manager.CreateVirtualPointerWithOutput(seat, out)
63+ }
64+ if err != nil {
65+ return fail(err)
66+ }
67+ return &pointerSession{c: c, vp: vp, start: time.Now()}, nil
68+}
69+
70+// findOutput binds every wl_output (v4, for names) and returns the named one.
71+func findOutput(c *wlturbo.Display, name string) (*clientcore.Output, error) {
72+ var found *clientcore.Output
73+ var names []string
74+ for gname, g := range c.Registry().GetGlobals() {
75+ if g.Interface != clientcore.OutputInterface || g.Version < 4 {
76+ continue
77+ }
78+ o := clientcore.NewOutput(c.Context())
79+ if err := c.Registry().Bind(gname, g.Interface, 4, o); err != nil {
80+ return nil, err
81+ }
82+ o.OnName(func(n string) {
83+ names = append(names, n)
84+ if n == name {
85+ found = o
86+ }
87+ })
88+ }
89+ if err := c.Roundtrip(); err != nil {
90+ return nil, err
91+ }
92+ if found == nil {
93+ return nil, usagef("no output %q (outputs: %v)", name, names)
94+ }
95+ return found, nil
96+}
97+
98+func (p *pointerSession) now() uint32 { return uint32(time.Since(p.start).Milliseconds()) }
99+
100+func fixed(v float64) wlturbo.Fixed { return wlturbo.NewFixed(v) }
101+
102+func (p *pointerSession) frame() error { return p.vp.Frame() }
103+
104+func (p *pointerSession) button(b uint32, down bool) error {
105+ state := uint32(0)
106+ if down {
107+ state = 1
108+ }
109+ if err := p.vp.Button(p.now(), b, state); err != nil {
110+ return err
111+ }
112+ return p.frame()
113+}
114+
115+func (p *pointerSession) move(dx, dy float64) error {
116+ if err := p.vp.Motion(p.now(), fixed(dx), fixed(dy)); err != nil {
117+ return err
118+ }
119+ return p.frame()
120+}
121+
122+// sync waits until the compositor has handled every request so far.
123+func (p *pointerSession) sync() error { return p.c.Roundtrip() }
124+
125+// sleep waits d, or until ctx ends.
126+func sleep(ctx context.Context, d time.Duration) error {
127+ if d <= 0 {
128+ return ctx.Err()
129+ }
130+ t := time.NewTimer(d)
131+ defer t.Stop()
132+ select {
133+ case <-ctx.Done():
134+ return ctx.Err()
135+ case <-t.C:
136+ return nil
137+ }
138+}
139+
140+const absExtent = 1 << 16
141+
142+func (p *pointerSession) run(ctx context.Context, o pointerOptions, a pointerAction) error {
143+ switch a.kind {
144+ case "abs":
145+ if err := p.vp.MotionAbsolute(p.now(), uint32(math.Round(a.x*absExtent)), uint32(math.Round(a.y*absExtent)), absExtent, absExtent); err != nil {
146+ return err
147+ }
148+ return p.frame()
149+ case "move":
150+ return p.move(a.x, a.y)
151+ case "press":
152+ return p.button(a.button, true)
153+ case "release":
154+ return p.button(a.button, false)
155+ case "click", "hold":
156+ d := o.clickDuration
157+ if a.kind == "hold" {
158+ d = o.holdDuration
159+ if a.duration > 0 {
160+ d = a.duration
161+ }
162+ }
163+ if err := p.button(a.button, true); err != nil {
164+ return err
165+ }
166+ if err := p.sync(); err != nil {
167+ return err
168+ }
169+ if err := sleep(ctx, d); err != nil {
170+ return err
171+ }
172+ return p.button(a.button, false)
173+ case "drag":
174+ if err := p.button(a.button, true); err != nil {
175+ return err
176+ }
177+ sx, sy := a.x/float64(o.dragSteps), a.y/float64(o.dragSteps)
178+ for i := 0; i < o.dragSteps; i++ {
179+ if err := p.sync(); err != nil {
180+ return err
181+ }
182+ if err := sleep(ctx, o.dragStepDelay); err != nil {
183+ return err
184+ }
185+ if err := p.move(sx, sy); err != nil {
186+ return err
187+ }
188+ }
189+ if err := p.sync(); err != nil {
190+ return err
191+ }
192+ return p.button(a.button, false)
193+ case "scroll":
194+ if err := p.vp.AxisSource(uint32(clientcore.AXIS_SOURCE_WHEEL)); err != nil {
195+ return err
196+ }
197+ n := a.steps
198+ if err := p.vp.AxisDiscrete(p.now(), a.axis, fixed(15*float64(n)), int32(n)); err != nil {
199+ return err
200+ }
201+ return p.frame()
202+ }
203+ return fmt.Errorf("unknown action %q", a.kind)
204+}
205+
206+// runPointer runs actions in order. Buttons still pressed at the end are
207+// released by the compositor when the virtual pointer goes away.
208+func runPointer(ctx context.Context, o pointerOptions, actions []pointerAction) (err error) {
209+ p, err := dialPointer(o.output)
210+ if err != nil {
211+ return err
212+ }
213+ defer func() {
214+ // Destroying the pointer releases held buttons (protocol release on
215+ // disconnect); the roundtrip makes sure the compositor saw it all.
216+ derr := p.vp.Destroy()
217+ if derr == nil {
218+ derr = p.sync()
219+ }
220+ if cerr := p.c.Close(); derr == nil {
221+ derr = cerr
222+ }
223+ if err == nil {
224+ err = derr
225+ }
226+ }()
227+ for _, a := range actions {
228+ if err := p.run(ctx, o, a); err != nil {
229+ return fmt.Errorf("%s: %w", a, err)
230+ }
231+ }
232+ return p.sync()
233+}
M internal/core/client.go +6 -0
@@ -2,6 +2,7 @@ package core
22
33 import (
44 "context"
5+ "errors"
56
67 "github.com/bnema/neferwl/internal/ports"
78 )
@@ -128,6 +129,11 @@ func (c *Core) clientEvent(ctx context.Context, ev ports.ClientEvent) (publish,
128129 if c.warpPointer(ctx, v) != nil {
129130 return false, true
130131 }
132+ case ports.VirtualPointerInput:
133+ if err := c.virtualPointer(ctx, v); err != nil && !errors.Is(err, errSecurityChanged) {
134+ return false, true
135+ }
136+ return false, false
131137 case ports.WindowMoveRequest:
132138 if c.clientDrag(ctx, v) != nil {
133139 return false, true
@@ -2,6 +2,7 @@ package core
2 2
3 import (3 import (
4 "context"4 "context"
5+ "errors"
5 6
6 "github.com/bnema/neferwl/internal/ports"7 "github.com/bnema/neferwl/internal/ports"
7 )8 )
@@ -128,6 +129,11 @@ func (c *Core) clientEvent(ctx context.Context, ev ports.ClientEvent) (publish,
128 if c.warpPointer(ctx, v) != nil {129 if c.warpPointer(ctx, v) != nil {
129 return false, true130 return false, true
130 }131 }
132+ case ports.VirtualPointerInput:
133+ if err := c.virtualPointer(ctx, v); err != nil && !errors.Is(err, errSecurityChanged) {
134+ return false, true
135+ }
136+ return false, false
131 case ports.WindowMoveRequest:137 case ports.WindowMoveRequest:
132 if c.clientDrag(ctx, v) != nil {138 if c.clientDrag(ctx, v) != nil {
133 return false, true139 return false, true
M internal/core/core.go +1 -0
@@ -126,6 +126,7 @@ type Core struct {
126126 focusByPointer bool // set while focusPointer publishes, so syncFocus does not arm the hold
127127 grab WindowID
128128 buttons map[uint32]bool
129+ virtualButtons map[virtualButton]bool // admitted virtual presses, never global button ownership
129130 placed map[WindowID]Rect
130131 // drag is the pointer drag in progress (drag.go); swallow holds the
131132 // buttons whose release no client must see. mods are the modifiers
@@ -126,6 +126,7 @@ type Core struct {
126 focusByPointer bool // set while focusPointer publishes, so syncFocus does not arm the hold126 focusByPointer bool // set while focusPointer publishes, so syncFocus does not arm the hold
127 grab WindowID127 grab WindowID
128 buttons map[uint32]bool128 buttons map[uint32]bool
129+ virtualButtons map[virtualButton]bool // admitted virtual presses, never global button ownership
129 placed map[WindowID]Rect130 placed map[WindowID]Rect
130 // drag is the pointer drag in progress (drag.go); swallow holds the131 // drag is the pointer drag in progress (drag.go); swallow holds the
131 // buttons whose release no client must see. mods are the modifiers132 // buttons whose release no client must see. mods are the modifiers
M internal/core/security.go +2 -1
@@ -30,6 +30,7 @@ func (c *Core) syncSecurity() bool {
3030 return false
3131 }
3232 c.security = state
33+ clear(c.virtualButtons)
3334 c.osd.shown, c.osd.pinned = false, false
3435 c.osdReset()
3536 c.lock = lockState{}
@@ -109,7 +110,7 @@ func (c *Core) blockProtected(ev ports.ClientEvent) bool {
109110
110111 func blockedProtectedEvent(ev ports.ClientEvent) bool {
111112 switch ev.(type) {
112- case ports.WindowActivate, ports.WorkspaceActivate, ports.PointerWarp, ports.PointerConstrained,
113+ case ports.WindowActivate, ports.WorkspaceActivate, ports.PointerWarp, ports.PointerConstrained, ports.VirtualPointerInput,
113114 ports.PopupRequest, ports.ShortcutsInhibit, ports.WindowFullscreenRequest,
114115 ports.CaptureSessionOpen, ports.CaptureFrameTaken, ports.CaptureExclusionBegin, ports.CaptureExclusionLayer,
115116 ports.WindowMoveRequest:
@@ -30,6 +30,7 @@ func (c *Core) syncSecurity() bool {
30 return false30 return false
31 }31 }
32 c.security = state32 c.security = state
33+ clear(c.virtualButtons)
33 c.osd.shown, c.osd.pinned = false, false34 c.osd.shown, c.osd.pinned = false, false
34 c.osdReset()35 c.osdReset()
35 c.lock = lockState{}36 c.lock = lockState{}
@@ -109,7 +110,7 @@ func (c *Core) blockProtected(ev ports.ClientEvent) bool {
109 110
110 func blockedProtectedEvent(ev ports.ClientEvent) bool {111 func blockedProtectedEvent(ev ports.ClientEvent) bool {
111 switch ev.(type) {112 switch ev.(type) {
112- case ports.WindowActivate, ports.WorkspaceActivate, ports.PointerWarp, ports.PointerConstrained,113+ case ports.WindowActivate, ports.WorkspaceActivate, ports.PointerWarp, ports.PointerConstrained, ports.VirtualPointerInput,
113 ports.PopupRequest, ports.ShortcutsInhibit, ports.WindowFullscreenRequest,114 ports.PopupRequest, ports.ShortcutsInhibit, ports.WindowFullscreenRequest,
114 ports.CaptureSessionOpen, ports.CaptureFrameTaken, ports.CaptureExclusionBegin, ports.CaptureExclusionLayer,115 ports.CaptureSessionOpen, ports.CaptureFrameTaken, ports.CaptureExclusionBegin, ports.CaptureExclusionLayer,
115 ports.WindowMoveRequest:116 ports.WindowMoveRequest:
A internal/core/virtualpointer.go +125 -0
@@ -0,0 +1,125 @@
1+package core
2+
3+import (
4+ "context"
5+
6+ "github.com/bnema/neferwl/internal/domain"
7+ "github.com/bnema/neferwl/internal/ports"
8+)
9+
10+// virtualButton identifies a press admitted from one virtual device.
11+type virtualButton struct {
12+ device domain.InputDeviceID
13+ button uint32
14+}
15+
16+// virtualPointer routes one zwlr_virtual_pointer_v1 event as real pointer
17+// input: core places the pointer (relative from its cursor, absolute in an
18+// output or the whole layout), then the usual handlers decide hover, focus,
19+// grabs and delivery. A protected session or a stale epoch drops it (the
20+// virtual pointer never reaches a lock surface), and a release without a
21+// press is ignored like one from a device. Input learns the new position
22+// by a warp so the visible cursor follows.
23+func (c *Core) virtualPointer(ctx context.Context, v ports.VirtualPointerInput) error {
24+ if c.security.Protected || v.State.Protected || c.opts.Security != nil && v.State != c.security {
25+ return nil
26+ }
27+ // Keep every event associated with its bound output, not just absolute
28+ // motion. A paired release must still unwind held buttons and grabs if
29+ // the output disappeared after the press.
30+ r, available := c.virtualArea(v.Output)
31+ if v.Output != "" && !available {
32+ e, release := v.Event.(ports.PointerButton)
33+ if !release || e.Pressed || !c.virtualButtons[virtualButton{e.Device, e.Button}] {
34+ return nil
35+ }
36+ }
37+ if err := c.userActivity(ctx); err != nil {
38+ return err
39+ }
40+ c.inputActive = true
41+ defer func() { c.inputActive = false }()
42+ switch e := v.Event.(type) {
43+ case ports.PointerMotion:
44+ if v.Absolute {
45+ if !available {
46+ return nil
47+ }
48+ // FX, FY may reach 1 (x == extent): clamp onto the last pixel.
49+ fx, fy := min(max(v.FX, 0), 1), min(max(v.FY, 0), 1)
50+ e.X = min(float64(r.X)+fx*float64(r.W), float64(r.X+r.W)-1)
51+ e.Y = min(float64(r.Y)+fy*float64(r.H), float64(r.Y+r.H)-1)
52+ e.DX, e.DY, e.UnaccelDX, e.UnaccelDY = 0, 0, 0, 0
53+ } else {
54+ e.X, e.Y = c.cursorX+e.DX, c.cursorY+e.DY
55+ }
56+ if c.inputBindings {
57+ if err := c.activateInput(ctx, e); err != nil {
58+ return err
59+ }
60+ }
61+ if err := c.pointerMotion(ctx, e); err != nil {
62+ return err
63+ }
64+ if c.ch.Constraints != nil {
65+ g := c.constraint
66+ g.X, g.Y, g.Warp = c.cursorX, c.cursorY, true
67+ latest(c.ch.Constraints, g)
68+ }
69+ case ports.PointerButton:
70+ // Only evdev button codes (BTN_MISC..BTN_GEAR_UP): c.buttons stays
71+ // bounded whatever virtual pointers send.
72+ if e.Button < 0x100 || e.Button > 0x151 {
73+ return nil
74+ }
75+ key := virtualButton{e.Device, e.Button}
76+ if !e.Pressed {
77+ admitted := c.virtualButtons[key]
78+ delete(c.virtualButtons, key)
79+ if !admitted || !c.buttons[e.Button] {
80+ return nil
81+ }
82+ }
83+ if err := c.pointerButton(ctx, e); err != nil {
84+ return err
85+ }
86+ if e.Pressed && c.buttons[e.Button] {
87+ if c.virtualButtons == nil {
88+ c.virtualButtons = make(map[virtualButton]bool)
89+ }
90+ c.virtualButtons[key] = true
91+ }
92+ return nil
93+ case ports.PointerAxis:
94+ return c.pointerAxis(ctx, e)
95+ }
96+ return nil
97+}
98+
99+// virtualArea is the rectangle absolute virtual motion maps into: the named
100+// output if reachable, or the bounds of every reachable output when unbound.
101+// An unavailable named output must never fall back to the live desktop.
102+func (c *Core) virtualArea(output string) (Rect, bool) {
103+ if output != "" {
104+ if i := c.screenIndex(output); i >= 0 && !c.screens[i].isolated {
105+ return c.screens[i].rect(), true
106+ }
107+ return Rect{}, false
108+ }
109+ var r Rect
110+ found := false
111+ for _, s := range c.screens {
112+ if s.name() == "" || s.isolated {
113+ continue
114+ }
115+ sr := s.rect()
116+ if !found {
117+ r, found = sr, true
118+ continue
119+ }
120+ x0, y0 := min(r.X, sr.X), min(r.Y, sr.Y)
121+ x1, y1 := max(r.X+r.W, sr.X+sr.W), max(r.Y+r.H, sr.Y+sr.H)
122+ r = Rect{X: x0, Y: y0, W: x1 - x0, H: y1 - y0}
123+ }
124+ return r, found
125+}
@@ -0,0 +1,125 @@
1+package core
2+
3+import (
4+ "context"
5+
6+ "github.com/bnema/neferwl/internal/domain"
7+ "github.com/bnema/neferwl/internal/ports"
8+)
9+
10+// virtualButton identifies a press admitted from one virtual device.
11+type virtualButton struct {
12+ device domain.InputDeviceID
13+ button uint32
14+}
15+
16+// virtualPointer routes one zwlr_virtual_pointer_v1 event as real pointer
17+// input: core places the pointer (relative from its cursor, absolute in an
18+// output or the whole layout), then the usual handlers decide hover, focus,
19+// grabs and delivery. A protected session or a stale epoch drops it (the
20+// virtual pointer never reaches a lock surface), and a release without a
21+// press is ignored like one from a device. Input learns the new position
22+// by a warp so the visible cursor follows.
23+func (c *Core) virtualPointer(ctx context.Context, v ports.VirtualPointerInput) error {
24+ if c.security.Protected || v.State.Protected || c.opts.Security != nil && v.State != c.security {
25+ return nil
26+ }
27+ // Keep every event associated with its bound output, not just absolute
28+ // motion. A paired release must still unwind held buttons and grabs if
29+ // the output disappeared after the press.
30+ r, available := c.virtualArea(v.Output)
31+ if v.Output != "" && !available {
32+ e, release := v.Event.(ports.PointerButton)
33+ if !release || e.Pressed || !c.virtualButtons[virtualButton{e.Device, e.Button}] {
34+ return nil
35+ }
36+ }
37+ if err := c.userActivity(ctx); err != nil {
38+ return err
39+ }
40+ c.inputActive = true
41+ defer func() { c.inputActive = false }()
42+ switch e := v.Event.(type) {
43+ case ports.PointerMotion:
44+ if v.Absolute {
45+ if !available {
46+ return nil
47+ }
48+ // FX, FY may reach 1 (x == extent): clamp onto the last pixel.
49+ fx, fy := min(max(v.FX, 0), 1), min(max(v.FY, 0), 1)
50+ e.X = min(float64(r.X)+fx*float64(r.W), float64(r.X+r.W)-1)
51+ e.Y = min(float64(r.Y)+fy*float64(r.H), float64(r.Y+r.H)-1)
52+ e.DX, e.DY, e.UnaccelDX, e.UnaccelDY = 0, 0, 0, 0
53+ } else {
54+ e.X, e.Y = c.cursorX+e.DX, c.cursorY+e.DY
55+ }
56+ if c.inputBindings {
57+ if err := c.activateInput(ctx, e); err != nil {
58+ return err
59+ }
60+ }
61+ if err := c.pointerMotion(ctx, e); err != nil {
62+ return err
63+ }
64+ if c.ch.Constraints != nil {
65+ g := c.constraint
66+ g.X, g.Y, g.Warp = c.cursorX, c.cursorY, true
67+ latest(c.ch.Constraints, g)
68+ }
69+ case ports.PointerButton:
70+ // Only evdev button codes (BTN_MISC..BTN_GEAR_UP): c.buttons stays
71+ // bounded whatever virtual pointers send.
72+ if e.Button < 0x100 || e.Button > 0x151 {
73+ return nil
74+ }
75+ key := virtualButton{e.Device, e.Button}
76+ if !e.Pressed {
77+ admitted := c.virtualButtons[key]
78+ delete(c.virtualButtons, key)
79+ if !admitted || !c.buttons[e.Button] {
80+ return nil
81+ }
82+ }
83+ if err := c.pointerButton(ctx, e); err != nil {
84+ return err
85+ }
86+ if e.Pressed && c.buttons[e.Button] {
87+ if c.virtualButtons == nil {
88+ c.virtualButtons = make(map[virtualButton]bool)
89+ }
90+ c.virtualButtons[key] = true
91+ }
92+ return nil
93+ case ports.PointerAxis:
94+ return c.pointerAxis(ctx, e)
95+ }
96+ return nil
97+}
98+
99+// virtualArea is the rectangle absolute virtual motion maps into: the named
100+// output if reachable, or the bounds of every reachable output when unbound.
101+// An unavailable named output must never fall back to the live desktop.
102+func (c *Core) virtualArea(output string) (Rect, bool) {
103+ if output != "" {
104+ if i := c.screenIndex(output); i >= 0 && !c.screens[i].isolated {
105+ return c.screens[i].rect(), true
106+ }
107+ return Rect{}, false
108+ }
109+ var r Rect
110+ found := false
111+ for _, s := range c.screens {
112+ if s.name() == "" || s.isolated {
113+ continue
114+ }
115+ sr := s.rect()
116+ if !found {
117+ r, found = sr, true
118+ continue
119+ }
120+ x0, y0 := min(r.X, sr.X), min(r.Y, sr.Y)
121+ x1, y1 := max(r.X+r.W, sr.X+sr.W), max(r.Y+r.H, sr.Y+sr.H)
122+ r = Rect{X: x0, Y: y0, W: x1 - x0, H: y1 - y0}
123+ }
124+ return r, found
125+}
A internal/core/virtualpointer_test.go +143 -0
A internal/core/virtualpointer_unavailable_test.go +109 -0
M internal/ports/pointer_events.go +19 -0

Comments00 unresolved

  1. bnema will resolve #88 feat(wayland): support wlr-virtual-pointer ·

no comments yet.

Keyboard shortcuts

Single-key shortcuts are on. ctrl k and esc work whatever this says.

ctrl k :
go to a section, branch, tag, file or repository
?
this list
j k
move the row cursor down / up
enter
open the row under the cursor; on Files, a directory opens or closes in place
↑ ↓ ← → home end
in the file tree (focus on a row): previous / next row, close / open a directory or go to its parent / first entry, first / last row. They never open a file
l h
on Files: open a directory or a file / close a directory or go up to its parent, then to the parent page. On pages with a side panel: into the panel (on a merge request, its files) / back to the content; there j k move between its controls
g g G
first / last row
/
filter the list; on Files, find a file anywhere in the repository (esc clears, enter opens the best match)
b
switch branch or tag
g s g f g c g b g m g i g r g a
tabs: summary, files, commits, branches & tags, merge requests, issues, releases, actions (when available)
g h
home
w
wide mode: use the whole width of the screen, or not (remembered)
y
copy a permalink pinned to the commit; in a file, click a line number to select it, shift-click for a range
d u x
diff: split / unified / collapse or expand all
e E
diff: the whole file in view / every file, with the lines the hunks hide
n
merge request: next note in the diff
l
merge request: the list of files (j k move, enter opens, esc back)
M
merge request: the merge menu; there 1–3 pick the mode, D deletes the branch, enter merges
R
merge request: mark the head read
esc
close a dialog, leave a field

Branches 33

Tags 15

↑↓ moveenter openesc close