--- trunk/dwm-status.pl 2008/05/14 16:28:17 79 +++ trunk/dwm-status.pl 2008/07/22 01:15:19 80 @@ -91,41 +91,53 @@ my ( $lrx, $ltx ) = ( 0, 0 ); my ( $ld_r, $ld_w ) = ( 0, 0 ); -my $bat; +my $bat = ''; my $i = 0; +my $sys_fs = '/sys/class/power_supply/BAT0'; +sub read_file { + my $path = shift; + open(my $fh, '<', $path) || die "can't open $path: $!"; + my $c = <$fh>; + chomp($c); + close($fh); + return $c; +} while ( 1 ) { my $s = strftime("%Y-%m-%d %H:%M:%S", localtime()); - if ( $i % $acpi_every == 0 && $proc_acpi_battery ) { + if ( $i % $acpi_every == 0 ) { + if ( $proc_acpi_battery ) { - my $state = proc2hash( "$proc_acpi_battery/state" ); + my $state = proc2hash( "$proc_acpi_battery/state" ); - if ( $state->{'present rate'} != 0 ) { - my $info = proc2hash( "$proc_acpi_battery/info" ); + if ( $state->{'present rate'} =~ m/^\d+$/ && $state->{'present rate'} != 0 ) { + my $info = proc2hash( "$proc_acpi_battery/info" ); - my $pcnt = $state->{'remaining capacity'} / $info->{'design capacity'}; + my $pcnt = $state->{'remaining capacity'} / $info->{'design capacity'}; - my $time = $state->{'remaining capacity'} / ( $state->{'present rate'} ); - $time = ( $info->{'design capacity'} - $state->{'remaining capacity'} ) / $state->{'present rate'} if ( $state->{'charging state'} eq 'charging' ); + my $time = $state->{'remaining capacity'} / ( $state->{'present rate'} ); + $time = ( $info->{'design capacity'} - $state->{'remaining capacity'} ) / $state->{'present rate'} if ( $state->{'charging state'} eq 'charging' ); - warn "time = $time\n" if ($debug); + warn "time = $time\n" if ($debug); - my $hh = int( $time ); - my $mm = int( ( $time - $hh ) * 60 ); - my $ss = ( $time * 3600 ) % 60; + my $hh = int( $time ); + my $mm = int( ( $time - $hh ) * 60 ); + my $ss = ( $time * 3600 ) % 60; - $bat = sprintf("%s %2d%% %02d:%02d:%02d %3.1fW!| ", - substr($state->{'charging state'},0,1), - $pcnt * 100, $hh, $mm, $ss, - $state->{'present rate'} / 1000 - ); - - } else { - $bat = ''; + $bat = sprintf("%s %2d%% %02d:%02d:%02d %3.1fW!| ", + substr($state->{'charging state'},0,1), + $pcnt * 100, $hh, $mm, $ss, + $state->{'present rate'} / 1000 + ); } + } elsif ( -e $sys_fs ) { + my $full = read_file( "$sys_fs/charge_full" ); + my $now = read_file( "$sys_fs/charge_now" ); + $bat = sprintf("%2d%% | ", $now * 100 / $full ); + } } else { $bat =~ s/!(\|\s)$/ $1/; }