--- trunk/dwm-status.pl 2007/05/27 09:14:14 35 +++ trunk/dwm-status.pl 2007/10/02 16:32:17 56 @@ -8,7 +8,7 @@ use POSIX qw/strftime/; use File::Slurp; use Time::HiRes; -use Data::Dump qw/dump/; +eval "use Data::Dump qw/dump/;"; my $dt = 3; my $acpi_every = 10; @@ -21,6 +21,7 @@ sub proc2hash { my $f = shift; + return unless -f $f; open(my $fh, '<', $f) || die "can't open $f: $!"; my $h; while(<$fh>) { @@ -29,10 +30,42 @@ $value =~ s/ m[VW]h*$//; $h->{$key} = $value; } - warn dump( $h ) if ( $debug ); + warn "$f ",dump( $h ) if ( $debug ); return $h; } +my $proc_acpi_battery; +sub find_proc_acpi { + my ( $path, $check ) = @_; + + if ( -e "/proc/acpi/$path" ) { + opendir(my $items, "/proc/acpi/$path") || die "can't open /proc/acpi/$path: $!"; + foreach my $item ( readdir( $items ) ) { + $check->( $item ) && last; + } + } +} + +my $proc_acpi_thermal_zone_temperature; +find_proc_acpi('thermal_zone', sub { + my $thm = shift; + return if ( ! -e "/proc/acpi/thermal_zone/$thm/temperature" ); + warn "# using thermal zone $thm\n" if ( $debug ); + $proc_acpi_thermal_zone_temperature = "/proc/acpi/thermal_zone/$thm/temperature"; +}); + +find_proc_acpi('battery', sub { + my $bat = shift; + return unless $bat =~ /\w+/; + + warn "# testing battery $bat\n" if ( $debug ); + + if ( proc2hash( "/proc/acpi/battery/$bat/info" )->{present} eq 'yes' ) { + $proc_acpi_battery = "/proc/acpi/battery/$bat"; + warn "using $proc_acpi_battery to monitor battery\n"; + } +}); + sub unit { my $v = shift; @@ -41,7 +74,7 @@ my @units = qw/b k M G/; my $o = 0; - while ( ( $v / 1024 ) >= 1 ) { + while ( ( $v / 10000 ) >= 1 ) { $o++; $v /= 1024; } @@ -64,16 +97,17 @@ while ( 1 ) { my $s = strftime("%Y-%m-%d %H:%M:%S", localtime()); - if ( $i % $acpi_every == 0 ) { + if ( $i % $acpi_every == 0 && $proc_acpi_battery ) { - my $state = proc2hash( '/proc/acpi/battery/BAT0/state' ); + my $state = proc2hash( "$proc_acpi_battery/state" ); if ( $state->{'present rate'} != 0 ) { - my $info = proc2hash( '/proc/acpi/battery/BAT0/info' ); + my $info = proc2hash( "$proc_acpi_battery/info" ); 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' ); warn "time = $time\n" if ($debug); @@ -99,21 +133,25 @@ chomp( $load ); $load =~ s!\s\d+/\d+.*!!; - my $temp = read_file('/proc/acpi/thermal_zone/THM0/temperature'); - chomp( $temp ); - $temp =~ s!^.*:\s+!!; + my $temp = ''; + if ( $proc_acpi_thermal_zone_temperature ) { + $temp = read_file( $proc_acpi_thermal_zone_temperature ); + chomp( $temp ); + $temp =~ s!^.*:\s+!!; + } my $net = read_file('/proc/net/dev'); my ( $rx, $tx ) = ( 0,0 ); foreach ( split(/\n/, $net) ) { s/^\s+//; + s/:\s+/:/; my @n = split(/\s+/, $_, 17); - next unless ( $n[0] =~ m!(eth\d|ath\d):! ); + next unless ( $n[0] =~ s!(eth\d|ath\d):!! ); warn dump( @n ) if ($debug); - $rx += $n[1]; - $tx += $n[9]; + $rx += $n[0]; + $tx += $n[8]; } warn "rx: $rx tx: $tx\n" if ($debug);