fix: prevent idle sleep during playback

This commit is contained in:
2026-10-17 22:05:27 +02:00
parent f49eb6d084
commit 2b96a165d2
7 changed files with 82 additions and 7 deletions
+19
View File
@@ -0,0 +1,19 @@
import Foundation
import IOKit.pwr_mgt
// Inspect this process, not global assertions from audio drivers or other apps.
func requirePlaybackPower(system: Bool, display: Bool, _ context: String) {
var result: Unmanaged<CFDictionary>?
precondition(IOPMCopyAssertionsByProcess(&result) == kIOReturnSuccess,
"Could not inspect macOS power assertions")
let byProcess = result!.takeRetainedValue() as NSDictionary
let assertions = byProcess[NSNumber(value: ProcessInfo.processInfo.processIdentifier)] as? [[String: Any]] ?? []
let active = assertions.filter { ($0[kIOPMAssertionLevelKey] as? NSNumber)?.intValue != 0 }
let types = Set(active.compactMap { $0[kIOPMAssertionTypeKey] as? String })
guard types.contains(kIOPMAssertPreventUserIdleSystemSleep) == system &&
types.contains(kIOPMAssertPreventUserIdleDisplaySleep) == display else {
FileHandle.standardError.write(Data(
"FAIL: \(context): expected system=\(system), display=\(display); actual=\(types)\n".utf8))
exit(EXIT_FAILURE)
}
}