Some changes to the remote code : some one lines function turned into macros ; change...
[Rockbox.git] / apps / tree.c
blob738b9fcf11171bcf8613332927fa94650b3ad828
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 "wps.h"
40 #include "wps-display.h"
41 #include "settings.h"
42 #include "status.h"
43 #include "debug.h"
44 #include "ata.h"
45 #include "rolo.h"
46 #include "icons.h"
47 #include "lang.h"
48 #include "language.h"
49 #include "screens.h"
50 #include "keyboard.h"
51 #include "bookmark.h"
52 #include "onplay.h"
53 #include "buffer.h"
54 #include "plugin.h"
55 #include "power.h"
56 #include "action.h"
57 #include "talk.h"
58 #include "filetypes.h"
59 #include "misc.h"
60 #include "filetree.h"
61 #include "dbtree.h"
62 #include "recorder/recording.h"
63 #include "rtc.h"
64 #include "dircache.h"
66 /* gui api */
67 #include "list.h"
68 #include "statusbar.h"
69 #include "splash.h"
70 #include "buttonbar.h"
72 #ifdef HAVE_LCD_BITMAP
73 #include "widgets.h"
74 #endif
76 /* a table for the know file types */
77 const struct filetype filetypes[] = {
78 { "mp3", TREE_ATTR_MPA, Icon_Audio, VOICE_EXT_MPA },
79 { "mp2", TREE_ATTR_MPA, Icon_Audio, VOICE_EXT_MPA },
80 { "mpa", TREE_ATTR_MPA, Icon_Audio, VOICE_EXT_MPA },
81 #if CONFIG_CODEC == SWCODEC
82 /* Temporary hack to allow playlist creation */
83 { "mp1", TREE_ATTR_MPA, Icon_Audio, VOICE_EXT_MPA },
84 { "ogg", TREE_ATTR_MPA, Icon_Audio, VOICE_EXT_MPA },
85 { "wma", TREE_ATTR_MPA, Icon_Audio, VOICE_EXT_MPA },
86 { "wav", TREE_ATTR_MPA, Icon_Audio, VOICE_EXT_MPA },
87 { "flac", TREE_ATTR_MPA, Icon_Audio, VOICE_EXT_MPA },
88 { "ac3", TREE_ATTR_MPA, Icon_Audio, VOICE_EXT_MPA },
89 { "a52", TREE_ATTR_MPA, Icon_Audio, VOICE_EXT_MPA },
90 { "mpc", TREE_ATTR_MPA, Icon_Audio, VOICE_EXT_MPA },
91 { "wv", TREE_ATTR_MPA, Icon_Audio, VOICE_EXT_MPA },
92 { "m4a", TREE_ATTR_MPA, Icon_Audio, VOICE_EXT_MPA },
93 #endif
94 { "m3u", TREE_ATTR_M3U, Icon_Playlist, LANG_PLAYLIST },
95 { "cfg", TREE_ATTR_CFG, Icon_Config, VOICE_EXT_CFG },
96 { "wps", TREE_ATTR_WPS, Icon_Wps, VOICE_EXT_WPS },
97 { "lng", TREE_ATTR_LNG, Icon_Language, LANG_LANGUAGE },
98 { "rock",TREE_ATTR_ROCK,Icon_Plugin, VOICE_EXT_ROCK },
99 #ifdef HAVE_LCD_BITMAP
100 { "fnt", TREE_ATTR_FONT,Icon_Font, VOICE_EXT_FONT },
101 #endif
102 { "bmark",TREE_ATTR_BMARK, Icon_Bookmark, VOICE_EXT_BMARK },
103 #ifdef BOOTFILE_EXT
104 { BOOTFILE_EXT, TREE_ATTR_MOD, Icon_Firmware, VOICE_EXT_AJZ },
105 #endif /* #ifndef SIMULATOR */
108 struct gui_synclist tree_lists;
110 /* I put it here because other files doesn't use it yet,
111 * but should be elsewhere since it will be used mostly everywhere */
112 struct gui_syncstatusbar statusbars;
113 #ifdef HAS_BUTTONBAR
114 struct gui_buttonbar tree_buttonbar;
115 #endif
116 static struct tree_context tc;
118 bool boot_changed = false;
120 char lastfile[MAX_PATH];
121 static char lastdir[MAX_PATH];
122 static int lasttable, lastextra, lastfirstpos;
123 static int max_files = 0;
125 static bool reload_dir = false;
127 static bool start_wps = false;
128 static bool dirbrowse(void);
129 static int curr_context = false;/* id3db or tree*/
132 * removes the extension of filename (if it doesn't start with a .)
133 * puts the result in buffer
135 char * strip_extension(char * filename, char * buffer)
137 int dotpos;
138 char * dot=strrchr(filename, '.');
139 if(dot!=0 && filename[0]!='.')
141 dotpos = dot-filename;
142 strncpy(buffer, filename, dotpos);
143 buffer[dotpos]='\0';
144 return(buffer);
146 else
147 return(filename);
149 char * tree_get_filename(int selected_item, void * data, char *buffer)
151 struct tree_context * local_tc=(struct tree_context *)data;
152 char *name;
153 int attr=0;
154 bool id3db = *(local_tc->dirfilter) == SHOW_ID3DB;
156 if (id3db) {
157 name = ((char**)local_tc->dircache)[selected_item * local_tc->dentry_size];
159 else {
160 struct entry* dc = local_tc->dircache;
161 struct entry* e = &dc[selected_item];
162 name = e->name;
163 attr = e->attr;
165 /* if any file filter is on, and if it's not a directory,
166 * strip the extension */
168 if ( (*(local_tc->dirfilter) != SHOW_ID3DB) && !(attr & ATTR_DIRECTORY)
169 && (*(local_tc->dirfilter) != SHOW_ALL) )
171 return(strip_extension(name, buffer));
173 return(name);
177 void tree_get_fileicon(int selected_item, void * data, ICON * icon)
179 struct tree_context * local_tc=(struct tree_context *)data;
180 bool id3db = *(local_tc->dirfilter) == SHOW_ID3DB;
181 if (id3db) {
182 *icon = db_get_icon(&tc);
184 else {
185 struct entry* dc = local_tc->dircache;
186 struct entry* e = &dc[selected_item];
187 *icon = filetype_get_icon(e->attr);
191 bool check_rockboxdir(void)
193 DIR *dir = opendir(ROCKBOX_DIR);
194 if(!dir)
196 int i;
197 for(i = 0;i < NB_SCREENS;++i)
198 screens[i].clear_display();
199 gui_syncsplash(HZ*2, true, str(LANG_NO_ROCKBOX_DIR));
200 for(i = 0;i < NB_SCREENS;++i)
201 screens[i].clear_display();
202 gui_syncsplash(HZ*2, true, str(LANG_INSTALLATION_INCOMPLETE));
203 return false;
205 closedir(dir);
206 return true;
209 void browse_root(void)
211 /* essential to all programs that wants to display things */
212 screen_access_init();
214 filetype_init();
215 check_rockboxdir();
217 strcpy(tc.currdir, "/");
219 #ifdef HAVE_LCD_CHARCELLS
220 int i;
221 for(i = 0;i < NB_SCREENS;++i)
222 screens[i].double_height(false);
223 #endif
224 #ifdef HAS_BUTTONBAR
225 gui_buttonbar_init(&tree_buttonbar);
226 /* since archos only have one screen, no need to create more than that */
227 gui_buttonbar_set_display(&tree_buttonbar, &(screens[SCREEN_MAIN]) );
228 #endif
229 gui_syncstatusbar_init(&statusbars);
230 gui_synclist_init(&tree_lists, &tree_get_fileicon, &tree_get_filename, &tc);
231 #ifndef SIMULATOR
232 dirbrowse();
233 #else
234 if (!dirbrowse()) {
235 DEBUGF("No filesystem found. Have you forgotten to create it?\n");
237 #endif
240 void tree_get_filetypes(const struct filetype** types, int* count)
242 *types = filetypes;
243 *count = sizeof(filetypes) / sizeof(*filetypes);
246 struct tree_context* tree_get_context(void)
248 return &tc;
251 /* talkbox hovering delay, to avoid immediate disk activity */
252 #define HOVER_DELAY (HZ/2)
254 * Returns the position of a given file in the current directory
255 * returns -1 if not found
257 int tree_get_file_position(char * filename)
259 int i;
260 /* use lastfile to determine the selected item (default=0) */
261 for (i=0; i < tc.filesindir; i++)
263 struct entry* dc = tc.dircache;
264 struct entry* e = &dc[i];
265 if (!strcasecmp(e->name, filename))
266 return(i);
268 return(-1);/* no file can match, returns undefined */
272 * Called when a new dir is loaded (for example when returning from other apps ...)
273 * also completely redraws the tree
275 static int update_dir(void)
277 bool id3db = *tc.dirfilter == SHOW_ID3DB;
278 bool changed = false;
279 /* Checks for changes */
280 if (id3db) {
281 if (tc.currtable != lasttable ||
282 tc.currextra != lastextra ||
283 tc.firstpos != lastfirstpos)
285 if (db_load(&tc) < 0)
286 return -1;
288 lasttable = tc.currtable;
289 lastextra = tc.currextra;
290 lastfirstpos = tc.firstpos;
291 changed = true;
294 else {
295 /* if the tc.currdir has been changed, reload it ...*/
296 if (strncmp(tc.currdir, lastdir, sizeof(lastdir)) || reload_dir) {
298 if (ft_load(&tc, NULL) < 0)
299 return -1;
300 strcpy(lastdir, tc.currdir);
301 changed = true;
304 /* if selected item is undefined */
305 if (tc.selected_item == -1)
307 /* use lastfile to determine the selected item */
308 tc.selected_item = tree_get_file_position(lastfile);
310 /* If the file doesn't exists, select the first one (default) */
311 if(tc.selected_item < 0)
312 tc.selected_item = 0;
313 changed = true;
315 if (changed)
317 if(!id3db && (tc.dirfull ||
318 tc.filesindir == global_settings.max_files_in_dir) )
320 /* dir full */
321 int i;
322 for(i = 0;i < NB_SCREENS;++i)
324 #ifdef HAVE_LCD_CHARCELLS
325 screens[i].double_height(false);
326 #endif
327 screens[i].clear_display();
328 screens[i].puts(0,0,str(LANG_SHOWDIR_ERROR_BUFFER));
329 screens[i].puts(0,1,str(LANG_SHOWDIR_ERROR_FULL));
330 #if defined(HAVE_LCD_BITMAP) || defined(SIMULATOR)
331 screens[i].update();
332 #endif
334 sleep(HZ*2);
335 for(i = 0;i < NB_SCREENS;++i)
336 screens[i].clear_display();
339 gui_synclist_set_nb_items(&tree_lists, tc.filesindir);
340 gui_synclist_select_item(&tree_lists, tc.selected_item);
341 gui_synclist_draw(&tree_lists);
342 gui_syncstatusbar_draw(&statusbars, true);
343 #ifdef HAS_BUTTONBAR
344 if (global_settings.buttonbar) {
345 if (*tc.dirfilter < NUM_FILTER_MODES)
346 gui_buttonbar_set(&tree_buttonbar, str(LANG_DIRBROWSE_F1),
347 str(LANG_DIRBROWSE_F2),
348 str(LANG_DIRBROWSE_F3));
349 else
350 gui_buttonbar_set(&tree_buttonbar, "<<<", "", "");
351 gui_buttonbar_draw(&tree_buttonbar);
353 #endif
354 return tc.filesindir;
357 /* load tracks from specified directory to resume play */
358 void resume_directory(const char *dir)
360 if (ft_load(&tc, dir) < 0)
361 return;
362 lastdir[0] = 0;
364 ft_build_playlist(&tc, 0);
367 /* Returns the current working directory and also writes cwd to buf if
368 non-NULL. In case of error, returns NULL. */
369 char *getcwd(char *buf, int size)
371 if (!buf)
372 return tc.currdir;
373 else if (size > 0)
375 strncpy(buf, tc.currdir, size);
376 return buf;
378 else
379 return NULL;
382 /* Force a reload of the directory next time directory browser is called */
383 void reload_directory(void)
385 reload_dir = true;
388 static void start_resume(bool just_powered_on)
390 bool do_resume = false;
391 if ( global_settings.resume_index != -1 ) {
392 DEBUGF("Resume index %X offset %X\n",
393 global_settings.resume_index,
394 global_settings.resume_offset);
396 #ifdef HAVE_ALARM_MOD
397 if ( rtc_check_alarm_started(true) ) {
398 rtc_enable_alarm(false);
399 do_resume = true;
401 #endif
403 /* always resume? */
404 if ( global_settings.resume || ! just_powered_on)
405 do_resume = true;
407 if (! do_resume) return;
409 if (playlist_resume() != -1)
411 playlist_start(global_settings.resume_index,
412 global_settings.resume_offset);
414 start_wps = true;
416 else return;
417 } else if (! just_powered_on) {
418 gui_syncsplash(HZ*2, true, str(LANG_NOTHING_TO_RESUME));
422 /* Selects a file and update tree context properly */
423 void set_current_file(char *path)
425 char *name;
426 int i;
428 /* in ID3DB mode it is a bad idea to call this function */
429 /* (only happens with `follow playlist') */
430 if( *tc.dirfilter == SHOW_ID3DB )
431 return;
433 /* separate directory from filename */
434 /* gets the directory's name and put it into tc.currdir */
435 name = strrchr(path+1,'/');
436 if (name)
438 *name = 0;
439 strcpy(tc.currdir, path);
440 *name = '/';
441 name++;
443 else
445 strcpy(tc.currdir, "/");
446 name = path+1;
449 strcpy(lastfile, name);
451 /* undefined item selected */
452 tc.selected_item = -1;
454 /* If we changed dir we must recalculate the dirlevel
455 and adjust the selected history properly */
456 if (strncmp(tc.currdir,lastdir,sizeof(lastdir)))
458 tc.dirlevel = 0;
459 tc.selected_item_history[tc.dirlevel] = -1;
461 /* use '/' to calculate dirlevel */
462 /* FIXME : strlen(path) : crazy oO better to store it at
463 the beginning */
464 int path_len = strlen(path) + 1;
465 for (i = 1; i < path_len; i++)
467 if (path[i] == '/')
469 tc.dirlevel++;
470 tc.selected_item_history[tc.dirlevel] = -1;
476 static bool check_changed_id3mode(bool currmode)
478 if (currmode != (global_settings.dirfilter == SHOW_ID3DB)) {
479 currmode = global_settings.dirfilter == SHOW_ID3DB;
480 if (currmode) {
481 curr_context=CONTEXT_ID3DB;
482 db_load(&tc);
484 else
486 curr_context=CONTEXT_TREE;
487 ft_load(&tc, NULL);
490 return currmode;
492 /* main loop, handles key events */
493 static bool dirbrowse(void)
495 int numentries=0;
496 char buf[MAX_PATH];
497 int lasti = -1;
498 unsigned button;
499 bool reload_root = false;
500 int lastfilter = *tc.dirfilter;
501 bool lastsortcase = global_settings.sort_case;
502 bool need_update = true;
503 bool exit_func = false;
504 long thumbnail_time = -1; /* for delaying a thumbnail */
506 unsigned lastbutton = 0;
507 char* currdir = tc.currdir; /* just a shortcut */
508 bool id3db = *tc.dirfilter == SHOW_ID3DB;
510 if (id3db)
511 curr_context=CONTEXT_ID3DB;
512 else
513 curr_context=CONTEXT_TREE;
514 #ifdef HAVE_LCD_BITMAP
515 screen_access_update_nb_lines();
516 #endif
517 tc.selected_item = 0;
518 tc.dirlevel=0;
519 tc.firstpos=0;
520 lasttable = -1;
521 lastextra = -1;
522 lastfirstpos = 0;
524 if (*tc.dirfilter < NUM_FILTER_MODES) {
525 #ifdef HAVE_RECORDING
526 #ifndef SIMULATOR
527 if (global_settings.rec_startup) {
528 /* We fake being in the menu structure by calling
529 the appropriate parent when we drop out of each screen */
530 recording_screen();
531 rec_menu();
532 main_menu();
534 else
535 #endif
536 #endif
537 start_resume(true);
540 /* If we don't need to show the wps, draw the dir */
541 if (!start_wps) {
542 numentries = update_dir();
543 if (numentries == -1)
544 return false; /* currdir is not a directory */
546 if (*tc.dirfilter > NUM_FILTER_MODES && numentries==0)
548 gui_syncsplash(HZ*2, true, str(LANG_NO_FILES));
549 return false; /* No files found for rockbox_browser() */
553 while(1) {
554 struct entry *dircache = tc.dircache;
555 bool restore = false;
557 button = button_get_w_tmo(HZ/5);
559 #ifdef BOOTFILE
560 if (boot_changed) {
561 bool stop = false;
562 unsigned int button;
563 int i;
564 for(i = 0;i < NB_SCREENS;++i)
566 screens[i].clear_display();
567 screens[i].puts(0,0,str(LANG_BOOT_CHANGED));
568 screens[i].puts(0,1,str(LANG_REBOOT_NOW));
569 #ifdef HAVE_LCD_BITMAP
570 screens[i].puts(0,3,str(LANG_CONFIRM_WITH_PLAY_RECORDER));
571 screens[i].puts(0,4,str(LANG_CANCEL_WITH_ANY_RECORDER));
572 screens[i].update();
573 #endif
575 while (!stop) {
576 button = button_get(true);
577 switch (button) {
578 case TREE_RUN:
579 #ifdef TREE_RC_RUN
580 case TREE_RC_RUN:
581 #endif
582 rolo_load("/" BOOTFILE);
583 stop = true;
584 break;
586 default:
587 if(default_event_handler(button) ||
588 (button & BUTTON_REL))
589 stop = true;
590 break;
594 restore = true;
595 boot_changed = false;
597 #endif
598 need_update = gui_synclist_do_button(&tree_lists, button);
600 switch ( button ) {
601 #ifdef TREE_ENTER
602 case TREE_ENTER:
603 case TREE_ENTER | BUTTON_REPEAT:
604 #endif
605 #ifdef TREE_RC_RUN
606 case TREE_RC_RUN:
607 #endif
608 case TREE_RUN:
609 #ifdef TREE_RUN_PRE
610 if (((button == TREE_RUN)
611 #ifdef TREE_RC_RUN_PRE
612 || (button == TREE_RC_RUN))
613 && ((lastbutton != TREE_RC_RUN_PRE)
614 #endif
615 && (lastbutton != TREE_RUN_PRE)))
616 break;
617 #endif
618 /* nothing to do if no files to display */
619 if ( numentries == 0 )
620 break;
622 switch (id3db?db_enter(&tc):ft_enter(&tc))
624 case 1: reload_dir = true; break;
625 case 2: start_wps = true; break;
626 case 3: exit_func = true; break;
627 default: break;
629 restore = true;
630 break;
632 case TREE_EXIT:
633 case TREE_EXIT | BUTTON_REPEAT:
634 #ifdef TREE_RC_EXIT
635 case TREE_RC_EXIT:
636 #endif
637 if (*tc.dirfilter > NUM_FILTER_MODES && tc.dirlevel < 1) {
638 exit_func = true;
639 break;
641 /* if we are in /, nothing to do */
642 if (tc.dirlevel == 0)
643 break;
645 if (id3db)
646 db_exit(&tc);
647 else
648 if (ft_exit(&tc) == 3)
649 exit_func = true;
651 restore = true;
652 break;
654 #ifdef TREE_OFF
655 case TREE_OFF:
656 if (*tc.dirfilter < NUM_FILTER_MODES)
658 /* Stop the music if it is playing */
659 if(audio_status())
660 audio_stop();
661 #if (CONFIG_KEYPAD == RECORDER_PAD) && !defined(HAVE_SW_POWEROFF)
662 else {
663 if (!charger_inserted()) {
664 if(shutdown_screen())
665 reload_dir = true;
666 } else {
667 charging_splash();
669 restore = true;
671 #endif
673 break;
674 #if defined(HAVE_CHARGING) && !defined(HAVE_POWEROFF_WHILE_CHARGING)
675 case TREE_OFF | BUTTON_REPEAT:
676 if (charger_inserted()) {
677 charging_splash();
678 restore = true;
680 break;
681 #endif
682 #endif /* TREE_OFF */
683 case TREE_MENU:
684 #ifdef TREE_RC_MENU
685 case TREE_RC_MENU:
686 #endif
687 #ifdef TREE_MENU_PRE
688 if (lastbutton != TREE_MENU_PRE)
689 break;
690 #endif
691 /* don't enter menu from plugin browser */
692 if (*tc.dirfilter < NUM_FILTER_MODES)
694 int i;
695 for(i = 0;i < NB_SCREENS;++i)
696 screens[i].stop_scroll();
697 if (main_menu())
698 reload_dir = true;
699 restore = true;
701 id3db = check_changed_id3mode(id3db);
703 else /* use it as a quick exit instead */
704 exit_func = true;
705 break;
707 case TREE_WPS:
708 #ifdef TREE_RC_WPS
709 case TREE_RC_WPS:
710 #endif
711 #ifdef TREE_WPS_PRE
712 if ((lastbutton != TREE_WPS_PRE)
713 #ifdef TREE_RC_WPS
714 && (lastbutton != TREE_RC_WPS_PRE)
715 #endif
717 break;
718 #endif
719 /* don't enter wps from plugin browser etc */
720 if (*tc.dirfilter < NUM_FILTER_MODES)
722 if (audio_status() & AUDIO_STATUS_PLAY)
724 start_wps=true;
726 else
728 start_resume(false);
729 restore = true;
732 break;
734 #ifdef TREE_QUICK
735 case TREE_QUICK:
736 /* don't enter f2 from plugin browser */
737 if (*tc.dirfilter < NUM_FILTER_MODES)
739 if (quick_screen(curr_context, TREE_QUICK))
740 reload_dir = true;
741 restore = true;
743 id3db = check_changed_id3mode(id3db);
744 break;
747 #endif
749 #ifdef BUTTON_F3
750 case BUTTON_F3:
751 /* don't enter f3 from plugin browser */
752 if (*tc.dirfilter < NUM_FILTER_MODES)
754 if (quick_screen(curr_context, BUTTON_F3))
755 reload_dir = true;
756 screen_access_update_nb_lines();
757 restore = true;
759 break;
760 #endif
762 case TREE_CONTEXT:
763 #ifdef TREE_RC_CONTEXT
764 case TREE_RC_CONTEXT:
765 #endif
766 #ifdef TREE_CONTEXT2
767 case TREE_CONTEXT2:
768 #endif
770 int onplay_result;
771 int attr = 0;
773 if(!numentries)
774 onplay_result = onplay(NULL, 0, curr_context);
775 else {
776 if (id3db)
778 switch (tc.currtable)
780 case allsongs:
781 case songs4album:
782 case songs4artist:
783 case searchsongs:
784 attr=TREE_ATTR_MPA;
785 db_get_filename(&tc, buf, sizeof(buf));
786 break;
789 else
791 attr = dircache[tc.selected_item].attr;
793 if (currdir[1]) /* Not in / */
794 snprintf(buf, sizeof buf, "%s/%s",
795 currdir,
796 dircache[tc.selected_item].name);
797 else /* In / */
798 snprintf(buf, sizeof buf, "/%s",
799 dircache[tc.selected_item].name);
801 onplay_result = onplay(buf, attr, curr_context);
804 switch (onplay_result)
806 case ONPLAY_OK:
807 restore = true;
808 break;
810 case ONPLAY_RELOAD_DIR:
811 reload_dir = true;
812 break;
814 case ONPLAY_START_PLAY:
815 start_wps = true;
816 break;
818 break;
821 case BUTTON_NONE:
822 if (thumbnail_time != -1 &&
823 TIME_AFTER(current_tick, thumbnail_time))
824 { /* a delayed hovering thumbnail is due now */
825 int res;
826 if (dircache[lasti].attr & ATTR_DIRECTORY)
828 DEBUGF("Playing directory thumbnail: %s", currdir);
829 res = ft_play_dirname(lasti);
830 if (res < 0) /* failed, not existing */
831 { /* say the number instead, as a fallback */
832 talk_id(VOICE_DIR, false);
833 talk_number(lasti+1, true);
836 else
838 DEBUGF("Playing file thumbnail: %s/%s%s\n",
839 currdir, dircache[lasti].name, file_thumbnail_ext);
840 /* no fallback necessary, we knew in advance
841 that the file exists */
842 ft_play_filename(currdir, dircache[lasti].name);
844 thumbnail_time = -1; /* job done */
846 gui_syncstatusbar_draw(&statusbars, false);
847 break;
849 #ifdef HAVE_HOTSWAP
850 case SYS_FS_CHANGED:
851 if (!id3db)
852 reload_dir = true;
853 /* The 'dir no longer valid' situation will be caught later
854 * by checking the showdir() result. */
855 break;
856 #endif
858 default:
859 if (default_event_handler(button) == SYS_USB_CONNECTED)
861 if(*tc.dirfilter > NUM_FILTER_MODES)
862 /* leave sub-browsers after usb, doing otherwise
863 might be confusing to the user */
864 exit_func = true;
865 else
866 reload_dir = true;
868 break;
871 if ( button )
873 ata_spin();
874 lastbutton = button;
877 if (start_wps && audio_status() )
879 int i;
880 for(i = 0;i < NB_SCREENS;++i)
881 screens[i].stop_scroll();
882 if (wps_show() == SYS_USB_CONNECTED)
883 reload_dir = true;
884 #ifdef HAVE_HOTSWAP
885 else
886 if (!id3db) /* Try reload to catch 'no longer valid' case. */
887 reload_dir = true;
888 #endif
889 #ifdef HAVE_LCD_BITMAP
890 screen_access_update_nb_lines();
891 #endif
892 id3db = check_changed_id3mode(id3db);
893 restore = true;
894 start_wps=false;
897 check_rescan:
898 /* do we need to rescan dir? */
899 if (reload_dir || reload_root ||
900 lastfilter != *tc.dirfilter ||
901 lastsortcase != global_settings.sort_case)
903 if ( reload_root ) {
904 strcpy(currdir, "/");
905 tc.dirlevel = 0;
906 tc.currtable = 0;
907 tc.currextra = 0;
908 lasttable = -1;
909 lastextra = -1;
910 reload_root = false;
912 if (! reload_dir )
914 gui_synclist_select_item(&tree_lists, 0);
915 gui_synclist_draw(&tree_lists);
916 tc.selected_item = 0;
917 lastdir[0] = 0;
920 lastfilter = *tc.dirfilter;
921 lastsortcase = global_settings.sort_case;
922 restore = true;
923 button_clear_queue(); /* clear button queue */
926 if (exit_func)
927 break;
929 if (restore || reload_dir) {
930 /* restore display */
931 #ifdef HAVE_LCD_BITMAP
932 screen_access_update_nb_lines();
933 #endif
934 numentries = update_dir();
935 if (currdir[1] && (numentries < 0))
936 { /* not in root and reload failed */
937 reload_root = true; /* try root */
938 reload_dir = false;
939 goto check_rescan;
941 need_update = true;
942 reload_dir = false;
944 if(need_update) {
945 tc.selected_item = gui_synclist_get_sel_pos(&tree_lists);
946 need_update=false;
947 if ( numentries > 0 ) {
948 /* Voice the file if changed */
949 if(lasti != tc.selected_item || restore) {
950 lasti = tc.selected_item;
951 thumbnail_time = -1; /* Cancel whatever we were
952 about to say */
954 /* Directory? */
955 if (dircache[tc.selected_item].attr & ATTR_DIRECTORY)
957 /* play directory thumbnail */
958 switch (global_settings.talk_dir) {
959 case 1: /* dirs as numbers */
960 talk_id(VOICE_DIR, false);
961 talk_number(tc.selected_item+1, true);
962 break;
964 case 2: /* dirs spelled */
965 talk_spell(dircache[tc.selected_item].name,
966 false);
967 break;
969 case 3: /* thumbnail clip */
970 /* "schedule" a thumbnail, to have a little
971 delay */
972 thumbnail_time = current_tick + HOVER_DELAY;
973 break;
975 default:
976 break;
979 else /* file */
981 switch (global_settings.talk_file) {
982 case 1: /* files as numbers */
983 ft_play_filenumber(
984 tc.selected_item-tc.dirsindir+1,
985 dircache[tc.selected_item].attr &
986 TREE_ATTR_MASK);
987 break;
989 case 2: /* files spelled */
990 talk_spell(dircache[tc.selected_item].name,
991 false);
992 break;
994 case 3: /* thumbnail clip */
995 /* "schedule" a thumbnail, to have a little
996 delay */
997 if (dircache[tc.selected_item].attr &
998 TREE_ATTR_THUMBNAIL)
999 thumbnail_time = current_tick + HOVER_DELAY;
1000 else
1001 /* spell the number as fallback */
1002 talk_spell(dircache[tc.selected_item].name,
1003 false);
1004 break;
1006 default:
1007 break;
1015 return true;
1018 static int plsize = 0;
1019 static bool add_dir(char* dirname, int len, int fd)
1021 bool abort = false;
1022 DIRCACHED* dir;
1024 /* check for user abort */
1025 #ifdef BUTTON_STOP
1026 if (button_get(false) == BUTTON_STOP)
1027 #else
1028 if (button_get(false) == BUTTON_OFF)
1029 #endif
1030 return true;
1032 dir = opendir_cached(dirname);
1033 if(!dir)
1034 return true;
1036 while (true) {
1037 struct dircache_entry *entry;
1039 entry = readdir_cached(dir);
1040 if (!entry)
1041 break;
1042 if (entry->attribute & ATTR_DIRECTORY) {
1043 int dirlen = strlen(dirname);
1044 bool result;
1046 if (!strcmp(entry->d_name, ".") ||
1047 !strcmp(entry->d_name, ".."))
1048 continue;
1050 if (dirname[1])
1051 snprintf(dirname+dirlen, len-dirlen, "/%s", entry->d_name);
1052 else
1053 snprintf(dirname, len, "/%s", entry->d_name);
1055 result = add_dir(dirname, len, fd);
1056 dirname[dirlen] = '\0';
1057 if (result) {
1058 abort = true;
1059 break;
1062 else {
1063 int x = strlen(entry->d_name);
1064 unsigned int i;
1065 char *cp = strrchr(entry->d_name,'.');
1067 if (cp) {
1068 cp++;
1070 /* add all supported audio files to playlists */
1071 for (i=0; i < sizeof(filetypes); i++) {
1072 if (filetypes[i].tree_attr == TREE_ATTR_MPA) {
1073 if (!strcasecmp(cp, filetypes[i].extension))
1075 char buf[8];
1076 int i;
1077 write(fd, dirname, strlen(dirname));
1078 write(fd, "/", 1);
1079 write(fd, entry->d_name, x);
1080 write(fd, "\n", 1);
1082 plsize++;
1083 snprintf(buf, sizeof buf, "%d", plsize);
1084 #ifdef HAVE_LCD_BITMAP
1085 for(i = 0;i < NB_SCREENS;++i)
1087 screens[i].puts(0,4,buf);
1088 screens[i].update();
1090 #else
1091 x = 10;
1092 if (plsize > 999)
1093 x=7;
1094 else {
1095 if (plsize > 99)
1096 x=8;
1097 else {
1098 if (plsize > 9)
1099 x=9;
1102 for(i = 0;i < NB_SCREENS;++i)
1103 screens[i].puts(x,0,buf);
1104 #endif
1105 break;
1112 closedir_cached(dir);
1114 return abort;
1117 bool create_playlist(void)
1119 int fd;
1120 int i;
1121 char filename[MAX_PATH];
1123 snprintf(filename, sizeof filename, "%s.m3u",
1124 tc.currdir[1] ? tc.currdir : "/root");
1125 for(i = 0;i < NB_SCREENS;++i)
1127 screens[i].clear_display();
1128 screens[i].puts(0,0,str(LANG_CREATING));
1129 screens[i].puts_scroll(0,1,filename);
1130 #if defined(HAVE_LCD_BITMAP) || defined(SIMULATOR)
1131 screens[i].update();
1132 #endif
1134 fd = creat(filename,0);
1135 if (fd < 0)
1136 return false;
1138 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
1139 cpu_boost(true);
1140 #endif
1142 snprintf(filename, sizeof(filename), "%s",
1143 tc.currdir[1] ? tc.currdir : "/");
1144 plsize = 0;
1145 add_dir(filename, sizeof(filename), fd);
1146 close(fd);
1148 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
1149 cpu_boost(false);
1150 #endif
1152 sleep(HZ);
1154 return true;
1157 bool rockbox_browse(const char *root, int dirfilter)
1159 static struct tree_context backup;
1161 backup = tc;
1162 reload_dir = true;
1163 memcpy(tc.currdir, root, sizeof(tc.currdir));
1164 start_wps = false;
1165 tc.dirfilter = &dirfilter;
1167 dirbrowse();
1169 tc = backup;
1170 return false;
1173 void tree_init(void)
1175 /* We copy the settings value in case it is changed by the user. We can't
1176 use it until the next reboot. */
1177 max_files = global_settings.max_files_in_dir;
1179 /* initialize tree context struct */
1180 memset(&tc, 0, sizeof(tc));
1181 tc.dirfilter = &global_settings.dirfilter;
1183 tagdb_init();
1185 tc.name_buffer_size = AVERAGE_FILENAME_LENGTH * max_files;
1186 tc.name_buffer = buffer_alloc(tc.name_buffer_size);
1188 tc.dircache_size = max_files * sizeof(struct entry);
1189 tc.dircache = buffer_alloc(tc.dircache_size);
1192 void bookmark_play(char *resume_file, int index, int offset, int seed,
1193 char *filename)
1195 int i;
1196 int len=strlen(resume_file);
1198 if (!strcasecmp(&resume_file[len-4], ".m3u"))
1200 /* Playlist playback */
1201 char* slash;
1202 /* check that the file exists */
1203 int fd = open(resume_file, O_RDONLY);
1204 if(fd<0)
1205 return;
1206 close(fd);
1208 slash = strrchr(resume_file,'/');
1209 if (slash)
1211 char* cp;
1212 *slash=0;
1214 cp=resume_file;
1215 if (!cp[0])
1216 cp="/";
1218 if (playlist_create(cp, slash+1) != -1)
1220 if (global_settings.playlist_shuffle)
1221 playlist_shuffle(seed, -1);
1222 playlist_start(index,offset);
1224 *slash='/';
1227 else
1229 /* Directory playback */
1230 lastdir[0]='\0';
1231 if (playlist_create(resume_file, NULL) != -1)
1233 resume_directory(resume_file);
1234 if (global_settings.playlist_shuffle)
1235 playlist_shuffle(seed, -1);
1237 /* Check if the file is at the same spot in the directory,
1238 else search for it */
1239 if ((strcmp(strrchr(playlist_peek(index) + 1,'/') + 1,
1240 filename)))
1242 for ( i=0; i < playlist_amount(); i++ )
1244 if ((strcmp(strrchr(playlist_peek(i) + 1,'/') + 1,
1245 filename)) == 0)
1246 break;
1248 if (i < playlist_amount())
1249 index = i;
1250 else
1251 return;
1253 playlist_start(index,offset);
1257 start_wps=true;
1260 int ft_play_filenumber(int pos, int attr)
1262 /* try to find a voice ID for the extension, if known */
1263 unsigned int j;
1264 int ext_id = -1; /* default to none */
1265 for (j=0; j<sizeof(filetypes)/sizeof(*filetypes); j++)
1267 if (attr == filetypes[j].tree_attr)
1269 ext_id = filetypes[j].voiceclip;
1270 break;
1274 talk_id(VOICE_FILE, false);
1275 talk_number(pos, true);
1276 talk_id(ext_id, true);
1277 return 1;
1280 int ft_play_dirname(int start_index)
1282 int fd;
1283 char dirname_mp3_filename[MAX_PATH+1];
1284 struct entry *dircache = tc.dircache;
1286 if (audio_status() & AUDIO_STATUS_PLAY)
1287 return 0;
1289 snprintf(dirname_mp3_filename, sizeof(dirname_mp3_filename), "%s/%s/%s",
1290 tc.currdir[1] ? tc.currdir : "" , dircache[start_index].name,
1291 dir_thumbnail_name);
1293 DEBUGF("Checking for %s\n", dirname_mp3_filename);
1295 fd = open(dirname_mp3_filename, O_RDONLY);
1296 if (fd < 0)
1298 DEBUGF("Failed to find: %s\n", dirname_mp3_filename);
1299 return -1;
1302 close(fd);
1304 DEBUGF("Found: %s\n", dirname_mp3_filename);
1306 talk_file(dirname_mp3_filename, false);
1307 return 1;
1310 void ft_play_filename(char *dir, char *file)
1312 char name_mp3_filename[MAX_PATH+1];
1314 if (audio_status() & AUDIO_STATUS_PLAY)
1315 return;
1317 if (strcasecmp(&file[strlen(file) - strlen(file_thumbnail_ext)],
1318 file_thumbnail_ext))
1319 { /* file has no .talk extension */
1320 snprintf(name_mp3_filename, sizeof(name_mp3_filename),
1321 "%s/%s%s", dir, file, file_thumbnail_ext);
1323 talk_file(name_mp3_filename, false);
1325 else
1326 { /* it already is a .talk file, play this directly */
1327 snprintf(name_mp3_filename, sizeof(name_mp3_filename),
1328 "%s/%s", dir, file);
1329 talk_id(LANG_VOICE_DIR_HOVER, false); /* prefix it */
1330 talk_file(name_mp3_filename, true);
1334 /* These two functions are called by the USB and shutdown handlers */
1335 void tree_flush(void)
1337 rundb_shutdown();
1338 tagdb_shutdown();
1339 #ifdef HAVE_DIRCACHE
1340 if (global_settings.dircache)
1342 if (dircache_is_enabled())
1343 global_settings.dircache_size = dircache_get_cache_size();
1344 dircache_disable();
1346 else
1348 global_settings.dircache_size = 0;
1350 settings_save();
1351 #endif
1354 void tree_restore(void)
1356 tagdb_init();
1357 rundb_init();
1358 #ifdef HAVE_DIRCACHE
1359 if (global_settings.dircache)
1361 /* Print "Scanning disk..." to the display. */
1362 int i;
1363 for(i = 0;i < NB_SCREENS;++i)
1365 screens[i].putsxy((LCD_WIDTH/2) -
1366 ((strlen(str(LANG_DIRCACHE_BUILDING)) *
1367 screens[i].char_width)/2),
1368 LCD_HEIGHT-screens[i].char_height*3,
1369 str(LANG_DIRCACHE_BUILDING));
1370 screens[i].update();
1372 dircache_build(global_settings.dircache_size);
1373 /* Clean the text when we are done. */
1374 for(i=0;i<NB_SCREENS;++i)
1376 screens[i].set_drawmode(DRMODE_SOLID|DRMODE_INVERSEVID);
1377 screens[i].fillrect(0, LCD_HEIGHT-screens[i].char_height*3,
1378 LCD_WIDTH, screens[i].char_height);
1379 screens[i].set_drawmode(DRMODE_SOLID);
1380 screens[i].update();
1383 #endif