more green seen as everyone has a green fetish atm
[Rockbox.git] / apps / tree.c
blobf70c07ca154cf2c7c077429fb1a2582f913170b5
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"
80 #include "quickscreen.h"
82 #include "root_menu.h"
84 #if (LCD_DEPTH > 1) || (defined(HAVE_LCD_REMOTE) && (LCD_REMOTE_DEPTH > 1))
85 #include "backdrop.h"
86 #endif
88 static const struct filetype *filetypes;
89 static int filetypes_count;
91 struct gui_synclist tree_lists;
93 /* I put it here because other files doesn't use it yet,
94 * but should be elsewhere since it will be used mostly everywhere */
95 #ifdef HAS_BUTTONBAR
96 struct gui_buttonbar tree_buttonbar;
97 #endif
98 static struct tree_context tc;
100 bool boot_changed = false;
102 char lastfile[MAX_PATH];
103 static char lastdir[MAX_PATH];
104 #ifdef HAVE_TAGCACHE
105 static int lasttable, lastextra, lastfirstpos;
106 #endif
107 static int max_files = 0;
109 static bool reload_dir = false;
111 static bool start_wps = false;
112 static int curr_context = false;/* id3db or tree*/
114 static int dirbrowse(void);
115 static int ft_play_dirname(char* name);
116 static void ft_play_filename(char *dir, char *file);
117 static void say_filetype(int attr);
119 static char * tree_get_filename(int selected_item, void * data, char *buffer)
121 struct tree_context * local_tc=(struct tree_context *)data;
122 char *name;
123 int attr=0;
124 bool stripit = false;
125 #ifdef HAVE_TAGCACHE
126 bool id3db = *(local_tc->dirfilter) == SHOW_ID3DB;
128 if (id3db)
130 return tagtree_get_entry(&tc, selected_item)->name;
132 else
133 #endif
135 struct entry* dc = local_tc->dircache;
136 struct entry* e = &dc[selected_item];
137 name = e->name;
138 attr = e->attr;
141 if(!(attr & ATTR_DIRECTORY))
143 switch(global_settings.show_filename_ext)
145 case 0:
146 /* show file extension: off */
147 stripit = true;
148 break;
149 case 1:
150 /* show file extension: on */
151 break;
152 case 2:
153 /* show file extension: only unknown types */
154 stripit = filetype_supported(attr);
155 break;
156 case 3:
157 default:
158 /* show file extension: only when viewing all */
159 stripit = (*(local_tc->dirfilter) != SHOW_ID3DB) &&
160 (*(local_tc->dirfilter) != SHOW_ALL);
161 break;
165 if(stripit)
167 return(strip_extension(name, buffer));
169 return(name);
172 #ifdef HAVE_LCD_COLOR
173 static int tree_get_filecolor(int selected_item, void * data)
175 if (*tc.dirfilter == SHOW_ID3DB)
176 return -1;
177 struct tree_context * local_tc=(struct tree_context *)data;
178 struct entry* dc = local_tc->dircache;
179 struct entry* e = &dc[selected_item];
180 return filetype_get_color(e->name, e->attr);
182 #endif
184 static int tree_get_fileicon(int selected_item, void * data)
186 struct tree_context * local_tc=(struct tree_context *)data;
187 #ifdef HAVE_TAGCACHE
188 bool id3db = *(local_tc->dirfilter) == SHOW_ID3DB;
189 if (id3db) {
190 return tagtree_get_icon(&tc);
192 else
193 #endif
195 struct entry* dc = local_tc->dircache;
196 struct entry* e = &dc[selected_item];
197 return filetype_get_icon(e->attr);
201 static int tree_voice_cb(int selected_item, void * data)
203 struct tree_context * local_tc=(struct tree_context *)data;
204 char *name;
205 int attr=0;
206 #ifdef HAVE_TAGCACHE
207 bool id3db = *(local_tc->dirfilter) == SHOW_ID3DB;
209 if (id3db)
211 attr = tagtree_get_attr(local_tc);
212 name = tagtree_get_entry(local_tc, selected_item)->name;
214 else
215 #endif
217 struct entry* dc = local_tc->dircache;
218 struct entry* e = &dc[selected_item];
219 name = e->name;
220 attr = e->attr;
222 bool is_dir = (attr & ATTR_DIRECTORY);
223 bool did_clip = false;
224 /* First the .talk clip case */
225 if(is_dir)
227 if(global_settings.talk_dir_clip)
229 DEBUGF("Playing directory thumbnail: %s", local_tc->currdir);
230 did_clip = true;
231 if(ft_play_dirname(name) <0)
232 /* failed, not existing */
233 did_clip = false;
235 } else { /* it's a file */
236 if (global_settings.talk_file_clip && (attr & FILE_ATTR_THUMBNAIL))
238 did_clip = true;
239 DEBUGF("Playing file thumbnail: %s/%s%s\n",
240 local_tc->currdir, name, file_thumbnail_ext);
241 ft_play_filename(local_tc->currdir, name);
244 if(!did_clip)
246 /* say the number or spell if required or as a fallback */
247 switch (is_dir ? global_settings.talk_dir : global_settings.talk_file)
249 case 1: /* as numbers */
250 talk_id(is_dir ? VOICE_DIR : VOICE_FILE, false);
251 talk_number(selected_item+1 - (is_dir ? 0 : local_tc->dirsindir),
252 true);
253 if(global_settings.talk_filetype
254 && !is_dir && *local_tc->dirfilter < NUM_FILTER_MODES)
255 say_filetype(attr);
256 break;
257 case 2: /* spelled */
258 talk_shutup();
259 if(global_settings.talk_filetype)
261 if(is_dir)
262 talk_id(VOICE_DIR, true);
263 else if(*local_tc->dirfilter < NUM_FILTER_MODES)
264 say_filetype(attr);
266 talk_spell(name, true);
267 break;
270 return 0;
273 bool check_rockboxdir(void)
275 DIR *dir = opendir(ROCKBOX_DIR);
276 if(!dir)
277 { /* No need to localise this message.
278 If .rockbox is missing, it wouldn't work anyway */
279 int i;
280 FOR_NB_SCREENS(i)
281 screens[i].clear_display();
282 gui_syncsplash(HZ*2, "No .rockbox directory");
283 FOR_NB_SCREENS(i)
284 screens[i].clear_display();
285 gui_syncsplash(HZ*2, "Installation incomplete");
286 return false;
288 closedir(dir);
289 return true;
292 /* do this really late in the init sequence */
293 void tree_gui_init(void)
295 gui_sync_wps_screen_init();
297 check_rockboxdir();
299 strcpy(tc.currdir, "/");
301 #ifdef HAVE_LCD_CHARCELLS
302 int i;
303 FOR_NB_SCREENS(i)
304 screens[i].double_height(false);
305 #endif
306 #ifdef HAS_BUTTONBAR
307 gui_buttonbar_init(&tree_buttonbar);
308 /* since archos only have one screen, no need to create more than that */
309 gui_buttonbar_set_display(&tree_buttonbar, &(screens[SCREEN_MAIN]) );
310 #endif
311 gui_synclist_init(&tree_lists, &tree_get_filename, &tc, false, 1);
312 gui_synclist_set_voice_callback(&tree_lists, tree_voice_cb);
313 gui_synclist_set_icon_callback(&tree_lists, &tree_get_fileicon);
314 #ifdef HAVE_LCD_COLOR
315 gui_list_set_color_callback(&tree_lists,
316 &tree_get_filecolor);
317 #endif
322 struct tree_context* tree_get_context(void)
324 return &tc;
328 * Returns the position of a given file in the current directory
329 * returns -1 if not found
331 static int tree_get_file_position(char * filename)
333 int i;
335 /* use lastfile to determine the selected item (default=0) */
336 for (i=0; i < tc.filesindir; i++)
338 struct entry* dc = tc.dircache;
339 struct entry* e = &dc[i];
340 if (!strcasecmp(e->name, filename))
341 return(i);
343 return(-1);/* no file can match, returns undefined */
347 * Called when a new dir is loaded (for example when returning from other apps ...)
348 * also completely redraws the tree
350 static int update_dir(void)
352 bool changed = false;
353 #ifdef HAVE_TAGCACHE
354 bool id3db = *tc.dirfilter == SHOW_ID3DB;
355 /* Checks for changes */
356 if (id3db) {
357 if (tc.currtable != lasttable ||
358 tc.currextra != lastextra ||
359 tc.firstpos != lastfirstpos ||
360 reload_dir)
362 if (tagtree_load(&tc) < 0)
363 return -1;
365 lasttable = tc.currtable;
366 lastextra = tc.currextra;
367 lastfirstpos = tc.firstpos;
368 changed = true;
371 else
372 #endif
374 /* if the tc.currdir has been changed, reload it ...*/
375 if (strncmp(tc.currdir, lastdir, sizeof(lastdir)) || reload_dir)
377 if (ft_load(&tc, NULL) < 0)
378 return -1;
379 strcpy(lastdir, tc.currdir);
380 changed = true;
383 /* if selected item is undefined */
384 if (tc.selected_item == -1)
386 /* use lastfile to determine the selected item */
387 tc.selected_item = tree_get_file_position(lastfile);
389 /* If the file doesn't exists, select the first one (default) */
390 if(tc.selected_item < 0)
391 tc.selected_item = 0;
392 changed = true;
394 if (changed)
397 #ifdef HAVE_TAGCACHE
398 !id3db &&
399 #endif
400 (tc.dirfull || tc.filesindir == global_settings.max_files_in_dir) )
402 gui_syncsplash(HZ, ID2P(LANG_SHOWDIR_BUFFER_FULL));
405 #ifdef HAVE_TAGCACHE
406 if (id3db)
408 #ifdef HAVE_LCD_BITMAP
409 if (global_settings.show_path_in_browser == SHOW_PATH_FULL
410 || global_settings.show_path_in_browser == SHOW_PATH_CURRENT)
412 gui_synclist_set_title(&tree_lists, tagtree_get_title(&tc),
413 filetype_get_icon(ATTR_DIRECTORY));
415 else
417 /* Must clear the title as the list is reused */
418 gui_synclist_set_title(&tree_lists, NULL, NOICON);
420 #endif
422 else
423 #endif
425 #ifdef HAVE_LCD_BITMAP
426 if (global_settings.show_path_in_browser &&
427 *(tc.dirfilter) == SHOW_PLUGINS)
429 char *title;
430 if (!strcmp(tc.currdir, PLUGIN_GAMES_DIR))
431 title = str(LANG_PLUGIN_GAMES);
432 else if (!strcmp(tc.currdir, PLUGIN_APPS_DIR))
433 title = str(LANG_PLUGIN_APPS);
434 else if (!strcmp(tc.currdir, PLUGIN_DEMOS_DIR))
435 title = str(LANG_PLUGIN_DEMOS);
436 else title = str(LANG_PLUGINS);
437 gui_synclist_set_title(&tree_lists, title, Icon_Plugin);
439 else if (global_settings.show_path_in_browser == SHOW_PATH_FULL)
441 gui_synclist_set_title(&tree_lists, tc.currdir,
442 filetype_get_icon(ATTR_DIRECTORY));
444 else if (global_settings.show_path_in_browser == SHOW_PATH_CURRENT)
446 char *title = strrchr(tc.currdir, '/') + 1;
447 if (*title == '\0')
449 /* Display "Files" for the root dir */
450 gui_synclist_set_title(&tree_lists, str(LANG_DIR_BROWSER),
451 filetype_get_icon(ATTR_DIRECTORY));
453 else
454 gui_synclist_set_title(&tree_lists, title,
455 filetype_get_icon(ATTR_DIRECTORY));
457 else
459 /* Must clear the title as the list is reused */
460 gui_synclist_set_title(&tree_lists, NULL, NOICON);
462 #endif
465 gui_synclist_set_nb_items(&tree_lists, tc.filesindir);
466 gui_synclist_set_icon_callback(&tree_lists, tree_get_fileicon);
467 if( tc.selected_item >= tc.filesindir)
468 tc.selected_item=tc.filesindir-1;
470 gui_synclist_select_item(&tree_lists, tc.selected_item);
471 #ifdef HAS_BUTTONBAR
472 if (global_settings.buttonbar) {
473 if (*tc.dirfilter < NUM_FILTER_MODES)
474 gui_buttonbar_set(&tree_buttonbar, str(LANG_SYSFONT_DIRBROWSE_F1),
475 str(LANG_SYSFONT_DIRBROWSE_F2),
476 str(LANG_SYSFONT_DIRBROWSE_F3));
477 else
478 gui_buttonbar_set(&tree_buttonbar, "<<<", "", "");
479 gui_buttonbar_draw(&tree_buttonbar);
481 #endif
482 gui_synclist_draw(&tree_lists);
483 gui_synclist_speak_item(&tree_lists);
484 gui_syncstatusbar_draw(&statusbars, true);
485 return tc.filesindir;
488 /* load tracks from specified directory to resume play */
489 void resume_directory(const char *dir)
491 #ifdef HAVE_TAGCACHE
492 bool id3db = *tc.dirfilter == SHOW_ID3DB;
493 #endif
495 if (ft_load(&tc, dir) < 0)
496 return;
497 lastdir[0] = 0;
499 ft_build_playlist(&tc, 0);
501 #ifdef HAVE_TAGCACHE
502 if (id3db)
503 tagtree_load(&tc);
504 #endif
507 /* Returns the current working directory and also writes cwd to buf if
508 non-NULL. In case of error, returns NULL. */
509 char *getcwd(char *buf, int size)
511 if (!buf)
512 return tc.currdir;
513 else if (size > 0)
515 strncpy(buf, tc.currdir, size);
516 return buf;
518 else
519 return NULL;
522 /* Force a reload of the directory next time directory browser is called */
523 void reload_directory(void)
525 reload_dir = true;
528 void get_current_file(char* buffer, int buffer_len)
530 #ifdef HAVE_TAGCACHE
531 /* in ID3DB mode it is a bad idea to call this function */
532 /* (only happens with `follow playlist') */
533 if( *tc.dirfilter == SHOW_ID3DB )
534 return;
535 #endif
537 struct entry* dc = tc.dircache;
538 struct entry* e = &dc[tc.selected_item];
539 snprintf(buffer, buffer_len, "%s/%s", getcwd(NULL,0),
540 tc.dirlength ? e->name : "");
543 /* Allow apps to change our dirfilter directly (required for sub browsers)
544 if they're suddenly going to become a file browser for example */
545 void set_dirfilter(int l_dirfilter)
547 *tc.dirfilter = l_dirfilter;
550 /* Selects a file and update tree context properly */
551 void set_current_file(char *path)
553 char *name;
554 int i;
556 #ifdef HAVE_TAGCACHE
557 /* in ID3DB mode it is a bad idea to call this function */
558 /* (only happens with `follow playlist') */
559 if( *tc.dirfilter == SHOW_ID3DB )
560 return;
561 #endif
563 /* separate directory from filename */
564 /* gets the directory's name and put it into tc.currdir */
565 name = strrchr(path+1,'/');
566 if (name)
568 *name = 0;
569 strcpy(tc.currdir, path);
570 *name = '/';
571 name++;
573 else
575 strcpy(tc.currdir, "/");
576 name = path+1;
579 strcpy(lastfile, name);
582 /* If we changed dir we must recalculate the dirlevel
583 and adjust the selected history properly */
584 if (strncmp(tc.currdir,lastdir,sizeof(lastdir)))
586 tc.dirlevel = 0;
587 tc.selected_item_history[tc.dirlevel] = -1;
589 /* use '/' to calculate dirlevel */
590 for (i = 1; path[i] != '\0'; i++)
592 if (path[i] == '/')
594 tc.dirlevel++;
595 tc.selected_item_history[tc.dirlevel] = -1;
599 if (ft_load(&tc, NULL) >= 0)
601 tc.selected_item = tree_get_file_position(lastfile);
606 /* main loop, handles key events */
607 static int dirbrowse()
609 int numentries=0;
610 char buf[MAX_PATH];
611 unsigned button, oldbutton;
612 bool reload_root = false;
613 int lastfilter = *tc.dirfilter;
614 bool lastsortcase = global_settings.sort_case;
615 bool exit_func = false;
617 char* currdir = tc.currdir; /* just a shortcut */
618 #ifdef HAVE_TAGCACHE
619 bool id3db = *tc.dirfilter == SHOW_ID3DB;
621 if (id3db)
622 curr_context=CONTEXT_ID3DB;
623 else
624 #endif
625 curr_context=CONTEXT_TREE;
626 if (tc.selected_item < 0)
627 tc.selected_item = 0;
628 #ifdef HAVE_TAGCACHE
629 tc.firstpos = 0;
630 lasttable = -1;
631 lastextra = -1;
632 lastfirstpos = 0;
633 #endif
635 start_wps = false;
636 numentries = update_dir();
637 reload_dir = false;
638 if (numentries == -1)
639 return GO_TO_PREVIOUS; /* currdir is not a directory */
641 if (*tc.dirfilter > NUM_FILTER_MODES && numentries==0)
643 gui_syncsplash(HZ*2, ID2P(LANG_NO_FILES));
644 return GO_TO_PREVIOUS; /* No files found for rockbox_browser() */
647 while(1) {
648 struct entry *dircache = tc.dircache;
649 bool restore = false;
650 if (tc.dirlevel < 0)
651 tc.dirlevel = 0; /* shouldnt be needed.. this code needs work! */
652 #ifdef BOOTFILE
653 if (boot_changed) {
654 char *lines[]={ID2P(LANG_BOOT_CHANGED), ID2P(LANG_REBOOT_NOW)};
655 struct text_message message={lines, 2};
656 if(gui_syncyesno_run(&message, NULL, NULL)==YESNO_YES)
657 rolo_load("/" BOOTFILE);
658 restore = true;
659 boot_changed = false;
661 #endif
662 button = get_action(CONTEXT_TREE,
663 list_do_action_timeout(&tree_lists, HZ/2));
664 oldbutton = button;
665 gui_synclist_do_button(&tree_lists, &button,LIST_WRAP_UNLESS_HELD);
666 tc.selected_item = gui_synclist_get_sel_pos(&tree_lists);
667 switch ( button ) {
668 case ACTION_STD_OK:
669 /* nothing to do if no files to display */
670 if ( numentries == 0 )
671 break;
673 #ifdef HAVE_TAGCACHE
674 switch (id3db?tagtree_enter(&tc):ft_enter(&tc))
675 #else
676 switch (ft_enter(&tc))
677 #endif
679 case 1: reload_dir = true; break;
680 case 2: start_wps = true; break;
681 case 3: exit_func = true; break;
682 default: break;
684 restore = true;
685 break;
687 case ACTION_STD_CANCEL:
688 if (*tc.dirfilter > NUM_FILTER_MODES && tc.dirlevel < 1) {
689 exit_func = true;
690 break;
692 if ((*tc.dirfilter == SHOW_ID3DB && tc.dirlevel == 0) ||
693 ((*tc.dirfilter != SHOW_ID3DB && !strcmp(currdir,"/"))))
695 #ifdef HAVE_LCD_BITMAP /* charcell doesnt have ACTION_TREE_PGLEFT so this isnt needed */
696 if (oldbutton == ACTION_TREE_PGLEFT)
697 break;
698 else
699 #endif
700 return GO_TO_ROOT;
703 #ifdef HAVE_TAGCACHE
704 if (id3db)
705 tagtree_exit(&tc);
706 else
707 #endif
708 if (ft_exit(&tc) == 3)
709 exit_func = true;
711 restore = true;
712 break;
714 case ACTION_TREE_STOP:
715 if (list_stop_handler())
716 restore = true;
717 break;
719 case ACTION_STD_MENU:
720 return GO_TO_ROOT;
721 break;
723 #ifdef HAVE_RECORDING
724 case ACTION_STD_REC:
725 return GO_TO_RECSCREEN;
726 #endif
728 case ACTION_TREE_WPS:
729 return GO_TO_PREVIOUS_MUSIC;
730 break;
731 #ifdef HAVE_QUICKSCREEN
732 case ACTION_STD_QUICKSCREEN:
733 /* don't enter f2 from plugin browser */
734 if (*tc.dirfilter < NUM_FILTER_MODES)
736 if (quick_screen_quick(button))
737 reload_dir = true;
738 restore = true;
740 break;
741 #endif
742 #ifdef BUTTON_F3
743 case ACTION_F3:
744 /* don't enter f3 from plugin browser */
745 if (*tc.dirfilter < NUM_FILTER_MODES)
747 if (quick_screen_f3(BUTTON_F3))
748 reload_dir = true;
749 restore = true;
751 break;
752 #endif
754 case ACTION_STD_CONTEXT:
756 int onplay_result;
757 int attr = 0;
759 if(!numentries)
760 onplay_result = onplay(NULL, 0, curr_context);
761 else {
762 #ifdef HAVE_TAGCACHE
763 if (id3db)
765 if (tagtree_get_attr(&tc) == FILE_ATTR_AUDIO)
767 attr = FILE_ATTR_AUDIO;
768 tagtree_get_filename(&tc, buf, sizeof(buf));
770 else
771 attr = ATTR_DIRECTORY;
773 else
774 #endif
776 attr = dircache[tc.selected_item].attr;
778 if (currdir[1]) /* Not in / */
779 snprintf(buf, sizeof buf, "%s/%s",
780 currdir,
781 dircache[tc.selected_item].name);
782 else /* In / */
783 snprintf(buf, sizeof buf, "/%s",
784 dircache[tc.selected_item].name);
786 onplay_result = onplay(buf, attr, curr_context);
788 switch (onplay_result)
790 case ONPLAY_MAINMENU:
791 return GO_TO_ROOT;
793 case ONPLAY_OK:
794 restore = true;
795 break;
797 case ONPLAY_RELOAD_DIR:
798 reload_dir = true;
799 break;
801 case ONPLAY_START_PLAY:
802 return GO_TO_WPS;
803 break;
805 break;
808 case ACTION_NONE:
809 gui_syncstatusbar_draw(&statusbars, false);
810 break;
812 #ifdef HAVE_HOTSWAP
813 case SYS_FS_CHANGED:
814 #ifdef HAVE_TAGCACHE
815 if (!id3db)
816 #endif
817 reload_dir = true;
818 /* The 'dir no longer valid' situation will be caught later
819 * by checking the showdir() result. */
820 break;
821 #endif
823 default:
824 if (default_event_handler(button) == SYS_USB_CONNECTED)
826 if(*tc.dirfilter > NUM_FILTER_MODES)
827 /* leave sub-browsers after usb, doing otherwise
828 might be confusing to the user */
829 exit_func = true;
830 else
831 reload_dir = true;
833 break;
835 if (start_wps)
836 return GO_TO_WPS;
837 if (button)
839 ata_spin();
843 check_rescan:
844 /* do we need to rescan dir? */
845 if (reload_dir || reload_root ||
846 lastfilter != *tc.dirfilter ||
847 lastsortcase != global_settings.sort_case)
849 if (reload_root) {
850 strcpy(currdir, "/");
851 tc.dirlevel = 0;
852 #ifdef HAVE_TAGCACHE
853 tc.currtable = 0;
854 tc.currextra = 0;
855 lasttable = -1;
856 lastextra = -1;
857 #endif
858 reload_root = false;
861 if (!reload_dir)
863 gui_synclist_select_item(&tree_lists, 0);
864 gui_synclist_draw(&tree_lists);
865 tc.selected_item = 0;
866 lastdir[0] = 0;
869 lastfilter = *tc.dirfilter;
870 lastsortcase = global_settings.sort_case;
871 restore = true;
874 if (exit_func)
875 return GO_TO_PREVIOUS;
877 if (restore || reload_dir) {
878 /* restore display */
879 numentries = update_dir();
880 reload_dir = false;
881 if (currdir[1] && (numentries < 0))
882 { /* not in root and reload failed */
883 reload_root = true; /* try root */
884 goto check_rescan;
888 return true;
891 static int plsize = 0;
892 static long pltick;
893 static bool add_dir(char* dirname, int len, int fd)
895 bool abort = false;
896 DIR* dir;
898 /* check for user abort */
899 if (action_userabort(TIMEOUT_NOBLOCK))
900 return true;
902 dir = opendir(dirname);
903 if(!dir)
904 return true;
906 while (true) {
907 struct dirent *entry;
909 entry = readdir(dir);
910 if (!entry)
911 break;
912 if (entry->attribute & ATTR_DIRECTORY) {
913 int dirlen = strlen(dirname);
914 bool result;
916 if (!strcmp((char *)entry->d_name, ".") ||
917 !strcmp((char *)entry->d_name, ".."))
918 continue;
920 if (dirname[1])
921 snprintf(dirname+dirlen, len-dirlen, "/%s", entry->d_name);
922 else
923 snprintf(dirname, len, "/%s", entry->d_name);
925 result = add_dir(dirname, len, fd);
926 dirname[dirlen] = '\0';
927 if (result) {
928 abort = true;
929 break;
932 else {
933 int x = strlen((char *)entry->d_name);
934 int i;
935 char *cp = strrchr((char *)entry->d_name,'.');
937 if (cp) {
938 cp++;
940 /* add all supported audio files to playlists */
941 for (i=0; i < filetypes_count; i++) {
942 if (filetypes[i].tree_attr == FILE_ATTR_AUDIO) {
943 if (!strcasecmp(cp, filetypes[i].extension)) {
944 char buf[8];
945 int i;
946 write(fd, dirname, strlen(dirname));
947 write(fd, "/", 1);
948 write(fd, entry->d_name, x);
949 write(fd, "\n", 1);
951 plsize++;
952 if(TIME_AFTER(current_tick, pltick+HZ/4)) {
953 pltick = current_tick;
955 snprintf(buf, sizeof buf, "%d", plsize);
956 #ifdef HAVE_LCD_BITMAP
957 FOR_NB_SCREENS(i)
959 screens[i].puts(0, 4, (unsigned char *)buf);
960 gui_textarea_update(&screens[i]);
962 #else
963 if (plsize > 999)
964 x=7;
965 else if (plsize > 99)
966 x=8;
967 else if (plsize > 9)
968 x=9;
969 else
970 x = 10;
972 FOR_NB_SCREENS(i) {
973 screens[i].puts(x,0,buf);
975 #endif
977 break;
984 closedir(dir);
986 return abort;
989 bool create_playlist(void)
991 int fd;
992 int i;
993 char filename[MAX_PATH];
995 pltick = current_tick;
997 snprintf(filename, sizeof filename, "%s.m3u8",
998 tc.currdir[1] ? tc.currdir : "/root");
999 FOR_NB_SCREENS(i)
1001 gui_textarea_clear(&screens[i]);
1002 screens[i].puts(0, 0, str(LANG_CREATING));
1003 screens[i].puts_scroll(0, 1, (unsigned char *)filename);
1004 #if defined(HAVE_LCD_BITMAP) || defined(SIMULATOR)
1005 gui_textarea_update(&screens[i]);
1006 #endif
1008 fd = creat(filename);
1009 if (fd < 0)
1010 return false;
1012 trigger_cpu_boost();
1014 snprintf(filename, sizeof(filename), "%s",
1015 tc.currdir[1] ? tc.currdir : "/");
1016 plsize = 0;
1017 add_dir(filename, sizeof(filename), fd);
1018 close(fd);
1020 cancel_cpu_boost();
1021 sleep(HZ);
1023 return true;
1026 int rockbox_browse(const char *root, int dirfilter)
1028 int ret_val = 0;
1029 int *last_filter = tc.dirfilter;
1030 tc.dirfilter = &dirfilter;
1032 reload_dir = true;
1033 if (dirfilter >= NUM_FILTER_MODES)
1035 static struct tree_context backup;
1036 int last_context;
1038 backup = tc;
1039 tc.selected_item = 0;
1040 tc.dirlevel = 0;
1041 memcpy(tc.currdir, root, sizeof(tc.currdir));
1042 start_wps = false;
1043 last_context = curr_context;
1045 ret_val = dirbrowse();
1046 tc = backup;
1047 curr_context = last_context;
1049 else
1051 static char buf[MAX_PATH];
1052 if (dirfilter != SHOW_ID3DB)
1053 tc.dirfilter = &global_settings.dirfilter;
1054 strcpy(buf,root);
1055 set_current_file(buf);
1056 ret_val = dirbrowse();
1058 tc.dirfilter = last_filter;
1059 return ret_val;
1062 void tree_mem_init(void)
1064 /* We copy the settings value in case it is changed by the user. We can't
1065 use it until the next reboot. */
1066 max_files = global_settings.max_files_in_dir;
1068 /* initialize tree context struct */
1069 memset(&tc, 0, sizeof(tc));
1070 tc.dirfilter = &global_settings.dirfilter;
1072 tc.name_buffer_size = AVERAGE_FILENAME_LENGTH * max_files;
1073 tc.name_buffer = buffer_alloc(tc.name_buffer_size);
1075 tc.dircache_size = max_files * sizeof(struct entry);
1076 tc.dircache = buffer_alloc(tc.dircache_size);
1077 tree_get_filetypes(&filetypes, &filetypes_count);
1080 void bookmark_play(char *resume_file, int index, int offset, int seed,
1081 char *filename)
1083 int i;
1084 char* suffix = strrchr(resume_file, '.');
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;
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);
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;
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;
1143 if (!strcmp(strrchr(peek_filename, '/') + 1, filename))
1144 break;
1146 if (i < playlist_amount())
1147 index = i;
1148 else
1150 /* File not found, so bail out. Maybe not the best
1151 * message; perhaps "Bookmarked file not found"?
1153 gui_syncsplash(HZ*2, ID2P(LANG_NOTHING_TO_RESUME));
1154 return;
1157 playlist_start(index,offset);
1161 start_wps=true;
1164 static void say_filetype(int attr)
1166 /* try to find a voice ID for the extension, if known */
1167 int j;
1168 attr &= FILE_ATTR_MASK; /* file type */
1169 for (j=0; j<filetypes_count; j++)
1170 if (attr == filetypes[j].tree_attr)
1172 talk_id(filetypes[j].voiceclip, true);
1173 return;
1177 static int ft_play_dirname(char* name)
1179 int fd;
1180 char dirname_mp3_filename[MAX_PATH+1];
1182 #if CONFIG_CODEC != SWCODEC
1183 if (audio_status() & AUDIO_STATUS_PLAY)
1184 return 0;
1185 #endif
1187 snprintf(dirname_mp3_filename, sizeof(dirname_mp3_filename), "%s/%s/%s",
1188 tc.currdir[1] ? tc.currdir : "" , name,
1189 dir_thumbnail_name);
1191 DEBUGF("Checking for %s\n", dirname_mp3_filename);
1193 fd = open(dirname_mp3_filename, O_RDONLY);
1194 if (fd < 0)
1196 DEBUGF("Failed to find: %s\n", dirname_mp3_filename);
1197 return -1;
1200 close(fd);
1202 DEBUGF("Found: %s\n", dirname_mp3_filename);
1204 talk_file(dirname_mp3_filename, false);
1205 if(global_settings.talk_filetype)
1206 talk_id(VOICE_DIR, true);
1207 return 1;
1210 static void ft_play_filename(char *dir, char *file)
1212 char name_mp3_filename[MAX_PATH+1];
1214 #if CONFIG_CODEC != SWCODEC
1215 if (audio_status() & AUDIO_STATUS_PLAY)
1216 return;
1217 #endif
1219 if (strcasecmp(&file[strlen(file) - strlen(file_thumbnail_ext)],
1220 file_thumbnail_ext))
1221 { /* file has no .talk extension */
1222 snprintf(name_mp3_filename, sizeof(name_mp3_filename),
1223 "%s/%s%s", dir, file, file_thumbnail_ext);
1225 talk_file(name_mp3_filename, false);
1227 else
1228 { /* it already is a .talk file, play this directly */
1229 snprintf(name_mp3_filename, sizeof(name_mp3_filename),
1230 "%s/%s", dir, file);
1231 talk_id(LANG_VOICE_DIR_HOVER, false); /* prefix it */
1232 talk_file(name_mp3_filename, true);
1236 /* These two functions are called by the USB and shutdown handlers */
1237 void tree_flush(void)
1239 scrobbler_shutdown();
1240 #ifdef HAVE_TAGCACHE
1241 tagcache_shutdown();
1242 #endif
1243 playlist_shutdown();
1245 #ifdef HAVE_TC_RAMCACHE
1246 tagcache_unload_ramcache();
1247 #endif
1249 #ifdef HAVE_DIRCACHE
1251 int old_val = global_status.dircache_size;
1252 if (global_settings.dircache)
1254 if (!dircache_is_initializing())
1255 global_status.dircache_size = dircache_get_cache_size();
1256 # ifdef HAVE_EEPROM_SETTINGS
1257 if (firmware_settings.initialized)
1258 dircache_save();
1259 # endif
1260 dircache_disable();
1262 else
1264 global_status.dircache_size = 0;
1266 if (old_val != global_status.dircache_size)
1267 status_save();
1269 #endif
1272 void tree_restore(void)
1274 #ifdef HAVE_EEPROM_SETTINGS
1275 firmware_settings.disk_clean = false;
1276 #endif
1278 #ifdef HAVE_TC_RAMCACHE
1279 remove(TAGCACHE_STATEFILE);
1280 #endif
1282 #ifdef HAVE_DIRCACHE
1283 remove(DIRCACHE_FILE);
1284 if (global_settings.dircache)
1286 /* Print "Scanning disk..." to the display. */
1287 int i;
1288 FOR_NB_SCREENS(i)
1290 screens[i].putsxy((LCD_WIDTH/2) -
1291 ((strlen(str(LANG_SCANNING_DISK)) *
1292 screens[i].char_width)/2),
1293 LCD_HEIGHT-screens[i].char_height*3,
1294 str(LANG_SCANNING_DISK));
1295 gui_textarea_update(&screens[i]);
1297 cond_talk_ids_fq(LANG_SCANNING_DISK);
1299 dircache_build(global_status.dircache_size);
1301 /* Clean the text when we are done. */
1302 FOR_NB_SCREENS(i)
1304 gui_textarea_clear(&screens[i]);
1307 #endif
1308 #ifdef HAVE_TAGCACHE
1309 tagcache_start_scan();
1310 #endif
1311 scrobbler_init();