From e76bafa482037d0a83c227852460d9ef9019108c Mon Sep 17 00:00:00 2001 From: randomuser Date: Wed, 21 Sep 2022 12:50:10 -0500 Subject: [PATCH] add thermal module --- battery.c | 29 ++--------------------------- battery.h | 1 - file.c | 37 +++++++++++++++++++++++++++++++++++++ file.h | 8 ++++++++ module.h | 2 +- thermal.c | 27 +++++++++++++++++++++++++++ thermal.h | 18 ++++++++++++++++++ tstatus.c | 5 +++++ 8 files changed, 98 insertions(+), 29 deletions(-) create mode 100644 file.c create mode 100644 file.h create mode 100644 thermal.c create mode 100644 thermal.h diff --git a/battery.c b/battery.c index b4da1b5..1718fff 100644 --- a/battery.c +++ b/battery.c @@ -3,36 +3,11 @@ #include #include "module.h" +#include "file.c" #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; + return read_file_into_buffer(filepath, (char *)&module->buffer, MODULE_BUFFER_LEN); } diff --git a/battery.h b/battery.h index 3b153f3..b6c2c67 100644 --- a/battery.h +++ b/battery.h @@ -12,7 +12,6 @@ #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 diff --git a/file.c b/file.c new file mode 100644 index 0000000..2f8597c --- /dev/null +++ b/file.c @@ -0,0 +1,37 @@ +/* see LICENSE file for details on license */ +#ifndef TSTATUS_FILE_C + +#include "file.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 read_file_into_buffer(char *filename, char *buffer, int length) { + int i; + if (!access(filename, F_OK) == 0) { + populate_buffer(buffer, '?'); + return -1; + } + + FILE *file = fopen(filename, "r"); + i = fread(buffer, 1, length, file); + *(buffer + i - 1) = '\0'; + + if(ferror(file)) { + populate_buffer(buffer, '!'); + return -1; + } + + fclose(file); + + *(buffer + i) = '\0'; + + return 0; +} +#endif +#define TSTATUS_FILE_C diff --git a/file.h b/file.h new file mode 100644 index 0000000..f8a3e55 --- /dev/null +++ b/file.h @@ -0,0 +1,8 @@ +/* see LICENSE file for details on license */ +#ifndef TSTATUS_FILE_H + +void populate_buffer(char *buffer, char pattern); +int read_file_into_buffer(char *filename, char *buffer, ssize_t length); + +#endif +#define TSTATUS_FILE_H diff --git a/module.h b/module.h index e228079..1f80ad0 100644 --- a/module.h +++ b/module.h @@ -1,6 +1,6 @@ /* see LICENSE file for details on license */ #ifndef TSTATUS_MODULE_H -#define MODULE_BUFFER_LEN 64 +#define MODULE_BUFFER_LEN 128 struct module { int update; /* update interval in seconds */ int termcode; /* update termcode */ diff --git a/thermal.c b/thermal.c new file mode 100644 index 0000000..4f1999f --- /dev/null +++ b/thermal.c @@ -0,0 +1,27 @@ +/* see LICENSE file for details on license */ +#include +#include + +#include "module.h" +#include "file.c" +#include "thermal.h" + +int convert_milli_to_reg(int millidegree) { + return millidegree / 1000; +} + +int thermal_update(struct module *module) { + char *filepath = THERMAL_PRE THERMAL_DIR THERMAL_TMP; + int i; + + i = read_file_into_buffer(filepath, (char *)&module->buffer, MODULE_BUFFER_LEN); + if(i == -1) return i; /* indicate an error */ + + i = atoi((char *)&module->buffer); + if(!i) return -1; + + i = convert_milli_to_reg(i); + snprintf((char *)&module->buffer, MODULE_BUFFER_LEN, "%i", i); + + return 0; +} diff --git a/thermal.h b/thermal.h new file mode 100644 index 0000000..225f4dc --- /dev/null +++ b/thermal.h @@ -0,0 +1,18 @@ +/* see LICENSE file for details on license */ +#ifndef TSTATUS_THERMAL_H + +#ifdef FISH +#define THERMAL_DIR "thermal_zone1" +/* you can add other computer hostnames here */ +#else +#define THERMAL_DIR "thermal_zone1" +#endif + +#define THERMAL_PRE "/sys/class/thermal/" +#define THERMAL_TMP "/temp" + +int convert_milli_to_reg(int millidegree); +int thermal_update(struct module *module); + +#endif +#define TSTATUS_THERMAL_H diff --git a/tstatus.c b/tstatus.c index 49d9f95..2f4afca 100644 --- a/tstatus.c +++ b/tstatus.c @@ -4,10 +4,12 @@ #include "module.h" #include "bspwm.c" #include "battery.c" +#include "thermal.c" struct module table[] = { {0, 10, bspwm_update, {'\0'}}, {0, 10, battery_update, {'\0'}}, + {0, 10, thermal_update, {'\0'}}, }; int main(void) { @@ -17,5 +19,8 @@ int main(void) { table[1].updatecallback(&table[1]); printf("%s\n", table[1].buffer); + table[2].updatecallback(&table[2]); + printf("%s\n", table[2].buffer); + return 0; }