From baa7f52e2197c40e169377ecde5ff2528177cd4a Mon Sep 17 00:00:00 2001 From: Alexis Date: Fri, 23 Nov 2018 16:04:29 +0100 Subject: [PATCH] refactor(network): wait for new address --- src/main.cpp | 2 +- src/modules/battery.cpp | 2 +- src/modules/network.cpp | 19 ++++++++++++++----- 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 85e120e..54cc9a0 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -15,7 +15,7 @@ int main(int argc, char* argv[]) waybar::client = &c; std::signal(SIGUSR1, [] (int /*signal*/) { for (auto& bar : waybar::client->bars) { - (*bar).toggle(); + bar->toggle(); } }); diff --git a/src/modules/battery.cpp b/src/modules/battery.cpp index c954685..9fae5ed 100644 --- a/src/modules/battery.cpp +++ b/src/modules/battery.cpp @@ -101,7 +101,7 @@ const std::string waybar::modules::Battery::getState(uint8_t capacity) const return a.second < b.second; }); std::string valid_state; - for (auto state : states) { + for (auto const& state : states) { if (capacity <= state.second && valid_state.empty()) { label_.get_style_context()->add_class(state.first); valid_state = state.first; diff --git a/src/modules/network.cpp b/src/modules/network.cpp index 9d5e341..979cb90 100644 --- a/src/modules/network.cpp +++ b/src/modules/network.cpp @@ -50,6 +50,7 @@ void waybar::modules::Network::worker() uint64_t len = netlinkResponse(sock_fd_, buf, sizeof(buf), RTMGRP_LINK | RTMGRP_IPV4_IFADDR); bool need_update = false; + bool new_addr = false; for (auto nh = reinterpret_cast(buf); NLMSG_OK(nh, len); nh = NLMSG_NEXT(nh, len)) { if (nh->nlmsg_type == NLMSG_DONE) { @@ -58,6 +59,9 @@ void waybar::modules::Network::worker() if (nh->nlmsg_type == NLMSG_ERROR) { continue; } + if (nh->nlmsg_type == RTM_NEWADDR) { + new_addr = true; + } if (nh->nlmsg_type < RTM_NEWADDR) { auto rtif = static_cast(NLMSG_DATA(nh)); if (rtif->ifi_index == static_cast(ifid_)) { @@ -69,9 +73,15 @@ void waybar::modules::Network::worker() } } if (ifid_ <= 0 && !config_["interface"].isString()) { - // Need to wait before get external interface - thread_.sleep_for(std::chrono::seconds(1)); - ifid_ = getExternalInterface(); + if (new_addr) { + // Need to wait before get external interface + while (ifid_ <= 0) { + ifid_ = getExternalInterface(); + thread_.sleep_for(std::chrono::seconds(1)); + } + } else { + ifid_ = getExternalInterface(); + } if (ifid_ > 0) { char ifname[IF_NAMESIZE]; if_indextoname(ifid_, ifname); @@ -319,13 +329,12 @@ int waybar::modules::Network::netlinkRequest(int fd, void *req, int waybar::modules::Network::netlinkResponse(int fd, void *resp, uint32_t resplen, uint32_t groups) { - int ret; struct sockaddr_nl sa = {}; sa.nl_family = AF_NETLINK; sa.nl_groups = groups; struct iovec iov = { resp, resplen }; struct msghdr msg = { &sa, sizeof(sa), &iov, 1, nullptr, 0, 0 }; - ret = recvmsg(fd, &msg, 0); + auto ret = recvmsg(fd, &msg, 0); if (msg.msg_flags & MSG_TRUNC) { return -1; }