add rudimentary bluetooth module functionality

pull/568/head
Marc 2020-01-21 17:04:54 +01:00
parent 2c4369a653
commit 626af1ddc1
3 changed files with 102 additions and 0 deletions

View File

@ -0,0 +1,18 @@
#pragma once
#include <fmt/format.h>
#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

View File

@ -0,0 +1,55 @@
#pragma once
#include <linux/rfkill.h>
#include <unistd.h>
//#include <stdlib.h>
#include <cstring>
#include <linux/rfkill.h>
#include <fcntl.h>
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

View File

@ -0,0 +1,29 @@
#include "modules/bluetooth.hpp"
#include "util/rfkill.hpp"
#include <linux/rfkill.h>
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);
}
}
}