bluetooth module handles rfkill events instantly

This commit is contained in:
Marc 2020-01-23 17:17:29 +01:00
parent d85f0e1060
commit e3bf6b968c
6 changed files with 97 additions and 17 deletions

View File

@ -5,6 +5,7 @@
#include <fmt/chrono.h> #include <fmt/chrono.h>
#include "util/sleeper_thread.hpp" #include "util/sleeper_thread.hpp"
#include "util/rfkill.hpp"
namespace waybar::modules { namespace waybar::modules {
@ -17,6 +18,8 @@ class Bluetooth : public ALabel {
private: private:
std::string status_; std::string status_;
util::SleeperThread thread_; util::SleeperThread thread_;
util::Rfkill rfkill_;
}; };
} // namespace waybar::modules } // namespace waybar::modules

View File

@ -11,6 +11,7 @@
#include <sys/epoll.h> #include <sys/epoll.h>
#include "ALabel.hpp" #include "ALabel.hpp"
#include "util/sleeper_thread.hpp" #include "util/sleeper_thread.hpp"
#include "util/rfkill.hpp"
namespace waybar::modules { namespace waybar::modules {
@ -71,6 +72,8 @@ class Network : public ALabel {
util::SleeperThread thread_; util::SleeperThread thread_;
util::SleeperThread thread_timer_; util::SleeperThread thread_timer_;
util::Rfkill rfkill_;
}; };
} // namespace waybar::modules } // namespace waybar::modules

View File

@ -2,8 +2,20 @@
#include <linux/rfkill.h> #include <linux/rfkill.h>
namespace waybar::util::rfkill { namespace waybar::util {
bool isDisabled(enum rfkill_type rfkill_type); class Rfkill {
public:
Rfkill(enum rfkill_type rfkill_type);
~Rfkill() = default;
bool isDisabled() const;
void waitForEvent();
int getState();
private:
enum rfkill_type rfkill_type_;
int state_ = 0;
int prev_state_ = 0;
};
} // namespace waybar::util } // namespace waybar::util

View File

@ -1,25 +1,23 @@
#include "modules/bluetooth.hpp" #include "modules/bluetooth.hpp"
#include "util/rfkill.hpp" #include "util/rfkill.hpp"
#include <linux/rfkill.h> #include <linux/rfkill.h>
#include <time.h> #include <time.h>
#include <iostream>
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") { status_("disabled"),
rfkill_(*(new waybar::util::Rfkill(RFKILL_TYPE_BLUETOOTH))) {
thread_ = [this] { thread_ = [this] {
dp.emit(); dp.emit();
auto now = std::chrono::system_clock::now(); rfkill_.waitForEvent();
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 {
status_ = "enabled"; status_ = "enabled";
if (waybar::util::rfkill::isDisabled(RFKILL_TYPE_BLUETOOTH)) { if (rfkill_.getState()) {
status_ = "disabled"; status_ = "disabled";
} else { } else {
status_ = "enabled"; status_ = "enabled";
@ -35,7 +33,7 @@ auto waybar::modules::Bluetooth::update() -> void {
////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(status_);
//} //}
//} //}
} }

View File

@ -87,7 +87,9 @@ waybar::modules::Network::Network(const std::string &id, const Json::Value &conf
cidr_(-1), cidr_(-1),
signal_strength_dbm_(0), signal_strength_dbm_(0),
signal_strength_(0), signal_strength_(0),
frequency_(0) { frequency_(0),
rfkill_(*(new waybar::util::Rfkill(RFKILL_TYPE_WLAN))) {
auto down_octets = read_netstat(BANDWIDTH_CATEGORY, BANDWIDTH_DOWN_TOTAL_KEY); auto down_octets = read_netstat(BANDWIDTH_CATEGORY, BANDWIDTH_DOWN_TOTAL_KEY);
auto up_octets = read_netstat(BANDWIDTH_CATEGORY, BANDWIDTH_UP_TOTAL_KEY); auto up_octets = read_netstat(BANDWIDTH_CATEGORY, BANDWIDTH_UP_TOTAL_KEY);
if (down_octets) { if (down_octets) {
@ -224,7 +226,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::rfkill::isDisabled(RFKILL_TYPE_WLAN)) if (rfkill_.isDisabled())
return "disabled"; return "disabled";
return "disconnected"; return "disconnected";
} }

View File

@ -3,9 +3,71 @@
#include <stdlib.h> #include <stdlib.h>
#include <cstring> #include <cstring>
#include <fcntl.h> #include <fcntl.h>
#include <sys/poll.h>
#include <cerrno> #include <cerrno>
#include <stdio.h>
bool waybar::util::rfkill::isDisabled(enum rfkill_type rfkill_type) { waybar::util::Rfkill::Rfkill(const enum rfkill_type rfkill_type)
: rfkill_type_(rfkill_type) {
}
void waybar::util::Rfkill::waitForEvent() {
struct rfkill_event event;
struct pollfd p;
ssize_t len;
int fd, n;
fd = open("/dev/rfkill", O_RDONLY);
if (fd < 0) {
//perror("Can't open RFKILL control device");
return;
}
memset(&p, 0, sizeof(p));
p.fd = fd;
p.events = POLLIN | POLLHUP;
while (1) {
n = poll(&p, 1, -1);
if (n < 0) {
//perror("Failed to poll RFKILL control device");
break;
}
if (n == 0)
continue;
len = read(fd, &event, sizeof(event));
if (len < 0) {
//perror("Reading of RFKILL events failed");
break;
}
if (len != RFKILL_EVENT_SIZE_V1) {
//fprintf(stderr, "Wrong size of RFKILL event\n");
continue;
}
if(event.type == rfkill_type_) {
state_ = event.soft || event.hard;
if (prev_state_ != state_) {
prev_state_ = state_;
break;
}
//ret = event.soft || event.hard;
}
}
close(fd);
return;
}
int waybar::util::Rfkill::getState() {
return state_;
}
bool waybar::util::Rfkill::isDisabled() const {
struct rfkill_event event; struct rfkill_event event;
ssize_t len; ssize_t len;
int fd; int fd;
@ -38,7 +100,7 @@ bool waybar::util::rfkill::isDisabled(enum rfkill_type rfkill_type) {
return false; return false;
} }
if(event.type == rfkill_type) { if(event.type == rfkill_type_) {
ret = event.soft || event.hard; ret = event.soft || event.hard;
break; break;
} }