refactor(network): better events handler
parent
070619fa34
commit
ecec02c8be
|
@ -41,6 +41,7 @@ class Network : public ALabel {
|
||||||
bool checkInterface(struct ifinfomsg* rtif, std::string name);
|
bool checkInterface(struct ifinfomsg* rtif, std::string name);
|
||||||
int getPreferredIface(int skip_idx = -1) const;
|
int getPreferredIface(int skip_idx = -1) const;
|
||||||
auto getInfo() -> void;
|
auto getInfo() -> void;
|
||||||
|
void checkNewInterface(struct ifinfomsg* rtif);
|
||||||
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;
|
||||||
|
|
|
@ -576,12 +576,53 @@ void waybar::modules::Network::clearIface() {
|
||||||
linked_ = false;
|
linked_ = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void waybar::modules::Network::checkNewInterface(struct ifinfomsg *rtif) {
|
||||||
|
auto new_iface = getPreferredIface(rtif->ifi_index);
|
||||||
|
if (new_iface != -1) {
|
||||||
|
ifid_ = new_iface;
|
||||||
|
char ifname[IF_NAMESIZE];
|
||||||
|
if_indextoname(new_iface, ifname);
|
||||||
|
ifname_ = ifname;
|
||||||
|
getInterfaceAddress();
|
||||||
|
thread_timer_.wake_up();
|
||||||
|
} else {
|
||||||
|
ifid_ = -1;
|
||||||
|
dp.emit();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
int waybar::modules::Network::handleEvents(struct nl_msg *msg, void *data) {
|
int waybar::modules::Network::handleEvents(struct nl_msg *msg, void *data) {
|
||||||
auto net = static_cast<waybar::modules::Network *>(data);
|
auto net = static_cast<waybar::modules::Network *>(data);
|
||||||
std::lock_guard<std::mutex> lock(net->mutex_);
|
std::lock_guard<std::mutex> lock(net->mutex_);
|
||||||
auto nh = nlmsg_hdr(msg);
|
auto nh = nlmsg_hdr(msg);
|
||||||
auto rtif = static_cast<struct ifinfomsg *>(NLMSG_DATA(nh));
|
auto rtif = static_cast<struct ifinfomsg *>(NLMSG_DATA(nh));
|
||||||
if (nh->nlmsg_type == RTM_NEWADDR) {
|
if (nh->nlmsg_type == RTM_DELADDR) {
|
||||||
|
// Check for valid interface
|
||||||
|
if (rtif->ifi_index == net->ifid_) {
|
||||||
|
net->ipaddr_.clear();
|
||||||
|
net->netmask_.clear();
|
||||||
|
net->cidr_ = 0;
|
||||||
|
if (!(rtif->ifi_flags & IFF_RUNNING)) {
|
||||||
|
net->clearIface();
|
||||||
|
// Check for a new interface and get info
|
||||||
|
net->checkNewInterface(rtif);
|
||||||
|
}
|
||||||
|
net->dp.emit();
|
||||||
|
}
|
||||||
|
} else if (nh->nlmsg_type == RTM_NEWLINK || nh->nlmsg_type == RTM_DELLINK) {
|
||||||
|
char ifname[IF_NAMESIZE];
|
||||||
|
if_indextoname(rtif->ifi_index, ifname);
|
||||||
|
// Check for valid interface
|
||||||
|
if (rtif->ifi_flags & IFF_RUNNING && net->checkInterface(rtif, ifname)) {
|
||||||
|
net->linked_ = true;
|
||||||
|
net->ifname_ = ifname;
|
||||||
|
net->ifid_ = rtif->ifi_index;
|
||||||
|
} else if (rtif->ifi_index == net->ifid_ && !(rtif->ifi_flags & IFF_RUNNING)) {
|
||||||
|
net->clearIface();
|
||||||
|
// Check for a new interface and get info
|
||||||
|
net->checkNewInterface(rtif);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
char ifname[IF_NAMESIZE];
|
char ifname[IF_NAMESIZE];
|
||||||
if_indextoname(rtif->ifi_index, ifname);
|
if_indextoname(rtif->ifi_index, ifname);
|
||||||
// Auto detected network can also be assigned here
|
// Auto detected network can also be assigned here
|
||||||
|
@ -601,39 +642,6 @@ int waybar::modules::Network::handleEvents(struct nl_msg *msg, void *data) {
|
||||||
net->getInterfaceAddress();
|
net->getInterfaceAddress();
|
||||||
net->thread_timer_.wake_up();
|
net->thread_timer_.wake_up();
|
||||||
}
|
}
|
||||||
} else if (nh->nlmsg_type == RTM_DELADDR) {
|
|
||||||
// Check for valid interface
|
|
||||||
if (rtif->ifi_index == net->ifid_) {
|
|
||||||
net->ipaddr_.clear();
|
|
||||||
net->netmask_.clear();
|
|
||||||
net->cidr_ = 0;
|
|
||||||
net->dp.emit();
|
|
||||||
}
|
|
||||||
} else if (nh->nlmsg_type < RTM_NEWADDR) {
|
|
||||||
char ifname[IF_NAMESIZE];
|
|
||||||
if_indextoname(rtif->ifi_index, ifname);
|
|
||||||
// Check for valid interface
|
|
||||||
if (rtif->ifi_flags & IFF_RUNNING && net->checkInterface(rtif, ifname)) {
|
|
||||||
net->linked_ = true;
|
|
||||||
net->ifname_ = ifname;
|
|
||||||
net->ifid_ = rtif->ifi_index;
|
|
||||||
net->dp.emit();
|
|
||||||
} else if (rtif->ifi_index == net->ifid_) {
|
|
||||||
net->clearIface();
|
|
||||||
// Check for a new interface and get info
|
|
||||||
auto new_iface = net->getPreferredIface(rtif->ifi_index);
|
|
||||||
if (new_iface != -1) {
|
|
||||||
net->ifid_ = new_iface;
|
|
||||||
char ifname[IF_NAMESIZE];
|
|
||||||
if_indextoname(new_iface, ifname);
|
|
||||||
net->ifname_ = ifname;
|
|
||||||
net->getInterfaceAddress();
|
|
||||||
net->thread_timer_.wake_up();
|
|
||||||
} else {
|
|
||||||
net->ifid_ = -1;
|
|
||||||
net->dp.emit();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return NL_SKIP;
|
return NL_SKIP;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue