controller: Cleanup process_inputdata

This commit is contained in:
patrick96 2020-11-25 10:16:26 +01:00 committed by Patrick Ziegler
parent 1afb333858
commit ff3340e062
3 changed files with 37 additions and 32 deletions

View File

@ -10,6 +10,7 @@
#include "events/signal_receiver.hpp" #include "events/signal_receiver.hpp"
#include "events/types.hpp" #include "events/types.hpp"
#include "settings.hpp" #include "settings.hpp"
#include "utils/actions.hpp"
#include "utils/file.hpp" #include "utils/file.hpp"
#include "x11/types.hpp" #include "x11/types.hpp"
@ -74,6 +75,7 @@ class controller
private: private:
size_t setup_modules(alignment align); size_t setup_modules(alignment align);
bool forward_action(const actions_util::action& cmd);
bool try_forward_legacy_action(const string& cmd); bool try_forward_legacy_action(const string& cmd);
connection& m_connection; connection& m_connection;

View File

@ -9,6 +9,9 @@ namespace modules {
POLYBAR_NS POLYBAR_NS
namespace actions_util { namespace actions_util {
using action = std::tuple<string, string, string>;
string get_action_string(const modules::module_interface& module, string action, string data); string get_action_string(const modules::module_interface& module, string action, string data);
/** /**
@ -22,7 +25,7 @@ namespace actions_util {
* same result. * same result.
* \throws runtime_error If the action string is malformed * \throws runtime_error If the action string is malformed
*/ */
std::tuple<string, string, string> parse_action_string(string action); action parse_action_string(string action);
} // namespace actions_util } // namespace actions_util
POLYBAR_NS_END POLYBAR_NS_END

View File

@ -501,6 +501,35 @@ bool controller::try_forward_legacy_action(const string& cmd) {
return false; 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 * Process stored input data
*/ */
@ -516,41 +545,12 @@ void controller::process_inputdata() {
// Every command that starts with '#' is considered an action string. // Every command that starts with '#' is considered an action string.
if (cmd.front() == '#') { if (cmd.front() == '#') {
string module_name;
string action;
string data;
try { try {
auto res = actions_util::parse_action_string(cmd); this->forward_action(actions_util::parse_action_string(cmd));
module_name = std::get<0>(res);
action = std::get<1>(res);
data = std::get<2>(res);
} catch (runtime_error& e) { } catch (runtime_error& e) {
m_log.err("Invalid action string (reason: %s)", e.what()); m_log.err("Invalid action string (action: %s, reason: %s)", cmd, e.what());
return;
} }
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; return;
} }