Support hotplugging of batteries

Refresh the list of batteries on update to handle hotplug correctly.
Probably fixes #490.
This commit is contained in:
Pedro Côrte-Real 2020-11-27 13:56:04 +00:00
parent 908fa2c6c2
commit 89ca155c43
2 changed files with 58 additions and 17 deletions

View File

@ -31,16 +31,16 @@ class Battery : public ALabel {
private: private:
static inline const fs::path data_dir_ = "/sys/class/power_supply/"; static inline const fs::path data_dir_ = "/sys/class/power_supply/";
void getBatteries(); void refreshBatteries();
void worker(); void worker();
const std::string getAdapterStatus(uint8_t capacity) const; const std::string getAdapterStatus(uint8_t capacity) const;
const std::tuple<uint8_t, float, std::string> getInfos() const; const std::tuple<uint8_t, float, std::string> getInfos() const;
const std::string formatTimeRemaining(float hoursRemaining); const std::string formatTimeRemaining(float hoursRemaining);
std::vector<fs::path> batteries_; int global_watch;
std::map<fs::path,int> batteries_;
fs::path adapter_; fs::path adapter_;
int fd_; int fd_;
std::vector<int> wds_;
std::string old_status_; std::string old_status_;
util::SleeperThread thread_; util::SleeperThread thread_;

View File

@ -4,23 +4,31 @@
waybar::modules::Battery::Battery(const std::string& id, const Json::Value& config) waybar::modules::Battery::Battery(const std::string& id, const Json::Value& config)
: ALabel(config, "battery", id, "{capacity}%", 60) { : ALabel(config, "battery", id, "{capacity}%", 60) {
getBatteries();
fd_ = inotify_init1(IN_CLOEXEC); fd_ = inotify_init1(IN_CLOEXEC);
if (fd_ == -1) { if (fd_ == -1) {
throw std::runtime_error("Unable to listen batteries."); throw std::runtime_error("Unable to listen batteries.");
} }
for (auto const& bat : batteries_) {
auto wd = inotify_add_watch(fd_, (bat / "uevent").c_str(), IN_ACCESS); // Watch the directory for any added or removed batteries
if (wd != -1) { global_watch = inotify_add_watch(fd_, data_dir_.c_str(), IN_CREATE | IN_DELETE);
wds_.push_back(wd); if (global_watch < 0) {
} throw std::runtime_error("Could not watch for battery plug/unplug");
} }
refreshBatteries();
worker(); worker();
} }
waybar::modules::Battery::~Battery() { waybar::modules::Battery::~Battery() {
for (auto wd : wds_) { if (global_watch >= 0) {
inotify_rm_watch(fd_, wd); inotify_rm_watch(fd_, global_watch);
}
for (auto it = batteries_.cbegin(); it != batteries_.cend(); it++) {
auto watch_id = (*it).second;
if (watch_id >= 0) {
inotify_rm_watch(fd_, watch_id);
}
batteries_.erase(it);
} }
close(fd_); close(fd_);
} }
@ -43,7 +51,13 @@ void waybar::modules::Battery::worker() {
}; };
} }
void waybar::modules::Battery::getBatteries() { void waybar::modules::Battery::refreshBatteries() {
// 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;
}
try { try {
for (auto& node : fs::directory_iterator(data_dir_)) { for (auto& node : fs::directory_iterator(data_dir_)) {
if (!fs::is_directory(node)) { if (!fs::is_directory(node)) {
@ -58,7 +72,17 @@ void waybar::modules::Battery::getBatteries() {
std::ifstream(node.path() / "type") >> type; std::ifstream(node.path() / "type") >> type;
if (!type.compare("Battery")){ if (!type.compare("Battery")){
batteries_.push_back(node.path()); 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");
auto wd = inotify_add_watch(fd_, event_path.c_str(), IN_ACCESS);
if (wd < 0) {
throw std::runtime_error("Could not watch events for " + node.path().string());
}
batteries_[node.path()] = wd;
}
} }
} }
auto adap_defined = config_["adapter"].isString(); auto adap_defined = config_["adapter"].isString();
@ -76,6 +100,17 @@ void waybar::modules::Battery::getBatteries() {
} }
throw std::runtime_error("No batteries."); throw std::runtime_error("No batteries.");
} }
// 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) {
inotify_rm_watch(fd_, watch_id);
}
batteries_.erase(check.first);
}
}
} }
const std::tuple<uint8_t, float, std::string> waybar::modules::Battery::getInfos() const { const std::tuple<uint8_t, float, std::string> waybar::modules::Battery::getInfos() const {
@ -84,7 +119,8 @@ const std::tuple<uint8_t, float, std::string> waybar::modules::Battery::getInfos
uint32_t total_energy = 0; // μWh uint32_t total_energy = 0; // μWh
uint32_t total_energy_full = 0; uint32_t total_energy_full = 0;
std::string status = "Unknown"; std::string status = "Unknown";
for (auto const& bat : batteries_) { for (auto const& item : batteries_) {
auto bat = item.first;
uint32_t power_now; uint32_t power_now;
uint32_t energy_full; uint32_t energy_full;
uint32_t energy_now; uint32_t energy_now;
@ -175,6 +211,11 @@ const std::string waybar::modules::Battery::formatTimeRemaining(float hoursRemai
} }
auto waybar::modules::Battery::update() -> void { auto waybar::modules::Battery::update() -> void {
// Make sure we have the correct set of batteries, in case of hotplug
// TODO: split the global watch into it's own event and only run the refresh
// when there's been a CREATE/DELETE event
refreshBatteries();
auto [capacity, time_remaining, status] = getInfos(); auto [capacity, time_remaining, status] = getInfos();
if (status == "Unknown") { if (status == "Unknown") {
status = getAdapterStatus(capacity); status = getAdapterStatus(capacity);