Properly synchronize LCD and SDL button thread when switching between fullscreen...
[maemo-rb.git] / apps / tree.c
blobd87a93e2a096ada4b293b383a154ccd84cab0b43
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 (tc.browse && tc.browse->title)
422 int icon = tc.browse->icon;
423 if (icon == NOICON)
424 icon = filetype_get_icon(ATTR_DIRECTORY);
425 gui_synclist_set_title(&tree_lists, tc.browse->title, icon);
427 else if (global_settings.show_path_in_browser == SHOW_PATH_FULL)
429 gui_synclist_set_title(&tree_lists, tc.currdir,
430 filetype_get_icon(ATTR_DIRECTORY));
432 else if (global_settings.show_path_in_browser == SHOW_PATH_CURRENT)
434 char *title = strrchr(tc.currdir, '/') + 1;
435 if (*title == '\0')
437 /* Display "Files" for the root dir */
438 gui_synclist_set_title(&tree_lists, str(LANG_DIR_BROWSER),
439 filetype_get_icon(ATTR_DIRECTORY));
441 else
442 gui_synclist_set_title(&tree_lists, title,
443 filetype_get_icon(ATTR_DIRECTORY));
445 else
447 /* Must clear the title as the list is reused */
448 gui_synclist_set_title(&tree_lists, NULL, NOICON);
450 #endif
453 gui_synclist_set_nb_items(&tree_lists, tc.filesindir);
454 gui_synclist_set_icon_callback(&tree_lists,
455 global_settings.show_icons?tree_get_fileicon:NULL);
456 if( tc.selected_item >= tc.filesindir)
457 tc.selected_item=tc.filesindir-1;
459 gui_synclist_select_item(&tree_lists, tc.selected_item);
460 #ifdef HAVE_BUTTONBAR
461 if (global_settings.buttonbar) {
462 if (*tc.dirfilter < NUM_FILTER_MODES)
463 gui_buttonbar_set(&tree_buttonbar, str(LANG_SYSFONT_DIRBROWSE_F1),
464 str(LANG_SYSFONT_DIRBROWSE_F2),
465 str(LANG_SYSFONT_DIRBROWSE_F3));
466 else
467 gui_buttonbar_set(&tree_buttonbar, "<<<", "", "");
468 gui_buttonbar_draw(&tree_buttonbar);
470 #endif
471 gui_synclist_draw(&tree_lists);
472 gui_synclist_speak_item(&tree_lists);
473 return tc.filesindir;
476 /* load tracks from specified directory to resume play */
477 void resume_directory(const char *dir)
479 int dirfilter = *tc.dirfilter;
480 int ret;
481 #ifdef HAVE_TAGCACHE
482 bool id3db = *tc.dirfilter == SHOW_ID3DB;
483 #endif
484 /* make sure the dirfilter is sane. The only time it should be possible
485 * thats its not is when resume playlist is called from a plugin
487 #ifdef HAVE_TAGCACHE
488 if (!id3db)
489 #endif
490 *tc.dirfilter = global_settings.dirfilter;
491 ret = ft_load(&tc, dir);
492 *tc.dirfilter = dirfilter;
493 if (ret < 0)
494 return;
495 lastdir[0] = 0;
497 ft_build_playlist(&tc, 0);
499 #ifdef HAVE_TAGCACHE
500 if (id3db)
501 tagtree_load(&tc);
502 #endif
505 /* Returns the current working directory and also writes cwd to buf if
506 non-NULL. In case of error, returns NULL. */
507 char *getcwd(char *buf, getcwd_size_t size)
509 if (!buf)
510 return tc.currdir;
511 else if (size)
513 if ((getcwd_size_t)strlcpy(buf, tc.currdir, size) < size)
514 return buf;
516 /* size == 0, or truncation in strlcpy */
517 return NULL;
520 /* Force a reload of the directory next time directory browser is called */
521 void reload_directory(void)
523 reload_dir = true;
526 char* get_current_file(char* buffer, size_t buffer_len)
528 #ifdef HAVE_TAGCACHE
529 /* in ID3DB mode it is a bad idea to call this function */
530 /* (only happens with `follow playlist') */
531 if( *tc.dirfilter == SHOW_ID3DB )
532 return NULL;
533 #endif
535 struct entry* dc = tc.dircache;
536 struct entry* e = &dc[tc.selected_item];
537 if (getcwd(buffer, buffer_len))
539 if (tc.dirlength)
541 if (buffer[strlen(buffer)-1] != '/')
542 strlcat(buffer, "/", buffer_len);
543 if (strlcat(buffer, e->name, buffer_len) >= buffer_len)
544 return NULL;
546 return buffer;
548 return NULL;
551 /* Allow apps to change our dirfilter directly (required for sub browsers)
552 if they're suddenly going to become a file browser for example */
553 void set_dirfilter(int l_dirfilter)
555 *tc.dirfilter = l_dirfilter;
558 /* Selects a file and update tree context properly */
559 void set_current_file(const char *path)
561 const char *name;
562 int i;
564 #ifdef HAVE_TAGCACHE
565 /* in ID3DB mode it is a bad idea to call this function */
566 /* (only happens with `follow playlist') */
567 if( *tc.dirfilter == SHOW_ID3DB )
568 return;
569 #endif
571 /* separate directory from filename */
572 /* gets the directory's name and put it into tc.currdir */
573 name = strrchr(path+1,'/');
574 if (name)
576 strlcpy(tc.currdir, path, name - path + 1);
577 name++;
579 else
581 strcpy(tc.currdir, "/");
582 name = path+1;
585 strcpy(lastfile, name);
588 /* If we changed dir we must recalculate the dirlevel
589 and adjust the selected history properly */
590 if (strncmp(tc.currdir,lastdir,sizeof(lastdir)))
592 tc.dirlevel = 0;
593 tc.selected_item_history[tc.dirlevel] = -1;
595 /* use '/' to calculate dirlevel */
596 for (i = 1; path[i] != '\0'; i++)
598 if (path[i] == '/')
600 tc.dirlevel++;
601 tc.selected_item_history[tc.dirlevel] = -1;
605 if (ft_load(&tc, NULL) >= 0)
607 tc.selected_item = tree_get_file_position(lastfile);
612 /* main loop, handles key events */
613 static int dirbrowse(void)
615 int numentries=0;
616 char buf[MAX_PATH];
617 int button, oldbutton;
618 bool reload_root = false;
619 int lastfilter = *tc.dirfilter;
620 bool lastsortcase = global_settings.sort_case;
621 bool exit_func = false;
623 char* currdir = tc.currdir; /* just a shortcut */
624 #ifdef HAVE_TAGCACHE
625 bool id3db = *tc.dirfilter == SHOW_ID3DB;
627 if (id3db)
628 curr_context=CONTEXT_ID3DB;
629 else
630 #endif
631 curr_context=CONTEXT_TREE;
632 if (tc.selected_item < 0)
633 tc.selected_item = 0;
634 #ifdef HAVE_TAGCACHE
635 tc.firstpos = 0;
636 lasttable = -1;
637 lastextra = -1;
638 lastfirstpos = 0;
639 #endif
641 start_wps = false;
642 numentries = update_dir();
643 reload_dir = false;
644 if (numentries == -1)
645 return GO_TO_PREVIOUS; /* currdir is not a directory */
647 if (*tc.dirfilter > NUM_FILTER_MODES && numentries==0)
649 splash(HZ*2, ID2P(LANG_NO_FILES));
650 return GO_TO_PREVIOUS; /* No files found for rockbox_browse() */
653 gui_synclist_draw(&tree_lists);
654 while(1) {
655 struct entry *dircache = tc.dircache;
656 bool restore = false;
657 if (tc.dirlevel < 0)
658 tc.dirlevel = 0; /* shouldnt be needed.. this code needs work! */
659 #ifdef BOOTFILE
660 if (boot_changed) {
661 static const char *lines[]={ID2P(LANG_BOOT_CHANGED), ID2P(LANG_REBOOT_NOW)};
662 static const struct text_message message={lines, 2};
663 if(gui_syncyesno_run(&message, NULL, NULL)==YESNO_YES)
664 rolo_load("/" BOOTFILE);
665 restore = true;
666 boot_changed = false;
668 #endif
669 button = get_action(CONTEXT_TREE,
670 list_do_action_timeout(&tree_lists, HZ/2));
671 oldbutton = button;
672 gui_synclist_do_button(&tree_lists, &button,LIST_WRAP_UNLESS_HELD);
673 tc.selected_item = gui_synclist_get_sel_pos(&tree_lists);
674 switch ( button ) {
675 case ACTION_STD_OK:
676 /* nothing to do if no files to display */
677 if ( numentries == 0 )
678 break;
680 if ((tc.browse->flags & BROWSE_SELECTONLY) &&
681 !(dircache[tc.selected_item].attr & ATTR_DIRECTORY))
683 tc.browse->flags |= BROWSE_SELECTED;
684 get_current_file(tc.browse->buf, tc.browse->bufsize);
685 return GO_TO_PREVIOUS;
688 #ifdef HAVE_TAGCACHE
689 switch (id3db?tagtree_enter(&tc):ft_enter(&tc))
690 #else
691 switch (ft_enter(&tc))
692 #endif
694 case GO_TO_FILEBROWSER: reload_dir = true; break;
695 case GO_TO_WPS:
696 return GO_TO_WPS;
697 #if CONFIG_TUNER
698 case GO_TO_FM:
699 return GO_TO_FM;
700 #endif
701 case GO_TO_ROOT: exit_func = true; break;
702 default: break;
704 restore = true;
705 break;
707 case ACTION_STD_CANCEL:
708 if (*tc.dirfilter > NUM_FILTER_MODES && tc.dirlevel < 1) {
709 exit_func = true;
710 break;
712 if ((*tc.dirfilter == SHOW_ID3DB && tc.dirlevel == 0) ||
713 ((*tc.dirfilter != SHOW_ID3DB && !strcmp(currdir,"/"))))
715 #ifdef HAVE_LCD_BITMAP /* charcell doesnt have ACTION_TREE_PGLEFT so this isnt needed */
716 if (oldbutton == ACTION_TREE_PGLEFT)
717 break;
718 else
719 #endif
720 return GO_TO_ROOT;
723 #ifdef HAVE_TAGCACHE
724 if (id3db)
725 tagtree_exit(&tc);
726 else
727 #endif
728 if (ft_exit(&tc) == 3)
729 exit_func = true;
731 restore = true;
732 break;
734 case ACTION_TREE_STOP:
735 if (list_stop_handler())
736 restore = true;
737 break;
739 case ACTION_STD_MENU:
740 return GO_TO_ROOT;
741 break;
743 #ifdef HAVE_RECORDING
744 case ACTION_STD_REC:
745 return GO_TO_RECSCREEN;
746 #endif
748 case ACTION_TREE_WPS:
749 return GO_TO_PREVIOUS_MUSIC;
750 break;
751 #ifdef HAVE_QUICKSCREEN
752 case ACTION_STD_QUICKSCREEN:
753 /* don't enter f2 from plugin browser */
754 if (*tc.dirfilter < NUM_FILTER_MODES)
756 if (quick_screen_quick(button))
757 reload_dir = true;
758 restore = true;
760 break;
761 #endif
762 #ifdef BUTTON_F3
763 case ACTION_F3:
764 /* don't enter f3 from plugin browser */
765 if (*tc.dirfilter < NUM_FILTER_MODES)
767 if (quick_screen_f3(ACTION_F3))
768 reload_dir = true;
769 restore = true;
771 break;
772 #endif
774 #ifdef HAVE_HOTKEY
775 case ACTION_TREE_HOTKEY:
776 if (!global_settings.hotkey_tree)
777 break;
778 /* fall through */
779 #endif
780 case ACTION_STD_CONTEXT:
782 bool hotkey = button == ACTION_TREE_HOTKEY;
783 int onplay_result;
784 int attr = 0;
786 if (tc.browse->flags & BROWSE_NO_CONTEXT_MENU)
787 break;
789 if(!numentries)
790 onplay_result = onplay(NULL, 0, curr_context, hotkey);
791 else {
792 #ifdef HAVE_TAGCACHE
793 if (id3db)
795 if (tagtree_get_attr(&tc) == FILE_ATTR_AUDIO)
797 attr = FILE_ATTR_AUDIO;
798 tagtree_get_filename(&tc, buf, sizeof(buf));
800 else
801 attr = ATTR_DIRECTORY;
803 else
804 #endif
806 attr = dircache[tc.selected_item].attr;
808 if (currdir[1]) /* Not in / */
809 snprintf(buf, sizeof buf, "%s/%s",
810 currdir,
811 dircache[tc.selected_item].name);
812 else /* In / */
813 snprintf(buf, sizeof buf, "/%s",
814 dircache[tc.selected_item].name);
816 onplay_result = onplay(buf, attr, curr_context, hotkey);
818 switch (onplay_result)
820 case ONPLAY_MAINMENU:
821 return GO_TO_ROOT;
823 case ONPLAY_OK:
824 restore = true;
825 break;
827 case ONPLAY_RELOAD_DIR:
828 reload_dir = true;
829 break;
831 case ONPLAY_START_PLAY:
832 return GO_TO_WPS;
833 break;
835 break;
838 #ifdef HAVE_HOTSWAP
839 case SYS_FS_CHANGED:
840 #ifdef HAVE_TAGCACHE
841 if (!id3db)
842 #endif
843 reload_dir = true;
844 /* The 'dir no longer valid' situation will be caught later
845 * by checking the showdir() result. */
846 break;
847 #endif
849 default:
850 if (default_event_handler(button) == SYS_USB_CONNECTED)
852 if(*tc.dirfilter > NUM_FILTER_MODES)
853 /* leave sub-browsers after usb, doing otherwise
854 might be confusing to the user */
855 exit_func = true;
856 else
857 reload_dir = true;
859 break;
861 if (start_wps)
862 return GO_TO_WPS;
863 if (button && !IS_SYSEVENT(button))
865 storage_spin();
869 check_rescan:
870 /* do we need to rescan dir? */
871 if (reload_dir || reload_root ||
872 lastfilter != *tc.dirfilter ||
873 lastsortcase != global_settings.sort_case)
875 if (reload_root) {
876 strcpy(currdir, "/");
877 tc.dirlevel = 0;
878 #ifdef HAVE_TAGCACHE
879 tc.currtable = 0;
880 tc.currextra = 0;
881 lasttable = -1;
882 lastextra = -1;
883 #endif
884 reload_root = false;
887 if (!reload_dir)
889 gui_synclist_select_item(&tree_lists, 0);
890 gui_synclist_draw(&tree_lists);
891 tc.selected_item = 0;
892 lastdir[0] = 0;
895 lastfilter = *tc.dirfilter;
896 lastsortcase = global_settings.sort_case;
897 restore = true;
900 if (exit_func)
901 return GO_TO_PREVIOUS;
903 if (restore || reload_dir) {
904 /* restore display */
905 numentries = update_dir();
906 reload_dir = false;
907 if (currdir[1] && (numentries < 0))
908 { /* not in root and reload failed */
909 reload_root = true; /* try root */
910 goto check_rescan;
914 return true;
917 bool create_playlist(void)
919 char filename[MAX_PATH];
921 snprintf(filename, sizeof filename, "%s.m3u8",
922 tc.currdir[1] ? tc.currdir : "/root");
923 splashf(0, "%s %s", str(LANG_CREATING), filename);
925 trigger_cpu_boost();
926 catalog_add_to_a_playlist(tc.currdir, ATTR_DIRECTORY, true, filename);
927 cancel_cpu_boost();
929 return true;
932 void browse_context_init(struct browse_context *browse,
933 int dirfilter, unsigned flags,
934 char *title, enum themable_icons icon,
935 const char *root, const char *selected)
937 browse->dirfilter = dirfilter;
938 browse->flags = flags;
939 browse->callback_show_item = NULL;
940 browse->title = title;
941 browse->icon = icon;
942 browse->root = root;
943 browse->selected = selected;
944 browse->buf = NULL;
945 browse->bufsize = 0;
948 #define NUM_TC_BACKUP 3
949 static struct tree_context backups[NUM_TC_BACKUP];
950 /* do not make backup if it is not recursive call */
951 static int backup_count = -1;
952 int rockbox_browse(struct browse_context *browse)
954 static char current[MAX_PATH];
955 int ret_val = 0;
956 int dirfilter = browse->dirfilter;
958 if (backup_count >= NUM_TC_BACKUP)
959 return GO_TO_PREVIOUS;
960 if (backup_count >= 0)
961 backups[backup_count] = tc;
962 backup_count++;
964 tc.dirfilter = &dirfilter;
965 tc.sort_dir = global_settings.sort_dir;
967 reload_dir = true;
968 if (*tc.dirfilter >= NUM_FILTER_MODES)
970 int last_context;
972 tc.browse = browse;
973 tc.selected_item = 0;
974 tc.dirlevel = 0;
975 strlcpy(tc.currdir, browse->root, sizeof(tc.currdir));
976 start_wps = false;
977 last_context = curr_context;
979 if (browse->selected)
981 snprintf(current, sizeof(current), "%s/%s",
982 browse->root, browse->selected);
983 set_current_file(current);
984 /* set_current_file changes dirlevel, change it back */
985 tc.dirlevel = 0;
988 ret_val = dirbrowse();
989 curr_context = last_context;
991 else
993 if (dirfilter != SHOW_ID3DB)
994 tc.dirfilter = &global_settings.dirfilter;
995 tc.browse = browse;
996 strcpy(current, browse->root);
997 set_current_file(current);
998 ret_val = dirbrowse();
1000 backup_count--;
1001 if (backup_count >= 0)
1002 tc = backups[backup_count];
1003 return ret_val;
1006 void tree_mem_init(void)
1008 /* We copy the settings value in case it is changed by the user. We can't
1009 use it until the next reboot. */
1010 max_files = global_settings.max_files_in_dir;
1012 /* initialize tree context struct */
1013 memset(&tc, 0, sizeof(tc));
1014 tc.dirfilter = &global_settings.dirfilter;
1015 tc.sort_dir = global_settings.sort_dir;
1017 tc.name_buffer_size = AVERAGE_FILENAME_LENGTH * max_files;
1018 tc.name_buffer = buffer_alloc(tc.name_buffer_size);
1020 tc.dircache_size = max_files * sizeof(struct entry);
1021 tc.dircache = buffer_alloc(tc.dircache_size);
1022 tree_get_filetypes(&filetypes, &filetypes_count);
1025 bool bookmark_play(char *resume_file, int index, int offset, int seed,
1026 char *filename)
1028 int i;
1029 char* suffix = strrchr(resume_file, '.');
1030 bool started = false;
1032 if (suffix != NULL &&
1033 (!strcasecmp(suffix, ".m3u") || !strcasecmp(suffix, ".m3u8")))
1035 /* Playlist playback */
1036 char* slash;
1037 /* check that the file exists */
1038 if(!file_exists(resume_file))
1039 return false;
1041 slash = strrchr(resume_file,'/');
1042 if (slash)
1044 char* cp;
1045 *slash=0;
1047 cp=resume_file;
1048 if (!cp[0])
1049 cp="/";
1051 if (playlist_create(cp, slash+1) != -1)
1053 if (global_settings.playlist_shuffle)
1054 playlist_shuffle(seed, -1);
1055 playlist_start(index,offset);
1056 started = true;
1058 *slash='/';
1061 else
1063 /* Directory playback */
1064 lastdir[0]='\0';
1065 if (playlist_create(resume_file, NULL) != -1)
1067 char filename_buf[MAX_PATH + 1];
1068 const char* peek_filename;
1069 resume_directory(resume_file);
1070 if (global_settings.playlist_shuffle)
1071 playlist_shuffle(seed, -1);
1073 /* Check if the file is at the same spot in the directory,
1074 else search for it */
1075 peek_filename = playlist_peek(index, filename_buf,
1076 sizeof(filename_buf));
1078 if (peek_filename == NULL)
1080 /* playlist has shrunk, search from the top */
1081 index = 0;
1082 peek_filename = playlist_peek(index, filename_buf,
1083 sizeof(filename_buf));
1084 if (peek_filename == NULL)
1085 return false;
1088 if (strcmp(strrchr(peek_filename, '/') + 1, filename))
1090 for ( i=0; i < playlist_amount(); i++ )
1092 peek_filename = playlist_peek(i, filename_buf,
1093 sizeof(filename_buf));
1095 if (peek_filename == NULL)
1096 return false;
1098 if (!strcmp(strrchr(peek_filename, '/') + 1, filename))
1099 break;
1101 if (i < playlist_amount())
1102 index = i;
1103 else
1104 return false;
1106 playlist_start(index,offset);
1107 started = true;
1111 if (started)
1112 start_wps = true;
1113 return started;
1116 static void say_filetype(int attr)
1118 /* try to find a voice ID for the extension, if known */
1119 int j;
1120 attr &= FILE_ATTR_MASK; /* file type */
1121 for (j=0; j<filetypes_count; j++)
1122 if (attr == filetypes[j].tree_attr)
1124 talk_id(filetypes[j].voiceclip, true);
1125 return;
1129 static int ft_play_dirname(char* name)
1131 #if CONFIG_CODEC != SWCODEC
1132 if (audio_status() & AUDIO_STATUS_PLAY)
1133 return 0;
1134 #endif
1136 if(talk_file(tc.currdir, name, dir_thumbnail_name, NULL,
1137 NULL, false))
1139 if(global_settings.talk_filetype)
1140 talk_id(VOICE_DIR, true);
1141 return 1;
1143 else
1144 return -1;
1147 static void ft_play_filename(char *dir, char *file)
1149 #if CONFIG_CODEC != SWCODEC
1150 if (audio_status() & AUDIO_STATUS_PLAY)
1151 return;
1152 #endif
1154 if (strlen(file) >= strlen(file_thumbnail_ext)
1155 && strcasecmp(&file[strlen(file) - strlen(file_thumbnail_ext)],
1156 file_thumbnail_ext))
1157 /* file has no .talk extension */
1158 talk_file(dir, NULL, file, file_thumbnail_ext,
1159 NULL, false);
1160 else
1161 /* it already is a .talk file, play this directly, but prefix it. */
1162 talk_file(dir, NULL, file, NULL,
1163 TALK_IDARRAY(LANG_VOICE_DIR_HOVER), false);
1166 /* These two functions are called by the USB and shutdown handlers */
1167 void tree_flush(void)
1169 #ifdef HAVE_TAGCACHE
1170 tagcache_shutdown();
1171 #endif
1173 #ifdef HAVE_TC_RAMCACHE
1174 tagcache_unload_ramcache();
1175 #endif
1177 #ifdef HAVE_DIRCACHE
1179 int old_val = global_status.dircache_size;
1180 if (global_settings.dircache)
1182 if (!dircache_is_initializing())
1183 global_status.dircache_size = dircache_get_cache_size();
1184 # ifdef HAVE_EEPROM_SETTINGS
1185 if (firmware_settings.initialized)
1186 dircache_save();
1187 # endif
1188 dircache_disable();
1190 else
1192 global_status.dircache_size = 0;
1194 if (old_val != global_status.dircache_size)
1195 status_save();
1197 #endif
1200 void tree_restore(void)
1202 #ifdef HAVE_EEPROM_SETTINGS
1203 firmware_settings.disk_clean = false;
1204 #endif
1206 #ifdef HAVE_TC_RAMCACHE
1207 remove(TAGCACHE_STATEFILE);
1208 #endif
1210 #ifdef HAVE_DIRCACHE
1211 remove(DIRCACHE_FILE);
1212 if (global_settings.dircache)
1214 /* Print "Scanning disk..." to the display. */
1215 splash(0, str(LANG_SCANNING_DISK));
1217 dircache_build(global_status.dircache_size);
1219 #endif
1220 #ifdef HAVE_TAGCACHE
1221 tagcache_start_scan();
1222 #endif