Skip to content

RediWM is a Wayland compositor written in Zig (OpenGL ES) that includes everything in one process

Input: wp_pointer_warp_v1

oxydizer406a0030a673
diff --git a/build/protocols.zig b/build/protocols.zig
index cce78b6..46ed3ed 100644
--- a/build/protocols.zig
+++ b/build/protocols.zig
@@ -34,6 +34,7 @@ pub fn module(b: *std.Build) *std.Build.Module {
scanner.addSystemProtocol("unstable/xdg-output/xdg-output-unstable-v1.xml");
scanner.addSystemProtocol("unstable/pointer-constraints/pointer-constraints-unstable-v1.xml");
scanner.addSystemProtocol("unstable/relative-pointer/relative-pointer-unstable-v1.xml");
+ scanner.addSystemProtocol("staging/pointer-warp/pointer-warp-v1.xml");
scanner.addSystemProtocol("staging/ext-session-lock/ext-session-lock-v1.xml");
scanner.addCustomProtocol(b.path("protocol/wlr-output-power-management-unstable-v1.xml"));
@@ -63,6 +64,7 @@ pub fn module(b: *std.Build) *std.Build.Module {
scanner.generate("zxdg_output_manager_v1", 3);
scanner.generate("zwp_pointer_constraints_v1", 1);
scanner.generate("zwp_relative_pointer_manager_v1", 1);
+ scanner.generate("wp_pointer_warp_v1", 1);
scanner.generate("ext_session_lock_manager_v1", 1);
scanner.generate("zwlr_output_power_manager_v1", 1);
diff --git a/src/Input.zig b/src/Input.zig
index 84d3da1..eebbf56 100644
--- a/src/Input.zig
+++ b/src/Input.zig
@@ -24,6 +24,7 @@ const CursorSerial = @import("input/cursor_serial.zig");
const ClientWheel = @import("input/client_wheel.zig");
const WheelDebounce = @import("input/wheel_debounce.zig");
const PointerConstraints = @import("input/pointer_constraints.zig");
+const PointerWarp = @import("input/pointer_warp.zig");
const ShortcutsInhibit = @import("input/shortcuts_inhibit.zig");
const Touch = @import("input/touch.zig");
const Tablet = @import("input/tablet.zig");
@@ -134,6 +135,7 @@ cursor_mgr: *wlr.XcursorManager,
pointer_gestures: *wlr.PointerGesturesV1,
/// Relative motion and pointer lock/confinement for games.
pointer_constraints: PointerConstraints = undefined,
+pointer_warp: PointerWarp = undefined,
/// Compositor bindings handed to VMs and remote desktops.
shortcuts_inhibit: ShortcutsInhibit = undefined,
/// A file manager slides aside while a file is dragged out of it.
@@ -393,6 +395,8 @@ pub fn init(input: *Input, server: *Server) Server.CompositorError!void {
input.cursor.events.hold_end.add(&input.cursor_hold_end);
input.pointer_constraints.init(input) catch return error.PointerConstraintsCreateFailed;
errdefer input.pointer_constraints.deinit();
+ input.pointer_warp.init(input) catch return error.PointerWarpCreateFailed;
+ errdefer input.pointer_warp.deinit();
input.shortcuts_inhibit.init(input) catch return error.ShortcutsInhibitCreateFailed;
errdefer input.shortcuts_inhibit.deinit();
input.drag_dodge.init(input);
@@ -437,6 +441,7 @@ pub fn deinit(input: *Input) void {
input.start_drag.link.remove();
if (input.drag_icon_tree) |tree| tree.node.destroy();
input.pointer_constraints.deinit();
+ input.pointer_warp.deinit();
input.shortcuts_inhibit.deinit();
input.drag_dodge.deinit();
input.trash_drop.deinit();
diff --git a/src/Server.zig b/src/Server.zig
index e98504c..977cdd6 100644
--- a/src/Server.zig
+++ b/src/Server.zig
@@ -91,6 +91,7 @@ pub const CompositorError = error{
SocketCreateFailed,
PointerGesturesCreateFailed,
PointerConstraintsCreateFailed,
+ PointerWarpCreateFailed,
ShortcutsInhibitCreateFailed,
};
diff --git a/src/input/AGENTS.md b/src/input/AGENTS.md
index 3d7417d..2502f7f 100644
--- a/src/input/AGENTS.md
+++ b/src/input/AGENTS.md
@@ -39,6 +39,12 @@
focus; new compositor input modes go in `motionReachesClient` or
`candidate`. `sendDeactivated` destroys oneshots synchronously: clear
`active` first.
+* **Pointer warp** (`pointer_warp.zig`, wp_pointer_warp_v1; Wine's
+ SetCursorPos): honoured only while the target's surface tree has pointer
+ focus and `motionReachesClient`, with a seat serial for that client, inside
+ the surface; never through a lock, only within a confinement. It skips
+ `clientMotion` on purpose (a warp is no relative delta) but runs
+ `processCursorMotion`, the frame and `sync` like `warpCursor`.
* **The switcher owns the pointer** while it is up: opening it clears pointer
focus and shows the arrow (a client's hidden or text cursor would otherwise
stay, since no motion reaches it), unless a press or grab is in progress.
diff --git a/src/input/pointer_warp.zig b/src/input/pointer_warp.zig
new file mode 100644
index 0000000..ec668ac
--- /dev/null
+++ b/src/input/pointer_warp.zig
@@ -0,0 +1,100 @@
+//! wp_pointer_warp_v1: a client moves the pointer to a point on its own
+//! surface. Wine implements SetCursorPos with it (games recentring the cursor
+//! every frame, launchers placing it on a button); without it Wine falls
+//! back to a short pointer lock with a cursor hint, which the compositor
+//! cannot tell from a real lock.
+//!
+//! A warp is honoured only while the target surface's tree has pointer focus
+//! and motion reaches the client (the same states that keep pointer
+//! constraints off), with a serial this seat sent that client, to a point
+//! inside the surface. A locked pointer stays put (its cursor hint is the
+//! warp), a confined one only moves within its region. The client then gets
+//! ordinary absolute motion to the new point; relative-pointer clients get
+//! no delta, since the hand on the mouse did not move.
+const std = @import("std");
+const wl = @import("wayland").server.wl;
+const wp = @import("wayland").server.wp;
+const wlr = @import("wlroots");
+const Input = @import("../Input.zig");
+const VirtualInput = @import("virtual.zig").VirtualInput;
+const Self = @This();
+
+input: *Input,
+global: *wl.Global,
+
+pub fn init(self: *Self, input: *Input) !void {
+ self.* = .{
+ .input = input,
+ .global = try wl.Global.create(input.server.wl_server, wp.PointerWarpV1, 1, *Self, self, bind),
+ };
+}
+
+/// Bound managers outlive the global until their clients go; they carry no
+/// state, and no request is dispatched after the display stops.
+pub fn deinit(self: *Self) void {
+ self.global.destroy();
+}
+
+fn bind(client: *wl.Client, self: *Self, version: u32, id: u32) void {
+ const resource = wp.PointerWarpV1.create(client, version, id) catch {
+ client.postNoMemory();
+ return;
+ };
+ resource.setHandler(*Self, handleRequest, null, self);
+}
+
+fn handleRequest(resource: *wp.PointerWarpV1, request: wp.PointerWarpV1.Request, self: *Self) void {
+ switch (request) {
+ .destroy => resource.destroy(),
+ .warp_pointer => |args| {
+ const surface = wlr.Surface.fromWlSurface(args.surface);
+ self.warp(resource.getClient(), surface, args.pointer, args.x.toDouble(), args.y.toDouble(), args.serial);
+ },
+ }
+}
+
+fn warp(self: *Self, client: *wl.Client, surface: *wlr.Surface, pointer: *wl.Pointer, sx: f64, sy: f64, serial: u32) void {
+ const input = self.input;
+ const seat = input.seat;
+ if (surface.resource.getClient() != client) return;
+ const seat_client = wlr.Seat.Client.fromWlPointer(pointer) orelse return;
+ if (seat_client.seat != seat or seat_client.client != client) return;
+ if (!seat_client.validateEventSerial(serial)) return;
+ if (!(sx >= 0 and sy >= 0 and sx < @as(f64, @floatFromInt(surface.current.width)) and
+ sy < @as(f64, @floatFromInt(surface.current.height)))) return;
+ if (!input.motionReachesClient()) return;
+ const focused = seat.pointer_state.focused_surface orelse return;
+ if (focused.getRootSurface() != surface.getRootSurface()) return;
+ // The cursor/surface correspondence is kept for the focused surface;
+ // carry the point there through the shared root's coordinates.
+ const target = rootOffset(surface);
+ const origin = rootOffset(focused);
+ const fx = sx + target.x - origin.x;
+ const fy = sy + target.y - origin.y;
+ if (input.pointer_constraints.active) |constraint| switch (constraint.type) {
+ .locked => return,
+ .confined => {
+ const x = std.math.lossyCast(c_int, @floor(fx));
+ const y = std.math.lossyCast(c_int, @floor(fy));
+ if (constraint.surface != focused or !constraint.region.containsPoint(x, y, null)) return;
+ },
+ };
+ const point = input.focusedSurfaceToLayout(fx, fy);
+ if (!input.cursor.warp(null, point.x, point.y)) return;
+ input.processCursorMotion(VirtualInput.nowMs());
+ seat.pointerNotifyFrame();
+ input.pointer_constraints.sync();
+}
+
+/// Where `surface`'s origin sits in its root surface's coordinates.
+fn rootOffset(surface: *wlr.Surface) struct { x: f64, y: f64 } {
+ var x: i32 = 0;
+ var y: i32 = 0;
+ var current = surface;
+ while (wlr.Subsurface.tryFromWlrSurface(current)) |sub| {
+ x += sub.current.x;
+ y += sub.current.y;
+ current = sub.parent;
+ }
+ return .{ .x = @floatFromInt(x), .y = @floatFromInt(y) };
+}
diff --git a/tests/pointer_constraints.py b/tests/pointer_constraints.py
index 078db4f..7473e07 100644
--- a/tests/pointer_constraints.py
+++ b/tests/pointer_constraints.py
@@ -1,5 +1,5 @@
#!/usr/bin/env python3
-"""Relative pointer motion and pointer lock/confinement with real Wayland peers."""
+"""Relative pointer motion, pointer lock/confinement and pointer warps with real Wayland peers."""
import os
from pathlib import Path
import subprocess
@@ -17,6 +17,7 @@ def compile_client(tmp):
protocols = {
"pointer-constraints-unstable-v1": system / "unstable/pointer-constraints/pointer-constraints-unstable-v1.xml",
"relative-pointer-unstable-v1": system / "unstable/relative-pointer/relative-pointer-unstable-v1.xml",
+ "pointer-warp-v1": system / "staging/pointer-warp/pointer-warp-v1.xml",
}
sources = [tmp / "xdg-shell-protocol.c", tmp / "xdg-decoration-protocol.c"]
for name, path in protocols.items():
@@ -87,7 +88,7 @@ def run():
return tuple(float(v) for v in line.split()[1:])
game = Peer("rediwm.game")
- for interface in ("zwp_pointer_constraints_v1", "zwp_relative_pointer_manager_v1"):
+ for interface in ("zwp_pointer_constraints_v1", "zwp_relative_pointer_manager_v1", "wp_pointer_warp_v1"):
assert f"global {interface} 1\n" in game.text(), game.text()
other = Peer("rediwm.other")
focus("rediwm.game")
@@ -108,6 +109,29 @@ def run():
game.expect_count("relative 7.000 -3.000 7.000 -3.000\n", 1)
print("PASS: globals and relative motion")
+ # A warp moves the cursor to a point on the client's own surface
+ # and reports plain motion there, never a relative delta.
+ relatives = game.count("relative ")
+ game.send("warp main 30 40")
+ assert cursor() == (ox + 30, oy + 40), (cursor(), (ox, oy))
+ assert game.last_motion() == (30, 40), game.last_motion()
+ # Any surface of the focused tree: the child sits at (100,80).
+ game.send("warp child 10 5")
+ assert cursor() == (ox + 110, oy + 85), (cursor(), (ox, oy))
+ game.send("warp main 30 40")
+ assert cursor() == (ox + 30, oy + 40), cursor()
+ assert game.count("relative ") == relatives, "a warp sent relative motion"
+ for refused in ("warp main 50 50 1", "warp main -1 10", "warp main 10 100000"):
+ game.send(refused)
+ assert cursor() == (ox + 30, oy + 40), (refused, cursor())
+ # Only the client under the pointer may move it.
+ focus("rediwm.other")
+ other.send("warp main 20 20")
+ assert cursor() == (ox + 30, oy + 40), ("unfocused warp", cursor())
+ focus("rediwm.game")
+ move(ox + 20, oy + 20)
+ print("PASS: pointer warp to the focused surface tree; bad serial, outside and unfocused refused")
+
# A lock freezes the cursor and wl_pointer motion but keeps
# relative deltas flowing: this is mouse-look.
game.send("lock persistent")
@@ -118,6 +142,8 @@ def run():
game.expect_count("relative 40.000 25.000 40.000 25.000\n", 1)
assert cursor() == held, (cursor(), held)
assert game.count("motion ") == motions, "locked pointer still sent wl_pointer.motion"
+ game.send("warp main 30 40")
+ assert cursor() == held, ("warp moved a locked pointer", cursor(), held)
# Losing keyboard focus releases it; a persistent lock returns
# with focus while the pointer is still over the surface.
diff --git a/tests/pointer_constraints_client.c b/tests/pointer_constraints_client.c
index 5b6d08c..93b4d17 100644
--- a/tests/pointer_constraints_client.c
+++ b/tests/pointer_constraints_client.c
@@ -4,6 +4,7 @@
#undef main
#include "pointer-constraints-unstable-v1-client-protocol.h"
#include "relative-pointer-unstable-v1-client-protocol.h"
+#include "pointer-warp-v1-client-protocol.h"
#include <poll.h>
static struct zwp_pointer_constraints_v1 *constraints;
@@ -12,6 +13,16 @@ static struct zwp_relative_pointer_v1 *relative;
static struct zwp_locked_pointer_v1 *locked;
static struct zwp_confined_pointer_v1 *confined;
static struct wl_pointer *pointer;
+static struct wp_pointer_warp_v1 *warper;
+static uint32_t enter_serial;
+static struct wl_pointer_listener constraint_pointer_listener;
+
+// The shared fixture listener, plus the enter serial a warp must quote.
+static void warp_enter(void *d, struct wl_pointer *p, uint32_t serial, struct wl_surface *s,
+ wl_fixed_t x, wl_fixed_t y) {
+ enter_serial = serial;
+ pointer_enter(d, p, serial, s, x, y);
+}
static void relative_motion(void *d, struct zwp_relative_pointer_v1 *r, uint32_t hi, uint32_t lo,
wl_fixed_t dx, wl_fixed_t dy, wl_fixed_t udx, wl_fixed_t udy) {
@@ -31,7 +42,9 @@ static void constraint_caps(void *d, struct wl_seat *s, uint32_t c) {
(void)d;
if ((c & WL_SEAT_CAPABILITY_POINTER) && !pointer) {
pointer = wl_seat_get_pointer(s);
- wl_pointer_add_listener(pointer, &pointer_listener, NULL);
+ constraint_pointer_listener = pointer_listener;
+ constraint_pointer_listener.enter = warp_enter;
+ wl_pointer_add_listener(pointer, &constraint_pointer_listener, NULL);
}
}
static const struct wl_seat_listener constraint_seat_listener = {constraint_caps, seat_name};
@@ -39,6 +52,8 @@ static void constraint_global(void *d, struct wl_registry *r, uint32_t id, const
printf("global %s %u\n", iface, version);
if (!strcmp(iface, "zwp_pointer_constraints_v1"))
constraints = wl_registry_bind(r, id, &zwp_pointer_constraints_v1_interface, 1);
+ else if (!strcmp(iface, "wp_pointer_warp_v1"))
+ warper = wl_registry_bind(r, id, &wp_pointer_warp_v1_interface, 1);
else if (!strcmp(iface, "zwp_relative_pointer_manager_v1"))
relative_manager = wl_registry_bind(r, id, &zwp_relative_pointer_manager_v1_interface, 1);
else if (!strcmp(iface, "wl_seat")) {
@@ -55,9 +70,14 @@ static uint32_t lifetime(const char *mode) {
static void command(char *line) {
char mode[32];
- int x, y, w, h;
+ int x, y, w, h, stale = 0;
double hx, hy;
- if (!strcmp(line, "relative")) {
+ if (sscanf(line, "warp %31s %lf %lf %d", mode, &hx, &hy, &stale) >= 3) {
+ // "warp main|child X Y [1]": 1 quotes a serial never sent.
+ wp_pointer_warp_v1_warp_pointer(warper, !strcmp(mode, "child") ? child : surface, pointer,
+ wl_fixed_from_double(hx), wl_fixed_from_double(hy),
+ stale ? enter_serial + 1000 : enter_serial);
+ } else if (!strcmp(line, "relative")) {
relative = zwp_relative_pointer_manager_v1_get_relative_pointer(relative_manager, pointer);
zwp_relative_pointer_v1_add_listener(relative, &relative_listener, NULL);
} else if (sscanf(line, "lock %31s", mode) == 1) {
@@ -94,7 +114,7 @@ int main(int argc, char **argv) {
wl_registry_add_listener(wl_display_get_registry(display), &constraint_registry, NULL);
assert(wl_display_roundtrip(display) >= 0);
assert(wl_display_roundtrip(display) >= 0);
- assert(constraints && relative_manager && pointer);
+ assert(constraints && relative_manager && warper && pointer);
app_id = argc > 1 ? argv[1] : "rediwm.pointer-constraints";
surface = wl_compositor_create_surface(compositor);
xdg = xdg_wm_base_get_xdg_surface(wm, surface); xdg_surface_add_listener(xdg, &xdg_listener, NULL);