This repository has been archived by the owner on Jul 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ime.c
505 lines (407 loc) · 14.2 KB
/
ime.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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
#include "ime.h"
#if defined(FOOT_IME_ENABLED) && FOOT_IME_ENABLED
#include <string.h>
#include "text-input-unstable-v3.h"
#define LOG_MODULE "ime"
#define LOG_ENABLE_DBG 0
#include "log.h"
#include "render.h"
#include "search.h"
#include "terminal.h"
#include "util.h"
#include "wayland.h"
#include "xmalloc.h"
static void
enter(void *data, struct zwp_text_input_v3 *zwp_text_input_v3,
struct wl_surface *surface)
{
struct seat *seat = data;
LOG_DBG("enter: seat=%s", seat->name);
/* The main grid is the *only* input-receiving surface we have */
xassert(seat->kbd_focus != NULL);
seat->ime.focused = true;
ime_enable(seat);
}
static void
leave(void *data, struct zwp_text_input_v3 *zwp_text_input_v3,
struct wl_surface *surface)
{
struct seat *seat = data;
LOG_DBG("leave: seat=%s", seat->name);
ime_disable(seat);
seat->ime.focused = false;
}
static void
preedit_string(void *data, struct zwp_text_input_v3 *zwp_text_input_v3,
const char *text, int32_t cursor_begin, int32_t cursor_end)
{
LOG_DBG("preedit-string: text=%s, begin=%d, end=%d", text, cursor_begin, cursor_end);
struct seat *seat = data;
ime_reset_pending_preedit(seat);
if (text != NULL) {
seat->ime.preedit.pending.text = xstrdup(text);
seat->ime.preedit.pending.cursor_begin = cursor_begin;
seat->ime.preedit.pending.cursor_end = cursor_end;
}
}
static void
commit_string(void *data, struct zwp_text_input_v3 *zwp_text_input_v3,
const char *text)
{
LOG_DBG("commit: text=%s", text);
struct seat *seat = data;
ime_reset_pending_commit(seat);
if (text != NULL)
seat->ime.commit.pending.text = xstrdup(text);
}
static void
delete_surrounding_text(void *data, struct zwp_text_input_v3 *zwp_text_input_v3,
uint32_t before_length, uint32_t after_length)
{
LOG_DBG("delete-surrounding: before=%d, after=%d", before_length, after_length);
struct seat *seat = data;
seat->ime.surrounding.pending.before_length = before_length;
seat->ime.surrounding.pending.after_length = after_length;
}
static void
done(void *data, struct zwp_text_input_v3 *zwp_text_input_v3,
uint32_t serial)
{
/*
* From text-input-unstable-v3.h:
*
* The application must proceed by evaluating the changes in the
* following order:
*
* 1. Replace existing preedit string with the cursor.
* 2. Delete requested surrounding text.
* 3. Insert commit string with the cursor at its end.
* 4. Calculate surrounding text to send.
* 5. Insert new preedit text in cursor position.
* 6. Place cursor inside preedit text.
*/
LOG_DBG("done: serial=%u", serial);
struct seat *seat = data;
struct terminal *term = seat->kbd_focus;
if (seat->ime.serial != serial) {
LOG_DBG("IME serial mismatch: expected=0x%08x, got 0x%08x",
seat->ime.serial, serial);
return;
}
if (term == NULL) {
static bool have_warned = false;
if (!have_warned) {
LOG_WARN(
"%s: text-input::done() received on seat that isn't "
"focusing a terminal window", seat->name);
have_warned = true;
}
}
/* 1. Delete existing pre-edit text */
if (seat->ime.preedit.cells != NULL) {
ime_reset_preedit(seat);
if (term != NULL) {
if (term->is_searching)
render_refresh_search(term);
else
render_refresh(term);
}
}
/*
* 2. Delete requested surroundin text
*
* We don't support deleting surrounding text. But, we also never
* call set_surrounding_text() so hopefully we should never
* receive any requests to delete surrounding text.
*/
/* 3. Insert commit string */
if (seat->ime.commit.pending.text != NULL) {
const char *text = seat->ime.commit.pending.text;
size_t len = strlen(text);
if (term != NULL) {
if (term->is_searching) {
search_add_chars(term, text, len);
render_refresh_search(term);
} else
term_to_slave(term, text, len);
}
ime_reset_pending_commit(seat);
}
/* 4. Calculate surrounding text to send - not supported */
/* 5. Insert new pre-edit text */
size_t wchars = seat->ime.preedit.pending.text != NULL
? mbstowcs(NULL, seat->ime.preedit.pending.text, 0)
: 0;
if (wchars == 0 || wchars == (size_t)-1) {
ime_reset_pending_preedit(seat);
return;
}
/* First, convert to unicode */
seat->ime.preedit.text = xmalloc((wchars + 1) * sizeof(wchar_t));
mbstowcs(seat->ime.preedit.text, seat->ime.preedit.pending.text, wchars);
seat->ime.preedit.text[wchars] = L'\0';
/* Next, count number of cells needed */
size_t cell_count = 0;
size_t widths[wchars + 1];
for (size_t i = 0; i < wchars; i++) {
int width = max(wcwidth(seat->ime.preedit.text[i]), 1);
widths[i] = width;
cell_count += width;
}
/* Allocate cells */
seat->ime.preedit.cells = xmalloc(
cell_count * sizeof(seat->ime.preedit.cells[0]));
seat->ime.preedit.count = cell_count;
/* Populate cells */
for (size_t i = 0, cell_idx = 0; i < wchars; i++) {
struct cell *cell = &seat->ime.preedit.cells[cell_idx];
int width = widths[i];
cell->wc = seat->ime.preedit.text[i];
cell->attrs = (struct attributes){.clean = 0};
for (int j = 1; j < width; j++) {
cell = &seat->ime.preedit.cells[cell_idx + j];
cell->wc = CELL_SPACER + width - j;
cell->attrs = (struct attributes){.clean = 1};
}
cell_idx += width;
}
const size_t byte_len = strlen(seat->ime.preedit.pending.text);
/* Pre-edit cursor - hidden */
if (seat->ime.preedit.pending.cursor_begin == -1 ||
seat->ime.preedit.pending.cursor_end == -1)
{
/* Note: docs says *both* begin and end should be -1,
* but what else can we do if only one is -1? */
LOG_DBG("pre-edit cursor is hidden");
seat->ime.preedit.cursor.hidden = true;
seat->ime.preedit.cursor.start = -1;
seat->ime.preedit.cursor.end = -1;
}
else if (seat->ime.preedit.pending.cursor_begin == byte_len &&
seat->ime.preedit.pending.cursor_end == byte_len)
{
/* Cursor is *after* the entire pre-edit string */
seat->ime.preedit.cursor.hidden = false;
seat->ime.preedit.cursor.start = cell_count;
seat->ime.preedit.cursor.end = cell_count;
}
else {
/*
* Translate cursor position to cell indices
*
* The cursor_begin and cursor_end are counted in
* *bytes*. We want to map them to *cell* indices.
*
* To do this, we use mblen() to step though the utf-8
* pre-edit string, advancing a unicode character index as
* we go, *and* advancing a *cell* index using wcwidth()
* of the unicode character.
*
* When we find the matching *byte* index, we at the same
* time know both the unicode *and* cell index.
*/
int cell_begin = -1, cell_end = -1;
for (size_t byte_idx = 0, wc_idx = 0, cell_idx = 0;
byte_idx < byte_len &&
wc_idx < wchars &&
cell_idx < cell_count &&
(cell_begin < 0 || cell_end < 0);
cell_idx += widths[wc_idx], wc_idx++)
{
if (seat->ime.preedit.pending.cursor_begin == byte_idx)
cell_begin = cell_idx;
if (seat->ime.preedit.pending.cursor_end == byte_idx)
cell_end = cell_idx;
/* Number of bytes of *next* utf-8 character */
size_t left = byte_len - byte_idx;
int wc_bytes = mblen(&seat->ime.preedit.pending.text[byte_idx], left);
if (wc_bytes <= 0)
break;
byte_idx += wc_bytes;
}
if (seat->ime.preedit.pending.cursor_end >= byte_len)
cell_end = cell_count;
/* Bounded by number of screen columns */
cell_begin = min(max(cell_begin, 0), cell_count - 1);
cell_end = min(max(cell_end, 0), cell_count);
if (cell_end < cell_begin)
cell_end = cell_begin;
/* Expand cursor end to end of glyph */
while (cell_end > cell_begin && cell_end < cell_count &&
seat->ime.preedit.cells[cell_end].wc >= CELL_SPACER)
{
cell_end++;
}
LOG_DBG("pre-edit cursor: begin=%d, end=%d", cell_begin, cell_end);
xassert(cell_begin >= 0);
xassert(cell_begin < cell_count);
xassert(cell_begin <= cell_end);
xassert(cell_end >= 0);
xassert(cell_end <= cell_count);
seat->ime.preedit.cursor.hidden = false;
seat->ime.preedit.cursor.start = cell_begin;
seat->ime.preedit.cursor.end = cell_end;
}
/* Underline pre-edit string that is *not* covered by the cursor */
bool hidden = seat->ime.preedit.cursor.hidden;
int start = seat->ime.preedit.cursor.start;
int end = seat->ime.preedit.cursor.end;
for (size_t i = 0, cell_idx = 0; i < wchars; cell_idx += widths[i], i++) {
if (hidden || start == end || cell_idx < start || cell_idx >= end) {
struct cell *cell = &seat->ime.preedit.cells[cell_idx];
cell->attrs.underline = true;
}
}
ime_reset_pending_preedit(seat);
if (term != NULL) {
if (term->is_searching)
render_refresh_search(term);
else
render_refresh(term);
}
}
void
ime_reset_pending_preedit(struct seat *seat)
{
free(seat->ime.preedit.pending.text);
seat->ime.preedit.pending.text = NULL;
}
void
ime_reset_pending_commit(struct seat *seat)
{
free(seat->ime.commit.pending.text);
seat->ime.commit.pending.text = NULL;
}
void
ime_reset_pending(struct seat *seat)
{
ime_reset_pending_preedit(seat);
ime_reset_pending_commit(seat);
}
void
ime_reset_preedit(struct seat *seat)
{
if (seat->ime.preedit.cells == NULL)
return;
free(seat->ime.preedit.text);
free(seat->ime.preedit.cells);
seat->ime.preedit.text = NULL;
seat->ime.preedit.cells = NULL;
seat->ime.preedit.count = 0;
}
static void
ime_send_cursor_rect(struct seat *seat)
{
if (unlikely(seat->wayl->text_input_manager == NULL))
return;
if (!seat->ime.focused)
return;
struct terminal *term = seat->kbd_focus;
if (!term->ime_enabled)
return;
if (seat->ime.cursor_rect.pending.x == seat->ime.cursor_rect.sent.x &&
seat->ime.cursor_rect.pending.y == seat->ime.cursor_rect.sent.y &&
seat->ime.cursor_rect.pending.width == seat->ime.cursor_rect.sent.width &&
seat->ime.cursor_rect.pending.height == seat->ime.cursor_rect.sent.height)
{
return;
}
zwp_text_input_v3_set_cursor_rectangle(
seat->wl_text_input,
seat->ime.cursor_rect.pending.x / term->scale,
seat->ime.cursor_rect.pending.y / term->scale,
seat->ime.cursor_rect.pending.width / term->scale,
seat->ime.cursor_rect.pending.height / term->scale);
zwp_text_input_v3_commit(seat->wl_text_input);
seat->ime.serial++;
seat->ime.cursor_rect.sent = seat->ime.cursor_rect.pending;
}
void
ime_enable(struct seat *seat)
{
if (unlikely(seat->wayl->text_input_manager == NULL))
return;
if (!seat->ime.focused)
return;
struct terminal *term = seat->kbd_focus;
if (term == NULL)
return;
if (!term->ime_enabled)
return;
ime_reset_pending(seat);
ime_reset_preedit(seat);
zwp_text_input_v3_enable(seat->wl_text_input);
zwp_text_input_v3_set_content_type(
seat->wl_text_input,
ZWP_TEXT_INPUT_V3_CONTENT_HINT_NONE,
ZWP_TEXT_INPUT_V3_CONTENT_PURPOSE_TERMINAL);
zwp_text_input_v3_set_cursor_rectangle(
seat->wl_text_input,
seat->ime.cursor_rect.pending.x / term->scale,
seat->ime.cursor_rect.pending.y / term->scale,
seat->ime.cursor_rect.pending.width / term->scale,
seat->ime.cursor_rect.pending.height / term->scale);
seat->ime.cursor_rect.sent = seat->ime.cursor_rect.pending;
zwp_text_input_v3_commit(seat->wl_text_input);
seat->ime.serial++;
}
void
ime_disable(struct seat *seat)
{
if (unlikely(seat->wayl->text_input_manager == NULL))
return;
if (!seat->ime.focused)
return;
ime_reset_pending(seat);
ime_reset_preedit(seat);
zwp_text_input_v3_disable(seat->wl_text_input);
zwp_text_input_v3_commit(seat->wl_text_input);
seat->ime.serial++;
}
void
ime_update_cursor_rect(struct seat *seat)
{
struct terminal *term = seat->kbd_focus;
/* Set in render_ime_preedit() */
if (seat->ime.preedit.cells != NULL)
goto update;
/* Set in render_search_box() */
if (term->is_searching)
goto update;
int x, y, width, height;
int col = term->grid->cursor.point.col;
int row = term->grid->cursor.point.row;
row += term->grid->offset;
row -= term->grid->view;
row &= term->grid->num_rows - 1;
x = term->margins.left + col * term->cell_width;
y = term->margins.top + row * term->cell_height;
if (term->cursor_style == CURSOR_BEAM)
width = 1;
else
width = term->cell_width;
height = term->cell_height;
seat->ime.cursor_rect.pending.x = x;
seat->ime.cursor_rect.pending.y = y;
seat->ime.cursor_rect.pending.width = width;
seat->ime.cursor_rect.pending.height = height;
update:
ime_send_cursor_rect(seat);
}
const struct zwp_text_input_v3_listener text_input_listener = {
.enter = &enter,
.leave = &leave,
.preedit_string = &preedit_string,
.commit_string = &commit_string,
.delete_surrounding_text = &delete_surrounding_text,
.done = &done,
};
#else /* !FOOT_IME_ENABLED */
void ime_enable(struct seat *seat) {}
void ime_disable(struct seat *seat) {}
void ime_update_cursor_rect(struct seat *seat) {}
void ime_reset_pending_preedit(struct seat *seat) {}
void ime_reset_pending_commit(struct seat *seat) {}
void ime_reset_pending(struct seat *seat) {}
void ime_reset_preedit(struct seat *seat) {}
#endif