Fixes a problem where the sim would try to start the WPS on HAVE_RTC_ALARM sims ...
[Rockbox.git] / apps / tree.c
blob74a6134f57b0e367c7a4c4fb5db227ad54ef1e10
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 "sprintf.h"
35 #include "audio.h"
36 #include "playlist.h"
37 #include "menu.h"
38 #include "gwps.h"
39 #include "settings.h"
40 #include "status.h"
41 #include "debug.h"
42 #include "ata.h"
43 #include "rolo.h"
44 #include "icons.h"
45 #include "lang.h"
46 #include "language.h"
47 #include "screens.h"
48 #include "keyboard.h"
49 #include "bookmark.h"
50 #include "onplay.h"
51 #include "buffer.h"
52 #include "plugin.h"
53 #include "power.h"
54 #include "action.h"
55 #include "talk.h"
56 #include "filetypes.h"
57 #include "misc.h"
58 #include "filetree.h"
59 #include "tagtree.h"
60 #ifdef HAVE_RECORDING
61 #include "recorder/recording.h"
62 #endif
63 #include "rtc.h"
64 #include "dircache.h"
65 #ifdef HAVE_TAGCACHE
66 #include "tagcache.h"
67 #endif
68 #include "yesno.h"
69 #include "gwps-common.h"
70 #include "eeprom_settings.h"
71 #include "scrobbler.h"
73 /* gui api */
74 #include "list.h"
75 #include "statusbar.h"
76 #include "splash.h"
77 #include "buttonbar.h"
78 #include "textarea.h"
79 #include "action.h"
81 #include "root_menu.h"
83 #if (LCD_DEPTH > 1) || (defined(HAVE_LCD_REMOTE) && (LCD_REMOTE_DEPTH > 1))
84 #include "backdrop.h"
85 #endif
87 static const struct filetype *filetypes;
88 static int filetypes_count;
90 struct gui_synclist tree_lists;
92 /* I put it here because other files doesn't use it yet,
93 * but should be elsewhere since it will be used mostly everywhere */
94 #ifdef HAS_BUTTONBAR
95 struct gui_buttonbar tree_buttonbar;
96 #endif
97 static struct tree_context tc;
99 bool boot_changed = false;
101 char lastfile[MAX_PATH];
102 static char lastdir[MAX_PATH];
103 #ifdef HAVE_TAGCACHE
104 static int lasttable, lastextra, lastfirstpos;
105 #endif
106 static int max_files = 0;
108 static bool reload_dir = false;
110 static bool start_wps = false;
111 static int curr_context = false;/* id3db or tree*/
113 static int dirbrowse(void);
114 static int ft_play_filenumber(int pos, int attr);
115 static int ft_play_dirname(char* name);
116 static void ft_play_filename(char *dir, char *file);
119 * removes the extension of filename (if it doesn't start with a .)
120 * puts the result in buffer
122 static char * strip_extension(char * filename, char * buffer)
124 int dotpos;
125 char * dot=strrchr(filename, '.');
126 if(dot!=0 && filename[0]!='.')
128 dotpos = dot-filename;
129 strncpy(buffer, filename, dotpos);
130 buffer[dotpos]='\0';
131 return(buffer);
133 else
134 return(filename);
137 static char * tree_get_filename(int selected_item, void * data, char *buffer)
139 struct tree_context * local_tc=(struct tree_context *)data;
140 char *name;
141 int attr=0;
142 bool stripit = false;
143 #ifdef HAVE_TAGCACHE
144 bool id3db = *(local_tc->dirfilter) == SHOW_ID3DB;
146 if (id3db)
148 return tagtree_get_entry(&tc, selected_item)->name;
150 else
151 #endif
153 struct entry* dc = local_tc->dircache;
154 struct entry* e = &dc[selected_item];
155 name = e->name;
156 attr = e->attr;
159 if(!(attr & ATTR_DIRECTORY))
161 switch(global_settings.show_filename_ext)
163 case 0:
164 /* show file extension: off */
165 stripit = true;
166 break;
167 case 1:
168 /* show file extension: on */
169 break;
170 case 2:
171 /* show file extension: only unknown types */
172 stripit = filetype_supported(attr);
173 break;
174 case 3:
175 default:
176 /* show file extension: only when viewing all */
177 stripit = (*(local_tc->dirfilter) != SHOW_ID3DB) &&
178 (*(local_tc->dirfilter) != SHOW_ALL);
179 break;
183 if(stripit)
185 return(strip_extension(name, buffer));
187 return(name);
190 #ifdef HAVE_LCD_COLOR
191 static int tree_get_filecolor(int selected_item, void * data)
193 if (*tc.dirfilter == SHOW_ID3DB)
194 return -1;
195 struct tree_context * local_tc=(struct tree_context *)data;
196 struct entry* dc = local_tc->dircache;
197 struct entry* e = &dc[selected_item];
198 return filetype_get_color(e->name, e->attr);
200 #endif
202 static int tree_get_fileicon(int selected_item, void * data)
204 struct tree_context * local_tc=(struct tree_context *)data;
205 #ifdef HAVE_TAGCACHE
206 bool id3db = *(local_tc->dirfilter) == SHOW_ID3DB;
207 if (id3db) {
208 return tagtree_get_icon(&tc);
210 else
211 #endif
213 struct entry* dc = local_tc->dircache;
214 struct entry* e = &dc[selected_item];
215 return filetype_get_icon(e->attr);
219 bool check_rockboxdir(void)
221 DIR *dir = opendir(ROCKBOX_DIR);
222 if(!dir)
223 { /* No need to localise this message.
224 If .rockbox is missing, it wouldn't work anyway */
225 int i;
226 FOR_NB_SCREENS(i)
227 screens[i].clear_display();
228 gui_syncsplash(HZ*2, "No .rockbox directory");
229 FOR_NB_SCREENS(i)
230 screens[i].clear_display();
231 gui_syncsplash(HZ*2, "Installation incomplete");
232 return false;
234 closedir(dir);
235 return true;
238 /* do this really late in the init sequence */
239 void tree_gui_init(void)
241 gui_sync_wps_screen_init();
243 check_rockboxdir();
245 strcpy(tc.currdir, "/");
247 #ifdef HAVE_LCD_CHARCELLS
248 int i;
249 FOR_NB_SCREENS(i)
250 screens[i].double_height(false);
251 #endif
252 #ifdef HAS_BUTTONBAR
253 gui_buttonbar_init(&tree_buttonbar);
254 /* since archos only have one screen, no need to create more than that */
255 gui_buttonbar_set_display(&tree_buttonbar, &(screens[SCREEN_MAIN]) );
256 #endif
257 gui_synclist_init(&tree_lists, &tree_get_filename, &tc, false, 1);
258 gui_synclist_set_icon_callback(&tree_lists, &tree_get_fileicon);
259 #ifdef HAVE_LCD_COLOR
260 gui_list_set_color_callback(&tree_lists.gui_list[SCREEN_MAIN],
261 &tree_get_filecolor);
262 #endif
267 struct tree_context* tree_get_context(void)
269 return &tc;
272 /* talkbox hovering delay, to avoid immediate disk activity */
273 #define HOVER_DELAY (HZ/2)
275 * Returns the position of a given file in the current directory
276 * returns -1 if not found
278 static int tree_get_file_position(char * filename)
280 int i;
282 /* use lastfile to determine the selected item (default=0) */
283 for (i=0; i < tc.filesindir; i++)
285 struct entry* dc = tc.dircache;
286 struct entry* e = &dc[i];
287 if (!strcasecmp(e->name, filename))
288 return(i);
290 return(-1);/* no file can match, returns undefined */
294 * Called when a new dir is loaded (for example when returning from other apps ...)
295 * also completely redraws the tree
297 static int update_dir(void)
299 bool changed = false;
300 #ifdef HAVE_TAGCACHE
301 bool id3db = *tc.dirfilter == SHOW_ID3DB;
302 /* Checks for changes */
303 if (id3db) {
304 if (tc.currtable != lasttable ||
305 tc.currextra != lastextra ||
306 tc.firstpos != lastfirstpos ||
307 reload_dir)
309 if (tagtree_load(&tc) < 0)
310 return -1;
312 lasttable = tc.currtable;
313 lastextra = tc.currextra;
314 lastfirstpos = tc.firstpos;
315 changed = true;
318 else
319 #endif
321 /* if the tc.currdir has been changed, reload it ...*/
322 if (strncmp(tc.currdir, lastdir, sizeof(lastdir)) || reload_dir) {
324 if (ft_load(&tc, NULL) < 0)
325 return -1;
326 strcpy(lastdir, tc.currdir);
327 changed = true;
330 /* if selected item is undefined */
331 if (tc.selected_item == -1)
333 /* use lastfile to determine the selected item */
334 tc.selected_item = tree_get_file_position(lastfile);
336 /* If the file doesn't exists, select the first one (default) */
337 if(tc.selected_item < 0)
338 tc.selected_item = 0;
339 changed = true;
341 if (changed)
344 #ifdef HAVE_TAGCACHE
345 !id3db &&
346 #endif
347 (tc.dirfull ||
348 tc.filesindir == global_settings.max_files_in_dir) )
350 gui_syncsplash(HZ, ID2P(LANG_SHOWDIR_BUFFER_FULL));
353 #ifdef HAVE_TAGCACHE
354 if (id3db)
356 if (global_settings.show_path_in_browser == SHOW_PATH_FULL
357 || global_settings.show_path_in_browser == SHOW_PATH_CURRENT)
359 gui_synclist_set_title(&tree_lists, tagtree_get_title(&tc),
360 filetype_get_icon(ATTR_DIRECTORY));
362 else
364 /* Must clear the title as the list is reused */
365 gui_synclist_set_title(&tree_lists, NULL, NOICON);
368 else
369 #endif
371 if (global_settings.show_path_in_browser &&
372 *(tc.dirfilter) == SHOW_PLUGINS)
374 char *title;
375 if (!strcmp(tc.currdir, PLUGIN_GAMES_DIR))
376 title = str(LANG_PLUGIN_GAMES);
377 else if (!strcmp(tc.currdir, PLUGIN_APPS_DIR))
378 title = str(LANG_PLUGIN_APPS);
379 else if (!strcmp(tc.currdir, PLUGIN_DEMOS_DIR))
380 title = str(LANG_PLUGIN_DEMOS);
381 else title = str(LANG_PLUGINS);
382 gui_synclist_set_title(&tree_lists, title, Icon_Plugin);
384 else if (global_settings.show_path_in_browser == SHOW_PATH_FULL)
386 gui_synclist_set_title(&tree_lists, tc.currdir,
387 filetype_get_icon(ATTR_DIRECTORY));
389 else if (global_settings.show_path_in_browser == SHOW_PATH_CURRENT)
391 char *title = strrchr(tc.currdir, '/') + 1;
392 if (*title == '\0')
394 /* Display "Files" for the root dir */
395 gui_synclist_set_title(&tree_lists, str(LANG_DIR_BROWSER),
396 filetype_get_icon(ATTR_DIRECTORY));
398 else
399 gui_synclist_set_title(&tree_lists, title,
400 filetype_get_icon(ATTR_DIRECTORY));
402 else
404 /* Must clear the title as the list is reused */
405 gui_synclist_set_title(&tree_lists, NULL, NOICON);
409 gui_synclist_set_nb_items(&tree_lists, tc.filesindir);
410 gui_synclist_set_icon_callback(&tree_lists, tree_get_fileicon);
411 if( tc.selected_item >= tc.filesindir)
412 tc.selected_item=tc.filesindir-1;
414 gui_synclist_select_item(&tree_lists, tc.selected_item);
415 #ifdef HAS_BUTTONBAR
416 if (global_settings.buttonbar) {
417 if (*tc.dirfilter < NUM_FILTER_MODES)
418 gui_buttonbar_set(&tree_buttonbar, str(LANG_SYSFONT_DIRBROWSE_F1),
419 str(LANG_SYSFONT_DIRBROWSE_F2),
420 str(LANG_SYSFONT_DIRBROWSE_F3));
421 else
422 gui_buttonbar_set(&tree_buttonbar, "<<<", "", "");
423 gui_buttonbar_draw(&tree_buttonbar);
425 #endif
426 gui_synclist_draw(&tree_lists);
427 gui_syncstatusbar_draw(&statusbars, true);
428 return tc.filesindir;
431 /* load tracks from specified directory to resume play */
432 void resume_directory(const char *dir)
434 #ifdef HAVE_TAGCACHE
435 bool id3db = *tc.dirfilter == SHOW_ID3DB;
436 #endif
438 if (ft_load(&tc, dir) < 0)
439 return;
440 lastdir[0] = 0;
442 ft_build_playlist(&tc, 0);
444 #ifdef HAVE_TAGCACHE
445 if (id3db)
446 tagtree_load(&tc);
447 #endif
450 /* Returns the current working directory and also writes cwd to buf if
451 non-NULL. In case of error, returns NULL. */
452 char *getcwd(char *buf, int size)
454 if (!buf)
455 return tc.currdir;
456 else if (size > 0)
458 strncpy(buf, tc.currdir, size);
459 return buf;
461 else
462 return NULL;
465 /* Force a reload of the directory next time directory browser is called */
466 void reload_directory(void)
468 reload_dir = true;
471 void get_current_file(char* buffer, int buffer_len)
473 #ifdef HAVE_TAGCACHE
474 /* in ID3DB mode it is a bad idea to call this function */
475 /* (only happens with `follow playlist') */
476 if( *tc.dirfilter == SHOW_ID3DB )
477 return;
478 #endif
480 struct entry* dc = tc.dircache;
481 struct entry* e = &dc[tc.selected_item];
482 snprintf(buffer, buffer_len, "%s/%s", getcwd(NULL,0),
483 tc.dirlength ? e->name : "");
486 /* Allow apps to change our dirfilter directly (required for sub browsers)
487 if they're suddenly going to become a file browser for example */
488 void set_dirfilter(int l_dirfilter)
490 *tc.dirfilter = l_dirfilter;
493 /* Selects a file and update tree context properly */
494 void set_current_file(char *path)
496 char *name;
497 int i;
499 #ifdef HAVE_TAGCACHE
500 /* in ID3DB mode it is a bad idea to call this function */
501 /* (only happens with `follow playlist') */
502 if( *tc.dirfilter == SHOW_ID3DB )
503 return;
504 #endif
506 /* separate directory from filename */
507 /* gets the directory's name and put it into tc.currdir */
508 name = strrchr(path+1,'/');
509 if (name)
511 *name = 0;
512 strcpy(tc.currdir, path);
513 *name = '/';
514 name++;
516 else
518 strcpy(tc.currdir, "/");
519 name = path+1;
522 strcpy(lastfile, name);
525 /* If we changed dir we must recalculate the dirlevel
526 and adjust the selected history properly */
527 if (strncmp(tc.currdir,lastdir,sizeof(lastdir)))
529 tc.dirlevel = 0;
530 tc.selected_item_history[tc.dirlevel] = -1;
532 /* use '/' to calculate dirlevel */
533 for (i = 1; path[i] != '\0'; i++)
535 if (path[i] == '/')
537 tc.dirlevel++;
538 tc.selected_item_history[tc.dirlevel] = -1;
542 if (ft_load(&tc, NULL) >= 0)
544 tc.selected_item = tree_get_file_position(lastfile);
549 /* main loop, handles key events */
550 static int dirbrowse()
552 int numentries=0;
553 char buf[MAX_PATH];
554 int lasti = -1;
555 unsigned button, oldbutton;
556 bool reload_root = false;
557 int lastfilter = *tc.dirfilter;
558 bool lastsortcase = global_settings.sort_case;
559 bool need_update = true;
560 bool exit_func = false;
561 long thumbnail_time = -1; /* for delaying a thumbnail */
563 char* currdir = tc.currdir; /* just a shortcut */
564 #ifdef HAVE_TAGCACHE
565 bool id3db = *tc.dirfilter == SHOW_ID3DB;
567 if (id3db)
568 curr_context=CONTEXT_ID3DB;
569 else
570 #endif
571 curr_context=CONTEXT_TREE;
572 if (tc.selected_item < 0)
573 tc.selected_item = 0;
574 #ifdef HAVE_TAGCACHE
575 tc.firstpos=0;
576 lasttable = -1;
577 lastextra = -1;
578 lastfirstpos = 0;
579 #endif
581 start_wps = false;
582 numentries = update_dir();
583 if (numentries == -1)
584 return false; /* currdir is not a directory */
586 if (*tc.dirfilter > NUM_FILTER_MODES && numentries==0)
588 gui_syncsplash(HZ*2, ID2P(LANG_NO_FILES));
589 return false; /* No files found for rockbox_browser() */
592 while(1) {
593 struct entry *dircache = tc.dircache;
594 bool restore = false;
595 if (tc.dirlevel < 0)
596 tc.dirlevel = 0; /* shouldnt be needed.. this code needs work! */
597 #ifdef BOOTFILE
598 if (boot_changed) {
599 char *lines[]={ID2P(LANG_BOOT_CHANGED), ID2P(LANG_REBOOT_NOW)};
600 struct text_message message={lines, 2};
601 if(gui_syncyesno_run(&message, NULL, NULL)==YESNO_YES)
602 rolo_load("/" BOOTFILE);
603 restore = true;
604 boot_changed = false;
606 #endif
607 button = get_action(CONTEXT_TREE,HZ/5);
608 oldbutton = button;
609 need_update = gui_synclist_do_button(&tree_lists, &button,LIST_WRAP_UNLESS_HELD);
610 tc.selected_item = gui_synclist_get_sel_pos(&tree_lists);
611 switch ( button ) {
612 case ACTION_STD_OK:
613 /* nothing to do if no files to display */
614 if ( numentries == 0 )
615 break;
617 #ifdef HAVE_TAGCACHE
618 switch (id3db?tagtree_enter(&tc):ft_enter(&tc))
619 #else
620 switch (ft_enter(&tc))
621 #endif
623 case 1: reload_dir = true; break;
624 case 2: start_wps = true; break;
625 case 3: exit_func = true; break;
626 default: break;
628 restore = true;
629 break;
631 case ACTION_STD_CANCEL:
632 if (*tc.dirfilter > NUM_FILTER_MODES && tc.dirlevel < 1) {
633 exit_func = true;
634 break;
636 if ((*tc.dirfilter == SHOW_ID3DB && tc.dirlevel == 0) ||
637 ((*tc.dirfilter != SHOW_ID3DB && !strcmp(currdir,"/"))))
639 #ifdef HAVE_LCD_BITMAP /* charcell doesnt have ACTION_TREE_PGLEFT so this isnt needed */
640 if (oldbutton == ACTION_TREE_PGLEFT)
641 break;
642 else
643 #endif
644 return GO_TO_ROOT;
647 #ifdef HAVE_TAGCACHE
648 if (id3db)
649 tagtree_exit(&tc);
650 else
651 #endif
652 if (ft_exit(&tc) == 3)
653 exit_func = true;
655 restore = true;
656 break;
658 case ACTION_TREE_STOP:
659 if (list_stop_handler())
660 restore = true;
661 break;
663 case ACTION_STD_MENU:
664 return GO_TO_ROOT;
665 break;
667 #ifdef HAVE_RECORDING
668 case ACTION_STD_REC:
669 return GO_TO_RECSCREEN;
670 #endif
672 case ACTION_TREE_WPS:
673 return GO_TO_PREVIOUS_MUSIC;
674 break;
675 #ifdef HAVE_QUICKSCREEN
676 case ACTION_STD_QUICKSCREEN:
677 /* don't enter f2 from plugin browser */
678 if (*tc.dirfilter < NUM_FILTER_MODES)
680 if (quick_screen_quick(button))
681 reload_dir = true;
682 restore = true;
684 break;
685 #endif
686 #ifdef BUTTON_F3
687 case ACTION_F3:
688 /* don't enter f3 from plugin browser */
689 if (*tc.dirfilter < NUM_FILTER_MODES)
691 if (quick_screen_f3(BUTTON_F3))
692 reload_dir = true;
693 restore = true;
695 break;
696 #endif
698 case ACTION_STD_CONTEXT:
700 int onplay_result;
701 int attr = 0;
703 if(!numentries)
704 onplay_result = onplay(NULL, 0, curr_context);
705 else {
706 #ifdef HAVE_TAGCACHE
707 if (id3db)
709 if (tagtree_get_attr(&tc) == FILE_ATTR_AUDIO)
711 attr = FILE_ATTR_AUDIO;
712 tagtree_get_filename(&tc, buf, sizeof(buf));
714 else
715 attr = ATTR_DIRECTORY;
717 else
718 #endif
720 attr = dircache[tc.selected_item].attr;
722 if (currdir[1]) /* Not in / */
723 snprintf(buf, sizeof buf, "%s/%s",
724 currdir,
725 dircache[tc.selected_item].name);
726 else /* In / */
727 snprintf(buf, sizeof buf, "/%s",
728 dircache[tc.selected_item].name);
730 onplay_result = onplay(buf, attr, curr_context);
732 switch (onplay_result)
734 case ONPLAY_MAINMENU:
735 return GO_TO_ROOT;
737 case ONPLAY_OK:
738 restore = true;
739 break;
741 case ONPLAY_RELOAD_DIR:
742 reload_dir = true;
743 break;
745 case ONPLAY_START_PLAY:
746 return GO_TO_WPS;
747 break;
749 break;
752 case ACTION_NONE:
753 if (thumbnail_time != -1 &&
754 TIME_AFTER(current_tick, thumbnail_time))
755 { /* a delayed hovering thumbnail is due now */
756 int res;
757 int attr;
758 char* name;
760 #ifdef HAVE_TAGCACHE
761 if (id3db)
763 attr = tagtree_get_attr(&tc);
764 name = tagtree_get_entry(&tc, lasti)->name;
766 else
767 #endif
769 attr = dircache[lasti].attr;
770 name = dircache[lasti].name;
773 if (attr & ATTR_DIRECTORY)
775 DEBUGF("Playing directory thumbnail: %s", currdir);
776 res = ft_play_dirname(name);
777 if (res < 0) /* failed, not existing */
779 /* say the number or spell if required as a fallback */
780 switch (global_settings.talk_dir)
782 case 1: /* dirs as numbers */
783 talk_id(VOICE_DIR, false);
784 talk_number(lasti+1, true);
785 break;
787 case 2: /* dirs spelled */
788 talk_spell(name, false);
789 break;
793 else
795 DEBUGF("Playing file thumbnail: %s/%s%s\n",
796 currdir, name,
797 file_thumbnail_ext);
798 /* no fallback necessary, we knew in advance
799 that the file exists */
800 ft_play_filename(currdir, name);
802 thumbnail_time = -1; /* job done */
804 gui_syncstatusbar_draw(&statusbars, false);
805 break;
807 #ifdef HAVE_HOTSWAP
808 case SYS_FS_CHANGED:
809 #ifdef HAVE_TAGCACHE
810 if (!id3db)
811 #endif
812 reload_dir = true;
813 /* The 'dir no longer valid' situation will be caught later
814 * by checking the showdir() result. */
815 break;
816 #endif
818 default:
819 if (default_event_handler(button) == SYS_USB_CONNECTED)
821 if(*tc.dirfilter > NUM_FILTER_MODES)
822 /* leave sub-browsers after usb, doing otherwise
823 might be confusing to the user */
824 exit_func = true;
825 else
826 reload_dir = true;
828 break;
830 if (start_wps)
831 return GO_TO_WPS;
832 if ( button )
834 ata_spin();
838 check_rescan:
839 /* do we need to rescan dir? */
840 if (reload_dir || reload_root ||
841 lastfilter != *tc.dirfilter ||
842 lastsortcase != global_settings.sort_case)
844 if ( reload_root ) {
845 strcpy(currdir, "/");
846 tc.dirlevel = 0;
847 #ifdef HAVE_TAGCACHE
848 tc.currtable = 0;
849 tc.currextra = 0;
850 lasttable = -1;
851 lastextra = -1;
852 #endif
853 reload_root = false;
856 if (! reload_dir )
858 gui_synclist_select_item(&tree_lists, 0);
859 gui_synclist_draw(&tree_lists);
860 tc.selected_item = 0;
861 lastdir[0] = 0;
864 lastfilter = *tc.dirfilter;
865 lastsortcase = global_settings.sort_case;
866 restore = true;
869 if (exit_func)
870 return GO_TO_PREVIOUS;
872 if (restore || reload_dir) {
873 /* restore display */
874 numentries = update_dir();
875 if (currdir[1] && (numentries < 0))
876 { /* not in root and reload failed */
877 reload_root = true; /* try root */
878 reload_dir = false;
879 goto check_rescan;
881 need_update = true;
882 reload_dir = false;
885 if(need_update) {
886 need_update=false;
887 if ( numentries > 0 ) {
888 /* Voice the file if changed */
889 if(lasti != tc.selected_item || restore) {
890 int attr;
891 char* name;
893 lasti = tc.selected_item;
894 thumbnail_time = -1; /* Cancel whatever we were
895 about to say */
897 #ifdef HAVE_TAGCACHE
898 if (id3db)
900 attr = tagtree_get_attr(&tc);
901 name = tagtree_get_entry(&tc, tc.selected_item)->name;
903 else
904 #endif
906 attr = dircache[tc.selected_item].attr;
907 name = dircache[tc.selected_item].name;
910 /* Directory? */
911 if (attr & ATTR_DIRECTORY)
913 /* schedule thumbnail playback if required */
914 if (global_settings.talk_dir_clip)
915 thumbnail_time = current_tick + HOVER_DELAY;
916 else
918 /* talk directly */
919 switch (global_settings.talk_dir)
921 case 1: /* dirs as numbers */
922 talk_id(VOICE_DIR, false);
923 talk_number(tc.selected_item+1, true);
924 break;
926 case 2: /* dirs spelled */
927 talk_spell(name, false);
928 break;
932 else /* file */
934 /* schedule thumbnail playback if required */
935 if (global_settings.talk_file_clip && (attr & FILE_ATTR_THUMBNAIL))
936 thumbnail_time = current_tick + HOVER_DELAY;
937 else
939 /* talk directly */
940 switch (global_settings.talk_file)
942 case 1: /* files as numbers */
943 ft_play_filenumber(
944 tc.selected_item-tc.dirsindir+1,
945 attr & FILE_ATTR_MASK);
946 break;
948 case 2: /* files spelled */
949 talk_spell(name, false);
950 break;
958 return true;
961 static int plsize = 0;
962 static long pltick;
963 static bool add_dir(char* dirname, int len, int fd)
965 bool abort = false;
966 DIR* dir;
968 /* check for user abort */
969 if (action_userabort(TIMEOUT_NOBLOCK))
970 return true;
972 dir = opendir(dirname);
973 if(!dir)
974 return true;
976 while (true) {
977 struct dirent *entry;
979 entry = readdir(dir);
980 if (!entry)
981 break;
982 if (entry->attribute & ATTR_DIRECTORY) {
983 int dirlen = strlen(dirname);
984 bool result;
986 if (!strcmp((char *)entry->d_name, ".") ||
987 !strcmp((char *)entry->d_name, ".."))
988 continue;
990 if (dirname[1])
991 snprintf(dirname+dirlen, len-dirlen, "/%s", entry->d_name);
992 else
993 snprintf(dirname, len, "/%s", entry->d_name);
995 result = add_dir(dirname, len, fd);
996 dirname[dirlen] = '\0';
997 if (result) {
998 abort = true;
999 break;
1002 else {
1003 int x = strlen((char *)entry->d_name);
1004 int i;
1005 char *cp = strrchr((char *)entry->d_name,'.');
1007 if (cp) {
1008 cp++;
1010 /* add all supported audio files to playlists */
1011 for (i=0; i < filetypes_count; i++) {
1012 if (filetypes[i].tree_attr == FILE_ATTR_AUDIO) {
1013 if (!strcasecmp(cp, filetypes[i].extension)) {
1014 char buf[8];
1015 int i;
1016 write(fd, dirname, strlen(dirname));
1017 write(fd, "/", 1);
1018 write(fd, entry->d_name, x);
1019 write(fd, "\n", 1);
1021 plsize++;
1022 if(TIME_AFTER(current_tick, pltick+HZ/4)) {
1023 pltick = current_tick;
1025 snprintf(buf, sizeof buf, "%d", plsize);
1026 #ifdef HAVE_LCD_BITMAP
1027 FOR_NB_SCREENS(i)
1029 screens[i].puts(0, 4, (unsigned char *)buf);
1030 gui_textarea_update(&screens[i]);
1032 #else
1033 x = 10;
1034 if (plsize > 999)
1035 x=7;
1036 else {
1037 if (plsize > 99)
1038 x=8;
1039 else {
1040 if (plsize > 9)
1041 x=9;
1044 FOR_NB_SCREENS(i) {
1045 screens[i].puts(x,0,buf);
1047 #endif
1049 break;
1056 closedir(dir);
1058 return abort;
1061 bool create_playlist(void)
1063 int fd;
1064 int i;
1065 char filename[MAX_PATH];
1067 pltick = current_tick;
1069 snprintf(filename, sizeof filename, "%s.m3u8",
1070 tc.currdir[1] ? tc.currdir : "/root");
1071 FOR_NB_SCREENS(i)
1073 gui_textarea_clear(&screens[i]);
1074 screens[i].puts(0, 0, str(LANG_CREATING));
1075 screens[i].puts_scroll(0, 1, (unsigned char *)filename);
1076 #if defined(HAVE_LCD_BITMAP) || defined(SIMULATOR)
1077 gui_textarea_update(&screens[i]);
1078 #endif
1080 fd = creat(filename);
1081 if (fd < 0)
1082 return false;
1084 trigger_cpu_boost();
1086 snprintf(filename, sizeof(filename), "%s",
1087 tc.currdir[1] ? tc.currdir : "/");
1088 plsize = 0;
1089 add_dir(filename, sizeof(filename), fd);
1090 close(fd);
1092 sleep(HZ);
1094 return true;
1097 int rockbox_browse(const char *root, int dirfilter)
1099 int ret_val = 0;
1100 int *last_filter = tc.dirfilter;
1101 tc.dirfilter = &dirfilter;
1103 reload_dir = true;
1104 if (dirfilter >= NUM_FILTER_MODES)
1106 static struct tree_context backup;
1107 int last_context;
1109 backup = tc;
1110 tc.selected_item = 0;
1111 tc.dirlevel = 0;
1112 memcpy(tc.currdir, root, sizeof(tc.currdir));
1113 start_wps = false;
1114 last_context = curr_context;
1116 ret_val = dirbrowse();
1117 tc = backup;
1118 curr_context = last_context;
1120 else
1122 static char buf[MAX_PATH];
1123 if (dirfilter != SHOW_ID3DB)
1124 tc.dirfilter = &global_settings.dirfilter;
1125 strcpy(buf,root);
1126 set_current_file(buf);
1127 ret_val = dirbrowse();
1129 tc.dirfilter = last_filter;
1130 return ret_val;
1133 void tree_mem_init(void)
1135 /* We copy the settings value in case it is changed by the user. We can't
1136 use it until the next reboot. */
1137 max_files = global_settings.max_files_in_dir;
1139 /* initialize tree context struct */
1140 memset(&tc, 0, sizeof(tc));
1141 tc.dirfilter = &global_settings.dirfilter;
1143 tc.name_buffer_size = AVERAGE_FILENAME_LENGTH * max_files;
1144 tc.name_buffer = buffer_alloc(tc.name_buffer_size);
1146 tc.dircache_size = max_files * sizeof(struct entry);
1147 tc.dircache = buffer_alloc(tc.dircache_size);
1148 tree_get_filetypes(&filetypes, &filetypes_count);
1151 void bookmark_play(char *resume_file, int index, int offset, int seed,
1152 char *filename)
1154 int i;
1155 char* suffix = strrchr(resume_file, '.');
1157 if (suffix != NULL &&
1158 (!strcasecmp(suffix, ".m3u") || !strcasecmp(suffix, ".m3u8")))
1160 /* Playlist playback */
1161 char* slash;
1162 /* check that the file exists */
1163 int fd = open(resume_file, O_RDONLY);
1164 if(fd<0)
1165 return;
1166 close(fd);
1168 slash = strrchr(resume_file,'/');
1169 if (slash)
1171 char* cp;
1172 *slash=0;
1174 cp=resume_file;
1175 if (!cp[0])
1176 cp="/";
1178 if (playlist_create(cp, slash+1) != -1)
1180 if (global_settings.playlist_shuffle)
1181 playlist_shuffle(seed, -1);
1182 playlist_start(index,offset);
1184 *slash='/';
1187 else
1189 /* Directory playback */
1190 lastdir[0]='\0';
1191 if (playlist_create(resume_file, NULL) != -1)
1193 char* peek_filename;
1194 resume_directory(resume_file);
1195 if (global_settings.playlist_shuffle)
1196 playlist_shuffle(seed, -1);
1198 /* Check if the file is at the same spot in the directory,
1199 else search for it */
1200 peek_filename = playlist_peek(index);
1202 if (peek_filename == NULL)
1203 return;
1205 if (strcmp(strrchr(peek_filename, '/') + 1, filename))
1207 for ( i=0; i < playlist_amount(); i++ )
1209 peek_filename = playlist_peek(i);
1211 if (peek_filename == NULL)
1212 return;
1214 if (!strcmp(strrchr(peek_filename, '/') + 1, filename))
1215 break;
1217 if (i < playlist_amount())
1218 index = i;
1219 else
1220 return;
1222 playlist_start(index,offset);
1226 start_wps=true;
1229 static int ft_play_filenumber(int pos, int attr)
1231 /* try to find a voice ID for the extension, if known */
1232 int j;
1233 int ext_id = -1; /* default to none */
1234 for (j=0; j<filetypes_count; j++)
1236 if (attr == filetypes[j].tree_attr)
1238 ext_id = filetypes[j].voiceclip;
1239 break;
1243 talk_id(VOICE_FILE, false);
1244 talk_number(pos, true);
1245 talk_id(ext_id, true);
1246 return 1;
1249 static int ft_play_dirname(char* name)
1251 int fd;
1252 char dirname_mp3_filename[MAX_PATH+1];
1254 #if CONFIG_CODEC != SWCODEC
1255 if (audio_status() & AUDIO_STATUS_PLAY)
1256 return 0;
1257 #endif
1259 snprintf(dirname_mp3_filename, sizeof(dirname_mp3_filename), "%s/%s/%s",
1260 tc.currdir[1] ? tc.currdir : "" , name,
1261 dir_thumbnail_name);
1263 DEBUGF("Checking for %s\n", dirname_mp3_filename);
1265 fd = open(dirname_mp3_filename, O_RDONLY);
1266 if (fd < 0)
1268 DEBUGF("Failed to find: %s\n", dirname_mp3_filename);
1269 return -1;
1272 close(fd);
1274 DEBUGF("Found: %s\n", dirname_mp3_filename);
1276 talk_file(dirname_mp3_filename, false);
1277 return 1;
1280 static void ft_play_filename(char *dir, char *file)
1282 char name_mp3_filename[MAX_PATH+1];
1284 #if CONFIG_CODEC != SWCODEC
1285 if (audio_status() & AUDIO_STATUS_PLAY)
1286 return;
1287 #endif
1289 if (strcasecmp(&file[strlen(file) - strlen(file_thumbnail_ext)],
1290 file_thumbnail_ext))
1291 { /* file has no .talk extension */
1292 snprintf(name_mp3_filename, sizeof(name_mp3_filename),
1293 "%s/%s%s", dir, file, file_thumbnail_ext);
1295 talk_file(name_mp3_filename, false);
1297 else
1298 { /* it already is a .talk file, play this directly */
1299 snprintf(name_mp3_filename, sizeof(name_mp3_filename),
1300 "%s/%s", dir, file);
1301 talk_id(LANG_VOICE_DIR_HOVER, false); /* prefix it */
1302 talk_file(name_mp3_filename, true);
1306 /* These two functions are called by the USB and shutdown handlers */
1307 void tree_flush(void)
1309 scrobbler_shutdown();
1310 #ifdef HAVE_TAGCACHE
1311 tagcache_shutdown();
1312 #endif
1313 playlist_shutdown();
1315 #ifdef HAVE_TC_RAMCACHE
1316 tagcache_unload_ramcache();
1317 #endif
1319 #ifdef HAVE_DIRCACHE
1321 int old_val = global_status.dircache_size;
1322 if (global_settings.dircache)
1324 if (!dircache_is_initializing())
1325 global_status.dircache_size = dircache_get_cache_size();
1326 # ifdef HAVE_EEPROM_SETTINGS
1327 if (firmware_settings.initialized)
1328 dircache_save();
1329 # endif
1330 dircache_disable();
1332 else
1334 global_status.dircache_size = 0;
1336 if (old_val != global_status.dircache_size)
1337 status_save();
1339 #endif
1342 void tree_restore(void)
1344 #ifdef HAVE_EEPROM_SETTINGS
1345 firmware_settings.disk_clean = false;
1346 #endif
1348 #ifdef HAVE_TC_RAMCACHE
1349 remove(TAGCACHE_STATEFILE);
1350 #endif
1352 #ifdef HAVE_DIRCACHE
1353 remove(DIRCACHE_FILE);
1354 if (global_settings.dircache)
1356 /* Print "Scanning disk..." to the display. */
1357 int i;
1358 FOR_NB_SCREENS(i)
1360 screens[i].putsxy((LCD_WIDTH/2) -
1361 ((strlen(str(LANG_SCANNING_DISK)) *
1362 screens[i].char_width)/2),
1363 LCD_HEIGHT-screens[i].char_height*3,
1364 str(LANG_SCANNING_DISK));
1365 gui_textarea_update(&screens[i]);
1367 cond_talk_ids_fq(LANG_SCANNING_DISK);
1369 dircache_build(global_status.dircache_size);
1371 /* Clean the text when we are done. */
1372 FOR_NB_SCREENS(i)
1374 gui_textarea_clear(&screens[i]);
1377 #endif
1378 #ifdef HAVE_TAGCACHE
1379 tagcache_start_scan();
1380 #endif
1381 scrobbler_init();