Ariver
2026-08-14 86f0b4bd20cdec86dfb5bdb7978d1f6ff4af111a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import Foundation
 
struct PrivateSpaceFocusOutcome {
    let targetSpaceID: UInt64
    let displayIdentifier: String?
    let previousCurrentSpaceID: UInt64?
    let didRequestFocus: Bool
    let error: String?
}
 
final class PrivateSpaceActivationBridge: @unchecked Sendable {
    enum BridgeError: Error {
        case frameworkUnavailable
        case symbolUnavailable(String)
    }
 
    private typealias CGSMainConnectionIDFunction = @convention(c) () -> UInt32
    private typealias CGSCopyManagedDisplaySpacesFunction = @convention(c) (UInt32) -> Unmanaged<CFArray>?
    private typealias CGSManagedDisplaySetCurrentSpaceFunction = @convention(c) (UInt32, CFString, UInt64) -> Void
 
    static let shared = try? PrivateSpaceActivationBridge()
 
    private let handle: UnsafeMutableRawPointer?
    private let mainConnectionID: CGSMainConnectionIDFunction
    private let copyManagedDisplaySpaces: CGSCopyManagedDisplaySpacesFunction
    private let setCurrentSpace: CGSManagedDisplaySetCurrentSpaceFunction
 
    private init() throws {
        guard let handle = dlopen("/System/Library/PrivateFrameworks/SkyLight.framework/SkyLight", RTLD_LAZY) else {
            throw BridgeError.frameworkUnavailable
        }
 
        self.handle = handle
        self.mainConnectionID = try Self.symbol("CGSMainConnectionID", in: handle)
        self.copyManagedDisplaySpaces = try Self.symbol("CGSCopyManagedDisplaySpaces", in: handle)
        self.setCurrentSpace = try Self.symbol("CGSManagedDisplaySetCurrentSpace", in: handle)
    }
 
    deinit {
        if let handle {
            dlclose(handle)
        }
    }
 
    func focus(spaceIDs: [UInt64]) -> PrivateSpaceFocusOutcome? {
        guard let targetSpaceID = spaceIDs.first else { return nil }
 
        let connectionID = mainConnectionID()
        guard
            let unmanagedDisplays = copyManagedDisplaySpaces(connectionID),
            let displayRecords = unmanagedDisplays.takeRetainedValue() as? [[String: Any]]
        else {
            return PrivateSpaceFocusOutcome(
                targetSpaceID: targetSpaceID,
                displayIdentifier: nil,
                previousCurrentSpaceID: nil,
                didRequestFocus: false,
                error: "managedDisplaySpacesUnavailable"
            )
        }
 
        for displayRecord in displayRecords {
            let displayIdentifier = stringValue(displayRecord["Display Identifier"])
            let spaces = displayRecord["Spaces"] as? [[String: Any]] ?? []
            guard spaces.contains(where: { spaceID(from: $0) == targetSpaceID }) else {
                continue
            }
 
            let previousCurrentSpaceID = (displayRecord["Current Space"] as? [String: Any]).flatMap(spaceID)
            guard !displayIdentifier.isEmpty else {
                return PrivateSpaceFocusOutcome(
                    targetSpaceID: targetSpaceID,
                    displayIdentifier: nil,
                    previousCurrentSpaceID: previousCurrentSpaceID,
                    didRequestFocus: false,
                    error: "displayIdentifierMissing"
                )
            }
 
            if previousCurrentSpaceID == targetSpaceID {
                return PrivateSpaceFocusOutcome(
                    targetSpaceID: targetSpaceID,
                    displayIdentifier: displayIdentifier,
                    previousCurrentSpaceID: previousCurrentSpaceID,
                    didRequestFocus: false,
                    error: nil
                )
            }
 
            setCurrentSpace(connectionID, displayIdentifier as CFString, targetSpaceID)
            return PrivateSpaceFocusOutcome(
                targetSpaceID: targetSpaceID,
                displayIdentifier: displayIdentifier,
                previousCurrentSpaceID: previousCurrentSpaceID,
                didRequestFocus: true,
                error: nil
            )
        }
 
        return PrivateSpaceFocusOutcome(
            targetSpaceID: targetSpaceID,
            displayIdentifier: nil,
            previousCurrentSpaceID: nil,
            didRequestFocus: false,
            error: "targetSpaceNotFound"
        )
    }
 
    private static func symbol<T>(_ name: String, in handle: UnsafeMutableRawPointer) throws -> T {
        guard let rawSymbol = dlsym(handle, name) else {
            throw BridgeError.symbolUnavailable(name)
        }
 
        return unsafeBitCast(rawSymbol, to: T.self)
    }
 
    private func spaceID(from record: [String: Any]) -> UInt64? {
        uint64Value(record["id64"] ?? record["id"])
    }
 
    private func stringValue(_ value: Any?) -> String {
        if let string = value as? String { return string }
        if let number = value as? NSNumber { return number.stringValue }
        return ""
    }
 
    private func uint64Value(_ value: Any?) -> UInt64? {
        if let number = value as? NSNumber { return number.uint64Value }
        if let value = value as? UInt64 { return value }
        if let value = value as? UInt32 { return UInt64(value) }
        if let value = value as? Int { return UInt64(value) }
        if let string = value as? String { return UInt64(string) }
        return nil
    }
}