Merge pull request #4616 from schmop/battery-plugging-instant-updates
2. Try: Make battery module update on plugging/unplugging again (refs #2519)
This commit is contained in:
commit
97eb60677e
|
|
@ -6,6 +6,7 @@
|
||||||
#if defined(__linux__)
|
#if defined(__linux__)
|
||||||
#include <sys/inotify.h>
|
#include <sys/inotify.h>
|
||||||
#endif
|
#endif
|
||||||
|
#include <sys/poll.h>
|
||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
|
|
@ -15,6 +16,7 @@
|
||||||
#include "ALabel.hpp"
|
#include "ALabel.hpp"
|
||||||
#include "bar.hpp"
|
#include "bar.hpp"
|
||||||
#include "util/sleeper_thread.hpp"
|
#include "util/sleeper_thread.hpp"
|
||||||
|
#include "util/udev_deleter.hpp"
|
||||||
|
|
||||||
namespace waybar::modules {
|
namespace waybar::modules {
|
||||||
|
|
||||||
|
|
@ -37,11 +39,12 @@ class Battery : public ALabel {
|
||||||
void setBarClass(std::string&);
|
void setBarClass(std::string&);
|
||||||
void processEvents(std::string& state, std::string& status, uint8_t capacity);
|
void processEvents(std::string& state, std::string& status, uint8_t capacity);
|
||||||
|
|
||||||
int global_watch;
|
|
||||||
std::map<fs::path, int> batteries_;
|
std::map<fs::path, int> batteries_;
|
||||||
|
std::unique_ptr<udev, util::UdevDeleter> udev_;
|
||||||
|
std::array<pollfd, 1> poll_fds_;
|
||||||
|
std::unique_ptr<udev_monitor, util::UdevMonitorDeleter> mon_;
|
||||||
fs::path adapter_;
|
fs::path adapter_;
|
||||||
int battery_watch_fd_;
|
int battery_watch_fd_;
|
||||||
int global_watch_fd_;
|
|
||||||
std::mutex battery_list_mutex_;
|
std::mutex battery_list_mutex_;
|
||||||
std::string old_status_;
|
std::string old_status_;
|
||||||
std::string last_event_;
|
std::string last_event_;
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,21 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <libudev.h>
|
||||||
|
|
||||||
|
namespace waybar::util {
|
||||||
|
struct UdevDeleter {
|
||||||
|
void operator()(udev *ptr) const { udev_unref(ptr); }
|
||||||
|
};
|
||||||
|
|
||||||
|
struct UdevDeviceDeleter {
|
||||||
|
void operator()(udev_device *ptr) const { udev_device_unref(ptr); }
|
||||||
|
};
|
||||||
|
|
||||||
|
struct UdevEnumerateDeleter {
|
||||||
|
void operator()(udev_enumerate *ptr) const { udev_enumerate_unref(ptr); }
|
||||||
|
};
|
||||||
|
|
||||||
|
struct UdevMonitorDeleter {
|
||||||
|
void operator()(udev_monitor *ptr) const { udev_monitor_unref(ptr); }
|
||||||
|
};
|
||||||
|
} // namespace waybar::util
|
||||||
|
|
@ -7,7 +7,10 @@
|
||||||
#if defined(__FreeBSD__)
|
#if defined(__FreeBSD__)
|
||||||
#include <sys/sysctl.h>
|
#include <sys/sysctl.h>
|
||||||
#endif
|
#endif
|
||||||
|
#include <libudev.h>
|
||||||
|
#include <poll.h>
|
||||||
#include <spdlog/spdlog.h>
|
#include <spdlog/spdlog.h>
|
||||||
|
#include <sys/signalfd.h>
|
||||||
|
|
||||||
waybar::modules::Battery::Battery(const std::string& id, const Bar& bar, const Json::Value& config)
|
waybar::modules::Battery::Battery(const std::string& id, const Bar& bar, const Json::Value& config)
|
||||||
: ALabel(config, "battery", id, "{capacity}%", 60), last_event_(""), bar_(bar) {
|
: ALabel(config, "battery", id, "{capacity}%", 60), last_event_(""), bar_(bar) {
|
||||||
|
|
@ -16,17 +19,19 @@ waybar::modules::Battery::Battery(const std::string& id, const Bar& bar, const J
|
||||||
if (battery_watch_fd_ == -1) {
|
if (battery_watch_fd_ == -1) {
|
||||||
throw std::runtime_error("Unable to listen batteries.");
|
throw std::runtime_error("Unable to listen batteries.");
|
||||||
}
|
}
|
||||||
|
udev_ = std::unique_ptr<udev, util::UdevDeleter>(udev_new());
|
||||||
global_watch_fd_ = inotify_init1(IN_CLOEXEC);
|
if (udev_ == nullptr) {
|
||||||
if (global_watch_fd_ == -1) {
|
throw std::runtime_error("udev_new failed");
|
||||||
throw std::runtime_error("Unable to listen batteries.");
|
|
||||||
}
|
}
|
||||||
|
mon_ = std::unique_ptr<udev_monitor, util::UdevMonitorDeleter>(
|
||||||
// Watch the directory for any added or removed batteries
|
udev_monitor_new_from_netlink(udev_.get(), "kernel"));
|
||||||
global_watch = inotify_add_watch(global_watch_fd_, data_dir_.c_str(), IN_CREATE | IN_DELETE);
|
if (mon_ == nullptr) {
|
||||||
if (global_watch < 0) {
|
throw std::runtime_error("udev monitor new failed");
|
||||||
throw std::runtime_error("Could not watch for battery plug/unplug");
|
|
||||||
}
|
}
|
||||||
|
if (udev_monitor_filter_add_match_subsystem_devtype(mon_.get(), "power_supply", nullptr) < 0) {
|
||||||
|
throw std::runtime_error("udev failed to add monitor filter");
|
||||||
|
}
|
||||||
|
udev_monitor_enable_receiving(mon_.get());
|
||||||
|
|
||||||
if (config_["weighted-average"].isBool()) weightedAverage_ = config_["weighted-average"].asBool();
|
if (config_["weighted-average"].isBool()) weightedAverage_ = config_["weighted-average"].asBool();
|
||||||
#endif
|
#endif
|
||||||
|
|
@ -38,11 +43,6 @@ waybar::modules::Battery::~Battery() {
|
||||||
#if defined(__linux__)
|
#if defined(__linux__)
|
||||||
std::lock_guard<std::mutex> guard(battery_list_mutex_);
|
std::lock_guard<std::mutex> guard(battery_list_mutex_);
|
||||||
|
|
||||||
if (global_watch >= 0) {
|
|
||||||
inotify_rm_watch(global_watch_fd_, global_watch);
|
|
||||||
}
|
|
||||||
close(global_watch_fd_);
|
|
||||||
|
|
||||||
for (auto it = batteries_.cbegin(), next_it = it; it != batteries_.cend(); it = next_it) {
|
for (auto it = batteries_.cbegin(), next_it = it; it != batteries_.cend(); it = next_it) {
|
||||||
++next_it;
|
++next_it;
|
||||||
auto watch_id = (*it).second;
|
auto watch_id = (*it).second;
|
||||||
|
|
@ -79,12 +79,18 @@ void waybar::modules::Battery::worker() {
|
||||||
dp.emit();
|
dp.emit();
|
||||||
};
|
};
|
||||||
thread_battery_update_ = [this] {
|
thread_battery_update_ = [this] {
|
||||||
struct inotify_event event = {0};
|
poll_fds_[0].revents = 0;
|
||||||
int nbytes = read(global_watch_fd_, &event, sizeof(event));
|
poll_fds_[0].events = POLLIN;
|
||||||
if (nbytes != sizeof(event) || event.mask & IN_IGNORED) {
|
poll_fds_[0].fd = udev_monitor_get_fd(mon_.get());
|
||||||
|
int ret = poll(poll_fds_.data(), poll_fds_.size(), -1);
|
||||||
|
if (ret < 0) {
|
||||||
thread_.stop();
|
thread_.stop();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
if ((poll_fds_[0].revents & POLLIN) != 0) {
|
||||||
|
signalfd_siginfo signal_info;
|
||||||
|
read(poll_fds_[0].fd, &signal_info, sizeof(signal_info));
|
||||||
|
}
|
||||||
refreshBatteries();
|
refreshBatteries();
|
||||||
dp.emit();
|
dp.emit();
|
||||||
};
|
};
|
||||||
|
|
@ -680,6 +686,7 @@ auto waybar::modules::Battery::update() -> void {
|
||||||
status = getAdapterStatus(capacity);
|
status = getAdapterStatus(capacity);
|
||||||
}
|
}
|
||||||
auto status_pretty = status;
|
auto status_pretty = status;
|
||||||
|
puts(status.c_str());
|
||||||
// Transform to lowercase and replace space with dash
|
// Transform to lowercase and replace space with dash
|
||||||
std::ranges::transform(status.begin(), status.end(), status.begin(),
|
std::ranges::transform(status.begin(), status.end(), status.begin(),
|
||||||
[](char ch) { return ch == ' ' ? '-' : std::tolower(ch); });
|
[](char ch) { return ch == ' ' ? '-' : std::tolower(ch); });
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,8 @@
|
||||||
#include <optional>
|
#include <optional>
|
||||||
#include <utility>
|
#include <utility>
|
||||||
|
|
||||||
|
#include "util/udev_deleter.hpp"
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
class FileDescriptor {
|
class FileDescriptor {
|
||||||
public:
|
public:
|
||||||
|
|
@ -29,22 +31,6 @@ class FileDescriptor {
|
||||||
int fd_;
|
int fd_;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct UdevDeleter {
|
|
||||||
void operator()(udev *ptr) { udev_unref(ptr); }
|
|
||||||
};
|
|
||||||
|
|
||||||
struct UdevDeviceDeleter {
|
|
||||||
void operator()(udev_device *ptr) { udev_device_unref(ptr); }
|
|
||||||
};
|
|
||||||
|
|
||||||
struct UdevEnumerateDeleter {
|
|
||||||
void operator()(udev_enumerate *ptr) { udev_enumerate_unref(ptr); }
|
|
||||||
};
|
|
||||||
|
|
||||||
struct UdevMonitorDeleter {
|
|
||||||
void operator()(udev_monitor *ptr) { udev_monitor_unref(ptr); }
|
|
||||||
};
|
|
||||||
|
|
||||||
void check_eq(int rc, int expected, const char *message = "eq, rc was: ") {
|
void check_eq(int rc, int expected, const char *message = "eq, rc was: ") {
|
||||||
if (rc != expected) {
|
if (rc != expected) {
|
||||||
throw std::runtime_error(fmt::format(fmt::runtime(message), rc));
|
throw std::runtime_error(fmt::format(fmt::runtime(message), rc));
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue