menu.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /*
  2. * Copyright (c) 2011-2012, Freescale Semiconductor, Inc.
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without modification,
  6. * are permitted provided that the following conditions are met:
  7. *
  8. * o Redistributions of source code must retain the above copyright notice, this list
  9. * of conditions and the following disclaimer.
  10. *
  11. * o Redistributions in binary form must reproduce the above copyright notice, this
  12. * list of conditions and the following disclaimer in the documentation and/or
  13. * other materials provided with the distribution.
  14. *
  15. * o Neither the name of Freescale Semiconductor, Inc. nor the names of its
  16. * contributors may be used to endorse or promote products derived from this
  17. * software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  20. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  21. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  22. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
  23. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  24. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  25. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  26. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  28. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. /*!
  31. * @file menu.h
  32. * @brief Defines related to the Menu Framework and used by menu.c
  33. * @ingroup diag_utility
  34. */
  35. #ifndef __MENU_H__
  36. #define __MENU_H__
  37. //! @addtogroup menu
  38. //! @{
  39. ////////////////////////////////////////////////////////////////////////////////
  40. // Definitions
  41. ////////////////////////////////////////////////////////////////////////////////
  42. //! @name Menu Framework configuration values
  43. //@{
  44. #define MENU_INDENT 4 //!< Default number of spaces for each level of indent.
  45. #define MENU_INDENT_MAX 120 //!< The maximum number of allowed for indent. Used for allocation purposes.
  46. #define MENU_KEY_MAX 8 //!< The maximum number of characters allowed for a menu key. Used for allocation purposes.
  47. //@}
  48. //////////////////////////////////////////////////////////////////////////////////////////
  49. // Types
  50. //////////////////////////////////////////////////////////////////////////////////////////
  51. // Forward type declarations.
  52. typedef struct _menu_context menu_context_t;
  53. typedef struct _menuitem menuitem_t;
  54. typedef struct _menu menu_t;
  55. //! @brief Available Menu Actions.
  56. typedef enum _menu_actions
  57. {
  58. MenuAction_Show, //!< Print the menu header, all of the menuitems, and the menu footer.
  59. MenuAction_Exit, //!< Exit all levels of the menu.
  60. MenuAction_Continue, //!< Continue processing the menu loop. Default.
  61. MenuAction_ContinuePrint, //!< Print the menu then continue processing the menu loop.
  62. MenuAction_Back //!< Exit current menu level and print the parent menu.
  63. } menu_action_t;
  64. /*!
  65. * @brief Menu Execution function definition. Called in response to a menuitem being selected by the user.
  66. *
  67. * @return How to proceed with menu processing.
  68. */
  69. typedef menu_action_t (*menu_function_t) (void* param);
  70. //! @brief Available MenuItem Types.
  71. typedef enum _menuitem_types
  72. {
  73. MenuItem_Group, //!< Describes a menu group and inserts a blank line in the menu before the optional description.
  74. MenuItem_Submenu, //!< Describes a selectable (by key) submenu with description.
  75. MenuItem_Function, //!< Describes a "regular" selectable (by key) menuitem with description and executable function.
  76. MenuItem_Null //!< Describes an end marker of a menu list.
  77. } menuitem_type_t;
  78. //! @brief MenuItem container to hold data needed to process each menu item.
  79. struct _menuitem
  80. {
  81. //! @brief The type of the menuitem; #MENUITEM_GROUP, #MENUITEM_SUBMENU, #MENUITEM_FUNCTION, ...
  82. menuitem_type_t type;
  83. //! @brief The key typed in by the user to select a menuitem for execution.
  84. const char* key;
  85. //! @brief The text associated with the menuitem describing the function. Can be NULL.
  86. const char* description;
  87. //! @brief A pointer to the submenu's menuitems. Valid for #MENUITEM_SUBMENU menuitems. Ignored otherwise.
  88. const menu_t* submenu;
  89. //! @brief A pointer to the menu execution function. Valid for #MENUITEM_FUNCTION menuitems. Ignored otherwise.
  90. menu_function_t func_ptr;
  91. void* func_param;
  92. };
  93. //! @brief Menu container to hold data needed to describe a menu or submenu.
  94. struct _menu
  95. {
  96. //! @brief Text introducing the menu and its purpose.
  97. const char* header;
  98. //! @brief A pointer to the list of menu items.
  99. const menuitem_t* menuitems;
  100. //! @brief Instructional text to help user execute the menu.
  101. const char* footer;
  102. };
  103. //////////////////////////////////////////////////////////////////////////////////////////
  104. // Private Variables
  105. //////////////////////////////////////////////////////////////////////////////////////////
  106. static char MenuIndent[];
  107. ////////////////////////////////////////////////////////////////////////////////
  108. // API
  109. ////////////////////////////////////////////////////////////////////////////////
  110. #if defined(__cplusplus)
  111. extern "C" {
  112. #endif
  113. //! @name Menu presentation
  114. //@{
  115. /*!
  116. * @brief Presents the menu UI and processes the user input.
  117. *
  118. * The menu description (header) is printed, followed by each of the menuitem key and
  119. * menuitem description. Instructions for the user (footer) are printed last. The
  120. * function then waits form user input. After each key press, the menu items keys are
  121. * compared to the input string. If the input string uniquely identifies a menuitem,
  122. * the function associated with that menitem is executed. If the input string does not
  123. * uniquely identify a menuitem, the function waits for more input or the Enter key.
  124. *
  125. * @param[in] menu A pointer to the menu structure to be presented and processed.
  126. * @return How to proceed with menu processing.
  127. * @retval #MenuAction_Show Print the menu header, all of the menuitems, and the menu footer.
  128. * @retval #MenuAction_Exit Exit all levels of the menu.
  129. * @retval #MenuAction_Continue Continue processing the menu loop. Default.
  130. * @retval #MenuAction_ContinuePrint Print the menu then continue processing the menu loop.
  131. * @retval #MenuAction_Back Exit current menu level and print the parent menu.
  132. */
  133. menu_action_t menu_present(const menu_t* menu);
  134. /*!
  135. * @brief Return a string used to align text for the current menu level.
  136. *
  137. * @param context Menu context that allows function to discern indent level among other things.
  138. * @return String of space characters used to align output text for the current menu level.
  139. */
  140. const char* menu_get_indent();
  141. //@}
  142. //@}
  143. /*!
  144. * @brief Default "Show Menu" handler.
  145. *
  146. * @return MENU_SHOW.
  147. */
  148. menu_action_t menuitem_cmd_showmenu(void* param);
  149. /*!
  150. * @brief Default "Exit Menu" handler.
  151. *
  152. * @return MENU_EXIT.
  153. */
  154. menu_action_t menuitem_cmd_exitmenu(void* param);
  155. //! @name Menu construction
  156. //!
  157. //! These functions allow the caller to easily construct a menu at runtime.
  158. //@{
  159. /*!
  160. * @brief Appends source_menuitems list to target_menuitems.
  161. *
  162. * @param[in,out] target_menuitems Menuitem array pointer for the target menuitems.
  163. * @param[in] target_size Maximum number of menuitems the target_menuitems can hold.
  164. * @param[in] source_menuitems Menuitem array pointer for the source menuitems.
  165. *
  166. * @return new size of the target menuitem array.
  167. */
  168. int menu_append_menuitems(menuitem_t* target_menuitems, int target_size, const menuitem_t* source_menuitems);
  169. /*!
  170. * @brief Initializes the passed in menu stuct with the menu elements.
  171. *
  172. * @param[in,out] menu Menu struct container for menu elements.
  173. * @param[in] header Description of the menu presented to the user before the list of menu items.
  174. * @param[in] menuitems Array of menu items representing grouping and selections of the menu.
  175. * @param[in] footer Instructions for the user presented after the list of menu items.
  176. */
  177. //void menu_make_menu(menu_t* menu, const char* header, const menuitem_t* menuitems, const char* footer);
  178. /*!
  179. * @brief Initializes the passed in menuitem stuct with the menuitem elements.
  180. *
  181. * @param[in,out] menuitem Menuitem struct to be initialized.
  182. * @param[in] key String used to select the menuitem. Key is limited to #KEY_MAX characters.
  183. * @param[in] description String used to describe the action executed if selected.
  184. * @param[in] funct Function pointer that is executed if the menuitem is selected.
  185. * @param[in] param Object pointer that is passed to the executed function. Generally used to
  186. * pass the return the results of the function.
  187. */
  188. //void menu_make_menuitem(menuitem_t* menuitem, const char* key, const char* description, menu_function_t func, void* func_param);
  189. #define MENU_MAKE_MENUITEM(k,d,f,p) { MenuItem_Function, k, d, NULL, f, p }
  190. /*!
  191. * @brief Initializes the passed in menuitem stuct to represent a "Show menu" menu item.
  192. *
  193. * This function creates a menuitem with key of "m" and a description of "Display menu."
  194. * When selected, the invoked function returns #MENU_SHOW.
  195. *
  196. * @param[in,out] menuitem Menuitem struct to be initialized.
  197. */
  198. //void menu_make_menuitem_showmenu(menuitem_t* menuitem);
  199. #define MENU_MAKE_MENUITEM_SHOW() { MenuItem_Function, "m", "Display menu.", NULL, menuitem_cmd_showmenu, NULL }
  200. /*!
  201. * @brief Initializes the passed in menuitem stuct to represent a "Exit menu" menu item.
  202. *
  203. * This function creates a menuitem with key of "q" and a description of "Exit menu."
  204. * When selected, the invoked function returns #MENU_EXIT.
  205. *
  206. * @param[in,out] menuitem Menuitem struct to be initialized.
  207. */
  208. //void menu_make_menuitem_exitmenu(menuitem_t* menuitem);
  209. #define MENU_MAKE_MENUITEM_EXIT() { MenuItem_Function, "q", "Exit menu.", NULL, menuitem_cmd_exitmenu, NULL }
  210. /*!
  211. * @brief Initializes the passed in menuitem stuct to represent a selectable submenu.
  212. *
  213. * This function creates a menuitem denoted by the key and description parameters.
  214. * When selected, the menu parameter will be presented by a recursive call to menu_present().
  215. *
  216. * @param[in,out] menuitem Menuitem struct to be initialized.
  217. * @param[in] key String used to select the submenu. Key is limited to #KEY_MAX characters.
  218. * @param[in] description String used to describe the submenu.
  219. * @param[in,out] menu Menu to be presented as a submenu when selected.
  220. */
  221. //void menu_make_menuitem_submenu(menuitem_t* menuitem, const char* key, const char* description, const menu_t* menu);
  222. /*!
  223. * @brief Initializes the passed in menuitem stuct to represent a menu group.
  224. *
  225. * This function creates a menuitem that will add a blank line in the menuitem listing.
  226. * If a group description is provided, the text will be presented after the blank line.
  227. * The group menuitem is not selectable. It is provided as a means to graphically arrange menu items.
  228. *
  229. * @param[in,out] menuitem Menuitem struct to be initialized.
  230. * @param[in] description String used to describe the following group of menu items. Can be NULL.
  231. */
  232. //void menu_make_menuitem_group(menuitem_t* menuitem, const char* description);
  233. #define MENU_MAKE_MENUITEM_GROUP(d) { MenuItem_Group, NULL, d, NULL, NULL, NULL }
  234. /*!
  235. * @brief Initializes the passed in menuitem stuct to represent the terminating element of a menu.
  236. *
  237. * The menu_present() function requires that a menuitem list of the menu be terminated with a
  238. * #MENUITEM_NULL element.
  239. *
  240. * @param[in,out] menuitem Menuitem struct representing the terminating element of a menu.
  241. */
  242. //void menu_make_menuitem_end(menuitem_t* menuitem);
  243. #define MENU_MAKE_MENUITEM_END() { MenuItem_Null, NULL, NULL, NULL, NULL, NULL }
  244. void menu_print_menuitem(menuitem_t* menuitem);
  245. //@}
  246. //! @}
  247. #endif //__MENU_H__
  248. ////////////////////////////////////////////////////////////////////////////////
  249. // EOF
  250. ////////////////////////////////////////////////////////////////////////////////