From 287a96875b1df5166107f8edcf968c4cafdf2440 Mon Sep 17 00:00:00 2001 From: randomuser Date: Tue, 20 Sep 2022 18:08:37 -0500 Subject: [PATCH] add battery module --- battery.c | 38 ++++++++++++++++++++++++++++++++++++++ battery.h | 19 +++++++++++++++++++ tstatus.c | 9 ++++++++- 3 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 battery.c create mode 100644 battery.h diff --git a/battery.c b/battery.c new file mode 100644 index 0000000..b4da1b5 --- /dev/null +++ b/battery.c @@ -0,0 +1,38 @@ +/* see LICENSE file for details on license */ +#include +#include + +#include "module.h" +#include "battery.h" + +void populate_buffer(char *buffer, char pattern) { + /* XXX: we assume the buffer is at least four bytes long */ + + int i; + for(i = 0; i < 3; i++) *(buffer + i) = pattern; + *(buffer + i + 1) = '\0'; +} + + +int battery_update(struct module *module) { + char *filepath = BATTERY_PRE BATTERY_DIR BATTERY_CAP; + int i; + + if (!access(filepath, F_OK) == 0) { + populate_buffer((char *)&module->buffer, '?'); + return 1; + } + + FILE *file = fopen(filepath, "r"); + i = fread(&module->buffer, 1, MODULE_BUFFER_LEN, file); + module->buffer[i - 1] = '\0'; + + if(ferror(file)) { + populate_buffer((char *)&module->buffer, '!'); + return 1; + } + + module->buffer[i] = '\0'; + + return 0; +} diff --git a/battery.h b/battery.h new file mode 100644 index 0000000..3b153f3 --- /dev/null +++ b/battery.h @@ -0,0 +1,19 @@ +#ifndef TSTATUS_BATTERY_H + +#ifdef FISH +#define BATTERY_DIR "sbs-20-000b" +#elif TOASTER +#define BATTERY_DIR "BAT0" +#else +/* just assume we're running on fish */ +#define BATTERY_DIR "sbs-20-000b" +#endif + +#define BATTERY_PRE "/sys/class/power_supply/" +#define BATTERY_CAP "/capacity" + +void populate_buffer(char *buffer, char pattern); +int battery_module(struct module *module); + +#endif +#define TSTATUS_BATTERY_H diff --git a/tstatus.c b/tstatus.c index 66941d7..49d9f95 100644 --- a/tstatus.c +++ b/tstatus.c @@ -1,14 +1,21 @@ /* see LICENSE file for details on license */ -#include "bspwm.c" +#include + #include "module.h" +#include "bspwm.c" +#include "battery.c" struct module table[] = { {0, 10, bspwm_update, {'\0'}}, + {0, 10, battery_update, {'\0'}}, }; int main(void) { table[0].updatecallback(&table[0]); printf("%s\n", table[0].buffer); + table[1].updatecallback(&table[1]); + printf("%s\n", table[1].buffer); + return 0; }