2021-02-20 20:26:45 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <uv.h>
|
|
|
|
|
2021-03-10 23:09:06 +00:00
|
|
|
#include <stdexcept>
|
|
|
|
|
2021-02-20 20:26:45 +00:00
|
|
|
#include "common.hpp"
|
2021-09-11 09:46:37 +00:00
|
|
|
#include "components/logger.hpp"
|
2021-02-20 20:26:45 +00:00
|
|
|
|
|
|
|
POLYBAR_NS
|
|
|
|
|
2021-03-10 23:09:06 +00:00
|
|
|
/**
|
|
|
|
* Runs any libuv function with an integer error code return value and throws an
|
|
|
|
* exception on error.
|
|
|
|
*/
|
2021-09-12 13:42:30 +00:00
|
|
|
#define UV(fun, ...) \
|
|
|
|
do { \
|
|
|
|
int res = fun(__VA_ARGS__); \
|
|
|
|
if (res < 0) { \
|
|
|
|
throw std::runtime_error( \
|
|
|
|
__FILE__ ":"s + std::to_string(__LINE__) + ": libuv error for '" #fun "': "s + uv_strerror(res)); \
|
|
|
|
} \
|
2021-09-11 09:46:37 +00:00
|
|
|
} while (0);
|
2021-03-10 23:09:06 +00:00
|
|
|
|
2021-03-01 23:00:24 +00:00
|
|
|
template <class H, class... Args>
|
|
|
|
struct cb_helper {
|
|
|
|
std::function<void(Args...)> func;
|
|
|
|
|
|
|
|
static void callback(H* context, Args... args) {
|
|
|
|
const auto unpackedThis = static_cast<const cb_helper*>(context->data);
|
|
|
|
return unpackedThis->func(std::forward<Args>(args)...);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-09-12 13:42:30 +00:00
|
|
|
/**
|
|
|
|
* \tparam H Type of the handle
|
|
|
|
* \tparam I Type of the handle passed to the callback. Often the same as H, but not always (e.g. uv_read_start)
|
|
|
|
*/
|
2021-09-11 09:46:50 +00:00
|
|
|
template <typename H, typename I, typename... Args>
|
|
|
|
struct UVHandleGeneric {
|
|
|
|
UVHandleGeneric(std::function<void(Args...)> fun) {
|
2021-03-10 23:36:03 +00:00
|
|
|
handle = std::make_unique<H>();
|
2021-09-11 09:46:50 +00:00
|
|
|
cb = cb_helper<I, Args...>{fun};
|
2021-03-01 23:00:24 +00:00
|
|
|
handle->data = &cb;
|
2021-03-10 23:36:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<H> handle;
|
2021-09-11 09:46:50 +00:00
|
|
|
cb_helper<I, Args...> cb;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename H, typename... Args>
|
|
|
|
struct UVHandle : public UVHandleGeneric<H, H, Args...> {
|
|
|
|
UVHandle(std::function<void(Args...)> fun) : UVHandleGeneric<H, H, Args...>(fun) {}
|
2021-03-10 23:36:03 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
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());
|
2021-03-10 23:54:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void start(int signum) {
|
|
|
|
UV(uv_signal_start, handle.get(), cb.callback, signum);
|
|
|
|
}
|
2021-03-10 23:36:03 +00:00
|
|
|
};
|
2021-03-01 23:00:24 +00:00
|
|
|
|
2021-03-10 23:36:03 +00:00
|
|
|
struct PollHandle : public UVHandle<uv_poll_t, int, int> {
|
2021-09-11 09:46:37 +00:00
|
|
|
// TODO wrap callback and handle status
|
2021-03-10 23:36:03 +00:00
|
|
|
PollHandle(uv_loop_t* loop, int fd, std::function<void(int, int)> fun) : UVHandle(fun) {
|
|
|
|
UV(uv_poll_init, loop, handle.get(), fd);
|
2021-03-10 23:54:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
2021-03-01 23:00:24 +00:00
|
|
|
};
|
|
|
|
|
2021-09-11 09:46:50 +00:00
|
|
|
struct PipeHandle : public UVHandleGeneric<uv_pipe_t, uv_stream_t, ssize_t, const uv_buf_t*> {
|
2021-09-12 13:42:30 +00:00
|
|
|
std::function<void(const string)> func;
|
|
|
|
|
|
|
|
int fd;
|
|
|
|
|
2021-09-11 09:46:50 +00:00
|
|
|
PipeHandle(uv_loop_t* loop, std::function<void(const string)> fun)
|
2021-09-12 13:42:30 +00:00
|
|
|
: UVHandleGeneric([&](ssize_t nread, const uv_buf_t* buf) { read_cb(nread, buf); }), func(fun) {
|
2021-09-11 09:46:50 +00:00
|
|
|
UV(uv_pipe_init, loop, handle.get(), false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void start(int fd) {
|
2021-09-12 13:42:30 +00:00
|
|
|
this->fd = fd;
|
2021-09-11 09:46:50 +00:00
|
|
|
UV(uv_pipe_open, handle.get(), fd);
|
2021-09-12 13:42:30 +00:00
|
|
|
UV(uv_read_start, (uv_stream_t*)handle.get(), alloc_cb, &cb.callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void alloc_cb(uv_handle_t*, size_t, uv_buf_t* buf) {
|
|
|
|
buf->base = new char[BUFSIZ];
|
|
|
|
buf->len = BUFSIZ;
|
|
|
|
}
|
|
|
|
|
|
|
|
void read_cb(ssize_t nread, const uv_buf_t* buf) {
|
|
|
|
auto log = logger::make();
|
|
|
|
if (nread > 0) {
|
|
|
|
string payload = string(buf->base, nread);
|
|
|
|
log.notice("Bytes read: %d: '%s'", nread, payload);
|
|
|
|
func(payload);
|
|
|
|
} else if (nread < 0) {
|
|
|
|
if (nread != UV_EOF) {
|
|
|
|
log.err("Read error: %s", uv_err_name(nread));
|
|
|
|
uv_close((uv_handle_t*)handle.get(), nullptr);
|
|
|
|
} else {
|
|
|
|
// TODO this causes constant EOFs
|
|
|
|
start(this->fd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (buf->base) {
|
|
|
|
delete[] buf->base;
|
|
|
|
}
|
2021-09-11 09:46:50 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-09-12 13:53:14 +00:00
|
|
|
struct TimerHandle : public UVHandle<uv_timer_t> {
|
|
|
|
TimerHandle(uv_loop_t* loop, std::function<void(void)> fun) : UVHandle(fun) {
|
|
|
|
UV(uv_timer_init, loop, handle.get());
|
|
|
|
}
|
|
|
|
|
|
|
|
void start(uint64_t timeout, uint64_t repeat) {
|
|
|
|
UV(uv_timer_start, handle.get(), &cb.callback, timeout, repeat);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-09-12 13:57:05 +00:00
|
|
|
struct AsyncHandle : public UVHandle<uv_async_t> {
|
|
|
|
AsyncHandle(uv_loop_t* loop, std::function<void(void)> fun) : UVHandle(fun) {
|
|
|
|
UV(uv_async_init, loop, handle.get(), &cb.callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
void send() {
|
|
|
|
UV(uv_async_send, handle.get());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-02-20 20:26:45 +00:00
|
|
|
class eventloop {
|
|
|
|
public:
|
|
|
|
eventloop();
|
|
|
|
~eventloop();
|
|
|
|
|
|
|
|
void run();
|
|
|
|
|
|
|
|
void stop();
|
|
|
|
|
|
|
|
/**
|
2021-03-01 23:00:24 +00:00
|
|
|
* TODO make protected
|
2021-02-20 20:26:45 +00:00
|
|
|
*/
|
|
|
|
uv_loop_t* get() const {
|
|
|
|
return m_loop.get();
|
|
|
|
}
|
|
|
|
|
2021-03-01 23:00:24 +00:00
|
|
|
void signal_handler(int signum, std::function<void(int)> fun) {
|
|
|
|
auto handle = std::make_unique<SignalHandle>(get(), fun);
|
2021-03-10 23:54:24 +00:00
|
|
|
handle->start(signum);
|
2021-03-01 23:00:24 +00:00
|
|
|
m_sig_handles.push_back(std::move(handle));
|
|
|
|
}
|
|
|
|
|
2021-03-10 23:36:03 +00:00
|
|
|
void poll_handler(int events, int fd, std::function<void(int, int)> fun) {
|
|
|
|
auto handle = std::make_unique<PollHandle>(get(), fd, fun);
|
2021-03-10 23:54:24 +00:00
|
|
|
handle->start(events);
|
2021-03-10 23:36:03 +00:00
|
|
|
m_poll_handles.push_back(std::move(handle));
|
|
|
|
}
|
|
|
|
|
2021-03-10 23:54:24 +00:00
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
2021-03-01 23:00:24 +00:00
|
|
|
protected:
|
|
|
|
template <typename H, typename T, typename... Args, void (T::*F)(Args...)>
|
|
|
|
static void generic_cb(H* handle, Args&&... args) {
|
|
|
|
(static_cast<T*>(handle->data).*F)(std::forward<Args>(args)...);
|
|
|
|
}
|
|
|
|
|
2021-02-20 20:26:45 +00:00
|
|
|
private:
|
|
|
|
std::unique_ptr<uv_loop_t> m_loop{nullptr};
|
2021-03-01 23:00:24 +00:00
|
|
|
|
|
|
|
vector<std::unique_ptr<SignalHandle>> m_sig_handles;
|
2021-03-10 23:36:03 +00:00
|
|
|
vector<std::unique_ptr<PollHandle>> m_poll_handles;
|
2021-03-10 23:54:24 +00:00
|
|
|
vector<std::unique_ptr<FSEventHandle>> m_fs_event_handles;
|
2021-02-20 20:26:45 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
POLYBAR_NS_END
|