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);
34 INIT_LIST_HEAD(&scn->obj_head);
35 scn->id = resolve_id(exp, id);
37 list_add_tail(&scn->sibling, &exp->scene_head);
44 void scene_obj_destroy(struct scene_obj *obj)
46 if (obj->type == SCENEOBJT_MENU)
47 scene_menu_destroy((struct scene_obj_menu *)obj);
52 void scene_destroy(struct scene *scn)
54 struct scene_obj *obj, *next;
56 list_for_each_entry_safe(obj, next, &scn->obj_head, sibling)
57 scene_obj_destroy(obj);
63 int scene_title_set(struct scene *scn, uint id)
70 int scene_obj_count(struct scene *scn)
72 struct scene_obj *obj;
75 list_for_each_entry(obj, &scn->obj_head, sibling)
81 void *scene_obj_find(struct scene *scn, uint id, enum scene_obj_t type)
83 struct scene_obj *obj;
85 list_for_each_entry(obj, &scn->obj_head, sibling) {
87 (type == SCENEOBJT_NONE || obj->type == type))
94 int scene_obj_add(struct scene *scn, const char *name, uint id,
95 enum scene_obj_t type, uint size, struct scene_obj **objp)
97 struct scene_obj *obj;
99 obj = calloc(1, size);
101 return log_msg_ret("obj", -ENOMEM);
102 obj->name = strdup(name);
105 return log_msg_ret("name", -ENOMEM);
108 obj->id = resolve_id(scn->expo, id);
111 list_add_tail(&obj->sibling, &scn->obj_head);
117 int scene_img(struct scene *scn, const char *name, uint id, char *data,
118 struct scene_obj_img **imgp)
120 struct scene_obj_img *img;
123 ret = scene_obj_add(scn, name, id, SCENEOBJT_IMAGE,
124 sizeof(struct scene_obj_img),
125 (struct scene_obj **)&img);
127 return log_msg_ret("obj", -ENOMEM);
137 int scene_txt(struct scene *scn, const char *name, uint id, uint str_id,
138 struct scene_obj_txt **txtp)
140 struct scene_obj_txt *txt;
143 ret = scene_obj_add(scn, name, id, SCENEOBJT_TEXT,
144 sizeof(struct scene_obj_txt),
145 (struct scene_obj **)&txt);
147 return log_msg_ret("obj", -ENOMEM);
149 txt->str_id = str_id;
157 int scene_txt_str(struct scene *scn, const char *name, uint id, uint str_id,
158 const char *str, struct scene_obj_txt **txtp)
160 struct scene_obj_txt *txt;
163 ret = expo_str(scn->expo, name, str_id, str);
165 return log_msg_ret("str", ret);
166 else if (ret != str_id)
167 return log_msg_ret("id", -EEXIST);
169 ret = scene_obj_add(scn, name, id, SCENEOBJT_TEXT,
170 sizeof(struct scene_obj_txt),
171 (struct scene_obj **)&txt);
173 return log_msg_ret("obj", -ENOMEM);
175 txt->str_id = str_id;
183 int scene_txt_set_font(struct scene *scn, uint id, const char *font_name,
186 struct scene_obj_txt *txt;
188 txt = scene_obj_find(scn, id, SCENEOBJT_TEXT);
190 return log_msg_ret("find", -ENOENT);
191 txt->font_name = font_name;
192 txt->font_size = font_size;
197 int scene_obj_set_pos(struct scene *scn, uint id, int x, int y)
199 struct scene_obj *obj;
201 obj = scene_obj_find(scn, id, SCENEOBJT_NONE);
203 return log_msg_ret("find", -ENOENT);
210 int scene_obj_set_size(struct scene *scn, uint id, int w, int h)
212 struct scene_obj *obj;
214 obj = scene_obj_find(scn, id, SCENEOBJT_NONE);
216 return log_msg_ret("find", -ENOENT);
223 int scene_obj_set_hide(struct scene *scn, uint id, bool hide)
227 ret = scene_obj_flag_clrset(scn, id, SCENEOF_HIDE,
228 hide ? SCENEOF_HIDE : 0);
230 return log_msg_ret("flg", ret);
235 int scene_obj_flag_clrset(struct scene *scn, uint id, uint clr, uint set)
237 struct scene_obj *obj;
239 obj = scene_obj_find(scn, id, SCENEOBJT_NONE);
241 return log_msg_ret("find", -ENOENT);
248 int scene_obj_get_hw(struct scene *scn, uint id, int *widthp)
250 struct scene_obj *obj;
252 obj = scene_obj_find(scn, id, SCENEOBJT_NONE);
254 return log_msg_ret("find", -ENOENT);
260 case SCENEOBJT_IMAGE: {
261 struct scene_obj_img *img = (struct scene_obj_img *)obj;
265 video_bmp_get_info(img->data, &width, &height, &bpix);
270 case SCENEOBJT_TEXT: {
271 struct scene_obj_txt *txt = (struct scene_obj_txt *)obj;
272 struct expo *exp = scn->expo;
273 struct vidconsole_bbox bbox;
277 str = expo_get_str(exp, txt->str_id);
279 return log_msg_ret("str", -ENOENT);
282 /* if there is no console, make it up */
289 ret = vidconsole_measure(scn->expo->cons, txt->font_name,
290 txt->font_size, str, &bbox);
292 return log_msg_ret("mea", ret);
304 * scene_obj_render() - Render an object
307 static int scene_obj_render(struct scene_obj *obj, bool text_mode)
309 struct scene *scn = obj->scene;
310 struct expo *exp = scn->expo;
311 struct udevice *dev = exp->display;
312 struct udevice *cons = text_mode ? NULL : exp->cons;
321 case SCENEOBJT_IMAGE: {
322 struct scene_obj_img *img = (struct scene_obj_img *)obj;
326 ret = video_bmp_display(dev, map_to_sysmem(img->data), x, y,
329 return log_msg_ret("img", ret);
332 case SCENEOBJT_TEXT: {
333 struct scene_obj_txt *txt = (struct scene_obj_txt *)obj;
339 if (txt->font_name || txt->font_size) {
340 ret = vidconsole_select_font(cons,
344 ret = vidconsole_select_font(cons, NULL, 0);
346 if (ret && ret != -ENOSYS)
347 return log_msg_ret("font", ret);
348 str = expo_get_str(exp, txt->str_id);
350 struct video_priv *vid_priv;
351 struct vidconsole_colour old;
352 enum colour_idx fore, back;
354 if (CONFIG_IS_ENABLED(SYS_WHITE_ON_BLACK)) {
358 fore = VID_LIGHT_GRAY;
362 vid_priv = dev_get_uclass_priv(dev);
363 if (obj->flags & SCENEOF_POINT) {
364 vidconsole_push_colour(cons, fore, back, &old);
365 video_fill_part(dev, x, y,
366 x + obj->dim.w, y + obj->dim.h,
367 vid_priv->colour_bg);
369 vidconsole_set_cursor_pos(cons, x, y);
370 vidconsole_put_string(cons, str);
371 if (obj->flags & SCENEOF_POINT)
372 vidconsole_pop_colour(cons, &old);
376 case SCENEOBJT_MENU: {
377 struct scene_obj_menu *menu = (struct scene_obj_menu *)obj;
379 if (exp->popup && (obj->flags & SCENEOF_OPEN)) {
383 /* draw a background behind the menu items */
384 scene_menu_render(menu);
387 * With a vidconsole, the text and item pointer are rendered as
388 * normal objects so we don't need to do anything here. The menu
389 * simply controls where they are positioned.
394 ret = scene_menu_display(menu);
396 return log_msg_ret("img", ret);
405 int scene_arrange(struct scene *scn)
407 struct scene_obj *obj;
410 list_for_each_entry(obj, &scn->obj_head, sibling) {
411 if (obj->type == SCENEOBJT_MENU) {
412 struct scene_obj_menu *menu;
414 menu = (struct scene_obj_menu *)obj,
415 ret = scene_menu_arrange(scn, menu);
417 return log_msg_ret("arr", ret);
424 int scene_render(struct scene *scn)
426 struct expo *exp = scn->expo;
427 struct scene_obj *obj;
430 list_for_each_entry(obj, &scn->obj_head, sibling) {
431 if (!(obj->flags & SCENEOF_HIDE)) {
432 ret = scene_obj_render(obj, exp->text_mode);
433 if (ret && ret != -ENOTSUPP)
434 return log_msg_ret("ren", ret);
441 int scene_send_key(struct scene *scn, int key, struct expo_action *event)
443 struct scene_obj *obj;
446 list_for_each_entry(obj, &scn->obj_head, sibling) {
447 if (obj->type == SCENEOBJT_MENU) {
448 struct scene_obj_menu *menu;
450 menu = (struct scene_obj_menu *)obj,
451 ret = scene_menu_send_key(scn, menu, key, event);
453 return log_msg_ret("key", ret);
461 int scene_calc_dims(struct scene *scn, bool do_menus)
463 struct scene_obj *obj;
466 list_for_each_entry(obj, &scn->obj_head, sibling) {
470 case SCENEOBJT_IMAGE: {
474 ret = scene_obj_get_hw(scn, obj->id, &width);
476 return log_msg_ret("get", ret);
482 case SCENEOBJT_MENU: {
483 struct scene_obj_menu *menu;
486 menu = (struct scene_obj_menu *)obj;
488 ret = scene_menu_calc_dims(menu);
490 return log_msg_ret("men", ret);
500 int scene_apply_theme(struct scene *scn, struct expo_theme *theme)
502 struct scene_obj *obj;
505 /* Avoid error-checking optional items */
506 scene_txt_set_font(scn, scn->title_id, NULL, theme->font_size);
508 list_for_each_entry(obj, &scn->obj_head, sibling) {
511 case SCENEOBJT_IMAGE:
515 scene_txt_set_font(scn, obj->id, NULL,
521 ret = scene_arrange(scn);
523 return log_msg_ret("arr", ret);
528 void scene_set_highlight_id(struct scene *scn, uint id)
530 scn->highlight_id = id;
533 void scene_highlight_first(struct scene *scn)
535 struct scene_obj *obj;
537 list_for_each_entry(obj, &scn->obj_head, sibling) {
540 scene_set_highlight_id(scn, obj->id);
548 int scene_set_open(struct scene *scn, uint id, bool open)
552 ret = scene_obj_flag_clrset(scn, id, SCENEOF_OPEN,
553 open ? SCENEOF_OPEN : 0);
555 return log_msg_ret("flg", ret);