Daniel Stenberg | 1c0c861 | 2002-05-17 12:22:24 +0000 | [diff] [blame] | 1 | /*************************************************************************** |
| 2 | * __________ __ ___. |
| 3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ |
| 4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / |
| 5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < |
| 6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ |
| 7 | * \/ \/ \/ \/ \/ |
| 8 | * $Id$ |
| 9 | * |
| 10 | * Copyright (C) 2002 Robert E. Hak |
| 11 | * |
| 12 | * All files in this archive are subject to the GNU General Public License. |
| 13 | * See the file COPYING in the source tree root for full license agreement. |
| 14 | * |
| 15 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY |
| 16 | * KIND, either express or implied. |
| 17 | * |
| 18 | ****************************************************************************/ |
| 19 | |
| 20 | #ifndef __MENU_H__ |
| 21 | #define __MENU_H__ |
| 22 | |
Daniel Stenberg | 91f743f | 2002-06-14 10:39:11 +0000 | [diff] [blame] | 23 | #include <stdbool.h> |
Jonathan Gordon | 5599d68 | 2007-02-14 06:58:30 +0000 | [diff] [blame] | 24 | #include "icon.h" |
| 25 | #include "icons.h" |
| 26 | |
Daniel Stenberg | 91f743f | 2002-06-14 10:39:11 +0000 | [diff] [blame] | 27 | |
Jonathan Gordon | 4718a1e | 2007-02-08 04:33:41 +0000 | [diff] [blame] | 28 | enum menu_item_type { |
| 29 | MT_MENU = 0, |
| 30 | MT_SETTING, |
Jonathan Gordon | 9709086 | 2007-03-03 14:23:03 +0000 | [diff] [blame] | 31 | MT_SETTING_W_TEXT, /* same as setting, but uses different |
| 32 | text for the setting title, |
| 33 | ID2P() or "literal" for the str param */ |
Jonathan Gordon | daf6694 | 2007-03-17 12:33:34 +0000 | [diff] [blame] | 34 | MT_FUNCTION_CALL, /* call a function from the menus */ |
Jonathan Gordon | 4718a1e | 2007-02-08 04:33:41 +0000 | [diff] [blame] | 35 | MT_RETURN_ID, /* returns the position of the selected item (starting at 0)*/ |
Jonathan Gordon | 91cb68a | 2007-03-01 11:14:46 +0000 | [diff] [blame] | 36 | MT_RETURN_VALUE, /* returns a value associated with an item */ |
Jonathan Gordon | 3452146 | 2007-03-07 13:00:46 +0000 | [diff] [blame] | 37 | MT_OLD_MENU, /* used so we can wrap the old menu api |
| 38 | around the new api. Noone else should use this */ |
Jonathan Gordon | 4718a1e | 2007-02-08 04:33:41 +0000 | [diff] [blame] | 39 | }; |
| 40 | |
| 41 | typedef int (*menu_function)(void); |
Jonathan Gordon | daf6694 | 2007-03-17 12:33:34 +0000 | [diff] [blame] | 42 | struct menu_func { |
| 43 | union { |
| 44 | int (*function_w_param)(void* param); /* intptr_t instead of void* |
| 45 | for 64bit systems */ |
| 46 | int (*function)(void); |
| 47 | }; |
| 48 | void *param; /* passed to function_w_param */ |
| 49 | int exit_value; /* exit do_menu() if function returns this value */ |
Jonathan Gordon | 4718a1e | 2007-02-08 04:33:41 +0000 | [diff] [blame] | 50 | }; |
| 51 | |
| 52 | #define MENU_TYPE_MASK 0xF /* MT_* type */ |
Jonathan Gordon | 2801a87 | 2007-02-19 02:14:51 +0000 | [diff] [blame] | 53 | /* these next two are mutually exclusive */ |
Jonathan Gordon | 4718a1e | 2007-02-08 04:33:41 +0000 | [diff] [blame] | 54 | #define MENU_HAS_DESC 0x10 |
Jonathan Gordon | 2801a87 | 2007-02-19 02:14:51 +0000 | [diff] [blame] | 55 | #define MENU_DYNAMIC_DESC 0x20 |
Jonathan Gordon | daf6694 | 2007-03-17 12:33:34 +0000 | [diff] [blame] | 56 | |
| 57 | /* Flags for MT_FUNCTION_CALL */ |
| 58 | #define MENU_FUNC_USEPARAM 0x40 |
| 59 | #define MENU_FUNC_CHECK_RETVAL 0x80 |
| 60 | |
| 61 | #define MENU_COUNT_MASK 0xFFF |
| 62 | #define MENU_COUNT_SHIFT 8 |
| 63 | #define MENU_ITEM_COUNT(c) ((c&MENU_COUNT_MASK)<<MENU_COUNT_SHIFT) |
Jonathan Gordon | 4718a1e | 2007-02-08 04:33:41 +0000 | [diff] [blame] | 64 | |
| 65 | struct menu_item_ex { |
Jonathan Gordon | daf6694 | 2007-03-17 12:33:34 +0000 | [diff] [blame] | 66 | unsigned int flags; /* above defines */ |
Jonathan Gordon | 4718a1e | 2007-02-08 04:33:41 +0000 | [diff] [blame] | 67 | union { |
| 68 | const struct menu_item_ex **submenus; /* used with MT_MENU */ |
| 69 | void *variable; /* used with MT_SETTING, |
| 70 | must be in the settings_list.c list */ |
Jonathan Gordon | daf6694 | 2007-03-17 12:33:34 +0000 | [diff] [blame] | 71 | const struct menu_func *function; /* MT_FUNCTION_* */ |
Jonathan Gordon | 4718a1e | 2007-02-08 04:33:41 +0000 | [diff] [blame] | 72 | const char **strings; /* used with MT_RETURN_ID */ |
Jonathan Gordon | 91cb68a | 2007-03-01 11:14:46 +0000 | [diff] [blame] | 73 | int value; /* MT_RETURN_VALUE */ |
Jonathan Gordon | 4718a1e | 2007-02-08 04:33:41 +0000 | [diff] [blame] | 74 | }; |
| 75 | union { |
Jonathan Gordon | 8ca99d3 | 2007-02-27 11:09:09 +0000 | [diff] [blame] | 76 | /* For settings */ |
Jonathan Gordon | 4718a1e | 2007-02-08 04:33:41 +0000 | [diff] [blame] | 77 | int (*menu_callback)(int action, const struct menu_item_ex *this_item); |
Jonathan Gordon | 8ca99d3 | 2007-02-27 11:09:09 +0000 | [diff] [blame] | 78 | /* For everything else, except if the text is dynamic */ |
Jonathan Gordon | 4718a1e | 2007-02-08 04:33:41 +0000 | [diff] [blame] | 79 | const struct menu_callback_with_desc { |
| 80 | int (*menu_callback)(int action, |
| 81 | const struct menu_item_ex *this_item); |
| 82 | unsigned char *desc; /* string or ID */ |
Jonathan Gordon | 02a8717 | 2007-03-03 13:52:14 +0000 | [diff] [blame] | 83 | int icon_id; /* from icons_6x8 in icons.h */ |
Jonathan Gordon | 4718a1e | 2007-02-08 04:33:41 +0000 | [diff] [blame] | 84 | } *callback_and_desc; |
Jonathan Gordon | 8ca99d3 | 2007-02-27 11:09:09 +0000 | [diff] [blame] | 85 | /* For when the item text is dynamic */ |
| 86 | const struct menu_get_name_and_icon { |
| 87 | int (*menu_callback)(int action, |
Jonathan Gordon | 2801a87 | 2007-02-19 02:14:51 +0000 | [diff] [blame] | 88 | const struct menu_item_ex *this_item); |
Jonathan Gordon | 8ca99d3 | 2007-02-27 11:09:09 +0000 | [diff] [blame] | 89 | char *(*list_get_name)(int selected_item, void * data, char *buffer); |
| 90 | void *list_get_name_data; |
Jonathan Gordon | 02a8717 | 2007-03-03 13:52:14 +0000 | [diff] [blame] | 91 | int icon_id; |
Jonathan Gordon | 8ca99d3 | 2007-02-27 11:09:09 +0000 | [diff] [blame] | 92 | } *menu_get_name_and_icon; |
Jonathan Gordon | 4718a1e | 2007-02-08 04:33:41 +0000 | [diff] [blame] | 93 | }; |
| 94 | }; |
| 95 | |
| 96 | typedef int (*menu_callback_type)(int action, |
| 97 | const struct menu_item_ex *this_item); |
Jonathan Gordon | 91cb68a | 2007-03-01 11:14:46 +0000 | [diff] [blame] | 98 | int do_menu(const struct menu_item_ex *menu, int *start_selected); |
Jonathan Gordon | 2801a87 | 2007-02-19 02:14:51 +0000 | [diff] [blame] | 99 | bool do_setting_from_menu(const struct menu_item_ex *temp); |
Jonathan Gordon | 4718a1e | 2007-02-08 04:33:41 +0000 | [diff] [blame] | 100 | |
Jonathan Gordon | 37d246a | 2007-02-11 10:31:50 +0000 | [diff] [blame] | 101 | /* In all the following macros the argument names are as follows: |
| 102 | - name: The name for the variable (so it can be used in a MAKE_MENU() |
| 103 | - str: the string to display for this menu item. use ID2P() for LANG_* id's |
| 104 | - callback: The callback function to call for this menu item. |
| 105 | */ |
Jonathan Gordon | 4718a1e | 2007-02-08 04:33:41 +0000 | [diff] [blame] | 106 | |
Jonathan Gordon | 37d246a | 2007-02-11 10:31:50 +0000 | [diff] [blame] | 107 | /* Use this to put a setting into a menu. |
| 108 | The setting must appear in settings_list.c. |
| 109 | If the setting is not configured properly, the menu will display "Not Done yet!" |
| 110 | When the user selects this item the setting select screen will load, |
| 111 | when that screen exits the user wll be back in the menu */ |
Jonathan Gordon | 4718a1e | 2007-02-08 04:33:41 +0000 | [diff] [blame] | 112 | #define MENUITEM_SETTING(name,var,callback) \ |
| 113 | static const struct menu_item_ex name = \ |
| 114 | {MT_SETTING, {.variable = (void*)var},{callback}}; |
| 115 | |
Jonathan Gordon | 9709086 | 2007-03-03 14:23:03 +0000 | [diff] [blame] | 116 | /* Use this for settings which have a differnt title in their |
| 117 | setting screen than in the menu (e.g scroll options */ |
| 118 | #define MENUITEM_SETTING_W_TEXT(name, var, str, callback ) \ |
| 119 | static const struct menu_callback_with_desc name##__ = {callback,str, Icon_NOICON};\ |
| 120 | static const struct menu_item_ex name = \ |
| 121 | {MT_SETTING_W_TEXT|MENU_HAS_DESC, {.variable = (void*)var }, \ |
| 122 | {.callback_and_desc = & name##__}}; |
| 123 | |
Jonathan Gordon | 37d246a | 2007-02-11 10:31:50 +0000 | [diff] [blame] | 124 | /* Use this To create a list of NON-XLATABLE (for the time being) Strings |
| 125 | When the user enters this list and selects one, the menu will exits |
| 126 | and its return value will be the index of the chosen item */ |
| 127 | #define MENUITEM_STRINGLIST(name, str, callback, ... ) \ |
Jonathan Gordon | 4718a1e | 2007-02-08 04:33:41 +0000 | [diff] [blame] | 128 | static const char *name##_[] = {__VA_ARGS__}; \ |
Jonathan Gordon | 02a8717 | 2007-03-03 13:52:14 +0000 | [diff] [blame] | 129 | static const struct menu_callback_with_desc name##__ = {callback,str, Icon_NOICON};\ |
Jonathan Gordon | 4718a1e | 2007-02-08 04:33:41 +0000 | [diff] [blame] | 130 | static const struct menu_item_ex name = \ |
| 131 | {MT_RETURN_ID|MENU_HAS_DESC| \ |
| 132 | MENU_ITEM_COUNT(sizeof( name##_)/sizeof(*name##_)), \ |
| 133 | { .submenus = name##_},{.callback_and_desc = & name##__}}; |
Jonathan Gordon | 5599d68 | 2007-02-14 06:58:30 +0000 | [diff] [blame] | 134 | |
Jonathan Gordon | 37d246a | 2007-02-11 10:31:50 +0000 | [diff] [blame] | 135 | |
Jonathan Gordon | 91cb68a | 2007-03-01 11:14:46 +0000 | [diff] [blame] | 136 | /* returns a value associated with the item */ |
| 137 | #define MENUITEM_RETURNVALUE(name, str, val, cb, icon) \ |
| 138 | static const struct menu_callback_with_desc name##_ = {cb,str,icon}; \ |
| 139 | static const struct menu_item_ex name = \ |
| 140 | { MT_RETURN_VALUE|MENU_HAS_DESC, { .value = val}, \ |
| 141 | {.callback_and_desc = & name##_}}; |
| 142 | |
| 143 | /* same as above, except the item name is dynamic */ |
| 144 | #define MENUITEM_RETURNVALUE_DYNTEXT(name, val, cb, text_callback, text_cb_data, icon) \ |
| 145 | static const struct menu_get_name_and_icon name##_ \ |
| 146 | = {cb,text_callback,text_cb_data,icon}; \ |
| 147 | static const struct menu_item_ex name = \ |
| 148 | { MT_RETURN_VALUE|MENU_DYNAMIC_DESC, { .value = val}, \ |
| 149 | {.menu_get_name_and_icon = & name##_}}; |
| 150 | |
Jonathan Gordon | 37d246a | 2007-02-11 10:31:50 +0000 | [diff] [blame] | 151 | /* Use this to put a function call into the menu. |
| 152 | When the user selects this item the function will be run, |
Jonathan Gordon | daf6694 | 2007-03-17 12:33:34 +0000 | [diff] [blame] | 153 | unless MENU_FUNC_IGNORE_RETVAL is set, when it exits the user |
| 154 | will be back in the menu and return value is ignored, |
| 155 | else if it returns exit_if do_mneu() will exit */ |
| 156 | #define MENUITEM_FUNCTION(name, flags, str, func, param, \ |
| 157 | exit_if, callback, icon) \ |
Jonathan Gordon | 5599d68 | 2007-02-14 06:58:30 +0000 | [diff] [blame] | 158 | static const struct menu_callback_with_desc name##_ = {callback,str,icon}; \ |
Jonathan Gordon | daf6694 | 2007-03-17 12:33:34 +0000 | [diff] [blame] | 159 | static const struct menu_func name##__ = {{(void*)func}, param, exit_if}; \ |
| 160 | /* should be const, but recording_settings wont let us do that */ \ |
| 161 | const struct menu_item_ex name = \ |
| 162 | { MT_FUNCTION_CALL|MENU_HAS_DESC|flags, \ |
| 163 | { .function = & name##__}, {.callback_and_desc = & name##_}}; |
Jonathan Gordon | 8ca99d3 | 2007-02-27 11:09:09 +0000 | [diff] [blame] | 164 | |
Jonathan Gordon | 2801a87 | 2007-02-19 02:14:51 +0000 | [diff] [blame] | 165 | /* As above, except the text is dynamic */ |
Jonathan Gordon | daf6694 | 2007-03-17 12:33:34 +0000 | [diff] [blame] | 166 | #define MENUITEM_FUNCTION_DYNTEXT(name, flags, func, param, exit_if, \ |
| 167 | text_callback, text_cb_data, callback, icon) \ |
Jonathan Gordon | 8ca99d3 | 2007-02-27 11:09:09 +0000 | [diff] [blame] | 168 | static const struct menu_get_name_and_icon name##_ \ |
Jonathan Gordon | daf6694 | 2007-03-17 12:33:34 +0000 | [diff] [blame] | 169 | = {callback,text_callback,text_cb_data,icon}; \ |
| 170 | static const struct menu_func name##__ = {{(void*)func}, param, exit_if}; \ |
| 171 | static const struct menu_item_ex name = \ |
| 172 | { MT_FUNCTION_CALL|MENU_DYNAMIC_DESC|flags, \ |
| 173 | { .function = & name##__}, {.menu_get_name_and_icon = & name##_}}; |
Jonathan Gordon | 4718a1e | 2007-02-08 04:33:41 +0000 | [diff] [blame] | 174 | |
Jonathan Gordon | 37d246a | 2007-02-11 10:31:50 +0000 | [diff] [blame] | 175 | /* Use this to actually create a menu. the ... argument is a list of pointers |
| 176 | to any of the above macro'd variables. (It can also have other menus in the list. */ |
Jonathan Gordon | 5599d68 | 2007-02-14 06:58:30 +0000 | [diff] [blame] | 177 | #define MAKE_MENU( name, str, callback, icon, ... ) \ |
Jonathan Gordon | 37d246a | 2007-02-11 10:31:50 +0000 | [diff] [blame] | 178 | static const struct menu_item_ex *name##_[] = {__VA_ARGS__}; \ |
Jonathan Gordon | 5599d68 | 2007-02-14 06:58:30 +0000 | [diff] [blame] | 179 | static const struct menu_callback_with_desc name##__ = {callback,str,icon};\ |
Jonathan Gordon | 37d246a | 2007-02-11 10:31:50 +0000 | [diff] [blame] | 180 | const struct menu_item_ex name = \ |
| 181 | {MT_MENU|MENU_HAS_DESC| \ |
| 182 | MENU_ITEM_COUNT(sizeof( name##_)/sizeof(*name##_)), \ |
| 183 | { (void*)name##_},{.callback_and_desc = & name##__}}; |
Jonathan Gordon | 91cb68a | 2007-03-01 11:14:46 +0000 | [diff] [blame] | 184 | |
Jonathan Gordon | 5599d68 | 2007-02-14 06:58:30 +0000 | [diff] [blame] | 185 | |
Jonathan Gordon | 3452146 | 2007-03-07 13:00:46 +0000 | [diff] [blame] | 186 | /* OLD API - only use if you really have to.. Ideally this will be dropped */ |
| 187 | struct menu_item { |
| 188 | unsigned char *desc; /* string or ID */ |
| 189 | bool (*function) (void); /* return true if USB was connected */ |
| 190 | }; |
| 191 | |
| 192 | int menu_init(const struct menu_item* mitems, int count, |
| 193 | int (*callback)(int, int), |
| 194 | const char *button1, const char *button2, const char *button3); |
| 195 | void menu_exit(int menu); |
| 196 | |
| 197 | /* Returns below define, or number of selected menu item*/ |
| 198 | int menu_show(int m); |
| 199 | #define MENU_ATTACHED_USB -1 |
| 200 | #define MENU_SELECTED_EXIT -2 |
| 201 | #define MENU_EXIT_ALL -3 |
| 202 | #define MENU_RETURN_TO_WPS -4 |
| 203 | |
| 204 | bool menu_run(int menu); |
| 205 | int menu_count(int menu); |
Jonathan Gordon | 02a8717 | 2007-03-03 13:52:14 +0000 | [diff] [blame] | 206 | |
Daniel Stenberg | 1c0c861 | 2002-05-17 12:22:24 +0000 | [diff] [blame] | 207 | #endif /* End __MENU_H__ */ |