From 81b63652bfbaaa50d4da16f9e76a2d96036ba434 Mon Sep 17 00:00:00 2001 From: Jos Dehaes Date: Fri, 22 Oct 2021 15:43:22 +0200 Subject: [PATCH] battery --- src/freebsd/btop_collect.cpp | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) 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 { 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}; }