From 30e4b404f2a30230f57c946b655c2365b3bf8e3d Mon Sep 17 00:00:00 2001 From: Vaxry Date: Sun, 21 Apr 2024 01:47:38 +0100 Subject: cursor-shape: move to new impl --- src/protocols/CursorShape.cpp | 98 +++++++++++++++++++++++++++++++++++++++++++ src/protocols/CursorShape.hpp | 42 +++++++++++++++++++ 2 files changed, 140 insertions(+) create mode 100644 src/protocols/CursorShape.cpp create mode 100644 src/protocols/CursorShape.hpp (limited to 'src/protocols') diff --git a/src/protocols/CursorShape.cpp b/src/protocols/CursorShape.cpp new file mode 100644 index 00000000..da27dbda --- /dev/null +++ b/src/protocols/CursorShape.cpp @@ -0,0 +1,98 @@ +#include "CursorShape.hpp" + +// clang-format off +constexpr const char* SHAPE_NAMES[] = { + "invalid", + "default", + "context_menu", + "help", + "pointer", + "progress", + "wait", + "cell", + "crosshair", + "text", + "vertical_text", + "alias", + "copy", + "move", + "no_drop", + "not_allowed", + "grab", + "grabbing", + "e_resize", + "n_resize", + "ne_resize", + "nw_resize", + "s_resize", + "se_resize", + "sw_resize", + "w_resize", + "ew_resize", + "ns_resize", + "nesw_resize", + "nwse_resize", + "col_resize", + "row_resize", + "all_scroll", + "zoom_in", + "zoom_out", +}; +// clang-format on + +CCursorShapeProtocol::CCursorShapeProtocol(const wl_interface* iface, const int& ver, const std::string& name) : IWaylandProtocol(iface, ver, name) { + ; +} + +void CCursorShapeProtocol::onManagerResourceDestroy(wl_resource* res) { + std::erase_if(m_vManagers, [&](const auto& other) { return other->resource() == res; }); +} + +void CCursorShapeProtocol::onDeviceResourceDestroy(wl_resource* res) { + m_mDevices.erase(res); +} + +void CCursorShapeProtocol::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](CWpCursorShapeManagerV1* p) { this->onManagerResourceDestroy(p->resource()); }); + + RESOURCE->setDestroy([this](CWpCursorShapeManagerV1* pMgr) { this->onManagerResourceDestroy(pMgr->resource()); }); + RESOURCE->setGetPointer([this](CWpCursorShapeManagerV1* pMgr, uint32_t id, wl_resource* pointer) { this->onGetPointer(pMgr, id, pointer); }); + RESOURCE->setGetTabletToolV2([this](CWpCursorShapeManagerV1* pMgr, uint32_t id, wl_resource* tablet) { this->onGetTabletToolV2(pMgr, id, tablet); }); +} + +void CCursorShapeProtocol::onGetPointer(CWpCursorShapeManagerV1* pMgr, uint32_t id, wl_resource* pointer) { + createCursorShapeDevice(pMgr, id, pointer); +} + +void CCursorShapeProtocol::onGetTabletToolV2(CWpCursorShapeManagerV1* pMgr, uint32_t id, wl_resource* tablet) { + createCursorShapeDevice(pMgr, id, tablet); +} + +void CCursorShapeProtocol::createCursorShapeDevice(CWpCursorShapeManagerV1* pMgr, uint32_t id, wl_resource* resource) { + if (m_mDevices.contains(resource)) { + wl_resource_post_error(resource, 0, "Device already exists"); + return; + } + + const auto CLIENT = wl_resource_get_client(pMgr->resource()); + const auto RESOURCE = m_mDevices.emplace(resource, std::make_shared(CLIENT, wl_resource_get_version(pMgr->resource()), id)).first->second.get(); + RESOURCE->setOnDestroy([this](CWpCursorShapeDeviceV1* p) { this->onDeviceResourceDestroy(p->resource()); }); + + RESOURCE->setDestroy([this](CWpCursorShapeDeviceV1* p) { this->onDeviceResourceDestroy(p->resource()); }); + RESOURCE->setSetShape([this](CWpCursorShapeDeviceV1* p, uint32_t serial, wpCursorShapeDeviceV1Shape shape) { this->onSetShape(p, serial, shape); }); +} + +void CCursorShapeProtocol::onSetShape(CWpCursorShapeDeviceV1* pMgr, uint32_t serial, wpCursorShapeDeviceV1Shape shape) { + if ((uint32_t)shape == 0 || (uint32_t)shape > sizeof(SHAPE_NAMES)) { + wl_resource_post_error(pMgr->resource(), ERROR_INVALID_SHAPE, "The shape is invalid"); + return; + } + + SSetShapeEvent event; + event.pMgr = pMgr; + event.shape = shape; + event.shapeName = SHAPE_NAMES[shape]; + + events.setShape.emit(event); +} \ No newline at end of file diff --git a/src/protocols/CursorShape.hpp b/src/protocols/CursorShape.hpp new file mode 100644 index 00000000..f410bf5b --- /dev/null +++ b/src/protocols/CursorShape.hpp @@ -0,0 +1,42 @@ +#pragma once + +#include +#include +#include "WaylandProtocol.hpp" +#include "../helpers/signal/Signal.hpp" +#include "cursor-shape-v1.hpp" + +class CCursorShapeProtocol : public IWaylandProtocol { + public: + CCursorShapeProtocol(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 onManagerResourceDestroy(wl_resource* res); + void onDeviceResourceDestroy(wl_resource* res); + + void onGetPointer(CWpCursorShapeManagerV1* pMgr, uint32_t id, wl_resource* pointer); + void onGetTabletToolV2(CWpCursorShapeManagerV1* pMgr, uint32_t id, wl_resource* tablet); + + void onSetShape(CWpCursorShapeDeviceV1* pMgr, uint32_t serial, wpCursorShapeDeviceV1Shape shape); + + struct SSetShapeEvent { + CWpCursorShapeDeviceV1* pMgr = nullptr; + wpCursorShapeDeviceV1Shape shape; + std::string shapeName; + }; + + struct { + CSignal setShape; + } events; + + private: + void createCursorShapeDevice(CWpCursorShapeManagerV1* pMgr, uint32_t id, wl_resource* resource); + + std::unordered_map> m_mDevices; + std::vector> m_vManagers; +}; + +namespace PROTO { + inline UP cursorShape; +}; \ No newline at end of file -- cgit v1.2.3