File: waylandinputmethodconnection.cpp

package info (click to toggle)
maliit-framework 2.3.0-3.2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 2,488 kB
  • sloc: cpp: 13,098; ansic: 2,506; xml: 299; sh: 34; makefile: 23; sed: 4
file content (614 lines) | stat: -rw-r--r-- 20,114 bytes parent folder | download | duplicates (3)
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
/* * This file is part of Maliit framework *
 *
 * Copyright (C) 2012 Canonical Ltd
 *
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License version 2.1 as published by the Free Software Foundation
 * and appearing in the file LICENSE.LGPL included in the packaging
 * of this file.
 */

#include <cerrno> // for errno
#include <cstring> // for strerror
#include <QGuiApplication>
#include <QKeyEvent>
#include <qpa/qplatformnativeinterface.h>

#include "wayland-client.h"
#include <qwayland-input-method-unstable-v1.h>
#include <QtWaylandClient/private/qwayland-text-input-unstable-v2.h>

#include <xkbcommon/xkbcommon.h>

#include "waylandinputmethodconnection.h"

Q_LOGGING_CATEGORY(lcWaylandConnection, "maliit.connection.wayland")

namespace {

// TODO: Deduplicate it. Those values are used in
// minputcontextconnection, mimpluginmanager,
// mattributeextensionmanager and in input context implementations.
const char * const FocusStateAttribute = "focusState";
const char * const ContentTypeAttribute = "contentType";
const char * const CorrectionAttribute = "correctionEnabled";
const char * const PredictionAttribute = "predictionEnabled";
const char * const AutoCapitalizationAttribute = "autocapitalizationEnabled";
const char * const SurroundingTextAttribute = "surroundingText";
const char * const AnchorPositionAttribute = "anchorPosition";
const char * const CursorPositionAttribute = "cursorPosition";
const char * const HasSelectionAttribute = "hasSelection";
const char * const HiddenTextAttribute = "hiddenText";

typedef QPair<Qt::KeyboardModifiers, const char *> Modifier;
const Modifier modifiers[] = {
    Modifier(Qt::ShiftModifier, XKB_MOD_NAME_SHIFT),
    Modifier(Qt::ControlModifier, XKB_MOD_NAME_CTRL),
    Modifier(Qt::AltModifier, XKB_MOD_NAME_ALT),
    Modifier(Qt::MetaModifier, XKB_MOD_NAME_LOGO),
    Modifier(Qt::KeypadModifier, XKB_LED_NAME_NUM)
};

QByteArray modifiersMap()
{
    QByteArray mod_map;
    for (unsigned int iter(0); iter < (sizeof(modifiers) / sizeof(modifiers[0])); ++iter) {
        mod_map.append(modifiers[iter].second);
        mod_map.append('\0');
    }
    return mod_map;
}

xkb_mod_mask_t modifiersFromQt(const Qt::KeyboardModifiers qt_mods)
{
    xkb_mod_mask_t mod_mask(0);

    if (qt_mods == Qt::NoModifier) {
        return mod_mask;
    }

    for (unsigned int iter(0); iter < (sizeof(modifiers) / sizeof(modifiers[0])); ++iter) {
        if ((qt_mods & modifiers[iter].first) == modifiers[iter].first) {
            mod_mask |= 1 << iter;
        }
    }

    return mod_mask;
}

xkb_keysym_t keyFromQt(int qt_key)
{
    switch (qt_key) {
    case Qt::Key_Escape:
        return XKB_KEY_Escape;
    case Qt::Key_Tab:
        return XKB_KEY_Tab;
    case Qt::Key_Backspace:
        return XKB_KEY_BackSpace;
    case Qt::Key_Return:
        return XKB_KEY_Return;
    case Qt::Key_Home:
        return XKB_KEY_Home;
    case Qt::Key_End:
        return XKB_KEY_End;
    case Qt::Key_Left:
        return XKB_KEY_Left;
    case Qt::Key_Up:
        return XKB_KEY_Up;
    case Qt::Key_Right:
        return XKB_KEY_Right;
    case Qt::Key_Down:
        return XKB_KEY_Down;
    case Qt::Key_PageUp:
        return XKB_KEY_Prior;
    case Qt::Key_PageDown:
        return XKB_KEY_Next;
    default:
        if (qt_key >= Qt::Key_Space && qt_key <= Qt::Key_ydiaeresis) {
            return qt_key;
        }
        return XKB_KEY_NoSymbol;
    }
}

QtWayland::zwp_text_input_v2::preedit_style preeditStyleFromMaliit(Maliit::PreeditFace face)
{
    switch (face) {
    case Maliit::PreeditDefault:
        return QtWayland::zwp_text_input_v2::preedit_style_default;
    case Maliit::PreeditNoCandidates:
        return QtWayland::zwp_text_input_v2::preedit_style_incorrect;
    case Maliit::PreeditKeyPress:
        return QtWayland::zwp_text_input_v2::preedit_style_highlight;
    case Maliit::PreeditUnconvertible:
        return QtWayland::zwp_text_input_v2::preedit_style_inactive;
    case Maliit::PreeditActive:
        return QtWayland::zwp_text_input_v2::preedit_style_active;
    default:
        return QtWayland::zwp_text_input_v2::preedit_style_none;
    }
}

Maliit::TextContentType contentTypeFromWayland(uint32_t purpose)
{
    switch (purpose) {
    case QtWayland::zwp_text_input_v2::content_purpose_normal:
        return Maliit::FreeTextContentType;
    case QtWayland::zwp_text_input_v2::content_purpose_digits:
    case QtWayland::zwp_text_input_v2::content_purpose_number:
        return Maliit::NumberContentType;
    case QtWayland::zwp_text_input_v2::content_purpose_phone:
        return Maliit::PhoneNumberContentType;
    case QtWayland::zwp_text_input_v2::content_purpose_url:
        return Maliit::UrlContentType;
    case QtWayland::zwp_text_input_v2::content_purpose_email:
        return Maliit::EmailContentType;
    default:
        return Maliit::CustomContentType;
    }
}

bool matchesFlag(int value,
                 int flag)
{
    return ((value & flag) == flag);
}

const unsigned int wayland_connection_id(1);

} // unnamed namespace

