Dircache: Allow dircache to be enabled without reboot.
[maemo-rb.git] / apps / tree.c
blob24acd5ac69413fad080a5f3b71925318b7a3ad35
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 "core_alloc.h"
50 #include "power.h"
51 #include "action.h"
52 #include "talk.h"
53 #include "filetypes.h"
54 #include "misc.h"
55 #include "filefuncs.h"
56 #include "filetree.h"
57 #include "tagtree.h"
58 #ifdef HAVE_RECORDING
59 #include "recorder/recording.h"
60 #endif
61 #include "rtc.h"
62 #include "dircache.h"
63 #ifdef HAVE_TAGCACHE
64 #include "tagcache.h"
65 #endif
66 #include "yesno.h"
67 #include "eeprom_settings.h"
68 #include "playlist_catalog.h"
70 /* gui api */
71 #include "list.h"
72 #include "splash.h"
73 #include "buttonbar.h"
74 #include "quickscreen.h"
75 #include "appevents.h"
77 #include "root_menu.h"
79 static const struct filetype *filetypes;
80 static int filetypes_count;
82 struct gui_synclist tree_lists;
84 /* I put it here because other files doesn't use it yet,
85 * but should be elsewhere since it will be used mostly everywhere */
86 #ifdef HAVE_BUTTONBAR
87 static struct gui_buttonbar tree_buttonbar;
88 #endif
89 static struct tree_context tc;
91 char lastfile[MAX_PATH];
92 static char lastdir[MAX_PATH];
93 #ifdef HAVE_TAGCACHE
94 static int lasttable, lastextra, lastfirstpos;
95 #endif
97 static bool reload_dir = false;
99 static bool start_wps = false;
100 static int curr_context = false;/* id3db or tree*/
102 static int dirbrowse(void);
103 static int ft_play_dirname(char* name);
104 static void ft_play_filename(char *dir, char *file);
105 static void say_filetype(int attr);
107 struct entry* tree_get_entries(struct tree_context *t)
109 return core_get_data(t->cache.entries_handle);
112 struct entry* tree_get_entry_at(struct tree_context *t, int index)
114 struct entry* entries = tree_get_entries(t);
115 return &entries[index];
119 static const char* tree_get_filename(int selected_item, void *data,
120 char *buffer, size_t buffer_len)
122 struct tree_context * local_tc=(struct tree_context *)data;
123 char *name;
124 int attr=0;
125 bool stripit = false;
126 #ifdef HAVE_TAGCACHE
127 bool id3db = *(local_tc->dirfilter) == SHOW_ID3DB;
129 if (id3db)
131 return tagtree_get_entry_name(&tc, selected_item, buffer, buffer_len);
133 else
134 #endif
136 struct entry* e = tree_get_entry_at(local_tc, selected_item);
137 name = e->name;
138 attr = e->attr;
141 if(!(attr & ATTR_DIRECTORY))
143 switch(global_settings.show_filename_ext)
145 case 0:
146 /* show file extension: off */
147 stripit = true;
148 break;
149 case 1:
150 /* show file extension: on */
151 break;
152 case 2:
153 /* show file extension: only unknown types */
154 stripit = filetype_supported(attr);
155 break;
156 case 3:
157 default:
158 /* show file extension: only when viewing all */
159 stripit = (*(local_tc->dirfilter) != SHOW_ID3DB) &&
160 (*(local_tc->dirfilter) != SHOW_ALL);
161 break;
165 if(stripit)
167 return(strip_extension(buffer, buffer_len, name));
169 return(name);
172 #ifdef HAVE_LCD_COLOR
173 static int tree_get_filecolor(int selected_item, void * data)
175 if (*tc.dirfilter == SHOW_ID3DB)
176 return -1;
177 struct tree_context * local_tc=(struct tree_context *)data;
178 struct entry* e = tree_get_entry_at(local_tc, selected_item);
179 return filetype_get_color(e->name, e->attr);
181 #endif
183 static enum themable_icons tree_get_fileicon(int selected_item, void * data)
185 struct tree_context * local_tc=(struct tree_context *)data;
186 #ifdef HAVE_TAGCACHE
187 bool id3db = *(local_tc->dirfilter) == SHOW_ID3DB;
188 if (id3db) {
189 return tagtree_get_icon(&tc);
191 else
192 #endif
194 struct entry* e = tree_get_entry_at(local_tc, 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;
206 char buf[AVERAGE_FILENAME_LENGTH*2];
208 if (id3db)
210 attr = tagtree_get_attr(local_tc);
211 name = tagtree_get_entry_name(local_tc, selected_item, buf, sizeof(buf));
213 else
214 #endif
216 struct entry* e = tree_get_entry_at(local_tc, 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 check_rockboxdir();
290 strcpy(tc.currdir, "/");
292 #ifdef HAVE_LCD_CHARCELLS
293 int i;
294 FOR_NB_SCREENS(i)
295 screens[i].double_height(false);
296 #endif
297 #ifdef HAVE_BUTTONBAR
298 gui_buttonbar_init(&tree_buttonbar);
299 /* since archos only have one screen, no need to create more than that */
300 gui_buttonbar_set_display(&tree_buttonbar, &(screens[SCREEN_MAIN]) );
301 #endif
302 gui_synclist_init(&tree_lists, &tree_get_filename, &tc, false, 1, NULL);
303 gui_synclist_set_voice_callback(&tree_lists, tree_voice_cb);
304 gui_synclist_set_icon_callback(&tree_lists,
305 global_settings.show_icons?&tree_get_fileicon:NULL);
306 #ifdef HAVE_LCD_COLOR
307 gui_synclist_set_color_callback(&tree_lists, &tree_get_filecolor);
308 #endif
312 /* drawer function for the GUI_EVENT_REDRAW callback */
313 void tree_drawlists(void)
315 /* band-aid to fix the bar/list redrawing properly after leaving a plugin */
316 send_event(GUI_EVENT_THEME_CHANGED, NULL);
317 /* end bandaid */
318 gui_synclist_draw(&tree_lists);
322 struct tree_context* tree_get_context(void)
324 return &tc;
328 * Returns the position of a given file in the current directory
329 * returns -1 if not found
331 static int tree_get_file_position(char * filename)
333 int i;
334 struct entry* e;
336 /* use lastfile to determine the selected item (default=0) */
337 for (i=0; i < tc.filesindir; i++)
339 e = tree_get_entry_at(&tc, i);
340 if (!strcasecmp(e->name, filename))
341 return(i);
343 return(-1);/* no file can match, returns undefined */
347 * Called when a new dir is loaded (for example when returning from other apps ...)
348 * also completely redraws the tree
350 static int update_dir(void)
352 bool changed = false;
353 #ifdef HAVE_TAGCACHE
354 bool id3db = *tc.dirfilter == SHOW_ID3DB;
355 /* Checks for changes */
356 if (id3db) {
357 if (tc.currtable != lasttable ||
358 tc.currextra != lastextra ||
359 tc.firstpos != lastfirstpos ||
360 reload_dir)
362 if (tagtree_load(&tc) < 0)
363 return -1;
365 lasttable = tc.currtable;
366 lastextra = tc.currextra;
367 lastfirstpos = tc.firstpos;
368 changed = true;
371 else
372 #endif
374 /* if the tc.currdir has been changed, reload it ...*/
375 if (strncmp(tc.currdir, lastdir, sizeof(lastdir)) || reload_dir)
377 if (ft_load(&tc, NULL) < 0)
378 return -1;
379 strcpy(lastdir, tc.currdir);
380 changed = true;
383 /* if selected item is undefined */
384 if (tc.selected_item == -1)
386 /* use lastfile to determine the selected item */
387 tc.selected_item = tree_get_file_position(lastfile);
389 /* If the file doesn't exists, select the first one (default) */
390 if(tc.selected_item < 0)
391 tc.selected_item = 0;
392 changed = true;
394 if (changed)
397 #ifdef HAVE_TAGCACHE
398 !id3db &&
399 #endif
400 tc.dirfull )
402 splash(HZ, ID2P(LANG_SHOWDIR_BUFFER_FULL));
405 #ifdef HAVE_TAGCACHE
406 if (id3db)
408 #ifdef HAVE_LCD_BITMAP
409 if (global_settings.show_path_in_browser == SHOW_PATH_FULL
410 || global_settings.show_path_in_browser == SHOW_PATH_CURRENT)
412 gui_synclist_set_title(&tree_lists, tagtree_get_title(&tc),
413 filetype_get_icon(ATTR_DIRECTORY));
415 else
417 /* Must clear the title as the list is reused */
418 gui_synclist_set_title(&tree_lists, NULL, NOICON);
420 #endif
422 else
423 #endif
425 #ifdef HAVE_LCD_BITMAP
426 if (tc.browse && tc.browse->title)
428 int icon = tc.browse->icon;
429 if (icon == NOICON)
430 icon = filetype_get_icon(ATTR_DIRECTORY);
431 gui_synclist_set_title(&tree_lists, tc.browse->title, icon);
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, getcwd_size_t size)
515 if (!buf)
516 return tc.currdir;
517 else if (size)
519 if ((getcwd_size_t)strlcpy(buf, tc.currdir, size) < size)
520 return buf;
522 /* size == 0, or truncation in strlcpy */
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 char* get_current_file(char* buffer, size_t 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 NULL;
539 #endif
541 struct entry* e = tree_get_entry_at(&tc, tc.selected_item);
542 if (getcwd(buffer, buffer_len))
544 if (tc.dirlength)
546 if (buffer[strlen(buffer)-1] != '/')
547 strlcat(buffer, "/", buffer_len);
548 if (strlcat(buffer, e->name, buffer_len) >= buffer_len)
549 return NULL;
551 return buffer;
553 return NULL;
556 /* Allow apps to change our dirfilter directly (required for sub browsers)
557 if they're suddenly going to become a file browser for example */
558 void set_dirfilter(int l_dirfilter)
560 *tc.dirfilter = l_dirfilter;
563 /* Selects a file and update tree context properly */
564 void set_current_file(const char *path)
566 const char *name;
567 int i;
569 #ifdef HAVE_TAGCACHE
570 /* in ID3DB mode it is a bad idea to call this function */
571 /* (only happens with `follow playlist') */
572 if( *tc.dirfilter == SHOW_ID3DB )
573 return;
574 #endif
576 /* separate directory from filename */
577 /* gets the directory's name and put it into tc.currdir */
578 name = strrchr(path+1,'/');
579 if (name)
581 strlcpy(tc.currdir, path, name - path + 1);
582 name++;
584 else
586 strcpy(tc.currdir, "/");
587 name = path+1;
590 strcpy(lastfile, name);
593 /* If we changed dir we must recalculate the dirlevel
594 and adjust the selected history properly */
595 if (strncmp(tc.currdir,lastdir,sizeof(lastdir)))
597 tc.dirlevel = 0;
598 tc.selected_item_history[tc.dirlevel] = -1;
600 /* use '/' to calculate dirlevel */
601 for (i = 1; path[i] != '\0'; i++)
603 if (path[i] == '/')
605 tc.dirlevel++;
606 tc.selected_item_history[tc.dirlevel] = -1;
610 if (ft_load(&tc, NULL) >= 0)
612 tc.selected_item = tree_get_file_position(lastfile);
617 /* main loop, handles key events */
618 static int dirbrowse(void)
620 int numentries=0;
621 char buf[MAX_PATH];
622 int button, oldbutton;
623 bool reload_root = false;
624 int lastfilter = *tc.dirfilter;
625 bool lastsortcase = global_settings.sort_case;
626 bool exit_func = false;
628 char* currdir = tc.currdir; /* just a shortcut */
629 #ifdef HAVE_TAGCACHE
630 bool id3db = *tc.dirfilter == SHOW_ID3DB;
632 if (id3db)
633 curr_context=CONTEXT_ID3DB;
634 else
635 #endif
636 curr_context=CONTEXT_TREE;
637 if (tc.selected_item < 0)
638 tc.selected_item = 0;
639 #ifdef HAVE_TAGCACHE
640 tc.firstpos = 0;
641 lasttable = -1;
642 lastextra = -1;
643 lastfirstpos = 0;
644 #endif
646 start_wps = false;
647 numentries = update_dir();
648 reload_dir = false;
649 if (numentries == -1)
650 return GO_TO_PREVIOUS; /* currdir is not a directory */
652 if (*tc.dirfilter > NUM_FILTER_MODES && numentries==0)
654 splash(HZ*2, ID2P(LANG_NO_FILES));
655 return GO_TO_PREVIOUS; /* No files found for rockbox_browse() */
658 gui_synclist_draw(&tree_lists);
659 while(1) {
660 bool restore = false;
661 if (tc.dirlevel < 0)
662 tc.dirlevel = 0; /* shouldnt be needed.. this code needs work! */
664 button = get_action(CONTEXT_TREE,
665 list_do_action_timeout(&tree_lists, HZ/2));
666 oldbutton = button;
667 gui_synclist_do_button(&tree_lists, &button,LIST_WRAP_UNLESS_HELD);
668 tc.selected_item = gui_synclist_get_sel_pos(&tree_lists);
669 switch ( button ) {
670 case ACTION_STD_OK:
671 /* nothing to do if no files to display */
672 if ( numentries == 0 )
673 break;
675 short attr = tree_get_entry_at(&tc, tc.selected_item)->attr;
676 if ((tc.browse->flags & BROWSE_SELECTONLY) &&
677 !(attr & ATTR_DIRECTORY))
679 tc.browse->flags |= BROWSE_SELECTED;
680 get_current_file(tc.browse->buf, tc.browse->bufsize);
681 return GO_TO_PREVIOUS;
684 #ifdef HAVE_TAGCACHE
685 switch (id3db?tagtree_enter(&tc):ft_enter(&tc))
686 #else
687 switch (ft_enter(&tc))
688 #endif
690 case GO_TO_FILEBROWSER: reload_dir = true; break;
691 case GO_TO_WPS:
692 return GO_TO_WPS;
693 #if CONFIG_TUNER
694 case GO_TO_FM:
695 return GO_TO_FM;
696 #endif
697 case GO_TO_ROOT: exit_func = true; break;
698 default: break;
700 restore = true;
701 break;
703 case ACTION_STD_CANCEL:
704 if (*tc.dirfilter > NUM_FILTER_MODES && tc.dirlevel < 1) {
705 exit_func = true;
706 break;
708 if ((*tc.dirfilter == SHOW_ID3DB && tc.dirlevel == 0) ||
709 ((*tc.dirfilter != SHOW_ID3DB && !strcmp(currdir,"/"))))
711 #ifdef HAVE_LCD_BITMAP /* charcell doesnt have ACTION_TREE_PGLEFT so this isnt needed */
712 if (oldbutton == ACTION_TREE_PGLEFT)
713 break;
714 else
715 #endif
716 return GO_TO_ROOT;
719 #ifdef HAVE_TAGCACHE
720 if (id3db)
721 tagtree_exit(&tc);
722 else
723 #endif
724 if (ft_exit(&tc) == 3)
725 exit_func = true;
727 restore = true;
728 break;
730 case ACTION_TREE_STOP:
731 if (list_stop_handler())
732 restore = true;
733 break;
735 case ACTION_STD_MENU:
736 return GO_TO_ROOT;
737 break;
739 #ifdef HAVE_RECORDING
740 case ACTION_STD_REC:
741 return GO_TO_RECSCREEN;
742 #endif
744 case ACTION_TREE_WPS:
745 return GO_TO_PREVIOUS_MUSIC;
746 break;
747 #ifdef HAVE_QUICKSCREEN
748 case ACTION_STD_QUICKSCREEN:
749 /* don't enter f2 from plugin browser */
750 if (*tc.dirfilter < NUM_FILTER_MODES)
752 if (quick_screen_quick(button))
753 reload_dir = true;
754 restore = true;
756 break;
757 #endif
758 #ifdef BUTTON_F3
759 case ACTION_F3:
760 /* don't enter f3 from plugin browser */
761 if (*tc.dirfilter < NUM_FILTER_MODES)
763 if (quick_screen_f3(ACTION_F3))
764 reload_dir = true;
765 restore = true;
767 break;
768 #endif
770 #ifdef HAVE_HOTKEY
771 case ACTION_TREE_HOTKEY:
772 if (!global_settings.hotkey_tree)
773 break;
774 /* fall through */
775 #endif
776 case ACTION_STD_CONTEXT:
778 bool hotkey = button == ACTION_TREE_HOTKEY;
779 int onplay_result;
780 int attr = 0;
782 if (tc.browse->flags & BROWSE_NO_CONTEXT_MENU)
783 break;
785 if(!numentries)
786 onplay_result = onplay(NULL, 0, curr_context, hotkey);
787 else {
788 #ifdef HAVE_TAGCACHE
789 if (id3db)
791 if (tagtree_get_attr(&tc) == FILE_ATTR_AUDIO)
793 attr = FILE_ATTR_AUDIO;
794 tagtree_get_filename(&tc, buf, sizeof(buf));
796 else
797 attr = ATTR_DIRECTORY;
799 else
800 #endif
802 struct entry *entry = tree_get_entry_at(&tc, tc.selected_item);
803 attr = entry->attr;
805 if (currdir[1]) /* Not in / */
806 snprintf(buf, sizeof buf, "%s/%s",
807 currdir, entry->name);
808 else /* In / */
809 snprintf(buf, sizeof buf, "/%s", entry->name);
811 onplay_result = onplay(buf, attr, curr_context, hotkey);
813 switch (onplay_result)
815 case ONPLAY_MAINMENU:
816 return GO_TO_ROOT;
818 case ONPLAY_OK:
819 restore = true;
820 break;
822 case ONPLAY_RELOAD_DIR:
823 reload_dir = true;
824 break;
826 case ONPLAY_START_PLAY:
827 return GO_TO_WPS;
828 break;
830 break;
833 #ifdef HAVE_HOTSWAP
834 case SYS_FS_CHANGED:
835 #ifdef HAVE_TAGCACHE
836 if (!id3db)
837 #endif
838 reload_dir = true;
839 /* The 'dir no longer valid' situation will be caught later
840 * by checking the showdir() result. */
841 break;
842 #endif
844 default:
845 if (default_event_handler(button) == SYS_USB_CONNECTED)
847 if(*tc.dirfilter > NUM_FILTER_MODES)
848 /* leave sub-browsers after usb, doing otherwise
849 might be confusing to the user */
850 exit_func = true;
851 else
852 reload_dir = true;
854 break;
856 if (start_wps)
857 return GO_TO_WPS;
858 if (button && !IS_SYSEVENT(button))
860 storage_spin();
864 check_rescan:
865 /* do we need to rescan dir? */
866 if (reload_dir || reload_root ||
867 lastfilter != *tc.dirfilter ||
868 lastsortcase != global_settings.sort_case)
870 if (reload_root) {
871 strcpy(currdir, "/");
872 tc.dirlevel = 0;
873 #ifdef HAVE_TAGCACHE
874 tc.currtable = 0;
875 tc.currextra = 0;
876 lasttable = -1;
877 lastextra = -1;
878 #endif
879 reload_root = false;
882 if (!reload_dir)
884 gui_synclist_select_item(&tree_lists, 0);
885 gui_synclist_draw(&tree_lists);
886 tc.selected_item = 0;
887 lastdir[0] = 0;
890 lastfilter = *tc.dirfilter;
891 lastsortcase = global_settings.sort_case;
892 restore = true;
895 if (exit_func)
896 return GO_TO_PREVIOUS;
898 if (restore || reload_dir) {
899 /* restore display */
900 numentries = update_dir();
901 reload_dir = false;
902 if (currdir[1] && (numentries < 0))
903 { /* not in root and reload failed */
904 reload_root = true; /* try root */
905 goto check_rescan;
909 return true;
912 bool create_playlist(void)
914 char filename[MAX_PATH];
916 if (tc.currdir[1])
917 snprintf(filename, sizeof filename, "%s.m3u8", tc.currdir);
918 else
919 snprintf(filename, sizeof filename, "%s/all.m3u8",
920 catalog_get_directory());
923 if (kbd_input(filename, MAX_PATH))
924 return false;
925 splashf(0, "%s %s", str(LANG_CREATING), filename);
927 trigger_cpu_boost();
928 catalog_add_to_a_playlist(tc.currdir, ATTR_DIRECTORY, true, filename);
929 cancel_cpu_boost();
931 return true;
934 void browse_context_init(struct browse_context *browse,
935 int dirfilter, unsigned flags,
936 char *title, enum themable_icons icon,
937 const char *root, const char *selected)
939 browse->dirfilter = dirfilter;
940 browse->flags = flags;
941 browse->callback_show_item = NULL;
942 browse->title = title;
943 browse->icon = icon;
944 browse->root = root;
945 browse->selected = selected;
946 browse->buf = NULL;
947 browse->bufsize = 0;
950 #define NUM_TC_BACKUP 3
951 static struct tree_context backups[NUM_TC_BACKUP];
952 /* do not make backup if it is not recursive call */
953 static int backup_count = -1;
954 int rockbox_browse(struct browse_context *browse)
956 static char current[MAX_PATH];
957 int ret_val = 0;
958 int dirfilter = browse->dirfilter;
960 if (backup_count >= NUM_TC_BACKUP)
961 return GO_TO_PREVIOUS;
962 if (backup_count >= 0)
963 backups[backup_count] = tc;
964 backup_count++;
966 tc.dirfilter = &dirfilter;
967 tc.sort_dir = global_settings.sort_dir;
969 reload_dir = true;
970 if (*tc.dirfilter >= NUM_FILTER_MODES)
972 int last_context;
974 tc.browse = browse;
975 tc.selected_item = 0;
976 tc.dirlevel = 0;
977 strlcpy(tc.currdir, browse->root, sizeof(tc.currdir));
978 start_wps = false;
979 last_context = curr_context;
981 if (browse->selected)
983 snprintf(current, sizeof(current), "%s/%s",
984 browse->root, browse->selected);
985 set_current_file(current);
986 /* set_current_file changes dirlevel, change it back */
987 tc.dirlevel = 0;
990 ret_val = dirbrowse();
991 curr_context = last_context;
993 else
995 if (dirfilter != SHOW_ID3DB)
996 tc.dirfilter = &global_settings.dirfilter;
997 tc.browse = browse;
998 strcpy(current, browse->root);
999 set_current_file(current);
1000 ret_val = dirbrowse();
1002 backup_count--;
1003 if (backup_count >= 0)
1004 tc = backups[backup_count];
1005 return ret_val;
1008 static int move_callback(int handle, void* current, void* new)
1010 struct tree_cache* cache = &tc.cache;
1011 if (cache->lock_count > 0)
1012 return BUFLIB_CB_CANNOT_MOVE;
1014 size_t diff = new - current;
1015 /* FIX_PTR makes sure to not accidentally update static allocations */
1016 #define FIX_PTR(x) \
1017 { if ((void*)x > current && (void*)x < (current+cache->name_buffer_size)) x+= diff; }
1019 if (handle == cache->name_buffer_handle)
1020 { /* update entry structs, *even if they are struct tagentry */
1021 struct entry *this = core_get_data(cache->entries_handle);
1022 struct entry *last = this + cache->max_entries;
1023 for(; this < last; this++)
1024 FIX_PTR(this->name);
1026 /* nothing to do if entries moved */
1027 return BUFLIB_CB_OK;
1030 static struct buflib_callbacks ops = {
1031 .move_callback = move_callback,
1032 .shrink_callback = NULL,
1035 void tree_mem_init(void)
1037 /* initialize tree context struct */
1038 struct tree_cache* cache = &tc.cache;
1039 memset(&tc, 0, sizeof(tc));
1040 tc.dirfilter = &global_settings.dirfilter;
1041 tc.sort_dir = global_settings.sort_dir;
1043 cache->name_buffer_size = AVERAGE_FILENAME_LENGTH *
1044 global_settings.max_files_in_dir;
1045 cache->name_buffer_handle = core_alloc_ex("tree names",
1046 cache->name_buffer_size,
1047 &ops);
1049 cache->max_entries = global_settings.max_files_in_dir;
1050 cache->entries_handle = core_alloc_ex("tree entries",
1051 cache->max_entries*(sizeof(struct entry)),
1052 &ops);
1053 tree_get_filetypes(&filetypes, &filetypes_count);
1056 bool bookmark_play(char *resume_file, int index, int offset, int seed,
1057 char *filename)
1059 int i;
1060 char* suffix = strrchr(resume_file, '.');
1061 bool started = false;
1063 if (suffix != NULL &&
1064 (!strcasecmp(suffix, ".m3u") || !strcasecmp(suffix, ".m3u8")))
1066 /* Playlist playback */
1067 char* slash;
1068 /* check that the file exists */
1069 if(!file_exists(resume_file))
1070 return false;
1072 slash = strrchr(resume_file,'/');
1073 if (slash)
1075 char* cp;
1076 *slash=0;
1078 cp=resume_file;
1079 if (!cp[0])
1080 cp="/";
1082 if (playlist_create(cp, slash+1) != -1)
1084 if (global_settings.playlist_shuffle)
1085 playlist_shuffle(seed, -1);
1086 playlist_start(index,offset);
1087 started = true;
1089 *slash='/';
1092 else
1094 /* Directory playback */
1095 lastdir[0]='\0';
1096 if (playlist_create(resume_file, NULL) != -1)
1098 char filename_buf[MAX_PATH + 1];
1099 const char* peek_filename;
1100 resume_directory(resume_file);
1101 if (global_settings.playlist_shuffle)
1102 playlist_shuffle(seed, -1);
1104 /* Check if the file is at the same spot in the directory,
1105 else search for it */
1106 peek_filename = playlist_peek(index, filename_buf,
1107 sizeof(filename_buf));
1109 if (peek_filename == NULL)
1111 /* playlist has shrunk, search from the top */
1112 index = 0;
1113 peek_filename = playlist_peek(index, filename_buf,
1114 sizeof(filename_buf));
1115 if (peek_filename == NULL)
1116 return false;
1119 if (strcmp(strrchr(peek_filename, '/') + 1, filename))
1121 for ( i=0; i < playlist_amount(); i++ )
1123 peek_filename = playlist_peek(i, filename_buf,
1124 sizeof(filename_buf));
1126 if (peek_filename == NULL)
1127 return false;
1129 if (!strcmp(strrchr(peek_filename, '/') + 1, filename))
1130 break;
1132 if (i < playlist_amount())
1133 index = i;
1134 else
1135 return false;
1137 playlist_start(index,offset);
1138 started = true;
1142 if (started)
1143 start_wps = true;
1144 return started;
1147 static void say_filetype(int attr)
1149 /* try to find a voice ID for the extension, if known */
1150 int j;
1151 attr &= FILE_ATTR_MASK; /* file type */
1152 for (j=0; j<filetypes_count; j++)
1153 if (attr == filetypes[j].tree_attr)
1155 talk_id(filetypes[j].voiceclip, true);
1156 return;
1160 static int ft_play_dirname(char* name)
1162 #if CONFIG_CODEC != SWCODEC
1163 if (audio_status() & AUDIO_STATUS_PLAY)
1164 return 0;
1165 #endif
1167 if(talk_file(tc.currdir, name, dir_thumbnail_name, NULL,
1168 NULL, false))
1170 if(global_settings.talk_filetype)
1171 talk_id(VOICE_DIR, true);
1172 return 1;
1174 else
1175 return -1;
1178 static void ft_play_filename(char *dir, char *file)
1180 #if CONFIG_CODEC != SWCODEC
1181 if (audio_status() & AUDIO_STATUS_PLAY)
1182 return;
1183 #endif
1185 if (strlen(file) >= strlen(file_thumbnail_ext)
1186 && strcasecmp(&file[strlen(file) - strlen(file_thumbnail_ext)],
1187 file_thumbnail_ext))
1188 /* file has no .talk extension */
1189 talk_file(dir, NULL, file, file_thumbnail_ext,
1190 NULL, false);
1191 else
1192 /* it already is a .talk file, play this directly, but prefix it. */
1193 talk_file(dir, NULL, file, NULL,
1194 TALK_IDARRAY(LANG_VOICE_DIR_HOVER), false);
1197 /* These two functions are called by the USB and shutdown handlers */
1198 void tree_flush(void)
1200 #ifdef HAVE_TAGCACHE
1201 tagcache_shutdown();
1202 #endif
1204 #ifdef HAVE_TC_RAMCACHE
1205 tagcache_unload_ramcache();
1206 #endif
1208 #ifdef HAVE_DIRCACHE
1210 int old_val = global_status.dircache_size;
1211 if (global_settings.dircache)
1213 if (!dircache_is_initializing())
1214 global_status.dircache_size = dircache_get_cache_size();
1215 # ifdef HAVE_EEPROM_SETTINGS
1216 if (firmware_settings.initialized)
1217 dircache_save();
1218 # endif
1219 dircache_suspend();
1221 else
1223 global_status.dircache_size = 0;
1225 if (old_val != global_status.dircache_size)
1226 status_save();
1228 #endif
1231 void tree_restore(void)
1233 #ifdef HAVE_EEPROM_SETTINGS
1234 firmware_settings.disk_clean = false;
1235 #endif
1237 #ifdef HAVE_TC_RAMCACHE
1238 remove(TAGCACHE_STATEFILE);
1239 #endif
1241 #ifdef HAVE_DIRCACHE
1242 remove(DIRCACHE_FILE);
1243 if (global_settings.dircache)
1245 /* Print "Scanning disk..." to the display. */
1246 splash(0, str(LANG_SCANNING_DISK));
1248 dircache_build(global_status.dircache_size);
1250 #endif
1251 #ifdef HAVE_TAGCACHE
1252 tagcache_start_scan();
1253 #endif