Swift macOS menu bar app with Samjokoo (삼족오) icon. Features: display/system sleep prevention, timer mode, battery awareness, auto-start on login. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
69 lines
2.1 KiB
Swift
69 lines
2.1 KiB
Swift
import Foundation
|
|
import IOKit
|
|
import IOKit.pwr_mgt
|
|
import IOKit.ps
|
|
|
|
final class SleepManager {
|
|
private var displayAssertionID: IOPMAssertionID = 0
|
|
private var systemAssertionID: IOPMAssertionID = 0
|
|
private(set) var isDisplayActive = false
|
|
private(set) var isSystemActive = false
|
|
private(set) var activatedAt: Date?
|
|
|
|
var preventDisplaySleep = true
|
|
var preventSystemSleep = true
|
|
|
|
func activate() {
|
|
deactivate()
|
|
|
|
if preventDisplaySleep {
|
|
let reason = "KPopBird: preventing display sleep" as CFString
|
|
let result = IOPMAssertionCreateWithName(
|
|
kIOPMAssertionTypePreventUserIdleDisplaySleep as CFString,
|
|
IOPMAssertionLevel(kIOPMAssertionLevelOn),
|
|
reason,
|
|
&displayAssertionID
|
|
)
|
|
isDisplayActive = (result == kIOReturnSuccess)
|
|
}
|
|
|
|
if preventSystemSleep {
|
|
let reason = "KPopBird: preventing system sleep" as CFString
|
|
let result = IOPMAssertionCreateWithName(
|
|
kIOPMAssertionTypePreventUserIdleSystemSleep as CFString,
|
|
IOPMAssertionLevel(kIOPMAssertionLevelOn),
|
|
reason,
|
|
&systemAssertionID
|
|
)
|
|
isSystemActive = (result == kIOReturnSuccess)
|
|
}
|
|
|
|
activatedAt = Date()
|
|
}
|
|
|
|
func deactivate() {
|
|
if isDisplayActive {
|
|
IOPMAssertionRelease(displayAssertionID)
|
|
isDisplayActive = false
|
|
displayAssertionID = 0
|
|
}
|
|
if isSystemActive {
|
|
IOPMAssertionRelease(systemAssertionID)
|
|
isSystemActive = false
|
|
systemAssertionID = 0
|
|
}
|
|
activatedAt = nil
|
|
}
|
|
|
|
var isActive: Bool {
|
|
isDisplayActive || isSystemActive
|
|
}
|
|
|
|
static func isOnACPower() -> Bool {
|
|
guard let info = IOPSCopyPowerSourcesInfo()?.takeRetainedValue(),
|
|
let providing = IOPSGetProvidingPowerSourceType(info)?.takeUnretainedValue()
|
|
else { return true }
|
|
return (providing as String) == kIOPMACPowerKey
|
|
}
|
|
}
|