fix m200v4 simulator.
[kugel-rb.git] / apps / tree.c
blob37078d4ebdcebc41e889234f067f44bfe9f8e4f3
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 "storage.h"
45 #include "rolo.h"
46 #include "icons.h"
47 #include "lang.h"
48 #include "screens.h"
49 #include "keyboard.h"
50 #include "bookmark.h"
51 #include "onplay.h"
52 #include "buffer.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"
72 #include "playlist_catalog.h"
74 /* gui api */
75 #include "list.h"
76 #include "statusbar.h"
77 #include "splash.h"
78 #include "buttonbar.h"
79 #include "action.h"
80 #include "quickscreen.h"
82 #include "root_menu.h"
83 #include "backdrop.h"
85 static const struct filetype *filetypes;
86 static int filetypes_count;
88 struct gui_synclist tree_lists;
90 /* I put it here because other files doesn't use it yet,
91 * but should be elsewhere since it will be used mostly everywhere */
92 #ifdef HAVE_BUTTONBAR
93 struct gui_buttonbar tree_buttonbar;
94 #endif
95 static struct tree_context tc;
97 bool boot_changed = false;
99 char lastfile[MAX_PATH];
100 static char lastdir[MAX_PATH];
101 #ifdef HAVE_TAGCACHE
102 static int lasttable, lastextra, lastfirstpos;
103 #endif
104 static int max_files = 0;
106 static bool reload_dir = false;
108 static bool start_wps = false;
109 static int curr_context = false;/* id3db or tree*/
111 static int dirbrowse(void);
112 static int ft_play_dirname(char* name);
113 static void ft_play_filename(char *dir, char *file);
114 static void say_filetype(int attr);
116 static char * tree_get_filename(int selected_item, void *data,
117 char *buffer, size_t buffer_len)
119 struct tree_context * local_tc=(struct tree_context *)data;
120 char *name;
121 int attr=0;
122 bool stripit = false;
123 #ifdef HAVE_TAGCACHE
124 bool id3db = *(local_tc->dirfilter) == SHOW_ID3DB;
126 if (id3db)
128 return tagtree_get_entry(&tc, selected_item)->name;
130 else
131 #endif
133 struct entry* dc = local_tc->dircache;
134 struct entry* e = &dc[selected_item];
135 name = e->name;
136 attr = e->attr;
139 if(!(attr & ATTR_DIRECTORY))
141 switch(global_settings.show_filename_ext)
143 case 0:
144 /* show file extension: off */
145 stripit = true;
146 break;
147 case 1:
148 /* show file extension: on */
149 break;
150 case 2:
151 /* show file extension: only unknown types */
152 stripit = filetype_supported(attr);
153 break;
154 case 3:
155 default:
156 /* show file extension: only when viewing all */
157 stripit = (*(local_tc->dirfilter) != SHOW_ID3DB) &&
158 (*(local_tc->dirfilter) != SHOW_ALL);
159 break;
163 if(stripit)
165 return(strip_extension(buffer, buffer_len, name));
167 return(name);
170 #ifdef HAVE_LCD_COLOR
171 static int tree_get_filecolor(int selected_item, void * data)
173 if (*tc.dirfilter == SHOW_ID3DB)
174 return -1;
175 struct tree_context * local_tc=(struct tree_context *)data;
176 struct entry* dc = local_tc->dircache;
177 struct entry* e = &dc[selected_item];
178 return filetype_get_color(e->name, e->attr);
180 #endif
182 static int tree_get_fileicon(int selected_item, void * data)
184 struct tree_context * local_tc=(struct tree_context *)data;
185 #ifdef HAVE_TAGCACHE
186 bool id3db = *(local_tc->dirfilter) == SHOW_ID3DB;
187 if (id3db) {
188 return tagtree_get_icon(&tc);
190 else
191 #endif
193 struct entry* dc = local_tc->dircache;
194 struct entry* e = &dc[selected_item];
195 return filetype_get_icon(e->attr);
199 static int tree_voice_cb(int selected_item, void * data)
201 struct tree_context * local_tc=(struct tree_context *)data;
202 char *name;
203 int attr=0;
204 #ifdef HAVE_TAGCACHE
205 bool id3db = *(local_tc->dirfilter) == SHOW_ID3DB;
207 if (id3db)
209 attr = tagtree_get_attr(local_tc);
210 name = tagtree_get_entry(local_tc, selected_item)->name;
212 else
213 #endif
215 struct entry* dc = local_tc->dircache;
216 struct entry* e = &dc[selected_item];
217 name = e->name;
218 attr = e->attr;
220 bool is_dir = (attr & ATTR_DIRECTORY);
221 bool did_clip = false;
222 /* First the .talk clip case */
223 if(is_dir)
225 if(global_settings.talk_dir_clip)
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 ft_play_filename(local_tc->currdir, name);
239 if(!did_clip)
241 /* say the number or spell if required or as a fallback */
242 switch (is_dir ? global_settings.talk_dir : global_settings.talk_file)
244 case 1: /* as numbers */
245 talk_id(is_dir ? VOICE_DIR : VOICE_FILE, false);
246 talk_number(selected_item+1 - (is_dir ? 0 : local_tc->dirsindir),
247 true);
248 if(global_settings.talk_filetype
249 && !is_dir && *local_tc->dirfilter < NUM_FILTER_MODES)
250 say_filetype(attr);
251 break;
252 case 2: /* spelled */
253 talk_shutup();
254 if(global_settings.talk_filetype)
256 if(is_dir)
257 talk_id(VOICE_DIR, true);
258 else if(*local_tc->dirfilter < NUM_FILTER_MODES)
259 say_filetype(attr);
261 talk_spell(name, true);
262 break;
265 return 0;
268 bool check_rockboxdir(void)
270 if(!dir_exists(ROCKBOX_DIR))
271 { /* No need to localise this message.
272 If .rockbox is missing, it wouldn't work anyway */
273 int i;
274 FOR_NB_SCREENS(i)
275 screens[i].clear_display();
276 splash(HZ*2, "No .rockbox directory");
277 FOR_NB_SCREENS(i)
278 screens[i].clear_display();
279 splash(HZ*2, "Installation incomplete");
280 return false;
282 return true;
285 /* do this really late in the init sequence */
286 void tree_gui_init(void)
288 check_rockboxdir();
290 strcpy(tc.currdir, "/");
292 #ifdef HAVE_LCD_CHARCELLS
293 int i;
294 FOR_NB_SCREENS(i)
295 screens[i].double_height(false);
296 #endif
297 #ifdef HAVE_BUTTONBAR
298 gui_buttonbar_init(&tree_buttonbar);
299 /* since archos only have one screen, no need to create more than that */
300 gui_buttonbar_set_display(&tree_buttonbar, &(screens[SCREEN_MAIN]) );
301 #endif
302 gui_synclist_init(&tree_lists, &tree_get_filename, &tc, false, 1, NULL);
303 gui_synclist_set_voice_callback(&tree_lists, tree_voice_cb);
304 gui_synclist_set_icon_callback(&tree_lists, &tree_get_fileicon);
305 #ifdef HAVE_LCD_COLOR
306 gui_synclist_set_color_callback(&tree_lists, &tree_get_filecolor);
307 #endif
312 struct tree_context* tree_get_context(void)
314 return &tc;
318 * Returns the position of a given file in the current directory
319 * returns -1 if not found
321 static int tree_get_file_position(char * filename)
323 int i;
325 /* use lastfile to determine the selected item (default=0) */
326 for (i=0; i < tc.filesindir; i++)
328 struct entry* dc = tc.dircache;
329 struct entry* e = &dc[i];
330 if (!strcasecmp(e->name, filename))
331 return(i);
333 return(-1);/* no file can match, returns undefined */
337 * Called when a new dir is loaded (for example when returning from other apps ...)
338 * also completely redraws the tree
340 static int update_dir(void)
342 bool changed = false;
343 #ifdef HAVE_TAGCACHE
344 bool id3db = *tc.dirfilter == SHOW_ID3DB;
345 /* Checks for changes */
346 if (id3db) {
347 if (tc.currtable != lasttable ||
348 tc.currextra != lastextra ||
349 tc.firstpos != lastfirstpos ||
350 reload_dir)
352 if (tagtree_load(&tc) < 0)
353 return -1;
355 lasttable = tc.currtable;
356 lastextra = tc.currextra;
357 lastfirstpos = tc.firstpos;
358 changed = true;
361 else
362 #endif
364 /* if the tc.currdir has been changed, reload it ...*/
365 if (strncmp(tc.currdir, lastdir, sizeof(lastdir)) || reload_dir)
367 if (ft_load(&tc, NULL) < 0)
368 return -1;
369 strcpy(lastdir, tc.currdir);
370 changed = true;
373 /* if selected item is undefined */
374 if (tc.selected_item == -1)
376 /* use lastfile to determine the selected item */
377 tc.selected_item = tree_get_file_position(lastfile);
379 /* If the file doesn't exists, select the first one (default) */
380 if(tc.selected_item < 0)
381 tc.selected_item = 0;
382 changed = true;
384 if (changed)
387 #ifdef HAVE_TAGCACHE
388 !id3db &&
389 #endif
390 (tc.dirfull || tc.filesindir == global_settings.max_files_in_dir) )
392 splash(HZ, ID2P(LANG_SHOWDIR_BUFFER_FULL));
395 #ifdef HAVE_TAGCACHE
396 if (id3db)
398 #ifdef HAVE_LCD_BITMAP
399 if (global_settings.show_path_in_browser == SHOW_PATH_FULL
400 || global_settings.show_path_in_browser == SHOW_PATH_CURRENT)
402 gui_synclist_set_title(&tree_lists, tagtree_get_title(&tc),
403 filetype_get_icon(ATTR_DIRECTORY));
405 else
407 /* Must clear the title as the list is reused */
408 gui_synclist_set_title(&tree_lists, NULL, NOICON);
410 #endif
412 else
413 #endif
415 #ifdef HAVE_LCD_BITMAP
416 if (global_settings.show_path_in_browser &&
417 *(tc.dirfilter) == SHOW_PLUGINS)
419 char *title;
420 if (!strcmp(tc.currdir, PLUGIN_GAMES_DIR))
421 title = str(LANG_PLUGIN_GAMES);
422 else if (!strcmp(tc.currdir, PLUGIN_APPS_DIR))
423 title = str(LANG_PLUGIN_APPS);
424 else if (!strcmp(tc.currdir, PLUGIN_DEMOS_DIR))
425 title = str(LANG_PLUGIN_DEMOS);
426 else title = str(LANG_PLUGINS);
427 gui_synclist_set_title(&tree_lists, title, Icon_Plugin);
429 else if (global_settings.show_path_in_browser == SHOW_PATH_FULL)
431 gui_synclist_set_title(&tree_lists, tc.currdir,
432 filetype_get_icon(ATTR_DIRECTORY));
434 else if (global_settings.show_path_in_browser == SHOW_PATH_CURRENT)
436 char *title = strrchr(tc.currdir, '/') + 1;
437 if (*title == '\0')
439 /* Display "Files" for the root dir */
440 gui_synclist_set_title(&tree_lists, str(LANG_DIR_BROWSER),
441 filetype_get_icon(ATTR_DIRECTORY));
443 else
444 gui_synclist_set_title(&tree_lists, title,
445 filetype_get_icon(ATTR_DIRECTORY));
447 else
449 /* Must clear the title as the list is reused */
450 gui_synclist_set_title(&tree_lists, NULL, NOICON);
452 #endif
455 gui_synclist_set_nb_items(&tree_lists, tc.filesindir);
456 gui_synclist_set_icon_callback(&tree_lists, tree_get_fileicon);
457 if( tc.selected_item >= tc.filesindir)
458 tc.selected_item=tc.filesindir-1;
460 gui_synclist_select_item(&tree_lists, tc.selected_item);
461 #ifdef HAVE_BUTTONBAR
462 if (global_settings.buttonbar) {
463 if (*tc.dirfilter < NUM_FILTER_MODES)
464 gui_buttonbar_set(&tree_buttonbar, str(LANG_SYSFONT_DIRBROWSE_F1),
465 str(LANG_SYSFONT_DIRBROWSE_F2),
466 str(LANG_SYSFONT_DIRBROWSE_F3));
467 else
468 gui_buttonbar_set(&tree_buttonbar, "<<<", "", "");
469 gui_buttonbar_draw(&tree_buttonbar);
471 #endif
472 gui_synclist_draw(&tree_lists);
473 gui_synclist_speak_item(&tree_lists);
474 return tc.filesindir;
477 /* load tracks from specified directory to resume play */
478 void resume_directory(const char *dir)
480 #ifdef HAVE_TAGCACHE
481 bool id3db = *tc.dirfilter == SHOW_ID3DB;
482 #endif
484 if (ft_load(&tc, dir) < 0)
485 return;
486 lastdir[0] = 0;
488 ft_build_playlist(&tc, 0);
490 #ifdef HAVE_TAGCACHE
491 if (id3db)
492 tagtree_load(&tc);
493 #endif
496 /* Returns the current working directory and also writes cwd to buf if
497 non-NULL. In case of error, returns NULL. */
498 char *getcwd(char *buf, int size)
500 if (!buf)
501 return tc.currdir;
502 else if (size > 0)
504 strncpy(buf, tc.currdir, size);
505 return buf;
507 else
508 return NULL;
511 /* Force a reload of the directory next time directory browser is called */
512 void reload_directory(void)
514 reload_dir = true;
517 void get_current_file(char* buffer, int buffer_len)
519 #ifdef HAVE_TAGCACHE
520 /* in ID3DB mode it is a bad idea to call this function */
521 /* (only happens with `follow playlist') */
522 if( *tc.dirfilter == SHOW_ID3DB )
523 return;
524 #endif
526 struct entry* dc = tc.dircache;
527 struct entry* e = &dc[tc.selected_item];
528 snprintf(buffer, buffer_len, "%s/%s", getcwd(NULL,0),
529 tc.dirlength ? e->name : "");
532 /* Allow apps to change our dirfilter directly (required for sub browsers)
533 if they're suddenly going to become a file browser for example */
534 void set_dirfilter(int l_dirfilter)
536 *tc.dirfilter = l_dirfilter;
539 /* Selects a file and update tree context properly */
540 void set_current_file(char *path)
542 char *name;
543 int i;
545 #ifdef HAVE_TAGCACHE
546 /* in ID3DB mode it is a bad idea to call this function */
547 /* (only happens with `follow playlist') */
548 if( *tc.dirfilter == SHOW_ID3DB )
549 return;
550 #endif
552 /* separate directory from filename */
553 /* gets the directory's name and put it into tc.currdir */
554 name = strrchr(path+1,'/');
555 if (name)
557 *name = 0;
558 strcpy(tc.currdir, path);
559 *name = '/';
560 name++;
562 else
564 strcpy(tc.currdir, "/");
565 name = path+1;
568 strcpy(lastfile, name);
571 /* If we changed dir we must recalculate the dirlevel
572 and adjust the selected history properly */
573 if (strncmp(tc.currdir,lastdir,sizeof(lastdir)))
575 tc.dirlevel = 0;
576 tc.selected_item_history[tc.dirlevel] = -1;
578 /* use '/' to calculate dirlevel */
579 for (i = 1; path[i] != '\0'; i++)
581 if (path[i] == '/')
583 tc.dirlevel++;
584 tc.selected_item_history[tc.dirlevel] = -1;
588 if (ft_load(&tc, NULL) >= 0)
590 tc.selected_item = tree_get_file_position(lastfile);
595 /* main loop, handles key events */
596 static int dirbrowse()
598 int numentries=0;
599 char buf[MAX_PATH];
600 int button, oldbutton;
601 bool reload_root = false;
602 int lastfilter = *tc.dirfilter;
603 bool lastsortcase = global_settings.sort_case;
604 bool exit_func = false;
606 char* currdir = tc.currdir; /* just a shortcut */
607 #ifdef HAVE_TAGCACHE
608 bool id3db = *tc.dirfilter == SHOW_ID3DB;
610 if (id3db)
611 curr_context=CONTEXT_ID3DB;
612 else
613 #endif
614 curr_context=CONTEXT_TREE;
615 if (tc.selected_item < 0)
616 tc.selected_item = 0;
617 #ifdef HAVE_TAGCACHE
618 tc.firstpos = 0;
619 lasttable = -1;
620 lastextra = -1;
621 lastfirstpos = 0;
622 #endif
624 start_wps = false;
625 numentries = update_dir();
626 reload_dir = false;
627 if (numentries == -1)
628 return GO_TO_PREVIOUS; /* currdir is not a directory */
630 if (*tc.dirfilter > NUM_FILTER_MODES && numentries==0)
632 splash(HZ*2, ID2P(LANG_NO_FILES));
633 return GO_TO_PREVIOUS; /* No files found for rockbox_browser() */
636 while(1) {
637 struct entry *dircache = tc.dircache;
638 bool restore = false;
639 if (tc.dirlevel < 0)
640 tc.dirlevel = 0; /* shouldnt be needed.. this code needs work! */
641 #ifdef BOOTFILE
642 if (boot_changed) {
643 static const char *lines[]={ID2P(LANG_BOOT_CHANGED), ID2P(LANG_REBOOT_NOW)};
644 static const struct text_message message={lines, 2};
645 if(gui_syncyesno_run(&message, NULL, NULL)==YESNO_YES)
646 rolo_load("/" BOOTFILE);
647 restore = true;
648 boot_changed = false;
650 #endif
651 button = get_action(CONTEXT_TREE,
652 list_do_action_timeout(&tree_lists, HZ/2));
653 oldbutton = button;
654 gui_synclist_do_button(&tree_lists, &button,LIST_WRAP_UNLESS_HELD);
655 tc.selected_item = gui_synclist_get_sel_pos(&tree_lists);
656 switch ( button ) {
657 case ACTION_STD_OK:
658 /* nothing to do if no files to display */
659 if ( numentries == 0 )
660 break;
662 #ifdef HAVE_TAGCACHE
663 switch (id3db?tagtree_enter(&tc):ft_enter(&tc))
664 #else
665 switch (ft_enter(&tc))
666 #endif
668 case 1: reload_dir = true; break;
669 case 2: start_wps = true; break;
670 case 3: exit_func = true; break;
671 default: break;
673 restore = true;
674 break;
676 case ACTION_STD_CANCEL:
677 if (*tc.dirfilter > NUM_FILTER_MODES && tc.dirlevel < 1) {
678 exit_func = true;
679 break;
681 if ((*tc.dirfilter == SHOW_ID3DB && tc.dirlevel == 0) ||
682 ((*tc.dirfilter != SHOW_ID3DB && !strcmp(currdir,"/"))))
684 #ifdef HAVE_LCD_BITMAP /* charcell doesnt have ACTION_TREE_PGLEFT so this isnt needed */
685 if (oldbutton == ACTION_TREE_PGLEFT)
686 break;
687 else
688 #endif
689 return GO_TO_ROOT;
692 #ifdef HAVE_TAGCACHE
693 if (id3db)
694 tagtree_exit(&tc);
695 else
696 #endif
697 if (ft_exit(&tc) == 3)
698 exit_func = true;
700 restore = true;
701 break;
703 case ACTION_TREE_STOP:
704 if (list_stop_handler())
705 restore = true;
706 break;
708 case ACTION_STD_MENU:
709 return GO_TO_ROOT;
710 break;
712 #ifdef HAVE_RECORDING
713 case ACTION_STD_REC:
714 return GO_TO_RECSCREEN;
715 #endif
717 case ACTION_TREE_WPS:
718 return GO_TO_PREVIOUS_MUSIC;
719 break;
720 #ifdef HAVE_QUICKSCREEN
721 case ACTION_STD_QUICKSCREEN:
722 /* don't enter f2 from plugin browser */
723 if (*tc.dirfilter < NUM_FILTER_MODES)
725 if (quick_screen_quick(button))
726 reload_dir = true;
727 restore = true;
729 break;
730 #endif
731 #ifdef BUTTON_F3
732 case ACTION_F3:
733 /* don't enter f3 from plugin browser */
734 if (*tc.dirfilter < NUM_FILTER_MODES)
736 if (quick_screen_f3(ACTION_F3))
737 reload_dir = true;
738 restore = true;
740 break;
741 #endif
743 case ACTION_STD_CONTEXT:
745 int onplay_result;
746 int attr = 0;
748 if(!numentries)
749 onplay_result = onplay(NULL, 0, curr_context);
750 else {
751 #ifdef HAVE_TAGCACHE
752 if (id3db)
754 if (tagtree_get_attr(&tc) == FILE_ATTR_AUDIO)
756 attr = FILE_ATTR_AUDIO;
757 tagtree_get_filename(&tc, buf, sizeof(buf));
759 else
760 attr = ATTR_DIRECTORY;
762 else
763 #endif
765 attr = dircache[tc.selected_item].attr;
767 if (currdir[1]) /* Not in / */
768 snprintf(buf, sizeof buf, "%s/%s",
769 currdir,
770 dircache[tc.selected_item].name);
771 else /* In / */
772 snprintf(buf, sizeof buf, "/%s",
773 dircache[tc.selected_item].name);
775 onplay_result = onplay(buf, attr, curr_context);
777 switch (onplay_result)
779 case ONPLAY_MAINMENU:
780 return GO_TO_ROOT;
782 case ONPLAY_OK:
783 restore = true;
784 break;
786 case ONPLAY_RELOAD_DIR:
787 reload_dir = true;
788 break;
790 case ONPLAY_START_PLAY:
791 return GO_TO_WPS;
792 break;
794 break;
797 #ifdef HAVE_HOTSWAP
798 case SYS_FS_CHANGED:
799 #ifdef HAVE_TAGCACHE
800 if (!id3db)
801 #endif
802 reload_dir = true;
803 /* The 'dir no longer valid' situation will be caught later
804 * by checking the showdir() result. */
805 break;
806 #endif
808 default:
809 if (default_event_handler(button) == SYS_USB_CONNECTED)
811 if(*tc.dirfilter > NUM_FILTER_MODES)
812 /* leave sub-browsers after usb, doing otherwise
813 might be confusing to the user */
814 exit_func = true;
815 else
816 reload_dir = true;
818 break;
820 if (start_wps)
821 return GO_TO_WPS;
822 if (button && !IS_SYSEVENT(button))
824 storage_spin();
828 check_rescan:
829 /* do we need to rescan dir? */
830 if (reload_dir || reload_root ||
831 lastfilter != *tc.dirfilter ||
832 lastsortcase != global_settings.sort_case)
834 if (reload_root) {
835 strcpy(currdir, "/");
836 tc.dirlevel = 0;
837 #ifdef HAVE_TAGCACHE
838 tc.currtable = 0;
839 tc.currextra = 0;
840 lasttable = -1;
841 lastextra = -1;
842 #endif
843 reload_root = false;
846 if (!reload_dir)
848 gui_synclist_select_item(&tree_lists, 0);
849 gui_synclist_draw(&tree_lists);
850 tc.selected_item = 0;
851 lastdir[0] = 0;
854 lastfilter = *tc.dirfilter;
855 lastsortcase = global_settings.sort_case;
856 restore = true;
859 if (exit_func)
860 return GO_TO_PREVIOUS;
862 if (restore || reload_dir) {
863 /* restore display */
864 numentries = update_dir();
865 reload_dir = false;
866 if (currdir[1] && (numentries < 0))
867 { /* not in root and reload failed */
868 reload_root = true; /* try root */
869 goto check_rescan;
873 return true;
876 bool create_playlist(void)
878 char filename[MAX_PATH];
880 snprintf(filename, sizeof filename, "%s.m3u8",
881 tc.currdir[1] ? tc.currdir : "/root");
882 splashf(0, "%s %s", str(LANG_CREATING), filename);
884 trigger_cpu_boost();
885 catalog_add_to_a_playlist(tc.currdir, ATTR_DIRECTORY, true, filename);
886 cancel_cpu_boost();
888 return true;
891 int rockbox_browse(const char *root, int dirfilter)
893 int ret_val = 0;
894 int *last_filter = tc.dirfilter;
895 tc.dirfilter = &dirfilter;
896 tc.sort_dir = global_settings.sort_dir;
898 reload_dir = true;
899 if (dirfilter >= NUM_FILTER_MODES)
901 static struct tree_context backup;
902 int last_context;
904 backup = tc;
905 tc.selected_item = 0;
906 tc.dirlevel = 0;
907 memcpy(tc.currdir, root, sizeof(tc.currdir));
908 start_wps = false;
909 last_context = curr_context;
911 ret_val = dirbrowse();
912 tc = backup;
913 curr_context = last_context;
915 else
917 static char buf[MAX_PATH];
918 if (dirfilter != SHOW_ID3DB)
919 tc.dirfilter = &global_settings.dirfilter;
920 strcpy(buf,root);
921 set_current_file(buf);
922 ret_val = dirbrowse();
924 tc.dirfilter = last_filter;
925 return ret_val;
928 void tree_mem_init(void)
930 /* We copy the settings value in case it is changed by the user. We can't
931 use it until the next reboot. */
932 max_files = global_settings.max_files_in_dir;
934 /* initialize tree context struct */
935 memset(&tc, 0, sizeof(tc));
936 tc.dirfilter = &global_settings.dirfilter;
937 tc.sort_dir = global_settings.sort_dir;
939 tc.name_buffer_size = AVERAGE_FILENAME_LENGTH * max_files;
940 tc.name_buffer = buffer_alloc(tc.name_buffer_size);
942 tc.dircache_size = max_files * sizeof(struct entry);
943 tc.dircache = buffer_alloc(tc.dircache_size);
944 tree_get_filetypes(&filetypes, &filetypes_count);
947 bool bookmark_play(char *resume_file, int index, int offset, int seed,
948 char *filename)
950 int i;
951 char* suffix = strrchr(resume_file, '.');
952 bool started = false;
954 if (suffix != NULL &&
955 (!strcasecmp(suffix, ".m3u") || !strcasecmp(suffix, ".m3u8")))
957 /* Playlist playback */
958 char* slash;
959 /* check that the file exists */
960 if(!file_exists(resume_file))
961 return false;
963 slash = strrchr(resume_file,'/');
964 if (slash)
966 char* cp;
967 *slash=0;
969 cp=resume_file;
970 if (!cp[0])
971 cp="/";
973 if (playlist_create(cp, slash+1) != -1)
975 if (global_settings.playlist_shuffle)
976 playlist_shuffle(seed, -1);
977 playlist_start(index,offset);
978 started = true;
980 *slash='/';
983 else
985 /* Directory playback */
986 lastdir[0]='\0';
987 if (playlist_create(resume_file, NULL) != -1)
989 char* peek_filename;
990 resume_directory(resume_file);
991 if (global_settings.playlist_shuffle)
992 playlist_shuffle(seed, -1);
994 /* Check if the file is at the same spot in the directory,
995 else search for it */
996 peek_filename = playlist_peek(index);
998 if (peek_filename == NULL)
999 return false;
1001 if (strcmp(strrchr(peek_filename, '/') + 1, filename))
1003 for ( i=0; i < playlist_amount(); i++ )
1005 peek_filename = playlist_peek(i);
1007 if (peek_filename == NULL)
1008 return false;
1010 if (!strcmp(strrchr(peek_filename, '/') + 1, filename))
1011 break;
1013 if (i < playlist_amount())
1014 index = i;
1015 else
1016 return false;
1018 playlist_start(index,offset);
1019 started = true;
1023 if (started)
1024 start_wps = true;
1025 return started;
1028 static void say_filetype(int attr)
1030 /* try to find a voice ID for the extension, if known */
1031 int j;
1032 attr &= FILE_ATTR_MASK; /* file type */
1033 for (j=0; j<filetypes_count; j++)
1034 if (attr == filetypes[j].tree_attr)
1036 talk_id(filetypes[j].voiceclip, true);
1037 return;
1041 static int ft_play_dirname(char* name)
1043 #if CONFIG_CODEC != SWCODEC
1044 if (audio_status() & AUDIO_STATUS_PLAY)
1045 return 0;
1046 #endif
1048 if(talk_file(tc.currdir, name, dir_thumbnail_name, NULL,
1049 NULL, false))
1051 if(global_settings.talk_filetype)
1052 talk_id(VOICE_DIR, true);
1053 return 1;
1055 else
1056 return -1;
1059 static void ft_play_filename(char *dir, char *file)
1061 #if CONFIG_CODEC != SWCODEC
1062 if (audio_status() & AUDIO_STATUS_PLAY)
1063 return;
1064 #endif
1066 if (strlen(file) >= strlen(file_thumbnail_ext)
1067 && strcasecmp(&file[strlen(file) - strlen(file_thumbnail_ext)],
1068 file_thumbnail_ext))
1069 /* file has no .talk extension */
1070 talk_file(dir, NULL, file, file_thumbnail_ext,
1071 NULL, false);
1072 else
1073 /* it already is a .talk file, play this directly, but prefix it. */
1074 talk_file(dir, NULL, file, NULL,
1075 TALK_IDARRAY(LANG_VOICE_DIR_HOVER), false);
1078 /* These two functions are called by the USB and shutdown handlers */
1079 void tree_flush(void)
1081 #ifdef HAVE_TAGCACHE
1082 tagcache_shutdown();
1083 #endif
1085 #ifdef HAVE_TC_RAMCACHE
1086 tagcache_unload_ramcache();
1087 #endif
1089 #ifdef HAVE_DIRCACHE
1091 int old_val = global_status.dircache_size;
1092 if (global_settings.dircache)
1094 if (!dircache_is_initializing())
1095 global_status.dircache_size = dircache_get_cache_size();
1096 # ifdef HAVE_EEPROM_SETTINGS
1097 if (firmware_settings.initialized)
1098 dircache_save();
1099 # endif
1100 dircache_disable();
1102 else
1104 global_status.dircache_size = 0;
1106 if (old_val != global_status.dircache_size)
1107 status_save();
1109 #endif
1112 void tree_restore(void)
1114 #ifdef HAVE_EEPROM_SETTINGS
1115 firmware_settings.disk_clean = false;
1116 #endif
1118 #ifdef HAVE_TC_RAMCACHE
1119 remove(TAGCACHE_STATEFILE);
1120 #endif
1122 #ifdef HAVE_DIRCACHE
1123 remove(DIRCACHE_FILE);
1124 if (global_settings.dircache)
1126 /* Print "Scanning disk..." to the display. */
1127 splash(0, str(LANG_SCANNING_DISK));
1129 dircache_build(global_status.dircache_size);
1131 #endif
1132 #ifdef HAVE_TAGCACHE
1133 tagcache_start_scan();
1134 #endif