--- trunk/dwm-status.pl 2007/06/15 20:34:04 40 +++ trunk/dwm-status.pl 2009/02/24 21:27:17 108 @@ -16,6 +16,7 @@ my $disk_blk_size = 512; my $debug = shift @ARGV; +my $awesome = 3; $|=1; @@ -27,7 +28,7 @@ while(<$fh>) { chomp; my ( $key, $value ) = split(/:\s+/, $_, 2); - $value =~ s/ m[VW]h*$//; + $value =~ s/ m[AVW]h*$//; $h->{$key} = $value; } warn "$f ",dump( $h ) if ( $debug ); @@ -56,7 +57,7 @@ find_proc_acpi('battery', sub { my $bat = shift; - return unless $bat =~ /BAT/; + return unless $bat =~ /\w+/; warn "# testing battery $bat\n" if ( $debug ); @@ -90,40 +91,48 @@ 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 ) { + if ( $i % $acpi_every == 0 ) { + my $sysfs_path = glob "$sys_fs/*_full"; + if ( $sysfs_path ) { - my $state = proc2hash( "$proc_acpi_battery/state" ); + my $full = read_file( $sysfs_path ); + $sysfs_path =~ s/_full/_now/; + my $now = read_file( $sysfs_path ); + $bat = sprintf("%2d%% | ", $now * 100 / $full ); - if ( $state->{'present rate'} != 0 ) { - my $info = proc2hash( "$proc_acpi_battery/info" ); + } elsif ( $proc_acpi_battery ) { - my $pcnt = $state->{'remaining capacity'} / $info->{'design capacity'}; + my $state = proc2hash( "$proc_acpi_battery/state" ); - my $time = $state->{'remaining capacity'} / ( $state->{'present rate'} ); - $time = ( $info->{'design capacity'} - $state->{'remaining capacity'} ) / $state->{'present rate'} if ( $state->{'charging state'} eq 'charging' ); + if ( $state->{'present rate'} =~ m/^\d+$/ && $state->{'present rate'} != 0 ) { + my $info = proc2hash( "$proc_acpi_battery/info" ); - warn "time = $time\n" if ($debug); + my $pcnt = $state->{'remaining capacity'} / $info->{'design capacity'}; - my $hh = int( $time ); - my $mm = int( ( $time - $hh ) * 60 ); - my $ss = ( $time * 3600 ) % 60; + my $time = $state->{'remaining capacity'} / ( $state->{'present rate'} ); + $time = ( $info->{'design capacity'} - $state->{'remaining capacity'} ) / $state->{'present rate'} if ( $state->{'charging state'} eq 'charging' ); - $bat = sprintf("%s %2d%% %02d:%02d:%02d %3.1fW!| ", - substr($state->{'charging state'},0,1), - $pcnt * 100, $hh, $mm, $ss, - $state->{'present rate'} / 1000 - ); + warn "time = $time\n" if ($debug); - } else { - $bat = ''; + 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 =~ s/!(\|\s)$/ $1/; } @@ -177,7 +186,7 @@ my $d_write = ( $d_w - $ld_w ) / $dt; ( $ld_r, $ld_w ) = ( $d_r, $d_w ); - printf("%s | %s |%5s D %-5s|%5s > %-5s| %s%s\n", + my $out = sprintf("%s | %s |%5s D %-5s|%5s > %-5s| %s%s", $s, $load, unit( $d_read ), unit( $d_write ), @@ -185,6 +194,16 @@ $bat, $temp, ); + print "$out\n"; + if ( $awesome ) { + open(my $fh, '|-', 'awesome-client') || die "can't pipe to awesome-client: $!"; + print $fh + $awesome == 3 ? qq{mytextbox.text="$out"\n} + : "0 widget_tell mystatusbar dwm-status text $out\n" + ; + close($fh); + } + sleep $dt; }