Jason Hobbs | 0685d82 | 2011-08-23 11:06:49 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2010-2011 Calxeda, Inc. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify it |
| 5 | * under the terms of the GNU General Public License as published by the Free |
| 6 | * Software Foundation; either version 2 of the License, or (at your option) |
| 7 | * any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope it will be useful, but WITHOUT |
| 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 12 | * more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License along with |
| 15 | * this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | */ |
| 17 | |
| 18 | U-boot provides a set of interfaces for creating and using simple, text |
| 19 | based menus. Menus are displayed as lists of labeled entries on the |
| 20 | console, and an entry can be selected by entering its label. |
| 21 | |
| 22 | To use the menu code, enable CONFIG_MENU, and include "menu.h" where |
| 23 | the interfaces should be available. |
| 24 | |
| 25 | Menus are composed of items. Each item has a key used to identify it in |
| 26 | the menu, and an opaque pointer to data controlled by the consumer. |
| 27 | |
| 28 | Interfaces |
| 29 | ---------- |
| 30 | #include "menu.h" |
| 31 | |
| 32 | /* |
| 33 | * Consumers of the menu interfaces will use a struct menu * as the |
| 34 | * handle for a menu. struct menu is only fully defined in menu.c, |
| 35 | * preventing consumers of the menu interfaces from accessing its |
| 36 | * contents directly. |
| 37 | */ |
| 38 | struct menu; |
| 39 | |
| 40 | /* |
| 41 | * NOTE: See comments in common/menu.c for more detailed documentation on |
| 42 | * these interfaces. |
| 43 | */ |
| 44 | |
| 45 | /* |
| 46 | * menu_create() - Creates a menu handle with default settings |
| 47 | */ |
Jason Hobbs | 65febe6 | 2011-08-23 11:06:50 +0000 | [diff] [blame] | 48 | struct menu *menu_create(char *title, int timeout, int prompt, |
Jason Hobbs | 0685d82 | 2011-08-23 11:06:49 +0000 | [diff] [blame] | 49 | void (*item_data_print)(void *)); |
| 50 | |
| 51 | /* |
| 52 | * menu_item_add() - Adds or replaces a menu item |
| 53 | */ |
| 54 | int menu_item_add(struct menu *m, char *item_key, void *item_data); |
| 55 | |
| 56 | /* |
| 57 | * menu_default_set() - Sets the default choice for the menu |
| 58 | */ |
| 59 | int menu_default_set(struct menu *m, char *item_key); |
| 60 | |
| 61 | /* |
| 62 | * menu_get_choice() - Returns the user's selected menu entry, or the |
Jason Hobbs | 65febe6 | 2011-08-23 11:06:50 +0000 | [diff] [blame] | 63 | * default if the menu is set to not prompt or the timeout expires. |
Jason Hobbs | 0685d82 | 2011-08-23 11:06:49 +0000 | [diff] [blame] | 64 | */ |
| 65 | int menu_get_choice(struct menu *m, void **choice); |
| 66 | |
| 67 | /* |
| 68 | * menu_destroy() - frees the memory used by a menu and its items. |
| 69 | */ |
| 70 | int menu_destroy(struct menu *m); |
| 71 | |
| 72 | |
| 73 | Example Code |
| 74 | ------------ |
| 75 | This example creates a menu that always prompts, and allows the user |
| 76 | to pick from a list of tools. The item key and data are the same. |
| 77 | |
| 78 | #include "menu.h" |
| 79 | |
| 80 | char *tools[] = { |
| 81 | "Hammer", |
| 82 | "Screwdriver", |
| 83 | "Nail gun", |
| 84 | NULL |
| 85 | }; |
| 86 | |
| 87 | char *pick_a_tool(void) |
| 88 | { |
| 89 | struct menu *m; |
| 90 | int i; |
| 91 | char *tool = NULL; |
| 92 | |
Jason Hobbs | 65febe6 | 2011-08-23 11:06:50 +0000 | [diff] [blame] | 93 | m = menu_create("Tools", 0, 1, NULL); |
Jason Hobbs | 0685d82 | 2011-08-23 11:06:49 +0000 | [diff] [blame] | 94 | |
| 95 | for(i = 0; tools[i]; i++) { |
| 96 | if (menu_item_add(m, tools[i], tools[i]) != 1) { |
| 97 | printf("failed to add item!"); |
| 98 | menu_destroy(m); |
| 99 | return NULL; |
Wolfgang Denk | b60c557 | 2011-12-19 12:03:40 +0100 | [diff] [blame] | 100 | } |
Jason Hobbs | 0685d82 | 2011-08-23 11:06:49 +0000 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | if (menu_get_choice(m, (void **)&tool) != 1) |
| 104 | printf("Problem picking tool!\n"); |
| 105 | |
| 106 | menu_destroy(m); |
| 107 | |
| 108 | return tool; |
| 109 | } |
| 110 | |
| 111 | void caller(void) |
| 112 | { |
| 113 | char *tool = pick_a_tool(); |
| 114 | |
| 115 | if (tool) { |
| 116 | printf("picked a tool: %s\n", tool); |
| 117 | use_tool(tool); |
| 118 | } |
| 119 | } |