Oops, unintended change slipped in.
[kugel-rb.git] / apps / tree.c
blob98880f001a7f8cfb4441b19aae911de341d59875
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2002 Daniel Stenberg
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <stdbool.h>
24 #include "string-extra.h"
26 #include "applimits.h"
27 #include "dir.h"
28 #include "file.h"
29 #include "lcd.h"
30 #include "font.h"
31 #include "button.h"
32 #include "kernel.h"
33 #include "usb.h"
34 #include "tree.h"
35 #include "audio.h"
36 #include "playlist.h"
37 #include "menu.h"
38 #include "skin_engine/skin_engine.h"
39 #include "settings.h"
40 #include "debug.h"
41 #include "storage.h"
42 #include "rolo.h"
43 #include "icons.h"
44 #include "lang.h"
45 #include "screens.h"
46 #include "keyboard.h"
47 #include "bookmark.h"
48 #include "onplay.h"
49 #include "buffer.h"
50 #include "power.h"
51 #include "action.h"
52 #include "talk.h"
53 #include "filetypes.h"
54 #include "misc.h"
55 #include "filefuncs.h"
56 #include "filetree.h"
57 #include "tagtree.h"
58 #ifdef HAVE_RECORDING
59 #include "recorder/recording.h"
60 #endif
61 #include "rtc.h"
62 #include "dircache.h"
63 #ifdef HAVE_TAGCACHE
64 #include "tagcache.h"
65 #endif
66 #include "yesno.h"
67 #include "eeprom_settings.h"
68 #include "playlist_catalog.h"
70 /* gui api */
71 #include "list.h"
72 #include "splash.h"
73 #include "buttonbar.h"
74 #include "quickscreen.h"
75 #include "appevents.h"
77 #include "root_menu.h"
79 static const struct filetype *filetypes;
80 static int filetypes_count;
82 struct gui_synclist tree_lists;
84 /* I put it here because other files doesn't use it yet,
85 * but should be elsewhere since it will be used mostly everywhere */
86 #ifdef HAVE_BUTTONBAR
87 static struct gui_buttonbar tree_buttonbar;
88 #endif
89 static struct tree_context tc;
91 bool boot_changed = false;
93 char lastfile[MAX_PATH];
94 static char lastdir[MAX_PATH];
95 #ifdef HAVE_TAGCACHE
96 static int lasttable, lastextra, lastfirstpos;
97 #endif
98 static int max_files = 0;
100 static bool reload_dir = false;
102 static bool start_wps = false;
103 static int curr_context = false;/* id3db or tree*/
105 static int dirbrowse(void);
106 static int ft_play_dirname(char* name);
107 static void ft_play_filename(char *dir, char *file);
108 static void say_filetype(int attr);
110 static const char* tree_get_filename(int selected_item, void *data,
111 char *buffer, size_t buffer_len)
113 struct tree_context * local_tc=(struct tree_context *)data;
114 char *name;
115 int attr=0;
116 bool stripit = false;
117 #ifdef HAVE_TAGCACHE
118 bool id3db = *(local_tc->dirfilter) == SHOW_ID3DB;
120 if (id3db)
122 return tagtree_get_entry(&tc, selected_item)->name;
124 else
125 #endif
127 struct entry* dc = local_tc->dircache;
128 struct entry* e = &dc[selected_item];
129 name = e->name;
130 attr = e->attr;
133 if(!(attr & ATTR_DIRECTORY))
135 switch(global_settings.show_filename_ext)
137 case 0:
138 /* show file extension: off */
139 stripit = true;
140 break;
141 case 1:
142 /* show file extension: on */
143 break;
144 case 2:
145 /* show file extension: only unknown types */
146 stripit = filetype_supported(attr);
147 break;
148 case 3:
149 default:
150 /* show file extension: only when viewing all */
151 stripit = (*(local_tc->dirfilter) != SHOW_ID3DB) &&
152 (*(local_tc->dirfilter) != SHOW_ALL);
153 break;
157 if(stripit)
159 return(strip_extension(buffer, buffer_len, name));
161 return(name);
164 #ifdef HAVE_LCD_COLOR
165 static int tree_get_filecolor(int selected_item, void * data)
167 if (*tc.dirfilter == SHOW_ID3DB)
168 return -1;
169 struct tree_context * local_tc=(struct tree_context *)data;
170 struct entry* dc = local_tc->dircache;
171 struct entry* e = &dc[selected_item];
172 return filetype_get_color(e->name, e->attr);
174 #endif
176 static enum themable_icons tree_get_fileicon(int selected_item, void * data)
178 struct tree_context * local_tc=(struct tree_context *)data;
179 #ifdef HAVE_TAGCACHE
180 bool id3db = *(local_tc->dirfilter) == SHOW_ID3DB;
181 if (id3db) {
182 return tagtree_get_icon(&tc);
184 else
185 #endif
187 struct entry* dc = local_tc->dircache;
188 struct entry* e = &dc[selected_item];
189 return filetype_get_icon(e->attr);
193 static int tree_voice_cb(int selected_item, void * data)
195 struct tree_context * local_tc=(struct tree_context *)data;
196 char *name;
197 int attr=0;
198 #ifdef HAVE_TAGCACHE
199 bool id3db = *(local_tc->dirfilter) == SHOW_ID3DB;
201 if (id3db)
203 attr = tagtree_get_attr(local_tc);
204 name = tagtree_get_entry(local_tc, selected_item)->name;
206 else
207 #endif
209 struct entry* dc = local_tc->dircache;
210 struct entry* e = &dc[selected_item];
211 name = e->name;
212 attr = e->attr;
214 bool is_dir = (attr & ATTR_DIRECTORY);
215 bool did_clip = false;
216 /* First the .talk clip case */
217 if(is_dir)
219 if(global_settings.talk_dir_clip)
221 did_clip = true;
222 if(ft_play_dirname(name) <0)
223 /* failed, not existing */
224 did_clip = false;
226 } else { /* it's a file */
227 if (global_settings.talk_file_clip && (attr & FILE_ATTR_THUMBNAIL))
229 did_clip = true;
230 ft_play_filename(local_tc->currdir, name);
233 if(!did_clip)
235 /* say the number or spell if required or as a fallback */
236 switch (is_dir ? global_settings.talk_dir : global_settings.talk_file)
238 case 1: /* as numbers */
239 talk_id(is_dir ? VOICE_DIR : VOICE_FILE, false);
240 talk_number(selected_item+1 - (is_dir ? 0 : local_tc->dirsindir),
241 true);
242 if(global_settings.talk_filetype
243 && !is_dir && *local_tc->dirfilter < NUM_FILTER_MODES)
244 say_filetype(attr);
245 break;
246 case 2: /* spelled */
247 talk_shutup();
248 if(global_settings.talk_filetype)
250 if(is_dir)
251 talk_id(VOICE_DIR, true);
252 else if(*local_tc->dirfilter < NUM_FILTER_MODES)
253 say_filetype(attr);
255 talk_spell(name, true);
256 break;
259 return 0;
262 bool check_rockboxdir(void)
264 if(!dir_exists(ROCKBOX_DIR))
265 { /* No need to localise this message.
266 If .rockbox is missing, it wouldn't work anyway */
267 int i;
268 FOR_NB_SCREENS(i)
269 screens[i].clear_display();
270 splash(HZ*2, "No .rockbox directory");
271 FOR_NB_SCREENS(i)
272 screens[i].clear_display();
273 splash(HZ*2, "Installation incomplete");
274 return false;
276 return true;
279 /* do this really late in the init sequence */
280 void tree_gui_init(void)
282 check_rockboxdir();
284 strcpy(tc.currdir, "/");
286 #ifdef HAVE_LCD_CHARCELLS
287 int i;
288 FOR_NB_SCREENS(i)
289 screens[i].double_height(false);
290 #endif
291 #ifdef HAVE_BUTTONBAR
292 gui_buttonbar_init(&tree_buttonbar);
293 /* since archos only have one screen, no need to create more than that */
294 gui_buttonbar_set_display(&tree_buttonbar, &(screens[SCREEN_MAIN]) );
295 #endif
296 gui_synclist_init(&tree_lists, &tree_get_filename, &tc, false, 1, NULL);
297 gui_synclist_set_voice_callback(&tree_lists, tree_voice_cb);
298 gui_synclist_set_icon_callback(&tree_lists,
299 global_settings.show_icons?&tree_get_fileicon:NULL);
300 #ifdef HAVE_LCD_COLOR
301 gui_synclist_set_color_callback(&tree_lists, &tree_get_filecolor);
302 #endif
306 /* drawer function for the GUI_EVENT_REDRAW callback */
307 void tree_drawlists(void)
309 /* band-aid to fix the bar/list redrawing properly after leaving a plugin */
310 send_event(GUI_EVENT_THEME_CHANGED, NULL);
311 /* end bandaid */
312 gui_synclist_draw(&tree_lists);
316 struct tree_context* tree_get_context(void)
318 return &tc;
322 * Returns the position of a given file in the current directory
323 * returns -1 if not found
325 static int tree_get_file_position(char * filename)
327 int i;
329 /* use lastfile to determine the selected item (default=0) */
330 for (i=0; i < tc.filesindir; i++)
332 struct entry* dc = tc.dircache;
333 struct entry* e = &dc[i];
334 if (!strcasecmp(e->name, filename))
335 return(i);
337 return(-1);/* no file can match, returns undefined */
341 * Called when a new dir is loaded (for example when returning from other apps ...)
342 * also completely redraws the tree
344 static int update_dir(void)
346 bool changed = false;
347 #ifdef HAVE_TAGCACHE
348 bool id3db = *tc.dirfilter == SHOW_ID3DB;
349 /* Checks for changes */
350 if (id3db) {
351 if (tc.currtable != lasttable ||
352 tc.currextra != lastextra ||
353 tc.firstpos != lastfirstpos ||
354 reload_dir)
356 if (tagtree_load(&tc) < 0)
357 return -1;
359 lasttable = tc.currtable;
360 lastextra = tc.currextra;
361 lastfirstpos = tc.firstpos;
362 changed = true;
365 else
366 #endif
368 /* if the tc.currdir has been changed, reload it ...*/
369 if (strncmp(tc.currdir, lastdir, sizeof(lastdir)) || reload_dir)
371 if (ft_load(&tc, NULL) < 0)
372 return -1;
373 strcpy(lastdir, tc.currdir);
374 changed = true;
377 /* if selected item is undefined */
378 if (tc.selected_item == -1)
380 /* use lastfile to determine the selected item */
381 tc.selected_item = tree_get_file_position(lastfile);
383 /* If the file doesn't exists, select the first one (default) */
384 if(tc.selected_item < 0)
385 tc.selected_item = 0;
386 changed = true;
388 if (changed)
391 #ifdef HAVE_TAGCACHE
392 !id3db &&
393 #endif
394 (tc.dirfull || tc.filesindir == global_settings.max_files_in_dir) )
396 splash(HZ, ID2P(LANG_SHOWDIR_BUFFER_FULL));
399 #ifdef HAVE_TAGCACHE
400 if (id3db)
402 #ifdef HAVE_LCD_BITMAP
403 if (global_settings.show_path_in_browser == SHOW_PATH_FULL
404 || global_settings.show_path_in_browser == SHOW_PATH_CURRENT)
406 gui_synclist_set_title(&tree_lists, tagtree_get_title(&tc),
407 filetype_get_icon(ATTR_DIRECTORY));
409 else
411 /* Must clear the title as the list is reused */
412 gui_synclist_set_title(&tree_lists, NULL, NOICON);
414 #endif
416 else
417 #endif
419 #ifdef HAVE_LCD_BITMAP
420 if (global_settings.show_path_in_browser &&
421 *(tc.dirfilter) == SHOW_PLUGINS)
423 char *title;
424 if (!strcmp(tc.currdir, PLUGIN_GAMES_DIR))
425 title = str(LANG_PLUGIN_GAMES);
426 else if (!strcmp(tc.currdir, PLUGIN_APPS_DIR))
427 title = str(LANG_PLUGIN_APPS);
428 else if (!strcmp(tc.currdir, PLUGIN_DEMOS_DIR))
429 title = str(LANG_PLUGIN_DEMOS);
430 else title = str(LANG_PLUGINS);
431 gui_synclist_set_title(&tree_lists, title, Icon_Plugin);
433 else if (global_settings.show_path_in_browser == SHOW_PATH_FULL)
435 gui_synclist_set_title(&tree_lists, tc.currdir,
436 filetype_get_icon(ATTR_DIRECTORY));
438 else if (global_settings.show_path_in_browser == SHOW_PATH_CURRENT)
440 char *title = strrchr(tc.currdir, '/') + 1;
441 if (*title == '\0')
443 /* Display "Files" for the root dir */
444 gui_synclist_set_title(&tree_lists, str(LANG_DIR_BROWSER),
445 filetype_get_icon(ATTR_DIRECTORY));
447 else
448 gui_synclist_set_title(&tree_lists, title,
449 filetype_get_icon(ATTR_DIRECTORY));
451 else
453 /* Must clear the title as the list is reused */
454 gui_synclist_set_title(&tree_lists, NULL, NOICON);
456 #endif
459 gui_synclist_set_nb_items(&tree_lists, tc.filesindir);
460 gui_synclist_set_icon_callback(&tree_lists,
461 global_settings.show_icons?tree_get_fileicon:NULL);
462 if( tc.selected_item >= tc.filesindir)
463 tc.selected_item=tc.filesindir-1;
465 gui_synclist_select_item(&tree_lists, tc.selected_item);
466 #ifdef HAVE_BUTTONBAR
467 if (global_settings.buttonbar) {
468 if (*tc.dirfilter < NUM_FILTER_MODES)
469 gui_buttonbar_set(&tree_buttonbar, str(LANG_SYSFONT_DIRBROWSE_F1),
470 str(LANG_SYSFONT_DIRBROWSE_F2),
471 str(LANG_SYSFONT_DIRBROWSE_F3));
472 else
473 gui_buttonbar_set(&tree_buttonbar, "<<<", "", "");
474 gui_buttonbar_draw(&tree_buttonbar);
476 #endif
477 gui_synclist_draw(&tree_lists);
478 gui_synclist_speak_item(&tree_lists);
479 return tc.filesindir;
482 /* load tracks from specified directory to resume play */
483 void resume_directory(const char *dir)
485 int dirfilter = *tc.dirfilter;
486 int ret;
487 #ifdef HAVE_TAGCACHE
488 bool id3db = *tc.dirfilter == SHOW_ID3DB;
489 #endif
490 /* make sure the dirfilter is sane. The only time it should be possible
491 * thats its not is when resume playlist is called from a plugin
493 #ifdef HAVE_TAGCACHE
494 if (!id3db)
495 #endif
496 *tc.dirfilter = global_settings.dirfilter;
497 ret = ft_load(&tc, dir);
498 *tc.dirfilter = dirfilter;
499 if (ret < 0)
500 return;
501 lastdir[0] = 0;
503 ft_build_playlist(&tc, 0);
505 #ifdef HAVE_TAGCACHE
506 if (id3db)
507 tagtree_load(&tc);
508 #endif
511 /* Returns the current working directory and also writes cwd to buf if
512 non-NULL. In case of error, returns NULL. */
513 char *getcwd(char *buf, getcwd_size_t size)
515 if (!buf)
516 return tc.currdir;
517 else if (size)
519 if ((getcwd_size_t)strlcpy(buf, tc.currdir, size) < size)
520 return buf;
522 /* size == 0, or truncation in strlcpy */
523 return NULL;
526 /* Force a reload of the directory next time directory browser is called */
527 void reload_directory(void)
529 reload_dir = true;
532 char* get_current_file(char* buffer, size_t buffer_len)
534 #ifdef HAVE_TAGCACHE
535 /* in ID3DB mode it is a bad idea to call this function */
536 /* (only happens with `follow playlist') */
537 if( *tc.dirfilter == SHOW_ID3DB )
538 return NULL;
539 #endif
541 struct entry* dc = tc.dircache;
542 struct entry* e = &dc[tc.selected_item];
543 if (getcwd(buffer, buffer_len))
545 if (tc.dirlength)
547 if (buffer[strlen(buffer)-1] != '/')
548 strlcat(buffer, "/", buffer_len);
549 if (strlcat(buffer, e->name, buffer_len) >= buffer_len)
550 return NULL;
552 return buffer;
554 return NULL;
557 /* Allow apps to change our dirfilter directly (required for sub browsers)
558 if they're suddenly going to become a file browser for example */
559 void set_dirfilter(int l_dirfilter)
561 *tc.dirfilter = l_dirfilter;
564 /* Selects a file and update tree context properly */
565 void set_current_file(const char *path)
567 const char *name;
568 int i;
570 #ifdef HAVE_TAGCACHE
571 /* in ID3DB mode it is a bad idea to call this function */
572 /* (only happens with `follow playlist') */
573 if( *tc.dirfilter == SHOW_ID3DB )
574 return;
575 #endif
577 /* separate directory from filename */
578 /* gets the directory's name and put it into tc.currdir */
579 name = strrchr(path+1,'/');
580 if (name)
582 strlcpy(tc.currdir, path, name - path + 1);
583 name++;
585 else
587 strcpy(tc.currdir, "/");
588 name = path+1;
591 strcpy(lastfile, name);
594 /* If we changed dir we must recalculate the dirlevel
595 and adjust the selected history properly */
596 if (strncmp(tc.currdir,lastdir,sizeof(lastdir)))
598 tc.dirlevel = 0;
599 tc.selected_item_history[tc.dirlevel] = -1;
601 /* use '/' to calculate dirlevel */
602 for (i = 1; path[i] != '\0'; i++)
604 if (path[i] == '/')
606 tc.dirlevel++;
607 tc.selected_item_history[tc.dirlevel] = -1;
611 if (ft_load(&tc, NULL) >= 0)
613 tc.selected_item = tree_get_file_position(lastfile);
618 /* main loop, handles key events */
619 static int dirbrowse()
621 int numentries=0;
622 char buf[MAX_PATH];
623 int button, oldbutton;
624 bool reload_root = false;
625 int lastfilter = *tc.dirfilter;
626 bool lastsortcase = global_settings.sort_case;
627 bool exit_func = false;
629 char* currdir = tc.currdir; /* just a shortcut */
630 #ifdef HAVE_TAGCACHE
631 bool id3db = *tc.dirfilter == SHOW_ID3DB;
633 if (id3db)
634 curr_context=CONTEXT_ID3DB;
635 else
636 #endif
637 curr_context=CONTEXT_TREE;
638 if (tc.selected_item < 0)
639 tc.selected_item = 0;
640 #ifdef HAVE_TAGCACHE
641 tc.firstpos = 0;
642 lasttable = -1;
643 lastextra = -1;
644 lastfirstpos = 0;
645 #endif
647 start_wps = false;
648 numentries = update_dir();
649 reload_dir = false;
650 if (numentries == -1)
651 return GO_TO_PREVIOUS; /* currdir is not a directory */
653 if (*tc.dirfilter > NUM_FILTER_MODES && numentries==0)
655 splash(HZ*2, ID2P(LANG_NO_FILES));
656 return GO_TO_PREVIOUS; /* No files found for rockbox_browser() */
659 gui_synclist_draw(&tree_lists);
660 while(1) {
661 struct entry *dircache = tc.dircache;
662 bool restore = false;
663 if (tc.dirlevel < 0)
664 tc.dirlevel = 0; /* shouldnt be needed.. this code needs work! */
665 #ifdef BOOTFILE
666 if (boot_changed) {
667 static const char *lines[]={ID2P(LANG_BOOT_CHANGED), ID2P(LANG_REBOOT_NOW)};
668 static const struct text_message message={lines, 2};
669 if(gui_syncyesno_run(&message, NULL, NULL)==YESNO_YES)
670 rolo_load("/" BOOTFILE);
671 restore = true;
672 boot_changed = false;
674 #endif
675 button = get_action(CONTEXT_TREE,
676 list_do_action_timeout(&tree_lists, HZ/2));
677 oldbutton = button;
678 gui_synclist_do_button(&tree_lists, &button,LIST_WRAP_UNLESS_HELD);
679 tc.selected_item = gui_synclist_get_sel_pos(&tree_lists);
680 switch ( button ) {
681 case ACTION_STD_OK:
682 /* nothing to do if no files to display */
683 if ( numentries == 0 )
684 break;
686 #ifdef HAVE_TAGCACHE
687 switch (id3db?tagtree_enter(&tc):ft_enter(&tc))
688 #else
689 switch (ft_enter(&tc))
690 #endif
692 case GO_TO_FILEBROWSER: reload_dir = true; break;
693 case GO_TO_WPS:
694 return GO_TO_WPS;
695 #if CONFIG_TUNER
696 case GO_TO_FM:
697 return GO_TO_FM;
698 #endif
699 case GO_TO_ROOT: exit_func = true; break;
700 default: break;
702 restore = true;
703 break;
705 case ACTION_STD_CANCEL:
706 if (*tc.dirfilter > NUM_FILTER_MODES && tc.dirlevel < 1) {
707 exit_func = true;
708 break;
710 if ((*tc.dirfilter == SHOW_ID3DB && tc.dirlevel == 0) ||
711 ((*tc.dirfilter != SHOW_ID3DB && !strcmp(currdir,"/"))))
713 #ifdef HAVE_LCD_BITMAP /* charcell doesnt have ACTION_TREE_PGLEFT so this isnt needed */
714 if (oldbutton == ACTION_TREE_PGLEFT)
715 break;
716 else
717 #endif
718 return GO_TO_ROOT;
721 #ifdef HAVE_TAGCACHE
722 if (id3db)
723 tagtree_exit(&tc);
724 else
725 #endif
726 if (ft_exit(&tc) == 3)
727 exit_func = true;
729 restore = true;
730 break;
732 case ACTION_TREE_STOP:
733 if (list_stop_handler())
734 restore = true;
735 break;
737 case ACTION_STD_MENU:
738 return GO_TO_ROOT;
739 break;
741 #ifdef HAVE_RECORDING
742 case ACTION_STD_REC:
743 return GO_TO_RECSCREEN;
744 #endif
746 case ACTION_TREE_WPS:
747 return GO_TO_PREVIOUS_MUSIC;
748 break;
749 #ifdef HAVE_QUICKSCREEN
750 case ACTION_STD_QUICKSCREEN:
751 /* don't enter f2 from plugin browser */
752 if (*tc.dirfilter < NUM_FILTER_MODES)
754 if (quick_screen_quick(button))
755 reload_dir = true;
756 restore = true;
758 break;
759 #endif
760 #ifdef BUTTON_F3
761 case ACTION_F3:
762 /* don't enter f3 from plugin browser */
763 if (*tc.dirfilter < NUM_FILTER_MODES)
765 if (quick_screen_f3(ACTION_F3))
766 reload_dir = true;
767 restore = true;
769 break;
770 #endif
772 #ifdef HAVE_HOTKEY
773 case ACTION_TREE_HOTKEY:
774 if (!global_settings.hotkey_tree)
775 break;
776 /* fall through */
777 #endif
778 case ACTION_STD_CONTEXT:
780 bool hotkey = button == ACTION_TREE_HOTKEY;
781 int onplay_result;
782 int attr = 0;
784 if(!numentries)
785 onplay_result = onplay(NULL, 0, curr_context, hotkey);
786 else {
787 #ifdef HAVE_TAGCACHE
788 if (id3db)
790 if (tagtree_get_attr(&tc) == FILE_ATTR_AUDIO)
792 attr = FILE_ATTR_AUDIO;
793 tagtree_get_filename(&tc, buf, sizeof(buf));
795 else
796 attr = ATTR_DIRECTORY;
798 else
799 #endif
801 attr = dircache[tc.selected_item].attr;
803 if (currdir[1]) /* Not in / */
804 snprintf(buf, sizeof buf, "%s/%s",
805 currdir,
806 dircache[tc.selected_item].name);
807 else /* In / */
808 snprintf(buf, sizeof buf, "/%s",
809 dircache[tc.selected_item].name);
811 onplay_result = onplay(buf, attr, curr_context, hotkey);
813 switch (onplay_result)
815 case ONPLAY_MAINMENU:
816 return GO_TO_ROOT;
818 case ONPLAY_OK:
819 restore = true;
820 break;
822 case ONPLAY_RELOAD_DIR:
823 reload_dir = true;
824 break;
826 case ONPLAY_START_PLAY:
827 return GO_TO_WPS;
828 break;
830 break;
833 #ifdef HAVE_HOTSWAP
834 case SYS_FS_CHANGED:
835 #ifdef HAVE_TAGCACHE
836 if (!id3db)
837 #endif
838 reload_dir = true;
839 /* The 'dir no longer valid' situation will be caught later
840 * by checking the showdir() result. */
841 break;
842 #endif
844 default:
845 if (default_event_handler(button) == SYS_USB_CONNECTED)
847 if(*tc.dirfilter > NUM_FILTER_MODES)
848 /* leave sub-browsers after usb, doing otherwise
849 might be confusing to the user */
850 exit_func = true;
851 else
852 reload_dir = true;
854 break;
856 if (start_wps)
857 return GO_TO_WPS;
858 if (button && !IS_SYSEVENT(button))
860 storage_spin();
864 check_rescan:
865 /* do we need to rescan dir? */
866 if (reload_dir || reload_root ||
867 lastfilter != *tc.dirfilter ||
868 lastsortcase != global_settings.sort_case)
870 if (reload_root) {
871 strcpy(currdir, "/");
872 tc.dirlevel = 0;
873 #ifdef HAVE_TAGCACHE
874 tc.currtable = 0;
875 tc.currextra = 0;
876 lasttable = -1;
877 lastextra = -1;
878 #endif
879 reload_root = false;
882 if (!reload_dir)
884 gui_synclist_select_item(&tree_lists, 0);
885 gui_synclist_draw(&tree_lists);
886 tc.selected_item = 0;
887 lastdir[0] = 0;
890 lastfilter = *tc.dirfilter;
891 lastsortcase = global_settings.sort_case;
892 restore = true;
895 if (exit_func)
896 return GO_TO_PREVIOUS;
898 if (restore || reload_dir) {
899 /* restore display */
900 numentries = update_dir();
901 reload_dir = false;
902 if (currdir[1] && (numentries < 0))
903 { /* not in root and reload failed */
904 reload_root = true; /* try root */
905 goto check_rescan;
909 return true;
912 bool create_playlist(void)
914 char filename[MAX_PATH];
916 snprintf(filename, sizeof filename, "%s.m3u8",
917 tc.currdir[1] ? tc.currdir : "/root");
918 splashf(0, "%s %s", str(LANG_CREATING), filename);
920 trigger_cpu_boost();
921 catalog_add_to_a_playlist(tc.currdir, ATTR_DIRECTORY, true, filename);
922 cancel_cpu_boost();
924 return true;
927 int rockbox_browse(const char *root, int dirfilter)
929 static char current[MAX_PATH];
930 int ret_val = 0;
931 int *last_filter = tc.dirfilter;
932 tc.dirfilter = &dirfilter;
933 tc.sort_dir = global_settings.sort_dir;
935 reload_dir = true;
936 if (dirfilter >= NUM_FILTER_MODES)
938 static struct tree_context backup;
939 int last_context;
940 const char *ext, *setting;
942 backup = tc;
943 tc.selected_item = 0;
944 tc.dirlevel = 0;
945 memcpy(tc.currdir, root, sizeof(tc.currdir));
946 start_wps = false;
947 last_context = curr_context;
949 /* if we are in a special settings folder, center the current setting */
950 switch(dirfilter)
952 case SHOW_LNG:
953 ext = "lng";
954 if (global_settings.lang_file[0])
955 setting = global_settings.lang_file;
956 else
957 setting = "english";
958 break;
959 case SHOW_WPS:
960 ext = "wps";
961 setting = global_settings.wps_file;
962 break;
963 #ifdef HAVE_REMOTE_LCD
964 case SHOW_RWPS:
965 ext = "rwps";
966 setting = global_settings.rwps_file;
967 break;
968 case SHOW_RSBS:
969 ext = "rsbs";
970 setting = global_settings.rsbs_file;
971 break;
972 #if CONFIG_TUNER
973 case SHOW_RFMS:
974 ext = "rfms";
975 setting = global_settings.rfms_file;
976 break;
977 #endif /* CONFIG_TUNER */
978 #endif
979 #ifdef HAVE_LCD_BITMAP
980 case SHOW_FONT:
981 ext = "fnt";
982 setting = global_settings.font_file;
983 break;
984 case SHOW_SBS:
985 ext = "sbs";
986 setting = global_settings.sbs_file;
987 break;
988 #if CONFIG_TUNER
989 case SHOW_FMS:
990 ext = "fms";
991 setting = global_settings.fms_file;
992 break;
993 #endif /* CONFIG_TUNER */
994 #endif
995 #if CONFIG_TUNER
996 case SHOW_FMR:
997 ext = "fmr";
998 setting = global_settings.fmr_file;
999 break;
1000 #endif
1001 default:
1002 ext = setting = NULL;
1003 break;
1006 /* If we've found a file to center on, do it */
1007 if (setting)
1009 /* if setting != NULL, ext is initialized */
1010 snprintf(current, sizeof(current), "%s/%s.%s", root, setting, ext);
1011 set_current_file(current);
1012 /* set_current_file changes dirlevel, change it back */
1013 tc.dirlevel = 0;
1016 ret_val = dirbrowse();
1017 tc = backup;
1018 curr_context = last_context;
1020 else
1022 if (dirfilter != SHOW_ID3DB)
1023 tc.dirfilter = &global_settings.dirfilter;
1024 strcpy(current,root);
1025 set_current_file(current);
1026 ret_val = dirbrowse();
1028 tc.dirfilter = last_filter;
1029 return ret_val;
1032 void tree_mem_init(void)
1034 /* We copy the settings value in case it is changed by the user. We can't
1035 use it until the next reboot. */
1036 max_files = global_settings.max_files_in_dir;
1038 /* initialize tree context struct */
1039 memset(&tc, 0, sizeof(tc));
1040 tc.dirfilter = &global_settings.dirfilter;
1041 tc.sort_dir = global_settings.sort_dir;
1043 tc.name_buffer_size = AVERAGE_FILENAME_LENGTH * max_files;
1044 tc.name_buffer = buffer_alloc(tc.name_buffer_size);
1046 tc.dircache_size = max_files * sizeof(struct entry);
1047 tc.dircache = buffer_alloc(tc.dircache_size);
1048 tree_get_filetypes(&filetypes, &filetypes_count);
1051 bool bookmark_play(char *resume_file, int index, int offset, int seed,
1052 char *filename)
1054 int i;
1055 char* suffix = strrchr(resume_file, '.');
1056 bool started = false;
1058 if (suffix != NULL &&
1059 (!strcasecmp(suffix, ".m3u") || !strcasecmp(suffix, ".m3u8")))
1061 /* Playlist playback */
1062 char* slash;
1063 /* check that the file exists */
1064 if(!file_exists(resume_file))
1065 return false;
1067 slash = strrchr(resume_file,'/');
1068 if (slash)
1070 char* cp;
1071 *slash=0;
1073 cp=resume_file;
1074 if (!cp[0])
1075 cp="/";
1077 if (playlist_create(cp, slash+1) != -1)
1079 if (global_settings.playlist_shuffle)
1080 playlist_shuffle(seed, -1);
1081 playlist_start(index,offset);
1082 started = true;
1084 *slash='/';
1087 else
1089 /* Directory playback */
1090 lastdir[0]='\0';
1091 if (playlist_create(resume_file, NULL) != -1)
1093 char filename_buf[MAX_PATH + 1];
1094 const char* peek_filename;
1095 resume_directory(resume_file);
1096 if (global_settings.playlist_shuffle)
1097 playlist_shuffle(seed, -1);
1099 /* Check if the file is at the same spot in the directory,
1100 else search for it */
1101 peek_filename = playlist_peek(index, filename_buf,
1102 sizeof(filename_buf));
1104 if (peek_filename == NULL)
1106 /* playlist has shrunk, search from the top */
1107 index = 0;
1108 peek_filename = playlist_peek(index, filename_buf,
1109 sizeof(filename_buf));
1110 if (peek_filename == NULL)
1111 return false;
1114 if (strcmp(strrchr(peek_filename, '/') + 1, filename))
1116 for ( i=0; i < playlist_amount(); i++ )
1118 peek_filename = playlist_peek(i, filename_buf,
1119 sizeof(filename_buf));
1121 if (peek_filename == NULL)
1122 return false;
1124 if (!strcmp(strrchr(peek_filename, '/') + 1, filename))
1125 break;
1127 if (i < playlist_amount())
1128 index = i;
1129 else
1130 return false;
1132 playlist_start(index,offset);
1133 started = true;
1137 if (started)
1138 start_wps = true;
1139 return started;
1142 static void say_filetype(int attr)
1144 /* try to find a voice ID for the extension, if known */
1145 int j;
1146 attr &= FILE_ATTR_MASK; /* file type */
1147 for (j=0; j<filetypes_count; j++)
1148 if (attr == filetypes[j].tree_attr)
1150 talk_id(filetypes[j].voiceclip, true);
1151 return;
1155 static int ft_play_dirname(char* name)
1157 #if CONFIG_CODEC != SWCODEC
1158 if (audio_status() & AUDIO_STATUS_PLAY)
1159 return 0;
1160 #endif
1162 if(talk_file(tc.currdir, name, dir_thumbnail_name, NULL,
1163 NULL, false))
1165 if(global_settings.talk_filetype)
1166 talk_id(VOICE_DIR, true);
1167 return 1;
1169 else
1170 return -1;
1173 static void ft_play_filename(char *dir, char *file)
1175 #if CONFIG_CODEC != SWCODEC
1176 if (audio_status() & AUDIO_STATUS_PLAY)
1177 return;
1178 #endif
1180 if (strlen(file) >= strlen(file_thumbnail_ext)
1181 && strcasecmp(&file[strlen(file) - strlen(file_thumbnail_ext)],
1182 file_thumbnail_ext))
1183 /* file has no .talk extension */
1184 talk_file(dir, NULL, file, file_thumbnail_ext,
1185 NULL, false);
1186 else
1187 /* it already is a .talk file, play this directly, but prefix it. */
1188 talk_file(dir, NULL, file, NULL,
1189 TALK_IDARRAY(LANG_VOICE_DIR_HOVER), false);
1192 /* These two functions are called by the USB and shutdown handlers */
1193 void tree_flush(void)
1195 #ifdef HAVE_TAGCACHE
1196 tagcache_shutdown();
1197 #endif
1199 #ifdef HAVE_TC_RAMCACHE
1200 tagcache_unload_ramcache();
1201 #endif
1203 #ifdef HAVE_DIRCACHE
1205 int old_val = global_status.dircache_size;
1206 if (global_settings.dircache)
1208 if (!dircache_is_initializing())
1209 global_status.dircache_size = dircache_get_cache_size();
1210 # ifdef HAVE_EEPROM_SETTINGS
1211 if (firmware_settings.initialized)
1212 dircache_save();
1213 # endif
1214 dircache_disable();
1216 else
1218 global_status.dircache_size = 0;
1220 if (old_val != global_status.dircache_size)
1221 status_save();
1223 #endif
1226 void tree_restore(void)
1228 #ifdef HAVE_EEPROM_SETTINGS
1229 firmware_settings.disk_clean = false;
1230 #endif
1232 #ifdef HAVE_TC_RAMCACHE
1233 remove(TAGCACHE_STATEFILE);
1234 #endif
1236 #ifdef HAVE_DIRCACHE
1237 remove(DIRCACHE_FILE);
1238 if (global_settings.dircache)
1240 /* Print "Scanning disk..." to the display. */
1241 splash(0, str(LANG_SCANNING_DISK));
1243 dircache_build(global_status.dircache_size);
1245 #endif
1246 #ifdef HAVE_TAGCACHE
1247 tagcache_start_scan();
1248 #endif