fix(sandbox): the builder holds the cursor in the game's window on Windows #3073
No reviewers
Labels
No labels
area/accessibility
area/audio
area/input
area/install
area/menu
area/mods
area/renderer
area/saves
area/tooling
area/upscaling
beanstalk: approved
beanstalk: failed
beanstalk: needs approval
beanstalk: running
beanstalk: tested
ci: approved
good first issue
gpu/amd
gpu/igpu
gpu/intel
gpu/nvidia
help wanted
kind
bug
kind
chore
kind
docs
kind
feature
kind
performance
kind
question
needs measurements
platform/linux
platform/macos
platform/steam-deck
platform/windows
preview
priority
critical
priority
high
priority
low
regression
risk
high
risk
low
risk
medium
status
blocked
status
confirmed
status
duplicate
status
invalid
status
needs-info
status
wontfix
ci: approved
No milestone
No project
No assignees
3 participants
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
MagicBeanDev/Longshot!3073
Loading…
Reference in a new issue
No description provided.
Delete branch "Veix/Longshot:fix/sandbox-clip-cursor"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Summary
The Windows cursor clip is routed through the builder: in the sandbox, the mouse no longer escapes to other monitors.
Since the sandbox is on by default (#2822), the game runs in an AppContainer on Windows. Windows refuses its
ClipCursorthere: the call needs the window station'sWINSTA_WRITEATTRIBUTES, which an AppContainer does not get(error 5, access denied).
host/display.c'smouse_capturenever looked at the result, so with the mouse cameracaptured the cursor was hidden but free, and ran onto the player's other monitors, in fullscreen too (reported on
Discord).
SetCursorPosneeds the same right, so the game cannot hold the cursor itself in any way.On main: Windows with two monitors, load a save, click into the game so it takes the mouse, move it toward the other
monitor: the cursor leaves the game. With
HOS_SANDBOX=0it stays.This PR does it the way the sandbox does the other things the container may not (
open,message_box): an op on thegame's channel to its builder.
clip_cursor(build_driver::channel,build_core::cursor_clip):{on}alone. The builder takes the window infront, checks it is the game's (its process id), reads its client area itself and clips the cursor to it, on a thread
set per-monitor DPI aware so the rectangle is in the screen's real pixels. The game names no rectangle: one that was
taken over can hold the cursor nowhere but over its own window. The clip lasts only while the game is in front: the
builder's own watch looks every 100 ms and lets it go once another window is in front, whatever the game asks. It is
let go too on
on: falseand when the channel ends (the game gone, crashed or not). The builder lets go only its ownclip (the clip now still the rectangle it set), never one another program made since. The Windows calls are
build_core::sys's, where the builder'sunsafecode belongs.host/display.c): its ownClipCursorfirst (unconfined,HOS_SANDBOX=0, it holds as before); refused,it asks the builder (
game_builder_clip_cursor,builder_link.rs, which sends{on}alone and waits at most asecond on the window's thread), and says once in the log which happened. Releasing the capture asks the builder to
let go whenever it asked, answered or not (a late answer may have clipped it).
build.md's table andmod-ops.md's list, the cursor insandbox.md's "What the builder does forthe game".
Linux is not affected: SDL's relative mouse mode is held by the display server (an X11 grab, Wayland's pointer
constraints) over the connection the sandbox leaves open.
Written with AI assistance (Claude).
Testing
Tested: Windows 11, RTX 4070 SUPER, NVIDIA 621.24, two monitors (2560x1440 and a 4K TV), fullscreen on the 2560x1440
one, a save loaded, the mouse captured and pushed toward the other monitor:
2ca7d4f7with a log line at the call: sandbox on,ClipCursorfailed with error 5 and the cursor left the game;HOS_SANDBOX=0,ClipCursorsucceeded, the clip read back as 0,0-2560,1440, and the cursor stayed.display: the cursor's clip refused (error 5): held by the builder, and the cursorstayed in the game; alt-tab to another program freed it on both monitors, and back in the game it was held again.
cargo test:build-corecursor_clip(5 passed, on a desktop of the test's: no clip for a game not started ornot in front, let go when the game asks, another program's clip made since never let go, the watch letting go once
the game is not in front, the watch's thread ending by itself),
build-driverchannel(18 passed),longshot-gamebuilder_link(35 passed, new:the_game_asks_the_cursor_held_with_on_alone);cargo clippy -p build-core -p build-driver --all-targets -D warningsandcargo fmt --checkclean.tests/run_all.py: the 32 checks that readhost/display.cand those that read the builder ops: passed, but fourthat need a Linux C toolchain (
test_key_press_queue,test_net_engine,test_pipeline_tour,test_sandbox_linux),which fail the same way on main on this PC.
tests/display_pointer_test.cpptakes the new state (builder_clip).Still to test:
window and no further (by hand)
@Beanstalksmoke: the game starts, plays and quits as beforeMeasurements
Nothing to measure: one request when the capture changes, none per frame; the mouse's input does not go through the builder.
Risks
A builder that does not serve
clip_cursor(an older one) leaves the cursor as main does (the game asks only a builderthat says it serves the op). A revert is enough. Players with one monitor notice nothing; with several, the cursor stays
in the game while it is captured.
Checklist
Ready for review
main; the title is a Conventional Commit.mise run testpasses (any failure that also fails onmainis named).main, or Measurements says why there is nothing to measure.third_party/untouched; nothing GPL-incompatible.When it applies
tests/that reads changed source is updated, not weakened.Assisted-by:/Co-Authored-By:trailers.Ready to merge
main, measured again or a comment says why the numbers still hold.🤖 Generated with Claude Code
Tested in game: Windows 11, RTX 4070 SUPER, two monitors, sandbox on: the game's ClipCursor refused (error 5), held by the builder, the cursor stayed in the game; on main it escaped
Possibly related, for context:
@review
Recommendation: Request changes. The fix holds the cursor in the reported initial capture case, but it can release another application's clip or leave its own clip active after a timed-out request. It also places unsafe code outside the repository's permitted locations.
The builder clips the Windows cursor on behalf of the sandboxed game. This fixes the reported escape in one tested setup, at the cost of a synchronous builder request on the window thread and new cursor-lifetime handling.
Blocking (3): must change before merging.
builder/crates/build-core/src/cursor_clip.rs:33: The flag records that the builder once set a clip, not that the current system clip is still its own. (more on the line) Do: Track and coordinate clip ownership across focus changes, and do not clear a clip that has since been replaced by another application.builder/crates/build-core/src/cursor_clip.rs:77: The foreground check occurs only when a request is processed; it does not keep the clip restricted to the time the game is in front. (more on the line) Do: Have the builder independently release its clip when the game loses foreground, and revalidate foreground when applying a clip.builder/crates/build-core/src/cursor_clip.rs:39: This introduces an unsafe-code allowance outside the locations permitted by the builder's code standard. (more on the line) Do: Move the unsafe Windows calls behind safe wrappers in an allowed system-call module, or obtain and document a change to the repository rule.Should fix (1): not blocking, but worth doing before merging.
host/display.c:357: A timed-out capture request is treated as if the builder cannot later apply the clip, so the corresponding release is skipped. (more on the line) Do: Make capture and release ordered operations whose desired state remains authoritative after a timeout, including cancellation or reconciliation of late requests.Testing before merging (1) (runs on the systems it can change; it doesn't hold the recommendation)
builder/crates/build-core/src/cursor_clip.rs:69: The scaled-display and focus-transition behavior remains unverified on Windows. (more on the line) Do: Run the described scaled, offset-window and alt-tab cases on Windows and record the results, including the cursor's bounds and release behavior.@fixcan do L1, L2, L3 and L4.Questions (1) (not confirmed from the code; they don't hold the recommendation)
tests/display_pointer_test.cpp:48: Is there a repository rule requiring a game-free behavioral test for this fallback?Checks: Still running: 11 checks.
More: does it help, other systems, switches, what was read
Does it help: Yes for the reported initial capture: the author observed the cursor escape on base
2ca7d4f7and remain in the game on this head, on Windows 11 with two monitors. Scaled displays and focus transitions remain untested.Other systems: Linux does not call the new host path; the builder returns a refusal there. Windows window movement, resizing, focus changes, other applications using ClipCursor, channel delays and game termination can affect the new clip. The reported run covers initial fullscreen capture only.
Switches: None added, changed or removed.
Reviewed:
56570a4abcagainst2ca7d4f741: Read the repository rules, standards, pull request template, commit and full diff; traced the changed cursor implementation, host capture and window events, builder channel and link calls, and relevant test harness. Skimmed the changed reference documentation and surrounding large files. Tooling's AGENTS.md and docs/review-checklist.md were absent. No recorded runs, translations or generated files were changed.; nothing run (a code review).Checked and fine:
Testing: Read-only review; no commands from the project, builds or tests were run. The author's Windows game run and reported checks are considered as evidence; CI is still running.
Overlaps and merge order: Not checked; network access was unavailable.
Automated code review by BeanBot (gpt-6.1-terra), asked for by Veix. Nothing was run; a maintainer decides.
@ -0,0 +30,4 @@/// A clip the builder made let go (the game's channel ended); nothing when it holds none, so another program's clip/// stays.pub fn release() {if HELD.swap(false, Ordering::SeqCst) {L1 (blocking): The flag records that the builder once set a clip, not that the current system clip is still its own. Releasing it can therefore remove another application's clip.
When: The game holds a clip, the player switches to another application and that application sets its own clip before the game's release request is served. HELD is still true, so release calls ClipCursor(NULL) and removes the other application's confinement.
Do: Track and coordinate clip ownership across focus changes, and do not clear a clip that has since been replaced by another application.
@ -0,0 +36,4 @@}#[cfg(windows)]#[allow(unsafe_code)]L4 (blocking): This introduces an unsafe-code allowance outside the locations permitted by the builder's code standard.
When: The new Windows module contains unsafe Windows calls in cursor_clip.rs, while docs/dev/standards.md permits unsafe builder code only in the named system-call modules.
Do: Move the unsafe Windows calls behind safe wrappers in an allowed system-call module, or obtain and document a change to the repository rule.
@ -0,0 +66,4 @@if pid == 0 {return Err("the game has not started".into());}let _aware = PerMonitor::new();L5 (should fix): The scaled-display and focus-transition behavior remains unverified on Windows.
When: The only reported run uses fullscreen on the primary monitor; it does not show whether a scaled, offset window receives the correct clip or whether switching away releases it.
Do: Run the described scaled, offset-window and alt-tab cases on Windows and record the results, including the cursor's bounds and release behavior.
@ -0,0 +74,4 @@// SAFETY: a window handle Windows just gave; one closed since leaves `owner` 0.unsafe { GetWindowThreadProcessId(window, &mut owner) };}if window.is_null() || owner != pid {L3 (blocking): The foreground check occurs only when a request is processed; it does not keep the clip restricted to the time the game is in front.
When: A compromised game requests a clip while its window is foreground and then fails to send the release on focus loss. The builder keeps the system-wide clip until the channel closes, so the cursor remains confined while another application is foreground.
Do: Have the builder independently release its clip when the game loses foreground, and revalidate foreground when applying a clip.
@ -349,0 +354,4 @@/* (refused: the sandbox's AppContainer has no WINSTA_WRITEATTRIBUTES, error 5, and the cursor would run onto theother monitors; the builder holds it in this window for the game, its window in front alone) */const DWORD err = GetLastError();builder_clip = game_builder_clip_cursor(1) == 1;L2 (should fix): A timed-out capture request is treated as if the builder cannot later apply the clip, so the corresponding release is skipped.
When: The builder's channel is delayed for more than the 30-second call timeout. The window thread records builder_clip as false; the builder can subsequently process the queued on request and clip the cursor, but focus loss and release send no off request, leaving the cursor confined until the channel ends.
Do: Make capture and release ordered operations whose desired state remains authoritative after a timeout, including cancellation or reconciliation of late requests.
@review
Recommendation: Request changes. The builder can still release another application's cursor clip, and a foreground-window race can clip the cursor to a different application's window. The scaled-display behavior also remains unverified.
The change lets the builder confine the cursor for a sandboxed Windows game and releases it on focus loss or channel shutdown. It fixes the reported escape on the tested setup, at the cost of a polling thread and additional synchronization that still needs correction.
Blocking (1): must change before merging.
builder/crates/build-core/src/cursor_clip.rs:33: The flag records that the builder once set a clip, not that the current system clip is still its own. Releasing it can therefore remove another application's clip. When: The game holds a clip, the player switches to another application and that application sets its own clip before the game's release request is served. HELD is still true, so release calls ClipCursor(NULL) and removes the other application's confinement. Do: Track and coordinate clip ownership across focus changes, and do not clear a clip that has since been replaced by another application.Should fix (1): not blocking, but worth doing before merging.
builder/crates/build-core/src/sys.rs:827: The foreground process check reads a different foreground window from the one whose client rectangle is used. (more on the line) Do: Get the process ID from the same window handle whose client rectangle is read, and recheck foreground ownership before applying the clip.Testing before merging (1) (runs on the systems it can change; it doesn't hold the recommendation)
builder/crates/build-core/src/cursor_clip.rs:69: The scaled-display and focus-transition behavior remains unverified on Windows. When: The only reported run uses fullscreen on the primary monitor; it does not show whether a scaled, offset window receives the correct clip or whether switching away releases it. Do: Run the described scaled, offset-window and alt-tab cases on Windows and record the results, including the cursor's bounds and release behavior.@fixcan do L1 and L6.Since the last review (on
56570a4abc)Resolved: L2 (A timed-out capture request is treated as if the builder cannot later apply the clip, so t…); L3 (The foreground check occurs only when a request is processed; it does not keep the clip re…); L4 (This introduces an unsafe-code allowance outside the locations permitted by the builder's…)
Still open: L1 (The flag records that the builder once set a clip, not that the current system clip is sti…); L5 (The scaled-display and focus-transition behavior remains unverified on Windows.)
New: L6 (The foreground process check reads a different foreground window from the one whose client…)
Checks: Still running: 11 checks.
More: does it help, other systems, switches, what was read
Does it help: Yes: the author reports that the cursor escaped on main and stayed in the game with this change on Windows 11 with two monitors; alt-tab released it. Scaled and offset windows remain untested.
Other systems: The new cursor operation is Windows-specific; Linux returns an error and retains its existing pointer handling. Unconfined Windows games continue to use their own ClipCursor call. Focus transitions and other applications' cursor clips share the affected Windows desktop state.
Switches: None.
Reviewed:
b2b820e519against2ca7d4f741: Read the cursor-clip implementation and its changed system calls, the capture and builder-link changes, channel diff, related documentation and tests, project rules, standards and pull request template. The remaining surrounding files and recorded runs were skimmed; the full contents of every changed code file were not read.; nothing run (a code review).Checked and fine:
Testing: The author reports an in-game Windows comparison and focus transition, plus game-free checks. A scaled display and offset window still need a Windows run; CI is still running. No commands from the pull request were run for this review.
Overlaps and merge order: Not checked; network access is closed.
Automated code review by BeanBot (gpt-6.1-terra), asked for by Veix. Nothing was run; a maintainer decides.
@ -750,0 +824,4 @@pub(super) fn clip_cursor_to_front(pid: u32) -> Result<[i32; 4], String> {use windows_sys::Win32::UI::WindowsAndMessaging::{ClipCursor, GetForegroundWindow, GetWindowInfo, WINDOWINFO};let _aware = PerMonitor::new();// SAFETY: no argument; a null window when none is in front.L6 (should fix): The foreground process check reads a different foreground window from the one whose client rectangle is used.
When: If focus moves from another application's window to the game between the two GetForegroundWindow calls, front_window_pid accepts the game but GetWindowInfo reads the earlier application's window, so the builder clips the cursor to that window.
Do: Get the process ID from the same window handle whose client rectangle is read, and recheck foreground ownership before applying the clip.
L6: fixed in
cebcbe8ba: one window handle throughout. Its owner is checked, its client area read, and it is checked to still be the window in front just beforeClipCursor.L1: addressed in
b2b820e51. TheHELDflag this comment describes is gone: the builder remembers the rectangle it set and releases only whenGetClipCursorstill returns exactly that rectangle (cursor_clip.rsrelease_with, testa_clip_another_program_made_since_is_never_let_go). What remains is what Windows can't tell apart: a clip records no owner, so another program setting the identical rectangle (a second fullscreen program on the same monitor) can't be distinguished from ours, and a clip set in the microseconds betweenGetClipCursorandClipCursor(NULL)could be lost. There is no API to do better.@review
Recommendation: Request changes. The builder can still release another application's cursor clip when that application sets the same rectangle or changes the clip between the check and release. The scaled, offset-window case also remains untested.
The builder confines the cursor for the sandboxed Windows game when the game's own call is refused. The reported two-monitor test shows the intended benefit, at the cost of a remaining risk to another application's clip.
Blocking (1): must change before merging.
builder/crates/build-core/src/cursor_clip.rs:33: The flag records that the builder once set a clip, not that the current system clip is still its own. Releasing it can therefore remove another application's clip. When: The game holds a clip, the player switches to another application and that application sets its own clip before the game's release request is served. HELD is still true, so release calls ClipCursor(NULL) and removes the other application's confinement. Do: Track and coordinate clip ownership across focus changes, and do not clear a clip that has since been replaced by another application.Testing before merging (1) (runs on the systems it can change; it doesn't hold the recommendation)
builder/crates/build-core/src/cursor_clip.rs:69: The scaled-display and focus-transition behavior remains unverified on Windows. When: The only reported run uses fullscreen on the primary monitor; it does not show whether a scaled, offset window receives the correct clip or whether switching away releases it. Do: Run the described scaled, offset-window and alt-tab cases on Windows and record the results, including the cursor's bounds and release behavior.@fixcan do L1.Since the last review (on
b2b820e519)Resolved: L6 (The foreground process check reads a different foreground window from the one whose client…)
Still open: L1 (The flag records that the builder once set a clip, not that the current system clip is sti…); L5 (The scaled-display and focus-transition behavior remains unverified on Windows.)
Checks: Still running: 11 checks.
More: does it help, other systems, switches, what was read
Does it help: Yes: the author reports that the cursor escaped on main and stayed in the game on this PR on Windows 11 with two monitors. Scaled and offset windows remain unverified.
Other systems: The new clipping calls target Windows; Linux continues to use SDL relative mouse mode. The author tested an unconfined Windows game on main and sandboxed focus transitions on the PR; scaled displays remain outstanding.
Switches: None added, changed or removed.
Reviewed:
cebcbe8ba4against2ca7d4f741: Read cursor_clip.rs and the changed Windows sys.rs functions in full, the changes to the channel, game link, host display, documentation and tests, and the repository rules and PR template. The larger existing files and their callers were only skimmed; the full changed code was not read.; nothing run (a code review).Checked and fine:
Testing: No commands that build or run the project were executed. The author reports a Windows in-game test and passing targeted checks; CI is still running.
Overlaps and merge order: Not established from the available information.
Automated code review by BeanBot (gpt-6.1-terra), asked for by Veix. Nothing was run; a maintainer decides.
L1: the text still describes the removed
HELDflag. The two residual cases (an identical rectangle set by another program, a clip set betweenGetClipCursorandClipCursor(NULL)) can't be told apart by any Windows API, since a clip records no owner. For comparison, main's own unsandboxed path inhost/display.creleases with a plainClipCursor(NULL)and no check at all; this PR's builder path only releases a clip that is still exactly its own. Leaving this to a maintainer.@review
Recommendation: Request changes. The builder can still release another application's cursor clip when that application uses the same rectangle or changes its clip between the check and release. The scaled-display and offset-window behavior also awaits a Windows run.
The builder confines the sandboxed game's cursor to its foreground window, fixing the reported multi-monitor escape in the author's Windows test. The change adds a watcher and shared cursor state, but its ownership check cannot reliably distinguish every other application's clip.
Blocking (1): must change before merging.
builder/crates/build-core/src/cursor_clip.rs:33: The flag records that the builder once set a clip, not that the current system clip is still its own. Releasing it can therefore remove another application's clip. When: The game holds a clip, the player switches to another application and that application sets its own clip before the game's release request is served. HELD is still true, so release calls ClipCursor(NULL) and removes the other application's confinement. Do: Track and coordinate clip ownership across focus changes, and do not clear a clip that has since been replaced by another application.Testing before merging (1) (runs on the systems it can change; it doesn't hold the recommendation)
builder/crates/build-core/src/cursor_clip.rs:69: The scaled-display and focus-transition behavior remains unverified on Windows. When: The only reported run uses fullscreen on the primary monitor; it does not show whether a scaled, offset window receives the correct clip or whether switching away releases it. Do: Run the described scaled, offset-window and alt-tab cases on Windows and record the results, including the cursor's bounds and release behavior.@fixcan do L1.Since the last review (on
cebcbe8ba4)Still open: L1 (The flag records that the builder once set a clip, not that the current system clip is sti…); L5 (The scaled-display and focus-transition behavior remains unverified on Windows.)
Checks: Still running: 11 checks.
More: does it help, other systems, switches, what was read
Does it help: Yes for the reported case: the author reports that the cursor escaped on main and stayed in the game on this PR on Windows 11 with two monitors. Scaled and offset windows remain untested.
Other systems: The new clipping path uses Windows window and DPI APIs; the author tested an unscaled Windows 11 NVIDIA setup and an alt-tab transition. Linux uses its existing display-server relative mouse handling and is not exercised by the Windows clipping path.
Switches: None added, changed, or removed.
Reviewed:
cebcbe8ba4against2ca7d4f741: Read AGENTS.md, CONTRIBUTING.md, the pull request template, cursor_clip.rs, the supplied description, comments, CI and ledger. Skimmed the pull request diff and commit list; the remaining changed code files and their callers were not read in full. docs/review-checklist.md and ../Tooling/AGENTS.md were unavailable.; nothing run (a code review).Checked and fine:
Testing: No commands that build or run project code were executed. The author reports in-game Windows testing and game-free checks; CI is still running. A scaled Windows display with an offset game window remains to be tested.
Overlaps and merge order: Not checked; network access is closed.
Automated code review by BeanBot (gpt-6.1-terra), asked for by llun. Nothing was run; a maintainer decides.