1
0
Fork 0
mirror of https://github.com/polybar/polybar.git synced 2024-11-25 13:55:47 -05:00

refactor(modules): Handle events using signal_receiver

This commit is contained in:
Michael Carlberg 2016-12-21 08:38:44 +01:00
parent c01f111e34
commit 7979f5b3d4
26 changed files with 176 additions and 161 deletions

View file

@ -7,15 +7,16 @@
POLYBAR_NS
namespace modules {
struct brightness_handle {
void filepath(const string& path);
float read() const;
private:
string m_path;
};
class backlight_module : public inotify_module<backlight_module> {
public:
struct brightness_handle {
void filepath(const string& path);
float read() const;
private:
string m_path;
};
public:
explicit backlight_module(const bar_settings&, string);

View file

@ -7,23 +7,24 @@
POLYBAR_NS
namespace modules {
enum class battery_state {
NONE = 0,
CHARGING,
DISCHARGING,
FULL,
};
enum class battery_value {
NONE = 0,
ADAPTER,
CAPACITY,
CAPACITY_MAX,
VOLTAGE,
RATE,
};
class battery_module : public inotify_module<battery_module> {
public:
enum class state {
NONE = 0,
CHARGING,
DISCHARGING,
FULL,
};
enum class value {
NONE = 0,
ADAPTER,
CAPACITY,
CAPACITY_MAX,
VOLTAGE,
RATE,
};
public:
explicit battery_module(const bar_settings&, string);
@ -36,7 +37,7 @@ namespace modules {
protected:
int current_percentage();
battery_state current_state();
battery_module::state current_state();
string current_time();
void subthread();
@ -61,8 +62,8 @@ namespace modules {
label_t m_label_discharging;
label_t m_label_full;
battery_state m_state{battery_state::DISCHARGING};
map<battery_value, string> m_valuepath;
battery_module::state m_state{battery_module::state::DISCHARGING};
map<value, string> m_valuepath;
std::atomic<int> m_percentage{0};
int m_fullat{100};
chrono::duration<double> m_interval{};

View file

@ -1,12 +1,13 @@
#pragma once
#include "modules/meta/event_module.hpp"
#include "modules/meta/input_handler.hpp"
#include "utils/bspwm.hpp"
POLYBAR_NS
namespace modules {
class bspwm_module : public event_module<bspwm_module> {
class bspwm_module : public event_module<bspwm_module>, public input_handler {
public:
enum class state {
NONE = 0U,
@ -44,10 +45,9 @@ namespace modules {
bool update();
string get_output();
bool build(builder* builder, const string& tag) const;
bool handle_event(string cmd);
bool receive_events() const {
return true;
}
protected:
bool on(const input_event_t& evt);
private:
static constexpr auto DEFAULT_ICON = "ws-icon-default";

View file

@ -1,20 +1,20 @@
#pragma once
#include "modules/meta/input_handler.hpp"
#include "modules/meta/timer_module.hpp"
POLYBAR_NS
namespace modules {
class date_module : public timer_module<date_module> {
class date_module : public timer_module<date_module>, public input_handler {
public:
explicit date_module(const bar_settings&, string);
bool update();
bool build(builder* builder, const string& tag) const;
bool handle_event(string cmd);
bool receive_events() const {
return true;
}
protected:
bool on(const input_event_t& evt);
private:
static constexpr auto TAG_LABEL = "<label>";

View file

@ -5,13 +5,14 @@
#include "components/config.hpp"
#include "config.hpp"
#include "modules/meta/event_module.hpp"
#include "modules/meta/input_handler.hpp"
#include "utils/i3.hpp"
#include "utils/io.hpp"
POLYBAR_NS
namespace modules {
class i3_module : public event_module<i3_module> {
class i3_module : public event_module<i3_module>, public input_handler {
public:
enum class state {
NONE,
@ -39,10 +40,9 @@ namespace modules {
bool has_event();
bool update();
bool build(builder* builder, const string& tag) const;
bool handle_event(string cmd);
bool receive_events() const {
return true;
}
protected:
bool on(const input_event_t& evt);
private:
static constexpr const char* DEFAULT_TAGS{"<label-state> <label-mode>"};

View file

@ -1,11 +1,12 @@
#pragma once
#include "modules/meta/input_handler.hpp"
#include "modules/meta/static_module.hpp"
POLYBAR_NS
namespace modules {
class menu_module : public static_module<menu_module> {
class menu_module : public static_module<menu_module>, public input_handler {
public:
struct menu_tree_item {
string exec;
@ -20,8 +21,9 @@ namespace modules {
explicit menu_module(const bar_settings&, string);
bool build(builder* builder, const string& tag) const;
bool handle_event(string cmd);
bool receive_events() const;
protected:
bool on(const input_event_t& evt);
private:
static constexpr auto TAG_LABEL_TOGGLE = "<label-toggle>";

View file

@ -109,9 +109,6 @@ namespace modules {
virtual void halt(string error_message) = 0;
virtual string contents() = 0;
virtual bool handle_event(string cmd) = 0;
virtual bool receive_events() const = 0;
virtual void set_update_cb(callback<>&& cb) = 0;
virtual void set_stop_cb(callback<>&& cb) = 0;
};
@ -134,8 +131,6 @@ namespace modules {
void halt(string error_message);
void teardown();
string contents();
bool handle_event(string cmd);
bool receive_events() const;
protected:
void broadcast();

View file

@ -87,16 +87,6 @@ namespace modules {
return m_cache;
}
template <typename Impl>
bool module<Impl>::handle_event(string cmd) {
return CAST_MOD(Impl)->handle_event(cmd);
}
template <typename Impl>
bool module<Impl>::receive_events() const {
return false;
}
// }}}
// module<Impl> protected {{{
@ -111,7 +101,7 @@ namespace modules {
if (m_update_callback)
m_update_callback();
else
m_log.warn("%s: No handler, ignoring broadcast...", name());
m_log.info("%s: No handler, ignoring broadcast...", name());
}
template <typename Impl>

View file

@ -0,0 +1,17 @@
#pragma once
#include "common.hpp"
#include "events/signal.hpp"
#include "events/signal_receiver.hpp"
POLYBAR_NS
namespace modules {
using input_event_t = signals::eventqueue::process_input;
class input_handler : public signal_receiver<0, input_event_t> {
public:
virtual ~input_handler() {}
};
}
POLYBAR_NS_END

View file

@ -4,6 +4,7 @@
#include "adapters/mpd.hpp"
#include "modules/meta/event_module.hpp"
#include "modules/meta/input_handler.hpp"
POLYBAR_NS
@ -12,7 +13,7 @@ using namespace mpd;
namespace chrono = std::chrono;
namespace modules {
class mpd_module : public event_module<mpd_module> {
class mpd_module : public event_module<mpd_module>, public input_handler {
public:
explicit mpd_module(const bar_settings&, string);
@ -24,8 +25,9 @@ namespace modules {
string get_format() const;
string get_output();
bool build(builder* builder, const string& tag) const;
bool handle_event(string cmd);
bool receive_events() const;
protected:
bool on(const input_event_t& evt);
private:
static constexpr auto FORMAT_ONLINE = "format-online";

View file

@ -2,6 +2,7 @@
#include "config.hpp"
#include "modules/meta/event_module.hpp"
#include "modules/meta/input_handler.hpp"
POLYBAR_NS
@ -18,7 +19,7 @@ namespace modules {
using mixer_t = shared_ptr<alsa::mixer>;
using control_t = shared_ptr<alsa::control>;
class volume_module : public event_module<volume_module> {
class volume_module : public event_module<volume_module>, public input_handler {
public:
explicit volume_module(const bar_settings&, string);
@ -28,8 +29,9 @@ namespace modules {
string get_format() const;
string get_output();
bool build(builder* builder, const string& tag) const;
bool handle_event(string cmd);
bool receive_events() const;
protected:
bool on(const input_event_t& evt);
private:
static constexpr auto FORMAT_VOLUME = "format-volume";

View file

@ -2,6 +2,7 @@
#include "components/config.hpp"
#include "config.hpp"
#include "modules/meta/input_handler.hpp"
#include "modules/meta/static_module.hpp"
#include "x11/randr.hpp"
@ -23,7 +24,9 @@ namespace modules {
*
* TODO: Implement backlight configuring using scroll events
*/
class xbacklight_module : public static_module<xbacklight_module>, public xpp::event::sink<evt::randr_notify> {
class xbacklight_module : public static_module<xbacklight_module>,
public xpp::event::sink<evt::randr_notify>,
public input_handler {
public:
explicit xbacklight_module(const bar_settings& bar, string name_);
@ -32,10 +35,9 @@ namespace modules {
void update();
string get_output();
bool build(builder* builder, const string& tag) const;
bool handle_event(string cmd);
bool receive_events() const {
return true;
}
protected:
bool on(const input_event_t& evt);
private:
static constexpr const char* TAG_LABEL{"<label>"};

View file

@ -3,6 +3,7 @@
#include "common.hpp"
#include "components/config.hpp"
#include "components/types.hpp"
#include "modules/meta/input_handler.hpp"
#include "modules/meta/static_module.hpp"
#include "x11/events.hpp"
#include "x11/window.hpp"
@ -16,18 +17,16 @@ namespace modules {
/**
* Keyboard module using the X keyboard extension
*/
class xkeyboard_module : public static_module<xkeyboard_module>,
public xpp::event::sink<evt::xkb_new_keyboard_notify, evt::xkb_state_notify, evt::xkb_indicator_state_notify> {
class xkeyboard_module
: public static_module<xkeyboard_module>,
public xpp::event::sink<evt::xkb_new_keyboard_notify, evt::xkb_state_notify, evt::xkb_indicator_state_notify>,
public input_handler {
public:
explicit xkeyboard_module(const bar_settings& bar, string name_);
void teardown();
void update();
bool build(builder* builder, const string& tag) const;
bool handle_event(string cmd);
bool receive_events() const {
return true;
}
protected:
bool query_keyboard();
@ -37,6 +36,8 @@ namespace modules {
void handle(const evt::xkb_state_notify& evt);
void handle(const evt::xkb_indicator_state_notify& evt);
bool on(const input_event_t&);
private:
static constexpr const char* TAG_LABEL_LAYOUT{"<label-layout>"};
static constexpr const char* TAG_LABEL_INDICATOR{"<label-indicator>"};

View file

@ -4,6 +4,7 @@
#include "components/config.hpp"
#include "components/types.hpp"
#include "modules/meta/input_handler.hpp"
#include "modules/meta/static_module.hpp"
#include "x11/events.hpp"
#include "x11/ewmh.hpp"
@ -47,7 +48,9 @@ namespace modules {
/**
* Module used to display EWMH desktops
*/
class xworkspaces_module : public static_module<xworkspaces_module>, public xpp::event::sink<evt::property_notify> {
class xworkspaces_module : public static_module<xworkspaces_module>,
public xpp::event::sink<evt::property_notify>,
public input_handler {
public:
explicit xworkspaces_module(const bar_settings& bar, string name_);
@ -56,14 +59,11 @@ namespace modules {
void update();
string get_output();
bool build(builder* builder, const string& tag) const;
bool handle_event(string cmd);
bool receive_events() const {
return true;
}
protected:
void rebuild_desktops();
void set_current_desktop();
bool on(const input_event_t&);
private:
static constexpr const char* DEFAULT_ICON{"icon-default"};

View file

@ -104,6 +104,11 @@ controller::controller(connection& conn, signal_emitter& emitter, const logger&
module->set_update_cb([&] { enqueue(make_update_evt(false)); });
module->set_stop_cb([&] { enqueue(make_check_evt()); });
input_handler* module_input_handler{nullptr};
if ((module_input_handler = dynamic_cast<input_handler*>(module)) != nullptr) {
m_sig.attach(module_input_handler);
}
m_modules[align].emplace_back(move(module));
created_modules++;
@ -440,19 +445,11 @@ bool controller::on(const sig_ev::process_update& evt) {
* Process eventqueue input event
*/
bool controller::on(const sig_ev::process_input& evt) {
string input{*evt()};
m_log.warn("Uncaught input event, forwarding to shell... (input: %s)", input);
try {
string input{*evt()};
for (auto&& block : m_modules) {
for (auto&& module : block.second) {
if (module->receive_events() && module->handle_event(input)) {
return true;
}
}
}
m_log.warn("Input event \"%s\" was rejected by all modules, passing to shell...", input);
if (m_command) {
m_log.warn("Terminating previous shell command");
m_command->terminate();

View file

@ -14,6 +14,17 @@ namespace modules {
template class module<backlight_module>;
template class inotify_module<backlight_module>;
void backlight_module::brightness_handle::filepath(const string& path) {
if (!file_util::exists(path)) {
throw module_error("The file '" + path + "' does not exist");
}
m_path = path;
}
float backlight_module::brightness_handle::read() const {
return std::strtof(file_util::get_contents(m_path).c_str(), nullptr);
}
backlight_module::backlight_module(const bar_settings& bar, string name_)
: inotify_module<backlight_module>(bar, move(name_)) {
auto card = m_conf.get<string>(name(), "card");
@ -39,17 +50,6 @@ namespace modules {
watch(string_util::replace(PATH_BACKLIGHT_VAL, "%card%", card));
}
void brightness_handle::filepath(const string& path) {
if (!file_util::exists(path)) {
throw module_error("The file '" + path + "' does not exist");
}
m_path = path;
}
float brightness_handle::read() const {
return std::strtof(file_util::get_contents(m_path).c_str(), nullptr);
}
void backlight_module::idle() {
sleep(75ms);
}

View file

@ -31,36 +31,36 @@ namespace modules {
if (!file_util::exists(path_adapter + "online")) {
throw module_error("The file '" + path_adapter + "online' does not exist");
}
m_valuepath[battery_value::ADAPTER] = path_adapter + "online";
m_valuepath[battery_module::value::ADAPTER] = path_adapter + "online";
if (!file_util::exists(path_battery + "voltage_now")) {
throw module_error("The file '" + path_battery + "voltage_now' does not exist");
}
m_valuepath[battery_value::VOLTAGE] = path_battery + "voltage_now";
m_valuepath[battery_module::value::VOLTAGE] = path_battery + "voltage_now";
for (auto&& file : vector<string>{"charge", "energy"}) {
if (file_util::exists(path_battery + file + "_now")) {
m_valuepath[battery_value::CAPACITY] = path_battery + file + "_now";
m_valuepath[battery_module::value::CAPACITY] = path_battery + file + "_now";
}
if (file_util::exists(path_battery + file + "_full")) {
m_valuepath[battery_value::CAPACITY_MAX] = path_battery + file + "_full";
m_valuepath[battery_module::value::CAPACITY_MAX] = path_battery + file + "_full";
}
}
if (m_valuepath[battery_value::CAPACITY].empty()) {
if (m_valuepath[battery_module::value::CAPACITY].empty()) {
throw module_error("The file '" + path_battery + "[charge|energy]_now' does not exist");
}
if (m_valuepath[battery_value::CAPACITY_MAX].empty()) {
if (m_valuepath[battery_module::value::CAPACITY_MAX].empty()) {
throw module_error("The file '" + path_battery + "[charge|energy]_full' does not exist");
}
for (auto&& file : vector<string>{"current", "power"}) {
if (file_util::exists(path_battery + file + "_now")) {
m_valuepath[battery_value::RATE] = path_battery + file + "_now";
m_valuepath[battery_module::value::RATE] = path_battery + file + "_now";
}
}
if (m_valuepath[battery_value::RATE].empty()) {
if (m_valuepath[battery_module::value::RATE].empty()) {
throw module_error("The file '" + path_battery + "[current|power]_now' does not exist");
}
@ -99,8 +99,8 @@ namespace modules {
}
// Create inotify watches
watch(m_valuepath[battery_value::CAPACITY], IN_ACCESS);
watch(m_valuepath[battery_value::ADAPTER], IN_ACCESS);
watch(m_valuepath[battery_module::value::CAPACITY], IN_ACCESS);
watch(m_valuepath[battery_module::value::ADAPTER], IN_ACCESS);
// Setup time if token is used
if (m_label_charging->has_token("%time%") || m_label_discharging->has_token("%time%")) {
@ -143,7 +143,7 @@ namespace modules {
if (chrono::duration_cast<decltype(m_interval)>(now - m_lastpoll) > m_interval) {
m_lastpoll = now;
m_log.info("%s: Polling values (inotify fallback)", name());
file_util::get_contents(m_valuepath[battery_value::CAPACITY]);
file_util::get_contents(m_valuepath[battery_module::value::CAPACITY]);
}
}
@ -164,7 +164,7 @@ namespace modules {
auto state = current_state();
int percentage = m_percentage;
if (state != battery_state::FULL) {
if (state != battery_module::state::FULL) {
percentage = current_percentage();
}
@ -178,21 +178,21 @@ namespace modules {
string time_remaining;
if (m_state == battery_state::CHARGING && m_label_charging) {
if (m_state == battery_module::state::CHARGING && m_label_charging) {
if (!m_timeformat.empty()) {
time_remaining = current_time();
}
m_label_charging->reset_tokens();
m_label_charging->replace_token("%percentage%", to_string(m_percentage) + "%");
m_label_charging->replace_token("%time%", time_remaining);
} else if (m_state == battery_state::DISCHARGING && m_label_discharging) {
} else if (m_state == battery_module::state::DISCHARGING && m_label_discharging) {
if (!m_timeformat.empty()) {
time_remaining = current_time();
}
m_label_discharging->reset_tokens();
m_label_discharging->replace_token("%percentage%", to_string(m_percentage) + "%");
m_label_discharging->replace_token("%time%", time_remaining);
} else if (m_state == battery_state::FULL && m_label_full) {
} else if (m_state == battery_module::state::FULL && m_label_full) {
m_label_full->reset_tokens();
m_label_full->replace_token("%percentage%", to_string(m_percentage) + "%");
}
@ -204,9 +204,9 @@ namespace modules {
* Get the output format based on state
*/
string battery_module::get_format() const {
if (m_state == battery_state::FULL) {
if (m_state == battery_module::state::FULL) {
return FORMAT_FULL;
} else if (m_state == battery_state::CHARGING) {
} else if (m_state == battery_module::state::CHARGING) {
return FORMAT_CHARGING;
} else {
return FORMAT_DISCHARGING;
@ -238,19 +238,19 @@ namespace modules {
/**
* Get the current battery state
*/
battery_state battery_module::current_state() {
auto adapter_status = file_util::get_contents(m_valuepath[battery_value::ADAPTER]);
battery_module::state battery_module::current_state() {
auto adapter_status = file_util::get_contents(m_valuepath[battery_module::value::ADAPTER]);
if (adapter_status.empty()) {
return battery_state::DISCHARGING;
return battery_module::state::DISCHARGING;
} else if (adapter_status[0] == '0') {
return battery_state::DISCHARGING;
return battery_module::state::DISCHARGING;
} else if (adapter_status[0] != '1') {
return battery_state::DISCHARGING;
return battery_module::state::DISCHARGING;
} else if (m_percentage < m_fullat) {
return battery_state::CHARGING;
return battery_module::state::CHARGING;
} else {
return battery_state::FULL;
return battery_module::state::FULL;
}
}
@ -259,9 +259,9 @@ namespace modules {
*/
int battery_module::current_percentage() {
auto capacity_now =
std::strtoul(file_util::get_contents(m_valuepath[battery_value::CAPACITY]).c_str(), nullptr, 10);
std::strtoul(file_util::get_contents(m_valuepath[battery_module::value::CAPACITY]).c_str(), nullptr, 10);
auto capacity_max =
std::strtoul(file_util::get_contents(m_valuepath[battery_value::CAPACITY_MAX]).c_str(), nullptr, 10);
std::strtoul(file_util::get_contents(m_valuepath[battery_module::value::CAPACITY_MAX]).c_str(), nullptr, 10);
auto percentage = math_util::percentage(capacity_now, 0UL, capacity_max);
return percentage < m_fullat ? percentage : 100;
@ -271,19 +271,19 @@ namespace modules {
* Get estimate of remaining time until fully dis-/charged
*/
string battery_module::current_time() {
if (m_state == battery_state::FULL) {
if (m_state == battery_module::state::FULL) {
return "";
}
int rate{atoi(file_util::get_contents(m_valuepath[battery_value::RATE]).c_str()) / 1000};
int volt{atoi(file_util::get_contents(m_valuepath[battery_value::VOLTAGE]).c_str()) / 1000};
int now{atoi(file_util::get_contents(m_valuepath[battery_value::CAPACITY]).c_str()) / 1000};
int max{atoi(file_util::get_contents(m_valuepath[battery_value::CAPACITY_MAX]).c_str()) / 1000};
int rate{atoi(file_util::get_contents(m_valuepath[battery_module::value::RATE]).c_str()) / 1000};
int volt{atoi(file_util::get_contents(m_valuepath[battery_module::value::VOLTAGE]).c_str()) / 1000};
int now{atoi(file_util::get_contents(m_valuepath[battery_module::value::CAPACITY]).c_str()) / 1000};
int max{atoi(file_util::get_contents(m_valuepath[battery_module::value::CAPACITY_MAX]).c_str()) / 1000};
int cap{0};
if (m_state == battery_state::CHARGING) {
if (m_state == battery_module::state::CHARGING) {
cap = max - now;
} else if (m_state == battery_state::DISCHARGING) {
} else if (m_state == battery_module::state::DISCHARGING) {
cap = now;
}
@ -332,7 +332,7 @@ namespace modules {
while (running()) {
for (int i = 0; running() && i < dur.count(); ++i) {
if (m_state == battery_state::CHARGING) {
if (m_state == battery_module::state::CHARGING) {
broadcast();
}

View file

@ -414,7 +414,9 @@ namespace modules {
return false;
}
bool bspwm_module::handle_event(string cmd) {
bool bspwm_module::on(const input_event_t& evt) {
string cmd{*evt.data()};
if (cmd.find(EVENT_PREFIX) != 0) {
return false;
}

View file

@ -86,7 +86,9 @@ namespace modules {
return true;
}
bool date_module::handle_event(string cmd) {
bool date_module::on(const input_event_t& evt) {
string cmd{*evt.data()};
if (cmd != EVENT_TOGGLE) {
return false;
}

View file

@ -193,7 +193,9 @@ namespace modules {
return true;
}
bool i3_module::handle_event(string cmd) {
bool i3_module::on(const input_event_t& evt) {
string cmd{*evt.data()};
if (cmd.find(EVENT_PREFIX) != 0) {
return false;
}

View file

@ -82,7 +82,9 @@ namespace modules {
return true;
}
bool menu_module::handle_event(string cmd) {
bool menu_module::on(const input_event_t& evt) {
string cmd{*evt.data()};
if (cmd.compare(0, 4, "menu") != 0) {
return false;
}
@ -123,10 +125,6 @@ namespace modules {
return true;
}
bool menu_module::receive_events() const {
return true;
}
}
POLYBAR_NS_END

View file

@ -318,7 +318,9 @@ namespace modules {
return true;
}
bool mpd_module::handle_event(string cmd) {
bool mpd_module::on(const input_event_t& evt) {
string cmd{*evt.data()};
if (cmd.compare(0, 3, "mpd") != 0) {
return false;
}
@ -369,10 +371,6 @@ namespace modules {
return true;
}
bool mpd_module::receive_events() const {
return true;
}
}
POLYBAR_NS_END

View file

@ -211,7 +211,9 @@ namespace modules {
return true;
}
bool volume_module::handle_event(string cmd) {
bool volume_module::on(const input_event_t& evt) {
string cmd{*evt.data()};
if (cmd.compare(0, 3, EVENT_PREFIX) != 0) {
return false;
}
@ -263,10 +265,6 @@ namespace modules {
return true;
}
bool volume_module::receive_events() const {
return true;
}
}
POLYBAR_NS_END

View file

@ -163,7 +163,9 @@ namespace modules {
/**
* Process scroll events by changing backlight value
*/
bool xbacklight_module::handle_event(string cmd) {
bool xbacklight_module::on(const input_event_t& evt) {
string cmd{*evt.data()};
int value_mod = 0;
if (cmd == EVENT_SCROLLUP) {

View file

@ -118,7 +118,9 @@ namespace modules {
/**
* Handle input command
*/
bool xkeyboard_module::handle_event(string cmd) {
bool xkeyboard_module::on(const input_event_t& evt) {
string cmd{*evt.data()};
if (cmd.compare(0, strlen(EVENT_SWITCH), EVENT_SWITCH) != 0) {
return false;
}

View file

@ -222,7 +222,8 @@ namespace modules {
return false;
}
bool xworkspaces_module::handle_event(string cmd) {
bool xworkspaces_module::on(const input_event_t& evt) {
string cmd{*evt.data()};
if (cmd.find(EVENT_PREFIX) != 0) {
return false;
}