clean up some debugging output.
[kugel-rb.git] / apps / tree.c
blob54f3b8997cc54960317cf2c68cedab1c52d78b42
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 "status.h"
43 #include "debug.h"
44 #include "ata.h"
45 #include "rolo.h"
46 #include "icons.h"
47 #include "lang.h"
48 #include "language.h"
49 #include "screens.h"
50 #include "keyboard.h"
51 #include "bookmark.h"
52 #include "onplay.h"
53 #include "buffer.h"
54 #include "power.h"
55 #include "action.h"
56 #include "talk.h"
57 #include "filetypes.h"
58 #include "misc.h"
59 #include "filetree.h"
60 #include "tagtree.h"
61 #ifdef HAVE_RECORDING
62 #include "recorder/recording.h"
63 #endif
64 #include "rtc.h"
65 #include "dircache.h"
66 #ifdef HAVE_TAGCACHE
67 #include "tagcache.h"
68 #endif
69 #include "yesno.h"
70 #include "gwps-common.h"
71 #include "eeprom_settings.h"
72 #include "scrobbler.h"
73 #include "playlist_catalog.h"
75 /* gui api */
76 #include "list.h"
77 #include "statusbar.h"
78 #include "splash.h"
79 #include "buttonbar.h"
80 #include "action.h"
81 #include "quickscreen.h"
83 #include "root_menu.h"
84 #include "backdrop.h"
86 static const struct filetype *filetypes;
87 static int filetypes_count;
89 struct gui_synclist tree_lists;
91 /* I put it here because other files doesn't use it yet,
92 * but should be elsewhere since it will be used mostly everywhere */
93 #ifdef HAVE_BUTTONBAR
94 struct gui_buttonbar tree_buttonbar;
95 #endif
96 static struct tree_context tc;
98 bool boot_changed = false;
100 char lastfile[MAX_PATH];
101 static char lastdir[MAX_PATH];
102 #ifdef HAVE_TAGCACHE
103 static int lasttable, lastextra, lastfirstpos;
104 #endif
105 static int max_files = 0;
107 static bool reload_dir = false;
109 static bool start_wps = false;
110 static int curr_context = false;/* id3db or tree*/
112 static int dirbrowse(void);
113 static int ft_play_dirname(char* name);
114 static void ft_play_filename(char *dir, char *file);
115 static void say_filetype(int attr);
117 static char * tree_get_filename(int selected_item, void *data,
118 char *buffer, size_t buffer_len)
120 struct tree_context * local_tc=(struct tree_context *)data;
121 char *name;
122 int attr=0;
123 bool stripit = false;
124 #ifdef HAVE_TAGCACHE
125 bool id3db = *(local_tc->dirfilter) == SHOW_ID3DB;
127 if (id3db)
129 return tagtree_get_entry(&tc, selected_item)->name;
131 else
132 #endif
134 struct entry* dc = local_tc->dircache;
135 struct entry* e = &dc[selected_item];
136 name = e->name;
137 attr = e->attr;
140 if(!(attr & ATTR_DIRECTORY))
142 switch(global_settings.show_filename_ext)
144 case 0:
145 /* show file extension: off */
146 stripit = true;
147 break;
148 case 1:
149 /* show file extension: on */
150 break;
151 case 2:
152 /* show file extension: only unknown types */
153 stripit = filetype_supported(attr);
154 break;
155 case 3:
156 default:
157 /* show file extension: only when viewing all */
158 stripit = (*(local_tc->dirfilter) != SHOW_ID3DB) &&
159 (*(local_tc->dirfilter) != SHOW_ALL);
160 break;
164 if(stripit)
166 return(strip_extension(buffer, buffer_len, name));
168 return(name);
171 #ifdef HAVE_LCD_COLOR
172 static int tree_get_filecolor(int selected_item, void * data)
174 if (*tc.dirfilter == SHOW_ID3DB)
175 return -1;
176 struct tree_context * local_tc=(struct tree_context *)data;
177 struct entry* dc = local_tc->dircache;
178 struct entry* e = &dc[selected_item];
179 return filetype_get_color(e->name, e->attr);
181 #endif
183 static int tree_get_fileicon(int selected_item, void * data)
185 struct tree_context * local_tc=(struct tree_context *)data;
186 #ifdef HAVE_TAGCACHE
187 bool id3db = *(local_tc->dirfilter) == SHOW_ID3DB;
188 if (id3db) {
189 return tagtree_get_icon(&tc);
191 else
192 #endif
194 struct entry* dc = local_tc->dircache;
195 struct entry* e = &dc[selected_item];
196 return filetype_get_icon(e->attr);
200 static int tree_voice_cb(int selected_item, void * data)
202 struct tree_context * local_tc=(struct tree_context *)data;
203 char *name;
204 int attr=0;
205 #ifdef HAVE_TAGCACHE
206 bool id3db = *(local_tc->dirfilter) == SHOW_ID3DB;
208 if (id3db)
210 attr = tagtree_get_attr(local_tc);
211 name = tagtree_get_entry(local_tc, selected_item)->name;
213 else
214 #endif
216 struct entry* dc = local_tc->dircache;
217 struct entry* e = &dc[selected_item];
218 name = e->name;
219 attr = e->attr;
221 bool is_dir = (attr & ATTR_DIRECTORY);
222 bool did_clip = false;
223 /* First the .talk clip case */
224 if(is_dir)
226 if(global_settings.talk_dir_clip)
228 DEBUGF("Playing directory thumbnail: %s", local_tc->currdir);
229 did_clip = true;
230 if(ft_play_dirname(name) <0)
231 /* failed, not existing */
232 did_clip = false;
234 } else { /* it's a file */
235 if (global_settings.talk_file_clip && (attr & FILE_ATTR_THUMBNAIL))
237 did_clip = true;
238 DEBUGF("Playing file thumbnail: %s/%s%s\n",
239 local_tc->currdir, name, file_thumbnail_ext);
240 ft_play_filename(local_tc->currdir, name);
243 if(!did_clip)
245 /* say the number or spell if required or as a fallback */
246 switch (is_dir ? global_settings.talk_dir : global_settings.talk_file)
248 case 1: /* as numbers */
249 talk_id(is_dir ? VOICE_DIR : VOICE_FILE, false);
250 talk_number(selected_item+1 - (is_dir ? 0 : local_tc->dirsindir),
251 true);
252 if(global_settings.talk_filetype
253 && !is_dir && *local_tc->dirfilter < NUM_FILTER_MODES)
254 say_filetype(attr);
255 break;
256 case 2: /* spelled */
257 talk_shutup();
258 if(global_settings.talk_filetype)
260 if(is_dir)
261 talk_id(VOICE_DIR, true);
262 else if(*local_tc->dirfilter < NUM_FILTER_MODES)
263 say_filetype(attr);
265 talk_spell(name, true);
266 break;
269 return 0;
272 bool check_rockboxdir(void)
274 if(!dir_exists(ROCKBOX_DIR))
275 { /* No need to localise this message.
276 If .rockbox is missing, it wouldn't work anyway */
277 int i;
278 FOR_NB_SCREENS(i)
279 screens[i].clear_display();
280 gui_syncsplash(HZ*2, "No .rockbox directory");
281 FOR_NB_SCREENS(i)
282 screens[i].clear_display();
283 gui_syncsplash(HZ*2, "Installation incomplete");
284 return false;
286 return true;
289 /* do this really late in the init sequence */
290 void tree_gui_init(void)
292 gui_sync_wps_screen_init();
294 check_rockboxdir();
296 strcpy(tc.currdir, "/");
298 #ifdef HAVE_LCD_CHARCELLS
299 int i;
300 FOR_NB_SCREENS(i)
301 screens[i].double_height(false);
302 #endif
303 #ifdef HAVE_BUTTONBAR
304 gui_buttonbar_init(&tree_buttonbar);
305 /* since archos only have one screen, no need to create more than that */
306 gui_buttonbar_set_display(&tree_buttonbar, &(screens[SCREEN_MAIN]) );
307 #endif
308 gui_synclist_init(&tree_lists, &tree_get_filename, &tc, false, 1, NULL);
309 gui_synclist_set_voice_callback(&tree_lists, tree_voice_cb);
310 gui_synclist_set_icon_callback(&tree_lists, &tree_get_fileicon);
311 #ifdef HAVE_LCD_COLOR
312 gui_synclist_set_color_callback(&tree_lists, &tree_get_filecolor);
313 #endif
318 struct tree_context* tree_get_context(void)
320 return &tc;
324 * Returns the position of a given file in the current directory
325 * returns -1 if not found
327 static int tree_get_file_position(char * filename)
329 int i;
331 /* use lastfile to determine the selected item (default=0) */
332 for (i=0; i < tc.filesindir; i++)
334 struct entry* dc = tc.dircache;
335 struct entry* e = &dc[i];
336 if (!strcasecmp(e->name, filename))
337 return(i);
339 return(-1);/* no file can match, returns undefined */
343 * Called when a new dir is loaded (for example when returning from other apps ...)
344 * also completely redraws the tree
346 static int update_dir(void)
348 bool changed = false;
349 #ifdef HAVE_TAGCACHE
350 bool id3db = *tc.dirfilter == SHOW_ID3DB;
351 /* Checks for changes */
352 if (id3db) {
353 if (tc.currtable != lasttable ||
354 tc.currextra != lastextra ||
355 tc.firstpos != lastfirstpos ||
356 reload_dir)
358 if (tagtree_load(&tc) < 0)
359 return -1;
361 lasttable = tc.currtable;
362 lastextra = tc.currextra;
363 lastfirstpos = tc.firstpos;
364 changed = true;
367 else
368 #endif
370 /* if the tc.currdir has been changed, reload it ...*/
371 if (strncmp(tc.currdir, lastdir, sizeof(lastdir)) || reload_dir)
373 if (ft_load(&tc, NULL) < 0)
374 return -1;
375 strcpy(lastdir, tc.currdir);
376 changed = true;
379 /* if selected item is undefined */
380 if (tc.selected_item == -1)
382 /* use lastfile to determine the selected item */
383 tc.selected_item = tree_get_file_position(lastfile);
385 /* If the file doesn't exists, select the first one (default) */
386 if(tc.selected_item < 0)
387 tc.selected_item = 0;
388 changed = true;
390 if (changed)
393 #ifdef HAVE_TAGCACHE
394 !id3db &&
395 #endif
396 (tc.dirfull || tc.filesindir == global_settings.max_files_in_dir) )
398 gui_syncsplash(HZ, ID2P(LANG_SHOWDIR_BUFFER_FULL));
401 #ifdef HAVE_TAGCACHE
402 if (id3db)
404 #ifdef HAVE_LCD_BITMAP
405 if (global_settings.show_path_in_browser == SHOW_PATH_FULL
406 || global_settings.show_path_in_browser == SHOW_PATH_CURRENT)
408 gui_synclist_set_title(&tree_lists, tagtree_get_title(&tc),
409 filetype_get_icon(ATTR_DIRECTORY));
411 else
413 /* Must clear the title as the list is reused */
414 gui_synclist_set_title(&tree_lists, NULL, NOICON);
416 #endif
418 else
419 #endif
421 #ifdef HAVE_LCD_BITMAP
422 if (global_settings.show_path_in_browser &&
423 *(tc.dirfilter) == SHOW_PLUGINS)
425 char *title;
426 if (!strcmp(tc.currdir, PLUGIN_GAMES_DIR))
427 title = str(LANG_PLUGIN_GAMES);
428 else if (!strcmp(tc.currdir, PLUGIN_APPS_DIR))
429 title = str(LANG_PLUGIN_APPS);
430 else if (!strcmp(tc.currdir, PLUGIN_DEMOS_DIR))
431 title = str(LANG_PLUGIN_DEMOS);
432 else title = str(LANG_PLUGINS);
433 gui_synclist_set_title(&tree_lists, title, Icon_Plugin);
435 else if (global_settings.show_path_in_browser == SHOW_PATH_FULL)
437 gui_synclist_set_title(&tree_lists, tc.currdir,
438 filetype_get_icon(ATTR_DIRECTORY));
440 else if (global_settings.show_path_in_browser == SHOW_PATH_CURRENT)
442 char *title = strrchr(tc.currdir, '/') + 1;
443 if (*title == '\0')
445 /* Display "Files" for the root dir */
446 gui_synclist_set_title(&tree_lists, str(LANG_DIR_BROWSER),
447 filetype_get_icon(ATTR_DIRECTORY));
449 else
450 gui_synclist_set_title(&tree_lists, title,
451 filetype_get_icon(ATTR_DIRECTORY));
453 else
455 /* Must clear the title as the list is reused */
456 gui_synclist_set_title(&tree_lists, NULL, NOICON);
458 #endif
461 gui_synclist_set_nb_items(&tree_lists, tc.filesindir);
462 gui_synclist_set_icon_callback(&tree_lists, tree_get_fileicon);
463 if( tc.selected_item >= tc.filesindir)
464 tc.selected_item=tc.filesindir-1;
466 gui_synclist_select_item(&tree_lists, tc.selected_item);
467 #ifdef HAVE_BUTTONBAR
468 if (global_settings.buttonbar) {
469 if (*tc.dirfilter < NUM_FILTER_MODES)
470 gui_buttonbar_set(&tree_buttonbar, str(LANG_SYSFONT_DIRBROWSE_F1),
471 str(LANG_SYSFONT_DIRBROWSE_F2),
472 str(LANG_SYSFONT_DIRBROWSE_F3));
473 else
474 gui_buttonbar_set(&tree_buttonbar, "<<<", "", "");
475 gui_buttonbar_draw(&tree_buttonbar);
477 #endif
478 gui_synclist_draw(&tree_lists);
479 gui_synclist_speak_item(&tree_lists);
480 gui_syncstatusbar_draw(&statusbars, true);
481 return tc.filesindir;
484 /* load tracks from specified directory to resume play */
485 void resume_directory(const char *dir)
487 #ifdef HAVE_TAGCACHE
488 bool id3db = *tc.dirfilter == SHOW_ID3DB;
489 #endif
491 if (ft_load(&tc, dir) < 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 unsigned 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 gui_syncsplash(HZ*2, ID2P(LANG_NO_FILES));
640 return GO_TO_PREVIOUS; /* No files found for rockbox_browser() */
643 while(1) {
644 struct entry *dircache = tc.dircache;
645 bool restore = false;
646 if (tc.dirlevel < 0)
647 tc.dirlevel = 0; /* shouldnt be needed.. this code needs work! */
648 #ifdef BOOTFILE
649 if (boot_changed) {
650 static const char *lines[]={ID2P(LANG_BOOT_CHANGED), ID2P(LANG_REBOOT_NOW)};
651 static const struct text_message message={lines, 2};
652 if(gui_syncyesno_run(&message, NULL, NULL)==YESNO_YES)
653 rolo_load("/" BOOTFILE);
654 restore = true;
655 boot_changed = false;
657 #endif
658 button = get_action(CONTEXT_TREE,
659 list_do_action_timeout(&tree_lists, HZ/2));
660 oldbutton = button;
661 gui_synclist_do_button(&tree_lists, &button,LIST_WRAP_UNLESS_HELD);
662 tc.selected_item = gui_synclist_get_sel_pos(&tree_lists);
663 switch ( button ) {
664 case ACTION_STD_OK:
665 /* nothing to do if no files to display */
666 if ( numentries == 0 )
667 break;
669 #ifdef HAVE_TAGCACHE
670 switch (id3db?tagtree_enter(&tc):ft_enter(&tc))
671 #else
672 switch (ft_enter(&tc))
673 #endif
675 case 1: reload_dir = true; break;
676 case 2: start_wps = true; break;
677 case 3: exit_func = true; break;
678 default: break;
680 restore = true;
681 break;
683 case ACTION_STD_CANCEL:
684 if (*tc.dirfilter > NUM_FILTER_MODES && tc.dirlevel < 1) {
685 exit_func = true;
686 break;
688 if ((*tc.dirfilter == SHOW_ID3DB && tc.dirlevel == 0) ||
689 ((*tc.dirfilter != SHOW_ID3DB && !strcmp(currdir,"/"))))
691 #ifdef HAVE_LCD_BITMAP /* charcell doesnt have ACTION_TREE_PGLEFT so this isnt needed */
692 if (oldbutton == ACTION_TREE_PGLEFT)
693 break;
694 else
695 #endif
696 return GO_TO_ROOT;
699 #ifdef HAVE_TAGCACHE
700 if (id3db)
701 tagtree_exit(&tc);
702 else
703 #endif
704 if (ft_exit(&tc) == 3)
705 exit_func = true;
707 restore = true;
708 break;
710 case ACTION_TREE_STOP:
711 if (list_stop_handler())
712 restore = true;
713 break;
715 case ACTION_STD_MENU:
716 return GO_TO_ROOT;
717 break;
719 #ifdef HAVE_RECORDING
720 case ACTION_STD_REC:
721 return GO_TO_RECSCREEN;
722 #endif
724 case ACTION_TREE_WPS:
725 return GO_TO_PREVIOUS_MUSIC;
726 break;
727 #ifdef HAVE_QUICKSCREEN
728 case ACTION_STD_QUICKSCREEN:
729 /* don't enter f2 from plugin browser */
730 if (*tc.dirfilter < NUM_FILTER_MODES)
732 if (quick_screen_quick(button))
733 reload_dir = true;
734 restore = true;
736 break;
737 #endif
738 #ifdef BUTTON_F3
739 case ACTION_F3:
740 /* don't enter f3 from plugin browser */
741 if (*tc.dirfilter < NUM_FILTER_MODES)
743 if (quick_screen_f3(ACTION_F3))
744 reload_dir = true;
745 restore = true;
747 break;
748 #endif
750 case ACTION_STD_CONTEXT:
752 int onplay_result;
753 int attr = 0;
755 if(!numentries)
756 onplay_result = onplay(NULL, 0, curr_context);
757 else {
758 #ifdef HAVE_TAGCACHE
759 if (id3db)
761 if (tagtree_get_attr(&tc) == FILE_ATTR_AUDIO)
763 attr = FILE_ATTR_AUDIO;
764 tagtree_get_filename(&tc, buf, sizeof(buf));
766 else
767 attr = ATTR_DIRECTORY;
769 else
770 #endif
772 attr = dircache[tc.selected_item].attr;
774 if (currdir[1]) /* Not in / */
775 snprintf(buf, sizeof buf, "%s/%s",
776 currdir,
777 dircache[tc.selected_item].name);
778 else /* In / */
779 snprintf(buf, sizeof buf, "/%s",
780 dircache[tc.selected_item].name);
782 onplay_result = onplay(buf, attr, curr_context);
784 switch (onplay_result)
786 case ONPLAY_MAINMENU:
787 return GO_TO_ROOT;
789 case ONPLAY_OK:
790 restore = true;
791 break;
793 case ONPLAY_RELOAD_DIR:
794 reload_dir = true;
795 break;
797 case ONPLAY_START_PLAY:
798 return GO_TO_WPS;
799 break;
801 break;
804 case ACTION_NONE:
805 gui_syncstatusbar_draw(&statusbars, false);
806 break;
808 #ifdef HAVE_HOTSWAP
809 case SYS_FS_CHANGED:
810 #ifdef HAVE_TAGCACHE
811 if (!id3db)
812 #endif
813 reload_dir = true;
814 /* The 'dir no longer valid' situation will be caught later
815 * by checking the showdir() result. */
816 break;
817 #endif
819 default:
820 if (default_event_handler(button) == SYS_USB_CONNECTED)
822 if(*tc.dirfilter > NUM_FILTER_MODES)
823 /* leave sub-browsers after usb, doing otherwise
824 might be confusing to the user */
825 exit_func = true;
826 else
827 reload_dir = true;
829 break;
831 if (start_wps)
832 return GO_TO_WPS;
833 if (button)
835 ata_spin();
839 check_rescan:
840 /* do we need to rescan dir? */
841 if (reload_dir || reload_root ||
842 lastfilter != *tc.dirfilter ||
843 lastsortcase != global_settings.sort_case)
845 if (reload_root) {
846 strcpy(currdir, "/");
847 tc.dirlevel = 0;
848 #ifdef HAVE_TAGCACHE
849 tc.currtable = 0;
850 tc.currextra = 0;
851 lasttable = -1;
852 lastextra = -1;
853 #endif
854 reload_root = false;
857 if (!reload_dir)
859 gui_synclist_select_item(&tree_lists, 0);
860 gui_synclist_draw(&tree_lists);
861 tc.selected_item = 0;
862 lastdir[0] = 0;
865 lastfilter = *tc.dirfilter;
866 lastsortcase = global_settings.sort_case;
867 restore = true;
870 if (exit_func)
871 return GO_TO_PREVIOUS;
873 if (restore || reload_dir) {
874 /* restore display */
875 numentries = update_dir();
876 reload_dir = false;
877 if (currdir[1] && (numentries < 0))
878 { /* not in root and reload failed */
879 reload_root = true; /* try root */
880 goto check_rescan;
884 return true;
887 bool create_playlist(void)
889 char filename[MAX_PATH];
891 snprintf(filename, sizeof filename, "%s.m3u8",
892 tc.currdir[1] ? tc.currdir : "/root");
893 gui_syncsplash(0, "%s %s", str(LANG_CREATING), filename);
895 trigger_cpu_boost();
896 catalog_add_to_a_playlist(tc.currdir, ATTR_DIRECTORY, true, filename);
897 cancel_cpu_boost();
899 return true;
902 int rockbox_browse(const char *root, int dirfilter)
904 int ret_val = 0;
905 int *last_filter = tc.dirfilter;
906 tc.dirfilter = &dirfilter;
908 reload_dir = true;
909 if (dirfilter >= NUM_FILTER_MODES)
911 static struct tree_context backup;
912 int last_context;
914 backup = tc;
915 tc.selected_item = 0;
916 tc.dirlevel = 0;
917 memcpy(tc.currdir, root, sizeof(tc.currdir));
918 start_wps = false;
919 last_context = curr_context;
921 ret_val = dirbrowse();
922 tc = backup;
923 curr_context = last_context;
925 else
927 static char buf[MAX_PATH];
928 if (dirfilter != SHOW_ID3DB)
929 tc.dirfilter = &global_settings.dirfilter;
930 strcpy(buf,root);
931 set_current_file(buf);
932 ret_val = dirbrowse();
934 tc.dirfilter = last_filter;
935 return ret_val;
938 void tree_mem_init(void)
940 /* We copy the settings value in case it is changed by the user. We can't
941 use it until the next reboot. */
942 max_files = global_settings.max_files_in_dir;
944 /* initialize tree context struct */
945 memset(&tc, 0, sizeof(tc));
946 tc.dirfilter = &global_settings.dirfilter;
948 tc.name_buffer_size = AVERAGE_FILENAME_LENGTH * max_files;
949 tc.name_buffer = buffer_alloc(tc.name_buffer_size);
951 tc.dircache_size = max_files * sizeof(struct entry);
952 tc.dircache = buffer_alloc(tc.dircache_size);
953 tree_get_filetypes(&filetypes, &filetypes_count);
956 bool bookmark_play(char *resume_file, int index, int offset, int seed,
957 char *filename)
959 int i;
960 char* suffix = strrchr(resume_file, '.');
961 bool started = false;
963 if (suffix != NULL &&
964 (!strcasecmp(suffix, ".m3u") || !strcasecmp(suffix, ".m3u8")))
966 /* Playlist playback */
967 char* slash;
968 /* check that the file exists */
969 if(!file_exists(resume_file))
970 return false;
972 slash = strrchr(resume_file,'/');
973 if (slash)
975 char* cp;
976 *slash=0;
978 cp=resume_file;
979 if (!cp[0])
980 cp="/";
982 if (playlist_create(cp, slash+1) != -1)
984 if (global_settings.playlist_shuffle)
985 playlist_shuffle(seed, -1);
986 playlist_start(index,offset);
987 started = true;
989 *slash='/';
992 else
994 /* Directory playback */
995 lastdir[0]='\0';
996 if (playlist_create(resume_file, NULL) != -1)
998 char* peek_filename;
999 resume_directory(resume_file);
1000 if (global_settings.playlist_shuffle)
1001 playlist_shuffle(seed, -1);
1003 /* Check if the file is at the same spot in the directory,
1004 else search for it */
1005 peek_filename = playlist_peek(index);
1007 if (peek_filename == NULL)
1008 return false;
1010 if (strcmp(strrchr(peek_filename, '/') + 1, filename))
1012 for ( i=0; i < playlist_amount(); i++ )
1014 peek_filename = playlist_peek(i);
1016 if (peek_filename == NULL)
1017 return false;
1019 if (!strcmp(strrchr(peek_filename, '/') + 1, filename))
1020 break;
1022 if (i < playlist_amount())
1023 index = i;
1024 else
1025 return false;
1027 playlist_start(index,offset);
1028 started = true;
1032 if (started)
1033 start_wps = true;
1034 return started;
1037 static void say_filetype(int attr)
1039 /* try to find a voice ID for the extension, if known */
1040 int j;
1041 attr &= FILE_ATTR_MASK; /* file type */
1042 for (j=0; j<filetypes_count; j++)
1043 if (attr == filetypes[j].tree_attr)
1045 talk_id(filetypes[j].voiceclip, true);
1046 return;
1050 static int ft_play_dirname(char* name)
1052 char dirname_mp3_filename[MAX_PATH+1];
1054 #if CONFIG_CODEC != SWCODEC
1055 if (audio_status() & AUDIO_STATUS_PLAY)
1056 return 0;
1057 #endif
1059 snprintf(dirname_mp3_filename, sizeof(dirname_mp3_filename), "%s/%s/%s",
1060 tc.currdir[1] ? tc.currdir : "" , name,
1061 dir_thumbnail_name);
1063 DEBUGF("Checking for %s\n", dirname_mp3_filename);
1065 if (!file_exists(dirname_mp3_filename))
1067 DEBUGF("Failed to find: %s\n", dirname_mp3_filename);
1068 return -1;
1071 DEBUGF("Found: %s\n", dirname_mp3_filename);
1073 talk_file(dirname_mp3_filename, false);
1074 if(global_settings.talk_filetype)
1075 talk_id(VOICE_DIR, true);
1076 return 1;
1079 static void ft_play_filename(char *dir, char *file)
1081 char name_mp3_filename[MAX_PATH+1];
1083 #if CONFIG_CODEC != SWCODEC
1084 if (audio_status() & AUDIO_STATUS_PLAY)
1085 return;
1086 #endif
1088 if (strcasecmp(&file[strlen(file) - strlen(file_thumbnail_ext)],
1089 file_thumbnail_ext))
1090 { /* file has no .talk extension */
1091 snprintf(name_mp3_filename, sizeof(name_mp3_filename),
1092 "%s/%s%s", dir, file, file_thumbnail_ext);
1094 talk_file(name_mp3_filename, false);
1096 else
1097 { /* it already is a .talk file, play this directly */
1098 snprintf(name_mp3_filename, sizeof(name_mp3_filename),
1099 "%s/%s", dir, file);
1100 talk_id(LANG_VOICE_DIR_HOVER, false); /* prefix it */
1101 talk_file(name_mp3_filename, true);
1105 /* These two functions are called by the USB and shutdown handlers */
1106 void tree_flush(void)
1108 scrobbler_shutdown();
1109 #ifdef HAVE_TAGCACHE
1110 tagcache_shutdown();
1111 #endif
1112 playlist_shutdown();
1114 #ifdef HAVE_TC_RAMCACHE
1115 tagcache_unload_ramcache();
1116 #endif
1118 #ifdef HAVE_DIRCACHE
1120 int old_val = global_status.dircache_size;
1121 if (global_settings.dircache)
1123 if (!dircache_is_initializing())
1124 global_status.dircache_size = dircache_get_cache_size();
1125 # ifdef HAVE_EEPROM_SETTINGS
1126 if (firmware_settings.initialized)
1127 dircache_save();
1128 # endif
1129 dircache_disable();
1131 else
1133 global_status.dircache_size = 0;
1135 if (old_val != global_status.dircache_size)
1136 status_save();
1138 #endif
1141 void tree_restore(void)
1143 #ifdef HAVE_EEPROM_SETTINGS
1144 firmware_settings.disk_clean = false;
1145 #endif
1147 #ifdef HAVE_TC_RAMCACHE
1148 remove(TAGCACHE_STATEFILE);
1149 #endif
1151 #ifdef HAVE_DIRCACHE
1152 remove(DIRCACHE_FILE);
1153 if (global_settings.dircache)
1155 /* Print "Scanning disk..." to the display. */
1156 gui_syncsplash(0, str(LANG_SCANNING_DISK));
1158 dircache_build(global_status.dircache_size);
1160 #endif
1161 #ifdef HAVE_TAGCACHE
1162 tagcache_start_scan();
1163 #endif
1164 scrobbler_init();