summaryrefslogtreecommitdiffstats
path: root/src/plugins/platforminputcontexts/compose/qcomposeplatforminputcontext.cpp
blob: 3e7418907687b6ce08b12cff38e742376c1956aa (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
// Copyright (C) 2019 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
#include "qcomposeplatforminputcontext.h"

#include <QtCore/QCoreApplication>
#include <QtCore/qvarlengtharray.h>
#include <QtGui/QKeyEvent>
#include <QtGui/QGuiApplication>

#include <locale.h>

QT_BEGIN_NAMESPACE

Q_LOGGING_CATEGORY(lcXkbCompose, "qt.xkb.compose")

QComposeInputContext::QComposeInputContext()
{
    setObjectName(QStringLiteral("QComposeInputContext"));
    qCDebug(lcXkbCompose, "using xkb compose input context");
}

QComposeInputContext::~QComposeInputContext()
{
    xkb_compose_state_unref(m_composeState);
    xkb_compose_table_unref(m_composeTable);
}

void QComposeInputContext::ensureInitialized()
{
    if (m_initialized)
        return;

    if (!m_XkbContext) {
        qCWarning(lcXkbCompose) << "error: xkb context has not been set on" << metaObject()->className();
        return;
    }

    m_initialized = true;
    // Get locale from user env settings, see also
    // https://xkbcommon.org/doc/current/group__compose.html#compose-locale
    const char *locale = getenv("LC_ALL");
    if (!locale || !*locale)
        locale = getenv("LC_CTYPE");
    if (!locale || !*locale)
        locale = getenv("LANG");
    if (!locale || !*locale)
         locale = "C";
    qCDebug(lcXkbCompose) << "detected locale:" << locale;

    m_composeTable = xkb_compose_table_new_from_locale(m_XkbContext, locale, XKB_COMPOSE_COMPILE_NO_FLAGS);
    if (m_composeTable)
        m_composeState = xkb_compose_state_new(m_composeTable, XKB_COMPOSE_STATE_NO_FLAGS);

    if (!m_composeTable) {
        qCWarning(lcXkbCompose, "failed to create compose table");
        return;
    }
    if (!m_composeState) {
        qCWarning(lcXkbCompose, "failed to create compose state");
        return;
    }
}

bool QComposeInputContext::filterEvent(const QEvent *event)
{
    auto keyEvent = static_cast<const QKeyEvent *>(event);
    if (keyEvent->type() != QEvent::KeyPress)
        return false;

    if (!inputMethodAccepted())
        return false;

    // lazy initialization - we don't want to do this on an app startup
    ensureInitialized();

    if (!m_composeTable || !m_composeState)
        return false;

    xkb_compose_state_feed(m_composeState, keyEvent->nativeVirtualKey());

    switch (xkb_compose_state_get_status(m_composeState)) {
    case XKB_COMPOSE_COMPOSING:
        return true;
    case XKB_COMPOSE_CANCELLED:
        reset();
        return false;
    case XKB_COMPOSE_COMPOSED:
    {
        const int size = xkb_compose_state_get_utf8(m_composeState, nullptr, 0);
        QVarLengthArray<char, 32> buffer(size + 1);
        xkb_compose_state_get_utf8(m_composeState, buffer.data(), buffer.size());
        QString composedText = QString::fromUtf8(buffer.constData());

        QInputMethodEvent event;
        event.setCommitString(composedText);

        if (!m_focusObject && qApp)
            m_focusObject = qApp->focusObject();

        if (m_focusObject)
            QCoreApplication::sendEvent(m_focusObject, &event);
        else
            qCWarning(lcXkbCompose, "no focus object");

        reset();
        return true;
    }
    case XKB_COMPOSE_NOTHING:
        return false;
    default:
        Q_UNREACHABLE_RETURN(false);
    }
}

bool QComposeInputContext::isValid() const
{
    return true;
}

void QComposeInputContext::setFocusObject(QObject *object)
{
    m_focusObject = object;
}

void QComposeInputContext::reset()
{
    if (m_composeState)
        xkb_compose_state_reset(m_composeState);
}

void QComposeInputContext::update(Qt::InputMethodQueries q)
{
    QPlatformInputContext::update(q);
}

QT_END_NAMESPACE

#include "moc_qcomposeplatforminputcontext.cpp"