Update Beast installation instructions to use beastpatcher and remove instructions...
[kugel-rb/myfork.git] / apps / filetree.c
blobfc5e4d39349f995c5f9fc1d98fc06dbe2d65625e
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2005 by Björn 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 <stdlib.h>
22 #include <file.h>
23 #include <dir.h>
24 #include <string.h>
25 #include <kernel.h>
26 #include <lcd.h>
27 #include <debug.h>
28 #include <font.h>
29 #include <limits.h>
30 #include "bookmark.h"
31 #include "tree.h"
32 #include "settings.h"
33 #include "filetypes.h"
34 #include "talk.h"
35 #include "playlist.h"
36 #include "gwps.h"
37 #include "lang.h"
38 #include "language.h"
39 #include "screens.h"
40 #include "plugin.h"
41 #include "rolo.h"
42 #include "sprintf.h"
43 #include "splash.h"
44 #include "yesno.h"
45 #include "cuesheet.h"
46 #include "filetree.h"
47 #include "misc.h"
48 #ifdef HAVE_LCD_BITMAP
49 #include "keyboard.h"
50 #endif
52 #if CONFIG_TUNER
53 #include "radio.h"
54 #endif
56 #include "backdrop.h"
58 static int compare_sort_dir; /* qsort key for sorting directories */
60 int ft_build_playlist(struct tree_context* c, int start_index)
62 int i;
63 int start=start_index;
65 struct entry *dircache = c->dircache;
67 for(i = 0;i < c->filesindir;i++)
69 if((dircache[i].attr & FILE_ATTR_MASK) == FILE_ATTR_AUDIO)
71 if (playlist_add(dircache[i].name) < 0)
72 break;
74 else
76 /* Adjust the start index when se skip non-MP3 entries */
77 if(i < start)
78 start_index--;
82 return start_index;
85 /* Start playback of a playlist, checking for bookmark autoload, modified
86 * playlists, etc., as required. Returns false if playback wasn't started,
87 * or started via bookmark autoload, true otherwise.
89 * Pointers to both the full pathname and the separated parts needed to
90 * avoid allocating yet another path buffer on the stack (and save some
91 * code; the caller typically needs to create the full pathname anyway)...
93 bool ft_play_playlist(char* pathname, char* dirname, char* filename)
95 if (global_settings.party_mode && audio_status())
97 splash(HZ, ID2P(LANG_PARTY_MODE));
98 return false;
101 if (bookmark_autoload(pathname))
103 return false;
106 splash(0, ID2P(LANG_WAIT));
108 /* about to create a new current playlist...
109 allow user to cancel the operation */
110 if (!warn_on_pl_erase())
111 return false;
113 if (playlist_create(dirname, filename) != -1)
115 if (global_settings.playlist_shuffle)
117 playlist_shuffle(current_tick, -1);
120 playlist_start(0, 0);
121 return true;
124 return false;
127 /* walk a directory and check all dircache entries if a .talk file exists */
128 static void check_file_thumbnails(struct tree_context* c)
130 int i;
131 struct dirent *entry;
132 struct entry* dircache = c->dircache;
133 DIR *dir;
135 dir = opendir(c->currdir);
136 if(!dir)
137 return;
138 /* mark all files as non talking, except the .talk ones */
139 for (i=0; i < c->filesindir; i++)
141 if (dircache[i].attr & ATTR_DIRECTORY)
142 continue; /* we're not touching directories */
144 if (strcasecmp(file_thumbnail_ext,
145 &dircache[i].name[strlen(dircache[i].name)
146 - strlen(file_thumbnail_ext)]))
147 { /* no .talk file */
148 dircache[i].attr &= ~FILE_ATTR_THUMBNAIL; /* clear */
150 else
151 { /* .talk file, we later let them speak themselves */
152 dircache[i].attr |= FILE_ATTR_THUMBNAIL; /* set */
156 while((entry = readdir(dir)) != 0) /* walk directory */
158 int ext_pos;
160 ext_pos = strlen((char *)entry->d_name) - strlen(file_thumbnail_ext);
161 if (ext_pos <= 0 /* too short to carry ".talk" */
162 || (entry->attribute & ATTR_DIRECTORY) /* no file */
163 || strcasecmp((char *)&entry->d_name[ext_pos], file_thumbnail_ext))
164 { /* or doesn't end with ".talk", no candidate */
165 continue;
168 /* terminate the (disposable) name in dir buffer,
169 this truncates off the ".talk" without needing an extra buffer */
170 entry->d_name[ext_pos] = '\0';
172 /* search corresponding file in dir cache */
173 for (i=0; i < c->filesindir; i++)
175 if (!strcasecmp(dircache[i].name, (char *)entry->d_name))
176 { /* match */
177 dircache[i].attr |= FILE_ATTR_THUMBNAIL; /* set the flag */
178 break; /* exit search loop, because we found it */
182 closedir(dir);
185 /* support function for qsort() */
186 static int compare(const void* p1, const void* p2)
188 struct entry* e1 = (struct entry*)p1;
189 struct entry* e2 = (struct entry*)p2;
190 int criteria;
192 if (e1->attr & ATTR_DIRECTORY && e2->attr & ATTR_DIRECTORY)
193 { /* two directories */
194 criteria = compare_sort_dir;
196 #ifdef HAVE_MULTIVOLUME
197 if (e1->attr & ATTR_VOLUME || e2->attr & ATTR_VOLUME)
198 { /* a volume identifier is involved */
199 if (e1->attr & ATTR_VOLUME && e2->attr & ATTR_VOLUME)
200 criteria = SORT_ALPHA; /* two volumes: sort alphabetically */
201 else /* only one is a volume: volume first */
202 return (e2->attr & ATTR_VOLUME) - (e1->attr & ATTR_VOLUME);
204 #endif
207 else if (!(e1->attr & ATTR_DIRECTORY) && !(e2->attr & ATTR_DIRECTORY))
208 { /* two files */
209 criteria = global_settings.sort_file;
211 else /* dir and file, dir goes first */
212 return (e2->attr & ATTR_DIRECTORY) - (e1->attr & ATTR_DIRECTORY);
214 switch(criteria)
216 case SORT_TYPE:
217 case SORT_TYPE_REVERSED:
219 int t1 = e1->attr & FILE_ATTR_MASK;
220 int t2 = e2->attr & FILE_ATTR_MASK;
222 if (!t1) /* unknown type */
223 t1 = INT_MAX; /* gets a high number, to sort after known */
224 if (!t2) /* unknown type */
225 t2 = INT_MAX; /* gets a high number, to sort after known */
227 if (t1 != t2) /* if different */
228 return (t1 - t2) * (criteria == SORT_TYPE_REVERSED ? -1 : 1);
229 /* else fall through to alphabetical sorting */
232 case SORT_DATE:
233 case SORT_DATE_REVERSED:
234 /* Ignore SORT_TYPE */
235 if (criteria == SORT_DATE || criteria == SORT_DATE_REVERSED)
237 if (e1->time_write != e2->time_write)
238 return (e1->time_write - e2->time_write)
239 * (criteria == SORT_DATE_REVERSED ? -1 : 1);
240 /* else fall through to alphabetical sorting */
243 case SORT_ALPHA:
244 case SORT_ALPHA_REVERSED:
245 if (global_settings.sort_case)
246 return strncmp(e1->name, e2->name, MAX_PATH)
247 * (criteria == SORT_ALPHA_REVERSED ? -1 : 1);
248 else
249 return strncasecmp(e1->name, e2->name, MAX_PATH)
250 * (criteria == SORT_ALPHA_REVERSED ? -1 : 1);
253 return 0; /* never reached */
256 /* load and sort directory into dircache. returns NULL on failure. */
257 int ft_load(struct tree_context* c, const char* tempdir)
259 int i;
260 int name_buffer_used = 0;
261 DIR *dir;
263 if (tempdir)
264 dir = opendir(tempdir);
265 else
266 dir = opendir(c->currdir);
267 if(!dir)
268 return -1; /* not a directory */
270 c->dirsindir = 0;
271 c->dirfull = false;
273 for ( i=0; i < global_settings.max_files_in_dir; i++ ) {
274 int len;
275 struct dirent *entry = readdir(dir);
276 struct entry* dptr =
277 (struct entry*)(c->dircache + i * sizeof(struct entry));
278 if (!entry)
279 break;
281 len = strlen((char *)entry->d_name);
283 /* skip directories . and .. */
284 if ((entry->attribute & ATTR_DIRECTORY) &&
285 (((len == 1) && (!strncmp((char *)entry->d_name, ".", 1))) ||
286 ((len == 2) && (!strncmp((char *)entry->d_name, "..", 2))))) {
287 i--;
288 continue;
291 /* Skip FAT volume ID */
292 if (entry->attribute & ATTR_VOLUME_ID) {
293 i--;
294 continue;
297 /* filter out dotfiles and hidden files */
298 if (*c->dirfilter != SHOW_ALL &&
299 ((entry->d_name[0]=='.') ||
300 (entry->attribute & ATTR_HIDDEN))) {
301 i--;
302 continue;
305 dptr->attr = entry->attribute;
307 /* check for known file types */
308 if ( !(dptr->attr & ATTR_DIRECTORY) )
309 dptr->attr |= filetype_get_attr((char *)entry->d_name);
311 /* filter out non-visible files */
312 if ((!(dptr->attr & ATTR_DIRECTORY) && (
313 (*c->dirfilter == SHOW_PLAYLIST &&
314 (dptr->attr & FILE_ATTR_MASK) != FILE_ATTR_M3U) ||
315 ((*c->dirfilter == SHOW_MUSIC &&
316 (dptr->attr & FILE_ATTR_MASK) != FILE_ATTR_AUDIO) &&
317 (dptr->attr & FILE_ATTR_MASK) != FILE_ATTR_M3U) ||
318 (*c->dirfilter == SHOW_SUPPORTED && !filetype_supported(dptr->attr)))) ||
319 (*c->dirfilter == SHOW_WPS && (dptr->attr & FILE_ATTR_MASK) != FILE_ATTR_WPS) ||
320 #ifdef HAVE_REMOTE_LCD
321 (*c->dirfilter == SHOW_RWPS && (dptr->attr & FILE_ATTR_MASK) != FILE_ATTR_RWPS) ||
322 #endif
323 #if CONFIG_TUNER
324 (*c->dirfilter == SHOW_FMR && (dptr->attr & FILE_ATTR_MASK) != FILE_ATTR_FMR) ||
325 #endif
326 (*c->dirfilter == SHOW_CFG && (dptr->attr & FILE_ATTR_MASK) != FILE_ATTR_CFG) ||
327 (*c->dirfilter == SHOW_LNG && (dptr->attr & FILE_ATTR_MASK) != FILE_ATTR_LNG) ||
328 (*c->dirfilter == SHOW_MOD && (dptr->attr & FILE_ATTR_MASK) != FILE_ATTR_MOD) ||
329 (*c->dirfilter == SHOW_FONT && (dptr->attr & FILE_ATTR_MASK) != FILE_ATTR_FONT) ||
330 (*c->dirfilter == SHOW_PLUGINS && (dptr->attr & FILE_ATTR_MASK) != FILE_ATTR_ROCK))
332 i--;
333 continue;
336 if (len > c->name_buffer_size - name_buffer_used - 1) {
337 /* Tell the world that we ran out of buffer space */
338 c->dirfull = true;
339 break;
341 dptr->name = &c->name_buffer[name_buffer_used];
342 dptr->time_write =
343 (long)entry->wrtdate<<16 |
344 (long)entry->wrttime; /* in one # */
345 strcpy(dptr->name, (char *)entry->d_name);
346 name_buffer_used += len + 1;
348 if (dptr->attr & ATTR_DIRECTORY) /* count the remaining dirs */
349 c->dirsindir++;
351 c->filesindir = i;
352 c->dirlength = i;
353 closedir(dir);
355 compare_sort_dir = c->sort_dir;
356 qsort(c->dircache,i,sizeof(struct entry),compare);
358 /* If thumbnail talking is enabled, make an extra run to mark files with
359 associated thumbnails, so we don't do unsuccessful spinups later. */
360 if (global_settings.talk_file_clip)
361 check_file_thumbnails(c); /* map .talk to ours */
363 return 0;
366 int ft_enter(struct tree_context* c)
368 int rc = 0;
369 char buf[MAX_PATH];
370 struct entry *dircache = c->dircache;
371 struct entry* file = &dircache[c->selected_item];
372 bool reload_dir = false;
373 bool start_wps = false;
374 bool exit_func = false;
376 if (c->currdir[1])
377 snprintf(buf,sizeof(buf),"%s/%s",c->currdir, file->name);
378 else
379 snprintf(buf,sizeof(buf),"/%s",file->name);
381 if (file->attr & ATTR_DIRECTORY) {
382 memcpy(c->currdir, buf, sizeof(c->currdir));
383 if ( c->dirlevel < MAX_DIR_LEVELS )
384 c->selected_item_history[c->dirlevel] = c->selected_item;
385 c->dirlevel++;
386 c->selected_item=0;
388 else {
389 int seed = current_tick;
390 bool play = false;
391 int start_index=0;
393 switch ( file->attr & FILE_ATTR_MASK ) {
394 case FILE_ATTR_M3U:
395 play = ft_play_playlist(buf, c->currdir, file->name);
397 if (play)
399 start_index = 0;
402 break;
404 case FILE_ATTR_AUDIO:
405 if (bookmark_autoload(c->currdir))
406 break;
408 splash(0, ID2P(LANG_WAIT));
410 /* about to create a new current playlist...
411 allow user to cancel the operation */
412 if (!warn_on_pl_erase())
413 break;
415 if (global_settings.party_mode && audio_status())
417 playlist_insert_track(NULL, buf,
418 PLAYLIST_INSERT_LAST, true, true);
419 splash(HZ, ID2P(LANG_QUEUE_LAST));
421 else if (playlist_create(c->currdir, NULL) != -1)
423 start_index = ft_build_playlist(c, c->selected_item);
424 if (global_settings.playlist_shuffle)
426 start_index = playlist_shuffle(seed, start_index);
428 /* when shuffling dir.: play all files
429 even if the file selected by user is
430 not the first one */
431 if (!global_settings.play_selected)
432 start_index = 0;
435 playlist_start(start_index, 0);
436 play = true;
438 break;
440 #if CONFIG_TUNER
441 /* fmr preset file */
442 case FILE_ATTR_FMR:
443 splash(0, ID2P(LANG_WAIT));
445 /* Preset inside the default folder. */
446 if(!strncasecmp(FMPRESET_PATH, buf, strlen(FMPRESET_PATH)))
448 set_file(buf, global_settings.fmr_file, MAX_FILENAME);
449 radio_load_presets(global_settings.fmr_file);
450 if(!in_radio_screen())
451 radio_screen();
454 * Preset outside default folder, we can choose such only
455 * if we are out of the radio screen, so the check for the
456 * radio status isn't neccessary
458 else
460 radio_load_presets(buf);
461 radio_screen();
464 break;
465 #endif
468 /* wps config file */
469 case FILE_ATTR_WPS:
470 splash(0, ID2P(LANG_WAIT));
471 #if LCD_DEPTH > 1
472 unload_wps_backdrop();
473 #endif
474 wps_data_load(gui_wps[0].data, &screens[0], buf, true);
475 set_file(buf, (char *)global_settings.wps_file,
476 MAX_FILENAME);
477 break;
479 #if defined(HAVE_REMOTE_LCD) && (NB_SCREENS > 1)
480 /* remote-wps config file */
481 case FILE_ATTR_RWPS:
482 splash(0, ID2P(LANG_WAIT));
483 #if defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1
484 unload_remote_wps_backdrop();
485 #endif
486 wps_data_load(gui_wps[1].data, &screens[1], buf, true);
487 set_file(buf, (char *)global_settings.rwps_file,
488 MAX_FILENAME);
489 break;
490 #endif
492 case FILE_ATTR_CFG:
493 splash(0, ID2P(LANG_WAIT));
494 if (!settings_load_config(buf,true))
495 break;
496 splash(HZ, ID2P(LANG_SETTINGS_LOADED));
497 break;
499 case FILE_ATTR_BMARK:
500 splash(0, ID2P(LANG_WAIT));
501 bookmark_load(buf, false);
502 reload_dir = true;
503 break;
505 case FILE_ATTR_LNG:
506 splash(0, ID2P(LANG_WAIT));
507 if(!lang_load(buf)) {
508 set_file(buf, (char *)global_settings.lang_file,
509 MAX_FILENAME);
510 talk_init(); /* use voice of same language */
511 splash(HZ, ID2P(LANG_LANGUAGE_LOADED));
513 break;
515 #ifdef HAVE_LCD_BITMAP
516 case FILE_ATTR_FONT:
517 splash(0, ID2P(LANG_WAIT));
518 font_load(buf);
519 set_file(buf, (char *)global_settings.font_file, MAX_FILENAME);
520 break;
522 case FILE_ATTR_KBD:
523 splash(0, ID2P(LANG_WAIT));
524 if (!load_kbd(buf))
525 splash(HZ, ID2P(LANG_KEYBOARD_LOADED));
526 set_file(buf, (char *)global_settings.kbd_file, MAX_FILENAME);
527 break;
528 #endif
530 #ifndef SIMULATOR
531 /* firmware file */
532 case FILE_ATTR_MOD:
533 splash(0, ID2P(LANG_WAIT));
534 rolo_load(buf);
535 break;
536 #endif
538 /* plugin file */
539 case FILE_ATTR_ROCK:
540 if (global_settings.party_mode && audio_status()) {
541 splash(HZ, ID2P(LANG_PARTY_MODE));
542 break;
545 if (plugin_load(buf,NULL) == PLUGIN_USB_CONNECTED)
547 if(*c->dirfilter > NUM_FILTER_MODES)
548 /* leave sub-browsers after usb, doing
549 otherwise might be confusing to the user */
550 exit_func = true;
551 else
552 reload_dir = true;
554 break;
556 case FILE_ATTR_CUE:
557 display_cuesheet_content(buf);
558 break;
560 default:
562 const char* plugin;
564 if (global_settings.party_mode && audio_status()) {
565 splash(HZ, ID2P(LANG_PARTY_MODE));
566 break;
569 plugin = filetype_get_plugin(file);
570 if (plugin)
572 if (plugin_load(plugin,buf) == PLUGIN_USB_CONNECTED)
573 reload_dir = true;
575 break;
579 if ( play ) {
580 /* the resume_index must always be the index in the
581 shuffled list in case shuffle is enabled */
582 global_status.resume_index = start_index;
583 global_status.resume_offset = 0;
584 status_save();
586 start_wps = true;
588 else {
589 if (*c->dirfilter > NUM_FILTER_MODES &&
590 *c->dirfilter != SHOW_FONT &&
591 *c->dirfilter != SHOW_PLUGINS)
593 exit_func = true;
598 if (reload_dir)
599 rc = 1;
600 if (start_wps)
601 rc = 2;
602 if (exit_func)
603 rc = 3;
605 return rc;
608 int ft_exit(struct tree_context* c)
610 extern char lastfile[]; /* from tree.c */
611 char buf[MAX_PATH];
612 int rc = 0;
613 bool exit_func = false;
615 int i = strlen(c->currdir);
616 if (i>1) {
617 while (c->currdir[i-1]!='/')
618 i--;
619 strcpy(buf,&c->currdir[i]);
620 if (i==1)
621 c->currdir[i]=0;
622 else
623 c->currdir[i-1]=0;
625 if (*c->dirfilter > NUM_FILTER_MODES && c->dirlevel < 1)
626 exit_func = true;
628 c->dirlevel--;
629 if ( c->dirlevel < MAX_DIR_LEVELS )
630 c->selected_item=c->selected_item_history[c->dirlevel];
631 else
632 c->selected_item=0;
634 /* if undefined position */
635 if (c->selected_item == -1)
636 strcpy(lastfile, buf);
638 else
640 if (*c->dirfilter > NUM_FILTER_MODES && c->dirlevel < 1)
641 exit_func = true;
644 if (exit_func)
645 rc = 3;
647 return rc;