FS#6948 - Fix the broken 'follow playlist' feature
[Rockbox.git] / apps / root_menu.c
blobc9abd662db09a915e974a69aa2fdc515a827aa8f
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2007 Jonathan Gordon
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 ****************************************************************************/
19 #include <stdio.h>
20 #include <string.h>
21 #include <stdlib.h>
22 #include <stdbool.h>
23 #include "config.h"
24 #include "menu.h"
25 #include "root_menu.h"
26 #include "lang.h"
27 #include "settings.h"
28 #include "kernel.h"
29 #include "debug.h"
30 #include "misc.h"
31 #include "rolo.h"
32 #include "powermgmt.h"
34 #if LCD_DEPTH > 1
35 #include "backdrop.h"
36 #endif
37 #include "talk.h"
38 #include "audio.h"
40 /* gui api */
41 #include "list.h"
42 #include "statusbar.h"
43 #include "splash.h"
44 #include "buttonbar.h"
45 #include "textarea.h"
46 #include "action.h"
47 #include "yesno.h"
49 #include "main_menu.h"
50 #include "tree.h"
51 #if CONFIG_TUNER
52 #include "radio.h"
53 #endif
54 #ifdef HAVE_RECORDING
55 #include "recording.h"
56 #endif
57 #include "gwps-common.h"
58 #include "bookmark.h"
59 #include "tagtree.h"
60 #include "menus/exported_menus.h"
61 #ifdef HAVE_RTC_ALARM
62 #include "rtc.h"
63 #endif
64 #ifdef HAVE_TAGCACHE
65 #include "tagcache.h"
66 #endif
68 struct root_items {
69 int (*function)(void* param);
70 void* param;
72 static int last_screen = GO_TO_ROOT; /* unfortunatly needed so we can resume
73 or goto current track based on previous
74 screen */
75 static int browser(void* param)
77 int ret_val;
78 #ifdef HAVE_TAGCACHE
79 struct tree_context* tc = tree_get_context();
80 #endif
81 int filter = SHOW_SUPPORTED;
82 char folder[MAX_PATH] = "/";
83 /* stuff needed to remember position in file browser */
84 static char last_folder[MAX_PATH] = "/";
85 /* and stuff for the database browser */
86 #ifdef HAVE_TAGCACHE
87 static int last_db_dirlevel = 0, last_db_selection = 0;
88 #endif
90 switch ((intptr_t)param)
92 case GO_TO_FILEBROWSER:
93 filter = global_settings.dirfilter;
94 if (global_settings.browse_current &&
95 last_screen == GO_TO_WPS && audio_status() &&
96 wps_state.current_track_path[0] != '\0')
98 strcpy(folder, wps_state.current_track_path);
100 else
101 strcpy(folder, last_folder);
102 break;
103 #ifdef HAVE_TAGCACHE
104 case GO_TO_DBBROWSER:
105 if (!tagcache_is_usable())
107 bool reinit_attempted = false;
109 /* Now display progress until it's ready or the user exits */
110 while(!tagcache_is_usable())
112 gui_syncstatusbar_draw(&statusbars, false);
113 struct tagcache_stat *stat = tagcache_get_stat();
115 /* Allow user to exit */
116 if (action_userabort(HZ/2))
117 break;
119 /* Maybe just needs to reboot due to delayed commit */
120 if (stat->commit_delayed)
122 gui_syncsplash(HZ*2, str(LANG_PLEASE_REBOOT));
123 break;
126 /* Check if ready status is known */
127 if (!stat->readyvalid)
129 gui_syncsplash(0, str(LANG_TAGCACHE_BUSY));
130 continue;
133 /* Re-init if required */
134 if (!reinit_attempted && !stat->ready &&
135 stat->processed_entries == 0 && stat->commit_step == 0)
137 /* Prompt the user */
138 reinit_attempted = true;
139 char *lines[]={str(LANG_TAGCACHE_BUSY), str(LANG_TAGCACHE_FORCE_UPDATE)};
140 struct text_message message={lines, 2};
141 if(gui_syncyesno_run(&message, NULL, NULL) == YESNO_NO)
142 break;
143 int i;
144 FOR_NB_SCREENS(i)
145 screens[i].clear_display();
147 /* Start initialisation */
148 tagcache_rebuild();
151 /* Display building progress */
152 if (stat->commit_step > 0)
154 gui_syncsplash(0, "%s [%d/%d]",
155 str(LANG_TAGCACHE_INIT), stat->commit_step,
156 tagcache_get_max_commit_step());
158 else
160 gui_syncsplash(0, str(LANG_BUILDING_DATABASE),
161 stat->processed_entries);
165 if (!tagcache_is_usable())
166 return GO_TO_PREVIOUS;
167 filter = SHOW_ID3DB;
168 tc->dirlevel = last_db_dirlevel;
169 tc->selected_item = last_db_selection;
170 break;
171 #endif
172 case GO_TO_BROWSEPLUGINS:
173 filter = SHOW_PLUGINS;
174 snprintf(folder, MAX_PATH, "%s/", PLUGIN_DIR);
175 break;
177 ret_val = rockbox_browse(folder, filter);
178 switch ((intptr_t)param)
180 case GO_TO_FILEBROWSER:
181 get_current_file(last_folder, MAX_PATH);
182 break;
183 #ifdef HAVE_TAGCACHE
184 case GO_TO_DBBROWSER:
185 last_db_dirlevel = tc->dirlevel;
186 last_db_selection = tc->selected_item;
187 break;
188 #endif
190 return ret_val;
193 static int menu(void* param)
195 (void)param;
196 return do_menu(NULL, 0);
199 #ifdef HAVE_RECORDING
200 static int recscrn(void* param)
202 (void)param;
203 recording_screen(false);
204 return GO_TO_ROOT;
206 #endif
207 static int wpsscrn(void* param)
209 int ret_val = GO_TO_PREVIOUS;
210 (void)param;
211 if (audio_status())
213 ret_val = gui_wps_show();
215 else if ( global_status.resume_index != -1 )
217 DEBUGF("Resume index %X offset %lX\n",
218 global_status.resume_index,
219 (unsigned long)global_status.resume_offset);
220 if (playlist_resume() != -1)
222 playlist_start(global_status.resume_index,
223 global_status.resume_offset);
224 ret_val = gui_wps_show();
227 else
229 gui_syncsplash(HZ*2, str(LANG_NOTHING_TO_RESUME));
231 #if LCD_DEPTH > 1
232 show_main_backdrop();
233 #endif
234 return ret_val;
236 #if CONFIG_TUNER
237 static int radio(void* param)
239 (void)param;
240 radio_screen();
241 return GO_TO_ROOT;
243 #endif
245 static int load_bmarks(void* param)
247 (void)param;
248 bookmark_mrb_load();
249 return GO_TO_PREVIOUS;
252 static const struct root_items items[] = {
253 [GO_TO_FILEBROWSER] = { browser, (void*)GO_TO_FILEBROWSER },
254 [GO_TO_DBBROWSER] = { browser, (void*)GO_TO_DBBROWSER },
255 [GO_TO_WPS] = { wpsscrn, NULL },
256 [GO_TO_MAINMENU] = { menu, NULL },
258 #ifdef HAVE_RECORDING
259 [GO_TO_RECSCREEN] = { recscrn, NULL },
260 #endif
262 #if CONFIG_TUNER
263 [GO_TO_FM] = { radio, NULL },
264 #endif
266 [GO_TO_RECENTBMARKS] = { load_bmarks, NULL },
267 [GO_TO_BROWSEPLUGINS] = { browser, (void*)GO_TO_BROWSEPLUGINS },
270 static const int nb_items = sizeof(items)/sizeof(*items);
272 int item_callback(int action, const struct menu_item_ex *this_item) ;
274 MENUITEM_RETURNVALUE(file_browser, ID2P(LANG_DIR_BROWSER), GO_TO_FILEBROWSER,
275 NULL, Icon_file_view_menu);
276 #ifdef HAVE_TAGCACHE
277 MENUITEM_RETURNVALUE(db_browser, ID2P(LANG_TAGCACHE), GO_TO_DBBROWSER,
278 NULL, Icon_Audio);
279 #endif
280 MENUITEM_RETURNVALUE(rocks_browser, ID2P(LANG_PLUGINS), GO_TO_BROWSEPLUGINS,
281 NULL, Icon_Plugin);
282 char *get_wps_item_name(int selected_item, void * data, char *buffer)
284 (void)selected_item; (void)data; (void)buffer;
285 if (audio_status())
286 return ID2P(LANG_NOW_PLAYING);
287 return ID2P(LANG_RESUME_PLAYBACK);
289 MENUITEM_RETURNVALUE_DYNTEXT(wps_item, GO_TO_WPS, NULL, get_wps_item_name,
290 NULL, Icon_Playback_menu);
291 #ifdef HAVE_RECORDING
292 MENUITEM_RETURNVALUE(rec, ID2P(LANG_RECORDING_MENU), GO_TO_RECSCREEN,
293 NULL, Icon_Recording);
294 #endif
295 #if CONFIG_TUNER
296 MENUITEM_RETURNVALUE(fm, ID2P(LANG_FM_RADIO), GO_TO_FM,
297 item_callback, Icon_Radio_screen);
298 #endif
299 MENUITEM_RETURNVALUE(menu_, ID2P(LANG_SETTINGS_MENU), GO_TO_MAINMENU,
300 NULL, Icon_Submenu_Entered);
301 MENUITEM_RETURNVALUE(bookmarks, ID2P(LANG_BOOKMARK_MENU_RECENT_BOOKMARKS),
302 GO_TO_RECENTBMARKS, item_callback,
303 Icon_Bookmark);
304 #ifdef HAVE_LCD_CHARCELLS
305 static int do_shutdown(void)
307 sys_poweroff();
308 return 0;
310 MENUITEM_FUNCTION(do_shutdown_item, 0, ID2P(LANG_SHUTDOWN),
311 do_shutdown, NULL, NULL, Icon_NOICON);
312 #endif
313 MAKE_MENU(root_menu_, ID2P(LANG_ROCKBOX_TITLE),
314 NULL, Icon_Rockbox,
315 &bookmarks, &file_browser,
316 #ifdef HAVE_TAGCACHE
317 &db_browser,
318 #endif
319 &wps_item, &menu_,
320 #ifdef HAVE_RECORDING
321 &rec,
322 #endif
323 #if CONFIG_TUNER
324 &fm,
325 #endif
326 &playlist_options, &rocks_browser, &info_menu
328 #ifdef HAVE_LCD_CHARCELLS
329 ,&do_shutdown_item
330 #endif
333 int item_callback(int action, const struct menu_item_ex *this_item)
335 switch (action)
337 case ACTION_REQUEST_MENUITEM:
338 #if CONFIG_TUNER
339 if (this_item == &fm)
341 if (radio_hardware_present() == 0)
342 return ACTION_EXIT_MENUITEM;
344 else
345 #endif
346 if (this_item == &bookmarks)
348 if (global_settings.usemrb == 0)
349 return ACTION_EXIT_MENUITEM;
351 break;
353 return action;
355 static int get_selection(int last_screen)
357 unsigned int i;
358 for(i=0; i< sizeof(root_menu__)/sizeof(*root_menu__); i++)
360 if ((root_menu__[i]->flags&MT_RETURN_VALUE) &&
361 (root_menu__[i]->value == last_screen))
363 return i;
366 return 0;
369 static inline int load_screen(int screen)
371 /* set the global_status.last_screen before entering,
372 if we dont we will always return to the wrong screen on boot */
373 int old_previous = last_screen;
374 int ret_val;
375 if (screen <= GO_TO_ROOT)
376 return screen;
377 if (screen == old_previous)
378 old_previous = GO_TO_ROOT;
379 global_status.last_screen = (char)screen;
380 status_save();
381 action_signalscreenchange();
382 ret_val = items[screen].function(items[screen].param);
383 last_screen = screen;
384 if (ret_val == GO_TO_PREVIOUS)
385 last_screen = old_previous;
386 return ret_val;
389 void root_menu(void)
391 int previous_browser = GO_TO_FILEBROWSER;
392 int previous_music = GO_TO_WPS;
393 int next_screen = GO_TO_ROOT;
394 int selected = 0;
396 if (global_settings.start_in_screen == 0)
397 next_screen = (int)global_status.last_screen;
398 else next_screen = global_settings.start_in_screen - 2;
400 #ifdef HAVE_RTC_ALARM
401 if ( rtc_check_alarm_started(true) )
403 rtc_enable_alarm(false);
404 next_screen = GO_TO_WPS;
405 #if CONFIG_TUNER
406 if (global_settings.alarm_wake_up_screen == ALARM_START_FM)
407 next_screen = GO_TO_FM;
408 #endif
409 #ifdef HAVE_RECORDING
410 if (global_settings.alarm_wake_up_screen == ALARM_START_REC)
412 recording_start_automatic = true;
413 next_screen = GO_TO_RECSCREEN;
415 #endif
417 #endif /* HAVE_RTC_ALARM */
419 #ifdef HAVE_HEADPHONE_DETECTION
420 if (next_screen == GO_TO_WPS &&
421 (global_settings.unplug_autoresume && !headphones_inserted() ))
422 next_screen = GO_TO_ROOT;
423 #endif
425 while (true)
427 switch (next_screen)
429 case MENU_ATTACHED_USB:
430 case MENU_SELECTED_EXIT:
431 /* fall through */
432 case GO_TO_ROOT:
433 if (last_screen != GO_TO_ROOT)
434 selected = get_selection(last_screen);
435 next_screen = do_menu(&root_menu_, &selected);
436 if (next_screen != GO_TO_PREVIOUS)
437 last_screen = GO_TO_ROOT;
438 break;
440 case GO_TO_PREVIOUS:
441 next_screen = last_screen;
442 break;
444 case GO_TO_PREVIOUS_BROWSER:
445 next_screen = previous_browser;
446 break;
448 case GO_TO_PREVIOUS_MUSIC:
449 next_screen = previous_music;
450 break;
452 default:
453 if (next_screen == GO_TO_FILEBROWSER
454 #ifdef HAVE_TAGCACHE
455 || next_screen == GO_TO_DBBROWSER
456 #endif
458 previous_browser = next_screen;
459 if (next_screen == GO_TO_WPS
460 #ifdef CONFIG_TUNER
461 || next_screen == GO_TO_FM
462 #endif
464 previous_music = next_screen;
465 next_screen = load_screen(next_screen);
466 break;
467 } /* switch() */
469 return;