Hyprland/src/managers/input/Swipe.cpp

257 lines
12 KiB
C++
Raw Normal View History

2022-07-07 11:52:12 +02:00
#include "InputManager.hpp"
#include "../../Compositor.hpp"
void CInputManager::onSwipeBegin(wlr_pointer_swipe_begin_event* e) {
static auto *const PSWIPE = &g_pConfigManager->getConfigValuePtr("gestures:workspace_swipe")->intValue;
2022-07-19 18:35:24 +02:00
static auto *const PSWIPEFINGERS = &g_pConfigManager->getConfigValuePtr("gestures:workspace_swipe_fingers")->intValue;
2022-10-20 15:54:32 +02:00
static auto *const PSWIPENEW = &g_pConfigManager->getConfigValuePtr("gestures:workspace_swipe_create_new")->intValue;
2022-07-07 11:52:12 +02:00
2022-08-27 21:37:35 +02:00
if (e->fingers != *PSWIPEFINGERS || *PSWIPE == 0)
2022-07-07 11:52:12 +02:00
return;
int onMonitor = 0;
for (auto& w : g_pCompositor->m_vWorkspaces) {
2022-09-17 16:05:12 +02:00
if (w->m_iMonitorID == g_pCompositor->m_pLastMonitor->ID && w->m_iID != SPECIAL_WORKSPACE_ID) {
onMonitor++;
}
}
2022-10-20 15:54:32 +02:00
if (onMonitor < 2 && !*PSWIPENEW)
return; // disallow swiping when there's 1 workspace on a monitor
2022-07-07 11:52:12 +02:00
const auto PWORKSPACE = g_pCompositor->getWorkspaceByID(g_pCompositor->m_pLastMonitor->activeWorkspace);
Debug::log(LOG, "Starting a swipe from %s", PWORKSPACE->m_szName.c_str());
m_sActiveSwipe.pWorkspaceBegin = PWORKSPACE;
m_sActiveSwipe.delta = 0;
m_sActiveSwipe.pMonitor = g_pCompositor->m_pLastMonitor;
m_sActiveSwipe.avgSpeed = 0;
m_sActiveSwipe.speedPoints = 0;
2022-09-19 11:23:13 +02:00
if (PWORKSPACE->m_bHasFullscreenWindow) {
for (auto& ls : g_pCompositor->m_pLastMonitor->m_aLayerSurfaceLists[ZWLR_LAYER_SHELL_V1_LAYER_TOP]) {
ls->alpha = 255.f;
}
}
2022-07-07 11:52:12 +02:00
}
void CInputManager::onSwipeEnd(wlr_pointer_swipe_end_event* e) {
2022-07-07 20:53:22 +02:00
if (!m_sActiveSwipe.pWorkspaceBegin)
return; // no valid swipe
2022-07-07 11:52:12 +02:00
static auto *const PSWIPEPERC = &g_pConfigManager->getConfigValuePtr("gestures:workspace_swipe_cancel_ratio")->floatValue;
static auto *const PSWIPEDIST = &g_pConfigManager->getConfigValuePtr("gestures:workspace_swipe_distance")->intValue;
static auto *const PSWIPEFORC = &g_pConfigManager->getConfigValuePtr("gestures:workspace_swipe_min_speed_to_force")->intValue;
2022-10-20 15:54:32 +02:00
static auto *const PSWIPENEW = &g_pConfigManager->getConfigValuePtr("gestures:workspace_swipe_create_new")->intValue;
2022-10-07 17:52:53 +02:00
const bool VERTANIMS = m_sActiveSwipe.pWorkspaceBegin->m_vRenderOffset.getConfig()->pValues->internalStyle == "slidevert";
2022-07-07 11:52:12 +02:00
// commit
std::string wsname = "";
auto workspaceIDLeft = getWorkspaceIDFromString("m-1", wsname);
auto workspaceIDRight = getWorkspaceIDFromString("m+1", wsname);
2022-10-20 15:54:32 +02:00
if ((workspaceIDRight <= m_sActiveSwipe.pWorkspaceBegin->m_iID || workspaceIDRight == workspaceIDLeft) && *PSWIPENEW) {
workspaceIDRight = m_sActiveSwipe.pWorkspaceBegin->m_iID > 0 ? m_sActiveSwipe.pWorkspaceBegin->m_iID + 1 : 1;
}
auto PWORKSPACER = g_pCompositor->getWorkspaceByID(workspaceIDRight); // not guaranteed if PSWIPENEW
2022-07-07 11:52:12 +02:00
const auto PWORKSPACEL = g_pCompositor->getWorkspaceByID(workspaceIDLeft);
const auto RENDEROFFSETMIDDLE = m_sActiveSwipe.pWorkspaceBegin->m_vRenderOffset.vec();
2022-09-19 11:23:13 +02:00
CWorkspace* pSwitchedTo = nullptr;
2022-07-07 11:52:12 +02:00
if ((abs(m_sActiveSwipe.delta) < *PSWIPEDIST * *PSWIPEPERC && (*PSWIPEFORC == 0 || (*PSWIPEFORC != 0 && m_sActiveSwipe.avgSpeed < *PSWIPEFORC))) || abs(m_sActiveSwipe.delta) < 2) {
// revert
2022-07-25 14:06:49 +02:00
if (abs(m_sActiveSwipe.delta) < 2) {
PWORKSPACEL->m_vRenderOffset.setValueAndWarp(Vector2D(0,0));
2022-10-20 15:54:32 +02:00
if (PWORKSPACER)
PWORKSPACER->m_vRenderOffset.setValueAndWarp(Vector2D(0,0));
2022-07-25 14:06:49 +02:00
m_sActiveSwipe.pWorkspaceBegin->m_vRenderOffset.setValueAndWarp(Vector2D(0,0));
2022-07-07 11:52:12 +02:00
} else {
2022-07-25 14:06:49 +02:00
if (m_sActiveSwipe.delta < 0) {
// to left
2022-10-07 17:52:53 +02:00
if (VERTANIMS)
PWORKSPACEL->m_vRenderOffset = Vector2D({0, -m_sActiveSwipe.pMonitor->vecSize.y});
else
PWORKSPACEL->m_vRenderOffset = Vector2D({-m_sActiveSwipe.pMonitor->vecSize.x, 0});
2022-10-20 15:54:32 +02:00
} else if (PWORKSPACER) {
2022-07-25 14:06:49 +02:00
// to right
2022-10-07 17:52:53 +02:00
if (VERTANIMS)
PWORKSPACER->m_vRenderOffset = Vector2D({0, m_sActiveSwipe.pMonitor->vecSize.y});
else
PWORKSPACER->m_vRenderOffset = Vector2D({m_sActiveSwipe.pMonitor->vecSize.x, 0});
2022-07-25 14:06:49 +02:00
}
m_sActiveSwipe.pWorkspaceBegin->m_vRenderOffset = Vector2D();
2022-07-07 11:52:12 +02:00
}
2022-09-19 11:23:13 +02:00
pSwitchedTo = m_sActiveSwipe.pWorkspaceBegin;
2022-07-07 11:52:12 +02:00
} else if (m_sActiveSwipe.delta < 0) {
// switch to left
const auto RENDEROFFSET = PWORKSPACEL->m_vRenderOffset.vec();
g_pKeybindManager->m_mDispatchers["workspace"]("[internal]" + std::to_string(workspaceIDLeft));
PWORKSPACEL->m_vRenderOffset.setValue(RENDEROFFSET);
PWORKSPACEL->m_fAlpha.setValueAndWarp(255.f);
2022-07-19 18:30:53 +02:00
m_sActiveSwipe.pWorkspaceBegin->m_vRenderOffset.setValue(RENDEROFFSETMIDDLE);
2022-10-07 17:52:53 +02:00
if (VERTANIMS)
m_sActiveSwipe.pWorkspaceBegin->m_vRenderOffset = Vector2D(0, m_sActiveSwipe.pMonitor->vecSize.y);
else
m_sActiveSwipe.pWorkspaceBegin->m_vRenderOffset = Vector2D(m_sActiveSwipe.pMonitor->vecSize.x, 0);
2022-07-19 18:30:53 +02:00
m_sActiveSwipe.pWorkspaceBegin->m_fAlpha.setValueAndWarp(255.f);
2022-08-09 20:36:21 +02:00
g_pInputManager->unconstrainMouse();
2022-07-26 18:38:30 +02:00
2022-07-07 11:52:12 +02:00
Debug::log(LOG, "Ended swipe to the left");
2022-09-19 11:23:13 +02:00
pSwitchedTo = PWORKSPACEL;
2022-07-07 11:52:12 +02:00
} else {
// switch to right
2022-10-20 15:54:32 +02:00
const auto RENDEROFFSET = PWORKSPACER ? PWORKSPACER->m_vRenderOffset.vec() : Vector2D();
if (PWORKSPACER)
g_pKeybindManager->m_mDispatchers["workspace"]("[internal]" + std::to_string(workspaceIDRight));
else
g_pKeybindManager->m_mDispatchers["workspace"](std::to_string(workspaceIDRight)); // so that the ID is created properly
2022-07-07 11:52:12 +02:00
2022-10-20 15:54:32 +02:00
if (!PWORKSPACER)
PWORKSPACER = g_pCompositor->getWorkspaceByID(workspaceIDRight); // not guaranteed on PSWIPENEW
2022-07-07 11:52:12 +02:00
PWORKSPACER->m_vRenderOffset.setValue(RENDEROFFSET);
PWORKSPACER->m_fAlpha.setValueAndWarp(255.f);
2022-07-19 18:30:53 +02:00
m_sActiveSwipe.pWorkspaceBegin->m_vRenderOffset.setValue(RENDEROFFSETMIDDLE);
2022-10-07 17:52:53 +02:00
if (VERTANIMS)
m_sActiveSwipe.pWorkspaceBegin->m_vRenderOffset = Vector2D(0, -m_sActiveSwipe.pMonitor->vecSize.y);
else
m_sActiveSwipe.pWorkspaceBegin->m_vRenderOffset = Vector2D(-m_sActiveSwipe.pMonitor->vecSize.x, 0);
2022-07-19 18:30:53 +02:00
m_sActiveSwipe.pWorkspaceBegin->m_fAlpha.setValueAndWarp(255.f);
2022-08-09 20:36:21 +02:00
g_pInputManager->unconstrainMouse();
2022-07-26 18:38:30 +02:00
2022-07-07 11:52:12 +02:00
Debug::log(LOG, "Ended swipe to the right");
2022-09-19 11:23:13 +02:00
pSwitchedTo = PWORKSPACER;
2022-07-07 11:52:12 +02:00
}
2022-08-16 16:34:36 +02:00
g_pHyprRenderer->damageMonitor(m_sActiveSwipe.pMonitor);
2022-07-07 11:52:12 +02:00
PWORKSPACEL->m_bForceRendering = false;
2022-10-20 15:54:32 +02:00
if (PWORKSPACER)
PWORKSPACER->m_bForceRendering = false;
2022-07-07 11:52:12 +02:00
m_sActiveSwipe.pWorkspaceBegin->m_bForceRendering = false;
m_sActiveSwipe.pWorkspaceBegin = nullptr;
g_pInputManager->refocus();
2022-09-19 11:23:13 +02:00
// apply alpha
for (auto& ls : g_pCompositor->m_pLastMonitor->m_aLayerSurfaceLists[ZWLR_LAYER_SHELL_V1_LAYER_TOP]) {
ls->alpha = pSwitchedTo->m_bHasFullscreenWindow ? 0.f : 255.f;
}
2022-07-07 11:52:12 +02:00
}
void CInputManager::onSwipeUpdate(wlr_pointer_swipe_update_event* e) {
if (!m_sActiveSwipe.pWorkspaceBegin)
return;
static auto *const PSWIPEDIST = &g_pConfigManager->getConfigValuePtr("gestures:workspace_swipe_distance")->intValue;
static auto *const PSWIPEINVR = &g_pConfigManager->getConfigValuePtr("gestures:workspace_swipe_invert")->intValue;
2022-10-20 15:54:32 +02:00
static auto *const PSWIPENEW = &g_pConfigManager->getConfigValuePtr("gestures:workspace_swipe_create_new")->intValue;
2022-07-07 11:52:12 +02:00
2022-10-07 17:52:53 +02:00
const bool VERTANIMS = m_sActiveSwipe.pWorkspaceBegin->m_vRenderOffset.getConfig()->pValues->internalStyle == "slidevert";
m_sActiveSwipe.delta += VERTANIMS ? (*PSWIPEINVR ? -e->dy : e->dy) : (*PSWIPEINVR ? -e->dx : e->dx);
2022-07-07 11:52:12 +02:00
m_sActiveSwipe.avgSpeed = (m_sActiveSwipe.avgSpeed * m_sActiveSwipe.speedPoints + abs(e->dx)) / (m_sActiveSwipe.speedPoints + 1);
m_sActiveSwipe.speedPoints++;
std::string wsname = "";
auto workspaceIDLeft = getWorkspaceIDFromString("m-1", wsname);
auto workspaceIDRight = getWorkspaceIDFromString("m+1", wsname);
2022-10-20 15:54:32 +02:00
if ((workspaceIDLeft == INT_MAX || workspaceIDRight == INT_MAX || workspaceIDLeft == m_sActiveSwipe.pWorkspaceBegin->m_iID) && !*PSWIPENEW) {
2022-09-17 16:05:12 +02:00
m_sActiveSwipe.pWorkspaceBegin = nullptr; // invalidate the swipe
2022-07-07 11:52:12 +02:00
return;
2022-09-17 16:05:12 +02:00
}
2022-07-07 11:52:12 +02:00
2022-07-22 00:13:56 +02:00
m_sActiveSwipe.pWorkspaceBegin->m_bForceRendering = true;
2022-07-07 11:52:12 +02:00
m_sActiveSwipe.delta = std::clamp(m_sActiveSwipe.delta, (double)-*PSWIPEDIST, (double)*PSWIPEDIST);
if (((m_sActiveSwipe.pWorkspaceBegin->m_iID == workspaceIDLeft || m_sActiveSwipe.pWorkspaceBegin->m_iID == workspaceIDRight) && *PSWIPENEW && (m_sActiveSwipe.delta < 0)) || (m_sActiveSwipe.delta > 0 && g_pCompositor->getWindowsOnWorkspace(m_sActiveSwipe.pWorkspaceBegin->m_iID) == 0 && workspaceIDRight <= m_sActiveSwipe.pWorkspaceBegin->m_iID)) {
2022-10-20 15:54:32 +02:00
m_sActiveSwipe.delta = 0;
return;
}
2022-07-07 11:52:12 +02:00
if (m_sActiveSwipe.delta < 0) {
2022-10-20 15:54:32 +02:00
if (workspaceIDLeft > m_sActiveSwipe.pWorkspaceBegin->m_iID && !*PSWIPENEW){
2022-07-07 11:52:12 +02:00
m_sActiveSwipe.delta = 0;
return;
}
const auto PWORKSPACE = g_pCompositor->getWorkspaceByID(workspaceIDLeft);
PWORKSPACE->m_bForceRendering = true;
2022-08-19 17:36:01 +02:00
PWORKSPACE->m_fAlpha.setValueAndWarp(255.f);
2022-07-07 11:52:12 +02:00
2022-07-25 14:06:49 +02:00
if (workspaceIDLeft != workspaceIDRight) {
const auto PWORKSPACER = g_pCompositor->getWorkspaceByID(workspaceIDRight);
2022-07-22 00:10:26 +02:00
2022-07-25 14:06:49 +02:00
PWORKSPACER->m_bForceRendering = false;
2022-08-19 17:42:10 +02:00
PWORKSPACER->m_fAlpha.setValueAndWarp(0.f);
2022-07-25 14:06:49 +02:00
}
2022-07-07 11:52:12 +02:00
2022-10-07 17:52:53 +02:00
if (VERTANIMS) {
PWORKSPACE->m_vRenderOffset.setValueAndWarp(Vector2D(0, ((-m_sActiveSwipe.delta) / *PSWIPEDIST) * m_sActiveSwipe.pMonitor->vecSize.y - m_sActiveSwipe.pMonitor->vecSize.y));
m_sActiveSwipe.pWorkspaceBegin->m_vRenderOffset.setValueAndWarp(Vector2D(0, ((-m_sActiveSwipe.delta) / *PSWIPEDIST) * m_sActiveSwipe.pMonitor->vecSize.y));
} else {
PWORKSPACE->m_vRenderOffset.setValueAndWarp(Vector2D(((-m_sActiveSwipe.delta) / *PSWIPEDIST) * m_sActiveSwipe.pMonitor->vecSize.x - m_sActiveSwipe.pMonitor->vecSize.x, 0));
m_sActiveSwipe.pWorkspaceBegin->m_vRenderOffset.setValueAndWarp(Vector2D(((-m_sActiveSwipe.delta) / *PSWIPEDIST) * m_sActiveSwipe.pMonitor->vecSize.x, 0));
}
2022-07-07 11:52:12 +02:00
g_pCompositor->updateWorkspaceWindowDecos(workspaceIDLeft);
} else {
2022-10-20 16:02:46 +02:00
if (workspaceIDRight < m_sActiveSwipe.pWorkspaceBegin->m_iID) {
if (*PSWIPENEW) {
if (VERTANIMS)
m_sActiveSwipe.pWorkspaceBegin->m_vRenderOffset.setValueAndWarp(Vector2D(0, ((-m_sActiveSwipe.delta) / *PSWIPEDIST) * m_sActiveSwipe.pMonitor->vecSize.y));
else
m_sActiveSwipe.pWorkspaceBegin->m_vRenderOffset.setValueAndWarp(Vector2D(((-m_sActiveSwipe.delta) / *PSWIPEDIST) * m_sActiveSwipe.pMonitor->vecSize.x, 0));
return;
}
2022-07-07 11:52:12 +02:00
m_sActiveSwipe.delta = 0;
return;
}
const auto PWORKSPACE = g_pCompositor->getWorkspaceByID(workspaceIDRight);
PWORKSPACE->m_bForceRendering = true;
2022-08-19 17:36:01 +02:00
PWORKSPACE->m_fAlpha.setValueAndWarp(255.f);
2022-07-07 11:52:12 +02:00
2022-07-25 14:06:49 +02:00
if (workspaceIDLeft != workspaceIDRight) {
const auto PWORKSPACEL = g_pCompositor->getWorkspaceByID(workspaceIDLeft);
2022-07-22 00:10:26 +02:00
2022-07-25 14:06:49 +02:00
PWORKSPACEL->m_bForceRendering = false;
2022-08-19 17:42:10 +02:00
PWORKSPACEL->m_fAlpha.setValueAndWarp(0.f);
2022-07-25 14:06:49 +02:00
}
2022-07-07 11:52:12 +02:00
2022-10-07 17:52:53 +02:00
if (VERTANIMS) {
PWORKSPACE->m_vRenderOffset.setValueAndWarp(Vector2D(0, ((-m_sActiveSwipe.delta) / *PSWIPEDIST) * m_sActiveSwipe.pMonitor->vecSize.y + m_sActiveSwipe.pMonitor->vecSize.y));
m_sActiveSwipe.pWorkspaceBegin->m_vRenderOffset.setValueAndWarp(Vector2D(0, ((-m_sActiveSwipe.delta) / *PSWIPEDIST) * m_sActiveSwipe.pMonitor->vecSize.y));
} else {
PWORKSPACE->m_vRenderOffset.setValueAndWarp(Vector2D(((-m_sActiveSwipe.delta) / *PSWIPEDIST) * m_sActiveSwipe.pMonitor->vecSize.x + m_sActiveSwipe.pMonitor->vecSize.x, 0));
m_sActiveSwipe.pWorkspaceBegin->m_vRenderOffset.setValueAndWarp(Vector2D(((-m_sActiveSwipe.delta) / *PSWIPEDIST) * m_sActiveSwipe.pMonitor->vecSize.x, 0));
}
2022-07-07 11:52:12 +02:00
g_pCompositor->updateWorkspaceWindowDecos(workspaceIDRight);
}
g_pHyprRenderer->damageMonitor(m_sActiveSwipe.pMonitor);
g_pCompositor->updateWorkspaceWindowDecos(m_sActiveSwipe.pWorkspaceBegin->m_iID);
}