Differentiate between shipped and runtime generated files with NEED_WRITE flag.
[kugel-rb.git] / apps / tree.c
blob9229b22b7a22ae0517d5db4f3c1490d0f30d8278
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 <stdlib.h>
23 #include <stdbool.h>
24 #include "string-extra.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 "audio.h"
36 #include "playlist.h"
37 #include "menu.h"
38 #include "skin_engine/skin_engine.h"
39 #include "settings.h"
40 #include "debug.h"
41 #include "storage.h"
42 #include "rolo.h"
43 #include "icons.h"
44 #include "lang.h"
45 #include "screens.h"
46 #include "keyboard.h"
47 #include "bookmark.h"
48 #include "onplay.h"
49 #include "buffer.h"
50 #include "power.h"
51 #include "action.h"
52 #include "talk.h"
53 #include "filetypes.h"
54 #include "misc.h"
55 #include "filetree.h"
56 #include "tagtree.h"
57 #ifdef HAVE_RECORDING
58 #include "recorder/recording.h"
59 #endif
60 #include "rtc.h"
61 #include "dircache.h"
62 #ifdef HAVE_TAGCACHE
63 #include "tagcache.h"
64 #endif
65 #include "yesno.h"
66 #include "eeprom_settings.h"
67 #include "playlist_catalog.h"
69 /* gui api */
70 #include "list.h"
71 #include "splash.h"
72 #include "buttonbar.h"
73 #include "quickscreen.h"
74 #include "appevents.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 static 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 char path[MAX_PATH];
264 if(!dir_exists(get_user_file_path(ROCKBOX_DIR, 0, path, sizeof(path))))
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,
299 global_settings.show_icons?&tree_get_fileicon:NULL);
300 #ifdef HAVE_LCD_COLOR
301 gui_synclist_set_color_callback(&tree_lists, &tree_get_filecolor);
302 #endif
306 /* drawer function for the GUI_EVENT_REDRAW callback */
307 void tree_drawlists(void)
309 /* band-aid to fix the bar/list redrawing properly after leaving a plugin */
310 send_event(GUI_EVENT_THEME_CHANGED, NULL);
311 /* end bandaid */
312 gui_synclist_draw(&tree_lists);
316 struct tree_context* tree_get_context(void)
318 return &tc;
322 * Returns the position of a given file in the current directory
323 * returns -1 if not found
325 static int tree_get_file_position(char * filename)
327 int i;
329 /* use lastfile to determine the selected item (default=0) */
330 for (i=0; i < tc.filesindir; i++)
332 struct entry* dc = tc.dircache;
333 struct entry* e = &dc[i];
334 if (!strcasecmp(e->name, filename))
335 return(i);
337 return(-1);/* no file can match, returns undefined */
341 * Called when a new dir is loaded (for example when returning from other apps ...)
342 * also completely redraws the tree
344 static int update_dir(void)
346 bool changed = false;
347 #ifdef HAVE_TAGCACHE
348 bool id3db = *tc.dirfilter == SHOW_ID3DB;
349 /* Checks for changes */
350 if (id3db) {
351 if (tc.currtable != lasttable ||
352 tc.currextra != lastextra ||
353 tc.firstpos != lastfirstpos ||
354 reload_dir)
356 if (tagtree_load(&tc) < 0)
357 return -1;
359 lasttable = tc.currtable;
360 lastextra = tc.currextra;
361 lastfirstpos = tc.firstpos;
362 changed = true;
365 else
366 #endif
368 /* if the tc.currdir has been changed, reload it ...*/
369 if (strncmp(tc.currdir, lastdir, sizeof(lastdir)) || reload_dir)
371 if (ft_load(&tc, NULL) < 0)
372 return -1;
373 strcpy(lastdir, tc.currdir);
374 changed = true;
377 /* if selected item is undefined */
378 if (tc.selected_item == -1)
380 /* use lastfile to determine the selected item */
381 tc.selected_item = tree_get_file_position(lastfile);
383 /* If the file doesn't exists, select the first one (default) */
384 if(tc.selected_item < 0)
385 tc.selected_item = 0;
386 changed = true;
388 if (changed)
391 #ifdef HAVE_TAGCACHE
392 !id3db &&
393 #endif
394 (tc.dirfull || tc.filesindir == global_settings.max_files_in_dir) )
396 splash(HZ, ID2P(LANG_SHOWDIR_BUFFER_FULL));
399 #ifdef HAVE_TAGCACHE
400 if (id3db)
402 #ifdef HAVE_LCD_BITMAP
403 if (global_settings.show_path_in_browser == SHOW_PATH_FULL
404 || global_settings.show_path_in_browser == SHOW_PATH_CURRENT)
406 gui_synclist_set_title(&tree_lists, tagtree_get_title(&tc),
407 filetype_get_icon(ATTR_DIRECTORY));
409 else
411 /* Must clear the title as the list is reused */
412 gui_synclist_set_title(&tree_lists, NULL, NOICON);
414 #endif
416 else
417 #endif
419 #ifdef HAVE_LCD_BITMAP
420 if (global_settings.show_path_in_browser &&
421 *(tc.dirfilter) == SHOW_PLUGINS)
423 char *title;
424 if (!strcmp(tc.currdir, PLUGIN_GAMES_DIR))
425 title = str(LANG_PLUGIN_GAMES);
426 else if (!strcmp(tc.currdir, PLUGIN_APPS_DIR))
427 title = str(LANG_PLUGIN_APPS);
428 else if (!strcmp(tc.currdir, PLUGIN_DEMOS_DIR))
429 title = str(LANG_PLUGIN_DEMOS);
430 else title = str(LANG_PLUGINS);
431 gui_synclist_set_title(&tree_lists, title, Icon_Plugin);
433 else if (global_settings.show_path_in_browser == SHOW_PATH_FULL)
435 gui_synclist_set_title(&tree_lists, tc.currdir,
436 filetype_get_icon(ATTR_DIRECTORY));
438 else if (global_settings.show_path_in_browser == SHOW_PATH_CURRENT)
440 char *title = strrchr(tc.currdir, '/') + 1;
441 if (*title == '\0')
443 /* Display "Files" for the root dir */
444 gui_synclist_set_title(&tree_lists, str(LANG_DIR_BROWSER),
445 filetype_get_icon(ATTR_DIRECTORY));
447 else
448 gui_synclist_set_title(&tree_lists, title,
449 filetype_get_icon(ATTR_DIRECTORY));
451 else
453 /* Must clear the title as the list is reused */
454 gui_synclist_set_title(&tree_lists, NULL, NOICON);
456 #endif
459 gui_synclist_set_nb_items(&tree_lists, tc.filesindir);
460 gui_synclist_set_icon_callback(&tree_lists,
461 global_settings.show_icons?tree_get_fileicon:NULL);
462 if( tc.selected_item >= tc.filesindir)
463 tc.selected_item=tc.filesindir-1;
465 gui_synclist_select_item(&tree_lists, tc.selected_item);
466 #ifdef HAVE_BUTTONBAR
467 if (global_settings.buttonbar) {
468 if (*tc.dirfilter < NUM_FILTER_MODES)
469 gui_buttonbar_set(&tree_buttonbar, str(LANG_SYSFONT_DIRBROWSE_F1),
470 str(LANG_SYSFONT_DIRBROWSE_F2),
471 str(LANG_SYSFONT_DIRBROWSE_F3));
472 else
473 gui_buttonbar_set(&tree_buttonbar, "<<<", "", "");
474 gui_buttonbar_draw(&tree_buttonbar);
476 #endif
477 gui_synclist_draw(&tree_lists);
478 gui_synclist_speak_item(&tree_lists);
479 return tc.filesindir;
482 /* load tracks from specified directory to resume play */
483 void resume_directory(const char *dir)
485 int dirfilter = *tc.dirfilter;
486 int ret;
487 #ifdef HAVE_TAGCACHE
488 bool id3db = *tc.dirfilter == SHOW_ID3DB;
489 #endif
490 /* make sure the dirfilter is sane. The only time it should be possible
491 * thats its not is when resume playlist is called from a plugin
493 #ifdef HAVE_TAGCACHE
494 if (!id3db)
495 #endif
496 *tc.dirfilter = global_settings.dirfilter;
497 ret = ft_load(&tc, dir);
498 *tc.dirfilter = dirfilter;
499 if (ret < 0)
500 return;
501 lastdir[0] = 0;
503 ft_build_playlist(&tc, 0);
505 #ifdef HAVE_TAGCACHE
506 if (id3db)
507 tagtree_load(&tc);
508 #endif
511 /* Returns the current working directory and also writes cwd to buf if
512 non-NULL. In case of error, returns NULL. */
513 char *getcwd(char *buf, int size)
515 if (!buf)
516 return tc.currdir;
517 else if (size > 0)
519 strlcpy(buf, tc.currdir, size);
520 return buf;
522 else
523 return NULL;
526 /* Force a reload of the directory next time directory browser is called */
527 void reload_directory(void)
529 reload_dir = true;
532 void get_current_file(char* buffer, int buffer_len)
534 #ifdef HAVE_TAGCACHE
535 /* in ID3DB mode it is a bad idea to call this function */
536 /* (only happens with `follow playlist') */
537 if( *tc.dirfilter == SHOW_ID3DB )
538 return;
539 #endif
541 struct entry* dc = tc.dircache;
542 struct entry* e = &dc[tc.selected_item];
543 snprintf(buffer, buffer_len, "%s/%s", getcwd(NULL,0),
544 tc.dirlength ? e->name : "");
547 /* Allow apps to change our dirfilter directly (required for sub browsers)
548 if they're suddenly going to become a file browser for example */
549 void set_dirfilter(int l_dirfilter)
551 *tc.dirfilter = l_dirfilter;
554 /* Selects a file and update tree context properly */
555 void set_current_file(char *path)
557 char *name;
558 int i;
560 #ifdef HAVE_TAGCACHE
561 /* in ID3DB mode it is a bad idea to call this function */
562 /* (only happens with `follow playlist') */
563 if( *tc.dirfilter == SHOW_ID3DB )
564 return;
565 #endif
567 /* separate directory from filename */
568 /* gets the directory's name and put it into tc.currdir */
569 name = strrchr(path+1,'/');
570 if (name)
572 *name = 0;
573 strcpy(tc.currdir, path);
574 *name = '/';
575 name++;
577 else
579 strcpy(tc.currdir, "/");
580 name = path+1;
583 strcpy(lastfile, name);
586 /* If we changed dir we must recalculate the dirlevel
587 and adjust the selected history properly */
588 if (strncmp(tc.currdir,lastdir,sizeof(lastdir)))
590 tc.dirlevel = 0;
591 tc.selected_item_history[tc.dirlevel] = -1;
593 /* use '/' to calculate dirlevel */
594 for (i = 1; path[i] != '\0'; i++)
596 if (path[i] == '/')
598 tc.dirlevel++;
599 tc.selected_item_history[tc.dirlevel] = -1;
603 if (ft_load(&tc, NULL) >= 0)
605 tc.selected_item = tree_get_file_position(lastfile);
610 /* main loop, handles key events */
611 static int dirbrowse()
613 int numentries=0;
614 char buf[MAX_PATH];
615 int button, oldbutton;
616 bool reload_root = false;
617 int lastfilter = *tc.dirfilter;
618 bool lastsortcase = global_settings.sort_case;
619 bool exit_func = false;
621 char* currdir = tc.currdir; /* just a shortcut */
622 #ifdef HAVE_TAGCACHE
623 bool id3db = *tc.dirfilter == SHOW_ID3DB;
625 if (id3db)
626 curr_context=CONTEXT_ID3DB;
627 else
628 #endif
629 curr_context=CONTEXT_TREE;
630 if (tc.selected_item < 0)
631 tc.selected_item = 0;
632 #ifdef HAVE_TAGCACHE
633 tc.firstpos = 0;
634 lasttable = -1;
635 lastextra = -1;
636 lastfirstpos = 0;
637 #endif
639 start_wps = false;
640 numentries = update_dir();
641 reload_dir = false;
642 if (numentries == -1)
643 return GO_TO_PREVIOUS; /* currdir is not a directory */
645 if (*tc.dirfilter > NUM_FILTER_MODES && numentries==0)
647 splash(HZ*2, ID2P(LANG_NO_FILES));
648 return GO_TO_PREVIOUS; /* No files found for rockbox_browser() */
651 gui_synclist_draw(&tree_lists);
652 while(1) {
653 struct entry *dircache = tc.dircache;
654 bool restore = false;
655 if (tc.dirlevel < 0)
656 tc.dirlevel = 0; /* shouldnt be needed.. this code needs work! */
657 #ifdef BOOTFILE
658 if (boot_changed) {
659 static const char *lines[]={ID2P(LANG_BOOT_CHANGED), ID2P(LANG_REBOOT_NOW)};
660 static const struct text_message message={lines, 2};
661 if(gui_syncyesno_run(&message, NULL, NULL)==YESNO_YES)
662 rolo_load("/" BOOTFILE);
663 restore = true;
664 boot_changed = false;
666 #endif
667 button = get_action(CONTEXT_TREE,
668 list_do_action_timeout(&tree_lists, HZ/2));
669 oldbutton = button;
670 gui_synclist_do_button(&tree_lists, &button,LIST_WRAP_UNLESS_HELD);
671 tc.selected_item = gui_synclist_get_sel_pos(&tree_lists);
672 switch ( button ) {
673 case ACTION_STD_OK:
674 /* nothing to do if no files to display */
675 if ( numentries == 0 )
676 break;
678 #ifdef HAVE_TAGCACHE
679 switch (id3db?tagtree_enter(&tc):ft_enter(&tc))
680 #else
681 switch (ft_enter(&tc))
682 #endif
684 case 1: reload_dir = true; break;
685 case 2: start_wps = true; break;
686 case 3: exit_func = true; break;
687 default: break;
689 restore = true;
690 break;
692 case ACTION_STD_CANCEL:
693 if (*tc.dirfilter > NUM_FILTER_MODES && tc.dirlevel < 1) {
694 exit_func = true;
695 break;
697 if ((*tc.dirfilter == SHOW_ID3DB && tc.dirlevel == 0) ||
698 ((*tc.dirfilter != SHOW_ID3DB && !strcmp(currdir,"/"))))
700 #ifdef HAVE_LCD_BITMAP /* charcell doesnt have ACTION_TREE_PGLEFT so this isnt needed */
701 if (oldbutton == ACTION_TREE_PGLEFT)
702 break;
703 else
704 #endif
705 return GO_TO_ROOT;
708 #ifdef HAVE_TAGCACHE
709 if (id3db)
710 tagtree_exit(&tc);
711 else
712 #endif
713 if (ft_exit(&tc) == 3)
714 exit_func = true;
716 restore = true;
717 break;
719 case ACTION_TREE_STOP:
720 if (list_stop_handler())
721 restore = true;
722 break;
724 case ACTION_STD_MENU:
725 return GO_TO_ROOT;
726 break;
728 #ifdef HAVE_RECORDING
729 case ACTION_STD_REC:
730 return GO_TO_RECSCREEN;
731 #endif
733 case ACTION_TREE_WPS:
734 return GO_TO_PREVIOUS_MUSIC;
735 break;
736 #ifdef HAVE_QUICKSCREEN
737 case ACTION_STD_QUICKSCREEN:
738 /* don't enter f2 from plugin browser */
739 if (*tc.dirfilter < NUM_FILTER_MODES)
741 if (quick_screen_quick(button))
742 reload_dir = true;
743 restore = true;
745 break;
746 #endif
747 #ifdef BUTTON_F3
748 case ACTION_F3:
749 /* don't enter f3 from plugin browser */
750 if (*tc.dirfilter < NUM_FILTER_MODES)
752 if (quick_screen_f3(ACTION_F3))
753 reload_dir = true;
754 restore = true;
756 break;
757 #endif
759 #ifdef HAVE_HOTKEY
760 case ACTION_TREE_HOTKEY:
761 if (!global_settings.hotkey_tree)
762 break;
763 /* fall through */
764 #endif
765 case ACTION_STD_CONTEXT:
767 bool hotkey = button == ACTION_TREE_HOTKEY;
768 int onplay_result;
769 int attr = 0;
771 if(!numentries)
772 onplay_result = onplay(NULL, 0, curr_context, hotkey);
773 else {
774 #ifdef HAVE_TAGCACHE
775 if (id3db)
777 if (tagtree_get_attr(&tc) == FILE_ATTR_AUDIO)
779 attr = FILE_ATTR_AUDIO;
780 tagtree_get_filename(&tc, buf, sizeof(buf));
782 else
783 attr = ATTR_DIRECTORY;
785 else
786 #endif
788 attr = dircache[tc.selected_item].attr;
790 if (currdir[1]) /* Not in / */
791 snprintf(buf, sizeof buf, "%s/%s",
792 currdir,
793 dircache[tc.selected_item].name);
794 else /* In / */
795 snprintf(buf, sizeof buf, "/%s",
796 dircache[tc.selected_item].name);
798 onplay_result = onplay(buf, attr, curr_context, hotkey);
800 switch (onplay_result)
802 case ONPLAY_MAINMENU:
803 return GO_TO_ROOT;
805 case ONPLAY_OK:
806 restore = true;
807 break;
809 case ONPLAY_RELOAD_DIR:
810 reload_dir = true;
811 break;
813 case ONPLAY_START_PLAY:
814 return GO_TO_WPS;
815 break;
817 break;
820 #ifdef HAVE_HOTSWAP
821 case SYS_FS_CHANGED:
822 #ifdef HAVE_TAGCACHE
823 if (!id3db)
824 #endif
825 reload_dir = true;
826 /* The 'dir no longer valid' situation will be caught later
827 * by checking the showdir() result. */
828 break;
829 #endif
831 default:
832 if (default_event_handler(button) == SYS_USB_CONNECTED)
834 if(*tc.dirfilter > NUM_FILTER_MODES)
835 /* leave sub-browsers after usb, doing otherwise
836 might be confusing to the user */
837 exit_func = true;
838 else
839 reload_dir = true;
841 break;
843 if (start_wps)
844 return GO_TO_WPS;
845 if (button && !IS_SYSEVENT(button))
847 storage_spin();
851 check_rescan:
852 /* do we need to rescan dir? */
853 if (reload_dir || reload_root ||
854 lastfilter != *tc.dirfilter ||
855 lastsortcase != global_settings.sort_case)
857 if (reload_root) {
858 strcpy(currdir, "/");
859 tc.dirlevel = 0;
860 #ifdef HAVE_TAGCACHE
861 tc.currtable = 0;
862 tc.currextra = 0;
863 lasttable = -1;
864 lastextra = -1;
865 #endif
866 reload_root = false;
869 if (!reload_dir)
871 gui_synclist_select_item(&tree_lists, 0);
872 gui_synclist_draw(&tree_lists);
873 tc.selected_item = 0;
874 lastdir[0] = 0;
877 lastfilter = *tc.dirfilter;
878 lastsortcase = global_settings.sort_case;
879 restore = true;
882 if (exit_func)
883 return GO_TO_PREVIOUS;
885 if (restore || reload_dir) {
886 /* restore display */
887 numentries = update_dir();
888 reload_dir = false;
889 if (currdir[1] && (numentries < 0))
890 { /* not in root and reload failed */
891 reload_root = true; /* try root */
892 goto check_rescan;
896 return true;
899 bool create_playlist(void)
901 char filename[MAX_PATH];
903 snprintf(filename, sizeof filename, "%s.m3u8",
904 tc.currdir[1] ? tc.currdir : "/root");
905 splashf(0, "%s %s", str(LANG_CREATING), filename);
907 trigger_cpu_boost();
908 catalog_add_to_a_playlist(tc.currdir, ATTR_DIRECTORY, true, filename);
909 cancel_cpu_boost();
911 return true;
914 int rockbox_browse(const char *root, int dirfilter)
916 int ret_val = 0;
917 int *last_filter = tc.dirfilter;
918 tc.dirfilter = &dirfilter;
919 tc.sort_dir = global_settings.sort_dir;
921 reload_dir = true;
922 if (dirfilter >= NUM_FILTER_MODES)
924 static struct tree_context backup;
925 char current[MAX_PATH];
926 int last_context;
928 backup = tc;
929 tc.selected_item = 0;
930 tc.dirlevel = 0;
931 memcpy(tc.currdir, root, sizeof(tc.currdir));
932 start_wps = false;
933 last_context = curr_context;
935 /* Center on the currently loaded language when browsing languages. */
936 if (dirfilter == SHOW_LNG)
938 if (global_settings.lang_file[0])
940 snprintf(current, sizeof(current), LANG_DIR "/%s.lng",
941 global_settings.lang_file);
943 else
945 strlcpy(current, LANG_DIR "/english.lng", sizeof(current));
948 /* Center on currently loaded WPS */
949 else if (dirfilter == SHOW_WPS)
951 snprintf(current, sizeof(current), WPS_DIR "/%s.wps",
952 global_settings.wps_file);
954 #ifdef HAVE_REMOTE_LCD
955 /* Center on currently loaded RWPS */
956 else if (dirfilter == SHOW_RWPS)
958 snprintf(current, sizeof(current), WPS_DIR "/%s.rwps",
959 global_settings.rwps_file);
961 else if (dirfilter == SHOW_RSBS)
963 snprintf(current, sizeof(current), SBS_DIR "/%s.rsbs",
964 global_settings.rsbs_file);
966 #endif
967 #ifdef HAVE_LCD_BITMAP
968 /* Center on the currently loaded font when browsing fonts */
969 else if (dirfilter == SHOW_FONT)
971 snprintf(current, sizeof(current), FONT_DIR "/%s.fnt",
972 global_settings.font_file);
974 else if (dirfilter == SHOW_SBS)
976 snprintf(current, sizeof(current), SBS_DIR "/%s.sbs",
977 global_settings.sbs_file);
979 #endif
980 #if CONFIG_TUNER
981 /* Center on the currently loaded FM preset when browsing those */
982 else if (dirfilter == SHOW_FMR)
984 snprintf(current, sizeof(current), FMPRESET_PATH "/%s.fmr",
985 global_settings.fmr_file);
987 #endif
988 else /* reset current[] */
989 current[0] = '\0';
991 /* If we've found a file to center on, do it */
992 if (current[0] == '/')
994 set_current_file(current);
995 /* set_current_file changes dirlevel, change it back */
996 tc.dirlevel = 0;
999 ret_val = dirbrowse();
1000 tc = backup;
1001 curr_context = last_context;
1003 else
1005 static char buf[MAX_PATH];
1006 if (dirfilter != SHOW_ID3DB)
1007 tc.dirfilter = &global_settings.dirfilter;
1008 strcpy(buf,root);
1009 set_current_file(buf);
1010 ret_val = dirbrowse();
1012 tc.dirfilter = last_filter;
1013 return ret_val;
1016 void tree_mem_init(void)
1018 /* We copy the settings value in case it is changed by the user. We can't
1019 use it until the next reboot. */
1020 max_files = global_settings.max_files_in_dir;
1022 /* initialize tree context struct */
1023 memset(&tc, 0, sizeof(tc));
1024 tc.dirfilter = &global_settings.dirfilter;
1025 tc.sort_dir = global_settings.sort_dir;
1027 tc.name_buffer_size = AVERAGE_FILENAME_LENGTH * max_files;
1028 tc.name_buffer = buffer_alloc(tc.name_buffer_size);
1030 tc.dircache_size = max_files * sizeof(struct entry);
1031 tc.dircache = buffer_alloc(tc.dircache_size);
1032 tree_get_filetypes(&filetypes, &filetypes_count);
1035 bool bookmark_play(char *resume_file, int index, int offset, int seed,
1036 char *filename)
1038 int i;
1039 char* suffix = strrchr(resume_file, '.');
1040 bool started = false;
1042 if (suffix != NULL &&
1043 (!strcasecmp(suffix, ".m3u") || !strcasecmp(suffix, ".m3u8")))
1045 /* Playlist playback */
1046 char* slash;
1047 /* check that the file exists */
1048 if(!file_exists(resume_file))
1049 return false;
1051 slash = strrchr(resume_file,'/');
1052 if (slash)
1054 char* cp;
1055 *slash=0;
1057 cp=resume_file;
1058 if (!cp[0])
1059 cp="/";
1061 if (playlist_create(cp, slash+1) != -1)
1063 if (global_settings.playlist_shuffle)
1064 playlist_shuffle(seed, -1);
1065 playlist_start(index,offset);
1066 started = true;
1068 *slash='/';
1071 else
1073 /* Directory playback */
1074 lastdir[0]='\0';
1075 if (playlist_create(resume_file, NULL) != -1)
1077 const char* peek_filename;
1078 resume_directory(resume_file);
1079 if (global_settings.playlist_shuffle)
1080 playlist_shuffle(seed, -1);
1082 /* Check if the file is at the same spot in the directory,
1083 else search for it */
1084 peek_filename = playlist_peek(index);
1086 if (peek_filename == NULL)
1088 /* playlist has shrunk, search from the top */
1089 index = 0;
1090 peek_filename = playlist_peek(index);
1091 if (peek_filename == NULL)
1092 return false;
1095 if (strcmp(strrchr(peek_filename, '/') + 1, filename))
1097 for ( i=0; i < playlist_amount(); i++ )
1099 peek_filename = playlist_peek(i);
1101 if (peek_filename == NULL)
1102 return false;
1104 if (!strcmp(strrchr(peek_filename, '/') + 1, filename))
1105 break;
1107 if (i < playlist_amount())
1108 index = i;
1109 else
1110 return false;
1112 playlist_start(index,offset);
1113 started = true;
1117 if (started)
1118 start_wps = true;
1119 return started;
1122 static void say_filetype(int attr)
1124 /* try to find a voice ID for the extension, if known */
1125 int j;
1126 attr &= FILE_ATTR_MASK; /* file type */
1127 for (j=0; j<filetypes_count; j++)
1128 if (attr == filetypes[j].tree_attr)
1130 talk_id(filetypes[j].voiceclip, true);
1131 return;
1135 static int ft_play_dirname(char* name)
1137 #if CONFIG_CODEC != SWCODEC
1138 if (audio_status() & AUDIO_STATUS_PLAY)
1139 return 0;
1140 #endif
1142 if(talk_file(tc.currdir, name, dir_thumbnail_name, NULL,
1143 NULL, false))
1145 if(global_settings.talk_filetype)
1146 talk_id(VOICE_DIR, true);
1147 return 1;
1149 else
1150 return -1;
1153 static void ft_play_filename(char *dir, char *file)
1155 #if CONFIG_CODEC != SWCODEC
1156 if (audio_status() & AUDIO_STATUS_PLAY)
1157 return;
1158 #endif
1160 if (strlen(file) >= strlen(file_thumbnail_ext)
1161 && strcasecmp(&file[strlen(file) - strlen(file_thumbnail_ext)],
1162 file_thumbnail_ext))
1163 /* file has no .talk extension */
1164 talk_file(dir, NULL, file, file_thumbnail_ext,
1165 NULL, false);
1166 else
1167 /* it already is a .talk file, play this directly, but prefix it. */
1168 talk_file(dir, NULL, file, NULL,
1169 TALK_IDARRAY(LANG_VOICE_DIR_HOVER), false);
1172 /* These two functions are called by the USB and shutdown handlers */
1173 void tree_flush(void)
1175 #ifdef HAVE_TAGCACHE
1176 tagcache_shutdown();
1177 #endif
1179 #ifdef HAVE_TC_RAMCACHE
1180 tagcache_unload_ramcache();
1181 #endif
1183 #ifdef HAVE_DIRCACHE
1185 int old_val = global_status.dircache_size;
1186 if (global_settings.dircache)
1188 if (!dircache_is_initializing())
1189 global_status.dircache_size = dircache_get_cache_size();
1190 # ifdef HAVE_EEPROM_SETTINGS
1191 if (firmware_settings.initialized)
1192 dircache_save();
1193 # endif
1194 dircache_disable();
1196 else
1198 global_status.dircache_size = 0;
1200 if (old_val != global_status.dircache_size)
1201 status_save();
1203 #endif
1206 void tree_restore(void)
1208 #ifdef HAVE_EEPROM_SETTINGS
1209 firmware_settings.disk_clean = false;
1210 #endif
1212 #ifdef HAVE_TC_RAMCACHE
1213 remove(TAGCACHE_STATEFILE);
1214 #endif
1216 #ifdef HAVE_DIRCACHE
1217 remove(DIRCACHE_FILE);
1218 if (global_settings.dircache)
1220 /* Print "Scanning disk..." to the display. */
1221 splash(0, str(LANG_SCANNING_DISK));
1223 dircache_build(global_status.dircache_size);
1225 #endif
1226 #ifdef HAVE_TAGCACHE
1227 tagcache_start_scan();
1228 #endif