distinguish between wifi disabled and disconnected

This commit is contained in:
Marc 2020-01-20 00:35:37 +01:00
parent def4466953
commit 2dc4ae78fc
2 changed files with 55 additions and 1 deletions

View File

@ -9,6 +9,7 @@
#include <netlink/genl/genl.h> #include <netlink/genl/genl.h>
#include <netlink/netlink.h> #include <netlink/netlink.h>
#include <sys/epoll.h> #include <sys/epoll.h>
#include <linux/rfkill.h>
#include "ALabel.hpp" #include "ALabel.hpp"
#include "util/sleeper_thread.hpp" #include "util/sleeper_thread.hpp"
@ -45,6 +46,7 @@ class Network : public ALabel {
const std::string getNetworkState() const; const std::string getNetworkState() const;
void clearIface(); void clearIface();
bool wildcardMatch(const std::string& pattern, const std::string& text) const; bool wildcardMatch(const std::string& pattern, const std::string& text) const;
bool isDisabled(enum rfkill_type rfkill_type) const;
int ifid_; int ifid_;
sa_family_t family_; sa_family_t family_;

View File

@ -4,6 +4,11 @@
#include <fstream> #include <fstream>
#include "util/format.hpp" #include "util/format.hpp"
#include <unistd.h>
//#include <stdlib.h>
#include <cstring>
#include <linux/rfkill.h>
#include <fcntl.h>
namespace { namespace {
@ -221,12 +226,59 @@ void waybar::modules::Network::worker() {
} }
const std::string waybar::modules::Network::getNetworkState() const { 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 (ipaddr_.empty()) return "linked";
if (essid_.empty()) return "ethernet"; if (essid_.empty()) return "ethernet";
return "wifi"; 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 { auto waybar::modules::Network::update() -> void {
std::lock_guard<std::mutex> lock(mutex_); std::lock_guard<std::mutex> lock(mutex_);
std::string tooltip_format; std::string tooltip_format;