Commit | Line | Data |
---|---|---|
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 | # ----------------------------------------------------------------------------- | |
8482fea6 SK |
13 | $3 == "data" { |
14 | delete msg | |
15 | msg_in_parse(msg, $0) | |
16 | cache_update(msg["node"], msg["module"], msg["key"], msg["val"]) | |
75b23ff8 SK |
17 | } |
18 | ||
8482fea6 SK |
19 | $1 == Node && \ |
20 | $2 == "khatus_sensor_datetime" && \ | |
21 | $3 == "data" { | |
03c229bf | 22 | # Code for bar_make_status is expected to be passed as an |
ab9fe663 | 23 | # additional source file, using -f flag. |
8482fea6 | 24 | msg_out_status_bar(bar_make_status()) |
75b23ff8 SK |
25 | } |
26 | ||
75b23ff8 | 27 | # ----------------------------------------------------------------------------- |
e62110ca | 28 | # Energy |
75b23ff8 SK |
29 | # ----------------------------------------------------------------------------- |
30 | ||
65034298 SK |
31 | function bar_make_status_energy_percent() { |
32 | return cache_get_fmt_def("khatus_sensor_energy", "battery_percentage", 0, "%d") | |
e62110ca | 33 | } |
75b23ff8 | 34 | |
e62110ca SK |
35 | function bar_make_status_energy_direction( state, direction_of_change) { |
36 | cache_get(state, "khatus_sensor_energy", "battery_state", 0) | |
269df6fa | 37 | if (state["value"] == "discharging") { |
75b23ff8 | 38 | direction_of_change = "<" |
269df6fa | 39 | } else if (state["value"] == "charging") { |
75b23ff8 SK |
40 | direction_of_change = ">" |
41 | } else { | |
42 | direction_of_change = "=" | |
43 | } | |
e62110ca SK |
44 | return direction_of_change |
45 | } | |
75b23ff8 | 46 | |
e62110ca SK |
47 | # ----------------------------------------------------------------------------- |
48 | # Memory | |
49 | # ----------------------------------------------------------------------------- | |
50 | ||
e62110ca | 51 | function bar_make_status_mem_percent( total, used, percent, percent_str) { |
c4fd5e7d SK |
52 | cache_get(total, "khatus_sensor_memory", "total", 5) |
53 | cache_get(used , "khatus_sensor_memory", "used" , 5) | |
269df6fa SK |
54 | # Checking total["value"] to avoid division by zero when data is missing |
55 | if (!total["is_expired"] && \ | |
56 | !used["is_expired"] && \ | |
57 | total["value"] \ | |
58 | ) { | |
8482fea6 | 59 | percent = num_round((used["value"] / total["value"]) * 100) |
e62110ca | 60 | percent_str = sprintf("%d", percent) |
75b23ff8 | 61 | } else { |
e62110ca | 62 | percent_str = "__" |
75b23ff8 | 63 | } |
e62110ca | 64 | return percent_str |
75b23ff8 SK |
65 | } |
66 | ||
e62110ca SK |
67 | # ----------------------------------------------------------------------------- |
68 | # Processes | |
69 | # ----------------------------------------------------------------------------- | |
65034298 SK |
70 | # From man ps: |
71 | # D uninterruptible sleep (usually IO) | |
72 | # R running or runnable (on run queue) | |
73 | # S interruptible sleep (waiting for an event to complete) | |
74 | # T stopped by job control signal | |
75 | # t stopped by debugger during the tracing | |
76 | # W paging (not valid since the 2.6.xx kernel) | |
77 | # X dead (should never be seen) | |
78 | # Z defunct ("zombie") process, terminated but not reaped by its parent | |
79 | # | |
80 | # Additionally, not documented in ps man page: | |
81 | # I Idle | |
b9d5cfd4 | 82 | |
e62110ca SK |
83 | function bar_make_status_procs_count_all() { |
84 | return cache_get_fmt_def("khatus_sensor_procs", "total_procs", 15, "%d") | |
85 | } | |
86 | ||
87 | function bar_make_status_procs_count_r( src) { | |
88 | src = "khatus_sensor_procs" | |
89 | return cache_get_fmt_def(src, "total_per_state" Kfs "R", 15, "%d", "0") | |
90 | } | |
91 | ||
92 | function bar_make_status_procs_count_d( src) { | |
93 | src = "khatus_sensor_procs" | |
94 | return cache_get_fmt_def(src, "total_per_state" Kfs "D", 15, "%d", "0") | |
95 | } | |
96 | ||
97 | function bar_make_status_procs_count_t( src) { | |
98 | src = "khatus_sensor_procs" | |
99 | return cache_get_fmt_def(src, "total_per_state" Kfs "T", 15, "%d", "0") | |
100 | } | |
101 | ||
102 | function bar_make_status_procs_count_i( src) { | |
103 | src = "khatus_sensor_procs" | |
104 | return cache_get_fmt_def(src, "total_per_state" Kfs "I", 15, "%d", "0") | |
105 | } | |
106 | ||
107 | function bar_make_status_procs_count_z( src) { | |
108 | src = "khatus_sensor_procs" | |
109 | return cache_get_fmt_def(src, "total_per_state" Kfs "Z", 15, "%d", "0") | |
110 | } | |
111 | ||
112 | # ----------------------------------------------------------------------------- | |
113 | # CPU | |
114 | # ----------------------------------------------------------------------------- | |
115 | ||
e62110ca SK |
116 | function bar_make_status_cpu_loadavg( src) { |
117 | src = "khatus_sensor_loadavg" | |
118 | return cache_get_fmt_def(src, "load_avg_1min", 5, "%4.2f") | |
119 | } | |
120 | ||
121 | function bar_make_status_cpu_temperature() { | |
122 | return cache_get_fmt_def("khatus_sensor_temperature", "temp_c", 5, "%d") | |
123 | } | |
124 | ||
125 | function bar_make_status_cpu_fan_speed() { | |
126 | return cache_get_fmt_def("khatus_sensor_fan", "speed", 5, "%4d") | |
127 | } | |
128 | ||
129 | # ----------------------------------------------------------------------------- | |
130 | # Disk | |
131 | # ----------------------------------------------------------------------------- | |
132 | ||
e62110ca SK |
133 | function bar_make_status_disk_space( src) { |
134 | src = "khatus_sensor_disk_space" | |
135 | return cache_get_fmt_def(src, "disk_usage_percentage", 10, "%s") | |
136 | } | |
137 | ||
138 | function bar_make_status_disk_io_w( src) { | |
139 | src = "khatus_sensor_disk_io" | |
140 | return cache_get_fmt_def(src, "sectors_written", 5, "%0.3f") | |
141 | } | |
142 | ||
143 | function bar_make_status_disk_io_r( src) { | |
144 | src = "khatus_sensor_disk_io" | |
145 | return cache_get_fmt_def(src, "sectors_read", 5, "%0.3f") | |
146 | } | |
147 | ||
148 | # ----------------------------------------------------------------------------- | |
149 | # Network | |
150 | # ----------------------------------------------------------------------------- | |
151 | ||
e62110ca SK |
152 | function bar_make_status_net_addr(interface, src) { |
153 | src = "khatus_sensor_net_addr_io" | |
154 | return cache_get_fmt_def(src, "addr" Kfs interface, 5, "%s", "") | |
155 | } | |
156 | ||
157 | function bar_make_status_net_io_w(interface, src) { | |
158 | src = "khatus_sensor_net_addr_io" | |
159 | return cache_get_fmt_def(src, "bytes_written" Kfs interface, 5, "%0.3f") | |
160 | } | |
161 | ||
162 | function bar_make_status_net_io_r(interface, src) { | |
163 | src = "khatus_sensor_net_addr_io" | |
164 | return cache_get_fmt_def(src, "bytes_read" Kfs interface, 5, "%0.3f") | |
165 | } | |
166 | ||
167 | function bar_make_status_net_wifi(interface, src) { | |
168 | src = "khatus_sensor_net_wifi_status" | |
169 | return cache_get_fmt_def(src, "status" Kfs interface, 10, "%s") | |
170 | } | |
171 | ||
172 | # ----------------------------------------------------------------------------- | |
173 | # Bluetooth | |
174 | # ----------------------------------------------------------------------------- | |
175 | ||
e62110ca | 176 | function bar_make_status_bluetooth_power( src) { |
269df6fa | 177 | src = "khatus_sensor_bluetooth_power" |
e62110ca | 178 | return cache_get_fmt_def(src, "power_status", 10, "%s") |
75b23ff8 SK |
179 | } |
180 | ||
e62110ca | 181 | # ----------------------------------------------------------------------------- |
65034298 | 182 | # Backlight (screen brightness) |
e62110ca SK |
183 | # ----------------------------------------------------------------------------- |
184 | ||
e62110ca | 185 | function bar_make_status_backlight_percent( src) { |
269df6fa | 186 | src = "khatus_sensor_screen_brightness" |
e62110ca | 187 | return cache_get_fmt_def(src, "percentage", 5, "%d") |
75b23ff8 SK |
188 | } |
189 | ||
e62110ca SK |
190 | # ----------------------------------------------------------------------------- |
191 | # Volume | |
192 | # ----------------------------------------------------------------------------- | |
193 | ||
65034298 | 194 | function bar_make_status_volume_pulseaudio_sink(sink, mu, vl, vr, show) { |
fd710ac0 SK |
195 | cache_get(mu, "khatus_sensor_volume", "mute" Kfs sink, 5) |
196 | cache_get(vl, "khatus_sensor_volume", "vol_left" Kfs sink, 5) | |
197 | cache_get(vr, "khatus_sensor_volume", "vol_right" Kfs sink, 5) | |
767766df | 198 | show = "--" |
fd710ac0 SK |
199 | if (!mu["is_expired"] && !vl["is_expired"] && !vr["is_expired"]) { |
200 | if (mu["value"] == "yes") {show = "X"} | |
201 | else if (mu["value"] == "no") {show = vl["value"] " " vr["value"]} | |
75b23ff8 | 202 | else { |
8482fea6 | 203 | msg_out_log_error(\ |
65034298 | 204 | "bar_make_status_volume_pulseaudio_sink: " sink ". ", \ |
fd710ac0 | 205 | "Unexpected value for 'mute' field: " mu["value"] \ |
75b23ff8 SK |
206 | ) |
207 | } | |
75b23ff8 | 208 | } |
e62110ca | 209 | return show |
75b23ff8 SK |
210 | } |
211 | ||
e62110ca SK |
212 | # ----------------------------------------------------------------------------- |
213 | # MPD | |
214 | # ----------------------------------------------------------------------------- | |
215 | ||
65034298 | 216 | function bar_make_status_mpd( state, status) { |
c4fd5e7d | 217 | cache_get(state, "khatus_sensor_mpd", "state", 5) |
269df6fa SK |
218 | if (!state["is_expired"] && state["value"]) { |
219 | if (state["value"] == "play") { | |
03c229bf | 220 | status = bar_make_status_mpd_state_known("▶") |
269df6fa | 221 | } else if (state["value"] == "pause") { |
03c229bf | 222 | status = bar_make_status_mpd_state_known("❚❚") |
269df6fa | 223 | } else if (state["value"] == "stop") { |
03c229bf | 224 | status = bar_make_status_mpd_state_known("⬛") |
75b23ff8 | 225 | } else { |
8482fea6 | 226 | msg_out_log_error(\ |
03c229bf | 227 | "bar_make_status_mpd", \ |
269df6fa | 228 | "Unexpected value for 'state' field: " state["value"] \ |
75b23ff8 SK |
229 | ) |
230 | status = "--" | |
231 | } | |
232 | } else { | |
233 | status = "--" | |
234 | } | |
e62110ca | 235 | return status |
75b23ff8 SK |
236 | } |
237 | ||
03c229bf | 238 | function bar_make_status_mpd_state_known(symbol, s, song, time, percentage) { |
75b23ff8 | 239 | s = "khatus_sensor_mpd" |
c4fd5e7d SK |
240 | song = cache_get_fmt_def(s, "song" , 5, "%s", "?") |
241 | time = cache_get_fmt_def(s, "play_time_minimal_units", 5, "%s", "?") | |
242 | percent = cache_get_fmt_def(s, "play_time_percentage" , 5, "%s", "?") | |
b1c097d6 | 243 | song = substr(song, 1, Opt_Mpd_Song_Max_Chars) |
75b23ff8 SK |
244 | return sprintf("%s %s %s %s", symbol, time, percent, song) |
245 | } | |
246 | ||
e62110ca SK |
247 | # ----------------------------------------------------------------------------- |
248 | # Weather | |
249 | # ----------------------------------------------------------------------------- | |
250 | ||
e62110ca | 251 | function bar_make_status_weather_temp_f( src, hour) { |
fd710ac0 | 252 | src = "khatus_sensor_weather" |
75b23ff8 | 253 | hour = 60 * 60 |
e62110ca | 254 | return cache_get_fmt_def(src, "temperature_f", 3 * hour, "%d") |
75b23ff8 SK |
255 | } |
256 | ||
e62110ca SK |
257 | # ----------------------------------------------------------------------------- |
258 | # Datetime | |
259 | # ----------------------------------------------------------------------------- | |
260 | ||
03c229bf | 261 | function bar_make_status_datetime( dt) { |
c4fd5e7d | 262 | return cache_get_fmt_def("khatus_sensor_datetime", "datetime", 5, "%s") |
75b23ff8 | 263 | } |