Rename _buttonlight_timeout and use a function call instead of extern'ing buttonlight...
[kugel-rb.git] / apps / menu.c
blob92d14aac6a0b04d0ae6058bacc542859d5de5b5f
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 "backlight.h"
34 #include "menu.h"
35 #include "button.h"
36 #include "kernel.h"
37 #include "debug.h"
38 #include "usb.h"
39 #include "panic.h"
40 #include "settings.h"
41 #include "settings_list.h"
42 #include "option_select.h"
43 #include "screens.h"
44 #include "talk.h"
45 #include "lang.h"
46 #include "misc.h"
47 #include "action.h"
48 #include "menus/exported_menus.h"
49 #include "string.h"
50 #include "root_menu.h"
51 #include "bookmark.h"
52 #include "gwps-common.h" /* for fade() */
53 #include "audio.h"
54 #include "viewport.h"
55 #include "quickscreen.h"
57 #ifdef HAVE_LCD_BITMAP
58 #include "icons.h"
59 #endif
61 /* gui api */
62 #include "list.h"
63 #include "statusbar.h"
64 #include "buttonbar.h"
66 #define MAX_MENUS 8
67 /* used to allow for dynamic menus */
68 #define MAX_MENU_SUBITEMS 64
69 static struct menu_item_ex *current_submenus_menu;
70 static int current_subitems[MAX_MENU_SUBITEMS];
71 static int current_subitems_count = 0;
72 static int talk_menu_item(int selected_item, void *data);
74 static void get_menu_callback(const struct menu_item_ex *m,
75 menu_callback_type *menu_callback)
77 if (m->flags&(MENU_HAS_DESC|MENU_DYNAMIC_DESC))
78 *menu_callback= m->callback_and_desc->menu_callback;
79 else
80 *menu_callback = m->menu_callback;
83 static int get_menu_selection(int selected_item, const struct menu_item_ex *menu)
85 int type = (menu->flags&MENU_TYPE_MASK);
86 if (type == MT_MENU && (selected_item<current_subitems_count))
87 return current_subitems[selected_item];
88 return selected_item;
90 static int find_menu_selection(int selected)
92 int i;
93 for (i=0; i< current_subitems_count; i++)
94 if (current_subitems[i] == selected)
95 return i;
96 return 0;
98 static char * get_menu_item_name(int selected_item,
99 void * data,
100 char *buffer,
101 size_t buffer_len)
103 const struct menu_item_ex *menu = (const struct menu_item_ex *)data;
104 int type = (menu->flags&MENU_TYPE_MASK);
105 selected_item = get_menu_selection(selected_item, menu);
107 (void)buffer_len;
108 /* only MT_MENU or MT_RETURN_ID is allowed in here */
109 if (type == MT_RETURN_ID)
111 if (menu->flags&MENU_DYNAMIC_DESC)
112 return menu->menu_get_name_and_icon->list_get_name(selected_item,
113 menu->menu_get_name_and_icon->list_get_name_data, buffer);
114 return (char*)menu->strings[selected_item];
117 menu = menu->submenus[selected_item];
119 if ((menu->flags&MENU_DYNAMIC_DESC) && (type != MT_SETTING_W_TEXT))
120 return menu->menu_get_name_and_icon->list_get_name(selected_item,
121 menu->menu_get_name_and_icon->list_get_name_data, buffer);
123 type = (menu->flags&MENU_TYPE_MASK);
124 if ((type == MT_SETTING) || (type == MT_SETTING_W_TEXT))
126 const struct settings_list *v
127 = find_setting(menu->variable, NULL);
128 if (v)
129 return str(v->lang_id);
130 else return "Not Done yet!";
132 return P2STR(menu->callback_and_desc->desc);
134 #ifdef HAVE_LCD_BITMAP
135 static int menu_get_icon(int selected_item, void * data)
137 const struct menu_item_ex *menu = (const struct menu_item_ex *)data;
138 int menu_icon = Icon_NOICON;
139 selected_item = get_menu_selection(selected_item, menu);
141 if ((menu->flags&MENU_TYPE_MASK) == MT_RETURN_ID)
143 return Icon_Menu_functioncall;
145 menu = menu->submenus[selected_item];
146 if (menu->flags&MENU_HAS_DESC)
147 menu_icon = menu->callback_and_desc->icon_id;
148 else if (menu->flags&MENU_DYNAMIC_DESC)
149 menu_icon = menu->menu_get_name_and_icon->icon_id;
151 if (menu_icon == Icon_NOICON)
153 switch (menu->flags&MENU_TYPE_MASK)
155 case MT_SETTING:
156 case MT_SETTING_W_TEXT:
157 menu_icon = Icon_Menu_setting;
158 break;
159 case MT_MENU:
160 menu_icon = Icon_Submenu;
161 break;
162 case MT_FUNCTION_CALL:
163 case MT_RETURN_VALUE:
164 menu_icon = Icon_Menu_functioncall;
165 break;
168 return menu_icon;
170 #endif
172 static void init_menu_lists(const struct menu_item_ex *menu,
173 struct gui_synclist *lists, int selected, bool callback,
174 struct viewport parent[NB_SCREENS])
176 int i, count = MENU_GET_COUNT(menu->flags);
177 int type = (menu->flags&MENU_TYPE_MASK);
178 menu_callback_type menu_callback = NULL;
179 int icon;
180 current_subitems_count = 0;
182 if (type == MT_RETURN_ID)
183 get_menu_callback(menu, &menu_callback);
185 for (i=0; i<count; i++)
187 if (type != MT_RETURN_ID)
188 get_menu_callback(menu->submenus[i],&menu_callback);
189 if (menu_callback)
191 if (menu_callback(ACTION_REQUEST_MENUITEM,
192 type==MT_RETURN_ID ? (void*)(intptr_t)i: menu->submenus[i])
193 != ACTION_EXIT_MENUITEM)
195 current_subitems[current_subitems_count] = i;
196 current_subitems_count++;
199 else
201 current_subitems[current_subitems_count] = i;
202 current_subitems_count++;
205 current_submenus_menu = (struct menu_item_ex *)menu;
207 gui_synclist_init(lists,get_menu_item_name,(void*)menu,false,1, parent);
208 #ifdef HAVE_LCD_BITMAP
209 if (menu->callback_and_desc->icon_id == Icon_NOICON)
210 icon = Icon_Submenu_Entered;
211 else
212 icon = menu->callback_and_desc->icon_id;
213 gui_synclist_set_title(lists, P2STR(menu->callback_and_desc->desc), icon);
214 gui_synclist_set_icon_callback(lists, menu_get_icon);
215 #else
216 (void)icon;
217 gui_synclist_set_icon_callback(lists, NULL);
218 #endif
219 if(global_settings.talk_menu)
220 gui_synclist_set_voice_callback(lists, talk_menu_item);
221 gui_synclist_set_nb_items(lists,current_subitems_count);
222 gui_synclist_limit_scroll(lists,true);
223 gui_synclist_select_item(lists, find_menu_selection(selected));
225 get_menu_callback(menu,&menu_callback);
226 if (callback && menu_callback)
227 menu_callback(ACTION_ENTER_MENUITEM,menu);
228 gui_synclist_draw(lists);
229 gui_synclist_speak_item(lists);
232 static int talk_menu_item(int selected_item, void *data)
234 const struct menu_item_ex *menu = (const struct menu_item_ex *)data;
235 int id = -1;
236 int type;
237 unsigned char *str;
238 int sel = get_menu_selection(selected_item, menu);
240 if ((menu->flags&MENU_TYPE_MASK) == MT_MENU)
242 type = menu->submenus[sel]->flags&MENU_TYPE_MASK;
243 if ((type == MT_SETTING) || (type == MT_SETTING_W_TEXT))
244 talk_setting(menu->submenus[sel]->variable);
245 else
247 if (menu->submenus[sel]->flags&(MENU_DYNAMIC_DESC))
249 int (*list_speak_item)(int selected_item, void * data)
250 = menu->submenus[sel]->menu_get_name_and_icon->
251 list_speak_item;
252 if(list_speak_item)
253 list_speak_item(sel, menu->submenus[sel]->
254 menu_get_name_and_icon->
255 list_get_name_data);
256 else
258 char buffer[80];
259 str = menu->submenus[sel]->menu_get_name_and_icon->
260 list_get_name(sel, menu->submenus[sel]->
261 menu_get_name_and_icon->
262 list_get_name_data, buffer);
263 id = P2ID(str);
266 else
267 id = P2ID(menu->submenus[sel]->callback_and_desc->desc);
268 if (id != -1)
269 talk_id(id,false);
272 else if(((menu->flags&MENU_TYPE_MASK) == MT_RETURN_ID))
274 if ((menu->flags&MENU_DYNAMIC_DESC) == 0)
276 unsigned char *s = (unsigned char *)menu->strings[sel];
277 /* string list, try to talk it if ID2P was used */
278 id = P2ID(s);
279 if (id != -1)
280 talk_id(id,false);
283 return 0;
286 void do_setting_from_menu(const struct menu_item_ex *temp,
287 struct viewport parent[NB_SCREENS])
289 int setting_id, oldval;
290 const struct settings_list *setting = find_setting(
291 temp->variable,
292 &setting_id);
293 char *title;
294 char padded_title[MAX_PATH];
295 int var_type = setting->flags&F_T_MASK;
296 if (var_type == F_T_INT || var_type == F_T_UINT)
298 oldval = *(int*)setting->setting;
300 else if (var_type == F_T_BOOL)
302 oldval = *(bool*)setting->setting;
304 else
305 oldval = 0;
306 if ((temp->flags&MENU_TYPE_MASK) == MT_SETTING_W_TEXT)
307 title = temp->callback_and_desc->desc;
308 else
309 title = ID2P(setting->lang_id);
311 /* Pad the title string by repeating it. This is needed
312 so the scroll settings title can actually be used to
313 test the setting */
314 if (setting->flags&F_PADTITLE)
316 int i = 0, len;
317 if (setting->lang_id == -1)
318 title = (char*)setting->cfg_vals;
319 else
320 title = P2STR((unsigned char*)title);
321 len = strlen(title);
322 while (i < MAX_PATH-1)
324 int padlen = MIN(len, MAX_PATH-1-i);
325 strncpy(&padded_title[i], title, padlen);
326 i += padlen;
327 if (i<MAX_PATH-1)
328 padded_title[i++] = ' ';
330 padded_title[i] = '\0';
331 title = padded_title;
334 option_screen((struct settings_list *)setting, parent,
335 setting->flags&F_TEMPVAR, title);
338 /* display a menu */
339 int do_menu(const struct menu_item_ex *start_menu, int *start_selected,
340 struct viewport parent[NB_SCREENS], bool hide_bars)
342 int selected = start_selected? *start_selected : 0;
343 int action;
344 struct gui_synclist lists;
345 const struct menu_item_ex *temp, *menu;
346 int ret = 0, i;
347 bool redraw_lists;
348 int oldbars = viewportmanager_set_statusbar(
349 hide_bars ? VP_SB_HIDE_ALL : VP_SB_ALLSCREENS);
351 const struct menu_item_ex *menu_stack[MAX_MENUS];
352 int menu_stack_selected_item[MAX_MENUS];
353 int stack_top = 0;
354 bool in_stringlist, done = false;
355 struct viewport *vps = NULL;
356 #ifdef HAVE_BUTTONBAR
357 struct gui_buttonbar buttonbar;
358 gui_buttonbar_init(&buttonbar);
359 gui_buttonbar_set_display(&buttonbar, &(screens[SCREEN_MAIN]) );
360 gui_buttonbar_set(&buttonbar, "<<<", "", "");
361 #endif
363 menu_callback_type menu_callback = NULL;
364 if (start_menu == NULL)
365 menu = &main_menu_;
366 else menu = start_menu;
368 /* if hide_bars is true, assume parent has been fixed before passed into
369 * this function, e.g. with viewport_set_defaults(parent, screen, true) */
370 init_menu_lists(menu, &lists, selected, true, parent);
371 vps = *(lists.parent);
372 in_stringlist = ((menu->flags&MENU_TYPE_MASK) == MT_RETURN_ID);
373 /* load the callback, and only reload it if menu changes */
374 get_menu_callback(menu, &menu_callback);
377 #ifdef HAVE_BUTTONBAR
378 if (!hide_bars)
380 gui_buttonbar_set(&buttonbar, "<<<", "", "");
381 gui_buttonbar_draw(&buttonbar);
383 #endif
384 while (!done)
386 redraw_lists = false;
387 if (!hide_bars)
389 #ifdef HAVE_BUTTONBAR
390 gui_buttonbar_draw(&buttonbar);
391 #endif
393 action = get_action(CONTEXT_MAINMENU,
394 list_do_action_timeout(&lists, HZ));
395 /* HZ so the status bar redraws corectly */
397 if (menu_callback)
399 int old_action = action;
400 action = menu_callback(action, menu);
401 if (action == ACTION_EXIT_AFTER_THIS_MENUITEM)
403 action = old_action;
404 ret = MENU_SELECTED_EXIT; /* will exit after returning
405 from selection */
407 else if (action == ACTION_REDRAW)
409 action = old_action;
410 redraw_lists = true;
414 if (gui_synclist_do_button(&lists, &action, LIST_WRAP_UNLESS_HELD))
415 continue;
416 if (action == ACTION_NONE)
417 continue;
418 #ifdef HAVE_QUICKSCREEN
419 else if (action == ACTION_STD_QUICKSCREEN)
421 quick_screen_quick(action);
422 redraw_lists = true;
424 #endif
425 #ifdef HAVE_RECORDING
426 else if (action == ACTION_STD_REC)
428 ret = GO_TO_RECSCREEN;
429 done = true;
431 #endif
432 else if (action == ACTION_TREE_WPS)
434 ret = GO_TO_PREVIOUS_MUSIC;
435 done = true;
437 else if (action == ACTION_TREE_STOP)
439 redraw_lists = list_stop_handler();
441 else if (action == ACTION_STD_CONTEXT)
443 if (menu == &root_menu_)
445 ret = GO_TO_ROOTITEM_CONTEXT;
446 done = true;
448 else if (!in_stringlist)
450 int type;
451 selected = get_menu_selection(gui_synclist_get_sel_pos(&lists),menu);
452 temp = menu->submenus[selected];
453 type = (temp->flags&MENU_TYPE_MASK);
454 if ((type == MT_SETTING_W_TEXT || type == MT_SETTING))
456 #ifdef HAVE_QUICKSCREEN
457 MENUITEM_STRINGLIST(quickscreen_able_option,
458 ID2P(LANG_ONPLAY_MENU_TITLE), NULL,
459 ID2P(LANG_RESET_SETTING),
460 ID2P(LANG_LEFT_QS_ITEM),
461 ID2P(LANG_BOTTOM_QS_ITEM),
462 ID2P(LANG_RIGHT_QS_ITEM));
463 #endif
464 MENUITEM_STRINGLIST(notquickscreen_able_option,
465 ID2P(LANG_ONPLAY_MENU_TITLE), NULL,
466 ID2P(LANG_RESET_SETTING));
467 const struct menu_item_ex *menu;
468 int menu_selection = 0;
469 const struct settings_list *setting =
470 find_setting(temp->variable, NULL);
471 #ifdef HAVE_QUICKSCREEN
472 if (is_setting_quickscreenable(setting))
473 menu = &quickscreen_able_option;
474 else
475 #endif
476 menu = &notquickscreen_able_option;
477 switch (do_menu(menu, &menu_selection, NULL, false))
479 case GO_TO_PREVIOUS:
480 break;
481 case 0: /* reset setting */
482 reset_setting(setting, setting->setting);
483 break;
484 #ifdef HAVE_QUICKSCREEN
485 break;
486 case 1: /* set as left QS item */
487 set_as_qs_item(setting, QUICKSCREEN_LEFT);
488 break;
489 case 2: /* set as bottom QS item */
490 set_as_qs_item(setting, QUICKSCREEN_BOTTOM);
491 break;
492 case 3: /* set as right QS item */
493 set_as_qs_item(setting, QUICKSCREEN_RIGHT);
494 break;
495 #endif
496 } /* swicth(do_menu()) */
497 redraw_lists = true;
499 } /* else if (!in_stringlist) */
501 else if (action == ACTION_STD_MENU)
503 if (menu != &root_menu_)
504 ret = GO_TO_ROOT;
505 else
506 ret = GO_TO_PREVIOUS;
507 done = true;
509 else if (action == ACTION_STD_CANCEL)
511 bool exiting_menu = false;
512 in_stringlist = false;
513 /* might be leaving list, so stop scrolling */
514 FOR_NB_SCREENS(i)
516 screens[i].stop_scroll();
518 if (menu_callback)
519 menu_callback(ACTION_EXIT_MENUITEM, menu);
521 if (menu->flags&MENU_EXITAFTERTHISMENU)
522 done = true;
523 else if ((menu->flags&MENU_TYPE_MASK) == MT_MENU)
524 exiting_menu = true;
525 if (stack_top > 0)
527 stack_top--;
528 menu = menu_stack[stack_top];
529 if (!exiting_menu && (menu->flags&MENU_EXITAFTERTHISMENU))
530 done = true;
531 else
532 init_menu_lists(menu, &lists,
533 menu_stack_selected_item[stack_top], false, vps);
534 /* new menu, so reload the callback */
535 get_menu_callback(menu, &menu_callback);
537 else if (menu != &root_menu_)
539 ret = GO_TO_PREVIOUS;
540 done = true;
543 else if (action == ACTION_STD_OK)
545 int type;
546 /* entering an item that may not be a list, so stop scrolling */
547 FOR_NB_SCREENS(i)
549 screens[i].stop_scroll();
551 #ifdef HAVE_BUTTONBAR
552 if (!hide_bars)
554 gui_buttonbar_unset(&buttonbar);
555 gui_buttonbar_draw(&buttonbar);
557 #endif
558 selected = get_menu_selection(gui_synclist_get_sel_pos(&lists), menu);
559 temp = menu->submenus[selected];
560 redraw_lists = true;
561 if (in_stringlist)
562 type = (menu->flags&MENU_TYPE_MASK);
563 else
565 type = (temp->flags&MENU_TYPE_MASK);
566 get_menu_callback(temp, &menu_callback);
567 if (menu_callback)
569 action = menu_callback(ACTION_ENTER_MENUITEM,temp);
570 if (action == ACTION_EXIT_MENUITEM)
571 break;
574 switch (type)
576 case MT_MENU:
577 if (stack_top < MAX_MENUS)
579 menu_stack[stack_top] = menu;
580 menu_stack_selected_item[stack_top] = selected;
581 stack_top++;
582 init_menu_lists(temp, &lists, 0, true, vps);
583 redraw_lists = false; /* above does the redraw */
584 menu = temp;
586 break;
587 case MT_FUNCTION_CALL:
589 int return_value;
590 if (temp->flags&MENU_FUNC_USEPARAM)
591 return_value = temp->function->function_w_param(
592 temp->function->param);
593 else
594 return_value = temp->function->function();
595 if (!(menu->flags&MENU_EXITAFTERTHISMENU) ||
596 (temp->flags&MENU_EXITAFTERTHISMENU))
598 init_menu_lists(menu, &lists, selected, true, vps);
600 if (temp->flags&MENU_FUNC_CHECK_RETVAL)
602 if (return_value != 0)
604 done = true;
605 ret = return_value;
608 break;
610 case MT_SETTING:
611 case MT_SETTING_W_TEXT:
613 do_setting_from_menu(temp, vps);
614 break;
616 case MT_RETURN_ID:
617 if (in_stringlist)
619 done = true;
620 ret = selected;
622 else if (stack_top < MAX_MENUS)
624 menu_stack[stack_top] = menu;
625 menu_stack_selected_item[stack_top] = selected;
626 stack_top++;
627 menu = temp;
628 init_menu_lists(menu,&lists,0,false, vps);
629 redraw_lists = false; /* above does the redraw */
630 in_stringlist = true;
632 break;
633 case MT_RETURN_VALUE:
634 ret = temp->value;
635 done = true;
636 break;
638 if (type != MT_MENU)
640 if (menu_callback)
641 menu_callback(ACTION_EXIT_MENUITEM,temp);
643 if (current_submenus_menu != menu)
644 init_menu_lists(menu,&lists,selected,true,vps);
645 /* callback was changed, so reload the menu's callback */
646 get_menu_callback(menu, &menu_callback);
647 if ((menu->flags&MENU_EXITAFTERTHISMENU) &&
648 !(temp->flags&MENU_EXITAFTERTHISMENU))
650 done = true;
651 break;
653 #ifdef HAVE_BUTTONBAR
654 if (!hide_bars)
656 gui_buttonbar_set(&buttonbar, "<<<", "", "");
657 gui_buttonbar_draw(&buttonbar);
659 #endif
661 else if(default_event_handler(action) == SYS_USB_CONNECTED)
663 ret = MENU_ATTACHED_USB;
664 done = true;
667 if (redraw_lists && !done)
669 if (menu_callback)
670 menu_callback(ACTION_REDRAW, menu);
671 gui_synclist_draw(&lists);
672 gui_synclist_speak_item(&lists);
675 if (start_selected)
677 /* make sure the start_selected variable is set to
678 the selected item from the menu do_menu() was called from */
679 if (stack_top > 0)
681 menu = menu_stack[0];
682 init_menu_lists(menu,&lists,menu_stack_selected_item[0],true, vps);
684 *start_selected = get_menu_selection(
685 gui_synclist_get_sel_pos(&lists), menu);
687 viewportmanager_set_statusbar(oldbars);
688 return ret;