diff --git a/dwm-stat/status.c b/dwm-stat/status.c index 97cc212..2f86f58 100644 --- a/dwm-stat/status.c +++ b/dwm-stat/status.c @@ -4,12 +4,11 @@ // This version mainly aims to ve a visual overhaul of both code and output over barM // Dependancies: cat grep awk -// Define these however you like #include #include #include -#include #include +#include #include #include #include @@ -61,6 +60,34 @@ ram_usage(void) 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* cpu_usage(void) { @@ -90,6 +117,9 @@ main(void) static char* (*func_table[])(void) = { ram_usage, date_time, +#ifdef LAPTOP + battery_percentage, +#endif }; char stat_output[MAXSTR];