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
6 * Written by Simon Glass <sjg@chromium.org>
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_hide(struct scene *scn, uint id, bool hide)
212 struct scene_obj *obj;
214 obj = scene_obj_find(scn, id, SCENEOBJT_NONE);
216 return log_msg_ret("find", -ENOENT);
222 int scene_obj_get_hw(struct scene *scn, uint id, int *widthp)
224 struct scene_obj *obj;
226 obj = scene_obj_find(scn, id, SCENEOBJT_NONE);
228 return log_msg_ret("find", -ENOENT);
234 case SCENEOBJT_IMAGE: {
235 struct scene_obj_img *img = (struct scene_obj_img *)obj;
239 video_bmp_get_info(img->data, &width, &height, &bpix);
244 case SCENEOBJT_TEXT: {
245 struct scene_obj_txt *txt = (struct scene_obj_txt *)obj;
246 struct expo *exp = scn->expo;
249 *widthp = 16; /* fake value for now */
251 return txt->font_size;
253 return video_default_font_height(exp->display);
255 /* use a sensible default */
264 * scene_obj_render() - Render an object
267 static int scene_obj_render(struct scene_obj *obj, bool text_mode)
269 struct scene *scn = obj->scene;
270 struct expo *exp = scn->expo;
271 struct udevice *dev = exp->display;
272 struct udevice *cons = text_mode ? NULL : exp->cons;
281 case SCENEOBJT_IMAGE: {
282 struct scene_obj_img *img = (struct scene_obj_img *)obj;
286 ret = video_bmp_display(dev, map_to_sysmem(img->data), x, y,
289 return log_msg_ret("img", ret);
292 case SCENEOBJT_TEXT: {
293 struct scene_obj_txt *txt = (struct scene_obj_txt *)obj;
299 if (txt->font_name || txt->font_size) {
300 ret = vidconsole_select_font(cons,
304 ret = vidconsole_select_font(cons, NULL, 0);
306 if (ret && ret != -ENOSYS)
307 return log_msg_ret("font", ret);
308 vidconsole_set_cursor_pos(cons, x, y);
309 str = expo_get_str(exp, txt->str_id);
311 vidconsole_put_string(cons, str);
314 case SCENEOBJT_MENU: {
315 struct scene_obj_menu *menu = (struct scene_obj_menu *)obj;
317 * With a vidconsole, the text and item pointer are rendered as
318 * normal objects so we don't need to do anything here. The menu
319 * simply controls where they are positioned.
324 ret = scene_menu_display(menu);
326 return log_msg_ret("img", ret);
335 int scene_arrange(struct scene *scn)
337 struct scene_obj *obj;
340 list_for_each_entry(obj, &scn->obj_head, sibling) {
341 if (obj->type == SCENEOBJT_MENU) {
342 struct scene_obj_menu *menu;
344 menu = (struct scene_obj_menu *)obj,
345 ret = scene_menu_arrange(scn, menu);
347 return log_msg_ret("arr", ret);
354 int scene_render(struct scene *scn)
356 struct expo *exp = scn->expo;
357 struct scene_obj *obj;
360 list_for_each_entry(obj, &scn->obj_head, sibling) {
362 ret = scene_obj_render(obj, exp->text_mode);
363 if (ret && ret != -ENOTSUPP)
364 return log_msg_ret("ren", ret);
371 int scene_send_key(struct scene *scn, int key, struct expo_action *event)
373 struct scene_obj *obj;
376 list_for_each_entry(obj, &scn->obj_head, sibling) {
377 if (obj->type == SCENEOBJT_MENU) {
378 struct scene_obj_menu *menu;
380 menu = (struct scene_obj_menu *)obj,
381 ret = scene_menu_send_key(scn, menu, key, event);
383 return log_msg_ret("key", ret);