From f9d434052a41b72e6f62bec60ad2c4470791d2a0 Mon Sep 17 00:00:00 2001 From: patrick96 Date: Thu, 11 Mar 2021 00:36:03 +0100 Subject: [PATCH] Make UV handle wrappers more generic --- include/components/eventloop.hpp | 37 ++++++++++++++++++++++++-------- src/components/controller.cpp | 6 ++---- src/modules/xwindow.cpp | 1 - 3 files changed, 30 insertions(+), 14 deletions(-) diff --git a/include/components/eventloop.hpp b/include/components/eventloop.hpp index 3a9e49cb..db49826d 100644 --- a/include/components/eventloop.hpp +++ b/include/components/eventloop.hpp @@ -28,17 +28,29 @@ struct cb_helper { } }; -struct SignalHandle { - SignalHandle(uv_loop_t* loop, std::function fun) { - handle = std::make_unique(); - UV(uv_signal_init, loop, handle.get()); - cb = cb_helper{fun}; - +template +struct UVHandle { + UVHandle(std::function fun) { + handle = std::make_unique(); + cb = cb_helper{fun}; handle->data = &cb; - }; + } - std::unique_ptr handle; - cb_helper cb; + std::unique_ptr handle; + cb_helper cb; +}; + +struct SignalHandle : public UVHandle { + SignalHandle(uv_loop_t* loop, std::function fun) : UVHandle(fun) { + UV(uv_signal_init, loop, handle.get()); + }; +}; + +struct PollHandle : public UVHandle { + // TODO wrap callback and handle negative status + PollHandle(uv_loop_t* loop, int fd, std::function fun) : UVHandle(fun) { + UV(uv_poll_init, loop, handle.get(), fd); + }; }; class eventloop { @@ -63,6 +75,12 @@ class eventloop { m_sig_handles.push_back(std::move(handle)); } + void poll_handler(int events, int fd, std::function fun) { + auto handle = std::make_unique(get(), fd, fun); + UV(uv_poll_start, handle->handle.get(), events, &handle->cb.callback); + m_poll_handles.push_back(std::move(handle)); + } + protected: template static void generic_cb(H* handle, Args&&... args) { @@ -73,6 +91,7 @@ class eventloop { std::unique_ptr m_loop{nullptr}; vector> m_sig_handles; + vector> m_poll_handles; }; POLYBAR_NS_END diff --git a/src/components/controller.cpp b/src/components/controller.cpp index ea89fdda..5c2e5292 100644 --- a/src/components/controller.cpp +++ b/src/components/controller.cpp @@ -257,10 +257,8 @@ void controller::read_events() { eloop = std::make_unique(); auto loop = eloop->get(); - uv_poll_init(loop, conn_handle.get(), m_connection.get_file_descriptor()); - conn_handle->data = this; - - uv_poll_start(conn_handle.get(), UV_READABLE, conn_cb_wrapper); + eloop->poll_handler( + UV_READABLE, m_connection.get_file_descriptor(), [this](int status, int events) { conn_cb(status, events); }); for (auto s : {SIGINT, SIGQUIT, SIGTERM, SIGUSR1, SIGALRM}) { eloop->signal_handler(s, [this](int signum) { signal_handler(signum); }); diff --git a/src/modules/xwindow.cpp b/src/modules/xwindow.cpp index a6eccef5..010750c1 100644 --- a/src/modules/xwindow.cpp +++ b/src/modules/xwindow.cpp @@ -86,7 +86,6 @@ namespace modules { * Handler for XCB_PROPERTY_NOTIFY events */ void xwindow_module::handle(const evt::property_notify& evt) { - m_log.notice("%s: handle Thread id: %i", name(), concurrency_util::thread_id(this_thread::get_id())); if (evt->atom == _NET_ACTIVE_WINDOW) { update(true); } else if (evt->atom == _NET_CURRENT_DESKTOP) {