From b6ab0c979faedf2bc7aed9afb6fce89c270640c3 Mon Sep 17 00:00:00 2001 From: John Song Date: Sun, 29 Nov 2026 22:23:41 +0900 Subject: [PATCH] Fix remote trackpad cursor drifting on multi-display setups The mouse move and click handlers flipped NSEvent.mouseLocation into CoreGraphics coordinates using the height of NSScreen.main (cached once at launch for moves). CoreGraphics global coordinates are relative to the primary display, so when the focused screen at launch has a different height than the primary one, every mouse_move adds that height difference and the cursor slides in one direction (e.g. straight down with a 1080pt TV next to a 956pt built-in display). Clicks land off by the same amount. Read the current position with CGEvent(source: nil)?.location, which is already in global CoreGraphics coordinates. Co-Authored-By: Claude Opus 5.5 --- .../Core/Util/Remote/MacRemoteManager.swift | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/airsync-mac/Core/Util/Remote/MacRemoteManager.swift b/airsync-mac/Core/Util/Remote/MacRemoteManager.swift index 0ccb1576..0b612dfa 100644 --- a/airsync-mac/Core/Util/Remote/MacRemoteManager.swift +++ b/airsync-mac/Core/Util/Remote/MacRemoteManager.swift @@ -17,7 +17,6 @@ class MacRemoteManager: ObservableObject { @Published var lastVolumeLevel: Int = 0 private var volumeCheckTimer: Timer? - private var cachedScreenHeight: CGFloat = 1080 private var isMonitoring = false // Key codes @@ -48,7 +47,6 @@ class MacRemoteManager: ObservableObject { private init() { // Initialize last known volume self.lastVolumeLevel = getVolume() - self.cachedScreenHeight = NSScreen.main?.frame.height ?? 1080 } deinit { @@ -89,11 +87,9 @@ class MacRemoteManager: ObservableObject { // MARK: - Input Simulation func simulateMouseRelativeMove(dx: CGFloat, dy: CGFloat) { - let mouseLoc = NSEvent.mouseLocation - - // Convert Cocoa coordinates (bottom-left) to CoreGraphics (top-left) - // Use cached screen height to avoid querying screen frame 100 times/sec - let currentPos = CGPoint(x: mouseLoc.x, y: cachedScreenHeight - mouseLoc.y) + // CGEvent's location is already in global CoreGraphics coordinates (top-left of the primary display), + // so it stays correct with multiple displays, unlike flipping NSEvent.mouseLocation with one screen's height. + let currentPos = CGEvent(source: nil)?.location ?? .zero let newPos = CGPoint(x: currentPos.x + dx, y: currentPos.y + dy) if let event = CGEvent(mouseEventSource: nil, mouseType: .mouseMoved, mouseCursorPosition: newPos, mouseButton: .left) { @@ -102,9 +98,7 @@ class MacRemoteManager: ObservableObject { } func simulateMouseClick(button: CGMouseButton, isDown: Bool) { - let mouseLoc = NSEvent.mouseLocation - let screenFrame = NSScreen.main?.frame ?? .zero - let currentPos = CGPoint(x: mouseLoc.x, y: screenFrame.height - mouseLoc.y) + let currentPos = CGEvent(source: nil)?.location ?? .zero let mouseType: CGEventType switch button {