chiark / gitweb /
changelog: start 1.0-3
[chiark-tcl-applet.git] / xbatmon-simple-tray
index 2a3948c9131e19284890a7ab90c00d00d5fb7285..1c9c8644115b99238822cf5775eef718b4135fac 100755 (executable)
@@ -1,13 +1,20 @@
 #!/usr/bin/wish -f
 # -*- Tcl -*-
 
+# Copyright 2016,2020 Ian Jackson
+# SPDX-License-Identifier: GPL-3.0-or-later
+# There is NO WARRANTY.
+
 # usage:
 #  xbatmon-simple-tray
 #    [WISH-OPTIONS... [-- TRAY-EMBED-OPTIONS... [-- XBATMON-SIMPLE-OPTIONS...]]]
 
-source subproc.tcl
-source utils.tcl
-source args.tcl
+set lib .
+
+source $lib/subproc.tcl
+source $lib/utils.tcl
+source $lib/args.tcl
+source $lib/applet.tcl
 
 proc cmdline {id orientation} {
     global argv
@@ -34,6 +41,36 @@ proc tt-show {} {
     applet::tooltip-set [tt-string]
 }
 
+proc append-energies {av energy power eunit punit fmt} {
+    upvar 1 o o
+    upvar 1 $av a
+    append o [format "  ${fmt}%s / ${fmt}%s / ${fmt}%s  ${fmt}%s" \
+                 [expr { $a(${energy}_NOW)         * 1e-6 }]  $eunit  \
+                 [expr { $a(${energy}_FULL)        * 1e-6 }]  $eunit  \
+                 [expr { $a(${energy}_FULL_DESIGN) * 1e-6 }]  $eunit  \
+                 [expr { $a(${power}_NOW)          * 1e-6 }]  $punit]
+}
+
+proc append-percents {qv} {
+    upvar 1 o o
+    upvar 1 $qv q
+    append o [format "  %.0f%% (%.0f%%/%.0f%%)" \
+                  [expr { $q(energy_NOW)  * 100.0 / $q(energy_FULL_DESIGN) }] \
+                  [expr { $q(energy_NOW)  * 100.0 / $q(energy_FULL)        }] \
+                  [expr { $q(energy_FULL) * 100.0 / $q(energy_FULL_DESIGN) }]]
+    if {$q(power_NOW) < -1} {
+       set endpoint 0
+    } elseif {$q(power_NOW) > -1} {
+       set endpoint $q(energy_FULL)
+    }
+    if {[info exists endpoint]} {
+       set until [expr {($endpoint - $q(energy_NOW)) * 60.0/ $q(power_NOW)}]
+       if {$until < 72*60} {
+           append o [format "  %.fmins" $until]
+       }
+    }
+}
+
 proc tt-string {} {
     global errorInfo
     set lines {}
@@ -56,26 +93,52 @@ proc tt-string {} {
        lappend lines "error scanning: $emsg"
        debug::debug "scanning $errorInfo"
     }
+    if {$nbatts} {
+       set o "Total: $nbatts present  "
+       append o [join [lsort [array names states]] /]
+       append-energies tot energy power Wh W %.1f
+       append-percents tot
+       lappend lines $o
+    }
     if {![llength $lines]} {
        lappend lines "no power information"
     }
     return [join $lines "\n"]
 }
 
-proc compute {power energy factor} {
+proc compute {power energy factor punit eunit sign fmt} {
     upvar 1 a a
     upvar 1 q q
+    upvar 1 tot tot
+    upvar 1 o o
     debug::debug "COMPUTE $power $energy $factor"
-    foreach ent {energy NOW} {energy FULL} {energy FULL_DESIGN} {power NOW} {
+    set energy_sign 1
+    set power_sign $sign
+    set entl {{energy NOW} {energy FULL} {energy FULL_DESIGN} {power NOW}}
+    foreach ent $entl {
        manyset $ent pe k
+       set kq "${pe}_${k}"
        set kv "[set $pe]_$k"
        if {![info exists a($kv)]} { return 0 }
-       set q("${pe}_${k}") [expr {$a($kv) * $factor}]
+       set a($kv) [expr {$a($kv) * [set "${pe}_sign"]}]
+       set q($kq) [expr {$a($kv) * $factor}]
+    }
+    debug::debug "COMPUTE OK [array names q]"
+    foreach ent $entl {
+       manyset $ent pe k
+       set kq "${pe}_${k}"
+       if {![info exists tot($kq)]} { set tot($kq) 0 }
+       set tot($kq) [expr {$tot($kq) + $q($kq)}]
     }
+    append-energies a $energy $power $eunit $punit $fmt
+    append-percents q
     return 1
 }
 
 proc tt-info {chan} {
+    upvar 1 tot tot
+    upvar 1 nbatts nbatts
+    upvar 1 states states
     while {[gets $chan l] >= 0} {
        if {[regexp {^POWER_SUPPLY_([A-Z0-9_]+)=(.*)$} $l dummy k v]} {
            debug::debug "  uevent ok  $l"
@@ -84,7 +147,20 @@ proc tt-info {chan} {
            debug::debug "  uevent unk $l"
        }
     }
+    debug::debug "GOT [array names a]"
+    if {![info exists a(TYPE)]} {
+       set op {}
+       foreach k {ONLINE PRESENT} { append op [info exists a($k)] }
+       switch -exact $op {
+           10 { set a(TYPE) Mains }
+           01 { set a(TYPE) Battery }
+           default { error "no type and bad op $op" }
+       }
+       debug::debug "  guessed type $a(TYPE) from ONLINE PRESENT $op"
+    }
     set o "$a(TYPE)"
+
+    set nbatts 0
     switch -exact -- $a(TYPE) {
        Mains {
            switch -exact -- $a(ONLINE) {
@@ -99,21 +175,34 @@ proc tt-info {chan} {
                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 " ?"
+           set sign 1
+           append o " "
+           switch -exact -- $a(STATUS) {
+               Charging { }
+               Discharging { set sign -1 }
+               default { append o "Status=" }
+           }
+           set states($a(STATUS)) 1
+           append o $a(STATUS)
+           foreach power {POWER CURRENT} energy {ENERGY CHARGE} \
+                   punit {W A} eunit {Wh Ah} fmt {%.1f %.2f} {
+               switch -exact $power {
+                   POWER { set factor 1.0 }
+                   CURRENT { set factor [expr {$a(VOLTAGE_NOW) * 1e-6}] }
+               }
+               if {![compute $power $energy $factor \
+                              $punit $eunit $sign $fmt]} continue
+               incr nbatts
            }
        }
     }
+    return $o
 }
 
 #----- modes -----
 
 proc mode/normal {} {
-    uplevel #0 source applet.tcl
+    uplevel #0 { applet::become }
     applet::setup-subproc cmdline
     applet::setup-tooltip tt-show tt-invisible
 }
@@ -126,6 +215,7 @@ proc mode/-tooltip-string {} {
 #----- command line parsing -----
 
 set mode normal
+set usersettings $env(HOME)/.config/xbatmon-simple-tray
 
 while {[args::next_special arg]} {
     switch -exact -- $arg {
@@ -135,4 +225,8 @@ while {[args::next_special arg]} {
     }
 }
 
+if {[file exists $usersettings]} {
+    source $usersettings
+}
+
 mode/$mode