Hyprland/src/managers/input/InputMethodRelay.cpp

178 lines
4.8 KiB
C++
Raw Normal View History

2022-08-05 13:03:37 +02:00
#include "InputMethodRelay.hpp"
#include "InputManager.hpp"
2022-08-05 16:21:08 +02:00
#include "../../Compositor.hpp"
2024-04-25 23:27:25 +01:00
#include "../../protocols/TextInputV3.hpp"
#include "../../protocols/TextInputV1.hpp"
2024-05-01 16:41:17 +01:00
#include "../../protocols/InputMethodV2.hpp"
#include "../../protocols/core/Compositor.hpp"
#include "../../managers/HookSystemManager.hpp"
2022-08-05 13:03:37 +02:00
CInputMethodRelay::CInputMethodRelay() {
static auto P =
g_pHookSystem->hookDynamic("keyboardFocus", [&](void* self, SCallbackInfo& info, std::any param) { onKeyboardFocus(std::any_cast<SP<CWLSurfaceResource>>(param)); });
2024-04-25 23:27:25 +01:00
listeners.newTIV3 = PROTO::textInputV3->events.newTextInput.registerListener([this](std::any ti) { onNewTextInput(std::any_cast<WP<CTextInputV3>>(ti)); });
listeners.newTIV1 = PROTO::textInputV1->events.newTextInput.registerListener([this](std::any ti) { onNewTextInput(std::any_cast<WP<CTextInputV1>>(ti)); });
2024-05-01 16:41:17 +01:00
listeners.newIME = PROTO::ime->events.newIME.registerListener([this](std::any ime) { onNewIME(std::any_cast<SP<CInputMethodV2>>(ime)); });
}
2022-08-05 13:03:37 +02:00
2024-05-01 16:41:17 +01:00
void CInputMethodRelay::onNewIME(SP<CInputMethodV2> pIME) {
if (!m_pIME.expired()) {
2022-08-05 13:03:37 +02:00
Debug::log(ERR, "Cannot register 2 IMEs at once!");
2024-05-01 16:41:17 +01:00
pIME->unavailable();
2022-08-05 13:03:37 +02:00
return;
}
2024-05-01 16:41:17 +01:00
m_pIME = pIME;
2022-08-05 13:03:37 +02:00
2024-05-01 16:41:17 +01:00
listeners.commitIME = pIME->events.onCommit.registerListener([this](std::any d) {
const auto PTI = getFocusedTextInput();
2022-08-05 13:03:37 +02:00
2024-05-01 16:41:17 +01:00
if (!PTI) {
Debug::log(LOG, "No focused TextInput on IME Commit");
return;
}
2022-08-05 13:03:37 +02:00
2024-05-01 16:41:17 +01:00
PTI->updateIMEState(m_pIME.lock());
});
2022-08-05 13:03:37 +02:00
2024-05-01 16:41:17 +01:00
listeners.destroyIME = pIME->events.destroy.registerListener([this](std::any d) {
const auto PTI = getFocusedTextInput();
2022-08-05 13:03:37 +02:00
2024-05-01 16:41:17 +01:00
Debug::log(LOG, "IME Destroy");
2022-08-05 16:21:08 +02:00
2024-05-01 16:41:17 +01:00
if (PTI)
PTI->leave();
2022-08-05 16:21:08 +02:00
2024-05-01 16:41:17 +01:00
m_pIME.reset();
});
2022-08-05 16:21:08 +02:00
2024-05-01 16:41:17 +01:00
listeners.newPopup = pIME->events.newPopup.registerListener([this](std::any d) {
m_vIMEPopups.emplace_back(makeUnique<CInputPopup>(std::any_cast<SP<CInputMethodPopupV2>>(d)));
2022-08-05 16:21:08 +02:00
2024-05-01 16:41:17 +01:00
Debug::log(LOG, "New input popup");
});
2022-08-05 17:07:01 +02:00
if (!g_pCompositor->m_pLastFocus)
return;
for (auto const& ti : m_vTextInputs) {
if (ti->client() != g_pCompositor->m_pLastFocus->client())
continue;
if (ti->isV3())
ti->enter(g_pCompositor->m_pLastFocus.lock());
else
ti->onEnabled(g_pCompositor->m_pLastFocus.lock());
}
}
2024-03-24 16:08:25 +00:00
void CInputMethodRelay::removePopup(CInputPopup* pPopup) {
std::erase_if(m_vIMEPopups, [pPopup](const auto& other) { return other.get() == pPopup; });
2022-08-05 17:07:01 +02:00
}
CTextInput* CInputMethodRelay::getFocusedTextInput() {
if (!g_pCompositor->m_pLastFocus)
return nullptr;
for (auto const& ti : m_vTextInputs) {
if (ti->focusedSurface() == g_pCompositor->m_pLastFocus)
return ti.get();
}
2022-08-05 13:03:37 +02:00
return nullptr;
}
void CInputMethodRelay::onNewTextInput(WP<CTextInputV3> tiv3) {
m_vTextInputs.emplace_back(makeUnique<CTextInput>(tiv3));
2022-08-05 13:03:37 +02:00
}
void CInputMethodRelay::onNewTextInput(WP<CTextInputV1> pTIV1) {
m_vTextInputs.emplace_back(makeUnique<CTextInput>(pTIV1));
2022-08-05 13:03:37 +02:00
}
void CInputMethodRelay::removeTextInput(CTextInput* pInput) {
2024-03-24 16:08:25 +00:00
std::erase_if(m_vTextInputs, [pInput](const auto& other) { return other.get() == pInput; });
}
void CInputMethodRelay::updateAllPopups() {
for (auto const& p : m_vIMEPopups) {
2024-03-24 16:08:25 +00:00
p->onCommit();
}
2022-08-05 13:03:37 +02:00
}
void CInputMethodRelay::activateIME(CTextInput* pInput, bool shouldCommit) {
2024-05-01 16:41:17 +01:00
if (m_pIME.expired())
return;
m_pIME->activate();
if (shouldCommit)
commitIMEState(pInput);
}
void CInputMethodRelay::deactivateIME(CTextInput* pInput, bool shouldCommit) {
2024-05-01 16:41:17 +01:00
if (m_pIME.expired())
return;
m_pIME->deactivate();
if (shouldCommit)
commitIMEState(pInput);
}
void CInputMethodRelay::commitIMEState(CTextInput* pInput) {
2024-05-01 16:41:17 +01:00
if (m_pIME.expired())
2022-08-05 17:19:49 +02:00
return;
2024-05-01 16:41:17 +01:00
pInput->commitStateToIME(m_pIME.lock());
2022-08-05 13:03:37 +02:00
}
void CInputMethodRelay::onKeyboardFocus(SP<CWLSurfaceResource> pSurface) {
2024-05-01 16:41:17 +01:00
if (m_pIME.expired())
2022-08-05 13:03:37 +02:00
return;
if (pSurface == m_pLastKbFocus)
return;
2022-08-05 13:03:37 +02:00
m_pLastKbFocus = pSurface;
for (auto const& ti : m_vTextInputs) {
if (!ti->focusedSurface())
continue;
2022-08-05 13:03:37 +02:00
ti->leave();
}
2022-08-05 13:19:16 +02:00
if (!pSurface)
return;
for (auto const& ti : m_vTextInputs) {
if (!ti->isV3())
continue;
if (ti->client() != pSurface->client())
continue;
2022-08-05 13:19:16 +02:00
ti->enter(pSurface);
2022-08-05 13:19:16 +02:00
}
2022-09-25 20:07:48 +02:00
}
2024-03-24 16:08:25 +00:00
CInputPopup* CInputMethodRelay::popupFromCoords(const Vector2D& point) {
for (auto const& p : m_vIMEPopups) {
2024-03-24 16:08:25 +00:00
if (p->isVecInPopup(point))
return p.get();
}
return nullptr;
}
CInputPopup* CInputMethodRelay::popupFromSurface(const SP<CWLSurfaceResource> surface) {
for (auto const& p : m_vIMEPopups) {
if (p->getSurface() == surface)
return p.get();
}
return nullptr;
}