From 9a8a6317ff9dc7d97a0018d8d0b19cd033d2c47b Mon Sep 17 00:00:00 2001 From: vaxerski <43317083+vaxerski@users.noreply.github.com> Date: Fri, 5 Aug 2022 16:21:08 +0200 Subject: [PATCH] multiple IME fixes --- src/Compositor.cpp | 1 + src/events/Devices.cpp | 6 +++ src/events/Events.hpp | 1 + src/helpers/WLClasses.hpp | 9 ++++ src/managers/KeybindManager.cpp | 3 ++ src/managers/input/InputManager.cpp | 50 ++++++++++++++++-- src/managers/input/InputManager.hpp | 1 + src/managers/input/InputMethodRelay.cpp | 70 +++++++++++++++++++++++-- src/managers/input/InputMethodRelay.hpp | 5 ++ 9 files changed, 138 insertions(+), 8 deletions(-) diff --git a/src/Compositor.cpp b/src/Compositor.cpp index b3b7657b..96be097a 100644 --- a/src/Compositor.cpp +++ b/src/Compositor.cpp @@ -201,6 +201,7 @@ void CCompositor::initAllSignals() { addWLSignal(&m_sWLRPointerConstraints->events.new_constraint, &Events::listen_newConstraint, m_sWLRPointerConstraints, "PointerConstraints"); addWLSignal(&m_sWLRXDGDecoMgr->events.new_toplevel_decoration, &Events::listen_NewXDGDeco, m_sWLRXDGDecoMgr, "XDGDecoMgr"); addWLSignal(&m_sWLRVirtPtrMgr->events.new_virtual_pointer, &Events::listen_newVirtPtr, m_sWLRVirtPtrMgr, "VirtPtrMgr"); + addWLSignal(&m_sWLRVKeyboardMgr->events.new_virtual_keyboard, &Events::listen_newVirtualKeyboard, m_sWLRVKeyboardMgr, "VKeyboardMgr"); addWLSignal(&m_sWLRRenderer->events.destroy, &Events::listen_RendererDestroy, m_sWLRRenderer, "WLRRenderer"); addWLSignal(&m_sWLRIdleInhibitMgr->events.new_inhibitor, &Events::listen_newIdleInhibitor, m_sWLRIdleInhibitMgr, "WLRIdleInhibitMgr"); addWLSignal(&m_sWLROutputPowerMgr->events.set_mode, &Events::listen_powerMgrSetMode, m_sWLROutputPowerMgr, "PowerMgr"); diff --git a/src/events/Devices.cpp b/src/events/Devices.cpp index 948e7aba..b29d0dbc 100644 --- a/src/events/Devices.cpp +++ b/src/events/Devices.cpp @@ -189,4 +189,10 @@ void Events::listener_pinchUpdate(wl_listener* listener, void* data) { void Events::listener_pinchEnd(wl_listener* listener, void* data) { const auto EV = (wlr_pointer_pinch_end_event*)data; wlr_pointer_gestures_v1_send_pinch_end(g_pCompositor->m_sWLRPointerGestures, g_pCompositor->m_sSeat.seat, EV->time_msec, EV->cancelled); +} + +void Events::listener_newVirtualKeyboard(wl_listener* listener, void* data) { + const auto WLRKB = (wlr_virtual_keyboard_v1*)data; + + g_pInputManager->newVirtualKeyboard(&WLRKB->keyboard.base); } \ No newline at end of file diff --git a/src/events/Events.hpp b/src/events/Events.hpp index 8d0f58cd..8f969012 100644 --- a/src/events/Events.hpp +++ b/src/events/Events.hpp @@ -137,4 +137,5 @@ namespace Events { // IME LISTENER(newIME); LISTENER(newTextInput); + LISTENER(newVirtualKeyboard); }; \ No newline at end of file diff --git a/src/helpers/WLClasses.hpp b/src/helpers/WLClasses.hpp index 76f9af2f..c07bf46a 100644 --- a/src/helpers/WLClasses.hpp +++ b/src/helpers/WLClasses.hpp @@ -96,6 +96,7 @@ struct SKeyboard { DYNLISTENER(keyboardKey); DYNLISTENER(keyboardDestroy); + bool isVirtual = false; bool active = false; std::string name = ""; @@ -285,4 +286,12 @@ struct STextInput { DYNLISTENER(textInputDestroy); DYNLISTENER(pendingSurfaceDestroy); +}; + +struct SIMEKbGrab { + wlr_input_method_keyboard_grab_v2* pWlrKbGrab = nullptr; + + wlr_keyboard* pKeyboard = nullptr; + + DYNLISTENER(grabDestroy); }; \ No newline at end of file diff --git a/src/managers/KeybindManager.cpp b/src/managers/KeybindManager.cpp index ce81e95b..00cfad3f 100644 --- a/src/managers/KeybindManager.cpp +++ b/src/managers/KeybindManager.cpp @@ -93,6 +93,9 @@ uint32_t CKeybindManager::stringToModMask(std::string mods) { } bool CKeybindManager::onKeyEvent(wlr_keyboard_key_event* e, SKeyboard* pKeyboard) { + if (pKeyboard->isVirtual) + return true; + const auto KEYCODE = e->keycode + 8; // Because to xkbcommon it's +8 from libinput const xkb_keysym_t keysym = xkb_state_key_get_one_sym(wlr_keyboard_from_input_device(pKeyboard->keyboard)->xkb_state, KEYCODE); diff --git a/src/managers/input/InputManager.cpp b/src/managers/input/InputManager.cpp index d0783533..cf381cbd 100644 --- a/src/managers/input/InputManager.cpp +++ b/src/managers/input/InputManager.cpp @@ -436,6 +436,33 @@ void CInputManager::newKeyboard(wlr_input_device* keyboard) { Debug::log(LOG, "New keyboard created, pointers Hypr: %x and WLR: %x", PNEWKEYBOARD, keyboard); } +void CInputManager::newVirtualKeyboard(wlr_input_device* keyboard) { + const auto PNEWKEYBOARD = &m_lKeyboards.emplace_back(); + + PNEWKEYBOARD->keyboard = keyboard; + PNEWKEYBOARD->isVirtual = true; + + try { + PNEWKEYBOARD->name = std::string(keyboard->name); + } catch (std::exception& e) { + Debug::log(ERR, "Keyboard had no name???"); // logic error + } + + PNEWKEYBOARD->hyprListener_keyboardMod.initCallback(&wlr_keyboard_from_input_device(keyboard)->events.modifiers, &Events::listener_keyboardMod, PNEWKEYBOARD, "Keyboard"); + PNEWKEYBOARD->hyprListener_keyboardKey.initCallback(&wlr_keyboard_from_input_device(keyboard)->events.key, &Events::listener_keyboardKey, PNEWKEYBOARD, "Keyboard"); + PNEWKEYBOARD->hyprListener_keyboardDestroy.initCallback(&keyboard->events.destroy, &Events::listener_keyboardDestroy, PNEWKEYBOARD, "Keyboard"); + + if (m_pActiveKeyboard) + m_pActiveKeyboard->active = false; + m_pActiveKeyboard = PNEWKEYBOARD; + + applyConfigToKeyboard(PNEWKEYBOARD); + + wlr_seat_set_keyboard(g_pCompositor->m_sSeat.seat, wlr_keyboard_from_input_device(keyboard)); + + Debug::log(LOG, "New virtual keyboard created, pointers Hypr: %x and WLR: %x", PNEWKEYBOARD, keyboard); +} + void CInputManager::setKeyboardLayout() { for (auto& k : m_lKeyboards) applyConfigToKeyboard(&k); @@ -667,14 +694,29 @@ void CInputManager::onKeyboardKey(wlr_keyboard_key_event* e, SKeyboard* pKeyboar wlr_idle_notify_activity(g_pCompositor->m_sWLRIdle, g_pCompositor->m_sSeat.seat); if (passEvent) { - wlr_seat_set_keyboard(g_pCompositor->m_sSeat.seat, wlr_keyboard_from_input_device(pKeyboard->keyboard)); - wlr_seat_keyboard_notify_key(g_pCompositor->m_sSeat.seat, e->time_msec, e->keycode, e->state); + + const auto PIMEGRAB = m_sIMERelay.getIMEKeyboardGrab(pKeyboard); + + if (PIMEGRAB && PIMEGRAB->pWlrKbGrab && PIMEGRAB->pWlrKbGrab->input_method) { + wlr_input_method_keyboard_grab_v2_set_keyboard(PIMEGRAB->pWlrKbGrab, wlr_keyboard_from_input_device(pKeyboard->keyboard)); + wlr_input_method_keyboard_grab_v2_send_key(PIMEGRAB->pWlrKbGrab, e->time_msec, e->keycode, e->state); + } else { + wlr_seat_set_keyboard(g_pCompositor->m_sSeat.seat, wlr_keyboard_from_input_device(pKeyboard->keyboard)); + wlr_seat_keyboard_notify_key(g_pCompositor->m_sSeat.seat, e->time_msec, e->keycode, e->state); + } } } void CInputManager::onKeyboardMod(void* data, SKeyboard* pKeyboard) { - wlr_seat_set_keyboard(g_pCompositor->m_sSeat.seat, wlr_keyboard_from_input_device(pKeyboard->keyboard)); - wlr_seat_keyboard_notify_modifiers(g_pCompositor->m_sSeat.seat, &wlr_keyboard_from_input_device(pKeyboard->keyboard)->modifiers); + const auto PIMEGRAB = m_sIMERelay.getIMEKeyboardGrab(pKeyboard); + + if (PIMEGRAB && PIMEGRAB->pWlrKbGrab && PIMEGRAB->pWlrKbGrab->input_method) { + wlr_input_method_keyboard_grab_v2_set_keyboard(PIMEGRAB->pWlrKbGrab, wlr_keyboard_from_input_device(pKeyboard->keyboard)); + wlr_input_method_keyboard_grab_v2_send_modifiers(PIMEGRAB->pWlrKbGrab, &wlr_keyboard_from_input_device(pKeyboard->keyboard)->modifiers); + } else { + wlr_seat_set_keyboard(g_pCompositor->m_sSeat.seat, wlr_keyboard_from_input_device(pKeyboard->keyboard)); + wlr_seat_keyboard_notify_modifiers(g_pCompositor->m_sSeat.seat, &wlr_keyboard_from_input_device(pKeyboard->keyboard)->modifiers); + } } void CInputManager::refocus() { diff --git a/src/managers/input/InputManager.hpp b/src/managers/input/InputManager.hpp index 362591a1..ed3940d6 100644 --- a/src/managers/input/InputManager.hpp +++ b/src/managers/input/InputManager.hpp @@ -23,6 +23,7 @@ public: void onKeyboardMod(void*, SKeyboard*); void newKeyboard(wlr_input_device*); + void newVirtualKeyboard(wlr_input_device*); void newMouse(wlr_input_device*, bool virt = false); void destroyKeyboard(SKeyboard*); void destroyMouse(wlr_input_device*); diff --git a/src/managers/input/InputMethodRelay.cpp b/src/managers/input/InputMethodRelay.cpp index ca8fff9d..dc03eb89 100644 --- a/src/managers/input/InputMethodRelay.cpp +++ b/src/managers/input/InputMethodRelay.cpp @@ -1,5 +1,6 @@ #include "InputMethodRelay.hpp" #include "InputManager.hpp" +#include "../../Compositor.hpp" CInputMethodRelay::CInputMethodRelay() { @@ -29,15 +30,17 @@ void CInputMethodRelay::onNewIME(wlr_input_method_v2* pIME) { Debug::log(LOG, "IME Commit"); if (PIMR->m_pWLRIME->current.preedit.text) { + Debug::log(LOG, "IME TextInput preedit"); 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) { + Debug::log(LOG, "IME TextInput commit"); 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) { + if (PIMR->m_pWLRIME->current.delete_.before_length || PIMR->m_pWLRIME->current.delete_.after_length) { + Debug::log(LOG, "IME TextInput delete"); wlr_text_input_v3_send_delete_surrounding_text(PTI->pWlrInput, PIMR->m_pWLRIME->current.delete_.before_length, PIMR->m_pWLRIME->current.delete_.after_length); } @@ -51,6 +54,9 @@ void CInputMethodRelay::onNewIME(wlr_input_method_v2* pIME) { hyprListener_IMEDestroy.removeCallback(); hyprListener_IMECommit.removeCallback(); + hyprListener_IMEGrab.removeCallback(); + + m_pKeyboardGrab.reset(nullptr); const auto PTI = getFocusedTextInput(); @@ -64,6 +70,38 @@ void CInputMethodRelay::onNewIME(wlr_input_method_v2* pIME) { }, this, "IMERelay"); + hyprListener_IMEGrab.initCallback(&m_pWLRIME->events.grab_keyboard, [&](void* owner, void* data) { + + Debug::log(LOG, "IME TextInput Keyboard Grab new"); + + m_pKeyboardGrab.reset(nullptr); + + m_pKeyboardGrab = std::make_unique(); + + m_pKeyboardGrab->pKeyboard = wlr_seat_get_keyboard(g_pCompositor->m_sSeat.seat); + + const auto PKBGRAB = (wlr_input_method_keyboard_grab_v2*)data; + + m_pKeyboardGrab->pWlrKbGrab = PKBGRAB; + + wlr_input_method_keyboard_grab_v2_set_keyboard(m_pKeyboardGrab->pWlrKbGrab, m_pKeyboardGrab->pKeyboard); + + m_pKeyboardGrab->hyprListener_grabDestroy.initCallback(&PKBGRAB->events.destroy, [&](void* owner, void* data) { + + m_pKeyboardGrab->hyprListener_grabDestroy.removeCallback(); + + Debug::log(LOG, "IME TextInput Keyboard Grab destroy"); + + if (m_pKeyboardGrab->pKeyboard) { + wlr_seat_keyboard_notify_modifiers(g_pCompositor->m_sSeat.seat, &m_pKeyboardGrab->pKeyboard->modifiers); + } + + m_pKeyboardGrab.reset(nullptr); + + }, m_pKeyboardGrab.get(), "IME Keyboard Grab"); + + }, this, "IMERelay"); + const auto PTI = getFocusableTextInput(); if (PTI) { @@ -72,6 +110,25 @@ void CInputMethodRelay::onNewIME(wlr_input_method_v2* pIME) { } } +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; + + if (wlr_keyboard_from_input_device(pKeyboard->keyboard) != m_pKeyboardGrab->pKeyboard) + return nullptr; + + return m_pKeyboardGrab.get(); +} + STextInput* CInputMethodRelay::getFocusedTextInput() { for (auto& ti : m_lTextInputs) { if (ti.pWlrInput->focused_surface) { @@ -184,9 +241,14 @@ void CInputMethodRelay::removeTextInput(wlr_text_input_v3* pInput) { } void CInputMethodRelay::commitIMEState(wlr_text_input_v3* pInput) { - wlr_input_method_v2_send_surrounding_text(m_pWLRIME, pInput->current.surrounding.text, pInput->current.surrounding.cursor, pInput->current.surrounding.anchor); + if (pInput->active_features & WLR_TEXT_INPUT_V3_FEATURE_SURROUNDING_TEXT) + wlr_input_method_v2_send_surrounding_text(m_pWLRIME, pInput->current.surrounding.text, pInput->current.surrounding.cursor, pInput->current.surrounding.anchor); + wlr_input_method_v2_send_text_change_cause(m_pWLRIME, pInput->current.text_change_cause); - wlr_input_method_v2_send_content_type(m_pWLRIME, pInput->current.content_type.hint, pInput->current.content_type.purpose); + + if (pInput->active_features & WLR_TEXT_INPUT_V3_FEATURE_CONTENT_TYPE) + wlr_input_method_v2_send_content_type(m_pWLRIME, pInput->current.content_type.hint, pInput->current.content_type.purpose); + wlr_input_method_v2_send_done(m_pWLRIME); } diff --git a/src/managers/input/InputMethodRelay.hpp b/src/managers/input/InputMethodRelay.hpp index 00a525d8..ff6fb128 100644 --- a/src/managers/input/InputMethodRelay.hpp +++ b/src/managers/input/InputMethodRelay.hpp @@ -22,13 +22,18 @@ public: void setPendingSurface(STextInput*, wlr_surface*); + SIMEKbGrab* getIMEKeyboardGrab(SKeyboard*); + private: + std::unique_ptr m_pKeyboardGrab; + std::list m_lTextInputs; DYNLISTENER(textInputNew); DYNLISTENER(IMECommit); DYNLISTENER(IMEDestroy); + DYNLISTENER(IMEGrab); void createNewTextInput(wlr_text_input_v3*); }; \ No newline at end of file