add battery_power function

This commit is contained in:
Aaron Marcher 2017-06-12 23:55:27 +02:00
parent 870d68d44e
commit 259e967cbf
No known key found for this signature in database
GPG key ID: 74B048E5C2474F9A
2 changed files with 21 additions and 0 deletions

View file

@ -8,6 +8,7 @@
/* statusbar /* statusbar
- battery_perc (battery percentage) [argument: battery name] - battery_perc (battery percentage) [argument: battery name]
- battery_power (battery power usage) [argument: battery name]
- battery_state (battery charging state) [argument: battery name] - battery_state (battery charging state) [argument: battery name]
- cpu_perc (cpu usage in percent) [argument: NULL] - cpu_perc (cpu usage in percent) [argument: NULL]
- datetime (date and time) [argument: format] - datetime (date and time) [argument: format]

View file

@ -35,6 +35,7 @@ struct arg {
static char *smprintf(const char *fmt, ...); static char *smprintf(const char *fmt, ...);
static char *battery_perc(const char *bat); static char *battery_perc(const char *bat);
static char *battery_power(const char *bat);
static char *battery_state(const char *bat); static char *battery_state(const char *bat);
static char *cpu_perc(void); static char *cpu_perc(void);
static char *datetime(const char *fmt); static char *datetime(const char *fmt);
@ -118,6 +119,25 @@ battery_perc(const char *bat)
return smprintf("%d%%", perc); return smprintf("%d%%", perc);
} }
static char *
battery_power(const char *bat)
{
char path[PATH_MAX];
FILE *fp;
int watts;
snprintf(path, sizeof(path), "%s%s%s", "/sys/class/power_supply/", bat, "/power_now");
fp = fopen(path, "r");
if (fp == NULL) {
warn("Failed to open file %s", path);
return smprintf("%s", UNKNOWN_STR);
}
fscanf(fp, "%i", &watts);
fclose(fp);
return smprintf("%d", (watts + 500000) / 1000000);
}
static char * static char *
battery_state(const char *bat) battery_state(const char *bat)
{ {