From 89cb9673d42c0d4eb97e134a40cce16a76b4ec1f Mon Sep 17 00:00:00 2001 From: Marc <39213657+marcplustwo@users.noreply.github.com> Date: Wed, 22 Jan 2020 11:37:47 +0100 Subject: [PATCH] bluetooth module working --- include/modules/bluetooth.hpp | 6 ++++- include/util/rfkill.hpp | 2 +- src/modules/bluetooth.cpp | 44 ++++++++++++++++++++++------------- src/modules/network.cpp | 2 +- src/util/rfkill.cpp | 2 +- 5 files changed, 36 insertions(+), 20 deletions(-) diff --git a/include/modules/bluetooth.hpp b/include/modules/bluetooth.hpp index 4329861..d0fdce8 100644 --- a/include/modules/bluetooth.hpp +++ b/include/modules/bluetooth.hpp @@ -3,6 +3,9 @@ #include #include "ALabel.hpp" +#include +#include "util/sleeper_thread.hpp" + namespace waybar::modules { class Bluetooth : public ALabel { @@ -12,7 +15,8 @@ class Bluetooth : public ALabel { auto update() -> void; private: - ; + std::string status_; + util::SleeperThread thread_; }; } // namespace waybar::modules diff --git a/include/util/rfkill.hpp b/include/util/rfkill.hpp index 53f558c..f309b6f 100644 --- a/include/util/rfkill.hpp +++ b/include/util/rfkill.hpp @@ -2,7 +2,7 @@ #include -namespace waybar::util { +namespace waybar::util::rfkill { bool isDisabled(enum rfkill_type rfkill_type); diff --git a/src/modules/bluetooth.cpp b/src/modules/bluetooth.cpp index ab8bdda..dff42cd 100644 --- a/src/modules/bluetooth.cpp +++ b/src/modules/bluetooth.cpp @@ -2,28 +2,40 @@ #include "util/rfkill.hpp" #include +#include + waybar::modules::Bluetooth::Bluetooth(const std::string& id, const Json::Value& config) - : ALabel(config, "bluetooth", id, "{status}", 10) { - dp.emit(); + : ALabel(config, "bluetooth", id, "{status}", 10), + status_("disabled") { + thread_ = [this] { + dp.emit(); + auto now = std::chrono::system_clock::now(); + auto timeout = std::chrono::floor(now + interval_); + auto diff = std::chrono::seconds(timeout.time_since_epoch().count() % interval_.count()); + thread_.sleep_until(timeout - diff); + }; + //dp.emit(); } auto waybar::modules::Bluetooth::update() -> void { - auto text = "enabled"; - if (waybar::util::isDisabled(RFKILL_TYPE_BLUETOOTH)) { - text = "disabled"; + status_ = "enabled"; + if (waybar::util::rfkill::isDisabled(RFKILL_TYPE_BLUETOOTH)) { + status_ = "disabled"; } else { - text = "enabled"; + status_ = "enabled"; } - label_.set_markup(text); + label_.set_markup( + fmt::format(format_, fmt::arg("status", status_), fmt::arg("icon", getIcon(0, status_)))); + label_.get_style_context()->add_class(status_); - if (tooltipEnabled()) { - if (config_["tooltip-format"].isString()) { - auto tooltip_format = config_["tooltip-format"].asString(); - auto tooltip_text = fmt::format(tooltip_format, localtime); - label_.set_tooltip_text(tooltip_text); - } else { - label_.set_tooltip_text(text); - } - } + //if (tooltipEnabled()) { + //if (config_["tooltip-format"].isString()) { + //auto tooltip_format = config_["tooltip-format"].asString(); + ////auto tooltip_text = fmt::format(tooltip_format, localtime); + //label_.set_tooltip_text(tooltip_text); + //} else { + //label_.set_tooltip_text(text); + //} + //} } diff --git a/src/modules/network.cpp b/src/modules/network.cpp index 2639a05..636db67 100644 --- a/src/modules/network.cpp +++ b/src/modules/network.cpp @@ -223,7 +223,7 @@ void waybar::modules::Network::worker() { const std::string waybar::modules::Network::getNetworkState() const { if (ifid_ == -1) { - if (waybar::util::isDisabled(RFKILL_TYPE_WLAN)) + if (waybar::util::rfkill::isDisabled(RFKILL_TYPE_WLAN)) return "disabled"; return "disconnected"; } diff --git a/src/util/rfkill.cpp b/src/util/rfkill.cpp index d1b38ec..2b0e42d 100644 --- a/src/util/rfkill.cpp +++ b/src/util/rfkill.cpp @@ -5,7 +5,7 @@ #include #include -bool waybar::util::isDisabled(enum rfkill_type rfkill_type) { +bool waybar::util::rfkill::isDisabled(enum rfkill_type rfkill_type) { struct rfkill_event event; ssize_t len; int fd;