--- trunk/dwm-status.pl 2008/02/15 22:01:01 67 +++ trunk/dwm-status.pl 2008/07/22 01:16:52 81 @@ -91,41 +91,44 @@ 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'; while ( 1 ) { my $s = strftime("%Y-%m-%d %H:%M:%S", localtime()); - if ( $i % $acpi_every == 0 && $proc_acpi_battery ) { - - my $state = proc2hash( "$proc_acpi_battery/state" ); + if ( $i % $acpi_every == 0 ) { + if ( $proc_acpi_battery ) { - if ( $state->{'present rate'} != 0 ) { - my $info = proc2hash( "$proc_acpi_battery/info" ); + my $state = proc2hash( "$proc_acpi_battery/state" ); - my $pcnt = $state->{'remaining capacity'} / $info->{'design capacity'}; + if ( $state->{'present rate'} =~ m/^\d+$/ && $state->{'present rate'} != 0 ) { + my $info = proc2hash( "$proc_acpi_battery/info" ); - 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 $pcnt = $state->{'remaining capacity'} / $info->{'design capacity'}; - warn "time = $time\n" if ($debug); + 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 $hh = int( $time ); - my $mm = int( ( $time - $hh ) * 60 ); - my $ss = ( $time * 3600 ) % 60; + warn "time = $time\n" if ($debug); - $bat = sprintf("%s %2d%% %02d:%02d:%02d %3.1fW!| ", - substr($state->{'charging state'},0,1), - $pcnt * 100, $hh, $mm, $ss, - $state->{'present rate'} / 1000 - ); + my $hh = int( $time ); + my $mm = int( ( $time - $hh ) * 60 ); + my $ss = ( $time * 3600 ) % 60; - } 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/; } @@ -190,7 +193,7 @@ print $out; if ( $awesome ) { open(my $fh, '|-', 'awesome-client') || die "can't pipe to awesome-client: $!"; - print $fh "0 widget_tell dwm-status $out\n"; + print $fh "0 widget_tell mystatusbar dwm-status text $out\n"; close($fh); }