Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qcomposeplatforminputcontext.cpp
Go to the documentation of this file.
1// Copyright (C) 2019 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
6
7#include <QtCore/QCoreApplication>
8#include <QtCore/qvarlengtharray.h>
9#include <QtGui/QKeyEvent>
10#include <QtGui/QGuiApplication>
11
12#include <locale.h>
13
15
16Q_LOGGING_CATEGORY(lcXkbCompose, "qt.xkb.compose")
17
18QComposeInputContext::QComposeInputContext()
19{
20 setObjectName(QStringLiteral("QComposeInputContext"));
21 qCDebug(lcXkbCompose, "using xkb compose input context");
22}
23
25{
26 xkb_compose_state_unref(m_composeState);
27 xkb_compose_table_unref(m_composeTable);
28}
29
31{
32 if (m_initialized)
33 return;
34
35 if (!m_XkbContext) {
36 qCWarning(lcXkbCompose) << "error: xkb context has not been set on" << metaObject()->className();
37 return;
38 }
39
40 m_initialized = true;
41 // Get locale from user env settings, see also
42 // https://xkbcommon.org/doc/current/group__compose.html#compose-locale
43 const char *locale = getenv("LC_ALL");
44 if (!locale || !*locale)
45 locale = getenv("LC_CTYPE");
46 if (!locale || !*locale)
47 locale = getenv("LANG");
48 if (!locale || !*locale)
49 locale = "C";
50 qCDebug(lcXkbCompose) << "detected locale:" << locale;
51
52 m_composeTable = xkb_compose_table_new_from_locale(m_XkbContext, locale, XKB_COMPOSE_COMPILE_NO_FLAGS);
53 if (m_composeTable)
54 m_composeState = xkb_compose_state_new(m_composeTable, XKB_COMPOSE_STATE_NO_FLAGS);
55
56 if (!m_composeTable) {
57 qCWarning(lcXkbCompose, "failed to create compose table");
58 return;
59 }
60 if (!m_composeState) {
61 qCWarning(lcXkbCompose, "failed to create compose state");
62 return;
63 }
64}
65
66bool QComposeInputContext::filterEvent(const QEvent *event)
67{
68 auto keyEvent = static_cast<const QKeyEvent *>(event);
69 if (keyEvent->type() != QEvent::KeyPress)
70 return false;
71
72 if (!inputMethodAccepted())
73 return false;
74
75 // lazy initialization - we don't want to do this on an app startup
77
78 if (!m_composeTable || !m_composeState)
79 return false;
80
81 xkb_compose_state_feed(m_composeState, keyEvent->nativeVirtualKey());
82
83 switch (xkb_compose_state_get_status(m_composeState)) {
84 case XKB_COMPOSE_COMPOSING:
85 return true;
86 case XKB_COMPOSE_CANCELLED:
87 reset();
88 return false;
89 case XKB_COMPOSE_COMPOSED:
90 {
91 const int size = xkb_compose_state_get_utf8(m_composeState, nullptr, 0);
92 QVarLengthArray<char, 32> buffer(size + 1);
93 xkb_compose_state_get_utf8(m_composeState, buffer.data(), buffer.size());
94 QString composedText = QString::fromUtf8(buffer.constData());
95
96 QInputMethodEvent event;
97 event.setCommitString(composedText);
98
99 if (!m_focusObject && qApp)
100 m_focusObject = qApp->focusObject();
101
102 if (m_focusObject)
103 QCoreApplication::sendEvent(m_focusObject, &event);
104 else
105 qCWarning(lcXkbCompose, "no focus object");
106
107 reset();
108 return true;
109 }
110 case XKB_COMPOSE_NOTHING:
111 return false;
112 default:
113 Q_UNREACHABLE_RETURN(false);
114 }
115}
116
118{
119 return true;
120}
121
123{
124 m_focusObject = object;
125}
126
128{
129 if (m_composeState)
130 xkb_compose_state_reset(m_composeState);
131}
132
133void QComposeInputContext::update(Qt::InputMethodQueries q)
134{
135 QPlatformInputContext::update(q);
136}
137
138QT_END_NAMESPACE
139
140#include "moc_qcomposeplatforminputcontext.cpp"
void reset() override
Method to be called when input method needs to be reset.
bool filterEvent(const QEvent *event) override
This function can be reimplemented to filter input events.
void setFocusObject(QObject *object) override
This virtual method gets called to notify updated focus to object.
bool isValid() const override
Returns input context validity.
void update(Qt::InputMethodQueries) override
Notification on editor updates.
Q_LOGGING_CATEGORY(lcEventDispatcher, "qt.eventdispatcher")