From 53ce1ae4147450fb28713b9d48b49c89251cbfb3 Mon Sep 17 00:00:00 2001
From: patrick96
Date: Thu, 11 Mar 2021 00:54:24 +0100
Subject: [PATCH] Move config watcher into eventloop
---
include/components/controller.hpp | 1 +
include/components/eventloop.hpp | 34 +++++++++++++++++++++++++++----
src/components/controller.cpp | 20 ++++--------------
3 files changed, 35 insertions(+), 20 deletions(-)
diff --git a/include/components/controller.hpp b/include/components/controller.hpp
index 023ebad0..ad834a6b 100644
--- a/include/components/controller.hpp
+++ b/include/components/controller.hpp
@@ -57,6 +57,7 @@ class controller
void conn_cb(int status, int events);
void ipc_cb(string buf);
+ void confwatch_handler(const char* fname, int events, int status);
protected:
void read_events();
diff --git a/include/components/eventloop.hpp b/include/components/eventloop.hpp
index db49826d..32c993fc 100644
--- a/include/components/eventloop.hpp
+++ b/include/components/eventloop.hpp
@@ -43,14 +43,33 @@ struct UVHandle {
struct SignalHandle : public UVHandle {
SignalHandle(uv_loop_t* loop, std::function fun) : UVHandle(fun) {
UV(uv_signal_init, loop, handle.get());
- };
+ }
+
+ void start(int signum) {
+ UV(uv_signal_start, handle.get(), cb.callback, signum);
+ }
};
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);
- };
+ }
+
+ void start(int events) {
+ UV(uv_poll_start, handle.get(), events, &cb.callback);
+ }
+};
+
+struct FSEventHandle : public UVHandle {
+ // TODO wrap callback and handle status
+ FSEventHandle(uv_loop_t* loop, std::function fun) : UVHandle(fun) {
+ UV(uv_fs_event_init, loop, handle.get());
+ }
+
+ void start(const string& path) {
+ UV(uv_fs_event_start, handle.get(), &cb.callback, path.c_str(), 0);
+ }
};
class eventloop {
@@ -71,16 +90,22 @@ class eventloop {
void signal_handler(int signum, std::function fun) {
auto handle = std::make_unique(get(), fun);
- UV(uv_signal_start, handle->handle.get(), &handle->cb.callback, signum);
+ handle->start(signum);
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);
+ handle->start(events);
m_poll_handles.push_back(std::move(handle));
}
+ void fs_event_handler(const string& path, std::function fun) {
+ auto handle = std::make_unique(get(), fun);
+ handle->start(path);
+ m_fs_event_handles.push_back(std::move(handle));
+ }
+
protected:
template
static void generic_cb(H* handle, Args&&... args) {
@@ -92,6 +117,7 @@ class eventloop {
vector> m_sig_handles;
vector> m_poll_handles;
+ vector> m_fs_event_handles;
};
POLYBAR_NS_END
diff --git a/src/components/controller.cpp b/src/components/controller.cpp
index 5c2e5292..56b36a88 100644
--- a/src/components/controller.cpp
+++ b/src/components/controller.cpp
@@ -174,7 +174,6 @@ bool controller::enqueue(string&& input_data) {
void controller::conn_cb(int, int) {
// TODO handle negative status
-
if (m_connection.connection_has_error()) {
g_terminate = 1;
g_reload = 0;
@@ -199,10 +198,6 @@ void controller::ipc_cb(string buf) {
m_ipc->receive_message(buf);
}
-static void conn_cb_wrapper(uv_poll_t* handle, int status, int events) {
- static_cast(handle->data)->conn_cb(status, events);
-}
-
void controller::signal_handler(int signum) {
m_log.notice("Received signal SIG%s", sigabbrev_np(signum));
g_terminate = 1;
@@ -210,12 +205,10 @@ void controller::signal_handler(int signum) {
eloop->stop();
}
-static void confwatch_cb_wrapper(uv_fs_event_t* handle, const char* fname, int, int) {
- // TODO handle error
- std::cout << fname << std::endl;
+void controller::confwatch_handler(const char*, int, int) {
g_terminate = 1;
g_reload = 1;
- static_cast(handle->loop->data)->stop();
+ eloop->stop();
}
static void ipc_alloc_cb(uv_handle_t*, size_t, uv_buf_t* buf) {
@@ -249,8 +242,6 @@ static void ipc_read_cb_wrapper(uv_stream_t* stream, ssize_t nread, const uv_buf
void controller::read_events() {
m_log.info("Entering event loop (thread-id=%lu)", this_thread::get_id());
- auto conn_handle = std::make_unique();
- auto conf_handle = std::unique_ptr(nullptr);
auto ipc_handle = std::unique_ptr(nullptr);
try {
@@ -265,11 +256,8 @@ void controller::read_events() {
}
if (m_confwatch) {
- conf_handle = std::make_unique();
- uv_fs_event_init(loop, conf_handle.get());
- conf_handle->data = this;
-
- uv_fs_event_start(conf_handle.get(), confwatch_cb_wrapper, m_confwatch->path().c_str(), 0);
+ eloop->fs_event_handler(m_confwatch->path(),
+ [this](const char* path, int events, int status) { confwatch_handler(path, events, status); });
}
if (m_ipc) {