Fix some quotation marks. Thanks to Alexander Levin for pointing it out.
[Rockbox.git] / apps / menu.c
blobde17f2a80908b4617840a35444071378d327789b
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,void * data, char *buffer)
98 const struct menu_item_ex *menu = (const struct menu_item_ex *)data;
99 int type = (menu->flags&MENU_TYPE_MASK);
100 selected_item = get_menu_selection(selected_item, menu);
102 (void)buffer;
103 /* only MT_MENU or MT_RETURN_ID is allowed in here */
104 if (type == MT_RETURN_ID)
106 if (menu->flags&MENU_DYNAMIC_DESC)
107 return menu->menu_get_name_and_icon->list_get_name(selected_item,
108 menu->menu_get_name_and_icon->list_get_name_data, buffer);
109 return (char*)menu->strings[selected_item];
112 menu = menu->submenus[selected_item];
114 if ((menu->flags&MENU_DYNAMIC_DESC) && (type != MT_SETTING_W_TEXT))
115 return menu->menu_get_name_and_icon->list_get_name(selected_item,
116 menu->menu_get_name_and_icon->list_get_name_data, buffer);
118 type = (menu->flags&MENU_TYPE_MASK);
119 if ((type == MT_SETTING) || (type == MT_SETTING_W_TEXT))
121 const struct settings_list *v
122 = find_setting(menu->variable, NULL);
123 if (v)
124 return str(v->lang_id);
125 else return "Not Done yet!";
127 return P2STR(menu->callback_and_desc->desc);
129 #ifdef HAVE_LCD_BITMAP
130 static int menu_get_icon(int selected_item, void * data)
132 const struct menu_item_ex *menu = (const struct menu_item_ex *)data;
133 int menu_icon = Icon_NOICON;
134 selected_item = get_menu_selection(selected_item, menu);
136 if ((menu->flags&MENU_TYPE_MASK) == MT_RETURN_ID)
138 return Icon_Menu_functioncall;
140 menu = menu->submenus[selected_item];
141 if (menu->flags&MENU_HAS_DESC)
142 menu_icon = menu->callback_and_desc->icon_id;
143 else if (menu->flags&MENU_DYNAMIC_DESC)
144 menu_icon = menu->menu_get_name_and_icon->icon_id;
146 if (menu_icon == Icon_NOICON)
148 switch (menu->flags&MENU_TYPE_MASK)
150 case MT_SETTING:
151 case MT_SETTING_W_TEXT:
152 menu_icon = Icon_Menu_setting;
153 break;
154 case MT_MENU:
155 menu_icon = Icon_Submenu;
156 break;
157 case MT_FUNCTION_CALL:
158 case MT_RETURN_VALUE:
159 menu_icon = Icon_Menu_functioncall;
160 break;
163 return menu_icon;
165 #endif
167 static void init_menu_lists(const struct menu_item_ex *menu,
168 struct gui_synclist *lists, int selected, bool callback,
169 struct viewport parent[NB_SCREENS])
171 int i, count = MENU_GET_COUNT(menu->flags);
172 int type = (menu->flags&MENU_TYPE_MASK);
173 menu_callback_type menu_callback = NULL;
174 int icon;
175 current_subitems_count = 0;
177 if (type == MT_RETURN_ID)
178 get_menu_callback(menu, &menu_callback);
180 for (i=0; i<count; i++)
182 if (type != MT_RETURN_ID)
183 get_menu_callback(menu->submenus[i],&menu_callback);
184 if (menu_callback)
186 if (menu_callback(ACTION_REQUEST_MENUITEM,
187 type==MT_RETURN_ID ? (void*)(intptr_t)i: menu->submenus[i])
188 != ACTION_EXIT_MENUITEM)
190 current_subitems[current_subitems_count] = i;
191 current_subitems_count++;
194 else
196 current_subitems[current_subitems_count] = i;
197 current_subitems_count++;
200 current_submenus_menu = (struct menu_item_ex *)menu;
202 gui_synclist_init(lists,get_menu_item_name,(void*)menu,false,1, parent);
203 #ifdef HAVE_LCD_BITMAP
204 if (menu->callback_and_desc->icon_id == Icon_NOICON)
205 icon = Icon_Submenu_Entered;
206 else
207 icon = menu->callback_and_desc->icon_id;
208 gui_synclist_set_title(lists, P2STR(menu->callback_and_desc->desc), icon);
209 gui_synclist_set_icon_callback(lists, menu_get_icon);
210 #else
211 (void)icon;
212 gui_synclist_set_icon_callback(lists, NULL);
213 #endif
214 if(global_settings.talk_menu)
215 gui_synclist_set_voice_callback(lists, talk_menu_item);
216 gui_synclist_set_nb_items(lists,current_subitems_count);
217 gui_synclist_limit_scroll(lists,true);
218 gui_synclist_select_item(lists, find_menu_selection(selected));
220 get_menu_callback(menu,&menu_callback);
221 if (callback && menu_callback)
222 menu_callback(ACTION_ENTER_MENUITEM,menu);
223 gui_synclist_draw(lists);
224 gui_synclist_speak_item(lists);
227 static int talk_menu_item(int selected_item, void *data)
229 const struct menu_item_ex *menu = (const struct menu_item_ex *)data;
230 int id = -1;
231 int type;
232 unsigned char *str;
233 int sel = get_menu_selection(selected_item, menu);
235 if ((menu->flags&MENU_TYPE_MASK) == MT_MENU)
237 type = menu->submenus[sel]->flags&MENU_TYPE_MASK;
238 if ((type == MT_SETTING) || (type == MT_SETTING_W_TEXT))
239 talk_setting(menu->submenus[sel]->variable);
240 else
242 if (menu->submenus[sel]->flags&(MENU_DYNAMIC_DESC))
244 int (*list_speak_item)(int selected_item, void * data)
245 = menu->submenus[sel]->menu_get_name_and_icon->
246 list_speak_item;
247 if(list_speak_item)
248 list_speak_item(sel, menu->submenus[sel]->
249 menu_get_name_and_icon->
250 list_get_name_data);
251 else
253 char buffer[80];
254 str = menu->submenus[sel]->menu_get_name_and_icon->
255 list_get_name(sel, menu->submenus[sel]->
256 menu_get_name_and_icon->
257 list_get_name_data, buffer);
258 id = P2ID(str);
261 else
262 id = P2ID(menu->submenus[sel]->callback_and_desc->desc);
263 if (id != -1)
264 talk_id(id,false);
267 else if(((menu->flags&MENU_TYPE_MASK) == MT_RETURN_ID))
269 if ((menu->flags&MENU_DYNAMIC_DESC) == 0)
271 unsigned char *s = (unsigned char *)menu->strings[sel];
272 /* string list, try to talk it if ID2P was used */
273 id = P2ID(s);
274 if (id != -1)
275 talk_id(id,false);
278 return 0;
280 /* this is used to reload the default menu viewports when the
281 theme changes. nothing happens if the menu is using a supplied parent vp */
282 void init_default_menu_viewports(struct viewport parent[NB_SCREENS], bool hide_bars)
284 int i;
285 FOR_NB_SCREENS(i)
287 viewport_set_defaults(&parent[i], i);
288 /* viewport_set_defaults() fixes the vp for the bars, so resize */
289 if (hide_bars)
291 if (global_settings.statusbar)
293 parent[i].y -= STATUSBAR_HEIGHT;
294 parent[i].height += STATUSBAR_HEIGHT;
298 #ifdef HAS_BUTTONBAR
299 if (!hide_bars && global_settings.buttonbar)
300 parent[0].height -= BUTTONBAR_HEIGHT;
301 #endif
304 bool do_setting_from_menu(const struct menu_item_ex *temp)
306 int setting_id, oldval;
307 const struct settings_list *setting = find_setting(
308 temp->variable,
309 &setting_id);
310 char *title;
311 char padded_title[MAX_PATH];
312 int var_type = setting->flags&F_T_MASK;
313 if (var_type == F_T_INT || var_type == F_T_UINT)
315 oldval = *(int*)setting->setting;
317 else if (var_type == F_T_BOOL)
319 oldval = *(bool*)setting->setting;
321 else
322 oldval = 0;
323 if ((temp->flags&MENU_TYPE_MASK) == MT_SETTING_W_TEXT)
324 title = temp->callback_and_desc->desc;
325 else
326 title = ID2P(setting->lang_id);
328 /* Pad the title string by repeating it. This is needed
329 so the scroll settings title can actually be used to
330 test the setting */
331 if (setting->flags&F_PADTITLE)
333 int i = 0, len;
334 if (setting->lang_id == -1)
335 title = (char*)setting->cfg_vals;
336 else
337 title = P2STR((unsigned char*)title);
338 len = strlen(title);
339 while (i < MAX_PATH-1)
341 int padlen = MIN(len, MAX_PATH-1-i);
342 strncpy(&padded_title[i], title, padlen);
343 i += padlen;
344 if (i<MAX_PATH-1)
345 padded_title[i++] = ' ';
347 padded_title[i] = '\0';
348 title = padded_title;
351 option_screen((struct settings_list *)setting,
352 setting->flags&F_TEMPVAR, title);
353 if (var_type == F_T_INT || var_type == F_T_UINT)
355 return oldval != *(int*)setting->setting;
357 else if (var_type == F_T_BOOL)
359 return oldval != *(bool*)setting->setting;
361 return false;
364 /* display a menu */
365 int do_menu(const struct menu_item_ex *start_menu, int *start_selected,
366 struct viewport parent[NB_SCREENS], bool hide_bars)
368 int selected = start_selected? *start_selected : 0;
369 int action;
370 struct gui_synclist lists;
371 const struct menu_item_ex *temp, *menu;
372 int ret = 0, i;
373 bool redraw_lists;
375 const struct menu_item_ex *menu_stack[MAX_MENUS];
376 int menu_stack_selected_item[MAX_MENUS];
377 int stack_top = 0;
378 bool in_stringlist, done = false;
380 struct viewport *vps, menu_vp[NB_SCREENS]; /* menu_vp will hopefully be phased out */
381 #ifdef HAS_BUTTONBAR
382 struct gui_buttonbar buttonbar;
383 gui_buttonbar_init(&buttonbar);
384 gui_buttonbar_set_display(&buttonbar, &(screens[SCREEN_MAIN]) );
385 gui_buttonbar_set(&buttonbar, "<<<", "", "");
386 #endif
388 menu_callback_type menu_callback = NULL;
389 if (start_menu == NULL)
390 menu = &main_menu_;
391 else menu = start_menu;
393 if (parent)
395 vps = parent;
396 /* if hide_bars == true we assume the viewport is correctly sized */
398 else
400 vps = menu_vp;
401 init_default_menu_viewports(vps, hide_bars);
403 FOR_NB_SCREENS(i)
405 screens[i].set_viewport(&vps[i]);
406 screens[i].clear_viewport();
407 screens[i].set_viewport(NULL);
409 init_menu_lists(menu, &lists, selected, true, vps);
410 in_stringlist = ((menu->flags&MENU_TYPE_MASK) == MT_RETURN_ID);
412 /* load the callback, and only reload it if menu changes */
413 get_menu_callback(menu, &menu_callback);
416 #ifdef HAS_BUTTONBAR
417 if (!hide_bars)
419 gui_buttonbar_set(&buttonbar, "<<<", "", "");
420 gui_buttonbar_draw(&buttonbar);
422 #endif
423 while (!done)
425 redraw_lists = false;
426 if (!hide_bars)
428 gui_syncstatusbar_draw(&statusbars, true);
430 action = get_action(CONTEXT_MAINMENU,
431 list_do_action_timeout(&lists, HZ));
432 /* HZ so the status bar redraws corectly */
434 if (action != ACTION_NONE && menu_callback)
436 int old_action = action;
437 action = menu_callback(action, menu);
438 if (action == ACTION_EXIT_AFTER_THIS_MENUITEM)
440 action = old_action;
441 ret = MENU_SELECTED_EXIT; /* will exit after returning
442 from selection */
444 else if (action == ACTION_REDRAW)
446 action = old_action;
447 redraw_lists = true;
451 if (gui_synclist_do_button(&lists, &action, LIST_WRAP_UNLESS_HELD))
452 continue;
453 if (action == ACTION_NONE)
454 continue;
456 #ifdef HAVE_RECORDING
457 if (action == ACTION_STD_REC)
459 ret = GO_TO_RECSCREEN;
460 done = true;
462 else
463 #endif
464 if (action == ACTION_TREE_WPS)
466 ret = GO_TO_PREVIOUS_MUSIC;
467 done = true;
469 else if (action == ACTION_TREE_STOP)
471 redraw_lists = list_stop_handler();
473 else if (action == ACTION_STD_CONTEXT &&
474 menu == &root_menu_)
476 ret = GO_TO_ROOTITEM_CONTEXT;
477 done = true;
479 else if (action == ACTION_STD_MENU)
481 if (menu != &root_menu_)
482 ret = GO_TO_ROOT;
483 else
484 ret = GO_TO_PREVIOUS;
485 done = true;
487 else if (action == ACTION_STD_CANCEL)
489 bool exiting_menu = false;
490 in_stringlist = false;
491 if (menu_callback)
492 menu_callback(ACTION_EXIT_MENUITEM, menu);
494 if (menu->flags&MENU_EXITAFTERTHISMENU)
495 done = true;
496 else if ((menu->flags&MENU_TYPE_MASK) == MT_MENU)
497 exiting_menu = true;
498 if (stack_top > 0)
500 stack_top--;
501 menu = menu_stack[stack_top];
502 if (!exiting_menu && (menu->flags&MENU_EXITAFTERTHISMENU))
503 done = true;
504 else
505 init_menu_lists(menu, &lists,
506 menu_stack_selected_item[stack_top], false, vps);
507 /* new menu, so reload the callback */
508 get_menu_callback(menu, &menu_callback);
510 else if (menu != &root_menu_)
512 ret = GO_TO_PREVIOUS;
513 done = true;
516 else if (action == ACTION_STD_OK)
518 int type;
519 #ifdef HAS_BUTTONBAR
520 if (!hide_bars)
522 gui_buttonbar_unset(&buttonbar);
523 gui_buttonbar_draw(&buttonbar);
525 #endif
526 selected = get_menu_selection(gui_synclist_get_sel_pos(&lists), menu);
527 temp = menu->submenus[selected];
528 redraw_lists = true;
529 if (in_stringlist)
530 type = (menu->flags&MENU_TYPE_MASK);
531 else
533 type = (temp->flags&MENU_TYPE_MASK);
534 get_menu_callback(temp, &menu_callback);
535 if (menu_callback)
537 action = menu_callback(ACTION_ENTER_MENUITEM,temp);
538 if (action == ACTION_EXIT_MENUITEM)
539 break;
542 switch (type)
544 case MT_MENU:
545 if (stack_top < MAX_MENUS)
547 menu_stack[stack_top] = menu;
548 menu_stack_selected_item[stack_top] = selected;
549 stack_top++;
550 init_menu_lists(temp, &lists, 0, true, vps);
551 redraw_lists = false; /* above does the redraw */
552 menu = temp;
554 break;
555 case MT_FUNCTION_CALL:
557 int return_value;
558 if (temp->flags&MENU_FUNC_USEPARAM)
559 return_value = temp->function->function_w_param(
560 temp->function->param);
561 else
562 return_value = temp->function->function();
564 init_default_menu_viewports(menu_vp, hide_bars);
565 init_menu_lists(menu, &lists, selected, true, vps);
567 if (temp->flags&MENU_FUNC_CHECK_RETVAL)
569 if (return_value == 1)
571 done = true;
572 ret = return_value;
575 break;
577 case MT_SETTING:
578 case MT_SETTING_W_TEXT:
580 if (do_setting_from_menu(temp))
582 init_default_menu_viewports(menu_vp, hide_bars);
583 init_menu_lists(menu, &lists, selected, true,vps);
584 redraw_lists = false; /* above does the redraw */
586 break;
588 case MT_RETURN_ID:
589 if (in_stringlist)
591 done = true;
592 ret = selected;
594 else if (stack_top < MAX_MENUS)
596 menu_stack[stack_top] = menu;
597 menu_stack_selected_item[stack_top] = selected;
598 stack_top++;
599 menu = temp;
600 init_menu_lists(menu,&lists,0,false, vps);
601 redraw_lists = false; /* above does the redraw */
602 in_stringlist = true;
604 break;
605 case MT_RETURN_VALUE:
606 ret = temp->value;
607 done = true;
608 break;
610 if (type != MT_MENU)
612 if (menu_callback)
613 menu_callback(ACTION_EXIT_MENUITEM,temp);
615 if (current_submenus_menu != menu)
616 init_menu_lists(menu,&lists,selected,true,vps);
617 /* callback was changed, so reload the menu's callback */
618 get_menu_callback(menu, &menu_callback);
619 if ((menu->flags&MENU_EXITAFTERTHISMENU) &&
620 !(temp->flags&MENU_EXITAFTERTHISMENU))
622 done = true;
623 break;
625 #ifdef HAS_BUTTONBAR
626 if (!hide_bars)
628 gui_buttonbar_set(&buttonbar, "<<<", "", "");
629 gui_buttonbar_draw(&buttonbar);
631 #endif
633 else if(default_event_handler(action) == SYS_USB_CONNECTED)
635 ret = MENU_ATTACHED_USB;
636 done = true;
639 if (redraw_lists && !done)
641 gui_synclist_draw(&lists);
642 gui_synclist_speak_item(&lists);
645 if (start_selected)
647 /* make sure the start_selected variable is set to
648 the selected item from the menu do_menu() was called from */
649 if (stack_top > 0)
651 menu = menu_stack[0];
652 init_menu_lists(menu,&lists,menu_stack_selected_item[0],true, vps);
654 *start_selected = get_menu_selection(
655 gui_synclist_get_sel_pos(&lists), menu);
657 return ret;