4 # Copyright 2016,2020 Ian Jackson
5 # SPDX-License-Identifier: GPL-3.0-or-later
6 # There is NO WARRANTY.
10 # [WISH-OPTIONS... [-- TRAY-EMBED-OPTIONS... [-- XBATMON-SIMPLE-OPTIONS...]]]
16 proc cmdline {id orientation} {
18 return [concat [list xacpi-simple -into $id] $argv]
21 #----- tooltip generation -----
23 proc tt-invisible {} {
25 applet::tooltip-set {}
30 catch { after cancel $ttafter }
31 catch { unset ttafter }
37 set ttafter [after 500 tt-show]
38 applet::tooltip-set [tt-string]
41 proc append-energies {av energy power eunit punit fmt} {
44 append o [format " ${fmt}%s / ${fmt}%s / ${fmt}%s ${fmt}%s" \
45 [expr { $a(${energy}_NOW) * 1e-6 }] $eunit \
46 [expr { $a(${energy}_FULL) * 1e-6 }] $eunit \
47 [expr { $a(${energy}_FULL_DESIGN) * 1e-6 }] $eunit \
48 [expr { $a(${power}_NOW) * 1e-6 }] $punit]
51 proc append-percents {qv} {
54 append o [format " %.0f%% (%.0f%%/%.0f%%)" \
55 [expr { $q(energy_NOW) * 100.0 / $q(energy_FULL_DESIGN) }] \
56 [expr { $q(energy_NOW) * 100.0 / $q(energy_FULL) }] \
57 [expr { $q(energy_FULL) * 100.0 / $q(energy_FULL_DESIGN) }]]
58 if {$q(power_NOW) < -1} {
60 } elseif {$q(power_NOW) > -1} {
61 set endpoint $q(energy_FULL)
63 if {[info exists endpoint]} {
64 set until [expr {($endpoint - $q(energy_NOW)) * 60.0/ $q(power_NOW)}]
66 append o [format " %.fmins" $until]
75 set dir /sys/class/power_supply
76 foreach f [glob -nocomplain -tails -directory $dir *] {
77 debug::debug "TT-INFO $f"
79 set chan [open $dir/$f/uevent]
82 set info "error: $info"
83 debug::debug "$f $errorInfo"
85 lappend lines "$f: $info"
90 lappend lines "error scanning: $emsg"
91 debug::debug "scanning $errorInfo"
94 set o "Total: $nbatts present "
95 append o [join [lsort [array names states]] /]
96 append-energies tot energy power Wh W %.1f
100 if {![llength $lines]} {
101 lappend lines "no power information"
103 return [join $lines "\n"]
106 proc compute {power energy factor punit eunit sign fmt} {
111 debug::debug "COMPUTE $power $energy $factor"
114 set entl {{energy NOW} {energy FULL} {energy FULL_DESIGN} {power NOW}}
118 set kv "[set $pe]_$k"
119 if {![info exists a($kv)]} { return 0 }
120 set a($kv) [expr {$a($kv) * [set "${pe}_sign"]}]
121 set q($kq) [expr {$a($kv) * $factor}]
123 debug::debug "COMPUTE OK [array names q]"
127 if {![info exists tot($kq)]} { set tot($kq) 0 }
128 set tot($kq) [expr {$tot($kq) + $q($kq)}]
130 append-energies a $energy $power $eunit $punit $fmt
135 proc tt-info {chan} {
137 upvar 1 nbatts nbatts
138 upvar 1 states states
139 while {[gets $chan l] >= 0} {
140 if {[regexp {^POWER_SUPPLY_([A-Z0-9_]+)=(.*)$} $l dummy k v]} {
141 debug::debug " uevent ok $l"
144 debug::debug " uevent unk $l"
147 debug::debug "GOT [array names a]"
148 if {![info exists a(TYPE)]} {
150 foreach k {ONLINE PRESENT} { append op [info exists a($k)] }
152 10 { set a(TYPE) Mains }
153 01 { set a(TYPE) Battery }
154 default { error "no type and bad op $op" }
156 debug::debug " guessed type $a(TYPE) from ONLINE PRESENT $op"
161 switch -exact -- $a(TYPE) {
163 switch -exact -- $a(ONLINE) {
164 0 { append o " Offline" }
165 1 { append o " Online" }
166 default { append o " ?$o" }
170 switch -exact -- $a(PRESENT) {
171 0 { append o " Absent"; return $o }
172 1 { append o " Present" }
173 default { apend o " ?$o" }
177 switch -exact -- $a(STATUS) {
179 Discharging { set sign -1 }
180 default { append o "Status=" }
182 set states($a(STATUS)) 1
184 foreach power {POWER CURRENT} energy {ENERGY CHARGE} \
185 punit {W A} eunit {Wh Ah} fmt {%.1f %.2f} {
186 switch -exact $power {
187 POWER { set factor 1.0 }
188 CURRENT { set factor [expr {$a(VOLTAGE_NOW) * 1e-6}] }
190 if {![compute $power $energy $factor \
191 $punit $eunit $sign $fmt]} continue
201 proc mode/normal {} {
202 uplevel #0 source applet.tcl
203 applet::setup-subproc cmdline
204 applet::setup-tooltip tt-show tt-invisible
207 proc mode/-tooltip-string {} {
212 #----- command line parsing -----
216 while {[args::next_special arg]} {
217 switch -exact -- $arg {
219 -tooltip-string { set mode $arg }
220 default { args::badoption }