Fix FS#111395. Call settings_apply_skins() after changing theme colours.
[maemo-rb.git] / apps / tree.c
blob97ddfbb44d117dae515943e91c38c8baace2f476
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 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,
298 global_settings.show_icons?&tree_get_fileicon:NULL);
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,
460 global_settings.show_icons?tree_get_fileicon:NULL);
461 if( tc.selected_item >= tc.filesindir)
462 tc.selected_item=tc.filesindir-1;
464 gui_synclist_select_item(&tree_lists, tc.selected_item);
465 #ifdef HAVE_BUTTONBAR
466 if (global_settings.buttonbar) {
467 if (*tc.dirfilter < NUM_FILTER_MODES)
468 gui_buttonbar_set(&tree_buttonbar, str(LANG_SYSFONT_DIRBROWSE_F1),
469 str(LANG_SYSFONT_DIRBROWSE_F2),
470 str(LANG_SYSFONT_DIRBROWSE_F3));
471 else
472 gui_buttonbar_set(&tree_buttonbar, "<<<", "", "");
473 gui_buttonbar_draw(&tree_buttonbar);
475 #endif
476 gui_synclist_draw(&tree_lists);
477 gui_synclist_speak_item(&tree_lists);
478 return tc.filesindir;
481 /* load tracks from specified directory to resume play */
482 void resume_directory(const char *dir)
484 int dirfilter = *tc.dirfilter;
485 int ret;
486 #ifdef HAVE_TAGCACHE
487 bool id3db = *tc.dirfilter == SHOW_ID3DB;
488 #endif
489 /* make sure the dirfilter is sane. The only time it should be possible
490 * thats its not is when resume playlist is called from a plugin
492 #ifdef HAVE_TAGCACHE
493 if (!id3db)
494 #endif
495 *tc.dirfilter = global_settings.dirfilter;
496 ret = ft_load(&tc, dir);
497 *tc.dirfilter = dirfilter;
498 if (ret < 0)
499 return;
500 lastdir[0] = 0;
502 ft_build_playlist(&tc, 0);
504 #ifdef HAVE_TAGCACHE
505 if (id3db)
506 tagtree_load(&tc);
507 #endif
510 /* Returns the current working directory and also writes cwd to buf if
511 non-NULL. In case of error, returns NULL. */
512 char *getcwd(char *buf, int size)
514 if (!buf)
515 return tc.currdir;
516 else if (size > 0)
518 strlcpy(buf, tc.currdir, size);
519 return buf;
521 else
522 return NULL;
525 /* Force a reload of the directory next time directory browser is called */
526 void reload_directory(void)
528 reload_dir = true;
531 void get_current_file(char* buffer, int buffer_len)
533 #ifdef HAVE_TAGCACHE
534 /* in ID3DB mode it is a bad idea to call this function */
535 /* (only happens with `follow playlist') */
536 if( *tc.dirfilter == SHOW_ID3DB )
537 return;
538 #endif
540 struct entry* dc = tc.dircache;
541 struct entry* e = &dc[tc.selected_item];
542 snprintf(buffer, buffer_len, "%s/%s", getcwd(NULL,0),
543 tc.dirlength ? e->name : "");
546 /* Allow apps to change our dirfilter directly (required for sub browsers)
547 if they're suddenly going to become a file browser for example */
548 void set_dirfilter(int l_dirfilter)
550 *tc.dirfilter = l_dirfilter;
553 /* Selects a file and update tree context properly */
554 void set_current_file(char *path)
556 char *name;
557 int i;
559 #ifdef HAVE_TAGCACHE
560 /* in ID3DB mode it is a bad idea to call this function */
561 /* (only happens with `follow playlist') */
562 if( *tc.dirfilter == SHOW_ID3DB )
563 return;
564 #endif
566 /* separate directory from filename */
567 /* gets the directory's name and put it into tc.currdir */
568 name = strrchr(path+1,'/');
569 if (name)
571 *name = 0;
572 strcpy(tc.currdir, path);
573 *name = '/';
574 name++;
576 else
578 strcpy(tc.currdir, "/");
579 name = path+1;
582 strcpy(lastfile, name);
585 /* If we changed dir we must recalculate the dirlevel
586 and adjust the selected history properly */
587 if (strncmp(tc.currdir,lastdir,sizeof(lastdir)))
589 tc.dirlevel = 0;
590 tc.selected_item_history[tc.dirlevel] = -1;
592 /* use '/' to calculate dirlevel */
593 for (i = 1; path[i] != '\0'; i++)
595 if (path[i] == '/')
597 tc.dirlevel++;
598 tc.selected_item_history[tc.dirlevel] = -1;
602 if (ft_load(&tc, NULL) >= 0)
604 tc.selected_item = tree_get_file_position(lastfile);
609 /* main loop, handles key events */
610 static int dirbrowse()
612 int numentries=0;
613 char buf[MAX_PATH];
614 int button, oldbutton;
615 bool reload_root = false;
616 int lastfilter = *tc.dirfilter;
617 bool lastsortcase = global_settings.sort_case;
618 bool exit_func = false;
620 char* currdir = tc.currdir; /* just a shortcut */
621 #ifdef HAVE_TAGCACHE
622 bool id3db = *tc.dirfilter == SHOW_ID3DB;
624 if (id3db)
625 curr_context=CONTEXT_ID3DB;
626 else
627 #endif
628 curr_context=CONTEXT_TREE;
629 if (tc.selected_item < 0)
630 tc.selected_item = 0;
631 #ifdef HAVE_TAGCACHE
632 tc.firstpos = 0;
633 lasttable = -1;
634 lastextra = -1;
635 lastfirstpos = 0;
636 #endif
638 start_wps = false;
639 numentries = update_dir();
640 reload_dir = false;
641 if (numentries == -1)
642 return GO_TO_PREVIOUS; /* currdir is not a directory */
644 if (*tc.dirfilter > NUM_FILTER_MODES && numentries==0)
646 splash(HZ*2, ID2P(LANG_NO_FILES));
647 return GO_TO_PREVIOUS; /* No files found for rockbox_browser() */
650 gui_synclist_draw(&tree_lists);
651 while(1) {
652 struct entry *dircache = tc.dircache;
653 bool restore = false;
654 if (tc.dirlevel < 0)
655 tc.dirlevel = 0; /* shouldnt be needed.. this code needs work! */
656 #ifdef BOOTFILE
657 if (boot_changed) {
658 static const char *lines[]={ID2P(LANG_BOOT_CHANGED), ID2P(LANG_REBOOT_NOW)};
659 static const struct text_message message={lines, 2};
660 if(gui_syncyesno_run(&message, NULL, NULL)==YESNO_YES)
661 rolo_load("/" BOOTFILE);
662 restore = true;
663 boot_changed = false;
665 #endif
666 button = get_action(CONTEXT_TREE,
667 list_do_action_timeout(&tree_lists, HZ/2));
668 oldbutton = button;
669 gui_synclist_do_button(&tree_lists, &button,LIST_WRAP_UNLESS_HELD);
670 tc.selected_item = gui_synclist_get_sel_pos(&tree_lists);
671 switch ( button ) {
672 case ACTION_STD_OK:
673 /* nothing to do if no files to display */
674 if ( numentries == 0 )
675 break;
677 #ifdef HAVE_TAGCACHE
678 switch (id3db?tagtree_enter(&tc):ft_enter(&tc))
679 #else
680 switch (ft_enter(&tc))
681 #endif
683 case 1: reload_dir = true; break;
684 case 2: start_wps = true; break;
685 case 3: exit_func = true; break;
686 default: break;
688 restore = true;
689 break;
691 case ACTION_STD_CANCEL:
692 if (*tc.dirfilter > NUM_FILTER_MODES && tc.dirlevel < 1) {
693 exit_func = true;
694 break;
696 if ((*tc.dirfilter == SHOW_ID3DB && tc.dirlevel == 0) ||
697 ((*tc.dirfilter != SHOW_ID3DB && !strcmp(currdir,"/"))))
699 #ifdef HAVE_LCD_BITMAP /* charcell doesnt have ACTION_TREE_PGLEFT so this isnt needed */
700 if (oldbutton == ACTION_TREE_PGLEFT)
701 break;
702 else
703 #endif
704 return GO_TO_ROOT;
707 #ifdef HAVE_TAGCACHE
708 if (id3db)
709 tagtree_exit(&tc);
710 else
711 #endif
712 if (ft_exit(&tc) == 3)
713 exit_func = true;
715 restore = true;
716 break;
718 case ACTION_TREE_STOP:
719 if (list_stop_handler())
720 restore = true;
721 break;
723 case ACTION_STD_MENU:
724 return GO_TO_ROOT;
725 break;
727 #ifdef HAVE_RECORDING
728 case ACTION_STD_REC:
729 return GO_TO_RECSCREEN;
730 #endif
732 case ACTION_TREE_WPS:
733 return GO_TO_PREVIOUS_MUSIC;
734 break;
735 #ifdef HAVE_QUICKSCREEN
736 case ACTION_STD_QUICKSCREEN:
737 /* don't enter f2 from plugin browser */
738 if (*tc.dirfilter < NUM_FILTER_MODES)
740 if (quick_screen_quick(button))
741 reload_dir = true;
742 restore = true;
744 break;
745 #endif
746 #ifdef BUTTON_F3
747 case ACTION_F3:
748 /* don't enter f3 from plugin browser */
749 if (*tc.dirfilter < NUM_FILTER_MODES)
751 if (quick_screen_f3(ACTION_F3))
752 reload_dir = true;
753 restore = true;
755 break;
756 #endif
758 #ifdef HAVE_HOTKEY
759 case ACTION_TREE_HOTKEY:
760 if (!global_settings.hotkey_tree)
761 break;
762 /* fall through */
763 #endif
764 case ACTION_STD_CONTEXT:
766 bool hotkey = button == ACTION_TREE_HOTKEY;
767 int onplay_result;
768 int attr = 0;
770 if(!numentries)
771 onplay_result = onplay(NULL, 0, curr_context, hotkey);
772 else {
773 #ifdef HAVE_TAGCACHE
774 if (id3db)
776 if (tagtree_get_attr(&tc) == FILE_ATTR_AUDIO)
778 attr = FILE_ATTR_AUDIO;
779 tagtree_get_filename(&tc, buf, sizeof(buf));
781 else
782 attr = ATTR_DIRECTORY;
784 else
785 #endif
787 attr = dircache[tc.selected_item].attr;
789 if (currdir[1]) /* Not in / */
790 snprintf(buf, sizeof buf, "%s/%s",
791 currdir,
792 dircache[tc.selected_item].name);
793 else /* In / */
794 snprintf(buf, sizeof buf, "/%s",
795 dircache[tc.selected_item].name);
797 onplay_result = onplay(buf, attr, curr_context, hotkey);
799 switch (onplay_result)
801 case ONPLAY_MAINMENU:
802 return GO_TO_ROOT;
804 case ONPLAY_OK:
805 restore = true;
806 break;
808 case ONPLAY_RELOAD_DIR:
809 reload_dir = true;
810 break;
812 case ONPLAY_START_PLAY:
813 return GO_TO_WPS;
814 break;
816 break;
819 #ifdef HAVE_HOTSWAP
820 case SYS_FS_CHANGED:
821 #ifdef HAVE_TAGCACHE
822 if (!id3db)
823 #endif
824 reload_dir = true;
825 /* The 'dir no longer valid' situation will be caught later
826 * by checking the showdir() result. */
827 break;
828 #endif
830 default:
831 if (default_event_handler(button) == SYS_USB_CONNECTED)
833 if(*tc.dirfilter > NUM_FILTER_MODES)
834 /* leave sub-browsers after usb, doing otherwise
835 might be confusing to the user */
836 exit_func = true;
837 else
838 reload_dir = true;
840 break;
842 if (start_wps)
843 return GO_TO_WPS;
844 if (button && !IS_SYSEVENT(button))
846 storage_spin();
850 check_rescan:
851 /* do we need to rescan dir? */
852 if (reload_dir || reload_root ||
853 lastfilter != *tc.dirfilter ||
854 lastsortcase != global_settings.sort_case)
856 if (reload_root) {
857 strcpy(currdir, "/");
858 tc.dirlevel = 0;
859 #ifdef HAVE_TAGCACHE
860 tc.currtable = 0;
861 tc.currextra = 0;
862 lasttable = -1;
863 lastextra = -1;
864 #endif
865 reload_root = false;
868 if (!reload_dir)
870 gui_synclist_select_item(&tree_lists, 0);
871 gui_synclist_draw(&tree_lists);
872 tc.selected_item = 0;
873 lastdir[0] = 0;
876 lastfilter = *tc.dirfilter;
877 lastsortcase = global_settings.sort_case;
878 restore = true;
881 if (exit_func)
882 return GO_TO_PREVIOUS;
884 if (restore || reload_dir) {
885 /* restore display */
886 numentries = update_dir();
887 reload_dir = false;
888 if (currdir[1] && (numentries < 0))
889 { /* not in root and reload failed */
890 reload_root = true; /* try root */
891 goto check_rescan;
895 return true;
898 bool create_playlist(void)
900 char filename[MAX_PATH];
902 snprintf(filename, sizeof filename, "%s.m3u8",
903 tc.currdir[1] ? tc.currdir : "/root");
904 splashf(0, "%s %s", str(LANG_CREATING), filename);
906 trigger_cpu_boost();
907 catalog_add_to_a_playlist(tc.currdir, ATTR_DIRECTORY, true, filename);
908 cancel_cpu_boost();
910 return true;
913 int rockbox_browse(const char *root, int dirfilter)
915 int ret_val = 0;
916 int *last_filter = tc.dirfilter;
917 tc.dirfilter = &dirfilter;
918 tc.sort_dir = global_settings.sort_dir;
920 reload_dir = true;
921 if (dirfilter >= NUM_FILTER_MODES)
923 static struct tree_context backup;
924 char current[MAX_PATH];
925 int last_context;
927 backup = tc;
928 tc.selected_item = 0;
929 tc.dirlevel = 0;
930 memcpy(tc.currdir, root, sizeof(tc.currdir));
931 start_wps = false;
932 last_context = curr_context;
934 /* Center on the currently loaded language when browsing languages. */
935 if (dirfilter == SHOW_LNG)
937 if (global_settings.lang_file[0])
939 snprintf(current, sizeof(current), LANG_DIR "/%s.lng",
940 global_settings.lang_file);
942 else
944 strlcpy(current, LANG_DIR "/english.lng", sizeof(current));
947 /* Center on currently loaded WPS */
948 else if (dirfilter == SHOW_WPS)
950 snprintf(current, sizeof(current), WPS_DIR "/%s.wps",
951 global_settings.wps_file);
953 #ifdef HAVE_REMOTE_LCD
954 /* Center on currently loaded RWPS */
955 else if (dirfilter == SHOW_RWPS)
957 snprintf(current, sizeof(current), WPS_DIR "/%s.rwps",
958 global_settings.rwps_file);
960 else if (dirfilter == SHOW_RSBS)
962 snprintf(current, sizeof(current), SBS_DIR "/%s.rsbs",
963 global_settings.rsbs_file);
965 #endif
966 #ifdef HAVE_LCD_BITMAP
967 /* Center on the currently loaded font when browsing fonts */
968 else if (dirfilter == SHOW_FONT)
970 snprintf(current, sizeof(current), FONT_DIR "/%s.fnt",
971 global_settings.font_file);
973 else if (dirfilter == SHOW_SBS)
975 snprintf(current, sizeof(current), SBS_DIR "/%s.sbs",
976 global_settings.sbs_file);
978 #endif
979 #if CONFIG_TUNER
980 /* Center on the currently loaded FM preset when browsing those */
981 else if (dirfilter == SHOW_FMR)
983 snprintf(current, sizeof(current), FMPRESET_PATH "/%s.fmr",
984 global_settings.fmr_file);
986 #endif
987 else /* reset current[] */
988 current[0] = '\0';
990 /* If we've found a file to center on, do it */
991 if (current[0] == '/')
993 set_current_file(current);
994 /* set_current_file changes dirlevel, change it back */
995 tc.dirlevel = 0;
998 ret_val = dirbrowse();
999 tc = backup;
1000 curr_context = last_context;
1002 else
1004 static char buf[MAX_PATH];
1005 if (dirfilter != SHOW_ID3DB)
1006 tc.dirfilter = &global_settings.dirfilter;
1007 strcpy(buf,root);
1008 set_current_file(buf);
1009 ret_val = dirbrowse();
1011 tc.dirfilter = last_filter;
1012 return ret_val;
1015 void tree_mem_init(void)
1017 /* We copy the settings value in case it is changed by the user. We can't
1018 use it until the next reboot. */
1019 max_files = global_settings.max_files_in_dir;
1021 /* initialize tree context struct */
1022 memset(&tc, 0, sizeof(tc));
1023 tc.dirfilter = &global_settings.dirfilter;
1024 tc.sort_dir = global_settings.sort_dir;
1026 tc.name_buffer_size = AVERAGE_FILENAME_LENGTH * max_files;
1027 tc.name_buffer = buffer_alloc(tc.name_buffer_size);
1029 tc.dircache_size = max_files * sizeof(struct entry);
1030 tc.dircache = buffer_alloc(tc.dircache_size);
1031 tree_get_filetypes(&filetypes, &filetypes_count);
1034 bool bookmark_play(char *resume_file, int index, int offset, int seed,
1035 char *filename)
1037 int i;
1038 char* suffix = strrchr(resume_file, '.');
1039 bool started = false;
1041 if (suffix != NULL &&
1042 (!strcasecmp(suffix, ".m3u") || !strcasecmp(suffix, ".m3u8")))
1044 /* Playlist playback */
1045 char* slash;
1046 /* check that the file exists */
1047 if(!file_exists(resume_file))
1048 return false;
1050 slash = strrchr(resume_file,'/');
1051 if (slash)
1053 char* cp;
1054 *slash=0;
1056 cp=resume_file;
1057 if (!cp[0])
1058 cp="/";
1060 if (playlist_create(cp, slash+1) != -1)
1062 if (global_settings.playlist_shuffle)
1063 playlist_shuffle(seed, -1);
1064 playlist_start(index,offset);
1065 started = true;
1067 *slash='/';
1070 else
1072 /* Directory playback */
1073 lastdir[0]='\0';
1074 if (playlist_create(resume_file, NULL) != -1)
1076 const char* peek_filename;
1077 resume_directory(resume_file);
1078 if (global_settings.playlist_shuffle)
1079 playlist_shuffle(seed, -1);
1081 /* Check if the file is at the same spot in the directory,
1082 else search for it */
1083 peek_filename = playlist_peek(index);
1085 if (peek_filename == NULL)
1087 /* playlist has shrunk, search from the top */
1088 index = 0;
1089 peek_filename = playlist_peek(index);
1090 if (peek_filename == NULL)
1091 return false;
1094 if (strcmp(strrchr(peek_filename, '/') + 1, filename))
1096 for ( i=0; i < playlist_amount(); i++ )
1098 peek_filename = playlist_peek(i);
1100 if (peek_filename == NULL)
1101 return false;
1103 if (!strcmp(strrchr(peek_filename, '/') + 1, filename))
1104 break;
1106 if (i < playlist_amount())
1107 index = i;
1108 else
1109 return false;
1111 playlist_start(index,offset);
1112 started = true;
1116 if (started)
1117 start_wps = true;
1118 return started;
1121 static void say_filetype(int attr)
1123 /* try to find a voice ID for the extension, if known */
1124 int j;
1125 attr &= FILE_ATTR_MASK; /* file type */
1126 for (j=0; j<filetypes_count; j++)
1127 if (attr == filetypes[j].tree_attr)
1129 talk_id(filetypes[j].voiceclip, true);
1130 return;
1134 static int ft_play_dirname(char* name)
1136 #if CONFIG_CODEC != SWCODEC
1137 if (audio_status() & AUDIO_STATUS_PLAY)
1138 return 0;
1139 #endif
1141 if(talk_file(tc.currdir, name, dir_thumbnail_name, NULL,
1142 NULL, false))
1144 if(global_settings.talk_filetype)
1145 talk_id(VOICE_DIR, true);
1146 return 1;
1148 else
1149 return -1;
1152 static void ft_play_filename(char *dir, char *file)
1154 #if CONFIG_CODEC != SWCODEC
1155 if (audio_status() & AUDIO_STATUS_PLAY)
1156 return;
1157 #endif
1159 if (strlen(file) >= strlen(file_thumbnail_ext)
1160 && strcasecmp(&file[strlen(file) - strlen(file_thumbnail_ext)],
1161 file_thumbnail_ext))
1162 /* file has no .talk extension */
1163 talk_file(dir, NULL, file, file_thumbnail_ext,
1164 NULL, false);
1165 else
1166 /* it already is a .talk file, play this directly, but prefix it. */
1167 talk_file(dir, NULL, file, NULL,
1168 TALK_IDARRAY(LANG_VOICE_DIR_HOVER), false);
1171 /* These two functions are called by the USB and shutdown handlers */
1172 void tree_flush(void)
1174 #ifdef HAVE_TAGCACHE
1175 tagcache_shutdown();
1176 #endif
1178 #ifdef HAVE_TC_RAMCACHE
1179 tagcache_unload_ramcache();
1180 #endif
1182 #ifdef HAVE_DIRCACHE
1184 int old_val = global_status.dircache_size;
1185 if (global_settings.dircache)
1187 if (!dircache_is_initializing())
1188 global_status.dircache_size = dircache_get_cache_size();
1189 # ifdef HAVE_EEPROM_SETTINGS
1190 if (firmware_settings.initialized)
1191 dircache_save();
1192 # endif
1193 dircache_disable();
1195 else
1197 global_status.dircache_size = 0;
1199 if (old_val != global_status.dircache_size)
1200 status_save();
1202 #endif
1205 void tree_restore(void)
1207 #ifdef HAVE_EEPROM_SETTINGS
1208 firmware_settings.disk_clean = false;
1209 #endif
1211 #ifdef HAVE_TC_RAMCACHE
1212 remove(TAGCACHE_STATEFILE);
1213 #endif
1215 #ifdef HAVE_DIRCACHE
1216 remove(DIRCACHE_FILE);
1217 if (global_settings.dircache)
1219 /* Print "Scanning disk..." to the display. */
1220 splash(0, str(LANG_SCANNING_DISK));
1222 dircache_build(global_status.dircache_size);
1224 #endif
1225 #ifdef HAVE_TAGCACHE
1226 tagcache_start_scan();
1227 #endif