mirror of
https://github.com/yattee/yattee.git
synced 2024-11-09 15:58:20 +00:00
94577332a1
Signed-off-by: Toni Förster <toni.foerster@gmail.com>
37 lines
949 B
Swift
37 lines
949 B
Swift
import Foundation
|
|
import IOKit.pwr_mgt
|
|
|
|
struct ScreenSaverManager {
|
|
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
|
|
}
|
|
}
|