blob: c55c39fc5c0264b961fa54490a19a54d6b88c5a4 [file] [log] [blame]
Daniel Stenberg1c0c8612002-05-17 12:22:24 +00001/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2002 Robert E. Hak
11 *
Daniel Stenberg2acc0ac2008-06-28 18:10:04 +000012 * 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.
Daniel Stenberg1c0c8612002-05-17 12:22:24 +000016 *
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
19 *
20 ****************************************************************************/
Kevin Ferraredb8415c2005-10-30 22:34:51 +000021/*
222005 Kevin Ferrare :
23 - Multi screen support
24 - Rewrote/removed a lot of code now useless with the new gui API
25*/
Björn Stenberg2ac05722002-05-26 17:03:17 +000026#include <stdbool.h>
Linus Nielsen Feltzing10b92c42004-03-12 10:20:33 +000027#include <stdlib.h>
Jonathan Gordon91cb68a2007-03-01 11:14:46 +000028#include "config.h"
Linus Nielsen Feltzing34d08b22007-11-16 08:18:08 +000029#include "system.h"
Justin Heinerb5025a82002-08-31 04:58:35 +000030
Daniel Stenberg1c0c8612002-05-17 12:22:24 +000031#include "lcd.h"
Daniel Stenberg93b231c2002-09-12 13:33:59 +000032#include "font.h"
Justin Heinerb5025a82002-08-31 04:58:35 +000033#include "backlight.h"
Daniel Stenberg1c0c8612002-05-17 12:22:24 +000034#include "menu.h"
Björn Stenbergb21a3bd2002-05-21 14:25:45 +000035#include "button.h"
36#include "kernel.h"
37#include "debug.h"
Justin Heiner26302452002-08-23 02:17:00 +000038#include "usb.h"
Björn Stenbergcd225732002-08-11 09:17:47 +000039#include "panic.h"
Markus Braun5e4c1d22002-08-20 19:37:00 +000040#include "settings.h"
Jonathan Gordon4718a1e2007-02-08 04:33:41 +000041#include "settings_list.h"
Jonathan Gordona5278fa2007-07-11 05:41:23 +000042#include "option_select.h"
Markus Braun5e4c1d22002-08-20 19:37:00 +000043#include "status.h"
Björn Stenberga4c3b032002-09-24 18:04:15 +000044#include "screens.h"
Jörg Hohensohn4f36ea82004-03-14 21:33:53 +000045#include "talk.h"
Jörg Hohensohnb1403ee2004-07-23 23:01:20 +000046#include "lang.h"
Linus Nielsen Feltzingade5d7b2004-07-26 16:06:59 +000047#include "misc.h"
Linus Nielsen Feltzing224c0a12006-08-15 12:27:07 +000048#include "action.h"
Jonathan Gordon4718a1e2007-02-08 04:33:41 +000049#include "menus/exported_menus.h"
Jonathan Gordonfe3cfcc2007-02-08 10:28:42 +000050#include "string.h"
Jonathan Gordon91cb68a2007-03-01 11:14:46 +000051#include "root_menu.h"
52#include "bookmark.h"
53#include "gwps-common.h" /* for fade() */
54#include "audio.h"
Jonathan Gordon5ca15392008-03-26 03:35:24 +000055#include "viewport.h"
Jonathan Gordonea664e02008-05-03 12:30:40 +000056#include "quickscreen.h"
Markus Braun000c2db2002-08-30 13:49:32 +000057
Daniel Stenbergbf603f92002-06-14 08:47:44 +000058#ifdef HAVE_LCD_BITMAP
59#include "icons.h"
60#endif
Markus Braun000c2db2002-08-30 13:49:32 +000061
Kevin Ferraredb8415c2005-10-30 22:34:51 +000062/* gui api */
63#include "list.h"
64#include "statusbar.h"
65#include "buttonbar.h"
Jonathan Gordon4718a1e2007-02-08 04:33:41 +000066
Jonathan Gordon77a458a2007-05-08 11:55:43 +000067#define MAX_MENUS 8
Jonathan Gordon4718a1e2007-02-08 04:33:41 +000068/* used to allow for dynamic menus */
69#define MAX_MENU_SUBITEMS 64
Jonathan Gordon208136d2007-03-17 14:10:51 +000070static struct menu_item_ex *current_submenus_menu;
Jonathan Gordon4718a1e2007-02-08 04:33:41 +000071static int current_subitems[MAX_MENU_SUBITEMS];
72static int current_subitems_count = 0;
Stéphane Doyon4e09b782007-10-21 00:23:23 +000073static int talk_menu_item(int selected_item, void *data);
Jonathan Gordon4718a1e2007-02-08 04:33:41 +000074
Nils Wallméniusf31e1232007-05-19 19:54:11 +000075static void get_menu_callback(const struct menu_item_ex *m,
Jonathan Gordonfe3cfcc2007-02-08 10:28:42 +000076 menu_callback_type *menu_callback)
Jonathan Gordon4718a1e2007-02-08 04:33:41 +000077{
Jonathan Gordon2801a872007-02-19 02:14:51 +000078 if (m->flags&(MENU_HAS_DESC|MENU_DYNAMIC_DESC))
Jonathan Gordonfe3cfcc2007-02-08 10:28:42 +000079 *menu_callback= m->callback_and_desc->menu_callback;
80 else
81 *menu_callback = m->menu_callback;
Jonathan Gordon4718a1e2007-02-08 04:33:41 +000082}
83
84static int get_menu_selection(int selected_item, const struct menu_item_ex *menu)
85{
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;
90}
Jonathan Gordon91cb68a2007-03-01 11:14:46 +000091static int find_menu_selection(int selected)
92{
93 int i;
94 for (i=0; i< current_subitems_count; i++)
95 if (current_subitems[i] == selected)
96 return i;
97 return 0;
98}
Nils Wallménius68489612008-04-09 15:25:17 +000099static char * get_menu_item_name(int selected_item,
100 void * data,
101 char *buffer,
102 size_t buffer_len)
Jonathan Gordon4718a1e2007-02-08 04:33:41 +0000103{
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);
107
Nils Wallménius68489612008-04-09 15:25:17 +0000108 (void)buffer_len;
Jonathan Gordon4718a1e2007-02-08 04:33:41 +0000109 /* only MT_MENU or MT_RETURN_ID is allowed in here */
110 if (type == MT_RETURN_ID)
111 {
Jonathan Gordon2816dc92007-02-27 09:06:30 +0000112 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);
Jonathan Gordon4718a1e2007-02-08 04:33:41 +0000115 return (char*)menu->strings[selected_item];
116 }
117
118 menu = menu->submenus[selected_item];
Jonathan Gordon1ba493e2007-02-26 08:17:07 +0000119
Jonathan Gordon97090862007-03-03 14:23:03 +0000120 if ((menu->flags&MENU_DYNAMIC_DESC) && (type != MT_SETTING_W_TEXT))
Jonathan Gordon1ba493e2007-02-26 08:17:07 +0000121 return menu->menu_get_name_and_icon->list_get_name(selected_item,
122 menu->menu_get_name_and_icon->list_get_name_data, buffer);
123
Jonathan Gordon4718a1e2007-02-08 04:33:41 +0000124 type = (menu->flags&MENU_TYPE_MASK);
Jonathan Gordon97090862007-03-03 14:23:03 +0000125 if ((type == MT_SETTING) || (type == MT_SETTING_W_TEXT))
Jonathan Gordon4718a1e2007-02-08 04:33:41 +0000126 {
127 const struct settings_list *v
Jonathan Gordonfe3cfcc2007-02-08 10:28:42 +0000128 = find_setting(menu->variable, NULL);
Jonathan Gordon4718a1e2007-02-08 04:33:41 +0000129 if (v)
130 return str(v->lang_id);
131 else return "Not Done yet!";
132 }
133 return P2STR(menu->callback_and_desc->desc);
134}
Jonathan Gordon5599d682007-02-14 06:58:30 +0000135#ifdef HAVE_LCD_BITMAP
Jonathan Gordon6a5cc0b2007-04-16 09:14:36 +0000136static int menu_get_icon(int selected_item, void * data)
Jonathan Gordon5599d682007-02-14 06:58:30 +0000137{
138 const struct menu_item_ex *menu = (const struct menu_item_ex *)data;
Jonathan Gordon97090862007-03-03 14:23:03 +0000139 int menu_icon = Icon_NOICON;
Jonathan Gordon5599d682007-02-14 06:58:30 +0000140 selected_item = get_menu_selection(selected_item, menu);
141
Jonathan Gordon1f5ee2a2007-03-25 14:31:56 +0000142 if ((menu->flags&MENU_TYPE_MASK) == MT_RETURN_ID)
143 {
Jonathan Gordon6a5cc0b2007-04-16 09:14:36 +0000144 return Icon_Menu_functioncall;
Jonathan Gordon1f5ee2a2007-03-25 14:31:56 +0000145 }
Jonathan Gordon5599d682007-02-14 06:58:30 +0000146 menu = menu->submenus[selected_item];
Jonathan Gordon1ba493e2007-02-26 08:17:07 +0000147 if (menu->flags&MENU_HAS_DESC)
Jonathan Gordon02a87172007-03-03 13:52:14 +0000148 menu_icon = menu->callback_and_desc->icon_id;
Jonathan Gordon1ba493e2007-02-26 08:17:07 +0000149 else if (menu->flags&MENU_DYNAMIC_DESC)
Jonathan Gordon02a87172007-03-03 13:52:14 +0000150 menu_icon = menu->menu_get_name_and_icon->icon_id;
Jonathan Gordon1ba493e2007-02-26 08:17:07 +0000151
Jonathan Gordon6a5cc0b2007-04-16 09:14:36 +0000152 if (menu_icon == Icon_NOICON)
Jonathan Gordon5599d682007-02-14 06:58:30 +0000153 {
Jonathan Gordon6a5cc0b2007-04-16 09:14:36 +0000154 switch (menu->flags&MENU_TYPE_MASK)
155 {
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;
167 }
Jonathan Gordon5599d682007-02-14 06:58:30 +0000168 }
Jonathan Gordon6a5cc0b2007-04-16 09:14:36 +0000169 return menu_icon;
Jonathan Gordon5599d682007-02-14 06:58:30 +0000170}
171#endif
Jonathan Gordon4718a1e2007-02-08 04:33:41 +0000172
173static void init_menu_lists(const struct menu_item_ex *menu,
Jonathan Gordon5ca15392008-03-26 03:35:24 +0000174 struct gui_synclist *lists, int selected, bool callback,
175 struct viewport parent[NB_SCREENS])
Jonathan Gordon4718a1e2007-02-08 04:33:41 +0000176{
Jonathan Gordonb5e587c2007-03-18 06:31:33 +0000177 int i, count = MENU_GET_COUNT(menu->flags);
Jonathan Gordon1f5ee2a2007-03-25 14:31:56 +0000178 int type = (menu->flags&MENU_TYPE_MASK);
Jonathan Gordon4718a1e2007-02-08 04:33:41 +0000179 menu_callback_type menu_callback = NULL;
Jonathan Gordon6a5cc0b2007-04-16 09:14:36 +0000180 int icon;
Jonathan Gordon4718a1e2007-02-08 04:33:41 +0000181 current_subitems_count = 0;
Jonathan Gordon34521462007-03-07 13:00:46 +0000182
Jonathan Gordon1f5ee2a2007-03-25 14:31:56 +0000183 if (type == MT_RETURN_ID)
184 get_menu_callback(menu, &menu_callback);
185
Jonathan Gordon4718a1e2007-02-08 04:33:41 +0000186 for (i=0; i<count; i++)
187 {
Jonathan Gordon1f5ee2a2007-03-25 14:31:56 +0000188 if (type != MT_RETURN_ID)
189 get_menu_callback(menu->submenus[i],&menu_callback);
Jonathan Gordon4718a1e2007-02-08 04:33:41 +0000190 if (menu_callback)
191 {
Jonathan Gordon1f5ee2a2007-03-25 14:31:56 +0000192 if (menu_callback(ACTION_REQUEST_MENUITEM,
193 type==MT_RETURN_ID ? (void*)(intptr_t)i: menu->submenus[i])
Jonathan Gordon4718a1e2007-02-08 04:33:41 +0000194 != ACTION_EXIT_MENUITEM)
195 {
196 current_subitems[current_subitems_count] = i;
197 current_subitems_count++;
198 }
199 }
200 else
201 {
202 current_subitems[current_subitems_count] = i;
203 current_subitems_count++;
204 }
205 }
Jonathan Gordon3d39c4d2007-03-17 14:16:11 +0000206 current_submenus_menu = (struct menu_item_ex *)menu;
Jonathan Gordon208136d2007-03-17 14:10:51 +0000207
Jonathan Gordon5ca15392008-03-26 03:35:24 +0000208 gui_synclist_init(lists,get_menu_item_name,(void*)menu,false,1, parent);
Jonathan Gordon5599d682007-02-14 06:58:30 +0000209#ifdef HAVE_LCD_BITMAP
Jonathan Gordon02a87172007-03-03 13:52:14 +0000210 if (menu->callback_and_desc->icon_id == Icon_NOICON)
Jonathan Gordon6a5cc0b2007-04-16 09:14:36 +0000211 icon = Icon_Submenu_Entered;
Jonathan Gordon5599d682007-02-14 06:58:30 +0000212 else
Jonathan Gordon6a5cc0b2007-04-16 09:14:36 +0000213 icon = menu->callback_and_desc->icon_id;
Jonathan Gordonf8e08f02007-02-15 23:27:57 +0000214 gui_synclist_set_title(lists, P2STR(menu->callback_and_desc->desc), icon);
Steve Bavin361a60c2007-03-01 18:25:13 +0000215 gui_synclist_set_icon_callback(lists, menu_get_icon);
Jonathan Gordonf8e08f02007-02-15 23:27:57 +0000216#else
217 (void)icon;
Steve Bavin361a60c2007-03-01 18:25:13 +0000218 gui_synclist_set_icon_callback(lists, NULL);
Jonathan Gordon5599d682007-02-14 06:58:30 +0000219#endif
Stéphane Doyon4e09b782007-10-21 00:23:23 +0000220 if(global_settings.talk_menu)
221 gui_synclist_set_voice_callback(lists, talk_menu_item);
Jonathan Gordon4718a1e2007-02-08 04:33:41 +0000222 gui_synclist_set_nb_items(lists,current_subitems_count);
223 gui_synclist_limit_scroll(lists,true);
Jonathan Gordon91cb68a2007-03-01 11:14:46 +0000224 gui_synclist_select_item(lists, find_menu_selection(selected));
Jonathan Gordon4718a1e2007-02-08 04:33:41 +0000225
Jonathan Gordonfe3cfcc2007-02-08 10:28:42 +0000226 get_menu_callback(menu,&menu_callback);
Jonathan Gordon4718a1e2007-02-08 04:33:41 +0000227 if (callback && menu_callback)
228 menu_callback(ACTION_ENTER_MENUITEM,menu);
Jonathan Gordonbaf9cf62007-05-07 08:50:47 +0000229 gui_synclist_draw(lists);
Stéphane Doyon4e09b782007-10-21 00:23:23 +0000230 gui_synclist_speak_item(lists);
Jonathan Gordon4718a1e2007-02-08 04:33:41 +0000231}
232
Stéphane Doyon4e09b782007-10-21 00:23:23 +0000233static int talk_menu_item(int selected_item, void *data)
Jonathan Gordon4718a1e2007-02-08 04:33:41 +0000234{
Stéphane Doyon4e09b782007-10-21 00:23:23 +0000235 const struct menu_item_ex *menu = (const struct menu_item_ex *)data;
Jonathan Gordon4718a1e2007-02-08 04:33:41 +0000236 int id = -1;
Jonathan Gordon97090862007-03-03 14:23:03 +0000237 int type;
Linus Nielsen Feltzing298c2bb2007-03-05 00:32:33 +0000238 unsigned char *str;
Stéphane Doyon4e09b782007-10-21 00:23:23 +0000239 int sel = get_menu_selection(selected_item, menu);
Linus Nielsen Feltzing298c2bb2007-03-05 00:32:33 +0000240
Jonathan Gordon4718a1e2007-02-08 04:33:41 +0000241 if ((menu->flags&MENU_TYPE_MASK) == MT_MENU)
242 {
Jonathan Gordon97090862007-03-03 14:23:03 +0000243 type = menu->submenus[sel]->flags&MENU_TYPE_MASK;
244 if ((type == MT_SETTING) || (type == MT_SETTING_W_TEXT))
Jonathan Gordon91cb68a2007-03-01 11:14:46 +0000245 talk_setting(menu->submenus[sel]->variable);
246 else
247 {
Linus Nielsen Feltzing298c2bb2007-03-05 00:32:33 +0000248 if (menu->submenus[sel]->flags&(MENU_DYNAMIC_DESC))
249 {
Stéphane Doyon5acf0912007-10-09 03:48:56 +0000250 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
258 {
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);
265 }
Linus Nielsen Feltzing298c2bb2007-03-05 00:32:33 +0000266 }
267 else
268 id = P2ID(menu->submenus[sel]->callback_and_desc->desc);
Jonathan Gordon91cb68a2007-03-01 11:14:46 +0000269 if (id != -1)
Jonathan Gordon4718a1e2007-02-08 04:33:41 +0000270 talk_id(id,false);
Jonathan Gordon91cb68a2007-03-01 11:14:46 +0000271 }
Jonathan Gordon4718a1e2007-02-08 04:33:41 +0000272 }
Jonathan Gordon94b98042007-08-20 09:11:05 +0000273 else if(((menu->flags&MENU_TYPE_MASK) == MT_RETURN_ID))
274 {
275 if ((menu->flags&MENU_DYNAMIC_DESC) == 0)
276 {
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);
282 }
283 }
Stéphane Doyon4e09b782007-10-21 00:23:23 +0000284 return 0;
Jonathan Gordon4718a1e2007-02-08 04:33:41 +0000285}
Jonathan Gordon5ca15392008-03-26 03:35:24 +0000286/* 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 */
Bertrik Sikkene15f8a22008-05-03 08:35:14 +0000288static void init_default_menu_viewports(struct viewport parent[NB_SCREENS], bool hide_bars)
Jonathan Gordon5ca15392008-03-26 03:35:24 +0000289{
290 int i;
291 FOR_NB_SCREENS(i)
292 {
293 viewport_set_defaults(&parent[i], i);
294 /* viewport_set_defaults() fixes the vp for the bars, so resize */
295 if (hide_bars)
296 {
297 if (global_settings.statusbar)
298 {
299 parent[i].y -= STATUSBAR_HEIGHT;
300 parent[i].height += STATUSBAR_HEIGHT;
301 }
302 }
303 }
Peter D'Hoyef76122f2008-05-29 20:32:39 +0000304#ifdef HAVE_BUTTONBAR
Jonathan Gordonbe185a12008-03-26 09:05:42 +0000305 if (!hide_bars && global_settings.buttonbar)
Jonathan Gordon5ca15392008-03-26 03:35:24 +0000306 parent[0].height -= BUTTONBAR_HEIGHT;
307#endif
308}
Steve Bavincd88e2a2008-03-25 15:24:03 +0000309
Jonathan Gordonfe9dca32008-04-23 11:07:40 +0000310bool do_setting_from_menu(const struct menu_item_ex *temp,
311 struct viewport parent[NB_SCREENS])
Jonathan Gordon2801a872007-02-19 02:14:51 +0000312{
Jonathan Gordonbe185a12008-03-26 09:05:42 +0000313 int setting_id, oldval;
Jonathan Gordon1ba493e2007-02-26 08:17:07 +0000314 const struct settings_list *setting = find_setting(
315 temp->variable,
316 &setting_id);
Jonathan Gordonfe2b3762007-07-23 05:40:45 +0000317 char *title;
Jonathan Gordon91ccc012007-11-05 13:15:35 +0000318 char padded_title[MAX_PATH];
Jonathan Gordonbe185a12008-03-26 09:05:42 +0000319 int var_type = setting->flags&F_T_MASK;
320 if (var_type == F_T_INT || var_type == F_T_UINT)
321 {
322 oldval = *(int*)setting->setting;
323 }
324 else if (var_type == F_T_BOOL)
325 {
326 oldval = *(bool*)setting->setting;
327 }
328 else
329 oldval = 0;
Jonathan Gordonfe2b3762007-07-23 05:40:45 +0000330 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);
Jonathan Gordon91ccc012007-11-05 13:15:35 +0000334
Linus Nielsen Feltzing34d08b22007-11-16 08:18:08 +0000335 /* 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 */
Jonathan Gordon91ccc012007-11-05 13:15:35 +0000338 if (setting->flags&F_PADTITLE)
339 {
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);
Linus Nielsen Feltzing34d08b22007-11-16 08:18:08 +0000346 while (i < MAX_PATH-1)
Jonathan Gordon91ccc012007-11-05 13:15:35 +0000347 {
Linus Nielsen Feltzing34d08b22007-11-16 08:18:08 +0000348 int padlen = MIN(len, MAX_PATH-1-i);
349 strncpy(&padded_title[i], title, padlen);
350 i += padlen;
Jonathan Gordon91ccc012007-11-05 13:15:35 +0000351 if (i<MAX_PATH-1)
352 padded_title[i++] = ' ';
353 }
354 padded_title[i] = '\0';
355 title = padded_title;
356 }
357
Jonathan Gordonfe9dca32008-04-23 11:07:40 +0000358 option_screen((struct settings_list *)setting, parent,
Jonathan Gordonfe2b3762007-07-23 05:40:45 +0000359 setting->flags&F_TEMPVAR, title);
Jonathan Gordonbe185a12008-03-26 09:05:42 +0000360 if (var_type == F_T_INT || var_type == F_T_UINT)
361 {
362 return oldval != *(int*)setting->setting;
363 }
364 else if (var_type == F_T_BOOL)
365 {
366 return oldval != *(bool*)setting->setting;
367 }
Jonathan Gordona5278fa2007-07-11 05:41:23 +0000368 return false;
Jonathan Gordon2801a872007-02-19 02:14:51 +0000369}
370
Jonathan Gordon62decc42007-11-04 12:10:41 +0000371/* display a menu */
Jonathan Gordon5ca15392008-03-26 03:35:24 +0000372int do_menu(const struct menu_item_ex *start_menu, int *start_selected,
373 struct viewport parent[NB_SCREENS], bool hide_bars)
Jonathan Gordon4718a1e2007-02-08 04:33:41 +0000374{
Jonathan Gordon91cb68a2007-03-01 11:14:46 +0000375 int selected = start_selected? *start_selected : 0;
Jonathan Gordon4718a1e2007-02-08 04:33:41 +0000376 int action;
Jonathan Gordon4718a1e2007-02-08 04:33:41 +0000377 struct gui_synclist lists;
378 const struct menu_item_ex *temp, *menu;
Jonathan Gordon5ca15392008-03-26 03:35:24 +0000379 int ret = 0, i;
Jonathan Gordon411f3e22007-07-11 02:22:03 +0000380 bool redraw_lists;
Jonathan Gordon5ca15392008-03-26 03:35:24 +0000381
Jonathan Gordon4718a1e2007-02-08 04:33:41 +0000382 const struct menu_item_ex *menu_stack[MAX_MENUS];
383 int menu_stack_selected_item[MAX_MENUS];
384 int stack_top = 0;
Jonathan Gordon43b2d092007-03-07 10:13:07 +0000385 bool in_stringlist, done = false;
Jonathan Gordon5ca15392008-03-26 03:35:24 +0000386
387 struct viewport *vps, menu_vp[NB_SCREENS]; /* menu_vp will hopefully be phased out */
Peter D'Hoyef76122f2008-05-29 20:32:39 +0000388#ifdef HAVE_BUTTONBAR
Jonathan Gordon5ca15392008-03-26 03:35:24 +0000389 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
394
Jonathan Gordon4718a1e2007-02-08 04:33:41 +0000395 menu_callback_type menu_callback = NULL;
396 if (start_menu == NULL)
397 menu = &main_menu_;
398 else menu = start_menu;
Jonathan Gordon5ca15392008-03-26 03:35:24 +0000399
400 if (parent)
401 {
402 vps = parent;
403 /* if hide_bars == true we assume the viewport is correctly sized */
404 }
405 else
406 {
407 vps = menu_vp;
408 init_default_menu_viewports(vps, hide_bars);
409 }
410 FOR_NB_SCREENS(i)
411 {
412 screens[i].set_viewport(&vps[i]);
413 screens[i].clear_viewport();
414 screens[i].set_viewport(NULL);
415 }
416 init_menu_lists(menu, &lists, selected, true, vps);
Jonathan Gordon4718a1e2007-02-08 04:33:41 +0000417 in_stringlist = ((menu->flags&MENU_TYPE_MASK) == MT_RETURN_ID);
418
Jonathan Gordond563cb22007-02-21 12:22:07 +0000419 /* load the callback, and only reload it if menu changes */
420 get_menu_callback(menu, &menu_callback);
421
Jonathan Gordon5ca15392008-03-26 03:35:24 +0000422
Peter D'Hoyef76122f2008-05-29 20:32:39 +0000423#ifdef HAVE_BUTTONBAR
Jonathan Gordon5ca15392008-03-26 03:35:24 +0000424 if (!hide_bars)
425 {
426 gui_buttonbar_set(&buttonbar, "<<<", "", "");
427 gui_buttonbar_draw(&buttonbar);
428 }
429#endif
Jonathan Gordon43b2d092007-03-07 10:13:07 +0000430 while (!done)
Jonathan Gordon4718a1e2007-02-08 04:33:41 +0000431 {
Jonathan Gordon411f3e22007-07-11 02:22:03 +0000432 redraw_lists = false;
Jonathan Gordon5ca15392008-03-26 03:35:24 +0000433 if (!hide_bars)
434 {
435 gui_syncstatusbar_draw(&statusbars, true);
436 }
Stéphane Doyon4e09b782007-10-21 00:23:23 +0000437 action = get_action(CONTEXT_MAINMENU,
438 list_do_action_timeout(&lists, HZ));
Jonathan Gordon4718a1e2007-02-08 04:33:41 +0000439 /* HZ so the status bar redraws corectly */
Jonathan Gordond563cb22007-02-21 12:22:07 +0000440
Stéphane Doyon4e09b782007-10-21 00:23:23 +0000441 if (action != ACTION_NONE && menu_callback)
Jonathan Gordon4718a1e2007-02-08 04:33:41 +0000442 {
Jonathan Gordon9144be52007-02-27 09:16:47 +0000443 int old_action = action;
Jonathan Gordond563cb22007-02-21 12:22:07 +0000444 action = menu_callback(action, menu);
Jonathan Gordon9144be52007-02-27 09:16:47 +0000445 if (action == ACTION_EXIT_AFTER_THIS_MENUITEM)
446 {
447 action = old_action;
448 ret = MENU_SELECTED_EXIT; /* will exit after returning
449 from selection */
450 }
Jonathan Gordon411f3e22007-07-11 02:22:03 +0000451 else if (action == ACTION_REDRAW)
452 {
453 action = old_action;
454 redraw_lists = true;
455 }
Jonathan Gordon4718a1e2007-02-08 04:33:41 +0000456 }
457
Jonathan Gordoncf1cef52007-09-17 10:08:50 +0000458 if (gui_synclist_do_button(&lists, &action, LIST_WRAP_UNLESS_HELD))
Jonathan Gordonb9f22af2007-07-11 01:23:57 +0000459 continue;
Stéphane Doyon4e09b782007-10-21 00:23:23 +0000460 if (action == ACTION_NONE)
461 continue;
Jonathan Gordon286d48f2008-04-23 10:33:46 +0000462#ifdef HAVE_QUICKSCREEN
463 else if (action == ACTION_STD_QUICKSCREEN)
464 {
465 quick_screen_quick(action);
466 redraw_lists = true;
467 }
468#endif
Jonathan Gordon73336e92007-08-02 12:13:20 +0000469#ifdef HAVE_RECORDING
Jonathan Gordon286d48f2008-04-23 10:33:46 +0000470 else if (action == ACTION_STD_REC)
Jonathan Gordon73336e92007-08-02 12:13:20 +0000471 {
472 ret = GO_TO_RECSCREEN;
473 done = true;
474 }
Jonathan Gordon73336e92007-08-02 12:13:20 +0000475#endif
Jonathan Gordon286d48f2008-04-23 10:33:46 +0000476 else if (action == ACTION_TREE_WPS)
Jonathan Gordon4718a1e2007-02-08 04:33:41 +0000477 {
Jonathan Gordon91cb68a2007-03-01 11:14:46 +0000478 ret = GO_TO_PREVIOUS_MUSIC;
Jonathan Gordon43b2d092007-03-07 10:13:07 +0000479 done = true;
Jonathan Gordon4718a1e2007-02-08 04:33:41 +0000480 }
Jonathan Gordona053e582007-04-08 01:33:01 +0000481 else if (action == ACTION_TREE_STOP)
Jonathan Gordon91cb68a2007-03-01 11:14:46 +0000482 {
Magnus Holmgren1d9f6932007-08-05 15:21:47 +0000483 redraw_lists = list_stop_handler();
Jonathan Gordon91cb68a2007-03-01 11:14:46 +0000484 }
Jonathan Gordonf9fb4922007-04-12 15:28:51 +0000485 else if (action == ACTION_STD_CONTEXT &&
486 menu == &root_menu_)
487 {
488 ret = GO_TO_ROOTITEM_CONTEXT;
489 done = true;
490 }
Jonathan Gordon91cb68a2007-03-01 11:14:46 +0000491 else if (action == ACTION_STD_MENU)
492 {
493 if (menu != &root_menu_)
494 ret = GO_TO_ROOT;
Jonathan Gordona74eefe2007-03-06 13:35:15 +0000495 else
496 ret = GO_TO_PREVIOUS;
Jonathan Gordon43b2d092007-03-07 10:13:07 +0000497 done = true;
Jonathan Gordon91cb68a2007-03-01 11:14:46 +0000498 }
499 else if (action == ACTION_STD_CANCEL)
Jonathan Gordon4718a1e2007-02-08 04:33:41 +0000500 {
Jonathan Gordon5cab5da2007-11-07 10:06:21 +0000501 bool exiting_menu = false;
Linus Nielsen Feltzing6ffd8042007-02-16 10:40:59 +0000502 in_stringlist = false;
Jonathan Gordond563cb22007-02-21 12:22:07 +0000503 if (menu_callback)
504 menu_callback(ACTION_EXIT_MENUITEM, menu);
Linus Nielsen Feltzing6ffd8042007-02-16 10:40:59 +0000505
Jonathan Gordon3a7760c2007-04-30 13:41:33 +0000506 if (menu->flags&MENU_EXITAFTERTHISMENU)
507 done = true;
Jonathan Gordon5cab5da2007-11-07 10:06:21 +0000508 else if ((menu->flags&MENU_TYPE_MASK) == MT_MENU)
509 exiting_menu = true;
Jonathan Gordon4718a1e2007-02-08 04:33:41 +0000510 if (stack_top > 0)
511 {
Jonathan Gordon4718a1e2007-02-08 04:33:41 +0000512 stack_top--;
513 menu = menu_stack[stack_top];
Jonathan Gordon5cab5da2007-11-07 10:06:21 +0000514 if (!exiting_menu && (menu->flags&MENU_EXITAFTERTHISMENU))
Jonathan Gordon3a7760c2007-04-30 13:41:33 +0000515 done = true;
Jonathan Gordon75e2f192007-08-04 09:47:17 +0000516 else
517 init_menu_lists(menu, &lists,
Jonathan Gordon5ca15392008-03-26 03:35:24 +0000518 menu_stack_selected_item[stack_top], false, vps);
Jonathan Gordond563cb22007-02-21 12:22:07 +0000519 /* new menu, so reload the callback */
Jonathan Gordon1ea4f0d2007-02-16 00:55:52 +0000520 get_menu_callback(menu, &menu_callback);
Jonathan Gordon4718a1e2007-02-08 04:33:41 +0000521 }
Jonathan Gordon91cb68a2007-03-01 11:14:46 +0000522 else if (menu != &root_menu_)
523 {
524 ret = GO_TO_PREVIOUS;
Jonathan Gordon43b2d092007-03-07 10:13:07 +0000525 done = true;
Jonathan Gordon91cb68a2007-03-01 11:14:46 +0000526 }
Jonathan Gordon4718a1e2007-02-08 04:33:41 +0000527 }
528 else if (action == ACTION_STD_OK)
529 {
530 int type;
Peter D'Hoyef76122f2008-05-29 20:32:39 +0000531#ifdef HAVE_BUTTONBAR
Jonathan Gordon5ca15392008-03-26 03:35:24 +0000532 if (!hide_bars)
533 {
534 gui_buttonbar_unset(&buttonbar);
535 gui_buttonbar_draw(&buttonbar);
536 }
Jonathan Gordon1ba493e2007-02-26 08:17:07 +0000537#endif
Jonathan Gordon4718a1e2007-02-08 04:33:41 +0000538 selected = get_menu_selection(gui_synclist_get_sel_pos(&lists), menu);
539 temp = menu->submenus[selected];
Jonathan Gordon50dc0ca2007-07-11 04:17:43 +0000540 redraw_lists = true;
Jonathan Gordon4718a1e2007-02-08 04:33:41 +0000541 if (in_stringlist)
542 type = (menu->flags&MENU_TYPE_MASK);
Jonathan Gordonfe3cfcc2007-02-08 10:28:42 +0000543 else
Jonathan Gordon4718a1e2007-02-08 04:33:41 +0000544 {
Jonathan Gordon1f5ee2a2007-03-25 14:31:56 +0000545 type = (temp->flags&MENU_TYPE_MASK);
546 get_menu_callback(temp, &menu_callback);
547 if (menu_callback)
548 {
549 action = menu_callback(ACTION_ENTER_MENUITEM,temp);
550 if (action == ACTION_EXIT_MENUITEM)
551 break;
552 }
Jonathan Gordon4718a1e2007-02-08 04:33:41 +0000553 }
554 switch (type)
555 {
556 case MT_MENU:
557 if (stack_top < MAX_MENUS)
558 {
559 menu_stack[stack_top] = menu;
Jonathan Gordon91cb68a2007-03-01 11:14:46 +0000560 menu_stack_selected_item[stack_top] = selected;
Jonathan Gordon4718a1e2007-02-08 04:33:41 +0000561 stack_top++;
Jonathan Gordon5ca15392008-03-26 03:35:24 +0000562 init_menu_lists(temp, &lists, 0, true, vps);
Jonathan Gordon50dc0ca2007-07-11 04:17:43 +0000563 redraw_lists = false; /* above does the redraw */
Jonathan Gordon4718a1e2007-02-08 04:33:41 +0000564 menu = temp;
Jonathan Gordon4718a1e2007-02-08 04:33:41 +0000565 }
566 break;
567 case MT_FUNCTION_CALL:
Jonathan Gordondaf66942007-03-17 12:33:34 +0000568 {
569 int return_value;
Jonathan Gordondaf66942007-03-17 12:33:34 +0000570 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();
Jonathan Gordon6d207952008-04-30 10:22:39 +0000575 if (!(menu->flags&MENU_EXITAFTERTHISMENU) || (temp->flags&MENU_EXITAFTERTHISMENU))
576 {
577 init_default_menu_viewports(menu_vp, hide_bars);
578 init_menu_lists(menu, &lists, selected, true, vps);
579 }
Jonathan Gordondaf66942007-03-17 12:33:34 +0000580 if (temp->flags&MENU_FUNC_CHECK_RETVAL)
581 {
Jonathan Gordone54d8e12007-03-18 07:03:43 +0000582 if (return_value == 1)
Jonathan Gordonb5e587c2007-03-18 06:31:33 +0000583 {
584 done = true;
585 ret = return_value;
586 }
Jonathan Gordondaf66942007-03-17 12:33:34 +0000587 }
Jonathan Gordon4718a1e2007-02-08 04:33:41 +0000588 break;
Jonathan Gordondaf66942007-03-17 12:33:34 +0000589 }
Jonathan Gordon4718a1e2007-02-08 04:33:41 +0000590 case MT_SETTING:
Jonathan Gordon97090862007-03-03 14:23:03 +0000591 case MT_SETTING_W_TEXT:
Jonathan Gordon4718a1e2007-02-08 04:33:41 +0000592 {
Jonathan Gordonfe9dca32008-04-23 11:07:40 +0000593 if (do_setting_from_menu(temp, menu_vp))
Jonathan Gordon50dc0ca2007-07-11 04:17:43 +0000594 {
Jonathan Gordon5ca15392008-03-26 03:35:24 +0000595 init_default_menu_viewports(menu_vp, hide_bars);
596 init_menu_lists(menu, &lists, selected, true,vps);
Jonathan Gordon50dc0ca2007-07-11 04:17:43 +0000597 redraw_lists = false; /* above does the redraw */
598 }
Jonathan Gordon4718a1e2007-02-08 04:33:41 +0000599 break;
600 }
601 case MT_RETURN_ID:
602 if (in_stringlist)
603 {
Jonathan Gordonb5e587c2007-03-18 06:31:33 +0000604 done = true;
605 ret = selected;
Jonathan Gordon4718a1e2007-02-08 04:33:41 +0000606 }
607 else if (stack_top < MAX_MENUS)
608 {
609 menu_stack[stack_top] = menu;
610 menu_stack_selected_item[stack_top] = selected;
611 stack_top++;
612 menu = temp;
Jonathan Gordon5ca15392008-03-26 03:35:24 +0000613 init_menu_lists(menu,&lists,0,false, vps);
Jonathan Gordon50dc0ca2007-07-11 04:17:43 +0000614 redraw_lists = false; /* above does the redraw */
Jonathan Gordon4718a1e2007-02-08 04:33:41 +0000615 in_stringlist = true;
616 }
617 break;
Jonathan Gordon91cb68a2007-03-01 11:14:46 +0000618 case MT_RETURN_VALUE:
Jonathan Gordon43b2d092007-03-07 10:13:07 +0000619 ret = temp->value;
620 done = true;
621 break;
Jonathan Gordon4718a1e2007-02-08 04:33:41 +0000622 }
Jonathan Gordon3a7760c2007-04-30 13:41:33 +0000623 if (type != MT_MENU)
624 {
625 if (menu_callback)
626 menu_callback(ACTION_EXIT_MENUITEM,temp);
627 }
Jonathan Gordon208136d2007-03-17 14:10:51 +0000628 if (current_submenus_menu != menu)
Jonathan Gordon5ca15392008-03-26 03:35:24 +0000629 init_menu_lists(menu,&lists,selected,true,vps);
Jonathan Gordond563cb22007-02-21 12:22:07 +0000630 /* callback was changed, so reload the menu's callback */
631 get_menu_callback(menu, &menu_callback);
Jonathan Gordon3a7760c2007-04-30 13:41:33 +0000632 if ((menu->flags&MENU_EXITAFTERTHISMENU) &&
633 !(temp->flags&MENU_EXITAFTERTHISMENU))
634 {
635 done = true;
636 break;
637 }
Peter D'Hoyef76122f2008-05-29 20:32:39 +0000638#ifdef HAVE_BUTTONBAR
Jonathan Gordon5ca15392008-03-26 03:35:24 +0000639 if (!hide_bars)
640 {
641 gui_buttonbar_set(&buttonbar, "<<<", "", "");
642 gui_buttonbar_draw(&buttonbar);
643 }
Jonathan Gordon1ba493e2007-02-26 08:17:07 +0000644#endif
Jonathan Gordon4718a1e2007-02-08 04:33:41 +0000645 }
646 else if(default_event_handler(action) == SYS_USB_CONNECTED)
Peter D'Hoyed2b30552007-03-30 21:54:48 +0000647 {
Jonathan Gordon4718a1e2007-02-08 04:33:41 +0000648 ret = MENU_ATTACHED_USB;
Peter D'Hoyed2b30552007-03-30 21:54:48 +0000649 done = true;
650 }
Jonathan Gordon411f3e22007-07-11 02:22:03 +0000651
Stéphane Doyon4e09b782007-10-21 00:23:23 +0000652 if (redraw_lists && !done)
653 {
Jonathan Gordon411f3e22007-07-11 02:22:03 +0000654 gui_synclist_draw(&lists);
Stéphane Doyon4e09b782007-10-21 00:23:23 +0000655 gui_synclist_speak_item(&lists);
656 }
Jonathan Gordon4718a1e2007-02-08 04:33:41 +0000657 }
Jonathan Gordon91cb68a2007-03-01 11:14:46 +0000658 if (start_selected)
Jonathan Gordonfd9d1402007-05-20 14:24:02 +0000659 {
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)
663 {
664 menu = menu_stack[0];
Jonathan Gordon5ca15392008-03-26 03:35:24 +0000665 init_menu_lists(menu,&lists,menu_stack_selected_item[0],true, vps);
Jonathan Gordonfd9d1402007-05-20 14:24:02 +0000666 }
Jonathan Gordon43b2d092007-03-07 10:13:07 +0000667 *start_selected = get_menu_selection(
668 gui_synclist_get_sel_pos(&lists), menu);
Jonathan Gordonfd9d1402007-05-20 14:24:02 +0000669 }
Jonathan Gordon4718a1e2007-02-08 04:33:41 +0000670 return ret;
671}
672