inputmethod.cpp 17.6 KB
Newer Older
Vlad Zahorodnii's avatar
Vlad Zahorodnii committed
1
2
3
/*
    KWin - the KDE window manager
    This file is part of the KDE project.
4

Vlad Zahorodnii's avatar
Vlad Zahorodnii committed
5
    SPDX-FileCopyrightText: 2016 Martin Gräßlin <mgraesslin@kde.org>
6

Vlad Zahorodnii's avatar
Vlad Zahorodnii committed
7
8
    SPDX-License-Identifier: GPL-2.0-or-later
*/
9
#include "inputmethod.h"
10
#include "abstract_client.h"
11
#include "virtualkeyboard_dbus.h"
12
#include "input.h"
13
#include "keyboard_input.h"
14
15
16
17
#include "utils.h"
#include "screens.h"
#include "wayland_server.h"
#include "workspace.h"
18
#include "screenlockerwatcher.h"
19
#include "deleted.h"
20

21
22
#include <KWaylandServer/display.h>
#include <KWaylandServer/seat_interface.h>
23
#include <KWaylandServer/textinput_v3_interface.h>
24
#include <KWaylandServer/surface_interface.h>
25
#include <KWaylandServer/inputmethod_v1_interface.h>
26
27
28
29
30
31
32
33

#include <KStatusNotifierItem>
#include <KLocalizedString>

#include <QDBusConnection>
#include <QDBusPendingCall>
#include <QDBusMessage>

34
35
36
#include <linux/input-event-codes.h>
#include <xkbcommon/xkbcommon-keysyms.h>

37
using namespace KWaylandServer;
38
39
40
41

