forked from lynks--/lifebar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
render.c
460 lines (376 loc) · 13.2 KB
/
render.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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
#include "lifebar.h"
int render_divider(cairo_t *cairo, int x, int d) {
if(d == RIGHT) {
if(conf->divstyle == LINE) {
set_cairo_source_colour(cairo, conf->divcol);
cairo_rectangle(cairo,
x - (conf->divpadding + conf->divwidth),
conf->divgap, conf->divwidth,
conf->depth - (conf->divgap * 2));
cairo_fill(cairo);
return (conf->divpadding * 2) + conf->divwidth + 1;
}
else if(conf->divstyle == GROOVE) {
set_cairo_source_colour(cairo, conf->groove_light);
cairo_rectangle(cairo, x - conf->divpadding, 0,
1, conf->depth);
cairo_fill(cairo);
set_cairo_source_colour(cairo, conf->groove_dark);
cairo_rectangle(cairo, x - (conf->divpadding + 1), 0,
1, conf->depth);
cairo_fill(cairo);
return (conf->divpadding * 2) + 2;
}
}
else if(d == LEFT) {
if(conf->divstyle == LINE) {
set_cairo_source_colour(cairo, conf->divcol);
cairo_rectangle(cairo,
x + conf->divpadding, conf->divgap,
conf->divwidth,
conf->depth - (conf->divgap * 2));
cairo_fill(cairo);
return (conf->divpadding * 2) + conf->divwidth + 1;
}
else if(conf->divstyle == GROOVE) {
set_cairo_source_colour(cairo, conf->groove_light);
cairo_rectangle(cairo, x + conf->divpadding + 1, 0,
1, conf->depth);
cairo_fill(cairo);
set_cairo_source_colour(cairo, conf->groove_dark);
cairo_rectangle(cairo, x + conf->divpadding, 0,
1, conf->depth);
cairo_fill(cairo);
return (conf->divpadding * 2) + 2;
}
}
return 10;
}
int render_interface(cairo_t *c, int x, int y, struct ifaddrs *a,
struct net_speed_info spd[], uint32_t spdi, int d) {
char k_string[32];
char v_string[64];
sprintf(k_string, "%s", a->ifa_name);
//if this interface is up
if(a->ifa_flags & IFF_UP) {
//if this is an ipv4 or ipv6 address
if((a->ifa_addr->sa_family == AF_INET) ||
(a->ifa_addr->sa_family == AF_INET6)) {
//parse the address to readable string
struct sockaddr_in *addr = (struct sockaddr_in *)a->ifa_addr;
char readable[256];
inet_ntop(addr->sin_family, &(addr->sin_addr), readable, 256);
//calculate speed
//NOTE: we are passed the NEXT index to be written to, so the
//newest index availiable is -1, and the oldest is spdi.
int new_index = spdi - 1;
if(new_index < 0) new_index = NET_SPEED_AVERAGE - 1;
int old_index = spdi;
//this loop ensures that we get no extreme values during the first
//few seconds of display. it finds the oldest value that has
//actually been written to, avoiding the zero values.
while(spd[old_index].down_bytes == 0) {
old_index = (old_index + 1) % NET_SPEED_AVERAGE;
if(old_index == new_index) break;
}
float down = (float)
(spd[new_index].down_bytes -
spd[old_index].down_bytes)
/
(float)
(EXPENSIVE_TIME * NET_SPEED_AVERAGE * 1024);
float up = (float)
(spd[new_index].up_bytes -
spd[old_index].up_bytes)
/
(float)
(EXPENSIVE_TIME * NET_SPEED_AVERAGE * 1024);
//build the string
sprintf(v_string, "%s [\xe2\x86\x93 %.2fKiB/s] [\xe2\x86\x91 %.2fKiB/s]",
readable, down, up);
}
else sprintf(v_string, "down");
}
else sprintf(v_string, "down");
return render_keyvalue(c, x, y, k_string, v_string, d);
}
int render_filesystem(cairo_t *c, int x, int y, struct statvfs *fs,
char *path, int d) {
unsigned long long fs_cap =
(unsigned long long)fs->f_bsize * fs->f_blocks;
unsigned long long fs_free =
(unsigned long long)fs->f_bsize * fs->f_bavail;
char k_string[32];
char v_string[32];
int giga = 1024 * 1024 * 1024;
sprintf(k_string, "%s", path);
sprintf(v_string, "%.1fGiB", (double)fs_free / giga);
return render_keyvalue(c, x, y, k_string, v_string, d);
}
int render_workspace(cairo_t *cairo, int x, int y,
struct i3_workspace *ws, int d) {
//set colour based on visibility and urgency
struct colour *c = NULL;
if(strcmp(ws->visible, "true") == 0) {
if(strcmp(ws->urgent, "true") == 0) {
c = conf->urgent_visible;
} else {
c = conf->viswscol;
}
} else {
if(strcmp(ws->urgent, "true") == 0) {
c = conf->urgent;
} else {
c = conf->inviswscol;
}
}
set_cairo_source_colour(cairo, c);
//set font
cairo_set_font_face(cairo, conf->wsfont);
cairo_set_font_size(cairo, conf->wsfontsize);
cairo_text_extents_t extents;
cairo_text_extents(cairo, ws->name, &extents);
int render_x = x;
if(d == RIGHT) render_x = x - extents.width;
//draw the text
cairo_move_to(cairo, render_x, y);
cairo_show_text(cairo, ws->name);
//update the padding
return extents.width + 2;
}
int render_bluetooth(cairo_t *c, int x, int y, int connected, int d) {
if(!connected) {
return 0;
}
char k_string[32];
char v_string[32];
sprintf(k_string, "Bluetooth: ");
sprintf(v_string, "Connected");
return render_keyvalue(c, x, y, k_string, v_string, d);
}
int render_interface_info(cairo_t *c, int x, int y,
struct net_info net, int d) {
char k_string[32];
char v_string[32];
sprintf(k_string, "Wireless: ");
sprintf(v_string, net.name);
return render_keyvalue(c, x, y, k_string, v_string, d);
}
int render_battery(cairo_t *c, int x, int y, struct batt_info *b, int d) {
char k_string[32];
char v_string[32];
sprintf(k_string, "Battery: ", b->index);
char time_left[64];
strncpy(time_left, b->time_left + 1, 4);
switch(b->status) {
case CHARGING:
sprintf(v_string, "\xe2\x86\x91%d%% %s", b->percent, time_left);
break;
case DISCHARGING:
sprintf(v_string, "\xe2\x86\x93%d%% %s", b->percent, time_left);
break;
case FULL:
sprintf(v_string, "100%%", b->index);
break;
case UNKNOWN:
sprintf(v_string, "idle %d%%", b->percent);
}
return render_keyvalue(c, x, y, k_string, v_string, d);
}
int render_thermal(cairo_t *c, int x, int y, struct thermal_info *t, int d) {
char k_string[32];
char v_string[32];
sprintf(k_string, "thermal-%d", t->index);
sprintf(v_string, "%d\xc2\xb0" "C", t->temp_c);
return render_keyvalue(c, x, y, k_string, v_string, d);
}
int render_time(cairo_t *cairo, int x, int y, int d) {
time_t rawnow = time(NULL);
struct tm *now = localtime(&rawnow);
char time_string[128];
strftime(time_string, 128, conf->timefmt, now);
set_cairo_source_colour(cairo, conf->timecol);
cairo_set_font_face(cairo, conf->timefont);
cairo_set_font_size(cairo, conf->timefontsize);
cairo_text_extents_t extents;
cairo_text_extents(cairo, time_string, &extents);
int render_x = x;
if(d == RIGHT) render_x = x - extents.width;
cairo_move_to(cairo, render_x, y);
cairo_show_text(cairo, time_string);
return extents.width - 1;
}
int render_volume(cairo_t *cairo, int x, int y, struct volume_info vol_info, int d) {
char k_string[32];
char v_string[32];
sprintf(k_string, "\u266a: ");
if(vol_info.is_muted) {
sprintf(v_string, "M");
} else {
sprintf(v_string, "%d%%", vol_info.volume_percent);
}
return render_keyvalue(cairo, x, y, k_string, v_string, d);
}
int render_alarm(cairo_t *cairo, uint32_t alarm_s, int x, int y, int d) {
struct tm *atm = malloc(sizeof *atm);
memset(atm, 0, sizeof *atm);
char *format = malloc(32);
sprintf(format, "%s", "%M:%S");
char time_string[128];
while(alarm_s >= 3600) {
atm->tm_hour++;
alarm_s -= 3600;
sprintf(format, "%s", "%H:%M:%S");
}
while(alarm_s >= 60) {
atm->tm_min++;
alarm_s -= 60;
}
atm->tm_sec = alarm_s;
strftime(time_string, 128, format, atm);
free(atm);
free(format);
set_cairo_source_colour(cairo, conf->alarmcol);
cairo_set_font_face(cairo, conf->timefont);
cairo_set_font_size(cairo, conf->timefontsize);
cairo_text_extents_t extents;
cairo_text_extents(cairo, time_string, &extents);
int render_x = x;
if(d == RIGHT) render_x = x - extents.width;
cairo_move_to(cairo, render_x, y);
cairo_show_text(cairo, time_string);
return extents.width - 1;
}
int render_uptime(cairo_t *cairo, uint32_t ut_s, int x, int y, int d) {
char time_string[128];
uint16_t days = 0;
uint16_t hours = 0;
uint16_t minutes = 0;
while(ut_s >= 3600 * 24) { // days
days++;
ut_s -= 3600 * 24;
}
while(ut_s >= 3600) {
hours++;
ut_s -= 3600;
}
while(ut_s >= 60) {
minutes++;
ut_s -= 60;
}
sprintf(time_string, "%d days, %d hours, %d minutes", days, hours, minutes);
return render_keyvalue(cairo, x, y, "uptime", time_string, d);
}
int render_date(cairo_t *cairo, int x, int y, int d) {
time_t rawnow = time(NULL);
struct tm *now = localtime(&rawnow);
char date_string[256];
strftime(date_string, 256, conf->datefmt, now);
set_cairo_source_colour(cairo, conf->datecol);
cairo_set_font_face(cairo, conf->datefont);
cairo_set_font_size(cairo, conf->datefontsize);
cairo_text_extents_t extents;
cairo_text_extents(cairo, date_string, &extents);
int render_x = x;
if(d == RIGHT) render_x = x - extents.width;
cairo_move_to(cairo, render_x, y);
cairo_show_text(cairo, date_string);
return extents.width - 1;
}
int render_keyvalue(cairo_t *cairo, int x, int y, char *k, char *v , int d) {
uint32_t render_x;
cairo_text_extents_t k_extents;
cairo_text_extents_t v_extents;
//we must set the fonts to measure extents
cairo_set_font_face(cairo, conf->keyfont);
cairo_set_font_size(cairo, conf->keyfontsize);
cairo_text_extents(cairo, k, &k_extents);
cairo_set_font_face(cairo, conf->valfont);
cairo_set_font_size(cairo, conf->valfontsize);
cairo_text_extents(cairo, v, &v_extents);
//render key
set_cairo_source_colour(cairo, conf->keycol);
cairo_set_font_face(cairo, conf->keyfont);
cairo_set_font_size(cairo, conf->keyfontsize);
render_x = x;
if(d == RIGHT)
render_x = x - (k_extents.width + v_extents.width + conf->kvpadding);
cairo_move_to(cairo, render_x, y);
cairo_show_text(cairo, k);
//render value
set_cairo_source_colour(cairo, conf->valcol);
cairo_set_font_face(cairo, conf->valfont);
cairo_set_font_size(cairo, conf->valfontsize);
render_x = x + k_extents.width + conf->kvpadding;
if(d == RIGHT)
render_x = x - v_extents.width;
cairo_move_to(cairo, render_x, y);
cairo_show_text(cairo, v);
return k_extents.width + conf->kvpadding + v_extents.width;
}
struct colour *prepare_colour(int r, int g, int b, int a) {
struct colour *c = malloc(sizeof c);
c->red = r;
c->green = g;
c->blue = b;
c->alpha = a;
return c;
}
struct colour *parse_config_colour(char *value) {
int r = 0;
int g = 0;
int b = 0;
int a = 0;
int success = sscanf(value, " %3d , %3d , %3d , %3d ", &r, &g, &b, &a);
if(success == 4) return prepare_colour(r, g, b, a);
fprintf(stderr, "%sbad formatting of config colour: '%s'\n",
BAD_MSG, value);
return NULL;
}
void parse_config_font(const char *confkey, char *value) {
char face[64];
char weight[8];
int size;
int success = sscanf(value, " %64[^:] : %8[^:] : %2u",
face, weight, &size);
cairo_font_weight_t weight_t = CAIRO_FONT_WEIGHT_NORMAL;
if(strcmp(weight, "bold") == 0) weight_t = CAIRO_FONT_WEIGHT_BOLD;
if(success == 3) {
//build the font
cairo_font_face_t *font =
cairo_toy_font_face_create(face,
CAIRO_FONT_SLANT_NORMAL,
weight_t);
//TODO replace this mess with enums and config array
if(strcmp(confkey, "keyfont") == 0) {
conf->keyfont = font;
conf->keyfontsize = (double)size;
}
else if(strcmp(confkey, "valfont") == 0) {
conf->valfont = font;
conf->valfontsize = (double)size;
}
else if(strcmp(confkey, "datefont") == 0) {
conf->datefont = font;
conf->datefontsize = (double)size;
}
else if(strcmp(confkey, "timefont") == 0) {
conf->timefont = font;
conf->timefontsize = (double)size;
}
else if(strcmp(confkey, "wsfont") == 0) {
conf->wsfont = font;
conf->wsfontsize = (double)size;
}
}
else fprintf(stderr, "%sbad formatting of config font: '%s'\n",
BAD_MSG, value);
}
void set_cairo_source_colour(cairo_t *cairo, struct colour *col) {
//cairo uses 0.0->1.0 for 0->255
double r = col->red / (double)255;
double g = col->green / (double)255;
double b = col->blue / (double)255;
double a = col->alpha / (double)255;
cairo_set_source_rgba(cairo, r, g, b, a);
}