Remove unused function audio_has_changed_track from apps/mpeg.c
[kugel-rb.git] / apps / menu.c
blob2de31cc0a99cac4aa735e25c550ab4d5ff7ee3c5
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2002 Robert E. Hak
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
22 2005 Kevin Ferrare :
23 - Multi screen support
24 - Rewrote/removed a lot of code now useless with the new gui API
26 #include <stdbool.h>
27 #include <stdlib.h>
28 #include "config.h"
29 #include "system.h"
31 #include "lcd.h"
32 #include "font.h"
33 #include "menu.h"
34 #include "button.h"
35 #include "kernel.h"
36 #include "debug.h"
37 #include "usb.h"
38 #include "panic.h"
39 #include "settings.h"
40 #include "settings_list.h"
41 #include "option_select.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 "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;
284 void do_setting_from_menu(const struct menu_item_ex *temp,
285 struct viewport parent[NB_SCREENS])
287 int setting_id, oldval;
288 const struct settings_list *setting = find_setting(
289 temp->variable,
290 &setting_id);
291 char *title;
292 char padded_title[MAX_PATH];
293 int var_type = setting->flags&F_T_MASK;
294 if (var_type == F_T_INT || var_type == F_T_UINT)
296 oldval = *(int*)setting->setting;
298 else if (var_type == F_T_BOOL)
300 oldval = *(bool*)setting->setting;
302 else
303 oldval = 0;
304 if ((temp->flags&MENU_TYPE_MASK) == MT_SETTING_W_TEXT)
305 title = temp->callback_and_desc->desc;
306 else
307 title = ID2P(setting->lang_id);
309 /* Pad the title string by repeating it. This is needed
310 so the scroll settings title can actually be used to
311 test the setting */
312 if (setting->flags&F_PADTITLE)
314 int i = 0, len;
315 if (setting->lang_id == -1)
316 title = (char*)setting->cfg_vals;
317 else
318 title = P2STR((unsigned char*)title);
319 len = strlen(title);
320 while (i < MAX_PATH-1)
322 int padlen = MIN(len, MAX_PATH-1-i);
323 strncpy(&padded_title[i], title, padlen);
324 i += padlen;
325 if (i<MAX_PATH-1)
326 padded_title[i++] = ' ';
328 padded_title[i] = '\0';
329 title = padded_title;
332 option_screen((struct settings_list *)setting, parent,
333 setting->flags&F_TEMPVAR, title);
336 /* display a menu */
337 int do_menu(const struct menu_item_ex *start_menu, int *start_selected,
338 struct viewport parent[NB_SCREENS], bool hide_bars)
340 int selected = start_selected? *start_selected : 0;
341 int action;
342 struct gui_synclist lists;
343 const struct menu_item_ex *temp, *menu;
344 int ret = 0, i;
345 bool redraw_lists;
346 int oldbars = viewportmanager_set_statusbar(
347 hide_bars ? VP_SB_HIDE_ALL : VP_SB_ALLSCREENS);
349 const struct menu_item_ex *menu_stack[MAX_MENUS];
350 int menu_stack_selected_item[MAX_MENUS];
351 int stack_top = 0;
352 bool in_stringlist, done = false;
353 struct viewport *vps = NULL;
354 #ifdef HAVE_BUTTONBAR
355 struct gui_buttonbar buttonbar;
356 gui_buttonbar_init(&buttonbar);
357 gui_buttonbar_set_display(&buttonbar, &(screens[SCREEN_MAIN]) );
358 gui_buttonbar_set(&buttonbar, "<<<", "", "");
359 #endif
361 menu_callback_type menu_callback = NULL;
362 if (start_menu == NULL)
363 menu = &main_menu_;
364 else menu = start_menu;
366 /* if hide_bars is true, assume parent has been fixed before passed into
367 * this function, e.g. with viewport_set_defaults(parent, screen, true) */
368 init_menu_lists(menu, &lists, selected, true, parent);
369 vps = *(lists.parent);
370 in_stringlist = ((menu->flags&MENU_TYPE_MASK) == MT_RETURN_ID);
371 /* load the callback, and only reload it if menu changes */
372 get_menu_callback(menu, &menu_callback);
375 #ifdef HAVE_BUTTONBAR
376 if (!hide_bars)
378 gui_buttonbar_set(&buttonbar, "<<<", "", "");
379 gui_buttonbar_draw(&buttonbar);
381 #endif
382 while (!done)
384 redraw_lists = false;
385 if (!hide_bars)
387 #ifdef HAVE_BUTTONBAR
388 gui_buttonbar_draw(&buttonbar);
389 #endif
391 action = get_action(CONTEXT_MAINMENU,
392 list_do_action_timeout(&lists, HZ));
393 /* HZ so the status bar redraws corectly */
395 if (menu_callback)
397 int old_action = action;
398 action = menu_callback(action, menu);
399 if (action == ACTION_EXIT_AFTER_THIS_MENUITEM)
401 action = old_action;
402 ret = MENU_SELECTED_EXIT; /* will exit after returning
403 from selection */
405 else if (action == ACTION_REDRAW)
407 action = old_action;
408 redraw_lists = true;
412 if (gui_synclist_do_button(&lists, &action, LIST_WRAP_UNLESS_HELD))
413 continue;
414 if (action == ACTION_NONE)
415 continue;
416 #ifdef HAVE_QUICKSCREEN
417 else if (action == ACTION_STD_QUICKSCREEN)
419 quick_screen_quick(action);
420 redraw_lists = true;
422 #endif
423 #ifdef HAVE_RECORDING
424 else if (action == ACTION_STD_REC)
426 ret = GO_TO_RECSCREEN;
427 done = true;
429 #endif
430 else if (action == ACTION_TREE_WPS)
432 ret = GO_TO_PREVIOUS_MUSIC;
433 done = true;
435 else if (action == ACTION_TREE_STOP)
437 redraw_lists = list_stop_handler();
439 else if (action == ACTION_STD_CONTEXT)
441 if (menu == &root_menu_)
443 ret = GO_TO_ROOTITEM_CONTEXT;
444 done = true;
446 else if (!in_stringlist)
448 int type;
449 selected = get_menu_selection(gui_synclist_get_sel_pos(&lists),menu);
450 temp = menu->submenus[selected];
451 type = (temp->flags&MENU_TYPE_MASK);
452 if ((type == MT_SETTING_W_TEXT || type == MT_SETTING))
454 #ifdef HAVE_QUICKSCREEN
455 MENUITEM_STRINGLIST(quickscreen_able_option,
456 ID2P(LANG_ONPLAY_MENU_TITLE), NULL,
457 ID2P(LANG_RESET_SETTING),
458 ID2P(LANG_LEFT_QS_ITEM),
459 ID2P(LANG_BOTTOM_QS_ITEM),
460 ID2P(LANG_RIGHT_QS_ITEM));
461 #endif
462 MENUITEM_STRINGLIST(notquickscreen_able_option,
463 ID2P(LANG_ONPLAY_MENU_TITLE), NULL,
464 ID2P(LANG_RESET_SETTING));
465 const struct menu_item_ex *menu;
466 int menu_selection = 0;
467 const struct settings_list *setting =
468 find_setting(temp->variable, NULL);
469 #ifdef HAVE_QUICKSCREEN
470 if (is_setting_quickscreenable(setting))
471 menu = &quickscreen_able_option;
472 else
473 #endif
474 menu = &notquickscreen_able_option;
475 switch (do_menu(menu, &menu_selection, NULL, false))
477 case GO_TO_PREVIOUS:
478 break;
479 case 0: /* reset setting */
480 reset_setting(setting, setting->setting);
481 break;
482 #ifdef HAVE_QUICKSCREEN
483 break;
484 case 1: /* set as left QS item */
485 set_as_qs_item(setting, QUICKSCREEN_LEFT);
486 break;
487 case 2: /* set as bottom QS item */
488 set_as_qs_item(setting, QUICKSCREEN_BOTTOM);
489 break;
490 case 3: /* set as right QS item */
491 set_as_qs_item(setting, QUICKSCREEN_RIGHT);
492 break;
493 #endif
494 } /* swicth(do_menu()) */
495 redraw_lists = true;
497 } /* else if (!in_stringlist) */
499 else if (action == ACTION_STD_MENU)
501 if (menu != &root_menu_)
502 ret = GO_TO_ROOT;
503 else
504 ret = GO_TO_PREVIOUS;
505 done = true;
507 else if (action == ACTION_STD_CANCEL)
509 bool exiting_menu = false;
510 in_stringlist = false;
511 /* might be leaving list, so stop scrolling */
512 FOR_NB_SCREENS(i)
514 screens[i].stop_scroll();
516 if (menu_callback)
517 menu_callback(ACTION_EXIT_MENUITEM, menu);
519 if (menu->flags&MENU_EXITAFTERTHISMENU)
520 done = true;
521 else if ((menu->flags&MENU_TYPE_MASK) == MT_MENU)
522 exiting_menu = true;
523 if (stack_top > 0)
525 stack_top--;
526 menu = menu_stack[stack_top];
527 if (!exiting_menu && (menu->flags&MENU_EXITAFTERTHISMENU))
528 done = true;
529 else
530 init_menu_lists(menu, &lists,
531 menu_stack_selected_item[stack_top], false, vps);
532 /* new menu, so reload the callback */
533 get_menu_callback(menu, &menu_callback);
535 else if (menu != &root_menu_)
537 ret = GO_TO_PREVIOUS;
538 done = true;
541 else if (action == ACTION_STD_OK)
543 int type;
544 /* entering an item that may not be a list, so stop scrolling */
545 FOR_NB_SCREENS(i)
547 screens[i].stop_scroll();
549 #ifdef HAVE_BUTTONBAR
550 if (!hide_bars)
552 gui_buttonbar_unset(&buttonbar);
553 gui_buttonbar_draw(&buttonbar);
555 #endif
556 selected = get_menu_selection(gui_synclist_get_sel_pos(&lists), menu);
557 temp = menu->submenus[selected];
558 redraw_lists = true;
559 if (in_stringlist)
560 type = (menu->flags&MENU_TYPE_MASK);
561 else
563 type = (temp->flags&MENU_TYPE_MASK);
564 get_menu_callback(temp, &menu_callback);
565 if (menu_callback)
567 action = menu_callback(ACTION_ENTER_MENUITEM,temp);
568 if (action == ACTION_EXIT_MENUITEM)
569 break;
572 switch (type)
574 case MT_MENU:
575 if (stack_top < MAX_MENUS)
577 menu_stack[stack_top] = menu;
578 menu_stack_selected_item[stack_top] = selected;
579 stack_top++;
580 init_menu_lists(temp, &lists, 0, true, vps);
581 redraw_lists = false; /* above does the redraw */
582 menu = temp;
584 break;
585 case MT_FUNCTION_CALL:
587 int return_value;
588 if (temp->flags&MENU_FUNC_USEPARAM)
589 return_value = temp->function->function_w_param(
590 temp->function->param);
591 else
592 return_value = temp->function->function();
593 if (!(menu->flags&MENU_EXITAFTERTHISMENU) ||
594 (temp->flags&MENU_EXITAFTERTHISMENU))
596 init_menu_lists(menu, &lists, selected, true, vps);
598 if (temp->flags&MENU_FUNC_CHECK_RETVAL)
600 if (return_value != 0)
602 done = true;
603 ret = return_value;
606 break;
608 case MT_SETTING:
609 case MT_SETTING_W_TEXT:
611 do_setting_from_menu(temp, vps);
612 break;
614 case MT_RETURN_ID:
615 if (in_stringlist)
617 done = true;
618 ret = selected;
620 else if (stack_top < MAX_MENUS)
622 menu_stack[stack_top] = menu;
623 menu_stack_selected_item[stack_top] = selected;
624 stack_top++;
625 menu = temp;
626 init_menu_lists(menu,&lists,0,false, vps);
627 redraw_lists = false; /* above does the redraw */
628 in_stringlist = true;
630 break;
631 case MT_RETURN_VALUE:
632 ret = temp->value;
633 done = true;
634 break;
636 if (type != MT_MENU)
638 if (menu_callback)
639 menu_callback(ACTION_EXIT_MENUITEM,temp);
641 if (current_submenus_menu != menu)
642 init_menu_lists(menu,&lists,selected,true,vps);
643 /* callback was changed, so reload the menu's callback */
644 get_menu_callback(menu, &menu_callback);
645 if ((menu->flags&MENU_EXITAFTERTHISMENU) &&
646 !(temp->flags&MENU_EXITAFTERTHISMENU))
648 done = true;
649 break;
651 #ifdef HAVE_BUTTONBAR
652 if (!hide_bars)
654 gui_buttonbar_set(&buttonbar, "<<<", "", "");
655 gui_buttonbar_draw(&buttonbar);
657 #endif
659 else if(default_event_handler(action) == SYS_USB_CONNECTED)
661 ret = MENU_ATTACHED_USB;
662 done = true;
665 if (redraw_lists && !done)
667 if (menu_callback)
668 menu_callback(ACTION_REDRAW, menu);
669 gui_synclist_draw(&lists);
670 gui_synclist_speak_item(&lists);
673 if (start_selected)
675 /* make sure the start_selected variable is set to
676 the selected item from the menu do_menu() was called from */
677 if (stack_top > 0)
679 menu = menu_stack[0];
680 init_menu_lists(menu,&lists,menu_stack_selected_item[0],true, vps);
682 *start_selected = get_menu_selection(
683 gui_synclist_get_sel_pos(&lists), menu);
685 viewportmanager_set_statusbar(oldbars);
686 return ret;