Don't use the same completion_event for both directions. This could cause problems...
[kugel-rb.git] / apps / root_menu.c
blobb02d2a1be84a8c04451022b132f334eaad7c333f
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"
42 /* gui api */
43 #include "list.h"
44 #include "splash.h"
45 #include "action.h"
46 #include "yesno.h"
47 #include "viewport.h"
49 #include "tree.h"
50 #if CONFIG_TUNER
51 #include "radio.h"
52 #endif
53 #ifdef HAVE_RECORDING
54 #include "recording.h"
55 #endif
56 #include "wps.h"
57 #include "bookmark.h"
58 #include "playlist.h"
59 #include "menus/exported_menus.h"
60 #ifdef HAVE_RTC_ALARM
61 #include "rtc.h"
62 #endif
63 #ifdef HAVE_TAGCACHE
64 #include "tagcache.h"
65 #endif
66 #include "language.h"
68 struct root_items {
69 int (*function)(void* param);
70 void* param;
71 const struct menu_item_ex *context_menu;
73 static int next_screen = GO_TO_ROOT; /* holding info about the upcoming screen
74 * which is the current screen for the
75 * rest of the code after load_screen
76 * is called */
77 static int last_screen = GO_TO_ROOT; /* unfortunatly needed so we can resume
78 or goto current track based on previous
79 screen */
82 static char current_track_path[MAX_PATH];
83 static void rootmenu_track_changed_callback(void* param)
85 struct mp3entry *id3 = (struct mp3entry *)param;
86 strlcpy(current_track_path, id3->path, MAX_PATH);
88 static int browser(void* param)
90 int ret_val;
91 #ifdef HAVE_TAGCACHE
92 struct tree_context* tc = tree_get_context();
93 #endif
94 int filter = SHOW_SUPPORTED;
95 char folder[MAX_PATH] = "/";
96 /* stuff needed to remember position in file browser */
97 static char last_folder[MAX_PATH] = "/";
98 /* and stuff for the database browser */
99 #ifdef HAVE_TAGCACHE
100 static int last_db_dirlevel = 0, last_db_selection = 0;
101 #endif
103 switch ((intptr_t)param)
105 case GO_TO_FILEBROWSER:
106 filter = global_settings.dirfilter;
107 if (global_settings.browse_current &&
108 last_screen == GO_TO_WPS &&
109 current_track_path[0])
111 strcpy(folder, current_track_path);
113 #ifdef HAVE_HOTSWAP /* quick hack to stop crashing if you try entering
114 the browser from the menu when you were in the card
115 and it was removed */
116 else if (strchr(last_folder, '<') && (card_detect() == false))
117 strcpy(folder, "/");
118 #endif
119 else
120 strcpy(folder, last_folder);
121 break;
122 #ifdef HAVE_TAGCACHE
123 case GO_TO_DBBROWSER:
124 if (!tagcache_is_usable())
126 bool reinit_attempted = false;
128 /* Now display progress until it's ready or the user exits */
129 while(!tagcache_is_usable())
131 struct tagcache_stat *stat = tagcache_get_stat();
133 /* Allow user to exit */
134 if (action_userabort(HZ/2))
135 break;
137 /* Maybe just needs to reboot due to delayed commit */
138 if (stat->commit_delayed)
140 splash(HZ*2, ID2P(LANG_PLEASE_REBOOT));
141 break;
144 /* Check if ready status is known */
145 if (!stat->readyvalid)
147 splash(0, str(LANG_TAGCACHE_BUSY));
148 continue;
151 /* Re-init if required */
152 if (!reinit_attempted && !stat->ready &&
153 stat->processed_entries == 0 && stat->commit_step == 0)
155 /* Prompt the user */
156 reinit_attempted = true;
157 static const char *lines[]={
158 ID2P(LANG_TAGCACHE_BUSY), ID2P(LANG_TAGCACHE_FORCE_UPDATE)};
159 static const struct text_message message={lines, 2};
160 if(gui_syncyesno_run(&message, NULL, NULL) == YESNO_NO)
161 break;
162 int i;
163 FOR_NB_SCREENS(i)
164 screens[i].clear_display();
166 /* Start initialisation */
167 tagcache_rebuild();
170 /* Display building progress */
171 static long talked_tick = 0;
172 if(global_settings.talk_menu &&
173 (talked_tick == 0
174 || TIME_AFTER(current_tick, talked_tick+7*HZ)))
176 talked_tick = current_tick;
177 if (stat->commit_step > 0)
179 talk_id(LANG_TAGCACHE_INIT, false);
180 talk_number(stat->commit_step, true);
181 talk_id(VOICE_OF, true);
182 talk_number(tagcache_get_max_commit_step(), true);
183 } else if(stat->processed_entries)
185 talk_number(stat->processed_entries, false);
186 talk_id(LANG_BUILDING_DATABASE, true);
189 if (stat->commit_step > 0)
191 if (lang_is_rtl())
193 splashf(0, "[%d/%d] %s", stat->commit_step,
194 tagcache_get_max_commit_step(),
195 str(LANG_TAGCACHE_INIT));
197 else
199 splashf(0, "%s [%d/%d]", str(LANG_TAGCACHE_INIT),
200 stat->commit_step,
201 tagcache_get_max_commit_step());
204 else
206 splashf(0, str(LANG_BUILDING_DATABASE),
207 stat->processed_entries);
211 send_event(GUI_EVENT_REFRESH, NULL);
212 if (!tagcache_is_usable())
213 return GO_TO_PREVIOUS;
214 filter = SHOW_ID3DB;
215 tc->dirlevel = last_db_dirlevel;
216 tc->selected_item = last_db_selection;
217 break;
218 #endif
219 case GO_TO_BROWSEPLUGINS:
220 filter = SHOW_PLUGINS;
221 strlcpy(folder, PLUGIN_DIR, MAX_PATH);
222 break;
224 ret_val = rockbox_browse(folder, filter);
225 switch ((intptr_t)param)
227 case GO_TO_FILEBROWSER:
228 get_current_file(last_folder, MAX_PATH);
229 break;
230 #ifdef HAVE_TAGCACHE
231 case GO_TO_DBBROWSER:
232 last_db_dirlevel = tc->dirlevel;
233 last_db_selection = tc->selected_item;
234 break;
235 #endif
237 return ret_val;
240 static int menu(void* param)
242 (void)param;
243 return do_menu(NULL, 0, NULL, false);
246 #ifdef HAVE_RECORDING
247 static int recscrn(void* param)
249 (void)param;
250 recording_screen(false);
251 return GO_TO_ROOT;
253 #endif
254 static int wpsscrn(void* param)
256 int ret_val = GO_TO_PREVIOUS;
257 (void)param;
258 if (audio_status())
260 talk_shutup();
261 ret_val = gui_wps_show();
263 else if ( global_status.resume_index != -1 )
265 DEBUGF("Resume index %X offset %lX\n",
266 global_status.resume_index,
267 (unsigned long)global_status.resume_offset);
268 if (playlist_resume() != -1)
270 playlist_start(global_status.resume_index,
271 global_status.resume_offset);
272 ret_val = gui_wps_show();
275 else
277 splash(HZ*2, ID2P(LANG_NOTHING_TO_RESUME));
279 return ret_val;
281 #if CONFIG_TUNER
282 static int radio(void* param)
284 (void)param;
285 radio_screen();
286 return GO_TO_ROOT;
288 #endif
290 static int load_bmarks(void* param)
292 (void)param;
293 if(bookmark_mrb_load())
294 return GO_TO_WPS;
295 return GO_TO_PREVIOUS;
297 static int plugins_menu(void* param)
299 (void)param;
300 MENUITEM_STRINGLIST(plugins_menu_items, ID2P(LANG_PLUGINS), NULL,
301 ID2P(LANG_PLUGIN_GAMES),
302 ID2P(LANG_PLUGIN_APPS), ID2P(LANG_PLUGIN_DEMOS));
303 char *folder;
304 int retval = GO_TO_PREVIOUS;
305 int selection = 0, current = 0;
306 while (retval == GO_TO_PREVIOUS)
308 selection = do_menu(&plugins_menu_items, &current, NULL, false);
309 switch (selection)
311 case 0:
312 folder = PLUGIN_GAMES_DIR;
313 break;
314 case 1:
315 folder = PLUGIN_APPS_DIR;
316 break;
317 case 2:
318 folder = PLUGIN_DEMOS_DIR;
319 break;
320 default:
321 return selection;
323 retval = rockbox_browse(folder, SHOW_PLUGINS);
325 return retval;
327 int time_screen(void* ignored);
329 /* These are all static const'd from apps/menus/ *.c
330 so little hack so we can use them */
331 extern struct menu_item_ex
332 file_menu,
333 #ifdef HAVE_TAGCACHE
334 tagcache_menu,
335 #endif
336 manage_settings,
337 recording_settings_menu,
338 radio_settings_menu,
339 bookmark_settings_menu,
340 system_menu;
341 static const struct root_items items[] = {
342 [GO_TO_FILEBROWSER] = { browser, (void*)GO_TO_FILEBROWSER, &file_menu},
343 #ifdef HAVE_TAGCACHE
344 [GO_TO_DBBROWSER] = { browser, (void*)GO_TO_DBBROWSER, &tagcache_menu },
345 #endif
346 [GO_TO_WPS] = { wpsscrn, NULL, &playback_settings },
347 [GO_TO_MAINMENU] = { menu, NULL, &manage_settings },
349 #ifdef HAVE_RECORDING
350 [GO_TO_RECSCREEN] = { recscrn, NULL, &recording_settings_menu },
351 #endif
353 #if CONFIG_TUNER
354 [GO_TO_FM] = { radio, NULL, &radio_settings_menu },
355 #endif
357 [GO_TO_RECENTBMARKS] = { load_bmarks, NULL, &bookmark_settings_menu },
358 [GO_TO_BROWSEPLUGINS] = { plugins_menu, NULL, NULL },
361 static const int nb_items = sizeof(items)/sizeof(*items);
363 static int item_callback(int action, const struct menu_item_ex *this_item) ;
365 MENUITEM_RETURNVALUE(file_browser, ID2P(LANG_DIR_BROWSER), GO_TO_FILEBROWSER,
366 NULL, Icon_file_view_menu);
367 #ifdef HAVE_TAGCACHE
368 MENUITEM_RETURNVALUE(db_browser, ID2P(LANG_TAGCACHE), GO_TO_DBBROWSER,
369 NULL, Icon_Audio);
370 #endif
371 MENUITEM_RETURNVALUE(rocks_browser, ID2P(LANG_PLUGINS), GO_TO_BROWSEPLUGINS,
372 NULL, Icon_Plugin);
373 static char *get_wps_item_name(int selected_item, void * data, char *buffer)
375 (void)selected_item; (void)data; (void)buffer;
376 if (audio_status())
377 return ID2P(LANG_NOW_PLAYING);
378 return ID2P(LANG_RESUME_PLAYBACK);
380 MENUITEM_RETURNVALUE_DYNTEXT(wps_item, GO_TO_WPS, NULL, get_wps_item_name,
381 NULL, NULL, Icon_Playback_menu);
382 #ifdef HAVE_RECORDING
383 MENUITEM_RETURNVALUE(rec, ID2P(LANG_RECORDING), GO_TO_RECSCREEN,
384 NULL, Icon_Recording);
385 #endif
386 #if CONFIG_TUNER
387 MENUITEM_RETURNVALUE(fm, ID2P(LANG_FM_RADIO), GO_TO_FM,
388 item_callback, Icon_Radio_screen);
389 #endif
390 MENUITEM_RETURNVALUE(menu_, ID2P(LANG_SETTINGS), GO_TO_MAINMENU,
391 NULL, Icon_Submenu_Entered);
392 MENUITEM_RETURNVALUE(bookmarks, ID2P(LANG_BOOKMARK_MENU_RECENT_BOOKMARKS),
393 GO_TO_RECENTBMARKS, item_callback,
394 Icon_Bookmark);
395 #ifdef HAVE_LCD_CHARCELLS
396 static int do_shutdown(void)
398 #if CONFIG_CHARGING
399 if (charger_inserted())
400 charging_splash();
401 else
402 #endif
403 sys_poweroff();
404 return 0;
406 MENUITEM_FUNCTION(do_shutdown_item, 0, ID2P(LANG_SHUTDOWN),
407 do_shutdown, NULL, NULL, Icon_NOICON);
408 #endif
409 MAKE_MENU(root_menu_, ID2P(LANG_ROCKBOX_TITLE),
410 item_callback, Icon_Rockbox,
411 &bookmarks, &file_browser,
412 #ifdef HAVE_TAGCACHE
413 &db_browser,
414 #endif
415 &wps_item, &menu_,
416 #ifdef HAVE_RECORDING
417 &rec,
418 #endif
419 #if CONFIG_TUNER
420 &fm,
421 #endif
422 &playlist_options, &rocks_browser, &info_menu
424 #ifdef HAVE_LCD_CHARCELLS
425 ,&do_shutdown_item
426 #endif
429 static int item_callback(int action, const struct menu_item_ex *this_item)
431 switch (action)
433 case ACTION_TREE_STOP:
434 return ACTION_REDRAW;
435 case ACTION_REQUEST_MENUITEM:
436 #if CONFIG_TUNER
437 if (this_item == &fm)
439 if (radio_hardware_present() == 0)
440 return ACTION_EXIT_MENUITEM;
442 else
443 #endif
444 if (this_item == &bookmarks)
446 if (global_settings.usemrb == 0)
447 return ACTION_EXIT_MENUITEM;
449 break;
451 return action;
453 static int get_selection(int last_screen)
455 int i;
456 int len = ARRAYLEN(root_menu__);
457 for(i=0; i < len; i++)
459 if (((root_menu__[i]->flags&MENU_TYPE_MASK) == MT_RETURN_VALUE) &&
460 (root_menu__[i]->value == last_screen))
462 return i;
465 return 0;
468 static inline int load_screen(int screen)
470 /* set the global_status.last_screen before entering,
471 if we dont we will always return to the wrong screen on boot */
472 int old_previous = last_screen;
473 int ret_val;
474 if (screen <= GO_TO_ROOT)
475 return screen;
476 if (screen == old_previous)
477 old_previous = GO_TO_ROOT;
478 global_status.last_screen = (char)screen;
479 status_save();
480 ret_val = items[screen].function(items[screen].param);
481 last_screen = screen;
482 if (ret_val == GO_TO_PREVIOUS)
483 last_screen = old_previous;
484 return ret_val;
486 static int load_context_screen(int selection)
488 const struct menu_item_ex *context_menu = NULL;
489 if ((root_menu__[selection]->flags&MENU_TYPE_MASK) == MT_RETURN_VALUE)
491 int item = root_menu__[selection]->value;
492 context_menu = items[item].context_menu;
494 /* special cases */
495 else if (root_menu__[selection] == &info_menu)
497 context_menu = &system_menu;
500 if (context_menu)
501 return do_menu(context_menu, NULL, NULL, false);
502 else
503 return GO_TO_PREVIOUS;
506 static int previous_music = GO_TO_WPS;
508 void previous_music_is_wps(void)
510 previous_music = GO_TO_WPS;
513 int current_screen(void)
515 return next_screen;
518 void root_menu(void)
520 int previous_browser = GO_TO_FILEBROWSER;
521 int selected = 0;
523 if (global_settings.start_in_screen == 0)
524 next_screen = (int)global_status.last_screen;
525 else next_screen = global_settings.start_in_screen - 2;
526 add_event(PLAYBACK_EVENT_TRACK_CHANGE, false, rootmenu_track_changed_callback);
527 #ifdef HAVE_RTC_ALARM
528 if ( rtc_check_alarm_started(true) )
530 rtc_enable_alarm(false);
531 next_screen = GO_TO_WPS;
532 #if CONFIG_TUNER
533 if (global_settings.alarm_wake_up_screen == ALARM_START_FM)
534 next_screen = GO_TO_FM;
535 #endif
536 #ifdef HAVE_RECORDING
537 if (global_settings.alarm_wake_up_screen == ALARM_START_REC)
539 recording_start_automatic = true;
540 next_screen = GO_TO_RECSCREEN;
542 #endif
544 #endif /* HAVE_RTC_ALARM */
546 #ifdef HAVE_HEADPHONE_DETECTION
547 if (next_screen == GO_TO_WPS &&
548 (global_settings.unplug_autoresume && !headphones_inserted() ))
549 next_screen = GO_TO_ROOT;
550 #endif
552 while (true)
554 switch (next_screen)
556 case MENU_ATTACHED_USB:
557 case MENU_SELECTED_EXIT:
558 /* fall through */
559 case GO_TO_ROOT:
560 if (last_screen != GO_TO_ROOT)
561 selected = get_selection(last_screen);
562 next_screen = do_menu(&root_menu_, &selected, NULL, false);
563 if (next_screen != GO_TO_PREVIOUS)
564 last_screen = GO_TO_ROOT;
565 break;
567 case GO_TO_PREVIOUS:
568 next_screen = last_screen;
569 break;
571 case GO_TO_PREVIOUS_BROWSER:
572 next_screen = previous_browser;
573 break;
575 case GO_TO_PREVIOUS_MUSIC:
576 next_screen = previous_music;
577 break;
578 case GO_TO_ROOTITEM_CONTEXT:
579 next_screen = load_context_screen(selected);
580 break;
581 default:
582 if (next_screen == GO_TO_FILEBROWSER
583 #ifdef HAVE_TAGCACHE
584 || next_screen == GO_TO_DBBROWSER
585 #endif
587 previous_browser = next_screen;
588 if (next_screen == GO_TO_WPS
589 #if CONFIG_TUNER
590 || next_screen == GO_TO_FM
591 #endif
593 previous_music = next_screen;
594 next_screen = load_screen(next_screen);
595 break;
596 } /* switch() */