Codebergdnkl

foot

master
ply5 git calls627 ms32 KiB3 cachedprotocol v2
540 lines15 KiBRaw
1#include "ime.h"
3#if defined(FOOT_IME_ENABLED) && FOOT_IME_ENABLED
5#include <string.h>
7#include "text-input-unstable-v3.h"
9#define LOG_MODULE "ime"
10#define LOG_ENABLE_DBG 0
11#include "log.h"
12#include "char32.h"
13#include "render.h"
14#include "search.h"
15#include "selection.h"
16#include "terminal.h"
17#include "util.h"
18#include "wayland.h"
19#include "xmalloc.h"
21static void
22ime_reset_pending_preedit(struct seat *seat)
24 free(seat->ime.preedit.pending.text);
25 seat->ime.preedit.pending.text = NULL;
28static void
29ime_reset_pending_commit(struct seat *seat)
31 free(seat->ime.commit.pending.text);
32 seat->ime.commit.pending.text = NULL;
35void
36ime_reset_pending(struct seat *seat)
38 ime_reset_pending_preedit(seat);
39 ime_reset_pending_commit(seat);
42void
43ime_reset_preedit(struct seat *seat)
45 if (seat->ime.preedit.cells == NULL)
46 return;
48 free(seat->ime.preedit.text);
49 free(seat->ime.preedit.cells);
50 seat->ime.preedit.text = NULL;
51 seat->ime.preedit.cells = NULL;
52 seat->ime.preedit.count = 0;
55static void
56enter(void *data, struct zwp_text_input_v3 *zwp_text_input_v3,
57 struct wl_surface *surface)
59 if (unlikely(surface == NULL)) {
60 /* Seen on Hyprland */
61 LOG_WARN("compositor sent ime_enter event with a NULL surface");
62 return;
63 }
65 struct seat *seat = data;
66 struct wl_window *win = wl_surface_get_user_data(surface);
67 struct terminal *term = win->term;
69 LOG_DBG("enter: seat=%s, term=%p", seat->name, (const void *)term);
71 if (seat->kbd_focus != term) {
72 LOG_WARN("compositor sent ime::enter() event before the "
73 "corresponding keyboard_enter() event");
74 }
76 /* The main grid is the *only* input-receiving surface we have */
77 seat->ime_focus = term;
79 const struct coord *cursor = &term->grid->cursor.point;
81 term_ime_set_cursor_rect(
82 term,
83 term->margins.left + cursor->col * term->cell_width,
84 term->margins.top + cursor->row * term->cell_height,
85 term->cell_width,
86 term->cell_height);
88 ime_enable(seat);
91static void
92leave(void *data, struct zwp_text_input_v3 *zwp_text_input_v3,
93 struct wl_surface *surface)
95 struct seat *seat = data;
96 LOG_DBG("leave: seat=%s", seat->name);
98 ime_disable(seat);
99 seat->ime_focus = NULL;
102static void
103preedit_string(void *data, struct zwp_text_input_v3 *zwp_text_input_v3,
104 const char *text, int32_t cursor_begin, int32_t cursor_end)
106 LOG_DBG("preedit-string: text=%s, begin=%d, end=%d", text, cursor_begin, cursor_end);
108 struct seat *seat = data;
110 ime_reset_pending_preedit(seat);
112 if (text != NULL) {
113 seat->ime.preedit.pending.text = xstrdup(text);
114 seat->ime.preedit.pending.cursor_begin = cursor_begin;
115 seat->ime.preedit.pending.cursor_end = cursor_end;
116 }
119static void
120commit_string(void *data, struct zwp_text_input_v3 *zwp_text_input_v3,
121 const char *text)
123 LOG_DBG("commit: text=%s", text);
125 struct seat *seat = data;
127 ime_reset_pending_commit(seat);
129 if (text != NULL)
130 seat->ime.commit.pending.text = xstrdup(text);
133static void
134delete_surrounding_text(void *data, struct zwp_text_input_v3 *zwp_text_input_v3,
135 uint32_t before_length, uint32_t after_length)
137 LOG_DBG("delete-surrounding: before=%d, after=%d", before_length, after_length);
139 struct seat *seat = data;
140 seat->ime.surrounding.pending.before_length = before_length;
141 seat->ime.surrounding.pending.after_length = after_length;
144static void
145done(void *data, struct zwp_text_input_v3 *zwp_text_input_v3,
146 uint32_t serial)
148 /*
149 * From text-input-unstable-v3.h:
150 *
151 * The application must proceed by evaluating the changes in the
152 * following order:
153 *
154 * 1. Replace existing preedit string with the cursor.
155 * 2. Delete requested surrounding text.
156 * 3. Insert commit string with the cursor at its end.
157 * 4. Calculate surrounding text to send.
158 * 5. Insert new preedit text in cursor position.
159 * 6. Place cursor inside preedit text.
160 */
162 LOG_DBG("done: serial=%u", serial);
163 struct seat *seat = data;
164 struct terminal *term = seat->ime_focus;
166 if (seat->ime.serial != serial) {
167 LOG_DBG("IME serial mismatch: expected=0x%08x, got 0x%08x",
168 seat->ime.serial, serial);
169 return;
170 }
172 if (term == NULL) {
173 static bool have_warned = false;
174 if (!have_warned) {
175 LOG_WARN(
176 "%s: text-input::done() received on seat that isn't "
177 "focusing a terminal window", seat->name);
178 have_warned = true;
179 }
180 }
182 /* 1. Delete existing pre-edit text */
183 if (seat->ime.preedit.cells != NULL) {
184 ime_reset_preedit(seat);
186 if (term != NULL) {
187 if (term->is_searching)
188 render_refresh_search(term);
189 else {
190 term_reset_view(term);
191 selection_cancel(term);
192 render_refresh(term);
193 }
194 }
195 }
197 /*
198 * 2. Delete requested surrounding text
199 *
200 * We don't support deleting surrounding text. But, we also never
201 * call set_surrounding_text() so hopefully we should never
202 * receive any requests to delete surrounding text.
203 */
205 /* 3. Insert commit string */
206 if (seat->ime.commit.pending.text != NULL) {
207 const char *text = seat->ime.commit.pending.text;
208 size_t len = strlen(text);
210 if (term != NULL) {
211 if (term->is_searching) {
212 search_add_chars(term, text, len);
213 search_find_next(term, SEARCH_BACKWARD_SAME_POSITION);
214 render_refresh_search(term);
215 } else {
216 term_to_slave(term, text, len);
217 term_reset_view(term);
218 selection_cancel(term);
219 render_refresh(term);
220 }
221 }
222 ime_reset_pending_commit(seat);
223 }
225 /* 4. Calculate surrounding text to send - not supported */
227 /* 5. Insert new pre-edit text */
228 char32_t *allocated_preedit_text = NULL;
230 if (seat->ime.preedit.pending.text == NULL ||
231 seat->ime.preedit.pending.text[0] == '\0' ||
232 (allocated_preedit_text = ambstoc32(seat->ime.preedit.pending.text)) == NULL)
233 {
234 ime_reset_pending_preedit(seat);
235 return;
236 }
238 xassert(seat->ime.preedit.pending.text != NULL);
239 xassert(allocated_preedit_text != NULL);
241 seat->ime.preedit.text = allocated_preedit_text;
243 size_t wchars = c32len(seat->ime.preedit.text);
245 /* Next, count number of cells needed */
246 size_t cell_count = 0;
247 size_t widths[wchars + 1];
249 for (size_t i = 0; i < wchars; i++) {
250 int width = max(c32width(seat->ime.preedit.text[i]), 1);
251 widths[i] = width;
252 cell_count += width;
253 }
255 /* Allocate cells */
256 seat->ime.preedit.cells = xmalloc(
257 cell_count * sizeof(seat->ime.preedit.cells[0]));
258 seat->ime.preedit.count = cell_count;
260 /* Populate cells */
261 for (size_t i = 0, cell_idx = 0; i < wchars; i++) {
262 struct cell *cell = &seat->ime.preedit.cells[cell_idx];
264 int width = widths[i];
266 cell->wc = seat->ime.preedit.text[i];
267 cell->attrs = (struct attributes){.clean = 0};
269 for (int j = 1; j < width; j++) {
270 cell = &seat->ime.preedit.cells[cell_idx + j];
271 cell->wc = CELL_SPACER + width - j;
272 cell->attrs = (struct attributes){.clean = 1};
273 }
275 cell_idx += width;
276 }
278 const size_t byte_len = strlen(seat->ime.preedit.pending.text);
280 /* Pre-edit cursor - hidden */
281 if (seat->ime.preedit.pending.cursor_begin == -1 ||
282 seat->ime.preedit.pending.cursor_end == -1)
283 {
284 /* Note: docs says *both* begin and end should be -1,
285 * but what else can we do if only one is -1? */
286 LOG_DBG("pre-edit cursor is hidden");
287 seat->ime.preedit.cursor.hidden = true;
288 seat->ime.preedit.cursor.start = -1;
289 seat->ime.preedit.cursor.end = -1;
290 }
292 else if (seat->ime.preedit.pending.cursor_begin == byte_len &&
293 seat->ime.preedit.pending.cursor_end == byte_len)
294 {
295 /* Cursor is *after* the entire pre-edit string */
296 seat->ime.preedit.cursor.hidden = false;
297 seat->ime.preedit.cursor.start = cell_count;
298 seat->ime.preedit.cursor.end = cell_count;
299 }
301 else {
302 /*
303 * Translate cursor position to cell indices
304 *
305 * The cursor_begin and cursor_end are counted in
306 * *bytes*. We want to map them to *cell* indices.
307 *
308 * To do this, we use mblen() to step though the utf-8
309 * pre-edit string, advancing a unicode character index as
310 * we go, *and* advancing a *cell* index using c32width()
311 * of the unicode character.
312 *
313 * When we find the matching *byte* index, we at the same
314 * time know both the unicode *and* cell index.
315 */
317 int cell_begin = -1, cell_end = -1;
318 for (size_t byte_idx = 0, wc_idx = 0, cell_idx = 0;
319 byte_idx < byte_len &&
320 wc_idx < wchars &&
321 cell_idx < cell_count &&
322 (cell_begin < 0 || cell_end < 0);
323 cell_idx += widths[wc_idx], wc_idx++)
324 {
325 if (seat->ime.preedit.pending.cursor_begin == byte_idx)
326 cell_begin = cell_idx;
327 if (seat->ime.preedit.pending.cursor_end == byte_idx)
328 cell_end = cell_idx;
330 /* Number of bytes of *next* utf-8 character */
331 size_t left = byte_len - byte_idx;
332 int wc_bytes = mblen(&seat->ime.preedit.pending.text[byte_idx], left);
334 if (wc_bytes <= 0)
335 break;
337 byte_idx += wc_bytes;
338 }
340 if (seat->ime.preedit.pending.cursor_end >= byte_len)
341 cell_end = cell_count;
343 /* Bounded by number of screen columns */
344 cell_begin = min(max(cell_begin, 0), cell_count - 1);
345 cell_end = min(max(cell_end, 0), cell_count);
347 if (cell_end < cell_begin)
348 cell_end = cell_begin;
350 /* Expand cursor end to end of glyph */
351 while (cell_end > cell_begin && cell_end < cell_count &&
352 seat->ime.preedit.cells[cell_end].wc >= CELL_SPACER)
353 {
354 cell_end++;
355 }
357 LOG_DBG("pre-edit cursor: begin=%d, end=%d", cell_begin, cell_end);
359 xassert(cell_begin >= 0);
360 xassert(cell_begin < cell_count);
361 xassert(cell_begin <= cell_end);
362 xassert(cell_end >= 0);
363 xassert(cell_end <= cell_count);
365 seat->ime.preedit.cursor.hidden = false;
366 seat->ime.preedit.cursor.start = cell_begin;
367 seat->ime.preedit.cursor.end = cell_end;
368 }
370 /* Underline pre-edit string that is *not* covered by the cursor */
371 bool hidden = seat->ime.preedit.cursor.hidden;
372 int start = seat->ime.preedit.cursor.start;
373 int end = seat->ime.preedit.cursor.end;
375 for (size_t i = 0, cell_idx = 0; i < wchars; cell_idx += widths[i], i++) {
376 if (hidden || start == end || cell_idx < start || cell_idx >= end) {
377 struct cell *cell = &seat->ime.preedit.cells[cell_idx];
378 cell->attrs.underline = true;
379 }
380 }
382 ime_reset_pending_preedit(seat);
384 if (term != NULL) {
385 if (term->is_searching)
386 render_refresh_search(term);
387 else
388 render_refresh(term);
389 }
392static void
393ime_send_cursor_rect(struct seat *seat)
395 if (unlikely(seat->wayl->text_input_manager == NULL))
396 return;
398 if (seat->ime_focus == NULL)
399 return;
401 struct terminal *term = seat->ime_focus;
403 if (!term->ime_enabled)
404 return;
406 if (seat->ime.cursor_rect.pending.x == seat->ime.cursor_rect.sent.x &&
407 seat->ime.cursor_rect.pending.y == seat->ime.cursor_rect.sent.y &&
408 seat->ime.cursor_rect.pending.width == seat->ime.cursor_rect.sent.width &&
409 seat->ime.cursor_rect.pending.height == seat->ime.cursor_rect.sent.height)
410 {
411 return;
412 }
414 zwp_text_input_v3_set_cursor_rectangle(
415 seat->wl_text_input,
416 seat->ime.cursor_rect.pending.x / term->scale,
417 seat->ime.cursor_rect.pending.y / term->scale,
418 seat->ime.cursor_rect.pending.width / term->scale,
419 seat->ime.cursor_rect.pending.height / term->scale);
421 zwp_text_input_v3_commit(seat->wl_text_input);
422 seat->ime.serial++;
424 seat->ime.cursor_rect.sent = seat->ime.cursor_rect.pending;
427void
428ime_enable(struct seat *seat)
430 if (unlikely(seat->wayl->text_input_manager == NULL))
431 return;
433 if (seat->ime_focus == NULL)
434 return;
436 struct terminal *term = seat->ime_focus;
437 if (term == NULL)
438 return;
440 if (!term->ime_enabled)
441 return;
443 ime_reset_pending(seat);
444 ime_reset_preedit(seat);
446 zwp_text_input_v3_enable(seat->wl_text_input);
447 zwp_text_input_v3_set_content_type(
448 seat->wl_text_input,
449 ZWP_TEXT_INPUT_V3_CONTENT_HINT_NONE,
450 ZWP_TEXT_INPUT_V3_CONTENT_PURPOSE_TERMINAL);
452 zwp_text_input_v3_set_cursor_rectangle(
453 seat->wl_text_input,
454 seat->ime.cursor_rect.pending.x / term->scale,
455 seat->ime.cursor_rect.pending.y / term->scale,
456 seat->ime.cursor_rect.pending.width / term->scale,
457 seat->ime.cursor_rect.pending.height / term->scale);
459 seat->ime.cursor_rect.sent = seat->ime.cursor_rect.pending;
461 zwp_text_input_v3_commit(seat->wl_text_input);
462 seat->ime.serial++;
465void
466ime_disable(struct seat *seat)
468 if (unlikely(seat->wayl->text_input_manager == NULL))
469 return;
471 if (seat->ime_focus == NULL)
472 return;
474 ime_reset_pending(seat);
475 ime_reset_preedit(seat);
477 zwp_text_input_v3_disable(seat->wl_text_input);
478 zwp_text_input_v3_commit(seat->wl_text_input);
479 seat->ime.serial++;
482void
483ime_update_cursor_rect(struct seat *seat)
485 struct terminal *term = seat->ime_focus;
487 /* Set in render_ime_preedit() */
488 if (seat->ime.preedit.cells != NULL)
489 goto update;
491 /* Set in render_search_box() */
492 if (term->is_searching)
493 goto update;
495 int x, y, width, height;
496 int col = term->grid->cursor.point.col;
497 int row = term->grid->cursor.point.row;
498 row += term->grid->offset;
499 row -= term->grid->view;
500 row &= term->grid->num_rows - 1;
501 x = term->margins.left + col * term->cell_width;
502 y = term->margins.top + row * term->cell_height;
504 if (term->cursor_style == CURSOR_BEAM)
505 width = 1;
506 else
507 width = term->cell_width;
509 height = term->cell_height;
511 seat->ime.cursor_rect.pending.x = x;
512 seat->ime.cursor_rect.pending.y = y;
513 seat->ime.cursor_rect.pending.width = width;
514 seat->ime.cursor_rect.pending.height = height;
516update:
517 ime_send_cursor_rect(seat);
520const struct zwp_text_input_v3_listener text_input_listener = {
521 .enter = &enter,
522 .leave = &leave,
523 .preedit_string = &preedit_string,
524 .commit_string = &commit_string,
525 .delete_surrounding_text = &delete_surrounding_text,
526 .done = &done,
527};
529#else /* !FOOT_IME_ENABLED */
531void ime_enable(struct seat *seat) {}
532void ime_disable(struct seat *seat) {}
533void ime_update_cursor_rect(struct seat *seat) {}
535void ime_reset_pending_preedit(struct seat *seat) {}
536void ime_reset_pending_commit(struct seat *seat) {}
537void ime_reset_pending(struct seat *seat) {}
538void ime_reset_preedit(struct seat *seat) {}
540#endif
Diagnostics5 git calls · 627 ms
CallStatusSizeTime0627 ms
discover200178 B2 ms
git.upload-pack discover
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=2
git.response.bytes=178
git.server.agent=git/2.56.1-Linux
git.server.object_format=sha1
git.server.caps=agent ls-refs fetch server-option object-format
git.server.fetch=shallow wait-for-done filter
git.protocol.version=2
git.pkt.frames=9
git.pkt.data=7
git.pkt.flush=2
git.pkt.delim=0
git.pkt.bytes=178
git.pkt.payload.max=35
ls-refs20010 KiB · hit2 ms
git.upload-pack ls-refs
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=2
git.response.bytes=10322
git.response.cache=HIT
git.response.duration_ms=2
git.protocol.version=2
git.pkt.frames=108
git.pkt.data=107
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=10322
git.pkt.payload.max=106
git.ls_refs.count=107
git.ls_refs.head=refs/heads/master
fetchtree:0 · deepen 102009.3 KiB · hit2 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=2
git.response.bytes=9489
git.response.cache=HIT
git.response.duration_ms=2
git.fetch.filter=tree:0
git.fetch.wants=c0d2c3e95d74cb60526ff28aec9f34ee3df545e0
git.fetch.haves.count=0
git.fetch.deepen=10
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=7
git.pkt.data=5
git.pkt.flush=1
git.pkt.delim=1
git.pkt.bytes=9489
git.pkt.payload.max=9389
git.sideband.band1.bytes=9389
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=10
git.pack.bytes=9389
git.pack.ofs_delta=0
git.pack.ref_delta=0
git.pack.chain.max=0
git.pack.trailer=de930681b6073ba5b599a0f956bfbd114762ad5a
git.pack.full=10
Open pack map
fetchcombine:blob:none+tree:2 · deepen 12008.6 KiB · hit3 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=3
git.response.bytes=8830
git.response.cache=HIT
git.response.duration_ms=3
git.fetch.filter=combine:blob:none+tree:2
git.fetch.wants=c0d2c3e95d74cb60526ff28aec9f34ee3df545e0
git.fetch.haves.count=0
git.fetch.deepen=1
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=7
git.pkt.data=5
git.pkt.flush=1
git.pkt.delim=1
git.pkt.bytes=8830
git.pkt.payload.max=8730
git.sideband.band1.bytes=8730
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=14
git.pack.bytes=8730
git.fetch.wide.objects=14
git.fetch.wide.extra_bytes=9909
git.fetch.wide.blobs=0
fetch2003.7 KiB622 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=622
git.response.bytes=3760
git.response.cache=MISS
git.response.duration_ms=622
git.fetch.wants=d2fecd9bb37574b8b91441d47a1083d721f9262a
git.fetch.haves.count=0
git.fetch.deepen=0
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=4
git.pkt.data=3
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=3760
git.pkt.payload.max=3733
git.sideband.band1.bytes=3733
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0

Each row is one HTTP round trip ply made to the git server for this page, on one timeline. discover asks what the server speaks, ls-refs lists branch tips, fetch brings objects by id. A hit was served by Workers Cache. Tap a row for its wire details.

Remote
https://codeberg.org/dnkl/foot
Ref
refs/heads/master
Commit
c0d2c3e95d74cb60526ff28aec9f34ee3df545e0
cf-ray
a5e1f9e02c8124ce
Request
30de71dd…
Protocol
v2 · sha1
Agent
git/2.56.1-Linux
Filter
advertised
Fetch
shallow wait-for-done filter
Capabilities
agent ls-refs fetch server-option object-format