Update Swedish translation.
[kugel-rb.git] / apps / menu.c
blobc55c39fc5c0264b961fa54490a19a54d6b88c5a4
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 "status.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 "bookmark.h"
53 #include "gwps-common.h" /* for fade() */
54 #include "audio.h"
55 #include "viewport.h"
56 #include "quickscreen.h"
58 #ifdef HAVE_LCD_BITMAP
59 #include "icons.h"
60 #endif
62 /* gui api */
63 #include "list.h"
64 #include "statusbar.h"
65 #include "buttonbar.h"
67 #define MAX_MENUS 8
68 /* used to allow for dynamic menus */
69 #define MAX_MENU_SUBITEMS 64
70 static struct menu_item_ex *current_submenus_menu;
71 static int current_subitems[MAX_MENU_SUBITEMS];
72 static int current_subitems_count = 0;
73 static int talk_menu_item(int selected_item, void *data);
75 static void get_menu_callback(const struct menu_item_ex *m,
76 menu_callback_type *menu_callback)
78 if (m->flags&(MENU_HAS_DESC|MENU_DYNAMIC_DESC))
79 *menu_callback= m->callback_and_desc->menu_callback;
80 else
81 *menu_callback = m->menu_callback;
84 static int get_menu_selection(int selected_item, const struct menu_item_ex *menu)
86 int type = (menu->flags&MENU_TYPE_MASK);
87 if (type == MT_MENU && (selected_item<current_subitems_count))
88 return current_subitems[selected_item];
89 return selected_item;
91 static int find_menu_selection(int selected)
93 int i;
94 for (i=0; i< current_subitems_count; i++)
95 if (current_subitems[i] == selected)
96 return i;
97 return 0;
99 static char * get_menu_item_name(int selected_item,
100 void * data,
101 char *buffer,
102 size_t buffer_len)
104 const struct menu_item_ex *menu = (const struct menu_item_ex *)data;
105 int type = (menu->flags&MENU_TYPE_MASK);
106 selected_item = get_menu_selection(selected_item, menu);
108 (void)buffer_len;
109 /* only MT_MENU or MT_RETURN_ID is allowed in here */
110 if (type == MT_RETURN_ID)
112 if (menu->flags&MENU_DYNAMIC_DESC)
113 return menu->menu_get_name_and_icon->list_get_name(selected_item,
114 menu->menu_get_name_and_icon->list_get_name_data, buffer);
115 return (char*)menu->strings[selected_item];
118 menu = menu->submenus[selected_item];
120 if ((menu->flags&MENU_DYNAMIC_DESC) && (type != MT_SETTING_W_TEXT))
121 return menu->menu_get_name_and_icon->list_get_name(selected_item,
122 menu->menu_get_name_and_icon->list_get_name_data, buffer);
124 type = (menu->flags&MENU_TYPE_MASK);
125 if ((type == MT_SETTING) || (type == MT_SETTING_W_TEXT))
127 const struct settings_list *v
128 = find_setting(menu->variable, NULL);
129 if (v)
130 return str(v->lang_id);
131 else return "Not Done yet!";
133 return P2STR(menu->callback_and_desc->desc);
135 #ifdef HAVE_LCD_BITMAP
136 static int menu_get_icon(int selected_item, void * data)
138 const struct menu_item_ex *menu = (const struct menu_item_ex *)data;
139 int menu_icon = Icon_NOICON;
140 selected_item = get_menu_selection(selected_item, menu);
142 if ((menu->flags&MENU_TYPE_MASK) == MT_RETURN_ID)
144 return Icon_Menu_functioncall;
146 menu = menu->submenus[selected_item];
147 if (menu->flags&MENU_HAS_DESC)
148 menu_icon = menu->callback_and_desc->icon_id;
149 else if (menu->flags&MENU_DYNAMIC_DESC)
150 menu_icon = menu->menu_get_name_and_icon->icon_id;
152 if (menu_icon == Icon_NOICON)
154 switch (menu->flags&MENU_TYPE_MASK)
156 case MT_SETTING:
157 case MT_SETTING_W_TEXT:
158 menu_icon = Icon_Menu_setting;
159 break;
160 case MT_MENU:
161 menu_icon = Icon_Submenu;
162 break;
163 case MT_FUNCTION_CALL:
164 case MT_RETURN_VALUE:
165 menu_icon = Icon_Menu_functioncall;
166 break;
169 return menu_icon;
171 #endif
173 static void init_menu_lists(const struct menu_item_ex *menu,
174 struct gui_synclist *lists, int selected, bool callback,
175 struct viewport parent[NB_SCREENS])
177 int i, count = MENU_GET_COUNT(menu->flags);
178 int type = (menu->flags&MENU_TYPE_MASK);
179 menu_callback_type menu_callback = NULL;
180 int icon;
181 current_subitems_count = 0;
183 if (type == MT_RETURN_ID)
184 get_menu_callback(menu, &menu_callback);
186 for (i=0; i<count; i++)
188 if (type != MT_RETURN_ID)
189 get_menu_callback(menu->submenus[i],&menu_callback);
190 if (menu_callback)
192 if (menu_callback(ACTION_REQUEST_MENUITEM,
193 type==MT_RETURN_ID ? (void*)(intptr_t)i: menu->submenus[i])
194 != ACTION_EXIT_MENUITEM)
196 current_subitems[current_subitems_count] = i;
197 current_subitems_count++;
200 else
202 current_subitems[current_subitems_count] = i;
203 current_subitems_count++;
206 current_submenus_menu = (struct menu_item_ex *)menu;
208 gui_synclist_init(lists,get_menu_item_name,(void*)menu,false,1, parent);
209 #ifdef HAVE_LCD_BITMAP
210 if (menu->callback_and_desc->icon_id == Icon_NOICON)
211 icon = Icon_Submenu_Entered;
212 else
213 icon = menu->callback_and_desc->icon_id;
214 gui_synclist_set_title(lists, P2STR(menu->callback_and_desc->desc), icon);
215 gui_synclist_set_icon_callback(lists, menu_get_icon);
216 #else
217 (void)icon;
218 gui_synclist_set_icon_callback(lists, NULL);
219 #endif
220 if(global_settings.talk_menu)
221 gui_synclist_set_voice_callback(lists, talk_menu_item);
222 gui_synclist_set_nb_items(lists,current_subitems_count);
223 gui_synclist_limit_scroll(lists,true);
224 gui_synclist_select_item(lists, find_menu_selection(selected));
226 get_menu_callback(menu,&menu_callback);
227 if (callback && menu_callback)
228 menu_callback(ACTION_ENTER_MENUITEM,menu);
229 gui_synclist_draw(lists);
230 gui_synclist_speak_item(lists);
233 static int talk_menu_item(int selected_item, void *data)
235 const struct menu_item_ex *menu = (const struct menu_item_ex *)data;
236 int id = -1;
237 int type;
238 unsigned char *str;
239 int sel = get_menu_selection(selected_item, menu);
241 if ((menu->flags&MENU_TYPE_MASK) == MT_MENU)
243 type = menu->submenus[sel]->flags&MENU_TYPE_MASK;
244 if ((type == MT_SETTING) || (type == MT_SETTING_W_TEXT))
245 talk_setting(menu->submenus[sel]->variable);
246 else
248 if (menu->submenus[sel]->flags&(MENU_DYNAMIC_DESC))
250 int (*list_speak_item)(int selected_item, void * data)
251 = menu->submenus[sel]->menu_get_name_and_icon->
252 list_speak_item;
253 if(list_speak_item)
254 list_speak_item(sel, menu->submenus[sel]->
255 menu_get_name_and_icon->
256 list_get_name_data);
257 else
259 char buffer[80];
260 str = menu->submenus[sel]->menu_get_name_and_icon->
261 list_get_name(sel, menu->submenus[sel]->
262 menu_get_name_and_icon->
263 list_get_name_data, buffer);
264 id = P2ID(str);
267 else
268 id = P2ID(menu->submenus[sel]->callback_and_desc->desc);
269 if (id != -1)
270 talk_id(id,false);
273 else if(((menu->flags&MENU_TYPE_MASK) == MT_RETURN_ID))
275 if ((menu->flags&MENU_DYNAMIC_DESC) == 0)
277 unsigned char *s = (unsigned char *)menu->strings[sel];
278 /* string list, try to talk it if ID2P was used */
279 id = P2ID(s);
280 if (id != -1)
281 talk_id(id,false);
284 return 0;
286 /* this is used to reload the default menu viewports when the
287 theme changes. nothing happens if the menu is using a supplied parent vp */
288 static void init_default_menu_viewports(struct viewport parent[NB_SCREENS], bool hide_bars)
290 int i;
291 FOR_NB_SCREENS(i)
293 viewport_set_defaults(&parent[i], i);
294 /* viewport_set_defaults() fixes the vp for the bars, so resize */
295 if (hide_bars)
297 if (global_settings.statusbar)
299 parent[i].y -= STATUSBAR_HEIGHT;
300 parent[i].height += STATUSBAR_HEIGHT;
304 #ifdef HAVE_BUTTONBAR
305 if (!hide_bars && global_settings.buttonbar)
306 parent[0].height -= BUTTONBAR_HEIGHT;
307 #endif
310 bool do_setting_from_menu(const struct menu_item_ex *temp,
311 struct viewport parent[NB_SCREENS])
313 int setting_id, oldval;
314 const struct settings_list *setting = find_setting(
315 temp->variable,
316 &setting_id);
317 char *title;
318 char padded_title[MAX_PATH];
319 int var_type = setting->flags&F_T_MASK;
320 if (var_type == F_T_INT || var_type == F_T_UINT)
322 oldval = *(int*)setting->setting;
324 else if (var_type == F_T_BOOL)
326 oldval = *(bool*)setting->setting;
328 else
329 oldval = 0;
330 if ((temp->flags&MENU_TYPE_MASK) == MT_SETTING_W_TEXT)
331 title = temp->callback_and_desc->desc;
332 else
333 title = ID2P(setting->lang_id);
335 /* Pad the title string by repeating it. This is needed
336 so the scroll settings title can actually be used to
337 test the setting */
338 if (setting->flags&F_PADTITLE)
340 int i = 0, len;
341 if (setting->lang_id == -1)
342 title = (char*)setting->cfg_vals;
343 else
344 title = P2STR((unsigned char*)title);
345 len = strlen(title);
346 while (i < MAX_PATH-1)
348 int padlen = MIN(len, MAX_PATH-1-i);
349 strncpy(&padded_title[i], title, padlen);
350 i += padlen;
351 if (i<MAX_PATH-1)
352 padded_title[i++] = ' ';
354 padded_title[i] = '\0';
355 title = padded_title;
358 option_screen((struct settings_list *)setting, parent,
359 setting->flags&F_TEMPVAR, title);
360 if (var_type == F_T_INT || var_type == F_T_UINT)
362 return oldval != *(int*)setting->setting;
364 else if (var_type == F_T_BOOL)
366 return oldval != *(bool*)setting->setting;
368 return false;
371 /* display a menu */
372 int do_menu(const struct menu_item_ex *start_menu, int *start_selected,
373 struct viewport parent[NB_SCREENS], bool hide_bars)
375 int selected = start_selected? *start_selected : 0;
376 int action;
377 struct gui_synclist lists;
378 const struct menu_item_ex *temp, *menu;
379 int ret = 0, i;
380 bool redraw_lists;
382 const struct menu_item_ex *menu_stack[MAX_MENUS];
383 int menu_stack_selected_item[MAX_MENUS];
384 int stack_top = 0;
385 bool in_stringlist, done = false;
387 struct viewport *vps, menu_vp[NB_SCREENS]; /* menu_vp will hopefully be phased out */
388 #ifdef HAVE_BUTTONBAR
389 struct gui_buttonbar buttonbar;
390 gui_buttonbar_init(&buttonbar);
391 gui_buttonbar_set_display(&buttonbar, &(screens[SCREEN_MAIN]) );
392 gui_buttonbar_set(&buttonbar, "<<<", "", "");
393 #endif
395 menu_callback_type menu_callback = NULL;
396 if (start_menu == NULL)
397 menu = &main_menu_;
398 else menu = start_menu;
400 if (parent)
402 vps = parent;
403 /* if hide_bars == true we assume the viewport is correctly sized */
405 else
407 vps = menu_vp;
408 init_default_menu_viewports(vps, hide_bars);
410 FOR_NB_SCREENS(i)
412 screens[i].set_viewport(&vps[i]);
413 screens[i].clear_viewport();
414 screens[i].set_viewport(NULL);
416 init_menu_lists(menu, &lists, selected, true, vps);
417 in_stringlist = ((menu->flags&MENU_TYPE_MASK) == MT_RETURN_ID);
419 /* load the callback, and only reload it if menu changes */
420 get_menu_callback(menu, &menu_callback);
423 #ifdef HAVE_BUTTONBAR
424 if (!hide_bars)
426 gui_buttonbar_set(&buttonbar, "<<<", "", "");
427 gui_buttonbar_draw(&buttonbar);
429 #endif
430 while (!done)
432 redraw_lists = false;
433 if (!hide_bars)
435 gui_syncstatusbar_draw(&statusbars, true);
437 action = get_action(CONTEXT_MAINMENU,
438 list_do_action_timeout(&lists, HZ));
439 /* HZ so the status bar redraws corectly */
441 if (action != ACTION_NONE && menu_callback)
443 int old_action = action;
444 action = menu_callback(action, menu);
445 if (action == ACTION_EXIT_AFTER_THIS_MENUITEM)
447 action = old_action;
448 ret = MENU_SELECTED_EXIT; /* will exit after returning
449 from selection */
451 else if (action == ACTION_REDRAW)
453 action = old_action;
454 redraw_lists = true;
458 if (gui_synclist_do_button(&lists, &action, LIST_WRAP_UNLESS_HELD))
459 continue;
460 if (action == ACTION_NONE)
461 continue;
462 #ifdef HAVE_QUICKSCREEN
463 else if (action == ACTION_STD_QUICKSCREEN)
465 quick_screen_quick(action);
466 redraw_lists = true;
468 #endif
469 #ifdef HAVE_RECORDING
470 else if (action == ACTION_STD_REC)
472 ret = GO_TO_RECSCREEN;
473 done = true;
475 #endif
476 else if (action == ACTION_TREE_WPS)
478 ret = GO_TO_PREVIOUS_MUSIC;
479 done = true;
481 else if (action == ACTION_TREE_STOP)
483 redraw_lists = list_stop_handler();
485 else if (action == ACTION_STD_CONTEXT &&
486 menu == &root_menu_)
488 ret = GO_TO_ROOTITEM_CONTEXT;
489 done = true;
491 else if (action == ACTION_STD_MENU)
493 if (menu != &root_menu_)
494 ret = GO_TO_ROOT;
495 else
496 ret = GO_TO_PREVIOUS;
497 done = true;
499 else if (action == ACTION_STD_CANCEL)
501 bool exiting_menu = false;
502 in_stringlist = false;
503 if (menu_callback)
504 menu_callback(ACTION_EXIT_MENUITEM, menu);
506 if (menu->flags&MENU_EXITAFTERTHISMENU)
507 done = true;
508 else if ((menu->flags&MENU_TYPE_MASK) == MT_MENU)
509 exiting_menu = true;
510 if (stack_top > 0)
512 stack_top--;
513 menu = menu_stack[stack_top];
514 if (!exiting_menu && (menu->flags&MENU_EXITAFTERTHISMENU))
515 done = true;
516 else
517 init_menu_lists(menu, &lists,
518 menu_stack_selected_item[stack_top], false, vps);
519 /* new menu, so reload the callback */
520 get_menu_callback(menu, &menu_callback);
522 else if (menu != &root_menu_)
524 ret = GO_TO_PREVIOUS;
525 done = true;
528 else if (action == ACTION_STD_OK)
530 int type;
531 #ifdef HAVE_BUTTONBAR
532 if (!hide_bars)
534 gui_buttonbar_unset(&buttonbar);
535 gui_buttonbar_draw(&buttonbar);
537 #endif
538 selected = get_menu_selection(gui_synclist_get_sel_pos(&lists), menu);
539 temp = menu->submenus[selected];
540 redraw_lists = true;
541 if (in_stringlist)
542 type = (menu->flags&MENU_TYPE_MASK);
543 else
545 type = (temp->flags&MENU_TYPE_MASK);
546 get_menu_callback(temp, &menu_callback);
547 if (menu_callback)
549 action = menu_callback(ACTION_ENTER_MENUITEM,temp);
550 if (action == ACTION_EXIT_MENUITEM)
551 break;
554 switch (type)
556 case MT_MENU:
557 if (stack_top < MAX_MENUS)
559 menu_stack[stack_top] = menu;
560 menu_stack_selected_item[stack_top] = selected;
561 stack_top++;
562 init_menu_lists(temp, &lists, 0, true, vps);
563 redraw_lists = false; /* above does the redraw */
564 menu = temp;
566 break;
567 case MT_FUNCTION_CALL:
569 int return_value;
570 if (temp->flags&MENU_FUNC_USEPARAM)
571 return_value = temp->function->function_w_param(
572 temp->function->param);
573 else
574 return_value = temp->function->function();
575 if (!(menu->flags&MENU_EXITAFTERTHISMENU) || (temp->flags&MENU_EXITAFTERTHISMENU))
577 init_default_menu_viewports(menu_vp, hide_bars);
578 init_menu_lists(menu, &lists, selected, true, vps);
580 if (temp->flags&MENU_FUNC_CHECK_RETVAL)
582 if (return_value == 1)
584 done = true;
585 ret = return_value;
588 break;
590 case MT_SETTING:
591 case MT_SETTING_W_TEXT:
593 if (do_setting_from_menu(temp, menu_vp))
595 init_default_menu_viewports(menu_vp, hide_bars);
596 init_menu_lists(menu, &lists, selected, true,vps);
597 redraw_lists = false; /* above does the redraw */
599 break;
601 case MT_RETURN_ID:
602 if (in_stringlist)
604 done = true;
605 ret = selected;
607 else if (stack_top < MAX_MENUS)
609 menu_stack[stack_top] = menu;
610 menu_stack_selected_item[stack_top] = selected;
611 stack_top++;
612 menu = temp;
613 init_menu_lists(menu,&lists,0,false, vps);
614 redraw_lists = false; /* above does the redraw */
615 in_stringlist = true;
617 break;
618 case MT_RETURN_VALUE:
619 ret = temp->value;
620 done = true;
621 break;
623 if (type != MT_MENU)
625 if (menu_callback)
626 menu_callback(ACTION_EXIT_MENUITEM,temp);
628 if (current_submenus_menu != menu)
629 init_menu_lists(menu,&lists,selected,true,vps);
630 /* callback was changed, so reload the menu's callback */
631 get_menu_callback(menu, &menu_callback);
632 if ((menu->flags&MENU_EXITAFTERTHISMENU) &&
633 !(temp->flags&MENU_EXITAFTERTHISMENU))
635 done = true;
636 break;
638 #ifdef HAVE_BUTTONBAR
639 if (!hide_bars)
641 gui_buttonbar_set(&buttonbar, "<<<", "", "");
642 gui_buttonbar_draw(&buttonbar);
644 #endif
646 else if(default_event_handler(action) == SYS_USB_CONNECTED)
648 ret = MENU_ATTACHED_USB;
649 done = true;
652 if (redraw_lists && !done)
654 gui_synclist_draw(&lists);
655 gui_synclist_speak_item(&lists);
658 if (start_selected)
660 /* make sure the start_selected variable is set to
661 the selected item from the menu do_menu() was called from */
662 if (stack_top > 0)
664 menu = menu_stack[0];
665 init_menu_lists(menu,&lists,menu_stack_selected_item[0],true, vps);
667 *start_selected = get_menu_selection(
668 gui_synclist_get_sel_pos(&lists), menu);
670 return ret;