Separate out voice options for .talk clips (FS #7249). This removes the assumptions...
[Rockbox.git] / apps / tree.c
blob53aca47d49c3fe51b44832583f33e08e56ba0dd1
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 "sprintf.h"
35 #include "audio.h"
36 #include "playlist.h"
37 #include "menu.h"
38 #include "gwps.h"
39 #include "settings.h"
40 #include "status.h"
41 #include "debug.h"
42 #include "ata.h"
43 #include "rolo.h"
44 #include "icons.h"
45 #include "lang.h"
46 #include "language.h"
47 #include "screens.h"
48 #include "keyboard.h"
49 #include "bookmark.h"
50 #include "onplay.h"
51 #include "buffer.h"
52 #include "plugin.h"
53 #include "power.h"
54 #include "action.h"
55 #include "talk.h"
56 #include "filetypes.h"
57 #include "misc.h"
58 #include "filetree.h"
59 #include "tagtree.h"
60 #ifdef HAVE_RECORDING
61 #include "recorder/recording.h"
62 #endif
63 #include "rtc.h"
64 #include "dircache.h"
65 #ifdef HAVE_TAGCACHE
66 #include "tagcache.h"
67 #endif
68 #include "yesno.h"
69 #include "gwps-common.h"
70 #include "eeprom_settings.h"
71 #include "scrobbler.h"
73 /* gui api */
74 #include "list.h"
75 #include "statusbar.h"
76 #include "splash.h"
77 #include "buttonbar.h"
78 #include "textarea.h"
79 #include "action.h"
81 #include "root_menu.h"
83 #if (LCD_DEPTH > 1) || (defined(HAVE_LCD_REMOTE) && (LCD_REMOTE_DEPTH > 1))
84 #include "backdrop.h"
85 #endif
87 static const struct filetype *filetypes;
88 static int filetypes_count;
90 struct gui_synclist tree_lists;
92 /* I put it here because other files doesn't use it yet,
93 * but should be elsewhere since it will be used mostly everywhere */
94 #ifdef HAS_BUTTONBAR
95 struct gui_buttonbar tree_buttonbar;
96 #endif
97 static struct tree_context tc;
99 bool boot_changed = false;
101 char lastfile[MAX_PATH];
102 static char lastdir[MAX_PATH];
103 #ifdef HAVE_TAGCACHE
104 static int lasttable, lastextra, lastfirstpos;
105 #endif
106 static int max_files = 0;
108 static bool reload_dir = false;
110 static bool start_wps = false;
111 static int curr_context = false;/* id3db or tree*/
113 static int dirbrowse(void);
114 static int ft_play_filenumber(int pos, int attr);
115 static int ft_play_dirname(char* name);
116 static void ft_play_filename(char *dir, char *file);
119 * removes the extension of filename (if it doesn't start with a .)
120 * puts the result in buffer
122 static char * strip_extension(char * filename, char * buffer)
124 int dotpos;
125 char * dot=strrchr(filename, '.');
126 if(dot!=0 && filename[0]!='.')
128 dotpos = dot-filename;
129 strncpy(buffer, filename, dotpos);
130 buffer[dotpos]='\0';
131 return(buffer);
133 else
134 return(filename);
137 static char * tree_get_filename(int selected_item, void * data, char *buffer)
139 struct tree_context * local_tc=(struct tree_context *)data;
140 char *name;
141 int attr=0;
142 #ifdef HAVE_TAGCACHE
143 bool id3db = *(local_tc->dirfilter) == SHOW_ID3DB;
145 if (id3db)
147 return tagtree_get_entry(&tc, selected_item)->name;
149 else
150 #endif
152 struct entry* dc = local_tc->dircache;
153 struct entry* e = &dc[selected_item];
154 name = e->name;
155 attr = e->attr;
157 /* if any file filter is on, and if it's not a directory,
158 * strip the extension */
160 if ( (*(local_tc->dirfilter) != SHOW_ID3DB) && !(attr & ATTR_DIRECTORY)
161 && (*(local_tc->dirfilter) != SHOW_ALL) )
163 return(strip_extension(name, buffer));
165 return(name);
169 static int tree_get_fileicon(int selected_item, void * data)
171 struct tree_context * local_tc=(struct tree_context *)data;
172 #ifdef HAVE_TAGCACHE
173 bool id3db = *(local_tc->dirfilter) == SHOW_ID3DB;
174 if (id3db) {
175 return tagtree_get_icon(&tc);
177 else
178 #endif
180 struct entry* dc = local_tc->dircache;
181 struct entry* e = &dc[selected_item];
182 return filetype_get_icon(e->attr);
186 bool check_rockboxdir(void)
188 DIR *dir = opendir(ROCKBOX_DIR);
189 if(!dir)
190 { /* No need to localise this message.
191 If .rockbox is missing, it wouldn't work anyway */
192 int i;
193 FOR_NB_SCREENS(i)
194 screens[i].clear_display();
195 gui_syncsplash(HZ*2, "No .rockbox directory");
196 FOR_NB_SCREENS(i)
197 screens[i].clear_display();
198 gui_syncsplash(HZ*2, "Installation incomplete");
199 return false;
201 closedir(dir);
202 return true;
205 void browse_root(void)
207 gui_sync_wps_screen_init();
209 check_rockboxdir();
211 strcpy(tc.currdir, "/");
213 #ifdef HAVE_LCD_CHARCELLS
214 int i;
215 FOR_NB_SCREENS(i)
216 screens[i].double_height(false);
217 #endif
218 #ifdef HAS_BUTTONBAR
219 gui_buttonbar_init(&tree_buttonbar);
220 /* since archos only have one screen, no need to create more than that */
221 gui_buttonbar_set_display(&tree_buttonbar, &(screens[SCREEN_MAIN]) );
222 #endif
223 gui_synclist_init(&tree_lists, &tree_get_filename, &tc, false, 1);
224 gui_synclist_set_icon_callback(&tree_lists, &tree_get_fileicon);
225 /* not the best place for this call... but... */
226 root_menu();
231 struct tree_context* tree_get_context(void)
233 return &tc;
236 /* talkbox hovering delay, to avoid immediate disk activity */
237 #define HOVER_DELAY (HZ/2)
239 * Returns the position of a given file in the current directory
240 * returns -1 if not found
242 static int tree_get_file_position(char * filename)
244 int i;
246 /* use lastfile to determine the selected item (default=0) */
247 for (i=0; i < tc.filesindir; i++)
249 struct entry* dc = tc.dircache;
250 struct entry* e = &dc[i];
251 if (!strcasecmp(e->name, filename))
252 return(i);
254 return(-1);/* no file can match, returns undefined */
258 * Called when a new dir is loaded (for example when returning from other apps ...)
259 * also completely redraws the tree
261 static int update_dir(void)
263 bool changed = false;
264 #ifdef HAVE_TAGCACHE
265 bool id3db = *tc.dirfilter == SHOW_ID3DB;
266 /* Checks for changes */
267 if (id3db) {
268 if (tc.currtable != lasttable ||
269 tc.currextra != lastextra ||
270 tc.firstpos != lastfirstpos ||
271 reload_dir)
273 if (tagtree_load(&tc) < 0)
274 return -1;
276 lasttable = tc.currtable;
277 lastextra = tc.currextra;
278 lastfirstpos = tc.firstpos;
279 changed = true;
282 else
283 #endif
285 /* if the tc.currdir has been changed, reload it ...*/
286 if (strncmp(tc.currdir, lastdir, sizeof(lastdir)) || reload_dir) {
288 if (ft_load(&tc, NULL) < 0)
289 return -1;
290 strcpy(lastdir, tc.currdir);
291 changed = true;
294 /* if selected item is undefined */
295 if (tc.selected_item == -1)
297 /* use lastfile to determine the selected item */
298 tc.selected_item = tree_get_file_position(lastfile);
300 /* If the file doesn't exists, select the first one (default) */
301 if(tc.selected_item < 0)
302 tc.selected_item = 0;
303 changed = true;
305 if (changed)
308 #ifdef HAVE_TAGCACHE
309 !id3db &&
310 #endif
311 (tc.dirfull ||
312 tc.filesindir == global_settings.max_files_in_dir) )
314 gui_syncsplash(HZ, str(LANG_SHOWDIR_BUFFER_FULL));
317 #ifdef HAVE_TAGCACHE
318 if (id3db)
320 if (global_settings.show_path_in_browser == SHOW_PATH_FULL
321 || global_settings.show_path_in_browser == SHOW_PATH_CURRENT)
323 gui_synclist_set_title(&tree_lists, tagtree_get_title(&tc),
324 filetype_get_icon(ATTR_DIRECTORY));
326 else
328 /* Must clear the title as the list is reused */
329 gui_synclist_set_title(&tree_lists, NULL, NOICON);
332 else
333 #endif
335 if (global_settings.show_path_in_browser == SHOW_PATH_FULL)
337 gui_synclist_set_title(&tree_lists, tc.currdir,
338 filetype_get_icon(ATTR_DIRECTORY));
340 else if (global_settings.show_path_in_browser == SHOW_PATH_CURRENT)
342 char *title = strrchr(tc.currdir, '/') + 1;
343 if (*title == '\0')
345 /* Display "Files" for the root dir */
346 gui_synclist_set_title(&tree_lists, str(LANG_DIR_BROWSER),
347 filetype_get_icon(ATTR_DIRECTORY));
349 else
350 gui_synclist_set_title(&tree_lists, title,
351 filetype_get_icon(ATTR_DIRECTORY));
353 else
355 /* Must clear the title as the list is reused */
356 gui_synclist_set_title(&tree_lists, NULL, NOICON);
360 gui_synclist_set_nb_items(&tree_lists, tc.filesindir);
361 gui_synclist_set_icon_callback(&tree_lists, tree_get_fileicon);
362 if( tc.selected_item >= tc.filesindir)
363 tc.selected_item=tc.filesindir-1;
365 gui_synclist_select_item(&tree_lists, tc.selected_item);
366 #ifdef HAS_BUTTONBAR
367 if (global_settings.buttonbar) {
368 if (*tc.dirfilter < NUM_FILTER_MODES)
369 gui_buttonbar_set(&tree_buttonbar, str(LANG_SYSFONT_DIRBROWSE_F1),
370 str(LANG_SYSFONT_DIRBROWSE_F2),
371 str(LANG_SYSFONT_DIRBROWSE_F3));
372 else
373 gui_buttonbar_set(&tree_buttonbar, "<<<", "", "");
374 gui_buttonbar_draw(&tree_buttonbar);
376 #endif
377 gui_synclist_draw(&tree_lists);
378 gui_syncstatusbar_draw(&statusbars, true);
379 return tc.filesindir;
382 /* load tracks from specified directory to resume play */
383 void resume_directory(const char *dir)
385 #ifdef HAVE_TAGCACHE
386 bool id3db = *tc.dirfilter == SHOW_ID3DB;
387 #endif
389 if (ft_load(&tc, dir) < 0)
390 return;
391 lastdir[0] = 0;
393 ft_build_playlist(&tc, 0);
395 #ifdef HAVE_TAGCACHE
396 if (id3db)
397 tagtree_load(&tc);
398 #endif
401 /* Returns the current working directory and also writes cwd to buf if
402 non-NULL. In case of error, returns NULL. */
403 char *getcwd(char *buf, int size)
405 if (!buf)
406 return tc.currdir;
407 else if (size > 0)
409 strncpy(buf, tc.currdir, size);
410 return buf;
412 else
413 return NULL;
416 /* Force a reload of the directory next time directory browser is called */
417 void reload_directory(void)
419 reload_dir = true;
422 void get_current_file(char* buffer, int buffer_len)
424 #ifdef HAVE_TAGCACHE
425 /* in ID3DB mode it is a bad idea to call this function */
426 /* (only happens with `follow playlist') */
427 if( *tc.dirfilter == SHOW_ID3DB )
428 return;
429 #endif
431 struct entry* dc = tc.dircache;
432 struct entry* e = &dc[tc.selected_item];
433 snprintf(buffer, buffer_len, "%s/%s", getcwd(NULL,0),
434 tc.dirlength ? e->name : "");
437 /* Selects a file and update tree context properly */
438 static void set_current_file(char *path)
440 char *name;
441 int i;
443 #ifdef HAVE_TAGCACHE
444 /* in ID3DB mode it is a bad idea to call this function */
445 /* (only happens with `follow playlist') */
446 if( *tc.dirfilter == SHOW_ID3DB )
447 return;
448 #endif
450 /* separate directory from filename */
451 /* gets the directory's name and put it into tc.currdir */
452 name = strrchr(path+1,'/');
453 if (name)
455 *name = 0;
456 strcpy(tc.currdir, path);
457 *name = '/';
458 name++;
460 else
462 strcpy(tc.currdir, "/");
463 name = path+1;
466 strcpy(lastfile, name);
469 /* If we changed dir we must recalculate the dirlevel
470 and adjust the selected history properly */
471 if (strncmp(tc.currdir,lastdir,sizeof(lastdir)))
473 tc.dirlevel = 0;
474 tc.selected_item_history[tc.dirlevel] = -1;
476 /* use '/' to calculate dirlevel */
477 for (i = 1; path[i] != '\0'; i++)
479 if (path[i] == '/')
481 tc.dirlevel++;
482 tc.selected_item_history[tc.dirlevel] = -1;
486 if (ft_load(&tc, NULL) >= 0)
488 tc.selected_item = tree_get_file_position(lastfile);
493 /* main loop, handles key events */
494 static int dirbrowse()
496 int numentries=0;
497 char buf[MAX_PATH];
498 int lasti = -1;
499 unsigned button, returned_button;
500 bool reload_root = false;
501 int lastfilter = *tc.dirfilter;
502 bool lastsortcase = global_settings.sort_case;
503 bool need_update = true;
504 bool exit_func = false;
505 long thumbnail_time = -1; /* for delaying a thumbnail */
506 long last_cancel = 0;
508 char* currdir = tc.currdir; /* just a shortcut */
509 #ifdef HAVE_TAGCACHE
510 bool id3db = *tc.dirfilter == SHOW_ID3DB;
512 if (id3db)
513 curr_context=CONTEXT_ID3DB;
514 else
515 #endif
516 curr_context=CONTEXT_TREE;
517 if (tc.selected_item < 0)
518 tc.selected_item = 0;
519 #ifdef HAVE_TAGCACHE
520 tc.firstpos=0;
521 lasttable = -1;
522 lastextra = -1;
523 lastfirstpos = 0;
524 #endif
526 start_wps = false;
527 numentries = update_dir();
528 if (numentries == -1)
529 return false; /* currdir is not a directory */
531 if (*tc.dirfilter > NUM_FILTER_MODES && numentries==0)
533 gui_syncsplash(HZ*2, str(LANG_NO_FILES));
534 return false; /* No files found for rockbox_browser() */
537 while(1) {
538 struct entry *dircache = tc.dircache;
539 bool restore = false;
540 if (tc.dirlevel < 0)
541 tc.dirlevel = 0; /* shouldnt be needed.. this code needs work! */
542 #ifdef BOOTFILE
543 if (boot_changed) {
544 char *lines[]={str(LANG_BOOT_CHANGED), str(LANG_REBOOT_NOW)};
545 struct text_message message={lines, 2};
546 if(gui_syncyesno_run(&message, NULL, NULL)==YESNO_YES)
547 rolo_load("/" BOOTFILE);
548 restore = true;
549 boot_changed = false;
551 #endif
552 button = get_action(CONTEXT_TREE,HZ/5);
553 returned_button = gui_synclist_do_button(&tree_lists, button,LIST_WRAP_UNLESS_HELD);
554 if (returned_button)
555 need_update = true;
556 if (returned_button == ACTION_STD_CANCEL)
557 button = ACTION_STD_CANCEL;
559 tc.selected_item = gui_synclist_get_sel_pos(&tree_lists);
560 switch ( button ) {
561 case ACTION_STD_OK:
562 /* nothing to do if no files to display */
563 if ( numentries == 0 )
564 break;
566 #ifdef HAVE_TAGCACHE
567 switch (id3db?tagtree_enter(&tc):ft_enter(&tc))
568 #else
569 switch (ft_enter(&tc))
570 #endif
572 case 1: reload_dir = true; break;
573 case 2: start_wps = true; break;
574 case 3: exit_func = true; break;
575 default: break;
577 restore = true;
578 break;
580 case ACTION_STD_CANCEL:
581 if (*tc.dirfilter > NUM_FILTER_MODES && tc.dirlevel < 1) {
582 exit_func = true;
583 break;
585 if ((*tc.dirfilter == SHOW_ID3DB && tc.dirlevel == 0) ||
586 ((*tc.dirfilter != SHOW_ID3DB && !strcmp(currdir,"/"))))
588 if (last_cancel && TIME_BEFORE(current_tick, last_cancel+HZ/2))
590 last_cancel = 0;
591 action_signalscreenchange(); /* eat the cancel presses */
592 break;
594 else
595 return GO_TO_ROOT;
597 last_cancel = current_tick;
599 #ifdef HAVE_TAGCACHE
600 if (id3db)
601 tagtree_exit(&tc);
602 else
603 #endif
604 if (ft_exit(&tc) == 3)
605 exit_func = true;
607 restore = true;
608 break;
610 case ACTION_TREE_STOP:
611 if (list_stop_handler())
612 restore = true;
613 break;
615 case ACTION_STD_MENU:
616 return GO_TO_ROOT;
617 break;
619 case ACTION_TREE_WPS:
620 return GO_TO_PREVIOUS_MUSIC;
621 break;
622 #ifdef HAVE_QUICKSCREEN
623 case ACTION_STD_QUICKSCREEN:
624 /* don't enter f2 from plugin browser */
625 if (*tc.dirfilter < NUM_FILTER_MODES)
627 if (quick_screen_quick(button))
628 reload_dir = true;
629 restore = true;
631 break;
632 #endif
633 #ifdef BUTTON_F3
634 case ACTION_F3:
635 /* don't enter f3 from plugin browser */
636 if (*tc.dirfilter < NUM_FILTER_MODES)
638 if (quick_screen_f3(BUTTON_F3))
639 reload_dir = true;
640 restore = true;
642 break;
643 #endif
645 case ACTION_STD_CONTEXT:
647 int onplay_result;
648 int attr = 0;
650 if(!numentries)
651 onplay_result = onplay(NULL, 0, curr_context);
652 else {
653 #ifdef HAVE_TAGCACHE
654 if (id3db)
656 if (tagtree_get_attr(&tc) == FILE_ATTR_AUDIO)
658 attr = FILE_ATTR_AUDIO;
659 tagtree_get_filename(&tc, buf, sizeof(buf));
661 else
662 attr = ATTR_DIRECTORY;
664 else
665 #endif
667 attr = dircache[tc.selected_item].attr;
669 if (currdir[1]) /* Not in / */
670 snprintf(buf, sizeof buf, "%s/%s",
671 currdir,
672 dircache[tc.selected_item].name);
673 else /* In / */
674 snprintf(buf, sizeof buf, "/%s",
675 dircache[tc.selected_item].name);
677 onplay_result = onplay(buf, attr, curr_context);
679 switch (onplay_result)
681 case ONPLAY_MAINMENU:
682 return GO_TO_ROOT;
684 case ONPLAY_OK:
685 restore = true;
686 break;
688 case ONPLAY_RELOAD_DIR:
689 reload_dir = true;
690 break;
692 case ONPLAY_START_PLAY:
693 return GO_TO_WPS;
694 break;
696 break;
699 case ACTION_NONE:
700 if (thumbnail_time != -1 &&
701 TIME_AFTER(current_tick, thumbnail_time))
702 { /* a delayed hovering thumbnail is due now */
703 int res;
704 int attr;
705 char* name;
707 #ifdef HAVE_TAGCACHE
708 if (id3db)
710 attr = tagtree_get_attr(&tc);
711 name = tagtree_get_entry(&tc, lasti)->name;
713 else
714 #endif
716 attr = dircache[lasti].attr;
717 name = dircache[lasti].name;
720 if (attr & ATTR_DIRECTORY)
722 DEBUGF("Playing directory thumbnail: %s", currdir);
723 res = ft_play_dirname(name);
724 if (res < 0) /* failed, not existing */
726 /* say the number or spell if required as a fallback */
727 switch (global_settings.talk_dir)
729 case 1: /* dirs as numbers */
730 talk_id(VOICE_DIR, false);
731 talk_number(lasti+1, true);
732 break;
734 case 2: /* dirs spelled */
735 talk_spell(name, false);
736 break;
740 else
742 DEBUGF("Playing file thumbnail: %s/%s%s\n",
743 currdir, name,
744 file_thumbnail_ext);
745 /* no fallback necessary, we knew in advance
746 that the file exists */
747 ft_play_filename(currdir, name);
749 thumbnail_time = -1; /* job done */
751 gui_syncstatusbar_draw(&statusbars, false);
752 break;
754 #ifdef HAVE_HOTSWAP
755 case SYS_FS_CHANGED:
756 #ifdef HAVE_TAGCACHE
757 if (!id3db)
758 #endif
759 reload_dir = true;
760 /* The 'dir no longer valid' situation will be caught later
761 * by checking the showdir() result. */
762 break;
763 #endif
765 default:
766 if (default_event_handler(button) == SYS_USB_CONNECTED)
768 if(*tc.dirfilter > NUM_FILTER_MODES)
769 /* leave sub-browsers after usb, doing otherwise
770 might be confusing to the user */
771 exit_func = true;
772 else
773 reload_dir = true;
775 break;
777 if (start_wps)
778 return GO_TO_WPS;
779 if ( button )
781 ata_spin();
785 check_rescan:
786 /* do we need to rescan dir? */
787 if (reload_dir || reload_root ||
788 lastfilter != *tc.dirfilter ||
789 lastsortcase != global_settings.sort_case)
791 if ( reload_root ) {
792 strcpy(currdir, "/");
793 tc.dirlevel = 0;
794 #ifdef HAVE_TAGCACHE
795 tc.currtable = 0;
796 tc.currextra = 0;
797 lasttable = -1;
798 lastextra = -1;
799 #endif
800 reload_root = false;
803 if (! reload_dir )
805 gui_synclist_select_item(&tree_lists, 0);
806 gui_synclist_draw(&tree_lists);
807 tc.selected_item = 0;
808 lastdir[0] = 0;
811 lastfilter = *tc.dirfilter;
812 lastsortcase = global_settings.sort_case;
813 restore = true;
816 if (exit_func)
817 return GO_TO_PREVIOUS;
819 if (restore || reload_dir) {
820 /* restore display */
821 numentries = update_dir();
822 if (currdir[1] && (numentries < 0))
823 { /* not in root and reload failed */
824 reload_root = true; /* try root */
825 reload_dir = false;
826 goto check_rescan;
828 need_update = true;
829 reload_dir = false;
832 if(need_update) {
833 need_update=false;
834 if ( numentries > 0 ) {
835 /* Voice the file if changed */
836 if(lasti != tc.selected_item || restore) {
837 int attr;
838 char* name;
840 lasti = tc.selected_item;
841 thumbnail_time = -1; /* Cancel whatever we were
842 about to say */
844 #ifdef HAVE_TAGCACHE
845 if (id3db)
847 attr = tagtree_get_attr(&tc);
848 name = tagtree_get_entry(&tc, tc.selected_item)->name;
850 else
851 #endif
853 attr = dircache[tc.selected_item].attr;
854 name = dircache[tc.selected_item].name;
857 /* Directory? */
858 if (attr & ATTR_DIRECTORY)
860 /* schedule thumbnail playback if required */
861 if (global_settings.talk_dir_clip)
862 thumbnail_time = current_tick + HOVER_DELAY;
863 else
865 /* talk directly */
866 switch (global_settings.talk_dir)
868 case 1: /* dirs as numbers */
869 talk_id(VOICE_DIR, false);
870 talk_number(tc.selected_item+1, true);
871 break;
873 case 2: /* dirs spelled */
874 talk_spell(name, false);
875 break;
879 else /* file */
881 /* schedule thumbnail playback if required */
882 if (global_settings.talk_file_clip && (attr & FILE_ATTR_THUMBNAIL))
883 thumbnail_time = current_tick + HOVER_DELAY;
884 else
886 /* talk directly */
887 switch (global_settings.talk_file)
889 case 1: /* files as numbers */
890 ft_play_filenumber(
891 tc.selected_item-tc.dirsindir+1,
892 attr & FILE_ATTR_MASK);
893 break;
895 case 2: /* files spelled */
896 talk_spell(name, false);
897 break;
905 action_signalscreenchange();
906 return true;
909 static int plsize = 0;
910 static long pltick;
911 static bool add_dir(char* dirname, int len, int fd)
913 bool abort = false;
914 DIRCACHED* dir;
916 /* check for user abort */
917 if (action_userabort(TIMEOUT_NOBLOCK))
918 return true;
920 dir = opendir_cached(dirname);
921 if(!dir)
922 return true;
924 while (true) {
925 struct dircache_entry *entry;
927 entry = readdir_cached(dir);
928 if (!entry)
929 break;
930 if (entry->attribute & ATTR_DIRECTORY) {
931 int dirlen = strlen(dirname);
932 bool result;
934 if (!strcmp((char *)entry->d_name, ".") ||
935 !strcmp((char *)entry->d_name, ".."))
936 continue;
938 if (dirname[1])
939 snprintf(dirname+dirlen, len-dirlen, "/%s", entry->d_name);
940 else
941 snprintf(dirname, len, "/%s", entry->d_name);
943 result = add_dir(dirname, len, fd);
944 dirname[dirlen] = '\0';
945 if (result) {
946 abort = true;
947 break;
950 else {
951 int x = strlen((char *)entry->d_name);
952 int i;
953 char *cp = strrchr((char *)entry->d_name,'.');
955 if (cp) {
956 cp++;
958 /* add all supported audio files to playlists */
959 for (i=0; i < filetypes_count; i++) {
960 if (filetypes[i].tree_attr == FILE_ATTR_AUDIO) {
961 if (!strcasecmp(cp, filetypes[i].extension)) {
962 char buf[8];
963 int i;
964 write(fd, dirname, strlen(dirname));
965 write(fd, "/", 1);
966 write(fd, entry->d_name, x);
967 write(fd, "\n", 1);
969 plsize++;
970 if(TIME_AFTER(current_tick, pltick+HZ/4)) {
971 pltick = current_tick;
973 snprintf(buf, sizeof buf, "%d", plsize);
974 #ifdef HAVE_LCD_BITMAP
975 FOR_NB_SCREENS(i)
977 screens[i].puts(0, 4, (unsigned char *)buf);
978 gui_textarea_update(&screens[i]);
980 #else
981 x = 10;
982 if (plsize > 999)
983 x=7;
984 else {
985 if (plsize > 99)
986 x=8;
987 else {
988 if (plsize > 9)
989 x=9;
992 FOR_NB_SCREENS(i) {
993 screens[i].puts(x,0,buf);
995 #endif
997 break;
1004 closedir_cached(dir);
1006 return abort;
1009 bool create_playlist(void)
1011 int fd;
1012 int i;
1013 char filename[MAX_PATH];
1015 pltick = current_tick;
1017 snprintf(filename, sizeof filename, "%s.m3u8",
1018 tc.currdir[1] ? tc.currdir : "/root");
1019 FOR_NB_SCREENS(i)
1021 gui_textarea_clear(&screens[i]);
1022 screens[i].puts(0, 0, str(LANG_CREATING));
1023 screens[i].puts_scroll(0, 1, (unsigned char *)filename);
1024 #if defined(HAVE_LCD_BITMAP) || defined(SIMULATOR)
1025 gui_textarea_update(&screens[i]);
1026 #endif
1028 fd = creat(filename);
1029 if (fd < 0)
1030 return false;
1032 trigger_cpu_boost();
1034 snprintf(filename, sizeof(filename), "%s",
1035 tc.currdir[1] ? tc.currdir : "/");
1036 plsize = 0;
1037 add_dir(filename, sizeof(filename), fd);
1038 close(fd);
1040 sleep(HZ);
1042 return true;
1045 int rockbox_browse(const char *root, int dirfilter)
1047 int ret_val = 0;
1048 int *last_filter = tc.dirfilter;
1049 tc.dirfilter = &dirfilter;
1051 reload_dir = true;
1052 if (dirfilter >= NUM_FILTER_MODES)
1054 static struct tree_context backup;
1055 int last_context;
1057 backup = tc;
1058 tc.selected_item = 0;
1059 tc.dirlevel = 0;
1060 memcpy(tc.currdir, root, sizeof(tc.currdir));
1061 start_wps = false;
1062 last_context = curr_context;
1064 ret_val = dirbrowse();
1065 tc = backup;
1066 curr_context = last_context;
1068 else
1070 static char buf[MAX_PATH];
1071 if (dirfilter != SHOW_ID3DB)
1072 tc.dirfilter = &global_settings.dirfilter;
1073 strcpy(buf,root);
1074 set_current_file(buf);
1075 ret_val = dirbrowse();
1077 tc.dirfilter = last_filter;
1078 return ret_val;
1081 void tree_init(void)
1083 /* We copy the settings value in case it is changed by the user. We can't
1084 use it until the next reboot. */
1085 max_files = global_settings.max_files_in_dir;
1087 /* initialize tree context struct */
1088 memset(&tc, 0, sizeof(tc));
1089 tc.dirfilter = &global_settings.dirfilter;
1091 tc.name_buffer_size = AVERAGE_FILENAME_LENGTH * max_files;
1092 tc.name_buffer = buffer_alloc(tc.name_buffer_size);
1094 tc.dircache_size = max_files * sizeof(struct entry);
1095 tc.dircache = buffer_alloc(tc.dircache_size);
1096 tree_get_filetypes(&filetypes, &filetypes_count);
1099 void bookmark_play(char *resume_file, int index, int offset, int seed,
1100 char *filename)
1102 int i;
1103 char* suffix = strrchr(resume_file, '.');
1105 if (suffix != NULL &&
1106 (!strcasecmp(suffix, ".m3u") || !strcasecmp(suffix, ".m3u8")))
1108 /* Playlist playback */
1109 char* slash;
1110 /* check that the file exists */
1111 int fd = open(resume_file, O_RDONLY);
1112 if(fd<0)
1113 return;
1114 close(fd);
1116 slash = strrchr(resume_file,'/');
1117 if (slash)
1119 char* cp;
1120 *slash=0;
1122 cp=resume_file;
1123 if (!cp[0])
1124 cp="/";
1126 if (playlist_create(cp, slash+1) != -1)
1128 if (global_settings.playlist_shuffle)
1129 playlist_shuffle(seed, -1);
1130 playlist_start(index,offset);
1132 *slash='/';
1135 else
1137 /* Directory playback */
1138 lastdir[0]='\0';
1139 if (playlist_create(resume_file, NULL) != -1)
1141 char* peek_filename;
1142 resume_directory(resume_file);
1143 if (global_settings.playlist_shuffle)
1144 playlist_shuffle(seed, -1);
1146 /* Check if the file is at the same spot in the directory,
1147 else search for it */
1148 peek_filename = playlist_peek(index);
1150 if (peek_filename == NULL)
1151 return;
1153 if (strcmp(strrchr(peek_filename, '/') + 1, filename))
1155 for ( i=0; i < playlist_amount(); i++ )
1157 peek_filename = playlist_peek(i);
1159 if (peek_filename == NULL)
1160 return;
1162 if (!strcmp(strrchr(peek_filename, '/') + 1, filename))
1163 break;
1165 if (i < playlist_amount())
1166 index = i;
1167 else
1168 return;
1170 playlist_start(index,offset);
1174 start_wps=true;
1177 static int ft_play_filenumber(int pos, int attr)
1179 /* try to find a voice ID for the extension, if known */
1180 int j;
1181 int ext_id = -1; /* default to none */
1182 for (j=0; j<filetypes_count; j++)
1184 if (attr == filetypes[j].tree_attr)
1186 ext_id = filetypes[j].voiceclip;
1187 break;
1191 talk_id(VOICE_FILE, false);
1192 talk_number(pos, true);
1193 talk_id(ext_id, true);
1194 return 1;
1197 static int ft_play_dirname(char* name)
1199 int fd;
1200 char dirname_mp3_filename[MAX_PATH+1];
1202 #if CONFIG_CODEC != SWCODEC
1203 if (audio_status() & AUDIO_STATUS_PLAY)
1204 return 0;
1205 #endif
1207 snprintf(dirname_mp3_filename, sizeof(dirname_mp3_filename), "%s/%s/%s",
1208 tc.currdir[1] ? tc.currdir : "" , name,
1209 dir_thumbnail_name);
1211 DEBUGF("Checking for %s\n", dirname_mp3_filename);
1213 fd = open(dirname_mp3_filename, O_RDONLY);
1214 if (fd < 0)
1216 DEBUGF("Failed to find: %s\n", dirname_mp3_filename);
1217 return -1;
1220 close(fd);
1222 DEBUGF("Found: %s\n", dirname_mp3_filename);
1224 talk_file(dirname_mp3_filename, false);
1225 return 1;
1228 static void ft_play_filename(char *dir, char *file)
1230 char name_mp3_filename[MAX_PATH+1];
1232 #if CONFIG_CODEC != SWCODEC
1233 if (audio_status() & AUDIO_STATUS_PLAY)
1234 return;
1235 #endif
1237 if (strcasecmp(&file[strlen(file) - strlen(file_thumbnail_ext)],
1238 file_thumbnail_ext))
1239 { /* file has no .talk extension */
1240 snprintf(name_mp3_filename, sizeof(name_mp3_filename),
1241 "%s/%s%s", dir, file, file_thumbnail_ext);
1243 talk_file(name_mp3_filename, false);
1245 else
1246 { /* it already is a .talk file, play this directly */
1247 snprintf(name_mp3_filename, sizeof(name_mp3_filename),
1248 "%s/%s", dir, file);
1249 talk_id(LANG_VOICE_DIR_HOVER, false); /* prefix it */
1250 talk_file(name_mp3_filename, true);
1254 /* These two functions are called by the USB and shutdown handlers */
1255 void tree_flush(void)
1257 scrobbler_shutdown();
1258 #ifdef HAVE_TAGCACHE
1259 tagcache_shutdown();
1260 #endif
1261 playlist_shutdown();
1263 #ifdef HAVE_TC_RAMCACHE
1264 tagcache_unload_ramcache();
1265 #endif
1267 #ifdef HAVE_DIRCACHE
1269 int old_val = global_status.dircache_size;
1270 if (global_settings.dircache)
1272 global_status.dircache_size = dircache_get_cache_size();
1273 # ifdef HAVE_EEPROM_SETTINGS
1274 if (firmware_settings.initialized)
1275 dircache_save();
1276 # endif
1277 dircache_disable();
1279 else
1281 global_status.dircache_size = 0;
1283 if (old_val != global_status.dircache_size)
1284 status_save();
1286 #endif
1289 void tree_restore(void)
1291 #ifdef HAVE_EEPROM_SETTINGS
1292 firmware_settings.disk_clean = false;
1293 #endif
1295 #ifdef HAVE_TC_RAMCACHE
1296 remove(TAGCACHE_STATEFILE);
1297 #endif
1299 #ifdef HAVE_DIRCACHE
1300 remove(DIRCACHE_FILE);
1301 if (global_settings.dircache)
1303 /* Print "Scanning disk..." to the display. */
1304 int i;
1305 FOR_NB_SCREENS(i)
1307 screens[i].putsxy((LCD_WIDTH/2) -
1308 ((strlen(str(LANG_DIRCACHE_BUILDING)) *
1309 screens[i].char_width)/2),
1310 LCD_HEIGHT-screens[i].char_height*3,
1311 str(LANG_DIRCACHE_BUILDING));
1312 gui_textarea_update(&screens[i]);
1315 dircache_build(global_status.dircache_size);
1317 /* Clean the text when we are done. */
1318 FOR_NB_SCREENS(i)
1320 gui_textarea_clear(&screens[i]);
1323 #endif
1324 #ifdef HAVE_TAGCACHE
1325 tagcache_start_scan();
1326 #endif
1327 scrobbler_init();