1 /**
2 * I3cstatus prints a configurable status bar for the i3 window manager
3 * Copyright (C) 2020 Aaron Ball <nullspoon@oper.io>
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 #include "config_mem.h"
19
20 void config_mem_init(struct node* n) {
21 n->data = NULL;
22 n->type = CTYPE_MEM;
23 n->loadfunc = &config_mem_load;
24 n->loadkey = &load_mem_key;
25 }
26
27 void load_mem_key(struct node* n, char* key, char* val) {
28 }
29
30 int config_mem_load(struct node* n) {
31 long total;
32 long free;
33 long available;
34
35 int percent;
36 long used;
37
38 // See https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=34e431b0ae398fc54ea69ff85ec700722c9da773
39 FILE* fd = fopen("/proc/meminfo", "r");
40 fscanf(fd, "MemTotal: %ld kB ", &total);
41 fscanf(fd, "MemFree: %ld kB ", &free);
42 fscanf(fd, "MemAvailable: %ld kB ", &available);
43 fclose(fd);
44
45 used = total - available;
46 percent = used * 100 / total;
47
48 if(percent < 20) {
49 strcpy(n->color, C_GREEN);
50 } else if(percent < 40) {
51 strcpy(n->color, C_YELLOW);
52 } else if(percent < 60) {
53 strcpy(n->color, C_ORANGE);
54 } else {
55 strcpy(n->color, C_RED);
56 }
57 strcpy(n->label_color, C_LGREY);
58
59 if(strcmp(n->display, "bar") == 0) {
60 print_bar(12, (double)used / total, n->text);
61 } else {
62 sprintf(n->text, "%02d%%", percent);
63 }
64
65 return 0;
66 }
|