diff --git a/acpid.pl b/acpid.pl index 09355df..116bd74 100755 --- a/acpid.pl +++ b/acpid.pl @@ -15,6 +15,42 @@ sub logger ($) { } } +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 = ; + 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'; unless(-f "$base/present") { @@ -30,6 +66,7 @@ while ( sleep(180) ) { # check that battery is in place open(ACPI, "$base/present") or next; my $buf = ''; + my $ac = 0; # assume, we're on battery if (read(ACPI, $buf, 1)) { if ($battery_check == 0) { @@ -81,7 +118,8 @@ while ( sleep(180) ) { logger("Battery charging.\n"); $charging = 1; } - next; + + $ac = 1; } else { if ($charging != 0) { logger("Battery discharging.\n"); @@ -107,12 +145,15 @@ while ( sleep(180) ) { }; chomp($buf); - - if ($buf < 10) { + + if (($ac == 0) and ($buf < 10)) { 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`; } else { logger("Charge is $buf%\n"); + + if (($ac == 1) and ($buf > 100)) { + system ('notify-send', '-u', 'normal', '-t', '3000', 'Battery overcharging'); + } } } -