Shift X2 status from legacy to archived
[khatus.git] / x2 / src / awk / exe / bar.awk
CommitLineData
c4fd5e7d
SK
1# Naming convention:
2# Variables:
3# - global, builtin : ALLCAPS
b1c097d6 4# - global, public : Camel_Snake_Man_Bear_Pig
c4fd5e7d
SK
5# - global, private : _snake_case_prefixed_underscore
6# - local : snake_case
7# Functions:
8# - global, public : snake_case
9
75b23ff8
SK
10# -----------------------------------------------------------------------------
11# Input
12# -----------------------------------------------------------------------------
81336934
SK
13{
14 delete msg
15 msg_parse(msg, $0)
16}
17
18msg["type"] == "data" {
8482fea6 19 cache_update(msg["node"], msg["module"], msg["key"], msg["val"])
75b23ff8
SK
20}
21
81336934
SK
22msg["node"] == Node && \
23msg["module"] == "khatus_sensor_datetime" && \
24msg["type"] == "data" {
03c229bf 25 # Code for bar_make_status is expected to be passed as an
ab9fe663 26 # additional source file, using -f flag.
8482fea6 27 msg_out_status_bar(bar_make_status())
75b23ff8
SK
28}
29
75b23ff8 30# -----------------------------------------------------------------------------
e62110ca 31# Energy
75b23ff8
SK
32# -----------------------------------------------------------------------------
33
65034298
SK
34function bar_make_status_energy_percent() {
35 return cache_get_fmt_def("khatus_sensor_energy", "battery_percentage", 0, "%d")
e62110ca 36}
75b23ff8 37
e62110ca
SK
38function bar_make_status_energy_direction( state, direction_of_change) {
39 cache_get(state, "khatus_sensor_energy", "battery_state", 0)
269df6fa 40 if (state["value"] == "discharging") {
75b23ff8 41 direction_of_change = "<"
269df6fa 42 } else if (state["value"] == "charging") {
75b23ff8
SK
43 direction_of_change = ">"
44 } else {
45 direction_of_change = "="
46 }
e62110ca
SK
47 return direction_of_change
48}
75b23ff8 49
e62110ca
SK
50# -----------------------------------------------------------------------------
51# Memory
52# -----------------------------------------------------------------------------
53
e62110ca 54function bar_make_status_mem_percent( total, used, percent, percent_str) {
c4fd5e7d
SK
55 cache_get(total, "khatus_sensor_memory", "total", 5)
56 cache_get(used , "khatus_sensor_memory", "used" , 5)
269df6fa
SK
57 # Checking total["value"] to avoid division by zero when data is missing
58 if (!total["is_expired"] && \
59 !used["is_expired"] && \
60 total["value"] \
61 ) {
8482fea6 62 percent = num_round((used["value"] / total["value"]) * 100)
e62110ca 63 percent_str = sprintf("%d", percent)
75b23ff8 64 } else {
e62110ca 65 percent_str = "__"
75b23ff8 66 }
e62110ca 67 return percent_str
75b23ff8
SK
68}
69
e62110ca
SK
70# -----------------------------------------------------------------------------
71# Processes
72# -----------------------------------------------------------------------------
65034298
SK
73# From man ps:
74# D uninterruptible sleep (usually IO)
75# R running or runnable (on run queue)
76# S interruptible sleep (waiting for an event to complete)
77# T stopped by job control signal
78# t stopped by debugger during the tracing
79# W paging (not valid since the 2.6.xx kernel)
80# X dead (should never be seen)
81# Z defunct ("zombie") process, terminated but not reaped by its parent
82#
83# Additionally, not documented in ps man page:
84# I Idle
b9d5cfd4 85
e62110ca
SK
86function bar_make_status_procs_count_all() {
87 return cache_get_fmt_def("khatus_sensor_procs", "total_procs", 15, "%d")
88}
89
90function bar_make_status_procs_count_r( src) {
91 src = "khatus_sensor_procs"
92 return cache_get_fmt_def(src, "total_per_state" Kfs "R", 15, "%d", "0")
93}
94
95function bar_make_status_procs_count_d( src) {
96 src = "khatus_sensor_procs"
97 return cache_get_fmt_def(src, "total_per_state" Kfs "D", 15, "%d", "0")
98}
99
100function bar_make_status_procs_count_t( src) {
101 src = "khatus_sensor_procs"
102 return cache_get_fmt_def(src, "total_per_state" Kfs "T", 15, "%d", "0")
103}
104
105function bar_make_status_procs_count_i( src) {
106 src = "khatus_sensor_procs"
107 return cache_get_fmt_def(src, "total_per_state" Kfs "I", 15, "%d", "0")
108}
109
110function bar_make_status_procs_count_z( src) {
111 src = "khatus_sensor_procs"
112 return cache_get_fmt_def(src, "total_per_state" Kfs "Z", 15, "%d", "0")
113}
114
115# -----------------------------------------------------------------------------
116# CPU
117# -----------------------------------------------------------------------------
118
e62110ca
SK
119function bar_make_status_cpu_loadavg( src) {
120 src = "khatus_sensor_loadavg"
121 return cache_get_fmt_def(src, "load_avg_1min", 5, "%4.2f")
122}
123
124function bar_make_status_cpu_temperature() {
125 return cache_get_fmt_def("khatus_sensor_temperature", "temp_c", 5, "%d")
126}
127
128function bar_make_status_cpu_fan_speed() {
129 return cache_get_fmt_def("khatus_sensor_fan", "speed", 5, "%4d")
130}
131
132# -----------------------------------------------------------------------------
133# Disk
134# -----------------------------------------------------------------------------
135
e62110ca
SK
136function bar_make_status_disk_space( src) {
137 src = "khatus_sensor_disk_space"
138 return cache_get_fmt_def(src, "disk_usage_percentage", 10, "%s")
139}
140
141function bar_make_status_disk_io_w( src) {
142 src = "khatus_sensor_disk_io"
143 return cache_get_fmt_def(src, "sectors_written", 5, "%0.3f")
144}
145
146function bar_make_status_disk_io_r( src) {
147 src = "khatus_sensor_disk_io"
148 return cache_get_fmt_def(src, "sectors_read", 5, "%0.3f")
149}
150
151# -----------------------------------------------------------------------------
152# Network
153# -----------------------------------------------------------------------------
154
01da02dd
SK
155function bar_make_status_net_iface_status(interface, is_plugged_in) {
156 # TODO: Integrate connection/address status into the symbol somehow.
157 cache_get(is_plugged_in, "khatus_sensor_net_carrier", interface, 5)
64771080 158 if (!is_plugged_in["is_expired"] && is_plugged_in["value"] == 1)
01da02dd 159 return "<>"
887d3829
SK
160 else
161 return "--"
162}
163
e62110ca
SK
164function bar_make_status_net_addr(interface, src) {
165 src = "khatus_sensor_net_addr_io"
6facb1a2
SK
166 addr = cache_get_fmt_def(src, "addr" Kfs interface, 5, "%s")
167 return addr ? addr : "--"
e62110ca
SK
168}
169
170function bar_make_status_net_io_w(interface, src) {
171 src = "khatus_sensor_net_addr_io"
172 return cache_get_fmt_def(src, "bytes_written" Kfs interface, 5, "%0.3f")
173}
174
175function bar_make_status_net_io_r(interface, src) {
176 src = "khatus_sensor_net_addr_io"
177 return cache_get_fmt_def(src, "bytes_read" Kfs interface, 5, "%0.3f")
178}
179
180function bar_make_status_net_wifi(interface, src) {
181 src = "khatus_sensor_net_wifi_status"
182 return cache_get_fmt_def(src, "status" Kfs interface, 10, "%s")
183}
184
fcc91548 185function bar_make_status_net_wifi_link(interface, link) {
50401a4d 186 cache_get(link, "khatus_sensor_net_wifi_status", "link" Kfs interface, 10)
64771080 187 if (!link["is_expired"] && link["value"] > 0)
fcc91548
SK
188 return sprintf("%d%%", link["value"])
189 else
190 return "--"
daf7f29d
SK
191}
192
e62110ca
SK
193# -----------------------------------------------------------------------------
194# Bluetooth
195# -----------------------------------------------------------------------------
196
b9592ab6
SK
197function bar_make_status_bluetooth( src, controllers, devices) {
198 src = "khatus_sensor_bluetooth"
199 controllers = cache_get_fmt_def(src, "count_powered_controllers", 10, "%d")
200 devices = cache_get_fmt_def(src, "count_connected_devices" , 10, "%d")
201 # Using %s format bellow because default value is a string
202 return sprintf("%s:%s", controllers, devices)
203}
204
e62110ca 205function bar_make_status_bluetooth_power( src) {
269df6fa 206 src = "khatus_sensor_bluetooth_power"
e62110ca 207 return cache_get_fmt_def(src, "power_status", 10, "%s")
75b23ff8
SK
208}
209
e62110ca 210# -----------------------------------------------------------------------------
65034298 211# Backlight (screen brightness)
e62110ca
SK
212# -----------------------------------------------------------------------------
213
e62110ca 214function bar_make_status_backlight_percent( src) {
269df6fa 215 src = "khatus_sensor_screen_brightness"
e62110ca 216 return cache_get_fmt_def(src, "percentage", 5, "%d")
75b23ff8
SK
217}
218
e62110ca
SK
219# -----------------------------------------------------------------------------
220# Volume
221# -----------------------------------------------------------------------------
222
c7a86f0f
SK
223function bar_make_status_volume( m, l, r, show) {
224 cache_get(m, "khatus_sensor_volume", "mute" , 5)
225 cache_get(l, "khatus_sensor_volume", "vol_left" , 5)
226 cache_get(r, "khatus_sensor_volume", "vol_right" , 5)
767766df 227 show = "--"
07d37103 228 if (!m["is_expired"] && !l["is_expired"] && !r["is_expired"]) {
774d8cd1
SK
229 if (m["value"] == "yes")
230 show = "X"
231 else if (m["value"] == "no")
232 show = l["value"] #" " r["value"]
233 else
8482fea6 234 msg_out_log_error(\
c7a86f0f 235 "bar_make_status_volume", \
07d37103 236 "Unexpected value for 'mute' field: " m["value"] \
75b23ff8 237 )
75b23ff8 238 }
e62110ca 239 return show
75b23ff8
SK
240}
241
e62110ca
SK
242# -----------------------------------------------------------------------------
243# MPD
244# -----------------------------------------------------------------------------
245
65034298 246function bar_make_status_mpd( state, status) {
c4fd5e7d 247 cache_get(state, "khatus_sensor_mpd", "state", 5)
269df6fa
SK
248 if (!state["is_expired"] && state["value"]) {
249 if (state["value"] == "play") {
03c229bf 250 status = bar_make_status_mpd_state_known("▶")
269df6fa 251 } else if (state["value"] == "pause") {
e05c10ea 252 status = bar_make_status_mpd_state_known("⏸")
269df6fa 253 } else if (state["value"] == "stop") {
e05c10ea 254 status = bar_make_status_mpd_state_known("⏹")
75b23ff8 255 } else {
8482fea6 256 msg_out_log_error(\
03c229bf 257 "bar_make_status_mpd", \
269df6fa 258 "Unexpected value for 'state' field: " state["value"] \
75b23ff8
SK
259 )
260 status = "--"
261 }
262 } else {
263 status = "--"
264 }
e62110ca 265 return status
75b23ff8
SK
266}
267
03c229bf 268function bar_make_status_mpd_state_known(symbol, s, song, time, percentage) {
75b23ff8 269 s = "khatus_sensor_mpd"
ac294969 270 #song = cache_get_fmt_def(s, "song" , 5, "%s", "?")
c4fd5e7d
SK
271 time = cache_get_fmt_def(s, "play_time_minimal_units", 5, "%s", "?")
272 percent = cache_get_fmt_def(s, "play_time_percentage" , 5, "%s", "?")
ac294969
SK
273 #song = substr(song, 1, Opt_Mpd_Song_Max_Chars)
274 return sprintf("%s %s %s", symbol, time, percent)
75b23ff8
SK
275}
276
e62110ca
SK
277# -----------------------------------------------------------------------------
278# Weather
279# -----------------------------------------------------------------------------
280
e62110ca 281function bar_make_status_weather_temp_f( src, hour) {
fd710ac0 282 src = "khatus_sensor_weather"
75b23ff8 283 hour = 60 * 60
e62110ca 284 return cache_get_fmt_def(src, "temperature_f", 3 * hour, "%d")
75b23ff8
SK
285}
286
e62110ca
SK
287# -----------------------------------------------------------------------------
288# Datetime
289# -----------------------------------------------------------------------------
290
03c229bf 291function bar_make_status_datetime( dt) {
c4fd5e7d 292 return cache_get_fmt_def("khatus_sensor_datetime", "datetime", 5, "%s")
75b23ff8 293}
This page took 0.080675 seconds and 5 git commands to generate.