Fix yellow caused by signedness.
[kugel-rb.git] / apps / menu.c
blobeb4bf0e1c993a7f04f676ed3fbd4cd4254466ebb
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 "file.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 "audio.h"
52 #include "viewport.h"
53 #include "quickscreen.h"
55 #ifdef HAVE_LCD_BITMAP
56 #include "icons.h"
57 #endif
59 /* gui api */
60 #include "list.h"
61 #include "buttonbar.h"
63 #define MAX_MENUS 8
64 /* used to allow for dynamic menus */
65 #define MAX_MENU_SUBITEMS 64
66 static struct menu_item_ex *current_submenus_menu;
67 static int current_subitems[MAX_MENU_SUBITEMS];
68 static int current_subitems_count = 0;
69 static int talk_menu_item(int selected_item, void *data);
71 static void get_menu_callback(const struct menu_item_ex *m,
72 menu_callback_type *menu_callback)
74 if (m->flags&(MENU_HAS_DESC|MENU_DYNAMIC_DESC))
75 *menu_callback= m->callback_and_desc->menu_callback;
76 else
77 *menu_callback = m->menu_callback;
80 static int get_menu_selection(int selected_item, const struct menu_item_ex *menu)
82 int type = (menu->flags&MENU_TYPE_MASK);
83 if ((type == MT_MENU || type == MT_RETURN_ID)
84 && (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 const 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 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 enum themable_icons 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, global_settings.show_icons?menu_get_icon:NULL);
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 =
289 find_setting(temp->variable, &setting_id);
290 char *title;
291 char padded_title[MAX_PATH];
292 int var_type = setting->flags&F_T_MASK;
293 if (var_type == F_T_INT || var_type == F_T_UINT)
295 oldval = *(int*)setting->setting;
297 else if (var_type == F_T_BOOL)
299 oldval = *(bool*)setting->setting;
301 else
302 oldval = 0;
303 if ((temp->flags&MENU_TYPE_MASK) == MT_SETTING_W_TEXT)
304 title = temp->callback_and_desc->desc;
305 else
306 title = ID2P(setting->lang_id);
308 /* Pad the title string by repeating it. This is needed
309 so the scroll settings title can actually be used to
310 test the setting */
311 if (setting->flags&F_PADTITLE)
313 int i = 0, len;
314 if (setting->lang_id == -1)
315 title = (char*)setting->cfg_vals;
316 else
317 title = P2STR((unsigned char*)title);
318 len = strlen(title);
319 while (i < MAX_PATH-1)
321 int padlen = MIN(len, MAX_PATH-1-i);
322 memcpy(&padded_title[i], title, padlen);
323 i += padlen;
324 if (i<MAX_PATH-1)
325 padded_title[i++] = ' ';
327 padded_title[i] = '\0';
328 title = padded_title;
331 option_screen((struct settings_list *)setting, parent,
332 setting->flags&F_TEMPVAR, title);
335 /* display a menu */
336 int do_menu(const struct menu_item_ex *start_menu, int *start_selected,
337 struct viewport parent[NB_SCREENS], bool hide_theme)
339 int selected = start_selected? *start_selected : 0;
340 int action;
341 struct gui_synclist lists;
342 const struct menu_item_ex *temp, *menu;
343 int ret = 0, i;
344 bool redraw_lists;
345 FOR_NB_SCREENS(i)
346 viewportmanager_theme_enable(i, !hide_theme, NULL);
348 const struct menu_item_ex *menu_stack[MAX_MENUS];
349 int menu_stack_selected_item[MAX_MENUS];
350 int stack_top = 0;
351 bool in_stringlist, done = false;
352 struct viewport *vps = NULL;
353 #ifdef HAVE_BUTTONBAR
354 struct gui_buttonbar buttonbar;
355 gui_buttonbar_init(&buttonbar);
356 gui_buttonbar_set_display(&buttonbar, &(screens[SCREEN_MAIN]) );
357 gui_buttonbar_set(&buttonbar, "<<<", "", "");
358 #endif
360 menu_callback_type menu_callback = NULL;
361 if (start_menu == NULL)
362 menu = &main_menu_;
363 else menu = start_menu;
365 /* if hide_theme is true, assume parent has been fixed before passed into
366 * this function, e.g. with viewport_set_defaults(parent, screen) */
367 init_menu_lists(menu, &lists, selected, true, parent);
368 vps = *(lists.parent);
369 in_stringlist = ((menu->flags&MENU_TYPE_MASK) == MT_RETURN_ID);
370 /* load the callback, and only reload it if menu changes */
371 get_menu_callback(menu, &menu_callback);
373 #ifdef HAVE_BUTTONBAR
374 if (!hide_theme)
376 gui_buttonbar_set(&buttonbar, "<<<", "", "");
377 gui_buttonbar_draw(&buttonbar);
379 #endif
380 while (!done)
382 redraw_lists = false;
383 if (!hide_theme)
385 #ifdef HAVE_BUTTONBAR
386 gui_buttonbar_draw(&buttonbar);
387 #endif
389 action = get_action(CONTEXT_MAINMENU,
390 list_do_action_timeout(&lists, HZ));
391 /* HZ so the status bar redraws corectly */
393 if (menu_callback)
395 int old_action = action;
396 action = menu_callback(action, menu);
397 if (action == ACTION_EXIT_AFTER_THIS_MENUITEM)
399 action = old_action;
400 ret = MENU_SELECTED_EXIT; /* will exit after returning
401 from selection */
403 else if (action == ACTION_REDRAW)
405 action = old_action;
406 redraw_lists = true;
410 if (gui_synclist_do_button(&lists, &action, LIST_WRAP_UNLESS_HELD))
411 continue;
412 if (action == ACTION_NONE)
413 continue;
414 #ifdef HAVE_QUICKSCREEN
415 else if (action == ACTION_STD_QUICKSCREEN)
417 quick_screen_quick(action);
418 redraw_lists = true;
420 #endif
421 #ifdef HAVE_RECORDING
422 else if (action == ACTION_STD_REC)
424 ret = GO_TO_RECSCREEN;
425 done = true;
427 #endif
428 else if (action == ACTION_TREE_WPS)
430 ret = GO_TO_PREVIOUS_MUSIC;
431 done = true;
433 else if (action == ACTION_TREE_STOP)
435 redraw_lists = list_stop_handler();
437 else if (action == ACTION_STD_CONTEXT)
439 if (menu == &root_menu_)
441 ret = GO_TO_ROOTITEM_CONTEXT;
442 done = true;
444 else if (!in_stringlist)
446 int type;
447 selected = get_menu_selection(gui_synclist_get_sel_pos(&lists),menu);
448 temp = menu->submenus[selected];
449 type = (temp->flags&MENU_TYPE_MASK);
450 if ((type == MT_SETTING_W_TEXT || type == MT_SETTING))
452 #ifdef HAVE_QUICKSCREEN
453 MENUITEM_STRINGLIST(quickscreen_able_option,
454 ID2P(LANG_ONPLAY_MENU_TITLE), NULL,
455 ID2P(LANG_RESET_SETTING),
456 ID2P(LANG_TOP_QS_ITEM),
457 ID2P(LANG_LEFT_QS_ITEM),
458 ID2P(LANG_BOTTOM_QS_ITEM),
459 ID2P(LANG_RIGHT_QS_ITEM));
460 #endif
461 MENUITEM_STRINGLIST(notquickscreen_able_option,
462 ID2P(LANG_ONPLAY_MENU_TITLE), NULL,
463 ID2P(LANG_RESET_SETTING));
464 const struct menu_item_ex *menu;
465 int menu_selection = 0;
466 const struct settings_list *setting =
467 find_setting(temp->variable, NULL);
468 #ifdef HAVE_QUICKSCREEN
469 if (is_setting_quickscreenable(setting))
470 menu = &quickscreen_able_option;
471 else
472 #endif
473 menu = &notquickscreen_able_option;
474 switch (do_menu(menu, &menu_selection, NULL, false))
476 case GO_TO_PREVIOUS:
477 break;
478 case 0: /* reset setting */
479 reset_setting(setting, setting->setting);
480 break;
481 #ifdef HAVE_QUICKSCREEN
482 case 1: /* set as top QS item */
483 set_as_qs_item(setting, QUICKSCREEN_TOP);
484 break;
485 case 2: /* set as left QS item */
486 set_as_qs_item(setting, QUICKSCREEN_LEFT);
487 break;
488 case 3: /* set as bottom QS item */
489 set_as_qs_item(setting, QUICKSCREEN_BOTTOM);
490 break;
491 case 4: /* set as right QS item */
492 set_as_qs_item(setting, QUICKSCREEN_RIGHT);
493 break;
494 #endif
495 } /* swicth(do_menu()) */
496 redraw_lists = true;
498 } /* else if (!in_stringlist) */
500 else if (action == ACTION_STD_MENU)
502 if (menu != &root_menu_)
503 ret = GO_TO_ROOT;
504 else
505 ret = GO_TO_PREVIOUS;
506 done = true;
508 else if (action == ACTION_STD_CANCEL)
510 bool exiting_menu = false;
511 in_stringlist = false;
512 /* might be leaving list, so stop scrolling */
513 gui_synclist_scroll_stop(&lists);
514 if (menu_callback)
515 menu_callback(ACTION_EXIT_MENUITEM, menu);
517 if (menu->flags&MENU_EXITAFTERTHISMENU)
518 done = true;
519 else if ((menu->flags&MENU_TYPE_MASK) == MT_MENU)
520 exiting_menu = true;
521 if (stack_top > 0)
523 stack_top--;
524 menu = menu_stack[stack_top];
525 if (!exiting_menu && (menu->flags&MENU_EXITAFTERTHISMENU))
526 done = true;
527 else
528 init_menu_lists(menu, &lists,
529 menu_stack_selected_item[stack_top], false, vps);
530 /* new menu, so reload the callback */
531 get_menu_callback(menu, &menu_callback);
533 else if (menu != &root_menu_)
535 ret = GO_TO_PREVIOUS;
536 done = true;
539 else if (action == ACTION_STD_OK)
541 int type;
542 /* entering an item that may not be a list, so stop scrolling */
543 gui_synclist_scroll_stop(&lists);
544 #ifdef HAVE_BUTTONBAR
545 if (!hide_theme)
547 gui_buttonbar_unset(&buttonbar);
548 gui_buttonbar_draw(&buttonbar);
550 #endif
551 selected = get_menu_selection(gui_synclist_get_sel_pos(&lists), menu);
552 temp = menu->submenus[selected];
553 redraw_lists = true;
554 if (in_stringlist)
555 type = (menu->flags&MENU_TYPE_MASK);
556 else
558 type = (temp->flags&MENU_TYPE_MASK);
559 get_menu_callback(temp, &menu_callback);
560 if (menu_callback)
562 action = menu_callback(ACTION_ENTER_MENUITEM,temp);
563 if (action == ACTION_EXIT_MENUITEM)
564 break;
567 switch (type)
569 case MT_MENU:
570 if (stack_top < MAX_MENUS)
572 menu_stack[stack_top] = menu;
573 menu_stack_selected_item[stack_top] = selected;
574 stack_top++;
575 init_menu_lists(temp, &lists, 0, true, vps);
576 redraw_lists = false; /* above does the redraw */
577 menu = temp;
579 break;
580 case MT_FUNCTION_CALL:
582 int return_value;
583 if (temp->flags&MENU_FUNC_USEPARAM)
584 return_value = temp->function->function_w_param(
585 temp->function->param);
586 else
587 return_value = temp->function->function();
588 if (!(menu->flags&MENU_EXITAFTERTHISMENU) ||
589 (temp->flags&MENU_EXITAFTERTHISMENU))
591 init_menu_lists(menu, &lists, selected, true, vps);
593 if (temp->flags&MENU_FUNC_CHECK_RETVAL)
595 if (return_value != 0)
597 done = true;
598 ret = return_value;
601 break;
603 case MT_SETTING:
604 case MT_SETTING_W_TEXT:
606 do_setting_from_menu(temp, vps);
607 break;
609 case MT_RETURN_ID:
610 if (in_stringlist)
612 done = true;
613 ret = selected;
615 else if (stack_top < MAX_MENUS)
617 menu_stack[stack_top] = menu;
618 menu_stack_selected_item[stack_top] = selected;
619 stack_top++;
620 menu = temp;
621 init_menu_lists(menu,&lists,0,false, vps);
622 redraw_lists = false; /* above does the redraw */
623 in_stringlist = true;
625 break;
626 case MT_RETURN_VALUE:
627 ret = temp->value;
628 done = true;
629 break;
631 if (type != MT_MENU)
633 if (menu_callback)
634 menu_callback(ACTION_EXIT_MENUITEM,temp);
636 if (current_submenus_menu != menu)
637 init_menu_lists(menu,&lists,selected,true,vps);
638 /* callback was changed, so reload the menu's callback */
639 get_menu_callback(menu, &menu_callback);
640 if ((menu->flags&MENU_EXITAFTERTHISMENU) &&
641 !(temp->flags&MENU_EXITAFTERTHISMENU))
643 done = true;
644 break;
646 #ifdef HAVE_BUTTONBAR
647 if (!hide_theme)
649 gui_buttonbar_set(&buttonbar, "<<<", "", "");
650 gui_buttonbar_draw(&buttonbar);
652 #endif
654 else if(default_event_handler(action) == SYS_USB_CONNECTED)
656 ret = MENU_ATTACHED_USB;
657 done = true;
660 if (redraw_lists && !done)
662 if (menu_callback)
663 menu_callback(ACTION_REDRAW, menu);
664 gui_synclist_draw(&lists);
665 gui_synclist_speak_item(&lists);
668 if (start_selected)
670 /* make sure the start_selected variable is set to
671 the selected item from the menu do_menu() was called from */
672 if (stack_top > 0)
674 menu = menu_stack[0];
675 init_menu_lists(menu,&lists,menu_stack_selected_item[0],true, vps);
677 *start_selected = get_menu_selection(
678 gui_synclist_get_sel_pos(&lists), menu);
680 FOR_NB_SCREENS(i)
681 viewportmanager_theme_undo(i, false);
682 return ret;