1 /****************************************************************************
2 **
3 ** Copyright (C) 2019 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the plugins of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or (at your option) the GNU General
28 ** Public license version 3 or any later version approved by the KDE Free
29 ** Qt Foundation. The licenses are as published by the Free Software
30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31 ** included in the packaging of this file. Please review the following
32 ** information to ensure the GNU General Public License requirements will
33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34 ** https://www.gnu.org/licenses/gpl-3.0.html.
35 **
36 ** $QT_END_LICENSE$
37 **
38 ****************************************************************************/
39 #include "qcomposeplatforminputcontext.h"
40 
41 #include <QtCore/QCoreApplication>
42 #include <QtGui/QKeyEvent>
43 #include <QtGui/QGuiApplication>
44 
45 #include <locale.h>
46 
47 QT_BEGIN_NAMESPACE
48 
49 Q_LOGGING_CATEGORY(lcXkbCompose, "qt.xkb.compose")
50 
QComposeInputContext()51 QComposeInputContext::QComposeInputContext()
52 {
53     setObjectName(QStringLiteral("QComposeInputContext"));
54     qCDebug(lcXkbCompose, "using xkb compose input context");
55 }
56 
~QComposeInputContext()57 QComposeInputContext::~QComposeInputContext()
58 {
59     xkb_compose_state_unref(m_composeState);
60     xkb_compose_table_unref(m_composeTable);
61 }
62 
ensureInitialized()63 void QComposeInputContext::ensureInitialized()
64 {
65     if (m_initialized)
66         return;
67 
68     if (!m_XkbContext) {
69         qCWarning(lcXkbCompose) << "error: xkb context has not been set on" << metaObject()->className();
70         return;
71     }
72 
73     m_initialized = true;
74     const char *locale = setlocale(LC_CTYPE, "");
75     if (!locale)
76         locale = setlocale(LC_CTYPE, nullptr);
77     qCDebug(lcXkbCompose) << "detected locale (LC_CTYPE):" << locale;
78 
79     m_composeTable = xkb_compose_table_new_from_locale(m_XkbContext, locale, XKB_COMPOSE_COMPILE_NO_FLAGS);
80     if (m_composeTable)
81         m_composeState = xkb_compose_state_new(m_composeTable, XKB_COMPOSE_STATE_NO_FLAGS);
82 
83     if (!m_composeTable) {
84         qCWarning(lcXkbCompose, "failed to create compose table");
85         return;
86     }
87     if (!m_composeState) {
88         qCWarning(lcXkbCompose, "failed to create compose state");
89         return;
90     }
91 }
92 
filterEvent(const QEvent * event)93 bool QComposeInputContext::filterEvent(const QEvent *event)
94 {
95     auto keyEvent = static_cast<const QKeyEvent *>(event);
96     if (keyEvent->type() != QEvent::KeyPress)
97         return false;
98 
99     if (!inputMethodAccepted())
100         return false;
101 
102     // lazy initialization - we don't want to do this on an app startup
103     ensureInitialized();
104 
105     if (!m_composeTable || !m_composeState)
106         return false;
107 
108     xkb_compose_state_feed(m_composeState, keyEvent->nativeVirtualKey());
109 
110     switch (xkb_compose_state_get_status(m_composeState)) {
111     case XKB_COMPOSE_COMPOSING:
112         return true;
113     case XKB_COMPOSE_CANCELLED:
114         reset();
115         return false;
116     case XKB_COMPOSE_COMPOSED:
117     {
118         const int size = xkb_compose_state_get_utf8(m_composeState, nullptr, 0);
119         QVarLengthArray<char, 32> buffer(size + 1);
120         xkb_compose_state_get_utf8(m_composeState, buffer.data(), buffer.size());
121         QString composedText = QString::fromUtf8(buffer.constData());
122 
123         QInputMethodEvent event;
124         event.setCommitString(composedText);
125 
126         if (!m_focusObject && qApp)
127             m_focusObject = qApp->focusObject();
128 
129         if (m_focusObject)
130             QCoreApplication::sendEvent(m_focusObject, &event);
131         else
132             qCWarning(lcXkbCompose, "no focus object");
133 
134         reset();
135         return true;
136     }
137     case XKB_COMPOSE_NOTHING:
138         return false;
139     default:
140         Q_UNREACHABLE();
141         return false;
142     }
143 }
144 
isValid() const145 bool QComposeInputContext::isValid() const
146 {
147     return true;
148 }
149 
setFocusObject(QObject * object)150 void QComposeInputContext::setFocusObject(QObject *object)
151 {
152     m_focusObject = object;
153 }
154 
reset()155 void QComposeInputContext::reset()
156 {
157     if (m_composeState)
158         xkb_compose_state_reset(m_composeState);
159 }
160 
update(Qt::InputMethodQueries q)161 void QComposeInputContext::update(Qt::InputMethodQueries q)
162 {
163     QPlatformInputContext::update(q);
164 }
165 
166 QT_END_NAMESPACE
167