2015-09-18 04:46:48 +00:00
|
|
|
/*
|
|
|
|
htop - openbsd/Battery.c
|
|
|
|
(C) 2015 Hisham H. Muhammad
|
|
|
|
(C) 2015 Michael McConville
|
2020-10-05 07:51:32 +00:00
|
|
|
Released under the GNU GPLv2, see the COPYING file
|
2015-09-18 04:46:48 +00:00
|
|
|
in the source distribution for its full text.
|
|
|
|
*/
|
|
|
|
|
2020-09-25 12:03:55 +00:00
|
|
|
#include "Battery.h"
|
|
|
|
|
2018-03-05 00:54:37 +00:00
|
|
|
#include <sys/sysctl.h>
|
|
|
|
#include <sys/sensors.h>
|
|
|
|
#include <errno.h>
|
2020-09-07 09:52:42 +00:00
|
|
|
#include <math.h>
|
2018-12-16 08:25:54 +00:00
|
|
|
#include <string.h>
|
2018-03-05 00:54:37 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-09-18 04:46:48 +00:00
|
|
|
|
|
|
|
void Battery_getData(double* level, ACPresence* isOnAC) {
|
2018-03-05 00:54:37 +00:00
|
|
|
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);
|
2019-10-31 16:39:12 +00:00
|
|
|
|
2020-09-07 09:52:42 +00:00
|
|
|
*level = NAN;
|
2018-03-05 00:54:37 +00:00
|
|
|
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;
|
2020-10-31 22:28:02 +00:00
|
|
|
*level = 100 * (charge / last_full_capacity);
|
2018-03-05 00:54:37 +00:00
|
|
|
if (charge >= last_full_capacity) {
|
|
|
|
*level = 100;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-10-31 16:39:12 +00:00
|
|
|
|
2018-03-05 00:54:37 +00:00
|
|
|
found = findDevice("acpiac0", mib, &snsrdev, &sdlen);
|
2019-10-31 16:39:12 +00:00
|
|
|
|
2015-09-18 04:46:48 +00:00
|
|
|
*isOnAC = AC_ERROR;
|
2018-03-05 00:54:37 +00:00
|
|
|
if (found) {
|
|
|
|
mib[3] = 9;
|
|
|
|
mib[4] = 0;
|
|
|
|
if (sysctl(mib, 5, &s, &slen, NULL, 0) != -1) {
|
|
|
|
*isOnAC = s.value;
|
|
|
|
}
|
|
|
|
}
|
2015-09-18 04:46:48 +00:00
|
|
|
}
|