From 26be83f89325c578f1ca5b4efd0cc2d6f4fc14a0 Mon Sep 17 00:00:00 2001 From: Patrick Ziegler Date: Mon, 4 Jan 2021 10:25:52 +0100 Subject: [PATCH] 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 --- include/modules/alsa.hpp | 10 +- include/modules/backlight.hpp | 5 +- include/modules/bspwm.hpp | 7 +- include/modules/date.hpp | 2 +- include/modules/i3.hpp | 6 +- include/modules/menu.hpp | 4 +- include/modules/meta/base.hpp | 7 +- include/modules/meta/base.inl | 19 ++- include/modules/mpd.hpp | 13 +- include/modules/pulseaudio.hpp | 4 +- include/modules/systray.hpp | 3 +- include/modules/xbacklight.hpp | 6 +- include/modules/xkeyboard.hpp | 2 +- include/modules/xworkspaces.hpp | 7 +- include/utils/action_router.hpp | 94 +++++++++++++++ src/components/controller.cpp | 12 +- src/modules/alsa.cpp | 120 ++++++++++--------- src/modules/backlight.cpp | 23 ++-- src/modules/bspwm.cpp | 64 +++++----- src/modules/date.cpp | 8 +- src/modules/i3.cpp | 75 ++++++------ src/modules/menu.cpp | 105 ++++++++-------- src/modules/mpd.cpp | 145 +++++++++++++++-------- src/modules/pulseaudio.cpp | 35 +++--- src/modules/systray.cpp | 16 +-- src/modules/xbacklight.cpp | 52 ++++---- src/modules/xkeyboard.cpp | 10 +- src/modules/xworkspaces.cpp | 38 ++++-- tests/CMakeLists.txt | 1 + tests/unit_tests/utils/action_router.cpp | 48 ++++++++ 30 files changed, 585 insertions(+), 356 deletions(-) create mode 100644 include/utils/action_router.hpp create mode 100644 tests/unit_tests/utils/action_router.cpp diff --git a/include/modules/alsa.hpp b/include/modules/alsa.hpp index 21903382..dd28b03a 100644 --- a/include/modules/alsa.hpp +++ b/include/modules/alsa.hpp @@ -36,7 +36,15 @@ namespace modules { static constexpr auto EVENT_TOGGLE = "toggle"; protected: - bool input(const string& action, const string& data); + void action_inc(); + void action_dec(); + void action_toggle(); + + void change_volume(int interval); + + void action_epilogue(const vector& mixers); + + vector get_mixers(); private: static constexpr auto FORMAT_VOLUME = "format-volume"; diff --git a/include/modules/backlight.hpp b/include/modules/backlight.hpp index 779821ba..34dcfe4a 100644 --- a/include/modules/backlight.hpp +++ b/include/modules/backlight.hpp @@ -32,7 +32,10 @@ namespace modules { static constexpr const char* EVENT_DEC = "dec"; protected: - bool input(const string& action, const string& data); + void action_inc(); + void action_dec(); + + void change_value(int value_mod); private: static constexpr auto TAG_LABEL = "