FS#6948 - Fix the broken 'follow playlist' feature
[Rockbox.git] / apps / tree.c
blob2b5dba3e1855350327f8d612647a0bce9a9458af
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 static int dirbrowse(void);
167 static int ft_play_filenumber(int pos, int attr);
168 static int ft_play_dirname(char* name);
169 static void ft_play_filename(char *dir, char *file);
172 * removes the extension of filename (if it doesn't start with a .)
173 * puts the result in buffer
175 static char * strip_extension(char * filename, char * buffer)
177 int dotpos;
178 char * dot=strrchr(filename, '.');
179 if(dot!=0 && filename[0]!='.')
181 dotpos = dot-filename;
182 strncpy(buffer, filename, dotpos);
183 buffer[dotpos]='\0';
184 return(buffer);
186 else
187 return(filename);
190 static char * tree_get_filename(int selected_item, void * data, char *buffer)
192 struct tree_context * local_tc=(struct tree_context *)data;
193 char *name;
194 int attr=0;
195 #ifdef HAVE_TAGCACHE
196 bool id3db = *(local_tc->dirfilter) == SHOW_ID3DB;
198 if (id3db)
200 return tagtree_get_entry(&tc, selected_item)->name;
202 else
203 #endif
205 struct entry* dc = local_tc->dircache;
206 struct entry* e = &dc[selected_item];
207 name = e->name;
208 attr = e->attr;
210 /* if any file filter is on, and if it's not a directory,
211 * strip the extension */
213 if ( (*(local_tc->dirfilter) != SHOW_ID3DB) && !(attr & ATTR_DIRECTORY)
214 && (*(local_tc->dirfilter) != SHOW_ALL) )
216 return(strip_extension(name, buffer));
218 return(name);
222 static void tree_get_fileicon(int selected_item, void * data, ICON * icon)
224 struct tree_context * local_tc=(struct tree_context *)data;
225 #ifdef HAVE_TAGCACHE
226 bool id3db = *(local_tc->dirfilter) == SHOW_ID3DB;
227 if (id3db) {
228 *icon = (ICON)tagtree_get_icon(&tc);
230 else
231 #endif
233 struct entry* dc = local_tc->dircache;
234 struct entry* e = &dc[selected_item];
235 *icon = (ICON)filetype_get_icon(e->attr);
239 bool check_rockboxdir(void)
241 DIR *dir = opendir(ROCKBOX_DIR);
242 if(!dir)
243 { /* No need to localise this message.
244 If .rockbox is missing, it wouldn't work anyway */
245 int i;
246 FOR_NB_SCREENS(i)
247 screens[i].clear_display();
248 gui_syncsplash(HZ*2, "No .rockbox directory");
249 FOR_NB_SCREENS(i)
250 screens[i].clear_display();
251 gui_syncsplash(HZ*2, "Installation incomplete");
252 return false;
254 closedir(dir);
255 return true;
258 void browse_root(void)
260 gui_sync_wps_screen_init();
262 check_rockboxdir();
264 strcpy(tc.currdir, "/");
266 #ifdef HAVE_LCD_CHARCELLS
267 int i;
268 FOR_NB_SCREENS(i)
269 screens[i].double_height(false);
270 #endif
271 #ifdef HAS_BUTTONBAR
272 gui_buttonbar_init(&tree_buttonbar);
273 /* since archos only have one screen, no need to create more than that */
274 gui_buttonbar_set_display(&tree_buttonbar, &(screens[SCREEN_MAIN]) );
275 #endif
276 gui_synclist_init(&tree_lists, &tree_get_filename, &tc, false, 1);
277 gui_synclist_set_icon_callback(&tree_lists, &tree_get_fileicon);
278 /* not the best place for this call... but... */
279 root_menu();
282 void tree_get_filetypes(const struct filetype** types, int* count)
284 *types = filetypes;
285 *count = sizeof(filetypes) / sizeof(*filetypes);
288 struct tree_context* tree_get_context(void)
290 return &tc;
293 /* talkbox hovering delay, to avoid immediate disk activity */
294 #define HOVER_DELAY (HZ/2)
296 * Returns the position of a given file in the current directory
297 * returns -1 if not found
299 static int tree_get_file_position(char * filename)
301 int i;
303 /* use lastfile to determine the selected item (default=0) */
304 for (i=0; i < tc.filesindir; i++)
306 struct entry* dc = tc.dircache;
307 struct entry* e = &dc[i];
308 if (!strcasecmp(e->name, filename))
309 return(i);
311 return(-1);/* no file can match, returns undefined */
315 * Called when a new dir is loaded (for example when returning from other apps ...)
316 * also completely redraws the tree
318 static int update_dir(void)
320 bool changed = false;
321 #ifdef HAVE_TAGCACHE
322 bool id3db = *tc.dirfilter == SHOW_ID3DB;
323 /* Checks for changes */
324 if (id3db) {
325 if (tc.currtable != lasttable ||
326 tc.currextra != lastextra ||
327 tc.firstpos != lastfirstpos ||
328 reload_dir)
330 if (tagtree_load(&tc) < 0)
331 return -1;
333 lasttable = tc.currtable;
334 lastextra = tc.currextra;
335 lastfirstpos = tc.firstpos;
336 changed = true;
339 else
340 #endif
342 /* if the tc.currdir has been changed, reload it ...*/
343 if (strncmp(tc.currdir, lastdir, sizeof(lastdir)) || reload_dir) {
345 if (ft_load(&tc, NULL) < 0)
346 return -1;
347 strcpy(lastdir, tc.currdir);
348 changed = true;
351 /* if selected item is undefined */
352 if (tc.selected_item == -1)
354 /* use lastfile to determine the selected item */
355 tc.selected_item = tree_get_file_position(lastfile);
357 /* If the file doesn't exists, select the first one (default) */
358 if(tc.selected_item < 0)
359 tc.selected_item = 0;
360 changed = true;
362 if (changed)
365 #ifdef HAVE_TAGCACHE
366 !id3db &&
367 #endif
368 (tc.dirfull ||
369 tc.filesindir == global_settings.max_files_in_dir) )
371 gui_syncsplash(HZ, str(LANG_SHOWDIR_BUFFER_FULL));
374 #ifdef HAVE_TAGCACHE
375 if (id3db)
377 if (global_settings.show_path_in_browser == SHOW_PATH_FULL
378 || global_settings.show_path_in_browser == SHOW_PATH_CURRENT)
380 gui_synclist_set_title(&tree_lists, tagtree_get_title(&tc),
381 filetype_get_icon(ATTR_DIRECTORY));
383 else
385 /* Must clear the title as the list is reused */
386 gui_synclist_set_title(&tree_lists, NULL, NOICON);
389 else
390 #endif
392 if (global_settings.show_path_in_browser == SHOW_PATH_FULL)
394 gui_synclist_set_title(&tree_lists, tc.currdir,
395 filetype_get_icon(ATTR_DIRECTORY));
397 else if (global_settings.show_path_in_browser == SHOW_PATH_CURRENT)
399 char *title = strrchr(tc.currdir, '/') + 1;
400 if (*title == '\0')
402 /* Display "Files" for the root dir */
403 gui_synclist_set_title(&tree_lists, str(LANG_DIR_BROWSER),
404 filetype_get_icon(ATTR_DIRECTORY));
406 else
407 gui_synclist_set_title(&tree_lists, title,
408 filetype_get_icon(ATTR_DIRECTORY));
410 else
412 /* Must clear the title as the list is reused */
413 gui_synclist_set_title(&tree_lists, NULL, NOICON);
417 gui_synclist_set_nb_items(&tree_lists, tc.filesindir);
418 gui_synclist_set_icon_callback(&tree_lists, tree_get_fileicon);
419 if( tc.selected_item >= tc.filesindir)
420 tc.selected_item=tc.filesindir-1;
422 gui_synclist_select_item(&tree_lists, tc.selected_item);
423 #ifdef HAS_BUTTONBAR
424 if (global_settings.buttonbar) {
425 if (*tc.dirfilter < NUM_FILTER_MODES)
426 gui_buttonbar_set(&tree_buttonbar, str(LANG_SYSFONT_DIRBROWSE_F1),
427 str(LANG_SYSFONT_DIRBROWSE_F2),
428 str(LANG_SYSFONT_DIRBROWSE_F3));
429 else
430 gui_buttonbar_set(&tree_buttonbar, "<<<", "", "");
431 gui_buttonbar_draw(&tree_buttonbar);
433 #endif
434 gui_synclist_draw(&tree_lists);
435 gui_syncstatusbar_draw(&statusbars, true);
436 return tc.filesindir;
439 /* load tracks from specified directory to resume play */
440 void resume_directory(const char *dir)
442 #ifdef HAVE_TAGCACHE
443 bool id3db = *tc.dirfilter == SHOW_ID3DB;
444 #endif
446 if (ft_load(&tc, dir) < 0)
447 return;
448 lastdir[0] = 0;
450 ft_build_playlist(&tc, 0);
452 #ifdef HAVE_TAGCACHE
453 if (id3db)
454 tagtree_load(&tc);
455 #endif
458 /* Returns the current working directory and also writes cwd to buf if
459 non-NULL. In case of error, returns NULL. */
460 char *getcwd(char *buf, int size)
462 if (!buf)
463 return tc.currdir;
464 else if (size > 0)
466 strncpy(buf, tc.currdir, size);
467 return buf;
469 else
470 return NULL;
473 /* Force a reload of the directory next time directory browser is called */
474 void reload_directory(void)
476 reload_dir = true;
479 void get_current_file(char* buffer, int buffer_len)
481 #ifdef HAVE_TAGCACHE
482 /* in ID3DB mode it is a bad idea to call this function */
483 /* (only happens with `follow playlist') */
484 if( *tc.dirfilter == SHOW_ID3DB )
485 return;
486 #endif
488 struct entry* dc = tc.dircache;
489 struct entry* e = &dc[tc.selected_item];
490 snprintf(buffer, buffer_len, "%s/%s", getcwd(NULL,0),
491 e->name);
494 /* Selects a file and update tree context properly */
495 static void set_current_file(char *path)
497 char *name;
498 int i;
500 #ifdef HAVE_TAGCACHE
501 /* in ID3DB mode it is a bad idea to call this function */
502 /* (only happens with `follow playlist') */
503 if( *tc.dirfilter == SHOW_ID3DB )
504 return;
505 #endif
507 /* separate directory from filename */
508 /* gets the directory's name and put it into tc.currdir */
509 name = strrchr(path+1,'/');
510 if (name)
512 *name = 0;
513 strcpy(tc.currdir, path);
514 *name = '/';
515 name++;
517 else
519 strcpy(tc.currdir, "/");
520 name = path+1;
523 strcpy(lastfile, name);
526 /* If we changed dir we must recalculate the dirlevel
527 and adjust the selected history properly */
528 if (strncmp(tc.currdir,lastdir,sizeof(lastdir)))
530 tc.dirlevel = 0;
531 tc.selected_item_history[tc.dirlevel] = -1;
533 /* use '/' to calculate dirlevel */
534 for (i = 1; path[i] != '\0'; i++)
536 if (path[i] == '/')
538 tc.dirlevel++;
539 tc.selected_item_history[tc.dirlevel] = -1;
543 if (ft_load(&tc, NULL) >= 0)
545 tc.selected_item = tree_get_file_position(lastfile);
550 /* main loop, handles key events */
551 static int dirbrowse()
553 int numentries=0;
554 char buf[MAX_PATH];
555 int lasti = -1;
556 unsigned button, returned_button;
557 bool reload_root = false;
558 int lastfilter = *tc.dirfilter;
559 bool lastsortcase = global_settings.sort_case;
560 bool need_update = true;
561 bool exit_func = false;
562 long thumbnail_time = -1; /* for delaying a thumbnail */
563 long last_cancel = 0;
565 char* currdir = tc.currdir; /* just a shortcut */
566 #ifdef HAVE_TAGCACHE
567 bool id3db = *tc.dirfilter == SHOW_ID3DB;
569 if (id3db)
570 curr_context=CONTEXT_ID3DB;
571 else
572 #endif
573 curr_context=CONTEXT_TREE;
574 if (tc.selected_item < 0)
575 tc.selected_item = 0;
576 #ifdef HAVE_TAGCACHE
577 tc.firstpos=0;
578 lasttable = -1;
579 lastextra = -1;
580 lastfirstpos = 0;
581 #endif
583 start_wps = false;
584 numentries = update_dir();
585 if (numentries == -1)
586 return false; /* currdir is not a directory */
588 if (*tc.dirfilter > NUM_FILTER_MODES && numentries==0)
590 gui_syncsplash(HZ*2, str(LANG_NO_FILES));
591 return false; /* No files found for rockbox_browser() */
594 while(1) {
595 struct entry *dircache = tc.dircache;
596 bool restore = false;
597 if (tc.dirlevel < 0)
598 tc.dirlevel = 0; /* shouldnt be needed.. this code needs work! */
599 #ifdef BOOTFILE
600 if (boot_changed) {
601 char *lines[]={str(LANG_BOOT_CHANGED), str(LANG_REBOOT_NOW)};
602 struct text_message message={lines, 2};
603 if(gui_syncyesno_run(&message, NULL, NULL)==YESNO_YES)
604 rolo_load("/" BOOTFILE);
605 restore = true;
606 boot_changed = false;
608 #endif
609 button = get_action(CONTEXT_TREE,HZ/5);
610 returned_button = gui_synclist_do_button(&tree_lists, button,LIST_WRAP_UNLESS_HELD);
611 if (returned_button)
612 need_update = true;
613 if (returned_button == ACTION_STD_CANCEL)
614 button = ACTION_STD_CANCEL;
616 tc.selected_item = gui_synclist_get_sel_pos(&tree_lists);
617 switch ( button ) {
618 case ACTION_STD_OK:
619 /* nothing to do if no files to display */
620 if ( numentries == 0 )
621 break;
623 #ifdef HAVE_TAGCACHE
624 switch (id3db?tagtree_enter(&tc):ft_enter(&tc))
625 #else
626 switch (ft_enter(&tc))
627 #endif
629 case 1: reload_dir = true; break;
630 case 2: start_wps = true; break;
631 case 3: exit_func = true; break;
632 default: break;
634 restore = true;
635 break;
637 case ACTION_STD_CANCEL:
638 if (*tc.dirfilter > NUM_FILTER_MODES && tc.dirlevel < 1) {
639 exit_func = true;
640 break;
642 if ((*tc.dirfilter == SHOW_ID3DB && tc.dirlevel == 0) ||
643 ((*tc.dirfilter != SHOW_ID3DB && !strcmp(currdir,"/"))))
645 if (last_cancel && TIME_BEFORE(current_tick, last_cancel+HZ/2))
647 last_cancel = 0;
648 action_signalscreenchange(); /* eat the cancel presses */
649 break;
651 else
652 return GO_TO_ROOT;
654 last_cancel = current_tick;
656 #ifdef HAVE_TAGCACHE
657 if (id3db)
658 tagtree_exit(&tc);
659 else
660 #endif
661 if (ft_exit(&tc) == 3)
662 exit_func = true;
664 restore = true;
665 break;
667 case ACTION_TREE_STOP:
668 if (*tc.dirfilter < NUM_FILTER_MODES)
670 /* Stop the music if it is playing */
671 if(audio_status()) {
672 if (!global_settings.party_mode) {
673 if (global_settings.fade_on_stop)
674 fade(0);
675 bookmark_autobookmark();
676 audio_stop();
679 #if CONFIG_CHARGING && \
680 (CONFIG_KEYPAD == RECORDER_PAD) && !defined(HAVE_SW_POWEROFF)
681 else {
682 if (!charger_inserted()) {
683 if(shutdown_screen())
684 reload_dir = true;
685 } else {
686 charging_splash();
688 restore = true;
690 #endif
692 #if CONFIG_CHARGING && !defined(HAVE_POWEROFF_WHILE_CHARGING)
694 static int last_off = 0;
695 if (current_tick - last_off < 50) {
696 if (charger_inserted()) {
697 charging_splash();
698 restore = true;
701 last_off = current_tick;
703 #endif
704 break; /* case ACTION_TREE_STOP: */
705 case ACTION_STD_MENU:
706 return GO_TO_ROOT;
707 break;
709 case ACTION_TREE_WPS:
710 return GO_TO_PREVIOUS_MUSIC;
711 break;
712 #ifdef HAVE_QUICKSCREEN
713 case ACTION_STD_QUICKSCREEN:
714 /* don't enter f2 from plugin browser */
715 if (*tc.dirfilter < NUM_FILTER_MODES)
717 if (quick_screen_quick(button))
718 reload_dir = true;
719 restore = true;
721 break;
722 #endif
723 #ifdef BUTTON_F3
724 case ACTION_F3:
725 /* don't enter f3 from plugin browser */
726 if (*tc.dirfilter < NUM_FILTER_MODES)
728 if (quick_screen_f3(BUTTON_F3))
729 reload_dir = true;
730 restore = true;
732 break;
733 #endif
735 case ACTION_STD_CONTEXT:
737 int onplay_result;
738 int attr = 0;
740 if(!numentries)
741 onplay_result = onplay(NULL, 0, curr_context);
742 else {
743 #ifdef HAVE_TAGCACHE
744 if (id3db)
746 if (tagtree_get_attr(&tc) == TREE_ATTR_MPA)
748 attr = TREE_ATTR_MPA;
749 tagtree_get_filename(&tc, buf, sizeof(buf));
751 else
752 attr = ATTR_DIRECTORY;
754 else
755 #endif
757 attr = dircache[tc.selected_item].attr;
759 if (currdir[1]) /* Not in / */
760 snprintf(buf, sizeof buf, "%s/%s",
761 currdir,
762 dircache[tc.selected_item].name);
763 else /* In / */
764 snprintf(buf, sizeof buf, "/%s",
765 dircache[tc.selected_item].name);
767 onplay_result = onplay(buf, attr, curr_context);
769 switch (onplay_result)
771 case ONPLAY_MAINMENU:
772 return GO_TO_ROOT;
774 case ONPLAY_OK:
775 restore = true;
776 break;
778 case ONPLAY_RELOAD_DIR:
779 reload_dir = true;
780 break;
782 case ONPLAY_START_PLAY:
783 return GO_TO_WPS;
784 break;
786 break;
789 case ACTION_NONE:
790 if (thumbnail_time != -1 &&
791 TIME_AFTER(current_tick, thumbnail_time))
792 { /* a delayed hovering thumbnail is due now */
793 int res;
794 int attr;
795 char* name;
797 #ifdef HAVE_TAGCACHE
798 if (id3db)
800 attr = tagtree_get_attr(&tc);
801 name = tagtree_get_entry(&tc, lasti)->name;
803 else
804 #endif
806 attr = dircache[lasti].attr;
807 name = dircache[lasti].name;
810 if (attr & ATTR_DIRECTORY)
812 DEBUGF("Playing directory thumbnail: %s", currdir);
813 res = ft_play_dirname(name);
814 if (res < 0) /* failed, not existing */
815 { /* say the number instead, as a fallback */
816 talk_id(VOICE_DIR, false);
817 talk_number(lasti+1, true);
820 else
822 DEBUGF("Playing file thumbnail: %s/%s%s\n",
823 currdir, name,
824 file_thumbnail_ext);
825 /* no fallback necessary, we knew in advance
826 that the file exists */
827 ft_play_filename(currdir, name);
829 thumbnail_time = -1; /* job done */
831 gui_syncstatusbar_draw(&statusbars, false);
832 break;
834 #ifdef HAVE_HOTSWAP
835 case SYS_FS_CHANGED:
836 #ifdef HAVE_TAGCACHE
837 if (!id3db)
838 #endif
839 reload_dir = true;
840 /* The 'dir no longer valid' situation will be caught later
841 * by checking the showdir() result. */
842 break;
843 #endif
845 default:
846 if (default_event_handler(button) == SYS_USB_CONNECTED)
848 if(*tc.dirfilter > NUM_FILTER_MODES)
849 /* leave sub-browsers after usb, doing otherwise
850 might be confusing to the user */
851 exit_func = true;
852 else
853 reload_dir = true;
855 break;
857 if (start_wps)
858 return GO_TO_WPS;
859 if ( button )
861 ata_spin();
865 check_rescan:
866 /* do we need to rescan dir? */
867 if (reload_dir || reload_root ||
868 lastfilter != *tc.dirfilter ||
869 lastsortcase != global_settings.sort_case)
871 if ( reload_root ) {
872 strcpy(currdir, "/");
873 tc.dirlevel = 0;
874 #ifdef HAVE_TAGCACHE
875 tc.currtable = 0;
876 tc.currextra = 0;
877 lasttable = -1;
878 lastextra = -1;
879 #endif
880 reload_root = false;
883 if (! reload_dir )
885 gui_synclist_select_item(&tree_lists, 0);
886 gui_synclist_draw(&tree_lists);
887 tc.selected_item = 0;
888 lastdir[0] = 0;
891 lastfilter = *tc.dirfilter;
892 lastsortcase = global_settings.sort_case;
893 restore = true;
896 if (exit_func)
897 return GO_TO_PREVIOUS;
899 if (restore || reload_dir) {
900 /* restore display */
901 numentries = update_dir();
902 if (currdir[1] && (numentries < 0))
903 { /* not in root and reload failed */
904 reload_root = true; /* try root */
905 reload_dir = false;
906 goto check_rescan;
908 need_update = true;
909 reload_dir = false;
912 if(need_update) {
913 need_update=false;
914 if ( numentries > 0 ) {
915 /* Voice the file if changed */
916 if(lasti != tc.selected_item || restore) {
917 int attr;
918 char* name;
920 lasti = tc.selected_item;
921 thumbnail_time = -1; /* Cancel whatever we were
922 about to say */
924 #ifdef HAVE_TAGCACHE
925 if (id3db)
927 attr = tagtree_get_attr(&tc);
928 name = tagtree_get_entry(&tc, tc.selected_item)->name;
930 else
931 #endif
933 attr = dircache[tc.selected_item].attr;
934 name = dircache[tc.selected_item].name;
937 /* Directory? */
938 if (attr & ATTR_DIRECTORY)
940 /* play directory thumbnail */
941 switch (global_settings.talk_dir) {
942 case 1: /* dirs as numbers */
943 talk_id(VOICE_DIR, false);
944 talk_number(tc.selected_item+1, true);
945 break;
947 case 2: /* dirs spelled */
948 talk_spell(name, false);
949 break;
951 case 3: /* thumbnail clip */
952 /* "schedule" a thumbnail, to have a little
953 delay */
954 thumbnail_time = current_tick + HOVER_DELAY;
955 break;
957 default:
958 break;
961 else /* file */
963 switch (global_settings.talk_file) {
964 case 1: /* files as numbers */
965 ft_play_filenumber(
966 tc.selected_item-tc.dirsindir+1,
967 attr & TREE_ATTR_MASK);
968 break;
970 case 2: /* files spelled */
971 talk_spell(name, false);
972 break;
974 case 3: /* thumbnail clip */
975 /* "schedule" a thumbnail, to have a little
976 delay */
977 if (attr & TREE_ATTR_THUMBNAIL)
978 thumbnail_time = current_tick + HOVER_DELAY;
979 else
980 /* spell the number as fallback */
981 talk_spell(name, false);
982 break;
984 default:
985 break;
992 action_signalscreenchange();
993 return true;
996 static int plsize = 0;
997 static long pltick;
998 static bool add_dir(char* dirname, int len, int fd)
1000 bool abort = false;
1001 DIRCACHED* dir;
1003 /* check for user abort */
1004 if (action_userabort(TIMEOUT_NOBLOCK))
1005 return true;
1007 dir = opendir_cached(dirname);
1008 if(!dir)
1009 return true;
1011 while (true) {
1012 struct dircache_entry *entry;
1014 entry = readdir_cached(dir);
1015 if (!entry)
1016 break;
1017 if (entry->attribute & ATTR_DIRECTORY) {
1018 int dirlen = strlen(dirname);
1019 bool result;
1021 if (!strcmp((char *)entry->d_name, ".") ||
1022 !strcmp((char *)entry->d_name, ".."))
1023 continue;
1025 if (dirname[1])
1026 snprintf(dirname+dirlen, len-dirlen, "/%s", entry->d_name);
1027 else
1028 snprintf(dirname, len, "/%s", entry->d_name);
1030 result = add_dir(dirname, len, fd);
1031 dirname[dirlen] = '\0';
1032 if (result) {
1033 abort = true;
1034 break;
1037 else {
1038 int x = strlen((char *)entry->d_name);
1039 unsigned int i;
1040 char *cp = strrchr((char *)entry->d_name,'.');
1042 if (cp) {
1043 cp++;
1045 /* add all supported audio files to playlists */
1046 for (i=0; i < sizeof(filetypes)/sizeof(struct filetype); i++) {
1047 if (filetypes[i].tree_attr == TREE_ATTR_MPA) {
1048 if (!strcasecmp(cp, filetypes[i].extension)) {
1049 char buf[8];
1050 int i;
1051 write(fd, dirname, strlen(dirname));
1052 write(fd, "/", 1);
1053 write(fd, entry->d_name, x);
1054 write(fd, "\n", 1);
1056 plsize++;
1057 if(TIME_AFTER(current_tick, pltick+HZ/4)) {
1058 pltick = current_tick;
1060 snprintf(buf, sizeof buf, "%d", plsize);
1061 #ifdef HAVE_LCD_BITMAP
1062 FOR_NB_SCREENS(i)
1064 screens[i].puts(0, 4, (unsigned char *)buf);
1065 gui_textarea_update(&screens[i]);
1067 #else
1068 x = 10;
1069 if (plsize > 999)
1070 x=7;
1071 else {
1072 if (plsize > 99)
1073 x=8;
1074 else {
1075 if (plsize > 9)
1076 x=9;
1079 FOR_NB_SCREENS(i) {
1080 screens[i].puts(x,0,buf);
1082 #endif
1084 break;
1091 closedir_cached(dir);
1093 return abort;
1096 bool create_playlist(void)
1098 int fd;
1099 int i;
1100 char filename[MAX_PATH];
1102 pltick = current_tick;
1104 snprintf(filename, sizeof filename, "%s.m3u8",
1105 tc.currdir[1] ? tc.currdir : "/root");
1106 FOR_NB_SCREENS(i)
1108 gui_textarea_clear(&screens[i]);
1109 screens[i].puts(0, 0, str(LANG_CREATING));
1110 screens[i].puts_scroll(0, 1, (unsigned char *)filename);
1111 #if defined(HAVE_LCD_BITMAP) || defined(SIMULATOR)
1112 gui_textarea_update(&screens[i]);
1113 #endif
1115 fd = creat(filename);
1116 if (fd < 0)
1117 return false;
1119 trigger_cpu_boost();
1121 snprintf(filename, sizeof(filename), "%s",
1122 tc.currdir[1] ? tc.currdir : "/");
1123 plsize = 0;
1124 add_dir(filename, sizeof(filename), fd);
1125 close(fd);
1127 sleep(HZ);
1129 return true;
1132 int rockbox_browse(const char *root, int dirfilter)
1134 int ret_val = 0;
1135 int *last_filter = tc.dirfilter;
1136 tc.dirfilter = &dirfilter;
1138 reload_dir = true;
1139 if (dirfilter >= NUM_FILTER_MODES)
1141 static struct tree_context backup;
1142 int last_context;
1144 backup = tc;
1145 tc.selected_item = 0;
1146 tc.dirlevel = 0;
1147 memcpy(tc.currdir, root, sizeof(tc.currdir));
1148 start_wps = false;
1149 last_context = curr_context;
1151 ret_val = dirbrowse();
1152 tc = backup;
1153 curr_context = last_context;
1155 else
1157 static char buf[MAX_PATH];
1158 if (dirfilter != SHOW_ID3DB)
1159 tc.dirfilter = &global_settings.dirfilter;
1160 strcpy(buf,root);
1161 set_current_file(buf);
1162 ret_val = dirbrowse();
1164 tc.dirfilter = last_filter;
1165 return ret_val;
1168 void tree_init(void)
1170 /* We copy the settings value in case it is changed by the user. We can't
1171 use it until the next reboot. */
1172 max_files = global_settings.max_files_in_dir;
1174 /* initialize tree context struct */
1175 memset(&tc, 0, sizeof(tc));
1176 tc.dirfilter = &global_settings.dirfilter;
1178 tc.name_buffer_size = AVERAGE_FILENAME_LENGTH * max_files;
1179 tc.name_buffer = buffer_alloc(tc.name_buffer_size);
1181 tc.dircache_size = max_files * sizeof(struct entry);
1182 tc.dircache = buffer_alloc(tc.dircache_size);
1185 void bookmark_play(char *resume_file, int index, int offset, int seed,
1186 char *filename)
1188 int i;
1189 char* suffix = strrchr(resume_file, '.');
1191 if (suffix != NULL &&
1192 (!strcasecmp(suffix, ".m3u") || !strcasecmp(suffix, ".m3u8")))
1194 /* Playlist playback */
1195 char* slash;
1196 /* check that the file exists */
1197 int fd = open(resume_file, O_RDONLY);
1198 if(fd<0)
1199 return;
1200 close(fd);
1202 slash = strrchr(resume_file,'/');
1203 if (slash)
1205 char* cp;
1206 *slash=0;
1208 cp=resume_file;
1209 if (!cp[0])
1210 cp="/";
1212 if (playlist_create(cp, slash+1) != -1)
1214 if (global_settings.playlist_shuffle)
1215 playlist_shuffle(seed, -1);
1216 playlist_start(index,offset);
1218 *slash='/';
1221 else
1223 /* Directory playback */
1224 lastdir[0]='\0';
1225 if (playlist_create(resume_file, NULL) != -1)
1227 resume_directory(resume_file);
1228 if (global_settings.playlist_shuffle)
1229 playlist_shuffle(seed, -1);
1231 /* Check if the file is at the same spot in the directory,
1232 else search for it */
1233 if ((strcmp(strrchr(playlist_peek(index),'/') + 1,
1234 filename)))
1236 for ( i=0; i < playlist_amount(); i++ )
1238 if ((strcmp(strrchr(playlist_peek(i),'/') + 1,
1239 filename)) == 0)
1240 break;
1242 if (i < playlist_amount())
1243 index = i;
1244 else
1245 return;
1247 playlist_start(index,offset);
1251 start_wps=true;
1254 static int ft_play_filenumber(int pos, int attr)
1256 /* try to find a voice ID for the extension, if known */
1257 unsigned int j;
1258 int ext_id = -1; /* default to none */
1259 for (j=0; j<sizeof(filetypes)/sizeof(*filetypes); j++)
1261 if (attr == filetypes[j].tree_attr)
1263 ext_id = filetypes[j].voiceclip;
1264 break;
1268 talk_id(VOICE_FILE, false);
1269 talk_number(pos, true);
1270 talk_id(ext_id, true);
1271 return 1;
1274 static int ft_play_dirname(char* name)
1276 int fd;
1277 char dirname_mp3_filename[MAX_PATH+1];
1279 #if CONFIG_CODEC != SWCODEC
1280 if (audio_status() & AUDIO_STATUS_PLAY)
1281 return 0;
1282 #endif
1284 snprintf(dirname_mp3_filename, sizeof(dirname_mp3_filename), "%s/%s/%s",
1285 tc.currdir[1] ? tc.currdir : "" , name,
1286 dir_thumbnail_name);
1288 DEBUGF("Checking for %s\n", dirname_mp3_filename);
1290 fd = open(dirname_mp3_filename, O_RDONLY);
1291 if (fd < 0)
1293 DEBUGF("Failed to find: %s\n", dirname_mp3_filename);
1294 return -1;
1297 close(fd);
1299 DEBUGF("Found: %s\n", dirname_mp3_filename);
1301 talk_file(dirname_mp3_filename, false);
1302 return 1;
1305 static void ft_play_filename(char *dir, char *file)
1307 char name_mp3_filename[MAX_PATH+1];
1309 #if CONFIG_CODEC != SWCODEC
1310 if (audio_status() & AUDIO_STATUS_PLAY)
1311 return;
1312 #endif
1314 if (strcasecmp(&file[strlen(file) - strlen(file_thumbnail_ext)],
1315 file_thumbnail_ext))
1316 { /* file has no .talk extension */
1317 snprintf(name_mp3_filename, sizeof(name_mp3_filename),
1318 "%s/%s%s", dir, file, file_thumbnail_ext);
1320 talk_file(name_mp3_filename, false);
1322 else
1323 { /* it already is a .talk file, play this directly */
1324 snprintf(name_mp3_filename, sizeof(name_mp3_filename),
1325 "%s/%s", dir, file);
1326 talk_id(LANG_VOICE_DIR_HOVER, false); /* prefix it */
1327 talk_file(name_mp3_filename, true);
1331 /* These two functions are called by the USB and shutdown handlers */
1332 void tree_flush(void)
1334 scrobbler_shutdown();
1335 #ifdef HAVE_TAGCACHE
1336 tagcache_shutdown();
1337 #endif
1338 playlist_shutdown();
1340 #ifdef HAVE_TC_RAMCACHE
1341 tagcache_unload_ramcache();
1342 #endif
1344 #ifdef HAVE_DIRCACHE
1346 int old_val = global_status.dircache_size;
1347 if (global_settings.dircache)
1349 global_status.dircache_size = dircache_get_cache_size();
1350 # ifdef HAVE_EEPROM_SETTINGS
1351 dircache_save();
1352 # endif
1353 dircache_disable();
1355 else
1357 global_status.dircache_size = 0;
1359 if (old_val != global_status.dircache_size)
1360 status_save();
1362 #endif
1365 void tree_restore(void)
1367 #ifdef HAVE_EEPROM_SETTINGS
1368 firmware_settings.disk_clean = false;
1369 #endif
1371 #ifdef HAVE_TC_RAMCACHE
1372 remove(TAGCACHE_STATEFILE);
1373 #endif
1375 #ifdef HAVE_DIRCACHE
1376 remove(DIRCACHE_FILE);
1377 if (global_settings.dircache)
1379 /* Print "Scanning disk..." to the display. */
1380 int i;
1381 FOR_NB_SCREENS(i)
1383 screens[i].putsxy((LCD_WIDTH/2) -
1384 ((strlen(str(LANG_DIRCACHE_BUILDING)) *
1385 screens[i].char_width)/2),
1386 LCD_HEIGHT-screens[i].char_height*3,
1387 str(LANG_DIRCACHE_BUILDING));
1388 gui_textarea_update(&screens[i]);
1391 dircache_build(global_status.dircache_size);
1393 /* Clean the text when we are done. */
1394 FOR_NB_SCREENS(i)
1396 gui_textarea_clear(&screens[i]);
1399 #endif
1400 #ifdef HAVE_TAGCACHE
1401 tagcache_start_scan();
1402 #endif
1403 scrobbler_init();