From 3878f806ff771651829977a01b84313c34997285 Mon Sep 17 00:00:00 2001 From: Vaxry Date: Wed, 24 Apr 2024 21:12:26 +0100 Subject: pointer-gestures: move to new impl --- src/protocols/PointerGestures.cpp | 246 ++++++++++++++++++++++++++++++++++++++ src/protocols/PointerGestures.hpp | 84 +++++++++++++ 2 files changed, 330 insertions(+) create mode 100644 src/protocols/PointerGestures.cpp create mode 100644 src/protocols/PointerGestures.hpp (limited to 'src/protocols') diff --git a/src/protocols/PointerGestures.cpp b/src/protocols/PointerGestures.cpp new file mode 100644 index 00000000..a6f3d19c --- /dev/null +++ b/src/protocols/PointerGestures.cpp @@ -0,0 +1,246 @@ +#include "PointerGestures.hpp" +#include "../Compositor.hpp" + +#define LOGM PROTO::pointerGestures->protoLog + +CPointerGestureSwipe::CPointerGestureSwipe(SP resource_) : resource(resource_) { + if (!resource->resource()) + return; + + resource->setOnDestroy([this](CZwpPointerGestureSwipeV1* p) { PROTO::pointerGestures->onGestureDestroy(this); }); + resource->setDestroy([this](CZwpPointerGestureSwipeV1* p) { PROTO::pointerGestures->onGestureDestroy(this); }); +} + +bool CPointerGestureSwipe::good() { + return resource->resource(); +} + +CPointerGestureHold::CPointerGestureHold(SP resource_) : resource(resource_) { + if (!resource->resource()) + return; + + resource->setOnDestroy([this](CZwpPointerGestureHoldV1* p) { PROTO::pointerGestures->onGestureDestroy(this); }); + resource->setDestroy([this](CZwpPointerGestureHoldV1* p) { PROTO::pointerGestures->onGestureDestroy(this); }); +} + +bool CPointerGestureHold::good() { + return resource->resource(); +} + +CPointerGesturePinch::CPointerGesturePinch(SP resource_) : resource(resource_) { + if (!resource->resource()) + return; + + resource->setOnDestroy([this](CZwpPointerGesturePinchV1* p) { PROTO::pointerGestures->onGestureDestroy(this); }); + resource->setDestroy([this](CZwpPointerGesturePinchV1* p) { PROTO::pointerGestures->onGestureDestroy(this); }); +} + +bool CPointerGesturePinch::good() { + return resource->resource(); +} + +CPointerGesturesProtocol::CPointerGesturesProtocol(const wl_interface* iface, const int& ver, const std::string& name) : IWaylandProtocol(iface, ver, name) { + ; +} + +void CPointerGesturesProtocol::bindManager(wl_client* client, void* data, uint32_t ver, uint32_t id) { + const auto RESOURCE = m_vManagers.emplace_back(std::make_unique(client, ver, id)).get(); + RESOURCE->setOnDestroy([this](CZwpPointerGesturesV1* p) { this->onManagerResourceDestroy(p->resource()); }); + RESOURCE->setRelease([this](CZwpPointerGesturesV1* pMgr) { this->onManagerResourceDestroy(pMgr->resource()); }); + + RESOURCE->setGetHoldGesture([this](CZwpPointerGesturesV1* pMgr, uint32_t id, wl_resource* pointer) { this->onGetHoldGesture(pMgr, id, pointer); }); + RESOURCE->setGetPinchGesture([this](CZwpPointerGesturesV1* pMgr, uint32_t id, wl_resource* pointer) { this->onGetPinchGesture(pMgr, id, pointer); }); + RESOURCE->setGetSwipeGesture([this](CZwpPointerGesturesV1* pMgr, uint32_t id, wl_resource* pointer) { this->onGetSwipeGesture(pMgr, id, pointer); }); +} + +void CPointerGesturesProtocol::onManagerResourceDestroy(wl_resource* res) { + std::erase_if(m_vManagers, [&](const auto& other) { return other->resource() == res; }); +} + +void CPointerGesturesProtocol::onGestureDestroy(CPointerGestureSwipe* gesture) { + std::erase_if(m_vSwipes, [&](const auto& other) { return other.get() == gesture; }); +} + +void CPointerGesturesProtocol::onGestureDestroy(CPointerGesturePinch* gesture) { + std::erase_if(m_vPinches, [&](const auto& other) { return other.get() == gesture; }); +} + +void CPointerGesturesProtocol::onGestureDestroy(CPointerGestureHold* gesture) { + std::erase_if(m_vHolds, [&](const auto& other) { return other.get() == gesture; }); +} + +void CPointerGesturesProtocol::onGetPinchGesture(CZwpPointerGesturesV1* pMgr, uint32_t id, wl_resource* pointer) { + const auto CLIENT = wl_resource_get_client(pMgr->resource()); + const auto RESOURCE = + m_vPinches.emplace_back(std::make_unique(std::make_shared(CLIENT, wl_resource_get_version(pMgr->resource()), id))).get(); + + if (!RESOURCE->good()) { + wl_resource_post_no_memory(pMgr->resource()); + LOGM(ERR, "Couldn't create gesture"); + return; + } +} + +void CPointerGesturesProtocol::onGetSwipeGesture(CZwpPointerGesturesV1* pMgr, uint32_t id, wl_resource* pointer) { + const auto CLIENT = wl_resource_get_client(pMgr->resource()); + const auto RESOURCE = + m_vSwipes.emplace_back(std::make_unique(std::make_shared(CLIENT, wl_resource_get_version(pMgr->resource()), id))).get(); + + if (!RESOURCE->good()) { + wl_resource_post_no_memory(pMgr->resource()); + LOGM(ERR, "Couldn't create gesture"); + return; + } +} + +void CPointerGesturesProtocol::onGetHoldGesture(CZwpPointerGesturesV1* pMgr, uint32_t id, wl_resource* pointer) { + const auto CLIENT = wl_resource_get_client(pMgr->resource()); + const auto RESOURCE = + m_vHolds.emplace_back(std::make_unique(std::make_shared(CLIENT, wl_resource_get_version(pMgr->resource()), id))).get(); + + if (!RESOURCE->good()) { + wl_resource_post_no_memory(pMgr->resource()); + LOGM(ERR, "Couldn't create gesture"); + return; + } +} + +void CPointerGesturesProtocol::swipeBegin(uint32_t timeMs, uint32_t fingers) { + if (!g_pCompositor->m_sSeat.seat->pointer_state.focused_client) + return; + + const auto FOCUSEDCLIENT = g_pCompositor->m_sSeat.seat->pointer_state.focused_client->client; + + const auto SERIAL = wlr_seat_client_next_serial(g_pCompositor->m_sSeat.seat->pointer_state.focused_client); + + for (auto& sw : m_vSwipes) { + const auto CLIENT = wl_resource_get_client(sw->resource->resource()); + + if (CLIENT != FOCUSEDCLIENT) + continue; + + sw->resource->sendBegin(SERIAL, timeMs, g_pCompositor->m_sSeat.seat->pointer_state.focused_surface->resource, fingers); + } +} + +void CPointerGesturesProtocol::swipeUpdate(uint32_t timeMs, const Vector2D& delta) { + if (!g_pCompositor->m_sSeat.seat->pointer_state.focused_client) + return; + + const auto FOCUSEDCLIENT = g_pCompositor->m_sSeat.seat->pointer_state.focused_client->client; + + for (auto& sw : m_vSwipes) { + const auto CLIENT = wl_resource_get_client(sw->resource->resource()); + + if (CLIENT != FOCUSEDCLIENT) + continue; + + sw->resource->sendUpdate(timeMs, wl_fixed_from_double(delta.x), wl_fixed_from_double(delta.y)); + } +} + +void CPointerGesturesProtocol::swipeEnd(uint32_t timeMs, bool cancelled) { + if (!g_pCompositor->m_sSeat.seat->pointer_state.focused_client) + return; + + const auto FOCUSEDCLIENT = g_pCompositor->m_sSeat.seat->pointer_state.focused_client->client; + + const auto SERIAL = wlr_seat_client_next_serial(g_pCompositor->m_sSeat.seat->pointer_state.focused_client); + + for (auto& sw : m_vSwipes) { + const auto CLIENT = wl_resource_get_client(sw->resource->resource()); + + if (CLIENT != FOCUSEDCLIENT) + continue; + + sw->resource->sendEnd(SERIAL, timeMs, cancelled); + } +} + +void CPointerGesturesProtocol::pinchBegin(uint32_t timeMs, uint32_t fingers) { + if (!g_pCompositor->m_sSeat.seat->pointer_state.focused_client) + return; + + const auto FOCUSEDCLIENT = g_pCompositor->m_sSeat.seat->pointer_state.focused_client->client; + + const auto SERIAL = wlr_seat_client_next_serial(g_pCompositor->m_sSeat.seat->pointer_state.focused_client); + + for (auto& sw : m_vPinches) { + const auto CLIENT = wl_resource_get_client(sw->resource->resource()); + + if (CLIENT != FOCUSEDCLIENT) + continue; + + sw->resource->sendBegin(SERIAL, timeMs, g_pCompositor->m_sSeat.seat->pointer_state.focused_surface->resource, fingers); + } +} + +void CPointerGesturesProtocol::pinchUpdate(uint32_t timeMs, const Vector2D& delta, double scale, double rotation) { + if (!g_pCompositor->m_sSeat.seat->pointer_state.focused_client) + return; + + const auto FOCUSEDCLIENT = g_pCompositor->m_sSeat.seat->pointer_state.focused_client->client; + + for (auto& sw : m_vPinches) { + const auto CLIENT = wl_resource_get_client(sw->resource->resource()); + + if (CLIENT != FOCUSEDCLIENT) + continue; + + sw->resource->sendUpdate(timeMs, wl_fixed_from_double(delta.x), wl_fixed_from_double(delta.y), wl_fixed_from_double(scale), wl_fixed_from_double(rotation)); + } +} + +void CPointerGesturesProtocol::pinchEnd(uint32_t timeMs, bool cancelled) { + if (!g_pCompositor->m_sSeat.seat->pointer_state.focused_client) + return; + + const auto FOCUSEDCLIENT = g_pCompositor->m_sSeat.seat->pointer_state.focused_client->client; + + const auto SERIAL = wlr_seat_client_next_serial(g_pCompositor->m_sSeat.seat->pointer_state.focused_client); + + for (auto& sw : m_vPinches) { + const auto CLIENT = wl_resource_get_client(sw->resource->resource()); + + if (CLIENT != FOCUSEDCLIENT) + continue; + + sw->resource->sendEnd(SERIAL, timeMs, cancelled); + } +} + +void CPointerGesturesProtocol::holdBegin(uint32_t timeMs, uint32_t fingers) { + if (!g_pCompositor->m_sSeat.seat->pointer_state.focused_client) + return; + + const auto FOCUSEDCLIENT = g_pCompositor->m_sSeat.seat->pointer_state.focused_client->client; + + const auto SERIAL = wlr_seat_client_next_serial(g_pCompositor->m_sSeat.seat->pointer_state.focused_client); + + for (auto& sw : m_vHolds) { + const auto CLIENT = wl_resource_get_client(sw->resource->resource()); + + if (CLIENT != FOCUSEDCLIENT) + continue; + + sw->resource->sendBegin(SERIAL, timeMs, g_pCompositor->m_sSeat.seat->pointer_state.focused_surface->resource, fingers); + } +} + +void CPointerGesturesProtocol::holdEnd(uint32_t timeMs, bool cancelled) { + if (!g_pCompositor->m_sSeat.seat->pointer_state.focused_client) + return; + + const auto FOCUSEDCLIENT = g_pCompositor->m_sSeat.seat->pointer_state.focused_client->client; + + const auto SERIAL = wlr_seat_client_next_serial(g_pCompositor->m_sSeat.seat->pointer_state.focused_client); + + for (auto& sw : m_vHolds) { + const auto CLIENT = wl_resource_get_client(sw->resource->resource()); + + if (CLIENT != FOCUSEDCLIENT) + continue; + + sw->resource->sendEnd(SERIAL, timeMs, cancelled); + } +} diff --git a/src/protocols/PointerGestures.hpp b/src/protocols/PointerGestures.hpp new file mode 100644 index 00000000..33c2bace --- /dev/null +++ b/src/protocols/PointerGestures.hpp @@ -0,0 +1,84 @@ +#pragma once + +#include +#include +#include "WaylandProtocol.hpp" +#include "pointer-gestures-unstable-v1.hpp" +#include "../helpers/Vector2D.hpp" + +class CPointerGestureSwipe { + public: + CPointerGestureSwipe(SP resource_); + + bool good(); + + private: + SP resource; + + friend class CPointerGesturesProtocol; +}; + +class CPointerGesturePinch { + public: + CPointerGesturePinch(SP resource_); + + bool good(); + + private: + SP resource; + + friend class CPointerGesturesProtocol; +}; + +class CPointerGestureHold { + public: + CPointerGestureHold(SP resource_); + + bool good(); + + private: + SP resource; + + friend class CPointerGesturesProtocol; +}; + +class CPointerGesturesProtocol : public IWaylandProtocol { + public: + CPointerGesturesProtocol(const wl_interface* iface, const int& ver, const std::string& name); + + virtual void bindManager(wl_client* client, void* data, uint32_t ver, uint32_t id); + + void swipeBegin(uint32_t timeMs, uint32_t fingers); + void swipeUpdate(uint32_t timeMs, const Vector2D& delta); + void swipeEnd(uint32_t timeMs, bool cancelled); + + void pinchBegin(uint32_t timeMs, uint32_t fingers); + void pinchUpdate(uint32_t timeMs, const Vector2D& delta, double scale, double rotation); + void pinchEnd(uint32_t timeMs, bool cancelled); + + void holdBegin(uint32_t timeMs, uint32_t fingers); + void holdEnd(uint32_t timeMs, bool cancelled); + + private: + void onManagerResourceDestroy(wl_resource* res); + void onGestureDestroy(CPointerGestureSwipe* gesture); + void onGestureDestroy(CPointerGesturePinch* gesture); + void onGestureDestroy(CPointerGestureHold* gesture); + void onGetPinchGesture(CZwpPointerGesturesV1* pMgr, uint32_t id, wl_resource* pointer); + void onGetSwipeGesture(CZwpPointerGesturesV1* pMgr, uint32_t id, wl_resource* pointer); + void onGetHoldGesture(CZwpPointerGesturesV1* pMgr, uint32_t id, wl_resource* pointer); + + // + std::vector> m_vManagers; + std::vector> m_vSwipes; + std::vector> m_vPinches; + std::vector> m_vHolds; + + friend class CPointerGestureHold; + friend class CPointerGesturePinch; + friend class CPointerGestureSwipe; +}; + +namespace PROTO { + inline UP pointerGestures; +}; \ No newline at end of file -- cgit v1.2.3