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 | 0a4d14b | 2023-01-06 08:52:37 -0600 | [diff] [blame] | 309 | int scene_obj_set_hide(struct scene *scn, uint id, bool hide) |
| 310 | { |
Simon Glass | 6081b0f | 2023-06-01 10:22:50 -0600 | [diff] [blame] | 311 | int ret; |
| 312 | |
| 313 | ret = scene_obj_flag_clrset(scn, id, SCENEOF_HIDE, |
| 314 | hide ? SCENEOF_HIDE : 0); |
| 315 | if (ret) |
| 316 | return log_msg_ret("flg", ret); |
| 317 | |
| 318 | return 0; |
| 319 | } |
| 320 | |
| 321 | int scene_obj_flag_clrset(struct scene *scn, uint id, uint clr, uint set) |
| 322 | { |
Simon Glass | 0a4d14b | 2023-01-06 08:52:37 -0600 | [diff] [blame] | 323 | struct scene_obj *obj; |
| 324 | |
| 325 | obj = scene_obj_find(scn, id, SCENEOBJT_NONE); |
| 326 | if (!obj) |
| 327 | return log_msg_ret("find", -ENOENT); |
Simon Glass | 6081b0f | 2023-06-01 10:22:50 -0600 | [diff] [blame] | 328 | obj->flags &= ~clr; |
| 329 | obj->flags |= set; |
Simon Glass | 0a4d14b | 2023-01-06 08:52:37 -0600 | [diff] [blame] | 330 | |
| 331 | return 0; |
| 332 | } |
| 333 | |
| 334 | int scene_obj_get_hw(struct scene *scn, uint id, int *widthp) |
| 335 | { |
| 336 | struct scene_obj *obj; |
| 337 | |
| 338 | obj = scene_obj_find(scn, id, SCENEOBJT_NONE); |
| 339 | if (!obj) |
| 340 | return log_msg_ret("find", -ENOENT); |
| 341 | |
| 342 | switch (obj->type) { |
| 343 | case SCENEOBJT_NONE: |
| 344 | case SCENEOBJT_MENU: |
Simon Glass | 0023d18 | 2023-10-01 19:13:34 -0600 | [diff] [blame] | 345 | case SCENEOBJT_TEXTLINE: |
Simon Glass | 138a397 | 2025-05-02 08:46:44 -0600 | [diff] [blame^] | 346 | case SCENEOBJT_BOX: |
Simon Glass | 0a4d14b | 2023-01-06 08:52:37 -0600 | [diff] [blame] | 347 | break; |
| 348 | case SCENEOBJT_IMAGE: { |
| 349 | struct scene_obj_img *img = (struct scene_obj_img *)obj; |
| 350 | ulong width, height; |
| 351 | uint bpix; |
| 352 | |
| 353 | video_bmp_get_info(img->data, &width, &height, &bpix); |
| 354 | if (widthp) |
| 355 | *widthp = width; |
| 356 | return height; |
| 357 | } |
| 358 | case SCENEOBJT_TEXT: { |
Simon Glass | 9ef02aa | 2025-05-02 08:46:37 -0600 | [diff] [blame] | 359 | struct scene_txt_generic *gen = &((struct scene_obj_txt *)obj)->gen; |
Simon Glass | 0a4d14b | 2023-01-06 08:52:37 -0600 | [diff] [blame] | 360 | struct expo *exp = scn->expo; |
Simon Glass | 9e1a86d | 2023-06-01 10:22:51 -0600 | [diff] [blame] | 361 | struct vidconsole_bbox bbox; |
Simon Glass | cfb4f2c | 2025-05-02 08:46:42 -0600 | [diff] [blame] | 362 | int len, ret, limit; |
Simon Glass | 9e1a86d | 2023-06-01 10:22:51 -0600 | [diff] [blame] | 363 | const char *str; |
Simon Glass | 0a4d14b | 2023-01-06 08:52:37 -0600 | [diff] [blame] | 364 | |
Simon Glass | 9ef02aa | 2025-05-02 08:46:37 -0600 | [diff] [blame] | 365 | str = expo_get_str(exp, gen->str_id); |
Simon Glass | 9e1a86d | 2023-06-01 10:22:51 -0600 | [diff] [blame] | 366 | if (!str) |
| 367 | return log_msg_ret("str", -ENOENT); |
| 368 | len = strlen(str); |
| 369 | |
| 370 | /* if there is no console, make it up */ |
| 371 | if (!exp->cons) { |
| 372 | if (widthp) |
| 373 | *widthp = 8 * len; |
| 374 | return 16; |
| 375 | } |
| 376 | |
Simon Glass | cfb4f2c | 2025-05-02 08:46:42 -0600 | [diff] [blame] | 377 | limit = obj->flags & SCENEOF_SIZE_VALID ? |
| 378 | obj->bbox.x1 - obj->bbox.x0 : -1; |
| 379 | |
Simon Glass | 9ef02aa | 2025-05-02 08:46:37 -0600 | [diff] [blame] | 380 | ret = vidconsole_measure(scn->expo->cons, gen->font_name, |
Simon Glass | cfb4f2c | 2025-05-02 08:46:42 -0600 | [diff] [blame] | 381 | gen->font_size, str, limit, &bbox, |
| 382 | &gen->lines); |
Simon Glass | 9e1a86d | 2023-06-01 10:22:51 -0600 | [diff] [blame] | 383 | if (ret) |
| 384 | return log_msg_ret("mea", ret); |
Simon Glass | 0a4d14b | 2023-01-06 08:52:37 -0600 | [diff] [blame] | 385 | if (widthp) |
Simon Glass | 9e1a86d | 2023-06-01 10:22:51 -0600 | [diff] [blame] | 386 | *widthp = bbox.x1; |
Simon Glass | 0a4d14b | 2023-01-06 08:52:37 -0600 | [diff] [blame] | 387 | |
Simon Glass | 9e1a86d | 2023-06-01 10:22:51 -0600 | [diff] [blame] | 388 | return bbox.y1; |
Simon Glass | 0a4d14b | 2023-01-06 08:52:37 -0600 | [diff] [blame] | 389 | } |
| 390 | } |
| 391 | |
| 392 | return 0; |
| 393 | } |
| 394 | |
| 395 | /** |
Simon Glass | 118a727 | 2023-10-01 19:13:30 -0600 | [diff] [blame] | 396 | * scene_render_background() - Render the background for an object |
| 397 | * |
| 398 | * @obj: Object to render |
Simon Glass | 0023d18 | 2023-10-01 19:13:34 -0600 | [diff] [blame] | 399 | * @box_only: true to show a box around the object, but keep the normal |
| 400 | * background colour inside |
Simon Glass | 118a727 | 2023-10-01 19:13:30 -0600 | [diff] [blame] | 401 | */ |
Simon Glass | 0023d18 | 2023-10-01 19:13:34 -0600 | [diff] [blame] | 402 | static void scene_render_background(struct scene_obj *obj, bool box_only) |
Simon Glass | 118a727 | 2023-10-01 19:13:30 -0600 | [diff] [blame] | 403 | { |
Simon Glass | 96910ef | 2025-05-02 08:46:34 -0600 | [diff] [blame] | 404 | struct vidconsole_bbox bbox[SCENEBB_count], *sel; |
Simon Glass | 118a727 | 2023-10-01 19:13:30 -0600 | [diff] [blame] | 405 | struct expo *exp = obj->scene->expo; |
| 406 | const struct expo_theme *theme = &exp->theme; |
Simon Glass | 118a727 | 2023-10-01 19:13:30 -0600 | [diff] [blame] | 407 | struct udevice *dev = exp->display; |
| 408 | struct video_priv *vid_priv; |
| 409 | struct udevice *cons = exp->cons; |
| 410 | struct vidconsole_colour old; |
| 411 | enum colour_idx fore, back; |
| 412 | uint inset = theme->menu_inset; |
| 413 | |
Simon Glass | 21320da | 2025-04-02 06:29:33 +1300 | [diff] [blame] | 414 | vid_priv = dev_get_uclass_priv(dev); |
Simon Glass | 118a727 | 2023-10-01 19:13:30 -0600 | [diff] [blame] | 415 | /* draw a background for the object */ |
Simon Glass | 21320da | 2025-04-02 06:29:33 +1300 | [diff] [blame] | 416 | if (vid_priv->white_on_black) { |
Simon Glass | bda3adc | 2024-10-14 16:31:53 -0600 | [diff] [blame] | 417 | fore = VID_DARK_GREY; |
Simon Glass | 118a727 | 2023-10-01 19:13:30 -0600 | [diff] [blame] | 418 | back = VID_WHITE; |
| 419 | } else { |
| 420 | fore = VID_LIGHT_GRAY; |
| 421 | back = VID_BLACK; |
| 422 | } |
| 423 | |
| 424 | /* see if this object wants to render a background */ |
Simon Glass | 96910ef | 2025-05-02 08:46:34 -0600 | [diff] [blame] | 425 | if (scene_obj_calc_bbox(obj, bbox)) |
Simon Glass | 118a727 | 2023-10-01 19:13:30 -0600 | [diff] [blame] | 426 | return; |
| 427 | |
Simon Glass | 96910ef | 2025-05-02 08:46:34 -0600 | [diff] [blame] | 428 | sel = &bbox[SCENEBB_label]; |
| 429 | if (!sel->valid) |
| 430 | return; |
| 431 | |
Simon Glass | 118a727 | 2023-10-01 19:13:30 -0600 | [diff] [blame] | 432 | vidconsole_push_colour(cons, fore, back, &old); |
Simon Glass | 96910ef | 2025-05-02 08:46:34 -0600 | [diff] [blame] | 433 | video_fill_part(dev, sel->x0 - inset, sel->y0 - inset, |
| 434 | sel->x1 + inset, sel->y1 + inset, |
Simon Glass | 118a727 | 2023-10-01 19:13:30 -0600 | [diff] [blame] | 435 | vid_priv->colour_fg); |
| 436 | vidconsole_pop_colour(cons, &old); |
Simon Glass | 0023d18 | 2023-10-01 19:13:34 -0600 | [diff] [blame] | 437 | if (box_only) { |
Simon Glass | 96910ef | 2025-05-02 08:46:34 -0600 | [diff] [blame] | 438 | video_fill_part(dev, sel->x0, sel->y0, sel->x1, sel->y1, |
Simon Glass | 0023d18 | 2023-10-01 19:13:34 -0600 | [diff] [blame] | 439 | vid_priv->colour_bg); |
| 440 | } |
Simon Glass | 118a727 | 2023-10-01 19:13:30 -0600 | [diff] [blame] | 441 | } |
| 442 | |
Simon Glass | a841d1a | 2025-05-02 08:46:38 -0600 | [diff] [blame] | 443 | static int scene_txt_render(struct expo *exp, struct udevice *dev, |
| 444 | struct udevice *cons, struct scene_obj *obj, |
| 445 | struct scene_txt_generic *gen, int x, int y, |
| 446 | int menu_inset) |
| 447 | { |
Simon Glass | cfb4f2c | 2025-05-02 08:46:42 -0600 | [diff] [blame] | 448 | const struct vidconsole_mline *mline; |
Simon Glass | 62f39d2 | 2025-05-02 08:46:39 -0600 | [diff] [blame] | 449 | struct video_priv *vid_priv; |
| 450 | struct vidconsole_colour old; |
| 451 | enum colour_idx fore, back; |
Simon Glass | a841d1a | 2025-05-02 08:46:38 -0600 | [diff] [blame] | 452 | const char *str; |
| 453 | int ret; |
| 454 | |
| 455 | if (!cons) |
| 456 | return -ENOTSUPP; |
| 457 | |
| 458 | if (gen->font_name || gen->font_size) { |
| 459 | ret = vidconsole_select_font(cons, gen->font_name, |
| 460 | gen->font_size); |
| 461 | } else { |
| 462 | ret = vidconsole_select_font(cons, NULL, 0); |
| 463 | } |
| 464 | if (ret && ret != -ENOSYS) |
| 465 | return log_msg_ret("font", ret); |
| 466 | str = expo_get_str(exp, gen->str_id); |
Simon Glass | 62f39d2 | 2025-05-02 08:46:39 -0600 | [diff] [blame] | 467 | if (!str) |
| 468 | return 0; |
Simon Glass | a841d1a | 2025-05-02 08:46:38 -0600 | [diff] [blame] | 469 | |
Simon Glass | 62f39d2 | 2025-05-02 08:46:39 -0600 | [diff] [blame] | 470 | vid_priv = dev_get_uclass_priv(dev); |
| 471 | if (vid_priv->white_on_black) { |
| 472 | fore = VID_BLACK; |
| 473 | back = VID_WHITE; |
| 474 | } else { |
| 475 | fore = VID_LIGHT_GRAY; |
| 476 | back = VID_BLACK; |
| 477 | } |
Simon Glass | a841d1a | 2025-05-02 08:46:38 -0600 | [diff] [blame] | 478 | |
Simon Glass | 62f39d2 | 2025-05-02 08:46:39 -0600 | [diff] [blame] | 479 | if (obj->flags & SCENEOF_POINT) { |
| 480 | vidconsole_push_colour(cons, fore, back, &old); |
| 481 | video_fill_part(dev, x - menu_inset, y, obj->bbox.x1, |
| 482 | obj->bbox.y1, vid_priv->colour_bg); |
Simon Glass | a841d1a | 2025-05-02 08:46:38 -0600 | [diff] [blame] | 483 | } |
Simon Glass | cfb4f2c | 2025-05-02 08:46:42 -0600 | [diff] [blame] | 484 | |
| 485 | if (!gen->lines.count) { |
| 486 | vidconsole_set_cursor_pos(cons, x, y); |
| 487 | vidconsole_put_string(cons, str); |
| 488 | } |
| 489 | alist_for_each(mline, &gen->lines) { |
| 490 | vidconsole_set_cursor_pos(cons, x + mline->bbox.x0, |
| 491 | y + mline->bbox.y0); |
| 492 | vidconsole_put_stringn(cons, str + mline->start, mline->len); |
| 493 | } |
Simon Glass | 62f39d2 | 2025-05-02 08:46:39 -0600 | [diff] [blame] | 494 | if (obj->flags & SCENEOF_POINT) |
| 495 | vidconsole_pop_colour(cons, &old); |
Simon Glass | a841d1a | 2025-05-02 08:46:38 -0600 | [diff] [blame] | 496 | |
| 497 | return 0; |
| 498 | } |
| 499 | |
Simon Glass | 118a727 | 2023-10-01 19:13:30 -0600 | [diff] [blame] | 500 | /** |
Simon Glass | 0a4d14b | 2023-01-06 08:52:37 -0600 | [diff] [blame] | 501 | * scene_obj_render() - Render an object |
| 502 | * |
Simon Glass | cfb4f2c | 2025-05-02 08:46:42 -0600 | [diff] [blame] | 503 | * @obj: Object to render |
| 504 | * @text_mode: true to use text mode |
| 505 | * Return: 0 if OK, -ve on error |
Simon Glass | 0a4d14b | 2023-01-06 08:52:37 -0600 | [diff] [blame] | 506 | */ |
| 507 | static int scene_obj_render(struct scene_obj *obj, bool text_mode) |
| 508 | { |
| 509 | struct scene *scn = obj->scene; |
| 510 | struct expo *exp = scn->expo; |
Simon Glass | 86f1ac5 | 2023-06-01 10:23:00 -0600 | [diff] [blame] | 511 | const struct expo_theme *theme = &exp->theme; |
Simon Glass | 67e2af1 | 2023-06-01 10:22:34 -0600 | [diff] [blame] | 512 | struct udevice *dev = exp->display; |
| 513 | struct udevice *cons = text_mode ? NULL : exp->cons; |
Simon Glass | 138a397 | 2025-05-02 08:46:44 -0600 | [diff] [blame^] | 514 | struct video_priv *vid_priv; |
Simon Glass | 0a4d14b | 2023-01-06 08:52:37 -0600 | [diff] [blame] | 515 | int x, y, ret; |
| 516 | |
Simon Glass | bc3a15f | 2025-05-02 08:46:31 -0600 | [diff] [blame] | 517 | y = obj->bbox.y0; |
Simon Glass | 138a397 | 2025-05-02 08:46:44 -0600 | [diff] [blame^] | 518 | x = obj->bbox.x0; |
| 519 | vid_priv = dev_get_uclass_priv(dev); |
Simon Glass | 0a4d14b | 2023-01-06 08:52:37 -0600 | [diff] [blame] | 520 | |
| 521 | switch (obj->type) { |
| 522 | case SCENEOBJT_NONE: |
| 523 | break; |
| 524 | case SCENEOBJT_IMAGE: { |
| 525 | struct scene_obj_img *img = (struct scene_obj_img *)obj; |
| 526 | |
| 527 | if (!cons) |
| 528 | return -ENOTSUPP; |
| 529 | ret = video_bmp_display(dev, map_to_sysmem(img->data), x, y, |
| 530 | true); |
| 531 | if (ret < 0) |
| 532 | return log_msg_ret("img", ret); |
| 533 | break; |
| 534 | } |
| 535 | case SCENEOBJT_TEXT: { |
Simon Glass | a841d1a | 2025-05-02 08:46:38 -0600 | [diff] [blame] | 536 | struct scene_obj_txt *txt = (struct scene_obj_txt *)obj; |
Simon Glass | 0a4d14b | 2023-01-06 08:52:37 -0600 | [diff] [blame] | 537 | |
Simon Glass | a841d1a | 2025-05-02 08:46:38 -0600 | [diff] [blame] | 538 | ret = scene_txt_render(exp, dev, cons, obj, &txt->gen, x, y, |
| 539 | theme->menu_inset); |
Simon Glass | 0a4d14b | 2023-01-06 08:52:37 -0600 | [diff] [blame] | 540 | break; |
| 541 | } |
| 542 | case SCENEOBJT_MENU: { |
| 543 | struct scene_obj_menu *menu = (struct scene_obj_menu *)obj; |
Simon Glass | 01922ec | 2023-06-01 10:22:57 -0600 | [diff] [blame] | 544 | |
| 545 | if (exp->popup && (obj->flags & SCENEOF_OPEN)) { |
| 546 | if (!cons) |
| 547 | return -ENOTSUPP; |
| 548 | |
| 549 | /* draw a background behind the menu items */ |
Simon Glass | 0023d18 | 2023-10-01 19:13:34 -0600 | [diff] [blame] | 550 | scene_render_background(obj, false); |
Simon Glass | 01922ec | 2023-06-01 10:22:57 -0600 | [diff] [blame] | 551 | } |
Simon Glass | 0a4d14b | 2023-01-06 08:52:37 -0600 | [diff] [blame] | 552 | /* |
| 553 | * With a vidconsole, the text and item pointer are rendered as |
| 554 | * normal objects so we don't need to do anything here. The menu |
| 555 | * simply controls where they are positioned. |
| 556 | */ |
| 557 | if (cons) |
| 558 | return -ENOTSUPP; |
| 559 | |
| 560 | ret = scene_menu_display(menu); |
| 561 | if (ret < 0) |
| 562 | return log_msg_ret("img", ret); |
| 563 | |
| 564 | break; |
| 565 | } |
Simon Glass | 0023d18 | 2023-10-01 19:13:34 -0600 | [diff] [blame] | 566 | case SCENEOBJT_TEXTLINE: |
| 567 | if (obj->flags & SCENEOF_OPEN) |
| 568 | scene_render_background(obj, true); |
| 569 | break; |
Simon Glass | 138a397 | 2025-05-02 08:46:44 -0600 | [diff] [blame^] | 570 | case SCENEOBJT_BOX: { |
| 571 | struct scene_obj_box *box = (struct scene_obj_box *)obj; |
| 572 | |
| 573 | video_draw_box(dev, obj->bbox.x0, obj->bbox.y0, obj->bbox.x1, |
| 574 | obj->bbox.y1, box->width, vid_priv->colour_fg); |
| 575 | break; |
Simon Glass | 0a4d14b | 2023-01-06 08:52:37 -0600 | [diff] [blame] | 576 | } |
Simon Glass | 138a397 | 2025-05-02 08:46:44 -0600 | [diff] [blame^] | 577 | } |
Simon Glass | 0a4d14b | 2023-01-06 08:52:37 -0600 | [diff] [blame] | 578 | |
| 579 | return 0; |
| 580 | } |
| 581 | |
Simon Glass | 377f18e | 2024-10-14 16:31:55 -0600 | [diff] [blame] | 582 | int scene_calc_arrange(struct scene *scn, struct expo_arrange_info *arr) |
| 583 | { |
| 584 | struct scene_obj *obj; |
| 585 | |
| 586 | arr->label_width = 0; |
| 587 | list_for_each_entry(obj, &scn->obj_head, sibling) { |
| 588 | uint label_id = 0; |
| 589 | int width; |
| 590 | |
| 591 | switch (obj->type) { |
| 592 | case SCENEOBJT_NONE: |
| 593 | case SCENEOBJT_IMAGE: |
| 594 | case SCENEOBJT_TEXT: |
Simon Glass | 138a397 | 2025-05-02 08:46:44 -0600 | [diff] [blame^] | 595 | case SCENEOBJT_BOX: |
Simon Glass | 377f18e | 2024-10-14 16:31:55 -0600 | [diff] [blame] | 596 | break; |
| 597 | case SCENEOBJT_MENU: { |
| 598 | struct scene_obj_menu *menu; |
| 599 | |
| 600 | menu = (struct scene_obj_menu *)obj, |
| 601 | label_id = menu->title_id; |
| 602 | break; |
| 603 | } |
| 604 | case SCENEOBJT_TEXTLINE: { |
| 605 | struct scene_obj_textline *tline; |
| 606 | |
| 607 | tline = (struct scene_obj_textline *)obj, |
| 608 | label_id = tline->label_id; |
| 609 | break; |
| 610 | } |
| 611 | } |
| 612 | |
| 613 | if (label_id) { |
| 614 | int ret; |
| 615 | |
| 616 | ret = scene_obj_get_hw(scn, label_id, &width); |
| 617 | if (ret < 0) |
| 618 | return log_msg_ret("hei", ret); |
| 619 | arr->label_width = max(arr->label_width, width); |
| 620 | } |
| 621 | } |
| 622 | |
| 623 | return 0; |
| 624 | } |
| 625 | |
Simon Glass | 0a4d14b | 2023-01-06 08:52:37 -0600 | [diff] [blame] | 626 | int scene_arrange(struct scene *scn) |
| 627 | { |
Simon Glass | 377f18e | 2024-10-14 16:31:55 -0600 | [diff] [blame] | 628 | struct expo_arrange_info arr; |
Simon Glass | 0a4d14b | 2023-01-06 08:52:37 -0600 | [diff] [blame] | 629 | struct scene_obj *obj; |
| 630 | int ret; |
| 631 | |
Simon Glass | 377f18e | 2024-10-14 16:31:55 -0600 | [diff] [blame] | 632 | ret = scene_calc_arrange(scn, &arr); |
| 633 | if (ret < 0) |
| 634 | return log_msg_ret("arr", ret); |
| 635 | |
Simon Glass | 0a4d14b | 2023-01-06 08:52:37 -0600 | [diff] [blame] | 636 | list_for_each_entry(obj, &scn->obj_head, sibling) { |
Simon Glass | b7a6453 | 2023-10-01 19:13:24 -0600 | [diff] [blame] | 637 | switch (obj->type) { |
| 638 | case SCENEOBJT_NONE: |
| 639 | case SCENEOBJT_IMAGE: |
| 640 | case SCENEOBJT_TEXT: |
Simon Glass | 138a397 | 2025-05-02 08:46:44 -0600 | [diff] [blame^] | 641 | case SCENEOBJT_BOX: |
Simon Glass | b7a6453 | 2023-10-01 19:13:24 -0600 | [diff] [blame] | 642 | break; |
| 643 | case SCENEOBJT_MENU: { |
Simon Glass | 0a4d14b | 2023-01-06 08:52:37 -0600 | [diff] [blame] | 644 | struct scene_obj_menu *menu; |
| 645 | |
| 646 | menu = (struct scene_obj_menu *)obj, |
Simon Glass | 377f18e | 2024-10-14 16:31:55 -0600 | [diff] [blame] | 647 | ret = scene_menu_arrange(scn, &arr, menu); |
Simon Glass | 0a4d14b | 2023-01-06 08:52:37 -0600 | [diff] [blame] | 648 | if (ret) |
| 649 | return log_msg_ret("arr", ret); |
Simon Glass | b7a6453 | 2023-10-01 19:13:24 -0600 | [diff] [blame] | 650 | break; |
| 651 | } |
Simon Glass | 0023d18 | 2023-10-01 19:13:34 -0600 | [diff] [blame] | 652 | case SCENEOBJT_TEXTLINE: { |
| 653 | struct scene_obj_textline *tline; |
| 654 | |
| 655 | tline = (struct scene_obj_textline *)obj, |
Simon Glass | 377f18e | 2024-10-14 16:31:55 -0600 | [diff] [blame] | 656 | ret = scene_textline_arrange(scn, &arr, tline); |
Simon Glass | 0023d18 | 2023-10-01 19:13:34 -0600 | [diff] [blame] | 657 | if (ret) |
| 658 | return log_msg_ret("arr", ret); |
| 659 | break; |
| 660 | } |
Simon Glass | 0a4d14b | 2023-01-06 08:52:37 -0600 | [diff] [blame] | 661 | } |
| 662 | } |
| 663 | |
| 664 | return 0; |
| 665 | } |
| 666 | |
Simon Glass | 12f5773 | 2023-06-01 10:22:58 -0600 | [diff] [blame] | 667 | int scene_render_deps(struct scene *scn, uint id) |
| 668 | { |
| 669 | struct scene_obj *obj; |
| 670 | int ret; |
| 671 | |
| 672 | if (!id) |
| 673 | return 0; |
| 674 | obj = scene_obj_find(scn, id, SCENEOBJT_NONE); |
| 675 | if (!obj) |
| 676 | return log_msg_ret("obj", -ENOENT); |
| 677 | |
| 678 | if (!(obj->flags & SCENEOF_HIDE)) { |
| 679 | ret = scene_obj_render(obj, false); |
| 680 | if (ret && ret != -ENOTSUPP) |
| 681 | return log_msg_ret("ren", ret); |
| 682 | |
Simon Glass | b7a6453 | 2023-10-01 19:13:24 -0600 | [diff] [blame] | 683 | switch (obj->type) { |
| 684 | case SCENEOBJT_NONE: |
| 685 | case SCENEOBJT_IMAGE: |
| 686 | case SCENEOBJT_TEXT: |
Simon Glass | 138a397 | 2025-05-02 08:46:44 -0600 | [diff] [blame^] | 687 | case SCENEOBJT_BOX: |
Simon Glass | b7a6453 | 2023-10-01 19:13:24 -0600 | [diff] [blame] | 688 | break; |
| 689 | case SCENEOBJT_MENU: |
Simon Glass | 12f5773 | 2023-06-01 10:22:58 -0600 | [diff] [blame] | 690 | scene_menu_render_deps(scn, |
| 691 | (struct scene_obj_menu *)obj); |
Simon Glass | b7a6453 | 2023-10-01 19:13:24 -0600 | [diff] [blame] | 692 | break; |
Simon Glass | 0023d18 | 2023-10-01 19:13:34 -0600 | [diff] [blame] | 693 | case SCENEOBJT_TEXTLINE: |
| 694 | scene_textline_render_deps(scn, |
| 695 | (struct scene_obj_textline *)obj); |
| 696 | break; |
Simon Glass | b7a6453 | 2023-10-01 19:13:24 -0600 | [diff] [blame] | 697 | } |
Simon Glass | 12f5773 | 2023-06-01 10:22:58 -0600 | [diff] [blame] | 698 | } |
| 699 | |
| 700 | return 0; |
| 701 | } |
| 702 | |
Simon Glass | 0a4d14b | 2023-01-06 08:52:37 -0600 | [diff] [blame] | 703 | int scene_render(struct scene *scn) |
| 704 | { |
| 705 | struct expo *exp = scn->expo; |
| 706 | struct scene_obj *obj; |
| 707 | int ret; |
| 708 | |
| 709 | list_for_each_entry(obj, &scn->obj_head, sibling) { |
Simon Glass | 6081b0f | 2023-06-01 10:22:50 -0600 | [diff] [blame] | 710 | if (!(obj->flags & SCENEOF_HIDE)) { |
Simon Glass | 0a4d14b | 2023-01-06 08:52:37 -0600 | [diff] [blame] | 711 | ret = scene_obj_render(obj, exp->text_mode); |
| 712 | if (ret && ret != -ENOTSUPP) |
| 713 | return log_msg_ret("ren", ret); |
| 714 | } |
| 715 | } |
| 716 | |
Simon Glass | 12f5773 | 2023-06-01 10:22:58 -0600 | [diff] [blame] | 717 | /* render any highlighted object on top of the others */ |
| 718 | if (scn->highlight_id && !exp->text_mode) { |
| 719 | ret = scene_render_deps(scn, scn->highlight_id); |
| 720 | if (ret && ret != -ENOTSUPP) |
| 721 | return log_msg_ret("dep", ret); |
| 722 | } |
| 723 | |
Simon Glass | 0a4d14b | 2023-01-06 08:52:37 -0600 | [diff] [blame] | 724 | return 0; |
| 725 | } |
| 726 | |
Simon Glass | f0e1e8c | 2023-06-01 10:22:59 -0600 | [diff] [blame] | 727 | /** |
| 728 | * send_key_obj() - Handle a keypress for moving between objects |
| 729 | * |
| 730 | * @scn: Scene to receive the key |
| 731 | * @key: Key to send (KEYCODE_UP) |
| 732 | * @event: Returns resulting event from this keypress |
| 733 | * Returns: 0 if OK, -ve on error |
| 734 | */ |
| 735 | static void send_key_obj(struct scene *scn, struct scene_obj *obj, int key, |
| 736 | struct expo_action *event) |
| 737 | { |
| 738 | switch (key) { |
| 739 | case BKEY_UP: |
| 740 | while (obj != list_first_entry(&scn->obj_head, struct scene_obj, |
| 741 | sibling)) { |
| 742 | obj = list_entry(obj->sibling.prev, |
| 743 | struct scene_obj, sibling); |
Simon Glass | 193bfea | 2023-10-01 19:13:27 -0600 | [diff] [blame] | 744 | if (scene_obj_can_highlight(obj)) { |
Simon Glass | f0e1e8c | 2023-06-01 10:22:59 -0600 | [diff] [blame] | 745 | event->type = EXPOACT_POINT_OBJ; |
| 746 | event->select.id = obj->id; |
| 747 | log_debug("up to obj %d\n", event->select.id); |
| 748 | break; |
| 749 | } |
| 750 | } |
| 751 | break; |
| 752 | case BKEY_DOWN: |
| 753 | while (!list_is_last(&obj->sibling, &scn->obj_head)) { |
| 754 | obj = list_entry(obj->sibling.next, struct scene_obj, |
| 755 | sibling); |
Simon Glass | 193bfea | 2023-10-01 19:13:27 -0600 | [diff] [blame] | 756 | if (scene_obj_can_highlight(obj)) { |
Simon Glass | f0e1e8c | 2023-06-01 10:22:59 -0600 | [diff] [blame] | 757 | event->type = EXPOACT_POINT_OBJ; |
| 758 | event->select.id = obj->id; |
| 759 | log_debug("down to obj %d\n", event->select.id); |
| 760 | break; |
| 761 | } |
| 762 | } |
| 763 | break; |
| 764 | case BKEY_SELECT: |
Simon Glass | 193bfea | 2023-10-01 19:13:27 -0600 | [diff] [blame] | 765 | if (scene_obj_can_highlight(obj)) { |
Simon Glass | f0e1e8c | 2023-06-01 10:22:59 -0600 | [diff] [blame] | 766 | event->type = EXPOACT_OPEN; |
| 767 | event->select.id = obj->id; |
| 768 | log_debug("open obj %d\n", event->select.id); |
| 769 | } |
| 770 | break; |
| 771 | case BKEY_QUIT: |
| 772 | event->type = EXPOACT_QUIT; |
| 773 | log_debug("obj quit\n"); |
| 774 | break; |
| 775 | } |
| 776 | } |
| 777 | |
Simon Glass | 0a4d14b | 2023-01-06 08:52:37 -0600 | [diff] [blame] | 778 | int scene_send_key(struct scene *scn, int key, struct expo_action *event) |
| 779 | { |
| 780 | struct scene_obj *obj; |
| 781 | int ret; |
| 782 | |
Simon Glass | f0e1e8c | 2023-06-01 10:22:59 -0600 | [diff] [blame] | 783 | event->type = EXPOACT_NONE; |
| 784 | |
| 785 | /* |
| 786 | * In 'popup' mode, arrow keys move betwen objects, unless a menu is |
| 787 | * opened |
| 788 | */ |
| 789 | if (scn->expo->popup) { |
| 790 | obj = NULL; |
| 791 | if (scn->highlight_id) { |
| 792 | obj = scene_obj_find(scn, scn->highlight_id, |
| 793 | SCENEOBJT_NONE); |
| 794 | } |
| 795 | if (!obj) |
| 796 | return 0; |
| 797 | |
| 798 | if (!(obj->flags & SCENEOF_OPEN)) { |
| 799 | send_key_obj(scn, obj, key, event); |
| 800 | return 0; |
| 801 | } |
| 802 | |
Simon Glass | b7a6453 | 2023-10-01 19:13:24 -0600 | [diff] [blame] | 803 | switch (obj->type) { |
| 804 | case SCENEOBJT_NONE: |
| 805 | case SCENEOBJT_IMAGE: |
| 806 | case SCENEOBJT_TEXT: |
Simon Glass | 138a397 | 2025-05-02 08:46:44 -0600 | [diff] [blame^] | 807 | case SCENEOBJT_BOX: |
Simon Glass | b7a6453 | 2023-10-01 19:13:24 -0600 | [diff] [blame] | 808 | break; |
| 809 | case SCENEOBJT_MENU: { |
| 810 | struct scene_obj_menu *menu; |
| 811 | |
| 812 | menu = (struct scene_obj_menu *)obj, |
| 813 | ret = scene_menu_send_key(scn, menu, key, event); |
| 814 | if (ret) |
| 815 | return log_msg_ret("key", ret); |
| 816 | break; |
| 817 | } |
Simon Glass | 0023d18 | 2023-10-01 19:13:34 -0600 | [diff] [blame] | 818 | case SCENEOBJT_TEXTLINE: { |
| 819 | struct scene_obj_textline *tline; |
| 820 | |
| 821 | tline = (struct scene_obj_textline *)obj, |
| 822 | ret = scene_textline_send_key(scn, tline, key, event); |
| 823 | if (ret) |
| 824 | return log_msg_ret("key", ret); |
| 825 | break; |
| 826 | } |
Simon Glass | b7a6453 | 2023-10-01 19:13:24 -0600 | [diff] [blame] | 827 | } |
Simon Glass | f0e1e8c | 2023-06-01 10:22:59 -0600 | [diff] [blame] | 828 | return 0; |
| 829 | } |
| 830 | |
Simon Glass | 0a4d14b | 2023-01-06 08:52:37 -0600 | [diff] [blame] | 831 | list_for_each_entry(obj, &scn->obj_head, sibling) { |
| 832 | if (obj->type == SCENEOBJT_MENU) { |
| 833 | struct scene_obj_menu *menu; |
| 834 | |
| 835 | menu = (struct scene_obj_menu *)obj, |
| 836 | ret = scene_menu_send_key(scn, menu, key, event); |
| 837 | if (ret) |
| 838 | return log_msg_ret("key", ret); |
Simon Glass | 0a4d14b | 2023-01-06 08:52:37 -0600 | [diff] [blame] | 839 | break; |
| 840 | } |
| 841 | } |
| 842 | |
| 843 | return 0; |
| 844 | } |
Simon Glass | 7a96005 | 2023-06-01 10:22:52 -0600 | [diff] [blame] | 845 | |
Simon Glass | 96910ef | 2025-05-02 08:46:34 -0600 | [diff] [blame] | 846 | 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] | 847 | { |
| 848 | switch (obj->type) { |
| 849 | case SCENEOBJT_NONE: |
| 850 | case SCENEOBJT_IMAGE: |
| 851 | case SCENEOBJT_TEXT: |
Simon Glass | 138a397 | 2025-05-02 08:46:44 -0600 | [diff] [blame^] | 852 | case SCENEOBJT_BOX: |
Simon Glass | f099469 | 2023-10-01 19:13:29 -0600 | [diff] [blame] | 853 | return -ENOSYS; |
| 854 | case SCENEOBJT_MENU: { |
| 855 | struct scene_obj_menu *menu = (struct scene_obj_menu *)obj; |
| 856 | |
Simon Glass | 96910ef | 2025-05-02 08:46:34 -0600 | [diff] [blame] | 857 | scene_menu_calc_bbox(menu, bbox); |
Simon Glass | f099469 | 2023-10-01 19:13:29 -0600 | [diff] [blame] | 858 | break; |
| 859 | } |
Simon Glass | 0023d18 | 2023-10-01 19:13:34 -0600 | [diff] [blame] | 860 | case SCENEOBJT_TEXTLINE: { |
| 861 | struct scene_obj_textline *tline; |
| 862 | |
| 863 | tline = (struct scene_obj_textline *)obj; |
Simon Glass | 96910ef | 2025-05-02 08:46:34 -0600 | [diff] [blame] | 864 | scene_textline_calc_bbox(tline, &bbox[SCENEBB_all], |
| 865 | &bbox[SCENEBB_label]); |
Simon Glass | 0023d18 | 2023-10-01 19:13:34 -0600 | [diff] [blame] | 866 | break; |
| 867 | } |
Simon Glass | f099469 | 2023-10-01 19:13:29 -0600 | [diff] [blame] | 868 | } |
| 869 | |
| 870 | return 0; |
| 871 | } |
| 872 | |
Simon Glass | 7a96005 | 2023-06-01 10:22:52 -0600 | [diff] [blame] | 873 | int scene_calc_dims(struct scene *scn, bool do_menus) |
| 874 | { |
| 875 | struct scene_obj *obj; |
| 876 | int ret; |
| 877 | |
| 878 | list_for_each_entry(obj, &scn->obj_head, sibling) { |
| 879 | switch (obj->type) { |
| 880 | case SCENEOBJT_NONE: |
| 881 | case SCENEOBJT_TEXT: |
Simon Glass | 138a397 | 2025-05-02 08:46:44 -0600 | [diff] [blame^] | 882 | case SCENEOBJT_BOX: |
Simon Glass | 7a96005 | 2023-06-01 10:22:52 -0600 | [diff] [blame] | 883 | case SCENEOBJT_IMAGE: { |
| 884 | int width; |
| 885 | |
| 886 | if (!do_menus) { |
| 887 | ret = scene_obj_get_hw(scn, obj->id, &width); |
| 888 | if (ret < 0) |
| 889 | return log_msg_ret("get", ret); |
Simon Glass | ebec497 | 2025-05-02 08:46:33 -0600 | [diff] [blame] | 890 | obj->dims.x = width; |
| 891 | obj->dims.y = ret; |
| 892 | if (!(obj->flags & SCENEOF_SIZE_VALID)) { |
| 893 | obj->bbox.x1 = obj->bbox.x0 + width; |
| 894 | obj->bbox.y1 = obj->bbox.y0 + ret; |
| 895 | obj->flags |= SCENEOF_SIZE_VALID; |
| 896 | } |
Simon Glass | 7a96005 | 2023-06-01 10:22:52 -0600 | [diff] [blame] | 897 | } |
| 898 | break; |
| 899 | } |
| 900 | case SCENEOBJT_MENU: { |
| 901 | struct scene_obj_menu *menu; |
| 902 | |
| 903 | if (do_menus) { |
| 904 | menu = (struct scene_obj_menu *)obj; |
| 905 | |
| 906 | ret = scene_menu_calc_dims(menu); |
| 907 | if (ret) |
| 908 | return log_msg_ret("men", ret); |
| 909 | } |
| 910 | break; |
| 911 | } |
Simon Glass | 0023d18 | 2023-10-01 19:13:34 -0600 | [diff] [blame] | 912 | case SCENEOBJT_TEXTLINE: { |
| 913 | struct scene_obj_textline *tline; |
| 914 | |
| 915 | tline = (struct scene_obj_textline *)obj; |
| 916 | ret = scene_textline_calc_dims(tline); |
| 917 | if (ret) |
| 918 | return log_msg_ret("men", ret); |
| 919 | |
| 920 | break; |
| 921 | } |
Simon Glass | 7a96005 | 2023-06-01 10:22:52 -0600 | [diff] [blame] | 922 | } |
| 923 | } |
| 924 | |
| 925 | return 0; |
| 926 | } |
Simon Glass | c999e17 | 2023-06-01 10:22:53 -0600 | [diff] [blame] | 927 | |
| 928 | int scene_apply_theme(struct scene *scn, struct expo_theme *theme) |
| 929 | { |
| 930 | struct scene_obj *obj; |
| 931 | int ret; |
| 932 | |
| 933 | /* Avoid error-checking optional items */ |
| 934 | scene_txt_set_font(scn, scn->title_id, NULL, theme->font_size); |
| 935 | |
| 936 | list_for_each_entry(obj, &scn->obj_head, sibling) { |
| 937 | switch (obj->type) { |
| 938 | case SCENEOBJT_NONE: |
| 939 | case SCENEOBJT_IMAGE: |
| 940 | case SCENEOBJT_MENU: |
Simon Glass | 138a397 | 2025-05-02 08:46:44 -0600 | [diff] [blame^] | 941 | case SCENEOBJT_BOX: |
Simon Glass | 0023d18 | 2023-10-01 19:13:34 -0600 | [diff] [blame] | 942 | case SCENEOBJT_TEXTLINE: |
Simon Glass | c999e17 | 2023-06-01 10:22:53 -0600 | [diff] [blame] | 943 | break; |
| 944 | case SCENEOBJT_TEXT: |
| 945 | scene_txt_set_font(scn, obj->id, NULL, |
| 946 | theme->font_size); |
| 947 | break; |
| 948 | } |
| 949 | } |
| 950 | |
| 951 | ret = scene_arrange(scn); |
| 952 | if (ret) |
| 953 | return log_msg_ret("arr", ret); |
| 954 | |
| 955 | return 0; |
| 956 | } |
Simon Glass | 01922ec | 2023-06-01 10:22:57 -0600 | [diff] [blame] | 957 | |
| 958 | void scene_set_highlight_id(struct scene *scn, uint id) |
| 959 | { |
| 960 | scn->highlight_id = id; |
| 961 | } |
| 962 | |
| 963 | void scene_highlight_first(struct scene *scn) |
| 964 | { |
| 965 | struct scene_obj *obj; |
| 966 | |
| 967 | list_for_each_entry(obj, &scn->obj_head, sibling) { |
Simon Glass | 193bfea | 2023-10-01 19:13:27 -0600 | [diff] [blame] | 968 | if (scene_obj_can_highlight(obj)) { |
Simon Glass | 01922ec | 2023-06-01 10:22:57 -0600 | [diff] [blame] | 969 | scene_set_highlight_id(scn, obj->id); |
| 970 | return; |
Simon Glass | 01922ec | 2023-06-01 10:22:57 -0600 | [diff] [blame] | 971 | } |
| 972 | } |
| 973 | } |
| 974 | |
Simon Glass | f6a943a | 2023-10-01 19:13:33 -0600 | [diff] [blame] | 975 | static int scene_obj_open(struct scene *scn, struct scene_obj *obj) |
| 976 | { |
| 977 | int ret; |
| 978 | |
| 979 | switch (obj->type) { |
| 980 | case SCENEOBJT_NONE: |
| 981 | case SCENEOBJT_IMAGE: |
| 982 | case SCENEOBJT_MENU: |
| 983 | case SCENEOBJT_TEXT: |
Simon Glass | 138a397 | 2025-05-02 08:46:44 -0600 | [diff] [blame^] | 984 | case SCENEOBJT_BOX: |
Simon Glass | f6a943a | 2023-10-01 19:13:33 -0600 | [diff] [blame] | 985 | break; |
| 986 | case SCENEOBJT_TEXTLINE: |
| 987 | ret = scene_textline_open(scn, |
| 988 | (struct scene_obj_textline *)obj); |
| 989 | if (ret) |
| 990 | return log_msg_ret("op", ret); |
| 991 | break; |
| 992 | } |
| 993 | |
| 994 | return 0; |
| 995 | } |
| 996 | |
Simon Glass | 01922ec | 2023-06-01 10:22:57 -0600 | [diff] [blame] | 997 | int scene_set_open(struct scene *scn, uint id, bool open) |
| 998 | { |
Simon Glass | f6a943a | 2023-10-01 19:13:33 -0600 | [diff] [blame] | 999 | struct scene_obj *obj; |
Simon Glass | 01922ec | 2023-06-01 10:22:57 -0600 | [diff] [blame] | 1000 | int ret; |
| 1001 | |
Simon Glass | f6a943a | 2023-10-01 19:13:33 -0600 | [diff] [blame] | 1002 | obj = scene_obj_find(scn, id, SCENEOBJT_NONE); |
| 1003 | if (!obj) |
| 1004 | return log_msg_ret("find", -ENOENT); |
| 1005 | |
| 1006 | if (open) { |
| 1007 | ret = scene_obj_open(scn, obj); |
| 1008 | if (ret) |
| 1009 | return log_msg_ret("op", ret); |
| 1010 | } |
| 1011 | |
Simon Glass | 01922ec | 2023-06-01 10:22:57 -0600 | [diff] [blame] | 1012 | ret = scene_obj_flag_clrset(scn, id, SCENEOF_OPEN, |
| 1013 | open ? SCENEOF_OPEN : 0); |
| 1014 | if (ret) |
| 1015 | return log_msg_ret("flg", ret); |
| 1016 | |
| 1017 | return 0; |
| 1018 | } |
Simon Glass | e90acd8 | 2023-08-14 16:40:23 -0600 | [diff] [blame] | 1019 | |
| 1020 | int scene_iter_objs(struct scene *scn, expo_scene_obj_iterator iter, |
| 1021 | void *priv) |
| 1022 | { |
| 1023 | struct scene_obj *obj; |
| 1024 | |
| 1025 | list_for_each_entry(obj, &scn->obj_head, sibling) { |
| 1026 | int ret; |
| 1027 | |
| 1028 | ret = iter(obj, priv); |
| 1029 | if (ret) |
| 1030 | return log_msg_ret("itr", ret); |
| 1031 | } |
| 1032 | |
| 1033 | return 0; |
| 1034 | } |
Simon Glass | f099469 | 2023-10-01 19:13:29 -0600 | [diff] [blame] | 1035 | |
Simon Glass | 96910ef | 2025-05-02 08:46:34 -0600 | [diff] [blame] | 1036 | int scene_bbox_join(const struct vidconsole_bbox *src, int inset, |
| 1037 | struct vidconsole_bbox *dst) |
| 1038 | { |
| 1039 | if (dst->valid) { |
| 1040 | dst->x0 = min(dst->x0, src->x0 - inset); |
| 1041 | dst->y0 = min(dst->y0, src->y0); |
| 1042 | dst->x1 = max(dst->x1, src->x1 + inset); |
| 1043 | dst->y1 = max(dst->y1, src->y1); |
| 1044 | } else { |
| 1045 | dst->x0 = src->x0 - inset; |
| 1046 | dst->y0 = src->y0; |
| 1047 | dst->x1 = src->x1 + inset; |
| 1048 | dst->y1 = src->y1; |
| 1049 | dst->valid = true; |
| 1050 | } |
| 1051 | |
| 1052 | return 0; |
| 1053 | } |
| 1054 | |
Simon Glass | f099469 | 2023-10-01 19:13:29 -0600 | [diff] [blame] | 1055 | int scene_bbox_union(struct scene *scn, uint id, int inset, |
| 1056 | struct vidconsole_bbox *bbox) |
| 1057 | { |
| 1058 | struct scene_obj *obj; |
Simon Glass | 96910ef | 2025-05-02 08:46:34 -0600 | [diff] [blame] | 1059 | struct vidconsole_bbox local; |
Simon Glass | f099469 | 2023-10-01 19:13:29 -0600 | [diff] [blame] | 1060 | |
| 1061 | if (!id) |
| 1062 | return 0; |
| 1063 | obj = scene_obj_find(scn, id, SCENEOBJT_NONE); |
| 1064 | if (!obj) |
| 1065 | return log_msg_ret("obj", -ENOENT); |
Simon Glass | 96910ef | 2025-05-02 08:46:34 -0600 | [diff] [blame] | 1066 | local.x0 = obj->bbox.x0; |
| 1067 | local.y0 = obj->bbox.y0; |
| 1068 | local.x1 = obj->bbox.x1; |
| 1069 | local.y1 = obj->bbox.y1; |
| 1070 | local.valid = true; |
| 1071 | scene_bbox_join(&local, inset, bbox); |
Simon Glass | f099469 | 2023-10-01 19:13:29 -0600 | [diff] [blame] | 1072 | |
| 1073 | return 0; |
| 1074 | } |