Fixed m200v4 red build.
[kugel-rb.git] / apps / tree.c
bloba3765d74d3f1dfa30b220c2eab08acb55447586c
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 gui_sync_wps_screen_init();
290 check_rockboxdir();
292 strcpy(tc.currdir, "/");
294 #ifdef HAVE_LCD_CHARCELLS
295 int i;
296 FOR_NB_SCREENS(i)
297 screens[i].double_height(false);
298 #endif
299 #ifdef HAVE_BUTTONBAR
300 gui_buttonbar_init(&tree_buttonbar);
301 /* since archos only have one screen, no need to create more than that */
302 gui_buttonbar_set_display(&tree_buttonbar, &(screens[SCREEN_MAIN]) );
303 #endif
304 gui_synclist_init(&tree_lists, &tree_get_filename, &tc, false, 1, NULL);
305 gui_synclist_set_voice_callback(&tree_lists, tree_voice_cb);
306 gui_synclist_set_icon_callback(&tree_lists, &tree_get_fileicon);
307 #ifdef HAVE_LCD_COLOR
308 gui_synclist_set_color_callback(&tree_lists, &tree_get_filecolor);
309 #endif
314 struct tree_context* tree_get_context(void)
316 return &tc;
320 * Returns the position of a given file in the current directory
321 * returns -1 if not found
323 static int tree_get_file_position(char * filename)
325 int i;
327 /* use lastfile to determine the selected item (default=0) */
328 for (i=0; i < tc.filesindir; i++)
330 struct entry* dc = tc.dircache;
331 struct entry* e = &dc[i];
332 if (!strcasecmp(e->name, filename))
333 return(i);
335 return(-1);/* no file can match, returns undefined */
339 * Called when a new dir is loaded (for example when returning from other apps ...)
340 * also completely redraws the tree
342 static int update_dir(void)
344 bool changed = false;
345 #ifdef HAVE_TAGCACHE
346 bool id3db = *tc.dirfilter == SHOW_ID3DB;
347 /* Checks for changes */
348 if (id3db) {
349 if (tc.currtable != lasttable ||
350 tc.currextra != lastextra ||
351 tc.firstpos != lastfirstpos ||
352 reload_dir)
354 if (tagtree_load(&tc) < 0)
355 return -1;
357 lasttable = tc.currtable;
358 lastextra = tc.currextra;
359 lastfirstpos = tc.firstpos;
360 changed = true;
363 else
364 #endif
366 /* if the tc.currdir has been changed, reload it ...*/
367 if (strncmp(tc.currdir, lastdir, sizeof(lastdir)) || reload_dir)
369 if (ft_load(&tc, NULL) < 0)
370 return -1;
371 strcpy(lastdir, tc.currdir);
372 changed = true;
375 /* if selected item is undefined */
376 if (tc.selected_item == -1)
378 /* use lastfile to determine the selected item */
379 tc.selected_item = tree_get_file_position(lastfile);
381 /* If the file doesn't exists, select the first one (default) */
382 if(tc.selected_item < 0)
383 tc.selected_item = 0;
384 changed = true;
386 if (changed)
389 #ifdef HAVE_TAGCACHE
390 !id3db &&
391 #endif
392 (tc.dirfull || tc.filesindir == global_settings.max_files_in_dir) )
394 splash(HZ, ID2P(LANG_SHOWDIR_BUFFER_FULL));
397 #ifdef HAVE_TAGCACHE
398 if (id3db)
400 #ifdef HAVE_LCD_BITMAP
401 if (global_settings.show_path_in_browser == SHOW_PATH_FULL
402 || global_settings.show_path_in_browser == SHOW_PATH_CURRENT)
404 gui_synclist_set_title(&tree_lists, tagtree_get_title(&tc),
405 filetype_get_icon(ATTR_DIRECTORY));
407 else
409 /* Must clear the title as the list is reused */
410 gui_synclist_set_title(&tree_lists, NULL, NOICON);
412 #endif
414 else
415 #endif
417 #ifdef HAVE_LCD_BITMAP
418 if (global_settings.show_path_in_browser &&
419 *(tc.dirfilter) == SHOW_PLUGINS)
421 char *title;
422 if (!strcmp(tc.currdir, PLUGIN_GAMES_DIR))
423 title = str(LANG_PLUGIN_GAMES);
424 else if (!strcmp(tc.currdir, PLUGIN_APPS_DIR))
425 title = str(LANG_PLUGIN_APPS);
426 else if (!strcmp(tc.currdir, PLUGIN_DEMOS_DIR))
427 title = str(LANG_PLUGIN_DEMOS);
428 else title = str(LANG_PLUGINS);
429 gui_synclist_set_title(&tree_lists, title, Icon_Plugin);
431 else if (global_settings.show_path_in_browser == SHOW_PATH_FULL)
433 gui_synclist_set_title(&tree_lists, tc.currdir,
434 filetype_get_icon(ATTR_DIRECTORY));
436 else if (global_settings.show_path_in_browser == SHOW_PATH_CURRENT)
438 char *title = strrchr(tc.currdir, '/') + 1;
439 if (*title == '\0')
441 /* Display "Files" for the root dir */
442 gui_synclist_set_title(&tree_lists, str(LANG_DIR_BROWSER),
443 filetype_get_icon(ATTR_DIRECTORY));
445 else
446 gui_synclist_set_title(&tree_lists, title,
447 filetype_get_icon(ATTR_DIRECTORY));
449 else
451 /* Must clear the title as the list is reused */
452 gui_synclist_set_title(&tree_lists, NULL, NOICON);
454 #endif
457 gui_synclist_set_nb_items(&tree_lists, tc.filesindir);
458 gui_synclist_set_icon_callback(&tree_lists, tree_get_fileicon);
459 if( tc.selected_item >= tc.filesindir)
460 tc.selected_item=tc.filesindir-1;
462 gui_synclist_select_item(&tree_lists, tc.selected_item);
463 #ifdef HAVE_BUTTONBAR
464 if (global_settings.buttonbar) {
465 if (*tc.dirfilter < NUM_FILTER_MODES)
466 gui_buttonbar_set(&tree_buttonbar, str(LANG_SYSFONT_DIRBROWSE_F1),
467 str(LANG_SYSFONT_DIRBROWSE_F2),
468 str(LANG_SYSFONT_DIRBROWSE_F3));
469 else
470 gui_buttonbar_set(&tree_buttonbar, "<<<", "", "");
471 gui_buttonbar_draw(&tree_buttonbar);
473 #endif
474 gui_synclist_draw(&tree_lists);
475 gui_synclist_speak_item(&tree_lists);
476 gui_syncstatusbar_draw(&statusbars, true);
477 return tc.filesindir;
480 /* load tracks from specified directory to resume play */
481 void resume_directory(const char *dir)
483 #ifdef HAVE_TAGCACHE
484 bool id3db = *tc.dirfilter == SHOW_ID3DB;
485 #endif
487 if (ft_load(&tc, dir) < 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 strncpy(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 unsigned 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 while(1) {
640 struct entry *dircache = tc.dircache;
641 bool restore = false;
642 if (tc.dirlevel < 0)
643 tc.dirlevel = 0; /* shouldnt be needed.. this code needs work! */
644 #ifdef BOOTFILE
645 if (boot_changed) {
646 static const char *lines[]={ID2P(LANG_BOOT_CHANGED), ID2P(LANG_REBOOT_NOW)};
647 static const struct text_message message={lines, 2};
648 if(gui_syncyesno_run(&message, NULL, NULL)==YESNO_YES)
649 rolo_load("/" BOOTFILE);
650 restore = true;
651 boot_changed = false;
653 #endif
654 button = get_action(CONTEXT_TREE,
655 list_do_action_timeout(&tree_lists, HZ/2));
656 oldbutton = button;
657 gui_synclist_do_button(&tree_lists, &button,LIST_WRAP_UNLESS_HELD);
658 tc.selected_item = gui_synclist_get_sel_pos(&tree_lists);
659 switch ( button ) {
660 case ACTION_STD_OK:
661 /* nothing to do if no files to display */
662 if ( numentries == 0 )
663 break;
665 #ifdef HAVE_TAGCACHE
666 switch (id3db?tagtree_enter(&tc):ft_enter(&tc))
667 #else
668 switch (ft_enter(&tc))
669 #endif
671 case 1: reload_dir = true; break;
672 case 2: start_wps = true; break;
673 case 3: exit_func = true; break;
674 default: break;
676 restore = true;
677 break;
679 case ACTION_STD_CANCEL:
680 if (*tc.dirfilter > NUM_FILTER_MODES && tc.dirlevel < 1) {
681 exit_func = true;
682 break;
684 if ((*tc.dirfilter == SHOW_ID3DB && tc.dirlevel == 0) ||
685 ((*tc.dirfilter != SHOW_ID3DB && !strcmp(currdir,"/"))))
687 #ifdef HAVE_LCD_BITMAP /* charcell doesnt have ACTION_TREE_PGLEFT so this isnt needed */
688 if (oldbutton == ACTION_TREE_PGLEFT)
689 break;
690 else
691 #endif
692 return GO_TO_ROOT;
695 #ifdef HAVE_TAGCACHE
696 if (id3db)
697 tagtree_exit(&tc);
698 else
699 #endif
700 if (ft_exit(&tc) == 3)
701 exit_func = true;
703 restore = true;
704 break;
706 case ACTION_TREE_STOP:
707 if (list_stop_handler())
708 restore = true;
709 break;
711 case ACTION_STD_MENU:
712 return GO_TO_ROOT;
713 break;
715 #ifdef HAVE_RECORDING
716 case ACTION_STD_REC:
717 return GO_TO_RECSCREEN;
718 #endif
720 case ACTION_TREE_WPS:
721 return GO_TO_PREVIOUS_MUSIC;
722 break;
723 #ifdef HAVE_QUICKSCREEN
724 case ACTION_STD_QUICKSCREEN:
725 /* don't enter f2 from plugin browser */
726 if (*tc.dirfilter < NUM_FILTER_MODES)
728 if (quick_screen_quick(button))
729 reload_dir = true;
730 restore = true;
732 break;
733 #endif
734 #ifdef BUTTON_F3
735 case ACTION_F3:
736 /* don't enter f3 from plugin browser */
737 if (*tc.dirfilter < NUM_FILTER_MODES)
739 if (quick_screen_f3(ACTION_F3))
740 reload_dir = true;
741 restore = true;
743 break;
744 #endif
746 case ACTION_STD_CONTEXT:
748 int onplay_result;
749 int attr = 0;
751 if(!numentries)
752 onplay_result = onplay(NULL, 0, curr_context);
753 else {
754 #ifdef HAVE_TAGCACHE
755 if (id3db)
757 if (tagtree_get_attr(&tc) == FILE_ATTR_AUDIO)
759 attr = FILE_ATTR_AUDIO;
760 tagtree_get_filename(&tc, buf, sizeof(buf));
762 else
763 attr = ATTR_DIRECTORY;
765 else
766 #endif
768 attr = dircache[tc.selected_item].attr;
770 if (currdir[1]) /* Not in / */
771 snprintf(buf, sizeof buf, "%s/%s",
772 currdir,
773 dircache[tc.selected_item].name);
774 else /* In / */
775 snprintf(buf, sizeof buf, "/%s",
776 dircache[tc.selected_item].name);
778 onplay_result = onplay(buf, attr, curr_context);
780 switch (onplay_result)
782 case ONPLAY_MAINMENU:
783 return GO_TO_ROOT;
785 case ONPLAY_OK:
786 restore = true;
787 break;
789 case ONPLAY_RELOAD_DIR:
790 reload_dir = true;
791 break;
793 case ONPLAY_START_PLAY:
794 return GO_TO_WPS;
795 break;
797 break;
800 case ACTION_NONE:
801 gui_syncstatusbar_draw(&statusbars, false);
802 break;
804 #ifdef HAVE_HOTSWAP
805 case SYS_FS_CHANGED:
806 #ifdef HAVE_TAGCACHE
807 if (!id3db)
808 #endif
809 reload_dir = true;
810 /* The 'dir no longer valid' situation will be caught later
811 * by checking the showdir() result. */
812 break;
813 #endif
815 default:
816 if (default_event_handler(button) == SYS_USB_CONNECTED)
818 if(*tc.dirfilter > NUM_FILTER_MODES)
819 /* leave sub-browsers after usb, doing otherwise
820 might be confusing to the user */
821 exit_func = true;
822 else
823 reload_dir = true;
825 break;
827 if (start_wps)
828 return GO_TO_WPS;
829 if (button)
831 storage_spin();
835 check_rescan:
836 /* do we need to rescan dir? */
837 if (reload_dir || reload_root ||
838 lastfilter != *tc.dirfilter ||
839 lastsortcase != global_settings.sort_case)
841 if (reload_root) {
842 strcpy(currdir, "/");
843 tc.dirlevel = 0;
844 #ifdef HAVE_TAGCACHE
845 tc.currtable = 0;
846 tc.currextra = 0;
847 lasttable = -1;
848 lastextra = -1;
849 #endif
850 reload_root = false;
853 if (!reload_dir)
855 gui_synclist_select_item(&tree_lists, 0);
856 gui_synclist_draw(&tree_lists);
857 tc.selected_item = 0;
858 lastdir[0] = 0;
861 lastfilter = *tc.dirfilter;
862 lastsortcase = global_settings.sort_case;
863 restore = true;
866 if (exit_func)
867 return GO_TO_PREVIOUS;
869 if (restore || reload_dir) {
870 /* restore display */
871 numentries = update_dir();
872 reload_dir = false;
873 if (currdir[1] && (numentries < 0))
874 { /* not in root and reload failed */
875 reload_root = true; /* try root */
876 goto check_rescan;
880 return true;
883 bool create_playlist(void)
885 char filename[MAX_PATH];
887 snprintf(filename, sizeof filename, "%s.m3u8",
888 tc.currdir[1] ? tc.currdir : "/root");
889 splashf(0, "%s %s", str(LANG_CREATING), filename);
891 trigger_cpu_boost();
892 catalog_add_to_a_playlist(tc.currdir, ATTR_DIRECTORY, true, filename);
893 cancel_cpu_boost();
895 return true;
898 int rockbox_browse(const char *root, int dirfilter)
900 int ret_val = 0;
901 int *last_filter = tc.dirfilter;
902 tc.dirfilter = &dirfilter;
903 tc.sort_dir = global_settings.sort_dir;
905 reload_dir = true;
906 if (dirfilter >= NUM_FILTER_MODES)
908 static struct tree_context backup;
909 int last_context;
911 backup = tc;
912 tc.selected_item = 0;
913 tc.dirlevel = 0;
914 memcpy(tc.currdir, root, sizeof(tc.currdir));
915 start_wps = false;
916 last_context = curr_context;
918 ret_val = dirbrowse();
919 tc = backup;
920 curr_context = last_context;
922 else
924 static char buf[MAX_PATH];
925 if (dirfilter != SHOW_ID3DB)
926 tc.dirfilter = &global_settings.dirfilter;
927 strcpy(buf,root);
928 set_current_file(buf);
929 ret_val = dirbrowse();
931 tc.dirfilter = last_filter;
932 return ret_val;
935 void tree_mem_init(void)
937 /* We copy the settings value in case it is changed by the user. We can't
938 use it until the next reboot. */
939 max_files = global_settings.max_files_in_dir;
941 /* initialize tree context struct */
942 memset(&tc, 0, sizeof(tc));
943 tc.dirfilter = &global_settings.dirfilter;
944 tc.sort_dir = global_settings.sort_dir;
946 tc.name_buffer_size = AVERAGE_FILENAME_LENGTH * max_files;
947 tc.name_buffer = buffer_alloc(tc.name_buffer_size);
949 tc.dircache_size = max_files * sizeof(struct entry);
950 tc.dircache = buffer_alloc(tc.dircache_size);
951 tree_get_filetypes(&filetypes, &filetypes_count);
954 bool bookmark_play(char *resume_file, int index, int offset, int seed,
955 char *filename)
957 int i;
958 char* suffix = strrchr(resume_file, '.');
959 bool started = false;
961 if (suffix != NULL &&
962 (!strcasecmp(suffix, ".m3u") || !strcasecmp(suffix, ".m3u8")))
964 /* Playlist playback */
965 char* slash;
966 /* check that the file exists */
967 if(!file_exists(resume_file))
968 return false;
970 slash = strrchr(resume_file,'/');
971 if (slash)
973 char* cp;
974 *slash=0;
976 cp=resume_file;
977 if (!cp[0])
978 cp="/";
980 if (playlist_create(cp, slash+1) != -1)
982 if (global_settings.playlist_shuffle)
983 playlist_shuffle(seed, -1);
984 playlist_start(index,offset);
985 started = true;
987 *slash='/';
990 else
992 /* Directory playback */
993 lastdir[0]='\0';
994 if (playlist_create(resume_file, NULL) != -1)
996 char* peek_filename;
997 resume_directory(resume_file);
998 if (global_settings.playlist_shuffle)
999 playlist_shuffle(seed, -1);
1001 /* Check if the file is at the same spot in the directory,
1002 else search for it */
1003 peek_filename = playlist_peek(index);
1005 if (peek_filename == NULL)
1006 return false;
1008 if (strcmp(strrchr(peek_filename, '/') + 1, filename))
1010 for ( i=0; i < playlist_amount(); i++ )
1012 peek_filename = playlist_peek(i);
1014 if (peek_filename == NULL)
1015 return false;
1017 if (!strcmp(strrchr(peek_filename, '/') + 1, filename))
1018 break;
1020 if (i < playlist_amount())
1021 index = i;
1022 else
1023 return false;
1025 playlist_start(index,offset);
1026 started = true;
1030 if (started)
1031 start_wps = true;
1032 return started;
1035 static void say_filetype(int attr)
1037 /* try to find a voice ID for the extension, if known */
1038 int j;
1039 attr &= FILE_ATTR_MASK; /* file type */
1040 for (j=0; j<filetypes_count; j++)
1041 if (attr == filetypes[j].tree_attr)
1043 talk_id(filetypes[j].voiceclip, true);
1044 return;
1048 static int ft_play_dirname(char* name)
1050 #if CONFIG_CODEC != SWCODEC
1051 if (audio_status() & AUDIO_STATUS_PLAY)
1052 return 0;
1053 #endif
1055 if(talk_file(tc.currdir, name, dir_thumbnail_name, NULL,
1056 NULL, false))
1058 if(global_settings.talk_filetype)
1059 talk_id(VOICE_DIR, true);
1060 return 1;
1062 else
1063 return -1;
1066 static void ft_play_filename(char *dir, char *file)
1068 #if CONFIG_CODEC != SWCODEC
1069 if (audio_status() & AUDIO_STATUS_PLAY)
1070 return;
1071 #endif
1073 if (strlen(file) >= strlen(file_thumbnail_ext)
1074 && strcasecmp(&file[strlen(file) - strlen(file_thumbnail_ext)],
1075 file_thumbnail_ext))
1076 /* file has no .talk extension */
1077 talk_file(dir, NULL, file, file_thumbnail_ext,
1078 NULL, false);
1079 else
1080 /* it already is a .talk file, play this directly, but prefix it. */
1081 talk_file(dir, NULL, file, NULL,
1082 TALK_IDARRAY(LANG_VOICE_DIR_HOVER), false);
1085 /* These two functions are called by the USB and shutdown handlers */
1086 void tree_flush(void)
1088 #ifdef HAVE_TAGCACHE
1089 tagcache_shutdown();
1090 #endif
1092 #ifdef HAVE_TC_RAMCACHE
1093 tagcache_unload_ramcache();
1094 #endif
1096 #ifdef HAVE_DIRCACHE
1098 int old_val = global_status.dircache_size;
1099 if (global_settings.dircache)
1101 if (!dircache_is_initializing())
1102 global_status.dircache_size = dircache_get_cache_size();
1103 # ifdef HAVE_EEPROM_SETTINGS
1104 if (firmware_settings.initialized)
1105 dircache_save();
1106 # endif
1107 dircache_disable();
1109 else
1111 global_status.dircache_size = 0;
1113 if (old_val != global_status.dircache_size)
1114 status_save();
1116 #endif
1119 void tree_restore(void)
1121 #ifdef HAVE_EEPROM_SETTINGS
1122 firmware_settings.disk_clean = false;
1123 #endif
1125 #ifdef HAVE_TC_RAMCACHE
1126 remove(TAGCACHE_STATEFILE);
1127 #endif
1129 #ifdef HAVE_DIRCACHE
1130 remove(DIRCACHE_FILE);
1131 if (global_settings.dircache)
1133 /* Print "Scanning disk..." to the display. */
1134 splash(0, str(LANG_SCANNING_DISK));
1136 dircache_build(global_status.dircache_size);
1138 #endif
1139 #ifdef HAVE_TAGCACHE
1140 tagcache_start_scan();
1141 #endif