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] 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); + } + } +}