From ff3340e062b01cd77d3837399daa1078bbe04b3d Mon Sep 17 00:00:00 2001 From: patrick96 Date: Wed, 25 Nov 2020 10:16:26 +0100 Subject: [PATCH] controller: Cleanup process_inputdata --- include/components/controller.hpp | 2 + include/utils/actions.hpp | 5 ++- src/components/controller.cpp | 62 +++++++++++++++---------------- 3 files changed, 37 insertions(+), 32 deletions(-) diff --git a/include/components/controller.hpp b/include/components/controller.hpp index eb7a40b1..18c3e679 100644 --- a/include/components/controller.hpp +++ b/include/components/controller.hpp @@ -10,6 +10,7 @@ #include "events/signal_receiver.hpp" #include "events/types.hpp" #include "settings.hpp" +#include "utils/actions.hpp" #include "utils/file.hpp" #include "x11/types.hpp" @@ -74,6 +75,7 @@ class controller private: size_t setup_modules(alignment align); + bool forward_action(const actions_util::action& cmd); bool try_forward_legacy_action(const string& cmd); connection& m_connection; diff --git a/include/utils/actions.hpp b/include/utils/actions.hpp index 7ed20e3d..1aa74531 100644 --- a/include/utils/actions.hpp +++ b/include/utils/actions.hpp @@ -9,6 +9,9 @@ namespace modules { POLYBAR_NS namespace actions_util { + + using action = std::tuple; + string get_action_string(const modules::module_interface& module, string action, string data); /** @@ -22,7 +25,7 @@ namespace actions_util { * same result. * \throws runtime_error If the action string is malformed */ - std::tuple parse_action_string(string action); + action parse_action_string(string action); } // namespace actions_util POLYBAR_NS_END diff --git a/src/components/controller.cpp b/src/components/controller.cpp index 3880c91e..d809a1ca 100644 --- a/src/components/controller.cpp +++ b/src/components/controller.cpp @@ -501,6 +501,35 @@ bool controller::try_forward_legacy_action(const string& cmd) { return false; } +bool controller::forward_action(const actions_util::action& action_triple) { + string module_name = std::get<0>(action_triple); + string action = std::get<1>(action_triple); + string data = std::get<2>(action_triple); + + m_log.info("Forwarding action to modules (module: '%s', action: '%s', data: '%s')", module_name, action, data); + + int num_delivered = 0; + + // 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++; + } + } + + if (num_delivered == 0) { + m_log.err("Could not forward action to module: No module named '%s' (action: '%s', data: '%s')", module_name, + action, data); + } else { + m_log.info("Delivered action to %d module%s", num_delivered, num_delivered > 1 ? "s" : ""); + } + return true; +} + /** * Process stored input data */ @@ -516,41 +545,12 @@ void controller::process_inputdata() { // Every command that starts with '#' is considered an action string. if (cmd.front() == '#') { - string module_name; - string action; - string data; - try { - auto res = actions_util::parse_action_string(cmd); - - module_name = std::get<0>(res); - action = std::get<1>(res); - data = std::get<2>(res); + this->forward_action(actions_util::parse_action_string(cmd)); } catch (runtime_error& e) { - m_log.err("Invalid action string (reason: %s)", e.what()); - return; + m_log.err("Invalid action string (action: %s, reason: %s)", cmd, e.what()); } - 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 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++; - } - } - - if (num_delivered == 0) { - m_log.err("There exists no module with name '%s' (input: %s)", module_name, cmd); - } else { - m_log.info("Delivered action to %d module%s", num_delivered, num_delivered > 1 ? "s" : ""); - } return; }