From ea9622b8c9444d92007f24fc54597f83c498f11d Mon Sep 17 00:00:00 2001 From: Nathan Scott Date: Tue, 17 Nov 2020 18:12:38 +1100 Subject: Merge individual Battery.[ch] files into Platform.[ch] Consistent with everything else involving platform-specific calls from core htop code. --- openbsd/Battery.c | 74 ------------------------------------------------------ openbsd/Battery.h | 15 ----------- openbsd/Platform.c | 59 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 59 insertions(+), 89 deletions(-) delete mode 100644 openbsd/Battery.c delete mode 100644 openbsd/Battery.h (limited to 'openbsd') diff --git a/openbsd/Battery.c b/openbsd/Battery.c deleted file mode 100644 index b72253e0..00000000 --- a/openbsd/Battery.c +++ /dev/null @@ -1,74 +0,0 @@ -/* -htop - openbsd/Battery.c -(C) 2015 Hisham H. Muhammad -(C) 2015 Michael McConville -Released under the GNU GPLv2, see the COPYING file -in the source distribution for its full text. -*/ - -#include "Battery.h" - -#include -#include -#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) { - 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 = NAN; - 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; - } - } -} diff --git a/openbsd/Battery.h b/openbsd/Battery.h deleted file mode 100644 index 60b632d2..00000000 --- a/openbsd/Battery.h +++ /dev/null @@ -1,15 +0,0 @@ -#ifndef HEADER_Battery -#define HEADER_Battery -/* -htop - openbsd/Battery.h -(C) 2015 Hisham H. Muhammad -(C) 2015 Michael McConville -Released under the GNU GPLv2, see the COPYING file -in the source distribution for its full text. -*/ - -#include "BatteryMeter.h" - -void Battery_getData(double* level, ACPresence* isOnAC); - -#endif diff --git a/openbsd/Platform.c b/openbsd/Platform.c index 5601048d..bf045396 100644 --- a/openbsd/Platform.c +++ b/openbsd/Platform.c @@ -25,6 +25,7 @@ in the source distribution for its full text. #include #include +#include #include #include @@ -38,6 +39,7 @@ in the source distribution for its full text. #include #include #include +#include #include @@ -322,3 +324,60 @@ bool Platform_getNetworkIO(unsigned long int* bytesReceived, *packetsTransmitted = 0; return false; } + +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 Platform_getBattery(double* level, ACPresence* isOnAC) { + 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 = NAN; + 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