-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodules.c
409 lines (321 loc) · 8.85 KB
/
modules.c
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
#include <fcntl.h>
#include <libgen.h>
#include <time.h>
#include <pthread.h>
#include <sensors/sensors.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "modules.h"
#include "log/log.h"
static const char *get_label(enum blocks id);
static const char *get_command(enum blocks id);
void
init_text(block_text *text) {
memset(text, 0, sizeof(block_text));
text->color = 0xFFFFFF;
text->monitor = -1; // Default to all monitors.
}
void
init_internal(const void *input, block_internal *internal) {
const block_input *in = (block_input *) input;
internal->id = in->id;
internal->pipes[0] = in->pipes[0];
internal->pipes[1] = in->pipes[1];
internal->nt = in->nt;
internal->text = malloc(in->nt * sizeof(block_text));
for (int i = 0; i < in->nt; i++) {
init_text(&internal->text[i]);
SET_LABEL(internal->text[i], get_label(internal->id));
SET_COMMAND(internal->text[i], get_command(internal->id));
}
}
void
free_internal(block_internal *internal) { free(internal->text); }
inline void
write_data(const block_internal *internal) {
write(internal->pipes[1], &internal->id, sizeof(enum blocks));
for (int i = 0; i < internal->nt; i++)
write(internal->pipes[1], &internal->text[i], sizeof(block_text));
}
void *battery_block(void *input) {
int cap;
FILE *f;
block_internal block;
init_internal(input, &block);
f = fopen("/sys/class/power_supply/BAT0/capacity" , "r");
while (1) {
fscanf(f, "%d", &cap);
if (cap <= battery_args.bat_crit) SET_COLOR(block.text[0], 0xFF0000);
else if (cap <= battery_args.bat_warn) SET_COLOR(block.text[0], 0xFFFC00);
else SET_COLOR(block.text[0], 0xFFFFFF);
snprintf(block.text[0].text, TEXT_LEN, "%d", cap);
write_data(&block);
sleep(battery_args.dt);
}
free_internal(&block);
fclose(f);
}
void *clock_block (void *input) {
time_t t;
block_internal block;
block_input *in = (block_input *) input;
init_internal(in, &block);
while (1) {
t = time(NULL);
strftime(block.text[0].text, 32, clock_args.time_format, localtime(&t));
write_data(&block);
sleep(clock_args.dt);
}
}
/* CPU Usage */
void *cpu_block (void *input) {
char *color;
FILE *cpuinfo;
unsigned long long int user, nice, sys, idle, iowait, irq, sirq, \
steal, guest, nguest, in_use, total, old_in_use, old_total;
float percent;
block_internal block;
init_internal(input, &block);
cpuinfo = fopen("/proc/stat", "r");
if (fscanf(cpuinfo,
"cpu %llu %llu %llu %llu %llu %llu %llu %llu %llu %llu",
&user, &nice, &sys, &idle, &iowait, &irq, &sirq,
&steal, &guest, &nguest) != 10) {
perror("Couldn't read CPU usage from /proc/stat\n");
exit(1);
}
fclose(cpuinfo);
old_in_use = user + nice + sys + irq + sirq + steal + guest + nguest;
old_total = in_use + idle + iowait;
while (1) {
cpuinfo = fopen("/proc/stat", "r");
if (fscanf(cpuinfo,
"cpu %llu %llu %llu %llu %llu %llu %llu %llu %llu %llu",
&user, &nice, &sys, &idle, &iowait, &irq, &sirq,
&steal, &guest, &nguest) != 10) {
perror("Couldn't read CPU usage from /proc/stat\n");
exit(1);
}
fclose(cpuinfo);
in_use = user + nice + sys + irq + sirq + steal + guest + nguest;
total = in_use + idle + iowait;
percent = 100.0f * (in_use - old_in_use) / (float) (total - old_total);
// Get color
if (percent > cpu_args.cpu_crit) SET_COLOR(block.text[0], 0xFF0000);
else if (percent > cpu_args.cpu_warn) SET_COLOR(block.text[0], 0xFFFC00);
else SET_COLOR(block.text[0], 0xFFFFFF);
snprintf(block.text[0].text, 32, "%.1lf%%", percent);
write_data(&block);
old_in_use = in_use;
old_total = total;
sleep(cpu_args.dt);
}
}
void *mail_block (void *input) {
FILE *mail_fd;
int unread;
block_internal block;
init_internal(input, &block);
while (1) {
mail_fd = popen(mail_args.command, "r");
fscanf(mail_fd, "%d\n", &unread);
pclose(mail_fd);
sprintf(block.text[0].text, "%d", unread);
write_data(&block);
usleep(2000);
read(block.pipes[0], block.text[0].text, 1);
}
}
void *mem_block (void *input) {
FILE *meminfo_fd;
int mem_free, mem_tot;
block_internal block;
init_internal(input, &block);
while (1) {
meminfo_fd = fopen("/proc/meminfo", "r");
fscanf(meminfo_fd, "MemTotal: %d kB\nMemFree: %d kB", &mem_tot, &mem_free);
fclose(meminfo_fd);
snprintf(block.text[0].text, 32, "%.1f G",
((float) mem_free) / ((float) (1<<20)));
write_data(&block);
sleep(mem_args.dt);
}
}
/* Temperature */
void *temp_block (void *input) {
char *color;
sensors_chip_name match;
const sensors_chip_name *chip;
const sensors_feature *f;
const sensors_subfeature *sf;
int nc = 0, nf = 0, nsf = 0;
int temp_sf = -1;
double temp;
block_internal block;
init_internal(input, &block);
if (sensors_init(NULL)) {
log_error("sensor init");
exit(EXIT_FAILURE);
}
if (sensors_parse_chip_name(temp_args.chip, &match)) {
log_error("parse name\n");
exit(EXIT_FAILURE);
}
// Use first matching chip
if (!(chip = sensors_get_detected_chips(&match, &nc))) {
log_error("get_chip");
exit(EXIT_FAILURE);
}
while ((f = sensors_get_features(chip, &nf))) {
if (temp_sf >= 0) break;
if (f->type != SENSORS_FEATURE_TEMP) continue;
while ((sf = sensors_get_all_subfeatures(chip, f, &nsf))) {
if (temp_sf >= 0) break;
if (sf->type != SENSORS_SUBFEATURE_TEMP_INPUT) continue;
if (sensors_get_value(chip, nsf, &temp)) {
log_error("sensor read");
exit(EXIT_FAILURE);
}
temp_sf = nsf;
break;
}
}
while (1) {
if (sensors_get_value(chip, temp_sf, &temp)) {
log_error("sensor read");
exit(EXIT_FAILURE);
}
if (temp > temp_args.T_crit)
SET_COLOR(block.text[0], 0xFF0000);
else if (temp > temp_args.T_warn)
SET_COLOR(block.text[0], 0xFFFC00);
else
SET_COLOR(block.text[0], 0xFFFFFF);
snprintf(block.text[0].text, TEXT_LEN, "%.1f°C", temp);
write_data(&block);
sleep(temp_args.dt);
}
sensors_free_chip_name(&match);
sensors_cleanup();
}
void *vol_block(void *input) {
FILE *pulse_fd;
int vol[2];
block_internal block;
init_internal(input, &block);
while (1) {
pulse_fd = popen("pulsemixer --get-volume", "r");
fscanf(pulse_fd, "%d %d\n", vol, vol + 1);
pclose(pulse_fd);
snprintf(block.text[0].text, TEXT_LEN, "%d", vol[0]);
write_data(&block);
usleep(2000);
read(block.pipes[0], block.text[0].text, 1);
}
}
/* void *systray(void *input) { */
/* char buf[256], desktop[32]; */
/* FILE *systray, *xdo; */
/* int fifo, width, height, pos_x, pos_y; */
/* unsigned long systray_wid; */
/* float temp; */
/* tray_arg* arg; */
/* block_input* in; */
/* block_output out; */
/* in = (block_input *) input; */
/* arg = (tray_arg *) in->arg; */
/* out.id = in->id; */
/* out.data = buf; */
/* snprintf(buf, 256, */
/* "stalonetray --geometry 1x1+%d-%d -i %d \ */
/* --grow-direction W --log-level info \ */
/* --kludges fix_window_pos,force_icons_size,use_icons_hints", */
/* arg->x_pos, arg->y_pos, arg->icon_size); */
/* systray = popen(buf, "r"); */
/* // Get id */
/* xdo = popen("xdo id -a cobar", "r"); */
/* fscanf(xdo, "%lx", &systray_wid); */
/* pclose(xdo); */
/* while (1) { */
/* // read input from tray */
/* fgets(buf, 128, systray); */
/* printf("%s\n", buf); */
/* if ( sscanf(buf, "geometry: %dx%d+%d+%d", */
/* &width, &height, &pos_x, &pos_y) == 4 ) { */
/* // Write new geometry to bar */
/* out.flags = BAR_RESIZE; */
/* write(fifo, &out, sizeof(out)); */
/* } */
/* } */
/* } */
void *(*get_block_func (enum blocks id)) (void *) {
switch (id) {
case BATTERY:
return battery_block;
case CLOCK:
return clock_block;
case CPU:
return cpu_block;
case DESKTOP:
return desktop_block;
case MAIL:
return mail_block;
case MEMORY:
return mem_block;
case TEMP:
return temp_block;
case VOLUME:
return vol_block;
default:
return NULL;
}
}
const char *get_label(enum blocks id) {
switch (id) {
case BATTERY:
return "BAT";
case CLOCK:
return NULL;
case CPU:
return "CPU";
case DESKTOP:
return NULL;
case MAIL:
return "MAIL";
case MEMORY:
return "MEM";
case TEMP:
return NULL;
case VOLUME:
return "VOL";
default:
return NULL;
}
}
const char *get_command(enum blocks id) {
switch (id) {
case BATTERY:
return NULL;
case CLOCK:
return NULL;
case CPU:
return NULL;
case DESKTOP:
return NULL;
case MAIL:
return NULL;
case MEMORY:
return NULL;
case TEMP:
return NULL;
case VOLUME:
return NULL;
default:
return NULL;
}
}