From 23df7fbbc2e9f48354a80898cc17ea1315a728a3 Mon Sep 17 00:00:00 2001 From: Ian Ynda-Hummel Date: Mon, 27 May 2024 20:18:44 -0400 Subject: [PATCH] Apply yabai's fix for moving windows between spaces in macOS 14.5 (#1648) --- .../xcshareddata/swiftpm/Package.resolved | 3 +- Amethyst/Model/Window.swift | 36 +++++++++++++++++-- 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/Amethyst.xcworkspace/xcshareddata/swiftpm/Package.resolved b/Amethyst.xcworkspace/xcshareddata/swiftpm/Package.resolved index 8acf1ea6..3d16718f 100644 --- a/Amethyst.xcworkspace/xcshareddata/swiftpm/Package.resolved +++ b/Amethyst.xcworkspace/xcshareddata/swiftpm/Package.resolved @@ -1,4 +1,5 @@ { + "originHash" : "43f1b1e9bfd866a37b073557f24d496c85ab3a7508ae523528cda0c3afb30aa9", "pins" : [ { "identity" : "cartography", @@ -127,5 +128,5 @@ } } ], - "version" : 2 + "version" : 3 } diff --git a/Amethyst/Model/Window.swift b/Amethyst/Model/Window.swift index 70a37f9a..fb019409 100644 --- a/Amethyst/Model/Window.swift +++ b/Amethyst/Model/Window.swift @@ -22,6 +22,12 @@ func SLPSPostEventRecordTo(_ psn: inout ProcessSerialNumber, _ bytes: inout UInt @_silgen_name("SLSMoveWindowsToManagedSpace") func SLSMoveWindowsToManagedSpace(_ cid: Int32, _ window_ids: CFArray, _ sid: Int) +@_silgen_name("SLSSpaceSetCompatID") +func SLSSpaceSetCompatID(_ cid: Int32, _ sid: Int, _ workspace: Int32) -> CGError + +@_silgen_name("SLSSetWindowListWorkspace") +func SLSSetWindowListWorkspace(_ cid: Int32, _ window_ids: UnsafePointer, _ window_count: Int32, _ workspace: Int32) -> CGError + let kCPSUserGenerated: UInt32 = 0x200 let kCPSNoWindows: UInt32 = 0x400 // swiftlint:enable identifier_name @@ -292,7 +298,7 @@ extension AXWindow: WindowType { What a mess. See: https://github.com/Hammerspoon/hammerspoon/issues/370#issuecomment-545545468 */ @discardableResult override func focus() -> Bool { - var pid = self.pid() + let pid = self.pid() var wid = self.cgID() var psn = ProcessSerialNumber() let status = GetProcessForPID(pid, &psn) @@ -367,6 +373,32 @@ extension AXWindow: WindowType { } func move(toSpace spaceID: CGSSpaceID) { - SLSMoveWindowsToManagedSpace(CGSMainConnectionID(), [cgID()] as CFArray, spaceID) + if ProcessInfo.processInfo.isOperatingSystemAtLeast(OperatingSystemVersion(majorVersion: 14, minorVersion: 5, patchVersion: 0)) { + /* + See: + - https://github.com/ianyh/Amethyst/issues/1643 + - https://github.com/koekeishiya/yabai/issues/2240 + - https://github.com/koekeishiya/yabai/commit/98bbdbd1363f27d35f09338cded0de1ec010d830 + */ + var error: CGError = .success + + error = SLSSpaceSetCompatID(CGSMainConnectionID(), spaceID, 0x79616265) + defer { _ = SLSSpaceSetCompatID(CGSMainConnectionID(), spaceID, 0x0) } + guard error == .success else { + log.error("failed to set compat aside id: \(error)") + return + } + + var id = cgID() + error = withUnsafeMutablePointer(to: &id, { pointer -> CGError in + return SLSSetWindowListWorkspace(CGSMainConnectionID(), pointer, 1, 0x79616265) + }) + guard error == .success else { + log.error("failed to throw window: \(error)") + return + } + } else { + SLSMoveWindowsToManagedSpace(CGSMainConnectionID(), [cgID()] as CFArray, spaceID) + } } }