#pragma once #include #include #include "common.hpp" #include "components/logger.hpp" POLYBAR_NS /** * Runs any libuv function with an integer error code return value and throws an * exception on error. */ #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)); \ } \ } while (0); void close_callback(uv_handle_t*); /** * \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) */ template struct UVHandleGeneric { UVHandleGeneric(std::function fun) { handle = new H; handle->data = this; this->func = fun; } ~UVHandleGeneric() { if (handle && !uv_is_closing((uv_handle_t*)handle)) { uv_close((uv_handle_t*)handle, close_callback); } } void close() { if (handle) { delete handle; handle = nullptr; } } static void callback(I* context, Args... args) { const auto unpackedThis = static_cast(context->data); return unpackedThis->func(std::forward(args)...); } H* handle; std::function func; }; template struct UVHandle : public UVHandleGeneric { UVHandle(std::function fun) : UVHandleGeneric(fun) {} }; struct SignalHandle : public UVHandle { SignalHandle(uv_loop_t* loop, std::function fun); void start(int signum); }; struct PollHandle : public UVHandle { PollHandle(uv_loop_t* loop, int fd, std::function fun); void start(int events); }; struct FSEventHandle : public UVHandle { FSEventHandle(uv_loop_t* loop, std::function fun); void start(const string& path); }; struct PipeHandle : public UVHandleGeneric { PipeHandle(uv_loop_t* loop, std::function fun, std::function eof_cb); void start(int fd); void read_cb(ssize_t nread, const uv_buf_t* buf); std::function func; std::function eof_cb; int fd; }; struct TimerHandle : public UVHandle { TimerHandle(uv_loop_t* loop, std::function fun); void start(uint64_t timeout, uint64_t repeat); }; struct AsyncHandle : public UVHandle { AsyncHandle(uv_loop_t* loop, std::function fun); void send(); }; using SignalHandle_t = std::unique_ptr; using PollHandle_t = std::unique_ptr; using FSEventHandle_t = std::unique_ptr; using PipeHandle_t = std::unique_ptr; using TimerHandle_t = std::unique_ptr; // shared_ptr because we need a reference outside to call send using AsyncHandle_t = std::shared_ptr; class eventloop { public: eventloop(); ~eventloop(); void run(); void stop(); void signal_handler(int signum, std::function fun); void poll_handler(int events, int fd, std::function fun); void fs_event_handler(const string& path, std::function fun); void pipe_handle(int fd, std::function fun, std::function eof_cb); void timer_handle(uint64_t timeout, uint64_t repeat, std::function fun); AsyncHandle_t async_handle(std::function fun); protected: uv_loop_t* get() const; private: std::unique_ptr m_loop{nullptr}; vector m_sig_handles; vector m_poll_handles; vector m_fs_event_handles; vector m_pipe_handles; vector m_timer_handles; vector m_async_handles; }; POLYBAR_NS_END