From e42fae32ab3aee5d52d94d1b3ee2292287d060e3 Mon Sep 17 00:00:00 2001 From: Alexis Date: Fri, 16 Nov 2018 10:02:12 +0100 Subject: [PATCH] feat(network): network info interval --- include/modules/battery.hpp | 2 +- include/modules/network.hpp | 4 +++- src/client.cpp | 4 ++++ src/modules/battery.cpp | 4 ++-- src/modules/cpu.cpp | 2 +- src/modules/network.cpp | 25 +++++++++++++++++++------ 6 files changed, 30 insertions(+), 11 deletions(-) diff --git a/include/modules/battery.hpp b/include/modules/battery.hpp index 3974b014..29b89b6f 100644 --- a/include/modules/battery.hpp +++ b/include/modules/battery.hpp @@ -34,7 +34,7 @@ class Battery : public ALabel { std::string getState(uint16_t); util::SleeperThread thread_; - util::SleeperThread threadTimer_; + util::SleeperThread thread_timer_; std::vector batteries_; int fd_; std::string old_status_; diff --git a/include/modules/network.hpp b/include/modules/network.hpp index 50b77e7d..c120846d 100644 --- a/include/modules/network.hpp +++ b/include/modules/network.hpp @@ -23,6 +23,7 @@ class Network : public ALabel { static int netlinkResponse(int, void*, uint32_t, uint32_t groups = 0); static int scanCb(struct nl_msg*, void*); + void worker(); void disconnected(); void initNL80211(); int getExternalInterface(); @@ -33,10 +34,11 @@ class Network : public ALabel { auto getInfo() -> void; waybar::util::SleeperThread thread_; + waybar::util::SleeperThread thread_timer_; int ifid_; sa_family_t family_; int sock_fd_; - struct sockaddr_nl nladdr_ = {}; + struct sockaddr_nl nladdr_ = {0}; struct nl_sock* sk_ = nullptr; int nl80211_id_; diff --git a/src/client.cpp b/src/client.cpp index ab8b1ed8..945e38ad 100644 --- a/src/client.cpp +++ b/src/client.cpp @@ -90,6 +90,10 @@ void waybar::Client::bindInterfaces() }; wl_registry_add_listener(registry, ®istry_listener, this); wl_display_roundtrip(wl_display); + if (!layer_shell || !seat || !xdg_output_manager) { + throw std::runtime_error("Failed to acquire required resources."); + } + wl_display_roundtrip(wl_display); } int waybar::Client::main(int /*argc*/, char* /*argv*/[]) diff --git a/src/modules/battery.cpp b/src/modules/battery.cpp index bff44470..3e2626b6 100644 --- a/src/modules/battery.cpp +++ b/src/modules/battery.cpp @@ -47,7 +47,7 @@ void waybar::modules::Battery::worker() // Trigger first values update(); uint32_t interval = config_["interval"].isUInt() ? config_["interval"].asUInt() : 60; - threadTimer_ = [this, interval] { + thread_timer_ = [this, interval] { thread_.sleep_for(chrono::seconds(interval)); dp.emit(); }; @@ -58,7 +58,7 @@ void waybar::modules::Battery::worker() return; } // TODO: don't stop timer for now since there is some bugs :? - // threadTimer_.stop(); + // thread_timer_.stop(); dp.emit(); }; } diff --git a/src/modules/cpu.cpp b/src/modules/cpu.cpp index 5a639629..01a432bb 100644 --- a/src/modules/cpu.cpp +++ b/src/modules/cpu.cpp @@ -14,7 +14,7 @@ waybar::modules::Cpu::Cpu(const Json::Value& config) auto waybar::modules::Cpu::update() -> void { try { - // TODO: as creating dynamic fmt::arg arrays is buggy we have to do this + // TODO: as creating dynamic fmt::arg arrays is buggy we have to calc both auto cpu_load = getCpuLoad(); auto [cpu_usage, tooltip] = getCpuUsage(); label_.set_tooltip_text(tooltip); diff --git a/src/modules/network.cpp b/src/modules/network.cpp index 3daa62fb..a4fefa17 100644 --- a/src/modules/network.cpp +++ b/src/modules/network.cpp @@ -34,6 +34,17 @@ waybar::modules::Network::Network(const Json::Value& config) // Trigger first values getInfo(); update(); + worker(); +} + +waybar::modules::Network::~Network() +{ + close(sock_fd_); + nl_socket_free(sk_); +} + +void waybar::modules::Network::worker() +{ thread_ = [this] { char buf[4096]; uint64_t len = netlinkResponse(sock_fd_, buf, sizeof(buf), @@ -74,12 +85,14 @@ waybar::modules::Network::Network(const Json::Value& config) dp.emit(); } }; -} - -waybar::modules::Network::~Network() -{ - close(sock_fd_); - nl_socket_free(sk_); + uint32_t interval = config_["interval"].isUInt() ? config_["interval"].asUInt() : 60; + thread_timer_ = [this, interval] { + thread_.sleep_for(std::chrono::seconds(interval)); + if (ifid_ > 0) { + getInfo(); + dp.emit(); + } + }; } auto waybar::modules::Network::update() -> void