Skip to content

fence: a patrol, for a pointer that arrives without moving - #9

Merged
fdussert merged 1 commit into
fdussert:mainfrom
mcouzinet:cursor-patrol
Nov 19, 2026
Merged

fdussert merged 1 commit into
fdussert:mainfrom
mcouzinet:cursor-patrol

Conversation

@mcouzinet

Copy link
Copy Markdown
Contributor

The mouse fence taps mouseMoved and the drags, so it only ever acts on a pointer that moves. A cursor reaches the Edge in other ways:

  • an application warps it there, and CGWarpMouseCursorPosition posts no event at all;
  • the system disables the tap for a moment (tapDisabledByTimeout, tapDisabledByUserInput) and it crosses unseen;
  • a display is rearranged under a pointer that was resting where the Edge now is.

Once there it stays, because nothing moves it, and the next click or scroll goes to the dashboard rather than to the window the user is looking at. That is the part that hurts: the pointer is invisible under the kiosk, and the way out is to find the mouse and swing it across two displays. It caught me twice this week.

So the fence also looks at where the pointer really is, every 200 ms, and pushes it back with the geometry the tap already used. The read is one CGEvent(source: nil): no event, no round trip, nothing allocated.

Worth a look:

  • The patrol runs even when the tap could not be created. Warping the cursor needs no Accessibility grant, so a Mac that has granted nothing still keeps its pointer off the panel. The message on the missing permission now says what is left rather than implying the fence is off.
  • It stands back while a gesture owns the cursor. The driver warps the pointer onto the panel for the length of a touch and restores it afterwards; EventPoster.isDrivingCursor is exactly that window (hidden cursor or pending restore, both already tracked under its lock), and pushing during it would fight the finger.
  • The decision is FenceMath.patrol, beside clamp, so the rules are testable with plain rectangles: four new cases in FenceMathTests, registered in the shim.

scripts/test-helper.sh green (80 tests), scripts/build-helper.sh builds. Checked on my Edge with the helper installed: a cursor warped to the middle of the panel is back on the main display 50 ms later, at the first tick, and a touch gesture is not disturbed.

🤖 Generated with Claude Code

@fdussert fdussert left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks — a pointer stranded under the kiosk is exactly the kind of thing nobody can diagnose. Reviewed merged onto current main with #7 and #8: scripts/test-helper.sh 89/89, the helper builds clean; removing the !driven guard in FenceMath.patrol fails testPatrolStandsBackWhileAGestureOwnsTheCursor. Standing back on poster.isDrivingCursor covers a gesture and its 0.25 s restore, both on the main thread, and running the patrol without Accessibility is a good call.

Two things I'd like fixed before merging:

  1. A race that now moves the pointer, not just shows it. In the restore block of EventPoster.swift (around lines 180–195), a new touch can start on the driver thread between warpCursor(to: saved) and showCursorIfHidden(). That clears cursorHidden while the new gesture runs, so isDrivingCursor reads false with the cursor on the Edge, and the patrol (MouseFence.swift:196-206) pushes it off mid-gesture. Taps and drags survive because each report re-warps, but a finger held still can lose it. Re-checking the gesture counter before showCursorIfHidden() would close it.
  2. The patrol can switch itself off for good. If CGEvent(source: nil) returns nil at the first warp (EventPoster.swift:165), restoreCursor returns early and cursorHidden stays true forever — which keeps the cursor hidden (that half predates this PR) and now also disables the patrol permanently. Rare, but cheap to guard: clear the state on that early return.

Smaller:

  • The tick reads the pointer before checking there is an Edge (MouseFence.swift:199); checking edge first makes it free with no Edge connected, and the timer could carry a tolerance.
  • isDrivingCursor lives in the helper and is untested; worth a test if it can move to FremkitCore.

Note for the rebase: #7 and #9 conflict in CHANGELOG.md and docs/troubleshooting.md (the "Fence: permission missing" row); both sides keep, nothing subtle.

@fdussert fdussert mentioned this pull request Oct 15, 2026

@fdussert fdussert left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks Mickael, nice catch, and a neat fit: putting the patrol inside MouseFence means it follows syncFences() with no extra wiring, and isDrivingCursor is the right guard. I checked the lock ordering against warp/restoreCursor and found no window where the patrol could fight a gesture. Built and tested locally (83/83, and 92/92 merged with #7). Two small things:

  1. MouseFence.swift:35/:205: last survives a display rearrangement, which is one of the cases this PR targets. If it now sits on no display, clamp still returns it, the warp can be clamped back onto the Edge, and the patrol repeats that every 200 ms. Could you reset last when edge changes (a didSet), or check that it is still on an active display?
  2. Nit, MouseFence.swift:183: a tolerance of 50–100 ms on the patrol timer would let macOS coalesce the wake-ups, since it runs for as long as the dashboard is shown. Also, CGEvent(source: nil) does allocate an event, so "nothing allocated" (comment at line 30 and the PR body) is slightly off. The cost is negligible either way.

Heads-up: #7 and #9 conflict only in CHANGELOG.md and docs/troubleshooting.md. Keeping both sides resolves it, and whichever lands second will need that small rebase.

The tap only ever sees movement, so the fence only ever acted on a
pointer that moved. A cursor reaches the Edge in other ways: an
application warping it there posts no event at all, a tap the system
disables for a moment lets it cross unseen, and a display rearranged
under a resting pointer leaves it stranded. Once there it stays, since
nothing moves it, and the next click or scroll lands on the dashboard
rather than on the window the user is looking at. It happened to me
twice this week, and the way out is to find the mouse and swing it.

So the fence now also looks, every 200 ms, at where the pointer really
is, and pushes it back with the geometry the tap already used. The read
is one `CGEvent(source: nil)`, no event and no round trip. The patrol
runs even when the tap could not be created: warping the cursor needs
no Accessibility grant, so a Mac that has granted nothing still keeps
its pointer off the panel, and the message about the missing permission
says what is left.

It stands back while a touch gesture owns the cursor. The driver warps
the pointer onto the panel for the length of a gesture and restores it
afterwards; `EventPoster.isDrivingCursor` is that window, and pushing
during it would fight the finger.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@fdussert

Copy link
Copy Markdown
Owner

Hi Mickael, the patrol is a great catch, thank you, and sorry for the silence since your rebase. We added the follow-ups on top of your commit, in the feature/edge-fences branch:

  • a stale last position (after a display rearrangement) is dropped, so the patrol can no longer loop, with tests in FenceMath;
  • a 50 ms tolerance on the patrol timer;
  • the cursor restore is atomic against a new touch, and the cursor no longer stays hidden when its position could not be read.

Your commit is kept as is, and this PR will show as merged once main is pushed.

@fdussert
fdussert merged commit dccb950 into fdussert:main Nov 19, 2026
2 checks passed
fdussert added a commit that referenced this pull request Nov 19, 2026
… Edge

mcouzinet's window fence (#7) and pointer patrol (#9), with the review's remaining points finished on top of his commits: Accessibility calls off the main thread, no move during a drag, attempts counted per episode, a stale last position dropped after a display rearrangement, an atomic cursor restore.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
fdussert added a commit that referenced this pull request Nov 19, 2026
The Edge fence for windows and the pointer patrol (mcouzinet, #7 and #9), the Claude limits widget with one row per account, and pinned Claude sessions.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants