// Keeping this Mac's display awake while somebody drives it from another // machine — the receiving half of `BlockScreenSaverOnOtherMachines`. // // Windows: a peer that has seen real input since its previous beat sends // `Awake (21)` instead of `Heartbeat (20)` (`Common.SendAwakeBeat`, // `Common.cs:545-559`, docs/protocol.md §3.3), and the receiver answers it with // `Common.HumanBeingDetected()` (`Receiver.cs:245-248`). That method // (`Common.cs:561-572`) checks whether *local* input has happened since its own // previous call and, if not, runs `PokeMyself()` (`:574-591`): ten ±9-pixel // relative mouse jiggles, 50 ms apart, stopping early if the local user moves. // The jiggle is there to reset the Windows idle timer, which is the only lever // a user-mode process has. // // macOS has the lever directly: a power assertion. `kIOPMAssertionTypePrevent // UserIdleDisplaySleep` holds the display on without touching the pointer, which // also keeps CLAUDE.md invariant 5 — nothing here posts an event. The assertion // carries its own 90 s timeout and is re-created by each `Awake`, so it lapses // on its own about half a beat after the beats stop (60 s cadence, §3.3). // // `IOKit` is imported here and nowhere else in the port. import Foundation import IOKit.pwr_mgt import os /// Holds the display awake for as long as `Awake` packets keep arriving. /// /// One per process, owned by the Engine. Thread-safe: `renew()` is called from /// the Engine actor and `release()` also from the shutdown path. public final class DisplayAwake: Sendable { /// How long one `Awake` packet is worth. A peer beats every 60 s while it is /// blocking screen savers (`Common.cs:547`, §3.3), so one and a half beats /// of slack keeps the display on through a late packet and lets the /// assertion lapse by itself when the peer goes quiet. public static let timeout: TimeInterval = 90 /// What the assertion is called in `pmset -g assertions`. static let assertionName = "Mouse Without Borders: a remote user is active" private let assertion: OSAllocatedUnfairLock /// A releaser that holds nothing yet. public init() { assertion = OSAllocatedUnfairLock(initialState: nil) } /// Whether an assertion is being held right now. `false` says nothing about /// the display: the assertion may simply have timed out in the kernel. public var isAsserted: Bool { assertion.withLock { $0 != nil } } /// Takes a fresh assertion and drops the previous one, in that order so the /// display is never momentarily unasserted. /// /// Call this on every `Awake (21)`, never on a plain `Heartbeat (20)` — /// that distinction is the whole of the sender's /// `BlockScreenSaverOnOtherMachines` setting, and the receiver honours it /// unconditionally, exactly as `Receiver.cs:245-248` does. public func renew(seconds: TimeInterval = DisplayAwake.timeout) { guard let fresh = DisplayAwake.create(seconds: seconds) else { return } let previous = assertion.withLock { held -> IOPMAssertionID? in let previous = held held = fresh return previous } if let previous { IOPMAssertionRelease(previous) } } /// Drops the assertion now. Idempotent, and safe to call when none is held. public func release() { let previous = assertion.withLock { held -> IOPMAssertionID? in let previous = held held = nil return previous } if let previous { IOPMAssertionRelease(previous) } } /// One assertion with its own expiry, so a lost `release()` cannot leave a /// Mac awake for ever. private static func create(seconds: TimeInterval) -> IOPMAssertionID? { let properties: [String: Any] = [ kIOPMAssertionTypeKey: kIOPMAssertionTypePreventUserIdleDisplaySleep, kIOPMAssertionNameKey: assertionName, kIOPMAssertionLevelKey: kIOPMAssertionLevelOn, kIOPMAssertionTimeoutKey: seconds, kIOPMAssertionTimeoutActionKey: kIOPMAssertionTimeoutActionRelease, ] var id = IOPMAssertionID(0) let result = IOPMAssertionCreateWithProperties(properties as CFDictionary, &id) guard result == kIOReturnSuccess else { Log.app.error("could not hold the display awake: IOKit error \(result)") return nil } return id } }