README.menu 2.9 KB

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