fix(network): disconnected forced interface

This commit is contained in:
Alexis 2018-11-25 21:32:50 +01:00
parent 391a7de94a
commit a123711a8d
1 changed files with 12 additions and 6 deletions

View File

@ -2,7 +2,7 @@
waybar::modules::Network::Network(const Json::Value& config) waybar::modules::Network::Network(const Json::Value& config)
: ALabel(config, "{ifname}", 60), family_(AF_INET), : ALabel(config, "{ifname}", 60), family_(AF_INET),
signal_strength_dbm_(0), signal_strength_(0) cidr_(-1), signal_strength_dbm_(0), signal_strength_(0)
{ {
sock_fd_ = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE); sock_fd_ = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
if (sock_fd_ < 0) { if (sock_fd_ < 0) {
@ -26,7 +26,6 @@ waybar::modules::Network::Network(const Json::Value& config)
char ifname[IF_NAMESIZE]; char ifname[IF_NAMESIZE];
if_indextoname(ifid_, ifname); if_indextoname(ifid_, ifname);
ifname_ = ifname; ifname_ = ifname;
getInterfaceAddress();
} }
} }
initNL80211(); initNL80211();
@ -60,6 +59,9 @@ void waybar::modules::Network::worker()
if (nh->nlmsg_type == NLMSG_ERROR) { if (nh->nlmsg_type == NLMSG_ERROR) {
continue; continue;
} }
if (nh->nlmsg_type == RTM_NEWADDR) {
need_update = true;
}
if (nh->nlmsg_type < RTM_NEWADDR) { if (nh->nlmsg_type < RTM_NEWADDR) {
auto rtif = static_cast<struct ifinfomsg *>(NLMSG_DATA(nh)); auto rtif = static_cast<struct ifinfomsg *>(NLMSG_DATA(nh));
if (rtif->ifi_index == static_cast<int>(ifid_)) { if (rtif->ifi_index == static_cast<int>(ifid_)) {
@ -78,7 +80,6 @@ void waybar::modules::Network::worker()
char ifname[IF_NAMESIZE]; char ifname[IF_NAMESIZE];
if_indextoname(ifid_, ifname); if_indextoname(ifid_, ifname);
ifname_ = ifname; ifname_ = ifname;
getInterfaceAddress();
need_update = true; need_update = true;
} }
} }
@ -101,7 +102,7 @@ void waybar::modules::Network::worker()
auto waybar::modules::Network::update() -> void auto waybar::modules::Network::update() -> void
{ {
auto format = format_; auto format = format_;
if (ifid_ <= 0) { if (ifid_ <= 0 || ipaddr_.empty()) {
format = config_["format-disconnected"].isString() format = config_["format-disconnected"].isString()
? config_["format-disconnected"].asString() : format; ? config_["format-disconnected"].asString() : format;
label_.get_style_context()->add_class("disconnected"); label_.get_style_context()->add_class("disconnected");
@ -134,8 +135,12 @@ void waybar::modules::Network::disconnected()
ipaddr_.clear(); ipaddr_.clear();
netmask_.clear(); netmask_.clear();
cidr_ = 0; cidr_ = 0;
ifname_.clear(); if (!config_["interface"].isString()) {
ifid_ = -1; ifname_.clear();
ifid_ = -1;
}
// Need to wait otherwise we'll have the same information
thread_.sleep_for(std::chrono::seconds(1));
} }
void waybar::modules::Network::initNL80211() void waybar::modules::Network::initNL80211()
@ -421,6 +426,7 @@ bool waybar::modules::Network::associatedOrJoined(struct nlattr** bss)
auto waybar::modules::Network::getInfo() -> void auto waybar::modules::Network::getInfo() -> void
{ {
getInterfaceAddress();
struct nl_msg* nl_msg = nlmsg_alloc(); struct nl_msg* nl_msg = nlmsg_alloc();
if (nl_msg == nullptr) { if (nl_msg == nullptr) {
nlmsg_free(nl_msg); nlmsg_free(nl_msg);