This repository has been archived by the owner on Jan 20, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodule.c
511 lines (414 loc) · 11.3 KB
/
module.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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <math.h>
#include <errno.h>
#include <stdbool.h>
#include <locale.h>
#include <wchar.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/statvfs.h>
#include <stdint.h>
#include <inttypes.h>
#include <unistd.h>
#include <sys/wait.h>
#include "module.h"
#include "util.h"
#define Icon_markup(icon) "<span color='#" icon_color "'>" icon "</span> "
#define Percent_size (sizeof("100 %") - 1)
#define Icon_size 4 // Unicode
#define Markup_size (sizeof(Icon_markup("")) - 1 + Icon_size)
#define Memory_size 20 + Markup_size
#define Brightness_size Markup_size + Percent_size + 1
#define Memtotal_bufferlen 25 // 28 - 3
#define Memory_bufferlen 81 // 28 × 3 - 3
#define Batpath "/sys/class/power_supply/BAT0/"
#define Backlightpath "/sys/class/backlight/intel_backlight/"
#define Icon_markupf "<span color='#" icon_color "'>%s</span> "
#define String_array_size(arr) (sizeof(arr) / sizeof((arr)[0]) - 1)
#define Char_array_size(arr) (sizeof(arr) / sizeof((arr)[0]))
#define ncmp(buffer, str) strncmp(buffer, str, sizeof(str) - 1)
#define len(arg) (sizeof(arg) - 1)
#define die(e) do { fprintf(stderr, "%s\n", e); exit(EXIT_FAILURE); } while (0);
char *module_execute(const char *path) {
static char buffer[128];
FILE *fp = popen(path, "r");
if (fp == NULL)
return "Failed to execute file.";
char ch; uint8_t i = 0;
while ((ch = getc(fp)) != '\n' && ch != EOF && i < sizeof(buffer)) {
buffer[i] = ch;
i += 1;
}
buffer[i] = '\0';
pclose(fp);
return buffer;
}
float get_memory_total(void)
{
FILE *meminfo = fopen("/proc/meminfo", "r");
if (meminfo == NULL) {
exit(1);
}
char buffer[Memtotal_bufferlen];
fgets(buffer, sizeof(buffer), meminfo);
fclose(meminfo);
uint32_t total;
sscanf(buffer, "MemTotal: %" PRIu32, &total);
return total;
}
float get_memory_used(float total)
{
FILE *fp = fopen("/proc/meminfo", "r");
if (fp == NULL) {
exit(1);
}
char buffer[Memory_bufferlen];
float free = 0;
float cached = 0;
bool found_free = false;
bool found_cached = false;
while (fgets(buffer, sizeof(buffer), fp)) {
if (ncmp(buffer, "MemFree:") == 0) {
sscanf(buffer + len("MemFree:"), "%f", &free);
if (found_cached)
break;
found_free = true;
} else if (ncmp(buffer, "Cached:") == 0) {
sscanf(buffer + len("Cached:"), "%f", &cached);
if (found_free)
break;
found_cached = true;
}
}
fclose(fp);
return total - free - cached;
}
void memstr(char *buffer, float mem)
{
const char unit_pfx[] = { 'K', 'M', 'G', 'T' };
uint8_t i = 0;
for (; mem >= 1024 && i < 4; i += 1) {
mem /= 1024;
}
char num[sizeof("1024")];
if (mem < 10) {
snprintf(num, sizeof(num), "%.2f", mem);
} else if (mem < 100) {
snprintf(num, sizeof(num), "%.1f", mem);
} else {
snprintf(num, sizeof(num), "%.f", mem);
}
snprintf(buffer, sizeof("1024 KiB"), "%s %ciB", num, unit_pfx[i]);
}
char *module_cpu_icon(void)
{
return Icon_markup("");
}
char *module_memory_icon(void)
{
return Icon_markup("");
}
char *module_memory_total(float total) {
static char total_str[sizeof("1024 KiB")];
memstr(total_str, total);
return total_str;
}
char *module_memory_used(float used) {
static char used_str[sizeof("1024 KiB")];
memstr(used_str, used);
return used_str;
}
inline float get_drive_total(void)
{
struct statvfs buffer;
const int8_t ret = statvfs("/", &buffer);
if (!ret) {
return buffer.f_blocks * buffer.f_frsize / 1024;
}
exit(1);
}
float get_drive_used(float total)
{
struct statvfs buffer;
const int8_t ret = statvfs("/", &buffer);
const float available = buffer.f_bfree * buffer.f_frsize / 1024;
float used = total - available;
return used;
}
char *module_drive_icon(void)
{
return Icon_markup("");
}
char *module_drive_total(float total)
{
static char total_str[sizeof("1024 KiB")];
memstr(total_str, total);
return total_str;
}
char *module_drive_used(float used)
{
static char used_str[sizeof("1024 KiB")];
memstr(used_str, used);
return used_str;
}
char *module_time_icon(void)
{
return Icon_markup("");
}
char *module_time_str(const char *format)
{
time_t raw_time;
time(&raw_time);
struct tm* local_time;
local_time = localtime(&raw_time);
static char time[100];
strftime(time, sizeof(time), format, local_time);
return time;
}
char *module_battery_icon()
{
struct stat s;
int err = stat(Batpath, &s);
if(err == -1) {
if(ENOENT == errno) {
/* does not exist */
return NULL;
} else {
perror("stat");
exit(1);
}
}
char capacity_str[4]; // percentage
char *capacity_ptr;
read_first_line(capacity_str, sizeof(capacity_str), Batpath "capacity");
uint8_t percent = strtol(capacity_str, &capacity_ptr, 10);
char icons[11][5] = {
"", "", "", "", "", "", "", "", "", "", ""
};
char icons_charging[11][5] = {
"", "", "", "", "", "", "", "", "", "", ""
};
uint8_t index = percent / 10;
FILE *status_file = fopen(Batpath "status", "r");
if (status_file == NULL) {
return NULL;
}
char status = fgetc(status_file);
fclose(status_file);
char icon[5];
switch (status)
{
case 'C': // ‘Charging’
strcpy(icon, icons_charging[index]);
//charge_now = charge_full - charge_now;
break;
default: // ‘Discharging’ / ‘Not charging’ / ‘Unknown’
strcpy(icon, icons[index]);
break;
}
static char markup[Markup_size + 1];
snprintf(markup, sizeof(markup),
"<span color='#" icon_color "'>%s</span> ", icon);
return markup;
}
char *module_battery_remaining_percentage()
{
struct stat s;
int err = stat(Batpath, &s);
if(err == -1) {
if(ENOENT == errno) {
/* does not exist */
return NULL;
} else {
perror("stat");
exit(1);
}
}
char capacity_str[4]; // percentage
char *capacity_ptr;
read_first_line(capacity_str, sizeof(capacity_str), Batpath "capacity");
uint8_t percent = strtol(capacity_str, &capacity_ptr, 10);
static char battery_percentage[sizeof("100 %")];
snprintf(battery_percentage, sizeof(battery_percentage),
"%d %%", percent);
return battery_percentage;
}
char *module_battery_time_remaining() {
/* char icons_misc[3][5] = { */
/* "", "", "" */
/* }; */
struct stat s;
int err = stat(Batpath, &s);
if(err == -1) {
if(ENOENT == errno) {
/* does not exist */
return NULL;
} else {
perror("stat");
exit(1);
}
}
char capacity_str[4]; // percentage
read_first_line(capacity_str, sizeof(capacity_str), Batpath "capacity");
FILE *status_file = fopen(Batpath "status", "r");
if (status_file == NULL) {
return NULL;
}
char status = fgetc(status_file);
fclose(status_file);
char charge_now_str[10];
char charge_full_str[10];
char current_now_str[10];
// Use energy and power if charge and current are not available.
if (read_first_line(charge_now_str,
sizeof(charge_now_str),
Batpath "charge_now") != 0 ||
read_first_line(charge_full_str,
sizeof(charge_full_str),
Batpath "charge_full") != 0 ||
read_first_line(current_now_str,
sizeof(current_now_str),
Batpath "current_now") != 0
) {
if (read_first_line(charge_now_str,
sizeof(charge_now_str),
Batpath "energy_now") != 0 ||
read_first_line(charge_full_str,
sizeof(charge_full_str),
Batpath "energy_full") != 0 ||
read_first_line(current_now_str,
sizeof(current_now_str),
Batpath "power_now") != 0
) {
return NULL;
}
}
char charge_stop_threshold_str[4]; // percentage
read_first_line(charge_stop_threshold_str, sizeof(charge_stop_threshold_str), Batpath "charge_stop_threshold");
char *charge_now_ptr;
double charge_now = strtod(charge_now_str, &charge_now_ptr);
char *current_now_ptr;
double current_now = strtod(current_now_str, ¤t_now_ptr);
char *capacity_ptr;
//uint8_t capacity = strtol(capacity_str, &capacity_ptr, 10);
//uint8_t capacity
char *charge_stop_threshold_ptr;
uint8_t charge_stop_threshold = strtol(charge_stop_threshold_str, &charge_stop_threshold_ptr, 10);
char *charge_full_ptr;
double charge_full = strtod(charge_full_str, &charge_full_ptr);
uint8_t capacity = round((charge_now / charge_full) * 100);
uint8_t capacity_index = capacity / 10;
//printf("%"PRIu8 "\n", (uint8_t)round((charge_now / charge_full) * 100));
//char icon[5];
switch (status)
{
case 'C': // ‘Charging’
//strcpy(icon, icons_charging[index]);
charge_now = charge_full - charge_now;
break;
}
if (capacity == charge_stop_threshold) {
return "~";
}
if (current_now == 0) {
return "\u221e"; // infinity
}
double hours_total = charge_now / current_now;
/* Truncate the integers after the decimal point (fractions of an hour). */
uint8_t hours = hours_total;
/* Convert the number after the decimal point to minutes. */
double minutes_total = (hours_total - hours) * 60;
/* Truncate the integers after the decimal point (fractions of a minute). */
uint8_t minutes = minutes_total;
/* Convert the number after the decimal point to seconds. */
uint8_t seconds = round((minutes_total - minutes) * 60);
//static char battery_life[sizeof("000:00:00")];
static char battery_life[sizeof("000 h 00 m 00 s")];
if (hours != 0) {
snprintf(battery_life, sizeof(battery_life),
"%" PRIu8 " h %02" PRIu8 " m %02" PRIu8 " s",
hours, minutes, seconds);
} else if (minutes != 0) {
snprintf(battery_life, sizeof(battery_life),
"%" PRIu8 " m %02" PRIu8 " s",
minutes, seconds);
} else {
snprintf(battery_life, sizeof(battery_life),
"%" PRIu8 " s",
seconds);
}
return battery_life;
//return "lol";
}
uint8_t get_backlight_brightness_percentage(void)
{
struct stat s;
int err = stat(Backlightpath, &s);
if (err == -1) {
if (ENOENT == errno) {
0; // NULL before
} else {
perror("stat");
exit(1);
}
}
char brightness_str[7];
read_first_line(brightness_str, sizeof(brightness_str),
Backlightpath "brightness");
char max_brightness_str[7];
read_first_line(max_brightness_str, sizeof(max_brightness_str),
Backlightpath "max_brightness");
float brightness = strtod(brightness_str, NULL);
float max_brightness = strtod(max_brightness_str, NULL);
uint8_t brightness_percent = round(brightness / max_brightness * 100);
return brightness_percent;
}
char *module_backlight_icon(const uint8_t percent)
{
char icon[5];
if (percent < 25) {
strcpy(icon, "");
} else if (percent < 50) {
strcpy(icon, "");
} else if (percent < 75) {
strcpy(icon, "");
} else {
strcpy(icon, "");
}
static char backlight[Markup_size + 1];
snprintf(backlight, sizeof(backlight),
"<span color='#" icon_color "'>%s</span> ", icon);
return backlight;
}
char *format_percentage(const uint8_t percent)
{
static char backlight[sizeof("100 %")];
snprintf(backlight, sizeof(backlight),
"%" PRIu8 " %%", percent);
return backlight;
}
void get_uptime_info(double uptime_info[]) {
char *line = read_file("/proc/uptime");
int line_len = strlen(line);
line[line_len - 1] = '\0';
int space_index = 0;
while (line[space_index] != ' ') {
space_index++;
}
char uptime_str[space_index + 1];
strncpy(uptime_str, line, space_index);
uptime_str[space_index] = '\0';
char *uptime_ptr;
double uptime = strtod(uptime_str, &uptime_ptr);
char idletime_str[line_len];
strncpy(idletime_str, &line[line_len - space_index - 1], line_len - 1);
idletime_str[line_len - 1] = '\0';
char *idletime_ptr;
double idletime = strtod(idletime_str, &idletime_ptr);
free(line);
uptime_info[0] = uptime;
uptime_info[1] = idletime;
}