diff options
author | Aaron Ball <nullspoon@oper.io> | 2020-05-12 08:35:44 -0600 |
---|---|---|
committer | Aaron Ball <nullspoon@oper.io> | 2020-05-12 09:03:49 -0600 |
commit | 3350ce6f2546c5d8891074e21b0d4971c6ffca8c (patch) | |
tree | aba195ab474f90cfb0b5e48b88cb32c2c2eac2db /src | |
download | i3cstat-3350ce6f2546c5d8891074e21b0d4971c6ffca8c.tar.gz i3cstat-3350ce6f2546c5d8891074e21b0d4971c6ffca8c.tar.xz |
Initial commit
This includes support for printing battery, cpu, filesystem, memory,
time, and date sections. Sections with percentage values also support
the `display = bar` configuration to make them display a bar rather than
the actual percent. Finally, for relevant data types, colors change from
green to red as percentage approaches appropriate levels (eg: when
memory is high, color is red; when battery is low, color is red,
otherwise green).
Diffstat (limited to 'src')
-rw-r--r-- | src/common.c | 77 | ||||
-rw-r--r-- | src/common.h | 30 | ||||
-rw-r--r-- | src/config.c | 124 | ||||
-rw-r--r-- | src/config.h | 31 | ||||
-rw-r--r-- | src/config_bat.c | 88 | ||||
-rw-r--r-- | src/config_bat.h | 40 | ||||
-rw-r--r-- | src/config_cpu.c | 68 | ||||
-rw-r--r-- | src/config_cpu.h | 32 | ||||
-rw-r--r-- | src/config_date.c | 47 | ||||
-rw-r--r-- | src/config_date.h | 36 | ||||
-rw-r--r-- | src/config_fs.c | 63 | ||||
-rw-r--r-- | src/config_fs.h | 40 | ||||
-rw-r--r-- | src/config_mem.c | 68 | ||||
-rw-r--r-- | src/config_mem.h | 30 | ||||
-rw-r--r-- | src/config_net.c | 81 | ||||
-rw-r--r-- | src/config_net.h | 39 | ||||
-rw-r--r-- | src/config_node.c | 74 | ||||
-rw-r--r-- | src/config_node.h | 49 | ||||
-rw-r--r-- | src/config_time.c | 50 | ||||
-rw-r--r-- | src/config_time.h | 38 | ||||
-rw-r--r-- | src/main.c | 67 |
21 files changed, 1172 insertions, 0 deletions
diff --git a/src/common.c b/src/common.c new file mode 100644 index 0000000..6d5d540 --- /dev/null +++ b/src/common.c @@ -0,0 +1,77 @@ +/** + * I3cstatus prints a configurable status bar for the i3 window manager + * Copyright (C) 2020 Aaron Ball <nullspoon@oper.io> + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ +#include "common.h" +#include <stdio.h> + +int str_is_empty(char* str) { + int i = 0; + while(str[i] == ' ' || str[i] == '\t') + i++; + if(str[i] == '\0' || str[i] == '\n') + return 1; + return 0; +} + +char* trim(char* str) { + int i = 0; + char* start; + + // Move the cursor forward + while(str[i] == ' ' || str[i] == '\t') + i++; + start = &str[i]; + + // Reset i to end of string + i = strlen(str) - 1; + while(str[i] == ' ' || str[i] == '\t' || str[i] == '\n') + i--; + + if(str[i] != '\0') + str[i + 1] = '\0'; + return start; +} + +void print_bar(int width, float percent, char* buf) { + double fill = width * percent; + int i = 1; + int remainder = 0; + + buf[0] = '['; + + while(i < fill) { + buf[i] = ':'; + i++; + } + + remainder = (fill + 1 - i) * 10; + if(i > fill && remainder >= 5.0) { + buf[i] = ':'; + i++; + } else if(i > fill && remainder < 5.0 && remainder > 0) { + buf[i] = '.'; + i++; + } + + while(i < width) { + buf[i] = ' '; + i++; + } + + buf[i] = ']'; + buf[i+1] = '\0'; +} diff --git a/src/common.h b/src/common.h new file mode 100644 index 0000000..a49aaaa --- /dev/null +++ b/src/common.h @@ -0,0 +1,30 @@ +/** + * I3cstatus prints a configurable status bar for the i3 window manager + * Copyright (C) 2020 Aaron Ball <nullspoon@oper.io> + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ +#include <string.h> + +#define C_LGREY "#dddddd" +#define C_DGREY "#999999" +#define C_RED "#bf616a" +#define C_ORANGE "#eeaa77" +#define C_YELLOW "#dddd99" +#define C_GREEN "#aaddaa" +#define C_BLUE "#aaccee" + +int str_is_empty(char*); +char* trim(char*); +void print_bar(int, float, char*); diff --git a/src/config.c b/src/config.c new file mode 100644 index 0000000..51503c3 --- /dev/null +++ b/src/config.c @@ -0,0 +1,124 @@ +/** + * I3cstatus prints a configurable status bar for the i3 window manager + * Copyright (C) 2020 Aaron Ball <nullspoon@oper.io> + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ +#include "config.h" + +int _ini_read_header(char* line, char* out) { + int i = 0; + if(line[0] != '[') { + out = NULL; + return -1; + } + while(line[i] != ']' && line[i] != '\0') + i++; + + strncpy(out, &line[1], i - 1); + out[i-1] = '\0'; + return 0; +} + +int _ini_read_keyval(char* line, char* key, char* val) { + int i = 0; + char buf[256] = {'\0'}; + while(line[i] != '=') + i++; + + // Copy the key + strncpy(&buf[0], line, i); + buf[i] = '\0'; + strcpy(key, trim(buf)); + + // Copy the value + strcpy(val, trim(&line[i+1])); + return 0; +} + + +struct node* config_load(char* path) { + FILE* fd; + char buf[256] = {'\0'}; + char section[256] = {'\0'}; + char key[256] = {'\0'}; + char val[256] = {'\0'}; + fd = fopen(path, "r"); + + struct node* list = NULL; + struct node* cur = NULL; + + if(!fd) + return NULL; + + while(fgets(buf, 256, fd) != NULL) { + // Skip comments + if(buf[0] == '#') + continue; + + // If we've found a section + if(buf[0] == '[') { + _ini_read_header(buf, section); + + if(strcmp(section, "fs") == 0) { + cur = node_new(&config_fs_init); + + } else if(strcmp(section, "net") == 0) { + cur = node_new(&config_net_init); + + } else if(strcmp(section, "time") == 0) { + cur = node_new(&config_time_init); + + } else if(strcmp(section, "date") == 0) { + cur = node_new(&config_date_init); + + } else if(strcmp(section, "bat") == 0) { + cur = node_new(&config_bat_init); + + } else if(strcmp(section, "mem") == 0) { + cur = node_new(&config_mem_init); + + } else if(strcmp(section, "cpu") == 0) { + cur = node_new(&config_cpu_init); + } + + if(!list) + list = cur; + else + lladd(list, cur); + + continue; + } + + if(str_is_empty(buf)) + continue; + + _ini_read_keyval(buf, key, val); + + // Check if generic node key + if(strcmp(key, "label") == 0 + || strcmp(key, "text") == 0 + || strcmp(key, "color") == 0 + || strcmp(key, "label_color") == 0 + || strcmp(key, "display") == 0) { + _load_node_key(cur, key, val); + continue; + } + + cur->loadkey(cur, key, val); + } + + fclose(fd); + return list; +} diff --git a/src/config.h b/src/config.h new file mode 100644 index 0000000..8364ab5 --- /dev/null +++ b/src/config.h @@ -0,0 +1,31 @@ +/** + * I3cstatus prints a configurable status bar for the i3 window manager + * Copyright (C) 2020 Aaron Ball <nullspoon@oper.io> + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include "common.h" +#include "config_node.h" +#include "config_mem.h" +#include "config_cpu.h" +#include "config_net.h" +#include "config_date.h" +#include "config_time.h" +#include "config_fs.h" +#include "config_bat.h" + +struct node* config_load(char*); diff --git a/src/config_bat.c b/src/config_bat.c new file mode 100644 index 0000000..b25f1cc --- /dev/null +++ b/src/config_bat.c @@ -0,0 +1,88 @@ +/** + * I3cstatus prints a configurable status bar for the i3 window manager + * Copyright (C) 2020 Aaron Ball <nullspoon@oper.io> + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ +#include "config_bat.h" + +void config_bat_init(struct node* n) { + n->data = malloc(sizeof(struct config_bat)); + n->type = CTYPE_BAT; + n->loadfunc = &config_bat_load; + n->loadkey = &load_bat_key; +} + +long _fgetl(char* path) { + char buf[256] = {'\0'}; + FILE* fd; + + fd = fopen(path, "r"); + fgets(buf, 256, fd); + fclose(fd); + + return strtol(buf, NULL, 10); +} + +int _bat_is_charging(char* bat) { + char buf[256] = {'\0'}; + FILE* fd = fopen(bat, "r"); + fgets(buf, 256, fd); + fclose(fd); + + if(strncmp(buf, "Discharging", 11) == 0) + return 0; + return 1; +} + + +int config_bat_load(struct node* n) { + long level; + char batstatus[256]; + char batcapacity[256]; + + sprintf(batstatus, "/sys/class/power_supply/%s/status", ((struct config_bat*)n->data)->name); + sprintf(batcapacity, "/sys/class/power_supply/%s/capacity", ((struct config_bat*)n->data)->name); + + level = _fgetl(batcapacity); + + if(level > 70) { + strcpy(n->color, C_GREEN); + } else if(level > 50) { + strcpy(n->color, C_YELLOW); + } else if(level > 30) { + strcpy(n->color, C_ORANGE); + } else { + strcpy(n->color, C_RED); + } + + if(_bat_is_charging(batstatus) == 1) { + sprintf(n->text + strlen(n->text), "%lc", 0x26A1); + } else { + strcat(n->text, " "); + } + + if(strcmp(n->display, "bar") == 0) { + print_bar(12, (float) level / 100, &n->text[3]); + } else { + sprintf(n->text, "%02d%%", (int)(level / 100)); + } + + return 0; +} + +void load_bat_key(struct node* n, char* key, char* val) { + if(strcmp(key, "name") == 0) + strcpy(((struct config_bat*)n->data)->name, val); +} diff --git a/src/config_bat.h b/src/config_bat.h new file mode 100644 index 0000000..e310a98 --- /dev/null +++ b/src/config_bat.h @@ -0,0 +1,40 @@ +/** + * I3cstatus prints a configurable status bar for the i3 window manager + * Copyright (C) 2020 Aaron Ball <nullspoon@oper.io> + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ +#ifndef CONFIG_BAT +#define CONFIG_BAT + +#define CTYPE_BAT 50 + +#include <string.h> +#include "common.h" +#include "config_node.h" + +struct config_bat { + char name[64]; + int max; + int cur; +}; + + +void config_bat_init(struct node*); + +int config_bat_load(struct node*); + +void load_bat_key(struct node*, char*, char*); + +#endif diff --git a/src/config_cpu.c b/src/config_cpu.c new file mode 100644 index 0000000..c194893 --- /dev/null +++ b/src/config_cpu.c @@ -0,0 +1,68 @@ +/** + * I3cstatus prints a configurable status bar for the i3 window manager + * Copyright (C) 2020 Aaron Ball <nullspoon@oper.io> + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ +#include "config_cpu.h" + +void config_cpu_init(struct node* n) { + n->data = NULL; + n->type = CTYPE_CPU; + n->loadfunc = &config_cpu_load; + n->loadkey = &load_cpu_key; +} + +void load_cpu_key(struct node* n, char* key, char* val) { +} + +int config_cpu_load(struct node* n) { + double avgs[1] = {0}; + double level = 0; + int nproc = 0; + + nproc = get_nprocs(); + getloadavg(avgs, 1); + + if(n->label[0] == '\0') + strcpy(n->label, "cpu:"); + + // Get level + if(nproc < avgs[0]) { + level = 1; + } else { + level = avgs[0] / nproc; + } + + // Set color + if(level > .8) { + strcpy(n->color, C_RED); + } else if(level > .5) { + strcpy(n->color, C_ORANGE); + } else if(level > .3) { + strcpy(n->color, C_YELLOW); + } else { + strcpy(n->color, C_GREEN); + } + + if(strcmp(n->display, "bar") == 0) { + print_bar(12, (float) level, n->text); + } else { + // Default to percent display + sprintf(n->text, "%02d%%", (int)(level * 100)); + } + return 0; +} + + diff --git a/src/config_cpu.h b/src/config_cpu.h new file mode 100644 index 0000000..3d012e7 --- /dev/null +++ b/src/config_cpu.h @@ -0,0 +1,32 @@ +/** + * I3cstatus prints a configurable status bar for the i3 window manager + * Copyright (C) 2020 Aaron Ball <nullspoon@oper.io> + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ +#ifndef CONFIG_CPU +#define CONFIG_CPU + +#include <stdlib.h> +#include <sys/sysinfo.h> +#include "common.h" +#include "config_node.h" + +#define CTYPE_CPU 70 + +void config_cpu_init(struct node* n); +void load_cpu_key(struct node* n, char* key, char* val); +int config_cpu_load(struct node* n); + +#endif diff --git a/src/config_date.c b/src/config_date.c new file mode 100644 index 0000000..10a6a78 --- /dev/null +++ b/src/config_date.c @@ -0,0 +1,47 @@ +/** + * I3cstatus prints a configurable status bar for the i3 window manager + * Copyright (C) 2020 Aaron Ball <nullspoon@oper.io> + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ +#include "config_date.h" + +void config_date_init(struct node* n) { + n->data = malloc(sizeof(struct config_date)); + n->type = CTYPE_DATE; + n->loadfunc = &config_date_load; + n->loadkey = &load_date_key; +} + +void load_date_key(struct node* n, char* key, char* val) { + if(strcmp(key, "tz") == 0) + strcpy(((struct config_date*) n->data)->tz, val); + else + printf("ERROR: Unknown time key %s\n", key); +} + +int config_date_load(struct node* n) { + char buf[256]; + time_t timep; + struct tm* info; + + setenv("TZ", ((struct config_date*)n->data)->tz, 1); + time(&timep); + info = localtime(&timep); + + strftime(buf, 256, "%F", info); + strcpy(n->label, buf); + strcpy(n->color, C_LGREY); + return 0; +} diff --git a/src/config_date.h b/src/config_date.h new file mode 100644 index 0000000..b6dd0c2 --- /dev/null +++ b/src/config_date.h @@ -0,0 +1,36 @@ +/** + * I3cstatus prints a configurable status bar for the i3 window manager + * Copyright (C) 2020 Aaron Ball <nullspoon@oper.io> + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ +#ifndef CONFIG_DATE +#define CONFIG_DATE + +#define CTYPE_DATE 40 + +#include <stdlib.h> +#include <time.h> +#include "common.h" +#include "config_node.h" + +struct config_date { + char tz[64]; +}; + +void config_date_init(struct node*); +void load_date_key(struct node*, char*, char*); +int config_date_load(struct node*); + +#endif diff --git a/src/config_fs.c b/src/config_fs.c new file mode 100644 index 0000000..45ccd46 --- /dev/null +++ b/src/config_fs.c @@ -0,0 +1,63 @@ +/** + * I3cstatus prints a configurable status bar for the i3 window manager + * Copyright (C) 2020 Aaron Ball <nullspoon@oper.io> + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ +#include "config_fs.h" + +void config_fs_init(struct node* n) { + n->data = malloc(sizeof(struct config_fs)); + n->type = CTYPE_FS; + n->loadfunc = &config_fs_load; + n->loadkey = &load_fs_key; +} + +void load_fs_key(struct node* n, char* key, char* val) { + struct config_fs* data = (struct config_fs*) n->data; + + if(strcmp(key, "path") == 0) + strcpy(data->path, val); + else + printf("ERROR: Unknown fs key %s\n", key); +} + +int config_fs_load(struct node* n) { + struct statfs fs; + int percent; + unsigned long used; + + statfs(((struct config_fs*)n->data)->path, &fs); + + used = (unsigned long) fs.f_blocks - (unsigned long) fs.f_bfree; + percent = ((long double) used * 100) / (long double)fs.f_blocks; + + if(percent > 80) { + strcpy(n->color, C_RED); + } else if(percent > 50) { + strcpy(n->color, C_ORANGE); + } else if(percent > 30) { + strcpy(n->color, C_YELLOW); + } else { + strcpy(n->color, C_GREEN); + } + + if(strcmp(n->display, "bar") == 0) { + print_bar(10, ((long double) used) / (long double)fs.f_blocks, n->text); + } else { + sprintf(n->text, "%d%%", percent); + } + sprintf(n->label, "%s:", ((struct config_fs*)n->data)->path); + return 0; +} diff --git a/src/config_fs.h b/src/config_fs.h new file mode 100644 index 0000000..8633ce9 --- /dev/null +++ b/src/config_fs.h @@ -0,0 +1,40 @@ +/** + * I3cstatus prints a configurable status bar for the i3 window manager + * Copyright (C) 2020 Aaron Ball <nullspoon@oper.io> + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ +#ifndef CONFIG_FS +#define CONFIG_FS + +#define CTYPE_FS 20 + +#include <stdio.h> +#include <string.h> +#include <sys/vfs.h> +#include "common.h" +#include "config_node.h" + +struct config_fs { + char path[128]; + char label[128]; +}; + +void config_fs_init(struct node*); + +int config_fs_load(struct node*); + +void load_fs_key(struct node*, char*, char*); + +#endif diff --git a/src/config_mem.c b/src/config_mem.c new file mode 100644 index 0000000..699e22e --- /dev/null +++ b/src/config_mem.c @@ -0,0 +1,68 @@ +/** + * I3cstatus prints a configurable status bar for the i3 window manager + * Copyright (C) 2020 Aaron Ball <nullspoon@oper.io> + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ +#include "config_mem.h" + +void config_mem_init(struct node* n) { + n->data = NULL; + n->type = CTYPE_MEM; + n->loadfunc = &config_mem_load; + n->loadkey = &load_mem_key; +} + +void load_mem_key(struct node* n, char* key, char* val) { +} + +int config_mem_load(struct node* n) { + long total; + long free; + long available; + + int percent; + long used; + + // See https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=34e431b0ae398fc54ea69ff85ec700722c9da773 + FILE* fd = fopen("/proc/meminfo", "r"); + fscanf(fd, "MemTotal: %ld kB ", &total); + fscanf(fd, "MemFree: %ld kB ", &free); + fscanf(fd, "MemAvailable: %ld kB ", &available); + fclose(fd); + + used = total - available; + percent = used * 100 / total; + + if(percent < 20) { + strcpy(n->color, C_GREEN); + } else if(percent < 40) { + strcpy(n->color, C_YELLOW); + } else if(percent < 60) { + strcpy(n->color, C_ORANGE); + } else { + strcpy(n->color, C_RED); + } + strcpy(n->label_color, C_LGREY); + + if(strcmp(n->display, "bar") == 0) { + print_bar(12, (double)used / total, n->text); + } else { + sprintf(n->text, "%02d%%", percent); + } + + return 0; +} + + diff --git a/src/config_mem.h b/src/config_mem.h new file mode 100644 index 0000000..77e5021 --- /dev/null +++ b/src/config_mem.h @@ -0,0 +1,30 @@ +/** + * I3cstatus prints a configurable status bar for the i3 window manager + * Copyright (C) 2020 Aaron Ball <nullspoon@oper.io> + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ +#ifndef CONFIG_MEM +#define CONFIG_MEM + +#include "common.h" +#include "config_node.h" + +#define CTYPE_MEM 60 + +void config_mem_init(struct node*); +int config_mem_load(struct node*); +void load_mem_key(struct node*, char*, char*); + +#endif diff --git a/src/config_net.c b/src/config_net.c new file mode 100644 index 0000000..fe600d7 --- /dev/null +++ b/src/config_net.c @@ -0,0 +1,81 @@ +/** + * I3cstatus prints a configurable status bar for the i3 window manager + * Copyright (C) 2020 Aaron Ball <nullspoon@oper.io> + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ +#include "config_net.h" + +void config_net_init(struct node* n) { + n->data = malloc(sizeof(struct config_net)); + n->type = CTYPE_NET; + n->loadfunc = &config_net_load; + n->loadkey = &load_net_key; +} + + +void load_net_key(struct node* n, char* key, char* val) { + struct config_net* data = (struct config_net*) n->data; + + if(strcmp(key, "ifname") == 0) + strcpy(data->ifname, val); + else + printf("ERROR: Unknown net key %s\n", key); +} + + +int config_net_load(struct node* n) { + struct ifaddrs *addrs,*tmp; + char host[224] = "--"; + strcpy(n->color, C_RED); + + getifaddrs(&addrs); + tmp = addrs; + + while (tmp) { + if(strcmp(tmp->ifa_name, ((struct config_net*)n->data)->ifname) == 0) { + // Skip if no address is present + if(! tmp->ifa_addr) { + tmp = tmp->ifa_next; + continue; + } + + // Ensure sa_family is type 2 (17 doesn't have an IP) + if(tmp->ifa_addr->sa_family != 2) { + tmp = tmp->ifa_next; + continue; + } + + // Get the device IP address + getnameinfo( + tmp->ifa_addr, + sizeof(struct sockaddr_in), + host, + NI_MAXHOST, + NULL, + 0, + NI_NUMERICHOST); + + strcpy(n->color, C_GREEN); + break; + } + tmp = tmp->ifa_next; + } + freeifaddrs(addrs); + + strcpy(n->label_color, C_LGREY); + strcpy(n->text, host); + return 0; +} + diff --git a/src/config_net.h b/src/config_net.h new file mode 100644 index 0000000..8d971fc --- /dev/null +++ b/src/config_net.h @@ -0,0 +1,39 @@ +/** + * I3cstatus prints a configurable status bar for the i3 window manager + * Copyright (C) 2020 Aaron Ball <nullspoon@oper.io> + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ +#ifndef CONFIG_NET +#define CONFIG_NET + +#include <stdio.h> +#include <string.h> +#include <ifaddrs.h> +#include <netdb.h> + +#include "common.h" +#include "config_node.h" + +#define CTYPE_NET 10 + +struct config_net { + char ifname[32]; +}; + +void config_net_init(struct node*); +int config_net_load(struct node*); +void load_net_key(struct node*, char*, char*); + +#endif diff --git a/src/config_node.c b/src/config_node.c new file mode 100644 index 0000000..dcccc00 --- /dev/null +++ b/src/config_node.c @@ -0,0 +1,74 @@ +/** + * I3cstatus prints a configurable status bar for the i3 window manager + * Copyright (C) 2020 Aaron Ball <nullspoon@oper.io> + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ +#include "config_node.h" + + +struct node* node_new(void (*init_callback)(struct node*)) { + struct node* out = malloc(sizeof(struct node)); + out->data = NULL; + out->next = NULL; + out->display[0] = '\0'; + out->text[0] = '\0'; + out->label[0] = '\0'; + strcpy(out->color, "#eeeeee"); + strcpy(out->label_color, "#eeeeee"); + init_callback(out); + return out; +} + + +struct node* lladd(struct node* list, struct node* next) { + while(list->next) + list = list->next; + list->next = next; + return next; +} + +void llfree(struct node* list) { + struct node* next = list; + while(next) { + next = list->next; + free(list); + } +} + +void _load_node_key(struct node* n, char* key, char* val) { + if(strcmp(key, "color") == 0) + strcpy(n->color, val); + else if(strcmp(key, "label_color") == 0) + strcpy(n->label_color, val); + else if(strcmp(key, "label") == 0) + strcpy(n->label, val); + else if(strcmp(key, "text") == 0) + strcpy(n->text, val); + else if(strcmp(key, "display") == 0) + strcpy(n->display, val); + else + printf("ERROR: Unknown node key %s\n", key); +} + +void node_print(struct node* n) { + printf("{ \"full_text\":\" %s\", \"color\": \"%s\", \"separator\":false },", + n->label, + n->label_color); + printf("{ \"full_text\":\" %s \", \"color\":\"%s\" }", n->text, n->color); + if(n->next) + printf(","); + printf("\n"); + fflush(stdout); +} diff --git a/src/config_node.h b/src/config_node.h new file mode 100644 index 0000000..520c533 --- /dev/null +++ b/src/config_node.h @@ -0,0 +1,49 @@ +/** + * I3cstatus prints a configurable status bar for the i3 window manager + * Copyright (C) 2020 Aaron Ball <nullspoon@oper.io> + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ +#ifndef CONFIG_NODE +#define CONFIG_NODE + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +struct node { + struct node* next; + int type; + void* data; // Additional data for the specific node type + int (*loadfunc)(struct node*); // callback function so modules can manipulate their node entity + void (*loadkey)(struct node*, char*, char*); // callback function to load a type-specific ini key + char color[8]; // Color to print + char label_color[8]; // Color to print + char label[256]; // Label for the node + char text[256]; // Value for full_text + char display[64]; // How to display the data (eg: bar, percent, etc) +}; + +//struct node* config_node_new(void* data, void(*init_callback)(struct node*)) { +struct node* node_new(void (*init_callback)(struct node*)); + +struct node* lladd(struct node*, struct node*); + +void llfree(struct node*); + +void _load_node_key(struct node*, char*, char*); + +void node_print(struct node*); + +#endif diff --git a/src/config_time.c b/src/config_time.c new file mode 100644 index 0000000..e7c3866 --- /dev/null +++ b/src/config_time.c @@ -0,0 +1,50 @@ +/** + * I3cstatus prints a configurable status bar for the i3 window manager + * Copyright (C) 2020 Aaron Ball <nullspoon@oper.io> + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ +#include "config_time.h" + +void config_time_init(struct node* n) { + n->data = malloc(sizeof(struct config_time)); + n->type = CTYPE_TIME; + n->loadfunc = &config_time_load; + n->loadkey = &load_time_key; +} + + +void load_time_key(struct node* n, char* key, char* val) { + if(strcmp(key, "tz") == 0) + strcpy(((struct config_time*) n->data)->tz, val); + else + printf("ERROR: Unknown time key %s\n", key); +} + + +int config_time_load(struct node* n) { + time_t timep; + struct tm* info; + + setenv("TZ", ((struct config_time*)n->data)->tz, 1); + time(&timep); + info = localtime(&timep); + + strftime(n->text, 256, "%T", info); + + strcpy(n->color, C_DGREY); + strcpy(n->label_color, C_LGREY); + + return 0; +} diff --git a/src/config_time.h b/src/config_time.h new file mode 100644 index 0000000..624dd84 --- /dev/null +++ b/src/config_time.h @@ -0,0 +1,38 @@ +/** + * I3cstatus prints a configurable status bar for the i3 window manager + * Copyright (C) 2020 Aaron Ball <nullspoon@oper.io> + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ +#ifndef CONFIG_TIME +#define CONFIG_TIME + +#define CTYPE_TIME 30 + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <time.h> +#include "common.h" +#include "config_node.h" + +struct config_time { + char tz[256]; +}; + +void config_time_init(struct node*); +void load_time_key(struct node*, char*, char*); +int config_time_load(struct node*); + +#endif diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..64d2ff4 --- /dev/null +++ b/src/main.c @@ -0,0 +1,67 @@ +/** + * I3cstatus prints a configurable status bar for the i3 window manager + * Copyright (C) 2020 Aaron Ball <nullspoon@oper.io> + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> +#include <string.h> + +#include <locale.h> + +#include "common.h" +#include "config.h" + +#define xstr(s) str(s) +#define str(s) #s + + +int main(int argc, char* argv[]) { + char confpath[256]; + int sleeptime = 500 * 1000; // 200 ms * 1000 + struct node* list = NULL; + struct node* cur = NULL; + + // Required for unicode symbols to work + setlocale(LC_ALL, ""); + + // Build the config path + sprintf(confpath, "%s/.config/i3cstat.conf", getenv("HOME")); + + list = config_load(confpath); + if(!list) { + printf("Error: Could load config file from %s\n", confpath); + return 2; + } + cur = list; + + printf("{ \"version\": 1 }\n"); + printf("[\n[],\n"); + + while(1) { + printf("[\n"); + while(cur) { + cur->loadfunc(cur); + node_print(cur); + cur = cur->next; + } + cur = list; + printf("],\n"); + usleep(sleeptime); + } + + return 0; +} |