Update installation document a bit to catch up with recent changes. Add notes about...
[Rockbox.git] / apps / tree.c
blob4d3b9faca16024e34c544e11fd82e3c096817fa2
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"
82 #include "backdrop.h"
84 static const struct filetype *filetypes;
85 static int filetypes_count;
87 struct gui_synclist tree_lists;
89 /* I put it here because other files doesn't use it yet,
90 * but should be elsewhere since it will be used mostly everywhere */
91 #ifdef HAS_BUTTONBAR
92 struct gui_buttonbar tree_buttonbar;
93 #endif
94 static struct tree_context tc;
96 bool boot_changed = false;
98 char lastfile[MAX_PATH];
99 static char lastdir[MAX_PATH];
100 #ifdef HAVE_TAGCACHE
101 static int lasttable, lastextra, lastfirstpos;
102 #endif
103 static int max_files = 0;
105 static bool reload_dir = false;
107 static bool start_wps = false;
108 static int curr_context = false;/* id3db or tree*/
110 static int dirbrowse(void);
111 static int ft_play_dirname(char* name);
112 static void ft_play_filename(char *dir, char *file);
113 static void say_filetype(int attr);
115 static char * tree_get_filename(int selected_item, void * data, char *buffer)
117 struct tree_context * local_tc=(struct tree_context *)data;
118 char *name;
119 int attr=0;
120 bool stripit = false;
121 #ifdef HAVE_TAGCACHE
122 bool id3db = *(local_tc->dirfilter) == SHOW_ID3DB;
124 if (id3db)
126 return tagtree_get_entry(&tc, selected_item)->name;
128 else
129 #endif
131 struct entry* dc = local_tc->dircache;
132 struct entry* e = &dc[selected_item];
133 name = e->name;
134 attr = e->attr;
137 if(!(attr & ATTR_DIRECTORY))
139 switch(global_settings.show_filename_ext)
141 case 0:
142 /* show file extension: off */
143 stripit = true;
144 break;
145 case 1:
146 /* show file extension: on */
147 break;
148 case 2:
149 /* show file extension: only unknown types */
150 stripit = filetype_supported(attr);
151 break;
152 case 3:
153 default:
154 /* show file extension: only when viewing all */
155 stripit = (*(local_tc->dirfilter) != SHOW_ID3DB) &&
156 (*(local_tc->dirfilter) != SHOW_ALL);
157 break;
161 if(stripit)
163 return(strip_extension(buffer, MAX_PATH, name));
165 return(name);
168 #ifdef HAVE_LCD_COLOR
169 static int tree_get_filecolor(int selected_item, void * data)
171 if (*tc.dirfilter == SHOW_ID3DB)
172 return -1;
173 struct tree_context * local_tc=(struct tree_context *)data;
174 struct entry* dc = local_tc->dircache;
175 struct entry* e = &dc[selected_item];
176 return filetype_get_color(e->name, e->attr);
178 #endif
180 static int tree_get_fileicon(int selected_item, void * data)
182 struct tree_context * local_tc=(struct tree_context *)data;
183 #ifdef HAVE_TAGCACHE
184 bool id3db = *(local_tc->dirfilter) == SHOW_ID3DB;
185 if (id3db) {
186 return tagtree_get_icon(&tc);
188 else
189 #endif
191 struct entry* dc = local_tc->dircache;
192 struct entry* e = &dc[selected_item];
193 return filetype_get_icon(e->attr);
197 static int tree_voice_cb(int selected_item, void * data)
199 struct tree_context * local_tc=(struct tree_context *)data;
200 char *name;
201 int attr=0;
202 #ifdef HAVE_TAGCACHE
203 bool id3db = *(local_tc->dirfilter) == SHOW_ID3DB;
205 if (id3db)
207 attr = tagtree_get_attr(local_tc);
208 name = tagtree_get_entry(local_tc, selected_item)->name;
210 else
211 #endif
213 struct entry* dc = local_tc->dircache;
214 struct entry* e = &dc[selected_item];
215 name = e->name;
216 attr = e->attr;
218 bool is_dir = (attr & ATTR_DIRECTORY);
219 bool did_clip = false;
220 /* First the .talk clip case */
221 if(is_dir)
223 if(global_settings.talk_dir_clip)
225 DEBUGF("Playing directory thumbnail: %s", local_tc->currdir);
226 did_clip = true;
227 if(ft_play_dirname(name) <0)
228 /* failed, not existing */
229 did_clip = false;
231 } else { /* it's a file */
232 if (global_settings.talk_file_clip && (attr & FILE_ATTR_THUMBNAIL))
234 did_clip = true;
235 DEBUGF("Playing file thumbnail: %s/%s%s\n",
236 local_tc->currdir, name, file_thumbnail_ext);
237 ft_play_filename(local_tc->currdir, name);
240 if(!did_clip)
242 /* say the number or spell if required or as a fallback */
243 switch (is_dir ? global_settings.talk_dir : global_settings.talk_file)
245 case 1: /* as numbers */
246 talk_id(is_dir ? VOICE_DIR : VOICE_FILE, false);
247 talk_number(selected_item+1 - (is_dir ? 0 : local_tc->dirsindir),
248 true);
249 if(global_settings.talk_filetype
250 && !is_dir && *local_tc->dirfilter < NUM_FILTER_MODES)
251 say_filetype(attr);
252 break;
253 case 2: /* spelled */
254 talk_shutup();
255 if(global_settings.talk_filetype)
257 if(is_dir)
258 talk_id(VOICE_DIR, true);
259 else if(*local_tc->dirfilter < NUM_FILTER_MODES)
260 say_filetype(attr);
262 talk_spell(name, true);
263 break;
266 return 0;
269 bool check_rockboxdir(void)
271 DIR *dir = opendir(ROCKBOX_DIR);
272 if(!dir)
273 { /* No need to localise this message.
274 If .rockbox is missing, it wouldn't work anyway */
275 int i;
276 FOR_NB_SCREENS(i)
277 screens[i].clear_display();
278 gui_syncsplash(HZ*2, "No .rockbox directory");
279 FOR_NB_SCREENS(i)
280 screens[i].clear_display();
281 gui_syncsplash(HZ*2, "Installation incomplete");
282 return false;
284 closedir(dir);
285 return true;
288 /* do this really late in the init sequence */
289 void tree_gui_init(void)
291 gui_sync_wps_screen_init();
293 check_rockboxdir();
295 strcpy(tc.currdir, "/");
297 #ifdef HAVE_LCD_CHARCELLS
298 int i;
299 FOR_NB_SCREENS(i)
300 screens[i].double_height(false);
301 #endif
302 #ifdef HAS_BUTTONBAR
303 gui_buttonbar_init(&tree_buttonbar);
304 /* since archos only have one screen, no need to create more than that */
305 gui_buttonbar_set_display(&tree_buttonbar, &(screens[SCREEN_MAIN]) );
306 #endif
307 gui_synclist_init(&tree_lists, &tree_get_filename, &tc, false, 1, NULL);
308 gui_synclist_set_voice_callback(&tree_lists, tree_voice_cb);
309 gui_synclist_set_icon_callback(&tree_lists, &tree_get_fileicon);
310 #ifdef HAVE_LCD_COLOR
311 gui_synclist_set_color_callback(&tree_lists, &tree_get_filecolor);
312 #endif
317 struct tree_context* tree_get_context(void)
319 return &tc;
323 * Returns the position of a given file in the current directory
324 * returns -1 if not found
326 static int tree_get_file_position(char * filename)
328 int i;
330 /* use lastfile to determine the selected item (default=0) */
331 for (i=0; i < tc.filesindir; i++)
333 struct entry* dc = tc.dircache;
334 struct entry* e = &dc[i];
335 if (!strcasecmp(e->name, filename))
336 return(i);
338 return(-1);/* no file can match, returns undefined */
342 * Called when a new dir is loaded (for example when returning from other apps ...)
343 * also completely redraws the tree
345 static int update_dir(void)
347 bool changed = false;
348 #ifdef HAVE_TAGCACHE
349 bool id3db = *tc.dirfilter == SHOW_ID3DB;
350 /* Checks for changes */
351 if (id3db) {
352 if (tc.currtable != lasttable ||
353 tc.currextra != lastextra ||
354 tc.firstpos != lastfirstpos ||
355 reload_dir)
357 if (tagtree_load(&tc) < 0)
358 return -1;
360 lasttable = tc.currtable;
361 lastextra = tc.currextra;
362 lastfirstpos = tc.firstpos;
363 changed = true;
366 else
367 #endif
369 /* if the tc.currdir has been changed, reload it ...*/
370 if (strncmp(tc.currdir, lastdir, sizeof(lastdir)) || reload_dir)
372 if (ft_load(&tc, NULL) < 0)
373 return -1;
374 strcpy(lastdir, tc.currdir);
375 changed = true;
378 /* if selected item is undefined */
379 if (tc.selected_item == -1)
381 /* use lastfile to determine the selected item */
382 tc.selected_item = tree_get_file_position(lastfile);
384 /* If the file doesn't exists, select the first one (default) */
385 if(tc.selected_item < 0)
386 tc.selected_item = 0;
387 changed = true;
389 if (changed)
392 #ifdef HAVE_TAGCACHE
393 !id3db &&
394 #endif
395 (tc.dirfull || tc.filesindir == global_settings.max_files_in_dir) )
397 gui_syncsplash(HZ, ID2P(LANG_SHOWDIR_BUFFER_FULL));
400 #ifdef HAVE_TAGCACHE
401 if (id3db)
403 #ifdef HAVE_LCD_BITMAP
404 if (global_settings.show_path_in_browser == SHOW_PATH_FULL
405 || global_settings.show_path_in_browser == SHOW_PATH_CURRENT)
407 gui_synclist_set_title(&tree_lists, tagtree_get_title(&tc),
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);
415 #endif
417 else
418 #endif
420 #ifdef HAVE_LCD_BITMAP
421 if (global_settings.show_path_in_browser &&
422 *(tc.dirfilter) == SHOW_PLUGINS)
424 char *title;
425 if (!strcmp(tc.currdir, PLUGIN_GAMES_DIR))
426 title = str(LANG_PLUGIN_GAMES);
427 else if (!strcmp(tc.currdir, PLUGIN_APPS_DIR))
428 title = str(LANG_PLUGIN_APPS);
429 else if (!strcmp(tc.currdir, PLUGIN_DEMOS_DIR))
430 title = str(LANG_PLUGIN_DEMOS);
431 else title = str(LANG_PLUGINS);
432 gui_synclist_set_title(&tree_lists, title, Icon_Plugin);
434 else if (global_settings.show_path_in_browser == SHOW_PATH_FULL)
436 gui_synclist_set_title(&tree_lists, tc.currdir,
437 filetype_get_icon(ATTR_DIRECTORY));
439 else if (global_settings.show_path_in_browser == SHOW_PATH_CURRENT)
441 char *title = strrchr(tc.currdir, '/') + 1;
442 if (*title == '\0')
444 /* Display "Files" for the root dir */
445 gui_synclist_set_title(&tree_lists, str(LANG_DIR_BROWSER),
446 filetype_get_icon(ATTR_DIRECTORY));
448 else
449 gui_synclist_set_title(&tree_lists, title,
450 filetype_get_icon(ATTR_DIRECTORY));
452 else
454 /* Must clear the title as the list is reused */
455 gui_synclist_set_title(&tree_lists, NULL, NOICON);
457 #endif
460 gui_synclist_set_nb_items(&tree_lists, tc.filesindir);
461 gui_synclist_set_icon_callback(&tree_lists, tree_get_fileicon);
462 if( tc.selected_item >= tc.filesindir)
463 tc.selected_item=tc.filesindir-1;
465 gui_synclist_select_item(&tree_lists, tc.selected_item);
466 #ifdef HAS_BUTTONBAR
467 if (global_settings.buttonbar) {
468 if (*tc.dirfilter < NUM_FILTER_MODES)
469 gui_buttonbar_set(&tree_buttonbar, str(LANG_SYSFONT_DIRBROWSE_F1),
470 str(LANG_SYSFONT_DIRBROWSE_F2),
471 str(LANG_SYSFONT_DIRBROWSE_F3));
472 else
473 gui_buttonbar_set(&tree_buttonbar, "<<<", "", "");
474 gui_buttonbar_draw(&tree_buttonbar);
476 #endif
477 gui_synclist_draw(&tree_lists);
478 gui_synclist_speak_item(&tree_lists);
479 gui_syncstatusbar_draw(&statusbars, true);
480 return tc.filesindir;
483 /* load tracks from specified directory to resume play */
484 void resume_directory(const char *dir)
486 #ifdef HAVE_TAGCACHE
487 bool id3db = *tc.dirfilter == SHOW_ID3DB;
488 #endif
490 if (ft_load(&tc, dir) < 0)
491 return;
492 lastdir[0] = 0;
494 ft_build_playlist(&tc, 0);
496 #ifdef HAVE_TAGCACHE
497 if (id3db)
498 tagtree_load(&tc);
499 #endif
502 /* Returns the current working directory and also writes cwd to buf if
503 non-NULL. In case of error, returns NULL. */
504 char *getcwd(char *buf, int size)
506 if (!buf)
507 return tc.currdir;
508 else if (size > 0)
510 strncpy(buf, tc.currdir, size);
511 return buf;
513 else
514 return NULL;
517 /* Force a reload of the directory next time directory browser is called */
518 void reload_directory(void)
520 reload_dir = true;
523 void get_current_file(char* buffer, int buffer_len)
525 #ifdef HAVE_TAGCACHE
526 /* in ID3DB mode it is a bad idea to call this function */
527 /* (only happens with `follow playlist') */
528 if( *tc.dirfilter == SHOW_ID3DB )
529 return;
530 #endif
532 struct entry* dc = tc.dircache;
533 struct entry* e = &dc[tc.selected_item];
534 snprintf(buffer, buffer_len, "%s/%s", getcwd(NULL,0),
535 tc.dirlength ? e->name : "");
538 /* Allow apps to change our dirfilter directly (required for sub browsers)
539 if they're suddenly going to become a file browser for example */
540 void set_dirfilter(int l_dirfilter)
542 *tc.dirfilter = l_dirfilter;
545 /* Selects a file and update tree context properly */
546 void set_current_file(char *path)
548 char *name;
549 int i;
551 #ifdef HAVE_TAGCACHE
552 /* in ID3DB mode it is a bad idea to call this function */
553 /* (only happens with `follow playlist') */
554 if( *tc.dirfilter == SHOW_ID3DB )
555 return;
556 #endif
558 /* separate directory from filename */
559 /* gets the directory's name and put it into tc.currdir */
560 name = strrchr(path+1,'/');
561 if (name)
563 *name = 0;
564 strcpy(tc.currdir, path);
565 *name = '/';
566 name++;
568 else
570 strcpy(tc.currdir, "/");
571 name = path+1;
574 strcpy(lastfile, name);
577 /* If we changed dir we must recalculate the dirlevel
578 and adjust the selected history properly */
579 if (strncmp(tc.currdir,lastdir,sizeof(lastdir)))
581 tc.dirlevel = 0;
582 tc.selected_item_history[tc.dirlevel] = -1;
584 /* use '/' to calculate dirlevel */
585 for (i = 1; path[i] != '\0'; i++)
587 if (path[i] == '/')
589 tc.dirlevel++;
590 tc.selected_item_history[tc.dirlevel] = -1;
594 if (ft_load(&tc, NULL) >= 0)
596 tc.selected_item = tree_get_file_position(lastfile);
601 /* main loop, handles key events */
602 static int dirbrowse()
604 int numentries=0;
605 char buf[MAX_PATH];
606 unsigned button, oldbutton;
607 bool reload_root = false;
608 int lastfilter = *tc.dirfilter;
609 bool lastsortcase = global_settings.sort_case;
610 bool exit_func = false;
612 char* currdir = tc.currdir; /* just a shortcut */
613 #ifdef HAVE_TAGCACHE
614 bool id3db = *tc.dirfilter == SHOW_ID3DB;
616 if (id3db)
617 curr_context=CONTEXT_ID3DB;
618 else
619 #endif
620 curr_context=CONTEXT_TREE;
621 if (tc.selected_item < 0)
622 tc.selected_item = 0;
623 #ifdef HAVE_TAGCACHE
624 tc.firstpos = 0;
625 lasttable = -1;
626 lastextra = -1;
627 lastfirstpos = 0;
628 #endif
630 start_wps = false;
631 numentries = update_dir();
632 reload_dir = false;
633 if (numentries == -1)
634 return GO_TO_PREVIOUS; /* currdir is not a directory */
636 if (*tc.dirfilter > NUM_FILTER_MODES && numentries==0)
638 gui_syncsplash(HZ*2, ID2P(LANG_NO_FILES));
639 return GO_TO_PREVIOUS; /* No files found for rockbox_browser() */
642 while(1) {
643 struct entry *dircache = tc.dircache;
644 bool restore = false;
645 if (tc.dirlevel < 0)
646 tc.dirlevel = 0; /* shouldnt be needed.. this code needs work! */
647 #ifdef BOOTFILE
648 if (boot_changed) {
649 char *lines[]={ID2P(LANG_BOOT_CHANGED), ID2P(LANG_REBOOT_NOW)};
650 struct text_message message={lines, 2};
651 if(gui_syncyesno_run(&message, NULL, NULL)==YESNO_YES)
652 rolo_load("/" BOOTFILE);
653 restore = true;
654 boot_changed = false;
656 #endif
657 button = get_action(CONTEXT_TREE,
658 list_do_action_timeout(&tree_lists, HZ/2));
659 oldbutton = button;
660 gui_synclist_do_button(&tree_lists, &button,LIST_WRAP_UNLESS_HELD);
661 tc.selected_item = gui_synclist_get_sel_pos(&tree_lists);
662 switch ( button ) {
663 case ACTION_STD_OK:
664 /* nothing to do if no files to display */
665 if ( numentries == 0 )
666 break;
668 #ifdef HAVE_TAGCACHE
669 switch (id3db?tagtree_enter(&tc):ft_enter(&tc))
670 #else
671 switch (ft_enter(&tc))
672 #endif
674 case 1: reload_dir = true; break;
675 case 2: start_wps = true; break;
676 case 3: exit_func = true; break;
677 default: break;
679 restore = true;
680 break;
682 case ACTION_STD_CANCEL:
683 if (*tc.dirfilter > NUM_FILTER_MODES && tc.dirlevel < 1) {
684 exit_func = true;
685 break;
687 if ((*tc.dirfilter == SHOW_ID3DB && tc.dirlevel == 0) ||
688 ((*tc.dirfilter != SHOW_ID3DB && !strcmp(currdir,"/"))))
690 #ifdef HAVE_LCD_BITMAP /* charcell doesnt have ACTION_TREE_PGLEFT so this isnt needed */
691 if (oldbutton == ACTION_TREE_PGLEFT)
692 break;
693 else
694 #endif
695 return GO_TO_ROOT;
698 #ifdef HAVE_TAGCACHE
699 if (id3db)
700 tagtree_exit(&tc);
701 else
702 #endif
703 if (ft_exit(&tc) == 3)
704 exit_func = true;
706 restore = true;
707 break;
709 case ACTION_TREE_STOP:
710 if (list_stop_handler())
711 restore = true;
712 break;
714 case ACTION_STD_MENU:
715 return GO_TO_ROOT;
716 break;
718 #ifdef HAVE_RECORDING
719 case ACTION_STD_REC:
720 return GO_TO_RECSCREEN;
721 #endif
723 case ACTION_TREE_WPS:
724 return GO_TO_PREVIOUS_MUSIC;
725 break;
726 #ifdef HAVE_QUICKSCREEN
727 case ACTION_STD_QUICKSCREEN:
728 /* don't enter f2 from plugin browser */
729 if (*tc.dirfilter < NUM_FILTER_MODES)
731 if (quick_screen_quick(button))
732 reload_dir = true;
733 restore = true;
735 break;
736 #endif
737 #ifdef BUTTON_F3
738 case ACTION_F3:
739 /* don't enter f3 from plugin browser */
740 if (*tc.dirfilter < NUM_FILTER_MODES)
742 if (quick_screen_f3(BUTTON_F3))
743 reload_dir = true;
744 restore = true;
746 break;
747 #endif
749 case ACTION_STD_CONTEXT:
751 int onplay_result;
752 int attr = 0;
754 if(!numentries)
755 onplay_result = onplay(NULL, 0, curr_context);
756 else {
757 #ifdef HAVE_TAGCACHE
758 if (id3db)
760 if (tagtree_get_attr(&tc) == FILE_ATTR_AUDIO)
762 attr = FILE_ATTR_AUDIO;
763 tagtree_get_filename(&tc, buf, sizeof(buf));
765 else
766 attr = ATTR_DIRECTORY;
768 else
769 #endif
771 attr = dircache[tc.selected_item].attr;
773 if (currdir[1]) /* Not in / */
774 snprintf(buf, sizeof buf, "%s/%s",
775 currdir,
776 dircache[tc.selected_item].name);
777 else /* In / */
778 snprintf(buf, sizeof buf, "/%s",
779 dircache[tc.selected_item].name);
781 onplay_result = onplay(buf, attr, curr_context);
783 switch (onplay_result)
785 case ONPLAY_MAINMENU:
786 return GO_TO_ROOT;
788 case ONPLAY_OK:
789 restore = true;
790 break;
792 case ONPLAY_RELOAD_DIR:
793 reload_dir = true;
794 break;
796 case ONPLAY_START_PLAY:
797 return GO_TO_WPS;
798 break;
800 break;
803 case ACTION_NONE:
804 gui_syncstatusbar_draw(&statusbars, false);
805 break;
807 #ifdef HAVE_HOTSWAP
808 case SYS_FS_CHANGED:
809 #ifdef HAVE_TAGCACHE
810 if (!id3db)
811 #endif
812 reload_dir = true;
813 /* The 'dir no longer valid' situation will be caught later
814 * by checking the showdir() result. */
815 break;
816 #endif
818 default:
819 if (default_event_handler(button) == SYS_USB_CONNECTED)
821 if(*tc.dirfilter > NUM_FILTER_MODES)
822 /* leave sub-browsers after usb, doing otherwise
823 might be confusing to the user */
824 exit_func = true;
825 else
826 reload_dir = true;
828 break;
830 if (start_wps)
831 return GO_TO_WPS;
832 if (button)
834 ata_spin();
838 check_rescan:
839 /* do we need to rescan dir? */
840 if (reload_dir || reload_root ||
841 lastfilter != *tc.dirfilter ||
842 lastsortcase != global_settings.sort_case)
844 if (reload_root) {
845 strcpy(currdir, "/");
846 tc.dirlevel = 0;
847 #ifdef HAVE_TAGCACHE
848 tc.currtable = 0;
849 tc.currextra = 0;
850 lasttable = -1;
851 lastextra = -1;
852 #endif
853 reload_root = false;
856 if (!reload_dir)
858 gui_synclist_select_item(&tree_lists, 0);
859 gui_synclist_draw(&tree_lists);
860 tc.selected_item = 0;
861 lastdir[0] = 0;
864 lastfilter = *tc.dirfilter;
865 lastsortcase = global_settings.sort_case;
866 restore = true;
869 if (exit_func)
870 return GO_TO_PREVIOUS;
872 if (restore || reload_dir) {
873 /* restore display */
874 numentries = update_dir();
875 reload_dir = false;
876 if (currdir[1] && (numentries < 0))
877 { /* not in root and reload failed */
878 reload_root = true; /* try root */
879 goto check_rescan;
883 return true;
886 static int plsize = 0;
887 static long pltick;
888 static bool add_dir(char* dirname, int len, int fd)
890 bool abort = false;
891 DIR* dir;
893 /* check for user abort */
894 if (action_userabort(TIMEOUT_NOBLOCK))
895 return true;
897 dir = opendir(dirname);
898 if(!dir)
899 return true;
901 while (true) {
902 struct dirent *entry;
904 entry = readdir(dir);
905 if (!entry)
906 break;
907 if (entry->attribute & ATTR_DIRECTORY) {
908 int dirlen = strlen(dirname);
909 bool result;
911 if (!strcmp((char *)entry->d_name, ".") ||
912 !strcmp((char *)entry->d_name, ".."))
913 continue;
915 if (dirname[1])
916 snprintf(dirname+dirlen, len-dirlen, "/%s", entry->d_name);
917 else
918 snprintf(dirname, len, "/%s", entry->d_name);
920 result = add_dir(dirname, len, fd);
921 dirname[dirlen] = '\0';
922 if (result) {
923 abort = true;
924 break;
927 else {
928 int x = strlen((char *)entry->d_name);
929 int i;
930 char *cp = strrchr((char *)entry->d_name,'.');
932 if (cp) {
933 cp++;
935 /* add all supported audio files to playlists */
936 for (i=0; i < filetypes_count; i++) {
937 if (filetypes[i].tree_attr == FILE_ATTR_AUDIO) {
938 if (!strcasecmp(cp, filetypes[i].extension)) {
939 char buf[8];
940 int i;
941 write(fd, dirname, strlen(dirname));
942 write(fd, "/", 1);
943 write(fd, entry->d_name, x);
944 write(fd, "\n", 1);
946 plsize++;
947 if(TIME_AFTER(current_tick, pltick+HZ/4)) {
948 pltick = current_tick;
950 snprintf(buf, sizeof buf, "%d", plsize);
951 #ifdef HAVE_LCD_BITMAP
952 FOR_NB_SCREENS(i)
954 screens[i].puts(0, 4, (unsigned char *)buf);
955 gui_textarea_update(&screens[i]);
957 #else
958 if (plsize > 999)
959 x=7;
960 else if (plsize > 99)
961 x=8;
962 else if (plsize > 9)
963 x=9;
964 else
965 x = 10;
967 FOR_NB_SCREENS(i) {
968 screens[i].puts(x,0,buf);
970 #endif
972 break;
979 closedir(dir);
981 return abort;
984 bool create_playlist(void)
986 int fd;
987 int i;
988 char filename[MAX_PATH];
990 pltick = current_tick;
992 snprintf(filename, sizeof filename, "%s.m3u8",
993 tc.currdir[1] ? tc.currdir : "/root");
994 FOR_NB_SCREENS(i)
996 gui_textarea_clear(&screens[i]);
997 screens[i].puts(0, 0, str(LANG_CREATING));
998 screens[i].puts_scroll(0, 1, (unsigned char *)filename);
999 #if defined(HAVE_LCD_BITMAP) || defined(SIMULATOR)
1000 gui_textarea_update(&screens[i]);
1001 #endif
1003 fd = creat(filename);
1004 if (fd < 0)
1005 return false;
1007 trigger_cpu_boost();
1009 snprintf(filename, sizeof(filename), "%s",
1010 tc.currdir[1] ? tc.currdir : "/");
1011 plsize = 0;
1012 add_dir(filename, sizeof(filename), fd);
1013 close(fd);
1015 cancel_cpu_boost();
1016 sleep(HZ);
1018 return true;
1021 int rockbox_browse(const char *root, int dirfilter)
1023 int ret_val = 0;
1024 int *last_filter = tc.dirfilter;
1025 tc.dirfilter = &dirfilter;
1027 reload_dir = true;
1028 if (dirfilter >= NUM_FILTER_MODES)
1030 static struct tree_context backup;
1031 int last_context;
1033 backup = tc;
1034 tc.selected_item = 0;
1035 tc.dirlevel = 0;
1036 memcpy(tc.currdir, root, sizeof(tc.currdir));
1037 start_wps = false;
1038 last_context = curr_context;
1040 ret_val = dirbrowse();
1041 tc = backup;
1042 curr_context = last_context;
1044 else
1046 static char buf[MAX_PATH];
1047 if (dirfilter != SHOW_ID3DB)
1048 tc.dirfilter = &global_settings.dirfilter;
1049 strcpy(buf,root);
1050 set_current_file(buf);
1051 ret_val = dirbrowse();
1053 tc.dirfilter = last_filter;
1054 return ret_val;
1057 void tree_mem_init(void)
1059 /* We copy the settings value in case it is changed by the user. We can't
1060 use it until the next reboot. */
1061 max_files = global_settings.max_files_in_dir;
1063 /* initialize tree context struct */
1064 memset(&tc, 0, sizeof(tc));
1065 tc.dirfilter = &global_settings.dirfilter;
1067 tc.name_buffer_size = AVERAGE_FILENAME_LENGTH * max_files;
1068 tc.name_buffer = buffer_alloc(tc.name_buffer_size);
1070 tc.dircache_size = max_files * sizeof(struct entry);
1071 tc.dircache = buffer_alloc(tc.dircache_size);
1072 tree_get_filetypes(&filetypes, &filetypes_count);
1075 bool bookmark_play(char *resume_file, int index, int offset, int seed,
1076 char *filename)
1078 int i;
1079 char* suffix = strrchr(resume_file, '.');
1080 bool started = false;
1082 if (suffix != NULL &&
1083 (!strcasecmp(suffix, ".m3u") || !strcasecmp(suffix, ".m3u8")))
1085 /* Playlist playback */
1086 char* slash;
1087 /* check that the file exists */
1088 int fd = open(resume_file, O_RDONLY);
1089 if(fd<0)
1090 return false;
1091 close(fd);
1093 slash = strrchr(resume_file,'/');
1094 if (slash)
1096 char* cp;
1097 *slash=0;
1099 cp=resume_file;
1100 if (!cp[0])
1101 cp="/";
1103 if (playlist_create(cp, slash+1) != -1)
1105 if (global_settings.playlist_shuffle)
1106 playlist_shuffle(seed, -1);
1107 playlist_start(index,offset);
1108 started = true;
1110 *slash='/';
1113 else
1115 /* Directory playback */
1116 lastdir[0]='\0';
1117 if (playlist_create(resume_file, NULL) != -1)
1119 char* peek_filename;
1120 resume_directory(resume_file);
1121 if (global_settings.playlist_shuffle)
1122 playlist_shuffle(seed, -1);
1124 /* Check if the file is at the same spot in the directory,
1125 else search for it */
1126 peek_filename = playlist_peek(index);
1128 if (peek_filename == NULL)
1129 return false;
1131 if (strcmp(strrchr(peek_filename, '/') + 1, filename))
1133 for ( i=0; i < playlist_amount(); i++ )
1135 peek_filename = playlist_peek(i);
1137 if (peek_filename == NULL)
1138 return false;
1140 if (!strcmp(strrchr(peek_filename, '/') + 1, filename))
1141 break;
1143 if (i < playlist_amount())
1144 index = i;
1145 else
1146 return false;
1148 playlist_start(index,offset);
1149 started = true;
1153 if (started)
1154 start_wps = true;
1155 return started;
1158 static void say_filetype(int attr)
1160 /* try to find a voice ID for the extension, if known */
1161 int j;
1162 attr &= FILE_ATTR_MASK; /* file type */
1163 for (j=0; j<filetypes_count; j++)
1164 if (attr == filetypes[j].tree_attr)
1166 talk_id(filetypes[j].voiceclip, true);
1167 return;
1171 static int ft_play_dirname(char* name)
1173 int fd;
1174 char dirname_mp3_filename[MAX_PATH+1];
1176 #if CONFIG_CODEC != SWCODEC
1177 if (audio_status() & AUDIO_STATUS_PLAY)
1178 return 0;
1179 #endif
1181 snprintf(dirname_mp3_filename, sizeof(dirname_mp3_filename), "%s/%s/%s",
1182 tc.currdir[1] ? tc.currdir : "" , name,
1183 dir_thumbnail_name);
1185 DEBUGF("Checking for %s\n", dirname_mp3_filename);
1187 fd = open(dirname_mp3_filename, O_RDONLY);
1188 if (fd < 0)
1190 DEBUGF("Failed to find: %s\n", dirname_mp3_filename);
1191 return -1;
1194 close(fd);
1196 DEBUGF("Found: %s\n", dirname_mp3_filename);
1198 talk_file(dirname_mp3_filename, false);
1199 if(global_settings.talk_filetype)
1200 talk_id(VOICE_DIR, true);
1201 return 1;
1204 static void ft_play_filename(char *dir, char *file)
1206 char name_mp3_filename[MAX_PATH+1];
1208 #if CONFIG_CODEC != SWCODEC
1209 if (audio_status() & AUDIO_STATUS_PLAY)
1210 return;
1211 #endif
1213 if (strcasecmp(&file[strlen(file) - strlen(file_thumbnail_ext)],
1214 file_thumbnail_ext))
1215 { /* file has no .talk extension */
1216 snprintf(name_mp3_filename, sizeof(name_mp3_filename),
1217 "%s/%s%s", dir, file, file_thumbnail_ext);
1219 talk_file(name_mp3_filename, false);
1221 else
1222 { /* it already is a .talk file, play this directly */
1223 snprintf(name_mp3_filename, sizeof(name_mp3_filename),
1224 "%s/%s", dir, file);
1225 talk_id(LANG_VOICE_DIR_HOVER, false); /* prefix it */
1226 talk_file(name_mp3_filename, true);
1230 /* These two functions are called by the USB and shutdown handlers */
1231 void tree_flush(void)
1233 scrobbler_shutdown();
1234 #ifdef HAVE_TAGCACHE
1235 tagcache_shutdown();
1236 #endif
1237 playlist_shutdown();
1239 #ifdef HAVE_TC_RAMCACHE
1240 tagcache_unload_ramcache();
1241 #endif
1243 #ifdef HAVE_DIRCACHE
1245 int old_val = global_status.dircache_size;
1246 if (global_settings.dircache)
1248 if (!dircache_is_initializing())
1249 global_status.dircache_size = dircache_get_cache_size();
1250 # ifdef HAVE_EEPROM_SETTINGS
1251 if (firmware_settings.initialized)
1252 dircache_save();
1253 # endif
1254 dircache_disable();
1256 else
1258 global_status.dircache_size = 0;
1260 if (old_val != global_status.dircache_size)
1261 status_save();
1263 #endif
1266 void tree_restore(void)
1268 #ifdef HAVE_EEPROM_SETTINGS
1269 firmware_settings.disk_clean = false;
1270 #endif
1272 #ifdef HAVE_TC_RAMCACHE
1273 remove(TAGCACHE_STATEFILE);
1274 #endif
1276 #ifdef HAVE_DIRCACHE
1277 remove(DIRCACHE_FILE);
1278 if (global_settings.dircache)
1280 /* Print "Scanning disk..." to the display. */
1281 int i;
1282 FOR_NB_SCREENS(i)
1284 screens[i].putsxy((LCD_WIDTH/2) -
1285 ((strlen(str(LANG_SCANNING_DISK)) *
1286 screens[i].char_width)/2),
1287 LCD_HEIGHT-screens[i].char_height*3,
1288 str(LANG_SCANNING_DISK));
1289 gui_textarea_update(&screens[i]);
1291 cond_talk_ids_fq(LANG_SCANNING_DISK);
1293 dircache_build(global_status.dircache_size);
1295 /* Clean the text when we are done. */
1296 FOR_NB_SCREENS(i)
1298 gui_textarea_clear(&screens[i]);
1301 #endif
1302 #ifdef HAVE_TAGCACHE
1303 tagcache_start_scan();
1304 #endif
1305 scrobbler_init();