Unify the way functions are called from menus.
[Rockbox.git] / apps / tree.c
blobceeb64e74811fa14fa9314db4ea55062d0a55485
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2002 Daniel Stenberg
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
19 #include <stdio.h>
20 #include <string.h>
21 #include <stdlib.h>
22 #include <stdbool.h>
24 #include "applimits.h"
25 #include "dir.h"
26 #include "file.h"
27 #include "lcd.h"
28 #include "font.h"
29 #include "backlight.h"
30 #include "button.h"
31 #include "kernel.h"
32 #include "usb.h"
33 #include "tree.h"
34 #include "main_menu.h"
35 #include "sprintf.h"
36 #include "audio.h"
37 #include "playlist.h"
38 #include "menu.h"
39 #include "gwps.h"
40 #include "settings.h"
41 #include "status.h"
42 #include "debug.h"
43 #include "ata.h"
44 #include "rolo.h"
45 #include "icons.h"
46 #include "lang.h"
47 #include "language.h"
48 #include "screens.h"
49 #include "keyboard.h"
50 #include "bookmark.h"
51 #include "onplay.h"
52 #include "buffer.h"
53 #include "plugin.h"
54 #include "power.h"
55 #include "action.h"
56 #include "talk.h"
57 #include "filetypes.h"
58 #include "misc.h"
59 #include "filetree.h"
60 #include "tagtree.h"
61 #ifdef HAVE_RECORDING
62 #include "recorder/recording.h"
63 #endif
64 #include "rtc.h"
65 #include "dircache.h"
66 #ifdef HAVE_TAGCACHE
67 #include "tagcache.h"
68 #endif
69 #include "yesno.h"
70 #include "gwps-common.h"
71 #include "eeprom_settings.h"
72 #include "scrobbler.h"
74 /* gui api */
75 #include "list.h"
76 #include "statusbar.h"
77 #include "splash.h"
78 #include "buttonbar.h"
79 #include "textarea.h"
80 #include "action.h"
82 #include "root_menu.h"
84 #if LCD_DEPTH > 1
85 #include "backdrop.h"
86 #endif
88 /* a table for the know file types */
89 const struct filetype filetypes[] = {
90 { "mp3", TREE_ATTR_MPA, Icon_Audio, VOICE_EXT_MPA },
91 { "mp2", TREE_ATTR_MPA, Icon_Audio, VOICE_EXT_MPA },
92 { "mpa", TREE_ATTR_MPA, Icon_Audio, VOICE_EXT_MPA },
93 #if CONFIG_CODEC == SWCODEC
94 /* Temporary hack to allow playlist creation */
95 { "mp1", TREE_ATTR_MPA, Icon_Audio, VOICE_EXT_MPA },
96 { "ogg", TREE_ATTR_MPA, Icon_Audio, VOICE_EXT_MPA },
97 { "wma", TREE_ATTR_MPA, Icon_Audio, VOICE_EXT_MPA },
98 { "wav", TREE_ATTR_MPA, Icon_Audio, VOICE_EXT_MPA },
99 { "flac",TREE_ATTR_MPA, Icon_Audio, VOICE_EXT_MPA },
100 { "ac3", TREE_ATTR_MPA, Icon_Audio, VOICE_EXT_MPA },
101 { "a52", TREE_ATTR_MPA, Icon_Audio, VOICE_EXT_MPA },
102 { "mpc", TREE_ATTR_MPA, Icon_Audio, VOICE_EXT_MPA },
103 { "wv", TREE_ATTR_MPA, Icon_Audio, VOICE_EXT_MPA },
104 { "m4a", TREE_ATTR_MPA, Icon_Audio, VOICE_EXT_MPA },
105 { "m4b", TREE_ATTR_MPA, Icon_Audio, VOICE_EXT_MPA },
106 { "mp4", TREE_ATTR_MPA, Icon_Audio, VOICE_EXT_MPA },
107 { "shn", TREE_ATTR_MPA, Icon_Audio, VOICE_EXT_MPA },
108 { "aif", TREE_ATTR_MPA, Icon_Audio, VOICE_EXT_MPA },
109 { "aiff",TREE_ATTR_MPA, Icon_Audio, VOICE_EXT_MPA },
110 { "spx" ,TREE_ATTR_MPA, Icon_Audio, VOICE_EXT_MPA },
111 { "sid", TREE_ATTR_MPA, Icon_Audio, VOICE_EXT_MPA },
112 { "adx", TREE_ATTR_MPA, Icon_Audio, VOICE_EXT_MPA },
113 { "nsf", TREE_ATTR_MPA, Icon_Audio, VOICE_EXT_MPA },
114 { "nsfe", TREE_ATTR_MPA, Icon_Audio, VOICE_EXT_MPA },
115 { "spc", TREE_ATTR_MPA, Icon_Audio, VOICE_EXT_MPA },
116 #endif
117 { "m3u", TREE_ATTR_M3U, Icon_Playlist, LANG_PLAYLIST },
118 { "m3u8", TREE_ATTR_M3U, Icon_Playlist, LANG_PLAYLIST },
119 { "cfg", TREE_ATTR_CFG, Icon_Config, VOICE_EXT_CFG },
120 { "wps", TREE_ATTR_WPS, Icon_Wps, VOICE_EXT_WPS },
121 #ifdef HAVE_REMOTE_LCD
122 { "rwps", TREE_ATTR_RWPS, Icon_Wps, VOICE_EXT_RWPS },
123 #endif
124 #if LCD_DEPTH > 1
125 { "bmp", TREE_ATTR_BMP, Icon_Wps, VOICE_EXT_WPS },
126 #endif
127 #if CONFIG_TUNER
128 { "fmr", TREE_ATTR_FMR, Icon_Preset, LANG_FMR },
129 #endif
130 { "lng", TREE_ATTR_LNG, Icon_Language, LANG_LANGUAGE },
131 { "rock",TREE_ATTR_ROCK,Icon_Plugin, VOICE_EXT_ROCK },
132 #ifdef HAVE_LCD_BITMAP
133 { "fnt", TREE_ATTR_FONT,Icon_Font, VOICE_EXT_FONT },
134 { "kbd", TREE_ATTR_KBD, Icon_Keyboard, VOICE_EXT_KBD },
135 #endif
136 { "bmark",TREE_ATTR_BMARK, Icon_Bookmark, VOICE_EXT_BMARK },
137 { "cue", TREE_ATTR_CUE, Icon_Bookmark, LANG_CUESHEET },
138 #ifdef BOOTFILE_EXT
139 { BOOTFILE_EXT, TREE_ATTR_MOD, Icon_Firmware, VOICE_EXT_AJZ },
140 #endif /* #ifndef SIMULATOR */
143 struct gui_synclist tree_lists;
145 /* I put it here because other files doesn't use it yet,
146 * but should be elsewhere since it will be used mostly everywhere */
147 #ifdef HAS_BUTTONBAR
148 struct gui_buttonbar tree_buttonbar;
149 #endif
150 static struct tree_context tc;
152 bool boot_changed = false;
154 char lastfile[MAX_PATH];
155 static char lastdir[MAX_PATH];
156 #ifdef HAVE_TAGCACHE
157 static int lasttable, lastextra, lastfirstpos;
158 #endif
159 static int max_files = 0;
161 static bool reload_dir = false;
163 static bool start_wps = false;
164 static int curr_context = false;/* id3db or tree*/
166 static int dirbrowse(void);
167 static int ft_play_filenumber(int pos, int attr);
168 static int ft_play_dirname(char* name);
169 static void ft_play_filename(char *dir, char *file);
172 * removes the extension of filename (if it doesn't start with a .)
173 * puts the result in buffer
175 static char * strip_extension(char * filename, char * buffer)
177 int dotpos;
178 char * dot=strrchr(filename, '.');
179 if(dot!=0 && filename[0]!='.')
181 dotpos = dot-filename;
182 strncpy(buffer, filename, dotpos);
183 buffer[dotpos]='\0';
184 return(buffer);
186 else
187 return(filename);
190 static char * tree_get_filename(int selected_item, void * data, char *buffer)
192 struct tree_context * local_tc=(struct tree_context *)data;
193 char *name;
194 int attr=0;
195 #ifdef HAVE_TAGCACHE
196 bool id3db = *(local_tc->dirfilter) == SHOW_ID3DB;
198 if (id3db)
200 return tagtree_get_entry(&tc, selected_item)->name;
202 else
203 #endif
205 struct entry* dc = local_tc->dircache;
206 struct entry* e = &dc[selected_item];
207 name = e->name;
208 attr = e->attr;
210 /* if any file filter is on, and if it's not a directory,
211 * strip the extension */
213 if ( (*(local_tc->dirfilter) != SHOW_ID3DB) && !(attr & ATTR_DIRECTORY)
214 && (*(local_tc->dirfilter) != SHOW_ALL) )
216 return(strip_extension(name, buffer));
218 return(name);
222 static void tree_get_fileicon(int selected_item, void * data, ICON * icon)
224 struct tree_context * local_tc=(struct tree_context *)data;
225 #ifdef HAVE_TAGCACHE
226 bool id3db = *(local_tc->dirfilter) == SHOW_ID3DB;
227 if (id3db) {
228 *icon = (ICON)tagtree_get_icon(&tc);
230 else
231 #endif
233 struct entry* dc = local_tc->dircache;
234 struct entry* e = &dc[selected_item];
235 *icon = (ICON)filetype_get_icon(e->attr);
239 bool check_rockboxdir(void)
241 DIR *dir = opendir(ROCKBOX_DIR);
242 if(!dir)
243 { /* No need to localise this message.
244 If .rockbox is missing, it wouldn't work anyway */
245 int i;
246 FOR_NB_SCREENS(i)
247 screens[i].clear_display();
248 gui_syncsplash(HZ*2, "No .rockbox directory");
249 FOR_NB_SCREENS(i)
250 screens[i].clear_display();
251 gui_syncsplash(HZ*2, "Installation incomplete");
252 return false;
254 closedir(dir);
255 return true;
258 void browse_root(void)
260 gui_sync_wps_screen_init();
262 filetype_init();
263 check_rockboxdir();
265 strcpy(tc.currdir, "/");
267 #ifdef HAVE_LCD_CHARCELLS
268 int i;
269 FOR_NB_SCREENS(i)
270 screens[i].double_height(false);
271 #endif
272 #ifdef HAS_BUTTONBAR
273 gui_buttonbar_init(&tree_buttonbar);
274 /* since archos only have one screen, no need to create more than that */
275 gui_buttonbar_set_display(&tree_buttonbar, &(screens[SCREEN_MAIN]) );
276 #endif
277 gui_synclist_init(&tree_lists, &tree_get_filename, &tc, false, 1);
278 gui_synclist_set_icon_callback(&tree_lists, &tree_get_fileicon);
279 /* not the best place for this call... but... */
280 root_menu();
283 void tree_get_filetypes(const struct filetype** types, int* count)
285 *types = filetypes;
286 *count = sizeof(filetypes) / sizeof(*filetypes);
289 struct tree_context* tree_get_context(void)
291 return &tc;
294 /* talkbox hovering delay, to avoid immediate disk activity */
295 #define HOVER_DELAY (HZ/2)
297 * Returns the position of a given file in the current directory
298 * returns -1 if not found
300 static int tree_get_file_position(char * filename)
302 int i;
304 /* use lastfile to determine the selected item (default=0) */
305 for (i=0; i < tc.filesindir; i++)
307 struct entry* dc = tc.dircache;
308 struct entry* e = &dc[i];
309 if (!strcasecmp(e->name, filename))
310 return(i);
312 return(-1);/* no file can match, returns undefined */
316 * Called when a new dir is loaded (for example when returning from other apps ...)
317 * also completely redraws the tree
319 static int update_dir(void)
321 bool changed = false;
322 #ifdef HAVE_TAGCACHE
323 bool id3db = *tc.dirfilter == SHOW_ID3DB;
324 /* Checks for changes */
325 if (id3db) {
326 if (tc.currtable != lasttable ||
327 tc.currextra != lastextra ||
328 tc.firstpos != lastfirstpos ||
329 reload_dir)
331 if (tagtree_load(&tc) < 0)
332 return -1;
334 lasttable = tc.currtable;
335 lastextra = tc.currextra;
336 lastfirstpos = tc.firstpos;
337 changed = true;
340 else
341 #endif
343 /* if the tc.currdir has been changed, reload it ...*/
344 if (strncmp(tc.currdir, lastdir, sizeof(lastdir)) || reload_dir) {
346 if (ft_load(&tc, NULL) < 0)
347 return -1;
348 strcpy(lastdir, tc.currdir);
349 changed = true;
352 /* if selected item is undefined */
353 if (tc.selected_item == -1)
355 /* use lastfile to determine the selected item */
356 tc.selected_item = tree_get_file_position(lastfile);
358 /* If the file doesn't exists, select the first one (default) */
359 if(tc.selected_item < 0)
360 tc.selected_item = 0;
361 changed = true;
363 if (changed)
366 #ifdef HAVE_TAGCACHE
367 !id3db &&
368 #endif
369 (tc.dirfull ||
370 tc.filesindir == global_settings.max_files_in_dir) )
372 gui_syncsplash(HZ, str(LANG_SHOWDIR_BUFFER_FULL));
375 #ifdef HAVE_TAGCACHE
376 if (id3db)
378 if (global_settings.show_path_in_browser == SHOW_PATH_FULL
379 || global_settings.show_path_in_browser == SHOW_PATH_CURRENT)
381 gui_synclist_set_title(&tree_lists, tagtree_get_title(&tc),
382 filetype_get_icon(ATTR_DIRECTORY));
384 else
386 /* Must clear the title as the list is reused */
387 gui_synclist_set_title(&tree_lists, NULL, NOICON);
390 else
391 #endif
393 if (global_settings.show_path_in_browser == SHOW_PATH_FULL)
395 gui_synclist_set_title(&tree_lists, tc.currdir,
396 filetype_get_icon(ATTR_DIRECTORY));
398 else if (global_settings.show_path_in_browser == SHOW_PATH_CURRENT)
400 char *title = strrchr(tc.currdir, '/') + 1;
401 if (*title == '\0')
403 /* Display "Files" for the root dir */
404 gui_synclist_set_title(&tree_lists, str(LANG_DIR_BROWSER),
405 filetype_get_icon(ATTR_DIRECTORY));
407 else
408 gui_synclist_set_title(&tree_lists, title,
409 filetype_get_icon(ATTR_DIRECTORY));
411 else
413 /* Must clear the title as the list is reused */
414 gui_synclist_set_title(&tree_lists, NULL, NOICON);
418 gui_synclist_set_nb_items(&tree_lists, tc.filesindir);
419 gui_synclist_set_icon_callback(&tree_lists, tree_get_fileicon);
420 if( tc.selected_item >= tc.filesindir)
421 tc.selected_item=tc.filesindir-1;
423 gui_synclist_select_item(&tree_lists, tc.selected_item);
424 #ifdef HAS_BUTTONBAR
425 if (global_settings.buttonbar) {
426 if (*tc.dirfilter < NUM_FILTER_MODES)
427 gui_buttonbar_set(&tree_buttonbar, str(LANG_SYSFONT_DIRBROWSE_F1),
428 str(LANG_SYSFONT_DIRBROWSE_F2),
429 str(LANG_SYSFONT_DIRBROWSE_F3));
430 else
431 gui_buttonbar_set(&tree_buttonbar, "<<<", "", "");
432 gui_buttonbar_draw(&tree_buttonbar);
434 #endif
435 gui_synclist_draw(&tree_lists);
436 gui_syncstatusbar_draw(&statusbars, true);
437 return tc.filesindir;
440 /* load tracks from specified directory to resume play */
441 void resume_directory(const char *dir)
443 #ifdef HAVE_TAGCACHE
444 bool id3db = *tc.dirfilter == SHOW_ID3DB;
445 #endif
447 if (ft_load(&tc, dir) < 0)
448 return;
449 lastdir[0] = 0;
451 ft_build_playlist(&tc, 0);
453 #ifdef HAVE_TAGCACHE
454 if (id3db)
455 tagtree_load(&tc);
456 #endif
459 /* Returns the current working directory and also writes cwd to buf if
460 non-NULL. In case of error, returns NULL. */
461 char *getcwd(char *buf, int size)
463 if (!buf)
464 return tc.currdir;
465 else if (size > 0)
467 strncpy(buf, tc.currdir, size);
468 return buf;
470 else
471 return NULL;
474 /* Force a reload of the directory next time directory browser is called */
475 void reload_directory(void)
477 reload_dir = true;
480 void get_current_file(char* buffer, int buffer_len)
482 #ifdef HAVE_TAGCACHE
483 /* in ID3DB mode it is a bad idea to call this function */
484 /* (only happens with `follow playlist') */
485 if( *tc.dirfilter == SHOW_ID3DB )
486 return;
487 #endif
489 struct entry* dc = tc.dircache;
490 struct entry* e = &dc[tc.selected_item];
491 snprintf(buffer, buffer_len, "%s/%s", getcwd(NULL,0),
492 e->name);
495 /* Selects a file and update tree context properly */
496 static void set_current_file(char *path)
498 char *name;
499 int i;
501 #ifdef HAVE_TAGCACHE
502 /* in ID3DB mode it is a bad idea to call this function */
503 /* (only happens with `follow playlist') */
504 if( *tc.dirfilter == SHOW_ID3DB )
505 return;
506 #endif
508 /* separate directory from filename */
509 /* gets the directory's name and put it into tc.currdir */
510 name = strrchr(path+1,'/');
511 if (name)
513 *name = 0;
514 strcpy(tc.currdir, path);
515 *name = '/';
516 name++;
518 else
520 strcpy(tc.currdir, "/");
521 name = path+1;
524 strcpy(lastfile, name);
527 /* If we changed dir we must recalculate the dirlevel
528 and adjust the selected history properly */
529 if (strncmp(tc.currdir,lastdir,sizeof(lastdir)))
531 tc.dirlevel = 0;
532 tc.selected_item_history[tc.dirlevel] = -1;
534 /* use '/' to calculate dirlevel */
535 for (i = 1; path[i] != '\0'; i++)
537 if (path[i] == '/')
539 tc.dirlevel++;
540 tc.selected_item_history[tc.dirlevel] = -1;
544 if (ft_load(&tc, NULL) >= 0)
546 tc.selected_item = tree_get_file_position(lastfile);
550 #if defined(HAVE_TAGCACHE) && defined(HAVE_QUICKSCREEN)
551 static bool check_changed_id3mode(bool currmode)
553 if (currmode != (global_settings.dirfilter == SHOW_ID3DB)) {
554 currmode = global_settings.dirfilter == SHOW_ID3DB;
555 if (currmode) {
556 curr_context=CONTEXT_ID3DB;
557 tc.dirlevel = 0;
558 tagtree_load(&tc);
560 else
562 tc.dirlevel = 0;
563 curr_context=CONTEXT_TREE;
564 ft_load(&tc, NULL);
565 reload_dir = true;
568 return currmode;
570 #endif
572 /* main loop, handles key events */
573 static int dirbrowse()
575 int numentries=0;
576 char buf[MAX_PATH];
577 int lasti = -1;
578 unsigned button, returned_button;
579 bool reload_root = false;
580 int lastfilter = *tc.dirfilter;
581 bool lastsortcase = global_settings.sort_case;
582 bool need_update = true;
583 bool exit_func = false;
584 long thumbnail_time = -1; /* for delaying a thumbnail */
585 long last_cancel = 0;
587 char* currdir = tc.currdir; /* just a shortcut */
588 #ifdef HAVE_TAGCACHE
589 bool id3db = *tc.dirfilter == SHOW_ID3DB;
591 if (id3db)
592 curr_context=CONTEXT_ID3DB;
593 else
594 #endif
595 curr_context=CONTEXT_TREE;
596 if (tc.selected_item < 0)
597 tc.selected_item = 0;
598 #ifdef HAVE_TAGCACHE
599 tc.firstpos=0;
600 lasttable = -1;
601 lastextra = -1;
602 lastfirstpos = 0;
603 #endif
605 start_wps = false;
606 numentries = update_dir();
607 if (numentries == -1)
608 return false; /* currdir is not a directory */
610 if (*tc.dirfilter > NUM_FILTER_MODES && numentries==0)
612 gui_syncsplash(HZ*2, str(LANG_NO_FILES));
613 return false; /* No files found for rockbox_browser() */
616 while(1) {
617 struct entry *dircache = tc.dircache;
618 bool restore = false;
619 if (tc.dirlevel < 0)
620 tc.dirlevel = 0; /* shouldnt be needed.. this code needs work! */
621 #ifdef BOOTFILE
622 if (boot_changed) {
623 char *lines[]={str(LANG_BOOT_CHANGED), str(LANG_REBOOT_NOW)};
624 struct text_message message={lines, 2};
625 if(gui_syncyesno_run(&message, NULL, NULL)==YESNO_YES)
626 rolo_load("/" BOOTFILE);
627 restore = true;
628 boot_changed = false;
630 #endif
631 button = get_action(CONTEXT_TREE,HZ/5);
632 returned_button = gui_synclist_do_button(&tree_lists, button,LIST_WRAP_UNLESS_HELD);
633 if (returned_button)
634 need_update = true;
635 if (returned_button == ACTION_STD_CANCEL)
636 button = ACTION_STD_CANCEL;
638 tc.selected_item = gui_synclist_get_sel_pos(&tree_lists);
639 switch ( button ) {
640 case ACTION_STD_OK:
641 /* nothing to do if no files to display */
642 if ( numentries == 0 )
643 break;
645 #ifdef HAVE_TAGCACHE
646 switch (id3db?tagtree_enter(&tc):ft_enter(&tc))
647 #else
648 switch (ft_enter(&tc))
649 #endif
651 case 1: reload_dir = true; break;
652 case 2: start_wps = true; break;
653 case 3: exit_func = true; break;
654 default: break;
656 restore = true;
657 break;
659 case ACTION_STD_CANCEL:
660 if (*tc.dirfilter > NUM_FILTER_MODES && tc.dirlevel < 1) {
661 exit_func = true;
662 break;
664 if ((*tc.dirfilter == SHOW_ID3DB && tc.dirlevel == 0) ||
665 ((*tc.dirfilter != SHOW_ID3DB && !strcmp(currdir,"/"))))
667 if (last_cancel && TIME_BEFORE(current_tick, last_cancel+HZ/2))
669 last_cancel = 0;
670 action_signalscreenchange(); /* eat the cancel presses */
671 break;
673 else
674 return GO_TO_ROOT;
676 last_cancel = current_tick;
678 #ifdef HAVE_TAGCACHE
679 if (id3db)
680 tagtree_exit(&tc);
681 else
682 #endif
683 if (ft_exit(&tc) == 3)
684 exit_func = true;
686 restore = true;
687 break;
689 case ACTION_TREE_STOP:
690 if (*tc.dirfilter < NUM_FILTER_MODES)
692 /* Stop the music if it is playing */
693 if(audio_status()) {
694 if (!global_settings.party_mode) {
695 if (global_settings.fade_on_stop)
696 fade(0);
697 bookmark_autobookmark();
698 audio_stop();
701 #if CONFIG_CHARGING && \
702 (CONFIG_KEYPAD == RECORDER_PAD) && !defined(HAVE_SW_POWEROFF)
703 else {
704 if (!charger_inserted()) {
705 if(shutdown_screen())
706 reload_dir = true;
707 } else {
708 charging_splash();
710 restore = true;
712 #endif
714 #if CONFIG_CHARGING && !defined(HAVE_POWEROFF_WHILE_CHARGING)
716 static int last_off = 0;
717 if (current_tick - last_off < 50) {
718 if (charger_inserted()) {
719 charging_splash();
720 restore = true;
723 last_off = current_tick;
725 #endif
726 break; /* case ACTION_TREE_STOP: */
727 case ACTION_STD_MENU:
728 return GO_TO_ROOT;
729 break;
731 case ACTION_TREE_WPS:
732 return GO_TO_PREVIOUS_MUSIC;
733 break;
734 #ifdef HAVE_QUICKSCREEN
735 case ACTION_STD_QUICKSCREEN:
736 /* don't enter f2 from plugin browser */
737 if (*tc.dirfilter < NUM_FILTER_MODES)
739 if (quick_screen_quick(button))
740 reload_dir = true;
741 restore = true;
743 #ifdef HAVE_TAGCACHE
744 id3db = check_changed_id3mode(id3db);
745 reload_dir = true;
746 #endif
748 break;
749 #endif
750 #ifdef BUTTON_F3
751 case ACTION_F3:
752 /* don't enter f3 from plugin browser */
753 if (*tc.dirfilter < NUM_FILTER_MODES)
755 if (quick_screen_f3(BUTTON_F3))
756 reload_dir = true;
757 restore = true;
759 break;
760 #endif
762 case ACTION_STD_CONTEXT:
764 int onplay_result;
765 int attr = 0;
767 if(!numentries)
768 onplay_result = onplay(NULL, 0, curr_context);
769 else {
770 #ifdef HAVE_TAGCACHE
771 if (id3db)
773 if (tagtree_get_attr(&tc) == TREE_ATTR_MPA)
775 attr = TREE_ATTR_MPA;
776 tagtree_get_filename(&tc, buf, sizeof(buf));
778 else
779 attr = ATTR_DIRECTORY;
781 else
782 #endif
784 attr = dircache[tc.selected_item].attr;
786 if (currdir[1]) /* Not in / */
787 snprintf(buf, sizeof buf, "%s/%s",
788 currdir,
789 dircache[tc.selected_item].name);
790 else /* In / */
791 snprintf(buf, sizeof buf, "/%s",
792 dircache[tc.selected_item].name);
794 onplay_result = onplay(buf, attr, curr_context);
796 switch (onplay_result)
798 case ONPLAY_MAINMENU:
799 return GO_TO_ROOT;
801 case ONPLAY_OK:
802 restore = true;
803 break;
805 case ONPLAY_RELOAD_DIR:
806 reload_dir = true;
807 break;
809 case ONPLAY_START_PLAY:
810 return GO_TO_WPS;
811 break;
813 break;
816 case ACTION_NONE:
817 if (thumbnail_time != -1 &&
818 TIME_AFTER(current_tick, thumbnail_time))
819 { /* a delayed hovering thumbnail is due now */
820 int res;
821 int attr;
822 char* name;
824 #ifdef HAVE_TAGCACHE
825 if (id3db)
827 attr = tagtree_get_attr(&tc);
828 name = tagtree_get_entry(&tc, lasti)->name;
830 else
831 #endif
833 attr = dircache[lasti].attr;
834 name = dircache[lasti].name;
837 if (attr & ATTR_DIRECTORY)
839 DEBUGF("Playing directory thumbnail: %s", currdir);
840 res = ft_play_dirname(name);
841 if (res < 0) /* failed, not existing */
842 { /* say the number instead, as a fallback */
843 talk_id(VOICE_DIR, false);
844 talk_number(lasti+1, true);
847 else
849 DEBUGF("Playing file thumbnail: %s/%s%s\n",
850 currdir, name,
851 file_thumbnail_ext);
852 /* no fallback necessary, we knew in advance
853 that the file exists */
854 ft_play_filename(currdir, name);
856 thumbnail_time = -1; /* job done */
858 gui_syncstatusbar_draw(&statusbars, false);
859 break;
861 #ifdef HAVE_HOTSWAP
862 case SYS_FS_CHANGED:
863 #ifdef HAVE_TAGCACHE
864 if (!id3db)
865 #endif
866 reload_dir = true;
867 /* The 'dir no longer valid' situation will be caught later
868 * by checking the showdir() result. */
869 break;
870 #endif
872 default:
873 if (default_event_handler(button) == SYS_USB_CONNECTED)
875 if(*tc.dirfilter > NUM_FILTER_MODES)
876 /* leave sub-browsers after usb, doing otherwise
877 might be confusing to the user */
878 exit_func = true;
879 else
880 reload_dir = true;
882 break;
884 if (start_wps)
885 return GO_TO_WPS;
886 if ( button )
888 ata_spin();
892 check_rescan:
893 /* do we need to rescan dir? */
894 if (reload_dir || reload_root ||
895 lastfilter != *tc.dirfilter ||
896 lastsortcase != global_settings.sort_case)
898 if ( reload_root ) {
899 strcpy(currdir, "/");
900 tc.dirlevel = 0;
901 #ifdef HAVE_TAGCACHE
902 tc.currtable = 0;
903 tc.currextra = 0;
904 lasttable = -1;
905 lastextra = -1;
906 #endif
907 reload_root = false;
910 if (! reload_dir )
912 gui_synclist_select_item(&tree_lists, 0);
913 gui_synclist_draw(&tree_lists);
914 tc.selected_item = 0;
915 lastdir[0] = 0;
918 lastfilter = *tc.dirfilter;
919 lastsortcase = global_settings.sort_case;
920 restore = true;
923 if (exit_func)
924 return GO_TO_PREVIOUS;
926 if (restore || reload_dir) {
927 /* restore display */
928 numentries = update_dir();
929 if (currdir[1] && (numentries < 0))
930 { /* not in root and reload failed */
931 reload_root = true; /* try root */
932 reload_dir = false;
933 goto check_rescan;
935 need_update = true;
936 reload_dir = false;
939 if(need_update) {
940 need_update=false;
941 if ( numentries > 0 ) {
942 /* Voice the file if changed */
943 if(lasti != tc.selected_item || restore) {
944 int attr;
945 char* name;
947 lasti = tc.selected_item;
948 thumbnail_time = -1; /* Cancel whatever we were
949 about to say */
951 #ifdef HAVE_TAGCACHE
952 if (id3db)
954 attr = tagtree_get_attr(&tc);
955 name = tagtree_get_entry(&tc, tc.selected_item)->name;
957 else
958 #endif
960 attr = dircache[tc.selected_item].attr;
961 name = dircache[tc.selected_item].name;
964 /* Directory? */
965 if (attr & ATTR_DIRECTORY)
967 /* play directory thumbnail */
968 switch (global_settings.talk_dir) {
969 case 1: /* dirs as numbers */
970 talk_id(VOICE_DIR, false);
971 talk_number(tc.selected_item+1, true);
972 break;
974 case 2: /* dirs spelled */
975 talk_spell(name, false);
976 break;
978 case 3: /* thumbnail clip */
979 /* "schedule" a thumbnail, to have a little
980 delay */
981 thumbnail_time = current_tick + HOVER_DELAY;
982 break;
984 default:
985 break;
988 else /* file */
990 switch (global_settings.talk_file) {
991 case 1: /* files as numbers */
992 ft_play_filenumber(
993 tc.selected_item-tc.dirsindir+1,
994 attr & TREE_ATTR_MASK);
995 break;
997 case 2: /* files spelled */
998 talk_spell(name, false);
999 break;
1001 case 3: /* thumbnail clip */
1002 /* "schedule" a thumbnail, to have a little
1003 delay */
1004 if (attr & TREE_ATTR_THUMBNAIL)
1005 thumbnail_time = current_tick + HOVER_DELAY;
1006 else
1007 /* spell the number as fallback */
1008 talk_spell(name, false);
1009 break;
1011 default:
1012 break;
1019 action_signalscreenchange();
1020 return true;
1023 static int plsize = 0;
1024 static long pltick;
1025 static bool add_dir(char* dirname, int len, int fd)
1027 bool abort = false;
1028 DIRCACHED* dir;
1030 /* check for user abort */
1031 if (action_userabort(TIMEOUT_NOBLOCK))
1032 return true;
1034 dir = opendir_cached(dirname);
1035 if(!dir)
1036 return true;
1038 while (true) {
1039 struct dircache_entry *entry;
1041 entry = readdir_cached(dir);
1042 if (!entry)
1043 break;
1044 if (entry->attribute & ATTR_DIRECTORY) {
1045 int dirlen = strlen(dirname);
1046 bool result;
1048 if (!strcmp((char *)entry->d_name, ".") ||
1049 !strcmp((char *)entry->d_name, ".."))
1050 continue;
1052 if (dirname[1])
1053 snprintf(dirname+dirlen, len-dirlen, "/%s", entry->d_name);
1054 else
1055 snprintf(dirname, len, "/%s", entry->d_name);
1057 result = add_dir(dirname, len, fd);
1058 dirname[dirlen] = '\0';
1059 if (result) {
1060 abort = true;
1061 break;
1064 else {
1065 int x = strlen((char *)entry->d_name);
1066 unsigned int i;
1067 char *cp = strrchr((char *)entry->d_name,'.');
1069 if (cp) {
1070 cp++;
1072 /* add all supported audio files to playlists */
1073 for (i=0; i < sizeof(filetypes)/sizeof(struct filetype); i++) {
1074 if (filetypes[i].tree_attr == TREE_ATTR_MPA) {
1075 if (!strcasecmp(cp, filetypes[i].extension)) {
1076 char buf[8];
1077 int i;
1078 write(fd, dirname, strlen(dirname));
1079 write(fd, "/", 1);
1080 write(fd, entry->d_name, x);
1081 write(fd, "\n", 1);
1083 plsize++;
1084 if(TIME_AFTER(current_tick, pltick+HZ/4)) {
1085 pltick = current_tick;
1087 snprintf(buf, sizeof buf, "%d", plsize);
1088 #ifdef HAVE_LCD_BITMAP
1089 FOR_NB_SCREENS(i)
1091 screens[i].puts(0, 4, (unsigned char *)buf);
1092 gui_textarea_update(&screens[i]);
1094 #else
1095 x = 10;
1096 if (plsize > 999)
1097 x=7;
1098 else {
1099 if (plsize > 99)
1100 x=8;
1101 else {
1102 if (plsize > 9)
1103 x=9;
1106 FOR_NB_SCREENS(i) {
1107 screens[i].puts(x,0,buf);
1109 #endif
1111 break;
1118 closedir_cached(dir);
1120 return abort;
1123 bool create_playlist(void)
1125 int fd;
1126 int i;
1127 char filename[MAX_PATH];
1129 pltick = current_tick;
1131 snprintf(filename, sizeof filename, "%s.m3u8",
1132 tc.currdir[1] ? tc.currdir : "/root");
1133 FOR_NB_SCREENS(i)
1135 gui_textarea_clear(&screens[i]);
1136 screens[i].puts(0, 0, str(LANG_CREATING));
1137 screens[i].puts_scroll(0, 1, (unsigned char *)filename);
1138 #if defined(HAVE_LCD_BITMAP) || defined(SIMULATOR)
1139 gui_textarea_update(&screens[i]);
1140 #endif
1142 fd = creat(filename);
1143 if (fd < 0)
1144 return false;
1146 trigger_cpu_boost();
1148 snprintf(filename, sizeof(filename), "%s",
1149 tc.currdir[1] ? tc.currdir : "/");
1150 plsize = 0;
1151 add_dir(filename, sizeof(filename), fd);
1152 close(fd);
1154 sleep(HZ);
1156 return true;
1159 int rockbox_browse(const char *root, int dirfilter)
1161 int ret_val = 0;
1162 int *last_filter = tc.dirfilter;
1163 tc.dirfilter = &dirfilter;
1165 reload_dir = true;
1166 if (dirfilter >= NUM_FILTER_MODES)
1168 static struct tree_context backup;
1169 int last_context;
1171 backup = tc;
1172 tc.selected_item = 0;
1173 tc.dirlevel = 0;
1174 memcpy(tc.currdir, root, sizeof(tc.currdir));
1175 start_wps = false;
1176 last_context = curr_context;
1178 ret_val = dirbrowse();
1179 tc = backup;
1180 curr_context = last_context;
1182 else
1184 static char buf[MAX_PATH];
1185 if (dirfilter != SHOW_ID3DB)
1186 tc.dirfilter = &global_settings.dirfilter;
1187 strcpy(buf,root);
1188 set_current_file(buf);
1189 ret_val = dirbrowse();
1191 tc.dirfilter = last_filter;
1192 return ret_val;
1195 void tree_init(void)
1197 /* We copy the settings value in case it is changed by the user. We can't
1198 use it until the next reboot. */
1199 max_files = global_settings.max_files_in_dir;
1201 /* initialize tree context struct */
1202 memset(&tc, 0, sizeof(tc));
1203 tc.dirfilter = &global_settings.dirfilter;
1205 tc.name_buffer_size = AVERAGE_FILENAME_LENGTH * max_files;
1206 tc.name_buffer = buffer_alloc(tc.name_buffer_size);
1208 tc.dircache_size = max_files * sizeof(struct entry);
1209 tc.dircache = buffer_alloc(tc.dircache_size);
1212 void bookmark_play(char *resume_file, int index, int offset, int seed,
1213 char *filename)
1215 int i;
1216 char* suffix = strrchr(resume_file, '.');
1218 if (suffix != NULL &&
1219 (!strcasecmp(suffix, ".m3u") || !strcasecmp(suffix, ".m3u8")))
1221 /* Playlist playback */
1222 char* slash;
1223 /* check that the file exists */
1224 int fd = open(resume_file, O_RDONLY);
1225 if(fd<0)
1226 return;
1227 close(fd);
1229 slash = strrchr(resume_file,'/');
1230 if (slash)
1232 char* cp;
1233 *slash=0;
1235 cp=resume_file;
1236 if (!cp[0])
1237 cp="/";
1239 if (playlist_create(cp, slash+1) != -1)
1241 if (global_settings.playlist_shuffle)
1242 playlist_shuffle(seed, -1);
1243 playlist_start(index,offset);
1245 *slash='/';
1248 else
1250 /* Directory playback */
1251 lastdir[0]='\0';
1252 if (playlist_create(resume_file, NULL) != -1)
1254 resume_directory(resume_file);
1255 if (global_settings.playlist_shuffle)
1256 playlist_shuffle(seed, -1);
1258 /* Check if the file is at the same spot in the directory,
1259 else search for it */
1260 if ((strcmp(strrchr(playlist_peek(index),'/') + 1,
1261 filename)))
1263 for ( i=0; i < playlist_amount(); i++ )
1265 if ((strcmp(strrchr(playlist_peek(i),'/') + 1,
1266 filename)) == 0)
1267 break;
1269 if (i < playlist_amount())
1270 index = i;
1271 else
1272 return;
1274 playlist_start(index,offset);
1278 start_wps=true;
1281 static int ft_play_filenumber(int pos, int attr)
1283 /* try to find a voice ID for the extension, if known */
1284 unsigned int j;
1285 int ext_id = -1; /* default to none */
1286 for (j=0; j<sizeof(filetypes)/sizeof(*filetypes); j++)
1288 if (attr == filetypes[j].tree_attr)
1290 ext_id = filetypes[j].voiceclip;
1291 break;
1295 talk_id(VOICE_FILE, false);
1296 talk_number(pos, true);
1297 talk_id(ext_id, true);
1298 return 1;
1301 static int ft_play_dirname(char* name)
1303 int fd;
1304 char dirname_mp3_filename[MAX_PATH+1];
1306 #if CONFIG_CODEC != SWCODEC
1307 if (audio_status() & AUDIO_STATUS_PLAY)
1308 return 0;
1309 #endif
1311 snprintf(dirname_mp3_filename, sizeof(dirname_mp3_filename), "%s/%s/%s",
1312 tc.currdir[1] ? tc.currdir : "" , name,
1313 dir_thumbnail_name);
1315 DEBUGF("Checking for %s\n", dirname_mp3_filename);
1317 fd = open(dirname_mp3_filename, O_RDONLY);
1318 if (fd < 0)
1320 DEBUGF("Failed to find: %s\n", dirname_mp3_filename);
1321 return -1;
1324 close(fd);
1326 DEBUGF("Found: %s\n", dirname_mp3_filename);
1328 talk_file(dirname_mp3_filename, false);
1329 return 1;
1332 static void ft_play_filename(char *dir, char *file)
1334 char name_mp3_filename[MAX_PATH+1];
1336 #if CONFIG_CODEC != SWCODEC
1337 if (audio_status() & AUDIO_STATUS_PLAY)
1338 return;
1339 #endif
1341 if (strcasecmp(&file[strlen(file) - strlen(file_thumbnail_ext)],
1342 file_thumbnail_ext))
1343 { /* file has no .talk extension */
1344 snprintf(name_mp3_filename, sizeof(name_mp3_filename),
1345 "%s/%s%s", dir, file, file_thumbnail_ext);
1347 talk_file(name_mp3_filename, false);
1349 else
1350 { /* it already is a .talk file, play this directly */
1351 snprintf(name_mp3_filename, sizeof(name_mp3_filename),
1352 "%s/%s", dir, file);
1353 talk_id(LANG_VOICE_DIR_HOVER, false); /* prefix it */
1354 talk_file(name_mp3_filename, true);
1358 /* These two functions are called by the USB and shutdown handlers */
1359 void tree_flush(void)
1361 scrobbler_shutdown();
1362 #ifdef HAVE_TAGCACHE
1363 tagcache_shutdown();
1364 #endif
1365 playlist_shutdown();
1367 #ifdef HAVE_TC_RAMCACHE
1368 tagcache_unload_ramcache();
1369 #endif
1371 #ifdef HAVE_DIRCACHE
1373 int old_val = global_status.dircache_size;
1374 if (global_settings.dircache)
1376 global_status.dircache_size = dircache_get_cache_size();
1377 # ifdef HAVE_EEPROM_SETTINGS
1378 dircache_save();
1379 # endif
1380 dircache_disable();
1382 else
1384 global_status.dircache_size = 0;
1386 if (old_val != global_status.dircache_size)
1387 status_save();
1389 #endif
1392 void tree_restore(void)
1394 #ifdef HAVE_EEPROM_SETTINGS
1395 firmware_settings.disk_clean = false;
1396 #endif
1398 #ifdef HAVE_TC_RAMCACHE
1399 remove(TAGCACHE_STATEFILE);
1400 #endif
1402 #ifdef HAVE_DIRCACHE
1403 remove(DIRCACHE_FILE);
1404 if (global_settings.dircache)
1406 /* Print "Scanning disk..." to the display. */
1407 int i;
1408 FOR_NB_SCREENS(i)
1410 screens[i].putsxy((LCD_WIDTH/2) -
1411 ((strlen(str(LANG_DIRCACHE_BUILDING)) *
1412 screens[i].char_width)/2),
1413 LCD_HEIGHT-screens[i].char_height*3,
1414 str(LANG_DIRCACHE_BUILDING));
1415 gui_textarea_update(&screens[i]);
1418 dircache_build(global_status.dircache_size);
1420 /* Clean the text when we are done. */
1421 FOR_NB_SCREENS(i)
1423 gui_textarea_clear(&screens[i]);
1426 #endif
1427 #ifdef HAVE_TAGCACHE
1428 tagcache_start_scan();
1429 #endif
1430 scrobbler_init();