aboutsummaryrefslogtreecommitdiff
path: root/x/battery
blob: 10a3673b59f735524aea98d37c9963bd2fcd71d4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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;
}