diff --git a/include/modules/network.hpp b/include/modules/network.hpp index f0d2e8e7..91e4ddb2 100644 --- a/include/modules/network.hpp +++ b/include/modules/network.hpp @@ -9,7 +9,6 @@ #include #include #include -#include #include "ALabel.hpp" #include "util/sleeper_thread.hpp" @@ -46,7 +45,6 @@ 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/include/util/rfkill.hpp b/include/util/rfkill.hpp index 9ef68923..53f558c9 100644 --- a/include/util/rfkill.hpp +++ b/include/util/rfkill.hpp @@ -1,55 +1,9 @@ #pragma once #include -#include -//#include -#include -#include -#include namespace waybar::util { -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; -} +bool isDisabled(enum rfkill_type rfkill_type); } // namespace waybar::util diff --git a/meson.build b/meson.build index 0032c915..8f29d10c 100644 --- a/meson.build +++ b/meson.build @@ -97,7 +97,8 @@ src_files = files( 'src/modules/temperature.cpp', 'src/main.cpp', 'src/bar.cpp', - 'src/client.cpp' + 'src/client.cpp', + 'src/util/rfkill.cpp' ) if true # find_program('sway', required : false).found() diff --git a/src/modules/network.cpp b/src/modules/network.cpp index b140a955..2639a052 100644 --- a/src/modules/network.cpp +++ b/src/modules/network.cpp @@ -3,12 +3,8 @@ #include #include #include "util/format.hpp" - -#include -//#include -#include +#include "util/rfkill.hpp" #include -#include namespace { @@ -227,58 +223,15 @@ void waybar::modules::Network::worker() { const std::string waybar::modules::Network::getNetworkState() const { if (ifid_ == -1) { - if (isDisabled(RFKILL_TYPE_WLAN)) - return "disabled"; - return "disconnected"; - } + if (waybar::util::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; diff --git a/src/util/rfkill.cpp b/src/util/rfkill.cpp new file mode 100644 index 00000000..d1b38ec0 --- /dev/null +++ b/src/util/rfkill.cpp @@ -0,0 +1,49 @@ +#include "util/rfkill.hpp" +#include +#include +#include +#include +#include + +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; +}