GSoC/Buflib: Add buflib memory alocator to the core.
[kugel-rb.git] / apps / tree.c
blob211ddb2f9be519ef002c63b0a42797e71b16d3a3
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 "core_alloc.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 char lastfile[MAX_PATH];
92 static char lastdir[MAX_PATH];
93 #ifdef HAVE_TAGCACHE
94 static int lasttable, lastextra, lastfirstpos;
95 #endif
97 static bool reload_dir = false;
99 static bool start_wps = false;
100 static int curr_context = false;/* id3db or tree*/
102 static int dirbrowse(void);
103 static int ft_play_dirname(char* name);
104 static void ft_play_filename(char *dir, char *file);
105 static void say_filetype(int attr);
107 static struct entry* get_entry_at(struct tree_context *t, int index)
109 struct entry* entries = t->cache.entries;
110 return &entries[index];
113 static const char* tree_get_filename(int selected_item, void *data,
114 char *buffer, size_t buffer_len)
116 struct tree_context * local_tc=(struct tree_context *)data;
117 char *name;
118 int attr=0;
119 bool stripit = false;
120 #ifdef HAVE_TAGCACHE
121 bool id3db = *(local_tc->dirfilter) == SHOW_ID3DB;
123 if (id3db)
125 return tagtree_get_entry(&tc, selected_item)->name;
127 else
128 #endif
130 struct entry* e = get_entry_at(local_tc, selected_item);
131 name = e->name;
132 attr = e->attr;
135 if(!(attr & ATTR_DIRECTORY))
137 switch(global_settings.show_filename_ext)
139 case 0:
140 /* show file extension: off */
141 stripit = true;
142 break;
143 case 1:
144 /* show file extension: on */
145 break;
146 case 2:
147 /* show file extension: only unknown types */
148 stripit = filetype_supported(attr);
149 break;
150 case 3:
151 default:
152 /* show file extension: only when viewing all */
153 stripit = (*(local_tc->dirfilter) != SHOW_ID3DB) &&
154 (*(local_tc->dirfilter) != SHOW_ALL);
155 break;
159 if(stripit)
161 return(strip_extension(buffer, buffer_len, name));
163 return(name);
166 #ifdef HAVE_LCD_COLOR
167 static int tree_get_filecolor(int selected_item, void * data)
169 if (*tc.dirfilter == SHOW_ID3DB)
170 return -1;
171 struct tree_context * local_tc=(struct tree_context *)data;
172 struct entry* e = get_entry_at(local_tc, selected_item);
173 return filetype_get_color(e->name, e->attr);
175 #endif
177 static enum themable_icons tree_get_fileicon(int selected_item, void * data)
179 struct tree_context * local_tc=(struct tree_context *)data;
180 #ifdef HAVE_TAGCACHE
181 bool id3db = *(local_tc->dirfilter) == SHOW_ID3DB;
182 if (id3db) {
183 return tagtree_get_icon(&tc);
185 else
186 #endif
188 struct entry* e = get_entry_at(local_tc, 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* e = get_entry_at(local_tc, selected_item);
210 name = e->name;
211 attr = e->attr;
213 bool is_dir = (attr & ATTR_DIRECTORY);
214 bool did_clip = false;
215 /* First the .talk clip case */
216 if(is_dir)
218 if(global_settings.talk_dir_clip)
220 did_clip = true;
221 if(ft_play_dirname(name) <0)
222 /* failed, not existing */
223 did_clip = false;
225 } else { /* it's a file */
226 if (global_settings.talk_file_clip && (attr & FILE_ATTR_THUMBNAIL))
228 did_clip = true;
229 ft_play_filename(local_tc->currdir, name);
232 if(!did_clip)
234 /* say the number or spell if required or as a fallback */
235 switch (is_dir ? global_settings.talk_dir : global_settings.talk_file)
237 case 1: /* as numbers */
238 talk_id(is_dir ? VOICE_DIR : VOICE_FILE, false);
239 talk_number(selected_item+1 - (is_dir ? 0 : local_tc->dirsindir),
240 true);
241 if(global_settings.talk_filetype
242 && !is_dir && *local_tc->dirfilter < NUM_FILTER_MODES)
243 say_filetype(attr);
244 break;
245 case 2: /* spelled */
246 talk_shutup();
247 if(global_settings.talk_filetype)
249 if(is_dir)
250 talk_id(VOICE_DIR, true);
251 else if(*local_tc->dirfilter < NUM_FILTER_MODES)
252 say_filetype(attr);
254 talk_spell(name, true);
255 break;
258 return 0;
261 bool check_rockboxdir(void)
263 if(!dir_exists(ROCKBOX_DIR))
264 { /* No need to localise this message.
265 If .rockbox is missing, it wouldn't work anyway */
266 int i;
267 FOR_NB_SCREENS(i)
268 screens[i].clear_display();
269 splash(HZ*2, "No .rockbox directory");
270 FOR_NB_SCREENS(i)
271 screens[i].clear_display();
272 splash(HZ*2, "Installation incomplete");
273 return false;
275 return true;
278 /* do this really late in the init sequence */
279 void tree_gui_init(void)
281 check_rockboxdir();
283 strcpy(tc.currdir, "/");
285 #ifdef HAVE_LCD_CHARCELLS
286 int i;
287 FOR_NB_SCREENS(i)
288 screens[i].double_height(false);
289 #endif
290 #ifdef HAVE_BUTTONBAR
291 gui_buttonbar_init(&tree_buttonbar);
292 /* since archos only have one screen, no need to create more than that */
293 gui_buttonbar_set_display(&tree_buttonbar, &(screens[SCREEN_MAIN]) );
294 #endif
295 gui_synclist_init(&tree_lists, &tree_get_filename, &tc, false, 1, NULL);
296 gui_synclist_set_voice_callback(&tree_lists, tree_voice_cb);
297 gui_synclist_set_icon_callback(&tree_lists,
298 global_settings.show_icons?&tree_get_fileicon:NULL);
299 #ifdef HAVE_LCD_COLOR
300 gui_synclist_set_color_callback(&tree_lists, &tree_get_filecolor);
301 #endif
305 /* drawer function for the GUI_EVENT_REDRAW callback */
306 void tree_drawlists(void)
308 /* band-aid to fix the bar/list redrawing properly after leaving a plugin */
309 send_event(GUI_EVENT_THEME_CHANGED, NULL);
310 /* end bandaid */
311 gui_synclist_draw(&tree_lists);
315 struct tree_context* tree_get_context(void)
317 return &tc;
321 * Returns the position of a given file in the current directory
322 * returns -1 if not found
324 static int tree_get_file_position(char * filename)
326 int i;
327 struct entry* e;
329 /* use lastfile to determine the selected item (default=0) */
330 for (i=0; i < tc.filesindir; i++)
332 e = get_entry_at(&tc, i);
333 if (!strcasecmp(e->name, filename))
334 return(i);
336 return(-1);/* no file can match, returns undefined */
340 * Called when a new dir is loaded (for example when returning from other apps ...)
341 * also completely redraws the tree
343 static int update_dir(void)
345 bool changed = false;
346 #ifdef HAVE_TAGCACHE
347 bool id3db = *tc.dirfilter == SHOW_ID3DB;
348 /* Checks for changes */
349 if (id3db) {
350 if (tc.currtable != lasttable ||
351 tc.currextra != lastextra ||
352 tc.firstpos != lastfirstpos ||
353 reload_dir)
355 if (tagtree_load(&tc) < 0)
356 return -1;
358 lasttable = tc.currtable;
359 lastextra = tc.currextra;
360 lastfirstpos = tc.firstpos;
361 changed = true;
364 else
365 #endif
367 /* if the tc.currdir has been changed, reload it ...*/
368 if (strncmp(tc.currdir, lastdir, sizeof(lastdir)) || reload_dir)
370 if (ft_load(&tc, NULL) < 0)
371 return -1;
372 strcpy(lastdir, tc.currdir);
373 changed = true;
376 /* if selected item is undefined */
377 if (tc.selected_item == -1)
379 /* use lastfile to determine the selected item */
380 tc.selected_item = tree_get_file_position(lastfile);
382 /* If the file doesn't exists, select the first one (default) */
383 if(tc.selected_item < 0)
384 tc.selected_item = 0;
385 changed = true;
387 if (changed)
390 #ifdef HAVE_TAGCACHE
391 !id3db &&
392 #endif
393 tc.dirfull )
395 splash(HZ, ID2P(LANG_SHOWDIR_BUFFER_FULL));
398 #ifdef HAVE_TAGCACHE
399 if (id3db)
401 #ifdef HAVE_LCD_BITMAP
402 if (global_settings.show_path_in_browser == SHOW_PATH_FULL
403 || global_settings.show_path_in_browser == SHOW_PATH_CURRENT)
405 gui_synclist_set_title(&tree_lists, tagtree_get_title(&tc),
406 filetype_get_icon(ATTR_DIRECTORY));
408 else
410 /* Must clear the title as the list is reused */
411 gui_synclist_set_title(&tree_lists, NULL, NOICON);
413 #endif
415 else
416 #endif
418 #ifdef HAVE_LCD_BITMAP
419 if (tc.browse && tc.browse->title)
421 int icon = tc.browse->icon;
422 if (icon == NOICON)
423 icon = filetype_get_icon(ATTR_DIRECTORY);
424 gui_synclist_set_title(&tree_lists, tc.browse->title, icon);
426 else if (global_settings.show_path_in_browser == SHOW_PATH_FULL)
428 gui_synclist_set_title(&tree_lists, tc.currdir,
429 filetype_get_icon(ATTR_DIRECTORY));
431 else if (global_settings.show_path_in_browser == SHOW_PATH_CURRENT)
433 char *title = strrchr(tc.currdir, '/') + 1;
434 if (*title == '\0')
436 /* Display "Files" for the root dir */
437 gui_synclist_set_title(&tree_lists, str(LANG_DIR_BROWSER),
438 filetype_get_icon(ATTR_DIRECTORY));
440 else
441 gui_synclist_set_title(&tree_lists, title,
442 filetype_get_icon(ATTR_DIRECTORY));
444 else
446 /* Must clear the title as the list is reused */
447 gui_synclist_set_title(&tree_lists, NULL, NOICON);
449 #endif
452 gui_synclist_set_nb_items(&tree_lists, tc.filesindir);
453 gui_synclist_set_icon_callback(&tree_lists,
454 global_settings.show_icons?tree_get_fileicon:NULL);
455 if( tc.selected_item >= tc.filesindir)
456 tc.selected_item=tc.filesindir-1;
458 gui_synclist_select_item(&tree_lists, tc.selected_item);
459 #ifdef HAVE_BUTTONBAR
460 if (global_settings.buttonbar) {
461 if (*tc.dirfilter < NUM_FILTER_MODES)
462 gui_buttonbar_set(&tree_buttonbar, str(LANG_SYSFONT_DIRBROWSE_F1),
463 str(LANG_SYSFONT_DIRBROWSE_F2),
464 str(LANG_SYSFONT_DIRBROWSE_F3));
465 else
466 gui_buttonbar_set(&tree_buttonbar, "<<<", "", "");
467 gui_buttonbar_draw(&tree_buttonbar);
469 #endif
470 gui_synclist_draw(&tree_lists);
471 gui_synclist_speak_item(&tree_lists);
472 return tc.filesindir;
475 /* load tracks from specified directory to resume play */
476 void resume_directory(const char *dir)
478 int dirfilter = *tc.dirfilter;
479 int ret;
480 #ifdef HAVE_TAGCACHE
481 bool id3db = *tc.dirfilter == SHOW_ID3DB;
482 #endif
483 /* make sure the dirfilter is sane. The only time it should be possible
484 * thats its not is when resume playlist is called from a plugin
486 #ifdef HAVE_TAGCACHE
487 if (!id3db)
488 #endif
489 *tc.dirfilter = global_settings.dirfilter;
490 ret = ft_load(&tc, dir);
491 *tc.dirfilter = dirfilter;
492 if (ret < 0)
493 return;
494 lastdir[0] = 0;
496 ft_build_playlist(&tc, 0);
498 #ifdef HAVE_TAGCACHE
499 if (id3db)
500 tagtree_load(&tc);
501 #endif
504 /* Returns the current working directory and also writes cwd to buf if
505 non-NULL. In case of error, returns NULL. */
506 char *getcwd(char *buf, getcwd_size_t size)
508 if (!buf)
509 return tc.currdir;
510 else if (size)
512 if ((getcwd_size_t)strlcpy(buf, tc.currdir, size) < size)
513 return buf;
515 /* size == 0, or truncation in strlcpy */
516 return NULL;
519 /* Force a reload of the directory next time directory browser is called */
520 void reload_directory(void)
522 reload_dir = true;
525 char* get_current_file(char* buffer, size_t buffer_len)
527 #ifdef HAVE_TAGCACHE
528 /* in ID3DB mode it is a bad idea to call this function */
529 /* (only happens with `follow playlist') */
530 if( *tc.dirfilter == SHOW_ID3DB )
531 return NULL;
532 #endif
534 struct entry* e = get_entry_at(&tc, tc.selected_item);
535 if (getcwd(buffer, buffer_len))
537 if (tc.dirlength)
539 if (buffer[strlen(buffer)-1] != '/')
540 strlcat(buffer, "/", buffer_len);
541 if (strlcat(buffer, e->name, buffer_len) >= buffer_len)
542 return NULL;
544 return buffer;
546 return NULL;
549 /* Allow apps to change our dirfilter directly (required for sub browsers)
550 if they're suddenly going to become a file browser for example */
551 void set_dirfilter(int l_dirfilter)
553 *tc.dirfilter = l_dirfilter;
556 /* Selects a file and update tree context properly */
557 void set_current_file(const char *path)
559 const char *name;
560 int i;
562 #ifdef HAVE_TAGCACHE
563 /* in ID3DB mode it is a bad idea to call this function */
564 /* (only happens with `follow playlist') */
565 if( *tc.dirfilter == SHOW_ID3DB )
566 return;
567 #endif
569 /* separate directory from filename */
570 /* gets the directory's name and put it into tc.currdir */
571 name = strrchr(path+1,'/');
572 if (name)
574 strlcpy(tc.currdir, path, name - path + 1);
575 name++;
577 else
579 strcpy(tc.currdir, "/");
580 name = path+1;
583 strcpy(lastfile, name);
586 /* If we changed dir we must recalculate the dirlevel
587 and adjust the selected history properly */
588 if (strncmp(tc.currdir,lastdir,sizeof(lastdir)))
590 tc.dirlevel = 0;
591 tc.selected_item_history[tc.dirlevel] = -1;
593 /* use '/' to calculate dirlevel */
594 for (i = 1; path[i] != '\0'; i++)
596 if (path[i] == '/')
598 tc.dirlevel++;
599 tc.selected_item_history[tc.dirlevel] = -1;
603 if (ft_load(&tc, NULL) >= 0)
605 tc.selected_item = tree_get_file_position(lastfile);
610 /* main loop, handles key events */
611 static int dirbrowse(void)
613 int numentries=0;
614 char buf[MAX_PATH];
615 int button, oldbutton;
616 bool reload_root = false;
617 int lastfilter = *tc.dirfilter;
618 bool lastsortcase = global_settings.sort_case;
619 bool exit_func = false;
621 char* currdir = tc.currdir; /* just a shortcut */
622 #ifdef HAVE_TAGCACHE
623 bool id3db = *tc.dirfilter == SHOW_ID3DB;
625 if (id3db)
626 curr_context=CONTEXT_ID3DB;
627 else
628 #endif
629 curr_context=CONTEXT_TREE;
630 if (tc.selected_item < 0)
631 tc.selected_item = 0;
632 #ifdef HAVE_TAGCACHE
633 tc.firstpos = 0;
634 lasttable = -1;
635 lastextra = -1;
636 lastfirstpos = 0;
637 #endif
639 start_wps = false;
640 numentries = update_dir();
641 reload_dir = false;
642 if (numentries == -1)
643 return GO_TO_PREVIOUS; /* currdir is not a directory */
645 if (*tc.dirfilter > NUM_FILTER_MODES && numentries==0)
647 splash(HZ*2, ID2P(LANG_NO_FILES));
648 return GO_TO_PREVIOUS; /* No files found for rockbox_browse() */
651 gui_synclist_draw(&tree_lists);
652 while(1) {
653 struct entry *entries = tc.cache.entries;
654 bool restore = false;
655 if (tc.dirlevel < 0)
656 tc.dirlevel = 0; /* shouldnt be needed.. this code needs work! */
658 button = get_action(CONTEXT_TREE,
659 list_do_action_timeout(&tree_lists, HZ/2));
660 oldbutton = button;
661 gui_synclist_do_button(&tree_lists, &button,LIST_WRAP_UNLESS_HELD);
662 tc.selected_item = gui_synclist_get_sel_pos(&tree_lists);
663 switch ( button ) {
664 case ACTION_STD_OK:
665 /* nothing to do if no files to display */
666 if ( numentries == 0 )
667 break;
669 if ((tc.browse->flags & BROWSE_SELECTONLY) &&
670 !(entries[tc.selected_item].attr & ATTR_DIRECTORY))
672 tc.browse->flags |= BROWSE_SELECTED;
673 get_current_file(tc.browse->buf, tc.browse->bufsize);
674 return GO_TO_PREVIOUS;
677 #ifdef HAVE_TAGCACHE
678 switch (id3db?tagtree_enter(&tc):ft_enter(&tc))
679 #else
680 switch (ft_enter(&tc))
681 #endif
683 case GO_TO_FILEBROWSER: reload_dir = true; break;
684 case GO_TO_WPS:
685 return GO_TO_WPS;
686 #if CONFIG_TUNER
687 case GO_TO_FM:
688 return GO_TO_FM;
689 #endif
690 case GO_TO_ROOT: exit_func = true; break;
691 default: break;
693 restore = true;
694 break;
696 case ACTION_STD_CANCEL:
697 if (*tc.dirfilter > NUM_FILTER_MODES && tc.dirlevel < 1) {
698 exit_func = true;
699 break;
701 if ((*tc.dirfilter == SHOW_ID3DB && tc.dirlevel == 0) ||
702 ((*tc.dirfilter != SHOW_ID3DB && !strcmp(currdir,"/"))))
704 #ifdef HAVE_LCD_BITMAP /* charcell doesnt have ACTION_TREE_PGLEFT so this isnt needed */
705 if (oldbutton == ACTION_TREE_PGLEFT)
706 break;
707 else
708 #endif
709 return GO_TO_ROOT;
712 #ifdef HAVE_TAGCACHE
713 if (id3db)
714 tagtree_exit(&tc);
715 else
716 #endif
717 if (ft_exit(&tc) == 3)
718 exit_func = true;
720 restore = true;
721 break;
723 case ACTION_TREE_STOP:
724 if (list_stop_handler())
725 restore = true;
726 break;
728 case ACTION_STD_MENU:
729 return GO_TO_ROOT;
730 break;
732 #ifdef HAVE_RECORDING
733 case ACTION_STD_REC:
734 return GO_TO_RECSCREEN;
735 #endif
737 case ACTION_TREE_WPS:
738 return GO_TO_PREVIOUS_MUSIC;
739 break;
740 #ifdef HAVE_QUICKSCREEN
741 case ACTION_STD_QUICKSCREEN:
742 /* don't enter f2 from plugin browser */
743 if (*tc.dirfilter < NUM_FILTER_MODES)
745 if (quick_screen_quick(button))
746 reload_dir = true;
747 restore = true;
749 break;
750 #endif
751 #ifdef BUTTON_F3
752 case ACTION_F3:
753 /* don't enter f3 from plugin browser */
754 if (*tc.dirfilter < NUM_FILTER_MODES)
756 if (quick_screen_f3(ACTION_F3))
757 reload_dir = true;
758 restore = true;
760 break;
761 #endif
763 #ifdef HAVE_HOTKEY
764 case ACTION_TREE_HOTKEY:
765 if (!global_settings.hotkey_tree)
766 break;
767 /* fall through */
768 #endif
769 case ACTION_STD_CONTEXT:
771 bool hotkey = button == ACTION_TREE_HOTKEY;
772 int onplay_result;
773 int attr = 0;
775 if (tc.browse->flags & BROWSE_NO_CONTEXT_MENU)
776 break;
778 if(!numentries)
779 onplay_result = onplay(NULL, 0, curr_context, hotkey);
780 else {
781 #ifdef HAVE_TAGCACHE
782 if (id3db)
784 if (tagtree_get_attr(&tc) == FILE_ATTR_AUDIO)
786 attr = FILE_ATTR_AUDIO;
787 tagtree_get_filename(&tc, buf, sizeof(buf));
789 else
790 attr = ATTR_DIRECTORY;
792 else
793 #endif
795 attr = entries[tc.selected_item].attr;
797 if (currdir[1]) /* Not in / */
798 snprintf(buf, sizeof buf, "%s/%s",
799 currdir,
800 entries[tc.selected_item].name);
801 else /* In / */
802 snprintf(buf, sizeof buf, "/%s",
803 entries[tc.selected_item].name);
805 onplay_result = onplay(buf, attr, curr_context, hotkey);
807 switch (onplay_result)
809 case ONPLAY_MAINMENU:
810 return GO_TO_ROOT;
812 case ONPLAY_OK:
813 restore = true;
814 break;
816 case ONPLAY_RELOAD_DIR:
817 reload_dir = true;
818 break;
820 case ONPLAY_START_PLAY:
821 return GO_TO_WPS;
822 break;
824 break;
827 #ifdef HAVE_HOTSWAP
828 case SYS_FS_CHANGED:
829 #ifdef HAVE_TAGCACHE
830 if (!id3db)
831 #endif
832 reload_dir = true;
833 /* The 'dir no longer valid' situation will be caught later
834 * by checking the showdir() result. */
835 break;
836 #endif
838 default:
839 if (default_event_handler(button) == SYS_USB_CONNECTED)
841 if(*tc.dirfilter > NUM_FILTER_MODES)
842 /* leave sub-browsers after usb, doing otherwise
843 might be confusing to the user */
844 exit_func = true;
845 else
846 reload_dir = true;
848 break;
850 if (start_wps)
851 return GO_TO_WPS;
852 if (button && !IS_SYSEVENT(button))
854 storage_spin();
858 check_rescan:
859 /* do we need to rescan dir? */
860 if (reload_dir || reload_root ||
861 lastfilter != *tc.dirfilter ||
862 lastsortcase != global_settings.sort_case)
864 if (reload_root) {
865 strcpy(currdir, "/");
866 tc.dirlevel = 0;
867 #ifdef HAVE_TAGCACHE
868 tc.currtable = 0;
869 tc.currextra = 0;
870 lasttable = -1;
871 lastextra = -1;
872 #endif
873 reload_root = false;
876 if (!reload_dir)
878 gui_synclist_select_item(&tree_lists, 0);
879 gui_synclist_draw(&tree_lists);
880 tc.selected_item = 0;
881 lastdir[0] = 0;
884 lastfilter = *tc.dirfilter;
885 lastsortcase = global_settings.sort_case;
886 restore = true;
889 if (exit_func)
890 return GO_TO_PREVIOUS;
892 if (restore || reload_dir) {
893 /* restore display */
894 numentries = update_dir();
895 reload_dir = false;
896 if (currdir[1] && (numentries < 0))
897 { /* not in root and reload failed */
898 reload_root = true; /* try root */
899 goto check_rescan;
903 return true;
906 bool create_playlist(void)
908 char filename[MAX_PATH];
910 if (tc.currdir[1])
911 snprintf(filename, sizeof filename, "%s.m3u8", tc.currdir);
912 else
913 snprintf(filename, sizeof filename, "%s/all.m3u8",
914 catalog_get_directory());
917 if (kbd_input(filename, MAX_PATH))
918 return false;
919 splashf(0, "%s %s", str(LANG_CREATING), filename);
921 trigger_cpu_boost();
922 catalog_add_to_a_playlist(tc.currdir, ATTR_DIRECTORY, true, filename);
923 cancel_cpu_boost();
925 return true;
928 void browse_context_init(struct browse_context *browse,
929 int dirfilter, unsigned flags,
930 char *title, enum themable_icons icon,
931 const char *root, const char *selected)
933 browse->dirfilter = dirfilter;
934 browse->flags = flags;
935 browse->callback_show_item = NULL;
936 browse->title = title;
937 browse->icon = icon;
938 browse->root = root;
939 browse->selected = selected;
940 browse->buf = NULL;
941 browse->bufsize = 0;
944 #define NUM_TC_BACKUP 3
945 static struct tree_context backups[NUM_TC_BACKUP];
946 /* do not make backup if it is not recursive call */
947 static int backup_count = -1;
948 int rockbox_browse(struct browse_context *browse)
950 static char current[MAX_PATH];
951 int ret_val = 0;
952 int dirfilter = browse->dirfilter;
954 if (backup_count >= NUM_TC_BACKUP)
955 return GO_TO_PREVIOUS;
956 if (backup_count >= 0)
957 backups[backup_count] = tc;
958 backup_count++;
960 tc.dirfilter = &dirfilter;
961 tc.sort_dir = global_settings.sort_dir;
963 reload_dir = true;
964 if (*tc.dirfilter >= NUM_FILTER_MODES)
966 int last_context;
968 tc.browse = browse;
969 tc.selected_item = 0;
970 tc.dirlevel = 0;
971 strlcpy(tc.currdir, browse->root, sizeof(tc.currdir));
972 start_wps = false;
973 last_context = curr_context;
975 if (browse->selected)
977 snprintf(current, sizeof(current), "%s/%s",
978 browse->root, browse->selected);
979 set_current_file(current);
980 /* set_current_file changes dirlevel, change it back */
981 tc.dirlevel = 0;
984 ret_val = dirbrowse();
985 curr_context = last_context;
987 else
989 if (dirfilter != SHOW_ID3DB)
990 tc.dirfilter = &global_settings.dirfilter;
991 tc.browse = browse;
992 strcpy(current, browse->root);
993 set_current_file(current);
994 ret_val = dirbrowse();
996 backup_count--;
997 if (backup_count >= 0)
998 tc = backups[backup_count];
999 return ret_val;
1002 void tree_mem_init(void)
1004 /* initialize tree context struct */
1005 int handle;
1006 struct tree_cache* cache = &tc.cache;
1007 memset(&tc, 0, sizeof(tc));
1008 tc.dirfilter = &global_settings.dirfilter;
1009 tc.sort_dir = global_settings.sort_dir;
1011 cache->name_buffer_size = AVERAGE_FILENAME_LENGTH *
1012 global_settings.max_files_in_dir;
1013 handle = core_alloc("tree names", cache->name_buffer_size);
1014 cache->name_buffer = core_get_data(handle);
1016 cache->max_entries = global_settings.max_files_in_dir;
1017 handle = core_alloc("tree entries", cache->max_entries*(sizeof(struct entry)));
1018 cache->entries = core_get_data(handle);
1019 tree_get_filetypes(&filetypes, &filetypes_count);
1022 bool bookmark_play(char *resume_file, int index, int offset, int seed,
1023 char *filename)
1025 int i;
1026 char* suffix = strrchr(resume_file, '.');
1027 bool started = false;
1029 if (suffix != NULL &&
1030 (!strcasecmp(suffix, ".m3u") || !strcasecmp(suffix, ".m3u8")))
1032 /* Playlist playback */
1033 char* slash;
1034 /* check that the file exists */
1035 if(!file_exists(resume_file))
1036 return false;
1038 slash = strrchr(resume_file,'/');
1039 if (slash)
1041 char* cp;
1042 *slash=0;
1044 cp=resume_file;
1045 if (!cp[0])
1046 cp="/";
1048 if (playlist_create(cp, slash+1) != -1)
1050 if (global_settings.playlist_shuffle)
1051 playlist_shuffle(seed, -1);
1052 playlist_start(index,offset);
1053 started = true;
1055 *slash='/';
1058 else
1060 /* Directory playback */
1061 lastdir[0]='\0';
1062 if (playlist_create(resume_file, NULL) != -1)
1064 char filename_buf[MAX_PATH + 1];
1065 const char* peek_filename;
1066 resume_directory(resume_file);
1067 if (global_settings.playlist_shuffle)
1068 playlist_shuffle(seed, -1);
1070 /* Check if the file is at the same spot in the directory,
1071 else search for it */
1072 peek_filename = playlist_peek(index, filename_buf,
1073 sizeof(filename_buf));
1075 if (peek_filename == NULL)
1077 /* playlist has shrunk, search from the top */
1078 index = 0;
1079 peek_filename = playlist_peek(index, filename_buf,
1080 sizeof(filename_buf));
1081 if (peek_filename == NULL)
1082 return false;
1085 if (strcmp(strrchr(peek_filename, '/') + 1, filename))
1087 for ( i=0; i < playlist_amount(); i++ )
1089 peek_filename = playlist_peek(i, filename_buf,
1090 sizeof(filename_buf));
1092 if (peek_filename == NULL)
1093 return false;
1095 if (!strcmp(strrchr(peek_filename, '/') + 1, filename))
1096 break;
1098 if (i < playlist_amount())
1099 index = i;
1100 else
1101 return false;
1103 playlist_start(index,offset);
1104 started = true;
1108 if (started)
1109 start_wps = true;
1110 return started;
1113 static void say_filetype(int attr)
1115 /* try to find a voice ID for the extension, if known */
1116 int j;
1117 attr &= FILE_ATTR_MASK; /* file type */
1118 for (j=0; j<filetypes_count; j++)
1119 if (attr == filetypes[j].tree_attr)
1121 talk_id(filetypes[j].voiceclip, true);
1122 return;
1126 static int ft_play_dirname(char* name)
1128 #if CONFIG_CODEC != SWCODEC
1129 if (audio_status() & AUDIO_STATUS_PLAY)
1130 return 0;
1131 #endif
1133 if(talk_file(tc.currdir, name, dir_thumbnail_name, NULL,
1134 NULL, false))
1136 if(global_settings.talk_filetype)
1137 talk_id(VOICE_DIR, true);
1138 return 1;
1140 else
1141 return -1;
1144 static void ft_play_filename(char *dir, char *file)
1146 #if CONFIG_CODEC != SWCODEC
1147 if (audio_status() & AUDIO_STATUS_PLAY)
1148 return;
1149 #endif
1151 if (strlen(file) >= strlen(file_thumbnail_ext)
1152 && strcasecmp(&file[strlen(file) - strlen(file_thumbnail_ext)],
1153 file_thumbnail_ext))
1154 /* file has no .talk extension */
1155 talk_file(dir, NULL, file, file_thumbnail_ext,
1156 NULL, false);
1157 else
1158 /* it already is a .talk file, play this directly, but prefix it. */
1159 talk_file(dir, NULL, file, NULL,
1160 TALK_IDARRAY(LANG_VOICE_DIR_HOVER), false);
1163 /* These two functions are called by the USB and shutdown handlers */
1164 void tree_flush(void)
1166 #ifdef HAVE_TAGCACHE
1167 tagcache_shutdown();
1168 #endif
1170 #ifdef HAVE_TC_RAMCACHE
1171 tagcache_unload_ramcache();
1172 #endif
1174 #ifdef HAVE_DIRCACHE
1176 int old_val = global_status.dircache_size;
1177 if (global_settings.dircache)
1179 if (!dircache_is_initializing())
1180 global_status.dircache_size = dircache_get_cache_size();
1181 # ifdef HAVE_EEPROM_SETTINGS
1182 if (firmware_settings.initialized)
1183 dircache_save();
1184 # endif
1185 dircache_disable();
1187 else
1189 global_status.dircache_size = 0;
1191 if (old_val != global_status.dircache_size)
1192 status_save();
1194 #endif
1197 void tree_restore(void)
1199 #ifdef HAVE_EEPROM_SETTINGS
1200 firmware_settings.disk_clean = false;
1201 #endif
1203 #ifdef HAVE_TC_RAMCACHE
1204 remove(TAGCACHE_STATEFILE);
1205 #endif
1207 #ifdef HAVE_DIRCACHE
1208 remove(DIRCACHE_FILE);
1209 if (global_settings.dircache)
1211 /* Print "Scanning disk..." to the display. */
1212 splash(0, str(LANG_SCANNING_DISK));
1214 dircache_build(global_status.dircache_size);
1216 #endif
1217 #ifdef HAVE_TAGCACHE
1218 tagcache_start_scan();
1219 #endif