Simon Glass | 0a4d14b | 2023-01-06 08:52:37 -0600 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Implementation of a scene, a collection of text/image/menu items in an expo |
| 4 | * |
| 5 | * Copyright 2022 Google LLC |
| 6 | * Written by Simon Glass <sjg@chromium.org> |
| 7 | */ |
| 8 | |
Simon Glass | fe4c1e2 | 2023-06-01 10:22:43 -0600 | [diff] [blame] | 9 | #define LOG_CATEGORY LOGC_EXPO |
| 10 | |
Simon Glass | cfb4f2c | 2025-05-02 08:46:42 -0600 | [diff] [blame] | 11 | #include <alist.h> |
Simon Glass | 0a4d14b | 2023-01-06 08:52:37 -0600 | [diff] [blame] | 12 | #include <dm.h> |
| 13 | #include <expo.h> |
| 14 | #include <malloc.h> |
| 15 | #include <mapmem.h> |
Simon Glass | f0e1e8c | 2023-06-01 10:22:59 -0600 | [diff] [blame] | 16 | #include <menu.h> |
Simon Glass | 0a4d14b | 2023-01-06 08:52:37 -0600 | [diff] [blame] | 17 | #include <video.h> |
| 18 | #include <video_console.h> |
| 19 | #include <linux/input.h> |
| 20 | #include "scene_internal.h" |
| 21 | |
Simon Glass | 0a4d14b | 2023-01-06 08:52:37 -0600 | [diff] [blame] | 22 | int scene_new(struct expo *exp, const char *name, uint id, struct scene **scnp) |
| 23 | { |
| 24 | struct scene *scn; |
| 25 | |
| 26 | scn = calloc(1, sizeof(struct scene)); |
| 27 | if (!scn) |
| 28 | return log_msg_ret("expo", -ENOMEM); |
| 29 | scn->name = strdup(name); |
| 30 | if (!scn->name) { |
| 31 | free(scn); |
| 32 | return log_msg_ret("name", -ENOMEM); |
| 33 | } |
| 34 | |
Simon Glass | 6651e94 | 2025-05-01 07:37:01 -0600 | [diff] [blame] | 35 | if (!abuf_init_size(&scn->buf, EXPO_MAX_CHARS + 1)) { |
Simon Glass | a968f5f | 2023-10-01 19:13:31 -0600 | [diff] [blame] | 36 | free(scn->name); |
| 37 | free(scn); |
| 38 | return log_msg_ret("buf", -ENOMEM); |
| 39 | } |
| 40 | abuf_init(&scn->entry_save); |
| 41 | |
Simon Glass | 0a4d14b | 2023-01-06 08:52:37 -0600 | [diff] [blame] | 42 | INIT_LIST_HEAD(&scn->obj_head); |
| 43 | scn->id = resolve_id(exp, id); |
| 44 | scn->expo = exp; |
| 45 | list_add_tail(&scn->sibling, &exp->scene_head); |
| 46 | |
| 47 | *scnp = scn; |
| 48 | |
| 49 | return scn->id; |
| 50 | } |
| 51 | |
| 52 | void scene_obj_destroy(struct scene_obj *obj) |
| 53 | { |
| 54 | if (obj->type == SCENEOBJT_MENU) |
| 55 | scene_menu_destroy((struct scene_obj_menu *)obj); |
| 56 | free(obj->name); |
| 57 | free(obj); |
| 58 | } |
| 59 | |
| 60 | void scene_destroy(struct scene *scn) |
| 61 | { |
| 62 | struct scene_obj *obj, *next; |
| 63 | |
| 64 | list_for_each_entry_safe(obj, next, &scn->obj_head, sibling) |
| 65 | scene_obj_destroy(obj); |
| 66 | |
Simon Glass | a968f5f | 2023-10-01 19:13:31 -0600 | [diff] [blame] | 67 | abuf_uninit(&scn->entry_save); |
| 68 | abuf_uninit(&scn->buf); |
Simon Glass | 0a4d14b | 2023-01-06 08:52:37 -0600 | [diff] [blame] | 69 | free(scn->name); |
Simon Glass | 0a4d14b | 2023-01-06 08:52:37 -0600 | [diff] [blame] | 70 | free(scn); |
| 71 | } |
| 72 | |
Simon Glass | 0a4d14b | 2023-01-06 08:52:37 -0600 | [diff] [blame] | 73 | int scene_obj_count(struct scene *scn) |
| 74 | { |
Sughosh Ganu | ebb1c20 | 2024-08-28 22:24:22 +0530 | [diff] [blame] | 75 | return list_count_nodes(&scn->obj_head); |
Simon Glass | 0a4d14b | 2023-01-06 08:52:37 -0600 | [diff] [blame] | 76 | } |
| 77 | |
Simon Glass | 45ff0bc | 2023-08-14 16:40:21 -0600 | [diff] [blame] | 78 | void *scene_obj_find(const struct scene *scn, uint id, enum scene_obj_t type) |
Simon Glass | 0a4d14b | 2023-01-06 08:52:37 -0600 | [diff] [blame] | 79 | { |
| 80 | struct scene_obj *obj; |
| 81 | |
| 82 | list_for_each_entry(obj, &scn->obj_head, sibling) { |
| 83 | if (obj->id == id && |
| 84 | (type == SCENEOBJT_NONE || obj->type == type)) |
| 85 | return obj; |
| 86 | } |
| 87 | |
| 88 | return NULL; |
| 89 | } |
| 90 | |
Simon Glass | c892511 | 2023-06-01 10:23:02 -0600 | [diff] [blame] | 91 | void *scene_obj_find_by_name(struct scene *scn, const char *name) |
| 92 | { |
| 93 | struct scene_obj *obj; |
| 94 | |
| 95 | list_for_each_entry(obj, &scn->obj_head, sibling) { |
| 96 | if (!strcmp(name, obj->name)) |
| 97 | return obj; |
| 98 | } |
| 99 | |
| 100 | return NULL; |
| 101 | } |
| 102 | |
Simon Glass | 0a4d14b | 2023-01-06 08:52:37 -0600 | [diff] [blame] | 103 | int scene_obj_add(struct scene *scn, const char *name, uint id, |
| 104 | enum scene_obj_t type, uint size, struct scene_obj **objp) |
| 105 | { |
| 106 | struct scene_obj *obj; |
| 107 | |
| 108 | obj = calloc(1, size); |
| 109 | if (!obj) |
| 110 | return log_msg_ret("obj", -ENOMEM); |
| 111 | obj->name = strdup(name); |
| 112 | if (!obj->name) { |
| 113 | free(obj); |
| 114 | return log_msg_ret("name", -ENOMEM); |
| 115 | } |
| 116 | |
| 117 | obj->id = resolve_id(scn->expo, id); |
| 118 | obj->scene = scn; |
| 119 | obj->type = type; |
| 120 | list_add_tail(&obj->sibling, &scn->obj_head); |
| 121 | *objp = obj; |
| 122 | |
| 123 | return obj->id; |
| 124 | } |
| 125 | |
| 126 | int scene_img(struct scene *scn, const char *name, uint id, char *data, |
| 127 | struct scene_obj_img **imgp) |
| 128 | { |
| 129 | struct scene_obj_img *img; |
| 130 | int ret; |
| 131 | |
| 132 | ret = scene_obj_add(scn, name, id, SCENEOBJT_IMAGE, |
| 133 | sizeof(struct scene_obj_img), |
| 134 | (struct scene_obj **)&img); |
| 135 | if (ret < 0) |
Simon Glass | 1b4a225 | 2023-10-01 19:13:25 -0600 | [diff] [blame] | 136 | return log_msg_ret("obj", ret); |
Simon Glass | 0a4d14b | 2023-01-06 08:52:37 -0600 | [diff] [blame] | 137 | |
| 138 | img->data = data; |
| 139 | |
| 140 | if (imgp) |
| 141 | *imgp = img; |
| 142 | |
| 143 | return img->obj.id; |
| 144 | } |
| 145 | |
Simon Glass | 9ef02aa | 2025-05-02 08:46:37 -0600 | [diff] [blame] | 146 | int scene_txt_generic_init(struct expo *exp, struct scene_txt_generic *gen, |
| 147 | const char *name, uint str_id, const char *str) |
| 148 | { |
| 149 | int ret; |
| 150 | |
| 151 | if (str) { |
| 152 | ret = expo_str(exp, name, str_id, str); |
| 153 | if (ret < 0) |
| 154 | return log_msg_ret("str", ret); |
| 155 | if (str_id && ret != str_id) |
| 156 | return log_msg_ret("id", -EEXIST); |
| 157 | str_id = ret; |
| 158 | } else { |
| 159 | ret = resolve_id(exp, str_id); |
| 160 | if (ret < 0) |
| 161 | return log_msg_ret("nst", ret); |
| 162 | if (str_id && ret != str_id) |
| 163 | return log_msg_ret("nid", -EEXIST); |
| 164 | } |
| 165 | |
| 166 | gen->str_id = str_id; |
Simon Glass | cfb4f2c | 2025-05-02 08:46:42 -0600 | [diff] [blame] | 167 | alist_init_struct(&gen->lines, struct vidconsole_mline); |
Simon Glass | 9ef02aa | 2025-05-02 08:46:37 -0600 | [diff] [blame] | 168 | |
| 169 | return 0; |
| 170 | } |
| 171 | |
Simon Glass | 0a4d14b | 2023-01-06 08:52:37 -0600 | [diff] [blame] | 172 | int scene_txt(struct scene *scn, const char *name, uint id, uint str_id, |
| 173 | struct scene_obj_txt **txtp) |
| 174 | { |
| 175 | struct scene_obj_txt *txt; |
| 176 | int ret; |
| 177 | |
| 178 | ret = scene_obj_add(scn, name, id, SCENEOBJT_TEXT, |
| 179 | sizeof(struct scene_obj_txt), |
| 180 | (struct scene_obj **)&txt); |
| 181 | if (ret < 0) |
Simon Glass | 1b4a225 | 2023-10-01 19:13:25 -0600 | [diff] [blame] | 182 | return log_msg_ret("obj", ret); |
Simon Glass | 0a4d14b | 2023-01-06 08:52:37 -0600 | [diff] [blame] | 183 | |
Simon Glass | 9ef02aa | 2025-05-02 08:46:37 -0600 | [diff] [blame] | 184 | ret = scene_txt_generic_init(scn->expo, &txt->gen, name, str_id, NULL); |
| 185 | if (ret) |
| 186 | return log_msg_ret("stg", ret); |
Simon Glass | 0a4d14b | 2023-01-06 08:52:37 -0600 | [diff] [blame] | 187 | if (txtp) |
| 188 | *txtp = txt; |
| 189 | |
| 190 | return txt->obj.id; |
| 191 | } |
| 192 | |
| 193 | int scene_txt_str(struct scene *scn, const char *name, uint id, uint str_id, |
| 194 | const char *str, struct scene_obj_txt **txtp) |
| 195 | { |
| 196 | struct scene_obj_txt *txt; |
| 197 | int ret; |
| 198 | |
Simon Glass | 0a4d14b | 2023-01-06 08:52:37 -0600 | [diff] [blame] | 199 | ret = scene_obj_add(scn, name, id, SCENEOBJT_TEXT, |
| 200 | sizeof(struct scene_obj_txt), |
| 201 | (struct scene_obj **)&txt); |
| 202 | if (ret < 0) |
Simon Glass | 1b4a225 | 2023-10-01 19:13:25 -0600 | [diff] [blame] | 203 | return log_msg_ret("obj", ret); |
Simon Glass | 0a4d14b | 2023-01-06 08:52:37 -0600 | [diff] [blame] | 204 | |
Simon Glass | 9ef02aa | 2025-05-02 08:46:37 -0600 | [diff] [blame] | 205 | ret = scene_txt_generic_init(scn->expo, &txt->gen, name, str_id, str); |
| 206 | if (ret) |
| 207 | return log_msg_ret("tsg", ret); |
Simon Glass | 0a4d14b | 2023-01-06 08:52:37 -0600 | [diff] [blame] | 208 | if (txtp) |
| 209 | *txtp = txt; |
| 210 | |
| 211 | return txt->obj.id; |
| 212 | } |
| 213 | |
Simon Glass | 138a397 | 2025-05-02 08:46:44 -0600 | [diff] [blame] | 214 | int scene_box(struct scene *scn, const char *name, uint id, uint width, |
| 215 | struct scene_obj_box **boxp) |
| 216 | { |
| 217 | struct scene_obj_box *box; |
| 218 | int ret; |
| 219 | |
| 220 | ret = scene_obj_add(scn, name, id, SCENEOBJT_BOX, |
| 221 | sizeof(struct scene_obj_box), |
| 222 | (struct scene_obj **)&box); |
| 223 | if (ret < 0) |
| 224 | return log_msg_ret("obj", ret); |
| 225 | |
| 226 | box->width = width; |
| 227 | |
| 228 | if (boxp) |
| 229 | *boxp = box; |
| 230 | |
| 231 | return box->obj.id; |
| 232 | } |
| 233 | |
Simon Glass | 0a4d14b | 2023-01-06 08:52:37 -0600 | [diff] [blame] | 234 | int scene_txt_set_font(struct scene *scn, uint id, const char *font_name, |
| 235 | uint font_size) |
| 236 | { |
| 237 | struct scene_obj_txt *txt; |
| 238 | |
| 239 | txt = scene_obj_find(scn, id, SCENEOBJT_TEXT); |
| 240 | if (!txt) |
| 241 | return log_msg_ret("find", -ENOENT); |
Simon Glass | 9ef02aa | 2025-05-02 08:46:37 -0600 | [diff] [blame] | 242 | txt->gen.font_name = font_name; |
| 243 | txt->gen.font_size = font_size; |
Simon Glass | 0a4d14b | 2023-01-06 08:52:37 -0600 | [diff] [blame] | 244 | |
| 245 | return 0; |
| 246 | } |
| 247 | |
| 248 | int scene_obj_set_pos(struct scene *scn, uint id, int x, int y) |
| 249 | { |
| 250 | struct scene_obj *obj; |
Simon Glass | ebec497 | 2025-05-02 08:46:33 -0600 | [diff] [blame] | 251 | int w, h; |
Simon Glass | 0a4d14b | 2023-01-06 08:52:37 -0600 | [diff] [blame] | 252 | |
| 253 | obj = scene_obj_find(scn, id, SCENEOBJT_NONE); |
| 254 | if (!obj) |
| 255 | return log_msg_ret("find", -ENOENT); |
Simon Glass | ebec497 | 2025-05-02 08:46:33 -0600 | [diff] [blame] | 256 | w = obj->bbox.x1 - obj->bbox.x0; |
| 257 | h = obj->bbox.y1 - obj->bbox.y0; |
Simon Glass | bc3a15f | 2025-05-02 08:46:31 -0600 | [diff] [blame] | 258 | obj->bbox.x0 = x; |
| 259 | obj->bbox.y0 = y; |
Simon Glass | ebec497 | 2025-05-02 08:46:33 -0600 | [diff] [blame] | 260 | obj->bbox.x1 = obj->bbox.x0 + w; |
| 261 | obj->bbox.y1 = obj->bbox.y0 + h; |
Simon Glass | 0a4d14b | 2023-01-06 08:52:37 -0600 | [diff] [blame] | 262 | |
| 263 | return 0; |
| 264 | } |
| 265 | |
Simon Glass | 7a96005 | 2023-06-01 10:22:52 -0600 | [diff] [blame] | 266 | int scene_obj_set_size(struct scene *scn, uint id, int w, int h) |
| 267 | { |
| 268 | struct scene_obj *obj; |
| 269 | |
| 270 | obj = scene_obj_find(scn, id, SCENEOBJT_NONE); |
| 271 | if (!obj) |
| 272 | return log_msg_ret("find", -ENOENT); |
Simon Glass | ebec497 | 2025-05-02 08:46:33 -0600 | [diff] [blame] | 273 | obj->bbox.x1 = obj->bbox.x0 + w; |
| 274 | obj->bbox.y1 = obj->bbox.y0 + h; |
| 275 | obj->flags |= SCENEOF_SIZE_VALID; |
Simon Glass | 7a96005 | 2023-06-01 10:22:52 -0600 | [diff] [blame] | 276 | |
| 277 | return 0; |
| 278 | } |
| 279 | |
Simon Glass | c6143dc | 2025-05-02 08:46:35 -0600 | [diff] [blame] | 280 | int scene_obj_set_width(struct scene *scn, uint id, int w) |
| 281 | { |
| 282 | struct scene_obj *obj; |
| 283 | |
| 284 | obj = scene_obj_find(scn, id, SCENEOBJT_NONE); |
| 285 | if (!obj) |
| 286 | return log_msg_ret("find", -ENOENT); |
| 287 | obj->bbox.x1 = obj->bbox.x0 + w; |
| 288 | |
| 289 | return 0; |
| 290 | } |
| 291 | |
| 292 | int scene_obj_set_bbox(struct scene *scn, uint id, int x0, int y0, int x1, |
| 293 | int y1) |
| 294 | { |
| 295 | struct scene_obj *obj; |
| 296 | |
| 297 | obj = scene_obj_find(scn, id, SCENEOBJT_NONE); |
| 298 | if (!obj) |
| 299 | return log_msg_ret("find", -ENOENT); |
| 300 | obj->bbox.x0 = x0; |
| 301 | obj->bbox.y0 = y0; |
| 302 | obj->bbox.x1 = x1; |
| 303 | obj->bbox.y1 = y1; |
| 304 | obj->flags |= SCENEOF_SIZE_VALID; |
| 305 | |
| 306 | return 0; |
| 307 | } |
| 308 | |
Simon Glass | 5beb057 | 2025-05-02 08:46:45 -0600 | [diff] [blame^] | 309 | int scene_obj_set_halign(struct scene *scn, uint id, enum scene_obj_align aln) |
| 310 | { |
| 311 | struct scene_obj *obj; |
| 312 | |
| 313 | obj = scene_obj_find(scn, id, SCENEOBJT_NONE); |
| 314 | if (!obj) |
| 315 | return log_msg_ret("osh", -ENOENT); |
| 316 | obj->horiz = aln; |
| 317 | |
| 318 | return 0; |
| 319 | } |
| 320 | |
| 321 | int scene_obj_set_valign(struct scene *scn, uint id, enum scene_obj_align aln) |
| 322 | { |
| 323 | struct scene_obj *obj; |
| 324 | |
| 325 | obj = scene_obj_find(scn, id, SCENEOBJT_NONE); |
| 326 | if (!obj) |
| 327 | return log_msg_ret("osv", -ENOENT); |
| 328 | obj->vert = aln; |
| 329 | |
| 330 | return 0; |
| 331 | } |
| 332 | |
Simon Glass | 0a4d14b | 2023-01-06 08:52:37 -0600 | [diff] [blame] | 333 | int scene_obj_set_hide(struct scene *scn, uint id, bool hide) |
| 334 | { |
Simon Glass | 6081b0f | 2023-06-01 10:22:50 -0600 | [diff] [blame] | 335 | int ret; |
| 336 | |
| 337 | ret = scene_obj_flag_clrset(scn, id, SCENEOF_HIDE, |
| 338 | hide ? SCENEOF_HIDE : 0); |
| 339 | if (ret) |
| 340 | return log_msg_ret("flg", ret); |
| 341 | |
| 342 | return 0; |
| 343 | } |
| 344 | |
| 345 | int scene_obj_flag_clrset(struct scene *scn, uint id, uint clr, uint set) |
| 346 | { |
Simon Glass | 0a4d14b | 2023-01-06 08:52:37 -0600 | [diff] [blame] | 347 | struct scene_obj *obj; |
| 348 | |
| 349 | obj = scene_obj_find(scn, id, SCENEOBJT_NONE); |
| 350 | if (!obj) |
| 351 | return log_msg_ret("find", -ENOENT); |
Simon Glass | 6081b0f | 2023-06-01 10:22:50 -0600 | [diff] [blame] | 352 | obj->flags &= ~clr; |
| 353 | obj->flags |= set; |
Simon Glass | 0a4d14b | 2023-01-06 08:52:37 -0600 | [diff] [blame] | 354 | |
| 355 | return 0; |
| 356 | } |
| 357 | |
Simon Glass | 5beb057 | 2025-05-02 08:46:45 -0600 | [diff] [blame^] | 358 | static void handle_alignment(enum scene_obj_align horiz, |
| 359 | enum scene_obj_align vert, |
| 360 | struct scene_obj_bbox *bbox, |
| 361 | struct scene_obj_dims *dims, |
| 362 | int xsize, int ysize, |
| 363 | struct scene_obj_offset *offset) |
| 364 | { |
| 365 | int width, height; |
| 366 | |
| 367 | width = bbox->x1 - bbox->x0; |
| 368 | height = bbox->y1 - bbox->y0; |
| 369 | |
| 370 | switch (horiz) { |
| 371 | case SCENEOA_CENTRE: |
| 372 | offset->xofs = (width - dims->x) / 2; |
| 373 | break; |
| 374 | case SCENEOA_RIGHT: |
| 375 | offset->xofs = width - dims->x; |
| 376 | break; |
| 377 | case SCENEOA_LEFT: |
| 378 | offset->xofs = 0; |
| 379 | break; |
| 380 | } |
| 381 | |
| 382 | switch (vert) { |
| 383 | case SCENEOA_CENTRE: |
| 384 | offset->yofs = (height - dims->y) / 2; |
| 385 | break; |
| 386 | case SCENEOA_BOTTOM: |
| 387 | offset->yofs = height - dims->y; |
| 388 | break; |
| 389 | case SCENEOA_TOP: |
| 390 | default: |
| 391 | offset->yofs = 0; |
| 392 | break; |
| 393 | } |
| 394 | } |
| 395 | |
Simon Glass | 0a4d14b | 2023-01-06 08:52:37 -0600 | [diff] [blame] | 396 | int scene_obj_get_hw(struct scene *scn, uint id, int *widthp) |
| 397 | { |
| 398 | struct scene_obj *obj; |
| 399 | |
| 400 | obj = scene_obj_find(scn, id, SCENEOBJT_NONE); |
| 401 | if (!obj) |
| 402 | return log_msg_ret("find", -ENOENT); |
| 403 | |
| 404 | switch (obj->type) { |
| 405 | case SCENEOBJT_NONE: |
| 406 | case SCENEOBJT_MENU: |
Simon Glass | 0023d18 | 2023-10-01 19:13:34 -0600 | [diff] [blame] | 407 | case SCENEOBJT_TEXTLINE: |
Simon Glass | 138a397 | 2025-05-02 08:46:44 -0600 | [diff] [blame] | 408 | case SCENEOBJT_BOX: |
Simon Glass | 0a4d14b | 2023-01-06 08:52:37 -0600 | [diff] [blame] | 409 | break; |
| 410 | case SCENEOBJT_IMAGE: { |
| 411 | struct scene_obj_img *img = (struct scene_obj_img *)obj; |
| 412 | ulong width, height; |
| 413 | uint bpix; |
| 414 | |
| 415 | video_bmp_get_info(img->data, &width, &height, &bpix); |
| 416 | if (widthp) |
| 417 | *widthp = width; |
| 418 | return height; |
| 419 | } |
| 420 | case SCENEOBJT_TEXT: { |
Simon Glass | 9ef02aa | 2025-05-02 08:46:37 -0600 | [diff] [blame] | 421 | struct scene_txt_generic *gen = &((struct scene_obj_txt *)obj)->gen; |
Simon Glass | 0a4d14b | 2023-01-06 08:52:37 -0600 | [diff] [blame] | 422 | struct expo *exp = scn->expo; |
Simon Glass | 9e1a86d | 2023-06-01 10:22:51 -0600 | [diff] [blame] | 423 | struct vidconsole_bbox bbox; |
Simon Glass | cfb4f2c | 2025-05-02 08:46:42 -0600 | [diff] [blame] | 424 | int len, ret, limit; |
Simon Glass | 9e1a86d | 2023-06-01 10:22:51 -0600 | [diff] [blame] | 425 | const char *str; |
Simon Glass | 0a4d14b | 2023-01-06 08:52:37 -0600 | [diff] [blame] | 426 | |
Simon Glass | 9ef02aa | 2025-05-02 08:46:37 -0600 | [diff] [blame] | 427 | str = expo_get_str(exp, gen->str_id); |
Simon Glass | 9e1a86d | 2023-06-01 10:22:51 -0600 | [diff] [blame] | 428 | if (!str) |
| 429 | return log_msg_ret("str", -ENOENT); |
| 430 | len = strlen(str); |
| 431 | |
| 432 | /* if there is no console, make it up */ |
| 433 | if (!exp->cons) { |
| 434 | if (widthp) |
| 435 | *widthp = 8 * len; |
| 436 | return 16; |
| 437 | } |
| 438 | |
Simon Glass | cfb4f2c | 2025-05-02 08:46:42 -0600 | [diff] [blame] | 439 | limit = obj->flags & SCENEOF_SIZE_VALID ? |
| 440 | obj->bbox.x1 - obj->bbox.x0 : -1; |
| 441 | |
Simon Glass | 9ef02aa | 2025-05-02 08:46:37 -0600 | [diff] [blame] | 442 | ret = vidconsole_measure(scn->expo->cons, gen->font_name, |
Simon Glass | cfb4f2c | 2025-05-02 08:46:42 -0600 | [diff] [blame] | 443 | gen->font_size, str, limit, &bbox, |
| 444 | &gen->lines); |
Simon Glass | 9e1a86d | 2023-06-01 10:22:51 -0600 | [diff] [blame] | 445 | if (ret) |
| 446 | return log_msg_ret("mea", ret); |
Simon Glass | 0a4d14b | 2023-01-06 08:52:37 -0600 | [diff] [blame] | 447 | if (widthp) |
Simon Glass | 9e1a86d | 2023-06-01 10:22:51 -0600 | [diff] [blame] | 448 | *widthp = bbox.x1; |
Simon Glass | 0a4d14b | 2023-01-06 08:52:37 -0600 | [diff] [blame] | 449 | |
Simon Glass | 9e1a86d | 2023-06-01 10:22:51 -0600 | [diff] [blame] | 450 | return bbox.y1; |
Simon Glass | 0a4d14b | 2023-01-06 08:52:37 -0600 | [diff] [blame] | 451 | } |
| 452 | } |
| 453 | |
| 454 | return 0; |
| 455 | } |
| 456 | |
| 457 | /** |
Simon Glass | 118a727 | 2023-10-01 19:13:30 -0600 | [diff] [blame] | 458 | * scene_render_background() - Render the background for an object |
| 459 | * |
| 460 | * @obj: Object to render |
Simon Glass | 0023d18 | 2023-10-01 19:13:34 -0600 | [diff] [blame] | 461 | * @box_only: true to show a box around the object, but keep the normal |
| 462 | * background colour inside |
Simon Glass | 118a727 | 2023-10-01 19:13:30 -0600 | [diff] [blame] | 463 | */ |
Simon Glass | 0023d18 | 2023-10-01 19:13:34 -0600 | [diff] [blame] | 464 | static void scene_render_background(struct scene_obj *obj, bool box_only) |
Simon Glass | 118a727 | 2023-10-01 19:13:30 -0600 | [diff] [blame] | 465 | { |
Simon Glass | 96910ef | 2025-05-02 08:46:34 -0600 | [diff] [blame] | 466 | struct vidconsole_bbox bbox[SCENEBB_count], *sel; |
Simon Glass | 118a727 | 2023-10-01 19:13:30 -0600 | [diff] [blame] | 467 | struct expo *exp = obj->scene->expo; |
| 468 | const struct expo_theme *theme = &exp->theme; |
Simon Glass | 118a727 | 2023-10-01 19:13:30 -0600 | [diff] [blame] | 469 | struct udevice *dev = exp->display; |
| 470 | struct video_priv *vid_priv; |
| 471 | struct udevice *cons = exp->cons; |
| 472 | struct vidconsole_colour old; |
| 473 | enum colour_idx fore, back; |
| 474 | uint inset = theme->menu_inset; |
| 475 | |
Simon Glass | 21320da | 2025-04-02 06:29:33 +1300 | [diff] [blame] | 476 | vid_priv = dev_get_uclass_priv(dev); |
Simon Glass | 118a727 | 2023-10-01 19:13:30 -0600 | [diff] [blame] | 477 | /* draw a background for the object */ |
Simon Glass | 21320da | 2025-04-02 06:29:33 +1300 | [diff] [blame] | 478 | if (vid_priv->white_on_black) { |
Simon Glass | bda3adc | 2024-10-14 16:31:53 -0600 | [diff] [blame] | 479 | fore = VID_DARK_GREY; |
Simon Glass | 118a727 | 2023-10-01 19:13:30 -0600 | [diff] [blame] | 480 | back = VID_WHITE; |
| 481 | } else { |
| 482 | fore = VID_LIGHT_GRAY; |
| 483 | back = VID_BLACK; |
| 484 | } |
| 485 | |
| 486 | /* see if this object wants to render a background */ |
Simon Glass | 96910ef | 2025-05-02 08:46:34 -0600 | [diff] [blame] | 487 | if (scene_obj_calc_bbox(obj, bbox)) |
Simon Glass | 118a727 | 2023-10-01 19:13:30 -0600 | [diff] [blame] | 488 | return; |
| 489 | |
Simon Glass | 96910ef | 2025-05-02 08:46:34 -0600 | [diff] [blame] | 490 | sel = &bbox[SCENEBB_label]; |
| 491 | if (!sel->valid) |
| 492 | return; |
| 493 | |
Simon Glass | 118a727 | 2023-10-01 19:13:30 -0600 | [diff] [blame] | 494 | vidconsole_push_colour(cons, fore, back, &old); |
Simon Glass | 96910ef | 2025-05-02 08:46:34 -0600 | [diff] [blame] | 495 | video_fill_part(dev, sel->x0 - inset, sel->y0 - inset, |
| 496 | sel->x1 + inset, sel->y1 + inset, |
Simon Glass | 118a727 | 2023-10-01 19:13:30 -0600 | [diff] [blame] | 497 | vid_priv->colour_fg); |
| 498 | vidconsole_pop_colour(cons, &old); |
Simon Glass | 0023d18 | 2023-10-01 19:13:34 -0600 | [diff] [blame] | 499 | if (box_only) { |
Simon Glass | 96910ef | 2025-05-02 08:46:34 -0600 | [diff] [blame] | 500 | video_fill_part(dev, sel->x0, sel->y0, sel->x1, sel->y1, |
Simon Glass | 0023d18 | 2023-10-01 19:13:34 -0600 | [diff] [blame] | 501 | vid_priv->colour_bg); |
| 502 | } |
Simon Glass | 118a727 | 2023-10-01 19:13:30 -0600 | [diff] [blame] | 503 | } |
| 504 | |
Simon Glass | a841d1a | 2025-05-02 08:46:38 -0600 | [diff] [blame] | 505 | static int scene_txt_render(struct expo *exp, struct udevice *dev, |
| 506 | struct udevice *cons, struct scene_obj *obj, |
| 507 | struct scene_txt_generic *gen, int x, int y, |
| 508 | int menu_inset) |
| 509 | { |
Simon Glass | 5beb057 | 2025-05-02 08:46:45 -0600 | [diff] [blame^] | 510 | const struct vidconsole_mline *mline, *last; |
Simon Glass | 62f39d2 | 2025-05-02 08:46:39 -0600 | [diff] [blame] | 511 | struct video_priv *vid_priv; |
| 512 | struct vidconsole_colour old; |
| 513 | enum colour_idx fore, back; |
Simon Glass | 5beb057 | 2025-05-02 08:46:45 -0600 | [diff] [blame^] | 514 | struct scene_obj_dims dims; |
| 515 | struct scene_obj_bbox bbox; |
Simon Glass | a841d1a | 2025-05-02 08:46:38 -0600 | [diff] [blame] | 516 | const char *str; |
| 517 | int ret; |
| 518 | |
| 519 | if (!cons) |
| 520 | return -ENOTSUPP; |
| 521 | |
| 522 | if (gen->font_name || gen->font_size) { |
| 523 | ret = vidconsole_select_font(cons, gen->font_name, |
| 524 | gen->font_size); |
| 525 | } else { |
| 526 | ret = vidconsole_select_font(cons, NULL, 0); |
| 527 | } |
| 528 | if (ret && ret != -ENOSYS) |
| 529 | return log_msg_ret("font", ret); |
| 530 | str = expo_get_str(exp, gen->str_id); |
Simon Glass | 62f39d2 | 2025-05-02 08:46:39 -0600 | [diff] [blame] | 531 | if (!str) |
| 532 | return 0; |
Simon Glass | a841d1a | 2025-05-02 08:46:38 -0600 | [diff] [blame] | 533 | |
Simon Glass | 62f39d2 | 2025-05-02 08:46:39 -0600 | [diff] [blame] | 534 | vid_priv = dev_get_uclass_priv(dev); |
| 535 | if (vid_priv->white_on_black) { |
| 536 | fore = VID_BLACK; |
| 537 | back = VID_WHITE; |
| 538 | } else { |
| 539 | fore = VID_LIGHT_GRAY; |
| 540 | back = VID_BLACK; |
| 541 | } |
Simon Glass | a841d1a | 2025-05-02 08:46:38 -0600 | [diff] [blame] | 542 | |
Simon Glass | 62f39d2 | 2025-05-02 08:46:39 -0600 | [diff] [blame] | 543 | if (obj->flags & SCENEOF_POINT) { |
| 544 | vidconsole_push_colour(cons, fore, back, &old); |
| 545 | video_fill_part(dev, x - menu_inset, y, obj->bbox.x1, |
| 546 | obj->bbox.y1, vid_priv->colour_bg); |
Simon Glass | a841d1a | 2025-05-02 08:46:38 -0600 | [diff] [blame] | 547 | } |
Simon Glass | cfb4f2c | 2025-05-02 08:46:42 -0600 | [diff] [blame] | 548 | |
Simon Glass | 5beb057 | 2025-05-02 08:46:45 -0600 | [diff] [blame^] | 549 | mline = alist_get(&gen->lines, 0, typeof(*mline)); |
| 550 | last = alist_get(&gen->lines, gen->lines.count - 1, typeof(*mline)); |
| 551 | if (mline) |
| 552 | dims.y = last->bbox.y1 - mline->bbox.y0; |
| 553 | bbox.y0 = obj->bbox.y0; |
| 554 | bbox.y1 = obj->bbox.y1; |
| 555 | |
| 556 | if (!mline) { |
Simon Glass | cfb4f2c | 2025-05-02 08:46:42 -0600 | [diff] [blame] | 557 | vidconsole_set_cursor_pos(cons, x, y); |
| 558 | vidconsole_put_string(cons, str); |
| 559 | } |
Simon Glass | 5beb057 | 2025-05-02 08:46:45 -0600 | [diff] [blame^] | 560 | |
Simon Glass | cfb4f2c | 2025-05-02 08:46:42 -0600 | [diff] [blame] | 561 | alist_for_each(mline, &gen->lines) { |
Simon Glass | 5beb057 | 2025-05-02 08:46:45 -0600 | [diff] [blame^] | 562 | struct scene_obj_offset offset; |
| 563 | |
| 564 | bbox.x0 = obj->bbox.x0; |
| 565 | bbox.x1 = obj->bbox.x1; |
| 566 | dims.x = mline->bbox.x1 - mline->bbox.x0; |
| 567 | handle_alignment(obj->horiz, obj->vert, &bbox, &dims, |
| 568 | obj->bbox.x1 - obj->bbox.x0, |
| 569 | obj->bbox.y1 - obj->bbox.y0, &offset); |
| 570 | |
| 571 | x = obj->bbox.x0 + offset.xofs; |
| 572 | y = obj->bbox.y0 + offset.yofs + mline->bbox.y0; |
| 573 | if (y > bbox.y1) |
| 574 | break; /* clip this line and any following */ |
| 575 | vidconsole_set_cursor_pos(cons, x, y); |
Simon Glass | cfb4f2c | 2025-05-02 08:46:42 -0600 | [diff] [blame] | 576 | vidconsole_put_stringn(cons, str + mline->start, mline->len); |
| 577 | } |
Simon Glass | 62f39d2 | 2025-05-02 08:46:39 -0600 | [diff] [blame] | 578 | if (obj->flags & SCENEOF_POINT) |
| 579 | vidconsole_pop_colour(cons, &old); |
Simon Glass | a841d1a | 2025-05-02 08:46:38 -0600 | [diff] [blame] | 580 | |
| 581 | return 0; |
| 582 | } |
| 583 | |
Simon Glass | 118a727 | 2023-10-01 19:13:30 -0600 | [diff] [blame] | 584 | /** |
Simon Glass | 0a4d14b | 2023-01-06 08:52:37 -0600 | [diff] [blame] | 585 | * scene_obj_render() - Render an object |
| 586 | * |
Simon Glass | cfb4f2c | 2025-05-02 08:46:42 -0600 | [diff] [blame] | 587 | * @obj: Object to render |
| 588 | * @text_mode: true to use text mode |
| 589 | * Return: 0 if OK, -ve on error |
Simon Glass | 0a4d14b | 2023-01-06 08:52:37 -0600 | [diff] [blame] | 590 | */ |
| 591 | static int scene_obj_render(struct scene_obj *obj, bool text_mode) |
| 592 | { |
| 593 | struct scene *scn = obj->scene; |
| 594 | struct expo *exp = scn->expo; |
Simon Glass | 86f1ac5 | 2023-06-01 10:23:00 -0600 | [diff] [blame] | 595 | const struct expo_theme *theme = &exp->theme; |
Simon Glass | 67e2af1 | 2023-06-01 10:22:34 -0600 | [diff] [blame] | 596 | struct udevice *dev = exp->display; |
| 597 | struct udevice *cons = text_mode ? NULL : exp->cons; |
Simon Glass | 138a397 | 2025-05-02 08:46:44 -0600 | [diff] [blame] | 598 | struct video_priv *vid_priv; |
Simon Glass | 0a4d14b | 2023-01-06 08:52:37 -0600 | [diff] [blame] | 599 | int x, y, ret; |
| 600 | |
Simon Glass | bc3a15f | 2025-05-02 08:46:31 -0600 | [diff] [blame] | 601 | y = obj->bbox.y0; |
Simon Glass | 5beb057 | 2025-05-02 08:46:45 -0600 | [diff] [blame^] | 602 | x = obj->bbox.x0 + obj->ofs.xofs; |
Simon Glass | 138a397 | 2025-05-02 08:46:44 -0600 | [diff] [blame] | 603 | vid_priv = dev_get_uclass_priv(dev); |
Simon Glass | 0a4d14b | 2023-01-06 08:52:37 -0600 | [diff] [blame] | 604 | |
| 605 | switch (obj->type) { |
| 606 | case SCENEOBJT_NONE: |
| 607 | break; |
| 608 | case SCENEOBJT_IMAGE: { |
| 609 | struct scene_obj_img *img = (struct scene_obj_img *)obj; |
| 610 | |
| 611 | if (!cons) |
| 612 | return -ENOTSUPP; |
| 613 | ret = video_bmp_display(dev, map_to_sysmem(img->data), x, y, |
| 614 | true); |
| 615 | if (ret < 0) |
| 616 | return log_msg_ret("img", ret); |
| 617 | break; |
| 618 | } |
| 619 | case SCENEOBJT_TEXT: { |
Simon Glass | a841d1a | 2025-05-02 08:46:38 -0600 | [diff] [blame] | 620 | struct scene_obj_txt *txt = (struct scene_obj_txt *)obj; |
Simon Glass | 0a4d14b | 2023-01-06 08:52:37 -0600 | [diff] [blame] | 621 | |
Simon Glass | a841d1a | 2025-05-02 08:46:38 -0600 | [diff] [blame] | 622 | ret = scene_txt_render(exp, dev, cons, obj, &txt->gen, x, y, |
| 623 | theme->menu_inset); |
Simon Glass | 0a4d14b | 2023-01-06 08:52:37 -0600 | [diff] [blame] | 624 | break; |
| 625 | } |
| 626 | case SCENEOBJT_MENU: { |
| 627 | struct scene_obj_menu *menu = (struct scene_obj_menu *)obj; |
Simon Glass | 01922ec | 2023-06-01 10:22:57 -0600 | [diff] [blame] | 628 | |
| 629 | if (exp->popup && (obj->flags & SCENEOF_OPEN)) { |
| 630 | if (!cons) |
| 631 | return -ENOTSUPP; |
| 632 | |
| 633 | /* draw a background behind the menu items */ |
Simon Glass | 0023d18 | 2023-10-01 19:13:34 -0600 | [diff] [blame] | 634 | scene_render_background(obj, false); |
Simon Glass | 01922ec | 2023-06-01 10:22:57 -0600 | [diff] [blame] | 635 | } |
Simon Glass | 0a4d14b | 2023-01-06 08:52:37 -0600 | [diff] [blame] | 636 | /* |
| 637 | * With a vidconsole, the text and item pointer are rendered as |
| 638 | * normal objects so we don't need to do anything here. The menu |
| 639 | * simply controls where they are positioned. |
| 640 | */ |
| 641 | if (cons) |
| 642 | return -ENOTSUPP; |
| 643 | |
| 644 | ret = scene_menu_display(menu); |
| 645 | if (ret < 0) |
| 646 | return log_msg_ret("img", ret); |
| 647 | |
| 648 | break; |
| 649 | } |
Simon Glass | 0023d18 | 2023-10-01 19:13:34 -0600 | [diff] [blame] | 650 | case SCENEOBJT_TEXTLINE: |
| 651 | if (obj->flags & SCENEOF_OPEN) |
| 652 | scene_render_background(obj, true); |
| 653 | break; |
Simon Glass | 138a397 | 2025-05-02 08:46:44 -0600 | [diff] [blame] | 654 | case SCENEOBJT_BOX: { |
| 655 | struct scene_obj_box *box = (struct scene_obj_box *)obj; |
| 656 | |
| 657 | video_draw_box(dev, obj->bbox.x0, obj->bbox.y0, obj->bbox.x1, |
| 658 | obj->bbox.y1, box->width, vid_priv->colour_fg); |
| 659 | break; |
Simon Glass | 0a4d14b | 2023-01-06 08:52:37 -0600 | [diff] [blame] | 660 | } |
Simon Glass | 138a397 | 2025-05-02 08:46:44 -0600 | [diff] [blame] | 661 | } |
Simon Glass | 0a4d14b | 2023-01-06 08:52:37 -0600 | [diff] [blame] | 662 | |
| 663 | return 0; |
| 664 | } |
| 665 | |
Simon Glass | 377f18e | 2024-10-14 16:31:55 -0600 | [diff] [blame] | 666 | int scene_calc_arrange(struct scene *scn, struct expo_arrange_info *arr) |
| 667 | { |
| 668 | struct scene_obj *obj; |
| 669 | |
| 670 | arr->label_width = 0; |
| 671 | list_for_each_entry(obj, &scn->obj_head, sibling) { |
| 672 | uint label_id = 0; |
| 673 | int width; |
| 674 | |
| 675 | switch (obj->type) { |
| 676 | case SCENEOBJT_NONE: |
| 677 | case SCENEOBJT_IMAGE: |
| 678 | case SCENEOBJT_TEXT: |
Simon Glass | 138a397 | 2025-05-02 08:46:44 -0600 | [diff] [blame] | 679 | case SCENEOBJT_BOX: |
Simon Glass | 377f18e | 2024-10-14 16:31:55 -0600 | [diff] [blame] | 680 | break; |
| 681 | case SCENEOBJT_MENU: { |
| 682 | struct scene_obj_menu *menu; |
| 683 | |
| 684 | menu = (struct scene_obj_menu *)obj, |
| 685 | label_id = menu->title_id; |
| 686 | break; |
| 687 | } |
| 688 | case SCENEOBJT_TEXTLINE: { |
| 689 | struct scene_obj_textline *tline; |
| 690 | |
| 691 | tline = (struct scene_obj_textline *)obj, |
| 692 | label_id = tline->label_id; |
| 693 | break; |
| 694 | } |
| 695 | } |
| 696 | |
| 697 | if (label_id) { |
| 698 | int ret; |
| 699 | |
| 700 | ret = scene_obj_get_hw(scn, label_id, &width); |
| 701 | if (ret < 0) |
| 702 | return log_msg_ret("hei", ret); |
| 703 | arr->label_width = max(arr->label_width, width); |
| 704 | } |
| 705 | } |
| 706 | |
| 707 | return 0; |
| 708 | } |
| 709 | |
Simon Glass | 0a4d14b | 2023-01-06 08:52:37 -0600 | [diff] [blame] | 710 | int scene_arrange(struct scene *scn) |
| 711 | { |
Simon Glass | 377f18e | 2024-10-14 16:31:55 -0600 | [diff] [blame] | 712 | struct expo_arrange_info arr; |
Simon Glass | 5beb057 | 2025-05-02 08:46:45 -0600 | [diff] [blame^] | 713 | int xsize = 0, ysize = 0; |
Simon Glass | 0a4d14b | 2023-01-06 08:52:37 -0600 | [diff] [blame] | 714 | struct scene_obj *obj; |
Simon Glass | 5beb057 | 2025-05-02 08:46:45 -0600 | [diff] [blame^] | 715 | struct udevice *dev; |
Simon Glass | 0a4d14b | 2023-01-06 08:52:37 -0600 | [diff] [blame] | 716 | int ret; |
| 717 | |
Simon Glass | 5beb057 | 2025-05-02 08:46:45 -0600 | [diff] [blame^] | 718 | dev = scn->expo->display; |
| 719 | if (dev) { |
| 720 | struct video_priv *priv = dev_get_uclass_priv(dev); |
| 721 | |
| 722 | xsize = priv->xsize; |
| 723 | ysize = priv->ysize; |
| 724 | } |
| 725 | |
Simon Glass | 377f18e | 2024-10-14 16:31:55 -0600 | [diff] [blame] | 726 | ret = scene_calc_arrange(scn, &arr); |
| 727 | if (ret < 0) |
| 728 | return log_msg_ret("arr", ret); |
| 729 | |
Simon Glass | 0a4d14b | 2023-01-06 08:52:37 -0600 | [diff] [blame] | 730 | list_for_each_entry(obj, &scn->obj_head, sibling) { |
Simon Glass | 5beb057 | 2025-05-02 08:46:45 -0600 | [diff] [blame^] | 731 | handle_alignment(obj->horiz, obj->vert, &obj->bbox, &obj->dims, |
| 732 | xsize, ysize, &obj->ofs); |
| 733 | |
Simon Glass | b7a6453 | 2023-10-01 19:13:24 -0600 | [diff] [blame] | 734 | switch (obj->type) { |
| 735 | case SCENEOBJT_NONE: |
| 736 | case SCENEOBJT_IMAGE: |
| 737 | case SCENEOBJT_TEXT: |
Simon Glass | 138a397 | 2025-05-02 08:46:44 -0600 | [diff] [blame] | 738 | case SCENEOBJT_BOX: |
Simon Glass | b7a6453 | 2023-10-01 19:13:24 -0600 | [diff] [blame] | 739 | break; |
| 740 | case SCENEOBJT_MENU: { |
Simon Glass | 0a4d14b | 2023-01-06 08:52:37 -0600 | [diff] [blame] | 741 | struct scene_obj_menu *menu; |
| 742 | |
| 743 | menu = (struct scene_obj_menu *)obj, |
Simon Glass | 377f18e | 2024-10-14 16:31:55 -0600 | [diff] [blame] | 744 | ret = scene_menu_arrange(scn, &arr, menu); |
Simon Glass | 0a4d14b | 2023-01-06 08:52:37 -0600 | [diff] [blame] | 745 | if (ret) |
| 746 | return log_msg_ret("arr", ret); |
Simon Glass | b7a6453 | 2023-10-01 19:13:24 -0600 | [diff] [blame] | 747 | break; |
| 748 | } |
Simon Glass | 0023d18 | 2023-10-01 19:13:34 -0600 | [diff] [blame] | 749 | case SCENEOBJT_TEXTLINE: { |
| 750 | struct scene_obj_textline *tline; |
| 751 | |
| 752 | tline = (struct scene_obj_textline *)obj, |
Simon Glass | 377f18e | 2024-10-14 16:31:55 -0600 | [diff] [blame] | 753 | ret = scene_textline_arrange(scn, &arr, tline); |
Simon Glass | 0023d18 | 2023-10-01 19:13:34 -0600 | [diff] [blame] | 754 | if (ret) |
| 755 | return log_msg_ret("arr", ret); |
| 756 | break; |
| 757 | } |
Simon Glass | 0a4d14b | 2023-01-06 08:52:37 -0600 | [diff] [blame] | 758 | } |
| 759 | } |
| 760 | |
| 761 | return 0; |
| 762 | } |
| 763 | |
Simon Glass | 12f5773 | 2023-06-01 10:22:58 -0600 | [diff] [blame] | 764 | int scene_render_deps(struct scene *scn, uint id) |
| 765 | { |
| 766 | struct scene_obj *obj; |
| 767 | int ret; |
| 768 | |
| 769 | if (!id) |
| 770 | return 0; |
| 771 | obj = scene_obj_find(scn, id, SCENEOBJT_NONE); |
| 772 | if (!obj) |
| 773 | return log_msg_ret("obj", -ENOENT); |
| 774 | |
| 775 | if (!(obj->flags & SCENEOF_HIDE)) { |
| 776 | ret = scene_obj_render(obj, false); |
| 777 | if (ret && ret != -ENOTSUPP) |
| 778 | return log_msg_ret("ren", ret); |
| 779 | |
Simon Glass | b7a6453 | 2023-10-01 19:13:24 -0600 | [diff] [blame] | 780 | switch (obj->type) { |
| 781 | case SCENEOBJT_NONE: |
| 782 | case SCENEOBJT_IMAGE: |
| 783 | case SCENEOBJT_TEXT: |
Simon Glass | 138a397 | 2025-05-02 08:46:44 -0600 | [diff] [blame] | 784 | case SCENEOBJT_BOX: |
Simon Glass | b7a6453 | 2023-10-01 19:13:24 -0600 | [diff] [blame] | 785 | break; |
| 786 | case SCENEOBJT_MENU: |
Simon Glass | 12f5773 | 2023-06-01 10:22:58 -0600 | [diff] [blame] | 787 | scene_menu_render_deps(scn, |
| 788 | (struct scene_obj_menu *)obj); |
Simon Glass | b7a6453 | 2023-10-01 19:13:24 -0600 | [diff] [blame] | 789 | break; |
Simon Glass | 0023d18 | 2023-10-01 19:13:34 -0600 | [diff] [blame] | 790 | case SCENEOBJT_TEXTLINE: |
| 791 | scene_textline_render_deps(scn, |
| 792 | (struct scene_obj_textline *)obj); |
| 793 | break; |
Simon Glass | b7a6453 | 2023-10-01 19:13:24 -0600 | [diff] [blame] | 794 | } |
Simon Glass | 12f5773 | 2023-06-01 10:22:58 -0600 | [diff] [blame] | 795 | } |
| 796 | |
| 797 | return 0; |
| 798 | } |
| 799 | |
Simon Glass | 0a4d14b | 2023-01-06 08:52:37 -0600 | [diff] [blame] | 800 | int scene_render(struct scene *scn) |
| 801 | { |
| 802 | struct expo *exp = scn->expo; |
| 803 | struct scene_obj *obj; |
| 804 | int ret; |
| 805 | |
| 806 | list_for_each_entry(obj, &scn->obj_head, sibling) { |
Simon Glass | 6081b0f | 2023-06-01 10:22:50 -0600 | [diff] [blame] | 807 | if (!(obj->flags & SCENEOF_HIDE)) { |
Simon Glass | 0a4d14b | 2023-01-06 08:52:37 -0600 | [diff] [blame] | 808 | ret = scene_obj_render(obj, exp->text_mode); |
| 809 | if (ret && ret != -ENOTSUPP) |
| 810 | return log_msg_ret("ren", ret); |
| 811 | } |
| 812 | } |
| 813 | |
Simon Glass | 12f5773 | 2023-06-01 10:22:58 -0600 | [diff] [blame] | 814 | /* render any highlighted object on top of the others */ |
| 815 | if (scn->highlight_id && !exp->text_mode) { |
| 816 | ret = scene_render_deps(scn, scn->highlight_id); |
| 817 | if (ret && ret != -ENOTSUPP) |
| 818 | return log_msg_ret("dep", ret); |
| 819 | } |
| 820 | |
Simon Glass | 0a4d14b | 2023-01-06 08:52:37 -0600 | [diff] [blame] | 821 | return 0; |
| 822 | } |
| 823 | |
Simon Glass | f0e1e8c | 2023-06-01 10:22:59 -0600 | [diff] [blame] | 824 | /** |
| 825 | * send_key_obj() - Handle a keypress for moving between objects |
| 826 | * |
| 827 | * @scn: Scene to receive the key |
| 828 | * @key: Key to send (KEYCODE_UP) |
| 829 | * @event: Returns resulting event from this keypress |
| 830 | * Returns: 0 if OK, -ve on error |
| 831 | */ |
| 832 | static void send_key_obj(struct scene *scn, struct scene_obj *obj, int key, |
| 833 | struct expo_action *event) |
| 834 | { |
| 835 | switch (key) { |
| 836 | case BKEY_UP: |
| 837 | while (obj != list_first_entry(&scn->obj_head, struct scene_obj, |
| 838 | sibling)) { |
| 839 | obj = list_entry(obj->sibling.prev, |
| 840 | struct scene_obj, sibling); |
Simon Glass | 193bfea | 2023-10-01 19:13:27 -0600 | [diff] [blame] | 841 | if (scene_obj_can_highlight(obj)) { |
Simon Glass | f0e1e8c | 2023-06-01 10:22:59 -0600 | [diff] [blame] | 842 | event->type = EXPOACT_POINT_OBJ; |
| 843 | event->select.id = obj->id; |
| 844 | log_debug("up to obj %d\n", event->select.id); |
| 845 | break; |
| 846 | } |
| 847 | } |
| 848 | break; |
| 849 | case BKEY_DOWN: |
| 850 | while (!list_is_last(&obj->sibling, &scn->obj_head)) { |
| 851 | obj = list_entry(obj->sibling.next, struct scene_obj, |
| 852 | sibling); |
Simon Glass | 193bfea | 2023-10-01 19:13:27 -0600 | [diff] [blame] | 853 | if (scene_obj_can_highlight(obj)) { |
Simon Glass | f0e1e8c | 2023-06-01 10:22:59 -0600 | [diff] [blame] | 854 | event->type = EXPOACT_POINT_OBJ; |
| 855 | event->select.id = obj->id; |
| 856 | log_debug("down to obj %d\n", event->select.id); |
| 857 | break; |
| 858 | } |
| 859 | } |
| 860 | break; |
| 861 | case BKEY_SELECT: |
Simon Glass | 193bfea | 2023-10-01 19:13:27 -0600 | [diff] [blame] | 862 | if (scene_obj_can_highlight(obj)) { |
Simon Glass | f0e1e8c | 2023-06-01 10:22:59 -0600 | [diff] [blame] | 863 | event->type = EXPOACT_OPEN; |
| 864 | event->select.id = obj->id; |
| 865 | log_debug("open obj %d\n", event->select.id); |
| 866 | } |
| 867 | break; |
| 868 | case BKEY_QUIT: |
| 869 | event->type = EXPOACT_QUIT; |
| 870 | log_debug("obj quit\n"); |
| 871 | break; |
| 872 | } |
| 873 | } |
| 874 | |
Simon Glass | 0a4d14b | 2023-01-06 08:52:37 -0600 | [diff] [blame] | 875 | int scene_send_key(struct scene *scn, int key, struct expo_action *event) |
| 876 | { |
| 877 | struct scene_obj *obj; |
| 878 | int ret; |
| 879 | |
Simon Glass | f0e1e8c | 2023-06-01 10:22:59 -0600 | [diff] [blame] | 880 | event->type = EXPOACT_NONE; |
| 881 | |
| 882 | /* |
| 883 | * In 'popup' mode, arrow keys move betwen objects, unless a menu is |
| 884 | * opened |
| 885 | */ |
| 886 | if (scn->expo->popup) { |
| 887 | obj = NULL; |
| 888 | if (scn->highlight_id) { |
| 889 | obj = scene_obj_find(scn, scn->highlight_id, |
| 890 | SCENEOBJT_NONE); |
| 891 | } |
| 892 | if (!obj) |
| 893 | return 0; |
| 894 | |
| 895 | if (!(obj->flags & SCENEOF_OPEN)) { |
| 896 | send_key_obj(scn, obj, key, event); |
| 897 | return 0; |
| 898 | } |
| 899 | |
Simon Glass | b7a6453 | 2023-10-01 19:13:24 -0600 | [diff] [blame] | 900 | switch (obj->type) { |
| 901 | case SCENEOBJT_NONE: |
| 902 | case SCENEOBJT_IMAGE: |
| 903 | case SCENEOBJT_TEXT: |
Simon Glass | 138a397 | 2025-05-02 08:46:44 -0600 | [diff] [blame] | 904 | case SCENEOBJT_BOX: |
Simon Glass | b7a6453 | 2023-10-01 19:13:24 -0600 | [diff] [blame] | 905 | break; |
| 906 | case SCENEOBJT_MENU: { |
| 907 | struct scene_obj_menu *menu; |
| 908 | |
| 909 | menu = (struct scene_obj_menu *)obj, |
| 910 | ret = scene_menu_send_key(scn, menu, key, event); |
| 911 | if (ret) |
| 912 | return log_msg_ret("key", ret); |
| 913 | break; |
| 914 | } |
Simon Glass | 0023d18 | 2023-10-01 19:13:34 -0600 | [diff] [blame] | 915 | case SCENEOBJT_TEXTLINE: { |
| 916 | struct scene_obj_textline *tline; |
| 917 | |
| 918 | tline = (struct scene_obj_textline *)obj, |
| 919 | ret = scene_textline_send_key(scn, tline, key, event); |
| 920 | if (ret) |
| 921 | return log_msg_ret("key", ret); |
| 922 | break; |
| 923 | } |
Simon Glass | b7a6453 | 2023-10-01 19:13:24 -0600 | [diff] [blame] | 924 | } |
Simon Glass | f0e1e8c | 2023-06-01 10:22:59 -0600 | [diff] [blame] | 925 | return 0; |
| 926 | } |
| 927 | |
Simon Glass | 0a4d14b | 2023-01-06 08:52:37 -0600 | [diff] [blame] | 928 | list_for_each_entry(obj, &scn->obj_head, sibling) { |
| 929 | if (obj->type == SCENEOBJT_MENU) { |
| 930 | struct scene_obj_menu *menu; |
| 931 | |
| 932 | menu = (struct scene_obj_menu *)obj, |
| 933 | ret = scene_menu_send_key(scn, menu, key, event); |
| 934 | if (ret) |
| 935 | return log_msg_ret("key", ret); |
Simon Glass | 0a4d14b | 2023-01-06 08:52:37 -0600 | [diff] [blame] | 936 | break; |
| 937 | } |
| 938 | } |
| 939 | |
| 940 | return 0; |
| 941 | } |
Simon Glass | 7a96005 | 2023-06-01 10:22:52 -0600 | [diff] [blame] | 942 | |
Simon Glass | 96910ef | 2025-05-02 08:46:34 -0600 | [diff] [blame] | 943 | int scene_obj_calc_bbox(struct scene_obj *obj, struct vidconsole_bbox bbox[]) |
Simon Glass | f099469 | 2023-10-01 19:13:29 -0600 | [diff] [blame] | 944 | { |
| 945 | switch (obj->type) { |
| 946 | case SCENEOBJT_NONE: |
| 947 | case SCENEOBJT_IMAGE: |
| 948 | case SCENEOBJT_TEXT: |
Simon Glass | 138a397 | 2025-05-02 08:46:44 -0600 | [diff] [blame] | 949 | case SCENEOBJT_BOX: |
Simon Glass | f099469 | 2023-10-01 19:13:29 -0600 | [diff] [blame] | 950 | return -ENOSYS; |
| 951 | case SCENEOBJT_MENU: { |
| 952 | struct scene_obj_menu *menu = (struct scene_obj_menu *)obj; |
| 953 | |
Simon Glass | 96910ef | 2025-05-02 08:46:34 -0600 | [diff] [blame] | 954 | scene_menu_calc_bbox(menu, bbox); |
Simon Glass | f099469 | 2023-10-01 19:13:29 -0600 | [diff] [blame] | 955 | break; |
| 956 | } |
Simon Glass | 0023d18 | 2023-10-01 19:13:34 -0600 | [diff] [blame] | 957 | case SCENEOBJT_TEXTLINE: { |
| 958 | struct scene_obj_textline *tline; |
| 959 | |
| 960 | tline = (struct scene_obj_textline *)obj; |
Simon Glass | 96910ef | 2025-05-02 08:46:34 -0600 | [diff] [blame] | 961 | scene_textline_calc_bbox(tline, &bbox[SCENEBB_all], |
| 962 | &bbox[SCENEBB_label]); |
Simon Glass | 0023d18 | 2023-10-01 19:13:34 -0600 | [diff] [blame] | 963 | break; |
| 964 | } |
Simon Glass | f099469 | 2023-10-01 19:13:29 -0600 | [diff] [blame] | 965 | } |
| 966 | |
| 967 | return 0; |
| 968 | } |
| 969 | |
Simon Glass | 7a96005 | 2023-06-01 10:22:52 -0600 | [diff] [blame] | 970 | int scene_calc_dims(struct scene *scn, bool do_menus) |
| 971 | { |
| 972 | struct scene_obj *obj; |
| 973 | int ret; |
| 974 | |
| 975 | list_for_each_entry(obj, &scn->obj_head, sibling) { |
| 976 | switch (obj->type) { |
| 977 | case SCENEOBJT_NONE: |
| 978 | case SCENEOBJT_TEXT: |
Simon Glass | 138a397 | 2025-05-02 08:46:44 -0600 | [diff] [blame] | 979 | case SCENEOBJT_BOX: |
Simon Glass | 7a96005 | 2023-06-01 10:22:52 -0600 | [diff] [blame] | 980 | case SCENEOBJT_IMAGE: { |
| 981 | int width; |
| 982 | |
| 983 | if (!do_menus) { |
| 984 | ret = scene_obj_get_hw(scn, obj->id, &width); |
| 985 | if (ret < 0) |
| 986 | return log_msg_ret("get", ret); |
Simon Glass | ebec497 | 2025-05-02 08:46:33 -0600 | [diff] [blame] | 987 | obj->dims.x = width; |
| 988 | obj->dims.y = ret; |
| 989 | if (!(obj->flags & SCENEOF_SIZE_VALID)) { |
| 990 | obj->bbox.x1 = obj->bbox.x0 + width; |
| 991 | obj->bbox.y1 = obj->bbox.y0 + ret; |
| 992 | obj->flags |= SCENEOF_SIZE_VALID; |
| 993 | } |
Simon Glass | 7a96005 | 2023-06-01 10:22:52 -0600 | [diff] [blame] | 994 | } |
| 995 | break; |
| 996 | } |
| 997 | case SCENEOBJT_MENU: { |
| 998 | struct scene_obj_menu *menu; |
| 999 | |
| 1000 | if (do_menus) { |
| 1001 | menu = (struct scene_obj_menu *)obj; |
| 1002 | |
| 1003 | ret = scene_menu_calc_dims(menu); |
| 1004 | if (ret) |
| 1005 | return log_msg_ret("men", ret); |
| 1006 | } |
| 1007 | break; |
| 1008 | } |
Simon Glass | 0023d18 | 2023-10-01 19:13:34 -0600 | [diff] [blame] | 1009 | case SCENEOBJT_TEXTLINE: { |
| 1010 | struct scene_obj_textline *tline; |
| 1011 | |
| 1012 | tline = (struct scene_obj_textline *)obj; |
| 1013 | ret = scene_textline_calc_dims(tline); |
| 1014 | if (ret) |
| 1015 | return log_msg_ret("men", ret); |
| 1016 | |
| 1017 | break; |
| 1018 | } |
Simon Glass | 7a96005 | 2023-06-01 10:22:52 -0600 | [diff] [blame] | 1019 | } |
| 1020 | } |
| 1021 | |
| 1022 | return 0; |
| 1023 | } |
Simon Glass | c999e17 | 2023-06-01 10:22:53 -0600 | [diff] [blame] | 1024 | |
| 1025 | int scene_apply_theme(struct scene *scn, struct expo_theme *theme) |
| 1026 | { |
| 1027 | struct scene_obj *obj; |
| 1028 | int ret; |
| 1029 | |
| 1030 | /* Avoid error-checking optional items */ |
| 1031 | scene_txt_set_font(scn, scn->title_id, NULL, theme->font_size); |
| 1032 | |
| 1033 | list_for_each_entry(obj, &scn->obj_head, sibling) { |
| 1034 | switch (obj->type) { |
| 1035 | case SCENEOBJT_NONE: |
| 1036 | case SCENEOBJT_IMAGE: |
| 1037 | case SCENEOBJT_MENU: |
Simon Glass | 138a397 | 2025-05-02 08:46:44 -0600 | [diff] [blame] | 1038 | case SCENEOBJT_BOX: |
Simon Glass | 0023d18 | 2023-10-01 19:13:34 -0600 | [diff] [blame] | 1039 | case SCENEOBJT_TEXTLINE: |
Simon Glass | c999e17 | 2023-06-01 10:22:53 -0600 | [diff] [blame] | 1040 | break; |
| 1041 | case SCENEOBJT_TEXT: |
| 1042 | scene_txt_set_font(scn, obj->id, NULL, |
| 1043 | theme->font_size); |
| 1044 | break; |
| 1045 | } |
| 1046 | } |
| 1047 | |
| 1048 | ret = scene_arrange(scn); |
| 1049 | if (ret) |
| 1050 | return log_msg_ret("arr", ret); |
| 1051 | |
| 1052 | return 0; |
| 1053 | } |
Simon Glass | 01922ec | 2023-06-01 10:22:57 -0600 | [diff] [blame] | 1054 | |
| 1055 | void scene_set_highlight_id(struct scene *scn, uint id) |
| 1056 | { |
| 1057 | scn->highlight_id = id; |
| 1058 | } |
| 1059 | |
| 1060 | void scene_highlight_first(struct scene *scn) |
| 1061 | { |
| 1062 | struct scene_obj *obj; |
| 1063 | |
| 1064 | list_for_each_entry(obj, &scn->obj_head, sibling) { |
Simon Glass | 193bfea | 2023-10-01 19:13:27 -0600 | [diff] [blame] | 1065 | if (scene_obj_can_highlight(obj)) { |
Simon Glass | 01922ec | 2023-06-01 10:22:57 -0600 | [diff] [blame] | 1066 | scene_set_highlight_id(scn, obj->id); |
| 1067 | return; |
Simon Glass | 01922ec | 2023-06-01 10:22:57 -0600 | [diff] [blame] | 1068 | } |
| 1069 | } |
| 1070 | } |
| 1071 | |
Simon Glass | f6a943a | 2023-10-01 19:13:33 -0600 | [diff] [blame] | 1072 | static int scene_obj_open(struct scene *scn, struct scene_obj *obj) |
| 1073 | { |
| 1074 | int ret; |
| 1075 | |
| 1076 | switch (obj->type) { |
| 1077 | case SCENEOBJT_NONE: |
| 1078 | case SCENEOBJT_IMAGE: |
| 1079 | case SCENEOBJT_MENU: |
| 1080 | case SCENEOBJT_TEXT: |
Simon Glass | 138a397 | 2025-05-02 08:46:44 -0600 | [diff] [blame] | 1081 | case SCENEOBJT_BOX: |
Simon Glass | f6a943a | 2023-10-01 19:13:33 -0600 | [diff] [blame] | 1082 | break; |
| 1083 | case SCENEOBJT_TEXTLINE: |
| 1084 | ret = scene_textline_open(scn, |
| 1085 | (struct scene_obj_textline *)obj); |
| 1086 | if (ret) |
| 1087 | return log_msg_ret("op", ret); |
| 1088 | break; |
| 1089 | } |
| 1090 | |
| 1091 | return 0; |
| 1092 | } |
| 1093 | |
Simon Glass | 01922ec | 2023-06-01 10:22:57 -0600 | [diff] [blame] | 1094 | int scene_set_open(struct scene *scn, uint id, bool open) |
| 1095 | { |
Simon Glass | f6a943a | 2023-10-01 19:13:33 -0600 | [diff] [blame] | 1096 | struct scene_obj *obj; |
Simon Glass | 01922ec | 2023-06-01 10:22:57 -0600 | [diff] [blame] | 1097 | int ret; |
| 1098 | |
Simon Glass | f6a943a | 2023-10-01 19:13:33 -0600 | [diff] [blame] | 1099 | obj = scene_obj_find(scn, id, SCENEOBJT_NONE); |
| 1100 | if (!obj) |
| 1101 | return log_msg_ret("find", -ENOENT); |
| 1102 | |
| 1103 | if (open) { |
| 1104 | ret = scene_obj_open(scn, obj); |
| 1105 | if (ret) |
| 1106 | return log_msg_ret("op", ret); |
| 1107 | } |
| 1108 | |
Simon Glass | 01922ec | 2023-06-01 10:22:57 -0600 | [diff] [blame] | 1109 | ret = scene_obj_flag_clrset(scn, id, SCENEOF_OPEN, |
| 1110 | open ? SCENEOF_OPEN : 0); |
| 1111 | if (ret) |
| 1112 | return log_msg_ret("flg", ret); |
| 1113 | |
| 1114 | return 0; |
| 1115 | } |
Simon Glass | e90acd8 | 2023-08-14 16:40:23 -0600 | [diff] [blame] | 1116 | |
| 1117 | int scene_iter_objs(struct scene *scn, expo_scene_obj_iterator iter, |
| 1118 | void *priv) |
| 1119 | { |
| 1120 | struct scene_obj *obj; |
| 1121 | |
| 1122 | list_for_each_entry(obj, &scn->obj_head, sibling) { |
| 1123 | int ret; |
| 1124 | |
| 1125 | ret = iter(obj, priv); |
| 1126 | if (ret) |
| 1127 | return log_msg_ret("itr", ret); |
| 1128 | } |
| 1129 | |
| 1130 | return 0; |
| 1131 | } |
Simon Glass | f099469 | 2023-10-01 19:13:29 -0600 | [diff] [blame] | 1132 | |
Simon Glass | 96910ef | 2025-05-02 08:46:34 -0600 | [diff] [blame] | 1133 | int scene_bbox_join(const struct vidconsole_bbox *src, int inset, |
| 1134 | struct vidconsole_bbox *dst) |
| 1135 | { |
| 1136 | if (dst->valid) { |
| 1137 | dst->x0 = min(dst->x0, src->x0 - inset); |
| 1138 | dst->y0 = min(dst->y0, src->y0); |
| 1139 | dst->x1 = max(dst->x1, src->x1 + inset); |
| 1140 | dst->y1 = max(dst->y1, src->y1); |
| 1141 | } else { |
| 1142 | dst->x0 = src->x0 - inset; |
| 1143 | dst->y0 = src->y0; |
| 1144 | dst->x1 = src->x1 + inset; |
| 1145 | dst->y1 = src->y1; |
| 1146 | dst->valid = true; |
| 1147 | } |
| 1148 | |
| 1149 | return 0; |
| 1150 | } |
| 1151 | |
Simon Glass | f099469 | 2023-10-01 19:13:29 -0600 | [diff] [blame] | 1152 | int scene_bbox_union(struct scene *scn, uint id, int inset, |
| 1153 | struct vidconsole_bbox *bbox) |
| 1154 | { |
| 1155 | struct scene_obj *obj; |
Simon Glass | 96910ef | 2025-05-02 08:46:34 -0600 | [diff] [blame] | 1156 | struct vidconsole_bbox local; |
Simon Glass | f099469 | 2023-10-01 19:13:29 -0600 | [diff] [blame] | 1157 | |
| 1158 | if (!id) |
| 1159 | return 0; |
| 1160 | obj = scene_obj_find(scn, id, SCENEOBJT_NONE); |
| 1161 | if (!obj) |
| 1162 | return log_msg_ret("obj", -ENOENT); |
Simon Glass | 96910ef | 2025-05-02 08:46:34 -0600 | [diff] [blame] | 1163 | local.x0 = obj->bbox.x0; |
| 1164 | local.y0 = obj->bbox.y0; |
| 1165 | local.x1 = obj->bbox.x1; |
| 1166 | local.y1 = obj->bbox.y1; |
| 1167 | local.valid = true; |
| 1168 | scene_bbox_join(&local, inset, bbox); |
Simon Glass | f099469 | 2023-10-01 19:13:29 -0600 | [diff] [blame] | 1169 | |
| 1170 | return 0; |
| 1171 | } |