Skip to content

Commit bb2c67e

Browse files
authored
Revert "Make battery module update on plugging/unplugging again (refs #2519)"
1 parent dc64b35 commit bb2c67e

File tree

4 files changed

+35
-51
lines changed

4 files changed

+35
-51
lines changed

include/modules/battery.hpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,8 @@
55
#include <filesystem>
66
#if defined(__linux__)
77
#include <sys/inotify.h>
8-
#include "util/udev_deleter.hpp"
98
#endif
109

11-
#include <sys/poll.h>
12-
1310
#include <algorithm>
1411
#include <fstream>
1512
#include <string>
@@ -39,12 +36,11 @@ class Battery : public ALabel {
3936
const std::string formatTimeRemaining(float hoursRemaining);
4037
void setBarClass(std::string&);
4138

39+
int global_watch;
4240
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_;
4641
fs::path adapter_;
4742
int battery_watch_fd_;
43+
int global_watch_fd_;
4844
std::mutex battery_list_mutex_;
4945
std::string old_status_;
5046
bool warnFirstTime_{true};

include/util/udev_deleter.hpp

Lines changed: 0 additions & 21 deletions
This file was deleted.

src/modules/battery.cpp

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@
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>
1310
waybar::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); });

src/util/backlight_backend.cpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#include "util/backlight_backend.hpp"
2-
#include "util/udev_deleter.hpp"
32

43
#include <fmt/core.h>
54
#include <spdlog/spdlog.h>
@@ -30,6 +29,22 @@ class FileDescriptor {
3029
int fd_;
3130
};
3231

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+
3348
void check_eq(int rc, int expected, const char *message = "eq, rc was: ") {
3449
if (rc != expected) {
3550
throw std::runtime_error(fmt::format(fmt::runtime(message), rc));

0 commit comments

Comments
 (0)