20 lines
1.0 KiB
Swift
20 lines
1.0 KiB
Swift
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)
|
|
}
|
|
}
|