don't save mountpoint using native separators in the configuration. Makes things...
[Rockbox.git] / apps / menu.c
blobbbe3d697dd082e722a9d6ad649d774e9892a7775
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"
54 #include "quickscreen.h"
56 #ifdef HAVE_LCD_BITMAP
57 #include "icons.h"
58 #endif
60 /* gui api */
61 #include "list.h"
62 #include "statusbar.h"
63 #include "buttonbar.h"
65 #define MAX_MENUS 8
66 /* used to allow for dynamic menus */
67 #define MAX_MENU_SUBITEMS 64
68 static struct menu_item_ex *current_submenus_menu;
69 static int current_subitems[MAX_MENU_SUBITEMS];
70 static int current_subitems_count = 0;
71 static int talk_menu_item(int selected_item, void *data);
73 static void get_menu_callback(const struct menu_item_ex *m,
74 menu_callback_type *menu_callback)
76 if (m->flags&(MENU_HAS_DESC|MENU_DYNAMIC_DESC))
77 *menu_callback= m->callback_and_desc->menu_callback;
78 else
79 *menu_callback = m->menu_callback;
82 static int get_menu_selection(int selected_item, const struct menu_item_ex *menu)
84 int type = (menu->flags&MENU_TYPE_MASK);
85 if (type == MT_MENU && (selected_item<current_subitems_count))
86 return current_subitems[selected_item];
87 return selected_item;
89 static int find_menu_selection(int selected)
91 int i;
92 for (i=0; i< current_subitems_count; i++)
93 if (current_subitems[i] == selected)
94 return i;
95 return 0;
97 static char * get_menu_item_name(int selected_item,
98 void * data,
99 char *buffer,
100 size_t buffer_len)
102 const struct menu_item_ex *menu = (const struct menu_item_ex *)data;
103 int type = (menu->flags&MENU_TYPE_MASK);
104 selected_item = get_menu_selection(selected_item, menu);
106 (void)buffer_len;
107 /* only MT_MENU or MT_RETURN_ID is allowed in here */
108 if (type == MT_RETURN_ID)
110 if (menu->flags&MENU_DYNAMIC_DESC)
111 return menu->menu_get_name_and_icon->list_get_name(selected_item,
112 menu->menu_get_name_and_icon->list_get_name_data, buffer);
113 return (char*)menu->strings[selected_item];
116 menu = menu->submenus[selected_item];
118 if ((menu->flags&MENU_DYNAMIC_DESC) && (type != MT_SETTING_W_TEXT))
119 return menu->menu_get_name_and_icon->list_get_name(selected_item,
120 menu->menu_get_name_and_icon->list_get_name_data, buffer);
122 type = (menu->flags&MENU_TYPE_MASK);
123 if ((type == MT_SETTING) || (type == MT_SETTING_W_TEXT))
125 const struct settings_list *v
126 = find_setting(menu->variable, NULL);
127 if (v)
128 return str(v->lang_id);
129 else return "Not Done yet!";
131 return P2STR(menu->callback_and_desc->desc);
133 #ifdef HAVE_LCD_BITMAP
134 static int menu_get_icon(int selected_item, void * data)
136 const struct menu_item_ex *menu = (const struct menu_item_ex *)data;
137 int menu_icon = Icon_NOICON;
138 selected_item = get_menu_selection(selected_item, menu);
140 if ((menu->flags&MENU_TYPE_MASK) == MT_RETURN_ID)
142 return Icon_Menu_functioncall;
144 menu = menu->submenus[selected_item];
145 if (menu->flags&MENU_HAS_DESC)
146 menu_icon = menu->callback_and_desc->icon_id;
147 else if (menu->flags&MENU_DYNAMIC_DESC)
148 menu_icon = menu->menu_get_name_and_icon->icon_id;
150 if (menu_icon == Icon_NOICON)
152 switch (menu->flags&MENU_TYPE_MASK)
154 case MT_SETTING:
155 case MT_SETTING_W_TEXT:
156 menu_icon = Icon_Menu_setting;
157 break;
158 case MT_MENU:
159 menu_icon = Icon_Submenu;
160 break;
161 case MT_FUNCTION_CALL:
162 case MT_RETURN_VALUE:
163 menu_icon = Icon_Menu_functioncall;
164 break;
167 return menu_icon;
169 #endif
171 static void init_menu_lists(const struct menu_item_ex *menu,
172 struct gui_synclist *lists, int selected, bool callback,
173 struct viewport parent[NB_SCREENS])
175 int i, count = MENU_GET_COUNT(menu->flags);
176 int type = (menu->flags&MENU_TYPE_MASK);
177 menu_callback_type menu_callback = NULL;
178 int icon;
179 current_subitems_count = 0;
181 if (type == MT_RETURN_ID)
182 get_menu_callback(menu, &menu_callback);
184 for (i=0; i<count; i++)
186 if (type != MT_RETURN_ID)
187 get_menu_callback(menu->submenus[i],&menu_callback);
188 if (menu_callback)
190 if (menu_callback(ACTION_REQUEST_MENUITEM,
191 type==MT_RETURN_ID ? (void*)(intptr_t)i: menu->submenus[i])
192 != ACTION_EXIT_MENUITEM)
194 current_subitems[current_subitems_count] = i;
195 current_subitems_count++;
198 else
200 current_subitems[current_subitems_count] = i;
201 current_subitems_count++;
204 current_submenus_menu = (struct menu_item_ex *)menu;
206 gui_synclist_init(lists,get_menu_item_name,(void*)menu,false,1, parent);
207 #ifdef HAVE_LCD_BITMAP
208 if (menu->callback_and_desc->icon_id == Icon_NOICON)
209 icon = Icon_Submenu_Entered;
210 else
211 icon = menu->callback_and_desc->icon_id;
212 gui_synclist_set_title(lists, P2STR(menu->callback_and_desc->desc), icon);
213 gui_synclist_set_icon_callback(lists, menu_get_icon);
214 #else
215 (void)icon;
216 gui_synclist_set_icon_callback(lists, NULL);
217 #endif
218 if(global_settings.talk_menu)
219 gui_synclist_set_voice_callback(lists, talk_menu_item);
220 gui_synclist_set_nb_items(lists,current_subitems_count);
221 gui_synclist_limit_scroll(lists,true);
222 gui_synclist_select_item(lists, find_menu_selection(selected));
224 get_menu_callback(menu,&menu_callback);
225 if (callback && menu_callback)
226 menu_callback(ACTION_ENTER_MENUITEM,menu);
227 gui_synclist_draw(lists);
228 gui_synclist_speak_item(lists);
231 static int talk_menu_item(int selected_item, void *data)
233 const struct menu_item_ex *menu = (const struct menu_item_ex *)data;
234 int id = -1;
235 int type;
236 unsigned char *str;
237 int sel = get_menu_selection(selected_item, menu);
239 if ((menu->flags&MENU_TYPE_MASK) == MT_MENU)
241 type = menu->submenus[sel]->flags&MENU_TYPE_MASK;
242 if ((type == MT_SETTING) || (type == MT_SETTING_W_TEXT))
243 talk_setting(menu->submenus[sel]->variable);
244 else
246 if (menu->submenus[sel]->flags&(MENU_DYNAMIC_DESC))
248 int (*list_speak_item)(int selected_item, void * data)
249 = menu->submenus[sel]->menu_get_name_and_icon->
250 list_speak_item;
251 if(list_speak_item)
252 list_speak_item(sel, menu->submenus[sel]->
253 menu_get_name_and_icon->
254 list_get_name_data);
255 else
257 char buffer[80];
258 str = menu->submenus[sel]->menu_get_name_and_icon->
259 list_get_name(sel, menu->submenus[sel]->
260 menu_get_name_and_icon->
261 list_get_name_data, buffer);
262 id = P2ID(str);
265 else
266 id = P2ID(menu->submenus[sel]->callback_and_desc->desc);
267 if (id != -1)
268 talk_id(id,false);
271 else if(((menu->flags&MENU_TYPE_MASK) == MT_RETURN_ID))
273 if ((menu->flags&MENU_DYNAMIC_DESC) == 0)
275 unsigned char *s = (unsigned char *)menu->strings[sel];
276 /* string list, try to talk it if ID2P was used */
277 id = P2ID(s);
278 if (id != -1)
279 talk_id(id,false);
282 return 0;
284 /* this is used to reload the default menu viewports when the
285 theme changes. nothing happens if the menu is using a supplied parent vp */
286 static void init_default_menu_viewports(struct viewport parent[NB_SCREENS], bool hide_bars)
288 int i;
289 FOR_NB_SCREENS(i)
291 viewport_set_defaults(&parent[i], i);
292 /* viewport_set_defaults() fixes the vp for the bars, so resize */
293 if (hide_bars)
295 if (global_settings.statusbar)
297 parent[i].y -= STATUSBAR_HEIGHT;
298 parent[i].height += STATUSBAR_HEIGHT;
302 #ifdef HAS_BUTTONBAR
303 if (!hide_bars && global_settings.buttonbar)
304 parent[0].height -= BUTTONBAR_HEIGHT;
305 #endif
308 bool do_setting_from_menu(const struct menu_item_ex *temp,
309 struct viewport parent[NB_SCREENS])
311 int setting_id, oldval;
312 const struct settings_list *setting = find_setting(
313 temp->variable,
314 &setting_id);
315 char *title;
316 char padded_title[MAX_PATH];
317 int var_type = setting->flags&F_T_MASK;
318 if (var_type == F_T_INT || var_type == F_T_UINT)
320 oldval = *(int*)setting->setting;
322 else if (var_type == F_T_BOOL)
324 oldval = *(bool*)setting->setting;
326 else
327 oldval = 0;
328 if ((temp->flags&MENU_TYPE_MASK) == MT_SETTING_W_TEXT)
329 title = temp->callback_and_desc->desc;
330 else
331 title = ID2P(setting->lang_id);
333 /* Pad the title string by repeating it. This is needed
334 so the scroll settings title can actually be used to
335 test the setting */
336 if (setting->flags&F_PADTITLE)
338 int i = 0, len;
339 if (setting->lang_id == -1)
340 title = (char*)setting->cfg_vals;
341 else
342 title = P2STR((unsigned char*)title);
343 len = strlen(title);
344 while (i < MAX_PATH-1)
346 int padlen = MIN(len, MAX_PATH-1-i);
347 strncpy(&padded_title[i], title, padlen);
348 i += padlen;
349 if (i<MAX_PATH-1)
350 padded_title[i++] = ' ';
352 padded_title[i] = '\0';
353 title = padded_title;
356 option_screen((struct settings_list *)setting, parent,
357 setting->flags&F_TEMPVAR, title);
358 if (var_type == F_T_INT || var_type == F_T_UINT)
360 return oldval != *(int*)setting->setting;
362 else if (var_type == F_T_BOOL)
364 return oldval != *(bool*)setting->setting;
366 return false;
369 /* display a menu */
370 int do_menu(const struct menu_item_ex *start_menu, int *start_selected,
371 struct viewport parent[NB_SCREENS], bool hide_bars)
373 int selected = start_selected? *start_selected : 0;
374 int action;
375 struct gui_synclist lists;
376 const struct menu_item_ex *temp, *menu;
377 int ret = 0, i;
378 bool redraw_lists;
380 const struct menu_item_ex *menu_stack[MAX_MENUS];
381 int menu_stack_selected_item[MAX_MENUS];
382 int stack_top = 0;
383 bool in_stringlist, done = false;
385 struct viewport *vps, menu_vp[NB_SCREENS]; /* menu_vp will hopefully be phased out */
386 #ifdef HAS_BUTTONBAR
387 struct gui_buttonbar buttonbar;
388 gui_buttonbar_init(&buttonbar);
389 gui_buttonbar_set_display(&buttonbar, &(screens[SCREEN_MAIN]) );
390 gui_buttonbar_set(&buttonbar, "<<<", "", "");
391 #endif
393 menu_callback_type menu_callback = NULL;
394 if (start_menu == NULL)
395 menu = &main_menu_;
396 else menu = start_menu;
398 if (parent)
400 vps = parent;
401 /* if hide_bars == true we assume the viewport is correctly sized */
403 else
405 vps = menu_vp;
406 init_default_menu_viewports(vps, hide_bars);
408 FOR_NB_SCREENS(i)
410 screens[i].set_viewport(&vps[i]);
411 screens[i].clear_viewport();
412 screens[i].set_viewport(NULL);
414 init_menu_lists(menu, &lists, selected, true, vps);
415 in_stringlist = ((menu->flags&MENU_TYPE_MASK) == MT_RETURN_ID);
417 /* load the callback, and only reload it if menu changes */
418 get_menu_callback(menu, &menu_callback);
421 #ifdef HAS_BUTTONBAR
422 if (!hide_bars)
424 gui_buttonbar_set(&buttonbar, "<<<", "", "");
425 gui_buttonbar_draw(&buttonbar);
427 #endif
428 while (!done)
430 redraw_lists = false;
431 if (!hide_bars)
433 gui_syncstatusbar_draw(&statusbars, true);
435 action = get_action(CONTEXT_MAINMENU,
436 list_do_action_timeout(&lists, HZ));
437 /* HZ so the status bar redraws corectly */
439 if (action != ACTION_NONE && menu_callback)
441 int old_action = action;
442 action = menu_callback(action, menu);
443 if (action == ACTION_EXIT_AFTER_THIS_MENUITEM)
445 action = old_action;
446 ret = MENU_SELECTED_EXIT; /* will exit after returning
447 from selection */
449 else if (action == ACTION_REDRAW)
451 action = old_action;
452 redraw_lists = true;
456 if (gui_synclist_do_button(&lists, &action, LIST_WRAP_UNLESS_HELD))
457 continue;
458 if (action == ACTION_NONE)
459 continue;
460 #ifdef HAVE_QUICKSCREEN
461 else if (action == ACTION_STD_QUICKSCREEN)
463 quick_screen_quick(action);
464 redraw_lists = true;
466 #endif
467 #ifdef HAVE_RECORDING
468 else if (action == ACTION_STD_REC)
470 ret = GO_TO_RECSCREEN;
471 done = true;
473 #endif
474 else if (action == ACTION_TREE_WPS)
476 ret = GO_TO_PREVIOUS_MUSIC;
477 done = true;
479 else if (action == ACTION_TREE_STOP)
481 redraw_lists = list_stop_handler();
483 else if (action == ACTION_STD_CONTEXT &&
484 menu == &root_menu_)
486 ret = GO_TO_ROOTITEM_CONTEXT;
487 done = true;
489 else if (action == ACTION_STD_MENU)
491 if (menu != &root_menu_)
492 ret = GO_TO_ROOT;
493 else
494 ret = GO_TO_PREVIOUS;
495 done = true;
497 else if (action == ACTION_STD_CANCEL)
499 bool exiting_menu = false;
500 in_stringlist = false;
501 if (menu_callback)
502 menu_callback(ACTION_EXIT_MENUITEM, menu);
504 if (menu->flags&MENU_EXITAFTERTHISMENU)
505 done = true;
506 else if ((menu->flags&MENU_TYPE_MASK) == MT_MENU)
507 exiting_menu = true;
508 if (stack_top > 0)
510 stack_top--;
511 menu = menu_stack[stack_top];
512 if (!exiting_menu && (menu->flags&MENU_EXITAFTERTHISMENU))
513 done = true;
514 else
515 init_menu_lists(menu, &lists,
516 menu_stack_selected_item[stack_top], false, vps);
517 /* new menu, so reload the callback */
518 get_menu_callback(menu, &menu_callback);
520 else if (menu != &root_menu_)
522 ret = GO_TO_PREVIOUS;
523 done = true;
526 else if (action == ACTION_STD_OK)
528 int type;
529 #ifdef HAS_BUTTONBAR
530 if (!hide_bars)
532 gui_buttonbar_unset(&buttonbar);
533 gui_buttonbar_draw(&buttonbar);
535 #endif
536 selected = get_menu_selection(gui_synclist_get_sel_pos(&lists), menu);
537 temp = menu->submenus[selected];
538 redraw_lists = true;
539 if (in_stringlist)
540 type = (menu->flags&MENU_TYPE_MASK);
541 else
543 type = (temp->flags&MENU_TYPE_MASK);
544 get_menu_callback(temp, &menu_callback);
545 if (menu_callback)
547 action = menu_callback(ACTION_ENTER_MENUITEM,temp);
548 if (action == ACTION_EXIT_MENUITEM)
549 break;
552 switch (type)
554 case MT_MENU:
555 if (stack_top < MAX_MENUS)
557 menu_stack[stack_top] = menu;
558 menu_stack_selected_item[stack_top] = selected;
559 stack_top++;
560 init_menu_lists(temp, &lists, 0, true, vps);
561 redraw_lists = false; /* above does the redraw */
562 menu = temp;
564 break;
565 case MT_FUNCTION_CALL:
567 int return_value;
568 if (temp->flags&MENU_FUNC_USEPARAM)
569 return_value = temp->function->function_w_param(
570 temp->function->param);
571 else
572 return_value = temp->function->function();
573 if (!(menu->flags&MENU_EXITAFTERTHISMENU) || (temp->flags&MENU_EXITAFTERTHISMENU))
575 init_default_menu_viewports(menu_vp, hide_bars);
576 init_menu_lists(menu, &lists, selected, true, vps);
578 if (temp->flags&MENU_FUNC_CHECK_RETVAL)
580 if (return_value == 1)
582 done = true;
583 ret = return_value;
586 break;
588 case MT_SETTING:
589 case MT_SETTING_W_TEXT:
591 if (do_setting_from_menu(temp, menu_vp))
593 init_default_menu_viewports(menu_vp, hide_bars);
594 init_menu_lists(menu, &lists, selected, true,vps);
595 redraw_lists = false; /* above does the redraw */
597 break;
599 case MT_RETURN_ID:
600 if (in_stringlist)
602 done = true;
603 ret = selected;
605 else if (stack_top < MAX_MENUS)
607 menu_stack[stack_top] = menu;
608 menu_stack_selected_item[stack_top] = selected;
609 stack_top++;
610 menu = temp;
611 init_menu_lists(menu,&lists,0,false, vps);
612 redraw_lists = false; /* above does the redraw */
613 in_stringlist = true;
615 break;
616 case MT_RETURN_VALUE:
617 ret = temp->value;
618 done = true;
619 break;
621 if (type != MT_MENU)
623 if (menu_callback)
624 menu_callback(ACTION_EXIT_MENUITEM,temp);
626 if (current_submenus_menu != menu)
627 init_menu_lists(menu,&lists,selected,true,vps);
628 /* callback was changed, so reload the menu's callback */
629 get_menu_callback(menu, &menu_callback);
630 if ((menu->flags&MENU_EXITAFTERTHISMENU) &&
631 !(temp->flags&MENU_EXITAFTERTHISMENU))
633 done = true;
634 break;
636 #ifdef HAS_BUTTONBAR
637 if (!hide_bars)
639 gui_buttonbar_set(&buttonbar, "<<<", "", "");
640 gui_buttonbar_draw(&buttonbar);
642 #endif
644 else if(default_event_handler(action) == SYS_USB_CONNECTED)
646 ret = MENU_ATTACHED_USB;
647 done = true;
650 if (redraw_lists && !done)
652 gui_synclist_draw(&lists);
653 gui_synclist_speak_item(&lists);
656 if (start_selected)
658 /* make sure the start_selected variable is set to
659 the selected item from the menu do_menu() was called from */
660 if (stack_top > 0)
662 menu = menu_stack[0];
663 init_menu_lists(menu,&lists,menu_stack_selected_item[0],true, vps);
665 *start_selected = get_menu_selection(
666 gui_synclist_get_sel_pos(&lists), menu);
668 return ret;