diff --git a/Sources/MWBCore/Switching.swift b/Sources/MWBCore/Switching.swift index b8e66e8..3ada6a2 100644 --- a/Sources/MWBCore/Switching.swift +++ b/Sources/MWBCore/Switching.swift @@ -28,10 +28,20 @@ public struct Jump: Sendable, Equatable { public enum SwitchAction: Sendable, Equatable { /// Queue this packet on every connection (`Des` is already set). case send(Packet) - /// Park the real cursor at the top centre of the primary screen - /// (`HideMouseCursor`, `Common.cs:1482-1496`). `byHideMouseMessage` is the - /// Windows flag: true when a peer asked with `HideMouse (50)`. - case parkCursor(byHideMouseMessage: Bool) + /// Park the cursor at the top centre of the primary screen — the "hidden" + /// position the app also remembers as `LastPos` (`HideMouseCursor`, + /// `Common.cs:1482-1496`). + /// + /// **Record the position always; move the real OS cursor only when + /// `destinationIsRemote || byHideMouseMessage`.** That is the whole of the + /// `Common.cs:1488` gate, pre-evaluated here so no consumer has to re-derive + /// it from `desMachineID`: `destinationIsRemote` is + /// `desMachineID != localID && desMachineID != ID.ALL` *after* the switch, + /// and `byHideMouseMessage` is true only when a peer asked with + /// `HideMouse (50)`. Moving the cursor when both are false is a visible bug: + /// control coming home (a `ByeBye`, a disconnect, a hotkey back to this + /// machine) would jerk the user's own cursor to the top of the screen. + case parkCursor(destinationIsRemote: Bool, byHideMouseMessage: Bool) /// Put the cursor at this switch location; pixel-tagged values mean pixels /// (`Coordinates.isPixels`, `Helper.cs:100-115`). case moveCursor(UniversalPoint) @@ -107,6 +117,13 @@ public final class Switcher { /// True while input goes to every machine at once (`ID.ALL`). public var isBroadcasting: Bool { desMachineID == MachineID.all } + /// Whether the machine currently receiving input is a remote one — the + /// `desMachineID != MachineID && desMachineID != ID.ALL` half of the + /// `HideMouseCursor` gate (`Common.cs:1488`). + public var destinationIsRemote: Bool { + desMachineID != localID && desMachineID != MachineID.all + } + /// Whether a machine can be reached right now: itself always, others only /// with a live socket (`Common.cs:852-855`). public func isConnected(to id: UInt32) -> Bool { @@ -197,7 +214,10 @@ public final class Switcher { let centre = Coordinates.centre(of: edge.config.primary) // MachineStuff.cs:1021-1022 actions.append(.moveCursor(Coordinates.tagPixels(centre))) - actions.append(.parkCursor(byHideMouseMessage: false)) // MachineStuff.cs:1025 + // HideMouseCursor(false) after DesMachineID is set (MachineStuff.cs:1025), + // so the gate reads the new destination, not the old one. + actions.append( + .parkCursor(destinationIsRemote: destinationIsRemote, byHideMouseMessage: false)) actions.append(.desMachineChanged(id: desMachineID, name: desMachineName)) return actions } @@ -298,7 +318,10 @@ public final class Switcher { /// Handles `HideMouse (50)`: a peer took control, so park our cursor and let /// go of every modifier (`Receiver.cs:383-388`). public func handleHideMouse() -> [SwitchAction] { - [.parkCursor(byHideMouseMessage: true), .releaseAllKeys] + [ + .parkCursor(destinationIsRemote: destinationIsRemote, byHideMouseMessage: true), + .releaseAllKeys, + ] } /// A connection dropped: back-date the machine so it reads as dead, and take diff --git a/docs/features/session-matrix/README.md b/docs/features/session-matrix/README.md index 77bb029..b664fbb 100644 --- a/docs/features/session-matrix/README.md +++ b/docs/features/session-matrix/README.md @@ -268,7 +268,7 @@ public struct Jump: Sendable, Equatable { } public enum SwitchAction: Sendable, Equatable { case send(Packet) // already addressed - case parkCursor(byHideMouseMessage: Bool) + case parkCursor(destinationIsRemote: Bool, byHideMouseMessage: Bool) case moveCursor(UniversalPoint) // pixel-tagged means pixels case releaseAllKeys case desMachineChanged(id: UInt32, name: String?) @@ -287,6 +287,7 @@ public final class Switcher { // not Sendable: app-wide public private(set) var desMachineName: String? public private(set) var newDesMachineID: UInt32 public var isBroadcasting: Bool { get } + public var destinationIsRemote: Bool { get } // the HideMouseCursor gate public func isConnected(to id: UInt32) -> Bool public func crossing(at point: CGPoint, now: TimeInterval) -> Jump? public func prepareToSwitch(to:landing:now:) -> [SwitchAction] @@ -317,6 +318,17 @@ Rules the consuming lanes must honour: **not** `Session.send(_:)` — one broadcast must keep one `Id` across a peer's two connections or the receiver's replay ring will not collapse the duplicate. +- **`.parkCursor` records a position; it moves the OS cursor only when + `destinationIsRemote || byHideMouseMessage`.** `Switcher` pre-evaluates + both halves of the `HideMouseCursor` gate (`Common.cs:1488`) so no + consumer needs `desMachineID`, and `destinationIsRemote` is computed + *after* the switch, as `SwitchToMachine` does (`MachineStuff.cs:1025`). + The hidden position (primary screen, horizontal centre, `y = 4` with + `HideMouse` on, else vertical centre) is remembered as `LastPos` every + time, because that is what the cursor is restored to. An injector that + moves unconditionally will jerk the local cursor to the top of the + screen every time control comes home — on every `ByeBye` and every peer + disconnect (`Common.cs:622-625`). - **`Matrix.live(connectedNames:)` must be given the local machine's own name**, because the Windows `IsConnectedTo` answers yes for it without a socket. `Switcher.crossing` adds it; a direct caller must too.