commit 5fc4644c7df82545a24307f4dc1f80e3dfdfe743
parent b646036ca3c11745293c5c961145b5e8b0973846
Author: bsandro <email@bsandro.tech>
Date: Wed, 7 Sep 2022 22:07:34 +0300
Thinkpad T60 battery support
Diffstat:
M | dwmstatus.c | | | 55 | +++++++++++++++++++++++++++++++++++++++---------------- |
1 file changed, 39 insertions(+), 16 deletions(-)
diff --git a/dwmstatus.c b/dwmstatus.c
@@ -156,40 +156,63 @@ cpufreq(void)
}
char *
-getbattery()
+getbattery(void)
{
int mib[5];
struct sensor temp;
- size_t len;
- float percentage = 0.0;
+ size_t len = sizeof(temp);
+ double full_charge = -1.0;
+ double cur_charge = -1.0;
+ double consumption = -1.0;
char charger_state = ' ';
+ double percentage = 0.0;
+ int remaining = 0.0;
mib[0] = CTL_HW;
mib[1] = HW_SENSORS;
- mib[2] = 1; // cwfg0
- mib[3] = SENSOR_PERCENT;
- mib[4] = 0;
-
- len = sizeof(temp);
+ mib[2] = 1; // acpibat0
+ mib[3] = SENSOR_WATTHOUR;
+ mib[4] = 0; // watthour0 - last full capacity
+ if (sysctl(mib, 5, &temp, &len, NULL, 0) == -1) {
+ return smprintf("sysctl error: %s\n", strerror(errno));
+ } else {
+ full_charge = (double)temp.value;
+ }
- // battery charge value
+ mib[4] = 3; // watthour3 = remaining capacity
if (sysctl(mib, 5, &temp, &len, NULL, 0) == -1) {
return smprintf("sysctl error: %s\n", strerror(errno));
} else {
- percentage = (float)temp.value / 1000.0;
+ cur_charge = (double)temp.value;
}
- // charger state
- mib[2] = 3;
- mib[3] = SENSOR_INDICATOR;
+ mib[3] = SENSOR_INTEGER; // charger state
+ mib[4] = 0;
+ if (sysctl(mib, 5, &temp, &len, NULL, 0) == -1) {
+ smprintf("sysctl error: %s\n", strerror(errno));
+ } else {
+ charger_state = temp.value == 1 ? '-' : '+';
+ }
+ mib[3] = SENSOR_WATTS;
if (sysctl(mib, 5, &temp, &len, NULL, 0) == -1) {
smprintf("sysctl error: %s\n", strerror(errno));
} else {
- charger_state = temp.value == 1 ? '+' : '-';
+ consumption = (double)temp.value;
}
- return smprintf("%.1f%%%c", percentage, charger_state);
+ percentage = cur_charge / full_charge * 100.0;
+ if (charger_state == '-') {
+ remaining = cur_charge / consumption * 60.0;
+ } else {
+ remaining = (full_charge - cur_charge) / consumption * 60.0;
+ }
+
+ if (remaining < 60.0) {
+ return smprintf("%.1f%%%c(%dm)", percentage, charger_state, remaining);
+ } else {
+ return smprintf("%.1f%%%c(%dh%dm)", percentage, charger_state, remaining / 60, remaining % 60);
+ }
}
char *
@@ -201,7 +224,7 @@ gettemperature()
mib[0] = CTL_HW;
mib[1] = HW_SENSORS;
- mib[2] = 0; // bcmtmon0
+ mib[2] = 3; // bcmtmon0
mib[3] = SENSOR_TEMP;
mib[4] = 0; // temp0