xcb: Cleanup value packing

This commit is contained in:
patrick96 2022-03-06 21:51:07 +01:00
parent ba0e478026
commit 7acd4c703c
No known key found for this signature in database
GPG Key ID: 521E5E03AEBCA1A7
9 changed files with 52 additions and 44 deletions

View File

@ -110,10 +110,7 @@ class connection : public detail::connection_base<connection&, XPP_EXTENSION_LIS
return o; return o;
} }
static void pack_values(unsigned int mask, const unsigned int* src, unsigned int* dest); static void pack_values(uint32_t mask, const void* src, std::array<uint32_t, 32>& 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);
xcb_screen_t* screen(bool realloc = false); xcb_screen_t* screen(bool realloc = false);

View File

@ -1,6 +1,6 @@
#pragma once #pragma once
#include <xcb/xcb_aux.h> #include <xcb/xcb.h>
#include "common.hpp" #include "common.hpp"
#include "components/types.hpp" #include "components/types.hpp"
@ -173,7 +173,7 @@ class winspec {
unsigned short int m_width{1U}; unsigned short int m_width{1U};
unsigned short int m_height{1U}; unsigned short int m_height{1U};
unsigned short int m_border{0}; unsigned short int m_border{0};
unsigned int m_mask{0}; uint32_t m_mask{0};
xcb_params_cw_t m_params{}; xcb_params_cw_t m_params{};
}; };

View File

