From 7acd4c703c2aabc36583fcaf3662929b0f0ef2ae Mon Sep 17 00:00:00 2001
From: patrick96
Date: Sun, 6 Mar 2022 21:51:07 +0100
Subject: [PATCH] xcb: Cleanup value packing
---
include/x11/connection.hpp | 5 +----
include/x11/winspec.hpp | 4 ++--
src/components/renderer.cpp | 6 +++---
src/x11/background_manager.cpp | 11 ++++++++---
src/x11/connection.cpp | 31 ++++++++++++++++++-------------
src/x11/tray_client.cpp | 6 +++---
src/x11/tray_manager.cpp | 12 ++++++------
src/x11/window.cpp | 12 ++++++------
src/x11/winspec.cpp | 9 +++++----
9 files changed, 52 insertions(+), 44 deletions(-)
diff --git a/include/x11/connection.hpp b/include/x11/connection.hpp
index 4147920f..7030dd00 100644
--- a/include/x11/connection.hpp
+++ b/include/x11/connection.hpp
@@ -110,10 +110,7 @@ class connection : public detail::connection_base& dest);
xcb_screen_t* screen(bool realloc = false);
diff --git a/include/x11/winspec.hpp b/include/x11/winspec.hpp
index 281e8f51..51451f67 100644
--- a/include/x11/winspec.hpp
+++ b/include/x11/winspec.hpp
@@ -1,6 +1,6 @@
#pragma once
-#include
+#include
#include "common.hpp"
#include "components/types.hpp"
@@ -173,7 +173,7 @@ class winspec {
unsigned short int m_width{1U};
unsigned short int m_height{1U};
unsigned short int m_border{0};
- unsigned int m_mask{0};
+ uint32_t m_mask{0};
xcb_params_cw_t m_params{};
};
diff --git a/src/components/renderer.cpp b/src/components/renderer.cpp
index 4e1b4504..e184a6fa 100644
--- a/src/components/renderer.cpp
+++ b/src/components/renderer.cpp
@@ -98,14 +98,14 @@ renderer::renderer(connection& conn, signal_emitter& sig, const config& conf, co
m_log.trace("renderer: Allocate graphic contexts");
{
- unsigned int mask{0};
- unsigned int value_list[32]{0};
+ uint32_t mask{0};
+ std::array value_list{};
xcb_params_gc_t params{};
XCB_AUX_ADD_PARAM(&mask, ¶ms, foreground, m_bar.foreground);
XCB_AUX_ADD_PARAM(&mask, ¶ms, graphics_exposures, 0);
connection::pack_values(mask, ¶ms, value_list);
m_gcontext = m_connection.generate_id();
- m_connection.create_gc(m_gcontext, m_pixmap, mask, value_list);
+ m_connection.create_gc(m_gcontext, m_pixmap, mask, value_list.data());
}
m_log.trace("renderer: Allocate alignment blocks");
diff --git a/src/x11/background_manager.cpp b/src/x11/background_manager.cpp
index 8dc2b132..baf8f8c1 100644
--- a/src/x11/background_manager.cpp
+++ b/src/x11/background_manager.cpp
@@ -172,10 +172,15 @@ void bg_slice::allocate_resources(const logger& log, xcb_visualtype_t* visual) {
if(m_gcontext == XCB_NONE) {
log.trace("background_manager: Allocating graphics context");
auto black_pixel = m_connection.screen()->black_pixel;
- unsigned int mask = XCB_GC_GRAPHICS_EXPOSURES | XCB_GC_FOREGROUND | XCB_GC_BACKGROUND;
- unsigned int value_list[3] = {black_pixel, black_pixel, 0};
+ uint32_t mask = 0;
+ xcb_params_gc_t params{};
+ std::array value_list{};
+ XCB_AUX_ADD_PARAM(&mask, ¶ms, foreground, black_pixel);
+ XCB_AUX_ADD_PARAM(&mask, ¶ms, background, black_pixel);
+ XCB_AUX_ADD_PARAM(&mask, ¶ms, graphics_exposures, 0);
+ m_connection.pack_values(mask, ¶ms, value_list);
m_gcontext = m_connection.generate_id();
- m_connection.create_gc(m_gcontext, m_pixmap, mask, value_list);
+ m_connection.create_gc(m_gcontext, m_pixmap, mask, value_list.data());
}
if(!m_surface) {
diff --git a/src/x11/connection.cpp b/src/x11/connection.cpp
index 4acf442e..82482b93 100644
--- a/src/x11/connection.cpp
+++ b/src/x11/connection.cpp
@@ -50,22 +50,27 @@ connection::~connection() {
disconnect();
}
-void connection::pack_values(unsigned int mask, const unsigned int* src, unsigned int* dest) {
- for (; mask; mask >>= 1, src++) {
- if (mask & 1) {
- *dest++ = *src;
+/**
+ * Packs data in src into the dest array.
+ *
+ * Each value in src is transferred into dest, if the corresponding bit in the
+ * mask is set.
+ *
+ * Required if parameters were set using XCB_AUX_ADD_PARAM but a value_list is needed in the function call.
+ *
+ * @param mask bitmask specifying which entries in src are selected
+ * @param src Array of 32-bit integers. Must have at least as many entries as the highest bit set in mask
+ * @param dest Entries from src are packed into this array
+ */
+void connection::pack_values(uint32_t mask, const void* src, std::array& dest) {
+ size_t dest_i = 0;
+ for (size_t i = 0; i < dest.size() && mask; i++, mask >>= 1) {
+ if (mask & 0x1) {
+ dest[dest_i] = reinterpret_cast(src)[i];
+ dest_i++;
}
}
}
-void connection::pack_values(unsigned int mask, const xcb_params_cw_t* src, unsigned int* dest) {
- pack_values(mask, reinterpret_cast(src), dest);
-}
-void connection::pack_values(unsigned int mask, const xcb_params_gc_t* src, unsigned int* dest) {
- pack_values(mask, reinterpret_cast(src), dest);
-}
-void connection::pack_values(unsigned int mask, const xcb_params_configure_window_t* src, unsigned int* dest) {
- pack_values(mask, reinterpret_cast(src), dest);
-}
/**
* Create X window id string
diff --git a/src/x11/tray_client.cpp b/src/x11/tray_client.cpp
index 89fdf638..c7f45253 100644
--- a/src/x11/tray_client.cpp
+++ b/src/x11/tray_client.cpp
@@ -87,8 +87,8 @@ void tray_client::ensure_state() const {
* Configure window size
*/
void tray_client::reconfigure(int x, int y) const {
- unsigned int configure_mask = 0;
- unsigned int configure_values[7];
+ uint32_t configure_mask = 0;
+ std::array configure_values{};
xcb_params_configure_window_t configure_params{};
XCB_AUX_ADD_PARAM(&configure_mask, &configure_params, width, m_size.w);
@@ -97,7 +97,7 @@ void tray_client::reconfigure(int x, int y) const {
XCB_AUX_ADD_PARAM(&configure_mask, &configure_params, y, y);
connection::pack_values(configure_mask, &configure_params, configure_values);
- m_connection.configure_window_checked(window(), configure_mask, configure_values);
+ m_connection.configure_window_checked(window(), configure_mask, configure_values.data());
}
/**
diff --git a/src/x11/tray_manager.cpp b/src/x11/tray_manager.cpp
index e181daae..60007ccf 100644
--- a/src/x11/tray_manager.cpp
+++ b/src/x11/tray_manager.cpp
@@ -336,14 +336,14 @@ void tray_manager::reconfigure_window() {
auto x = calculate_x(width);
m_log.trace("tray: New window values, width=%d, x=%d", width, x);
- unsigned int mask = 0;
- unsigned int values[7];
+ uint32_t mask = 0;
+ std::array values{};
xcb_params_configure_window_t params{};
XCB_AUX_ADD_PARAM(&mask, ¶ms, width, width);
XCB_AUX_ADD_PARAM(&mask, ¶ms, x, x);
connection::pack_values(mask, ¶ms, values);
- m_connection.configure_window_checked(m_tray, mask, values);
+ m_connection.configure_window_checked(m_tray, mask, values.data());
}
}
@@ -527,12 +527,12 @@ void tray_manager::create_bg() {
if (!m_gc) {
try {
xcb_params_gc_t params{};
- unsigned int mask = 0;
- unsigned int values[32];
+ uint32_t mask = 0;
+ std::array values{};
XCB_AUX_ADD_PARAM(&mask, ¶ms, graphics_exposures, 1);
connection::pack_values(mask, ¶ms, values);
m_gc = m_connection.generate_id();
- m_connection.create_gc_checked(m_gc, m_pixmap, mask, values);
+ m_connection.create_gc_checked(m_gc, m_pixmap, mask, values.data());
} catch (const exception& err) {
return m_log.err("Failed to create gcontext for tray background (err: %s)", err.what());
}
diff --git a/src/x11/window.cpp b/src/x11/window.cpp
index 0c31505a..5eb0dff0 100644
--- a/src/x11/window.cpp
+++ b/src/x11/window.cpp
@@ -12,8 +12,8 @@ POLYBAR_NS
* Reconfigure the window geometry
*/
window window::reconfigure_geom(unsigned short int w, unsigned short int h, short int x, short int y) {
- unsigned int mask{0};
- unsigned int values[7]{0};
+ uint32_t mask{0};
+ std::array values{};
xcb_params_configure_window_t params{};
XCB_AUX_ADD_PARAM(&mask, ¶ms, width, w);
@@ -22,7 +22,7 @@ window window::reconfigure_geom(unsigned short int w, unsigned short int h, shor
XCB_AUX_ADD_PARAM(&mask, ¶ms, y, y);
connection::pack_values(mask, ¶ms, values);
- configure_checked(mask, values);
+ configure_checked(mask, values.data());
return *this;
}
@@ -31,15 +31,15 @@ window window::reconfigure_geom(unsigned short int w, unsigned short int h, shor
* Reconfigure the window position
*/
window window::reconfigure_pos(short int x, short int y) {
- unsigned int mask{0};
- unsigned int values[2]{0};
+ uint32_t mask{0};
+ std::array values{};
xcb_params_configure_window_t params{};
XCB_AUX_ADD_PARAM(&mask, ¶ms, x, x);
XCB_AUX_ADD_PARAM(&mask, ¶ms, y, y);
connection::pack_values(mask, ¶ms, values);
- configure_checked(mask, values);
+ configure_checked(mask, values.data());
return *this;
}
diff --git a/src/x11/winspec.cpp b/src/x11/winspec.cpp
index fb921252..92e57ba2 100644
--- a/src/x11/winspec.cpp
+++ b/src/x11/winspec.cpp
@@ -1,6 +1,7 @@
-#include "x11/connection.hpp"
#include "x11/winspec.hpp"
+#include "x11/connection.hpp"
+
POLYBAR_NS
winspec::winspec(connection& conn) : m_connection(conn) {}
@@ -14,7 +15,7 @@ winspec::operator xcb_rectangle_t() const {
}
xcb_window_t winspec::operator<<(const cw_flush& f) {
- unsigned int values[16]{0};
+ std::array values{};
if (m_window == XCB_NONE) {
m_window = m_connection.generate_id();
@@ -34,10 +35,10 @@ xcb_window_t winspec::operator<<(const cw_flush& f) {
if (f.checked) {
m_connection.create_window_checked(
- m_depth, m_window, m_parent, m_x, m_y, m_width, m_height, m_border, m_class, m_visual, m_mask, values);
+ m_depth, m_window, m_parent, m_x, m_y, m_width, m_height, m_border, m_class, m_visual, m_mask, values.data());
} else {
m_connection.create_window(
- m_depth, m_window, m_parent, m_x, m_y, m_width, m_height, m_border, m_class, m_visual, m_mask, values);
+ m_depth, m_window, m_parent, m_x, m_y, m_width, m_height, m_border, m_class, m_visual, m_mask, values.data());
}
return m_window;