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