Add test if there is battery

This commit is contained in:
Loïc Bartoletti 2022-10-04 06:14:37 +02:00
parent 45e44e03bd
commit 9d5f0e45c0
2 changed files with 12 additions and 1 deletions

View File

@ -6,7 +6,7 @@
#include <filesystem> #include <filesystem>
#endif #endif
#include <fmt/format.h> #include <fmt/format.h>
#if #defined(__linux__) #if defined(__linux__)
#include <sys/inotify.h> #include <sys/inotify.h>
#endif #endif

View File

@ -177,6 +177,17 @@ const std::tuple<uint8_t, float, std::string, float> waybar::modules::Battery::g
uint32_t total_power = 0; // μW uint32_t total_power = 0; // μW
bool total_power_exists = false; bool total_power_exists = false;
#if defined(__FreeBSD__) #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; int capacity;
size_t size_capacity = sizeof capacity; size_t size_capacity = sizeof capacity;
if (sysctlbyname("hw.acpi.battery.life", &capacity, &size_capacity, NULL,0) != 0) { if (sysctlbyname("hw.acpi.battery.life", &capacity, &size_capacity, NULL,0) != 0) {