refactor: execute update on idle

pull/328/head
Alex 2019-05-18 15:32:40 +02:00
parent 4865a9ad6c
commit aa385e28b6
2 changed files with 30 additions and 24 deletions

View File

@ -272,12 +272,15 @@ void waybar::Bar::getModules(const Factory& factory, const std::string& pos) {
modules_right_.emplace_back(module); modules_right_.emplace_back(module);
} }
module->dp.connect([module, &name] { module->dp.connect([module, &name] {
// Fix https://github.com/Alexays/Waybar/issues/320, proper way?
Glib::signal_idle().connect_once([module, &name] {
try { try {
module->update(); module->update();
} catch (const std::exception& e) { } catch (const std::exception& e) {
std::cerr << name.asString() + ": " + e.what() << std::endl; std::cerr << name.asString() + ": " + e.what() << std::endl;
} }
}); });
});
} catch (const std::exception& e) { } catch (const std::exception& e) {
std::cerr << e.what() << std::endl; std::cerr << e.what() << std::endl;
} }

View File

@ -42,8 +42,6 @@ waybar::modules::Network::~Network() {
nl_socket_drop_membership(ev_sock_, RTNLGRP_LINK); nl_socket_drop_membership(ev_sock_, RTNLGRP_LINK);
nl_socket_drop_membership(ev_sock_, RTNLGRP_IPV4_IFADDR); 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_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_close(ev_sock_);
nl_socket_free(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_LINK);
nl_socket_add_membership(ev_sock_, RTNLGRP_IPV4_IFADDR); 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_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); efd_ = epoll_create1(EPOLL_CLOEXEC);
if (efd_ < 0) { if (efd_ < 0) {
throw std::runtime_error("Can't create epoll"); throw std::runtime_error("Can't create epoll");
@ -108,10 +104,13 @@ void waybar::modules::Network::createEventSocket() {
void waybar::modules::Network::worker() { void waybar::modules::Network::worker() {
thread_timer_ = [this] { thread_timer_ = [this] {
{
std::lock_guard<std::mutex> lock(mutex_);
if (ifid_ > 0) { if (ifid_ > 0) {
getInfo(); getInfo();
dp.emit(); dp.emit();
} }
}
thread_timer_.sleep_for(interval_); thread_timer_.sleep_for(interval_);
}; };
std::array<struct epoll_event, EPOLL_MAX> events{}; std::array<struct epoll_event, EPOLL_MAX> events{};
@ -133,9 +132,7 @@ void waybar::modules::Network::worker() {
auto waybar::modules::Network::update() -> void { auto waybar::modules::Network::update() -> void {
std::string connectiontype; std::string connectiontype;
std::string tooltip_format; std::string tooltip_format;
if (config_["tooltip-format"].isString()) { std::lock_guard<std::mutex> lock(mutex_);
tooltip_format = config_["tooltip-format"].asString();
}
if (ifid_ <= 0 || !linked_) { if (ifid_ <= 0 || !linked_) {
if (config_["format-disconnected"].isString()) { if (config_["format-disconnected"].isString()) {
default_format_ = config_["format-disconnected"].asString(); default_format_ = config_["format-disconnected"].asString();
@ -187,8 +184,13 @@ auto waybar::modules::Network::update() -> void {
fmt::arg("cidr", cidr_), fmt::arg("cidr", cidr_),
fmt::arg("frequency", frequency_), fmt::arg("frequency", frequency_),
fmt::arg("icon", getIcon(signal_strength_, connectiontype))); fmt::arg("icon", getIcon(signal_strength_, connectiontype)));
if (text != label_.get_label()) {
label_.set_markup(text); label_.set_markup(text);
}
if (tooltipEnabled()) { if (tooltipEnabled()) {
if (tooltip_format.empty() && config_["tooltip-format"].isString()) {
tooltip_format = config_["tooltip-format"].asString();
}
if (!tooltip_format.empty()) { if (!tooltip_format.empty()) {
auto tooltip_text = fmt::format(tooltip_format, auto tooltip_text = fmt::format(tooltip_format,
fmt::arg("essid", essid_), fmt::arg("essid", essid_),
@ -199,10 +201,11 @@ auto waybar::modules::Network::update() -> void {
fmt::arg("ipaddr", ipaddr_), fmt::arg("ipaddr", ipaddr_),
fmt::arg("cidr", cidr_), fmt::arg("cidr", cidr_),
fmt::arg("frequency", frequency_), fmt::arg("frequency", frequency_),
fmt::arg("icon", getIcon(signal_strength_, fmt::arg("icon", getIcon(signal_strength_, connectiontype)));
connectiontype))); if (label_.get_tooltip_text() != text) {
label_.set_tooltip_text(tooltip_text); label_.set_tooltip_text(tooltip_text);
} else { }
} else if (label_.get_tooltip_text() != text) {
label_.set_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; net->ifid_ = rtif->ifi_index;
} }
// Check for valid interface // Check for valid interface
if (rtif->ifi_index == static_cast<int>(net->ifid_)) { if (rtif->ifi_index == net->ifid_) {
// Get Iface and WIFI info // Get Iface and WIFI info
net->getInterfaceAddress(); net->getInterfaceAddress();
net->thread_timer_.wake_up(); 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) { } else if (nh->nlmsg_type == RTM_DELADDR) {
auto rtif = static_cast<struct ifinfomsg *>(NLMSG_DATA(nh)); auto rtif = static_cast<struct ifinfomsg *>(NLMSG_DATA(nh));
// Check for valid interface // Check for valid interface
if (rtif->ifi_index == static_cast<int>(net->ifid_)) { if (rtif->ifi_index == net->ifid_) {
net->ipaddr_.clear(); net->ipaddr_.clear();
net->netmask_.clear(); net->netmask_.clear();
net->cidr_ = 0; net->cidr_ = 0;