summaryrefslogtreecommitdiff
path: root/src/desktop
diff options
context:
space:
mode:
authorVaxry <vaxry@vaxry.net>2024-04-26 23:55:41 +0100
committerVaxry <vaxry@vaxry.net>2024-04-27 03:17:04 +0100
commit25aec3ac8ce65ed224f025f8f6dfef73780577a4 (patch)
tree9fa63de1670bd2f535f3bc237800fc6b7a81f646 /src/desktop
parentf94264928a8ab4da8759d4ded25a46af44451d38 (diff)
pointer-constraints: move to new impl
Diffstat (limited to 'src/desktop')
-rw-r--r--src/desktop/Constraint.cpp142
-rw-r--r--src/desktop/Constraint.hpp44
-rw-r--r--src/desktop/WLSurface.cpp13
-rw-r--r--src/desktop/WLSurface.hpp23
4 files changed, 21 insertions, 201 deletions
diff --git a/src/desktop/Constraint.cpp b/src/desktop/Constraint.cpp
deleted file mode 100644
index 428c8745..00000000
--- a/src/desktop/Constraint.cpp
+++ /dev/null
@@ -1,142 +0,0 @@
-#include "Constraint.hpp"
-#include "WLSurface.hpp"
-#include "../Compositor.hpp"
-#include "../config/ConfigValue.hpp"
-
-CConstraint::CConstraint(wlr_pointer_constraint_v1* constraint, CWLSurface* owner) : m_pOwner(owner), m_pConstraint(constraint) {
- RASSERT(!constraint->data, "CConstraint: attempted to duplicate ownership");
-
- constraint->data = this;
- initSignals();
-
- m_vCursorPosOnActivate = g_pInputManager->getMouseCoordsInternal();
-
- g_pInputManager->m_vConstraints.push_back(this);
-
- if (g_pCompositor->m_pLastFocus == m_pOwner->wlr())
- activate();
-}
-
-CConstraint::~CConstraint() {
- std::erase(g_pInputManager->m_vConstraints, this);
-}
-
-static void onConstraintDestroy(void* owner, void* data) {
- const auto CONSTRAINT = (CConstraint*)owner;
- CONSTRAINT->onDestroy();
-}
-
-static void onConstraintSetRegion(void* owner, void* data) {
- const auto CONSTRAINT = (CConstraint*)owner;
- CONSTRAINT->onSetRegion();
-}
-
-void CConstraint::initSignals() {
- hyprListener_setConstraintRegion.initCallback(&m_pConstraint->events.set_region, ::onConstraintSetRegion, this, "CConstraint");
- hyprListener_destroyConstraint.initCallback(&m_pConstraint->events.destroy, ::onConstraintDestroy, this, "CConstraint");
-}
-
-void CConstraint::onDestroy() {
- hyprListener_setConstraintRegion.removeCallback();
- hyprListener_destroyConstraint.removeCallback();
-
- if (active() && isLocked())
- g_pCompositor->warpCursorTo(logicPositionHint(), true);
-
- // this is us
- m_pOwner->m_pConstraint.reset();
-}
-
-void CConstraint::onSetRegion() {
- if (!m_bActive)
- return;
-
- m_rRegion.set(&m_pConstraint->region);
- m_vPositionHint = m_rRegion.closestPoint(m_vPositionHint);
- g_pInputManager->simulateMouseMovement(); // to warp the cursor if anything's amiss
-}
-
-void CConstraint::onCommit() {
- if (!m_bActive)
- return;
-
- const auto COMMITTED = m_pConstraint->current.committed;
-
- if (COMMITTED & WLR_POINTER_CONSTRAINT_V1_STATE_CURSOR_HINT) {
- static auto PXWLFORCESCALEZERO = CConfigValue<Hyprlang::INT>("xwayland:force_zero_scaling");
-
- m_bHintSet = true;
-
- float scale = 1.f;
- const auto PWINDOW = m_pOwner->getWindow();
- if (PWINDOW) {
- const auto ISXWL = PWINDOW->m_bIsX11;
- scale = ISXWL && *PXWLFORCESCALEZERO ? PWINDOW->m_fX11SurfaceScaledBy : 1.f;
- }
-
- m_vPositionHint = {m_pConstraint->current.cursor_hint.x / scale, m_pConstraint->current.cursor_hint.y / scale};
- g_pInputManager->simulateMouseMovement();
- }
-
- if (COMMITTED & WLR_POINTER_CONSTRAINT_V1_STATE_REGION)
- onSetRegion();
-}
-
-CRegion CConstraint::logicConstraintRegion() {
- CRegion rg = m_rRegion;
- const auto SURFBOX = m_pOwner->getSurfaceBoxGlobal();
- const auto CONSTRAINTPOS = SURFBOX.has_value() ? SURFBOX->pos() : Vector2D{};
- rg.translate(CONSTRAINTPOS);
- return rg;
-}
-
-CWLSurface* CConstraint::owner() {
- return m_pOwner;
-}
-
-bool CConstraint::isLocked() {
- return m_pConstraint->type == WLR_POINTER_CONSTRAINT_V1_LOCKED;
-}
-
-Vector2D CConstraint::logicPositionHint() {
- const auto SURFBOX = m_pOwner->getSurfaceBoxGlobal();
- const auto CONSTRAINTPOS = SURFBOX.has_value() ? SURFBOX->pos() : Vector2D{};
-
- return m_bHintSet ? CONSTRAINTPOS + m_vPositionHint : m_vCursorPosOnActivate;
-}
-
-void CConstraint::deactivate() {
- if (!m_bActive)
- return;
-
- m_bActive = false;
-
- if (isLocked())
- g_pCompositor->warpCursorTo(logicPositionHint(), true);
-
- if (m_pConstraint->lifetime == ZWP_POINTER_CONSTRAINTS_V1_LIFETIME_ONESHOT)
- m_bDead = true;
-
- wlr_pointer_constraint_v1_send_deactivated(m_pConstraint);
-}
-
-void CConstraint::activate() {
- if (m_bActive || m_bDead)
- return;
-
- m_bActive = true;
-
- // TODO: hack, probably not a super duper great idea
- if (g_pCompositor->m_sSeat.seat->pointer_state.focused_surface != m_pOwner->wlr()) {
- const auto SURFBOX = m_pOwner->getSurfaceBoxGlobal();
- const auto LOCAL = SURFBOX.has_value() ? logicPositionHint() - SURFBOX->pos() : Vector2D{};
- wlr_seat_pointer_enter(g_pCompositor->m_sSeat.seat, m_pOwner->wlr(), LOCAL.x, LOCAL.y);
- }
-
- g_pCompositor->warpCursorTo(logicPositionHint(), true);
- wlr_pointer_constraint_v1_send_activated(m_pConstraint);
-}
-
-bool CConstraint::active() {
- return m_bActive;
-}
diff --git a/src/desktop/Constraint.hpp b/src/desktop/Constraint.hpp
deleted file mode 100644
index 34690217..00000000
--- a/src/desktop/Constraint.hpp
+++ /dev/null
@@ -1,44 +0,0 @@
-#pragma once
-
-#include "../includes.hpp"
-#include "../helpers/Region.hpp"
-#include "../helpers/WLListener.hpp"
-
-class CWLSurface;
-
-class CConstraint {
- public:
- CConstraint(wlr_pointer_constraint_v1* constraint, CWLSurface* owner);
- ~CConstraint();
-
- void onCommit();
- void onDestroy();
- void onSetRegion();
- CRegion logicConstraintRegion();
- bool isLocked();
- Vector2D logicPositionHint();
-
- void deactivate();
- void activate();
- bool active();
-
- CWLSurface* owner();
-
- private:
- bool m_bActive = false;
- CWLSurface* m_pOwner = nullptr;
- wlr_pointer_constraint_v1* m_pConstraint;
-
- CRegion m_rRegion;
- bool m_bHintSet = false;
- Vector2D m_vPositionHint = {-1, -1};
- Vector2D m_vCursorPosOnActivate = {-1, -1};
-
- // for oneshot constraints that have been activated once
- bool m_bDead = false;
-
- DYNLISTENER(destroyConstraint);
- DYNLISTENER(setConstraintRegion);
-
- void initSignals();
-}; \ No newline at end of file
diff --git a/src/desktop/WLSurface.cpp b/src/desktop/WLSurface.cpp
index b102c5ce..4c2f6c84 100644
--- a/src/desktop/WLSurface.cpp
+++ b/src/desktop/WLSurface.cpp
@@ -103,6 +103,8 @@ void CWLSurface::destroy() {
if (!m_pWLRSurface)
return;
+ events.destroy.emit();
+
m_pConstraint.reset();
hyprListener_destroy.removeCallback();
@@ -182,15 +184,14 @@ std::optional<CBox> CWLSurface::getSurfaceBoxGlobal() {
return {};
}
-void CWLSurface::appendConstraint(wlr_pointer_constraint_v1* constraint) {
- m_pConstraint = std::make_unique<CConstraint>(constraint, this);
+void CWLSurface::appendConstraint(std::weak_ptr<CPointerConstraint> constraint) {
+ m_pConstraint = constraint;
}
void CWLSurface::onCommit() {
- if (m_pConstraint)
- m_pConstraint->onCommit();
+ ;
}
-CConstraint* CWLSurface::constraint() {
- return m_pConstraint.get();
+std::shared_ptr<CPointerConstraint> CWLSurface::constraint() {
+ return m_pConstraint.lock();
}
diff --git a/src/desktop/WLSurface.hpp b/src/desktop/WLSurface.hpp
index a47b2bdd..d018d11f 100644
--- a/src/desktop/WLSurface.hpp
+++ b/src/desktop/WLSurface.hpp
@@ -2,12 +2,13 @@
#include "../defines.hpp"
#include "../helpers/Region.hpp"
-#include "Constraint.hpp"
+#include "../helpers/signal/Signal.hpp"
class CWindow;
struct SLayerSurface;
class CSubsurface;
class CPopup;
+class CPointerConstraint;
class CWLSurface {
public:
@@ -42,9 +43,9 @@ class CWLSurface {
CSubsurface* getSubsurface();
// desktop components misc utils
- std::optional<CBox> getSurfaceBoxGlobal();
- void appendConstraint(wlr_pointer_constraint_v1* constraint);
- CConstraint* constraint();
+ std::optional<CBox> getSurfaceBoxGlobal();
+ void appendConstraint(std::weak_ptr<CPointerConstraint> constraint);
+ std::shared_ptr<CPointerConstraint> constraint();
// allow stretching. Useful for plugins.
bool m_bFillIgnoreSmall = false;
@@ -84,6 +85,10 @@ class CWLSurface {
// used by the alpha-modifier protocol
float m_pAlphaModifier = 1.F;
+ struct {
+ CSignal destroy;
+ } events;
+
private:
bool m_bInert = true;
@@ -95,14 +100,14 @@ class CWLSurface {
CSubsurface* m_pSubsurfaceOwner = nullptr;
//
- std::unique_ptr<CConstraint> m_pConstraint;
+ std::weak_ptr<CPointerConstraint> m_pConstraint;
- void destroy();
- void init();
- bool desktopComponent();
+ void destroy();
+ void init();
+ bool desktopComponent();
DYNLISTENER(destroy);
DYNLISTENER(commit);
- friend class CConstraint;
+ friend class CPointerConstraint;
}; \ No newline at end of file