IME/TI: Fixes and refactoring

Fixes #5189
This commit is contained in:
Vaxry 2024-03-22 23:08:52 +00:00
parent 8c88689faf
commit 9f2ed02f35
8 changed files with 403 additions and 421 deletions

View file

@ -244,39 +244,3 @@ void SKeyboard::updateXKBTranslationState(xkb_keymap* const keymap) {
xkb_keymap_unref(NEWKEYMAP);
xkb_context_unref(PCONTEXT);
}
void STextInput::setFocusedSurface(wlr_surface* pSurface) {
focusedSurface = pSurface;
hyprListener_surfaceUnmapped.removeCallback();
hyprListener_surfaceDestroyed.removeCallback();
if (!pSurface)
return;
hyprListener_surfaceUnmapped.initCallback(
&pSurface->events.unmap,
[this](void* owner, void* data) {
if (!focusedSurface)
return;
focusedSurface = nullptr;
hyprListener_surfaceUnmapped.removeCallback();
hyprListener_surfaceDestroyed.removeCallback();
g_pInputManager->m_sIMERelay.removeSurfaceToPTI(this);
},
this, "STextInput");
hyprListener_surfaceDestroyed.initCallback(
&pSurface->events.destroy,
[this](void* owner, void* data) {
if (!focusedSurface)
return;
focusedSurface = nullptr;
hyprListener_surfaceUnmapped.removeCallback();
hyprListener_surfaceDestroyed.removeCallback();
g_pInputManager->m_sIMERelay.removeSurfaceToPTI(this);
},
this, "STextInput");
}

View file

