Document the Play Next option. (adjusted FS#6486)
[Rockbox.git] / apps / tree.c
blob06b42b7c86593f4c841eee98446024a5fb1e45d4
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2002 Daniel Stenberg
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>
24 #include "applimits.h"
25 #include "dir.h"
26 #include "file.h"
27 #include "lcd.h"
28 #include "font.h"
29 #include "backlight.h"
30 #include "button.h"
31 #include "kernel.h"
32 #include "usb.h"
33 #include "tree.h"
34 #include "main_menu.h"
35 #include "sprintf.h"
36 #include "audio.h"
37 #include "playlist.h"
38 #include "menu.h"
39 #include "gwps.h"
40 #include "settings.h"
41 #include "status.h"
42 #include "debug.h"
43 #include "ata.h"
44 #include "rolo.h"
45 #include "icons.h"
46 #include "lang.h"
47 #include "language.h"
48 #include "screens.h"
49 #include "keyboard.h"
50 #include "bookmark.h"
51 #include "onplay.h"
52 #include "buffer.h"
53 #include "plugin.h"
54 #include "power.h"
55 #include "action.h"
56 #include "talk.h"
57 #include "filetypes.h"
58 #include "misc.h"
59 #include "filetree.h"
60 #include "tagtree.h"
61 #ifdef HAVE_RECORDING
62 #include "recorder/recording.h"
63 #endif
64 #include "rtc.h"
65 #include "dircache.h"
66 #ifdef HAVE_TAGCACHE
67 #include "tagcache.h"
68 #endif
69 #include "yesno.h"
70 #include "gwps-common.h"
71 #include "eeprom_settings.h"
72 #include "scrobbler.h"
74 /* gui api */
75 #include "list.h"
76 #include "statusbar.h"
77 #include "splash.h"
78 #include "buttonbar.h"
79 #include "textarea.h"
80 #include "action.h"
82 #include "root_menu.h"
84 #if LCD_DEPTH > 1
85 #include "backdrop.h"
86 #endif
88 /* a table for the know file types */
89 const struct filetype filetypes[] = {
90 { "mp3", TREE_ATTR_MPA, Icon_Audio, VOICE_EXT_MPA },
91 { "mp2", TREE_ATTR_MPA, Icon_Audio, VOICE_EXT_MPA },
92 { "mpa", TREE_ATTR_MPA, Icon_Audio, VOICE_EXT_MPA },
93 #if CONFIG_CODEC == SWCODEC
94 /* Temporary hack to allow playlist creation */
95 { "mp1", TREE_ATTR_MPA, Icon_Audio, VOICE_EXT_MPA },
96 { "ogg", TREE_ATTR_MPA, Icon_Audio, VOICE_EXT_MPA },
97 { "wma", TREE_ATTR_MPA, Icon_Audio, VOICE_EXT_MPA },
98 { "wav", TREE_ATTR_MPA, Icon_Audio, VOICE_EXT_MPA },
99 { "flac",TREE_ATTR_MPA, Icon_Audio, VOICE_EXT_MPA },
100 { "ac3", TREE_ATTR_MPA, Icon_Audio, VOICE_EXT_MPA },
101 { "a52", TREE_ATTR_MPA, Icon_Audio, VOICE_EXT_MPA },
102 { "mpc", TREE_ATTR_MPA, Icon_Audio, VOICE_EXT_MPA },
103 { "wv", TREE_ATTR_MPA, Icon_Audio, VOICE_EXT_MPA },
104 { "m4a", TREE_ATTR_MPA, Icon_Audio, VOICE_EXT_MPA },
105 { "m4b", TREE_ATTR_MPA, Icon_Audio, VOICE_EXT_MPA },
106 { "mp4", TREE_ATTR_MPA, Icon_Audio, VOICE_EXT_MPA },
107 { "shn", TREE_ATTR_MPA, Icon_Audio, VOICE_EXT_MPA },
108 { "aif", TREE_ATTR_MPA, Icon_Audio, VOICE_EXT_MPA },
109 { "aiff",TREE_ATTR_MPA, Icon_Audio, VOICE_EXT_MPA },
110 { "spx" ,TREE_ATTR_MPA, Icon_Audio, VOICE_EXT_MPA },
111 { "sid", TREE_ATTR_MPA, Icon_Audio, VOICE_EXT_MPA },
112 { "adx", TREE_ATTR_MPA, Icon_Audio, VOICE_EXT_MPA },
113 { "nsf", TREE_ATTR_MPA, Icon_Audio, VOICE_EXT_MPA },
114 { "nsfe", TREE_ATTR_MPA, Icon_Audio, VOICE_EXT_MPA },
115 { "spc", TREE_ATTR_MPA, Icon_Audio, VOICE_EXT_MPA },
116 #endif
117 { "m3u", TREE_ATTR_M3U, Icon_Playlist, LANG_PLAYLIST },
118 { "m3u8", TREE_ATTR_M3U, Icon_Playlist, LANG_PLAYLIST },
119 { "cfg", TREE_ATTR_CFG, Icon_Config, VOICE_EXT_CFG },
120 { "wps", TREE_ATTR_WPS, Icon_Wps, VOICE_EXT_WPS },
121 #ifdef HAVE_REMOTE_LCD
122 { "rwps", TREE_ATTR_RWPS, Icon_Wps, VOICE_EXT_RWPS },
123 #endif
124 #if LCD_DEPTH > 1
125 { "bmp", TREE_ATTR_BMP, Icon_Wps, VOICE_EXT_WPS },
126 #endif
127 #if CONFIG_TUNER
128 { "fmr", TREE_ATTR_FMR, Icon_Preset, LANG_FMR },
129 #endif
130 { "lng", TREE_ATTR_LNG, Icon_Language, LANG_LANGUAGE },
131 { "rock",TREE_ATTR_ROCK,Icon_Plugin, VOICE_EXT_ROCK },
132 #ifdef HAVE_LCD_BITMAP
133 { "fnt", TREE_ATTR_FONT,Icon_Font, VOICE_EXT_FONT },
134 { "kbd", TREE_ATTR_KBD, Icon_Keyboard, VOICE_EXT_KBD },
135 #endif
136 { "bmark",TREE_ATTR_BMARK, Icon_Bookmark, VOICE_EXT_BMARK },
137 { "cue", TREE_ATTR_CUE, Icon_Bookmark, LANG_CUESHEET },
138 #ifdef BOOTFILE_EXT
139 { BOOTFILE_EXT, TREE_ATTR_MOD, Icon_Firmware, VOICE_EXT_AJZ },
140 #endif /* #ifndef SIMULATOR */
143 struct gui_synclist tree_lists;
145 /* I put it here because other files doesn't use it yet,
146 * but should be elsewhere since it will be used mostly everywhere */
147 #ifdef HAS_BUTTONBAR
148 struct gui_buttonbar tree_buttonbar;
149 #endif
150 static struct tree_context tc;
152 bool boot_changed = false;
154 char lastfile[MAX_PATH];
155 static char lastdir[MAX_PATH];
156 #ifdef HAVE_TAGCACHE
157 static int lasttable, lastextra, lastfirstpos;
158 #endif
159 static int max_files = 0;
161 static bool reload_dir = false;
163 static bool start_wps = false;
164 static int curr_context = false;/* id3db or tree*/
166 int dirbrowse(void);
169 * removes the extension of filename (if it doesn't start with a .)
170 * puts the result in buffer
172 static char * strip_extension(char * filename, char * buffer)
174 int dotpos;
175 char * dot=strrchr(filename, '.');
176 if(dot!=0 && filename[0]!='.')
178 dotpos = dot-filename;
179 strncpy(buffer, filename, dotpos);
180 buffer[dotpos]='\0';
181 return(buffer);
183 else
184 return(filename);
187 static char * tree_get_filename(int selected_item, void * data, char *buffer)
189 struct tree_context * local_tc=(struct tree_context *)data;
190 char *name;
191 int attr=0;
192 #ifdef HAVE_TAGCACHE
193 bool id3db = *(local_tc->dirfilter) == SHOW_ID3DB;
195 if (id3db)
197 return tagtree_get_entry(&tc, selected_item)->name;
199 else
200 #endif
202 struct entry* dc = local_tc->dircache;
203 struct entry* e = &dc[selected_item];
204 name = e->name;
205 attr = e->attr;
207 /* if any file filter is on, and if it's not a directory,
208 * strip the extension */
210 if ( (*(local_tc->dirfilter) != SHOW_ID3DB) && !(attr & ATTR_DIRECTORY)
211 && (*(local_tc->dirfilter) != SHOW_ALL) )
213 return(strip_extension(name, buffer));
215 return(name);
219 static void tree_get_fileicon(int selected_item, void * data, ICON * icon)
221 struct tree_context * local_tc=(struct tree_context *)data;
222 #ifdef HAVE_TAGCACHE
223 bool id3db = *(local_tc->dirfilter) == SHOW_ID3DB;
224 if (id3db) {
225 *icon = (ICON)tagtree_get_icon(&tc);
227 else
228 #endif
230 struct entry* dc = local_tc->dircache;
231 struct entry* e = &dc[selected_item];
232 *icon = (ICON)filetype_get_icon(e->attr);
236 bool check_rockboxdir(void)
238 DIR *dir = opendir(ROCKBOX_DIR);
239 if(!dir)
240 { /* No need to localise this message.
241 If .rockbox is missing, it wouldn't work anyway */
242 int i;
243 FOR_NB_SCREENS(i)
244 screens[i].clear_display();
245 gui_syncsplash(HZ*2, true, "No .rockbox directory");
246 FOR_NB_SCREENS(i)
247 screens[i].clear_display();
248 gui_syncsplash(HZ*2, true, "Installation incomplete");
249 return false;
251 closedir(dir);
252 return true;
255 void browse_root(void)
257 gui_sync_wps_screen_init();
259 filetype_init();
260 check_rockboxdir();
262 strcpy(tc.currdir, "/");
264 #ifdef HAVE_LCD_CHARCELLS
265 int i;
266 FOR_NB_SCREENS(i)
267 screens[i].double_height(false);
268 #endif
269 #ifdef HAS_BUTTONBAR
270 gui_buttonbar_init(&tree_buttonbar);
271 /* since archos only have one screen, no need to create more than that */
272 gui_buttonbar_set_display(&tree_buttonbar, &(screens[SCREEN_MAIN]) );
273 #endif
274 gui_synclist_init(&tree_lists, &tree_get_filename, &tc, false, 1);
275 gui_synclist_set_icon_callback(&tree_lists, &tree_get_fileicon);
276 /* not the best place for this call... but... */
277 root_menu();
280 void tree_get_filetypes(const struct filetype** types, int* count)
282 *types = filetypes;
283 *count = sizeof(filetypes) / sizeof(*filetypes);
286 struct tree_context* tree_get_context(void)
288 return &tc;
291 /* talkbox hovering delay, to avoid immediate disk activity */
292 #define HOVER_DELAY (HZ/2)
294 * Returns the position of a given file in the current directory
295 * returns -1 if not found
297 static int tree_get_file_position(char * filename)
299 int i;
301 /* use lastfile to determine the selected item (default=0) */
302 for (i=0; i < tc.filesindir; i++)
304 struct entry* dc = tc.dircache;
305 struct entry* e = &dc[i];
306 if (!strcasecmp(e->name, filename))
307 return(i);
309 return(-1);/* no file can match, returns undefined */
313 * Called when a new dir is loaded (for example when returning from other apps ...)
314 * also completely redraws the tree
316 static int update_dir(void)
318 bool changed = false;
319 #ifdef HAVE_TAGCACHE
320 bool id3db = *tc.dirfilter == SHOW_ID3DB;
321 /* Checks for changes */
322 if (id3db) {
323 if (tc.currtable != lasttable ||
324 tc.currextra != lastextra ||
325 tc.firstpos != lastfirstpos ||
326 reload_dir)
328 if (tagtree_load(&tc) < 0)
329 return -1;
331 lasttable = tc.currtable;
332 lastextra = tc.currextra;
333 lastfirstpos = tc.firstpos;
334 changed = true;
337 else
338 #endif
340 /* if the tc.currdir has been changed, reload it ...*/
341 if (strncmp(tc.currdir, lastdir, sizeof(lastdir)) || reload_dir) {
343 if (ft_load(&tc, NULL) < 0)
344 return -1;
345 strcpy(lastdir, tc.currdir);
346 changed = true;
349 /* if selected item is undefined */
350 if (tc.selected_item == -1)
352 /* use lastfile to determine the selected item */
353 tc.selected_item = tree_get_file_position(lastfile);
355 /* If the file doesn't exists, select the first one (default) */
356 if(tc.selected_item < 0)
357 tc.selected_item = 0;
358 changed = true;
360 if (changed)
363 #ifdef HAVE_TAGCACHE
364 !id3db &&
365 #endif
366 (tc.dirfull ||
367 tc.filesindir == global_settings.max_files_in_dir) )
369 gui_syncsplash(HZ, true, str(LANG_SHOWDIR_BUFFER_FULL));
372 #ifdef HAVE_TAGCACHE
373 if (id3db)
375 if (global_settings.show_path_in_browser == SHOW_PATH_FULL
376 || global_settings.show_path_in_browser == SHOW_PATH_CURRENT)
378 gui_synclist_set_title(&tree_lists, tagtree_get_title(&tc),
379 filetype_get_icon(ATTR_DIRECTORY));
381 else
383 /* Must clear the title as the list is reused */
384 gui_synclist_set_title(&tree_lists, NULL, NOICON);
387 else
388 #endif
390 if (global_settings.show_path_in_browser == SHOW_PATH_FULL)
392 gui_synclist_set_title(&tree_lists, tc.currdir,
393 filetype_get_icon(ATTR_DIRECTORY));
395 else if (global_settings.show_path_in_browser == SHOW_PATH_CURRENT)
397 char *title = strrchr(tc.currdir, '/') + 1;
398 if (*title == '\0')
400 /* Display "Files" for the root dir */
401 gui_synclist_set_title(&tree_lists, str(LANG_DIR_BROWSER),
402 filetype_get_icon(ATTR_DIRECTORY));
404 else
405 gui_synclist_set_title(&tree_lists, title,
406 filetype_get_icon(ATTR_DIRECTORY));
408 else
410 /* Must clear the title as the list is reused */
411 gui_synclist_set_title(&tree_lists, NULL, NOICON);
415 gui_synclist_set_nb_items(&tree_lists, tc.filesindir);
416 gui_synclist_set_icon_callback(&tree_lists, tree_get_fileicon);
417 if( tc.selected_item >= tc.filesindir)
418 tc.selected_item=tc.filesindir-1;
420 gui_synclist_select_item(&tree_lists, tc.selected_item);
421 #ifdef HAS_BUTTONBAR
422 if (global_settings.buttonbar) {
423 if (*tc.dirfilter < NUM_FILTER_MODES)
424 gui_buttonbar_set(&tree_buttonbar, str(LANG_SYSFONT_DIRBROWSE_F1),
425 str(LANG_SYSFONT_DIRBROWSE_F2),
426 str(LANG_SYSFONT_DIRBROWSE_F3));
427 else
428 gui_buttonbar_set(&tree_buttonbar, "<<<", "", "");
429 gui_buttonbar_draw(&tree_buttonbar);
431 #endif
432 gui_synclist_draw(&tree_lists);
433 gui_syncstatusbar_draw(&statusbars, true);
434 return tc.filesindir;
437 /* load tracks from specified directory to resume play */
438 void resume_directory(const char *dir)
440 #ifdef HAVE_TAGCACHE
441 bool id3db = *tc.dirfilter == SHOW_ID3DB;
442 #endif
444 if (ft_load(&tc, dir) < 0)
445 return;
446 lastdir[0] = 0;
448 ft_build_playlist(&tc, 0);
450 #ifdef HAVE_TAGCACHE
451 if (id3db)
452 tagtree_load(&tc);
453 #endif
456 /* Returns the current working directory and also writes cwd to buf if
457 non-NULL. In case of error, returns NULL. */
458 char *getcwd(char *buf, int size)
460 if (!buf)
461 return tc.currdir;
462 else if (size > 0)
464 strncpy(buf, tc.currdir, size);
465 return buf;
467 else
468 return NULL;
471 /* Force a reload of the directory next time directory browser is called */
472 void reload_directory(void)
474 reload_dir = true;
477 /* Selects a file and update tree context properly */
478 void set_current_file(char *path)
480 char *name;
481 int i;
483 #ifdef HAVE_TAGCACHE
484 /* in ID3DB mode it is a bad idea to call this function */
485 /* (only happens with `follow playlist') */
486 if( *tc.dirfilter == SHOW_ID3DB )
487 return;
488 #endif
490 /* separate directory from filename */
491 /* gets the directory's name and put it into tc.currdir */
492 name = strrchr(path+1,'/');
493 if (name)
495 *name = 0;
496 strcpy(tc.currdir, path);
497 *name = '/';
498 name++;
500 else
502 strcpy(tc.currdir, "/");
503 name = path+1;
506 strcpy(lastfile, name);
508 /* undefined item selected */
509 tc.selected_item = -1;
511 /* If we changed dir we must recalculate the dirlevel
512 and adjust the selected history properly */
513 if (strncmp(tc.currdir,lastdir,sizeof(lastdir)))
515 tc.dirlevel = 0;
516 tc.selected_item_history[tc.dirlevel] = -1;
518 /* use '/' to calculate dirlevel */
519 for (i = 1; path[i] != '\0'; i++)
521 if (path[i] == '/')
523 tc.dirlevel++;
524 tc.selected_item_history[tc.dirlevel] = -1;
528 if (ft_load(&tc, NULL) >= 0)
530 tc.selected_item = tree_get_file_position(lastfile);
534 #if defined(HAVE_TAGCACHE) && defined(HAVE_QUICKSCREEN)
535 static bool check_changed_id3mode(bool currmode)
537 if (currmode != (global_settings.dirfilter == SHOW_ID3DB)) {
538 currmode = global_settings.dirfilter == SHOW_ID3DB;
539 if (currmode) {
540 curr_context=CONTEXT_ID3DB;
541 tc.dirlevel = 0;
542 tagtree_load(&tc);
544 else
546 tc.dirlevel = 0;
547 curr_context=CONTEXT_TREE;
548 ft_load(&tc, NULL);
549 reload_dir = true;
552 return currmode;
554 #endif
556 /* main loop, handles key events */
557 int dirbrowse()
559 int numentries=0;
560 char buf[MAX_PATH];
561 int lasti = -1;
562 unsigned button, returned_button;
563 bool reload_root = false;
564 int lastfilter = *tc.dirfilter;
565 bool lastsortcase = global_settings.sort_case;
566 bool need_update = true;
567 bool exit_func = false;
568 long thumbnail_time = -1; /* for delaying a thumbnail */
569 long last_cancel = 0;
571 char* currdir = tc.currdir; /* just a shortcut */
572 #ifdef HAVE_TAGCACHE
573 bool id3db = *tc.dirfilter == SHOW_ID3DB;
575 if (id3db)
576 curr_context=CONTEXT_ID3DB;
577 else
578 #endif
579 curr_context=CONTEXT_TREE;
580 if (tc.selected_item < 0)
581 tc.selected_item = 0;
582 #ifdef HAVE_TAGCACHE
583 tc.firstpos=0;
584 lasttable = -1;
585 lastextra = -1;
586 lastfirstpos = 0;
587 #endif
589 start_wps = false;
590 numentries = update_dir();
591 if (numentries == -1)
592 return false; /* currdir is not a directory */
594 if (*tc.dirfilter > NUM_FILTER_MODES && numentries==0)
596 gui_syncsplash(HZ*2, true, str(LANG_NO_FILES));
597 return false; /* No files found for rockbox_browser() */
600 while(1) {
601 struct entry *dircache = tc.dircache;
602 bool restore = false;
603 if (tc.dirlevel < 0)
604 tc.dirlevel = 0; /* shouldnt be needed.. this code needs work! */
605 #ifdef BOOTFILE
606 if (boot_changed) {
607 char *lines[]={str(LANG_BOOT_CHANGED), str(LANG_REBOOT_NOW)};
608 struct text_message message={lines, 2};
609 if(gui_syncyesno_run(&message, NULL, NULL)==YESNO_YES)
610 rolo_load("/" BOOTFILE);
611 restore = true;
612 boot_changed = false;
614 #endif
615 button = get_action(CONTEXT_TREE,HZ/5);
616 returned_button = gui_synclist_do_button(&tree_lists, button,LIST_WRAP_UNLESS_HELD);
617 if (returned_button)
618 need_update = true;
619 if (returned_button == ACTION_STD_CANCEL)
620 button = ACTION_STD_CANCEL;
622 switch ( button ) {
623 case ACTION_STD_OK:
624 /* nothing to do if no files to display */
625 if ( numentries == 0 )
626 break;
628 #ifdef HAVE_TAGCACHE
629 switch (id3db?tagtree_enter(&tc):ft_enter(&tc))
630 #else
631 switch (ft_enter(&tc))
632 #endif
634 case 1: reload_dir = true; break;
635 case 2: start_wps = true; break;
636 case 3: exit_func = true; break;
637 default: break;
639 restore = true;
640 break;
642 case ACTION_STD_CANCEL:
643 if (*tc.dirfilter > NUM_FILTER_MODES && tc.dirlevel < 1) {
644 exit_func = true;
645 break;
647 if ((*tc.dirfilter == SHOW_ID3DB && tc.dirlevel == 0) ||
648 ((*tc.dirfilter != SHOW_ID3DB && !strcmp(currdir,"/"))))
650 if (last_cancel && TIME_BEFORE(current_tick, last_cancel+HZ/2))
652 last_cancel = 0;
653 action_signalscreenchange(); /* eat the cancel presses */
654 break;
656 else
657 return GO_TO_ROOT;
659 last_cancel = current_tick;
661 #ifdef HAVE_TAGCACHE
662 if (id3db)
663 tagtree_exit(&tc);
664 else
665 #endif
666 if (ft_exit(&tc) == 3)
667 exit_func = true;
669 restore = true;
670 break;
672 case ACTION_TREE_STOP:
673 if (*tc.dirfilter < NUM_FILTER_MODES)
675 /* Stop the music if it is playing */
676 if(audio_status()) {
677 if (!global_settings.party_mode) {
678 if (global_settings.fade_on_stop)
679 fade(0);
680 bookmark_autobookmark();
681 audio_stop();
684 #if CONFIG_CHARGING && \
685 (CONFIG_KEYPAD == RECORDER_PAD) && !defined(HAVE_SW_POWEROFF)
686 else {
687 if (!charger_inserted()) {
688 if(shutdown_screen())
689 reload_dir = true;
690 } else {
691 charging_splash();
693 restore = true;
695 #endif
697 #if CONFIG_CHARGING && !defined(HAVE_POWEROFF_WHILE_CHARGING)
699 static int last_off = 0;
700 if (current_tick - last_off < 50) {
701 if (charger_inserted()) {
702 charging_splash();
703 restore = true;
706 last_off = current_tick;
708 #endif
709 break; /* case ACTION_TREE_STOP: */
710 case ACTION_STD_MENU:
711 return GO_TO_ROOT;
712 break;
714 case ACTION_TREE_WPS:
715 return GO_TO_PREVIOUS_MUSIC;
716 break;
717 #ifdef HAVE_QUICKSCREEN
718 case ACTION_STD_QUICKSCREEN:
719 /* don't enter f2 from plugin browser */
720 if (*tc.dirfilter < NUM_FILTER_MODES)
722 if (quick_screen_quick(button))
723 reload_dir = true;
724 restore = true;
726 #ifdef HAVE_TAGCACHE
727 id3db = check_changed_id3mode(id3db);
728 reload_dir = true;
729 #endif
731 break;
732 #endif
733 #ifdef BUTTON_F3
734 case ACTION_F3:
735 /* don't enter f3 from plugin browser */
736 if (*tc.dirfilter < NUM_FILTER_MODES)
738 if (quick_screen_f3(BUTTON_F3))
739 reload_dir = true;
740 restore = true;
742 break;
743 #endif
745 case ACTION_STD_CONTEXT:
747 int onplay_result;
748 int attr = 0;
750 if(!numentries)
751 onplay_result = onplay(NULL, 0, curr_context);
752 else {
753 #ifdef HAVE_TAGCACHE
754 if (id3db)
756 if (tagtree_get_attr(&tc) == TREE_ATTR_MPA)
758 attr = TREE_ATTR_MPA;
759 tagtree_get_filename(&tc, buf, sizeof(buf));
761 else
762 attr = ATTR_DIRECTORY;
764 else
765 #endif
767 attr = dircache[tc.selected_item].attr;
769 if (currdir[1]) /* Not in / */
770 snprintf(buf, sizeof buf, "%s/%s",
771 currdir,
772 dircache[tc.selected_item].name);
773 else /* In / */
774 snprintf(buf, sizeof buf, "/%s",
775 dircache[tc.selected_item].name);
777 onplay_result = onplay(buf, attr, curr_context);
779 switch (onplay_result)
781 case ONPLAY_MAINMENU:
782 return GO_TO_ROOT;
784 case ONPLAY_OK:
785 restore = true;
786 break;
788 case ONPLAY_RELOAD_DIR:
789 reload_dir = true;
790 break;
792 case ONPLAY_START_PLAY:
793 return GO_TO_WPS;
794 break;
796 break;
799 case ACTION_NONE:
800 if (thumbnail_time != -1 &&
801 TIME_AFTER(current_tick, thumbnail_time))
802 { /* a delayed hovering thumbnail is due now */
803 int res;
804 int attr;
805 char* name;
807 #ifdef HAVE_TAGCACHE
808 if (id3db)
810 attr = tagtree_get_attr(&tc);
811 name = tagtree_get_entry(&tc, lasti)->name;
813 else
814 #endif
816 attr = dircache[lasti].attr;
817 name = dircache[lasti].name;
820 if (attr & ATTR_DIRECTORY)
822 DEBUGF("Playing directory thumbnail: %s", currdir);
823 res = ft_play_dirname(name);
824 if (res < 0) /* failed, not existing */
825 { /* say the number instead, as a fallback */
826 talk_id(VOICE_DIR, false);
827 talk_number(lasti+1, true);
830 else
832 DEBUGF("Playing file thumbnail: %s/%s%s\n",
833 currdir, name,
834 file_thumbnail_ext);
835 /* no fallback necessary, we knew in advance
836 that the file exists */
837 ft_play_filename(currdir, name);
839 thumbnail_time = -1; /* job done */
841 gui_syncstatusbar_draw(&statusbars, false);
842 break;
844 #ifdef HAVE_HOTSWAP
845 case SYS_FS_CHANGED:
846 #ifdef HAVE_TAGCACHE
847 if (!id3db)
848 #endif
849 reload_dir = true;
850 /* The 'dir no longer valid' situation will be caught later
851 * by checking the showdir() result. */
852 break;
853 #endif
855 default:
856 if (default_event_handler(button) == SYS_USB_CONNECTED)
858 if(*tc.dirfilter > NUM_FILTER_MODES)
859 /* leave sub-browsers after usb, doing otherwise
860 might be confusing to the user */
861 exit_func = true;
862 else
863 reload_dir = true;
865 break;
867 if (start_wps)
868 return GO_TO_WPS;
869 if ( button )
871 ata_spin();
875 check_rescan:
876 /* do we need to rescan dir? */
877 if (reload_dir || reload_root ||
878 lastfilter != *tc.dirfilter ||
879 lastsortcase != global_settings.sort_case)
881 if ( reload_root ) {
882 strcpy(currdir, "/");
883 tc.dirlevel = 0;
884 #ifdef HAVE_TAGCACHE
885 tc.currtable = 0;
886 tc.currextra = 0;
887 lasttable = -1;
888 lastextra = -1;
889 #endif
890 reload_root = false;
893 if (! reload_dir )
895 gui_synclist_select_item(&tree_lists, 0);
896 gui_synclist_draw(&tree_lists);
897 tc.selected_item = 0;
898 lastdir[0] = 0;
901 lastfilter = *tc.dirfilter;
902 lastsortcase = global_settings.sort_case;
903 restore = true;
906 if (exit_func)
907 return GO_TO_PREVIOUS;
909 if (restore || reload_dir) {
910 /* restore display */
911 numentries = update_dir();
912 if (currdir[1] && (numentries < 0))
913 { /* not in root and reload failed */
914 reload_root = true; /* try root */
915 reload_dir = false;
916 goto check_rescan;
918 need_update = true;
919 reload_dir = false;
922 if(need_update) {
923 tc.selected_item = gui_synclist_get_sel_pos(&tree_lists);
924 need_update=false;
925 if ( numentries > 0 ) {
926 /* Voice the file if changed */
927 if(lasti != tc.selected_item || restore) {
928 int attr;
929 char* name;
931 lasti = tc.selected_item;
932 thumbnail_time = -1; /* Cancel whatever we were
933 about to say */
935 #ifdef HAVE_TAGCACHE
936 if (id3db)
938 attr = tagtree_get_attr(&tc);
939 name = tagtree_get_entry(&tc, tc.selected_item)->name;
941 else
942 #endif
944 attr = dircache[tc.selected_item].attr;
945 name = dircache[tc.selected_item].name;
948 /* Directory? */
949 if (attr & ATTR_DIRECTORY)
951 /* play directory thumbnail */
952 switch (global_settings.talk_dir) {
953 case 1: /* dirs as numbers */
954 talk_id(VOICE_DIR, false);
955 talk_number(tc.selected_item+1, true);
956 break;
958 case 2: /* dirs spelled */
959 talk_spell(name, false);
960 break;
962 case 3: /* thumbnail clip */
963 /* "schedule" a thumbnail, to have a little
964 delay */
965 thumbnail_time = current_tick + HOVER_DELAY;
966 break;
968 default:
969 break;
972 else /* file */
974 switch (global_settings.talk_file) {
975 case 1: /* files as numbers */
976 ft_play_filenumber(
977 tc.selected_item-tc.dirsindir+1,
978 attr & TREE_ATTR_MASK);
979 break;
981 case 2: /* files spelled */
982 talk_spell(name, false);
983 break;
985 case 3: /* thumbnail clip */
986 /* "schedule" a thumbnail, to have a little
987 delay */
988 if (attr & TREE_ATTR_THUMBNAIL)
989 thumbnail_time = current_tick + HOVER_DELAY;
990 else
991 /* spell the number as fallback */
992 talk_spell(name, false);
993 break;
995 default:
996 break;
1003 action_signalscreenchange();
1004 return true;
1007 static int plsize = 0;
1008 static long pltick;
1009 static bool add_dir(char* dirname, int len, int fd)
1011 bool abort = false;
1012 DIRCACHED* dir;
1014 /* check for user abort */
1015 if (action_userabort(TIMEOUT_NOBLOCK))
1016 return true;
1018 dir = opendir_cached(dirname);
1019 if(!dir)
1020 return true;
1022 while (true) {
1023 struct dircache_entry *entry;
1025 entry = readdir_cached(dir);
1026 if (!entry)
1027 break;
1028 if (entry->attribute & ATTR_DIRECTORY) {
1029 int dirlen = strlen(dirname);
1030 bool result;
1032 if (!strcmp((char *)entry->d_name, ".") ||
1033 !strcmp((char *)entry->d_name, ".."))
1034 continue;
1036 if (dirname[1])
1037 snprintf(dirname+dirlen, len-dirlen, "/%s", entry->d_name);
1038 else
1039 snprintf(dirname, len, "/%s", entry->d_name);
1041 result = add_dir(dirname, len, fd);
1042 dirname[dirlen] = '\0';
1043 if (result) {
1044 abort = true;
1045 break;
1048 else {
1049 int x = strlen((char *)entry->d_name);
1050 unsigned int i;
1051 char *cp = strrchr((char *)entry->d_name,'.');
1053 if (cp) {
1054 cp++;
1056 /* add all supported audio files to playlists */
1057 for (i=0; i < sizeof(filetypes)/sizeof(struct filetype); i++) {
1058 if (filetypes[i].tree_attr == TREE_ATTR_MPA) {
1059 if (!strcasecmp(cp, filetypes[i].extension)) {
1060 char buf[8];
1061 int i;
1062 write(fd, dirname, strlen(dirname));
1063 write(fd, "/", 1);
1064 write(fd, entry->d_name, x);
1065 write(fd, "\n", 1);
1067 plsize++;
1068 if(TIME_AFTER(current_tick, pltick+HZ/4)) {
1069 pltick = current_tick;
1071 snprintf(buf, sizeof buf, "%d", plsize);
1072 #ifdef HAVE_LCD_BITMAP
1073 FOR_NB_SCREENS(i)
1075 screens[i].puts(0, 4, (unsigned char *)buf);
1076 gui_textarea_update(&screens[i]);
1078 #else
1079 x = 10;
1080 if (plsize > 999)
1081 x=7;
1082 else {
1083 if (plsize > 99)
1084 x=8;
1085 else {
1086 if (plsize > 9)
1087 x=9;
1090 FOR_NB_SCREENS(i) {
1091 screens[i].puts(x,0,buf);
1093 #endif
1095 break;
1102 closedir_cached(dir);
1104 return abort;
1107 bool create_playlist(void)
1109 int fd;
1110 int i;
1111 char filename[MAX_PATH];
1113 pltick = current_tick;
1115 snprintf(filename, sizeof filename, "%s.m3u8",
1116 tc.currdir[1] ? tc.currdir : "/root");
1117 FOR_NB_SCREENS(i)
1119 gui_textarea_clear(&screens[i]);
1120 screens[i].puts(0, 0, str(LANG_CREATING));
1121 screens[i].puts_scroll(0, 1, (unsigned char *)filename);
1122 #if defined(HAVE_LCD_BITMAP) || defined(SIMULATOR)
1123 gui_textarea_update(&screens[i]);
1124 #endif
1126 fd = creat(filename);
1127 if (fd < 0)
1128 return false;
1130 trigger_cpu_boost();
1132 snprintf(filename, sizeof(filename), "%s",
1133 tc.currdir[1] ? tc.currdir : "/");
1134 plsize = 0;
1135 add_dir(filename, sizeof(filename), fd);
1136 close(fd);
1138 sleep(HZ);
1140 return true;
1143 int rockbox_browse(const char *root, int dirfilter)
1145 int ret_val = 0;
1146 int *last_filter = tc.dirfilter;
1147 tc.dirfilter = &dirfilter;
1149 reload_dir = true;
1150 if (dirfilter >= NUM_FILTER_MODES)
1152 static struct tree_context backup;
1153 int last_context;
1155 backup = tc;
1156 tc.dirlevel = 0;
1157 memcpy(tc.currdir, root, sizeof(tc.currdir));
1158 start_wps = false;
1159 last_context = curr_context;
1161 ret_val = dirbrowse();
1162 tc = backup;
1163 curr_context = last_context;
1165 else
1167 static char buf[MAX_PATH];
1168 if (dirfilter != SHOW_ID3DB)
1169 tc.dirfilter = &global_settings.dirfilter;
1170 strcpy(buf,root);
1171 set_current_file(buf);
1172 ret_val = dirbrowse();
1174 tc.dirfilter = last_filter;
1175 return ret_val;
1178 void tree_init(void)
1180 /* We copy the settings value in case it is changed by the user. We can't
1181 use it until the next reboot. */
1182 max_files = global_settings.max_files_in_dir;
1184 /* initialize tree context struct */
1185 memset(&tc, 0, sizeof(tc));
1186 tc.dirfilter = &global_settings.dirfilter;
1188 tc.name_buffer_size = AVERAGE_FILENAME_LENGTH * max_files;
1189 tc.name_buffer = buffer_alloc(tc.name_buffer_size);
1191 tc.dircache_size = max_files * sizeof(struct entry);
1192 tc.dircache = buffer_alloc(tc.dircache_size);
1195 void bookmark_play(char *resume_file, int index, int offset, int seed,
1196 char *filename)
1198 int i;
1199 char* suffix = strrchr(resume_file, '.');
1201 if (suffix != NULL &&
1202 (!strcasecmp(suffix, ".m3u") || !strcasecmp(suffix, ".m3u8")))
1204 /* Playlist playback */
1205 char* slash;
1206 /* check that the file exists */
1207 int fd = open(resume_file, O_RDONLY);
1208 if(fd<0)
1209 return;
1210 close(fd);
1212 slash = strrchr(resume_file,'/');
1213 if (slash)
1215 char* cp;
1216 *slash=0;
1218 cp=resume_file;
1219 if (!cp[0])
1220 cp="/";
1222 if (playlist_create(cp, slash+1) != -1)
1224 if (global_settings.playlist_shuffle)
1225 playlist_shuffle(seed, -1);
1226 playlist_start(index,offset);
1228 *slash='/';
1231 else
1233 /* Directory playback */
1234 lastdir[0]='\0';
1235 if (playlist_create(resume_file, NULL) != -1)
1237 resume_directory(resume_file);
1238 if (global_settings.playlist_shuffle)
1239 playlist_shuffle(seed, -1);
1241 /* Check if the file is at the same spot in the directory,
1242 else search for it */
1243 if ((strcmp(strrchr(playlist_peek(index),'/') + 1,
1244 filename)))
1246 for ( i=0; i < playlist_amount(); i++ )
1248 if ((strcmp(strrchr(playlist_peek(i),'/') + 1,
1249 filename)) == 0)
1250 break;
1252 if (i < playlist_amount())
1253 index = i;
1254 else
1255 return;
1257 playlist_start(index,offset);
1261 start_wps=true;
1264 int ft_play_filenumber(int pos, int attr)
1266 /* try to find a voice ID for the extension, if known */
1267 unsigned int j;
1268 int ext_id = -1; /* default to none */
1269 for (j=0; j<sizeof(filetypes)/sizeof(*filetypes); j++)
1271 if (attr == filetypes[j].tree_attr)
1273 ext_id = filetypes[j].voiceclip;
1274 break;
1278 talk_id(VOICE_FILE, false);
1279 talk_number(pos, true);
1280 talk_id(ext_id, true);
1281 return 1;
1284 int ft_play_dirname(char* name)
1286 int fd;
1287 char dirname_mp3_filename[MAX_PATH+1];
1289 #if CONFIG_CODEC != SWCODEC
1290 if (audio_status() & AUDIO_STATUS_PLAY)
1291 return 0;
1292 #endif
1294 snprintf(dirname_mp3_filename, sizeof(dirname_mp3_filename), "%s/%s/%s",
1295 tc.currdir[1] ? tc.currdir : "" , name,
1296 dir_thumbnail_name);
1298 DEBUGF("Checking for %s\n", dirname_mp3_filename);
1300 fd = open(dirname_mp3_filename, O_RDONLY);
1301 if (fd < 0)
1303 DEBUGF("Failed to find: %s\n", dirname_mp3_filename);
1304 return -1;
1307 close(fd);
1309 DEBUGF("Found: %s\n", dirname_mp3_filename);
1311 talk_file(dirname_mp3_filename, false);
1312 return 1;
1315 void ft_play_filename(char *dir, char *file)
1317 char name_mp3_filename[MAX_PATH+1];
1319 #if CONFIG_CODEC != SWCODEC
1320 if (audio_status() & AUDIO_STATUS_PLAY)
1321 return;
1322 #endif
1324 if (strcasecmp(&file[strlen(file) - strlen(file_thumbnail_ext)],
1325 file_thumbnail_ext))
1326 { /* file has no .talk extension */
1327 snprintf(name_mp3_filename, sizeof(name_mp3_filename),
1328 "%s/%s%s", dir, file, file_thumbnail_ext);
1330 talk_file(name_mp3_filename, false);
1332 else
1333 { /* it already is a .talk file, play this directly */
1334 snprintf(name_mp3_filename, sizeof(name_mp3_filename),
1335 "%s/%s", dir, file);
1336 talk_id(LANG_VOICE_DIR_HOVER, false); /* prefix it */
1337 talk_file(name_mp3_filename, true);
1341 /* These two functions are called by the USB and shutdown handlers */
1342 void tree_flush(void)
1344 scrobbler_shutdown();
1345 #ifdef HAVE_TAGCACHE
1346 tagcache_shutdown();
1347 #endif
1348 playlist_shutdown();
1350 #ifdef HAVE_TC_RAMCACHE
1351 tagcache_unload_ramcache();
1352 #endif
1354 #ifdef HAVE_DIRCACHE
1356 int old_val = global_status.dircache_size;
1357 if (global_settings.dircache)
1359 global_status.dircache_size = dircache_get_cache_size();
1360 # ifdef HAVE_EEPROM_SETTINGS
1361 dircache_save();
1362 # endif
1363 dircache_disable();
1365 else
1367 global_status.dircache_size = 0;
1369 if (old_val != global_status.dircache_size)
1370 status_save();
1372 #endif
1375 void tree_restore(void)
1377 #ifdef HAVE_EEPROM_SETTINGS
1378 firmware_settings.disk_clean = false;
1379 #endif
1381 #ifdef HAVE_TC_RAMCACHE
1382 remove(TAGCACHE_STATEFILE);
1383 #endif
1385 #ifdef HAVE_DIRCACHE
1386 remove(DIRCACHE_FILE);
1387 if (global_settings.dircache)
1389 /* Print "Scanning disk..." to the display. */
1390 int i;
1391 FOR_NB_SCREENS(i)
1393 screens[i].putsxy((LCD_WIDTH/2) -
1394 ((strlen(str(LANG_DIRCACHE_BUILDING)) *
1395 screens[i].char_width)/2),
1396 LCD_HEIGHT-screens[i].char_height*3,
1397 str(LANG_DIRCACHE_BUILDING));
1398 gui_textarea_update(&screens[i]);
1401 dircache_build(global_status.dircache_size);
1403 /* Clean the text when we are done. */
1404 FOR_NB_SCREENS(i)
1406 gui_textarea_clear(&screens[i]);
1409 #endif
1410 #ifdef HAVE_TAGCACHE
1411 tagcache_start_scan();
1412 #endif
1413 scrobbler_init();