Bugfix from Alistair Marshall for FS#5660. Bookmark function was not called when...
[kugel-rb.git] / apps / tree.c
blobd991905d10f04906824d2530aac37224726810df
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 "main_menu.h"
35 #include "sprintf.h"
36 #include "audio.h"
37 #include "playlist.h"
38 #include "menu.h"
39 #include "gwps.h"
40 #include "settings.h"
41 #include "status.h"
42 #include "debug.h"
43 #include "ata.h"
44 #include "rolo.h"
45 #include "icons.h"
46 #include "lang.h"
47 #include "language.h"
48 #include "screens.h"
49 #include "keyboard.h"
50 #include "bookmark.h"
51 #include "onplay.h"
52 #include "buffer.h"
53 #include "plugin.h"
54 #include "power.h"
55 #include "action.h"
56 #include "talk.h"
57 #include "filetypes.h"
58 #include "misc.h"
59 #include "filetree.h"
60 #include "tagtree.h"
61 #include "recorder/recording.h"
62 #include "rtc.h"
63 #include "dircache.h"
64 #include "tagcache.h"
65 #include "yesno.h"
66 #include "gwps-common.h"
68 /* gui api */
69 #include "list.h"
70 #include "statusbar.h"
71 #include "splash.h"
72 #include "buttonbar.h"
73 #include "textarea.h"
75 #ifdef HAVE_LCD_BITMAP
76 #include "widgets.h"
77 #endif
79 #ifdef HAVE_LCD_COLOR
80 #include "backdrop.h"
81 #endif
83 /* a table for the know file types */
84 const struct filetype filetypes[] = {
85 { "mp3", TREE_ATTR_MPA, Icon_Audio, VOICE_EXT_MPA },
86 { "mp2", TREE_ATTR_MPA, Icon_Audio, VOICE_EXT_MPA },
87 { "mpa", TREE_ATTR_MPA, Icon_Audio, VOICE_EXT_MPA },
88 #if CONFIG_CODEC == SWCODEC
89 /* Temporary hack to allow playlist creation */
90 { "mp1", TREE_ATTR_MPA, Icon_Audio, VOICE_EXT_MPA },
91 { "ogg", TREE_ATTR_MPA, Icon_Audio, VOICE_EXT_MPA },
92 { "wma", TREE_ATTR_MPA, Icon_Audio, VOICE_EXT_MPA },
93 { "wav", TREE_ATTR_MPA, Icon_Audio, VOICE_EXT_MPA },
94 { "flac",TREE_ATTR_MPA, Icon_Audio, VOICE_EXT_MPA },
95 { "ac3", TREE_ATTR_MPA, Icon_Audio, VOICE_EXT_MPA },
96 { "a52", TREE_ATTR_MPA, Icon_Audio, VOICE_EXT_MPA },
97 { "mpc", TREE_ATTR_MPA, Icon_Audio, VOICE_EXT_MPA },
98 { "wv", TREE_ATTR_MPA, Icon_Audio, VOICE_EXT_MPA },
99 { "m4a", TREE_ATTR_MPA, Icon_Audio, VOICE_EXT_MPA },
100 { "mp4", TREE_ATTR_MPA, Icon_Audio, VOICE_EXT_MPA },
101 { "shn", TREE_ATTR_MPA, Icon_Audio, VOICE_EXT_MPA },
102 { "aif", TREE_ATTR_MPA, Icon_Audio, VOICE_EXT_MPA },
103 { "aiff",TREE_ATTR_MPA, Icon_Audio, VOICE_EXT_MPA },
104 #endif
105 { "m3u", TREE_ATTR_M3U, Icon_Playlist, LANG_PLAYLIST },
106 { "cfg", TREE_ATTR_CFG, Icon_Config, VOICE_EXT_CFG },
107 { "wps", TREE_ATTR_WPS, Icon_Wps, VOICE_EXT_WPS },
108 #ifdef HAVE_REMOTE_LCD
109 { "rwps", TREE_ATTR_RWPS, Icon_Wps, VOICE_EXT_RWPS },
110 #endif
111 #ifdef HAVE_LCD_COLOR
112 { "bmp", TREE_ATTR_BMP, Icon_Wps, VOICE_EXT_WPS },
113 #endif
114 #ifdef CONFIG_TUNER
115 { "fmr", TREE_ATTR_FMR, Icon_Preset, LANG_FMR },
116 #endif
117 { "lng", TREE_ATTR_LNG, Icon_Language, LANG_LANGUAGE },
118 { "rock",TREE_ATTR_ROCK,Icon_Plugin, VOICE_EXT_ROCK },
119 #ifdef HAVE_LCD_BITMAP
120 { "fnt", TREE_ATTR_FONT,Icon_Font, VOICE_EXT_FONT },
121 { "kbd", TREE_ATTR_KBD, Icon_Keyboard, VOICE_EXT_KBD },
122 #endif
123 { "bmark",TREE_ATTR_BMARK, Icon_Bookmark, VOICE_EXT_BMARK },
124 #ifdef BOOTFILE_EXT
125 { BOOTFILE_EXT, TREE_ATTR_MOD, Icon_Firmware, VOICE_EXT_AJZ },
126 #endif /* #ifndef SIMULATOR */
129 struct gui_synclist tree_lists;
131 /* I put it here because other files doesn't use it yet,
132 * but should be elsewhere since it will be used mostly everywhere */
133 #ifdef HAS_BUTTONBAR
134 struct gui_buttonbar tree_buttonbar;
135 #endif
136 static struct tree_context tc;
138 bool boot_changed = false;
140 char lastfile[MAX_PATH];
141 static char lastdir[MAX_PATH];
142 static int lasttable, lastextra, lastfirstpos;
143 static int max_files = 0;
145 static bool reload_dir = false;
147 static bool start_wps = false;
148 static bool dirbrowse(void);
149 static int curr_context = false;/* id3db or tree*/
152 * removes the extension of filename (if it doesn't start with a .)
153 * puts the result in buffer
155 char * strip_extension(char * filename, char * buffer)
157 int dotpos;
158 char * dot=strrchr(filename, '.');
159 if(dot!=0 && filename[0]!='.')
161 dotpos = dot-filename;
162 strncpy(buffer, filename, dotpos);
163 buffer[dotpos]='\0';
164 return(buffer);
166 else
167 return(filename);
169 char * tree_get_filename(int selected_item, void * data, char *buffer)
171 struct tree_context * local_tc=(struct tree_context *)data;
172 char *name;
173 int attr=0;
174 bool id3db = *(local_tc->dirfilter) == SHOW_ID3DB;
176 if (id3db)
178 return tagtree_get_entry(&tc, selected_item)->name;
180 else
182 struct entry* dc = local_tc->dircache;
183 struct entry* e = &dc[selected_item];
184 name = e->name;
185 attr = e->attr;
187 /* if any file filter is on, and if it's not a directory,
188 * strip the extension */
190 if ( (*(local_tc->dirfilter) != SHOW_ID3DB) && !(attr & ATTR_DIRECTORY)
191 && (*(local_tc->dirfilter) != SHOW_ALL) )
193 return(strip_extension(name, buffer));
195 return(name);
199 void tree_get_fileicon(int selected_item, void * data, ICON * icon)
201 struct tree_context * local_tc=(struct tree_context *)data;
202 bool id3db = *(local_tc->dirfilter) == SHOW_ID3DB;
203 if (id3db) {
204 *icon = (ICON)tagtree_get_icon(&tc);
206 else {
207 struct entry* dc = local_tc->dircache;
208 struct entry* e = &dc[selected_item];
209 *icon = (ICON)filetype_get_icon(e->attr);
213 bool check_rockboxdir(void)
215 DIR *dir = opendir(ROCKBOX_DIR);
216 if(!dir)
217 { /* No need to localise this message.
218 If .rockbox is missing, it wouldn't work anyway */
219 int i;
220 FOR_NB_SCREENS(i)
221 screens[i].clear_display();
222 gui_syncsplash(HZ*2, true, "No .rockbox directory");
223 FOR_NB_SCREENS(i)
224 screens[i].clear_display();
225 gui_syncsplash(HZ*2, true, "Installation incomplete");
226 return false;
228 closedir(dir);
229 return true;
232 void browse_root(void)
234 gui_sync_wps_screen_init();
236 filetype_init();
237 check_rockboxdir();
239 strcpy(tc.currdir, "/");
241 #ifdef HAVE_LCD_CHARCELLS
242 int i;
243 FOR_NB_SCREENS(i)
244 screens[i].double_height(false);
245 #endif
246 #ifdef HAS_BUTTONBAR
247 gui_buttonbar_init(&tree_buttonbar);
248 /* since archos only have one screen, no need to create more than that */
249 gui_buttonbar_set_display(&tree_buttonbar, &(screens[SCREEN_MAIN]) );
250 #endif
251 gui_synclist_init(&tree_lists, &tree_get_filename, &tc, false, 1);
252 gui_synclist_set_icon_callback(&tree_lists,
253 global_settings.show_icons?&tree_get_fileicon:NULL);
254 #ifndef SIMULATOR
255 dirbrowse();
256 #else
257 if (!dirbrowse()) {
258 DEBUGF("No filesystem found. Have you forgotten to create it?\n");
260 #endif
263 void tree_get_filetypes(const struct filetype** types, int* count)
265 *types = filetypes;
266 *count = sizeof(filetypes) / sizeof(*filetypes);
269 struct tree_context* tree_get_context(void)
271 return &tc;
274 /* talkbox hovering delay, to avoid immediate disk activity */
275 #define HOVER_DELAY (HZ/2)
277 * Returns the position of a given file in the current directory
278 * returns -1 if not found
280 int tree_get_file_position(char * filename)
282 int i;
284 /* use lastfile to determine the selected item (default=0) */
285 for (i=0; i < tc.filesindir; i++)
287 struct entry* dc = tc.dircache;
288 struct entry* e = &dc[i];
289 if (!strcasecmp(e->name, filename))
290 return(i);
292 return(-1);/* no file can match, returns undefined */
296 * Called when a new dir is loaded (for example when returning from other apps ...)
297 * also completely redraws the tree
299 static int update_dir(void)
301 bool id3db = *tc.dirfilter == SHOW_ID3DB;
302 bool changed = false;
303 /* Checks for changes */
304 if (id3db) {
305 if (tc.currtable != lasttable ||
306 tc.currextra != lastextra ||
307 tc.firstpos != lastfirstpos ||
308 reload_dir)
310 if (tagtree_load(&tc) < 0)
311 return -1;
313 lasttable = tc.currtable;
314 lastextra = tc.currextra;
315 lastfirstpos = tc.firstpos;
316 changed = true;
319 else {
320 /* if the tc.currdir has been changed, reload it ...*/
321 if (strncmp(tc.currdir, lastdir, sizeof(lastdir)) || reload_dir) {
323 if (ft_load(&tc, NULL) < 0)
324 return -1;
325 strcpy(lastdir, tc.currdir);
326 changed = true;
329 /* if selected item is undefined */
330 if (tc.selected_item == -1)
332 /* use lastfile to determine the selected item */
333 tc.selected_item = tree_get_file_position(lastfile);
335 /* If the file doesn't exists, select the first one (default) */
336 if(tc.selected_item < 0)
337 tc.selected_item = 0;
338 changed = true;
340 if (changed)
342 if(!id3db && (tc.dirfull ||
343 tc.filesindir == global_settings.max_files_in_dir) )
345 gui_syncsplash(HZ, true, str(LANG_SHOWDIR_BUFFER_FULL));
348 gui_synclist_set_nb_items(&tree_lists, tc.filesindir);
349 gui_synclist_set_icon_callback(&tree_lists,
350 global_settings.show_icons?&tree_get_fileicon:NULL);
351 if( tc.selected_item >= tc.filesindir)
352 tc.selected_item=tc.filesindir-1;
354 gui_synclist_select_item(&tree_lists, tc.selected_item);
355 #ifdef HAS_BUTTONBAR
356 if (global_settings.buttonbar) {
357 if (*tc.dirfilter < NUM_FILTER_MODES)
358 gui_buttonbar_set(&tree_buttonbar, str(LANG_DIRBROWSE_F1),
359 str(LANG_DIRBROWSE_F2),
360 str(LANG_DIRBROWSE_F3));
361 else
362 gui_buttonbar_set(&tree_buttonbar, "<<<", "", "");
363 gui_buttonbar_draw(&tree_buttonbar);
365 #endif
366 gui_synclist_draw(&tree_lists);
367 gui_syncstatusbar_draw(&statusbars, true);
368 return tc.filesindir;
371 /* load tracks from specified directory to resume play */
372 void resume_directory(const char *dir)
374 bool id3db = *tc.dirfilter == SHOW_ID3DB;
376 if (ft_load(&tc, dir) < 0)
377 return;
378 lastdir[0] = 0;
380 ft_build_playlist(&tc, 0);
382 if (id3db)
383 tagtree_load(&tc);
386 /* Returns the current working directory and also writes cwd to buf if
387 non-NULL. In case of error, returns NULL. */
388 char *getcwd(char *buf, int size)
390 if (!buf)
391 return tc.currdir;
392 else if (size > 0)
394 strncpy(buf, tc.currdir, size);
395 return buf;
397 else
398 return NULL;
401 /* Force a reload of the directory next time directory browser is called */
402 void reload_directory(void)
404 reload_dir = true;
407 static void start_resume(bool just_powered_on)
409 bool do_resume = false;
410 if ( global_settings.resume_index != -1 ) {
411 DEBUGF("Resume index %X offset %X\n",
412 global_settings.resume_index,
413 global_settings.resume_offset);
415 #ifdef HAVE_ALARM_MOD
416 if ( rtc_check_alarm_started(true) ) {
417 rtc_enable_alarm(false);
418 do_resume = true;
420 #endif
422 /* always resume? */
423 if ( global_settings.resume || ! just_powered_on)
424 do_resume = true;
426 if (! do_resume) return;
428 if (playlist_resume() != -1)
430 playlist_start(global_settings.resume_index,
431 global_settings.resume_offset);
433 start_wps = true;
435 else return;
437 else if (! just_powered_on) {
438 gui_syncsplash(HZ*2, true, str(LANG_NOTHING_TO_RESUME));
442 /* Selects a file and update tree context properly */
443 void set_current_file(char *path)
445 char *name;
446 int i;
448 /* in ID3DB mode it is a bad idea to call this function */
449 /* (only happens with `follow playlist') */
450 if( *tc.dirfilter == SHOW_ID3DB )
451 return;
453 /* separate directory from filename */
454 /* gets the directory's name and put it into tc.currdir */
455 name = strrchr(path+1,'/');
456 if (name)
458 *name = 0;
459 strcpy(tc.currdir, path);
460 *name = '/';
461 name++;
463 else
465 strcpy(tc.currdir, "/");
466 name = path+1;
469 strcpy(lastfile, name);
471 /* undefined item selected */
472 tc.selected_item = -1;
474 /* If we changed dir we must recalculate the dirlevel
475 and adjust the selected history properly */
476 if (strncmp(tc.currdir,lastdir,sizeof(lastdir)))
478 tc.dirlevel = 0;
479 tc.selected_item_history[tc.dirlevel] = -1;
481 /* use '/' to calculate dirlevel */
482 /* FIXME : strlen(path) : crazy oO better to store it at
483 the beginning */
484 int path_len = strlen(path) + 1;
485 for (i = 1; i < path_len; i++)
487 if (path[i] == '/')
489 tc.dirlevel++;
490 tc.selected_item_history[tc.dirlevel] = -1;
496 static bool check_changed_id3mode(bool currmode)
498 if (currmode != (global_settings.dirfilter == SHOW_ID3DB)) {
499 currmode = global_settings.dirfilter == SHOW_ID3DB;
500 if (currmode) {
501 curr_context=CONTEXT_ID3DB;
502 tagtree_load(&tc);
504 else
506 curr_context=CONTEXT_TREE;
507 ft_load(&tc, NULL);
510 return currmode;
512 /* main loop, handles key events */
513 static bool dirbrowse(void)
515 int numentries=0;
516 char buf[MAX_PATH];
517 int lasti = -1;
518 unsigned button;
519 bool reload_root = false;
520 int lastfilter = *tc.dirfilter;
521 bool lastsortcase = global_settings.sort_case;
522 bool need_update = true;
523 bool exit_func = false;
524 long thumbnail_time = -1; /* for delaying a thumbnail */
526 unsigned lastbutton = 0;
527 char* currdir = tc.currdir; /* just a shortcut */
528 bool id3db = *tc.dirfilter == SHOW_ID3DB;
530 if (id3db)
531 curr_context=CONTEXT_ID3DB;
532 else
533 curr_context=CONTEXT_TREE;
534 tc.selected_item = 0;
535 tc.dirlevel=0;
536 tc.firstpos=0;
537 lasttable = -1;
538 lastextra = -1;
539 lastfirstpos = 0;
541 if (*tc.dirfilter < NUM_FILTER_MODES) {
542 #ifdef HAVE_RECORDING
543 #ifndef SIMULATOR
544 if (global_settings.rec_startup) {
545 /* We fake being in the menu structure by calling
546 the appropriate parent when we drop out of each screen */
547 recording_screen();
548 rec_menu();
549 main_menu();
551 else
552 #endif
553 #endif
554 start_resume(true);
557 /* If we don't need to show the wps, draw the dir */
558 if (!start_wps) {
559 numentries = update_dir();
560 if (numentries == -1)
561 return false; /* currdir is not a directory */
563 if (*tc.dirfilter > NUM_FILTER_MODES && numentries==0)
565 gui_syncsplash(HZ*2, true, str(LANG_NO_FILES));
566 return false; /* No files found for rockbox_browser() */
570 while(1) {
571 struct entry *dircache = tc.dircache;
572 bool restore = false;
573 #ifdef BOOTFILE
574 if (boot_changed) {
575 char *lines[]={str(LANG_BOOT_CHANGED), str(LANG_REBOOT_NOW)};
576 struct text_message message={lines, 2};
577 if(gui_syncyesno_run(&message, NULL, NULL)==YESNO_YES)
578 rolo_load("/" BOOTFILE);
579 restore = true;
580 boot_changed = false;
582 #endif
583 button = button_get_w_tmo(HZ/5);
584 need_update = gui_synclist_do_button(&tree_lists, button);
586 switch ( button ) {
587 #ifdef TREE_ENTER
588 case TREE_ENTER:
589 case TREE_ENTER | BUTTON_REPEAT:
590 #endif
591 #ifdef TREE_RC_RUN
592 case TREE_RC_RUN:
593 #endif
594 case TREE_RUN:
595 #ifdef TREE_RUN_PRE
596 if (((button == TREE_RUN)
597 #ifdef TREE_RC_RUN_PRE
598 || (button == TREE_RC_RUN))
599 && ((lastbutton != TREE_RC_RUN_PRE)
600 #endif
601 && (lastbutton != TREE_RUN_PRE)))
602 break;
603 #endif
604 /* nothing to do if no files to display */
605 if ( numentries == 0 )
606 break;
608 switch (id3db?tagtree_enter(&tc):ft_enter(&tc))
610 case 1: reload_dir = true; break;
611 case 2: start_wps = true; break;
612 case 3: exit_func = true; break;
613 default: break;
615 restore = true;
616 break;
618 case TREE_EXIT:
619 case TREE_EXIT | BUTTON_REPEAT:
620 #ifdef TREE_RC_EXIT
621 case TREE_RC_EXIT:
622 #endif
623 if (*tc.dirfilter > NUM_FILTER_MODES && tc.dirlevel < 1) {
624 exit_func = true;
625 break;
627 /* if we are in /, nothing to do */
628 if (tc.dirlevel == 0 && !strcmp(currdir,"/"))
629 break;
631 if (id3db)
632 tagtree_exit(&tc);
633 else
634 if (ft_exit(&tc) == 3)
635 exit_func = true;
637 restore = true;
638 break;
640 #ifdef TREE_OFF
641 case TREE_OFF:
642 if (*tc.dirfilter < NUM_FILTER_MODES)
644 /* Stop the music if it is playing */
645 if(audio_status()) {
646 if (!global_settings.party_mode) {
647 if (global_settings.fade_on_stop)
648 fade(0);
649 bookmark_autobookmark();
650 audio_stop();
653 #if defined(CONFIG_CHARGING) && \
654 (CONFIG_KEYPAD == RECORDER_PAD) && !defined(HAVE_SW_POWEROFF)
655 else {
656 if (!charger_inserted()) {
657 if(shutdown_screen())
658 reload_dir = true;
659 } else {
660 charging_splash();
662 restore = true;
664 #endif
666 break;
667 #if defined(CONFIG_CHARGING) && !defined(HAVE_POWEROFF_WHILE_CHARGING)
668 case TREE_OFF | BUTTON_REPEAT:
669 if (charger_inserted()) {
670 charging_splash();
671 restore = true;
673 break;
674 #endif
675 #endif /* TREE_OFF */
676 case TREE_MENU:
677 #ifdef TREE_RC_MENU
678 case TREE_RC_MENU:
679 #endif
680 #ifdef TREE_MENU_PRE
681 if (lastbutton != TREE_MENU_PRE
682 #ifdef TREE_RC_MENU_PRE
683 && lastbutton != TREE_RC_MENU_PRE
684 #endif
686 break;
687 #endif
688 /* don't enter menu from plugin browser */
689 if (*tc.dirfilter < NUM_FILTER_MODES)
691 int i;
692 FOR_NB_SCREENS(i)
693 screens[i].stop_scroll();
694 if (main_menu())
695 reload_dir = true;
696 restore = true;
698 id3db = check_changed_id3mode(id3db);
699 if(id3db)
700 reload_dir = true;
702 else /* use it as a quick exit instead */
703 exit_func = true;
704 break;
706 case TREE_WPS:
707 #ifdef TREE_RC_WPS
708 case TREE_RC_WPS:
709 #endif
710 #ifdef TREE_WPS_PRE
711 if ((lastbutton != TREE_WPS_PRE)
712 #ifdef TREE_RC_WPS
713 && (lastbutton != TREE_RC_WPS_PRE)
714 #endif
716 break;
717 #endif
718 /* don't enter wps from plugin browser etc */
719 if (*tc.dirfilter < NUM_FILTER_MODES)
721 if (audio_status() & AUDIO_STATUS_PLAY)
723 start_wps=true;
725 else
727 start_resume(false);
728 restore = true;
731 break;
733 #ifdef HAVE_QUICKSCREEN
734 case TREE_QUICK:
735 #ifdef TREE_RC_QUICK
736 case TREE_RC_QUICK:
737 #endif
738 /* don't enter f2 from plugin browser */
739 if (*tc.dirfilter < NUM_FILTER_MODES)
741 if (quick_screen_quick(button))
742 reload_dir = true;
743 restore = true;
745 id3db = check_changed_id3mode(id3db);
746 reload_dir = true;
748 break;
749 #endif
751 #ifdef BUTTON_F3
752 case BUTTON_F3:
753 /* don't enter f3 from plugin browser */
754 if (*tc.dirfilter < NUM_FILTER_MODES)
756 if (quick_screen_f3(button))
757 reload_dir = true;
758 restore = true;
760 break;
761 #endif
763 case TREE_CONTEXT:
764 #ifdef TREE_RC_CONTEXT
765 case TREE_RC_CONTEXT:
766 #endif
767 #ifdef TREE_CONTEXT2
768 case TREE_CONTEXT2:
769 #endif
771 int onplay_result;
772 int attr = 0;
774 if(!numentries)
775 onplay_result = onplay(NULL, 0, curr_context);
776 else {
777 if (id3db)
779 if (tagtree_get_attr(&tc) == TREE_ATTR_MPA)
781 attr = TREE_ATTR_MPA;
782 tagtree_get_filename(&tc, buf, sizeof(buf));
785 else
787 attr = dircache[tc.selected_item].attr;
789 if (currdir[1]) /* Not in / */
790 snprintf(buf, sizeof buf, "%s/%s",
791 currdir,
792 dircache[tc.selected_item].name);
793 else /* In / */
794 snprintf(buf, sizeof buf, "/%s",
795 dircache[tc.selected_item].name);
797 onplay_result = onplay(buf, attr, curr_context);
799 switch (onplay_result)
801 case ONPLAY_OK:
802 restore = true;
803 break;
805 case ONPLAY_RELOAD_DIR:
806 reload_dir = true;
807 break;
809 case ONPLAY_START_PLAY:
810 start_wps = true;
811 break;
813 break;
816 case BUTTON_NONE:
817 if (thumbnail_time != -1 &&
818 TIME_AFTER(current_tick, thumbnail_time))
819 { /* a delayed hovering thumbnail is due now */
820 int res;
821 int attr;
822 char* name;
824 if (id3db)
826 attr = tagtree_get_attr(&tc);
827 name = tagtree_get_entry(&tc, lasti)->name;
829 else
831 attr = dircache[lasti].attr;
832 name = dircache[lasti].name;
835 if (attr & ATTR_DIRECTORY)
837 DEBUGF("Playing directory thumbnail: %s", currdir);
838 res = ft_play_dirname(name);
839 if (res < 0) /* failed, not existing */
840 { /* say the number instead, as a fallback */
841 talk_id(VOICE_DIR, false);
842 talk_number(lasti+1, true);
845 else
847 DEBUGF("Playing file thumbnail: %s/%s%s\n",
848 currdir, name,
849 file_thumbnail_ext);
850 /* no fallback necessary, we knew in advance
851 that the file exists */
852 ft_play_filename(currdir, name);
854 thumbnail_time = -1; /* job done */
856 gui_syncstatusbar_draw(&statusbars, false);
857 break;
859 #ifdef HAVE_HOTSWAP
860 case SYS_FS_CHANGED:
861 if (!id3db)
862 reload_dir = true;
863 /* The 'dir no longer valid' situation will be caught later
864 * by checking the showdir() result. */
865 break;
866 #endif
868 default:
869 if (default_event_handler(button) == SYS_USB_CONNECTED)
871 if(*tc.dirfilter > NUM_FILTER_MODES)
872 /* leave sub-browsers after usb, doing otherwise
873 might be confusing to the user */
874 exit_func = true;
875 else
876 reload_dir = true;
878 break;
881 if ( button )
883 ata_spin();
884 lastbutton = button;
887 if (start_wps && audio_status() )
889 int i;
891 FOR_NB_SCREENS(i)
892 screens[i].stop_scroll();
894 if (gui_wps_show() == SYS_USB_CONNECTED)
895 reload_dir = true;
896 #ifdef HAVE_HOTSWAP
897 else
898 if (!id3db) /* Try reload to catch 'no longer valid' case. */
899 reload_dir = true;
900 #endif
901 #ifdef HAVE_LCD_COLOR
902 show_main_backdrop();
903 #endif
904 id3db = check_changed_id3mode(id3db);
905 restore = true;
906 start_wps=false;
909 check_rescan:
910 /* do we need to rescan dir? */
911 if (reload_dir || reload_root ||
912 lastfilter != *tc.dirfilter ||
913 lastsortcase != global_settings.sort_case)
915 if ( reload_root ) {
916 strcpy(currdir, "/");
917 tc.dirlevel = 0;
918 tc.currtable = 0;
919 tc.currextra = 0;
920 lasttable = -1;
921 lastextra = -1;
922 reload_root = false;
925 if (! reload_dir )
927 gui_synclist_select_item(&tree_lists, 0);
928 gui_synclist_draw(&tree_lists);
929 tc.selected_item = 0;
930 lastdir[0] = 0;
933 lastfilter = *tc.dirfilter;
934 lastsortcase = global_settings.sort_case;
935 restore = true;
936 button_clear_queue(); /* clear button queue */
939 if (exit_func)
940 break;
942 if (restore || reload_dir) {
943 /* restore display */
944 numentries = update_dir();
945 if (currdir[1] && (numentries < 0))
946 { /* not in root and reload failed */
947 reload_root = true; /* try root */
948 reload_dir = false;
949 goto check_rescan;
951 need_update = true;
952 reload_dir = false;
955 if(need_update) {
956 tc.selected_item = gui_synclist_get_sel_pos(&tree_lists);
957 need_update=false;
958 if ( numentries > 0 ) {
959 /* Voice the file if changed */
960 if(lasti != tc.selected_item || restore) {
961 int attr;
962 char* name;
964 lasti = tc.selected_item;
965 thumbnail_time = -1; /* Cancel whatever we were
966 about to say */
968 if (id3db)
970 attr = tagtree_get_attr(&tc);
971 name = tagtree_get_entry(&tc, tc.selected_item)->name;
973 else
975 attr = dircache[tc.selected_item].attr;
976 name = dircache[tc.selected_item].name;
979 /* Directory? */
980 if (attr & ATTR_DIRECTORY)
982 /* play directory thumbnail */
983 switch (global_settings.talk_dir) {
984 case 1: /* dirs as numbers */
985 talk_id(VOICE_DIR, false);
986 talk_number(tc.selected_item+1, true);
987 break;
989 case 2: /* dirs spelled */
990 talk_spell(name, false);
991 break;
993 case 3: /* thumbnail clip */
994 /* "schedule" a thumbnail, to have a little
995 delay */
996 thumbnail_time = current_tick + HOVER_DELAY;
997 break;
999 default:
1000 break;
1003 else /* file */
1005 switch (global_settings.talk_file) {
1006 case 1: /* files as numbers */
1007 ft_play_filenumber(
1008 tc.selected_item-tc.dirsindir+1,
1009 attr & TREE_ATTR_MASK);
1010 break;
1012 case 2: /* files spelled */
1013 talk_spell(name, false);
1014 break;
1016 case 3: /* thumbnail clip */
1017 /* "schedule" a thumbnail, to have a little
1018 delay */
1019 if (attr & TREE_ATTR_THUMBNAIL)
1020 thumbnail_time = current_tick + HOVER_DELAY;
1021 else
1022 /* spell the number as fallback */
1023 talk_spell(name, false);
1024 break;
1026 default:
1027 break;
1035 return true;
1038 static int plsize = 0;
1039 static long pltick;
1040 static bool add_dir(char* dirname, int len, int fd)
1042 bool abort = false;
1043 DIRCACHED* dir;
1045 /* check for user abort */
1046 if (button_get(false) == TREE_ABORT)
1047 return true;
1049 dir = opendir_cached(dirname);
1050 if(!dir)
1051 return true;
1053 while (true) {
1054 struct dircache_entry *entry;
1056 entry = readdir_cached(dir);
1057 if (!entry)
1058 break;
1059 if (entry->attribute & ATTR_DIRECTORY) {
1060 int dirlen = strlen(dirname);
1061 bool result;
1063 if (!strcmp((char *)entry->d_name, ".") ||
1064 !strcmp((char *)entry->d_name, ".."))
1065 continue;
1067 if (dirname[1])
1068 snprintf(dirname+dirlen, len-dirlen, "/%s", entry->d_name);
1069 else
1070 snprintf(dirname, len, "/%s", entry->d_name);
1072 result = add_dir(dirname, len, fd);
1073 dirname[dirlen] = '\0';
1074 if (result) {
1075 abort = true;
1076 break;
1079 else {
1080 int x = strlen((char *)entry->d_name);
1081 unsigned int i;
1082 char *cp = strrchr((char *)entry->d_name,'.');
1084 if (cp) {
1085 cp++;
1087 /* add all supported audio files to playlists */
1088 for (i=0; i < sizeof(filetypes)/sizeof(struct filetype); i++) {
1089 if (filetypes[i].tree_attr == TREE_ATTR_MPA) {
1090 if (!strcasecmp(cp, filetypes[i].extension)) {
1091 char buf[8];
1092 int i;
1093 write(fd, dirname, strlen(dirname));
1094 write(fd, "/", 1);
1095 write(fd, entry->d_name, x);
1096 write(fd, "\n", 1);
1098 plsize++;
1099 if(TIME_AFTER(current_tick, pltick+HZ/4)) {
1100 pltick = current_tick;
1102 snprintf(buf, sizeof buf, "%d", plsize);
1103 #ifdef HAVE_LCD_BITMAP
1104 FOR_NB_SCREENS(i)
1106 screens[i].puts(0, 4, (unsigned char *)buf);
1107 gui_textarea_update(&screens[i]);
1109 #else
1110 x = 10;
1111 if (plsize > 999)
1112 x=7;
1113 else {
1114 if (plsize > 99)
1115 x=8;
1116 else {
1117 if (plsize > 9)
1118 x=9;
1121 FOR_NB_SCREENS(i) {
1122 screens[i].puts(x,0,buf);
1124 #endif
1126 break;
1133 closedir_cached(dir);
1135 return abort;
1138 bool create_playlist(void)
1140 int fd;
1141 int i;
1142 char filename[MAX_PATH];
1144 pltick = current_tick;
1146 snprintf(filename, sizeof filename, "%s.m3u",
1147 tc.currdir[1] ? tc.currdir : "/root");
1148 FOR_NB_SCREENS(i)
1150 gui_textarea_clear(&screens[i]);
1151 screens[i].puts(0, 0, str(LANG_CREATING));
1152 screens[i].puts_scroll(0, 1, (unsigned char *)filename);
1153 #if defined(HAVE_LCD_BITMAP) || defined(SIMULATOR)
1154 gui_textarea_update(&screens[i]);
1155 #endif
1157 fd = creat(filename, O_WRONLY);
1158 if (fd < 0)
1159 return false;
1161 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
1162 cpu_boost(true);
1163 #endif
1165 snprintf(filename, sizeof(filename), "%s",
1166 tc.currdir[1] ? tc.currdir : "/");
1167 plsize = 0;
1168 add_dir(filename, sizeof(filename), fd);
1169 close(fd);
1171 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
1172 cpu_boost(false);
1173 #endif
1175 sleep(HZ);
1177 return true;
1180 bool rockbox_browse(const char *root, int dirfilter)
1182 static struct tree_context backup;
1184 backup = tc;
1185 reload_dir = true;
1186 memcpy(tc.currdir, root, sizeof(tc.currdir));
1187 start_wps = false;
1188 tc.dirfilter = &dirfilter;
1190 dirbrowse();
1192 tc = backup;
1193 return false;
1196 void tree_init(void)
1198 /* We copy the settings value in case it is changed by the user. We can't
1199 use it until the next reboot. */
1200 max_files = global_settings.max_files_in_dir;
1202 /* initialize tree context struct */
1203 memset(&tc, 0, sizeof(tc));
1204 tc.dirfilter = &global_settings.dirfilter;
1206 tc.name_buffer_size = AVERAGE_FILENAME_LENGTH * max_files;
1207 tc.name_buffer = buffer_alloc(tc.name_buffer_size);
1209 tc.dircache_size = max_files * sizeof(struct entry);
1210 tc.dircache = buffer_alloc(tc.dircache_size);
1213 void bookmark_play(char *resume_file, int index, int offset, int seed,
1214 char *filename)
1216 int i;
1217 int len=strlen(resume_file);
1219 if (!strcasecmp(&resume_file[len-4], ".m3u"))
1221 /* Playlist playback */
1222 char* slash;
1223 /* check that the file exists */
1224 int fd = open(resume_file, O_RDONLY);
1225 if(fd<0)
1226 return;
1227 close(fd);
1229 slash = strrchr(resume_file,'/');
1230 if (slash)
1232 char* cp;
1233 *slash=0;
1235 cp=resume_file;
1236 if (!cp[0])
1237 cp="/";
1239 if (playlist_create(cp, slash+1) != -1)
1241 if (global_settings.playlist_shuffle)
1242 playlist_shuffle(seed, -1);
1243 playlist_start(index,offset);
1245 *slash='/';
1248 else
1250 /* Directory playback */
1251 lastdir[0]='\0';
1252 if (playlist_create(resume_file, NULL) != -1)
1254 resume_directory(resume_file);
1255 if (global_settings.playlist_shuffle)
1256 playlist_shuffle(seed, -1);
1258 /* Check if the file is at the same spot in the directory,
1259 else search for it */
1260 if ((strcmp(strrchr(playlist_peek(index),'/') + 1,
1261 filename)))
1263 for ( i=0; i < playlist_amount(); i++ )
1265 if ((strcmp(strrchr(playlist_peek(i),'/') + 1,
1266 filename)) == 0)
1267 break;
1269 if (i < playlist_amount())
1270 index = i;
1271 else
1272 return;
1274 playlist_start(index,offset);
1278 start_wps=true;
1281 int ft_play_filenumber(int pos, int attr)
1283 /* try to find a voice ID for the extension, if known */
1284 unsigned int j;
1285 int ext_id = -1; /* default to none */
1286 for (j=0; j<sizeof(filetypes)/sizeof(*filetypes); j++)
1288 if (attr == filetypes[j].tree_attr)
1290 ext_id = filetypes[j].voiceclip;
1291 break;
1295 talk_id(VOICE_FILE, false);
1296 talk_number(pos, true);
1297 talk_id(ext_id, true);
1298 return 1;
1301 int ft_play_dirname(char* name)
1303 int fd;
1304 char dirname_mp3_filename[MAX_PATH+1];
1306 if (audio_status() & AUDIO_STATUS_PLAY)
1307 return 0;
1309 snprintf(dirname_mp3_filename, sizeof(dirname_mp3_filename), "%s/%s/%s",
1310 tc.currdir[1] ? tc.currdir : "" , name,
1311 dir_thumbnail_name);
1313 DEBUGF("Checking for %s\n", dirname_mp3_filename);
1315 fd = open(dirname_mp3_filename, O_RDONLY);
1316 if (fd < 0)
1318 DEBUGF("Failed to find: %s\n", dirname_mp3_filename);
1319 return -1;
1322 close(fd);
1324 DEBUGF("Found: %s\n", dirname_mp3_filename);
1326 talk_file(dirname_mp3_filename, false);
1327 return 1;
1330 void ft_play_filename(char *dir, char *file)
1332 char name_mp3_filename[MAX_PATH+1];
1334 if (audio_status() & AUDIO_STATUS_PLAY)
1335 return;
1337 if (strcasecmp(&file[strlen(file) - strlen(file_thumbnail_ext)],
1338 file_thumbnail_ext))
1339 { /* file has no .talk extension */
1340 snprintf(name_mp3_filename, sizeof(name_mp3_filename),
1341 "%s/%s%s", dir, file, file_thumbnail_ext);
1343 talk_file(name_mp3_filename, false);
1345 else
1346 { /* it already is a .talk file, play this directly */
1347 snprintf(name_mp3_filename, sizeof(name_mp3_filename),
1348 "%s/%s", dir, file);
1349 talk_id(LANG_VOICE_DIR_HOVER, false); /* prefix it */
1350 talk_file(name_mp3_filename, true);
1354 /* These two functions are called by the USB and shutdown handlers */
1355 void tree_flush(void)
1357 tagcache_stop_scan();
1358 playlist_shutdown();
1360 #ifdef HAVE_DIRCACHE
1361 if (global_settings.dircache)
1363 if (dircache_is_enabled())
1364 global_settings.dircache_size = dircache_get_cache_size();
1365 dircache_disable();
1367 else
1369 global_settings.dircache_size = 0;
1371 settings_save();
1372 #endif
1375 void tree_restore(void)
1377 #ifdef HAVE_DIRCACHE
1378 if (global_settings.dircache)
1380 /* Print "Scanning disk..." to the display. */
1381 int i;
1382 FOR_NB_SCREENS(i)
1384 screens[i].putsxy((LCD_WIDTH/2) -
1385 ((strlen(str(LANG_DIRCACHE_BUILDING)) *
1386 screens[i].char_width)/2),
1387 LCD_HEIGHT-screens[i].char_height*3,
1388 str(LANG_DIRCACHE_BUILDING));
1389 gui_textarea_update(&screens[i]);
1392 dircache_build(global_settings.dircache_size);
1394 /* Clean the text when we are done. */
1395 FOR_NB_SCREENS(i)
1397 gui_textarea_clear(&screens[i]);
1400 #endif
1401 tagcache_start_scan();