diff options
| author | Vaxry <vaxry@vaxry.net> | 2024-04-25 23:27:25 +0100 |
|---|---|---|
| committer | Vaxry <vaxry@vaxry.net> | 2024-04-25 23:27:44 +0100 |
| commit | 1d40af64d352bd5a87c7d53a52707fc5cfc6940e (patch) | |
| tree | 7ad6d1f0dbe3f4d972faf487ffa0cbc9420ee82d /src/protocols | |
| parent | e87227e00ae350adebafd2adde95a47e1f68cb3a (diff) | |
text-input-v3: move to new impl
Diffstat (limited to 'src/protocols')
| -rw-r--r-- | src/protocols/TextInputV3.cpp | 129 | ||||
| -rw-r--r-- | src/protocols/TextInputV3.hpp | 92 |
2 files changed, 221 insertions, 0 deletions
diff --git a/src/protocols/TextInputV3.cpp b/src/protocols/TextInputV3.cpp new file mode 100644 index 00000000..fbb5c3ae --- /dev/null +++ b/src/protocols/TextInputV3.cpp @@ -0,0 +1,129 @@ +#include "TextInputV3.hpp" +#include <algorithm> + +#define LOGM PROTO::textInputV3->protoLog + +void CTextInputV3::SState::reset() { + cause = ZWP_TEXT_INPUT_V3_CHANGE_CAUSE_INPUT_METHOD; + surrounding.updated = false; + contentType.updated = false; + box.updated = false; + enabled = false; +} + +CTextInputV3::CTextInputV3(SP<CZwpTextInputV3> resource_) : resource(resource_) { + if (!resource->resource()) + return; + + LOGM(LOG, "New tiv3 at {:016x}", (uintptr_t)this); + + resource->setDestroy([this](CZwpTextInputV3* r) { PROTO::textInputV3->destroyTextInput(this); }); + resource->setOnDestroy([this](CZwpTextInputV3* r) { PROTO::textInputV3->destroyTextInput(this); }); + + resource->setCommit([this](CZwpTextInputV3* r) { + current = pending; + events.onCommit.emit(); + serial++; + }); + + resource->setSetSurroundingText([this](CZwpTextInputV3* r, const char* text, int32_t cursor, int32_t anchor) { + pending.surrounding.updated = true; + pending.surrounding.anchor = anchor; + pending.surrounding.cursor = cursor; + pending.surrounding.text = text; + }); + + resource->setSetTextChangeCause([this](CZwpTextInputV3* r, zwpTextInputV3ChangeCause cause) { pending.cause = cause; }); + + resource->setSetContentType([this](CZwpTextInputV3* r, zwpTextInputV3ContentHint hint, zwpTextInputV3ContentPurpose purpose) { + pending.contentType.updated = true; + pending.contentType.hint = hint; + pending.contentType.purpose = purpose; + }); + + resource->setSetCursorRectangle([this](CZwpTextInputV3* r, int32_t x, int32_t y, int32_t w, int32_t h) { + pending.box.updated = true; + pending.box.cursorBox = {x, y, w, h}; + }); + + resource->setEnable([this](CZwpTextInputV3* r) { + events.enable.emit(); + pending.enabled = true; + }); + + resource->setDisable([this](CZwpTextInputV3* r) { + events.disable.emit(); + pending.enabled = false; + pending.reset(); + }); +} + +CTextInputV3::~CTextInputV3() { + events.destroy.emit(); +} + +void CTextInputV3::enter(wlr_surface* surf) { + resource->sendEnter(surf->resource); +} + +void CTextInputV3::leave(wlr_surface* surf) { + resource->sendLeave(surf->resource); +} + +void CTextInputV3::preeditString(const std::string& text, int32_t cursorBegin, int32_t cursorEnd) { + resource->sendPreeditString(text.c_str(), cursorBegin, cursorEnd); +} + +void CTextInputV3::commitString(const std::string& text) { + resource->sendCommitString(text.c_str()); +} + +void CTextInputV3::deleteSurroundingText(uint32_t beforeLength, uint32_t afterLength) { + resource->sendDeleteSurroundingText(beforeLength, afterLength); +} + +void CTextInputV3::sendDone() { + resource->sendDone(serial); +} + +bool CTextInputV3::good() { + return resource->resource(); +} + +wl_client* CTextInputV3::client() { + return wl_resource_get_client(resource->resource()); +} + +CTextInputV3Protocol::CTextInputV3Protocol(const wl_interface* iface, const int& ver, const std::string& name) : IWaylandProtocol(iface, ver, name) { + ; +} + +void CTextInputV3Protocol::bindManager(wl_client* client, void* data, uint32_t ver, uint32_t id) { + const auto RESOURCE = m_vManagers.emplace_back(std::make_unique<CZwpTextInputManagerV3>(client, ver, id)).get(); + RESOURCE->setOnDestroy([this](CZwpTextInputManagerV3* p) { this->onManagerResourceDestroy(p->resource()); }); + + RESOURCE->setDestroy([this](CZwpTextInputManagerV3* pMgr) { this->onManagerResourceDestroy(pMgr->resource()); }); + RESOURCE->setGetTextInput([this](CZwpTextInputManagerV3* pMgr, uint32_t id, wl_resource* seat) { this->onGetTextInput(pMgr, id, seat); }); +} + +void CTextInputV3Protocol::onManagerResourceDestroy(wl_resource* res) { + std::erase_if(m_vManagers, [&](const auto& other) { return other->resource() == res; }); +} + +void CTextInputV3Protocol::destroyTextInput(CTextInputV3* input) { + std::erase_if(m_vTextInputs, [&](const auto& other) { return other.get() == input; }); +} + +void CTextInputV3Protocol::onGetTextInput(CZwpTextInputManagerV3* pMgr, uint32_t id, wl_resource* seat) { + const auto CLIENT = wl_resource_get_client(pMgr->resource()); + const auto RESOURCE = m_vTextInputs.emplace_back(std::make_shared<CTextInputV3>(std::make_shared<CZwpTextInputV3>(CLIENT, wl_resource_get_version(pMgr->resource()), id))); + + if (!RESOURCE->good()) { + wl_resource_post_no_memory(pMgr->resource()); + m_vTextInputs.pop_back(); + LOGM(ERR, "Failed to create a tiv3 resource"); + return; + } + + events.newTextInput.emit(std::weak_ptr<CTextInputV3>(RESOURCE)); +}
\ No newline at end of file diff --git a/src/protocols/TextInputV3.hpp b/src/protocols/TextInputV3.hpp new file mode 100644 index 00000000..6d4f3d54 --- /dev/null +++ b/src/protocols/TextInputV3.hpp @@ -0,0 +1,92 @@ +#pragma once + +#include <memory> +#include <vector> +#include <cstdint> +#include <string> +#include "WaylandProtocol.hpp" +#include "text-input-unstable-v3.hpp" +#include "../helpers/signal/Signal.hpp" +#include "../helpers/Box.hpp" + +class CTextInputV3 { + public: + CTextInputV3(SP<CZwpTextInputV3> resource_); + ~CTextInputV3(); + + void enter(wlr_surface* surf); + void leave(wlr_surface* surf); + void preeditString(const std::string& text, int32_t cursorBegin, int32_t cursorEnd); + void commitString(const std::string& text); + void deleteSurroundingText(uint32_t beforeLength, uint32_t afterLength); + void sendDone(); + + bool good(); + + wl_client* client(); + + struct { + CSignal onCommit; + CSignal enable; + CSignal disable; + CSignal destroy; + } events; + + struct SState { + struct { + bool updated = false; + std::string text = ""; + uint32_t cursor = 0; + uint32_t anchor = 0; + } surrounding; + + struct { + bool updated = false; + zwpTextInputV3ContentHint hint = ZWP_TEXT_INPUT_V3_CONTENT_HINT_NONE; + zwpTextInputV3ContentPurpose purpose = ZWP_TEXT_INPUT_V3_CONTENT_PURPOSE_NORMAL; + } contentType; + + struct { + bool updated = false; + CBox cursorBox; + } box; + + bool enabled = false; + + zwpTextInputV3ChangeCause cause = ZWP_TEXT_INPUT_V3_CHANGE_CAUSE_INPUT_METHOD; + + void reset(); + }; + SState pending, current; + + private: + SP<CZwpTextInputV3> resource; + + int serial = 0; +}; + +class CTextInputV3Protocol : public IWaylandProtocol { + public: + CTextInputV3Protocol(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); + + struct { + CSignal newTextInput; // WP<CTextInputV3> + } events; + + private: + void onManagerResourceDestroy(wl_resource* res); + void destroyTextInput(CTextInputV3* input); + void onGetTextInput(CZwpTextInputManagerV3* pMgr, uint32_t id, wl_resource* seat); + + // + std::vector<UP<CZwpTextInputManagerV3>> m_vManagers; + std::vector<SP<CTextInputV3>> m_vTextInputs; + + friend class CTextInputV3; +}; + +namespace PROTO { + inline UP<CTextInputV3Protocol> textInputV3; +};
\ No newline at end of file |
