From 210695d377ec798176df6cca6df20005bc6f6c65 Mon Sep 17 00:00:00 2001 From: Michael Carlberg Date: Sat, 31 Dec 2016 07:39:50 +0100 Subject: [PATCH] refactor(x11): Merge xlib/xutils with connection --- .valgrind-suppressions | 40 ++++++++++++++------- include/utils/color.hpp | 1 - include/x11/connection.hpp | 48 ++++++++++++++++--------- include/x11/fonts.hpp | 2 +- include/x11/window.hpp | 2 ++ include/x11/xlib.hpp | 30 ---------------- include/x11/xutils.hpp | 32 ----------------- src/components/bar.cpp | 3 +- src/components/renderer.cpp | 9 +++-- src/main.cpp | 11 ++---- src/modules/xbacklight.cpp | 1 - src/x11/color.cpp | 9 +---- src/x11/connection.cpp | 71 +++++++++++++++++++++++++------------ src/x11/ewmh.cpp | 5 ++- src/x11/fonts.cpp | 28 +++++++-------- src/x11/graphics.cpp | 3 +- src/x11/tray_client.cpp | 3 +- src/x11/tray_manager.cpp | 8 ++--- src/x11/window.cpp | 25 +++++++++---- src/x11/winspec.cpp | 3 +- src/x11/xlib.cpp | 44 ----------------------- src/x11/xresources.cpp | 4 +-- src/x11/xutils.cpp | 59 ------------------------------ 23 files changed, 163 insertions(+), 278 deletions(-) delete mode 100644 include/x11/xlib.hpp delete mode 100644 include/x11/xutils.hpp delete mode 100644 src/x11/xlib.cpp delete mode 100644 src/x11/xutils.cpp diff --git a/.valgrind-suppressions b/.valgrind-suppressions index 5d83a045..6d33fd43 100644 --- a/.valgrind-suppressions +++ b/.valgrind-suppressions @@ -20,13 +20,13 @@ Memcheck:Leak match-leak-kinds: definite fun:malloc - obj:/usr/lib/libfontconfig.so.1.9.2 - obj:/usr/lib/libfontconfig.so.1.9.2 + obj:/usr/lib/libfontconfig.so.* + obj:/usr/lib/libfontconfig.so.* fun:FcPatternAddDouble - obj:/usr/lib/libXft.so.2.3.2 - obj:/usr/lib/libXft.so.2.3.2 - obj:/usr/lib/libXft.so.2.3.2 - obj:/usr/lib/libXft.so.2.3.2 + obj:/usr/lib/libXft.so.* + obj:/usr/lib/libXft.so.* + obj:/usr/lib/libXft.so.* + obj:/usr/lib/libXft.so.* fun:XftDefaultHasRender fun:XftDefaultSubstitute fun:XftFontMatch @@ -54,8 +54,8 @@ Memcheck:Leak match-leak-kinds: definite fun:realloc - obj:/usr/lib/libfontconfig.so.1.9.2 - obj:/usr/lib/libfontconfig.so.1.9.2 + obj:/usr/lib/libfontconfig.so.* + obj:/usr/lib/libfontconfig.so.* fun:FcFontRenderPrepare fun:FcFontMatch fun:XftFontMatch @@ -71,15 +71,31 @@ Memcheck:Leak match-leak-kinds: definite fun:realloc - obj:/usr/lib/libX11.so.6.3.0 - obj:/usr/lib/libX11.so.6.3.0 - obj:/usr/lib/libX11.so.6.3.0 + obj:/usr/lib/libX11.so.* + obj:/usr/lib/libX11.so.* + obj:/usr/lib/libX11.so.* fun:_XlcCreateLC fun:_XlcDefaultLoader fun:_XOpenLC fun:_XrmInitParseInfo - obj:/usr/lib/libX11.so.6.3.0 + obj:/usr/lib/libX11.so.* fun:XrmGetStringDatabase ... ... } +{ + xft conditional jump + Memcheck:Cond + obj:/usr/lib/libfreetype.so.* + obj:/usr/lib/libfreetype.so.* + fun:FT_Outline_Decompose + obj:/usr/lib/libfreetype.so.* + obj:/usr/lib/libfreetype.so.* + obj:/usr/lib/libfreetype.so.* + obj:/usr/lib/libfreetype.so.* + obj:/usr/lib/libfreetype.so.* + fun:XftFontLoadGlyphs + fun:XftGlyphExtents + ... + ... +} diff --git a/include/utils/color.hpp b/include/utils/color.hpp index ab826b22..72090adf 100644 --- a/include/utils/color.hpp +++ b/include/utils/color.hpp @@ -4,7 +4,6 @@ #include "common.hpp" #include "utils/string.hpp" -#include "x11/xlib.hpp" POLYBAR_NS diff --git a/include/x11/connection.hpp b/include/x11/connection.hpp index f83cdc89..1d72258a 100644 --- a/include/x11/connection.hpp +++ b/include/x11/connection.hpp @@ -1,5 +1,8 @@ #pragma once +#include +#include +#include #include #include #include @@ -15,6 +18,19 @@ POLYBAR_NS namespace detail { + class displaylock { + public: + explicit displaylock(Display* display) : m_display(forward(display)) { + XLockDisplay(m_display); + } + ~displaylock() { + XUnlockDisplay(m_display); + } + + protected: + Display* m_display; + }; + template class interfaces : public xpp::x::extension::interface, Connection>, public Extensions::template interface, Connection>... { @@ -44,10 +60,6 @@ namespace detail { virtual ~connection_base() {} - const Derived& operator=(const Derived& o) { - return o; - } - void operator()(const shared_ptr& error) const { check(error); } @@ -59,7 +71,7 @@ namespace detail { template WindowType root() const { - using make = xpp::generic::factory::make; + using make = xpp::generic::factory::make; return make()(*this, m_root_window); } @@ -102,32 +114,32 @@ namespace detail { class connection : public detail::connection_base { public: using base_type = detail::connection_base; - using make_type = connection&; - static make_type make(xcb_connection_t* conn = nullptr); - template - explicit connection(Args&&... args) : base_type(forward(args)...) {} + using make_type = connection&; + static make_type make(Display* display = nullptr); + + explicit connection(Display* dsp); + ~connection(); const connection& operator=(const connection& o) { return o; } - // operator Display*() const; + static void pack_values(uint32_t mask, const uint32_t* src, uint32_t* dest); + static void pack_values(uint32_t mask, const xcb_params_cw_t* src, uint32_t* dest); + static void pack_values(uint32_t mask, const xcb_params_gc_t* src, uint32_t* dest); + static void pack_values(uint32_t mask, const xcb_params_configure_window_t* src, uint32_t* dest); - void preload_atoms(); - - void query_extensions(); + operator Display*() const; + Visual* visual(uint8_t depth = 32U); + xcb_screen_t* screen(bool realloc = false); string id(xcb_window_t w) const; - 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; - void send_client_message(const shared_ptr& message, xcb_window_t target, uint32_t event_mask = 0xFFFFFF, bool propagate = false) const; @@ -169,6 +181,8 @@ class connection : public detail::connection_base m_visual; registry m_registry{*this}; xcb_screen_t* m_screen{nullptr}; }; diff --git a/include/x11/fonts.hpp b/include/x11/fonts.hpp index 69e86ad6..fbf32bee 100644 --- a/include/x11/fonts.hpp +++ b/include/x11/fonts.hpp @@ -46,7 +46,7 @@ class font_manager { using make_type = unique_ptr; static make_type make(); - explicit font_manager(connection& conn, const logger& logger, Display* dsp, Visual* vis, Colormap&& cm); + explicit font_manager(connection& conn, const logger& logger); ~font_manager(); font_manager(const font_manager& o) = delete; diff --git a/include/x11/window.hpp b/include/x11/window.hpp index 87ba2149..586a8fb4 100644 --- a/include/x11/window.hpp +++ b/include/x11/window.hpp @@ -27,6 +27,8 @@ class window : public xpp::window { window reconfigure_struts(uint16_t w, uint16_t h, int16_t x, bool bottom = false); void redraw(); + + void visibility_notify(xcb_visibility_t state); }; POLYBAR_NS_END diff --git a/include/x11/xlib.hpp b/include/x11/xlib.hpp deleted file mode 100644 index 2644095a..00000000 --- a/include/x11/xlib.hpp +++ /dev/null @@ -1,30 +0,0 @@ -#pragma once - -#include - -#include "common.hpp" - -POLYBAR_NS - -namespace xlib { - namespace detail { - /** - * RAII wrapper for Xlib display locking - */ - class display_lock { - public: - explicit display_lock(Display* display); - ~display_lock(); - - protected: - Display* m_display; - }; - } - - Display* get_display(); - Visual* get_visual(int screen = 0, uint8_t depth = 32); - Colormap create_colormap(int screen = 0); - inline auto make_display_lock(); -} - -POLYBAR_NS_END diff --git a/include/x11/xutils.hpp b/include/x11/xutils.hpp deleted file mode 100644 index 790d8a66..00000000 --- a/include/x11/xutils.hpp +++ /dev/null @@ -1,32 +0,0 @@ -#pragma once - -#include -#include - -#include "common.hpp" - -POLYBAR_NS - -class connection; -class config; - -namespace xutils { - struct xcb_connection_deleter { - void operator()(xcb_connection_t* c) { - xcb_disconnect(c); - } - }; - - xcb_connection_t* get_connection(); - - void pack_values(uint32_t mask, const uint32_t* src, uint32_t* dest); - void pack_values(uint32_t mask, const xcb_params_cw_t* src, uint32_t* dest); - void pack_values(uint32_t mask, const xcb_params_gc_t* src, uint32_t* dest); - void pack_values(uint32_t mask, const xcb_params_configure_window_t* src, uint32_t* dest); - - void visibility_notify(connection& conn, const xcb_window_t& win, xcb_visibility_t state); - - void compton_shadow_exclude(connection& conn, const xcb_window_t& win); -} - -POLYBAR_NS_END diff --git a/src/components/bar.cpp b/src/components/bar.cpp index ffba28d1..55d33256 100644 --- a/src/components/bar.cpp +++ b/src/components/bar.cpp @@ -20,7 +20,6 @@ #include "x11/extensions/all.hpp" #include "x11/tray_manager.hpp" #include "x11/wm.hpp" -#include "x11/xutils.hpp" #if ENABLE_I3 #include "utils/i3.hpp" @@ -712,7 +711,7 @@ bool bar::on(const sig_ui::tick&) { params.y = std::max(params.y, static_cast(m_opts.shade_pos.y)); } - xutils::pack_values(mask, ¶ms, values); + connection::pack_values(mask, ¶ms, values); m_connection.configure_window(m_opts.window, mask, values); m_connection.flush(); diff --git a/src/components/renderer.cpp b/src/components/renderer.cpp index b08a8162..1580c7fe 100644 --- a/src/components/renderer.cpp +++ b/src/components/renderer.cpp @@ -9,8 +9,6 @@ #include "x11/extensions/all.hpp" #include "x11/generic.hpp" #include "x11/winspec.hpp" -#include "x11/xlib.hpp" -#include "x11/xutils.hpp" POLYBAR_NS @@ -58,7 +56,7 @@ renderer::renderer(connection& conn, signal_emitter& emitter, const logger& logg m_depth = 24; - m_fontmanager->set_visual(xlib::get_visual(m_connection.default_screen(), m_depth)); + m_fontmanager->set_visual(m_connection.visual(m_depth)); } m_log.trace("renderer: Allocate colormap"); @@ -112,7 +110,7 @@ renderer::renderer(connection& conn, signal_emitter& emitter, const logger& logg xcb_params_gc_t params{}; XCB_AUX_ADD_PARAM(&mask, ¶ms, foreground, colors[i]); XCB_AUX_ADD_PARAM(&mask, ¶ms, graphics_exposures, 0); - xutils::pack_values(mask, ¶ms, value_list); + connection::pack_values(mask, ¶ms, value_list); m_colors.emplace(gc(i), colors[i]); m_gcontexts.emplace(gc(i), m_connection.generate_id()); @@ -524,7 +522,8 @@ void renderer::debug_hints() { ; // clang-format on - xutils::compton_shadow_exclude(m_connection, hintwin); + const uint32_t shadow{0}; + m_connection.change_property(XCB_PROP_MODE_REPLACE, hintwin, _COMPTON_SHADOW, XCB_ATOM_CARDINAL, 32, 1, &shadow); m_connection.map_window(hintwin); } } diff --git a/src/main.cpp b/src/main.cpp index 58474b85..08c94ccf 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -16,7 +16,6 @@ #include "utils/process.hpp" #include "x11/connection.hpp" #include "x11/tray_manager.hpp" -#include "x11/xutils.hpp" using namespace polybar; @@ -71,18 +70,14 @@ int main(int argc, char** argv) { // Connect to X server //================================================== XInitThreads(); + Display* xdisplay{XOpenDisplay(nullptr)}; - // Store the xcb connection pointer with a disconnect deleter - unique_ptr xcbconn{xutils::get_connection()}; - - if (!xcbconn) { + if (xdisplay == nullptr) { logger.err("A connection to X could not be established... "); return EXIT_FAILURE; } - connection& conn{connection::make(&*xcbconn)}; - conn.preload_atoms(); - conn.query_extensions(); + connection& conn{connection::make(xdisplay)}; conn.ensure_event_mask(conn.root(), XCB_EVENT_MASK_PROPERTY_CHANGE); //================================================== diff --git a/src/modules/xbacklight.cpp b/src/modules/xbacklight.cpp index 5ad375aa..20a9bb91 100644 --- a/src/modules/xbacklight.cpp +++ b/src/modules/xbacklight.cpp @@ -5,7 +5,6 @@ #include "utils/math.hpp" #include "x11/connection.hpp" #include "x11/graphics.hpp" -#include "x11/xutils.hpp" #include "modules/meta/base.inl" diff --git a/src/x11/color.cpp b/src/x11/color.cpp index 38eb1571..94adc29d 100644 --- a/src/x11/color.cpp +++ b/src/x11/color.cpp @@ -20,14 +20,7 @@ color::color(string hex) : m_source(hex) { } m_value = std::strtoul(&hex[1], nullptr, 16); - - // Premultiply alpha - auto a = color_util::alpha_channel(m_value); - auto r = color_util::red_channel(m_value) * a / 255; - auto g = color_util::green_channel(m_value) * a / 255; - auto b = color_util::blue_channel(m_value) * a / 255; - - m_color = (a << 24) | (r << 16) | (g << 8) | b; + m_color = color_util::premultiply_alpha(m_value); } string color::source() const { diff --git a/src/x11/connection.cpp b/src/x11/connection.cpp index aae3e943..58351185 100644 --- a/src/x11/connection.cpp +++ b/src/x11/connection.cpp @@ -1,3 +1,4 @@ +#include #include #include "errors.hpp" @@ -6,28 +7,22 @@ #include "utils/string.hpp" #include "x11/atoms.hpp" #include "x11/connection.hpp" -#include "x11/xlib.hpp" -#include "x11/xutils.hpp" POLYBAR_NS /** * Create instance */ -connection::make_type connection::make(xcb_connection_t* conn) { - if (conn == nullptr) { - conn = xutils::get_connection(); - } - // return static_cast(*factory_util::singleton>( - // conn, file_util::make_file_descriptor(xcb_get_file_descriptor(conn)))); +connection::make_type connection::make(Display* display) { return static_cast( - *factory_util::singleton>(conn)); + *factory_util::singleton>(display)); } -/** - * Preload required xcb atoms - */ -void connection::preload_atoms() { +connection::connection(Display* dsp) : base_type(XGetXCBConnection(dsp)), m_display(dsp) { + XSetEventQueueOwner(m_display, XCBOwnsEventQueue); + + // Preload required xcb atoms {{{ + vector cookies(memory_util::countof(ATOMS)); xcb_intern_atom_reply_t* reply{nullptr}; @@ -42,12 +37,9 @@ void connection::preload_atoms() { free(reply); } -} -/** - * Query for X extensions - */ -void connection::query_extensions() { +// }}} +// Query for X extensions {{{ #if WITH_XDAMAGE damage_util::query_extension(*this); #endif @@ -66,6 +58,45 @@ void connection::query_extensions() { #if WITH_XKB xkb_util::query_extension(*this); #endif + // }}} +} + +connection::~connection() { + disconnect(); + std::for_each(m_visual.begin(), m_visual.end(), [=](pair p) { XFree(p.second); }); + m_visual.clear(); +} + +void connection::pack_values(uint32_t mask, const uint32_t* src, uint32_t* dest) { + for (; mask; mask >>= 1, src++) { + if (mask & 1) { + *dest++ = *src; + } + } +} +void connection::pack_values(uint32_t mask, const xcb_params_cw_t* src, uint32_t* dest) { + pack_values(mask, reinterpret_cast(src), dest); +} +void connection::pack_values(uint32_t mask, const xcb_params_gc_t* src, uint32_t* dest) { + pack_values(mask, reinterpret_cast(src), dest); +} +void connection::pack_values(uint32_t mask, const xcb_params_configure_window_t* src, uint32_t* dest) { + pack_values(mask, reinterpret_cast(src), dest); +} + +connection::operator Display*() const { + return m_display; +} + +Visual* connection::visual(uint8_t depth) { + auto visual_it = m_visual.find(depth); + if (visual_it == m_visual.end()) { + XVisualInfo info{}; + if (XMatchVisualInfo(*this, default_screen(), depth, TrueColor, &info)) { + visual_it = m_visual.emplace_hint(visual_it, depth, info.visual); + } + } + return visual_it->second; } /** @@ -184,8 +215,4 @@ void connection::dispatch_event(const shared_ptr& evt) cons m_registry.dispatch(evt); } -// connection::operator Display*() const { -// return xlib::get_display(); -// } - POLYBAR_NS_END diff --git a/src/x11/ewmh.cpp b/src/x11/ewmh.cpp index 2147b8d6..e6b4fe34 100644 --- a/src/x11/ewmh.cpp +++ b/src/x11/ewmh.cpp @@ -1,6 +1,6 @@ #include "x11/ewmh.hpp" #include "components/types.hpp" -#include "x11/xutils.hpp" +#include "x11/connection.hpp" POLYBAR_NS @@ -10,8 +10,7 @@ namespace ewmh_util { if (!g_connection) { g_connection = memory_util::make_malloc_ptr( [=](xcb_ewmh_connection_t* c) { xcb_ewmh_connection_wipe(c); }); - xcb_ewmh_init_atoms_replies( - &*g_connection, xcb_ewmh_init_atoms(xutils::get_connection(), &*g_connection), nullptr); + xcb_ewmh_init_atoms_replies(&*g_connection, xcb_ewmh_init_atoms(connection::make(), &*g_connection), nullptr); } return g_connection; } diff --git a/src/x11/fonts.cpp b/src/x11/fonts.cpp index ac597aa3..ac879306 100644 --- a/src/x11/fonts.cpp +++ b/src/x11/fonts.cpp @@ -1,5 +1,3 @@ -#include - #include "components/logger.hpp" #include "errors.hpp" #include "utils/color.hpp" @@ -8,8 +6,6 @@ #include "x11/connection.hpp" #include "x11/draw.hpp" #include "x11/fonts.hpp" -#include "x11/xlib.hpp" -#include "x11/xutils.hpp" POLYBAR_NS @@ -17,11 +13,14 @@ void font_ref::_deleter::operator()(font_ref* font) { font->glyph_widths.clear(); font->width_lut.clear(); - if (font->xft != nullptr) { - XftFontClose(&*xlib::get_display(), font->xft); - } - if (font->ptr != XCB_NONE) { - xcb_close_font(&*xutils::get_connection(), font->ptr); + if (font->xft != nullptr || font->ptr != XCB_NONE) { + auto& conn = connection::make(); + if (font->xft != nullptr) { + XftFontClose(conn, font->xft); + } + if (font->ptr != XCB_NONE) { + xcb_close_font(conn, font->ptr); + } } delete font; } @@ -30,16 +29,15 @@ void font_ref::_deleter::operator()(font_ref* font) { * Create instance */ font_manager::make_type font_manager::make() { - return factory_util::unique( - connection::make(), logger::make(), xlib::get_display(), xlib::get_visual(), xlib::create_colormap()); + return factory_util::unique(connection::make(), logger::make()); } -font_manager::font_manager(connection& conn, const logger& logger, Display* dsp, Visual* vis, Colormap&& cm) +font_manager::font_manager(connection& conn, const logger& logger) : m_connection(conn) , m_logger(logger) - , m_display(forward(dsp)) - , m_visual(forward(vis)) - , m_colormap(forward(cm)) { + , m_display(m_connection) + , m_visual(m_connection.visual()) + , m_colormap(XDefaultColormap(m_display, m_connection.default_screen())) { if (!XftInit(nullptr) || !XftInitFtLibrary()) { throw application_error("Could not initialize Xft library"); } diff --git a/src/x11/graphics.cpp b/src/x11/graphics.cpp index ab5fd08b..58f7648d 100644 --- a/src/x11/graphics.cpp +++ b/src/x11/graphics.cpp @@ -6,7 +6,6 @@ #include "x11/atoms.hpp" #include "x11/connection.hpp" #include "x11/graphics.hpp" -#include "x11/xutils.hpp" POLYBAR_NS @@ -66,7 +65,7 @@ namespace graphics_util { uint32_t values[32]; XCB_AUX_ADD_PARAM(&mask, ¶ms, graphics_exposures, 1); - xutils::pack_values(mask, ¶ms, values); + connection::pack_values(mask, ¶ms, values); *gc = conn.generate_id(); conn.create_gc_checked(*gc, drawable, mask, values); diff --git a/src/x11/tray_client.cpp b/src/x11/tray_client.cpp index e9b6b866..98cf0ccd 100644 --- a/src/x11/tray_client.cpp +++ b/src/x11/tray_client.cpp @@ -5,7 +5,6 @@ #include "x11/connection.hpp" #include "x11/tray_client.hpp" #include "x11/xembed.hpp" -#include "x11/xutils.hpp" POLYBAR_NS @@ -95,7 +94,7 @@ void tray_client::reconfigure(int16_t x, int16_t y) const { XCB_AUX_ADD_PARAM(&configure_mask, &configure_params, x, x); XCB_AUX_ADD_PARAM(&configure_mask, &configure_params, y, y); - xutils::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); } diff --git a/src/x11/tray_manager.cpp b/src/x11/tray_manager.cpp index fb1ca1e9..36912804 100644 --- a/src/x11/tray_manager.cpp +++ b/src/x11/tray_manager.cpp @@ -21,7 +21,6 @@ #include "x11/winspec.hpp" #include "x11/wm.hpp" #include "x11/xembed.hpp" -#include "x11/xutils.hpp" // ==================================================================================================== // @@ -366,7 +365,7 @@ void tray_manager::reconfigure_window() { XCB_AUX_ADD_PARAM(&mask, ¶ms, width, width); XCB_AUX_ADD_PARAM(&mask, ¶ms, x, x); - xutils::pack_values(mask, ¶ms, values); + connection::pack_values(mask, ¶ms, values); m_connection.configure_window_checked(m_tray, mask, values); } @@ -561,7 +560,8 @@ void tray_manager::create_window() { m_tray = win << cw_flush(true); m_log.info("Tray window: %s", m_connection.id(m_tray)); - xutils::compton_shadow_exclude(m_connection, m_tray); + const uint32_t shadow{0}; + m_connection.change_property(XCB_PROP_MODE_REPLACE, m_tray, _COMPTON_SHADOW, XCB_ATOM_CARDINAL, 32, 1, &shadow); } /** @@ -617,7 +617,7 @@ void tray_manager::restack_window() { XCB_AUX_ADD_PARAM(&mask, ¶ms, sibling, m_opts.sibling); XCB_AUX_ADD_PARAM(&mask, ¶ms, stack_mode, XCB_STACK_MODE_ABOVE); - xutils::pack_values(mask, ¶ms, values); + connection::pack_values(mask, ¶ms, values); m_connection.configure_window_checked(m_tray, mask, values); } catch (const exception& err) { auto id = m_connection.id(m_opts.sibling); diff --git a/src/x11/window.cpp b/src/x11/window.cpp index 04bdd1a3..4c4518dd 100644 --- a/src/x11/window.cpp +++ b/src/x11/window.cpp @@ -5,7 +5,7 @@ #include "x11/connection.hpp" #include "x11/extensions/randr.hpp" #include "x11/window.hpp" -#include "x11/xutils.hpp" +#include "utils/memory.hpp" POLYBAR_NS @@ -25,7 +25,7 @@ window window::create_checked(int16_t x, int16_t y, uint16_t w, uint16_t h, uint auto root = connection().screen()->root; auto copy = XCB_COPY_FROM_PARENT; uint32_t values[16]{0}; - xutils::pack_values(mask, p, values); + connection::pack_values(mask, p, values); connection().create_window_checked(copy, *this, root, x, y, w, h, 0, copy, copy, mask, values); return *this; @@ -60,7 +60,7 @@ window window::reconfigure_geom(uint16_t w, uint16_t h, int16_t x, int16_t y) { XCB_AUX_ADD_PARAM(&mask, ¶ms, x, x); XCB_AUX_ADD_PARAM(&mask, ¶ms, y, y); - xutils::pack_values(mask, ¶ms, values); + connection::pack_values(mask, ¶ms, values); configure_checked(mask, values); return *this; @@ -77,7 +77,7 @@ window window::reconfigure_pos(int16_t x, int16_t y) { XCB_AUX_ADD_PARAM(&mask, ¶ms, x, x); XCB_AUX_ADD_PARAM(&mask, ¶ms, y, y); - xutils::pack_values(mask, ¶ms, values); + connection::pack_values(mask, ¶ms, values); configure_checked(mask, values); return *this; @@ -111,9 +111,22 @@ window window::reconfigure_struts(uint16_t w, uint16_t h, int16_t x, bool bottom * Trigger redraw by toggling visibility state */ void window::redraw() { - xutils::visibility_notify(connection(), *this, XCB_VISIBILITY_FULLY_OBSCURED); - xutils::visibility_notify(connection(), *this, XCB_VISIBILITY_UNOBSCURED); + visibility_notify(XCB_VISIBILITY_FULLY_OBSCURED); + visibility_notify(XCB_VISIBILITY_UNOBSCURED); connection().flush(); } +/** + * Send visibility notify event + */ +void window::visibility_notify(xcb_visibility_t state) { + auto notify = memory_util::make_malloc_ptr(); + notify->response_type = XCB_VISIBILITY_NOTIFY; + notify->window = *this; + notify->state = state; + + uint32_t mask{XCB_EVENT_MASK_NO_EVENT}; + connection().send_event(false, *this, mask, reinterpret_cast(notify.get())); +} + POLYBAR_NS_END diff --git a/src/x11/winspec.cpp b/src/x11/winspec.cpp index ba4c3145..6e2ecc40 100644 --- a/src/x11/winspec.cpp +++ b/src/x11/winspec.cpp @@ -2,7 +2,6 @@ #include "x11/connection.hpp" #include "x11/winspec.hpp" -#include "x11/xutils.hpp" POLYBAR_NS @@ -33,7 +32,7 @@ xcb_window_t winspec::operator<<(const cw_flush& f) { m_height = 1; } - xutils::pack_values(m_mask, &m_params, values); + connection::pack_values(m_mask, &m_params, values); if (f.checked) { m_connection.create_window_checked( diff --git a/src/x11/xlib.cpp b/src/x11/xlib.cpp deleted file mode 100644 index b2b196f8..00000000 --- a/src/x11/xlib.cpp +++ /dev/null @@ -1,44 +0,0 @@ -#include - -#include "utils/factory.hpp" -#include "x11/xlib.hpp" - -POLYBAR_NS - -namespace xlib { - namespace detail { - display_lock::display_lock(Display* display) : m_display(forward(display)) { - XLockDisplay(m_display); - } - - display_lock::~display_lock() { - XUnlockDisplay(m_display); - } - } - - Display* get_display() { - static Display* display{XOpenDisplay(nullptr)}; - return display; - } - - Visual* get_visual(int screen, uint8_t depth) { - static shared_ptr visual; - if (!visual) { - XVisualInfo info{}; - if (XMatchVisualInfo(get_display(), screen, depth, TrueColor, &info)) { - visual = shared_ptr(info.visual, [=](Visual* v) { XFree(v); }); - } - } - return &*visual; - } - - Colormap create_colormap(int screen) { - return XDefaultColormap(get_display(), screen); - } - - inline auto make_display_lock() { - return make_unique(get_display()); - } -} - -POLYBAR_NS_END diff --git a/src/x11/xresources.cpp b/src/x11/xresources.cpp index 54846c09..c29e8339 100644 --- a/src/x11/xresources.cpp +++ b/src/x11/xresources.cpp @@ -3,7 +3,7 @@ #include #include "utils/factory.hpp" -#include "x11/xlib.hpp" +#include "x11/connection.hpp" #include "x11/xresources.hpp" POLYBAR_NS @@ -13,7 +13,7 @@ POLYBAR_NS */ xresource_manager::make_type xresource_manager::make() { return static_cast( - *factory_util::singleton>(xlib::get_display())); + *factory_util::singleton>(connection::make())); } /** diff --git a/src/x11/xutils.cpp b/src/x11/xutils.cpp deleted file mode 100644 index 6cc23706..00000000 --- a/src/x11/xutils.cpp +++ /dev/null @@ -1,59 +0,0 @@ -#include - -#include "components/config.hpp" -#include "utils/memory.hpp" -#include "x11/atoms.hpp" -#include "x11/connection.hpp" -#include "x11/xlib.hpp" -#include "x11/xutils.hpp" - -POLYBAR_NS - -namespace xutils { - xcb_connection_t* get_connection() { - static xcb_connection_t* connection; - if (!connection) { - auto display = xlib::get_display(); - if (display != nullptr) { - XSetEventQueueOwner(display, XCBOwnsEventQueue); - connection = XGetXCBConnection(xlib::get_display()); - } - } - return connection; - } - - void pack_values(uint32_t mask, const uint32_t* src, uint32_t* dest) { - for (; mask; mask >>= 1, src++) { - if (mask & 1) { - *dest++ = *src; - } - } - } - - void pack_values(uint32_t mask, const xcb_params_cw_t* src, uint32_t* dest) { - xutils::pack_values(mask, reinterpret_cast(src), dest); - } - - void pack_values(uint32_t mask, const xcb_params_gc_t* src, uint32_t* dest) { - xutils::pack_values(mask, reinterpret_cast(src), dest); - } - - void pack_values(uint32_t mask, const xcb_params_configure_window_t* src, uint32_t* dest) { - xutils::pack_values(mask, reinterpret_cast(src), dest); - } - - void visibility_notify(connection& conn, const xcb_window_t& win, xcb_visibility_t state) { - auto notify = memory_util::make_malloc_ptr(); - notify->response_type = XCB_VISIBILITY_NOTIFY; - notify->window = win; - notify->state = state; - conn.send_event(true, win, XCB_EVENT_MASK_NO_EVENT, reinterpret_cast(&*notify)); - } - - void compton_shadow_exclude(connection& conn, const xcb_window_t& win) { - const uint32_t shadow{0}; - conn.change_property(XCB_PROP_MODE_REPLACE, win, _COMPTON_SHADOW, XCB_ATOM_CARDINAL, 32, 1, &shadow); - } -} - -POLYBAR_NS_END