// The drawn cursor: a small arrow where the remote pointer is, on the Mac that // is being controlled. `DrawMouseCursor` in the settings. // // Windows: `CustomCursor.ShowFakeMouseCursor(x, y)` (`CustomCursor.cs:68-146`) // is one call that both shows and hides — `int.MinValue` for either coordinate // means hide. It is fed from `Receiver.cs:184-188`, which injects the remote // mouse event, reads `GetCursorPos` into `Common.LastX/LastY` and draws there; // and it is called with `int.MinValue` from `Common.HideMouseCursor` // (`Common.cs:1495`), from the three `Helper` form routines (`:192, 224, 244`) // and from the mouse hook when the local user moves their own mouse // (`InputHook.cs:261-263`). The form itself is a 32×32 borderless top-most // window placed at (x+1, y+1) (`CustomCursor.cs:100-103`). // // Two deviations, both recorded in the feature README: // // 1. Windows only draws while the real cursor is hidden or suppressed // (`GetCursorInfo`, `ci.flags != 1`, `CustomCursor.cs:73-79`). macOS has no // read-only equivalent, and a Mac being driven keeps its real pointer // visible because the injected moves move it, so the overlay is a second, // deliberately offset marker rather than a replacement. // 2. `DrawMouseEx` selects a richer drawing path on Windows // (`frmMouseCursor.cs:32-52` paints pixels straight onto the desktop DC). // It is accepted and ignored here; there is no macOS equivalent worth // having. import AppKit import CoreGraphics import Foundation import MWBCore import os /// The Engine's door to the drawn cursor: callable from any isolation, one /// coalesced hop to the main thread per burst of positions. /// /// The overlay is an AppKit window, so it can only be touched on the main /// thread; a controlled Mac receives up to a hundred mouse packets a second and /// one `Task` per packet would both allocate and arrive out of order. Instead /// the latest wanted state lives under a lock and one hop is scheduled at a /// time — whatever the hop finds when it runs is what gets drawn. public final class FakeCursor: Sendable { private struct Wanted: Sendable { var enabled = false var target: CGPoint? var hopScheduled = false } private let wanted = OSAllocatedUnfairLock(initialState: Wanted()) @MainActor private var overlay: CursorOverlay? /// A driver that draws nothing until `setEnabled(true)`. public init() {} /// Follows the `DrawMouseCursor` setting. Turning it off hides the overlay /// at once (`Setting.cs:97-101`, `frmMatrix.cs:1096-1101`). public func setEnabled(_ isOn: Bool) { update { $0.enabled = isOn } } /// `ShowFakeMouseCursor(x, y)`: draw the arrow at this screen position, in /// CoreGraphics coordinates (top-left origin). public func show(at point: CGPoint) { update { $0.target = point } } /// `ShowFakeMouseCursor(int.MinValue, int.MinValue)`: take it away. public func hide() { update { $0.target = nil } } private func update(_ body: @Sendable (inout Wanted) -> Void) { let schedule = wanted.withLock { state -> Bool in body(&state) guard !state.hopScheduled else { return false } state.hopScheduled = true return true } guard schedule else { return } Task { @MainActor in self.draw() } } @MainActor private func draw() { let state = wanted.withLock { state -> Wanted in state.hopScheduled = false return state } guard state.enabled, let target = state.target else { overlay?.hide() return } let overlay = overlay ?? CursorOverlay() self.overlay = overlay overlay.show(at: target) } } /// The borderless, click-through window that draws the arrow. @MainActor public final class CursorOverlay { /// Windows offsets the form by one pixel so it does not sit exactly under /// the real pointer (`CustomCursor.cs:100-101`). static let offset = CGPoint(x: 1, y: 1) private var window: NSWindow? private var isShowing = false /// An overlay that has not been drawn yet. The window is built on the first /// `show(at:)`, so a Mac that is never controlled never makes one. public init() {} /// Puts the arrow at `point`, in CoreGraphics screen coordinates. public func show(at point: CGPoint) { let window = window ?? CursorOverlay.makeWindow() self.window = window window.setFrameOrigin(CursorOverlay.frameOrigin(for: point, size: window.frame.size)) if !isShowing { window.orderFrontRegardless() isShowing = true } } /// Takes the arrow away. Idempotent. public func hide() { guard isShowing else { return } isShowing = false window?.orderOut(nil) } /// CoreGraphics puts (0, 0) at the top-left of the main display and grows /// downwards; AppKit puts it at that display's bottom-left and grows /// upwards, and a window's origin is its *bottom* left. static func frameOrigin(for point: CGPoint, size: CGSize) -> CGPoint { let mainHeight = NSScreen.screens.first?.frame.height ?? 0 let top = point.y + offset.y return CGPoint(x: point.x + offset.x, y: mainHeight - top - size.height) } private static func makeWindow() -> NSWindow { let image = NSCursor.arrow.image let frame = NSRect(origin: .zero, size: image.size) let window = NSWindow( contentRect: frame, styleMask: .borderless, backing: .buffered, defer: false) window.isReleasedWhenClosed = false window.isOpaque = false window.backgroundColor = .clear window.hasShadow = false window.ignoresMouseEvents = true // Above ordinary windows and above a full-screen app's own space, and // present on every space without following the user between them. window.level = .screenSaver window.collectionBehavior = [.canJoinAllSpaces, .stationary, .ignoresCycle] let view = NSImageView(frame: frame) view.image = image view.imageScaling = .scaleNone window.contentView = view return window } }