X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ian/git?p=chiark-tcl-applet.git;a=blobdiff_plain;f=xbatmon-simple-tray;h=2a3948c9131e19284890a7ab90c00d00d5fb7285;hp=e53945a4ab6e3c0010a07d59f964e5d06eda2cf9;hb=a28043c357ce68cd1d1dd17abc81e39cc7f7b84a;hpb=2ad3f13ef50c105ebedbaff56992df7b23c0136c diff --git a/xbatmon-simple-tray b/xbatmon-simple-tray index e53945a..2a3948c 100755 --- a/xbatmon-simple-tray +++ b/xbatmon-simple-tray @@ -5,8 +5,8 @@ # xbatmon-simple-tray # [WISH-OPTIONS... [-- TRAY-EMBED-OPTIONS... [-- XBATMON-SIMPLE-OPTIONS...]]] -source applet.tcl source subproc.tcl +source utils.tcl source args.tcl proc cmdline {id orientation} { @@ -14,11 +14,125 @@ proc cmdline {id orientation} { return [concat [list xacpi-simple -into $id] $argv] } +#----- tooltip generation ----- + +proc tt-invisible {} { + tt-noafter + applet::tooltip-set {} +} + +proc tt-noafter {} { + global ttafter + catch { after cancel $ttafter } + catch { unset ttafter } +} + +proc tt-show {} { + global ttafter + tt-noafter + set ttafter [after 500 tt-show] + applet::tooltip-set [tt-string] +} + +proc tt-string {} { + global errorInfo + set lines {} + if {[catch { + set dir /sys/class/power_supply + foreach f [glob -nocomplain -tails -directory $dir *] { + debug::debug "TT-INFO $f" + if {[catch { + set chan [open $dir/$f/uevent] + tt-info $chan + } info]} { + set info "error: $info" + debug::debug "$f $errorInfo" + } + lappend lines "$f: $info" + catch { close $chan } + catch { unset chan } + } + } emsg]} { + lappend lines "error scanning: $emsg" + debug::debug "scanning $errorInfo" + } + if {![llength $lines]} { + lappend lines "no power information" + } + return [join $lines "\n"] +} + +proc compute {power energy factor} { + upvar 1 a a + upvar 1 q q + debug::debug "COMPUTE $power $energy $factor" + foreach ent {energy NOW} {energy FULL} {energy FULL_DESIGN} {power NOW} { + manyset $ent pe k + set kv "[set $pe]_$k" + if {![info exists a($kv)]} { return 0 } + set q("${pe}_${k}") [expr {$a($kv) * $factor}] + } + return 1 +} + +proc tt-info {chan} { + while {[gets $chan l] >= 0} { + if {[regexp {^POWER_SUPPLY_([A-Z0-9_]+)=(.*)$} $l dummy k v]} { + debug::debug " uevent ok $l" + set a($k) $v + } else { + debug::debug " uevent unk $l" + } + } + set o "$a(TYPE)" + switch -exact -- $a(TYPE) { + Mains { + switch -exact -- $a(ONLINE) { + 0 { append o " Offline" } + 1 { append o " Online" } + default { append o " ?$o" } + } + } + Battery { + switch -exact -- $a(PRESENT) { + 0 { append o " Absent"; return $o } + 1 { append o " Present" } + default { apend o " ?$o" } + } + append o " $a(STATUS)" + if {[compute POWER ENERGY 1.0] || + [compute CURRENT CHARGE [expr {$a(VOLTAGE_NOW) * 1e-6}]]} { + debug::debug "COMPUTE OK" + } else { + append o " ?" + } + } + } +} + +#----- modes ----- + +proc mode/normal {} { + uplevel #0 source applet.tcl + applet::setup-subproc cmdline + applet::setup-tooltip tt-show tt-invisible +} + +proc mode/-tooltip-string {} { + puts [tt-string] + exit 0 +} + +#----- command line parsing ----- + +set mode normal + while {[args::next_special arg]} { switch -exact -- $arg { -- { break } + -tooltip-string { set mode $arg } default { args::badoption } } } -applet::setup-subproc cmdline +mode/$mode