acpid.pl: script that triggers pm-hibernate via consolekit stuff when battery charge drops lower than 10%
This commit is contained in:
118
acpid.pl
Executable file
118
acpid.pl
Executable file
@@ -0,0 +1,118 @@
|
||||
#!/usr/bin/perl
|
||||
|
||||
use strict;
|
||||
use warnings "all";
|
||||
use Fcntl;
|
||||
|
||||
$0 = 'acpid.pl';
|
||||
my $debug = 0;
|
||||
|
||||
sub logger ($) {
|
||||
if ($debug != 0) {
|
||||
my $msg = shift;
|
||||
$msg = localtime() . ' ' . $msg;
|
||||
syswrite STDOUT, $msg;
|
||||
}
|
||||
}
|
||||
|
||||
my $base = '/sys/class/power_supply/BAT0';
|
||||
|
||||
unless(-f "$base/present") {
|
||||
logger("Battery info at /sys/class/BAT0/present not found, quitting.\n");
|
||||
exit 0;
|
||||
}
|
||||
|
||||
my $battery_present = 2;
|
||||
my $battery_check = 2;
|
||||
my $charging = 2;
|
||||
|
||||
while ( sleep(180) ) {
|
||||
# check that battery is in place
|
||||
open(ACPI, "$base/present") or next;
|
||||
my $buf = '';
|
||||
|
||||
if (read(ACPI, $buf, 1)) {
|
||||
if ($battery_check == 0) {
|
||||
logger("Ability to read from /sys/class/BAT0/present restored\n");
|
||||
$battery_check = 1;
|
||||
}
|
||||
|
||||
if ($buf == '0') {
|
||||
if ($battery_present != 0) {
|
||||
logger("We have no battery, skipping charge level polling until it appears\n");
|
||||
$battery_present = 0;
|
||||
}
|
||||
|
||||
close ACPI;
|
||||
next;
|
||||
}
|
||||
} else {
|
||||
if ($battery_check != 0) {
|
||||
logger("Unable to read from /sys/class/BAT0/present\n");
|
||||
$battery_check = 0;
|
||||
}
|
||||
|
||||
close ACPI;
|
||||
next;
|
||||
}
|
||||
|
||||
close ACPI;
|
||||
$battery_present = 1;
|
||||
|
||||
# check status, we have no need to poll battery if it charging/charged
|
||||
unless (-f "$base/status") {
|
||||
logger("Something broken in /sys, unable to find $base/status, quitting\n");
|
||||
exit 1;
|
||||
}
|
||||
|
||||
# TODO: file access save state and print on change
|
||||
open(STATUS, "$base/status") or do {
|
||||
logger("Unable to open $base/status\n");
|
||||
next;
|
||||
};
|
||||
|
||||
$buf = <STATUS>;
|
||||
close STATUS;
|
||||
|
||||
chomp($buf);
|
||||
|
||||
if ($buf ne 'Discharging') {
|
||||
if ($charging != 1) {
|
||||
logger("Battery charging.\n");
|
||||
$charging = 1;
|
||||
}
|
||||
next;
|
||||
} else {
|
||||
if ($charging != 0) {
|
||||
logger("Battery discharging.\n");
|
||||
$charging = 0;
|
||||
}
|
||||
}
|
||||
|
||||
# read charge state
|
||||
unless (-f "$base/capacity") {
|
||||
logger("Something broken in /sys, unable to find $base/capacity, quitting\n");
|
||||
exit 1;
|
||||
}
|
||||
|
||||
# TODO: file access save state and print on change
|
||||
open(CHARGE, "$base/capacity") or do {
|
||||
logger("Unable to open $base/capacity\n");
|
||||
next;
|
||||
};
|
||||
|
||||
read(CHARGE, $buf, 3) or do {
|
||||
logger("Unable to read $base/capacity\n");
|
||||
next;
|
||||
};
|
||||
|
||||
chomp($buf);
|
||||
|
||||
if ($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");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,13 +27,13 @@ sub zlist(@){
|
||||
my $item;
|
||||
|
||||
foreach $item (@list) {
|
||||
printf ("zipping: %s to %s.zip", $item, $item);
|
||||
zip [$item] => "$item.zip", -Level => Z_BEST_COMPRESSION, Minimal => 1, TextFlag => 0, BinModeIn => 1, AutoClose => 1, CanonicalName => 1;
|
||||
# printf ("zipping: %s to %s.zip", $item, $item);
|
||||
zip [$item] => "$item.zip", -Level => Z_BEST_COMPRESSION, Minimal => 1, TextFlag => 1, BinModeIn => 1, AutoClose => 1, CanonicalName => 1;
|
||||
|
||||
if(length($ZipError) > 0){
|
||||
printf ("Warning: unable to zip %s - %s\n", $item, $ZipError);
|
||||
}else{
|
||||
printf (" and deleting source file\n");
|
||||
# printf (" and deleting source file\n");
|
||||
unlink($item);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user