feat: ipc events for cursor shake

ref #3
This commit is contained in:
Virt 2024-06-28 18:03:59 +02:00
commit fcb4c2b87b
5 changed files with 34 additions and 3 deletions

View file

@ -146,6 +146,10 @@ plugin:dynamic-cursors {
# use nearest-neighbour (pixelated) scaling when shaking # use nearest-neighbour (pixelated) scaling when shaking
# may look weird when effects are enabled # may look weird when effects are enabled
nearest = true nearest = true
# enable ipc events for shake
# see #3
ipc = false
} }
} }
``` ```

View file

@ -10,6 +10,7 @@
#define CONFIG_SHAKE_THRESHOLD "plugin:dynamic-cursors:shake:threshold" #define CONFIG_SHAKE_THRESHOLD "plugin:dynamic-cursors:shake:threshold"
#define CONFIG_SHAKE_FACTOR "plugin:dynamic-cursors:shake:factor" #define CONFIG_SHAKE_FACTOR "plugin:dynamic-cursors:shake:factor"
#define CONFIG_SHAKE_EFFECTS "plugin:dynamic-cursors:shake:effects" #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_LENGTH "plugin:dynamic-cursors:rotate:length"
#define CONFIG_MASS "plugin:dynamic-cursors:tilt:limit" #define CONFIG_MASS "plugin:dynamic-cursors:tilt:limit"
#define CONFIG_FUNCTION "plugin:dynamic-cursors:tilt:function" #define CONFIG_FUNCTION "plugin:dynamic-cursors:tilt:function"

View file

@ -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, Hyprlang::INT{1});
HyprlandAPI::addConfigValue(PHANDLE, CONFIG_SHAKE_NEAREST, 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_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_THRESHOLD, Hyprlang::FLOAT{4});
HyprlandAPI::addConfigValue(PHANDLE, CONFIG_SHAKE_FACTOR, Hyprlang::FLOAT{1.5}); HyprlandAPI::addConfigValue(PHANDLE, CONFIG_SHAKE_FACTOR, Hyprlang::FLOAT{1.5});

View file

@ -1,10 +1,12 @@
#include "../globals.hpp" #include "../globals.hpp"
#include "src/managers/EventManager.hpp"
#include "Shake.hpp" #include "Shake.hpp"
#include <hyprland/src/Compositor.hpp> #include <hyprland/src/Compositor.hpp>
double CShake::update(Vector2D pos) { double CShake::update(Vector2D pos) {
static auto* const* PTHRESHOLD = (Hyprlang::FLOAT* const*)HyprlandAPI::getConfigValue(PHANDLE, CONFIG_SHAKE_THRESHOLD)->getDataStaticPtr(); 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* 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 int max = g_pHyprRenderer->m_pMostHzMonitor->refreshRate; // 1s worth of history
samples.resize(max); 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 // discard when the diagonal is small, so we don't have issues with inaccuracies
if (diagonal < 100) return 1.0; 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);
} }

View file

@ -1,6 +1,10 @@
#include <hyprutils/math/Vector2D.hpp> #include <hyprutils/math/Vector2D.hpp>
#include <vector> #include <vector>
#define IPC_SHAKE_START "shakestart"
#define IPC_SHAKE_UPDATE "shakeupdate"
#define IPC_SHAKE_END "shakeend"
using namespace Hyprutils::Math; using namespace Hyprutils::Math;
class CShake { class CShake {
@ -9,8 +13,8 @@ class CShake {
double update(Vector2D pos); double update(Vector2D pos);
private: private:
/* tracks the global software lock issued by cursor shaking */ /* tracks whether the current shake has already been announced in the ipc */
bool software = false; bool ipc = false;
/* ringbuffer for last samples */ /* ringbuffer for last samples */
std::vector<Vector2D> samples; std::vector<Vector2D> samples;