Files
lucioandClaude Opus 5 21c886165e extras: awake assertion, fullscreen rule, drawn cursor, transfer notices
- MWBMac/Awake.swift: DisplayAwake renews an IOPMAssertion
  (PreventUserIdleDisplaySleep, 90 s timeout) on every Awake (21), the
  macOS answer to Common.HumanBeingDetected/PokeMyself
  (Receiver.cs:244-249, Common.cs:561-591) without posting input.
- MWBMac/Fullscreen.swift: the frontmost app's excluded-apps test and a
  covers-a-whole-display window check, cached 250 ms; gated in the
  Engine by IsEasyMouseSwitchAllowed's two early outs
  (Common.cs:1638-1650), so it is asked only on the edge path.
- MWBMac/FakeCursor.swift: CursorOverlay draws NSCursor.arrow at the
  last injected position (Receiver.cs:186-188) and hides on park, local
  input and shutdown (Common.cs:1495, InputHook.cs:256-264); FakeCursor
  coalesces the main-thread hops.
- Settings.drawMouseCursor accessor + the Behaviour-pane toggle,
  answering app-ui's open request.
- UIBridge.TransferNotices wires ClipboardAction.note to the Notifier,
  answering clipboard's open request (Clipboard.cs:610-618, 869-881, 916).

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
2026-09-16 05:25:10 -07:00

97 lines
4.4 KiB
Swift

// 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<IOPMAssertionID?>
/// 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
}
}