Skip to content

Commit

Permalink
text-input-v3: move to new impl
Browse files Browse the repository at this point in the history
  • Loading branch information
vaxerski committed Apr 25, 2024
1 parent e87227e commit 1d40af6
Show file tree
Hide file tree
Showing 14 changed files with 317 additions and 73 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,7 @@ protocolNew("staging/alpha-modifier/alpha-modifier-v1.xml" "alpha-modifier-v1" f
protocolNew("staging/ext-foreign-toplevel-list/ext-foreign-toplevel-list-v1.xml" "ext-foreign-toplevel-list-v1" false)
protocolNew("unstable/pointer-gestures/pointer-gestures-unstable-v1.xml" "pointer-gestures-unstable-v1" false)
protocolNew("unstable/keyboard-shortcuts-inhibit/keyboard-shortcuts-inhibit-unstable-v1.xml" "keyboard-shortcuts-inhibit-unstable-v1" false)
protocolNew("unstable/text-input/text-input-unstable-v3.xml" "text-input-unstable-v3" false)

# tools
add_subdirectory(hyprctl)
Expand Down
1 change: 1 addition & 0 deletions protocols/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ new_protocols = [
[wl_protocol_dir, 'staging/ext-foreign-toplevel-list/ext-foreign-toplevel-list-v1.xml'],
[wl_protocol_dir, 'unstable/pointer-gestures/pointer-gestures-unstable-v1.xml'],
[wl_protocol_dir, 'unstable/keyboard-shortcuts-inhibit/keyboard-shortcuts-inhibit-unstable-v1.xml'],
[wl_protocol_dir, 'unstable/text-input/text-input-unstable-v3.xml'],
]

wl_protos_src = []
Expand Down
4 changes: 0 additions & 4 deletions src/Compositor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -230,8 +230,6 @@ void CCompositor::initServer() {
wlr_xdg_foreign_v1_create(m_sWLDisplay, m_sWLRForeignRegistry);
wlr_xdg_foreign_v2_create(m_sWLDisplay, m_sWLRForeignRegistry);

m_sWLRTextInputMgr = wlr_text_input_manager_v3_create(m_sWLDisplay);

m_sWLRIMEMgr = wlr_input_method_manager_v2_create(m_sWLDisplay);

m_sWLRActivation = wlr_xdg_activation_v1_create(m_sWLDisplay);
Expand Down Expand Up @@ -289,7 +287,6 @@ void CCompositor::initAllSignals() {
addWLSignal(&m_sWLRRenderer->events.destroy, &Events::listen_RendererDestroy, m_sWLRRenderer, "WLRRenderer");
addWLSignal(&m_sWLROutputPowerMgr->events.set_mode, &Events::listen_powerMgrSetMode, m_sWLROutputPowerMgr, "PowerMgr");
addWLSignal(&m_sWLRIMEMgr->events.input_method, &Events::listen_newIME, m_sWLRIMEMgr, "IMEMgr");
addWLSignal(&m_sWLRTextInputMgr->events.text_input, &Events::listen_newTextInput, m_sWLRTextInputMgr, "TextInputMgr");
addWLSignal(&m_sWLRActivation->events.request_activate, &Events::listen_activateXDG, m_sWLRActivation, "ActivationV1");
addWLSignal(&m_sWLRSessionLockMgr->events.new_lock, &Events::listen_newSessionLock, m_sWLRSessionLockMgr, "SessionLockMgr");

Expand Down Expand Up @@ -337,7 +334,6 @@ void CCompositor::removeAllSignals() {
removeWLSignal(&Events::listen_RendererDestroy);
removeWLSignal(&Events::listen_powerMgrSetMode);
removeWLSignal(&Events::listen_newIME);
removeWLSignal(&Events::listen_newTextInput);
removeWLSignal(&Events::listen_activateXDG);
removeWLSignal(&Events::listen_newSessionLock);

Expand Down
1 change: 0 additions & 1 deletion src/Compositor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ class CCompositor {
wlr_xdg_foreign_registry* m_sWLRForeignRegistry;
wlr_output_power_manager_v1* m_sWLROutputPowerMgr;
wlr_input_method_manager_v2* m_sWLRIMEMgr;
wlr_text_input_manager_v3* m_sWLRTextInputMgr;
wlr_xdg_activation_v1* m_sWLRActivation;
wlr_linux_dmabuf_v1* m_sWLRLinuxDMABuf;
wlr_backend* m_sWLRHeadlessBackend;
Expand Down
1 change: 0 additions & 1 deletion src/events/Events.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@ namespace Events {

// IME
LISTENER(newIME);
LISTENER(newTextInput);
LISTENER(newVirtualKeyboard);

// Touch
Expand Down
6 changes: 0 additions & 6 deletions src/events/Misc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -210,12 +210,6 @@ void Events::listener_newIME(wl_listener* listener, void* data) {
g_pInputManager->m_sIMERelay.onNewIME((wlr_input_method_v2*)data);
}

void Events::listener_newTextInput(wl_listener* listener, void* data) {
Debug::log(LOG, "New TextInput added!");

g_pInputManager->m_sIMERelay.onNewTextInput((wlr_text_input_v3*)data);
}

void Events::listener_newSessionLock(wl_listener* listener, void* data) {
Debug::log(LOG, "New session lock!");

Expand Down
1 change: 0 additions & 1 deletion src/includes.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ extern "C" {
#include <wlr/types/wlr_xdg_foreign_v2.h>
#include <wlr/types/wlr_output_power_management_v1.h>
#include <wlr/types/wlr_input_method_v2.h>
#include <wlr/types/wlr_text_input_v3.h>
#include <wlr/types/wlr_touch.h>
#include <wlr/types/wlr_switch.h>
#include <wlr/config.h>
Expand Down
3 changes: 3 additions & 0 deletions src/managers/ProtocolManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "../protocols/PointerGestures.hpp"
#include "../protocols/ForeignToplevelWlr.hpp"
#include "../protocols/ShortcutsInhibit.hpp"
#include "../protocols/TextInputV3.hpp"

#include "tearing-control-v1.hpp"
#include "fractional-scale-v1.hpp"
Expand All @@ -27,6 +28,7 @@
#include "pointer-gestures-unstable-v1.hpp"
#include "wlr-foreign-toplevel-management-unstable-v1.hpp"
#include "keyboard-shortcuts-inhibit-unstable-v1.hpp"
#include "text-input-unstable-v3.hpp"

CProtocolManager::CProtocolManager() {

Expand All @@ -43,6 +45,7 @@ CProtocolManager::CProtocolManager() {
PROTO::pointerGestures = std::make_unique<CPointerGesturesProtocol>(&zwp_pointer_gestures_v1_interface, 3, "PointerGestures");
PROTO::foreignToplevelWlr = std::make_unique<CForeignToplevelWlrProtocol>(&zwlr_foreign_toplevel_manager_v1_interface, 3, "ForeignToplevelWlr");
PROTO::shortcutsInhibit = std::make_unique<CKeyboardShortcutsInhibitProtocol>(&zwp_keyboard_shortcuts_inhibit_manager_v1_interface, 1, "ShortcutsInhibit");
PROTO::textInputV3 = std::make_unique<CTextInputV3Protocol>(&zwp_text_input_manager_v3_interface, 1, "TextInputV3");

// Old protocol implementations.
// TODO: rewrite them to use hyprwayland-scanner.
Expand Down
7 changes: 5 additions & 2 deletions src/managers/input/InputMethodRelay.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
#include "InputMethodRelay.hpp"
#include "InputManager.hpp"
#include "../../Compositor.hpp"
#include "../../protocols/TextInputV3.hpp"

CInputMethodRelay::CInputMethodRelay() {
static auto P = g_pHookSystem->hookDynamic("keyboardFocus", [&](void* self, SCallbackInfo& info, std::any param) { onKeyboardFocus(std::any_cast<wlr_surface*>(param)); });

listeners.newTIV3 = PROTO::textInputV3->events.newTextInput.registerListener([this](std::any ti) { onNewTextInput(ti); });
}

void CInputMethodRelay::onNewIME(wlr_input_method_v2* pIME) {
Expand Down Expand Up @@ -142,8 +145,8 @@ CTextInput* CInputMethodRelay::getFocusedTextInput() {
return nullptr;
}

void CInputMethodRelay::onNewTextInput(wlr_text_input_v3* pInput) {
m_vTextInputs.emplace_back(std::make_unique<CTextInput>(pInput));
void CInputMethodRelay::onNewTextInput(std::any tiv3) {
m_vTextInputs.emplace_back(std::make_unique<CTextInput>(std::any_cast<std::weak_ptr<CTextInputV3>>(tiv3)));
}

void CInputMethodRelay::onNewTextInput(STextInputV1* pTIV1) {
Expand Down
8 changes: 7 additions & 1 deletion src/managers/input/InputMethodRelay.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
#include <list>
#include "../../defines.hpp"
#include "../../helpers/WLClasses.hpp"
#include "../../helpers/signal/Listener.hpp"
#include "TextInput.hpp"
#include "InputMethodPopup.hpp"
#include <any>

class CInputManager;
class CHyprRenderer;
Expand All @@ -15,7 +17,7 @@ class CInputMethodRelay {
CInputMethodRelay();

void onNewIME(wlr_input_method_v2*);
void onNewTextInput(wlr_text_input_v3*);
void onNewTextInput(std::any tiv3);
void onNewTextInput(STextInputV1* pTIV1);

wlr_input_method_v2* m_pWLRIME = nullptr;
Expand Down Expand Up @@ -47,6 +49,10 @@ class CInputMethodRelay {

wlr_surface* m_pLastKbFocus = nullptr;

struct {
CHyprSignalListener newTIV3;
} listeners;

DYNLISTENER(textInputNew);
DYNLISTENER(IMECommit);
DYNLISTENER(IMEDestroy);
Expand Down
111 changes: 62 additions & 49 deletions src/managers/input/TextInput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
#include "InputManager.hpp"
#include "../../protocols/TextInputV1.hpp"
#include "../../Compositor.hpp"
#include "../../protocols/TextInputV3.hpp"

CTextInput::CTextInput(STextInputV1* ti) : pV1Input(ti) {
ti->pTextInput = this;
initCallbacks();
}

CTextInput::CTextInput(wlr_text_input_v3* ti) : pWlrInput(ti) {
CTextInput::CTextInput(std::weak_ptr<CTextInputV3> ti) : pV3Input(ti) {
initCallbacks();
}

Expand All @@ -25,31 +26,42 @@ void CTextInput::tiV1Destroyed() {
}

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 (pWlrInput && pWlrInput->current_enabled && focusedSurface())
if (isV3()) {
const auto INPUT = pV3Input.lock();

listeners.enable = INPUT->events.enable.registerListener([this](std::any p) { onEnabled(); });
listeners.disable = INPUT->events.disable.registerListener([this](std::any p) { onDisabled(); });
listeners.commit = INPUT->events.onCommit.registerListener([this](std::any p) { onCommit(); });
listeners.destroy = INPUT->events.destroy.registerListener([this](std::any p) {
const auto INPUT = pV3Input.lock();
if (INPUT && INPUT->current.enabled && focusedSurface())
g_pInputManager->m_sIMERelay.deactivateIME(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");
});
} else {
hyprListener_textInputEnable.initCallback(
&pV1Input->sEnable, [this](void* owner, void* data) { onEnabled(); }, this, "textInput");

hyprListener_textInputCommit.initCallback(
&pV1Input->sCommit, [this](void* owner, void* data) { onCommit(); }, this, "textInput");

hyprListener_textInputDisable.initCallback(
&pV1Input->sDisable, [this](void* owner, void* data) { onDisabled(); }, this, "textInput");

hyprListener_textInputDestroy.initCallback(
&pV1Input->sDestroy,
[this](void* owner, void* data) {
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) {
Expand Down Expand Up @@ -96,7 +108,7 @@ void CTextInput::onCommit() {
return;
}

if (!(pWlrInput ? pWlrInput->current_enabled : pV1Input->active)) {
if (!(isV3() ? pV3Input.lock()->current.enabled : pV1Input->active)) {
Debug::log(WARN, "Disabled TextInput commit?");
return;
}
Expand Down Expand Up @@ -144,7 +156,7 @@ void CTextInput::setFocusedSurface(wlr_surface* pSurface) {
}

bool CTextInput::isV3() {
return pWlrInput;
return !pV1Input;
}

void CTextInput::enter(wlr_surface* pSurface) {
Expand All @@ -166,8 +178,8 @@ void CTextInput::enter(wlr_surface* pSurface) {
enterLocks = 1;
}

if (pWlrInput)
wlr_text_input_v3_send_enter(pWlrInput, pSurface);
if (isV3())
pV3Input.lock()->enter(pSurface);
else {
zwp_text_input_v1_send_enter(pV1Input->resourceImpl, pSurface->resource);
pV1Input->active = true;
Expand All @@ -186,8 +198,8 @@ void CTextInput::leave() {
enterLocks = 0;
}

if (pWlrInput && pWlrInput->focused_surface)
wlr_text_input_v3_send_leave(pWlrInput);
if (isV3() && focusedSurface())
pV3Input.lock()->leave(focusedSurface());
else if (focusedSurface() && pV1Input) {
zwp_text_input_v1_send_leave(pV1Input->resourceImpl);
pV1Input->active = false;
Expand All @@ -199,22 +211,24 @@ void CTextInput::leave() {
}

wlr_surface* CTextInput::focusedSurface() {
return pWlrInput ? pWlrInput->focused_surface : pFocusedSurface;
return pFocusedSurface;
}

wl_client* CTextInput::client() {
return pWlrInput ? wl_resource_get_client(pWlrInput->resource) : pV1Input->client;
return isV3() ? pV3Input.lock()->client() : 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);
const auto INPUT = pV3Input.lock();

if (INPUT->current.surrounding.updated)
wlr_input_method_v2_send_surrounding_text(ime, INPUT->current.surrounding.text.c_str(), INPUT->current.surrounding.cursor, INPUT->current.surrounding.anchor);

wlr_input_method_v2_send_text_change_cause(ime, pWlrInput->current.text_change_cause);
wlr_input_method_v2_send_text_change_cause(ime, INPUT->current.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);
if (INPUT->current.contentType.updated)
wlr_input_method_v2_send_content_type(ime, INPUT->current.contentType.hint, INPUT->current.contentType.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);
Expand All @@ -232,19 +246,18 @@ void CTextInput::commitStateToIME(wlr_input_method_v2* 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);
}
const auto INPUT = pV3Input.lock();

if (ime->current.commit_text) {
wlr_text_input_v3_send_commit_string(pWlrInput, ime->current.commit_text);
}
if (ime->current.preedit.text)
INPUT->preeditString(ime->current.preedit.text, ime->current.preedit.cursor_begin, ime->current.preedit.cursor_end);

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);
}
if (ime->current.commit_text)
INPUT->commitString(ime->current.commit_text);

if (ime->current.delete_.before_length || ime->current.delete_.after_length)
INPUT->deleteSurroundingText(ime->current.delete_.before_length, ime->current.delete_.after_length);

wlr_text_input_v3_send_done(pWlrInput);
INPUT->sendDone();
} else {
if (ime->current.preedit.text) {
zwp_text_input_v1_send_preedit_cursor(pV1Input->resourceImpl, ime->current.preedit.cursor_begin);
Expand All @@ -271,9 +284,9 @@ void CTextInput::updateIMEState(wlr_input_method_v2* ime) {
}

bool CTextInput::hasCursorRectangle() {
return !isV3() || pWlrInput->current.features & WLR_TEXT_INPUT_V3_FEATURE_CURSOR_RECTANGLE;
return !isV3() || pV3Input.lock()->current.box.updated;
}

CBox CTextInput::cursorBox() {
return CBox{isV3() ? pWlrInput->current.cursor_rectangle : pV1Input->cursorRectangle};
return CBox{isV3() ? pV3Input.lock()->current.box.cursorBox : pV1Input->cursorRectangle};
}

0 comments on commit 1d40af6

Please sign in to comment.