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
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
|
diff --git a/README.md b/README.md
index c95b837..729f0b9 100644
--- a/README.md
+++ b/README.md
@@ -55,8 +55,9 @@ _Fuzzel, with transparency, on top of a browser window showing a diff of a fuzze
- Zsh and Fish completions
- Support for true fractional scaling
- Wayland native
+- IME support via the _text-input-unstable-v3_ protocol
-For details, see [`man fuzzel`](./doc/fuzzel.1.scd) and [`man fuzzel.ini`](./doc/fuzzel.ini.5.scd)
+For details, see [`man fuzzel`](./doc/fuzzel.1.scd)
## Requirements
diff --git a/char32.c b/char32.c
index 44f267d..467d3d2 100644
--- a/char32.c
+++ b/char32.c
@@ -32,7 +32,7 @@ _Static_assert(
#if !defined(__STDC_UTF_32__) || !__STDC_UTF_32__
#error "char32_t does not use UTF-32"
#endif
-#if (!defined(__STDC_ISO_10646__) || !__STDC_ISO_10646__) && !defined(__FreeBSD__) && !defined(__OpenBSD__)
+#if (!defined(__STDC_ISO_10646__) || !__STDC_ISO_10646__) && !defined(__FreeBSD__)
#error "wchar_t does not use UTF-32"
#endif
diff --git a/doc/fuzzel.1.scd b/doc/fuzzel.1.scd
index 04f3c60..eeb7246 100644
--- a/doc/fuzzel.1.scd
+++ b/doc/fuzzel.1.scd
@@ -629,6 +629,11 @@ For more information, see *fuzzel.ini*(5).
Fuzzel uses the localized strings from the .desktop files by
default. To disable this, run fuzzel with *LC_MESSAGES=C*.
+# IME
+
+Fuzzel supports input method editors (IME) via the
+*text-input-unstable-v3* Wayland protocol.
+
# FONT FORMAT
The font is specified in FontConfig syntax. That is, a colon-separated
diff --git a/fuzzel.ini b/fuzzel.ini
index 3a76e89..e8a99e8 100644
--- a/fuzzel.ini
+++ b/fuzzel.ini
@@ -49,8 +49,6 @@
# render-workers=<number of logical CPUs>
# match-workers=<number of logical CPUs>
-# auto-select=no
-
# enable-mouse=yes
[colors]
diff --git a/meson.build b/meson.build
index ab348f2..5d6b4ca 100644
--- a/meson.build
+++ b/meson.build
@@ -153,6 +153,7 @@ wl_proto_xml = [
wayland_protocols_datadir / 'unstable/primary-selection/primary-selection-unstable-v1.xml',
wayland_protocols_datadir / 'staging/color-management/color-management-v1.xml',
wayland_protocols_datadir / 'staging/single-pixel-buffer/single-pixel-buffer-v1.xml',
+ wayland_protocols_datadir / 'unstable/text-input/text-input-unstable-v3.xml',
]
foreach prot : wl_proto_xml
diff --git a/render.c b/render.c
index 2ba509f..13b8d48 100644
--- a/render.c
+++ b/render.c
@@ -101,6 +101,11 @@ struct render {
unsigned input_glyph_offset; /* At which glyph to start rendering input */
+ /* Set during render_prompt; used by IME for cursor rectangle */
+ struct {
+ int x, y, w, h; /* buffer pixel coordinates */
+ } cursor_rect;
+
struct {
uint16_t count;
sem_t start;
@@ -730,9 +735,88 @@ render_message(struct render *render, struct buffer *buf)
fcft_text_run_destroy(message_run);
}
+static void
+update_cursor_rect(struct render *render, int x, int y)
+{
+ struct fcft_font *font = render->font;
+ const int height = min(font->ascent + font->descent, render->row_height);
+ render->cursor_rect.x = x;
+ render->cursor_rect.y = y + font->descent - height;
+ render->cursor_rect.w = font->underline.thickness;
+ render->cursor_rect.h = height;
+}
+
+/*
+ * Render preedit text at the cursor position, following foot's approach:
+ * - All preedit glyphs are rendered with an underline.
+ * - A bar cursor is placed at the glyph index given by preedit_cursor
+ * (-1 means the cursor is hidden).
+ * Returns the new x position after all preedit glyphs.
+ */
+static int
+render_preedit_at_cursor(const struct render *render, struct buffer *buf,
+ int x, int y, int max_x,
+ const char32_t *preedit, size_t preedit_len,
+ int32_t preedit_cursor,
+ enum fcft_subpixel subpixel)
+{
+ struct fcft_font *font = render->font;
+ const int ul_y = y - font->underline.position;
+ const int ul_h = font->underline.thickness;
+
+ char32_t prev = 0;
+
+ for (size_t i = 0; i < preedit_len; i++) {
+ /* Cursor bar before this glyph */
+ if (preedit_cursor >= 0 && (int32_t)i == preedit_cursor)
+ render_cursor(render, x, y, buf->pix[0]);
+
+ const struct fcft_glyph *glyph =
+ fcft_rasterize_char_utf32(font, preedit[i], subpixel);
+
+ if (glyph == NULL) {
+ prev = 0;
+ continue;
+ }
+
+ long x_kern = 0;
+ if (prev != 0)
+ fcft_kerning(font, prev, preedit[i], &x_kern, NULL);
+ x += x_kern;
+
+ const int pixels_needed = max(glyph->x + glyph->width, glyph->advance.x);
+ if (x + pixels_needed > max_x)
+ break;
+
+ const int glyph_start_x = x;
+
+ render_glyph(buf->pix[0], glyph, x, y, &render->pix_input_color);
+ x += glyph->advance.x;
+ x += pt_or_px_as_pixels(render, &render->conf->letter_spacing);
+
+ /* Underline below the preedit glyph */
+ pixman_image_fill_rectangles(
+ PIXMAN_OP_SRC, buf->pix[0], &render->pix_input_color,
+ 1, &(pixman_rectangle16_t){
+ glyph_start_x,
+ ul_y,
+ x - glyph_start_x,
+ ul_h});
+
+ prev = preedit[i];
+ }
+
+ /* Cursor bar after all preedit glyphs */
+ if (preedit_cursor < 0 || preedit_cursor >= (int32_t)preedit_len)
+ render_cursor(render, x, y, buf->pix[0]);
+
+ return x;
+}
+
void
render_prompt(struct render *render, struct buffer *buf,
- const struct prompt *prompt, const struct matches *matches)
+ const struct prompt *prompt, const struct matches *matches,
+ const char32_t *preedit, int32_t preedit_cursor)
{
struct fcft_font *font = render->font;
assert(font != NULL);
@@ -745,7 +829,9 @@ render_prompt(struct render *render, struct buffer *buf,
const char32_t *ptext = prompt_text(prompt);
size_t text_len = c32len(ptext);
- bool use_placeholder = text_len == 0;
+ /* Suppress placeholder while IME preedit is active */
+ const bool have_preedit = preedit != NULL && preedit[0] != U'\0';
+ bool use_placeholder = text_len == 0 && !have_preedit;
const enum fcft_subpixel subpixel =
(render->conf->colors.background.a == 1. &&
@@ -871,11 +957,17 @@ render_prompt(struct render *render, struct buffer *buf,
/* Cursor, if right after the prompt. In all other cases, the
* cursor will be rendered by the loop below */
- if (cursor_location == render->input_glyph_offset
- || (conf->password_mode.enabled &&
- conf->password_mode.character == U'\0'))
- {
- render_cursor(render, x, y, buf->pix[0]);
+ const bool is_hidden_pwd =
+ conf->password_mode.enabled && conf->password_mode.character == U'\0';
+ if (cursor_location == render->input_glyph_offset || is_hidden_pwd) {
+ update_cursor_rect(render, x, y);
+ if (have_preedit && !is_hidden_pwd) {
+ x = render_preedit_at_cursor(
+ render, buf, x, y, max_x,
+ preedit, c32len(preedit), preedit_cursor, subpixel);
+ } else {
+ render_cursor(render, x, y, buf->pix[0]);
+ }
}
if (input_run != NULL && !(conf->password_mode.enabled && !use_placeholder)) {
@@ -944,8 +1036,16 @@ render_prompt(struct render *render, struct buffer *buf,
x += pt_or_px_as_pixels(render, &render->conf->letter_spacing);
/* Cursor */
- if (cursor_location > 0 && cursor_location - 1 == i)
- render_cursor(render, x, y, buf->pix[0]);
+ if (cursor_location > 0 && cursor_location - 1 == i) {
+ update_cursor_rect(render, x, y);
+ if (have_preedit) {
+ x = render_preedit_at_cursor(
+ render, buf, x, y, max_x,
+ preedit, c32len(preedit), preedit_cursor, subpixel);
+ } else {
+ render_cursor(render, x, y, buf->pix[0]);
+ }
+ }
prev = glyph->cp;
}
@@ -2174,6 +2274,15 @@ render_get_row_num(const struct render *render, int width, int x, int y,
return row;
}
+void
+render_get_cursor_rect(const struct render *render, int *x, int *y, int *w, int *h)
+{
+ *x = render->cursor_rect.x;
+ *y = render->cursor_rect.y;
+ *w = render->cursor_rect.w;
+ *h = render->cursor_rect.h;
+}
+
void
render_flush_text_run_cache(struct render *render)
{
diff --git a/render.h b/render.h
index f220fab..db23230 100644
--- a/render.h
+++ b/render.h
@@ -31,7 +31,11 @@ void render_message(struct render *render, struct buffer *buf);
void render_prompt(
struct render *render, struct buffer *buf,
- const struct prompt *prompt, const struct matches *matches);
+ const struct prompt *prompt, const struct matches *matches,
+ const char32_t *preedit, int32_t preedit_cursor);
+
+void render_get_cursor_rect(
+ const struct render *render, int *x, int *y, int *w, int *h);
void render_match_list(
struct render *render, struct buffer *buf,
diff --git a/wayland.c b/wayland.c
index e767b01..93b1fc6 100644
--- a/wayland.c
+++ b/wayland.c
@@ -31,6 +31,7 @@
#include <fractional-scale-v1.h>
#include <primary-selection-unstable-v1.h>
#include <single-pixel-buffer-v1.h>
+#include <text-input-unstable-v3.h>
#include <viewporter.h>
#include <wlr-layer-shell-unstable-v1.h>
#include <xdg-activation-v1.h>
@@ -195,6 +196,13 @@ struct wayland {
} color_management;
struct wp_single_pixel_buffer_manager_v1 *single_pixel_manager;
+ struct zwp_text_input_manager_v3 *text_input_manager;
+
+ /* Active preedit text (displayed inline in the prompt) */
+ struct {
+ char32_t *text; /* NULL when no preedit is active */
+ int32_t cursor; /* glyph index for cursor (-1 = hidden) */
+ } preedit;
tll(struct monitor) monitors;
const struct monitor *monitor;
@@ -304,6 +312,14 @@ seat_destroy(struct seat *seat)
if (seat->wl_seat != NULL)
wl_seat_release(seat->wl_seat);
+ if (seat->text_input != NULL) {
+ zwp_text_input_v3_disable(seat->text_input);
+ zwp_text_input_v3_commit(seat->text_input);
+ zwp_text_input_v3_destroy(seat->text_input);
+ }
+ free(seat->ime.preedit_pending.text);
+ free(seat->ime.commit_pending);
+
free(seat->clipboard.text);
free(seat->primary.text);
free(seat->name);
@@ -1498,6 +1514,270 @@ static const struct wl_touch_listener touch_listener = {
.orientation = wl_touch_orientation,
};
+/*---------------------------------------------------------------------------
+ * text-input-unstable-v3 listener
+ *---------------------------------------------------------------------------*/
+
+/*
+ * Convert a UTF-8 byte offset within a preedit string to the corresponding
+ * char32_t (glyph) index. Returns the glyph count if byte_offset is at or
+ * beyond the end of the string.
+ */
+static int32_t
+byte_offset_to_glyph_idx(const char *text, size_t byte_len, int32_t byte_offset)
+{
+ if (byte_offset < 0)
+ return -1;
+
+ int32_t glyph_idx = 0;
+ size_t byte_idx = 0;
+
+ while (byte_idx < byte_len) {
+ if ((int32_t)byte_idx == byte_offset)
+ return glyph_idx;
+
+ int n = mblen(&text[byte_idx], byte_len - byte_idx);
+ if (n <= 0)
+ break;
+
+ byte_idx += n;
+ glyph_idx++;
+ }
+
+ /* byte_offset at or past end of string */
+ return glyph_idx;
+}
+
+static void
+text_input_enter(void *data, struct zwp_text_input_v3 *text_input,
+ struct wl_surface *surface)
+{
+ struct seat *seat = data;
+ struct wayland *wayl = seat->wayl;
+
+ LOG_DBG("text_input enter");
+
+ /* Reset any stale pending state */
+ free(seat->ime.preedit_pending.text);
+ seat->ime.preedit_pending.text = NULL;
+ free(seat->ime.commit_pending);
+ seat->ime.commit_pending = NULL;
+
+ zwp_text_input_v3_enable(text_input);
+ zwp_text_input_v3_set_content_type(
+ text_input,
+ ZWP_TEXT_INPUT_V3_CONTENT_HINT_NONE,
+ ZWP_TEXT_INPUT_V3_CONTENT_PURPOSE_NORMAL);
+
+ /* Send current cursor rect to position the IME popup */
+ int cx, cy, cw, ch;
+ render_get_cursor_rect(wayl->render, &cx, &cy, &cw, &ch);
+ const float scale = wayl->scale;
+ zwp_text_input_v3_set_cursor_rectangle(
+ text_input,
+ (int)(cx / scale), (int)(cy / scale),
+ (int)(cw / scale), (int)(ch / scale));
+
+ zwp_text_input_v3_commit(text_input);
+ seat->ime.serial++;
+
+ /* Record what we just sent */
+ seat->ime.cursor_rect.x = (int)(cx / scale);
+ seat->ime.cursor_rect.y = (int)(cy / scale);
+ seat->ime.cursor_rect.w = (int)(cw / scale);
+ seat->ime.cursor_rect.h = (int)(ch / scale);
+}
+
+static void
+text_input_leave(void *data, struct zwp_text_input_v3 *text_input,
+ struct wl_surface *surface)
+{
+ struct seat *seat = data;
+ struct wayland *wayl = seat->wayl;
+
+ LOG_DBG("text_input leave");
+
+ free(seat->ime.preedit_pending.text);
+ seat->ime.preedit_pending.text = NULL;
+ free(seat->ime.commit_pending);
+ seat->ime.commit_pending = NULL;
+
+ /* Clear any active preedit */
+ if (wayl->preedit.text != NULL) {
+ free(wayl->preedit.text);
+ wayl->preedit.text = NULL;
+ wayl->preedit.cursor = -1;
+ wayl_refresh(wayl);
+ }
+
+ zwp_text_input_v3_disable(text_input);
+ zwp_text_input_v3_commit(text_input);
+ seat->ime.serial++;
+}
+
+static void
+text_input_preedit_string(void *data, struct zwp_text_input_v3 *text_input,
+ const char *text, int32_t cursor_begin, int32_t cursor_end)
+{
+ struct seat *seat = data;
+
+ LOG_DBG("preedit_string: text=%s begin=%d end=%d",
+ text ? text : "(null)", cursor_begin, cursor_end);
+
+ free(seat->ime.preedit_pending.text);
+ seat->ime.preedit_pending.text = text ? xstrdup(text) : NULL;
+ seat->ime.preedit_pending.cursor_begin = cursor_begin;
+ seat->ime.preedit_pending.cursor_end = cursor_end;
+}
+
+static void
+text_input_commit_string(void *data, struct zwp_text_input_v3 *text_input,
+ const char *text)
+{
+ struct seat *seat = data;
+
+ LOG_DBG("commit_string: text=%s", text ? text : "(null)");
+
+ free(seat->ime.commit_pending);
+ seat->ime.commit_pending = text ? xstrdup(text) : NULL;
+}
+
+static void
+text_input_delete_surrounding_text(void *data,
+ struct zwp_text_input_v3 *text_input,
+ uint32_t before_length,
+ uint32_t after_length)
+{
+ /*
+ * We never call set_surrounding_text(), so the compositor should
+ * never request deletion of surrounding text. Log it anyway.
+ */
+ LOG_DBG("delete_surrounding_text: before=%u after=%u (ignored)",
+ before_length, after_length);
+}
+
+static void
+text_input_done(void *data, struct zwp_text_input_v3 *text_input,
+ uint32_t serial)
+{
+ struct seat *seat = data;
+ struct wayland *wayl = seat->wayl;
+
+ LOG_DBG("done: serial=%u (expected=%u)", serial, seat->ime.serial);
+
+ /*
+ * Ignore stale events. The serial we track is the number of
+ * commit() calls we have made; the compositor echoes it back.
+ */
+ if (seat->ime.serial != serial) {
+ LOG_DBG("IME serial mismatch, discarding");
+ free(seat->ime.preedit_pending.text);
+ seat->ime.preedit_pending.text = NULL;
+ free(seat->ime.commit_pending);
+ seat->ime.commit_pending = NULL;
+ return;
+ }
+
+ bool refresh = false;
+
+ /*
+ * Apply protocol events in the required order:
+ * 1. Replace existing preedit with cursor.
+ * 2. Delete surrounding text (not implemented).
+ * 3. Insert commit string.
+ * 4. Calculate new surrounding text (not implemented).
+ * 5. Insert new preedit text at cursor.
+ * 6. Place cursor inside preedit.
+ */
+
+ /* 1. Clear existing preedit */
+ if (wayl->preedit.text != NULL) {
+ free(wayl->preedit.text);
+ wayl->preedit.text = NULL;
+ wayl->preedit.cursor = -1;
+ refresh = true;
+ }
+
+ /* 3. Insert commit string into the prompt */
+ bool did_commit = false;
+ if (seat->ime.commit_pending != NULL) {
+ const char *text = seat->ime.commit_pending;
+ prompt_insert_chars(wayl->prompt, text, strlen(text));
+ free(seat->ime.commit_pending);
+ seat->ime.commit_pending = NULL;
+ matches_update_incremental(wayl->matches);
+ matches_selected_set(wayl->matches, 0);
+ did_commit = true;
+ refresh = true;
+ }
+
+ /* 5 & 6. Activate new preedit and compute cursor position */
+ if (seat->ime.preedit_pending.text != NULL &&
+ seat->ime.preedit_pending.text[0] != '\0')
+ {
+ const char *preedit_utf8 = seat->ime.preedit_pending.text;
+ const size_t byte_len = strlen(preedit_utf8);
+
+ char32_t *preedit_c32 = ambstoc32(preedit_utf8);
+ if (preedit_c32 != NULL) {
+ wayl->preedit.text = preedit_c32;
+
+ const int32_t cb = seat->ime.preedit_pending.cursor_begin;
+ const int32_t ce = seat->ime.preedit_pending.cursor_end;
+
+ if (cb < 0 || ce < 0) {
+ /* Compositor requests hidden cursor within preedit */
+ wayl->preedit.cursor = -1;
+ } else {
+ /* Map the byte offset to a glyph (char32_t) index */
+ wayl->preedit.cursor =
+ byte_offset_to_glyph_idx(preedit_utf8, byte_len, cb);
+ }
+ }
+ free(seat->ime.preedit_pending.text);
+ seat->ime.preedit_pending.text = NULL;
+ refresh = true;
+ } else {
+ free(seat->ime.preedit_pending.text);
+ seat->ime.preedit_pending.text = NULL;
+ }
+
+ if (refresh) {
+ wayl_refresh(wayl);
+ if (did_commit)
+ check_auto_select(seat, true);
+ }
+}
+
+static const struct zwp_text_input_v3_listener text_input_listener = {
+ .enter = text_input_enter,
+ .leave = text_input_leave,
+ .preedit_string = text_input_preedit_string,
+ .commit_string = text_input_commit_string,
+ .delete_surrounding_text= text_input_delete_surrounding_text,
+ .done = text_input_done,
+};
+
+static void
+seat_add_text_input(struct seat *seat)
+{
+ struct wayland *wayl = seat->wayl;
+
+ if (wayl->text_input_manager == NULL)
+ return;
+ if (seat->text_input != NULL)
+ return;
+
+ seat->text_input = zwp_text_input_manager_v3_get_text_input(
+ wayl->text_input_manager, seat->wl_seat);
+ if (seat->text_input == NULL) {
+ LOG_ERR("failed to create text_input object");
+ return;
+ }
+ zwp_text_input_v3_add_listener(
+ seat->text_input, &text_input_listener, seat);
+}
+
static void
seat_handle_capabilities(void *data, struct wl_seat *wl_seat,
enum wl_seat_capability caps)
@@ -1509,12 +1789,17 @@ seat_handle_capabilities(void *data, struct wl_seat *wl_seat,
if (seat->wl_keyboard == NULL) {
seat->wl_keyboard = wl_seat_get_keyboard(wl_seat);
wl_keyboard_add_listener(seat->wl_keyboard, &keyboard_listener, seat);
+ seat_add_text_input(seat);
}
} else {
if (seat->wl_keyboard != NULL) {
wl_keyboard_release(seat->wl_keyboard);
seat->wl_keyboard = NULL;
}
+ if (seat->text_input != NULL) {
+ zwp_text_input_v3_destroy(seat->text_input);
+ seat->text_input = NULL;
+ }
}
if (caps & WL_SEAT_CAPABILITY_POINTER) {
@@ -2348,6 +2633,20 @@ handle_global(void *data, struct wl_registry *registry,
&wp_single_pixel_buffer_manager_v1_interface, required);
}
+ else if (strcmp(interface, zwp_text_input_manager_v3_interface.name) == 0) {
+ const uint32_t required = 1;
+ if (!verify_iface_version(interface, version, required))
+ return;
+
+ wayl->text_input_manager = wl_registry_bind(
+ wayl->registry, name,
+ &zwp_text_input_manager_v3_interface, required);
+
+ /* Late-bind: create text_input for seats already registered */
+ tll_foreach(wayl->seats, it)
+ seat_add_text_input(&it->item);
+ }
+
}
static void
@@ -2575,6 +2874,7 @@ wayl_refresh(struct wayland *wayl)
}
struct buffer *buf = shm_get_buffer(wayl->chain, wayl->width, wayl->height, true);
+ bool did_render_prompt = false;
pixman_region32_t clip;
pixman_region32_init_rect(&clip, 0, 0, buf->width, buf->height);
@@ -2600,7 +2900,9 @@ wayl_refresh(struct wayland *wayl)
matches_lock(wayl->matches);
render_message(wayl->render, buf);
if (!wayl->conf->hide_prompt) {
- render_prompt(wayl->render, buf, wayl->prompt, wayl->matches);
+ render_prompt(wayl->render, buf, wayl->prompt, wayl->matches,
+ wayl->preedit.text, wayl->preedit.cursor);
+ did_render_prompt = true;
}
if (wayl->hide_when_prompt_empty) {
if (prompt_text(wayl->prompt)[0] != '\0') {
@@ -2623,6 +2925,42 @@ commit:
/* No pending frames - render immediately */
assert(!wayl->need_refresh);
commit_buffer(wayl, buf, false);
+
+ /*
+ * After rendering, send an updated cursor rectangle to any active
+ * text_input so the IME popup tracks the cursor position.
+ * Only send when the cursor rect has actually changed to avoid
+ * generating spurious serial increments.
+ */
+ if (did_render_prompt) {
+ int cx, cy, cw, ch;
+ render_get_cursor_rect(wayl->render, &cx, &cy, &cw, &ch);
+ const float scale = wayl->scale;
+ const int sx = (int)(cx / scale);
+ const int sy = (int)(cy / scale);
+ const int sw = (int)(cw / scale);
+ const int sh = (int)(ch / scale);
+
+ tll_foreach(wayl->seats, it) {
+ struct seat *seat = &it->item;
+ if (seat->text_input == NULL)
+ continue;
+ if (sx == seat->ime.cursor_rect.x &&
+ sy == seat->ime.cursor_rect.y &&
+ sw == seat->ime.cursor_rect.w &&
+ sh == seat->ime.cursor_rect.h)
+ continue;
+ zwp_text_input_v3_set_cursor_rectangle(
+ seat->text_input, sx, sy, sw, sh);
+ zwp_text_input_v3_commit(seat->text_input);
+ seat->ime.serial++;
+ seat->ime.cursor_rect.x = sx;
+ seat->ime.cursor_rect.y = sy;
+ seat->ime.cursor_rect.w = sw;
+ seat->ime.cursor_rect.h = sh;
+ }
+ }
+
done:
time_finish(
@@ -3052,6 +3390,12 @@ wayl_destroy(struct wayland *wayl)
monitor_destroy(&it->item);
tll_free(wayl->monitors);
+ free(wayl->preedit.text);
+ wayl->preedit.text = NULL;
+
+ if (wayl->text_input_manager != NULL)
+ zwp_text_input_manager_v3_destroy(wayl->text_input_manager);
+
if (wayl->single_pixel_manager != NULL)
wp_single_pixel_buffer_manager_v1_destroy(wayl->single_pixel_manager);
if (wayl->color_management_surface != NULL)
diff --git a/wayland.h b/wayland.h
index 528cd36..68cec51 100644
--- a/wayland.h
+++ b/wayland.h
@@ -3,6 +3,8 @@
#include <stdbool.h>
#include <stdint.h>
+struct zwp_text_input_v3; /* text-input-unstable-v3 */
+
#include <fcft/fcft.h>
#include "clipboard.h"
@@ -82,6 +84,20 @@ struct seat {
bool is_pasting;
struct wl_clipboard clipboard;
struct wl_primary primary;
+
+ struct zwp_text_input_v3 *text_input;
+ struct {
+ uint32_t serial; /* incremented on each commit() */
+ struct {
+ char *text; /* pending preedit UTF-8, NULL if none */
+ int32_t cursor_begin; /* byte offset within preedit */
+ int32_t cursor_end; /* byte offset within preedit */
+ } preedit_pending;
+ char *commit_pending; /* pending commit UTF-8, NULL if none */
+ struct {
+ int x, y, w, h; /* last sent cursor rect (surface coords) */
+ } cursor_rect;
+ } ime;
};
|