-
Notifications
You must be signed in to change notification settings - Fork 5
/
im.c
467 lines (405 loc) · 13.7 KB
/
im.c
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
#include <poll.h>
#include <signal.h>
#include <stdlib.h>
#include <string.h>
#include <sys/eventfd.h>
#include <sys/mman.h>
#include <sys/signalfd.h>
#include <sys/stat.h>
#include <sys/timerfd.h>
#include <time.h>
#include <unistd.h>
#include <wayland-client-protocol.h>
#include "input-method-unstable-v2-client-protocol.h"
#include "wlpinyin.h"
#define MIN(a, b) (a < b ? a : b)
struct wlpinyin_key {
xkb_keysym_t keysym;
uint32_t keycode;
bool pressed;
};
static int32_t get_miliseconds() {
struct timespec time;
clock_gettime(CLOCK_MONOTONIC, &time);
return time.tv_sec * 1000 + time.tv_nsec / (1000 * 1000);
}
static void im_send_preedit(struct wlpinyin_state *state, const char *text) {
wlpinyin_dbg("upd_preedit: %s", text ? text : "");
zwp_input_method_v2_set_preedit_string(state->input_method, text ? text : "",
0, 0);
}
static void im_send_text(struct wlpinyin_state *state, const char *text) {
wlpinyin_dbg("upd_text: %s", text ? text : "");
zwp_input_method_v2_commit_string(state->input_method, text ? text : "");
}
static void noop() {}
static void im_panel_update(struct wlpinyin_state *state) {
char buf[2048] = {};
int bufptr = 0;
preedit_t preedit = im_engine_preedit(state->engine);
if (!preedit.text) {
im_send_preedit(state, "");
return;
}
candidate_t cand = im_engine_candidate(state->engine);
bufptr += snprintf(&buf[bufptr], sizeof buf - bufptr, "[%d] ", cand.page_no);
int preedit_len = strlen(preedit.text);
for (int i = 0; i <= preedit_len; i++) {
if (preedit.start < preedit.end) {
if (i == preedit.start)
bufptr += snprintf(&buf[bufptr], sizeof buf - bufptr, "<");
else if (i == preedit.end)
bufptr += snprintf(&buf[bufptr], sizeof buf - bufptr, ">");
}
if (i == preedit.cursor)
bufptr += snprintf(&buf[bufptr], sizeof buf - bufptr, "|");
if (i < preedit_len)
bufptr +=
snprintf(&buf[bufptr], sizeof buf - bufptr, "%c", preedit.text[i]);
}
for (int i = 0; i < cand.num_candidates; i++) {
bool highlighted = i == cand.highlighted_candidate_index;
bufptr += snprintf(&buf[bufptr], sizeof buf - bufptr, " %s%d %s%s",
highlighted ? "[" : "", i + 1,
im_engine_candidate_get(state->engine, i),
highlighted ? "]" : "");
}
buf[bufptr] = 0;
im_send_preedit(state, buf);
}
static void im_handle_key(struct wlpinyin_state *state,
struct wlpinyin_key *keynode) {
if (state->xkb_state == NULL)
return;
xkb_state_update_key(state->xkb_state, keynode->keycode + 8,
keynode->pressed ? XKB_KEY_DOWN : XKB_KEY_UP);
bool handled = false;
if (!handled &&
im_toggle(state->xkb_state, keynode->keysym, keynode->pressed)) {
wlpinyin_err("toggle");
im_engine_toggle(state->engine);
handled = true;
}
if (!handled && keynode->pressed) {
handled =
im_engine_key(state->engine, keynode->keysym,
xkb_state_serialize_mods(state->xkb_state,
XKB_STATE_MODS_EFFECTIVE |
XKB_STATE_LAYOUT_EFFECTIVE));
}
if (!handled && im_engine_bypass(state->engine)) {
#ifndef NDEBUG
char buf[512] = {};
xkb_keysym_get_name(keynode->keysym, buf, sizeof buf);
wlpinyin_dbg("upd_kbd[%s]: keycode %02x, keysym %02x, %s", buf,
keynode->keycode, keynode->keysym,
keynode->pressed ? "pressed" : "released");
#endif
zwp_virtual_keyboard_v1_key(
state->virtual_keyboard, get_miliseconds(), keynode->keycode,
keynode->pressed ? WL_KEYBOARD_KEY_STATE_PRESSED
: WL_KEYBOARD_KEY_STATE_RELEASED);
} else {
im_panel_update(state);
const char *commit = im_engine_commit_text(state->engine);
if (strlen(commit) != 0)
im_send_text(state, commit);
zwp_input_method_v2_commit(state->input_method, state->im_serial);
}
wl_display_flush(state->display);
}
static void handle_keymap(
void *data,
struct zwp_input_method_keyboard_grab_v2 *zwp_input_method_keyboard_grab_v2,
uint32_t format,
int32_t fd,
uint32_t size) {
UNUSED(zwp_input_method_keyboard_grab_v2);
struct wlpinyin_state *state = data;
wlpinyin_dbg("ev_keymap: format %d, size %d, fd %d", format, size, fd);
char *keymap_string = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
if (state->xkb_keymap_string != NULL &&
!strcmp(keymap_string, state->xkb_keymap_string)) {
munmap(keymap_string, size);
return;
}
xkb_state_unref(state->xkb_state);
xkb_keymap_unref(state->xkb_keymap);
if (state->xkb_keymap_string != NULL)
free(state->xkb_keymap_string);
state->xkb_keymap_string = strdup(keymap_string);
state->xkb_keymap =
xkb_keymap_new_from_string(state->xkb_context, state->xkb_keymap_string,
format, XKB_KEYMAP_COMPILE_NO_FLAGS);
state->xkb_state = xkb_state_new(state->xkb_keymap);
munmap(keymap_string, size);
zwp_virtual_keyboard_v1_keymap(state->virtual_keyboard, format, fd, size);
}
static void handle_key(
void *data,
struct zwp_input_method_keyboard_grab_v2 *zwp_input_method_keyboard_grab_v2,
uint32_t serial,
uint32_t time,
uint32_t key,
uint32_t kstate) {
UNUSED(zwp_input_method_keyboard_grab_v2);
struct wlpinyin_state *state = data;
struct wlpinyin_key keynode = {};
keynode.keycode = key;
keynode.keysym = xkb_state_key_get_one_sym(state->xkb_state, key + 8);
keynode.pressed = kstate == WL_KEYBOARD_KEY_STATE_PRESSED;
#ifndef NDEBUG
char buf[512] = {};
xkb_keysym_get_name(keynode.keysym, buf, sizeof buf);
wlpinyin_dbg("ev_key[%s]: keycode %02x, keysym %02x, serial %d, time %d, %s",
buf, keynode.keycode, keynode.keysym, serial, time,
keynode.pressed ? "pressed" : "released");
#endif
// reset repeat key
if (keynode.pressed &&
xkb_keymap_key_repeats(state->xkb_keymap, keynode.keycode + 8) == 1) {
state->im_repeat_key = keynode.keycode;
struct itimerspec timer = {
.it_value = {.tv_nsec = state->im_repeat_delay * 1000000},
.it_interval = {.tv_nsec = 1000000000 / state->im_repeat_rate},
};
timerfd_settime(state->timerfd, 0, &timer, NULL);
} else {
struct itimerspec timer = {};
timerfd_settime(state->timerfd, 0, &timer, NULL);
}
// handle it
im_handle_key(state, &keynode);
}
static void handle_modifiers(
void *data,
struct zwp_input_method_keyboard_grab_v2 *zwp_input_method_keyboard_grab_v2,
uint32_t serial,
uint32_t mods_depressed,
uint32_t mods_latched,
uint32_t mods_locked,
uint32_t group) {
UNUSED(zwp_input_method_keyboard_grab_v2);
struct wlpinyin_state *state = data;
wlpinyin_dbg(
"ev_modifiers: serial %d, depressed %d, latched %d, locked %d, group %d",
serial, mods_depressed, mods_latched, mods_locked, group);
xkb_state_update_mask(state->xkb_state, mods_depressed, mods_latched,
mods_locked, 0, 0, group);
zwp_virtual_keyboard_v1_modifiers(state->virtual_keyboard, mods_depressed,
mods_latched, mods_locked, group);
}
static void handle_repeat_info(
void *data,
struct zwp_input_method_keyboard_grab_v2 *zwp_input_method_keyboard_grab_v2,
int32_t rate,
int32_t delay) {
UNUSED(zwp_input_method_keyboard_grab_v2);
struct wlpinyin_state *state = data;
wlpinyin_dbg("ev_repeat: rate %d, delay %d", rate, delay);
state->im_repeat_delay = (uint32_t)delay;
state->im_repeat_rate = (uint32_t)rate;
}
static void handle_reset(void *data,
struct zwp_input_method_v2 *zwp_input_method_v2) {
UNUSED(zwp_input_method_v2);
struct wlpinyin_state *state = data;
wlpinyin_dbg("ev_reset");
im_engine_reset(state->engine);
}
static void handle_done(void *data,
struct zwp_input_method_v2 *zwp_input_method_v2) {
UNUSED(zwp_input_method_v2);
struct wlpinyin_state *state = data;
state->im_serial++;
}
static void handle_global(void *data,
struct wl_registry *registry,
uint32_t name,
const char *interface,
uint32_t version) {
struct wlpinyin_state *state = data;
if (strcmp(interface, zwp_input_method_manager_v2_interface.name) == 0) {
state->input_method_manager = wl_registry_bind(
registry, name, &zwp_input_method_manager_v2_interface, 1);
} else if (strcmp(interface,
zwp_virtual_keyboard_manager_v1_interface.name) == 0) {
state->virtual_keyboard_manager = wl_registry_bind(
registry, name, &zwp_virtual_keyboard_manager_v1_interface, 1);
} else if (strcmp(interface, wl_seat_interface.name) == 0) {
state->seat = wl_registry_bind(registry, name, &wl_seat_interface, version);
} else if (strcmp(interface, wl_compositor_interface.name) == 0) {
state->compositor =
wl_registry_bind(registry, name, &wl_compositor_interface, version);
}
}
struct wlpinyin_state *im_setup(int signalfd, struct wl_display *display) {
struct wlpinyin_state *state = calloc(1, sizeof(struct wlpinyin_state));
if (state == NULL) {
wlpinyin_err("failed to calloc state");
return NULL;
}
state->signalfd = signalfd;
state->display = display;
{
struct wl_registry *registry = wl_display_get_registry(state->display);
static const struct wl_registry_listener registry_listener = {
.global = handle_global,
.global_remove = NULL,
};
wl_registry_add_listener(registry, ®istry_listener, state);
wl_display_roundtrip(state->display);
if (state->input_method_manager == NULL ||
state->virtual_keyboard_manager == NULL || state->seat == NULL ||
state->compositor == NULL) {
wlpinyin_err("required wayland interface not available");
goto clean;
}
}
state->timerfd = timerfd_create(CLOCK_REALTIME, TFD_CLOEXEC);
if (state->timerfd == -1) {
wlpinyin_err("failed to setup event timer");
goto clean;
}
state->im_serial = 0;
state->engine = im_engine_new();
if (state->engine == NULL) {
wlpinyin_err("failed to setup engine");
goto clean;
}
state->xkb_context = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
if (state->xkb_context == NULL) {
wlpinyin_err("failed to setup xkb context");
goto clean;
}
state->virtual_keyboard =
zwp_virtual_keyboard_manager_v1_create_virtual_keyboard(
state->virtual_keyboard_manager, state->seat);
if (state->virtual_keyboard == NULL) {
wlpinyin_err("failed to setup virtual keyboard");
goto clean;
}
state->input_method = zwp_input_method_manager_v2_get_input_method(
state->input_method_manager, state->seat);
if (state->input_method == NULL) {
wlpinyin_err("failed to setup input_method");
goto clean;
}
/*
state->popup_surface_wl = wl_compositor_create_surface(state->compositor);
if (state->popup_surface_wl == NULL) {
wlpinyin_err("failed to create surface");
goto clean;
}
state->popup_surface = zwp_input_method_v2_get_input_popup_surface(
state->input_method, state->popup_surface_wl);
if (state->popup_surface_wl == NULL) {
wlpinyin_err("failed to get popup surface");
goto clean;
}
*/
static const struct zwp_input_method_v2_listener im_listener = {
.activate = handle_reset,
.deactivate = handle_reset,
.surrounding_text = noop,
.text_change_cause = noop,
.content_type = noop,
.done = handle_done,
.unavailable = handle_reset,
};
zwp_input_method_v2_add_listener(state->input_method, &im_listener, state);
state->input_method_keyboard_grab =
zwp_input_method_v2_grab_keyboard(state->input_method);
static const struct zwp_input_method_keyboard_grab_v2_listener
im_activate_listener = {
.keymap = handle_keymap,
.key = handle_key,
.modifiers = handle_modifiers,
.repeat_info = handle_repeat_info,
};
zwp_input_method_keyboard_grab_v2_add_listener(
state->input_method_keyboard_grab, &im_activate_listener, state);
wl_display_roundtrip(state->display);
return state;
clean:
im_destroy(state);
return NULL;
}
int im_loop(struct wlpinyin_state *state) {
enum {
fd_signal = 0,
fd_wayland,
fd_repeat,
fd_max,
};
struct pollfd fds[fd_max] = {};
fds[fd_signal].fd = state->signalfd;
fds[fd_signal].events = POLLIN;
fds[fd_wayland].fd = wl_display_get_fd(state->display);
fds[fd_wayland].events = POLLIN;
fds[fd_repeat].fd = state->timerfd;
fds[fd_repeat].events = POLLIN;
bool running = true;
while (running && poll(fds, sizeof fds / sizeof fds[fd_wayland], -1) != -1) {
if (fds[fd_signal].revents & POLLIN) {
fds[fd_signal].revents = 0;
struct signalfd_siginfo info = {};
read(fds[fd_signal].fd, &info, sizeof(info));
switch (info.ssi_signo) {
case SIGINT:
case SIGTERM:
running = false;
break;
}
wlpinyin_dbg("signal: %d, running: %d", info.ssi_signo, running);
} else if (fds[fd_wayland].revents & POLLIN) {
fds[fd_wayland].revents = 0;
if (wl_display_roundtrip(state->display) == -1) {
break;
}
} else if (fds[fd_repeat].revents & POLLIN) {
fds[fd_repeat].revents = 0;
uint64_t tick;
read(fds[fd_repeat].fd, &tick, sizeof tick);
struct wlpinyin_key keynode = {
.keycode = state->im_repeat_key,
.keysym = xkb_state_key_get_one_sym(state->xkb_state,
state->im_repeat_key + 8),
};
for (; tick != 0; tick--) {
keynode.pressed = true;
im_handle_key(state, &keynode);
keynode.pressed = false;
im_handle_key(state, &keynode);
}
}
}
return 0;
}
int im_destroy(struct wlpinyin_state *state) {
if (state->input_method_keyboard_grab != NULL)
zwp_input_method_keyboard_grab_v2_release(
state->input_method_keyboard_grab);
if (state->engine)
im_engine_free(state->engine);
/*
if (state->popup_surface)
zwp_input_popup_surface_v2_destroy(state->popup_surface);
if (state->popup_surface_wl)
wl_surface_destroy(state->popup_surface_wl);
*/
if (state->virtual_keyboard)
zwp_virtual_keyboard_v1_destroy(state->virtual_keyboard);
if (state->input_method)
zwp_input_method_v2_destroy(state->input_method);
if (state->xkb_keymap_string)
free(state->xkb_keymap_string);
if (state->xkb_state)
xkb_state_unref(state->xkb_state);
if (state->xkb_keymap)
xkb_keymap_unref(state->xkb_keymap);
if (state->xkb_context)
xkb_context_unref(state->xkb_context);
wl_display_flush(state->display);
return 0;
}