properly structure rfkill util

This commit is contained in:
Marc 2020-01-21 17:48:45 +01:00
parent 626af1ddc1
commit f0dbd8b78d
5 changed files with 57 additions and 102 deletions

View File

@ -9,7 +9,6 @@
#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"
@ -46,7 +45,6 @@ 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

@ -1,55 +1,9 @@
#pragma once #pragma once
#include <linux/rfkill.h> #include <linux/rfkill.h>
#include <unistd.h>
//#include <stdlib.h>
#include <cstring>
#include <linux/rfkill.h>
#include <fcntl.h>
namespace waybar::util { namespace waybar::util {
bool isDisabled(enum rfkill_type rfkill_type) { bool isDisabled(enum rfkill_type rfkill_type);
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;
}
} // namespace waybar::util } // namespace waybar::util

View File

@ -97,7 +97,8 @@ src_files = files(
'src/modules/temperature.cpp', 'src/modules/temperature.cpp',
'src/main.cpp', 'src/main.cpp',
'src/bar.cpp', 'src/bar.cpp',
'src/client.cpp' 'src/client.cpp',
'src/util/rfkill.cpp'
) )
if true # find_program('sway', required : false).found() if true # find_program('sway', required : false).found()

View File

@ -3,12 +3,8 @@
#include <sys/eventfd.h> #include <sys/eventfd.h>
#include <fstream> #include <fstream>
#include "util/format.hpp" #include "util/format.hpp"
#include "util/rfkill.hpp"
#include <unistd.h>
//#include <stdlib.h>
#include <cstring>
#include <linux/rfkill.h> #include <linux/rfkill.h>
#include <fcntl.h>
namespace { namespace {
@ -227,7 +223,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 (isDisabled(RFKILL_TYPE_WLAN)) if (waybar::util::isDisabled(RFKILL_TYPE_WLAN))
return "disabled"; return "disabled";
return "disconnected"; return "disconnected";
} }
@ -236,49 +232,6 @@ const std::string waybar::modules::Network::getNetworkState() const {
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;

49
src/util/rfkill.cpp Normal file
View File

@ -0,0 +1,49 @@
#include "util/rfkill.hpp"
#include <unistd.h>
#include <stdlib.h>
#include <cstring>
#include <fcntl.h>
#include <cerrno>
bool waybar::util::isDisabled(enum rfkill_type rfkill_type) {
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;
}