diff --git a/.xsessionrc b/.xsessionrc index b270226..eb8f07e 100755 --- a/.xsessionrc +++ b/.xsessionrc @@ -13,6 +13,7 @@ if [[ "$DESKTOP_SESSION" = "dwm" || "$DESKTOP_SESSION" == "dwm-gnome" ]]; then dunst & numlockx on & nm-applet & + battery-watch & if command -v barrier &> /dev/null ; then barrier & diff --git a/bin/battery-watch b/bin/battery-watch new file mode 100755 index 0000000..e96cba3 --- /dev/null +++ b/bin/battery-watch @@ -0,0 +1,43 @@ +#!/bin/sh + +# https://www.reddit.com/r/suckless/comments/m6r71v/comment/gr7t2z1/?utm_source=reddit&utm_medium=web2x&context=3 + +# Control variable +# Possible values: NONE, FULL, LOW, CRITICAL +last="NONE" + +# Default values for LOW/CRITICAL status +low=15 +critical=10 + +while true; do + + # If battery is plugged, do stuff + battery="/sys/class/power_supply/BAT0" + if [ -d $battery ]; then + + capacity=$(cat $battery/capacity) + status=$(cat $battery/status) + + # If battery full and not already warned about that + if [ "$last" != "FULL" ] && [ "$status" = "Full" ]; then + notify-send "Battery full" + last="FULL" + fi + + # If low and discharging + if [ "$last" != "LOW" ] && [ "$status" = "Discharging" ] && \ + [ $capacity -le $low ]; then + notify-send "Battery low: $capacity%" + last=LOW + fi + + # If critical and discharging + if [ "$status" = "Discharging" ] && [ $capacity -le $critical ]; then + notify-send -u critical "Battery very low: $capacity%" + last=CRITICAL + fi + fi + sleep 60 +done +