Files
lucioandClaude Opus 5 42c614591b input-capture: Screens and Cursor
ScreenLayout is GetScreenConfig/MonitorEnumProc on CGDisplayBounds, with
the four corners BlockMouseAtScreenCorners needs, refreshed on
didChangeScreenParameters. Cursor is HideMouseCursor plus the macOS-only
decoupling that keeps the real pointer still while a peer is controlled.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
2026-09-16 02:09:27 -07:00

134 lines
5.4 KiB
Swift

// The real pointer: park it, hide it, unhook it from the mouse while a remote
// machine is being controlled, and put it back when control comes home.
//
// Windows parks the cursor with `SetCursorPos` and relies on the low-level hook
// swallowing every mouse event to keep it there (`Common.HideMouseCursor`,
// `Common.cs:1482-1496`). macOS needs one thing more: swallowing events in a tap
// does not stop the window server from moving the pointer, so the pointer has to
// be decoupled from the mouse with `CGAssociateMouseAndMouseCursorPosition`.
// Deltas keep arriving on the tap while it is decoupled, which is exactly what
// the virtual cursor in `RoutingState` consumes.
//
// CLAUDE.md invariant 3: everything here is reversible from `recouple`, and
// `recouple` is what every recovery path in contracts C2 ends with.
import CoreGraphics
import Foundation
import MWBCore
import os
/// The real mouse pointer.
public final class Cursor: Sendable {
private struct State: Sendable {
var hidden = false
var decoupled = false
}
private let state: OSAllocatedUnfairLock<State>
/// A cursor controller for this session. One per process.
public init() {
state = OSAllocatedUnfairLock(initialState: State())
}
/// Where the pointer is parked while a remote machine has control
/// (`Common.cs:1484-1486`): the primary screen's horizontal centre, and
/// `y = 4` when `HideMouseAtScreenEdge` is on so the pointer sits in the menu
/// bar out of the way, otherwise the vertical centre.
public static func parkPosition(hideMouse: Bool, in layout: ScreenLayout) -> CGPoint {
let centre = Coordinates.centre(of: layout.primary)
return CGPoint(x: centre.x, y: hideMouse ? 4 : centre.y)
}
/// `Common.HideMouseCursor` (`Common.cs:1482-1496`): work out the park
/// position, and move the pointer there **only** when the gate passes.
///
/// The gate is Windows' `(desMachineID != MachineID && != ALL) ||
/// byHideMouseMessage` (`:1488`), pre-evaluated by `Switcher` and handed over
/// as `SwitchAction.parkCursor(destinationIsRemote:byHideMouseMessage:)` —
/// see the session-matrix Handshakes. Moving unconditionally would jerk the
/// pointer to the top of the screen every time control comes home.
///
/// Returns the park position either way, because Windows records it as
/// `LastPos` (the position the pointer is restored to) on every call.
@discardableResult
public func park(
hideMouse: Bool, layout: ScreenLayout, destinationIsRemote: Bool,
byHideMouseMessage: Bool
) -> CGPoint {
let position = Cursor.parkPosition(hideMouse: hideMouse, in: layout)
if destinationIsRemote || byHideMouseMessage { warp(to: position) }
return position
}
/// Hands the pointer to a remote machine: park it, hide it, and stop the
/// mouse from moving it. Returns the parked position.
///
/// Call once per switch away from this Mac, from the Engine — never from the
/// tap callback, since all three calls talk to the window server.
@discardableResult
public func decouple(parkHidden: Bool, layout: ScreenLayout) -> CGPoint {
let position = park(
hideMouse: parkHidden, layout: layout, destinationIsRemote: true,
byHideMouseMessage: false)
associate(false)
hide()
return position
}
/// Gives the pointer back to this Mac: show it, re-associate it with the
/// mouse, and put it where it was. The order matters — re-associating first
/// would let a queued delta move it before the warp lands.
public func recouple(to point: CGPoint?) {
show()
associate(true)
if let point { warp(to: point) }
}
/// Moves the pointer without generating an event (`SetCursorPos`).
public func warp(to point: CGPoint) {
_ = CGWarpMouseCursorPosition(point)
}
/// Hides the pointer. Balanced with `show()`: `CGDisplayHideCursor` is
/// reference counted, so an unmatched hide leaves the Mac without a pointer.
public func hide() {
let shouldHide = state.withLock { state -> Bool in
guard !state.hidden else { return false }
state.hidden = true
return true
}
if shouldHide { _ = CGDisplayHideCursor(CGMainDisplayID()) }
}
/// Shows the pointer again, if this `Cursor` hid it.
public func show() {
let shouldShow = state.withLock { state -> Bool in
guard state.hidden else { return false }
state.hidden = false
return true
}
if shouldShow { _ = CGDisplayShowCursor(CGMainDisplayID()) }
}
/// Whether the pointer is currently frozen by us.
public var isDecoupled: Bool { state.withLock { $0.decoupled } }
/// Everything undone, for `atexit` and the signal handler (contracts C2, last
/// row): the Mac must never be left with a frozen or invisible pointer.
public func restoreEverything() {
show()
associate(true)
}
private func associate(_ connected: Bool) {
let changed = state.withLock { state -> Bool in
guard state.decoupled == connected else { return false }
state.decoupled = !connected
return true
}
guard changed else { return }
_ = CGAssociateMouseAndMouseCursorPosition(connected ? 1 : 0)
}
}