@ -98,14 +98,14 @@ renderer::renderer(connection& conn, signal_emitter& sig, const config& conf, co
m_log.trace("renderer: Allocate graphic contexts"); m_log.trace("renderer: Allocate graphic contexts");
{ {
unsigned int mask{0}; uint32_t mask{0};
unsigned int value_list[32]{0}; std::array<uint32_t, 32> value_list{};
xcb_params_gc_t params{}; xcb_params_gc_t params{};
XCB_AUX_ADD_PARAM(&mask, &params, foreground, m_bar.foreground); XCB_AUX_ADD_PARAM(&mask, &params, foreground, m_bar.foreground);
XCB_AUX_ADD_PARAM(&mask, &params, graphics_exposures, 0); XCB_AUX_ADD_PARAM(&mask, &params, graphics_exposures, 0);
connection::pack_values(mask, &params, value_list); connection::pack_values(mask, &params, value_list);
m_gcontext = m_connection.generate_id(); 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"); m_log.trace("renderer: Allocate alignment blocks");

View File

@ -172,10 +172,15 @@ void bg_slice::allocate_resources(const logger& log, xcb_visualtype_t* visual) {
if(m_gcontext == XCB_NONE) { if(m_gcontext == XCB_NONE) {
log.trace("background_manager: Allocating graphics context"); log.trace("background_manager: Allocating graphics context");
auto black_pixel = m_connection.screen()->black_pixel; auto black_pixel = m_connection.screen()->black_pixel;
unsigned int mask = XCB_GC_GRAPHICS_EXPOSURES | XCB_GC_FOREGROUND | XCB_GC_BACKGROUND; uint32_t mask = 0;
unsigned int value_list[3] = {black_pixel, black_pixel, 0}; xcb_params_gc_t params{};
std::array<uint32_t, 32> value_list{};
XCB_AUX_ADD_PARAM(&mask, &params, foreground, black_pixel);
XCB_AUX_ADD_PARAM(&mask, &params, background, black_pixel);
XCB_AUX_ADD_PARAM(&mask, &params, graphics_exposures, 0);
m_connection.pack_values(mask, &params, value_list);
m_gcontext = m_connection.generate_id(); 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) { if(!m_surface) {

View File

@ -50,22 +50,27 @@ connection::~connection() {
disconnect(); disconnect();
} }
void connection::pack_values(unsigned int mask, const unsigned int* src, unsigned int* dest) { /**
for (; mask; mask >>= 1, src++) { * Packs data in src into the dest array.
if (mask & 1) { *
*dest++ = *src; * 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<uint32_t, 32>& 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<const uint32_t*>(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<const unsigned int*>(src), dest);
}
void connection::pack_values(unsigned int mask, const xcb_params_gc_t* src, unsigned int* dest) {
pack_values(mask, reinterpret_cast<const unsigned int*>(src), dest);
}
void connection::pack_values(unsigned int mask, const xcb_params_configure_window_t* src, unsigned int* dest) {
pack_values(mask, reinterpret_cast<const unsigned int*>(src), dest);
}
/** /**
* Create X window id string * Create X window id string

View File

@ -87,8 +87,8 @@ void tray_client::ensure_state() const {
* Configure window size * Configure window size
*/ */
void tray_client::reconfigure(int x, int y) const { void tray_client::reconfigure(int x, int y) const {
unsigned int configure_mask = 0; uint32_t configure_mask = 0;
unsigned int configure_values[7]; std::array<uint32_t, 32> configure_values{};
xcb_params_configure_window_t configure_params{}; xcb_params_configure_window_t configure_params{};
XCB_AUX_ADD_PARAM(&configure_mask, &configure_params, width, m_size.w); 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); XCB_AUX_ADD_PARAM(&configure_mask, &configure_params, y, y);
connection::pack_values(configure_mask, &configure_params, configure_values); 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());
} }
/** /**

View File

@ -336,14 +336,14 @@ void tray_manager::reconfigure_window() {
auto x = calculate_x(width); auto x = calculate_x(width);
m_log.trace("tray: New window values, width=%d, x=%d", width, x); m_log.trace("tray: New window values, width=%d, x=%d", width, x);
unsigned int mask = 0; uint32_t mask = 0;
unsigned int values[7]; std::array<uint32_t, 32> values{};
xcb_params_configure_window_t params{}; xcb_params_configure_window_t params{};
XCB_AUX_ADD_PARAM(&mask, &params, width, width); XCB_AUX_ADD_PARAM(&mask, &params, width, width);
XCB_AUX_ADD_PARAM(&mask, &params, x, x); XCB_AUX_ADD_PARAM(&mask, &params, x, x);
connection::pack_values(mask, &params, values); connection::pack_values(mask, &params, 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) { if (!m_gc) {
try { try {
xcb_params_gc_t params{}; xcb_params_gc_t params{};
unsigned int mask = 0; uint32_t mask = 0;
unsigned int values[32]; std::array<uint32_t, 32> values{};
XCB_AUX_ADD_PARAM(&mask, &params, graphics_exposures, 1); XCB_AUX_ADD_PARAM(&mask, &params, graphics_exposures, 1);
connection::pack_values(mask, &params, values); connection::pack_values(mask, &params, values);
m_gc = m_connection.generate_id(); 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) { } catch (const exception& err) {
return m_log.err("Failed to create gcontext for tray background (err: %s)", err.what()); return m_log.err("Failed to create gcontext for tray background (err: %s)", err.what());
} }

View File

@ -12,8 +12,8 @@ POLYBAR_NS
* Reconfigure the window geometry * Reconfigure the window geometry
*/ */
window window::reconfigure_geom(unsigned short int w, unsigned short int h, short int x, short int y) { window window::reconfigure_geom(unsigned short int w, unsigned short int h, short int x, short int y) {
unsigned int mask{0}; uint32_t mask{0};
unsigned int values[7]{0}; std::array<uint32_t, 32> values{};
xcb_params_configure_window_t params{}; xcb_params_configure_window_t params{};
XCB_AUX_ADD_PARAM(&mask, &params, width, w); XCB_AUX_ADD_PARAM(&mask, &params, width, w);
@ -22,7 +22,7 @@ window window::reconfigure_geom(unsigned short int w, unsigned short int h, shor
XCB_AUX_ADD_PARAM(&mask, &params, y, y); XCB_AUX_ADD_PARAM(&mask, &params, y, y);
connection::pack_values(mask, &params, values); connection::pack_values(mask, &params, values);
configure_checked(mask, values); configure_checked(mask, values.data());
return *this; return *this;
} }
@ -31,15 +31,15 @@ window window::reconfigure_geom(unsigned short int w, unsigned short int h, shor
* Reconfigure the window position * Reconfigure the window position
*/ */
window window::reconfigure_pos(short int x, short int y) { window window::reconfigure_pos(short int x, short int y) {
unsigned int mask{0}; uint32_t mask{0};
unsigned int values[2]{0}; std::array<uint32_t, 32> values{};
xcb_params_configure_window_t params{}; xcb_params_configure_window_t params{};
XCB_AUX_ADD_PARAM(&mask, &params, x, x); XCB_AUX_ADD_PARAM(&mask, &params, x, x);
XCB_AUX_ADD_PARAM(&mask, &params, y, y); XCB_AUX_ADD_PARAM(&mask, &params, y, y);
connection::pack_values(mask, &params, values); connection::pack_values(mask, &params, values);
configure_checked(mask, values); configure_checked(mask, values.data());
return *this; return *this;
} }

View File

@ -1,6 +1,7 @@
#include "x11/connection.hpp"
#include "x11/winspec.hpp" #include "x11/winspec.hpp"
#include "x11/connection.hpp"
POLYBAR_NS POLYBAR_NS
winspec::winspec(connection& conn) : m_connection(conn) {} 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) { xcb_window_t winspec::operator<<(const cw_flush& f) {
unsigned int values[16]{0}; std::array<uint32_t, 32> values{};
if (m_window == XCB_NONE) { if (m_window == XCB_NONE) {
m_window = m_connection.generate_id(); m_window = m_connection.generate_id();
@ -34,10 +35,10 @@ xcb_window_t winspec::operator<<(const cw_flush& f) {
if (f.checked) { if (f.checked) {
m_connection.create_window_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 { } else {
m_connection.create_window( 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; return m_window;