From 9d5f0e45c051de9b424c89bfb32f43ac7392ab85 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Bartoletti?= Date: Tue, 4 Oct 2022 06:14:37 +0200 Subject: [PATCH] Add test if there is battery --- include/modules/battery.hpp | 2 +- src/modules/battery.cpp | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/include/modules/battery.hpp b/include/modules/battery.hpp index 42cf1647..614b36ab 100644 --- a/include/modules/battery.hpp +++ b/include/modules/battery.hpp @@ -6,7 +6,7 @@ #include #endif #include -#if #defined(__linux__) +#if defined(__linux__) #include #endif diff --git a/src/modules/battery.cpp b/src/modules/battery.cpp index 13a49208..a4657409 100644 --- a/src/modules/battery.cpp +++ b/src/modules/battery.cpp @@ -177,6 +177,17 @@ const std::tuple waybar::modules::Battery::g uint32_t total_power = 0; // μW bool total_power_exists = false; #if defined(__FreeBSD__) + /* Allocate state of battery units reported via ACPI. */ + int battery_units = 0; + size_t battery_units_size = sizeof battery_units; + if( sysctlbyname("hw.acpi.battery.units", &battery_units, &battery_units_size, NULL, 0) != 0) { + throw std::runtime_error("sysctl hw.acpi.battery.units failed"); + } + + if(battery_units < 0) { + throw std::runtime_error("No battery units"); + } + int capacity; size_t size_capacity = sizeof capacity; if (sysctlbyname("hw.acpi.battery.life", &capacity, &size_capacity, NULL,0) != 0) {