1 /* SPDX-License-Identifier: GPL-2.0+ */
2 /*
3  * Copyright 2010-2011 Calxeda, Inc.
4  */
5 
6 #ifndef __MENU_H__
7 #define __MENU_H__
8 
9 struct cli_ch_state;
10 struct menu;
11 
12 struct menu *menu_create(char *title, int timeout, int prompt,
13 				void (*display_statusline)(struct menu *),
14 				void (*item_data_print)(void *),
15 				char *(*item_choice)(void *),
16 				bool (*need_reprint)(void *),
17 				void *item_choice_data);
18 int menu_default_set(struct menu *m, char *item_key);
19 int menu_get_choice(struct menu *m, void **choice);
20 int menu_item_add(struct menu *m, char *item_key, void *item_data);
21 int menu_destroy(struct menu *m);
22 int menu_default_choice(struct menu *m, void **choice);
23 
24 /**
25  * menu_show() Show a boot menu
26  *
27  * This shows a menu and lets the user select an option. The menu is defined by
28  * environment variables (see README.bootmenu).
29  *
30  * This function doesn't normally return, but if the users requests the command
31  * problem, it will.
32  *
33  * @bootdelay: Delay to wait before running the default menu option (0 to run
34  *		the entry immediately)
35  * Return: If it returns, it always returns -1 to indicate that the boot should
36  *	be aborted and the command prompt should be provided
37  */
38 int menu_show(int bootdelay);
39 
40 struct bootmenu_data {
41 	int delay;			/* delay for autoboot */
42 	int active;			/* active menu entry */
43 	int last_active;		/* last active menu entry */
44 	int count;			/* total count of menu entries */
45 	struct bootmenu_entry *first;	/* first menu entry */
46 };
47 
48 /** enum bootmenu_key - keys that can be returned by the bootmenu */
49 enum bootmenu_key {
50 	BKEY_NONE = 0,
51 	BKEY_UP,
52 	BKEY_DOWN,
53 	BKEY_SELECT,
54 	BKEY_QUIT,
55 	BKEY_SAVE,
56 
57 	/* shortcut key to select menu option directly */
58 	BKEY_SHORTCUT,
59 
60 	/* 'extra' keys, which are used by menus but not cedit */
61 	BKEY_PLUS,
62 	BKEY_MINUS,
63 	BKEY_SPACE,
64 
65 	BKEY_COUNT,
66 
67 	/* Keys from here on are not used by cedit */
68 	BKEY_FIRST_EXTRA = BKEY_PLUS,
69 };
70 
71 /**
72  * bootmenu_autoboot_loop() - handle autobooting if no key is pressed
73  *
74  * This shows a prompt to allow the user to press a key to interrupt auto boot
75  * of the first menu option.
76  *
77  * It then waits for the required time (menu->delay in seconds) for a key to be
78  * pressed. If nothing is pressed in that time, @key returns KEY_SELECT
79  * indicating that the current option should be chosen.
80  *
81  * @menu: Menu being processed
82  * @esc: Set to 1 if the escape key is pressed, otherwise not updated
83  * Returns: code for the key the user pressed:
84  *	enter: KEY_SELECT
85  *	Ctrl-C: KEY_QUIT
86  *	anything else: KEY_NONE
87  */
88 enum bootmenu_key bootmenu_autoboot_loop(struct bootmenu_data *menu,
89 					 struct cli_ch_state *cch);
90 
91 /**
92  * bootmenu_loop() - handle waiting for a keypress when autoboot is disabled
93  *
94  * This is used when the menu delay is negative, indicating that the delay has
95  * elapsed, or there was no delay to begin with.
96  *
97  * It reads a character and processes it, returning a menu-key code if a
98  * character is recognised
99  *
100  * @menu: Menu being processed
101  * @esc: On input, a non-zero value indicates that an escape sequence has
102  *	resulted in that many characters so far. On exit this is updated to the
103  *	new number of characters
104  * Returns: code for the key the user pressed:
105  *	enter: BKEY_SELECT
106  *	Ctrl-C: BKEY_QUIT
107  *	Up arrow: BKEY_UP
108  *	Down arrow: BKEY_DOWN
109  *	Escape (by itself): BKEY_QUIT
110  *	Plus: BKEY_PLUS
111  *	Minus: BKEY_MINUS
112  *	Space: BKEY_SPACE
113  */
114 enum bootmenu_key bootmenu_loop(struct bootmenu_data *menu,
115 				struct cli_ch_state *cch);
116 
117 /**
118  * bootmenu_conv_key() - Convert a U-Boot keypress into a menu key
119  *
120  * @ichar: Keypress to convert (ASCII, including control characters)
121  * Returns: Menu key that corresponds to @ichar, or BKEY_NONE if none
122  */
123 enum bootmenu_key bootmenu_conv_key(int ichar);
124 
125 #endif /* __MENU_H__ */
126