Sign in

    proton_power_monitor

    Native power and idle state queries for MoonBit on Windows, Linux, and macOS.

    power-monitor
    native
    windows
    linux
    macos
    power-management
    Download zip
    Version
    0.6.0
    License
    Apache-2.0
    Last updated
    10 hours ago
    Downloads
    23K

    #proton_power_monitor

    Native power and idle state queries for MoonBit on Windows, Linux, and macOS.

    PowerMonitor::with_watching owns event watching for the duration of its async callback. The callback begins only after registration succeeds. Returning, raising, or cancelling the scope stops the watcher and waits for cleanup. Cancel its owning task to stop watching; there is no separate start/stop queue. A second scope on the same monitor raises OperationFailed immediately.

    let monitor = @power_monitor.PowerMonitor()
    defer monitor.destroy()
    monitor.with_watching(() => {
    // Use monitor.drain_events() from the application's event loop.
    // Keep this scope alive until the owning task is cancelled.
    @async.pause()
    })

    destroy() is synchronous and accepts only an idle monitor. End its watching scope first. Idle and power-source queries do not start event watching.

    On Windows, AC and onboard-battery notifications produce OnAC and OnBattery respectively. UPS and unrecognized source notifications do not produce either event. Idle time is measured in whole seconds for the calling session, modulo the Windows last-input counter's 2^32-millisecond period (about 49.7 days); it cannot distinguish an idle interval spanning a full period.

    On Linux, building requires pkg-config and GIO development headers (libglib2.0-dev on Ubuntu). GIO is loaded dynamically and remains optional at runtime. A missing library or unavailable system bus raises BackendUnavailable; queries remain usable. Connection establishment, authentication and subscription are asynchronous and cancellable, with a five-second deadline for the whole startup. A disconnected bus fails the scope and cancels its callback. A new scope can retry after failure; there is no automatic reconnection.

    The Proton extension owns the scope in an application task. watch_status() reports Starting, Watching, or Unavailable(detail); application teardown resets it to Stopped.

    The Linux regression tests require dbus-daemon and dbus-send. They use an isolated bus and loopback listener, never the host system bus. They cover paused startup, timeout, cancellation during transport establishment, concurrent-scope rejection, partial registration rollback, signals, disconnection and restart.

    PowerMonitorError

    pub(all) suberror PowerMonitorError {
    BackendUnavailable(detail~ : String)
    OperationFailed(operation~ : String, detail~ : String)
    } derive(Eq)

    PowerMonitorError::equal

    PowerMonitorError::not_equal

    fn PowerMonitorError::not_equal(x : PowerMonitorError, y : PowerMonitorError) -> Bool

    PowerMonitorError::output

    fn PowerMonitorError::output(self : PowerMonitorError, logger : &Logger) -> Unit

    PowerMonitorError::to_string

    fn PowerMonitorError::to_string(self : PowerMonitorError) -> String

    PowerMonitor

    pub struct PowerMonitor {
    // private fields
    }

    PowerMonitor::PowerMonitor

    fn PowerMonitor::PowerMonitor() -> PowerMonitor raise PowerMonitorError

    PowerMonitor::destroy

    fn PowerMonitor::destroy(self : PowerMonitor) -> Unit raise PowerMonitorError

    Destroys an idle monitor. End its watching scope before destroying it.

    PowerMonitor::drain_events

    fn PowerMonitor::drain_events(self : PowerMonitor) -> Array[PowerMonitorEvent]

    Drains every pending event from the queue. Returns an empty array when no event is pending; never raises for an empty queue.

    PowerMonitor::idle_seconds

    fn PowerMonitor::idle_seconds(self : PowerMonitor) -> Int raise PowerMonitorError

    PowerMonitor::power_source

    PowerMonitor::with_watching

    async fn[T] PowerMonitor::with_watching(self : PowerMonitor, body : async () -> T) -> T

    Runs body after native event watching is ready. Returning, raising, or cancelling always stops the watcher and awaits native cleanup. A second watching scope on the same monitor is rejected rather than queued. Linux connection setup and subscriptions are cancellable and have a five second startup deadline. A disconnected bus fails the scope and cancels body.

    PowerMonitorEvent

    pub enum PowerMonitorEvent {
    Suspend
    Resume
    OnAC
    OnBattery
    LockScreen
    UnlockScreen
    } derive(Eq,
    Debug
    )

    System power and session events delivered by the watch backend.

    PowerMonitorEvent::equal

    PowerMonitorEvent::not_equal

    fn PowerMonitorEvent::not_equal(x : PowerMonitorEvent, y : PowerMonitorEvent) -> Bool

    PowerSource

    pub(all) enum PowerSource {
    Unknown
    AC
    Battery
    } derive(Eq,
    Debug
    )

    PowerSource::equal

    fn PowerSource::equal(PowerSource, PowerSource) -> Bool

    PowerSource::not_equal

    fn PowerSource::not_equal(x : PowerSource, y : PowerSource) -> Bool

    PowerSourceInfo

    pub(all) struct PowerSourceInfo {
    source : PowerSource
    battery_percent : Int?
    } derive(Eq,
    Debug
    )

    PowerSourceInfo::equal

    PowerSourceInfo::not_equal

    fn PowerSourceInfo::not_equal(x : PowerSourceInfo, y : PowerSourceInfo) -> Bool

    State

    pub type State

    Powered by MoonBit

    Site sourceReport issuePackagesBuild queueSkillsStatistics

    © 2026 mooncakes.io