1
0
mirror of https://github.com/yattee/yattee.git synced 2024-12-15 06:40:32 +05:30
yattee/Model/Player/ScreenSaverManager.swift

35 lines
1.0 KiB
Swift
Raw Normal View History

import Foundation
import IOKit.pwr_mgt
struct ScreenSaverManager {
2023-04-22 18:38:33 +05:30
static var shared = Self()
var noSleepAssertion: IOPMAssertionID = 0
var noSleepReturn: IOReturn?
var enabled: Bool {
noSleepReturn == nil
}
@discardableResult mutating func disable(reason: String = "Unknown reason") -> Bool {
guard enabled else {
return false
}
noSleepReturn = IOPMAssertionCreateWithName(kIOPMAssertionTypeNoDisplaySleep as CFString,
IOPMAssertionLevel(kIOPMAssertionLevelOn),
reason as CFString,
&noSleepAssertion)
return noSleepReturn == kIOReturnSuccess
}
@discardableResult mutating func enable() -> Bool {
if noSleepReturn != nil {
_ = IOPMAssertionRelease(noSleepAssertion) == kIOReturnSuccess
noSleepReturn = nil
return true
}
return false
}
}