2024-04-26 00:27:25 +02:00
|
|
|
#include "TextInputV3.hpp"
|
|
|
|
#include <algorithm>
|
2024-06-08 10:07:59 +02:00
|
|
|
#include "core/Compositor.hpp"
|
2024-04-26 00:27:25 +02:00
|
|
|
|
|
|
|
void CTextInputV3::SState::reset() {
|
|
|
|
cause = ZWP_TEXT_INPUT_V3_CHANGE_CAUSE_INPUT_METHOD;
|
|
|
|
surrounding.updated = false;
|
|
|
|
contentType.updated = false;
|
|
|
|
box.updated = 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) {
|
2024-05-09 15:27:48 +02:00
|
|
|
bool wasEnabled = current.enabled;
|
|
|
|
|
2024-04-26 00:27:25 +02:00
|
|
|
current = pending;
|
|
|
|
serial++;
|
2024-05-09 15:27:48 +02:00
|
|
|
|
|
|
|
if (wasEnabled && !current.enabled) {
|
|
|
|
current.reset();
|
|
|
|
events.disable.emit();
|
|
|
|
} else if (!wasEnabled && current.enabled)
|
|
|
|
events.enable.emit();
|
|
|
|
else
|
|
|
|
events.onCommit.emit();
|
2024-04-26 00:27:25 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
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};
|
|
|
|
});
|
|
|
|
|
2024-05-09 15:27:48 +02:00
|
|
|
resource->setEnable([this](CZwpTextInputV3* r) { pending.enabled = true; });
|
2024-04-26 00:27:25 +02:00
|
|
|
|
|
|
|
resource->setDisable([this](CZwpTextInputV3* r) {
|
|
|
|
pending.enabled = false;
|
|
|
|
pending.reset();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
CTextInputV3::~CTextInputV3() {
|
|
|
|
events.destroy.emit();
|
|
|
|
}
|
|
|
|
|
2024-06-08 10:07:59 +02:00
|
|
|
void CTextInputV3::enter(SP<CWLSurfaceResource> surf) {
|
|
|
|
resource->sendEnter(surf->getResource()->resource());
|
2024-04-26 00:27:25 +02:00
|
|
|
}
|
|
|
|
|
2024-06-08 10:07:59 +02:00
|
|
|
void CTextInputV3::leave(SP<CWLSurfaceResource> surf) {
|
|
|
|
resource->sendLeave(surf->getResource()->resource());
|
2024-04-26 00:27:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2024-05-01 20:40:35 +02:00
|
|
|
const auto CLIENT = pMgr->client();
|
2024-05-05 18:16:00 +02:00
|
|
|
const auto RESOURCE = m_vTextInputs.emplace_back(makeShared<CTextInputV3>(makeShared<CZwpTextInputV3>(CLIENT, pMgr->version(), id)));
|
2024-04-26 00:27:25 +02:00
|
|
|
|
|
|
|
if (!RESOURCE->good()) {
|
2024-05-01 20:40:35 +02:00
|
|
|
pMgr->noMemory();
|
2024-04-26 00:27:25 +02:00
|
|
|
m_vTextInputs.pop_back();
|
|
|
|
LOGM(ERR, "Failed to create a tiv3 resource");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-05-05 18:16:00 +02:00
|
|
|
events.newTextInput.emit(WP<CTextInputV3>(RESOURCE));
|
2024-04-26 00:27:25 +02:00
|
|
|
}
|