summaryrefslogtreecommitdiff
path: root/src/managers/input/InputMethodRelay.cpp
blob: dc03eb8908fdd902159549ffa3f45a28fe0e5013 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
#include "InputMethodRelay.hpp"
#include "InputManager.hpp"
#include "../../Compositor.hpp"

CInputMethodRelay::CInputMethodRelay() {
    
}

void CInputMethodRelay::onNewIME(wlr_input_method_v2* pIME) {
    if (m_pWLRIME) {
        Debug::log(ERR, "Cannot register 2 IMEs at once!");

        wlr_input_method_v2_send_unavailable(pIME);

        return;
    }

    m_pWLRIME = pIME;

    hyprListener_IMECommit.initCallback(&m_pWLRIME->events.commit, [&](void* owner, void* data) {

        const auto PTI = getFocusedTextInput();
        const auto PIMR = (CInputMethodRelay*)owner;

        if (!PTI) {
            Debug::log(LOG, "No focused TextInput on IME Commit");
            return;
        }

        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) {
            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);
        }

        wlr_text_input_v3_send_done(PTI->pWlrInput);

    }, this, "IMERelay");

    hyprListener_IMEDestroy.initCallback(&m_pWLRIME->events.destroy, [&](void* owner, void* data) {

        m_pWLRIME = nullptr;

        hyprListener_IMEDestroy.removeCallback();
        hyprListener_IMECommit.removeCallback();
        hyprListener_IMEGrab.removeCallback();

        m_pKeyboardGrab.reset(nullptr);

        const auto PTI = getFocusedTextInput();

        Debug::log(LOG, "IME Destroy");

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

            wlr_text_input_v3_send_leave(PTI->pWlrInput);
        }

    }, 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<SIMEKbGrab>();

        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) {
        wlr_text_input_v3_send_enter(PTI->pWlrInput, PTI->pPendingSurface);
        setPendingSurface(PTI, nullptr);
    }
}

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) {
            return &ti;
        }
    }

    return nullptr;
}

STextInput* CInputMethodRelay::getFocusableTextInput() {
    for (auto& ti : m_lTextInputs) {
        if (ti.pPendingSurface) {
            return &ti;
        }
    }

    return nullptr;
}

void CInputMethodRelay::onNewTextInput(wlr_text_input_v3* pInput) {
    createNewTextInput(pInput);
}

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

    PTEXTINPUT->pWlrInput = pInput;

    PTEXTINPUT->hyprListener_textInputEnable.initCallback(&pInput->events.enable, [](void* owner, void* data) {

        const auto PINPUT = (STextInput*)owner;

        if (!g_pInputManager->m_sIMERelay.m_pWLRIME) {
            Debug::log(ERR, "Enabling TextInput on no IME!");
            return;
        }

        Debug::log(LOG, "Enable TextInput");

        wlr_input_method_v2_send_activate(g_pInputManager->m_sIMERelay.m_pWLRIME);
        g_pInputManager->m_sIMERelay.commitIMEState(PINPUT->pWlrInput);

    }, PTEXTINPUT, "textInput");

    PTEXTINPUT->hyprListener_textInputCommit.initCallback(&pInput->events.commit, [](void* owner, void* data) {

        const auto PINPUT = (STextInput*)owner;

        if (!g_pInputManager->m_sIMERelay.m_pWLRIME) {
            Debug::log(ERR, "Committing TextInput on no IME!");
            return;
        }

        if (!PINPUT->pWlrInput->current_enabled) {
            Debug::log(ERR, "Disabled TextInput commit?");
            return;
        }

        g_pInputManager->m_sIMERelay.commitIMEState(PINPUT->pWlrInput);

    }, PTEXTINPUT, "textInput");

    PTEXTINPUT->hyprListener_textInputDisable.initCallback(&pInput->events.disable, [](void* owner, void* data) {

        const auto PINPUT = (STextInput*)owner;

        if (!g_pInputManager->m_sIMERelay.m_pWLRIME) {
            Debug::log(ERR, "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.commitIMEState(PINPUT->pWlrInput);

    }, PTEXTINPUT, "textInput");

    PTEXTINPUT->hyprListener_textInputDestroy.initCallback(&pInput->events.destroy, [](void* owner, void* data) {

        const auto PINPUT = (STextInput*)owner;

        if (!g_pInputManager->m_sIMERelay.m_pWLRIME) {
            Debug::log(ERR, "Disabling TextInput on no IME!");
            return;
        }

        if (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.setPendingSurface(PINPUT, nullptr);

        PINPUT->hyprListener_textInputCommit.removeCallback();
        PINPUT->hyprListener_textInputDestroy.removeCallback();
        PINPUT->hyprListener_textInputDisable.removeCallback();
        PINPUT->hyprListener_textInputEnable.removeCallback();

        g_pInputManager->m_sIMERelay.removeTextInput(PINPUT->pWlrInput);

    }, PTEXTINPUT, "textInput");
}

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

void CInputMethodRelay::commitIMEState(wlr_text_input_v3* pInput) {
    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);

    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);
}

void CInputMethodRelay::onKeyboardFocus(wlr_surface* pSurface) {
    if (!m_pWLRIME)
        return;

    for (auto& ti : m_lTextInputs) {
        if (ti.pPendingSurface) {

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

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

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

                wlr_text_input_v3_send_leave(ti.pWlrInput);
            } else {
                continue;
            }

        }

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

            if (m_pWLRIME) {
                wlr_text_input_v3_send_enter(ti.pWlrInput, pSurface);
            } else {
                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();
    }
}