File: xbatmon-simple-tray

package info (click to toggle)
chiark-tcl-applet 1.0-2
  • links: PTS
  • area: main
  • in suites: bullseye, sid
  • size: 144 kB
  • sloc: tcl: 860; makefile: 32
file content (232 lines) | stat: -rwxr-xr-x 5,651 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#!/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...]]]

set lib .

source $lib/subproc.tcl
source $lib/utils.tcl
source $lib/args.tcl
source $lib/applet.tcl

proc cmdline {id orientation} {
    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 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 {}
    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 {$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 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"
    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 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"
	    set a($k) $v
	} else {
	    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) {
		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" }
	    }
	    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 { applet::become }
    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
set usersettings $env(HOME)/.config/xbatmon-simple-tray

while {[args::next_special arg]} {
    switch -exact -- $arg {
	-- { break }
	-tooltip-string { set mode $arg }
	default { args::badoption }
    }
}

if {[file exists $usersettings]} {
    source $usersettings
}

mode/$mode