rbutil: Add quessing of the Drive letter via Windows API, also restructuring for...
[kugel-rb.git] / apps / tree.c
blob1f76994968b3416e58a7f97230acba3a7a0119af
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 #ifdef HAVE_TAGCACHE
143 bool id3db = *(local_tc->dirfilter) == SHOW_ID3DB;
145 if (id3db)
147 return tagtree_get_entry(&tc, selected_item)->name;
149 else
150 #endif
152 struct entry* dc = local_tc->dircache;
153 struct entry* e = &dc[selected_item];
154 name = e->name;
155 attr = e->attr;
157 /* if any file filter is on, and if it's not a directory,
158 * strip the extension */
160 if ( (*(local_tc->dirfilter) != SHOW_ID3DB) && !(attr & ATTR_DIRECTORY)
161 && (*(local_tc->dirfilter) != SHOW_ALL) )
163 return(strip_extension(name, buffer));
165 return(name);
169 static int tree_get_fileicon(int selected_item, void * data)
171 struct tree_context * local_tc=(struct tree_context *)data;
172 #ifdef HAVE_TAGCACHE
173 bool id3db = *(local_tc->dirfilter) == SHOW_ID3DB;
174 if (id3db) {
175 return tagtree_get_icon(&tc);
177 else
178 #endif
180 struct entry* dc = local_tc->dircache;
181 struct entry* e = &dc[selected_item];
182 return filetype_get_icon(e->attr);
186 bool check_rockboxdir(void)
188 DIR *dir = opendir(ROCKBOX_DIR);
189 if(!dir)
190 { /* No need to localise this message.
191 If .rockbox is missing, it wouldn't work anyway */
192 int i;
193 FOR_NB_SCREENS(i)
194 screens[i].clear_display();
195 gui_syncsplash(HZ*2, "No .rockbox directory");
196 FOR_NB_SCREENS(i)
197 screens[i].clear_display();
198 gui_syncsplash(HZ*2, "Installation incomplete");
199 return false;
201 closedir(dir);
202 return true;
205 /* do this really late in the init sequence */
206 void tree_gui_init(void)
208 gui_sync_wps_screen_init();
210 check_rockboxdir();
212 strcpy(tc.currdir, "/");
214 #ifdef HAVE_LCD_CHARCELLS
215 int i;
216 FOR_NB_SCREENS(i)
217 screens[i].double_height(false);
218 #endif
219 #ifdef HAS_BUTTONBAR
220 gui_buttonbar_init(&tree_buttonbar);
221 /* since archos only have one screen, no need to create more than that */
222 gui_buttonbar_set_display(&tree_buttonbar, &(screens[SCREEN_MAIN]) );
223 #endif
224 gui_synclist_init(&tree_lists, &tree_get_filename, &tc, false, 1);
225 gui_synclist_set_icon_callback(&tree_lists, &tree_get_fileicon);
230 struct tree_context* tree_get_context(void)
232 return &tc;
235 /* talkbox hovering delay, to avoid immediate disk activity */
236 #define HOVER_DELAY (HZ/2)
238 * Returns the position of a given file in the current directory
239 * returns -1 if not found
241 static int tree_get_file_position(char * filename)
243 int i;
245 /* use lastfile to determine the selected item (default=0) */
246 for (i=0; i < tc.filesindir; i++)
248 struct entry* dc = tc.dircache;
249 struct entry* e = &dc[i];
250 if (!strcasecmp(e->name, filename))
251 return(i);
253 return(-1);/* no file can match, returns undefined */
257 * Called when a new dir is loaded (for example when returning from other apps ...)
258 * also completely redraws the tree
260 static int update_dir(void)
262 bool changed = false;
263 #ifdef HAVE_TAGCACHE
264 bool id3db = *tc.dirfilter == SHOW_ID3DB;
265 /* Checks for changes */
266 if (id3db) {
267 if (tc.currtable != lasttable ||
268 tc.currextra != lastextra ||
269 tc.firstpos != lastfirstpos ||
270 reload_dir)
272 if (tagtree_load(&tc) < 0)
273 return -1;
275 lasttable = tc.currtable;
276 lastextra = tc.currextra;
277 lastfirstpos = tc.firstpos;
278 changed = true;
281 else
282 #endif
284 /* if the tc.currdir has been changed, reload it ...*/
285 if (strncmp(tc.currdir, lastdir, sizeof(lastdir)) || reload_dir) {
287 if (ft_load(&tc, NULL) < 0)
288 return -1;
289 strcpy(lastdir, tc.currdir);
290 changed = true;
293 /* if selected item is undefined */
294 if (tc.selected_item == -1)
296 /* use lastfile to determine the selected item */
297 tc.selected_item = tree_get_file_position(lastfile);
299 /* If the file doesn't exists, select the first one (default) */
300 if(tc.selected_item < 0)
301 tc.selected_item = 0;
302 changed = true;
304 if (changed)
307 #ifdef HAVE_TAGCACHE
308 !id3db &&
309 #endif
310 (tc.dirfull ||
311 tc.filesindir == global_settings.max_files_in_dir) )
313 gui_syncsplash(HZ, str(LANG_SHOWDIR_BUFFER_FULL));
316 #ifdef HAVE_TAGCACHE
317 if (id3db)
319 if (global_settings.show_path_in_browser == SHOW_PATH_FULL
320 || global_settings.show_path_in_browser == SHOW_PATH_CURRENT)
322 gui_synclist_set_title(&tree_lists, tagtree_get_title(&tc),
323 filetype_get_icon(ATTR_DIRECTORY));
325 else
327 /* Must clear the title as the list is reused */
328 gui_synclist_set_title(&tree_lists, NULL, NOICON);
331 else
332 #endif
334 if (global_settings.show_path_in_browser == SHOW_PATH_FULL)
336 gui_synclist_set_title(&tree_lists, tc.currdir,
337 filetype_get_icon(ATTR_DIRECTORY));
339 else if (global_settings.show_path_in_browser == SHOW_PATH_CURRENT)
341 char *title = strrchr(tc.currdir, '/') + 1;
342 if (*title == '\0')
344 /* Display "Files" for the root dir */
345 gui_synclist_set_title(&tree_lists, str(LANG_DIR_BROWSER),
346 filetype_get_icon(ATTR_DIRECTORY));
348 else
349 gui_synclist_set_title(&tree_lists, title,
350 filetype_get_icon(ATTR_DIRECTORY));
352 else
354 /* Must clear the title as the list is reused */
355 gui_synclist_set_title(&tree_lists, NULL, NOICON);
359 gui_synclist_set_nb_items(&tree_lists, tc.filesindir);
360 gui_synclist_set_icon_callback(&tree_lists, tree_get_fileicon);
361 if( tc.selected_item >= tc.filesindir)
362 tc.selected_item=tc.filesindir-1;
364 gui_synclist_select_item(&tree_lists, tc.selected_item);
365 #ifdef HAS_BUTTONBAR
366 if (global_settings.buttonbar) {
367 if (*tc.dirfilter < NUM_FILTER_MODES)
368 gui_buttonbar_set(&tree_buttonbar, str(LANG_SYSFONT_DIRBROWSE_F1),
369 str(LANG_SYSFONT_DIRBROWSE_F2),
370 str(LANG_SYSFONT_DIRBROWSE_F3));
371 else
372 gui_buttonbar_set(&tree_buttonbar, "<<<", "", "");
373 gui_buttonbar_draw(&tree_buttonbar);
375 #endif
376 gui_synclist_draw(&tree_lists);
377 gui_syncstatusbar_draw(&statusbars, true);
378 return tc.filesindir;
381 /* load tracks from specified directory to resume play */
382 void resume_directory(const char *dir)
384 #ifdef HAVE_TAGCACHE
385 bool id3db = *tc.dirfilter == SHOW_ID3DB;
386 #endif
388 if (ft_load(&tc, dir) < 0)
389 return;
390 lastdir[0] = 0;
392 ft_build_playlist(&tc, 0);
394 #ifdef HAVE_TAGCACHE
395 if (id3db)
396 tagtree_load(&tc);
397 #endif
400 /* Returns the current working directory and also writes cwd to buf if
401 non-NULL. In case of error, returns NULL. */
402 char *getcwd(char *buf, int size)
404 if (!buf)
405 return tc.currdir;
406 else if (size > 0)
408 strncpy(buf, tc.currdir, size);
409 return buf;
411 else
412 return NULL;
415 /* Force a reload of the directory next time directory browser is called */
416 void reload_directory(void)
418 reload_dir = true;
421 void get_current_file(char* buffer, int buffer_len)
423 #ifdef HAVE_TAGCACHE
424 /* in ID3DB mode it is a bad idea to call this function */
425 /* (only happens with `follow playlist') */
426 if( *tc.dirfilter == SHOW_ID3DB )
427 return;
428 #endif
430 struct entry* dc = tc.dircache;
431 struct entry* e = &dc[tc.selected_item];
432 snprintf(buffer, buffer_len, "%s/%s", getcwd(NULL,0),
433 tc.dirlength ? e->name : "");
436 /* Selects a file and update tree context properly */
437 static void set_current_file(char *path)
439 char *name;
440 int i;
442 #ifdef HAVE_TAGCACHE
443 /* in ID3DB mode it is a bad idea to call this function */
444 /* (only happens with `follow playlist') */
445 if( *tc.dirfilter == SHOW_ID3DB )
446 return;
447 #endif
449 /* separate directory from filename */
450 /* gets the directory's name and put it into tc.currdir */
451 name = strrchr(path+1,'/');
452 if (name)
454 *name = 0;
455 strcpy(tc.currdir, path);
456 *name = '/';
457 name++;
459 else
461 strcpy(tc.currdir, "/");
462 name = path+1;
465 strcpy(lastfile, name);
468 /* If we changed dir we must recalculate the dirlevel
469 and adjust the selected history properly */
470 if (strncmp(tc.currdir,lastdir,sizeof(lastdir)))
472 tc.dirlevel = 0;
473 tc.selected_item_history[tc.dirlevel] = -1;
475 /* use '/' to calculate dirlevel */
476 for (i = 1; path[i] != '\0'; i++)
478 if (path[i] == '/')
480 tc.dirlevel++;
481 tc.selected_item_history[tc.dirlevel] = -1;
485 if (ft_load(&tc, NULL) >= 0)
487 tc.selected_item = tree_get_file_position(lastfile);
492 /* main loop, handles key events */
493 static int dirbrowse()
495 int numentries=0;
496 char buf[MAX_PATH];
497 int lasti = -1;
498 unsigned button, returned_button;
499 bool reload_root = false;
500 int lastfilter = *tc.dirfilter;
501 bool lastsortcase = global_settings.sort_case;
502 bool need_update = true;
503 bool exit_func = false;
504 long thumbnail_time = -1; /* for delaying a thumbnail */
505 long last_cancel = 0;
507 char* currdir = tc.currdir; /* just a shortcut */
508 #ifdef HAVE_TAGCACHE
509 bool id3db = *tc.dirfilter == SHOW_ID3DB;
511 if (id3db)
512 curr_context=CONTEXT_ID3DB;
513 else
514 #endif
515 curr_context=CONTEXT_TREE;
516 if (tc.selected_item < 0)
517 tc.selected_item = 0;
518 #ifdef HAVE_TAGCACHE
519 tc.firstpos=0;
520 lasttable = -1;
521 lastextra = -1;
522 lastfirstpos = 0;
523 #endif
525 start_wps = false;
526 numentries = update_dir();
527 if (numentries == -1)
528 return false; /* currdir is not a directory */
530 if (*tc.dirfilter > NUM_FILTER_MODES && numentries==0)
532 gui_syncsplash(HZ*2, str(LANG_NO_FILES));
533 return false; /* No files found for rockbox_browser() */
536 while(1) {
537 struct entry *dircache = tc.dircache;
538 bool restore = false;
539 if (tc.dirlevel < 0)
540 tc.dirlevel = 0; /* shouldnt be needed.. this code needs work! */
541 #ifdef BOOTFILE
542 if (boot_changed) {
543 char *lines[]={str(LANG_BOOT_CHANGED), str(LANG_REBOOT_NOW)};
544 struct text_message message={lines, 2};
545 if(gui_syncyesno_run(&message, NULL, NULL)==YESNO_YES)
546 rolo_load("/" BOOTFILE);
547 restore = true;
548 boot_changed = false;
550 #endif
551 button = get_action(CONTEXT_TREE,HZ/5);
552 returned_button = gui_synclist_do_button(&tree_lists, button,LIST_WRAP_UNLESS_HELD);
553 if (returned_button)
554 need_update = true;
555 if (returned_button == ACTION_STD_CANCEL)
556 button = ACTION_STD_CANCEL;
558 tc.selected_item = gui_synclist_get_sel_pos(&tree_lists);
559 switch ( button ) {
560 case ACTION_STD_OK:
561 /* nothing to do if no files to display */
562 if ( numentries == 0 )
563 break;
565 #ifdef HAVE_TAGCACHE
566 switch (id3db?tagtree_enter(&tc):ft_enter(&tc))
567 #else
568 switch (ft_enter(&tc))
569 #endif
571 case 1: reload_dir = true; break;
572 case 2: start_wps = true; break;
573 case 3: exit_func = true; break;
574 default: break;
576 restore = true;
577 break;
579 case ACTION_STD_CANCEL:
580 if (*tc.dirfilter > NUM_FILTER_MODES && tc.dirlevel < 1) {
581 exit_func = true;
582 break;
584 if ((*tc.dirfilter == SHOW_ID3DB && tc.dirlevel == 0) ||
585 ((*tc.dirfilter != SHOW_ID3DB && !strcmp(currdir,"/"))))
587 if (last_cancel && TIME_BEFORE(current_tick, last_cancel+HZ/2))
589 last_cancel = 0;
590 action_signalscreenchange(); /* eat the cancel presses */
591 break;
593 else
594 return GO_TO_ROOT;
596 last_cancel = current_tick;
598 #ifdef HAVE_TAGCACHE
599 if (id3db)
600 tagtree_exit(&tc);
601 else
602 #endif
603 if (ft_exit(&tc) == 3)
604 exit_func = true;
606 restore = true;
607 break;
609 case ACTION_TREE_STOP:
610 if (list_stop_handler())
611 restore = true;
612 break;
614 case ACTION_STD_MENU:
615 return GO_TO_ROOT;
616 break;
618 case ACTION_TREE_WPS:
619 return GO_TO_PREVIOUS_MUSIC;
620 break;
621 #ifdef HAVE_QUICKSCREEN
622 case ACTION_STD_QUICKSCREEN:
623 /* don't enter f2 from plugin browser */
624 if (*tc.dirfilter < NUM_FILTER_MODES)
626 if (quick_screen_quick(button))
627 reload_dir = true;
628 restore = true;
630 break;
631 #endif
632 #ifdef BUTTON_F3
633 case ACTION_F3:
634 /* don't enter f3 from plugin browser */
635 if (*tc.dirfilter < NUM_FILTER_MODES)
637 if (quick_screen_f3(BUTTON_F3))
638 reload_dir = true;
639 restore = true;
641 break;
642 #endif
644 case ACTION_STD_CONTEXT:
646 int onplay_result;
647 int attr = 0;
649 if(!numentries)
650 onplay_result = onplay(NULL, 0, curr_context);
651 else {
652 #ifdef HAVE_TAGCACHE
653 if (id3db)
655 if (tagtree_get_attr(&tc) == FILE_ATTR_AUDIO)
657 attr = FILE_ATTR_AUDIO;
658 tagtree_get_filename(&tc, buf, sizeof(buf));
660 else
661 attr = ATTR_DIRECTORY;
663 else
664 #endif
666 attr = dircache[tc.selected_item].attr;
668 if (currdir[1]) /* Not in / */
669 snprintf(buf, sizeof buf, "%s/%s",
670 currdir,
671 dircache[tc.selected_item].name);
672 else /* In / */
673 snprintf(buf, sizeof buf, "/%s",
674 dircache[tc.selected_item].name);
676 onplay_result = onplay(buf, attr, curr_context);
678 switch (onplay_result)
680 case ONPLAY_MAINMENU:
681 return GO_TO_ROOT;
683 case ONPLAY_OK:
684 restore = true;
685 break;
687 case ONPLAY_RELOAD_DIR:
688 reload_dir = true;
689 break;
691 case ONPLAY_START_PLAY:
692 return GO_TO_WPS;
693 break;
695 break;
698 case ACTION_NONE:
699 if (thumbnail_time != -1 &&
700 TIME_AFTER(current_tick, thumbnail_time))
701 { /* a delayed hovering thumbnail is due now */
702 int res;
703 int attr;
704 char* name;
706 #ifdef HAVE_TAGCACHE
707 if (id3db)
709 attr = tagtree_get_attr(&tc);
710 name = tagtree_get_entry(&tc, lasti)->name;
712 else
713 #endif
715 attr = dircache[lasti].attr;
716 name = dircache[lasti].name;
719 if (attr & ATTR_DIRECTORY)
721 DEBUGF("Playing directory thumbnail: %s", currdir);
722 res = ft_play_dirname(name);
723 if (res < 0) /* failed, not existing */
725 /* say the number or spell if required as a fallback */
726 switch (global_settings.talk_dir)
728 case 1: /* dirs as numbers */
729 talk_id(VOICE_DIR, false);
730 talk_number(lasti+1, true);
731 break;
733 case 2: /* dirs spelled */
734 talk_spell(name, false);
735 break;
739 else
741 DEBUGF("Playing file thumbnail: %s/%s%s\n",
742 currdir, name,
743 file_thumbnail_ext);
744 /* no fallback necessary, we knew in advance
745 that the file exists */
746 ft_play_filename(currdir, name);
748 thumbnail_time = -1; /* job done */
750 gui_syncstatusbar_draw(&statusbars, false);
751 break;
753 #ifdef HAVE_HOTSWAP
754 case SYS_FS_CHANGED:
755 #ifdef HAVE_TAGCACHE
756 if (!id3db)
757 #endif
758 reload_dir = true;
759 /* The 'dir no longer valid' situation will be caught later
760 * by checking the showdir() result. */
761 break;
762 #endif
764 default:
765 if (default_event_handler(button) == SYS_USB_CONNECTED)
767 if(*tc.dirfilter > NUM_FILTER_MODES)
768 /* leave sub-browsers after usb, doing otherwise
769 might be confusing to the user */
770 exit_func = true;
771 else
772 reload_dir = true;
774 break;
776 if (start_wps)
777 return GO_TO_WPS;
778 if ( button )
780 ata_spin();
784 check_rescan:
785 /* do we need to rescan dir? */
786 if (reload_dir || reload_root ||
787 lastfilter != *tc.dirfilter ||
788 lastsortcase != global_settings.sort_case)
790 if ( reload_root ) {
791 strcpy(currdir, "/");
792 tc.dirlevel = 0;
793 #ifdef HAVE_TAGCACHE
794 tc.currtable = 0;
795 tc.currextra = 0;
796 lasttable = -1;
797 lastextra = -1;
798 #endif
799 reload_root = false;
802 if (! reload_dir )
804 gui_synclist_select_item(&tree_lists, 0);
805 gui_synclist_draw(&tree_lists);
806 tc.selected_item = 0;
807 lastdir[0] = 0;
810 lastfilter = *tc.dirfilter;
811 lastsortcase = global_settings.sort_case;
812 restore = true;
815 if (exit_func)
816 return GO_TO_PREVIOUS;
818 if (restore || reload_dir) {
819 /* restore display */
820 numentries = update_dir();
821 if (currdir[1] && (numentries < 0))
822 { /* not in root and reload failed */
823 reload_root = true; /* try root */
824 reload_dir = false;
825 goto check_rescan;
827 need_update = true;
828 reload_dir = false;
831 if(need_update) {
832 need_update=false;
833 if ( numentries > 0 ) {
834 /* Voice the file if changed */
835 if(lasti != tc.selected_item || restore) {
836 int attr;
837 char* name;
839 lasti = tc.selected_item;
840 thumbnail_time = -1; /* Cancel whatever we were
841 about to say */
843 #ifdef HAVE_TAGCACHE
844 if (id3db)
846 attr = tagtree_get_attr(&tc);
847 name = tagtree_get_entry(&tc, tc.selected_item)->name;
849 else
850 #endif
852 attr = dircache[tc.selected_item].attr;
853 name = dircache[tc.selected_item].name;
856 /* Directory? */
857 if (attr & ATTR_DIRECTORY)
859 /* schedule thumbnail playback if required */
860 if (global_settings.talk_dir_clip)
861 thumbnail_time = current_tick + HOVER_DELAY;
862 else
864 /* talk directly */
865 switch (global_settings.talk_dir)
867 case 1: /* dirs as numbers */
868 talk_id(VOICE_DIR, false);
869 talk_number(tc.selected_item+1, true);
870 break;
872 case 2: /* dirs spelled */
873 talk_spell(name, false);
874 break;
878 else /* file */
880 /* schedule thumbnail playback if required */
881 if (global_settings.talk_file_clip && (attr & FILE_ATTR_THUMBNAIL))
882 thumbnail_time = current_tick + HOVER_DELAY;
883 else
885 /* talk directly */
886 switch (global_settings.talk_file)
888 case 1: /* files as numbers */
889 ft_play_filenumber(
890 tc.selected_item-tc.dirsindir+1,
891 attr & FILE_ATTR_MASK);
892 break;
894 case 2: /* files spelled */
895 talk_spell(name, false);
896 break;
904 action_signalscreenchange();
905 return true;
908 static int plsize = 0;
909 static long pltick;
910 static bool add_dir(char* dirname, int len, int fd)
912 bool abort = false;
913 DIRCACHED* dir;
915 /* check for user abort */
916 if (action_userabort(TIMEOUT_NOBLOCK))
917 return true;
919 dir = opendir_cached(dirname);
920 if(!dir)
921 return true;
923 while (true) {
924 struct dircache_entry *entry;
926 entry = readdir_cached(dir);
927 if (!entry)
928 break;
929 if (entry->attribute & ATTR_DIRECTORY) {
930 int dirlen = strlen(dirname);
931 bool result;
933 if (!strcmp((char *)entry->d_name, ".") ||
934 !strcmp((char *)entry->d_name, ".."))
935 continue;
937 if (dirname[1])
938 snprintf(dirname+dirlen, len-dirlen, "/%s", entry->d_name);
939 else
940 snprintf(dirname, len, "/%s", entry->d_name);
942 result = add_dir(dirname, len, fd);
943 dirname[dirlen] = '\0';
944 if (result) {
945 abort = true;
946 break;
949 else {
950 int x = strlen((char *)entry->d_name);
951 int i;
952 char *cp = strrchr((char *)entry->d_name,'.');
954 if (cp) {
955 cp++;
957 /* add all supported audio files to playlists */
958 for (i=0; i < filetypes_count; i++) {
959 if (filetypes[i].tree_attr == FILE_ATTR_AUDIO) {
960 if (!strcasecmp(cp, filetypes[i].extension)) {
961 char buf[8];
962 int i;
963 write(fd, dirname, strlen(dirname));
964 write(fd, "/", 1);
965 write(fd, entry->d_name, x);
966 write(fd, "\n", 1);
968 plsize++;
969 if(TIME_AFTER(current_tick, pltick+HZ/4)) {
970 pltick = current_tick;
972 snprintf(buf, sizeof buf, "%d", plsize);
973 #ifdef HAVE_LCD_BITMAP
974 FOR_NB_SCREENS(i)
976 screens[i].puts(0, 4, (unsigned char *)buf);
977 gui_textarea_update(&screens[i]);
979 #else
980 x = 10;
981 if (plsize > 999)
982 x=7;
983 else {
984 if (plsize > 99)
985 x=8;
986 else {
987 if (plsize > 9)
988 x=9;
991 FOR_NB_SCREENS(i) {
992 screens[i].puts(x,0,buf);
994 #endif
996 break;
1003 closedir_cached(dir);
1005 return abort;
1008 bool create_playlist(void)
1010 int fd;
1011 int i;
1012 char filename[MAX_PATH];
1014 pltick = current_tick;
1016 snprintf(filename, sizeof filename, "%s.m3u8",
1017 tc.currdir[1] ? tc.currdir : "/root");
1018 FOR_NB_SCREENS(i)
1020 gui_textarea_clear(&screens[i]);
1021 screens[i].puts(0, 0, str(LANG_CREATING));
1022 screens[i].puts_scroll(0, 1, (unsigned char *)filename);
1023 #if defined(HAVE_LCD_BITMAP) || defined(SIMULATOR)
1024 gui_textarea_update(&screens[i]);
1025 #endif
1027 fd = creat(filename);
1028 if (fd < 0)
1029 return false;
1031 trigger_cpu_boost();
1033 snprintf(filename, sizeof(filename), "%s",
1034 tc.currdir[1] ? tc.currdir : "/");
1035 plsize = 0;
1036 add_dir(filename, sizeof(filename), fd);
1037 close(fd);
1039 sleep(HZ);
1041 return true;
1044 int rockbox_browse(const char *root, int dirfilter)
1046 int ret_val = 0;
1047 int *last_filter = tc.dirfilter;
1048 tc.dirfilter = &dirfilter;
1050 reload_dir = true;
1051 if (dirfilter >= NUM_FILTER_MODES)
1053 static struct tree_context backup;
1054 int last_context;
1056 backup = tc;
1057 tc.selected_item = 0;
1058 tc.dirlevel = 0;
1059 memcpy(tc.currdir, root, sizeof(tc.currdir));
1060 start_wps = false;
1061 last_context = curr_context;
1063 ret_val = dirbrowse();
1064 tc = backup;
1065 curr_context = last_context;
1067 else
1069 static char buf[MAX_PATH];
1070 if (dirfilter != SHOW_ID3DB)
1071 tc.dirfilter = &global_settings.dirfilter;
1072 strcpy(buf,root);
1073 set_current_file(buf);
1074 ret_val = dirbrowse();
1076 tc.dirfilter = last_filter;
1077 return ret_val;
1080 void tree_mem_init(void)
1082 /* We copy the settings value in case it is changed by the user. We can't
1083 use it until the next reboot. */
1084 max_files = global_settings.max_files_in_dir;
1086 /* initialize tree context struct */
1087 memset(&tc, 0, sizeof(tc));
1088 tc.dirfilter = &global_settings.dirfilter;
1090 tc.name_buffer_size = AVERAGE_FILENAME_LENGTH * max_files;
1091 tc.name_buffer = buffer_alloc(tc.name_buffer_size);
1093 tc.dircache_size = max_files * sizeof(struct entry);
1094 tc.dircache = buffer_alloc(tc.dircache_size);
1095 tree_get_filetypes(&filetypes, &filetypes_count);
1098 void bookmark_play(char *resume_file, int index, int offset, int seed,
1099 char *filename)
1101 int i;
1102 char* suffix = strrchr(resume_file, '.');
1104 if (suffix != NULL &&
1105 (!strcasecmp(suffix, ".m3u") || !strcasecmp(suffix, ".m3u8")))
1107 /* Playlist playback */
1108 char* slash;
1109 /* check that the file exists */
1110 int fd = open(resume_file, O_RDONLY);
1111 if(fd<0)
1112 return;
1113 close(fd);
1115 slash = strrchr(resume_file,'/');
1116 if (slash)
1118 char* cp;
1119 *slash=0;
1121 cp=resume_file;
1122 if (!cp[0])
1123 cp="/";
1125 if (playlist_create(cp, slash+1) != -1)
1127 if (global_settings.playlist_shuffle)
1128 playlist_shuffle(seed, -1);
1129 playlist_start(index,offset);
1131 *slash='/';
1134 else
1136 /* Directory playback */
1137 lastdir[0]='\0';
1138 if (playlist_create(resume_file, NULL) != -1)
1140 char* peek_filename;
1141 resume_directory(resume_file);
1142 if (global_settings.playlist_shuffle)
1143 playlist_shuffle(seed, -1);
1145 /* Check if the file is at the same spot in the directory,
1146 else search for it */
1147 peek_filename = playlist_peek(index);
1149 if (peek_filename == NULL)
1150 return;
1152 if (strcmp(strrchr(peek_filename, '/') + 1, filename))
1154 for ( i=0; i < playlist_amount(); i++ )
1156 peek_filename = playlist_peek(i);
1158 if (peek_filename == NULL)
1159 return;
1161 if (!strcmp(strrchr(peek_filename, '/') + 1, filename))
1162 break;
1164 if (i < playlist_amount())
1165 index = i;
1166 else
1167 return;
1169 playlist_start(index,offset);
1173 start_wps=true;
1176 static int ft_play_filenumber(int pos, int attr)
1178 /* try to find a voice ID for the extension, if known */
1179 int j;
1180 int ext_id = -1; /* default to none */
1181 for (j=0; j<filetypes_count; j++)
1183 if (attr == filetypes[j].tree_attr)
1185 ext_id = filetypes[j].voiceclip;
1186 break;
1190 talk_id(VOICE_FILE, false);
1191 talk_number(pos, true);
1192 talk_id(ext_id, true);
1193 return 1;
1196 static int ft_play_dirname(char* name)
1198 int fd;
1199 char dirname_mp3_filename[MAX_PATH+1];
1201 #if CONFIG_CODEC != SWCODEC
1202 if (audio_status() & AUDIO_STATUS_PLAY)
1203 return 0;
1204 #endif
1206 snprintf(dirname_mp3_filename, sizeof(dirname_mp3_filename), "%s/%s/%s",
1207 tc.currdir[1] ? tc.currdir : "" , name,
1208 dir_thumbnail_name);
1210 DEBUGF("Checking for %s\n", dirname_mp3_filename);
1212 fd = open(dirname_mp3_filename, O_RDONLY);
1213 if (fd < 0)
1215 DEBUGF("Failed to find: %s\n", dirname_mp3_filename);
1216 return -1;
1219 close(fd);
1221 DEBUGF("Found: %s\n", dirname_mp3_filename);
1223 talk_file(dirname_mp3_filename, false);
1224 return 1;
1227 static void ft_play_filename(char *dir, char *file)
1229 char name_mp3_filename[MAX_PATH+1];
1231 #if CONFIG_CODEC != SWCODEC
1232 if (audio_status() & AUDIO_STATUS_PLAY)
1233 return;
1234 #endif
1236 if (strcasecmp(&file[strlen(file) - strlen(file_thumbnail_ext)],
1237 file_thumbnail_ext))
1238 { /* file has no .talk extension */
1239 snprintf(name_mp3_filename, sizeof(name_mp3_filename),
1240 "%s/%s%s", dir, file, file_thumbnail_ext);
1242 talk_file(name_mp3_filename, false);
1244 else
1245 { /* it already is a .talk file, play this directly */
1246 snprintf(name_mp3_filename, sizeof(name_mp3_filename),
1247 "%s/%s", dir, file);
1248 talk_id(LANG_VOICE_DIR_HOVER, false); /* prefix it */
1249 talk_file(name_mp3_filename, true);
1253 /* These two functions are called by the USB and shutdown handlers */
1254 void tree_flush(void)
1256 scrobbler_shutdown();
1257 #ifdef HAVE_TAGCACHE
1258 tagcache_shutdown();
1259 #endif
1260 playlist_shutdown();
1262 #ifdef HAVE_TC_RAMCACHE
1263 tagcache_unload_ramcache();
1264 #endif
1266 #ifdef HAVE_DIRCACHE
1268 int old_val = global_status.dircache_size;
1269 if (global_settings.dircache)
1271 global_status.dircache_size = dircache_get_cache_size();
1272 # ifdef HAVE_EEPROM_SETTINGS
1273 if (firmware_settings.initialized)
1274 dircache_save();
1275 # endif
1276 dircache_disable();
1278 else
1280 global_status.dircache_size = 0;
1282 if (old_val != global_status.dircache_size)
1283 status_save();
1285 #endif
1288 void tree_restore(void)
1290 #ifdef HAVE_EEPROM_SETTINGS
1291 firmware_settings.disk_clean = false;
1292 #endif
1294 #ifdef HAVE_TC_RAMCACHE
1295 remove(TAGCACHE_STATEFILE);
1296 #endif
1298 #ifdef HAVE_DIRCACHE
1299 remove(DIRCACHE_FILE);
1300 if (global_settings.dircache)
1302 /* Print "Scanning disk..." to the display. */
1303 int i;
1304 FOR_NB_SCREENS(i)
1306 screens[i].putsxy((LCD_WIDTH/2) -
1307 ((strlen(str(LANG_DIRCACHE_BUILDING)) *
1308 screens[i].char_width)/2),
1309 LCD_HEIGHT-screens[i].char_height*3,
1310 str(LANG_DIRCACHE_BUILDING));
1311 gui_textarea_update(&screens[i]);
1314 dircache_build(global_status.dircache_size);
1316 /* Clean the text when we are done. */
1317 FOR_NB_SCREENS(i)
1319 gui_textarea_clear(&screens[i]);
1322 #endif
1323 #ifdef HAVE_TAGCACHE
1324 tagcache_start_scan();
1325 #endif
1326 scrobbler_init();