Small '#ifdef HAVE_TAGCACHE' fix
[Rockbox.git] / apps / tree.c
blobe4237dc59874df39784d37cf3ee4af691b91f3a2
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 void get_current_file(char* buffer, int buffer_len)
479 #ifdef HAVE_TAGCACHE
480 /* in ID3DB mode it is a bad idea to call this function */
481 /* (only happens with `follow playlist') */
482 if( *tc.dirfilter == SHOW_ID3DB )
483 return;
484 #endif
486 struct entry* dc = tc.dircache;
487 struct entry* e = &dc[tc.selected_item];
488 snprintf(buffer, buffer_len, "%s/%s", getcwd(NULL,0),
489 e->name);
492 /* Selects a file and update tree context properly */
493 void set_current_file(char *path)
495 char *name;
496 int i;
498 #ifdef HAVE_TAGCACHE
499 /* in ID3DB mode it is a bad idea to call this function */
500 /* (only happens with `follow playlist') */
501 if( *tc.dirfilter == SHOW_ID3DB )
502 return;
503 #endif
505 /* separate directory from filename */
506 /* gets the directory's name and put it into tc.currdir */
507 name = strrchr(path+1,'/');
508 if (name)
510 *name = 0;
511 strcpy(tc.currdir, path);
512 *name = '/';
513 name++;
515 else
517 strcpy(tc.currdir, "/");
518 name = path+1;
521 strcpy(lastfile, name);
524 /* If we changed dir we must recalculate the dirlevel
525 and adjust the selected history properly */
526 if (strncmp(tc.currdir,lastdir,sizeof(lastdir)))
528 tc.dirlevel = 0;
529 tc.selected_item_history[tc.dirlevel] = -1;
531 /* use '/' to calculate dirlevel */
532 for (i = 1; path[i] != '\0'; i++)
534 if (path[i] == '/')
536 tc.dirlevel++;
537 tc.selected_item_history[tc.dirlevel] = -1;
541 if (ft_load(&tc, NULL) >= 0)
543 tc.selected_item = tree_get_file_position(lastfile);
547 #if defined(HAVE_TAGCACHE) && defined(HAVE_QUICKSCREEN)
548 static bool check_changed_id3mode(bool currmode)
550 if (currmode != (global_settings.dirfilter == SHOW_ID3DB)) {
551 currmode = global_settings.dirfilter == SHOW_ID3DB;
552 if (currmode) {
553 curr_context=CONTEXT_ID3DB;
554 tc.dirlevel = 0;
555 tagtree_load(&tc);
557 else
559 tc.dirlevel = 0;
560 curr_context=CONTEXT_TREE;
561 ft_load(&tc, NULL);
562 reload_dir = true;
565 return currmode;
567 #endif
569 /* main loop, handles key events */
570 int dirbrowse()
572 int numentries=0;
573 char buf[MAX_PATH];
574 int lasti = -1;
575 unsigned button, returned_button;
576 bool reload_root = false;
577 int lastfilter = *tc.dirfilter;
578 bool lastsortcase = global_settings.sort_case;
579 bool need_update = true;
580 bool exit_func = false;
581 long thumbnail_time = -1; /* for delaying a thumbnail */
582 long last_cancel = 0;
584 char* currdir = tc.currdir; /* just a shortcut */
585 #ifdef HAVE_TAGCACHE
586 bool id3db = *tc.dirfilter == SHOW_ID3DB;
588 if (id3db)
589 curr_context=CONTEXT_ID3DB;
590 else
591 #endif
592 curr_context=CONTEXT_TREE;
593 if (tc.selected_item < 0)
594 tc.selected_item = 0;
595 #ifdef HAVE_TAGCACHE
596 tc.firstpos=0;
597 lasttable = -1;
598 lastextra = -1;
599 lastfirstpos = 0;
600 #endif
602 start_wps = false;
603 numentries = update_dir();
604 if (numentries == -1)
605 return false; /* currdir is not a directory */
607 if (*tc.dirfilter > NUM_FILTER_MODES && numentries==0)
609 gui_syncsplash(HZ*2, true, str(LANG_NO_FILES));
610 return false; /* No files found for rockbox_browser() */
613 while(1) {
614 struct entry *dircache = tc.dircache;
615 bool restore = false;
616 if (tc.dirlevel < 0)
617 tc.dirlevel = 0; /* shouldnt be needed.. this code needs work! */
618 #ifdef BOOTFILE
619 if (boot_changed) {
620 char *lines[]={str(LANG_BOOT_CHANGED), str(LANG_REBOOT_NOW)};
621 struct text_message message={lines, 2};
622 if(gui_syncyesno_run(&message, NULL, NULL)==YESNO_YES)
623 rolo_load("/" BOOTFILE);
624 restore = true;
625 boot_changed = false;
627 #endif
628 button = get_action(CONTEXT_TREE,HZ/5);
629 returned_button = gui_synclist_do_button(&tree_lists, button,LIST_WRAP_UNLESS_HELD);
630 if (returned_button)
631 need_update = true;
632 if (returned_button == ACTION_STD_CANCEL)
633 button = ACTION_STD_CANCEL;
635 switch ( button ) {
636 case ACTION_STD_OK:
637 /* nothing to do if no files to display */
638 if ( numentries == 0 )
639 break;
641 #ifdef HAVE_TAGCACHE
642 switch (id3db?tagtree_enter(&tc):ft_enter(&tc))
643 #else
644 switch (ft_enter(&tc))
645 #endif
647 case 1: reload_dir = true; break;
648 case 2: start_wps = true; break;
649 case 3: exit_func = true; break;
650 default: break;
652 restore = true;
653 break;
655 case ACTION_STD_CANCEL:
656 if (*tc.dirfilter > NUM_FILTER_MODES && tc.dirlevel < 1) {
657 exit_func = true;
658 break;
660 if ((*tc.dirfilter == SHOW_ID3DB && tc.dirlevel == 0) ||
661 ((*tc.dirfilter != SHOW_ID3DB && !strcmp(currdir,"/"))))
663 if (last_cancel && TIME_BEFORE(current_tick, last_cancel+HZ/2))
665 last_cancel = 0;
666 action_signalscreenchange(); /* eat the cancel presses */
667 break;
669 else
670 return GO_TO_ROOT;
672 last_cancel = current_tick;
674 #ifdef HAVE_TAGCACHE
675 if (id3db)
676 tagtree_exit(&tc);
677 else
678 #endif
679 if (ft_exit(&tc) == 3)
680 exit_func = true;
682 restore = true;
683 break;
685 case ACTION_TREE_STOP:
686 if (*tc.dirfilter < NUM_FILTER_MODES)
688 /* Stop the music if it is playing */
689 if(audio_status()) {
690 if (!global_settings.party_mode) {
691 if (global_settings.fade_on_stop)
692 fade(0);
693 bookmark_autobookmark();
694 audio_stop();
697 #if CONFIG_CHARGING && \
698 (CONFIG_KEYPAD == RECORDER_PAD) && !defined(HAVE_SW_POWEROFF)
699 else {
700 if (!charger_inserted()) {
701 if(shutdown_screen())
702 reload_dir = true;
703 } else {
704 charging_splash();
706 restore = true;
708 #endif
710 #if CONFIG_CHARGING && !defined(HAVE_POWEROFF_WHILE_CHARGING)
712 static int last_off = 0;
713 if (current_tick - last_off < 50) {
714 if (charger_inserted()) {
715 charging_splash();
716 restore = true;
719 last_off = current_tick;
721 #endif
722 break; /* case ACTION_TREE_STOP: */
723 case ACTION_STD_MENU:
724 return GO_TO_ROOT;
725 break;
727 case ACTION_TREE_WPS:
728 return GO_TO_PREVIOUS_MUSIC;
729 break;
730 #ifdef HAVE_QUICKSCREEN
731 case ACTION_STD_QUICKSCREEN:
732 /* don't enter f2 from plugin browser */
733 if (*tc.dirfilter < NUM_FILTER_MODES)
735 if (quick_screen_quick(button))
736 reload_dir = true;
737 restore = true;
739 #ifdef HAVE_TAGCACHE
740 id3db = check_changed_id3mode(id3db);
741 reload_dir = true;
742 #endif
744 break;
745 #endif
746 #ifdef BUTTON_F3
747 case ACTION_F3:
748 /* don't enter f3 from plugin browser */
749 if (*tc.dirfilter < NUM_FILTER_MODES)
751 if (quick_screen_f3(BUTTON_F3))
752 reload_dir = true;
753 restore = true;
755 break;
756 #endif
758 case ACTION_STD_CONTEXT:
760 int onplay_result;
761 int attr = 0;
763 if(!numentries)
764 onplay_result = onplay(NULL, 0, curr_context);
765 else {
766 #ifdef HAVE_TAGCACHE
767 if (id3db)
769 if (tagtree_get_attr(&tc) == TREE_ATTR_MPA)
771 attr = TREE_ATTR_MPA;
772 tagtree_get_filename(&tc, buf, sizeof(buf));
774 else
775 attr = ATTR_DIRECTORY;
777 else
778 #endif
780 attr = dircache[tc.selected_item].attr;
782 if (currdir[1]) /* Not in / */
783 snprintf(buf, sizeof buf, "%s/%s",
784 currdir,
785 dircache[tc.selected_item].name);
786 else /* In / */
787 snprintf(buf, sizeof buf, "/%s",
788 dircache[tc.selected_item].name);
790 onplay_result = onplay(buf, attr, curr_context);
792 switch (onplay_result)
794 case ONPLAY_MAINMENU:
795 return GO_TO_ROOT;
797 case ONPLAY_OK:
798 restore = true;
799 break;
801 case ONPLAY_RELOAD_DIR:
802 reload_dir = true;
803 break;
805 case ONPLAY_START_PLAY:
806 return GO_TO_WPS;
807 break;
809 break;
812 case ACTION_NONE:
813 if (thumbnail_time != -1 &&
814 TIME_AFTER(current_tick, thumbnail_time))
815 { /* a delayed hovering thumbnail is due now */
816 int res;
817 int attr;
818 char* name;
820 #ifdef HAVE_TAGCACHE
821 if (id3db)
823 attr = tagtree_get_attr(&tc);
824 name = tagtree_get_entry(&tc, lasti)->name;
826 else
827 #endif
829 attr = dircache[lasti].attr;
830 name = dircache[lasti].name;
833 if (attr & ATTR_DIRECTORY)
835 DEBUGF("Playing directory thumbnail: %s", currdir);
836 res = ft_play_dirname(name);
837 if (res < 0) /* failed, not existing */
838 { /* say the number instead, as a fallback */
839 talk_id(VOICE_DIR, false);
840 talk_number(lasti+1, true);
843 else
845 DEBUGF("Playing file thumbnail: %s/%s%s\n",
846 currdir, name,
847 file_thumbnail_ext);
848 /* no fallback necessary, we knew in advance
849 that the file exists */
850 ft_play_filename(currdir, name);
852 thumbnail_time = -1; /* job done */
854 gui_syncstatusbar_draw(&statusbars, false);
855 break;
857 #ifdef HAVE_HOTSWAP
858 case SYS_FS_CHANGED:
859 #ifdef HAVE_TAGCACHE
860 if (!id3db)
861 #endif
862 reload_dir = true;
863 /* The 'dir no longer valid' situation will be caught later
864 * by checking the showdir() result. */
865 break;
866 #endif
868 default:
869 if (default_event_handler(button) == SYS_USB_CONNECTED)
871 if(*tc.dirfilter > NUM_FILTER_MODES)
872 /* leave sub-browsers after usb, doing otherwise
873 might be confusing to the user */
874 exit_func = true;
875 else
876 reload_dir = true;
878 break;
880 if (start_wps)
881 return GO_TO_WPS;
882 if ( button )
884 ata_spin();
888 check_rescan:
889 /* do we need to rescan dir? */
890 if (reload_dir || reload_root ||
891 lastfilter != *tc.dirfilter ||
892 lastsortcase != global_settings.sort_case)
894 if ( reload_root ) {
895 strcpy(currdir, "/");
896 tc.dirlevel = 0;
897 #ifdef HAVE_TAGCACHE
898 tc.currtable = 0;
899 tc.currextra = 0;
900 lasttable = -1;
901 lastextra = -1;
902 #endif
903 reload_root = false;
906 if (! reload_dir )
908 gui_synclist_select_item(&tree_lists, 0);
909 gui_synclist_draw(&tree_lists);
910 tc.selected_item = 0;
911 lastdir[0] = 0;
914 lastfilter = *tc.dirfilter;
915 lastsortcase = global_settings.sort_case;
916 restore = true;
919 if (exit_func)
920 return GO_TO_PREVIOUS;
922 if (restore || reload_dir) {
923 /* restore display */
924 numentries = update_dir();
925 if (currdir[1] && (numentries < 0))
926 { /* not in root and reload failed */
927 reload_root = true; /* try root */
928 reload_dir = false;
929 goto check_rescan;
931 need_update = true;
932 reload_dir = false;
935 if(need_update) {
936 tc.selected_item = gui_synclist_get_sel_pos(&tree_lists);
937 need_update=false;
938 if ( numentries > 0 ) {
939 /* Voice the file if changed */
940 if(lasti != tc.selected_item || restore) {
941 int attr;
942 char* name;
944 lasti = tc.selected_item;
945 thumbnail_time = -1; /* Cancel whatever we were
946 about to say */
948 #ifdef HAVE_TAGCACHE
949 if (id3db)
951 attr = tagtree_get_attr(&tc);
952 name = tagtree_get_entry(&tc, tc.selected_item)->name;
954 else
955 #endif
957 attr = dircache[tc.selected_item].attr;
958 name = dircache[tc.selected_item].name;
961 /* Directory? */
962 if (attr & ATTR_DIRECTORY)
964 /* play directory thumbnail */
965 switch (global_settings.talk_dir) {
966 case 1: /* dirs as numbers */
967 talk_id(VOICE_DIR, false);
968 talk_number(tc.selected_item+1, true);
969 break;
971 case 2: /* dirs spelled */
972 talk_spell(name, false);
973 break;
975 case 3: /* thumbnail clip */
976 /* "schedule" a thumbnail, to have a little
977 delay */
978 thumbnail_time = current_tick + HOVER_DELAY;
979 break;
981 default:
982 break;
985 else /* file */
987 switch (global_settings.talk_file) {
988 case 1: /* files as numbers */
989 ft_play_filenumber(
990 tc.selected_item-tc.dirsindir+1,
991 attr & TREE_ATTR_MASK);
992 break;
994 case 2: /* files spelled */
995 talk_spell(name, false);
996 break;
998 case 3: /* thumbnail clip */
999 /* "schedule" a thumbnail, to have a little
1000 delay */
1001 if (attr & TREE_ATTR_THUMBNAIL)
1002 thumbnail_time = current_tick + HOVER_DELAY;
1003 else
1004 /* spell the number as fallback */
1005 talk_spell(name, false);
1006 break;
1008 default:
1009 break;
1016 action_signalscreenchange();
1017 return true;
1020 static int plsize = 0;
1021 static long pltick;
1022 static bool add_dir(char* dirname, int len, int fd)
1024 bool abort = false;
1025 DIRCACHED* dir;
1027 /* check for user abort */
1028 if (action_userabort(TIMEOUT_NOBLOCK))
1029 return true;
1031 dir = opendir_cached(dirname);
1032 if(!dir)
1033 return true;
1035 while (true) {
1036 struct dircache_entry *entry;
1038 entry = readdir_cached(dir);
1039 if (!entry)
1040 break;
1041 if (entry->attribute & ATTR_DIRECTORY) {
1042 int dirlen = strlen(dirname);
1043 bool result;
1045 if (!strcmp((char *)entry->d_name, ".") ||
1046 !strcmp((char *)entry->d_name, ".."))
1047 continue;
1049 if (dirname[1])
1050 snprintf(dirname+dirlen, len-dirlen, "/%s", entry->d_name);
1051 else
1052 snprintf(dirname, len, "/%s", entry->d_name);
1054 result = add_dir(dirname, len, fd);
1055 dirname[dirlen] = '\0';
1056 if (result) {
1057 abort = true;
1058 break;
1061 else {
1062 int x = strlen((char *)entry->d_name);
1063 unsigned int i;
1064 char *cp = strrchr((char *)entry->d_name,'.');
1066 if (cp) {
1067 cp++;
1069 /* add all supported audio files to playlists */
1070 for (i=0; i < sizeof(filetypes)/sizeof(struct filetype); i++) {
1071 if (filetypes[i].tree_attr == TREE_ATTR_MPA) {
1072 if (!strcasecmp(cp, filetypes[i].extension)) {
1073 char buf[8];
1074 int i;
1075 write(fd, dirname, strlen(dirname));
1076 write(fd, "/", 1);
1077 write(fd, entry->d_name, x);
1078 write(fd, "\n", 1);
1080 plsize++;
1081 if(TIME_AFTER(current_tick, pltick+HZ/4)) {
1082 pltick = current_tick;
1084 snprintf(buf, sizeof buf, "%d", plsize);
1085 #ifdef HAVE_LCD_BITMAP
1086 FOR_NB_SCREENS(i)
1088 screens[i].puts(0, 4, (unsigned char *)buf);
1089 gui_textarea_update(&screens[i]);
1091 #else
1092 x = 10;
1093 if (plsize > 999)
1094 x=7;
1095 else {
1096 if (plsize > 99)
1097 x=8;
1098 else {
1099 if (plsize > 9)
1100 x=9;
1103 FOR_NB_SCREENS(i) {
1104 screens[i].puts(x,0,buf);
1106 #endif
1108 break;
1115 closedir_cached(dir);
1117 return abort;
1120 bool create_playlist(void)
1122 int fd;
1123 int i;
1124 char filename[MAX_PATH];
1126 pltick = current_tick;
1128 snprintf(filename, sizeof filename, "%s.m3u8",
1129 tc.currdir[1] ? tc.currdir : "/root");
1130 FOR_NB_SCREENS(i)
1132 gui_textarea_clear(&screens[i]);
1133 screens[i].puts(0, 0, str(LANG_CREATING));
1134 screens[i].puts_scroll(0, 1, (unsigned char *)filename);
1135 #if defined(HAVE_LCD_BITMAP) || defined(SIMULATOR)
1136 gui_textarea_update(&screens[i]);
1137 #endif
1139 fd = creat(filename);
1140 if (fd < 0)
1141 return false;
1143 trigger_cpu_boost();
1145 snprintf(filename, sizeof(filename), "%s",
1146 tc.currdir[1] ? tc.currdir : "/");
1147 plsize = 0;
1148 add_dir(filename, sizeof(filename), fd);
1149 close(fd);
1151 sleep(HZ);
1153 return true;
1156 int rockbox_browse(const char *root, int dirfilter)
1158 int ret_val = 0;
1159 int *last_filter = tc.dirfilter;
1160 tc.dirfilter = &dirfilter;
1162 reload_dir = true;
1163 if (dirfilter >= NUM_FILTER_MODES)
1165 static struct tree_context backup;
1166 int last_context;
1168 backup = tc;
1169 tc.selected_item = 0;
1170 tc.dirlevel = 0;
1171 memcpy(tc.currdir, root, sizeof(tc.currdir));
1172 start_wps = false;
1173 last_context = curr_context;
1175 ret_val = dirbrowse();
1176 tc = backup;
1177 curr_context = last_context;
1179 else
1181 static char buf[MAX_PATH];
1182 if (dirfilter != SHOW_ID3DB)
1183 tc.dirfilter = &global_settings.dirfilter;
1184 strcpy(buf,root);
1185 set_current_file(buf);
1186 ret_val = dirbrowse();
1188 tc.dirfilter = last_filter;
1189 return ret_val;
1192 void tree_init(void)
1194 /* We copy the settings value in case it is changed by the user. We can't
1195 use it until the next reboot. */
1196 max_files = global_settings.max_files_in_dir;
1198 /* initialize tree context struct */
1199 memset(&tc, 0, sizeof(tc));
1200 tc.dirfilter = &global_settings.dirfilter;
1202 tc.name_buffer_size = AVERAGE_FILENAME_LENGTH * max_files;
1203 tc.name_buffer = buffer_alloc(tc.name_buffer_size);
1205 tc.dircache_size = max_files * sizeof(struct entry);
1206 tc.dircache = buffer_alloc(tc.dircache_size);
1209 void bookmark_play(char *resume_file, int index, int offset, int seed,
1210 char *filename)
1212 int i;
1213 char* suffix = strrchr(resume_file, '.');
1215 if (suffix != NULL &&
1216 (!strcasecmp(suffix, ".m3u") || !strcasecmp(suffix, ".m3u8")))
1218 /* Playlist playback */
1219 char* slash;
1220 /* check that the file exists */
1221 int fd = open(resume_file, O_RDONLY);
1222 if(fd<0)
1223 return;
1224 close(fd);
1226 slash = strrchr(resume_file,'/');
1227 if (slash)
1229 char* cp;
1230 *slash=0;
1232 cp=resume_file;
1233 if (!cp[0])
1234 cp="/";
1236 if (playlist_create(cp, slash+1) != -1)
1238 if (global_settings.playlist_shuffle)
1239 playlist_shuffle(seed, -1);
1240 playlist_start(index,offset);
1242 *slash='/';
1245 else
1247 /* Directory playback */
1248 lastdir[0]='\0';
1249 if (playlist_create(resume_file, NULL) != -1)
1251 resume_directory(resume_file);
1252 if (global_settings.playlist_shuffle)
1253 playlist_shuffle(seed, -1);
1255 /* Check if the file is at the same spot in the directory,
1256 else search for it */
1257 if ((strcmp(strrchr(playlist_peek(index),'/') + 1,
1258 filename)))
1260 for ( i=0; i < playlist_amount(); i++ )
1262 if ((strcmp(strrchr(playlist_peek(i),'/') + 1,
1263 filename)) == 0)
1264 break;
1266 if (i < playlist_amount())
1267 index = i;
1268 else
1269 return;
1271 playlist_start(index,offset);
1275 start_wps=true;
1278 int ft_play_filenumber(int pos, int attr)
1280 /* try to find a voice ID for the extension, if known */
1281 unsigned int j;
1282 int ext_id = -1; /* default to none */
1283 for (j=0; j<sizeof(filetypes)/sizeof(*filetypes); j++)
1285 if (attr == filetypes[j].tree_attr)
1287 ext_id = filetypes[j].voiceclip;
1288 break;
1292 talk_id(VOICE_FILE, false);
1293 talk_number(pos, true);
1294 talk_id(ext_id, true);
1295 return 1;
1298 int ft_play_dirname(char* name)
1300 int fd;
1301 char dirname_mp3_filename[MAX_PATH+1];
1303 #if CONFIG_CODEC != SWCODEC
1304 if (audio_status() & AUDIO_STATUS_PLAY)
1305 return 0;
1306 #endif
1308 snprintf(dirname_mp3_filename, sizeof(dirname_mp3_filename), "%s/%s/%s",
1309 tc.currdir[1] ? tc.currdir : "" , name,
1310 dir_thumbnail_name);
1312 DEBUGF("Checking for %s\n", dirname_mp3_filename);
1314 fd = open(dirname_mp3_filename, O_RDONLY);
1315 if (fd < 0)
1317 DEBUGF("Failed to find: %s\n", dirname_mp3_filename);
1318 return -1;
1321 close(fd);
1323 DEBUGF("Found: %s\n", dirname_mp3_filename);
1325 talk_file(dirname_mp3_filename, false);
1326 return 1;
1329 void ft_play_filename(char *dir, char *file)
1331 char name_mp3_filename[MAX_PATH+1];
1333 #if CONFIG_CODEC != SWCODEC
1334 if (audio_status() & AUDIO_STATUS_PLAY)
1335 return;
1336 #endif
1338 if (strcasecmp(&file[strlen(file) - strlen(file_thumbnail_ext)],
1339 file_thumbnail_ext))
1340 { /* file has no .talk extension */
1341 snprintf(name_mp3_filename, sizeof(name_mp3_filename),
1342 "%s/%s%s", dir, file, file_thumbnail_ext);
1344 talk_file(name_mp3_filename, false);
1346 else
1347 { /* it already is a .talk file, play this directly */
1348 snprintf(name_mp3_filename, sizeof(name_mp3_filename),
1349 "%s/%s", dir, file);
1350 talk_id(LANG_VOICE_DIR_HOVER, false); /* prefix it */
1351 talk_file(name_mp3_filename, true);
1355 /* These two functions are called by the USB and shutdown handlers */
1356 void tree_flush(void)
1358 scrobbler_shutdown();
1359 #ifdef HAVE_TAGCACHE
1360 tagcache_shutdown();
1361 #endif
1362 playlist_shutdown();
1364 #ifdef HAVE_TC_RAMCACHE
1365 tagcache_unload_ramcache();
1366 #endif
1368 #ifdef HAVE_DIRCACHE
1370 int old_val = global_status.dircache_size;
1371 if (global_settings.dircache)
1373 global_status.dircache_size = dircache_get_cache_size();
1374 # ifdef HAVE_EEPROM_SETTINGS
1375 dircache_save();
1376 # endif
1377 dircache_disable();
1379 else
1381 global_status.dircache_size = 0;
1383 if (old_val != global_status.dircache_size)
1384 status_save();
1386 #endif
1389 void tree_restore(void)
1391 #ifdef HAVE_EEPROM_SETTINGS
1392 firmware_settings.disk_clean = false;
1393 #endif
1395 #ifdef HAVE_TC_RAMCACHE
1396 remove(TAGCACHE_STATEFILE);
1397 #endif
1399 #ifdef HAVE_DIRCACHE
1400 remove(DIRCACHE_FILE);
1401 if (global_settings.dircache)
1403 /* Print "Scanning disk..." to the display. */
1404 int i;
1405 FOR_NB_SCREENS(i)
1407 screens[i].putsxy((LCD_WIDTH/2) -
1408 ((strlen(str(LANG_DIRCACHE_BUILDING)) *
1409 screens[i].char_width)/2),
1410 LCD_HEIGHT-screens[i].char_height*3,
1411 str(LANG_DIRCACHE_BUILDING));
1412 gui_textarea_update(&screens[i]);
1415 dircache_build(global_status.dircache_size);
1417 /* Clean the text when we are done. */
1418 FOR_NB_SCREENS(i)
1420 gui_textarea_clear(&screens[i]);
1423 #endif
1424 #ifdef HAVE_TAGCACHE
1425 tagcache_start_scan();
1426 #endif
1427 scrobbler_init();