diff --git a/.clang-tidy b/.clang-tidy new file mode 100644 index 0000000..f0dfdf1 --- /dev/null +++ b/.clang-tidy @@ -0,0 +1,101 @@ +WarningsAsErrors: '*' +HeaderFilterRegex: '.*\.hpp' +FormatStyle: file +Checks: > + -*, + bugprone-*, + -bugprone-easily-swappable-parameters, + -bugprone-forward-declararion-namespace, + -bugprone-forward-declararion-namespace, + -bugprone-macro-parentheses, + -bugprone-narrowing-conversions, + -bugprone-branch-clone, + -bugprone-assignment-in-if-condition, + concurrency-*, + -concurrency-mt-unsafe, + cppcoreguidelines-*, + -cppcoreguidelines-owning-memory, + -cppcoreguidelines-avoid-magic-numbers, + -cppcoreguidelines-pro-bounds-constant-array-index, + -cppcoreguidelines-avoid-const-or-ref-data-members, + -cppcoreguidelines-non-private-member-variables-in-classes, + -cppcoreguidelines-avoid-goto, + -cppcoreguidelines-pro-bounds-array-to-pointer-decay, + -cppcoreguidelines-avoid-do-while, + -cppcoreguidelines-avoid-non-const-global-variables, + -cppcoreguidelines-special-member-functions, + -cppcoreguidelines-explicit-virtual-functions, + -cppcoreguidelines-avoid-c-arrays, + -cppcoreguidelines-pro-bounds-pointer-arithmetic, + -cppcoreguidelines-narrowing-conversions, + -cppcoreguidelines-pro-type-union-access, + -cppcoreguidelines-pro-type-member-init, + -cppcoreguidelines-macro-usage, + -cppcoreguidelines-macro-to-enum, + -cppcoreguidelines-init-variables, + -cppcoreguidelines-pro-type-cstyle-cast, + -cppcoreguidelines-pro-type-vararg, + -cppcoreguidelines-pro-type-reinterpret-cast, + google-global-names-in-headers, + -google-readability-casting, + google-runtime-operator, + misc-*, + -misc-unused-parameters, + -misc-no-recursion, + -misc-non-private-member-variables-in-classes, + -misc-include-cleaner, + -misc-use-anonymous-namespace, + -misc-const-correctness, + modernize-*, + -modernize-return-braced-init-list, + -modernize-use-trailing-return-type, + -modernize-use-using, + -modernize-use-override, + -modernize-avoid-c-arrays, + -modernize-macro-to-enum, + -modernize-loop-convert, + -modernize-use-nodiscard, + -modernize-pass-by-value, + -modernize-use-auto, + performance-*, + -performance-avoid-endl, + -performance-unnecessary-value-param, + portability-std-allocator-const, + readability-*, + -readability-function-cognitive-complexity, + -readability-function-size, + -readability-identifier-length, + -readability-magic-numbers, + -readability-uppercase-literal-suffix, + -readability-braces-around-statements, + -readability-redundant-access-specifiers, + -readability-else-after-return, + -readability-container-data-pointer, + -readability-implicit-bool-conversion, + -readability-avoid-nested-conditional-operator, + -readability-redundant-member-init, + -readability-redundant-string-init, + -readability-avoid-const-params-in-decls, + -readability-named-parameter, + -readability-convert-member-functions-to-static, + -readability-qualified-auto, + -readability-make-member-function-const, + -readability-isolate-declaration, + -readability-inconsistent-declaration-parameter-name, + -clang-diagnostic-error, + +CheckOptions: + performance-for-range-copy.WarnOnAllAutoCopies: true + performance-inefficient-string-concatenation.StrictMode: true + readability-braces-around-statements.ShortStatementLines: 0 + readability-identifier-naming.ClassCase: CamelCase + readability-identifier-naming.ClassIgnoredRegexp: I.* + readability-identifier-naming.ClassPrefix: C # We can't use regex here?!?!?!? + readability-identifier-naming.EnumCase: CamelCase + readability-identifier-naming.EnumPrefix: e + readability-identifier-naming.EnumConstantCase: UPPER_CASE + readability-identifier-naming.FunctionCase: camelBack + readability-identifier-naming.NamespaceCase: CamelCase + readability-identifier-naming.NamespacePrefix: N + readability-identifier-naming.StructPrefix: S + readability-identifier-naming.StructCase: CamelCase diff --git a/CMakeLists.txt b/CMakeLists.txt index 1455860..6fcfbc1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -42,15 +42,15 @@ execute_process( include_directories(.) set(CMAKE_CXX_STANDARD 23) +set(CMAKE_EXPORT_COMPILE_COMMANDS TRUE) add_compile_options(-DWLR_USE_UNSTABLE) add_compile_options( -Wall -Wextra + -Wuseless-cast -Wno-unused-parameter -Wno-unused-value - -Wno-missing-field-initializers - -Wno-narrowing - -Wno-pointer-arith) + -Wno-missing-field-initializers) find_package(Threads REQUIRED) find_package(PkgConfig REQUIRED) diff --git a/src/clipboard/Clipboard.cpp b/src/clipboard/Clipboard.cpp index b20908c..28390df 100644 --- a/src/clipboard/Clipboard.cpp +++ b/src/clipboard/Clipboard.cpp @@ -17,4 +17,4 @@ void Clipboard::copy(const char* fmt, ...) { execlp("wl-copy", "wl-copy", outputStr, NULL); free(outputStr); -} \ No newline at end of file +} diff --git a/src/debug/Log.cpp b/src/debug/Log.cpp index a283591..9d2c860 100644 --- a/src/debug/Log.cpp +++ b/src/debug/Log.cpp @@ -2,6 +2,7 @@ #include #include +#include #include "../includes.hpp" @@ -38,7 +39,7 @@ void Debug::log(LogLevel level, const char* fmt, ...) { outputStr = (char*)malloc(logLen + 1); if (!outputStr) { - printf("CRITICAL: Cannot alloc size %d for log! (Out of memory?)", logLen + 1); + std::print("CRITICAL: Cannot alloc size {} for log! (Out of memory?)", logLen + 1); return; } diff --git a/src/helpers/LayerSurface.cpp b/src/helpers/LayerSurface.cpp index 7054e0f..da1fec5 100644 --- a/src/helpers/LayerSurface.cpp +++ b/src/helpers/LayerSurface.cpp @@ -2,9 +2,7 @@ #include "../hyprpicker.hpp" -CLayerSurface::CLayerSurface(SMonitor* pMonitor) { - m_pMonitor = pMonitor; - +CLayerSurface::CLayerSurface(SMonitor* pMonitor) : m_pMonitor(pMonitor) { pSurface = makeShared(g_pHyprpicker->m_pCompositor->sendCreateSurface()); if (!pSurface) { diff --git a/src/helpers/PoolBuffer.cpp b/src/helpers/PoolBuffer.cpp index 9d40f7d..836f89f 100644 --- a/src/helpers/PoolBuffer.cpp +++ b/src/helpers/PoolBuffer.cpp @@ -11,7 +11,7 @@ SPoolBuffer::SPoolBuffer(const Vector2D& pixelSize_, uint32_t format_, uint32_t g_pHyprpicker->finish(1); } - const auto DATA = mmap(NULL, SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, FD, 0); + const auto DATA = mmap(nullptr, SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, FD, 0); size = SIZE; data = DATA; @@ -39,4 +39,4 @@ SPoolBuffer::~SPoolBuffer() { if (paddedData) free(paddedData); -} \ No newline at end of file +} diff --git a/src/hyprpicker.cpp b/src/hyprpicker.cpp index 8cc6a47..f33d94f 100644 --- a/src/hyprpicker.cpp +++ b/src/hyprpicker.cpp @@ -1,5 +1,5 @@ #include "hyprpicker.hpp" -#include +#include void sigHandler(int sig) { g_pHyprpicker->m_vLayerSurfaces.clear(); @@ -248,7 +248,7 @@ void CHyprpicker::convertBuffer(SP pBuffer) { unsigned char green; unsigned char red; unsigned char alpha; - }* px = (struct pixel*)(data + y * (int)pBuffer->pixelSize.x * 4 + x * 4); + }* px = (struct pixel*)(data + (y * (int)pBuffer->pixelSize.x * 4) + (x * 4)); std::swap(px->red, px->blue); } @@ -262,7 +262,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 + (y * (int)pBuffer->pixelSize.x * 4) + (x * 4)); // conv to 8 bit uint8_t R = (uint8_t)std::round((255.0 * (((*px) & 0b00000000000000000000001111111111) >> 0) / 1023.0)); @@ -297,15 +297,15 @@ void* CHyprpicker::convert24To32Buffer(SP pBuffer) { unsigned char blue; unsigned char green; unsigned char red; - }* srcPx = (struct pixel3*)(oldBuffer + y * pBuffer->stride + x * 3); + }* srcPx = (struct pixel3*)(oldBuffer + (y * pBuffer->stride) + (x * 3)); struct pixel4 { // little-endian ARGB unsigned char blue; unsigned char green; unsigned char red; unsigned char alpha; - }* dstPx = (struct pixel4*)(newBuffer + y * newBufferStride + x * 4); - *dstPx = {srcPx->red, srcPx->green, srcPx->blue, 0xFF}; + }* dstPx = (struct pixel4*)(newBuffer + (y * newBufferStride) + (x * 4)); + *dstPx = {.blue = srcPx->red, .green = srcPx->green, .red = srcPx->blue, .alpha = 0xFF}; } } } break; @@ -317,15 +317,15 @@ void* CHyprpicker::convert24To32Buffer(SP pBuffer) { unsigned char red; unsigned char green; unsigned char blue; - }* srcPx = (struct pixel3*)(oldBuffer + y * pBuffer->stride + x * 3); + }* srcPx = (struct pixel3*)(oldBuffer + (y * pBuffer->stride) + (x * 3)); struct pixel4 { // big-endian ARGB unsigned char alpha; unsigned char red; unsigned char green; unsigned char blue; - }* dstPx = (struct pixel4*)(newBuffer + y * newBufferStride + x * 4); - *dstPx = {0xFF, srcPx->red, srcPx->green, srcPx->blue}; + }* dstPx = (struct pixel4*)(newBuffer + (y * newBufferStride) + (x * 4)); + *dstPx = {.alpha = 0xFF, .red = srcPx->red, .green = srcPx->green, .blue = srcPx->blue}; } } } break; @@ -418,7 +418,7 @@ void CHyprpicker::renderSurface(CLayerSurface* pSurface, bool forceInactive) { cairo_matrix_init_identity(&matrix); cairo_matrix_translate(&matrix, CLICKPOSBUF.x + 0.5f, CLICKPOSBUF.y + 0.5f); cairo_matrix_scale(&matrix, 0.1f, 0.1f); - cairo_matrix_translate(&matrix, -CLICKPOSBUF.x / SCALEBUFS.x - 0.5f, -CLICKPOSBUF.y / SCALEBUFS.y - 0.5f); + cairo_matrix_translate(&matrix, (-CLICKPOSBUF.x / SCALEBUFS.x) - 0.5f, (-CLICKPOSBUF.y / SCALEBUFS.y) - 0.5f); cairo_pattern_set_matrix(PATTERN, &matrix); cairo_set_source(PCAIRO, PATTERN); cairo_arc(PCAIRO, CLICKPOS.x, CLICKPOS.y, 100 / SCALEBUFS.x, 0, 2 * M_PI); @@ -518,7 +518,7 @@ CColor CHyprpicker::getColorFromPixel(CLayerSurface* pLS, Vector2D pix) { pix = pix.floor(); if (pix.x >= pLS->screenBuffer->pixelSize.x || pix.y >= pLS->screenBuffer->pixelSize.y || pix.x < 0 || pix.y < 0) - return CColor{0, 0, 0, 0}; + return CColor{.r = 0, .g = 0, .b = 0, .a = 0}; void* dataSrc = pLS->screenBuffer->paddedData ? pLS->screenBuffer->paddedData : pLS->screenBuffer->data; struct pixel { @@ -526,9 +526,9 @@ CColor CHyprpicker::getColorFromPixel(CLayerSurface* pLS, Vector2D pix) { unsigned char green; unsigned char red; unsigned char alpha; - }* px = (struct pixel*)((char*)dataSrc + (int)pix.y * (int)pLS->screenBuffer->pixelSize.x * 4 + (int)pix.x * 4); + }* px = (struct pixel*)((char*)dataSrc + ((ptrdiff_t)pix.y * (int)pLS->screenBuffer->pixelSize.x * 4) + ((ptrdiff_t)pix.x * 4)); - return CColor{(uint8_t)px->red, (uint8_t)px->green, (uint8_t)px->blue, (uint8_t)px->alpha}; + return CColor{.r = px->red, .g = px->green, .b = px->blue, .a = px->alpha}; } void CHyprpicker::initKeyboard() { @@ -541,7 +541,7 @@ void CHyprpicker::initKeyboard() { return; } - const char* buf = (const char*)mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0); + const char* buf = (const char*)mmap(nullptr, size, PROT_READ, MAP_SHARED, fd, 0); if (buf == MAP_FAILED) { Debug::log(ERR, "Failed to mmap xkb keymap: %d", errno); return; @@ -631,7 +631,7 @@ void CHyprpicker::initMouse() { 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 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 = (k == 1) ? 1 : 1 - k; float c = (r - k) / K, m = (g - k) / K, y = (b - k) / K; @@ -661,6 +661,7 @@ void CHyprpicker::initMouse() { return result; }; + auto hexR = toHex(COL.r); auto hexG = toHex(COL.g); auto hexB = toHex(COL.b); diff --git a/src/includes.hpp b/src/includes.hpp index 4286f39..0b17c60 100644 --- a/src/includes.hpp +++ b/src/includes.hpp @@ -4,12 +4,11 @@ #include #include #include -#include +#include #include #include #include -#include #include "protocols/cursor-shape-v1.hpp" #include "protocols/fractional-scale-v1.hpp" @@ -18,14 +17,13 @@ #include "protocols/viewporter.hpp" #include "protocols/wayland.hpp" -#include +#include #include #include #include #include -#include -#include -#include +#include +#include #include #include #include diff --git a/src/main.cpp b/src/main.cpp index 5ccb341..0692feb 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -4,7 +4,7 @@ #include "hyprpicker.hpp" -static void help(void) { +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" @@ -25,19 +25,19 @@ int main(int argc, char** argv, char** envp) { 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'}, - {"render-inactive", no_argument, NULL, 'r'}, - {"no-zoom", no_argument, NULL, 'z'}, - {"no-fractional", no_argument, NULL, 't'}, - {"quiet", no_argument, NULL, 'q'}, - {"verbose", no_argument, NULL, 'v'}, - {"disable-hex-preview", no_argument, NULL, 'd'}, - {"lowercase-hex", no_argument, NULL, 'l'}, - {"version", no_argument, NULL, 'V'}, - {NULL, 0, NULL, 0}}; + 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'}, + {"render-inactive", no_argument, nullptr, 'r'}, + {"no-zoom", no_argument, nullptr, 'z'}, + {"no-fractional", no_argument, nullptr, 't'}, + {"quiet", no_argument, nullptr, 'q'}, + {"verbose", no_argument, nullptr, 'v'}, + {"disable-hex-preview", no_argument, nullptr, 'd'}, + {"lowercase-hex", no_argument, nullptr, 'l'}, + {"version", no_argument, nullptr, 'V'}, + {nullptr, 0, nullptr, 0}}; int c = getopt_long(argc, argv, ":f:hnarzqvtdlV", long_options, &option_index); if (c == -1)