Quit with an error message if the LCD is not supported
[kugel-rb.git] / apps / tree.c
blob228f491c15bd03e3f7879adc7f42a8c19b9192c6
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"
76 #include "root_menu.h"
78 static const struct filetype *filetypes;
79 static int filetypes_count;
81 struct gui_synclist tree_lists;
83 /* I put it here because other files doesn't use it yet,
84 * but should be elsewhere since it will be used mostly everywhere */
85 #ifdef HAVE_BUTTONBAR
86 struct gui_buttonbar tree_buttonbar;
87 #endif
88 static struct tree_context tc;
90 bool boot_changed = false;
92 char lastfile[MAX_PATH];
93 static char lastdir[MAX_PATH];
94 #ifdef HAVE_TAGCACHE
95 static int lasttable, lastextra, lastfirstpos;
96 #endif
97 static int max_files = 0;
99 static bool reload_dir = false;
101 static bool start_wps = false;
102 static int curr_context = false;/* id3db or tree*/
104 static int dirbrowse(void);
105 static int ft_play_dirname(char* name);
106 static void ft_play_filename(char *dir, char *file);
107 static void say_filetype(int attr);
109 static const char* tree_get_filename(int selected_item, void *data,
110 char *buffer, size_t buffer_len)
112 struct tree_context * local_tc=(struct tree_context *)data;
113 char *name;
114 int attr=0;
115 bool stripit = false;
116 #ifdef HAVE_TAGCACHE
117 bool id3db = *(local_tc->dirfilter) == SHOW_ID3DB;
119 if (id3db)
121 return tagtree_get_entry(&tc, selected_item)->name;
123 else
124 #endif
126 struct entry* dc = local_tc->dircache;
127 struct entry* e = &dc[selected_item];
128 name = e->name;
129 attr = e->attr;
132 if(!(attr & ATTR_DIRECTORY))
134 switch(global_settings.show_filename_ext)
136 case 0:
137 /* show file extension: off */
138 stripit = true;
139 break;
140 case 1:
141 /* show file extension: on */
142 break;
143 case 2:
144 /* show file extension: only unknown types */
145 stripit = filetype_supported(attr);
146 break;
147 case 3:
148 default:
149 /* show file extension: only when viewing all */
150 stripit = (*(local_tc->dirfilter) != SHOW_ID3DB) &&
151 (*(local_tc->dirfilter) != SHOW_ALL);
152 break;
156 if(stripit)
158 return(strip_extension(buffer, buffer_len, name));
160 return(name);
163 #ifdef HAVE_LCD_COLOR
164 static int tree_get_filecolor(int selected_item, void * data)
166 if (*tc.dirfilter == SHOW_ID3DB)
167 return -1;
168 struct tree_context * local_tc=(struct tree_context *)data;
169 struct entry* dc = local_tc->dircache;
170 struct entry* e = &dc[selected_item];
171 return filetype_get_color(e->name, e->attr);
173 #endif
175 static enum themable_icons tree_get_fileicon(int selected_item, void * data)
177 struct tree_context * local_tc=(struct tree_context *)data;
178 #ifdef HAVE_TAGCACHE
179 bool id3db = *(local_tc->dirfilter) == SHOW_ID3DB;
180 if (id3db) {
181 return tagtree_get_icon(&tc);
183 else
184 #endif
186 struct entry* dc = local_tc->dircache;
187 struct entry* e = &dc[selected_item];
188 return filetype_get_icon(e->attr);
192 static int tree_voice_cb(int selected_item, void * data)
194 struct tree_context * local_tc=(struct tree_context *)data;
195 char *name;
196 int attr=0;
197 #ifdef HAVE_TAGCACHE
198 bool id3db = *(local_tc->dirfilter) == SHOW_ID3DB;
200 if (id3db)
202 attr = tagtree_get_attr(local_tc);
203 name = tagtree_get_entry(local_tc, selected_item)->name;
205 else
206 #endif
208 struct entry* dc = local_tc->dircache;
209 struct entry* e = &dc[selected_item];
210 name = e->name;
211 attr = e->attr;
213 bool is_dir = (attr & ATTR_DIRECTORY);
214 bool did_clip = false;
215 /* First the .talk clip case */
216 if(is_dir)
218 if(global_settings.talk_dir_clip)
220 did_clip = true;
221 if(ft_play_dirname(name) <0)
222 /* failed, not existing */
223 did_clip = false;
225 } else { /* it's a file */
226 if (global_settings.talk_file_clip && (attr & FILE_ATTR_THUMBNAIL))
228 did_clip = true;
229 ft_play_filename(local_tc->currdir, name);
232 if(!did_clip)
234 /* say the number or spell if required or as a fallback */
235 switch (is_dir ? global_settings.talk_dir : global_settings.talk_file)
237 case 1: /* as numbers */
238 talk_id(is_dir ? VOICE_DIR : VOICE_FILE, false);
239 talk_number(selected_item+1 - (is_dir ? 0 : local_tc->dirsindir),
240 true);
241 if(global_settings.talk_filetype
242 && !is_dir && *local_tc->dirfilter < NUM_FILTER_MODES)
243 say_filetype(attr);
244 break;
245 case 2: /* spelled */
246 talk_shutup();
247 if(global_settings.talk_filetype)
249 if(is_dir)
250 talk_id(VOICE_DIR, true);
251 else if(*local_tc->dirfilter < NUM_FILTER_MODES)
252 say_filetype(attr);
254 talk_spell(name, true);
255 break;
258 return 0;
261 bool check_rockboxdir(void)
263 if(!dir_exists(ROCKBOX_DIR))
264 { /* No need to localise this message.
265 If .rockbox is missing, it wouldn't work anyway */
266 int i;
267 FOR_NB_SCREENS(i)
268 screens[i].clear_display();
269 splash(HZ*2, "No .rockbox directory");
270 FOR_NB_SCREENS(i)
271 screens[i].clear_display();
272 splash(HZ*2, "Installation incomplete");
273 return false;
275 return true;
278 /* do this really late in the init sequence */
279 void tree_gui_init(void)
281 check_rockboxdir();
283 strcpy(tc.currdir, "/");
285 #ifdef HAVE_LCD_CHARCELLS
286 int i;
287 FOR_NB_SCREENS(i)
288 screens[i].double_height(false);
289 #endif
290 #ifdef HAVE_BUTTONBAR
291 gui_buttonbar_init(&tree_buttonbar);
292 /* since archos only have one screen, no need to create more than that */
293 gui_buttonbar_set_display(&tree_buttonbar, &(screens[SCREEN_MAIN]) );
294 #endif
295 gui_synclist_init(&tree_lists, &tree_get_filename, &tc, false, 1, NULL);
296 gui_synclist_set_voice_callback(&tree_lists, tree_voice_cb);
297 gui_synclist_set_icon_callback(&tree_lists, &tree_get_fileicon);
298 #ifdef HAVE_LCD_COLOR
299 gui_synclist_set_color_callback(&tree_lists, &tree_get_filecolor);
300 #endif
305 struct tree_context* tree_get_context(void)
307 return &tc;
311 * Returns the position of a given file in the current directory
312 * returns -1 if not found
314 static int tree_get_file_position(char * filename)
316 int i;
318 /* use lastfile to determine the selected item (default=0) */
319 for (i=0; i < tc.filesindir; i++)
321 struct entry* dc = tc.dircache;
322 struct entry* e = &dc[i];
323 if (!strcasecmp(e->name, filename))
324 return(i);
326 return(-1);/* no file can match, returns undefined */
330 * Called when a new dir is loaded (for example when returning from other apps ...)
331 * also completely redraws the tree
333 static int update_dir(void)
335 bool changed = false;
336 #ifdef HAVE_TAGCACHE
337 bool id3db = *tc.dirfilter == SHOW_ID3DB;
338 /* Checks for changes */
339 if (id3db) {
340 if (tc.currtable != lasttable ||
341 tc.currextra != lastextra ||
342 tc.firstpos != lastfirstpos ||
343 reload_dir)
345 if (tagtree_load(&tc) < 0)
346 return -1;
348 lasttable = tc.currtable;
349 lastextra = tc.currextra;
350 lastfirstpos = tc.firstpos;
351 changed = true;
354 else
355 #endif
357 /* if the tc.currdir has been changed, reload it ...*/
358 if (strncmp(tc.currdir, lastdir, sizeof(lastdir)) || reload_dir)
360 if (ft_load(&tc, NULL) < 0)
361 return -1;
362 strcpy(lastdir, tc.currdir);
363 changed = true;
366 /* if selected item is undefined */
367 if (tc.selected_item == -1)
369 /* use lastfile to determine the selected item */
370 tc.selected_item = tree_get_file_position(lastfile);
372 /* If the file doesn't exists, select the first one (default) */
373 if(tc.selected_item < 0)
374 tc.selected_item = 0;
375 changed = true;
377 if (changed)
380 #ifdef HAVE_TAGCACHE
381 !id3db &&
382 #endif
383 (tc.dirfull || tc.filesindir == global_settings.max_files_in_dir) )
385 splash(HZ, ID2P(LANG_SHOWDIR_BUFFER_FULL));
388 #ifdef HAVE_TAGCACHE
389 if (id3db)
391 #ifdef HAVE_LCD_BITMAP
392 if (global_settings.show_path_in_browser == SHOW_PATH_FULL
393 || global_settings.show_path_in_browser == SHOW_PATH_CURRENT)
395 gui_synclist_set_title(&tree_lists, tagtree_get_title(&tc),
396 filetype_get_icon(ATTR_DIRECTORY));
398 else
400 /* Must clear the title as the list is reused */
401 gui_synclist_set_title(&tree_lists, NULL, NOICON);
403 #endif
405 else
406 #endif
408 #ifdef HAVE_LCD_BITMAP
409 if (global_settings.show_path_in_browser &&
410 *(tc.dirfilter) == SHOW_PLUGINS)
412 char *title;
413 if (!strcmp(tc.currdir, PLUGIN_GAMES_DIR))
414 title = str(LANG_PLUGIN_GAMES);
415 else if (!strcmp(tc.currdir, PLUGIN_APPS_DIR))
416 title = str(LANG_PLUGIN_APPS);
417 else if (!strcmp(tc.currdir, PLUGIN_DEMOS_DIR))
418 title = str(LANG_PLUGIN_DEMOS);
419 else title = str(LANG_PLUGINS);
420 gui_synclist_set_title(&tree_lists, title, Icon_Plugin);
422 else if (global_settings.show_path_in_browser == SHOW_PATH_FULL)
424 gui_synclist_set_title(&tree_lists, tc.currdir,
425 filetype_get_icon(ATTR_DIRECTORY));
427 else if (global_settings.show_path_in_browser == SHOW_PATH_CURRENT)
429 char *title = strrchr(tc.currdir, '/') + 1;
430 if (*title == '\0')
432 /* Display "Files" for the root dir */
433 gui_synclist_set_title(&tree_lists, str(LANG_DIR_BROWSER),
434 filetype_get_icon(ATTR_DIRECTORY));
436 else
437 gui_synclist_set_title(&tree_lists, title,
438 filetype_get_icon(ATTR_DIRECTORY));
440 else
442 /* Must clear the title as the list is reused */
443 gui_synclist_set_title(&tree_lists, NULL, NOICON);
445 #endif
448 gui_synclist_set_nb_items(&tree_lists, tc.filesindir);
449 gui_synclist_set_icon_callback(&tree_lists, tree_get_fileicon);
450 if( tc.selected_item >= tc.filesindir)
451 tc.selected_item=tc.filesindir-1;
453 gui_synclist_select_item(&tree_lists, tc.selected_item);
454 #ifdef HAVE_BUTTONBAR
455 if (global_settings.buttonbar) {
456 if (*tc.dirfilter < NUM_FILTER_MODES)
457 gui_buttonbar_set(&tree_buttonbar, str(LANG_SYSFONT_DIRBROWSE_F1),
458 str(LANG_SYSFONT_DIRBROWSE_F2),
459 str(LANG_SYSFONT_DIRBROWSE_F3));
460 else
461 gui_buttonbar_set(&tree_buttonbar, "<<<", "", "");
462 gui_buttonbar_draw(&tree_buttonbar);
464 #endif
465 gui_synclist_draw(&tree_lists);
466 gui_synclist_speak_item(&tree_lists);
467 return tc.filesindir;
470 /* load tracks from specified directory to resume play */
471 void resume_directory(const char *dir)
473 int dirfilter = *tc.dirfilter;
474 int ret;
475 #ifdef HAVE_TAGCACHE
476 bool id3db = *tc.dirfilter == SHOW_ID3DB;
477 #endif
478 /* make sure the dirfilter is sane. The only time it should be possible
479 * thats its not is when resume playlist is called from a plugin
481 #ifdef HAVE_TAGCACHE
482 if (!id3db)
483 #endif
484 *tc.dirfilter = global_settings.dirfilter;
485 ret = ft_load(&tc, dir);
486 *tc.dirfilter = dirfilter;
487 if (ret < 0)
488 return;
489 lastdir[0] = 0;
491 ft_build_playlist(&tc, 0);
493 #ifdef HAVE_TAGCACHE
494 if (id3db)
495 tagtree_load(&tc);
496 #endif
499 /* Returns the current working directory and also writes cwd to buf if
500 non-NULL. In case of error, returns NULL. */
501 char *getcwd(char *buf, int size)
503 if (!buf)
504 return tc.currdir;
505 else if (size > 0)
507 strlcpy(buf, tc.currdir, size);
508 return buf;
510 else
511 return NULL;
514 /* Force a reload of the directory next time directory browser is called */
515 void reload_directory(void)
517 reload_dir = true;
520 void get_current_file(char* buffer, int buffer_len)
522 #ifdef HAVE_TAGCACHE
523 /* in ID3DB mode it is a bad idea to call this function */
524 /* (only happens with `follow playlist') */
525 if( *tc.dirfilter == SHOW_ID3DB )
526 return;
527 #endif
529 struct entry* dc = tc.dircache;
530 struct entry* e = &dc[tc.selected_item];
531 snprintf(buffer, buffer_len, "%s/%s", getcwd(NULL,0),
532 tc.dirlength ? e->name : "");
535 /* Allow apps to change our dirfilter directly (required for sub browsers)
536 if they're suddenly going to become a file browser for example */
537 void set_dirfilter(int l_dirfilter)
539 *tc.dirfilter = l_dirfilter;
542 /* Selects a file and update tree context properly */
543 void set_current_file(char *path)
545 char *name;
546 int i;
548 #ifdef HAVE_TAGCACHE
549 /* in ID3DB mode it is a bad idea to call this function */
550 /* (only happens with `follow playlist') */
551 if( *tc.dirfilter == SHOW_ID3DB )
552 return;
553 #endif
555 /* separate directory from filename */
556 /* gets the directory's name and put it into tc.currdir */
557 name = strrchr(path+1,'/');
558 if (name)
560 *name = 0;
561 strcpy(tc.currdir, path);
562 *name = '/';
563 name++;
565 else
567 strcpy(tc.currdir, "/");
568 name = path+1;
571 strcpy(lastfile, name);
574 /* If we changed dir we must recalculate the dirlevel
575 and adjust the selected history properly */
576 if (strncmp(tc.currdir,lastdir,sizeof(lastdir)))
578 tc.dirlevel = 0;
579 tc.selected_item_history[tc.dirlevel] = -1;
581 /* use '/' to calculate dirlevel */
582 for (i = 1; path[i] != '\0'; i++)
584 if (path[i] == '/')
586 tc.dirlevel++;
587 tc.selected_item_history[tc.dirlevel] = -1;
591 if (ft_load(&tc, NULL) >= 0)
593 tc.selected_item = tree_get_file_position(lastfile);
598 /* main loop, handles key events */
599 static int dirbrowse()
601 int numentries=0;
602 char buf[MAX_PATH];
603 int button, oldbutton;
604 bool reload_root = false;
605 int lastfilter = *tc.dirfilter;
606 bool lastsortcase = global_settings.sort_case;
607 bool exit_func = false;
609 char* currdir = tc.currdir; /* just a shortcut */
610 #ifdef HAVE_TAGCACHE
611 bool id3db = *tc.dirfilter == SHOW_ID3DB;
613 if (id3db)
614 curr_context=CONTEXT_ID3DB;
615 else
616 #endif
617 curr_context=CONTEXT_TREE;
618 if (tc.selected_item < 0)
619 tc.selected_item = 0;
620 #ifdef HAVE_TAGCACHE
621 tc.firstpos = 0;
622 lasttable = -1;
623 lastextra = -1;
624 lastfirstpos = 0;
625 #endif
627 start_wps = false;
628 numentries = update_dir();
629 reload_dir = false;
630 if (numentries == -1)
631 return GO_TO_PREVIOUS; /* currdir is not a directory */
633 if (*tc.dirfilter > NUM_FILTER_MODES && numentries==0)
635 splash(HZ*2, ID2P(LANG_NO_FILES));
636 return GO_TO_PREVIOUS; /* No files found for rockbox_browser() */
639 gui_synclist_draw(&tree_lists);
640 while(1) {
641 struct entry *dircache = tc.dircache;
642 bool restore = false;
643 if (tc.dirlevel < 0)
644 tc.dirlevel = 0; /* shouldnt be needed.. this code needs work! */
645 #ifdef BOOTFILE
646 if (boot_changed) {
647 static const char *lines[]={ID2P(LANG_BOOT_CHANGED), ID2P(LANG_REBOOT_NOW)};
648 static const struct text_message message={lines, 2};
649 if(gui_syncyesno_run(&message, NULL, NULL)==YESNO_YES)
650 rolo_load("/" BOOTFILE);
651 restore = true;
652 boot_changed = false;
654 #endif
655 button = get_action(CONTEXT_TREE,
656 list_do_action_timeout(&tree_lists, HZ/2));
657 oldbutton = button;
658 gui_synclist_do_button(&tree_lists, &button,LIST_WRAP_UNLESS_HELD);
659 tc.selected_item = gui_synclist_get_sel_pos(&tree_lists);
660 switch ( button ) {
661 case ACTION_STD_OK:
662 /* nothing to do if no files to display */
663 if ( numentries == 0 )
664 break;
666 #ifdef HAVE_TAGCACHE
667 switch (id3db?tagtree_enter(&tc):ft_enter(&tc))
668 #else
669 switch (ft_enter(&tc))
670 #endif
672 case 1: reload_dir = true; break;
673 case 2: start_wps = true; break;
674 case 3: exit_func = true; break;
675 default: break;
677 restore = true;
678 break;
680 case ACTION_STD_CANCEL:
681 if (*tc.dirfilter > NUM_FILTER_MODES && tc.dirlevel < 1) {
682 exit_func = true;
683 break;
685 if ((*tc.dirfilter == SHOW_ID3DB && tc.dirlevel == 0) ||
686 ((*tc.dirfilter != SHOW_ID3DB && !strcmp(currdir,"/"))))
688 #ifdef HAVE_LCD_BITMAP /* charcell doesnt have ACTION_TREE_PGLEFT so this isnt needed */
689 if (oldbutton == ACTION_TREE_PGLEFT)
690 break;
691 else
692 #endif
693 return GO_TO_ROOT;
696 #ifdef HAVE_TAGCACHE
697 if (id3db)
698 tagtree_exit(&tc);
699 else
700 #endif
701 if (ft_exit(&tc) == 3)
702 exit_func = true;
704 restore = true;
705 break;
707 case ACTION_TREE_STOP:
708 if (list_stop_handler())
709 restore = true;
710 break;
712 case ACTION_STD_MENU:
713 return GO_TO_ROOT;
714 break;
716 #ifdef HAVE_RECORDING
717 case ACTION_STD_REC:
718 return GO_TO_RECSCREEN;
719 #endif
721 case ACTION_TREE_WPS:
722 return GO_TO_PREVIOUS_MUSIC;
723 break;
724 #ifdef HAVE_QUICKSCREEN
725 case ACTION_STD_QUICKSCREEN:
726 /* don't enter f2 from plugin browser */
727 if (*tc.dirfilter < NUM_FILTER_MODES)
729 if (quick_screen_quick(button))
730 reload_dir = true;
731 restore = true;
733 break;
734 #endif
735 #ifdef BUTTON_F3
736 case ACTION_F3:
737 /* don't enter f3 from plugin browser */
738 if (*tc.dirfilter < NUM_FILTER_MODES)
740 if (quick_screen_f3(ACTION_F3))
741 reload_dir = true;
742 restore = true;
744 break;
745 #endif
747 case ACTION_STD_CONTEXT:
749 int onplay_result;
750 int attr = 0;
752 if(!numentries)
753 onplay_result = onplay(NULL, 0, curr_context);
754 else {
755 #ifdef HAVE_TAGCACHE
756 if (id3db)
758 if (tagtree_get_attr(&tc) == FILE_ATTR_AUDIO)
760 attr = FILE_ATTR_AUDIO;
761 tagtree_get_filename(&tc, buf, sizeof(buf));
763 else
764 attr = ATTR_DIRECTORY;
766 else
767 #endif
769 attr = dircache[tc.selected_item].attr;
771 if (currdir[1]) /* Not in / */
772 snprintf(buf, sizeof buf, "%s/%s",
773 currdir,
774 dircache[tc.selected_item].name);
775 else /* In / */
776 snprintf(buf, sizeof buf, "/%s",
777 dircache[tc.selected_item].name);
779 onplay_result = onplay(buf, attr, curr_context);
781 switch (onplay_result)
783 case ONPLAY_MAINMENU:
784 return GO_TO_ROOT;
786 case ONPLAY_OK:
787 restore = true;
788 break;
790 case ONPLAY_RELOAD_DIR:
791 reload_dir = true;
792 break;
794 case ONPLAY_START_PLAY:
795 return GO_TO_WPS;
796 break;
798 break;
801 #ifdef HAVE_HOTSWAP
802 case SYS_FS_CHANGED:
803 #ifdef HAVE_TAGCACHE
804 if (!id3db)
805 #endif
806 reload_dir = true;
807 /* The 'dir no longer valid' situation will be caught later
808 * by checking the showdir() result. */
809 break;
810 #endif
812 default:
813 if (default_event_handler(button) == SYS_USB_CONNECTED)
815 if(*tc.dirfilter > NUM_FILTER_MODES)
816 /* leave sub-browsers after usb, doing otherwise
817 might be confusing to the user */
818 exit_func = true;
819 else
820 reload_dir = true;
822 break;
824 if (start_wps)
825 return GO_TO_WPS;
826 if (button && !IS_SYSEVENT(button))
828 storage_spin();
832 check_rescan:
833 /* do we need to rescan dir? */
834 if (reload_dir || reload_root ||
835 lastfilter != *tc.dirfilter ||
836 lastsortcase != global_settings.sort_case)
838 if (reload_root) {
839 strcpy(currdir, "/");
840 tc.dirlevel = 0;
841 #ifdef HAVE_TAGCACHE
842 tc.currtable = 0;
843 tc.currextra = 0;
844 lasttable = -1;
845 lastextra = -1;
846 #endif
847 reload_root = false;
850 if (!reload_dir)
852 gui_synclist_select_item(&tree_lists, 0);
853 gui_synclist_draw(&tree_lists);
854 tc.selected_item = 0;
855 lastdir[0] = 0;
858 lastfilter = *tc.dirfilter;
859 lastsortcase = global_settings.sort_case;
860 restore = true;
863 if (exit_func)
864 return GO_TO_PREVIOUS;
866 if (restore || reload_dir) {
867 /* restore display */
868 numentries = update_dir();
869 reload_dir = false;
870 if (currdir[1] && (numentries < 0))
871 { /* not in root and reload failed */
872 reload_root = true; /* try root */
873 goto check_rescan;
877 return true;
880 bool create_playlist(void)
882 char filename[MAX_PATH];
884 snprintf(filename, sizeof filename, "%s.m3u8",
885 tc.currdir[1] ? tc.currdir : "/root");
886 splashf(0, "%s %s", str(LANG_CREATING), filename);
888 trigger_cpu_boost();
889 catalog_add_to_a_playlist(tc.currdir, ATTR_DIRECTORY, true, filename);
890 cancel_cpu_boost();
892 return true;
895 int rockbox_browse(const char *root, int dirfilter)
897 int ret_val = 0;
898 int *last_filter = tc.dirfilter;
899 tc.dirfilter = &dirfilter;
900 tc.sort_dir = global_settings.sort_dir;
902 reload_dir = true;
903 if (dirfilter >= NUM_FILTER_MODES)
905 static struct tree_context backup;
906 char current[MAX_PATH];
907 int last_context;
909 backup = tc;
910 tc.selected_item = 0;
911 tc.dirlevel = 0;
912 memcpy(tc.currdir, root, sizeof(tc.currdir));
913 start_wps = false;
914 last_context = curr_context;
916 /* Center on the currently loaded language when browsing languages. */
917 if (dirfilter == SHOW_LNG)
919 if (global_settings.lang_file[0])
921 snprintf(current, sizeof(current), LANG_DIR "/%s.lng",
922 global_settings.lang_file);
924 else
926 strlcpy(current, LANG_DIR "/english.lng", sizeof(current));
929 /* Center on currently loaded WPS */
930 else if (dirfilter == SHOW_WPS)
932 snprintf(current, sizeof(current), WPS_DIR "/%s.wps",
933 global_settings.wps_file);
935 #ifdef HAVE_REMOTE_LCD
936 /* Center on currently loaded RWPS */
937 else if (dirfilter == SHOW_RWPS)
939 snprintf(current, sizeof(current), WPS_DIR "/%s.rwps",
940 global_settings.rwps_file);
942 #endif
943 #ifdef HAVE_LCD_BITMAP
944 /* Center on the currently loaded font when browsing fonts */
945 else if (dirfilter == SHOW_FONT)
947 snprintf(current, sizeof(current), FONT_DIR "/%s.fnt",
948 global_settings.font_file);
950 #endif
951 #if CONFIG_TUNER
952 /* Center on the currently loaded FM preset when browsing those */
953 else if (dirfilter == SHOW_FMR)
955 snprintf(current, sizeof(current), FMPRESET_PATH "/%s.fmr",
956 global_settings.fmr_file);
958 #endif
959 else /* reset current[] */
960 current[0] = '\0';
962 /* If we've found a file to center on, do it */
963 if (current[0] == '/')
965 set_current_file(current);
966 /* set_current_file changes dirlevel, change it back */
967 tc.dirlevel = 0;
970 ret_val = dirbrowse();
971 tc = backup;
972 curr_context = last_context;
974 else
976 static char buf[MAX_PATH];
977 if (dirfilter != SHOW_ID3DB)
978 tc.dirfilter = &global_settings.dirfilter;
979 strcpy(buf,root);
980 set_current_file(buf);
981 ret_val = dirbrowse();
983 tc.dirfilter = last_filter;
984 return ret_val;
987 void tree_mem_init(void)
989 /* We copy the settings value in case it is changed by the user. We can't
990 use it until the next reboot. */
991 max_files = global_settings.max_files_in_dir;
993 /* initialize tree context struct */
994 memset(&tc, 0, sizeof(tc));
995 tc.dirfilter = &global_settings.dirfilter;
996 tc.sort_dir = global_settings.sort_dir;
998 tc.name_buffer_size = AVERAGE_FILENAME_LENGTH * max_files;
999 tc.name_buffer = buffer_alloc(tc.name_buffer_size);
1001 tc.dircache_size = max_files * sizeof(struct entry);
1002 tc.dircache = buffer_alloc(tc.dircache_size);
1003 tree_get_filetypes(&filetypes, &filetypes_count);
1006 bool bookmark_play(char *resume_file, int index, int offset, int seed,
1007 char *filename)
1009 int i;
1010 char* suffix = strrchr(resume_file, '.');
1011 bool started = false;
1013 if (suffix != NULL &&
1014 (!strcasecmp(suffix, ".m3u") || !strcasecmp(suffix, ".m3u8")))
1016 /* Playlist playback */
1017 char* slash;
1018 /* check that the file exists */
1019 if(!file_exists(resume_file))
1020 return false;
1022 slash = strrchr(resume_file,'/');
1023 if (slash)
1025 char* cp;
1026 *slash=0;
1028 cp=resume_file;
1029 if (!cp[0])
1030 cp="/";
1032 if (playlist_create(cp, slash+1) != -1)
1034 if (global_settings.playlist_shuffle)
1035 playlist_shuffle(seed, -1);
1036 playlist_start(index,offset);
1037 started = true;
1039 *slash='/';
1042 else
1044 /* Directory playback */
1045 lastdir[0]='\0';
1046 if (playlist_create(resume_file, NULL) != -1)
1048 char* peek_filename;
1049 resume_directory(resume_file);
1050 if (global_settings.playlist_shuffle)
1051 playlist_shuffle(seed, -1);
1053 /* Check if the file is at the same spot in the directory,
1054 else search for it */
1055 peek_filename = playlist_peek(index);
1057 if (peek_filename == NULL)
1058 return false;
1060 if (strcmp(strrchr(peek_filename, '/') + 1, filename))
1062 for ( i=0; i < playlist_amount(); i++ )
1064 peek_filename = playlist_peek(i);
1066 if (peek_filename == NULL)
1067 return false;
1069 if (!strcmp(strrchr(peek_filename, '/') + 1, filename))
1070 break;
1072 if (i < playlist_amount())
1073 index = i;
1074 else
1075 return false;
1077 playlist_start(index,offset);
1078 started = true;
1082 if (started)
1083 start_wps = true;
1084 return started;
1087 static void say_filetype(int attr)
1089 /* try to find a voice ID for the extension, if known */
1090 int j;
1091 attr &= FILE_ATTR_MASK; /* file type */
1092 for (j=0; j<filetypes_count; j++)
1093 if (attr == filetypes[j].tree_attr)
1095 talk_id(filetypes[j].voiceclip, true);
1096 return;
1100 static int ft_play_dirname(char* name)
1102 #if CONFIG_CODEC != SWCODEC
1103 if (audio_status() & AUDIO_STATUS_PLAY)
1104 return 0;
1105 #endif
1107 if(talk_file(tc.currdir, name, dir_thumbnail_name, NULL,
1108 NULL, false))
1110 if(global_settings.talk_filetype)
1111 talk_id(VOICE_DIR, true);
1112 return 1;
1114 else
1115 return -1;
1118 static void ft_play_filename(char *dir, char *file)
1120 #if CONFIG_CODEC != SWCODEC
1121 if (audio_status() & AUDIO_STATUS_PLAY)
1122 return;
1123 #endif
1125 if (strlen(file) >= strlen(file_thumbnail_ext)
1126 && strcasecmp(&file[strlen(file) - strlen(file_thumbnail_ext)],
1127 file_thumbnail_ext))
1128 /* file has no .talk extension */
1129 talk_file(dir, NULL, file, file_thumbnail_ext,
1130 NULL, false);
1131 else
1132 /* it already is a .talk file, play this directly, but prefix it. */
1133 talk_file(dir, NULL, file, NULL,
1134 TALK_IDARRAY(LANG_VOICE_DIR_HOVER), false);
1137 /* These two functions are called by the USB and shutdown handlers */
1138 void tree_flush(void)
1140 #ifdef HAVE_TAGCACHE
1141 tagcache_shutdown();
1142 #endif
1144 #ifdef HAVE_TC_RAMCACHE
1145 tagcache_unload_ramcache();
1146 #endif
1148 #ifdef HAVE_DIRCACHE
1150 int old_val = global_status.dircache_size;
1151 if (global_settings.dircache)
1153 if (!dircache_is_initializing())
1154 global_status.dircache_size = dircache_get_cache_size();
1155 # ifdef HAVE_EEPROM_SETTINGS
1156 if (firmware_settings.initialized)
1157 dircache_save();
1158 # endif
1159 dircache_disable();
1161 else
1163 global_status.dircache_size = 0;
1165 if (old_val != global_status.dircache_size)
1166 status_save();
1168 #endif
1171 void tree_restore(void)
1173 #ifdef HAVE_EEPROM_SETTINGS
1174 firmware_settings.disk_clean = false;
1175 #endif
1177 #ifdef HAVE_TC_RAMCACHE
1178 remove(TAGCACHE_STATEFILE);
1179 #endif
1181 #ifdef HAVE_DIRCACHE
1182 remove(DIRCACHE_FILE);
1183 if (global_settings.dircache)
1185 /* Print "Scanning disk..." to the display. */
1186 splash(0, str(LANG_SCANNING_DISK));
1188 dircache_build(global_status.dircache_size);
1190 #endif
1191 #ifdef HAVE_TAGCACHE
1192 tagcache_start_scan();
1193 #endif