Commit 0b14fd8a authored by Glen Mével's avatar Glen Mével

`battery-monitor.sh`: display warnings and take power-saving actions when battery becomes low

parent 0b1bd005
#!/bin/sh
# This script monitors the battery. It shows warnings and adjust power-saving
# parameters when the battery becomes low, and suspend/hibernate the computer
# when the battery becomes critically low.
# Battery thresholds, in percents.
THRESHOLD_LOW=20
THRESHOLD_VERY_LOW=10
THRESHOLD_CRITICAL=3
# Brightness at various stages of depletion, in percents.
BRIGHTNESS_NORMAL=20 # unused
BRIGHTNESS_SAVING=0.02
# Frequency at which to check the status of the battery.
FREQUENCY=30s
# This script depends on the program “acpi”.
command -V acpi >/dev/null || return 1
# How to ring the bell.
bell() {
duration="$1"
# Save the bell settings.
settings=( $(xset -q | grep bell | grep -o '[0-9]*') )
# Set up the bell.
xset b 100 800 "$duration"
# Ring the bell.
echo -ne '\a'
# Restore the bell settings.
xset b "${settings[@]}"
}
state=3
normal_brightness="$BRIGHTNESS_NORMAL"
while true ; do
sleep "$FREQUENCY"
output=( $(acpi) )
status="${output[2]%,}"
percentage="${output[3]%\%,}"
# Battery becomes charging, or charged enough.
if [ "$state" -lt 3 -a \( "$status" != 'Discharging' -o "$percentage" -gt "$THRESHOLD_LOW" \) ] ; then
state=3
xbacklight -set "$normal_brightness"
# Battery becomes low.
elif [ "$state" -gt 2 -a "$status" = 'Discharging' -a "$percentage" -le "$THRESHOLD_LOW" ] ; then
state=2
bell 100
notify-send -u critical -i battery-good-symbolic \
"Batterie à $percentage %" 'Je suis fatigué…'
normal_brightness="$(xbacklight)"
xbacklight -set "$BRIGHTNESS_SAVING"
# Battery becomes very low.
elif [ "$state" -gt 1 -a "$status" = 'Discharging' -a "$percentage" -le "$THRESHOLD_VERY_LOW" ] ; then
state=1
bell 100
# Note: notify-send is buggy, it picks the incorrect icon “battery-low”
# from /usr/share/notify-osd/icons/ instead of “battery-low-symbolic”
# from /usr/share/icons/.
notify-send -u critical -i battery-low-symbolic \
"Batterie à $percentage %" 'Mes paupières sont si lourdes…'
# Battery becomes critically low.
elif [ "$state" -gt 0 -a "$status" = 'Discharging' -a "$percentage" -le "$THRESHOLD_CRITICAL" ] ; then
state=0
bell 200
notify-send -u critical -i battery-caution-symbolic \
"Batterie à $percentage %" 'Je vais dormir.'
logger 'Battery critically low, going to sleep'
# FIXME: Set up the computer so that hibernation works.
# FIXME: Make sure this bypasses all existing inhibiters.
systemctl suspend
fi
done
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment