blob: 270c9c67233939811b3a2dce4034a4db94e696af [file] [log] [blame]
Simon Glass0a4d14b2023-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 Glassfe4c1e22023-06-01 10:22:43 -06009#define LOG_CATEGORY LOGC_EXPO
10
Simon Glass0a4d14b2023-01-06 08:52:37 -060011#include <dm.h>
12#include <expo.h>
13#include <malloc.h>
14#include <mapmem.h>
Simon Glassf0e1e8c2023-06-01 10:22:59 -060015#include <menu.h>
Simon Glass0a4d14b2023-01-06 08:52:37 -060016#include <video.h>
17#include <video_console.h>
18#include <linux/input.h>
19#include "scene_internal.h"
20
Simon Glass0a4d14b2023-01-06 08:52:37 -060021int scene_new(struct expo *exp, const char *name, uint id, struct scene **scnp)
22{
23 struct scene *scn;
24
25 scn = calloc(1, sizeof(struct scene));
26 if (!scn)
27 return log_msg_ret("expo", -ENOMEM);
28 scn->name = strdup(name);
29 if (!scn->name) {
30 free(scn);
31 return log_msg_ret("name", -ENOMEM);
32 }
33
Simon Glassa968f5f2023-10-01 19:13:31 -060034 abuf_init(&scn->buf);
35 if (!abuf_realloc(&scn->buf, EXPO_MAX_CHARS + 1)) {
36 free(scn->name);
37 free(scn);
38 return log_msg_ret("buf", -ENOMEM);
39 }
40 abuf_init(&scn->entry_save);
41
Simon Glass0a4d14b2023-01-06 08:52:37 -060042 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
52void 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
60void 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 Glassa968f5f2023-10-01 19:13:31 -060067 abuf_uninit(&scn->entry_save);
68 abuf_uninit(&scn->buf);
Simon Glass0a4d14b2023-01-06 08:52:37 -060069 free(scn->name);
Simon Glass0a4d14b2023-01-06 08:52:37 -060070 free(scn);
71}
72
Simon Glassea274b62023-06-01 10:22:27 -060073int scene_title_set(struct scene *scn, uint id)
Simon Glass0a4d14b2023-01-06 08:52:37 -060074{
Simon Glassea274b62023-06-01 10:22:27 -060075 scn->title_id = id;
Simon Glass0a4d14b2023-01-06 08:52:37 -060076
77 return 0;
78}
79
80int scene_obj_count(struct scene *scn)
81{
Sughosh Ganuebb1c202024-08-28 22:24:22 +053082 return list_count_nodes(&scn->obj_head);
Simon Glass0a4d14b2023-01-06 08:52:37 -060083}
84
Simon Glass45ff0bc2023-08-14 16:40:21 -060085void *scene_obj_find(const struct scene *scn, uint id, enum scene_obj_t type)
Simon Glass0a4d14b2023-01-06 08:52:37 -060086{
87 struct scene_obj *obj;
88
89 list_for_each_entry(obj, &scn->obj_head, sibling) {
90 if (obj->id == id &&
91 (type == SCENEOBJT_NONE || obj->type == type))
92 return obj;
93 }
94
95 return NULL;
96}
97
Simon Glassc8925112023-06-01 10:23:02 -060098void *scene_obj_find_by_name(struct scene *scn, const char *name)
99{
100 struct scene_obj *obj;
101
102 list_for_each_entry(obj, &scn->obj_head, sibling) {
103 if (!strcmp(name, obj->name))
104 return obj;
105 }
106
107 return NULL;
108}
109
Simon Glass0a4d14b2023-01-06 08:52:37 -0600110int scene_obj_add(struct scene *scn, const char *name, uint id,
111 enum scene_obj_t type, uint size, struct scene_obj **objp)
112{
113 struct scene_obj *obj;
114
115 obj = calloc(1, size);
116 if (!obj)
117 return log_msg_ret("obj", -ENOMEM);
118 obj->name = strdup(name);
119 if (!obj->name) {
120 free(obj);
121 return log_msg_ret("name", -ENOMEM);
122 }
123
124 obj->id = resolve_id(scn->expo, id);
125 obj->scene = scn;
126 obj->type = type;
127 list_add_tail(&obj->sibling, &scn->obj_head);
128 *objp = obj;
129
130 return obj->id;
131}
132
133int scene_img(struct scene *scn, const char *name, uint id, char *data,
134 struct scene_obj_img **imgp)
135{
136 struct scene_obj_img *img;
137 int ret;
138
139 ret = scene_obj_add(scn, name, id, SCENEOBJT_IMAGE,
140 sizeof(struct scene_obj_img),
141 (struct scene_obj **)&img);
142 if (ret < 0)
Simon Glass1b4a2252023-10-01 19:13:25 -0600143 return log_msg_ret("obj", ret);
Simon Glass0a4d14b2023-01-06 08:52:37 -0600144
145 img->data = data;
146
147 if (imgp)
148 *imgp = img;
149
150 return img->obj.id;
151}
152
153int scene_txt(struct scene *scn, const char *name, uint id, uint str_id,
154 struct scene_obj_txt **txtp)
155{
156 struct scene_obj_txt *txt;
157 int ret;
158
159 ret = scene_obj_add(scn, name, id, SCENEOBJT_TEXT,
160 sizeof(struct scene_obj_txt),
161 (struct scene_obj **)&txt);
162 if (ret < 0)
Simon Glass1b4a2252023-10-01 19:13:25 -0600163 return log_msg_ret("obj", ret);
Simon Glass0a4d14b2023-01-06 08:52:37 -0600164
165 txt->str_id = str_id;
166
167 if (txtp)
168 *txtp = txt;
169
170 return txt->obj.id;
171}
172
173int scene_txt_str(struct scene *scn, const char *name, uint id, uint str_id,
174 const char *str, struct scene_obj_txt **txtp)
175{
176 struct scene_obj_txt *txt;
177 int ret;
178
179 ret = expo_str(scn->expo, name, str_id, str);
180 if (ret < 0)
181 return log_msg_ret("str", ret);
Simon Glass51bde1a2023-10-01 19:13:26 -0600182 if (str_id && ret != str_id)
Simon Glass0a4d14b2023-01-06 08:52:37 -0600183 return log_msg_ret("id", -EEXIST);
Simon Glass51bde1a2023-10-01 19:13:26 -0600184 str_id = ret;
Simon Glass0a4d14b2023-01-06 08:52:37 -0600185
186 ret = scene_obj_add(scn, name, id, SCENEOBJT_TEXT,
187 sizeof(struct scene_obj_txt),
188 (struct scene_obj **)&txt);
189 if (ret < 0)
Simon Glass1b4a2252023-10-01 19:13:25 -0600190 return log_msg_ret("obj", ret);
Simon Glass0a4d14b2023-01-06 08:52:37 -0600191
192 txt->str_id = str_id;
193
194 if (txtp)
195 *txtp = txt;
196
197 return txt->obj.id;
198}
199
200int scene_txt_set_font(struct scene *scn, uint id, const char *font_name,
201 uint font_size)
202{
203 struct scene_obj_txt *txt;
204
205 txt = scene_obj_find(scn, id, SCENEOBJT_TEXT);
206 if (!txt)
207 return log_msg_ret("find", -ENOENT);
208 txt->font_name = font_name;
209 txt->font_size = font_size;
210
211 return 0;
212}
213
214int scene_obj_set_pos(struct scene *scn, uint id, int x, int y)
215{
216 struct scene_obj *obj;
217
218 obj = scene_obj_find(scn, id, SCENEOBJT_NONE);
219 if (!obj)
220 return log_msg_ret("find", -ENOENT);
Simon Glass7b043952023-06-01 10:22:49 -0600221 obj->dim.x = x;
222 obj->dim.y = y;
Simon Glass0a4d14b2023-01-06 08:52:37 -0600223
224 return 0;
225}
226
Simon Glass7a960052023-06-01 10:22:52 -0600227int scene_obj_set_size(struct scene *scn, uint id, int w, int h)
228{
229 struct scene_obj *obj;
230
231 obj = scene_obj_find(scn, id, SCENEOBJT_NONE);
232 if (!obj)
233 return log_msg_ret("find", -ENOENT);
234 obj->dim.w = w;
235 obj->dim.h = h;
236
237 return 0;
238}
239
Simon Glass0a4d14b2023-01-06 08:52:37 -0600240int scene_obj_set_hide(struct scene *scn, uint id, bool hide)
241{
Simon Glass6081b0f2023-06-01 10:22:50 -0600242 int ret;
243
244 ret = scene_obj_flag_clrset(scn, id, SCENEOF_HIDE,
245 hide ? SCENEOF_HIDE : 0);
246 if (ret)
247 return log_msg_ret("flg", ret);
248
249 return 0;
250}
251
252int scene_obj_flag_clrset(struct scene *scn, uint id, uint clr, uint set)
253{
Simon Glass0a4d14b2023-01-06 08:52:37 -0600254 struct scene_obj *obj;
255
256 obj = scene_obj_find(scn, id, SCENEOBJT_NONE);
257 if (!obj)
258 return log_msg_ret("find", -ENOENT);
Simon Glass6081b0f2023-06-01 10:22:50 -0600259 obj->flags &= ~clr;
260 obj->flags |= set;
Simon Glass0a4d14b2023-01-06 08:52:37 -0600261
262 return 0;
263}
264
265int scene_obj_get_hw(struct scene *scn, uint id, int *widthp)
266{
267 struct scene_obj *obj;
268
269 obj = scene_obj_find(scn, id, SCENEOBJT_NONE);
270 if (!obj)
271 return log_msg_ret("find", -ENOENT);
272
273 switch (obj->type) {
274 case SCENEOBJT_NONE:
275 case SCENEOBJT_MENU:
Simon Glass0023d182023-10-01 19:13:34 -0600276 case SCENEOBJT_TEXTLINE:
Simon Glass0a4d14b2023-01-06 08:52:37 -0600277 break;
278 case SCENEOBJT_IMAGE: {
279 struct scene_obj_img *img = (struct scene_obj_img *)obj;
280 ulong width, height;
281 uint bpix;
282
283 video_bmp_get_info(img->data, &width, &height, &bpix);
284 if (widthp)
285 *widthp = width;
286 return height;
287 }
288 case SCENEOBJT_TEXT: {
289 struct scene_obj_txt *txt = (struct scene_obj_txt *)obj;
290 struct expo *exp = scn->expo;
Simon Glass9e1a86d2023-06-01 10:22:51 -0600291 struct vidconsole_bbox bbox;
292 const char *str;
293 int len, ret;
Simon Glass0a4d14b2023-01-06 08:52:37 -0600294
Simon Glass9e1a86d2023-06-01 10:22:51 -0600295 str = expo_get_str(exp, txt->str_id);
296 if (!str)
297 return log_msg_ret("str", -ENOENT);
298 len = strlen(str);
299
300 /* if there is no console, make it up */
301 if (!exp->cons) {
302 if (widthp)
303 *widthp = 8 * len;
304 return 16;
305 }
306
307 ret = vidconsole_measure(scn->expo->cons, txt->font_name,
308 txt->font_size, str, &bbox);
309 if (ret)
310 return log_msg_ret("mea", ret);
Simon Glass0a4d14b2023-01-06 08:52:37 -0600311 if (widthp)
Simon Glass9e1a86d2023-06-01 10:22:51 -0600312 *widthp = bbox.x1;
Simon Glass0a4d14b2023-01-06 08:52:37 -0600313
Simon Glass9e1a86d2023-06-01 10:22:51 -0600314 return bbox.y1;
Simon Glass0a4d14b2023-01-06 08:52:37 -0600315 }
316 }
317
318 return 0;
319}
320
321/**
Simon Glass118a7272023-10-01 19:13:30 -0600322 * scene_render_background() - Render the background for an object
323 *
324 * @obj: Object to render
Simon Glass0023d182023-10-01 19:13:34 -0600325 * @box_only: true to show a box around the object, but keep the normal
326 * background colour inside
Simon Glass118a7272023-10-01 19:13:30 -0600327 */
Simon Glass0023d182023-10-01 19:13:34 -0600328static void scene_render_background(struct scene_obj *obj, bool box_only)
Simon Glass118a7272023-10-01 19:13:30 -0600329{
330 struct expo *exp = obj->scene->expo;
331 const struct expo_theme *theme = &exp->theme;
332 struct vidconsole_bbox bbox, label_bbox;
333 struct udevice *dev = exp->display;
334 struct video_priv *vid_priv;
335 struct udevice *cons = exp->cons;
336 struct vidconsole_colour old;
337 enum colour_idx fore, back;
338 uint inset = theme->menu_inset;
339
340 /* draw a background for the object */
341 if (CONFIG_IS_ENABLED(SYS_WHITE_ON_BLACK)) {
342 fore = VID_BLACK;
343 back = VID_WHITE;
344 } else {
345 fore = VID_LIGHT_GRAY;
346 back = VID_BLACK;
347 }
348
349 /* see if this object wants to render a background */
350 if (scene_obj_calc_bbox(obj, &bbox, &label_bbox))
351 return;
352
353 vidconsole_push_colour(cons, fore, back, &old);
354 vid_priv = dev_get_uclass_priv(dev);
355 video_fill_part(dev, label_bbox.x0 - inset, label_bbox.y0 - inset,
356 label_bbox.x1 + inset, label_bbox.y1 + inset,
357 vid_priv->colour_fg);
358 vidconsole_pop_colour(cons, &old);
Simon Glass0023d182023-10-01 19:13:34 -0600359 if (box_only) {
360 video_fill_part(dev, label_bbox.x0, label_bbox.y0,
361 label_bbox.x1, label_bbox.y1,
362 vid_priv->colour_bg);
363 }
Simon Glass118a7272023-10-01 19:13:30 -0600364}
365
366/**
Simon Glass0a4d14b2023-01-06 08:52:37 -0600367 * scene_obj_render() - Render an object
368 *
369 */
370static int scene_obj_render(struct scene_obj *obj, bool text_mode)
371{
372 struct scene *scn = obj->scene;
373 struct expo *exp = scn->expo;
Simon Glass86f1ac52023-06-01 10:23:00 -0600374 const struct expo_theme *theme = &exp->theme;
Simon Glass67e2af12023-06-01 10:22:34 -0600375 struct udevice *dev = exp->display;
376 struct udevice *cons = text_mode ? NULL : exp->cons;
Simon Glass0a4d14b2023-01-06 08:52:37 -0600377 int x, y, ret;
378
Simon Glass7b043952023-06-01 10:22:49 -0600379 x = obj->dim.x;
380 y = obj->dim.y;
Simon Glass0a4d14b2023-01-06 08:52:37 -0600381
382 switch (obj->type) {
383 case SCENEOBJT_NONE:
384 break;
385 case SCENEOBJT_IMAGE: {
386 struct scene_obj_img *img = (struct scene_obj_img *)obj;
387
388 if (!cons)
389 return -ENOTSUPP;
390 ret = video_bmp_display(dev, map_to_sysmem(img->data), x, y,
391 true);
392 if (ret < 0)
393 return log_msg_ret("img", ret);
394 break;
395 }
396 case SCENEOBJT_TEXT: {
397 struct scene_obj_txt *txt = (struct scene_obj_txt *)obj;
398 const char *str;
399
400 if (!cons)
401 return -ENOTSUPP;
402
403 if (txt->font_name || txt->font_size) {
404 ret = vidconsole_select_font(cons,
405 txt->font_name,
406 txt->font_size);
407 } else {
408 ret = vidconsole_select_font(cons, NULL, 0);
409 }
410 if (ret && ret != -ENOSYS)
411 return log_msg_ret("font", ret);
Simon Glass0a4d14b2023-01-06 08:52:37 -0600412 str = expo_get_str(exp, txt->str_id);
Simon Glass01922ec2023-06-01 10:22:57 -0600413 if (str) {
414 struct video_priv *vid_priv;
415 struct vidconsole_colour old;
416 enum colour_idx fore, back;
417
418 if (CONFIG_IS_ENABLED(SYS_WHITE_ON_BLACK)) {
419 fore = VID_BLACK;
420 back = VID_WHITE;
421 } else {
422 fore = VID_LIGHT_GRAY;
423 back = VID_BLACK;
424 }
425
426 vid_priv = dev_get_uclass_priv(dev);
427 if (obj->flags & SCENEOF_POINT) {
428 vidconsole_push_colour(cons, fore, back, &old);
Simon Glass86f1ac52023-06-01 10:23:00 -0600429 video_fill_part(dev, x - theme->menu_inset, y,
430 x + obj->dim.w,
431 y + obj->dim.h,
Simon Glass01922ec2023-06-01 10:22:57 -0600432 vid_priv->colour_bg);
433 }
434 vidconsole_set_cursor_pos(cons, x, y);
Simon Glass0a4d14b2023-01-06 08:52:37 -0600435 vidconsole_put_string(cons, str);
Simon Glass01922ec2023-06-01 10:22:57 -0600436 if (obj->flags & SCENEOF_POINT)
437 vidconsole_pop_colour(cons, &old);
438 }
Simon Glass0a4d14b2023-01-06 08:52:37 -0600439 break;
440 }
441 case SCENEOBJT_MENU: {
442 struct scene_obj_menu *menu = (struct scene_obj_menu *)obj;
Simon Glass01922ec2023-06-01 10:22:57 -0600443
444 if (exp->popup && (obj->flags & SCENEOF_OPEN)) {
445 if (!cons)
446 return -ENOTSUPP;
447
448 /* draw a background behind the menu items */
Simon Glass0023d182023-10-01 19:13:34 -0600449 scene_render_background(obj, false);
Simon Glass01922ec2023-06-01 10:22:57 -0600450 }
Simon Glass0a4d14b2023-01-06 08:52:37 -0600451 /*
452 * With a vidconsole, the text and item pointer are rendered as
453 * normal objects so we don't need to do anything here. The menu
454 * simply controls where they are positioned.
455 */
456 if (cons)
457 return -ENOTSUPP;
458
459 ret = scene_menu_display(menu);
460 if (ret < 0)
461 return log_msg_ret("img", ret);
462
463 break;
464 }
Simon Glass0023d182023-10-01 19:13:34 -0600465 case SCENEOBJT_TEXTLINE:
466 if (obj->flags & SCENEOF_OPEN)
467 scene_render_background(obj, true);
468 break;
Simon Glass0a4d14b2023-01-06 08:52:37 -0600469 }
470
471 return 0;
472}
473
474int scene_arrange(struct scene *scn)
475{
476 struct scene_obj *obj;
477 int ret;
478
479 list_for_each_entry(obj, &scn->obj_head, sibling) {
Simon Glassb7a64532023-10-01 19:13:24 -0600480 switch (obj->type) {
481 case SCENEOBJT_NONE:
482 case SCENEOBJT_IMAGE:
483 case SCENEOBJT_TEXT:
484 break;
485 case SCENEOBJT_MENU: {
Simon Glass0a4d14b2023-01-06 08:52:37 -0600486 struct scene_obj_menu *menu;
487
488 menu = (struct scene_obj_menu *)obj,
489 ret = scene_menu_arrange(scn, menu);
490 if (ret)
491 return log_msg_ret("arr", ret);
Simon Glassb7a64532023-10-01 19:13:24 -0600492 break;
493 }
Simon Glass0023d182023-10-01 19:13:34 -0600494 case SCENEOBJT_TEXTLINE: {
495 struct scene_obj_textline *tline;
496
497 tline = (struct scene_obj_textline *)obj,
498 ret = scene_textline_arrange(scn, tline);
499 if (ret)
500 return log_msg_ret("arr", ret);
501 break;
502 }
Simon Glass0a4d14b2023-01-06 08:52:37 -0600503 }
504 }
505
506 return 0;
507}
508
Simon Glass12f57732023-06-01 10:22:58 -0600509int scene_render_deps(struct scene *scn, uint id)
510{
511 struct scene_obj *obj;
512 int ret;
513
514 if (!id)
515 return 0;
516 obj = scene_obj_find(scn, id, SCENEOBJT_NONE);
517 if (!obj)
518 return log_msg_ret("obj", -ENOENT);
519
520 if (!(obj->flags & SCENEOF_HIDE)) {
521 ret = scene_obj_render(obj, false);
522 if (ret && ret != -ENOTSUPP)
523 return log_msg_ret("ren", ret);
524
Simon Glassb7a64532023-10-01 19:13:24 -0600525 switch (obj->type) {
526 case SCENEOBJT_NONE:
527 case SCENEOBJT_IMAGE:
528 case SCENEOBJT_TEXT:
529 break;
530 case SCENEOBJT_MENU:
Simon Glass12f57732023-06-01 10:22:58 -0600531 scene_menu_render_deps(scn,
532 (struct scene_obj_menu *)obj);
Simon Glassb7a64532023-10-01 19:13:24 -0600533 break;
Simon Glass0023d182023-10-01 19:13:34 -0600534 case SCENEOBJT_TEXTLINE:
535 scene_textline_render_deps(scn,
536 (struct scene_obj_textline *)obj);
537 break;
Simon Glassb7a64532023-10-01 19:13:24 -0600538 }
Simon Glass12f57732023-06-01 10:22:58 -0600539 }
540
541 return 0;
542}
543
Simon Glass0a4d14b2023-01-06 08:52:37 -0600544int scene_render(struct scene *scn)
545{
546 struct expo *exp = scn->expo;
547 struct scene_obj *obj;
548 int ret;
549
550 list_for_each_entry(obj, &scn->obj_head, sibling) {
Simon Glass6081b0f2023-06-01 10:22:50 -0600551 if (!(obj->flags & SCENEOF_HIDE)) {
Simon Glass0a4d14b2023-01-06 08:52:37 -0600552 ret = scene_obj_render(obj, exp->text_mode);
553 if (ret && ret != -ENOTSUPP)
554 return log_msg_ret("ren", ret);
555 }
556 }
557
Simon Glass12f57732023-06-01 10:22:58 -0600558 /* render any highlighted object on top of the others */
559 if (scn->highlight_id && !exp->text_mode) {
560 ret = scene_render_deps(scn, scn->highlight_id);
561 if (ret && ret != -ENOTSUPP)
562 return log_msg_ret("dep", ret);
563 }
564
Simon Glass0a4d14b2023-01-06 08:52:37 -0600565 return 0;
566}
567
Simon Glassf0e1e8c2023-06-01 10:22:59 -0600568/**
569 * send_key_obj() - Handle a keypress for moving between objects
570 *
571 * @scn: Scene to receive the key
572 * @key: Key to send (KEYCODE_UP)
573 * @event: Returns resulting event from this keypress
574 * Returns: 0 if OK, -ve on error
575 */
576static void send_key_obj(struct scene *scn, struct scene_obj *obj, int key,
577 struct expo_action *event)
578{
579 switch (key) {
580 case BKEY_UP:
581 while (obj != list_first_entry(&scn->obj_head, struct scene_obj,
582 sibling)) {
583 obj = list_entry(obj->sibling.prev,
584 struct scene_obj, sibling);
Simon Glass193bfea2023-10-01 19:13:27 -0600585 if (scene_obj_can_highlight(obj)) {
Simon Glassf0e1e8c2023-06-01 10:22:59 -0600586 event->type = EXPOACT_POINT_OBJ;
587 event->select.id = obj->id;
588 log_debug("up to obj %d\n", event->select.id);
589 break;
590 }
591 }
592 break;
593 case BKEY_DOWN:
594 while (!list_is_last(&obj->sibling, &scn->obj_head)) {
595 obj = list_entry(obj->sibling.next, struct scene_obj,
596 sibling);
Simon Glass193bfea2023-10-01 19:13:27 -0600597 if (scene_obj_can_highlight(obj)) {
Simon Glassf0e1e8c2023-06-01 10:22:59 -0600598 event->type = EXPOACT_POINT_OBJ;
599 event->select.id = obj->id;
600 log_debug("down to obj %d\n", event->select.id);
601 break;
602 }
603 }
604 break;
605 case BKEY_SELECT:
Simon Glass193bfea2023-10-01 19:13:27 -0600606 if (scene_obj_can_highlight(obj)) {
Simon Glassf0e1e8c2023-06-01 10:22:59 -0600607 event->type = EXPOACT_OPEN;
608 event->select.id = obj->id;
609 log_debug("open obj %d\n", event->select.id);
610 }
611 break;
612 case BKEY_QUIT:
613 event->type = EXPOACT_QUIT;
614 log_debug("obj quit\n");
615 break;
616 }
617}
618
Simon Glass0a4d14b2023-01-06 08:52:37 -0600619int scene_send_key(struct scene *scn, int key, struct expo_action *event)
620{
621 struct scene_obj *obj;
622 int ret;
623
Simon Glassf0e1e8c2023-06-01 10:22:59 -0600624 event->type = EXPOACT_NONE;
625
626 /*
627 * In 'popup' mode, arrow keys move betwen objects, unless a menu is
628 * opened
629 */
630 if (scn->expo->popup) {
631 obj = NULL;
632 if (scn->highlight_id) {
633 obj = scene_obj_find(scn, scn->highlight_id,
634 SCENEOBJT_NONE);
635 }
636 if (!obj)
637 return 0;
638
639 if (!(obj->flags & SCENEOF_OPEN)) {
640 send_key_obj(scn, obj, key, event);
641 return 0;
642 }
643
Simon Glassb7a64532023-10-01 19:13:24 -0600644 switch (obj->type) {
645 case SCENEOBJT_NONE:
646 case SCENEOBJT_IMAGE:
647 case SCENEOBJT_TEXT:
648 break;
649 case SCENEOBJT_MENU: {
650 struct scene_obj_menu *menu;
651
652 menu = (struct scene_obj_menu *)obj,
653 ret = scene_menu_send_key(scn, menu, key, event);
654 if (ret)
655 return log_msg_ret("key", ret);
656 break;
657 }
Simon Glass0023d182023-10-01 19:13:34 -0600658 case SCENEOBJT_TEXTLINE: {
659 struct scene_obj_textline *tline;
660
661 tline = (struct scene_obj_textline *)obj,
662 ret = scene_textline_send_key(scn, tline, key, event);
663 if (ret)
664 return log_msg_ret("key", ret);
665 break;
666 }
Simon Glassb7a64532023-10-01 19:13:24 -0600667 }
Simon Glassf0e1e8c2023-06-01 10:22:59 -0600668 return 0;
669 }
670
Simon Glass0a4d14b2023-01-06 08:52:37 -0600671 list_for_each_entry(obj, &scn->obj_head, sibling) {
672 if (obj->type == SCENEOBJT_MENU) {
673 struct scene_obj_menu *menu;
674
675 menu = (struct scene_obj_menu *)obj,
676 ret = scene_menu_send_key(scn, menu, key, event);
677 if (ret)
678 return log_msg_ret("key", ret);
Simon Glass0a4d14b2023-01-06 08:52:37 -0600679 break;
680 }
681 }
682
683 return 0;
684}
Simon Glass7a960052023-06-01 10:22:52 -0600685
Simon Glassf0994692023-10-01 19:13:29 -0600686int scene_obj_calc_bbox(struct scene_obj *obj, struct vidconsole_bbox *bbox,
687 struct vidconsole_bbox *label_bbox)
688{
689 switch (obj->type) {
690 case SCENEOBJT_NONE:
691 case SCENEOBJT_IMAGE:
692 case SCENEOBJT_TEXT:
693 return -ENOSYS;
694 case SCENEOBJT_MENU: {
695 struct scene_obj_menu *menu = (struct scene_obj_menu *)obj;
696
697 scene_menu_calc_bbox(menu, bbox, label_bbox);
698 break;
699 }
Simon Glass0023d182023-10-01 19:13:34 -0600700 case SCENEOBJT_TEXTLINE: {
701 struct scene_obj_textline *tline;
702
703 tline = (struct scene_obj_textline *)obj;
704 scene_textline_calc_bbox(tline, bbox, label_bbox);
705 break;
706 }
Simon Glassf0994692023-10-01 19:13:29 -0600707 }
708
709 return 0;
710}
711
Simon Glass7a960052023-06-01 10:22:52 -0600712int scene_calc_dims(struct scene *scn, bool do_menus)
713{
714 struct scene_obj *obj;
715 int ret;
716
717 list_for_each_entry(obj, &scn->obj_head, sibling) {
718 switch (obj->type) {
719 case SCENEOBJT_NONE:
720 case SCENEOBJT_TEXT:
721 case SCENEOBJT_IMAGE: {
722 int width;
723
724 if (!do_menus) {
725 ret = scene_obj_get_hw(scn, obj->id, &width);
726 if (ret < 0)
727 return log_msg_ret("get", ret);
728 obj->dim.w = width;
729 obj->dim.h = ret;
730 }
731 break;
732 }
733 case SCENEOBJT_MENU: {
734 struct scene_obj_menu *menu;
735
736 if (do_menus) {
737 menu = (struct scene_obj_menu *)obj;
738
739 ret = scene_menu_calc_dims(menu);
740 if (ret)
741 return log_msg_ret("men", ret);
742 }
743 break;
744 }
Simon Glass0023d182023-10-01 19:13:34 -0600745 case SCENEOBJT_TEXTLINE: {
746 struct scene_obj_textline *tline;
747
748 tline = (struct scene_obj_textline *)obj;
749 ret = scene_textline_calc_dims(tline);
750 if (ret)
751 return log_msg_ret("men", ret);
752
753 break;
754 }
Simon Glass7a960052023-06-01 10:22:52 -0600755 }
756 }
757
758 return 0;
759}
Simon Glassc999e172023-06-01 10:22:53 -0600760
761int scene_apply_theme(struct scene *scn, struct expo_theme *theme)
762{
763 struct scene_obj *obj;
764 int ret;
765
766 /* Avoid error-checking optional items */
767 scene_txt_set_font(scn, scn->title_id, NULL, theme->font_size);
768
769 list_for_each_entry(obj, &scn->obj_head, sibling) {
770 switch (obj->type) {
771 case SCENEOBJT_NONE:
772 case SCENEOBJT_IMAGE:
773 case SCENEOBJT_MENU:
Simon Glass0023d182023-10-01 19:13:34 -0600774 case SCENEOBJT_TEXTLINE:
Simon Glassc999e172023-06-01 10:22:53 -0600775 break;
776 case SCENEOBJT_TEXT:
777 scene_txt_set_font(scn, obj->id, NULL,
778 theme->font_size);
779 break;
780 }
781 }
782
783 ret = scene_arrange(scn);
784 if (ret)
785 return log_msg_ret("arr", ret);
786
787 return 0;
788}
Simon Glass01922ec2023-06-01 10:22:57 -0600789
790void scene_set_highlight_id(struct scene *scn, uint id)
791{
792 scn->highlight_id = id;
793}
794
795void scene_highlight_first(struct scene *scn)
796{
797 struct scene_obj *obj;
798
799 list_for_each_entry(obj, &scn->obj_head, sibling) {
Simon Glass193bfea2023-10-01 19:13:27 -0600800 if (scene_obj_can_highlight(obj)) {
Simon Glass01922ec2023-06-01 10:22:57 -0600801 scene_set_highlight_id(scn, obj->id);
802 return;
Simon Glass01922ec2023-06-01 10:22:57 -0600803 }
804 }
805}
806
Simon Glassf6a943a2023-10-01 19:13:33 -0600807static int scene_obj_open(struct scene *scn, struct scene_obj *obj)
808{
809 int ret;
810
811 switch (obj->type) {
812 case SCENEOBJT_NONE:
813 case SCENEOBJT_IMAGE:
814 case SCENEOBJT_MENU:
815 case SCENEOBJT_TEXT:
816 break;
817 case SCENEOBJT_TEXTLINE:
818 ret = scene_textline_open(scn,
819 (struct scene_obj_textline *)obj);
820 if (ret)
821 return log_msg_ret("op", ret);
822 break;
823 }
824
825 return 0;
826}
827
Simon Glass01922ec2023-06-01 10:22:57 -0600828int scene_set_open(struct scene *scn, uint id, bool open)
829{
Simon Glassf6a943a2023-10-01 19:13:33 -0600830 struct scene_obj *obj;
Simon Glass01922ec2023-06-01 10:22:57 -0600831 int ret;
832
Simon Glassf6a943a2023-10-01 19:13:33 -0600833 obj = scene_obj_find(scn, id, SCENEOBJT_NONE);
834 if (!obj)
835 return log_msg_ret("find", -ENOENT);
836
837 if (open) {
838 ret = scene_obj_open(scn, obj);
839 if (ret)
840 return log_msg_ret("op", ret);
841 }
842
Simon Glass01922ec2023-06-01 10:22:57 -0600843 ret = scene_obj_flag_clrset(scn, id, SCENEOF_OPEN,
844 open ? SCENEOF_OPEN : 0);
845 if (ret)
846 return log_msg_ret("flg", ret);
847
848 return 0;
849}
Simon Glasse90acd82023-08-14 16:40:23 -0600850
851int scene_iter_objs(struct scene *scn, expo_scene_obj_iterator iter,
852 void *priv)
853{
854 struct scene_obj *obj;
855
856 list_for_each_entry(obj, &scn->obj_head, sibling) {
857 int ret;
858
859 ret = iter(obj, priv);
860 if (ret)
861 return log_msg_ret("itr", ret);
862 }
863
864 return 0;
865}
Simon Glassf0994692023-10-01 19:13:29 -0600866
867int scene_bbox_union(struct scene *scn, uint id, int inset,
868 struct vidconsole_bbox *bbox)
869{
870 struct scene_obj *obj;
871
872 if (!id)
873 return 0;
874 obj = scene_obj_find(scn, id, SCENEOBJT_NONE);
875 if (!obj)
876 return log_msg_ret("obj", -ENOENT);
877 if (bbox->valid) {
878 bbox->x0 = min(bbox->x0, obj->dim.x - inset);
879 bbox->y0 = min(bbox->y0, obj->dim.y);
880 bbox->x1 = max(bbox->x1, obj->dim.x + obj->dim.w + inset);
881 bbox->y1 = max(bbox->y1, obj->dim.y + obj->dim.h);
882 } else {
883 bbox->x0 = obj->dim.x - inset;
884 bbox->y0 = obj->dim.y;
885 bbox->x1 = obj->dim.x + obj->dim.w + inset;
886 bbox->y1 = obj->dim.y + obj->dim.h;
887 bbox->valid = true;
888 }
889
890 return 0;
891}