Change i2c config on e200. Seems to speed things up somewhat.
[kugel-rb.git] / apps / root_menu.c
blob5f16a0ecb83ddb92c2f103a28b53179fc1161f48
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 "screens.h"
29 #include "kernel.h"
30 #include "debug.h"
31 #include "misc.h"
32 #include "rolo.h"
33 #include "powermgmt.h"
34 #include "power.h"
35 #include "talk.h"
36 #include "audio.h"
38 #if (LCD_DEPTH > 1) || (defined(HAVE_LCD_REMOTE) && (LCD_REMOTE_DEPTH > 1))
39 #include "backdrop.h"
40 #endif
43 /* gui api */
44 #include "list.h"
45 #include "statusbar.h"
46 #include "splash.h"
47 #include "buttonbar.h"
48 #include "textarea.h"
49 #include "action.h"
50 #include "yesno.h"
52 #include "main_menu.h"
53 #include "tree.h"
54 #if CONFIG_TUNER
55 #include "radio.h"
56 #endif
57 #ifdef HAVE_RECORDING
58 #include "recording.h"
59 #endif
60 #include "gwps-common.h"
61 #include "bookmark.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 int browser(void* param)
81 int ret_val;
82 #ifdef HAVE_TAGCACHE
83 struct tree_context* tc = tree_get_context();
84 #endif
85 int filter = SHOW_SUPPORTED;
86 char folder[MAX_PATH] = "/";
87 /* stuff needed to remember position in file browser */
88 static char last_folder[MAX_PATH] = "/";
89 /* and stuff for the database browser */
90 #ifdef HAVE_TAGCACHE
91 static int last_db_dirlevel = 0, last_db_selection = 0;
92 #endif
94 switch ((intptr_t)param)
96 case GO_TO_FILEBROWSER:
97 filter = global_settings.dirfilter;
98 if (global_settings.browse_current &&
99 last_screen == GO_TO_WPS && audio_status() &&
100 wps_state.current_track_path[0] != '\0')
102 strcpy(folder, wps_state.current_track_path);
104 else
105 strcpy(folder, last_folder);
106 break;
107 #ifdef HAVE_TAGCACHE
108 case GO_TO_DBBROWSER:
109 if (!tagcache_is_usable())
111 bool reinit_attempted = false;
113 /* Now display progress until it's ready or the user exits */
114 while(!tagcache_is_usable())
116 gui_syncstatusbar_draw(&statusbars, false);
117 struct tagcache_stat *stat = tagcache_get_stat();
119 /* Allow user to exit */
120 if (action_userabort(HZ/2))
121 break;
123 /* Maybe just needs to reboot due to delayed commit */
124 if (stat->commit_delayed)
126 gui_syncsplash(HZ*2, str(LANG_PLEASE_REBOOT));
127 break;
130 /* Check if ready status is known */
131 if (!stat->readyvalid)
133 gui_syncsplash(0, str(LANG_TAGCACHE_BUSY));
134 continue;
137 /* Re-init if required */
138 if (!reinit_attempted && !stat->ready &&
139 stat->processed_entries == 0 && stat->commit_step == 0)
141 /* Prompt the user */
142 reinit_attempted = true;
143 char *lines[]={str(LANG_TAGCACHE_BUSY), str(LANG_TAGCACHE_FORCE_UPDATE)};
144 struct text_message message={lines, 2};
145 if(gui_syncyesno_run(&message, NULL, NULL) == YESNO_NO)
146 break;
147 int i;
148 FOR_NB_SCREENS(i)
149 screens[i].clear_display();
151 /* Start initialisation */
152 tagcache_rebuild();
155 /* Display building progress */
156 if (stat->commit_step > 0)
158 gui_syncsplash(0, "%s [%d/%d]",
159 str(LANG_TAGCACHE_INIT), stat->commit_step,
160 tagcache_get_max_commit_step());
162 else
164 gui_syncsplash(0, str(LANG_BUILDING_DATABASE),
165 stat->processed_entries);
169 if (!tagcache_is_usable())
170 return GO_TO_PREVIOUS;
171 filter = SHOW_ID3DB;
172 tc->dirlevel = last_db_dirlevel;
173 tc->selected_item = last_db_selection;
174 break;
175 #endif
176 case GO_TO_BROWSEPLUGINS:
177 filter = SHOW_PLUGINS;
178 snprintf(folder, MAX_PATH, "%s/", PLUGIN_DIR);
179 break;
181 ret_val = rockbox_browse(folder, filter);
182 switch ((intptr_t)param)
184 case GO_TO_FILEBROWSER:
185 get_current_file(last_folder, MAX_PATH);
186 break;
187 #ifdef HAVE_TAGCACHE
188 case GO_TO_DBBROWSER:
189 last_db_dirlevel = tc->dirlevel;
190 last_db_selection = tc->selected_item;
191 break;
192 #endif
194 return ret_val;
197 static int menu(void* param)
199 (void)param;
200 return do_menu(NULL, 0);
203 #ifdef HAVE_RECORDING
204 static int recscrn(void* param)
206 (void)param;
207 recording_screen(false);
208 return GO_TO_ROOT;
210 #endif
211 static int wpsscrn(void* param)
213 int ret_val = GO_TO_PREVIOUS;
214 (void)param;
215 if (audio_status())
217 ret_val = gui_wps_show();
219 else if ( global_status.resume_index != -1 )
221 DEBUGF("Resume index %X offset %lX\n",
222 global_status.resume_index,
223 (unsigned long)global_status.resume_offset);
224 if (playlist_resume() != -1)
226 playlist_start(global_status.resume_index,
227 global_status.resume_offset);
228 ret_val = gui_wps_show();
231 else
233 gui_syncsplash(HZ*2, str(LANG_NOTHING_TO_RESUME));
235 #if LCD_DEPTH > 1
236 show_main_backdrop();
237 #endif
238 #if defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1
239 show_remote_main_backdrop();
240 #endif
241 return ret_val;
243 #if CONFIG_TUNER
244 static int radio(void* param)
246 (void)param;
247 radio_screen();
248 return GO_TO_ROOT;
250 #endif
252 static int load_bmarks(void* param)
254 (void)param;
255 bookmark_mrb_load();
256 return GO_TO_PREVIOUS;
258 /* These are all static const'd from apps/menus/ *.c
259 so little hack so we can use them */
260 extern struct menu_item_ex
261 file_menu,
262 #ifdef HAVE_TAGCACHE
263 tagcache_menu,
264 #endif
265 manage_settings,
266 recording_setting_menu,
267 bookmark_settings_menu,
268 system_menu;
269 static const struct root_items items[] = {
270 [GO_TO_FILEBROWSER] = { browser, (void*)GO_TO_FILEBROWSER, &file_menu},
271 #ifdef HAVE_TAGCACHE
272 [GO_TO_DBBROWSER] = { browser, (void*)GO_TO_DBBROWSER, &tagcache_menu },
273 #endif
274 [GO_TO_WPS] = { wpsscrn, NULL, &playback_menu_item },
275 [GO_TO_MAINMENU] = { menu, NULL, &manage_settings },
277 #ifdef HAVE_RECORDING
278 [GO_TO_RECSCREEN] = { recscrn, NULL, &recording_setting_menu },
279 #endif
281 #if CONFIG_TUNER
282 [GO_TO_FM] = { radio, NULL, NULL },
283 #endif
285 [GO_TO_RECENTBMARKS] = { load_bmarks, NULL, &bookmark_settings_menu },
286 [GO_TO_BROWSEPLUGINS] = { browser, (void*)GO_TO_BROWSEPLUGINS, NULL },
289 static const int nb_items = sizeof(items)/sizeof(*items);
291 int item_callback(int action, const struct menu_item_ex *this_item) ;
293 MENUITEM_RETURNVALUE(file_browser, ID2P(LANG_DIR_BROWSER), GO_TO_FILEBROWSER,
294 NULL, Icon_file_view_menu);
295 #ifdef HAVE_TAGCACHE
296 MENUITEM_RETURNVALUE(db_browser, ID2P(LANG_TAGCACHE), GO_TO_DBBROWSER,
297 NULL, Icon_Audio);
298 #endif
299 MENUITEM_RETURNVALUE(rocks_browser, ID2P(LANG_PLUGINS), GO_TO_BROWSEPLUGINS,
300 NULL, Icon_Plugin);
301 char *get_wps_item_name(int selected_item, void * data, char *buffer)
303 (void)selected_item; (void)data; (void)buffer;
304 if (audio_status())
305 return ID2P(LANG_NOW_PLAYING);
306 return ID2P(LANG_RESUME_PLAYBACK);
308 MENUITEM_RETURNVALUE_DYNTEXT(wps_item, GO_TO_WPS, NULL, get_wps_item_name,
309 NULL, Icon_Playback_menu);
310 #ifdef HAVE_RECORDING
311 MENUITEM_RETURNVALUE(rec, ID2P(LANG_RECORDING_MENU), GO_TO_RECSCREEN,
312 NULL, Icon_Recording);
313 #endif
314 #if CONFIG_TUNER
315 MENUITEM_RETURNVALUE(fm, ID2P(LANG_FM_RADIO), GO_TO_FM,
316 item_callback, Icon_Radio_screen);
317 #endif
318 MENUITEM_RETURNVALUE(menu_, ID2P(LANG_SETTINGS_MENU), GO_TO_MAINMENU,
319 NULL, Icon_Submenu_Entered);
320 MENUITEM_RETURNVALUE(bookmarks, ID2P(LANG_BOOKMARK_MENU_RECENT_BOOKMARKS),
321 GO_TO_RECENTBMARKS, item_callback,
322 Icon_Bookmark);
323 #ifdef HAVE_LCD_CHARCELLS
324 static int do_shutdown(void)
326 #if CONFIG_CHARGING
327 if (charger_inserted())
328 charging_splash();
329 else
330 #endif
331 sys_poweroff();
332 return 0;
334 MENUITEM_FUNCTION(do_shutdown_item, 0, ID2P(LANG_SHUTDOWN),
335 do_shutdown, NULL, NULL, Icon_NOICON);
336 #endif
337 MAKE_MENU(root_menu_, ID2P(LANG_ROCKBOX_TITLE),
338 NULL, Icon_Rockbox,
339 &bookmarks, &file_browser,
340 #ifdef HAVE_TAGCACHE
341 &db_browser,
342 #endif
343 &wps_item, &menu_,
344 #ifdef HAVE_RECORDING
345 &rec,
346 #endif
347 #if CONFIG_TUNER
348 &fm,
349 #endif
350 &playlist_options, &rocks_browser, &info_menu
352 #ifdef HAVE_LCD_CHARCELLS
353 ,&do_shutdown_item
354 #endif
357 int item_callback(int action, const struct menu_item_ex *this_item)
359 switch (action)
361 case ACTION_REQUEST_MENUITEM:
362 #if CONFIG_TUNER
363 if (this_item == &fm)
365 if (radio_hardware_present() == 0)
366 return ACTION_EXIT_MENUITEM;
368 else
369 #endif
370 if (this_item == &bookmarks)
372 if (global_settings.usemrb == 0)
373 return ACTION_EXIT_MENUITEM;
375 break;
377 return action;
379 static int get_selection(int last_screen)
381 unsigned int i;
382 for(i=0; i< sizeof(root_menu__)/sizeof(*root_menu__); i++)
384 if (((root_menu__[i]->flags&MENU_TYPE_MASK) == MT_RETURN_VALUE) &&
385 (root_menu__[i]->value == last_screen))
387 return i;
390 return 0;
393 static inline int load_screen(int screen)
395 /* set the global_status.last_screen before entering,
396 if we dont we will always return to the wrong screen on boot */
397 int old_previous = last_screen;
398 int ret_val;
399 if (screen <= GO_TO_ROOT)
400 return screen;
401 if (screen == old_previous)
402 old_previous = GO_TO_ROOT;
403 global_status.last_screen = (char)screen;
404 status_save();
405 action_signalscreenchange();
406 ret_val = items[screen].function(items[screen].param);
407 last_screen = screen;
408 if (ret_val == GO_TO_PREVIOUS)
409 last_screen = old_previous;
410 return ret_val;
412 static int load_context_screen(int selection)
414 const struct menu_item_ex *context_menu = NULL;
415 if ((root_menu__[selection]->flags&MENU_TYPE_MASK) == MT_RETURN_VALUE)
417 int item = root_menu__[selection]->value;
418 context_menu = items[item].context_menu;
420 /* special cases */
421 else if (root_menu__[selection] == &info_menu)
423 context_menu = &system_menu;
426 if (context_menu)
427 return do_menu(context_menu, NULL);
428 else
429 return GO_TO_PREVIOUS;
431 void root_menu(void)
433 int previous_browser = GO_TO_FILEBROWSER;
434 int previous_music = GO_TO_WPS;
435 int next_screen = GO_TO_ROOT;
436 int selected = 0;
438 if (global_settings.start_in_screen == 0)
439 next_screen = (int)global_status.last_screen;
440 else next_screen = global_settings.start_in_screen - 2;
442 #ifdef HAVE_RTC_ALARM
443 if ( rtc_check_alarm_started(true) )
445 rtc_enable_alarm(false);
446 next_screen = GO_TO_WPS;
447 #if CONFIG_TUNER
448 if (global_settings.alarm_wake_up_screen == ALARM_START_FM)
449 next_screen = GO_TO_FM;
450 #endif
451 #ifdef HAVE_RECORDING
452 if (global_settings.alarm_wake_up_screen == ALARM_START_REC)
454 recording_start_automatic = true;
455 next_screen = GO_TO_RECSCREEN;
457 #endif
459 #endif /* HAVE_RTC_ALARM */
461 #ifdef HAVE_HEADPHONE_DETECTION
462 if (next_screen == GO_TO_WPS &&
463 (global_settings.unplug_autoresume && !headphones_inserted() ))
464 next_screen = GO_TO_ROOT;
465 #endif
467 while (true)
469 switch (next_screen)
471 case MENU_ATTACHED_USB:
472 case MENU_SELECTED_EXIT:
473 /* fall through */
474 case GO_TO_ROOT:
475 if (last_screen != GO_TO_ROOT)
476 selected = get_selection(last_screen);
477 next_screen = do_menu(&root_menu_, &selected);
478 if (next_screen != GO_TO_PREVIOUS)
479 last_screen = GO_TO_ROOT;
480 break;
482 case GO_TO_PREVIOUS:
483 next_screen = last_screen;
484 break;
486 case GO_TO_PREVIOUS_BROWSER:
487 next_screen = previous_browser;
488 break;
490 case GO_TO_PREVIOUS_MUSIC:
491 next_screen = previous_music;
492 break;
493 case GO_TO_ROOTITEM_CONTEXT:
494 next_screen = load_context_screen(selected);
495 break;
496 default:
497 if (next_screen == GO_TO_FILEBROWSER
498 #ifdef HAVE_TAGCACHE
499 || next_screen == GO_TO_DBBROWSER
500 #endif
502 previous_browser = next_screen;
503 if (next_screen == GO_TO_WPS
504 #if CONFIG_TUNER
505 || next_screen == GO_TO_FM
506 #endif
508 previous_music = next_screen;
509 next_screen = load_screen(next_screen);
510 break;
511 } /* switch() */
513 return;