Skip to content
Closed
Show file tree
Hide file tree
Changes from 13 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
16 changes: 16 additions & 0 deletions bin/omarchy-battery-plugged
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/bin/bash

# omarchy:summary=Announce that the charger was just connected.
# omarchy:hidden=true

set -euo pipefail

# Nerd Font power plug. The icon theme is tried first; this is the fallback.
glyph=$'\U000f06a5'
status=$(omarchy-battery-status || true)

if [[ -n $status ]]; then
omarchy-notification-send -g "$glyph" -u normal "$(omarchy-i18n battery.chargerConnectedTitle)" "$status" -i ac-adapter --transient
else
omarchy-notification-send -g "$glyph" -u normal "$(omarchy-i18n battery.chargerConnectedTitle)" "$(omarchy-i18n battery.chargerConnectedBody)" -i ac-adapter --transient
fi
36 changes: 36 additions & 0 deletions bin/omarchy-battery-status
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

shell_output=false
power_supply_path="${OMARCHY_POWER_SUPPLY_PATH:-/sys/class/power_supply}"
hwmon_path="${OMARCHY_HWMON_PATH:-/sys/class/hwmon}"
sleep_run_dir="${OMARCHY_SLEEP_RUN_DIR:-/run/omarchy}"
battery_state_dir="${OMARCHY_BATTERY_STATE_DIR:-${XDG_STATE_HOME:-$HOME/.local/state}/omarchy/battery}"

Expand Down Expand Up @@ -229,6 +230,41 @@ if [[ $shell_output == "true" ]]; then
fi
fi

# The connected charger: what it can give, and what is drawn from it now. A
# small charger under load leaves little for the battery, and the panel shows
# that split instead of a charge rate that looks like a fault. Apple Silicon
# reports the rating as input_power_limit on its Mains supply; USB-PD supplies
# elsewhere (UCSI) report the negotiated voltage_max and current_max. Below
# 5W is a plain USB port, not a charger rating.
charger_uw=0
for supply in "$power_supply_path"/*; do
[[ -r $supply/type && $(<"$supply/type") =~ ^(Mains|USB) ]] || continue
[[ $(read_sysfs_number "$supply/online" || true) == "1" ]] || continue
if supply_uw=$(read_sysfs_number "$supply/input_power_limit"); then
:
elif microvolts=$(read_sysfs_number "$supply/voltage_max") && microamps=$(read_sysfs_number "$supply/current_max"); then
supply_uw=$(( microvolts * microamps / 1000000 ))
else
continue
fi
if (( supply_uw > charger_uw )); then
charger_uw=$supply_uw
fi
done

if (( charger_uw >= 5000000 )); then
printf 'charger\t%sW\n' "$(( (charger_uw + 500000) / 1000000 ))"

# Only a sensor gives the draw itself (the Apple SMC's "AC Input Power");
# without one the panel shows the rating alone.
for label in "$hwmon_path"/*/power*_label; do
[[ -r $label && $(<"$label") == "AC Input Power" ]] || continue
microwatts=$(read_sysfs_number "${label%_label}_input") || continue
printf 'charger_draw\t%sW\n' "$(awk -v microwatts="$microwatts" 'BEGIN { rounded = sprintf("%.1f", microwatts / 1000000); sub(/\.0$/, "", rounded); print rounded }')"
break
done
fi

