refactor(event_handler): Abstract sink attaching

This commit is contained in:
Michael Carlberg 2016-12-23 05:08:19 +01:00
parent b765094a29
commit 8cff01e3d8
10 changed files with 54 additions and 52 deletions

View File

@ -0,0 +1,33 @@
#pragma once
#include "common.hpp"
#include "utils/concurrency.hpp"
#include "x11/connection.hpp"
POLYBAR_NS
class connection;
namespace modules {
struct event_handler_interface {
virtual ~event_handler_interface() {}
virtual void connect(connection&) {}
virtual void disconnect(connection&) {}
};
template <typename Event, typename... Events>
class event_handler : public event_handler_interface, public xpp::event::sink<Event, Events...> {
public:
virtual ~event_handler() {}
virtual void connect(connection& conn) override {
conn.attach_sink(this, SINK_PRIORITY_MODULE);
}
virtual void disconnect(connection& conn) override {
conn.detach_sink(this, SINK_PRIORITY_MODULE);
}
};
}
POLYBAR_NS_END

View File

@ -2,6 +2,7 @@
#include "components/config.hpp" #include "components/config.hpp"
#include "config.hpp" #include "config.hpp"
#include "modules/meta/event_handler.hpp"
#include "modules/meta/input_handler.hpp" #include "modules/meta/input_handler.hpp"
#include "modules/meta/static_module.hpp" #include "modules/meta/static_module.hpp"
#include "x11/extensions/randr.hpp" #include "x11/extensions/randr.hpp"
@ -25,18 +26,17 @@ namespace modules {
* TODO: Implement backlight configuring using scroll events * TODO: Implement backlight configuring using scroll events
*/ */
class xbacklight_module : public static_module<xbacklight_module>, class xbacklight_module : public static_module<xbacklight_module>,
public xpp::event::sink<evt::randr_notify>, public event_handler<evt::randr_notify>,
public input_handler { public input_handler {
public: public:
explicit xbacklight_module(const bar_settings& bar, string name_); explicit xbacklight_module(const bar_settings& bar, string name_);
void teardown();
void handle(const evt::randr_notify& evt);
void update(); void update();
string get_output(); string get_output();
bool build(builder* builder, const string& tag) const; bool build(builder* builder, const string& tag) const;
protected: protected:
void handle(const evt::randr_notify& evt);
bool on(const input_event_t& evt); bool on(const input_event_t& evt);
private: private:

View File

@ -3,11 +3,12 @@
#include "common.hpp" #include "common.hpp"
#include "components/config.hpp" #include "components/config.hpp"
#include "components/types.hpp" #include "components/types.hpp"
#include "modules/meta/event_handler.hpp"
#include "modules/meta/input_handler.hpp" #include "modules/meta/input_handler.hpp"
#include "modules/meta/static_module.hpp" #include "modules/meta/static_module.hpp"
#include "x11/events.hpp" #include "x11/events.hpp"
#include "x11/window.hpp"
#include "x11/extensions/xkb.hpp" #include "x11/extensions/xkb.hpp"
#include "x11/window.hpp"
POLYBAR_NS POLYBAR_NS
@ -19,12 +20,11 @@ namespace modules {
*/ */
class xkeyboard_module class xkeyboard_module
: public static_module<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 event_handler<evt::xkb_new_keyboard_notify, evt::xkb_state_notify, evt::xkb_indicator_state_notify>,
public input_handler { public input_handler {
public: public:
explicit xkeyboard_module(const bar_settings& bar, string name_); explicit xkeyboard_module(const bar_settings& bar, string name_);
void teardown();
void update(); void update();
bool build(builder* builder, const string& tag) const; bool build(builder* builder, const string& tag) const;

View File

@ -1,5 +1,6 @@
#pragma once #pragma once
#include "modules/meta/event_handler.hpp"
#include "modules/meta/static_module.hpp" #include "modules/meta/static_module.hpp"
#include "x11/events.hpp" #include "x11/events.hpp"
#include "x11/ewmh.hpp" #include "x11/ewmh.hpp"
@ -28,11 +29,10 @@ namespace modules {
* Module used to display information about the * Module used to display information about the
* currently active X window. * currently active X window.
*/ */
class xwindow_module : public static_module<xwindow_module>, public xpp::event::sink<evt::property_notify> { class xwindow_module : public static_module<xwindow_module>, public event_handler<evt::property_notify> {
public: public:
explicit xwindow_module(const bar_settings&, string); explicit xwindow_module(const bar_settings&, string);
void teardown();
void update(bool force = false); void update(bool force = false);
bool build(builder* builder, const string& tag) const; bool build(builder* builder, const string& tag) const;

View File

@ -4,6 +4,7 @@
#include "components/config.hpp" #include "components/config.hpp"
#include "components/types.hpp" #include "components/types.hpp"
#include "modules/meta/event_handler.hpp"
#include "modules/meta/input_handler.hpp" #include "modules/meta/input_handler.hpp"
#include "modules/meta/static_module.hpp" #include "modules/meta/static_module.hpp"
#include "x11/events.hpp" #include "x11/events.hpp"
@ -49,18 +50,17 @@ namespace modules {
* Module used to display EWMH desktops * Module used to display EWMH desktops
*/ */
class xworkspaces_module : public static_module<xworkspaces_module>, class xworkspaces_module : public static_module<xworkspaces_module>,
public xpp::event::sink<evt::property_notify>, public event_handler<evt::property_notify>,
public input_handler { public input_handler {
public: public:
explicit xworkspaces_module(const bar_settings& bar, string name_); explicit xworkspaces_module(const bar_settings& bar, string name_);
void teardown();
void handle(const evt::property_notify& evt);
void update(); void update();
string get_output(); string get_output();
bool build(builder* builder, const string& tag) const; bool build(builder* builder, const string& tag) const;
protected: protected:
void handle(const evt::property_notify& evt);
void rebuild_desktops(); void rebuild_desktops();
void set_current_desktop(); void set_current_desktop();
bool on(const input_event_t&); bool on(const input_event_t&);

View File

@ -9,6 +9,7 @@
#include "components/types.hpp" #include "components/types.hpp"
#include "events/signal.hpp" #include "events/signal.hpp"
#include "events/signal_emitter.hpp" #include "events/signal_emitter.hpp"
#include "modules/meta/event_handler.hpp"
#include "modules/meta/factory.hpp" #include "modules/meta/factory.hpp"
#include "utils/command.hpp" #include "utils/command.hpp"
#include "utils/factory.hpp" #include "utils/factory.hpp"
@ -155,9 +156,15 @@ bool controller::run(bool writeback) {
size_t started_modules{0}; size_t started_modules{0};
for (const auto& block : m_modules) { for (const auto& block : m_modules) {
for (const auto& module : block.second) { for (const auto& module : block.second) {
auto handler = dynamic_cast<input_handler*>(&*module); auto inp_handler = dynamic_cast<input_handler*>(&*module);
if (handler != nullptr) { auto evt_handler = dynamic_cast<event_handler_interface*>(&*module);
m_inputhandlers.emplace_back(handler);
if (inp_handler != nullptr) {
m_inputhandlers.emplace_back(inp_handler);
}
if (evt_handler != nullptr) {
evt_handler->connect(m_connection);
} }
try { try {

View File

@ -54,7 +54,6 @@ namespace modules {
// Connect with the event registry and make sure we get // Connect with the event registry and make sure we get
// notified when a RandR output property gets modified // notified when a RandR output property gets modified
m_connection.attach_sink(this, SINK_PRIORITY_MODULE);
m_connection.select_input_checked(m_proxy, XCB_RANDR_NOTIFY_MASK_OUTPUT_PROPERTY); m_connection.select_input_checked(m_proxy, XCB_RANDR_NOTIFY_MASK_OUTPUT_PROPERTY);
// Add formats and elements // Add formats and elements
@ -74,13 +73,6 @@ namespace modules {
update(); update();
} }
/**
* Disconnect from the event registry
*/
void xbacklight_module::teardown() {
m_connection.detach_sink(this, SINK_PRIORITY_MODULE);
}
/** /**
* Handler for XCB_RANDR_NOTIFY events * Handler for XCB_RANDR_NOTIFY events
*/ */

View File

@ -30,9 +30,6 @@ namespace modules {
m_indicator = load_optional_label(m_conf, name(), TAG_LABEL_INDICATOR, "%name%"); m_indicator = load_optional_label(m_conf, name(), TAG_LABEL_INDICATOR, "%name%");
} }
// Connect to the event registry
m_connection.attach_sink(this, SINK_PRIORITY_MODULE);
// Setup extension // Setup extension
// clang-format off // clang-format off
m_connection.xkb().select_events_checked(XCB_XKB_ID_USE_CORE_KBD, m_connection.xkb().select_events_checked(XCB_XKB_ID_USE_CORE_KBD,
@ -46,13 +43,6 @@ namespace modules {
update(); update();
} }
/**
* Disconnect from the event registry
*/
void xkeyboard_module::teardown() {
m_connection.detach_sink(this, SINK_PRIORITY_MODULE);
}
/** /**
* Update labels with extension data * Update labels with extension data
*/ */

View File

@ -81,20 +81,10 @@ namespace modules {
m_label = load_optional_label(m_conf, name(), TAG_LABEL, "%title%"); m_label = load_optional_label(m_conf, name(), TAG_LABEL, "%title%");
} }
// Connect with the event registry
m_connection.attach_sink(this, SINK_PRIORITY_MODULE);
// Trigger the initial draw event // Trigger the initial draw event
update(); update();
} }
/**
* Disconnect from the event registry
*/
void xwindow_module::teardown() {
m_connection.detach_sink(this, SINK_PRIORITY_MODULE);
}
/** /**
* Handler for XCB_PROPERTY_NOTIFY events * Handler for XCB_PROPERTY_NOTIFY events
*/ */

View File

@ -79,16 +79,6 @@ namespace modules {
// Make sure we get notified when root properties change // Make sure we get notified when root properties change
window{m_connection, m_connection.root()}.ensure_event_mask(XCB_EVENT_MASK_PROPERTY_CHANGE); window{m_connection, m_connection.root()}.ensure_event_mask(XCB_EVENT_MASK_PROPERTY_CHANGE);
// Connect with the event registry
m_connection.attach_sink(this, SINK_PRIORITY_MODULE);
}
/**
* Disconnect from the event registry
*/
void xworkspaces_module::teardown() {
m_connection.detach_sink(this, SINK_PRIORITY_MODULE);
} }
/** /**