2018-08-08 23:54:58 +02:00
|
|
|
#include "modules/battery.hpp"
|
2020-05-22 21:23:04 +02:00
|
|
|
|
2019-05-19 01:44:45 +02:00
|
|
|
#include <spdlog/spdlog.h>
|
2018-08-08 23:54:58 +02:00
|
|
|
|
2018-12-18 17:30:54 +01:00
|
|
|
waybar::modules::Battery::Battery(const std::string& id, const Json::Value& config)
|
2019-05-22 12:06:24 +02:00
|
|
|
: ALabel(config, "battery", id, "{capacity}%", 60) {
|
2020-12-03 10:52:33 +01:00
|
|
|
battery_watch_fd_ = inotify_init1(IN_CLOEXEC);
|
|
|
|
if (battery_watch_fd_ == -1) {
|
|
|
|
throw std::runtime_error("Unable to listen batteries.");
|
|
|
|
}
|
|
|
|
|
|
|
|
global_watch_fd_ = inotify_init1(IN_CLOEXEC);
|
|
|
|
if (global_watch_fd_ == -1) {
|
2018-08-13 14:05:13 +02:00
|
|
|
throw std::runtime_error("Unable to listen batteries.");
|
2018-08-16 14:29:41 +02:00
|
|
|
}
|
2020-11-27 14:56:04 +01:00
|
|
|
|
|
|
|
// Watch the directory for any added or removed batteries
|
2020-12-03 10:52:33 +01:00
|
|
|
global_watch = inotify_add_watch(global_watch_fd_, data_dir_.c_str(), IN_CREATE | IN_DELETE);
|
2020-11-27 14:56:04 +01:00
|
|
|
if (global_watch < 0) {
|
|
|
|
throw std::runtime_error("Could not watch for battery plug/unplug");
|
2018-08-16 14:29:41 +02:00
|
|
|
}
|
2020-11-27 14:56:04 +01:00
|
|
|
|
|
|
|
refreshBatteries();
|
2018-08-20 14:50:45 +02:00
|
|
|
worker();
|
|
|
|
}
|
|
|
|
|
2019-04-18 17:52:00 +02:00
|
|
|
waybar::modules::Battery::~Battery() {
|
2020-12-03 10:52:33 +01:00
|
|
|
std::lock_guard<std::mutex> guard(battery_list_mutex_);
|
|
|
|
|
2020-11-27 14:56:04 +01:00
|
|
|
if (global_watch >= 0) {
|
2020-12-03 10:52:33 +01:00
|
|
|
inotify_rm_watch(global_watch_fd_, global_watch);
|
2020-11-27 14:56:04 +01:00
|
|
|
}
|
2020-12-03 10:52:33 +01:00
|
|
|
close(global_watch_fd_);
|
|
|
|
|
2020-11-27 14:56:04 +01:00
|
|
|
for (auto it = batteries_.cbegin(); it != batteries_.cend(); it++) {
|
|
|
|
auto watch_id = (*it).second;
|
|
|
|
if (watch_id >= 0) {
|
2020-12-03 10:52:33 +01:00
|
|
|
inotify_rm_watch(battery_watch_fd_, watch_id);
|
2020-11-27 14:56:04 +01:00
|
|
|
}
|
|
|
|
batteries_.erase(it);
|
2018-12-24 08:37:10 +01:00
|
|
|
}
|
2020-12-03 10:52:33 +01:00
|
|
|
close(battery_watch_fd_);
|
2018-08-20 14:50:45 +02:00
|
|
|
}
|
|
|
|
|
2019-04-18 17:52:00 +02:00
|
|
|
void waybar::modules::Battery::worker() {
|
2018-11-23 11:57:37 +01:00
|
|
|
thread_timer_ = [this] {
|
2020-12-03 10:52:33 +01:00
|
|
|
// Make sure we eventually update the list of batteries even if we miss an
|
|
|
|
// inotify event for some reason
|
|
|
|
refreshBatteries();
|
2018-10-25 17:39:15 +02:00
|
|
|
dp.emit();
|
2018-12-24 08:50:58 +01:00
|
|
|
thread_timer_.sleep_for(interval_);
|
2018-10-25 17:39:15 +02:00
|
|
|
};
|
2018-08-19 13:39:57 +02:00
|
|
|
thread_ = [this] {
|
2018-08-17 14:24:00 +02:00
|
|
|
struct inotify_event event = {0};
|
2020-12-03 10:52:33 +01:00
|
|
|
int nbytes = read(battery_watch_fd_, &event, sizeof(event));
|
|
|
|
if (nbytes != sizeof(event) || event.mask & IN_IGNORED) {
|
|
|
|
thread_.stop();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
dp.emit();
|
|
|
|
};
|
|
|
|
thread_battery_update_ = [this] {
|
|
|
|
struct inotify_event event = {0};
|
|
|
|
int nbytes = read(global_watch_fd_, &event, sizeof(event));
|
2018-12-24 08:37:10 +01:00
|
|
|
if (nbytes != sizeof(event) || event.mask & IN_IGNORED) {
|
|
|
|
thread_.stop();
|
2018-08-13 14:05:13 +02:00
|
|
|
return;
|
2018-08-16 14:29:41 +02:00
|
|
|
}
|
2020-12-03 10:52:33 +01:00
|
|
|
refreshBatteries();
|
2018-08-20 14:50:45 +02:00
|
|
|
dp.emit();
|
2018-08-08 23:54:58 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-11-27 14:56:04 +01:00
|
|
|
void waybar::modules::Battery::refreshBatteries() {
|
2020-12-03 10:52:33 +01:00
|
|
|
std::lock_guard<std::mutex> guard(battery_list_mutex_);
|
|
|
|
|
2020-11-27 14:56:04 +01:00
|
|
|
// Mark existing list of batteries as not necessarily found
|
|
|
|
std::map<fs::path, bool> check_map;
|
|
|
|
for (auto const& bat : batteries_) {
|
|
|
|
check_map[bat.first] = false;
|
|
|
|
}
|
|
|
|
|
2018-12-24 08:37:10 +01:00
|
|
|
try {
|
2019-05-12 19:53:14 +02:00
|
|
|
for (auto& node : fs::directory_iterator(data_dir_)) {
|
2018-12-24 08:37:10 +01:00
|
|
|
if (!fs::is_directory(node)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
auto dir_name = node.path().filename();
|
|
|
|
auto bat_defined = config_["bat"].isString();
|
2019-04-18 17:52:00 +02:00
|
|
|
if (((bat_defined && dir_name == config_["bat"].asString()) || !bat_defined) &&
|
2019-05-12 19:53:14 +02:00
|
|
|
fs::exists(node.path() / "capacity") && fs::exists(node.path() / "uevent") &&
|
2020-06-16 12:30:21 +02:00
|
|
|
fs::exists(node.path() / "status") && fs::exists(node.path() / "type")) {
|
2020-11-27 14:56:04 +01:00
|
|
|
std::string type;
|
|
|
|
std::ifstream(node.path() / "type") >> type;
|
2020-06-16 12:30:21 +02:00
|
|
|
|
2020-11-27 14:56:04 +01:00
|
|
|
if (!type.compare("Battery")){
|
|
|
|
check_map[node.path()] = true;
|
|
|
|
auto search = batteries_.find(node.path());
|
|
|
|
if (search == batteries_.end()) {
|
|
|
|
// We've found a new battery save it and start listening for events
|
|
|
|
auto event_path = (node.path() / "uevent");
|
2020-12-03 10:52:33 +01:00
|
|
|
auto wd = inotify_add_watch(battery_watch_fd_, event_path.c_str(), IN_ACCESS);
|
2020-11-27 14:56:04 +01:00
|
|
|
if (wd < 0) {
|
|
|
|
throw std::runtime_error("Could not watch events for " + node.path().string());
|
2020-06-16 12:30:21 +02:00
|
|
|
}
|
2020-11-27 14:56:04 +01:00
|
|
|
batteries_[node.path()] = wd;
|
|
|
|
}
|
|
|
|
}
|
2018-12-24 08:37:10 +01:00
|
|
|
}
|
|
|
|
auto adap_defined = config_["adapter"].isString();
|
2019-04-18 17:52:00 +02:00
|
|
|
if (((adap_defined && dir_name == config_["adapter"].asString()) || !adap_defined) &&
|
2019-05-12 19:53:14 +02:00
|
|
|
fs::exists(node.path() / "online")) {
|
|
|
|
adapter_ = node.path();
|
2018-12-24 08:37:10 +01:00
|
|
|
}
|
|
|
|
}
|
2019-04-18 17:52:00 +02:00
|
|
|
} catch (fs::filesystem_error& e) {
|
2018-12-24 08:37:10 +01:00
|
|
|
throw std::runtime_error(e.what());
|
|
|
|
}
|
|
|
|
if (batteries_.empty()) {
|
|
|
|
if (config_["bat"].isString()) {
|
|
|
|
throw std::runtime_error("No battery named " + config_["bat"].asString());
|
|
|
|
}
|
|
|
|
throw std::runtime_error("No batteries.");
|
|
|
|
}
|
2020-11-27 14:56:04 +01:00
|
|
|
|
|
|
|
// Remove any batteries that are no longer present and unwatch them
|
|
|
|
for (auto const& check : check_map) {
|
|
|
|
if (!check.second) {
|
|
|
|
auto watch_id = batteries_[check.first];
|
|
|
|
if (watch_id >= 0) {
|
2020-12-03 10:52:33 +01:00
|
|
|
inotify_rm_watch(battery_watch_fd_, watch_id);
|
2020-11-27 14:56:04 +01:00
|
|
|
}
|
|
|
|
batteries_.erase(check.first);
|
|
|
|
}
|
|
|
|
}
|
2018-12-24 08:37:10 +01:00
|
|
|
}
|
|
|
|
|
2020-12-03 10:52:33 +01:00
|
|
|
const std::tuple<uint8_t, float, std::string> waybar::modules::Battery::getInfos() {
|
|
|
|
std::lock_guard<std::mutex> guard(battery_list_mutex_);
|
|
|
|
|
2018-08-08 23:54:58 +02:00
|
|
|
try {
|
2019-05-22 10:09:05 +02:00
|
|
|
uint32_t total_power = 0; // μW
|
|
|
|
uint32_t total_energy = 0; // μWh
|
2019-05-21 19:36:14 +02:00
|
|
|
uint32_t total_energy_full = 0;
|
2018-11-02 22:50:01 +01:00
|
|
|
std::string status = "Unknown";
|
2020-11-27 14:56:04 +01:00
|
|
|
for (auto const& item : batteries_) {
|
|
|
|
auto bat = item.first;
|
2019-05-21 19:36:14 +02:00
|
|
|
uint32_t power_now;
|
|
|
|
uint32_t energy_full;
|
|
|
|
uint32_t energy_now;
|
2018-08-15 22:19:17 +02:00
|
|
|
std::string _status;
|
|
|
|
std::ifstream(bat / "status") >> _status;
|
2019-05-22 10:06:54 +02:00
|
|
|
auto rate_path = fs::exists(bat / "current_now") ? "current_now" : "power_now";
|
|
|
|
std::ifstream(bat / rate_path) >> power_now;
|
|
|
|
auto now_path = fs::exists(bat / "charge_now") ? "charge_now" : "energy_now";
|
|
|
|
std::ifstream(bat / now_path) >> energy_now;
|
|
|
|
auto full_path = fs::exists(bat / "charge_full") ? "charge_full" : "energy_full";
|
|
|
|
std::ifstream(bat / full_path) >> energy_full;
|
2018-08-16 14:29:41 +02:00
|
|
|
if (_status != "Unknown") {
|
2018-08-15 22:19:17 +02:00
|
|
|
status = _status;
|
2018-08-16 14:29:41 +02:00
|
|
|
}
|
2019-05-21 19:36:14 +02:00
|
|
|
total_power += power_now;
|
|
|
|
total_energy += energy_now;
|
|
|
|
total_energy_full += energy_full;
|
2018-08-08 23:54:58 +02:00
|
|
|
}
|
2019-05-17 10:59:54 +02:00
|
|
|
if (!adapter_.empty() && status == "Discharging") {
|
|
|
|
bool online;
|
|
|
|
std::ifstream(adapter_ / "online") >> online;
|
|
|
|
if (online) {
|
|
|
|
status = "Plugged";
|
|
|
|
}
|
2019-05-14 15:43:57 +02:00
|
|
|
}
|
2019-05-21 19:36:14 +02:00
|
|
|
float time_remaining = 0;
|
|
|
|
if (status == "Discharging" && total_power != 0) {
|
2019-05-22 10:09:05 +02:00
|
|
|
time_remaining = (float)total_energy / total_power;
|
2019-05-21 19:36:14 +02:00
|
|
|
} else if (status == "Charging" && total_power != 0) {
|
2019-05-22 10:09:05 +02:00
|
|
|
time_remaining = -(float)(total_energy_full - total_energy) / total_power;
|
2020-11-26 03:16:57 +01:00
|
|
|
if (time_remaining > 0.0f) {
|
|
|
|
// If we've turned positive it means the battery is past 100% and so
|
|
|
|
// just report that as no time remaining
|
|
|
|
time_remaining = 0.0f;
|
|
|
|
}
|
2019-05-21 19:36:14 +02:00
|
|
|
}
|
2020-11-26 02:02:06 +01:00
|
|
|
float capacity = ((float)total_energy * 100.0f / (float) total_energy_full);
|
2020-04-05 16:56:51 +02:00
|
|
|
// Handle full-at
|
|
|
|
if (config_["full-at"].isUInt()) {
|
|
|
|
auto full_at = config_["full-at"].asUInt();
|
|
|
|
if (full_at < 100) {
|
2020-04-21 00:53:36 +02:00
|
|
|
capacity = 100.f * capacity / full_at;
|
2020-04-05 16:56:51 +02:00
|
|
|
}
|
|
|
|
}
|
2020-11-26 03:16:57 +01:00
|
|
|
if (capacity > 100.f) {
|
|
|
|
// This can happen when the battery is calibrating and goes above 100%
|
2020-11-26 03:34:36 +01:00
|
|
|
// Handle it gracefully by clamping at 100%
|
2020-11-26 03:16:57 +01:00
|
|
|
capacity = 100.f;
|
2020-11-26 03:34:36 +01:00
|
|
|
}
|
|
|
|
uint8_t cap = round(capacity);
|
|
|
|
if (cap == 100) {
|
|
|
|
// If we've reached 100% just mark as full as some batteries can stay
|
|
|
|
// stuck reporting they're still charging but not yet done
|
2020-11-26 03:16:57 +01:00
|
|
|
status = "Full";
|
|
|
|
}
|
2020-11-26 03:34:36 +01:00
|
|
|
|
|
|
|
return {cap, time_remaining, status};
|
2018-11-02 11:23:29 +01:00
|
|
|
} catch (const std::exception& e) {
|
2019-05-22 10:09:05 +02:00
|
|
|
spdlog::error("Battery: {}", e.what());
|
2019-05-21 19:36:14 +02:00
|
|
|
return {0, 0, "Unknown"};
|
2018-11-02 11:23:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-22 10:09:05 +02:00
|
|
|
const std::string waybar::modules::Battery::getAdapterStatus(uint8_t capacity) const {
|
2018-12-24 08:37:10 +01:00
|
|
|
if (!adapter_.empty()) {
|
|
|
|
bool online;
|
|
|
|
std::ifstream(adapter_ / "online") >> online;
|
|
|
|
if (capacity == 100) {
|
|
|
|
return "Full";
|
|
|
|
}
|
2019-05-14 15:43:57 +02:00
|
|
|
if (online) {
|
2019-10-03 10:37:37 +02:00
|
|
|
return "Plugged";
|
2019-05-14 15:43:57 +02:00
|
|
|
}
|
|
|
|
return "Discharging";
|
2018-12-24 08:37:10 +01:00
|
|
|
}
|
|
|
|
return "Unknown";
|
|
|
|
}
|
|
|
|
|
2019-05-21 19:36:14 +02:00
|
|
|
const std::string waybar::modules::Battery::formatTimeRemaining(float hoursRemaining) {
|
2020-11-27 15:23:37 +01:00
|
|
|
hoursRemaining = std::fabs(hoursRemaining);
|
2019-05-22 10:09:05 +02:00
|
|
|
uint16_t full_hours = static_cast<uint16_t>(hoursRemaining);
|
|
|
|
uint16_t minutes = static_cast<uint16_t>(60 * (hoursRemaining - full_hours));
|
2020-05-22 21:23:04 +02:00
|
|
|
auto format = std::string("{H} h {M} min");
|
2020-11-27 15:23:37 +01:00
|
|
|
if (full_hours == 0 && minutes == 0) {
|
|
|
|
// Migh as well not show "0h 0min"
|
|
|
|
return "";
|
|
|
|
}
|
2019-09-04 20:43:52 +02:00
|
|
|
if (config_["format-time"].isString()) {
|
|
|
|
format = config_["format-time"].asString();
|
|
|
|
}
|
|
|
|
return fmt::format(format, fmt::arg("H", full_hours), fmt::arg("M", minutes));
|
2019-05-21 19:36:14 +02:00
|
|
|
}
|
|
|
|
|
2019-04-18 17:52:00 +02:00
|
|
|
auto waybar::modules::Battery::update() -> void {
|
2019-05-21 19:36:14 +02:00
|
|
|
auto [capacity, time_remaining, status] = getInfos();
|
2018-12-24 12:17:07 +01:00
|
|
|
if (status == "Unknown") {
|
2019-05-21 19:35:39 +02:00
|
|
|
status = getAdapterStatus(capacity);
|
2018-12-24 12:17:07 +01:00
|
|
|
}
|
2019-02-22 11:35:26 +01:00
|
|
|
if (tooltipEnabled()) {
|
2019-05-21 19:36:14 +02:00
|
|
|
std::string tooltip_text;
|
|
|
|
if (time_remaining != 0) {
|
2019-05-22 10:09:05 +02:00
|
|
|
std::string time_to = std::string("Time to ") + ((time_remaining > 0) ? "empty" : "full");
|
|
|
|
tooltip_text = time_to + ": " + formatTimeRemaining(time_remaining);
|
2019-05-21 19:36:14 +02:00
|
|
|
} else {
|
2019-05-22 10:09:05 +02:00
|
|
|
tooltip_text = status;
|
2019-05-21 19:36:14 +02:00
|
|
|
}
|
|
|
|
label_.set_tooltip_text(tooltip_text);
|
2019-02-22 11:35:26 +01:00
|
|
|
}
|
2020-05-22 21:23:04 +02:00
|
|
|
// Transform to lowercase and replace space with dash
|
2019-12-30 13:55:49 +01:00
|
|
|
std::transform(status.begin(), status.end(), status.begin(), [](char ch) {
|
2020-05-22 21:23:04 +02:00
|
|
|
return ch == ' ' ? '-' : std::tolower(ch);
|
2019-12-30 13:55:49 +01:00
|
|
|
});
|
2018-11-02 11:23:29 +01:00
|
|
|
auto format = format_;
|
2019-05-13 11:36:34 +02:00
|
|
|
auto state = getState(capacity, true);
|
2019-05-14 15:43:57 +02:00
|
|
|
if (!old_status_.empty()) {
|
|
|
|
label_.get_style_context()->remove_class(old_status_);
|
|
|
|
}
|
2018-11-02 22:04:43 +01:00
|
|
|
label_.get_style_context()->add_class(status);
|
|
|
|
old_status_ = status;
|
|
|
|
if (!state.empty() && config_["format-" + status + "-" + state].isString()) {
|
|
|
|
format = config_["format-" + status + "-" + state].asString();
|
2018-11-02 22:50:01 +01:00
|
|
|
} else if (config_["format-" + status].isString()) {
|
2018-11-02 22:04:43 +01:00
|
|
|
format = config_["format-" + status].asString();
|
2018-11-02 22:50:01 +01:00
|
|
|
} else if (!state.empty() && config_["format-" + state].isString()) {
|
2018-11-02 11:23:29 +01:00
|
|
|
format = config_["format-" + state].asString();
|
|
|
|
}
|
|
|
|
if (format.empty()) {
|
|
|
|
event_box_.hide();
|
|
|
|
} else {
|
|
|
|
event_box_.show();
|
2020-05-22 21:23:04 +02:00
|
|
|
auto icons = std::vector<std::string>{status + "-" + state, status, state};
|
2019-05-22 10:09:05 +02:00
|
|
|
label_.set_markup(fmt::format(format,
|
|
|
|
fmt::arg("capacity", capacity),
|
2020-05-22 21:23:04 +02:00
|
|
|
fmt::arg("icon", getIcon(capacity, icons)),
|
2019-05-22 10:09:05 +02:00
|
|
|
fmt::arg("time", formatTimeRemaining(time_remaining))));
|
2018-08-08 23:54:58 +02:00
|
|
|
}
|
2020-04-12 18:30:21 +02:00
|
|
|
// Call parent update
|
|
|
|
ALabel::update();
|
2018-08-08 23:54:58 +02:00
|
|
|
}
|