Player: Save a bit of space by only using 7 bytes/char in the glyph table.
[Rockbox.git] / apps / menu.c
blob0ac0166971d27578fd573729f7b2b423e76820f1
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 "hwcompat.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 "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"
61 /* needed for the old menu system */
62 struct menu {
63 struct menu_item* items;
64 int count;
65 int (*callback)(int, int);
66 int current_selection;
68 #define MAX_MENUS 6
69 static struct menu menus[MAX_MENUS];
70 static bool inuse[MAX_MENUS] = { false };
71 static void init_oldmenu(const struct menu_item_ex *menu,
72 struct gui_synclist *lists, int selected, bool callback);
73 static void menu_talk_selected(int m);
75 /* used to allow for dynamic menus */
76 #define MAX_MENU_SUBITEMS 64
77 static struct menu_item_ex *current_submenus_menu;
78 static int current_subitems[MAX_MENU_SUBITEMS];
79 static int current_subitems_count = 0;
81 void get_menu_callback(const struct menu_item_ex *m,
82 menu_callback_type *menu_callback)
84 if (m->flags&(MENU_HAS_DESC|MENU_DYNAMIC_DESC))
85 *menu_callback= m->callback_and_desc->menu_callback;
86 else
87 *menu_callback = m->menu_callback;
90 static int get_menu_selection(int selected_item, const struct menu_item_ex *menu)
92 int type = (menu->flags&MENU_TYPE_MASK);
93 if (type == MT_MENU && (selected_item<current_subitems_count))
94 return current_subitems[selected_item];
95 return selected_item;
97 static int find_menu_selection(int selected)
99 int i;
100 for (i=0; i< current_subitems_count; i++)
101 if (current_subitems[i] == selected)
102 return i;
103 return 0;
105 static char * get_menu_item_name(int selected_item,void * data, char *buffer)
107 const struct menu_item_ex *menu = (const struct menu_item_ex *)data;
108 int type = (menu->flags&MENU_TYPE_MASK);
109 selected_item = get_menu_selection(selected_item, menu);
111 (void)buffer;
112 /* only MT_MENU or MT_RETURN_ID is allowed in here */
113 if (type == MT_RETURN_ID)
115 if (menu->flags&MENU_DYNAMIC_DESC)
116 return menu->menu_get_name_and_icon->list_get_name(selected_item,
117 menu->menu_get_name_and_icon->list_get_name_data, buffer);
118 return (char*)menu->strings[selected_item];
121 menu = menu->submenus[selected_item];
123 if ((menu->flags&MENU_DYNAMIC_DESC) && (type != MT_SETTING_W_TEXT))
124 return menu->menu_get_name_and_icon->list_get_name(selected_item,
125 menu->menu_get_name_and_icon->list_get_name_data, buffer);
127 type = (menu->flags&MENU_TYPE_MASK);
128 if ((type == MT_SETTING) || (type == MT_SETTING_W_TEXT))
130 const struct settings_list *v
131 = find_setting(menu->variable, NULL);
132 if (v)
133 return str(v->lang_id);
134 else return "Not Done yet!";
136 return P2STR(menu->callback_and_desc->desc);
138 #ifdef HAVE_LCD_BITMAP
139 static void menu_get_icon(int selected_item, void * data, ICON * icon)
141 const struct menu_item_ex *menu = (const struct menu_item_ex *)data;
142 int menu_icon = Icon_NOICON;
143 selected_item = get_menu_selection(selected_item, menu);
145 if ((menu->flags&MENU_TYPE_MASK) == MT_RETURN_ID)
147 *icon = bitmap_icons_6x8[Icon_Menu_functioncall];
148 return;
150 menu = menu->submenus[selected_item];
151 if (menu->flags&MENU_HAS_DESC)
152 menu_icon = menu->callback_and_desc->icon_id;
153 else if (menu->flags&MENU_DYNAMIC_DESC)
154 menu_icon = menu->menu_get_name_and_icon->icon_id;
156 switch (menu->flags&MENU_TYPE_MASK)
158 case MT_SETTING:
159 case MT_SETTING_W_TEXT:
160 *icon = bitmap_icons_6x8[Icon_Menu_setting];
161 break;
162 case MT_MENU:
163 if (menu_icon == Icon_NOICON)
164 *icon = bitmap_icons_6x8[Icon_Submenu];
165 else
166 *icon = bitmap_icons_6x8[menu_icon];
167 break;
168 case MT_FUNCTION_CALL:
169 case MT_RETURN_VALUE:
170 if (menu_icon == Icon_NOICON)
171 *icon = bitmap_icons_6x8[Icon_Menu_functioncall];
172 else
173 *icon = bitmap_icons_6x8[menu_icon];
174 break;
175 default:
176 *icon = NOICON;
179 #endif
181 static void init_menu_lists(const struct menu_item_ex *menu,
182 struct gui_synclist *lists, int selected, bool callback)
184 int i, count = MENU_GET_COUNT(menu->flags);
185 int type = (menu->flags&MENU_TYPE_MASK);
186 menu_callback_type menu_callback = NULL;
187 ICON icon = NOICON;
188 current_subitems_count = 0;
190 if (type == MT_OLD_MENU)
192 init_oldmenu(menu, lists, selected, callback);
193 return;
195 if (type == MT_RETURN_ID)
196 get_menu_callback(menu, &menu_callback);
198 for (i=0; i<count; i++)
200 if (type != MT_RETURN_ID)
201 get_menu_callback(menu->submenus[i],&menu_callback);
202 if (menu_callback)
204 if (menu_callback(ACTION_REQUEST_MENUITEM,
205 type==MT_RETURN_ID ? (void*)(intptr_t)i: menu->submenus[i])
206 != ACTION_EXIT_MENUITEM)
208 current_subitems[current_subitems_count] = i;
209 current_subitems_count++;
212 else
214 current_subitems[current_subitems_count] = i;
215 current_subitems_count++;
218 current_submenus_menu = (struct menu_item_ex *)menu;
220 gui_synclist_init(lists,get_menu_item_name,(void*)menu,false,1);
221 #ifdef HAVE_LCD_BITMAP
222 if (menu->callback_and_desc->icon_id == Icon_NOICON)
223 icon = bitmap_icons_6x8[Icon_Submenu_Entered];
224 else
225 icon = bitmap_icons_6x8[menu->callback_and_desc->icon_id];
226 gui_synclist_set_title(lists, P2STR(menu->callback_and_desc->desc), icon);
227 gui_synclist_set_icon_callback(lists, menu_get_icon);
228 #else
229 (void)icon;
230 gui_synclist_set_icon_callback(lists, NULL);
231 #endif
232 gui_synclist_set_nb_items(lists,current_subitems_count);
233 gui_synclist_limit_scroll(lists,true);
234 gui_synclist_select_item(lists, find_menu_selection(selected));
236 get_menu_callback(menu,&menu_callback);
237 if (callback && menu_callback)
238 menu_callback(ACTION_ENTER_MENUITEM,menu);
241 static void talk_menu_item(const struct menu_item_ex *menu,
242 struct gui_synclist *lists)
244 int id = -1;
245 int type;
246 unsigned char *str;
247 int sel;
249 if (global_settings.talk_menu)
251 if ((menu->flags&MENU_TYPE_MASK) == MT_OLD_MENU)
253 menus[menu->value].current_selection =
254 gui_synclist_get_sel_pos(lists);
255 menu_talk_selected(menu->value);
256 return;
258 sel = get_menu_selection(gui_synclist_get_sel_pos(lists),menu);
259 if ((menu->flags&MENU_TYPE_MASK) == MT_MENU)
261 type = menu->submenus[sel]->flags&MENU_TYPE_MASK;
262 if ((type == MT_SETTING) || (type == MT_SETTING_W_TEXT))
263 talk_setting(menu->submenus[sel]->variable);
264 else
266 if (menu->submenus[sel]->flags&(MENU_DYNAMIC_DESC))
268 char buffer[80];
269 str = menu->submenus[sel]->menu_get_name_and_icon->
270 list_get_name(sel, menu->submenus[sel]->
271 menu_get_name_and_icon->
272 list_get_name_data, buffer);
273 id = P2ID(str);
275 else
276 id = P2ID(menu->submenus[sel]->callback_and_desc->desc);
277 if (id != -1)
278 talk_id(id,false);
283 #define MAX_OPTIONS 32
284 /* returns true if the menu needs to be redrwan */
285 bool do_setting_from_menu(const struct menu_item_ex *temp)
287 int setting_id;
288 const struct settings_list *setting = find_setting(
289 temp->variable,
290 &setting_id);
291 bool ret_val = false;
292 unsigned char *title;
293 if (setting)
295 if ((temp->flags&MENU_TYPE_MASK) == MT_SETTING_W_TEXT)
296 title = temp->callback_and_desc->desc;
297 else
298 title = ID2P(setting->lang_id);
300 if ((setting->flags&F_BOOL_SETTING) == F_BOOL_SETTING)
302 bool temp_var, *var;
303 bool show_icons = global_settings.show_icons;
304 if (setting->flags&F_TEMPVAR)
306 temp_var = *(bool*)setting->setting;
307 var = &temp_var;
309 else
311 var = (bool*)setting->setting;
313 set_bool_options(P2STR(title), var,
314 STR(setting->bool_setting->lang_yes),
315 STR(setting->bool_setting->lang_no),
316 setting->bool_setting->option_callback);
317 if (setting->flags&F_TEMPVAR)
318 *(bool*)setting->setting = temp_var;
319 if (show_icons != global_settings.show_icons)
320 ret_val = true;
322 else if (setting->flags&F_T_SOUND)
324 set_sound(P2STR(title), setting->setting,
325 setting->sound_setting->setting);
327 else /* other setting, must be an INT type */
329 int temp_var, *var;
330 if (setting->flags&F_TEMPVAR)
332 temp_var = *(int*)setting->setting;
333 var = &temp_var;
335 else
337 var = (int*)setting->setting;
339 if (setting->flags&F_INT_SETTING)
341 int min, max, step;
342 if (setting->flags&F_FLIPLIST)
344 min = setting->int_setting->max;
345 max = setting->int_setting->min;
346 step = -setting->int_setting->step;
348 else
350 max = setting->int_setting->max;
351 min = setting->int_setting->min;
352 step = setting->int_setting->step;
354 set_int_ex(P2STR(title), NULL,
355 setting->int_setting->unit,var,
356 setting->int_setting->option_callback,
357 step, min, max,
358 setting->int_setting->formatter,
359 setting->int_setting->get_talk_id);
361 else if (setting->flags&F_CHOICE_SETTING)
363 static struct opt_items options[MAX_OPTIONS];
364 char buffer[256];
365 char *buf_start = buffer;
366 int buf_free = 256;
367 int i,j, count = setting->choice_setting->count;
368 for (i=0, j=0; i<count && i<MAX_OPTIONS; i++)
370 if (setting->flags&F_CHOICETALKS)
372 if (cfg_int_to_string(setting_id, i,
373 buf_start, buf_free))
375 int len = strlen(buf_start) +1;
376 options[j].string = buf_start;
377 buf_start += len;
378 buf_free -= len;
379 options[j].voice_id =
380 setting->choice_setting->talks[i];
381 j++;
384 else
386 options[j].string =
387 P2STR(setting->
388 choice_setting->desc[i]);
389 options[j].voice_id =
390 P2ID(setting->
391 choice_setting->desc[i]);
392 j++;
395 set_option(P2STR(title), var, INT,
396 options,j,
397 setting->
398 choice_setting->option_callback);
400 if (setting->flags&F_TEMPVAR)
401 *(int*)setting->setting = temp_var;
404 return ret_val;
407 int do_menu(const struct menu_item_ex *start_menu, int *start_selected)
409 int selected = start_selected? *start_selected : 0;
410 int action;
411 struct gui_synclist lists;
412 const struct menu_item_ex *temp, *menu;
413 int ret = 0;
414 #ifdef HAS_BUTTONBAR
415 struct gui_buttonbar buttonbar;
416 #endif
418 const struct menu_item_ex *menu_stack[MAX_MENUS];
419 int menu_stack_selected_item[MAX_MENUS];
420 int stack_top = 0;
421 bool in_stringlist, done = false;
422 menu_callback_type menu_callback = NULL;
423 if (start_menu == NULL)
424 menu = &main_menu_;
425 else menu = start_menu;
426 #ifdef HAS_BUTTONBAR
427 gui_buttonbar_init(&buttonbar);
428 gui_buttonbar_set_display(&buttonbar, &(screens[SCREEN_MAIN]) );
429 gui_buttonbar_set(&buttonbar, "<<<", "", "");
430 gui_buttonbar_draw(&buttonbar);
431 #endif
432 init_menu_lists(menu,&lists,selected,true);
433 in_stringlist = ((menu->flags&MENU_TYPE_MASK) == MT_RETURN_ID);
435 talk_menu_item(menu, &lists);
437 gui_synclist_draw(&lists);
438 gui_syncstatusbar_draw(&statusbars, true);
439 action_signalscreenchange();
441 /* load the callback, and only reload it if menu changes */
442 get_menu_callback(menu, &menu_callback);
444 while (!done)
446 action = get_action(CONTEXT_MAINMENU,HZ);
447 /* HZ so the status bar redraws corectly */
448 if (action == ACTION_NONE)
450 gui_syncstatusbar_draw(&statusbars, true);
451 continue;
455 if (menu_callback)
457 int old_action = action;
458 action = menu_callback(action, menu);
459 if (action == ACTION_EXIT_AFTER_THIS_MENUITEM)
461 action = old_action;
462 ret = MENU_SELECTED_EXIT; /* will exit after returning
463 from selection */
467 if (gui_synclist_do_button(&lists,action,LIST_WRAP_UNLESS_HELD))
469 talk_menu_item(menu, &lists);
471 else if (action == ACTION_MENU_WPS)
473 ret = GO_TO_PREVIOUS_MUSIC;
474 done = true;
476 else if (action == ACTION_MENU_STOP)
478 if (audio_status() && !global_settings.party_mode)
480 if (global_settings.fade_on_stop)
481 fade(0);
482 bookmark_autobookmark();
483 audio_stop();
486 else if (action == ACTION_STD_MENU)
488 if ((menu->flags&MENU_TYPE_MASK) == MT_OLD_MENU)
489 return MENU_SELECTED_EXIT;
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 in_stringlist = false;
499 if (menu_callback)
500 menu_callback(ACTION_EXIT_MENUITEM, menu);
502 if (stack_top > 0)
504 stack_top--;
505 menu = menu_stack[stack_top];
506 init_menu_lists(menu, &lists,
507 menu_stack_selected_item[stack_top], false);
508 talk_menu_item(menu, &lists);
509 /* new menu, so reload the callback */
510 get_menu_callback(menu, &menu_callback);
512 else if (menu != &root_menu_)
514 ret = GO_TO_PREVIOUS;
515 done = true;
518 else if (action == ACTION_STD_OK)
520 int type;
521 #ifdef HAS_BUTTONBAR
522 gui_buttonbar_unset(&buttonbar);
523 gui_buttonbar_draw(&buttonbar);
524 #endif
525 if ((menu->flags&MENU_TYPE_MASK) == MT_OLD_MENU)
527 selected = gui_synclist_get_sel_pos(&lists);
528 menus[menu->value].current_selection = selected;
529 action_signalscreenchange();
530 return selected;
532 selected = get_menu_selection(gui_synclist_get_sel_pos(&lists), menu);
533 temp = menu->submenus[selected];
534 if (in_stringlist)
535 type = (menu->flags&MENU_TYPE_MASK);
536 else
538 type = (temp->flags&MENU_TYPE_MASK);
539 get_menu_callback(temp, &menu_callback);
540 if (menu_callback)
542 action = menu_callback(ACTION_ENTER_MENUITEM,temp);
543 if (action == ACTION_EXIT_MENUITEM)
544 break;
547 switch (type)
549 case MT_MENU:
550 if (stack_top < MAX_MENUS)
552 menu_stack[stack_top] = menu;
553 menu_stack_selected_item[stack_top] = selected;
554 stack_top++;
555 init_menu_lists(temp, &lists, 0, true);
556 menu = temp;
557 talk_menu_item(menu, &lists);
559 break;
560 case MT_FUNCTION_CALL:
562 int return_value;
563 action_signalscreenchange();
564 if (temp->flags&MENU_FUNC_USEPARAM)
565 return_value = temp->function->function_w_param(
566 temp->function->param);
567 else
568 return_value = temp->function->function();
569 if (temp->flags&MENU_FUNC_CHECK_RETVAL)
571 if (return_value == 1)
573 done = true;
574 ret = return_value;
577 break;
579 case MT_SETTING:
580 case MT_SETTING_W_TEXT:
582 if (do_setting_from_menu(temp))
583 init_menu_lists(menu, &lists, 0, true);
584 break;
586 case MT_RETURN_ID:
587 if (in_stringlist)
589 action_signalscreenchange();
590 done = true;
591 ret = selected;
593 else if (stack_top < MAX_MENUS)
595 menu_stack[stack_top] = menu;
596 menu_stack_selected_item[stack_top] = selected;
597 stack_top++;
598 menu = temp;
599 init_menu_lists(menu,&lists,0,false);
600 in_stringlist = true;
602 break;
603 case MT_RETURN_VALUE:
604 ret = temp->value;
605 done = true;
606 break;
608 if (type != MT_MENU && menu_callback)
609 menu_callback(ACTION_EXIT_MENUITEM,temp);
610 if (current_submenus_menu != menu)
611 init_menu_lists(menu,&lists,selected,true);
612 /* callback was changed, so reload the menu's callback */
613 get_menu_callback(menu, &menu_callback);
614 #ifdef HAS_BUTTONBAR
615 gui_buttonbar_set(&buttonbar, "<<<", "", "");
616 gui_buttonbar_draw(&buttonbar);
617 #endif
619 else if(default_event_handler(action) == SYS_USB_CONNECTED)
620 ret = MENU_ATTACHED_USB;
621 gui_syncstatusbar_draw(&statusbars, true);
622 gui_synclist_draw(&lists);
624 action_signalscreenchange();
625 if (start_selected)
626 *start_selected = get_menu_selection(
627 gui_synclist_get_sel_pos(&lists), menu);
628 return ret;
631 int main_menu(void)
633 return do_menu(NULL, 0) == MENU_ATTACHED_USB;
636 /* wrappers for the old menu system to work with the new system */
639 static int menu_find_free(void)
641 int i;
642 /* Tries to find an unused slot to put the new menu */
643 for ( i=0; i<MAX_MENUS; i++ ) {
644 if ( !inuse[i] ) {
645 inuse[i] = true;
646 break;
649 if ( i == MAX_MENUS ) {
650 DEBUGF("Out of menus!\n");
651 return -1;
653 return(i);
656 int menu_init(const struct menu_item* mitems, int count, int (*callback)(int, int),
657 const char *button1, const char *button2, const char *button3)
659 (void)button1;
660 (void)button2;
661 (void)button3;
662 int menu=menu_find_free();
663 if(menu==-1)/* Out of menus */
664 return -1;
665 menus[menu].items = (struct menu_item*)mitems; /* de-const */
666 menus[menu].count = count;
667 menus[menu].callback = callback;
668 menus[menu].current_selection = 0;
669 return menu;
672 void menu_exit(int m)
674 inuse[m] = false;
679 static int oldmenuwrapper_callback(int action,
680 const struct menu_item_ex *this_item)
682 if (menus[this_item->value].callback)
684 int val = menus[this_item->value].callback(action, this_item->value);
685 switch (val)
687 case MENU_SELECTED_EXIT:
688 return ACTION_EXIT_MENUITEM;
690 return val;
692 return action;
695 static char* oldmenuwrapper_getname(int selected_item,
696 void * data, char *buffer)
698 (void)buffer;
699 unsigned char* desc = menus[(intptr_t)data].items[selected_item].desc;
700 return P2STR(desc);
702 static void init_oldmenu(const struct menu_item_ex *menu,
703 struct gui_synclist *lists, int selected, bool callback)
705 (void)callback;
706 gui_synclist_init(lists, oldmenuwrapper_getname,
707 (void*)(intptr_t)menu->value, false, 1);
708 gui_synclist_set_nb_items(lists, MENU_GET_COUNT(menu->flags));
709 gui_synclist_limit_scroll(lists, true);
710 gui_synclist_select_item(lists, selected);
713 static void menu_talk_selected(int m)
715 int selected = menus[m].current_selection;
716 int voice_id = P2ID(menus[m].items[selected].desc);
717 if (voice_id >= 0) /* valid ID given? */
718 talk_id(voice_id, false); /* say it */
721 int menu_show(int m)
723 int value;
724 struct menu_item_ex menu;
725 struct menu_get_name_and_icon menu_info =
727 oldmenuwrapper_callback,
728 oldmenuwrapper_getname,
729 (void*)(intptr_t)m, Icon_NOICON
732 menu.flags = (MENU_TYPE_MASK&MT_OLD_MENU) | MENU_DYNAMIC_DESC |
733 MENU_ITEM_COUNT(menus[m].count);
734 menu.value = m;
735 menu.menu_get_name_and_icon = &menu_info;
736 value = do_menu(&menu, &menus[m].current_selection);
737 switch (value)
739 case MENU_ATTACHED_USB:
740 case GO_TO_ROOT:
741 return MENU_ATTACHED_USB;
742 case GO_TO_PREVIOUS:
743 return MENU_SELECTED_EXIT;
744 default:
745 if (value < 0)
746 return menus[m].current_selection;
747 else return value;
752 bool menu_run(int m)
754 int selected;
755 while (1) {
756 switch (selected=menu_show(m))
758 case MENU_SELECTED_EXIT:
759 return false;
761 case MENU_ATTACHED_USB:
762 return true;
764 default:
766 if (selected >= 0 && selected < menus[m].count)
768 if (menus[m].items[selected].function &&
769 menus[m].items[selected].function())
770 return true;
772 gui_syncstatusbar_draw(&statusbars, true);
776 return false;
780 * Property function - return the "count" of menu items in "menu"
783 int menu_count(int menu)
785 return menus[menu].count;