Commit b41bc5a82d8a67d347f2fc12cf2106b8a37e4336
Committed by
Wolfgang Denk
1 parent
b69bf52dfe
Exists in
master
and in
54 other branches
common, menu: use abortboot for menu timeout
Signed-off-by: Jason Hobbs <jason.hobbs@calxeda.com>
Showing 5 changed files with 47 additions and 14 deletions Side-by-side Diff
common/main.c
... | ... | @@ -88,7 +88,10 @@ |
88 | 88 | */ |
89 | 89 | #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0) |
90 | 90 | # if defined(CONFIG_AUTOBOOT_KEYED) |
91 | -static inline int abortboot(int bootdelay) | |
91 | +#ifndef CONFIG_MENU | |
92 | +static inline | |
93 | +#endif | |
94 | +int abortboot(int bootdelay) | |
92 | 95 | { |
93 | 96 | int abort = 0; |
94 | 97 | uint64_t etime = endtick(bootdelay); |
... | ... | @@ -202,7 +205,10 @@ |
202 | 205 | static int menukey = 0; |
203 | 206 | #endif |
204 | 207 | |
205 | -static inline int abortboot(int bootdelay) | |
208 | +#ifndef CONFIG_MENU | |
209 | +static inline | |
210 | +#endif | |
211 | +int abortboot(int bootdelay) | |
206 | 212 | { |
207 | 213 | int abort = 0; |
208 | 214 |
common/menu.c
... | ... | @@ -43,6 +43,7 @@ |
43 | 43 | */ |
44 | 44 | struct menu { |
45 | 45 | struct menu_item *default_item; |
46 | + int timeout; | |
46 | 47 | char *title; |
47 | 48 | int prompt; |
48 | 49 | void (*item_data_print)(void *); |
49 | 50 | |
50 | 51 | |
... | ... | @@ -158,13 +159,29 @@ |
158 | 159 | } |
159 | 160 | |
160 | 161 | /* |
162 | + * Wait for the user to hit a key according to the timeout set for the menu. | |
163 | + * Returns 1 if the user hit a key, or 0 if the timeout expired. | |
164 | + */ | |
165 | +static inline int menu_interrupted(struct menu *m) | |
166 | +{ | |
167 | + if (!m->timeout) | |
168 | + return 0; | |
169 | + | |
170 | + if (abortboot(m->timeout/10)) | |
171 | + return 1; | |
172 | + | |
173 | + return 0; | |
174 | +} | |
175 | + | |
176 | +/* | |
161 | 177 | * Checks whether or not the default menu item should be used without |
162 | - * prompting for a user choice. If the menu is set to always prompt, return | |
163 | - * 0. Otherwise, return 1 to indicate we should use the default menu item. | |
178 | + * prompting for a user choice. If the menu is set to always prompt, or the | |
179 | + * user hits a key during the timeout period, return 0. Otherwise, return 1 to | |
180 | + * indicate we should use the default menu item. | |
164 | 181 | */ |
165 | 182 | static inline int menu_use_default(struct menu *m) |
166 | 183 | { |
167 | - return !m->prompt; | |
184 | + return !m->prompt && !menu_interrupted(m); | |
168 | 185 | } |
169 | 186 | |
170 | 187 | /* |
... | ... | @@ -250,7 +267,8 @@ |
250 | 267 | |
251 | 268 | /* |
252 | 269 | * menu_get_choice() - Returns the user's selected menu entry, or the default |
253 | - * if the menu is set to not prompt. This is safe to call more than once. | |
270 | + * if the menu is set to not prompt or the timeout expires. This is safe to | |
271 | + * call more than once. | |
254 | 272 | * |
255 | 273 | * m - Points to a menu created by menu_create(). |
256 | 274 | * |
... | ... | @@ -259,8 +277,8 @@ |
259 | 277 | * written at the location it points to. |
260 | 278 | * |
261 | 279 | * Returns 1 if successful, -EINVAL if m or choice is NULL, -ENOENT if no |
262 | - * default has been set and the menu is set to not prompt, or -EINTR if the | |
263 | - * user exits the menu via ^c. | |
280 | + * default has been set and the menu is set to not prompt or the timeout | |
281 | + * expires, or -EINTR if the user exits the menu via ^c. | |
264 | 282 | */ |
265 | 283 | int menu_get_choice(struct menu *m, void **choice) |
266 | 284 | { |
267 | 285 | |
... | ... | @@ -330,8 +348,13 @@ |
330 | 348 | * list of menu items. It will be copied to internal storage, and is safe to |
331 | 349 | * discard after passing to menu_create(). |
332 | 350 | * |
333 | - * prompt - If 0, don't ask for user input. | |
351 | + * timeout - A delay in seconds to wait for user input. If 0, timeout is | |
352 | + * disabled, and the default choice will be returned unless prompt is 1. | |
334 | 353 | * |
354 | + * prompt - If 0, don't ask for user input unless there is an interrupted | |
355 | + * timeout. If 1, the user will be prompted for input regardless of the value | |
356 | + * of timeout. | |
357 | + * | |
335 | 358 | * item_data_print - If not NULL, will be called for each item when the menu |
336 | 359 | * is displayed, with the pointer to the item's data passed as the argument. |
337 | 360 | * If NULL, each item's key will be printed instead. Since an item's key is |
... | ... | @@ -341,7 +364,7 @@ |
341 | 364 | * Returns a pointer to the menu if successful, or NULL if there is |
342 | 365 | * insufficient memory available to create the menu. |
343 | 366 | */ |
344 | -struct menu *menu_create(char *title, int prompt, | |
367 | +struct menu *menu_create(char *title, int timeout, int prompt, | |
345 | 368 | void (*item_data_print)(void *)) |
346 | 369 | { |
347 | 370 | struct menu *m; |
... | ... | @@ -353,6 +376,7 @@ |
353 | 376 | |
354 | 377 | m->default_item = NULL; |
355 | 378 | m->prompt = prompt; |
379 | + m->timeout = timeout; | |
356 | 380 | m->item_data_print = item_data_print; |
357 | 381 | |
358 | 382 | if (title) { |
doc/README.menu
... | ... | @@ -45,7 +45,7 @@ |
45 | 45 | /* |
46 | 46 | * menu_create() - Creates a menu handle with default settings |
47 | 47 | */ |
48 | -struct menu *menu_create(char *title, int prompt, | |
48 | +struct menu *menu_create(char *title, int timeout, int prompt, | |
49 | 49 | void (*item_data_print)(void *)); |
50 | 50 | |
51 | 51 | /* |
... | ... | @@ -60,7 +60,7 @@ |
60 | 60 | |
61 | 61 | /* |
62 | 62 | * menu_get_choice() - Returns the user's selected menu entry, or the |
63 | - * default if the menu is set to not prompt. | |
63 | + * default if the menu is set to not prompt or the timeout expires. | |
64 | 64 | */ |
65 | 65 | int menu_get_choice(struct menu *m, void **choice); |
66 | 66 | |
... | ... | @@ -90,7 +90,7 @@ |
90 | 90 | int i; |
91 | 91 | char *tool = NULL; |
92 | 92 | |
93 | - m = menu_create("Tools", 1, NULL); | |
93 | + m = menu_create("Tools", 0, 1, NULL); | |
94 | 94 | |
95 | 95 | for(i = 0; tools[i]; i++) { |
96 | 96 | if (menu_item_add(m, tools[i], tools[i]) != 1) { |
include/common.h
... | ... | @@ -260,6 +260,9 @@ |
260 | 260 | int parse_line (char *, char *[]); |
261 | 261 | void init_cmd_timeout(void); |
262 | 262 | void reset_cmd_timeout(void); |
263 | +#ifdef CONFIG_MENU | |
264 | +int abortboot(int bootdelay); | |
265 | +#endif | |
263 | 266 | |
264 | 267 | /* arch/$(ARCH)/lib/board.c */ |
265 | 268 | void board_init_f (ulong) __attribute__ ((noreturn)); |
include/menu.h
... | ... | @@ -20,7 +20,7 @@ |
20 | 20 | |
21 | 21 | struct menu; |
22 | 22 | |
23 | -struct menu *menu_create(char *title, int prompt, | |
23 | +struct menu *menu_create(char *title, int timeout, int prompt, | |
24 | 24 | void (*item_data_print)(void *)); |
25 | 25 | int menu_default_set(struct menu *m, char *item_key); |
26 | 26 | int menu_get_choice(struct menu *m, void **choice); |