From b66f9203080047feb861aad39d22cbbf913c430d Mon Sep 17 00:00:00 2001
From: patrick96
Date: Sun, 6 Mar 2022 16:40:42 +0100
Subject: [PATCH] Cleanup xcb stuff
---
include/modules/xworkspaces.hpp | 4 +-
include/utils/memory.hpp | 28 +-----------
include/x11/atoms.hpp | 10 ++--
include/x11/connection.hpp | 10 ++--
include/x11/ewmh.hpp | 18 ++++++--
src/components/controller.cpp | 4 +-
src/modules/xwindow.cpp | 6 +--
src/modules/xworkspaces.cpp | 22 ++++-----
src/x11/atoms.cpp | 76 +++++++++++++++----------------
src/x11/connection.cpp | 57 +++++++++++------------
src/x11/ewmh.cpp | 29 ++++++++----
src/x11/tray_client.cpp | 24 +++++-----
src/x11/tray_manager.cpp | 6 +--
src/x11/xembed.cpp | 17 +++----
tests/CMakeLists.txt | 1 -
tests/unit_tests/utils/memory.cpp | 23 ----------
16 files changed, 150 insertions(+), 185 deletions(-)
delete mode 100644 tests/unit_tests/utils/memory.cpp
diff --git a/include/modules/xworkspaces.hpp b/include/modules/xworkspaces.hpp
index 72da2b62..364eae39 100644
--- a/include/modules/xworkspaces.hpp
+++ b/include/modules/xworkspaces.hpp
@@ -83,7 +83,7 @@ namespace modules {
static constexpr const char* TAG_LABEL_STATE{""};
connection& m_connection;
- ewmh_connection_t m_ewmh;
+ ewmh_util::ewmh_connection& m_ewmh;
vector m_monitors;
@@ -106,6 +106,6 @@ namespace modules {
bool m_revscroll{false};
size_t m_index{0};
};
-} // namespace modules
+} // namespace modules
POLYBAR_NS_END
diff --git a/include/utils/memory.hpp b/include/utils/memory.hpp
index d190f450..88f8a280 100644
--- a/include/utils/memory.hpp
+++ b/include/utils/memory.hpp
@@ -9,31 +9,7 @@ POLYBAR_NS
template
using malloc_ptr_t = shared_ptr;
-namespace memory_util {
- /**
- * Create a shared pointer using malloc/free
- *
- * Generates a noexcept-type warning because the mangled name for
- * malloc_ptr_t will change in C++17. This doesn't affect use because we have
- * no public ABI, so we ignore it here.
- * See also this SO answer: https://stackoverflow.com/a/46857525/5363071
- */
-
-#pragma GCC diagnostic push
-#pragma GCC diagnostic ignored "-Wnoexcept-type"
- template
- inline malloc_ptr_t make_malloc_ptr(Deleter deleter = std::free) {
- return malloc_ptr_t(static_cast(calloc(1, Size)), deleter);
- }
-#pragma GCC diagnostic pop
-
- /**
- * Get the number of elements in T
- */
- template
- inline auto countof(T& p) {
- return sizeof(p) / sizeof(p[0]);
- }
-} // namespace memory_util
+template
+using malloc_unique_ptr = unique_ptr;
POLYBAR_NS_END
diff --git a/include/x11/atoms.hpp b/include/x11/atoms.hpp
index 10611d84..ef42c1a6 100644
--- a/include/x11/atoms.hpp
+++ b/include/x11/atoms.hpp
@@ -2,13 +2,15 @@
#include
+#include
+#include
+
struct cached_atom {
- const char* name;
- size_t len;
- xcb_atom_t* atom;
+ const std::string name;
+ xcb_atom_t& atom;
};
-extern cached_atom ATOMS[36];
+extern std::array ATOMS;
extern xcb_atom_t _NET_SUPPORTED;
extern xcb_atom_t _NET_CURRENT_DESKTOP;
diff --git a/include/x11/connection.hpp b/include/x11/connection.hpp
index e28f2127..4147920f 100644
--- a/include/x11/connection.hpp
+++ b/include/x11/connection.hpp
@@ -66,7 +66,7 @@ namespace detail {
} catch (const shared_ptr& error) {
check(error);
}
- throw; // re-throw exception
+ throw; // re-throw exception
}
shared_ptr wait_for_special_event(xcb_special_event_t* se) const override {
@@ -75,7 +75,7 @@ namespace detail {
} catch (const shared_ptr& error) {
check(error);
}
- throw; // re-throw exception
+ throw; // re-throw exception
}
private:
@@ -94,7 +94,7 @@ namespace detail {
dispatcher(error);
}
};
-} // namespace detail
+} // namespace detail
class connection : public detail::connection_base {
public:
@@ -122,8 +122,8 @@ class connection : public detail::connection_base make_client_message(xcb_atom_t type, xcb_window_t target) const;
- void send_client_message(const shared_ptr& message, xcb_window_t target,
+ 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,
unsigned int event_mask = 0xFFFFFF, bool propagate = false) const;
xcb_visualtype_t* visual_type(xcb_screen_t* screen, int match_depth = 32);
diff --git a/include/x11/ewmh.hpp b/include/x11/ewmh.hpp
index e6fb1371..6088f18e 100644
--- a/include/x11/ewmh.hpp
+++ b/include/x11/ewmh.hpp
@@ -4,15 +4,25 @@
#include "common.hpp"
#include "utils/memory.hpp"
+#include "utils/mixins.hpp"
POLYBAR_NS
struct position;
-using ewmh_connection_t = malloc_ptr_t;
-
namespace ewmh_util {
- ewmh_connection_t initialize();
+ class ewmh_connection : public non_copyable_mixin, public non_movable_mixin {
+ public:
+ ewmh_connection();
+ ~ewmh_connection();
+
+ xcb_ewmh_connection_t* get();
+
+ private:
+ xcb_ewmh_connection_t c;
+ };
+
+ ewmh_connection& initialize();
bool supports(xcb_atom_t atom, int screen = 0);
@@ -42,6 +52,6 @@ namespace ewmh_util {
void set_wm_window_opacity(xcb_window_t win, unsigned long int values);
vector get_client_list(int screen = 0);
-}
+} // namespace ewmh_util
POLYBAR_NS_END
diff --git a/src/components/controller.cpp b/src/components/controller.cpp
index cb6651b6..54117f97 100644
--- a/src/components/controller.cpp
+++ b/src/components/controller.cpp
@@ -170,8 +170,8 @@ void controller::conn_cb() {
return;
}
- shared_ptr evt{};
- while ((evt = shared_ptr(xcb_poll_for_event(m_connection), free)) != nullptr) {
+ malloc_ptr_t evt{};
+ while ((evt = malloc_ptr_t(xcb_poll_for_event(m_connection), free)) != nullptr) {
try {
m_connection.dispatch_event(evt);
} catch (xpp::connection_error& err) {
diff --git a/src/modules/xwindow.cpp b/src/modules/xwindow.cpp
index 37c1014a..80383ce3 100644
--- a/src/modules/xwindow.cpp
+++ b/src/modules/xwindow.cpp
@@ -63,9 +63,7 @@ namespace modules {
xwindow_module::xwindow_module(const bar_settings& bar, string name_)
: static_module(bar, move(name_)), m_connection(connection::make()) {
// Initialize ewmh atoms
- if ((ewmh_util::initialize()) == nullptr) {
- throw module_error("Failed to initialize ewmh atoms");
- }
+ ewmh_util::initialize();
// Check if the WM supports _NET_ACTIVE_WINDOW
if (!ewmh_util::supports(_NET_ACTIVE_WINDOW)) {
@@ -133,6 +131,6 @@ namespace modules {
}
return false;
}
-} // namespace modules
+} // namespace modules
POLYBAR_NS_END
diff --git a/src/modules/xworkspaces.cpp b/src/modules/xworkspaces.cpp
index 57060e73..200eea5a 100644
--- a/src/modules/xworkspaces.cpp
+++ b/src/modules/xworkspaces.cpp
@@ -18,7 +18,7 @@ namespace {
inline bool operator==(const position& a, const position& b) {
return a.x + a.y == b.x + b.y;
}
-} // namespace
+} // namespace
/**
* Defines a lexicographical order on position
@@ -34,7 +34,9 @@ namespace modules {
* Construct module
*/
xworkspaces_module::xworkspaces_module(const bar_settings& bar, string name_)
- : static_module(bar, move(name_)), m_connection(connection::make()) {
+ : static_module(bar, move(name_))
+ , m_connection(connection::make())
+ , m_ewmh(ewmh_util::initialize()) {
m_router->register_action_with_data(EVENT_FOCUS, [this](const std::string& data) { action_focus(data); });
m_router->register_action(EVENT_NEXT, [this]() { action_next(); });
m_router->register_action(EVENT_PREV, [this]() { action_prev(); });
@@ -45,11 +47,6 @@ namespace modules {
m_scroll = m_conf.get(name(), "enable-scroll", m_scroll);
m_revscroll = m_conf.get(name(), "reverse-scroll", m_revscroll);
- // Initialize ewmh atoms
- if ((m_ewmh = ewmh_util::initialize()) == nullptr) {
- throw module_error("Failed to initialize ewmh atoms");
- }
-
// Add formats and elements
m_formatter->add(DEFAULT_FORMAT, TAG_LABEL_STATE, {TAG_LABEL_STATE, TAG_LABEL_MONITOR});
@@ -79,7 +76,8 @@ namespace modules {
if (vec.size() == 2) {
m_icons->add(vec[0], std::make_shared