diff --git a/include/modules/network.hpp b/include/modules/network.hpp index 91e4ddb2..f0d2e8e7 100644 --- a/include/modules/network.hpp +++ b/include/modules/network.hpp @@ -9,6 +9,7 @@ #include #include #include +#include #include "ALabel.hpp" #include "util/sleeper_thread.hpp" @@ -45,6 +46,7 @@ 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/src/modules/network.cpp b/src/modules/network.cpp index a332d5a2..b140a955 100644 --- a/src/modules/network.cpp +++ b/src/modules/network.cpp @@ -4,6 +4,11 @@ #include #include "util/format.hpp" +#include +//#include +#include +#include +#include namespace { @@ -221,12 +226,59 @@ void waybar::modules::Network::worker() { } const std::string waybar::modules::Network::getNetworkState() const { - if (ifid_ == -1) return "disconnected"; + if (ifid_ == -1) { + if (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;