Merge branch 'hotswap'
[kugel-rb.git] / apps / tree.c
blobd932094d4497763ecd2337d33235a1212533973f
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 /* band-aid to fix the bar/list redrawing properly after leaving a plugin */
309 send_event(GUI_EVENT_THEME_CHANGED, NULL);
310 /* end bandaid */
311 gui_synclist_draw(&tree_lists);
315 struct tree_context* tree_get_context(void)
317 return &tc;
321 * Returns the position of a given file in the current directory
322 * returns -1 if not found
324 static int tree_get_file_position(char * filename)
326 int i;
328 /* use lastfile to determine the selected item (default=0) */
329 for (i=0; i < tc.filesindir; i++)
331 struct entry* dc = tc.dircache;
332 struct entry* e = &dc[i];
333 if (!strcasecmp(e->name, filename))
334 return(i);
336 return(-1);/* no file can match, returns undefined */
340 * Called when a new dir is loaded (for example when returning from other apps ...)
341 * also completely redraws the tree
343 static int update_dir(void)
345 bool changed = false;
346 #ifdef HAVE_TAGCACHE
347 bool id3db = *tc.dirfilter == SHOW_ID3DB;
348 /* Checks for changes */
349 if (id3db) {
350 if (tc.currtable != lasttable ||
351 tc.currextra != lastextra ||
352 tc.firstpos != lastfirstpos ||
353 reload_dir)
355 if (tagtree_load(&tc) < 0)
356 return -1;
358 lasttable = tc.currtable;
359 lastextra = tc.currextra;
360 lastfirstpos = tc.firstpos;
361 changed = true;
364 else
365 #endif
367 /* if the tc.currdir has been changed, reload it ...*/
368 if (strncmp(tc.currdir, lastdir, sizeof(lastdir)) || reload_dir)
370 if (ft_load(&tc, NULL) < 0)
371 return -1;
372 strcpy(lastdir, tc.currdir);
373 changed = true;
376 /* if selected item is undefined */
377 if (tc.selected_item == -1)
379 /* use lastfile to determine the selected item */
380 tc.selected_item = tree_get_file_position(lastfile);
382 /* If the file doesn't exists, select the first one (default) */
383 if(tc.selected_item < 0)
384 tc.selected_item = 0;
385 changed = true;
387 if (changed)
390 #ifdef HAVE_TAGCACHE
391 !id3db &&
392 #endif
393 (tc.dirfull || tc.filesindir == global_settings.max_files_in_dir) )
395 splash(HZ, ID2P(LANG_SHOWDIR_BUFFER_FULL));
398 #ifdef HAVE_TAGCACHE
399 if (id3db)
401 #ifdef HAVE_LCD_BITMAP
402 if (global_settings.show_path_in_browser == SHOW_PATH_FULL
403 || global_settings.show_path_in_browser == SHOW_PATH_CURRENT)
405 gui_synclist_set_title(&tree_lists, tagtree_get_title(&tc),
406 filetype_get_icon(ATTR_DIRECTORY));
408 else
410 /* Must clear the title as the list is reused */
411 gui_synclist_set_title(&tree_lists, NULL, NOICON);
413 #endif
415 else
416 #endif
418 #ifdef HAVE_LCD_BITMAP
419 if (global_settings.show_path_in_browser &&
420 *(tc.dirfilter) == SHOW_PLUGINS)
422 char *title;
423 if (!strcmp(tc.currdir, PLUGIN_GAMES_DIR))
424 title = str(LANG_PLUGIN_GAMES);
425 else if (!strcmp(tc.currdir, PLUGIN_APPS_DIR))
426 title = str(LANG_PLUGIN_APPS);
427 else if (!strcmp(tc.currdir, PLUGIN_DEMOS_DIR))
428 title = str(LANG_PLUGIN_DEMOS);
429 else title = str(LANG_PLUGINS);
430 gui_synclist_set_title(&tree_lists, title, Icon_Plugin);
432 else if (global_settings.show_path_in_browser == SHOW_PATH_FULL)
434 gui_synclist_set_title(&tree_lists, tc.currdir,
435 filetype_get_icon(ATTR_DIRECTORY));
437 else if (global_settings.show_path_in_browser == SHOW_PATH_CURRENT)
439 char *title = strrchr(tc.currdir, '/') + 1;
440 if (*title == '\0')
442 /* Display "Files" for the root dir */
443 gui_synclist_set_title(&tree_lists, str(LANG_DIR_BROWSER),
444 filetype_get_icon(ATTR_DIRECTORY));
446 else
447 gui_synclist_set_title(&tree_lists, title,
448 filetype_get_icon(ATTR_DIRECTORY));
450 else
452 /* Must clear the title as the list is reused */
453 gui_synclist_set_title(&tree_lists, NULL, NOICON);
455 #endif
458 gui_synclist_set_nb_items(&tree_lists, tc.filesindir);
459 gui_synclist_set_icon_callback(&tree_lists, tree_get_fileicon);
460 if( tc.selected_item >= tc.filesindir)
461 tc.selected_item=tc.filesindir-1;
463 gui_synclist_select_item(&tree_lists, tc.selected_item);
464 #ifdef HAVE_BUTTONBAR
465 if (global_settings.buttonbar) {
466 if (*tc.dirfilter < NUM_FILTER_MODES)
467 gui_buttonbar_set(&tree_buttonbar, str(LANG_SYSFONT_DIRBROWSE_F1),
468 str(LANG_SYSFONT_DIRBROWSE_F2),
469 str(LANG_SYSFONT_DIRBROWSE_F3));
470 else
471 gui_buttonbar_set(&tree_buttonbar, "<<<", "", "");
472 gui_buttonbar_draw(&tree_buttonbar);
474 #endif
475 gui_synclist_draw(&tree_lists);
476 gui_synclist_speak_item(&tree_lists);
477 return tc.filesindir;
480 /* load tracks from specified directory to resume play */
481 void resume_directory(const char *dir)
483 int dirfilter = *tc.dirfilter;
484 int ret;
485 #ifdef HAVE_TAGCACHE
486 bool id3db = *tc.dirfilter == SHOW_ID3DB;
487 #endif
488 /* make sure the dirfilter is sane. The only time it should be possible
489 * thats its not is when resume playlist is called from a plugin
491 #ifdef HAVE_TAGCACHE
492 if (!id3db)
493 #endif
494 *tc.dirfilter = global_settings.dirfilter;
495 ret = ft_load(&tc, dir);
496 *tc.dirfilter = dirfilter;
497 if (ret < 0)
498 return;
499 lastdir[0] = 0;
501 ft_build_playlist(&tc, 0);
503 #ifdef HAVE_TAGCACHE
504 if (id3db)
505 tagtree_load(&tc);
506 #endif
509 /* Returns the current working directory and also writes cwd to buf if
510 non-NULL. In case of error, returns NULL. */
511 char *getcwd(char *buf, int size)
513 if (!buf)
514 return tc.currdir;
515 else if (size > 0)
517 strlcpy(buf, tc.currdir, size);
518 return buf;
520 else
521 return NULL;
524 /* Force a reload of the directory next time directory browser is called */
525 void reload_directory(void)
527 reload_dir = true;
530 void get_current_file(char* buffer, int buffer_len)
532 #ifdef HAVE_TAGCACHE
533 /* in ID3DB mode it is a bad idea to call this function */
534 /* (only happens with `follow playlist') */
535 if( *tc.dirfilter == SHOW_ID3DB )
536 return;
537 #endif
539 struct entry* dc = tc.dircache;
540 struct entry* e = &dc[tc.selected_item];
541 snprintf(buffer, buffer_len, "%s/%s", getcwd(NULL,0),
542 tc.dirlength ? e->name : "");
545 /* Allow apps to change our dirfilter directly (required for sub browsers)
546 if they're suddenly going to become a file browser for example */
547 void set_dirfilter(int l_dirfilter)
549 *tc.dirfilter = l_dirfilter;
552 /* Selects a file and update tree context properly */
553 void set_current_file(char *path)
555 char *name;
556 int i;
558 #ifdef HAVE_TAGCACHE
559 /* in ID3DB mode it is a bad idea to call this function */
560 /* (only happens with `follow playlist') */
561 if( *tc.dirfilter == SHOW_ID3DB )
562 return;
563 #endif
565 /* separate directory from filename */
566 /* gets the directory's name and put it into tc.currdir */
567 name = strrchr(path+1,'/');
568 if (name)
570 *name = 0;
571 strcpy(tc.currdir, path);
572 *name = '/';
573 name++;
575 else
577 strcpy(tc.currdir, "/");
578 name = path+1;
581 strcpy(lastfile, name);
584 /* If we changed dir we must recalculate the dirlevel
585 and adjust the selected history properly */
586 if (strncmp(tc.currdir,lastdir,sizeof(lastdir)))
588 tc.dirlevel = 0;
589 tc.selected_item_history[tc.dirlevel] = -1;
591 /* use '/' to calculate dirlevel */
592 for (i = 1; path[i] != '\0'; i++)
594 if (path[i] == '/')
596 tc.dirlevel++;
597 tc.selected_item_history[tc.dirlevel] = -1;
601 if (ft_load(&tc, NULL) >= 0)
603 tc.selected_item = tree_get_file_position(lastfile);
608 /* main loop, handles key events */
609 static int dirbrowse()
611 int numentries=0;
612 char buf[MAX_PATH];
613 int button, oldbutton;
614 bool reload_root = false;
615 int lastfilter = *tc.dirfilter;
616 bool lastsortcase = global_settings.sort_case;
617 bool exit_func = false;
619 char* currdir = tc.currdir; /* just a shortcut */
620 #ifdef HAVE_TAGCACHE
621 bool id3db = *tc.dirfilter == SHOW_ID3DB;
623 if (id3db)
624 curr_context=CONTEXT_ID3DB;
625 else
626 #endif
627 curr_context=CONTEXT_TREE;
628 if (tc.selected_item < 0)
629 tc.selected_item = 0;
630 #ifdef HAVE_TAGCACHE
631 tc.firstpos = 0;
632 lasttable = -1;
633 lastextra = -1;
634 lastfirstpos = 0;
635 #endif
637 start_wps = false;
638 numentries = update_dir();
639 reload_dir = false;
640 if (numentries == -1)
641 return GO_TO_PREVIOUS; /* currdir is not a directory */
643 if (*tc.dirfilter > NUM_FILTER_MODES && numentries==0)
645 splash(HZ*2, ID2P(LANG_NO_FILES));
646 return GO_TO_PREVIOUS; /* No files found for rockbox_browser() */
649 gui_synclist_draw(&tree_lists);
650 while(1) {
651 struct entry *dircache = tc.dircache;
652 bool restore = false;
653 if (tc.dirlevel < 0)
654 tc.dirlevel = 0; /* shouldnt be needed.. this code needs work! */
655 #ifdef BOOTFILE
656 if (boot_changed) {
657 static const char *lines[]={ID2P(LANG_BOOT_CHANGED), ID2P(LANG_REBOOT_NOW)};
658 static const struct text_message message={lines, 2};
659 if(gui_syncyesno_run(&message, NULL, NULL)==YESNO_YES)
660 rolo_load("/" BOOTFILE);
661 restore = true;
662 boot_changed = false;
664 #endif
665 button = get_action(CONTEXT_TREE,
666 list_do_action_timeout(&tree_lists, HZ/2));
667 oldbutton = button;
668 gui_synclist_do_button(&tree_lists, &button,LIST_WRAP_UNLESS_HELD);
669 tc.selected_item = gui_synclist_get_sel_pos(&tree_lists);
670 switch ( button ) {
671 case ACTION_STD_OK:
672 /* nothing to do if no files to display */
673 if ( numentries == 0 )
674 break;
676 #ifdef HAVE_TAGCACHE
677 switch (id3db?tagtree_enter(&tc):ft_enter(&tc))
678 #else
679 switch (ft_enter(&tc))
680 #endif
682 case 1: reload_dir = true; break;
683 case 2: start_wps = true; break;
684 case 3: exit_func = true; break;
685 default: break;
687 restore = true;
688 break;
690 case ACTION_STD_CANCEL:
691 if (*tc.dirfilter > NUM_FILTER_MODES && tc.dirlevel < 1) {
692 exit_func = true;
693 break;
695 if ((*tc.dirfilter == SHOW_ID3DB && tc.dirlevel == 0) ||
696 ((*tc.dirfilter != SHOW_ID3DB && !strcmp(currdir,"/"))))
698 #ifdef HAVE_LCD_BITMAP /* charcell doesnt have ACTION_TREE_PGLEFT so this isnt needed */
699 if (oldbutton == ACTION_TREE_PGLEFT)
700 break;
701 else
702 #endif
703 return GO_TO_ROOT;
706 #ifdef HAVE_TAGCACHE
707 if (id3db)
708 tagtree_exit(&tc);
709 else
710 #endif
711 if (ft_exit(&tc) == 3)
712 exit_func = true;
714 restore = true;
715 break;
717 case ACTION_TREE_STOP:
718 if (list_stop_handler())
719 restore = true;
720 break;
722 case ACTION_STD_MENU:
723 return GO_TO_ROOT;
724 break;
726 #ifdef HAVE_RECORDING
727 case ACTION_STD_REC:
728 return GO_TO_RECSCREEN;
729 #endif
731 case ACTION_TREE_WPS:
732 return GO_TO_PREVIOUS_MUSIC;
733 break;
734 #ifdef HAVE_QUICKSCREEN
735 case ACTION_STD_QUICKSCREEN:
736 /* don't enter f2 from plugin browser */
737 if (*tc.dirfilter < NUM_FILTER_MODES)
739 if (quick_screen_quick(button))
740 reload_dir = true;
741 restore = true;
743 break;
744 #endif
745 #ifdef BUTTON_F3
746 case ACTION_F3:
747 /* don't enter f3 from plugin browser */
748 if (*tc.dirfilter < NUM_FILTER_MODES)
750 if (quick_screen_f3(ACTION_F3))
751 reload_dir = true;
752 restore = true;
754 break;
755 #endif
757 case ACTION_STD_CONTEXT:
759 int onplay_result;
760 int attr = 0;
762 if(!numentries)
763 onplay_result = onplay(NULL, 0, curr_context);
764 else {
765 #ifdef HAVE_TAGCACHE
766 if (id3db)
768 if (tagtree_get_attr(&tc) == FILE_ATTR_AUDIO)
770 attr = FILE_ATTR_AUDIO;
771 tagtree_get_filename(&tc, buf, sizeof(buf));
773 else
774 attr = ATTR_DIRECTORY;
776 else
777 #endif
779 attr = dircache[tc.selected_item].attr;
781 if (currdir[1]) /* Not in / */
782 snprintf(buf, sizeof buf, "%s/%s",
783 currdir,
784 dircache[tc.selected_item].name);
785 else /* In / */
786 snprintf(buf, sizeof buf, "/%s",
787 dircache[tc.selected_item].name);
789 onplay_result = onplay(buf, attr, curr_context);
791 send_event(GUI_EVENT_REFRESH, tree_drawlists);
792 switch (onplay_result)
794 case ONPLAY_MAINMENU:
795 return GO_TO_ROOT;
797 case ONPLAY_OK:
798 restore = true;
799 break;
801 case ONPLAY_RELOAD_DIR:
802 reload_dir = true;
803 break;
805 case ONPLAY_START_PLAY:
806 return GO_TO_WPS;
807 break;
809 break;
812 #ifdef HAVE_HOTSWAP
813 case SYS_FS_CHANGED:
814 #ifdef HAVE_TAGCACHE
815 if (!id3db)
816 #endif
817 reload_dir = true;
818 /* The 'dir no longer valid' situation will be caught later
819 * by checking the showdir() result. */
820 break;
821 #endif
823 default:
824 if (default_event_handler(button) == SYS_USB_CONNECTED)
826 if(*tc.dirfilter > NUM_FILTER_MODES)
827 /* leave sub-browsers after usb, doing otherwise
828 might be confusing to the user */
829 exit_func = true;
830 else
831 reload_dir = true;
833 break;
835 if (start_wps)
836 return GO_TO_WPS;
837 if (button && !IS_SYSEVENT(button))
839 storage_spin();
843 check_rescan:
844 /* do we need to rescan dir? */
845 if (reload_dir || reload_root ||
846 lastfilter != *tc.dirfilter ||
847 lastsortcase != global_settings.sort_case)
849 if (reload_root) {
850 strcpy(currdir, "/");
851 tc.dirlevel = 0;
852 #ifdef HAVE_TAGCACHE
853 tc.currtable = 0;
854 tc.currextra = 0;
855 lasttable = -1;
856 lastextra = -1;
857 #endif
858 reload_root = false;
861 if (!reload_dir)
863 gui_synclist_select_item(&tree_lists, 0);
864 gui_synclist_draw(&tree_lists);
865 tc.selected_item = 0;
866 lastdir[0] = 0;
869 lastfilter = *tc.dirfilter;
870 lastsortcase = global_settings.sort_case;
871 restore = true;
874 if (exit_func)
875 return GO_TO_PREVIOUS;
877 if (restore || reload_dir) {
878 /* restore display */
879 numentries = update_dir();
880 reload_dir = false;
881 if (currdir[1] && (numentries < 0))
882 { /* not in root and reload failed */
883 reload_root = true; /* try root */
884 goto check_rescan;
888 return true;
891 bool create_playlist(void)
893 char filename[MAX_PATH];
895 snprintf(filename, sizeof filename, "%s.m3u8",
896 tc.currdir[1] ? tc.currdir : "/root");
897 splashf(0, "%s %s", str(LANG_CREATING), filename);
899 trigger_cpu_boost();
900 catalog_add_to_a_playlist(tc.currdir, ATTR_DIRECTORY, true, filename);
901 cancel_cpu_boost();
903 return true;
906 int rockbox_browse(const char *root, int dirfilter)
908 int ret_val = 0;
909 int *last_filter = tc.dirfilter;
910 tc.dirfilter = &dirfilter;
911 tc.sort_dir = global_settings.sort_dir;
913 reload_dir = true;
914 if (dirfilter >= NUM_FILTER_MODES)
916 static struct tree_context backup;
917 char current[MAX_PATH];
918 int last_context;
920 backup = tc;
921 tc.selected_item = 0;
922 tc.dirlevel = 0;
923 memcpy(tc.currdir, root, sizeof(tc.currdir));
924 start_wps = false;
925 last_context = curr_context;
927 /* Center on the currently loaded language when browsing languages. */
928 if (dirfilter == SHOW_LNG)
930 if (global_settings.lang_file[0])
932 snprintf(current, sizeof(current), LANG_DIR "/%s.lng",
933 global_settings.lang_file);
935 else
937 strlcpy(current, LANG_DIR "/english.lng", sizeof(current));
940 /* Center on currently loaded WPS */
941 else if (dirfilter == SHOW_WPS)
943 snprintf(current, sizeof(current), WPS_DIR "/%s.wps",
944 global_settings.wps_file);
946 #ifdef HAVE_REMOTE_LCD
947 /* Center on currently loaded RWPS */
948 else if (dirfilter == SHOW_RWPS)
950 snprintf(current, sizeof(current), WPS_DIR "/%s.rwps",
951 global_settings.rwps_file);
953 else if (dirfilter == SHOW_RSBS)
955 snprintf(current, sizeof(current), SBS_DIR "/%s.rsbs",
956 global_settings.rsbs_file);
958 #endif
959 #ifdef HAVE_LCD_BITMAP
960 /* Center on the currently loaded font when browsing fonts */
961 else if (dirfilter == SHOW_FONT)
963 snprintf(current, sizeof(current), FONT_DIR "/%s.fnt",
964 global_settings.font_file);
966 else if (dirfilter == SHOW_SBS)
968 snprintf(current, sizeof(current), SBS_DIR "/%s.sbs",
969 global_settings.sbs_file);
971 #endif
972 #if CONFIG_TUNER
973 /* Center on the currently loaded FM preset when browsing those */
974 else if (dirfilter == SHOW_FMR)
976 snprintf(current, sizeof(current), FMPRESET_PATH "/%s.fmr",
977 global_settings.fmr_file);
979 #endif
980 else /* reset current[] */
981 current[0] = '\0';
983 /* If we've found a file to center on, do it */
984 if (current[0] == '/')
986 set_current_file(current);
987 /* set_current_file changes dirlevel, change it back */
988 tc.dirlevel = 0;
991 ret_val = dirbrowse();
992 tc = backup;
993 curr_context = last_context;
995 else
997 static char buf[MAX_PATH];
998 if (dirfilter != SHOW_ID3DB)
999 tc.dirfilter = &global_settings.dirfilter;
1000 strcpy(buf,root);
1001 set_current_file(buf);
1002 ret_val = dirbrowse();
1004 tc.dirfilter = last_filter;
1005 return ret_val;
1008 void tree_mem_init(void)
1010 /* We copy the settings value in case it is changed by the user. We can't
1011 use it until the next reboot. */
1012 max_files = global_settings.max_files_in_dir;
1014 /* initialize tree context struct */
1015 memset(&tc, 0, sizeof(tc));
1016 tc.dirfilter = &global_settings.dirfilter;
1017 tc.sort_dir = global_settings.sort_dir;
1019 tc.name_buffer_size = AVERAGE_FILENAME_LENGTH * max_files;
1020 tc.name_buffer = buffer_alloc(tc.name_buffer_size);
1022 tc.dircache_size = max_files * sizeof(struct entry);
1023 tc.dircache = buffer_alloc(tc.dircache_size);
1024 tree_get_filetypes(&filetypes, &filetypes_count);
1027 bool bookmark_play(char *resume_file, int index, int offset, int seed,
1028 char *filename)
1030 int i;
1031 char* suffix = strrchr(resume_file, '.');
1032 bool started = false;
1034 if (suffix != NULL &&
1035 (!strcasecmp(suffix, ".m3u") || !strcasecmp(suffix, ".m3u8")))
1037 /* Playlist playback */
1038 char* slash;
1039 /* check that the file exists */
1040 if(!file_exists(resume_file))
1041 return false;
1043 slash = strrchr(resume_file,'/');
1044 if (slash)
1046 char* cp;
1047 *slash=0;
1049 cp=resume_file;
1050 if (!cp[0])
1051 cp="/";
1053 if (playlist_create(cp, slash+1) != -1)
1055 if (global_settings.playlist_shuffle)
1056 playlist_shuffle(seed, -1);
1057 playlist_start(index,offset);
1058 started = true;
1060 *slash='/';
1063 else
1065 /* Directory playback */
1066 lastdir[0]='\0';
1067 if (playlist_create(resume_file, NULL) != -1)
1069 char* peek_filename;
1070 resume_directory(resume_file);
1071 if (global_settings.playlist_shuffle)
1072 playlist_shuffle(seed, -1);
1074 /* Check if the file is at the same spot in the directory,
1075 else search for it */
1076 peek_filename = playlist_peek(index);
1078 if (peek_filename == NULL)
1079 return false;
1081 if (strcmp(strrchr(peek_filename, '/') + 1, filename))
1083 for ( i=0; i < playlist_amount(); i++ )
1085 peek_filename = playlist_peek(i);
1087 if (peek_filename == NULL)
1088 return false;
1090 if (!strcmp(strrchr(peek_filename, '/') + 1, filename))
1091 break;
1093 if (i < playlist_amount())
1094 index = i;
1095 else
1096 return false;
1098 playlist_start(index,offset);
1099 started = true;
1103 if (started)
1104 start_wps = true;
1105 return started;
1108 static void say_filetype(int attr)
1110 /* try to find a voice ID for the extension, if known */
1111 int j;
1112 attr &= FILE_ATTR_MASK; /* file type */
1113 for (j=0; j<filetypes_count; j++)
1114 if (attr == filetypes[j].tree_attr)
1116 talk_id(filetypes[j].voiceclip, true);
1117 return;
1121 static int ft_play_dirname(char* name)
1123 #if CONFIG_CODEC != SWCODEC
1124 if (audio_status() & AUDIO_STATUS_PLAY)
1125 return 0;
1126 #endif
1128 if(talk_file(tc.currdir, name, dir_thumbnail_name, NULL,
1129 NULL, false))
1131 if(global_settings.talk_filetype)
1132 talk_id(VOICE_DIR, true);
1133 return 1;
1135 else
1136 return -1;
1139 static void ft_play_filename(char *dir, char *file)
1141 #if CONFIG_CODEC != SWCODEC
1142 if (audio_status() & AUDIO_STATUS_PLAY)
1143 return;
1144 #endif
1146 if (strlen(file) >= strlen(file_thumbnail_ext)
1147 && strcasecmp(&file[strlen(file) - strlen(file_thumbnail_ext)],
1148 file_thumbnail_ext))
1149 /* file has no .talk extension */
1150 talk_file(dir, NULL, file, file_thumbnail_ext,
1151 NULL, false);
1152 else
1153 /* it already is a .talk file, play this directly, but prefix it. */
1154 talk_file(dir, NULL, file, NULL,
1155 TALK_IDARRAY(LANG_VOICE_DIR_HOVER), false);
1158 /* These two functions are called by the USB and shutdown handlers */
1159 void tree_flush(void)
1161 #ifdef HAVE_TAGCACHE
1162 tagcache_shutdown();
1163 #endif
1165 #ifdef HAVE_TC_RAMCACHE
1166 tagcache_unload_ramcache();
1167 #endif
1169 #ifdef HAVE_DIRCACHE
1171 int old_val = global_status.dircache_size;
1172 if (global_settings.dircache)
1174 if (!dircache_is_initializing())
1175 global_status.dircache_size = dircache_get_cache_size();
1176 # ifdef HAVE_EEPROM_SETTINGS
1177 if (firmware_settings.initialized)
1178 dircache_save();
1179 # endif
1180 dircache_disable();
1182 else
1184 global_status.dircache_size = 0;
1186 if (old_val != global_status.dircache_size)
1187 status_save();
1189 #endif
1192 void tree_restore(void)
1194 #ifdef HAVE_EEPROM_SETTINGS
1195 firmware_settings.disk_clean = false;
1196 #endif
1198 #ifdef HAVE_TC_RAMCACHE
1199 remove(TAGCACHE_STATEFILE);
1200 #endif
1202 #ifdef HAVE_DIRCACHE
1203 remove(DIRCACHE_FILE);
1204 if (global_settings.dircache)
1206 /* Print "Scanning disk..." to the display. */
1207 splash(0, str(LANG_SCANNING_DISK));
1209 dircache_build(global_status.dircache_size);
1211 #endif
1212 #ifdef HAVE_TAGCACHE
1213 tagcache_start_scan();
1214 #endif