From fcfb3bb4a17c916faf5da1ddf9aaba301bd28696 Mon Sep 17 00:00:00 2001 From: Sandy Mossgrave Date: Sun, 29 Sep 2024 13:34:16 -0700 Subject: [PATCH] Added ability to search for /sys/class.../energy_now and energy_full for battery status, to accommodate for my other laptop. --- Makefile | 2 +- sources/include/main.h | 8 +++-- sources/main/battery.c | 76 +++++++++++++++++++++++++++++++++--------- 3 files changed, 68 insertions(+), 18 deletions(-) diff --git a/Makefile b/Makefile index 2160fca..b9c6736 100644 --- a/Makefile +++ b/Makefile @@ -42,7 +42,7 @@ SOURCES := $(wildcard $(SRCDIR)/*.c) OBJECTS := $(patsubst $(SRCDIR)/%.c, $(OBJDIR)/%.o, $(SOURCES)) CPPFLAGS := -I$(INCDIR) -MMD -MP -DUSE_LIBNOTIFY -CFLAGS := -Wall -Werror -Wpedantic $(shell pkg-config --cflags libnotify) +CFLAGS := -Wall -Werror -Wpedantic $(shell pkg-config --cflags libnotify) -g LDFLAGS := #-Lmath or whatever LDLIBS := $(shell pkg-config --libs libnotify) #-lm or whatever diff --git a/sources/include/main.h b/sources/include/main.h index c7de2d9..f1b4f06 100644 --- a/sources/include/main.h +++ b/sources/include/main.h @@ -5,8 +5,12 @@ #define DEFAULT_LOG_INTERVAL 15 #define DEFAULT_LOG_INTERVAL_STRING "15" -#define CHARGE_NOW_PATH "/sys/class/power_supply/BAT0/charge_now" -#define CHARGE_FULL_PATH "/sys/class/power_supply/BAT0/charge_full" +#define BAT_PATH "/sys/class/power_supply/BAT1/" + +#define CHARGE_NOW_PATH "charge_now" +#define CHARGE_FULL_PATH "charge_full" +#define ENERGY_NOW_PATH "energy_now" +#define ENERGY_FULL_PATH "energy_full" #define VERSION "v0.2" #define TITLE "Battery Percentage, by Catluck Kettlemerry" #define PROGNAME "battery" diff --git a/sources/main/battery.c b/sources/main/battery.c index ef6faaf..2758de9 100644 --- a/sources/main/battery.c +++ b/sources/main/battery.c @@ -35,10 +35,45 @@ void print_help(void){ OPTIONS10); } -int get_percentage -(FILE * fd_now, FILE * fd_full, int * c_now, int * c_full, float * frac, int *pct) +int get_fds(FILE ** fd_now, FILE ** fd_full, const char * bat_path, const char * unit_now, const char * unit_full) { - int vals_read = fscanf(fd_now, "%d", c_now); + size_t bat_path_len = strlen(bat_path); + size_t unit_now_len = strlen(unit_now); + size_t unit_full_len = strlen(unit_full); + char * now_path = (char*)malloc(bat_path_len + unit_now_len + 1); + char * full_path = (char*)malloc(bat_path_len + unit_full_len + 1); + + strncpy(now_path, bat_path, bat_path_len + 1); + strncpy(full_path, bat_path, bat_path_len + 1); + + strncat(now_path, unit_now, unit_now_len); + strncat(full_path, unit_full, unit_full_len); + + printf("Attempting to open:\n\t%s\n\t%s\n", now_path, full_path); + + (*fd_now) = fopen(now_path, "r"); + if(*fd_now == NULL){ + perror("Error opening file for current charge level"); + // if exiting here, no need to close fd_now because it is null + return 1; + } + (*fd_full) = fopen(full_path, "r"); + if(*fd_full == NULL){ + perror("Error opening file for maximum charge level"); + // it just makes me feel safer, but this should be redundant + if((*fd_now) != NULL){ + fclose(*fd_now); + } + return 1; + } + printf("Successfully found files at %p and %p\n", (void*)fd_now, (void*)fd_full); + return 0; +} + +int get_percentage +(FILE * fd_now, FILE * fd_full, double * c_now, double * c_full, double * frac, int *pct) +{ + int vals_read = fscanf(fd_now, "%lf", c_now); if(vals_read != 1){ if(errno != 0){ perror("Error reading full battery charge: "); @@ -49,7 +84,7 @@ int get_percentage } return 1; } - vals_read = fscanf(fd_full, "%d", c_full); + vals_read = fscanf(fd_full, "%lf", c_full); if(vals_read != 1){ if(errno != 0){ perror("Error reading full battery charge: "); @@ -60,7 +95,7 @@ int get_percentage } return 1; } - (*frac) = ((float)(*c_now)) / ((float)(*c_full)); + (*frac) = *c_now / *c_full; (*pct) = (int)((*frac) * 100); return 0; } @@ -155,12 +190,23 @@ int main(int argc, char *const argv[]){ openlog(PROGNAME, 0, LOG_USER); #endif - FILE * fd_now = fopen(CHARGE_NOW_PATH, "r"); - FILE * fd_full = fopen(CHARGE_FULL_PATH, "r"); + FILE * fd_now; + FILE * fd_full; - int c_now; - int c_full; - float frac; + +if(get_fds(&fd_now, &fd_full, BAT_PATH, CHARGE_NOW_PATH, CHARGE_FULL_PATH)){ + printf("Charge unavailable, attempting to use energy\n"); +if(get_fds(&fd_now, &fd_full, BAT_PATH, ENERGY_NOW_PATH, ENERGY_FULL_PATH)){ + printf("Could not find a charge unit that works.\n"); + exit(1); +} else { + printf("Unit is energy"); +} +} + fflush(stdout); + double c_now; + double c_full; + double frac; int pct; do{ @@ -170,8 +216,8 @@ int main(int argc, char *const argv[]){ return 1; } if(run_verbose){ - printf("Current charge: %d\n microampere-hours\n", c_now); - printf("Full charge: %d\n microampere-hours\n", c_full); + printf("Current charge: %lf\n microampere-hours\n", c_now); + printf("Full charge: %lf\n microampere-hours\n", c_full); } if(!run_terse) { @@ -190,7 +236,7 @@ int main(int argc, char *const argv[]){ syslog(LOG_INFO, "%d%%%s", pct, delimiter); #endif if(run_continuously){ - sleep(interval);/* + sleep(interval); errno = 0; rewind(fd_now); if(errno != 0){ @@ -201,11 +247,11 @@ int main(int argc, char *const argv[]){ if(errno != 0){ perror("Error rewinding " CHARGE_FULL_PATH); return 1; - }*/ + }/* fclose(fd_now); fclose(fd_full); fd_now = fopen(CHARGE_NOW_PATH, "r"); - fd_full = fopen(CHARGE_FULL_PATH, "r"); + fd_full = fopen(CHARGE_FULL_PATH, "r");*/ } } while (run_continuously);