FS#10066 by Michael Chicoine: Sansa AMS bootloader "Hold" switch detection.
[kugel-rb.git] / apps / tree.c
blob66979ea3c1ca0af7e41d288074d185f3afbd95f6
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2002 Daniel Stenberg
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
21 #include <stdio.h>
22 #include <string.h>
23 #include <stdlib.h>
24 #include <stdbool.h>
26 #include "applimits.h"
27 #include "dir.h"
28 #include "file.h"
29 #include "lcd.h"
30 #include "font.h"
31 #include "backlight.h"
32 #include "button.h"
33 #include "kernel.h"
34 #include "usb.h"
35 #include "tree.h"
36 #include "sprintf.h"
37 #include "audio.h"
38 #include "playlist.h"
39 #include "menu.h"
40 #include "gwps.h"
41 #include "settings.h"
42 #include "debug.h"
43 #include "storage.h"
44 #include "rolo.h"
45 #include "icons.h"
46 #include "lang.h"
47 #include "screens.h"
48 #include "keyboard.h"
49 #include "bookmark.h"
50 #include "onplay.h"
51 #include "buffer.h"
52 #include "power.h"
53 #include "action.h"
54 #include "talk.h"
55 #include "filetypes.h"
56 #include "misc.h"
57 #include "filetree.h"
58 #include "tagtree.h"
59 #ifdef HAVE_RECORDING
60 #include "recorder/recording.h"
61 #endif
62 #include "rtc.h"
63 #include "dircache.h"
64 #ifdef HAVE_TAGCACHE
65 #include "tagcache.h"
66 #endif
67 #include "yesno.h"
68 #include "gwps-common.h"
69 #include "eeprom_settings.h"
70 #include "playlist_catalog.h"
72 /* gui api */
73 #include "list.h"
74 #include "statusbar.h"
75 #include "splash.h"
76 #include "buttonbar.h"
77 #include "action.h"
78 #include "quickscreen.h"
80 #include "root_menu.h"
82 static const struct filetype *filetypes;
83 static int filetypes_count;
85 struct gui_synclist tree_lists;
87 /* I put it here because other files doesn't use it yet,
88 * but should be elsewhere since it will be used mostly everywhere */
89 #ifdef HAVE_BUTTONBAR
90 struct gui_buttonbar tree_buttonbar;
91 #endif
92 static struct tree_context tc;
94 bool boot_changed = false;
96 char lastfile[MAX_PATH];
97 static char lastdir[MAX_PATH];
98 #ifdef HAVE_TAGCACHE
99 static int lasttable, lastextra, lastfirstpos;
100 #endif
101 static int max_files = 0;
103 static bool reload_dir = false;
105 static bool start_wps = false;
106 static int curr_context = false;/* id3db or tree*/
108 static int dirbrowse(void);
109 static int ft_play_dirname(char* name);
110 static void ft_play_filename(char *dir, char *file);
111 static void say_filetype(int attr);
113 static char * tree_get_filename(int selected_item, void *data,
114 char *buffer, size_t buffer_len)
116 struct tree_context * local_tc=(struct tree_context *)data;
117 char *name;
118 int attr=0;
119 bool stripit = false;
120 #ifdef HAVE_TAGCACHE
121 bool id3db = *(local_tc->dirfilter) == SHOW_ID3DB;
123 if (id3db)
125 return tagtree_get_entry(&tc, selected_item)->name;
127 else
128 #endif
130 struct entry* dc = local_tc->dircache;
131 struct entry* e = &dc[selected_item];
132 name = e->name;
133 attr = e->attr;
136 if(!(attr & ATTR_DIRECTORY))
138 switch(global_settings.show_filename_ext)
140 case 0:
141 /* show file extension: off */
142 stripit = true;
143 break;
144 case 1:
145 /* show file extension: on */
146 break;
147 case 2:
148 /* show file extension: only unknown types */
149 stripit = filetype_supported(attr);
150 break;
151 case 3:
152 default:
153 /* show file extension: only when viewing all */
154 stripit = (*(local_tc->dirfilter) != SHOW_ID3DB) &&
155 (*(local_tc->dirfilter) != SHOW_ALL);
156 break;
160 if(stripit)
162 return(strip_extension(buffer, buffer_len, name));
164 return(name);
167 #ifdef HAVE_LCD_COLOR
168 static int tree_get_filecolor(int selected_item, void * data)
170 if (*tc.dirfilter == SHOW_ID3DB)
171 return -1;
172 struct tree_context * local_tc=(struct tree_context *)data;
173 struct entry* dc = local_tc->dircache;
174 struct entry* e = &dc[selected_item];
175 return filetype_get_color(e->name, e->attr);
177 #endif
179 static int tree_get_fileicon(int selected_item, void * data)
181 struct tree_context * local_tc=(struct tree_context *)data;
182 #ifdef HAVE_TAGCACHE
183 bool id3db = *(local_tc->dirfilter) == SHOW_ID3DB;
184 if (id3db) {
185 return tagtree_get_icon(&tc);
187 else
188 #endif
190 struct entry* dc = local_tc->dircache;
191 struct entry* e = &dc[selected_item];
192 return filetype_get_icon(e->attr);
196 static int tree_voice_cb(int selected_item, void * data)
198 struct tree_context * local_tc=(struct tree_context *)data;
199 char *name;
200 int attr=0;
201 #ifdef HAVE_TAGCACHE
202 bool id3db = *(local_tc->dirfilter) == SHOW_ID3DB;
204 if (id3db)
206 attr = tagtree_get_attr(local_tc);
207 name = tagtree_get_entry(local_tc, selected_item)->name;
209 else
210 #endif
212 struct entry* dc = local_tc->dircache;
213 struct entry* e = &dc[selected_item];
214 name = e->name;
215 attr = e->attr;
217 bool is_dir = (attr & ATTR_DIRECTORY);
218 bool did_clip = false;
219 /* First the .talk clip case */
220 if(is_dir)
222 if(global_settings.talk_dir_clip)
224 did_clip = true;
225 if(ft_play_dirname(name) <0)
226 /* failed, not existing */
227 did_clip = false;
229 } else { /* it's a file */
230 if (global_settings.talk_file_clip && (attr & FILE_ATTR_THUMBNAIL))
232 did_clip = true;
233 ft_play_filename(local_tc->currdir, name);
236 if(!did_clip)
238 /* say the number or spell if required or as a fallback */
239 switch (is_dir ? global_settings.talk_dir : global_settings.talk_file)
241 case 1: /* as numbers */
242 talk_id(is_dir ? VOICE_DIR : VOICE_FILE, false);
243 talk_number(selected_item+1 - (is_dir ? 0 : local_tc->dirsindir),
244 true);
245 if(global_settings.talk_filetype
246 && !is_dir && *local_tc->dirfilter < NUM_FILTER_MODES)
247 say_filetype(attr);
248 break;
249 case 2: /* spelled */
250 talk_shutup();
251 if(global_settings.talk_filetype)
253 if(is_dir)
254 talk_id(VOICE_DIR, true);
255 else if(*local_tc->dirfilter < NUM_FILTER_MODES)
256 say_filetype(attr);
258 talk_spell(name, true);
259 break;
262 return 0;
265 bool check_rockboxdir(void)
267 if(!dir_exists(ROCKBOX_DIR))
268 { /* No need to localise this message.
269 If .rockbox is missing, it wouldn't work anyway */
270 int i;
271 FOR_NB_SCREENS(i)
272 screens[i].clear_display();
273 splash(HZ*2, "No .rockbox directory");
274 FOR_NB_SCREENS(i)
275 screens[i].clear_display();
276 splash(HZ*2, "Installation incomplete");
277 return false;
279 return true;
282 /* do this really late in the init sequence */
283 void tree_gui_init(void)
285 check_rockboxdir();
287 strcpy(tc.currdir, "/");
289 #ifdef HAVE_LCD_CHARCELLS
290 int i;
291 FOR_NB_SCREENS(i)
292 screens[i].double_height(false);
293 #endif
294 #ifdef HAVE_BUTTONBAR
295 gui_buttonbar_init(&tree_buttonbar);
296 /* since archos only have one screen, no need to create more than that */
297 gui_buttonbar_set_display(&tree_buttonbar, &(screens[SCREEN_MAIN]) );
298 #endif
299 gui_synclist_init(&tree_lists, &tree_get_filename, &tc, false, 1, NULL);
300 gui_synclist_set_voice_callback(&tree_lists, tree_voice_cb);
301 gui_synclist_set_icon_callback(&tree_lists, &tree_get_fileicon);
302 #ifdef HAVE_LCD_COLOR
303 gui_synclist_set_color_callback(&tree_lists, &tree_get_filecolor);
304 #endif
309 struct tree_context* tree_get_context(void)
311 return &tc;
315 * Returns the position of a given file in the current directory
316 * returns -1 if not found
318 static int tree_get_file_position(char * filename)
320 int i;
322 /* use lastfile to determine the selected item (default=0) */
323 for (i=0; i < tc.filesindir; i++)
325 struct entry* dc = tc.dircache;
326 struct entry* e = &dc[i];
327 if (!strcasecmp(e->name, filename))
328 return(i);
330 return(-1);/* no file can match, returns undefined */
334 * Called when a new dir is loaded (for example when returning from other apps ...)
335 * also completely redraws the tree
337 static int update_dir(void)
339 bool changed = false;
340 #ifdef HAVE_TAGCACHE
341 bool id3db = *tc.dirfilter == SHOW_ID3DB;
342 /* Checks for changes */
343 if (id3db) {
344 if (tc.currtable != lasttable ||
345 tc.currextra != lastextra ||
346 tc.firstpos != lastfirstpos ||
347 reload_dir)
349 if (tagtree_load(&tc) < 0)
350 return -1;
352 lasttable = tc.currtable;
353 lastextra = tc.currextra;
354 lastfirstpos = tc.firstpos;
355 changed = true;
358 else
359 #endif
361 /* if the tc.currdir has been changed, reload it ...*/
362 if (strncmp(tc.currdir, lastdir, sizeof(lastdir)) || reload_dir)
364 if (ft_load(&tc, NULL) < 0)
365 return -1;
366 strcpy(lastdir, tc.currdir);
367 changed = true;
370 /* if selected item is undefined */
371 if (tc.selected_item == -1)
373 /* use lastfile to determine the selected item */
374 tc.selected_item = tree_get_file_position(lastfile);
376 /* If the file doesn't exists, select the first one (default) */
377 if(tc.selected_item < 0)
378 tc.selected_item = 0;
379 changed = true;
381 if (changed)
384 #ifdef HAVE_TAGCACHE
385 !id3db &&
386 #endif
387 (tc.dirfull || tc.filesindir == global_settings.max_files_in_dir) )
389 splash(HZ, ID2P(LANG_SHOWDIR_BUFFER_FULL));
392 #ifdef HAVE_TAGCACHE
393 if (id3db)
395 #ifdef HAVE_LCD_BITMAP
396 if (global_settings.show_path_in_browser == SHOW_PATH_FULL
397 || global_settings.show_path_in_browser == SHOW_PATH_CURRENT)
399 gui_synclist_set_title(&tree_lists, tagtree_get_title(&tc),
400 filetype_get_icon(ATTR_DIRECTORY));
402 else
404 /* Must clear the title as the list is reused */
405 gui_synclist_set_title(&tree_lists, NULL, NOICON);
407 #endif
409 else
410 #endif
412 #ifdef HAVE_LCD_BITMAP
413 if (global_settings.show_path_in_browser &&
414 *(tc.dirfilter) == SHOW_PLUGINS)
416 char *title;
417 if (!strcmp(tc.currdir, PLUGIN_GAMES_DIR))
418 title = str(LANG_PLUGIN_GAMES);
419 else if (!strcmp(tc.currdir, PLUGIN_APPS_DIR))
420 title = str(LANG_PLUGIN_APPS);
421 else if (!strcmp(tc.currdir, PLUGIN_DEMOS_DIR))
422 title = str(LANG_PLUGIN_DEMOS);
423 else title = str(LANG_PLUGINS);
424 gui_synclist_set_title(&tree_lists, title, Icon_Plugin);
426 else if (global_settings.show_path_in_browser == SHOW_PATH_FULL)
428 gui_synclist_set_title(&tree_lists, tc.currdir,
429 filetype_get_icon(ATTR_DIRECTORY));
431 else if (global_settings.show_path_in_browser == SHOW_PATH_CURRENT)
433 char *title = strrchr(tc.currdir, '/') + 1;
434 if (*title == '\0')
436 /* Display "Files" for the root dir */
437 gui_synclist_set_title(&tree_lists, str(LANG_DIR_BROWSER),
438 filetype_get_icon(ATTR_DIRECTORY));
440 else
441 gui_synclist_set_title(&tree_lists, title,
442 filetype_get_icon(ATTR_DIRECTORY));
444 else
446 /* Must clear the title as the list is reused */
447 gui_synclist_set_title(&tree_lists, NULL, NOICON);
449 #endif
452 gui_synclist_set_nb_items(&tree_lists, tc.filesindir);
453 gui_synclist_set_icon_callback(&tree_lists, tree_get_fileicon);
454 if( tc.selected_item >= tc.filesindir)
455 tc.selected_item=tc.filesindir-1;
457 gui_synclist_select_item(&tree_lists, tc.selected_item);
458 #ifdef HAVE_BUTTONBAR
459 if (global_settings.buttonbar) {
460 if (*tc.dirfilter < NUM_FILTER_MODES)
461 gui_buttonbar_set(&tree_buttonbar, str(LANG_SYSFONT_DIRBROWSE_F1),
462 str(LANG_SYSFONT_DIRBROWSE_F2),
463 str(LANG_SYSFONT_DIRBROWSE_F3));
464 else
465 gui_buttonbar_set(&tree_buttonbar, "<<<", "", "");
466 gui_buttonbar_draw(&tree_buttonbar);
468 #endif
469 gui_synclist_draw(&tree_lists);
470 gui_synclist_speak_item(&tree_lists);
471 return tc.filesindir;
474 /* load tracks from specified directory to resume play */
475 void resume_directory(const char *dir)
477 int dirfilter = *tc.dirfilter;
478 int ret;
479 #ifdef HAVE_TAGCACHE
480 bool id3db = *tc.dirfilter == SHOW_ID3DB;
481 #endif
482 /* make sure the dirfilter is sane. The only time it should be possible
483 * thats its not is when resume playlist is called from a plugin
485 #ifdef HAVE_TAGCACHE
486 if (!id3db)
487 #endif
488 *tc.dirfilter = global_settings.dirfilter;
489 ret = ft_load(&tc, dir);
490 *tc.dirfilter = dirfilter;
491 if (ret < 0)
492 return;
493 lastdir[0] = 0;
495 ft_build_playlist(&tc, 0);
497 #ifdef HAVE_TAGCACHE
498 if (id3db)
499 tagtree_load(&tc);
500 #endif
503 /* Returns the current working directory and also writes cwd to buf if
504 non-NULL. In case of error, returns NULL. */
505 char *getcwd(char *buf, int size)
507 if (!buf)
508 return tc.currdir;
509 else if (size > 0)
511 strncpy(buf, tc.currdir, size);
512 return buf;
514 else
515 return NULL;
518 /* Force a reload of the directory next time directory browser is called */
519 void reload_directory(void)
521 reload_dir = true;
524 void get_current_file(char* buffer, int buffer_len)
526 #ifdef HAVE_TAGCACHE
527 /* in ID3DB mode it is a bad idea to call this function */
528 /* (only happens with `follow playlist') */
529 if( *tc.dirfilter == SHOW_ID3DB )
530 return;
531 #endif
533 struct entry* dc = tc.dircache;
534 struct entry* e = &dc[tc.selected_item];
535 snprintf(buffer, buffer_len, "%s/%s", getcwd(NULL,0),
536 tc.dirlength ? e->name : "");
539 /* Allow apps to change our dirfilter directly (required for sub browsers)
540 if they're suddenly going to become a file browser for example */
541 void set_dirfilter(int l_dirfilter)
543 *tc.dirfilter = l_dirfilter;
546 /* Selects a file and update tree context properly */
547 void set_current_file(char *path)
549 char *name;
550 int i;
552 #ifdef HAVE_TAGCACHE
553 /* in ID3DB mode it is a bad idea to call this function */
554 /* (only happens with `follow playlist') */
555 if( *tc.dirfilter == SHOW_ID3DB )
556 return;
557 #endif
559 /* separate directory from filename */
560 /* gets the directory's name and put it into tc.currdir */
561 name = strrchr(path+1,'/');
562 if (name)
564 *name = 0;
565 strcpy(tc.currdir, path);
566 *name = '/';
567 name++;
569 else
571 strcpy(tc.currdir, "/");
572 name = path+1;
575 strcpy(lastfile, name);
578 /* If we changed dir we must recalculate the dirlevel
579 and adjust the selected history properly */
580 if (strncmp(tc.currdir,lastdir,sizeof(lastdir)))
582 tc.dirlevel = 0;
583 tc.selected_item_history[tc.dirlevel] = -1;
585 /* use '/' to calculate dirlevel */
586 for (i = 1; path[i] != '\0'; i++)
588 if (path[i] == '/')
590 tc.dirlevel++;
591 tc.selected_item_history[tc.dirlevel] = -1;
595 if (ft_load(&tc, NULL) >= 0)
597 tc.selected_item = tree_get_file_position(lastfile);
602 /* main loop, handles key events */
603 static int dirbrowse()
605 int numentries=0;
606 char buf[MAX_PATH];
607 int button, oldbutton;
608 bool reload_root = false;
609 int lastfilter = *tc.dirfilter;
610 bool lastsortcase = global_settings.sort_case;
611 bool exit_func = false;
613 char* currdir = tc.currdir; /* just a shortcut */
614 #ifdef HAVE_TAGCACHE
615 bool id3db = *tc.dirfilter == SHOW_ID3DB;
617 if (id3db)
618 curr_context=CONTEXT_ID3DB;
619 else
620 #endif
621 curr_context=CONTEXT_TREE;
622 if (tc.selected_item < 0)
623 tc.selected_item = 0;
624 #ifdef HAVE_TAGCACHE
625 tc.firstpos = 0;
626 lasttable = -1;
627 lastextra = -1;
628 lastfirstpos = 0;
629 #endif
631 start_wps = false;
632 numentries = update_dir();
633 reload_dir = false;
634 if (numentries == -1)
635 return GO_TO_PREVIOUS; /* currdir is not a directory */
637 if (*tc.dirfilter > NUM_FILTER_MODES && numentries==0)
639 splash(HZ*2, ID2P(LANG_NO_FILES));
640 return GO_TO_PREVIOUS; /* No files found for rockbox_browser() */
643 gui_synclist_draw(&tree_lists);
644 while(1) {
645 struct entry *dircache = tc.dircache;
646 bool restore = false;
647 if (tc.dirlevel < 0)
648 tc.dirlevel = 0; /* shouldnt be needed.. this code needs work! */
649 #ifdef BOOTFILE
650 if (boot_changed) {
651 static const char *lines[]={ID2P(LANG_BOOT_CHANGED), ID2P(LANG_REBOOT_NOW)};
652 static const struct text_message message={lines, 2};
653 if(gui_syncyesno_run(&message, NULL, NULL)==YESNO_YES)
654 rolo_load("/" BOOTFILE);
655 restore = true;
656 boot_changed = false;
658 #endif
659 button = get_action(CONTEXT_TREE,
660 list_do_action_timeout(&tree_lists, HZ/2));
661 oldbutton = button;
662 gui_synclist_do_button(&tree_lists, &button,LIST_WRAP_UNLESS_HELD);
663 tc.selected_item = gui_synclist_get_sel_pos(&tree_lists);
664 switch ( button ) {
665 case ACTION_STD_OK:
666 /* nothing to do if no files to display */
667 if ( numentries == 0 )
668 break;
670 #ifdef HAVE_TAGCACHE
671 switch (id3db?tagtree_enter(&tc):ft_enter(&tc))
672 #else
673 switch (ft_enter(&tc))
674 #endif
676 case 1: reload_dir = true; break;
677 case 2: start_wps = true; break;
678 case 3: exit_func = true; break;
679 default: break;
681 restore = true;
682 break;
684 case ACTION_STD_CANCEL:
685 if (*tc.dirfilter > NUM_FILTER_MODES && tc.dirlevel < 1) {
686 exit_func = true;
687 break;
689 if ((*tc.dirfilter == SHOW_ID3DB && tc.dirlevel == 0) ||
690 ((*tc.dirfilter != SHOW_ID3DB && !strcmp(currdir,"/"))))
692 #ifdef HAVE_LCD_BITMAP /* charcell doesnt have ACTION_TREE_PGLEFT so this isnt needed */
693 if (oldbutton == ACTION_TREE_PGLEFT)
694 break;
695 else
696 #endif
697 return GO_TO_ROOT;
700 #ifdef HAVE_TAGCACHE
701 if (id3db)
702 tagtree_exit(&tc);
703 else
704 #endif
705 if (ft_exit(&tc) == 3)
706 exit_func = true;
708 restore = true;
709 break;
711 case ACTION_TREE_STOP:
712 if (list_stop_handler())
713 restore = true;
714 break;
716 case ACTION_STD_MENU:
717 return GO_TO_ROOT;
718 break;
720 #ifdef HAVE_RECORDING
721 case ACTION_STD_REC:
722 return GO_TO_RECSCREEN;
723 #endif
725 case ACTION_TREE_WPS:
726 return GO_TO_PREVIOUS_MUSIC;
727 break;
728 #ifdef HAVE_QUICKSCREEN
729 case ACTION_STD_QUICKSCREEN:
730 /* don't enter f2 from plugin browser */
731 if (*tc.dirfilter < NUM_FILTER_MODES)
733 if (quick_screen_quick(button))
734 reload_dir = true;
735 restore = true;
737 break;
738 #endif
739 #ifdef BUTTON_F3
740 case ACTION_F3:
741 /* don't enter f3 from plugin browser */
742 if (*tc.dirfilter < NUM_FILTER_MODES)
744 if (quick_screen_f3(ACTION_F3))
745 reload_dir = true;
746 restore = true;
748 break;
749 #endif
751 case ACTION_STD_CONTEXT:
753 int onplay_result;
754 int attr = 0;
756 if(!numentries)
757 onplay_result = onplay(NULL, 0, curr_context);
758 else {
759 #ifdef HAVE_TAGCACHE
760 if (id3db)
762 if (tagtree_get_attr(&tc) == FILE_ATTR_AUDIO)
764 attr = FILE_ATTR_AUDIO;
765 tagtree_get_filename(&tc, buf, sizeof(buf));
767 else
768 attr = ATTR_DIRECTORY;
770 else
771 #endif
773 attr = dircache[tc.selected_item].attr;
775 if (currdir[1]) /* Not in / */
776 snprintf(buf, sizeof buf, "%s/%s",
777 currdir,
778 dircache[tc.selected_item].name);
779 else /* In / */
780 snprintf(buf, sizeof buf, "/%s",
781 dircache[tc.selected_item].name);
783 onplay_result = onplay(buf, attr, curr_context);
785 switch (onplay_result)
787 case ONPLAY_MAINMENU:
788 return GO_TO_ROOT;
790 case ONPLAY_OK:
791 restore = true;
792 break;
794 case ONPLAY_RELOAD_DIR:
795 reload_dir = true;
796 break;
798 case ONPLAY_START_PLAY:
799 return GO_TO_WPS;
800 break;
802 break;
805 #ifdef HAVE_HOTSWAP
806 case SYS_FS_CHANGED:
807 #ifdef HAVE_TAGCACHE
808 if (!id3db)
809 #endif
810 reload_dir = true;
811 /* The 'dir no longer valid' situation will be caught later
812 * by checking the showdir() result. */
813 break;
814 #endif
816 default:
817 if (default_event_handler(button) == SYS_USB_CONNECTED)
819 if(*tc.dirfilter > NUM_FILTER_MODES)
820 /* leave sub-browsers after usb, doing otherwise
821 might be confusing to the user */
822 exit_func = true;
823 else
824 reload_dir = true;
826 break;
828 if (start_wps)
829 return GO_TO_WPS;
830 if (button && !IS_SYSEVENT(button))
832 storage_spin();
836 check_rescan:
837 /* do we need to rescan dir? */
838 if (reload_dir || reload_root ||
839 lastfilter != *tc.dirfilter ||
840 lastsortcase != global_settings.sort_case)
842 if (reload_root) {
843 strcpy(currdir, "/");
844 tc.dirlevel = 0;
845 #ifdef HAVE_TAGCACHE
846 tc.currtable = 0;
847 tc.currextra = 0;
848 lasttable = -1;
849 lastextra = -1;
850 #endif
851 reload_root = false;
854 if (!reload_dir)
856 gui_synclist_select_item(&tree_lists, 0);
857 gui_synclist_draw(&tree_lists);
858 tc.selected_item = 0;
859 lastdir[0] = 0;
862 lastfilter = *tc.dirfilter;
863 lastsortcase = global_settings.sort_case;
864 restore = true;
867 if (exit_func)
868 return GO_TO_PREVIOUS;
870 if (restore || reload_dir) {
871 /* restore display */
872 numentries = update_dir();
873 reload_dir = false;
874 if (currdir[1] && (numentries < 0))
875 { /* not in root and reload failed */
876 reload_root = true; /* try root */
877 goto check_rescan;
881 return true;
884 bool create_playlist(void)
886 char filename[MAX_PATH];
888 snprintf(filename, sizeof filename, "%s.m3u8",
889 tc.currdir[1] ? tc.currdir : "/root");
890 splashf(0, "%s %s", str(LANG_CREATING), filename);
892 trigger_cpu_boost();
893 catalog_add_to_a_playlist(tc.currdir, ATTR_DIRECTORY, true, filename);
894 cancel_cpu_boost();
896 return true;
899 int rockbox_browse(const char *root, int dirfilter)
901 int ret_val = 0;
902 int *last_filter = tc.dirfilter;
903 tc.dirfilter = &dirfilter;
904 tc.sort_dir = global_settings.sort_dir;
906 reload_dir = true;
907 if (dirfilter >= NUM_FILTER_MODES)
909 static struct tree_context backup;
910 int last_context;
912 backup = tc;
913 tc.selected_item = 0;
914 tc.dirlevel = 0;
915 memcpy(tc.currdir, root, sizeof(tc.currdir));
916 start_wps = false;
917 last_context = curr_context;
919 ret_val = dirbrowse();
920 tc = backup;
921 curr_context = last_context;
923 else
925 static char buf[MAX_PATH];
926 if (dirfilter != SHOW_ID3DB)
927 tc.dirfilter = &global_settings.dirfilter;
928 strcpy(buf,root);
929 set_current_file(buf);
930 ret_val = dirbrowse();
932 tc.dirfilter = last_filter;
933 return ret_val;
936 void tree_mem_init(void)
938 /* We copy the settings value in case it is changed by the user. We can't
939 use it until the next reboot. */
940 max_files = global_settings.max_files_in_dir;
942 /* initialize tree context struct */
943 memset(&tc, 0, sizeof(tc));
944 tc.dirfilter = &global_settings.dirfilter;
945 tc.sort_dir = global_settings.sort_dir;
947 tc.name_buffer_size = AVERAGE_FILENAME_LENGTH * max_files;
948 tc.name_buffer = buffer_alloc(tc.name_buffer_size);
950 tc.dircache_size = max_files * sizeof(struct entry);
951 tc.dircache = buffer_alloc(tc.dircache_size);
952 tree_get_filetypes(&filetypes, &filetypes_count);
955 bool bookmark_play(char *resume_file, int index, int offset, int seed,
956 char *filename)
958 int i;
959 char* suffix = strrchr(resume_file, '.');
960 bool started = false;
962 if (suffix != NULL &&
963 (!strcasecmp(suffix, ".m3u") || !strcasecmp(suffix, ".m3u8")))
965 /* Playlist playback */
966 char* slash;
967 /* check that the file exists */
968 if(!file_exists(resume_file))
969 return false;
971 slash = strrchr(resume_file,'/');
972 if (slash)
974 char* cp;
975 *slash=0;
977 cp=resume_file;
978 if (!cp[0])
979 cp="/";
981 if (playlist_create(cp, slash+1) != -1)
983 if (global_settings.playlist_shuffle)
984 playlist_shuffle(seed, -1);
985 playlist_start(index,offset);
986 started = true;
988 *slash='/';
991 else
993 /* Directory playback */
994 lastdir[0]='\0';
995 if (playlist_create(resume_file, NULL) != -1)
997 char* peek_filename;
998 resume_directory(resume_file);
999 if (global_settings.playlist_shuffle)
1000 playlist_shuffle(seed, -1);
1002 /* Check if the file is at the same spot in the directory,
1003 else search for it */
1004 peek_filename = playlist_peek(index);
1006 if (peek_filename == NULL)
1007 return false;
1009 if (strcmp(strrchr(peek_filename, '/') + 1, filename))
1011 for ( i=0; i < playlist_amount(); i++ )
1013 peek_filename = playlist_peek(i);
1015 if (peek_filename == NULL)
1016 return false;
1018 if (!strcmp(strrchr(peek_filename, '/') + 1, filename))
1019 break;
1021 if (i < playlist_amount())
1022 index = i;
1023 else
1024 return false;
1026 playlist_start(index,offset);
1027 started = true;
1031 if (started)
1032 start_wps = true;
1033 return started;
1036 static void say_filetype(int attr)
1038 /* try to find a voice ID for the extension, if known */
1039 int j;
1040 attr &= FILE_ATTR_MASK; /* file type */
1041 for (j=0; j<filetypes_count; j++)
1042 if (attr == filetypes[j].tree_attr)
1044 talk_id(filetypes[j].voiceclip, true);
1045 return;
1049 static int ft_play_dirname(char* name)
1051 #if CONFIG_CODEC != SWCODEC
1052 if (audio_status() & AUDIO_STATUS_PLAY)
1053 return 0;
1054 #endif
1056 if(talk_file(tc.currdir, name, dir_thumbnail_name, NULL,
1057 NULL, false))
1059 if(global_settings.talk_filetype)
1060 talk_id(VOICE_DIR, true);
1061 return 1;
1063 else
1064 return -1;
1067 static void ft_play_filename(char *dir, char *file)
1069 #if CONFIG_CODEC != SWCODEC
1070 if (audio_status() & AUDIO_STATUS_PLAY)
1071 return;
1072 #endif
1074 if (strlen(file) >= strlen(file_thumbnail_ext)
1075 && strcasecmp(&file[strlen(file) - strlen(file_thumbnail_ext)],
1076 file_thumbnail_ext))
1077 /* file has no .talk extension */
1078 talk_file(dir, NULL, file, file_thumbnail_ext,
1079 NULL, false);
1080 else
1081 /* it already is a .talk file, play this directly, but prefix it. */
1082 talk_file(dir, NULL, file, NULL,
1083 TALK_IDARRAY(LANG_VOICE_DIR_HOVER), false);
1086 /* These two functions are called by the USB and shutdown handlers */
1087 void tree_flush(void)
1089 #ifdef HAVE_TAGCACHE
1090 tagcache_shutdown();
1091 #endif
1093 #ifdef HAVE_TC_RAMCACHE
1094 tagcache_unload_ramcache();
1095 #endif
1097 #ifdef HAVE_DIRCACHE
1099 int old_val = global_status.dircache_size;
1100 if (global_settings.dircache)
1102 if (!dircache_is_initializing())
1103 global_status.dircache_size = dircache_get_cache_size();
1104 # ifdef HAVE_EEPROM_SETTINGS
1105 if (firmware_settings.initialized)
1106 dircache_save();
1107 # endif
1108 dircache_disable();
1110 else
1112 global_status.dircache_size = 0;
1114 if (old_val != global_status.dircache_size)
1115 status_save();
1117 #endif
1120 void tree_restore(void)
1122 #ifdef HAVE_EEPROM_SETTINGS
1123 firmware_settings.disk_clean = false;
1124 #endif
1126 #ifdef HAVE_TC_RAMCACHE
1127 remove(TAGCACHE_STATEFILE);
1128 #endif
1130 #ifdef HAVE_DIRCACHE
1131 remove(DIRCACHE_FILE);
1132 if (global_settings.dircache)
1134 /* Print "Scanning disk..." to the display. */
1135 splash(0, str(LANG_SCANNING_DISK));
1137 dircache_build(global_status.dircache_size);
1139 #endif
1140 #ifdef HAVE_TAGCACHE
1141 tagcache_start_scan();
1142 #endif