Atoll/DynamicIsland/managers/SystemDisplayManager.swift
Joseph fc063499ae Fix brightness HUD not working on macOS Tahoe 26.5
The brightness HUD broke on Tahoe because Apple deprecated/changed the
DisplayServices private framework and stopped posting the BezelEngine
distributed notifications that Atoll relied on for change detection.

This commit:
- Adds CoreBrightnessDisplayClient using Apple's CoreBrightness framework
  (mirrors the existing KeyboardBrightnessClient pattern) as the primary
  brightness read/write path
- Falls back gracefully to DisplayServices then IODisplay if CoreBrightness
  is unavailable
- Adds a lightweight polling fallback (150ms) so the HUD fires even when
  no distributed notifications are posted (Control Center, Touch Bar, auto-
  brightness changes)
- Registers the new com.apple.CoreBrightness.DisplayBrightnessChanged
  notification
- Fixes deprecated kIOMasterPortDefault → kIOMainPortDefault in
  SystemDisplayManager.swift

Fixes #452

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-05-18 12:08:01 +01:00

83 lines
No EOL
2.9 KiB
Swift

/*
* Atoll (DynamicIsland)
* Copyright (C) 2024-2026 Atoll Contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import Foundation
import Cocoa
class SystemDisplayManager {
private init() {}
private static var method = SensorMethod.standard
static func getDisplayBrightness() throws -> Float {
switch SystemDisplayManager.method {
case .standard:
do {
return try getStandardDisplayBrightness()
} catch {
method = .m1
}
case .m1:
do {
return try getM1DisplayBrightness()
} catch {
method = .allFailed
}
case .allFailed:
throw SensorError.Display.notFound
}
return try getDisplayBrightness()
}
private static func getStandardDisplayBrightness() throws -> Float {
var brightness: float_t = 1
let service = IOServiceGetMatchingService(kIOMainPortDefault, IOServiceMatching("IODisplayConnect"))
defer {
IOObjectRelease(service)
}
let result = IODisplayGetFloatParameter(service, 0, kIODisplayBrightnessKey as CFString, &brightness)
if result != kIOReturnSuccess {
throw SensorError.Display.notStandard
}
return brightness
}
private static func getM1DisplayBrightness() throws -> Float {
let task = Process()
task.launchPath = "/usr/libexec/corebrightnessdiag"
task.arguments = ["status-info"]
let pipe = Pipe()
task.standardOutput = pipe
try task.run()
let data = pipe.fileHandleForReading.readDataToEndOfFile()
task.waitUntilExit()
if let plist = try? PropertyListSerialization.propertyList(from: data, options: [], format: nil) as? NSDictionary,
let displays = plist["CBDisplays"] as? [String: [String: Any]] {
for display in displays.values {
if let displayInfo = display["Display"] as? [String: Any],
displayInfo["DisplayServicesIsBuiltInDisplay"] as? Bool == true,
let brightness = displayInfo["DisplayServicesBrightness"] as? Float {
return brightness
}
}
}
throw SensorError.Display.notSilicon
}
}