From 8cff01e3d8f940be04dd77e1fd759efa8c39ff27 Mon Sep 17 00:00:00 2001 From: Michael Carlberg Date: Fri, 23 Dec 2016 05:08:19 +0100 Subject: [PATCH] refactor(event_handler): Abstract sink attaching --- include/modules/meta/event_handler.hpp | 33 ++++++++++++++++++++++++++ include/modules/xbacklight.hpp | 6 ++--- include/modules/xkeyboard.hpp | 6 ++--- include/modules/xwindow.hpp | 4 ++-- include/modules/xworkspaces.hpp | 6 ++--- src/components/controller.cpp | 13 +++++++--- src/modules/xbacklight.cpp | 8 ------- src/modules/xkeyboard.cpp | 10 -------- src/modules/xwindow.cpp | 10 -------- src/modules/xworkspaces.cpp | 10 -------- 10 files changed, 54 insertions(+), 52 deletions(-) create mode 100644 include/modules/meta/event_handler.hpp diff --git a/include/modules/meta/event_handler.hpp b/include/modules/meta/event_handler.hpp new file mode 100644 index 00000000..c1c786a0 --- /dev/null +++ b/include/modules/meta/event_handler.hpp @@ -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 + class event_handler : public event_handler_interface, public xpp::event::sink { + 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 diff --git a/include/modules/xbacklight.hpp b/include/modules/xbacklight.hpp index 05c75789..8ee7766a 100644 --- a/include/modules/xbacklight.hpp +++ b/include/modules/xbacklight.hpp @@ -2,6 +2,7 @@ #include "components/config.hpp" #include "config.hpp" +#include "modules/meta/event_handler.hpp" #include "modules/meta/input_handler.hpp" #include "modules/meta/static_module.hpp" #include "x11/extensions/randr.hpp" @@ -25,18 +26,17 @@ namespace modules { * TODO: Implement backlight configuring using scroll events */ class xbacklight_module : public static_module, - public xpp::event::sink, + public event_handler, public input_handler { public: explicit xbacklight_module(const bar_settings& bar, string name_); - void teardown(); - void handle(const evt::randr_notify& evt); void update(); string get_output(); bool build(builder* builder, const string& tag) const; protected: + void handle(const evt::randr_notify& evt); bool on(const input_event_t& evt); private: diff --git a/include/modules/xkeyboard.hpp b/include/modules/xkeyboard.hpp index 839665b7..6ef343dc 100644 --- a/include/modules/xkeyboard.hpp +++ b/include/modules/xkeyboard.hpp @@ -3,11 +3,12 @@ #include "common.hpp" #include "components/config.hpp" #include "components/types.hpp" +#include "modules/meta/event_handler.hpp" #include "modules/meta/input_handler.hpp" #include "modules/meta/static_module.hpp" #include "x11/events.hpp" -#include "x11/window.hpp" #include "x11/extensions/xkb.hpp" +#include "x11/window.hpp" POLYBAR_NS @@ -19,12 +20,11 @@ namespace modules { */ class xkeyboard_module : public static_module, - public xpp::event::sink, + public event_handler, 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; diff --git a/include/modules/xwindow.hpp b/include/modules/xwindow.hpp index 6e25edc6..401e7d1b 100644 --- a/include/modules/xwindow.hpp +++ b/include/modules/xwindow.hpp @@ -1,5 +1,6 @@ #pragma once +#include "modules/meta/event_handler.hpp" #include "modules/meta/static_module.hpp" #include "x11/events.hpp" #include "x11/ewmh.hpp" @@ -28,11 +29,10 @@ namespace modules { * Module used to display information about the * currently active X window. */ - class xwindow_module : public static_module, public xpp::event::sink { + class xwindow_module : public static_module, public event_handler { public: explicit xwindow_module(const bar_settings&, string); - void teardown(); void update(bool force = false); bool build(builder* builder, const string& tag) const; diff --git a/include/modules/xworkspaces.hpp b/include/modules/xworkspaces.hpp index 45f16742..e7df5fa2 100644 --- a/include/modules/xworkspaces.hpp +++ b/include/modules/xworkspaces.hpp @@ -4,6 +4,7 @@ #include "components/config.hpp" #include "components/types.hpp" +#include "modules/meta/event_handler.hpp" #include "modules/meta/input_handler.hpp" #include "modules/meta/static_module.hpp" #include "x11/events.hpp" @@ -49,18 +50,17 @@ namespace modules { * Module used to display EWMH desktops */ class xworkspaces_module : public static_module, - public xpp::event::sink, + public event_handler, public input_handler { public: explicit xworkspaces_module(const bar_settings& bar, string name_); - void teardown(); - void handle(const evt::property_notify& evt); void update(); string get_output(); bool build(builder* builder, const string& tag) const; protected: + void handle(const evt::property_notify& evt); void rebuild_desktops(); void set_current_desktop(); bool on(const input_event_t&); diff --git a/src/components/controller.cpp b/src/components/controller.cpp index 3c9f8796..54c2b0c2 100644 --- a/src/components/controller.cpp +++ b/src/components/controller.cpp @@ -9,6 +9,7 @@ #include "components/types.hpp" #include "events/signal.hpp" #include "events/signal_emitter.hpp" +#include "modules/meta/event_handler.hpp" #include "modules/meta/factory.hpp" #include "utils/command.hpp" #include "utils/factory.hpp" @@ -155,9 +156,15 @@ bool controller::run(bool writeback) { size_t started_modules{0}; for (const auto& block : m_modules) { for (const auto& module : block.second) { - auto handler = dynamic_cast(&*module); - if (handler != nullptr) { - m_inputhandlers.emplace_back(handler); + auto inp_handler = dynamic_cast(&*module); + auto evt_handler = dynamic_cast(&*module); + + if (inp_handler != nullptr) { + m_inputhandlers.emplace_back(inp_handler); + } + + if (evt_handler != nullptr) { + evt_handler->connect(m_connection); } try { diff --git a/src/modules/xbacklight.cpp b/src/modules/xbacklight.cpp index 54b34d8a..6a1887e6 100644 --- a/src/modules/xbacklight.cpp +++ b/src/modules/xbacklight.cpp @@ -54,7 +54,6 @@ namespace modules { // Connect with the event registry and make sure we get // 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); // Add formats and elements @@ -74,13 +73,6 @@ namespace modules { update(); } - /** - * Disconnect from the event registry - */ - void xbacklight_module::teardown() { - m_connection.detach_sink(this, SINK_PRIORITY_MODULE); - } - /** * Handler for XCB_RANDR_NOTIFY events */ diff --git a/src/modules/xkeyboard.cpp b/src/modules/xkeyboard.cpp index 65fbd12b..359af3cc 100644 --- a/src/modules/xkeyboard.cpp +++ b/src/modules/xkeyboard.cpp @@ -30,9 +30,6 @@ namespace modules { 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 // clang-format off m_connection.xkb().select_events_checked(XCB_XKB_ID_USE_CORE_KBD, @@ -46,13 +43,6 @@ namespace modules { update(); } - /** - * Disconnect from the event registry - */ - void xkeyboard_module::teardown() { - m_connection.detach_sink(this, SINK_PRIORITY_MODULE); - } - /** * Update labels with extension data */ diff --git a/src/modules/xwindow.cpp b/src/modules/xwindow.cpp index f6d9df17..be4c56a4 100644 --- a/src/modules/xwindow.cpp +++ b/src/modules/xwindow.cpp @@ -81,20 +81,10 @@ namespace modules { 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 update(); } - /** - * Disconnect from the event registry - */ - void xwindow_module::teardown() { - m_connection.detach_sink(this, SINK_PRIORITY_MODULE); - } - /** * Handler for XCB_PROPERTY_NOTIFY events */ diff --git a/src/modules/xworkspaces.cpp b/src/modules/xworkspaces.cpp index 88da4eb2..ea2dd19a 100644 --- a/src/modules/xworkspaces.cpp +++ b/src/modules/xworkspaces.cpp @@ -79,16 +79,6 @@ namespace modules { // Make sure we get notified when root properties 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); } /**