acpid.pl: now it sends desktop notification if charge too low
This commit is contained in:
120
acpid.pl
120
acpid.pl
@@ -1,56 +1,20 @@
|
|||||||
#!/usr/bin/perl
|
#!/usr/bin/perl
|
||||||
# hibernates laptop if battery discharging and charge is too low
|
#
|
||||||
|
# Hibernates system if battery charge drops under $MIN_CHARGE and battery discharging
|
||||||
|
|
||||||
|
|
||||||
use strict;
|
use strict;
|
||||||
use warnings "all";
|
use warnings "all";
|
||||||
use Fcntl;
|
use Fcntl;
|
||||||
|
|
||||||
|
sub logger($); # logs messages if $debug != 0
|
||||||
|
# maybe one time i'll use it
|
||||||
|
sub i3wmstate; # returns 1 if i3wm running, else - 0
|
||||||
|
|
||||||
|
|
||||||
$0 = 'acpid.pl';
|
$0 = 'acpid.pl';
|
||||||
my $debug = 0;
|
my $debug = 0;
|
||||||
|
my $MIN_CHARGE = 10;
|
||||||
sub logger ($) {
|
|
||||||
if ($debug != 0) {
|
|
||||||
my $msg = shift;
|
|
||||||
$msg = localtime() . ' ' . $msg;
|
|
||||||
syswrite STDOUT, $msg;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
sub i3wmstate {
|
|
||||||
my $flag = 0;
|
|
||||||
opendir(P, '/proc') || die 'no /proc found!';
|
|
||||||
|
|
||||||
while (readdir(P)) {
|
|
||||||
my $pid = $_;
|
|
||||||
next unless(defined($pid));
|
|
||||||
next if ($pid eq '');
|
|
||||||
|
|
||||||
if (-d "/proc/$pid") {
|
|
||||||
# dir UID == APCID.pl process user owner
|
|
||||||
if ((stat("/proc/$pid"))[4] == int(getpwnam($ENV{'LOGNAME'}))) {
|
|
||||||
next unless(-r "/proc/$pid/cmdline");
|
|
||||||
open(CMDLINE, "/proc/$pid/cmdline") || die "unable to open /proc/$pid/cmdline";
|
|
||||||
my $cl = <CMDLINE>;
|
|
||||||
close(CMDLINE);
|
|
||||||
|
|
||||||
if(defined($cl)) {
|
|
||||||
if ((substr($cl, 0, 2) eq 'i3') and (length($cl) <= 4)) {
|
|
||||||
$flag = 1;
|
|
||||||
undef $cl;
|
|
||||||
undef $pid;
|
|
||||||
last;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
undef $cl;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
undef $pid;
|
|
||||||
}
|
|
||||||
|
|
||||||
return $flag;
|
|
||||||
}
|
|
||||||
|
|
||||||
my $base = '/sys/class/power_supply/BAT0';
|
my $base = '/sys/class/power_supply/BAT0';
|
||||||
|
|
||||||
@@ -67,7 +31,6 @@ while ( sleep(180) ) {
|
|||||||
# check that battery is in place
|
# check that battery is in place
|
||||||
open(ACPI, "$base/present") or next;
|
open(ACPI, "$base/present") or next;
|
||||||
my $buf = '';
|
my $buf = '';
|
||||||
my $ac = 0; # assume, we're on battery
|
|
||||||
|
|
||||||
if (read(ACPI, $buf, 1)) {
|
if (read(ACPI, $buf, 1)) {
|
||||||
if ($battery_check == 0) {
|
if ($battery_check == 0) {
|
||||||
@@ -119,8 +82,6 @@ while ( sleep(180) ) {
|
|||||||
logger("Battery charging.\n");
|
logger("Battery charging.\n");
|
||||||
$charging = 1;
|
$charging = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
$ac = 1;
|
|
||||||
} else {
|
} else {
|
||||||
if ($charging != 0) {
|
if ($charging != 0) {
|
||||||
logger("Battery discharging.\n");
|
logger("Battery discharging.\n");
|
||||||
@@ -141,20 +102,69 @@ while ( sleep(180) ) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
read(CHARGE, $buf, 3) or do {
|
read(CHARGE, $buf, 3) or do {
|
||||||
logger("Unable to read $base/capacity\n");
|
logger("Unable to read $base/capacity, but file is in place\n");
|
||||||
next;
|
next;
|
||||||
};
|
};
|
||||||
|
|
||||||
chomp($buf);
|
chomp($buf); # just in case
|
||||||
|
|
||||||
if (($ac == 0) and ($buf < 10)) {
|
if ($buf < $MIN_CHARGE) {
|
||||||
logger("Battery charge is less than 10%, hibernating\n");
|
logger("Battery charge is less than 10%, hibernating\n");
|
||||||
`dbus-send --system --print-reply --dest="org.freedesktop.UPower" /org/freedesktop/UPower org.freedesktop.UPower.Hibernate`;
|
`dbus-send --system --print-reply --dest="org.freedesktop.UPower" /org/freedesktop/UPower org.freedesktop.UPower.Hibernate`;
|
||||||
} else {
|
} else {
|
||||||
logger("Charge is $buf%\n");
|
if ($buf < ($MIN_CHARGE + 5)) {
|
||||||
|
`notify-send --urgency=critical --expire-time=10000 --icon=battery-caution "Low battery charge: $buf%"`;
|
||||||
if (($ac == 1) and ($buf > 100)) {
|
|
||||||
system ('notify-send', '-u', 'normal', '-t', '3000', 'Battery overcharging');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ( ($charging == 1) and ( $buf > 100) ) {
|
||||||
|
`notify-send --urgency=normal --expire-time=3000 "Battery overcharging"`;
|
||||||
|
}
|
||||||
|
|
||||||
|
logger("Charge is $buf%\n");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
sub logger ($) {
|
||||||
|
if ($debug != 0) {
|
||||||
|
my $msg = shift;
|
||||||
|
$msg = localtime() . ' ' . $msg;
|
||||||
|
syswrite STDOUT, $msg;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
sub i3wmstate {
|
||||||
|
my $flag = 0;
|
||||||
|
opendir(P, '/proc') || die 'no /proc found!';
|
||||||
|
|
||||||
|
while (readdir(P)) {
|
||||||
|
my $pid = $_;
|
||||||
|
next unless(defined($pid));
|
||||||
|
next if ($pid eq '');
|
||||||
|
|
||||||
|
if (-d "/proc/$pid") {
|
||||||
|
# dir UID == APCID.pl process user owner
|
||||||
|
if ((stat("/proc/$pid"))[4] == int(getpwnam($ENV{'LOGNAME'}))) {
|
||||||
|
next unless(-r "/proc/$pid/cmdline");
|
||||||
|
open(CMDLINE, "/proc/$pid/cmdline") || die "unable to open /proc/$pid/cmdline";
|
||||||
|
my $cl = <CMDLINE>;
|
||||||
|
close(CMDLINE);
|
||||||
|
|
||||||
|
if(defined($cl)) {
|
||||||
|
if ((substr($cl, 0, 2) eq 'i3') and (length($cl) <= 4)) {
|
||||||
|
$flag = 1;
|
||||||
|
undef $cl;
|
||||||
|
undef $pid;
|
||||||
|
last;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
undef $cl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
undef $pid;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $flag;
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user