commit b646036ca3c11745293c5c961145b5e8b0973846
parent 7c47d03e8c364214598b5e3df0135d51526e23a3
Author: bsandro <brian.drosan@gmail.com>
Date: Wed, 2 Feb 2022 03:00:22 +0200
Pinebook Pro support - displaying battery % and charger state (+ or - sign)
Diffstat:
M | dwmstatus.c | | | 71 | ++++++++++++++++++++++++++++++----------------------------------------- |
1 file changed, 30 insertions(+), 41 deletions(-)
diff --git a/dwmstatus.c b/dwmstatus.c
@@ -156,54 +156,40 @@ cpufreq(void)
}
char *
-getbattery(char *base)
+getbattery()
{
- char *co, status;
- int descap, remcap;
+ int mib[5];
+ struct sensor temp;
+ size_t len;
+ float percentage = 0.0;
+ char charger_state = ' ';
- descap = -1;
- remcap = -1;
+ mib[0] = CTL_HW;
+ mib[1] = HW_SENSORS;
+ mib[2] = 1; // cwfg0
+ mib[3] = SENSOR_PERCENT;
+ mib[4] = 0;
- co = readfile(base, "present");
- if (co == NULL)
- return smprintf("");
- if (co[0] != '1') {
- free(co);
- return smprintf("not present");
- }
- free(co);
+ len = sizeof(temp);
- co = readfile(base, "charge_full_design");
- if (co == NULL) {
- co = readfile(base, "energy_full_design");
- if (co == NULL)
- return smprintf("");
- }
- sscanf(co, "%d", &descap);
- free(co);
-
- co = readfile(base, "charge_now");
- if (co == NULL) {
- co = readfile(base, "energy_now");
- if (co == NULL)
- return smprintf("");
- }
- sscanf(co, "%d", &remcap);
- free(co);
-
- co = readfile(base, "status");
- if (!strncmp(co, "Discharging", 11)) {
- status = '-';
- } else if(!strncmp(co, "Charging", 8)) {
- status = '+';
+ // battery charge value
+ if (sysctl(mib, 5, &temp, &len, NULL, 0) == -1) {
+ return smprintf("sysctl error: %s\n", strerror(errno));
} else {
- status = '?';
+ percentage = (float)temp.value / 1000.0;
}
- if (remcap < 0 || descap < 0)
- return smprintf("invalid");
+ // charger state
+ mib[2] = 3;
+ mib[3] = SENSOR_INDICATOR;
+
+ if (sysctl(mib, 5, &temp, &len, NULL, 0) == -1) {
+ smprintf("sysctl error: %s\n", strerror(errno));
+ } else {
+ charger_state = temp.value == 1 ? '+' : '-';
+ }
- return smprintf("%.0f%%%c", ((float)remcap / (float)descap) * 100, status);
+ return smprintf("%.1f%%%c", percentage, charger_state);
}
char *
@@ -235,6 +221,7 @@ main(void)
char *tmmoscow;
char *tmcyprus;
char *cpu_temp;
+ char *battery;
if (!(dpy = XOpenDisplay(NULL))) {
fprintf(stderr, "dwmstatus: cannot open display.\n");
@@ -245,13 +232,15 @@ main(void)
tmmoscow = mktimes("%H:%M", tzmoscow);
tmcyprus = mktimes("%a %F %R", tzcyprus);
cpu_temp = gettemperature();
- status = smprintf("CPUTEMP: %s MSK(%s) %s", cpu_temp, tmmoscow, tmcyprus);
+ battery = getbattery();
+ status = smprintf("CPUTEMP: %s BAT: %s MSK(%s) %s", cpu_temp, battery, tmmoscow, tmcyprus);
setstatus(status);
free(cpu_temp);
free(tmmoscow);
free(tmcyprus);
free(status);
+ free(battery);
}
XCloseDisplay(dpy);