From 5da4693a6627e7e43b31530b537a1f5b05fe310c Mon Sep 17 00:00:00 2001 From: r3b00thx Date: Wed, 4 Jun 2025 20:53:57 +0300 Subject: [PATCH] feat(notify): add notify support In this commit I added a new feature, a flag for notifications -n / --notify. It's using the notify-send command line utility to send desktop notifications. By default I made it compatible with dunst (you will need to enable full markup in the dunst config) but it should work with other notification daemons too. --- src/defines.hpp | 1 + src/hyprpicker.cpp | 31 +++++++++++++++++++++++++++++++ src/hyprpicker.hpp | 1 + src/main.cpp | 5 ++++- src/notify/Notify.cpp | 18 ++++++++++++++++++ src/notify/Notify.hpp | 8 ++++++++ 6 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 src/notify/Notify.cpp create mode 100644 src/notify/Notify.hpp diff --git a/src/defines.hpp b/src/defines.hpp index 599c418..dbf5438 100644 --- a/src/defines.hpp +++ b/src/defines.hpp @@ -5,6 +5,7 @@ #include "helpers/Monitor.hpp" #include "helpers/Color.hpp" #include "clipboard/Clipboard.hpp" +#include "notify/Notify.hpp" // git stuff #ifndef GIT_COMMIT_HASH diff --git a/src/hyprpicker.cpp b/src/hyprpicker.cpp index 8041d83..1e07c4d 100644 --- a/src/hyprpicker.cpp +++ b/src/hyprpicker.cpp @@ -1,5 +1,8 @@ #include "hyprpicker.hpp" +#include "src/notify/Notify.hpp" #include +#include +#include void sigHandler(int sig) { g_pHyprpicker->m_vLayerSurfaces.clear(); @@ -666,6 +669,13 @@ void CHyprpicker::initMouse() { if (m_bAutoCopy) Clipboard::copy("%g%% %g%% %g%% %g%%", c, m, y, k); + + if (m_bNotify) { + char buf[64]; + snprintf(buf, sizeof(buf), "cmyk(%f%%, %f%%, %f%%, %f%%)", c, m, y, k); + Notify::send(buf); + } + finish(); break; } @@ -693,6 +703,13 @@ void CHyprpicker::initMouse() { if (m_bAutoCopy) Clipboard::copy("#%s%s%s", toHex(COL.r).c_str(), toHex(COL.g).c_str(), toHex(COL.b).c_str()); + + if (m_bNotify) { + char buf[64]; + snprintf(buf, sizeof(buf), "#%s%s%s", toHex(COL.r).c_str(), toHex(COL.g).c_str(), toHex(COL.b).c_str()); + Notify::send(buf); + } + finish(); break; } @@ -704,6 +721,13 @@ void CHyprpicker::initMouse() { if (m_bAutoCopy) Clipboard::copy("%i %i %i", COL.r, COL.g, COL.b); + + if (m_bNotify) { + char buf[64]; + snprintf(buf, sizeof(buf), "rgb(%i, %i, %i)", COL.r, COL.g, COL.b); + Notify::send(buf); + } + finish(); break; } @@ -721,6 +745,13 @@ void CHyprpicker::initMouse() { if (m_bAutoCopy) Clipboard::copy("%g %g%% %g%%", h, s, l_or_v); + + if (m_bNotify) { + char buf[64]; + snprintf(buf, sizeof(buf), "hsl(%f, %f%%, %f%%)", h, s, l_or_v); + Notify::send(buf); + } + finish(); break; } diff --git a/src/hyprpicker.hpp b/src/hyprpicker.hpp index 288b869..0453f61 100644 --- a/src/hyprpicker.hpp +++ b/src/hyprpicker.hpp @@ -41,6 +41,7 @@ class CHyprpicker { bool m_bFancyOutput = true; bool m_bAutoCopy = false; + bool m_bNotify = false; bool m_bRenderInactive = false; bool m_bNoZoom = false; bool m_bNoFractional = false; diff --git a/src/main.cpp b/src/main.cpp index 1fa9f9f..4c5bb72 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -8,6 +8,7 @@ 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" << " -h | --help | Show this help message\n" << " -r | --render-inactive | Render (freeze) inactive displays\n" @@ -29,6 +30,7 @@ int main(int argc, char** argv, char** envp) { {"format", required_argument, nullptr, 'f'}, {"help", no_argument, nullptr, 'h'}, {"no-fancy", no_argument, nullptr, 'n'}, + {"notify", no_argument, nullptr, 'j'}, {"render-inactive", no_argument, nullptr, 'r'}, {"no-zoom", no_argument, nullptr, 'z'}, {"no-fractional", no_argument, nullptr, 't'}, @@ -39,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:hnarzqvtdlV", long_options, &option_index); + int c = getopt_long(argc, argv, ":f:hnjarzqvtdlV", long_options, &option_index); if (c == -1) break; @@ -62,6 +64,7 @@ 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 'r': g_pHyprpicker->m_bRenderInactive = true; break; case 'z': g_pHyprpicker->m_bNoZoom = true; break; diff --git a/src/notify/Notify.cpp b/src/notify/Notify.cpp new file mode 100644 index 0000000..51950f2 --- /dev/null +++ b/src/notify/Notify.cpp @@ -0,0 +1,18 @@ +#include "Notify.hpp" + +#include "../includes.hpp" +#include +#include +#include + +void Notify::send(const char* color) { + char bodyBuf[NOTIFYBODYSIZE]; + char colorBuf[64]; + + snprintf(bodyBuf, sizeof(bodyBuf), "You selected the color: %s", color, color); + + if (fork() == 0) + execlp("notify-send", "notify-send", "-t", "5000", "-i", "color-select-symbolic", "Color Picker", bodyBuf, NULL); + + std::cout << colorBuf; +} diff --git a/src/notify/Notify.hpp b/src/notify/Notify.hpp new file mode 100644 index 0000000..1f0e4d0 --- /dev/null +++ b/src/notify/Notify.hpp @@ -0,0 +1,8 @@ +#pragma once + +#include +#define NOTIFYBODYSIZE 128 + +namespace Notify { + void send(const char* color); +}