#!/usr/bin wish if {[file exists /proc/apm]} { namespace eval apm { variable colors variable help set c [canvas .apm -width 62 -height 15 -relief sunken -bd 1] pack $c -side right $c create rectangle 10 3 30 13 -outline black $c create rectangle 30 6 33 10 -outline black -fill black $c create rectangle 10 3 30 13 -outline black -stipple @[file join $tk_library demos images gray25.bmp] -fill black -tag energy $c create polygon 2 1 6 8 3 8 8 15 6 9 9 9 5 1 -outline red -fill red -tag power $c create polygon 35 8 38 5 38 11 -fill black -outline black -tag charge $c create polygon 35 5 35 11 38 8 -fill black -outline black -tag discharge $c create text 61 9 -anchor e -text 100% -font 6x10 -tag pwtext bind .apm ::apm::start_help bind .apm ::apm::cancel_help bind .apm ::apm::reset_help array set colors { charge black discharge black power red } proc toggle_item {tag state} { variable colors if {$state} { .apm itemconfig $tag -fill $colors($tag) -outline $colors($tag) } else { .apm itemconfig $tag -fill {} -outline {} } } proc set_power {percent} { set x [expr 20*$percent/100+10] .apm coords energy 10 3 $x 13 .apm itemconfig pwtext -text "$percent%" } proc update {} { set f [open /proc/apm] set status [gets $f] close $f variable help set battery [expr [lindex $status 5]] set help "" if {[lindex $status 3] == 1} { toggle_item power 1 toggle_item discharge 0 append help AC if {$battery!=255 && ($battery&8)} { toggle_item charge 1 append help " charging [calc_time $status] to complete" } else { toggle_item charge 0 append help " not charging" } .apm configure -background [lindex [.apm configure -background] 3] } else { toggle_item power 0 toggle_item discharge 1 toggle_item charge 0 if {$battery != 255} { if {$battery & 1} { .apm configure -background yellow } elseif {$battery & 2} { .apm configure -background #ff7777 } else { .apm configure -background [lindex [.apm configure -background] 3] } } append help "Battery [calc_time $status] remains" } set_power [scan [lindex $status 6] "%d%%"] after 5000 ::apm::update } proc calc_time {apm_status} { set units [lindex $apm_status 8] set count [lindex $apm_status 7] if {![regexp {[0-9]+} $count]} { return ?? } if {$units == "sec"} { set sec [expr $count % 60] set min1 [expr $count / 60] set min [expr $min1 % 60] set hours [expr $min1 /60] return [format "%d:%02d:%02d" $hours $min $sec] } else { set min [expr $count % 60] set hours [expr $count / 60] return [format "%d:%02d" $hours $min] } } proc cancel_help {} { variable help_after_id if [info exists help_after_id] { after cancel $help_after_id unset help_after_id } if {[wm state .apm.help]=="normal"} { wm withdraw .apm.help } } proc start_help {} { variable help_after_id set help_after_id [after 1000 ::apm::show_help] } proc reset_help {} { cancel_help start_help } proc show_help {} { wm geometry .apm.help +[expr [winfo pointerx .]+2]+[expr [winfo pointery .]+2] wm deiconify .apm.help raise .apm.help } toplevel .apm.help wm withdraw .apm.help wm overrideredirect .apm.help y label .apm.help.l -textvar ::apm::help -background yellow -font 6x10 pack .apm.help.l -side left unset c } } ::apm::update