Change HMAC-SHA1 implementation to the one taken from gnulib, which
[kugel-rb.git] / apps / menu.c
blobc215d6812d34a68a813ee4a5b311c9fd24ad80e4
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"
27 #include "system.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 "option_select.h"
41 #include "status.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"
55 #ifdef HAVE_LCD_BITMAP
56 #include "icons.h"
57 #endif
59 /* gui api */
60 #include "list.h"
61 #include "statusbar.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;
283 /* this is used to reload the default menu viewports when the
284 theme changes. nothing happens if the menu is using a supplied parent vp */
285 void init_default_menu_viewports(struct viewport parent[NB_SCREENS], bool hide_bars)
287 int i;
288 FOR_NB_SCREENS(i)
290 viewport_set_defaults(&parent[i], i);
291 /* viewport_set_defaults() fixes the vp for the bars, so resize */
292 if (hide_bars)
294 if (global_settings.statusbar)
296 parent[i].y -= STATUSBAR_HEIGHT;
297 parent[i].height += STATUSBAR_HEIGHT;
301 #ifdef HAS_BUTTONBAR
302 if (!hide_bars && global_settings.buttonbar)
303 parent[0].height -= BUTTONBAR_HEIGHT;
304 #endif
307 bool do_setting_from_menu(const struct menu_item_ex *temp)
309 int setting_id, oldval;
310 const struct settings_list *setting = find_setting(
311 temp->variable,
312 &setting_id);
313 char *title;
314 char padded_title[MAX_PATH];
315 int var_type = setting->flags&F_T_MASK;
316 if (var_type == F_T_INT || var_type == F_T_UINT)
318 oldval = *(int*)setting->setting;
320 else if (var_type == F_T_BOOL)
322 oldval = *(bool*)setting->setting;
324 else
325 oldval = 0;
326 if ((temp->flags&MENU_TYPE_MASK) == MT_SETTING_W_TEXT)
327 title = temp->callback_and_desc->desc;
328 else
329 title = ID2P(setting->lang_id);
331 /* Pad the title string by repeating it. This is needed
332 so the scroll settings title can actually be used to
333 test the setting */
334 if (setting->flags&F_PADTITLE)
336 int i = 0, len;
337 if (setting->lang_id == -1)
338 title = (char*)setting->cfg_vals;
339 else
340 title = P2STR((unsigned char*)title);
341 len = strlen(title);
342 while (i < MAX_PATH-1)
344 int padlen = MIN(len, MAX_PATH-1-i);
345 strncpy(&padded_title[i], title, padlen);
346 i += padlen;
347 if (i<MAX_PATH-1)
348 padded_title[i++] = ' ';
350 padded_title[i] = '\0';
351 title = padded_title;
354 option_screen((struct settings_list *)setting,
355 setting->flags&F_TEMPVAR, title);
356 if (var_type == F_T_INT || var_type == F_T_UINT)
358 return oldval != *(int*)setting->setting;
360 else if (var_type == F_T_BOOL)
362 return oldval != *(bool*)setting->setting;
364 return false;
367 /* display a menu */
368 int do_menu(const struct menu_item_ex *start_menu, int *start_selected,
369 struct viewport parent[NB_SCREENS], bool hide_bars)
371 int selected = start_selected? *start_selected : 0;
372 int action;
373 struct gui_synclist lists;
374 const struct menu_item_ex *temp, *menu;
375 int ret = 0, i;
376 bool redraw_lists;
378 const struct menu_item_ex *menu_stack[MAX_MENUS];
379 int menu_stack_selected_item[MAX_MENUS];
380 int stack_top = 0;
381 bool in_stringlist, done = false;
383 struct viewport *vps, menu_vp[NB_SCREENS]; /* menu_vp will hopefully be phased out */
384 #ifdef HAS_BUTTONBAR
385 struct gui_buttonbar buttonbar;
386 gui_buttonbar_init(&buttonbar);
387 gui_buttonbar_set_display(&buttonbar, &(screens[SCREEN_MAIN]) );
388 gui_buttonbar_set(&buttonbar, "<<<", "", "");
389 #endif
391 menu_callback_type menu_callback = NULL;
392 if (start_menu == NULL)
393 menu = &main_menu_;
394 else menu = start_menu;
396 if (parent)
398 vps = parent;
399 /* if hide_bars == true we assume the viewport is correctly sized */
401 else
403 vps = menu_vp;
404 init_default_menu_viewports(vps, hide_bars);
406 FOR_NB_SCREENS(i)
408 screens[i].set_viewport(&vps[i]);
409 screens[i].clear_viewport();
410 screens[i].set_viewport(NULL);
412 init_menu_lists(menu, &lists, selected, true, vps);
413 in_stringlist = ((menu->flags&MENU_TYPE_MASK) == MT_RETURN_ID);
415 /* load the callback, and only reload it if menu changes */
416 get_menu_callback(menu, &menu_callback);
419 #ifdef HAS_BUTTONBAR
420 if (!hide_bars)
422 gui_buttonbar_set(&buttonbar, "<<<", "", "");
423 gui_buttonbar_draw(&buttonbar);
425 #endif
426 while (!done)
428 redraw_lists = false;
429 if (!hide_bars)
431 gui_syncstatusbar_draw(&statusbars, true);
433 action = get_action(CONTEXT_MAINMENU,
434 list_do_action_timeout(&lists, HZ));
435 /* HZ so the status bar redraws corectly */
437 if (action != ACTION_NONE && menu_callback)
439 int old_action = action;
440 action = menu_callback(action, menu);
441 if (action == ACTION_EXIT_AFTER_THIS_MENUITEM)
443 action = old_action;
444 ret = MENU_SELECTED_EXIT; /* will exit after returning
445 from selection */
447 else if (action == ACTION_REDRAW)
449 action = old_action;
450 redraw_lists = true;
454 if (gui_synclist_do_button(&lists, &action, LIST_WRAP_UNLESS_HELD))
455 continue;
456 if (action == ACTION_NONE)
457 continue;
459 #ifdef HAVE_RECORDING
460 if (action == ACTION_STD_REC)
462 ret = GO_TO_RECSCREEN;
463 done = true;
465 else
466 #endif
467 if (action == ACTION_TREE_WPS)
469 ret = GO_TO_PREVIOUS_MUSIC;
470 done = true;
472 else if (action == ACTION_TREE_STOP)
474 redraw_lists = list_stop_handler();
476 else if (action == ACTION_STD_CONTEXT &&
477 menu == &root_menu_)
479 ret = GO_TO_ROOTITEM_CONTEXT;
480 done = true;
482 else if (action == ACTION_STD_MENU)
484 if (menu != &root_menu_)
485 ret = GO_TO_ROOT;
486 else
487 ret = GO_TO_PREVIOUS;
488 done = true;
490 else if (action == ACTION_STD_CANCEL)
492 bool exiting_menu = false;
493 in_stringlist = false;
494 if (menu_callback)
495 menu_callback(ACTION_EXIT_MENUITEM, menu);
497 if (menu->flags&MENU_EXITAFTERTHISMENU)
498 done = true;
499 else if ((menu->flags&MENU_TYPE_MASK) == MT_MENU)
500 exiting_menu = true;
501 if (stack_top > 0)
503 stack_top--;
504 menu = menu_stack[stack_top];
505 if (!exiting_menu && (menu->flags&MENU_EXITAFTERTHISMENU))
506 done = true;
507 else
508 init_menu_lists(menu, &lists,
509 menu_stack_selected_item[stack_top], false, vps);
510 /* new menu, so reload the callback */
511 get_menu_callback(menu, &menu_callback);
513 else if (menu != &root_menu_)
515 ret = GO_TO_PREVIOUS;
516 done = true;
519 else if (action == ACTION_STD_OK)
521 int type;
522 #ifdef HAS_BUTTONBAR
523 if (!hide_bars)
525 gui_buttonbar_unset(&buttonbar);
526 gui_buttonbar_draw(&buttonbar);
528 #endif
529 selected = get_menu_selection(gui_synclist_get_sel_pos(&lists), menu);
530 temp = menu->submenus[selected];
531 redraw_lists = true;
532 if (in_stringlist)
533 type = (menu->flags&MENU_TYPE_MASK);
534 else
536 type = (temp->flags&MENU_TYPE_MASK);
537 get_menu_callback(temp, &menu_callback);
538 if (menu_callback)
540 action = menu_callback(ACTION_ENTER_MENUITEM,temp);
541 if (action == ACTION_EXIT_MENUITEM)
542 break;
545 switch (type)
547 case MT_MENU:
548 if (stack_top < MAX_MENUS)
550 menu_stack[stack_top] = menu;
551 menu_stack_selected_item[stack_top] = selected;
552 stack_top++;
553 init_menu_lists(temp, &lists, 0, true, vps);
554 redraw_lists = false; /* above does the redraw */
555 menu = temp;
557 break;
558 case MT_FUNCTION_CALL:
560 int return_value;
561 if (temp->flags&MENU_FUNC_USEPARAM)
562 return_value = temp->function->function_w_param(
563 temp->function->param);
564 else
565 return_value = temp->function->function();
567 init_default_menu_viewports(menu_vp, hide_bars);
568 init_menu_lists(menu, &lists, selected, true, vps);
570 if (temp->flags&MENU_FUNC_CHECK_RETVAL)
572 if (return_value == 1)
574 done = true;
575 ret = return_value;
578 break;
580 case MT_SETTING:
581 case MT_SETTING_W_TEXT:
583 if (do_setting_from_menu(temp))
585 init_default_menu_viewports(menu_vp, hide_bars);
586 init_menu_lists(menu, &lists, selected, true,vps);
587 redraw_lists = false; /* above does the redraw */
589 break;
591 case MT_RETURN_ID:
592 if (in_stringlist)
594 done = true;
595 ret = selected;
597 else if (stack_top < MAX_MENUS)
599 menu_stack[stack_top] = menu;
600 menu_stack_selected_item[stack_top] = selected;
601 stack_top++;
602 menu = temp;
603 init_menu_lists(menu,&lists,0,false, vps);
604 redraw_lists = false; /* above does the redraw */
605 in_stringlist = true;
607 break;
608 case MT_RETURN_VALUE:
609 ret = temp->value;
610 done = true;
611 break;
613 if (type != MT_MENU)
615 if (menu_callback)
616 menu_callback(ACTION_EXIT_MENUITEM,temp);
618 if (current_submenus_menu != menu)
619 init_menu_lists(menu,&lists,selected,true,vps);
620 /* callback was changed, so reload the menu's callback */
621 get_menu_callback(menu, &menu_callback);
622 if ((menu->flags&MENU_EXITAFTERTHISMENU) &&
623 !(temp->flags&MENU_EXITAFTERTHISMENU))
625 done = true;
626 break;
628 #ifdef HAS_BUTTONBAR
629 if (!hide_bars)
631 gui_buttonbar_set(&buttonbar, "<<<", "", "");
632 gui_buttonbar_draw(&buttonbar);
634 #endif
636 else if(default_event_handler(action) == SYS_USB_CONNECTED)
638 ret = MENU_ATTACHED_USB;
639 done = true;
642 if (redraw_lists && !done)
644 gui_synclist_draw(&lists);
645 gui_synclist_speak_item(&lists);
648 if (start_selected)
650 /* make sure the start_selected variable is set to
651 the selected item from the menu do_menu() was called from */
652 if (stack_top > 0)
654 menu = menu_stack[0];
655 init_menu_lists(menu,&lists,menu_stack_selected_item[0],true, vps);
657 *start_selected = get_menu_selection(
658 gui_synclist_get_sel_pos(&lists), menu);
660 return ret;