From cb5521ec6e83a062cc9830b7eaca80f6e0b4db7c Mon Sep 17 00:00:00 2001 From: vaxerski <43317083+vaxerski@users.noreply.github.com> Date: Fri, 5 Aug 2022 13:19:16 +0200 Subject: [PATCH] fix possible crashes with IME --- src/helpers/WLClasses.hpp | 2 ++ src/managers/input/InputMethodRelay.cpp | 26 +++++++++++++++++++++---- src/managers/input/InputMethodRelay.hpp | 2 ++ 3 files changed, 26 insertions(+), 4 deletions(-) diff --git a/src/helpers/WLClasses.hpp b/src/helpers/WLClasses.hpp index 60283dc0..76f9af2f 100644 --- a/src/helpers/WLClasses.hpp +++ b/src/helpers/WLClasses.hpp @@ -283,4 +283,6 @@ struct STextInput { DYNLISTENER(textInputDisable); DYNLISTENER(textInputCommit); DYNLISTENER(textInputDestroy); + + DYNLISTENER(pendingSurfaceDestroy); }; \ No newline at end of file diff --git a/src/managers/input/InputMethodRelay.cpp b/src/managers/input/InputMethodRelay.cpp index 406e76d0..ca8fff9d 100644 --- a/src/managers/input/InputMethodRelay.cpp +++ b/src/managers/input/InputMethodRelay.cpp @@ -57,7 +57,7 @@ void CInputMethodRelay::onNewIME(wlr_input_method_v2* pIME) { Debug::log(LOG, "IME Destroy"); if (PTI) { - PTI->pPendingSurface = PTI->pWlrInput->focused_surface; + setPendingSurface(PTI, PTI->pWlrInput->focused_surface); wlr_text_input_v3_send_leave(PTI->pWlrInput); } @@ -68,7 +68,7 @@ void CInputMethodRelay::onNewIME(wlr_input_method_v2* pIME) { if (PTI) { wlr_text_input_v3_send_enter(PTI->pWlrInput, PTI->pPendingSurface); - PTI->pPendingSurface = nullptr; + setPendingSurface(PTI, nullptr); } } @@ -167,6 +167,8 @@ void CInputMethodRelay::createNewTextInput(wlr_text_input_v3* pInput) { g_pInputManager->m_sIMERelay.commitIMEState(PINPUT->pWlrInput); } + g_pInputManager->m_sIMERelay.setPendingSurface(PINPUT, nullptr); + PINPUT->hyprListener_textInputCommit.removeCallback(); PINPUT->hyprListener_textInputDestroy.removeCallback(); PINPUT->hyprListener_textInputDisable.removeCallback(); @@ -196,7 +198,7 @@ void CInputMethodRelay::onKeyboardFocus(wlr_surface* pSurface) { if (ti.pPendingSurface) { if (pSurface != ti.pPendingSurface) - ti.pPendingSurface = nullptr; + setPendingSurface(&ti, nullptr); } else if (ti.pWlrInput->focused_surface) { @@ -216,9 +218,25 @@ void CInputMethodRelay::onKeyboardFocus(wlr_surface* pSurface) { if (m_pWLRIME) { wlr_text_input_v3_send_enter(ti.pWlrInput, pSurface); } else { - ti.pPendingSurface = pSurface; + setPendingSurface(&ti, pSurface); } } } +} + +void CInputMethodRelay::setPendingSurface(STextInput* pInput, wlr_surface* pSurface) { + pInput->pPendingSurface = pSurface; + + if (pSurface) { + pInput->hyprListener_pendingSurfaceDestroy.initCallback(&pSurface->events.destroy, [](void* owner, void* data) { + const auto PINPUT = (STextInput*)owner; + + PINPUT->pPendingSurface = nullptr; + + PINPUT->hyprListener_pendingSurfaceDestroy.removeCallback(); + }, pInput, "TextInput"); + } else { + pInput->hyprListener_pendingSurfaceDestroy.removeCallback(); + } } \ No newline at end of file diff --git a/src/managers/input/InputMethodRelay.hpp b/src/managers/input/InputMethodRelay.hpp index 4a9ea7b1..00a525d8 100644 --- a/src/managers/input/InputMethodRelay.hpp +++ b/src/managers/input/InputMethodRelay.hpp @@ -20,6 +20,8 @@ public: STextInput* getFocusedTextInput(); STextInput* getFocusableTextInput(); + void setPendingSurface(STextInput*, wlr_surface*); + private: std::list m_lTextInputs;