namespace Maliit {
namespace Wayland {

class InputMethodContext;

class InputMethod : public QtWayland::zwp_input_method_v1
{
public:
    InputMethod(MInputContextConnection *connection, struct wl_registry *registry, int id);
    ~InputMethod();

    InputMethodContext *context() const;

protected:
    void zwp_input_method_v1_activate(struct ::zwp_input_method_context_v1 *id) Q_DECL_OVERRIDE;
    void zwp_input_method_v1_deactivate(struct ::zwp_input_method_context_v1 *context) Q_DECL_OVERRIDE;

private:
    MInputContextConnection *m_connection;
    QScopedPointer<InputMethodContext> m_context;
};

class InputMethodContext : public QtWayland::zwp_input_method_context_v1
{
public:
    InputMethodContext(MInputContextConnection *connection, struct ::zwp_input_method_context_v1 *object);
    ~InputMethodContext();

    QString selection() const;
    uint32_t serial() const;

protected:
    void zwp_input_method_context_v1_commit_state(uint32_t serial) Q_DECL_OVERRIDE;
    void zwp_input_method_context_v1_content_type(uint32_t hint, uint32_t purpose) Q_DECL_OVERRIDE;
    void zwp_input_method_context_v1_invoke_action(uint32_t button, uint32_t index) Q_DECL_OVERRIDE;
    void zwp_input_method_context_v1_preferred_language(const QString &language) Q_DECL_OVERRIDE;
    void zwp_input_method_context_v1_reset() Q_DECL_OVERRIDE;
    void zwp_input_method_context_v1_surrounding_text(const QString &text, uint32_t cursor, uint32_t anchor) Q_DECL_OVERRIDE;

private:
    MInputContextConnection *m_connection;
    QVariantMap m_stateInfo;
    uint32_t m_serial;
    QString m_selection;
};

}
}

struct WaylandInputMethodConnectionPrivate
{
    Q_DECLARE_PUBLIC(WaylandInputMethodConnection)

    WaylandInputMethodConnectionPrivate(WaylandInputMethodConnection *connection);
    ~WaylandInputMethodConnectionPrivate();

    void handleRegistryGlobal(uint32_t name,
                              const char *interface,
                              uint32_t version);
    void handleRegistryGlobalRemove(uint32_t name);

    Maliit::Wayland::InputMethodContext *context();

    WaylandInputMethodConnection *q_ptr;
    wl_display *display;
    wl_registry *registry;
    QScopedPointer<Maliit::Wayland::InputMethod> input_method;
};

