From 3b512d1a2cfcfcb454616e3e3988fab34b1ac3c4 Mon Sep 17 00:00:00 2001 From: Keepo Date: Sat, 28 Mar 2026 21:10:15 -0400 Subject: [PATCH 1/5] feat(client): add support for 8-bit hex color codes in CSS This allows users to use #RRGGBBAA format in their style.css. The client now detects 8-bit hex codes, transforms them into GTK-compatible rgba() syntax, and loads the modified data into the CSS provider. - Added utility to detect 8-bit hex patterns. - Added transformation logic to convert hex alpha to decimal. - Intercepted CSS loading in Client::setupCss to handle the conversion. --- include/util/hex_checker.hpp | 17 ++++++++++++ meson.build | 4 ++- src/client.cpp | 22 +++++++++------- src/util/has_8bit_hex.cpp | 15 +++++++++++ src/util/transform_8bit_to_rgba.cpp | 41 +++++++++++++++++++++++++++++ 5 files changed, 88 insertions(+), 11 deletions(-) create mode 100644 include/util/hex_checker.hpp create mode 100644 src/util/has_8bit_hex.cpp create mode 100644 src/util/transform_8bit_to_rgba.cpp diff --git a/include/util/hex_checker.hpp b/include/util/hex_checker.hpp new file mode 100644 index 00000000..5e742436 --- /dev/null +++ b/include/util/hex_checker.hpp @@ -0,0 +1,17 @@ +#pragma once + +#include + +/** + * Reads a CSS file, searches for 8-bit hex codes (#RRGGBBAA), + * and returns true if found, or false if not found. + */ + +bool has_8bit_hex(std::string file_path); + +/** + * Reads a CSS file, searches for 8-bit hex codes (#RRGGBBAA), + * and transforms them into GTK-compatible rgba() syntax. + */ + +std::string transform_8bit_to_hex(std::string file_path); diff --git a/meson.build b/meson.build index db9407eb..b9df3f80 100644 --- a/meson.build +++ b/meson.build @@ -185,7 +185,9 @@ src_files = files( 'src/util/gtk_icon.cpp', 'src/util/icon_loader.cpp', 'src/util/regex_collection.cpp', - 'src/util/css_reload_helper.cpp' + 'src/util/css_reload_helper.cpp', + 'src/util/has_8bit_hex.cpp', + 'src/util/transform_8bit_to_rgba.cpp' ) man_files = files( diff --git a/src/client.cpp b/src/client.cpp index 1da3ffcb..178552a4 100644 --- a/src/client.cpp +++ b/src/client.cpp @@ -10,6 +10,7 @@ #include "idle-inhibit-unstable-v1-client-protocol.h" #include "util/clara.hpp" #include "util/format.hpp" +#include "util/hex_checker.hpp" waybar::Client* waybar::Client::inst() { static auto* c = new Client(); @@ -22,21 +23,18 @@ void waybar::Client::handleGlobal(void* data, struct wl_registry* registry, uint if (strcmp(interface, zxdg_output_manager_v1_interface.name) == 0 && version >= ZXDG_OUTPUT_V1_NAME_SINCE_VERSION) { - if (client->xdg_output_manager != nullptr) { zxdg_output_manager_v1_destroy(client->xdg_output_manager); client->xdg_output_manager = nullptr; } - client->xdg_output_manager = static_cast( - wl_registry_bind(registry, name, &zxdg_output_manager_v1_interface, - ZXDG_OUTPUT_V1_NAME_SINCE_VERSION)); + client->xdg_output_manager = static_cast(wl_registry_bind( + registry, name, &zxdg_output_manager_v1_interface, ZXDG_OUTPUT_V1_NAME_SINCE_VERSION)); } else if (strcmp(interface, zwp_idle_inhibit_manager_v1_interface.name) == 0) { - if (client->idle_inhibit_manager != nullptr) { zwp_idle_inhibit_manager_v1_destroy(client->idle_inhibit_manager); - client->idle_inhibit_manager = nullptr; + client->idle_inhibit_manager = nullptr; } client->idle_inhibit_manager = static_cast( @@ -209,11 +207,15 @@ auto waybar::Client::setupCss(const std::string& css_file) -> void { } css_provider_ = Gtk::CssProvider::create(); - if (!css_provider_->load_from_path(css_file)) { - css_provider_.reset(); - throw std::runtime_error("Can't open style file"); + if (has_8bit_hex(css_file)) { + std::string modified_css = transform_8bit_to_hex(css_file); + css_provider_->load_from_data(modified_css); + } else { + if (!css_provider_->load_from_path(css_file)) { + css_provider_.reset(); + throw std::runtime_error("Can't open style file"); + } } - Gtk::StyleContext::add_provider_for_screen(screen, css_provider_, GTK_STYLE_PROVIDER_PRIORITY_USER); } diff --git a/src/util/has_8bit_hex.cpp b/src/util/has_8bit_hex.cpp new file mode 100644 index 00000000..db594980 --- /dev/null +++ b/src/util/has_8bit_hex.cpp @@ -0,0 +1,15 @@ +#include +#include +#include + +namespace fs = std::filesystem; + +bool has_8bit_hex(std::string file_path) { + std::ifstream f(file_path, std::ios::in | std::ios::binary); + const auto size = fs::file_size(file_path); + std::string result(size, '\0'); + f.read(result.data(), size); + std::regex pattern( + R"((?:\#([a-fA-F0-9]{2})([a-fA-F0-9]{2})([a-fA-F0-9]{2})([a-fA-F0-9]{2})))"); + return std::regex_search(result, pattern); +} diff --git a/src/util/transform_8bit_to_rgba.cpp b/src/util/transform_8bit_to_rgba.cpp new file mode 100644 index 00000000..4e0cd3a8 --- /dev/null +++ b/src/util/transform_8bit_to_rgba.cpp @@ -0,0 +1,41 @@ +#include +#include +#include +#include +#include +namespace fs = std::filesystem; +std::string transform_8bit_to_hex(std::string file_path) { + std::ifstream f(file_path, std::ios::in | std::ios::binary); + const auto size = fs::file_size(file_path); + std::string result(size, '\0'); + f.read(result.data(), size); + + std::regex pattern( + R"((?:\#([a-fA-F0-9]{2})([a-fA-F0-9]{2})([a-fA-F0-9]{2})([a-fA-F0-9]{2})))"); + std::string final_output; + + auto it = std::sregex_iterator(result.begin(), result.end(), pattern); + auto eof = std::sregex_iterator(); + std::smatch match = *it; + + while (it != eof) { + + final_output += match.prefix().str(); + + int r = stoi(match[1].str(), nullptr, 16); + int g = stoi(match[2].str(), nullptr, 16); + int b = stoi(match[3].str(), nullptr, 16); + double a = (stoi(match[4].str(), nullptr, 16) / 255.0); + + std::stringstream ss; + ss << "rgba(" << r << "," << g << "," << b << "," << std::fixed + << std::setprecision(2) << a << ")"; + final_output += ss.str(); + + ++it; + } + + final_output += match.suffix().str(); + + return final_output; +} From 937ef176ffc5af51e243e744adc3d965ec8dd006 Mon Sep 17 00:00:00 2001 From: Keepo Date: Sat, 28 Mar 2026 22:45:42 -0400 Subject: [PATCH 2/5] fix: memory issues & duplicate file loads. --- src/client.cpp | 4 ++-- src/util/transform_8bit_to_rgba.cpp | 33 +++++++++++++++++++++++------ 2 files changed, 28 insertions(+), 9 deletions(-) diff --git a/src/client.cpp b/src/client.cpp index 178552a4..d1489186 100644 --- a/src/client.cpp +++ b/src/client.cpp @@ -207,8 +207,8 @@ auto waybar::Client::setupCss(const std::string& css_file) -> void { } css_provider_ = Gtk::CssProvider::create(); - if (has_8bit_hex(css_file)) { - std::string modified_css = transform_8bit_to_hex(css_file); + auto [modified_css, was_transformed] = transform_8bit_to_hex(css_file); + if (was_transformed) { css_provider_->load_from_data(modified_css); } else { if (!css_provider_->load_from_path(css_file)) { diff --git a/src/util/transform_8bit_to_rgba.cpp b/src/util/transform_8bit_to_rgba.cpp index 4e0cd3a8..ca95784d 100644 --- a/src/util/transform_8bit_to_rgba.cpp +++ b/src/util/transform_8bit_to_rgba.cpp @@ -4,21 +4,40 @@ #include #include namespace fs = std::filesystem; -std::string transform_8bit_to_hex(std::string file_path) { + +struct TransformResult { + std::string css; + bool was_transformed; +}; + +TransformResult transform_8bit_to_hex(const std::string& file_path) { std::ifstream f(file_path, std::ios::in | std::ios::binary); const auto size = fs::file_size(file_path); std::string result(size, '\0'); + if (!f.is_open() || !f.good()) { + throw std::runtime_error("Cannot open file: " + file_path); + } + + if (size == 0) { + return {.css = result, .was_transformed = false}; + } + f.read(result.data(), size); - std::regex pattern( - R"((?:\#([a-fA-F0-9]{2})([a-fA-F0-9]{2})([a-fA-F0-9]{2})([a-fA-F0-9]{2})))"); + static std::regex pattern( + R"((\#([a-fA-F0-9]{2})([a-fA-F0-9]{2})([a-fA-F0-9]{2})([a-fA-F0-9]{2})))"); std::string final_output; auto it = std::sregex_iterator(result.begin(), result.end(), pattern); auto eof = std::sregex_iterator(); - std::smatch match = *it; + if (it == eof) { + return {.css = result, .was_transformed = false}; + } + + std::smatch match; while (it != eof) { + match = *it; final_output += match.prefix().str(); @@ -28,8 +47,8 @@ std::string transform_8bit_to_hex(std::string file_path) { double a = (stoi(match[4].str(), nullptr, 16) / 255.0); std::stringstream ss; - ss << "rgba(" << r << "," << g << "," << b << "," << std::fixed - << std::setprecision(2) << a << ")"; + ss << "rgba(" << r << "," << g << "," << b << "," << std::fixed << std::setprecision(2) << a + << ")"; final_output += ss.str(); ++it; @@ -37,5 +56,5 @@ std::string transform_8bit_to_hex(std::string file_path) { final_output += match.suffix().str(); - return final_output; + return {.css = final_output, .was_transformed = true}; } From 3533265675084b07ff5fb51f9b1ec8570080e954 Mon Sep 17 00:00:00 2001 From: Keepo Date: Sat, 28 Mar 2026 23:02:16 -0400 Subject: [PATCH 3/5] fix: removed unnecessary function, update header file for signature change. --- include/util/hex_checker.hpp | 9 +-------- src/util/has_8bit_hex.cpp | 15 --------------- 2 files changed, 1 insertion(+), 23 deletions(-) delete mode 100644 src/util/has_8bit_hex.cpp diff --git a/include/util/hex_checker.hpp b/include/util/hex_checker.hpp index 5e742436..e896031b 100644 --- a/include/util/hex_checker.hpp +++ b/include/util/hex_checker.hpp @@ -2,16 +2,9 @@ #include -/** - * Reads a CSS file, searches for 8-bit hex codes (#RRGGBBAA), - * and returns true if found, or false if not found. - */ - -bool has_8bit_hex(std::string file_path); - /** * Reads a CSS file, searches for 8-bit hex codes (#RRGGBBAA), * and transforms them into GTK-compatible rgba() syntax. */ -std::string transform_8bit_to_hex(std::string file_path); +std::string transform_8bit_to_hex(const std::string& file_path); diff --git a/src/util/has_8bit_hex.cpp b/src/util/has_8bit_hex.cpp deleted file mode 100644 index db594980..00000000 --- a/src/util/has_8bit_hex.cpp +++ /dev/null @@ -1,15 +0,0 @@ -#include -#include -#include - -namespace fs = std::filesystem; - -bool has_8bit_hex(std::string file_path) { - std::ifstream f(file_path, std::ios::in | std::ios::binary); - const auto size = fs::file_size(file_path); - std::string result(size, '\0'); - f.read(result.data(), size); - std::regex pattern( - R"((?:\#([a-fA-F0-9]{2})([a-fA-F0-9]{2})([a-fA-F0-9]{2})([a-fA-F0-9]{2})))"); - return std::regex_search(result, pattern); -} From 72d6a51fb7e2fe04aa36515b8db8107c03d3372e Mon Sep 17 00:00:00 2001 From: Keepo Date: Sat, 28 Mar 2026 23:26:34 -0400 Subject: [PATCH 4/5] fix: fix compile/run bugs from trying to be safer. rip --- include/util/hex_checker.hpp | 2 +- meson.build | 1 - src/util/transform_8bit_to_rgba.cpp | 2 +- 3 files changed, 2 insertions(+), 3 deletions(-) diff --git a/include/util/hex_checker.hpp b/include/util/hex_checker.hpp index e896031b..9855c2de 100644 --- a/include/util/hex_checker.hpp +++ b/include/util/hex_checker.hpp @@ -7,4 +7,4 @@ * and transforms them into GTK-compatible rgba() syntax. */ -std::string transform_8bit_to_hex(const std::string& file_path); +std::pair transform_8bit_to_hex(const std::string& file_path); diff --git a/meson.build b/meson.build index b9df3f80..0c494eb2 100644 --- a/meson.build +++ b/meson.build @@ -186,7 +186,6 @@ src_files = files( 'src/util/icon_loader.cpp', 'src/util/regex_collection.cpp', 'src/util/css_reload_helper.cpp', - 'src/util/has_8bit_hex.cpp', 'src/util/transform_8bit_to_rgba.cpp' ) diff --git a/src/util/transform_8bit_to_rgba.cpp b/src/util/transform_8bit_to_rgba.cpp index ca95784d..dc87a88e 100644 --- a/src/util/transform_8bit_to_rgba.cpp +++ b/src/util/transform_8bit_to_rgba.cpp @@ -25,7 +25,7 @@ TransformResult transform_8bit_to_hex(const std::string& file_path) { f.read(result.data(), size); static std::regex pattern( - R"((\#([a-fA-F0-9]{2})([a-fA-F0-9]{2})([a-fA-F0-9]{2})([a-fA-F0-9]{2})))"); + R"(\#([a-fA-F0-9]{2})([a-fA-F0-9]{2})([a-fA-F0-9]{2})([a-fA-F0-9]{2}))"); std::string final_output; auto it = std::sregex_iterator(result.begin(), result.end(), pattern); From a9aab4e3569a0c771a873e2a4b1c8695f70430e9 Mon Sep 17 00:00:00 2001 From: Keepo Date: Sat, 28 Mar 2026 23:44:41 -0400 Subject: [PATCH 5/5] fix: type consistency --- include/util/hex_checker.hpp | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/include/util/hex_checker.hpp b/include/util/hex_checker.hpp index 9855c2de..27a84d1f 100644 --- a/include/util/hex_checker.hpp +++ b/include/util/hex_checker.hpp @@ -2,9 +2,16 @@ #include +/** + * Result of transforming 8-bit hex codes to rgba(). + */ +struct TransformResult { + std::string css; + bool was_transformed; +}; + /** * Reads a CSS file, searches for 8-bit hex codes (#RRGGBBAA), * and transforms them into GTK-compatible rgba() syntax. */ - -std::pair transform_8bit_to_hex(const std::string& file_path); +TransformResult transform_8bit_to_hex(const std::string& file_path);