blob: 06d7e9fc913a2e96df0fa27deb7cca01436b3e1d [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,
105 menu->obj.dim.x + 200, label->dim.y);
106 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) {
189 menu->obj.dim.w = bbox.x1 - bbox.x0;
190 menu->obj.dim.h = bbox.y1 - bbox.y0;
191 }
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 Glassd353b752023-06-01 10:22:55 -0600208 x = menu->obj.dim.x;
Simon Glass7b043952023-06-01 10:22:49 -0600209 y = menu->obj.dim.y;
Simon Glass9f513932023-01-06 08:52:38 -0600210 if (menu->title_id) {
Simon Glass377f18e2024-10-14 16:31:55 -0600211 int width;
212
Simon Glass7b043952023-06-01 10:22:49 -0600213 ret = scene_obj_set_pos(scn, menu->title_id, menu->obj.dim.x, 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
409 menu_point_to_item(menu, item->id);
410
411 return 0;
412}
413
414int scene_menuitem(struct scene *scn, uint menu_id, const char *name, uint id,
415 uint key_id, uint label_id, uint desc_id, uint preview_id,
416 uint flags, struct scene_menitem **itemp)
417{
418 struct scene_obj_menu *menu;
419 struct scene_menitem *item;
Simon Glass9f513932023-01-06 08:52:38 -0600420
421 menu = scene_obj_find(scn, menu_id, SCENEOBJT_MENU);
422 if (!menu)
423 return log_msg_ret("find", -ENOENT);
424
425 /* Check that the text ID is valid */
Simon Glassd353b752023-06-01 10:22:55 -0600426 if (!scene_obj_find(scn, label_id, SCENEOBJT_TEXT))
Simon Glass9f513932023-01-06 08:52:38 -0600427 return log_msg_ret("txt", -EINVAL);
428
Dan Carpenter463be542023-07-31 17:08:29 +0300429 item = calloc(1, sizeof(struct scene_menitem));
Simon Glass9f513932023-01-06 08:52:38 -0600430 if (!item)
431 return log_msg_ret("item", -ENOMEM);
432 item->name = strdup(name);
433 if (!item->name) {
434 free(item);
435 return log_msg_ret("name", -ENOMEM);
436 }
437
438 item->id = resolve_id(scn->expo, id);
439 item->key_id = key_id;
440 item->label_id = label_id;
441 item->desc_id = desc_id;
442 item->preview_id = preview_id;
443 item->flags = flags;
Simon Glass100389f2024-10-14 16:31:58 -0600444 item->value = INT_MAX;
Simon Glass9f513932023-01-06 08:52:38 -0600445 list_add_tail(&item->sibling, &menu->item_head);
446
Simon Glass9f513932023-01-06 08:52:38 -0600447 if (itemp)
448 *itemp = item;
449
450 return item->id;
451}
452
453int scene_menu_set_title(struct scene *scn, uint id, uint title_id)
454{
455 struct scene_obj_menu *menu;
456 struct scene_obj_txt *txt;
457
458 menu = scene_obj_find(scn, id, SCENEOBJT_MENU);
459 if (!menu)
460 return log_msg_ret("menu", -ENOENT);
461
462 /* Check that the ID is valid */
463 if (title_id) {
464 txt = scene_obj_find(scn, title_id, SCENEOBJT_TEXT);
465 if (!txt)
466 return log_msg_ret("txt", -EINVAL);
467 }
468
469 menu->title_id = title_id;
470
471 return 0;
472}
473
474int scene_menu_set_pointer(struct scene *scn, uint id, uint pointer_id)
475{
476 struct scene_obj_menu *menu;
477 struct scene_obj *obj;
478
479 menu = scene_obj_find(scn, id, SCENEOBJT_MENU);
480 if (!menu)
481 return log_msg_ret("menu", -ENOENT);
482
483 /* Check that the ID is valid */
484 if (pointer_id) {
485 obj = scene_obj_find(scn, pointer_id, SCENEOBJT_NONE);
486 if (!obj)
487 return log_msg_ret("obj", -EINVAL);
488 }
489
490 menu->pointer_id = pointer_id;
491
492 return 0;
493}
494
Simon Glassbe04b962025-05-02 08:46:24 -0600495int scene_menu_select_item(struct scene *scn, uint id, uint cur_item_id)
496{
497 struct scene_obj_menu *menu;
498 int ret;
499
500 menu = scene_obj_find(scn, id, SCENEOBJT_MENU);
501 if (!menu)
502 return log_msg_ret("menu", -ENOENT);
503
504 ret = menu_point_to_item(menu, cur_item_id);
505 if (ret)
506 return log_msg_ret("msi", ret);
507
508 return 0;
509}
510
511int scene_menu_get_cur_item(struct scene *scn, uint id)
512{
513 struct scene_obj_menu *menu;
514
515 menu = scene_obj_find(scn, id, SCENEOBJT_MENU);
516 if (!menu)
517 return log_msg_ret("menu", -ENOENT);
518
519 return menu->cur_item_id;
520}
521
Simon Glass9f513932023-01-06 08:52:38 -0600522int scene_menu_display(struct scene_obj_menu *menu)
523{
524 struct scene *scn = menu->obj.scene;
525 struct scene_obj_txt *pointer;
526 struct expo *exp = scn->expo;
527 struct scene_menitem *item;
528 const char *pstr;
529
530 printf("U-Boot : Boot Menu\n\n");
531 if (menu->title_id) {
532 struct scene_obj_txt *txt;
533 const char *str;
534
535 txt = scene_obj_find(scn, menu->title_id, SCENEOBJT_TEXT);
536 if (!txt)
537 return log_msg_ret("txt", -EINVAL);
538
539 str = expo_get_str(exp, txt->str_id);
540 printf("%s\n\n", str);
541 }
542
543 if (list_empty(&menu->item_head))
544 return 0;
545
546 pointer = scene_obj_find(scn, menu->pointer_id, SCENEOBJT_TEXT);
547 pstr = expo_get_str(scn->expo, pointer->str_id);
548
549 list_for_each_entry(item, &menu->item_head, sibling) {
550 struct scene_obj_txt *key = NULL, *label = NULL;
551 struct scene_obj_txt *desc = NULL;
552 const char *kstr = NULL, *lstr = NULL, *dstr = NULL;
553
554 key = scene_obj_find(scn, item->key_id, SCENEOBJT_TEXT);
555 if (key)
556 kstr = expo_get_str(exp, key->str_id);
557
558 label = scene_obj_find(scn, item->label_id, SCENEOBJT_TEXT);
559 if (label)
560 lstr = expo_get_str(exp, label->str_id);
561
562 desc = scene_obj_find(scn, item->desc_id, SCENEOBJT_TEXT);
563 if (desc)
564 dstr = expo_get_str(exp, desc->str_id);
565
566 printf("%3s %3s %-10s %s\n",
567 pointer && menu->cur_item_id == item->id ? pstr : "",
568 kstr, lstr, dstr);
569 }
570
571 return -ENOTSUPP;
572}
Simon Glass01922ec2023-06-01 10:22:57 -0600573
Simon Glass12f57732023-06-01 10:22:58 -0600574int scene_menu_render_deps(struct scene *scn, struct scene_obj_menu *menu)
575{
576 struct scene_menitem *item;
577
578 scene_render_deps(scn, menu->title_id);
579 scene_render_deps(scn, menu->cur_item_id);
580 scene_render_deps(scn, menu->pointer_id);
581
582 list_for_each_entry(item, &menu->item_head, sibling) {
583 scene_render_deps(scn, item->key_id);
584 scene_render_deps(scn, item->label_id);
585 scene_render_deps(scn, item->desc_id);
586 }
587
588 return 0;
589}