bluetooth module working

This commit is contained in:
Marc 2020-01-22 11:37:47 +01:00
parent f0dbd8b78d
commit 89cb9673d4
5 changed files with 36 additions and 20 deletions

View File

@ -3,6 +3,9 @@
#include <fmt/format.h> #include <fmt/format.h>
#include "ALabel.hpp" #include "ALabel.hpp"
#include <fmt/chrono.h>
#include "util/sleeper_thread.hpp"
namespace waybar::modules { namespace waybar::modules {
class Bluetooth : public ALabel { class Bluetooth : public ALabel {
@ -12,7 +15,8 @@ class Bluetooth : public ALabel {
auto update() -> void; auto update() -> void;
private: private:
; std::string status_;
util::SleeperThread thread_;
}; };
} // namespace waybar::modules } // namespace waybar::modules

View File

@ -2,7 +2,7 @@
#include <linux/rfkill.h> #include <linux/rfkill.h>
namespace waybar::util { namespace waybar::util::rfkill {
bool isDisabled(enum rfkill_type rfkill_type); bool isDisabled(enum rfkill_type rfkill_type);

View File

@ -2,28 +2,40 @@
#include "util/rfkill.hpp" #include "util/rfkill.hpp"
#include <linux/rfkill.h> #include <linux/rfkill.h>
#include <time.h>
waybar::modules::Bluetooth::Bluetooth(const std::string& id, const Json::Value& config) waybar::modules::Bluetooth::Bluetooth(const std::string& id, const Json::Value& config)
: ALabel(config, "bluetooth", id, "{status}", 10) { : ALabel(config, "bluetooth", id, "{status}", 10),
status_("disabled") {
thread_ = [this] {
dp.emit(); dp.emit();
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();
} }
auto waybar::modules::Bluetooth::update() -> void { auto waybar::modules::Bluetooth::update() -> void {
auto text = "enabled"; status_ = "enabled";
if (waybar::util::isDisabled(RFKILL_TYPE_BLUETOOTH)) { if (waybar::util::rfkill::isDisabled(RFKILL_TYPE_BLUETOOTH)) {
text = "disabled"; status_ = "disabled";
} else { } 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 (tooltipEnabled()) {
if (config_["tooltip-format"].isString()) { //if (config_["tooltip-format"].isString()) {
auto tooltip_format = config_["tooltip-format"].asString(); //auto tooltip_format = config_["tooltip-format"].asString();
auto tooltip_text = fmt::format(tooltip_format, localtime); ////auto tooltip_text = fmt::format(tooltip_format, localtime);
label_.set_tooltip_text(tooltip_text); //label_.set_tooltip_text(tooltip_text);
} else { //} else {
label_.set_tooltip_text(text); //label_.set_tooltip_text(text);
} //}
} //}
} }

View File

@ -223,7 +223,7 @@ void waybar::modules::Network::worker() {
const std::string waybar::modules::Network::getNetworkState() const { const std::string waybar::modules::Network::getNetworkState() const {
if (ifid_ == -1) { if (ifid_ == -1) {
if (waybar::util::isDisabled(RFKILL_TYPE_WLAN)) if (waybar::util::rfkill::isDisabled(RFKILL_TYPE_WLAN))
return "disabled"; return "disabled";
return "disconnected"; return "disconnected";
} }

View File

@ -5,7 +5,7 @@
#include <fcntl.h> #include <fcntl.h>
#include <cerrno> #include <cerrno>
bool waybar::util::isDisabled(enum rfkill_type rfkill_type) { bool waybar::util::rfkill::isDisabled(enum rfkill_type rfkill_type) {
struct rfkill_event event; struct rfkill_event event;
ssize_t len; ssize_t len;
int fd; int fd;