summaryrefslogtreecommitdiff
path: root/components/battery.c
diff options
context:
space:
mode:
Diffstat (limited to 'components/battery.c')
-rw-r--r--components/battery.c46
1 files changed, 22 insertions, 24 deletions
diff --git a/components/battery.c b/components/battery.c
index f0fe8bf..959e2e2 100644
--- a/components/battery.c
+++ b/components/battery.c
@@ -10,6 +10,13 @@
#include <stdint.h>
#include <unistd.h>
+ #define POWER_SUPPLY_CAPACITY "/sys/class/power_supply/%s/capacity"
+ #define POWER_SUPPLY_STATUS "/sys/class/power_supply/%s/status"
+ #define POWER_SUPPLY_CHARGE "/sys/class/power_supply/%s/charge_now"
+ #define POWER_SUPPLY_ENERGY "/sys/class/power_supply/%s/energy_now"
+ #define POWER_SUPPLY_CURRENT "/sys/class/power_supply/%s/current"
+ #define POWER_SUPPLY_POWER "/sys/class/power_supply/%s/power"
+
static const char *
pick(const char *bat, const char *f1, const char *f2, char *path,
size_t length)
@@ -33,10 +40,8 @@
int perc;
char path[PATH_MAX];
- if (esnprintf(path, sizeof(path),
- "/sys/class/power_supply/%s/capacity", bat) < 0) {
+ if (esnprintf(path, sizeof(path), POWER_SUPPLY_CAPACITY, bat) < 0)
return NULL;
- }
if (pscanf(path, "%d", &perc) != 1) {
return NULL;
}
@@ -59,10 +64,8 @@
size_t i;
char path[PATH_MAX], state[12];
- if (esnprintf(path, sizeof(path),
- "/sys/class/power_supply/%s/status", bat) < 0) {
+ if (esnprintf(path, sizeof(path), POWER_SUPPLY_STATUS, bat) < 0)
return NULL;
- }
if (pscanf(path, "%12[a-zA-Z ]", state) != 1) {
return NULL;
}
@@ -82,28 +85,22 @@
double timeleft;
char path[PATH_MAX], state[12];
- if (esnprintf(path, sizeof(path),
- "/sys/class/power_supply/%s/status", bat) < 0) {
+ if (esnprintf(path, sizeof(path), POWER_SUPPLY_STATUS, bat) < 0)
return NULL;
- }
if (pscanf(path, "%12[a-zA-Z ]", state) != 1) {
return NULL;
}
- if (!pick(bat, "/sys/class/power_supply/%s/charge_now",
- "/sys/class/power_supply/%s/energy_now", path,
+ if (!pick(bat, POWER_SUPPLY_CHARGE, POWER_SUPPLY_ENERGY, path,
sizeof(path)) ||
- pscanf(path, "%ju", &charge_now) < 0) {
+ pscanf(path, "%ju", &charge_now) < 0)
return NULL;
- }
if (!strcmp(state, "Discharging")) {
- if (!pick(bat, "/sys/class/power_supply/%s/current_now",
- "/sys/class/power_supply/%s/power_now", path,
+ if (!pick(bat, POWER_SUPPLY_CURRENT, POWER_SUPPLY_POWER, path,
sizeof(path)) ||
- pscanf(path, "%ju", &current_now) < 0) {
+ pscanf(path, "%ju", &current_now) < 0)
return NULL;
- }
if (current_now == 0) {
return NULL;
@@ -201,6 +198,10 @@
#elif defined(__FreeBSD__)
#include <sys/sysctl.h>
+ #define BATTERY_LIFE "hw.acpi.battery.life"
+ #define BATTERY_STATE "hw.acpi.battery.state"
+ #define BATTERY_TIME "hw.acpi.battery.time"
+
const char *
battery_perc(const char *unused)
{
@@ -208,8 +209,7 @@
size_t len;
len = sizeof(cap);
- if (sysctlbyname("hw.acpi.battery.life", &cap, &len, NULL, 0) == -1
- || !len)
+ if (sysctlbyname(BATTERY_LIFE, &cap, &len, NULL, 0) < 0 || !len)
return NULL;
return bprintf("%d", cap);
@@ -222,8 +222,7 @@
size_t len;
len = sizeof(state);
- if (sysctlbyname("hw.acpi.battery.state", &state, &len, NULL, 0) == -1
- || !len)
+ if (sysctlbyname(BATTERY_STATE, &state, &len, NULL, 0) < 0 || !len)
return NULL;
switch(state) {
@@ -244,9 +243,8 @@
size_t len;
len = sizeof(rem);
- if (sysctlbyname("hw.acpi.battery.time", &rem, &len, NULL, 0) == -1
- || !len
- || rem == -1)
+ if (sysctlbyname(BATTERY_TIME, &rem, &len, NULL, 0) < 0 || !len
+ || rem < 0)
return NULL;
return bprintf("%uh %02um", rem / 60, rem % 60);