Make skin_render_playlistviewer() noinline. This function uses lots of stack (around...
[kugel-rb.git] / apps / tree.c
blob01a00d1f400d5c3b4d88ccdba781bfa20804c723
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 "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 bool boot_changed = false;
93 char lastfile[MAX_PATH];
94 static char lastdir[MAX_PATH];
95 #ifdef HAVE_TAGCACHE
96 static int lasttable, lastextra, lastfirstpos;
97 #endif
98 static int max_files = 0;
100 static bool reload_dir = false;
102 static bool start_wps = false;
103 static int curr_context = false;/* id3db or tree*/
105 static int dirbrowse(void);
106 static int ft_play_dirname(char* name);
107 static void ft_play_filename(char *dir, char *file);
108 static void say_filetype(int attr);
110 static const char* tree_get_filename(int selected_item, void *data,
111 char *buffer, size_t buffer_len)
113 struct tree_context * local_tc=(struct tree_context *)data;
114 char *name;
115 int attr=0;
116 bool stripit = false;
117 #ifdef HAVE_TAGCACHE
118 bool id3db = *(local_tc->dirfilter) == SHOW_ID3DB;
120 if (id3db)
122 return tagtree_get_entry(&tc, selected_item)->name;
124 else
125 #endif
127 struct entry* dc = local_tc->dircache;
128 struct entry* e = &dc[selected_item];
129 name = e->name;
130 attr = e->attr;
133 if(!(attr & ATTR_DIRECTORY))
135 switch(global_settings.show_filename_ext)
137 case 0:
138 /* show file extension: off */
139 stripit = true;
140 break;
141 case 1:
142 /* show file extension: on */
143 break;
144 case 2:
145 /* show file extension: only unknown types */
146 stripit = filetype_supported(attr);
147 break;
148 case 3:
149 default:
150 /* show file extension: only when viewing all */
151 stripit = (*(local_tc->dirfilter) != SHOW_ID3DB) &&
152 (*(local_tc->dirfilter) != SHOW_ALL);
153 break;
157 if(stripit)
159 return(strip_extension(buffer, buffer_len, name));
161 return(name);
164 #ifdef HAVE_LCD_COLOR
165 static int tree_get_filecolor(int selected_item, void * data)
167 if (*tc.dirfilter == SHOW_ID3DB)
168 return -1;
169 struct tree_context * local_tc=(struct tree_context *)data;
170 struct entry* dc = local_tc->dircache;
171 struct entry* e = &dc[selected_item];
172 return filetype_get_color(e->name, e->attr);
174 #endif
176 static enum themable_icons tree_get_fileicon(int selected_item, void * data)
178 struct tree_context * local_tc=(struct tree_context *)data;
179 #ifdef HAVE_TAGCACHE
180 bool id3db = *(local_tc->dirfilter) == SHOW_ID3DB;
181 if (id3db) {
182 return tagtree_get_icon(&tc);
184 else
185 #endif
187 struct entry* dc = local_tc->dircache;
188 struct entry* e = &dc[selected_item];
189 return filetype_get_icon(e->attr);
193 static int tree_voice_cb(int selected_item, void * data)
195 struct tree_context * local_tc=(struct tree_context *)data;
196 char *name;
197 int attr=0;
198 #ifdef HAVE_TAGCACHE
199 bool id3db = *(local_tc->dirfilter) == SHOW_ID3DB;
201 if (id3db)
203 attr = tagtree_get_attr(local_tc);
204 name = tagtree_get_entry(local_tc, selected_item)->name;
206 else
207 #endif
209 struct entry* dc = local_tc->dircache;
210 struct entry* e = &dc[selected_item];
211 name = e->name;
212 attr = e->attr;
214 bool is_dir = (attr & ATTR_DIRECTORY);
215 bool did_clip = false;
216 /* First the .talk clip case */
217 if(is_dir)
219 if(global_settings.talk_dir_clip)
221 did_clip = true;
222 if(ft_play_dirname(name) <0)
223 /* failed, not existing */
224 did_clip = false;
226 } else { /* it's a file */
227 if (global_settings.talk_file_clip && (attr & FILE_ATTR_THUMBNAIL))
229 did_clip = true;
230 ft_play_filename(local_tc->currdir, name);
233 if(!did_clip)
235 /* say the number or spell if required or as a fallback */
236 switch (is_dir ? global_settings.talk_dir : global_settings.talk_file)
238 case 1: /* as numbers */
239 talk_id(is_dir ? VOICE_DIR : VOICE_FILE, false);
240 talk_number(selected_item+1 - (is_dir ? 0 : local_tc->dirsindir),
241 true);
242 if(global_settings.talk_filetype
243 && !is_dir && *local_tc->dirfilter < NUM_FILTER_MODES)
244 say_filetype(attr);
245 break;
246 case 2: /* spelled */
247 talk_shutup();
248 if(global_settings.talk_filetype)
250 if(is_dir)
251 talk_id(VOICE_DIR, true);
252 else if(*local_tc->dirfilter < NUM_FILTER_MODES)
253 say_filetype(attr);
255 talk_spell(name, true);
256 break;
259 return 0;
262 bool check_rockboxdir(void)
264 char path[MAX_PATH];
265 if(!dir_exists(get_user_file_path(ROCKBOX_DIR, 0, path, sizeof(path))))
266 { /* No need to localise this message.
267 If .rockbox is missing, it wouldn't work anyway */
268 int i;
269 FOR_NB_SCREENS(i)
270 screens[i].clear_display();
271 splash(HZ*2, "No .rockbox directory");
272 FOR_NB_SCREENS(i)
273 screens[i].clear_display();
274 splash(HZ*2, "Installation incomplete");
275 return false;
277 return true;
280 /* do this really late in the init sequence */
281 void tree_gui_init(void)
283 check_rockboxdir();
285 strcpy(tc.currdir, "/");
287 #ifdef HAVE_LCD_CHARCELLS
288 int i;
289 FOR_NB_SCREENS(i)
290 screens[i].double_height(false);
291 #endif
292 #ifdef HAVE_BUTTONBAR
293 gui_buttonbar_init(&tree_buttonbar);
294 /* since archos only have one screen, no need to create more than that */
295 gui_buttonbar_set_display(&tree_buttonbar, &(screens[SCREEN_MAIN]) );
296 #endif
297 gui_synclist_init(&tree_lists, &tree_get_filename, &tc, false, 1, NULL);
298 gui_synclist_set_voice_callback(&tree_lists, tree_voice_cb);
299 gui_synclist_set_icon_callback(&tree_lists,
300 global_settings.show_icons?&tree_get_fileicon:NULL);
301 #ifdef HAVE_LCD_COLOR
302 gui_synclist_set_color_callback(&tree_lists, &tree_get_filecolor);
303 #endif
307 /* drawer function for the GUI_EVENT_REDRAW callback */
308 void tree_drawlists(void)
310 /* band-aid to fix the bar/list redrawing properly after leaving a plugin */
311 send_event(GUI_EVENT_THEME_CHANGED, NULL);
312 /* end bandaid */
313 gui_synclist_draw(&tree_lists);
317 struct tree_context* tree_get_context(void)
319 return &tc;
323 * Returns the position of a given file in the current directory
324 * returns -1 if not found
326 static int tree_get_file_position(char * filename)
328 int i;
330 /* use lastfile to determine the selected item (default=0) */
331 for (i=0; i < tc.filesindir; i++)
333 struct entry* dc = tc.dircache;
334 struct entry* e = &dc[i];
335 if (!strcasecmp(e->name, filename))
336 return(i);
338 return(-1);/* no file can match, returns undefined */
342 * Called when a new dir is loaded (for example when returning from other apps ...)
343 * also completely redraws the tree
345 static int update_dir(void)
347 bool changed = false;
348 #ifdef HAVE_TAGCACHE
349 bool id3db = *tc.dirfilter == SHOW_ID3DB;
350 /* Checks for changes */
351 if (id3db) {
352 if (tc.currtable != lasttable ||
353 tc.currextra != lastextra ||
354 tc.firstpos != lastfirstpos ||
355 reload_dir)
357 if (tagtree_load(&tc) < 0)
358 return -1;
360 lasttable = tc.currtable;
361 lastextra = tc.currextra;
362 lastfirstpos = tc.firstpos;
363 changed = true;
366 else
367 #endif
369 /* if the tc.currdir has been changed, reload it ...*/
370 if (strncmp(tc.currdir, lastdir, sizeof(lastdir)) || reload_dir)
372 if (ft_load(&tc, NULL) < 0)
373 return -1;
374 strcpy(lastdir, tc.currdir);
375 changed = true;
378 /* if selected item is undefined */
379 if (tc.selected_item == -1)
381 /* use lastfile to determine the selected item */
382 tc.selected_item = tree_get_file_position(lastfile);
384 /* If the file doesn't exists, select the first one (default) */
385 if(tc.selected_item < 0)
386 tc.selected_item = 0;
387 changed = true;
389 if (changed)
392 #ifdef HAVE_TAGCACHE
393 !id3db &&
394 #endif
395 (tc.dirfull || tc.filesindir == global_settings.max_files_in_dir) )
397 splash(HZ, ID2P(LANG_SHOWDIR_BUFFER_FULL));
400 #ifdef HAVE_TAGCACHE
401 if (id3db)
403 #ifdef HAVE_LCD_BITMAP
404 if (global_settings.show_path_in_browser == SHOW_PATH_FULL
405 || global_settings.show_path_in_browser == SHOW_PATH_CURRENT)
407 gui_synclist_set_title(&tree_lists, tagtree_get_title(&tc),
408 filetype_get_icon(ATTR_DIRECTORY));
410 else
412 /* Must clear the title as the list is reused */
413 gui_synclist_set_title(&tree_lists, NULL, NOICON);
415 #endif
417 else
418 #endif
420 #ifdef HAVE_LCD_BITMAP
421 if (global_settings.show_path_in_browser &&
422 *(tc.dirfilter) == SHOW_PLUGINS)
424 char *title;
425 if (!strcmp(tc.currdir, PLUGIN_GAMES_DIR))
426 title = str(LANG_PLUGIN_GAMES);
427 else if (!strcmp(tc.currdir, PLUGIN_APPS_DIR))
428 title = str(LANG_PLUGIN_APPS);
429 else if (!strcmp(tc.currdir, PLUGIN_DEMOS_DIR))
430 title = str(LANG_PLUGIN_DEMOS);
431 else title = str(LANG_PLUGINS);
432 gui_synclist_set_title(&tree_lists, title, Icon_Plugin);
434 else if (global_settings.show_path_in_browser == SHOW_PATH_FULL)
436 gui_synclist_set_title(&tree_lists, tc.currdir,
437 filetype_get_icon(ATTR_DIRECTORY));
439 else if (global_settings.show_path_in_browser == SHOW_PATH_CURRENT)
441 char *title = strrchr(tc.currdir, '/') + 1;
442 if (*title == '\0')
444 /* Display "Files" for the root dir */
445 gui_synclist_set_title(&tree_lists, str(LANG_DIR_BROWSER),
446 filetype_get_icon(ATTR_DIRECTORY));
448 else
449 gui_synclist_set_title(&tree_lists, title,
450 filetype_get_icon(ATTR_DIRECTORY));
452 else
454 /* Must clear the title as the list is reused */
455 gui_synclist_set_title(&tree_lists, NULL, NOICON);
457 #endif
460 gui_synclist_set_nb_items(&tree_lists, tc.filesindir);
461 gui_synclist_set_icon_callback(&tree_lists,
462 global_settings.show_icons?tree_get_fileicon:NULL);
463 if( tc.selected_item >= tc.filesindir)
464 tc.selected_item=tc.filesindir-1;
466 gui_synclist_select_item(&tree_lists, tc.selected_item);
467 #ifdef HAVE_BUTTONBAR
468 if (global_settings.buttonbar) {
469 if (*tc.dirfilter < NUM_FILTER_MODES)
470 gui_buttonbar_set(&tree_buttonbar, str(LANG_SYSFONT_DIRBROWSE_F1),
471 str(LANG_SYSFONT_DIRBROWSE_F2),
472 str(LANG_SYSFONT_DIRBROWSE_F3));
473 else
474 gui_buttonbar_set(&tree_buttonbar, "<<<", "", "");
475 gui_buttonbar_draw(&tree_buttonbar);
477 #endif
478 gui_synclist_draw(&tree_lists);
479 gui_synclist_speak_item(&tree_lists);
480 return tc.filesindir;
483 /* load tracks from specified directory to resume play */
484 void resume_directory(const char *dir)
486 int dirfilter = *tc.dirfilter;
487 int ret;
488 #ifdef HAVE_TAGCACHE
489 bool id3db = *tc.dirfilter == SHOW_ID3DB;
490 #endif
491 /* make sure the dirfilter is sane. The only time it should be possible
492 * thats its not is when resume playlist is called from a plugin
494 #ifdef HAVE_TAGCACHE
495 if (!id3db)
496 #endif
497 *tc.dirfilter = global_settings.dirfilter;
498 ret = ft_load(&tc, dir);
499 *tc.dirfilter = dirfilter;
500 if (ret < 0)
501 return;
502 lastdir[0] = 0;
504 ft_build_playlist(&tc, 0);
506 #ifdef HAVE_TAGCACHE
507 if (id3db)
508 tagtree_load(&tc);
509 #endif
512 /* Returns the current working directory and also writes cwd to buf if
513 non-NULL. In case of error, returns NULL. */
514 char *getcwd(char *buf, getcwd_size_t size)
516 if (!buf)
517 return tc.currdir;
518 else if (size)
520 if ((getcwd_size_t)strlcpy(buf, tc.currdir, size) < size)
521 return buf;
523 /* size == 0, or truncation in strlcpy */
524 return NULL;
527 /* Force a reload of the directory next time directory browser is called */
528 void reload_directory(void)
530 reload_dir = true;
533 char* get_current_file(char* buffer, size_t buffer_len)
535 #ifdef HAVE_TAGCACHE
536 /* in ID3DB mode it is a bad idea to call this function */
537 /* (only happens with `follow playlist') */
538 if( *tc.dirfilter == SHOW_ID3DB )
539 return NULL;
540 #endif
542 struct entry* dc = tc.dircache;
543 struct entry* e = &dc[tc.selected_item];
544 if (getcwd(buffer, buffer_len))
546 if (tc.dirlength)
548 if (buffer[strlen(buffer)-1] != '/')
549 strlcat(buffer, "/", buffer_len);
550 if (strlcat(buffer, e->name, buffer_len) >= buffer_len)
551 return NULL;
553 return buffer;
555 return NULL;
558 /* Allow apps to change our dirfilter directly (required for sub browsers)
559 if they're suddenly going to become a file browser for example */
560 void set_dirfilter(int l_dirfilter)
562 *tc.dirfilter = l_dirfilter;
565 /* Selects a file and update tree context properly */
566 void set_current_file(char *path)
568 char *name;
569 int i;
571 #ifdef HAVE_TAGCACHE
572 /* in ID3DB mode it is a bad idea to call this function */
573 /* (only happens with `follow playlist') */
574 if( *tc.dirfilter == SHOW_ID3DB )
575 return;
576 #endif
578 /* separate directory from filename */
579 /* gets the directory's name and put it into tc.currdir */
580 name = strrchr(path+1,'/');
581 if (name)
583 *name = 0;
584 strcpy(tc.currdir, path);
585 *name = '/';
586 name++;
588 else
590 strcpy(tc.currdir, "/");
591 name = path+1;
594 strcpy(lastfile, name);
597 /* If we changed dir we must recalculate the dirlevel
598 and adjust the selected history properly */
599 if (strncmp(tc.currdir,lastdir,sizeof(lastdir)))
601 tc.dirlevel = 0;
602 tc.selected_item_history[tc.dirlevel] = -1;
604 /* use '/' to calculate dirlevel */
605 for (i = 1; path[i] != '\0'; i++)
607 if (path[i] == '/')
609 tc.dirlevel++;
610 tc.selected_item_history[tc.dirlevel] = -1;
614 if (ft_load(&tc, NULL) >= 0)
616 tc.selected_item = tree_get_file_position(lastfile);
621 /* main loop, handles key events */
622 static int dirbrowse()
624 int numentries=0;
625 char buf[MAX_PATH];
626 int button, oldbutton;
627 bool reload_root = false;
628 int lastfilter = *tc.dirfilter;
629 bool lastsortcase = global_settings.sort_case;
630 bool exit_func = false;
632 char* currdir = tc.currdir; /* just a shortcut */
633 #ifdef HAVE_TAGCACHE
634 bool id3db = *tc.dirfilter == SHOW_ID3DB;
636 if (id3db)
637 curr_context=CONTEXT_ID3DB;
638 else
639 #endif
640 curr_context=CONTEXT_TREE;
641 if (tc.selected_item < 0)
642 tc.selected_item = 0;
643 #ifdef HAVE_TAGCACHE
644 tc.firstpos = 0;
645 lasttable = -1;
646 lastextra = -1;
647 lastfirstpos = 0;
648 #endif
650 start_wps = false;
651 numentries = update_dir();
652 reload_dir = false;
653 if (numentries == -1)
654 return GO_TO_PREVIOUS; /* currdir is not a directory */
656 if (*tc.dirfilter > NUM_FILTER_MODES && numentries==0)
658 splash(HZ*2, ID2P(LANG_NO_FILES));
659 return GO_TO_PREVIOUS; /* No files found for rockbox_browser() */
662 gui_synclist_draw(&tree_lists);
663 while(1) {
664 struct entry *dircache = tc.dircache;
665 bool restore = false;
666 if (tc.dirlevel < 0)
667 tc.dirlevel = 0; /* shouldnt be needed.. this code needs work! */
668 #ifdef BOOTFILE
669 if (boot_changed) {
670 static const char *lines[]={ID2P(LANG_BOOT_CHANGED), ID2P(LANG_REBOOT_NOW)};
671 static const struct text_message message={lines, 2};
672 if(gui_syncyesno_run(&message, NULL, NULL)==YESNO_YES)
673 rolo_load("/" BOOTFILE);
674 restore = true;
675 boot_changed = false;
677 #endif
678 button = get_action(CONTEXT_TREE,
679 list_do_action_timeout(&tree_lists, HZ/2));
680 oldbutton = button;
681 gui_synclist_do_button(&tree_lists, &button,LIST_WRAP_UNLESS_HELD);
682 tc.selected_item = gui_synclist_get_sel_pos(&tree_lists);
683 switch ( button ) {
684 case ACTION_STD_OK:
685 /* nothing to do if no files to display */
686 if ( numentries == 0 )
687 break;
689 #ifdef HAVE_TAGCACHE
690 switch (id3db?tagtree_enter(&tc):ft_enter(&tc))
691 #else
692 switch (ft_enter(&tc))
693 #endif
695 case GO_TO_FILEBROWSER: reload_dir = true; break;
696 case GO_TO_WPS:
697 return GO_TO_WPS;
698 #if CONFIG_TUNER
699 case GO_TO_FM:
700 return GO_TO_FM;
701 #endif
702 case GO_TO_ROOT: exit_func = true; break;
703 default: break;
705 restore = true;
706 break;
708 case ACTION_STD_CANCEL:
709 if (*tc.dirfilter > NUM_FILTER_MODES && tc.dirlevel < 1) {
710 exit_func = true;
711 break;
713 if ((*tc.dirfilter == SHOW_ID3DB && tc.dirlevel == 0) ||
714 ((*tc.dirfilter != SHOW_ID3DB && !strcmp(currdir,"/"))))
716 #ifdef HAVE_LCD_BITMAP /* charcell doesnt have ACTION_TREE_PGLEFT so this isnt needed */
717 if (oldbutton == ACTION_TREE_PGLEFT)
718 break;
719 else
720 #endif
721 return GO_TO_ROOT;
724 #ifdef HAVE_TAGCACHE
725 if (id3db)
726 tagtree_exit(&tc);
727 else
728 #endif
729 if (ft_exit(&tc) == 3)
730 exit_func = true;
732 restore = true;
733 break;
735 case ACTION_TREE_STOP:
736 if (list_stop_handler())
737 restore = true;
738 break;
740 case ACTION_STD_MENU:
741 return GO_TO_ROOT;
742 break;
744 #ifdef HAVE_RECORDING
745 case ACTION_STD_REC:
746 return GO_TO_RECSCREEN;
747 #endif
749 case ACTION_TREE_WPS:
750 return GO_TO_PREVIOUS_MUSIC;
751 break;
752 #ifdef HAVE_QUICKSCREEN
753 case ACTION_STD_QUICKSCREEN:
754 /* don't enter f2 from plugin browser */
755 if (*tc.dirfilter < NUM_FILTER_MODES)
757 if (quick_screen_quick(button))
758 reload_dir = true;
759 restore = true;
761 break;
762 #endif
763 #ifdef BUTTON_F3
764 case ACTION_F3:
765 /* don't enter f3 from plugin browser */
766 if (*tc.dirfilter < NUM_FILTER_MODES)
768 if (quick_screen_f3(ACTION_F3))
769 reload_dir = true;
770 restore = true;
772 break;
773 #endif
775 #ifdef HAVE_HOTKEY
776 case ACTION_TREE_HOTKEY:
777 if (!global_settings.hotkey_tree)
778 break;
779 /* fall through */
780 #endif
781 case ACTION_STD_CONTEXT:
783 bool hotkey = button == ACTION_TREE_HOTKEY;
784 int onplay_result;
785 int attr = 0;
787 if(!numentries)
788 onplay_result = onplay(NULL, 0, curr_context, hotkey);
789 else {
790 #ifdef HAVE_TAGCACHE
791 if (id3db)
793 if (tagtree_get_attr(&tc) == FILE_ATTR_AUDIO)
795 attr = FILE_ATTR_AUDIO;
796 tagtree_get_filename(&tc, buf, sizeof(buf));
798 else
799 attr = ATTR_DIRECTORY;
801 else
802 #endif
804 attr = dircache[tc.selected_item].attr;
806 if (currdir[1]) /* Not in / */
807 snprintf(buf, sizeof buf, "%s/%s",
808 currdir,
809 dircache[tc.selected_item].name);
810 else /* In / */
811 snprintf(buf, sizeof buf, "/%s",
812 dircache[tc.selected_item].name);
814 onplay_result = onplay(buf, attr, curr_context, hotkey);
816 switch (onplay_result)
818 case ONPLAY_MAINMENU:
819 return GO_TO_ROOT;
821 case ONPLAY_OK:
822 restore = true;
823 break;
825 case ONPLAY_RELOAD_DIR:
826 reload_dir = true;
827 break;
829 case ONPLAY_START_PLAY:
830 return GO_TO_WPS;
831 break;
833 break;
836 #ifdef HAVE_HOTSWAP
837 case SYS_FS_CHANGED:
838 #ifdef HAVE_TAGCACHE
839 if (!id3db)
840 #endif
841 reload_dir = true;
842 /* The 'dir no longer valid' situation will be caught later
843 * by checking the showdir() result. */
844 break;
845 #endif
847 default:
848 if (default_event_handler(button) == SYS_USB_CONNECTED)
850 if(*tc.dirfilter > NUM_FILTER_MODES)
851 /* leave sub-browsers after usb, doing otherwise
852 might be confusing to the user */
853 exit_func = true;
854 else
855 reload_dir = true;
857 break;
859 if (start_wps)
860 return GO_TO_WPS;
861 if (button && !IS_SYSEVENT(button))
863 storage_spin();
867 check_rescan:
868 /* do we need to rescan dir? */
869 if (reload_dir || reload_root ||
870 lastfilter != *tc.dirfilter ||
871 lastsortcase != global_settings.sort_case)
873 if (reload_root) {
874 strcpy(currdir, "/");
875 tc.dirlevel = 0;
876 #ifdef HAVE_TAGCACHE
877 tc.currtable = 0;
878 tc.currextra = 0;
879 lasttable = -1;
880 lastextra = -1;
881 #endif
882 reload_root = false;
885 if (!reload_dir)
887 gui_synclist_select_item(&tree_lists, 0);
888 gui_synclist_draw(&tree_lists);
889 tc.selected_item = 0;
890 lastdir[0] = 0;
893 lastfilter = *tc.dirfilter;
894 lastsortcase = global_settings.sort_case;
895 restore = true;
898 if (exit_func)
899 return GO_TO_PREVIOUS;
901 if (restore || reload_dir) {
902 /* restore display */
903 numentries = update_dir();
904 reload_dir = false;
905 if (currdir[1] && (numentries < 0))
906 { /* not in root and reload failed */
907 reload_root = true; /* try root */
908 goto check_rescan;
912 return true;
915 bool create_playlist(void)
917 char filename[MAX_PATH];
919 snprintf(filename, sizeof filename, "%s.m3u8",
920 tc.currdir[1] ? tc.currdir : "/root");
921 splashf(0, "%s %s", str(LANG_CREATING), filename);
923 trigger_cpu_boost();
924 catalog_add_to_a_playlist(tc.currdir, ATTR_DIRECTORY, true, filename);
925 cancel_cpu_boost();
927 return true;
930 int rockbox_browse(const char *root, int dirfilter)
932 int ret_val = 0;
933 int *last_filter = tc.dirfilter;
934 tc.dirfilter = &dirfilter;
935 tc.sort_dir = global_settings.sort_dir;
937 reload_dir = true;
938 if (dirfilter >= NUM_FILTER_MODES)
940 static struct tree_context backup;
941 int last_context;
942 const char *dir, *ext, *setting = NULL;
944 backup = tc;
945 tc.selected_item = 0;
946 tc.dirlevel = 0;
947 memcpy(tc.currdir, root, sizeof(tc.currdir));
948 start_wps = false;
949 last_context = curr_context;
951 /* if we are in a special settings folder, center the current setting */
952 switch(dirfilter)
954 case SHOW_LNG:
955 dir = LANG_DIR;
956 ext = "lng";
957 if (global_settings.lang_file[0])
958 setting = global_settings.lang_file;
959 else
960 setting = "english";
961 break;
962 case SHOW_WPS:
963 dir = WPS_DIR;
964 ext = "wps";
965 setting = global_settings.wps_file;
966 break;
967 #ifdef HAVE_REMOTE_LCD
968 case SHOW_RWPS:
969 dir = WPS_DIR;
970 ext = "rwps";
971 setting = global_settings.rwps_file;
972 break;
973 case SHOW_RSBS:
974 dir = WPS_DIR;
975 ext = "rsbs";
976 setting = global_settings.rsbs_file;
977 break;
978 #if CONFIG_TUNER
979 case SHOW_RFMS:
980 dir = WPS_DIR;
981 ext = "rfms";
982 setting = global_settings.rfms_file;
983 break;
984 #endif /* CONFIG_TUNER */
985 #endif
986 #ifdef HAVE_LCD_BITMAP
987 case SHOW_FONT:
988 dir = FONT_DIR;
989 ext = "fnt";
990 setting = global_settings.font_file;
991 break;
992 case SHOW_SBS:
993 dir = WPS_DIR;
994 ext = "sbs";
995 setting = global_settings.sbs_file;
996 break;
997 #if CONFIG_TUNER
998 case SHOW_FMS:
999 dir = WPS_DIR;
1000 ext = "fms";
1001 setting = global_settings.fms_file;
1002 break;
1003 #endif /* CONFIG_TUNER */
1004 #endif
1005 #if CONFIG_TUNER
1006 case SHOW_FMR:
1007 dir = FMPRESET_PATH;
1008 ext = "fmr";
1009 setting = global_settings.fmr_file;
1010 break;
1011 #endif
1012 default:
1013 dir = ext = setting = NULL;
1014 break;
1017 /* If we've found a file to center on, do it */
1018 if (setting)
1020 char current[MAX_PATH], _dir[MAX_PATH];
1021 /* if setting != NULL, ext and dir are not used uninitialized */
1022 snprintf(current, sizeof(current), "%s/%s.%s",
1023 get_user_file_path(dir, 0, _dir, sizeof(_dir)), setting, ext);
1024 set_current_file(current);
1025 /* set_current_file changes dirlevel, change it back */
1026 tc.dirlevel = 0;
1029 ret_val = dirbrowse();
1030 tc = backup;
1031 curr_context = last_context;
1033 else
1035 static char buf[MAX_PATH];
1036 if (dirfilter != SHOW_ID3DB)
1037 tc.dirfilter = &global_settings.dirfilter;
1038 strcpy(buf,root);
1039 set_current_file(buf);
1040 ret_val = dirbrowse();
1042 tc.dirfilter = last_filter;
1043 return ret_val;
1046 void tree_mem_init(void)
1048 /* We copy the settings value in case it is changed by the user. We can't
1049 use it until the next reboot. */
1050 max_files = global_settings.max_files_in_dir;
1052 /* initialize tree context struct */
1053 memset(&tc, 0, sizeof(tc));
1054 tc.dirfilter = &global_settings.dirfilter;
1055 tc.sort_dir = global_settings.sort_dir;
1057 tc.name_buffer_size = AVERAGE_FILENAME_LENGTH * max_files;
1058 tc.name_buffer = buffer_alloc(tc.name_buffer_size);
1060 tc.dircache_size = max_files * sizeof(struct entry);
1061 tc.dircache = buffer_alloc(tc.dircache_size);
1062 tree_get_filetypes(&filetypes, &filetypes_count);
1065 bool bookmark_play(char *resume_file, int index, int offset, int seed,
1066 char *filename)
1068 int i;
1069 char* suffix = strrchr(resume_file, '.');
1070 bool started = false;
1072 if (suffix != NULL &&
1073 (!strcasecmp(suffix, ".m3u") || !strcasecmp(suffix, ".m3u8")))
1075 /* Playlist playback */
1076 char* slash;
1077 /* check that the file exists */
1078 if(!file_exists(resume_file))
1079 return false;
1081 slash = strrchr(resume_file,'/');
1082 if (slash)
1084 char* cp;
1085 *slash=0;
1087 cp=resume_file;
1088 if (!cp[0])
1089 cp="/";
1091 if (playlist_create(cp, slash+1) != -1)
1093 if (global_settings.playlist_shuffle)
1094 playlist_shuffle(seed, -1);
1095 playlist_start(index,offset);
1096 started = true;
1098 *slash='/';
1101 else
1103 /* Directory playback */
1104 lastdir[0]='\0';
1105 if (playlist_create(resume_file, NULL) != -1)
1107 char filename_buf[MAX_PATH + 1];
1108 const char* peek_filename;
1109 resume_directory(resume_file);
1110 if (global_settings.playlist_shuffle)
1111 playlist_shuffle(seed, -1);
1113 /* Check if the file is at the same spot in the directory,
1114 else search for it */
1115 peek_filename = playlist_peek(index, filename_buf,
1116 sizeof(filename_buf));
1118 if (peek_filename == NULL)
1120 /* playlist has shrunk, search from the top */
1121 index = 0;
1122 peek_filename = playlist_peek(index, filename_buf,
1123 sizeof(filename_buf));
1124 if (peek_filename == NULL)
1125 return false;
1128 if (strcmp(strrchr(peek_filename, '/') + 1, filename))
1130 for ( i=0; i < playlist_amount(); i++ )
1132 peek_filename = playlist_peek(i, filename_buf,
1133 sizeof(filename_buf));
1135 if (peek_filename == NULL)
1136 return false;
1138 if (!strcmp(strrchr(peek_filename, '/') + 1, filename))
1139 break;
1141 if (i < playlist_amount())
1142 index = i;
1143 else
1144 return false;
1146 playlist_start(index,offset);
1147 started = true;
1151 if (started)
1152 start_wps = true;
1153 return started;
1156 static void say_filetype(int attr)
1158 /* try to find a voice ID for the extension, if known */
1159 int j;
1160 attr &= FILE_ATTR_MASK; /* file type */
1161 for (j=0; j<filetypes_count; j++)
1162 if (attr == filetypes[j].tree_attr)
1164 talk_id(filetypes[j].voiceclip, true);
1165 return;
1169 static int ft_play_dirname(char* name)
1171 #if CONFIG_CODEC != SWCODEC
1172 if (audio_status() & AUDIO_STATUS_PLAY)
1173 return 0;
1174 #endif
1176 if(talk_file(tc.currdir, name, dir_thumbnail_name, NULL,
1177 NULL, false))
1179 if(global_settings.talk_filetype)
1180 talk_id(VOICE_DIR, true);
1181 return 1;
1183 else
1184 return -1;
1187 static void ft_play_filename(char *dir, char *file)
1189 #if CONFIG_CODEC != SWCODEC
1190 if (audio_status() & AUDIO_STATUS_PLAY)
1191 return;
1192 #endif
1194 if (strlen(file) >= strlen(file_thumbnail_ext)
1195 && strcasecmp(&file[strlen(file) - strlen(file_thumbnail_ext)],
1196 file_thumbnail_ext))
1197 /* file has no .talk extension */
1198 talk_file(dir, NULL, file, file_thumbnail_ext,
1199 NULL, false);
1200 else
1201 /* it already is a .talk file, play this directly, but prefix it. */
1202 talk_file(dir, NULL, file, NULL,
1203 TALK_IDARRAY(LANG_VOICE_DIR_HOVER), false);
1206 /* These two functions are called by the USB and shutdown handlers */
1207 void tree_flush(void)
1209 #ifdef HAVE_TAGCACHE
1210 tagcache_shutdown();
1211 #endif
1213 #ifdef HAVE_TC_RAMCACHE
1214 tagcache_unload_ramcache();
1215 #endif
1217 #ifdef HAVE_DIRCACHE
1219 int old_val = global_status.dircache_size;
1220 if (global_settings.dircache)
1222 if (!dircache_is_initializing())
1223 global_status.dircache_size = dircache_get_cache_size();
1224 # ifdef HAVE_EEPROM_SETTINGS
1225 if (firmware_settings.initialized)
1226 dircache_save();
1227 # endif
1228 dircache_disable();
1230 else
1232 global_status.dircache_size = 0;
1234 if (old_val != global_status.dircache_size)
1235 status_save();
1237 #endif
1240 void tree_restore(void)
1242 #ifdef HAVE_EEPROM_SETTINGS
1243 firmware_settings.disk_clean = false;
1244 #endif
1246 #ifdef HAVE_TC_RAMCACHE
1247 remove(TAGCACHE_STATEFILE);
1248 #endif
1250 #ifdef HAVE_DIRCACHE
1251 remove(DIRCACHE_FILE);
1252 if (global_settings.dircache)
1254 /* Print "Scanning disk..." to the display. */
1255 splash(0, str(LANG_SCANNING_DISK));
1257 dircache_build(global_status.dircache_size);
1259 #endif
1260 #ifdef HAVE_TAGCACHE
1261 tagcache_start_scan();
1262 #endif