Simon Glass | d8adbe9 | 2023-01-06 08:52:36 -0600 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Implementation of a expo, a collection of scenes providing menu options |
| 4 | * |
| 5 | * Copyright 2022 Google LLC |
| 6 | * Written by Simon Glass <sjg@chromium.org> |
| 7 | */ |
| 8 | |
Simon Glass | fe4c1e2 | 2023-06-01 10:22:43 -0600 | [diff] [blame] | 9 | #define LOG_CATEGORY LOGC_EXPO |
| 10 | |
Simon Glass | d8adbe9 | 2023-01-06 08:52:36 -0600 | [diff] [blame] | 11 | #include <dm.h> |
| 12 | #include <expo.h> |
Simon Glass | 137b442 | 2025-05-02 08:46:16 -0600 | [diff] [blame] | 13 | #include <log.h> |
Simon Glass | d8adbe9 | 2023-01-06 08:52:36 -0600 | [diff] [blame] | 14 | #include <malloc.h> |
Simon Glass | 137b442 | 2025-05-02 08:46:16 -0600 | [diff] [blame] | 15 | #include <menu.h> |
Simon Glass | d8adbe9 | 2023-01-06 08:52:36 -0600 | [diff] [blame] | 16 | #include <video.h> |
Simon Glass | 137b442 | 2025-05-02 08:46:16 -0600 | [diff] [blame] | 17 | #include <watchdog.h> |
| 18 | #include <linux/delay.h> |
Simon Glass | d8adbe9 | 2023-01-06 08:52:36 -0600 | [diff] [blame] | 19 | #include "scene_internal.h" |
| 20 | |
| 21 | int expo_new(const char *name, void *priv, struct expo **expp) |
| 22 | { |
| 23 | struct expo *exp; |
| 24 | |
| 25 | exp = calloc(1, sizeof(struct expo)); |
| 26 | if (!exp) |
| 27 | return log_msg_ret("expo", -ENOMEM); |
| 28 | exp->name = strdup(name); |
| 29 | if (!exp->name) { |
| 30 | free(exp); |
| 31 | return log_msg_ret("name", -ENOMEM); |
| 32 | } |
| 33 | exp->priv = priv; |
| 34 | INIT_LIST_HEAD(&exp->scene_head); |
| 35 | INIT_LIST_HEAD(&exp->str_head); |
Simon Glass | 53a0a2f | 2024-10-14 16:31:57 -0600 | [diff] [blame] | 36 | exp->next_id = EXPOID_BASE_ID; |
Simon Glass | 683d883 | 2025-05-02 08:46:15 -0600 | [diff] [blame] | 37 | cli_ch_init(&exp->cch); |
Simon Glass | d8adbe9 | 2023-01-06 08:52:36 -0600 | [diff] [blame] | 38 | |
| 39 | *expp = exp; |
| 40 | |
| 41 | return 0; |
| 42 | } |
| 43 | |
| 44 | static void estr_destroy(struct expo_string *estr) |
| 45 | { |
| 46 | free(estr); |
| 47 | } |
| 48 | |
| 49 | void expo_destroy(struct expo *exp) |
| 50 | { |
| 51 | struct scene *scn, *next; |
| 52 | struct expo_string *estr, *enext; |
| 53 | |
| 54 | list_for_each_entry_safe(scn, next, &exp->scene_head, sibling) |
| 55 | scene_destroy(scn); |
| 56 | |
| 57 | list_for_each_entry_safe(estr, enext, &exp->str_head, sibling) |
| 58 | estr_destroy(estr); |
| 59 | |
| 60 | free(exp->name); |
| 61 | free(exp); |
| 62 | } |
| 63 | |
Simon Glass | 6e9e415 | 2023-06-01 10:22:47 -0600 | [diff] [blame] | 64 | uint resolve_id(struct expo *exp, uint id) |
| 65 | { |
Simon Glass | 6130072 | 2023-06-01 10:23:01 -0600 | [diff] [blame] | 66 | log_debug("resolve id %d\n", id); |
Simon Glass | 6e9e415 | 2023-06-01 10:22:47 -0600 | [diff] [blame] | 67 | if (!id) |
| 68 | id = exp->next_id++; |
| 69 | else if (id >= exp->next_id) |
| 70 | exp->next_id = id + 1; |
| 71 | |
| 72 | return id; |
| 73 | } |
| 74 | |
| 75 | void expo_set_dynamic_start(struct expo *exp, uint dyn_start) |
| 76 | { |
| 77 | exp->next_id = dyn_start; |
| 78 | } |
| 79 | |
Simon Glass | d8adbe9 | 2023-01-06 08:52:36 -0600 | [diff] [blame] | 80 | int expo_str(struct expo *exp, const char *name, uint id, const char *str) |
| 81 | { |
| 82 | struct expo_string *estr; |
| 83 | |
| 84 | estr = calloc(1, sizeof(struct expo_string)); |
| 85 | if (!estr) |
| 86 | return log_msg_ret("obj", -ENOMEM); |
| 87 | |
| 88 | estr->id = resolve_id(exp, id); |
Simon Glass | da33775 | 2025-05-02 08:46:32 -0600 | [diff] [blame] | 89 | abuf_init_const(&estr->buf, str, strlen(str) + 1); |
Simon Glass | d8adbe9 | 2023-01-06 08:52:36 -0600 | [diff] [blame] | 90 | list_add_tail(&estr->sibling, &exp->str_head); |
| 91 | |
| 92 | return estr->id; |
| 93 | } |
| 94 | |
| 95 | const char *expo_get_str(struct expo *exp, uint id) |
| 96 | { |
| 97 | struct expo_string *estr; |
| 98 | |
| 99 | list_for_each_entry(estr, &exp->str_head, sibling) { |
| 100 | if (estr->id == id) |
Simon Glass | da33775 | 2025-05-02 08:46:32 -0600 | [diff] [blame] | 101 | return estr->buf.data; |
Simon Glass | d8adbe9 | 2023-01-06 08:52:36 -0600 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | return NULL; |
| 105 | } |
| 106 | |
Simon Glass | c5ae5b1 | 2025-05-02 08:46:40 -0600 | [diff] [blame^] | 107 | int expo_edit_str(struct expo *exp, uint id, struct abuf *orig, |
| 108 | struct abuf **copyp) |
| 109 | { |
| 110 | struct expo_string *estr; |
| 111 | struct abuf old; |
| 112 | |
| 113 | list_for_each_entry(estr, &exp->str_head, sibling) { |
| 114 | if (estr->id == id) { |
| 115 | old = estr->buf; |
| 116 | if (!abuf_copy(&old, &estr->buf)) |
| 117 | return -ENOMEM; |
| 118 | *copyp = &estr->buf; |
| 119 | if (orig) |
| 120 | *orig = old; |
| 121 | return 0; |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | return -ENOENT; |
| 126 | } |
| 127 | |
Simon Glass | d8adbe9 | 2023-01-06 08:52:36 -0600 | [diff] [blame] | 128 | int expo_set_display(struct expo *exp, struct udevice *dev) |
| 129 | { |
Simon Glass | 67e2af1 | 2023-06-01 10:22:34 -0600 | [diff] [blame] | 130 | struct udevice *cons; |
| 131 | int ret; |
| 132 | |
| 133 | ret = device_find_first_child_by_uclass(dev, UCLASS_VIDEO_CONSOLE, |
| 134 | &cons); |
| 135 | if (ret) |
| 136 | return log_msg_ret("con", ret); |
| 137 | |
Simon Glass | d8adbe9 | 2023-01-06 08:52:36 -0600 | [diff] [blame] | 138 | exp->display = dev; |
Simon Glass | 67e2af1 | 2023-06-01 10:22:34 -0600 | [diff] [blame] | 139 | exp->cons = cons; |
Simon Glass | d8adbe9 | 2023-01-06 08:52:36 -0600 | [diff] [blame] | 140 | |
| 141 | return 0; |
| 142 | } |
| 143 | |
Simon Glass | 7a96005 | 2023-06-01 10:22:52 -0600 | [diff] [blame] | 144 | int expo_calc_dims(struct expo *exp) |
| 145 | { |
| 146 | struct scene *scn; |
| 147 | int ret; |
| 148 | |
| 149 | if (!exp->cons) |
| 150 | return log_msg_ret("dim", -ENOTSUPP); |
| 151 | |
| 152 | list_for_each_entry(scn, &exp->scene_head, sibling) { |
| 153 | /* |
| 154 | * Do the menus last so that all the menus' text objects |
| 155 | * are dimensioned |
| 156 | */ |
| 157 | ret = scene_calc_dims(scn, false); |
| 158 | if (ret) |
| 159 | return log_msg_ret("scn", ret); |
| 160 | ret = scene_calc_dims(scn, true); |
| 161 | if (ret) |
| 162 | return log_msg_ret("scn", ret); |
| 163 | } |
| 164 | |
| 165 | return 0; |
| 166 | } |
| 167 | |
Simon Glass | b2c4034 | 2023-06-01 10:22:37 -0600 | [diff] [blame] | 168 | void expo_set_text_mode(struct expo *exp, bool text_mode) |
Simon Glass | d8adbe9 | 2023-01-06 08:52:36 -0600 | [diff] [blame] | 169 | { |
| 170 | exp->text_mode = text_mode; |
| 171 | } |
| 172 | |
| 173 | struct scene *expo_lookup_scene_id(struct expo *exp, uint scene_id) |
| 174 | { |
| 175 | struct scene *scn; |
| 176 | |
| 177 | list_for_each_entry(scn, &exp->scene_head, sibling) { |
| 178 | if (scn->id == scene_id) |
| 179 | return scn; |
| 180 | } |
| 181 | |
| 182 | return NULL; |
| 183 | } |
| 184 | |
| 185 | int expo_set_scene_id(struct expo *exp, uint scene_id) |
| 186 | { |
Simon Glass | d7e32a8 | 2023-06-01 10:22:35 -0600 | [diff] [blame] | 187 | struct scene *scn; |
| 188 | int ret; |
| 189 | |
| 190 | scn = expo_lookup_scene_id(exp, scene_id); |
| 191 | if (!scn) |
Simon Glass | d8adbe9 | 2023-01-06 08:52:36 -0600 | [diff] [blame] | 192 | return log_msg_ret("id", -ENOENT); |
Simon Glass | d7e32a8 | 2023-06-01 10:22:35 -0600 | [diff] [blame] | 193 | ret = scene_arrange(scn); |
| 194 | if (ret) |
| 195 | return log_msg_ret("arr", ret); |
| 196 | |
Simon Glass | d8adbe9 | 2023-01-06 08:52:36 -0600 | [diff] [blame] | 197 | exp->scene_id = scene_id; |
| 198 | |
| 199 | return 0; |
| 200 | } |
| 201 | |
Simon Glass | 12f5773 | 2023-06-01 10:22:58 -0600 | [diff] [blame] | 202 | int expo_first_scene_id(struct expo *exp) |
| 203 | { |
| 204 | struct scene *scn; |
| 205 | |
| 206 | if (list_empty(&exp->scene_head)) |
| 207 | return -ENOENT; |
| 208 | |
| 209 | scn = list_first_entry(&exp->scene_head, struct scene, sibling); |
| 210 | |
| 211 | return scn->id; |
| 212 | } |
| 213 | |
Simon Glass | d8adbe9 | 2023-01-06 08:52:36 -0600 | [diff] [blame] | 214 | int expo_render(struct expo *exp) |
| 215 | { |
| 216 | struct udevice *dev = exp->display; |
| 217 | struct video_priv *vid_priv = dev_get_uclass_priv(dev); |
| 218 | struct scene *scn = NULL; |
Simon Glass | 9ac53fb | 2023-10-01 19:14:41 -0600 | [diff] [blame] | 219 | enum colour_idx back; |
Simon Glass | d8adbe9 | 2023-01-06 08:52:36 -0600 | [diff] [blame] | 220 | u32 colour; |
| 221 | int ret; |
| 222 | |
Simon Glass | 21320da | 2025-04-02 06:29:33 +1300 | [diff] [blame] | 223 | back = vid_priv->white_on_black ? VID_BLACK : VID_WHITE; |
Simon Glass | 9ac53fb | 2023-10-01 19:14:41 -0600 | [diff] [blame] | 224 | colour = video_index_to_colour(vid_priv, back); |
Simon Glass | d8adbe9 | 2023-01-06 08:52:36 -0600 | [diff] [blame] | 225 | ret = video_fill(dev, colour); |
| 226 | if (ret) |
| 227 | return log_msg_ret("fill", ret); |
| 228 | |
| 229 | if (exp->scene_id) { |
| 230 | scn = expo_lookup_scene_id(exp, exp->scene_id); |
| 231 | if (!scn) |
| 232 | return log_msg_ret("scn", -ENOENT); |
| 233 | |
| 234 | ret = scene_render(scn); |
| 235 | if (ret) |
| 236 | return log_msg_ret("ren", ret); |
| 237 | } |
| 238 | |
| 239 | video_sync(dev, true); |
| 240 | |
| 241 | return scn ? 0 : -ECHILD; |
| 242 | } |
| 243 | |
| 244 | int expo_send_key(struct expo *exp, int key) |
| 245 | { |
| 246 | struct scene *scn = NULL; |
| 247 | |
| 248 | if (exp->scene_id) { |
| 249 | int ret; |
| 250 | |
| 251 | scn = expo_lookup_scene_id(exp, exp->scene_id); |
| 252 | if (!scn) |
| 253 | return log_msg_ret("scn", -ENOENT); |
| 254 | |
| 255 | ret = scene_send_key(scn, key, &exp->action); |
| 256 | if (ret) |
| 257 | return log_msg_ret("key", ret); |
Simon Glass | d7e32a8 | 2023-06-01 10:22:35 -0600 | [diff] [blame] | 258 | |
| 259 | /* arrange it to get any changes */ |
| 260 | ret = scene_arrange(scn); |
| 261 | if (ret) |
| 262 | return log_msg_ret("arr", ret); |
Simon Glass | d8adbe9 | 2023-01-06 08:52:36 -0600 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | return scn ? 0 : -ECHILD; |
| 266 | } |
| 267 | |
| 268 | int expo_action_get(struct expo *exp, struct expo_action *act) |
| 269 | { |
| 270 | *act = exp->action; |
| 271 | exp->action.type = EXPOACT_NONE; |
| 272 | |
| 273 | return act->type == EXPOACT_NONE ? -EAGAIN : 0; |
| 274 | } |
Simon Glass | c999e17 | 2023-06-01 10:22:53 -0600 | [diff] [blame] | 275 | |
| 276 | int expo_apply_theme(struct expo *exp, ofnode node) |
| 277 | { |
| 278 | struct scene *scn; |
| 279 | struct expo_theme *theme = &exp->theme; |
| 280 | int ret; |
| 281 | |
| 282 | log_debug("Applying theme %s\n", ofnode_get_name(node)); |
| 283 | |
| 284 | memset(theme, '\0', sizeof(struct expo_theme)); |
| 285 | ofnode_read_u32(node, "font-size", &theme->font_size); |
Simon Glass | 86f1ac5 | 2023-06-01 10:23:00 -0600 | [diff] [blame] | 286 | ofnode_read_u32(node, "menu-inset", &theme->menu_inset); |
| 287 | ofnode_read_u32(node, "menuitem-gap-y", &theme->menuitem_gap_y); |
Simon Glass | 377f18e | 2024-10-14 16:31:55 -0600 | [diff] [blame] | 288 | ofnode_read_u32(node, "menu-title-margin-x", |
| 289 | &theme->menu_title_margin_x); |
Simon Glass | c999e17 | 2023-06-01 10:22:53 -0600 | [diff] [blame] | 290 | |
| 291 | list_for_each_entry(scn, &exp->scene_head, sibling) { |
| 292 | ret = scene_apply_theme(scn, theme); |
| 293 | if (ret) |
| 294 | return log_msg_ret("app", ret); |
| 295 | } |
| 296 | |
| 297 | return 0; |
| 298 | } |
Simon Glass | e90acd8 | 2023-08-14 16:40:23 -0600 | [diff] [blame] | 299 | |
| 300 | int expo_iter_scene_objs(struct expo *exp, expo_scene_obj_iterator iter, |
| 301 | void *priv) |
| 302 | { |
| 303 | struct scene *scn; |
| 304 | int ret; |
| 305 | |
| 306 | list_for_each_entry(scn, &exp->scene_head, sibling) { |
| 307 | ret = scene_iter_objs(scn, iter, priv); |
| 308 | if (ret) |
| 309 | return log_msg_ret("wr", ret); |
| 310 | } |
| 311 | |
| 312 | return 0; |
| 313 | } |
Simon Glass | 137b442 | 2025-05-02 08:46:16 -0600 | [diff] [blame] | 314 | |
| 315 | int expo_poll(struct expo *exp, struct expo_action *act) |
| 316 | { |
| 317 | int ichar, key, ret; |
| 318 | |
| 319 | ret = expo_render(exp); |
| 320 | if (ret) |
| 321 | return log_msg_ret("ere", ret); |
| 322 | |
| 323 | ichar = cli_ch_process(&exp->cch, 0); |
| 324 | if (!ichar) { |
| 325 | while (!ichar && !tstc()) { |
| 326 | schedule(); |
| 327 | mdelay(2); |
| 328 | ichar = cli_ch_process(&exp->cch, -ETIMEDOUT); |
| 329 | } |
| 330 | if (!ichar) { |
| 331 | ichar = getchar(); |
| 332 | ichar = cli_ch_process(&exp->cch, ichar); |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | key = 0; |
| 337 | if (ichar) { |
| 338 | key = bootmenu_conv_key(ichar); |
| 339 | if (key == BKEY_NONE || key >= BKEY_FIRST_EXTRA) |
| 340 | key = ichar; |
| 341 | } |
| 342 | if (!key) |
| 343 | return -EAGAIN; |
| 344 | |
| 345 | ret = expo_send_key(exp, key); |
| 346 | if (ret) |
| 347 | return log_msg_ret("epk", ret); |
| 348 | ret = expo_action_get(exp, act); |
| 349 | if (ret) |
| 350 | return log_msg_ret("eag", ret); |
| 351 | |
| 352 | return 0; |
| 353 | } |