From 2512d515647b2c0866452dacd897bc7c0500822b Mon Sep 17 00:00:00 2001 From: Alexis Date: Fri, 10 Aug 2018 23:21:21 +0200 Subject: [PATCH] fix(gtk): use idle to avoid mutex --- include/modules/clock.hpp | 1 - include/modules/cpu.hpp | 1 - include/modules/custom.hpp | 1 - include/modules/memory.hpp | 1 - include/modules/network.hpp | 1 - include/modules/workspaces.hpp | 10 ++----- meson.build | 2 +- src/bar.cpp | 1 - src/modules/battery.cpp | 6 ++-- src/modules/clock.cpp | 4 ++- src/modules/cpu.cpp | 8 +++-- src/modules/custom.cpp | 8 +++-- src/modules/memory.cpp | 8 +++-- src/modules/network.cpp | 7 +++-- src/modules/workspaces.cpp | 54 ++++++++-------------------------- 15 files changed, 42 insertions(+), 71 deletions(-) diff --git a/include/modules/clock.hpp b/include/modules/clock.hpp index b20c6816..3f673c12 100644 --- a/include/modules/clock.hpp +++ b/include/modules/clock.hpp @@ -3,7 +3,6 @@ #include #include #include -#include #include "util/chrono.hpp" #include "IModule.hpp" diff --git a/include/modules/cpu.hpp b/include/modules/cpu.hpp index 56067644..86f00867 100644 --- a/include/modules/cpu.hpp +++ b/include/modules/cpu.hpp @@ -4,7 +4,6 @@ #include #include #include -#include #include "util/chrono.hpp" #include "IModule.hpp" diff --git a/include/modules/custom.hpp b/include/modules/custom.hpp index 03c2b021..c152fe64 100644 --- a/include/modules/custom.hpp +++ b/include/modules/custom.hpp @@ -3,7 +3,6 @@ #include #include #include -#include #include "util/chrono.hpp" #include "IModule.hpp" diff --git a/include/modules/memory.hpp b/include/modules/memory.hpp index eb62ebf8..aec5e0df 100644 --- a/include/modules/memory.hpp +++ b/include/modules/memory.hpp @@ -4,7 +4,6 @@ #include #include #include -#include #include "util/chrono.hpp" #include "IModule.hpp" diff --git a/include/modules/network.hpp b/include/modules/network.hpp index b8a16245..27a0f669 100644 --- a/include/modules/network.hpp +++ b/include/modules/network.hpp @@ -9,7 +9,6 @@ #include #include #include -#include #include "util/chrono.hpp" #include "IModule.hpp" diff --git a/include/modules/workspaces.hpp b/include/modules/workspaces.hpp index c5929617..33e47994 100644 --- a/include/modules/workspaces.hpp +++ b/include/modules/workspaces.hpp @@ -14,20 +14,14 @@ namespace waybar::modules { auto update() -> void; operator Gtk::Widget &(); private: - void _updateThread(); - static void _handle_idle(void *data, - struct org_kde_kwin_idle_timeout *timer); - static void _handle_resume(void *data, - struct org_kde_kwin_idle_timeout *timer); void _addWorkspace(Json::Value node); Json::Value _getWorkspaces(); Bar &_bar; - util::SleeperThread *_thread; - Gtk::Box *_box; + waybar::util::SleeperThread _thread; + Gtk::Box _box; std::unordered_map _buttons; int _ipcSocketfd; int _ipcEventSocketfd; - struct org_kde_kwin_idle_timeout *_idle_timer; }; } diff --git a/meson.build b/meson.build index dec251c3..b6c8a4f9 100644 --- a/meson.build +++ b/meson.build @@ -6,7 +6,7 @@ project( ) cpp_args = [] -cpp_link_args = [] +cpp_link_args = ['-g'] if false # libc++ cpp_args += ['-stdlib=libc++'] diff --git a/src/bar.cpp b/src/bar.cpp index 92629f35..97113fa7 100644 --- a/src/bar.cpp +++ b/src/bar.cpp @@ -5,7 +5,6 @@ #include "bar.hpp" #include "client.hpp" #include "factory.hpp" -#include "util/chrono.hpp" waybar::Bar::Bar(Client &client, std::unique_ptr &&p_output) : client(client), window{Gtk::WindowType::WINDOW_TOPLEVEL}, diff --git a/src/modules/battery.cpp b/src/modules/battery.cpp index 0067389e..e60d8c0b 100644 --- a/src/modules/battery.cpp +++ b/src/modules/battery.cpp @@ -20,10 +20,10 @@ waybar::modules::Battery::Battery(Json::Value config) } _label.get_style_context()->add_class("battery"); - - _thread = [this] { + int interval = _config["interval"] ? _config["inveral"].asInt() : 1; + _thread = [this, interval] { update(); - _thread.sleep_for(chrono::minutes(1)); + _thread.sleep_for(chrono::seconds(interval)); }; } diff --git a/src/modules/clock.cpp b/src/modules/clock.cpp index c84dc941..b7950715 100644 --- a/src/modules/clock.cpp +++ b/src/modules/clock.cpp @@ -5,7 +5,9 @@ waybar::modules::Clock::Clock(Json::Value config) { _label.get_style_context()->add_class("clock"); _thread = [this] { - update(); + Glib::signal_idle().connect_once([this] { + update(); + }); auto now = waybar::chrono::clock::now(); auto timeout = std::chrono::floor(now + std::chrono::minutes(1)); diff --git a/src/modules/cpu.cpp b/src/modules/cpu.cpp index 6678c0ca..534f50de 100644 --- a/src/modules/cpu.cpp +++ b/src/modules/cpu.cpp @@ -4,9 +4,11 @@ waybar::modules::Cpu::Cpu(Json::Value config) : _config(config) { _label.get_style_context()->add_class("cpu"); - _thread = [this] { - update(); - int interval = _config["interval"] ? _config["inveral"].asInt() : 10; + int interval = _config["interval"] ? _config["inveral"].asInt() : 10; + _thread = [this, interval] { + Glib::signal_idle().connect_once([this] { + update(); + }); _thread.sleep_for(chrono::seconds(interval)); }; }; diff --git a/src/modules/custom.cpp b/src/modules/custom.cpp index 8f5ab562..e6ad7c39 100644 --- a/src/modules/custom.cpp +++ b/src/modules/custom.cpp @@ -8,9 +8,11 @@ waybar::modules::Custom::Custom(std::string name, Json::Value config) std::cerr << name + " has no exec path." << std::endl; return; } - _thread = [this] { - update(); - int interval = _config["interval"] ? _config["inveral"].asInt() : 30; + int interval = _config["interval"] ? _config["inveral"].asInt() : 30; + _thread = [this, interval] { + Glib::signal_idle().connect_once([this] { + update(); + }); _thread.sleep_for(chrono::seconds(interval)); }; }; diff --git a/src/modules/memory.cpp b/src/modules/memory.cpp index 8d5274f1..a69cfe6a 100644 --- a/src/modules/memory.cpp +++ b/src/modules/memory.cpp @@ -4,9 +4,11 @@ waybar::modules::Memory::Memory(Json::Value config) : _config(config) { _label.get_style_context()->add_class("memory"); - _thread = [this] { - update(); - int interval = _config["interval"] ? _config["inveral"].asInt() : 30; + int interval = _config["interval"] ? _config["inveral"].asInt() : 30; + _thread = [this, interval] { + Glib::signal_idle().connect_once([this] { + update(); + }); _thread.sleep_for(chrono::seconds(interval)); }; }; diff --git a/src/modules/network.cpp b/src/modules/network.cpp index ad8d5e73..9aa6948e 100644 --- a/src/modules/network.cpp +++ b/src/modules/network.cpp @@ -4,8 +4,11 @@ waybar::modules::Network::Network(Json::Value config) : _config(config), _ifid(if_nametoindex(config["interface"].asString().c_str())) { _label.get_style_context()->add_class("network"); - _thread = [this] { - update(); + int interval = _config["interval"] ? _config["inveral"].asInt() : 30; + _thread = [this, interval] { + Glib::signal_idle().connect_once([this] { + update(); + }); _thread.sleep_for(chrono::minutes(1)); }; }; diff --git a/src/modules/workspaces.cpp b/src/modules/workspaces.cpp index 44d50504..195f97f3 100644 --- a/src/modules/workspaces.cpp +++ b/src/modules/workspaces.cpp @@ -2,27 +2,24 @@ #include "ipc/client.hpp" waybar::modules::Workspaces::Workspaces(Bar &bar) - : _bar(bar), _thread(nullptr), _box(Gtk::manage(new Gtk::Box)) + : _bar(bar) { - _box->get_style_context()->add_class("workspaces"); + _box.get_style_context()->add_class("workspaces"); std::string socketPath = get_socketpath(); _ipcSocketfd = ipc_open_socket(socketPath); _ipcEventSocketfd = ipc_open_socket(socketPath); const char *subscribe = "[ \"workspace\", \"mode\" ]"; uint32_t len = strlen(subscribe); ipc_single_command(_ipcEventSocketfd, IPC_SUBSCRIBE, subscribe, &len); - _idle_timer = - org_kde_kwin_idle_get_idle_timeout(_bar.client.idle_manager, - _bar.client.seat, 10000); // 10 seconds - static const struct org_kde_kwin_idle_timeout_listener idle_timer_listener = { - .idle = _handle_idle, - .resumed = _handle_resume, + _thread = [this] { + Glib::signal_idle().connect_once([this] { + update(); + }); + _thread.sleep_for(chrono::milliseconds(250)); }; - org_kde_kwin_idle_timeout_add_listener(_idle_timer, - &idle_timer_listener, this); - _updateThread(); } + auto waybar::modules::Workspaces::update() -> void { Json::Value workspaces = _getWorkspaces(); @@ -31,7 +28,7 @@ auto waybar::modules::Workspaces::update() -> void auto ws = std::find_if(workspaces.begin(), workspaces.end(), [it](auto node) -> bool { return node["num"].asInt() == it->first; }); if (ws == workspaces.end()) { - it->second.hide(); + it = _buttons.erase(it); needReorder = true; } } @@ -49,49 +46,24 @@ auto waybar::modules::Workspaces::update() -> void styleContext->add_class("current"); } if (needReorder) - _box->reorder_child(it->second, node["num"].asInt() - 1); + _box.reorder_child(it->second, node["num"].asInt() - 1); it->second.show(); } } } -void waybar::modules::Workspaces::_updateThread() -{ - _thread = new waybar::util::SleeperThread([this] { - update(); - _thread->sleep_for(waybar::chrono::milliseconds(150)); - }); -} - -void waybar::modules::Workspaces::_handle_idle(void *data, - struct org_kde_kwin_idle_timeout *timer) { - auto o = reinterpret_cast(data); - if (o->_thread) { - delete o->_thread; - o->_thread = nullptr; - } -} - -void waybar::modules::Workspaces::_handle_resume(void *data, - struct org_kde_kwin_idle_timeout *timer) { - auto o = reinterpret_cast(data); - if (!o->_thread) { - o->_updateThread(); - } -} - void waybar::modules::Workspaces::_addWorkspace(Json::Value node) { auto pair = _buttons.emplace(node["num"].asInt(), node["name"].asString()); auto &button = pair.first->second; + _box.pack_start(button, false, false, 0); button.set_relief(Gtk::RELIEF_NONE); button.signal_clicked().connect([this, pair] { auto value = fmt::format("workspace \"{}\"", pair.first->first); uint32_t size = value.size(); ipc_single_command(_ipcSocketfd, IPC_COMMAND, value.c_str(), &size); }); - _box->pack_start(button, false, false, 0); - _box->reorder_child(button, node["num"].asInt() - 1); + _box.reorder_child(button, node["num"].asInt() - 1); if (node["focused"].asBool()) { button.get_style_context()->add_class("current"); } @@ -117,5 +89,5 @@ Json::Value waybar::modules::Workspaces::_getWorkspaces() } waybar::modules::Workspaces::operator Gtk::Widget &() { - return *_box; + return _box; }