Simon Glass | 5e2607a | 2023-01-06 08:52:37 -0600 | [diff] [blame] | 1 | // 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 Glass | c98cb51 | 2023-06-01 10:22:43 -0600 | [diff] [blame] | 9 | #define LOG_CATEGORY LOGC_EXPO |
| 10 | |
Simon Glass | 5e2607a | 2023-01-06 08:52:37 -0600 | [diff] [blame] | 11 | #include <common.h> |
| 12 | #include <dm.h> |
| 13 | #include <expo.h> |
| 14 | #include <malloc.h> |
| 15 | #include <mapmem.h> |
| 16 | #include <video.h> |
| 17 | #include <video_console.h> |
| 18 | #include <linux/input.h> |
| 19 | #include "scene_internal.h" |
| 20 | |
Simon Glass | 5e2607a | 2023-01-06 08:52:37 -0600 | [diff] [blame] | 21 | int 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 | |
| 34 | INIT_LIST_HEAD(&scn->obj_head); |
| 35 | scn->id = resolve_id(exp, id); |
| 36 | scn->expo = exp; |
| 37 | list_add_tail(&scn->sibling, &exp->scene_head); |
| 38 | |
| 39 | *scnp = scn; |
| 40 | |
| 41 | return scn->id; |
| 42 | } |
| 43 | |
| 44 | void scene_obj_destroy(struct scene_obj *obj) |
| 45 | { |
| 46 | if (obj->type == SCENEOBJT_MENU) |
| 47 | scene_menu_destroy((struct scene_obj_menu *)obj); |
| 48 | free(obj->name); |
| 49 | free(obj); |
| 50 | } |
| 51 | |
| 52 | void scene_destroy(struct scene *scn) |
| 53 | { |
| 54 | struct scene_obj *obj, *next; |
| 55 | |
| 56 | list_for_each_entry_safe(obj, next, &scn->obj_head, sibling) |
| 57 | scene_obj_destroy(obj); |
| 58 | |
| 59 | free(scn->name); |
Simon Glass | 5e2607a | 2023-01-06 08:52:37 -0600 | [diff] [blame] | 60 | free(scn); |
| 61 | } |
| 62 | |
Simon Glass | def898c | 2023-06-01 10:22:27 -0600 | [diff] [blame] | 63 | int scene_title_set(struct scene *scn, uint id) |
Simon Glass | 5e2607a | 2023-01-06 08:52:37 -0600 | [diff] [blame] | 64 | { |
Simon Glass | def898c | 2023-06-01 10:22:27 -0600 | [diff] [blame] | 65 | scn->title_id = id; |
Simon Glass | 5e2607a | 2023-01-06 08:52:37 -0600 | [diff] [blame] | 66 | |
| 67 | return 0; |
| 68 | } |
| 69 | |
| 70 | int scene_obj_count(struct scene *scn) |
| 71 | { |
| 72 | struct scene_obj *obj; |
| 73 | int count = 0; |
| 74 | |
| 75 | list_for_each_entry(obj, &scn->obj_head, sibling) |
| 76 | count++; |
| 77 | |
| 78 | return count; |
| 79 | } |
| 80 | |
| 81 | void *scene_obj_find(struct scene *scn, uint id, enum scene_obj_t type) |
| 82 | { |
| 83 | struct scene_obj *obj; |
| 84 | |
| 85 | list_for_each_entry(obj, &scn->obj_head, sibling) { |
| 86 | if (obj->id == id && |
| 87 | (type == SCENEOBJT_NONE || obj->type == type)) |
| 88 | return obj; |
| 89 | } |
| 90 | |
| 91 | return NULL; |
| 92 | } |
| 93 | |
| 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) |
| 96 | { |
| 97 | struct scene_obj *obj; |
| 98 | |
| 99 | obj = calloc(1, size); |
| 100 | if (!obj) |
| 101 | return log_msg_ret("obj", -ENOMEM); |
| 102 | obj->name = strdup(name); |
| 103 | if (!obj->name) { |
| 104 | free(obj); |
| 105 | return log_msg_ret("name", -ENOMEM); |
| 106 | } |
| 107 | |
| 108 | obj->id = resolve_id(scn->expo, id); |
| 109 | obj->scene = scn; |
| 110 | obj->type = type; |
| 111 | list_add_tail(&obj->sibling, &scn->obj_head); |
| 112 | *objp = obj; |
| 113 | |
| 114 | return obj->id; |
| 115 | } |
| 116 | |
| 117 | int scene_img(struct scene *scn, const char *name, uint id, char *data, |
| 118 | struct scene_obj_img **imgp) |
| 119 | { |
| 120 | struct scene_obj_img *img; |
| 121 | int ret; |
| 122 | |
| 123 | ret = scene_obj_add(scn, name, id, SCENEOBJT_IMAGE, |
| 124 | sizeof(struct scene_obj_img), |
| 125 | (struct scene_obj **)&img); |
| 126 | if (ret < 0) |
| 127 | return log_msg_ret("obj", -ENOMEM); |
| 128 | |
| 129 | img->data = data; |
| 130 | |
| 131 | if (imgp) |
| 132 | *imgp = img; |
| 133 | |
| 134 | return img->obj.id; |
| 135 | } |
| 136 | |
| 137 | int scene_txt(struct scene *scn, const char *name, uint id, uint str_id, |
| 138 | struct scene_obj_txt **txtp) |
| 139 | { |
| 140 | struct scene_obj_txt *txt; |
| 141 | int ret; |
| 142 | |
| 143 | ret = scene_obj_add(scn, name, id, SCENEOBJT_TEXT, |
| 144 | sizeof(struct scene_obj_txt), |
| 145 | (struct scene_obj **)&txt); |
| 146 | if (ret < 0) |
| 147 | return log_msg_ret("obj", -ENOMEM); |
| 148 | |
| 149 | txt->str_id = str_id; |
| 150 | |
| 151 | if (txtp) |
| 152 | *txtp = txt; |
| 153 | |
| 154 | return txt->obj.id; |
| 155 | } |
| 156 | |
| 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) |
| 159 | { |
| 160 | struct scene_obj_txt *txt; |
| 161 | int ret; |
| 162 | |
| 163 | ret = expo_str(scn->expo, name, str_id, str); |
| 164 | if (ret < 0) |
| 165 | return log_msg_ret("str", ret); |
| 166 | else if (ret != str_id) |
| 167 | return log_msg_ret("id", -EEXIST); |
| 168 | |
| 169 | ret = scene_obj_add(scn, name, id, SCENEOBJT_TEXT, |
| 170 | sizeof(struct scene_obj_txt), |
| 171 | (struct scene_obj **)&txt); |
| 172 | if (ret < 0) |
| 173 | return log_msg_ret("obj", -ENOMEM); |
| 174 | |
| 175 | txt->str_id = str_id; |
| 176 | |
| 177 | if (txtp) |
| 178 | *txtp = txt; |
| 179 | |
| 180 | return txt->obj.id; |
| 181 | } |
| 182 | |
| 183 | int scene_txt_set_font(struct scene *scn, uint id, const char *font_name, |
| 184 | uint font_size) |
| 185 | { |
| 186 | struct scene_obj_txt *txt; |
| 187 | |
| 188 | txt = scene_obj_find(scn, id, SCENEOBJT_TEXT); |
| 189 | if (!txt) |
| 190 | return log_msg_ret("find", -ENOENT); |
| 191 | txt->font_name = font_name; |
| 192 | txt->font_size = font_size; |
| 193 | |
| 194 | return 0; |
| 195 | } |
| 196 | |
| 197 | int scene_obj_set_pos(struct scene *scn, uint id, int x, int y) |
| 198 | { |
| 199 | struct scene_obj *obj; |
| 200 | |
| 201 | obj = scene_obj_find(scn, id, SCENEOBJT_NONE); |
| 202 | if (!obj) |
| 203 | return log_msg_ret("find", -ENOENT); |
Simon Glass | ae45d6c | 2023-06-01 10:22:49 -0600 | [diff] [blame] | 204 | obj->dim.x = x; |
| 205 | obj->dim.y = y; |
Simon Glass | 5e2607a | 2023-01-06 08:52:37 -0600 | [diff] [blame] | 206 | |
| 207 | return 0; |
| 208 | } |
| 209 | |
| 210 | int scene_obj_set_hide(struct scene *scn, uint id, bool hide) |
| 211 | { |
Simon Glass | ce72c9e | 2023-06-01 10:22:50 -0600 | [diff] [blame] | 212 | int ret; |
| 213 | |
| 214 | ret = scene_obj_flag_clrset(scn, id, SCENEOF_HIDE, |
| 215 | hide ? SCENEOF_HIDE : 0); |
| 216 | if (ret) |
| 217 | return log_msg_ret("flg", ret); |
| 218 | |
| 219 | return 0; |
| 220 | } |
| 221 | |
| 222 | int scene_obj_flag_clrset(struct scene *scn, uint id, uint clr, uint set) |
| 223 | { |
Simon Glass | 5e2607a | 2023-01-06 08:52:37 -0600 | [diff] [blame] | 224 | struct scene_obj *obj; |
| 225 | |
| 226 | obj = scene_obj_find(scn, id, SCENEOBJT_NONE); |
| 227 | if (!obj) |
| 228 | return log_msg_ret("find", -ENOENT); |
Simon Glass | ce72c9e | 2023-06-01 10:22:50 -0600 | [diff] [blame] | 229 | obj->flags &= ~clr; |
| 230 | obj->flags |= set; |
Simon Glass | 5e2607a | 2023-01-06 08:52:37 -0600 | [diff] [blame] | 231 | |
| 232 | return 0; |
| 233 | } |
| 234 | |
| 235 | int scene_obj_get_hw(struct scene *scn, uint id, int *widthp) |
| 236 | { |
| 237 | struct scene_obj *obj; |
| 238 | |
| 239 | obj = scene_obj_find(scn, id, SCENEOBJT_NONE); |
| 240 | if (!obj) |
| 241 | return log_msg_ret("find", -ENOENT); |
| 242 | |
| 243 | switch (obj->type) { |
| 244 | case SCENEOBJT_NONE: |
| 245 | case SCENEOBJT_MENU: |
| 246 | break; |
| 247 | case SCENEOBJT_IMAGE: { |
| 248 | struct scene_obj_img *img = (struct scene_obj_img *)obj; |
| 249 | ulong width, height; |
| 250 | uint bpix; |
| 251 | |
| 252 | video_bmp_get_info(img->data, &width, &height, &bpix); |
| 253 | if (widthp) |
| 254 | *widthp = width; |
| 255 | return height; |
| 256 | } |
| 257 | case SCENEOBJT_TEXT: { |
| 258 | struct scene_obj_txt *txt = (struct scene_obj_txt *)obj; |
| 259 | struct expo *exp = scn->expo; |
Simon Glass | 50f0203 | 2023-06-01 10:22:51 -0600 | [diff] [blame^] | 260 | struct vidconsole_bbox bbox; |
| 261 | const char *str; |
| 262 | int len, ret; |
Simon Glass | 5e2607a | 2023-01-06 08:52:37 -0600 | [diff] [blame] | 263 | |
Simon Glass | 50f0203 | 2023-06-01 10:22:51 -0600 | [diff] [blame^] | 264 | str = expo_get_str(exp, txt->str_id); |
| 265 | if (!str) |
| 266 | return log_msg_ret("str", -ENOENT); |
| 267 | len = strlen(str); |
| 268 | |
| 269 | /* if there is no console, make it up */ |
| 270 | if (!exp->cons) { |
| 271 | if (widthp) |
| 272 | *widthp = 8 * len; |
| 273 | return 16; |
| 274 | } |
| 275 | |
| 276 | ret = vidconsole_measure(scn->expo->cons, txt->font_name, |
| 277 | txt->font_size, str, &bbox); |
| 278 | if (ret) |
| 279 | return log_msg_ret("mea", ret); |
Simon Glass | 5e2607a | 2023-01-06 08:52:37 -0600 | [diff] [blame] | 280 | if (widthp) |
Simon Glass | 50f0203 | 2023-06-01 10:22:51 -0600 | [diff] [blame^] | 281 | *widthp = bbox.x1; |
Simon Glass | 5e2607a | 2023-01-06 08:52:37 -0600 | [diff] [blame] | 282 | |
Simon Glass | 50f0203 | 2023-06-01 10:22:51 -0600 | [diff] [blame^] | 283 | return bbox.y1; |
Simon Glass | 5e2607a | 2023-01-06 08:52:37 -0600 | [diff] [blame] | 284 | } |
| 285 | } |
| 286 | |
| 287 | return 0; |
| 288 | } |
| 289 | |
| 290 | /** |
| 291 | * scene_obj_render() - Render an object |
| 292 | * |
| 293 | */ |
| 294 | static int scene_obj_render(struct scene_obj *obj, bool text_mode) |
| 295 | { |
| 296 | struct scene *scn = obj->scene; |
| 297 | struct expo *exp = scn->expo; |
Simon Glass | 42b1849 | 2023-06-01 10:22:34 -0600 | [diff] [blame] | 298 | struct udevice *dev = exp->display; |
| 299 | struct udevice *cons = text_mode ? NULL : exp->cons; |
Simon Glass | 5e2607a | 2023-01-06 08:52:37 -0600 | [diff] [blame] | 300 | int x, y, ret; |
| 301 | |
Simon Glass | ae45d6c | 2023-06-01 10:22:49 -0600 | [diff] [blame] | 302 | x = obj->dim.x; |
| 303 | y = obj->dim.y; |
Simon Glass | 5e2607a | 2023-01-06 08:52:37 -0600 | [diff] [blame] | 304 | |
| 305 | switch (obj->type) { |
| 306 | case SCENEOBJT_NONE: |
| 307 | break; |
| 308 | case SCENEOBJT_IMAGE: { |
| 309 | struct scene_obj_img *img = (struct scene_obj_img *)obj; |
| 310 | |
| 311 | if (!cons) |
| 312 | return -ENOTSUPP; |
| 313 | ret = video_bmp_display(dev, map_to_sysmem(img->data), x, y, |
| 314 | true); |
| 315 | if (ret < 0) |
| 316 | return log_msg_ret("img", ret); |
| 317 | break; |
| 318 | } |
| 319 | case SCENEOBJT_TEXT: { |
| 320 | struct scene_obj_txt *txt = (struct scene_obj_txt *)obj; |
| 321 | const char *str; |
| 322 | |
| 323 | if (!cons) |
| 324 | return -ENOTSUPP; |
| 325 | |
| 326 | if (txt->font_name || txt->font_size) { |
| 327 | ret = vidconsole_select_font(cons, |
| 328 | txt->font_name, |
| 329 | txt->font_size); |
| 330 | } else { |
| 331 | ret = vidconsole_select_font(cons, NULL, 0); |
| 332 | } |
| 333 | if (ret && ret != -ENOSYS) |
| 334 | return log_msg_ret("font", ret); |
| 335 | vidconsole_set_cursor_pos(cons, x, y); |
| 336 | str = expo_get_str(exp, txt->str_id); |
| 337 | if (str) |
| 338 | vidconsole_put_string(cons, str); |
| 339 | break; |
| 340 | } |
| 341 | case SCENEOBJT_MENU: { |
| 342 | struct scene_obj_menu *menu = (struct scene_obj_menu *)obj; |
| 343 | /* |
| 344 | * With a vidconsole, the text and item pointer are rendered as |
| 345 | * normal objects so we don't need to do anything here. The menu |
| 346 | * simply controls where they are positioned. |
| 347 | */ |
| 348 | if (cons) |
| 349 | return -ENOTSUPP; |
| 350 | |
| 351 | ret = scene_menu_display(menu); |
| 352 | if (ret < 0) |
| 353 | return log_msg_ret("img", ret); |
| 354 | |
| 355 | break; |
| 356 | } |
| 357 | } |
| 358 | |
| 359 | return 0; |
| 360 | } |
| 361 | |
| 362 | int scene_arrange(struct scene *scn) |
| 363 | { |
| 364 | struct scene_obj *obj; |
| 365 | int ret; |
| 366 | |
| 367 | list_for_each_entry(obj, &scn->obj_head, sibling) { |
| 368 | if (obj->type == SCENEOBJT_MENU) { |
| 369 | struct scene_obj_menu *menu; |
| 370 | |
| 371 | menu = (struct scene_obj_menu *)obj, |
| 372 | ret = scene_menu_arrange(scn, menu); |
| 373 | if (ret) |
| 374 | return log_msg_ret("arr", ret); |
| 375 | } |
| 376 | } |
| 377 | |
| 378 | return 0; |
| 379 | } |
| 380 | |
| 381 | int scene_render(struct scene *scn) |
| 382 | { |
| 383 | struct expo *exp = scn->expo; |
| 384 | struct scene_obj *obj; |
| 385 | int ret; |
| 386 | |
| 387 | list_for_each_entry(obj, &scn->obj_head, sibling) { |
Simon Glass | ce72c9e | 2023-06-01 10:22:50 -0600 | [diff] [blame] | 388 | if (!(obj->flags & SCENEOF_HIDE)) { |
Simon Glass | 5e2607a | 2023-01-06 08:52:37 -0600 | [diff] [blame] | 389 | ret = scene_obj_render(obj, exp->text_mode); |
| 390 | if (ret && ret != -ENOTSUPP) |
| 391 | return log_msg_ret("ren", ret); |
| 392 | } |
| 393 | } |
| 394 | |
| 395 | return 0; |
| 396 | } |
| 397 | |
| 398 | int scene_send_key(struct scene *scn, int key, struct expo_action *event) |
| 399 | { |
| 400 | struct scene_obj *obj; |
| 401 | int ret; |
| 402 | |
| 403 | list_for_each_entry(obj, &scn->obj_head, sibling) { |
| 404 | if (obj->type == SCENEOBJT_MENU) { |
| 405 | struct scene_obj_menu *menu; |
| 406 | |
| 407 | menu = (struct scene_obj_menu *)obj, |
| 408 | ret = scene_menu_send_key(scn, menu, key, event); |
| 409 | if (ret) |
| 410 | return log_msg_ret("key", ret); |
Simon Glass | 5e2607a | 2023-01-06 08:52:37 -0600 | [diff] [blame] | 411 | break; |
| 412 | } |
| 413 | } |
| 414 | |
| 415 | return 0; |
| 416 | } |