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]>
This commit is contained in:
1 parent
c4c04914b3
commit
d2d221fe29
2 files changed
+235
No files matched your search
@@ -0,0 +1,137 @@
|
||||
// The bounded ring the event-tap callback pushes into and the Engine drains
|
||||
// (contracts C3).
|
||||
//
|
||||
// The callback may not allocate, may not block and may not await, so it cannot
|
||||
// hand a value to an actor directly. It appends to a fixed-size ring under an
|
||||
// uncontended `OSAllocatedUnfairLock` and returns; the Engine drains on its own
|
||||
// task. The ring lives in `MWBMac` rather than `MWBCore` because it needs that
|
||||
// lock, and `MWBCore` may import Foundation only (README D2).
|
||||
//
|
||||
// Overflow policy, from contracts C3: pointer motion is coalesced, so a busy
|
||||
// mouse cannot fill the ring; key and button transitions are never dropped, and
|
||||
// if one cannot be enqueued the tap routes control local instead, because losing
|
||||
// a key-up on a remote machine is worse than losing control.
|
||||
|
||||
import Foundation
|
||||
import MWBCore
|
||||
import os
|
||||
|
||||
/// Something the tap must tell the Engine that is not itself an input event and
|
||||
/// must never be lost to a full ring.
|
||||
public enum CaptureNotice: Sendable {
|
||||
/// The tap was disabled and re-enabled; held-key state is unknown.
|
||||
case tapLost
|
||||
/// The ring filled and input was dropped.
|
||||
case overflow
|
||||
}
|
||||
|
||||
/// A fixed-capacity queue of captured input, safe to push to from the tap
|
||||
/// callback and to drain from the Engine.
|
||||
public final class CaptureQueue: Sendable {
|
||||
/// The default depth: ~4 seconds of a fast mouse, before coalescing
|
||||
/// (contracts C3).
|
||||
public static let defaultCapacity = 4096
|
||||
|
||||
private struct Ring: Sendable {
|
||||
var slots: [CapturedInput?]
|
||||
var head = 0
|
||||
var count = 0
|
||||
var tapLost = false
|
||||
var overflowed = false
|
||||
}
|
||||
|
||||
private let capacity: Int
|
||||
private let ring: OSAllocatedUnfairLock<Ring>
|
||||
|
||||
/// A queue holding at most `capacity` events.
|
||||
public init(capacity: Int = CaptureQueue.defaultCapacity) {
|
||||
let size = max(1, capacity)
|
||||
self.capacity = size
|
||||
ring = OSAllocatedUnfairLock(initialState: Ring(slots: Array(repeating: nil, count: size)))
|
||||
}
|
||||
|
||||
/// Appends one event, coalescing it into the previous one where contracts C3
|
||||
/// allows. Returns `false` if the ring was full and the event was dropped —
|
||||
/// the caller must then route control local and `signal(.overflow)`.
|
||||
@discardableResult
|
||||
public func push(_ input: CapturedInput) -> Bool {
|
||||
ring.withLock { state in
|
||||
if let last = lastIndex(of: state), let merged = coalesce(state.slots[last], input) {
|
||||
state.slots[last] = merged
|
||||
return true
|
||||
}
|
||||
guard state.count < capacity else { return false }
|
||||
state.slots[(state.head + state.count) % capacity] = input
|
||||
state.count += 1
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
/// Raises a notice that `drain()` will report even if the ring is full.
|
||||
public func signal(_ notice: CaptureNotice) {
|
||||
ring.withLock { state in
|
||||
switch notice {
|
||||
case .tapLost: state.tapLost = true
|
||||
case .overflow: state.overflowed = true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Everything queued since the last drain, oldest first, with any raised
|
||||
/// notices appended — they describe the state the tap is in *now*, so they
|
||||
/// belong after the events they interrupted.
|
||||
public func drain() -> [CapturedInput] {
|
||||
ring.withLock { state in
|
||||
var result: [CapturedInput] = []
|
||||
result.reserveCapacity(state.count + 2)
|
||||
for offset in 0..<state.count {
|
||||
let index = (state.head + offset) % capacity
|
||||
if let value = state.slots[index] { result.append(value) }
|
||||
state.slots[index] = nil
|
||||
}
|
||||
state.head = 0
|
||||
state.count = 0
|
||||
if state.tapLost {
|
||||
result.append(.tapLost)
|
||||
state.tapLost = false
|
||||
}
|
||||
if state.overflowed {
|
||||
result.append(.overflow)
|
||||
state.overflowed = false
|
||||
}
|
||||
return result
|
||||
}
|
||||
}
|
||||
|
||||
/// How many events are waiting. For the menu bar and the perf notes only.
|
||||
public var depth: Int { ring.withLock { $0.count } }
|
||||
|
||||
private func lastIndex(of state: Ring) -> Int? {
|
||||
guard state.count > 0 else { return nil }
|
||||
return (state.head + state.count - 1) % capacity
|
||||
}
|
||||
|
||||
/// The two coalescing rules of contracts C3, or `nil` when the new event must
|
||||
/// be queued on its own. Only ever merges like with like: a transition never
|
||||
/// disappears into a move.
|
||||
private func coalesce(_ previous: CapturedInput?, _ next: CapturedInput) -> CapturedInput? {
|
||||
switch (previous, next) {
|
||||
case (.edgeCandidate, .edgeCandidate):
|
||||
return next // the latest pointer position is the only one that matters
|
||||
case (.mouse(let old), .mouse(let new)):
|
||||
if old.flags == WindowsMessage.mouseMove && new.flags == WindowsMessage.mouseMove {
|
||||
return next
|
||||
}
|
||||
if old.flags == new.flags,
|
||||
new.flags == WindowsMessage.wheel || new.flags == WindowsMessage.horizontalWheel
|
||||
{
|
||||
var summed = new
|
||||
summed.wheelDelta = old.wheelDelta &+ new.wheelDelta
|
||||
return .mouse(summed)
|
||||
}
|
||||
return nil
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user