menu.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. #ifndef MENU_H
  2. #define MENU_H
  3. /* A single menu */
  4. typedef void (*menu_finish_callback)(struct menu_s *menu);
  5. typedef struct menu_s {
  6. char *name; /* Menu name */
  7. int num_options; /* Number of options in this menu */
  8. int flags; /* Various flags - see below */
  9. int option_align; /* Aligns options to a field width of this much characters if != 0 */
  10. struct menu_option_s **options; /* Pointer to this menu's options */
  11. menu_finish_callback callback; /* Called when the menu closes */
  12. } menu_t;
  13. /*
  14. * type: Type of the option (see below)
  15. * name: Name to display for this option
  16. * help: Optional help string
  17. * id : optional id number
  18. * sys : pointer for system-specific data, init to NULL and don't touch
  19. */
  20. #define OPTION_PREAMBLE \
  21. int type; \
  22. char *name; \
  23. char *help; \
  24. int id; \
  25. void *sys;
  26. /*
  27. * Menu option types.
  28. * There are a number of different layouts for menu options depending
  29. * on their types. Currently there are the following possibilities:
  30. *
  31. * Submenu:
  32. * This entry links to a new menu.
  33. *
  34. * Boolean:
  35. * A simple on/off toggle entry. Booleans can be either yes/no, 0/1 or on/off.
  36. * Optionally, this entry can enable/disable a set of other options. An example would
  37. * be to enable/disable on-board USB, and if enabled give access to further options like
  38. * irq settings, base address etc.
  39. *
  40. * Text:
  41. * A single line/limited number of characters text entry box. Text can be restricted
  42. * to a certain charset (digits/hex digits/all/custom). Result is also available as an
  43. * int if numeric.
  44. *
  45. * Selection:
  46. * One-of-many type of selection entry. User may choose on of a set of strings, which
  47. * maps to a specific value for the variable.
  48. *
  49. * Routine:
  50. * Selecting this calls an entry-specific routine. This can be used for saving contents etc.
  51. *
  52. * Custom:
  53. * Display and behaviour of this entry is defined by a set of callbacks.
  54. */
  55. #define MENU_SUBMENU_TYPE 0
  56. typedef struct menu_submenu_s
  57. {
  58. OPTION_PREAMBLE
  59. menu_t * submenu; /* Pointer to the submenu */
  60. } menu_submenu_t;
  61. #define MENU_BOOLEAN_TYPE 1
  62. typedef struct menu_boolean_s
  63. {
  64. OPTION_PREAMBLE
  65. char *variable; /* Name of the variable to getenv()/setenv() */
  66. int subtype; /* Subtype (on/off, 0/1, yes/no, enable/disable), see below */
  67. int mutex; /* Bit mask of options to enable/disable. Bit 0 is the option
  68. immediately following this one, bit 1 is the next one etc.
  69. bit 7 = 0 means to disable when this option is off,
  70. bit 7 = 1 means to disable when this option is on.
  71. An option is disabled when the type field's upper bit is set */
  72. } menu_boolean_t;
  73. /* BOOLEAN Menu flags */
  74. #define MENU_BOOLEAN_ONOFF 0x01
  75. #define MENU_BOOLEAN_01 0x02
  76. #define MENU_BOOLEAN_YESNO 0x03
  77. #define MENU_BOOLEAN_ENDIS 0x04
  78. #define MENU_BOOLEAN_TYPE_MASK 0x07
  79. #define MENU_TEXT_TYPE 2
  80. typedef struct menu_text_s
  81. {
  82. OPTION_PREAMBLE
  83. char *variable; /* Name of the variable to getenv()/setenv() */
  84. int maxchars; /* Max number of characters */
  85. char *charset; /* Optional charset to use */
  86. int flags; /* Flags - see below */
  87. } menu_text_t;
  88. /* TEXT entry menu flags */
  89. #define MENU_TEXT_NUMERIC 0x01
  90. #define MENU_TEXT_HEXADECIMAL 0x02
  91. #define MENU_TEXT_FREE 0x03
  92. #define MENU_TEXT_TYPE_MASK 0x07
  93. #define MENU_SELECTION_TYPE 3
  94. typedef struct menu_select_option_s {
  95. char *map_from; /* Map this variable contents ... */
  96. char *map_to; /* ... to this menu text and vice versa */
  97. } menu_select_option_t;
  98. typedef struct menu_select_s {
  99. OPTION_PREAMBLE int num_options; /* Number of mappings */
  100. menu_select_option_t **options;
  101. /* Option list array */
  102. } menu_select_t;
  103. #define MENU_ROUTINE_TYPE 4
  104. typedef void (*menu_routine_callback) (struct menu_routine_s *);
  105. typedef struct menu_routine_s {
  106. OPTION_PREAMBLE menu_routine_callback callback;
  107. /* routine to be called */
  108. void *user_data; /* User data, don't care for system */
  109. } menu_routine_t;
  110. #define MENU_CUSTOM_TYPE 5
  111. typedef void (*menu_custom_draw) (struct menu_custom_s *);
  112. typedef void (*menu_custom_key) (struct menu_custom_s *, int);
  113. typedef struct menu_custom_s {
  114. OPTION_PREAMBLE menu_custom_draw drawfunc;
  115. menu_custom_key keyfunc;
  116. void *user_data;
  117. } menu_custom_t;
  118. /*
  119. * The menu option superstructure
  120. */
  121. typedef struct menu_option_s {
  122. union {
  123. menu_submenu_t m_sub_menu;
  124. menu_boolean_t m_boolean;
  125. menu_text_t m_text;
  126. menu_select_t m_select;
  127. menu_routine_t m_routine;
  128. };
  129. } menu_option_t;
  130. /* Init the menu system. Returns <0 on error */
  131. int menu_init(menu_t *root);
  132. /* Execute a single menu. Returns <0 on error */
  133. int menu_do(menu_t *menu);
  134. #endif