Merge pull request #1015 from alebastr/rfkill-events
rfkill code refactoring
This commit is contained in:
commit
08e19602f7
|
@ -1,10 +1,6 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <fmt/format.h>
|
|
||||||
#include "ALabel.hpp"
|
#include "ALabel.hpp"
|
||||||
|
|
||||||
#include <fmt/chrono.h>
|
|
||||||
#include "util/sleeper_thread.hpp"
|
|
||||||
#include "util/rfkill.hpp"
|
#include "util/rfkill.hpp"
|
||||||
|
|
||||||
namespace waybar::modules {
|
namespace waybar::modules {
|
||||||
|
@ -16,10 +12,6 @@ class Bluetooth : public ALabel {
|
||||||
auto update() -> void;
|
auto update() -> void;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::string status_;
|
|
||||||
util::SleeperThread thread_;
|
|
||||||
util::SleeperThread intervall_thread_;
|
|
||||||
|
|
||||||
util::Rfkill rfkill_;
|
util::Rfkill rfkill_;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -75,8 +75,6 @@ class Network : public ALabel {
|
||||||
util::SleeperThread thread_;
|
util::SleeperThread thread_;
|
||||||
util::SleeperThread thread_timer_;
|
util::SleeperThread thread_timer_;
|
||||||
#ifdef WANT_RFKILL
|
#ifdef WANT_RFKILL
|
||||||
util::SleeperThread thread_rfkill_;
|
|
||||||
|
|
||||||
util::Rfkill rfkill_;
|
util::Rfkill rfkill_;
|
||||||
#endif
|
#endif
|
||||||
};
|
};
|
||||||
|
|
|
@ -1,19 +1,26 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <glibmm/iochannel.h>
|
||||||
#include <linux/rfkill.h>
|
#include <linux/rfkill.h>
|
||||||
|
#include <sigc++/signal.h>
|
||||||
|
#include <sigc++/trackable.h>
|
||||||
|
|
||||||
namespace waybar::util {
|
namespace waybar::util {
|
||||||
|
|
||||||
class Rfkill {
|
class Rfkill : public sigc::trackable {
|
||||||
public:
|
public:
|
||||||
Rfkill(enum rfkill_type rfkill_type);
|
Rfkill(enum rfkill_type rfkill_type);
|
||||||
~Rfkill() = default;
|
~Rfkill();
|
||||||
void waitForEvent();
|
|
||||||
bool getState() const;
|
bool getState() const;
|
||||||
|
|
||||||
|
sigc::signal<void(struct rfkill_event&)> on_update;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
enum rfkill_type rfkill_type_;
|
enum rfkill_type rfkill_type_;
|
||||||
int state_ = 0;
|
bool state_ = false;
|
||||||
|
int fd_ = -1;
|
||||||
|
|
||||||
|
bool on_event(Glib::IOCondition cond);
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace waybar::util
|
} // namespace waybar::util
|
||||||
|
|
|
@ -12,11 +12,6 @@ The *bluetooth* module displays information about the status of the device's blu
|
||||||
|
|
||||||
Addressed by *bluetooth*
|
Addressed by *bluetooth*
|
||||||
|
|
||||||
*interval*: ++
|
|
||||||
typeof: integer ++
|
|
||||||
default: 60 ++
|
|
||||||
The interval in which the bluetooth state gets updated.
|
|
||||||
|
|
||||||
*format*: ++
|
*format*: ++
|
||||||
typeof: string ++
|
typeof: string ++
|
||||||
default: *{icon}* ++
|
default: *{icon}* ++
|
||||||
|
@ -88,7 +83,6 @@ Addressed by *bluetooth*
|
||||||
"bluetooth": {
|
"bluetooth": {
|
||||||
"format": "{icon}",
|
"format": "{icon}",
|
||||||
"format-alt": "bluetooth: {status}",
|
"format-alt": "bluetooth: {status}",
|
||||||
"interval": 30,
|
|
||||||
"format-icons": {
|
"format-icons": {
|
||||||
"enabled": "",
|
"enabled": "",
|
||||||
"disabled": ""
|
"disabled": ""
|
||||||
|
|
|
@ -1,45 +1,25 @@
|
||||||
#include "modules/bluetooth.hpp"
|
#include "modules/bluetooth.hpp"
|
||||||
#include "util/rfkill.hpp"
|
|
||||||
#include <linux/rfkill.h>
|
#include <fmt/format.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, "{icon}", 10),
|
: ALabel(config, "bluetooth", id, "{icon}", 10), rfkill_{RFKILL_TYPE_BLUETOOTH} {
|
||||||
status_("disabled"),
|
rfkill_.on_update.connect(sigc::hide(sigc::mem_fun(*this, &Bluetooth::update)));
|
||||||
rfkill_{RFKILL_TYPE_BLUETOOTH} {
|
|
||||||
thread_ = [this] {
|
|
||||||
dp.emit();
|
|
||||||
rfkill_.waitForEvent();
|
|
||||||
};
|
|
||||||
intervall_thread_ = [this] {
|
|
||||||
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 {
|
||||||
if (rfkill_.getState()) {
|
std::string status = rfkill_.getState() ? "disabled" : "enabled";
|
||||||
status_ = "disabled";
|
|
||||||
} else {
|
|
||||||
status_ = "enabled";
|
|
||||||
}
|
|
||||||
|
|
||||||
label_.set_markup(
|
label_.set_markup(
|
||||||
fmt::format(
|
fmt::format(format_, fmt::arg("status", status), fmt::arg("icon", getIcon(0, status))));
|
||||||
format_,
|
|
||||||
fmt::arg("status", status_),
|
|
||||||
fmt::arg("icon", getIcon(0, 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, status_);
|
auto tooltip_text = fmt::format(tooltip_format, status);
|
||||||
label_.set_tooltip_text(tooltip_text);
|
label_.set_tooltip_text(tooltip_text);
|
||||||
} else {
|
} else {
|
||||||
label_.set_tooltip_text(status_);
|
label_.set_tooltip_text(status);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -212,16 +212,13 @@ void waybar::modules::Network::worker() {
|
||||||
thread_timer_.sleep_for(interval_);
|
thread_timer_.sleep_for(interval_);
|
||||||
};
|
};
|
||||||
#ifdef WANT_RFKILL
|
#ifdef WANT_RFKILL
|
||||||
thread_rfkill_ = [this] {
|
rfkill_.on_update.connect([this](auto &) {
|
||||||
rfkill_.waitForEvent();
|
/* If we are here, it's likely that the network thread already holds the mutex and will be
|
||||||
{
|
* holding it for a next few seconds.
|
||||||
std::lock_guard<std::mutex> lock(mutex_);
|
* Let's delegate the update to the timer thread instead of blocking the main thread.
|
||||||
if (ifid_ > 0) {
|
*/
|
||||||
getInfo();
|
thread_timer_.wake_up();
|
||||||
dp.emit();
|
});
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
#else
|
#else
|
||||||
spdlog::warn("Waybar has been built without rfkill support.");
|
spdlog::warn("Waybar has been built without rfkill support.");
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -19,60 +19,64 @@
|
||||||
#include "util/rfkill.hpp"
|
#include "util/rfkill.hpp"
|
||||||
|
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
|
#include <glibmm/main.h>
|
||||||
#include <linux/rfkill.h>
|
#include <linux/rfkill.h>
|
||||||
#include <poll.h>
|
#include <spdlog/spdlog.h>
|
||||||
#include <stdlib.h>
|
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
#include <cerrno>
|
#include <cerrno>
|
||||||
#include <cstring>
|
|
||||||
#include <stdexcept>
|
|
||||||
|
|
||||||
waybar::util::Rfkill::Rfkill(const enum rfkill_type rfkill_type) : rfkill_type_(rfkill_type) {}
|
waybar::util::Rfkill::Rfkill(const enum rfkill_type rfkill_type) : rfkill_type_(rfkill_type) {
|
||||||
|
fd_ = open("/dev/rfkill", O_RDONLY);
|
||||||
void waybar::util::Rfkill::waitForEvent() {
|
if (fd_ < 0) {
|
||||||
struct rfkill_event event;
|
spdlog::error("Can't open RFKILL control device");
|
||||||
struct pollfd p;
|
|
||||||
ssize_t len;
|
|
||||||
int fd, n;
|
|
||||||
|
|
||||||
fd = open("/dev/rfkill", O_RDONLY);
|
|
||||||
if (fd < 0) {
|
|
||||||
throw std::runtime_error("Can't open RFKILL control device");
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
int rc = fcntl(fd_, F_SETFL, O_NONBLOCK);
|
||||||
memset(&p, 0, sizeof(p));
|
if (rc < 0) {
|
||||||
p.fd = fd;
|
spdlog::error("Can't set RFKILL control device to non-blocking: {}", errno);
|
||||||
p.events = POLLIN | POLLHUP;
|
close(fd_);
|
||||||
|
fd_ = -1;
|
||||||
while (1) {
|
return;
|
||||||
n = poll(&p, 1, -1);
|
}
|
||||||
if (n < 0) {
|
Glib::signal_io().connect(
|
||||||
throw std::runtime_error("Failed to poll RFKILL control device");
|
sigc::mem_fun(*this, &Rfkill::on_event), fd_, Glib::IO_IN | Glib::IO_ERR | Glib::IO_HUP);
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (n == 0) continue;
|
waybar::util::Rfkill::~Rfkill() {
|
||||||
|
if (fd_ >= 0) {
|
||||||
|
close(fd_);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
len = read(fd, &event, sizeof(event));
|
bool waybar::util::Rfkill::on_event(Glib::IOCondition cond) {
|
||||||
|
if (cond & Glib::IO_IN) {
|
||||||
|
struct rfkill_event event;
|
||||||
|
ssize_t len;
|
||||||
|
|
||||||
|
len = read(fd_, &event, sizeof(event));
|
||||||
if (len < 0) {
|
if (len < 0) {
|
||||||
throw std::runtime_error("Reading of RFKILL events failed");
|
if (errno == EAGAIN) {
|
||||||
break;
|
return true;
|
||||||
|
}
|
||||||
|
spdlog::error("Reading of RFKILL events failed: {}", errno);
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (len != RFKILL_EVENT_SIZE_V1) {
|
if (len < RFKILL_EVENT_SIZE_V1) {
|
||||||
throw std::runtime_error("Wrong size of RFKILL event");
|
spdlog::error("Wrong size of RFKILL event: {} < {}", len, RFKILL_EVENT_SIZE_V1);
|
||||||
continue;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (event.type == rfkill_type_ && event.op == RFKILL_OP_CHANGE) {
|
if (event.type == rfkill_type_ && (event.op == RFKILL_OP_ADD || event.op == RFKILL_OP_CHANGE)) {
|
||||||
state_ = event.soft || event.hard;
|
state_ = event.soft || event.hard;
|
||||||
break;
|
on_update.emit(event);
|
||||||
}
|
}
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
spdlog::error("Failed to poll RFKILL control device");
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
close(fd);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool waybar::util::Rfkill::getState() const { return state_; }
|
bool waybar::util::Rfkill::getState() const { return state_; }
|
||||||
|
|
Loading…
Reference in New Issue