# Charge limit control metadata for the power panel.
limit_supported=0
if [[ $upower_threshold_supported == "yes" || -r $battery_path/charge_control_end_threshold ]]; then
Expand Down
14 changes: 11 additions & 3 deletions bin/omarchy-powerprofiles-set
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,18 @@ 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.
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
elif [[ -z $state || $state == "u 0" ]]; then
Comment thread
chessing234 marked this conversation as resolved.
Resolved
# Unknown state: fall back to OnBattery, as the shell's power panel does.
if [[ $(busctl get-property org.freedesktop.UPower /org/freedesktop/UPower org.freedesktop.UPower OnBattery 2>/dev/null) == "b true" ]]; then
action=battery
else
action=ac
fi
else
action=ac
fi
Expand Down
2 changes: 2 additions & 0 deletions default/i18n/en/cli.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@
"audio.micOn": "Microphone on",
"audio.noDevices": "No audio devices found",
"audio.noMicrophone": "No microphone found",
"battery.chargerConnectedBody": "Now charging",
"battery.chargerConnectedTitle": "Charger connected",
"battery.durationHours": "%1h",
"battery.durationHoursMinutes": "%1h %2m",
"battery.durationMinutes": "%1m",
Expand Down
7 changes: 7 additions & 0 deletions default/i18n/en/shell.json
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,13 @@
"power.chargeLimit": "Charge limit",
"power.chargeLimitFailed": "Could not update charge limit",
"power.chargeLimitTo": "Charge to only %1",
"power.charger": "CHARGER",
"power.chargerFromBattery": "From battery",
"power.chargerMaxed": "The charger is at its limit: a stronger one would charge faster",
"power.chargerShort": "The charger can't keep up: the battery covers the rest",
"power.chargerToBattery": "To battery",
"power.chargerToSystem": "To system",
"power.chargerUse": "%1 of %2",
"power.charging": "Charging",
"power.chargingPhrase1": "Pumping power",
"power.chargingPhrase2": "Injecting electrons",
Expand Down
1 change: 1 addition & 0 deletions docs/notifications.md
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ The `notifications` IPC target also answers `ping` (used by `omarchy-notificatio
Everything goes through the same sender contract, so the pieces are small:

- **Low battery** — `omarchy-battery-low` sends a normal toast that expires after 30 seconds (paused, like any card, while the deck is open) and runs the `battery-low` hook. Not critical: a critical toast never expires and comes back on every shell start, so the warning would outlive the charger going in. As an `omarchy-action` toast it still gets through Do Not Disturb.
- **Charger connected** — when line power arrives after the machine has been on battery, `omarchy-battery-plugged` sends a normal transient toast. A flip shorter than two seconds is ignored, so a USB-C renegotiation or a UPower reading that settles at startup does not announce a charger that was not just plugged in. A confirmation already earned survives a reconnect inside that window, and is dropped only if the machine stays on battery through it.
- **Crash capture** — `omarchy-crash-watch` follows the systemd-coredump
journal stream and announces each crashed program (deduped per minute) as a
critical toast whose click runs `omarchy-agent-crash` (via `--exec`, so a
Expand Down
69 changes: 68 additions & 1 deletion shell/plugins/panels/power/Model.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,23 @@ function batteryFraction(device) {
return device && device.isPresent ? Math.max(0, Math.min(1, device.percentage)) : 0
}

// Read the battery's state; OnBattery can stay stale when an AC unplug is missed.
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
Comment thread
chessing234 marked this conversation as resolved.
Resolved
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 @@ -222,6 +239,52 @@ function usageChart(hours, nowSeconds) {
return { columns: columns, screen: screen, used: used }
}

// How the charger's power is shared out. Battery status reports the charger's
// rating ("140W") and, where a sensor gives it, the power drawn from it now
// ("136.9W"), with the battery's state and rate. The battery's state, not the
// plug, gives the direction: a charger too weak for the load leaves the battery
// draining while plugged in. Charging, the rate flows into the battery and the
// rest of the draw is what the system takes; draining, the whole draw goes to
// the system and the battery covers the rest (drain). Fractions are of the
// rating. maxed: the charger gives all it has while the battery gets a small
// share of it or none. Null without a charger rating.
function chargerFlow(batteryInfo) {
var info = batteryInfo || {}
var limit = parseFloat(info.charger)
if (!(limit > 0)) return null

var rate = Math.max(0, parseFloat(info.rate) || 0)
var charging = info.state === "charging"
var draining = info.state === "discharging"
var draw = parseFloat(info.charger_draw)
var hasDraw = draw >= 0
var battery = charging ? rate : 0
if (hasDraw) battery = Math.min(battery, draw)
var system = hasDraw ? Math.max(0, draw - battery) : -1

function fraction(watts) { return Math.max(0, Math.min(1, watts / limit)) }

return {
limit: limit,
draw: hasDraw ? draw : -1,
battery: battery,
drain: draining ? rate : 0,
system: system,
batteryFraction: fraction(battery),
systemFraction: hasDraw ? fraction(system) : 0,
maxed: hasDraw && (charging || draining) && draw >= limit * 0.93 && battery < limit * 0.3
}
}

// A wattage in the shape battery status writes one, for localizeReading:
// whole watts from 10W up, one decimal below that.
function wattsReading(watts) {
var value = Math.max(0, Number(watts) || 0)
if (value >= 10) return Math.round(value) + "W"
var rounded = String(Math.round(value * 10) / 10)
return rounded + "W"
}

if (typeof module !== "undefined") {
module.exports = {
clampIndex: clampIndex,
Expand All @@ -230,6 +293,8 @@ if (typeof module !== "undefined") {
parseProfiles: parseProfiles,
profileIcon: profileIcon,
batteryFraction: batteryFraction,
isDischarging: isDischarging,
isCharging: isCharging,
chargeThresholdActive: chargeThresholdActive,
batteryIcon: batteryIcon,
modeLabel: modeLabel,
Expand All @@ -241,6 +306,8 @@ if (typeof module !== "undefined") {
formatSleepTooltip: formatSleepTooltip,
isLowS0ix: isLowS0ix,
moveFocus: moveFocus,
usageChart: usageChart
usageChart: usageChart,
chargerFlow: chargerFlow,
wattsReading: wattsReading
}
}
Loading