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 vidconsole_set_cursor_pos(cons, x, y);
349 str = expo_get_str(exp, txt->str_id);
351 vidconsole_put_string(cons, str);
354 case SCENEOBJT_MENU: {
355 struct scene_obj_menu *menu = (struct scene_obj_menu *)obj;
357 * With a vidconsole, the text and item pointer are rendered as
358 * normal objects so we don't need to do anything here. The menu
359 * simply controls where they are positioned.
364 ret = scene_menu_display(menu);
366 return log_msg_ret("img", ret);
375 int scene_arrange(struct scene *scn)
377 struct scene_obj *obj;
380 list_for_each_entry(obj, &scn->obj_head, sibling) {
381 if (obj->type == SCENEOBJT_MENU) {
382 struct scene_obj_menu *menu;
384 menu = (struct scene_obj_menu *)obj,
385 ret = scene_menu_arrange(scn, menu);
387 return log_msg_ret("arr", ret);
394 int scene_render(struct scene *scn)
396 struct expo *exp = scn->expo;
397 struct scene_obj *obj;
400 list_for_each_entry(obj, &scn->obj_head, sibling) {
401 if (!(obj->flags & SCENEOF_HIDE)) {
402 ret = scene_obj_render(obj, exp->text_mode);
403 if (ret && ret != -ENOTSUPP)
404 return log_msg_ret("ren", ret);
411 int scene_send_key(struct scene *scn, int key, struct expo_action *event)
413 struct scene_obj *obj;
416 list_for_each_entry(obj, &scn->obj_head, sibling) {
417 if (obj->type == SCENEOBJT_MENU) {
418 struct scene_obj_menu *menu;
420 menu = (struct scene_obj_menu *)obj,
421 ret = scene_menu_send_key(scn, menu, key, event);
423 return log_msg_ret("key", ret);
431 int scene_calc_dims(struct scene *scn, bool do_menus)
433 struct scene_obj *obj;
436 list_for_each_entry(obj, &scn->obj_head, sibling) {
440 case SCENEOBJT_IMAGE: {
444 ret = scene_obj_get_hw(scn, obj->id, &width);
446 return log_msg_ret("get", ret);
452 case SCENEOBJT_MENU: {
453 struct scene_obj_menu *menu;
456 menu = (struct scene_obj_menu *)obj;
458 ret = scene_menu_calc_dims(menu);
460 return log_msg_ret("men", ret);