upower

package
v0.3.16 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 8, 2026 License: MIT Imports: 7 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package upower reads battery state from UPower over the system D-Bus, the Go counterpart of crates/wayle-battery's zbus proxy: the DisplayDevice aggregate, the peripherals UPower enumerates beside it, and live updates through PropertiesChanged, DeviceAdded and DeviceRemoved.

Index

Constants

View Source
const (
	// BusName is UPower's well-known name on the system bus.
	BusName = "org.freedesktop.UPower"
)
View Source
const DisplayDevicePath = "/org/freedesktop/UPower/devices/DisplayDevice"

DisplayDevicePath is UPower's composite device across all batteries.

Variables

This section is empty.

Functions

This section is empty.

Types

type Device

type Device struct {
	Percentage  float64
	State       DeviceState
	TimeToEmpty time.Duration // 0 when unknown
	TimeToFull  time.Duration // 0 when unknown
	// IsPresent is UPower's IsPresent: a battery is in its bay.
	IsPresent bool
	// EnergyRate is the charge or discharge rate in watts.
	EnergyRate float64
	// Energy and EnergyFull are watt-hours now and at full charge.
	Energy     float64
	EnergyFull float64
	// Capacity is the health percentage (full against design); 0 when
	// unknown.
	Capacity     float64
	WarningLevel WarningLevel
	// ChargeEndThreshold is the charge limit percentage.
	ChargeEndThreshold       uint32
	ChargeThresholdSupported bool
	ChargeThresholdEnabled   bool
}

Device is one battery snapshot: the org.freedesktop.UPower.Device properties the bar and its dropdown read.

type DeviceKind added in v0.3.15

type DeviceKind uint8

DeviceKind mirrors UPower's device Type enum.

const (
	KindUnknown DeviceKind = iota
	KindLinePower
	KindBattery
	KindUPS
	KindMonitor
	KindMouse
	KindKeyboard
	KindPDA
	KindPhone
	KindMediaPlayer
	KindTablet
	KindComputer
	KindGamingInput
	KindPen
	KindTouchpad
	KindModem
	KindNetwork
	KindHeadset
	KindSpeakers
	KindHeadphones
	KindVideo
	KindOtherAudio
	KindRemoteControl
	KindPrinter
	KindScanner
	KindCamera
	KindWearable
	KindToy
	KindBluetoothGeneric
)

Device kinds, numbered as UPower's Type property.

func KindFromUint32 added in v0.3.15

func KindFromUint32(v uint32) DeviceKind

KindFromUint32 maps UPower's wire value; anything else is Unknown.

type DeviceState

type DeviceState uint8

DeviceState mirrors UPower's device state enum (types.rs's DeviceState/From<u32>).

const (
	StateUnknown          DeviceState = iota
	StateCharging                     // 1
	StateDischarging                  // 2
	StateEmpty                        // 3
	StateFullyCharged                 // 4
	StatePendingCharge                // 5
	StatePendingDischarge             // 6
)

Device states.

func StateFromUint32

func StateFromUint32(v uint32) DeviceState

StateFromUint32 maps UPower's wire value; anything else is Unknown.

type Peripheral added in v0.3.15

type Peripheral struct {
	Path         dbus.ObjectPath
	Kind         DeviceKind
	Model        string
	Percentage   float64
	State        DeviceState
	WarningLevel WarningLevel
}

Peripheral is one device UPower reports beside the DisplayDevice aggregate: a wireless mouse, a keyboard, a controller, a headset, a UPS.

type Source

type Source interface {
	// Read returns the current device snapshot.
	Read(ctx context.Context) (Device, error)
	// Subscribe delivers a tick per PropertiesChanged on the device.
	// The returned stop function unsubscribes; the channel closes on
	// ctx completion either way.
	Subscribe(ctx context.Context) (<-chan struct{}, func(), error)
	// EnableChargeThreshold turns the charge limit on or off
	// (Device::enable_charge_threshold).
	EnableChargeThreshold(ctx context.Context, enabled bool) error
	// Peripherals returns the peripherals UPower enumerates, ordered by
	// object path.
	Peripherals(ctx context.Context) ([]Peripheral, error)
	// SubscribePeripherals delivers a tick per device added or removed
	// and per PropertiesChanged on any device but the DisplayDevice.
	SubscribePeripherals(ctx context.Context) (<-chan struct{}, func(), error)
}

Source is the seam the bar module and dropdown consume; tests swap in a fake.

type System

type System struct {
	// contains filtered or unexported fields
}

System reads the real UPower on the system bus.

func NewOn

func NewOn(conn *dbus.Conn) *System

NewOn targets the DisplayDevice over conn, which stays the caller's.

func (*System) EnableChargeThreshold

func (s *System) EnableChargeThreshold(ctx context.Context, enabled bool) error

EnableChargeThreshold calls the device's EnableChargeThreshold.

func (*System) OnBattery added in v0.3.15

func (s *System) OnBattery(ctx context.Context) (bool, error)

OnBattery reads UPower's OnBattery: the machine runs on its battery.

func (*System) Peripherals added in v0.3.15

func (s *System) Peripherals(ctx context.Context) ([]Peripheral, error)

Peripherals enumerates UPower's devices and reads each one, keeping the peripherals.

func (*System) Read

func (s *System) Read(ctx context.Context) (Device, error)

Read fetches every DisplayDevice property in one GetAll.

func (*System) Subscribe

func (s *System) Subscribe(ctx context.Context) (<-chan struct{}, func(), error)

Subscribe matches PropertiesChanged for the DisplayDevice, and UPower gaining an owner, and ticks the channel per signal. The matches are removed by stop.

func (*System) SubscribeOnBattery added in v0.3.15

func (s *System) SubscribeOnBattery(ctx context.Context) (<-chan struct{}, func(), error)

SubscribeOnBattery ticks per PropertiesChanged of UPower itself, which carries OnBattery. The match is removed by stop.

func (*System) SubscribePeripherals added in v0.3.15

func (s *System) SubscribePeripherals(ctx context.Context) (<-chan struct{}, func(), error)

SubscribePeripherals matches DeviceAdded and DeviceRemoved on UPower and PropertiesChanged under its devices, and UPower gaining an owner. The matches are removed by stop.

type WarningLevel

type WarningLevel uint8

WarningLevel mirrors UPower's warning level enum (types.rs's WarningLevel/From<u32>).

const (
	WarningUnknown     WarningLevel = iota
	WarningNone                     // 1
	WarningDischarging              // 2, UPSes only
	WarningLow                      // 3
	WarningCritical                 // 4
	WarningAction                   // 5
)

Warning levels.

func WarningFromUint32

func WarningFromUint32(v uint32) WarningLevel

WarningFromUint32 maps UPower's wire value; anything else is Unknown.

func (WarningLevel) Low

func (w WarningLevel) Low() bool

Low reports Low, Critical, or Action: the levels the dropdown flags (helpers.rs's is_low_battery).

func (WarningLevel) Rank added in v0.3.15

func (w WarningLevel) Rank() uint8

Rank orders the levels a battery alert follows: 0 for none, 1 for Low, 2 for Critical and Action.

Directories

Path Synopsis
Package upowertest serves a scriptable UPower on a test bus: the DisplayDevice and enumerated devices, each change announced with the signal the real daemon sends, and every GetAll counted.
Package upowertest serves a scriptable UPower on a test bus: the DisplayDevice and enumerated devices, each change announced with the signal the real daemon sends, and every GetAll counted.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL