Skip to content
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions bin/omarchy-powerprofiles-set
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,14 @@ usage() {

case "$action" in
autodetect)
# Use the same signal as the shell (UPower.onBattery) so every entry point
# saves and restores the profile under the same ac/battery key.
if [[ $(busctl get-property org.freedesktop.UPower /org/freedesktop/UPower org.freedesktop.UPower OnBattery 2>/dev/null) == "b true" ]]; then
# Follow the battery's own state, like the shell's power panel, so every
# entry point saves and restores the profile under the same ac/battery key.
# UPower.OnBattery ANDs the composite state with its cached AC-online
# state, so a missed AC uevent leaves it false after unplug while the
# battery already reports discharging. The display device's State comes
# from the batteries alone, so it has no such dependency.
state=$(busctl get-property org.freedesktop.UPower /org/freedesktop/UPower/devices/DisplayDevice org.freedesktop.UPower.Device State 2>/dev/null)
if [[ $state == "u 2" ]]; then
action=battery
else
action=ac
Comment thread
greptile-apps[bot] marked this conversation as resolved.
Resolved
Expand Down
22 changes: 22 additions & 0 deletions shell/plugins/panels/power/Model.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,26 @@ function batteryFraction(device) {
return device && device.isPresent ? Math.max(0, Math.min(1, device.percentage)) : 0
}

// upowerd computes OnBattery as "the composite battery state is discharging
// and no AC supply is online", so a missed AC uevent can leave it false after
// unplug while the battery already reports Discharging. Read the battery's own
// state and fall back to OnBattery only when it reports no state at all.
function isDischarging(device, onBattery, states) {
var d = device || {}
var s = states || {}
if (!d.isPresent) return false
if (d.state === undefined || d.state === s.Unknown) return !!onBattery
return d.state === s.Discharging
}

function isCharging(device, onBattery, states) {
var d = device || {}
var s = states || {}
if (!d.isPresent) return false
if (d.state === undefined || d.state === s.Unknown) return !onBattery
return d.state === s.Charging
}

