1 // SPDX-License-Identifier: GPL-2.0+
3 * Implementation of a scene, a collection of text/image/menu items in an expo
5 * Copyright 2022 Google LLC
9 #define LOG_CATEGORY LOGC_EXPO
17 #include <video_console.h>
18 #include <linux/input.h>
19 #include "scene_internal.h"
21 int scene_new(struct expo *exp, const char *name, uint id, struct scene **scnp)
25 scn = calloc(1, sizeof(struct scene));
27 return log_msg_ret("expo", -ENOMEM);
28 scn->name = strdup(name);
31 return log_msg_ret("name", -ENOMEM);
35 if (!abuf_realloc(&scn->buf, EXPO_MAX_CHARS + 1)) {
38 return log_msg_ret("buf", -ENOMEM);
40 abuf_init(&scn->entry_save);
42 INIT_LIST_HEAD(&scn->obj_head);
43 scn->id = resolve_id(exp, id);
45 list_add_tail(&scn->sibling, &exp->scene_head);
52 void scene_obj_destroy(struct scene_obj *obj)
54 if (obj->type == SCENEOBJT_MENU)
55 scene_menu_destroy((struct scene_obj_menu *)obj);
60 void scene_destroy(struct scene *scn)
62 struct scene_obj *obj, *next;
64 list_for_each_entry_safe(obj, next, &scn->obj_head, sibling)
65 scene_obj_destroy(obj);
67 abuf_uninit(&scn->entry_save);
68 abuf_uninit(&scn->buf);
73 int scene_title_set(struct scene *scn, uint id)
80 int scene_obj_count(struct scene *scn)
82 struct scene_obj *obj;
85 list_for_each_entry(obj, &scn->obj_head, sibling)
91 void *scene_obj_find(const struct scene *scn, uint id, enum scene_obj_t type)
93 struct scene_obj *obj;
95 list_for_each_entry(obj, &scn->obj_head, sibling) {
97 (type == SCENEOBJT_NONE || obj->type == type))
104 void *scene_obj_find_by_name(struct scene *scn, const char *name)
106 struct scene_obj *obj;
108 list_for_each_entry(obj, &scn->obj_head, sibling) {
109 if (!strcmp(name, obj->name))
116 int scene_obj_add(struct scene *scn, const char *name, uint id,
117 enum scene_obj_t type, uint size, struct scene_obj **objp)
119 struct scene_obj *obj;
121 obj = calloc(1, size);
123 return log_msg_ret("obj", -ENOMEM);
124 obj->name = strdup(name);
127 return log_msg_ret("name", -ENOMEM);
130 obj->id = resolve_id(scn->expo, id);
133 list_add_tail(&obj->sibling, &scn->obj_head);
139 int scene_img(struct scene *scn, const char *name, uint id, char *data,
140 struct scene_obj_img **imgp)
142 struct scene_obj_img *img;
145 ret = scene_obj_add(scn, name, id, SCENEOBJT_IMAGE,
146 sizeof(struct scene_obj_img),
147 (struct scene_obj **)&img);
149 return log_msg_ret("obj", ret);
159 int scene_txt(struct scene *scn, const char *name, uint id, uint str_id,
160 struct scene_obj_txt **txtp)
162 struct scene_obj_txt *txt;
165 ret = scene_obj_add(scn, name, id, SCENEOBJT_TEXT,
166 sizeof(struct scene_obj_txt),
167 (struct scene_obj **)&txt);
169 return log_msg_ret("obj", ret);
171 txt->str_id = str_id;
179 int scene_txt_str(struct scene *scn, const char *name, uint id, uint str_id,
180 const char *str, struct scene_obj_txt **txtp)
182 struct scene_obj_txt *txt;
185 ret = expo_str(scn->expo, name, str_id, str);
187 return log_msg_ret("str", ret);
188 if (str_id && ret != str_id)
189 return log_msg_ret("id", -EEXIST);
192 ret = scene_obj_add(scn, name, id, SCENEOBJT_TEXT,
193 sizeof(struct scene_obj_txt),
194 (struct scene_obj **)&txt);
196 return log_msg_ret("obj", ret);
198 txt->str_id = str_id;
206 int scene_txt_set_font(struct scene *scn, uint id, const char *font_name,
209 struct scene_obj_txt *txt;
211 txt = scene_obj_find(scn, id, SCENEOBJT_TEXT);
213 return log_msg_ret("find", -ENOENT);
214 txt->font_name = font_name;
215 txt->font_size = font_size;
220 int scene_obj_set_pos(struct scene *scn, uint id, int x, int y)
222 struct scene_obj *obj;
224 obj = scene_obj_find(scn, id, SCENEOBJT_NONE);
226 return log_msg_ret("find", -ENOENT);
233 int scene_obj_set_size(struct scene *scn, uint id, int w, int h)
235 struct scene_obj *obj;
237 obj = scene_obj_find(scn, id, SCENEOBJT_NONE);
239 return log_msg_ret("find", -ENOENT);
246 int scene_obj_set_hide(struct scene *scn, uint id, bool hide)
250 ret = scene_obj_flag_clrset(scn, id, SCENEOF_HIDE,
251 hide ? SCENEOF_HIDE : 0);
253 return log_msg_ret("flg", ret);
258 int scene_obj_flag_clrset(struct scene *scn, uint id, uint clr, uint set)
260 struct scene_obj *obj;
262 obj = scene_obj_find(scn, id, SCENEOBJT_NONE);
264 return log_msg_ret("find", -ENOENT);
271 int scene_obj_get_hw(struct scene *scn, uint id, int *widthp)
273 struct scene_obj *obj;
275 obj = scene_obj_find(scn, id, SCENEOBJT_NONE);
277 return log_msg_ret("find", -ENOENT);
282 case SCENEOBJT_TEXTLINE:
284 case SCENEOBJT_IMAGE: {
285 struct scene_obj_img *img = (struct scene_obj_img *)obj;
289 video_bmp_get_info(img->data, &width, &height, &bpix);
294 case SCENEOBJT_TEXT: {
295 struct scene_obj_txt *txt = (struct scene_obj_txt *)obj;
296 struct expo *exp = scn->expo;
297 struct vidconsole_bbox bbox;
301 str = expo_get_str(exp, txt->str_id);
303 return log_msg_ret("str", -ENOENT);
306 /* if there is no console, make it up */
313 ret = vidconsole_measure(scn->expo->cons, txt->font_name,
314 txt->font_size, str, &bbox);
316 return log_msg_ret("mea", ret);
328 * scene_render_background() - Render the background for an object
330 * @obj: Object to render
331 * @box_only: true to show a box around the object, but keep the normal
332 * background colour inside
334 static void scene_render_background(struct scene_obj *obj, bool box_only)
336 struct expo *exp = obj->scene->expo;
337 const struct expo_theme *theme = &exp->theme;
338 struct vidconsole_bbox bbox, label_bbox;
339 struct udevice *dev = exp->display;
340 struct video_priv *vid_priv;
341 struct udevice *cons = exp->cons;
342 struct vidconsole_colour old;
343 enum colour_idx fore, back;
344 uint inset = theme->menu_inset;
346 /* draw a background for the object */
347 if (CONFIG_IS_ENABLED(SYS_WHITE_ON_BLACK)) {
351 fore = VID_LIGHT_GRAY;
355 /* see if this object wants to render a background */
356 if (scene_obj_calc_bbox(obj, &bbox, &label_bbox))
359 vidconsole_push_colour(cons, fore, back, &old);
360 vid_priv = dev_get_uclass_priv(dev);
361 video_fill_part(dev, label_bbox.x0 - inset, label_bbox.y0 - inset,
362 label_bbox.x1 + inset, label_bbox.y1 + inset,
363 vid_priv->colour_fg);
364 vidconsole_pop_colour(cons, &old);
366 video_fill_part(dev, label_bbox.x0, label_bbox.y0,
367 label_bbox.x1, label_bbox.y1,
368 vid_priv->colour_bg);
373 * scene_obj_render() - Render an object
376 static int scene_obj_render(struct scene_obj *obj, bool text_mode)
378 struct scene *scn = obj->scene;
379 struct expo *exp = scn->expo;
380 const struct expo_theme *theme = &exp->theme;
381 struct udevice *dev = exp->display;
382 struct udevice *cons = text_mode ? NULL : exp->cons;
391 case SCENEOBJT_IMAGE: {
392 struct scene_obj_img *img = (struct scene_obj_img *)obj;
396 ret = video_bmp_display(dev, map_to_sysmem(img->data), x, y,
399 return log_msg_ret("img", ret);
402 case SCENEOBJT_TEXT: {
403 struct scene_obj_txt *txt = (struct scene_obj_txt *)obj;
409 if (txt->font_name || txt->font_size) {
410 ret = vidconsole_select_font(cons,
414 ret = vidconsole_select_font(cons, NULL, 0);
416 if (ret && ret != -ENOSYS)
417 return log_msg_ret("font", ret);
418 str = expo_get_str(exp, txt->str_id);
420 struct video_priv *vid_priv;
421 struct vidconsole_colour old;
422 enum colour_idx fore, back;
424 if (CONFIG_IS_ENABLED(SYS_WHITE_ON_BLACK)) {
428 fore = VID_LIGHT_GRAY;
432 vid_priv = dev_get_uclass_priv(dev);
433 if (obj->flags & SCENEOF_POINT) {
434 vidconsole_push_colour(cons, fore, back, &old);
435 video_fill_part(dev, x - theme->menu_inset, y,
438 vid_priv->colour_bg);
440 vidconsole_set_cursor_pos(cons, x, y);
441 vidconsole_put_string(cons, str);
442 if (obj->flags & SCENEOF_POINT)
443 vidconsole_pop_colour(cons, &old);
447 case SCENEOBJT_MENU: {
448 struct scene_obj_menu *menu = (struct scene_obj_menu *)obj;
450 if (exp->popup && (obj->flags & SCENEOF_OPEN)) {
454 /* draw a background behind the menu items */
455 scene_render_background(obj, false);
458 * With a vidconsole, the text and item pointer are rendered as
459 * normal objects so we don't need to do anything here. The menu
460 * simply controls where they are positioned.
465 ret = scene_menu_display(menu);
467 return log_msg_ret("img", ret);
471 case SCENEOBJT_TEXTLINE:
472 if (obj->flags & SCENEOF_OPEN)
473 scene_render_background(obj, true);
480 int scene_arrange(struct scene *scn)
482 struct scene_obj *obj;
485 list_for_each_entry(obj, &scn->obj_head, sibling) {
488 case SCENEOBJT_IMAGE:
491 case SCENEOBJT_MENU: {
492 struct scene_obj_menu *menu;
494 menu = (struct scene_obj_menu *)obj,
495 ret = scene_menu_arrange(scn, menu);
497 return log_msg_ret("arr", ret);
500 case SCENEOBJT_TEXTLINE: {
501 struct scene_obj_textline *tline;
503 tline = (struct scene_obj_textline *)obj,
504 ret = scene_textline_arrange(scn, tline);
506 return log_msg_ret("arr", ret);
515 int scene_render_deps(struct scene *scn, uint id)
517 struct scene_obj *obj;
522 obj = scene_obj_find(scn, id, SCENEOBJT_NONE);
524 return log_msg_ret("obj", -ENOENT);
526 if (!(obj->flags & SCENEOF_HIDE)) {
527 ret = scene_obj_render(obj, false);
528 if (ret && ret != -ENOTSUPP)
529 return log_msg_ret("ren", ret);
533 case SCENEOBJT_IMAGE:
537 scene_menu_render_deps(scn,
538 (struct scene_obj_menu *)obj);
540 case SCENEOBJT_TEXTLINE:
541 scene_textline_render_deps(scn,
542 (struct scene_obj_textline *)obj);
550 int scene_render(struct scene *scn)
552 struct expo *exp = scn->expo;
553 struct scene_obj *obj;
556 list_for_each_entry(obj, &scn->obj_head, sibling) {
557 if (!(obj->flags & SCENEOF_HIDE)) {
558 ret = scene_obj_render(obj, exp->text_mode);
559 if (ret && ret != -ENOTSUPP)
560 return log_msg_ret("ren", ret);
564 /* render any highlighted object on top of the others */
565 if (scn->highlight_id && !exp->text_mode) {
566 ret = scene_render_deps(scn, scn->highlight_id);
567 if (ret && ret != -ENOTSUPP)
568 return log_msg_ret("dep", ret);
575 * send_key_obj() - Handle a keypress for moving between objects
577 * @scn: Scene to receive the key
578 * @key: Key to send (KEYCODE_UP)
579 * @event: Returns resulting event from this keypress
580 * Returns: 0 if OK, -ve on error
582 static void send_key_obj(struct scene *scn, struct scene_obj *obj, int key,
583 struct expo_action *event)
587 while (obj != list_first_entry(&scn->obj_head, struct scene_obj,
589 obj = list_entry(obj->sibling.prev,
590 struct scene_obj, sibling);
591 if (scene_obj_can_highlight(obj)) {
592 event->type = EXPOACT_POINT_OBJ;
593 event->select.id = obj->id;
594 log_debug("up to obj %d\n", event->select.id);
600 while (!list_is_last(&obj->sibling, &scn->obj_head)) {
601 obj = list_entry(obj->sibling.next, struct scene_obj,
603 if (scene_obj_can_highlight(obj)) {
604 event->type = EXPOACT_POINT_OBJ;
605 event->select.id = obj->id;
606 log_debug("down to obj %d\n", event->select.id);
612 if (scene_obj_can_highlight(obj)) {
613 event->type = EXPOACT_OPEN;
614 event->select.id = obj->id;
615 log_debug("open obj %d\n", event->select.id);
619 event->type = EXPOACT_QUIT;
620 log_debug("obj quit\n");
625 int scene_send_key(struct scene *scn, int key, struct expo_action *event)
627 struct scene_obj *obj;
630 event->type = EXPOACT_NONE;
633 * In 'popup' mode, arrow keys move betwen objects, unless a menu is
636 if (scn->expo->popup) {
638 if (scn->highlight_id) {
639 obj = scene_obj_find(scn, scn->highlight_id,
645 if (!(obj->flags & SCENEOF_OPEN)) {
646 send_key_obj(scn, obj, key, event);
652 case SCENEOBJT_IMAGE:
655 case SCENEOBJT_MENU: {
656 struct scene_obj_menu *menu;
658 menu = (struct scene_obj_menu *)obj,
659 ret = scene_menu_send_key(scn, menu, key, event);
661 return log_msg_ret("key", ret);
664 case SCENEOBJT_TEXTLINE: {
665 struct scene_obj_textline *tline;
667 tline = (struct scene_obj_textline *)obj,
668 ret = scene_textline_send_key(scn, tline, key, event);
670 return log_msg_ret("key", ret);
677 list_for_each_entry(obj, &scn->obj_head, sibling) {
678 if (obj->type == SCENEOBJT_MENU) {
679 struct scene_obj_menu *menu;
681 menu = (struct scene_obj_menu *)obj,
682 ret = scene_menu_send_key(scn, menu, key, event);
684 return log_msg_ret("key", ret);
692 int scene_obj_calc_bbox(struct scene_obj *obj, struct vidconsole_bbox *bbox,
693 struct vidconsole_bbox *label_bbox)
697 case SCENEOBJT_IMAGE:
700 case SCENEOBJT_MENU: {
701 struct scene_obj_menu *menu = (struct scene_obj_menu *)obj;
703 scene_menu_calc_bbox(menu, bbox, label_bbox);
706 case SCENEOBJT_TEXTLINE: {
707 struct scene_obj_textline *tline;
709 tline = (struct scene_obj_textline *)obj;
710 scene_textline_calc_bbox(tline, bbox, label_bbox);
718 int scene_calc_dims(struct scene *scn, bool do_menus)
720 struct scene_obj *obj;
723 list_for_each_entry(obj, &scn->obj_head, sibling) {
727 case SCENEOBJT_IMAGE: {
731 ret = scene_obj_get_hw(scn, obj->id, &width);
733 return log_msg_ret("get", ret);
739 case SCENEOBJT_MENU: {
740 struct scene_obj_menu *menu;
743 menu = (struct scene_obj_menu *)obj;
745 ret = scene_menu_calc_dims(menu);
747 return log_msg_ret("men", ret);
751 case SCENEOBJT_TEXTLINE: {
752 struct scene_obj_textline *tline;
754 tline = (struct scene_obj_textline *)obj;
755 ret = scene_textline_calc_dims(tline);
757 return log_msg_ret("men", ret);
767 int scene_apply_theme(struct scene *scn, struct expo_theme *theme)
769 struct scene_obj *obj;
772 /* Avoid error-checking optional items */
773 scene_txt_set_font(scn, scn->title_id, NULL, theme->font_size);
775 list_for_each_entry(obj, &scn->obj_head, sibling) {
778 case SCENEOBJT_IMAGE:
780 case SCENEOBJT_TEXTLINE:
783 scene_txt_set_font(scn, obj->id, NULL,
789 ret = scene_arrange(scn);
791 return log_msg_ret("arr", ret);
796 void scene_set_highlight_id(struct scene *scn, uint id)
798 scn->highlight_id = id;
801 void scene_highlight_first(struct scene *scn)
803 struct scene_obj *obj;
805 list_for_each_entry(obj, &scn->obj_head, sibling) {
806 if (scene_obj_can_highlight(obj)) {
807 scene_set_highlight_id(scn, obj->id);
813 static int scene_obj_open(struct scene *scn, struct scene_obj *obj)
819 case SCENEOBJT_IMAGE:
823 case SCENEOBJT_TEXTLINE:
824 ret = scene_textline_open(scn,
825 (struct scene_obj_textline *)obj);
827 return log_msg_ret("op", ret);
834 int scene_set_open(struct scene *scn, uint id, bool open)
836 struct scene_obj *obj;
839 obj = scene_obj_find(scn, id, SCENEOBJT_NONE);
841 return log_msg_ret("find", -ENOENT);
844 ret = scene_obj_open(scn, obj);
846 return log_msg_ret("op", ret);
849 ret = scene_obj_flag_clrset(scn, id, SCENEOF_OPEN,
850 open ? SCENEOF_OPEN : 0);
852 return log_msg_ret("flg", ret);
857 int scene_iter_objs(struct scene *scn, expo_scene_obj_iterator iter,
860 struct scene_obj *obj;
862 list_for_each_entry(obj, &scn->obj_head, sibling) {
865 ret = iter(obj, priv);
867 return log_msg_ret("itr", ret);
873 int scene_bbox_union(struct scene *scn, uint id, int inset,
874 struct vidconsole_bbox *bbox)
876 struct scene_obj *obj;
880 obj = scene_obj_find(scn, id, SCENEOBJT_NONE);
882 return log_msg_ret("obj", -ENOENT);
884 bbox->x0 = min(bbox->x0, obj->dim.x - inset);
885 bbox->y0 = min(bbox->y0, obj->dim.y);
886 bbox->x1 = max(bbox->x1, obj->dim.x + obj->dim.w + inset);
887 bbox->y1 = max(bbox->y1, obj->dim.y + obj->dim.h);
889 bbox->x0 = obj->dim.x - inset;
890 bbox->y0 = obj->dim.y;
891 bbox->x1 = obj->dim.x + obj->dim.w + inset;
892 bbox->y1 = obj->dim.y + obj->dim.h;