mirror of
https://github.com/xzeldon/htop.git
synced 2025-07-12 12:14:36 +03:00
Merge individual Battery.[ch] files into Platform.[ch]
Consistent with everything else involving platform-specific calls from core htop code.
This commit is contained in:
@ -25,6 +25,7 @@ in the source distribution for its full text.
|
||||
|
||||
#include <sys/param.h>
|
||||
#include <sys/sysctl.h>
|
||||
#include <sys/sensors.h>
|
||||
#include <sys/swap.h>
|
||||
|
||||
#include <unistd.h>
|
||||
@ -38,6 +39,7 @@ in the source distribution for its full text.
|
||||
#include <fcntl.h>
|
||||
#include <kvm.h>
|
||||
#include <limits.h>
|
||||
#include <errno.h>
|
||||
#include <math.h>
|
||||
|
||||
|
||||
@ -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;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user