Debian Codegraph @

qt6/platforminputcontext/qfcitxplatforminputcontext.h

raw
1 /*
2 * SPDX-FileCopyrightText: 2012~2017 CSSlayer <[email protected]>
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 *
6 */
7
8 #ifndef QFCITXPLATFORMINPUTCONTEXT_H
9 #define QFCITXPLATFORMINPUTCONTEXT_H
10
11 #include "fcitxcandidatewindow.h"
12 #include "fcitxqtwatcher.h"
13 #include "hybridinputcontext.h"
14 #include <QDBusConnection>
15 #include <QDBusPendingCallWatcher>
16 #include <QDBusServiceWatcher>
17 #include <QGuiApplication>
18 #include <QKeyEvent>
19 #include <QPointer>
20 #include <QRect>
21 #include <QWindow>
22 #include <memory>
23 #include <qpa/qplatforminputcontext.h>
24 #include <unordered_map>
25 #include <xkbcommon/xkbcommon-compose.h>
26
27 namespace fcitx {
28
29 class FcitxQtConnection;
30 class QFcitxPlatformInputContext;
31
32 class FcitxQtICData : public QObject {
33 public:
34 FcitxQtICData(QFcitxPlatformInputContext *context, QWindow *window);
35 FcitxQtICData(const FcitxQtICData &that) = delete;
36 ~FcitxQtICData() override;
37
38 FcitxCandidateWindow *candidateWindow(bool create);
39
40 QWindow *window() { return window_.data(); }
41
42 void resetCandidateWindow();
43
44 quint64 capability = 0;
45 HybridInputContext *proxy;
46 QRect rect;
47 // Last key event forwarded.
48 std::unique_ptr<QKeyEvent> event;
49 QString surroundingText;
50 int surroundingAnchor = -1;
51 int surroundingCursor = -1;
52 bool expectingMicroFocusChange = false;
53 bool eventFilter(QObject *watched, QEvent *event) override;
54
55 private:
56 QFcitxPlatformInputContext *context_;
57 QPointer<QWindow> window_;
58 QPointer<FcitxCandidateWindow> candidateWindow_;
59 };
60
61 class ProcessKeyWatcher : public QDBusPendingCallWatcher {
62 Q_OBJECT
63 public:
64 ProcessKeyWatcher(const QKeyEvent &event, QWindow *window,
65 const QDBusPendingCall &call, QObject *parent = nullptr)
66 : QDBusPendingCallWatcher(call, parent),
67 event_(event.type(), event.key(), event.modifiers(),
68 event.nativeScanCode(), event.nativeVirtualKey(),
69 event.nativeModifiers(), event.text(), event.isAutoRepeat(),
70 event.count()),
71 window_(window) {}
72
73 const QKeyEvent &keyEvent() { return event_; }
74
75 QWindow *window() { return window_.data(); }
76
77 private:
78 QKeyEvent event_;
79 QPointer<QWindow> window_;
80 };
81
82 struct XkbContextDeleter {
83 static inline void cleanup(struct xkb_context *pointer) {
84 if (pointer) {
85 xkb_context_unref(pointer);
86 }
87 }
88 };
89
90 struct XkbComposeTableDeleter {
91 static inline void cleanup(struct xkb_compose_table *pointer) {
92 if (pointer) {
93 xkb_compose_table_unref(pointer);
94 }
95 }
96 };
97
98 struct XkbComposeStateDeleter {
99 static inline void cleanup(struct xkb_compose_state *pointer) {
100 if (pointer) {
101 xkb_compose_state_unref(pointer);
102 }
103 }
104 };
105
106 class QFcitxPlatformInputContext : public QPlatformInputContext {
107 Q_OBJECT
108 public:
109 QFcitxPlatformInputContext();
110 ~QFcitxPlatformInputContext() override;
111
112 bool isValid() const override;
113 void setFocusObject(QObject *object) override;
114 void invokeAction(QInputMethod::Action imAction,
115 int cursorPosition) override;
116 void reset() override;
117 void commit() override;
118 void update(Qt::InputMethodQueries queries) override;
119 bool filterEvent(const QEvent *event) override;
120 QLocale locale() const override;
121 bool hasCapability(Capability capability) const override;
122 void showInputPanel() override;
123 void hideInputPanel() override;
124 bool isInputPanelVisible() const override;
125
126 auto *watcher() { return watcher_; }
127 auto *fcitx4Watcher() { return fcitx4Watcher_; }
128
129 // Use Wrapper as suffix to avoid upstream add function with same name.
130 QObject *focusObjectWrapper() const;
131 QWindow *focusWindowWrapper() const;
132 QRect cursorRectangleWrapper() const;
133
134 // Initialize theme object on demand.
135 FcitxTheme *theme();
136 bool hasPreedit() const { return !preeditList_.isEmpty(); }
137
138 public Q_SLOTS:
139 void cursorRectChanged();
140 void commitString(const QString &str);
141 void updateFormattedPreedit(const FcitxQtFormattedPreeditList &preeditList,
142 int cursorPos);
143 void deleteSurroundingText(int offset, unsigned int nchar);
144 void forwardKey(unsigned int keyval, unsigned int state, bool type);
145 void createInputContextFinished(const QByteArray &uuid);
146 void cleanUp();
147 void windowDestroyed(QObject *object);
148 void updateCurrentIM(const QString &name, const QString &uniqueName,
149 const QString &langCode);
150 void updateClientSideUI(const FcitxQtFormattedPreeditList &preedit,
151 int cursorpos,
152 const FcitxQtFormattedPreeditList &auxUp,
153 const FcitxQtFormattedPreeditList &auxDown,
154 const FcitxQtStringKeyValueList &candidates,
155 int candidateIndex, int layoutHint, bool hasPrev,
156 bool hasNext);
157 void serverSideFocusOut();
158 bool commitPreedit(QPointer<QObject> input = qApp->focusObject());
159 private Q_SLOTS:
160 void processKeyEventFinished(QDBusPendingCallWatcher *);
161
162 private:
163 bool processCompose(unsigned int keyval, unsigned int state,
164 bool isRelease);
165 QKeyEvent *createKeyEvent(unsigned int keyval, unsigned int state,
166 bool isRelease, const QKeyEvent *event);
167 void forwardEvent(QWindow *window, const QKeyEvent &event);
168
169 void addCapability(FcitxQtICData &data, quint64 capability,
170 bool forceUpdate = false) {
171 auto newcaps = data.capability | capability;
172 if (data.capability != newcaps || forceUpdate) {
173 data.capability = newcaps;
174 updateCapability(data);
175 }
176 }
177
178 void removeCapability(FcitxQtICData &data, quint64 capability,
179 bool forceUpdate = false) {
180 auto newcaps = data.capability & (~capability);
181 if (data.capability != newcaps || forceUpdate) {
182 data.capability = newcaps;
183 updateCapability(data);
184 }
185 }
186
187 void updateCapability(const FcitxQtICData &data);
188 void createICData(QWindow *w);
189 HybridInputContext *validIC() const;
190 HybridInputContext *validICByWindow(QWindow *window) const;
191 bool filterEventFallback(unsigned int keyval, unsigned int keycode,
192 unsigned int state, bool isRelease);
193
194 void updateCursorRect();
195 bool objectAcceptsInputMethod() const;
196 bool shouldDisableInputMethod() const;
197
198 void updateInputPanelVisible();
199
200 FcitxQtWatcher *watcher_;
201 Fcitx4Watcher *fcitx4Watcher_;
202 QString preedit_;
203 QString commitPreedit_;
204 FcitxQtFormattedPreeditList preeditList_;
205 int cursorPos_;
206 bool useSurroundingText_;
207 bool syncMode_;
208 std::unordered_map<QWindow *, FcitxQtICData> icMap_;
209 QPointer<QWindow> lastWindow_;
210 QPointer<QObject> lastObject_;
211 bool destroy_;
212 bool virtualKeyboardVisible_;
213 QScopedPointer<struct xkb_context, XkbContextDeleter> xkbContext_;
214 QScopedPointer<struct xkb_compose_table, XkbComposeTableDeleter>
215 xkbComposeTable_;
216 QScopedPointer<struct xkb_compose_state, XkbComposeStateDeleter>
217 xkbComposeState_;
218 QLocale locale_;
219 FcitxTheme *theme_ = nullptr;
220 bool inputPanelVisible_ = false;
221 };
222 } // namespace fcitx
223
224 #endif // QFCITXPLATFORMINPUTCONTEXT_H