namespace {

void registryGlobal(void *data,
                    wl_registry *registry,
                    uint32_t name,
                    const char *interface,
                    uint32_t version)
{
    WaylandInputMethodConnectionPrivate *d =
            static_cast<WaylandInputMethodConnectionPrivate *>(data);

    Q_UNUSED(registry);
    d->handleRegistryGlobal(name, interface, version);
}

void registryGlobalRemove(void *data,
                          wl_registry *registry,
                          uint32_t name)
{
    WaylandInputMethodConnectionPrivate *d =
            static_cast<WaylandInputMethodConnectionPrivate *>(data);

    Q_UNUSED(registry);
    d->handleRegistryGlobalRemove(name);
}

const wl_registry_listener maliit_registry_listener = {
    registryGlobal,
    registryGlobalRemove
};


} // unnamed namespace

WaylandInputMethodConnectionPrivate::WaylandInputMethodConnectionPrivate(WaylandInputMethodConnection *connection)
    : q_ptr(connection),
      display(0),
      registry(0),
      input_method()
{
    display = static_cast<wl_display *>(QGuiApplication::platformNativeInterface()->nativeResourceForIntegration("display"));
    if (!display) {
        qCritical() << Q_FUNC_INFO << "Failed to get a display.";
        return;
    }
    registry = wl_display_get_registry(display);
    wl_registry_add_listener(registry, &maliit_registry_listener, this);
}

WaylandInputMethodConnectionPrivate::~WaylandInputMethodConnectionPrivate()
{
    input_method.reset();
    if (registry) {
        wl_registry_destroy(registry);
    }
}

void WaylandInputMethodConnectionPrivate::handleRegistryGlobal(uint32_t name,
                                                               const char *interface,
                                                               uint32_t version)
{
    Q_UNUSED(version);
    Q_Q(WaylandInputMethodConnection);

    if (!strcmp(interface, "zwp_input_method_v1")) {
        input_method.reset(new Maliit::Wayland::InputMethod(q, registry, name));
    }
}

void WaylandInputMethodConnectionPrivate::handleRegistryGlobalRemove(uint32_t name)
{
    qCDebug(lcWaylandConnection) << Q_FUNC_INFO << name;
}

Maliit::Wayland::InputMethodContext *WaylandInputMethodConnectionPrivate::context()
{
    return input_method ? input_method->context() : 0;
}

// MInputContextWestonIMProtocolConnection

WaylandInputMethodConnection::WaylandInputMethodConnection()
    : d_ptr(new WaylandInputMethodConnectionPrivate(this))
{
}

WaylandInputMethodConnection::~WaylandInputMethodConnection()
{
}

void WaylandInputMethodConnection::sendPreeditString(const QString &string,
                                                     const QList<Maliit::PreeditTextFormat> &preedit_formats,
                                                     int replace_start,
                                                     int replace_length,
                                                     int cursor_pos)
{
    Q_D(WaylandInputMethodConnection);

    qCDebug(lcWaylandConnection) << Q_FUNC_INFO << string << replace_start << replace_length << cursor_pos;

    if (!d->context())
        return;

    MInputContextConnection::sendPreeditString(string, preedit_formats,
                                               replace_start, replace_length,
                                               cursor_pos);

    if (replace_length > 0) {
        int cursor = widgetState().value(CursorPositionAttribute).toInt();
        uint32_t index = string.midRef(qMin(cursor + replace_start, cursor), qAbs(replace_start)).toUtf8().size();
        uint32_t length = string.midRef(cursor + replace_start, replace_length).toUtf8().size();
        d->context()->delete_surrounding_text(index, length);
    }

    Q_FOREACH (const Maliit::PreeditTextFormat& format, preedit_formats) {
        QtWayland::zwp_text_input_v2::preedit_style style = preeditStyleFromMaliit(format.preeditFace);
        uint32_t index = string.leftRef(format.start).toUtf8().size();
        uint32_t length = string.leftRef(format.start + format.length).toUtf8().size() - index;
        qCDebug(lcWaylandConnection) << Q_FUNC_INFO << "preedit_styling" << index << length;
        d->context()->preedit_styling(index, length, style);
    }

    // TODO check if defined like that/required
    if (cursor_pos < 0) {
        cursor_pos = string.size() + 1 - cursor_pos;
    }

    qCDebug(lcWaylandConnection) << Q_FUNC_INFO << "preedit_cursor" << string.leftRef(cursor_pos).toUtf8().size();
    d->context()->preedit_cursor(string.leftRef(cursor_pos).toUtf8().size());
    qCDebug(lcWaylandConnection) << Q_FUNC_INFO << "preedit_string" << string;
    d->context()->preedit_string(d->context()->serial(), string, string);
}


