Fix returning too early (before cleanup) in RFAC, which led to not cancelling
[kugel-rb/myfork.git] / apps / tree.c
blobd1d0ec3b2717fb43824d9de808fbbaaa0fc2aa43
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 "button.h"
32 #include "kernel.h"
33 #include "usb.h"
34 #include "tree.h"
35 #include "sprintf.h"
36 #include "audio.h"
37 #include "playlist.h"
38 #include "menu.h"
39 #include "gwps.h"
40 #include "settings.h"
41 #include "debug.h"
42 #include "storage.h"
43 #include "rolo.h"
44 #include "icons.h"
45 #include "lang.h"
46 #include "screens.h"
47 #include "keyboard.h"
48 #include "bookmark.h"
49 #include "onplay.h"
50 #include "buffer.h"
51 #include "power.h"
52 #include "action.h"
53 #include "talk.h"
54 #include "filetypes.h"
55 #include "misc.h"
56 #include "filetree.h"
57 #include "tagtree.h"
58 #ifdef HAVE_RECORDING
59 #include "recorder/recording.h"
60 #endif
61 #include "rtc.h"
62 #include "dircache.h"
63 #ifdef HAVE_TAGCACHE
64 #include "tagcache.h"
65 #endif
66 #include "yesno.h"
67 #include "gwps-common.h"
68 #include "eeprom_settings.h"
69 #include "playlist_catalog.h"
71 /* gui api */
72 #include "list.h"
73 #include "splash.h"
74 #include "buttonbar.h"
75 #include "quickscreen.h"
77 #include "root_menu.h"
79 static const struct filetype *filetypes;
80 static int filetypes_count;
82 struct gui_synclist tree_lists;
84 /* I put it here because other files doesn't use it yet,
85 * but should be elsewhere since it will be used mostly everywhere */
86 #ifdef HAVE_BUTTONBAR
87 struct gui_buttonbar tree_buttonbar;
88 #endif
89 static struct tree_context tc;
91 bool boot_changed = false;
93 char lastfile[MAX_PATH];
94 static char lastdir[MAX_PATH];
95 #ifdef HAVE_TAGCACHE
96 static int lasttable, lastextra, lastfirstpos;
97 #endif
98 static int max_files = 0;
100 static bool reload_dir = false;
102 static bool start_wps = false;
103 static int curr_context = false;/* id3db or tree*/
105 static int dirbrowse(void);
106 static int ft_play_dirname(char* name);
107 static void ft_play_filename(char *dir, char *file);
108 static void say_filetype(int attr);
110 static char * tree_get_filename(int selected_item, void *data,
111 char *buffer, size_t buffer_len)
113 struct tree_context * local_tc=(struct tree_context *)data;
114 char *name;
115 int attr=0;
116 bool stripit = false;
117 #ifdef HAVE_TAGCACHE
118 bool id3db = *(local_tc->dirfilter) == SHOW_ID3DB;
120 if (id3db)
122 return tagtree_get_entry(&tc, selected_item)->name;
124 else
125 #endif
127 struct entry* dc = local_tc->dircache;
128 struct entry* e = &dc[selected_item];
129 name = e->name;
130 attr = e->attr;
133 if(!(attr & ATTR_DIRECTORY))
135 switch(global_settings.show_filename_ext)
137 case 0:
138 /* show file extension: off */
139 stripit = true;
140 break;
141 case 1:
142 /* show file extension: on */
143 break;
144 case 2:
145 /* show file extension: only unknown types */
146 stripit = filetype_supported(attr);
147 break;
148 case 3:
149 default:
150 /* show file extension: only when viewing all */
151 stripit = (*(local_tc->dirfilter) != SHOW_ID3DB) &&
152 (*(local_tc->dirfilter) != SHOW_ALL);
153 break;
157 if(stripit)
159 return(strip_extension(buffer, buffer_len, name));
161 return(name);
164 #ifdef HAVE_LCD_COLOR
165 static int tree_get_filecolor(int selected_item, void * data)
167 if (*tc.dirfilter == SHOW_ID3DB)
168 return -1;
169 struct tree_context * local_tc=(struct tree_context *)data;
170 struct entry* dc = local_tc->dircache;
171 struct entry* e = &dc[selected_item];
172 return filetype_get_color(e->name, e->attr);
174 #endif
176 static int tree_get_fileicon(int selected_item, void * data)
178 struct tree_context * local_tc=(struct tree_context *)data;
179 #ifdef HAVE_TAGCACHE
180 bool id3db = *(local_tc->dirfilter) == SHOW_ID3DB;
181 if (id3db) {
182 return tagtree_get_icon(&tc);
184 else
185 #endif
187 struct entry* dc = local_tc->dircache;
188 struct entry* e = &dc[selected_item];
189 return filetype_get_icon(e->attr);
193 static int tree_voice_cb(int selected_item, void * data)
195 struct tree_context * local_tc=(struct tree_context *)data;
196 char *name;
197 int attr=0;
198 #ifdef HAVE_TAGCACHE
199 bool id3db = *(local_tc->dirfilter) == SHOW_ID3DB;
201 if (id3db)
203 attr = tagtree_get_attr(local_tc);
204 name = tagtree_get_entry(local_tc, selected_item)->name;
206 else
207 #endif
209 struct entry* dc = local_tc->dircache;
210 struct entry* e = &dc[selected_item];
211 name = e->name;
212 attr = e->attr;
214 bool is_dir = (attr & ATTR_DIRECTORY);
215 bool did_clip = false;
216 /* First the .talk clip case */
217 if(is_dir)
219 if(global_settings.talk_dir_clip)
221 did_clip = true;
222 if(ft_play_dirname(name) <0)
223 /* failed, not existing */
224 did_clip = false;
226 } else { /* it's a file */
227 if (global_settings.talk_file_clip && (attr & FILE_ATTR_THUMBNAIL))
229 did_clip = true;
230 ft_play_filename(local_tc->currdir, name);
233 if(!did_clip)
235 /* say the number or spell if required or as a fallback */
236 switch (is_dir ? global_settings.talk_dir : global_settings.talk_file)
238 case 1: /* as numbers */
239 talk_id(is_dir ? VOICE_DIR : VOICE_FILE, false);
240 talk_number(selected_item+1 - (is_dir ? 0 : local_tc->dirsindir),
241 true);
242 if(global_settings.talk_filetype
243 && !is_dir && *local_tc->dirfilter < NUM_FILTER_MODES)
244 say_filetype(attr);
245 break;
246 case 2: /* spelled */
247 talk_shutup();
248 if(global_settings.talk_filetype)
250 if(is_dir)
251 talk_id(VOICE_DIR, true);
252 else if(*local_tc->dirfilter < NUM_FILTER_MODES)
253 say_filetype(attr);
255 talk_spell(name, true);
256 break;
259 return 0;
262 bool check_rockboxdir(void)
264 if(!dir_exists(ROCKBOX_DIR))
265 { /* No need to localise this message.
266 If .rockbox is missing, it wouldn't work anyway */
267 int i;
268 FOR_NB_SCREENS(i)
269 screens[i].clear_display();
270 splash(HZ*2, "No .rockbox directory");
271 FOR_NB_SCREENS(i)
272 screens[i].clear_display();
273 splash(HZ*2, "Installation incomplete");
274 return false;
276 return true;
279 /* do this really late in the init sequence */
280 void tree_gui_init(void)
282 check_rockboxdir();
284 strcpy(tc.currdir, "/");
286 #ifdef HAVE_LCD_CHARCELLS
287 int i;
288 FOR_NB_SCREENS(i)
289 screens[i].double_height(false);
290 #endif
291 #ifdef HAVE_BUTTONBAR
292 gui_buttonbar_init(&tree_buttonbar);
293 /* since archos only have one screen, no need to create more than that */
294 gui_buttonbar_set_display(&tree_buttonbar, &(screens[SCREEN_MAIN]) );
295 #endif
296 gui_synclist_init(&tree_lists, &tree_get_filename, &tc, false, 1, NULL);
297 gui_synclist_set_voice_callback(&tree_lists, tree_voice_cb);
298 gui_synclist_set_icon_callback(&tree_lists, &tree_get_fileicon);
299 #ifdef HAVE_LCD_COLOR
300 gui_synclist_set_color_callback(&tree_lists, &tree_get_filecolor);
301 #endif
306 struct tree_context* tree_get_context(void)
308 return &tc;
312 * Returns the position of a given file in the current directory
313 * returns -1 if not found
315 static int tree_get_file_position(char * filename)
317 int i;
319 /* use lastfile to determine the selected item (default=0) */
320 for (i=0; i < tc.filesindir; i++)
322 struct entry* dc = tc.dircache;
323 struct entry* e = &dc[i];
324 if (!strcasecmp(e->name, filename))
325 return(i);
327 return(-1);/* no file can match, returns undefined */
331 * Called when a new dir is loaded (for example when returning from other apps ...)
332 * also completely redraws the tree
334 static int update_dir(void)
336 bool changed = false;
337 #ifdef HAVE_TAGCACHE
338 bool id3db = *tc.dirfilter == SHOW_ID3DB;
339 /* Checks for changes */
340 if (id3db) {
341 if (tc.currtable != lasttable ||
342 tc.currextra != lastextra ||
343 tc.firstpos != lastfirstpos ||
344 reload_dir)
346 if (tagtree_load(&tc) < 0)
347 return -1;
349 lasttable = tc.currtable;
350 lastextra = tc.currextra;
351 lastfirstpos = tc.firstpos;
352 changed = true;
355 else
356 #endif
358 /* if the tc.currdir has been changed, reload it ...*/
359 if (strncmp(tc.currdir, lastdir, sizeof(lastdir)) || reload_dir)
361 if (ft_load(&tc, NULL) < 0)
362 return -1;
363 strcpy(lastdir, tc.currdir);
364 changed = true;
367 /* if selected item is undefined */
368 if (tc.selected_item == -1)
370 /* use lastfile to determine the selected item */
371 tc.selected_item = tree_get_file_position(lastfile);
373 /* If the file doesn't exists, select the first one (default) */
374 if(tc.selected_item < 0)
375 tc.selected_item = 0;
376 changed = true;
378 if (changed)
381 #ifdef HAVE_TAGCACHE
382 !id3db &&
383 #endif
384 (tc.dirfull || tc.filesindir == global_settings.max_files_in_dir) )
386 splash(HZ, ID2P(LANG_SHOWDIR_BUFFER_FULL));
389 #ifdef HAVE_TAGCACHE
390 if (id3db)
392 #ifdef HAVE_LCD_BITMAP
393 if (global_settings.show_path_in_browser == SHOW_PATH_FULL
394 || global_settings.show_path_in_browser == SHOW_PATH_CURRENT)
396 gui_synclist_set_title(&tree_lists, tagtree_get_title(&tc),
397 filetype_get_icon(ATTR_DIRECTORY));
399 else
401 /* Must clear the title as the list is reused */
402 gui_synclist_set_title(&tree_lists, NULL, NOICON);
404 #endif
406 else
407 #endif
409 #ifdef HAVE_LCD_BITMAP
410 if (global_settings.show_path_in_browser &&
411 *(tc.dirfilter) == SHOW_PLUGINS)
413 char *title;
414 if (!strcmp(tc.currdir, PLUGIN_GAMES_DIR))
415 title = str(LANG_PLUGIN_GAMES);
416 else if (!strcmp(tc.currdir, PLUGIN_APPS_DIR))
417 title = str(LANG_PLUGIN_APPS);
418 else if (!strcmp(tc.currdir, PLUGIN_DEMOS_DIR))
419 title = str(LANG_PLUGIN_DEMOS);
420 else title = str(LANG_PLUGINS);
421 gui_synclist_set_title(&tree_lists, title, Icon_Plugin);
423 else if (global_settings.show_path_in_browser == SHOW_PATH_FULL)
425 gui_synclist_set_title(&tree_lists, tc.currdir,
426 filetype_get_icon(ATTR_DIRECTORY));
428 else if (global_settings.show_path_in_browser == SHOW_PATH_CURRENT)
430 char *title = strrchr(tc.currdir, '/') + 1;
431 if (*title == '\0')
433 /* Display "Files" for the root dir */
434 gui_synclist_set_title(&tree_lists, str(LANG_DIR_BROWSER),
435 filetype_get_icon(ATTR_DIRECTORY));
437 else
438 gui_synclist_set_title(&tree_lists, title,
439 filetype_get_icon(ATTR_DIRECTORY));
441 else
443 /* Must clear the title as the list is reused */
444 gui_synclist_set_title(&tree_lists, NULL, NOICON);
446 #endif
449 gui_synclist_set_nb_items(&tree_lists, tc.filesindir);
450 gui_synclist_set_icon_callback(&tree_lists, tree_get_fileicon);
451 if( tc.selected_item >= tc.filesindir)
452 tc.selected_item=tc.filesindir-1;
454 gui_synclist_select_item(&tree_lists, tc.selected_item);
455 #ifdef HAVE_BUTTONBAR
456 if (global_settings.buttonbar) {
457 if (*tc.dirfilter < NUM_FILTER_MODES)
458 gui_buttonbar_set(&tree_buttonbar, str(LANG_SYSFONT_DIRBROWSE_F1),
459 str(LANG_SYSFONT_DIRBROWSE_F2),
460 str(LANG_SYSFONT_DIRBROWSE_F3));
461 else
462 gui_buttonbar_set(&tree_buttonbar, "<<<", "", "");
463 gui_buttonbar_draw(&tree_buttonbar);
465 #endif
466 gui_synclist_draw(&tree_lists);
467 gui_synclist_speak_item(&tree_lists);
468 return tc.filesindir;
471 /* load tracks from specified directory to resume play */
472 void resume_directory(const char *dir)
474 int dirfilter = *tc.dirfilter;
475 int ret;
476 #ifdef HAVE_TAGCACHE
477 bool id3db = *tc.dirfilter == SHOW_ID3DB;
478 #endif
479 /* make sure the dirfilter is sane. The only time it should be possible
480 * thats its not is when resume playlist is called from a plugin
482 #ifdef HAVE_TAGCACHE
483 if (!id3db)
484 #endif
485 *tc.dirfilter = global_settings.dirfilter;
486 ret = ft_load(&tc, dir);
487 *tc.dirfilter = dirfilter;
488 if (ret < 0)
489 return;
490 lastdir[0] = 0;
492 ft_build_playlist(&tc, 0);
494 #ifdef HAVE_TAGCACHE
495 if (id3db)
496 tagtree_load(&tc);
497 #endif
500 /* Returns the current working directory and also writes cwd to buf if
501 non-NULL. In case of error, returns NULL. */
502 char *getcwd(char *buf, int size)
504 if (!buf)
505 return tc.currdir;
506 else if (size > 0)
508 strncpy(buf, tc.currdir, size);
509 return buf;
511 else
512 return NULL;
515 /* Force a reload of the directory next time directory browser is called */
516 void reload_directory(void)
518 reload_dir = true;
521 void get_current_file(char* buffer, int buffer_len)
523 #ifdef HAVE_TAGCACHE
524 /* in ID3DB mode it is a bad idea to call this function */
525 /* (only happens with `follow playlist') */
526 if( *tc.dirfilter == SHOW_ID3DB )
527 return;
528 #endif
530 struct entry* dc = tc.dircache;
531 struct entry* e = &dc[tc.selected_item];
532 snprintf(buffer, buffer_len, "%s/%s", getcwd(NULL,0),
533 tc.dirlength ? e->name : "");
536 /* Allow apps to change our dirfilter directly (required for sub browsers)
537 if they're suddenly going to become a file browser for example */
538 void set_dirfilter(int l_dirfilter)
540 *tc.dirfilter = l_dirfilter;
543 /* Selects a file and update tree context properly */
544 void set_current_file(char *path)
546 char *name;
547 int i;
549 #ifdef HAVE_TAGCACHE
550 /* in ID3DB mode it is a bad idea to call this function */
551 /* (only happens with `follow playlist') */
552 if( *tc.dirfilter == SHOW_ID3DB )
553 return;
554 #endif
556 /* separate directory from filename */
557 /* gets the directory's name and put it into tc.currdir */
558 name = strrchr(path+1,'/');
559 if (name)
561 *name = 0;
562 strcpy(tc.currdir, path);
563 *name = '/';
564 name++;
566 else
568 strcpy(tc.currdir, "/");
569 name = path+1;
572 strcpy(lastfile, name);
575 /* If we changed dir we must recalculate the dirlevel
576 and adjust the selected history properly */
577 if (strncmp(tc.currdir,lastdir,sizeof(lastdir)))
579 tc.dirlevel = 0;
580 tc.selected_item_history[tc.dirlevel] = -1;
582 /* use '/' to calculate dirlevel */
583 for (i = 1; path[i] != '\0'; i++)
585 if (path[i] == '/')
587 tc.dirlevel++;
588 tc.selected_item_history[tc.dirlevel] = -1;
592 if (ft_load(&tc, NULL) >= 0)
594 tc.selected_item = tree_get_file_position(lastfile);
599 /* main loop, handles key events */
600 static int dirbrowse()
602 int numentries=0;
603 char buf[MAX_PATH];
604 int button, oldbutton;
605 bool reload_root = false;
606 int lastfilter = *tc.dirfilter;
607 bool lastsortcase = global_settings.sort_case;
608 bool exit_func = false;
610 char* currdir = tc.currdir; /* just a shortcut */
611 #ifdef HAVE_TAGCACHE
612 bool id3db = *tc.dirfilter == SHOW_ID3DB;
614 if (id3db)
615 curr_context=CONTEXT_ID3DB;
616 else
617 #endif
618 curr_context=CONTEXT_TREE;
619 if (tc.selected_item < 0)
620 tc.selected_item = 0;
621 #ifdef HAVE_TAGCACHE
622 tc.firstpos = 0;
623 lasttable = -1;
624 lastextra = -1;
625 lastfirstpos = 0;
626 #endif
628 start_wps = false;
629 numentries = update_dir();
630 reload_dir = false;
631 if (numentries == -1)
632 return GO_TO_PREVIOUS; /* currdir is not a directory */
634 if (*tc.dirfilter > NUM_FILTER_MODES && numentries==0)
636 splash(HZ*2, ID2P(LANG_NO_FILES));
637 return GO_TO_PREVIOUS; /* No files found for rockbox_browser() */
640 gui_synclist_draw(&tree_lists);
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 #ifdef HAVE_HOTSWAP
803 case SYS_FS_CHANGED:
804 #ifdef HAVE_TAGCACHE
805 if (!id3db)
806 #endif
807 reload_dir = true;
808 /* The 'dir no longer valid' situation will be caught later
809 * by checking the showdir() result. */
810 break;
811 #endif
813 default:
814 if (default_event_handler(button) == SYS_USB_CONNECTED)
816 if(*tc.dirfilter > NUM_FILTER_MODES)
817 /* leave sub-browsers after usb, doing otherwise
818 might be confusing to the user */
819 exit_func = true;
820 else
821 reload_dir = true;
823 break;
825 if (start_wps)
826 return GO_TO_WPS;
827 if (button && !IS_SYSEVENT(button))
829 storage_spin();
833 check_rescan:
834 /* do we need to rescan dir? */
835 if (reload_dir || reload_root ||
836 lastfilter != *tc.dirfilter ||
837 lastsortcase != global_settings.sort_case)
839 if (reload_root) {
840 strcpy(currdir, "/");
841 tc.dirlevel = 0;
842 #ifdef HAVE_TAGCACHE
843 tc.currtable = 0;
844 tc.currextra = 0;
845 lasttable = -1;
846 lastextra = -1;
847 #endif
848 reload_root = false;
851 if (!reload_dir)
853 gui_synclist_select_item(&tree_lists, 0);
854 gui_synclist_draw(&tree_lists);
855 tc.selected_item = 0;
856 lastdir[0] = 0;
859 lastfilter = *tc.dirfilter;
860 lastsortcase = global_settings.sort_case;
861 restore = true;
864 if (exit_func)
865 return GO_TO_PREVIOUS;
867 if (restore || reload_dir) {
868 /* restore display */
869 numentries = update_dir();
870 reload_dir = false;
871 if (currdir[1] && (numentries < 0))
872 { /* not in root and reload failed */
873 reload_root = true; /* try root */
874 goto check_rescan;
878 return true;
881 bool create_playlist(void)
883 char filename[MAX_PATH];
885 snprintf(filename, sizeof filename, "%s.m3u8",
886 tc.currdir[1] ? tc.currdir : "/root");
887 splashf(0, "%s %s", str(LANG_CREATING), filename);
889 trigger_cpu_boost();
890 catalog_add_to_a_playlist(tc.currdir, ATTR_DIRECTORY, true, filename);
891 cancel_cpu_boost();
893 return true;
896 int rockbox_browse(const char *root, int dirfilter)
898 int ret_val = 0;
899 int *last_filter = tc.dirfilter;
900 tc.dirfilter = &dirfilter;
901 tc.sort_dir = global_settings.sort_dir;
903 reload_dir = true;
904 if (dirfilter >= NUM_FILTER_MODES)
906 static struct tree_context backup;
907 char current[MAX_PATH];
908 int last_context;
910 backup = tc;
911 tc.selected_item = 0;
912 tc.dirlevel = 0;
913 memcpy(tc.currdir, root, sizeof(tc.currdir));
914 start_wps = false;
915 last_context = curr_context;
917 /* Center on the currently loaded language when browsing languages. */
918 if (dirfilter == SHOW_LNG)
920 if (global_settings.lang_file[0])
922 snprintf(current, sizeof(current), LANG_DIR "/%s.lng",
923 global_settings.lang_file);
925 else
927 strncpy(current, LANG_DIR "/english.lng", sizeof(current));
930 /* Center on currently loaded WPS */
931 else if (dirfilter == SHOW_WPS)
933 snprintf(current, sizeof(current), WPS_DIR "/%s.wps",
934 global_settings.wps_file);
936 #ifdef HAVE_REMOTE_LCD
937 /* Center on currently loaded RWPS */
938 else if (dirfilter == SHOW_RWPS)
940 snprintf(current, sizeof(current), WPS_DIR "/%s.rwps",
941 global_settings.rwps_file);
943 #endif
944 #ifdef HAVE_LCD_BITMAP
945 /* Center on the currently loaded font when browsing fonts */
946 else if (dirfilter == SHOW_FONT)
948 snprintf(current, sizeof(current), FONT_DIR "/%s.fnt",
949 global_settings.font_file);
951 #endif
952 #if CONFIG_TUNER
953 /* Center on the currently loaded FM preset when browsing those */
954 else if (dirfilter == SHOW_FMR)
956 snprintf(current, sizeof(current), FMPRESET_PATH "/%s.fmr",
957 global_settings.fmr_file);
959 #endif
961 /* If we've found a file to center on, do it */
962 if (current[0] == '/')
964 set_current_file(current);
965 /* set_current_file changes dirlevel, change it back */
966 tc.dirlevel = 0;
969 ret_val = dirbrowse();
970 tc = backup;
971 curr_context = last_context;
973 else
975 static char buf[MAX_PATH];
976 if (dirfilter != SHOW_ID3DB)
977 tc.dirfilter = &global_settings.dirfilter;
978 strcpy(buf,root);
979 set_current_file(buf);
980 ret_val = dirbrowse();
982 tc.dirfilter = last_filter;
983 return ret_val;
986 void tree_mem_init(void)
988 /* We copy the settings value in case it is changed by the user. We can't
989 use it until the next reboot. */
990 max_files = global_settings.max_files_in_dir;
992 /* initialize tree context struct */
993 memset(&tc, 0, sizeof(tc));
994 tc.dirfilter = &global_settings.dirfilter;
995 tc.sort_dir = global_settings.sort_dir;
997 tc.name_buffer_size = AVERAGE_FILENAME_LENGTH * max_files;
998 tc.name_buffer = buffer_alloc(tc.name_buffer_size);
1000 tc.dircache_size = max_files * sizeof(struct entry);
1001 tc.dircache = buffer_alloc(tc.dircache_size);
1002 tree_get_filetypes(&filetypes, &filetypes_count);
1005 bool bookmark_play(char *resume_file, int index, int offset, int seed,
1006 char *filename)
1008 int i;
1009 char* suffix = strrchr(resume_file, '.');
1010 bool started = false;
1012 if (suffix != NULL &&
1013 (!strcasecmp(suffix, ".m3u") || !strcasecmp(suffix, ".m3u8")))
1015 /* Playlist playback */
1016 char* slash;
1017 /* check that the file exists */
1018 if(!file_exists(resume_file))
1019 return false;
1021 slash = strrchr(resume_file,'/');
1022 if (slash)
1024 char* cp;
1025 *slash=0;
1027 cp=resume_file;
1028 if (!cp[0])
1029 cp="/";
1031 if (playlist_create(cp, slash+1) != -1)
1033 if (global_settings.playlist_shuffle)
1034 playlist_shuffle(seed, -1);
1035 playlist_start(index,offset);
1036 started = true;
1038 *slash='/';
1041 else
1043 /* Directory playback */
1044 lastdir[0]='\0';
1045 if (playlist_create(resume_file, NULL) != -1)
1047 char* peek_filename;
1048 resume_directory(resume_file);
1049 if (global_settings.playlist_shuffle)
1050 playlist_shuffle(seed, -1);
1052 /* Check if the file is at the same spot in the directory,
1053 else search for it */
1054 peek_filename = playlist_peek(index);
1056 if (peek_filename == NULL)
1057 return false;
1059 if (strcmp(strrchr(peek_filename, '/') + 1, filename))
1061 for ( i=0; i < playlist_amount(); i++ )
1063 peek_filename = playlist_peek(i);
1065 if (peek_filename == NULL)
1066 return false;
1068 if (!strcmp(strrchr(peek_filename, '/') + 1, filename))
1069 break;
1071 if (i < playlist_amount())
1072 index = i;
1073 else
1074 return false;
1076 playlist_start(index,offset);
1077 started = true;
1081 if (started)
1082 start_wps = true;
1083 return started;
1086 static void say_filetype(int attr)
1088 /* try to find a voice ID for the extension, if known */
1089 int j;
1090 attr &= FILE_ATTR_MASK; /* file type */
1091 for (j=0; j<filetypes_count; j++)
1092 if (attr == filetypes[j].tree_attr)
1094 talk_id(filetypes[j].voiceclip, true);
1095 return;
1099 static int ft_play_dirname(char* name)
1101 #if CONFIG_CODEC != SWCODEC
1102 if (audio_status() & AUDIO_STATUS_PLAY)
1103 return 0;
1104 #endif
1106 if(talk_file(tc.currdir, name, dir_thumbnail_name, NULL,
1107 NULL, false))
1109 if(global_settings.talk_filetype)
1110 talk_id(VOICE_DIR, true);
1111 return 1;
1113 else
1114 return -1;
1117 static void ft_play_filename(char *dir, char *file)
1119 #if CONFIG_CODEC != SWCODEC
1120 if (audio_status() & AUDIO_STATUS_PLAY)
1121 return;
1122 #endif
1124 if (strlen(file) >= strlen(file_thumbnail_ext)
1125 && strcasecmp(&file[strlen(file) - strlen(file_thumbnail_ext)],
1126 file_thumbnail_ext))
1127 /* file has no .talk extension */
1128 talk_file(dir, NULL, file, file_thumbnail_ext,
1129 NULL, false);
1130 else
1131 /* it already is a .talk file, play this directly, but prefix it. */
1132 talk_file(dir, NULL, file, NULL,
1133 TALK_IDARRAY(LANG_VOICE_DIR_HOVER), false);
1136 /* These two functions are called by the USB and shutdown handlers */
1137 void tree_flush(void)
1139 #ifdef HAVE_TAGCACHE
1140 tagcache_shutdown();
1141 #endif
1143 #ifdef HAVE_TC_RAMCACHE
1144 tagcache_unload_ramcache();
1145 #endif
1147 #ifdef HAVE_DIRCACHE
1149 int old_val = global_status.dircache_size;
1150 if (global_settings.dircache)
1152 if (!dircache_is_initializing())
1153 global_status.dircache_size = dircache_get_cache_size();
1154 # ifdef HAVE_EEPROM_SETTINGS
1155 if (firmware_settings.initialized)
1156 dircache_save();
1157 # endif
1158 dircache_disable();
1160 else
1162 global_status.dircache_size = 0;
1164 if (old_val != global_status.dircache_size)
1165 status_save();
1167 #endif
1170 void tree_restore(void)
1172 #ifdef HAVE_EEPROM_SETTINGS
1173 firmware_settings.disk_clean = false;
1174 #endif
1176 #ifdef HAVE_TC_RAMCACHE
1177 remove(TAGCACHE_STATEFILE);
1178 #endif
1180 #ifdef HAVE_DIRCACHE
1181 remove(DIRCACHE_FILE);
1182 if (global_settings.dircache)
1184 /* Print "Scanning disk..." to the display. */
1185 splash(0, str(LANG_SCANNING_DISK));
1187 dircache_build(global_status.dircache_size);
1189 #endif
1190 #ifdef HAVE_TAGCACHE
1191 tagcache_start_scan();
1192 #endif