From aa385e28b6c2c6e8e261b6b108f3635e86796b01 Mon Sep 17 00:00:00 2001 From: Alex Date: Sat, 18 May 2019 15:32:40 +0200 Subject: [PATCH] refactor: execute update on idle --- src/bar.cpp | 13 ++++++++----- src/modules/network.cpp | 41 ++++++++++++++++++++++------------------- 2 files changed, 30 insertions(+), 24 deletions(-) diff --git a/src/bar.cpp b/src/bar.cpp index dd7b8805..09d7335c 100644 --- a/src/bar.cpp +++ b/src/bar.cpp @@ -272,11 +272,14 @@ void waybar::Bar::getModules(const Factory& factory, const std::string& pos) { modules_right_.emplace_back(module); } module->dp.connect([module, &name] { - try { - module->update(); - } catch (const std::exception& e) { - std::cerr << name.asString() + ": " + e.what() << std::endl; - } + // Fix https://github.com/Alexays/Waybar/issues/320, proper way? + Glib::signal_idle().connect_once([module, &name] { + try { + module->update(); + } catch (const std::exception& e) { + std::cerr << name.asString() + ": " + e.what() << std::endl; + } + }); }); } catch (const std::exception& e) { std::cerr << e.what() << std::endl; diff --git a/src/modules/network.cpp b/src/modules/network.cpp index 17795960..9c1dc84e 100644 --- a/src/modules/network.cpp +++ b/src/modules/network.cpp @@ -42,8 +42,6 @@ waybar::modules::Network::~Network() { nl_socket_drop_membership(ev_sock_, RTNLGRP_LINK); nl_socket_drop_membership(ev_sock_, RTNLGRP_IPV4_IFADDR); nl_socket_drop_membership(ev_sock_, RTNLGRP_IPV6_IFADDR); - nl_socket_drop_membership(ev_sock_, RTNLGRP_IPV4_ROUTE); - nl_socket_drop_membership(ev_sock_, RTNLGRP_IPV6_ROUTE); nl_close(ev_sock_); nl_socket_free(ev_sock_); } @@ -64,8 +62,6 @@ void waybar::modules::Network::createInfoSocket() { nl_socket_add_membership(ev_sock_, RTNLGRP_LINK); nl_socket_add_membership(ev_sock_, RTNLGRP_IPV4_IFADDR); nl_socket_add_membership(ev_sock_, RTNLGRP_IPV6_IFADDR); - nl_socket_add_membership(ev_sock_, RTNLGRP_IPV4_ROUTE); - nl_socket_add_membership(ev_sock_, RTNLGRP_IPV6_ROUTE); efd_ = epoll_create1(EPOLL_CLOEXEC); if (efd_ < 0) { throw std::runtime_error("Can't create epoll"); @@ -108,9 +104,12 @@ void waybar::modules::Network::createEventSocket() { void waybar::modules::Network::worker() { thread_timer_ = [this] { - if (ifid_ > 0) { - getInfo(); - dp.emit(); + { + std::lock_guard lock(mutex_); + if (ifid_ > 0) { + getInfo(); + dp.emit(); + } } thread_timer_.sleep_for(interval_); }; @@ -131,11 +130,9 @@ void waybar::modules::Network::worker() { } auto waybar::modules::Network::update() -> void { - std::string connectiontype; - std::string tooltip_format; - if (config_["tooltip-format"].isString()) { - tooltip_format = config_["tooltip-format"].asString(); - } + std::string connectiontype; + std::string tooltip_format; + std::lock_guard lock(mutex_); if (ifid_ <= 0 || !linked_) { if (config_["format-disconnected"].isString()) { default_format_ = config_["format-disconnected"].asString(); @@ -187,8 +184,13 @@ auto waybar::modules::Network::update() -> void { fmt::arg("cidr", cidr_), fmt::arg("frequency", frequency_), fmt::arg("icon", getIcon(signal_strength_, connectiontype))); - label_.set_markup(text); + if (text != label_.get_label()) { + label_.set_markup(text); + } if (tooltipEnabled()) { + if (tooltip_format.empty() && config_["tooltip-format"].isString()) { + tooltip_format = config_["tooltip-format"].asString(); + } if (!tooltip_format.empty()) { auto tooltip_text = fmt::format(tooltip_format, fmt::arg("essid", essid_), @@ -199,10 +201,11 @@ auto waybar::modules::Network::update() -> void { fmt::arg("ipaddr", ipaddr_), fmt::arg("cidr", cidr_), fmt::arg("frequency", frequency_), - fmt::arg("icon", getIcon(signal_strength_, - connectiontype))); - label_.set_tooltip_text(tooltip_text); - } else { + fmt::arg("icon", getIcon(signal_strength_, connectiontype))); + if (label_.get_tooltip_text() != text) { + label_.set_tooltip_text(tooltip_text); + } + } else if (label_.get_tooltip_text() != text) { label_.set_tooltip_text(text); } } @@ -464,7 +467,7 @@ int waybar::modules::Network::handleEvents(struct nl_msg *msg, void *data) { net->ifid_ = rtif->ifi_index; } // Check for valid interface - if (rtif->ifi_index == static_cast(net->ifid_)) { + if (rtif->ifi_index == net->ifid_) { // Get Iface and WIFI info net->getInterfaceAddress(); net->thread_timer_.wake_up(); @@ -472,7 +475,7 @@ int waybar::modules::Network::handleEvents(struct nl_msg *msg, void *data) { } else if (nh->nlmsg_type == RTM_DELADDR) { auto rtif = static_cast(NLMSG_DATA(nh)); // Check for valid interface - if (rtif->ifi_index == static_cast(net->ifid_)) { + if (rtif->ifi_index == net->ifid_) { net->ipaddr_.clear(); net->netmask_.clear(); net->cidr_ = 0;