summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJos Dehaes <jos.dehaes@gmail.com>2021-10-22 15:43:22 +0200
committerJos Dehaes <jos.dehaes@gmail.com>2021-10-22 15:43:22 +0200
commit81b63652bfbaaa50d4da16f9e76a2d96036ba434 (patch)
tree39b9bf317efece7225c9728e69f7e0ecc0fc4f46
parent41ba98695400a1a82aa6ddb884344b7c1e556f49 (diff)
battery
-rw-r--r--src/freebsd/btop_collect.cpp25
1 files changed, 24 insertions, 1 deletions
diff --git a/src/freebsd/btop_collect.cpp b/src/freebsd/btop_collect.cpp
index 2a7dcdd..2b0bee6 100644
--- a/src/freebsd/btop_collect.cpp
+++ b/src/freebsd/btop_collect.cpp
@@ -297,9 +297,32 @@ namespace Cpu {
auto get_battery() -> tuple<int, long, string> {
if (not has_battery) return {0, 0, ""};
- uint32_t percent = -1;
long seconds = -1;
+ uint32_t percent = -1;
+ size_t size = sizeof(percent);
string status = "discharging";
+ if (sysctlbyname("hw.acpi.battery.life", &percent, &size, NULL, 0) < 0) {
+ Logger::warning("Could not get battery pct");
+ } else {
+ has_battery = true;
+ size_t size = sizeof(seconds);
+ if (sysctlbyname("hw.acpi.battery.time", &seconds, &size, NULL, 0) < 0) {
+ Logger::warning("Could not get battery seconds");
+ }
+ int state;
+ size = sizeof(state);
+ if (sysctlbyname("hw.acpi.battery.state", &state, &size, NULL, 0) < 0) {
+ Logger::warning("Could not get battery state");
+ } else {
+ if (state == 2) {
+ status = "charging";
+ }
+ }
+ if (percent == 100) {
+ status = "full";
+ }
+ }
+
return {percent, seconds, status};
}