mirror of
https://github.com/rad4day/Waybar.git
synced 2023-12-21 10:22:59 +01:00
Merge pull request #1015 from alebastr/rfkill-events
rfkill code refactoring
This commit is contained in:
@ -1,45 +1,25 @@
|
||||
#include "modules/bluetooth.hpp"
|
||||
#include "util/rfkill.hpp"
|
||||
#include <linux/rfkill.h>
|
||||
#include <time.h>
|
||||
|
||||
#include <fmt/format.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} {
|
||||
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());
|
||||
thread_.sleep_until(timeout - diff);
|
||||
dp.emit();
|
||||
};
|
||||
: ALabel(config, "bluetooth", id, "{icon}", 10), rfkill_{RFKILL_TYPE_BLUETOOTH} {
|
||||
rfkill_.on_update.connect(sigc::hide(sigc::mem_fun(*this, &Bluetooth::update)));
|
||||
}
|
||||
|
||||
auto waybar::modules::Bluetooth::update() -> void {
|
||||
if (rfkill_.getState()) {
|
||||
status_ = "disabled";
|
||||
} else {
|
||||
status_ = "enabled";
|
||||
}
|
||||
std::string status = rfkill_.getState() ? "disabled" : "enabled";
|
||||
|
||||
label_.set_markup(
|
||||
fmt::format(
|
||||
format_,
|
||||
fmt::arg("status", status_),
|
||||
fmt::arg("icon", getIcon(0, status_))));
|
||||
fmt::format(format_, fmt::arg("status", status), fmt::arg("icon", getIcon(0, status))));
|
||||
|
||||
if (tooltipEnabled()) {
|
||||
if (config_["tooltip-format"].isString()) {
|
||||
auto tooltip_format = config_["tooltip-format"].asString();
|
||||
auto tooltip_text = fmt::format(tooltip_format, status_);
|
||||
auto tooltip_text = fmt::format(tooltip_format, status);
|
||||
label_.set_tooltip_text(tooltip_text);
|
||||
} else {
|
||||
label_.set_tooltip_text(status_);
|
||||
label_.set_tooltip_text(status);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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 &) {
|
||||
/* If we are here, it's likely that the network thread already holds the mutex and will be
|
||||
* holding it for a next few seconds.
|
||||
* Let's delegate the update to the timer thread instead of blocking the main thread.
|
||||
*/
|
||||
thread_timer_.wake_up();
|
||||
});
|
||||
#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{};
|
||||
|
Reference in New Issue
Block a user