Fix backlight timeout in 'keep backlight running' plugins (related to yesterday's...
[Rockbox.git] / apps / tree.c
blobb800833c7df9b8e8dc98663f52af10011cf6e0eb
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2002 Daniel Stenberg
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
19 #include <stdio.h>
20 #include <string.h>
21 #include <stdlib.h>
22 #include <stdbool.h>
24 #include "applimits.h"
25 #include "dir.h"
26 #include "file.h"
27 #include "lcd.h"
28 #include "font.h"
29 #include "backlight.h"
30 #include "button.h"
31 #include "kernel.h"
32 #include "usb.h"
33 #include "tree.h"
34 #include "sprintf.h"
35 #include "audio.h"
36 #include "playlist.h"
37 #include "menu.h"
38 #include "gwps.h"
39 #include "settings.h"
40 #include "status.h"
41 #include "debug.h"
42 #include "ata.h"
43 #include "rolo.h"
44 #include "icons.h"
45 #include "lang.h"
46 #include "language.h"
47 #include "screens.h"
48 #include "keyboard.h"
49 #include "bookmark.h"
50 #include "onplay.h"
51 #include "buffer.h"
52 #include "plugin.h"
53 #include "power.h"
54 #include "action.h"
55 #include "talk.h"
56 #include "filetypes.h"
57 #include "misc.h"
58 #include "filetree.h"
59 #include "tagtree.h"
60 #ifdef HAVE_RECORDING
61 #include "recorder/recording.h"
62 #endif
63 #include "rtc.h"
64 #include "dircache.h"
65 #ifdef HAVE_TAGCACHE
66 #include "tagcache.h"
67 #endif
68 #include "yesno.h"
69 #include "gwps-common.h"
70 #include "eeprom_settings.h"
71 #include "scrobbler.h"
73 /* gui api */
74 #include "list.h"
75 #include "statusbar.h"
76 #include "splash.h"
77 #include "buttonbar.h"
78 #include "textarea.h"
79 #include "action.h"
81 #include "root_menu.h"
83 #if (LCD_DEPTH > 1) || (defined(HAVE_LCD_REMOTE) && (LCD_REMOTE_DEPTH > 1))
84 #include "backdrop.h"
85 #endif
87 static const struct filetype *filetypes;
88 static int filetypes_count;
90 struct gui_synclist tree_lists;
92 /* I put it here because other files doesn't use it yet,
93 * but should be elsewhere since it will be used mostly everywhere */
94 #ifdef HAS_BUTTONBAR
95 struct gui_buttonbar tree_buttonbar;
96 #endif
97 static struct tree_context tc;
99 bool boot_changed = false;
101 char lastfile[MAX_PATH];
102 static char lastdir[MAX_PATH];
103 #ifdef HAVE_TAGCACHE
104 static int lasttable, lastextra, lastfirstpos;
105 #endif
106 static int max_files = 0;
108 static bool reload_dir = false;
110 static bool start_wps = false;
111 static int curr_context = false;/* id3db or tree*/
113 static int dirbrowse(void);
114 static int ft_play_dirname(char* name);
115 static void ft_play_filename(char *dir, char *file);
116 static void say_filetype(int attr);
119 * removes the extension of filename (if it doesn't start with a .)
120 * puts the result in buffer
122 static char * strip_extension(char * filename, char * buffer)
124 int dotpos;
125 char * dot=strrchr(filename, '.');
126 if(dot!=0 && filename[0]!='.')
128 dotpos = dot-filename;
129 strncpy(buffer, filename, dotpos);
130 buffer[dotpos]='\0';
131 return(buffer);
133 else
134 return(filename);
137 static char * tree_get_filename(int selected_item, void * data, char *buffer)
139 struct tree_context * local_tc=(struct tree_context *)data;
140 char *name;
141 int attr=0;
142 bool stripit = false;
143 #ifdef HAVE_TAGCACHE
144 bool id3db = *(local_tc->dirfilter) == SHOW_ID3DB;
146 if (id3db)
148 return tagtree_get_entry(&tc, selected_item)->name;
150 else
151 #endif
153 struct entry* dc = local_tc->dircache;
154 struct entry* e = &dc[selected_item];
155 name = e->name;
156 attr = e->attr;
159 if(!(attr & ATTR_DIRECTORY))
161 switch(global_settings.show_filename_ext)
163 case 0:
164 /* show file extension: off */
165 stripit = true;
166 break;
167 case 1:
168 /* show file extension: on */
169 break;
170 case 2:
171 /* show file extension: only unknown types */
172 stripit = filetype_supported(attr);
173 break;
174 case 3:
175 default:
176 /* show file extension: only when viewing all */
177 stripit = (*(local_tc->dirfilter) != SHOW_ID3DB) &&
178 (*(local_tc->dirfilter) != SHOW_ALL);
179 break;
183 if(stripit)
185 return(strip_extension(name, buffer));
187 return(name);
190 #ifdef HAVE_LCD_COLOR
191 static int tree_get_filecolor(int selected_item, void * data)
193 if (*tc.dirfilter == SHOW_ID3DB)
194 return -1;
195 struct tree_context * local_tc=(struct tree_context *)data;
196 struct entry* dc = local_tc->dircache;
197 struct entry* e = &dc[selected_item];
198 return filetype_get_color(e->name, e->attr);
200 #endif
202 static int tree_get_fileicon(int selected_item, void * data)
204 struct tree_context * local_tc=(struct tree_context *)data;
205 #ifdef HAVE_TAGCACHE
206 bool id3db = *(local_tc->dirfilter) == SHOW_ID3DB;
207 if (id3db) {
208 return tagtree_get_icon(&tc);
210 else
211 #endif
213 struct entry* dc = local_tc->dircache;
214 struct entry* e = &dc[selected_item];
215 return filetype_get_icon(e->attr);
219 static int tree_voice_cb(int selected_item, void * data)
221 struct tree_context * local_tc=(struct tree_context *)data;
222 char *name;
223 int attr=0;
224 #ifdef HAVE_TAGCACHE
225 bool id3db = *(local_tc->dirfilter) == SHOW_ID3DB;
227 if (id3db)
229 attr = tagtree_get_attr(local_tc);
230 name = tagtree_get_entry(local_tc, selected_item)->name;
232 else
233 #endif
235 struct entry* dc = local_tc->dircache;
236 struct entry* e = &dc[selected_item];
237 name = e->name;
238 attr = e->attr;
240 bool is_dir = (attr & ATTR_DIRECTORY);
241 bool did_clip = false;
242 /* First the .talk clip case */
243 if(is_dir)
245 if(global_settings.talk_dir_clip)
247 DEBUGF("Playing directory thumbnail: %s", local_tc->currdir);
248 did_clip = true;
249 if(ft_play_dirname(name) <0)
250 /* failed, not existing */
251 did_clip = false;
253 } else { /* it's a file */
254 if (global_settings.talk_file_clip && (attr & FILE_ATTR_THUMBNAIL))
256 did_clip = true;
257 DEBUGF("Playing file thumbnail: %s/%s%s\n",
258 local_tc->currdir, name, file_thumbnail_ext);
259 ft_play_filename(local_tc->currdir, name);
262 if(!did_clip)
264 /* say the number or spell if required or as a fallback */
265 switch (is_dir ? global_settings.talk_dir : global_settings.talk_file)
267 case 1: /* as numbers */
268 talk_id(is_dir ? VOICE_DIR : VOICE_FILE, false);
269 talk_number(selected_item+1 - (is_dir ? 0 : local_tc->dirsindir),
270 true);
271 if(global_settings.talk_filetype
272 && !is_dir && *local_tc->dirfilter < NUM_FILTER_MODES)
273 say_filetype(attr);
274 break;
275 case 2: /* spelled */
276 talk_shutup();
277 if(global_settings.talk_filetype)
279 if(is_dir)
280 talk_id(VOICE_DIR, true);
281 else if(*local_tc->dirfilter < NUM_FILTER_MODES)
282 say_filetype(attr);
284 talk_spell(name, true);
285 break;
288 return 0;
291 bool check_rockboxdir(void)
293 DIR *dir = opendir(ROCKBOX_DIR);
294 if(!dir)
295 { /* No need to localise this message.
296 If .rockbox is missing, it wouldn't work anyway */
297 int i;
298 FOR_NB_SCREENS(i)
299 screens[i].clear_display();
300 gui_syncsplash(HZ*2, "No .rockbox directory");
301 FOR_NB_SCREENS(i)
302 screens[i].clear_display();
303 gui_syncsplash(HZ*2, "Installation incomplete");
304 return false;
306 closedir(dir);
307 return true;
310 /* do this really late in the init sequence */
311 void tree_gui_init(void)
313 gui_sync_wps_screen_init();
315 check_rockboxdir();
317 strcpy(tc.currdir, "/");
319 #ifdef HAVE_LCD_CHARCELLS
320 int i;
321 FOR_NB_SCREENS(i)
322 screens[i].double_height(false);
323 #endif
324 #ifdef HAS_BUTTONBAR
325 gui_buttonbar_init(&tree_buttonbar);
326 /* since archos only have one screen, no need to create more than that */
327 gui_buttonbar_set_display(&tree_buttonbar, &(screens[SCREEN_MAIN]) );
328 #endif
329 gui_synclist_init(&tree_lists, &tree_get_filename, &tc, false, 1);
330 gui_synclist_set_voice_callback(&tree_lists, tree_voice_cb);
331 gui_synclist_set_icon_callback(&tree_lists, &tree_get_fileicon);
332 #ifdef HAVE_LCD_COLOR
333 gui_list_set_color_callback(&tree_lists.gui_list[SCREEN_MAIN],
334 &tree_get_filecolor);
335 #endif
340 struct tree_context* tree_get_context(void)
342 return &tc;
346 * Returns the position of a given file in the current directory
347 * returns -1 if not found
349 static int tree_get_file_position(char * filename)
351 int i;
353 /* use lastfile to determine the selected item (default=0) */
354 for (i=0; i < tc.filesindir; i++)
356 struct entry* dc = tc.dircache;
357 struct entry* e = &dc[i];
358 if (!strcasecmp(e->name, filename))
359 return(i);
361 return(-1);/* no file can match, returns undefined */
365 * Called when a new dir is loaded (for example when returning from other apps ...)
366 * also completely redraws the tree
368 static int update_dir(void)
370 bool changed = false;
371 #ifdef HAVE_TAGCACHE
372 bool id3db = *tc.dirfilter == SHOW_ID3DB;
373 /* Checks for changes */
374 if (id3db) {
375 if (tc.currtable != lasttable ||
376 tc.currextra != lastextra ||
377 tc.firstpos != lastfirstpos ||
378 reload_dir)
380 if (tagtree_load(&tc) < 0)
381 return -1;
383 lasttable = tc.currtable;
384 lastextra = tc.currextra;
385 lastfirstpos = tc.firstpos;
386 changed = true;
389 else
390 #endif
392 /* if the tc.currdir has been changed, reload it ...*/
393 if (strncmp(tc.currdir, lastdir, sizeof(lastdir)) || reload_dir)
395 if (ft_load(&tc, NULL) < 0)
396 return -1;
397 strcpy(lastdir, tc.currdir);
398 changed = true;
401 /* if selected item is undefined */
402 if (tc.selected_item == -1)
404 /* use lastfile to determine the selected item */
405 tc.selected_item = tree_get_file_position(lastfile);
407 /* If the file doesn't exists, select the first one (default) */
408 if(tc.selected_item < 0)
409 tc.selected_item = 0;
410 changed = true;
412 if (changed)
415 #ifdef HAVE_TAGCACHE
416 !id3db &&
417 #endif
418 (tc.dirfull || tc.filesindir == global_settings.max_files_in_dir) )
420 gui_syncsplash(HZ, ID2P(LANG_SHOWDIR_BUFFER_FULL));
423 #ifdef HAVE_TAGCACHE
424 if (id3db)
426 if (global_settings.show_path_in_browser == SHOW_PATH_FULL
427 || global_settings.show_path_in_browser == SHOW_PATH_CURRENT)
429 gui_synclist_set_title(&tree_lists, tagtree_get_title(&tc),
430 filetype_get_icon(ATTR_DIRECTORY));
432 else
434 /* Must clear the title as the list is reused */
435 gui_synclist_set_title(&tree_lists, NULL, NOICON);
438 else
439 #endif
441 if (global_settings.show_path_in_browser &&
442 *(tc.dirfilter) == SHOW_PLUGINS)
444 char *title;
445 if (!strcmp(tc.currdir, PLUGIN_GAMES_DIR))
446 title = str(LANG_PLUGIN_GAMES);
447 else if (!strcmp(tc.currdir, PLUGIN_APPS_DIR))
448 title = str(LANG_PLUGIN_APPS);
449 else if (!strcmp(tc.currdir, PLUGIN_DEMOS_DIR))
450 title = str(LANG_PLUGIN_DEMOS);
451 else title = str(LANG_PLUGINS);
452 gui_synclist_set_title(&tree_lists, title, Icon_Plugin);
454 else if (global_settings.show_path_in_browser == SHOW_PATH_FULL)
456 gui_synclist_set_title(&tree_lists, tc.currdir,
457 filetype_get_icon(ATTR_DIRECTORY));
459 else if (global_settings.show_path_in_browser == SHOW_PATH_CURRENT)
461 char *title = strrchr(tc.currdir, '/') + 1;
462 if (*title == '\0')
464 /* Display "Files" for the root dir */
465 gui_synclist_set_title(&tree_lists, str(LANG_DIR_BROWSER),
466 filetype_get_icon(ATTR_DIRECTORY));
468 else
469 gui_synclist_set_title(&tree_lists, title,
470 filetype_get_icon(ATTR_DIRECTORY));
472 else
474 /* Must clear the title as the list is reused */
475 gui_synclist_set_title(&tree_lists, NULL, NOICON);
479 gui_synclist_set_nb_items(&tree_lists, tc.filesindir);
480 gui_synclist_set_icon_callback(&tree_lists, tree_get_fileicon);
481 if( tc.selected_item >= tc.filesindir)
482 tc.selected_item=tc.filesindir-1;
484 gui_synclist_select_item(&tree_lists, tc.selected_item);
485 #ifdef HAS_BUTTONBAR
486 if (global_settings.buttonbar) {
487 if (*tc.dirfilter < NUM_FILTER_MODES)
488 gui_buttonbar_set(&tree_buttonbar, str(LANG_SYSFONT_DIRBROWSE_F1),
489 str(LANG_SYSFONT_DIRBROWSE_F2),
490 str(LANG_SYSFONT_DIRBROWSE_F3));
491 else
492 gui_buttonbar_set(&tree_buttonbar, "<<<", "", "");
493 gui_buttonbar_draw(&tree_buttonbar);
495 #endif
496 gui_synclist_draw(&tree_lists);
497 gui_synclist_speak_item(&tree_lists);
498 gui_syncstatusbar_draw(&statusbars, true);
499 return tc.filesindir;
502 /* load tracks from specified directory to resume play */
503 void resume_directory(const char *dir)
505 #ifdef HAVE_TAGCACHE
506 bool id3db = *tc.dirfilter == SHOW_ID3DB;
507 #endif
509 if (ft_load(&tc, dir) < 0)
510 return;
511 lastdir[0] = 0;
513 ft_build_playlist(&tc, 0);
515 #ifdef HAVE_TAGCACHE
516 if (id3db)
517 tagtree_load(&tc);
518 #endif
521 /* Returns the current working directory and also writes cwd to buf if
522 non-NULL. In case of error, returns NULL. */
523 char *getcwd(char *buf, int size)
525 if (!buf)
526 return tc.currdir;
527 else if (size > 0)
529 strncpy(buf, tc.currdir, size);
530 return buf;
532 else
533 return NULL;
536 /* Force a reload of the directory next time directory browser is called */
537 void reload_directory(void)
539 reload_dir = true;
542 void get_current_file(char* buffer, int buffer_len)
544 #ifdef HAVE_TAGCACHE
545 /* in ID3DB mode it is a bad idea to call this function */
546 /* (only happens with `follow playlist') */
547 if( *tc.dirfilter == SHOW_ID3DB )
548 return;
549 #endif
551 struct entry* dc = tc.dircache;
552 struct entry* e = &dc[tc.selected_item];
553 snprintf(buffer, buffer_len, "%s/%s", getcwd(NULL,0),
554 tc.dirlength ? e->name : "");
557 /* Allow apps to change our dirfilter directly (required for sub browsers)
558 if they're suddenly going to become a file browser for example */
559 void set_dirfilter(int l_dirfilter)
561 *tc.dirfilter = l_dirfilter;
564 /* Selects a file and update tree context properly */
565 void set_current_file(char *path)
567 char *name;
568 int i;
570 #ifdef HAVE_TAGCACHE
571 /* in ID3DB mode it is a bad idea to call this function */
572 /* (only happens with `follow playlist') */
573 if( *tc.dirfilter == SHOW_ID3DB )
574 return;
575 #endif
577 /* separate directory from filename */
578 /* gets the directory's name and put it into tc.currdir */
579 name = strrchr(path+1,'/');
580 if (name)
582 *name = 0;
583 strcpy(tc.currdir, path);
584 *name = '/';
585 name++;
587 else
589 strcpy(tc.currdir, "/");
590 name = path+1;
593 strcpy(lastfile, name);
596 /* If we changed dir we must recalculate the dirlevel
597 and adjust the selected history properly */
598 if (strncmp(tc.currdir,lastdir,sizeof(lastdir)))
600 tc.dirlevel = 0;
601 tc.selected_item_history[tc.dirlevel] = -1;
603 /* use '/' to calculate dirlevel */
604 for (i = 1; path[i] != '\0'; i++)
606 if (path[i] == '/')
608 tc.dirlevel++;
609 tc.selected_item_history[tc.dirlevel] = -1;
613 if (ft_load(&tc, NULL) >= 0)
615 tc.selected_item = tree_get_file_position(lastfile);
620 /* main loop, handles key events */
621 static int dirbrowse()
623 int numentries=0;
624 char buf[MAX_PATH];
625 unsigned button, oldbutton;
626 bool reload_root = false;
627 int lastfilter = *tc.dirfilter;
628 bool lastsortcase = global_settings.sort_case;
629 bool exit_func = false;
631 char* currdir = tc.currdir; /* just a shortcut */
632 #ifdef HAVE_TAGCACHE
633 bool id3db = *tc.dirfilter == SHOW_ID3DB;
635 if (id3db)
636 curr_context=CONTEXT_ID3DB;
637 else
638 #endif
639 curr_context=CONTEXT_TREE;
640 if (tc.selected_item < 0)
641 tc.selected_item = 0;
642 #ifdef HAVE_TAGCACHE
643 tc.firstpos = 0;
644 lasttable = -1;
645 lastextra = -1;
646 lastfirstpos = 0;
647 #endif
649 start_wps = false;
650 numentries = update_dir();
651 reload_dir = false;
652 if (numentries == -1)
653 return GO_TO_PREVIOUS; /* currdir is not a directory */
655 if (*tc.dirfilter > NUM_FILTER_MODES && numentries==0)
657 gui_syncsplash(HZ*2, ID2P(LANG_NO_FILES));
658 return GO_TO_PREVIOUS; /* No files found for rockbox_browser() */
661 while(1) {
662 struct entry *dircache = tc.dircache;
663 bool restore = false;
664 if (tc.dirlevel < 0)
665 tc.dirlevel = 0; /* shouldnt be needed.. this code needs work! */
666 #ifdef BOOTFILE
667 if (boot_changed) {
668 char *lines[]={ID2P(LANG_BOOT_CHANGED), ID2P(LANG_REBOOT_NOW)};
669 struct text_message message={lines, 2};
670 if(gui_syncyesno_run(&message, NULL, NULL)==YESNO_YES)
671 rolo_load("/" BOOTFILE);
672 restore = true;
673 boot_changed = false;
675 #endif
676 button = get_action(CONTEXT_TREE,
677 list_do_action_timeout(&tree_lists, HZ/2));
678 oldbutton = button;
679 gui_synclist_do_button(&tree_lists, &button,LIST_WRAP_UNLESS_HELD);
680 tc.selected_item = gui_synclist_get_sel_pos(&tree_lists);
681 switch ( button ) {
682 case ACTION_STD_OK:
683 /* nothing to do if no files to display */
684 if ( numentries == 0 )
685 break;
687 #ifdef HAVE_TAGCACHE
688 switch (id3db?tagtree_enter(&tc):ft_enter(&tc))
689 #else
690 switch (ft_enter(&tc))
691 #endif
693 case 1: reload_dir = true; break;
694 case 2: start_wps = true; break;
695 case 3: exit_func = true; break;
696 default: break;
698 restore = true;
699 break;
701 case ACTION_STD_CANCEL:
702 if (*tc.dirfilter > NUM_FILTER_MODES && tc.dirlevel < 1) {
703 exit_func = true;
704 break;
706 if ((*tc.dirfilter == SHOW_ID3DB && tc.dirlevel == 0) ||
707 ((*tc.dirfilter != SHOW_ID3DB && !strcmp(currdir,"/"))))
709 #ifdef HAVE_LCD_BITMAP /* charcell doesnt have ACTION_TREE_PGLEFT so this isnt needed */
710 if (oldbutton == ACTION_TREE_PGLEFT)
711 break;
712 else
713 #endif
714 return GO_TO_ROOT;
717 #ifdef HAVE_TAGCACHE
718 if (id3db)
719 tagtree_exit(&tc);
720 else
721 #endif
722 if (ft_exit(&tc) == 3)
723 exit_func = true;
725 restore = true;
726 break;
728 case ACTION_TREE_STOP:
729 if (list_stop_handler())
730 restore = true;
731 break;
733 case ACTION_STD_MENU:
734 return GO_TO_ROOT;
735 break;
737 #ifdef HAVE_RECORDING
738 case ACTION_STD_REC:
739 return GO_TO_RECSCREEN;
740 #endif
742 case ACTION_TREE_WPS:
743 return GO_TO_PREVIOUS_MUSIC;
744 break;
745 #ifdef HAVE_QUICKSCREEN
746 case ACTION_STD_QUICKSCREEN:
747 /* don't enter f2 from plugin browser */
748 if (*tc.dirfilter < NUM_FILTER_MODES)
750 if (quick_screen_quick(button))
751 reload_dir = true;
752 restore = true;
754 break;
755 #endif
756 #ifdef BUTTON_F3
757 case ACTION_F3:
758 /* don't enter f3 from plugin browser */
759 if (*tc.dirfilter < NUM_FILTER_MODES)
761 if (quick_screen_f3(BUTTON_F3))
762 reload_dir = true;
763 restore = true;
765 break;
766 #endif
768 case ACTION_STD_CONTEXT:
770 int onplay_result;
771 int attr = 0;
773 if(!numentries)
774 onplay_result = onplay(NULL, 0, curr_context);
775 else {
776 #ifdef HAVE_TAGCACHE
777 if (id3db)
779 if (tagtree_get_attr(&tc) == FILE_ATTR_AUDIO)
781 attr = FILE_ATTR_AUDIO;
782 tagtree_get_filename(&tc, buf, sizeof(buf));
784 else
785 attr = ATTR_DIRECTORY;
787 else
788 #endif
790 attr = dircache[tc.selected_item].attr;
792 if (currdir[1]) /* Not in / */
793 snprintf(buf, sizeof buf, "%s/%s",
794 currdir,
795 dircache[tc.selected_item].name);
796 else /* In / */
797 snprintf(buf, sizeof buf, "/%s",
798 dircache[tc.selected_item].name);
800 onplay_result = onplay(buf, attr, curr_context);
802 switch (onplay_result)
804 case ONPLAY_MAINMENU:
805 return GO_TO_ROOT;
807 case ONPLAY_OK:
808 restore = true;
809 break;
811 case ONPLAY_RELOAD_DIR:
812 reload_dir = true;
813 break;
815 case ONPLAY_START_PLAY:
816 return GO_TO_WPS;
817 break;
819 break;
822 case ACTION_NONE:
823 gui_syncstatusbar_draw(&statusbars, false);
824 break;
826 #ifdef HAVE_HOTSWAP
827 case SYS_FS_CHANGED:
828 #ifdef HAVE_TAGCACHE
829 if (!id3db)
830 #endif
831 reload_dir = true;
832 /* The 'dir no longer valid' situation will be caught later
833 * by checking the showdir() result. */
834 break;
835 #endif
837 default:
838 if (default_event_handler(button) == SYS_USB_CONNECTED)
840 if(*tc.dirfilter > NUM_FILTER_MODES)
841 /* leave sub-browsers after usb, doing otherwise
842 might be confusing to the user */
843 exit_func = true;
844 else
845 reload_dir = true;
847 break;
849 if (start_wps)
850 return GO_TO_WPS;
851 if (button)
853 ata_spin();
857 check_rescan:
858 /* do we need to rescan dir? */
859 if (reload_dir || reload_root ||
860 lastfilter != *tc.dirfilter ||
861 lastsortcase != global_settings.sort_case)
863 if (reload_root) {
864 strcpy(currdir, "/");
865 tc.dirlevel = 0;
866 #ifdef HAVE_TAGCACHE
867 tc.currtable = 0;
868 tc.currextra = 0;
869 lasttable = -1;
870 lastextra = -1;
871 #endif
872 reload_root = false;
875 if (!reload_dir)
877 gui_synclist_select_item(&tree_lists, 0);
878 gui_synclist_draw(&tree_lists);
879 tc.selected_item = 0;
880 lastdir[0] = 0;
883 lastfilter = *tc.dirfilter;
884 lastsortcase = global_settings.sort_case;
885 restore = true;
888 if (exit_func)
889 return GO_TO_PREVIOUS;
891 if (restore || reload_dir) {
892 /* restore display */
893 numentries = update_dir();
894 reload_dir = false;
895 if (currdir[1] && (numentries < 0))
896 { /* not in root and reload failed */
897 reload_root = true; /* try root */
898 goto check_rescan;
902 return true;
905 static int plsize = 0;
906 static long pltick;
907 static bool add_dir(char* dirname, int len, int fd)
909 bool abort = false;
910 DIR* dir;
912 /* check for user abort */
913 if (action_userabort(TIMEOUT_NOBLOCK))
914 return true;
916 dir = opendir(dirname);
917 if(!dir)
918 return true;
920 while (true) {
921 struct dirent *entry;
923 entry = readdir(dir);
924 if (!entry)
925 break;
926 if (entry->attribute & ATTR_DIRECTORY) {
927 int dirlen = strlen(dirname);
928 bool result;
930 if (!strcmp((char *)entry->d_name, ".") ||
931 !strcmp((char *)entry->d_name, ".."))
932 continue;
934 if (dirname[1])
935 snprintf(dirname+dirlen, len-dirlen, "/%s", entry->d_name);
936 else
937 snprintf(dirname, len, "/%s", entry->d_name);
939 result = add_dir(dirname, len, fd);
940 dirname[dirlen] = '\0';
941 if (result) {
942 abort = true;
943 break;
946 else {
947 int x = strlen((char *)entry->d_name);
948 int i;
949 char *cp = strrchr((char *)entry->d_name,'.');
951 if (cp) {
952 cp++;
954 /* add all supported audio files to playlists */
955 for (i=0; i < filetypes_count; i++) {
956 if (filetypes[i].tree_attr == FILE_ATTR_AUDIO) {
957 if (!strcasecmp(cp, filetypes[i].extension)) {
958 char buf[8];
959 int i;
960 write(fd, dirname, strlen(dirname));
961 write(fd, "/", 1);
962 write(fd, entry->d_name, x);
963 write(fd, "\n", 1);
965 plsize++;
966 if(TIME_AFTER(current_tick, pltick+HZ/4)) {
967 pltick = current_tick;
969 snprintf(buf, sizeof buf, "%d", plsize);
970 #ifdef HAVE_LCD_BITMAP
971 FOR_NB_SCREENS(i)
973 screens[i].puts(0, 4, (unsigned char *)buf);
974 gui_textarea_update(&screens[i]);
976 #else
977 if (plsize > 999)
978 x=7;
979 else if (plsize > 99)
980 x=8;
981 else if (plsize > 9)
982 x=9;
983 else
984 x = 10;
986 FOR_NB_SCREENS(i) {
987 screens[i].puts(x,0,buf);
989 #endif
991 break;
998 closedir(dir);
1000 return abort;
1003 bool create_playlist(void)
1005 int fd;
1006 int i;
1007 char filename[MAX_PATH];
1009 pltick = current_tick;
1011 snprintf(filename, sizeof filename, "%s.m3u8",
1012 tc.currdir[1] ? tc.currdir : "/root");
1013 FOR_NB_SCREENS(i)
1015 gui_textarea_clear(&screens[i]);
1016 screens[i].puts(0, 0, str(LANG_CREATING));
1017 screens[i].puts_scroll(0, 1, (unsigned char *)filename);
1018 #if defined(HAVE_LCD_BITMAP) || defined(SIMULATOR)
1019 gui_textarea_update(&screens[i]);
1020 #endif
1022 fd = creat(filename);
1023 if (fd < 0)
1024 return false;
1026 trigger_cpu_boost();
1028 snprintf(filename, sizeof(filename), "%s",
1029 tc.currdir[1] ? tc.currdir : "/");
1030 plsize = 0;
1031 add_dir(filename, sizeof(filename), fd);
1032 close(fd);
1034 cancel_cpu_boost();
1035 sleep(HZ);
1037 return true;
1040 int rockbox_browse(const char *root, int dirfilter)
1042 int ret_val = 0;
1043 int *last_filter = tc.dirfilter;
1044 tc.dirfilter = &dirfilter;
1046 reload_dir = true;
1047 if (dirfilter >= NUM_FILTER_MODES)
1049 static struct tree_context backup;
1050 int last_context;
1052 backup = tc;
1053 tc.selected_item = 0;
1054 tc.dirlevel = 0;
1055 memcpy(tc.currdir, root, sizeof(tc.currdir));
1056 start_wps = false;
1057 last_context = curr_context;
1059 ret_val = dirbrowse();
1060 tc = backup;
1061 curr_context = last_context;
1063 else
1065 static char buf[MAX_PATH];
1066 if (dirfilter != SHOW_ID3DB)
1067 tc.dirfilter = &global_settings.dirfilter;
1068 strcpy(buf,root);
1069 set_current_file(buf);
1070 ret_val = dirbrowse();
1072 tc.dirfilter = last_filter;
1073 return ret_val;
1076 void tree_mem_init(void)
1078 /* We copy the settings value in case it is changed by the user. We can't
1079 use it until the next reboot. */
1080 max_files = global_settings.max_files_in_dir;
1082 /* initialize tree context struct */
1083 memset(&tc, 0, sizeof(tc));
1084 tc.dirfilter = &global_settings.dirfilter;
1086 tc.name_buffer_size = AVERAGE_FILENAME_LENGTH * max_files;
1087 tc.name_buffer = buffer_alloc(tc.name_buffer_size);
1089 tc.dircache_size = max_files * sizeof(struct entry);
1090 tc.dircache = buffer_alloc(tc.dircache_size);
1091 tree_get_filetypes(&filetypes, &filetypes_count);
1094 void bookmark_play(char *resume_file, int index, int offset, int seed,
1095 char *filename)
1097 int i;
1098 char* suffix = strrchr(resume_file, '.');
1100 if (suffix != NULL &&
1101 (!strcasecmp(suffix, ".m3u") || !strcasecmp(suffix, ".m3u8")))
1103 /* Playlist playback */
1104 char* slash;
1105 /* check that the file exists */
1106 int fd = open(resume_file, O_RDONLY);
1107 if(fd<0)
1108 return;
1109 close(fd);
1111 slash = strrchr(resume_file,'/');
1112 if (slash)
1114 char* cp;
1115 *slash=0;
1117 cp=resume_file;
1118 if (!cp[0])
1119 cp="/";
1121 if (playlist_create(cp, slash+1) != -1)
1123 if (global_settings.playlist_shuffle)
1124 playlist_shuffle(seed, -1);
1125 playlist_start(index,offset);
1127 *slash='/';
1130 else
1132 /* Directory playback */
1133 lastdir[0]='\0';
1134 if (playlist_create(resume_file, NULL) != -1)
1136 char* peek_filename;
1137 resume_directory(resume_file);
1138 if (global_settings.playlist_shuffle)
1139 playlist_shuffle(seed, -1);
1141 /* Check if the file is at the same spot in the directory,
1142 else search for it */
1143 peek_filename = playlist_peek(index);
1145 if (peek_filename == NULL)
1146 return;
1148 if (strcmp(strrchr(peek_filename, '/') + 1, filename))
1150 for ( i=0; i < playlist_amount(); i++ )
1152 peek_filename = playlist_peek(i);
1154 if (peek_filename == NULL)
1155 return;
1157 if (!strcmp(strrchr(peek_filename, '/') + 1, filename))
1158 break;
1160 if (i < playlist_amount())
1161 index = i;
1162 else
1163 return;
1165 playlist_start(index,offset);
1169 start_wps=true;
1172 static void say_filetype(int attr)
1174 /* try to find a voice ID for the extension, if known */
1175 int j;
1176 attr &= FILE_ATTR_MASK; /* file type */
1177 for (j=0; j<filetypes_count; j++)
1178 if (attr == filetypes[j].tree_attr)
1180 talk_id(filetypes[j].voiceclip, true);
1181 return;
1185 static int ft_play_dirname(char* name)
1187 int fd;
1188 char dirname_mp3_filename[MAX_PATH+1];
1190 #if CONFIG_CODEC != SWCODEC
1191 if (audio_status() & AUDIO_STATUS_PLAY)
1192 return 0;
1193 #endif
1195 snprintf(dirname_mp3_filename, sizeof(dirname_mp3_filename), "%s/%s/%s",
1196 tc.currdir[1] ? tc.currdir : "" , name,
1197 dir_thumbnail_name);
1199 DEBUGF("Checking for %s\n", dirname_mp3_filename);
1201 fd = open(dirname_mp3_filename, O_RDONLY);
1202 if (fd < 0)
1204 DEBUGF("Failed to find: %s\n", dirname_mp3_filename);
1205 return -1;
1208 close(fd);
1210 DEBUGF("Found: %s\n", dirname_mp3_filename);
1212 talk_file(dirname_mp3_filename, false);
1213 if(global_settings.talk_filetype)
1214 talk_id(VOICE_DIR, true);
1215 return 1;
1218 static void ft_play_filename(char *dir, char *file)
1220 char name_mp3_filename[MAX_PATH+1];
1222 #if CONFIG_CODEC != SWCODEC
1223 if (audio_status() & AUDIO_STATUS_PLAY)
1224 return;
1225 #endif
1227 if (strcasecmp(&file[strlen(file) - strlen(file_thumbnail_ext)],
1228 file_thumbnail_ext))
1229 { /* file has no .talk extension */
1230 snprintf(name_mp3_filename, sizeof(name_mp3_filename),
1231 "%s/%s%s", dir, file, file_thumbnail_ext);
1233 talk_file(name_mp3_filename, false);
1235 else
1236 { /* it already is a .talk file, play this directly */
1237 snprintf(name_mp3_filename, sizeof(name_mp3_filename),
1238 "%s/%s", dir, file);
1239 talk_id(LANG_VOICE_DIR_HOVER, false); /* prefix it */
1240 talk_file(name_mp3_filename, true);
1244 /* These two functions are called by the USB and shutdown handlers */
1245 void tree_flush(void)
1247 scrobbler_shutdown();
1248 #ifdef HAVE_TAGCACHE
1249 tagcache_shutdown();
1250 #endif
1251 playlist_shutdown();
1253 #ifdef HAVE_TC_RAMCACHE
1254 tagcache_unload_ramcache();
1255 #endif
1257 #ifdef HAVE_DIRCACHE
1259 int old_val = global_status.dircache_size;
1260 if (global_settings.dircache)
1262 if (!dircache_is_initializing())
1263 global_status.dircache_size = dircache_get_cache_size();
1264 # ifdef HAVE_EEPROM_SETTINGS
1265 if (firmware_settings.initialized)
1266 dircache_save();
1267 # endif
1268 dircache_disable();
1270 else
1272 global_status.dircache_size = 0;
1274 if (old_val != global_status.dircache_size)
1275 status_save();
1277 #endif
1280 void tree_restore(void)
1282 #ifdef HAVE_EEPROM_SETTINGS
1283 firmware_settings.disk_clean = false;
1284 #endif
1286 #ifdef HAVE_TC_RAMCACHE
1287 remove(TAGCACHE_STATEFILE);
1288 #endif
1290 #ifdef HAVE_DIRCACHE
1291 remove(DIRCACHE_FILE);
1292 if (global_settings.dircache)
1294 /* Print "Scanning disk..." to the display. */
1295 int i;
1296 FOR_NB_SCREENS(i)
1298 screens[i].putsxy((LCD_WIDTH/2) -
1299 ((strlen(str(LANG_SCANNING_DISK)) *
1300 screens[i].char_width)/2),
1301 LCD_HEIGHT-screens[i].char_height*3,
1302 str(LANG_SCANNING_DISK));
1303 gui_textarea_update(&screens[i]);
1305 cond_talk_ids_fq(LANG_SCANNING_DISK);
1307 dircache_build(global_status.dircache_size);
1309 /* Clean the text when we are done. */
1310 FOR_NB_SCREENS(i)
1312 gui_textarea_clear(&screens[i]);
1315 #endif
1316 #ifdef HAVE_TAGCACHE
1317 tagcache_start_scan();
1318 #endif
1319 scrobbler_init();