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