hypr-dynamic-cursors/src/cursor.cpp

410 lines
15 KiB
C++
Raw Normal View History

2024-06-21 14:32:42 +02:00
#include "globals.hpp"
2024-06-26 17:14:16 +02:00
#include "src/debug/Log.hpp"
2024-06-21 14:32:42 +02:00
#include <cmath>
#include <cstdlib>
2024-06-26 17:14:16 +02:00
#include <cstring>
2024-06-27 17:21:39 +02:00
#include <hyprlang.hpp>
2024-06-21 14:32:42 +02:00
#define private public
#include <hyprland/src/managers/PointerManager.hpp>
2024-06-21 16:06:55 +02:00
#include <hyprland/src/render/OpenGL.hpp>
#include <hyprland/src/Compositor.hpp>
2024-06-21 14:32:42 +02:00
#undef private
2024-06-21 16:06:55 +02:00
#include <hyprland/src/config/ConfigValue.hpp>
#include "hyprland/cursor.hpp"
#include <hyprland/wlr/interfaces/wlr_output.h>
#include <hyprland/wlr/render/interface.h>
#include <hyprland/wlr/render/wlr_renderer.h>
2024-06-21 16:06:55 +02:00
#include "cursor.hpp"
#include "renderer.hpp"
2024-06-21 14:32:42 +02:00
/*
Reimplements rendering of the software cursor.
Is also largely identical to hyprlands impl, but uses our custom rendering to rotate the cursor.
*/
2024-06-21 16:36:10 +02:00
void CDynamicCursors::renderSoftware(CPointerManager* pointers, SP<CMonitor> pMonitor, timespec* now, CRegion& damage, std::optional<Vector2D> overridePos) {
2024-06-27 17:21:39 +02:00
static auto* const* PNEAREST = (Hyprlang::INT* const*)HyprlandAPI::getConfigValue(PHANDLE, CONFIG_SHAKE_NEAREST)->getDataStaticPtr();
2024-06-21 14:32:42 +02:00
if (!pointers->hasCursor())
return;
auto state = pointers->stateFor(pMonitor);
if ((!state->hardwareFailed && state->softwareLocks == 0)) {
return;
}
auto box = state->box.copy();
if (overridePos.has_value()) {
box.x = overridePos->x;
box.y = overridePos->y;
}
if (box.intersection(CBox{{}, {pMonitor->vecSize}}).empty())
return;
auto texture = pointers->getCurrentCursorTexture();
if (!texture)
return;
box.scale(pMonitor->scale);
2024-06-27 17:21:39 +02:00
box.w *= zoom;
box.h *= zoom;
2024-06-21 16:36:10 +02:00
// we rotate the cursor by our calculated amount
box.rot = this->angle;
2024-06-21 14:32:42 +02:00
2024-06-21 16:36:10 +02:00
// now pass the hotspot to rotate around
2024-06-27 17:21:39 +02:00
renderCursorTextureInternalWithDamage(texture, &box, &damage, 1.F, pointers->currentCursorImage.hotspot * zoom, zoom > 1 && **PNEAREST);
2024-06-21 14:32:42 +02:00
}
/*
This function implements damaging the screen such that the software cursor is drawn.
It is largely identical to hyprlands implementation, but expands the damage reagion, to accomodate various rotations.
*/
2024-06-21 16:36:10 +02:00
void CDynamicCursors::damageSoftware(CPointerManager* pointers) {
// we damage a 3x3 area around the cursor, to accomodate for all possible hotspots and rotations
2024-06-27 17:21:39 +02:00
Vector2D size = pointers->currentCursorImage.size / pointers->currentCursorImage.scale * zoom;
CBox b = CBox{pointers->pointerPos, size * 3}.translate(-(pointers->currentCursorImage.hotspot * zoom + size));
2024-06-21 16:36:10 +02:00
static auto PNOHW = CConfigValue<Hyprlang::INT>("cursor:no_hardware_cursors");
for (auto& mw : pointers->monitorStates) {
if (mw->monitor.expired())
continue;
if ((mw->softwareLocks > 0 || mw->hardwareFailed || *PNOHW) && b.overlaps({mw->monitor->vecPosition, mw->monitor->vecSize})) {
g_pHyprRenderer->damageBox(&b, mw->monitor->shouldSkipScheduleFrameOnMouseEvent());
break;
}
}
}
/*
This function reimplements the hardware cursor buffer drawing.
It is largely copied from hyprland, but adjusted to allow the cursor to be rotated.
*/
wlr_buffer* CDynamicCursors::renderHardware(CPointerManager* pointers, SP<CPointerManager::SMonitorPointerState> state, SP<CTexture> texture) {
static auto* const* PHW_DEBUG= (Hyprlang::INT* const*)HyprlandAPI::getConfigValue(PHANDLE, CONFIG_HW_DEBUG)->getDataStaticPtr();
2024-06-27 17:21:39 +02:00
static auto* const* PNEAREST = (Hyprlang::INT* const*)HyprlandAPI::getConfigValue(PHANDLE, CONFIG_SHAKE_NEAREST)->getDataStaticPtr();
auto output = state->monitor->output;
2024-06-27 17:21:39 +02:00
auto size = pointers->currentCursorImage.size * zoom;
// we try to allocate a buffer that is thrice as big, see software rendering
auto target = size * 3;
if (output->impl->get_cursor_size) {
int w, h;
output->impl->get_cursor_size(output, &w, &h);
if (w < target.x || h < target.y) {
Debug::log(TRACE, "hardware cursor too big! {} > {}x{}", pointers->currentCursorImage.size, w, h);
return nullptr;
}
target.x = w;
target.y = h;
}
if (target.x <= 0 || target.y <= 0) {
Debug::log(TRACE, "hw cursor for output {} failed the size checks ({}x{} is invalid)", state->monitor->szName, target.x, target.y);
return nullptr;
}
if (!output->cursor_swapchain || target != Vector2D{output->cursor_swapchain->width, output->cursor_swapchain->height}) {
wlr_drm_format fmt = {0};
if (!output_pick_cursor_format(output, &fmt)) {
Debug::log(TRACE, "Failed to pick cursor format");
return nullptr;
}
wlr_swapchain_destroy(output->cursor_swapchain);
output->cursor_swapchain = wlr_swapchain_create(output->allocator, target.x, target.y, &fmt);
wlr_drm_format_finish(&fmt);
if (!output->cursor_swapchain) {
Debug::log(TRACE, "Failed to create cursor swapchain");
return nullptr;
}
}
wlr_buffer* buf = wlr_swapchain_acquire(output->cursor_swapchain, nullptr);
if (!buf) {
Debug::log(TRACE, "Failed to acquire a buffer from the cursor swapchain");
return nullptr;
}
CRegion damage = {0, 0, INT16_MAX, INT16_MAX};
g_pHyprRenderer->makeEGLCurrent();
g_pHyprOpenGL->m_RenderData.pMonitor = state->monitor.get(); // has to be set cuz allocs
const auto RBO = g_pHyprRenderer->getOrCreateRenderbuffer(buf, DRM_FORMAT_ARGB8888);
RBO->bind();
g_pHyprOpenGL->beginSimple(state->monitor.get(), damage, RBO);
if (**PHW_DEBUG)
g_pHyprOpenGL->clear(CColor{rand() / float(RAND_MAX), rand() / float(RAND_MAX), rand() / float(RAND_MAX), 1.F});
else
g_pHyprOpenGL->clear(CColor{0.F, 0.F, 0.F, 0.F});
// the box should start in the middle portion, rotate by our calculated amount
2024-06-27 17:21:39 +02:00
CBox xbox = {size, Vector2D{pointers->currentCursorImage.size / pointers->currentCursorImage.scale * state->monitor->scale * zoom}.round()};
xbox.rot = this->angle;
// use our custom draw function
2024-06-27 17:21:39 +02:00
renderCursorTextureInternalWithDamage(texture, &xbox, &damage, 1.F, pointers->currentCursorImage.hotspot * zoom, zoom > 1 && **PNEAREST);
g_pHyprOpenGL->end();
glFlush();
g_pHyprOpenGL->m_RenderData.pMonitor = nullptr;
wlr_buffer_unlock(buf);
return buf;
}
/*
Implements the hardware cursor setting.
It is also mostly the same as stock hyprland, but with the hotspot translated more into the middle.
*/
bool CDynamicCursors::setHardware(CPointerManager* pointers, SP<CPointerManager::SMonitorPointerState> state, wlr_buffer* buf) {
if (!state->monitor->output->impl->set_cursor)
return false;
auto P_MONITOR = state->monitor.lock();
if (!P_MONITOR->output->cursor_swapchain) return false;
// we need to transform the hotspot manually as we need to indent it by the size
2024-06-27 17:21:39 +02:00
const auto HOTSPOT = CBox{(pointers->currentCursorImage.hotspot + pointers->currentCursorImage.size) * P_MONITOR->scale * zoom, {0, 0}}
.transform(wlTransformToHyprutils(wlr_output_transform_invert(P_MONITOR->transform)), P_MONITOR->output->cursor_swapchain->width, P_MONITOR->output->cursor_swapchain->height)
.pos();
Debug::log(TRACE, "[pointer] hw transformed hotspot for {}: {}", state->monitor->szName, HOTSPOT);
if (!state->monitor->output->impl->set_cursor(state->monitor->output, buf, HOTSPOT.x, HOTSPOT.y))
return false;
wlr_buffer_unlock(state->cursorFrontBuffer);
state->cursorFrontBuffer = buf;
g_pCompositor->scheduleFrameForMonitor(state->monitor.get());
if (buf)
wlr_buffer_lock(buf);
return true;
}
/*
Handles cursor move events.
*/
void CDynamicCursors::onCursorMoved(CPointerManager* pointers) {
if (!pointers->hasCursor())
return;
for (auto& m : g_pCompositor->m_vMonitors) {
auto state = pointers->stateFor(m);
state->box = pointers->getCursorBoxLogicalForMonitor(state->monitor.lock());
if (state->hardwareFailed || !state->entered)
continue;
const auto CURSORPOS = pointers->getCursorPosForMonitor(m);
m->output->impl->move_cursor(m->output, CURSORPOS.x, CURSORPOS.y);
2024-06-26 17:14:16 +02:00
}
2024-06-26 18:02:09 +02:00
static auto const* PMODE = (Hyprlang::STRING const*)HyprlandAPI::getConfigValue(PHANDLE, CONFIG_MODE)->getDataStaticPtr();
if (!strcmp(*PMODE, "rotate")) calculate(false);
2024-06-26 17:14:16 +02:00
}
2024-06-26 18:02:09 +02:00
/*
Handle cursor tick events.
*/
2024-06-26 17:14:16 +02:00
void CDynamicCursors::onTick(CPointerManager* pointers) {
2024-06-26 18:02:09 +02:00
static auto const* PMODE = (Hyprlang::STRING const*)HyprlandAPI::getConfigValue(PHANDLE, CONFIG_MODE)->getDataStaticPtr();
2024-06-27 17:21:39 +02:00
static auto* const* PSHAKE = (Hyprlang::INT* const*)HyprlandAPI::getConfigValue(PHANDLE, CONFIG_SHAKE)->getDataStaticPtr();
2024-06-26 18:02:09 +02:00
if (!strcmp(*PMODE, "tilt") || **PSHAKE) calculate(true);
2024-06-26 17:14:16 +02:00
}
void CDynamicCursors::calculate(bool tick) {
2024-06-26 17:14:16 +02:00
static auto const* PMODE = (Hyprlang::STRING const*)HyprlandAPI::getConfigValue(PHANDLE, CONFIG_MODE)->getDataStaticPtr();
2024-06-26 18:02:09 +02:00
static auto* const* PTHRESHOLD = (Hyprlang::INT* const*)HyprlandAPI::getConfigValue(PHANDLE, CONFIG_THRESHOLD)->getDataStaticPtr();
2024-06-27 17:21:39 +02:00
static auto* const* PSHAKE = (Hyprlang::INT* const*)HyprlandAPI::getConfigValue(PHANDLE, CONFIG_SHAKE)->getDataStaticPtr();
static auto* const* PSHAKE_EFFECTS = (Hyprlang::INT* const*)HyprlandAPI::getConfigValue(PHANDLE, CONFIG_SHAKE_EFFECTS)->getDataStaticPtr();
// only calculate zoom on tick
// yes, a refactor of this whole file would be a godsent
double zoom = tick ? 1 : this->zoom;
if (**PSHAKE && tick)
2024-06-27 17:21:39 +02:00
zoom = calculateShake();
2024-06-26 17:14:16 +02:00
double angle = 0;
2024-06-26 18:02:09 +02:00
if (!strcmp(*PMODE, "rotate"))
2024-06-26 17:14:16 +02:00
angle = calculateStick();
2024-06-26 18:02:09 +02:00
else if (!strcmp(*PMODE, "tilt"))
2024-06-26 17:14:16 +02:00
angle = calculateAir();
2024-06-27 17:21:39 +02:00
else if (strcmp(*PMODE, "none")) // if not none, print warning
2024-06-26 18:02:09 +02:00
Debug::log(WARN, "[dynamic-cursors] unknown mode specified");
2024-06-26 17:14:16 +02:00
2024-06-27 18:04:49 +02:00
if (zoom > 1) {
if (!**PSHAKE_EFFECTS) angle = 0;
if (!software) {
g_pPointerManager->lockSoftwareAll();
software = true;
}
} else {
if (software) {
// damage so it is cleared
g_pPointerManager->damageIfSoftware();
g_pPointerManager->unlockSoftwareAll();
software = false;
}
}
2024-06-27 17:21:39 +02:00
// we only consider the angle changed if it is larger than the threshold
2024-06-27 18:04:49 +02:00
if (abs(this->angle - angle) > ((PI / 180) * **PTHRESHOLD) || abs(this->zoom - zoom) > 0.1 || (zoom == 1 && this->zoom != 1)) {
2024-06-26 17:14:16 +02:00
this->angle = angle;
2024-06-27 17:21:39 +02:00
this->zoom = zoom;
2024-06-26 17:14:16 +02:00
// damage software and change hardware cursor shape
g_pPointerManager->damageIfSoftware();
for (auto& m : g_pCompositor->m_vMonitors) {
auto state = g_pPointerManager->stateFor(m);
if (state->hardwareFailed || !state->entered)
continue;
g_pPointerManager->attemptHardwareCursor(state);
}
}
}
double airFunction(double speed) {
static auto const* PFUNCTION = (Hyprlang::STRING const*)HyprlandAPI::getConfigValue(PHANDLE, CONFIG_FUNCTION)->getDataStaticPtr();
static auto* const* PMASS = (Hyprlang::INT* const*)HyprlandAPI::getConfigValue(PHANDLE, CONFIG_MASS)->getDataStaticPtr();
double mass = **PMASS;
double result = 0;
if (!strcmp(*PFUNCTION, "linear")) {
result = speed / **PMASS;
} else if (!strcmp(*PFUNCTION, "quadratic")) {
// (1 / m²) * x², is a quadratic function which will reach 1 at m
result = (1.0 / (mass * mass)) * (speed * speed);
result *= (speed > 0 ? 1 : -1);
} else if (!strcmp(*PFUNCTION, "negative_quadratic")) {
float x = std::abs(speed);
// (-1 / m²) * (x - m)² + 1, is a quadratic function with the inverse curvature which will reach 1 at m
result = (-1.0 / (mass * mass)) * ((x - mass) * (x - mass)) + 1;
if (x > mass) result = 1; // need to clamp manually, as the function would decrease again
result *= (speed > 0 ? 1 : -1);
2024-06-26 18:02:09 +02:00
} else
2024-06-26 17:14:16 +02:00
Debug::log(WARN, "[dynamic-cursors] unknown air function specified");
2024-06-26 17:14:16 +02:00
return std::clamp(result, -1.0, 1.0);
}
double CDynamicCursors::calculateAir() {
// create samples array
int max = g_pHyprRenderer->m_pMostHzMonitor->refreshRate / 10; // 100ms worth of history
samples.resize(max);
// capture current sample
samples[samples_index] = Vector2D{g_pPointerManager->pointerPos};
int current = samples_index;
samples_index = (samples_index + 1) % max; // increase for next sample
int first = samples_index;
// calculate speed and tilt
double speed = (samples[current].x - samples[first].x) / 0.1;
return airFunction(speed) * (PI / 3); // 120° in both directions
}
2024-06-27 17:21:39 +02:00
double CDynamicCursors::calculateShake() {
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();
int max = g_pHyprRenderer->m_pMostHzMonitor->refreshRate; // 1s worth of history
shake_samples.resize(max);
shake_samples_distance.resize(max);
int previous = shake_samples_index == 0 ? max - 1 : shake_samples_index - 1;
shake_samples[shake_samples_index] = Vector2D{g_pPointerManager->pointerPos};
shake_samples_distance[shake_samples_index] = shake_samples[shake_samples_index].distance(shake_samples[previous]);
shake_samples_index = (shake_samples_index + 1) % max; // increase for next sample
/*
The idea for this algorith was largely inspired by KDE Plasma
https://invent.kde.org/plasma/kwin/-/blob/master/src/plugins/shakecursor/shakedetector.cpp
*/
// calculate total distance travelled
double trail = 0;
for (double distance : shake_samples_distance) trail += distance;
// calculate diagonal of bounding box travelled within
double left = 1e100, right = 0, bottom = 0, top = 1e100;
for (Vector2D position : shake_samples) {
left = std::min(left, position.x);
right = std::max(right, position.x);
top = std::min(top, position.y);
bottom = std::max(bottom, position.y);
}
double diagonal = Vector2D{left, top}.distance(Vector2D(right, bottom));
// 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);
}
2024-06-26 17:14:16 +02:00
double CDynamicCursors::calculateStick() {
2024-06-21 16:06:55 +02:00
static auto* const* PLENGTH = (Hyprlang::INT* const*)HyprlandAPI::getConfigValue(PHANDLE, CONFIG_LENGTH)->getDataStaticPtr();
2024-06-26 17:14:16 +02:00
auto pos = g_pPointerManager->pointerPos;
2024-06-21 14:32:42 +02:00
// translate to origin
2024-06-26 17:14:16 +02:00
end.x -= pos.x;
end.y -= pos.y;
2024-06-21 14:32:42 +02:00
// normalize
2024-06-21 16:06:55 +02:00
double size = end.size();
end.x /= size;
end.y /= size;
2024-06-21 14:32:42 +02:00
// scale to length
2024-06-21 16:06:55 +02:00
end.x *= **PLENGTH;
end.y *= **PLENGTH;
2024-06-21 14:32:42 +02:00
// calculate angle
2024-06-21 16:06:55 +02:00
double angle = -atan(end.x / end.y);
if (end.y > 0) angle += PI;
2024-06-21 14:32:42 +02:00
angle += PI;
// translate back
2024-06-26 17:14:16 +02:00
end.x += pos.x;
end.y += pos.y;
2024-06-26 17:14:16 +02:00
return angle;
2024-06-21 14:32:42 +02:00
}