]> git.donarmstrong.com Git - bin.git/commitdiff
handle acpi batteries in pb properly
authorDon Armstrong <don@donarmstrong.com>
Fri, 2 Apr 2010 13:54:50 +0000 (13:54 +0000)
committerDon Armstrong <don@donarmstrong.com>
Fri, 2 Apr 2010 13:54:50 +0000 (13:54 +0000)
pb

diff --git a/pb b/pb
index 4d57fd13bdd7ceb2d3bdff67ecd2f5056d64fce6..f1d41b586b55207544a9f7f13003b19d9239fae9 100755 (executable)
--- a/pb
+++ b/pb
@@ -8,49 +8,69 @@ use POSIX qw(floor);
 
 use Term::ANSIColor qw(:constants);
 
-die "No information on battery 0\n" if not -r '/proc/pmu/battery_0';
+sub catfile{
+    my $temp = IO::File->new($_[0],'r') or
+       die "Unable to open $_[0] for reading: $!";
+    my $foo = <$temp>;
+    chomp $foo;
+    return $foo;
+}
 
-my $bat_inf_fh = new IO::File '/proc/pmu/battery_0';
+my %bat_inf;
 
-my $max_out = -1300;
-my $max_in = 1700;
+if (-r '/proc/pmu/battery_0') {
+    my $bat_inf_fh = new IO::File '/proc/pmu/battery_0';
+    my $max_out = -1300;
+    my $max_in = 1700;
 
-my $battery_info = {};
+    my $battery_info = {};
 
-while (<$bat_inf_fh>) {
-     if (/(.+?)\s*:\s*(-?\d+)/) {
-         $battery_info->{$1} = int $2;
-     }
+    while (<$bat_inf_fh>) {
+       if (/(.+?)\s*:\s*(-?\d+)/) {
+           $battery_info->{$1} = int $2;
+       }
+    }
+    undef $bat_inf_fh;
+    $bat_inf{charging} = hex $battery_info->{flags} & 2;
+    $bat_inf{current} = 0;
+    if ($battery_info->{current} > 0) {
+       $bat_inf{current} = floor($battery_info->{current}/$max_in*1000)/10;
+    }
+    else {
+       $bat_inf{current} = floor($battery_info->{current}/$max_out*1000)/10; 
+    }
+    $bat_inf{percent} = floor($battery_info->{'charge'}/$battery_info->{'max_charge'}*1000)/10;
+    $bat_inf{time_rem} = $battery_info->{'time rem.'};
+}
+elsif (-r '/sys/devices/platform/smapi/BAT0/remaining_capacity') {
+    $bat_inf{percent} = catfile('/sys/devices/platform/smapi/BAT0/remaining_percent');
+    $bat_inf{charging} = catfile('/sys/devices/platform/smapi/BAT0/state') ne 'discharging';
+    $bat_inf{time_rem} = $bat_inf{charging} ? catfile('/sys/devices/platform/smapi/BAT0/remaining_charging_time') : 
+       catfile('/sys/devices/platform/smapi/BAT0/remaining_running_time');
+    $bat_inf{time_rem} !~ /\d/ and $bat_inf{time_rem} = 0;
+    $bat_inf{time_rem} *= 60;
+    $bat_inf{current} = -catfile('/sys/devices/platform/smapi/BAT0/current_now') / 10;}
+else {
+    print STDERR "No battery information\n";
+    exit 1;
 }
 
-undef $bat_inf_fh;
 
 #use Data::Dumper;
 #print Dumper($battery_info);
 
 
-if (exists $battery_info->{'time rem.'}) {
-     my $hours = int $battery_info->{'time rem.'}/3600;
-     my $minutes = (int $battery_info->{'time rem.'}/60)%60;
-     my $seconds = $battery_info->{'time rem.'}%60;
-     my $is_charging = hex $battery_info->{flags} & 2;
-     my $charge_state = $is_charging?'Charging':'Discharging';
-     # TODO Split charging and discharging current
-     my $current;
-     if ($battery_info->{current} > 0) {
-         $current = floor($battery_info->{current}/$max_in*1000)/10;
-     }
-     else {
-         $current = floor($battery_info->{current}/$max_out*1000)/10; 
-     }
-
-     my $percentage = floor($battery_info->{'charge'}/$battery_info->{'max_charge'}*1000)/10;
-     if ($is_charging) {
-         print BOLD GREEN;
-     }
-     else {
-         print BOLD RED;
-     }
-     print "$charge_state at $current\%: $hours hours, $minutes minutes, and $seconds seconds remaining. [$percentage\%]\n";
-     print RESET;
+my $hours = int $bat_inf{time_rem}/3600;
+my $minutes = (int $bat_inf{time_rem}/60)%60;
+my $seconds = $bat_inf{time_rem}%60;
+my $current = $bat_inf{current};
+my $charge_state = $bat_inf{charging}?'Charging':'Discharging';
+my $percentage = $bat_inf{percent};
+if ($bat_inf{charging}) {
+    print BOLD GREEN;
+}
+else {
+    print BOLD RED;
 }
+print "$charge_state at $current\%: $hours hours, $minutes minutes, and $seconds seconds remaining. [$percentage\%]\n";
+print RESET;