Move config watcher into eventloop

This commit is contained in:
patrick96 2021-03-11 00:54:24 +01:00 committed by Patrick Ziegler
parent f9d434052a
commit 53ce1ae414
3 changed files with 35 additions and 20 deletions

View File

@ -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();

View File

@ -43,14 +43,33 @@ struct UVHandle {
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());
};
}
void start(int signum) {
UV(uv_signal_start, handle.get(), cb.callback, signum);
}
};
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);
};
}
void start(int events) {
UV(uv_poll_start, handle.get(), events, &cb.callback);
}
};
struct FSEventHandle : public UVHandle<uv_fs_event_t, const char*, int, int> {
// TODO wrap callback and handle status
FSEventHandle(uv_loop_t* loop, std::function<void(const char*, int, int)> 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<void(int)> fun) {
auto handle = std::make_unique<SignalHandle>(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<void(int, int)> fun) {
auto handle = std::make_unique<PollHandle>(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<void(const char*, int, int)> fun) {
auto handle = std::make_unique<FSEventHandle>(get(), fun);
handle->start(path);
m_fs_event_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) {
@ -92,6 +117,7 @@ class eventloop {
vector<std::unique_ptr<SignalHandle>> m_sig_handles;
vector<std::unique_ptr<PollHandle>> m_poll_handles;
vector<std::unique_ptr<FSEventHandle>> m_fs_event_handles;
};
POLYBAR_NS_END

View File

@ -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<controller*>(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<eventloop*>(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<uv_poll_t>();
auto conf_handle = std::unique_ptr<uv_fs_event_t>(nullptr);
auto ipc_handle = std::unique_ptr<uv_pipe_t>(nullptr);
try {
@ -265,11 +256,8 @@ void controller::read_events() {
}
if (m_confwatch) {
conf_handle = std::make_unique<uv_fs_event_t>();
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) {