Fix a dircache NULL-pointer dereference.
[kugel-rb.git] / apps / root_menu.c
blob5124375490203ecb75c4bd5ecb121ca7e5d7ff1e
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"
41 #ifdef HAVE_HOTSWAP
42 #include "storage.h"
43 #include "hotswap.h"
44 #include "dir.h"
45 #endif
46 /* gui api */
47 #include "list.h"
48 #include "splash.h"
49 #include "action.h"
50 #include "yesno.h"
51 #include "viewport.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 "wps.h"
61 #include "bookmark.h"
62 #include "playlist.h"
63 #include "playlist_viewer.h"
64 #include "menus/exported_menus.h"
65 #ifdef HAVE_RTC_ALARM
66 #include "rtc.h"
67 #endif
68 #ifdef HAVE_TAGCACHE
69 #include "tagcache.h"
70 #endif
71 #include "language.h"
73 struct root_items {
74 int (*function)(void* param);
75 void* param;
76 const struct menu_item_ex *context_menu;
78 static int next_screen = GO_TO_ROOT; /* holding info about the upcoming screen
79 * which is the current screen for the
80 * rest of the code after load_screen
81 * is called */
82 static int last_screen = GO_TO_ROOT; /* unfortunatly needed so we can resume
83 or goto current track based on previous
84 screen */
87 static char current_track_path[MAX_PATH];
88 static void rootmenu_track_changed_callback(void* param)
90 struct mp3entry *id3 = (struct mp3entry *)param;
91 strlcpy(current_track_path, id3->path, MAX_PATH);
93 static int browser(void* param)
95 int ret_val;
96 #ifdef HAVE_TAGCACHE
97 struct tree_context* tc = tree_get_context();
98 #endif
99 int filter = SHOW_SUPPORTED;
100 char folder[MAX_PATH] = "/";
101 /* stuff needed to remember position in file browser */
102 static char last_folder[MAX_PATH] = "/";
103 /* and stuff for the database browser */
104 #ifdef HAVE_TAGCACHE
105 static int last_db_dirlevel = 0, last_db_selection = 0;
106 #endif
108 switch ((intptr_t)param)
110 case GO_TO_FILEBROWSER:
111 filter = global_settings.dirfilter;
112 if (global_settings.browse_current &&
113 last_screen == GO_TO_WPS &&
114 current_track_path[0])
116 strcpy(folder, current_track_path);
118 else
120 #ifdef HAVE_HOTSWAP
121 bool in_hotswap = false;
122 /* handle entering an ejected drive */
123 int i;
124 for (i = 0; i < NUM_VOLUMES; i++)
126 char vol_string[VOL_ENUM_POS + 8];
127 if (!storage_removable(i))
128 continue;
129 /* VOL_NAMES contains a %d */
130 snprintf(vol_string, sizeof(vol_string), "/"VOL_NAMES, i);
131 /* test whether we would browse the external card */
132 if (!storage_present(i) &&
133 (strstr(last_folder, vol_string)
134 #ifdef HAVE_HOTSWAP_STORAGE_AS_MAIN
135 || (i == 0)
136 #endif
138 { /* leave folder as "/" to avoid crash when trying
139 * to access an ejected drive */
140 strcpy(folder, "/");
141 in_hotswap = true;
142 break;
145 if (!in_hotswap)
146 #endif
147 strcpy(folder, last_folder);
149 break;
150 #ifdef HAVE_TAGCACHE
151 case GO_TO_DBBROWSER:
152 if (!tagcache_is_usable())
154 bool reinit_attempted = false;
156 /* Now display progress until it's ready or the user exits */
157 while(!tagcache_is_usable())
159 struct tagcache_stat *stat = tagcache_get_stat();
161 /* Allow user to exit */
162 if (action_userabort(HZ/2))
163 break;
165 /* Maybe just needs to reboot due to delayed commit */
166 if (stat->commit_delayed)
168 splash(HZ*2, ID2P(LANG_PLEASE_REBOOT));
169 break;
172 /* Check if ready status is known */
173 if (!stat->readyvalid)
175 splash(0, str(LANG_TAGCACHE_BUSY));
176 continue;
179 /* Re-init if required */
180 if (!reinit_attempted && !stat->ready &&
181 stat->processed_entries == 0 && stat->commit_step == 0)
183 /* Prompt the user */
184 reinit_attempted = true;
185 static const char *lines[]={
186 ID2P(LANG_TAGCACHE_BUSY), ID2P(LANG_TAGCACHE_FORCE_UPDATE)};
187 static const struct text_message message={lines, 2};
188 if(gui_syncyesno_run(&message, NULL, NULL) == YESNO_NO)
189 break;
190 int i;
191 FOR_NB_SCREENS(i)
192 screens[i].clear_display();
194 /* Start initialisation */
195 tagcache_rebuild();
198 /* Display building progress */
199 static long talked_tick = 0;
200 if(global_settings.talk_menu &&
201 (talked_tick == 0
202 || TIME_AFTER(current_tick, talked_tick+7*HZ)))
204 talked_tick = current_tick;
205 if (stat->commit_step > 0)
207 talk_id(LANG_TAGCACHE_INIT, false);
208 talk_number(stat->commit_step, true);
209 talk_id(VOICE_OF, true);
210 talk_number(tagcache_get_max_commit_step(), true);
211 } else if(stat->processed_entries)
213 talk_number(stat->processed_entries, false);
214 talk_id(LANG_BUILDING_DATABASE, true);
217 if (stat->commit_step > 0)
219 if (lang_is_rtl())
221 splashf(0, "[%d/%d] %s", stat->commit_step,
222 tagcache_get_max_commit_step(),
223 str(LANG_TAGCACHE_INIT));
225 else
227 splashf(0, "%s [%d/%d]", str(LANG_TAGCACHE_INIT),
228 stat->commit_step,
229 tagcache_get_max_commit_step());
232 else
234 splashf(0, str(LANG_BUILDING_DATABASE),
235 stat->processed_entries);
239 if (!tagcache_is_usable())
240 return GO_TO_PREVIOUS;
241 filter = SHOW_ID3DB;
242 tc->dirlevel = last_db_dirlevel;
243 tc->selected_item = last_db_selection;
244 break;
245 #endif
246 case GO_TO_BROWSEPLUGINS:
247 filter = SHOW_PLUGINS;
248 strlcpy(folder, PLUGIN_DIR, MAX_PATH);
249 break;
251 ret_val = rockbox_browse(folder, filter);
252 switch ((intptr_t)param)
254 case GO_TO_FILEBROWSER:
255 get_current_file(last_folder, MAX_PATH);
256 break;
257 #ifdef HAVE_TAGCACHE
258 case GO_TO_DBBROWSER:
259 last_db_dirlevel = tc->dirlevel;
260 last_db_selection = tc->selected_item;
261 break;
262 #endif
264 return ret_val;
267 static int menu(void* param)
269 (void)param;
270 return do_menu(NULL, 0, NULL, false);
273 #ifdef HAVE_RECORDING
274 static int recscrn(void* param)
276 (void)param;
277 recording_screen(false);
278 return GO_TO_ROOT;
280 #endif
281 static int wpsscrn(void* param)
283 int ret_val = GO_TO_PREVIOUS;
284 (void)param;
285 if (audio_status())
287 talk_shutup();
288 ret_val = gui_wps_show();
290 else if ( global_status.resume_index != -1 )
292 DEBUGF("Resume index %X offset %lX\n",
293 global_status.resume_index,
294 (unsigned long)global_status.resume_offset);
295 if (playlist_resume() != -1)
297 playlist_start(global_status.resume_index,
298 global_status.resume_offset);
299 ret_val = gui_wps_show();
302 else
304 splash(HZ*2, ID2P(LANG_NOTHING_TO_RESUME));
306 return ret_val;
308 #if CONFIG_TUNER
309 static int radio(void* param)
311 (void)param;
312 radio_screen();
313 return GO_TO_ROOT;
315 #endif
317 static int playlist_view(void * param)
319 (void)param;
320 switch (playlist_viewer())
322 case PLAYLIST_VIEWER_MAINMENU:
323 case PLAYLIST_VIEWER_USB:
324 return GO_TO_ROOT;
325 case PLAYLIST_VIEWER_OK:
326 return GO_TO_PREVIOUS;
328 return GO_TO_PREVIOUS;
331 static int load_bmarks(void* param)
333 (void)param;
334 if(bookmark_mrb_load())
335 return GO_TO_WPS;
336 return GO_TO_PREVIOUS;
338 static int plugins_menu(void* param)
340 (void)param;
341 MENUITEM_STRINGLIST(plugins_menu_items, ID2P(LANG_PLUGINS), NULL,
342 ID2P(LANG_PLUGIN_GAMES),
343 ID2P(LANG_PLUGIN_APPS), ID2P(LANG_PLUGIN_DEMOS));
344 char *folder;
345 int retval = GO_TO_PREVIOUS;
346 int selection = 0, current = 0;
347 while (retval == GO_TO_PREVIOUS)
349 selection = do_menu(&plugins_menu_items, &current, NULL, false);
350 switch (selection)
352 case 0:
353 folder = PLUGIN_GAMES_DIR;
354 break;
355 case 1:
356 folder = PLUGIN_APPS_DIR;
357 break;
358 case 2:
359 folder = PLUGIN_DEMOS_DIR;
360 break;
361 default:
362 return selection;
364 retval = rockbox_browse(folder, SHOW_PLUGINS);
366 return retval;
368 int time_screen(void* ignored);
370 /* These are all static const'd from apps/menus/ *.c
371 so little hack so we can use them */
372 extern struct menu_item_ex
373 file_menu,
374 #ifdef HAVE_TAGCACHE
375 tagcache_menu,
376 #endif
377 manage_settings,
378 recording_settings_menu,
379 radio_settings_menu,
380 bookmark_settings_menu,
381 system_menu;
382 static const struct root_items items[] = {
383 [GO_TO_FILEBROWSER] = { browser, (void*)GO_TO_FILEBROWSER, &file_menu},
384 #ifdef HAVE_TAGCACHE
385 [GO_TO_DBBROWSER] = { browser, (void*)GO_TO_DBBROWSER, &tagcache_menu },
386 #endif
387 [GO_TO_WPS] = { wpsscrn, NULL, &playback_settings },
388 [GO_TO_MAINMENU] = { menu, NULL, &manage_settings },
390 #ifdef HAVE_RECORDING
391 [GO_TO_RECSCREEN] = { recscrn, NULL, &recording_settings_menu },
392 #endif
394 #if CONFIG_TUNER
395 [GO_TO_FM] = { radio, NULL, &radio_settings_menu },
396 #endif
398 [GO_TO_RECENTBMARKS] = { load_bmarks, NULL, &bookmark_settings_menu },
399 [GO_TO_BROWSEPLUGINS] = { plugins_menu, NULL, NULL },
400 [GO_TO_PLAYLIST_VIEWER] = { playlist_view, NULL, NULL },
403 static const int nb_items = sizeof(items)/sizeof(*items);
405 static int item_callback(int action, const struct menu_item_ex *this_item) ;
407 MENUITEM_RETURNVALUE(file_browser, ID2P(LANG_DIR_BROWSER), GO_TO_FILEBROWSER,
408 NULL, Icon_file_view_menu);
409 #ifdef HAVE_TAGCACHE
410 MENUITEM_RETURNVALUE(db_browser, ID2P(LANG_TAGCACHE), GO_TO_DBBROWSER,
411 NULL, Icon_Audio);
412 #endif
413 MENUITEM_RETURNVALUE(rocks_browser, ID2P(LANG_PLUGINS), GO_TO_BROWSEPLUGINS,
414 NULL, Icon_Plugin);
415 static char *get_wps_item_name(int selected_item, void * data, char *buffer)
417 (void)selected_item; (void)data; (void)buffer;
418 if (audio_status())
419 return ID2P(LANG_NOW_PLAYING);
420 return ID2P(LANG_RESUME_PLAYBACK);
422 MENUITEM_RETURNVALUE_DYNTEXT(wps_item, GO_TO_WPS, NULL, get_wps_item_name,
423 NULL, NULL, Icon_Playback_menu);
424 #ifdef HAVE_RECORDING
425 MENUITEM_RETURNVALUE(rec, ID2P(LANG_RECORDING), GO_TO_RECSCREEN,
426 NULL, Icon_Recording);
427 #endif
428 #if CONFIG_TUNER
429 MENUITEM_RETURNVALUE(fm, ID2P(LANG_FM_RADIO), GO_TO_FM,
430 item_callback, Icon_Radio_screen);
431 #endif
432 MENUITEM_RETURNVALUE(menu_, ID2P(LANG_SETTINGS), GO_TO_MAINMENU,
433 NULL, Icon_Submenu_Entered);
434 MENUITEM_RETURNVALUE(bookmarks, ID2P(LANG_BOOKMARK_MENU_RECENT_BOOKMARKS),
435 GO_TO_RECENTBMARKS, item_callback,
436 Icon_Bookmark);
437 #ifdef HAVE_LCD_CHARCELLS
438 static int do_shutdown(void)
440 #if CONFIG_CHARGING
441 if (charger_inserted())
442 charging_splash();
443 else
444 #endif
445 sys_poweroff();
446 return 0;
448 MENUITEM_FUNCTION(do_shutdown_item, 0, ID2P(LANG_SHUTDOWN),
449 do_shutdown, NULL, NULL, Icon_NOICON);
450 #endif
451 MAKE_MENU(root_menu_, ID2P(LANG_ROCKBOX_TITLE),
452 item_callback, Icon_Rockbox,
453 &bookmarks, &file_browser,
454 #ifdef HAVE_TAGCACHE
455 &db_browser,
456 #endif
457 &wps_item, &menu_,
458 #ifdef HAVE_RECORDING
459 &rec,
460 #endif
461 #if CONFIG_TUNER
462 &fm,
463 #endif
464 &playlist_options, &rocks_browser, &info_menu
466 #ifdef HAVE_LCD_CHARCELLS
467 ,&do_shutdown_item
468 #endif
471 static int item_callback(int action, const struct menu_item_ex *this_item)
473 switch (action)
475 case ACTION_TREE_STOP:
476 return ACTION_REDRAW;
477 case ACTION_REQUEST_MENUITEM:
478 #if CONFIG_TUNER
479 if (this_item == &fm)
481 if (radio_hardware_present() == 0)
482 return ACTION_EXIT_MENUITEM;
484 else
485 #endif
486 if (this_item == &bookmarks)
488 if (global_settings.usemrb == 0)
489 return ACTION_EXIT_MENUITEM;
491 break;
493 return action;
495 static int get_selection(int last_screen)
497 int i;
498 int len = ARRAYLEN(root_menu__);
499 for(i=0; i < len; i++)
501 if (((root_menu__[i]->flags&MENU_TYPE_MASK) == MT_RETURN_VALUE) &&
502 (root_menu__[i]->value == last_screen))
504 return i;
507 return 0;
510 static inline int load_screen(int screen)
512 /* set the global_status.last_screen before entering,
513 if we dont we will always return to the wrong screen on boot */
514 int old_previous = last_screen;
515 int ret_val;
516 if (screen <= GO_TO_ROOT)
517 return screen;
518 if (screen == old_previous)
519 old_previous = GO_TO_ROOT;
520 global_status.last_screen = (char)screen;
521 status_save();
522 ret_val = items[screen].function(items[screen].param);
523 last_screen = screen;
524 if (ret_val == GO_TO_PREVIOUS)
525 last_screen = old_previous;
526 return ret_val;
528 static int load_context_screen(int selection)
530 const struct menu_item_ex *context_menu = NULL;
531 if ((root_menu__[selection]->flags&MENU_TYPE_MASK) == MT_RETURN_VALUE)
533 int item = root_menu__[selection]->value;
534 context_menu = items[item].context_menu;
536 /* special cases */
537 else if (root_menu__[selection] == &info_menu)
539 context_menu = &system_menu;
542 if (context_menu)
543 return do_menu(context_menu, NULL, NULL, false);
544 else
545 return GO_TO_PREVIOUS;
548 static int previous_music = GO_TO_WPS;
550 void previous_music_is_wps(void)
552 previous_music = GO_TO_WPS;
555 int current_screen(void)
557 return next_screen;
560 void root_menu(void)
562 int previous_browser = GO_TO_FILEBROWSER;
563 int selected = 0;
565 if (global_settings.start_in_screen == 0)
566 next_screen = (int)global_status.last_screen;
567 else next_screen = global_settings.start_in_screen - 2;
568 add_event(PLAYBACK_EVENT_TRACK_CHANGE, false, rootmenu_track_changed_callback);
569 #ifdef HAVE_RTC_ALARM
570 if ( rtc_check_alarm_started(true) )
572 rtc_enable_alarm(false);
573 next_screen = GO_TO_WPS;
574 #if CONFIG_TUNER
575 if (global_settings.alarm_wake_up_screen == ALARM_START_FM)
576 next_screen = GO_TO_FM;
577 #endif
578 #ifdef HAVE_RECORDING
579 if (global_settings.alarm_wake_up_screen == ALARM_START_REC)
581 recording_start_automatic = true;
582 next_screen = GO_TO_RECSCREEN;
584 #endif
586 #endif /* HAVE_RTC_ALARM */
588 #ifdef HAVE_HEADPHONE_DETECTION
589 if (next_screen == GO_TO_WPS &&
590 (global_settings.unplug_autoresume && !headphones_inserted() ))
591 next_screen = GO_TO_ROOT;
592 #endif
594 while (true)
596 switch (next_screen)
598 case MENU_ATTACHED_USB:
599 case MENU_SELECTED_EXIT:
600 /* fall through */
601 case GO_TO_ROOT:
602 if (last_screen != GO_TO_ROOT)
603 selected = get_selection(last_screen);
604 next_screen = do_menu(&root_menu_, &selected, NULL, false);
605 if (next_screen != GO_TO_PREVIOUS)
606 last_screen = GO_TO_ROOT;
607 break;
609 case GO_TO_PREVIOUS:
610 next_screen = last_screen;
611 break;
613 case GO_TO_PREVIOUS_BROWSER:
614 next_screen = previous_browser;
615 break;
617 case GO_TO_PREVIOUS_MUSIC:
618 next_screen = previous_music;
619 break;
620 case GO_TO_ROOTITEM_CONTEXT:
621 next_screen = load_context_screen(selected);
622 break;
623 default:
624 if (next_screen == GO_TO_FILEBROWSER
625 #ifdef HAVE_TAGCACHE
626 || next_screen == GO_TO_DBBROWSER
627 #endif
629 previous_browser = next_screen;
630 if (next_screen == GO_TO_WPS
631 #if CONFIG_TUNER
632 || next_screen == GO_TO_FM
633 #endif
635 previous_music = next_screen;
636 next_screen = load_screen(next_screen);
637 break;
638 } /* switch() */