From b4715770db0f725841c772be48e36f6a0cd952f8 Mon Sep 17 00:00:00 2001 From: Squibid Date: Tue, 23 Sep 2025 02:20:07 -0400 Subject: [PATCH] seperate out the battery funcs --- include/battery.h | 6 ++++++ src/battery.c | 54 +++++++++++++++++++++++++++++++++++++++++++++++ src/main.c | 48 +---------------------------------------- 3 files changed, 61 insertions(+), 47 deletions(-) create mode 100644 include/battery.h create mode 100644 src/battery.c diff --git a/include/battery.h b/include/battery.h new file mode 100644 index 0000000..9fb259c --- /dev/null +++ b/include/battery.h @@ -0,0 +1,6 @@ +#pragma once + +#include + +char *battery_percent(void); +bool plugged_in(char *fn); diff --git a/src/battery.c b/src/battery.c new file mode 100644 index 0000000..19c5f98 --- /dev/null +++ b/src/battery.c @@ -0,0 +1,54 @@ +#include +#include +#include +#include + +#include "utils.h" + +#include "config.h" + +char +*battery_percent(void) +{ + char *r, c, *d; + int i; + FILE *f; + + /* open up the battery capacity for reading */ + d = concat(battery_dir, "/capacity"); + f = fopen(d, "r"); + free(d); + + /* create enough space for the battery percentage */ + r = calloc(4, sizeof(char)); + + /* read in the battery percentage */ + i = 0; + while ((c = getc(f)) && i < 4) { + /* make sure to replace the newline with a string terminator */ + if (c == '\n') { + r[i] = '\0'; + break; + } + r[i] = c; + i++; + } + + fclose(f); + return r; +} + +bool +plugged_in(char *fn) +{ + FILE *f; + char buf[1]; + + f = fopen(fn, "r"); + if (read(fileno(f), buf, 1) < 0) { + return -1; + } + fclose(f); + + return buf[0] - '0'; +} diff --git a/src/main.c b/src/main.c index 4a01961..ee04163 100644 --- a/src/main.c +++ b/src/main.c @@ -1,6 +1,5 @@ #include #include -#include #include #include #include @@ -17,57 +16,12 @@ #include "wayland.h" #include "acpi.h" #include "utils.h" +#include "battery.h" #include "config.h" #define MAX_BUFLEN 1024 -char -*battery_percent(void) -{ - char *r, c, *d; - int i; - FILE *f; - - /* open up the battery capacity for reading */ - d = concat(battery_dir, "/capacity"); - f = fopen(d, "r"); - free(d); - - /* create enough space for the battery percentage */ - r = calloc(4, sizeof(char)); - - /* read in the battery percentage */ - i = 0; - while ((c = getc(f)) && i < 4) { - /* make sure to replace the newline with a string terminator */ - if (c == '\n') { - r[i] = '\0'; - break; - } - r[i] = c; - i++; - } - - fclose(f); - return r; -} - -bool -plugged_in(char *fn) -{ - FILE *f; - char buf[1]; - - f = fopen(fn, "r"); - if (read(fileno(f), buf, 1) < 0) { - return -1; - } - fclose(f); - - return buf[0] - '0'; -} - void on_battery_event(char *percent, bool plugged) {