Merge pull request #2 from marcplustwo/airplane_mode

distinguish between wifi disabled and disconnected
pull/568/head
Marc Radau 2020-01-20 10:46:59 +01:00 committed by GitHub
commit b8aeda794c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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

@ -5,6 +5,11 @@
#include <cassert> #include <cassert>
#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 {
@ -222,12 +227,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;