Skip to content

Commit

Permalink
PoC: Use udev polling of netlink kernel events for battery plugging e…
Browse files Browse the repository at this point in the history
…vents
  • Loading branch information
schmop committed Jul 24, 2024
1 parent 003dd3a commit a519c0c
Showing 1 changed file with 35 additions and 5 deletions.
40 changes: 35 additions & 5 deletions src/modules/battery.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include <sys/sysctl.h>
#endif
#include <spdlog/spdlog.h>
#include <libudev.h>
#include <poll.h>

#include <iostream>
waybar::modules::Battery::Battery(const std::string& id, const Bar& bar, const Json::Value& config)
Expand Down Expand Up @@ -74,12 +76,40 @@ void waybar::modules::Battery::worker() {
dp.emit();
};
thread_battery_update_ = [this] {
struct inotify_event event = {0};
int nbytes = read(global_watch_fd_, &event, sizeof(event));
if (nbytes != sizeof(event) || event.mask & IN_IGNORED) {
thread_.stop();
return;
struct udev *udev;
struct udev_monitor *mon;
struct pollfd fds[1];
int fd;
udev = udev_new();
puts("Lets start udev shit!");
if (udev == NULL) {
fputs("Udev failed", stderr);
thread_.stop();
return;
}
mon = udev_monitor_new_from_netlink(udev, "kernel");
udev_monitor_filter_add_match_subsystem_devtype(mon, "power_supply", NULL);
udev_monitor_enable_receiving(mon);
fd = udev_monitor_get_fd(mon);
fds[0].fd = fd;
fds[0].events = POLLIN;
fds[0].revents = 0;
puts("Start listening");
if (poll(fds, 1, -1) > 0) {
puts("Got signal!");
struct udev_device *const dev = udev_monitor_receive_device(mon);
if (dev != NULL) {
puts(udev_device_get_sysname(dev));
udev_device_unref(dev);
}
else {
fputs("udev_monitor_receive_device() failed\n", stderr);
thread_.stop();
return;
}
}
puts("Signaling ends");
udev_unref(udev);
refreshBatteries();
dp.emit();
};
Expand Down

0 comments on commit a519c0c

Please sign in to comment.