Skip to content

Commit

Permalink
input: text-input-v1 support (#1778)
Browse files Browse the repository at this point in the history
support zwp_text_input_v1

---------

Co-authored-by: Mihai Fufezan <fufexan@protonmail.com>
  • Loading branch information
vaxerski and fufexan committed Mar 14, 2023
1 parent e749af7 commit 5dc1a5f
Show file tree
Hide file tree
Showing 10 changed files with 426 additions and 51 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Expand Up @@ -140,5 +140,6 @@ target_link_libraries(Hyprland
${CMAKE_SOURCE_DIR}/wlr-foreign-toplevel-management-unstable-v1-protocol.o
${CMAKE_SOURCE_DIR}/hyprland-toplevel-export-v1-protocol.o
${CMAKE_SOURCE_DIR}/fractional-scale-v1-protocol.o
${CMAKE_SOURCE_DIR}/text-input-unstable-v1-protocol.o
${CMAKE_SOURCE_DIR}/subprojects/udis86/build/libudis86/liblibudis86.a
)
12 changes: 11 additions & 1 deletion Makefile
Expand Up @@ -131,6 +131,16 @@ fractional-scale-v1-protocol.c:

fractional-scale-v1-protocol.o: fractional-scale-v1-protocol.h

text-input-unstable-v1-protocol.h:
$(WAYLAND_SCANNER) server-header \
$(WAYLAND_PROTOCOLS)/unstable/text-input/text-input-unstable-v1.xml $@

text-input-unstable-v1-protocol.c:
$(WAYLAND_SCANNER) private-code \
$(WAYLAND_PROTOCOLS)/unstable/text-input/text-input-unstable-v1.xml $@

text-input-unstable-v1-protocol.o: text-input-unstable-v1-protocol.h

legacyrenderer:
mkdir -p build && cmake --no-warn-unused-cli -DCMAKE_BUILD_TYPE:STRING=Release -DLEGACY_RENDERER:STRING=true -H./ -B./build -G Ninja
cmake --build ./build --config Release --target all -j$(shell nproc)
Expand Down Expand Up @@ -196,7 +206,7 @@ uninstall:
rm -f ${PREFIX}/share/man/man1/Hyprland.1
rm -f ${PREFIX}/share/man/man1/hyprctl.1

protocols: xdg-shell-protocol.o wlr-layer-shell-unstable-v1-protocol.o wlr-screencopy-unstable-v1-protocol.o idle-protocol.o ext-workspace-unstable-v1-protocol.o pointer-constraints-unstable-v1-protocol.o tablet-unstable-v2-protocol.o wlr-output-power-management-unstable-v1-protocol.o linux-dmabuf-unstable-v1-protocol.o hyprland-toplevel-export-v1-protocol.o wlr-foreign-toplevel-management-unstable-v1-protocol.o fractional-scale-v1-protocol.o
protocols: xdg-shell-protocol.o wlr-layer-shell-unstable-v1-protocol.o wlr-screencopy-unstable-v1-protocol.o idle-protocol.o ext-workspace-unstable-v1-protocol.o pointer-constraints-unstable-v1-protocol.o tablet-unstable-v2-protocol.o wlr-output-power-management-unstable-v1-protocol.o linux-dmabuf-unstable-v1-protocol.o hyprland-toplevel-export-v1-protocol.o wlr-foreign-toplevel-management-unstable-v1-protocol.o fractional-scale-v1-protocol.o text-input-unstable-v1-protocol.o

fixwlr:
sed -i -E 's/(soversion = 12)([^032]|$$)/soversion = 12032/g' subprojects/wlroots/meson.build
Expand Down
1 change: 1 addition & 0 deletions protocols/meson.build
Expand Up @@ -21,6 +21,7 @@ wayland_scanner = find_program(
protocols = [
[wl_protocol_dir, 'stable/xdg-shell/xdg-shell.xml'],
[wl_protocol_dir, 'unstable/linux-dmabuf/linux-dmabuf-unstable-v1.xml'],
[wl_protocol_dir, 'unstable/text-input/text-input-unstable-v1.xml'],
[wl_protocol_dir, 'staging/fractional-scale/fractional-scale-v1.xml'],
['wlr-foreign-toplevel-management-unstable-v1.xml'],
['wlr-layer-shell-unstable-v1.xml'],
Expand Down
3 changes: 3 additions & 0 deletions src/helpers/WLClasses.hpp
Expand Up @@ -297,8 +297,11 @@ struct SSwipeGesture {
CMonitor* pMonitor = nullptr;
};

struct STextInputV1;

struct STextInput {
wlr_text_input_v3* pWlrInput = nullptr;
STextInputV1* pV1Input = nullptr;

wlr_surface* pPendingSurface = nullptr;

Expand Down
1 change: 1 addition & 0 deletions src/managers/ProtocolManager.cpp
Expand Up @@ -3,4 +3,5 @@
CProtocolManager::CProtocolManager() {
m_pToplevelExportProtocolManager = std::make_unique<CToplevelExportProtocolManager>();
m_pFractionalScaleProtocolManager = std::make_unique<CFractionalScaleProtocolManager>();
m_pTextInputV1ProtocolManager = std::make_unique<CTextInputV1ProtocolManager>();
}
2 changes: 2 additions & 0 deletions src/managers/ProtocolManager.hpp
Expand Up @@ -3,13 +3,15 @@
#include "../defines.hpp"
#include "../protocols/ToplevelExport.hpp"
#include "../protocols/FractionalScale.hpp"
#include "../protocols/TextInputV1.hpp"

class CProtocolManager {
public:
CProtocolManager();

std::unique_ptr<CToplevelExportProtocolManager> m_pToplevelExportProtocolManager;
std::unique_ptr<CFractionalScaleProtocolManager> m_pFractionalScaleProtocolManager;
std::unique_ptr<CTextInputV1ProtocolManager> m_pTextInputV1ProtocolManager;
};

inline std::unique_ptr<CProtocolManager> g_pProtocolManager;
157 changes: 110 additions & 47 deletions src/managers/input/InputMethodRelay.cpp
Expand Up @@ -28,20 +28,41 @@ void CInputMethodRelay::onNewIME(wlr_input_method_v2* pIME) {
return;
}

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 (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.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);
}
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);
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, 0);
zwp_text_input_v1_send_preedit_styling(PTI->pV1Input->resourceImpl, 0, std::string(PIMR->m_pWLRIME->current.preedit.text).length() - 1,
ZWP_TEXT_INPUT_V1_PREEDIT_STYLE_ACTIVE);
zwp_text_input_v1_send_preedit_string(PTI->pV1Input->resourceImpl, PTI->pV1Input->serial, PIMR->m_pWLRIME->current.preedit.text, "");
}

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, PIMR->m_pWLRIME->current.delete_.before_length,
PIMR->m_pWLRIME->current.delete_.after_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);
}
}
},
this, "IMERelay");

