#!/usr/bin/wish wm title . "ACPI" set w 30 set h 115 set lv 1.0 set ld [clock seconds] set oac DC set started 0 set new 1 set batmax(0) 0 set batmax(1) 0 proc bat_init {} { global batmax for { set v 0} { $v < 2} { incr v } { set fh [open "/proc/acpi/battery/BAT$v/info"] while { [ gets $fh line ] >= 0 } { if { [ regexp {^design capacity:[ \t]*([0-9]+) mWh$} $line all max ] } { set batmax($v) $max break } } close $fh } } proc m_update { } { global .t.c w h ld lv oac started new batmax set ac AC set rate 0 set remaining 0 for { set v 0} { $v < 2} { incr v } { if { $batmax($v) == 0 } { continue } set fh [open "/proc/acpi/battery/BAT$v/state"] while { [ gets $fh line ] >= 0 } { if { [ regexp {^charging state:[ \t]*discharging$} $line ] } { set ac DC } if { [ regexp {^present rate:[ \t]*([0-9]+) mW$} $line all r ] } { set rate [ expr $rate + $r ] } if { [ regexp {^remaining capacity:[ \t]*([0-9]+) mWh$} $line all r ] } { set remaining [ expr $remaining + $r ] } } close $fh } set max [ expr $batmax(0) + $batmax(1) ] set s [ expr "$remaining.0" / $max ] if { $rate > 0 } { if {$ac == "DC"} { set min [ expr "int($remaining.0 / $rate * 60)" ] } else { set min [ expr "int(($max.0 - $remaining.0) / $rate * 60)" ] } } else { set min "???" } set unit "min" #echo Rem: $remaining Rate $rate Max $max Min $min if { $oac != $ac && $ac == "DC" } { tk_messageBox -icon info -title "Warning"\ -message "Warning: AC lost, please check" -type ok } set oac $ac if { $lv != $s } { set now [clock seconds] set diff [ expr $now - $ld ] if { $lv < $s } { set f [ expr $diff * (1.0-$s) / ($s-$lv) / 60 ] .t.c itemconfigure full -text [ format "%.0f" $f ] .t.c itemconfigure hour -text "min" set lv $s set ld $now } elseif { $lv && $lv > $s && $started == 1 && $diff < 1800 } { set f [ expr 60.0/((3600/$diff)*($lv-$s)) ] set f2 [ expr $f * $s ] .t.c itemconfigure full -text [ format "%.0f" $f ] .t.c itemconfigure hour -text [ format "(%.0f)" $f2 ] } else { .t.c itemconfigure full -text "" .t.c itemconfigure hour -text "" } set started 1 set lv $s set ld $now set new 1 } if {$new } { .t.c itemconfigure min -text $min .t.c itemconfigure unit -text $unit .t.c itemconfigure ac -text $ac .t.c delete battery .t.c create rectangle 2 [expr 2 + ((1.0-$s)*($h-0))]\ [expr $w+2] [expr $h+2] -fill #2b2 -outline "" -tag battery .t.c lower battery set new 0 } after 30000 m_update } frame .t -borderwidth 0 canvas .t.c -width $w -height $h -background #eee -borderwidth 0 set rw [expr $w / 2 +2] .t.c create text $rw 15 -tag ac .t.c create text $rw 27 -tag full .t.c create text $rw 39 -tag hour .t.c create text $rw [expr $h - 8] -tag unit .t.c create text $rw [expr $h - 20] -tag min pack .t.c .t bat_init m_update