polybar-dwm/include/x11/connection.hpp

173 lines
5.9 KiB
C++
Raw Normal View History

2016-11-02 19:22:45 +00:00
#pragma once
2017-01-24 07:49:27 +00:00
#include <xcb/xcb.h>
2017-01-24 11:09:07 +00:00
#include <cstdlib>
2016-12-26 08:40:15 +00:00
#include <xpp/core.hpp>
#include <xpp/generic/factory.hpp>
#include <xpp/proto/x.hpp>
2016-11-02 19:22:45 +00:00
#include "common.hpp"
2016-12-26 09:27:30 +00:00
#include "components/screen.hpp"
2016-12-21 13:55:19 +00:00
#include "x11/extensions/all.hpp"
#include "x11/registry.hpp"
2016-12-26 09:27:30 +00:00
#include "x11/types.hpp"
2016-11-02 19:22:45 +00:00
2016-11-19 05:22:44 +00:00
POLYBAR_NS
2016-11-02 19:22:45 +00:00
2016-12-26 08:40:15 +00:00
namespace detail {
template <typename Connection, typename... Extensions>
class interfaces : public xpp::x::extension::interface<interfaces<Connection, Extensions...>, Connection>,
public Extensions::template interface<interfaces<Connection, Extensions...>, Connection>... {
public:
const Connection& connection() const {
return static_cast<const Connection&>(*this);
}
};
template <typename Derived, typename... Extensions>
class connection_base : public xpp::core,
public xpp::generic::error_dispatcher,
public detail::interfaces<connection_base<Derived, Extensions...>, Extensions...>,
private xpp::x::extension,
private xpp::x::extension::error_dispatcher,
private Extensions...,
private Extensions::error_dispatcher... {
public:
2017-01-24 07:49:27 +00:00
explicit connection_base(xcb_connection_t* c, int s)
: xpp::core(c)
2016-12-26 08:40:15 +00:00
, interfaces<connection_base<Derived, Extensions...>, Extensions...>(*this)
, Extensions(m_c.get())...
2016-12-26 08:40:15 +00:00
, Extensions::error_dispatcher(static_cast<Extensions&>(*this).get())... {
2017-01-24 07:49:27 +00:00
core::m_screen = s;
2016-12-26 08:40:15 +00:00
m_root_window = screen_of_display(default_screen())->root;
}
void operator()(const shared_ptr<xcb_generic_error_t>& error) const override {
2016-12-26 08:40:15 +00:00
check<xpp::x::extension, Extensions...>(error);
}
template <typename Extension>
const Extension& extension() const {
return static_cast<const Extension&>(*this);
}
template <typename WindowType = xcb_window_t>
WindowType root() const {
using make = xpp::generic::factory::make<connection_base, xcb_window_t, WindowType>;
2016-12-26 08:40:15 +00:00
return make()(*this, m_root_window);
}
2016-11-02 19:22:45 +00:00
shared_ptr<xcb_generic_event_t> wait_for_event() const override {
2016-12-26 08:40:15 +00:00
try {
return core::wait_for_event();
} catch (const shared_ptr<xcb_generic_error_t>& error) {
check<xpp::x::extension, Extensions...>(error);
}
2022-03-06 15:40:42 +00:00
throw; // re-throw exception
2016-12-26 08:40:15 +00:00
}
shared_ptr<xcb_generic_event_t> wait_for_special_event(xcb_special_event_t* se) const override {
2016-12-26 08:40:15 +00:00
try {
return core::wait_for_special_event(se);
} catch (const shared_ptr<xcb_generic_error_t>& error) {
check<xpp::x::extension, Extensions...>(error);
}
2022-03-06 15:40:42 +00:00
throw; // re-throw exception
2016-12-26 08:40:15 +00:00
}
private:
xcb_window_t m_root_window;
template <typename Extension, typename Next, typename... Rest>
void check(const shared_ptr<xcb_generic_error_t>& error) const {
check<Extension>(error);
check<Next, Rest...>(error);
}
template <typename Extension>
void check(const shared_ptr<xcb_generic_error_t>& error) const {
using error_dispatcher = typename Extension::error_dispatcher;
auto& dispatcher = static_cast<const error_dispatcher&>(*this);
dispatcher(error);
}
};
2022-03-06 15:40:42 +00:00
} // namespace detail
2016-12-26 08:40:15 +00:00
class connection : public detail::connection_base<connection&, XPP_EXTENSION_LIST> {
2016-11-02 19:22:45 +00:00
public:
2016-12-26 08:40:15 +00:00
using base_type = detail::connection_base<connection&, XPP_EXTENSION_LIST>;
2016-12-09 08:40:46 +00:00
using make_type = connection&;
2017-01-24 07:49:27 +00:00
static make_type make(xcb_connection_t* conn = nullptr, int default_screen = 0);
2016-12-21 13:55:19 +00:00
2017-01-24 07:49:27 +00:00
explicit connection(xcb_connection_t* c, int default_screen);
~connection();
2016-11-02 19:22:45 +00:00
2016-12-26 08:40:15 +00:00
const connection& operator=(const connection& o) {
return o;
2016-11-02 19:22:45 +00:00
}
2017-01-19 10:11:28 +00:00
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);
2016-12-21 13:55:19 +00:00
xcb_screen_t* screen(bool realloc = false);
2016-11-02 19:22:45 +00:00
string id(xcb_window_t w) const;
2017-01-19 10:11:28 +00:00
void ensure_event_mask(xcb_window_t win, unsigned int event);
void clear_event_mask(xcb_window_t win);
2022-03-06 15:40:42 +00:00
xcb_client_message_event_t make_client_message(xcb_atom_t type, xcb_window_t target) const;
void send_client_message(const xcb_client_message_event_t& message, xcb_window_t target,
2017-01-19 10:11:28 +00:00
unsigned int event_mask = 0xFFFFFF, bool propagate = false) const;
2016-11-02 19:22:45 +00:00
xcb_visualtype_t* visual_type(xcb_screen_t* screen, int match_depth = 32);
xcb_visualtype_t* visual_type_for_id(xcb_screen_t* screen, xcb_visualid_t visual_id);
2016-11-02 19:22:45 +00:00
2017-01-24 07:49:27 +00:00
bool root_pixmap(xcb_pixmap_t* pixmap, int* depth, xcb_rectangle_t* rect);
2016-11-02 19:22:45 +00:00
static string error_str(int error_code);
2016-12-21 22:22:02 +00:00
void dispatch_event(const shared_ptr<xcb_generic_event_t>& evt) const;
2016-12-21 13:55:19 +00:00
2017-01-19 10:11:28 +00:00
template <typename Event, unsigned int ResponseType>
2016-12-21 22:22:02 +00:00
void wait_for_response(function<bool(const Event*)> check_event) {
2016-12-26 08:40:15 +00:00
int fd = get_file_descriptor();
2016-12-21 22:22:02 +00:00
shared_ptr<xcb_generic_event_t> evt{};
2016-12-21 13:55:19 +00:00
while (!connection_has_error()) {
fd_set fds;
FD_ZERO(&fds);
2016-12-26 08:40:15 +00:00
FD_SET(fd, &fds);
2016-12-21 13:55:19 +00:00
2016-12-26 08:40:15 +00:00
if (!select(fd + 1, &fds, nullptr, nullptr, nullptr)) {
2016-12-21 13:55:19 +00:00
continue;
2016-12-21 22:22:02 +00:00
} else if ((evt = shared_ptr<xcb_generic_event_t>(xcb_poll_for_event(*this), free)) == nullptr) {
2016-12-21 13:55:19 +00:00
continue;
} else if (evt->response_type != ResponseType) {
continue;
2016-12-21 22:22:02 +00:00
} else if (check_event(reinterpret_cast<const Event*>(&*evt))) {
2016-12-21 13:55:19 +00:00
break;
}
}
}
2016-11-02 19:22:45 +00:00
template <typename Sink>
void attach_sink(Sink&& sink, registry::priority prio = 0) {
m_registry.attach(prio, forward<Sink>(sink));
}
template <typename Sink>
void detach_sink(Sink&& sink, registry::priority prio = 0) {
m_registry.detach(prio, forward<Sink>(sink));
}
protected:
registry m_registry{*this};
2016-12-03 15:44:08 +00:00
xcb_screen_t* m_screen{nullptr};
2016-11-02 19:22:45 +00:00
};
2016-11-19 05:22:44 +00:00
POLYBAR_NS_END