55#include < sys/sysctl.h>
66#endif
77#include < spdlog/spdlog.h>
8- #include < libudev.h>
9- #include < poll.h>
10- #include < sys/signalfd.h>
118
129#include < iostream>
1310waybar::modules::Battery::Battery (const std::string& id, const Bar& bar, const Json::Value& config)
@@ -17,18 +14,17 @@ waybar::modules::Battery::Battery(const std::string& id, const Bar& bar, const J
1714 if (battery_watch_fd_ == -1 ) {
1815 throw std::runtime_error (" Unable to listen batteries." );
1916 }
20- udev_ = std::unique_ptr<udev, util::UdevDeleter>(udev_new ());
21- if (udev_ == nullptr ) {
22- throw std::runtime_error (" udev_new failed" );
23- }
24- mon_ = std::unique_ptr<udev_monitor, util::UdevMonitorDeleter>(udev_monitor_new_from_netlink (udev_.get (), " kernel" ));
25- if (mon_ == nullptr ) {
26- throw std::runtime_error (" udev monitor new failed" );
17+
18+ global_watch_fd_ = inotify_init1 (IN_CLOEXEC);
19+ if (global_watch_fd_ == -1 ) {
20+ throw std::runtime_error (" Unable to listen batteries." );
2721 }
28- if (udev_monitor_filter_add_match_subsystem_devtype (mon_.get (), " power_supply" , nullptr ) < 0 ) {
29- throw std::runtime_error (" udev failed to add monitor filter" );
22+
23+ // Watch the directory for any added or removed batteries
24+ global_watch = inotify_add_watch (global_watch_fd_, data_dir_.c_str (), IN_CREATE | IN_DELETE);
25+ if (global_watch < 0 ) {
26+ throw std::runtime_error (" Could not watch for battery plug/unplug" );
3027 }
31- udev_monitor_enable_receiving (mon_.get ());
3228#endif
3329 worker ();
3430}
@@ -37,6 +33,11 @@ waybar::modules::Battery::~Battery() {
3733#if defined(__linux__)
3834 std::lock_guard<std::mutex> guard (battery_list_mutex_);
3935
36+ if (global_watch >= 0 ) {
37+ inotify_rm_watch (global_watch_fd_, global_watch);
38+ }
39+ close (global_watch_fd_);
40+
4041 for (auto it = batteries_.cbegin (), next_it = it; it != batteries_.cend (); it = next_it) {
4142 ++next_it;
4243 auto watch_id = (*it).second ;
@@ -73,18 +74,12 @@ void waybar::modules::Battery::worker() {
7374 dp.emit ();
7475 };
7576 thread_battery_update_ = [this ] {
76- poll_fds_[0 ].revents = 0 ;
77- poll_fds_[0 ].events = POLLIN;
78- poll_fds_[0 ].fd = udev_monitor_get_fd (mon_.get ());
79- int ret = poll (poll_fds_.data (), poll_fds_.size (), -1 );
80- if (ret < 0 ) {
77+ struct inotify_event event = {0 };
78+ int nbytes = read (global_watch_fd_, &event, sizeof (event));
79+ if (nbytes != sizeof (event) || event.mask & IN_IGNORED) {
8180 thread_.stop ();
8281 return ;
8382 }
84- if ((poll_fds_[0 ].revents & POLLIN) != 0 ) {
85- signalfd_siginfo signal_info;
86- read (poll_fds_[0 ].fd , &signal_info, sizeof (signal_info));
87- }
8883 refreshBatteries ();
8984 dp.emit ();
9085 };
@@ -673,7 +668,6 @@ auto waybar::modules::Battery::update() -> void {
673668 status = getAdapterStatus (capacity);
674669 }
675670 auto status_pretty = status;
676- puts (status.c_str ());
677671 // Transform to lowercase and replace space with dash
678672 std::transform (status.begin (), status.end (), status.begin (),
679673 [](char ch) { return ch == ' ' ? ' -' : std::tolower (ch); });
0 commit comments