Accept a quick patch from Alexander Levin to neaten up the #defines and comments...
[kugel-rb.git] / apps / root_menu.c
blob8c88375d1a5cf763160cdd5101809701845e90b5
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2007 Jonathan Gordon
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 ****************************************************************************/
21 #include <stdio.h>
22 #include <string.h>
23 #include <stdlib.h>
24 #include <stdbool.h>
25 #include "config.h"
26 #include "appevents.h"
27 #include "menu.h"
28 #include "root_menu.h"
29 #include "lang.h"
30 #include "settings.h"
31 #include "screens.h"
32 #include "kernel.h"
33 #include "debug.h"
34 #include "misc.h"
35 #include "rolo.h"
36 #include "powermgmt.h"
37 #include "power.h"
38 #include "talk.h"
39 #include "audio.h"
40 #include "hotswap.h"
41 #include "backdrop.h"
43 /* gui api */
44 #include "list.h"
45 #include "statusbar.h"
46 #include "splash.h"
47 #include "buttonbar.h"
48 #include "action.h"
49 #include "yesno.h"
50 #include "viewport.h"
52 #include "tree.h"
53 #if CONFIG_TUNER
54 #include "radio.h"
55 #endif
56 #ifdef HAVE_RECORDING
57 #include "recording.h"
58 #endif
59 #include "gwps-common.h"
60 #include "bookmark.h"
61 #include "playlist.h"
62 #include "tagtree.h"
63 #include "menus/exported_menus.h"
64 #ifdef HAVE_RTC_ALARM
65 #include "rtc.h"
66 #endif
67 #ifdef HAVE_TAGCACHE
68 #include "tagcache.h"
69 #endif
71 struct root_items {
72 int (*function)(void* param);
73 void* param;
74 const struct menu_item_ex *context_menu;
76 static int last_screen = GO_TO_ROOT; /* unfortunatly needed so we can resume
77 or goto current track based on previous
78 screen */
79 static char current_track_path[MAX_PATH];
80 static void rootmenu_track_changed_callback(void* param)
82 struct mp3entry *id3 = (struct mp3entry *)param;
83 strncpy(current_track_path, id3->path, MAX_PATH);
85 static int browser(void* param)
87 int ret_val;
88 #ifdef HAVE_TAGCACHE
89 struct tree_context* tc = tree_get_context();
90 #endif
91 int filter = SHOW_SUPPORTED;
92 char folder[MAX_PATH] = "/";
93 /* stuff needed to remember position in file browser */
94 static char last_folder[MAX_PATH] = "/";
95 /* and stuff for the database browser */
96 #ifdef HAVE_TAGCACHE
97 static int last_db_dirlevel = 0, last_db_selection = 0;
98 #endif
100 switch ((intptr_t)param)
102 case GO_TO_FILEBROWSER:
103 filter = global_settings.dirfilter;
104 if (global_settings.browse_current &&
105 last_screen == GO_TO_WPS &&
106 current_track_path[0])
108 strcpy(folder, current_track_path);
110 #ifdef HAVE_HOTSWAP /* quick hack to stop crashing if you try entering
111 the browser from the menu when you were in the card
112 and it was removed */
113 else if (strchr(last_folder, '<') && (card_detect() == false))
114 strcpy(folder, "/");
115 #endif
116 else
117 strcpy(folder, last_folder);
118 break;
119 #ifdef HAVE_TAGCACHE
120 case GO_TO_DBBROWSER:
121 if (!tagcache_is_usable())
123 bool reinit_attempted = false;
125 /* Now display progress until it's ready or the user exits */
126 while(!tagcache_is_usable())
128 struct tagcache_stat *stat = tagcache_get_stat();
130 /* Allow user to exit */
131 if (action_userabort(HZ/2))
132 break;
134 /* Maybe just needs to reboot due to delayed commit */
135 if (stat->commit_delayed)
137 splash(HZ*2, ID2P(LANG_PLEASE_REBOOT));
138 break;
141 /* Check if ready status is known */
142 if (!stat->readyvalid)
144 splash(0, str(LANG_TAGCACHE_BUSY));
145 continue;
148 /* Re-init if required */
149 if (!reinit_attempted && !stat->ready &&
150 stat->processed_entries == 0 && stat->commit_step == 0)
152 /* Prompt the user */
153 reinit_attempted = true;
154 static const char *lines[]={
155 ID2P(LANG_TAGCACHE_BUSY), ID2P(LANG_TAGCACHE_FORCE_UPDATE)};
156 static const struct text_message message={lines, 2};
157 if(gui_syncyesno_run(&message, NULL, NULL) == YESNO_NO)
158 break;
159 int i;
160 FOR_NB_SCREENS(i)
161 screens[i].clear_display();
163 /* Start initialisation */
164 tagcache_rebuild();
167 /* Display building progress */
168 static long talked_tick = 0;
169 if(global_settings.talk_menu &&
170 (talked_tick == 0
171 || TIME_AFTER(current_tick, talked_tick+7*HZ)))
173 talked_tick = current_tick;
174 if (stat->commit_step > 0)
176 talk_id(LANG_TAGCACHE_INIT, false);
177 talk_number(stat->commit_step, true);
178 talk_id(VOICE_OF, true);
179 talk_number(tagcache_get_max_commit_step(), true);
180 } else if(stat->processed_entries)
182 talk_number(stat->processed_entries, false);
183 talk_id(LANG_BUILDING_DATABASE, true);
186 if (stat->commit_step > 0)
188 splashf(0, "%s [%d/%d]",
189 str(LANG_TAGCACHE_INIT), stat->commit_step,
190 tagcache_get_max_commit_step());
192 else
194 splashf(0, str(LANG_BUILDING_DATABASE),
195 stat->processed_entries);
199 if (!tagcache_is_usable())
200 return GO_TO_PREVIOUS;
201 filter = SHOW_ID3DB;
202 tc->dirlevel = last_db_dirlevel;
203 tc->selected_item = last_db_selection;
204 break;
205 #endif
206 case GO_TO_BROWSEPLUGINS:
207 filter = SHOW_PLUGINS;
208 strncpy(folder, PLUGIN_DIR, MAX_PATH);
209 break;
211 ret_val = rockbox_browse(folder, filter);
212 switch ((intptr_t)param)
214 case GO_TO_FILEBROWSER:
215 get_current_file(last_folder, MAX_PATH);
216 break;
217 #ifdef HAVE_TAGCACHE
218 case GO_TO_DBBROWSER:
219 last_db_dirlevel = tc->dirlevel;
220 last_db_selection = tc->selected_item;
221 break;
222 #endif
224 return ret_val;
227 static int menu(void* param)
229 (void)param;
230 return do_menu(NULL, 0, NULL, false);
233 #ifdef HAVE_RECORDING
234 static int recscrn(void* param)
236 (void)param;
237 recording_screen(false);
238 return GO_TO_ROOT;
240 #endif
241 static int wpsscrn(void* param)
243 int ret_val = GO_TO_PREVIOUS;
244 (void)param;
245 if (audio_status())
247 talk_shutup();
248 ret_val = gui_wps_show();
250 else if ( global_status.resume_index != -1 )
252 DEBUGF("Resume index %X offset %lX\n",
253 global_status.resume_index,
254 (unsigned long)global_status.resume_offset);
255 if (playlist_resume() != -1)
257 playlist_start(global_status.resume_index,
258 global_status.resume_offset);
259 ret_val = gui_wps_show();
262 else
264 splash(HZ*2, ID2P(LANG_NOTHING_TO_RESUME));
266 #if LCD_DEPTH > 1
267 show_main_backdrop();
268 #endif
269 #if defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1
270 show_remote_main_backdrop();
271 #endif
272 /* always re-enable the statusbar after the WPS */
273 viewportmanager_set_statusbar(VP_SB_ALLSCREENS);
274 return ret_val;
276 #if CONFIG_TUNER
277 static int radio(void* param)
279 (void)param;
280 radio_screen();
281 return GO_TO_ROOT;
283 #endif
285 static int load_bmarks(void* param)
287 (void)param;
288 if(bookmark_mrb_load())
289 return GO_TO_WPS;
290 return GO_TO_PREVIOUS;
292 static int plugins_menu(void* param)
294 (void)param;
295 MENUITEM_STRINGLIST(plugins_menu_items, ID2P(LANG_PLUGINS), NULL,
296 ID2P(LANG_PLUGIN_GAMES),
297 ID2P(LANG_PLUGIN_APPS), ID2P(LANG_PLUGIN_DEMOS));
298 char *folder;
299 int retval = GO_TO_PREVIOUS;
300 int selection = 0, current = 0;
301 while (retval == GO_TO_PREVIOUS)
303 selection = do_menu(&plugins_menu_items, &current, NULL, false);
304 switch (selection)
306 case 0:
307 folder = PLUGIN_GAMES_DIR;
308 break;
309 case 1:
310 folder = PLUGIN_APPS_DIR;
311 break;
312 case 2:
313 folder = PLUGIN_DEMOS_DIR;
314 break;
315 default:
316 return selection;
318 retval = rockbox_browse(folder, SHOW_PLUGINS);
320 return retval;
322 int time_screen(void* ignored);
324 /* These are all static const'd from apps/menus/ *.c
325 so little hack so we can use them */
326 extern struct menu_item_ex
327 file_menu,
328 #ifdef HAVE_TAGCACHE
329 tagcache_menu,
330 #endif
331 manage_settings,
332 recording_settings_menu,
333 radio_settings_menu,
334 bookmark_settings_menu,
335 system_menu;
336 static const struct root_items items[] = {
337 [GO_TO_FILEBROWSER] = { browser, (void*)GO_TO_FILEBROWSER, &file_menu},
338 #ifdef HAVE_TAGCACHE
339 [GO_TO_DBBROWSER] = { browser, (void*)GO_TO_DBBROWSER, &tagcache_menu },
340 #endif
341 [GO_TO_WPS] = { wpsscrn, NULL, &playback_settings },
342 [GO_TO_MAINMENU] = { menu, NULL, &manage_settings },
344 #ifdef HAVE_RECORDING
345 [GO_TO_RECSCREEN] = { recscrn, NULL, &recording_settings_menu },
346 #endif
348 #if CONFIG_TUNER
349 [GO_TO_FM] = { radio, NULL, &radio_settings_menu },
350 #endif
352 [GO_TO_RECENTBMARKS] = { load_bmarks, NULL, &bookmark_settings_menu },
353 [GO_TO_BROWSEPLUGINS] = { plugins_menu, NULL, NULL },
356 static const int nb_items = sizeof(items)/sizeof(*items);
358 static int item_callback(int action, const struct menu_item_ex *this_item) ;
360 MENUITEM_RETURNVALUE(file_browser, ID2P(LANG_DIR_BROWSER), GO_TO_FILEBROWSER,
361 NULL, Icon_file_view_menu);
362 #ifdef HAVE_TAGCACHE
363 MENUITEM_RETURNVALUE(db_browser, ID2P(LANG_TAGCACHE), GO_TO_DBBROWSER,
364 NULL, Icon_Audio);
365 #endif
366 MENUITEM_RETURNVALUE(rocks_browser, ID2P(LANG_PLUGINS), GO_TO_BROWSEPLUGINS,
367 NULL, Icon_Plugin);
368 static char *get_wps_item_name(int selected_item, void * data, char *buffer)
370 (void)selected_item; (void)data; (void)buffer;
371 if (audio_status())
372 return ID2P(LANG_NOW_PLAYING);
373 return ID2P(LANG_RESUME_PLAYBACK);
375 MENUITEM_RETURNVALUE_DYNTEXT(wps_item, GO_TO_WPS, NULL, get_wps_item_name,
376 NULL, NULL, Icon_Playback_menu);
377 #ifdef HAVE_RECORDING
378 MENUITEM_RETURNVALUE(rec, ID2P(LANG_RECORDING), GO_TO_RECSCREEN,
379 NULL, Icon_Recording);
380 #endif
381 #if CONFIG_TUNER
382 MENUITEM_RETURNVALUE(fm, ID2P(LANG_FM_RADIO), GO_TO_FM,
383 item_callback, Icon_Radio_screen);
384 #endif
385 MENUITEM_RETURNVALUE(menu_, ID2P(LANG_SETTINGS), GO_TO_MAINMENU,
386 NULL, Icon_Submenu_Entered);
387 MENUITEM_RETURNVALUE(bookmarks, ID2P(LANG_BOOKMARK_MENU_RECENT_BOOKMARKS),
388 GO_TO_RECENTBMARKS, item_callback,
389 Icon_Bookmark);
390 #ifdef HAVE_LCD_CHARCELLS
391 static int do_shutdown(void)
393 #if CONFIG_CHARGING
394 if (charger_inserted())
395 charging_splash();
396 else
397 #endif
398 sys_poweroff();
399 return 0;
401 MENUITEM_FUNCTION(do_shutdown_item, 0, ID2P(LANG_SHUTDOWN),
402 do_shutdown, NULL, NULL, Icon_NOICON);
403 #endif
404 MAKE_MENU(root_menu_, ID2P(LANG_ROCKBOX_TITLE),
405 item_callback, Icon_Rockbox,
406 &bookmarks, &file_browser,
407 #ifdef HAVE_TAGCACHE
408 &db_browser,
409 #endif
410 &wps_item, &menu_,
411 #ifdef HAVE_RECORDING
412 &rec,
413 #endif
414 #if CONFIG_TUNER
415 &fm,
416 #endif
417 &playlist_options, &rocks_browser, &info_menu
419 #ifdef HAVE_LCD_CHARCELLS
420 ,&do_shutdown_item
421 #endif
424 static int item_callback(int action, const struct menu_item_ex *this_item)
426 switch (action)
428 case ACTION_TREE_STOP:
429 return ACTION_REDRAW;
430 case ACTION_REQUEST_MENUITEM:
431 #if CONFIG_TUNER
432 if (this_item == &fm)
434 if (radio_hardware_present() == 0)
435 return ACTION_EXIT_MENUITEM;
437 else
438 #endif
439 if (this_item == &bookmarks)
441 if (global_settings.usemrb == 0)
442 return ACTION_EXIT_MENUITEM;
444 break;
446 return action;
448 static int get_selection(int last_screen)
450 unsigned int i;
451 for(i=0; i< sizeof(root_menu__)/sizeof(*root_menu__); i++)
453 if (((root_menu__[i]->flags&MENU_TYPE_MASK) == MT_RETURN_VALUE) &&
454 (root_menu__[i]->value == last_screen))
456 return i;
459 return 0;
462 static inline int load_screen(int screen)
464 /* set the global_status.last_screen before entering,
465 if we dont we will always return to the wrong screen on boot */
466 int old_previous = last_screen;
467 int ret_val;
468 if (screen <= GO_TO_ROOT)
469 return screen;
470 if (screen == old_previous)
471 old_previous = GO_TO_ROOT;
472 global_status.last_screen = (char)screen;
473 status_save();
474 ret_val = items[screen].function(items[screen].param);
475 last_screen = screen;
476 if (ret_val == GO_TO_PREVIOUS)
477 last_screen = old_previous;
478 return ret_val;
480 static int load_context_screen(int selection)
482 const struct menu_item_ex *context_menu = NULL;
483 if ((root_menu__[selection]->flags&MENU_TYPE_MASK) == MT_RETURN_VALUE)
485 int item = root_menu__[selection]->value;
486 context_menu = items[item].context_menu;
488 /* special cases */
489 else if (root_menu__[selection] == &info_menu)
491 context_menu = &system_menu;
494 if (context_menu)
495 return do_menu(context_menu, NULL, NULL, false);
496 else
497 return GO_TO_PREVIOUS;
500 static int previous_music = GO_TO_WPS;
502 void previous_music_is_wps(void)
504 previous_music = GO_TO_WPS;
507 void root_menu(void)
509 int previous_browser = GO_TO_FILEBROWSER;
510 int next_screen = GO_TO_ROOT;
511 int selected = 0;
513 if (global_settings.start_in_screen == 0)
514 next_screen = (int)global_status.last_screen;
515 else next_screen = global_settings.start_in_screen - 2;
516 add_event(PLAYBACK_EVENT_TRACK_CHANGE, false, rootmenu_track_changed_callback);
517 #ifdef HAVE_RTC_ALARM
518 if ( rtc_check_alarm_started(true) )
520 rtc_enable_alarm(false);
521 next_screen = GO_TO_WPS;
522 #if CONFIG_TUNER
523 if (global_settings.alarm_wake_up_screen == ALARM_START_FM)
524 next_screen = GO_TO_FM;
525 #endif
526 #ifdef HAVE_RECORDING
527 if (global_settings.alarm_wake_up_screen == ALARM_START_REC)
529 recording_start_automatic = true;
530 next_screen = GO_TO_RECSCREEN;
532 #endif
534 #endif /* HAVE_RTC_ALARM */
536 #ifdef HAVE_HEADPHONE_DETECTION
537 if (next_screen == GO_TO_WPS &&
538 (global_settings.unplug_autoresume && !headphones_inserted() ))
539 next_screen = GO_TO_ROOT;
540 #endif
542 while (true)
544 switch (next_screen)
546 case MENU_ATTACHED_USB:
547 case MENU_SELECTED_EXIT:
548 /* fall through */
549 case GO_TO_ROOT:
550 if (last_screen != GO_TO_ROOT)
551 selected = get_selection(last_screen);
552 next_screen = do_menu(&root_menu_, &selected, NULL, false);
553 if (next_screen != GO_TO_PREVIOUS)
554 last_screen = GO_TO_ROOT;
555 break;
557 case GO_TO_PREVIOUS:
558 next_screen = last_screen;
559 break;
561 case GO_TO_PREVIOUS_BROWSER:
562 next_screen = previous_browser;
563 break;
565 case GO_TO_PREVIOUS_MUSIC:
566 next_screen = previous_music;
567 break;
568 case GO_TO_ROOTITEM_CONTEXT:
569 next_screen = load_context_screen(selected);
570 break;
571 default:
572 if (next_screen == GO_TO_FILEBROWSER
573 #ifdef HAVE_TAGCACHE
574 || next_screen == GO_TO_DBBROWSER
575 #endif
577 previous_browser = next_screen;
578 if (next_screen == GO_TO_WPS
579 #if CONFIG_TUNER
580 || next_screen == GO_TO_FM
581 #endif
583 previous_music = next_screen;
584 next_screen = load_screen(next_screen);
585 break;
586 } /* switch() */
588 return;