From d8a9408bc6be7e8fe0a9dcba32a717969d89ef57 Mon Sep 17 00:00:00 2001 From: r3b00thx Date: Thu, 5 Jun 2025 16:36:29 +0300 Subject: [PATCH] refactor(codebase): refactor some of the c style code into cpp In this commit I moddified some naming conventions to algin better with cpp standards, I also moddified the clipboard function to use just a single argument instead of multiple. --- src/clipboard/Clipboard.cpp | 17 ++-------- src/clipboard/Clipboard.hpp | 7 ++-- src/hyprpicker.cpp | 64 ++++++++++++++++++------------------- src/main.cpp | 15 ++++----- src/notify/Notify.cpp | 2 +- src/notify/Notify.hpp | 2 +- 6 files changed, 48 insertions(+), 59 deletions(-) diff --git a/src/clipboard/Clipboard.cpp b/src/clipboard/Clipboard.cpp index 28390df..c7c65c0 100644 --- a/src/clipboard/Clipboard.cpp +++ b/src/clipboard/Clipboard.cpp @@ -1,20 +1,9 @@ #include "Clipboard.hpp" #include "../includes.hpp" +#include -void Clipboard::copy(const char* fmt, ...) { - char buf[CLIPBOARDMESSAGESIZE] = ""; - char* outputStr; - - va_list args; - va_start(args, fmt); - vsnprintf(buf, sizeof buf, fmt, args); - va_end(args); - - outputStr = strdup(buf); - +void NClipboard::copy(std::string color) { if (fork() == 0) - execlp("wl-copy", "wl-copy", outputStr, NULL); - - free(outputStr); + execlp("wl-copy", "wl-copy", color.c_str(), NULL); } diff --git a/src/clipboard/Clipboard.hpp b/src/clipboard/Clipboard.hpp index 1dc2be1..7e4e193 100644 --- a/src/clipboard/Clipboard.hpp +++ b/src/clipboard/Clipboard.hpp @@ -1,7 +1,8 @@ #pragma once +#include #define CLIPBOARDMESSAGESIZE 24 -namespace Clipboard { - void copy(const char* fmt, ...); -}; \ No newline at end of file +namespace NClipboard { + void copy(std::string color); +}; diff --git a/src/hyprpicker.cpp b/src/hyprpicker.cpp index 41bb3ee..ebe190f 100644 --- a/src/hyprpicker.cpp +++ b/src/hyprpicker.cpp @@ -1,10 +1,11 @@ #include "hyprpicker.hpp" #include "src/notify/Notify.hpp" #include +#include #include #include -void sigHandler(int sig) { +static void sigHandler(int sig) { g_pHyprpicker->m_vLayerSurfaces.clear(); exit(0); } @@ -245,13 +246,13 @@ void CHyprpicker::convertBuffer(SP pBuffer) { for (int y = 0; y < pBuffer->pixelSize.y; ++y) { for (int x = 0; x < pBuffer->pixelSize.x; ++x) { - struct pixel { + struct SPixel { // little-endian ARGB unsigned char blue; unsigned char green; unsigned char red; unsigned char alpha; - }* px = (struct pixel*)(data + (y * (int)pBuffer->pixelSize.x * 4) + (x * 4)); + }* px = (struct SPixel*)(data + (static_cast(y * (int)pBuffer->pixelSize.x * 4)) + (static_cast(x * 4))); std::swap(px->red, px->blue); } @@ -265,7 +266,7 @@ void CHyprpicker::convertBuffer(SP pBuffer) { for (int y = 0; y < pBuffer->pixelSize.y; ++y) { for (int x = 0; x < pBuffer->pixelSize.x; ++x) { - uint32_t* px = (uint32_t*)(data + (y * (int)pBuffer->pixelSize.x * 4) + (x * 4)); + uint32_t* px = (uint32_t*)(data + (static_cast(y * (int)pBuffer->pixelSize.x * 4)) + (static_cast(x * 4))); // conv to 8 bit uint8_t R = (uint8_t)std::round((255.0 * (((*px) & 0b00000000000000000000001111111111) >> 0) / 1023.0)); @@ -295,19 +296,19 @@ void* CHyprpicker::convert24To32Buffer(SP pBuffer) { case WL_SHM_FORMAT_BGR888: { for (int y = 0; y < pBuffer->pixelSize.y; ++y) { for (int x = 0; x < pBuffer->pixelSize.x; ++x) { - struct pixel3 { + struct SPixel3 { // little-endian RGB unsigned char blue; unsigned char green; unsigned char red; - }* srcPx = (struct pixel3*)(oldBuffer + (y * pBuffer->stride) + (x * 3)); - struct pixel4 { + }* srcPx = (struct SPixel3*)(oldBuffer + (static_cast(y * pBuffer->stride)) + (static_cast(x * 3))); + struct SPixel4 { // little-endian ARGB unsigned char blue; unsigned char green; unsigned char red; unsigned char alpha; - }* dstPx = (struct pixel4*)(newBuffer + (y * newBufferStride) + (x * 4)); + }* dstPx = (struct SPixel4*)(newBuffer + (static_cast(y * newBufferStride)) + (static_cast(x * 4))); *dstPx = {.blue = srcPx->red, .green = srcPx->green, .red = srcPx->blue, .alpha = 0xFF}; } } @@ -315,19 +316,19 @@ void* CHyprpicker::convert24To32Buffer(SP pBuffer) { case WL_SHM_FORMAT_RGB888: { for (int y = 0; y < pBuffer->pixelSize.y; ++y) { for (int x = 0; x < pBuffer->pixelSize.x; ++x) { - struct pixel3 { + struct SPixel3 { // big-endian RGB unsigned char red; unsigned char green; unsigned char blue; - }* srcPx = (struct pixel3*)(oldBuffer + (y * pBuffer->stride) + (x * 3)); - struct pixel4 { + }* srcPx = (struct SPixel3*)(oldBuffer + (y * pBuffer->stride) + (x * 3)); + struct SPixel4 { // big-endian ARGB unsigned char alpha; unsigned char red; unsigned char green; unsigned char blue; - }* dstPx = (struct pixel4*)(newBuffer + (y * newBufferStride) + (x * 4)); + }* dstPx = (struct SPixel4*)(newBuffer + (y * newBufferStride) + (x * 4)); *dstPx = {.alpha = 0xFF, .red = srcPx->red, .green = srcPx->green, .blue = srcPx->blue}; } } @@ -551,12 +552,13 @@ CColor CHyprpicker::getColorFromPixel(CLayerSurface* pLS, Vector2D pix) { return CColor{.r = 0, .g = 0, .b = 0, .a = 0}; void* dataSrc = pLS->screenBuffer->paddedData ? pLS->screenBuffer->paddedData : pLS->screenBuffer->data; - struct pixel { + + struct SPixel { unsigned char blue; unsigned char green; unsigned char red; unsigned char alpha; - }* px = (struct pixel*)((char*)dataSrc + ((ptrdiff_t)pix.y * (int)pLS->screenBuffer->pixelSize.x * 4) + ((ptrdiff_t)pix.x * 4)); + }* px = (struct SPixel*)((char*)dataSrc + ((ptrdiff_t)pix.y * (int)pLS->screenBuffer->pixelSize.x * 4) + ((ptrdiff_t)pix.x * 4)); return CColor{.r = px->red, .g = px->green, .b = px->blue, .a = px->alpha}; } @@ -675,28 +677,25 @@ void CHyprpicker::initMouse() { case OUTPUT_CMYK: { float c, m, y, k; COL.getCMYK(c, m, y, k); + + std::string formattedColor = std::format("{}% {}% {}% {}%", c, m, y, k); + if (m_bFancyOutput) Debug::log(NONE, "\033[38;2;%i;%i;%i;48;2;%i;%i;%im%g%% %g%% %g%% %g%%\033[0m", FG, FG, FG, COL.r, COL.g, COL.b, c, m, y, k); else Debug::log(NONE, "%g%% %g%% %g%% %g%%", c, m, y, k); if (m_bAutoCopy) - Clipboard::copy("%g%% %g%% %g%% %g%%", c, m, y, k); + NClipboard::copy(formattedColor); if (m_bNotify) { - std::string formattedColor = std::format("cmyk({}%, {}%, {}%, {}%)", c, m, y, k); - - Notify::send(hexColor, formattedColor); + NNotify::send(hexColor, formattedColor); } finish(); break; } case OUTPUT_HEX: { - auto hexR = toHex(COL.r); - auto hexG = toHex(COL.g); - auto hexB = toHex(COL.b); - if (m_bFancyOutput) Debug::log(NONE, "\033[38;2;%i;%i;%i;48;2;%i;%i;%im#%s%s%s\033[0m", FG, FG, FG, COL.r, COL.g, COL.b, toHex(COL.r).c_str(), toHex(COL.g).c_str(), toHex(COL.b).c_str()); @@ -704,28 +703,28 @@ void CHyprpicker::initMouse() { Debug::log(NONE, "#%s%s%s", toHex(COL.r).c_str(), toHex(COL.g).c_str(), toHex(COL.b).c_str()); if (m_bAutoCopy) - Clipboard::copy("#%s%s%s", toHex(COL.r).c_str(), toHex(COL.g).c_str(), toHex(COL.b).c_str()); + NClipboard::copy(hexColor); if (m_bNotify) { - Notify::send(hexColor, hexColor); + NNotify::send(hexColor, hexColor); } finish(); break; } case OUTPUT_RGB: { + std::string formattedColor = std::format("{} {} {}", COL.r, COL.g, COL.b); + if (m_bFancyOutput) Debug::log(NONE, "\033[38;2;%i;%i;%i;48;2;%i;%i;%im%i %i %i\033[0m", FG, FG, FG, COL.r, COL.g, COL.b, COL.r, COL.g, COL.b); else Debug::log(NONE, "%i %i %i", COL.r, COL.g, COL.b); if (m_bAutoCopy) - Clipboard::copy("%i %i %i", COL.r, COL.g, COL.b); + NClipboard::copy(formattedColor); if (m_bNotify) { - std::string formattedColor = std::format("rgb({}, {}, {})", COL.r, COL.g, COL.b); - - Notify::send(hexColor, formattedColor); + NNotify::send(hexColor, formattedColor); } finish(); @@ -738,18 +737,19 @@ void CHyprpicker::initMouse() { COL.getHSV(h, s, l_or_v); else COL.getHSL(h, s, l_or_v); + + std::string formattedColor = std::format("{} {}% {}%", h, s, l_or_v); + if (m_bFancyOutput) Debug::log(NONE, "\033[38;2;%i;%i;%i;48;2;%i;%i;%im%g %g%% %g%%\033[0m", FG, FG, FG, COL.r, COL.g, COL.b, h, s, l_or_v); else Debug::log(NONE, "%g %g%% %g%%", h, s, l_or_v); if (m_bAutoCopy) - Clipboard::copy("%g %g%% %g%%", h, s, l_or_v); + NClipboard::copy(formattedColor); if (m_bNotify) { - std::string formattedColor = std::format("hsl({}, {}%, {}%)", h, s, l_or_v); - - Notify::send(hexColor, formattedColor); + NNotify::send(hexColor, formattedColor); } finish(); diff --git a/src/main.cpp b/src/main.cpp index 4c5bb72..764c623 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -8,8 +8,8 @@ static void help() { std::cout << "Hyprpicker usage: hyprpicker [arg [...]].\n\nArguments:\n" << " -a | --autocopy | Automatically copies the output to the clipboard (requires wl-clipboard)\n" << " -f | --format=fmt | Specifies the output format (cmyk, hex, rgb, hsl, hsv)\n" - << " -j | --notify | Sends a desktop notification when a color is picked (requires notify-send and a notification daemon like dunst)\n" - << " -n | --no-fancy | Disables the \"fancy\" (aka. colored) outputting\n" + << " -n | --notify | Sends a desktop notification when a color is picked (requires notify-send and a notification daemon like dunst)\n" + << " -b | --no-fancy | Disables the \"fancy\" (aka. colored) outputting\n" << " -h | --help | Show this help message\n" << " -r | --render-inactive | Render (freeze) inactive displays\n" << " -z | --no-zoom | Disable the zoom lens\n" @@ -29,8 +29,8 @@ int main(int argc, char** argv, char** envp) { static struct option long_options[] = {{"autocopy", no_argument, nullptr, 'a'}, {"format", required_argument, nullptr, 'f'}, {"help", no_argument, nullptr, 'h'}, - {"no-fancy", no_argument, nullptr, 'n'}, - {"notify", no_argument, nullptr, 'j'}, + {"notify", no_argument, nullptr, 'n'}, + {"no-fancy", no_argument, nullptr, 'b'}, {"render-inactive", no_argument, nullptr, 'r'}, {"no-zoom", no_argument, nullptr, 'z'}, {"no-fractional", no_argument, nullptr, 't'}, @@ -41,7 +41,7 @@ int main(int argc, char** argv, char** envp) { {"version", no_argument, nullptr, 'V'}, {nullptr, 0, nullptr, 0}}; - int c = getopt_long(argc, argv, ":f:hnjarzqvtdlV", long_options, &option_index); + int c = getopt_long(argc, argv, ":f:hnbarzqvtdlV", long_options, &option_index); if (c == -1) break; @@ -63,9 +63,8 @@ int main(int argc, char** argv, char** envp) { } break; case 'h': help(); exit(0); - case 'n': g_pHyprpicker->m_bFancyOutput = false; break; - case 'j': g_pHyprpicker->m_bNotify = true; break; - case 'a': g_pHyprpicker->m_bAutoCopy = true; break; + case 'n': g_pHyprpicker->m_bNotify = true; break; + case 'b': g_pHyprpicker->m_bFancyOutput = false; break; case 'r': g_pHyprpicker->m_bRenderInactive = true; break; case 'z': g_pHyprpicker->m_bNoZoom = true; break; case 't': g_pHyprpicker->m_bNoFractional = true; break; diff --git a/src/notify/Notify.cpp b/src/notify/Notify.cpp index 1847b61..dbd3b24 100644 --- a/src/notify/Notify.cpp +++ b/src/notify/Notify.cpp @@ -7,7 +7,7 @@ #include #include -void Notify::send(std::string hexColor, std::string formattedColor) { +void NNotify::send(std::string hexColor, std::string formattedColor) { std::string bodyString = std::format("You selected the color: {}", hexColor, formattedColor); if (fork() == 0) diff --git a/src/notify/Notify.hpp b/src/notify/Notify.hpp index 489daf7..79a7271 100644 --- a/src/notify/Notify.hpp +++ b/src/notify/Notify.hpp @@ -4,6 +4,6 @@ #include #define NOTIFYBODYSIZE 128 -namespace Notify { +namespace NNotify { void send(std::string hexColor, std::string formattedColor); }