1*4882a593Smuzhiyun/* 2*4882a593Smuzhiyun * Copyright 2010-2011 Calxeda, Inc. 3*4882a593Smuzhiyun * 4*4882a593Smuzhiyun * SPDX-License-Identifier: GPL-2.0+ 5*4882a593Smuzhiyun */ 6*4882a593Smuzhiyun 7*4882a593SmuzhiyunU-Boot provides a set of interfaces for creating and using simple, text 8*4882a593Smuzhiyunbased menus. Menus are displayed as lists of labeled entries on the 9*4882a593Smuzhiyunconsole, and an entry can be selected by entering its label. 10*4882a593Smuzhiyun 11*4882a593SmuzhiyunTo use the menu code, enable CONFIG_MENU, and include "menu.h" where 12*4882a593Smuzhiyunthe interfaces should be available. 13*4882a593Smuzhiyun 14*4882a593SmuzhiyunMenus are composed of items. Each item has a key used to identify it in 15*4882a593Smuzhiyunthe menu, and an opaque pointer to data controlled by the consumer. 16*4882a593Smuzhiyun 17*4882a593SmuzhiyunIf you want to show a menu, instead starting the shell, define 18*4882a593SmuzhiyunCONFIG_MENU_SHOW. You have to code the int menu_show(int bootdelay) 19*4882a593Smuzhiyunfunction, which handle your menu. This function returns the remaining 20*4882a593Smuzhiyunbootdelay. 21*4882a593Smuzhiyun 22*4882a593SmuzhiyunInterfaces 23*4882a593Smuzhiyun---------- 24*4882a593Smuzhiyun#include "menu.h" 25*4882a593Smuzhiyun 26*4882a593Smuzhiyun/* 27*4882a593Smuzhiyun * Consumers of the menu interfaces will use a struct menu * as the 28*4882a593Smuzhiyun * handle for a menu. struct menu is only fully defined in menu.c, 29*4882a593Smuzhiyun * preventing consumers of the menu interfaces from accessing its 30*4882a593Smuzhiyun * contents directly. 31*4882a593Smuzhiyun */ 32*4882a593Smuzhiyunstruct menu; 33*4882a593Smuzhiyun 34*4882a593Smuzhiyun/* 35*4882a593Smuzhiyun * NOTE: See comments in common/menu.c for more detailed documentation on 36*4882a593Smuzhiyun * these interfaces. 37*4882a593Smuzhiyun */ 38*4882a593Smuzhiyun 39*4882a593Smuzhiyun/* 40*4882a593Smuzhiyun * menu_create() - Creates a menu handle with default settings 41*4882a593Smuzhiyun */ 42*4882a593Smuzhiyunstruct menu *menu_create(char *title, int timeout, int prompt, 43*4882a593Smuzhiyun void (*item_data_print)(void *), 44*4882a593Smuzhiyun char *(*item_choice)(void *), 45*4882a593Smuzhiyun void *item_choice_data); 46*4882a593Smuzhiyun 47*4882a593Smuzhiyun/* 48*4882a593Smuzhiyun * menu_item_add() - Adds or replaces a menu item 49*4882a593Smuzhiyun */ 50*4882a593Smuzhiyunint menu_item_add(struct menu *m, char *item_key, void *item_data); 51*4882a593Smuzhiyun 52*4882a593Smuzhiyun/* 53*4882a593Smuzhiyun * menu_default_set() - Sets the default choice for the menu 54*4882a593Smuzhiyun */ 55*4882a593Smuzhiyunint menu_default_set(struct menu *m, char *item_key); 56*4882a593Smuzhiyun 57*4882a593Smuzhiyun/* 58*4882a593Smuzhiyun * menu_default_choice() - Set *choice to point to the default item's data 59*4882a593Smuzhiyun */ 60*4882a593Smuzhiyunint menu_default_choice(struct menu *m, void **choice); 61*4882a593Smuzhiyun 62*4882a593Smuzhiyun/* 63*4882a593Smuzhiyun * menu_get_choice() - Returns the user's selected menu entry, or the 64*4882a593Smuzhiyun * default if the menu is set to not prompt or the timeout expires. 65*4882a593Smuzhiyun */ 66*4882a593Smuzhiyunint menu_get_choice(struct menu *m, void **choice); 67*4882a593Smuzhiyun 68*4882a593Smuzhiyun/* 69*4882a593Smuzhiyun * menu_destroy() - frees the memory used by a menu and its items. 70*4882a593Smuzhiyun */ 71*4882a593Smuzhiyunint menu_destroy(struct menu *m); 72*4882a593Smuzhiyun 73*4882a593Smuzhiyun/* 74*4882a593Smuzhiyun * menu_display_statusline(struct menu *m); 75*4882a593Smuzhiyun * shows a statusline for every menu_display call. 76*4882a593Smuzhiyun */ 77*4882a593Smuzhiyunvoid menu_display_statusline(struct menu *m); 78*4882a593Smuzhiyun 79*4882a593SmuzhiyunExample Code 80*4882a593Smuzhiyun------------ 81*4882a593SmuzhiyunThis example creates a menu that always prompts, and allows the user 82*4882a593Smuzhiyunto pick from a list of tools. The item key and data are the same. 83*4882a593Smuzhiyun 84*4882a593Smuzhiyun#include "menu.h" 85*4882a593Smuzhiyun 86*4882a593Smuzhiyunchar *tools[] = { 87*4882a593Smuzhiyun "Hammer", 88*4882a593Smuzhiyun "Screwdriver", 89*4882a593Smuzhiyun "Nail gun", 90*4882a593Smuzhiyun NULL 91*4882a593Smuzhiyun}; 92*4882a593Smuzhiyun 93*4882a593Smuzhiyunchar *pick_a_tool(void) 94*4882a593Smuzhiyun{ 95*4882a593Smuzhiyun struct menu *m; 96*4882a593Smuzhiyun int i; 97*4882a593Smuzhiyun char *tool = NULL; 98*4882a593Smuzhiyun 99*4882a593Smuzhiyun m = menu_create("Tools", 0, 1, NULL); 100*4882a593Smuzhiyun 101*4882a593Smuzhiyun for(i = 0; tools[i]; i++) { 102*4882a593Smuzhiyun if (menu_item_add(m, tools[i], tools[i]) != 1) { 103*4882a593Smuzhiyun printf("failed to add item!"); 104*4882a593Smuzhiyun menu_destroy(m); 105*4882a593Smuzhiyun return NULL; 106*4882a593Smuzhiyun } 107*4882a593Smuzhiyun } 108*4882a593Smuzhiyun 109*4882a593Smuzhiyun if (menu_get_choice(m, (void **)&tool) != 1) 110*4882a593Smuzhiyun printf("Problem picking tool!\n"); 111*4882a593Smuzhiyun 112*4882a593Smuzhiyun menu_destroy(m); 113*4882a593Smuzhiyun 114*4882a593Smuzhiyun return tool; 115*4882a593Smuzhiyun} 116*4882a593Smuzhiyun 117*4882a593Smuzhiyunvoid caller(void) 118*4882a593Smuzhiyun{ 119*4882a593Smuzhiyun char *tool = pick_a_tool(); 120*4882a593Smuzhiyun 121*4882a593Smuzhiyun if (tool) { 122*4882a593Smuzhiyun printf("picked a tool: %s\n", tool); 123*4882a593Smuzhiyun use_tool(tool); 124*4882a593Smuzhiyun } 125*4882a593Smuzhiyun} 126