2021-10-24 19:31:36 +05:30
|
|
|
import Foundation
|
|
|
|
import IOKit.pwr_mgt
|
|
|
|
|
|
|
|
struct ScreenSaverManager {
|
2023-04-22 18:38:33 +05:30
|
|
|
static var shared = Self()
|
2021-10-24 19:31:36 +05:30
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2024-08-18 18:16:51 +05:30
|
|
|
noSleepReturn = IOPMAssertionCreateWithName(
|
|
|
|
kIOPMAssertionTypeNoDisplaySleep as CFString,
|
|
|
|
IOPMAssertionLevel(kIOPMAssertionLevelOn),
|
|
|
|
reason as CFString,
|
|
|
|
&noSleepAssertion
|
|
|
|
)
|
2021-10-24 19:31:36 +05:30
|
|
|
return noSleepReturn == kIOReturnSuccess
|
|
|
|
}
|
|
|
|
|
|
|
|
@discardableResult mutating func enable() -> Bool {
|
|
|
|
if noSleepReturn != nil {
|
|
|
|
_ = IOPMAssertionRelease(noSleepAssertion) == kIOReturnSuccess
|
|
|
|
noSleepReturn = nil
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|