Fix warning and typos.
[maemo-rb.git] / apps / tree.c
blobeb7783d4ee0f744f3fc467fca058ef1c662a8566
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 /* drawer function for the GUI_EVENT_REDRAW callback */
311 void tree_drawlists(void)
313 /* band-aid to fix the bar/list redrawing properly after leaving a plugin */
314 send_event(GUI_EVENT_THEME_CHANGED, NULL);
315 /* end bandaid */
316 gui_synclist_draw(&tree_lists);
320 struct tree_context* tree_get_context(void)
322 return &tc;
326 * Returns the position of a given file in the current directory
327 * returns -1 if not found
329 static int tree_get_file_position(char * filename)
331 int i;
332 struct entry* e;
334 /* use lastfile to determine the selected item (default=0) */
335 for (i=0; i < tc.filesindir; i++)
337 e = tree_get_entry_at(&tc, i);
338 if (!strcasecmp(e->name, filename))
339 return(i);
341 return(-1);/* no file can match, returns undefined */
345 * Called when a new dir is loaded (for example when returning from other apps ...)
346 * also completely redraws the tree
348 static int update_dir(void)
350 bool changed = false;
351 #ifdef HAVE_TAGCACHE
352 bool id3db = *tc.dirfilter == SHOW_ID3DB;
353 /* Checks for changes */
354 if (id3db) {
355 if (tc.currtable != lasttable ||
356 tc.currextra != lastextra ||
357 tc.firstpos != lastfirstpos ||
358 reload_dir)
360 if (tagtree_load(&tc) < 0)
361 return -1;
363 lasttable = tc.currtable;
364 lastextra = tc.currextra;
365 lastfirstpos = tc.firstpos;
366 changed = true;
369 else
370 #endif
372 /* if the tc.currdir has been changed, reload it ...*/
373 if (strncmp(tc.currdir, lastdir, sizeof(lastdir)) || reload_dir)
375 if (ft_load(&tc, NULL) < 0)
376 return -1;
377 strcpy(lastdir, tc.currdir);
378 changed = true;
381 /* if selected item is undefined */
382 if (tc.selected_item == -1)
384 /* use lastfile to determine the selected item */
385 tc.selected_item = tree_get_file_position(lastfile);
387 /* If the file doesn't exists, select the first one (default) */
388 if(tc.selected_item < 0)
389 tc.selected_item = 0;
390 changed = true;
392 if (changed)
395 #ifdef HAVE_TAGCACHE
396 !id3db &&
397 #endif
398 tc.dirfull )
400 splash(HZ, ID2P(LANG_SHOWDIR_BUFFER_FULL));
403 #ifdef HAVE_TAGCACHE
404 if (id3db)
406 #ifdef HAVE_LCD_BITMAP
407 if (global_settings.show_path_in_browser == SHOW_PATH_FULL
408 || global_settings.show_path_in_browser == SHOW_PATH_CURRENT)
410 gui_synclist_set_title(&tree_lists, tagtree_get_title(&tc),
411 filetype_get_icon(ATTR_DIRECTORY));
413 else
415 /* Must clear the title as the list is reused */
416 gui_synclist_set_title(&tree_lists, NULL, NOICON);
418 #endif
420 else
421 #endif
423 #ifdef HAVE_LCD_BITMAP
424 if (tc.browse && tc.browse->title)
426 int icon = tc.browse->icon;
427 if (icon == NOICON)
428 icon = filetype_get_icon(ATTR_DIRECTORY);
429 gui_synclist_set_title(&tree_lists, tc.browse->title, icon);
431 else if (global_settings.show_path_in_browser == SHOW_PATH_FULL)
433 gui_synclist_set_title(&tree_lists, tc.currdir,
434 filetype_get_icon(ATTR_DIRECTORY));
436 else if (global_settings.show_path_in_browser == SHOW_PATH_CURRENT)
438 char *title = strrchr(tc.currdir, '/') + 1;
439 if (*title == '\0')
441 /* Display "Files" for the root dir */
442 gui_synclist_set_title(&tree_lists, str(LANG_DIR_BROWSER),
443 filetype_get_icon(ATTR_DIRECTORY));
445 else
446 gui_synclist_set_title(&tree_lists, title,
447 filetype_get_icon(ATTR_DIRECTORY));
449 else
451 /* Must clear the title as the list is reused */
452 gui_synclist_set_title(&tree_lists, NULL, NOICON);
454 #endif
457 gui_synclist_set_nb_items(&tree_lists, tc.filesindir);
458 gui_synclist_set_icon_callback(&tree_lists,
459 global_settings.show_icons?tree_get_fileicon:NULL);
460 if( tc.selected_item >= tc.filesindir)
461 tc.selected_item=tc.filesindir-1;
463 gui_synclist_select_item(&tree_lists, tc.selected_item);
464 #ifdef HAVE_BUTTONBAR
465 if (global_settings.buttonbar) {
466 if (*tc.dirfilter < NUM_FILTER_MODES)
467 gui_buttonbar_set(&tree_buttonbar, str(LANG_SYSFONT_DIRBROWSE_F1),
468 str(LANG_SYSFONT_DIRBROWSE_F2),
469 str(LANG_SYSFONT_DIRBROWSE_F3));
470 else
471 gui_buttonbar_set(&tree_buttonbar, "<<<", "", "");
472 gui_buttonbar_draw(&tree_buttonbar);
474 #endif
475 gui_synclist_draw(&tree_lists);
476 gui_synclist_speak_item(&tree_lists);
477 return tc.filesindir;
480 /* load tracks from specified directory to resume play */
481 void resume_directory(const char *dir)
483 int dirfilter = *tc.dirfilter;
484 int ret;
485 #ifdef HAVE_TAGCACHE
486 bool id3db = *tc.dirfilter == SHOW_ID3DB;
487 #endif
488 /* make sure the dirfilter is sane. The only time it should be possible
489 * thats its not is when resume playlist is called from a plugin
491 #ifdef HAVE_TAGCACHE
492 if (!id3db)
493 #endif
494 *tc.dirfilter = global_settings.dirfilter;
495 ret = ft_load(&tc, dir);
496 *tc.dirfilter = dirfilter;
497 if (ret < 0)
498 return;
499 lastdir[0] = 0;
501 ft_build_playlist(&tc, 0);
503 #ifdef HAVE_TAGCACHE
504 if (id3db)
505 tagtree_load(&tc);
506 #endif
509 /* Returns the current working directory and also writes cwd to buf if
510 non-NULL. In case of error, returns NULL. */
511 char *getcwd(char *buf, getcwd_size_t size)
513 if (!buf)
514 return tc.currdir;
515 else if (size)
517 if ((getcwd_size_t)strlcpy(buf, tc.currdir, size) < size)
518 return buf;
520 /* size == 0, or truncation in strlcpy */
521 return NULL;
524 /* Force a reload of the directory next time directory browser is called */
525 void reload_directory(void)
527 reload_dir = true;
530 char* get_current_file(char* buffer, size_t buffer_len)
532 #ifdef HAVE_TAGCACHE
533 /* in ID3DB mode it is a bad idea to call this function */
534 /* (only happens with `follow playlist') */
535 if( *tc.dirfilter == SHOW_ID3DB )
536 return NULL;
537 #endif
539 struct entry* e = tree_get_entry_at(&tc, tc.selected_item);
540 if (getcwd(buffer, buffer_len))
542 if (tc.dirlength)
544 if (buffer[strlen(buffer)-1] != '/')
545 strlcat(buffer, "/", buffer_len);
546 if (strlcat(buffer, e->name, buffer_len) >= buffer_len)
547 return NULL;
549 return buffer;
551 return NULL;
554 /* Allow apps to change our dirfilter directly (required for sub browsers)
555 if they're suddenly going to become a file browser for example */
556 void set_dirfilter(int l_dirfilter)
558 *tc.dirfilter = l_dirfilter;
561 /* Selects a file and update tree context properly */
562 void set_current_file(const char *path)
564 const char *name;
565 int i;
567 #ifdef HAVE_TAGCACHE
568 /* in ID3DB mode it is a bad idea to call this function */
569 /* (only happens with `follow playlist') */
570 if( *tc.dirfilter == SHOW_ID3DB )
571 return;
572 #endif
574 /* separate directory from filename */
575 /* gets the directory's name and put it into tc.currdir */
576 name = strrchr(path+1,'/');
577 if (name)
579 strlcpy(tc.currdir, path, name - path + 1);
580 name++;
582 else
584 strcpy(tc.currdir, "/");
585 name = path+1;
588 strcpy(lastfile, name);
591 /* If we changed dir we must recalculate the dirlevel
592 and adjust the selected history properly */
593 if (strncmp(tc.currdir,lastdir,sizeof(lastdir)))
595 tc.dirlevel = 0;
596 tc.selected_item_history[tc.dirlevel] = -1;
598 /* use '/' to calculate dirlevel */
599 for (i = 1; path[i] != '\0'; i++)
601 if (path[i] == '/')
603 tc.dirlevel++;
604 tc.selected_item_history[tc.dirlevel] = -1;
608 if (ft_load(&tc, NULL) >= 0)
610 tc.selected_item = tree_get_file_position(lastfile);
615 /* main loop, handles key events */
616 static int dirbrowse(void)
618 int numentries=0;
619 char buf[MAX_PATH];
620 int button;
621 #ifdef HAVE_LCD_BITMAP
622 int oldbutton;
623 #endif
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_browse() */
659 gui_synclist_draw(&tree_lists);
660 while(1) {
661 bool restore = false;
662 if (tc.dirlevel < 0)
663 tc.dirlevel = 0; /* shouldnt be needed.. this code needs work! */
665 button = get_action(CONTEXT_TREE,
666 list_do_action_timeout(&tree_lists, HZ/2));
667 #ifdef HAVE_LCD_BITMAP
668 oldbutton = button;
669 #endif
670 gui_synclist_do_button(&tree_lists, &button,LIST_WRAP_UNLESS_HELD);
671 tc.selected_item = gui_synclist_get_sel_pos(&tree_lists);
672 switch ( button ) {
673 case ACTION_STD_OK:
674 /* nothing to do if no files to display */
675 if ( numentries == 0 )
676 break;
678 short attr = tree_get_entry_at(&tc, tc.selected_item)->attr;
679 if ((tc.browse->flags & BROWSE_SELECTONLY) &&
680 !(attr & ATTR_DIRECTORY))
682 tc.browse->flags |= BROWSE_SELECTED;
683 get_current_file(tc.browse->buf, tc.browse->bufsize);
684 return GO_TO_PREVIOUS;
687 #ifdef HAVE_TAGCACHE
688 switch (id3db?tagtree_enter(&tc):ft_enter(&tc))
689 #else
690 switch (ft_enter(&tc))
691 #endif
693 case GO_TO_FILEBROWSER: reload_dir = true; break;
694 case GO_TO_WPS:
695 return GO_TO_WPS;
696 #if CONFIG_TUNER
697 case GO_TO_FM:
698 return GO_TO_FM;
699 #endif
700 case GO_TO_ROOT: exit_func = true; break;
701 default: break;
703 restore = true;
704 break;
706 case ACTION_STD_CANCEL:
707 if (*tc.dirfilter > NUM_FILTER_MODES && tc.dirlevel < 1) {
708 exit_func = true;
709 break;
711 if ((*tc.dirfilter == SHOW_ID3DB && tc.dirlevel == 0) ||
712 ((*tc.dirfilter != SHOW_ID3DB && !strcmp(currdir,"/"))))
714 #ifdef HAVE_LCD_BITMAP /* charcell doesnt have ACTION_TREE_PGLEFT so this isnt needed */
715 if (oldbutton == ACTION_TREE_PGLEFT)
716 break;
717 else
718 #endif
719 return GO_TO_ROOT;
722 #ifdef HAVE_TAGCACHE
723 if (id3db)
724 tagtree_exit(&tc);
725 else
726 #endif
727 if (ft_exit(&tc) == 3)
728 exit_func = true;
730 restore = true;
731 break;
733 case ACTION_TREE_STOP:
734 if (list_stop_handler())
735 restore = true;
736 break;
738 case ACTION_STD_MENU:
739 return GO_TO_ROOT;
740 break;
742 #ifdef HAVE_RECORDING
743 case ACTION_STD_REC:
744 return GO_TO_RECSCREEN;
745 #endif
747 case ACTION_TREE_WPS:
748 return GO_TO_PREVIOUS_MUSIC;
749 break;
750 #ifdef HAVE_QUICKSCREEN
751 case ACTION_STD_QUICKSCREEN:
752 /* don't enter f2 from plugin browser */
753 if (*tc.dirfilter < NUM_FILTER_MODES)
755 if (quick_screen_quick(button))
756 reload_dir = true;
757 restore = true;
759 break;
760 #endif
761 #ifdef BUTTON_F3
762 case ACTION_F3:
763 /* don't enter f3 from plugin browser */
764 if (*tc.dirfilter < NUM_FILTER_MODES)
766 if (quick_screen_f3(ACTION_F3))
767 reload_dir = true;
768 restore = true;
770 break;
771 #endif
773 #ifdef HAVE_HOTKEY
774 case ACTION_TREE_HOTKEY:
775 if (!global_settings.hotkey_tree)
776 break;
777 /* fall through */
778 #endif
779 case ACTION_STD_CONTEXT:
781 bool hotkey = button == ACTION_TREE_HOTKEY;
782 int onplay_result;
783 int attr = 0;
785 if (tc.browse->flags & BROWSE_NO_CONTEXT_MENU)
786 break;
788 if(!numentries)
789 onplay_result = onplay(NULL, 0, curr_context, hotkey);
790 else {
791 #ifdef HAVE_TAGCACHE
792 if (id3db)
794 if (tagtree_get_attr(&tc) == FILE_ATTR_AUDIO)
796 attr = FILE_ATTR_AUDIO;
797 tagtree_get_filename(&tc, buf, sizeof(buf));
799 else
800 attr = ATTR_DIRECTORY;
802 else
803 #endif
805 struct entry *entry = tree_get_entry_at(&tc, tc.selected_item);
806 attr = entry->attr;
808 if (currdir[1]) /* Not in / */
809 snprintf(buf, sizeof buf, "%s/%s",
810 currdir, entry->name);
811 else /* In / */
812 snprintf(buf, sizeof buf, "/%s", entry->name);
814 onplay_result = onplay(buf, attr, curr_context, hotkey);
816 switch (onplay_result)
818 case ONPLAY_MAINMENU:
819 return GO_TO_ROOT;
821 case ONPLAY_OK:
822 restore = true;
823 break;
825 case ONPLAY_RELOAD_DIR:
826 reload_dir = true;
827 break;
829 case ONPLAY_START_PLAY:
830 return GO_TO_WPS;
831 break;
833 break;
836 #ifdef HAVE_HOTSWAP
837 case SYS_FS_CHANGED:
838 #ifdef HAVE_TAGCACHE
839 if (!id3db)
840 #endif
841 reload_dir = true;
842 /* The 'dir no longer valid' situation will be caught later
843 * by checking the showdir() result. */
844 break;
845 #endif
847 default:
848 if (default_event_handler(button) == SYS_USB_CONNECTED)
850 if(*tc.dirfilter > NUM_FILTER_MODES)
851 /* leave sub-browsers after usb, doing otherwise
852 might be confusing to the user */
853 exit_func = true;
854 else
855 reload_dir = true;
857 break;
859 if (start_wps)
860 return GO_TO_WPS;
861 if (button && !IS_SYSEVENT(button))
863 storage_spin();
867 check_rescan:
868 /* do we need to rescan dir? */
869 if (reload_dir || reload_root ||
870 lastfilter != *tc.dirfilter ||
871 lastsortcase != global_settings.sort_case)
873 if (reload_root) {
874 strcpy(currdir, "/");
875 tc.dirlevel = 0;
876 #ifdef HAVE_TAGCACHE
877 tc.currtable = 0;
878 tc.currextra = 0;
879 lasttable = -1;
880 lastextra = -1;
881 #endif
882 reload_root = false;
885 if (!reload_dir)
887 gui_synclist_select_item(&tree_lists, 0);
888 gui_synclist_draw(&tree_lists);
889 tc.selected_item = 0;
890 lastdir[0] = 0;
893 lastfilter = *tc.dirfilter;
894 lastsortcase = global_settings.sort_case;
895 restore = true;
898 if (exit_func)
899 return GO_TO_PREVIOUS;
901 if (restore || reload_dir) {
902 /* restore display */
903 numentries = update_dir();
904 reload_dir = false;
905 if (currdir[1] && (numentries < 0))
906 { /* not in root and reload failed */
907 reload_root = true; /* try root */
908 goto check_rescan;
912 return true;
915 bool create_playlist(void)
917 char filename[MAX_PATH];
919 if (tc.currdir[1])
920 snprintf(filename, sizeof filename, "%s.m3u8", tc.currdir);
921 else
922 snprintf(filename, sizeof filename, "%s/all.m3u8",
923 catalog_get_directory());
926 if (kbd_input(filename, MAX_PATH))
927 return false;
928 splashf(0, "%s %s", str(LANG_CREATING), filename);
930 trigger_cpu_boost();
931 catalog_add_to_a_playlist(tc.currdir, ATTR_DIRECTORY, true, filename);
932 cancel_cpu_boost();
934 return true;
937 void browse_context_init(struct browse_context *browse,
938 int dirfilter, unsigned flags,
939 char *title, enum themable_icons icon,
940 const char *root, const char *selected)
942 browse->dirfilter = dirfilter;
943 browse->flags = flags;
944 browse->callback_show_item = NULL;
945 browse->title = title;
946 browse->icon = icon;
947 browse->root = root;
948 browse->selected = selected;
949 browse->buf = NULL;
950 browse->bufsize = 0;
953 #define NUM_TC_BACKUP 3
954 static struct tree_context backups[NUM_TC_BACKUP];
955 /* do not make backup if it is not recursive call */
956 static int backup_count = -1;
957 int rockbox_browse(struct browse_context *browse)
959 static char current[MAX_PATH];
960 int ret_val = 0;
961 int dirfilter = browse->dirfilter;
963 if (backup_count >= NUM_TC_BACKUP)
964 return GO_TO_PREVIOUS;
965 if (backup_count >= 0)
966 backups[backup_count] = tc;
967 backup_count++;
969 tc.dirfilter = &dirfilter;
970 tc.sort_dir = global_settings.sort_dir;
972 reload_dir = true;
973 if (*tc.dirfilter >= NUM_FILTER_MODES)
975 int last_context;
977 tc.browse = browse;
978 tc.selected_item = 0;
979 tc.dirlevel = 0;
980 strlcpy(tc.currdir, browse->root, sizeof(tc.currdir));
981 start_wps = false;
982 last_context = curr_context;
984 if (browse->selected)
986 snprintf(current, sizeof(current), "%s/%s",
987 browse->root, browse->selected);
988 set_current_file(current);
989 /* set_current_file changes dirlevel, change it back */
990 tc.dirlevel = 0;
993 ret_val = dirbrowse();
994 curr_context = last_context;
996 else
998 if (dirfilter != SHOW_ID3DB)
999 tc.dirfilter = &global_settings.dirfilter;
1000 tc.browse = browse;
1001 strcpy(current, browse->root);
1002 set_current_file(current);
1003 ret_val = dirbrowse();
1005 backup_count--;
1006 if (backup_count >= 0)
1007 tc = backups[backup_count];
1008 return ret_val;
1011 static int move_callback(int handle, void* current, void* new)
1013 struct tree_cache* cache = &tc.cache;
1014 if (cache->lock_count > 0)
1015 return BUFLIB_CB_CANNOT_MOVE;
1017 size_t diff = new - current;
1018 /* FIX_PTR makes sure to not accidentally update static allocations */
1019 #define FIX_PTR(x) \
1020 { if ((void*)x >= current && (void*)x < (current+cache->name_buffer_size)) x+= diff; }
1022 if (handle == cache->name_buffer_handle)
1023 { /* update entry structs, *even if they are struct tagentry */
1024 struct entry *this = core_get_data(cache->entries_handle);
1025 struct entry *last = this + cache->max_entries;
1026 for(; this < last; this++)
1027 FIX_PTR(this->name);
1029 /* nothing to do if entries moved */
1030 return BUFLIB_CB_OK;
1033 static struct buflib_callbacks ops = {
1034 .move_callback = move_callback,
1035 .shrink_callback = NULL,
1038 void tree_mem_init(void)
1040 /* initialize tree context struct */
1041 struct tree_cache* cache = &tc.cache;
1042 memset(&tc, 0, sizeof(tc));
1043 tc.dirfilter = &global_settings.dirfilter;
1044 tc.sort_dir = global_settings.sort_dir;
1046 cache->name_buffer_size = AVERAGE_FILENAME_LENGTH *
1047 global_settings.max_files_in_dir;
1048 cache->name_buffer_handle = core_alloc_ex("tree names",
1049 cache->name_buffer_size,
1050 &ops);
1052 cache->max_entries = global_settings.max_files_in_dir;
1053 cache->entries_handle = core_alloc_ex("tree entries",
1054 cache->max_entries*(sizeof(struct entry)),
1055 &ops);
1056 tree_get_filetypes(&filetypes, &filetypes_count);
1059 bool bookmark_play(char *resume_file, int index, int offset, int seed,
1060 char *filename)
1062 int i;
1063 char* suffix = strrchr(resume_file, '.');
1064 bool started = false;
1066 if (suffix != NULL &&
1067 (!strcasecmp(suffix, ".m3u") || !strcasecmp(suffix, ".m3u8")))
1069 /* Playlist playback */
1070 char* slash;
1071 /* check that the file exists */
1072 if(!file_exists(resume_file))
1073 return false;
1075 slash = strrchr(resume_file,'/');
1076 if (slash)
1078 char* cp;
1079 *slash=0;
1081 cp=resume_file;
1082 if (!cp[0])
1083 cp="/";
1085 if (playlist_create(cp, slash+1) != -1)
1087 if (global_settings.playlist_shuffle)
1088 playlist_shuffle(seed, -1);
1089 playlist_start(index,offset);
1090 started = true;
1092 *slash='/';
1095 else
1097 /* Directory playback */
1098 lastdir[0]='\0';
1099 if (playlist_create(resume_file, NULL) != -1)
1101 char filename_buf[MAX_PATH + 1];
1102 const char* peek_filename;
1103 resume_directory(resume_file);
1104 if (global_settings.playlist_shuffle)
1105 playlist_shuffle(seed, -1);
1107 /* Check if the file is at the same spot in the directory,
1108 else search for it */
1109 peek_filename = playlist_peek(index, filename_buf,
1110 sizeof(filename_buf));
1112 if (peek_filename == NULL)
1114 /* playlist has shrunk, search from the top */
1115 index = 0;
1116 peek_filename = playlist_peek(index, filename_buf,
1117 sizeof(filename_buf));
1118 if (peek_filename == NULL)
1119 return false;
1122 if (strcmp(strrchr(peek_filename, '/') + 1, filename))
1124 for ( i=0; i < playlist_amount(); i++ )
1126 peek_filename = playlist_peek(i, filename_buf,
1127 sizeof(filename_buf));
1129 if (peek_filename == NULL)
1130 return false;
1132 if (!strcmp(strrchr(peek_filename, '/') + 1, filename))
1133 break;
1135 if (i < playlist_amount())
1136 index = i;
1137 else
1138 return false;
1140 playlist_start(index,offset);
1141 started = true;
1145 if (started)
1146 start_wps = true;
1147 return started;
1150 static void say_filetype(int attr)
1152 /* try to find a voice ID for the extension, if known */
1153 int j;
1154 attr &= FILE_ATTR_MASK; /* file type */
1155 for (j=0; j<filetypes_count; j++)
1156 if (attr == filetypes[j].tree_attr)
1158 talk_id(filetypes[j].voiceclip, true);
1159 return;
1163 static int ft_play_dirname(char* name)
1165 #if CONFIG_CODEC != SWCODEC
1166 if (audio_status() & AUDIO_STATUS_PLAY)
1167 return 0;
1168 #endif
1170 if(talk_file(tc.currdir, name, dir_thumbnail_name, NULL,
1171 NULL, false))
1173 if(global_settings.talk_filetype)
1174 talk_id(VOICE_DIR, true);
1175 return 1;
1177 else
1178 return -1;
1181 static void ft_play_filename(char *dir, char *file)
1183 #if CONFIG_CODEC != SWCODEC
1184 if (audio_status() & AUDIO_STATUS_PLAY)
1185 return;
1186 #endif
1188 if (strlen(file) >= strlen(file_thumbnail_ext)
1189 && strcasecmp(&file[strlen(file) - strlen(file_thumbnail_ext)],
1190 file_thumbnail_ext))
1191 /* file has no .talk extension */
1192 talk_file(dir, NULL, file, file_thumbnail_ext,
1193 NULL, false);
1194 else
1195 /* it already is a .talk file, play this directly, but prefix it. */
1196 talk_file(dir, NULL, file, NULL,
1197 TALK_IDARRAY(LANG_VOICE_DIR_HOVER), false);
1200 /* These two functions are called by the USB and shutdown handlers */
1201 void tree_flush(void)
1203 #ifdef HAVE_TAGCACHE
1204 tagcache_shutdown();
1205 #endif
1207 #ifdef HAVE_TC_RAMCACHE
1208 tagcache_unload_ramcache();
1209 #endif
1211 #ifdef HAVE_DIRCACHE
1213 int old_val = global_status.dircache_size;
1214 if (global_settings.dircache)
1216 if (!dircache_is_initializing())
1217 global_status.dircache_size = dircache_get_cache_size();
1218 # ifdef HAVE_EEPROM_SETTINGS
1219 if (firmware_settings.initialized)
1220 dircache_save();
1221 # endif
1222 dircache_suspend();
1224 else
1226 global_status.dircache_size = 0;
1228 if (old_val != global_status.dircache_size)
1229 status_save();
1231 #endif
1234 void tree_restore(void)
1236 #ifdef HAVE_EEPROM_SETTINGS
1237 firmware_settings.disk_clean = false;
1238 #endif
1240 #ifdef HAVE_TC_RAMCACHE
1241 remove(TAGCACHE_STATEFILE);
1242 #endif
1244 #ifdef HAVE_DIRCACHE
1245 remove(DIRCACHE_FILE);
1246 if (global_settings.dircache)
1248 /* Print "Scanning disk..." to the display. */
1249 splash(0, str(LANG_SCANNING_DISK));
1251 dircache_build(global_status.dircache_size);
1253 #endif
1254 #ifdef HAVE_TAGCACHE
1255 tagcache_start_scan();
1256 #endif