Documentation
¶
Overview ¶
Package power reports the machine's power state (AC or battery, charge, power-saving mode), tells the app when the system sleeps and wakes, and keeps the machine awake while the app plays video or runs a long task.
st, err := power.Get(a) // st.Source == power.Battery, st.Battery == 0.42 power.OnSuspend(a, pauseSync) power.OnResume(a, resumeSync) blk, err := power.KeepDisplayOn(a, "Playing video") defer blk.Release()
Platforms: macOS uses IOKit (IOPowerSources, IOPMAssertion, sleep/wake notifications) and NSProcessInfo's Low Power Mode; Windows GetSystemPowerStatus, SetThreadExecutionState and WM_POWERBROADCAST; Linux UPower and systemd-logind (Inhibit, PrepareForSleep) over the system D-Bus. A sleep the user asks for (closing the lid, the Sleep command) is never blocked: blocks only stop idle sleep.
On iOS, UIDevice battery monitoring and NSProcessInfo's Low Power Mode; both block kinds disable the idle timer (the screen stays on: an app cannot keep the device awake with the screen off). On Android, the battery broadcast and PowerManager's power-saver mode; KeepDisplayOn sets FLAG_KEEP_SCREEN_ON, KeepAwake holds a partial wake lock when the app declares android.permission.WAKE_LOCK and keeps the screen on otherwise. Neither mobile system tells apps about sleep and wake, so OnSuspend and OnResume never fire there; use the app lifecycle.
Functions are safe from any goroutine once the app runs; handlers run on the main thread. On* may be called before Run. Get returns ErrUnsupported where the backend has no power support (minimal backends) and tango.ErrNotRunning after the app quit; handlers then simply never fire.
Index ¶
Constants ¶
const ( Unknown = platform.PowerSourceUnknown AC = platform.PowerSourceAC Battery = platform.PowerSourceBattery )
Variables ¶
var ErrUnsupported = platform.ErrUnsupported
ErrUnsupported is platform.ErrUnsupported.
Functions ¶
Types ¶
type Block ¶
type Block struct {
// contains filtered or unexported fields
}
Block is one active sleep block. Release it when the work is done; the system drops it when the process exits.
func KeepAwake ¶
KeepAwake stops the system from idle-sleeping (the display may still turn off) until the block is released. Use it for long tasks.
func KeepDisplayOn ¶
KeepDisplayOn keeps the display on and the system awake until the block is released. Use it for video playback and presentations.