support for laptop battery percentage in statline

This commit is contained in:
shockrahwow 2019-09-02 02:50:31 -07:00
parent 89263c87ed
commit 0707bf28db

View File

@ -4,12 +4,11 @@
// This version mainly aims to ve a visual overhaul of both code and output over barM // This version mainly aims to ve a visual overhaul of both code and output over barM
// Dependancies: cat grep awk // Dependancies: cat grep awk
// Define these however you like
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h> #include <unistd.h>
#include <time.h>
#include <string.h> #include <string.h>
#include <time.h>
#include <stdarg.h> #include <stdarg.h>
#include <X11/Xlib.h> #include <X11/Xlib.h>
#include <sys/utsname.h> #include <sys/utsname.h>
@ -61,6 +60,34 @@ ram_usage(void)
return buf; return buf;
} }
#ifdef LAPTOP
#define BATTERY_STATUS_BUFFER 50
static char*
battery_percentage(void)
{
unsigned long total, curr;
static char buf[BATTERY_STATUS_BUFFER];
FILE* file_max = fopen("/sys/class/power_supply/BAT0/energy_full","r");
if(file_max) {
fgets(buf, BATTERY_STATUS_BUFFER, file_max);
fclose(file_max);
total = atoi(buf);
}
FILE* file_curr = fopen("/sys/class/power_supply/BAT0/energy_now", "r");
if(file_curr) {
fgets(buf, BATTERY_STATUS_BUFFER, file_max);
fclose(file_curr);
total = atoi(buf);
}
snprintf(buf, BATTERY_STATUS_BUFFER, "Battery: %.2f", (float)(curr/total));
return buf;
}
#endif
static char* static char*
cpu_usage(void) cpu_usage(void)
{ {
@ -90,6 +117,9 @@ main(void)
static char* (*func_table[])(void) = { static char* (*func_table[])(void) = {
ram_usage, ram_usage,
date_time, date_time,
#ifdef LAPTOP
battery_percentage,
#endif
}; };
char stat_output[MAXSTR]; char stat_output[MAXSTR];