#pragma once #include #include #include #include "common.hpp" #include "utils/file.hpp" #include "x11/extensions/all.hpp" #include "x11/registry.hpp" #include "x11/types.hpp" POLYBAR_NS using xpp_connection = xpp::connection; class connection : public xpp_connection { public: using make_type = connection&; static make_type make(xcb_connection_t* conn = nullptr); explicit connection(xcb_connection_t* conn) : xpp_connection(conn) {} explicit connection(xcb_connection_t* conn, int connection_fd) : xpp_connection(conn), m_connection_fd(file_util::make_file_descriptor(connection_fd)) {} connection& operator=(const connection&) { return *this; } void preload_atoms(); void query_extensions(); string id(xcb_window_t w) const; xcb_screen_t* screen(bool realloc = false); void ensure_event_mask(xcb_window_t win, uint32_t event); void clear_event_mask(xcb_window_t win); shared_ptr make_client_message(xcb_atom_t type, xcb_window_t target) const; void send_client_message(const shared_ptr& message, xcb_window_t target, uint32_t event_mask = 0xFFFFFF, bool propagate = false) const; xcb_visualtype_t* visual_type(xcb_screen_t* screen, int match_depth = 32); static string error_str(int error_code); void dispatch_event(const shared_ptr& evt) const; template void wait_for_response(function check_event) { shared_ptr evt{}; while (!connection_has_error()) { fd_set fds; FD_ZERO(&fds); FD_SET(*m_connection_fd, &fds); if (!select(*m_connection_fd + 1, &fds, nullptr, nullptr, nullptr)) { continue; } else if ((evt = shared_ptr(xcb_poll_for_event(*this), free)) == nullptr) { continue; } else if (evt->response_type != ResponseType) { continue; } else if (check_event(reinterpret_cast(&*evt))) { break; } } } /** * Attach sink to the registry */ template void attach_sink(Sink&& sink, registry::priority prio = 0) { m_registry.attach(prio, forward(sink)); } /** * Detach sink from the registry */ template void detach_sink(Sink&& sink, registry::priority prio = 0) { m_registry.detach(prio, forward(sink)); } protected: registry m_registry{*this}; xcb_screen_t* m_screen{nullptr}; unique_ptr m_connection_fd; }; POLYBAR_NS_END