rbutil: store the voice language in the correct setting.
[kugel-rb.git] / apps / tree.c
blob35707987060e96acc789478978c275c5bde6bd72
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 "skin_engine/skin_engine.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 "eeprom_settings.h"
68 #include "playlist_catalog.h"
70 /* gui api */
71 #include "list.h"
72 #include "splash.h"
73 #include "buttonbar.h"
74 #include "quickscreen.h"
75 #include "appevents.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 const 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 enum themable_icons 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
305 /* drawer function for the GUI_EVENT_REDRAW callback */
306 void tree_drawlists(void)
308 gui_synclist_draw(&tree_lists);
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 int dirfilter = *tc.dirfilter;
481 int ret;
482 #ifdef HAVE_TAGCACHE
483 bool id3db = *tc.dirfilter == SHOW_ID3DB;
484 #endif
485 /* make sure the dirfilter is sane. The only time it should be possible
486 * thats its not is when resume playlist is called from a plugin
488 #ifdef HAVE_TAGCACHE
489 if (!id3db)
490 #endif
491 *tc.dirfilter = global_settings.dirfilter;
492 ret = ft_load(&tc, dir);
493 *tc.dirfilter = dirfilter;
494 if (ret < 0)
495 return;
496 lastdir[0] = 0;
498 ft_build_playlist(&tc, 0);
500 #ifdef HAVE_TAGCACHE
501 if (id3db)
502 tagtree_load(&tc);
503 #endif
506 /* Returns the current working directory and also writes cwd to buf if
507 non-NULL. In case of error, returns NULL. */
508 char *getcwd(char *buf, int size)
510 if (!buf)
511 return tc.currdir;
512 else if (size > 0)
514 strlcpy(buf, tc.currdir, size);
515 return buf;
517 else
518 return NULL;
521 /* Force a reload of the directory next time directory browser is called */
522 void reload_directory(void)
524 reload_dir = true;
527 void get_current_file(char* buffer, int buffer_len)
529 #ifdef HAVE_TAGCACHE
530 /* in ID3DB mode it is a bad idea to call this function */
531 /* (only happens with `follow playlist') */
532 if( *tc.dirfilter == SHOW_ID3DB )
533 return;
534 #endif
536 struct entry* dc = tc.dircache;
537 struct entry* e = &dc[tc.selected_item];
538 snprintf(buffer, buffer_len, "%s/%s", getcwd(NULL,0),
539 tc.dirlength ? e->name : "");
542 /* Allow apps to change our dirfilter directly (required for sub browsers)
543 if they're suddenly going to become a file browser for example */
544 void set_dirfilter(int l_dirfilter)
546 *tc.dirfilter = l_dirfilter;
549 /* Selects a file and update tree context properly */
550 void set_current_file(char *path)
552 char *name;
553 int i;
555 #ifdef HAVE_TAGCACHE
556 /* in ID3DB mode it is a bad idea to call this function */
557 /* (only happens with `follow playlist') */
558 if( *tc.dirfilter == SHOW_ID3DB )
559 return;
560 #endif
562 /* separate directory from filename */
563 /* gets the directory's name and put it into tc.currdir */
564 name = strrchr(path+1,'/');
565 if (name)
567 *name = 0;
568 strcpy(tc.currdir, path);
569 *name = '/';
570 name++;
572 else
574 strcpy(tc.currdir, "/");
575 name = path+1;
578 strcpy(lastfile, name);
581 /* If we changed dir we must recalculate the dirlevel
582 and adjust the selected history properly */
583 if (strncmp(tc.currdir,lastdir,sizeof(lastdir)))
585 tc.dirlevel = 0;
586 tc.selected_item_history[tc.dirlevel] = -1;
588 /* use '/' to calculate dirlevel */
589 for (i = 1; path[i] != '\0'; i++)
591 if (path[i] == '/')
593 tc.dirlevel++;
594 tc.selected_item_history[tc.dirlevel] = -1;
598 if (ft_load(&tc, NULL) >= 0)
600 tc.selected_item = tree_get_file_position(lastfile);
605 /* main loop, handles key events */
606 static int dirbrowse()
608 int numentries=0;
609 char buf[MAX_PATH];
610 int button, oldbutton;
611 bool reload_root = false;
612 int lastfilter = *tc.dirfilter;
613 bool lastsortcase = global_settings.sort_case;
614 bool exit_func = false;
616 char* currdir = tc.currdir; /* just a shortcut */
617 #ifdef HAVE_TAGCACHE
618 bool id3db = *tc.dirfilter == SHOW_ID3DB;
620 if (id3db)
621 curr_context=CONTEXT_ID3DB;
622 else
623 #endif
624 curr_context=CONTEXT_TREE;
625 if (tc.selected_item < 0)
626 tc.selected_item = 0;
627 #ifdef HAVE_TAGCACHE
628 tc.firstpos = 0;
629 lasttable = -1;
630 lastextra = -1;
631 lastfirstpos = 0;
632 #endif
634 start_wps = false;
635 numentries = update_dir();
636 reload_dir = false;
637 if (numentries == -1)
638 return GO_TO_PREVIOUS; /* currdir is not a directory */
640 if (*tc.dirfilter > NUM_FILTER_MODES && numentries==0)
642 splash(HZ*2, ID2P(LANG_NO_FILES));
643 return GO_TO_PREVIOUS; /* No files found for rockbox_browser() */
646 gui_synclist_draw(&tree_lists);
647 while(1) {
648 struct entry *dircache = tc.dircache;
649 bool restore = false;
650 if (tc.dirlevel < 0)
651 tc.dirlevel = 0; /* shouldnt be needed.. this code needs work! */
652 #ifdef BOOTFILE
653 if (boot_changed) {
654 static const char *lines[]={ID2P(LANG_BOOT_CHANGED), ID2P(LANG_REBOOT_NOW)};
655 static const struct text_message message={lines, 2};
656 if(gui_syncyesno_run(&message, NULL, NULL)==YESNO_YES)
657 rolo_load("/" BOOTFILE);
658 restore = true;
659 boot_changed = false;
661 #endif
662 button = get_action(CONTEXT_TREE,
663 list_do_action_timeout(&tree_lists, HZ/2));
664 oldbutton = button;
665 gui_synclist_do_button(&tree_lists, &button,LIST_WRAP_UNLESS_HELD);
666 tc.selected_item = gui_synclist_get_sel_pos(&tree_lists);
667 switch ( button ) {
668 case ACTION_STD_OK:
669 /* nothing to do if no files to display */
670 if ( numentries == 0 )
671 break;
673 #ifdef HAVE_TAGCACHE
674 switch (id3db?tagtree_enter(&tc):ft_enter(&tc))
675 #else
676 switch (ft_enter(&tc))
677 #endif
679 case 1: reload_dir = true; break;
680 case 2: start_wps = true; break;
681 case 3: exit_func = true; break;
682 default: break;
684 restore = true;
685 break;
687 case ACTION_STD_CANCEL:
688 if (*tc.dirfilter > NUM_FILTER_MODES && tc.dirlevel < 1) {
689 exit_func = true;
690 break;
692 if ((*tc.dirfilter == SHOW_ID3DB && tc.dirlevel == 0) ||
693 ((*tc.dirfilter != SHOW_ID3DB && !strcmp(currdir,"/"))))
695 #ifdef HAVE_LCD_BITMAP /* charcell doesnt have ACTION_TREE_PGLEFT so this isnt needed */
696 if (oldbutton == ACTION_TREE_PGLEFT)
697 break;
698 else
699 #endif
700 return GO_TO_ROOT;
703 #ifdef HAVE_TAGCACHE
704 if (id3db)
705 tagtree_exit(&tc);
706 else
707 #endif
708 if (ft_exit(&tc) == 3)
709 exit_func = true;
711 restore = true;
712 break;
714 case ACTION_TREE_STOP:
715 if (list_stop_handler())
716 restore = true;
717 break;
719 case ACTION_STD_MENU:
720 return GO_TO_ROOT;
721 break;
723 #ifdef HAVE_RECORDING
724 case ACTION_STD_REC:
725 return GO_TO_RECSCREEN;
726 #endif
728 case ACTION_TREE_WPS:
729 return GO_TO_PREVIOUS_MUSIC;
730 break;
731 #ifdef HAVE_QUICKSCREEN
732 case ACTION_STD_QUICKSCREEN:
733 /* don't enter f2 from plugin browser */
734 if (*tc.dirfilter < NUM_FILTER_MODES)
736 if (quick_screen_quick(button))
737 reload_dir = true;
738 restore = true;
740 break;
741 #endif
742 #ifdef BUTTON_F3
743 case ACTION_F3:
744 /* don't enter f3 from plugin browser */
745 if (*tc.dirfilter < NUM_FILTER_MODES)
747 if (quick_screen_f3(ACTION_F3))
748 reload_dir = true;
749 restore = true;
751 break;
752 #endif
754 case ACTION_STD_CONTEXT:
756 int onplay_result;
757 int attr = 0;
759 if(!numentries)
760 onplay_result = onplay(NULL, 0, curr_context);
761 else {
762 #ifdef HAVE_TAGCACHE
763 if (id3db)
765 if (tagtree_get_attr(&tc) == FILE_ATTR_AUDIO)
767 attr = FILE_ATTR_AUDIO;
768 tagtree_get_filename(&tc, buf, sizeof(buf));
770 else
771 attr = ATTR_DIRECTORY;
773 else
774 #endif
776 attr = dircache[tc.selected_item].attr;
778 if (currdir[1]) /* Not in / */
779 snprintf(buf, sizeof buf, "%s/%s",
780 currdir,
781 dircache[tc.selected_item].name);
782 else /* In / */
783 snprintf(buf, sizeof buf, "/%s",
784 dircache[tc.selected_item].name);
786 onplay_result = onplay(buf, attr, curr_context);
788 send_event(GUI_EVENT_REFRESH, tree_drawlists);
789 switch (onplay_result)
791 case ONPLAY_MAINMENU:
792 return GO_TO_ROOT;
794 case ONPLAY_OK:
795 restore = true;
796 break;
798 case ONPLAY_RELOAD_DIR:
799 reload_dir = true;
800 break;
802 case ONPLAY_START_PLAY:
803 return GO_TO_WPS;
804 break;
806 break;
809 #ifdef HAVE_HOTSWAP
810 case SYS_FS_CHANGED:
811 #ifdef HAVE_TAGCACHE
812 if (!id3db)
813 #endif
814 reload_dir = true;
815 /* The 'dir no longer valid' situation will be caught later
816 * by checking the showdir() result. */
817 break;
818 #endif
820 default:
821 if (default_event_handler(button) == SYS_USB_CONNECTED)
823 if(*tc.dirfilter > NUM_FILTER_MODES)
824 /* leave sub-browsers after usb, doing otherwise
825 might be confusing to the user */
826 exit_func = true;
827 else
828 reload_dir = true;
830 break;
832 if (start_wps)
833 return GO_TO_WPS;
834 if (button && !IS_SYSEVENT(button))
836 storage_spin();
840 check_rescan:
841 /* do we need to rescan dir? */
842 if (reload_dir || reload_root ||
843 lastfilter != *tc.dirfilter ||
844 lastsortcase != global_settings.sort_case)
846 if (reload_root) {
847 strcpy(currdir, "/");
848 tc.dirlevel = 0;
849 #ifdef HAVE_TAGCACHE
850 tc.currtable = 0;
851 tc.currextra = 0;
852 lasttable = -1;
853 lastextra = -1;
854 #endif
855 reload_root = false;
858 if (!reload_dir)
860 gui_synclist_select_item(&tree_lists, 0);
861 gui_synclist_draw(&tree_lists);
862 tc.selected_item = 0;
863 lastdir[0] = 0;
866 lastfilter = *tc.dirfilter;
867 lastsortcase = global_settings.sort_case;
868 restore = true;
871 if (exit_func)
872 return GO_TO_PREVIOUS;
874 if (restore || reload_dir) {
875 /* restore display */
876 numentries = update_dir();
877 reload_dir = false;
878 if (currdir[1] && (numentries < 0))
879 { /* not in root and reload failed */
880 reload_root = true; /* try root */
881 goto check_rescan;
885 return true;
888 bool create_playlist(void)
890 char filename[MAX_PATH];
892 snprintf(filename, sizeof filename, "%s.m3u8",
893 tc.currdir[1] ? tc.currdir : "/root");
894 splashf(0, "%s %s", str(LANG_CREATING), filename);
896 trigger_cpu_boost();
897 catalog_add_to_a_playlist(tc.currdir, ATTR_DIRECTORY, true, filename);
898 cancel_cpu_boost();
900 return true;
903 int rockbox_browse(const char *root, int dirfilter)
905 int ret_val = 0;
906 int *last_filter = tc.dirfilter;
907 tc.dirfilter = &dirfilter;
908 tc.sort_dir = global_settings.sort_dir;
910 reload_dir = true;
911 if (dirfilter >= NUM_FILTER_MODES)
913 static struct tree_context backup;
914 char current[MAX_PATH];
915 int last_context;
917 backup = tc;
918 tc.selected_item = 0;
919 tc.dirlevel = 0;
920 memcpy(tc.currdir, root, sizeof(tc.currdir));
921 start_wps = false;
922 last_context = curr_context;
924 /* Center on the currently loaded language when browsing languages. */
925 if (dirfilter == SHOW_LNG)
927 if (global_settings.lang_file[0])
929 snprintf(current, sizeof(current), LANG_DIR "/%s.lng",
930 global_settings.lang_file);
932 else
934 strlcpy(current, LANG_DIR "/english.lng", sizeof(current));
937 /* Center on currently loaded WPS */
938 else if (dirfilter == SHOW_WPS)
940 snprintf(current, sizeof(current), WPS_DIR "/%s.wps",
941 global_settings.wps_file);
943 #ifdef HAVE_REMOTE_LCD
944 /* Center on currently loaded RWPS */
945 else if (dirfilter == SHOW_RWPS)
947 snprintf(current, sizeof(current), WPS_DIR "/%s.rwps",
948 global_settings.rwps_file);
950 #endif
951 #ifdef HAVE_LCD_BITMAP
952 /* Center on the currently loaded font when browsing fonts */
953 else if (dirfilter == SHOW_FONT)
955 snprintf(current, sizeof(current), FONT_DIR "/%s.fnt",
956 global_settings.font_file);
958 #endif
959 #if CONFIG_TUNER
960 /* Center on the currently loaded FM preset when browsing those */
961 else if (dirfilter == SHOW_FMR)
963 snprintf(current, sizeof(current), FMPRESET_PATH "/%s.fmr",
964 global_settings.fmr_file);
966 #endif
967 else /* reset current[] */
968 current[0] = '\0';
970 /* If we've found a file to center on, do it */
971 if (current[0] == '/')
973 set_current_file(current);
974 /* set_current_file changes dirlevel, change it back */
975 tc.dirlevel = 0;
978 ret_val = dirbrowse();
979 tc = backup;
980 curr_context = last_context;
982 else
984 static char buf[MAX_PATH];
985 if (dirfilter != SHOW_ID3DB)
986 tc.dirfilter = &global_settings.dirfilter;
987 strcpy(buf,root);
988 set_current_file(buf);
989 ret_val = dirbrowse();
991 tc.dirfilter = last_filter;
992 return ret_val;
995 void tree_mem_init(void)
997 /* We copy the settings value in case it is changed by the user. We can't
998 use it until the next reboot. */
999 max_files = global_settings.max_files_in_dir;
1001 /* initialize tree context struct */
1002 memset(&tc, 0, sizeof(tc));
1003 tc.dirfilter = &global_settings.dirfilter;
1004 tc.sort_dir = global_settings.sort_dir;
1006 tc.name_buffer_size = AVERAGE_FILENAME_LENGTH * max_files;
1007 tc.name_buffer = buffer_alloc(tc.name_buffer_size);
1009 tc.dircache_size = max_files * sizeof(struct entry);
1010 tc.dircache = buffer_alloc(tc.dircache_size);
1011 tree_get_filetypes(&filetypes, &filetypes_count);
1014 bool bookmark_play(char *resume_file, int index, int offset, int seed,
1015 char *filename)
1017 int i;
1018 char* suffix = strrchr(resume_file, '.');
1019 bool started = false;
1021 if (suffix != NULL &&
1022 (!strcasecmp(suffix, ".m3u") || !strcasecmp(suffix, ".m3u8")))
1024 /* Playlist playback */
1025 char* slash;
1026 /* check that the file exists */
1027 if(!file_exists(resume_file))
1028 return false;
1030 slash = strrchr(resume_file,'/');
1031 if (slash)
1033 char* cp;
1034 *slash=0;
1036 cp=resume_file;
1037 if (!cp[0])
1038 cp="/";
1040 if (playlist_create(cp, slash+1) != -1)
1042 if (global_settings.playlist_shuffle)
1043 playlist_shuffle(seed, -1);
1044 playlist_start(index,offset);
1045 started = true;
1047 *slash='/';
1050 else
1052 /* Directory playback */
1053 lastdir[0]='\0';
1054 if (playlist_create(resume_file, NULL) != -1)
1056 char* peek_filename;
1057 resume_directory(resume_file);
1058 if (global_settings.playlist_shuffle)
1059 playlist_shuffle(seed, -1);
1061 /* Check if the file is at the same spot in the directory,
1062 else search for it */
1063 peek_filename = playlist_peek(index);
1065 if (peek_filename == NULL)
1066 return false;
1068 if (strcmp(strrchr(peek_filename, '/') + 1, filename))
1070 for ( i=0; i < playlist_amount(); i++ )
1072 peek_filename = playlist_peek(i);
1074 if (peek_filename == NULL)
1075 return false;
1077 if (!strcmp(strrchr(peek_filename, '/') + 1, filename))
1078 break;
1080 if (i < playlist_amount())
1081 index = i;
1082 else
1083 return false;
1085 playlist_start(index,offset);
1086 started = true;
1090 if (started)
1091 start_wps = true;
1092 return started;
1095 static void say_filetype(int attr)
1097 /* try to find a voice ID for the extension, if known */
1098 int j;
1099 attr &= FILE_ATTR_MASK; /* file type */
1100 for (j=0; j<filetypes_count; j++)
1101 if (attr == filetypes[j].tree_attr)
1103 talk_id(filetypes[j].voiceclip, true);
1104 return;
1108 static int ft_play_dirname(char* name)
1110 #if CONFIG_CODEC != SWCODEC
1111 if (audio_status() & AUDIO_STATUS_PLAY)
1112 return 0;
1113 #endif
1115 if(talk_file(tc.currdir, name, dir_thumbnail_name, NULL,
1116 NULL, false))
1118 if(global_settings.talk_filetype)
1119 talk_id(VOICE_DIR, true);
1120 return 1;
1122 else
1123 return -1;
1126 static void ft_play_filename(char *dir, char *file)
1128 #if CONFIG_CODEC != SWCODEC
1129 if (audio_status() & AUDIO_STATUS_PLAY)
1130 return;
1131 #endif
1133 if (strlen(file) >= strlen(file_thumbnail_ext)
1134 && strcasecmp(&file[strlen(file) - strlen(file_thumbnail_ext)],
1135 file_thumbnail_ext))
1136 /* file has no .talk extension */
1137 talk_file(dir, NULL, file, file_thumbnail_ext,
1138 NULL, false);
1139 else
1140 /* it already is a .talk file, play this directly, but prefix it. */
1141 talk_file(dir, NULL, file, NULL,
1142 TALK_IDARRAY(LANG_VOICE_DIR_HOVER), false);
1145 /* These two functions are called by the USB and shutdown handlers */
1146 void tree_flush(void)
1148 #ifdef HAVE_TAGCACHE
1149 tagcache_shutdown();
1150 #endif
1152 #ifdef HAVE_TC_RAMCACHE
1153 tagcache_unload_ramcache();
1154 #endif
1156 #ifdef HAVE_DIRCACHE
1158 int old_val = global_status.dircache_size;
1159 if (global_settings.dircache)
1161 if (!dircache_is_initializing())
1162 global_status.dircache_size = dircache_get_cache_size();
1163 # ifdef HAVE_EEPROM_SETTINGS
1164 if (firmware_settings.initialized)
1165 dircache_save();
1166 # endif
1167 dircache_disable();
1169 else
1171 global_status.dircache_size = 0;
1173 if (old_val != global_status.dircache_size)
1174 status_save();
1176 #endif
1179 void tree_restore(void)
1181 #ifdef HAVE_EEPROM_SETTINGS
1182 firmware_settings.disk_clean = false;
1183 #endif
1185 #ifdef HAVE_TC_RAMCACHE
1186 remove(TAGCACHE_STATEFILE);
1187 #endif
1189 #ifdef HAVE_DIRCACHE
1190 remove(DIRCACHE_FILE);
1191 if (global_settings.dircache)
1193 /* Print "Scanning disk..." to the display. */
1194 splash(0, str(LANG_SCANNING_DISK));
1196 dircache_build(global_status.dircache_size);
1198 #endif
1199 #ifdef HAVE_TAGCACHE
1200 tagcache_start_scan();
1201 #endif