make usb_serial work again. Also know as "make sure arrays are allocated at their...
[kugel-rb.git] / apps / tree.c
blobec70cb3e2e6f487accfb64a252ed7c2a3c8eb295
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_synclist_set_color_callback(&tree_lists, &tree_get_filecolor);
315 #endif
320 struct tree_context* tree_get_context(void)
322 return &tc;
326 * Returns the position of a given file in the current directory
327 * returns -1 if not found
329 static int tree_get_file_position(char * filename)
331 int i;
333 /* use lastfile to determine the selected item (default=0) */
334 for (i=0; i < tc.filesindir; i++)
336 struct entry* dc = tc.dircache;
337 struct entry* e = &dc[i];
338 if (!strcasecmp(e->name, filename))
339 return(i);
341 return(-1);/* no file can match, returns undefined */
345 * Called when a new dir is loaded (for example when returning from other apps ...)
346 * also completely redraws the tree
348 static int update_dir(void)
350 bool changed = false;
351 #ifdef HAVE_TAGCACHE
352 bool id3db = *tc.dirfilter == SHOW_ID3DB;
353 /* Checks for changes */
354 if (id3db) {
355 if (tc.currtable != lasttable ||
356 tc.currextra != lastextra ||
357 tc.firstpos != lastfirstpos ||
358 reload_dir)
360 if (tagtree_load(&tc) < 0)
361 return -1;
363 lasttable = tc.currtable;
364 lastextra = tc.currextra;
365 lastfirstpos = tc.firstpos;
366 changed = true;
369 else
370 #endif
372 /* if the tc.currdir has been changed, reload it ...*/
373 if (strncmp(tc.currdir, lastdir, sizeof(lastdir)) || reload_dir)
375 if (ft_load(&tc, NULL) < 0)
376 return -1;
377 strcpy(lastdir, tc.currdir);
378 changed = true;
381 /* if selected item is undefined */
382 if (tc.selected_item == -1)
384 /* use lastfile to determine the selected item */
385 tc.selected_item = tree_get_file_position(lastfile);
387 /* If the file doesn't exists, select the first one (default) */
388 if(tc.selected_item < 0)
389 tc.selected_item = 0;
390 changed = true;
392 if (changed)
395 #ifdef HAVE_TAGCACHE
396 !id3db &&
397 #endif
398 (tc.dirfull || tc.filesindir == global_settings.max_files_in_dir) )
400 gui_syncsplash(HZ, ID2P(LANG_SHOWDIR_BUFFER_FULL));
403 #ifdef HAVE_TAGCACHE
404 if (id3db)
406 #ifdef HAVE_LCD_BITMAP
407 if (global_settings.show_path_in_browser == SHOW_PATH_FULL
408 || global_settings.show_path_in_browser == SHOW_PATH_CURRENT)
410 gui_synclist_set_title(&tree_lists, tagtree_get_title(&tc),
411 filetype_get_icon(ATTR_DIRECTORY));
413 else
415 /* Must clear the title as the list is reused */
416 gui_synclist_set_title(&tree_lists, NULL, NOICON);
418 #endif
420 else
421 #endif
423 #ifdef HAVE_LCD_BITMAP
424 if (global_settings.show_path_in_browser &&
425 *(tc.dirfilter) == SHOW_PLUGINS)
427 char *title;
428 if (!strcmp(tc.currdir, PLUGIN_GAMES_DIR))
429 title = str(LANG_PLUGIN_GAMES);
430 else if (!strcmp(tc.currdir, PLUGIN_APPS_DIR))
431 title = str(LANG_PLUGIN_APPS);
432 else if (!strcmp(tc.currdir, PLUGIN_DEMOS_DIR))
433 title = str(LANG_PLUGIN_DEMOS);
434 else title = str(LANG_PLUGINS);
435 gui_synclist_set_title(&tree_lists, title, Icon_Plugin);
437 else if (global_settings.show_path_in_browser == SHOW_PATH_FULL)
439 gui_synclist_set_title(&tree_lists, tc.currdir,
440 filetype_get_icon(ATTR_DIRECTORY));
442 else if (global_settings.show_path_in_browser == SHOW_PATH_CURRENT)
444 char *title = strrchr(tc.currdir, '/') + 1;
445 if (*title == '\0')
447 /* Display "Files" for the root dir */
448 gui_synclist_set_title(&tree_lists, str(LANG_DIR_BROWSER),
449 filetype_get_icon(ATTR_DIRECTORY));
451 else
452 gui_synclist_set_title(&tree_lists, title,
453 filetype_get_icon(ATTR_DIRECTORY));
455 else
457 /* Must clear the title as the list is reused */
458 gui_synclist_set_title(&tree_lists, NULL, NOICON);
460 #endif
463 gui_synclist_set_nb_items(&tree_lists, tc.filesindir);
464 gui_synclist_set_icon_callback(&tree_lists, tree_get_fileicon);
465 if( tc.selected_item >= tc.filesindir)
466 tc.selected_item=tc.filesindir-1;
468 gui_synclist_select_item(&tree_lists, tc.selected_item);
469 #ifdef HAS_BUTTONBAR
470 if (global_settings.buttonbar) {
471 if (*tc.dirfilter < NUM_FILTER_MODES)
472 gui_buttonbar_set(&tree_buttonbar, str(LANG_SYSFONT_DIRBROWSE_F1),
473 str(LANG_SYSFONT_DIRBROWSE_F2),
474 str(LANG_SYSFONT_DIRBROWSE_F3));
475 else
476 gui_buttonbar_set(&tree_buttonbar, "<<<", "", "");
477 gui_buttonbar_draw(&tree_buttonbar);
479 #endif
480 gui_synclist_draw(&tree_lists);
481 gui_synclist_speak_item(&tree_lists);
482 gui_syncstatusbar_draw(&statusbars, true);
483 return tc.filesindir;
486 /* load tracks from specified directory to resume play */
487 void resume_directory(const char *dir)
489 #ifdef HAVE_TAGCACHE
490 bool id3db = *tc.dirfilter == SHOW_ID3DB;
491 #endif
493 if (ft_load(&tc, dir) < 0)
494 return;
495 lastdir[0] = 0;
497 ft_build_playlist(&tc, 0);
499 #ifdef HAVE_TAGCACHE
500 if (id3db)
501 tagtree_load(&tc);
502 #endif
505 /* Returns the current working directory and also writes cwd to buf if
506 non-NULL. In case of error, returns NULL. */
507 char *getcwd(char *buf, int size)
509 if (!buf)
510 return tc.currdir;
511 else if (size > 0)
513 strncpy(buf, tc.currdir, size);
514 return buf;
516 else
517 return NULL;
520 /* Force a reload of the directory next time directory browser is called */
521 void reload_directory(void)
523 reload_dir = true;
526 void get_current_file(char* buffer, int buffer_len)
528 #ifdef HAVE_TAGCACHE
529 /* in ID3DB mode it is a bad idea to call this function */
530 /* (only happens with `follow playlist') */
531 if( *tc.dirfilter == SHOW_ID3DB )
532 return;
533 #endif
535 struct entry* dc = tc.dircache;
536 struct entry* e = &dc[tc.selected_item];
537 snprintf(buffer, buffer_len, "%s/%s", getcwd(NULL,0),
538 tc.dirlength ? e->name : "");
541 /* Allow apps to change our dirfilter directly (required for sub browsers)
542 if they're suddenly going to become a file browser for example */
543 void set_dirfilter(int l_dirfilter)
545 *tc.dirfilter = l_dirfilter;
548 /* Selects a file and update tree context properly */
549 void set_current_file(char *path)
551 char *name;
552 int i;
554 #ifdef HAVE_TAGCACHE
555 /* in ID3DB mode it is a bad idea to call this function */
556 /* (only happens with `follow playlist') */
557 if( *tc.dirfilter == SHOW_ID3DB )
558 return;
559 #endif
561 /* separate directory from filename */
562 /* gets the directory's name and put it into tc.currdir */
563 name = strrchr(path+1,'/');
564 if (name)
566 *name = 0;
567 strcpy(tc.currdir, path);
568 *name = '/';
569 name++;
571 else
573 strcpy(tc.currdir, "/");
574 name = path+1;
577 strcpy(lastfile, name);
580 /* If we changed dir we must recalculate the dirlevel
581 and adjust the selected history properly */
582 if (strncmp(tc.currdir,lastdir,sizeof(lastdir)))
584 tc.dirlevel = 0;
585 tc.selected_item_history[tc.dirlevel] = -1;
587 /* use '/' to calculate dirlevel */
588 for (i = 1; path[i] != '\0'; i++)
590 if (path[i] == '/')
592 tc.dirlevel++;
593 tc.selected_item_history[tc.dirlevel] = -1;
597 if (ft_load(&tc, NULL) >= 0)
599 tc.selected_item = tree_get_file_position(lastfile);
604 /* main loop, handles key events */
605 static int dirbrowse()
607 int numentries=0;
608 char buf[MAX_PATH];
609 unsigned button, oldbutton;
610 bool reload_root = false;
611 int lastfilter = *tc.dirfilter;
612 bool lastsortcase = global_settings.sort_case;
613 bool exit_func = false;
615 char* currdir = tc.currdir; /* just a shortcut */
616 #ifdef HAVE_TAGCACHE
617 bool id3db = *tc.dirfilter == SHOW_ID3DB;
619 if (id3db)
620 curr_context=CONTEXT_ID3DB;
621 else
622 #endif
623 curr_context=CONTEXT_TREE;
624 if (tc.selected_item < 0)
625 tc.selected_item = 0;
626 #ifdef HAVE_TAGCACHE
627 tc.firstpos = 0;
628 lasttable = -1;
629 lastextra = -1;
630 lastfirstpos = 0;
631 #endif
633 start_wps = false;
634 numentries = update_dir();
635 reload_dir = false;
636 if (numentries == -1)
637 return GO_TO_PREVIOUS; /* currdir is not a directory */
639 if (*tc.dirfilter > NUM_FILTER_MODES && numentries==0)
641 gui_syncsplash(HZ*2, ID2P(LANG_NO_FILES));
642 return GO_TO_PREVIOUS; /* No files found for rockbox_browser() */
645 while(1) {
646 struct entry *dircache = tc.dircache;
647 bool restore = false;
648 if (tc.dirlevel < 0)
649 tc.dirlevel = 0; /* shouldnt be needed.. this code needs work! */
650 #ifdef BOOTFILE
651 if (boot_changed) {
652 char *lines[]={ID2P(LANG_BOOT_CHANGED), ID2P(LANG_REBOOT_NOW)};
653 struct text_message message={lines, 2};
654 if(gui_syncyesno_run(&message, NULL, NULL)==YESNO_YES)
655 rolo_load("/" BOOTFILE);
656 restore = true;
657 boot_changed = false;
659 #endif
660 button = get_action(CONTEXT_TREE,
661 list_do_action_timeout(&tree_lists, HZ/2));
662 oldbutton = button;
663 gui_synclist_do_button(&tree_lists, &button,LIST_WRAP_UNLESS_HELD);
664 tc.selected_item = gui_synclist_get_sel_pos(&tree_lists);
665 switch ( button ) {
666 case ACTION_STD_OK:
667 /* nothing to do if no files to display */
668 if ( numentries == 0 )
669 break;
671 #ifdef HAVE_TAGCACHE
672 switch (id3db?tagtree_enter(&tc):ft_enter(&tc))
673 #else
674 switch (ft_enter(&tc))
675 #endif
677 case 1: reload_dir = true; break;
678 case 2: start_wps = true; break;
679 case 3: exit_func = true; break;
680 default: break;
682 restore = true;
683 break;
685 case ACTION_STD_CANCEL:
686 if (*tc.dirfilter > NUM_FILTER_MODES && tc.dirlevel < 1) {
687 exit_func = true;
688 break;
690 if ((*tc.dirfilter == SHOW_ID3DB && tc.dirlevel == 0) ||
691 ((*tc.dirfilter != SHOW_ID3DB && !strcmp(currdir,"/"))))
693 #ifdef HAVE_LCD_BITMAP /* charcell doesnt have ACTION_TREE_PGLEFT so this isnt needed */
694 if (oldbutton == ACTION_TREE_PGLEFT)
695 break;
696 else
697 #endif
698 return GO_TO_ROOT;
701 #ifdef HAVE_TAGCACHE
702 if (id3db)
703 tagtree_exit(&tc);
704 else
705 #endif
706 if (ft_exit(&tc) == 3)
707 exit_func = true;
709 restore = true;
710 break;
712 case ACTION_TREE_STOP:
713 if (list_stop_handler())
714 restore = true;
715 break;
717 case ACTION_STD_MENU:
718 return GO_TO_ROOT;
719 break;
721 #ifdef HAVE_RECORDING
722 case ACTION_STD_REC:
723 return GO_TO_RECSCREEN;
724 #endif
726 case ACTION_TREE_WPS:
727 return GO_TO_PREVIOUS_MUSIC;
728 break;
729 #ifdef HAVE_QUICKSCREEN
730 case ACTION_STD_QUICKSCREEN:
731 /* don't enter f2 from plugin browser */
732 if (*tc.dirfilter < NUM_FILTER_MODES)
734 if (quick_screen_quick(button))
735 reload_dir = true;
736 restore = true;
738 break;
739 #endif
740 #ifdef BUTTON_F3
741 case ACTION_F3:
742 /* don't enter f3 from plugin browser */
743 if (*tc.dirfilter < NUM_FILTER_MODES)
745 if (quick_screen_f3(BUTTON_F3))
746 reload_dir = true;
747 restore = true;
749 break;
750 #endif
752 case ACTION_STD_CONTEXT:
754 int onplay_result;
755 int attr = 0;
757 if(!numentries)
758 onplay_result = onplay(NULL, 0, curr_context);
759 else {
760 #ifdef HAVE_TAGCACHE
761 if (id3db)
763 if (tagtree_get_attr(&tc) == FILE_ATTR_AUDIO)
765 attr = FILE_ATTR_AUDIO;
766 tagtree_get_filename(&tc, buf, sizeof(buf));
768 else
769 attr = ATTR_DIRECTORY;
771 else
772 #endif
774 attr = dircache[tc.selected_item].attr;
776 if (currdir[1]) /* Not in / */
777 snprintf(buf, sizeof buf, "%s/%s",
778 currdir,
779 dircache[tc.selected_item].name);
780 else /* In / */
781 snprintf(buf, sizeof buf, "/%s",
782 dircache[tc.selected_item].name);
784 onplay_result = onplay(buf, attr, curr_context);
786 switch (onplay_result)
788 case ONPLAY_MAINMENU:
789 return GO_TO_ROOT;
791 case ONPLAY_OK:
792 restore = true;
793 break;
795 case ONPLAY_RELOAD_DIR:
796 reload_dir = true;
797 break;
799 case ONPLAY_START_PLAY:
800 return GO_TO_WPS;
801 break;
803 break;
806 case ACTION_NONE:
807 gui_syncstatusbar_draw(&statusbars, false);
808 break;
810 #ifdef HAVE_HOTSWAP
811 case SYS_FS_CHANGED:
812 #ifdef HAVE_TAGCACHE
813 if (!id3db)
814 #endif
815 reload_dir = true;
816 /* The 'dir no longer valid' situation will be caught later
817 * by checking the showdir() result. */
818 break;
819 #endif
821 default:
822 if (default_event_handler(button) == SYS_USB_CONNECTED)
824 if(*tc.dirfilter > NUM_FILTER_MODES)
825 /* leave sub-browsers after usb, doing otherwise
826 might be confusing to the user */
827 exit_func = true;
828 else
829 reload_dir = true;
831 break;
833 if (start_wps)
834 return GO_TO_WPS;
835 if (button)
837 ata_spin();
841 check_rescan:
842 /* do we need to rescan dir? */
843 if (reload_dir || reload_root ||
844 lastfilter != *tc.dirfilter ||
845 lastsortcase != global_settings.sort_case)
847 if (reload_root) {
848 strcpy(currdir, "/");
849 tc.dirlevel = 0;
850 #ifdef HAVE_TAGCACHE
851 tc.currtable = 0;
852 tc.currextra = 0;
853 lasttable = -1;
854 lastextra = -1;
855 #endif
856 reload_root = false;
859 if (!reload_dir)
861 gui_synclist_select_item(&tree_lists, 0);
862 gui_synclist_draw(&tree_lists);
863 tc.selected_item = 0;
864 lastdir[0] = 0;
867 lastfilter = *tc.dirfilter;
868 lastsortcase = global_settings.sort_case;
869 restore = true;
872 if (exit_func)
873 return GO_TO_PREVIOUS;
875 if (restore || reload_dir) {
876 /* restore display */
877 numentries = update_dir();
878 reload_dir = false;
879 if (currdir[1] && (numentries < 0))
880 { /* not in root and reload failed */
881 reload_root = true; /* try root */
882 goto check_rescan;
886 return true;
889 static int plsize = 0;
890 static long pltick;
891 static bool add_dir(char* dirname, int len, int fd)
893 bool abort = false;
894 DIR* dir;
896 /* check for user abort */
897 if (action_userabort(TIMEOUT_NOBLOCK))
898 return true;
900 dir = opendir(dirname);
901 if(!dir)
902 return true;
904 while (true) {
905 struct dirent *entry;
907 entry = readdir(dir);
908 if (!entry)
909 break;
910 if (entry->attribute & ATTR_DIRECTORY) {
911 int dirlen = strlen(dirname);
912 bool result;
914 if (!strcmp((char *)entry->d_name, ".") ||
915 !strcmp((char *)entry->d_name, ".."))
916 continue;
918 if (dirname[1])
919 snprintf(dirname+dirlen, len-dirlen, "/%s", entry->d_name);
920 else
921 snprintf(dirname, len, "/%s", entry->d_name);
923 result = add_dir(dirname, len, fd);
924 dirname[dirlen] = '\0';
925 if (result) {
926 abort = true;
927 break;
930 else {
931 int x = strlen((char *)entry->d_name);
932 int i;
933 char *cp = strrchr((char *)entry->d_name,'.');
935 if (cp) {
936 cp++;
938 /* add all supported audio files to playlists */
939 for (i=0; i < filetypes_count; i++) {
940 if (filetypes[i].tree_attr == FILE_ATTR_AUDIO) {
941 if (!strcasecmp(cp, filetypes[i].extension)) {
942 char buf[8];
943 int i;
944 write(fd, dirname, strlen(dirname));
945 write(fd, "/", 1);
946 write(fd, entry->d_name, x);
947 write(fd, "\n", 1);
949 plsize++;
950 if(TIME_AFTER(current_tick, pltick+HZ/4)) {
951 pltick = current_tick;
953 snprintf(buf, sizeof buf, "%d", plsize);
954 #ifdef HAVE_LCD_BITMAP
955 FOR_NB_SCREENS(i)
957 screens[i].puts(0, 4, (unsigned char *)buf);
958 gui_textarea_update(&screens[i]);
960 #else
961 if (plsize > 999)
962 x=7;
963 else if (plsize > 99)
964 x=8;
965 else if (plsize > 9)
966 x=9;
967 else
968 x = 10;
970 FOR_NB_SCREENS(i) {
971 screens[i].puts(x,0,buf);
973 #endif
975 break;
982 closedir(dir);
984 return abort;
987 bool create_playlist(void)
989 int fd;
990 int i;
991 char filename[MAX_PATH];
993 pltick = current_tick;
995 snprintf(filename, sizeof filename, "%s.m3u8",
996 tc.currdir[1] ? tc.currdir : "/root");
997 FOR_NB_SCREENS(i)
999 gui_textarea_clear(&screens[i]);
1000 screens[i].puts(0, 0, str(LANG_CREATING));
1001 screens[i].puts_scroll(0, 1, (unsigned char *)filename);
1002 #if defined(HAVE_LCD_BITMAP) || defined(SIMULATOR)
1003 gui_textarea_update(&screens[i]);
1004 #endif
1006 fd = creat(filename);
1007 if (fd < 0)
1008 return false;
1010 trigger_cpu_boost();
1012 snprintf(filename, sizeof(filename), "%s",
1013 tc.currdir[1] ? tc.currdir : "/");
1014 plsize = 0;
1015 add_dir(filename, sizeof(filename), fd);
1016 close(fd);
1018 cancel_cpu_boost();
1019 sleep(HZ);
1021 return true;
1024 int rockbox_browse(const char *root, int dirfilter)
1026 int ret_val = 0;
1027 int *last_filter = tc.dirfilter;
1028 tc.dirfilter = &dirfilter;
1030 reload_dir = true;
1031 if (dirfilter >= NUM_FILTER_MODES)
1033 static struct tree_context backup;
1034 int last_context;
1036 backup = tc;
1037 tc.selected_item = 0;
1038 tc.dirlevel = 0;
1039 memcpy(tc.currdir, root, sizeof(tc.currdir));
1040 start_wps = false;
1041 last_context = curr_context;
1043 ret_val = dirbrowse();
1044 tc = backup;
1045 curr_context = last_context;
1047 else
1049 static char buf[MAX_PATH];
1050 if (dirfilter != SHOW_ID3DB)
1051 tc.dirfilter = &global_settings.dirfilter;
1052 strcpy(buf,root);
1053 set_current_file(buf);
1054 ret_val = dirbrowse();
1056 tc.dirfilter = last_filter;
1057 return ret_val;
1060 void tree_mem_init(void)
1062 /* We copy the settings value in case it is changed by the user. We can't
1063 use it until the next reboot. */
1064 max_files = global_settings.max_files_in_dir;
1066 /* initialize tree context struct */
1067 memset(&tc, 0, sizeof(tc));
1068 tc.dirfilter = &global_settings.dirfilter;
1070 tc.name_buffer_size = AVERAGE_FILENAME_LENGTH * max_files;
1071 tc.name_buffer = buffer_alloc(tc.name_buffer_size);
1073 tc.dircache_size = max_files * sizeof(struct entry);
1074 tc.dircache = buffer_alloc(tc.dircache_size);
1075 tree_get_filetypes(&filetypes, &filetypes_count);
1078 bool bookmark_play(char *resume_file, int index, int offset, int seed,
1079 char *filename)
1081 int i;
1082 char* suffix = strrchr(resume_file, '.');
1083 bool started = false;
1085 if (suffix != NULL &&
1086 (!strcasecmp(suffix, ".m3u") || !strcasecmp(suffix, ".m3u8")))
1088 /* Playlist playback */
1089 char* slash;
1090 /* check that the file exists */
1091 int fd = open(resume_file, O_RDONLY);
1092 if(fd<0)
1093 return false;
1094 close(fd);
1096 slash = strrchr(resume_file,'/');
1097 if (slash)
1099 char* cp;
1100 *slash=0;
1102 cp=resume_file;
1103 if (!cp[0])
1104 cp="/";
1106 if (playlist_create(cp, slash+1) != -1)
1108 if (global_settings.playlist_shuffle)
1109 playlist_shuffle(seed, -1);
1110 playlist_start(index,offset);
1111 started = true;
1113 *slash='/';
1116 else
1118 /* Directory playback */
1119 lastdir[0]='\0';
1120 if (playlist_create(resume_file, NULL) != -1)
1122 char* peek_filename;
1123 resume_directory(resume_file);
1124 if (global_settings.playlist_shuffle)
1125 playlist_shuffle(seed, -1);
1127 /* Check if the file is at the same spot in the directory,
1128 else search for it */
1129 peek_filename = playlist_peek(index);
1131 if (peek_filename == NULL)
1132 return false;
1134 if (strcmp(strrchr(peek_filename, '/') + 1, filename))
1136 for ( i=0; i < playlist_amount(); i++ )
1138 peek_filename = playlist_peek(i);
1140 if (peek_filename == NULL)
1141 return false;
1143 if (!strcmp(strrchr(peek_filename, '/') + 1, filename))
1144 break;
1146 if (i < playlist_amount())
1147 index = i;
1148 else
1149 return false;
1151 playlist_start(index,offset);
1152 started = true;
1156 if (started)
1157 start_wps = true;
1158 return started;
1161 static void say_filetype(int attr)
1163 /* try to find a voice ID for the extension, if known */
1164 int j;
1165 attr &= FILE_ATTR_MASK; /* file type */
1166 for (j=0; j<filetypes_count; j++)
1167 if (attr == filetypes[j].tree_attr)
1169 talk_id(filetypes[j].voiceclip, true);
1170 return;
1174 static int ft_play_dirname(char* name)
1176 int fd;
1177 char dirname_mp3_filename[MAX_PATH+1];
1179 #if CONFIG_CODEC != SWCODEC
1180 if (audio_status() & AUDIO_STATUS_PLAY)
1181 return 0;
1182 #endif
1184 snprintf(dirname_mp3_filename, sizeof(dirname_mp3_filename), "%s/%s/%s",
1185 tc.currdir[1] ? tc.currdir : "" , name,
1186 dir_thumbnail_name);
1188 DEBUGF("Checking for %s\n", dirname_mp3_filename);
1190 fd = open(dirname_mp3_filename, O_RDONLY);
1191 if (fd < 0)
1193 DEBUGF("Failed to find: %s\n", dirname_mp3_filename);
1194 return -1;
1197 close(fd);
1199 DEBUGF("Found: %s\n", dirname_mp3_filename);
1201 talk_file(dirname_mp3_filename, false);
1202 if(global_settings.talk_filetype)
1203 talk_id(VOICE_DIR, true);
1204 return 1;
1207 static void ft_play_filename(char *dir, char *file)
1209 char name_mp3_filename[MAX_PATH+1];
1211 #if CONFIG_CODEC != SWCODEC
1212 if (audio_status() & AUDIO_STATUS_PLAY)
1213 return;
1214 #endif
1216 if (strcasecmp(&file[strlen(file) - strlen(file_thumbnail_ext)],
1217 file_thumbnail_ext))
1218 { /* file has no .talk extension */
1219 snprintf(name_mp3_filename, sizeof(name_mp3_filename),
1220 "%s/%s%s", dir, file, file_thumbnail_ext);
1222 talk_file(name_mp3_filename, false);
1224 else
1225 { /* it already is a .talk file, play this directly */
1226 snprintf(name_mp3_filename, sizeof(name_mp3_filename),
1227 "%s/%s", dir, file);
1228 talk_id(LANG_VOICE_DIR_HOVER, false); /* prefix it */
1229 talk_file(name_mp3_filename, true);
1233 /* These two functions are called by the USB and shutdown handlers */
1234 void tree_flush(void)
1236 scrobbler_shutdown();
1237 #ifdef HAVE_TAGCACHE
1238 tagcache_shutdown();
1239 #endif
1240 playlist_shutdown();
1242 #ifdef HAVE_TC_RAMCACHE
1243 tagcache_unload_ramcache();
1244 #endif
1246 #ifdef HAVE_DIRCACHE
1248 int old_val = global_status.dircache_size;
1249 if (global_settings.dircache)
1251 if (!dircache_is_initializing())
1252 global_status.dircache_size = dircache_get_cache_size();
1253 # ifdef HAVE_EEPROM_SETTINGS
1254 if (firmware_settings.initialized)
1255 dircache_save();
1256 # endif
1257 dircache_disable();
1259 else
1261 global_status.dircache_size = 0;
1263 if (old_val != global_status.dircache_size)
1264 status_save();
1266 #endif
1269 void tree_restore(void)
1271 #ifdef HAVE_EEPROM_SETTINGS
1272 firmware_settings.disk_clean = false;
1273 #endif
1275 #ifdef HAVE_TC_RAMCACHE
1276 remove(TAGCACHE_STATEFILE);
1277 #endif
1279 #ifdef HAVE_DIRCACHE
1280 remove(DIRCACHE_FILE);
1281 if (global_settings.dircache)
1283 /* Print "Scanning disk..." to the display. */
1284 int i;
1285 FOR_NB_SCREENS(i)
1287 screens[i].putsxy((LCD_WIDTH/2) -
1288 ((strlen(str(LANG_SCANNING_DISK)) *
1289 screens[i].char_width)/2),
1290 LCD_HEIGHT-screens[i].char_height*3,
1291 str(LANG_SCANNING_DISK));
1292 gui_textarea_update(&screens[i]);
1294 cond_talk_ids_fq(LANG_SCANNING_DISK);
1296 dircache_build(global_status.dircache_size);
1298 /* Clean the text when we are done. */
1299 FOR_NB_SCREENS(i)
1301 gui_textarea_clear(&screens[i]);
1304 #endif
1305 #ifdef HAVE_TAGCACHE
1306 tagcache_start_scan();
1307 #endif
1308 scrobbler_init();