Skip to content

Commit 97eb606

Browse files
authored
Merge pull request #4616 from schmop/battery-plugging-instant-updates
2. Try: Make battery module update on plugging/unplugging again (refs #2519)
2 parents 3d0b942 + c3d29b1 commit 97eb606

File tree

4 files changed

+52
-35
lines changed

4 files changed

+52
-35
lines changed

include/modules/battery.hpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#if defined(__linux__)
77
#include <sys/inotify.h>
88
#endif
9+
#include <sys/poll.h>
910

1011
#include <algorithm>
1112
#include <fstream>
@@ -15,6 +16,7 @@
1516
#include "ALabel.hpp"
1617
#include "bar.hpp"
1718
#include "util/sleeper_thread.hpp"
19+
#include "util/udev_deleter.hpp"
1820

1921
namespace waybar::modules {
2022

@@ -37,11 +39,12 @@ class Battery : public ALabel {
3739
void setBarClass(std::string&);
3840
void processEvents(std::string& state, std::string& status, uint8_t capacity);
3941

40-
int global_watch;
4142
std::map<fs::path, int> batteries_;
43+
std::unique_ptr<udev, util::UdevDeleter> udev_;
44+
std::array<pollfd, 1> poll_fds_;
45+
std::unique_ptr<udev_monitor, util::UdevMonitorDeleter> mon_;
4246
fs::path adapter_;
4347
int battery_watch_fd_;
44-
int global_watch_fd_;
4548
std::mutex battery_list_mutex_;
4649
std::string old_status_;
4750
std::string last_event_;

include/util/udev_deleter.hpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#pragma once
2+
3+
#include <libudev.h>
4+
5+
namespace waybar::util {
6+
struct UdevDeleter {
7+
void operator()(udev *ptr) const { udev_unref(ptr); }
8+
};
9+
10+
struct UdevDeviceDeleter {
11+
void operator()(udev_device *ptr) const { udev_device_unref(ptr); }
12+
};
13+
14+
struct UdevEnumerateDeleter {
15+
void operator()(udev_enumerate *ptr) const { udev_enumerate_unref(ptr); }
16+
};
17+
18+
struct UdevMonitorDeleter {
19+
void operator()(udev_monitor *ptr) const { udev_monitor_unref(ptr); }
20+
};
21+
} // namespace waybar::util

src/modules/battery.cpp

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@
77
#if defined(__FreeBSD__)
88
#include <sys/sysctl.h>
99
#endif
10+
#include <libudev.h>
11+
#include <poll.h>
1012
#include <spdlog/spdlog.h>
13+
#include <sys/signalfd.h>
1114

1215
waybar::modules::Battery::Battery(const std::string& id, const Bar& bar, const Json::Value& config)
1316
: 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
1619
if (battery_watch_fd_ == -1) {
1720
throw std::runtime_error("Unable to listen batteries.");
1821
}
19-
20-
global_watch_fd_ = inotify_init1(IN_CLOEXEC);
21-
if (global_watch_fd_ == -1) {
22-
throw std::runtime_error("Unable to listen batteries.");
22+
udev_ = std::unique_ptr<udev, util::UdevDeleter>(udev_new());
23+
if (udev_ == nullptr) {
24+
throw std::runtime_error("udev_new failed");
2325
}
24-
25-
// Watch the directory for any added or removed batteries
26-
global_watch = inotify_add_watch(global_watch_fd_, data_dir_.c_str(), IN_CREATE | IN_DELETE);
27-
if (global_watch < 0) {
28-
throw std::runtime_error("Could not watch for battery plug/unplug");
26+
mon_ = std::unique_ptr<udev_monitor, util::UdevMonitorDeleter>(
27+
udev_monitor_new_from_netlink(udev_.get(), "kernel"));
28+
if (mon_ == nullptr) {
29+
throw std::runtime_error("udev monitor new failed");
2930
}
31+
if (udev_monitor_filter_add_match_subsystem_devtype(mon_.get(), "power_supply", nullptr) < 0) {
32+
throw std::runtime_error("udev failed to add monitor filter");
33+
}
34+
udev_monitor_enable_receiving(mon_.get());
3035

3136
if (config_["weighted-average"].isBool()) weightedAverage_ = config_["weighted-average"].asBool();
3237
#endif
@@ -38,11 +43,6 @@ waybar::modules::Battery::~Battery() {
3843
#if defined(__linux__)
3944
std::lock_guard<std::mutex> guard(battery_list_mutex_);
4045

41-
if (global_watch >= 0) {
42-
inotify_rm_watch(global_watch_fd_, global_watch);
43-
}
44-
close(global_watch_fd_);
45-
4646
for (auto it = batteries_.cbegin(), next_it = it; it != batteries_.cend(); it = next_it) {
4747
++next_it;
4848
auto watch_id = (*it).second;
@@ -79,12 +79,18 @@ void waybar::modules::Battery::worker() {
7979
dp.emit();
8080
};
8181
thread_battery_update_ = [this] {
82-
struct inotify_event event = {0};
83-
int nbytes = read(global_watch_fd_, &event, sizeof(event));
84-
if (nbytes != sizeof(event) || event.mask & IN_IGNORED) {
82+
poll_fds_[0].revents = 0;
83+
poll_fds_[0].events = POLLIN;
84+
poll_fds_[0].fd = udev_monitor_get_fd(mon_.get());
85+
int ret = poll(poll_fds_.data(), poll_fds_.size(), -1);
86+
if (ret < 0) {
8587
thread_.stop();
8688
return;
8789
}
90+
if ((poll_fds_[0].revents & POLLIN) != 0) {
91+
signalfd_siginfo signal_info;
92+
read(poll_fds_[0].fd, &signal_info, sizeof(signal_info));
93+
}
8894
refreshBatteries();
8995
dp.emit();
9096
};
@@ -680,6 +686,7 @@ auto waybar::modules::Battery::update() -> void {
680686
status = getAdapterStatus(capacity);
681687
}
682688
auto status_pretty = status;
689+
puts(status.c_str());
683690
// Transform to lowercase and replace space with dash
684691
std::ranges::transform(status.begin(), status.end(), status.begin(),
685692
[](char ch) { return ch == ' ' ? '-' : std::tolower(ch); });

src/util/backlight_backend.cpp

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
#include <optional>
99
#include <utility>
1010

11+
#include "util/udev_deleter.hpp"
12+
1113
namespace {
1214
class FileDescriptor {
1315
public:
@@ -29,22 +31,6 @@ class FileDescriptor {
2931
int fd_;
3032
};
3133

32-
struct UdevDeleter {
33-
void operator()(udev *ptr) { udev_unref(ptr); }
34-
};
35-
36-
struct UdevDeviceDeleter {
37-
void operator()(udev_device *ptr) { udev_device_unref(ptr); }
38-
};
39-
40-
struct UdevEnumerateDeleter {
41-
void operator()(udev_enumerate *ptr) { udev_enumerate_unref(ptr); }
42-
};
43-
44-
struct UdevMonitorDeleter {
45-
void operator()(udev_monitor *ptr) { udev_monitor_unref(ptr); }
46-
};
47-
4834
void check_eq(int rc, int expected, const char *message = "eq, rc was: ") {
4935
if (rc != expected) {
5036
throw std::runtime_error(fmt::format(fmt::runtime(message), rc));

0 commit comments

Comments
 (0)