void WaylandInputMethodConnection::sendCommitString(const QString &string,
                                                    int replace_start,
                                                    int replace_length,
                                                    int cursor_pos)
{
    Q_D(WaylandInputMethodConnection);

    qCDebug(lcWaylandConnection) << Q_FUNC_INFO << string << replace_start << replace_length << cursor_pos;

    if (!d->context())
        return;

    MInputContextConnection::sendCommitString(string, replace_start, replace_length, cursor_pos);

    if (cursor_pos != 0) {
        qCWarning(lcWaylandConnection) << Q_FUNC_INFO << "cursor_pos:" << cursor_pos << "!= 0 not supported yet";
        cursor_pos = 0;
    }

    if (replace_length > 0) {
        int cursor = widgetState().value(CursorPositionAttribute).toInt();
        uint32_t index = string.midRef(qMin(cursor + replace_start, cursor), qAbs(replace_start)).toUtf8().size();
        uint32_t length = string.midRef(cursor + replace_start, replace_length).toUtf8().size();
        d->context()->delete_surrounding_text(index, length);
    }

    cursor_pos = string.leftRef(cursor_pos).toUtf8().size();
    d->context()->cursor_position(cursor_pos, cursor_pos);
    d->context()->commit_string(d->context()->serial(), string);
}

void WaylandInputMethodConnection::sendKeyEvent(const QKeyEvent &keyEvent,
                                                Maliit::EventRequestType requestType)
{
    Q_D(WaylandInputMethodConnection);

    qCDebug(lcWaylandConnection) << Q_FUNC_INFO;

    if (!d->context())
        return;

    xkb_keysym_t sym(keyFromQt(keyEvent.key()));

    if (sym == XKB_KEY_NoSymbol) {
        qCWarning(lcWaylandConnection) << "No conversion from Qt::Key:" << keyEvent.key() << "to XKB key. Update the keyFromQt() function.";
        return;
    }

    wl_keyboard_key_state state;

    switch (keyEvent.type()) {
    case QEvent::KeyPress:
        state = WL_KEYBOARD_KEY_STATE_PRESSED;
        break;

    case QEvent::KeyRelease:
        state = WL_KEYBOARD_KEY_STATE_RELEASED;
        break;

    default:
        qCWarning(lcWaylandConnection) << "Unknown QKeyEvent type:" << keyEvent.type();
        return;
    }

    xkb_mod_mask_t modifiers(modifiersFromQt(keyEvent.modifiers()));

    MInputContextConnection::sendKeyEvent(keyEvent, requestType);

    d->context()->keysym(d->context()->serial(),
                         keyEvent.timestamp(),
                         sym, state, modifiers);
}

QString WaylandInputMethodConnection::selection(bool &valid)
{
    Q_D(WaylandInputMethodConnection);

    qCDebug(lcWaylandConnection) << Q_FUNC_INFO;

    Maliit::Wayland::InputMethodContext *context = d->input_method->context();

    valid = context && !context->selection().isEmpty();
    return context ? context->selection() : QString();
}

void WaylandInputMethodConnection::setLanguage(const QString &language)
{
    Q_D(WaylandInputMethodConnection);

    qCDebug(lcWaylandConnection) << Q_FUNC_INFO;

    if (!d->context())
        return;

    d->context()->language(d->context()->serial(), language);
}

void WaylandInputMethodConnection::setSelection(int start, int length)
{
    Q_D (WaylandInputMethodConnection);

    qCDebug(lcWaylandConnection) << Q_FUNC_INFO;

    if (!d->context())
        return;

    QString surrounding = widgetState().value(SurroundingTextAttribute).toString();
    uint32_t index(surrounding.leftRef(start + length).toUtf8().size());
    uint32_t anchor(surrounding.leftRef(start).toUtf8().size());

    d->context()->cursor_position(index, anchor);
    d->context()->commit_string(d->context()->serial(), QString());
}

