summaryrefslogtreecommitdiff
path: root/slstatus.c
diff options
context:
space:
mode:
authorAaron Marcher <info@nulltime.net>2017-06-12 23:55:27 +0200
committerAaron Marcher <info@nulltime.net>2017-06-12 23:55:27 +0200
commit259e967cbf855d1ed3886c7afafb1bfe6ab11645 (patch)
tree71f2b79c998a43c53fc76761808acd5895e3168e /slstatus.c
parent870d68d44e33f9fb7a5402becbc0b5c363066bcc (diff)
add battery_power function
Diffstat (limited to 'slstatus.c')
-rw-r--r--slstatus.c20
1 files changed, 20 insertions, 0 deletions
diff --git a/slstatus.c b/slstatus.c
index 8241451..caccc6e 100644
--- a/slstatus.c
+++ b/slstatus.c
@@ -35,6 +35,7 @@ struct arg {
static char *smprintf(const char *fmt, ...);
static char *battery_perc(const char *bat);
+static char *battery_power(const char *bat);
static char *battery_state(const char *bat);
static char *cpu_perc(void);
static char *datetime(const char *fmt);
@@ -119,6 +120,25 @@ battery_perc(const char *bat)
}
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 *
battery_state(const char *bat)
{
char path[PATH_MAX];