fix: recalulate on mouse move, not render

This commit is contained in:
Virt 2024-06-22 15:46:00 +02:00
commit 4fce380c13
4 changed files with 47 additions and 13 deletions

View file

@ -51,7 +51,7 @@ void CDynamicCursors::renderSoftware(CPointerManager* pointers, SP<CMonitor> pMo
box.scale(pMonitor->scale);
// we rotate the cursor by our calculated amount
box.rot = this->calculate(&pointers->pointerPos);
box.rot = this->angle;
// now pass the hotspot to rotate around
renderCursorTextureInternalWithDamage(texture, &box, &damage, 1.F, pointers->currentCursorImage.hotspot);
@ -151,7 +151,7 @@ wlr_buffer* CDynamicCursors::renderHardware(CPointerManager* pointers, SP<CPoint
// the box should start in the middle portion, rotate by our calculated amount
CBox xbox = {size, Vector2D{pointers->currentCursorImage.size / pointers->currentCursorImage.scale * state->monitor->scale}.round()};
xbox.rot = this->calculate(&pointers->pointerPos);
xbox.rot = this->angle;
// use our custom draw function
renderCursorTextureInternalWithDamage(texture, &xbox, &damage, 1.F, pointers->currentCursorImage.hotspot);
@ -198,6 +198,8 @@ void CDynamicCursors::onCursorMoved(CPointerManager* pointers) {
if (!pointers->hasCursor())
return;
bool changed = calculate(&pointers->pointerPos);
for (auto& m : g_pCompositor->m_vMonitors) {
auto state = pointers->stateFor(m);
@ -206,12 +208,16 @@ void CDynamicCursors::onCursorMoved(CPointerManager* pointers) {
if (state->hardwareFailed || !state->entered)
continue;
// we set a new hardware cursor on every move
const auto CURSORPOS = pointers->getCursorPosForMonitor(m);
m->output->impl->move_cursor(m->output, CURSORPOS.x, CURSORPOS.y);
// we set a new hardware cursor if the angle has changed significantly
if (changed)
pointers->attemptHardwareCursor(state);
}
}
double CDynamicCursors::calculate(Vector2D* pos) {
bool CDynamicCursors::calculate(Vector2D* pos) {
static auto* const* PLENGTH = (Hyprlang::INT* const*)HyprlandAPI::getConfigValue(PHANDLE, CONFIG_LENGTH)->getDataStaticPtr();
// translate to origin
@ -236,5 +242,11 @@ double CDynamicCursors::calculate(Vector2D* pos) {
end.x += pos->x;
end.y += pos->y;
return angle;
// we only consider the angle changed if it is larger than 1 degree
if (abs(this->angle - angle) > (PI / 180)) {
this->angle = angle;
return true;
}
return false;
}

View file

@ -21,8 +21,11 @@ class CDynamicCursors {
bool setHardware(CPointerManager* pointers, SP<CPointerManager::SMonitorPointerState> state, wlr_buffer* buf);
private:
// calculates the current angle of the cursor
double calculate(Vector2D* pos);
// current angle of the cursor in radiants
double angle;
// calculates the current angle of the cursor, returns whether the angle has changed
bool calculate(Vector2D* pos);
// this is the end of the virtual stick
Vector2D end;
};

View file

@ -2,6 +2,7 @@
#include <hyprland/src/plugins/PluginAPI.hpp>
#define CONFIG_ENABLED "plugin:dynamic-cursors:enabled"
#define CONFIG_LENGTH "plugin:dynamic-cursors:length"
#define CONFIG_HW_DEBUG "plugin:dynamic-cursors:hw_debug"

View file

@ -11,31 +11,46 @@
typedef void (*origRenderSofwareCursorsFor)(void*, SP<CMonitor>, timespec*, CRegion&, std::optional<Vector2D>);
inline CFunctionHook* g_pRenderSoftwareCursorsForHook = nullptr;
void hkRenderSoftwareCursorsFor(void* thisptr, SP<CMonitor> pMonitor, timespec* now, CRegion& damage, std::optional<Vector2D> overridePos) {
g_pDynamicCursors->renderSoftware((CPointerManager*) thisptr, pMonitor, now, damage, overridePos);
static auto* const* PENABLED = (Hyprlang::INT* const*)HyprlandAPI::getConfigValue(PHANDLE, CONFIG_ENABLED)->getDataStaticPtr();
if (**PENABLED) g_pDynamicCursors->renderSoftware((CPointerManager*) thisptr, pMonitor, now, damage, overridePos);
else (*(origRenderSofwareCursorsFor)g_pRenderSoftwareCursorsForHook->m_pOriginal)(thisptr, pMonitor, now, damage, overridePos);
}
typedef void (*origDamageIfSoftware)(void*);
inline CFunctionHook* g_pDamageIfSoftwareHook = nullptr;
void hkDamageIfSoftware(void* thisptr) {
g_pDynamicCursors->damageSoftware((CPointerManager*) thisptr);
static auto* const* PENABLED = (Hyprlang::INT* const*)HyprlandAPI::getConfigValue(PHANDLE, CONFIG_ENABLED)->getDataStaticPtr();
if (**PENABLED) g_pDynamicCursors->damageSoftware((CPointerManager*) thisptr);
else (*(origDamageIfSoftware)g_pDamageIfSoftwareHook->m_pOriginal)(thisptr);
}
typedef wlr_buffer* (*origRenderHWCursorBuffer)(void*, SP<CPointerManager::SMonitorPointerState>, SP<CTexture>);
inline CFunctionHook* g_pRenderHWCursorBufferHook = nullptr;
wlr_buffer* hkRenderHWCursorBuffer(void* thisptr, SP<CPointerManager::SMonitorPointerState> state, SP<CTexture> texture) {
return g_pDynamicCursors->renderHardware((CPointerManager*) thisptr, state, texture);
static auto* const* PENABLED = (Hyprlang::INT* const*)HyprlandAPI::getConfigValue(PHANDLE, CONFIG_ENABLED)->getDataStaticPtr();
if (**PENABLED) return g_pDynamicCursors->renderHardware((CPointerManager*) thisptr, state, texture);
else return (*(origRenderHWCursorBuffer)g_pRenderHWCursorBufferHook->m_pOriginal)(thisptr, state, texture);
}
typedef bool (*origSetHWCursorBuffer)(void*, SP<CPointerManager::SMonitorPointerState>, wlr_buffer*);
inline CFunctionHook* g_pSetHWCursorBufferHook = nullptr;
bool hkSetHWCursorBuffer(void* thisptr, SP<CPointerManager::SMonitorPointerState> state, wlr_buffer* buffer) {
return g_pDynamicCursors->setHardware((CPointerManager*) thisptr, state, buffer);
static auto* const* PENABLED = (Hyprlang::INT* const*)HyprlandAPI::getConfigValue(PHANDLE, CONFIG_ENABLED)->getDataStaticPtr();
if (**PENABLED) return g_pDynamicCursors->setHardware((CPointerManager*) thisptr, state, buffer);
else return (*(origSetHWCursorBuffer)g_pSetHWCursorBufferHook->m_pOriginal)(thisptr, state, buffer);
}
typedef void (*origOnCursorMoved)(void*);
inline CFunctionHook* g_pOnCursorMovedHook = nullptr;
void hkOnCursorMoved(void* thisptr) {
return g_pDynamicCursors->onCursorMoved((CPointerManager*) thisptr);
static auto* const* PENABLED = (Hyprlang::INT* const*)HyprlandAPI::getConfigValue(PHANDLE, CONFIG_ENABLED)->getDataStaticPtr();
if (**PENABLED) return g_pDynamicCursors->onCursorMoved((CPointerManager*) thisptr);
else return (*(origOnCursorMoved)g_pOnCursorMovedHook->m_pOriginal)(thisptr);
}
APICALL EXPORT PLUGIN_DESCRIPTION_INFO PLUGIN_INIT(HANDLE handle) {
@ -71,9 +86,12 @@ APICALL EXPORT PLUGIN_DESCRIPTION_INFO PLUGIN_INIT(HANDLE handle) {
g_pOnCursorMovedHook = HyprlandAPI::createFunctionHook(PHANDLE, ON_CURSOR_MOVED_METHODS[0].address, (void*) &hkOnCursorMoved);
g_pOnCursorMovedHook->hook();
HyprlandAPI::addConfigValue(PHANDLE, CONFIG_ENABLED, Hyprlang::INT{1});
HyprlandAPI::addConfigValue(PHANDLE, CONFIG_LENGTH, Hyprlang::INT{20});
HyprlandAPI::addConfigValue(PHANDLE, CONFIG_HW_DEBUG, Hyprlang::INT{0});
HyprlandAPI::reloadConfig();
return {"dynamic-cursors", "The most stupid cursor plugin.", "Virt", "1.1"};
}