Files
lucioandClaude Opus 5 d2d221fe29 input-capture: the routing snapshot lock and the bounded capture ring
RoutingState holds the snapshot and the virtual cursor behind one
OSAllocatedUnfairLock so the tap callback can decide synchronously
(contracts C3); CaptureQueue is the 4096-entry ring with the C3
coalescing and overflow policy, plus notices that survive a full ring.

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

99 lines
4.1 KiB
Swift

// The one piece of shared state the event-tap callback is allowed to read:
// where input is going right now. See contracts C3.
//
// The Engine (an actor) owns everything else, but the tap must decide whether to
// swallow an event *synchronously*, inside a callback that may not await and may
// not block (CLAUDE.md invariant 4). So the decision inputs live here, behind an
// `OSAllocatedUnfairLock` that is only ever held for a struct copy.
//
// The virtual cursor lives here for the same reason: while a remote machine is
// being controlled the real pointer is parked (`Cursor.decouple`), so the tap
// keeps its own idea of where the pointer "is", seeded by the Engine when a
// switch lands and advanced by the callback from the raw deltas. It is the
// macOS stand-in for Windows' `Common.LastPos` plus `hookCallbackMouseData.X/Y`
// (`InputHook.cs:287-309`).
import Foundation
import MWBCore
import os
/// The routing snapshot and the virtual cursor, shared between the Engine and
/// the event-tap callback.
public final class RoutingState: Sendable {
private struct Box: Sendable {
var snapshot: RoutingSnapshot
var virtualCursor: CGPoint
}
private let box: OSAllocatedUnfairLock<Box>
/// Starts routed local, with the pointer believed to be at `virtualCursor`.
public init(
_ snapshot: RoutingSnapshot = RoutingSnapshot(),
virtualCursor: CGPoint = Coordinates.point(0, 0)
) {
box = OSAllocatedUnfairLock(initialState: Box(snapshot: snapshot, virtualCursor: virtualCursor))
}
/// A copy of the current snapshot. Called once per captured event.
public func read() -> RoutingSnapshot {
box.withLock { $0.snapshot }
}
/// Replaces the snapshot. Called by the Engine on every switch and whenever
/// settings change.
public func write(_ snapshot: RoutingSnapshot) {
box.withLock { $0.snapshot = snapshot }
}
/// Edits the snapshot in place. The closure runs with the lock held, so it
/// must not block or call back into the Engine.
public func update(_ body: @Sendable (inout RoutingSnapshot) -> Void) {
box.withLock { body(&$0.snapshot) }
}
/// Routes control back to this Mac and stops swallowing — the first half of
/// every recovery path in contracts C2, safe to call from the tap callback.
///
/// Returns `true` if this actually changed anything, so a caller can skip
/// telling the Engine about a no-op.
@discardableResult
public func routeLocal() -> Bool {
box.withLock { state in
let local = state.snapshot.routedLocal()
guard local != state.snapshot else { return false }
state.snapshot = local
return true
}
}
/// Where the tap believes the pointer is, in this Mac's screen coordinates.
public var virtualCursor: CGPoint {
get { box.withLock { $0.virtualCursor } }
set { box.withLock { $0.virtualCursor = newValue } }
}
/// Moves the virtual cursor by a raw device delta and clamps it to one pixel
/// outside `primary`, returning the new position.
///
/// The clamp is Windows' (`InputHook.cs:293-309`): while a remote machine is
/// controlled the virtual pointer may sit one pixel beyond each edge of the
/// controller's primary monitor, which is exactly what edge detection needs
/// to see to trigger the next switch, and no further.
public func advanceVirtualCursor(dx: Double, dy: Double, clampedTo primary: CGRect) -> CGPoint {
let bounds = Coordinates.bounds(primary)
return box.withLock { state in
let x = clamp(state.virtualCursor.x + dx, low: bounds.left, high: bounds.right)
let y = clamp(state.virtualCursor.y + dy, low: bounds.top, high: bounds.bottom)
state.virtualCursor = CGPoint(x: x, y: y)
return state.virtualCursor
}
}
private func clamp(_ value: CGFloat, low: Int, high: Int) -> CGFloat {
if value < CGFloat(low) { return CGFloat(low - 1) }
if value > CGFloat(high) { return CGFloat(high + 1) }
return value
}
}