From 148bb1ff136ee225aaa93ff9b40700feb81503c5 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sat, 10 Oct 2020 22:14:35 +0200 Subject: ime: wip: add text-input object to seat --- ime.c | 97 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 ime.c (limited to 'ime.c') diff --git a/ime.c b/ime.c new file mode 100644 index 00000000..baac7db5 --- /dev/null +++ b/ime.c @@ -0,0 +1,97 @@ +#include "ime.h" + +#include "text-input-unstable-v3.h" + +#define LOG_MODULE "ime" +#define LOG_ENABLE_DBG 1 +#include "log.h" +#include "terminal.h" +#include "wayland.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); + + assert(seat->kbd_focus != NULL); + + switch (term_surface_kind(seat->kbd_focus, surface)) { + case TERM_SURF_GRID: + 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); + + /* TODO: set cursor rectangle */ + zwp_text_input_v3_set_cursor_rectangle(seat->wl_text_input, 0, 0, 15, 15); + break; + + case TERM_SURF_SEARCH: + /* TODO */ + /* FALLTHROUGH */ + + case TERM_SURF_NONE: + case TERM_SURF_SCROLLBACK_INDICATOR: + case TERM_SURF_RENDER_TIMER: + case TERM_SURF_TITLE: + case TERM_SURF_BORDER_LEFT: + case TERM_SURF_BORDER_RIGHT: + case TERM_SURF_BORDER_TOP: + case TERM_SURF_BORDER_BOTTOM: + case TERM_SURF_BUTTON_MINIMIZE: + case TERM_SURF_BUTTON_MAXIMIZE: + case TERM_SURF_BUTTON_CLOSE: + zwp_text_input_v3_disable(seat->wl_text_input); + break; + } + + zwp_text_input_v3_commit(seat->wl_text_input); +} + +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); +} + +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-strig: text=%s, begin=%d, end=%d", text, cursor_begin, 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); +} + +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); +} + +static void +done(void *data, struct zwp_text_input_v3 *zwp_text_input_v3, + uint32_t serial) +{ + LOG_DBG("done: serial=%u", serial); +} + +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, +}; -- cgit v1.3 From e97024116e0efcf956fd7504aeee70bd3ee0d999 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sat, 28 Nov 2020 21:59:58 +0100 Subject: ime: disable text-input on surface leave --- ime.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'ime.c') diff --git a/ime.c b/ime.c index baac7db5..1619e584 100644 --- a/ime.c +++ b/ime.c @@ -48,7 +48,7 @@ enter(void *data, struct zwp_text_input_v3 *zwp_text_input_v3, break; } - zwp_text_input_v3_commit(seat->wl_text_input); + zwp_text_input_v3_commit(seat->wl_text_input); } static void @@ -57,13 +57,15 @@ leave(void *data, struct zwp_text_input_v3 *zwp_text_input_v3, { struct seat *seat = data; LOG_DBG("leave: seat=%s", seat->name); + zwp_text_input_v3_disable(seat->wl_text_input); + zwp_text_input_v3_commit(seat->wl_text_input); } 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-strig: text=%s, begin=%d, end=%d", text, cursor_begin, cursor_end); + LOG_DBG("preedit-string: text=%s, begin=%d, end=%d", text, cursor_begin, cursor_end); } static void -- cgit v1.3 From b85e2f40bbc45709e8f47a435ff458f054832a99 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sat, 28 Nov 2020 22:00:26 +0100 Subject: ime: feed committed string to client application --- ime.c | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'ime.c') diff --git a/ime.c b/ime.c index 1619e584..eda975c5 100644 --- a/ime.c +++ b/ime.c @@ -1,5 +1,7 @@ #include "ime.h" +#include + #include "text-input-unstable-v3.h" #define LOG_MODULE "ime" @@ -72,7 +74,9 @@ static void commit_string(void *data, struct zwp_text_input_v3 *zwp_text_input_v3, const char *text) { + struct seat *seat = data; LOG_DBG("commit: text=%s", text); + term_to_slave(seat->kbd_focus, text, strlen(text)); } static void -- cgit v1.3 From 5745c610ac5e9b96f9bac8347c6a5645d89a66c6 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Tue, 1 Dec 2020 19:31:49 +0100 Subject: ime: wip: commit all changes in ‘done()’ MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ime.c | 42 ++++++++++++++++++++++++++++++++++++++++-- input.c | 4 +++- vt.c | 2 +- wayland.c | 2 ++ wayland.h | 20 ++++++++++++++++++-- 5 files changed, 64 insertions(+), 6 deletions(-) (limited to 'ime.c') diff --git a/ime.c b/ime.c index eda975c5..9bcf0a9f 100644 --- a/ime.c +++ b/ime.c @@ -9,6 +9,7 @@ #include "log.h" #include "terminal.h" #include "wayland.h" +#include "xmalloc.h" static void enter(void *data, struct zwp_text_input_v3 *zwp_text_input_v3, @@ -51,6 +52,7 @@ enter(void *data, struct zwp_text_input_v3 *zwp_text_input_v3, } zwp_text_input_v3_commit(seat->wl_text_input); + seat->ime.serial++; } static void @@ -61,6 +63,7 @@ leave(void *data, struct zwp_text_input_v3 *zwp_text_input_v3, LOG_DBG("leave: seat=%s", seat->name); zwp_text_input_v3_disable(seat->wl_text_input); zwp_text_input_v3_commit(seat->wl_text_input); + seat->ime.serial++; } static void @@ -68,15 +71,23 @@ 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; + + free(seat->ime.preedit.text); + seat->ime.preedit.text = text != NULL ? xstrdup(text) : NULL; + seat->ime.preedit.cursor_begin = cursor_begin; + seat->ime.preedit.cursor_end = cursor_end; } static void commit_string(void *data, struct zwp_text_input_v3 *zwp_text_input_v3, const char *text) { - struct seat *seat = data; LOG_DBG("commit: text=%s", text); - term_to_slave(seat->kbd_focus, text, strlen(text)); + struct seat *seat = data; + free(seat->ime.commit.text); + seat->ime.commit.text = text != NULL ? xstrdup(text) : NULL; + //term_to_slave(seat->kbd_focus, text, strlen(text)); } static void @@ -84,13 +95,40 @@ 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.before_length = before_length; + seat->ime.surrounding.after_length = after_length; } static void done(void *data, struct zwp_text_input_v3 *zwp_text_input_v3, uint32_t serial) { + /* + * 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; + + if (seat->ime.serial != serial) + LOG_WARN("IME serial mismatch: expected=0x%08x, got 0x%08x", + seat->ime.serial, serial); + + assert(seat->kbd_focus); + + if (seat->ime.commit.text != NULL) + term_to_slave(seat->kbd_focus, seat->ime.commit.text, strlen(seat->ime.commit.text)); + + free(seat->ime.commit.text); + seat->ime.commit.text = NULL; } const struct zwp_text_input_v3_listener text_input_listener = { diff --git a/input.c b/input.c index 31c1d816..4abbb89e 100644 --- a/input.c +++ b/input.c @@ -21,7 +21,7 @@ #include #define LOG_MODULE "input" -#define LOG_ENABLE_DBG 0 +#define LOG_ENABLE_DBG 1 #include "log.h" #include "config.h" #include "commands.h" @@ -752,6 +752,8 @@ keymap_lookup(struct seat *seat, struct terminal *term, const enum keypad_keys keypad_keys_mode = term->num_lock_modifier ? KEYPAD_NUMERICAL : term->keypad_keys_mode; + log_dbg("keypad mode: %d, num-lock=%d", keypad_keys_mode, seat->kbd.num); + for (size_t j = 0; j < count; j++) { if (info[j].modifiers != MOD_ANY && info[j].modifiers != mods) continue; diff --git a/vt.c b/vt.c index ed26da73..1522dacf 100644 --- a/vt.c +++ b/vt.c @@ -408,7 +408,7 @@ action_esc_dispatch(struct terminal *term, uint8_t final) case '=': term->keypad_keys_mode = KEYPAD_APPLICATION; break; - +ö case '>': term->keypad_keys_mode = KEYPAD_NUMERICAL; break; diff --git a/wayland.c b/wayland.c index 2f7b3b95..ecaecb1c 100644 --- a/wayland.c +++ b/wayland.c @@ -191,6 +191,8 @@ seat_destroy(struct seat *seat) free(seat->clipboard.text); free(seat->primary.text); + free(seat->ime.preedit.text); + free(seat->ime.commit.text); free(seat->name); } diff --git a/wayland.h b/wayland.h index e006b395..b29bb8ab 100644 --- a/wayland.h +++ b/wayland.h @@ -223,8 +223,24 @@ struct seat { /* Input Method Editor */ struct zwp_text_input_v3 *wl_text_input; - //struct { - //} ime; + struct { + struct { + char *text; + int32_t cursor_begin; + int32_t cursor_end; + } preedit; + + struct { + char *text; + } commit; + + struct { + uint32_t before_length; + uint32_t after_length; + } surrounding; + + uint32_t serial; + } ime; }; enum csd_surface { -- cgit v1.3 From 8c3d48c5cdb2ae4e8bdbdc48f155f53d1a92973b Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Wed, 2 Dec 2020 18:52:50 +0100 Subject: ime: render pre-edit text MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This is done by allocating cells for the pre-edit text when receiving the text-input::done() call, and populating them by converting the utf-8 formatted pre-edit text to wchars. We also convert the pre-edit cursor position to cell positions (it can cover multiple cells). When rendering, we simply render the pre-edit cells on-top off the regular grid. While doing so, we also mark the underlying, “real”, cells as dirty, to ensure they are re-rendered when the pre-edit text is modified or removed. --- ime.c | 209 ++++++++++++++++++++++++++++++++++++++++++++++++++++++------- input.c | 4 +- render.c | 146 ++++++++++++++++++++++++++++++++++++------ terminal.c | 9 +++ terminal.h | 13 ++++ vt.c | 2 +- wayland.c | 4 +- wayland.h | 21 +++++-- 8 files changed, 356 insertions(+), 52 deletions(-) (limited to 'ime.c') diff --git a/ime.c b/ime.c index 9bcf0a9f..787985cf 100644 --- a/ime.c +++ b/ime.c @@ -5,9 +5,11 @@ #include "text-input-unstable-v3.h" #define LOG_MODULE "ime" -#define LOG_ENABLE_DBG 1 +#define LOG_ENABLE_DBG 0 #include "log.h" +#include "render.h" #include "terminal.h" +#include "util.h" #include "wayland.h" #include "xmalloc.h" @@ -64,6 +66,12 @@ leave(void *data, struct zwp_text_input_v3 *zwp_text_input_v3, zwp_text_input_v3_disable(seat->wl_text_input); zwp_text_input_v3_commit(seat->wl_text_input); seat->ime.serial++; + + free(seat->ime.preedit.pending.text); + seat->ime.preedit.pending.text = NULL; + + free(seat->ime.commit.pending.text); + seat->ime.commit.pending.text = NULL; } static void @@ -71,12 +79,13 @@ 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; - free(seat->ime.preedit.text); - seat->ime.preedit.text = text != NULL ? xstrdup(text) : NULL; - seat->ime.preedit.cursor_begin = cursor_begin; - seat->ime.preedit.cursor_end = cursor_end; + free(seat->ime.preedit.pending.text); + seat->ime.preedit.pending.text = text != NULL ? xstrdup(text) : NULL; + seat->ime.preedit.pending.cursor_begin = cursor_begin; + seat->ime.preedit.pending.cursor_end = cursor_end; } static void @@ -84,10 +93,10 @@ 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; - free(seat->ime.commit.text); - seat->ime.commit.text = text != NULL ? xstrdup(text) : NULL; - //term_to_slave(seat->kbd_focus, text, strlen(text)); + free(seat->ime.commit.pending.text); + seat->ime.commit.pending.text = text != NULL ? xstrdup(text) : NULL; } static void @@ -95,9 +104,10 @@ 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.before_length = before_length; - seat->ime.surrounding.after_length = after_length; + seat->ime.surrounding.pending.before_length = before_length; + seat->ime.surrounding.pending.after_length = after_length; } static void @@ -105,30 +115,185 @@ 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. + * 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; - if (seat->ime.serial != serial) - LOG_WARN("IME serial mismatch: expected=0x%08x, got 0x%08x", - seat->ime.serial, serial); + if (seat->ime.serial != serial) { + LOG_DBG("IME serial mismatch: expected=0x%08x, got 0x%08x", + seat->ime.serial, serial); + return; + } assert(seat->kbd_focus); + struct terminal *term = seat->kbd_focus; + + /* 1. Delete existing pre-edit text */ + if (term->ime.preedit.cells != NULL) { + free(term->ime.preedit.cells); + term->ime.preedit.cells = NULL; + term->ime.preedit.count = 0; + 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) { + term_to_slave( + term, + seat->ime.commit.pending.text, + strlen(seat->ime.commit.pending.text)); + + free(seat->ime.commit.pending.text); + seat->ime.commit.pending.text = NULL; + } + + /* 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) { + /* First, convert to unicode */ + wchar_t wcs[wchars + 1]; + mbstowcs(wcs, seat->ime.preedit.pending.text, wchars); - if (seat->ime.commit.text != NULL) - term_to_slave(seat->kbd_focus, seat->ime.commit.text, strlen(seat->ime.commit.text)); + /* Next, count number of cells needed */ + size_t cell_count = 0; + for (size_t i = 0; i < wchars; i++) + cell_count += max(wcwidth(wcs[i]), 1); + + /* Allocate cells */ + term->ime.preedit.cells = xmalloc( + cell_count * sizeof(term->ime.preedit.cells[0])); + term->ime.preedit.count = cell_count; + + /* Populate cells */ + for (size_t i = 0, cell_idx = 0; i < wchars; i++) { + struct cell *cell = &term->ime.preedit.cells[cell_idx]; + + int width = max(wcwidth(wcs[i]), 1); + + cell->wc = wcs[i]; + cell->attrs = (struct attributes){.clean = 0, .underline = 1}; + + for (int j = 1; j < width; j++) { + cell = &term->ime.preedit.cells[cell_idx + j]; + cell->wc = CELL_MULT_COL_SPACER; + cell->attrs = (struct attributes){.clean = 1}; + } + + cell_idx += width; + } + + /* 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"); + term->ime.preedit.cursor.hidden = true; + term->ime.preedit.cursor.start = -1; + term->ime.preedit.cursor.end = -1; + } + + 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. + * + * Note that this has only been tested with + * + * cursor_begin == cursor_end == 0 + * + * I haven't found an IME that requests anything else + */ + + const size_t byte_len = strlen(seat->ime.preedit.pending.text); + + 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); + ) + { + 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; + cell_idx += max(wcwidth(term->ime.preedit.cells[wc_idx].wc), 1); + wc_idx++; + } + + /* Bounded by number of screen columns */ + cell_begin = min(max(cell_begin, 0), cell_count - 1); + + /* Ensure end comes *after* begin, and is bounded by screen */ + if (cell_end <= cell_begin) + cell_end = cell_begin + max(wcwidth(term->ime.preedit.cells[cell_begin].wc), 1); + cell_end = min(max(cell_end, 0), cell_count); + + LOG_DBG("pre-edit cursor: begin=%d, end=%d", cell_begin, cell_end); + + assert(cell_begin >= 0); + assert(cell_begin < cell_count); + assert(cell_end >= 1); + assert(cell_end <= cell_count); + assert(cell_begin < cell_end); + + term->ime.preedit.cursor.hidden = false; + term->ime.preedit.cursor.start = cell_begin; + term->ime.preedit.cursor.end = cell_end; + } + + render_refresh(term); + } - free(seat->ime.commit.text); - seat->ime.commit.text = NULL; + free(seat->ime.preedit.pending.text); + seat->ime.preedit.pending.text = NULL; } const struct zwp_text_input_v3_listener text_input_listener = { diff --git a/input.c b/input.c index 4abbb89e..81129edc 100644 --- a/input.c +++ b/input.c @@ -21,7 +21,7 @@ #include #define LOG_MODULE "input" -#define LOG_ENABLE_DBG 1 +#define LOG_ENABLE_DBG 0 #include "log.h" #include "config.h" #include "commands.h" @@ -752,7 +752,7 @@ keymap_lookup(struct seat *seat, struct terminal *term, const enum keypad_keys keypad_keys_mode = term->num_lock_modifier ? KEYPAD_NUMERICAL : term->keypad_keys_mode; - log_dbg("keypad mode: %d, num-lock=%d", keypad_keys_mode, seat->kbd.num); + LOG_DBG("keypad mode: %d, num-lock=%d", keypad_keys_mode, seat->kbd.num); for (size_t j = 0; j < count; j++) { if (info[j].modifiers != MOD_ANY && info[j].modifiers != mods) diff --git a/render.c b/render.c index dc926cc4..88a47d47 100644 --- a/render.c +++ b/render.c @@ -314,42 +314,49 @@ draw_strikeout(const struct terminal *term, pixman_image_t *pix, } static void -draw_cursor(const struct terminal *term, const struct cell *cell, - const struct fcft_font *font, pixman_image_t *pix, pixman_color_t *fg, - const pixman_color_t *bg, int x, int y, int cols) +cursor_colors_for_cell(const struct terminal *term, const struct cell *cell, + const pixman_color_t *fg, const pixman_color_t *bg, + pixman_color_t *cursor_color, pixman_color_t *text_color) { - pixman_color_t cursor_color; - pixman_color_t text_color; - bool is_selected = cell->attrs.selected; if (term->cursor_color.cursor >> 31) { - cursor_color = color_hex_to_pixman(term->cursor_color.cursor); - text_color = color_hex_to_pixman( + *cursor_color = color_hex_to_pixman(term->cursor_color.cursor); + *text_color = color_hex_to_pixman( term->cursor_color.text >> 31 ? term->cursor_color.text : term->colors.bg); if (term->reverse ^ cell->attrs.reverse ^ is_selected) { - pixman_color_t swap = cursor_color; - cursor_color = text_color; - text_color = swap; + pixman_color_t swap = *cursor_color; + *cursor_color = *text_color; + *text_color = swap; } if (term->is_searching && !is_selected) { - color_dim_for_search(&cursor_color); - color_dim_for_search(&text_color); + color_dim_for_search(cursor_color); + color_dim_for_search(text_color); } } else { - cursor_color = *fg; - text_color = *bg; + *cursor_color = *fg; + *text_color = *bg; } +} + +static void +draw_cursor(const struct terminal *term, const struct cell *cell, + const struct fcft_font *font, pixman_image_t *pix, pixman_color_t *fg, + const pixman_color_t *bg, int x, int y, int cols) +{ + pixman_color_t cursor_color; + pixman_color_t text_color; + cursor_colors_for_cell(term, cell, fg, bg, &cursor_color, &text_color); switch (term->cursor_style) { case CURSOR_BLOCK: - if (!term->kbd_focus) + if (unlikely(!term->kbd_focus || term->ime.preedit.cells != NULL)) draw_unfocused_block(term, pix, &cursor_color, x, y, cols); - else if (term->cursor_blink.state == CURSOR_BLINK_ON) { + else if (likely(term->cursor_blink.state == CURSOR_BLINK_ON)) { *fg = text_color; pixman_image_fill_rectangles( PIXMAN_OP_SRC, pix, &cursor_color, 1, @@ -358,12 +365,17 @@ draw_cursor(const struct terminal *term, const struct cell *cell, break; case CURSOR_BAR: - if (term->cursor_blink.state == CURSOR_BLINK_ON || !term->kbd_focus) + if (likely(term->cursor_blink.state == CURSOR_BLINK_ON || + !term->kbd_focus)) + { draw_bar(term, pix, font, &cursor_color, x, y); + } break; case CURSOR_UNDERLINE: - if (term->cursor_blink.state == CURSOR_BLINK_ON || !term->kbd_focus) { + if (likely(term->cursor_blink.state == CURSOR_BLINK_ON || + !term->kbd_focus)) + { draw_underline( term, pix, attrs_to_font(term, &cell->attrs), &cursor_color, x, y, cols); @@ -1009,6 +1021,99 @@ render_sixel_images(struct terminal *term, pixman_image_t *pix) } } +static void +render_ime_preedit(struct terminal *term, struct buffer *buf, + struct coord cursor) +{ + if (likely(term->ime.preedit.cells == NULL)) + return; + + int cells_needed = term->ime.preedit.count; + + int row_idx = cursor.row; + int col_idx = cursor.col; + + int cells_left = term->cols - cursor.col; + int cells_used = min(cells_needed, term->cols); + + /* Adjust start of pre-edit text to the left if string doesn't fit on row */ + if (cells_left < cells_used) + col_idx -= cells_used - cells_left; + + assert(col_idx >= 0); + assert(col_idx < term->cols); + + struct row *row = grid_row_in_view(term->grid, row_idx); + + /* Don't start pre-edit text in the middle of a double-width character */ + while (col_idx > 0 && row->cells[col_idx].wc == CELL_MULT_COL_SPACER) { + cells_used++; + col_idx--; + } + + /* + * Copy original content (render_cell() reads cell data directly + * from grid), and mark all cells as dirty. This ensures they are + * re-rendered when the pre-edit text is modified or removed. + */ + struct cell *real_cells = malloc(cells_used * sizeof(real_cells[0])); + for (int i = 0; i < cells_used; i++) { + assert(col_idx + i < term->cols); + real_cells[i] = row->cells[col_idx + i]; + real_cells[i].attrs.clean = 0; + } + row->dirty = true; + + /* Render pre-edit text */ + for (int i = 0; i < term->ime.preedit.count; i++) { + const struct cell *cell = &term->ime.preedit.cells[i]; + + if (cell->wc == CELL_MULT_COL_SPACER) + continue; + + int width = wcwidth(term->ime.preedit.cells[i].wc); + width = max(1, width); + + if (col_idx + i + width > term->cols) + break; + + row->cells[col_idx + i] = term->ime.preedit.cells[i]; + render_cell(term, buf->pix[0], row, col_idx + i, row_idx, false); + } + + /* Hollow cursor */ + if (!term->ime.preedit.cursor.hidden) { + int start = term->ime.preedit.cursor.start; + int end = term->ime.preedit.cursor.end; + + pixman_color_t fg = color_hex_to_pixman(term->colors.fg); + pixman_color_t bg = color_hex_to_pixman(term->colors.bg); + + pixman_color_t cursor_color, text_color; + cursor_colors_for_cell( + term, &term->ime.preedit.cells[start], + &fg, &bg, &cursor_color, &text_color); + + int x = term->margins.left + (col_idx + start) * term->cell_width; + int y = term->margins.top + row_idx * term->cell_height; + + int cols = end - start; + draw_unfocused_block(term, buf->pix[0], &cursor_color, x, y, cols); + } + + /* Restore original content (but do not render) */ + for (int i = 0; i < cells_used; i++) + row->cells[col_idx + i] = real_cells[i]; + free(real_cells); + + wl_surface_damage_buffer( + term->window->surface, + term->margins.left, + term->margins.top + row_idx * term->cell_height, + term->width - term->margins.left - term->margins.right, + 1 * term->cell_height); +} + static void render_row(struct terminal *term, pixman_image_t *pix, struct row *row, int row_no, int cursor_col) @@ -1903,6 +2008,9 @@ grid_render(struct terminal *term) term->render.workers.buf = NULL; } + /* Render IME pre-edit text */ + render_ime_preedit(term, buf, cursor); + if (term->flash.active) { /* Note: alpha is pre-computed in each color component */ /* TODO: dim while searching */ diff --git a/terminal.c b/terminal.c index 3bb41d12..d08d8658 100644 --- a/terminal.c +++ b/terminal.c @@ -1403,6 +1403,8 @@ term_destroy(struct terminal *term) tll_free(term->alt.sixel_images); sixel_fini(term); + free(term->ime.preedit.cells); + free(term->foot_exe); free(term->cwd); @@ -2216,6 +2218,13 @@ term_kbd_focus_out(struct terminal *term) if (it->item.kbd_focus == term) return; + if (term->ime.preedit.cells != NULL) { + free(term->ime.preedit.cells); + term->ime.preedit.cells = NULL; + term->ime.preedit.count = 0; + render_refresh(term); + } + term->kbd_focus = false; cursor_refresh(term); diff --git a/terminal.h b/terminal.h index 4eca0aa9..b8339d8d 100644 --- a/terminal.h +++ b/terminal.h @@ -470,6 +470,19 @@ struct terminal { unsigned max_height; /* Maximum image height, in pixels */ } sixel; + struct { + struct { + struct cell *cells; + int count; + + struct { + bool hidden; + int start; /* Cell index, inclusive */ + int end; /* Cell index, exclusive */ + } cursor; + } preedit; + } ime; + bool quit; bool is_shutting_down; void (*shutdown_cb)(void *data, int exit_code); diff --git a/vt.c b/vt.c index 1522dacf..ed26da73 100644 --- a/vt.c +++ b/vt.c @@ -408,7 +408,7 @@ action_esc_dispatch(struct terminal *term, uint8_t final) case '=': term->keypad_keys_mode = KEYPAD_APPLICATION; break; -ö + case '>': term->keypad_keys_mode = KEYPAD_NUMERICAL; break; diff --git a/wayland.c b/wayland.c index ecaecb1c..c0a9c512 100644 --- a/wayland.c +++ b/wayland.c @@ -191,8 +191,8 @@ seat_destroy(struct seat *seat) free(seat->clipboard.text); free(seat->primary.text); - free(seat->ime.preedit.text); - free(seat->ime.commit.text); + free(seat->ime.preedit.pending.text); + free(seat->ime.commit.pending.text); free(seat->name); } diff --git a/wayland.h b/wayland.h index b29bb8ab..51a6fad1 100644 --- a/wayland.h +++ b/wayland.h @@ -15,6 +15,9 @@ #include "fdm.h" +/* Forward declarations */ +struct terminal; + typedef tll(xkb_keycode_t) xkb_keycode_list_t; struct key_binding { @@ -225,18 +228,24 @@ struct seat { struct zwp_text_input_v3 *wl_text_input; struct { struct { - char *text; - int32_t cursor_begin; - int32_t cursor_end; + struct { + char *text; + int32_t cursor_begin; + int32_t cursor_end; + } pending; } preedit; struct { - char *text; + struct { + char *text; + } pending; } commit; struct { - uint32_t before_length; - uint32_t after_length; + struct { + uint32_t before_length; + uint32_t after_length; + } pending; } surrounding; uint32_t serial; -- cgit v1.3 From 05083110c3fad046ccf0838a364150b8d2bab888 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Thu, 3 Dec 2020 18:36:56 +0100 Subject: ime: make IME compile-time optional --- ime.c | 63 ++++++++++++++++++++++++++++++++++++++++--------------- ime.h | 10 +++++++++ meson.build | 15 +++++++------ meson_options.txt | 1 + render.c | 3 +++ terminal.c | 18 ++++++++++++---- terminal.h | 4 ++++ wayland.c | 14 +++++++++++-- wayland.h | 4 ++++ 9 files changed, 103 insertions(+), 29 deletions(-) create mode 100644 meson_options.txt (limited to 'ime.c') diff --git a/ime.c b/ime.c index 787985cf..ccdbb098 100644 --- a/ime.c +++ b/ime.c @@ -1,5 +1,7 @@ #include "ime.h" +#if defined(FOOT_IME_ENABLED) && FOOT_IME_ENABLED + #include #include "text-input-unstable-v3.h" @@ -67,11 +69,7 @@ leave(void *data, struct zwp_text_input_v3 *zwp_text_input_v3, zwp_text_input_v3_commit(seat->wl_text_input); seat->ime.serial++; - free(seat->ime.preedit.pending.text); - seat->ime.preedit.pending.text = NULL; - - free(seat->ime.commit.pending.text); - seat->ime.commit.pending.text = NULL; + ime_reset(seat); } static void @@ -82,10 +80,13 @@ preedit_string(void *data, struct zwp_text_input_v3 *zwp_text_input_v3, struct seat *seat = data; - free(seat->ime.preedit.pending.text); - seat->ime.preedit.pending.text = text != NULL ? xstrdup(text) : NULL; - seat->ime.preedit.pending.cursor_begin = cursor_begin; - seat->ime.preedit.pending.cursor_end = cursor_end; + ime_reset_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 @@ -95,8 +96,11 @@ commit_string(void *data, struct zwp_text_input_v3 *zwp_text_input_v3, LOG_DBG("commit: text=%s", text); struct seat *seat = data; - free(seat->ime.commit.pending.text); - seat->ime.commit.pending.text = text != NULL ? xstrdup(text) : NULL; + + ime_reset_commit(seat); + + if (text != NULL) + seat->ime.commit.pending.text = xstrdup(text); } static void @@ -142,9 +146,7 @@ done(void *data, struct zwp_text_input_v3 *zwp_text_input_v3, /* 1. Delete existing pre-edit text */ if (term->ime.preedit.cells != NULL) { - free(term->ime.preedit.cells); - term->ime.preedit.cells = NULL; - term->ime.preedit.count = 0; + term_reset_ime(term); render_refresh(term); } @@ -162,9 +164,7 @@ done(void *data, struct zwp_text_input_v3 *zwp_text_input_v3, term, seat->ime.commit.pending.text, strlen(seat->ime.commit.pending.text)); - - free(seat->ime.commit.pending.text); - seat->ime.commit.pending.text = NULL; + ime_reset_commit(seat); } /* 4. Calculate surrounding text to send - not supported */ @@ -292,10 +292,31 @@ done(void *data, struct zwp_text_input_v3 *zwp_text_input_v3, render_refresh(term); } + ime_reset_preedit(seat); +} + +void +ime_reset_preedit(struct seat *seat) +{ free(seat->ime.preedit.pending.text); seat->ime.preedit.pending.text = NULL; } +void +ime_reset_commit(struct seat *seat) +{ + free(seat->ime.commit.pending.text); + seat->ime.commit.pending.text = NULL; +} + +void +ime_reset(struct seat *seat) +{ + ime_reset_preedit(seat); + ime_reset_commit(seat); +} + + const struct zwp_text_input_v3_listener text_input_listener = { .enter = &enter, .leave = &leave, @@ -304,3 +325,11 @@ const struct zwp_text_input_v3_listener text_input_listener = { .delete_surrounding_text = &delete_surrounding_text, .done = &done, }; + +#else /* !FOOT_IME_ENABLED */ + +void ime_reset_preedit(struct seat *seat) {} +void ime_reset_commit(struct seat *seat) {} +void ime_reset(struct seat *seat) {} + +#endif diff --git a/ime.h b/ime.h index d1ce7ff0..d5c96534 100644 --- a/ime.h +++ b/ime.h @@ -1,5 +1,15 @@ #pragma once +#if defined(FOOT_IME_ENABLED) && FOOT_IME_ENABLED + #include "text-input-unstable-v3.h" extern const struct zwp_text_input_v3_listener text_input_listener; + +#endif /* FOOT_IME_ENABLED */ + +struct seat; + +void ime_reset_preedit(struct seat *seat); +void ime_reset_commit(struct seat *seat); +void ime_reset(struct seat *seat); diff --git a/meson.build b/meson.build index 1aa018a9..6e04ba8b 100644 --- a/meson.build +++ b/meson.build @@ -17,6 +17,9 @@ add_project_arguments( (is_debug_build ? ['-D_DEBUG'] : [cc.get_supported_arguments('-fno-asynchronous-unwind-tables')]) + + (get_option('ime') + ? ['-DFOOT_IME_ENABLED=1'] + : []) + cc.get_supported_arguments( ['-pedantic', '-fstrict-aliasing', @@ -198,9 +201,9 @@ subdir('completions') subdir('doc') subdir('icons') -# summary( -# { -# '': false, -# }, -# bool_yn: true -# ) +summary( + { + 'IME': get_option('ime'), + }, + bool_yn: true +) diff --git a/meson_options.txt b/meson_options.txt new file mode 100644 index 00000000..fc3c1b0f --- /dev/null +++ b/meson_options.txt @@ -0,0 +1 @@ +option('ime', type: 'boolean', value: true, description: 'IME (Input Method Editor) support') diff --git a/render.c b/render.c index 88a47d47..3e7ed96a 100644 --- a/render.c +++ b/render.c @@ -1025,6 +1025,8 @@ static void render_ime_preedit(struct terminal *term, struct buffer *buf, struct coord cursor) { +#if defined(FOOT_IME_ENABLED) && FOOT_IME_ENABLED + if (likely(term->ime.preedit.cells == NULL)) return; @@ -1112,6 +1114,7 @@ render_ime_preedit(struct terminal *term, struct buffer *buf, term->margins.top + row_idx * term->cell_height, term->width - term->margins.left - term->margins.right, 1 * term->cell_height); +#endif } static void diff --git a/terminal.c b/terminal.c index d08d8658..cfcc2040 100644 --- a/terminal.c +++ b/terminal.c @@ -1403,7 +1403,7 @@ term_destroy(struct terminal *term) tll_free(term->alt.sixel_images); sixel_fini(term); - free(term->ime.preedit.cells); + term_reset_ime(term); free(term->foot_exe); free(term->cwd); @@ -2218,12 +2218,12 @@ term_kbd_focus_out(struct terminal *term) if (it->item.kbd_focus == term) return; +#if defined(FOOT_IME_ENABLED) && FOOT_IME_ENABLED if (term->ime.preedit.cells != NULL) { - free(term->ime.preedit.cells); - term->ime.preedit.cells = NULL; - term->ime.preedit.count = 0; + term_reset_ime(term); render_refresh(term); } +#endif term->kbd_focus = false; cursor_refresh(term); @@ -2753,3 +2753,13 @@ term_view_to_text(const struct terminal *term, char **text, size_t *len) int end = grid_row_absolute_in_view(term->grid, term->rows - 1); return rows_to_text(term, start, end, text, len); } + +void +term_reset_ime(struct terminal *term) +{ +#if defined(FOOT_IME_ENABLED) && FOOT_IME_ENABLED + free(term->ime.preedit.cells); + term->ime.preedit.cells = NULL; + term->ime.preedit.count = 0; +#endif +} diff --git a/terminal.h b/terminal.h index b8339d8d..bbe7df25 100644 --- a/terminal.h +++ b/terminal.h @@ -470,6 +470,7 @@ struct terminal { unsigned max_height; /* Maximum image height, in pixels */ } sixel; +#if defined(FOOT_IME_ENABLED) && FOOT_IME_ENABLED struct { struct { struct cell *cells; @@ -482,6 +483,7 @@ struct terminal { } cursor; } preedit; } ime; +#endif bool quit; bool is_shutting_down; @@ -605,3 +607,5 @@ bool term_scrollback_to_text( const struct terminal *term, char **text, size_t *len); bool term_view_to_text( const struct terminal *term, char **text, size_t *len); + +void term_reset_ime(struct terminal *term); diff --git a/wayland.c b/wayland.c index c0a9c512..c8cb2c9c 100644 --- a/wayland.c +++ b/wayland.c @@ -116,6 +116,7 @@ seat_add_primary_selection(struct seat *seat) static void seat_add_text_input(struct seat *seat) { +#if defined(FOOT_IME_ENABLED) && FOOT_IME_ENABLED if (seat->wayl->text_input_manager == NULL) return; @@ -128,6 +129,7 @@ seat_add_text_input(struct seat *seat) seat->wl_text_input = text_input; zwp_text_input_v3_add_listener(text_input, &text_input_listener, seat); +#endif } static void @@ -184,15 +186,18 @@ seat_destroy(struct seat *seat) wl_keyboard_release(seat->wl_keyboard); if (seat->wl_pointer != NULL) wl_pointer_release(seat->wl_pointer); + +#if defined(FOOT_IME_ENABLED) && FOOT_IME_ENABLED if (seat->wl_text_input != NULL) zwp_text_input_v3_destroy(seat->wl_text_input); +#endif + if (seat->wl_seat != NULL) wl_seat_release(seat->wl_seat); + ime_reset(seat); free(seat->clipboard.text); free(seat->primary.text); - free(seat->ime.preedit.pending.text); - free(seat->ime.commit.pending.text); free(seat->name); } @@ -920,6 +925,7 @@ handle_global(void *data, struct wl_registry *registry, } } +#if defined(FOOT_IME_ENABLED) && FOOT_IME_ENABLED else if (strcmp(interface, zwp_text_input_manager_v3_interface.name) == 0) { const uint32_t required = 1; if (!verify_iface_version(interface, version, required)) @@ -931,6 +937,7 @@ handle_global(void *data, struct wl_registry *registry, tll_foreach(wayl->seats, it) seat_add_text_input(&it->item); } +#endif } static void @@ -1190,8 +1197,11 @@ wayl_destroy(struct wayland *wayl) seat_destroy(&it->item); tll_free(wayl->seats); +#if defined(FOOT_IME_ENABLED) && FOOT_IME_ENABLED if (wayl->text_input_manager != NULL) zwp_text_input_manager_v3_destroy(wayl->text_input_manager); +#endif + if (wayl->xdg_output_manager != NULL) zxdg_output_manager_v1_destroy(wayl->xdg_output_manager); if (wayl->shell != NULL) diff --git a/wayland.h b/wayland.h index 51a6fad1..f275c69c 100644 --- a/wayland.h +++ b/wayland.h @@ -224,6 +224,7 @@ struct seat { struct wl_clipboard clipboard; struct wl_primary primary; +#if defined(FOOT_IME_ENABLED) && FOOT_IME_ENABLED /* Input Method Editor */ struct zwp_text_input_v3 *wl_text_input; struct { @@ -250,6 +251,7 @@ struct seat { uint32_t serial; } ime; +#endif }; enum csd_surface { @@ -404,7 +406,9 @@ struct wayland { struct wp_presentation *presentation; uint32_t presentation_clock_id; +#if defined(FOOT_IME_ENABLED) && FOOT_IME_ENABLED struct zwp_text_input_manager_v3 *text_input_manager; +#endif bool have_argb8888; tll(struct monitor) monitors; /* All available outputs */ -- cgit v1.3 From 98bd798daed897c56b6f0262e5105131a074bdad Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Thu, 3 Dec 2020 18:37:40 +0100 Subject: ime: calculate wchar widths once --- ime.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) (limited to 'ime.c') diff --git a/ime.c b/ime.c index ccdbb098..bde063a8 100644 --- a/ime.c +++ b/ime.c @@ -181,8 +181,13 @@ done(void *data, struct zwp_text_input_v3 *zwp_text_input_v3, /* Next, count number of cells needed */ size_t cell_count = 0; - for (size_t i = 0; i < wchars; i++) - cell_count += max(wcwidth(wcs[i]), 1); + size_t widths[wchars + 1]; + + for (size_t i = 0; i < wchars; i++) { + int width = max(wcwidth(wcs[i]), 1); + widths[i] = width; + cell_count += width; + } /* Allocate cells */ term->ime.preedit.cells = xmalloc( @@ -193,7 +198,7 @@ done(void *data, struct zwp_text_input_v3 *zwp_text_input_v3, for (size_t i = 0, cell_idx = 0; i < wchars; i++) { struct cell *cell = &term->ime.preedit.cells[cell_idx]; - int width = max(wcwidth(wcs[i]), 1); + int width = widths[i]; cell->wc = wcs[i]; cell->attrs = (struct attributes){.clean = 0, .underline = 1}; -- cgit v1.3 From cc76f91c43981cb95e508c5b90c71bec8bbcab76 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Thu, 3 Dec 2020 18:39:53 +0100 Subject: ime: use correct index when calculating number of *cells* to advance --- ime.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'ime.c') diff --git a/ime.c b/ime.c index bde063a8..970efc81 100644 --- a/ime.c +++ b/ime.c @@ -269,7 +269,7 @@ done(void *data, struct zwp_text_input_v3 *zwp_text_input_v3, break; byte_idx += wc_bytes; - cell_idx += max(wcwidth(term->ime.preedit.cells[wc_idx].wc), 1); + cell_idx += max(wcwidth(term->ime.preedit.cells[cell_idx].wc), 1); wc_idx++; } -- cgit v1.3 From 552b9884b905fc136e368344da313eabe7bad858 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Thu, 3 Dec 2020 18:40:18 +0100 Subject: ime: set cursor end correctly when it ends *after* the pre-edit string --- ime.c | 3 +++ 1 file changed, 3 insertions(+) (limited to 'ime.c') diff --git a/ime.c b/ime.c index 970efc81..67718517 100644 --- a/ime.c +++ b/ime.c @@ -273,6 +273,9 @@ done(void *data, struct zwp_text_input_v3 *zwp_text_input_v3, wc_idx++; } + 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); -- cgit v1.3 From 001c82fa547bb8c62c50bde1b17ff9c8dd3d61c8 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Thu, 3 Dec 2020 18:40:54 +0100 Subject: ime: don’t adjust cursor’s cell-end if it’s equal to the cell-begin MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ime.c | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) (limited to 'ime.c') diff --git a/ime.c b/ime.c index 67718517..eb4eadf4 100644 --- a/ime.c +++ b/ime.c @@ -278,19 +278,25 @@ done(void *data, struct zwp_text_input_v3 *zwp_text_input_v3, /* Bounded by number of screen columns */ cell_begin = min(max(cell_begin, 0), cell_count - 1); - - /* Ensure end comes *after* begin, and is bounded by screen */ - if (cell_end <= cell_begin) - cell_end = cell_begin + max(wcwidth(term->ime.preedit.cells[cell_begin].wc), 1); cell_end = min(max(cell_end, 0), cell_count); +#if 1 + if (cell_end < cell_begin) + cell_end = cell_begin; +#else + if (cell_end <= cell_begin) { + cell_end = cell_begin + max( + wcwidth(term->ime.preedit.cells[cell_begin].wc), 1); + } +#endif + LOG_DBG("pre-edit cursor: begin=%d, end=%d", cell_begin, cell_end); assert(cell_begin >= 0); assert(cell_begin < cell_count); - assert(cell_end >= 1); + assert(cell_begin <= cell_end); + assert(cell_end >= 0); assert(cell_end <= cell_count); - assert(cell_begin < cell_end); term->ime.preedit.cursor.hidden = false; term->ime.preedit.cursor.start = cell_begin; -- cgit v1.3 From 5c17b7f8e77f96622d558ccbd5071706863c2816 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Thu, 3 Dec 2020 18:41:42 +0100 Subject: ime: expand cursor cell-end across multi-cell characters This fixes an issue where the cursor ended in the middle of e.g double width characters. --- ime.c | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'ime.c') diff --git a/ime.c b/ime.c index eb4eadf4..53a908b0 100644 --- a/ime.c +++ b/ime.c @@ -290,6 +290,13 @@ done(void *data, struct zwp_text_input_v3 *zwp_text_input_v3, } #endif + /* Expand cursor end to end of glyph */ + while (cell_end > cell_begin && cell_end < cell_count && + term->ime.preedit.cells[cell_end].wc == CELL_MULT_COL_SPACER) + { + cell_end++; + } + LOG_DBG("pre-edit cursor: begin=%d, end=%d", cell_begin, cell_end); assert(cell_begin >= 0); -- cgit v1.3 From b59d695b2b00157e403399af78b3128848a65e0f Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Fri, 4 Dec 2020 18:39:11 +0100 Subject: ime: add functions to enable/disable IME, simplify code that enables IME MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit We may want to be able to enable/disable IME run-time, even though we have received an ‘enter’ IME event. This enables us to do that. Also add functions to enable/disable IME on a per-terminal instance basis. A terminal may have multiple seats focusing it, and enabling/disabling IME in a terminal instance enables/disables IME on all those seats. Finally, the code to enable IME is simplified; the *only* surface that can ever receive ‘enter’ IME events is the main grid. All other surfaces are sub-surfaces, without their own keyboard focus. --- ime.c | 82 ++++++++++++++++++++++++++++++++------------------------------ ime.h | 3 +++ pgo/pgo.c | 3 +++ terminal.c | 74 +++++++++++++++++++++++++++++++++++++++++++++++++++----- terminal.h | 6 ++++- 5 files changed, 121 insertions(+), 47 deletions(-) (limited to 'ime.c') diff --git a/ime.c b/ime.c index 53a908b0..a60e72f5 100644 --- a/ime.c +++ b/ime.c @@ -20,43 +20,16 @@ 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); - assert(seat->kbd_focus != NULL); - - switch (term_surface_kind(seat->kbd_focus, surface)) { - case TERM_SURF_GRID: - 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); - - /* TODO: set cursor rectangle */ - zwp_text_input_v3_set_cursor_rectangle(seat->wl_text_input, 0, 0, 15, 15); - break; - - case TERM_SURF_SEARCH: - /* TODO */ - /* FALLTHROUGH */ - - case TERM_SURF_NONE: - case TERM_SURF_SCROLLBACK_INDICATOR: - case TERM_SURF_RENDER_TIMER: - case TERM_SURF_TITLE: - case TERM_SURF_BORDER_LEFT: - case TERM_SURF_BORDER_RIGHT: - case TERM_SURF_BORDER_TOP: - case TERM_SURF_BORDER_BOTTOM: - case TERM_SURF_BUTTON_MINIMIZE: - case TERM_SURF_BUTTON_MAXIMIZE: - case TERM_SURF_BUTTON_CLOSE: - zwp_text_input_v3_disable(seat->wl_text_input); - break; - } + /* The main grid is the *only* input-receiving surface we have */ + /* TODO: can we receive text_input::enter() _before_ keyboard_enter()? */ + struct terminal UNUSED *term = seat->kbd_focus; + assert(term != NULL); + assert(term_surface_kind(term, surface) == TERM_SURF_GRID); - zwp_text_input_v3_commit(seat->wl_text_input); - seat->ime.serial++; + ime_enable(seat); } static void @@ -65,11 +38,7 @@ leave(void *data, struct zwp_text_input_v3 *zwp_text_input_v3, { struct seat *seat = data; LOG_DBG("leave: seat=%s", seat->name); - zwp_text_input_v3_disable(seat->wl_text_input); - zwp_text_input_v3_commit(seat->wl_text_input); - seat->ime.serial++; - - ime_reset(seat); + ime_disable(seat); } static void @@ -146,7 +115,7 @@ done(void *data, struct zwp_text_input_v3 *zwp_text_input_v3, /* 1. Delete existing pre-edit text */ if (term->ime.preedit.cells != NULL) { - term_reset_ime(term); + term_ime_reset(term); render_refresh(term); } @@ -337,6 +306,36 @@ ime_reset(struct seat *seat) ime_reset_commit(seat); } +void +ime_enable(struct seat *seat) +{ + struct terminal *term = seat->kbd_focus; + assert(term != NULL); + + if (!term->ime.enabled) + return; + + ime_reset(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_commit(seat->wl_text_input); + seat->ime.serial++; +} + +void +ime_disable(struct seat *seat) +{ + ime_reset(seat); + + zwp_text_input_v3_disable(seat->wl_text_input); + zwp_text_input_v3_commit(seat->wl_text_input); + seat->ime.serial++; +} const struct zwp_text_input_v3_listener text_input_listener = { .enter = &enter, @@ -349,6 +348,9 @@ const struct zwp_text_input_v3_listener text_input_listener = { #else /* !FOOT_IME_ENABLED */ +void ime_enable(struct seat *seat) {} +void ime_disable(struct seat *seat) {} + void ime_reset_preedit(struct seat *seat) {} void ime_reset_commit(struct seat *seat) {} void ime_reset(struct seat *seat) {} diff --git a/ime.h b/ime.h index d5c96534..7036bbde 100644 --- a/ime.h +++ b/ime.h @@ -10,6 +10,9 @@ extern const struct zwp_text_input_v3_listener text_input_listener; struct seat; +void ime_enable(struct seat *seat); +void ime_disable(struct seat *seat); + void ime_reset_preedit(struct seat *seat); void ime_reset_commit(struct seat *seat); void ime_reset(struct seat *seat); diff --git a/pgo/pgo.c b/pgo/pgo.c index d401e533..0dc08f61 100644 --- a/pgo/pgo.c +++ b/pgo/pgo.c @@ -126,6 +126,9 @@ extract_finish(struct extraction_context *context, char **text, size_t *len) void cmd_scrollback_up(struct terminal *term, int rows) {} void cmd_scrollback_down(struct terminal *term, int rows) {} +void ime_enable(struct seat *seat) {} +void ime_disable(struct seat *seat) {} + int main(int argc, const char *const *argv) { diff --git a/terminal.c b/terminal.c index cfcc2040..80c162bd 100644 --- a/terminal.c +++ b/terminal.c @@ -24,6 +24,7 @@ #include "config.h" #include "extract.h" #include "grid.h" +#include "ime.h" #include "quirks.h" #include "reaper.h" #include "render.h" @@ -1116,6 +1117,11 @@ term_init(const struct config *conf, struct fdm *fdm, struct reaper *reaper, .shutdown_data = shutdown_data, .foot_exe = xstrdup(foot_exe), .cwd = xstrdup(cwd), +#if defined(FOOT_IME_ENABLED) && FOOT_IME_ENABLED + .ime = { + .enabled = true, + }, +#endif }; for (size_t i = 0; i < 4; i++) { @@ -1403,7 +1409,7 @@ term_destroy(struct terminal *term) tll_free(term->alt.sixel_images); sixel_fini(term); - term_reset_ime(term); + term_ime_reset(term); free(term->foot_exe); free(term->cwd); @@ -1559,6 +1565,10 @@ term_reset(struct terminal *term, bool hard) sixel_destroy(&it->item); tll_free(term->alt.sixel_images); +#if defined(FOOT_IME_ENABLED) && FOOT_IME_ENABLED + term_ime_enable(term); +#endif + if (!hard) return; @@ -2220,7 +2230,7 @@ term_kbd_focus_out(struct terminal *term) #if defined(FOOT_IME_ENABLED) && FOOT_IME_ENABLED if (term->ime.preedit.cells != NULL) { - term_reset_ime(term); + term_ime_reset(term); render_refresh(term); } #endif @@ -2754,12 +2764,64 @@ term_view_to_text(const struct terminal *term, char **text, size_t *len) return rows_to_text(term, start, end, text, len); } +bool +term_ime_is_enabled(const struct terminal *term) +{ +#if defined(FOOT_IME_ENABLED) && FOOT_IME_ENABLED + return term->ime.enabled; +#else + return false; +#endif +} + void -term_reset_ime(struct terminal *term) +term_ime_enable(struct terminal *term) { #if defined(FOOT_IME_ENABLED) && FOOT_IME_ENABLED - free(term->ime.preedit.cells); - term->ime.preedit.cells = NULL; - term->ime.preedit.count = 0; + if (term->ime.enabled) + return; + + LOG_DBG("IME enabled"); + + term->ime.enabled = true; + term_ime_reset(term); + + /* IME is per seat - enable on all seat currenly focusing us */ + tll_foreach(term->wl->seats, it) { + if (it->item.kbd_focus == term) + ime_enable(&it->item); + } +#endif +} + +void +term_ime_disable(struct terminal *term) +{ +#if defined(FOOT_IME_ENABLED) && FOOT_IME_ENABLED + if (!term->ime.enabled) + return; + + LOG_DBG("IME disabled"); + + term->ime.enabled = false; + term_ime_reset(term); + + /* IME is per seat - disable on all seat currenly focusing us */ + tll_foreach(term->wl->seats, it) { + if (it->item.kbd_focus == term) + ime_disable(&it->item); + } +#endif +} + +void +term_ime_reset(struct terminal *term) +{ +#if defined(FOOT_IME_ENABLED) && FOOT_IME_ENABLED + if (term->ime.preedit.cells != NULL) { + free(term->ime.preedit.cells); + term->ime.preedit.cells = NULL; + term->ime.preedit.count = 0; + } #endif } diff --git a/terminal.h b/terminal.h index bbe7df25..6a634914 100644 --- a/terminal.h +++ b/terminal.h @@ -472,6 +472,7 @@ struct terminal { #if defined(FOOT_IME_ENABLED) && FOOT_IME_ENABLED struct { + bool enabled; struct { struct cell *cells; int count; @@ -608,4 +609,7 @@ bool term_scrollback_to_text( bool term_view_to_text( const struct terminal *term, char **text, size_t *len); -void term_reset_ime(struct terminal *term); +bool term_ime_is_enabled(const struct terminal *term); +void term_ime_enable(struct terminal *term); +void term_ime_disable(struct terminal *term); +void term_ime_reset(struct terminal *term); -- cgit v1.3 From 7eea6f94ed753dd358fb53dac889fc40455810e1 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sat, 5 Dec 2020 11:43:54 +0100 Subject: ime: don’t commit to the slave while scrollback searching MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ime.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) (limited to 'ime.c') diff --git a/ime.c b/ime.c index a60e72f5..17db0b1a 100644 --- a/ime.c +++ b/ime.c @@ -129,10 +129,14 @@ done(void *data, struct zwp_text_input_v3 *zwp_text_input_v3, /* 3. Insert commit string */ if (seat->ime.commit.pending.text != NULL) { - term_to_slave( - term, - seat->ime.commit.pending.text, - strlen(seat->ime.commit.pending.text)); + if (term->is_searching) { + /* TODO */ + } else { + term_to_slave( + term, + seat->ime.commit.pending.text, + strlen(seat->ime.commit.pending.text)); + } ime_reset_commit(seat); } @@ -214,7 +218,7 @@ done(void *data, struct zwp_text_input_v3 *zwp_text_input_v3, * * I haven't found an IME that requests anything else */ - + const size_t byte_len = strlen(seat->ime.preedit.pending.text); int cell_begin = -1, cell_end = -1; -- cgit v1.3 From d87160e5f679fc8d9c53d21584932368add61c4d Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sat, 5 Dec 2020 11:50:27 +0100 Subject: ime: commit text using search_add_chars() while in scrollback search mode --- ime.c | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) (limited to 'ime.c') diff --git a/ime.c b/ime.c index 17db0b1a..9c6676ab 100644 --- a/ime.c +++ b/ime.c @@ -10,6 +10,7 @@ #define LOG_ENABLE_DBG 0 #include "log.h" #include "render.h" +#include "search.h" #include "terminal.h" #include "util.h" #include "wayland.h" @@ -129,14 +130,14 @@ done(void *data, struct zwp_text_input_v3 *zwp_text_input_v3, /* 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->is_searching) { - /* TODO */ - } else { - term_to_slave( - term, - seat->ime.commit.pending.text, - strlen(seat->ime.commit.pending.text)); - } + search_add_chars(term, text, len); + render_refresh_search(term); + } else + term_to_slave(term, text, len); ime_reset_commit(seat); } -- cgit v1.3 From 194fbff883e605a3a9afac4ecc66c6cf3cc6b7fa Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sun, 6 Dec 2020 12:17:52 +0100 Subject: ime: store wchar version of pre-edit string in terminal struct --- ime.c | 9 +++++---- terminal.c | 2 ++ terminal.h | 1 + 3 files changed, 8 insertions(+), 4 deletions(-) (limited to 'ime.c') diff --git a/ime.c b/ime.c index 9c6676ab..d7dbc873 100644 --- a/ime.c +++ b/ime.c @@ -150,15 +150,16 @@ done(void *data, struct zwp_text_input_v3 *zwp_text_input_v3, if (wchars > 0) { /* First, convert to unicode */ - wchar_t wcs[wchars + 1]; - mbstowcs(wcs, seat->ime.preedit.pending.text, wchars); + term->ime.preedit.text = xmalloc((wchars + 1) * sizeof(wchar_t)); + mbstowcs(term->ime.preedit.text, seat->ime.preedit.pending.text, wchars); + term->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(wcs[i]), 1); + int width = max(wcwidth(term->ime.preedit.text[i]), 1); widths[i] = width; cell_count += width; } @@ -174,7 +175,7 @@ done(void *data, struct zwp_text_input_v3 *zwp_text_input_v3, int width = widths[i]; - cell->wc = wcs[i]; + cell->wc = term->ime.preedit.text[i]; cell->attrs = (struct attributes){.clean = 0, .underline = 1}; for (int j = 1; j < width; j++) { diff --git a/terminal.c b/terminal.c index 215304ec..a626b88d 100644 --- a/terminal.c +++ b/terminal.c @@ -2819,7 +2819,9 @@ term_ime_reset(struct terminal *term) { #if defined(FOOT_IME_ENABLED) && FOOT_IME_ENABLED if (term->ime.preedit.cells != NULL) { + free(term->ime.preedit.text); free(term->ime.preedit.cells); + term->ime.preedit.text = NULL; term->ime.preedit.cells = NULL; term->ime.preedit.count = 0; } diff --git a/terminal.h b/terminal.h index f99c77c0..07723e1e 100644 --- a/terminal.h +++ b/terminal.h @@ -475,6 +475,7 @@ struct terminal { struct { bool enabled; struct { + wchar_t *text; struct cell *cells; int count; -- cgit v1.3 From fd42a0bc23aa0f19596f40449bf869f4245cc29c Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sun, 6 Dec 2020 12:18:17 +0100 Subject: ime: call render_refresh_search() instead of render_refresh() When scrollback search is active --- ime.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) (limited to 'ime.c') diff --git a/ime.c b/ime.c index d7dbc873..ccfa6310 100644 --- a/ime.c +++ b/ime.c @@ -117,7 +117,10 @@ done(void *data, struct zwp_text_input_v3 *zwp_text_input_v3, /* 1. Delete existing pre-edit text */ if (term->ime.preedit.cells != NULL) { term_ime_reset(term); - render_refresh(term); + if (term->is_searching) + render_refresh_search(term); + else + render_refresh(term); } /* @@ -285,7 +288,10 @@ done(void *data, struct zwp_text_input_v3 *zwp_text_input_v3, term->ime.preedit.cursor.end = cell_end; } - render_refresh(term); + if (term->is_searching) + render_refresh_search(term); + else + render_refresh(term); } ime_reset_preedit(seat); -- cgit v1.3 From a6ed9a9773bbd4081f741dc6d792086877cdcb41 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Mon, 7 Dec 2020 18:57:49 +0100 Subject: ime: don’t underline characters inside the cursor-box MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ime.c | 254 ++++++++++++++++++++++++++++++++++-------------------------------- 1 file changed, 130 insertions(+), 124 deletions(-) (limited to 'ime.c') diff --git a/ime.c b/ime.c index ccfa6310..24237d69 100644 --- a/ime.c +++ b/ime.c @@ -151,150 +151,156 @@ done(void *data, struct zwp_text_input_v3 *zwp_text_input_v3, ? mbstowcs(NULL, seat->ime.preedit.pending.text, 0) : 0; - if (wchars > 0) { - /* First, convert to unicode */ - term->ime.preedit.text = xmalloc((wchars + 1) * sizeof(wchar_t)); - mbstowcs(term->ime.preedit.text, seat->ime.preedit.pending.text, wchars); - term->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(term->ime.preedit.text[i]), 1); - widths[i] = width; - cell_count += width; + if (wchars == 0 || wchars == (size_t)-1) { + ime_reset_preedit(seat); + return; + } + + /* First, convert to unicode */ + term->ime.preedit.text = xmalloc((wchars + 1) * sizeof(wchar_t)); + mbstowcs(term->ime.preedit.text, seat->ime.preedit.pending.text, wchars); + term->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(term->ime.preedit.text[i]), 1); + widths[i] = width; + cell_count += width; + } + + /* Allocate cells */ + term->ime.preedit.cells = xmalloc( + cell_count * sizeof(term->ime.preedit.cells[0])); + term->ime.preedit.count = cell_count; + + /* Populate cells */ + for (size_t i = 0, cell_idx = 0; i < wchars; i++) { + struct cell *cell = &term->ime.preedit.cells[cell_idx]; + + int width = widths[i]; + + cell->wc = term->ime.preedit.text[i]; + cell->attrs = (struct attributes){.clean = 0}; + + for (int j = 1; j < width; j++) { + cell = &term->ime.preedit.cells[cell_idx + j]; + cell->wc = CELL_MULT_COL_SPACER; + cell->attrs = (struct attributes){.clean = 1}; } - /* Allocate cells */ - term->ime.preedit.cells = xmalloc( - cell_count * sizeof(term->ime.preedit.cells[0])); - term->ime.preedit.count = cell_count; + cell_idx += width; + } - /* Populate cells */ - for (size_t i = 0, cell_idx = 0; i < wchars; i++) { - struct cell *cell = &term->ime.preedit.cells[cell_idx]; + /* 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"); + term->ime.preedit.cursor.hidden = true; + term->ime.preedit.cursor.start = -1; + term->ime.preedit.cursor.end = -1; + } - int width = widths[i]; + 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. + * + * Note that this has only been tested with + * + * cursor_begin == cursor_end == 0 + * + * I haven't found an IME that requests anything else + */ + + const size_t byte_len = strlen(seat->ime.preedit.pending.text); + + 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; - cell->wc = term->ime.preedit.text[i]; - cell->attrs = (struct attributes){.clean = 0, .underline = 1}; + /* 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); - for (int j = 1; j < width; j++) { - cell = &term->ime.preedit.cells[cell_idx + j]; - cell->wc = CELL_MULT_COL_SPACER; - cell->attrs = (struct attributes){.clean = 1}; - } + if (wc_bytes <= 0) + break; - cell_idx += width; + byte_idx += wc_bytes; } - /* Pre-edit cursor - hidden */ - if (seat->ime.preedit.pending.cursor_begin == -1 || - seat->ime.preedit.pending.cursor_end == -1) + 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 && + term->ime.preedit.cells[cell_end].wc == CELL_MULT_COL_SPACER) { - /* 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"); - term->ime.preedit.cursor.hidden = true; - term->ime.preedit.cursor.start = -1; - term->ime.preedit.cursor.end = -1; + cell_end++; } - 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. - * - * Note that this has only been tested with - * - * cursor_begin == cursor_end == 0 - * - * I haven't found an IME that requests anything else - */ - - const size_t byte_len = strlen(seat->ime.preedit.pending.text); - - 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); - ) - { - 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; - cell_idx += max(wcwidth(term->ime.preedit.cells[cell_idx].wc), 1); - wc_idx++; - } - - 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 1 - if (cell_end < cell_begin) - cell_end = cell_begin; -#else - if (cell_end <= cell_begin) { - cell_end = cell_begin + max( - wcwidth(term->ime.preedit.cells[cell_begin].wc), 1); - } -#endif + LOG_DBG("pre-edit cursor: begin=%d, end=%d", cell_begin, cell_end); - /* Expand cursor end to end of glyph */ - while (cell_end > cell_begin && cell_end < cell_count && - term->ime.preedit.cells[cell_end].wc == CELL_MULT_COL_SPACER) - { - cell_end++; - } + assert(cell_begin >= 0); + assert(cell_begin < cell_count); + assert(cell_begin <= cell_end); + assert(cell_end >= 0); + assert(cell_end <= cell_count); - LOG_DBG("pre-edit cursor: begin=%d, end=%d", cell_begin, cell_end); + term->ime.preedit.cursor.hidden = false; + term->ime.preedit.cursor.start = cell_begin; + term->ime.preedit.cursor.end = cell_end; + } - assert(cell_begin >= 0); - assert(cell_begin < cell_count); - assert(cell_begin <= cell_end); - assert(cell_end >= 0); - assert(cell_end <= cell_count); + /* Underline pre-edit string that is *not* covered by the cursor */ + bool hidden = term->ime.preedit.cursor.hidden; + int start = term->ime.preedit.cursor.start; + int end = term->ime.preedit.cursor.end; - term->ime.preedit.cursor.hidden = false; - term->ime.preedit.cursor.start = cell_begin; - term->ime.preedit.cursor.end = cell_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 = &term->ime.preedit.cells[cell_idx]; + cell->attrs.underline = true; } - - if (term->is_searching) - render_refresh_search(term); - else - render_refresh(term); } ime_reset_preedit(seat); + + if (term->is_searching) + render_refresh_search(term); + else + render_refresh(term); } void -- cgit v1.3