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; |
|
my $awesome = 0; |
|
20 |
|
|
21 |
$|=1; |
$|=1; |
22 |
|
|
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'; |
|
if ( $awesome ) { |
|
|
undef $awesome; |
|
|
open( $awesome, '|-', 'awesome-client' ) || die "can't open awesome-client: $!"; |
|
|
} |
|
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 |
|
my $sysfs_path = glob "$sys_fs/*_full"; |
104 |
|
if ( $sysfs_path ) { |
105 |
|
|
106 |
my $state = proc2hash( "$proc_acpi_battery/state" ); |
my $full = read_file( $sysfs_path ); |
107 |
|
$sysfs_path =~ s/_full/_now/; |
108 |
|
my $now = read_file( $sysfs_path ); |
109 |
|
$bat = sprintf("%2d%% | ", $now * 100 / $full ); |
110 |
|
|
111 |
if ( $state->{'present rate'} != 0 ) { |
} elsif ( $proc_acpi_battery ) { |
|
my $info = proc2hash( "$proc_acpi_battery/info" ); |
|
112 |
|
|
113 |
my $pcnt = $state->{'remaining capacity'} / $info->{'design capacity'}; |
my $state = proc2hash( "$proc_acpi_battery/state" ); |
114 |
|
|
115 |
my $time = $state->{'remaining capacity'} / ( $state->{'present rate'} ); |
if ( $state->{'present rate'} =~ m/^\d+$/ && $state->{'present rate'} != 0 ) { |
116 |
$time = ( $info->{'design capacity'} - $state->{'remaining capacity'} ) / $state->{'present rate'} if ( $state->{'charging state'} eq 'charging' ); |
my $info = proc2hash( "$proc_acpi_battery/info" ); |
117 |
|
|
118 |
warn "time = $time\n" if ($debug); |
my $pcnt = $state->{'remaining capacity'} / $info->{'design capacity'}; |
119 |
|
|
120 |
my $hh = int( $time ); |
my $time = $state->{'remaining capacity'} / ( $state->{'present rate'} ); |
121 |
my $mm = int( ( $time - $hh ) * 60 ); |
$time = ( $info->{'design capacity'} - $state->{'remaining capacity'} ) / $state->{'present rate'} if ( $state->{'charging state'} eq 'charging' ); |
|
my $ss = ( $time * 3600 ) % 60; |
|
122 |
|
|
123 |
$bat = sprintf("%s %2d%% %02d:%02d:%02d %3.1fW!| ", |
warn "time = $time\n" if ($debug); |
|
substr($state->{'charging state'},0,1), |
|
|
$pcnt * 100, $hh, $mm, $ss, |
|
|
$state->{'present rate'} / 1000 |
|
|
); |
|
124 |
|
|
125 |
} else { |
my $hh = int( $time ); |
126 |
$bat = ''; |
my $mm = int( ( $time - $hh ) * 60 ); |
127 |
|
my $ss = ( $time * 3600 ) % 60; |
128 |
|
|
129 |
|
$bat = sprintf("%s %2d%% %02d:%02d:%02d %3.1fW!| ", |
130 |
|
substr($state->{'charging state'},0,1), |
131 |
|
$pcnt * 100, $hh, $mm, $ss, |
132 |
|
$state->{'present rate'} / 1000 |
133 |
|
); |
134 |
} |
} |
135 |
|
} |
136 |
} else { |
} else { |
137 |
$bat =~ s/!(\|\s)$/ $1/; |
$bat =~ s/!(\|\s)$/ $1/; |
138 |
} |
} |
194 |
$bat, $temp, |
$bat, $temp, |
195 |
); |
); |
196 |
|
|
197 |
|
print $out; |
198 |
if ( $awesome ) { |
if ( $awesome ) { |
199 |
print $awesome "0 statusbar_set_text $out"; |
open(my $fh, '|-', 'awesome-client') || die "can't pipe to awesome-client: $!"; |
200 |
} else { |
print $fh "0 widget_tell mystatusbar dwm-status text $out\n"; |
201 |
print $out; |
close($fh); |
202 |
} |
} |
203 |
|
|
204 |
sleep $dt; |
sleep $dt; |