oops, removed a bit too much.
[Rockbox.git] / apps / tree.c
blob0b4ea95c41652d59cbaf5b964d4f150541448c0d
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);
118 static char * tree_get_filename(int selected_item, void * data, char *buffer)
120 struct tree_context * local_tc=(struct tree_context *)data;
121 char *name;
122 int attr=0;
123 bool stripit = false;
124 #ifdef HAVE_TAGCACHE
125 bool id3db = *(local_tc->dirfilter) == SHOW_ID3DB;
127 if (id3db)
129 return tagtree_get_entry(&tc, selected_item)->name;
131 else
132 #endif
134 struct entry* dc = local_tc->dircache;
135 struct entry* e = &dc[selected_item];
136 name = e->name;
137 attr = e->attr;
140 if(!(attr & ATTR_DIRECTORY))
142 switch(global_settings.show_filename_ext)
144 case 0:
145 /* show file extension: off */
146 stripit = true;
147 break;
148 case 1:
149 /* show file extension: on */
150 break;
151 case 2:
152 /* show file extension: only unknown types */
153 stripit = filetype_supported(attr);
154 break;
155 case 3:
156 default:
157 /* show file extension: only when viewing all */
158 stripit = (*(local_tc->dirfilter) != SHOW_ID3DB) &&
159 (*(local_tc->dirfilter) != SHOW_ALL);
160 break;
164 if(stripit)
166 return(strip_extension(buffer, MAX_PATH, name));
168 return(name);
171 #ifdef HAVE_LCD_COLOR
172 static int tree_get_filecolor(int selected_item, void * data)
174 if (*tc.dirfilter == SHOW_ID3DB)
175 return -1;
176 struct tree_context * local_tc=(struct tree_context *)data;
177 struct entry* dc = local_tc->dircache;
178 struct entry* e = &dc[selected_item];
179 return filetype_get_color(e->name, e->attr);
181 #endif
183 static int tree_get_fileicon(int selected_item, void * data)
185 struct tree_context * local_tc=(struct tree_context *)data;
186 #ifdef HAVE_TAGCACHE
187 bool id3db = *(local_tc->dirfilter) == SHOW_ID3DB;
188 if (id3db) {
189 return tagtree_get_icon(&tc);
191 else
192 #endif
194 struct entry* dc = local_tc->dircache;
195 struct entry* e = &dc[selected_item];
196 return filetype_get_icon(e->attr);
200 static int tree_voice_cb(int selected_item, void * data)
202 struct tree_context * local_tc=(struct tree_context *)data;
203 char *name;
204 int attr=0;
205 #ifdef HAVE_TAGCACHE
206 bool id3db = *(local_tc->dirfilter) == SHOW_ID3DB;
208 if (id3db)
210 attr = tagtree_get_attr(local_tc);
211 name = tagtree_get_entry(local_tc, selected_item)->name;
213 else
214 #endif
216 struct entry* dc = local_tc->dircache;
217 struct entry* e = &dc[selected_item];
218 name = e->name;
219 attr = e->attr;
221 bool is_dir = (attr & ATTR_DIRECTORY);
222 bool did_clip = false;
223 /* First the .talk clip case */
224 if(is_dir)
226 if(global_settings.talk_dir_clip)
228 DEBUGF("Playing directory thumbnail: %s", local_tc->currdir);
229 did_clip = true;
230 if(ft_play_dirname(name) <0)
231 /* failed, not existing */
232 did_clip = false;
234 } else { /* it's a file */
235 if (global_settings.talk_file_clip && (attr & FILE_ATTR_THUMBNAIL))
237 did_clip = true;
238 DEBUGF("Playing file thumbnail: %s/%s%s\n",
239 local_tc->currdir, name, file_thumbnail_ext);
240 ft_play_filename(local_tc->currdir, name);
243 if(!did_clip)
245 /* say the number or spell if required or as a fallback */
246 switch (is_dir ? global_settings.talk_dir : global_settings.talk_file)
248 case 1: /* as numbers */
249 talk_id(is_dir ? VOICE_DIR : VOICE_FILE, false);
250 talk_number(selected_item+1 - (is_dir ? 0 : local_tc->dirsindir),
251 true);
252 if(global_settings.talk_filetype
253 && !is_dir && *local_tc->dirfilter < NUM_FILTER_MODES)
254 say_filetype(attr);
255 break;
256 case 2: /* spelled */
257 talk_shutup();
258 if(global_settings.talk_filetype)
260 if(is_dir)
261 talk_id(VOICE_DIR, true);
262 else if(*local_tc->dirfilter < NUM_FILTER_MODES)
263 say_filetype(attr);
265 talk_spell(name, true);
266 break;
269 return 0;
272 bool check_rockboxdir(void)
274 DIR *dir = opendir(ROCKBOX_DIR);
275 if(!dir)
276 { /* No need to localise this message.
277 If .rockbox is missing, it wouldn't work anyway */
278 int i;
279 FOR_NB_SCREENS(i)
280 screens[i].clear_display();
281 gui_syncsplash(HZ*2, "No .rockbox directory");
282 FOR_NB_SCREENS(i)
283 screens[i].clear_display();
284 gui_syncsplash(HZ*2, "Installation incomplete");
285 return false;
287 closedir(dir);
288 return true;
291 /* do this really late in the init sequence */
292 void tree_gui_init(void)
294 gui_sync_wps_screen_init();
296 check_rockboxdir();
298 strcpy(tc.currdir, "/");
300 #ifdef HAVE_LCD_CHARCELLS
301 int i;
302 FOR_NB_SCREENS(i)
303 screens[i].double_height(false);
304 #endif
305 #ifdef HAS_BUTTONBAR
306 gui_buttonbar_init(&tree_buttonbar);
307 /* since archos only have one screen, no need to create more than that */
308 gui_buttonbar_set_display(&tree_buttonbar, &(screens[SCREEN_MAIN]) );
309 #endif
310 gui_synclist_init(&tree_lists, &tree_get_filename, &tc, false, 1);
311 gui_synclist_set_voice_callback(&tree_lists, tree_voice_cb);
312 gui_synclist_set_icon_callback(&tree_lists, &tree_get_fileicon);
313 #ifdef HAVE_LCD_COLOR
314 gui_list_set_color_callback(&tree_lists,
315 &tree_get_filecolor);
316 #endif
321 struct tree_context* tree_get_context(void)
323 return &tc;
327 * Returns the position of a given file in the current directory
328 * returns -1 if not found
330 static int tree_get_file_position(char * filename)
332 int i;
334 /* use lastfile to determine the selected item (default=0) */
335 for (i=0; i < tc.filesindir; i++)
337 struct entry* dc = tc.dircache;
338 struct entry* e = &dc[i];
339 if (!strcasecmp(e->name, filename))
340 return(i);
342 return(-1);/* no file can match, returns undefined */
346 * Called when a new dir is loaded (for example when returning from other apps ...)
347 * also completely redraws the tree
349 static int update_dir(void)
351 bool changed = false;
352 #ifdef HAVE_TAGCACHE
353 bool id3db = *tc.dirfilter == SHOW_ID3DB;
354 /* Checks for changes */
355 if (id3db) {
356 if (tc.currtable != lasttable ||
357 tc.currextra != lastextra ||
358 tc.firstpos != lastfirstpos ||
359 reload_dir)
361 if (tagtree_load(&tc) < 0)
362 return -1;
364 lasttable = tc.currtable;
365 lastextra = tc.currextra;
366 lastfirstpos = tc.firstpos;
367 changed = true;
370 else
371 #endif
373 /* if the tc.currdir has been changed, reload it ...*/
374 if (strncmp(tc.currdir, lastdir, sizeof(lastdir)) || reload_dir)
376 if (ft_load(&tc, NULL) < 0)
377 return -1;
378 strcpy(lastdir, tc.currdir);
379 changed = true;
382 /* if selected item is undefined */
383 if (tc.selected_item == -1)
385 /* use lastfile to determine the selected item */
386 tc.selected_item = tree_get_file_position(lastfile);
388 /* If the file doesn't exists, select the first one (default) */
389 if(tc.selected_item < 0)
390 tc.selected_item = 0;
391 changed = true;
393 if (changed)
396 #ifdef HAVE_TAGCACHE
397 !id3db &&
398 #endif
399 (tc.dirfull || tc.filesindir == global_settings.max_files_in_dir) )
401 gui_syncsplash(HZ, ID2P(LANG_SHOWDIR_BUFFER_FULL));
404 #ifdef HAVE_TAGCACHE
405 if (id3db)
407 #ifdef HAVE_LCD_BITMAP
408 if (global_settings.show_path_in_browser == SHOW_PATH_FULL
409 || global_settings.show_path_in_browser == SHOW_PATH_CURRENT)
411 gui_synclist_set_title(&tree_lists, tagtree_get_title(&tc),
412 filetype_get_icon(ATTR_DIRECTORY));
414 else
416 /* Must clear the title as the list is reused */
417 gui_synclist_set_title(&tree_lists, NULL, NOICON);
419 #endif
421 else
422 #endif
424 #ifdef HAVE_LCD_BITMAP
425 if (global_settings.show_path_in_browser &&
426 *(tc.dirfilter) == SHOW_PLUGINS)
428 char *title;
429 if (!strcmp(tc.currdir, PLUGIN_GAMES_DIR))
430 title = str(LANG_PLUGIN_GAMES);
431 else if (!strcmp(tc.currdir, PLUGIN_APPS_DIR))
432 title = str(LANG_PLUGIN_APPS);
433 else if (!strcmp(tc.currdir, PLUGIN_DEMOS_DIR))
434 title = str(LANG_PLUGIN_DEMOS);
435 else title = str(LANG_PLUGINS);
436 gui_synclist_set_title(&tree_lists, title, Icon_Plugin);
438 else if (global_settings.show_path_in_browser == SHOW_PATH_FULL)
440 gui_synclist_set_title(&tree_lists, tc.currdir,
441 filetype_get_icon(ATTR_DIRECTORY));
443 else if (global_settings.show_path_in_browser == SHOW_PATH_CURRENT)
445 char *title = strrchr(tc.currdir, '/') + 1;
446 if (*title == '\0')
448 /* Display "Files" for the root dir */
449 gui_synclist_set_title(&tree_lists, str(LANG_DIR_BROWSER),
450 filetype_get_icon(ATTR_DIRECTORY));
452 else
453 gui_synclist_set_title(&tree_lists, title,
454 filetype_get_icon(ATTR_DIRECTORY));
456 else
458 /* Must clear the title as the list is reused */
459 gui_synclist_set_title(&tree_lists, NULL, NOICON);
461 #endif
464 gui_synclist_set_nb_items(&tree_lists, tc.filesindir);
465 gui_synclist_set_icon_callback(&tree_lists, tree_get_fileicon);
466 if( tc.selected_item >= tc.filesindir)
467 tc.selected_item=tc.filesindir-1;
469 gui_synclist_select_item(&tree_lists, tc.selected_item);
470 #ifdef HAS_BUTTONBAR
471 if (global_settings.buttonbar) {
472 if (*tc.dirfilter < NUM_FILTER_MODES)
473 gui_buttonbar_set(&tree_buttonbar, str(LANG_SYSFONT_DIRBROWSE_F1),
474 str(LANG_SYSFONT_DIRBROWSE_F2),
475 str(LANG_SYSFONT_DIRBROWSE_F3));
476 else
477 gui_buttonbar_set(&tree_buttonbar, "<<<", "", "");
478 gui_buttonbar_draw(&tree_buttonbar);
480 #endif
481 gui_synclist_draw(&tree_lists);
482 gui_synclist_speak_item(&tree_lists);
483 gui_syncstatusbar_draw(&statusbars, true);
484 return tc.filesindir;
487 /* load tracks from specified directory to resume play */
488 void resume_directory(const char *dir)
490 #ifdef HAVE_TAGCACHE
491 bool id3db = *tc.dirfilter == SHOW_ID3DB;
492 #endif
494 if (ft_load(&tc, dir) < 0)
495 return;
496 lastdir[0] = 0;
498 ft_build_playlist(&tc, 0);
500 #ifdef HAVE_TAGCACHE
501 if (id3db)
502 tagtree_load(&tc);
503 #endif
506 /* Returns the current working directory and also writes cwd to buf if
507 non-NULL. In case of error, returns NULL. */
508 char *getcwd(char *buf, int size)
510 if (!buf)
511 return tc.currdir;
512 else if (size > 0)
514 strncpy(buf, tc.currdir, size);
515 return buf;
517 else
518 return NULL;
521 /* Force a reload of the directory next time directory browser is called */
522 void reload_directory(void)
524 reload_dir = true;
527 void get_current_file(char* buffer, int buffer_len)
529 #ifdef HAVE_TAGCACHE
530 /* in ID3DB mode it is a bad idea to call this function */
531 /* (only happens with `follow playlist') */
532 if( *tc.dirfilter == SHOW_ID3DB )
533 return;
534 #endif
536 struct entry* dc = tc.dircache;
537 struct entry* e = &dc[tc.selected_item];
538 snprintf(buffer, buffer_len, "%s/%s", getcwd(NULL,0),
539 tc.dirlength ? e->name : "");
542 /* Allow apps to change our dirfilter directly (required for sub browsers)
543 if they're suddenly going to become a file browser for example */
544 void set_dirfilter(int l_dirfilter)
546 *tc.dirfilter = l_dirfilter;
549 /* Selects a file and update tree context properly */
550 void set_current_file(char *path)
552 char *name;
553 int i;
555 #ifdef HAVE_TAGCACHE
556 /* in ID3DB mode it is a bad idea to call this function */
557 /* (only happens with `follow playlist') */
558 if( *tc.dirfilter == SHOW_ID3DB )
559 return;
560 #endif
562 /* separate directory from filename */
563 /* gets the directory's name and put it into tc.currdir */
564 name = strrchr(path+1,'/');
565 if (name)
567 *name = 0;
568 strcpy(tc.currdir, path);
569 *name = '/';
570 name++;
572 else
574 strcpy(tc.currdir, "/");
575 name = path+1;
578 strcpy(lastfile, name);
581 /* If we changed dir we must recalculate the dirlevel
582 and adjust the selected history properly */
583 if (strncmp(tc.currdir,lastdir,sizeof(lastdir)))
585 tc.dirlevel = 0;
586 tc.selected_item_history[tc.dirlevel] = -1;
588 /* use '/' to calculate dirlevel */
589 for (i = 1; path[i] != '\0'; i++)
591 if (path[i] == '/')
593 tc.dirlevel++;
594 tc.selected_item_history[tc.dirlevel] = -1;
598 if (ft_load(&tc, NULL) >= 0)
600 tc.selected_item = tree_get_file_position(lastfile);
605 /* main loop, handles key events */
606 static int dirbrowse()
608 int numentries=0;
609 char buf[MAX_PATH];
610 unsigned button, oldbutton;
611 bool reload_root = false;
612 int lastfilter = *tc.dirfilter;
613 bool lastsortcase = global_settings.sort_case;
614 bool exit_func = false;
616 char* currdir = tc.currdir; /* just a shortcut */
617 #ifdef HAVE_TAGCACHE
618 bool id3db = *tc.dirfilter == SHOW_ID3DB;
620 if (id3db)
621 curr_context=CONTEXT_ID3DB;
622 else
623 #endif
624 curr_context=CONTEXT_TREE;
625 if (tc.selected_item < 0)
626 tc.selected_item = 0;
627 #ifdef HAVE_TAGCACHE
628 tc.firstpos = 0;
629 lasttable = -1;
630 lastextra = -1;
631 lastfirstpos = 0;
632 #endif
634 start_wps = false;
635 numentries = update_dir();
636 reload_dir = false;
637 if (numentries == -1)
638 return GO_TO_PREVIOUS; /* currdir is not a directory */
640 if (*tc.dirfilter > NUM_FILTER_MODES && numentries==0)
642 gui_syncsplash(HZ*2, ID2P(LANG_NO_FILES));
643 return GO_TO_PREVIOUS; /* No files found for rockbox_browser() */
646 while(1) {
647 struct entry *dircache = tc.dircache;
648 bool restore = false;
649 if (tc.dirlevel < 0)
650 tc.dirlevel = 0; /* shouldnt be needed.. this code needs work! */
651 #ifdef BOOTFILE
652 if (boot_changed) {
653 char *lines[]={ID2P(LANG_BOOT_CHANGED), ID2P(LANG_REBOOT_NOW)};
654 struct text_message message={lines, 2};
655 if(gui_syncyesno_run(&message, NULL, NULL)==YESNO_YES)
656 rolo_load("/" BOOTFILE);
657 restore = true;
658 boot_changed = false;
660 #endif
661 button = get_action(CONTEXT_TREE,
662 list_do_action_timeout(&tree_lists, HZ/2));
663 oldbutton = button;
664 gui_synclist_do_button(&tree_lists, &button,LIST_WRAP_UNLESS_HELD);
665 tc.selected_item = gui_synclist_get_sel_pos(&tree_lists);
666 switch ( button ) {
667 case ACTION_STD_OK:
668 /* nothing to do if no files to display */
669 if ( numentries == 0 )
670 break;
672 #ifdef HAVE_TAGCACHE
673 switch (id3db?tagtree_enter(&tc):ft_enter(&tc))
674 #else
675 switch (ft_enter(&tc))
676 #endif
678 case 1: reload_dir = true; break;
679 case 2: start_wps = true; break;
680 case 3: exit_func = true; break;
681 default: break;
683 restore = true;
684 break;
686 case ACTION_STD_CANCEL:
687 if (*tc.dirfilter > NUM_FILTER_MODES && tc.dirlevel < 1) {
688 exit_func = true;
689 break;
691 if ((*tc.dirfilter == SHOW_ID3DB && tc.dirlevel == 0) ||
692 ((*tc.dirfilter != SHOW_ID3DB && !strcmp(currdir,"/"))))
694 #ifdef HAVE_LCD_BITMAP /* charcell doesnt have ACTION_TREE_PGLEFT so this isnt needed */
695 if (oldbutton == ACTION_TREE_PGLEFT)
696 break;
697 else
698 #endif
699 return GO_TO_ROOT;
702 #ifdef HAVE_TAGCACHE
703 if (id3db)
704 tagtree_exit(&tc);
705 else
706 #endif
707 if (ft_exit(&tc) == 3)
708 exit_func = true;
710 restore = true;
711 break;
713 case ACTION_TREE_STOP:
714 if (list_stop_handler())
715 restore = true;
716 break;
718 case ACTION_STD_MENU:
719 return GO_TO_ROOT;
720 break;
722 #ifdef HAVE_RECORDING
723 case ACTION_STD_REC:
724 return GO_TO_RECSCREEN;
725 #endif
727 case ACTION_TREE_WPS:
728 return GO_TO_PREVIOUS_MUSIC;
729 break;
730 #ifdef HAVE_QUICKSCREEN
731 case ACTION_STD_QUICKSCREEN:
732 /* don't enter f2 from plugin browser */
733 if (*tc.dirfilter < NUM_FILTER_MODES)
735 if (quick_screen_quick(button))
736 reload_dir = true;
737 restore = true;
739 break;
740 #endif
741 #ifdef BUTTON_F3
742 case ACTION_F3:
743 /* don't enter f3 from plugin browser */
744 if (*tc.dirfilter < NUM_FILTER_MODES)
746 if (quick_screen_f3(BUTTON_F3))
747 reload_dir = true;
748 restore = true;
750 break;
751 #endif
753 case ACTION_STD_CONTEXT:
755 int onplay_result;
756 int attr = 0;
758 if(!numentries)
759 onplay_result = onplay(NULL, 0, curr_context);
760 else {
761 #ifdef HAVE_TAGCACHE
762 if (id3db)
764 if (tagtree_get_attr(&tc) == FILE_ATTR_AUDIO)
766 attr = FILE_ATTR_AUDIO;
767 tagtree_get_filename(&tc, buf, sizeof(buf));
769 else
770 attr = ATTR_DIRECTORY;
772 else
773 #endif
775 attr = dircache[tc.selected_item].attr;
777 if (currdir[1]) /* Not in / */
778 snprintf(buf, sizeof buf, "%s/%s",
779 currdir,
780 dircache[tc.selected_item].name);
781 else /* In / */
782 snprintf(buf, sizeof buf, "/%s",
783 dircache[tc.selected_item].name);
785 onplay_result = onplay(buf, attr, curr_context);
787 switch (onplay_result)
789 case ONPLAY_MAINMENU:
790 return GO_TO_ROOT;
792 case ONPLAY_OK:
793 restore = true;
794 break;
796 case ONPLAY_RELOAD_DIR:
797 reload_dir = true;
798 break;
800 case ONPLAY_START_PLAY:
801 return GO_TO_WPS;
802 break;
804 break;
807 case ACTION_NONE:
808 gui_syncstatusbar_draw(&statusbars, false);
809 break;
811 #ifdef HAVE_HOTSWAP
812 case SYS_FS_CHANGED:
813 #ifdef HAVE_TAGCACHE
814 if (!id3db)
815 #endif
816 reload_dir = true;
817 /* The 'dir no longer valid' situation will be caught later
818 * by checking the showdir() result. */
819 break;
820 #endif
822 default:
823 if (default_event_handler(button) == SYS_USB_CONNECTED)
825 if(*tc.dirfilter > NUM_FILTER_MODES)
826 /* leave sub-browsers after usb, doing otherwise
827 might be confusing to the user */
828 exit_func = true;
829 else
830 reload_dir = true;
832 break;
834 if (start_wps)
835 return GO_TO_WPS;
836 if (button)
838 ata_spin();
842 check_rescan:
843 /* do we need to rescan dir? */
844 if (reload_dir || reload_root ||
845 lastfilter != *tc.dirfilter ||
846 lastsortcase != global_settings.sort_case)
848 if (reload_root) {
849 strcpy(currdir, "/");
850 tc.dirlevel = 0;
851 #ifdef HAVE_TAGCACHE
852 tc.currtable = 0;
853 tc.currextra = 0;
854 lasttable = -1;
855 lastextra = -1;
856 #endif
857 reload_root = false;
860 if (!reload_dir)
862 gui_synclist_select_item(&tree_lists, 0);
863 gui_synclist_draw(&tree_lists);
864 tc.selected_item = 0;
865 lastdir[0] = 0;
868 lastfilter = *tc.dirfilter;
869 lastsortcase = global_settings.sort_case;
870 restore = true;
873 if (exit_func)
874 return GO_TO_PREVIOUS;
876 if (restore || reload_dir) {
877 /* restore display */
878 numentries = update_dir();
879 reload_dir = false;
880 if (currdir[1] && (numentries < 0))
881 { /* not in root and reload failed */
882 reload_root = true; /* try root */
883 goto check_rescan;
887 return true;
890 static int plsize = 0;
891 static long pltick;
892 static bool add_dir(char* dirname, int len, int fd)
894 bool abort = false;
895 DIR* dir;
897 /* check for user abort */
898 if (action_userabort(TIMEOUT_NOBLOCK))
899 return true;
901 dir = opendir(dirname);
902 if(!dir)
903 return true;
905 while (true) {
906 struct dirent *entry;
908 entry = readdir(dir);
909 if (!entry)
910 break;
911 if (entry->attribute & ATTR_DIRECTORY) {
912 int dirlen = strlen(dirname);
913 bool result;
915 if (!strcmp((char *)entry->d_name, ".") ||
916 !strcmp((char *)entry->d_name, ".."))
917 continue;
919 if (dirname[1])
920 snprintf(dirname+dirlen, len-dirlen, "/%s", entry->d_name);
921 else
922 snprintf(dirname, len, "/%s", entry->d_name);
924 result = add_dir(dirname, len, fd);
925 dirname[dirlen] = '\0';
926 if (result) {
927 abort = true;
928 break;
931 else {
932 int x = strlen((char *)entry->d_name);
933 int i;
934 char *cp = strrchr((char *)entry->d_name,'.');
936 if (cp) {
937 cp++;
939 /* add all supported audio files to playlists */
940 for (i=0; i < filetypes_count; i++) {
941 if (filetypes[i].tree_attr == FILE_ATTR_AUDIO) {
942 if (!strcasecmp(cp, filetypes[i].extension)) {
943 char buf[8];
944 int i;
945 write(fd, dirname, strlen(dirname));
946 write(fd, "/", 1);
947 write(fd, entry->d_name, x);
948 write(fd, "\n", 1);
950 plsize++;
951 if(TIME_AFTER(current_tick, pltick+HZ/4)) {
952 pltick = current_tick;
954 snprintf(buf, sizeof buf, "%d", plsize);
955 #ifdef HAVE_LCD_BITMAP
956 FOR_NB_SCREENS(i)
958 screens[i].puts(0, 4, (unsigned char *)buf);
959 gui_textarea_update(&screens[i]);
961 #else
962 if (plsize > 999)
963 x=7;
964 else if (plsize > 99)
965 x=8;
966 else if (plsize > 9)
967 x=9;
968 else
969 x = 10;
971 FOR_NB_SCREENS(i) {
972 screens[i].puts(x,0,buf);
974 #endif
976 break;
983 closedir(dir);
985 return abort;
988 bool create_playlist(void)
990 int fd;
991 int i;
992 char filename[MAX_PATH];
994 pltick = current_tick;
996 snprintf(filename, sizeof filename, "%s.m3u8",
997 tc.currdir[1] ? tc.currdir : "/root");
998 FOR_NB_SCREENS(i)
1000 gui_textarea_clear(&screens[i]);
1001 screens[i].puts(0, 0, str(LANG_CREATING));
1002 screens[i].puts_scroll(0, 1, (unsigned char *)filename);
1003 #if defined(HAVE_LCD_BITMAP) || defined(SIMULATOR)
1004 gui_textarea_update(&screens[i]);
1005 #endif
1007 fd = creat(filename);
1008 if (fd < 0)
1009 return false;
1011 trigger_cpu_boost();
1013 snprintf(filename, sizeof(filename), "%s",
1014 tc.currdir[1] ? tc.currdir : "/");
1015 plsize = 0;
1016 add_dir(filename, sizeof(filename), fd);
1017 close(fd);
1019 cancel_cpu_boost();
1020 sleep(HZ);
1022 return true;
1025 int rockbox_browse(const char *root, int dirfilter)
1027 int ret_val = 0;
1028 int *last_filter = tc.dirfilter;
1029 tc.dirfilter = &dirfilter;
1031 reload_dir = true;
1032 if (dirfilter >= NUM_FILTER_MODES)
1034 static struct tree_context backup;
1035 int last_context;
1037 backup = tc;
1038 tc.selected_item = 0;
1039 tc.dirlevel = 0;
1040 memcpy(tc.currdir, root, sizeof(tc.currdir));
1041 start_wps = false;
1042 last_context = curr_context;
1044 ret_val = dirbrowse();
1045 tc = backup;
1046 curr_context = last_context;
1048 else
1050 static char buf[MAX_PATH];
1051 if (dirfilter != SHOW_ID3DB)
1052 tc.dirfilter = &global_settings.dirfilter;
1053 strcpy(buf,root);
1054 set_current_file(buf);
1055 ret_val = dirbrowse();
1057 tc.dirfilter = last_filter;
1058 return ret_val;
1061 void tree_mem_init(void)
1063 /* We copy the settings value in case it is changed by the user. We can't
1064 use it until the next reboot. */
1065 max_files = global_settings.max_files_in_dir;
1067 /* initialize tree context struct */
1068 memset(&tc, 0, sizeof(tc));
1069 tc.dirfilter = &global_settings.dirfilter;
1071 tc.name_buffer_size = AVERAGE_FILENAME_LENGTH * max_files;
1072 tc.name_buffer = buffer_alloc(tc.name_buffer_size);
1074 tc.dircache_size = max_files * sizeof(struct entry);
1075 tc.dircache = buffer_alloc(tc.dircache_size);
1076 tree_get_filetypes(&filetypes, &filetypes_count);
1079 bool bookmark_play(char *resume_file, int index, int offset, int seed,
1080 char *filename)
1082 int i;
1083 char* suffix = strrchr(resume_file, '.');
1084 bool started = false;
1086 if (suffix != NULL &&
1087 (!strcasecmp(suffix, ".m3u") || !strcasecmp(suffix, ".m3u8")))
1089 /* Playlist playback */
1090 char* slash;
1091 /* check that the file exists */
1092 int fd = open(resume_file, O_RDONLY);
1093 if(fd<0)
1094 return false;
1095 close(fd);
1097 slash = strrchr(resume_file,'/');
1098 if (slash)
1100 char* cp;
1101 *slash=0;
1103 cp=resume_file;
1104 if (!cp[0])
1105 cp="/";
1107 if (playlist_create(cp, slash+1) != -1)
1109 if (global_settings.playlist_shuffle)
1110 playlist_shuffle(seed, -1);
1111 playlist_start(index,offset);
1112 started = true;
1114 *slash='/';
1117 else
1119 /* Directory playback */
1120 lastdir[0]='\0';
1121 if (playlist_create(resume_file, NULL) != -1)
1123 char* peek_filename;
1124 resume_directory(resume_file);
1125 if (global_settings.playlist_shuffle)
1126 playlist_shuffle(seed, -1);
1128 /* Check if the file is at the same spot in the directory,
1129 else search for it */
1130 peek_filename = playlist_peek(index);
1132 if (peek_filename == NULL)
1133 return false;
1135 if (strcmp(strrchr(peek_filename, '/') + 1, filename))
1137 for ( i=0; i < playlist_amount(); i++ )
1139 peek_filename = playlist_peek(i);
1141 if (peek_filename == NULL)
1142 return false;
1144 if (!strcmp(strrchr(peek_filename, '/') + 1, filename))
1145 break;
1147 if (i < playlist_amount())
1148 index = i;
1149 else
1150 return false;
1152 playlist_start(index,offset);
1153 started = true;
1157 if (started)
1158 start_wps = true;
1159 return started;
1162 static void say_filetype(int attr)
1164 /* try to find a voice ID for the extension, if known */
1165 int j;
1166 attr &= FILE_ATTR_MASK; /* file type */
1167 for (j=0; j<filetypes_count; j++)
1168 if (attr == filetypes[j].tree_attr)
1170 talk_id(filetypes[j].voiceclip, true);
1171 return;
1175 static int ft_play_dirname(char* name)
1177 int fd;
1178 char dirname_mp3_filename[MAX_PATH+1];
1180 #if CONFIG_CODEC != SWCODEC
1181 if (audio_status() & AUDIO_STATUS_PLAY)
1182 return 0;
1183 #endif
1185 snprintf(dirname_mp3_filename, sizeof(dirname_mp3_filename), "%s/%s/%s",
1186 tc.currdir[1] ? tc.currdir : "" , name,
1187 dir_thumbnail_name);
1189 DEBUGF("Checking for %s\n", dirname_mp3_filename);
1191 fd = open(dirname_mp3_filename, O_RDONLY);
1192 if (fd < 0)
1194 DEBUGF("Failed to find: %s\n", dirname_mp3_filename);
1195 return -1;
1198 close(fd);
1200 DEBUGF("Found: %s\n", dirname_mp3_filename);
1202 talk_file(dirname_mp3_filename, false);
1203 if(global_settings.talk_filetype)
1204 talk_id(VOICE_DIR, true);
1205 return 1;
1208 static void ft_play_filename(char *dir, char *file)
1210 char name_mp3_filename[MAX_PATH+1];
1212 #if CONFIG_CODEC != SWCODEC
1213 if (audio_status() & AUDIO_STATUS_PLAY)
1214 return;
1215 #endif
1217 if (strcasecmp(&file[strlen(file) - strlen(file_thumbnail_ext)],
1218 file_thumbnail_ext))
1219 { /* file has no .talk extension */
1220 snprintf(name_mp3_filename, sizeof(name_mp3_filename),
1221 "%s/%s%s", dir, file, file_thumbnail_ext);
1223 talk_file(name_mp3_filename, false);
1225 else
1226 { /* it already is a .talk file, play this directly */
1227 snprintf(name_mp3_filename, sizeof(name_mp3_filename),
1228 "%s/%s", dir, file);
1229 talk_id(LANG_VOICE_DIR_HOVER, false); /* prefix it */
1230 talk_file(name_mp3_filename, true);
1234 /* These two functions are called by the USB and shutdown handlers */
1235 void tree_flush(void)
1237 scrobbler_shutdown();
1238 #ifdef HAVE_TAGCACHE
1239 tagcache_shutdown();
1240 #endif
1241 playlist_shutdown();
1243 #ifdef HAVE_TC_RAMCACHE
1244 tagcache_unload_ramcache();
1245 #endif
1247 #ifdef HAVE_DIRCACHE
1249 int old_val = global_status.dircache_size;
1250 if (global_settings.dircache)
1252 if (!dircache_is_initializing())
1253 global_status.dircache_size = dircache_get_cache_size();
1254 # ifdef HAVE_EEPROM_SETTINGS
1255 if (firmware_settings.initialized)
1256 dircache_save();
1257 # endif
1258 dircache_disable();
1260 else
1262 global_status.dircache_size = 0;
1264 if (old_val != global_status.dircache_size)
1265 status_save();
1267 #endif
1270 void tree_restore(void)
1272 #ifdef HAVE_EEPROM_SETTINGS
1273 firmware_settings.disk_clean = false;
1274 #endif
1276 #ifdef HAVE_TC_RAMCACHE
1277 remove(TAGCACHE_STATEFILE);
1278 #endif
1280 #ifdef HAVE_DIRCACHE
1281 remove(DIRCACHE_FILE);
1282 if (global_settings.dircache)
1284 /* Print "Scanning disk..." to the display. */
1285 int i;
1286 FOR_NB_SCREENS(i)
1288 screens[i].putsxy((LCD_WIDTH/2) -
1289 ((strlen(str(LANG_SCANNING_DISK)) *
1290 screens[i].char_width)/2),
1291 LCD_HEIGHT-screens[i].char_height*3,
1292 str(LANG_SCANNING_DISK));
1293 gui_textarea_update(&screens[i]);
1295 cond_talk_ids_fq(LANG_SCANNING_DISK);
1297 dircache_build(global_status.dircache_size);
1299 /* Clean the text when we are done. */
1300 FOR_NB_SCREENS(i)
1302 gui_textarea_clear(&screens[i]);
1305 #endif
1306 #ifdef HAVE_TAGCACHE
1307 tagcache_start_scan();
1308 #endif
1309 scrobbler_init();