@ -288,23 +288,6 @@ struct SSwipeGesture {
CMonitor* pMonitor = nullptr;
};
struct STextInputV1;
struct STextInput {
wlr_text_input_v3* pWlrInput = nullptr;
STextInputV1* pV1Input = nullptr;
wlr_surface* focusedSurface = nullptr;
void setFocusedSurface(wlr_surface* pSurface);
DYNLISTENER(textInputEnable);
DYNLISTENER(textInputDisable);
DYNLISTENER(textInputCommit);
DYNLISTENER(textInputDestroy);
DYNLISTENER(surfaceUnmapped);
DYNLISTENER(surfaceDestroyed);
};
struct SIMEKbGrab {
wlr_input_method_keyboard_grab_v2* pWlrKbGrab = nullptr;
@ -319,7 +302,7 @@ struct SIMEPopup {
int x, y;
int realX, realY;
bool visible;
Vector2D lastSize;
CBox lastBox;
DYNLISTENER(mapPopup);
DYNLISTENER(unmapPopup);

View file

@ -28,46 +28,7 @@ void CInputMethodRelay::onNewIME(wlr_input_method_v2* pIME) {
return;
}
if (PTI->pWlrInput) {
if (PIMR->m_pWLRIME->current.preedit.text) {
wlr_text_input_v3_send_preedit_string(PTI->pWlrInput, PIMR->m_pWLRIME->current.preedit.text, PIMR->m_pWLRIME->current.preedit.cursor_begin,
PIMR->m_pWLRIME->current.preedit.cursor_end);
}
if (PIMR->m_pWLRIME->current.commit_text) {
wlr_text_input_v3_send_commit_string(PTI->pWlrInput, PIMR->m_pWLRIME->current.commit_text);
}
if (PIMR->m_pWLRIME->current.delete_.before_length || PIMR->m_pWLRIME->current.delete_.after_length) {
wlr_text_input_v3_send_delete_surrounding_text(PTI->pWlrInput, PIMR->m_pWLRIME->current.delete_.before_length, PIMR->m_pWLRIME->current.delete_.after_length);
}
wlr_text_input_v3_send_done(PTI->pWlrInput);
} else {
if (PIMR->m_pWLRIME->current.preedit.text) {
zwp_text_input_v1_send_preedit_cursor(PTI->pV1Input->resourceImpl, PIMR->m_pWLRIME->current.preedit.cursor_begin);
zwp_text_input_v1_send_preedit_styling(PTI->pV1Input->resourceImpl, 0, std::string(PIMR->m_pWLRIME->current.preedit.text).length(),
ZWP_TEXT_INPUT_V1_PREEDIT_STYLE_HIGHLIGHT);
zwp_text_input_v1_send_preedit_string(PTI->pV1Input->resourceImpl, PTI->pV1Input->serial, PIMR->m_pWLRIME->current.preedit.text, "");
} else {
zwp_text_input_v1_send_preedit_cursor(PTI->pV1Input->resourceImpl, PIMR->m_pWLRIME->current.preedit.cursor_begin);
zwp_text_input_v1_send_preedit_styling(PTI->pV1Input->resourceImpl, 0, 0, ZWP_TEXT_INPUT_V1_PREEDIT_STYLE_HIGHLIGHT);
zwp_text_input_v1_send_preedit_string(PTI->pV1Input->resourceImpl, PTI->pV1Input->serial, "", "");
}
if (PIMR->m_pWLRIME->current.commit_text) {
zwp_text_input_v1_send_commit_string(PTI->pV1Input->resourceImpl, PTI->pV1Input->serial, PIMR->m_pWLRIME->current.commit_text);
}
if (PIMR->m_pWLRIME->current.delete_.before_length || PIMR->m_pWLRIME->current.delete_.after_length) {
zwp_text_input_v1_send_delete_surrounding_text(PTI->pV1Input->resourceImpl,
std::string(PIMR->m_pWLRIME->current.preedit.text).length() - PIMR->m_pWLRIME->current.delete_.before_length,
PIMR->m_pWLRIME->current.delete_.after_length + PIMR->m_pWLRIME->current.delete_.before_length);
if (PIMR->m_pWLRIME->current.preedit.text)
zwp_text_input_v1_send_commit_string(PTI->pV1Input->resourceImpl, PTI->pV1Input->serial, PIMR->m_pWLRIME->current.preedit.text);
}
}
PTI->updateIMEState(PIMR->m_pWLRIME);
},
this, "IMERelay");
@ -88,7 +49,7 @@ void CInputMethodRelay::onNewIME(wlr_input_method_v2* pIME) {
Debug::log(LOG, "IME Destroy");
if (PTI)
onTextInputEnter(PTI->focusedSurface);
PTI->enter(PTI->focusedSurface());
},
this, "IMERelay");
@ -139,11 +100,7 @@ void CInputMethodRelay::onNewIME(wlr_input_method_v2* pIME) {
this, "IMERelay");
if (const auto PTI = getFocusedTextInput(); PTI)
onTextInputEnter(PTI->focusedSurface);
}
wlr_surface* CInputMethodRelay::focusedSurface(STextInput* pTI) {
return pTI->pWlrInput ? pTI->pWlrInput->focused_surface : pTI->focusedSurface;
PTI->enter(PTI->focusedSurface());
}
void CInputMethodRelay::updateInputPopup(SIMEPopup* pPopup) {
@ -151,63 +108,46 @@ void CInputMethodRelay::updateInputPopup(SIMEPopup* pPopup) {
return;
// damage last known pos & size
g_pHyprRenderer->damageBox(pPopup->realX, pPopup->realY, pPopup->lastSize.x, pPopup->lastSize.y);
g_pHyprRenderer->damageBox(&pPopup->lastBox);
const auto PFOCUSEDTI = getFocusedTextInput();
if (!PFOCUSEDTI || !focusedSurface(PFOCUSEDTI))
if (!PFOCUSEDTI || !PFOCUSEDTI->focusedSurface())
return;
bool cursorRect = PFOCUSEDTI->pWlrInput ? PFOCUSEDTI->pWlrInput->current.features & WLR_TEXT_INPUT_V3_FEATURE_CURSOR_RECTANGLE : true;
const auto PFOCUSEDSURFACE = focusedSurface(PFOCUSEDTI);
CBox cursorBox = PFOCUSEDTI->pWlrInput ? PFOCUSEDTI->pWlrInput->current.cursor_rectangle : PFOCUSEDTI->pV1Input->cursorRectangle;
CMonitor* pMonitor = nullptr;
bool cursorRect = PFOCUSEDTI->hasCursorRectangle();
const auto PFOCUSEDSURFACE = PFOCUSEDTI->focusedSurface();
CBox cursorBox = PFOCUSEDTI->cursorBox();
Vector2D parentPos;
Vector2D parentSize;
CBox parentBox;
if (wlr_layer_surface_v1_try_from_wlr_surface(PFOCUSEDSURFACE)) {
const auto PLS = g_pCompositor->getLayerSurfaceFromWlr(wlr_layer_surface_v1_try_from_wlr_surface(PFOCUSEDSURFACE));
const auto PSURFACE = CWLSurface::surfaceFromWlr(PFOCUSEDSURFACE);
if (PLS) {
parentPos = Vector2D(PLS->geometry.x, PLS->geometry.y) + g_pCompositor->getMonitorFromID(PLS->monitorID)->vecPosition;
parentSize = Vector2D(PLS->geometry.width, PLS->geometry.height);
pMonitor = g_pCompositor->getMonitorFromID(PLS->monitorID);
}
} else {
const auto PWINDOW = g_pCompositor->getWindowFromSurface(PFOCUSEDSURFACE);
if (!PSURFACE)
parentBox = {0, 0, 200, 200};
else
parentBox = PSURFACE->getSurfaceBoxGlobal().value_or(CBox{0, 0, 200, 200});
if (PWINDOW) {
parentPos = PWINDOW->m_vRealPosition.goal();
parentSize = PWINDOW->m_vRealSize.goal();
pMonitor = g_pCompositor->getMonitorFromID(PWINDOW->m_iMonitorID);
}
}
if (!cursorRect)
cursorBox = {0, 0, (int)parentBox.w, (int)parentBox.h};
if (!cursorRect) {
cursorBox = {0, 0, (int)parentSize.x, (int)parentSize.y};
}
CMonitor* pMonitor = g_pCompositor->getMonitorFromVector(cursorBox.middle());
if (!pMonitor)
return;
if (cursorBox.y + parentBox.y + pPopup->pSurface->surface->current.height + cursorBox.height > pMonitor->vecPosition.y + pMonitor->vecSize.y)
cursorBox.y -= pPopup->pSurface->surface->current.height + cursorBox.height;
CBox finalBox = cursorBox;
if (cursorBox.x + parentBox.x + pPopup->pSurface->surface->current.width > pMonitor->vecPosition.x + pMonitor->vecSize.x)
cursorBox.x -= (cursorBox.x + parentBox.x + pPopup->pSurface->surface->current.width) - (pMonitor->vecPosition.x + pMonitor->vecSize.x);
if (cursorBox.y + parentPos.y + pPopup->pSurface->surface->current.height + finalBox.height > pMonitor->vecPosition.y + pMonitor->vecSize.y)
finalBox.y -= pPopup->pSurface->surface->current.height + finalBox.height;
pPopup->x = cursorBox.x;
pPopup->y = cursorBox.y + cursorBox.height;
if (cursorBox.x + parentPos.x + pPopup->pSurface->surface->current.width > pMonitor->vecPosition.x + pMonitor->vecSize.x)
finalBox.x -= (cursorBox.x + parentPos.x + pPopup->pSurface->surface->current.width) - (pMonitor->vecPosition.x + pMonitor->vecSize.x);
pPopup->realX = cursorBox.x + parentBox.x;
pPopup->realY = cursorBox.y + parentBox.y + cursorBox.height;
pPopup->x = finalBox.x;
pPopup->y = finalBox.y + finalBox.height;
pPopup->lastBox = cursorBox;
pPopup->realX = finalBox.x + parentPos.x;
pPopup->realY = finalBox.y + parentPos.y + finalBox.height;
pPopup->lastSize = Vector2D(pPopup->pSurface->surface->current.width, pPopup->pSurface->surface->current.height);
wlr_input_popup_surface_v2_send_text_input_rectangle(pPopup->pSurface, finalBox.pWlr());
wlr_input_popup_surface_v2_send_text_input_rectangle(pPopup->pSurface, cursorBox.pWlr());
damagePopup(pPopup);
}
@ -223,7 +163,7 @@ void Events::listener_mapInputPopup(void* owner, void* data) {
g_pInputManager->m_sIMERelay.updateInputPopup(PPOPUP);
if (const auto PMONITOR = g_pCompositor->getMonitorFromVector(Vector2D(PPOPUP->realX, PPOPUP->realY) + PPOPUP->lastSize / 2.f); PMONITOR)
if (const auto PMONITOR = g_pCompositor->getMonitorFromVector(PPOPUP->lastBox.middle()); PMONITOR)
wlr_surface_send_enter(PPOPUP->pSurface->surface, PMONITOR->output);
}
@ -232,7 +172,7 @@ void Events::listener_unmapInputPopup(void* owner, void* data) {
Debug::log(LOG, "Unmapped an IME Popup");
g_pHyprRenderer->damageBox(PPOPUP->realX, PPOPUP->realY, PPOPUP->lastSize.x, PPOPUP->lastSize.y);
g_pHyprRenderer->damageBox(&PPOPUP->lastBox);
g_pInputManager->m_sIMERelay.updateInputPopup(PPOPUP);
}
@ -267,12 +207,12 @@ void CInputMethodRelay::damagePopup(SIMEPopup* pPopup) {
const auto PFOCUSEDTI = getFocusedTextInput();
if (!PFOCUSEDTI || !focusedSurface(PFOCUSEDTI))
if (!PFOCUSEDTI || !PFOCUSEDTI->focusedSurface())
return;
Vector2D parentPos;
const auto PFOCUSEDSURFACE = focusedSurface(PFOCUSEDTI);
const auto PFOCUSEDSURFACE = PFOCUSEDTI->focusedSurface();
if (wlr_layer_surface_v1_try_from_wlr_surface(PFOCUSEDSURFACE)) {
const auto PLS = g_pCompositor->getLayerSurfaceFromWlr(wlr_layer_surface_v1_try_from_wlr_surface(PFOCUSEDSURFACE));
@ -307,276 +247,60 @@ SIMEKbGrab* CInputMethodRelay::getIMEKeyboardGrab(SKeyboard* pKeyboard) {
return m_pKeyboardGrab.get();
}
STextInput* CInputMethodRelay::getFocusedTextInput() {
if (m_pFocusedSurface)
return getTextInput(m_pFocusedSurface);
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();
}
return nullptr;
}
void CInputMethodRelay::onNewTextInput(wlr_text_input_v3* pInput) {
createNewTextInput(pInput);
m_vTextInputs.emplace_back(std::make_unique<CTextInput>(pInput));
}
void CInputMethodRelay::createNewTextInput(wlr_text_input_v3* pInput, STextInputV1* pTIV1) {
if (pInput) {
if (!setTextInputVersion(wl_resource_get_client(pInput->resource), 3))
return;
} else if (!setTextInputVersion(pTIV1->client, 1))
return;
const auto PTEXTINPUT = &m_lTextInputs.emplace_back();
PTEXTINPUT->pWlrInput = pInput;
PTEXTINPUT->pV1Input = pTIV1;
if (pTIV1)
pTIV1->pTextInput = PTEXTINPUT;
PTEXTINPUT->hyprListener_textInputEnable.initCallback(
pInput ? &pInput->events.enable : &pTIV1->sEnable,
[&](void* owner, void* data) {
const auto PINPUT = (STextInput*)owner;
if (!g_pInputManager->m_sIMERelay.m_pWLRIME) {
// Debug::log(WARN, "Enabling TextInput on no IME!");
return;
}
// v1 only, map surface to PTI
if (PINPUT->pV1Input) {
wlr_surface* pSurface = wlr_surface_from_resource((wl_resource*)data);
PINPUT->setFocusedSurface(pSurface);
setSurfaceToPTI(pSurface, PINPUT);
if (m_pFocusedSurface == pSurface)
onTextInputEnter(pSurface);
}
Debug::log(LOG, "Enable TextInput");
wlr_input_method_v2_send_activate(g_pInputManager->m_sIMERelay.m_pWLRIME);
g_pInputManager->m_sIMERelay.commitIMEState(PINPUT);
},
PTEXTINPUT, "textInput");
PTEXTINPUT->hyprListener_textInputCommit.initCallback(
pInput ? &pInput->events.commit : &pTIV1->sCommit,
[](void* owner, void* data) {
const auto PINPUT = (STextInput*)owner;
if (!g_pInputManager->m_sIMERelay.m_pWLRIME) {
// Debug::log(WARN, "Committing TextInput on no IME!");
return;
}
if (!(PINPUT->pWlrInput ? PINPUT->pWlrInput->current_enabled : PINPUT->pV1Input->active)) {
Debug::log(WARN, "Disabled TextInput commit?");
return;
}
g_pInputManager->m_sIMERelay.commitIMEState(PINPUT);
},
PTEXTINPUT, "textInput");
PTEXTINPUT->hyprListener_textInputDisable.initCallback(
pInput ? &pInput->events.disable : &pTIV1->sDisable,
[](void* owner, void* data) {
const auto PINPUT = (STextInput*)owner;
if (!g_pInputManager->m_sIMERelay.m_pWLRIME) {
// Debug::log(WARN, "Disabling TextInput on no IME!");
return;
}
Debug::log(LOG, "Disable TextInput");
wlr_input_method_v2_send_deactivate(g_pInputManager->m_sIMERelay.m_pWLRIME);
g_pInputManager->m_sIMERelay.removeSurfaceToPTI(PINPUT);
g_pInputManager->m_sIMERelay.commitIMEState(PINPUT);
},
PTEXTINPUT, "textInput");
PTEXTINPUT->hyprListener_textInputDestroy.initCallback(
pInput ? &pInput->events.destroy : &pTIV1->sDestroy,
[](void* owner, void* data) {
const auto PINPUT = (STextInput*)owner;
if (!g_pInputManager->m_sIMERelay.m_pWLRIME) {
// Debug::log(WARN, "Disabling TextInput on no IME!");
return;
}
if (PINPUT->pWlrInput && PINPUT->pWlrInput->current_enabled) {
wlr_input_method_v2_send_deactivate(g_pInputManager->m_sIMERelay.m_pWLRIME);
g_pInputManager->m_sIMERelay.commitIMEState(PINPUT);
}
PINPUT->hyprListener_textInputCommit.removeCallback();
PINPUT->hyprListener_textInputDestroy.removeCallback();
PINPUT->hyprListener_textInputDisable.removeCallback();
PINPUT->hyprListener_textInputEnable.removeCallback();
g_pInputManager->m_sIMERelay.removeTextInputVersion(PINPUT->pWlrInput ? wl_resource_get_client(PINPUT->pWlrInput->resource) : PINPUT->pV1Input->client);
g_pInputManager->m_sIMERelay.removeSurfaceToPTI(PINPUT);
g_pInputManager->m_sIMERelay.removeTextInput(PINPUT);
},
PTEXTINPUT, "textInput");
void CInputMethodRelay::onNewTextInput(STextInputV1* pTIV1) {
m_vTextInputs.emplace_back(std::make_unique<CTextInput>(pTIV1));
}
void CInputMethodRelay::removeTextInput(STextInput* pInput) {
m_lTextInputs.remove_if([&](const auto& other) { return other.pWlrInput == pInput->pWlrInput && other.pV1Input == pInput->pV1Input; });
void CInputMethodRelay::removeTextInput(CTextInput* pInput) {
m_vTextInputs.remove_if([&](const auto& other) { return other.get() == pInput; });
}
void CInputMethodRelay::commitIMEState(STextInput* pInput) {
void CInputMethodRelay::commitIMEState(CTextInput* pInput) {
if (!m_pWLRIME)
return;
if (pInput->pWlrInput) {
// V3
if (pInput->pWlrInput->active_features & WLR_TEXT_INPUT_V3_FEATURE_SURROUNDING_TEXT)
wlr_input_method_v2_send_surrounding_text(m_pWLRIME, pInput->pWlrInput->current.surrounding.text, pInput->pWlrInput->current.surrounding.cursor,
pInput->pWlrInput->current.surrounding.anchor);
wlr_input_method_v2_send_text_change_cause(m_pWLRIME, pInput->pWlrInput->current.text_change_cause);
if (pInput->pWlrInput->active_features & WLR_TEXT_INPUT_V3_FEATURE_CONTENT_TYPE)
wlr_input_method_v2_send_content_type(m_pWLRIME, pInput->pWlrInput->current.content_type.hint, pInput->pWlrInput->current.content_type.purpose);
} else {
// V1
if (pInput->pV1Input->pendingSurrounding.isPending)
wlr_input_method_v2_send_surrounding_text(m_pWLRIME, pInput->pV1Input->pendingSurrounding.text.c_str(), pInput->pV1Input->pendingSurrounding.cursor,
pInput->pV1Input->pendingSurrounding.anchor);
wlr_input_method_v2_send_text_change_cause(m_pWLRIME, 0);
if (pInput->pV1Input->pendingContentType.isPending)
wlr_input_method_v2_send_content_type(m_pWLRIME, pInput->pV1Input->pendingContentType.hint, pInput->pV1Input->pendingContentType.purpose);
}
for (auto& p : m_lIMEPopups) {
updateInputPopup(&p);
}
wlr_input_method_v2_send_done(m_pWLRIME);
pInput->commitStateToIME(m_pWLRIME);
}
void CInputMethodRelay::onKeyboardFocus(wlr_surface* pSurface) {
if (!m_pWLRIME)
return;
if (pSurface == m_pFocusedSurface)
if (pSurface == m_pLastKbFocus)
return;
// say goodbye to the last focused surface
if (STextInput* lastTI = getTextInput(m_pFocusedSurface); lastTI) {
wlr_input_method_v2_send_deactivate(m_pWLRIME);
commitIMEState(lastTI);
onTextInputLeave(m_pFocusedSurface);
m_pLastKbFocus = pSurface;
for (auto& ti : m_vTextInputs) {
if (!ti->focusedSurface())
continue;
ti->leave();
}
// do some work for the new focused surface
m_pFocusedSurface = pSurface;
for (auto& ti : m_vTextInputs) {
if (!ti->isV3())
continue;
/*
* v3 only. v1 is handled by hyprListener_textInputEnable.
* POSSIBLE BUG here: if one client has multiple STextInput and multiple surfaces, for any pSurface we can only record the last found ti.
* since original code has the same problem, it may not be a big deal.
*/
if (getTextInputVersion(wl_resource_get_client(pSurface->resource)) == 3) {
if (!getTextInput(pSurface)) {
auto client = [](STextInput* pTI) -> wl_client* { return pTI->pWlrInput ? wl_resource_get_client(pTI->pWlrInput->resource) : pTI->pV1Input->client; };
for (auto& ti : m_lTextInputs) {
if (client(&ti) == wl_resource_get_client(pSurface->resource) && ti.pWlrInput)
setSurfaceToPTI(pSurface, &ti);
}
}
}
if (ti->client() != wl_resource_get_client(pSurface->resource))
continue;
onTextInputEnter(m_pFocusedSurface);
}
void CInputMethodRelay::onTextInputLeave(wlr_surface* pSurface) {
if (!pSurface)
return;
STextInput* ti = getTextInput(pSurface);
if (!ti)
return;
if (ti->pWlrInput && !ti->pWlrInput->focused_surface)
return;
if (ti->pWlrInput)
wlr_text_input_v3_send_leave(ti->pWlrInput);
else {
zwp_text_input_v1_send_leave(ti->pV1Input->resourceImpl);
ti->setFocusedSurface(nullptr);
ti->pV1Input->active = false;
ti->enter(pSurface);
}
}
void CInputMethodRelay::onTextInputEnter(wlr_surface* pSurface) {
if (!pSurface)
return;
STextInput* ti = getTextInput(pSurface);
if (!ti)
return;
if (ti->pWlrInput)
wlr_text_input_v3_send_enter(ti->pWlrInput, pSurface);
else {
zwp_text_input_v1_send_enter(ti->pV1Input->resourceImpl, pSurface->resource);
ti->setFocusedSurface(pSurface);
ti->pV1Input->active = true;
}
}
void CInputMethodRelay::setSurfaceToPTI(wlr_surface* pSurface, STextInput* pInput) {
if (pSurface) {
m_mSurfaceToTextInput[pSurface] = pInput;
pInput->setFocusedSurface(pSurface);
}
}
void CInputMethodRelay::removeSurfaceToPTI(STextInput* pInput) {
if (pInput->focusedSurface) {
m_mSurfaceToTextInput.erase(pInput->focusedSurface);
pInput->setFocusedSurface(nullptr);
return;
}
std::erase_if(m_mSurfaceToTextInput, [pInput](const auto& el) { return el.second == pInput; });
}
STextInput* CInputMethodRelay::getTextInput(wlr_surface* pSurface) {
auto result = m_mSurfaceToTextInput.find(pSurface);
if (result != m_mSurfaceToTextInput.end())
return result->second;
return nullptr;
}
int CInputMethodRelay::setTextInputVersion(wl_client* pClient, int version) {
if (int v = getTextInputVersion(pClient); v != 0 && v != version) {
Debug::log(WARN, "Client attempt to register text-input-v{}, but it has already registered text-input-v{}, ignored", version, v);
return 0;
}
m_mClientTextInputVersion.insert({pClient, version});
return 1;
}
int CInputMethodRelay::getTextInputVersion(wl_client* pClient) {
auto result = m_mClientTextInputVersion.find(pClient);
if (result != m_mClientTextInputVersion.end())
return result->second;
return 0;
}
void CInputMethodRelay::removeTextInputVersion(wl_client* pClient) {
m_mClientTextInputVersion.erase(pClient);
}

View file

@ -3,6 +3,7 @@
#include <list>
#include "../../defines.hpp"
#include "../../helpers/WLClasses.hpp"
#include "TextInput.hpp"
class CInputManager;
struct STextInputV1;
@ -13,15 +14,16 @@ class CInputMethodRelay {
void onNewIME(wlr_input_method_v2*);
void onNewTextInput(wlr_text_input_v3*);
void onNewTextInput(STextInputV1* pTIV1);
wlr_input_method_v2* m_pWLRIME = nullptr;
void commitIMEState(STextInput* pInput);
void removeTextInput(STextInput* pInput);
void commitIMEState(CTextInput* pInput);
void removeTextInput(CTextInput* pInput);
void onKeyboardFocus(wlr_surface*);
STextInput* getFocusedTextInput();
CTextInput* getFocusedTextInput();
SIMEKbGrab* getIMEKeyboardGrab(SKeyboard*);
@ -31,10 +33,12 @@ class CInputMethodRelay {
void removePopup(SIMEPopup*);
private:
std::unique_ptr<SIMEKbGrab> m_pKeyboardGrab;
std::unique_ptr<SIMEKbGrab> m_pKeyboardGrab;
std::list<STextInput> m_lTextInputs;
std::list<SIMEPopup> m_lIMEPopups;
std::list<std::unique_ptr<CTextInput>> m_vTextInputs;
std::list<SIMEPopup> m_lIMEPopups;
wlr_surface* m_pLastKbFocus = nullptr;
DYNLISTENER(textInputNew);
DYNLISTENER(IMECommit);
@ -42,25 +46,8 @@ class CInputMethodRelay {
DYNLISTENER(IMEGrab);
DYNLISTENER(IMENewPopup);
void createNewTextInput(wlr_text_input_v3*, STextInputV1* tiv1 = nullptr);
wlr_surface* focusedSurface(STextInput* pInput);
wlr_surface* m_pFocusedSurface;
void onTextInputLeave(wlr_surface* pSurface);
void onTextInputEnter(wlr_surface* pSurface);
std::unordered_map<wlr_surface*, STextInput*> m_mSurfaceToTextInput;
void setSurfaceToPTI(wlr_surface* pSurface, STextInput* pInput);
STextInput* getTextInput(wlr_surface* pSurface);
void removeSurfaceToPTI(STextInput* pInput);
std::unordered_map<wl_client*, int> m_mClientTextInputVersion;
int setTextInputVersion(wl_client* pClient, int version);
int getTextInputVersion(wl_client* pClient);
void removeTextInputVersion(wl_client* pClient);
friend class CHyprRenderer;
friend class CInputManager;
friend class CTextInputV1ProtocolManager;
friend struct STextInput;
friend struct CTextInput;
};

View file

@ -0,0 +1,273 @@
#include "TextInput.hpp"
#include "../../defines.hpp"
#include "InputManager.hpp"
#include "../../protocols/TextInputV1.hpp"
#include "../../Compositor.hpp"
CTextInput::CTextInput(STextInputV1* ti) : pV1Input(ti) {
ti->pTextInput = this;
initCallbacks();
}
CTextInput::CTextInput(wlr_text_input_v3* ti) : pWlrInput(ti) {
initCallbacks();
}
CTextInput::~CTextInput() {
if (pV1Input)
pV1Input->pTextInput = nullptr;
}
void CTextInput::tiV1Destroyed() {
pV1Input = nullptr;
g_pInputManager->m_sIMERelay.removeTextInput(this);
}
void CTextInput::initCallbacks() {
hyprListener_textInputEnable.initCallback(
isV3() ? &pWlrInput->events.enable : &pV1Input->sEnable, [this](void* owner, void* data) { onEnabled(); }, this, "textInput");
hyprListener_textInputCommit.initCallback(
isV3() ? &pWlrInput->events.commit : &pV1Input->sCommit, [this](void* owner, void* data) { onCommit(); }, this, "textInput");
hyprListener_textInputDisable.initCallback(
isV3() ? &pWlrInput->events.disable : &pV1Input->sDisable, [this](void* owner, void* data) { onDisabled(); }, this, "textInput");
hyprListener_textInputDestroy.initCallback(
isV3() ? &pWlrInput->events.destroy : &pV1Input->sDestroy,
[this](void* owner, void* data) {
if (!g_pInputManager->m_sIMERelay.m_pWLRIME) {
// Debug::log(WARN, "Disabling TextInput on no IME!");
return;
}
if (pWlrInput && pWlrInput->current_enabled) {
wlr_input_method_v2_send_deactivate(g_pInputManager->m_sIMERelay.m_pWLRIME);
g_pInputManager->m_sIMERelay.commitIMEState(this);
}
hyprListener_textInputCommit.removeCallback();
hyprListener_textInputDestroy.removeCallback();
hyprListener_textInputDisable.removeCallback();
hyprListener_textInputEnable.removeCallback();
hyprListener_surfaceDestroyed.removeCallback();
hyprListener_surfaceUnmapped.removeCallback();
g_pInputManager->m_sIMERelay.removeTextInput(this);
},
this, "textInput");
}
void CTextInput::onEnabled(wlr_surface* surfV1) {
Debug::log(LOG, "TI ENABLE");
if (!g_pInputManager->m_sIMERelay.m_pWLRIME) {
// Debug::log(WARN, "Enabling TextInput on no IME!");
return;
}
// v1 only, map surface to PTI
if (!isV3()) {
wlr_surface* pSurface = surfV1;
setFocusedSurface(pSurface);
if (g_pCompositor->m_pLastFocus == pSurface)
enter(pSurface);
}
wlr_input_method_v2_send_activate(g_pInputManager->m_sIMERelay.m_pWLRIME);
g_pInputManager->m_sIMERelay.commitIMEState(this);
}
void CTextInput::onDisabled() {
if (!g_pInputManager->m_sIMERelay.m_pWLRIME) {
// Debug::log(WARN, "Disabling TextInput on no IME!");
return;
}
leave();
hyprListener_surfaceDestroyed.removeCallback();
hyprListener_surfaceUnmapped.removeCallback();
wlr_input_method_v2_send_deactivate(g_pInputManager->m_sIMERelay.m_pWLRIME);
g_pInputManager->m_sIMERelay.commitIMEState(this);
}
void CTextInput::onCommit() {
if (!g_pInputManager->m_sIMERelay.m_pWLRIME) {
// Debug::log(WARN, "Committing TextInput on no IME!");
return;
}
if (!(pWlrInput ? pWlrInput->current_enabled : pV1Input->active)) {
Debug::log(WARN, "Disabled TextInput commit?");
return;
}
g_pInputManager->m_sIMERelay.commitIMEState(this);
}
void CTextInput::setFocusedSurface(wlr_surface* pSurface) {
if (pSurface == pFocusedSurface)
return;
pFocusedSurface = pSurface;
hyprListener_surfaceUnmapped.removeCallback();
hyprListener_surfaceDestroyed.removeCallback();
if (!pSurface)
return;
hyprListener_surfaceUnmapped.initCallback(
&pSurface->events.unmap,
[this](void* owner, void* data) {
Debug::log(LOG, "Unmap TI owner1");
pFocusedSurface = nullptr;
hyprListener_surfaceUnmapped.removeCallback();
hyprListener_surfaceDestroyed.removeCallback();
},
this, "CTextInput");
hyprListener_surfaceDestroyed.initCallback(
&pSurface->events.destroy,
[this](void* owner, void* data) {
Debug::log(LOG, "destroy TI owner1");
pFocusedSurface = nullptr;
hyprListener_surfaceUnmapped.removeCallback();
hyprListener_surfaceDestroyed.removeCallback();
},
this, "CTextInput");
}
bool CTextInput::isV3() {
return pWlrInput;
}
void CTextInput::enter(wlr_surface* pSurface) {
if (!pSurface || !pSurface->mapped)
return;
if (pSurface == focusedSurface())
return;
if (focusedSurface()) {
leave();
setFocusedSurface(nullptr);
}
enterLocks++;
RASSERT(enterLocks == 1, "TextInput had != 1 lock in enter");
if (pWlrInput)
wlr_text_input_v3_send_enter(pWlrInput, pSurface);
else {
zwp_text_input_v1_send_enter(pV1Input->resourceImpl, pSurface->resource);
pV1Input->active = true;
}
setFocusedSurface(pSurface);
}
void CTextInput::leave() {
if (!focusedSurface())
return;
enterLocks--;
RASSERT(enterLocks == 0, "TextInput had != 0 locks in leave");
if (pWlrInput && pWlrInput->focused_surface)
wlr_text_input_v3_send_leave(pWlrInput);
else if (focusedSurface() && pV1Input) {
zwp_text_input_v1_send_leave(pV1Input->resourceImpl);
pV1Input->active = false;
}
setFocusedSurface(nullptr);
}
wlr_surface* CTextInput::focusedSurface() {
return pWlrInput ? pWlrInput->focused_surface : pFocusedSurface;
}
wl_client* CTextInput::client() {
return pWlrInput ? wl_resource_get_client(pWlrInput->resource) : pV1Input->client;
}
void CTextInput::commitStateToIME(wlr_input_method_v2* ime) {
if (isV3()) {
if (pWlrInput->active_features & WLR_TEXT_INPUT_V3_FEATURE_SURROUNDING_TEXT)
wlr_input_method_v2_send_surrounding_text(ime, pWlrInput->current.surrounding.text, pWlrInput->current.surrounding.cursor, pWlrInput->current.surrounding.anchor);
wlr_input_method_v2_send_text_change_cause(ime, pWlrInput->current.text_change_cause);
if (pWlrInput->active_features & WLR_TEXT_INPUT_V3_FEATURE_CONTENT_TYPE)
wlr_input_method_v2_send_content_type(ime, pWlrInput->current.content_type.hint, pWlrInput->current.content_type.purpose);
} else {
if (pV1Input->pendingSurrounding.isPending)
wlr_input_method_v2_send_surrounding_text(ime, pV1Input->pendingSurrounding.text.c_str(), pV1Input->pendingSurrounding.cursor, pV1Input->pendingSurrounding.anchor);
wlr_input_method_v2_send_text_change_cause(ime, 0);
if (pV1Input->pendingContentType.isPending)
wlr_input_method_v2_send_content_type(ime, pV1Input->pendingContentType.hint, pV1Input->pendingContentType.purpose);
}
for (auto& p : g_pInputManager->m_sIMERelay.m_lIMEPopups) {
g_pInputManager->m_sIMERelay.updateInputPopup(&p);
}
wlr_input_method_v2_send_done(ime);
}
void CTextInput::updateIMEState(wlr_input_method_v2* ime) {
if (isV3()) {
if (ime->current.preedit.text) {
wlr_text_input_v3_send_preedit_string(pWlrInput, ime->current.preedit.text, ime->current.preedit.cursor_begin, ime->current.preedit.cursor_end);
}
if (ime->current.commit_text) {
wlr_text_input_v3_send_commit_string(pWlrInput, ime->current.commit_text);
}
if (ime->current.delete_.before_length || ime->current.delete_.after_length) {
wlr_text_input_v3_send_delete_surrounding_text(pWlrInput, ime->current.delete_.before_length, ime->current.delete_.after_length);
}
wlr_text_input_v3_send_done(pWlrInput);
} else {
if (ime->current.preedit.text) {
zwp_text_input_v1_send_preedit_cursor(pV1Input->resourceImpl, ime->current.preedit.cursor_begin);
zwp_text_input_v1_send_preedit_styling(pV1Input->resourceImpl, 0, std::string(ime->current.preedit.text).length(), ZWP_TEXT_INPUT_V1_PREEDIT_STYLE_HIGHLIGHT);
zwp_text_input_v1_send_preedit_string(pV1Input->resourceImpl, pV1Input->serial, ime->current.preedit.text, "");
} else {
zwp_text_input_v1_send_preedit_cursor(pV1Input->resourceImpl, ime->current.preedit.cursor_begin);
zwp_text_input_v1_send_preedit_styling(pV1Input->resourceImpl, 0, 0, ZWP_TEXT_INPUT_V1_PREEDIT_STYLE_HIGHLIGHT);
zwp_text_input_v1_send_preedit_string(pV1Input->resourceImpl, pV1Input->serial, "", "");
}
if (ime->current.commit_text) {
zwp_text_input_v1_send_commit_string(pV1Input->resourceImpl, pV1Input->serial, ime->current.commit_text);
}
if (ime->current.delete_.before_length || ime->current.delete_.after_length) {
zwp_text_input_v1_send_delete_surrounding_text(pV1Input->resourceImpl, std::string(ime->current.preedit.text).length() - ime->current.delete_.before_length,
ime->current.delete_.after_length + ime->current.delete_.before_length);
if (ime->current.preedit.text)
zwp_text_input_v1_send_commit_string(pV1Input->resourceImpl, pV1Input->serial, ime->current.preedit.text);
}
}
}
bool CTextInput::hasCursorRectangle() {
return !isV3() || pWlrInput->current.features & WLR_TEXT_INPUT_V3_FEATURE_CURSOR_RECTANGLE;
}
CBox CTextInput::cursorBox() {
return CBox{isV3() ? pWlrInput->current.cursor_rectangle : pV1Input->cursorRectangle};
}

View file

@ -0,0 +1,51 @@
#pragma once
#include "../../helpers/WLListener.hpp"
#include "../../macros.hpp"
#include "../../helpers/Box.hpp"
struct wlr_text_input_v3;
struct wlr_surface;
struct wl_client;
struct STextInputV1;
class CTextInput {
public:
CTextInput(STextInputV1* ti);
CTextInput(wlr_text_input_v3* ti);
~CTextInput();
bool isV3();
void enter(wlr_surface* pSurface);
void leave();
void tiV1Destroyed();
wl_client* client();
void commitStateToIME(wlr_input_method_v2* ime);
void updateIMEState(wlr_input_method_v2* ime);
void onEnabled(wlr_surface* surfV1 = nullptr);
void onDisabled();
void onCommit();
bool hasCursorRectangle();
CBox cursorBox();
wlr_surface* focusedSurface();
private:
void setFocusedSurface(wlr_surface* pSurface);
void initCallbacks();
wlr_surface* pFocusedSurface = nullptr;
int enterLocks = 0;
wlr_text_input_v3* pWlrInput = nullptr;
STextInputV1* pV1Input = nullptr;
DYNLISTENER(textInputEnable);
DYNLISTENER(textInputDisable);
DYNLISTENER(textInputCommit);
DYNLISTENER(textInputDestroy);
DYNLISTENER(surfaceUnmapped);
DYNLISTENER(surfaceDestroyed);
};

View file

@ -131,7 +131,7 @@ static void destroyTI(wl_resource* resource) {
wl_resource_set_user_data(resource, nullptr);
}
TI->pTextInput->hyprListener_textInputDestroy.emit(nullptr);
TI->pTextInput->tiV1Destroyed();
g_pProtocolManager->m_pTextInputV1ProtocolManager->removeTI(TI);
}
@ -158,7 +158,7 @@ void CTextInputV1ProtocolManager::createTI(wl_client* client, wl_resource* resou
wl_signal_init(&PTI->sDestroy);
wl_signal_init(&PTI->sCommit);
g_pInputManager->m_sIMERelay.createNewTextInput(nullptr, PTI);
g_pInputManager->m_sIMERelay.onNewTextInput(PTI);
}
void CTextInputV1ProtocolManager::handleActivate(wl_client* client, wl_resource* resource, wl_resource* seat, wl_resource* surface) {
@ -167,12 +167,12 @@ void CTextInputV1ProtocolManager::handleActivate(wl_client* client, wl_resource*
Debug::log(WARN, "Text-input-v1 PTI{:x}: No surface to activate text input on!", (uintptr_t)PTI);
return;
}
PTI->pTextInput->hyprListener_textInputEnable.emit(surface);
PTI->pTextInput->onEnabled(wlr_surface_from_resource(surface));
}
void CTextInputV1ProtocolManager::handleDeactivate(wl_client* client, wl_resource* resource, wl_resource* seat) {
const auto PTI = tiFromResource(resource);
PTI->pTextInput->hyprListener_textInputDisable.emit(nullptr);
PTI->pTextInput->onDisabled();
}
void CTextInputV1ProtocolManager::handleShowInputPanel(wl_client* client, wl_resource* resource) {
@ -212,7 +212,7 @@ void CTextInputV1ProtocolManager::handleSetPreferredLanguage(wl_client* client,
void CTextInputV1ProtocolManager::handleCommitState(wl_client* client, wl_resource* resource, uint32_t serial) {
const auto PTI = tiFromResource(resource);
PTI->serial = serial;
PTI->pTextInput->hyprListener_textInputCommit.emit(nullptr);
PTI->pTextInput->onCommit();
}
void CTextInputV1ProtocolManager::handleInvokeAction(wl_client* client, wl_resource* resource, uint32_t button, uint32_t index) {

View file

@ -5,7 +5,7 @@
#include <vector>
struct STextInput;
class CTextInput;
struct STextInputV1 {
wl_client* client = nullptr;
@ -13,7 +13,7 @@ struct STextInputV1 {
wl_resource* resourceImpl = nullptr;
STextInput* pTextInput = nullptr;
CTextInput* pTextInput = nullptr;
wl_signal sEnable;
wl_signal sDisable;