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
+13 -3
View File
@@ -43,9 +43,12 @@ code does not enforce is here.
`swallowKeys`/`swallowMouse` are set by the Engine, not derived here.
- **The queue is a bounded ring of 4096 `CapturedInput`s** in
`MWBMac/CaptureQueue.swift` (it needs a lock, which `MWBCore` may not
import). Motion is coalesced on push when the previous entry is also motion —
latest position wins for an absolute move, deltas are **summed** for a
relative one, and wheel deltas of the same axis are summed. A push that
import). Motion is coalesced on push against either of the last two entries —
two, because one physical move queues a `.mouse` *and* an `.edgeCandidate`
while a peer is controlled — stopping at the first entry that is not motion,
so a transition is never reordered past. Latest position wins for an absolute
move, deltas are **summed** for a relative one, and wheel deltas of the same
axis are summed. A push that
cannot be satisfied routes control local and raises `.overflow`, which
`drain()` reports even when the ring is full (contracts C3).
- **Keyboard encoding**: `wVk` from `KeyMap.vk(forKeycode:)`; `dwFlags` =
@@ -401,6 +404,13 @@ Decisions, in the order they were made:
9. **A relative move is coalesced by summing.** The first version replaced the
older entry, which is right for an absolute position and loses motion for a
`MOVE_MOUSE_RELATIVE` delta. Fixed in the same lane.
10. **Coalescing looks back two entries.** The first version matched only the
last one, and in remote mode every physical move queues a `.mouse` followed
by an `.edgeCandidate`, so the kinds alternate and nothing ever merged — the
ring would have filled at two entries per move under a stalled Engine, which
is precisely the case coalescing exists to survive. The scan stops at the
first non-motion entry, so a button or key transition is never reordered
past.
Deviations from the brief: the brief listed the device masks 0x01..0x40 and
omitted `NX_DEVICERCTLKEYMASK 0x2000`, without which the right Control key