module: Implement action router (#2336)

* module: Implement proof of concept action router

Action implementation inside module becomes much cleaner because each
module just registers action names together with a callback (pointer to
member function) and the action router does the rest.

* Make input function final

This forces all modules to use the action router

* modules: Catch exceptions in action handlers

* Use action router for all modules

* Use action_ prefix for function names

The mpd module's 'stop' action overwrote the base module's stop function
which caused difficult to debug behavior.

To prevent this in the future we now prefix each function that is
responsible for an action with 'action_'

* Cleanup

* actions: Throw exception when re-registering action

Action names are unique inside modules. Unfortunately there is no way to
ensure this statically, the next best thing is to crash the module and
let the user know that this is a bug.

* Formatting

* actions: Ignore data for actions without data

This is the same behavior as before.

* action_router: Write tests
This commit is contained in:
Patrick Ziegler
2021-01-04 10:25:52 +01:00
committed by GitHub
parent 7521da900f
commit 26be83f893
30 changed files with 585 additions and 356 deletions

View File

@ -25,6 +25,9 @@ namespace modules {
backlight_module::backlight_module(const bar_settings& bar, string name_)
: inotify_module<backlight_module>(bar, move(name_)) {
m_router->register_action(EVENT_DEC, &backlight_module::action_dec);
m_router->register_action(EVENT_INC, &backlight_module::action_inc);
auto card = m_conf.get(name(), "card");
// Get flag to check if we should add scroll handlers for changing value
@ -113,18 +116,16 @@ namespace modules {
return true;
}
bool backlight_module::input(const string& action, const string&) {
double value_mod{0.0};
void backlight_module::action_inc() {
change_value(5);
}
if (action == EVENT_INC) {
value_mod = 5.0;
} else if (action == EVENT_DEC) {
value_mod = -5.0;
} else {
return false;
}
void backlight_module::action_dec() {
change_value(-5);
}
m_log.info("%s: Changing value by %f%", name(), value_mod);
void backlight_module::change_value(int value_mod) {
m_log.info("%s: Changing value by %d%", name(), value_mod);
try {
int rounded = math_util::cap<double>(m_percentage + value_mod, 0.0, 100.0) + 0.5;
@ -136,8 +137,6 @@ namespace modules {
"configuration. Please read the module documentation.\n(reason: %s)",
name(), err.what());
}
return true;
}
} // namespace modules