From e47e4399549659c80a706a43e0790d9961f75e54 Mon Sep 17 00:00:00 2001 From: Michael Carlberg Date: Thu, 22 Dec 2016 22:11:03 +0100 Subject: [PATCH] refactor(modules): Replace callbacks with signals Refs #265 --- include/components/controller.hpp | 7 ++++--- include/events/signal.hpp | 1 + include/events/signal_fwd.hpp | 1 + include/modules/meta/base.hpp | 14 +++++--------- include/modules/meta/base.inl | 27 ++++++++------------------- src/components/controller.cpp | 14 ++++++++------ 6 files changed, 27 insertions(+), 37 deletions(-) diff --git a/include/components/controller.hpp b/include/components/controller.hpp index d071ce6e..72899761 100644 --- a/include/components/controller.hpp +++ b/include/components/controller.hpp @@ -39,9 +39,9 @@ namespace sig_ev = signals::eventqueue; namespace sig_ui = signals::ui; namespace sig_ipc = signals::ipc; -class controller : public signal_receiver { +class controller : public signal_receiver { public: using make_type = unique_ptr; static make_type make(unique_ptr&& ipc, unique_ptr&& config_watch); @@ -60,6 +60,7 @@ class controller : public signal_receiver #include #include -#include #include +#include #include "common.hpp" -#include "components/config.hpp" -#include "components/logger.hpp" #include "components/types.hpp" #include "errors.hpp" #include "utils/concurrency.hpp" @@ -46,6 +44,9 @@ namespace drawtypes { } class builder; +class config; +class logger; +class signal_emitter; // }}} @@ -108,9 +109,6 @@ namespace modules { virtual void stop() = 0; virtual void halt(string error_message) = 0; virtual string contents() = 0; - - virtual void set_update_cb(callback<>&& cb) = 0; - virtual void set_stop_cb(callback<>&& cb) = 0; }; // }}} @@ -141,9 +139,7 @@ namespace modules { string get_output(); protected: - callback<> m_update_callback; - callback<> m_stop_callback; - + signal_emitter& m_sig; const bar_settings m_bar; const logger& m_log; const config& m_conf; diff --git a/include/modules/meta/base.inl b/include/modules/meta/base.inl index 9cc7ccfc..e291026a 100644 --- a/include/modules/meta/base.inl +++ b/include/modules/meta/base.inl @@ -1,15 +1,20 @@ #include "components/builder.hpp" #include "components/logger.hpp" #include "components/config.hpp" +#include "events/signal.hpp" +#include "events/signal_emitter.hpp" POLYBAR_NS +namespace sig_ev = signals::eventqueue; + namespace modules { // module public {{{ template module::module(const bar_settings bar, string name) - : m_bar(bar) + : m_sig(signal_emitter::make()) + , m_bar(bar) , m_log(logger::make()) , m_conf(config::make()) , m_name("module/" + name) @@ -30,16 +35,6 @@ namespace modules { } } - template - void module::set_update_cb(callback<>&& cb) { - m_update_callback = forward(cb); - } - - template - void module::set_stop_cb(callback<>&& cb) { - m_stop_callback = forward(cb); - } - template string module::name() const { return m_name; @@ -66,9 +61,7 @@ namespace modules { CAST_MOD(Impl)->wakeup(); CAST_MOD(Impl)->teardown(); - if (m_stop_callback) { - m_stop_callback(); - } + m_sig.emit(sig_ev::process_check{}); } } @@ -97,11 +90,7 @@ namespace modules { } m_cache = CAST_MOD(Impl)->get_output(); - - if (m_update_callback) - m_update_callback(); - else - m_log.info("%s: No handler, ignoring broadcast...", name()); + m_sig.emit(sig_ev::process_broadcast{}); } template diff --git a/src/components/controller.cpp b/src/components/controller.cpp index 234174c8..0a7fdd24 100644 --- a/src/components/controller.cpp +++ b/src/components/controller.cpp @@ -162,8 +162,6 @@ bool controller::run(bool writeback) { try { m_log.info("Starting %s", module->name()); module->start(); - module->set_update_cb([&] { enqueue(make_update_evt(false)); }); - module->set_stop_cb([&] { enqueue(make_check_evt()); }); started_modules++; } catch (const application_error& err) { m_log.err("Failed to start '%s' (reason: %s)", module->name(), err.what()); @@ -352,16 +350,20 @@ void controller::process_inputdata() { } } +/** + * Process broadcast events + */ +bool controller::on(const sig_ev::process_broadcast&) { + enqueue(make_update_evt(false)); + return true; +} + /** * Process eventqueue update event */ bool controller::on(const sig_ev::process_update& evt) { bool force{evt.data()->flag}; - if (!m_bar) { - return false; - } - const bar_settings& bar{m_bar->settings()}; string contents; string separator{bar.separator};