Hyprland/src/managers/input/InputMethodRelay.cpp

195 lines
5.4 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"
2022-08-05 13:03:37 +02:00
CInputMethodRelay::CInputMethodRelay() {
g_pHookSystem->hookDynamic("keyboardFocus", [&](void* self, SCallbackInfo& info, std::any param) { onKeyboardFocus(std::any_cast<wlr_surface*>(param)); });
}
2022-08-05 13:03:37 +02:00
void CInputMethodRelay::onNewIME(wlr_input_method_v2* pIME) {
if (m_pWLRIME) {
Debug::log(ERR, "Cannot register 2 IMEs at once!");
wlr_input_method_v2_send_unavailable(pIME);
return;
}
m_pWLRIME = pIME;
hyprListener_IMECommit.initCallback(
&m_pWLRIME->events.commit,
[&](void* owner, void* data) {
const auto PTI = getFocusedTextInput();
const auto PIMR = (CInputMethodRelay*)owner;
2022-08-05 13:03:37 +02:00
if (!PTI) {
Debug::log(LOG, "No focused TextInput on IME Commit");
return;
}
2022-08-05 13:03:37 +02:00
PTI->updateIMEState(PIMR->m_pWLRIME);
},
this, "IMERelay");
2022-08-05 13:03:37 +02:00
hyprListener_IMEDestroy.initCallback(
&m_pWLRIME->events.destroy,
[&](void* owner, void* data) {
m_pWLRIME = nullptr;
2022-08-05 13:03:37 +02:00
hyprListener_IMEDestroy.removeCallback();
hyprListener_IMECommit.removeCallback();
hyprListener_IMEGrab.removeCallback();
hyprListener_IMENewPopup.removeCallback();
2022-08-05 16:21:08 +02:00
m_pKeyboardGrab.reset(nullptr);
2022-08-05 16:21:08 +02:00
const auto PTI = getFocusedTextInput();
2022-08-05 16:21:08 +02:00
Debug::log(LOG, "IME Destroy");
2022-08-05 16:21:08 +02:00
if (PTI)
PTI->enter(PTI->focusedSurface());
},
this, "IMERelay");
2022-08-05 16:21:08 +02:00
hyprListener_IMEGrab.initCallback(
&m_pWLRIME->events.grab_keyboard,
[&](void* owner, void* data) {
Debug::log(LOG, "IME TextInput Keyboard Grab new");
2022-08-05 16:21:08 +02:00
m_pKeyboardGrab.reset(nullptr);
2022-08-05 16:21:08 +02:00
m_pKeyboardGrab = std::make_unique<SIMEKbGrab>();
2022-08-05 16:21:08 +02:00
m_pKeyboardGrab->pKeyboard = wlr_seat_get_keyboard(g_pCompositor->m_sSeat.seat);
2022-08-05 16:21:08 +02:00
const auto PKBGRAB = (wlr_input_method_keyboard_grab_v2*)data;
2022-08-05 16:21:08 +02:00
m_pKeyboardGrab->pWlrKbGrab = PKBGRAB;
2022-08-05 16:21:08 +02:00
wlr_input_method_keyboard_grab_v2_set_keyboard(m_pKeyboardGrab->pWlrKbGrab, m_pKeyboardGrab->pKeyboard);
2022-08-05 16:21:08 +02:00
m_pKeyboardGrab->hyprListener_grabDestroy.initCallback(
&PKBGRAB->events.destroy,
[&](void* owner, void* data) {
m_pKeyboardGrab->hyprListener_grabDestroy.removeCallback();
2022-08-05 16:21:08 +02:00
Debug::log(LOG, "IME TextInput Keyboard Grab destroy");
2022-08-05 16:21:08 +02:00
m_pKeyboardGrab.reset(nullptr);
},
m_pKeyboardGrab.get(), "IME Keyboard Grab");
},
this, "IMERelay");
2022-08-05 17:07:01 +02:00
hyprListener_IMENewPopup.initCallback(
&m_pWLRIME->events.new_popup_surface,
[&](void* owner, void* data) {
2024-03-24 17:08:25 +01:00
m_vIMEPopups.emplace_back(std::make_unique<CInputPopup>((wlr_input_popup_surface_v2*)data));
2022-08-05 17:07:01 +02:00
Debug::log(LOG, "New input popup");
},
this, "IMERelay");
2022-08-05 17:07:01 +02:00
if (const auto PTI = getFocusedTextInput(); PTI)
PTI->enter(PTI->focusedSurface());
}
2024-03-24 17:08:25 +01:00
void CInputMethodRelay::setIMEPopupFocus(CInputPopup* pPopup, wlr_surface* pSurface) {
pPopup->onCommit();
2022-08-05 17:07:01 +02:00
}
2024-03-24 17:08:25 +01: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
}
2022-08-05 16:21:08 +02:00
SIMEKbGrab* CInputMethodRelay::getIMEKeyboardGrab(SKeyboard* pKeyboard) {
if (!m_pWLRIME)
return nullptr;
if (!m_pKeyboardGrab.get())
return nullptr;
const auto VIRTKB = wlr_input_device_get_virtual_keyboard(pKeyboard->keyboard);
if (VIRTKB && (wl_resource_get_client(VIRTKB->resource) == wl_resource_get_client(m_pKeyboardGrab->pWlrKbGrab->resource)))
return nullptr;
return m_pKeyboardGrab.get();
}
CTextInput* CInputMethodRelay::getFocusedTextInput() {
if (!g_pCompositor->m_pLastFocus)
return nullptr;
for (auto& 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(wlr_text_input_v3* pInput) {
m_vTextInputs.emplace_back(std::make_unique<CTextInput>(pInput));
2022-08-05 13:03:37 +02:00
}
void CInputMethodRelay::onNewTextInput(STextInputV1* pTIV1) {
m_vTextInputs.emplace_back(std::make_unique<CTextInput>(pTIV1));
2022-08-05 13:03:37 +02:00
}
void CInputMethodRelay::removeTextInput(CTextInput* pInput) {
2024-03-24 17:08:25 +01:00
std::erase_if(m_vTextInputs, [pInput](const auto& other) { return other.get() == pInput; });
}
void CInputMethodRelay::updateAllPopups() {
for (auto& p : m_vIMEPopups) {
p->onCommit();
}
2022-08-05 13:03:37 +02:00
}
void CInputMethodRelay::commitIMEState(CTextInput* pInput) {
2022-08-05 17:19:49 +02:00
if (!m_pWLRIME)
return;
pInput->commitStateToIME(m_pWLRIME);
2022-08-05 13:03:37 +02:00
}
void CInputMethodRelay::onKeyboardFocus(wlr_surface* pSurface) {
if (!m_pWLRIME)
return;
if (pSurface == m_pLastKbFocus)
return;
2022-08-05 13:03:37 +02:00
m_pLastKbFocus = pSurface;
for (auto& 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
for (auto& ti : m_vTextInputs) {
if (!ti->isV3())
continue;
if (ti->client() != wl_resource_get_client(pSurface->resource))
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 17:08:25 +01:00
CInputPopup* CInputMethodRelay::popupFromCoords(const Vector2D& point) {
for (auto& p : m_vIMEPopups) {
if (p->isVecInPopup(point))
return p.get();
}
return nullptr;
}