Debian Codegraph @

src/qt6-im/hime-imcontext-qt.cpp

raw
1 /*
2 * Copyright (C) 2022 The HIME team, Taiwan
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 2 of the License.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16 */
17
18 #include <errno.h>
19 #include <signal.h>
20
21 #include <unistd.h>
22
23 #include <QtGui/QGuiApplication>
24 #include <QtGui/QInputMethod>
25 #include <QtGui/QKeyEvent>
26 #include <QtGui/QPalette>
27 #include <QtGui/QTextCharFormat>
28 #include <QtGui/QWindow>
29
30 // confliction of qt & x11
31 typedef unsigned long KeySym;
32 typedef struct _XDisplay Display;
33 typedef unsigned long Window;
34 typedef struct {
35 short x, y;
36 } XPoint;
37
38 #include "../util.h"
39 #include "hime-im-client.h"
40 #include "hime-imcontext-qt.h"
41
42 static WId focused_win;
43
44 #include <QtGui/qpa/qplatformnativeinterface.h>
45
46 #if DEBUG
47 FILE *out_fp;
48 void __hime_dbg_ (const char *fmt, ...) {
49 va_list args;
50
51 if (!out_fp) {
52 #if 0
53 out_fp = fopen("/tmp/a.txt", "w");
54 #else
55 out_fp = stdout;
56 #endif
57 }
58
59 va_start (args, fmt);
60 vfprintf (out_fp, fmt, args);
61 fflush (out_fp);
62 va_end (args);
63 }
64 #endif
65
66 QHimePlatformInputContext::QHimePlatformInputContext () {
67 dbg ("QHimePlatformInputContext::QHimePlatformInputContext() \n");
68 QPlatformNativeInterface *native = QGuiApplication::platformNativeInterface ();
69 if (!native)
70 return;
71 Display *display = static_cast<Display *> (native->nativeResourceForWindow ("display", NULL));
72
73 if (!(hime_ch = hime_im_client_open (display))) {
74 perror ("cannot open hime_ch");
75 dbg ("hime_im_client_open error\n");
76 return;
77 }
78
79 dbg ("QHimePlatformInputContext succ\n");
80 }
81
82 QHimePlatformInputContext::~QHimePlatformInputContext () {
83 if (hime_ch == NULL)
84 return;
85 hime_im_client_close (hime_ch);
86 hime_ch = NULL;
87 }
88
89 bool QHimePlatformInputContext::isValid () const {
90 dbg ("QHimePlatformInputContext::isValid()\n");
91 return true;
92 }
93
94 void QHimePlatformInputContext::invokeAction (QInputMethod::Action action, int cursorPosition) {
95 dbg ("QHimePlatformInputContext::invokeAction(n");
96 // FIXME nop? remove me
97 }
98
99 void QHimePlatformInputContext::commitPreedit () {
100 dbg ("QHimePlatformInputContext::commitPreedit\n");
101 // use this to flush
102 int preedit_cursor_position = 0;
103 int sub_comp_len;
104 char *str = NULL;
105 HIME_PREEDIT_ATTR att[HIME_PREEDIT_ATTR_MAX_N];
106 hime_im_client_get_preedit (hime_ch, &str, att, &preedit_cursor_position, &sub_comp_len);
107 if (str) {
108 if (strlen (str) > 0) {
109 dbg ("send enter to flush\n");
110 send_key_press (0xff0d, 0); // Enter
111 } else {
112 dbg ("empty string\n");
113 }
114
115 free (str);
116 update_preedit ();
117 } else {
118 dbg ("no str\n");
119 }
120 }
121
122 void QHimePlatformInputContext::reset () {
123 dbg ("QHimePlatformInputContext::reset()\n");
124 if (hime_ch) {
125 hime_im_client_reset (hime_ch);
126 update_preedit ();
127 }
128 }
129
130 void QHimePlatformInputContext::update (Qt::InputMethodQueries queries) {
131 dbg ("QHimePlatformInputContext::update\n");
132 QObject *input = qApp->focusObject ();
133 if (!input)
134 return;
135
136 QInputMethodQueryEvent query (queries);
137 QGuiApplication::sendEvent (input, &query);
138
139 if (queries & Qt::ImCursorRectangle) {
140 cursorMoved ();
141 }
142 }
143
144 // this one is essential
145 void QHimePlatformInputContext::commit () {
146 dbg ("QHimePlatformInputContext::commit()\n");
147 commitPreedit ();
148 }
149
150 void QHimePlatformInputContext::setFocusObject (QObject *object) {
151 dbg ("QHimePlatformInputContext::setFocusObject\n");
152 QWindow *window = qApp->focusWindow ();
153 if (!window) {
154 dbg ("no window, focus out\n");
155 focused_win = 0;
156 char *rstr = NULL;
157 hime_im_client_focus_out2 (hime_ch, &rstr);
158 if (rstr) {
159 send_str (rstr);
160 } else {
161 dbg ("no str in preedit\n");
162 }
163 return;
164 }
165
166 WId win = window->winId ();
167
168 if (focused_win && win != focused_win) {
169 if (hime_ch) {
170 hime_im_client_focus_out (hime_ch);
171 }
172 }
173
174 focused_win = win;
175
176 if (hime_ch) {
177 hime_im_client_set_client_window (hime_ch, win);
178 hime_im_client_focus_in (hime_ch);
179 cursorMoved ();
180 }
181 }
182
183 static int last_x = -1, last_y = -1;
184
185 void QHimePlatformInputContext::cursorMoved () {
186 dbg (" QHimePlatformInputContext::cursorMoved()\n");
187
188 QWindow *inputWindow = qApp->focusWindow ();
189 if (!inputWindow)
190 return;
191
192 QRect r = qApp->inputMethod ()->cursorRectangle ().toRect ();
193 if (!r.isValid ())
194 return;
195
196 // hime server will clear the string if the cursor is moved, make sure the x,y is valid
197 int x = r.left (), y = r.bottom ();
198 if (x > inputWindow->width () || y > inputWindow->height () || x < 0 || y < 0)
199 return;
200
201 if (hime_ch && (x != last_x || y != last_y)) {
202 last_x = x;
203 last_y = y;
204 dbg ("move cursor %d, %d\n", x, y);
205 hime_im_client_set_cursor_location (hime_ch, x, y);
206 }
207 }
208
209 void QHimePlatformInputContext::update_preedit () {
210 if (!hime_ch)
211 return;
212 QList<QInputMethodEvent::Attribute> attrList;
213 // QString preedit_string;
214 int preedit_cursor_position = 0;
215 int sub_comp_len;
216 char *str = NULL;
217 HIME_PREEDIT_ATTR att[HIME_PREEDIT_ATTR_MAX_N];
218 int attN = hime_im_client_get_preedit (hime_ch, &str, att, &preedit_cursor_position, &sub_comp_len);
219
220 int ret;
221 hime_im_client_set_flags (hime_ch, FLAG_HIME_client_handle_use_preedit, &ret);
222
223 QObject *input = qApp->focusObject ();
224
225 if (!input || !str) {
226 free (str);
227 return;
228 }
229
230 #if DEBUG
231 dbg ("update_preedit attN:%d '%s'\n", attN, str);
232 #endif
233
234 int i;
235 for (i = 0; i < attN; i++) {
236 int ofs0 = att[i].ofs0;
237 int len = att[i].ofs1 - att[i].ofs0;
238 QTextCharFormat format;
239
240 switch (att[i].flag) {
241 case HIME_PREEDIT_ATTR_FLAG_REVERSE: {
242 QBrush brush;
243 QPalette palette;
244 palette = QGuiApplication::palette ();
245 format.setBackground (QBrush (QColor (palette.color (QPalette::Active, QPalette::Highlight))));
246 format.setForeground (QBrush (QColor (palette.color (QPalette::Active, QPalette::HighlightedText))));
247 } break;
248 case HIME_PREEDIT_ATTR_FLAG_UNDERLINE: {
249 format.setUnderlineStyle (QTextCharFormat::DashUnderline);
250 } break;
251 default:;
252 }
253
254 attrList.append (QInputMethodEvent::Attribute (QInputMethodEvent::TextFormat, ofs0, len, format));
255 }
256
257 attrList.append (QInputMethodEvent::Attribute (QInputMethodEvent::Cursor, preedit_cursor_position, 1, 0));
258
259 QInputMethodEvent im_event (QString::fromUtf8 (str), attrList);
260 send_event (im_event);
261 free (str);
262 }
263
264 void QHimePlatformInputContext::send_event (QInputMethodEvent &e) {
265 QObject *input = qApp->focusObject ();
266 if (!input)
267 return;
268 QCoreApplication::sendEvent (input, &e);
269 }
270
271 void QHimePlatformInputContext::send_str (char *rstr) {
272 dbg ("send_str %s\n", rstr);
273 QString inputText = QString::fromUtf8 (rstr);
274 free (rstr);
275 QInputMethodEvent commit_event;
276 commit_event.setCommitString (inputText);
277 send_event (commit_event);
278 }
279
280 bool QHimePlatformInputContext::send_key_press (quint32 keysym, quint32 state) {
281 dbg ("send_key_press\n");
282 char *rstr = NULL;
283 int result = hime_im_client_forward_key_press (hime_ch, keysym, state, &rstr);
284
285 if (rstr) {
286 send_str (rstr);
287 }
288
289 return result;
290 }
291
292 bool QHimePlatformInputContext::filterEvent (const QEvent *event) {
293 dbg ("QHimePlatformInputContext::filterEvent\n");
294 if (event->type () != QEvent::KeyPress && event->type () != QEvent::KeyRelease) {
295 goto ret;
296 }
297
298 const QKeyEvent *keyEvent;
299 keyEvent = static_cast<const QKeyEvent *> (event);
300 quint32 keysym;
301 keysym = keyEvent->nativeVirtualKey ();
302 quint32 state;
303 state = keyEvent->nativeModifiers ();
304
305 if (!inputMethodAccepted ()) {
306 goto ret;
307 }
308
309 QObject *input;
310 input = qApp->focusObject ();
311
312 if (!input) {
313 goto ret;
314 }
315
316 int result;
317 if (event->type () == QEvent::KeyPress) {
318 if (send_key_press (keysym, state)) {
319 update_preedit ();
320 return true;
321 }
322 } else {
323 char *rstr = NULL;
324 result = hime_im_client_forward_key_release (hime_ch, keysym, state, &rstr);
325 if (rstr) {
326 free (rstr);
327 }
328
329 if (result) {
330 return true;
331 }
332 }
333
334 ret:
335 return QPlatformInputContext::filterEvent (event);
336 }