8 |
use POSIX qw/strftime/; |
use POSIX qw/strftime/; |
9 |
use File::Slurp; |
use File::Slurp; |
10 |
use Time::HiRes; |
use Time::HiRes; |
11 |
eval { |
use Data::Dump qw/dump/; |
|
use Data::Dump qw/dump/; |
|
|
}; |
|
12 |
|
|
13 |
my $dt = 3; |
my $dt = 3; |
14 |
my $acpi_every = 10; |
my $acpi_every = 10; |
16 |
my $disk_blk_size = 512; |
my $disk_blk_size = 512; |
17 |
|
|
18 |
my $debug = shift @ARGV; |
my $debug = shift @ARGV; |
19 |
|
my $awesome = 1; |
20 |
|
|
21 |
$|=1; |
$|=1; |
22 |
|
|
57 |
|
|
58 |
find_proc_acpi('battery', sub { |
find_proc_acpi('battery', sub { |
59 |
my $bat = shift; |
my $bat = shift; |
60 |
return unless $bat =~ /BAT/; |
return unless $bat =~ /\w+/; |
61 |
|
|
62 |
warn "# testing battery $bat\n" if ( $debug ); |
warn "# testing battery $bat\n" if ( $debug ); |
63 |
|
|
91 |
|
|
92 |
my ( $lrx, $ltx ) = ( 0, 0 ); |
my ( $lrx, $ltx ) = ( 0, 0 ); |
93 |
my ( $ld_r, $ld_w ) = ( 0, 0 ); |
my ( $ld_r, $ld_w ) = ( 0, 0 ); |
94 |
my $bat; |
my $bat = ''; |
95 |
|
|
96 |
my $i = 0; |
my $i = 0; |
97 |
|
my $sys_fs = '/sys/class/power_supply/BAT0'; |
98 |
|
|
99 |
while ( 1 ) { |
while ( 1 ) { |
100 |
my $s = strftime("%Y-%m-%d %H:%M:%S", localtime()); |
my $s = strftime("%Y-%m-%d %H:%M:%S", localtime()); |
101 |
|
|
102 |
if ( $i % $acpi_every == 0 && $proc_acpi_battery ) { |
if ( $i % $acpi_every == 0 ) { |
103 |
|
if ( $proc_acpi_battery ) { |
104 |
|
|
105 |
my $state = proc2hash( "$proc_acpi_battery/state" ); |
my $state = proc2hash( "$proc_acpi_battery/state" ); |
106 |
|
|
107 |
if ( $state->{'present rate'} != 0 ) { |
if ( $state->{'present rate'} =~ m/^\d+$/ && $state->{'present rate'} != 0 ) { |
108 |
my $info = proc2hash( "$proc_acpi_battery/info" ); |
my $info = proc2hash( "$proc_acpi_battery/info" ); |
109 |
|
|
110 |
my $pcnt = $state->{'remaining capacity'} / $info->{'design capacity'}; |
my $pcnt = $state->{'remaining capacity'} / $info->{'design capacity'}; |
111 |
|
|
112 |
my $time = $state->{'remaining capacity'} / ( $state->{'present rate'} ); |
my $time = $state->{'remaining capacity'} / ( $state->{'present rate'} ); |
113 |
$time = ( $info->{'design capacity'} - $state->{'remaining capacity'} ) / $state->{'present rate'} if ( $state->{'charging state'} eq 'charging' ); |
$time = ( $info->{'design capacity'} - $state->{'remaining capacity'} ) / $state->{'present rate'} if ( $state->{'charging state'} eq 'charging' ); |
114 |
|
|
115 |
warn "time = $time\n" if ($debug); |
warn "time = $time\n" if ($debug); |
116 |
|
|
117 |
my $hh = int( $time ); |
my $hh = int( $time ); |
118 |
my $mm = int( ( $time - $hh ) * 60 ); |
my $mm = int( ( $time - $hh ) * 60 ); |
119 |
my $ss = ( $time * 3600 ) % 60; |
my $ss = ( $time * 3600 ) % 60; |
120 |
|
|
121 |
$bat = sprintf("%s %2d%% %02d:%02d:%02d %3.1fW!| ", |
$bat = sprintf("%s %2d%% %02d:%02d:%02d %3.1fW!| ", |
122 |
substr($state->{'charging state'},0,1), |
substr($state->{'charging state'},0,1), |
123 |
$pcnt * 100, $hh, $mm, $ss, |
$pcnt * 100, $hh, $mm, $ss, |
124 |
$state->{'present rate'} / 1000 |
$state->{'present rate'} / 1000 |
125 |
); |
); |
|
|
|
|
} else { |
|
|
$bat = ''; |
|
126 |
} |
} |
127 |
|
} elsif ( -e $sys_fs ) { |
128 |
|
my $full = read_file( "$sys_fs/charge_full" ); |
129 |
|
my $now = read_file( "$sys_fs/charge_now" ); |
130 |
|
$bat = sprintf("%2d%% | ", $now * 100 / $full ); |
131 |
|
} |
132 |
} else { |
} else { |
133 |
$bat =~ s/!(\|\s)$/ $1/; |
$bat =~ s/!(\|\s)$/ $1/; |
134 |
} |
} |
182 |
my $d_write = ( $d_w - $ld_w ) / $dt; |
my $d_write = ( $d_w - $ld_w ) / $dt; |
183 |
( $ld_r, $ld_w ) = ( $d_r, $d_w ); |
( $ld_r, $ld_w ) = ( $d_r, $d_w ); |
184 |
|
|
185 |
printf("%s | %s |%5s D %-5s|%5s > %-5s| %s%s\n", |
my $out = sprintf("%s | %s |%5s D %-5s|%5s > %-5s| %s%s\n", |
186 |
$s, |
$s, |
187 |
$load, |
$load, |
188 |
unit( $d_read ), unit( $d_write ), |
unit( $d_read ), unit( $d_write ), |
190 |
$bat, $temp, |
$bat, $temp, |
191 |
); |
); |
192 |
|
|
193 |
|
print $out; |
194 |
|
if ( $awesome ) { |
195 |
|
open(my $fh, '|-', 'awesome-client') || die "can't pipe to awesome-client: $!"; |
196 |
|
print $fh "0 widget_tell mystatusbar dwm-status text $out\n"; |
197 |
|
close($fh); |
198 |
|
} |
199 |
|
|
200 |
sleep $dt; |
sleep $dt; |
201 |
} |
} |
202 |
|
|