namespace Maliit {
namespace Wayland {

InputMethod::InputMethod(MInputContextConnection *connection, struct wl_registry *registry, int id)
    : QtWayland::zwp_input_method_v1(registry, id, 1)
    , m_connection(connection)
    , m_context()
{
    qCDebug(lcWaylandConnection) << Q_FUNC_INFO;
}

InputMethod::~InputMethod()
{
}

InputMethodContext *InputMethod::context() const
{
    return m_context.data();
}

void InputMethod::zwp_input_method_v1_activate(struct ::zwp_input_method_context_v1 *id)
{
    qCDebug(lcWaylandConnection) << Q_FUNC_INFO;

    m_context.reset(new InputMethodContext(m_connection, id));

    m_context->modifiers_map(modifiersMap());

}

void InputMethod::zwp_input_method_v1_deactivate(struct zwp_input_method_context_v1 *)
{
    qCDebug(lcWaylandConnection) << Q_FUNC_INFO;

    m_context.reset();

    m_connection->handleDisconnection(wayland_connection_id);
}

InputMethodContext::InputMethodContext(MInputContextConnection *connection, struct ::zwp_input_method_context_v1 *object)
    : QtWayland::zwp_input_method_context_v1(object)
    , m_connection(connection)
    , m_stateInfo()
    , m_serial(0)
    , m_selection()
{
    qCDebug(lcWaylandConnection) << Q_FUNC_INFO;

    m_stateInfo[FocusStateAttribute] = true;
    m_connection->activateContext(wayland_connection_id);
    m_connection->showInputMethod(wayland_connection_id);
}

InputMethodContext::~InputMethodContext()
{
    qCDebug(lcWaylandConnection) << Q_FUNC_INFO;

    m_stateInfo.clear();
    m_stateInfo[FocusStateAttribute] = false;
    m_connection->updateWidgetInformation(wayland_connection_id, m_stateInfo, true);
    m_connection->hideInputMethod(wayland_connection_id);
}

QString InputMethodContext::selection() const
{
    return m_selection;
}

uint32_t InputMethodContext::serial() const
{
    return m_serial;
}

void InputMethodContext::zwp_input_method_context_v1_commit_state(uint32_t serial)
{
    qCDebug(lcWaylandConnection) << Q_FUNC_INFO;

    m_serial = serial;
    m_connection->updateWidgetInformation(wayland_connection_id, m_stateInfo, false);
}

void InputMethodContext::zwp_input_method_context_v1_content_type(uint32_t hint, uint32_t purpose)
{
    qCDebug(lcWaylandConnection) << Q_FUNC_INFO;

    m_stateInfo[ContentTypeAttribute] = contentTypeFromWayland(purpose);
    m_stateInfo[AutoCapitalizationAttribute] = matchesFlag(hint, QtWayland::zwp_text_input_v2::content_hint_auto_capitalization);
    m_stateInfo[CorrectionAttribute] = matchesFlag(hint, QtWayland::zwp_text_input_v2::content_hint_auto_correction);
    m_stateInfo[PredictionAttribute] = matchesFlag(hint, QtWayland::zwp_text_input_v2::content_hint_auto_completion);
    m_stateInfo[HiddenTextAttribute] = matchesFlag(hint, QtWayland::zwp_text_input_v2::content_hint_hidden_text);
}

void InputMethodContext::zwp_input_method_context_v1_invoke_action(uint32_t button, uint32_t index)
{
    qCDebug(lcWaylandConnection) << Q_FUNC_INFO << button << index;
}

void InputMethodContext::zwp_input_method_context_v1_preferred_language(const QString &language)
{
    qCDebug(lcWaylandConnection) << Q_FUNC_INFO << language;
}

void InputMethodContext::zwp_input_method_context_v1_reset()
{
    qCDebug(lcWaylandConnection) << Q_FUNC_INFO;

    m_connection->reset(wayland_connection_id);
    m_connection->showInputMethod(wayland_connection_id);
}

void InputMethodContext::zwp_input_method_context_v1_surrounding_text(const QString &text, uint32_t cursor, uint32_t anchor)
{
    qCDebug(lcWaylandConnection) << Q_FUNC_INFO;

    // Re-show the keyboard if it was hidden without changing focus.
    m_connection->showInputMethod(wayland_connection_id);

    const QByteArray &utf8_text(text.toUtf8());

    m_stateInfo[SurroundingTextAttribute] = text;
    m_stateInfo[CursorPositionAttribute] = QString::fromUtf8(utf8_text.constData(), cursor).size();
    m_stateInfo[AnchorPositionAttribute] = QString::fromUtf8(utf8_text.constData(), anchor).size();
    if (cursor == anchor) {
        m_stateInfo[HasSelectionAttribute] = false;
        m_selection.clear();
    } else {
        m_stateInfo[HasSelectionAttribute] = true;
        uint32_t begin = qMin(anchor, cursor);
        uint32_t end = qMax(anchor, cursor);
        m_selection = QString::fromUtf8(utf8_text.constData() + begin, end - begin);
    }
}

}
}