From 191fb2972cd56851bf777d3ac58d68a681b3af31 Mon Sep 17 00:00:00 2001 From: patrick96 Date: Sun, 22 Nov 2020 23:05:45 +0100 Subject: [PATCH] actions: Integrate input_handler into module Only modules can now be action handlers. This also slightly simplifies the controller because we don't need to keep track of input handlers, we can just use the module list. --- include/components/builder.hpp | 9 +++--- include/components/controller.hpp | 9 ------ include/modules/meta/base.hpp | 14 +++++++-- include/modules/meta/base.inl | 5 --- include/modules/meta/input_handler.hpp | 28 ----------------- include/modules/unsupported.hpp | 5 +-- include/utils/actions.hpp | 7 +++-- src/components/builder.cpp | 14 ++++----- src/components/controller.cpp | 42 +++++++++++--------------- src/utils/actions.cpp | 13 ++++---- 10 files changed, 54 insertions(+), 92 deletions(-) delete mode 100644 include/modules/meta/input_handler.hpp diff --git a/include/components/builder.hpp b/include/components/builder.hpp index c847d2ec..30386057 100644 --- a/include/components/builder.hpp +++ b/include/components/builder.hpp @@ -4,14 +4,15 @@ #include "common.hpp" #include "components/types.hpp" -#include "modules/meta/input_handler.hpp" - POLYBAR_NS using std::map; // fwd decl using namespace drawtypes; +namespace modules { + struct module_interface; +} class builder { public: @@ -49,9 +50,9 @@ class builder { void underline_close(); void control(controltag tag); void action(mousebtn index, string action); - void action(mousebtn btn, const modules::input_handler& handler, string action, string data); + void action(mousebtn btn, const modules::module_interface& module, string action, string data); void action(mousebtn index, string action, const label_t& label); - void action(mousebtn btn, const modules::input_handler& handler, string action, string data, const label_t& label); + void action(mousebtn btn, const modules::module_interface& module, string action, string data, const label_t& label); void action_close(); protected: diff --git a/include/components/controller.hpp b/include/components/controller.hpp index 5ea105fc..eb7a40b1 100644 --- a/include/components/controller.hpp +++ b/include/components/controller.hpp @@ -29,7 +29,6 @@ class logger; class signal_emitter; namespace modules { struct module_interface; - class input_handler; } // namespace modules using module_t = shared_ptr; using modulemap_t = std::map>; @@ -117,14 +116,6 @@ class controller */ modulemap_t m_blocks; - /** - * \brief Input handlers modules - * - * Maps the name of the input handler (module name) to the corresponding input - * handler - */ - std::vector> m_inputhandlers; - /** * \brief Maximum number of subsequent events to swallow */ diff --git a/include/modules/meta/base.hpp b/include/modules/meta/base.hpp index acab76ac..b23644d1 100644 --- a/include/modules/meta/base.hpp +++ b/include/modules/meta/base.hpp @@ -9,7 +9,6 @@ #include "common.hpp" #include "components/types.hpp" #include "errors.hpp" -#include "modules/meta/input_handler.hpp" #include "utils/concurrency.hpp" #include "utils/functional.hpp" #include "utils/inotify.hpp" @@ -112,6 +111,16 @@ namespace modules { virtual string name() const = 0; virtual bool running() const = 0; + /** + * Handle action, possibly with data attached + * + * Any implementation is free to ignore the data, if the action does not + * require additional data. + * + * \returns true if the action is supported and false otherwise + */ + virtual bool input(const string& action, const string& data) = 0; + virtual void start() = 0; virtual void stop() = 0; virtual void halt(string error_message) = 0; @@ -122,7 +131,7 @@ namespace modules { // class definition : module {{{ template - class module : public module_interface, public input_handler { + class module : public module_interface { public: module(const bar_settings bar, string name); ~module() noexcept; @@ -138,7 +147,6 @@ namespace modules { string contents(); bool input(const string& action, const string& data); - string input_handler_name() const; static constexpr auto TYPE = ""; diff --git a/include/modules/meta/base.inl b/include/modules/meta/base.inl index f1827934..5e713cd1 100644 --- a/include/modules/meta/base.inl +++ b/include/modules/meta/base.inl @@ -109,11 +109,6 @@ namespace modules { return false; } - template - string module::input_handler_name() const { - return m_name_raw; - } - // }}} // module protected {{{ diff --git a/include/modules/meta/input_handler.hpp b/include/modules/meta/input_handler.hpp deleted file mode 100644 index 60a5c202..00000000 --- a/include/modules/meta/input_handler.hpp +++ /dev/null @@ -1,28 +0,0 @@ -#pragma once - -#include "common.hpp" - -POLYBAR_NS - -namespace modules { - class input_handler { - public: - virtual ~input_handler() {} - /** - * Handle action, possibly with data attached - * - * Any implementation is free to ignore the data, if the action does not - * require additional data. - * - * \returns true if the action is supported and false otherwise - */ - virtual bool input(const string& action, const string& data) = 0; - - /** - * The name of this input handler - */ - virtual string input_handler_name() const = 0; - }; -} - -POLYBAR_NS_END diff --git a/include/modules/unsupported.hpp b/include/modules/unsupported.hpp index 1388e456..4694cfc5 100644 --- a/include/modules/unsupported.hpp +++ b/include/modules/unsupported.hpp @@ -9,7 +9,7 @@ namespace modules { struct module_interface; #define DEFINE_UNSUPPORTED_MODULE(MODULE_NAME, MODULE_TYPE) \ - class MODULE_NAME : public module_interface, public input_handler { \ + class MODULE_NAME : public module_interface { \ public: \ MODULE_NAME(const bar_settings, string) { \ throw application_error("No built-in support for '" + string{MODULE_TYPE} + "'"); \ @@ -33,9 +33,6 @@ namespace modules { string contents() { \ return ""; \ } \ - string input_handler_name() const { \ - return ""; \ - } \ bool input(const string&, const string&) { \ return false; \ } \ diff --git a/include/utils/actions.hpp b/include/utils/actions.hpp index 17e6d5b7..7ed20e3d 100644 --- a/include/utils/actions.hpp +++ b/include/utils/actions.hpp @@ -1,12 +1,15 @@ #pragma once #include "common.hpp" -#include "modules/meta/input_handler.hpp" + +namespace modules { + struct module_interface; +} // namespace modules POLYBAR_NS namespace actions_util { - string get_action_string(const modules::input_handler& handler, string action, string data); + string get_action_string(const modules::module_interface& module, string action, string data); /** * Parses an action string of the form "#name.action[.data]". diff --git a/src/components/builder.cpp b/src/components/builder.cpp index d3f5903b..0ee1dc9f 100644 --- a/src/components/builder.cpp +++ b/src/components/builder.cpp @@ -3,10 +3,10 @@ #include #include "drawtypes/label.hpp" +#include "utils/actions.hpp" #include "utils/color.hpp" #include "utils/string.hpp" #include "utils/time.hpp" -#include "utils/actions.hpp" POLYBAR_NS builder::builder(const bar_settings& bar) : m_bar(bar) { @@ -440,10 +440,10 @@ void builder::action(mousebtn index, string action) { } /** - * Open action tag for the action of the given input_handler + * Open action tag for the action of the given module */ -void builder::action(mousebtn btn, const modules::input_handler& handler, string action_name, string data) { - action(btn, actions_util::get_action_string(handler, action_name, data)); +void builder::action(mousebtn btn, const modules::module_interface& module, string action_name, string data) { + action(btn, actions_util::get_action_string(module, action_name, data)); } /** @@ -457,12 +457,12 @@ void builder::action(mousebtn index, string action_name, const label_t& label) { } } - /** * Wrap label in module action tag */ -void builder::action(mousebtn btn, const modules::input_handler& handler, string action_name, string data, const label_t& label) { - action(btn, actions_util::get_action_string(handler, action_name, data), label); +void builder::action( + mousebtn btn, const modules::module_interface& module, string action_name, string data, const label_t& label) { + action(btn, actions_util::get_action_string(module, action_name, data), label); } /** diff --git a/src/components/controller.cpp b/src/components/controller.cpp index 51cc6fc9..703b2683 100644 --- a/src/components/controller.cpp +++ b/src/components/controller.cpp @@ -11,6 +11,7 @@ #include "components/types.hpp" #include "events/signal.hpp" #include "events/signal_emitter.hpp" +#include "modules/meta/base.hpp" #include "modules/meta/event_handler.hpp" #include "modules/meta/factory.hpp" #include "utils/actions.hpp" @@ -146,14 +147,8 @@ bool controller::run(bool writeback, string snapshot_dst) { size_t started_modules{0}; for (const auto& module : m_modules) { - auto inp_handler = std::dynamic_pointer_cast(module); auto evt_handler = dynamic_cast(&*module); - if (inp_handler) { - m_log.trace("Registering module %s as input handler", module->name_raw()); - m_inputhandlers.push_back(inp_handler); - } - if (evt_handler != nullptr) { evt_handler->connect(m_connection); } @@ -474,20 +469,19 @@ bool controller::try_forward_legacy_action(const string& cmd) { string action = entry.second.second; // Search for the first module that matches the type for this legacy action - for (auto&& mod : m_modules) { - if (mod->type() == type) { - auto handler_ptr = std::dynamic_pointer_cast(mod); - auto handler_name = handler_ptr->input_handler_name(); + for (auto&& module : m_modules) { + if (module->type() == type) { + auto module_name = module->name_raw(); // TODO make this message more descriptive and maybe link to some documentation // TODO use route to string methods to print action name that should be used. if (data.empty()) { - m_log.warn("The action '%s' is deprecated, use '#%s.%s' instead!", cmd, handler_name, action); + m_log.warn("The action '%s' is deprecated, use '#%s.%s' instead!", cmd, module_name, action); } else { - m_log.warn("The action '%s' is deprecated, use '#%s.%s.%s' instead!", cmd, handler_name, action, data); + m_log.warn("The action '%s' is deprecated, use '#%s.%s.%s' instead!", cmd, module_name, action, data); } m_log.info( - "Forwarding legacy action '%s' to module '%s' as '%s' with data '%s'", cmd, handler_name, action, data); - if (!handler_ptr->input(action, data)) { + "Forwarding legacy action '%s' to module '%s' as '%s' with data '%s'", cmd, module_name, action, data); + if (!module->input(action, data)) { m_log.err("Failed to forward deprecated action to %s module", type); // Forward to shell if the module cannot accept the action to not break existing behavior. return false; @@ -521,14 +515,14 @@ void controller::process_inputdata() { // Every command that starts with '#' is considered an action string. if (cmd.front() == '#') { - string handler_name; + string module_name; string action; string data; try { auto res = actions_util::parse_action_string(cmd); - handler_name = std::get<0>(res); + module_name = std::get<0>(res); action = std::get<1>(res); data = std::get<2>(res); } catch (runtime_error& e) { @@ -536,15 +530,15 @@ void controller::process_inputdata() { return; } - m_log.info("Forwarding data to input handlers (name: '%s', action: '%s', data: '%s') ", handler_name, action, data); + m_log.info("Forwarding action to modules (name: '%s', action: '%s', data: '%s') ", module_name, action, data); int num_delivered = 0; - // Forwards the action to all input handlers that match the name - for (auto&& handler : m_inputhandlers) { - if (handler->input_handler_name() == handler_name) { - if (!handler->input(action, data)) { - m_log.err("The '%s' module does not support the '%s' action.", handler_name, action); + // Forwards the action to all modules that match the name + for (auto&& module : m_modules) { + if (module->name_raw() == module_name) { + if (!module->input(action, data)) { + m_log.err("The '%s' module does not support the '%s' action.", module_name, action); } num_delivered++; @@ -552,9 +546,9 @@ void controller::process_inputdata() { } if (num_delivered == 0) { - m_log.err("There exists no input handler with name '%s' (input: %s)", handler_name, cmd); + m_log.err("There exists no module with name '%s' (input: %s)", module_name, cmd); } else { - m_log.info("Delivered input to %d input handler%s", num_delivered, num_delivered > 1 ? "s" : ""); + m_log.info("Delivered action to %d module%s", num_delivered, num_delivered > 1 ? "s" : ""); } return; } diff --git a/src/utils/actions.cpp b/src/utils/actions.cpp index a4ae567b..d10f6934 100644 --- a/src/utils/actions.cpp +++ b/src/utils/actions.cpp @@ -4,12 +4,13 @@ #include #include "common.hpp" +#include "modules/meta/base.hpp" POLYBAR_NS namespace actions_util { - string get_action_string(const modules::input_handler& handler, string action, string data) { - string str = "#" + handler.input_handler_name() + "." + action; + string get_action_string(const modules::module_interface& module, string action, string data) { + string str = "#" + module.name_raw() + "." + action; if (!data.empty()) { str += "." + data; } @@ -28,10 +29,10 @@ namespace actions_util { throw std::runtime_error("Missing separator between name and action"); } - auto handler_name = action_str.substr(0, action_sep); + auto module_name = action_str.substr(0, action_sep); - if (handler_name.empty()) { - throw std::runtime_error("The handler name must not be empty"); + if (module_name.empty()) { + throw std::runtime_error("The module name must not be empty"); } auto action = action_str.substr(action_sep + 1); @@ -47,7 +48,7 @@ namespace actions_util { throw std::runtime_error("The action name must not be empty"); } - return std::tuple{handler_name, action, data}; + return std::tuple{module_name, action, data}; } } // namespace actions_util