From 185363056a57fb9dac8a33a71966583b2fbb188b Mon Sep 17 00:00:00 2001 From: Michael Carlberg Date: Wed, 21 Dec 2016 14:55:19 +0100 Subject: [PATCH] refactor(x11): Cleanup --- include/components/bar.hpp | 1 + include/components/types.hpp | 2 +- include/modules/xbacklight.hpp | 2 +- include/modules/xkeyboard.hpp | 2 +- include/utils/bspwm.hpp | 2 +- include/utils/i3.hpp | 2 +- include/x11/connection.hpp | 67 ++++++++++------------ include/x11/extensions.hpp | 22 ------- include/x11/extensions/all.hpp | 22 +++++++ include/x11/{ => extensions}/composite.hpp | 13 +++++ include/x11/{ => extensions}/damage.hpp | 13 +++++ include/x11/{ => extensions}/randr.hpp | 2 + include/x11/{ => extensions}/render.hpp | 0 include/x11/{ => extensions}/sync.hpp | 13 +++++ include/x11/{ => extensions}/xkb.hpp | 2 + include/x11/extensions_fwd.hpp | 36 ------------ include/x11/registry.hpp | 2 +- include/x11/window.hpp | 2 +- include/x11/xutils.hpp | 2 - src/CMakeLists.txt | 12 ++-- src/components/controller.cpp | 17 +++--- src/components/screen.cpp | 2 +- src/main.cpp | 2 +- src/x11/connection.cpp | 66 +++++++-------------- src/x11/extensions/composite.cpp | 20 +++++++ src/x11/extensions/damage.cpp | 20 +++++++ src/x11/{ => extensions}/randr.cpp | 13 ++++- src/x11/extensions/render.cpp | 20 +++++++ src/x11/extensions/sync.cpp | 20 +++++++ src/x11/{ => extensions}/xkb.cpp | 13 ++++- src/x11/registry.cpp | 1 - 31 files changed, 244 insertions(+), 169 deletions(-) delete mode 100644 include/x11/extensions.hpp create mode 100644 include/x11/extensions/all.hpp rename include/x11/{ => extensions}/composite.hpp (54%) rename include/x11/{ => extensions}/damage.hpp (53%) rename include/x11/{ => extensions}/randr.hpp (96%) rename include/x11/{ => extensions}/render.hpp (100%) rename include/x11/{ => extensions}/sync.hpp (52%) rename include/x11/{ => extensions}/xkb.hpp (98%) delete mode 100644 include/x11/extensions_fwd.hpp create mode 100644 src/x11/extensions/composite.cpp create mode 100644 src/x11/extensions/damage.cpp rename src/x11/{ => extensions}/randr.cpp (92%) create mode 100644 src/x11/extensions/render.cpp create mode 100644 src/x11/extensions/sync.cpp rename src/x11/{ => extensions}/xkb.cpp (94%) diff --git a/include/components/bar.hpp b/include/components/bar.hpp index aacd029a..07722081 100644 --- a/include/components/bar.hpp +++ b/include/components/bar.hpp @@ -18,6 +18,7 @@ namespace chrono = std::chrono; using namespace std::chrono_literals; // fwd +class config; class connection; class logger; class parser; diff --git a/include/components/types.hpp b/include/components/types.hpp index 9219deb7..affafa4d 100644 --- a/include/components/types.hpp +++ b/include/components/types.hpp @@ -4,7 +4,7 @@ #include "common.hpp" #include "x11/color.hpp" -#include "x11/randr.hpp" +#include "x11/extensions/randr.hpp" POLYBAR_NS diff --git a/include/modules/xbacklight.hpp b/include/modules/xbacklight.hpp index 62845b24..05c75789 100644 --- a/include/modules/xbacklight.hpp +++ b/include/modules/xbacklight.hpp @@ -4,7 +4,7 @@ #include "config.hpp" #include "modules/meta/input_handler.hpp" #include "modules/meta/static_module.hpp" -#include "x11/randr.hpp" +#include "x11/extensions/randr.hpp" POLYBAR_NS diff --git a/include/modules/xkeyboard.hpp b/include/modules/xkeyboard.hpp index 7a4d7d88..839665b7 100644 --- a/include/modules/xkeyboard.hpp +++ b/include/modules/xkeyboard.hpp @@ -7,7 +7,7 @@ #include "modules/meta/static_module.hpp" #include "x11/events.hpp" #include "x11/window.hpp" -#include "x11/xkb.hpp" +#include "x11/extensions/xkb.hpp" POLYBAR_NS diff --git a/include/utils/bspwm.hpp b/include/utils/bspwm.hpp index 69376420..89afa9a6 100644 --- a/include/utils/bspwm.hpp +++ b/include/utils/bspwm.hpp @@ -7,7 +7,7 @@ #include "config.hpp" #include "utils/socket.hpp" #include "utils/string.hpp" -#include "x11/randr.hpp" +#include "x11/extensions/randr.hpp" #include "x11/window.hpp" POLYBAR_NS diff --git a/include/utils/i3.hpp b/include/utils/i3.hpp index 49a85396..58e481b7 100644 --- a/include/utils/i3.hpp +++ b/include/utils/i3.hpp @@ -3,7 +3,7 @@ #include #include "common.hpp" -#include "x11/randr.hpp" +#include "x11/extensions/randr.hpp" POLYBAR_NS diff --git a/include/x11/connection.hpp b/include/x11/connection.hpp index 4f581698..62774cb5 100644 --- a/include/x11/connection.hpp +++ b/include/x11/connection.hpp @@ -3,14 +3,12 @@ #include #include #include -#include #include "common.hpp" -#include "utils/factory.hpp" -#include "x11/extensions.hpp" +#include "utils/file.hpp" +#include "x11/extensions/all.hpp" #include "x11/registry.hpp" #include "x11/types.hpp" -#include "x11/xutils.hpp" POLYBAR_NS @@ -19,44 +17,19 @@ using xpp_connection = xpp::connection; class connection : public xpp_connection { public: using make_type = connection&; - static make_type make(); + static make_type make(xcb_connection_t* conn = nullptr, int conn_fd = 0); explicit connection(xcb_connection_t* conn) : connection(conn, 0) {} + explicit connection(xcb_connection_t* conn, int connection_fd) - : xpp_connection(conn), m_connection_fd(connection_fd) {} + : xpp_connection(conn), m_connection_fd(file_util::make_file_descriptor(connection_fd)) {} connection& operator=(const connection&) { return *this; } - connection(const connection& o) = delete; - - virtual ~connection() {} - - template - void wait_for_response(function check_event) { - auto fd = get_file_descriptor(); - fd_set fds; - FD_ZERO(&fds); - FD_SET(fd, &fds); - - while (true) { - if (select(fd + 1, &fds, nullptr, nullptr, nullptr) > 0) { - shared_ptr evt; - - if ((evt = poll_for_event()) && evt->response_type == ResponseType) { - if (check_event(reinterpret_cast(*(evt.get())))) { - break; - } - } - } - - if (connection_has_error()) { - break; - } - } - } void preload_atoms(); + void query_extensions(); string id(xcb_window_t w) const; @@ -64,6 +37,7 @@ class connection : public xpp_connection { 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; @@ -75,10 +49,31 @@ class connection : public xpp_connection { static string error_str(int error_code); - void dispatch_event(const shared_ptr& evt) const; + void dispatch_event(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 = poll_for_event()) == nullptr) { + continue; + } else if (evt->response_type != ResponseType) { + continue; + } else if (check_event(reinterpret_cast(*(evt.get())))) { + break; + } + } + } /** - * Attach sink to the registry */ + * Attach sink to the registry + */ template void attach_sink(Sink&& sink, registry::priority prio = 0) { m_registry.attach(prio, forward(sink)); @@ -95,7 +90,7 @@ class connection : public xpp_connection { protected: registry m_registry{*this}; xcb_screen_t* m_screen{nullptr}; - int m_connection_fd{0}; + shared_ptr m_connection_fd; }; POLYBAR_NS_END diff --git a/include/x11/extensions.hpp b/include/x11/extensions.hpp deleted file mode 100644 index ac46b7c4..00000000 --- a/include/x11/extensions.hpp +++ /dev/null @@ -1,22 +0,0 @@ -#pragma once - -#include "config.hpp" - -#if WITH_XDAMAGE -#include "x11/damage.hpp" -#endif -#if WITH_XRENDER -#include "x11/render.hpp" -#endif -#if WITH_XRANDR -#include "x11/randr.hpp" -#endif -#if WITH_XSYNC -#include "x11/sync.hpp" -#endif -#if WITH_XCOMPOSITE -#include "x11/composite.hpp" -#endif -#if WITH_XKB -#include "x11/xkb.hpp" -#endif diff --git a/include/x11/extensions/all.hpp b/include/x11/extensions/all.hpp new file mode 100644 index 00000000..0d42bcda --- /dev/null +++ b/include/x11/extensions/all.hpp @@ -0,0 +1,22 @@ +#pragma once + +#include "config.hpp" + +#if WITH_XDAMAGE +#include "x11/extensions/damage.hpp" +#endif +#if WITH_XRENDER +#include "x11/extensions/render.hpp" +#endif +#if WITH_XRANDR +#include "x11/extensions/randr.hpp" +#endif +#if WITH_XSYNC +#include "x11/extensions/sync.hpp" +#endif +#if WITH_XCOMPOSITE +#include "x11/extensions/composite.hpp" +#endif +#if WITH_XKB +#include "x11/extensions/xkb.hpp" +#endif diff --git a/include/x11/composite.hpp b/include/x11/extensions/composite.hpp similarity index 54% rename from include/x11/composite.hpp rename to include/x11/extensions/composite.hpp index a4de96fe..d44e7cc7 100644 --- a/include/x11/composite.hpp +++ b/include/x11/extensions/composite.hpp @@ -8,3 +8,16 @@ #include #include + +#include "common.hpp" + +POLYBAR_NS + +// fwd +class connection; + +namespace composite_util { + void query_extension(connection& conn); +} + +POLYBAR_NS_END diff --git a/include/x11/damage.hpp b/include/x11/extensions/damage.hpp similarity index 53% rename from include/x11/damage.hpp rename to include/x11/extensions/damage.hpp index 2ae5cbfb..c0c654ea 100644 --- a/include/x11/damage.hpp +++ b/include/x11/extensions/damage.hpp @@ -8,3 +8,16 @@ #include #include + +#include "common.hpp" + +POLYBAR_NS + +// fwd +class connection; + +namespace damage_util { + void query_extension(connection& conn); +} + +POLYBAR_NS_END diff --git a/include/x11/randr.hpp b/include/x11/extensions/randr.hpp similarity index 96% rename from include/x11/randr.hpp rename to include/x11/extensions/randr.hpp index 73323749..51e10b4a 100644 --- a/include/x11/randr.hpp +++ b/include/x11/extensions/randr.hpp @@ -45,6 +45,8 @@ struct randr_output { using monitor_t = shared_ptr; namespace randr_util { + void query_extension(connection& conn); + monitor_t make_monitor(xcb_randr_output_t randr, string name, uint16_t w, uint16_t h, int16_t x, int16_t y); vector get_monitors(connection& conn, xcb_window_t root, bool connected_only = false); diff --git a/include/x11/render.hpp b/include/x11/extensions/render.hpp similarity index 100% rename from include/x11/render.hpp rename to include/x11/extensions/render.hpp diff --git a/include/x11/sync.hpp b/include/x11/extensions/sync.hpp similarity index 52% rename from include/x11/sync.hpp rename to include/x11/extensions/sync.hpp index e24a2784..3a8e8697 100644 --- a/include/x11/sync.hpp +++ b/include/x11/extensions/sync.hpp @@ -8,3 +8,16 @@ #include #include + +#include "common.hpp" + +POLYBAR_NS + +// fwd +class connection; + +namespace sync_util { + void query_extension(connection& conn); +} + +POLYBAR_NS_END diff --git a/include/x11/xkb.hpp b/include/x11/extensions/xkb.hpp similarity index 98% rename from include/x11/xkb.hpp rename to include/x11/extensions/xkb.hpp index d53858da..e8b866ee 100644 --- a/include/x11/xkb.hpp +++ b/include/x11/extensions/xkb.hpp @@ -83,6 +83,8 @@ class keyboard { namespace xkb_util { static constexpr const char* LAYOUT_SYMBOL_BLACKLIST{";group;inet;pc;"}; + void query_extension(connection& conn); + void switch_layout(connection& conn, xcb_xkb_device_spec_t device, uint8_t index); uint8_t get_current_group(connection& conn, xcb_xkb_device_spec_t device); vector get_layouts(connection& conn, xcb_xkb_device_spec_t device); diff --git a/include/x11/extensions_fwd.hpp b/include/x11/extensions_fwd.hpp deleted file mode 100644 index 8ce408fa..00000000 --- a/include/x11/extensions_fwd.hpp +++ /dev/null @@ -1,36 +0,0 @@ -#pragma once - -#include "common.hpp" - -namespace xpp { -#if WITH_XDAMAGE - namespace damage { - class extension; - } -#endif -#if WITH_XRANDR - namespace randr { - class extension; - } -#endif -#if WITH_XSYNC - namespace sync { - class extension; - } -#endif -#if WITH_XRENDER - namespace render { - class extension; - } -#endif -#if WITH_XCOMPOSITE - namespace composite { - class extension; - } -#endif -#if WITH_XKB - namespace xkb { - class extension; - } -#endif -} diff --git a/include/x11/registry.hpp b/include/x11/registry.hpp index 92a49107..6a707d2f 100644 --- a/include/x11/registry.hpp +++ b/include/x11/registry.hpp @@ -3,7 +3,7 @@ #include #include "common.hpp" -#include "x11/extensions_fwd.hpp" +#include "x11/extensions/all.hpp" // fwd namespace xpp { diff --git a/include/x11/window.hpp b/include/x11/window.hpp index d3d96a1b..a5e0eafa 100644 --- a/include/x11/window.hpp +++ b/include/x11/window.hpp @@ -5,7 +5,7 @@ #include "common.hpp" #include "x11/connection.hpp" -#include "x11/randr.hpp" +#include "x11/extensions/randr.hpp" POLYBAR_NS diff --git a/include/x11/xutils.hpp b/include/x11/xutils.hpp index 5f495a3d..a86108ed 100644 --- a/include/x11/xutils.hpp +++ b/include/x11/xutils.hpp @@ -4,8 +4,6 @@ #include #include "common.hpp" -#include "utils/memory.hpp" -#include "x11/randr.hpp" POLYBAR_NS diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 34e24462..15a02a4a 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -28,37 +28,37 @@ if(WITH_XRANDR) set(XCB_PROTOS "${XCB_PROTOS}" randr) set(XPP_EXTENSION_LIST ${XPP_EXTENSION_LIST} xpp::randr::extension) else() - list(REMOVE_ITEM SOURCES x11/randr.cpp) + list(REMOVE_ITEM SOURCES x11/extensions/randr.cpp) endif() if(WITH_XRENDER) set(XCB_PROTOS "${XCB_PROTOS}" render) set(XPP_EXTENSION_LIST ${XPP_EXTENSION_LIST} xpp::render::extension) else() - list(REMOVE_ITEM SOURCES x11/render.cpp) + list(REMOVE_ITEM SOURCES x11/extensions/render.cpp) endif() if(WITH_XDAMAGE) set(XCB_PROTOS "${XCB_PROTOS}" damage) set(XPP_EXTENSION_LIST ${XPP_EXTENSION_LIST} xpp::damage::extension) else() - list(REMOVE_ITEM SOURCES x11/damage.cpp) + list(REMOVE_ITEM SOURCES x11/extensions/damage.cpp) endif() if(WITH_XSYNC) set(XCB_PROTOS "${XCB_PROTOS}" sync) set(XPP_EXTENSION_LIST ${XPP_EXTENSION_LIST} xpp::sync::extension) else() - list(REMOVE_ITEM SOURCES x11/sync.cpp) + list(REMOVE_ITEM SOURCES x11/extensions/sync.cpp) endif() if(WITH_XCOMPOSITE) set(XCB_PROTOS "${XCB_PROTOS}" composite) set(XPP_EXTENSION_LIST ${XPP_EXTENSION_LIST} xpp::composite::extension) else() - list(REMOVE_ITEM SOURCES x11/composite.cpp) + list(REMOVE_ITEM SOURCES x11/extensions/composite.cpp) endif() if(WITH_XKB) set(XCB_PROTOS "${XCB_PROTOS}" xkb) set(XPP_EXTENSION_LIST ${XPP_EXTENSION_LIST} xpp::xkb::extension) else() - list(REMOVE_ITEM SOURCES x11/xkb.cpp modules/xkeyboard.cpp) + list(REMOVE_ITEM SOURCES x11/extensions/xkb.cpp modules/xkeyboard.cpp) endif() string(REPLACE ";" ", " XPP_EXTENSION_LIST "${XPP_EXTENSION_LIST}") diff --git a/src/components/controller.cpp b/src/components/controller.cpp index 7286f127..e91b22e7 100644 --- a/src/components/controller.cpp +++ b/src/components/controller.cpp @@ -273,22 +273,19 @@ void controller::read_events() { // Process event on the xcb connection fd if (fd_connection && FD_ISSET(fd_connection, &readfds)) { - shared_ptr evt; - while ((evt = m_connection.poll_for_event())) { - try { - m_connection.dispatch_event(evt); - } catch (xpp::connection_error& err) { - m_log.err("X connection error, terminating... (what: %s)", m_connection.error_str(err.code())); - } catch (const exception& err) { - m_log.err("Error in X event loop: %s", err.what()); - } + try { + m_connection.dispatch_event(m_connection.wait_for_event()); + } catch (xpp::connection_error& err) { + m_log.err("X connection error, terminating... (what: %s)", m_connection.error_str(err.code())); + } catch (const exception& err) { + m_log.err("Error in X event loop: %s", err.what()); } } // Process event on the ipc fd if (fd_ipc && FD_ISSET(fd_ipc, &readfds)) { m_ipc->receive_message(); - fds.erase(std::remove_if(fds.begin(), fds.end(), [fd_ipc](int fd) { return fd == fd_ipc; })); + fds.erase(std::remove_if(fds.begin(), fds.end(), [fd_ipc](int fd) { return fd == fd_ipc; }), fds.end()); fds.emplace_back((fd_ipc = m_ipc->get_file_descriptor())); } } diff --git a/src/components/screen.cpp b/src/components/screen.cpp index 3baa6e8a..04a3316f 100644 --- a/src/components/screen.cpp +++ b/src/components/screen.cpp @@ -8,7 +8,7 @@ #include "events/signal.hpp" #include "events/signal_emitter.hpp" #include "x11/connection.hpp" -#include "x11/randr.hpp" +#include "x11/extensions/randr.hpp" #include "x11/winspec.hpp" POLYBAR_NS diff --git a/src/main.cpp b/src/main.cpp index 57b7f1f5..e9fb1fb5 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -54,7 +54,7 @@ int main(int argc, char** argv) { return EXIT_FAILURE; } - connection conn{xcbconn.get()}; + connection& conn{connection::make(xcbconn.get())}; conn.preload_atoms(); conn.query_extensions(); diff --git a/src/x11/connection.cpp b/src/x11/connection.cpp index d188c2f9..0174c8f0 100644 --- a/src/x11/connection.cpp +++ b/src/x11/connection.cpp @@ -1,9 +1,11 @@ -#include "x11/connection.hpp" +#include + #include "errors.hpp" #include "utils/factory.hpp" #include "utils/memory.hpp" #include "utils/string.hpp" #include "x11/atoms.hpp" +#include "x11/connection.hpp" #include "x11/xutils.hpp" POLYBAR_NS @@ -11,21 +13,21 @@ POLYBAR_NS /** * Create instance */ -connection::make_type connection::make() { - return static_cast(*factory_util::singleton>( - xutils::get_connection().get(), xutils::get_connection_fd())); +connection::make_type connection::make(xcb_connection_t* conn, int conn_fd) { + if (conn == nullptr) { + conn = xutils::get_connection().get(); + } + if (conn_fd == 0) { + conn_fd = xutils::get_connection_fd(); + } + return static_cast( + *factory_util::singleton>(conn, conn_fd)); } /** * Preload required xcb atoms */ void connection::preload_atoms() { - static bool s_atoms_loaded{false}; - - if (s_atoms_loaded) { - return; - } - vector cookies(memory_util::countof(ATOMS)); xcb_intern_atom_reply_t* reply{nullptr}; @@ -40,56 +42,30 @@ void connection::preload_atoms() { free(reply); } - - s_atoms_loaded = true; } /** - * Check if required X extensions are available + * Query for X extensions */ void connection::query_extensions() { - static bool s_extensions_loaded{false}; - - if (s_extensions_loaded) { - return; - } - #if WITH_XDAMAGE - damage().query_version(XCB_DAMAGE_MAJOR_VERSION, XCB_DAMAGE_MINOR_VERSION); - if (!extension()->present) - throw application_error("Missing X extension: Damage"); + damage_util::query_extension(*this); #endif #if WITH_XRENDER - render().query_version(XCB_RENDER_MAJOR_VERSION, XCB_RENDER_MINOR_VERSION); - if (!extension()->present) - throw application_error("Missing X extension: Render"); + render_util::query_extension(*this); #endif #if WITH_XRANDR - randr().query_version(XCB_RANDR_MAJOR_VERSION, XCB_RANDR_MINOR_VERSION); - if (!extension()->present) { - throw application_error("Missing X extension: RandR"); - } + randr_util::query_extension(*this); #endif #if WITH_XSYNC - sync().initialize(XCB_SYNC_MAJOR_VERSION, XCB_SYNC_MINOR_VERSION); - if (!extension()->present) { - throw application_error("Missing X extension: Sync"); - } + sync_util::query_extension(*this); #endif #if WITH_XCOMPOSITE - composite().query_version(XCB_COMPOSITE_MAJOR_VERSION, XCB_COMPOSITE_MINOR_VERSION); - if (!extension()->present) { - throw application_error("Missing X extension: Composite"); - } + composite_util::query_extension(*this); #endif #if WITH_XKB - xkb().use_extension(XCB_XKB_MAJOR_VERSION, XCB_XKB_MINOR_VERSION); - if (!extension()->present) { - throw application_error("Missing X extension: Xkb"); - } + xkb_util::query_extension(*this); #endif - - s_extensions_loaded = true; } /** @@ -208,8 +184,8 @@ string connection::error_str(int error_code) { /** * Dispatch event through the registry */ -void connection::dispatch_event(const shared_ptr& evt) const { - m_registry.dispatch(evt); +void connection::dispatch_event(shared_ptr&& evt) const { + m_registry.dispatch(forward(evt)); } POLYBAR_NS_END diff --git a/src/x11/extensions/composite.cpp b/src/x11/extensions/composite.cpp new file mode 100644 index 00000000..d008ee55 --- /dev/null +++ b/src/x11/extensions/composite.cpp @@ -0,0 +1,20 @@ +#include "x11/extensions/composite.hpp" +#include "errors.hpp" +#include "x11/connection.hpp" + +POLYBAR_NS + +namespace composite_util { + /** + * Query for the XCOMPOSITE extension + */ + void query_extension(connection& conn) { + conn.composite().query_version(XCB_COMPOSITE_MAJOR_VERSION, XCB_COMPOSITE_MINOR_VERSION); + + if (!conn.extension()->present) { + throw application_error("Missing X extension: Composite"); + } + } +} + +POLYBAR_NS_END diff --git a/src/x11/extensions/damage.cpp b/src/x11/extensions/damage.cpp new file mode 100644 index 00000000..ab93d25d --- /dev/null +++ b/src/x11/extensions/damage.cpp @@ -0,0 +1,20 @@ +#include "x11/extensions/damage.hpp" +#include "errors.hpp" +#include "x11/connection.hpp" + +POLYBAR_NS + +namespace damage_util { + /** + * Query for the XDAMAGE extension + */ + void query_extension(connection& conn) { + conn.damage().query_version(XCB_DAMAGE_MAJOR_VERSION, XCB_DAMAGE_MINOR_VERSION); + + if (!conn.extension()->present) { + throw application_error("Missing X extension: Damage"); + } + } +} + +POLYBAR_NS_END diff --git a/src/x11/randr.cpp b/src/x11/extensions/randr.cpp similarity index 92% rename from src/x11/randr.cpp rename to src/x11/extensions/randr.cpp index af2d4c44..6be0f32a 100644 --- a/src/x11/randr.cpp +++ b/src/x11/extensions/randr.cpp @@ -2,10 +2,11 @@ #include #include "components/types.hpp" +#include "errors.hpp" #include "utils/string.hpp" #include "x11/atoms.hpp" #include "x11/connection.hpp" -#include "x11/randr.hpp" +#include "x11/extensions/randr.hpp" POLYBAR_NS @@ -29,6 +30,16 @@ bool randr_output::match(const position& p) const { } namespace randr_util { + /** + * Query for the XRandR extension + */ + void query_extension(connection& conn) { + conn.randr().query_version(XCB_RANDR_MAJOR_VERSION, XCB_RANDR_MINOR_VERSION); + + if (!conn.extension()->present) { + throw application_error("Missing X extension: Randr"); + } + } /** * Define monitor diff --git a/src/x11/extensions/render.cpp b/src/x11/extensions/render.cpp new file mode 100644 index 00000000..730b3250 --- /dev/null +++ b/src/x11/extensions/render.cpp @@ -0,0 +1,20 @@ +#include "x11/extensions/render.hpp" +#include "errors.hpp" +#include "x11/connection.hpp" + +POLYBAR_NS + +namespace render_util { + /** + * Query for the XRENDER extension + */ + void query_extension(connection& conn) { + conn.render().query_version(XCB_RENDER_MAJOR_VERSION, XCB_RENDER_MINOR_VERSION); + + if (!conn.extension()->present) { + throw application_error("Missing X extension: Render"); + } + } +} + +POLYBAR_NS_END diff --git a/src/x11/extensions/sync.cpp b/src/x11/extensions/sync.cpp new file mode 100644 index 00000000..ad8116d1 --- /dev/null +++ b/src/x11/extensions/sync.cpp @@ -0,0 +1,20 @@ +#include "x11/extensions/sync.hpp" +#include "errors.hpp" +#include "x11/connection.hpp" + +POLYBAR_NS + +namespace sync_util { + /** + * Query for the XSYNC extension + */ + void query_extension(connection& conn) { + conn.sync().initialize(XCB_SYNC_MAJOR_VERSION, XCB_SYNC_MINOR_VERSION); + + if (!conn.extension()->present) { + throw application_error("Missing X extension: Sync"); + } + } +} + +POLYBAR_NS_END diff --git a/src/x11/xkb.cpp b/src/x11/extensions/xkb.cpp similarity index 94% rename from src/x11/xkb.cpp rename to src/x11/extensions/xkb.cpp index 8bd4a0b7..f06d6638 100644 --- a/src/x11/xkb.cpp +++ b/src/x11/extensions/xkb.cpp @@ -2,7 +2,7 @@ #include "errors.hpp" #include "utils/string.hpp" -#include "x11/xkb.hpp" +#include "x11/extensions/xkb.hpp" POLYBAR_NS @@ -75,6 +75,17 @@ size_t keyboard::size() const { } namespace xkb_util { + /** + * Query for the XKB extension + */ + void query_extension(connection& conn) { + conn.xkb().use_extension(XCB_XKB_MAJOR_VERSION, XCB_XKB_MINOR_VERSION); + + if (!conn.extension()->present) { + throw application_error("Missing X extension: XKb"); + } + } + /** * Get current group number */ diff --git a/src/x11/registry.cpp b/src/x11/registry.cpp index 262eaac3..30390ca8 100644 --- a/src/x11/registry.cpp +++ b/src/x11/registry.cpp @@ -1,5 +1,4 @@ #include "x11/connection.hpp" -#include "x11/extensions.hpp" #include "x11/registry.hpp"