#pragma once #include #include #include #include #include #include #include #include "common.hpp" #include "components/screen.hpp" #include "x11/events.hpp" #include "x11/extensions/all.hpp" #include "x11/registry.hpp" #include "x11/types.hpp" POLYBAR_NS namespace detail { class displaylock { public: explicit displaylock(Display* display) : m_display(forward(display)) { XLockDisplay(m_display); } ~displaylock() { XUnlockDisplay(m_display); } protected: Display* m_display; }; template class interfaces : public xpp::x::extension::interface, Connection>, public Extensions::template interface, Connection>... { public: const Connection& connection() const { return static_cast(*this); } }; template class connection_base : public xpp::core, public xpp::generic::error_dispatcher, public detail::interfaces, Extensions...>, private xpp::x::extension, private xpp::x::extension::error_dispatcher, private Extensions..., private Extensions::error_dispatcher... { public: template explicit connection_base(Args&&... args) : xpp::core(forward(args)...) , interfaces, Extensions...>(*this) , Extensions(m_c.get())... , Extensions::error_dispatcher(static_cast(*this).get())... { m_root_window = screen_of_display(default_screen())->root; } virtual ~connection_base() {} void operator()(const shared_ptr& error) const { check(error); } template const Extension& extension() const { return static_cast(*this); } template WindowType root() const { using make = xpp::generic::factory::make; return make()(*this, m_root_window); } shared_ptr wait_for_event() const { try { return core::wait_for_event(); } catch (const shared_ptr& error) { check(error); } throw; // re-throw exception } shared_ptr wait_for_special_event(xcb_special_event_t* se) const { try { return core::wait_for_special_event(se); } catch (const shared_ptr& error) { check(error); } throw; // re-throw exception } private: xcb_window_t m_root_window; template void check(const shared_ptr& error) const { check(error); check(error); } template void check(const shared_ptr& error) const { using error_dispatcher = typename Extension::error_dispatcher; auto& dispatcher = static_cast(*this); dispatcher(error); } }; } class connection : public detail::connection_base { public: using base_type = detail::connection_base; using make_type = connection&; static make_type make(Display* display = nullptr); explicit connection(Display* dsp); ~connection(); const connection& operator=(const connection& o) { return o; } static void pack_values(unsigned int mask, const unsigned int* src, unsigned int* dest); static void pack_values(unsigned int mask, const xcb_params_cw_t* src, unsigned int* dest); static void pack_values(unsigned int mask, const xcb_params_gc_t* src, unsigned int* dest); static void pack_values(unsigned int mask, const xcb_params_configure_window_t* src, unsigned int* dest); operator Display*() const; Visual* visual(unsigned char depth = 32U); xcb_screen_t* screen(bool realloc = false); string id(xcb_window_t w) const; void ensure_event_mask(xcb_window_t win, unsigned int 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, unsigned int 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) { int fd = get_file_descriptor(); shared_ptr evt{}; while (!connection_has_error()) { fd_set fds; FD_ZERO(&fds); FD_SET(fd, &fds); if (!select(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; } } } template void attach_sink(Sink&& sink, registry::priority prio = 0) { m_registry.attach(prio, forward(sink)); } template void detach_sink(Sink&& sink, registry::priority prio = 0) { m_registry.detach(prio, forward(sink)); } protected: Display* m_display{nullptr}; map m_visual; registry m_registry{*this}; xcb_screen_t* m_screen{nullptr}; }; POLYBAR_NS_END