blob: 0b44a13748ab4005816fe4211736e41fac1b2e03 [file] [log] [blame]
Simon Glass5e2607a2023-01-06 08:52:37 -06001// 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 Glassc98cb512023-06-01 10:22:43 -06009#define LOG_CATEGORY LOGC_EXPO
10
Simon Glass5e2607a2023-01-06 08:52:37 -060011#include <common.h>
12#include <dm.h>
13#include <expo.h>
14#include <malloc.h>
15#include <mapmem.h>
Simon Glass4e64bee2023-06-01 10:22:59 -060016#include <menu.h>
Simon Glass5e2607a2023-01-06 08:52:37 -060017#include <video.h>
18#include <video_console.h>
19#include <linux/input.h>
20#include "scene_internal.h"
21
Simon Glass5e2607a2023-01-06 08:52:37 -060022int 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 Glass93f99b32023-10-01 19:13:31 -060035 abuf_init(&scn->buf);
36 if (!abuf_realloc(&scn->buf, EXPO_MAX_CHARS + 1)) {
37 free(scn->name);
38 free(scn);
39 return log_msg_ret("buf", -ENOMEM);
40 }
41 abuf_init(&scn->entry_save);
42
Simon Glass5e2607a2023-01-06 08:52:37 -060043 INIT_LIST_HEAD(&scn->obj_head);
44 scn->id = resolve_id(exp, id);
45 scn->expo = exp;
46 list_add_tail(&scn->sibling, &exp->scene_head);
47
48 *scnp = scn;
49
50 return scn->id;
51}
52
53void scene_obj_destroy(struct scene_obj *obj)
54{
55 if (obj->type == SCENEOBJT_MENU)
56 scene_menu_destroy((struct scene_obj_menu *)obj);
57 free(obj->name);
58 free(obj);
59}
60
61void scene_destroy(struct scene *scn)
62{
63 struct scene_obj *obj, *next;
64
65 list_for_each_entry_safe(obj, next, &scn->obj_head, sibling)
66 scene_obj_destroy(obj);
67
Simon Glass93f99b32023-10-01 19:13:31 -060068 abuf_uninit(&scn->entry_save);
69 abuf_uninit(&scn->buf);
Simon Glass5e2607a2023-01-06 08:52:37 -060070 free(scn->name);
Simon Glass5e2607a2023-01-06 08:52:37 -060071 free(scn);
72}
73
Simon Glassdef898c2023-06-01 10:22:27 -060074int scene_title_set(struct scene *scn, uint id)
Simon Glass5e2607a2023-01-06 08:52:37 -060075{
Simon Glassdef898c2023-06-01 10:22:27 -060076 scn->title_id = id;
Simon Glass5e2607a2023-01-06 08:52:37 -060077
78 return 0;
79}
80
81int scene_obj_count(struct scene *scn)
82{
83 struct scene_obj *obj;
84 int count = 0;
85
86 list_for_each_entry(obj, &scn->obj_head, sibling)
87 count++;
88
89 return count;
90}
91
Simon Glass633b3dc2023-08-14 16:40:21 -060092void *scene_obj_find(const struct scene *scn, uint id, enum scene_obj_t type)
Simon Glass5e2607a2023-01-06 08:52:37 -060093{
94 struct scene_obj *obj;
95
96 list_for_each_entry(obj, &scn->obj_head, sibling) {
97 if (obj->id == id &&
98 (type == SCENEOBJT_NONE || obj->type == type))
99 return obj;
100 }
101
102 return NULL;
103}
104
Simon Glassa0874dc2023-06-01 10:23:02 -0600105void *scene_obj_find_by_name(struct scene *scn, const char *name)
106{
107 struct scene_obj *obj;
108
109 list_for_each_entry(obj, &scn->obj_head, sibling) {
110 if (!strcmp(name, obj->name))
111 return obj;
112 }
113
114 return NULL;
115}
116
Simon Glass5e2607a2023-01-06 08:52:37 -0600117int scene_obj_add(struct scene *scn, const char *name, uint id,
118 enum scene_obj_t type, uint size, struct scene_obj **objp)
119{
120 struct scene_obj *obj;
121
122 obj = calloc(1, size);
123 if (!obj)
124 return log_msg_ret("obj", -ENOMEM);
125 obj->name = strdup(name);
126 if (!obj->name) {
127 free(obj);
128 return log_msg_ret("name", -ENOMEM);
129 }
130
131 obj->id = resolve_id(scn->expo, id);
132 obj->scene = scn;
133 obj->type = type;
134 list_add_tail(&obj->sibling, &scn->obj_head);
135 *objp = obj;
136
137 return obj->id;
138}
139
140int scene_img(struct scene *scn, const char *name, uint id, char *data,
141 struct scene_obj_img **imgp)
142{
143 struct scene_obj_img *img;
144 int ret;
145
146 ret = scene_obj_add(scn, name, id, SCENEOBJT_IMAGE,
147 sizeof(struct scene_obj_img),
148 (struct scene_obj **)&img);
149 if (ret < 0)
Simon Glass9767de72023-10-01 19:13:25 -0600150 return log_msg_ret("obj", ret);
Simon Glass5e2607a2023-01-06 08:52:37 -0600151
152 img->data = data;
153
154 if (imgp)
155 *imgp = img;
156
157 return img->obj.id;
158}
159
160int scene_txt(struct scene *scn, const char *name, uint id, uint str_id,
161 struct scene_obj_txt **txtp)
162{
163 struct scene_obj_txt *txt;
164 int ret;
165
166 ret = scene_obj_add(scn, name, id, SCENEOBJT_TEXT,
167 sizeof(struct scene_obj_txt),
168 (struct scene_obj **)&txt);
169 if (ret < 0)
Simon Glass9767de72023-10-01 19:13:25 -0600170 return log_msg_ret("obj", ret);
Simon Glass5e2607a2023-01-06 08:52:37 -0600171
172 txt->str_id = str_id;
173
174 if (txtp)
175 *txtp = txt;
176
177 return txt->obj.id;
178}
179
180int scene_txt_str(struct scene *scn, const char *name, uint id, uint str_id,
181 const char *str, struct scene_obj_txt **txtp)
182{
183 struct scene_obj_txt *txt;
184 int ret;
185
186 ret = expo_str(scn->expo, name, str_id, str);
187 if (ret < 0)
188 return log_msg_ret("str", ret);
Simon Glass408011c2023-10-01 19:13:26 -0600189 if (str_id && ret != str_id)
Simon Glass5e2607a2023-01-06 08:52:37 -0600190 return log_msg_ret("id", -EEXIST);
Simon Glass408011c2023-10-01 19:13:26 -0600191 str_id = ret;
Simon Glass5e2607a2023-01-06 08:52:37 -0600192
193 ret = scene_obj_add(scn, name, id, SCENEOBJT_TEXT,
194 sizeof(struct scene_obj_txt),
195 (struct scene_obj **)&txt);
196 if (ret < 0)
Simon Glass9767de72023-10-01 19:13:25 -0600197 return log_msg_ret("obj", ret);
Simon Glass5e2607a2023-01-06 08:52:37 -0600198
199 txt->str_id = str_id;
200
201 if (txtp)
202 *txtp = txt;
203
204 return txt->obj.id;
205}
206
207int scene_txt_set_font(struct scene *scn, uint id, const char *font_name,
208 uint font_size)
209{
210 struct scene_obj_txt *txt;
211
212 txt = scene_obj_find(scn, id, SCENEOBJT_TEXT);
213 if (!txt)
214 return log_msg_ret("find", -ENOENT);
215 txt->font_name = font_name;
216 txt->font_size = font_size;
217
218 return 0;
219}
220
221int scene_obj_set_pos(struct scene *scn, uint id, int x, int y)
222{
223 struct scene_obj *obj;
224
225 obj = scene_obj_find(scn, id, SCENEOBJT_NONE);
226 if (!obj)
227 return log_msg_ret("find", -ENOENT);
Simon Glassae45d6c2023-06-01 10:22:49 -0600228 obj->dim.x = x;
229 obj->dim.y = y;
Simon Glass5e2607a2023-01-06 08:52:37 -0600230
231 return 0;
232}
233
Simon Glass699b0ac2023-06-01 10:22:52 -0600234int scene_obj_set_size(struct scene *scn, uint id, int w, int h)
235{
236 struct scene_obj *obj;
237
238 obj = scene_obj_find(scn, id, SCENEOBJT_NONE);
239 if (!obj)
240 return log_msg_ret("find", -ENOENT);
241 obj->dim.w = w;
242 obj->dim.h = h;
243
244 return 0;
245}
246
Simon Glass5e2607a2023-01-06 08:52:37 -0600247int scene_obj_set_hide(struct scene *scn, uint id, bool hide)
248{
Simon Glassce72c9e2023-06-01 10:22:50 -0600249 int ret;
250
251 ret = scene_obj_flag_clrset(scn, id, SCENEOF_HIDE,
252 hide ? SCENEOF_HIDE : 0);
253 if (ret)
254 return log_msg_ret("flg", ret);
255
256 return 0;
257}
258
259int scene_obj_flag_clrset(struct scene *scn, uint id, uint clr, uint set)
260{
Simon Glass5e2607a2023-01-06 08:52:37 -0600261 struct scene_obj *obj;
262
263 obj = scene_obj_find(scn, id, SCENEOBJT_NONE);
264 if (!obj)
265 return log_msg_ret("find", -ENOENT);
Simon Glassce72c9e2023-06-01 10:22:50 -0600266 obj->flags &= ~clr;
267 obj->flags |= set;
Simon Glass5e2607a2023-01-06 08:52:37 -0600268
269 return 0;
270}
271
272int scene_obj_get_hw(struct scene *scn, uint id, int *widthp)
273{
274 struct scene_obj *obj;
275
276 obj = scene_obj_find(scn, id, SCENEOBJT_NONE);
277 if (!obj)
278 return log_msg_ret("find", -ENOENT);
279
280 switch (obj->type) {
281 case SCENEOBJT_NONE:
282 case SCENEOBJT_MENU:
283 break;
284 case SCENEOBJT_IMAGE: {
285 struct scene_obj_img *img = (struct scene_obj_img *)obj;
286 ulong width, height;
287 uint bpix;
288
289 video_bmp_get_info(img->data, &width, &height, &bpix);
290 if (widthp)
291 *widthp = width;
292 return height;
293 }
294 case SCENEOBJT_TEXT: {
295 struct scene_obj_txt *txt = (struct scene_obj_txt *)obj;
296 struct expo *exp = scn->expo;
Simon Glass50f02032023-06-01 10:22:51 -0600297 struct vidconsole_bbox bbox;
298 const char *str;
299 int len, ret;
Simon Glass5e2607a2023-01-06 08:52:37 -0600300
Simon Glass50f02032023-06-01 10:22:51 -0600301 str = expo_get_str(exp, txt->str_id);
302 if (!str)
303 return log_msg_ret("str", -ENOENT);
304 len = strlen(str);
305
306 /* if there is no console, make it up */
307 if (!exp->cons) {
308 if (widthp)
309 *widthp = 8 * len;
310 return 16;
311 }
312
313 ret = vidconsole_measure(scn->expo->cons, txt->font_name,
314 txt->font_size, str, &bbox);
315 if (ret)
316 return log_msg_ret("mea", ret);
Simon Glass5e2607a2023-01-06 08:52:37 -0600317 if (widthp)
Simon Glass50f02032023-06-01 10:22:51 -0600318 *widthp = bbox.x1;
Simon Glass5e2607a2023-01-06 08:52:37 -0600319
Simon Glass50f02032023-06-01 10:22:51 -0600320 return bbox.y1;
Simon Glass5e2607a2023-01-06 08:52:37 -0600321 }
322 }
323
324 return 0;
325}
326
327/**
Simon Glass94598d52023-10-01 19:13:30 -0600328 * scene_render_background() - Render the background for an object
329 *
330 * @obj: Object to render
331 */
332static void scene_render_background(struct scene_obj *obj)
333{
334 struct expo *exp = obj->scene->expo;
335 const struct expo_theme *theme = &exp->theme;
336 struct vidconsole_bbox bbox, label_bbox;
337 struct udevice *dev = exp->display;
338 struct video_priv *vid_priv;
339 struct udevice *cons = exp->cons;
340 struct vidconsole_colour old;
341 enum colour_idx fore, back;
342 uint inset = theme->menu_inset;
343
344 /* draw a background for the object */
345 if (CONFIG_IS_ENABLED(SYS_WHITE_ON_BLACK)) {
346 fore = VID_BLACK;
347 back = VID_WHITE;
348 } else {
349 fore = VID_LIGHT_GRAY;
350 back = VID_BLACK;
351 }
352
353 /* see if this object wants to render a background */
354 if (scene_obj_calc_bbox(obj, &bbox, &label_bbox))
355 return;
356
357 vidconsole_push_colour(cons, fore, back, &old);
358 vid_priv = dev_get_uclass_priv(dev);
359 video_fill_part(dev, label_bbox.x0 - inset, label_bbox.y0 - inset,
360 label_bbox.x1 + inset, label_bbox.y1 + inset,
361 vid_priv->colour_fg);
362 vidconsole_pop_colour(cons, &old);
363}
364
365/**
Simon Glass5e2607a2023-01-06 08:52:37 -0600366 * scene_obj_render() - Render an object
367 *
368 */
369static int scene_obj_render(struct scene_obj *obj, bool text_mode)
370{
371 struct scene *scn = obj->scene;
372 struct expo *exp = scn->expo;
Simon Glass7230fdb2023-06-01 10:23:00 -0600373 const struct expo_theme *theme = &exp->theme;
Simon Glass42b18492023-06-01 10:22:34 -0600374 struct udevice *dev = exp->display;
375 struct udevice *cons = text_mode ? NULL : exp->cons;
Simon Glass5e2607a2023-01-06 08:52:37 -0600376 int x, y, ret;
377
Simon Glassae45d6c2023-06-01 10:22:49 -0600378 x = obj->dim.x;
379 y = obj->dim.y;
Simon Glass5e2607a2023-01-06 08:52:37 -0600380
381 switch (obj->type) {
382 case SCENEOBJT_NONE:
383 break;
384 case SCENEOBJT_IMAGE: {
385 struct scene_obj_img *img = (struct scene_obj_img *)obj;
386
387 if (!cons)
388 return -ENOTSUPP;
389 ret = video_bmp_display(dev, map_to_sysmem(img->data), x, y,
390 true);
391 if (ret < 0)
392 return log_msg_ret("img", ret);
393 break;
394 }
395 case SCENEOBJT_TEXT: {
396 struct scene_obj_txt *txt = (struct scene_obj_txt *)obj;
397 const char *str;
398
399 if (!cons)
400 return -ENOTSUPP;
401
402 if (txt->font_name || txt->font_size) {
403 ret = vidconsole_select_font(cons,
404 txt->font_name,
405 txt->font_size);
406 } else {
407 ret = vidconsole_select_font(cons, NULL, 0);
408 }
409 if (ret && ret != -ENOSYS)
410 return log_msg_ret("font", ret);
Simon Glass5e2607a2023-01-06 08:52:37 -0600411 str = expo_get_str(exp, txt->str_id);
Simon Glass756c9552023-06-01 10:22:57 -0600412 if (str) {
413 struct video_priv *vid_priv;
414 struct vidconsole_colour old;
415 enum colour_idx fore, back;
416
417 if (CONFIG_IS_ENABLED(SYS_WHITE_ON_BLACK)) {
418 fore = VID_BLACK;
419 back = VID_WHITE;
420 } else {
421 fore = VID_LIGHT_GRAY;
422 back = VID_BLACK;
423 }
424
425 vid_priv = dev_get_uclass_priv(dev);
426 if (obj->flags & SCENEOF_POINT) {
427 vidconsole_push_colour(cons, fore, back, &old);
Simon Glass7230fdb2023-06-01 10:23:00 -0600428 video_fill_part(dev, x - theme->menu_inset, y,
429 x + obj->dim.w,
430 y + obj->dim.h,
Simon Glass756c9552023-06-01 10:22:57 -0600431 vid_priv->colour_bg);
432 }
433 vidconsole_set_cursor_pos(cons, x, y);
Simon Glass5e2607a2023-01-06 08:52:37 -0600434 vidconsole_put_string(cons, str);
Simon Glass756c9552023-06-01 10:22:57 -0600435 if (obj->flags & SCENEOF_POINT)
436 vidconsole_pop_colour(cons, &old);
437 }
Simon Glass5e2607a2023-01-06 08:52:37 -0600438 break;
439 }
440 case SCENEOBJT_MENU: {
441 struct scene_obj_menu *menu = (struct scene_obj_menu *)obj;
Simon Glass756c9552023-06-01 10:22:57 -0600442
443 if (exp->popup && (obj->flags & SCENEOF_OPEN)) {
444 if (!cons)
445 return -ENOTSUPP;
446
447 /* draw a background behind the menu items */
Simon Glass94598d52023-10-01 19:13:30 -0600448 scene_render_background(obj);
Simon Glass756c9552023-06-01 10:22:57 -0600449 }
Simon Glass5e2607a2023-01-06 08:52:37 -0600450 /*
451 * With a vidconsole, the text and item pointer are rendered as
452 * normal objects so we don't need to do anything here. The menu
453 * simply controls where they are positioned.
454 */
455 if (cons)
456 return -ENOTSUPP;
457
458 ret = scene_menu_display(menu);
459 if (ret < 0)
460 return log_msg_ret("img", ret);
461
462 break;
463 }
464 }
465
466 return 0;
467}
468
469int scene_arrange(struct scene *scn)
470{
471 struct scene_obj *obj;
472 int ret;
473
474 list_for_each_entry(obj, &scn->obj_head, sibling) {
Simon Glassfd6073a2023-10-01 19:13:24 -0600475 switch (obj->type) {
476 case SCENEOBJT_NONE:
477 case SCENEOBJT_IMAGE:
478 case SCENEOBJT_TEXT:
479 break;
480 case SCENEOBJT_MENU: {
Simon Glass5e2607a2023-01-06 08:52:37 -0600481 struct scene_obj_menu *menu;
482
483 menu = (struct scene_obj_menu *)obj,
484 ret = scene_menu_arrange(scn, menu);
485 if (ret)
486 return log_msg_ret("arr", ret);
Simon Glassfd6073a2023-10-01 19:13:24 -0600487 break;
488 }
Simon Glass5e2607a2023-01-06 08:52:37 -0600489 }
490 }
491
492 return 0;
493}
494
Simon Glass4c87e072023-06-01 10:22:58 -0600495int scene_render_deps(struct scene *scn, uint id)
496{
497 struct scene_obj *obj;
498 int ret;
499
500 if (!id)
501 return 0;
502 obj = scene_obj_find(scn, id, SCENEOBJT_NONE);
503 if (!obj)
504 return log_msg_ret("obj", -ENOENT);
505
506 if (!(obj->flags & SCENEOF_HIDE)) {
507 ret = scene_obj_render(obj, false);
508 if (ret && ret != -ENOTSUPP)
509 return log_msg_ret("ren", ret);
510
Simon Glassfd6073a2023-10-01 19:13:24 -0600511 switch (obj->type) {
512 case SCENEOBJT_NONE:
513 case SCENEOBJT_IMAGE:
514 case SCENEOBJT_TEXT:
515 break;
516 case SCENEOBJT_MENU:
Simon Glass4c87e072023-06-01 10:22:58 -0600517 scene_menu_render_deps(scn,
518 (struct scene_obj_menu *)obj);
Simon Glassfd6073a2023-10-01 19:13:24 -0600519 break;
520 }
Simon Glass4c87e072023-06-01 10:22:58 -0600521 }
522
523 return 0;
524}
525
Simon Glass5e2607a2023-01-06 08:52:37 -0600526int scene_render(struct scene *scn)
527{
528 struct expo *exp = scn->expo;
529 struct scene_obj *obj;
530 int ret;
531
532 list_for_each_entry(obj, &scn->obj_head, sibling) {
Simon Glassce72c9e2023-06-01 10:22:50 -0600533 if (!(obj->flags & SCENEOF_HIDE)) {
Simon Glass5e2607a2023-01-06 08:52:37 -0600534 ret = scene_obj_render(obj, exp->text_mode);
535 if (ret && ret != -ENOTSUPP)
536 return log_msg_ret("ren", ret);
537 }
538 }
539
Simon Glass4c87e072023-06-01 10:22:58 -0600540 /* render any highlighted object on top of the others */
541 if (scn->highlight_id && !exp->text_mode) {
542 ret = scene_render_deps(scn, scn->highlight_id);
543 if (ret && ret != -ENOTSUPP)
544 return log_msg_ret("dep", ret);
545 }
546
Simon Glass5e2607a2023-01-06 08:52:37 -0600547 return 0;
548}
549
Simon Glass4e64bee2023-06-01 10:22:59 -0600550/**
551 * send_key_obj() - Handle a keypress for moving between objects
552 *
553 * @scn: Scene to receive the key
554 * @key: Key to send (KEYCODE_UP)
555 * @event: Returns resulting event from this keypress
556 * Returns: 0 if OK, -ve on error
557 */
558static void send_key_obj(struct scene *scn, struct scene_obj *obj, int key,
559 struct expo_action *event)
560{
561 switch (key) {
562 case BKEY_UP:
563 while (obj != list_first_entry(&scn->obj_head, struct scene_obj,
564 sibling)) {
565 obj = list_entry(obj->sibling.prev,
566 struct scene_obj, sibling);
Simon Glassd88edd22023-10-01 19:13:27 -0600567 if (scene_obj_can_highlight(obj)) {
Simon Glass4e64bee2023-06-01 10:22:59 -0600568 event->type = EXPOACT_POINT_OBJ;
569 event->select.id = obj->id;
570 log_debug("up to obj %d\n", event->select.id);
571 break;
572 }
573 }
574 break;
575 case BKEY_DOWN:
576 while (!list_is_last(&obj->sibling, &scn->obj_head)) {
577 obj = list_entry(obj->sibling.next, struct scene_obj,
578 sibling);
Simon Glassd88edd22023-10-01 19:13:27 -0600579 if (scene_obj_can_highlight(obj)) {
Simon Glass4e64bee2023-06-01 10:22:59 -0600580 event->type = EXPOACT_POINT_OBJ;
581 event->select.id = obj->id;
582 log_debug("down to obj %d\n", event->select.id);
583 break;
584 }
585 }
586 break;
587 case BKEY_SELECT:
Simon Glassd88edd22023-10-01 19:13:27 -0600588 if (scene_obj_can_highlight(obj)) {
Simon Glass4e64bee2023-06-01 10:22:59 -0600589 event->type = EXPOACT_OPEN;
590 event->select.id = obj->id;
591 log_debug("open obj %d\n", event->select.id);
592 }
593 break;
594 case BKEY_QUIT:
595 event->type = EXPOACT_QUIT;
596 log_debug("obj quit\n");
597 break;
598 }
599}
600
Simon Glass5e2607a2023-01-06 08:52:37 -0600601int scene_send_key(struct scene *scn, int key, struct expo_action *event)
602{
603 struct scene_obj *obj;
604 int ret;
605
Simon Glass4e64bee2023-06-01 10:22:59 -0600606 event->type = EXPOACT_NONE;
607
608 /*
609 * In 'popup' mode, arrow keys move betwen objects, unless a menu is
610 * opened
611 */
612 if (scn->expo->popup) {
613 obj = NULL;
614 if (scn->highlight_id) {
615 obj = scene_obj_find(scn, scn->highlight_id,
616 SCENEOBJT_NONE);
617 }
618 if (!obj)
619 return 0;
620
621 if (!(obj->flags & SCENEOF_OPEN)) {
622 send_key_obj(scn, obj, key, event);
623 return 0;
624 }
625
Simon Glassfd6073a2023-10-01 19:13:24 -0600626 switch (obj->type) {
627 case SCENEOBJT_NONE:
628 case SCENEOBJT_IMAGE:
629 case SCENEOBJT_TEXT:
630 break;
631 case SCENEOBJT_MENU: {
632 struct scene_obj_menu *menu;
633
634 menu = (struct scene_obj_menu *)obj,
635 ret = scene_menu_send_key(scn, menu, key, event);
636 if (ret)
637 return log_msg_ret("key", ret);
638 break;
639 }
640 }
Simon Glass4e64bee2023-06-01 10:22:59 -0600641 return 0;
642 }
643
Simon Glass5e2607a2023-01-06 08:52:37 -0600644 list_for_each_entry(obj, &scn->obj_head, sibling) {
645 if (obj->type == SCENEOBJT_MENU) {
646 struct scene_obj_menu *menu;
647
648 menu = (struct scene_obj_menu *)obj,
649 ret = scene_menu_send_key(scn, menu, key, event);
650 if (ret)
651 return log_msg_ret("key", ret);
Simon Glass5e2607a2023-01-06 08:52:37 -0600652 break;
653 }
654 }
655
656 return 0;
657}
Simon Glass699b0ac2023-06-01 10:22:52 -0600658
Simon Glass8bc69b42023-10-01 19:13:29 -0600659int scene_obj_calc_bbox(struct scene_obj *obj, struct vidconsole_bbox *bbox,
660 struct vidconsole_bbox *label_bbox)
661{
662 switch (obj->type) {
663 case SCENEOBJT_NONE:
664 case SCENEOBJT_IMAGE:
665 case SCENEOBJT_TEXT:
666 return -ENOSYS;
667 case SCENEOBJT_MENU: {
668 struct scene_obj_menu *menu = (struct scene_obj_menu *)obj;
669
670 scene_menu_calc_bbox(menu, bbox, label_bbox);
671 break;
672 }
673 }
674
675 return 0;
676}
677
Simon Glass699b0ac2023-06-01 10:22:52 -0600678int scene_calc_dims(struct scene *scn, bool do_menus)
679{
680 struct scene_obj *obj;
681 int ret;
682
683 list_for_each_entry(obj, &scn->obj_head, sibling) {
684 switch (obj->type) {
685 case SCENEOBJT_NONE:
686 case SCENEOBJT_TEXT:
687 case SCENEOBJT_IMAGE: {
688 int width;
689
690 if (!do_menus) {
691 ret = scene_obj_get_hw(scn, obj->id, &width);
692 if (ret < 0)
693 return log_msg_ret("get", ret);
694 obj->dim.w = width;
695 obj->dim.h = ret;
696 }
697 break;
698 }
699 case SCENEOBJT_MENU: {
700 struct scene_obj_menu *menu;
701
702 if (do_menus) {
703 menu = (struct scene_obj_menu *)obj;
704
705 ret = scene_menu_calc_dims(menu);
706 if (ret)
707 return log_msg_ret("men", ret);
708 }
709 break;
710 }
711 }
712 }
713
714 return 0;
715}
Simon Glass2e593892023-06-01 10:22:53 -0600716
717int scene_apply_theme(struct scene *scn, struct expo_theme *theme)
718{
719 struct scene_obj *obj;
720 int ret;
721
722 /* Avoid error-checking optional items */
723 scene_txt_set_font(scn, scn->title_id, NULL, theme->font_size);
724
725 list_for_each_entry(obj, &scn->obj_head, sibling) {
726 switch (obj->type) {
727 case SCENEOBJT_NONE:
728 case SCENEOBJT_IMAGE:
729 case SCENEOBJT_MENU:
730 break;
731 case SCENEOBJT_TEXT:
732 scene_txt_set_font(scn, obj->id, NULL,
733 theme->font_size);
734 break;
735 }
736 }
737
738 ret = scene_arrange(scn);
739 if (ret)
740 return log_msg_ret("arr", ret);
741
742 return 0;
743}
Simon Glass756c9552023-06-01 10:22:57 -0600744
745void scene_set_highlight_id(struct scene *scn, uint id)
746{
747 scn->highlight_id = id;
748}
749
750void scene_highlight_first(struct scene *scn)
751{
752 struct scene_obj *obj;
753
754 list_for_each_entry(obj, &scn->obj_head, sibling) {
Simon Glassd88edd22023-10-01 19:13:27 -0600755 if (scene_obj_can_highlight(obj)) {
Simon Glass756c9552023-06-01 10:22:57 -0600756 scene_set_highlight_id(scn, obj->id);
757 return;
Simon Glass756c9552023-06-01 10:22:57 -0600758 }
759 }
760}
761
762int scene_set_open(struct scene *scn, uint id, bool open)
763{
764 int ret;
765
766 ret = scene_obj_flag_clrset(scn, id, SCENEOF_OPEN,
767 open ? SCENEOF_OPEN : 0);
768 if (ret)
769 return log_msg_ret("flg", ret);
770
771 return 0;
772}
Simon Glassf2eb6ad2023-08-14 16:40:23 -0600773
774int scene_iter_objs(struct scene *scn, expo_scene_obj_iterator iter,
775 void *priv)
776{
777 struct scene_obj *obj;
778
779 list_for_each_entry(obj, &scn->obj_head, sibling) {
780 int ret;
781
782 ret = iter(obj, priv);
783 if (ret)
784 return log_msg_ret("itr", ret);
785 }
786
787 return 0;
788}
Simon Glass8bc69b42023-10-01 19:13:29 -0600789
790int scene_bbox_union(struct scene *scn, uint id, int inset,
791 struct vidconsole_bbox *bbox)
792{
793 struct scene_obj *obj;
794
795 if (!id)
796 return 0;
797 obj = scene_obj_find(scn, id, SCENEOBJT_NONE);
798 if (!obj)
799 return log_msg_ret("obj", -ENOENT);
800 if (bbox->valid) {
801 bbox->x0 = min(bbox->x0, obj->dim.x - inset);
802 bbox->y0 = min(bbox->y0, obj->dim.y);
803 bbox->x1 = max(bbox->x1, obj->dim.x + obj->dim.w + inset);
804 bbox->y1 = max(bbox->y1, obj->dim.y + obj->dim.h);
805 } else {
806 bbox->x0 = obj->dim.x - inset;
807 bbox->y0 = obj->dim.y;
808 bbox->x1 = obj->dim.x + obj->dim.w + inset;
809 bbox->y1 = obj->dim.y + obj->dim.h;
810 bbox->valid = true;
811 }
812
813 return 0;
814}