iPod 5G: Split lcd_bcm_finishup() function into two halves, and incorporate into...
[Rockbox.git] / apps / tree.c
blobca7b83ef3f91840f91684d2608106c61b9b4f21f
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 "dbtree.h"
61 #include "recorder/recording.h"
62 #include "rtc.h"
63 #include "dircache.h"
64 #include "yesno.h"
66 /* gui api */
67 #include "list.h"
68 #include "statusbar.h"
69 #include "splash.h"
70 #include "buttonbar.h"
71 #include "textarea.h"
73 #ifdef HAVE_LCD_BITMAP
74 #include "widgets.h"
75 #endif
77 /* a table for the know file types */
78 const struct filetype filetypes[] = {
79 { "mp3", TREE_ATTR_MPA, Icon_Audio, VOICE_EXT_MPA },
80 { "mp2", TREE_ATTR_MPA, Icon_Audio, VOICE_EXT_MPA },
81 { "mpa", TREE_ATTR_MPA, Icon_Audio, VOICE_EXT_MPA },
82 #if CONFIG_CODEC == SWCODEC
83 /* Temporary hack to allow playlist creation */
84 { "mp1", TREE_ATTR_MPA, Icon_Audio, VOICE_EXT_MPA },
85 { "ogg", TREE_ATTR_MPA, Icon_Audio, VOICE_EXT_MPA },
86 { "wma", TREE_ATTR_MPA, Icon_Audio, VOICE_EXT_MPA },
87 { "wav", TREE_ATTR_MPA, Icon_Audio, VOICE_EXT_MPA },
88 { "flac",TREE_ATTR_MPA, Icon_Audio, VOICE_EXT_MPA },
89 { "ac3", TREE_ATTR_MPA, Icon_Audio, VOICE_EXT_MPA },
90 { "a52", TREE_ATTR_MPA, Icon_Audio, VOICE_EXT_MPA },
91 { "mpc", TREE_ATTR_MPA, Icon_Audio, VOICE_EXT_MPA },
92 { "wv", TREE_ATTR_MPA, Icon_Audio, VOICE_EXT_MPA },
93 { "m4a", TREE_ATTR_MPA, Icon_Audio, VOICE_EXT_MPA },
94 { "mp4", TREE_ATTR_MPA, Icon_Audio, VOICE_EXT_MPA },
95 { "shn", TREE_ATTR_MPA, Icon_Audio, VOICE_EXT_MPA },
96 { "aif", TREE_ATTR_MPA, Icon_Audio, VOICE_EXT_MPA },
97 { "aiff",TREE_ATTR_MPA, Icon_Audio, VOICE_EXT_MPA },
98 #endif
99 { "m3u", TREE_ATTR_M3U, Icon_Playlist, LANG_PLAYLIST },
100 { "cfg", TREE_ATTR_CFG, Icon_Config, VOICE_EXT_CFG },
101 { "wps", TREE_ATTR_WPS, Icon_Wps, VOICE_EXT_WPS },
102 #ifdef HAVE_REMOTE_LCD
103 { "rwps", TREE_ATTR_RWPS, Icon_Wps, VOICE_EXT_RWPS },
104 #endif
105 #ifdef HAVE_LCD_COLOR
106 { "bmp", TREE_ATTR_BMP, Icon_Wps, VOICE_EXT_WPS },
107 #endif
108 { "lng", TREE_ATTR_LNG, Icon_Language, LANG_LANGUAGE },
109 { "rock",TREE_ATTR_ROCK,Icon_Plugin, VOICE_EXT_ROCK },
110 #ifdef HAVE_LCD_BITMAP
111 { "fnt", TREE_ATTR_FONT,Icon_Font, VOICE_EXT_FONT },
112 #endif
113 { "bmark",TREE_ATTR_BMARK, Icon_Bookmark, VOICE_EXT_BMARK },
114 #ifdef BOOTFILE_EXT
115 { BOOTFILE_EXT, TREE_ATTR_MOD, Icon_Firmware, VOICE_EXT_AJZ },
116 #endif /* #ifndef SIMULATOR */
119 struct gui_synclist tree_lists;
121 /* I put it here because other files doesn't use it yet,
122 * but should be elsewhere since it will be used mostly everywhere */
123 #ifdef HAS_BUTTONBAR
124 struct gui_buttonbar tree_buttonbar;
125 #endif
126 static struct tree_context tc;
128 bool boot_changed = false;
130 char lastfile[MAX_PATH];
131 static char lastdir[MAX_PATH];
132 static int lasttable, lastextra, lastfirstpos;
133 static int max_files = 0;
135 static bool reload_dir = false;
137 static bool start_wps = false;
138 static bool dirbrowse(void);
139 static int curr_context = false;/* id3db or tree*/
142 * removes the extension of filename (if it doesn't start with a .)
143 * puts the result in buffer
145 char * strip_extension(char * filename, char * buffer)
147 int dotpos;
148 char * dot=strrchr(filename, '.');
149 if(dot!=0 && filename[0]!='.')
151 dotpos = dot-filename;
152 strncpy(buffer, filename, dotpos);
153 buffer[dotpos]='\0';
154 return(buffer);
156 else
157 return(filename);
159 char * tree_get_filename(int selected_item, void * data, char *buffer)
161 struct tree_context * local_tc=(struct tree_context *)data;
162 char *name;
163 int attr=0;
164 bool id3db = *(local_tc->dirfilter) == SHOW_ID3DB;
166 if (id3db) {
167 name = ((char**)local_tc->dircache)[selected_item * local_tc->dentry_size];
169 else {
170 struct entry* dc = local_tc->dircache;
171 struct entry* e = &dc[selected_item];
172 name = e->name;
173 attr = e->attr;
175 /* if any file filter is on, and if it's not a directory,
176 * strip the extension */
178 if ( (*(local_tc->dirfilter) != SHOW_ID3DB) && !(attr & ATTR_DIRECTORY)
179 && (*(local_tc->dirfilter) != SHOW_ALL) )
181 return(strip_extension(name, buffer));
183 return(name);
187 void tree_get_fileicon(int selected_item, void * data, ICON * icon)
189 struct tree_context * local_tc=(struct tree_context *)data;
190 bool id3db = *(local_tc->dirfilter) == SHOW_ID3DB;
191 if (id3db) {
192 *icon = (ICON)db_get_icon(&tc);
194 else {
195 struct entry* dc = local_tc->dircache;
196 struct entry* e = &dc[selected_item];
197 *icon = (ICON)filetype_get_icon(e->attr);
201 bool check_rockboxdir(void)
203 DIR *dir = opendir(ROCKBOX_DIR);
204 if(!dir)
206 int i;
207 FOR_NB_SCREENS(i)
208 screens[i].clear_display();
209 gui_syncsplash(HZ*2, true, str(LANG_NO_ROCKBOX_DIR));
210 FOR_NB_SCREENS(i)
211 screens[i].clear_display();
212 gui_syncsplash(HZ*2, true, str(LANG_INSTALLATION_INCOMPLETE));
213 return false;
215 closedir(dir);
216 return true;
219 void browse_root(void)
221 gui_sync_wps_screen_init();
223 filetype_init();
224 check_rockboxdir();
226 strcpy(tc.currdir, "/");
228 #ifdef HAVE_LCD_CHARCELLS
229 int i;
230 FOR_NB_SCREENS(i)
231 screens[i].double_height(false);
232 #endif
233 #ifdef HAS_BUTTONBAR
234 gui_buttonbar_init(&tree_buttonbar);
235 /* since archos only have one screen, no need to create more than that */
236 gui_buttonbar_set_display(&tree_buttonbar, &(screens[SCREEN_MAIN]) );
237 #endif
238 gui_synclist_init(&tree_lists, &tree_get_filename, &tc);
239 gui_synclist_set_icon_callback(&tree_lists,
240 global_settings.show_icons?&tree_get_fileicon:NULL);
241 #ifndef SIMULATOR
242 dirbrowse();
243 #else
244 if (!dirbrowse()) {
245 DEBUGF("No filesystem found. Have you forgotten to create it?\n");
247 #endif
250 void tree_get_filetypes(const struct filetype** types, int* count)
252 *types = filetypes;
253 *count = sizeof(filetypes) / sizeof(*filetypes);
256 struct tree_context* tree_get_context(void)
258 return &tc;
261 /* talkbox hovering delay, to avoid immediate disk activity */
262 #define HOVER_DELAY (HZ/2)
264 * Returns the position of a given file in the current directory
265 * returns -1 if not found
267 int tree_get_file_position(char * filename)
269 int i;
270 /* use lastfile to determine the selected item (default=0) */
271 for (i=0; i < tc.filesindir; i++)
273 struct entry* dc = tc.dircache;
274 struct entry* e = &dc[i];
275 if (!strcasecmp(e->name, filename))
276 return(i);
278 return(-1);/* no file can match, returns undefined */
282 * Called when a new dir is loaded (for example when returning from other apps ...)
283 * also completely redraws the tree
285 static int update_dir(void)
287 bool id3db = *tc.dirfilter == SHOW_ID3DB;
288 bool changed = false;
289 /* Checks for changes */
290 if (id3db) {
291 if (tc.currtable != lasttable ||
292 tc.currextra != lastextra ||
293 tc.firstpos != lastfirstpos)
295 if (db_load(&tc) < 0)
296 return -1;
298 lasttable = tc.currtable;
299 lastextra = tc.currextra;
300 lastfirstpos = tc.firstpos;
301 changed = true;
304 else {
305 /* if the tc.currdir has been changed, reload it ...*/
306 if (strncmp(tc.currdir, lastdir, sizeof(lastdir)) || reload_dir) {
308 if (ft_load(&tc, NULL) < 0)
309 return -1;
310 strcpy(lastdir, tc.currdir);
311 changed = true;
314 /* if selected item is undefined */
315 if (tc.selected_item == -1)
317 /* use lastfile to determine the selected item */
318 tc.selected_item = tree_get_file_position(lastfile);
320 /* If the file doesn't exists, select the first one (default) */
321 if(tc.selected_item < 0)
322 tc.selected_item = 0;
323 changed = true;
325 if (changed)
327 if(!id3db && (tc.dirfull ||
328 tc.filesindir == global_settings.max_files_in_dir) )
330 /* dir full */
331 int i;
332 FOR_NB_SCREENS(i)
334 gui_textarea_clear(&screens[i]);
335 #ifdef HAVE_LCD_CHARCELLS
336 screens[i].double_height(false);
337 #endif
338 screens[i].clear_display();
339 screens[i].puts(0,0,str(LANG_SHOWDIR_ERROR_BUFFER));
340 screens[i].puts(0,1,str(LANG_SHOWDIR_ERROR_FULL));
341 gui_textarea_update(&screens[i]);
343 sleep(HZ*2);
344 FOR_NB_SCREENS(i)
345 gui_textarea_clear(&screens[i]);
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 gui_synclist_draw(&tree_lists);
356 gui_syncstatusbar_draw(&statusbars, true);
357 #ifdef HAS_BUTTONBAR
358 if (global_settings.buttonbar) {
359 if (*tc.dirfilter < NUM_FILTER_MODES)
360 gui_buttonbar_set(&tree_buttonbar, str(LANG_DIRBROWSE_F1),
361 str(LANG_DIRBROWSE_F2),
362 str(LANG_DIRBROWSE_F3));
363 else
364 gui_buttonbar_set(&tree_buttonbar, "<<<", "", "");
365 gui_buttonbar_draw(&tree_buttonbar);
367 #endif
368 return tc.filesindir;
371 /* load tracks from specified directory to resume play */
372 void resume_directory(const char *dir)
374 if (ft_load(&tc, dir) < 0)
375 return;
376 lastdir[0] = 0;
378 ft_build_playlist(&tc, 0);
381 /* Returns the current working directory and also writes cwd to buf if
382 non-NULL. In case of error, returns NULL. */
383 char *getcwd(char *buf, int size)
385 if (!buf)
386 return tc.currdir;
387 else if (size > 0)
389 strncpy(buf, tc.currdir, size);
390 return buf;
392 else
393 return NULL;
396 /* Force a reload of the directory next time directory browser is called */
397 void reload_directory(void)
399 reload_dir = true;
402 static void start_resume(bool just_powered_on)
404 bool do_resume = false;
405 if ( global_settings.resume_index != -1 ) {
406 DEBUGF("Resume index %X offset %X\n",
407 global_settings.resume_index,
408 global_settings.resume_offset);
410 #ifdef HAVE_ALARM_MOD
411 if ( rtc_check_alarm_started(true) ) {
412 rtc_enable_alarm(false);
413 do_resume = true;
415 #endif
417 /* always resume? */
418 if ( global_settings.resume || ! just_powered_on)
419 do_resume = true;
421 if (! do_resume) return;
423 if (playlist_resume() != -1)
425 playlist_start(global_settings.resume_index,
426 global_settings.resume_offset);
428 start_wps = true;
430 else return;
432 else if (! just_powered_on) {
433 gui_syncsplash(HZ*2, true, str(LANG_NOTHING_TO_RESUME));
437 /* Selects a file and update tree context properly */
438 void set_current_file(char *path)
440 char *name;
441 int i;
443 /* in ID3DB mode it is a bad idea to call this function */
444 /* (only happens with `follow playlist') */
445 if( *tc.dirfilter == SHOW_ID3DB )
446 return;
448 /* separate directory from filename */
449 /* gets the directory's name and put it into tc.currdir */
450 name = strrchr(path+1,'/');
451 if (name)
453 *name = 0;
454 strcpy(tc.currdir, path);
455 *name = '/';
456 name++;
458 else
460 strcpy(tc.currdir, "/");
461 name = path+1;
464 strcpy(lastfile, name);
466 /* undefined item selected */
467 tc.selected_item = -1;
469 /* If we changed dir we must recalculate the dirlevel
470 and adjust the selected history properly */
471 if (strncmp(tc.currdir,lastdir,sizeof(lastdir)))
473 tc.dirlevel = 0;
474 tc.selected_item_history[tc.dirlevel] = -1;
476 /* use '/' to calculate dirlevel */
477 /* FIXME : strlen(path) : crazy oO better to store it at
478 the beginning */
479 int path_len = strlen(path) + 1;
480 for (i = 1; i < path_len; i++)
482 if (path[i] == '/')
484 tc.dirlevel++;
485 tc.selected_item_history[tc.dirlevel] = -1;
491 static bool check_changed_id3mode(bool currmode)
493 if (currmode != (global_settings.dirfilter == SHOW_ID3DB)) {
494 currmode = global_settings.dirfilter == SHOW_ID3DB;
495 if (currmode) {
496 curr_context=CONTEXT_ID3DB;
497 db_load(&tc);
499 else
501 curr_context=CONTEXT_TREE;
502 ft_load(&tc, NULL);
505 return currmode;
507 /* main loop, handles key events */
508 static bool dirbrowse(void)
510 int numentries=0;
511 char buf[MAX_PATH];
512 int lasti = -1;
513 unsigned button;
514 bool reload_root = false;
515 int lastfilter = *tc.dirfilter;
516 bool lastsortcase = global_settings.sort_case;
517 bool need_update = true;
518 bool exit_func = false;
519 long thumbnail_time = -1; /* for delaying a thumbnail */
521 unsigned lastbutton = 0;
522 char* currdir = tc.currdir; /* just a shortcut */
523 bool id3db = *tc.dirfilter == SHOW_ID3DB;
525 if (id3db)
526 curr_context=CONTEXT_ID3DB;
527 else
528 curr_context=CONTEXT_TREE;
529 tc.selected_item = 0;
530 tc.dirlevel=0;
531 tc.firstpos=0;
532 lasttable = -1;
533 lastextra = -1;
534 lastfirstpos = 0;
536 if (*tc.dirfilter < NUM_FILTER_MODES) {
537 #ifdef HAVE_RECORDING
538 #ifndef SIMULATOR
539 if (global_settings.rec_startup) {
540 /* We fake being in the menu structure by calling
541 the appropriate parent when we drop out of each screen */
542 recording_screen();
543 rec_menu();
544 main_menu();
546 else
547 #endif
548 #endif
549 start_resume(true);
552 /* If we don't need to show the wps, draw the dir */
553 if (!start_wps) {
554 numentries = update_dir();
555 if (numentries == -1)
556 return false; /* currdir is not a directory */
558 if (*tc.dirfilter > NUM_FILTER_MODES && numentries==0)
560 gui_syncsplash(HZ*2, true, str(LANG_NO_FILES));
561 return false; /* No files found for rockbox_browser() */
565 while(1) {
566 struct entry *dircache = tc.dircache;
567 bool restore = false;
568 #ifdef BOOTFILE
569 if (boot_changed) {
570 char *lines[]={str(LANG_BOOT_CHANGED), str(LANG_REBOOT_NOW)};
571 struct text_message message={lines, 2};
572 if(gui_syncyesno_run(&message, NULL, NULL)==YESNO_YES)
573 rolo_load("/" BOOTFILE);
574 restore = true;
575 boot_changed = false;
577 #endif
578 button = button_get_w_tmo(HZ/5);
579 need_update = gui_synclist_do_button(&tree_lists, button);
581 switch ( button ) {
582 #ifdef TREE_ENTER
583 case TREE_ENTER:
584 case TREE_ENTER | BUTTON_REPEAT:
585 #endif
586 #ifdef TREE_RC_RUN
587 case TREE_RC_RUN:
588 #endif
589 case TREE_RUN:
590 #ifdef TREE_RUN_PRE
591 if (((button == TREE_RUN)
592 #ifdef TREE_RC_RUN_PRE
593 || (button == TREE_RC_RUN))
594 && ((lastbutton != TREE_RC_RUN_PRE)
595 #endif
596 && (lastbutton != TREE_RUN_PRE)))
597 break;
598 #endif
599 /* nothing to do if no files to display */
600 if ( numentries == 0 )
601 break;
603 switch (id3db?db_enter(&tc):ft_enter(&tc))
605 case 1: reload_dir = true; break;
606 case 2: start_wps = true; break;
607 case 3: exit_func = true; break;
608 default: break;
610 restore = true;
611 break;
613 case TREE_EXIT:
614 case TREE_EXIT | BUTTON_REPEAT:
615 #ifdef TREE_RC_EXIT
616 case TREE_RC_EXIT:
617 #endif
618 if (*tc.dirfilter > NUM_FILTER_MODES && tc.dirlevel < 1) {
619 exit_func = true;
620 break;
622 /* if we are in /, nothing to do */
623 if (tc.dirlevel == 0)
624 break;
626 if (id3db)
627 db_exit(&tc);
628 else
629 if (ft_exit(&tc) == 3)
630 exit_func = true;
632 restore = true;
633 break;
635 #ifdef TREE_OFF
636 case TREE_OFF:
637 if (*tc.dirfilter < NUM_FILTER_MODES)
639 /* Stop the music if it is playing */
640 if(audio_status())
641 audio_stop();
642 #if defined(HAVE_CHARGING) && \
643 (CONFIG_KEYPAD == RECORDER_PAD) && !defined(HAVE_SW_POWEROFF)
644 else {
645 if (!charger_inserted()) {
646 if(shutdown_screen())
647 reload_dir = true;
648 } else {
649 charging_splash();
651 restore = true;
653 #endif
655 break;
656 #if defined(HAVE_CHARGING) && !defined(HAVE_POWEROFF_WHILE_CHARGING)
657 case TREE_OFF | BUTTON_REPEAT:
658 if (charger_inserted()) {
659 charging_splash();
660 restore = true;
662 break;
663 #endif
664 #endif /* TREE_OFF */
665 case TREE_MENU:
666 #ifdef TREE_RC_MENU
667 case TREE_RC_MENU:
668 #endif
669 #ifdef TREE_MENU_PRE
670 if (lastbutton != TREE_MENU_PRE
671 #ifdef TREE_RC_MENU_PRE
672 && lastbutton != TREE_RC_MENU_PRE
673 #endif
675 break;
676 #endif
677 /* don't enter menu from plugin browser */
678 if (*tc.dirfilter < NUM_FILTER_MODES)
680 int i;
681 FOR_NB_SCREENS(i)
682 screens[i].stop_scroll();
683 if (main_menu())
684 reload_dir = true;
685 restore = true;
687 id3db = check_changed_id3mode(id3db);
689 else /* use it as a quick exit instead */
690 exit_func = true;
691 break;
693 case TREE_WPS:
694 #ifdef TREE_RC_WPS
695 case TREE_RC_WPS:
696 #endif
697 #ifdef TREE_WPS_PRE
698 if ((lastbutton != TREE_WPS_PRE)
699 #ifdef TREE_RC_WPS
700 && (lastbutton != TREE_RC_WPS_PRE)
701 #endif
703 break;
704 #endif
705 /* don't enter wps from plugin browser etc */
706 if (*tc.dirfilter < NUM_FILTER_MODES)
708 if (audio_status() & AUDIO_STATUS_PLAY)
710 start_wps=true;
712 else
714 start_resume(false);
715 restore = true;
718 break;
720 #ifdef TREE_QUICK
721 case TREE_QUICK:
722 #ifdef TREE_RC_QUICK
723 case TREE_RC_QUICK:
724 #endif
725 /* don't enter f2 from plugin browser */
726 if (*tc.dirfilter < NUM_FILTER_MODES)
728 if (quick_screen_quick(button))
729 reload_dir = true;
730 restore = true;
732 id3db = check_changed_id3mode(id3db);
733 break;
736 #endif
738 #ifdef BUTTON_F3
739 case BUTTON_F3:
740 /* don't enter f3 from plugin browser */
741 if (*tc.dirfilter < NUM_FILTER_MODES)
743 if (quick_screen_f3(button))
744 reload_dir = true;
745 restore = true;
747 break;
748 #endif
750 case TREE_CONTEXT:
751 #ifdef TREE_RC_CONTEXT
752 case TREE_RC_CONTEXT:
753 #endif
754 #ifdef TREE_CONTEXT2
755 case TREE_CONTEXT2:
756 #endif
758 int onplay_result;
759 int attr = 0;
761 if(!numentries)
762 onplay_result = onplay(NULL, 0, curr_context);
763 else {
764 if (id3db)
766 switch (tc.currtable)
768 case allsongs:
769 case songs4album:
770 case songs4artist:
771 case searchsongs:
772 attr=TREE_ATTR_MPA;
773 db_get_filename(&tc, buf, sizeof(buf));
774 break;
777 else
779 attr = dircache[tc.selected_item].attr;
781 if (currdir[1]) /* Not in / */
782 snprintf(buf, sizeof buf, "%s/%s",
783 currdir,
784 dircache[tc.selected_item].name);
785 else /* In / */
786 snprintf(buf, sizeof buf, "/%s",
787 dircache[tc.selected_item].name);
789 onplay_result = onplay(buf, attr, curr_context);
791 switch (onplay_result)
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 start_wps = true;
803 break;
805 break;
808 case BUTTON_NONE:
809 if (thumbnail_time != -1 &&
810 TIME_AFTER(current_tick, thumbnail_time))
811 { /* a delayed hovering thumbnail is due now */
812 int res;
813 if (dircache[lasti].attr & ATTR_DIRECTORY)
815 DEBUGF("Playing directory thumbnail: %s", currdir);
816 res = ft_play_dirname(lasti);
817 if (res < 0) /* failed, not existing */
818 { /* say the number instead, as a fallback */
819 talk_id(VOICE_DIR, false);
820 talk_number(lasti+1, true);
823 else
825 DEBUGF("Playing file thumbnail: %s/%s%s\n",
826 currdir, dircache[lasti].name,
827 file_thumbnail_ext);
828 /* no fallback necessary, we knew in advance
829 that the file exists */
830 ft_play_filename(currdir, dircache[lasti].name);
832 thumbnail_time = -1; /* job done */
834 gui_syncstatusbar_draw(&statusbars, false);
835 break;
837 #ifdef HAVE_HOTSWAP
838 case SYS_FS_CHANGED:
839 if (!id3db)
840 reload_dir = true;
841 /* The 'dir no longer valid' situation will be caught later
842 * by checking the showdir() result. */
843 break;
844 #endif
846 default:
847 if (default_event_handler(button) == SYS_USB_CONNECTED)
849 if(*tc.dirfilter > NUM_FILTER_MODES)
850 /* leave sub-browsers after usb, doing otherwise
851 might be confusing to the user */
852 exit_func = true;
853 else
854 reload_dir = true;
856 break;
859 if ( button )
861 ata_spin();
862 lastbutton = button;
865 if (start_wps && audio_status() )
867 int i;
868 #ifdef HAVE_LCD_COLOR
869 fb_data* old_backdrop;
870 #endif
872 FOR_NB_SCREENS(i)
873 screens[i].stop_scroll();
874 #ifdef HAVE_LCD_COLOR
875 old_backdrop = lcd_get_backdrop();
876 #endif
877 if (gui_wps_show() == SYS_USB_CONNECTED)
878 reload_dir = true;
879 #ifdef HAVE_LCD_COLOR
880 lcd_set_backdrop(old_backdrop);
881 #endif
882 #ifdef HAVE_HOTSWAP
883 else
884 if (!id3db) /* Try reload to catch 'no longer valid' case. */
885 reload_dir = true;
886 #endif
887 id3db = check_changed_id3mode(id3db);
888 restore = true;
889 start_wps=false;
892 check_rescan:
893 /* do we need to rescan dir? */
894 if (reload_dir || reload_root ||
895 lastfilter != *tc.dirfilter ||
896 lastsortcase != global_settings.sort_case)
898 if ( reload_root ) {
899 strcpy(currdir, "/");
900 tc.dirlevel = 0;
901 tc.currtable = 0;
902 tc.currextra = 0;
903 lasttable = -1;
904 lastextra = -1;
905 reload_root = false;
907 if (! reload_dir )
909 gui_synclist_select_item(&tree_lists, 0);
910 gui_synclist_draw(&tree_lists);
911 tc.selected_item = 0;
912 lastdir[0] = 0;
915 lastfilter = *tc.dirfilter;
916 lastsortcase = global_settings.sort_case;
917 restore = true;
918 button_clear_queue(); /* clear button queue */
921 if (exit_func)
922 break;
924 if (restore || reload_dir) {
925 /* restore display */
926 numentries = update_dir();
927 if (currdir[1] && (numentries < 0))
928 { /* not in root and reload failed */
929 reload_root = true; /* try root */
930 reload_dir = false;
931 goto check_rescan;
933 need_update = true;
934 reload_dir = false;
936 if(need_update) {
937 tc.selected_item = gui_synclist_get_sel_pos(&tree_lists);
938 need_update=false;
939 if ( numentries > 0 ) {
940 /* Voice the file if changed */
941 if(lasti != tc.selected_item || restore) {
942 lasti = tc.selected_item;
943 thumbnail_time = -1; /* Cancel whatever we were
944 about to say */
946 /* Directory? */
947 if (dircache[tc.selected_item].attr & ATTR_DIRECTORY)
949 /* play directory thumbnail */
950 switch (global_settings.talk_dir) {
951 case 1: /* dirs as numbers */
952 talk_id(VOICE_DIR, false);
953 talk_number(tc.selected_item+1, true);
954 break;
956 case 2: /* dirs spelled */
957 talk_spell(dircache[tc.selected_item].name,
958 false);
959 break;
961 case 3: /* thumbnail clip */
962 /* "schedule" a thumbnail, to have a little
963 delay */
964 thumbnail_time = current_tick + HOVER_DELAY;
965 break;
967 default:
968 break;
971 else /* file */
973 switch (global_settings.talk_file) {
974 case 1: /* files as numbers */
975 ft_play_filenumber(
976 tc.selected_item-tc.dirsindir+1,
977 dircache[tc.selected_item].attr &
978 TREE_ATTR_MASK);
979 break;
981 case 2: /* files spelled */
982 talk_spell(dircache[tc.selected_item].name,
983 false);
984 break;
986 case 3: /* thumbnail clip */
987 /* "schedule" a thumbnail, to have a little
988 delay */
989 if (dircache[tc.selected_item].attr &
990 TREE_ATTR_THUMBNAIL)
991 thumbnail_time = current_tick + HOVER_DELAY;
992 else
993 /* spell the number as fallback */
994 talk_spell(dircache[tc.selected_item].name,
995 false);
996 break;
998 default:
999 break;
1007 return true;
1010 static int plsize = 0;
1011 static bool add_dir(char* dirname, int len, int fd)
1013 bool abort = false;
1014 DIRCACHED* dir;
1016 /* check for user abort */
1017 if (button_get(false) == TREE_ABORT)
1018 return true;
1020 dir = opendir_cached(dirname);
1021 if(!dir)
1022 return true;
1024 while (true) {
1025 struct dircache_entry *entry;
1027 entry = readdir_cached(dir);
1028 if (!entry)
1029 break;
1030 if (entry->attribute & ATTR_DIRECTORY) {
1031 int dirlen = strlen(dirname);
1032 bool result;
1034 if (!strcmp((char *)entry->d_name, ".") ||
1035 !strcmp((char *)entry->d_name, ".."))
1036 continue;
1038 if (dirname[1])
1039 snprintf(dirname+dirlen, len-dirlen, "/%s", entry->d_name);
1040 else
1041 snprintf(dirname, len, "/%s", entry->d_name);
1043 result = add_dir(dirname, len, fd);
1044 dirname[dirlen] = '\0';
1045 if (result) {
1046 abort = true;
1047 break;
1050 else {
1051 int x = strlen((char *)entry->d_name);
1052 unsigned int i;
1053 char *cp = strrchr((char *)entry->d_name,'.');
1055 if (cp) {
1056 cp++;
1058 /* add all supported audio files to playlists */
1059 for (i=0; i < sizeof(filetypes); i++) {
1060 if (filetypes[i].tree_attr == TREE_ATTR_MPA) {
1061 if (!strcasecmp(cp, filetypes[i].extension))
1063 char buf[8];
1064 int i;
1065 write(fd, dirname, strlen(dirname));
1066 write(fd, "/", 1);
1067 write(fd, entry->d_name, x);
1068 write(fd, "\n", 1);
1070 plsize++;
1071 snprintf(buf, sizeof buf, "%d", plsize);
1072 #ifdef HAVE_LCD_BITMAP
1073 FOR_NB_SCREENS(i)
1075 gui_textarea_clear(&screens[i]);
1076 screens[i].puts(0, 4, (unsigned char *)buf);
1078 #else
1079 x = 10;
1080 if (plsize > 999)
1081 x=7;
1082 else {
1083 if (plsize > 99)
1084 x=8;
1085 else {
1086 if (plsize > 9)
1087 x=9;
1090 FOR_NB_SCREENS(i) {
1091 screens[i].puts(x,0,buf);
1092 gui_textarea_update(&screens[i]);
1094 #endif
1095 break;
1102 closedir_cached(dir);
1104 return abort;
1107 bool create_playlist(void)
1109 int fd;
1110 int i;
1111 char filename[MAX_PATH];
1113 snprintf(filename, sizeof filename, "%s.m3u",
1114 tc.currdir[1] ? tc.currdir : "/root");
1115 FOR_NB_SCREENS(i)
1117 gui_textarea_clear(&screens[i]);
1118 screens[i].puts(0, 0, str(LANG_CREATING));
1119 screens[i].puts_scroll(0, 1, (unsigned char *)filename);
1120 #if defined(HAVE_LCD_BITMAP) || defined(SIMULATOR)
1121 gui_textarea_update(&screens[i]);
1122 #endif
1124 fd = creat(filename,0);
1125 if (fd < 0)
1126 return false;
1128 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
1129 cpu_boost(true);
1130 #endif
1132 snprintf(filename, sizeof(filename), "%s",
1133 tc.currdir[1] ? tc.currdir : "/");
1134 plsize = 0;
1135 add_dir(filename, sizeof(filename), fd);
1136 close(fd);
1138 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
1139 cpu_boost(false);
1140 #endif
1142 sleep(HZ);
1144 return true;
1147 bool rockbox_browse(const char *root, int dirfilter)
1149 static struct tree_context backup;
1151 backup = tc;
1152 reload_dir = true;
1153 memcpy(tc.currdir, root, sizeof(tc.currdir));
1154 start_wps = false;
1155 tc.dirfilter = &dirfilter;
1157 dirbrowse();
1159 tc = backup;
1160 return false;
1163 void tree_init(void)
1165 /* We copy the settings value in case it is changed by the user. We can't
1166 use it until the next reboot. */
1167 max_files = global_settings.max_files_in_dir;
1169 /* initialize tree context struct */
1170 memset(&tc, 0, sizeof(tc));
1171 tc.dirfilter = &global_settings.dirfilter;
1173 tagdb_init();
1175 tc.name_buffer_size = AVERAGE_FILENAME_LENGTH * max_files;
1176 tc.name_buffer = buffer_alloc(tc.name_buffer_size);
1178 tc.dircache_size = max_files * sizeof(struct entry);
1179 tc.dircache = buffer_alloc(tc.dircache_size);
1182 void bookmark_play(char *resume_file, int index, int offset, int seed,
1183 char *filename)
1185 int i;
1186 int len=strlen(resume_file);
1188 if (!strcasecmp(&resume_file[len-4], ".m3u"))
1190 /* Playlist playback */
1191 char* slash;
1192 /* check that the file exists */
1193 int fd = open(resume_file, O_RDONLY);
1194 if(fd<0)
1195 return;
1196 close(fd);
1198 slash = strrchr(resume_file,'/');
1199 if (slash)
1201 char* cp;
1202 *slash=0;
1204 cp=resume_file;
1205 if (!cp[0])
1206 cp="/";
1208 if (playlist_create(cp, slash+1) != -1)
1210 if (global_settings.playlist_shuffle)
1211 playlist_shuffle(seed, -1);
1212 playlist_start(index,offset);
1214 *slash='/';
1217 else
1219 /* Directory playback */
1220 lastdir[0]='\0';
1221 if (playlist_create(resume_file, NULL) != -1)
1223 resume_directory(resume_file);
1224 if (global_settings.playlist_shuffle)
1225 playlist_shuffle(seed, -1);
1227 /* Check if the file is at the same spot in the directory,
1228 else search for it */
1229 if ((strcmp(strrchr(playlist_peek(index) + 1,'/') + 1,
1230 filename)))
1232 for ( i=0; i < playlist_amount(); i++ )
1234 if ((strcmp(strrchr(playlist_peek(i) + 1,'/') + 1,
1235 filename)) == 0)
1236 break;
1238 if (i < playlist_amount())
1239 index = i;
1240 else
1241 return;
1243 playlist_start(index,offset);
1247 start_wps=true;
1250 int ft_play_filenumber(int pos, int attr)
1252 /* try to find a voice ID for the extension, if known */
1253 unsigned int j;
1254 int ext_id = -1; /* default to none */
1255 for (j=0; j<sizeof(filetypes)/sizeof(*filetypes); j++)
1257 if (attr == filetypes[j].tree_attr)
1259 ext_id = filetypes[j].voiceclip;
1260 break;
1264 talk_id(VOICE_FILE, false);
1265 talk_number(pos, true);
1266 talk_id(ext_id, true);
1267 return 1;
1270 int ft_play_dirname(int start_index)
1272 int fd;
1273 char dirname_mp3_filename[MAX_PATH+1];
1274 struct entry *dircache = tc.dircache;
1276 if (audio_status() & AUDIO_STATUS_PLAY)
1277 return 0;
1279 snprintf(dirname_mp3_filename, sizeof(dirname_mp3_filename), "%s/%s/%s",
1280 tc.currdir[1] ? tc.currdir : "" , dircache[start_index].name,
1281 dir_thumbnail_name);
1283 DEBUGF("Checking for %s\n", dirname_mp3_filename);
1285 fd = open(dirname_mp3_filename, O_RDONLY);
1286 if (fd < 0)
1288 DEBUGF("Failed to find: %s\n", dirname_mp3_filename);
1289 return -1;
1292 close(fd);
1294 DEBUGF("Found: %s\n", dirname_mp3_filename);
1296 talk_file(dirname_mp3_filename, false);
1297 return 1;
1300 void ft_play_filename(char *dir, char *file)
1302 char name_mp3_filename[MAX_PATH+1];
1304 if (audio_status() & AUDIO_STATUS_PLAY)
1305 return;
1307 if (strcasecmp(&file[strlen(file) - strlen(file_thumbnail_ext)],
1308 file_thumbnail_ext))
1309 { /* file has no .talk extension */
1310 snprintf(name_mp3_filename, sizeof(name_mp3_filename),
1311 "%s/%s%s", dir, file, file_thumbnail_ext);
1313 talk_file(name_mp3_filename, false);
1315 else
1316 { /* it already is a .talk file, play this directly */
1317 snprintf(name_mp3_filename, sizeof(name_mp3_filename),
1318 "%s/%s", dir, file);
1319 talk_id(LANG_VOICE_DIR_HOVER, false); /* prefix it */
1320 talk_file(name_mp3_filename, true);
1324 /* These two functions are called by the USB and shutdown handlers */
1325 void tree_flush(void)
1327 rundb_shutdown();
1328 tagdb_shutdown();
1329 playlist_shutdown();
1330 #ifdef HAVE_DIRCACHE
1331 if (global_settings.dircache)
1333 if (dircache_is_enabled())
1334 global_settings.dircache_size = dircache_get_cache_size();
1335 dircache_disable();
1337 else
1339 global_settings.dircache_size = 0;
1341 settings_save();
1342 #endif
1345 void tree_restore(void)
1347 tagdb_init();
1348 rundb_init();
1349 #ifdef HAVE_DIRCACHE
1350 if (global_settings.dircache)
1352 /* Print "Scanning disk..." to the display. */
1353 int i;
1354 FOR_NB_SCREENS(i)
1356 screens[i].putsxy((LCD_WIDTH/2) -
1357 ((strlen(str(LANG_DIRCACHE_BUILDING)) *
1358 screens[i].char_width)/2),
1359 LCD_HEIGHT-screens[i].char_height*3,
1360 str(LANG_DIRCACHE_BUILDING));
1361 gui_textarea_update(&screens[i]);
1364 dircache_build(global_settings.dircache_size);
1366 /* Clean the text when we are done. */
1367 FOR_NB_SCREENS(i)
1369 gui_textarea_clear(&screens[i]);
1372 #endif