feat: add battery conservation mode with Waybar toggle
Update PKGBUILD version / update-pkgver (push) Successful in 2s
Update PKGBUILD version / update-pkgver (push) Successful in 2s
Laptops with charge_control_end_threshold support get a click-to-toggle on the battery module (80% ↔ 100%). A ♥ icon appears when conservation is active, hidden when inactive. State persists across reboots via systemd oneshot service. udev rule grants wheel group write access so no sudo is needed for toggling.
This commit is contained in:
Executable
+28
@@ -0,0 +1,28 @@
|
||||
#!/usr/bin/bash
|
||||
# ABOUTME: Toggles battery conservation mode between 80% and 100% charge limit.
|
||||
# ABOUTME: Writes to sysfs (immediate) and state file (persistence across reboots).
|
||||
|
||||
THRESHOLD_FILE="/sys/class/power_supply/BAT0/charge_control_end_threshold"
|
||||
STATE_DIR="/var/lib/moonarch"
|
||||
STATE_FILE="${STATE_DIR}/batsaver-threshold"
|
||||
CONSERVATION_LIMIT=80
|
||||
|
||||
[[ -f "$THRESHOLD_FILE" ]] || exit 1
|
||||
|
||||
CURRENT=$(cat "$THRESHOLD_FILE")
|
||||
|
||||
if [[ "$CURRENT" -le "$CONSERVATION_LIMIT" ]]; then
|
||||
NEW=100
|
||||
else
|
||||
NEW="$CONSERVATION_LIMIT"
|
||||
fi
|
||||
|
||||
# Apply immediately
|
||||
echo "$NEW" > "$THRESHOLD_FILE" || exit 1
|
||||
|
||||
# Persist for next boot
|
||||
mkdir -p "$STATE_DIR"
|
||||
echo "$NEW" > "$STATE_FILE"
|
||||
|
||||
# Signal Waybar to refresh the batsaver module (SIGRTMIN+9)
|
||||
pkill -RTMIN+9 waybar
|
||||
Executable
+19
@@ -0,0 +1,19 @@
|
||||
#!/usr/bin/bash
|
||||
# ABOUTME: Waybar module showing battery conservation mode status.
|
||||
# ABOUTME: Outputs heart icon when active (threshold ≤80), empty when inactive.
|
||||
|
||||
THRESHOLD_FILE="/sys/class/power_supply/BAT0/charge_control_end_threshold"
|
||||
|
||||
# No battery threshold support → no output → Waybar hides module
|
||||
[[ -f "$THRESHOLD_FILE" ]] || exit 0
|
||||
|
||||
THRESHOLD=$(cat "$THRESHOLD_FILE")
|
||||
|
||||
if [[ "$THRESHOLD" -le 80 ]]; then
|
||||
jq --compact-output -n \
|
||||
--arg text "♥" \
|
||||
--arg tooltip "Battery Conservation: ON (limit ${THRESHOLD}%)" \
|
||||
--arg class "on" \
|
||||
'{text: $text, tooltip: $tooltip, class: $class}'
|
||||
fi
|
||||
# Threshold > 80 → no output → Waybar hides module
|
||||
@@ -0,0 +1,15 @@
|
||||
# ABOUTME: Restores battery charge threshold from saved state on boot.
|
||||
# ABOUTME: Only runs on laptops with threshold support and a saved state file.
|
||||
|
||||
[Unit]
|
||||
Description=Restore battery conservation mode threshold
|
||||
After=sysinit.target
|
||||
ConditionPathExists=/sys/class/power_supply/BAT0/charge_control_end_threshold
|
||||
ConditionPathExists=/var/lib/moonarch/batsaver-threshold
|
||||
|
||||
[Service]
|
||||
Type=oneshot
|
||||
ExecStart=/bin/sh -c 'cat /var/lib/moonarch/batsaver-threshold > /sys/class/power_supply/BAT0/charge_control_end_threshold'
|
||||
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
||||
@@ -0,0 +1,4 @@
|
||||
# ABOUTME: udev rule granting wheel group write access to battery charge threshold.
|
||||
# ABOUTME: Enables unprivileged toggling of conservation mode via moonarch-batsaver-toggle.
|
||||
|
||||
SUBSYSTEM=="power_supply", ATTR{type}=="Battery", RUN+="/bin/sh -c 'chgrp wheel /sys%p/charge_control_end_threshold 2>/dev/null; chmod g+w /sys%p/charge_control_end_threshold 2>/dev/null'"
|
||||
@@ -21,7 +21,7 @@
|
||||
"bluetooth",
|
||||
"group/sound",
|
||||
"backlight",
|
||||
"battery",
|
||||
"group/battery",
|
||||
"group/indicators"
|
||||
],
|
||||
"group/indicators": {
|
||||
@@ -58,6 +58,13 @@
|
||||
"transition-left-to-right": true
|
||||
}
|
||||
},
|
||||
"group/battery": {
|
||||
"orientation": "inherit",
|
||||
"modules": [
|
||||
"battery",
|
||||
"custom/batsaver"
|
||||
]
|
||||
},
|
||||
"group/sys": {
|
||||
"orientation": "inherit",
|
||||
"modules": [
|
||||
@@ -306,13 +313,26 @@
|
||||
},
|
||||
"format": "{capacity}% {icon}",
|
||||
"format-icons": [
|
||||
"",
|
||||
"",
|
||||
"",
|
||||
"",
|
||||
""
|
||||
"",
|
||||
"",
|
||||
"",
|
||||
"",
|
||||
"",
|
||||
"",
|
||||
"",
|
||||
"",
|
||||
"",
|
||||
"",
|
||||
""
|
||||
],
|
||||
"max-length": 25
|
||||
"max-length": 25,
|
||||
"on-click": "moonarch-batsaver-toggle"
|
||||
},
|
||||
"custom/batsaver": {
|
||||
"exec": "moonarch-waybar-batsaver",
|
||||
"return-type": "json",
|
||||
"interval": 30,
|
||||
"signal": 9
|
||||
},
|
||||
"bluetooth": {
|
||||
// "controller": "controller1", // specify the alias of the controller if there are more than 1 on the system
|
||||
|
||||
Reference in New Issue
Block a user