From 2c4369a65321901fb70b5d9327736a9aaa6e9bd0 Mon Sep 17 00:00:00 2001 From: Marc <39213657+marcplustwo@users.noreply.github.com> Date: Tue, 21 Jan 2020 15:46:08 +0100 Subject: [PATCH 1/4] add basis for bluetooth module implementation --- include/factory.hpp | 1 + meson.build | 1 + src/factory.cpp | 3 +++ 3 files changed, 5 insertions(+) diff --git a/include/factory.hpp b/include/factory.hpp index 7d4d14e3..b14b9988 100644 --- a/include/factory.hpp +++ b/include/factory.hpp @@ -32,6 +32,7 @@ #include "bar.hpp" #include "modules/custom.hpp" #include "modules/temperature.hpp" +#include "modules/bluetooth.hpp" namespace waybar { diff --git a/meson.build b/meson.build index a099ad2b..0032c915 100644 --- a/meson.build +++ b/meson.build @@ -88,6 +88,7 @@ src_files = files( 'src/ALabel.cpp', 'src/modules/memory.cpp', 'src/modules/battery.cpp', + 'src/modules/bluetooth.cpp', 'src/modules/clock.cpp', 'src/modules/custom.cpp', 'src/modules/cpu.cpp', diff --git a/src/factory.cpp b/src/factory.cpp index 8f7cea75..16a6903a 100644 --- a/src/factory.cpp +++ b/src/factory.cpp @@ -66,6 +66,9 @@ waybar::AModule* waybar::Factory::makeModule(const std::string& name) const { if (ref == "temperature") { return new waybar::modules::Temperature(id, config_[name]); } + if (ref == "bluetooth") { + return new waybar::modules::Bluetooth(id, config_[name]); + } if (ref.compare(0, 7, "custom/") == 0 && ref.size() > 7) { return new waybar::modules::Custom(ref.substr(7), id, config_[name]); } From 626af1ddc15df55b92bd38e03977089922e34f40 Mon Sep 17 00:00:00 2001 From: Marc <39213657+marcplustwo@users.noreply.github.com> Date: Tue, 21 Jan 2020 17:04:54 +0100 Subject: [PATCH 2/4] add rudimentary bluetooth module functionality --- include/modules/bluetooth.hpp | 18 ++++++++++++ include/util/rfkill.hpp | 55 +++++++++++++++++++++++++++++++++++ src/modules/bluetooth.cpp | 29 ++++++++++++++++++ 3 files changed, 102 insertions(+) create mode 100644 include/modules/bluetooth.hpp create mode 100644 include/util/rfkill.hpp create mode 100644 src/modules/bluetooth.cpp diff --git a/include/modules/bluetooth.hpp b/include/modules/bluetooth.hpp new file mode 100644 index 00000000..43298618 --- /dev/null +++ b/include/modules/bluetooth.hpp @@ -0,0 +1,18 @@ +#pragma once + +#include +#include "ALabel.hpp" + +namespace waybar::modules { + +class Bluetooth : public ALabel { + public: + Bluetooth(const std::string&, const Json::Value&); + ~Bluetooth() = default; + auto update() -> void; + + private: + ; +}; + +} // namespace waybar::modules diff --git a/include/util/rfkill.hpp b/include/util/rfkill.hpp new file mode 100644 index 00000000..9ef68923 --- /dev/null +++ b/include/util/rfkill.hpp @@ -0,0 +1,55 @@ +#pragma once + +#include +#include +//#include +#include +#include +#include + +namespace waybar::util { + +bool isDisabled(enum rfkill_type rfkill_type) { + struct rfkill_event event; + ssize_t len; + int fd; + int ret; + ret = false; + + fd = open("/dev/rfkill", O_RDONLY); + if (fd < 0) { + perror("Can't open RFKILL control device"); + return false; + } + + if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0) { + perror("Can't set RFKILL control device to non-blocking"); + close(fd); + return false; + } + + while(true) { + len = read(fd, &event, sizeof(event)); + if (len < 0) { + if (errno == EAGAIN) + return 1; + perror("Reading of RFKILL events failed"); + return false; + } + + if (len != RFKILL_EVENT_SIZE_V1) { + fprintf(stderr, "Wrong size of RFKILL event\n"); + return false; + } + + if(event.type == rfkill_type) { + ret = event.soft || event.hard; + break; + } + } + + close(fd); + return ret; +} + +} // namespace waybar::util diff --git a/src/modules/bluetooth.cpp b/src/modules/bluetooth.cpp new file mode 100644 index 00000000..ab8bddae --- /dev/null +++ b/src/modules/bluetooth.cpp @@ -0,0 +1,29 @@ +#include "modules/bluetooth.hpp" +#include "util/rfkill.hpp" +#include + +waybar::modules::Bluetooth::Bluetooth(const std::string& id, const Json::Value& config) + : ALabel(config, "bluetooth", id, "{status}", 10) { + dp.emit(); +} + +auto waybar::modules::Bluetooth::update() -> void { + auto text = "enabled"; + if (waybar::util::isDisabled(RFKILL_TYPE_BLUETOOTH)) { + text = "disabled"; + } else { + text = "enabled"; + } + + label_.set_markup(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); + } + } +} From f0dbd8b78ddacee555cabb0258d758ff670db122 Mon Sep 17 00:00:00 2001 From: Marc <39213657+marcplustwo@users.noreply.github.com> Date: Tue, 21 Jan 2020 17:48:45 +0100 Subject: [PATCH 3/4] properly structure rfkill util --- include/modules/network.hpp | 2 -- include/util/rfkill.hpp | 48 +------------------------------ meson.build | 3 +- src/modules/network.cpp | 57 ++++--------------------------------- src/util/rfkill.cpp | 49 +++++++++++++++++++++++++++++++ 5 files changed, 57 insertions(+), 102 deletions(-) create mode 100644 src/util/rfkill.cpp diff --git a/include/modules/network.hpp b/include/modules/network.hpp index f0d2e8e7..91e4ddb2 100644 --- a/include/modules/network.hpp +++ b/include/modules/network.hpp @@ -9,7 +9,6 @@ #include #include #include -#include #include "ALabel.hpp" #include "util/sleeper_thread.hpp" @@ -46,7 +45,6 @@ class Network : public ALabel { const std::string getNetworkState() const; void clearIface(); bool wildcardMatch(const std::string& pattern, const std::string& text) const; - bool isDisabled(enum rfkill_type rfkill_type) const; int ifid_; sa_family_t family_; diff --git a/include/util/rfkill.hpp b/include/util/rfkill.hpp index 9ef68923..53f558c9 100644 --- a/include/util/rfkill.hpp +++ b/include/util/rfkill.hpp @@ -1,55 +1,9 @@ #pragma once #include -#include -//#include -#include -#include -#include namespace waybar::util { -bool isDisabled(enum rfkill_type rfkill_type) { - struct rfkill_event event; - ssize_t len; - int fd; - int ret; - ret = false; - - fd = open("/dev/rfkill", O_RDONLY); - if (fd < 0) { - perror("Can't open RFKILL control device"); - return false; - } - - if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0) { - perror("Can't set RFKILL control device to non-blocking"); - close(fd); - return false; - } - - while(true) { - len = read(fd, &event, sizeof(event)); - if (len < 0) { - if (errno == EAGAIN) - return 1; - perror("Reading of RFKILL events failed"); - return false; - } - - if (len != RFKILL_EVENT_SIZE_V1) { - fprintf(stderr, "Wrong size of RFKILL event\n"); - return false; - } - - if(event.type == rfkill_type) { - ret = event.soft || event.hard; - break; - } - } - - close(fd); - return ret; -} +bool isDisabled(enum rfkill_type rfkill_type); } // namespace waybar::util diff --git a/meson.build b/meson.build index 0032c915..8f29d10c 100644 --- a/meson.build +++ b/meson.build @@ -97,7 +97,8 @@ src_files = files( 'src/modules/temperature.cpp', 'src/main.cpp', 'src/bar.cpp', - 'src/client.cpp' + 'src/client.cpp', + 'src/util/rfkill.cpp' ) if true # find_program('sway', required : false).found() diff --git a/src/modules/network.cpp b/src/modules/network.cpp index b140a955..2639a052 100644 --- a/src/modules/network.cpp +++ b/src/modules/network.cpp @@ -3,12 +3,8 @@ #include #include #include "util/format.hpp" - -#include -//#include -#include +#include "util/rfkill.hpp" #include -#include namespace { @@ -227,58 +223,15 @@ void waybar::modules::Network::worker() { const std::string waybar::modules::Network::getNetworkState() const { if (ifid_ == -1) { - if (isDisabled(RFKILL_TYPE_WLAN)) - return "disabled"; - return "disconnected"; - } + if (waybar::util::isDisabled(RFKILL_TYPE_WLAN)) + return "disabled"; + return "disconnected"; + } if (ipaddr_.empty()) return "linked"; if (essid_.empty()) return "ethernet"; return "wifi"; } -bool waybar::modules::Network::isDisabled(enum rfkill_type rfkill_type) const { - struct rfkill_event event; - ssize_t len; - int fd; - int ret; - ret = false; - - fd = open("/dev/rfkill", O_RDONLY); - if (fd < 0) { - perror("Can't open RFKILL control device"); - return false; - } - - if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0) { - perror("Can't set RFKILL control device to non-blocking"); - close(fd); - return false; - } - - while(true) { - len = read(fd, &event, sizeof(event)); - if (len < 0) { - if (errno == EAGAIN) - return 1; - perror("Reading of RFKILL events failed"); - return false; - } - - if (len != RFKILL_EVENT_SIZE_V1) { - fprintf(stderr, "Wrong size of RFKILL event\n"); - return false; - } - - if(event.type == rfkill_type) { - ret = event.soft || event.hard; - break; - } - } - - close(fd); - return ret; -} - auto waybar::modules::Network::update() -> void { std::lock_guard lock(mutex_); std::string tooltip_format; diff --git a/src/util/rfkill.cpp b/src/util/rfkill.cpp new file mode 100644 index 00000000..d1b38ec0 --- /dev/null +++ b/src/util/rfkill.cpp @@ -0,0 +1,49 @@ +#include "util/rfkill.hpp" +#include +#include +#include +#include +#include + +bool waybar::util::isDisabled(enum rfkill_type rfkill_type) { + struct rfkill_event event; + ssize_t len; + int fd; + int ret; + ret = false; + + fd = open("/dev/rfkill", O_RDONLY); + if (fd < 0) { + //perror("Can't open RFKILL control device"); + return false; + } + + if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0) { + //perror("Can't set RFKILL control device to non-blocking"); + close(fd); + return false; + } + + while(true) { + len = read(fd, &event, sizeof(event)); + if (len < 0) { + if (errno == EAGAIN) + return 1; + //perror("Reading of RFKILL events failed"); + return false; + } + + if (len != RFKILL_EVENT_SIZE_V1) { + //fprintf(stderr, "Wrong size of RFKILL event\n"); + return false; + } + + if(event.type == rfkill_type) { + ret = event.soft || event.hard; + break; + } + } + + close(fd); + return ret; +} 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 4/4] 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 43298618..d0fdce8a 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 53f558c9..f309b6fb 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 ab8bddae..dff42cdb 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 2639a052..636db671 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 d1b38ec0..2b0e42da 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;