From cc6b3234b2966acd61c8a2e5caae947774666601 Mon Sep 17 00:00:00 2001 From: vaxerski <43317083+vaxerski@users.noreply.github.com> Date: Fri, 31 Mar 2023 17:41:40 +0100 Subject: [PATCH] clang-format, no-zoom and render-inactive --- .clang-format | 65 ++++++++++++++++++++ src/events/Events.cpp | 139 +++++++++++++++++------------------------- src/hyprpicker.cpp | 139 +++++++++++++++++++++++------------------- src/hyprpicker.hpp | 66 ++++++++++---------- src/main.cpp | 53 ++++++++-------- 5 files changed, 259 insertions(+), 203 deletions(-) create mode 100644 .clang-format diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..90314ef --- /dev/null +++ b/.clang-format @@ -0,0 +1,65 @@ +--- +Language: Cpp +BasedOnStyle: LLVM + +AccessModifierOffset: -2 +AlignAfterOpenBracket: Align +AlignConsecutiveMacros: true +AlignConsecutiveAssignments: true +AlignEscapedNewlines: Right +AlignOperands: false +AlignTrailingComments: true +AllowAllArgumentsOnNextLine: true +AllowAllConstructorInitializersOnNextLine: true +AllowAllParametersOfDeclarationOnNextLine: true +AllowShortBlocksOnASingleLine: true +AllowShortCaseLabelsOnASingleLine: true +AllowShortFunctionsOnASingleLine: Empty +AllowShortIfStatementsOnASingleLine: Never +AllowShortLambdasOnASingleLine: All +AllowShortLoopsOnASingleLine: false +AlwaysBreakAfterDefinitionReturnType: None +AlwaysBreakAfterReturnType: None +AlwaysBreakBeforeMultilineStrings: false +AlwaysBreakTemplateDeclarations: Yes +BreakBeforeBraces: Attach +BreakBeforeTernaryOperators: false +BreakConstructorInitializers: AfterColon +ColumnLimit: 180 +CompactNamespaces: false +ConstructorInitializerAllOnOneLineOrOnePerLine: false +ExperimentalAutoDetectBinPacking: false +FixNamespaceComments: false +IncludeBlocks: Preserve +IndentCaseLabels: true +IndentWidth: 4 +PointerAlignment: Left +ReflowComments: false +SortIncludes: false +SortUsingDeclarations: false +SpaceAfterCStyleCast: false +SpaceAfterLogicalNot: false +SpaceAfterTemplateKeyword: true +SpaceBeforeCtorInitializerColon: true +SpaceBeforeInheritanceColon: true +SpaceBeforeParens: ControlStatements +SpaceBeforeRangeBasedForLoopColon: true +SpaceInEmptyParentheses: false +SpacesBeforeTrailingComments: 1 +SpacesInAngles: false +SpacesInCStyleCastParentheses: false +SpacesInContainerLiterals: false +SpacesInParentheses: false +SpacesInSquareBrackets: false +Standard: Auto +TabWidth: 4 +UseTab: Never + +AllowShortEnumsOnASingleLine: false + +BraceWrapping: + AfterEnum: false + +AlignConsecutiveDeclarations: AcrossEmptyLines + +NamespaceIndentation: All diff --git a/src/events/Events.cpp b/src/events/Events.cpp index cc781cb..d5eedb1 100644 --- a/src/events/Events.cpp +++ b/src/events/Events.cpp @@ -1,65 +1,67 @@ #include "Events.hpp" + #include "../hyprpicker.hpp" -void Events::geometry(void *data, wl_output *output, int32_t x, int32_t y, int32_t width_mm, int32_t height_mm, int32_t subpixel, const char *make, const char *model, int32_t transform) { +void Events::geometry(void* data, wl_output* output, int32_t x, int32_t y, int32_t width_mm, int32_t height_mm, int32_t subpixel, const char* make, const char* model, + int32_t transform) { // ignored } -void Events::mode(void *data, wl_output *output, uint32_t flags, int32_t width, int32_t height, int32_t refresh) { +void Events::mode(void* data, wl_output* output, uint32_t flags, int32_t width, int32_t height, int32_t refresh) { // ignored } -void Events::done(void *data, wl_output *wl_output) { +void Events::done(void* data, wl_output* wl_output) { const auto PMONITOR = (SMonitor*)data; PMONITOR->ready = true; } -void Events::scale(void *data, wl_output *wl_output, int32_t scale) { +void Events::scale(void* data, wl_output* wl_output, int32_t scale) { const auto PMONITOR = (SMonitor*)data; PMONITOR->scale = scale; } -void Events::name(void *data, wl_output *wl_output, const char *name) { +void Events::name(void* data, wl_output* wl_output, const char* name) { const auto PMONITOR = (SMonitor*)data; if (name) PMONITOR->name = name; } -void Events::description(void *data, wl_output *wl_output, const char *description) { +void Events::description(void* data, wl_output* wl_output, const char* description) { // i do not care } -void Events::ls_configure(void *data, zwlr_layer_surface_v1 *surface, uint32_t serial, uint32_t width, uint32_t height) { - const auto PLAYERSURFACE = (CLayerSurface *)data; +void Events::ls_configure(void* data, zwlr_layer_surface_v1* surface, uint32_t serial, uint32_t width, uint32_t height) { + const auto PLAYERSURFACE = (CLayerSurface*)data; PLAYERSURFACE->m_pMonitor->size = Vector2D(width, height); - PLAYERSURFACE->ACKSerial = serial; - PLAYERSURFACE->wantsACK = true; - PLAYERSURFACE->working = true; + PLAYERSURFACE->ACKSerial = serial; + PLAYERSURFACE->wantsACK = true; + PLAYERSURFACE->working = true; g_pHyprpicker->recheckACK(); } -void Events::handleGlobal(void *data, struct wl_registry *registry, uint32_t name, const char *interface, uint32_t version) { +void Events::handleGlobal(void* data, struct wl_registry* registry, uint32_t name, const char* interface, uint32_t version) { if (strcmp(interface, wl_compositor_interface.name) == 0) { - g_pHyprpicker->m_pCompositor = (wl_compositor *)wl_registry_bind(registry, name, &wl_compositor_interface, 4); + g_pHyprpicker->m_pCompositor = (wl_compositor*)wl_registry_bind(registry, name, &wl_compositor_interface, 4); } else if (strcmp(interface, wl_shm_interface.name) == 0) { - g_pHyprpicker->m_pWLSHM = (wl_shm *)wl_registry_bind(registry, name, &wl_shm_interface, 1); + g_pHyprpicker->m_pWLSHM = (wl_shm*)wl_registry_bind(registry, name, &wl_shm_interface, 1); } else if (strcmp(interface, wl_output_interface.name) == 0) { g_pHyprpicker->m_mtTickMutex.lock(); - const auto PMONITOR = g_pHyprpicker->m_vMonitors.emplace_back(std::make_unique()).get(); + const auto PMONITOR = g_pHyprpicker->m_vMonitors.emplace_back(std::make_unique()).get(); PMONITOR->wayland_name = name; - PMONITOR->name = ""; - PMONITOR->output = (wl_output *)wl_registry_bind(registry, name, &wl_output_interface, 4); + PMONITOR->name = ""; + PMONITOR->output = (wl_output*)wl_registry_bind(registry, name, &wl_output_interface, 4); wl_output_add_listener(PMONITOR->output, &Events::outputListener, PMONITOR); g_pHyprpicker->m_mtTickMutex.unlock(); } else if (strcmp(interface, zwlr_layer_shell_v1_interface.name) == 0) { - g_pHyprpicker->m_pLayerShell = (zwlr_layer_shell_v1 *)wl_registry_bind(registry, name, &zwlr_layer_shell_v1_interface, 1); + g_pHyprpicker->m_pLayerShell = (zwlr_layer_shell_v1*)wl_registry_bind(registry, name, &zwlr_layer_shell_v1_interface, 1); } else if (strcmp(interface, wl_seat_interface.name) == 0) { g_pHyprpicker->createSeat((wl_seat*)wl_registry_bind(registry, name, &wl_seat_interface, 1)); } else if (strcmp(interface, zwlr_screencopy_manager_v1_interface.name) == 0) { @@ -67,11 +69,11 @@ void Events::handleGlobal(void *data, struct wl_registry *registry, uint32_t nam } } -void Events::handleGlobalRemove(void *data, struct wl_registry *registry, uint32_t name) { +void Events::handleGlobalRemove(void* data, struct wl_registry* registry, uint32_t name) { // todo } -void Events::handleCapabilities(void *data, wl_seat *wl_seat, uint32_t capabilities) { +void Events::handleCapabilities(void* data, wl_seat* wl_seat, uint32_t capabilities) { if (capabilities & WL_SEAT_CAPABILITY_POINTER) { wl_pointer_add_listener(wl_seat_get_pointer(wl_seat), &pointerListener, wl_seat); } else { @@ -84,7 +86,7 @@ void Events::handleCapabilities(void *data, wl_seat *wl_seat, uint32_t capabilit } } -void Events::handlePointerEnter(void *data, struct wl_pointer *wl_pointer, uint32_t serial, struct wl_surface *surface, wl_fixed_t surface_x, wl_fixed_t surface_y) { +void Events::handlePointerEnter(void* data, struct wl_pointer* wl_pointer, uint32_t serial, struct wl_surface* surface, wl_fixed_t surface_x, wl_fixed_t surface_y) { g_pHyprpicker->markDirty(); for (auto& ls : g_pHyprpicker->m_vLayerSurfaces) { @@ -99,7 +101,7 @@ void Events::handlePointerEnter(void *data, struct wl_pointer *wl_pointer, uint3 } } -void Events::handlePointerLeave(void *data, struct wl_pointer *wl_pointer, uint32_t serial, struct wl_surface *surface) { +void Events::handlePointerLeave(void* data, struct wl_pointer* wl_pointer, uint32_t serial, struct wl_surface* surface) { for (auto& ls : g_pHyprpicker->m_vLayerSurfaces) { if (ls->pSurface == surface) { g_pHyprpicker->renderSurface(ls.get(), true); @@ -107,12 +109,11 @@ void Events::handlePointerLeave(void *data, struct wl_pointer *wl_pointer, uint3 } } -void Events::handlePointerAxis(void *data, wl_pointer *wl_pointer, uint32_t time, uint32_t axis, wl_fixed_t value) { +void Events::handlePointerAxis(void* data, wl_pointer* wl_pointer, uint32_t time, uint32_t axis, wl_fixed_t value) { // ignored } -void Events::handlePointerMotion(void *data, struct wl_pointer *wl_pointer, uint32_t time, wl_fixed_t surface_x, wl_fixed_t surface_y) { - +void Events::handlePointerMotion(void* data, struct wl_pointer* wl_pointer, uint32_t time, wl_fixed_t surface_x, wl_fixed_t surface_y) { auto x = wl_fixed_to_double(surface_x); auto y = wl_fixed_to_double(surface_y); @@ -121,37 +122,26 @@ void Events::handlePointerMotion(void *data, struct wl_pointer *wl_pointer, uint g_pHyprpicker->markDirty(); } -void Events::handlePointerButton(void *data, struct wl_pointer *wl_pointer, uint32_t serial, uint32_t time, uint32_t button, uint32_t button_state) { - auto fmax3 = [](float a, float b, float c) -> float { - return (a > b && a > c) ? a : (b > c) ? b : c; - }; - auto fmin3 = [](float a, float b, float c) -> float { - return (a < b && a < c) ? a : (b < c) ? b : c; - }; +void Events::handlePointerButton(void* data, struct wl_pointer* wl_pointer, uint32_t serial, uint32_t time, uint32_t button, uint32_t button_state) { + auto fmax3 = [](float a, float b, float c) -> float { return (a > b && a > c) ? a : (b > c) ? b : c; }; + auto fmin3 = [](float a, float b, float c) -> float { return (a < b && a < c) ? a : (b < c) ? b : c; }; // get the px and print it const auto SCALE = Vector2D{ g_pHyprpicker->m_pLastSurface->screenBuffer.pixelSize.x / (g_pHyprpicker->m_pLastSurface->buffers[0].pixelSize.x / g_pHyprpicker->m_pLastSurface->m_pMonitor->scale), - g_pHyprpicker->m_pLastSurface->screenBuffer.pixelSize.y / (g_pHyprpicker->m_pLastSurface->buffers[0].pixelSize.y / g_pHyprpicker->m_pLastSurface->m_pMonitor->scale) - }; + g_pHyprpicker->m_pLastSurface->screenBuffer.pixelSize.y / (g_pHyprpicker->m_pLastSurface->buffers[0].pixelSize.y / g_pHyprpicker->m_pLastSurface->m_pMonitor->scale)}; const auto CLICKPOS = Vector2D{g_pHyprpicker->m_vLastCoords.floor().x * SCALE.x, g_pHyprpicker->m_vLastCoords.floor().y * SCALE.y}; const auto COL = g_pHyprpicker->getColorFromPixel(g_pHyprpicker->m_pLastSurface, CLICKPOS); switch (g_pHyprpicker->m_bSelectedOutputMode) { - case OUTPUT_CMYK: - { + case OUTPUT_CMYK: { // http://www.codeproject.com/KB/applications/xcmyk.aspx - float r = 1 - COL.r / 255.0f, - g = 1 - COL.g / 255.0f, - b = 1 - COL.b / 255.0f; - float k = fmin3(r, g, b), - K = 1 - k; - float c = (r - k) / K, - m = (g - k) / K, - y = (b - k) / K; + float r = 1 - COL.r / 255.0f, g = 1 - COL.g / 255.0f, b = 1 - COL.b / 255.0f; + float k = fmin3(r, g, b), K = 1 - k; + float c = (r - k) / K, m = (g - k) / K, y = (b - k) / K; c = std::round(c * 100); m = std::round(m * 100); @@ -168,8 +158,7 @@ void Events::handlePointerButton(void *data, struct wl_pointer *wl_pointer, uint g_pHyprpicker->finish(); break; } - case OUTPUT_HEX: - { + case OUTPUT_HEX: { auto toHex = [](int i) -> std::string { const char* DS = "0123456789ABCDEF"; @@ -191,8 +180,7 @@ void Events::handlePointerButton(void *data, struct wl_pointer *wl_pointer, uint g_pHyprpicker->finish(); break; } - case OUTPUT_RGB: - { + case OUTPUT_RGB: { if (g_pHyprpicker->m_bFancyOutput) Debug::log(NONE, "\033[38;2;%i;%i;%im%i %i %i\033[0m", COL.r, COL.g, COL.b, COL.r, COL.g, COL.b); else @@ -204,20 +192,16 @@ void Events::handlePointerButton(void *data, struct wl_pointer *wl_pointer, uint break; } case OUTPUT_HSL: - case OUTPUT_HSV: - { + case OUTPUT_HSV: { // https://en.wikipedia.org/wiki/HSL_and_HSV#From_RGB auto floatEq = [](float a, float b) -> bool { - return std::nextafter(a, std::numeric_limits::lowest()) <= b && std::nextafter(a, std::numeric_limits::max()) >= b; + return std::nextafter(a, std::numeric_limits::lowest()) <= b && std::nextafter(a, std::numeric_limits::max()) >= b; }; float h, s, l, v; - float r = COL.r / 255.0f, - g = COL.g / 255.0f, - b = COL.b / 255.0f; - float max = fmax3(r, g, b), - min = fmin3(r, g, b); + float r = COL.r / 255.0f, g = COL.g / 255.0f, b = COL.b / 255.0f; + float max = fmax3(r, g, b), min = fmin3(r, g, b); float c = max - min; v = max; @@ -232,12 +216,12 @@ void Events::handlePointerButton(void *data, struct wl_pointer *wl_pointer, uint float l_or_v; if (g_pHyprpicker->m_bSelectedOutputMode == OUTPUT_HSL) { - l = (max + min) / 2; - s = (floatEq(l, 0.0f) || floatEq(l, 1.0f)) ? 0 : (v - l) / std::min(l, 1 - l); + l = (max + min) / 2; + s = (floatEq(l, 0.0f) || floatEq(l, 1.0f)) ? 0 : (v - l) / std::min(l, 1 - l); l_or_v = std::round(l * 100); } else { - v = max; - s = floatEq(v, 0.0f) ? 0 : c / v; + v = max; + s = floatEq(v, 0.0f) ? 0 : c / v; l_or_v = std::round(v * 100); } @@ -259,29 +243,20 @@ void Events::handlePointerButton(void *data, struct wl_pointer *wl_pointer, uint g_pHyprpicker->finish(); } -void Events::handleKeyboardKeymap(void* data, wl_keyboard* wl_keyboard, uint format, int fd, uint size) { +void Events::handleKeyboardKeymap(void* data, wl_keyboard* wl_keyboard, uint format, int fd, uint size) {} -} - -void Events::handleKeyboardKey(void *data, struct wl_keyboard *keyboard, uint32_t serial, uint32_t time, uint32_t key, uint32_t state) { +void Events::handleKeyboardKey(void* data, struct wl_keyboard* keyboard, uint32_t serial, uint32_t time, uint32_t key, uint32_t state) { if (key == 1) // escape g_pHyprpicker->finish(); } -void Events::handleKeyboardEnter(void* data, wl_keyboard* wl_keyboard, uint serial, wl_surface* surface, wl_array* keys) { +void Events::handleKeyboardEnter(void* data, wl_keyboard* wl_keyboard, uint serial, wl_surface* surface, wl_array* keys) {} -} +void Events::handleKeyboardLeave(void* data, wl_keyboard* wl_keyboard, uint serial, wl_surface* surface) {} -void Events::handleKeyboardLeave(void* data, wl_keyboard* wl_keyboard, uint serial, wl_surface* surface) { +void Events::handleKeyboardModifiers(void* data, wl_keyboard* wl_keyboard, uint serial, uint mods_depressed, uint mods_latched, uint mods_locked, uint group) {} -} - -void Events::handleKeyboardModifiers(void* data, wl_keyboard* wl_keyboard, uint serial, uint mods_depressed, uint mods_latched, uint mods_locked, uint group) { - -} - - -void Events::handleFrameDone(void *data, struct wl_callback *callback, uint32_t time) { +void Events::handleFrameDone(void* data, struct wl_callback* callback, uint32_t time) { CLayerSurface* pLS = (CLayerSurface*)data; if (pLS->frame_callback) @@ -293,12 +268,12 @@ void Events::handleFrameDone(void *data, struct wl_callback *callback, uint32_t g_pHyprpicker->renderSurface(g_pHyprpicker->m_pLastSurface); } -void Events::handleBufferRelease(void *data, struct wl_buffer *wl_buffer) { - auto buf = (SPoolBuffer*)data; +void Events::handleBufferRelease(void* data, struct wl_buffer* wl_buffer) { + auto buf = (SPoolBuffer*)data; buf->busy = false; } -void Events::handleSCBuffer(void *data, struct zwlr_screencopy_frame_v1 *frame, uint32_t format, uint32_t width, uint32_t height, uint32_t stride) { +void Events::handleSCBuffer(void* data, struct zwlr_screencopy_frame_v1* frame, uint32_t format, uint32_t width, uint32_t height, uint32_t stride) { const auto PLS = (CLayerSurface*)data; g_pHyprpicker->createBuffer(&PLS->screenBuffer, width, height, format, stride); @@ -306,8 +281,8 @@ void Events::handleSCBuffer(void *data, struct zwlr_screencopy_frame_v1 *frame, zwlr_screencopy_frame_v1_copy(frame, PLS->screenBuffer.buffer); } -void Events::handleSCFlags(void *data, struct zwlr_screencopy_frame_v1 *frame, uint32_t flags) { - const auto PLS = (CLayerSurface *)data; +void Events::handleSCFlags(void* data, struct zwlr_screencopy_frame_v1* frame, uint32_t flags) { + const auto PLS = (CLayerSurface*)data; PLS->scflags = flags; @@ -316,11 +291,11 @@ void Events::handleSCFlags(void *data, struct zwlr_screencopy_frame_v1 *frame, u g_pHyprpicker->renderSurface(PLS); } -void Events::handleSCReady(void *data, struct zwlr_screencopy_frame_v1 *frame, uint32_t tv_sec_hi, uint32_t tv_sec_lo, uint32_t tv_nsec) { +void Events::handleSCReady(void* data, struct zwlr_screencopy_frame_v1* frame, uint32_t tv_sec_hi, uint32_t tv_sec_lo, uint32_t tv_nsec) { // ignore } -void Events::handleSCFailed(void *data, struct zwlr_screencopy_frame_v1 *frame) { +void Events::handleSCFailed(void* data, struct zwlr_screencopy_frame_v1* frame) { Debug::log(CRIT, "Failed to get a Screencopy!"); g_pHyprpicker->finish(1); } diff --git a/src/hyprpicker.cpp b/src/hyprpicker.cpp index bc56a4e..8ff968d 100644 --- a/src/hyprpicker.cpp +++ b/src/hyprpicker.cpp @@ -65,8 +65,10 @@ void CHyprpicker::recheckACK() { zwlr_layer_surface_v1_ack_configure(ls->pLayerSurface, ls->ACKSerial); if (!ls->buffers[0].buffer) { - createBuffer(&ls->buffers[0], ls->m_pMonitor->size.x * ls->m_pMonitor->scale, ls->m_pMonitor->size.y * ls->m_pMonitor->scale, WL_SHM_FORMAT_ARGB8888, ls->m_pMonitor->size.x * ls->m_pMonitor->scale * 4); - createBuffer(&ls->buffers[1], ls->m_pMonitor->size.x * ls->m_pMonitor->scale, ls->m_pMonitor->size.y * ls->m_pMonitor->scale, WL_SHM_FORMAT_ARGB8888, ls->m_pMonitor->size.x * ls->m_pMonitor->scale * 4); + createBuffer(&ls->buffers[0], ls->m_pMonitor->size.x * ls->m_pMonitor->scale, ls->m_pMonitor->size.y * ls->m_pMonitor->scale, WL_SHM_FORMAT_ARGB8888, + ls->m_pMonitor->size.x * ls->m_pMonitor->scale * 4); + createBuffer(&ls->buffers[1], ls->m_pMonitor->size.x * ls->m_pMonitor->scale, ls->m_pMonitor->size.y * ls->m_pMonitor->scale, WL_SHM_FORMAT_ARGB8888, + ls->m_pMonitor->size.x * ls->m_pMonitor->scale * 4); int XCURSOR_SIZE = 24; if (getenv("XCURSOR_SIZE")) { @@ -158,8 +160,8 @@ int CHyprpicker::createPoolFile(size_t size, std::string& name) { void CHyprpicker::createBuffer(SPoolBuffer* pBuffer, int32_t w, int32_t h, uint32_t format, uint32_t stride) { const size_t SIZE = stride * h; - std::string name; - const auto FD = createPoolFile(SIZE, name); + std::string name; + const auto FD = createPoolFile(SIZE, name); if (FD == -1) { Debug::log(CRIT, "Unable to create pool file!"); @@ -176,12 +178,12 @@ void CHyprpicker::createBuffer(SPoolBuffer* pBuffer, int32_t w, int32_t h, uint3 close(FD); - pBuffer->format = format; - pBuffer->size = SIZE; - pBuffer->data = DATA; + pBuffer->format = format; + pBuffer->size = SIZE; + pBuffer->data = DATA; pBuffer->pixelSize = Vector2D(w, h); - pBuffer->name = name; - pBuffer->stride = stride; + pBuffer->name = name; + pBuffer->stride = stride; } void CHyprpicker::destroyBuffer(SPoolBuffer* pBuffer) { @@ -190,8 +192,8 @@ void CHyprpicker::destroyBuffer(SPoolBuffer* pBuffer) { cairo_surface_destroy(pBuffer->surface); munmap(pBuffer->data, pBuffer->size); - pBuffer->buffer = nullptr; - pBuffer->cairo = nullptr; + pBuffer->buffer = nullptr; + pBuffer->cairo = nullptr; pBuffer->surface = nullptr; unlink(pBuffer->name.c_str()); @@ -208,8 +210,7 @@ void CHyprpicker::createSeat(wl_seat* pSeat) { void CHyprpicker::convertBuffer(SPoolBuffer* pBuffer) { switch (pBuffer->format) { case WL_SHM_FORMAT_ARGB8888: - case WL_SHM_FORMAT_XRGB8888: - break; + case WL_SHM_FORMAT_XRGB8888: break; case WL_SHM_FORMAT_ABGR8888: case WL_SHM_FORMAT_XBGR8888: { uint8_t* data = (uint8_t*)pBuffer->data; @@ -237,9 +238,9 @@ void CHyprpicker::convertBuffer(SPoolBuffer* pBuffer) { // Mallocs a new buffer, which needs to be free'd! void* convert24To32Buffer(SPoolBuffer* pBuffer) { - uint8_t* newBuffer = (uint8_t*)malloc((size_t)pBuffer->pixelSize.x * pBuffer->pixelSize.y * 4); - int newBufferStride = pBuffer->pixelSize.x * 4; - uint8_t* oldBuffer = (uint8_t*)pBuffer->data; + uint8_t* newBuffer = (uint8_t*)malloc((size_t)pBuffer->pixelSize.x * pBuffer->pixelSize.y * 4); + int newBufferStride = pBuffer->pixelSize.x * 4; + uint8_t* oldBuffer = (uint8_t*)pBuffer->data; switch (pBuffer->format) { case WL_SHM_FORMAT_BGR888: { @@ -258,11 +259,10 @@ void* convert24To32Buffer(SPoolBuffer* pBuffer) { unsigned char red; unsigned char alpha; }* dstPx = (struct pixel4*)(newBuffer + y * newBufferStride + x * 4); - *dstPx = {srcPx->red, srcPx->green, srcPx->blue, 0xFF}; + *dstPx = {srcPx->red, srcPx->green, srcPx->blue, 0xFF}; } } - } - break; + } break; case WL_SHM_FORMAT_RGB888: { for (int y = 0; y < pBuffer->pixelSize.y; ++y) { for (int x = 0; x < pBuffer->pixelSize.x; ++x) { @@ -279,15 +279,14 @@ void* convert24To32Buffer(SPoolBuffer* pBuffer) { unsigned char green; unsigned char blue; }* dstPx = (struct pixel4*)(newBuffer + y * newBufferStride + x * 4); - *dstPx = {0xFF, srcPx->red, srcPx->green, srcPx->blue}; + *dstPx = {0xFF, srcPx->red, srcPx->green, srcPx->blue}; } } - } - break; + } break; default: { Debug::log(CRIT, "Unsupported format for 24bit buffer %i", pBuffer->format); } - g_pHyprpicker->finish(1); + g_pHyprpicker->finish(1); } return newBuffer; } @@ -299,25 +298,25 @@ void CHyprpicker::renderSurface(CLayerSurface* pSurface, bool forceInactive) { return; if (!pSurface->screenBuffer.surface) { - int bytesPerPixel = pSurface->screenBuffer.stride / (int)pSurface->screenBuffer.pixelSize.x; - void* data = pSurface->screenBuffer.data; + int bytesPerPixel = pSurface->screenBuffer.stride / (int)pSurface->screenBuffer.pixelSize.x; + void* data = pSurface->screenBuffer.data; if (bytesPerPixel == 4) { convertBuffer(&pSurface->screenBuffer); - } - else if (bytesPerPixel == 3) { + } else if (bytesPerPixel == 3) { Debug::log(WARN, "24 bit formats are unsupported, hyprpicker may or may not work as intended!"); - data = convert24To32Buffer(&pSurface->screenBuffer); + data = convert24To32Buffer(&pSurface->screenBuffer); pSurface->screenBuffer.paddedData = data; - } - else { + } else { Debug::log(CRIT, "Unsupported stride/bytes per pixel %i", bytesPerPixel); g_pHyprpicker->finish(1); } - pSurface->screenBuffer.surface = cairo_image_surface_create_for_data((unsigned char*)data, CAIRO_FORMAT_ARGB32, pSurface->screenBuffer.pixelSize.x, pSurface->screenBuffer.pixelSize.y, pSurface->screenBuffer.pixelSize.x * 4); + pSurface->screenBuffer.surface = cairo_image_surface_create_for_data((unsigned char*)data, CAIRO_FORMAT_ARGB32, pSurface->screenBuffer.pixelSize.x, + pSurface->screenBuffer.pixelSize.y, pSurface->screenBuffer.pixelSize.x * 4); } - PBUFFER->surface = cairo_image_surface_create_for_data((unsigned char*)PBUFFER->data, CAIRO_FORMAT_ARGB32, pSurface->m_pMonitor->size.x * pSurface->m_pMonitor->scale, pSurface->m_pMonitor->size.y * pSurface->m_pMonitor->scale, PBUFFER->pixelSize.x * 4); - PBUFFER->cairo = cairo_create(PBUFFER->surface); + PBUFFER->surface = cairo_image_surface_create_for_data((unsigned char*)PBUFFER->data, CAIRO_FORMAT_ARGB32, pSurface->m_pMonitor->size.x * pSurface->m_pMonitor->scale, + pSurface->m_pMonitor->size.y * pSurface->m_pMonitor->scale, PBUFFER->pixelSize.x * 4); + PBUFFER->cairo = cairo_create(PBUFFER->surface); const auto PCAIRO = PBUFFER->cairo; @@ -328,7 +327,7 @@ void CHyprpicker::renderSurface(CLayerSurface* pSurface, bool forceInactive) { cairo_fill(PCAIRO); if (pSurface == g_pHyprpicker->m_pLastSurface && !forceInactive) { - const auto SCALEBUFS = Vector2D{pSurface->screenBuffer.pixelSize.x / PBUFFER->pixelSize.x, pSurface->screenBuffer.pixelSize.y / PBUFFER->pixelSize.y}; + const auto SCALEBUFS = Vector2D{pSurface->screenBuffer.pixelSize.x / PBUFFER->pixelSize.x, pSurface->screenBuffer.pixelSize.y / PBUFFER->pixelSize.y}; const auto SCALECURSOR = Vector2D{ g_pHyprpicker->m_pLastSurface->screenBuffer.pixelSize.x / (g_pHyprpicker->m_pLastSurface->buffers[0].pixelSize.x / g_pHyprpicker->m_pLastSurface->m_pMonitor->scale), g_pHyprpicker->m_pLastSurface->screenBuffer.pixelSize.y / (g_pHyprpicker->m_pLastSurface->buffers[0].pixelSize.y / g_pHyprpicker->m_pLastSurface->m_pMonitor->scale)}; @@ -358,54 +357,70 @@ void CHyprpicker::renderSurface(CLayerSurface* pSurface, bool forceInactive) { // cairo_restore(PCAIRO); - cairo_save(PCAIRO); + if (!g_pHyprpicker->m_bNoZoom) { + cairo_save(PCAIRO); - const auto PIXCOLOR = getColorFromPixel(pSurface, CLICKPOS); - cairo_set_source_rgba(PCAIRO, PIXCOLOR.r / 255.f, PIXCOLOR.g / 255.f, PIXCOLOR.b / 255.f, PIXCOLOR.a / 255.f); + const auto PIXCOLOR = getColorFromPixel(pSurface, CLICKPOS); + cairo_set_source_rgba(PCAIRO, PIXCOLOR.r / 255.f, PIXCOLOR.g / 255.f, PIXCOLOR.b / 255.f, PIXCOLOR.a / 255.f); - cairo_scale(PCAIRO, 1, 1); + cairo_scale(PCAIRO, 1, 1); - cairo_arc(PCAIRO, m_vLastCoords.x * pSurface->m_pMonitor->scale, m_vLastCoords.y * pSurface->m_pMonitor->scale, 105 / SCALEBUFS.x, 0, 2 * M_PI); - cairo_clip(PCAIRO); + cairo_arc(PCAIRO, m_vLastCoords.x * pSurface->m_pMonitor->scale, m_vLastCoords.y * pSurface->m_pMonitor->scale, 105 / SCALEBUFS.x, 0, 2 * M_PI); + cairo_clip(PCAIRO); - cairo_fill(PCAIRO); - cairo_paint(PCAIRO); + cairo_fill(PCAIRO); + cairo_paint(PCAIRO); - cairo_surface_flush(PBUFFER->surface); + cairo_surface_flush(PBUFFER->surface); - cairo_restore(PCAIRO); - cairo_save(PCAIRO); + cairo_restore(PCAIRO); + cairo_save(PCAIRO); - const auto PATTERN = cairo_pattern_create_for_surface(pSurface->screenBuffer.surface); - cairo_pattern_set_filter(PATTERN, CAIRO_FILTER_NEAREST); - cairo_matrix_t matrix; - cairo_matrix_init_identity(&matrix); - cairo_matrix_translate(&matrix, CLICKPOS.x + 0.5f, CLICKPOS.y + 0.5f); - cairo_matrix_scale(&matrix, 0.1f, 0.1f); - cairo_matrix_translate(&matrix, -CLICKPOS.x / SCALEBUFS.x - 0.5f, -CLICKPOS.y / SCALEBUFS.y - 0.5f); - cairo_pattern_set_matrix(PATTERN, &matrix); - cairo_set_source(PCAIRO, PATTERN); - cairo_arc(PCAIRO, m_vLastCoords.x * pSurface->m_pMonitor->scale, m_vLastCoords.y * pSurface->m_pMonitor->scale, 100 / SCALEBUFS.x, 0, 2 * M_PI); - cairo_clip(PCAIRO); - cairo_paint(PCAIRO); + const auto PATTERN = cairo_pattern_create_for_surface(pSurface->screenBuffer.surface); + cairo_pattern_set_filter(PATTERN, CAIRO_FILTER_NEAREST); + cairo_matrix_t matrix; + cairo_matrix_init_identity(&matrix); + cairo_matrix_translate(&matrix, CLICKPOS.x + 0.5f, CLICKPOS.y + 0.5f); + cairo_matrix_scale(&matrix, 0.1f, 0.1f); + cairo_matrix_translate(&matrix, -CLICKPOS.x / SCALEBUFS.x - 0.5f, -CLICKPOS.y / SCALEBUFS.y - 0.5f); + cairo_pattern_set_matrix(PATTERN, &matrix); + cairo_set_source(PCAIRO, PATTERN); + cairo_arc(PCAIRO, m_vLastCoords.x * pSurface->m_pMonitor->scale, m_vLastCoords.y * pSurface->m_pMonitor->scale, 100 / SCALEBUFS.x, 0, 2 * M_PI); + cairo_clip(PCAIRO); + cairo_paint(PCAIRO); - cairo_surface_flush(PBUFFER->surface); + cairo_surface_flush(PBUFFER->surface); - cairo_restore(PCAIRO); + cairo_restore(PCAIRO); - cairo_pattern_destroy(PATTERN); - } else { + cairo_pattern_destroy(PATTERN); + } + } else if (!g_pHyprpicker->m_bRenderInactive) { cairo_set_operator(PCAIRO, CAIRO_OPERATOR_SOURCE); cairo_set_source_rgba(PCAIRO, 0, 0, 0, 0); cairo_rectangle(PCAIRO, 0, 0, pSurface->m_pMonitor->size.x * pSurface->m_pMonitor->scale, pSurface->m_pMonitor->size.y * pSurface->m_pMonitor->scale); cairo_fill(PCAIRO); + } else { + const auto SCALEBUFS = Vector2D{pSurface->screenBuffer.pixelSize.x / PBUFFER->pixelSize.x, pSurface->screenBuffer.pixelSize.y / PBUFFER->pixelSize.y}; + const auto PATTERNPRE = cairo_pattern_create_for_surface(pSurface->screenBuffer.surface); + cairo_pattern_set_filter(PATTERNPRE, CAIRO_FILTER_BILINEAR); + cairo_matrix_t matrixPre; + cairo_matrix_init_identity(&matrixPre); + cairo_matrix_scale(&matrixPre, SCALEBUFS.x, SCALEBUFS.y); + cairo_pattern_set_matrix(PATTERNPRE, &matrixPre); + cairo_set_source(PCAIRO, PATTERNPRE); + cairo_paint(PCAIRO); + + cairo_surface_flush(PBUFFER->surface); + + cairo_pattern_destroy(PATTERNPRE); } sendFrame(pSurface); cairo_destroy(PCAIRO); cairo_surface_destroy(PBUFFER->surface); - PBUFFER->cairo = nullptr; + PBUFFER->cairo = nullptr; PBUFFER->surface = nullptr; pSurface->rendered = true; diff --git a/src/hyprpicker.hpp b/src/hyprpicker.hpp index 05ed689..214b9dc 100644 --- a/src/hyprpicker.hpp +++ b/src/hyprpicker.hpp @@ -4,7 +4,8 @@ #include "helpers/LayerSurface.hpp" #include "helpers/PoolBuffer.hpp" -enum eOutputMode { +enum eOutputMode +{ OUTPUT_CMYK = 0, OUTPUT_HEX, OUTPUT_RGB, @@ -13,57 +14,58 @@ enum eOutputMode { }; class CHyprpicker { -public: - void init(); + public: + void init(); - std::mutex m_mtTickMutex; + std::mutex m_mtTickMutex; - wl_compositor* m_pCompositor; - wl_display* m_pWLDisplay; - wl_registry* m_pWLRegistry; - wl_shm* m_pWLSHM; - zwlr_layer_shell_v1* m_pLayerShell; - zwlr_screencopy_manager_v1* m_pSCMgr; + wl_compositor* m_pCompositor; + wl_display* m_pWLDisplay; + wl_registry* m_pWLRegistry; + wl_shm* m_pWLSHM; + zwlr_layer_shell_v1* m_pLayerShell; + zwlr_screencopy_manager_v1* m_pSCMgr; - eOutputMode m_bSelectedOutputMode = OUTPUT_HEX; + eOutputMode m_bSelectedOutputMode = OUTPUT_HEX; - bool m_bFancyOutput = true; + bool m_bFancyOutput = true; - bool m_bAutoCopy = false; + bool m_bAutoCopy = false; + bool m_bRenderInactive = false; + bool m_bNoZoom = false; - bool m_bRunning = true; + bool m_bRunning = true; - std::vector> m_vMonitors; + std::vector> m_vMonitors; std::vector> m_vLayerSurfaces; - void createSeat(wl_seat*); + void createSeat(wl_seat*); - CLayerSurface* m_pLastSurface; + CLayerSurface* m_pLastSurface; - Vector2D m_vLastCoords; + Vector2D m_vLastCoords; - void renderSurface(CLayerSurface*, bool forceInactive = false); + void renderSurface(CLayerSurface*, bool forceInactive = false); - void createBuffer(SPoolBuffer*, int32_t, int32_t, uint32_t, uint32_t); - void destroyBuffer(SPoolBuffer*); - int createPoolFile(size_t, std::string&); - bool setCloexec(const int&); + void createBuffer(SPoolBuffer*, int32_t, int32_t, uint32_t, uint32_t); + void destroyBuffer(SPoolBuffer*); + int createPoolFile(size_t, std::string&); + bool setCloexec(const int&); + void recheckACK(); - void recheckACK(); + void sendFrame(CLayerSurface*); - void sendFrame(CLayerSurface*); + SPoolBuffer* getBufferForLS(CLayerSurface*); - SPoolBuffer* getBufferForLS(CLayerSurface*); + void convertBuffer(SPoolBuffer*); - void convertBuffer(SPoolBuffer*); + void markDirty(); - void markDirty(); + void finish(int code = 0); - void finish(int code = 0); + CColor getColorFromPixel(CLayerSurface*, Vector2D); - CColor getColorFromPixel(CLayerSurface*, Vector2D); -private: - + private: }; inline std::unique_ptr g_pHyprpicker; \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index bc9308f..788cbbe 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,29 +1,33 @@ #include + #include + #include "hyprpicker.hpp" static void help(void) { - 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" << - " -n | --no-fancy | Disables the \"fancy\" (aka. colored) outputting\n" << - " -h | --help | Show this help message\n"; + 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" + << " -n | --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"; } int main(int argc, char** argv, char** envp) { g_pHyprpicker = std::make_unique(); while (true) { - int option_index = 0; - static struct option long_options[] = { - {"autocopy", no_argument, NULL, 'a'}, - {"format", required_argument, NULL, 'f'}, - {"help", no_argument, NULL, 'h'}, - {"no-fancy", no_argument, NULL, 'n'}, - {NULL, 0, NULL, 0 } - }; + int option_index = 0; + static struct option long_options[] = {{"autocopy", no_argument, NULL, 'a'}, + {"format", required_argument, NULL, 'f'}, + {"help", no_argument, NULL, 'h'}, + {"no-fancy", no_argument, NULL, 'n'}, + {"render-inactive", no_argument, NULL, 'r'}, + {"no-zoom", no_argument, NULL, 'z'}, + {NULL, 0, NULL, 0}}; - int c = getopt_long(argc, argv, ":f:hna", long_options, &option_index); + int c = getopt_long(argc, argv, ":f:hnarz", long_options, &option_index); if (c == -1) break; @@ -44,19 +48,14 @@ int main(int argc, char** argv, char** envp) { exit(1); } break; - case 'h': - help(); - exit(0); - case 'n': - g_pHyprpicker->m_bFancyOutput = false; - break; - case 'a': - g_pHyprpicker->m_bAutoCopy = true; - break; - default: - help(); - exit(1); - } + case 'h': help(); exit(0); + case 'n': g_pHyprpicker->m_bFancyOutput = false; break; + case 'a': g_pHyprpicker->m_bAutoCopy = true; break; + case 'r': g_pHyprpicker->m_bRenderInactive = true; break; + case 'z': g_pHyprpicker->m_bNoZoom = true; break; + + default: help(); exit(1); + } } if (!isatty(fileno(stdout)) || getenv("NO_COLOR"))