mirror of
https://github.com/FBukovina/Atoll.git
synced 2026-10-27 23:24:32 +00:00
Add: Lunar Support
This commit is contained in:
parent
93e0c86033
commit
14c8f244db
6 changed files with 1653 additions and 2923 deletions
505
DynamicIsland/managers/LunarManager.swift
Normal file
505
DynamicIsland/managers/LunarManager.swift
Normal file
|
|
@ -0,0 +1,505 @@
|
|||
/*
|
||||
* 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/>.
|
||||
*/
|
||||
|
||||
@preconcurrency import Foundation
|
||||
import AppKit
|
||||
import Combine
|
||||
import Defaults
|
||||
import Network
|
||||
|
||||
// MARK: - Lunar Display Data Model
|
||||
|
||||
/// Matches the JSON structure streamed by Lunar's socket listener.
|
||||
struct LunarDisplayData: Codable {
|
||||
var brightness: Float?
|
||||
var contrast: Float?
|
||||
var volume: Float?
|
||||
var mute: Bool?
|
||||
var nits: Float?
|
||||
var display: Int
|
||||
}
|
||||
|
||||
// MARK: - Lunar Control Category
|
||||
|
||||
enum LunarControlCategory {
|
||||
case brightness
|
||||
case contrast
|
||||
case volume
|
||||
}
|
||||
|
||||
// MARK: - Lunar Manager
|
||||
|
||||
/// Manages integration with the Lunar app (fyi.lunar.Lunar).
|
||||
///
|
||||
/// Responsibilities:
|
||||
/// - Detect whether Lunar is installed and running
|
||||
/// - Observe app launch / termination to connect / disconnect automatically
|
||||
/// - Connect to Lunar's TCP socket on localhost:23803 and listen for DDC changes
|
||||
/// - Route brightness / contrast / volume events to Atoll's HUD pipeline
|
||||
@MainActor
|
||||
final class LunarManager: ObservableObject {
|
||||
static let shared = LunarManager()
|
||||
|
||||
/// Bundle identifier of the Lunar app.
|
||||
nonisolated static let bundleID = "fyi.lunar.Lunar"
|
||||
|
||||
/// UserDefaults suite used by Lunar to store its API key.
|
||||
private static let lunarDomain = "fyi.lunar.Lunar"
|
||||
private static let apiKeyKey = "apiKey"
|
||||
|
||||
// MARK: Published state
|
||||
|
||||
/// Whether Lunar is currently detected (installed) on this machine.
|
||||
@Published private(set) var isDetected: Bool = false
|
||||
|
||||
/// Whether Lunar is currently running.
|
||||
@Published private(set) var isRunning: Bool = false
|
||||
|
||||
/// Whether the TCP socket connection to Lunar is active.
|
||||
@Published private(set) var isConnected: Bool = false
|
||||
|
||||
// MARK: Private
|
||||
|
||||
private var connection: NWConnection?
|
||||
private var workspaceLaunchObserver: NSObjectProtocol?
|
||||
private var workspaceTermObserver: NSObjectProtocol?
|
||||
private var cancellables = Set<AnyCancellable>()
|
||||
private weak var coordinator: DynamicIslandViewCoordinator?
|
||||
|
||||
/// Reconnection back-off state.
|
||||
private var reconnectTask: Task<Void, Never>?
|
||||
private var reconnectAttempts: Int = 0
|
||||
private static let maxReconnectAttempts = 5
|
||||
private static let baseReconnectDelay: UInt64 = 2_000_000_000 // 2 seconds
|
||||
|
||||
private init() {
|
||||
isDetected = Self.checkInstallation()
|
||||
isRunning = Self.checkRunning()
|
||||
setupWorkspaceObservers()
|
||||
setupSettingsObserver()
|
||||
}
|
||||
|
||||
// MARK: - Public API
|
||||
|
||||
/// Configure with the view coordinator for HUD dispatch.
|
||||
func configure(coordinator: DynamicIslandViewCoordinator) {
|
||||
self.coordinator = coordinator
|
||||
|
||||
if Defaults[.enableLunarIntegration] && isRunning {
|
||||
connectToLunar()
|
||||
}
|
||||
}
|
||||
|
||||
/// Called when Atoll is about to quit so Lunar's native OSD is restored.
|
||||
func appWillTerminate() {
|
||||
setLunarHideOSD(false)
|
||||
}
|
||||
|
||||
/// Refresh detection status (e.g. after manual check from Settings).
|
||||
func refreshDetectionStatus() {
|
||||
let wasRunning = isRunning
|
||||
isDetected = Self.checkInstallation()
|
||||
isRunning = Self.checkRunning()
|
||||
if wasRunning && !isRunning {
|
||||
disconnect()
|
||||
} else if !wasRunning && isRunning && Defaults[.enableLunarIntegration] {
|
||||
connectToLunar()
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Detection
|
||||
|
||||
/// Check if Lunar is installed by looking for its bundle ID.
|
||||
nonisolated static func checkInstallation() -> Bool {
|
||||
if NSWorkspace.shared.runningApplications.contains(where: { $0.bundleIdentifier == bundleID }) {
|
||||
return true
|
||||
}
|
||||
if let url = NSWorkspace.shared.urlForApplication(withBundleIdentifier: bundleID) {
|
||||
return FileManager.default.fileExists(atPath: url.path)
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
/// Check if Lunar is currently running.
|
||||
nonisolated static func checkRunning() -> Bool {
|
||||
NSWorkspace.shared.runningApplications.contains(where: { $0.bundleIdentifier == bundleID })
|
||||
}
|
||||
|
||||
// MARK: - Socket Connection
|
||||
|
||||
private func connectToLunar() {
|
||||
guard connection == nil else { return }
|
||||
guard let apiKey = Self.retrieveAPIKey() else {
|
||||
NSLog("⚠️ Lunar: failed to retrieve API key from UserDefaults suite '\(Self.lunarDomain)'")
|
||||
return
|
||||
}
|
||||
|
||||
setLunarHideOSD(true)
|
||||
|
||||
let host = NWEndpoint.Host("localhost")
|
||||
let port = NWEndpoint.Port(23803)
|
||||
let conn = NWConnection(host: host, port: port, using: .tcp)
|
||||
|
||||
conn.stateUpdateHandler = { [weak self] state in
|
||||
Task { @MainActor in
|
||||
self?.handleConnectionState(state, apiKey: apiKey)
|
||||
}
|
||||
}
|
||||
|
||||
connection = conn
|
||||
conn.start(queue: DispatchQueue.global(qos: .utility))
|
||||
NSLog("🌙 Lunar: connecting to socket on \(host):\(port)…")
|
||||
}
|
||||
|
||||
private func disconnect() {
|
||||
reconnectTask?.cancel()
|
||||
reconnectTask = nil
|
||||
reconnectAttempts = 0
|
||||
|
||||
if let conn = connection {
|
||||
conn.stateUpdateHandler = nil
|
||||
conn.cancel()
|
||||
connection = nil
|
||||
}
|
||||
isConnected = false
|
||||
setLunarHideOSD(false)
|
||||
NSLog("⏹ Lunar: socket disconnected")
|
||||
}
|
||||
|
||||
private func handleConnectionState(_ state: NWConnection.State, apiKey: String) {
|
||||
switch state {
|
||||
case .ready:
|
||||
isConnected = true
|
||||
reconnectAttempts = 0
|
||||
NSLog("🌙 Lunar: connected to socket")
|
||||
sendListenCommand(apiKey: apiKey)
|
||||
receiveMessages()
|
||||
|
||||
case .failed(let error):
|
||||
NSLog("🌙 Lunar: connection failed — \(error.localizedDescription)")
|
||||
isConnected = false
|
||||
connection?.cancel()
|
||||
connection = nil
|
||||
scheduleReconnect()
|
||||
|
||||
case .cancelled:
|
||||
isConnected = false
|
||||
|
||||
case .waiting(let error):
|
||||
NSLog("🌙 Lunar: connection waiting — \(error.localizedDescription)")
|
||||
|
||||
default:
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
/// Send Lunar's listen command with the API key.
|
||||
private func sendListenCommand(apiKey: String) {
|
||||
guard let conn = connection else { return }
|
||||
|
||||
let separator = String(UnicodeScalar(0x01))
|
||||
let message = [apiKey, "listen", "--json", "--only-user-adjustments"]
|
||||
.joined(separator: separator)
|
||||
|
||||
guard let data = message.data(using: .utf8) else {
|
||||
NSLog("⚠️ Lunar: failed to encode listen command")
|
||||
return
|
||||
}
|
||||
|
||||
conn.send(content: data, completion: .contentProcessed { error in
|
||||
if let error {
|
||||
NSLog("⚠️ Lunar: failed to send listen command — \(error.localizedDescription)")
|
||||
} else {
|
||||
NSLog("🌙 Lunar: listen command sent")
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/// Receive JSON messages from Lunar's socket in a loop.
|
||||
private func receiveMessages() {
|
||||
guard let conn = connection else { return }
|
||||
|
||||
conn.receive(minimumIncompleteLength: 1, maximumLength: 4096) { [weak self] data, _, isComplete, error in
|
||||
if let data, !data.isEmpty {
|
||||
Task { @MainActor in
|
||||
self?.handleReceivedData(data)
|
||||
}
|
||||
}
|
||||
|
||||
if isComplete {
|
||||
NSLog("🌙 Lunar: stream ended")
|
||||
Task { @MainActor in
|
||||
self?.isConnected = false
|
||||
self?.connection?.cancel()
|
||||
self?.connection = nil
|
||||
self?.scheduleReconnect()
|
||||
}
|
||||
} else if let error {
|
||||
NSLog("⚠️ Lunar: receive error — \(error.localizedDescription)")
|
||||
Task { @MainActor in
|
||||
self?.isConnected = false
|
||||
self?.connection?.cancel()
|
||||
self?.connection = nil
|
||||
self?.scheduleReconnect()
|
||||
}
|
||||
} else {
|
||||
// Continue reading
|
||||
Task { @MainActor in
|
||||
self?.receiveMessages()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private func handleReceivedData(_ data: Data) {
|
||||
guard Defaults[.enableLunarIntegration], isRunning else { return }
|
||||
|
||||
do {
|
||||
let displayData = try JSONDecoder().decode(LunarDisplayData.self, from: data)
|
||||
NSLog("🌙 Lunar: display=\(displayData.display) brightness=\(displayData.brightness ?? -1) contrast=\(displayData.contrast ?? -1) volume=\(displayData.volume ?? -1) mute=\(displayData.mute.map(String.init) ?? "nil")")
|
||||
routeToHUD(displayData)
|
||||
} catch {
|
||||
// Lunar may send multi-line JSON or partial data; try splitting by newlines
|
||||
let rawString = String(data: data, encoding: .utf8) ?? ""
|
||||
let lines = rawString.components(separatedBy: .newlines).filter { !$0.isEmpty }
|
||||
for line in lines {
|
||||
guard let lineData = line.data(using: .utf8) else { continue }
|
||||
if let displayData = try? JSONDecoder().decode(LunarDisplayData.self, from: lineData) {
|
||||
NSLog("🌙 Lunar: display=\(displayData.display) brightness=\(displayData.brightness ?? -1) contrast=\(displayData.contrast ?? -1) volume=\(displayData.volume ?? -1)")
|
||||
routeToHUD(displayData)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - HUD Routing
|
||||
|
||||
private func routeToHUD(_ data: LunarDisplayData) {
|
||||
let targetScreen = resolveScreen(for: data.display)
|
||||
let isExternal = isExternalDisplay(data.display)
|
||||
|
||||
// Nits-only events are informational (no user-facing HUD needed).
|
||||
let hasActionableChange = data.brightness != nil || data.contrast != nil || data.volume != nil || data.mute != nil
|
||||
guard hasActionableChange else { return }
|
||||
|
||||
// Brightness — values arrive in 0…1 range from Lunar
|
||||
if let brightness = data.brightness {
|
||||
let value = CGFloat(brightness)
|
||||
let icon = isExternal ? "display" : ""
|
||||
dispatchBrightnessHUD(value: value, icon: icon, onScreen: targetScreen)
|
||||
}
|
||||
|
||||
// Contrast — show as brightness-style HUD with a contrast icon
|
||||
if let contrast = data.contrast {
|
||||
let value = CGFloat(contrast)
|
||||
dispatchBrightnessHUD(value: value, icon: "circle.righthalf.filled", onScreen: targetScreen)
|
||||
}
|
||||
|
||||
// Volume
|
||||
if let volume = data.volume {
|
||||
let value = CGFloat(volume)
|
||||
let isMuted = data.mute ?? false
|
||||
dispatchVolumeHUD(value: value, isMuted: isMuted, onScreen: targetScreen)
|
||||
} else if let mute = data.mute {
|
||||
// Mute toggle without a volume value
|
||||
dispatchVolumeHUD(value: mute ? 0 : 1, isMuted: mute, onScreen: targetScreen)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - HUD Dispatch (mirrors BetterDisplayManager / SystemChangesObserver logic)
|
||||
|
||||
private func dispatchVolumeHUD(value: CGFloat, isMuted: Bool, onScreen targetScreen: NSScreen? = nil) {
|
||||
if HUDSuppressionCoordinator.shared.shouldSuppressVolumeHUD { return }
|
||||
|
||||
if Defaults[.enableCircularHUD] {
|
||||
CircularHUDWindowManager.shared.show(type: .volume, value: value, onScreen: targetScreen)
|
||||
return
|
||||
}
|
||||
if Defaults[.enableVerticalHUD] {
|
||||
VerticalHUDWindowManager.shared.show(type: .volume, value: value, icon: "", onScreen: targetScreen)
|
||||
return
|
||||
}
|
||||
if Defaults[.enableCustomOSD] && Defaults[.enableOSDVolume] {
|
||||
CustomOSDWindowManager.shared.showVolume(value: value, onScreen: targetScreen)
|
||||
}
|
||||
if Defaults[.enableSystemHUD] && !Defaults[.enableCustomOSD] && !Defaults[.enableVerticalHUD] && !Defaults[.enableCircularHUD] {
|
||||
coordinator?.toggleSneakPeek(
|
||||
status: true,
|
||||
type: .volume,
|
||||
value: value,
|
||||
icon: ""
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
private func dispatchBrightnessHUD(value: CGFloat, icon: String = "", onScreen targetScreen: NSScreen? = nil) {
|
||||
if Defaults[.enableCircularHUD] {
|
||||
CircularHUDWindowManager.shared.show(type: .brightness, value: value, icon: icon, onScreen: targetScreen)
|
||||
return
|
||||
}
|
||||
if Defaults[.enableVerticalHUD] {
|
||||
VerticalHUDWindowManager.shared.show(type: .brightness, value: value, icon: icon, onScreen: targetScreen)
|
||||
return
|
||||
}
|
||||
if Defaults[.enableCustomOSD] && Defaults[.enableOSDBrightness] {
|
||||
CustomOSDWindowManager.shared.showBrightness(value: value, icon: icon, onScreen: targetScreen)
|
||||
}
|
||||
if Defaults[.enableSystemHUD] && !Defaults[.enableCustomOSD] && !Defaults[.enableVerticalHUD] && !Defaults[.enableCircularHUD] {
|
||||
coordinator?.toggleSneakPeek(
|
||||
status: true,
|
||||
type: .brightness,
|
||||
value: value,
|
||||
icon: icon
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Helpers
|
||||
|
||||
/// Retrieve the Lunar API key from its UserDefaults suite.
|
||||
nonisolated private static func retrieveAPIKey() -> String? {
|
||||
UserDefaults(suiteName: lunarDomain)?.string(forKey: apiKeyKey)
|
||||
}
|
||||
|
||||
/// Resolve a Lunar display ID (CGDirectDisplayID) to the matching NSScreen.
|
||||
private func resolveScreen(for displayID: Int) -> NSScreen? {
|
||||
let target = UInt32(displayID)
|
||||
return NSScreen.screens.first { screen in
|
||||
guard let num = screen.deviceDescription[NSDeviceDescriptionKey("NSScreenNumber")] as? NSNumber else { return false }
|
||||
return num.uint32Value == target
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether the given displayID refers to an external (non-built-in) display.
|
||||
private func isExternalDisplay(_ displayID: Int) -> Bool {
|
||||
CGDisplayIsBuiltin(UInt32(displayID)) == 0
|
||||
}
|
||||
|
||||
// MARK: - Reconnection
|
||||
|
||||
private func scheduleReconnect() {
|
||||
guard Defaults[.enableLunarIntegration], isRunning else { return }
|
||||
guard reconnectAttempts < Self.maxReconnectAttempts else {
|
||||
NSLog("🌙 Lunar: max reconnect attempts reached, giving up")
|
||||
return
|
||||
}
|
||||
|
||||
reconnectTask?.cancel()
|
||||
let attempt = reconnectAttempts
|
||||
reconnectAttempts += 1
|
||||
|
||||
let delay = Self.baseReconnectDelay * UInt64(1 << min(attempt, 3)) // exponential back-off, max 16s
|
||||
|
||||
reconnectTask = Task { @MainActor in
|
||||
try? await Task.sleep(nanoseconds: delay)
|
||||
guard !Task.isCancelled, self.isRunning, Defaults[.enableLunarIntegration] else { return }
|
||||
NSLog("🌙 Lunar: reconnect attempt \(attempt + 1)/\(Self.maxReconnectAttempts)")
|
||||
self.connectToLunar()
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Workspace Observers (detect Lunar launch / quit)
|
||||
|
||||
private func setupWorkspaceObservers() {
|
||||
let lunarBundleID = Self.bundleID
|
||||
|
||||
workspaceLaunchObserver = NSWorkspace.shared.notificationCenter.addObserver(
|
||||
forName: NSWorkspace.didLaunchApplicationNotification,
|
||||
object: nil,
|
||||
queue: .main
|
||||
) { [weak self] notification in
|
||||
guard let app = notification.userInfo?[NSWorkspace.applicationUserInfoKey] as? NSRunningApplication,
|
||||
app.bundleIdentifier == lunarBundleID
|
||||
else { return }
|
||||
Task { @MainActor in
|
||||
NSLog("🟢 Lunar launched (pid: \(app.processIdentifier))")
|
||||
self?.isDetected = true
|
||||
self?.isRunning = true
|
||||
self?.reconnectAttempts = 0
|
||||
if Defaults[.enableLunarIntegration] {
|
||||
// Give Lunar a moment to start its socket server
|
||||
try? await Task.sleep(nanoseconds: 1_500_000_000)
|
||||
self?.connectToLunar()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
workspaceTermObserver = NSWorkspace.shared.notificationCenter.addObserver(
|
||||
forName: NSWorkspace.didTerminateApplicationNotification,
|
||||
object: nil,
|
||||
queue: .main
|
||||
) { [weak self] notification in
|
||||
guard let app = notification.userInfo?[NSWorkspace.applicationUserInfoKey] as? NSRunningApplication,
|
||||
app.bundleIdentifier == lunarBundleID
|
||||
else { return }
|
||||
Task { @MainActor in
|
||||
NSLog("🔴 Lunar terminated (pid: \(app.processIdentifier))")
|
||||
self?.isRunning = false
|
||||
self?.disconnect()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Settings Observer
|
||||
|
||||
private func setupSettingsObserver() {
|
||||
Defaults.publisher(.enableLunarIntegration, options: [])
|
||||
.sink { [weak self] change in
|
||||
Task { @MainActor in
|
||||
guard let self else { return }
|
||||
if change.newValue && self.isRunning {
|
||||
self.connectToLunar()
|
||||
} else {
|
||||
self.disconnect()
|
||||
}
|
||||
}
|
||||
}
|
||||
.store(in: &cancellables)
|
||||
}
|
||||
|
||||
deinit {
|
||||
// Restore Lunar's native OSD synchronously on teardown
|
||||
LunarManager.writeLunarDefault(hideOSD: false)
|
||||
connection?.cancel()
|
||||
reconnectTask?.cancel()
|
||||
if let workspaceLaunchObserver {
|
||||
NSWorkspace.shared.notificationCenter.removeObserver(workspaceLaunchObserver)
|
||||
}
|
||||
if let workspaceTermObserver {
|
||||
NSWorkspace.shared.notificationCenter.removeObserver(workspaceTermObserver)
|
||||
}
|
||||
cancellables.removeAll()
|
||||
}
|
||||
|
||||
// MARK: - Lunar OSD Suppression
|
||||
|
||||
/// Tell Lunar to hide (or show) its own native OSD by writing to its UserDefaults.
|
||||
private func setLunarHideOSD(_ hide: Bool) {
|
||||
Self.writeLunarDefault(hideOSD: hide)
|
||||
}
|
||||
|
||||
/// Synchronous, nonisolated helper so it can also be called from `deinit`.
|
||||
nonisolated private static func writeLunarDefault(hideOSD hide: Bool) {
|
||||
guard let defaults = UserDefaults(suiteName: lunarDomain) else {
|
||||
NSLog("⚠️ Lunar: unable to open UserDefaults suite '\(lunarDomain)' to set hideOSD")
|
||||
return
|
||||
}
|
||||
defaults.set(hide, forKey: "hideOSD")
|
||||
NSLog("🌙 Lunar: hideOSD set to \(hide)")
|
||||
}
|
||||
}
|
||||
|
|
@ -188,6 +188,14 @@ class SystemHUDManager {
|
|||
await self.startSystemObserver()
|
||||
}
|
||||
}.store(in: &cancellables)
|
||||
|
||||
// When Lunar integration toggled, restart observer to update brightness key interception
|
||||
Defaults.publisher(.enableLunarIntegration, options: []).sink { [weak self] _ in
|
||||
guard let self = self, self.isSetupComplete else { return }
|
||||
Task { @MainActor in
|
||||
await self.startSystemObserver()
|
||||
}
|
||||
}.store(in: &cancellables)
|
||||
}
|
||||
|
||||
private var cancellables = Set<AnyCancellable>()
|
||||
|
|
@ -240,9 +248,9 @@ class SystemHUDManager {
|
|||
keyboardBacklightEnabled = Defaults[.enableKeyboardBacklightHUD]
|
||||
}
|
||||
|
||||
// When BetterDisplay integration is on, stop intercepting brightness and
|
||||
// Cmd+Brightness keys so BetterDisplay receives them and sends OSD notifications.
|
||||
if Defaults[.enableBetterDisplayIntegration] {
|
||||
// When BetterDisplay or Lunar integration is on, stop intercepting brightness and
|
||||
// Cmd+Brightness keys so the external app receives them and sends DDC/OSD events.
|
||||
if Defaults[.enableBetterDisplayIntegration] || Defaults[.enableLunarIntegration] {
|
||||
brightnessEnabled = false
|
||||
keyboardBacklightEnabled = false
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue