Loading...
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 | // SPDX-License-Identifier: GPL-2.0+ /* * Common code for text-input scene objects (textline, textedit) * * Copyright 2026 Canonical Ltd * Written by Simon Glass <simon.glass@canonical.com> */ #define LOG_CATEGORY LOGC_EXPO #include <cli.h> #include <expo.h> #include <log.h> #include <malloc.h> #include <menu.h> #include <video_console.h> #include <linux/errno.h> #include <linux/kernel.h> #include <linux/string.h> #include "scene_internal.h" #ifdef CONFIG_CMDLINE_UNDO_COUNT #define UNDO_COUNT CONFIG_CMDLINE_UNDO_COUNT #else #define UNDO_COUNT 64 #endif int scene_txtin_init(struct scene_txtin *tin, uint size, uint line_chars) { char *buf; if (!abuf_init_size(&tin->buf, size)) return log_msg_ret("buf", -ENOMEM); buf = abuf_data(&tin->buf); *buf = '\0'; tin->line_chars = line_chars; return 0; } void scene_txtin_destroy(struct scene *scn, struct scene_txtin *tin) { abuf_uninit(&tin->buf); if (tin->ctx && scn->expo->cons) vidconsole_ctx_dispose(scn->expo->cons, tin->ctx); } int scene_txtin_arrange(struct scene *scn, struct expo_arrange_info *arr, struct scene_obj *obj, struct scene_txtin *tin) { const bool open = obj->flags & SCENEOF_OPEN; const struct expo_theme *theme = &scn->expo->theme; bool point; int x; int ret; x = obj->req_bbox.x0; if (tin->label_id) { ret = scene_obj_set_pos(scn, tin->label_id, x, obj->req_bbox.y0); if (ret < 0) return log_msg_ret("lab", ret); if (scene_chklog(obj->name)) log_debug("arr->label_width %d margin %d\n", arr->label_width, theme->textline_label_margin_x); x += arr->label_width + theme->textline_label_margin_x; } point = scn->highlight_id == obj->id; point &= !open; scene_obj_flag_clrset(scn, tin->edit_id, SCENEOF_POINT, point ? SCENEOF_POINT : 0); return x; } /** * set_cursor_pos() - Set cursor position for multiline text * * Finds the visual line containing the cursor and sets the cursor position * to the correct pixel location within that line. * * @cons: Vidconsole device * @ctx: Vidconsole context * @txt: Text object containing line measurement info * @buf: Text buffer * @pos: Cursor position in buffer */ static void set_cursor_pos(struct udevice *cons, void *ctx, struct scene_obj_txt *txt, const char *buf, uint pos) { const struct vidconsole_mline *mline, *res; struct vidconsole_bbox bbox; struct alist lines; uint i; alist_init_struct(&lines, struct vidconsole_mline); for (i = 0; i < txt->gen.lines.count; i++) { mline = alist_get(&txt->gen.lines, i, struct vidconsole_mline); if (pos < mline->start || pos > mline->start + mline->len) continue; if (vidconsole_measure(cons, txt->gen.font_name, txt->gen.font_size, buf + mline->start, pos - mline->start, -1, &bbox, &lines)) break; /* measured text is within a single line, so only one result */ res = alist_get(&lines, 0, struct vidconsole_mline); if (!res) break; vidconsole_set_cursor_pos(cons, ctx, txt->obj.bbox.x0 + res->xpos, txt->obj.bbox.y0 + mline->bbox.y0); break; } alist_uninit(&lines); } int scene_txtin_render_deps(struct scene *scn, struct scene_obj *obj, struct scene_txtin *tin) { struct cli_line_state *cls = &tin->cls; struct cli_editor_state *ed = cli_editor(cls); const bool open = obj->flags & SCENEOF_OPEN; struct udevice *cons = scn->expo->cons; void *ctx = tin->ctx; uint i; /* if open, render the edit text on top of the background */ if (open) { scene_render_obj(scn, tin->edit_id, ctx); if (ed->multiline) { /* for multiline, set cursor position directly */ struct scene_obj_txt *txt; txt = scene_obj_find(scn, tin->edit_id, SCENEOBJT_NONE); if (txt) set_cursor_pos(cons, ctx, txt, cls->buf, cls->num); } else { /* for single-line, use backspaces */ for (i = cls->num; i < cls->eol_num; i++) vidconsole_put_char(cons, ctx, '\b'); } vidconsole_show_cursor(cons, ctx); } return 0; } /** * scene_txtin_putch() - Output a character to the vidconsole * * This is used as the putch callback for CLI line editing, so that characters * are sent to the correct vidconsole. * * @cls: CLI line state * @ch: Character to output */ static void scene_txtin_putch(struct cli_line_state *cls, int ch) { struct scene_txtin *tin = container_of(cls, struct scene_txtin, cls); struct scene *scn = cls->priv; vidconsole_put_char(scn->expo->cons, tin->ctx, ch); } void scene_txtin_close(struct scene *scn, struct scene_txtin *tin) { struct cli_line_state *cls = &tin->cls; cli_cread_uninit(cls); /* cursor is not needed now */ vidconsole_readline_end(scn->expo->cons, tin->ctx); } /** * scene_txtin_line_nav() - Navigate to previous/next line in multi-line input * * Moves the cursor to the previous or next line, trying to maintain the same * horizontal pixel position. Uses the text measurement info attached to the * edit text object. * * @cls: CLI line state * @up: true to move to previous line, false for next line * Return: New cursor position, or -ve if at boundary */ static int scene_txtin_line_nav(struct cli_line_state *cls, bool up) { struct scene_txtin *tin = container_of(cls, struct scene_txtin, cls); struct scene *scn = cls->priv; struct scene_obj_txt *txt; const struct vidconsole_mline *mline; const struct vidconsole_mline *target; struct vidconsole_bbox bbox; uint pos = cls->num; int cur_line, target_line; int target_x, best_pos, best_diff; int i, ret; txt = scene_obj_find(scn, tin->edit_id, SCENEOBJT_NONE); if (!txt || !txt->gen.lines.count) return -ENOENT; /* find which line the cursor is on */ cur_line = -1; for (i = 0; i < txt->gen.lines.count; i++) { mline = alist_get(&txt->gen.lines, i, struct vidconsole_mline); if (pos >= mline->start && pos <= mline->start + mline->len) { cur_line = i; break; } } if (cur_line < 0) return -EINVAL; /* find target line */ target_line = up ? cur_line - 1 : cur_line + 1; if (target_line < 0 || target_line >= txt->gen.lines.count) return -EINVAL; /* measure text from line start to cursor to get x position */ ret = vidconsole_measure(scn->expo->cons, txt->gen.font_name, txt->gen.font_size, cls->buf + mline->start, pos - mline->start, -1, &bbox, NULL); if (ret) return ret; target_x = bbox.x1; /* find character position on target line closest to target_x */ target = alist_get(&txt->gen.lines, target_line, struct vidconsole_mline); best_pos = target->start; best_diff = target_x; /* diff from position 0 */ for (i = 1; i <= target->len; i++) { int diff; ret = vidconsole_measure(scn->expo->cons, txt->gen.font_name, txt->gen.font_size, cls->buf + target->start, i, -1, &bbox, NULL); if (ret) break; diff = abs(bbox.x1 - target_x); if (diff < best_diff) { best_diff = diff; best_pos = target->start + i; } /* stop if we've gone past the target */ if (bbox.x1 > target_x) break; } /* measure text to best_pos to get x coordinate for cursor */ ret = vidconsole_measure(scn->expo->cons, txt->gen.font_name, txt->gen.font_size, cls->buf + target->start, best_pos - target->start, -1, &bbox, NULL); if (ret) return ret; /* set cursor position: text object position + line offset + char offset */ vidconsole_set_cursor_pos(scn->expo->cons, tin->ctx, txt->obj.bbox.x0 + bbox.x1, txt->obj.bbox.y0 + target->bbox.y0); return best_pos; } int scene_txtin_open(struct scene *scn, struct scene_obj *obj, struct scene_txtin *tin) { struct cli_line_state *cls = &tin->cls; struct cli_editor_state *ed = cli_editor(cls); struct udevice *cons = scn->expo->cons; struct scene_obj_txt *txt; void *ctx; int ret, i; ctx = tin->ctx; if (!ctx) { ret = vidconsole_ctx_new(cons, &ctx); if (ret) return log_msg_ret("ctx", ret); tin->ctx = ctx; } /* Copy the text into the scene buffer in case the edit is cancelled */ memcpy(abuf_data(&scn->buf), abuf_data(&tin->buf), abuf_size(&scn->buf)); /* get the position of the editable */ txt = scene_obj_find(scn, tin->edit_id, SCENEOBJT_NONE); if (!txt) return log_msg_ret("cur", -ENOENT); vidconsole_set_cursor_pos(cons, ctx, txt->obj.bbox.x0, txt->obj.bbox.y0); vidconsole_entry_start(cons, ctx); cli_cread_init_undo(cls, abuf_data(&tin->buf), abuf_size(&tin->buf)); cls->insert = true; ed->putch = scene_txtin_putch; cls->priv = scn; /* Initialise undo ring buffer */ alist_init_struct(&ed->undo.pos, struct cli_undo_pos); for (i = 0; i < UNDO_COUNT; i++) { struct cli_undo_pos *pos; pos = alist_ensure(&ed->undo.pos, i, struct cli_undo_pos); abuf_init_size(&pos->buf, abuf_size(&tin->buf)); } /* Initialise redo ring buffer */ alist_init_struct(&ed->redo.pos, struct cli_undo_pos); for (i = 0; i < UNDO_COUNT; i++) { struct cli_undo_pos *pos; pos = alist_ensure(&ed->redo.pos, i, struct cli_undo_pos); abuf_init_size(&pos->buf, abuf_size(&tin->buf)); } /* yank buffer is initialised by cli_cread_init_undo() above */ if (obj->type == SCENEOBJT_TEXTEDIT) { ed->multiline = true; ed->line_nav = scene_txtin_line_nav; } cli_cread_add_initial(cls); /* make sure the cursor is visible */ vidconsole_readline_start(cons, ctx, true); return 0; } void scene_txtin_calc_bbox(struct scene_obj *obj, struct scene_txtin *tin, struct vidconsole_bbox *bbox, struct vidconsole_bbox *edit_bbox) { struct scene *scn = obj->scene; const struct expo_theme *theme = &scn->expo->theme; int inset = theme->menu_inset; bbox->valid = false; scene_bbox_union(scn, tin->label_id, inset, bbox); scene_bbox_union(scn, tin->edit_id, inset, bbox); edit_bbox->valid = false; scene_bbox_union(scn, tin->edit_id, inset, edit_bbox); } int scene_txtin_send_key(struct scene_obj *obj, struct scene_txtin *tin, int key, struct expo_action *event) { struct cli_line_state *cls = &tin->cls; const bool open = obj->flags & SCENEOF_OPEN; struct scene *scn = obj->scene; log_debug("key=%d\n", key); switch (key) { case BKEY_QUIT: if (open) { event->type = EXPOACT_CLOSE; event->select.id = obj->id; /* Copy the backup text from the scene buffer */ memcpy(abuf_data(&tin->buf), abuf_data(&scn->buf), abuf_size(&scn->buf)); scene_txtin_close(scn, tin); } else { event->type = EXPOACT_QUIT; log_debug("menu quit\n"); } break; case BKEY_SAVE: if (!open) break; /* Accept contents even in multiline mode */ event->type = EXPOACT_CLOSE; event->select.id = obj->id; scene_txtin_close(scn, tin); break; case BKEY_SELECT: if (!open) break; if (obj->flags & SCENEOF_MULTILINE) { char *buf = cls->buf; int wlen = cls->eol_num - cls->num; /* Insert newline at cursor position */ memmove(&buf[cls->num + 1], &buf[cls->num], wlen); buf[cls->num] = '\n'; cls->num++; cls->eol_num++; } else { event->type = EXPOACT_CLOSE; event->select.id = obj->id; scene_txtin_close(scn, tin); } break; case BKEY_UP: cread_line_process_ch(cls, CTL_CH('p')); break; case BKEY_DOWN: cread_line_process_ch(cls, CTL_CH('n')); break; default: cread_line_process_ch(cls, key); break; } return 0; } |