refactor(x11): Cleanup

This commit is contained in:
Michael Carlberg 2016-12-21 14:55:19 +01:00
parent 788a7ca3a3
commit 185363056a
31 changed files with 244 additions and 169 deletions

View File

@ -18,6 +18,7 @@ namespace chrono = std::chrono;
using namespace std::chrono_literals;
// fwd
class config;
class connection;
class logger;
class parser;

View File

@ -4,7 +4,7 @@
#include "common.hpp"
#include "x11/color.hpp"
#include "x11/randr.hpp"
#include "x11/extensions/randr.hpp"
POLYBAR_NS

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -3,7 +3,7 @@
#include <i3ipc++/ipc.hpp>
#include "common.hpp"
#include "x11/randr.hpp"
#include "x11/extensions/randr.hpp"
POLYBAR_NS

View File

@ -3,14 +3,12 @@
#include <X11/X.h>
#include <X11/Xlib-xcb.h>
#include <xcb/xcb.h>
#include <iomanip>
#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<XPP_EXTENSION_LIST>;
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 <typename Event, uint32_t ResponseType>
void wait_for_response(function<bool(const Event&)> 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<xcb_generic_event_t> evt;
if ((evt = poll_for_event()) && evt->response_type == ResponseType) {
if (check_event(reinterpret_cast<const xcb_map_notify_event_t&>(*(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<xcb_client_message_event_t> 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<xcb_generic_event_t>& evt) const;
void dispatch_event(shared_ptr<xcb_generic_event_t>&& evt) const;
template <typename Event, uint32_t ResponseType>
void wait_for_response(function<bool(const Event&)> check_event) {
shared_ptr<xcb_generic_event_t> 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<const Event&>(*(evt.get())))) {
break;
}
}
}
/**
* Attach sink to the registry */
* Attach sink to the registry
*/
template <typename Sink>
void attach_sink(Sink&& sink, registry::priority prio = 0) {
m_registry.attach(prio, forward<Sink>(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<file_descriptor> m_connection_fd;
};
POLYBAR_NS_END

View File

@ -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

View File

@ -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

View File

@ -8,3 +8,16 @@
#include <xcb/composite.h>
#include <xpp/proto/composite.hpp>
#include "common.hpp"
POLYBAR_NS
// fwd
class connection;
namespace composite_util {
void query_extension(connection& conn);
}
POLYBAR_NS_END

View File

@ -8,3 +8,16 @@
#include <xcb/damage.h>
#include <xpp/proto/damage.hpp>
#include "common.hpp"
POLYBAR_NS
// fwd
class connection;
namespace damage_util {
void query_extension(connection& conn);
}
POLYBAR_NS_END

View File

@ -45,6 +45,8 @@ struct randr_output {
using monitor_t = shared_ptr<randr_output>;
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<monitor_t> get_monitors(connection& conn, xcb_window_t root, bool connected_only = false);

View File

@ -8,3 +8,16 @@
#include <xcb/sync.h>
#include <xpp/proto/sync.hpp>
#include "common.hpp"
POLYBAR_NS
// fwd
class connection;
namespace sync_util {
void query_extension(connection& conn);
}
POLYBAR_NS_END

View File

@ -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<keyboard::layout> get_layouts(connection& conn, xcb_xkb_device_spec_t device);

View File

@ -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
}

View File

@ -3,7 +3,7 @@
#include <xcb/xcb.h>
#include "common.hpp"
#include "x11/extensions_fwd.hpp"
#include "x11/extensions/all.hpp"
// fwd
namespace xpp {

View File

@ -5,7 +5,7 @@
#include "common.hpp"
#include "x11/connection.hpp"
#include "x11/randr.hpp"
#include "x11/extensions/randr.hpp"
POLYBAR_NS

View File

@ -4,8 +4,6 @@
#include <xcb/xcb_util.h>
#include "common.hpp"
#include "utils/memory.hpp"
#include "x11/randr.hpp"
POLYBAR_NS

View File

@ -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}")

View File

@ -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<xcb_generic_event_t> 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()));
}
}

View File

@ -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

View File

@ -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();

View File

@ -1,9 +1,11 @@
#include "x11/connection.hpp"
#include <iomanip>
#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<connection::make_type>(*factory_util::singleton<std::remove_reference_t<connection::make_type>>(
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<connection::make_type>(
*factory_util::singleton<std::remove_reference_t<connection::make_type>>(conn, conn_fd));
}
/**
* Preload required xcb atoms
*/
void connection::preload_atoms() {
static bool s_atoms_loaded{false};
if (s_atoms_loaded) {
return;
}
vector<xcb_intern_atom_cookie_t> 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<xpp::damage::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<xpp::render::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<xpp::randr::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<xpp::sync::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<xpp::composite::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<xpp::xkb::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<xcb_generic_event_t>& evt) const {
m_registry.dispatch(evt);
void connection::dispatch_event(shared_ptr<xcb_generic_event_t>&& evt) const {
m_registry.dispatch(forward<decltype(evt)>(evt));
}
POLYBAR_NS_END

View File

@ -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<xpp::composite::extension>()->present) {
throw application_error("Missing X extension: Composite");
}
}
}
POLYBAR_NS_END

View File

@ -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<xpp::damage::extension>()->present) {
throw application_error("Missing X extension: Damage");
}
}
}
POLYBAR_NS_END

View File

@ -2,10 +2,11 @@
#include <utility>
#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<xpp::randr::extension>()->present) {
throw application_error("Missing X extension: Randr");
}
}
/**
* Define monitor

View File

@ -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<xpp::render::extension>()->present) {
throw application_error("Missing X extension: Render");
}
}
}
POLYBAR_NS_END

View File

@ -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<xpp::sync::extension>()->present) {
throw application_error("Missing X extension: Sync");
}
}
}
POLYBAR_NS_END

View File

@ -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<xpp::xkb::extension>()->present) {
throw application_error("Missing X extension: XKb");
}
}
/**
* Get current group number
*/

View File

@ -1,5 +1,4 @@
#include "x11/connection.hpp"
#include "x11/extensions.hpp"
#include "x11/registry.hpp"