Debian Codegraph @

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

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