i.MX31: Implement asynchronous version of I2C driver.
[maemo-rb.git] / apps / tree.c
blob4431db2c4a85ababe0d83bc98ac44034055a6cf2
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 struct entry* tree_get_entries(struct tree_context *t)
109 return core_get_data(t->cache.entries_handle);
112 struct entry* tree_get_entry_at(struct tree_context *t, int index)
114 struct entry* entries = tree_get_entries(t);
115 return &entries[index];
119 static const char* tree_get_filename(int selected_item, void *data,
120 char *buffer, size_t buffer_len)
122 struct tree_context * local_tc=(struct tree_context *)data;
123 char *name;
124 int attr=0;
125 bool stripit = false;
126 #ifdef HAVE_TAGCACHE
127 bool id3db = *(local_tc->dirfilter) == SHOW_ID3DB;
129 if (id3db)
131 return tagtree_get_entry_name(&tc, selected_item, buffer, buffer_len);
133 else
134 #endif
136 struct entry* e = tree_get_entry_at(local_tc, selected_item);
137 name = e->name;
138 attr = e->attr;
141 if(!(attr & ATTR_DIRECTORY))
143 switch(global_settings.show_filename_ext)
145 case 0:
146 /* show file extension: off */
147 stripit = true;
148 break;
149 case 1:
150 /* show file extension: on */
151 break;
152 case 2:
153 /* show file extension: only unknown types */
154 stripit = filetype_supported(attr);
155 break;
156 case 3:
157 default:
158 /* show file extension: only when viewing all */
159 stripit = (*(local_tc->dirfilter) != SHOW_ID3DB) &&
160 (*(local_tc->dirfilter) != SHOW_ALL);
161 break;
165 if(stripit)
167 return(strip_extension(buffer, buffer_len, name));
169 return(name);
172 #ifdef HAVE_LCD_COLOR
173 static int tree_get_filecolor(int selected_item, void * data)
175 if (*tc.dirfilter == SHOW_ID3DB)
176 return -1;
177 struct tree_context * local_tc=(struct tree_context *)data;
178 struct entry* e = tree_get_entry_at(local_tc, selected_item);
179 return filetype_get_color(e->name, e->attr);
181 #endif
183 static enum themable_icons tree_get_fileicon(int selected_item, void * data)
185 struct tree_context * local_tc=(struct tree_context *)data;
186 #ifdef HAVE_TAGCACHE
187 bool id3db = *(local_tc->dirfilter) == SHOW_ID3DB;
188 if (id3db) {
189 return tagtree_get_icon(&tc);
191 else
192 #endif
194 struct entry* e = tree_get_entry_at(local_tc, selected_item);
195 return filetype_get_icon(e->attr);
199 static int tree_voice_cb(int selected_item, void * data)
201 struct tree_context * local_tc=(struct tree_context *)data;
202 char *name;
203 int attr=0;
204 #ifdef HAVE_TAGCACHE
205 bool id3db = *(local_tc->dirfilter) == SHOW_ID3DB;
206 char buf[AVERAGE_FILENAME_LENGTH*2];
208 if (id3db)
210 attr = tagtree_get_attr(local_tc);
211 name = tagtree_get_entry_name(local_tc, selected_item, buf, sizeof(buf));
213 else
214 #endif
216 struct entry* e = tree_get_entry_at(local_tc, selected_item);
217 name = e->name;
218 attr = e->attr;
220 bool is_dir = (attr & ATTR_DIRECTORY);
221 bool did_clip = false;
222 /* First the .talk clip case */
223 if(is_dir)
225 if(global_settings.talk_dir_clip)
227 did_clip = true;
228 if(ft_play_dirname(name) <0)
229 /* failed, not existing */
230 did_clip = false;
232 } else { /* it's a file */
233 if (global_settings.talk_file_clip && (attr & FILE_ATTR_THUMBNAIL))
235 did_clip = true;
236 ft_play_filename(local_tc->currdir, name);
239 if(!did_clip)
241 /* say the number or spell if required or as a fallback */
242 switch (is_dir ? global_settings.talk_dir : global_settings.talk_file)
244 case 1: /* as numbers */
245 talk_id(is_dir ? VOICE_DIR : VOICE_FILE, false);
246 talk_number(selected_item+1 - (is_dir ? 0 : local_tc->dirsindir),
247 true);
248 if(global_settings.talk_filetype
249 && !is_dir && *local_tc->dirfilter < NUM_FILTER_MODES)
250 say_filetype(attr);
251 break;
252 case 2: /* spelled */
253 talk_shutup();
254 if(global_settings.talk_filetype)
256 if(is_dir)
257 talk_id(VOICE_DIR, true);
258 else if(*local_tc->dirfilter < NUM_FILTER_MODES)
259 say_filetype(attr);
261 talk_spell(name, true);
262 break;
265 return 0;
268 bool check_rockboxdir(void)
270 if(!dir_exists(ROCKBOX_DIR))
271 { /* No need to localise this message.
272 If .rockbox is missing, it wouldn't work anyway */
273 FOR_NB_SCREENS(i)
274 screens[i].clear_display();
275 splash(HZ*2, "No .rockbox directory");
276 FOR_NB_SCREENS(i)
277 screens[i].clear_display();
278 splash(HZ*2, "Installation incomplete");
279 return false;
281 return true;
284 /* do this really late in the init sequence */
285 void tree_gui_init(void)
287 check_rockboxdir();
289 strcpy(tc.currdir, "/");
291 #ifdef HAVE_LCD_CHARCELLS
292 FOR_NB_SCREENS(i)
293 screens[i].double_height(false);
294 #endif
295 #ifdef HAVE_BUTTONBAR
296 gui_buttonbar_init(&tree_buttonbar);
297 /* since archos only have one screen, no need to create more than that */
298 gui_buttonbar_set_display(&tree_buttonbar, &(screens[SCREEN_MAIN]) );
299 #endif
300 gui_synclist_init(&tree_lists, &tree_get_filename, &tc, false, 1, NULL);
301 gui_synclist_set_voice_callback(&tree_lists, tree_voice_cb);
302 gui_synclist_set_icon_callback(&tree_lists,
303 global_settings.show_icons?&tree_get_fileicon:NULL);
304 #ifdef HAVE_LCD_COLOR
305 gui_synclist_set_color_callback(&tree_lists, &tree_get_filecolor);
306 #endif
310 struct tree_context* tree_get_context(void)
312 return &tc;
316 * Returns the position of a given file in the current directory
317 * returns -1 if not found
319 static int tree_get_file_position(char * filename)
321 int i;
322 struct entry* e;
324 /* use lastfile to determine the selected item (default=0) */
325 for (i=0; i < tc.filesindir; i++)
327 e = tree_get_entry_at(&tc, i);
328 if (!strcasecmp(e->name, filename))
329 return(i);
331 return(-1);/* no file can match, returns undefined */
335 * Called when a new dir is loaded (for example when returning from other apps ...)
336 * also completely redraws the tree
338 static int update_dir(void)
340 bool changed = false;
341 #ifdef HAVE_TAGCACHE
342 bool id3db = *tc.dirfilter == SHOW_ID3DB;
343 /* Checks for changes */
344 if (id3db) {
345 if (tc.currtable != lasttable ||
346 tc.currextra != lastextra ||
347 tc.firstpos != lastfirstpos ||
348 reload_dir)
350 if (tagtree_load(&tc) < 0)
351 return -1;
353 lasttable = tc.currtable;
354 lastextra = tc.currextra;
355 lastfirstpos = tc.firstpos;
356 changed = true;
359 else
360 #endif
362 /* if the tc.currdir has been changed, reload it ...*/
363 if (strncmp(tc.currdir, lastdir, sizeof(lastdir)) || reload_dir)
365 if (ft_load(&tc, NULL) < 0)
366 return -1;
367 strcpy(lastdir, tc.currdir);
368 changed = true;
371 /* if selected item is undefined */
372 if (tc.selected_item == -1)
374 /* use lastfile to determine the selected item */
375 tc.selected_item = tree_get_file_position(lastfile);
377 /* If the file doesn't exists, select the first one (default) */
378 if(tc.selected_item < 0)
379 tc.selected_item = 0;
380 changed = true;
382 if (changed)
385 #ifdef HAVE_TAGCACHE
386 !id3db &&
387 #endif
388 tc.dirfull )
390 splash(HZ, ID2P(LANG_SHOWDIR_BUFFER_FULL));
393 #ifdef HAVE_TAGCACHE
394 if (id3db)
396 #ifdef HAVE_LCD_BITMAP
397 if (global_settings.show_path_in_browser == SHOW_PATH_FULL
398 || global_settings.show_path_in_browser == SHOW_PATH_CURRENT)
400 gui_synclist_set_title(&tree_lists, tagtree_get_title(&tc),
401 filetype_get_icon(ATTR_DIRECTORY));
403 else
405 /* Must clear the title as the list is reused */
406 gui_synclist_set_title(&tree_lists, NULL, NOICON);
408 #endif
410 else
411 #endif
413 #ifdef HAVE_LCD_BITMAP
414 if (tc.browse && tc.browse->title)
416 int icon = tc.browse->icon;
417 if (icon == NOICON)
418 icon = filetype_get_icon(ATTR_DIRECTORY);
419 gui_synclist_set_title(&tree_lists, tc.browse->title, icon);
421 else if (global_settings.show_path_in_browser == SHOW_PATH_FULL)
423 gui_synclist_set_title(&tree_lists, tc.currdir,
424 filetype_get_icon(ATTR_DIRECTORY));
426 else if (global_settings.show_path_in_browser == SHOW_PATH_CURRENT)
428 char *title = strrchr(tc.currdir, '/') + 1;
429 if (*title == '\0')
431 /* Display "Files" for the root dir */
432 gui_synclist_set_title(&tree_lists, str(LANG_DIR_BROWSER),
433 filetype_get_icon(ATTR_DIRECTORY));
435 else
436 gui_synclist_set_title(&tree_lists, title,
437 filetype_get_icon(ATTR_DIRECTORY));
439 else
441 /* Must clear the title as the list is reused */
442 gui_synclist_set_title(&tree_lists, NULL, NOICON);
444 #endif
447 gui_synclist_set_nb_items(&tree_lists, tc.filesindir);
448 gui_synclist_set_icon_callback(&tree_lists,
449 global_settings.show_icons?tree_get_fileicon:NULL);
450 if( tc.selected_item >= tc.filesindir)
451 tc.selected_item=tc.filesindir-1;
453 gui_synclist_select_item(&tree_lists, tc.selected_item);
454 #ifdef HAVE_BUTTONBAR
455 if (global_settings.buttonbar) {
456 if (*tc.dirfilter < NUM_FILTER_MODES)
457 gui_buttonbar_set(&tree_buttonbar, str(LANG_SYSFONT_DIRBROWSE_F1),
458 str(LANG_SYSFONT_DIRBROWSE_F2),
459 str(LANG_SYSFONT_DIRBROWSE_F3));
460 else
461 gui_buttonbar_set(&tree_buttonbar, "<<<", "", "");
462 gui_buttonbar_draw(&tree_buttonbar);
464 #endif
465 gui_synclist_draw(&tree_lists);
466 gui_synclist_speak_item(&tree_lists);
467 return tc.filesindir;
470 /* load tracks from specified directory to resume play */
471 void resume_directory(const char *dir)
473 int dirfilter = *tc.dirfilter;
474 int ret;
475 #ifdef HAVE_TAGCACHE
476 bool id3db = *tc.dirfilter == SHOW_ID3DB;
477 #endif
478 /* make sure the dirfilter is sane. The only time it should be possible
479 * thats its not is when resume playlist is called from a plugin
481 #ifdef HAVE_TAGCACHE
482 if (!id3db)
483 #endif
484 *tc.dirfilter = global_settings.dirfilter;
485 ret = ft_load(&tc, dir);
486 *tc.dirfilter = dirfilter;
487 if (ret < 0)
488 return;
489 lastdir[0] = 0;
491 ft_build_playlist(&tc, 0);
493 #ifdef HAVE_TAGCACHE
494 if (id3db)
495 tagtree_load(&tc);
496 #endif
499 /* Returns the current working directory and also writes cwd to buf if
500 non-NULL. In case of error, returns NULL. */
501 char *getcwd(char *buf, getcwd_size_t size)
503 if (!buf)
504 return tc.currdir;
505 else if (size)
507 if ((getcwd_size_t)strlcpy(buf, tc.currdir, size) < size)
508 return buf;
510 /* size == 0, or truncation in strlcpy */
511 return NULL;
514 /* Force a reload of the directory next time directory browser is called */
515 void reload_directory(void)
517 reload_dir = true;
520 char* get_current_file(char* buffer, size_t buffer_len)
522 #ifdef HAVE_TAGCACHE
523 /* in ID3DB mode it is a bad idea to call this function */
524 /* (only happens with `follow playlist') */
525 if( *tc.dirfilter == SHOW_ID3DB )
526 return NULL;
527 #endif
529 struct entry* e = tree_get_entry_at(&tc, tc.selected_item);
530 if (getcwd(buffer, buffer_len))
532 if (tc.dirlength)
534 if (buffer[strlen(buffer)-1] != '/')
535 strlcat(buffer, "/", buffer_len);
536 if (strlcat(buffer, e->name, buffer_len) >= buffer_len)
537 return NULL;
539 return buffer;
541 return NULL;
544 /* Allow apps to change our dirfilter directly (required for sub browsers)
545 if they're suddenly going to become a file browser for example */
546 void set_dirfilter(int l_dirfilter)
548 *tc.dirfilter = l_dirfilter;
551 /* Selects a file and update tree context properly */
552 void set_current_file(const char *path)
554 const char *name;
555 int i;
557 #ifdef HAVE_TAGCACHE
558 /* in ID3DB mode it is a bad idea to call this function */
559 /* (only happens with `follow playlist') */
560 if( *tc.dirfilter == SHOW_ID3DB )
561 return;
562 #endif
564 /* separate directory from filename */
565 /* gets the directory's name and put it into tc.currdir */
566 name = strrchr(path+1,'/');
567 if (name)
569 strlcpy(tc.currdir, path, name - path + 1);
570 name++;
572 else
574 strcpy(tc.currdir, "/");
575 name = path+1;
578 strcpy(lastfile, name);
581 /* If we changed dir we must recalculate the dirlevel
582 and adjust the selected history properly */
583 if (strncmp(tc.currdir,lastdir,sizeof(lastdir)))
585 tc.dirlevel = 0;
586 tc.selected_item_history[tc.dirlevel] = -1;
588 /* use '/' to calculate dirlevel */
589 for (i = 1; path[i] != '\0'; i++)
591 if (path[i] == '/')
593 tc.dirlevel++;
594 tc.selected_item_history[tc.dirlevel] = -1;
598 if (ft_load(&tc, NULL) >= 0)
600 tc.selected_item = tree_get_file_position(lastfile);
605 /* main loop, handles key events */
606 static int dirbrowse(void)
608 int numentries=0;
609 char buf[MAX_PATH];
610 int button;
611 #ifdef HAVE_LCD_BITMAP
612 int oldbutton;
613 #endif
614 bool reload_root = false;
615 int lastfilter = *tc.dirfilter;
616 bool lastsortcase = global_settings.sort_case;
617 bool exit_func = false;
619 char* currdir = tc.currdir; /* just a shortcut */
620 #ifdef HAVE_TAGCACHE
621 bool id3db = *tc.dirfilter == SHOW_ID3DB;
623 if (id3db)
624 curr_context=CONTEXT_ID3DB;
625 else
626 #endif
627 curr_context=CONTEXT_TREE;
628 if (tc.selected_item < 0)
629 tc.selected_item = 0;
630 #ifdef HAVE_TAGCACHE
631 tc.firstpos = 0;
632 lasttable = -1;
633 lastextra = -1;
634 lastfirstpos = 0;
635 #endif
637 start_wps = false;
638 numentries = update_dir();
639 reload_dir = false;
640 if (numentries == -1)
641 return GO_TO_PREVIOUS; /* currdir is not a directory */
643 if (*tc.dirfilter > NUM_FILTER_MODES && numentries==0)
645 splash(HZ*2, ID2P(LANG_NO_FILES));
646 return GO_TO_PREVIOUS; /* No files found for rockbox_browse() */
649 gui_synclist_draw(&tree_lists);
650 while(1) {
651 bool restore = false;
652 if (tc.dirlevel < 0)
653 tc.dirlevel = 0; /* shouldnt be needed.. this code needs work! */
655 button = get_action(CONTEXT_TREE,
656 list_do_action_timeout(&tree_lists, HZ/2));
657 #ifdef HAVE_LCD_BITMAP
658 oldbutton = button;
659 #endif
660 gui_synclist_do_button(&tree_lists, &button,LIST_WRAP_UNLESS_HELD);
661 tc.selected_item = gui_synclist_get_sel_pos(&tree_lists);
662 switch ( button ) {
663 case ACTION_STD_OK:
664 /* nothing to do if no files to display */
665 if ( numentries == 0 )
666 break;
668 short attr = tree_get_entry_at(&tc, tc.selected_item)->attr;
669 if ((tc.browse->flags & BROWSE_SELECTONLY) &&
670 !(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 struct entry *entry = tree_get_entry_at(&tc, tc.selected_item);
796 attr = entry->attr;
798 if (currdir[1]) /* Not in / */
799 snprintf(buf, sizeof buf, "%s/%s",
800 currdir, entry->name);
801 else /* In / */
802 snprintf(buf, sizeof buf, "/%s", entry->name);
804 onplay_result = onplay(buf, attr, curr_context, hotkey);
806 switch (onplay_result)
808 case ONPLAY_MAINMENU:
809 return GO_TO_ROOT;
811 case ONPLAY_OK:
812 restore = true;
813 break;
815 case ONPLAY_RELOAD_DIR:
816 reload_dir = true;
817 break;
819 case ONPLAY_START_PLAY:
820 return GO_TO_WPS;
821 break;
823 break;
826 #ifdef HAVE_HOTSWAP
827 case SYS_FS_CHANGED:
828 #ifdef HAVE_TAGCACHE
829 if (!id3db)
830 #endif
831 reload_dir = true;
832 /* The 'dir no longer valid' situation will be caught later
833 * by checking the showdir() result. */
834 break;
835 #endif
837 default:
838 if (default_event_handler(button) == SYS_USB_CONNECTED)
840 if(*tc.dirfilter > NUM_FILTER_MODES)
841 /* leave sub-browsers after usb, doing otherwise
842 might be confusing to the user */
843 exit_func = true;
844 else
845 reload_dir = true;
847 break;
849 if (start_wps)
850 return GO_TO_WPS;
851 if (button && !IS_SYSEVENT(button))
853 storage_spin();
857 check_rescan:
858 /* do we need to rescan dir? */
859 if (reload_dir || reload_root ||
860 lastfilter != *tc.dirfilter ||
861 lastsortcase != global_settings.sort_case)
863 if (reload_root) {
864 strcpy(currdir, "/");
865 tc.dirlevel = 0;
866 #ifdef HAVE_TAGCACHE
867 tc.currtable = 0;
868 tc.currextra = 0;
869 lasttable = -1;
870 lastextra = -1;
871 #endif
872 reload_root = false;
875 if (!reload_dir)
877 gui_synclist_select_item(&tree_lists, 0);
878 gui_synclist_draw(&tree_lists);
879 tc.selected_item = 0;
880 lastdir[0] = 0;
883 lastfilter = *tc.dirfilter;
884 lastsortcase = global_settings.sort_case;
885 restore = true;
888 if (exit_func)
889 return GO_TO_PREVIOUS;
891 if (restore || reload_dir) {
892 /* restore display */
893 numentries = update_dir();
894 reload_dir = false;
895 if (currdir[1] && (numentries < 0))
896 { /* not in root and reload failed */
897 reload_root = true; /* try root */
898 goto check_rescan;
902 return true;
905 bool create_playlist(void)
907 char filename[MAX_PATH];
909 if (tc.currdir[1])
910 snprintf(filename, sizeof filename, "%s.m3u8", tc.currdir);
911 else
912 snprintf(filename, sizeof filename, "%s/all.m3u8",
913 catalog_get_directory());
916 if (kbd_input(filename, MAX_PATH))
917 return false;
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 void browse_context_init(struct browse_context *browse,
928 int dirfilter, unsigned flags,
929 char *title, enum themable_icons icon,
930 const char *root, const char *selected)
932 browse->dirfilter = dirfilter;
933 browse->flags = flags;
934 browse->callback_show_item = NULL;
935 browse->title = title;
936 browse->icon = icon;
937 browse->root = root;
938 browse->selected = selected;
939 browse->buf = NULL;
940 browse->bufsize = 0;
943 #define NUM_TC_BACKUP 3
944 static struct tree_context backups[NUM_TC_BACKUP];
945 /* do not make backup if it is not recursive call */
946 static int backup_count = -1;
947 int rockbox_browse(struct browse_context *browse)
949 static char current[MAX_PATH];
950 int ret_val = 0;
951 int dirfilter = browse->dirfilter;
953 if (backup_count >= NUM_TC_BACKUP)
954 return GO_TO_PREVIOUS;
955 if (backup_count >= 0)
956 backups[backup_count] = tc;
957 backup_count++;
959 tc.dirfilter = &dirfilter;
960 tc.sort_dir = global_settings.sort_dir;
962 reload_dir = true;
963 if (*tc.dirfilter >= NUM_FILTER_MODES)
965 int last_context;
967 tc.browse = browse;
968 tc.selected_item = 0;
969 tc.dirlevel = 0;
970 strlcpy(tc.currdir, browse->root, sizeof(tc.currdir));
971 start_wps = false;
972 last_context = curr_context;
974 if (browse->selected)
976 snprintf(current, sizeof(current), "%s/%s",
977 browse->root, browse->selected);
978 set_current_file(current);
979 /* set_current_file changes dirlevel, change it back */
980 tc.dirlevel = 0;
983 ret_val = dirbrowse();
984 curr_context = last_context;
986 else
988 if (dirfilter != SHOW_ID3DB)
989 tc.dirfilter = &global_settings.dirfilter;
990 tc.browse = browse;
991 strcpy(current, browse->root);
992 set_current_file(current);
993 if (browse->flags&BROWSE_RUNFILE)
994 ret_val = ft_enter(&tc);
995 else
996 ret_val = dirbrowse();
998 backup_count--;
999 if (backup_count >= 0)
1000 tc = backups[backup_count];
1001 return ret_val;
1004 static int move_callback(int handle, void* current, void* new)
1006 struct tree_cache* cache = &tc.cache;
1007 if (cache->lock_count > 0)
1008 return BUFLIB_CB_CANNOT_MOVE;
1010 size_t diff = new - current;
1011 /* FIX_PTR makes sure to not accidentally update static allocations */
1012 #define FIX_PTR(x) \
1013 { if ((void*)x >= current && (void*)x < (current+cache->name_buffer_size)) x+= diff; }
1015 if (handle == cache->name_buffer_handle)
1016 { /* update entry structs, *even if they are struct tagentry */
1017 struct entry *this = core_get_data(cache->entries_handle);
1018 struct entry *last = this + cache->max_entries;
1019 for(; this < last; this++)
1020 FIX_PTR(this->name);
1022 /* nothing to do if entries moved */
1023 return BUFLIB_CB_OK;
1026 static struct buflib_callbacks ops = {
1027 .move_callback = move_callback,
1028 .shrink_callback = NULL,
1031 void tree_mem_init(void)
1033 /* initialize tree context struct */
1034 struct tree_cache* cache = &tc.cache;
1035 memset(&tc, 0, sizeof(tc));
1036 tc.dirfilter = &global_settings.dirfilter;
1037 tc.sort_dir = global_settings.sort_dir;
1039 cache->name_buffer_size = AVERAGE_FILENAME_LENGTH *
1040 global_settings.max_files_in_dir;
1041 cache->name_buffer_handle = core_alloc_ex("tree names",
1042 cache->name_buffer_size,
1043 &ops);
1045 cache->max_entries = global_settings.max_files_in_dir;
1046 cache->entries_handle = core_alloc_ex("tree entries",
1047 cache->max_entries*(sizeof(struct entry)),
1048 &ops);
1049 tree_get_filetypes(&filetypes, &filetypes_count);
1052 bool bookmark_play(char *resume_file, int index, int offset, int seed,
1053 char *filename)
1055 int i;
1056 char* suffix = strrchr(resume_file, '.');
1057 bool started = false;
1059 if (suffix != NULL &&
1060 (!strcasecmp(suffix, ".m3u") || !strcasecmp(suffix, ".m3u8")))
1062 /* Playlist playback */
1063 char* slash;
1064 /* check that the file exists */
1065 if(!file_exists(resume_file))
1066 return false;
1068 slash = strrchr(resume_file,'/');
1069 if (slash)
1071 char* cp;
1072 *slash=0;
1074 cp=resume_file;
1075 if (!cp[0])
1076 cp="/";
1078 if (playlist_create(cp, slash+1) != -1)
1080 if (global_settings.playlist_shuffle)
1081 playlist_shuffle(seed, -1);
1082 playlist_start(index,offset);
1083 started = true;
1085 *slash='/';
1088 else
1090 /* Directory playback */
1091 lastdir[0]='\0';
1092 if (playlist_create(resume_file, NULL) != -1)
1094 char filename_buf[MAX_PATH + 1];
1095 const char* peek_filename;
1096 resume_directory(resume_file);
1097 if (global_settings.playlist_shuffle)
1098 playlist_shuffle(seed, -1);
1100 /* Check if the file is at the same spot in the directory,
1101 else search for it */
1102 peek_filename = playlist_peek(index, filename_buf,
1103 sizeof(filename_buf));
1105 if (peek_filename == NULL)
1107 /* playlist has shrunk, search from the top */
1108 index = 0;
1109 peek_filename = playlist_peek(index, filename_buf,
1110 sizeof(filename_buf));
1111 if (peek_filename == NULL)
1112 return false;
1115 if (strcmp(strrchr(peek_filename, '/') + 1, filename))
1117 for ( i=0; i < playlist_amount(); i++ )
1119 peek_filename = playlist_peek(i, filename_buf,
1120 sizeof(filename_buf));
1122 if (peek_filename == NULL)
1123 return false;
1125 if (!strcmp(strrchr(peek_filename, '/') + 1, filename))
1126 break;
1128 if (i < playlist_amount())
1129 index = i;
1130 else
1131 return false;
1133 playlist_start(index,offset);
1134 started = true;
1138 if (started)
1139 start_wps = true;
1140 return started;
1143 static void say_filetype(int attr)
1145 /* try to find a voice ID for the extension, if known */
1146 int j;
1147 attr &= FILE_ATTR_MASK; /* file type */
1148 for (j=0; j<filetypes_count; j++)
1149 if (attr == filetypes[j].tree_attr)
1151 talk_id(filetypes[j].voiceclip, true);
1152 return;
1156 static int ft_play_dirname(char* name)
1158 #if CONFIG_CODEC != SWCODEC
1159 if (audio_status() & AUDIO_STATUS_PLAY)
1160 return 0;
1161 #endif
1163 if(talk_file(tc.currdir, name, dir_thumbnail_name, NULL,
1164 NULL, false))
1166 if(global_settings.talk_filetype)
1167 talk_id(VOICE_DIR, true);
1168 return 1;
1170 else
1171 return -1;
1174 static void ft_play_filename(char *dir, char *file)
1176 #if CONFIG_CODEC != SWCODEC
1177 if (audio_status() & AUDIO_STATUS_PLAY)
1178 return;
1179 #endif
1181 if (strlen(file) >= strlen(file_thumbnail_ext)
1182 && strcasecmp(&file[strlen(file) - strlen(file_thumbnail_ext)],
1183 file_thumbnail_ext))
1184 /* file has no .talk extension */
1185 talk_file(dir, NULL, file, file_thumbnail_ext,
1186 NULL, false);
1187 else
1188 /* it already is a .talk file, play this directly, but prefix it. */
1189 talk_file(dir, NULL, file, NULL,
1190 TALK_IDARRAY(LANG_VOICE_DIR_HOVER), false);
1193 /* These two functions are called by the USB and shutdown handlers */
1194 void tree_flush(void)
1196 #ifdef HAVE_TAGCACHE
1197 tagcache_shutdown();
1198 #endif
1200 #ifdef HAVE_TC_RAMCACHE
1201 tagcache_unload_ramcache();
1202 #endif
1204 #ifdef HAVE_DIRCACHE
1206 int old_val = global_status.dircache_size;
1207 if (global_settings.dircache)
1209 if (!dircache_is_initializing())
1210 global_status.dircache_size = dircache_get_cache_size();
1211 # ifdef HAVE_EEPROM_SETTINGS
1212 if (firmware_settings.initialized)
1213 dircache_save();
1214 # endif
1215 dircache_suspend();
1217 else
1219 global_status.dircache_size = 0;
1221 if (old_val != global_status.dircache_size)
1222 status_save();
1224 #endif
1227 void tree_restore(void)
1229 #ifdef HAVE_EEPROM_SETTINGS
1230 firmware_settings.disk_clean = false;
1231 #endif
1233 #ifdef HAVE_TC_RAMCACHE
1234 remove(TAGCACHE_STATEFILE);
1235 #endif
1237 #ifdef HAVE_DIRCACHE
1238 remove(DIRCACHE_FILE);
1239 if (global_settings.dircache)
1241 /* Print "Scanning disk..." to the display. */
1242 splash(0, str(LANG_SCANNING_DISK));
1244 dircache_build(global_status.dircache_size);
1246 #endif
1247 #ifdef HAVE_TAGCACHE
1248 tagcache_start_scan();
1249 #endif