Revert "Replace yield_codec() with a call to queue_wait_w_tmo()" and the related...
[Rockbox.git] / apps / menu.c
blob35b2527ccee7ad17c2e9a0507745eaf394cf4a20
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2002 Robert E. Hak
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.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
20 2005 Kevin Ferrare :
21 - Multi screen support
22 - Rewrote/removed a lot of code now useless with the new gui API
24 #include <stdbool.h>
25 #include <stdlib.h>
26 #include "config.h"
28 #include "lcd.h"
29 #include "font.h"
30 #include "backlight.h"
31 #include "menu.h"
32 #include "button.h"
33 #include "kernel.h"
34 #include "debug.h"
35 #include "usb.h"
36 #include "panic.h"
37 #include "settings.h"
38 #include "settings_list.h"
39 #include "option_select.h"
40 #include "status.h"
41 #include "screens.h"
42 #include "talk.h"
43 #include "lang.h"
44 #include "misc.h"
45 #include "action.h"
46 #include "menus/exported_menus.h"
47 #include "string.h"
48 #include "root_menu.h"
49 #include "bookmark.h"
50 #include "gwps-common.h" /* for fade() */
51 #include "audio.h"
53 #ifdef HAVE_LCD_BITMAP
54 #include "icons.h"
55 #endif
57 /* gui api */
58 #include "list.h"
59 #include "statusbar.h"
60 #include "buttonbar.h"
62 #define MAX_MENUS 8
63 /* used to allow for dynamic menus */
64 #define MAX_MENU_SUBITEMS 64
65 static struct menu_item_ex *current_submenus_menu;
66 static int current_subitems[MAX_MENU_SUBITEMS];
67 static int current_subitems_count = 0;
68 static int talk_menu_item(int selected_item, void *data);
70 static void get_menu_callback(const struct menu_item_ex *m,
71 menu_callback_type *menu_callback)
73 if (m->flags&(MENU_HAS_DESC|MENU_DYNAMIC_DESC))
74 *menu_callback= m->callback_and_desc->menu_callback;
75 else
76 *menu_callback = m->menu_callback;
79 static int get_menu_selection(int selected_item, const struct menu_item_ex *menu)
81 int type = (menu->flags&MENU_TYPE_MASK);
82 if (type == MT_MENU && (selected_item<current_subitems_count))
83 return current_subitems[selected_item];
84 return selected_item;
86 static int find_menu_selection(int selected)
88 int i;
89 for (i=0; i< current_subitems_count; i++)
90 if (current_subitems[i] == selected)
91 return i;
92 return 0;
94 static char * get_menu_item_name(int selected_item,void * data, char *buffer)
96 const struct menu_item_ex *menu = (const struct menu_item_ex *)data;
97 int type = (menu->flags&MENU_TYPE_MASK);
98 selected_item = get_menu_selection(selected_item, menu);
100 (void)buffer;
101 /* only MT_MENU or MT_RETURN_ID is allowed in here */
102 if (type == MT_RETURN_ID)
104 if (menu->flags&MENU_DYNAMIC_DESC)
105 return menu->menu_get_name_and_icon->list_get_name(selected_item,
106 menu->menu_get_name_and_icon->list_get_name_data, buffer);
107 return (char*)menu->strings[selected_item];
110 menu = menu->submenus[selected_item];
112 if ((menu->flags&MENU_DYNAMIC_DESC) && (type != MT_SETTING_W_TEXT))
113 return menu->menu_get_name_and_icon->list_get_name(selected_item,
114 menu->menu_get_name_and_icon->list_get_name_data, buffer);
116 type = (menu->flags&MENU_TYPE_MASK);
117 if ((type == MT_SETTING) || (type == MT_SETTING_W_TEXT))
119 const struct settings_list *v
120 = find_setting(menu->variable, NULL);
121 if (v)
122 return str(v->lang_id);
123 else return "Not Done yet!";
125 return P2STR(menu->callback_and_desc->desc);
127 #ifdef HAVE_LCD_BITMAP
128 static int menu_get_icon(int selected_item, void * data)
130 const struct menu_item_ex *menu = (const struct menu_item_ex *)data;
131 int menu_icon = Icon_NOICON;
132 selected_item = get_menu_selection(selected_item, menu);
134 if ((menu->flags&MENU_TYPE_MASK) == MT_RETURN_ID)
136 return Icon_Menu_functioncall;
138 menu = menu->submenus[selected_item];
139 if (menu->flags&MENU_HAS_DESC)
140 menu_icon = menu->callback_and_desc->icon_id;
141 else if (menu->flags&MENU_DYNAMIC_DESC)
142 menu_icon = menu->menu_get_name_and_icon->icon_id;
144 if (menu_icon == Icon_NOICON)
146 switch (menu->flags&MENU_TYPE_MASK)
148 case MT_SETTING:
149 case MT_SETTING_W_TEXT:
150 menu_icon = Icon_Menu_setting;
151 break;
152 case MT_MENU:
153 menu_icon = Icon_Submenu;
154 break;
155 case MT_FUNCTION_CALL:
156 case MT_RETURN_VALUE:
157 menu_icon = Icon_Menu_functioncall;
158 break;
161 return menu_icon;
163 #endif
165 static void init_menu_lists(const struct menu_item_ex *menu,
166 struct gui_synclist *lists, int selected, bool callback)
168 int i, count = MENU_GET_COUNT(menu->flags);
169 int type = (menu->flags&MENU_TYPE_MASK);
170 menu_callback_type menu_callback = NULL;
171 int icon;
172 current_subitems_count = 0;
174 if (type == MT_RETURN_ID)
175 get_menu_callback(menu, &menu_callback);
177 for (i=0; i<count; i++)
179 if (type != MT_RETURN_ID)
180 get_menu_callback(menu->submenus[i],&menu_callback);
181 if (menu_callback)
183 if (menu_callback(ACTION_REQUEST_MENUITEM,
184 type==MT_RETURN_ID ? (void*)(intptr_t)i: menu->submenus[i])
185 != ACTION_EXIT_MENUITEM)
187 current_subitems[current_subitems_count] = i;
188 current_subitems_count++;
191 else
193 current_subitems[current_subitems_count] = i;
194 current_subitems_count++;
197 current_submenus_menu = (struct menu_item_ex *)menu;
199 gui_synclist_init(lists,get_menu_item_name,(void*)menu,false,1);
200 #ifdef HAVE_LCD_BITMAP
201 if (menu->callback_and_desc->icon_id == Icon_NOICON)
202 icon = Icon_Submenu_Entered;
203 else
204 icon = menu->callback_and_desc->icon_id;
205 gui_synclist_set_title(lists, P2STR(menu->callback_and_desc->desc), icon);
206 gui_synclist_set_icon_callback(lists, menu_get_icon);
207 #else
208 (void)icon;
209 gui_synclist_set_icon_callback(lists, NULL);
210 #endif
211 if(global_settings.talk_menu)
212 gui_synclist_set_voice_callback(lists, talk_menu_item);
213 gui_synclist_set_nb_items(lists,current_subitems_count);
214 gui_synclist_limit_scroll(lists,true);
215 gui_synclist_select_item(lists, find_menu_selection(selected));
217 get_menu_callback(menu,&menu_callback);
218 if (callback && menu_callback)
219 menu_callback(ACTION_ENTER_MENUITEM,menu);
220 gui_synclist_draw(lists);
221 gui_synclist_speak_item(lists);
224 static int talk_menu_item(int selected_item, void *data)
226 const struct menu_item_ex *menu = (const struct menu_item_ex *)data;
227 int id = -1;
228 int type;
229 unsigned char *str;
230 int sel = get_menu_selection(selected_item, menu);
232 if ((menu->flags&MENU_TYPE_MASK) == MT_MENU)
234 type = menu->submenus[sel]->flags&MENU_TYPE_MASK;
235 if ((type == MT_SETTING) || (type == MT_SETTING_W_TEXT))
236 talk_setting(menu->submenus[sel]->variable);
237 else
239 if (menu->submenus[sel]->flags&(MENU_DYNAMIC_DESC))
241 int (*list_speak_item)(int selected_item, void * data)
242 = menu->submenus[sel]->menu_get_name_and_icon->
243 list_speak_item;
244 if(list_speak_item)
245 list_speak_item(sel, menu->submenus[sel]->
246 menu_get_name_and_icon->
247 list_get_name_data);
248 else
250 char buffer[80];
251 str = menu->submenus[sel]->menu_get_name_and_icon->
252 list_get_name(sel, menu->submenus[sel]->
253 menu_get_name_and_icon->
254 list_get_name_data, buffer);
255 id = P2ID(str);
258 else
259 id = P2ID(menu->submenus[sel]->callback_and_desc->desc);
260 if (id != -1)
261 talk_id(id,false);
264 else if(((menu->flags&MENU_TYPE_MASK) == MT_RETURN_ID))
266 if ((menu->flags&MENU_DYNAMIC_DESC) == 0)
268 unsigned char *s = (unsigned char *)menu->strings[sel];
269 /* string list, try to talk it if ID2P was used */
270 id = P2ID(s);
271 if (id != -1)
272 talk_id(id,false);
275 return 0;
277 #define MAX_OPTIONS 32
278 bool do_setting_from_menu(const struct menu_item_ex *temp)
280 int setting_id;
281 const struct settings_list *setting = find_setting(
282 temp->variable,
283 &setting_id);
284 char *title;
285 if ((temp->flags&MENU_TYPE_MASK) == MT_SETTING_W_TEXT)
286 title = temp->callback_and_desc->desc;
287 else
288 title = ID2P(setting->lang_id);
289 option_screen((struct settings_list *)setting,
290 setting->flags&F_TEMPVAR, title);
291 return false;
294 int do_menu(const struct menu_item_ex *start_menu, int *start_selected)
296 int selected = start_selected? *start_selected : 0;
297 int action;
298 struct gui_synclist lists;
299 const struct menu_item_ex *temp, *menu;
300 int ret = 0;
301 bool redraw_lists;
302 #ifdef HAS_BUTTONBAR
303 struct gui_buttonbar buttonbar;
304 #endif
306 const struct menu_item_ex *menu_stack[MAX_MENUS];
307 int menu_stack_selected_item[MAX_MENUS];
308 int stack_top = 0;
309 bool in_stringlist, done = false;
310 menu_callback_type menu_callback = NULL;
311 if (start_menu == NULL)
312 menu = &main_menu_;
313 else menu = start_menu;
314 #ifdef HAS_BUTTONBAR
315 gui_buttonbar_init(&buttonbar);
316 gui_buttonbar_set_display(&buttonbar, &(screens[SCREEN_MAIN]) );
317 gui_buttonbar_set(&buttonbar, "<<<", "", "");
318 gui_buttonbar_draw(&buttonbar);
319 #endif
320 init_menu_lists(menu,&lists,selected,true);
321 in_stringlist = ((menu->flags&MENU_TYPE_MASK) == MT_RETURN_ID);
323 /* load the callback, and only reload it if menu changes */
324 get_menu_callback(menu, &menu_callback);
326 while (!done)
328 redraw_lists = false;
329 gui_syncstatusbar_draw(&statusbars, true);
330 action = get_action(CONTEXT_MAINMENU,
331 list_do_action_timeout(&lists, HZ));
332 /* HZ so the status bar redraws corectly */
334 if (action != ACTION_NONE && menu_callback)
336 int old_action = action;
337 action = menu_callback(action, menu);
338 if (action == ACTION_EXIT_AFTER_THIS_MENUITEM)
340 action = old_action;
341 ret = MENU_SELECTED_EXIT; /* will exit after returning
342 from selection */
344 else if (action == ACTION_REDRAW)
346 action = old_action;
347 redraw_lists = true;
351 if (gui_synclist_do_button(&lists, &action, LIST_WRAP_UNLESS_HELD))
352 continue;
353 if (action == ACTION_NONE)
354 continue;
356 #ifdef HAVE_RECORDING
357 if (action == ACTION_STD_REC)
359 ret = GO_TO_RECSCREEN;
360 done = true;
362 else
363 #endif
364 if (action == ACTION_TREE_WPS)
366 ret = GO_TO_PREVIOUS_MUSIC;
367 done = true;
369 else if (action == ACTION_TREE_STOP)
371 redraw_lists = list_stop_handler();
373 else if (action == ACTION_STD_CONTEXT &&
374 menu == &root_menu_)
376 ret = GO_TO_ROOTITEM_CONTEXT;
377 done = true;
379 else if (action == ACTION_STD_MENU)
381 if (menu != &root_menu_)
382 ret = GO_TO_ROOT;
383 else
384 ret = GO_TO_PREVIOUS;
385 done = true;
387 else if (action == ACTION_STD_CANCEL)
389 in_stringlist = false;
390 if (menu_callback)
391 menu_callback(ACTION_EXIT_MENUITEM, menu);
393 if (menu->flags&MENU_EXITAFTERTHISMENU)
394 done = true;
395 if (stack_top > 0)
397 stack_top--;
398 menu = menu_stack[stack_top];
399 if (menu->flags&MENU_EXITAFTERTHISMENU)
400 done = true;
401 else
402 init_menu_lists(menu, &lists,
403 menu_stack_selected_item[stack_top], false);
404 /* new menu, so reload the callback */
405 get_menu_callback(menu, &menu_callback);
407 else if (menu != &root_menu_)
409 ret = GO_TO_PREVIOUS;
410 done = true;
413 else if (action == ACTION_STD_OK)
415 int type;
416 #ifdef HAS_BUTTONBAR
417 gui_buttonbar_unset(&buttonbar);
418 gui_buttonbar_draw(&buttonbar);
419 #endif
420 selected = get_menu_selection(gui_synclist_get_sel_pos(&lists), menu);
421 temp = menu->submenus[selected];
422 redraw_lists = true;
423 if (in_stringlist)
424 type = (menu->flags&MENU_TYPE_MASK);
425 else
427 type = (temp->flags&MENU_TYPE_MASK);
428 get_menu_callback(temp, &menu_callback);
429 if (menu_callback)
431 action = menu_callback(ACTION_ENTER_MENUITEM,temp);
432 if (action == ACTION_EXIT_MENUITEM)
433 break;
436 switch (type)
438 case MT_MENU:
439 if (stack_top < MAX_MENUS)
441 menu_stack[stack_top] = menu;
442 menu_stack_selected_item[stack_top] = selected;
443 stack_top++;
444 init_menu_lists(temp, &lists, 0, true);
445 redraw_lists = false; /* above does the redraw */
446 menu = temp;
448 break;
449 case MT_FUNCTION_CALL:
451 int return_value;
452 if (temp->flags&MENU_FUNC_USEPARAM)
453 return_value = temp->function->function_w_param(
454 temp->function->param);
455 else
456 return_value = temp->function->function();
457 if (temp->flags&MENU_FUNC_CHECK_RETVAL)
459 if (return_value == 1)
461 done = true;
462 ret = return_value;
465 break;
467 case MT_SETTING:
468 case MT_SETTING_W_TEXT:
470 if (do_setting_from_menu(temp))
472 init_menu_lists(menu, &lists, selected, true);
473 redraw_lists = false; /* above does the redraw */
475 break;
477 case MT_RETURN_ID:
478 if (in_stringlist)
480 done = true;
481 ret = selected;
483 else if (stack_top < MAX_MENUS)
485 menu_stack[stack_top] = menu;
486 menu_stack_selected_item[stack_top] = selected;
487 stack_top++;
488 menu = temp;
489 init_menu_lists(menu,&lists,0,false);
490 redraw_lists = false; /* above does the redraw */
491 in_stringlist = true;
493 break;
494 case MT_RETURN_VALUE:
495 ret = temp->value;
496 done = true;
497 break;
499 if (type != MT_MENU)
501 if (menu_callback)
502 menu_callback(ACTION_EXIT_MENUITEM,temp);
504 if (current_submenus_menu != menu)
505 init_menu_lists(menu,&lists,selected,true);
506 /* callback was changed, so reload the menu's callback */
507 get_menu_callback(menu, &menu_callback);
508 if ((menu->flags&MENU_EXITAFTERTHISMENU) &&
509 !(temp->flags&MENU_EXITAFTERTHISMENU))
511 done = true;
512 break;
514 #ifdef HAS_BUTTONBAR
515 gui_buttonbar_set(&buttonbar, "<<<", "", "");
516 gui_buttonbar_draw(&buttonbar);
517 #endif
519 else if(default_event_handler(action) == SYS_USB_CONNECTED)
521 ret = MENU_ATTACHED_USB;
522 done = true;
525 if (redraw_lists && !done)
527 gui_synclist_draw(&lists);
528 gui_synclist_speak_item(&lists);
531 if (start_selected)
533 /* make sure the start_selected variable is set to
534 the selected item from the menu do_menu() was called from */
535 if (stack_top > 0)
537 menu = menu_stack[0];
538 init_menu_lists(menu,&lists,menu_stack_selected_item[0],true);
540 *start_selected = get_menu_selection(
541 gui_synclist_get_sel_pos(&lists), menu);
543 return ret;