Added ability to search for /sys/class.../energy_now and energy_full for battery status, to accommodate for my other laptop.
This commit is contained in:
parent
15a20da1aa
commit
fcfb3bb4a1
2
Makefile
2
Makefile
@ -42,7 +42,7 @@ SOURCES := $(wildcard $(SRCDIR)/*.c)
|
|||||||
OBJECTS := $(patsubst $(SRCDIR)/%.c, $(OBJDIR)/%.o, $(SOURCES))
|
OBJECTS := $(patsubst $(SRCDIR)/%.c, $(OBJDIR)/%.o, $(SOURCES))
|
||||||
|
|
||||||
CPPFLAGS := -I$(INCDIR) -MMD -MP -DUSE_LIBNOTIFY
|
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
|
LDFLAGS := #-Lmath or whatever
|
||||||
LDLIBS := $(shell pkg-config --libs libnotify) #-lm or whatever
|
LDLIBS := $(shell pkg-config --libs libnotify) #-lm or whatever
|
||||||
|
|
||||||
|
@ -5,8 +5,12 @@
|
|||||||
#define DEFAULT_LOG_INTERVAL 15
|
#define DEFAULT_LOG_INTERVAL 15
|
||||||
#define DEFAULT_LOG_INTERVAL_STRING "15"
|
#define DEFAULT_LOG_INTERVAL_STRING "15"
|
||||||
|
|
||||||
#define CHARGE_NOW_PATH "/sys/class/power_supply/BAT0/charge_now"
|
#define BAT_PATH "/sys/class/power_supply/BAT1/"
|
||||||
#define CHARGE_FULL_PATH "/sys/class/power_supply/BAT0/charge_full"
|
|
||||||
|
#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 VERSION "v0.2"
|
||||||
#define TITLE "Battery Percentage, by Catluck Kettlemerry"
|
#define TITLE "Battery Percentage, by Catluck Kettlemerry"
|
||||||
#define PROGNAME "battery"
|
#define PROGNAME "battery"
|
||||||
|
@ -35,10 +35,45 @@ void print_help(void){
|
|||||||
OPTIONS10);
|
OPTIONS10);
|
||||||
}
|
}
|
||||||
|
|
||||||
int get_percentage
|
int get_fds(FILE ** fd_now, FILE ** fd_full, const char * bat_path, const char * unit_now, const char * unit_full)
|
||||||
(FILE * fd_now, FILE * fd_full, int * c_now, int * c_full, float * frac, int *pct)
|
|
||||||
{
|
{
|
||||||
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(vals_read != 1){
|
||||||
if(errno != 0){
|
if(errno != 0){
|
||||||
perror("Error reading full battery charge: ");
|
perror("Error reading full battery charge: ");
|
||||||
@ -49,7 +84,7 @@ int get_percentage
|
|||||||
}
|
}
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
vals_read = fscanf(fd_full, "%d", c_full);
|
vals_read = fscanf(fd_full, "%lf", c_full);
|
||||||
if(vals_read != 1){
|
if(vals_read != 1){
|
||||||
if(errno != 0){
|
if(errno != 0){
|
||||||
perror("Error reading full battery charge: ");
|
perror("Error reading full battery charge: ");
|
||||||
@ -60,7 +95,7 @@ int get_percentage
|
|||||||
}
|
}
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
(*frac) = ((float)(*c_now)) / ((float)(*c_full));
|
(*frac) = *c_now / *c_full;
|
||||||
(*pct) = (int)((*frac) * 100);
|
(*pct) = (int)((*frac) * 100);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@ -155,12 +190,23 @@ int main(int argc, char *const argv[]){
|
|||||||
openlog(PROGNAME, 0, LOG_USER);
|
openlog(PROGNAME, 0, LOG_USER);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
FILE * fd_now = fopen(CHARGE_NOW_PATH, "r");
|
FILE * fd_now;
|
||||||
FILE * fd_full = fopen(CHARGE_FULL_PATH, "r");
|
FILE * fd_full;
|
||||||
|
|
||||||
int c_now;
|
|
||||||
int c_full;
|
if(get_fds(&fd_now, &fd_full, BAT_PATH, CHARGE_NOW_PATH, CHARGE_FULL_PATH)){
|
||||||
float frac;
|
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;
|
int pct;
|
||||||
|
|
||||||
do{
|
do{
|
||||||
@ -170,8 +216,8 @@ int main(int argc, char *const argv[]){
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
if(run_verbose){
|
if(run_verbose){
|
||||||
printf("Current charge: %d\n microampere-hours\n", c_now);
|
printf("Current charge: %lf\n microampere-hours\n", c_now);
|
||||||
printf("Full charge: %d\n microampere-hours\n", c_full);
|
printf("Full charge: %lf\n microampere-hours\n", c_full);
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!run_terse) {
|
if(!run_terse) {
|
||||||
@ -190,7 +236,7 @@ int main(int argc, char *const argv[]){
|
|||||||
syslog(LOG_INFO, "%d%%%s", pct, delimiter);
|
syslog(LOG_INFO, "%d%%%s", pct, delimiter);
|
||||||
#endif
|
#endif
|
||||||
if(run_continuously){
|
if(run_continuously){
|
||||||
sleep(interval);/*
|
sleep(interval);
|
||||||
errno = 0;
|
errno = 0;
|
||||||
rewind(fd_now);
|
rewind(fd_now);
|
||||||
if(errno != 0){
|
if(errno != 0){
|
||||||
@ -201,11 +247,11 @@ int main(int argc, char *const argv[]){
|
|||||||
if(errno != 0){
|
if(errno != 0){
|
||||||
perror("Error rewinding " CHARGE_FULL_PATH);
|
perror("Error rewinding " CHARGE_FULL_PATH);
|
||||||
return 1;
|
return 1;
|
||||||
}*/
|
}/*
|
||||||
fclose(fd_now);
|
fclose(fd_now);
|
||||||
fclose(fd_full);
|
fclose(fd_full);
|
||||||
fd_now = fopen(CHARGE_NOW_PATH, "r");
|
fd_now = fopen(CHARGE_NOW_PATH, "r");
|
||||||
fd_full = fopen(CHARGE_FULL_PATH, "r");
|
fd_full = fopen(CHARGE_FULL_PATH, "r");*/
|
||||||
}
|
}
|
||||||
} while (run_continuously);
|
} while (run_continuously);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user