From df568a576f7b44ac5a2b9b7222c7f39d9932f626 Mon Sep 17 00:00:00 2001 From: Daniel Lange Date: Wed, 11 Apr 2018 01:26:28 +0200 Subject: Imported Upstream version 2.2.0 --- openbsd/Battery.c | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 57 insertions(+), 2 deletions(-) (limited to 'openbsd') diff --git a/openbsd/Battery.c b/openbsd/Battery.c index f9d0969..3a0bae1 100644 --- a/openbsd/Battery.c +++ b/openbsd/Battery.c @@ -7,10 +7,65 @@ in the source distribution for its full text. */ #include "BatteryMeter.h" +#include +#include +#include + +static bool findDevice(const char* name, int* mib, struct sensordev* snsrdev, size_t* sdlen) { + for (int devn = 0;; devn++) { + mib[2] = devn; + if (sysctl(mib, 3, snsrdev, sdlen, NULL, 0) == -1) { + if (errno == ENXIO) + continue; + if (errno == ENOENT) + return false; + } + if (strcmp(name, snsrdev->xname) == 0) { + return true; + } + } +} void Battery_getData(double* level, ACPresence* isOnAC) { - // TODO + static int mib[] = {CTL_HW, HW_SENSORS, 0, 0, 0}; + struct sensor s; + size_t slen = sizeof(struct sensor); + struct sensordev snsrdev; + size_t sdlen = sizeof(struct sensordev); + + bool found = findDevice("acpibat0", mib, &snsrdev, &sdlen); + *level = -1; + if (found) { + /* last full capacity */ + mib[3] = 7; + mib[4] = 0; + double last_full_capacity = 0; + if (sysctl(mib, 5, &s, &slen, NULL, 0) != -1) { + last_full_capacity = s.value; + } + if (last_full_capacity > 0) { + /* remaining capacity */ + mib[3] = 7; + mib[4] = 3; + if (sysctl(mib, 5, &s, &slen, NULL, 0) != -1) { + double charge = s.value; + *level = 100*(charge / last_full_capacity); + if (charge >= last_full_capacity) { + *level = 100; + } + } + } + } + + found = findDevice("acpiac0", mib, &snsrdev, &sdlen); + *isOnAC = AC_ERROR; + if (found) { + mib[3] = 9; + mib[4] = 0; + if (sysctl(mib, 5, &s, &slen, NULL, 0) != -1) { + *isOnAC = s.value; + } + } } - -- cgit v1.2.3