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