From 789800a68f68b985240b129fa50b8a51906377d6 Mon Sep 17 00:00:00 2001 From: Michael Carlberg Date: Fri, 23 Dec 2016 22:19:42 +0100 Subject: [PATCH] refactor(signals): Simple types --- include/components/controller.hpp | 13 ++++--- include/events/signal.hpp | 9 +++-- include/events/signal_fwd.hpp | 9 +++-- include/events/types.hpp | 10 +++++ include/modules/meta/base.inl | 4 +- src/components/controller.cpp | 64 +++++++++++++++++++------------ src/components/screen.cpp | 2 +- src/x11/tray_manager.cpp | 4 +- src/x11/xutils.cpp | 7 +++- 9 files changed, 76 insertions(+), 46 deletions(-) diff --git a/include/components/controller.hpp b/include/components/controller.hpp index d3e63cd0..10bbe5a1 100644 --- a/include/components/controller.hpp +++ b/include/components/controller.hpp @@ -42,8 +42,8 @@ namespace sig_ev = signals::eventqueue; namespace sig_ui = signals::ui; namespace sig_ipc = signals::ipc; -class controller : public signal_receiver { public: using make_type = unique_ptr; @@ -63,10 +63,11 @@ class controller : public signal_receiver +#include "common.hpp" + +POLYBAR_NS + enum class event_type : uint8_t { NONE = 0, UPDATE, @@ -16,6 +20,10 @@ struct event { }; namespace { + inline bool operator==(uint8_t id, event_type type) { + return id == static_cast(type); + } + /** * Create QUIT event */ @@ -44,3 +52,5 @@ namespace { return event{static_cast(event_type::CHECK)}; } } + +POLYBAR_NS_END diff --git a/include/modules/meta/base.inl b/include/modules/meta/base.inl index 9c0c6d62..8fd82e1a 100644 --- a/include/modules/meta/base.inl +++ b/include/modules/meta/base.inl @@ -61,7 +61,7 @@ namespace modules { CAST_MOD(Impl)->wakeup(); CAST_MOD(Impl)->teardown(); - m_sig.emit(sig_ev::process_check{}); + m_sig.emit(sig_ev::check_state{}); } } @@ -91,7 +91,7 @@ namespace modules { template void module::broadcast() { m_changed = true; - m_sig.emit(sig_ev::process_broadcast{}); + m_sig.emit(sig_ev::notify_change{}); } template diff --git a/src/components/controller.cpp b/src/components/controller.cpp index fea2c20c..53115f30 100644 --- a/src/components/controller.cpp +++ b/src/components/controller.cpp @@ -188,7 +188,7 @@ bool controller::run(bool writeback) { read_events(); if (m_event_thread.joinable()) { - m_queue.enqueue(make_quit_evt(static_cast(g_reload))); + enqueue(make_quit_evt(static_cast(g_reload))); m_event_thread.join(); } @@ -312,7 +312,7 @@ void controller::read_events() { void controller::process_eventqueue() { m_log.info("Eventqueue worker (thread-id=%lu)", this_thread::get_id()); - m_queue.enqueue(make_update_evt(true)); + enqueue(make_update_evt(true)); while (!g_terminate) { event evt{}; @@ -320,22 +320,26 @@ void controller::process_eventqueue() { if (g_terminate) { break; - } else if (evt.type == static_cast(event_type::QUIT)) { - m_sig.emit(sig_ev::process_quit{make_quit_evt(evt.flag)}); - } else if (evt.type == static_cast(event_type::INPUT)) { + } else if (evt.type == event_type::QUIT) { + if (evt.flag) { + on(sig_ev::exit_reload{}); + } else { + on(sig_ev::exit_terminate{}); + } + } else if (evt.type == event_type::INPUT) { process_inputdata(); } else { event next{}; size_t swallowed{0}; while (swallowed++ < m_swallow_limit && m_queue.wait_dequeue_timed(next, m_swallow_update)) { - if (next.type == static_cast(event_type::QUIT)) { + if (next.type == event_type::QUIT) { evt = next; break; - } else if (next.type == static_cast(event_type::INPUT)) { + } else if (next.type == event_type::INPUT) { evt = next; break; } else if (evt.type != next.type) { - m_queue.try_enqueue(move(next)); + enqueue(move(next)); break; } else { m_log.trace_x("controller: Swallowing event within timeframe"); @@ -343,14 +347,18 @@ void controller::process_eventqueue() { } } - if (evt.type == static_cast(event_type::UPDATE)) { - m_sig.emit(sig_ev::process_update{make_update_evt(evt.flag)}); - } else if (evt.type == static_cast(event_type::INPUT)) { + if (evt.type == event_type::UPDATE) { + on(sig_ev::update{}); + } else if (evt.type == event_type::INPUT) { process_inputdata(); - } else if (evt.type == static_cast(event_type::QUIT)) { - m_sig.emit(sig_ev::process_quit{make_quit_evt(evt.flag)}); - } else if (evt.type == static_cast(event_type::CHECK)) { - m_sig.emit(sig_ev::process_check{}); + } else if (evt.type == event_type::QUIT) { + if (evt.flag) { + on(sig_ev::exit_reload{}); + } else { + on(sig_ev::exit_terminate{}); + } + } else if (evt.type == event_type::CHECK) { + on(sig_ev::check_state{}); } else { m_log.warn("Unknown event type for enqueued event (%d)", evt.type); } @@ -394,7 +402,7 @@ void controller::process_inputdata() { /** * Process broadcast events */ -bool controller::on(const sig_ev::process_broadcast&) { +bool controller::on(const sig_ev::notify_change&) { enqueue(make_update_evt(false)); return true; } @@ -402,9 +410,7 @@ bool controller::on(const sig_ev::process_broadcast&) { /** * Process eventqueue update event */ -bool controller::on(const sig_ev::process_update& evt) { - bool force{evt.data()->flag}; - +bool controller::on(const sig_ev::update&) { const bar_settings& bar{m_bar->settings()}; string contents; string separator{bar.separator}; @@ -475,7 +481,7 @@ bool controller::on(const sig_ev::process_update& evt) { try { if (!m_writeback) { - m_bar->parse(move(contents), force); + m_bar->parse(move(contents), false); } else { std::cout << contents << std::endl; } @@ -487,17 +493,25 @@ bool controller::on(const sig_ev::process_update& evt) { } /** - * Process eventqueue quit event + * Process eventqueue terminate event */ -bool controller::on(const sig_ev::process_quit& evt) { - raise((*evt()).flag ? SIGUSR1 : SIGALRM); +bool controller::on(const sig_ev::exit_terminate&) { + raise(SIGALRM); + return true; +} + +/** + * Process eventqueue reload event + */ +bool controller::on(const sig_ev::exit_reload&) { + raise(SIGUSR1); return true; } /** * Process eventqueue check event */ -bool controller::on(const sig_ev::process_check&) { +bool controller::on(const sig_ev::check_state&) { for (const auto& block : m_modules) { for (const auto& module : block.second) { if (module->running()) { @@ -506,7 +520,7 @@ bool controller::on(const sig_ev::process_check&) { } } m_log.warn("No running modules..."); - enqueue(make_quit_evt(false)); + on(exit_terminate{}); return true; } diff --git a/src/components/screen.cpp b/src/components/screen.cpp index dbbddc06..b6232a1b 100644 --- a/src/components/screen.cpp +++ b/src/components/screen.cpp @@ -113,7 +113,7 @@ void screen::handle(const evt::randr_screen_change_notify& evt) { } m_log.warn("randr_screen_change_notify (%ux%u)... reloading", evt->width, evt->height); - m_sig.emit(process_quit{make_quit_evt(true)}); + m_sig.emit(exit_reload{}); m_sigraised = true; } diff --git a/src/x11/tray_manager.cpp b/src/x11/tray_manager.cpp index 47c5d23d..bb5e6f7d 100644 --- a/src/x11/tray_manager.cpp +++ b/src/x11/tray_manager.cpp @@ -286,7 +286,7 @@ void tray_manager::deactivate(bool clear_selection) { m_connection.flush(); - m_sig.emit(process_update{make_update_evt(true)}); + m_sig.emit(notify_change{}); } /** @@ -325,7 +325,7 @@ void tray_manager::reconfigure() { m_connection.flush(); - m_sig.emit(process_update{make_update_evt(true)}); + m_sig.emit(notify_change{}); } /** diff --git a/src/x11/xutils.cpp b/src/x11/xutils.cpp index 7a48d96e..6cc23706 100644 --- a/src/x11/xutils.cpp +++ b/src/x11/xutils.cpp @@ -13,8 +13,11 @@ namespace xutils { xcb_connection_t* get_connection() { static xcb_connection_t* connection; if (!connection) { - XSetEventQueueOwner(xlib::get_display(), XCBOwnsEventQueue); - connection = XGetXCBConnection(xlib::get_display()); + auto display = xlib::get_display(); + if (display != nullptr) { + XSetEventQueueOwner(display, XCBOwnsEventQueue); + connection = XGetXCBConnection(xlib::get_display()); + } } return connection; }