remove the 2nd last evil use of ata_disk_is_active() in apps/
[Rockbox.git] / apps / tree.c
blobd1d9adb3b2278c51d9d627227188623e639ea387
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,
116 char *buffer, size_t buffer_len)
118 struct tree_context * local_tc=(struct tree_context *)data;
119 char *name;
120 int attr=0;
121 bool stripit = false;
122 #ifdef HAVE_TAGCACHE
123 bool id3db = *(local_tc->dirfilter) == SHOW_ID3DB;
125 if (id3db)
127 return tagtree_get_entry(&tc, selected_item)->name;
129 else
130 #endif
132 struct entry* dc = local_tc->dircache;
133 struct entry* e = &dc[selected_item];
134 name = e->name;
135 attr = e->attr;
138 if(!(attr & ATTR_DIRECTORY))
140 switch(global_settings.show_filename_ext)
142 case 0:
143 /* show file extension: off */
144 stripit = true;
145 break;
146 case 1:
147 /* show file extension: on */
148 break;
149 case 2:
150 /* show file extension: only unknown types */
151 stripit = filetype_supported(attr);
152 break;
153 case 3:
154 default:
155 /* show file extension: only when viewing all */
156 stripit = (*(local_tc->dirfilter) != SHOW_ID3DB) &&
157 (*(local_tc->dirfilter) != SHOW_ALL);
158 break;
162 if(stripit)
164 return(strip_extension(buffer, buffer_len, name));
166 return(name);
169 #ifdef HAVE_LCD_COLOR
170 static int tree_get_filecolor(int selected_item, void * data)
172 if (*tc.dirfilter == SHOW_ID3DB)
173 return -1;
174 struct tree_context * local_tc=(struct tree_context *)data;
175 struct entry* dc = local_tc->dircache;
176 struct entry* e = &dc[selected_item];
177 return filetype_get_color(e->name, e->attr);
179 #endif
181 static int tree_get_fileicon(int selected_item, void * data)
183 struct tree_context * local_tc=(struct tree_context *)data;
184 #ifdef HAVE_TAGCACHE
185 bool id3db = *(local_tc->dirfilter) == SHOW_ID3DB;
186 if (id3db) {
187 return tagtree_get_icon(&tc);
189 else
190 #endif
192 struct entry* dc = local_tc->dircache;
193 struct entry* e = &dc[selected_item];
194 return filetype_get_icon(e->attr);
198 static int tree_voice_cb(int selected_item, void * data)
200 struct tree_context * local_tc=(struct tree_context *)data;
201 char *name;
202 int attr=0;
203 #ifdef HAVE_TAGCACHE
204 bool id3db = *(local_tc->dirfilter) == SHOW_ID3DB;
206 if (id3db)
208 attr = tagtree_get_attr(local_tc);
209 name = tagtree_get_entry(local_tc, selected_item)->name;
211 else
212 #endif
214 struct entry* dc = local_tc->dircache;
215 struct entry* e = &dc[selected_item];
216 name = e->name;
217 attr = e->attr;
219 bool is_dir = (attr & ATTR_DIRECTORY);
220 bool did_clip = false;
221 /* First the .talk clip case */
222 if(is_dir)
224 if(global_settings.talk_dir_clip)
226 DEBUGF("Playing directory thumbnail: %s", local_tc->currdir);
227 did_clip = true;
228 if(ft_play_dirname(name) <0)
229 /* failed, not existing */
230 did_clip = false;
232 } else { /* it's a file */
233 if (global_settings.talk_file_clip && (attr & FILE_ATTR_THUMBNAIL))
235 did_clip = true;
236 DEBUGF("Playing file thumbnail: %s/%s%s\n",
237 local_tc->currdir, name, file_thumbnail_ext);
238 ft_play_filename(local_tc->currdir, name);
241 if(!did_clip)
243 /* say the number or spell if required or as a fallback */
244 switch (is_dir ? global_settings.talk_dir : global_settings.talk_file)
246 case 1: /* as numbers */
247 talk_id(is_dir ? VOICE_DIR : VOICE_FILE, false);
248 talk_number(selected_item+1 - (is_dir ? 0 : local_tc->dirsindir),
249 true);
250 if(global_settings.talk_filetype
251 && !is_dir && *local_tc->dirfilter < NUM_FILTER_MODES)
252 say_filetype(attr);
253 break;
254 case 2: /* spelled */
255 talk_shutup();
256 if(global_settings.talk_filetype)
258 if(is_dir)
259 talk_id(VOICE_DIR, true);
260 else if(*local_tc->dirfilter < NUM_FILTER_MODES)
261 say_filetype(attr);
263 talk_spell(name, true);
264 break;
267 return 0;
270 bool check_rockboxdir(void)
272 if(!dir_exists(ROCKBOX_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 return true;
287 /* do this really late in the init sequence */
288 void tree_gui_init(void)
290 gui_sync_wps_screen_init();
292 check_rockboxdir();
294 strcpy(tc.currdir, "/");
296 #ifdef HAVE_LCD_CHARCELLS
297 int i;
298 FOR_NB_SCREENS(i)
299 screens[i].double_height(false);
300 #endif
301 #ifdef HAS_BUTTONBAR
302 gui_buttonbar_init(&tree_buttonbar);
303 /* since archos only have one screen, no need to create more than that */
304 gui_buttonbar_set_display(&tree_buttonbar, &(screens[SCREEN_MAIN]) );
305 #endif
306 gui_synclist_init(&tree_lists, &tree_get_filename, &tc, false, 1, NULL);
307 gui_synclist_set_voice_callback(&tree_lists, tree_voice_cb);
308 gui_synclist_set_icon_callback(&tree_lists, &tree_get_fileicon);
309 #ifdef HAVE_LCD_COLOR
310 gui_synclist_set_color_callback(&tree_lists, &tree_get_filecolor);
311 #endif
316 struct tree_context* tree_get_context(void)
318 return &tc;
322 * Returns the position of a given file in the current directory
323 * returns -1 if not found
325 static int tree_get_file_position(char * filename)
327 int i;
329 /* use lastfile to determine the selected item (default=0) */
330 for (i=0; i < tc.filesindir; i++)
332 struct entry* dc = tc.dircache;
333 struct entry* e = &dc[i];
334 if (!strcasecmp(e->name, filename))
335 return(i);
337 return(-1);/* no file can match, returns undefined */
341 * Called when a new dir is loaded (for example when returning from other apps ...)
342 * also completely redraws the tree
344 static int update_dir(void)
346 bool changed = false;
347 #ifdef HAVE_TAGCACHE
348 bool id3db = *tc.dirfilter == SHOW_ID3DB;
349 /* Checks for changes */
350 if (id3db) {
351 if (tc.currtable != lasttable ||
352 tc.currextra != lastextra ||
353 tc.firstpos != lastfirstpos ||
354 reload_dir)
356 if (tagtree_load(&tc) < 0)
357 return -1;
359 lasttable = tc.currtable;
360 lastextra = tc.currextra;
361 lastfirstpos = tc.firstpos;
362 changed = true;
365 else
366 #endif
368 /* if the tc.currdir has been changed, reload it ...*/
369 if (strncmp(tc.currdir, lastdir, sizeof(lastdir)) || reload_dir)
371 if (ft_load(&tc, NULL) < 0)
372 return -1;
373 strcpy(lastdir, tc.currdir);
374 changed = true;
377 /* if selected item is undefined */
378 if (tc.selected_item == -1)
380 /* use lastfile to determine the selected item */
381 tc.selected_item = tree_get_file_position(lastfile);
383 /* If the file doesn't exists, select the first one (default) */
384 if(tc.selected_item < 0)
385 tc.selected_item = 0;
386 changed = true;
388 if (changed)
391 #ifdef HAVE_TAGCACHE
392 !id3db &&
393 #endif
394 (tc.dirfull || tc.filesindir == global_settings.max_files_in_dir) )
396 gui_syncsplash(HZ, ID2P(LANG_SHOWDIR_BUFFER_FULL));
399 #ifdef HAVE_TAGCACHE
400 if (id3db)
402 #ifdef HAVE_LCD_BITMAP
403 if (global_settings.show_path_in_browser == SHOW_PATH_FULL
404 || global_settings.show_path_in_browser == SHOW_PATH_CURRENT)
406 gui_synclist_set_title(&tree_lists, tagtree_get_title(&tc),
407 filetype_get_icon(ATTR_DIRECTORY));
409 else
411 /* Must clear the title as the list is reused */
412 gui_synclist_set_title(&tree_lists, NULL, NOICON);
414 #endif
416 else
417 #endif
419 #ifdef HAVE_LCD_BITMAP
420 if (global_settings.show_path_in_browser &&
421 *(tc.dirfilter) == SHOW_PLUGINS)
423 char *title;
424 if (!strcmp(tc.currdir, PLUGIN_GAMES_DIR))
425 title = str(LANG_PLUGIN_GAMES);
426 else if (!strcmp(tc.currdir, PLUGIN_APPS_DIR))
427 title = str(LANG_PLUGIN_APPS);
428 else if (!strcmp(tc.currdir, PLUGIN_DEMOS_DIR))
429 title = str(LANG_PLUGIN_DEMOS);
430 else title = str(LANG_PLUGINS);
431 gui_synclist_set_title(&tree_lists, title, Icon_Plugin);
433 else if (global_settings.show_path_in_browser == SHOW_PATH_FULL)
435 gui_synclist_set_title(&tree_lists, tc.currdir,
436 filetype_get_icon(ATTR_DIRECTORY));
438 else if (global_settings.show_path_in_browser == SHOW_PATH_CURRENT)
440 char *title = strrchr(tc.currdir, '/') + 1;
441 if (*title == '\0')
443 /* Display "Files" for the root dir */
444 gui_synclist_set_title(&tree_lists, str(LANG_DIR_BROWSER),
445 filetype_get_icon(ATTR_DIRECTORY));
447 else
448 gui_synclist_set_title(&tree_lists, title,
449 filetype_get_icon(ATTR_DIRECTORY));
451 else
453 /* Must clear the title as the list is reused */
454 gui_synclist_set_title(&tree_lists, NULL, NOICON);
456 #endif
459 gui_synclist_set_nb_items(&tree_lists, tc.filesindir);
460 gui_synclist_set_icon_callback(&tree_lists, tree_get_fileicon);
461 if( tc.selected_item >= tc.filesindir)
462 tc.selected_item=tc.filesindir-1;
464 gui_synclist_select_item(&tree_lists, tc.selected_item);
465 #ifdef HAS_BUTTONBAR
466 if (global_settings.buttonbar) {
467 if (*tc.dirfilter < NUM_FILTER_MODES)
468 gui_buttonbar_set(&tree_buttonbar, str(LANG_SYSFONT_DIRBROWSE_F1),
469 str(LANG_SYSFONT_DIRBROWSE_F2),
470 str(LANG_SYSFONT_DIRBROWSE_F3));
471 else
472 gui_buttonbar_set(&tree_buttonbar, "<<<", "", "");
473 gui_buttonbar_draw(&tree_buttonbar);
475 #endif
476 gui_synclist_draw(&tree_lists);
477 gui_synclist_speak_item(&tree_lists);
478 gui_syncstatusbar_draw(&statusbars, true);
479 return tc.filesindir;
482 /* load tracks from specified directory to resume play */
483 void resume_directory(const char *dir)
485 #ifdef HAVE_TAGCACHE
486 bool id3db = *tc.dirfilter == SHOW_ID3DB;
487 #endif
489 if (ft_load(&tc, dir) < 0)
490 return;
491 lastdir[0] = 0;
493 ft_build_playlist(&tc, 0);
495 #ifdef HAVE_TAGCACHE
496 if (id3db)
497 tagtree_load(&tc);
498 #endif
501 /* Returns the current working directory and also writes cwd to buf if
502 non-NULL. In case of error, returns NULL. */
503 char *getcwd(char *buf, int size)
505 if (!buf)
506 return tc.currdir;
507 else if (size > 0)
509 strncpy(buf, tc.currdir, size);
510 return buf;
512 else
513 return NULL;
516 /* Force a reload of the directory next time directory browser is called */
517 void reload_directory(void)
519 reload_dir = true;
522 void get_current_file(char* buffer, int buffer_len)
524 #ifdef HAVE_TAGCACHE
525 /* in ID3DB mode it is a bad idea to call this function */
526 /* (only happens with `follow playlist') */
527 if( *tc.dirfilter == SHOW_ID3DB )
528 return;
529 #endif
531 struct entry* dc = tc.dircache;
532 struct entry* e = &dc[tc.selected_item];
533 snprintf(buffer, buffer_len, "%s/%s", getcwd(NULL,0),
534 tc.dirlength ? e->name : "");
537 /* Allow apps to change our dirfilter directly (required for sub browsers)
538 if they're suddenly going to become a file browser for example */
539 void set_dirfilter(int l_dirfilter)
541 *tc.dirfilter = l_dirfilter;
544 /* Selects a file and update tree context properly */
545 void set_current_file(char *path)
547 char *name;
548 int i;
550 #ifdef HAVE_TAGCACHE
551 /* in ID3DB mode it is a bad idea to call this function */
552 /* (only happens with `follow playlist') */
553 if( *tc.dirfilter == SHOW_ID3DB )
554 return;
555 #endif
557 /* separate directory from filename */
558 /* gets the directory's name and put it into tc.currdir */
559 name = strrchr(path+1,'/');
560 if (name)
562 *name = 0;
563 strcpy(tc.currdir, path);
564 *name = '/';
565 name++;
567 else
569 strcpy(tc.currdir, "/");
570 name = path+1;
573 strcpy(lastfile, name);
576 /* If we changed dir we must recalculate the dirlevel
577 and adjust the selected history properly */
578 if (strncmp(tc.currdir,lastdir,sizeof(lastdir)))
580 tc.dirlevel = 0;
581 tc.selected_item_history[tc.dirlevel] = -1;
583 /* use '/' to calculate dirlevel */
584 for (i = 1; path[i] != '\0'; i++)
586 if (path[i] == '/')
588 tc.dirlevel++;
589 tc.selected_item_history[tc.dirlevel] = -1;
593 if (ft_load(&tc, NULL) >= 0)
595 tc.selected_item = tree_get_file_position(lastfile);
600 /* main loop, handles key events */
601 static int dirbrowse()
603 int numentries=0;
604 char buf[MAX_PATH];
605 unsigned button, oldbutton;
606 bool reload_root = false;
607 int lastfilter = *tc.dirfilter;
608 bool lastsortcase = global_settings.sort_case;
609 bool exit_func = false;
611 char* currdir = tc.currdir; /* just a shortcut */
612 #ifdef HAVE_TAGCACHE
613 bool id3db = *tc.dirfilter == SHOW_ID3DB;
615 if (id3db)
616 curr_context=CONTEXT_ID3DB;
617 else
618 #endif
619 curr_context=CONTEXT_TREE;
620 if (tc.selected_item < 0)
621 tc.selected_item = 0;
622 #ifdef HAVE_TAGCACHE
623 tc.firstpos = 0;
624 lasttable = -1;
625 lastextra = -1;
626 lastfirstpos = 0;
627 #endif
629 start_wps = false;
630 numentries = update_dir();
631 reload_dir = false;
632 if (numentries == -1)
633 return GO_TO_PREVIOUS; /* currdir is not a directory */
635 if (*tc.dirfilter > NUM_FILTER_MODES && numentries==0)
637 gui_syncsplash(HZ*2, ID2P(LANG_NO_FILES));
638 return GO_TO_PREVIOUS; /* No files found for rockbox_browser() */
641 while(1) {
642 struct entry *dircache = tc.dircache;
643 bool restore = false;
644 if (tc.dirlevel < 0)
645 tc.dirlevel = 0; /* shouldnt be needed.. this code needs work! */
646 #ifdef BOOTFILE
647 if (boot_changed) {
648 static const char *lines[]={ID2P(LANG_BOOT_CHANGED), ID2P(LANG_REBOOT_NOW)};
649 static const struct text_message message={lines, 2};
650 if(gui_syncyesno_run(&message, NULL, NULL)==YESNO_YES)
651 rolo_load("/" BOOTFILE);
652 restore = true;
653 boot_changed = false;
655 #endif
656 button = get_action(CONTEXT_TREE,
657 list_do_action_timeout(&tree_lists, HZ/2));
658 oldbutton = button;
659 gui_synclist_do_button(&tree_lists, &button,LIST_WRAP_UNLESS_HELD);
660 tc.selected_item = gui_synclist_get_sel_pos(&tree_lists);
661 switch ( button ) {
662 case ACTION_STD_OK:
663 /* nothing to do if no files to display */
664 if ( numentries == 0 )
665 break;
667 #ifdef HAVE_TAGCACHE
668 switch (id3db?tagtree_enter(&tc):ft_enter(&tc))
669 #else
670 switch (ft_enter(&tc))
671 #endif
673 case 1: reload_dir = true; break;
674 case 2: start_wps = true; break;
675 case 3: exit_func = true; break;
676 default: break;
678 restore = true;
679 break;
681 case ACTION_STD_CANCEL:
682 if (*tc.dirfilter > NUM_FILTER_MODES && tc.dirlevel < 1) {
683 exit_func = true;
684 break;
686 if ((*tc.dirfilter == SHOW_ID3DB && tc.dirlevel == 0) ||
687 ((*tc.dirfilter != SHOW_ID3DB && !strcmp(currdir,"/"))))
689 #ifdef HAVE_LCD_BITMAP /* charcell doesnt have ACTION_TREE_PGLEFT so this isnt needed */
690 if (oldbutton == ACTION_TREE_PGLEFT)
691 break;
692 else
693 #endif
694 return GO_TO_ROOT;
697 #ifdef HAVE_TAGCACHE
698 if (id3db)
699 tagtree_exit(&tc);
700 else
701 #endif
702 if (ft_exit(&tc) == 3)
703 exit_func = true;
705 restore = true;
706 break;
708 case ACTION_TREE_STOP:
709 if (list_stop_handler())
710 restore = true;
711 break;
713 case ACTION_STD_MENU:
714 return GO_TO_ROOT;
715 break;
717 #ifdef HAVE_RECORDING
718 case ACTION_STD_REC:
719 return GO_TO_RECSCREEN;
720 #endif
722 case ACTION_TREE_WPS:
723 return GO_TO_PREVIOUS_MUSIC;
724 break;
725 #ifdef HAVE_QUICKSCREEN
726 case ACTION_STD_QUICKSCREEN:
727 /* don't enter f2 from plugin browser */
728 if (*tc.dirfilter < NUM_FILTER_MODES)
730 if (quick_screen_quick(button))
731 reload_dir = true;
732 restore = true;
734 break;
735 #endif
736 #ifdef BUTTON_F3
737 case ACTION_F3:
738 /* don't enter f3 from plugin browser */
739 if (*tc.dirfilter < NUM_FILTER_MODES)
741 if (quick_screen_f3(ACTION_F3))
742 reload_dir = true;
743 restore = true;
745 break;
746 #endif
748 case ACTION_STD_CONTEXT:
750 int onplay_result;
751 int attr = 0;
753 if(!numentries)
754 onplay_result = onplay(NULL, 0, curr_context);
755 else {
756 #ifdef HAVE_TAGCACHE
757 if (id3db)
759 if (tagtree_get_attr(&tc) == FILE_ATTR_AUDIO)
761 attr = FILE_ATTR_AUDIO;
762 tagtree_get_filename(&tc, buf, sizeof(buf));
764 else
765 attr = ATTR_DIRECTORY;
767 else
768 #endif
770 attr = dircache[tc.selected_item].attr;
772 if (currdir[1]) /* Not in / */
773 snprintf(buf, sizeof buf, "%s/%s",
774 currdir,
775 dircache[tc.selected_item].name);
776 else /* In / */
777 snprintf(buf, sizeof buf, "/%s",
778 dircache[tc.selected_item].name);
780 onplay_result = onplay(buf, attr, curr_context);
782 switch (onplay_result)
784 case ONPLAY_MAINMENU:
785 return GO_TO_ROOT;
787 case ONPLAY_OK:
788 restore = true;
789 break;
791 case ONPLAY_RELOAD_DIR:
792 reload_dir = true;
793 break;
795 case ONPLAY_START_PLAY:
796 return GO_TO_WPS;
797 break;
799 break;
802 case ACTION_NONE:
803 gui_syncstatusbar_draw(&statusbars, false);
804 break;
806 #ifdef HAVE_HOTSWAP
807 case SYS_FS_CHANGED:
808 #ifdef HAVE_TAGCACHE
809 if (!id3db)
810 #endif
811 reload_dir = true;
812 /* The 'dir no longer valid' situation will be caught later
813 * by checking the showdir() result. */
814 break;
815 #endif
817 default:
818 if (default_event_handler(button) == SYS_USB_CONNECTED)
820 if(*tc.dirfilter > NUM_FILTER_MODES)
821 /* leave sub-browsers after usb, doing otherwise
822 might be confusing to the user */
823 exit_func = true;
824 else
825 reload_dir = true;
827 break;
829 if (start_wps)
830 return GO_TO_WPS;
831 if (button)
833 ata_spin();
837 check_rescan:
838 /* do we need to rescan dir? */
839 if (reload_dir || reload_root ||
840 lastfilter != *tc.dirfilter ||
841 lastsortcase != global_settings.sort_case)
843 if (reload_root) {
844 strcpy(currdir, "/");
845 tc.dirlevel = 0;
846 #ifdef HAVE_TAGCACHE
847 tc.currtable = 0;
848 tc.currextra = 0;
849 lasttable = -1;
850 lastextra = -1;
851 #endif
852 reload_root = false;
855 if (!reload_dir)
857 gui_synclist_select_item(&tree_lists, 0);
858 gui_synclist_draw(&tree_lists);
859 tc.selected_item = 0;
860 lastdir[0] = 0;
863 lastfilter = *tc.dirfilter;
864 lastsortcase = global_settings.sort_case;
865 restore = true;
868 if (exit_func)
869 return GO_TO_PREVIOUS;
871 if (restore || reload_dir) {
872 /* restore display */
873 numentries = update_dir();
874 reload_dir = false;
875 if (currdir[1] && (numentries < 0))
876 { /* not in root and reload failed */
877 reload_root = true; /* try root */
878 goto check_rescan;
882 return true;
885 static int plsize = 0;
886 static long pltick;
887 static bool add_dir(char* dirname, int len, int fd)
889 bool abort = false;
890 DIR* dir;
892 /* check for user abort */
893 if (action_userabort(TIMEOUT_NOBLOCK))
894 return true;
896 dir = opendir(dirname);
897 if(!dir)
898 return true;
900 while (true) {
901 struct dirent *entry;
903 entry = readdir(dir);
904 if (!entry)
905 break;
906 if (entry->attribute & ATTR_DIRECTORY) {
907 int dirlen = strlen(dirname);
908 bool result;
910 if (!strcmp((char *)entry->d_name, ".") ||
911 !strcmp((char *)entry->d_name, ".."))
912 continue;
914 if (dirname[1])
915 snprintf(dirname+dirlen, len-dirlen, "/%s", entry->d_name);
916 else
917 snprintf(dirname, len, "/%s", entry->d_name);
919 result = add_dir(dirname, len, fd);
920 dirname[dirlen] = '\0';
921 if (result) {
922 abort = true;
923 break;
926 else {
927 int x = strlen((char *)entry->d_name);
928 int i;
929 char *cp = strrchr((char *)entry->d_name,'.');
931 if (cp) {
932 cp++;
934 /* add all supported audio files to playlists */
935 for (i=0; i < filetypes_count; i++) {
936 if (filetypes[i].tree_attr == FILE_ATTR_AUDIO) {
937 if (!strcasecmp(cp, filetypes[i].extension)) {
938 char buf[8];
939 int i;
940 write(fd, dirname, strlen(dirname));
941 write(fd, "/", 1);
942 write(fd, entry->d_name, x);
943 write(fd, "\n", 1);
945 plsize++;
946 if(TIME_AFTER(current_tick, pltick+HZ/4)) {
947 pltick = current_tick;
949 snprintf(buf, sizeof buf, "%d", plsize);
950 #ifdef HAVE_LCD_BITMAP
951 FOR_NB_SCREENS(i)
953 screens[i].puts(0, 4, (unsigned char *)buf);
954 gui_textarea_update(&screens[i]);
956 #else
957 if (plsize > 999)
958 x=7;
959 else if (plsize > 99)
960 x=8;
961 else if (plsize > 9)
962 x=9;
963 else
964 x = 10;
966 FOR_NB_SCREENS(i) {
967 screens[i].puts(x,0,buf);
969 #endif
971 break;
978 closedir(dir);
980 return abort;
983 bool create_playlist(void)
985 int fd;
986 int i;
987 char filename[MAX_PATH];
989 pltick = current_tick;
991 snprintf(filename, sizeof filename, "%s.m3u8",
992 tc.currdir[1] ? tc.currdir : "/root");
993 FOR_NB_SCREENS(i)
995 gui_textarea_clear(&screens[i]);
996 screens[i].puts(0, 0, str(LANG_CREATING));
997 screens[i].puts_scroll(0, 1, (unsigned char *)filename);
998 #if defined(HAVE_LCD_BITMAP) || defined(SIMULATOR)
999 gui_textarea_update(&screens[i]);
1000 #endif
1002 fd = creat(filename);
1003 if (fd < 0)
1004 return false;
1006 trigger_cpu_boost();
1008 snprintf(filename, sizeof(filename), "%s",
1009 tc.currdir[1] ? tc.currdir : "/");
1010 plsize = 0;
1011 add_dir(filename, sizeof(filename), fd);
1012 close(fd);
1014 cancel_cpu_boost();
1015 sleep(HZ);
1017 return true;
1020 int rockbox_browse(const char *root, int dirfilter)
1022 int ret_val = 0;
1023 int *last_filter = tc.dirfilter;
1024 tc.dirfilter = &dirfilter;
1026 reload_dir = true;
1027 if (dirfilter >= NUM_FILTER_MODES)
1029 static struct tree_context backup;
1030 int last_context;
1032 backup = tc;
1033 tc.selected_item = 0;
1034 tc.dirlevel = 0;
1035 memcpy(tc.currdir, root, sizeof(tc.currdir));
1036 start_wps = false;
1037 last_context = curr_context;
1039 ret_val = dirbrowse();
1040 tc = backup;
1041 curr_context = last_context;
1043 else
1045 static char buf[MAX_PATH];
1046 if (dirfilter != SHOW_ID3DB)
1047 tc.dirfilter = &global_settings.dirfilter;
1048 strcpy(buf,root);
1049 set_current_file(buf);
1050 ret_val = dirbrowse();
1052 tc.dirfilter = last_filter;
1053 return ret_val;
1056 void tree_mem_init(void)
1058 /* We copy the settings value in case it is changed by the user. We can't
1059 use it until the next reboot. */
1060 max_files = global_settings.max_files_in_dir;
1062 /* initialize tree context struct */
1063 memset(&tc, 0, sizeof(tc));
1064 tc.dirfilter = &global_settings.dirfilter;
1066 tc.name_buffer_size = AVERAGE_FILENAME_LENGTH * max_files;
1067 tc.name_buffer = buffer_alloc(tc.name_buffer_size);
1069 tc.dircache_size = max_files * sizeof(struct entry);
1070 tc.dircache = buffer_alloc(tc.dircache_size);
1071 tree_get_filetypes(&filetypes, &filetypes_count);
1074 bool bookmark_play(char *resume_file, int index, int offset, int seed,
1075 char *filename)
1077 int i;
1078 char* suffix = strrchr(resume_file, '.');
1079 bool started = false;
1081 if (suffix != NULL &&
1082 (!strcasecmp(suffix, ".m3u") || !strcasecmp(suffix, ".m3u8")))
1084 /* Playlist playback */
1085 char* slash;
1086 /* check that the file exists */
1087 if(!file_exists(resume_file))
1088 return false;
1090 slash = strrchr(resume_file,'/');
1091 if (slash)
1093 char* cp;
1094 *slash=0;
1096 cp=resume_file;
1097 if (!cp[0])
1098 cp="/";
1100 if (playlist_create(cp, slash+1) != -1)
1102 if (global_settings.playlist_shuffle)
1103 playlist_shuffle(seed, -1);
1104 playlist_start(index,offset);
1105 started = true;
1107 *slash='/';
1110 else
1112 /* Directory playback */
1113 lastdir[0]='\0';
1114 if (playlist_create(resume_file, NULL) != -1)
1116 char* peek_filename;
1117 resume_directory(resume_file);
1118 if (global_settings.playlist_shuffle)
1119 playlist_shuffle(seed, -1);
1121 /* Check if the file is at the same spot in the directory,
1122 else search for it */
1123 peek_filename = playlist_peek(index);
1125 if (peek_filename == NULL)
1126 return false;
1128 if (strcmp(strrchr(peek_filename, '/') + 1, filename))
1130 for ( i=0; i < playlist_amount(); i++ )
1132 peek_filename = playlist_peek(i);
1134 if (peek_filename == NULL)
1135 return false;
1137 if (!strcmp(strrchr(peek_filename, '/') + 1, filename))
1138 break;
1140 if (i < playlist_amount())
1141 index = i;
1142 else
1143 return false;
1145 playlist_start(index,offset);
1146 started = true;
1150 if (started)
1151 start_wps = true;
1152 return started;
1155 static void say_filetype(int attr)
1157 /* try to find a voice ID for the extension, if known */
1158 int j;
1159 attr &= FILE_ATTR_MASK; /* file type */
1160 for (j=0; j<filetypes_count; j++)
1161 if (attr == filetypes[j].tree_attr)
1163 talk_id(filetypes[j].voiceclip, true);
1164 return;
1168 static int ft_play_dirname(char* name)
1170 char dirname_mp3_filename[MAX_PATH+1];
1172 #if CONFIG_CODEC != SWCODEC
1173 if (audio_status() & AUDIO_STATUS_PLAY)
1174 return 0;
1175 #endif
1177 snprintf(dirname_mp3_filename, sizeof(dirname_mp3_filename), "%s/%s/%s",
1178 tc.currdir[1] ? tc.currdir : "" , name,
1179 dir_thumbnail_name);
1181 DEBUGF("Checking for %s\n", dirname_mp3_filename);
1183 if (!file_exists(dirname_mp3_filename))
1185 DEBUGF("Failed to find: %s\n", dirname_mp3_filename);
1186 return -1;
1189 DEBUGF("Found: %s\n", dirname_mp3_filename);
1191 talk_file(dirname_mp3_filename, false);
1192 if(global_settings.talk_filetype)
1193 talk_id(VOICE_DIR, true);
1194 return 1;
1197 static void ft_play_filename(char *dir, char *file)
1199 char name_mp3_filename[MAX_PATH+1];
1201 #if CONFIG_CODEC != SWCODEC
1202 if (audio_status() & AUDIO_STATUS_PLAY)
1203 return;
1204 #endif
1206 if (strcasecmp(&file[strlen(file) - strlen(file_thumbnail_ext)],
1207 file_thumbnail_ext))
1208 { /* file has no .talk extension */
1209 snprintf(name_mp3_filename, sizeof(name_mp3_filename),
1210 "%s/%s%s", dir, file, file_thumbnail_ext);
1212 talk_file(name_mp3_filename, false);
1214 else
1215 { /* it already is a .talk file, play this directly */
1216 snprintf(name_mp3_filename, sizeof(name_mp3_filename),
1217 "%s/%s", dir, file);
1218 talk_id(LANG_VOICE_DIR_HOVER, false); /* prefix it */
1219 talk_file(name_mp3_filename, true);
1223 /* These two functions are called by the USB and shutdown handlers */
1224 void tree_flush(void)
1226 scrobbler_shutdown();
1227 #ifdef HAVE_TAGCACHE
1228 tagcache_shutdown();
1229 #endif
1230 playlist_shutdown();
1232 #ifdef HAVE_TC_RAMCACHE
1233 tagcache_unload_ramcache();
1234 #endif
1236 #ifdef HAVE_DIRCACHE
1238 int old_val = global_status.dircache_size;
1239 if (global_settings.dircache)
1241 if (!dircache_is_initializing())
1242 global_status.dircache_size = dircache_get_cache_size();
1243 # ifdef HAVE_EEPROM_SETTINGS
1244 if (firmware_settings.initialized)
1245 dircache_save();
1246 # endif
1247 dircache_disable();
1249 else
1251 global_status.dircache_size = 0;
1253 if (old_val != global_status.dircache_size)
1254 status_save();
1256 #endif
1259 void tree_restore(void)
1261 #ifdef HAVE_EEPROM_SETTINGS
1262 firmware_settings.disk_clean = false;
1263 #endif
1265 #ifdef HAVE_TC_RAMCACHE
1266 remove(TAGCACHE_STATEFILE);
1267 #endif
1269 #ifdef HAVE_DIRCACHE
1270 remove(DIRCACHE_FILE);
1271 if (global_settings.dircache)
1273 /* Print "Scanning disk..." to the display. */
1274 int i;
1275 FOR_NB_SCREENS(i)
1277 screens[i].putsxy((LCD_WIDTH/2) -
1278 ((strlen(str(LANG_SCANNING_DISK)) *
1279 screens[i].char_width)/2),
1280 LCD_HEIGHT-screens[i].char_height*3,
1281 str(LANG_SCANNING_DISK));
1282 gui_textarea_update(&screens[i]);
1284 cond_talk_ids_fq(LANG_SCANNING_DISK);
1286 dircache_build(global_status.dircache_size);
1288 /* Clean the text when we are done. */
1289 FOR_NB_SCREENS(i)
1291 gui_textarea_clear(&screens[i]);
1294 #endif
1295 #ifdef HAVE_TAGCACHE
1296 tagcache_start_scan();
1297 #endif
1298 scrobbler_init();