Debian Codegraph @

qt4/inputcontext/qfcitxinputcontext.h

raw
1 /*
2 * SPDX-FileCopyrightText: 2012~2017 CSSlayer <[email protected]>
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 *
6 */
7
8 #ifndef QFCITXINPUTCONTEXT_H
9 #define QFCITXINPUTCONTEXT_H
10
11 #include "fcitxqtinputcontextproxy.h"
12 #include "fcitxqtwatcher.h"
13 #include <QApplication>
14 #include <QDBusConnection>
15 #include <QDBusServiceWatcher>
16 #include <QInputContext>
17 #include <QKeyEvent>
18 #include <QPointer>
19 #include <QRect>
20 #include <QWidget>
21 #include <memory>
22 #include <unordered_map>
23 #include <xkbcommon/xkbcommon-compose.h>
24
25 namespace fcitx {
26
27 class FcitxQtConnection;
28
29 struct FcitxQtICData {
30 FcitxQtICData(FcitxQtWatcher *watcher)
31 : proxy(new FcitxQtInputContextProxy(watcher, watcher)) {}
32 FcitxQtICData(const FcitxQtICData &that) = delete;
33 ~FcitxQtICData() { delete proxy; }
34 quint64 capability = 0;
35 FcitxQtInputContextProxy *proxy;
36 QRect rect;
37 // Last key event forwarded.
38 std::unique_ptr<QKeyEvent> event;
39 QString surroundingText;
40 int surroundingAnchor = -1;
41 int surroundingCursor = -1;
42 };
43
44 class ProcessKeyWatcher : public QDBusPendingCallWatcher {
45 Q_OBJECT
46 public:
47 ProcessKeyWatcher(const QKeyEvent &event, QWidget *window,
48 const QDBusPendingCall &call, QObject *parent = 0)
49 : QDBusPendingCallWatcher(call, parent),
50 event_(QKeyEvent::createExtendedKeyEvent(
51 event.type(), event.key(), event.modifiers(),
52 event.nativeScanCode(), event.nativeVirtualKey(),
53 event.nativeModifiers(), event.text(), event.isAutoRepeat(),
54 event.count())),
55 window_(window) {}
56
57 virtual ~ProcessKeyWatcher() { delete event_; }
58
59 QKeyEvent &keyEvent() { return *event_; }
60
61 QWidget *window() { return window_.data(); }
62
63 private:
64 QKeyEvent *event_;
65 QPointer<QWidget> window_;
66 };
67
68 struct XkbContextDeleter {
69 static inline void cleanup(struct xkb_context *pointer) {
70 if (pointer)
71 xkb_context_unref(pointer);
72 }
73 };
74
75 struct XkbComposeTableDeleter {
76 static inline void cleanup(struct xkb_compose_table *pointer) {
77 if (pointer)
78 xkb_compose_table_unref(pointer);
79 }
80 };
81
82 struct XkbComposeStateDeleter {
83 static inline void cleanup(struct xkb_compose_state *pointer) {
84 if (pointer)
85 xkb_compose_state_unref(pointer);
86 }
87 };
88
89 class FcitxQtInputMethodProxy;
90
91 class QFcitxInputContext : public QInputContext {
92 Q_OBJECT
93 public:
94 QFcitxInputContext();
95 virtual ~QFcitxInputContext();
96
97 QString identifierName() override;
98 QString language() override;
99 void reset() override;
100 bool isComposing() const override { return false; };
101 void update() override;
102 void setFocusWidget(QWidget *w) override;
103
104 void widgetDestroyed(QWidget *w) override;
105
106 bool filterEvent(const QEvent *event) override;
107 void mouseHandler(int x, QMouseEvent *event) override;
108
109 public Q_SLOTS:
110 void cursorRectChanged();
111 void commitString(const QString &str);
112 void updateFormattedPreedit(const FcitxQtFormattedPreeditList &preeditList,
113 int cursorPos);
114 void deleteSurroundingText(int offset, unsigned int nchar);
115 void forwardKey(unsigned int keyval, unsigned int state, bool type);
116 void createInputContextFinished(const QByteArray &uuid);
117 void serverSideFocusOut();
118 void cleanUp();
119 void windowDestroyed(QObject *object);
120
121 private:
122 bool processCompose(unsigned int keyval, unsigned int state,
123 bool isRelaese);
124 QKeyEvent *createKeyEvent(unsigned int keyval, unsigned int state,
125 bool isRelaese, const QKeyEvent *event);
126
127 void addCapability(FcitxQtICData &data, quint64 capability,
128 bool forceUpdate = false) {
129 auto newcaps = data.capability | capability;
130 if (data.capability != newcaps || forceUpdate) {
131 data.capability = newcaps;
132 updateCapability(data);
133 }
134 }
135
136 void removeCapability(FcitxQtICData &data, quint64 capability,
137 bool forceUpdate = false) {
138 auto newcaps = data.capability & (~capability);
139 if (data.capability != newcaps || forceUpdate) {
140 data.capability = newcaps;
141 updateCapability(data);
142 }
143 }
144
145 void updateCapability(const FcitxQtICData &data);
146 void commitPreedit(QPointer<QWidget> input = qApp->focusWidget());
147 void createICData(QWidget *w);
148 FcitxQtInputContextProxy *validIC();
149 FcitxQtInputContextProxy *validICByWindow(QWidget *window);
150 bool filterEventFallback(unsigned int keyval, unsigned int keycode,
151 unsigned int state, bool isRelaese);
152
153 FcitxQtWatcher *watcher_;
154 QString preedit_;
155 QString commitPreedit_;
156 FcitxQtFormattedPreeditList preeditList_;
157 int cursorPos_;
158 bool useSurroundingText_;
159 bool syncMode_;
160 std::unordered_map<QWidget *, FcitxQtICData> icMap_;
161 QPointer<QWidget> lastWindow_;
162 bool destroy_;
163 QScopedPointer<struct xkb_context, XkbContextDeleter> xkbContext_;
164 QScopedPointer<struct xkb_compose_table, XkbComposeTableDeleter>
165 xkbComposeTable_;
166 QScopedPointer<struct xkb_compose_state, XkbComposeStateDeleter>
167 xkbComposeState_;
168 private Q_SLOTS:
169 void processKeyEventFinished(QDBusPendingCallWatcher *);
170 };
171 } // namespace fcitx
172
173 #endif // QFCITXINPUTCONTEXT_H