Make UV handle wrappers more generic

This commit is contained in:
patrick96 2021-03-11 00:36:03 +01:00 committed by Patrick Ziegler
parent 60ee63c0db
commit f9d434052a
3 changed files with 30 additions and 14 deletions

View File

@ -28,17 +28,29 @@ struct cb_helper {
}
};
struct SignalHandle {
SignalHandle(uv_loop_t* loop, std::function<void(int)> fun) {
handle = std::make_unique<uv_signal_t>();
UV(uv_signal_init, loop, handle.get());
cb = cb_helper<uv_signal_t, int>{fun};
template <typename H, typename... Args>
struct UVHandle {
UVHandle(std::function<void(Args...)> fun) {
handle = std::make_unique<H>();
cb = cb_helper<H, Args...>{fun};
handle->data = &cb;
};
}
std::unique_ptr<uv_signal_t> handle;
cb_helper<uv_signal_t, int> cb;
std::unique_ptr<H> handle;
cb_helper<H, Args...> cb;
};
struct SignalHandle : public UVHandle<uv_signal_t, int> {
SignalHandle(uv_loop_t* loop, std::function<void(int)> fun) : UVHandle(fun) {
UV(uv_signal_init, loop, handle.get());
};
};
struct PollHandle : public UVHandle<uv_poll_t, int, int> {
// TODO wrap callback and handle negative status
PollHandle(uv_loop_t* loop, int fd, std::function<void(int, int)> 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<void(int, int)> fun) {
auto handle = std::make_unique<PollHandle>(get(), fd, fun);
UV(uv_poll_start, handle->handle.get(), events, &handle->cb.callback);
m_poll_handles.push_back(std::move(handle));
}
protected:
template <typename H, typename T, typename... Args, void (T::*F)(Args...)>
static void generic_cb(H* handle, Args&&... args) {
@ -73,6 +91,7 @@ class eventloop {
std::unique_ptr<uv_loop_t> m_loop{nullptr};
vector<std::unique_ptr<SignalHandle>> m_sig_handles;
vector<std::unique_ptr<PollHandle>> m_poll_handles;
};
POLYBAR_NS_END

View File

@ -257,10 +257,8 @@ void controller::read_events() {
eloop = std::make_unique<eventloop>();
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); });

View File

@ -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) {