Describe "jump to default" feature (FS#8919 by Alexander Levin).
[Rockbox.git] / apps / menu.c
blobe29b9c4e7651ba981f55f28c2f1a22bf6d8579cc
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"
27 #include "system.h"
29 #include "lcd.h"
30 #include "font.h"
31 #include "backlight.h"
32 #include "menu.h"
33 #include "button.h"
34 #include "kernel.h"
35 #include "debug.h"
36 #include "usb.h"
37 #include "panic.h"
38 #include "settings.h"
39 #include "settings_list.h"
40 #include "option_select.h"
41 #include "status.h"
42 #include "screens.h"
43 #include "talk.h"
44 #include "lang.h"
45 #include "misc.h"
46 #include "action.h"
47 #include "menus/exported_menus.h"
48 #include "string.h"
49 #include "root_menu.h"
50 #include "bookmark.h"
51 #include "gwps-common.h" /* for fade() */
52 #include "audio.h"
53 #include "viewport.h"
55 #ifdef HAVE_LCD_BITMAP
56 #include "icons.h"
57 #endif
59 /* gui api */
60 #include "list.h"
61 #include "statusbar.h"
62 #include "buttonbar.h"
64 #define MAX_MENUS 8
65 /* used to allow for dynamic menus */
66 #define MAX_MENU_SUBITEMS 64
67 static struct menu_item_ex *current_submenus_menu;
68 static int current_subitems[MAX_MENU_SUBITEMS];
69 static int current_subitems_count = 0;
70 static int talk_menu_item(int selected_item, void *data);
72 static void get_menu_callback(const struct menu_item_ex *m,
73 menu_callback_type *menu_callback)
75 if (m->flags&(MENU_HAS_DESC|MENU_DYNAMIC_DESC))
76 *menu_callback= m->callback_and_desc->menu_callback;
77 else
78 *menu_callback = m->menu_callback;
81 static int get_menu_selection(int selected_item, const struct menu_item_ex *menu)
83 int type = (menu->flags&MENU_TYPE_MASK);
84 if (type == MT_MENU && (selected_item<current_subitems_count))
85 return current_subitems[selected_item];
86 return selected_item;
88 static int find_menu_selection(int selected)
90 int i;
91 for (i=0; i< current_subitems_count; i++)
92 if (current_subitems[i] == selected)
93 return i;
94 return 0;
96 static char * get_menu_item_name(int selected_item,
97 void * data,
98 char *buffer,
99 size_t buffer_len)
101 const struct menu_item_ex *menu = (const struct menu_item_ex *)data;
102 int type = (menu->flags&MENU_TYPE_MASK);
103 selected_item = get_menu_selection(selected_item, menu);
105 (void)buffer_len;
106 /* only MT_MENU or MT_RETURN_ID is allowed in here */
107 if (type == MT_RETURN_ID)
109 if (menu->flags&MENU_DYNAMIC_DESC)
110 return menu->menu_get_name_and_icon->list_get_name(selected_item,
111 menu->menu_get_name_and_icon->list_get_name_data, buffer);
112 return (char*)menu->strings[selected_item];
115 menu = menu->submenus[selected_item];
117 if ((menu->flags&MENU_DYNAMIC_DESC) && (type != MT_SETTING_W_TEXT))
118 return menu->menu_get_name_and_icon->list_get_name(selected_item,
119 menu->menu_get_name_and_icon->list_get_name_data, buffer);
121 type = (menu->flags&MENU_TYPE_MASK);
122 if ((type == MT_SETTING) || (type == MT_SETTING_W_TEXT))
124 const struct settings_list *v
125 = find_setting(menu->variable, NULL);
126 if (v)
127 return str(v->lang_id);
128 else return "Not Done yet!";
130 return P2STR(menu->callback_and_desc->desc);
132 #ifdef HAVE_LCD_BITMAP
133 static int menu_get_icon(int selected_item, void * data)
135 const struct menu_item_ex *menu = (const struct menu_item_ex *)data;
136 int menu_icon = Icon_NOICON;
137 selected_item = get_menu_selection(selected_item, menu);
139 if ((menu->flags&MENU_TYPE_MASK) == MT_RETURN_ID)
141 return Icon_Menu_functioncall;
143 menu = menu->submenus[selected_item];
144 if (menu->flags&MENU_HAS_DESC)
145 menu_icon = menu->callback_and_desc->icon_id;
146 else if (menu->flags&MENU_DYNAMIC_DESC)
147 menu_icon = menu->menu_get_name_and_icon->icon_id;
149 if (menu_icon == Icon_NOICON)
151 switch (menu->flags&MENU_TYPE_MASK)
153 case MT_SETTING:
154 case MT_SETTING_W_TEXT:
155 menu_icon = Icon_Menu_setting;
156 break;
157 case MT_MENU:
158 menu_icon = Icon_Submenu;
159 break;
160 case MT_FUNCTION_CALL:
161 case MT_RETURN_VALUE:
162 menu_icon = Icon_Menu_functioncall;
163 break;
166 return menu_icon;
168 #endif
170 static void init_menu_lists(const struct menu_item_ex *menu,
171 struct gui_synclist *lists, int selected, bool callback,
172 struct viewport parent[NB_SCREENS])
174 int i, count = MENU_GET_COUNT(menu->flags);
175 int type = (menu->flags&MENU_TYPE_MASK);
176 menu_callback_type menu_callback = NULL;
177 int icon;
178 current_subitems_count = 0;
180 if (type == MT_RETURN_ID)
181 get_menu_callback(menu, &menu_callback);
183 for (i=0; i<count; i++)
185 if (type != MT_RETURN_ID)
186 get_menu_callback(menu->submenus[i],&menu_callback);
187 if (menu_callback)
189 if (menu_callback(ACTION_REQUEST_MENUITEM,
190 type==MT_RETURN_ID ? (void*)(intptr_t)i: menu->submenus[i])
191 != ACTION_EXIT_MENUITEM)
193 current_subitems[current_subitems_count] = i;
194 current_subitems_count++;
197 else
199 current_subitems[current_subitems_count] = i;
200 current_subitems_count++;
203 current_submenus_menu = (struct menu_item_ex *)menu;
205 gui_synclist_init(lists,get_menu_item_name,(void*)menu,false,1, parent);
206 #ifdef HAVE_LCD_BITMAP
207 if (menu->callback_and_desc->icon_id == Icon_NOICON)
208 icon = Icon_Submenu_Entered;
209 else
210 icon = menu->callback_and_desc->icon_id;
211 gui_synclist_set_title(lists, P2STR(menu->callback_and_desc->desc), icon);
212 gui_synclist_set_icon_callback(lists, menu_get_icon);
213 #else
214 (void)icon;
215 gui_synclist_set_icon_callback(lists, NULL);
216 #endif
217 if(global_settings.talk_menu)
218 gui_synclist_set_voice_callback(lists, talk_menu_item);
219 gui_synclist_set_nb_items(lists,current_subitems_count);
220 gui_synclist_limit_scroll(lists,true);
221 gui_synclist_select_item(lists, find_menu_selection(selected));
223 get_menu_callback(menu,&menu_callback);
224 if (callback && menu_callback)
225 menu_callback(ACTION_ENTER_MENUITEM,menu);
226 gui_synclist_draw(lists);
227 gui_synclist_speak_item(lists);
230 static int talk_menu_item(int selected_item, void *data)
232 const struct menu_item_ex *menu = (const struct menu_item_ex *)data;
233 int id = -1;
234 int type;
235 unsigned char *str;
236 int sel = get_menu_selection(selected_item, menu);
238 if ((menu->flags&MENU_TYPE_MASK) == MT_MENU)
240 type = menu->submenus[sel]->flags&MENU_TYPE_MASK;
241 if ((type == MT_SETTING) || (type == MT_SETTING_W_TEXT))
242 talk_setting(menu->submenus[sel]->variable);
243 else
245 if (menu->submenus[sel]->flags&(MENU_DYNAMIC_DESC))
247 int (*list_speak_item)(int selected_item, void * data)
248 = menu->submenus[sel]->menu_get_name_and_icon->
249 list_speak_item;
250 if(list_speak_item)
251 list_speak_item(sel, menu->submenus[sel]->
252 menu_get_name_and_icon->
253 list_get_name_data);
254 else
256 char buffer[80];
257 str = menu->submenus[sel]->menu_get_name_and_icon->
258 list_get_name(sel, menu->submenus[sel]->
259 menu_get_name_and_icon->
260 list_get_name_data, buffer);
261 id = P2ID(str);
264 else
265 id = P2ID(menu->submenus[sel]->callback_and_desc->desc);
266 if (id != -1)
267 talk_id(id,false);
270 else if(((menu->flags&MENU_TYPE_MASK) == MT_RETURN_ID))
272 if ((menu->flags&MENU_DYNAMIC_DESC) == 0)
274 unsigned char *s = (unsigned char *)menu->strings[sel];
275 /* string list, try to talk it if ID2P was used */
276 id = P2ID(s);
277 if (id != -1)
278 talk_id(id,false);
281 return 0;
283 /* this is used to reload the default menu viewports when the
284 theme changes. nothing happens if the menu is using a supplied parent vp */
285 void init_default_menu_viewports(struct viewport parent[NB_SCREENS], bool hide_bars)
287 int i;
288 FOR_NB_SCREENS(i)
290 viewport_set_defaults(&parent[i], i);
291 /* viewport_set_defaults() fixes the vp for the bars, so resize */
292 if (hide_bars)
294 if (global_settings.statusbar)
296 parent[i].y -= STATUSBAR_HEIGHT;
297 parent[i].height += STATUSBAR_HEIGHT;
301 #ifdef HAS_BUTTONBAR
302 if (!hide_bars && global_settings.buttonbar)
303 parent[0].height -= BUTTONBAR_HEIGHT;
304 #endif
307 bool do_setting_from_menu(const struct menu_item_ex *temp,
308 struct viewport parent[NB_SCREENS])
310 int setting_id, oldval;
311 const struct settings_list *setting = find_setting(
312 temp->variable,
313 &setting_id);
314 char *title;
315 char padded_title[MAX_PATH];
316 int var_type = setting->flags&F_T_MASK;
317 if (var_type == F_T_INT || var_type == F_T_UINT)
319 oldval = *(int*)setting->setting;
321 else if (var_type == F_T_BOOL)
323 oldval = *(bool*)setting->setting;
325 else
326 oldval = 0;
327 if ((temp->flags&MENU_TYPE_MASK) == MT_SETTING_W_TEXT)
328 title = temp->callback_and_desc->desc;
329 else
330 title = ID2P(setting->lang_id);
332 /* Pad the title string by repeating it. This is needed
333 so the scroll settings title can actually be used to
334 test the setting */
335 if (setting->flags&F_PADTITLE)
337 int i = 0, len;
338 if (setting->lang_id == -1)
339 title = (char*)setting->cfg_vals;
340 else
341 title = P2STR((unsigned char*)title);
342 len = strlen(title);
343 while (i < MAX_PATH-1)
345 int padlen = MIN(len, MAX_PATH-1-i);
346 strncpy(&padded_title[i], title, padlen);
347 i += padlen;
348 if (i<MAX_PATH-1)
349 padded_title[i++] = ' ';
351 padded_title[i] = '\0';
352 title = padded_title;
355 option_screen((struct settings_list *)setting, parent,
356 setting->flags&F_TEMPVAR, title);
357 if (var_type == F_T_INT || var_type == F_T_UINT)
359 return oldval != *(int*)setting->setting;
361 else if (var_type == F_T_BOOL)
363 return oldval != *(bool*)setting->setting;
365 return false;
368 /* display a menu */
369 int do_menu(const struct menu_item_ex *start_menu, int *start_selected,
370 struct viewport parent[NB_SCREENS], bool hide_bars)
372 int selected = start_selected? *start_selected : 0;
373 int action;
374 struct gui_synclist lists;
375 const struct menu_item_ex *temp, *menu;
376 int ret = 0, i;
377 bool redraw_lists;
379 const struct menu_item_ex *menu_stack[MAX_MENUS];
380 int menu_stack_selected_item[MAX_MENUS];
381 int stack_top = 0;
382 bool in_stringlist, done = false;
384 struct viewport *vps, menu_vp[NB_SCREENS]; /* menu_vp will hopefully be phased out */
385 #ifdef HAS_BUTTONBAR
386 struct gui_buttonbar buttonbar;
387 gui_buttonbar_init(&buttonbar);
388 gui_buttonbar_set_display(&buttonbar, &(screens[SCREEN_MAIN]) );
389 gui_buttonbar_set(&buttonbar, "<<<", "", "");
390 #endif
392 menu_callback_type menu_callback = NULL;
393 if (start_menu == NULL)
394 menu = &main_menu_;
395 else menu = start_menu;
397 if (parent)
399 vps = parent;
400 /* if hide_bars == true we assume the viewport is correctly sized */
402 else
404 vps = menu_vp;
405 init_default_menu_viewports(vps, hide_bars);
407 FOR_NB_SCREENS(i)
409 screens[i].set_viewport(&vps[i]);
410 screens[i].clear_viewport();
411 screens[i].set_viewport(NULL);
413 init_menu_lists(menu, &lists, selected, true, vps);
414 in_stringlist = ((menu->flags&MENU_TYPE_MASK) == MT_RETURN_ID);
416 /* load the callback, and only reload it if menu changes */
417 get_menu_callback(menu, &menu_callback);
420 #ifdef HAS_BUTTONBAR
421 if (!hide_bars)
423 gui_buttonbar_set(&buttonbar, "<<<", "", "");
424 gui_buttonbar_draw(&buttonbar);
426 #endif
427 while (!done)
429 redraw_lists = false;
430 if (!hide_bars)
432 gui_syncstatusbar_draw(&statusbars, true);
434 action = get_action(CONTEXT_MAINMENU,
435 list_do_action_timeout(&lists, HZ));
436 /* HZ so the status bar redraws corectly */
438 if (action != ACTION_NONE && menu_callback)
440 int old_action = action;
441 action = menu_callback(action, menu);
442 if (action == ACTION_EXIT_AFTER_THIS_MENUITEM)
444 action = old_action;
445 ret = MENU_SELECTED_EXIT; /* will exit after returning
446 from selection */
448 else if (action == ACTION_REDRAW)
450 action = old_action;
451 redraw_lists = true;
455 if (gui_synclist_do_button(&lists, &action, LIST_WRAP_UNLESS_HELD))
456 continue;
457 if (action == ACTION_NONE)
458 continue;
459 #ifdef HAVE_QUICKSCREEN
460 else if (action == ACTION_STD_QUICKSCREEN)
462 quick_screen_quick(action);
463 redraw_lists = true;
465 #endif
466 #ifdef HAVE_RECORDING
467 else if (action == ACTION_STD_REC)
469 ret = GO_TO_RECSCREEN;
470 done = true;
472 #endif
473 else if (action == ACTION_TREE_WPS)
475 ret = GO_TO_PREVIOUS_MUSIC;
476 done = true;
478 else if (action == ACTION_TREE_STOP)
480 redraw_lists = list_stop_handler();
482 else if (action == ACTION_STD_CONTEXT &&
483 menu == &root_menu_)
485 ret = GO_TO_ROOTITEM_CONTEXT;
486 done = true;
488 else if (action == ACTION_STD_MENU)
490 if (menu != &root_menu_)
491 ret = GO_TO_ROOT;
492 else
493 ret = GO_TO_PREVIOUS;
494 done = true;
496 else if (action == ACTION_STD_CANCEL)
498 bool exiting_menu = false;
499 in_stringlist = false;
500 if (menu_callback)
501 menu_callback(ACTION_EXIT_MENUITEM, menu);
503 if (menu->flags&MENU_EXITAFTERTHISMENU)
504 done = true;
505 else if ((menu->flags&MENU_TYPE_MASK) == MT_MENU)
506 exiting_menu = true;
507 if (stack_top > 0)
509 stack_top--;
510 menu = menu_stack[stack_top];
511 if (!exiting_menu && (menu->flags&MENU_EXITAFTERTHISMENU))
512 done = true;
513 else
514 init_menu_lists(menu, &lists,
515 menu_stack_selected_item[stack_top], false, vps);
516 /* new menu, so reload the callback */
517 get_menu_callback(menu, &menu_callback);
519 else if (menu != &root_menu_)
521 ret = GO_TO_PREVIOUS;
522 done = true;
525 else if (action == ACTION_STD_OK)
527 int type;
528 #ifdef HAS_BUTTONBAR
529 if (!hide_bars)
531 gui_buttonbar_unset(&buttonbar);
532 gui_buttonbar_draw(&buttonbar);
534 #endif
535 selected = get_menu_selection(gui_synclist_get_sel_pos(&lists), menu);
536 temp = menu->submenus[selected];
537 redraw_lists = true;
538 if (in_stringlist)
539 type = (menu->flags&MENU_TYPE_MASK);
540 else
542 type = (temp->flags&MENU_TYPE_MASK);
543 get_menu_callback(temp, &menu_callback);
544 if (menu_callback)
546 action = menu_callback(ACTION_ENTER_MENUITEM,temp);
547 if (action == ACTION_EXIT_MENUITEM)
548 break;
551 switch (type)
553 case MT_MENU:
554 if (stack_top < MAX_MENUS)
556 menu_stack[stack_top] = menu;
557 menu_stack_selected_item[stack_top] = selected;
558 stack_top++;
559 init_menu_lists(temp, &lists, 0, true, vps);
560 redraw_lists = false; /* above does the redraw */
561 menu = temp;
563 break;
564 case MT_FUNCTION_CALL:
566 int return_value;
567 if (temp->flags&MENU_FUNC_USEPARAM)
568 return_value = temp->function->function_w_param(
569 temp->function->param);
570 else
571 return_value = temp->function->function();
573 init_default_menu_viewports(menu_vp, hide_bars);
574 init_menu_lists(menu, &lists, selected, true, vps);
576 if (temp->flags&MENU_FUNC_CHECK_RETVAL)
578 if (return_value == 1)
580 done = true;
581 ret = return_value;
584 break;
586 case MT_SETTING:
587 case MT_SETTING_W_TEXT:
589 if (do_setting_from_menu(temp, menu_vp))
591 init_default_menu_viewports(menu_vp, hide_bars);
592 init_menu_lists(menu, &lists, selected, true,vps);
593 redraw_lists = false; /* above does the redraw */
595 break;
597 case MT_RETURN_ID:
598 if (in_stringlist)
600 done = true;
601 ret = selected;
603 else if (stack_top < MAX_MENUS)
605 menu_stack[stack_top] = menu;
606 menu_stack_selected_item[stack_top] = selected;
607 stack_top++;
608 menu = temp;
609 init_menu_lists(menu,&lists,0,false, vps);
610 redraw_lists = false; /* above does the redraw */
611 in_stringlist = true;
613 break;
614 case MT_RETURN_VALUE:
615 ret = temp->value;
616 done = true;
617 break;
619 if (type != MT_MENU)
621 if (menu_callback)
622 menu_callback(ACTION_EXIT_MENUITEM,temp);
624 if (current_submenus_menu != menu)
625 init_menu_lists(menu,&lists,selected,true,vps);
626 /* callback was changed, so reload the menu's callback */
627 get_menu_callback(menu, &menu_callback);
628 if ((menu->flags&MENU_EXITAFTERTHISMENU) &&
629 !(temp->flags&MENU_EXITAFTERTHISMENU))
631 done = true;
632 break;
634 #ifdef HAS_BUTTONBAR
635 if (!hide_bars)
637 gui_buttonbar_set(&buttonbar, "<<<", "", "");
638 gui_buttonbar_draw(&buttonbar);
640 #endif
642 else if(default_event_handler(action) == SYS_USB_CONNECTED)
644 ret = MENU_ATTACHED_USB;
645 done = true;
648 if (redraw_lists && !done)
650 gui_synclist_draw(&lists);
651 gui_synclist_speak_item(&lists);
654 if (start_selected)
656 /* make sure the start_selected variable is set to
657 the selected item from the menu do_menu() was called from */
658 if (stack_top > 0)
660 menu = menu_stack[0];
661 init_menu_lists(menu,&lists,menu_stack_selected_item[0],true, vps);
663 *start_selected = get_menu_selection(
664 gui_synclist_get_sel_pos(&lists), menu);
666 return ret;