Expand All @@ -62,9 +83,12 @@ void CInputMethodRelay::onNewIME(wlr_input_method_v2* pIME) {
Debug::log(LOG, "IME Destroy");

if (PTI) {
setPendingSurface(PTI, PTI->pWlrInput->focused_surface);
setPendingSurface(PTI, focusedSurface(PTI));

wlr_text_input_v3_send_leave(PTI->pWlrInput);
if (PTI->pWlrInput)
wlr_text_input_v3_send_leave(PTI->pWlrInput);
else
zwp_text_input_v1_send_leave(PTI->pV1Input->resourceImpl);
}
},
this, "IMERelay");
Expand Down Expand Up @@ -122,11 +146,19 @@ void CInputMethodRelay::onNewIME(wlr_input_method_v2* pIME) {
const auto PTI = getFocusableTextInput();

if (PTI) {
wlr_text_input_v3_send_enter(PTI->pWlrInput, PTI->pPendingSurface);
if (PTI->pWlrInput)
wlr_text_input_v3_send_enter(PTI->pWlrInput, PTI->pPendingSurface);
else
zwp_text_input_v1_send_enter(PTI->pV1Input->resourceImpl, PTI->pPendingSurface->resource);

setPendingSurface(PTI, nullptr);
}
}

wlr_surface* CInputMethodRelay::focusedSurface(STextInput* pTI) {
return pTI->pWlrInput ? pTI->pWlrInput->focused_surface : pTI->pV1Input->focusedSurface;
}

