Revert the recent change to bufread()
[Rockbox.git] / apps / tree.c
blob6a4c97adc15b81d22e92c338397b14d0eb04a456
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_dirname(char* name);
115 static void ft_play_filename(char *dir, char *file);
116 static void say_filetype(int attr);
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 static int tree_voice_cb(int selected_item, void * data)
221 struct tree_context * local_tc=(struct tree_context *)data;
222 char *name;
223 int attr=0;
224 #ifdef HAVE_TAGCACHE
225 bool id3db = *(local_tc->dirfilter) == SHOW_ID3DB;
227 if (id3db)
229 attr = tagtree_get_attr(local_tc);
230 name = tagtree_get_entry(local_tc, selected_item)->name;
232 else
233 #endif
235 struct entry* dc = local_tc->dircache;
236 struct entry* e = &dc[selected_item];
237 name = e->name;
238 attr = e->attr;
240 bool is_dir = (attr & ATTR_DIRECTORY);
241 bool did_clip = false;
242 /* First the .talk clip case */
243 if(is_dir)
245 if(global_settings.talk_dir_clip)
247 DEBUGF("Playing directory thumbnail: %s", local_tc->currdir);
248 did_clip = true;
249 if(ft_play_dirname(name) <0)
250 /* failed, not existing */
251 did_clip = false;
253 } else { /* it's a file */
254 if (global_settings.talk_file_clip && (attr & FILE_ATTR_THUMBNAIL))
256 did_clip = true;
257 DEBUGF("Playing file thumbnail: %s/%s%s\n",
258 local_tc->currdir, name, file_thumbnail_ext);
259 ft_play_filename(local_tc->currdir, name);
262 if(!did_clip)
264 /* say the number or spell if required or as a fallback */
265 switch (is_dir ? global_settings.talk_dir : global_settings.talk_file)
267 case 1: /* as numbers */
268 talk_id(is_dir ? VOICE_DIR : VOICE_FILE, false);
269 talk_number(selected_item+1 - (is_dir ? 0 : local_tc->dirsindir),
270 true);
271 if(!is_dir)
272 say_filetype(attr);
273 break;
274 case 2: /* spelled */
275 talk_spell(name, false);
276 break;
279 return 0;
282 bool check_rockboxdir(void)
284 DIR *dir = opendir(ROCKBOX_DIR);
285 if(!dir)
286 { /* No need to localise this message.
287 If .rockbox is missing, it wouldn't work anyway */
288 int i;
289 FOR_NB_SCREENS(i)
290 screens[i].clear_display();
291 gui_syncsplash(HZ*2, "No .rockbox directory");
292 FOR_NB_SCREENS(i)
293 screens[i].clear_display();
294 gui_syncsplash(HZ*2, "Installation incomplete");
295 return false;
297 closedir(dir);
298 return true;
301 /* do this really late in the init sequence */
302 void tree_gui_init(void)
304 gui_sync_wps_screen_init();
306 check_rockboxdir();
308 strcpy(tc.currdir, "/");
310 #ifdef HAVE_LCD_CHARCELLS
311 int i;
312 FOR_NB_SCREENS(i)
313 screens[i].double_height(false);
314 #endif
315 #ifdef HAS_BUTTONBAR
316 gui_buttonbar_init(&tree_buttonbar);
317 /* since archos only have one screen, no need to create more than that */
318 gui_buttonbar_set_display(&tree_buttonbar, &(screens[SCREEN_MAIN]) );
319 #endif
320 gui_synclist_init(&tree_lists, &tree_get_filename, &tc, false, 1);
321 gui_synclist_set_voice_callback(&tree_lists, tree_voice_cb);
322 gui_synclist_set_icon_callback(&tree_lists, &tree_get_fileicon);
323 #ifdef HAVE_LCD_COLOR
324 gui_list_set_color_callback(&tree_lists.gui_list[SCREEN_MAIN],
325 &tree_get_filecolor);
326 #endif
331 struct tree_context* tree_get_context(void)
333 return &tc;
337 * Returns the position of a given file in the current directory
338 * returns -1 if not found
340 static int tree_get_file_position(char * filename)
342 int i;
344 /* use lastfile to determine the selected item (default=0) */
345 for (i=0; i < tc.filesindir; i++)
347 struct entry* dc = tc.dircache;
348 struct entry* e = &dc[i];
349 if (!strcasecmp(e->name, filename))
350 return(i);
352 return(-1);/* no file can match, returns undefined */
356 * Called when a new dir is loaded (for example when returning from other apps ...)
357 * also completely redraws the tree
359 static int update_dir(void)
361 bool changed = false;
362 #ifdef HAVE_TAGCACHE
363 bool id3db = *tc.dirfilter == SHOW_ID3DB;
364 /* Checks for changes */
365 if (id3db) {
366 if (tc.currtable != lasttable ||
367 tc.currextra != lastextra ||
368 tc.firstpos != lastfirstpos ||
369 reload_dir)
371 if (tagtree_load(&tc) < 0)
372 return -1;
374 lasttable = tc.currtable;
375 lastextra = tc.currextra;
376 lastfirstpos = tc.firstpos;
377 changed = true;
380 else
381 #endif
383 /* if the tc.currdir has been changed, reload it ...*/
384 if (strncmp(tc.currdir, lastdir, sizeof(lastdir)) || reload_dir) {
386 if (ft_load(&tc, NULL) < 0)
387 return -1;
388 strcpy(lastdir, tc.currdir);
389 changed = true;
392 /* if selected item is undefined */
393 if (tc.selected_item == -1)
395 /* use lastfile to determine the selected item */
396 tc.selected_item = tree_get_file_position(lastfile);
398 /* If the file doesn't exists, select the first one (default) */
399 if(tc.selected_item < 0)
400 tc.selected_item = 0;
401 changed = true;
403 if (changed)
406 #ifdef HAVE_TAGCACHE
407 !id3db &&
408 #endif
409 (tc.dirfull ||
410 tc.filesindir == global_settings.max_files_in_dir) )
412 gui_syncsplash(HZ, ID2P(LANG_SHOWDIR_BUFFER_FULL));
415 #ifdef HAVE_TAGCACHE
416 if (id3db)
418 if (global_settings.show_path_in_browser == SHOW_PATH_FULL
419 || global_settings.show_path_in_browser == SHOW_PATH_CURRENT)
421 gui_synclist_set_title(&tree_lists, tagtree_get_title(&tc),
422 filetype_get_icon(ATTR_DIRECTORY));
424 else
426 /* Must clear the title as the list is reused */
427 gui_synclist_set_title(&tree_lists, NULL, NOICON);
430 else
431 #endif
433 if (global_settings.show_path_in_browser &&
434 *(tc.dirfilter) == SHOW_PLUGINS)
436 char *title;
437 if (!strcmp(tc.currdir, PLUGIN_GAMES_DIR))
438 title = str(LANG_PLUGIN_GAMES);
439 else if (!strcmp(tc.currdir, PLUGIN_APPS_DIR))
440 title = str(LANG_PLUGIN_APPS);
441 else if (!strcmp(tc.currdir, PLUGIN_DEMOS_DIR))
442 title = str(LANG_PLUGIN_DEMOS);
443 else title = str(LANG_PLUGINS);
444 gui_synclist_set_title(&tree_lists, title, Icon_Plugin);
446 else if (global_settings.show_path_in_browser == SHOW_PATH_FULL)
448 gui_synclist_set_title(&tree_lists, tc.currdir,
449 filetype_get_icon(ATTR_DIRECTORY));
451 else if (global_settings.show_path_in_browser == SHOW_PATH_CURRENT)
453 char *title = strrchr(tc.currdir, '/') + 1;
454 if (*title == '\0')
456 /* Display "Files" for the root dir */
457 gui_synclist_set_title(&tree_lists, str(LANG_DIR_BROWSER),
458 filetype_get_icon(ATTR_DIRECTORY));
460 else
461 gui_synclist_set_title(&tree_lists, title,
462 filetype_get_icon(ATTR_DIRECTORY));
464 else
466 /* Must clear the title as the list is reused */
467 gui_synclist_set_title(&tree_lists, NULL, NOICON);
471 gui_synclist_set_nb_items(&tree_lists, tc.filesindir);
472 gui_synclist_set_icon_callback(&tree_lists, tree_get_fileicon);
473 if( tc.selected_item >= tc.filesindir)
474 tc.selected_item=tc.filesindir-1;
476 gui_synclist_select_item(&tree_lists, tc.selected_item);
477 #ifdef HAS_BUTTONBAR
478 if (global_settings.buttonbar) {
479 if (*tc.dirfilter < NUM_FILTER_MODES)
480 gui_buttonbar_set(&tree_buttonbar, str(LANG_SYSFONT_DIRBROWSE_F1),
481 str(LANG_SYSFONT_DIRBROWSE_F2),
482 str(LANG_SYSFONT_DIRBROWSE_F3));
483 else
484 gui_buttonbar_set(&tree_buttonbar, "<<<", "", "");
485 gui_buttonbar_draw(&tree_buttonbar);
487 #endif
488 gui_synclist_draw(&tree_lists);
489 gui_synclist_speak_item(&tree_lists);
490 gui_syncstatusbar_draw(&statusbars, true);
491 return tc.filesindir;
494 /* load tracks from specified directory to resume play */
495 void resume_directory(const char *dir)
497 #ifdef HAVE_TAGCACHE
498 bool id3db = *tc.dirfilter == SHOW_ID3DB;
499 #endif
501 if (ft_load(&tc, dir) < 0)
502 return;
503 lastdir[0] = 0;
505 ft_build_playlist(&tc, 0);
507 #ifdef HAVE_TAGCACHE
508 if (id3db)
509 tagtree_load(&tc);
510 #endif
513 /* Returns the current working directory and also writes cwd to buf if
514 non-NULL. In case of error, returns NULL. */
515 char *getcwd(char *buf, int size)
517 if (!buf)
518 return tc.currdir;
519 else if (size > 0)
521 strncpy(buf, tc.currdir, size);
522 return buf;
524 else
525 return NULL;
528 /* Force a reload of the directory next time directory browser is called */
529 void reload_directory(void)
531 reload_dir = true;
534 void get_current_file(char* buffer, int buffer_len)
536 #ifdef HAVE_TAGCACHE
537 /* in ID3DB mode it is a bad idea to call this function */
538 /* (only happens with `follow playlist') */
539 if( *tc.dirfilter == SHOW_ID3DB )
540 return;
541 #endif
543 struct entry* dc = tc.dircache;
544 struct entry* e = &dc[tc.selected_item];
545 snprintf(buffer, buffer_len, "%s/%s", getcwd(NULL,0),
546 tc.dirlength ? e->name : "");
549 /* Allow apps to change our dirfilter directly (required for sub browsers)
550 if they're suddenly going to become a file browser for example */
551 void set_dirfilter(int l_dirfilter)
553 *tc.dirfilter = l_dirfilter;
556 /* Selects a file and update tree context properly */
557 void set_current_file(char *path)
559 char *name;
560 int i;
562 #ifdef HAVE_TAGCACHE
563 /* in ID3DB mode it is a bad idea to call this function */
564 /* (only happens with `follow playlist') */
565 if( *tc.dirfilter == SHOW_ID3DB )
566 return;
567 #endif
569 /* separate directory from filename */
570 /* gets the directory's name and put it into tc.currdir */
571 name = strrchr(path+1,'/');
572 if (name)
574 *name = 0;
575 strcpy(tc.currdir, path);
576 *name = '/';
577 name++;
579 else
581 strcpy(tc.currdir, "/");
582 name = path+1;
585 strcpy(lastfile, name);
588 /* If we changed dir we must recalculate the dirlevel
589 and adjust the selected history properly */
590 if (strncmp(tc.currdir,lastdir,sizeof(lastdir)))
592 tc.dirlevel = 0;
593 tc.selected_item_history[tc.dirlevel] = -1;
595 /* use '/' to calculate dirlevel */
596 for (i = 1; path[i] != '\0'; i++)
598 if (path[i] == '/')
600 tc.dirlevel++;
601 tc.selected_item_history[tc.dirlevel] = -1;
605 if (ft_load(&tc, NULL) >= 0)
607 tc.selected_item = tree_get_file_position(lastfile);
612 /* main loop, handles key events */
613 static int dirbrowse()
615 int numentries=0;
616 char buf[MAX_PATH];
617 unsigned button, oldbutton;
618 bool reload_root = false;
619 int lastfilter = *tc.dirfilter;
620 bool lastsortcase = global_settings.sort_case;
621 bool exit_func = false;
623 char* currdir = tc.currdir; /* just a shortcut */
624 #ifdef HAVE_TAGCACHE
625 bool id3db = *tc.dirfilter == SHOW_ID3DB;
627 if (id3db)
628 curr_context=CONTEXT_ID3DB;
629 else
630 #endif
631 curr_context=CONTEXT_TREE;
632 if (tc.selected_item < 0)
633 tc.selected_item = 0;
634 #ifdef HAVE_TAGCACHE
635 tc.firstpos=0;
636 lasttable = -1;
637 lastextra = -1;
638 lastfirstpos = 0;
639 #endif
641 start_wps = false;
642 numentries = update_dir();
643 reload_dir = false;
644 if (numentries == -1)
645 return GO_TO_PREVIOUS; /* currdir is not a directory */
647 if (*tc.dirfilter > NUM_FILTER_MODES && numentries==0)
649 gui_syncsplash(HZ*2, ID2P(LANG_NO_FILES));
650 return GO_TO_PREVIOUS; /* No files found for rockbox_browser() */
653 while(1) {
654 struct entry *dircache = tc.dircache;
655 bool restore = false;
656 if (tc.dirlevel < 0)
657 tc.dirlevel = 0; /* shouldnt be needed.. this code needs work! */
658 #ifdef BOOTFILE
659 if (boot_changed) {
660 char *lines[]={ID2P(LANG_BOOT_CHANGED), ID2P(LANG_REBOOT_NOW)};
661 struct text_message message={lines, 2};
662 if(gui_syncyesno_run(&message, NULL, NULL)==YESNO_YES)
663 rolo_load("/" BOOTFILE);
664 restore = true;
665 boot_changed = false;
667 #endif
668 button = get_action(CONTEXT_TREE,
669 list_do_action_timeout(&tree_lists, HZ/2));
670 oldbutton = button;
671 gui_synclist_do_button(&tree_lists, &button,LIST_WRAP_UNLESS_HELD);
672 tc.selected_item = gui_synclist_get_sel_pos(&tree_lists);
673 switch ( button ) {
674 case ACTION_STD_OK:
675 /* nothing to do if no files to display */
676 if ( numentries == 0 )
677 break;
679 #ifdef HAVE_TAGCACHE
680 switch (id3db?tagtree_enter(&tc):ft_enter(&tc))
681 #else
682 switch (ft_enter(&tc))
683 #endif
685 case 1: reload_dir = true; break;
686 case 2: start_wps = true; break;
687 case 3: exit_func = true; break;
688 default: break;
690 restore = true;
691 break;
693 case ACTION_STD_CANCEL:
694 if (*tc.dirfilter > NUM_FILTER_MODES && tc.dirlevel < 1) {
695 exit_func = true;
696 break;
698 if ((*tc.dirfilter == SHOW_ID3DB && tc.dirlevel == 0) ||
699 ((*tc.dirfilter != SHOW_ID3DB && !strcmp(currdir,"/"))))
701 #ifdef HAVE_LCD_BITMAP /* charcell doesnt have ACTION_TREE_PGLEFT so this isnt needed */
702 if (oldbutton == ACTION_TREE_PGLEFT)
703 break;
704 else
705 #endif
706 return GO_TO_ROOT;
709 #ifdef HAVE_TAGCACHE
710 if (id3db)
711 tagtree_exit(&tc);
712 else
713 #endif
714 if (ft_exit(&tc) == 3)
715 exit_func = true;
717 restore = true;
718 break;
720 case ACTION_TREE_STOP:
721 if (list_stop_handler())
722 restore = true;
723 break;
725 case ACTION_STD_MENU:
726 return GO_TO_ROOT;
727 break;
729 #ifdef HAVE_RECORDING
730 case ACTION_STD_REC:
731 return GO_TO_RECSCREEN;
732 #endif
734 case ACTION_TREE_WPS:
735 return GO_TO_PREVIOUS_MUSIC;
736 break;
737 #ifdef HAVE_QUICKSCREEN
738 case ACTION_STD_QUICKSCREEN:
739 /* don't enter f2 from plugin browser */
740 if (*tc.dirfilter < NUM_FILTER_MODES)
742 if (quick_screen_quick(button))
743 reload_dir = true;
744 restore = true;
746 break;
747 #endif
748 #ifdef BUTTON_F3
749 case ACTION_F3:
750 /* don't enter f3 from plugin browser */
751 if (*tc.dirfilter < NUM_FILTER_MODES)
753 if (quick_screen_f3(BUTTON_F3))
754 reload_dir = true;
755 restore = true;
757 break;
758 #endif
760 case ACTION_STD_CONTEXT:
762 int onplay_result;
763 int attr = 0;
765 if(!numentries)
766 onplay_result = onplay(NULL, 0, curr_context);
767 else {
768 #ifdef HAVE_TAGCACHE
769 if (id3db)
771 if (tagtree_get_attr(&tc) == FILE_ATTR_AUDIO)
773 attr = FILE_ATTR_AUDIO;
774 tagtree_get_filename(&tc, buf, sizeof(buf));
776 else
777 attr = ATTR_DIRECTORY;
779 else
780 #endif
782 attr = dircache[tc.selected_item].attr;
784 if (currdir[1]) /* Not in / */
785 snprintf(buf, sizeof buf, "%s/%s",
786 currdir,
787 dircache[tc.selected_item].name);
788 else /* In / */
789 snprintf(buf, sizeof buf, "/%s",
790 dircache[tc.selected_item].name);
792 onplay_result = onplay(buf, attr, curr_context);
794 switch (onplay_result)
796 case ONPLAY_MAINMENU:
797 return GO_TO_ROOT;
799 case ONPLAY_OK:
800 restore = true;
801 break;
803 case ONPLAY_RELOAD_DIR:
804 reload_dir = true;
805 break;
807 case ONPLAY_START_PLAY:
808 return GO_TO_WPS;
809 break;
811 break;
814 case ACTION_NONE:
815 gui_syncstatusbar_draw(&statusbars, false);
816 break;
818 #ifdef HAVE_HOTSWAP
819 case SYS_FS_CHANGED:
820 #ifdef HAVE_TAGCACHE
821 if (!id3db)
822 #endif
823 reload_dir = true;
824 /* The 'dir no longer valid' situation will be caught later
825 * by checking the showdir() result. */
826 break;
827 #endif
829 default:
830 if (default_event_handler(button) == SYS_USB_CONNECTED)
832 if(*tc.dirfilter > NUM_FILTER_MODES)
833 /* leave sub-browsers after usb, doing otherwise
834 might be confusing to the user */
835 exit_func = true;
836 else
837 reload_dir = true;
839 break;
841 if (start_wps)
842 return GO_TO_WPS;
843 if ( button )
845 ata_spin();
849 check_rescan:
850 /* do we need to rescan dir? */
851 if (reload_dir || reload_root ||
852 lastfilter != *tc.dirfilter ||
853 lastsortcase != global_settings.sort_case)
855 if ( reload_root ) {
856 strcpy(currdir, "/");
857 tc.dirlevel = 0;
858 #ifdef HAVE_TAGCACHE
859 tc.currtable = 0;
860 tc.currextra = 0;
861 lasttable = -1;
862 lastextra = -1;
863 #endif
864 reload_root = false;
867 if (! reload_dir )
869 gui_synclist_select_item(&tree_lists, 0);
870 gui_synclist_draw(&tree_lists);
871 tc.selected_item = 0;
872 lastdir[0] = 0;
875 lastfilter = *tc.dirfilter;
876 lastsortcase = global_settings.sort_case;
877 restore = true;
880 if (exit_func)
881 return GO_TO_PREVIOUS;
883 if (restore || reload_dir) {
884 /* restore display */
885 numentries = update_dir();
886 reload_dir = false;
887 if (currdir[1] && (numentries < 0))
888 { /* not in root and reload failed */
889 reload_root = true; /* try root */
890 goto check_rescan;
894 return true;
897 static int plsize = 0;
898 static long pltick;
899 static bool add_dir(char* dirname, int len, int fd)
901 bool abort = false;
902 DIR* dir;
904 /* check for user abort */
905 if (action_userabort(TIMEOUT_NOBLOCK))
906 return true;
908 dir = opendir(dirname);
909 if(!dir)
910 return true;
912 while (true) {
913 struct dirent *entry;
915 entry = readdir(dir);
916 if (!entry)
917 break;
918 if (entry->attribute & ATTR_DIRECTORY) {
919 int dirlen = strlen(dirname);
920 bool result;
922 if (!strcmp((char *)entry->d_name, ".") ||
923 !strcmp((char *)entry->d_name, ".."))
924 continue;
926 if (dirname[1])
927 snprintf(dirname+dirlen, len-dirlen, "/%s", entry->d_name);
928 else
929 snprintf(dirname, len, "/%s", entry->d_name);
931 result = add_dir(dirname, len, fd);
932 dirname[dirlen] = '\0';
933 if (result) {
934 abort = true;
935 break;
938 else {
939 int x = strlen((char *)entry->d_name);
940 int i;
941 char *cp = strrchr((char *)entry->d_name,'.');
943 if (cp) {
944 cp++;
946 /* add all supported audio files to playlists */
947 for (i=0; i < filetypes_count; i++) {
948 if (filetypes[i].tree_attr == FILE_ATTR_AUDIO) {
949 if (!strcasecmp(cp, filetypes[i].extension)) {
950 char buf[8];
951 int i;
952 write(fd, dirname, strlen(dirname));
953 write(fd, "/", 1);
954 write(fd, entry->d_name, x);
955 write(fd, "\n", 1);
957 plsize++;
958 if(TIME_AFTER(current_tick, pltick+HZ/4)) {
959 pltick = current_tick;
961 snprintf(buf, sizeof buf, "%d", plsize);
962 #ifdef HAVE_LCD_BITMAP
963 FOR_NB_SCREENS(i)
965 screens[i].puts(0, 4, (unsigned char *)buf);
966 gui_textarea_update(&screens[i]);
968 #else
969 x = 10;
970 if (plsize > 999)
971 x=7;
972 else {
973 if (plsize > 99)
974 x=8;
975 else {
976 if (plsize > 9)
977 x=9;
980 FOR_NB_SCREENS(i) {
981 screens[i].puts(x,0,buf);
983 #endif
985 break;
992 closedir(dir);
994 return abort;
997 bool create_playlist(void)
999 int fd;
1000 int i;
1001 char filename[MAX_PATH];
1003 pltick = current_tick;
1005 snprintf(filename, sizeof filename, "%s.m3u8",
1006 tc.currdir[1] ? tc.currdir : "/root");
1007 FOR_NB_SCREENS(i)
1009 gui_textarea_clear(&screens[i]);
1010 screens[i].puts(0, 0, str(LANG_CREATING));
1011 screens[i].puts_scroll(0, 1, (unsigned char *)filename);
1012 #if defined(HAVE_LCD_BITMAP) || defined(SIMULATOR)
1013 gui_textarea_update(&screens[i]);
1014 #endif
1016 fd = creat(filename);
1017 if (fd < 0)
1018 return false;
1020 trigger_cpu_boost();
1022 snprintf(filename, sizeof(filename), "%s",
1023 tc.currdir[1] ? tc.currdir : "/");
1024 plsize = 0;
1025 add_dir(filename, sizeof(filename), fd);
1026 close(fd);
1028 sleep(HZ);
1030 return true;
1033 int rockbox_browse(const char *root, int dirfilter)
1035 int ret_val = 0;
1036 int *last_filter = tc.dirfilter;
1037 tc.dirfilter = &dirfilter;
1039 reload_dir = true;
1040 if (dirfilter >= NUM_FILTER_MODES)
1042 static struct tree_context backup;
1043 int last_context;
1045 backup = tc;
1046 tc.selected_item = 0;
1047 tc.dirlevel = 0;
1048 memcpy(tc.currdir, root, sizeof(tc.currdir));
1049 start_wps = false;
1050 last_context = curr_context;
1052 ret_val = dirbrowse();
1053 tc = backup;
1054 curr_context = last_context;
1056 else
1058 static char buf[MAX_PATH];
1059 if (dirfilter != SHOW_ID3DB)
1060 tc.dirfilter = &global_settings.dirfilter;
1061 strcpy(buf,root);
1062 set_current_file(buf);
1063 ret_val = dirbrowse();
1065 tc.dirfilter = last_filter;
1066 return ret_val;
1069 void tree_mem_init(void)
1071 /* We copy the settings value in case it is changed by the user. We can't
1072 use it until the next reboot. */
1073 max_files = global_settings.max_files_in_dir;
1075 /* initialize tree context struct */
1076 memset(&tc, 0, sizeof(tc));
1077 tc.dirfilter = &global_settings.dirfilter;
1079 tc.name_buffer_size = AVERAGE_FILENAME_LENGTH * max_files;
1080 tc.name_buffer = buffer_alloc(tc.name_buffer_size);
1082 tc.dircache_size = max_files * sizeof(struct entry);
1083 tc.dircache = buffer_alloc(tc.dircache_size);
1084 tree_get_filetypes(&filetypes, &filetypes_count);
1087 void bookmark_play(char *resume_file, int index, int offset, int seed,
1088 char *filename)
1090 int i;
1091 char* suffix = strrchr(resume_file, '.');
1093 if (suffix != NULL &&
1094 (!strcasecmp(suffix, ".m3u") || !strcasecmp(suffix, ".m3u8")))
1096 /* Playlist playback */
1097 char* slash;
1098 /* check that the file exists */
1099 int fd = open(resume_file, O_RDONLY);
1100 if(fd<0)
1101 return;
1102 close(fd);
1104 slash = strrchr(resume_file,'/');
1105 if (slash)
1107 char* cp;
1108 *slash=0;
1110 cp=resume_file;
1111 if (!cp[0])
1112 cp="/";
1114 if (playlist_create(cp, slash+1) != -1)
1116 if (global_settings.playlist_shuffle)
1117 playlist_shuffle(seed, -1);
1118 playlist_start(index,offset);
1120 *slash='/';
1123 else
1125 /* Directory playback */
1126 lastdir[0]='\0';
1127 if (playlist_create(resume_file, NULL) != -1)
1129 char* peek_filename;
1130 resume_directory(resume_file);
1131 if (global_settings.playlist_shuffle)
1132 playlist_shuffle(seed, -1);
1134 /* Check if the file is at the same spot in the directory,
1135 else search for it */
1136 peek_filename = playlist_peek(index);
1138 if (peek_filename == NULL)
1139 return;
1141 if (strcmp(strrchr(peek_filename, '/') + 1, filename))
1143 for ( i=0; i < playlist_amount(); i++ )
1145 peek_filename = playlist_peek(i);
1147 if (peek_filename == NULL)
1148 return;
1150 if (!strcmp(strrchr(peek_filename, '/') + 1, filename))
1151 break;
1153 if (i < playlist_amount())
1154 index = i;
1155 else
1156 return;
1158 playlist_start(index,offset);
1162 start_wps=true;
1165 static void say_filetype(int attr)
1167 /* try to find a voice ID for the extension, if known */
1168 int j;
1169 attr &= FILE_ATTR_MASK; /* file type */
1170 for (j=0; j<filetypes_count; j++)
1171 if (attr == filetypes[j].tree_attr)
1173 talk_id(filetypes[j].voiceclip, true);
1174 return;
1178 static int ft_play_dirname(char* name)
1180 int fd;
1181 char dirname_mp3_filename[MAX_PATH+1];
1183 #if CONFIG_CODEC != SWCODEC
1184 if (audio_status() & AUDIO_STATUS_PLAY)
1185 return 0;
1186 #endif
1188 snprintf(dirname_mp3_filename, sizeof(dirname_mp3_filename), "%s/%s/%s",
1189 tc.currdir[1] ? tc.currdir : "" , name,
1190 dir_thumbnail_name);
1192 DEBUGF("Checking for %s\n", dirname_mp3_filename);
1194 fd = open(dirname_mp3_filename, O_RDONLY);
1195 if (fd < 0)
1197 DEBUGF("Failed to find: %s\n", dirname_mp3_filename);
1198 return -1;
1201 close(fd);
1203 DEBUGF("Found: %s\n", dirname_mp3_filename);
1205 talk_file(dirname_mp3_filename, false);
1206 return 1;
1209 static void ft_play_filename(char *dir, char *file)
1211 char name_mp3_filename[MAX_PATH+1];
1213 #if CONFIG_CODEC != SWCODEC
1214 if (audio_status() & AUDIO_STATUS_PLAY)
1215 return;
1216 #endif
1218 if (strcasecmp(&file[strlen(file) - strlen(file_thumbnail_ext)],
1219 file_thumbnail_ext))
1220 { /* file has no .talk extension */
1221 snprintf(name_mp3_filename, sizeof(name_mp3_filename),
1222 "%s/%s%s", dir, file, file_thumbnail_ext);
1224 talk_file(name_mp3_filename, false);
1226 else
1227 { /* it already is a .talk file, play this directly */
1228 snprintf(name_mp3_filename, sizeof(name_mp3_filename),
1229 "%s/%s", dir, file);
1230 talk_id(LANG_VOICE_DIR_HOVER, false); /* prefix it */
1231 talk_file(name_mp3_filename, true);
1235 /* These two functions are called by the USB and shutdown handlers */
1236 void tree_flush(void)
1238 scrobbler_shutdown();
1239 #ifdef HAVE_TAGCACHE
1240 tagcache_shutdown();
1241 #endif
1242 playlist_shutdown();
1244 #ifdef HAVE_TC_RAMCACHE
1245 tagcache_unload_ramcache();
1246 #endif
1248 #ifdef HAVE_DIRCACHE
1250 int old_val = global_status.dircache_size;
1251 if (global_settings.dircache)
1253 if (!dircache_is_initializing())
1254 global_status.dircache_size = dircache_get_cache_size();
1255 # ifdef HAVE_EEPROM_SETTINGS
1256 if (firmware_settings.initialized)
1257 dircache_save();
1258 # endif
1259 dircache_disable();
1261 else
1263 global_status.dircache_size = 0;
1265 if (old_val != global_status.dircache_size)
1266 status_save();
1268 #endif
1271 void tree_restore(void)
1273 #ifdef HAVE_EEPROM_SETTINGS
1274 firmware_settings.disk_clean = false;
1275 #endif
1277 #ifdef HAVE_TC_RAMCACHE
1278 remove(TAGCACHE_STATEFILE);
1279 #endif
1281 #ifdef HAVE_DIRCACHE
1282 remove(DIRCACHE_FILE);
1283 if (global_settings.dircache)
1285 /* Print "Scanning disk..." to the display. */
1286 int i;
1287 FOR_NB_SCREENS(i)
1289 screens[i].putsxy((LCD_WIDTH/2) -
1290 ((strlen(str(LANG_SCANNING_DISK)) *
1291 screens[i].char_width)/2),
1292 LCD_HEIGHT-screens[i].char_height*3,
1293 str(LANG_SCANNING_DISK));
1294 gui_textarea_update(&screens[i]);
1296 cond_talk_ids_fq(LANG_SCANNING_DISK);
1298 dircache_build(global_status.dircache_size);
1300 /* Clean the text when we are done. */
1301 FOR_NB_SCREENS(i)
1303 gui_textarea_clear(&screens[i]);
1306 #endif
1307 #ifdef HAVE_TAGCACHE
1308 tagcache_start_scan();
1309 #endif
1310 scrobbler_init();