Correct comment.
[kugel-rb.git] / apps / menu.c
blobb640905c0fc7b86961fd7899f4808c88b6cbb874
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 "appevents.h"
32 #include "lcd.h"
33 #include "font.h"
34 #include "file.h"
35 #include "menu.h"
36 #include "button.h"
37 #include "kernel.h"
38 #include "debug.h"
39 #include "usb.h"
40 #include "panic.h"
41 #include "settings.h"
42 #include "settings_list.h"
43 #include "option_select.h"
44 #include "screens.h"
45 #include "talk.h"
46 #include "lang.h"
47 #include "misc.h"
48 #include "action.h"
49 #include "menus/exported_menus.h"
50 #include "string.h"
51 #include "root_menu.h"
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 /* hotkey settings */
65 #ifdef HAVE_HOTKEY
66 const struct menu_item_ex *selected_menu_item;
67 bool hotkey_settable_menu = false;
68 #endif
70 #define MAX_MENUS 8
71 /* used to allow for dynamic menus */
72 #define MAX_MENU_SUBITEMS 64
73 static struct menu_item_ex *current_submenus_menu;
74 static int current_subitems[MAX_MENU_SUBITEMS];
75 static int current_subitems_count = 0;
76 static int talk_menu_item(int selected_item, void *data);
78 static void get_menu_callback(const struct menu_item_ex *m,
79 menu_callback_type *menu_callback)
81 if (m->flags&(MENU_HAS_DESC|MENU_DYNAMIC_DESC))
82 *menu_callback= m->callback_and_desc->menu_callback;
83 else
84 *menu_callback = m->menu_callback;
87 static int get_menu_selection(int selected_item, const struct menu_item_ex *menu)
89 int type = (menu->flags&MENU_TYPE_MASK);
90 if ((type == MT_MENU || type == MT_RETURN_ID)
91 && (selected_item<current_subitems_count))
92 return current_subitems[selected_item];
93 return selected_item;
95 static int find_menu_selection(int selected)
97 int i;
98 for (i=0; i< current_subitems_count; i++)
99 if (current_subitems[i] == selected)
100 return i;
101 return 0;
103 static const char* get_menu_item_name(int selected_item,
104 void * data,
105 char *buffer,
106 size_t buffer_len)
108 const struct menu_item_ex *menu = (const struct menu_item_ex *)data;
109 int type = (menu->flags&MENU_TYPE_MASK);
110 selected_item = get_menu_selection(selected_item, menu);
112 (void)buffer_len;
113 /* only MT_MENU or MT_RETURN_ID is allowed in here */
114 if (type == MT_RETURN_ID)
116 if (menu->flags&MENU_DYNAMIC_DESC)
117 return menu->menu_get_name_and_icon->list_get_name(selected_item,
118 menu->menu_get_name_and_icon->list_get_name_data, buffer);
119 return menu->strings[selected_item];
122 menu = menu->submenus[selected_item];
124 if ((menu->flags&MENU_DYNAMIC_DESC) && (type != MT_SETTING_W_TEXT))
125 return menu->menu_get_name_and_icon->list_get_name(selected_item,
126 menu->menu_get_name_and_icon->list_get_name_data, buffer);
128 type = (menu->flags&MENU_TYPE_MASK);
129 if ((type == MT_SETTING) || (type == MT_SETTING_W_TEXT))
131 const struct settings_list *v
132 = find_setting(menu->variable, NULL);
133 if (v)
134 return str(v->lang_id);
135 else return "Not Done yet!";
137 return P2STR(menu->callback_and_desc->desc);
139 #ifdef HAVE_LCD_BITMAP
140 static enum themable_icons menu_get_icon(int selected_item, void * data)
142 const struct menu_item_ex *menu = (const struct menu_item_ex *)data;
143 int menu_icon = Icon_NOICON;
144 selected_item = get_menu_selection(selected_item, menu);
146 if ((menu->flags&MENU_TYPE_MASK) == MT_RETURN_ID)
148 return Icon_Menu_functioncall;
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 if (menu_icon == Icon_NOICON)
158 switch (menu->flags&MENU_TYPE_MASK)
160 case MT_SETTING:
161 case MT_SETTING_W_TEXT:
162 menu_icon = Icon_Menu_setting;
163 break;
164 case MT_MENU:
165 menu_icon = Icon_Submenu;
166 break;
167 case MT_FUNCTION_CALL:
168 case MT_RETURN_VALUE:
169 menu_icon = Icon_Menu_functioncall;
170 break;
173 return menu_icon;
175 #endif
177 static void init_menu_lists(const struct menu_item_ex *menu,
178 struct gui_synclist *lists, int selected, bool callback,
179 struct viewport parent[NB_SCREENS])
181 int i, count = MENU_GET_COUNT(menu->flags);
182 int type = (menu->flags&MENU_TYPE_MASK);
183 menu_callback_type menu_callback = NULL;
184 int icon;
185 current_subitems_count = 0;
187 if (type == MT_RETURN_ID)
188 get_menu_callback(menu, &menu_callback);
190 for (i=0; i<count; i++)
192 if (type != MT_RETURN_ID)
193 get_menu_callback(menu->submenus[i],&menu_callback);
194 if (menu_callback)
196 if (menu_callback(ACTION_REQUEST_MENUITEM,
197 type==MT_RETURN_ID ? (void*)(intptr_t)i: menu->submenus[i])
198 != ACTION_EXIT_MENUITEM)
200 current_subitems[current_subitems_count] = i;
201 current_subitems_count++;
204 else
206 current_subitems[current_subitems_count] = i;
207 current_subitems_count++;
210 current_submenus_menu = (struct menu_item_ex *)menu;
212 gui_synclist_init(lists,get_menu_item_name,(void*)menu,false,1, parent);
213 #ifdef HAVE_LCD_BITMAP
214 if (menu->callback_and_desc->icon_id == Icon_NOICON)
215 icon = Icon_Submenu_Entered;
216 else
217 icon = menu->callback_and_desc->icon_id;
218 gui_synclist_set_title(lists, P2STR(menu->callback_and_desc->desc), icon);
219 gui_synclist_set_icon_callback(lists, global_settings.show_icons?menu_get_icon:NULL);
220 #else
221 (void)icon;
222 gui_synclist_set_icon_callback(lists, NULL);
223 #endif
224 if(global_settings.talk_menu)
225 gui_synclist_set_voice_callback(lists, talk_menu_item);
226 gui_synclist_set_nb_items(lists,current_subitems_count);
227 gui_synclist_limit_scroll(lists,true);
228 gui_synclist_select_item(lists, find_menu_selection(selected));
230 get_menu_callback(menu,&menu_callback);
231 if (callback && menu_callback)
232 menu_callback(ACTION_ENTER_MENUITEM,menu);
233 gui_synclist_draw(lists);
234 gui_synclist_speak_item(lists);
237 static int talk_menu_item(int selected_item, void *data)
239 const struct menu_item_ex *menu = (const struct menu_item_ex *)data;
240 int id = -1;
241 int type;
242 unsigned char *str;
243 int sel = get_menu_selection(selected_item, menu);
245 if ((menu->flags&MENU_TYPE_MASK) == MT_MENU)
247 type = menu->submenus[sel]->flags&MENU_TYPE_MASK;
248 if ((type == MT_SETTING) || (type == MT_SETTING_W_TEXT))
249 talk_setting(menu->submenus[sel]->variable);
250 else
252 if (menu->submenus[sel]->flags&(MENU_DYNAMIC_DESC))
254 int (*list_speak_item)(int selected_item, void * data)
255 = menu->submenus[sel]->menu_get_name_and_icon->
256 list_speak_item;
257 if(list_speak_item)
258 list_speak_item(sel, menu->submenus[sel]->
259 menu_get_name_and_icon->
260 list_get_name_data);
261 else
263 char buffer[80];
264 str = menu->submenus[sel]->menu_get_name_and_icon->
265 list_get_name(sel, menu->submenus[sel]->
266 menu_get_name_and_icon->
267 list_get_name_data, buffer);
268 id = P2ID(str);
271 else
272 id = P2ID(menu->submenus[sel]->callback_and_desc->desc);
273 if (id != -1)
274 talk_id(id,false);
277 else if(((menu->flags&MENU_TYPE_MASK) == MT_RETURN_ID))
279 if ((menu->flags&MENU_DYNAMIC_DESC) == 0)
281 unsigned char *s = (unsigned char *)menu->strings[sel];
282 /* string list, try to talk it if ID2P was used */
283 id = P2ID(s);
284 if (id != -1)
285 talk_id(id,false);
288 return 0;
291 void do_setting_from_menu(const struct menu_item_ex *temp,
292 struct viewport parent[NB_SCREENS])
294 int setting_id, oldval;
295 const struct settings_list *setting =
296 find_setting(temp->variable, &setting_id);
297 char *title;
298 char padded_title[MAX_PATH];
299 int var_type = setting->flags&F_T_MASK;
300 if (var_type == F_T_INT || var_type == F_T_UINT)
302 oldval = *(int*)setting->setting;
304 else if (var_type == F_T_BOOL)
306 oldval = *(bool*)setting->setting;
308 else
309 oldval = 0;
310 if ((temp->flags&MENU_TYPE_MASK) == MT_SETTING_W_TEXT)
311 title = temp->callback_and_desc->desc;
312 else
313 title = ID2P(setting->lang_id);
315 /* Pad the title string by repeating it. This is needed
316 so the scroll settings title can actually be used to
317 test the setting */
318 if (setting->flags&F_PADTITLE)
320 int i = 0, len;
321 if (setting->lang_id == -1)
322 title = (char*)setting->cfg_vals;
323 else
324 title = P2STR((unsigned char*)title);
325 len = strlen(title);
326 while (i < MAX_PATH-1)
328 int padlen = MIN(len, MAX_PATH-1-i);
329 memcpy(&padded_title[i], title, padlen);
330 i += padlen;
331 if (i<MAX_PATH-1)
332 padded_title[i++] = ' ';
334 padded_title[i] = '\0';
335 title = padded_title;
338 option_screen((struct settings_list *)setting, parent,
339 setting->flags&F_TEMPVAR, title);
342 /* display a menu */
343 int do_menu(const struct menu_item_ex *start_menu, int *start_selected,
344 struct viewport parent[NB_SCREENS], bool hide_theme)
346 int selected = start_selected? *start_selected : 0;
347 int action;
348 struct gui_synclist lists;
349 const struct menu_item_ex *temp, *menu;
350 int ret = 0, i;
351 bool redraw_lists;
352 FOR_NB_SCREENS(i)
353 viewportmanager_theme_enable(i, !hide_theme, NULL);
355 const struct menu_item_ex *menu_stack[MAX_MENUS];
356 int menu_stack_selected_item[MAX_MENUS];
357 int stack_top = 0;
358 bool in_stringlist, done = false;
359 struct viewport *vps = NULL;
360 #ifdef HAVE_BUTTONBAR
361 struct gui_buttonbar buttonbar;
362 gui_buttonbar_init(&buttonbar);
363 gui_buttonbar_set_display(&buttonbar, &(screens[SCREEN_MAIN]) );
364 gui_buttonbar_set(&buttonbar, "<<<", "", "");
365 #endif
367 menu_callback_type menu_callback = NULL;
368 if (start_menu == NULL)
369 menu = &main_menu_;
370 else menu = start_menu;
372 /* if hide_theme is true, assume parent has been fixed before passed into
373 * this function, e.g. with viewport_set_defaults(parent, screen) */
374 init_menu_lists(menu, &lists, selected, true, parent);
375 vps = *(lists.parent);
376 in_stringlist = ((menu->flags&MENU_TYPE_MASK) == MT_RETURN_ID);
377 /* load the callback, and only reload it if menu changes */
378 get_menu_callback(menu, &menu_callback);
380 #ifdef HAVE_BUTTONBAR
381 if (!hide_theme)
383 gui_buttonbar_set(&buttonbar, "<<<", "", "");
384 gui_buttonbar_draw(&buttonbar);
386 #endif
387 while (!done)
389 redraw_lists = false;
390 if (!hide_theme)
392 #ifdef HAVE_BUTTONBAR
393 gui_buttonbar_draw(&buttonbar);
394 #endif
396 action = get_action(CONTEXT_MAINMENU,
397 list_do_action_timeout(&lists, HZ));
398 /* HZ so the status bar redraws corectly */
400 if (menu_callback)
402 int old_action = action;
403 action = menu_callback(action, menu);
404 if (action == ACTION_EXIT_AFTER_THIS_MENUITEM)
406 action = old_action;
407 ret = MENU_SELECTED_EXIT; /* will exit after returning
408 from selection */
410 else if (action == ACTION_REDRAW)
412 action = old_action;
413 redraw_lists = true;
417 if (gui_synclist_do_button(&lists, &action, LIST_WRAP_UNLESS_HELD))
418 continue;
419 if (action == ACTION_NONE)
420 continue;
421 #ifdef HAVE_QUICKSCREEN
422 else if (action == ACTION_STD_QUICKSCREEN)
424 quick_screen_quick(action);
425 redraw_lists = true;
427 #endif
428 #ifdef HAVE_RECORDING
429 else if (action == ACTION_STD_REC)
431 ret = GO_TO_RECSCREEN;
432 done = true;
434 #endif
435 #ifdef HAVE_HOTKEY
436 else if (hotkey_settable_menu &&
437 ((action == ACTION_WPS_HOTKEY) ||
438 (action == ACTION_TREE_HOTKEY)))
440 ret = MENU_SELECTED_HOTKEY;
441 done = true;
442 selected = get_menu_selection(gui_synclist_get_sel_pos(&lists),menu);
443 selected_menu_item = menu->submenus[selected];
445 #endif
446 else if (action == ACTION_TREE_WPS)
448 ret = GO_TO_PREVIOUS_MUSIC;
449 done = true;
451 else if (action == ACTION_TREE_STOP)
453 redraw_lists = list_stop_handler();
455 else if (action == ACTION_STD_CONTEXT)
457 if (menu == &root_menu_)
459 ret = GO_TO_ROOTITEM_CONTEXT;
460 done = true;
462 else if (!in_stringlist)
464 int type;
465 selected = get_menu_selection(gui_synclist_get_sel_pos(&lists),menu);
466 temp = menu->submenus[selected];
467 type = (temp->flags&MENU_TYPE_MASK);
468 if ((type == MT_SETTING_W_TEXT || type == MT_SETTING))
470 #ifdef HAVE_QUICKSCREEN
471 MENUITEM_STRINGLIST(quickscreen_able_option,
472 ID2P(LANG_ONPLAY_MENU_TITLE), NULL,
473 ID2P(LANG_RESET_SETTING),
474 ID2P(LANG_TOP_QS_ITEM),
475 ID2P(LANG_LEFT_QS_ITEM),
476 ID2P(LANG_BOTTOM_QS_ITEM),
477 ID2P(LANG_RIGHT_QS_ITEM));
478 #endif
479 MENUITEM_STRINGLIST(notquickscreen_able_option,
480 ID2P(LANG_ONPLAY_MENU_TITLE), NULL,
481 ID2P(LANG_RESET_SETTING));
482 const struct menu_item_ex *menu;
483 int menu_selection = 0;
484 const struct settings_list *setting =
485 find_setting(temp->variable, NULL);
486 #ifdef HAVE_QUICKSCREEN
487 if (is_setting_quickscreenable(setting))
488 menu = &quickscreen_able_option;
489 else
490 #endif
491 menu = &notquickscreen_able_option;
492 switch (do_menu(menu, &menu_selection, NULL, false))
494 case GO_TO_PREVIOUS:
495 break;
496 case 0: /* reset setting */
497 reset_setting(setting, setting->setting);
498 break;
499 #ifdef HAVE_QUICKSCREEN
500 case 1: /* set as top QS item */
501 set_as_qs_item(setting, QUICKSCREEN_TOP);
502 break;
503 case 2: /* set as left QS item */
504 set_as_qs_item(setting, QUICKSCREEN_LEFT);
505 break;
506 case 3: /* set as bottom QS item */
507 set_as_qs_item(setting, QUICKSCREEN_BOTTOM);
508 break;
509 case 4: /* set as right QS item */
510 set_as_qs_item(setting, QUICKSCREEN_RIGHT);
511 break;
512 #endif
513 } /* swicth(do_menu()) */
514 redraw_lists = true;
516 } /* else if (!in_stringlist) */
518 else if (action == ACTION_STD_MENU)
520 if (menu != &root_menu_)
521 ret = GO_TO_ROOT;
522 else
523 ret = GO_TO_PREVIOUS;
524 done = true;
526 else if (action == ACTION_STD_CANCEL)
528 bool exiting_menu = false;
529 in_stringlist = false;
530 /* might be leaving list, so stop scrolling */
531 gui_synclist_scroll_stop(&lists);
532 if (menu_callback)
533 menu_callback(ACTION_EXIT_MENUITEM, menu);
535 if (menu->flags&MENU_EXITAFTERTHISMENU)
536 done = true;
537 else if ((menu->flags&MENU_TYPE_MASK) == MT_MENU)
538 exiting_menu = true;
539 if (stack_top > 0)
541 stack_top--;
542 menu = menu_stack[stack_top];
543 if (!exiting_menu && (menu->flags&MENU_EXITAFTERTHISMENU))
544 done = true;
545 else
546 init_menu_lists(menu, &lists,
547 menu_stack_selected_item[stack_top], false, vps);
548 /* new menu, so reload the callback */
549 get_menu_callback(menu, &menu_callback);
551 else if (menu != &root_menu_)
553 ret = GO_TO_PREVIOUS;
554 done = true;
557 else if (action == ACTION_STD_OK)
559 int type;
560 /* entering an item that may not be a list, so stop scrolling */
561 gui_synclist_scroll_stop(&lists);
562 #ifdef HAVE_BUTTONBAR
563 if (!hide_theme)
565 gui_buttonbar_unset(&buttonbar);
566 gui_buttonbar_draw(&buttonbar);
568 #endif
569 selected = get_menu_selection(gui_synclist_get_sel_pos(&lists), menu);
570 temp = menu->submenus[selected];
571 redraw_lists = true;
572 if (in_stringlist)
573 type = (menu->flags&MENU_TYPE_MASK);
574 else
576 type = (temp->flags&MENU_TYPE_MASK);
577 get_menu_callback(temp, &menu_callback);
578 if (menu_callback)
580 action = menu_callback(ACTION_ENTER_MENUITEM,temp);
581 if (action == ACTION_EXIT_MENUITEM)
582 break;
585 switch (type)
587 case MT_MENU:
588 if (stack_top < MAX_MENUS)
590 menu_stack[stack_top] = menu;
591 menu_stack_selected_item[stack_top] = selected;
592 stack_top++;
593 init_menu_lists(temp, &lists, 0, true, vps);
594 redraw_lists = false; /* above does the redraw */
595 menu = temp;
597 break;
598 case MT_FUNCTION_CALL:
600 int return_value;
601 if (temp->flags&MENU_FUNC_USEPARAM)
602 return_value = temp->function->function_w_param(
603 temp->function->param);
604 else
605 return_value = temp->function->function();
606 if (!(menu->flags&MENU_EXITAFTERTHISMENU) ||
607 (temp->flags&MENU_EXITAFTERTHISMENU))
609 init_menu_lists(menu, &lists, selected, true, vps);
611 if (temp->flags&MENU_FUNC_CHECK_RETVAL)
613 if (return_value != 0)
615 done = true;
616 ret = return_value;
619 break;
621 case MT_SETTING:
622 case MT_SETTING_W_TEXT:
624 do_setting_from_menu(temp, vps);
625 send_event(GUI_EVENT_ACTIONUPDATE, (void*)1); /* force a redraw */
626 break;
628 case MT_RETURN_ID:
629 if (in_stringlist)
631 done = true;
632 ret = selected;
634 else if (stack_top < MAX_MENUS)
636 menu_stack[stack_top] = menu;
637 menu_stack_selected_item[stack_top] = selected;
638 stack_top++;
639 menu = temp;
640 init_menu_lists(menu,&lists,0,false, vps);
641 redraw_lists = false; /* above does the redraw */
642 in_stringlist = true;
644 break;
645 case MT_RETURN_VALUE:
646 ret = temp->value;
647 done = true;
648 break;
650 if (type != MT_MENU)
652 if (menu_callback)
653 menu_callback(ACTION_EXIT_MENUITEM,temp);
655 if (current_submenus_menu != menu)
656 init_menu_lists(menu,&lists,selected,true,vps);
657 /* callback was changed, so reload the menu's callback */
658 get_menu_callback(menu, &menu_callback);
659 if ((menu->flags&MENU_EXITAFTERTHISMENU) &&
660 !(temp->flags&MENU_EXITAFTERTHISMENU))
662 done = true;
663 break;
665 #ifdef HAVE_BUTTONBAR
666 if (!hide_theme)
668 gui_buttonbar_set(&buttonbar, "<<<", "", "");
669 gui_buttonbar_draw(&buttonbar);
671 #endif
673 else if(default_event_handler(action) == SYS_USB_CONNECTED)
675 ret = MENU_ATTACHED_USB;
676 done = true;
679 if (redraw_lists && !done)
681 if (menu_callback)
682 menu_callback(ACTION_REDRAW, menu);
683 gui_synclist_draw(&lists);
684 gui_synclist_speak_item(&lists);
687 if (start_selected)
689 /* make sure the start_selected variable is set to
690 the selected item from the menu do_menu() was called from */
691 if (stack_top > 0)
693 menu = menu_stack[0];
694 init_menu_lists(menu,&lists,menu_stack_selected_item[0],true, vps);
696 *start_selected = get_menu_selection(
697 gui_synclist_get_sel_pos(&lists), menu);
699 FOR_NB_SCREENS(i)
700 viewportmanager_theme_undo(i, false);
701 return ret;