public import CoreGraphics import Darwin /// Experimental compositor brightness offsets. A successful status does not verify the effect. public struct Brightness { public enum Result: Equatable, Sendable { case unverified case noWindows } public enum Failure: Error, Equatable, Sendable { case invalidOffset case invalidWindowID(CGWindowID) case tooManyWindows case symbolUnavailable(String) case connectionUnavailable case invalidBuffer case status(Int32) } struct Dependencies { let lookup: (String) -> UnsafeMutableRawPointer? let connection: (UnsafeMutableRawPointer) -> Int32 let set: ( UnsafeMutableRawPointer, Int32, UnsafeMutablePointer, UnsafeMutablePointer, Int32 ) -> Int32 } private let dependencies: Dependencies public init() { let library = BrightnessLibrary() dependencies = Dependencies( lookup: { library.lookup($0) }, connection: { pointer in unsafeBitCast(pointer, to: BrightnessConnection.self)() }, set: { pointer, connection, windows, offsets, count in unsafeBitCast(pointer, to: SetWindowBrightness.self)(connection, windows, offsets, count) } ) } init(dependencies: Dependencies) { self.dependencies = dependencies } /// Accepts finite offsets from -1 to 1; zero requests normal brightness. /// The allowed range is conservative because the private API is undocumented. public func set(windowIDs: [CGWindowID], offset: Float) throws -> Result { guard offset.isFinite, (-1...1).contains(offset) else { throw Failure.invalidOffset } for id in windowIDs where id == kCGNullWindowID { throw Failure.invalidWindowID(id) } guard let count = Int32(exactly: windowIDs.count) else { throw Failure.tooManyWindows } guard count != 0 else { return .noWindows } guard let setter = dependencies.lookup("CGSSetWindowListBrightness") else { throw Failure.symbolUnavailable("CGSSetWindowListBrightness") } guard let connection = dependencies.lookup("SLSMainConnectionID") ?? dependencies.lookup("CGSMainConnectionID") else { throw Failure.symbolUnavailable("SLSMainConnectionID or CGSMainConnectionID") } let connectionID = dependencies.connection(connection) guard connectionID != 0 else { throw Failure.connectionUnavailable } var windows = windowIDs.map(Int.init) var offsets = [Float](repeating: offset, count: windows.count) let status = try windows.withUnsafeMutableBufferPointer { windowBuffer in try offsets.withUnsafeMutableBufferPointer { offsetBuffer in guard let windowPointer = windowBuffer.baseAddress, let offsetPointer = offsetBuffer.baseAddress else { throw Failure.invalidBuffer } return dependencies.set(setter, connectionID, windowPointer, offsetPointer, count) } } guard status == 0 else { throw Failure.status(status) } return .unverified } /// Requests a reset for the supplied WindowServer window IDs, such as at launch. public func resetAll(windowIDs: [CGWindowID]) throws -> Result { try set(windowIDs: windowIDs, offset: 0) } /// Enumerates all windows for a launch-time reset. The reset remains unverified. public func resetAll(using windowServer: WindowServer = WindowServer()) throws -> Result { try resetAll(windowIDs: windowServer.windows().map(\.number)) } } // The available external declaration uses C long window IDs and C float offsets. // Private ABI and behavior may change between macOS releases. private typealias BrightnessConnection = @convention(c) () -> Int32 private typealias SetWindowBrightness = @convention(c) ( Int32, UnsafeMutablePointer, UnsafeMutablePointer, Int32 ) -> Int32 private final class BrightnessLibrary { private let handle = dlopen("/System/Library/PrivateFrameworks/SkyLight.framework/SkyLight", RTLD_LAZY | RTLD_LOCAL) func lookup(_ name: String) -> UnsafeMutableRawPointer? { guard let handle else { return nil } return dlsym(handle, name) } deinit { if let handle { dlclose(handle) } } }