Move c/h files implementing/defining standard library stuff into a new libc directory...
[kugel-rb.git] / apps / filetree.c
blob9ff48dc4515c51140d262ed1bce8e36c078e03d6
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 "lang.h"
37 #include "language.h"
38 #include "screens.h"
39 #include "plugin.h"
40 #include "rolo.h"
41 #include "splash.h"
42 #include "cuesheet.h"
43 #include "filetree.h"
44 #include "misc.h"
45 #include "strnatcmp.h"
46 #ifdef HAVE_LCD_BITMAP
47 #include "keyboard.h"
48 #endif
50 #if CONFIG_TUNER
51 #include "radio.h"
52 #endif
53 #include "wps.h"
55 static int compare_sort_dir; /* qsort key for sorting directories */
57 int ft_build_playlist(struct tree_context* c, int start_index)
59 int i;
60 int start=start_index;
62 struct entry *dircache = c->dircache;
64 for(i = 0;i < c->filesindir;i++)
66 if((dircache[i].attr & FILE_ATTR_MASK) == FILE_ATTR_AUDIO)
68 if (playlist_add(dircache[i].name) < 0)
69 break;
71 else
73 /* Adjust the start index when se skip non-MP3 entries */
74 if(i < start)
75 start_index--;
79 return start_index;
82 /* Start playback of a playlist, checking for bookmark autoload, modified
83 * playlists, etc., as required. Returns false if playback wasn't started,
84 * or started via bookmark autoload, true otherwise.
86 * Pointers to both the full pathname and the separated parts needed to
87 * avoid allocating yet another path buffer on the stack (and save some
88 * code; the caller typically needs to create the full pathname anyway)...
90 bool ft_play_playlist(char* pathname, char* dirname, char* filename)
92 if (global_settings.party_mode && audio_status())
94 splash(HZ, ID2P(LANG_PARTY_MODE));
95 return false;
98 if (bookmark_autoload(pathname))
100 return false;
103 splash(0, ID2P(LANG_WAIT));
105 /* about to create a new current playlist...
106 allow user to cancel the operation */
107 if (!warn_on_pl_erase())
108 return false;
110 if (playlist_create(dirname, filename) != -1)
112 if (global_settings.playlist_shuffle)
114 playlist_shuffle(current_tick, -1);
117 playlist_start(0, 0);
118 return true;
121 return false;
124 /* walk a directory and check all dircache entries if a .talk file exists */
125 static void check_file_thumbnails(struct tree_context* c)
127 int i;
128 struct dirent *entry;
129 struct entry* dircache = c->dircache;
130 DIR *dir;
132 dir = opendir(c->currdir);
133 if(!dir)
134 return;
135 /* mark all files as non talking, except the .talk ones */
136 for (i=0; i < c->filesindir; i++)
138 if (dircache[i].attr & ATTR_DIRECTORY)
139 continue; /* we're not touching directories */
141 if (strcasecmp(file_thumbnail_ext,
142 &dircache[i].name[strlen(dircache[i].name)
143 - strlen(file_thumbnail_ext)]))
144 { /* no .talk file */
145 dircache[i].attr &= ~FILE_ATTR_THUMBNAIL; /* clear */
147 else
148 { /* .talk file, we later let them speak themselves */
149 dircache[i].attr |= FILE_ATTR_THUMBNAIL; /* set */
153 while((entry = readdir(dir)) != 0) /* walk directory */
155 int ext_pos;
157 ext_pos = strlen((char *)entry->d_name) - strlen(file_thumbnail_ext);
158 if (ext_pos <= 0 /* too short to carry ".talk" */
159 || (entry->attribute & ATTR_DIRECTORY) /* no file */
160 || strcasecmp((char *)&entry->d_name[ext_pos], file_thumbnail_ext))
161 { /* or doesn't end with ".talk", no candidate */
162 continue;
165 /* terminate the (disposable) name in dir buffer,
166 this truncates off the ".talk" without needing an extra buffer */
167 entry->d_name[ext_pos] = '\0';
169 /* search corresponding file in dir cache */
170 for (i=0; i < c->filesindir; i++)
172 if (!strcasecmp(dircache[i].name, (char *)entry->d_name))
173 { /* match */
174 dircache[i].attr |= FILE_ATTR_THUMBNAIL; /* set the flag */
175 break; /* exit search loop, because we found it */
179 closedir(dir);
182 /* support function for qsort() */
183 static int compare(const void* p1, const void* p2)
185 struct entry* e1 = (struct entry*)p1;
186 struct entry* e2 = (struct entry*)p2;
187 int criteria;
189 if (e1->attr & ATTR_DIRECTORY && e2->attr & ATTR_DIRECTORY)
190 { /* two directories */
191 criteria = compare_sort_dir;
193 #ifdef HAVE_MULTIVOLUME
194 if (e1->attr & ATTR_VOLUME || e2->attr & ATTR_VOLUME)
195 { /* a volume identifier is involved */
196 if (e1->attr & ATTR_VOLUME && e2->attr & ATTR_VOLUME)
197 criteria = SORT_ALPHA; /* two volumes: sort alphabetically */
198 else /* only one is a volume: volume first */
199 return (e2->attr & ATTR_VOLUME) - (e1->attr & ATTR_VOLUME);
201 #endif
204 else if (!(e1->attr & ATTR_DIRECTORY) && !(e2->attr & ATTR_DIRECTORY))
205 { /* two files */
206 criteria = global_settings.sort_file;
208 else /* dir and file, dir goes first */
209 return (e2->attr & ATTR_DIRECTORY) - (e1->attr & ATTR_DIRECTORY);
211 switch(criteria)
213 case SORT_TYPE:
214 case SORT_TYPE_REVERSED:
216 int t1 = e1->attr & FILE_ATTR_MASK;
217 int t2 = e2->attr & FILE_ATTR_MASK;
219 if (!t1) /* unknown type */
220 t1 = INT_MAX; /* gets a high number, to sort after known */
221 if (!t2) /* unknown type */
222 t2 = INT_MAX; /* gets a high number, to sort after known */
224 if (t1 != t2) /* if different */
225 return (t1 - t2) * (criteria == SORT_TYPE_REVERSED ? -1 : 1);
226 /* else fall through to alphabetical sorting */
229 case SORT_DATE:
230 case SORT_DATE_REVERSED:
231 /* Ignore SORT_TYPE */
232 if (criteria == SORT_DATE || criteria == SORT_DATE_REVERSED)
234 if (e1->time_write != e2->time_write)
235 return (e1->time_write - e2->time_write)
236 * (criteria == SORT_DATE_REVERSED ? -1 : 1);
237 /* else fall through to alphabetical sorting */
240 case SORT_ALPHA:
241 case SORT_ALPHA_REVERSED:
243 if (global_settings.sort_case)
245 if (global_settings.interpret_numbers == SORT_INTERPRET_AS_NUMBER)
246 return strnatcmp(e1->name, e2->name)
247 * (criteria == SORT_ALPHA_REVERSED ? -1 : 1);
248 else
249 return strncmp(e1->name, e2->name, MAX_PATH)
250 * (criteria == SORT_ALPHA_REVERSED ? -1 : 1);
252 else
254 if (global_settings.interpret_numbers == SORT_INTERPRET_AS_NUMBER)
255 return strnatcasecmp(e1->name, e2->name)
256 * (criteria == SORT_ALPHA_REVERSED ? -1 : 1);
257 else
258 return strncasecmp(e1->name, e2->name, MAX_PATH)
259 * (criteria == SORT_ALPHA_REVERSED ? -1 : 1);
264 return 0; /* never reached */
267 /* load and sort directory into dircache. returns NULL on failure. */
268 int ft_load(struct tree_context* c, const char* tempdir)
270 int i;
271 int name_buffer_used = 0;
272 DIR *dir;
274 if (tempdir)
275 dir = opendir(tempdir);
276 else
277 dir = opendir(c->currdir);
278 if(!dir)
279 return -1; /* not a directory */
281 c->dirsindir = 0;
282 c->dirfull = false;
284 for ( i=0; i < global_settings.max_files_in_dir; i++ ) {
285 int len;
286 struct dirent *entry = readdir(dir);
287 struct entry* dptr =
288 (struct entry*)(c->dircache + i * sizeof(struct entry));
289 if (!entry)
290 break;
292 len = strlen((char *)entry->d_name);
294 /* skip directories . and .. */
295 if ((entry->attribute & ATTR_DIRECTORY) &&
296 (((len == 1) && (!strncmp((char *)entry->d_name, ".", 1))) ||
297 ((len == 2) && (!strncmp((char *)entry->d_name, "..", 2))))) {
298 i--;
299 continue;
302 /* Skip FAT volume ID */
303 if (entry->attribute & ATTR_VOLUME_ID) {
304 i--;
305 continue;
308 /* filter out dotfiles and hidden files */
309 if (*c->dirfilter != SHOW_ALL &&
310 ((entry->d_name[0]=='.') ||
311 (entry->attribute & ATTR_HIDDEN))) {
312 i--;
313 continue;
316 dptr->attr = entry->attribute;
318 /* check for known file types */
319 if ( !(dptr->attr & ATTR_DIRECTORY) )
320 dptr->attr |= filetype_get_attr((char *)entry->d_name);
322 /* filter out non-visible files */
323 if ((!(dptr->attr & ATTR_DIRECTORY) && (
324 (*c->dirfilter == SHOW_PLAYLIST &&
325 (dptr->attr & FILE_ATTR_MASK) != FILE_ATTR_M3U) ||
326 ((*c->dirfilter == SHOW_MUSIC &&
327 (dptr->attr & FILE_ATTR_MASK) != FILE_ATTR_AUDIO) &&
328 (dptr->attr & FILE_ATTR_MASK) != FILE_ATTR_M3U) ||
329 (*c->dirfilter == SHOW_SUPPORTED && !filetype_supported(dptr->attr)))) ||
330 (*c->dirfilter == SHOW_WPS && (dptr->attr & FILE_ATTR_MASK) != FILE_ATTR_WPS) ||
331 #ifdef HAVE_LCD_BITMAP
332 (*c->dirfilter == SHOW_FONT && (dptr->attr & FILE_ATTR_MASK) != FILE_ATTR_FONT) ||
333 (*c->dirfilter == SHOW_SBS && (dptr->attr & FILE_ATTR_MASK) != FILE_ATTR_SBS) ||
334 #endif
335 #ifdef HAVE_REMOTE_LCD
336 (*c->dirfilter == SHOW_RWPS && (dptr->attr & FILE_ATTR_MASK) != FILE_ATTR_RWPS) ||
337 (*c->dirfilter == SHOW_RSBS && (dptr->attr & FILE_ATTR_MASK) != FILE_ATTR_RSBS) ||
338 #endif
339 #if CONFIG_TUNER
340 (*c->dirfilter == SHOW_FMR && (dptr->attr & FILE_ATTR_MASK) != FILE_ATTR_FMR) ||
341 #endif
342 (*c->dirfilter == SHOW_CFG && (dptr->attr & FILE_ATTR_MASK) != FILE_ATTR_CFG) ||
343 (*c->dirfilter == SHOW_LNG && (dptr->attr & FILE_ATTR_MASK) != FILE_ATTR_LNG) ||
344 (*c->dirfilter == SHOW_MOD && (dptr->attr & FILE_ATTR_MASK) != FILE_ATTR_MOD) ||
345 (*c->dirfilter == SHOW_PLUGINS && (dptr->attr & FILE_ATTR_MASK) != FILE_ATTR_ROCK &&
346 (dptr->attr & FILE_ATTR_MASK) != FILE_ATTR_LUA))
348 i--;
349 continue;
352 if (len > c->name_buffer_size - name_buffer_used - 1) {
353 /* Tell the world that we ran out of buffer space */
354 c->dirfull = true;
355 break;
357 dptr->name = &c->name_buffer[name_buffer_used];
358 dptr->time_write =
359 (long)entry->wrtdate<<16 |
360 (long)entry->wrttime; /* in one # */
361 strcpy(dptr->name, (char *)entry->d_name);
362 name_buffer_used += len + 1;
364 if (dptr->attr & ATTR_DIRECTORY) /* count the remaining dirs */
365 c->dirsindir++;
367 c->filesindir = i;
368 c->dirlength = i;
369 closedir(dir);
371 compare_sort_dir = c->sort_dir;
372 qsort(c->dircache,i,sizeof(struct entry),compare);
374 /* If thumbnail talking is enabled, make an extra run to mark files with
375 associated thumbnails, so we don't do unsuccessful spinups later. */
376 if (global_settings.talk_file_clip)
377 check_file_thumbnails(c); /* map .talk to ours */
379 return 0;
381 #ifdef HAVE_LCD_BITMAP
382 static void ft_load_font(char *file)
384 #if NB_SCREENS > 1
385 MENUITEM_STRINGLIST(menu, ID2P(LANG_CUSTOM_FONT), NULL,
386 ID2P(LANG_MAIN_SCREEN), ID2P(LANG_REMOTE_SCREEN))
387 switch (do_menu(&menu, NULL, NULL, false))
389 case 0: /* main lcd */
390 splash(0, ID2P(LANG_WAIT));
391 font_load(NULL, file);
392 set_file(file, (char *)global_settings.font_file, MAX_FILENAME);
393 break;
394 case 1: /* remote */
395 splash(0, ID2P(LANG_WAIT));
396 font_load_remoteui(file);
397 set_file(file, (char *)global_settings.remote_font_file, MAX_FILENAME);
398 break;
400 #else
401 splash(0, ID2P(LANG_WAIT));
402 font_load(NULL, file);
403 set_file(file, (char *)global_settings.font_file, MAX_FILENAME);
404 #endif
406 #endif
408 int ft_enter(struct tree_context* c)
410 int rc = 0;
411 char buf[MAX_PATH];
412 struct entry *dircache = c->dircache;
413 struct entry* file = &dircache[c->selected_item];
414 bool reload_dir = false;
415 bool start_wps = false;
416 bool exit_func = false;
418 if (c->currdir[1])
419 snprintf(buf,sizeof(buf),"%s/%s",c->currdir, file->name);
420 else
421 snprintf(buf,sizeof(buf),"/%s",file->name);
423 if (file->attr & ATTR_DIRECTORY) {
424 memcpy(c->currdir, buf, sizeof(c->currdir));
425 if ( c->dirlevel < MAX_DIR_LEVELS )
426 c->selected_item_history[c->dirlevel] = c->selected_item;
427 c->dirlevel++;
428 c->selected_item=0;
430 else {
431 int seed = current_tick;
432 bool play = false;
433 int start_index=0;
435 switch ( file->attr & FILE_ATTR_MASK ) {
436 case FILE_ATTR_M3U:
437 play = ft_play_playlist(buf, c->currdir, file->name);
439 if (play)
441 start_index = 0;
444 break;
446 case FILE_ATTR_AUDIO:
447 if (bookmark_autoload(c->currdir))
448 break;
450 splash(0, ID2P(LANG_WAIT));
452 /* about to create a new current playlist...
453 allow user to cancel the operation */
454 if (!warn_on_pl_erase())
455 break;
457 if (global_settings.party_mode && audio_status())
459 playlist_insert_track(NULL, buf,
460 PLAYLIST_INSERT_LAST, true, true);
461 splash(HZ, ID2P(LANG_QUEUE_LAST));
463 else if (playlist_create(c->currdir, NULL) != -1)
465 start_index = ft_build_playlist(c, c->selected_item);
466 if (global_settings.playlist_shuffle)
468 start_index = playlist_shuffle(seed, start_index);
470 /* when shuffling dir.: play all files
471 even if the file selected by user is
472 not the first one */
473 if (!global_settings.play_selected)
474 start_index = 0;
477 playlist_start(start_index, 0);
478 play = true;
480 break;
482 #if CONFIG_TUNER
483 /* fmr preset file */
484 case FILE_ATTR_FMR:
485 splash(0, ID2P(LANG_WAIT));
487 /* Preset inside the default folder. */
488 if(!strncasecmp(FMPRESET_PATH, buf, strlen(FMPRESET_PATH)))
490 set_file(buf, global_settings.fmr_file, MAX_FILENAME);
491 radio_load_presets(global_settings.fmr_file);
492 if(!in_radio_screen())
493 radio_screen();
496 * Preset outside default folder, we can choose such only
497 * if we are out of the radio screen, so the check for the
498 * radio status isn't neccessary
500 else
502 radio_load_presets(buf);
503 radio_screen();
506 break;
507 #endif
509 #ifdef HAVE_LCD_BITMAP
510 case FILE_ATTR_SBS:
511 splash(0, ID2P(LANG_WAIT));
512 set_file(buf, (char *)global_settings.sbs_file, MAX_FILENAME);
513 settings_apply_skins();
514 break;
515 #endif
516 #ifdef HAVE_REMOTE_LCD
517 case FILE_ATTR_RSBS:
518 splash(0, ID2P(LANG_WAIT));
519 set_file(buf, (char *)global_settings.rsbs_file, MAX_FILENAME);
520 settings_apply_skins();
521 break;
522 #endif
523 /* wps config file */
524 case FILE_ATTR_WPS:
525 splash(0, ID2P(LANG_WAIT));
526 set_file(buf, (char *)global_settings.wps_file,
527 MAX_FILENAME);
528 settings_apply_skins();
529 break;
531 #if defined(HAVE_REMOTE_LCD) && (NB_SCREENS > 1)
532 /* remote-wps config file */
533 case FILE_ATTR_RWPS:
534 splash(0, ID2P(LANG_WAIT));
535 set_file(buf, (char *)global_settings.rwps_file,
536 MAX_FILENAME);
537 settings_apply_skins();
538 break;
539 #endif
541 case FILE_ATTR_CFG:
542 splash(0, ID2P(LANG_WAIT));
543 if (!settings_load_config(buf,true))
544 break;
545 splash(HZ, ID2P(LANG_SETTINGS_LOADED));
546 break;
548 case FILE_ATTR_BMARK:
549 splash(0, ID2P(LANG_WAIT));
550 bookmark_load(buf, false);
551 reload_dir = true;
552 break;
554 case FILE_ATTR_LNG:
555 splash(0, ID2P(LANG_WAIT));
556 if (lang_core_load(buf))
558 splash(HZ, ID2P(LANG_FAILED));
559 break;
561 set_file(buf, (char *)global_settings.lang_file,
562 MAX_FILENAME);
563 talk_init(); /* use voice of same language */
564 viewportmanager_theme_changed(THEME_LANGUAGE);
565 settings_apply_skins();
566 splash(HZ, ID2P(LANG_LANGUAGE_LOADED));
567 break;
569 #ifdef HAVE_LCD_BITMAP
570 case FILE_ATTR_FONT:
571 ft_load_font(buf);
572 break;
574 case FILE_ATTR_KBD:
575 splash(0, ID2P(LANG_WAIT));
576 if (!load_kbd(buf))
577 splash(HZ, ID2P(LANG_KEYBOARD_LOADED));
578 set_file(buf, (char *)global_settings.kbd_file, MAX_FILENAME);
579 break;
580 #endif
582 #ifndef SIMULATOR
583 /* firmware file */
584 case FILE_ATTR_MOD:
585 splash(0, ID2P(LANG_WAIT));
586 rolo_load(buf);
587 break;
588 #endif
590 /* plugin file */
591 case FILE_ATTR_ROCK:
592 case FILE_ATTR_LUA:
594 char *plugin = buf, *argument = NULL;
595 int ret;
597 if ((file->attr & FILE_ATTR_MASK) == FILE_ATTR_LUA) {
598 plugin = VIEWERS_DIR "/lua.rock"; /* Use a #define here ? */
599 argument = buf;
602 if (global_settings.party_mode && audio_status()) {
603 splash(HZ, ID2P(LANG_PARTY_MODE));
604 break;
606 ret = plugin_load(plugin, argument);
607 switch (ret)
609 case PLUGIN_GOTO_WPS:
610 play = true;
611 break;
612 case PLUGIN_USB_CONNECTED:
613 if(*c->dirfilter > NUM_FILTER_MODES)
614 /* leave sub-browsers after usb, doing
615 otherwise might be confusing to the user */
616 exit_func = true;
617 else
618 reload_dir = true;
619 break;
621 case PLUGIN_ERROR:
622 case PLUGIN_OK:
624 default:
625 break;
627 break;
629 case FILE_ATTR_CUE:
630 display_cuesheet_content(buf);
631 break;
633 default:
635 const char* plugin;
637 if (global_settings.party_mode && audio_status()) {
638 splash(HZ, ID2P(LANG_PARTY_MODE));
639 break;
642 plugin = filetype_get_plugin(file);
643 if (plugin)
645 switch (plugin_load(plugin,buf))
647 case PLUGIN_USB_CONNECTED:
648 reload_dir = true;
649 break;
650 case PLUGIN_GOTO_WPS:
651 play = true;
652 break;
654 case PLUGIN_OK:
655 case PLUGIN_ERROR:
657 default:
658 break;
661 break;
665 if ( play ) {
666 /* the resume_index must always be the index in the
667 shuffled list in case shuffle is enabled */
668 global_status.resume_index = start_index;
669 global_status.resume_offset = 0;
670 status_save();
672 start_wps = true;
674 else {
675 if (*c->dirfilter > NUM_FILTER_MODES &&
676 *c->dirfilter != SHOW_CFG &&
677 *c->dirfilter != SHOW_FONT &&
678 *c->dirfilter != SHOW_PLUGINS)
680 exit_func = true;
685 if (reload_dir)
686 rc = 1;
687 if (start_wps)
688 rc = 2;
689 if (exit_func)
690 rc = 3;
692 return rc;
695 int ft_exit(struct tree_context* c)
697 extern char lastfile[]; /* from tree.c */
698 char buf[MAX_PATH];
699 int rc = 0;
700 bool exit_func = false;
702 int i = strlen(c->currdir);
703 if (i>1) {
704 while (c->currdir[i-1]!='/')
705 i--;
706 strcpy(buf,&c->currdir[i]);
707 if (i==1)
708 c->currdir[i]=0;
709 else
710 c->currdir[i-1]=0;
712 if (*c->dirfilter > NUM_FILTER_MODES && c->dirlevel < 1)
713 exit_func = true;
715 c->dirlevel--;
716 if ( c->dirlevel < MAX_DIR_LEVELS )
717 c->selected_item=c->selected_item_history[c->dirlevel];
718 else
719 c->selected_item=0;
721 /* if undefined position */
722 if (c->selected_item == -1)
723 strcpy(lastfile, buf);
725 else
727 if (*c->dirfilter > NUM_FILTER_MODES && c->dirlevel < 1)
728 exit_func = true;
731 if (exit_func)
732 rc = 3;
734 return rc;