From f50f43af02fb63d843059fd5c837928e208f0594 Mon Sep 17 00:00:00 2001 From: Michael Carlberg Date: Fri, 16 Dec 2016 10:23:54 +0100 Subject: [PATCH] refactor: Cleanup --- include/components/bar.hpp | 4 +++- include/modules/meta/base.hpp | 28 ++++++++++++------------- include/modules/meta/base.inl | 4 +++- include/modules/meta/event_module.inl | 3 +-- include/modules/meta/inotify_module.inl | 2 +- include/modules/meta/timer_module.inl | 2 +- src/components/bar.cpp | 22 +++++++++++++++++++ src/components/controller.cpp | 12 +++++------ src/components/eventloop.cpp | 10 +++------ src/components/renderer.cpp | 4 ---- src/x11/xlib.cpp | 3 +-- 11 files changed, 55 insertions(+), 39 deletions(-) diff --git a/include/components/bar.hpp b/include/components/bar.hpp index 67cdc16d..5db615f9 100644 --- a/include/components/bar.hpp +++ b/include/components/bar.hpp @@ -22,7 +22,7 @@ class screen; class tray_manager; class logger; -class bar : public xpp::event::sink { +class bar : public xpp::event::sink { public: using make_type = unique_ptr; static make_type make(); @@ -43,6 +43,8 @@ class bar : public xpp::event::sink tags; - label_t prefix; - label_t suffix; - string fg; - string bg; - string ul; - string ol; - int spacing; - int padding; - int margin; - int offset; + string value{}; + vector tags{}; + label_t prefix{}; + label_t suffix{}; + string fg{}; + string bg{}; + string ul{}; + string ol{}; + int spacing{}; + int padding{}; + int margin{}; + int offset{}; string decorate(builder* builder, string output); }; @@ -151,12 +151,12 @@ namespace modules { callback<> m_update_callback; callback<> m_stop_callback; - concurrency_util::spin_lock m_lock; - const bar_settings m_bar; const logger& m_log; const config& m_conf; + std::mutex m_buildlock; + std::mutex m_updatelock; std::mutex m_sleeplock; std::condition_variable m_sleephandler; diff --git a/include/modules/meta/base.inl b/include/modules/meta/base.inl index d06e8247..3c35350a 100644 --- a/include/modules/meta/base.inl +++ b/include/modules/meta/base.inl @@ -70,7 +70,8 @@ namespace modules { wakeup(); - std::lock_guard guard(m_lock); + std::lock_guard guard_a(m_buildlock); + std::lock_guard guard_b(m_updatelock); { CAST_MOD(Impl)->teardown(); @@ -154,6 +155,7 @@ namespace modules { m_log.trace("%s: Module is disabled", name()); return ""; } + std::lock_guard guard(m_buildlock); auto format_name = CONST_MOD(Impl).get_format(); auto format = m_formatter->get(format_name); diff --git a/include/modules/meta/event_module.inl b/include/modules/meta/event_module.inl index 4c2a5b8d..890bf0a5 100644 --- a/include/modules/meta/event_module.inl +++ b/include/modules/meta/event_module.inl @@ -26,7 +26,7 @@ namespace modules { if (!CONST_MOD(Impl).running()) break; - std::lock_guard guard(this->m_lock); + std::lock_guard guard(this->m_updatelock); { if (!CAST_MOD(Impl)->has_event()) continue; @@ -34,7 +34,6 @@ namespace modules { break; if (!CAST_MOD(Impl)->update()) continue; - CAST_MOD(Impl)->broadcast(); } } diff --git a/include/modules/meta/inotify_module.inl b/include/modules/meta/inotify_module.inl index 0e70adf3..7f61f0a5 100644 --- a/include/modules/meta/inotify_module.inl +++ b/include/modules/meta/inotify_module.inl @@ -58,7 +58,7 @@ namespace modules { } while (CONST_MOD(Impl).running()) { - std::unique_lock guard(this->m_lock); + std::unique_lock guard(this->m_updatelock); { for (auto&& w : watches) { this->m_log.trace_x("%s: Poll inotify watch %s", CONST_MOD(Impl).name(), w->path()); diff --git a/include/modules/meta/timer_module.inl b/include/modules/meta/timer_module.inl index dbda961b..409232be 100644 --- a/include/modules/meta/timer_module.inl +++ b/include/modules/meta/timer_module.inl @@ -15,7 +15,7 @@ namespace modules { void timer_module::runner() { try { while (CONST_MOD(Impl).running()) { - std::lock_guard guard(this->m_lock); + std::lock_guard guard(this->m_updatelock); { if (CAST_MOD(Impl)->update()) CAST_MOD(Impl)->broadcast(); diff --git a/src/components/bar.cpp b/src/components/bar.cpp index b8c5a789..982b0901 100644 --- a/src/components/bar.cpp +++ b/src/components/bar.cpp @@ -415,6 +415,28 @@ void bar::broadcast_visibility() { } } +/** + * Event handler for XCB_DESTROY_NOTIFY events + */ +void bar::handle(const evt::client_message& evt) { + if (evt->type != WM_PROTOCOLS || evt->data.data32[0] != WM_DELETE_WINDOW) { + return; + } + if (evt->window == m_opts.window) { + m_log.err("Bar window has been destroyed, shutting down..."); + m_connection.disconnect(); + } +} + +/** + * Event handler for XCB_DESTROY_NOTIFY events + */ +void bar::handle(const evt::destroy_notify& evt) { + if (evt->window == m_opts.window) { + m_connection.disconnect(); + } +} + /** * Event handler for XCB_ENTER_NOTIFY events * diff --git a/src/components/controller.cpp b/src/components/controller.cpp index f966986c..2646e84f 100644 --- a/src/components/controller.cpp +++ b/src/components/controller.cpp @@ -336,7 +336,7 @@ bool controller::on(const sig_ev::process_update& evt) { try { if (!m_writeback) { - m_bar->parse(move(contents), evt()); + m_bar->parse(move(contents), force); } else { std::cout << contents << std::endl; } @@ -349,7 +349,7 @@ bool controller::on(const sig_ev::process_update& evt) { bool controller::on(const sig_ev::process_input& evt) { try { - string input{*evt()}; + string input{*evt.data()}; if (m_command) { m_log.warn("Terminating previous shell command"); @@ -378,7 +378,7 @@ bool controller::on(const sig_ui::button_press& evt) { return false; } - string input{*evt()}; + string input{*evt.data()}; if (input.empty()) { m_log.err("Cannot enqueue empty input"); @@ -389,7 +389,7 @@ bool controller::on(const sig_ui::button_press& evt) { } bool controller::on(const sig_ipc::process_action& evt) { - ipc_action a{*evt()}; + ipc_action a{*evt.data()}; string action{a.payload}; action.erase(0, strlen(ipc_action::prefix)); @@ -403,7 +403,7 @@ bool controller::on(const sig_ipc::process_action& evt) { } bool controller::on(const sig_ipc::process_command& evt) { - ipc_command c{*evt()}; + ipc_command c{*evt.data()}; string command{c.payload}; command.erase(0, strlen(ipc_command::prefix)); @@ -423,7 +423,7 @@ bool controller::on(const sig_ipc::process_command& evt) { } bool controller::on(const sig_ipc::process_hook& evt) { - const ipc_hook hook{*evt()}; + const ipc_hook hook{*evt.data()}; for (const auto& block : m_eventloop->modules()) { for (const auto& module : block.second) { diff --git a/src/components/eventloop.cpp b/src/components/eventloop.cpp index 079b7c89..885dc13d 100644 --- a/src/components/eventloop.cpp +++ b/src/components/eventloop.cpp @@ -93,11 +93,11 @@ void eventloop::start() { if (evt.type == static_cast(event_type::INPUT)) { handle_inputdata(); } else if (evt.type == static_cast(event_type::QUIT)) { - m_sig.emit(process_quit{reinterpret_cast(evt)}); + m_sig.emit(process_quit{make_input_evt()}); } else if (evt.type == static_cast(event_type::UPDATE)) { - m_sig.emit(process_update{reinterpret_cast(evt)}); + m_sig.emit(process_update{make_update_evt(evt.flag)}); } else if (evt.type == static_cast(event_type::CHECK)) { - m_sig.emit(process_check{reinterpret_cast(evt)}); + m_sig.emit(process_check{make_check_evt()}); } else { m_log.warn("Unknown event type for enqueued event (%d)", evt.type); } @@ -241,7 +241,6 @@ bool eventloop::on(const process_check&) { } bool eventloop::on(const process_quit& evt) { - assert((*evt()).type == static_cast(event_type::QUIT)); const event quit{static_cast(*evt())}; m_log.info("Processing QUIT event (reload=%i)", quit.flag); m_running = false; @@ -254,7 +253,6 @@ bool eventloop::on(const enqueue_event& evt) { } bool eventloop::on(const enqueue_quit& evt) { - assert((*evt()).type == static_cast(event_type::QUIT)); if (m_running) { const event quit{reinterpret_cast(*evt())}; m_log.info("Enqueuing QUIT event (reload=%i)", quit.flag); @@ -265,7 +263,6 @@ bool eventloop::on(const enqueue_quit& evt) { bool eventloop::on(const enqueue_update& evt) { event update{reinterpret_cast(*evt())}; - assert(update.type == static_cast(event_type::UPDATE)); m_log.trace("eventloop: enqueuing UPDATE event (force=%i)", update.flag); return enqueue(move(update)); } @@ -277,7 +274,6 @@ bool eventloop::on(const enqueue_input& evt) { bool eventloop::on(const enqueue_check& evt) { event check{reinterpret_cast(*evt())}; - assert(check.type == static_cast(event_type::CHECK)); m_log.trace("eventloop: enqueuing CHECK event"); return enqueue(move(check)); } diff --git a/src/components/renderer.cpp b/src/components/renderer.cpp index 0020d821..d785cb4a 100644 --- a/src/components/renderer.cpp +++ b/src/components/renderer.cpp @@ -474,10 +474,6 @@ void renderer::debug_hints() { // clang-format on }}; - for (auto&& hintwin : m_debughints) { - m_connection.destroy_window(hintwin); - } - m_debughints.clear(); for (auto&& action : m_actions) { diff --git a/src/x11/xlib.cpp b/src/x11/xlib.cpp index 844fa703..fd072d3d 100644 --- a/src/x11/xlib.cpp +++ b/src/x11/xlib.cpp @@ -9,8 +9,7 @@ namespace xlib { shared_ptr get_display() { if (!g_display_ptr) { - // g_display_ptr = shared_ptr(XOpenDisplay(nullptr), bind(XCloseDisplay, placeholders::_1)); - g_display_ptr = shared_ptr(XOpenDisplay(nullptr), factory_util::null_deleter{}); + g_display_ptr = shared_ptr(XOpenDisplay(nullptr), [=](Display* ptr) { XCloseDisplay(ptr); }); } return g_display_ptr; }