Commit fc9d64ffcd94c1e22675830eae13956cc4fd822d
Committed by
Anatolij Gustschin
1 parent
009d75ccc1
Exists in
master
and in
53 other branches
menu: Add support for user defined item choice function
Selecting menu items is currently done in menu_interactive_choice() by reading the user input strings from standard input. Extend menu_interactive_choice() to support user defined function for selecting menu items. This function and its argument can be specified when creating the menu. Signed-off-by: Pali Rohár <pali.rohar@gmail.com> Signed-off-by: Anatolij Gustschin <agust@denx.de>
Showing 5 changed files with 41 additions and 15 deletions Side-by-side Diff
board/ait/cam_enc_4xx/cam_enc_4xx.c
... | ... | @@ -561,7 +561,8 @@ |
561 | 561 | char *s; |
562 | 562 | char temp[6][200]; |
563 | 563 | |
564 | - m = menu_create(display->title, display->timeout, 1, ait_menu_print); | |
564 | + m = menu_create(display->title, display->timeout, 1, ait_menu_print, | |
565 | + NULL, NULL); | |
565 | 566 | |
566 | 567 | for (i = 0; display->menulist[i]; i++) { |
567 | 568 | sprintf(key, "%d", i + 1); |
common/cmd_pxe.c
... | ... | @@ -1280,7 +1280,8 @@ |
1280 | 1280 | /* |
1281 | 1281 | * Create a menu and add items for all the labels. |
1282 | 1282 | */ |
1283 | - m = menu_create(cfg->title, cfg->timeout, cfg->prompt, label_print); | |
1283 | + m = menu_create(cfg->title, cfg->timeout, cfg->prompt, label_print, | |
1284 | + NULL, NULL); | |
1284 | 1285 | |
1285 | 1286 | if (!m) |
1286 | 1287 | return NULL; |
common/menu.c
... | ... | @@ -47,6 +47,8 @@ |
47 | 47 | char *title; |
48 | 48 | int prompt; |
49 | 49 | void (*item_data_print)(void *); |
50 | + char *(*item_choice)(void *); | |
51 | + void *item_choice_data; | |
50 | 52 | struct list_head items; |
51 | 53 | }; |
52 | 54 | |
53 | 55 | |
54 | 56 | |
... | ... | @@ -204,18 +206,26 @@ |
204 | 206 | |
205 | 207 | menu_display(m); |
206 | 208 | |
207 | - readret = readline_into_buffer("Enter choice: ", cbuf, | |
208 | - m->timeout / 10); | |
209 | + if (!m->item_choice) { | |
210 | + readret = readline_into_buffer("Enter choice: ", cbuf, | |
211 | + m->timeout / 10); | |
209 | 212 | |
210 | - if (readret >= 0) { | |
211 | - choice_item = menu_item_by_key(m, cbuf); | |
212 | - | |
213 | - if (!choice_item) { | |
214 | - printf("%s not found\n", cbuf); | |
215 | - m->timeout = 0; | |
213 | + if (readret >= 0) { | |
214 | + choice_item = menu_item_by_key(m, cbuf); | |
215 | + if (!choice_item) | |
216 | + printf("%s not found\n", cbuf); | |
217 | + } else { | |
218 | + return menu_default_choice(m, choice); | |
216 | 219 | } |
217 | - } else | |
218 | - return menu_default_choice(m, choice); | |
220 | + } else { | |
221 | + char *key = m->item_choice(m->item_choice_data); | |
222 | + | |
223 | + if (key) | |
224 | + choice_item = menu_item_by_key(m, key); | |
225 | + } | |
226 | + | |
227 | + if (!choice_item) | |
228 | + m->timeout = 0; | |
219 | 229 | } |
220 | 230 | |
221 | 231 | *choice = choice_item->data; |
222 | 232 | |
... | ... | @@ -348,11 +358,19 @@ |
348 | 358 | * what must be entered to select an item, the item_data_print function should |
349 | 359 | * make it obvious what the key for each entry is. |
350 | 360 | * |
361 | + * item_choice - If not NULL, will be called when asking the user to choose an | |
362 | + * item. Returns a key string corresponding to the choosen item or NULL if | |
363 | + * no item has been selected. | |
364 | + * | |
365 | + * item_choice_data - Will be passed as the argument to the item_choice function | |
366 | + * | |
351 | 367 | * Returns a pointer to the menu if successful, or NULL if there is |
352 | 368 | * insufficient memory available to create the menu. |
353 | 369 | */ |
354 | 370 | struct menu *menu_create(char *title, int timeout, int prompt, |
355 | - void (*item_data_print)(void *)) | |
371 | + void (*item_data_print)(void *), | |
372 | + char *(*item_choice)(void *), | |
373 | + void *item_choice_data) | |
356 | 374 | { |
357 | 375 | struct menu *m; |
358 | 376 | |
... | ... | @@ -365,6 +383,8 @@ |
365 | 383 | m->prompt = prompt; |
366 | 384 | m->timeout = timeout; |
367 | 385 | m->item_data_print = item_data_print; |
386 | + m->item_choice = item_choice; | |
387 | + m->item_choice_data = item_choice_data; | |
368 | 388 | |
369 | 389 | if (title) { |
370 | 390 | m->title = strdup(title); |
doc/README.menu
... | ... | @@ -51,7 +51,9 @@ |
51 | 51 | * menu_create() - Creates a menu handle with default settings |
52 | 52 | */ |
53 | 53 | struct menu *menu_create(char *title, int timeout, int prompt, |
54 | - void (*item_data_print)(void *)); | |
54 | + void (*item_data_print)(void *), | |
55 | + char *(*item_choice)(void *), | |
56 | + void *item_choice_data); | |
55 | 57 | |
56 | 58 | /* |
57 | 59 | * menu_item_add() - Adds or replaces a menu item |
include/menu.h
... | ... | @@ -21,7 +21,9 @@ |
21 | 21 | struct menu; |
22 | 22 | |
23 | 23 | struct menu *menu_create(char *title, int timeout, int prompt, |
24 | - void (*item_data_print)(void *)); | |
24 | + void (*item_data_print)(void *), | |
25 | + char *(*item_choice)(void *), | |
26 | + void *item_choice_data); | |
25 | 27 | int menu_default_set(struct menu *m, char *item_key); |
26 | 28 | int menu_get_choice(struct menu *m, void **choice); |
27 | 29 | int menu_item_add(struct menu *m, char *item_key, void *item_data); |