refactor(rfkill): poll rfkill events from Glib main loop

Open rfkill device only once per module.
Remove rfkill threads and use `Glib::signal_io` as a more efficient way
to poll the rfkill device.
Handle runtime errors from rfkill and stop polling of the device instead
of crashing waybar.
This commit is contained in:
Aleksei Bavshin
2021-02-02 19:17:06 -08:00
parent 40f4dc9ecf
commit 38c29fc242
6 changed files with 64 additions and 67 deletions

View File

@ -1,17 +1,11 @@
#include "modules/bluetooth.hpp"
#include "util/rfkill.hpp"
#include <linux/rfkill.h>
#include <time.h>
waybar::modules::Bluetooth::Bluetooth(const std::string& id, const Json::Value& config)
: ALabel(config, "bluetooth", id, "{icon}", 10),
status_("disabled"),
rfkill_{RFKILL_TYPE_BLUETOOTH} {
rfkill_.on_update.connect(sigc::hide(sigc::mem_fun(*this, &Bluetooth::update)));
thread_ = [this] {
dp.emit();
rfkill_.waitForEvent();
};
intervall_thread_ = [this] {
auto now = std::chrono::system_clock::now();
auto timeout = std::chrono::floor<std::chrono::seconds>(now + interval_);
auto diff = std::chrono::seconds(timeout.time_since_epoch().count() % interval_.count());

View File

@ -212,18 +212,15 @@ void waybar::modules::Network::worker() {
thread_timer_.sleep_for(interval_);
};
#ifdef WANT_RFKILL
thread_rfkill_ = [this] {
rfkill_.waitForEvent();
{
std::lock_guard<std::mutex> lock(mutex_);
if (ifid_ > 0) {
getInfo();
dp.emit();
}
rfkill_.on_update.connect([this](auto &) {
std::lock_guard<std::mutex> lock(mutex_);
if (ifid_ > 0) {
getInfo();
dp.emit();
}
};
});
#else
spdlog::warn("Waybar has been built without rfkill support.");
spdlog::warn("Waybar has been built without rfkill support.");
#endif
thread_ = [this] {
std::array<struct epoll_event, EPOLL_MAX> events{};