From fcb4c2b87bbaf9b70535b5996c7981519a9e082c Mon Sep 17 00:00:00 2001 From: Virt <41426325+VirtCode@users.noreply.github.com> Date: Fri, 28 Jun 2024 18:03:59 +0200 Subject: [PATCH] feat: ipc events for cursor shake ref #3 --- README.md | 4 ++++ src/globals.hpp | 1 + src/main.cpp | 1 + src/other/Shake.cpp | 23 ++++++++++++++++++++++- src/other/Shake.hpp | 8 ++++++-- 5 files changed, 34 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 392a8c0..4b2dc3b 100644 --- a/README.md +++ b/README.md @@ -146,6 +146,10 @@ plugin:dynamic-cursors { # use nearest-neighbour (pixelated) scaling when shaking # may look weird when effects are enabled nearest = true + + # enable ipc events for shake + # see #3 + ipc = false } } ``` diff --git a/src/globals.hpp b/src/globals.hpp index 7cb8ff7..893475c 100644 --- a/src/globals.hpp +++ b/src/globals.hpp @@ -10,6 +10,7 @@ #define CONFIG_SHAKE_THRESHOLD "plugin:dynamic-cursors:shake:threshold" #define CONFIG_SHAKE_FACTOR "plugin:dynamic-cursors:shake:factor" #define CONFIG_SHAKE_EFFECTS "plugin:dynamic-cursors:shake:effects" +#define CONFIG_SHAKE_IPC "plugin:dynamic-cursors:shake:ipc" #define CONFIG_LENGTH "plugin:dynamic-cursors:rotate:length" #define CONFIG_MASS "plugin:dynamic-cursors:tilt:limit" #define CONFIG_FUNCTION "plugin:dynamic-cursors:tilt:function" diff --git a/src/main.cpp b/src/main.cpp index cbb6d8a..ecac121 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -112,6 +112,7 @@ APICALL EXPORT PLUGIN_DESCRIPTION_INFO PLUGIN_INIT(HANDLE handle) { HyprlandAPI::addConfigValue(PHANDLE, CONFIG_SHAKE, Hyprlang::INT{1}); HyprlandAPI::addConfigValue(PHANDLE, CONFIG_SHAKE_NEAREST, Hyprlang::INT{1}); HyprlandAPI::addConfigValue(PHANDLE, CONFIG_SHAKE_EFFECTS, Hyprlang::INT{0}); + HyprlandAPI::addConfigValue(PHANDLE, CONFIG_SHAKE_IPC, Hyprlang::INT{0}); HyprlandAPI::addConfigValue(PHANDLE, CONFIG_SHAKE_THRESHOLD, Hyprlang::FLOAT{4}); HyprlandAPI::addConfigValue(PHANDLE, CONFIG_SHAKE_FACTOR, Hyprlang::FLOAT{1.5}); diff --git a/src/other/Shake.cpp b/src/other/Shake.cpp index 6c0cb49..2e78dbb 100644 --- a/src/other/Shake.cpp +++ b/src/other/Shake.cpp @@ -1,10 +1,12 @@ #include "../globals.hpp" +#include "src/managers/EventManager.hpp" #include "Shake.hpp" #include double CShake::update(Vector2D pos) { static auto* const* PTHRESHOLD = (Hyprlang::FLOAT* const*)HyprlandAPI::getConfigValue(PHANDLE, CONFIG_SHAKE_THRESHOLD)->getDataStaticPtr(); static auto* const* PFACTOR = (Hyprlang::FLOAT* const*)HyprlandAPI::getConfigValue(PHANDLE, CONFIG_SHAKE_FACTOR)->getDataStaticPtr(); + static auto* const* PIPC = (Hyprlang::INT* const*)HyprlandAPI::getConfigValue(PHANDLE, CONFIG_SHAKE_IPC)->getDataStaticPtr(); int max = g_pHyprRenderer->m_pMostHzMonitor->refreshRate; // 1s worth of history samples.resize(max); @@ -35,5 +37,24 @@ double CShake::update(Vector2D pos) { // discard when the diagonal is small, so we don't have issues with inaccuracies if (diagonal < 100) return 1.0; - return std::max(1.0, ((trail / diagonal) - **PTHRESHOLD) * **PFACTOR); + double zoom = ((trail / diagonal) - **PTHRESHOLD); + + if (**PIPC) { + if (zoom > 1) { + if (!ipc) { + g_pEventManager->postEvent(SHyprIPCEvent { IPC_SHAKE_START }); + ipc = true; + } + + g_pEventManager->postEvent(SHyprIPCEvent { IPC_SHAKE_UPDATE, std::format("{},{},{},{}", (int) pos.x, (int) pos.y, trail, diagonal) }); + } else { + if (ipc) { + g_pEventManager->postEvent(SHyprIPCEvent { IPC_SHAKE_END }); + ipc = false; + } + } + } + + // we want ipc to work with factor = 0, so we use it here + return std::max(1.0, zoom * **PFACTOR); } diff --git a/src/other/Shake.hpp b/src/other/Shake.hpp index 52d0e0d..27f6319 100644 --- a/src/other/Shake.hpp +++ b/src/other/Shake.hpp @@ -1,6 +1,10 @@ #include #include +#define IPC_SHAKE_START "shakestart" +#define IPC_SHAKE_UPDATE "shakeupdate" +#define IPC_SHAKE_END "shakeend" + using namespace Hyprutils::Math; class CShake { @@ -9,8 +13,8 @@ class CShake { double update(Vector2D pos); private: - /* tracks the global software lock issued by cursor shaking */ - bool software = false; + /* tracks whether the current shake has already been announced in the ipc */ + bool ipc = false; /* ringbuffer for last samples */ std::vector samples;