diff --git a/README.md b/README.md index bd5c4b3..e3e83b5 100644 --- a/README.md +++ b/README.md @@ -14,6 +14,8 @@ Launch it. Click. That's it. `--no-fancy` disables the "fancy" (aka. colored) outputting +`--autocopy` automatically copies the output to the clipboard (requires [wl-clipboard](https://github.com/bugaevc/wl-clipboard)) + # Building ## Arch diff --git a/src/clipboard/Clipboard.cpp b/src/clipboard/Clipboard.cpp new file mode 100644 index 0000000..ee018a1 --- /dev/null +++ b/src/clipboard/Clipboard.cpp @@ -0,0 +1,19 @@ +#include "Clipboard.hpp" + +#include "../includes.hpp" + +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); + + execlp("wl-copy", "wl-copy", outputStr, NULL); + + free(outputStr); +} \ No newline at end of file diff --git a/src/clipboard/Clipboard.hpp b/src/clipboard/Clipboard.hpp new file mode 100644 index 0000000..1dc2be1 --- /dev/null +++ b/src/clipboard/Clipboard.hpp @@ -0,0 +1,7 @@ +#pragma once + +#define CLIPBOARDMESSAGESIZE 24 + +namespace Clipboard { + void copy(const char* fmt, ...); +}; \ No newline at end of file diff --git a/src/defines.hpp b/src/defines.hpp index a25bab3..a40eaaa 100644 --- a/src/defines.hpp +++ b/src/defines.hpp @@ -5,6 +5,7 @@ #include "includes.hpp" #include "helpers/Monitor.hpp" #include "helpers/Color.hpp" +#include "clipboard/Clipboard.hpp" // git stuff #ifndef GIT_COMMIT_HASH diff --git a/src/events/Events.cpp b/src/events/Events.cpp index 80b6c06..ae66dce 100644 --- a/src/events/Events.cpp +++ b/src/events/Events.cpp @@ -147,6 +147,8 @@ void Events::handlePointerButton(void *data, struct wl_pointer *wl_pointer, uint else Debug::log(NONE, "#%s%s%s", toHex(COL.r).c_str(), toHex(COL.g).c_str(), toHex(COL.b).c_str()); + if (g_pHyprpicker->m_bAutoCopy) + Clipboard::copy("#%s%s%s", toHex(COL.r).c_str(), toHex(COL.g).c_str(), toHex(COL.b).c_str()); break; } case OUTPUT_RGB: @@ -155,6 +157,9 @@ void Events::handlePointerButton(void *data, struct wl_pointer *wl_pointer, uint 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 Debug::log(NONE, "%i %i %i", COL.r, COL.g, COL.b); + + if (g_pHyprpicker->m_bAutoCopy) + Clipboard::copy("%i %i %i", COL.r, COL.g, COL.b); break; } } diff --git a/src/hyprpicker.hpp b/src/hyprpicker.hpp index c04be90..f55eddf 100644 --- a/src/hyprpicker.hpp +++ b/src/hyprpicker.hpp @@ -26,6 +26,8 @@ public: bool m_bFancyOutput = true; + bool m_bAutoCopy = false; + bool m_bRunning = true; std::vector> m_vMonitors; diff --git a/src/main.cpp b/src/main.cpp index 1c9cc04..1d811cf 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -17,10 +17,13 @@ int main(int argc, char** argv, char** envp) { continue; } else if (arg == "--no-fancy") { g_pHyprpicker->m_bFancyOutput = false; + } else if (arg == "--autocopy") { + g_pHyprpicker->m_bAutoCopy = true; } else { std::cout << "Hyprpicker usage: hyprpicker [arg [...]].\n\nArguments:\n" << " --format [fmt] | Specifies the output format (hex, rgb)\n" << " --no-fancy | Disables the \"fancy\" (aka. colored) outputting\n" << + " --autocopy | Automatically copies the output to the clipboard (requires wl-clipboard)\n" << " --help | Show this help message\n"; exit(1); }