blob: c47c20305275eff4ba54cd1edc3f1f9775d8790e [file] [log] [blame]
Simon Glass9f513932023-01-06 08:52:38 -06001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Implementation of a menu in a scene
4 *
5 * Copyright 2022 Google LLC
6 * Written by Simon Glass <sjg@chromium.org>
7 */
8
Simon Glassfe4c1e22023-06-01 10:22:43 -06009#define LOG_CATEGORY LOGC_EXPO
Simon Glass9f513932023-01-06 08:52:38 -060010
Simon Glass9f513932023-01-06 08:52:38 -060011#include <dm.h>
12#include <expo.h>
13#include <malloc.h>
14#include <mapmem.h>
15#include <menu.h>
16#include <video.h>
17#include <video_console.h>
18#include <linux/input.h>
19#include "scene_internal.h"
20
21static void scene_menuitem_destroy(struct scene_menitem *item)
22{
23 free(item->name);
24 free(item);
25}
26
27void scene_menu_destroy(struct scene_obj_menu *menu)
28{
29 struct scene_menitem *item, *next;
30
31 list_for_each_entry_safe(item, next, &menu->item_head, sibling)
32 scene_menuitem_destroy(item);
33}
34
Simon Glass5fd4f782023-08-14 16:40:32 -060035struct scene_menitem *scene_menuitem_find(const struct scene_obj_menu *menu,
36 int id)
Simon Glassc55eeba2023-06-01 10:22:54 -060037{
38 struct scene_menitem *item;
39
40 list_for_each_entry(item, &menu->item_head, sibling) {
41 if (item->id == id)
42 return item;
43 }
44
45 return NULL;
46}
47
Simon Glass4462fa32023-08-14 16:40:38 -060048struct scene_menitem *scene_menuitem_find_seq(const struct scene_obj_menu *menu,
49 uint seq)
50{
51 struct scene_menitem *item;
52 uint i;
53
54 i = 0;
55 list_for_each_entry(item, &menu->item_head, sibling) {
56 if (i == seq)
57 return item;
58 i++;
59 }
60
61 return NULL;
62}
63
Simon Glass100389f2024-10-14 16:31:58 -060064struct scene_menitem *scene_menuitem_find_val(const struct scene_obj_menu *menu,
65 int val)
66{
67 struct scene_menitem *item;
68 uint i;
69
70 i = 0;
71 list_for_each_entry(item, &menu->item_head, sibling) {
Simon Glass6f3e87a2024-10-14 16:32:00 -060072 if (item->value == INT_MAX ? val == i : item->value == val)
Simon Glass100389f2024-10-14 16:31:58 -060073 return item;
74 i++;
75 }
76
77 return NULL;
78}
79
Simon Glassc55eeba2023-06-01 10:22:54 -060080/**
81 * update_pointers() - Update the pointer object and handle highlights
82 *
83 * @menu: Menu to update
84 * @id: ID of menu item to select/deselect
85 * @point: true if @id is being selected, false if it is being deselected
86 */
87static int update_pointers(struct scene_obj_menu *menu, uint id, bool point)
88{
89 struct scene *scn = menu->obj.scene;
Simon Glassd353b752023-06-01 10:22:55 -060090 const bool stack = scn->expo->popup;
Simon Glassc55eeba2023-06-01 10:22:54 -060091 const struct scene_menitem *item;
92 int ret;
93
94 item = scene_menuitem_find(menu, id);
95 if (!item)
96 return log_msg_ret("itm", -ENOENT);
97
98 /* adjust the pointer object to point to the selected item */
99 if (menu->pointer_id && item && point) {
100 struct scene_obj *label;
101
102 label = scene_obj_find(scn, item->label_id, SCENEOBJT_NONE);
103
104 ret = scene_obj_set_pos(scn, menu->pointer_id,
Simon Glassbc3a15f2025-05-02 08:46:31 -0600105 menu->obj.bbox.x0 + 200, label->bbox.y0);
Simon Glassc55eeba2023-06-01 10:22:54 -0600106 if (ret < 0)
107 return log_msg_ret("ptr", ret);
108 }
109
Simon Glassd353b752023-06-01 10:22:55 -0600110 if (stack) {
111 point &= scn->highlight_id == menu->obj.id;
112 scene_obj_flag_clrset(scn, item->label_id, SCENEOF_POINT,
113 point ? SCENEOF_POINT : 0);
114 }
115
Simon Glassc55eeba2023-06-01 10:22:54 -0600116 return 0;
117}
118
Simon Glass9f513932023-01-06 08:52:38 -0600119/**
120 * menu_point_to_item() - Point to a particular menu item
121 *
122 * Sets the currently pointed-to / highlighted menu item
123 */
Simon Glassbe04b962025-05-02 08:46:24 -0600124static int menu_point_to_item(struct scene_obj_menu *menu, uint item_id)
Simon Glass9f513932023-01-06 08:52:38 -0600125{
Simon Glassbe04b962025-05-02 08:46:24 -0600126 int ret;
127
128 if (menu->cur_item_id) {
129 ret = update_pointers(menu, menu->cur_item_id, false);
130 if (ret)
131 return log_msg_ret("mpi", ret);
132 }
Simon Glass9f513932023-01-06 08:52:38 -0600133 menu->cur_item_id = item_id;
Simon Glassbe04b962025-05-02 08:46:24 -0600134 ret = update_pointers(menu, item_id, true);
135 if (ret)
136 return log_msg_ret("mpu", ret);
137
138 return 0;
Simon Glass9f513932023-01-06 08:52:38 -0600139}
140
Simon Glassf0994692023-10-01 19:13:29 -0600141void scene_menu_calc_bbox(struct scene_obj_menu *menu,
142 struct vidconsole_bbox *bbox,
143 struct vidconsole_bbox *label_bbox)
Simon Glass7a960052023-06-01 10:22:52 -0600144{
Simon Glass86f1ac52023-06-01 10:23:00 -0600145 const struct expo_theme *theme = &menu->obj.scene->expo->theme;
Simon Glass7a960052023-06-01 10:22:52 -0600146 const struct scene_menitem *item;
147
148 bbox->valid = false;
Simon Glass86f1ac52023-06-01 10:23:00 -0600149 scene_bbox_union(menu->obj.scene, menu->title_id, 0, bbox);
Simon Glass7a960052023-06-01 10:22:52 -0600150
151 label_bbox->valid = false;
152
153 list_for_each_entry(item, &menu->item_head, sibling) {
Simon Glass86f1ac52023-06-01 10:23:00 -0600154 scene_bbox_union(menu->obj.scene, item->label_id,
155 theme->menu_inset, bbox);
156 scene_bbox_union(menu->obj.scene, item->key_id, 0, bbox);
157 scene_bbox_union(menu->obj.scene, item->desc_id, 0, bbox);
158 scene_bbox_union(menu->obj.scene, item->preview_id, 0, bbox);
Simon Glass7a960052023-06-01 10:22:52 -0600159
160 /* Get the bounding box of all labels */
Simon Glass86f1ac52023-06-01 10:23:00 -0600161 scene_bbox_union(menu->obj.scene, item->label_id,
162 theme->menu_inset, label_bbox);
Simon Glass7a960052023-06-01 10:22:52 -0600163 }
Simon Glass86f1ac52023-06-01 10:23:00 -0600164
165 /*
166 * subtract the final menuitem's gap to keep the insert the same top
167 * and bottom
168 */
169 label_bbox->y1 -= theme->menuitem_gap_y;
Simon Glass7a960052023-06-01 10:22:52 -0600170}
171
172int scene_menu_calc_dims(struct scene_obj_menu *menu)
173{
174 struct vidconsole_bbox bbox, label_bbox;
175 const struct scene_menitem *item;
176
177 scene_menu_calc_bbox(menu, &bbox, &label_bbox);
178
179 /* Make all labels the same size */
180 if (label_bbox.valid) {
181 list_for_each_entry(item, &menu->item_head, sibling) {
182 scene_obj_set_size(menu->obj.scene, item->label_id,
183 label_bbox.x1 - label_bbox.x0,
184 label_bbox.y1 - label_bbox.y0);
185 }
186 }
187
188 if (bbox.valid) {
Simon Glass854ca692025-05-02 08:46:30 -0600189 menu->obj.bbox.w = bbox.x1 - bbox.x0;
190 menu->obj.bbox.h = bbox.y1 - bbox.y0;
Simon Glass7a960052023-06-01 10:22:52 -0600191 }
192
193 return 0;
194}
195
Simon Glass377f18e2024-10-14 16:31:55 -0600196int scene_menu_arrange(struct scene *scn, struct expo_arrange_info *arr,
197 struct scene_obj_menu *menu)
Simon Glass9f513932023-01-06 08:52:38 -0600198{
Simon Glassd353b752023-06-01 10:22:55 -0600199 const bool open = menu->obj.flags & SCENEOF_OPEN;
200 struct expo *exp = scn->expo;
201 const bool stack = exp->popup;
Simon Glass86f1ac52023-06-01 10:23:00 -0600202 const struct expo_theme *theme = &exp->theme;
Simon Glass9f513932023-01-06 08:52:38 -0600203 struct scene_menitem *item;
Simon Glassc55eeba2023-06-01 10:22:54 -0600204 uint sel_id;
Simon Glassd353b752023-06-01 10:22:55 -0600205 int x, y;
Simon Glass9f513932023-01-06 08:52:38 -0600206 int ret;
207
Simon Glassbc3a15f2025-05-02 08:46:31 -0600208 x = menu->obj.bbox.x0;
209 y = menu->obj.bbox.y0;
Simon Glass9f513932023-01-06 08:52:38 -0600210 if (menu->title_id) {
Simon Glass377f18e2024-10-14 16:31:55 -0600211 int width;
212
Simon Glassbc3a15f2025-05-02 08:46:31 -0600213 ret = scene_obj_set_pos(scn, menu->title_id, menu->obj.bbox.x0, y);
Simon Glass9f513932023-01-06 08:52:38 -0600214 if (ret < 0)
215 return log_msg_ret("tit", ret);
216
Simon Glass377f18e2024-10-14 16:31:55 -0600217 ret = scene_obj_get_hw(scn, menu->title_id, &width);
Simon Glass9f513932023-01-06 08:52:38 -0600218 if (ret < 0)
219 return log_msg_ret("hei", ret);
220
Simon Glassd353b752023-06-01 10:22:55 -0600221 if (stack)
Simon Glass377f18e2024-10-14 16:31:55 -0600222 x += arr->label_width + theme->menu_title_margin_x;
Simon Glassd353b752023-06-01 10:22:55 -0600223 else
224 y += ret * 2;
Simon Glass9f513932023-01-06 08:52:38 -0600225 }
226
227 /*
228 * Currently everything is hard-coded to particular columns so this
229 * won't work on small displays and looks strange if the font size is
230 * small. This can be updated once text measuring is supported in
231 * vidconsole
232 */
Simon Glassc55eeba2023-06-01 10:22:54 -0600233 sel_id = menu->cur_item_id;
Simon Glass9f513932023-01-06 08:52:38 -0600234 list_for_each_entry(item, &menu->item_head, sibling) {
Simon Glassd353b752023-06-01 10:22:55 -0600235 bool selected;
Simon Glass9f513932023-01-06 08:52:38 -0600236 int height;
237
Simon Glassd353b752023-06-01 10:22:55 -0600238 ret = scene_obj_get_hw(scn, item->label_id, NULL);
Simon Glass9f513932023-01-06 08:52:38 -0600239 if (ret < 0)
240 return log_msg_ret("get", ret);
241 height = ret;
242
243 if (item->flags & SCENEMIF_GAP_BEFORE)
244 y += height;
245
246 /* select an item if not done already */
Simon Glassc55eeba2023-06-01 10:22:54 -0600247 if (!sel_id)
248 sel_id = item->id;
Simon Glass9f513932023-01-06 08:52:38 -0600249
Simon Glassd353b752023-06-01 10:22:55 -0600250 selected = sel_id == item->id;
251
Simon Glass9f513932023-01-06 08:52:38 -0600252 /*
253 * Put the label on the left, then leave a space for the
254 * pointer, then the key and the description
255 */
Simon Glass86f1ac52023-06-01 10:23:00 -0600256 ret = scene_obj_set_pos(scn, item->label_id,
257 x + theme->menu_inset, y);
Simon Glass9f513932023-01-06 08:52:38 -0600258 if (ret < 0)
Simon Glassd353b752023-06-01 10:22:55 -0600259 return log_msg_ret("nam", ret);
260 scene_obj_set_hide(scn, item->label_id,
261 stack && !open && !selected);
Simon Glass9f513932023-01-06 08:52:38 -0600262
Simon Glassd353b752023-06-01 10:22:55 -0600263 if (item->key_id) {
264 ret = scene_obj_set_pos(scn, item->key_id, x + 230, y);
265 if (ret < 0)
266 return log_msg_ret("key", ret);
267 }
Simon Glass9f513932023-01-06 08:52:38 -0600268
Simon Glassd353b752023-06-01 10:22:55 -0600269 if (item->desc_id) {
270 ret = scene_obj_set_pos(scn, item->desc_id, x + 280, y);
271 if (ret < 0)
272 return log_msg_ret("des", ret);
273 }
Simon Glass9f513932023-01-06 08:52:38 -0600274
275 if (item->preview_id) {
276 bool hide;
277
278 /*
279 * put all previews on top of each other, on the right
280 * size of the display
281 */
282 ret = scene_obj_set_pos(scn, item->preview_id, -4, y);
283 if (ret < 0)
284 return log_msg_ret("prev", ret);
285
286 hide = menu->cur_item_id != item->id;
287 ret = scene_obj_set_hide(scn, item->preview_id, hide);
288 if (ret < 0)
289 return log_msg_ret("hid", ret);
290 }
291
Simon Glassd353b752023-06-01 10:22:55 -0600292 if (!stack || open)
Simon Glass86f1ac52023-06-01 10:23:00 -0600293 y += height + theme->menuitem_gap_y;
Simon Glass9f513932023-01-06 08:52:38 -0600294 }
295
Simon Glassc55eeba2023-06-01 10:22:54 -0600296 if (sel_id)
297 menu_point_to_item(menu, sel_id);
Simon Glass9f513932023-01-06 08:52:38 -0600298
299 return 0;
300}
301
302int scene_menu(struct scene *scn, const char *name, uint id,
303 struct scene_obj_menu **menup)
304{
305 struct scene_obj_menu *menu;
306 int ret;
307
308 ret = scene_obj_add(scn, name, id, SCENEOBJT_MENU,
309 sizeof(struct scene_obj_menu),
310 (struct scene_obj **)&menu);
311 if (ret < 0)
312 return log_msg_ret("obj", -ENOMEM);
313
314 if (menup)
315 *menup = menu;
316 INIT_LIST_HEAD(&menu->item_head);
317
Simon Glass9f513932023-01-06 08:52:38 -0600318 return menu->obj.id;
319}
320
321static struct scene_menitem *scene_menu_find_key(struct scene *scn,
322 struct scene_obj_menu *menu,
323 int key)
324{
325 struct scene_menitem *item;
326
327 list_for_each_entry(item, &menu->item_head, sibling) {
328 if (item->key_id) {
329 struct scene_obj_txt *txt;
330 const char *str;
331
332 txt = scene_obj_find(scn, item->key_id, SCENEOBJT_TEXT);
333 if (txt) {
334 str = expo_get_str(scn->expo, txt->str_id);
335 if (str && *str == key)
336 return item;
337 }
338 }
339 }
340
341 return NULL;
342}
343
344int scene_menu_send_key(struct scene *scn, struct scene_obj_menu *menu, int key,
345 struct expo_action *event)
346{
Simon Glassf0e1e8c2023-06-01 10:22:59 -0600347 const bool open = menu->obj.flags & SCENEOF_OPEN;
Simon Glass9f513932023-01-06 08:52:38 -0600348 struct scene_menitem *item, *cur, *key_item;
349
350 cur = NULL;
351 key_item = NULL;
352
353 if (!list_empty(&menu->item_head)) {
354 list_for_each_entry(item, &menu->item_head, sibling) {
355 /* select an item if not done already */
356 if (menu->cur_item_id == item->id) {
357 cur = item;
358 break;
359 }
360 }
361 }
362
363 if (!cur)
364 return -ENOTTY;
365
366 switch (key) {
367 case BKEY_UP:
368 if (item != list_first_entry(&menu->item_head,
369 struct scene_menitem, sibling)) {
370 item = list_entry(item->sibling.prev,
371 struct scene_menitem, sibling);
Simon Glass719a3c62023-06-01 10:22:56 -0600372 event->type = EXPOACT_POINT_ITEM;
Simon Glass9f513932023-01-06 08:52:38 -0600373 event->select.id = item->id;
374 log_debug("up to item %d\n", event->select.id);
375 }
376 break;
377 case BKEY_DOWN:
378 if (!list_is_last(&item->sibling, &menu->item_head)) {
379 item = list_entry(item->sibling.next,
380 struct scene_menitem, sibling);
Simon Glass719a3c62023-06-01 10:22:56 -0600381 event->type = EXPOACT_POINT_ITEM;
Simon Glass9f513932023-01-06 08:52:38 -0600382 event->select.id = item->id;
383 log_debug("down to item %d\n", event->select.id);
384 }
385 break;
386 case BKEY_SELECT:
387 event->type = EXPOACT_SELECT;
388 event->select.id = item->id;
389 log_debug("select item %d\n", event->select.id);
390 break;
391 case BKEY_QUIT:
Simon Glassf0e1e8c2023-06-01 10:22:59 -0600392 if (scn->expo->popup && open) {
393 event->type = EXPOACT_CLOSE;
394 event->select.id = menu->obj.id;
395 } else {
396 event->type = EXPOACT_QUIT;
397 log_debug("menu quit\n");
398 }
Simon Glass9f513932023-01-06 08:52:38 -0600399 break;
400 case '0'...'9':
401 key_item = scene_menu_find_key(scn, menu, key);
402 if (key_item) {
403 event->type = EXPOACT_SELECT;
404 event->select.id = key_item->id;
405 }
406 break;
407 }
408
Simon Glass9f513932023-01-06 08:52:38 -0600409 return 0;
410}
411
412int scene_menuitem(struct scene *scn, uint menu_id, const char *name, uint id,
413 uint key_id, uint label_id, uint desc_id, uint preview_id,
414 uint flags, struct scene_menitem **itemp)
415{
416 struct scene_obj_menu *menu;
417 struct scene_menitem *item;
Simon Glass9f513932023-01-06 08:52:38 -0600418
419 menu = scene_obj_find(scn, menu_id, SCENEOBJT_MENU);
420 if (!menu)
421 return log_msg_ret("find", -ENOENT);
422
423 /* Check that the text ID is valid */
Simon Glassd353b752023-06-01 10:22:55 -0600424 if (!scene_obj_find(scn, label_id, SCENEOBJT_TEXT))
Simon Glass9f513932023-01-06 08:52:38 -0600425 return log_msg_ret("txt", -EINVAL);
426
Dan Carpenter463be542023-07-31 17:08:29 +0300427 item = calloc(1, sizeof(struct scene_menitem));
Simon Glass9f513932023-01-06 08:52:38 -0600428 if (!item)
429 return log_msg_ret("item", -ENOMEM);
430 item->name = strdup(name);
431 if (!item->name) {
432 free(item);
433 return log_msg_ret("name", -ENOMEM);
434 }
435
436 item->id = resolve_id(scn->expo, id);
437 item->key_id = key_id;
438 item->label_id = label_id;
439 item->desc_id = desc_id;
440 item->preview_id = preview_id;
441 item->flags = flags;
Simon Glass100389f2024-10-14 16:31:58 -0600442 item->value = INT_MAX;
Simon Glass9f513932023-01-06 08:52:38 -0600443 list_add_tail(&item->sibling, &menu->item_head);
444
Simon Glass9f513932023-01-06 08:52:38 -0600445 if (itemp)
446 *itemp = item;
447
448 return item->id;
449}
450
451int scene_menu_set_title(struct scene *scn, uint id, uint title_id)
452{
453 struct scene_obj_menu *menu;
454 struct scene_obj_txt *txt;
455
456 menu = scene_obj_find(scn, id, SCENEOBJT_MENU);
457 if (!menu)
458 return log_msg_ret("menu", -ENOENT);
459
460 /* Check that the ID is valid */
461 if (title_id) {
462 txt = scene_obj_find(scn, title_id, SCENEOBJT_TEXT);
463 if (!txt)
464 return log_msg_ret("txt", -EINVAL);
465 }
466
467 menu->title_id = title_id;
468
469 return 0;
470}
471
472int scene_menu_set_pointer(struct scene *scn, uint id, uint pointer_id)
473{
474 struct scene_obj_menu *menu;
475 struct scene_obj *obj;
476
477 menu = scene_obj_find(scn, id, SCENEOBJT_MENU);
478 if (!menu)
479 return log_msg_ret("menu", -ENOENT);
480
481 /* Check that the ID is valid */
482 if (pointer_id) {
483 obj = scene_obj_find(scn, pointer_id, SCENEOBJT_NONE);
484 if (!obj)
485 return log_msg_ret("obj", -EINVAL);
486 }
487
488 menu->pointer_id = pointer_id;
489
490 return 0;
491}
492
Simon Glassbe04b962025-05-02 08:46:24 -0600493int scene_menu_select_item(struct scene *scn, uint id, uint cur_item_id)
494{
495 struct scene_obj_menu *menu;
496 int ret;
497
498 menu = scene_obj_find(scn, id, SCENEOBJT_MENU);
499 if (!menu)
500 return log_msg_ret("menu", -ENOENT);
501
502 ret = menu_point_to_item(menu, cur_item_id);
503 if (ret)
504 return log_msg_ret("msi", ret);
505
506 return 0;
507}
508
509int scene_menu_get_cur_item(struct scene *scn, uint id)
510{
511 struct scene_obj_menu *menu;
512
513 menu = scene_obj_find(scn, id, SCENEOBJT_MENU);
514 if (!menu)
515 return log_msg_ret("menu", -ENOENT);
516
517 return menu->cur_item_id;
518}
519
Simon Glass9f513932023-01-06 08:52:38 -0600520int scene_menu_display(struct scene_obj_menu *menu)
521{
522 struct scene *scn = menu->obj.scene;
523 struct scene_obj_txt *pointer;
524 struct expo *exp = scn->expo;
525 struct scene_menitem *item;
526 const char *pstr;
527
528 printf("U-Boot : Boot Menu\n\n");
529 if (menu->title_id) {
530 struct scene_obj_txt *txt;
531 const char *str;
532
533 txt = scene_obj_find(scn, menu->title_id, SCENEOBJT_TEXT);
534 if (!txt)
535 return log_msg_ret("txt", -EINVAL);
536
537 str = expo_get_str(exp, txt->str_id);
538 printf("%s\n\n", str);
539 }
540
541 if (list_empty(&menu->item_head))
542 return 0;
543
544 pointer = scene_obj_find(scn, menu->pointer_id, SCENEOBJT_TEXT);
545 pstr = expo_get_str(scn->expo, pointer->str_id);
546
547 list_for_each_entry(item, &menu->item_head, sibling) {
548 struct scene_obj_txt *key = NULL, *label = NULL;
549 struct scene_obj_txt *desc = NULL;
550 const char *kstr = NULL, *lstr = NULL, *dstr = NULL;
551
552 key = scene_obj_find(scn, item->key_id, SCENEOBJT_TEXT);
553 if (key)
554 kstr = expo_get_str(exp, key->str_id);
555
556 label = scene_obj_find(scn, item->label_id, SCENEOBJT_TEXT);
557 if (label)
558 lstr = expo_get_str(exp, label->str_id);
559
560 desc = scene_obj_find(scn, item->desc_id, SCENEOBJT_TEXT);
561 if (desc)
562 dstr = expo_get_str(exp, desc->str_id);
563
564 printf("%3s %3s %-10s %s\n",
565 pointer && menu->cur_item_id == item->id ? pstr : "",
566 kstr, lstr, dstr);
567 }
568
569 return -ENOTSUPP;
570}
Simon Glass01922ec2023-06-01 10:22:57 -0600571
Simon Glass12f57732023-06-01 10:22:58 -0600572int scene_menu_render_deps(struct scene *scn, struct scene_obj_menu *menu)
573{
574 struct scene_menitem *item;
575
576 scene_render_deps(scn, menu->title_id);
577 scene_render_deps(scn, menu->cur_item_id);
578 scene_render_deps(scn, menu->pointer_id);
579
580 list_for_each_entry(item, &menu->item_head, sibling) {
581 scene_render_deps(scn, item->key_id);
582 scene_render_deps(scn, item->label_id);
583 scene_render_deps(scn, item->desc_id);
584 }
585
586 return 0;
587}