-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from samzong/feat/mac-sleep-preventer
Feat/mac sleep preventer
- Loading branch information
Showing
5 changed files
with
75 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// | ||
// SleepManager.swift | ||
// MacMusicPlayer | ||
// | ||
// Created by X on 10/1/24. | ||
// | ||
|
||
import Foundation | ||
import IOKit.pwr_mgt | ||
|
||
class SleepManager: ObservableObject { | ||
@Published var preventSleep = false { | ||
didSet { | ||
updateSleepAssertion() | ||
} | ||
} | ||
|
||
private var assertionID: IOPMAssertionID = 0 | ||
|
||
func updateSleepAssertion() { | ||
if preventSleep { | ||
createAssertion() | ||
} else { | ||
releaseAssertion() | ||
} | ||
} | ||
|
||
private func createAssertion() { | ||
let reason = "MacMusicPlayer is preventing display sleep" | ||
IOPMAssertionCreateWithName(kIOPMAssertionTypePreventUserIdleDisplaySleep as CFString, | ||
IOPMAssertionLevel(kIOPMAssertionLevelOn), | ||
reason as CFString, | ||
&assertionID) | ||
} | ||
|
||
private func releaseAssertion() { | ||
if assertionID != 0 { | ||
IOPMAssertionRelease(assertionID) | ||
assertionID = 0 | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters