polybar-dwm/include/modules/xbacklight.hpp
Patrick Ziegler 26be83f893
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
2021-01-04 10:25:52 +01:00

65 lines
1.7 KiB
C++

#pragma once
#include "components/config.hpp"
#include "modules/meta/event_handler.hpp"
#include "modules/meta/static_module.hpp"
#include "settings.hpp"
#include "x11/extensions/randr.hpp"
POLYBAR_NS
class connection;
namespace modules {
/**
* Backlight module built using the RandR X extension.
*
* This is built as a replacement for the old backlight
* module that was set up using with inotify watches listening
* for changes to the raw file handle.
*
* This module is a lot faster, it's more responsive and it will
* be dormant until new values are reported. Inotify watches
* are a bit random when it comes to proc-/sysfs.
*/
class xbacklight_module : public static_module<xbacklight_module>, public event_handler<evt::randr_notify> {
public:
explicit xbacklight_module(const bar_settings& bar, string name_);
void update();
string get_output();
bool build(builder* builder, const string& tag) const;
static constexpr auto TYPE = "internal/xbacklight";
static constexpr const char* EVENT_INC = "inc";
static constexpr const char* EVENT_DEC = "dec";
protected:
void handle(const evt::randr_notify& evt);
void action_inc();
void action_dec();
void change_value(int value_mod);
private:
static constexpr const char* TAG_LABEL{"<label>"};
static constexpr const char* TAG_BAR{"<bar>"};
static constexpr const char* TAG_RAMP{"<ramp>"};
connection& m_connection;
monitor_t m_output;
xcb_window_t m_proxy{};
ramp_t m_ramp;
label_t m_label;
progressbar_t m_progressbar;
bool m_scroll{true};
std::atomic<int> m_percentage{0};
};
} // namespace modules
POLYBAR_NS_END