input-capture: coalescing looks back two entries

In remote mode one physical move queues a .mouse and an .edgeCandidate,
so matching only the last entry meant the kinds alternated and motion was
never coalesced -- the ring would fill at two entries per move whenever
the Engine stalled, which is the case C3's coalescing exists to survive.
The scan stops at the first non-motion entry so transitions keep order.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
This commit is contained in:
lucioandClaude Opus 5 committed 2026-09-16 02:19:38 -07:00
1 parent fd1ceeac08
commit 84aa59a512
2 files changed
+50 -9

No files matched your search

+37 -6
View File
@@ -50,14 +50,16 @@ public final class CaptureQueue: Sendable {
ring = OSAllocatedUnfairLock(initialState: Ring(slots: Array(repeating: nil, count: size)))
}
/// Appends one event, coalescing it into the previous one where contracts C3
/// Appends one event, coalescing it into a recent 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
if let target = coalesceTarget(state, input),
let merged = coalesce(state.slots[target], input)
{
state.slots[target] = merged
return true
}
guard state.count < capacity else { return false }
@@ -106,9 +108,38 @@ public final class CaptureQueue: Sendable {
/// 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
/// Which queued entry `input` should merge into, if any.
///
/// The search looks back two entries, not one, because one physical mouse
/// move produces a `.mouse` *and* an `.edgeCandidate` while a peer is being
/// controlled: matching only the last entry would find the wrong kind every
/// time and coalescing would never happen. It stops at the first entry that
/// is not itself motion, so a button or key transition is never reordered
/// past.
private func coalesceTarget(_ state: Ring, _ input: CapturedInput) -> Int? {
var offset = state.count - 1
let limit = max(0, state.count - 2)
while offset >= limit {
let index = (state.head + offset) % capacity
let slot = state.slots[index]
if coalesce(slot, input) != nil { return index }
guard let slot, isMotion(slot) else { return nil }
offset -= 1
}
return nil
}
private func isMotion(_ input: CapturedInput) -> Bool {
switch input {
case .edgeCandidate:
return true
case .mouse(let data):
return data.flags == WindowsMessage.mouseMove
|| data.flags == WindowsMessage.wheel
|| data.flags == WindowsMessage.horizontalWheel
default:
return false
}
}
/// The two coalescing rules of contracts C3, or `nil` when the new event must