refactor(network): wait for new address
This commit is contained in:
parent
2b05b8e69a
commit
baa7f52e21
|
@ -15,7 +15,7 @@ int main(int argc, char* argv[])
|
||||||
waybar::client = &c;
|
waybar::client = &c;
|
||||||
std::signal(SIGUSR1, [] (int /*signal*/) {
|
std::signal(SIGUSR1, [] (int /*signal*/) {
|
||||||
for (auto& bar : waybar::client->bars) {
|
for (auto& bar : waybar::client->bars) {
|
||||||
(*bar).toggle();
|
bar->toggle();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -101,7 +101,7 @@ const std::string waybar::modules::Battery::getState(uint8_t capacity) const
|
||||||
return a.second < b.second;
|
return a.second < b.second;
|
||||||
});
|
});
|
||||||
std::string valid_state;
|
std::string valid_state;
|
||||||
for (auto state : states) {
|
for (auto const& state : states) {
|
||||||
if (capacity <= state.second && valid_state.empty()) {
|
if (capacity <= state.second && valid_state.empty()) {
|
||||||
label_.get_style_context()->add_class(state.first);
|
label_.get_style_context()->add_class(state.first);
|
||||||
valid_state = state.first;
|
valid_state = state.first;
|
||||||
|
|
|
@ -50,6 +50,7 @@ void waybar::modules::Network::worker()
|
||||||
uint64_t len = netlinkResponse(sock_fd_, buf, sizeof(buf),
|
uint64_t len = netlinkResponse(sock_fd_, buf, sizeof(buf),
|
||||||
RTMGRP_LINK | RTMGRP_IPV4_IFADDR);
|
RTMGRP_LINK | RTMGRP_IPV4_IFADDR);
|
||||||
bool need_update = false;
|
bool need_update = false;
|
||||||
|
bool new_addr = false;
|
||||||
for (auto nh = reinterpret_cast<struct nlmsghdr *>(buf); NLMSG_OK(nh, len);
|
for (auto nh = reinterpret_cast<struct nlmsghdr *>(buf); NLMSG_OK(nh, len);
|
||||||
nh = NLMSG_NEXT(nh, len)) {
|
nh = NLMSG_NEXT(nh, len)) {
|
||||||
if (nh->nlmsg_type == NLMSG_DONE) {
|
if (nh->nlmsg_type == NLMSG_DONE) {
|
||||||
|
@ -58,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) {
|
||||||
|
new_addr = 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_)) {
|
||||||
|
@ -69,9 +73,15 @@ void waybar::modules::Network::worker()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (ifid_ <= 0 && !config_["interface"].isString()) {
|
if (ifid_ <= 0 && !config_["interface"].isString()) {
|
||||||
// Need to wait before get external interface
|
if (new_addr) {
|
||||||
thread_.sleep_for(std::chrono::seconds(1));
|
// Need to wait before get external interface
|
||||||
ifid_ = getExternalInterface();
|
while (ifid_ <= 0) {
|
||||||
|
ifid_ = getExternalInterface();
|
||||||
|
thread_.sleep_for(std::chrono::seconds(1));
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
ifid_ = getExternalInterface();
|
||||||
|
}
|
||||||
if (ifid_ > 0) {
|
if (ifid_ > 0) {
|
||||||
char ifname[IF_NAMESIZE];
|
char ifname[IF_NAMESIZE];
|
||||||
if_indextoname(ifid_, ifname);
|
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,
|
int waybar::modules::Network::netlinkResponse(int fd, void *resp,
|
||||||
uint32_t resplen, uint32_t groups)
|
uint32_t resplen, uint32_t groups)
|
||||||
{
|
{
|
||||||
int ret;
|
|
||||||
struct sockaddr_nl sa = {};
|
struct sockaddr_nl sa = {};
|
||||||
sa.nl_family = AF_NETLINK;
|
sa.nl_family = AF_NETLINK;
|
||||||
sa.nl_groups = groups;
|
sa.nl_groups = groups;
|
||||||
struct iovec iov = { resp, resplen };
|
struct iovec iov = { resp, resplen };
|
||||||
struct msghdr msg = { &sa, sizeof(sa), &iov, 1, nullptr, 0, 0 };
|
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) {
|
if (msg.msg_flags & MSG_TRUNC) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue