mirror of
https://github.com/polybar/polybar.git
synced 2026-03-04 07:19:15 +00:00
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:
@ -1,13 +1,13 @@
|
||||
#include "modules/xbacklight.hpp"
|
||||
|
||||
#include "drawtypes/label.hpp"
|
||||
#include "drawtypes/progressbar.hpp"
|
||||
#include "drawtypes/ramp.hpp"
|
||||
#include "modules/meta/base.inl"
|
||||
#include "utils/math.hpp"
|
||||
#include "x11/connection.hpp"
|
||||
#include "x11/winspec.hpp"
|
||||
|
||||
#include "modules/meta/base.inl"
|
||||
|
||||
POLYBAR_NS
|
||||
|
||||
namespace modules {
|
||||
@ -18,6 +18,9 @@ namespace modules {
|
||||
*/
|
||||
xbacklight_module::xbacklight_module(const bar_settings& bar, string name_)
|
||||
: static_module<xbacklight_module>(bar, move(name_)), m_connection(connection::make()) {
|
||||
m_router->register_action(EVENT_INC, &xbacklight_module::action_inc);
|
||||
m_router->register_action(EVENT_DEC, &xbacklight_module::action_dec);
|
||||
|
||||
auto output = m_conf.get(name(), "output", m_bar.monitor->name);
|
||||
|
||||
auto monitors = randr_util::get_monitors(m_connection, m_connection.root(), bar.monitor_strict, false);
|
||||
@ -144,34 +147,23 @@ namespace modules {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Process scroll events by changing backlight value
|
||||
*/
|
||||
bool xbacklight_module::input(const string& action, const string&) {
|
||||
double value_mod{0.0};
|
||||
|
||||
if (action == EVENT_INC) {
|
||||
value_mod = 5.0;
|
||||
m_log.info("%s: Increasing value by %i%", name(), value_mod);
|
||||
} else if (action == EVENT_DEC) {
|
||||
value_mod = -5.0;
|
||||
m_log.info("%s: Decreasing value by %i%", name(), -value_mod);
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
int rounded = math_util::cap<double>(m_percentage + value_mod, 0.0, 100.0) + 0.5;
|
||||
const int values[1]{math_util::percentage_to_value<int>(rounded, m_output->backlight.max)};
|
||||
|
||||
m_connection.change_output_property_checked(
|
||||
m_output->output, m_output->backlight.atom, XCB_ATOM_INTEGER, 32, XCB_PROP_MODE_REPLACE, 1, values);
|
||||
} catch (const exception& err) {
|
||||
m_log.err("%s: %s", name(), err.what());
|
||||
}
|
||||
|
||||
return true;
|
||||
void xbacklight_module::action_inc() {
|
||||
change_value(5);
|
||||
}
|
||||
}
|
||||
|
||||
void xbacklight_module::action_dec() {
|
||||
change_value(-5);
|
||||
}
|
||||
|
||||
void xbacklight_module::change_value(int value_mod) {
|
||||
m_log.info("%s: Changing value by %i%", name(), value_mod);
|
||||
int rounded = math_util::cap<double>(m_percentage + value_mod, 0.0, 100.0) + 0.5;
|
||||
|
||||
const int values[1]{math_util::percentage_to_value<int>(rounded, m_output->backlight.max)};
|
||||
|
||||
m_connection.change_output_property_checked(
|
||||
m_output->output, m_output->backlight.atom, XCB_ATOM_INTEGER, 32, XCB_PROP_MODE_REPLACE, 1, values);
|
||||
}
|
||||
} // namespace modules
|
||||
|
||||
POLYBAR_NS_END
|
||||
|
||||
Reference in New Issue
Block a user