From ccbeff2e0a0e340b377ea7dad71c423bba8b3a95 Mon Sep 17 00:00:00 2001 From: Guoliang Chen Date: Wed, 16 Sep 2026 03:55:23 -0700 Subject: [PATCH] app-engine: release held keys on every switch that lands control here MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit QA finding 2. `Helper.HelperThread` releases once `NewDesMachineID == MachineID` (`Helper.cs:120-123`) and its event is set by both `SwitchToMachine` (`MachineStuff.cs:1024`) and `PrepareToSwitchToMachine` (`Event.cs:203`), so Windows releases on the `ByeBye`, edge-crossing and `NextMachine` paths too — we only did on the hotkey, the broadcast toggle and `recoverLocal`. The tail of `perform(_ actions:)` now covers all of them, gated on the destination actually moving so that a switch which leaves control local (a matrix edit while a peer drives this Mac) cannot drop that peer's held modifiers. Co-Authored-By: Claude Opus 5 (1M context) --- Sources/mwb/EngineActions.swift | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/Sources/mwb/EngineActions.swift b/Sources/mwb/EngineActions.swift index c04d42d..4f7675f 100644 --- a/Sources/mwb/EngineActions.swift +++ b/Sources/mwb/EngineActions.swift @@ -24,11 +24,34 @@ extension Engine { /// what tells the tap to start or stop swallowing, what arms the 30 s stall /// rule (control-channel request 2) and what hands the pointer back. func perform(_ actions: [SwitchAction]) { + let wasDes = switcher.desMachineID + let wasBroadcasting = switcher.isBroadcasting for action in actions { perform(action) } + releaseKeysIfControlCameHome(wasDes: wasDes, wasBroadcasting: wasBroadcasting) syncRouting() publish() } + /// `Helper.HelperThread` releases every held key as soon as + /// `NewDesMachineID == MachineID` (`Helper.cs:120-123`), and the event it + /// waits on is set by *every* switch — `SwitchToMachine` + /// (`MachineStuff.cs:1024`) and `PrepareToSwitchToMachine` (`Event.cs:203`) + /// alike. So a received `ByeBye`, an edge crossing and a `NextMachine` + /// packet that land control back on this Mac release too, not only the + /// hotkey and the broadcast toggle, which carry their own + /// `.releaseAllKeys`. Calling it twice is harmless: `releaseAll()` is + /// idempotent. + /// + /// Only on an actual change of target, though. While a peer is driving + /// *this* Mac our own destination is local the whole time, and releasing on + /// a switch that did not move it — a matrix edit, say — would drop the + /// modifiers that peer is holding down, mid-keystroke. + private func releaseKeysIfControlCameHome(wasDes: UInt32, wasBroadcasting: Bool) { + let moved = switcher.desMachineID != wasDes || switcher.isBroadcasting != wasBroadcasting + guard moved, !switcher.destinationIsRemote, !switcher.isBroadcasting else { return } + injector.releaseAll() + } + private func perform(_ action: SwitchAction) { switch action { case .send(let packet):