// rateFresh is false while the charge rate still dates from before external
// power arrived. A battery that reports its rate every half minute reads
// "charging at 0 W" until then, which is not a threshold holding it.
Expand Down Expand Up @@ -230,6 +250,8 @@ if (typeof module !== "undefined") {
parseProfiles: parseProfiles,
profileIcon: profileIcon,
batteryFraction: batteryFraction,
isDischarging: isDischarging,
isCharging: isCharging,
chargeThresholdActive: chargeThresholdActive,
batteryIcon: batteryIcon,
modeLabel: modeLabel,
Expand Down
15 changes: 7 additions & 8 deletions shell/plugins/panels/power/Panel.qml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ Panel {

function upowerStates() {
return {
Unknown: UPowerDeviceState.Unknown,
Charging: UPowerDeviceState.Charging,
Discharging: UPowerDeviceState.Discharging,
FullyCharged: UPowerDeviceState.FullyCharged,
Expand Down Expand Up @@ -96,10 +97,11 @@ Panel {
var device = UPower.displayDevice
return device && device.isPresent && device.state === UPowerDeviceState.FullyCharged && !root.chargeThresholdActive
}
readonly property bool discharging: {
var device = UPower.displayDevice
return !!(device && device.isPresent && UPower.onBattery)
}
// The battery's own state, not UPower.onBattery: upowerd ANDs that with its
// cached AC-online state, so a missed AC uevent leaves it false after unplug
// while the battery already reports Discharging. The display device is the
// composite of the batteries alone, so its state has no such dependency.
readonly property bool discharging: Model.isDischarging(UPower.displayDevice, UPower.onBattery, upowerStates())
// False from the moment external power arrives until the battery has had
// time to report a charge rate measured with it. Some batteries refresh
// the rate only every half minute, and until then it reads 0 W.
Expand Down Expand Up @@ -127,10 +129,7 @@ Panel {
return Model.batteryFraction(d)
}

readonly property bool charging: {
var d = UPower.displayDevice
return d && d.isPresent && !UPower.onBattery && !root.batteryFlowIdle
}
readonly property bool charging: Model.isCharging(UPower.displayDevice, UPower.onBattery, upowerStates()) && !root.batteryFlowIdle

readonly property color batteryFillColor: {
return root.bar ? root.bar.foreground : Commons.Color.foreground
Expand Down
44 changes: 36 additions & 8 deletions test/shell.d/power-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ source "$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)/base-test.sh"

run_node_test <<'JS'
const power = requireFromRoot('shell/plugins/panels/power/Model.js')
const states = { Charging: 1, Discharging: 2, FullyCharged: 3, PendingCharge: 4 }
const states = { Unknown: 0, Charging: 1, Discharging: 2, FullyCharged: 3, PendingCharge: 4 }

assertEqual(power.selectProfileIndex(0, 1, ['balanced', 'performance']), 1, 'power advances profile selection')
assertEqual(power.selectProfileIndex(1, 1, ['balanced', 'performance']), 1, 'power clamps profile selection')
Expand All @@ -21,6 +21,18 @@ assertDeepEqual(
assert(power.profileIcon('performance').length > 0, 'power maps profile icons')
assertEqual(power.batteryFraction({ isPresent: true, percentage: 1.5 }), 1, 'power clamps battery fraction')

assert(power.isDischarging({ isPresent: true, state: states.Discharging }, false, states), 'power reads discharging from the battery while OnBattery is stale after unplug')
assert(power.isDischarging({ isPresent: true, state: states.Discharging }, true, states), 'power reads discharging from the battery while OnBattery agrees')
assert(!power.isDischarging({ isPresent: true, state: states.Charging }, true, states), 'power does not read a charging battery as discharging')
assert(!power.isDischarging({ isPresent: false, state: states.Discharging }, true, states), 'power needs a present battery to discharge')
assert(power.isDischarging({ isPresent: true, state: states.Unknown }, true, states), 'power falls back to OnBattery when the battery state is unknown')
assert(!power.isDischarging({ isPresent: true, state: states.Unknown }, false, states), 'power keeps OnBattery false as plugged in when the battery state is unknown')
assert(power.isCharging({ isPresent: true, state: states.Charging }, true, states), 'power reads charging from the battery while OnBattery is stale after plugging in')
assert(!power.isCharging({ isPresent: true, state: states.Discharging }, false, states), 'power does not read a discharging battery as charging')
assert(!power.isCharging({ isPresent: true, state: states.FullyCharged }, false, states), 'power does not read a full battery as charging')
assert(power.isCharging({ isPresent: true, state: states.Unknown }, false, states), 'power falls back to OnBattery when the battery state is unknown')
assert(!power.isCharging({ isPresent: false, state: states.Charging }, false, states), 'power needs a present battery to charge')

assert(power.chargeThresholdActive({ isPresent: true, percentage: 0.8, state: states.PendingCharge }, false, states), 'power detects threshold by pending charge state')
assert(power.chargeThresholdActive({ isPresent: true, percentage: 0.8, state: states.Charging, changeRate: 0.1, timeToFull: 120 }, false, states), 'power detects threshold by stalled charging')
assert(!power.chargeThresholdActive({ isPresent: true, percentage: 0.8, state: states.Charging, changeRate: 1.0, timeToFull: 120 }, false, states), 'power does not flag active charging as threshold')
Expand All @@ -40,17 +52,33 @@ assertEqual(power.localizeReading('75-80%', function(v, d) { return '<' + v + '>
assertEqual(power.localizeReading('13.10W', function(v, d) { return v.toFixed(d).replace('.', ',') }), '13,10W', 'power keeps a reading\'s decimals when localizing')
assertEqual(power.localizeReading(undefined), '', 'power reads a missing battery field as empty')
assertEqual(power.modeLabel({ isPresent: true, percentage: 0.5, state: states.Discharging }, true, states), 'On battery', 'power labels battery mode')
assertEqual(power.modeLabel({ isPresent: true, percentage: 0.5, state: states.Discharging }, false, states), 'Charging', 'power treats external power as newer than stale discharging state')
assert(power.batteryIcon({ isPresent: true, percentage: 0.4, state: states.Charging }, false, states).length > 0, 'power maps battery icons')
assertEqual(
power.batteryIcon({ isPresent: true, percentage: 0.4, state: states.Discharging }, false, states),
power.batteryIcon({ isPresent: true, percentage: 0.4, state: states.Charging, changeRate: 1.0, timeToFull: 120 }, false, states),
'power shows charging icon when external power is present before battery state refreshes'
power.modeLabel(
{ isPresent: true, percentage: 0.5, state: states.Discharging },
power.isDischarging({ isPresent: true, state: states.Discharging }, false, states),
states
),
'On battery',
'power labels battery mode when OnBattery is stale after unplug'
)
assert(power.batteryIcon({ isPresent: true, percentage: 0.4, state: states.Charging }, false, states).length > 0, 'power maps battery icons')
assertEqual(
power.batteryIcon({ isPresent: true, percentage: 0.4, state: states.Charging }, true, states),
power.batteryIcon(
{ isPresent: true, percentage: 0.4, state: states.Discharging },
power.isDischarging({ isPresent: true, state: states.Discharging }, false, states),
states
),
power.batteryIcon({ isPresent: true, percentage: 0.4, state: states.Discharging }, true, states),
'power shows battery icon when unplugged before battery state refreshes'
'power shows the battery icon when OnBattery is stale after unplug'
)
assertEqual(
power.batteryIcon(
{ isPresent: true, percentage: 0.4, state: states.Charging },
power.isDischarging({ isPresent: true, state: states.Charging }, true, states),
states
),
power.batteryIcon({ isPresent: true, percentage: 0.4, state: states.Charging }, false, states),
'power shows the charging icon when OnBattery is stale after plugging in'
)

assert(!power.chargeThresholdActive({ isPresent: true, percentage: 0.8, state: states.Charging, changeRate: 0, timeToFull: 0 }, false, states, true, 'off'), 'power does not flag stalled charging as threshold when limit is off')
Expand Down
21 changes: 14 additions & 7 deletions test/shell.d/powerprofiles-set-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,12 @@ chmod +x "$tmp_dir/bin/powerprofilesctl"
cat >"$tmp_dir/bin/busctl" <<'EOF'
#!/bin/bash

if [[ ${ON_BATTERY:-0} == "1" ]]; then
echo "b true"
else
echo "b false"
fi
# Autodetect reads the battery's state; OnBattery is answered separately so a
# test can leave it stale while the battery reports discharging.
case "${!#}" in
State) printf 'u %s\n' "${BATTERY_STATE:-1}" ;;
OnBattery) printf 'b %s\n' "${ON_BATTERY:-false}" ;;
esac
EOF
chmod +x "$tmp_dir/bin/busctl"

Expand Down Expand Up @@ -63,12 +64,18 @@ pass "power profile stores battery preference separately"
[[ $(tail -n 1 "$tmp_dir/calls") == "balanced" ]] || fail "battery preference does not replace AC preference"
pass "power profile keeps AC and battery preferences separate"

ON_BATTERY=1 "$ROOT/bin/omarchy-powerprofiles-set"
BATTERY_STATE=2 "$ROOT/bin/omarchy-powerprofiles-set"
[[ $(tail -n 1 "$tmp_dir/calls") == "performance" ]] || fail "autodetect restores battery preference"
pass "power profile autodetect restores battery preference"

# upowerd can keep OnBattery false after unplug while the battery reports
# discharging; autodetect must follow the battery.
BATTERY_STATE=2 ON_BATTERY=false "$ROOT/bin/omarchy-powerprofiles-set"
[[ $(tail -n 1 "$tmp_dir/calls") == "performance" ]] || fail "autodetect follows the battery over a stale OnBattery"
pass "power profile autodetect follows the battery over a stale OnBattery"

rm "$tmp_dir/state/ac"
ON_BATTERY=0 "$ROOT/bin/omarchy-powerprofiles-set"
BATTERY_STATE=1 "$ROOT/bin/omarchy-powerprofiles-set"
[[ $(tail -n 1 "$tmp_dir/calls") == "performance" ]] || fail "power profile uses performance as AC default"
pass "power profile retains performance as AC default"

Expand Down