Update several codec Makefiles so that the codec libs build again on Coldfire targets...
[Rockbox.git] / apps / tree.c
blob772e2351bbd591c3d69a9da407c8efd0e2ce4091
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 #include "recorder/recording.h"
62 #include "rtc.h"
63 #include "dircache.h"
64 #ifdef HAVE_TAGCACHE
65 #include "tagcache.h"
66 #endif
67 #include "yesno.h"
68 #include "gwps-common.h"
69 #include "eeprom_settings.h"
70 #include "scrobbler.h"
72 /* gui api */
73 #include "list.h"
74 #include "statusbar.h"
75 #include "splash.h"
76 #include "buttonbar.h"
77 #include "textarea.h"
78 #include "action.h"
80 #ifdef HAVE_LCD_BITMAP
81 #include "widgets.h"
82 #endif
84 #ifdef HAVE_LCD_COLOR
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 { "mp4", TREE_ATTR_MPA, Icon_Audio, VOICE_EXT_MPA },
106 { "shn", TREE_ATTR_MPA, Icon_Audio, VOICE_EXT_MPA },
107 { "aif", TREE_ATTR_MPA, Icon_Audio, VOICE_EXT_MPA },
108 { "aiff",TREE_ATTR_MPA, Icon_Audio, VOICE_EXT_MPA },
109 { "sid", TREE_ATTR_MPA, Icon_Audio, VOICE_EXT_MPA },
110 { "adx", TREE_ATTR_MPA, Icon_Audio, VOICE_EXT_MPA },
111 #endif
112 { "m3u", TREE_ATTR_M3U, Icon_Playlist, LANG_PLAYLIST },
113 { "m3u8", TREE_ATTR_M3U, Icon_Playlist, LANG_PLAYLIST },
114 { "cfg", TREE_ATTR_CFG, Icon_Config, VOICE_EXT_CFG },
115 { "wps", TREE_ATTR_WPS, Icon_Wps, VOICE_EXT_WPS },
116 #ifdef HAVE_REMOTE_LCD
117 { "rwps", TREE_ATTR_RWPS, Icon_Wps, VOICE_EXT_RWPS },
118 #endif
119 #ifdef HAVE_LCD_COLOR
120 { "bmp", TREE_ATTR_BMP, Icon_Wps, VOICE_EXT_WPS },
121 #endif
122 #ifdef CONFIG_TUNER
123 { "fmr", TREE_ATTR_FMR, Icon_Preset, LANG_FMR },
124 #endif
125 { "lng", TREE_ATTR_LNG, Icon_Language, LANG_LANGUAGE },
126 { "rock",TREE_ATTR_ROCK,Icon_Plugin, VOICE_EXT_ROCK },
127 #ifdef HAVE_LCD_BITMAP
128 { "fnt", TREE_ATTR_FONT,Icon_Font, VOICE_EXT_FONT },
129 { "kbd", TREE_ATTR_KBD, Icon_Keyboard, VOICE_EXT_KBD },
130 #endif
131 { "bmark",TREE_ATTR_BMARK, Icon_Bookmark, VOICE_EXT_BMARK },
132 #ifdef BOOTFILE_EXT
133 { BOOTFILE_EXT, TREE_ATTR_MOD, Icon_Firmware, VOICE_EXT_AJZ },
134 #endif /* #ifndef SIMULATOR */
137 struct gui_synclist tree_lists;
139 /* I put it here because other files doesn't use it yet,
140 * but should be elsewhere since it will be used mostly everywhere */
141 #ifdef HAS_BUTTONBAR
142 struct gui_buttonbar tree_buttonbar;
143 #endif
144 static struct tree_context tc;
146 bool boot_changed = false;
148 char lastfile[MAX_PATH];
149 static char lastdir[MAX_PATH];
150 static int lasttable, lastextra, lastfirstpos;
151 static int max_files = 0;
153 static bool reload_dir = false;
155 static bool start_wps = false;
156 static bool dirbrowse(void);
157 static int curr_context = false;/* id3db or tree*/
160 * removes the extension of filename (if it doesn't start with a .)
161 * puts the result in buffer
163 char * strip_extension(char * filename, char * buffer)
165 int dotpos;
166 char * dot=strrchr(filename, '.');
167 if(dot!=0 && filename[0]!='.')
169 dotpos = dot-filename;
170 strncpy(buffer, filename, dotpos);
171 buffer[dotpos]='\0';
172 return(buffer);
174 else
175 return(filename);
177 char * tree_get_filename(int selected_item, void * data, char *buffer)
179 struct tree_context * local_tc=(struct tree_context *)data;
180 char *name;
181 int attr=0;
182 #ifdef HAVE_TAGCACHE
183 bool id3db = *(local_tc->dirfilter) == SHOW_ID3DB;
185 if (id3db)
187 return tagtree_get_entry(&tc, selected_item)->name;
189 else
190 #endif
192 struct entry* dc = local_tc->dircache;
193 struct entry* e = &dc[selected_item];
194 name = e->name;
195 attr = e->attr;
197 /* if any file filter is on, and if it's not a directory,
198 * strip the extension */
200 if ( (*(local_tc->dirfilter) != SHOW_ID3DB) && !(attr & ATTR_DIRECTORY)
201 && (*(local_tc->dirfilter) != SHOW_ALL) )
203 return(strip_extension(name, buffer));
205 return(name);
209 void tree_get_fileicon(int selected_item, void * data, ICON * icon)
211 struct tree_context * local_tc=(struct tree_context *)data;
212 #ifdef HAVE_TAGCACHE
213 bool id3db = *(local_tc->dirfilter) == SHOW_ID3DB;
214 if (id3db) {
215 *icon = (ICON)tagtree_get_icon(&tc);
217 else
218 #endif
220 struct entry* dc = local_tc->dircache;
221 struct entry* e = &dc[selected_item];
222 *icon = (ICON)filetype_get_icon(e->attr);
226 bool check_rockboxdir(void)
228 DIR *dir = opendir(ROCKBOX_DIR);
229 if(!dir)
230 { /* No need to localise this message.
231 If .rockbox is missing, it wouldn't work anyway */
232 int i;
233 FOR_NB_SCREENS(i)
234 screens[i].clear_display();
235 gui_syncsplash(HZ*2, true, "No .rockbox directory");
236 FOR_NB_SCREENS(i)
237 screens[i].clear_display();
238 gui_syncsplash(HZ*2, true, "Installation incomplete");
239 return false;
241 closedir(dir);
242 return true;
245 void browse_root(void)
247 gui_sync_wps_screen_init();
249 filetype_init();
250 check_rockboxdir();
252 strcpy(tc.currdir, "/");
254 #ifdef HAVE_LCD_CHARCELLS
255 int i;
256 FOR_NB_SCREENS(i)
257 screens[i].double_height(false);
258 #endif
259 #ifdef HAS_BUTTONBAR
260 gui_buttonbar_init(&tree_buttonbar);
261 /* since archos only have one screen, no need to create more than that */
262 gui_buttonbar_set_display(&tree_buttonbar, &(screens[SCREEN_MAIN]) );
263 #endif
264 gui_synclist_init(&tree_lists, &tree_get_filename, &tc, false, 1);
265 gui_synclist_set_icon_callback(&tree_lists,
266 global_settings.show_icons?&tree_get_fileicon:NULL);
267 #ifndef SIMULATOR
268 dirbrowse();
269 #else
270 if (!dirbrowse()) {
271 DEBUGF("No filesystem found. Have you forgotten to create it?\n");
273 #endif
276 void tree_get_filetypes(const struct filetype** types, int* count)
278 *types = filetypes;
279 *count = sizeof(filetypes) / sizeof(*filetypes);
282 struct tree_context* tree_get_context(void)
284 return &tc;
287 /* talkbox hovering delay, to avoid immediate disk activity */
288 #define HOVER_DELAY (HZ/2)
290 * Returns the position of a given file in the current directory
291 * returns -1 if not found
293 int tree_get_file_position(char * filename)
295 int i;
297 /* use lastfile to determine the selected item (default=0) */
298 for (i=0; i < tc.filesindir; i++)
300 struct entry* dc = tc.dircache;
301 struct entry* e = &dc[i];
302 if (!strcasecmp(e->name, filename))
303 return(i);
305 return(-1);/* no file can match, returns undefined */
309 * Called when a new dir is loaded (for example when returning from other apps ...)
310 * also completely redraws the tree
312 static int update_dir(void)
314 bool changed = false;
315 #ifdef HAVE_TAGCACHE
316 bool id3db = *tc.dirfilter == SHOW_ID3DB;
317 /* Checks for changes */
318 if (id3db) {
319 if (tc.currtable != lasttable ||
320 tc.currextra != lastextra ||
321 tc.firstpos != lastfirstpos ||
322 reload_dir)
324 if (tagtree_load(&tc) < 0)
325 return -1;
327 lasttable = tc.currtable;
328 lastextra = tc.currextra;
329 lastfirstpos = tc.firstpos;
330 changed = true;
333 else
334 #endif
336 /* if the tc.currdir has been changed, reload it ...*/
337 if (strncmp(tc.currdir, lastdir, sizeof(lastdir)) || reload_dir) {
339 if (ft_load(&tc, NULL) < 0)
340 return -1;
341 strcpy(lastdir, tc.currdir);
342 changed = true;
345 /* if selected item is undefined */
346 if (tc.selected_item == -1)
348 /* use lastfile to determine the selected item */
349 tc.selected_item = tree_get_file_position(lastfile);
351 /* If the file doesn't exists, select the first one (default) */
352 if(tc.selected_item < 0)
353 tc.selected_item = 0;
354 changed = true;
356 if (changed)
359 #ifdef HAVE_TAGCACHE
360 !id3db &&
361 #endif
362 (tc.dirfull ||
363 tc.filesindir == global_settings.max_files_in_dir) )
365 gui_syncsplash(HZ, true, str(LANG_SHOWDIR_BUFFER_FULL));
368 #ifdef HAVE_TAGCACHE
369 if (id3db)
371 if (global_settings.show_path_in_browser == SHOW_PATH_FULL
372 || global_settings.show_path_in_browser == SHOW_PATH_CURRENT)
374 gui_synclist_set_title(&tree_lists, tagtree_get_title(&tc),
375 filetype_get_icon(ATTR_DIRECTORY));
377 else
379 /* Must clear the title as the list is reused */
380 gui_synclist_set_title(&tree_lists, NULL, NOICON);
383 else
384 #endif
386 if (global_settings.show_path_in_browser == SHOW_PATH_FULL)
388 gui_synclist_set_title(&tree_lists, tc.currdir,
389 filetype_get_icon(ATTR_DIRECTORY));
391 else if (global_settings.show_path_in_browser == SHOW_PATH_CURRENT)
393 char *title = strrchr(tc.currdir, '/') + 1;
394 if (*title == '\0')
396 /* Display nothing for the root dir */
397 gui_synclist_set_title(&tree_lists, NULL, NOICON);
399 else
400 gui_synclist_set_title(&tree_lists, title,
401 filetype_get_icon(ATTR_DIRECTORY));
403 else
405 /* Must clear the title as the list is reused */
406 gui_synclist_set_title(&tree_lists, NULL, NOICON);
410 gui_synclist_set_nb_items(&tree_lists, tc.filesindir);
411 gui_synclist_set_icon_callback(&tree_lists,
412 global_settings.show_icons?&tree_get_fileicon:NULL);
413 if( tc.selected_item >= tc.filesindir)
414 tc.selected_item=tc.filesindir-1;
416 gui_synclist_select_item(&tree_lists, tc.selected_item);
417 #ifdef HAS_BUTTONBAR
418 if (global_settings.buttonbar) {
419 if (*tc.dirfilter < NUM_FILTER_MODES)
420 gui_buttonbar_set(&tree_buttonbar, str(LANG_SYSFONT_DIRBROWSE_F1),
421 str(LANG_SYSFONT_DIRBROWSE_F2),
422 str(LANG_SYSFONT_DIRBROWSE_F3));
423 else
424 gui_buttonbar_set(&tree_buttonbar, "<<<", "", "");
425 gui_buttonbar_draw(&tree_buttonbar);
427 #endif
428 gui_synclist_draw(&tree_lists);
429 gui_syncstatusbar_draw(&statusbars, true);
430 return tc.filesindir;
433 /* load tracks from specified directory to resume play */
434 void resume_directory(const char *dir)
436 #ifdef HAVE_TAGCACHE
437 bool id3db = *tc.dirfilter == SHOW_ID3DB;
438 #endif
440 if (ft_load(&tc, dir) < 0)
441 return;
442 lastdir[0] = 0;
444 ft_build_playlist(&tc, 0);
446 #ifdef HAVE_TAGCACHE
447 if (id3db)
448 tagtree_load(&tc);
449 #endif
452 /* Returns the current working directory and also writes cwd to buf if
453 non-NULL. In case of error, returns NULL. */
454 char *getcwd(char *buf, int size)
456 if (!buf)
457 return tc.currdir;
458 else if (size > 0)
460 strncpy(buf, tc.currdir, size);
461 return buf;
463 else
464 return NULL;
467 /* Force a reload of the directory next time directory browser is called */
468 void reload_directory(void)
470 reload_dir = true;
473 static void start_resume(bool just_powered_on)
475 bool do_resume = false;
476 if ( global_settings.resume_index != -1 ) {
477 DEBUGF("Resume index %X offset %X\n",
478 global_settings.resume_index,
479 global_settings.resume_offset);
481 #ifdef HAVE_ALARM_MOD
482 if ( rtc_check_alarm_started(true) ) {
483 rtc_enable_alarm(false);
484 do_resume = true;
486 #endif
488 /* always resume? */
489 if ( global_settings.resume || ! just_powered_on)
490 #ifdef HAVE_HEADPHONE_DETECTION
492 if ( just_powered_on )
494 if ( !global_settings.unplug_autoresume
495 || headphones_inserted() )
496 do_resume = true;
498 else
499 do_resume = true;
501 #else
502 do_resume = true;
503 #endif
505 if (! do_resume) return;
507 if (playlist_resume() != -1)
509 playlist_start(global_settings.resume_index,
510 global_settings.resume_offset);
512 start_wps = true;
514 else return;
516 else if (! just_powered_on) {
517 gui_syncsplash(HZ*2, true, str(LANG_NOTHING_TO_RESUME));
521 /* Selects a file and update tree context properly */
522 void set_current_file(char *path)
524 char *name;
525 int i;
527 #ifdef HAVE_TAGCACHE
528 /* in ID3DB mode it is a bad idea to call this function */
529 /* (only happens with `follow playlist') */
530 if( *tc.dirfilter == SHOW_ID3DB )
531 return;
532 #endif
534 /* separate directory from filename */
535 /* gets the directory's name and put it into tc.currdir */
536 name = strrchr(path+1,'/');
537 if (name)
539 *name = 0;
540 strcpy(tc.currdir, path);
541 *name = '/';
542 name++;
544 else
546 strcpy(tc.currdir, "/");
547 name = path+1;
550 strcpy(lastfile, name);
552 /* undefined item selected */
553 tc.selected_item = -1;
555 /* If we changed dir we must recalculate the dirlevel
556 and adjust the selected history properly */
557 if (strncmp(tc.currdir,lastdir,sizeof(lastdir)))
559 tc.dirlevel = 0;
560 tc.selected_item_history[tc.dirlevel] = -1;
562 /* use '/' to calculate dirlevel */
563 /* FIXME : strlen(path) : crazy oO better to store it at
564 the beginning */
565 int path_len = strlen(path) + 1;
566 for (i = 1; i < path_len; i++)
568 if (path[i] == '/')
570 tc.dirlevel++;
571 tc.selected_item_history[tc.dirlevel] = -1;
577 #ifdef HAVE_TAGCACHE
578 static bool check_changed_id3mode(bool currmode)
580 if (currmode != (global_settings.dirfilter == SHOW_ID3DB)) {
581 currmode = global_settings.dirfilter == SHOW_ID3DB;
582 if (currmode) {
583 curr_context=CONTEXT_ID3DB;
584 tc.dirlevel = 0;
585 tagtree_load(&tc);
587 else
589 tc.dirlevel = 0;
590 curr_context=CONTEXT_TREE;
591 ft_load(&tc, NULL);
592 reload_dir = true;
595 return currmode;
597 #endif
599 /* main loop, handles key events */
600 static bool dirbrowse(void)
602 int numentries=0;
603 char buf[MAX_PATH];
604 int lasti = -1;
605 unsigned button, returned_button;
606 bool reload_root = false;
607 int lastfilter = *tc.dirfilter;
608 bool lastsortcase = global_settings.sort_case;
609 bool need_update = true;
610 bool exit_func = false;
611 long thumbnail_time = -1; /* for delaying a thumbnail */
613 char* currdir = tc.currdir; /* just a shortcut */
614 #ifdef HAVE_TAGCACHE
615 bool id3db = *tc.dirfilter == SHOW_ID3DB;
617 if (id3db)
618 curr_context=CONTEXT_ID3DB;
619 else
620 #endif
621 curr_context=CONTEXT_TREE;
622 tc.selected_item = 0;
623 tc.dirlevel=0;
624 tc.firstpos=0;
625 lasttable = -1;
626 lastextra = -1;
627 lastfirstpos = 0;
629 if (*tc.dirfilter < NUM_FILTER_MODES) {
630 #ifdef HAVE_RECORDING
631 #ifndef SIMULATOR
632 if (global_settings.rec_startup) {
633 /* We fake being in the menu structure by calling
634 the appropriate parent when we drop out of each screen */
635 #if CONFIG_CODEC == SWCODEC
636 /* Put in a 1 sec pause to slow bootup or the recording codecs
637 won't initialize */
638 sleep(HZ);
639 #endif
640 recording_screen(false);
641 rec_menu();
642 main_menu();
644 else
645 #endif
646 #endif
647 start_resume(true);
650 /* If we don't need to show the wps, draw the dir */
651 if (!start_wps) {
652 numentries = update_dir();
653 if (numentries == -1)
654 return false; /* currdir is not a directory */
656 if (*tc.dirfilter > NUM_FILTER_MODES && numentries==0)
658 gui_syncsplash(HZ*2, true, str(LANG_NO_FILES));
659 return false; /* No files found for rockbox_browser() */
663 while(1) {
664 struct entry *dircache = tc.dircache;
665 bool restore = false;
666 #ifdef BOOTFILE
667 if (boot_changed) {
668 char *lines[]={str(LANG_BOOT_CHANGED), str(LANG_REBOOT_NOW)};
669 struct text_message message={lines, 2};
670 if(gui_syncyesno_run(&message, NULL, NULL)==YESNO_YES)
671 rolo_load("/" BOOTFILE);
672 restore = true;
673 boot_changed = false;
675 #endif
676 button = get_action(CONTEXT_TREE,HZ/5);
677 returned_button = gui_synclist_do_button(&tree_lists, button);
678 if (returned_button)
679 need_update = true;
680 if (returned_button == ACTION_STD_CANCEL)
681 button = ACTION_STD_CANCEL;
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 1: reload_dir = true; break;
696 case 2: start_wps = true; break;
697 case 3: 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 we are in /, nothing to do */
709 if (tc.dirlevel == 0 && !strcmp(currdir,"/"))
710 break;
712 #ifdef HAVE_TAGCACHE
713 if (id3db)
714 tagtree_exit(&tc);
715 else
716 #endif
717 if (ft_exit(&tc) == 3)
718 exit_func = true;
720 restore = true;
721 break;
723 case ACTION_TREE_STOP:
724 if (*tc.dirfilter < NUM_FILTER_MODES)
726 /* Stop the music if it is playing */
727 if(audio_status()) {
728 if (!global_settings.party_mode) {
729 if (global_settings.fade_on_stop)
730 fade(0);
731 bookmark_autobookmark();
732 audio_stop();
735 #if defined(CONFIG_CHARGING) && \
736 (CONFIG_KEYPAD == RECORDER_PAD) && !defined(HAVE_SW_POWEROFF)
737 else {
738 if (!charger_inserted()) {
739 if(shutdown_screen())
740 reload_dir = true;
741 } else {
742 charging_splash();
744 restore = true;
746 #endif
748 #if defined(CONFIG_CHARGING) && !defined(HAVE_POWEROFF_WHILE_CHARGING)
750 static int last_off = 0;
751 if (current_tick - last_off < 50) {
752 if (charger_inserted()) {
753 charging_splash();
754 restore = true;
757 last_off = current_tick;
759 #endif
760 break; /* case ACTION_TREE_STOP: */
761 case ACTION_STD_MENU:
762 /* don't enter menu from plugin browser */
763 if (*tc.dirfilter < NUM_FILTER_MODES)
765 int i;
766 FOR_NB_SCREENS(i)
767 screens[i].stop_scroll();
768 action_signalscreenchange();
769 if (main_menu())
770 reload_dir = true;
771 restore = true;
773 #ifdef HAVE_TAGCACHE
774 id3db = check_changed_id3mode(id3db);
775 if(id3db)
776 reload_dir = true;
777 #endif
779 else /* use it as a quick exit instead */
780 exit_func = true;
781 break;
783 case ACTION_TREE_WPS:
784 /* don't enter wps from plugin browser etc */
785 if (*tc.dirfilter < NUM_FILTER_MODES)
787 if (audio_status() & AUDIO_STATUS_PLAY)
789 start_wps=true;
791 else
793 start_resume(false);
794 restore = true;
797 break;
798 #ifdef HAVE_QUICKSCREEN
799 case ACTION_STD_QUICKSCREEN:
800 /* don't enter f2 from plugin browser */
801 if (*tc.dirfilter < NUM_FILTER_MODES)
803 if (quick_screen_quick(button))
804 reload_dir = true;
805 restore = true;
807 #ifdef HAVE_TAGCACHE
808 id3db = check_changed_id3mode(id3db);
809 reload_dir = true;
810 #endif
812 break;
813 #endif
814 #ifdef BUTTON_F3
815 case ACTION_F3:
816 /* don't enter f3 from plugin browser */
817 if (*tc.dirfilter < NUM_FILTER_MODES)
819 if (quick_screen_f3(BUTTON_F3))
820 reload_dir = true;
821 restore = true;
823 break;
824 #endif
826 case ACTION_STD_CONTEXT:
828 int onplay_result;
829 int attr = 0;
831 if(!numentries)
832 onplay_result = onplay(NULL, 0, curr_context);
833 else {
834 #ifdef HAVE_TAGCACHE
835 if (id3db)
837 if (tagtree_get_attr(&tc) == TREE_ATTR_MPA)
839 attr = TREE_ATTR_MPA;
840 tagtree_get_filename(&tc, buf, sizeof(buf));
842 else
843 attr = ATTR_DIRECTORY;
845 else
846 #endif
848 attr = dircache[tc.selected_item].attr;
850 if (currdir[1]) /* Not in / */
851 snprintf(buf, sizeof buf, "%s/%s",
852 currdir,
853 dircache[tc.selected_item].name);
854 else /* In / */
855 snprintf(buf, sizeof buf, "/%s",
856 dircache[tc.selected_item].name);
858 onplay_result = onplay(buf, attr, curr_context);
860 switch (onplay_result)
862 case ONPLAY_OK:
863 restore = true;
864 break;
866 case ONPLAY_RELOAD_DIR:
867 reload_dir = true;
868 break;
870 case ONPLAY_START_PLAY:
871 start_wps = true;
872 break;
874 break;
877 case ACTION_NONE:
878 if (thumbnail_time != -1 &&
879 TIME_AFTER(current_tick, thumbnail_time))
880 { /* a delayed hovering thumbnail is due now */
881 int res;
882 int attr;
883 char* name;
885 #ifdef HAVE_TAGCACHE
886 if (id3db)
888 attr = tagtree_get_attr(&tc);
889 name = tagtree_get_entry(&tc, lasti)->name;
891 else
892 #endif
894 attr = dircache[lasti].attr;
895 name = dircache[lasti].name;
898 if (attr & ATTR_DIRECTORY)
900 DEBUGF("Playing directory thumbnail: %s", currdir);
901 res = ft_play_dirname(name);
902 if (res < 0) /* failed, not existing */
903 { /* say the number instead, as a fallback */
904 talk_id(VOICE_DIR, false);
905 talk_number(lasti+1, true);
908 else
910 DEBUGF("Playing file thumbnail: %s/%s%s\n",
911 currdir, name,
912 file_thumbnail_ext);
913 /* no fallback necessary, we knew in advance
914 that the file exists */
915 ft_play_filename(currdir, name);
917 thumbnail_time = -1; /* job done */
919 gui_syncstatusbar_draw(&statusbars, false);
920 break;
922 #ifdef HAVE_HOTSWAP
923 case SYS_FS_CHANGED:
924 #ifdef HAVE_TAGCACHE
925 if (!id3db)
926 #endif
927 reload_dir = true;
928 /* The 'dir no longer valid' situation will be caught later
929 * by checking the showdir() result. */
930 break;
931 #endif
933 default:
934 if (default_event_handler(button) == SYS_USB_CONNECTED)
936 if(*tc.dirfilter > NUM_FILTER_MODES)
937 /* leave sub-browsers after usb, doing otherwise
938 might be confusing to the user */
939 exit_func = true;
940 else
941 reload_dir = true;
943 break;
946 if ( button )
948 ata_spin();
951 if (start_wps && audio_status() )
953 int i;
955 FOR_NB_SCREENS(i)
956 screens[i].stop_scroll();
958 if (gui_wps_show() == SYS_USB_CONNECTED)
959 reload_dir = true;
960 #ifdef HAVE_HOTSWAP
961 else
962 #ifdef HAVE_TAGCACHE
963 if (!id3db) /* Try reload to catch 'no longer valid' case. */
964 #endif
965 reload_dir = true;
966 #endif
967 #ifdef HAVE_LCD_COLOR
968 show_main_backdrop();
969 #endif
970 #ifdef HAVE_TAGCACHE
971 id3db = check_changed_id3mode(id3db);
972 #endif
973 restore = true;
974 start_wps=false;
977 check_rescan:
978 /* do we need to rescan dir? */
979 if (reload_dir || reload_root ||
980 lastfilter != *tc.dirfilter ||
981 lastsortcase != global_settings.sort_case)
983 if ( reload_root ) {
984 strcpy(currdir, "/");
985 tc.dirlevel = 0;
986 tc.currtable = 0;
987 tc.currextra = 0;
988 lasttable = -1;
989 lastextra = -1;
990 reload_root = false;
993 if (! reload_dir )
995 gui_synclist_select_item(&tree_lists, 0);
996 gui_synclist_draw(&tree_lists);
997 tc.selected_item = 0;
998 lastdir[0] = 0;
1001 lastfilter = *tc.dirfilter;
1002 lastsortcase = global_settings.sort_case;
1003 restore = true;
1006 if (exit_func)
1007 break;
1009 if (restore || reload_dir) {
1010 /* restore display */
1011 numentries = update_dir();
1012 if (currdir[1] && (numentries < 0))
1013 { /* not in root and reload failed */
1014 reload_root = true; /* try root */
1015 reload_dir = false;
1016 goto check_rescan;
1018 need_update = true;
1019 reload_dir = false;
1022 if(need_update) {
1023 tc.selected_item = gui_synclist_get_sel_pos(&tree_lists);
1024 need_update=false;
1025 if ( numentries > 0 ) {
1026 /* Voice the file if changed */
1027 if(lasti != tc.selected_item || restore) {
1028 int attr;
1029 char* name;
1031 lasti = tc.selected_item;
1032 thumbnail_time = -1; /* Cancel whatever we were
1033 about to say */
1035 #ifdef HAVE_TAGCACHE
1036 if (id3db)
1038 attr = tagtree_get_attr(&tc);
1039 name = tagtree_get_entry(&tc, tc.selected_item)->name;
1041 else
1042 #endif
1044 attr = dircache[tc.selected_item].attr;
1045 name = dircache[tc.selected_item].name;
1048 /* Directory? */
1049 if (attr & ATTR_DIRECTORY)
1051 /* play directory thumbnail */
1052 switch (global_settings.talk_dir) {
1053 case 1: /* dirs as numbers */
1054 talk_id(VOICE_DIR, false);
1055 talk_number(tc.selected_item+1, true);
1056 break;
1058 case 2: /* dirs spelled */
1059 talk_spell(name, false);
1060 break;
1062 case 3: /* thumbnail clip */
1063 /* "schedule" a thumbnail, to have a little
1064 delay */
1065 thumbnail_time = current_tick + HOVER_DELAY;
1066 break;
1068 default:
1069 break;
1072 else /* file */
1074 switch (global_settings.talk_file) {
1075 case 1: /* files as numbers */
1076 ft_play_filenumber(
1077 tc.selected_item-tc.dirsindir+1,
1078 attr & TREE_ATTR_MASK);
1079 break;
1081 case 2: /* files spelled */
1082 talk_spell(name, false);
1083 break;
1085 case 3: /* thumbnail clip */
1086 /* "schedule" a thumbnail, to have a little
1087 delay */
1088 if (attr & TREE_ATTR_THUMBNAIL)
1089 thumbnail_time = current_tick + HOVER_DELAY;
1090 else
1091 /* spell the number as fallback */
1092 talk_spell(name, false);
1093 break;
1095 default:
1096 break;
1103 action_signalscreenchange();
1104 return true;
1107 static int plsize = 0;
1108 static long pltick;
1109 static bool add_dir(char* dirname, int len, int fd)
1111 bool abort = false;
1112 DIRCACHED* dir;
1114 /* check for user abort */
1115 if (action_userabort(TIMEOUT_NOBLOCK))
1116 return true;
1118 dir = opendir_cached(dirname);
1119 if(!dir)
1120 return true;
1122 while (true) {
1123 struct dircache_entry *entry;
1125 entry = readdir_cached(dir);
1126 if (!entry)
1127 break;
1128 if (entry->attribute & ATTR_DIRECTORY) {
1129 int dirlen = strlen(dirname);
1130 bool result;
1132 if (!strcmp((char *)entry->d_name, ".") ||
1133 !strcmp((char *)entry->d_name, ".."))
1134 continue;
1136 if (dirname[1])
1137 snprintf(dirname+dirlen, len-dirlen, "/%s", entry->d_name);
1138 else
1139 snprintf(dirname, len, "/%s", entry->d_name);
1141 result = add_dir(dirname, len, fd);
1142 dirname[dirlen] = '\0';
1143 if (result) {
1144 abort = true;
1145 break;
1148 else {
1149 int x = strlen((char *)entry->d_name);
1150 unsigned int i;
1151 char *cp = strrchr((char *)entry->d_name,'.');
1153 if (cp) {
1154 cp++;
1156 /* add all supported audio files to playlists */
1157 for (i=0; i < sizeof(filetypes)/sizeof(struct filetype); i++) {
1158 if (filetypes[i].tree_attr == TREE_ATTR_MPA) {
1159 if (!strcasecmp(cp, filetypes[i].extension)) {
1160 char buf[8];
1161 int i;
1162 write(fd, dirname, strlen(dirname));
1163 write(fd, "/", 1);
1164 write(fd, entry->d_name, x);
1165 write(fd, "\n", 1);
1167 plsize++;
1168 if(TIME_AFTER(current_tick, pltick+HZ/4)) {
1169 pltick = current_tick;
1171 snprintf(buf, sizeof buf, "%d", plsize);
1172 #ifdef HAVE_LCD_BITMAP
1173 FOR_NB_SCREENS(i)
1175 screens[i].puts(0, 4, (unsigned char *)buf);
1176 gui_textarea_update(&screens[i]);
1178 #else
1179 x = 10;
1180 if (plsize > 999)
1181 x=7;
1182 else {
1183 if (plsize > 99)
1184 x=8;
1185 else {
1186 if (plsize > 9)
1187 x=9;
1190 FOR_NB_SCREENS(i) {
1191 screens[i].puts(x,0,buf);
1193 #endif
1195 break;
1202 closedir_cached(dir);
1204 return abort;
1207 bool create_playlist(void)
1209 int fd;
1210 int i;
1211 char filename[MAX_PATH];
1213 pltick = current_tick;
1215 snprintf(filename, sizeof filename, "%s.m3u",
1216 tc.currdir[1] ? tc.currdir : "/root");
1217 FOR_NB_SCREENS(i)
1219 gui_textarea_clear(&screens[i]);
1220 screens[i].puts(0, 0, str(LANG_CREATING));
1221 screens[i].puts_scroll(0, 1, (unsigned char *)filename);
1222 #if defined(HAVE_LCD_BITMAP) || defined(SIMULATOR)
1223 gui_textarea_update(&screens[i]);
1224 #endif
1226 fd = creat(filename, O_WRONLY);
1227 if (fd < 0)
1228 return false;
1230 cpu_boost_id(true, CPUBOOSTID_TREE);
1232 snprintf(filename, sizeof(filename), "%s",
1233 tc.currdir[1] ? tc.currdir : "/");
1234 plsize = 0;
1235 add_dir(filename, sizeof(filename), fd);
1236 close(fd);
1238 cpu_boost_id(false, CPUBOOSTID_TREE);
1240 sleep(HZ);
1242 return true;
1245 bool rockbox_browse(const char *root, int dirfilter)
1247 static struct tree_context backup;
1248 int last_context;
1250 backup = tc;
1251 reload_dir = true;
1252 memcpy(tc.currdir, root, sizeof(tc.currdir));
1253 start_wps = false;
1254 tc.dirfilter = &dirfilter;
1255 last_context = curr_context;
1257 dirbrowse();
1259 tc = backup;
1260 curr_context = last_context;
1262 return false;
1265 void tree_init(void)
1267 /* We copy the settings value in case it is changed by the user. We can't
1268 use it until the next reboot. */
1269 max_files = global_settings.max_files_in_dir;
1271 /* initialize tree context struct */
1272 memset(&tc, 0, sizeof(tc));
1273 tc.dirfilter = &global_settings.dirfilter;
1275 tc.name_buffer_size = AVERAGE_FILENAME_LENGTH * max_files;
1276 tc.name_buffer = buffer_alloc(tc.name_buffer_size);
1278 tc.dircache_size = max_files * sizeof(struct entry);
1279 tc.dircache = buffer_alloc(tc.dircache_size);
1282 void bookmark_play(char *resume_file, int index, int offset, int seed,
1283 char *filename)
1285 int i;
1286 char* suffix = strrchr(resume_file, '.');
1288 if (suffix != NULL &&
1289 (!strcasecmp(suffix, ".m3u") || !strcasecmp(suffix, ".m3u8")))
1291 /* Playlist playback */
1292 char* slash;
1293 /* check that the file exists */
1294 int fd = open(resume_file, O_RDONLY);
1295 if(fd<0)
1296 return;
1297 close(fd);
1299 slash = strrchr(resume_file,'/');
1300 if (slash)
1302 char* cp;
1303 *slash=0;
1305 cp=resume_file;
1306 if (!cp[0])
1307 cp="/";
1309 if (playlist_create(cp, slash+1) != -1)
1311 if (global_settings.playlist_shuffle)
1312 playlist_shuffle(seed, -1);
1313 playlist_start(index,offset);
1315 *slash='/';
1318 else
1320 /* Directory playback */
1321 lastdir[0]='\0';
1322 if (playlist_create(resume_file, NULL) != -1)
1324 resume_directory(resume_file);
1325 if (global_settings.playlist_shuffle)
1326 playlist_shuffle(seed, -1);
1328 /* Check if the file is at the same spot in the directory,
1329 else search for it */
1330 if ((strcmp(strrchr(playlist_peek(index),'/') + 1,
1331 filename)))
1333 for ( i=0; i < playlist_amount(); i++ )
1335 if ((strcmp(strrchr(playlist_peek(i),'/') + 1,
1336 filename)) == 0)
1337 break;
1339 if (i < playlist_amount())
1340 index = i;
1341 else
1342 return;
1344 playlist_start(index,offset);
1348 start_wps=true;
1351 int ft_play_filenumber(int pos, int attr)
1353 /* try to find a voice ID for the extension, if known */
1354 unsigned int j;
1355 int ext_id = -1; /* default to none */
1356 for (j=0; j<sizeof(filetypes)/sizeof(*filetypes); j++)
1358 if (attr == filetypes[j].tree_attr)
1360 ext_id = filetypes[j].voiceclip;
1361 break;
1365 talk_id(VOICE_FILE, false);
1366 talk_number(pos, true);
1367 talk_id(ext_id, true);
1368 return 1;
1371 int ft_play_dirname(char* name)
1373 int fd;
1374 char dirname_mp3_filename[MAX_PATH+1];
1376 #if CONFIG_CODEC != SWCODEC
1377 if (audio_status() & AUDIO_STATUS_PLAY)
1378 return 0;
1379 #endif
1381 snprintf(dirname_mp3_filename, sizeof(dirname_mp3_filename), "%s/%s/%s",
1382 tc.currdir[1] ? tc.currdir : "" , name,
1383 dir_thumbnail_name);
1385 DEBUGF("Checking for %s\n", dirname_mp3_filename);
1387 fd = open(dirname_mp3_filename, O_RDONLY);
1388 if (fd < 0)
1390 DEBUGF("Failed to find: %s\n", dirname_mp3_filename);
1391 return -1;
1394 close(fd);
1396 DEBUGF("Found: %s\n", dirname_mp3_filename);
1398 talk_file(dirname_mp3_filename, false);
1399 return 1;
1402 void ft_play_filename(char *dir, char *file)
1404 char name_mp3_filename[MAX_PATH+1];
1406 #if CONFIG_CODEC != SWCODEC
1407 if (audio_status() & AUDIO_STATUS_PLAY)
1408 return;
1409 #endif
1411 if (strcasecmp(&file[strlen(file) - strlen(file_thumbnail_ext)],
1412 file_thumbnail_ext))
1413 { /* file has no .talk extension */
1414 snprintf(name_mp3_filename, sizeof(name_mp3_filename),
1415 "%s/%s%s", dir, file, file_thumbnail_ext);
1417 talk_file(name_mp3_filename, false);
1419 else
1420 { /* it already is a .talk file, play this directly */
1421 snprintf(name_mp3_filename, sizeof(name_mp3_filename),
1422 "%s/%s", dir, file);
1423 talk_id(LANG_VOICE_DIR_HOVER, false); /* prefix it */
1424 talk_file(name_mp3_filename, true);
1428 /* These two functions are called by the USB and shutdown handlers */
1429 void tree_flush(void)
1431 scrobbler_shutdown();
1432 #ifdef HAVE_TAGCACHE
1433 tagcache_shutdown();
1434 #endif
1435 playlist_shutdown();
1437 #ifdef HAVE_TC_RAMCACHE
1438 tagcache_unload_ramcache();
1439 #endif
1441 #ifdef HAVE_DIRCACHE
1442 if (global_settings.dircache)
1444 global_settings.dircache_size = dircache_get_cache_size();
1445 # ifdef HAVE_EEPROM_SETTINGS
1446 if (dircache_is_enabled() && firmware_settings.initialized)
1447 dircache_save(DIRCACHE_FILE);
1448 # endif
1449 dircache_disable();
1451 else
1453 global_settings.dircache_size = 0;
1455 settings_save();
1456 #endif
1459 void tree_restore(void)
1461 #ifdef HAVE_EEPROM_SETTINGS
1462 firmware_settings.disk_clean = false;
1463 #endif
1465 #ifdef HAVE_TC_RAMCACHE
1466 remove(TAGCACHE_STATEFILE);
1467 #endif
1469 #ifdef HAVE_DIRCACHE
1470 remove(DIRCACHE_FILE);
1471 if (global_settings.dircache)
1473 /* Print "Scanning disk..." to the display. */
1474 int i;
1475 FOR_NB_SCREENS(i)
1477 screens[i].putsxy((LCD_WIDTH/2) -
1478 ((strlen(str(LANG_DIRCACHE_BUILDING)) *
1479 screens[i].char_width)/2),
1480 LCD_HEIGHT-screens[i].char_height*3,
1481 str(LANG_DIRCACHE_BUILDING));
1482 gui_textarea_update(&screens[i]);
1485 dircache_build(global_settings.dircache_size);
1487 /* Clean the text when we are done. */
1488 FOR_NB_SCREENS(i)
1490 gui_textarea_clear(&screens[i]);
1493 #endif
1494 #ifdef HAVE_TAGCACHE
1495 tagcache_start_scan();
1496 #endif
1497 scrobbler_init();