chiark / gitweb /
reorganise debug etc.
[chiark-tcl-applet.git] / xbatmon-simple-tray
index 77b9fb1a98e3299081e607f5bca79ff7593301d6..2a3948c9131e19284890a7ab90c00d00d5fb7285 100755 (executable)
-#!/usr/bin/wish
+#!/usr/bin/wish -f
 # -*- Tcl -*-
 
-source applet.tcl
+# usage:
+#  xbatmon-simple-tray
+#    [WISH-OPTIONS... [-- TRAY-EMBED-OPTIONS... [-- XBATMON-SIMPLE-OPTIONS...]]]
+
 source subproc.tcl
+source utils.tcl
+source args.tcl
 
 proc cmdline {id orientation} {
-    return [list xacpi-simple -into $id]
+    global argv
+    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 50 50 horizontal darkblue 1 cmdline
+mode/$mode