namespace KWin
{

42
KWIN_SINGLETON_FACTORY(InputMethod)
43

44
InputMethod::InputMethod(QObject *parent)
45
46
47
48
49
    : QObject(parent)
{
    // this is actually too late. Other processes are started before init,
    // so might miss the availability of text input
    // but without Workspace we don't have the window listed at all
50
    connect(kwinApp(), &Application::workspaceCreated, this, &InputMethod::init);
51
52
}

53
InputMethod::~InputMethod() = default;
54

55
void InputMethod::init()
56
{
57
    connect(ScreenLockerWatcher::self(), &ScreenLockerWatcher::aboutToLock, this, &InputMethod::hide);
58

59
    if (waylandServer()) {
60
        m_enabled = !input()->hasAlphaNumericKeyboard();
61
        qCDebug(KWIN_VIRTUALKEYBOARD) << "enabled by default: " << m_enabled;
62
63
        connect(input(), &InputRedirection::hasAlphaNumericKeyboardChanged, this,
            [this] (bool set) {
64
                qCDebug(KWIN_VIRTUALKEYBOARD) << "AlphaNumeric Keyboard changed:" << set << "toggling virtual keyboard.";
65
                setEnabled(!set);
66
67
68
69
            }
        );
    }

70
    qCDebug(KWIN_VIRTUALKEYBOARD) << "Registering the SNI";
71
    m_sni = new KStatusNotifierItem(QStringLiteral("kwin-virtual-keyboard"), this);
72
    m_sni->setStandardActionsEnabled(false);
73
74
75
76
77
78
79
80
81
    m_sni->setCategory(KStatusNotifierItem::Hardware);
    m_sni->setStatus(KStatusNotifierItem::Passive);
    m_sni->setTitle(i18n("Virtual Keyboard"));
    updateSni();
    connect(m_sni, &KStatusNotifierItem::activateRequested, this,
        [this] {
            setEnabled(!m_enabled);
        }
    );
82
    connect(this, &InputMethod::enabledChanged, this, &InputMethod::updateSni);
83

84
    new VirtualKeyboardDBus(this);
85
    qCDebug(KWIN_VIRTUALKEYBOARD) << "Registering the DBus interface";
86
    connect(input(), &InputRedirection::keyStateChanged, this, &InputMethod::hide);
87
88

    if (waylandServer()) {
89
90
        new TextInputManagerV2Interface(waylandServer()->display());
        new TextInputManagerV3Interface(waylandServer()->display());
91

92
93
        connect(workspace(), &Workspace::clientAdded, this, &InputMethod::clientAdded);
        connect(waylandServer()->seat(), &SeatInterface::focusedTextInputSurfaceChanged, this, &InputMethod::handleFocusedSurfaceChanged);
94
95

        TextInputV2Interface *textInputV2 = waylandServer()->seat()->textInputV2();
96
97
98
99
        connect(textInputV2, &TextInputV2Interface::requestShowInputPanel, this, &InputMethod::show);
        connect(textInputV2, &TextInputV2Interface::requestHideInputPanel, this, &InputMethod::hide);
        connect(textInputV2, &TextInputV2Interface::surroundingTextChanged, this, &InputMethod::surroundingTextChanged);
        connect(textInputV2, &TextInputV2Interface::contentTypeChanged, this, &InputMethod::contentTypeChanged);
100
        connect(textInputV2, &TextInputV2Interface::stateUpdated, this, &InputMethod::textInputInterfaceV2StateUpdated);
101
102

        TextInputV3Interface *textInputV3 = waylandServer()->seat()->textInputV3();
103
104
105
        connect(textInputV3, &TextInputV3Interface::surroundingTextChanged, this, &InputMethod::surroundingTextChanged);
        connect(textInputV3, &TextInputV3Interface::contentTypeChanged, this, &InputMethod::contentTypeChanged);
        connect(textInputV3, &TextInputV3Interface::stateCommitted, this, &InputMethod::stateCommitted);
106
107
108
109
110

        if (m_enabled) {
            connect(textInputV2, &TextInputV2Interface::enabledChanged, this, &InputMethod::textInputInterfaceV2EnabledChanged);
            connect(textInputV3, &TextInputV3Interface::enabledChanged, this, &InputMethod::textInputInterfaceV3EnabledChanged);
        }
111
    }
112
}
113

114
void InputMethod::show()
115
{
116
    setActive(true);
117
118
119
120
}

void InputMethod::hide()
{
121
    setActive(false);
122
123
}

124
void InputMethod::setActive(bool active)
125
{
126
    if (m_active) {
127
128
        waylandServer()->inputMethod()->sendDeactivate();
    }
129

130
    if (active) {
131
132
133
134
135
        if (!m_enabled) {
            return;
        }

        waylandServer()->inputMethod()->sendActivate();
136
        if (m_active) {
137
138
139
140
            adoptInputMethodContext();
        }
    } else {
        updateInputPanelState();
141
    }
142

143
144
145
    if (m_active != active) {
        m_active = active;
        Q_EMIT activeChanged(active);
146
    }
147
148
}

149
void InputMethod::clientAdded(AbstractClient* client)
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
{
    if (!client->isInputMethod()) {
        return;
    }
    m_inputClient = client;
    auto refreshFrame = [this] {
        if (!m_trackedClient) {
            return;
        }

        if (m_inputClient && !m_inputClient->inputGeometry().isEmpty()) {
            m_trackedClient->setVirtualKeyboardGeometry(m_inputClient->inputGeometry());
        }
    };
    connect(client->surface(), &SurfaceInterface::inputChanged, this, refreshFrame);
    connect(client, &QObject::destroyed, this, [this] {
        if (m_trackedClient) {
            m_trackedClient->setVirtualKeyboardGeometry({});
        }
    });
    connect(m_inputClient, &AbstractClient::frameGeometryChanged, this, refreshFrame);
171
172
173
174
    // Current code have a assumption that InputMethod started by the kwin is virtual keyboard,
    // InputMethod::hide sends out a deactivate signal to input-method client, this is not desired
    // when we support input methods like ibus which can show and hide surfaces/windows as they please
    // and are not exactly Virtual keyboards.
175
176
    connect(m_inputClient, &AbstractClient::windowHidden, this, &InputMethod::hide);
    connect(m_inputClient, &AbstractClient::windowClosed, this, &InputMethod::hide);
177
178
}

179
void InputMethod::handleFocusedSurfaceChanged()
180
{
181
182
183
    SurfaceInterface *focusedSurface = waylandServer()->seat()->focusedTextInputSurface();
    if (focusedSurface) {
        AbstractClient *focusedClient = waylandServer()->findClient(focusedSurface);
184
185
        // Reset the old client virtual keybaord geom if necessary
        // Old and new clients could be the same if focus moves between subsurfaces
186
        if (m_trackedClient != focusedClient) {
187
188
189
            if (m_trackedClient) {
                m_trackedClient->setVirtualKeyboardGeometry(QRect());
            }
190
            m_trackedClient = focusedClient;
191
        }
192
        updateInputPanelState();
193
    } else {
194
        setActive(false);
195
196
197
    }
}

198
void InputMethod::surroundingTextChanged()
199
{
200
201
    auto t2 = waylandServer()->seat()->textInputV2();
    auto t3 = waylandServer()->seat()->textInputV3();
202
203
204
205
    auto inputContext = waylandServer()->inputMethod()->context();
    if (!inputContext) {
        return;
    }
206
207
208
209
210
211
212
213
    if (t2 && t2->isEnabled()) {
        inputContext->sendSurroundingText(t2->surroundingText(), t2->surroundingTextCursorPosition(), t2->surroundingTextSelectionAnchor());
        return;
    }
    if (t3 && t3->isEnabled()) {
        inputContext->sendSurroundingText(t3->surroundingText(), t3->surroundingTextCursorPosition(), t3->surroundingTextSelectionAnchor());
        return;
    }
214
215
}

216
void InputMethod::contentTypeChanged()
217
{
218
219
    auto t2 = waylandServer()->seat()->textInputV2();
    auto t3 = waylandServer()->seat()->textInputV3();
220
221
222
223
    auto inputContext = waylandServer()->inputMethod()->context();
    if (!inputContext) {
        return;
    }
224
225
226
227
228
229
    if (t2 && t2->isEnabled()) {
        inputContext->sendContentType(t2->contentHints(), t2->contentPurpose());
    }
    if (t3 && t3->isEnabled()) {
        inputContext->sendContentType(t3->contentHints(), t3->contentPurpose());
    }
230
231
}

232
void InputMethod::textInputInterfaceV2StateUpdated(quint32 serial, KWaylandServer::TextInputV2Interface::UpdateReason reason)
233
{
234
    auto t2 = waylandServer()->seat()->textInputV2();
235
236
237
238
    auto inputContext = waylandServer()->inputMethod()->context();
    if (!inputContext) {
        return;
    }
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
    if (!t2 || !t2->isEnabled()) {
        return;
    }
    switch (reason) {
    case KWaylandServer::TextInputV2Interface::UpdateReason::StateChange:
        inputContext->sendCommitState(serial);
        break;
    case KWaylandServer::TextInputV2Interface::UpdateReason::StateEnter:
        waylandServer()->inputMethod()->sendActivate();
        inputContext->sendCommitState(serial);
        break;
    case KWaylandServer::TextInputV2Interface::UpdateReason::StateFull:
        adoptInputMethodContext();
        inputContext->sendCommitState(serial);
        break;
    case KWaylandServer::TextInputV2Interface::UpdateReason::StateReset:
        inputContext->sendReset();
        inputContext->sendCommitState(serial);
        break;
258
    }
259
260
}

261
void InputMethod::textInputInterfaceV2EnabledChanged()
262
{
263
    auto t = waylandServer()->seat()->textInputV2();
264
265
266
267
268
269
270
271
    if (t->isEnabled()) {
        waylandServer()->inputMethod()->sendActivate();
        adoptInputMethodContext();
    } else {
        hide();
    }
}

272
void InputMethod::textInputInterfaceV3EnabledChanged()
273
274
275
276
277
278
279
280
281
282
283
{
    auto t3 = waylandServer()->seat()->textInputV3();
    if (t3->isEnabled()) {
        waylandServer()->inputMethod()->sendActivate();
    } else {
        waylandServer()->inputMethod()->sendDeactivate();
        // reset value of preedit when textinput is disabled
        preedit.text = QString();
        preedit.begin = 0;
        preedit.end = 0;
    }
284
285
286
287
288
    auto context = waylandServer()->inputMethod()->context();
    if (context) {
        context->sendReset();
        adoptInputMethodContext();
    }
289
290
}

291
void InputMethod::stateCommitted(uint32_t serial)
292
293
294
295
296
297
298
299
{
    auto inputContext = waylandServer()->inputMethod()->context();
    if (!inputContext) {
        return;
    }
    inputContext->sendCommitState(serial);
}

300
void InputMethod::setEnabled(bool enabled)
301
302
303
304
305
{
    if (m_enabled == enabled) {
        return;
    }
    m_enabled = enabled;
306
    emit enabledChanged(m_enabled);
307
308
309
310
311
312
313
314
315
316

    // send OSD message
    QDBusMessage msg = QDBusMessage::createMethodCall(
        QStringLiteral("org.kde.plasmashell"),
        QStringLiteral("/org/kde/osdService"),
        QStringLiteral("org.kde.osdService"),
        QStringLiteral("virtualKeyboardEnabledChanged")
    );
    msg.setArguments({enabled});
    QDBusConnection::sessionBus().asyncCall(msg);
317
318
319
320
321
322
323
324
325
326
327
328

    auto textInputV2 = waylandServer()->seat()->textInputV2();
    auto textInputV3 = waylandServer()->seat()->textInputV3();
    if (m_enabled) {
        connect(textInputV2, &TextInputV2Interface::enabledChanged, this, &InputMethod::textInputInterfaceV2EnabledChanged, Qt::UniqueConnection);
        connect(textInputV3, &TextInputV3Interface::enabledChanged, this, &InputMethod::textInputInterfaceV3EnabledChanged, Qt::UniqueConnection);
    } else {
        hide();

        disconnect(textInputV2, &TextInputV2Interface::enabledChanged, this, &InputMethod::textInputInterfaceV2EnabledChanged);
        disconnect(textInputV3, &TextInputV3Interface::enabledChanged, this, &InputMethod::textInputInterfaceV3EnabledChanged);
    }
329
330
}

331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
static quint32 keysymToKeycode(quint32 sym)
{
    switch(sym) {
    case XKB_KEY_BackSpace:
        return KEY_BACKSPACE;
    case XKB_KEY_Return:
        return KEY_ENTER;
    case XKB_KEY_Left:
        return KEY_LEFT;
    case XKB_KEY_Right:
        return KEY_RIGHT;
    case XKB_KEY_Up:
        return KEY_UP;
    case XKB_KEY_Down:
        return KEY_DOWN;
    default:
        return KEY_UNKNOWN;
    }
}

351
352
353
354
static void keysymReceived(quint32 serial, quint32 time, quint32 sym, bool pressed, Qt::KeyboardModifiers modifiers)
{
    Q_UNUSED(serial)
    Q_UNUSED(time)
355
356
357
358
359
360
361
362
363
364
365
    auto t2 = waylandServer()->seat()->textInputV2();
    if (t2 && t2->isEnabled()) {
        if (pressed) {
            t2->keysymPressed(sym, modifiers);
        } else {
            t2->keysymReleased(sym, modifiers);
        }
        return;
    }
    auto t3 = waylandServer()->seat()->textInputV3();
    if (t3 && t3->isEnabled()) {
366
        if (pressed) {
367
            waylandServer()->seat()->keyboard()->keyPressed(keysymToKeycode(sym));
368
        } else {
369
            waylandServer()->seat()->keyboard()->keyReleased(keysymToKeycode(sym));
370
        }
371
        return;
372
373
374
375
376
377
    }
}

static void commitString(qint32 serial, const QString &text)
{
    Q_UNUSED(serial)
378
379
380
381
382
383
384
385
386
387
388
    auto t2 = waylandServer()->seat()->textInputV2();
    if (t2 && t2->isEnabled()) {
        t2->commitString(text.toUtf8());
        t2->preEdit({}, {});
        return;
    }
    auto t3 = waylandServer()->seat()->textInputV3();
    if (t3 && t3->isEnabled()) {
        t3->commitString(text.toUtf8());
        t3->done();
        return;
389
390
391
    }
}

392
static void deleteSurroundingText(int32_t index, uint32_t length)
393
{
394
395
396
397
398
399
400
    auto t2 = waylandServer()->seat()->textInputV2();
    if (t2 && t2->isEnabled()) {
        t2->deleteSurroundingText(index, length);
    }
    auto t3 = waylandServer()->seat()->textInputV3();
    if (t3 && t3->isEnabled()) {
        t3->deleteSurroundingText(index, length);
401
402
403
    }
}

404
static void setCursorPosition(qint32 index, qint32 anchor)
405
{
406
407
408
    auto t2 = waylandServer()->seat()->textInputV2();
    if (t2 && t2->isEnabled()) {
        t2->setCursorPosition(index, anchor);
409
410
411
    }
}

412
static void setLanguage(uint32_t serial, const QString &language)
413
{
414
415
416
417
    Q_UNUSED(serial)
    auto t2 = waylandServer()->seat()->textInputV2();
    if (t2 && t2->isEnabled()) {
        t2->setLanguage(language.toUtf8());
418
419
420
    }
}

421
static void setTextDirection(uint32_t serial, Qt::LayoutDirection direction)
422
{
423
424
425
426
    Q_UNUSED(serial)
    auto t2 = waylandServer()->seat()->textInputV2();
    if (t2 && t2->isEnabled()) {
        t2->setTextDirection(direction);
427
428
429
    }
}

430
void InputMethod::setPreeditCursor(qint32 index)
431
{
432
433
434
435
436
437
438
439
440
    auto t2 = waylandServer()->seat()->textInputV2();
    if (t2 && t2->isEnabled()) {
        t2->setPreEditCursor(index);
    }
    auto t3 = waylandServer()->seat()->textInputV3();
    if (t3 && t3->isEnabled()) {
        preedit.begin = index;
        preedit.end = index;
        t3->sendPreEditString(preedit.text, preedit.begin, preedit.end);
441
442
443
    }
}

444

445
void InputMethod::setPreeditString(uint32_t serial, const QString &text, const QString &commit)
446
447
{
    Q_UNUSED(serial)
448
449
450
451
452
453
454
455
    auto t2 = waylandServer()->seat()->textInputV2();
    if (t2 && t2->isEnabled()) {
        t2->preEdit(text.toUtf8(), commit.toUtf8());
    }
    auto t3 = waylandServer()->seat()->textInputV3();
    if (t3 && t3->isEnabled()) {
        preedit.text = text;
        t3->sendPreEditString(preedit.text, preedit.begin, preedit.end);
456
457
458
    }
}

459
void InputMethod::adoptInputMethodContext()
460
461
462
{
    auto inputContext = waylandServer()->inputMethod()->context();

463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
    TextInputV2Interface *t2 = waylandServer()->seat()->textInputV2();
    TextInputV3Interface *t3 = waylandServer()->seat()->textInputV3();

    if (t2 && t2->isEnabled()) {
        inputContext->sendSurroundingText(t2->surroundingText(), t2->surroundingTextCursorPosition(), t2->surroundingTextSelectionAnchor());
        inputContext->sendPreferredLanguage(t2->preferredLanguage());
        inputContext->sendContentType(t2->contentHints(), t2->contentPurpose());
        connect(inputContext, &KWaylandServer::InputMethodContextV1Interface::language, waylandServer(), &setLanguage);
        connect(inputContext, &KWaylandServer::InputMethodContextV1Interface::textDirection, waylandServer(), &setTextDirection);
    }

    if (t3 && t3->isEnabled()) {
        inputContext->sendSurroundingText(t3->surroundingText(), t3->surroundingTextCursorPosition(), t3->surroundingTextSelectionAnchor());
        inputContext->sendContentType(t3->contentHints(), t3->contentPurpose());
    }
478

479
480
481
482
483
484
    connect(inputContext, &KWaylandServer::InputMethodContextV1Interface::keysym, waylandServer(), &keysymReceived, Qt::UniqueConnection);
    connect(inputContext, &KWaylandServer::InputMethodContextV1Interface::commitString, waylandServer(), &commitString, Qt::UniqueConnection);
    connect(inputContext, &KWaylandServer::InputMethodContextV1Interface::deleteSurroundingText, waylandServer(), &deleteSurroundingText, Qt::UniqueConnection);
    connect(inputContext, &KWaylandServer::InputMethodContextV1Interface::cursorPosition, waylandServer(), &setCursorPosition, Qt::UniqueConnection);
    connect(inputContext, &KWaylandServer::InputMethodContextV1Interface::preeditString, this, &InputMethod::setPreeditString, Qt::UniqueConnection);
    connect(inputContext, &KWaylandServer::InputMethodContextV1Interface::preeditCursor, this, &InputMethod::setPreeditCursor, Qt::UniqueConnection);
485
486
}

487
void InputMethod::updateSni()
488
489
490
491
492
493
{
    if (!m_sni) {
        return;
    }
    if (m_enabled) {
        m_sni->setIconByName(QStringLiteral("input-keyboard-virtual-on"));
494
        m_sni->setTitle(i18n("Virtual Keyboard: enabled"));
495
496
    } else {
        m_sni->setIconByName(QStringLiteral("input-keyboard-virtual-off"));
497
        m_sni->setTitle(i18n("Virtual Keyboard: disabled"));
498
    }
499
    m_sni->setToolTipTitle(i18n("Whether to show the virtual keyboard on demand."));
500
501
}

502
void InputMethod::updateInputPanelState()
503
504
505
506
507
{
    if (!waylandServer()) {
        return;
    }

508
    auto t = waylandServer()->seat()->textInputV2();
509

510
    if (!t) {
511
512
513
        return;
    }

514
515
516
    if (m_trackedClient) {
        m_trackedClient->setVirtualKeyboardGeometry(m_inputClient ? m_inputClient->inputGeometry() : QRect());
    }
517
    t->setInputPanelState(m_inputClient && m_inputClient->isShown(false), QRect(0, 0, 0, 0));
518
519
520
}

}