void CInputMethodRelay::updateInputPopup(SIMEPopup* pPopup) {
if (!pPopup->pSurface->mapped)
return;
Expand All @@ -136,12 +168,12 @@ void CInputMethodRelay::updateInputPopup(SIMEPopup* pPopup) {

const auto PFOCUSEDTI = getFocusedTextInput();

if (!PFOCUSEDTI || !PFOCUSEDTI->pWlrInput->focused_surface)
if (!PFOCUSEDTI || !focusedSurface(PFOCUSEDTI))
return;

bool cursorRect = PFOCUSEDTI->pWlrInput->current.features & WLR_TEXT_INPUT_V3_FEATURE_CURSOR_RECTANGLE;
const auto PFOCUSEDSURFACE = PFOCUSEDTI->pWlrInput->focused_surface;
auto cursorBox = PFOCUSEDTI->pWlrInput->current.cursor_rectangle;
bool cursorRect = PFOCUSEDTI->pWlrInput ? PFOCUSEDTI->pWlrInput->current.features & WLR_TEXT_INPUT_V3_FEATURE_CURSOR_RECTANGLE : true;
const auto PFOCUSEDSURFACE = focusedSurface(PFOCUSEDTI);
auto cursorBox = PFOCUSEDTI->pWlrInput ? PFOCUSEDTI->pWlrInput->current.cursor_rectangle : PFOCUSEDTI->pV1Input->cursorRectangle;

Vector2D parentPos;
Vector2D parentSize;
Expand Down Expand Up @@ -238,12 +270,12 @@ void CInputMethodRelay::damagePopup(SIMEPopup* pPopup) {

const auto PFOCUSEDTI = getFocusedTextInput();

if (!PFOCUSEDTI || !PFOCUSEDTI->pWlrInput->focused_surface)
if (!PFOCUSEDTI || !focusedSurface(PFOCUSEDTI))
return;

Vector2D parentPos;

const auto PFOCUSEDSURFACE = PFOCUSEDTI->pWlrInput->focused_surface;
const auto PFOCUSEDSURFACE = focusedSurface(PFOCUSEDTI);

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));
Expand Down Expand Up @@ -279,8 +311,9 @@ SIMEKbGrab* CInputMethodRelay::getIMEKeyboardGrab(SKeyboard* pKeyboard) {
}

STextInput* CInputMethodRelay::getFocusedTextInput() {

for (auto& ti : m_lTextInputs) {
if (ti.pWlrInput->focused_surface) {
if (focusedSurface(&ti)) {
return &ti;
}
}
Expand All @@ -302,13 +335,17 @@ void CInputMethodRelay::onNewTextInput(wlr_text_input_v3* pInput) {
createNewTextInput(pInput);
}

void CInputMethodRelay::createNewTextInput(wlr_text_input_v3* pInput) {
void CInputMethodRelay::createNewTextInput(wlr_text_input_v3* pInput, STextInputV1* pTIV1) {
const auto PTEXTINPUT = &m_lTextInputs.emplace_back();

PTEXTINPUT->pWlrInput = pInput;
PTEXTINPUT->pV1Input = pTIV1;

if (pTIV1)
pTIV1->pTextInput = PTEXTINPUT;

PTEXTINPUT->hyprListener_textInputEnable.initCallback(
&pInput->events.enable,
pInput ? &pInput->events.enable : &pTIV1->sEnable,
[](void* owner, void* data) {
const auto PINPUT = (STextInput*)owner;

Expand All @@ -320,12 +357,12 @@ void CInputMethodRelay::createNewTextInput(wlr_text_input_v3* pInput) {
Debug::log(LOG, "Enable TextInput");

wlr_input_method_v2_send_activate(g_pInputManager->m_sIMERelay.m_pWLRIME);
g_pInputManager->m_sIMERelay.commitIMEState(PINPUT->pWlrInput);
g_pInputManager->m_sIMERelay.commitIMEState(PINPUT);
},
PTEXTINPUT, "textInput");

PTEXTINPUT->hyprListener_textInputCommit.initCallback(
&pInput->events.commit,
pInput ? &pInput->events.commit : &pTIV1->sCommit,
[](void* owner, void* data) {
const auto PINPUT = (STextInput*)owner;

Expand All @@ -339,12 +376,12 @@ void CInputMethodRelay::createNewTextInput(wlr_text_input_v3* pInput) {
return;
}

g_pInputManager->m_sIMERelay.commitIMEState(PINPUT->pWlrInput);
g_pInputManager->m_sIMERelay.commitIMEState(PINPUT);
},
PTEXTINPUT, "textInput");

PTEXTINPUT->hyprListener_textInputDisable.initCallback(
&pInput->events.disable,
pInput ? &pInput->events.disable : &pTIV1->sDisable,
[](void* owner, void* data) {
const auto PINPUT = (STextInput*)owner;

Expand All @@ -357,12 +394,12 @@ void CInputMethodRelay::createNewTextInput(wlr_text_input_v3* pInput) {

wlr_input_method_v2_send_deactivate(g_pInputManager->m_sIMERelay.m_pWLRIME);

g_pInputManager->m_sIMERelay.commitIMEState(PINPUT->pWlrInput);
g_pInputManager->m_sIMERelay.commitIMEState(PINPUT);
},
PTEXTINPUT, "textInput");

PTEXTINPUT->hyprListener_textInputDestroy.initCallback(
&pInput->events.destroy,
pInput ? &pInput->events.destroy : &pTIV1->sDestroy,
[](void* owner, void* data) {
const auto PINPUT = (STextInput*)owner;

Expand All @@ -371,10 +408,10 @@ void CInputMethodRelay::createNewTextInput(wlr_text_input_v3* pInput) {
return;
}

if (PINPUT->pWlrInput->current_enabled) {
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->pWlrInput);
g_pInputManager->m_sIMERelay.commitIMEState(PINPUT);
}

g_pInputManager->m_sIMERelay.setPendingSurface(PINPUT, nullptr);
Expand All @@ -384,26 +421,40 @@ void CInputMethodRelay::createNewTextInput(wlr_text_input_v3* pInput) {
PINPUT->hyprListener_textInputDisable.removeCallback();
PINPUT->hyprListener_textInputEnable.removeCallback();

g_pInputManager->m_sIMERelay.removeTextInput(PINPUT->pWlrInput);
g_pInputManager->m_sIMERelay.removeTextInput(PINPUT);
},
PTEXTINPUT, "textInput");
}

void CInputMethodRelay::removeTextInput(wlr_text_input_v3* pInput) {
m_lTextInputs.remove_if([&](const auto& other) { return other.pWlrInput == pInput; });
void CInputMethodRelay::removeTextInput(STextInput* pInput) {
m_lTextInputs.remove_if([&](const auto& other) { return other.pWlrInput == pInput->pWlrInput && other.pV1Input == pInput->pV1Input; });
}

void CInputMethodRelay::commitIMEState(wlr_text_input_v3* pInput) {
void CInputMethodRelay::commitIMEState(STextInput* pInput) {
if (!m_pWLRIME)
return;

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);
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, pInput->current.text_change_cause);
wlr_input_method_v2_send_text_change_cause(m_pWLRIME, 0);

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);
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);
Expand All @@ -416,28 +467,40 @@ void CInputMethodRelay::onKeyboardFocus(wlr_surface* pSurface) {
if (!m_pWLRIME)
return;

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 (ti.pPendingSurface) {

if (pSurface != ti.pPendingSurface)
setPendingSurface(&ti, nullptr);

} else if (ti.pWlrInput->focused_surface) {
} else if (focusedSurface(&ti)) {

if (pSurface != ti.pWlrInput->focused_surface) {
if (pSurface != focusedSurface(&ti)) {
wlr_input_method_v2_send_deactivate(m_pWLRIME);
commitIMEState(ti.pWlrInput);

wlr_text_input_v3_send_leave(ti.pWlrInput);
commitIMEState(&ti);

if (ti.pWlrInput)
wlr_text_input_v3_send_leave(ti.pWlrInput);
else {
zwp_text_input_v1_send_leave(ti.pV1Input->resourceImpl);
ti.pV1Input->focusedSurface = nullptr;
}
} else {
continue;
}
}

if (pSurface && wl_resource_get_client(ti.pWlrInput->resource) == wl_resource_get_client(pSurface->resource)) {
if (pSurface && client(&ti) == wl_resource_get_client(pSurface->resource)) {

if (m_pWLRIME) {
wlr_text_input_v3_send_enter(ti.pWlrInput, pSurface);
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.pV1Input->focusedSurface = pSurface;
}
} else {
setPendingSurface(&ti, pSurface);
}
Expand Down

0 comments on commit 5dc1a5f

Please sign in to comment.