summaryrefslogtreecommitdiff
path: root/dotfiles/dwm/.local/bin/battery_monitor
diff options
context:
space:
mode:
authorCraig Jennings <c@cjennings.net>2026-01-26 17:27:50 -0600
committerCraig Jennings <c@cjennings.net>2026-01-26 17:27:50 -0600
commita80dbca48b09ff50274e34fbf27c09c475aa09c7 (patch)
tree58198a3defb7320d7a21959a92cd84a004e9f27e /dotfiles/dwm/.local/bin/battery_monitor
parent0c182874d924d79ab5fba7f8f0172c5a4240a27a (diff)
refactor(scripts): reorganize scripts by desktop environment
Move X11/DWM-specific scripts to dotfiles/dwm/.local/bin/: - All dmenu* scripts (7) - All statusbar/sb-* scripts (24) - Display scripts: brightness, displayselect, monitor, colorpick - Input scripts: remaps, toggle-touchpad, samedir - Menu scripts: exitmenu, prompt, airplanemodetoggle, audioselect - Media scripts: screenshotmenu, recordnow, setbg, wallsearch, bookfind - Misc: battery_monitor, td-toggle Delete obsolete scripts: - build.emacs.aur.sh (redundant with build-emacs.sh) - bsdnet_bounce (FreeBSD only) - calibre-install (just curls installer) - debugemacs (hardcoded paths) - gruv (duplicate of alias) - reset-auth (unclear purpose) - touchpad-indicator-start (obsolete daemon) - starth (unused Hyprland starter) Universal scripts remain in dotfiles/system/.local/bin/ for both X11 and Wayland installs. Also update todo.org: cancel pywal and waybar netspeed tasks, mark desktop file pruning done. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Diffstat (limited to 'dotfiles/dwm/.local/bin/battery_monitor')
-rwxr-xr-xdotfiles/dwm/.local/bin/battery_monitor52
1 files changed, 52 insertions, 0 deletions
diff --git a/dotfiles/dwm/.local/bin/battery_monitor b/dotfiles/dwm/.local/bin/battery_monitor
new file mode 100755
index 0000000..dc8d5ea
--- /dev/null
+++ b/dotfiles/dwm/.local/bin/battery_monitor
@@ -0,0 +1,52 @@
+#!/usr/bin/env bash
+# battery_monitor
+# Intended to be run via .xinitrc
+# - Exit automatically if no battery (desktop)
+# - When below 15%, warn user of low battery
+# - When below 10%, suspend within 10 seconds if not charging
+#
+# Craig Jennings <c@cjennings.net>
+
+# check if acpi is installed
+if ! command -v acpi &> /dev/null; then
+ echo "acpi is not installed. Cannot continue. Exiting...."
+ exit 1
+fi
+
+# exit if a battery exists
+if [ ! -d "/sys/class/power_supply/BAT0" ] && [ ! -d "/sys/class/power_supply/BAT1" ]; then
+ echo "Acpi is installed but no battery detected. Assuming this is a desktop and exiting...."
+ exit 1
+fi
+
+while true; do
+ # Get the current battery percentage using acpi
+ battery_percentage=$(acpi -b | awk -F ', ' '{print $2}' | tr -d '%')
+ # battery_percentage=$(acpi -b | awk -F ', ' '{print $2}' | sed 's/%//')
+
+ # When below 10%, suspend within 10 seconds if not charging
+ if [ "$battery_percentage" -lt 11 ] && ! acpi -a | grep -q "on-line" ; then
+ # Send a notification of sleeping in 10 seconds
+ notify-send -u critical "Critical Battery" "Battery is at $battery_percentage%. System entering sleep in 30 seconds."
+
+ # sleep for 10 seconds, then abort if charging
+ sleep 30
+
+ # Check if the system is charging (AC adapter connected)
+ if acpi -a | grep -q "on-line"; then
+ notify-send "Charging" "The system is now charging. No action taken."
+ else
+ notify-send -u critical "Critical Battery" "Putting the system to sleep."
+ sudo systemctl suspend
+ fi
+ fi
+
+ # When below 15%, warn user
+ if [ "$battery_percentage" -lt 15 ] && ! acpi -a | grep -q "on-line" ; then
+ # Send a notification using notify-send and dunst
+ notify-send -u critical "Low Battery" "Battery is at $battery_percentage%. System will automatically sleep at 10%."
+ fi
+
+ # Sleep for 5 minutes before checking again
+ sleep 300
+done