diff options
Diffstat (limited to 'x/battery')
-rwxr-xr-x | x/battery | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/x/battery b/x/battery new file mode 100755 index 0000000..10a3673 --- /dev/null +++ b/x/battery @@ -0,0 +1,48 @@ +#!/usr/bin/env perl +$| = 1; + +my $CRITICAL = undef; +my $LOW = undef; +my $CHARGING = undef; +my $DEAD_LEVEL = 2; +my $CRITICAL_LEVEL = 5; +my $LOW_LEVEL = 10; + +while (1) { + my @acpi = split " ", `acpi | grep "Discharging" | grep -v "rate information"`; + my $battery_level = $acpi[3]; + my $status = $acpi[2]; + my $sent = undef; + $battery_level =~ s/%,//g; + $status =~ s/,//g; + + if ($status =~ "Discharging") { + $CHARGING = undef; + if (!$CRITICAL) { + if (int($battery_level) <= int($CRITICAL_LEVEL)) { + $CRITICAL = 'def'; + $sent = 'def'; + system "notify-send -i \"battery-empty-symbolic\" -t 0 -u critical \"BATTERY CRITICAL\" \"Battery level is ${battery_level}%\n\nCharge the system NOW.\"" + } + } if (!$LOW && !$sent) { + if (int($battery_level) <= int($LOW_LEVEL)) { + $LOW = 'def'; + $sent = 'def'; + system "notify-send -i \"battery-caution-symbolic\" -t 0 -u normal \"BATTERY LOW\" \"Battery level is ${battery_level}%\n\nCharge the system soon.\"" + } + } if (int($battery_level) <= int($DEAD_LEVEL)) { + system "notify-send -t 0 -u critical \"SHUTTING DOWN\" \"Battery level is too low. The system will shutdown in 2 minutes to prevent corruption.\n\nCharge the system NOW to cancel the shutdown.\""; + system "doas shutdown -Ph 2 &"; + } + } else { + if (!$CHARGING) { + $CHARGING = 'def'; + $CRITICAL = undef; + $LOW = undef; + system "doas shutdown -c"; + system "notify-send -t 3000 -i \"battery-good-charging-symbolic\" \"System is now charging\""; + } + } + + sleep 30; +} |