Quit with an error message if the LCD is not supported
[kugel-rb.git] / apps / filetree.c
blobec7932435d3b905d053b06ba7a614ab7e1b572a7
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 "sprintf.h"
42 #include "splash.h"
43 #include "cuesheet.h"
44 #include "filetree.h"
45 #include "misc.h"
46 #include "strnatcmp.h"
47 #ifdef HAVE_LCD_BITMAP
48 #include "keyboard.h"
49 #endif
51 #if CONFIG_TUNER
52 #include "radio.h"
53 #endif
54 #include "wps.h"
55 #include "backdrop.h"
57 static int compare_sort_dir; /* qsort key for sorting directories */
59 int ft_build_playlist(struct tree_context* c, int start_index)
61 int i;
62 int start=start_index;
64 struct entry *dircache = c->dircache;
66 for(i = 0;i < c->filesindir;i++)
68 if((dircache[i].attr & FILE_ATTR_MASK) == FILE_ATTR_AUDIO)
70 if (playlist_add(dircache[i].name) < 0)
71 break;
73 else
75 /* Adjust the start index when se skip non-MP3 entries */
76 if(i < start)
77 start_index--;
81 return start_index;
84 /* Start playback of a playlist, checking for bookmark autoload, modified
85 * playlists, etc., as required. Returns false if playback wasn't started,
86 * or started via bookmark autoload, true otherwise.
88 * Pointers to both the full pathname and the separated parts needed to
89 * avoid allocating yet another path buffer on the stack (and save some
90 * code; the caller typically needs to create the full pathname anyway)...
92 bool ft_play_playlist(char* pathname, char* dirname, char* filename)
94 if (global_settings.party_mode && audio_status())
96 splash(HZ, ID2P(LANG_PARTY_MODE));
97 return false;
100 if (bookmark_autoload(pathname))
102 return false;
105 splash(0, ID2P(LANG_WAIT));
107 /* about to create a new current playlist...
108 allow user to cancel the operation */
109 if (!warn_on_pl_erase())
110 return false;
112 if (playlist_create(dirname, filename) != -1)
114 if (global_settings.playlist_shuffle)
116 playlist_shuffle(current_tick, -1);
119 playlist_start(0, 0);
120 return true;
123 return false;
126 /* walk a directory and check all dircache entries if a .talk file exists */
127 static void check_file_thumbnails(struct tree_context* c)
129 int i;
130 struct dirent *entry;
131 struct entry* dircache = c->dircache;
132 DIR *dir;
134 dir = opendir(c->currdir);
135 if(!dir)
136 return;
137 /* mark all files as non talking, except the .talk ones */
138 for (i=0; i < c->filesindir; i++)
140 if (dircache[i].attr & ATTR_DIRECTORY)
141 continue; /* we're not touching directories */
143 if (strcasecmp(file_thumbnail_ext,
144 &dircache[i].name[strlen(dircache[i].name)
145 - strlen(file_thumbnail_ext)]))
146 { /* no .talk file */
147 dircache[i].attr &= ~FILE_ATTR_THUMBNAIL; /* clear */
149 else
150 { /* .talk file, we later let them speak themselves */
151 dircache[i].attr |= FILE_ATTR_THUMBNAIL; /* set */
155 while((entry = readdir(dir)) != 0) /* walk directory */
157 int ext_pos;
159 ext_pos = strlen((char *)entry->d_name) - strlen(file_thumbnail_ext);
160 if (ext_pos <= 0 /* too short to carry ".talk" */
161 || (entry->attribute & ATTR_DIRECTORY) /* no file */
162 || strcasecmp((char *)&entry->d_name[ext_pos], file_thumbnail_ext))
163 { /* or doesn't end with ".talk", no candidate */
164 continue;
167 /* terminate the (disposable) name in dir buffer,
168 this truncates off the ".talk" without needing an extra buffer */
169 entry->d_name[ext_pos] = '\0';
171 /* search corresponding file in dir cache */
172 for (i=0; i < c->filesindir; i++)
174 if (!strcasecmp(dircache[i].name, (char *)entry->d_name))
175 { /* match */
176 dircache[i].attr |= FILE_ATTR_THUMBNAIL; /* set the flag */
177 break; /* exit search loop, because we found it */
181 closedir(dir);
184 static void filetree_drawlists(void)
186 gui_synclist_draw(&tree_lists);
189 /* support function for qsort() */
190 static int compare(const void* p1, const void* p2)
192 struct entry* e1 = (struct entry*)p1;
193 struct entry* e2 = (struct entry*)p2;
194 int criteria;
196 if (e1->attr & ATTR_DIRECTORY && e2->attr & ATTR_DIRECTORY)
197 { /* two directories */
198 criteria = compare_sort_dir;
200 #ifdef HAVE_MULTIVOLUME
201 if (e1->attr & ATTR_VOLUME || e2->attr & ATTR_VOLUME)
202 { /* a volume identifier is involved */
203 if (e1->attr & ATTR_VOLUME && e2->attr & ATTR_VOLUME)
204 criteria = SORT_ALPHA; /* two volumes: sort alphabetically */
205 else /* only one is a volume: volume first */
206 return (e2->attr & ATTR_VOLUME) - (e1->attr & ATTR_VOLUME);
208 #endif
211 else if (!(e1->attr & ATTR_DIRECTORY) && !(e2->attr & ATTR_DIRECTORY))
212 { /* two files */
213 criteria = global_settings.sort_file;
215 else /* dir and file, dir goes first */
216 return (e2->attr & ATTR_DIRECTORY) - (e1->attr & ATTR_DIRECTORY);
218 switch(criteria)
220 case SORT_TYPE:
221 case SORT_TYPE_REVERSED:
223 int t1 = e1->attr & FILE_ATTR_MASK;
224 int t2 = e2->attr & FILE_ATTR_MASK;
226 if (!t1) /* unknown type */
227 t1 = INT_MAX; /* gets a high number, to sort after known */
228 if (!t2) /* unknown type */
229 t2 = INT_MAX; /* gets a high number, to sort after known */
231 if (t1 != t2) /* if different */
232 return (t1 - t2) * (criteria == SORT_TYPE_REVERSED ? -1 : 1);
233 /* else fall through to alphabetical sorting */
236 case SORT_DATE:
237 case SORT_DATE_REVERSED:
238 /* Ignore SORT_TYPE */
239 if (criteria == SORT_DATE || criteria == SORT_DATE_REVERSED)
241 if (e1->time_write != e2->time_write)
242 return (e1->time_write - e2->time_write)
243 * (criteria == SORT_DATE_REVERSED ? -1 : 1);
244 /* else fall through to alphabetical sorting */
247 case SORT_ALPHA:
248 case SORT_ALPHA_REVERSED:
250 if (global_settings.sort_case)
252 if (global_settings.interpret_numbers == SORT_INTERPRET_AS_NUMBER)
253 return strnatcmp(e1->name, e2->name)
254 * (criteria == SORT_ALPHA_REVERSED ? -1 : 1);
255 else
256 return strncmp(e1->name, e2->name, MAX_PATH)
257 * (criteria == SORT_ALPHA_REVERSED ? -1 : 1);
259 else
261 if (global_settings.interpret_numbers == SORT_INTERPRET_AS_NUMBER)
262 return strnatcasecmp(e1->name, e2->name)
263 * (criteria == SORT_ALPHA_REVERSED ? -1 : 1);
264 else
265 return strncasecmp(e1->name, e2->name, MAX_PATH)
266 * (criteria == SORT_ALPHA_REVERSED ? -1 : 1);
271 return 0; /* never reached */
274 /* load and sort directory into dircache. returns NULL on failure. */
275 int ft_load(struct tree_context* c, const char* tempdir)
277 int i;
278 int name_buffer_used = 0;
279 DIR *dir;
281 if (tempdir)
282 dir = opendir(tempdir);
283 else
284 dir = opendir(c->currdir);
285 if(!dir)
286 return -1; /* not a directory */
288 c->dirsindir = 0;
289 c->dirfull = false;
291 for ( i=0; i < global_settings.max_files_in_dir; i++ ) {
292 int len;
293 struct dirent *entry = readdir(dir);
294 struct entry* dptr =
295 (struct entry*)(c->dircache + i * sizeof(struct entry));
296 if (!entry)
297 break;
299 len = strlen((char *)entry->d_name);
301 /* skip directories . and .. */
302 if ((entry->attribute & ATTR_DIRECTORY) &&
303 (((len == 1) && (!strncmp((char *)entry->d_name, ".", 1))) ||
304 ((len == 2) && (!strncmp((char *)entry->d_name, "..", 2))))) {
305 i--;
306 continue;
309 /* Skip FAT volume ID */
310 if (entry->attribute & ATTR_VOLUME_ID) {
311 i--;
312 continue;
315 /* filter out dotfiles and hidden files */
316 if (*c->dirfilter != SHOW_ALL &&
317 ((entry->d_name[0]=='.') ||
318 (entry->attribute & ATTR_HIDDEN))) {
319 i--;
320 continue;
323 dptr->attr = entry->attribute;
325 /* check for known file types */
326 if ( !(dptr->attr & ATTR_DIRECTORY) )
327 dptr->attr |= filetype_get_attr((char *)entry->d_name);
329 /* filter out non-visible files */
330 if ((!(dptr->attr & ATTR_DIRECTORY) && (
331 (*c->dirfilter == SHOW_PLAYLIST &&
332 (dptr->attr & FILE_ATTR_MASK) != FILE_ATTR_M3U) ||
333 ((*c->dirfilter == SHOW_MUSIC &&
334 (dptr->attr & FILE_ATTR_MASK) != FILE_ATTR_AUDIO) &&
335 (dptr->attr & FILE_ATTR_MASK) != FILE_ATTR_M3U) ||
336 (*c->dirfilter == SHOW_SUPPORTED && !filetype_supported(dptr->attr)))) ||
337 (*c->dirfilter == SHOW_WPS && (dptr->attr & FILE_ATTR_MASK) != FILE_ATTR_WPS) ||
338 #ifdef HAVE_REMOTE_LCD
339 (*c->dirfilter == SHOW_RWPS && (dptr->attr & FILE_ATTR_MASK) != FILE_ATTR_RWPS) ||
340 #endif
341 #if CONFIG_TUNER
342 (*c->dirfilter == SHOW_FMR && (dptr->attr & FILE_ATTR_MASK) != FILE_ATTR_FMR) ||
343 #endif
344 (*c->dirfilter == SHOW_CFG && (dptr->attr & FILE_ATTR_MASK) != FILE_ATTR_CFG) ||
345 (*c->dirfilter == SHOW_LNG && (dptr->attr & FILE_ATTR_MASK) != FILE_ATTR_LNG) ||
346 (*c->dirfilter == SHOW_MOD && (dptr->attr & FILE_ATTR_MASK) != FILE_ATTR_MOD) ||
347 (*c->dirfilter == SHOW_FONT && (dptr->attr & FILE_ATTR_MASK) != FILE_ATTR_FONT) ||
348 (*c->dirfilter == SHOW_PLUGINS && (dptr->attr & FILE_ATTR_MASK) != FILE_ATTR_ROCK))
350 i--;
351 continue;
354 if (len > c->name_buffer_size - name_buffer_used - 1) {
355 /* Tell the world that we ran out of buffer space */
356 c->dirfull = true;
357 break;
359 dptr->name = &c->name_buffer[name_buffer_used];
360 dptr->time_write =
361 (long)entry->wrtdate<<16 |
362 (long)entry->wrttime; /* in one # */
363 strcpy(dptr->name, (char *)entry->d_name);
364 name_buffer_used += len + 1;
366 if (dptr->attr & ATTR_DIRECTORY) /* count the remaining dirs */
367 c->dirsindir++;
369 c->filesindir = i;
370 c->dirlength = i;
371 closedir(dir);
373 compare_sort_dir = c->sort_dir;
374 qsort(c->dircache,i,sizeof(struct entry),compare);
376 /* If thumbnail talking is enabled, make an extra run to mark files with
377 associated thumbnails, so we don't do unsuccessful spinups later. */
378 if (global_settings.talk_file_clip)
379 check_file_thumbnails(c); /* map .talk to ours */
381 return 0;
384 int ft_enter(struct tree_context* c)
386 int rc = 0;
387 char buf[MAX_PATH];
388 struct entry *dircache = c->dircache;
389 struct entry* file = &dircache[c->selected_item];
390 bool reload_dir = false;
391 bool start_wps = false;
392 bool exit_func = false;
394 if (c->currdir[1])
395 snprintf(buf,sizeof(buf),"%s/%s",c->currdir, file->name);
396 else
397 snprintf(buf,sizeof(buf),"/%s",file->name);
399 if (file->attr & ATTR_DIRECTORY) {
400 memcpy(c->currdir, buf, sizeof(c->currdir));
401 if ( c->dirlevel < MAX_DIR_LEVELS )
402 c->selected_item_history[c->dirlevel] = c->selected_item;
403 c->dirlevel++;
404 c->selected_item=0;
406 else {
407 int seed = current_tick;
408 bool play = false;
409 int start_index=0;
411 switch ( file->attr & FILE_ATTR_MASK ) {
412 case FILE_ATTR_M3U:
413 play = ft_play_playlist(buf, c->currdir, file->name);
415 if (play)
417 start_index = 0;
420 break;
422 case FILE_ATTR_AUDIO:
423 if (bookmark_autoload(c->currdir))
424 break;
426 splash(0, ID2P(LANG_WAIT));
428 /* about to create a new current playlist...
429 allow user to cancel the operation */
430 if (!warn_on_pl_erase())
431 break;
433 if (global_settings.party_mode && audio_status())
435 playlist_insert_track(NULL, buf,
436 PLAYLIST_INSERT_LAST, true, true);
437 splash(HZ, ID2P(LANG_QUEUE_LAST));
439 else if (playlist_create(c->currdir, NULL) != -1)
441 start_index = ft_build_playlist(c, c->selected_item);
442 if (global_settings.playlist_shuffle)
444 start_index = playlist_shuffle(seed, start_index);
446 /* when shuffling dir.: play all files
447 even if the file selected by user is
448 not the first one */
449 if (!global_settings.play_selected)
450 start_index = 0;
453 playlist_start(start_index, 0);
454 play = true;
456 break;
458 #if CONFIG_TUNER
459 /* fmr preset file */
460 case FILE_ATTR_FMR:
461 splash(0, ID2P(LANG_WAIT));
463 /* Preset inside the default folder. */
464 if(!strncasecmp(FMPRESET_PATH, buf, strlen(FMPRESET_PATH)))
466 set_file(buf, global_settings.fmr_file, MAX_FILENAME);
467 radio_load_presets(global_settings.fmr_file);
468 if(!in_radio_screen())
469 radio_screen();
472 * Preset outside default folder, we can choose such only
473 * if we are out of the radio screen, so the check for the
474 * radio status isn't neccessary
476 else
478 radio_load_presets(buf);
479 radio_screen();
482 break;
483 #endif
486 /* wps config file */
487 case FILE_ATTR_WPS:
488 splash(0, ID2P(LANG_WAIT));
489 #if LCD_DEPTH > 1
490 backdrop_unload(BACKDROP_SKIN_WPS);
491 #endif
492 wps_data_load(SCREEN_MAIN, buf, true);
493 set_file(buf, (char *)global_settings.wps_file,
494 MAX_FILENAME);
495 settings_apply_skins();
496 break;
498 #if defined(HAVE_REMOTE_LCD) && (NB_SCREENS > 1)
499 /* remote-wps config file */
500 case FILE_ATTR_RWPS:
501 splash(0, ID2P(LANG_WAIT));
502 #if defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1
503 remote_backdrop_unload(BACKDROP_SKIN_WPS);
504 #endif
505 wps_data_load(SCREEN_REMOTE, buf, true);
506 set_file(buf, (char *)global_settings.rwps_file,
507 MAX_FILENAME);
508 settings_apply_skins();
509 break;
510 #endif
512 case FILE_ATTR_CFG:
513 splash(0, ID2P(LANG_WAIT));
514 if (!settings_load_config(buf,true))
515 break;
516 /* do both steps seperately so that the redrawing after theme
517 * changing is independant of whether the theme has a custom ui
518 * vp or not */
519 send_event(GUI_EVENT_REFRESH, NULL);
520 /* for the statusbar */
521 send_event(GUI_EVENT_ACTIONUPDATE, (void*)true);
522 filetree_drawlists();
523 splash(HZ, ID2P(LANG_SETTINGS_LOADED));
524 break;
526 case FILE_ATTR_BMARK:
527 splash(0, ID2P(LANG_WAIT));
528 bookmark_load(buf, false);
529 reload_dir = true;
530 break;
532 case FILE_ATTR_LNG:
533 splash(0, ID2P(LANG_WAIT));
534 if(!lang_load(buf)) {
535 set_file(buf, (char *)global_settings.lang_file,
536 MAX_FILENAME);
537 talk_init(); /* use voice of same language */
538 splash(HZ, ID2P(LANG_LANGUAGE_LOADED));
540 break;
542 #ifdef HAVE_LCD_BITMAP
543 case FILE_ATTR_FONT:
544 splash(0, ID2P(LANG_WAIT));
545 font_load(buf);
546 set_file(buf, (char *)global_settings.font_file, MAX_FILENAME);
547 break;
549 case FILE_ATTR_KBD:
550 splash(0, ID2P(LANG_WAIT));
551 if (!load_kbd(buf))
552 splash(HZ, ID2P(LANG_KEYBOARD_LOADED));
553 set_file(buf, (char *)global_settings.kbd_file, MAX_FILENAME);
554 break;
555 #endif
557 #ifndef SIMULATOR
558 /* firmware file */
559 case FILE_ATTR_MOD:
560 splash(0, ID2P(LANG_WAIT));
561 rolo_load(buf);
562 break;
563 #endif
565 /* plugin file */
566 case FILE_ATTR_ROCK:
568 int ret;
569 if (global_settings.party_mode && audio_status()) {
570 splash(HZ, ID2P(LANG_PARTY_MODE));
571 break;
573 ret = plugin_load(buf,NULL);
574 switch (ret)
576 case PLUGIN_GOTO_WPS:
577 play = true;
578 break;
579 case PLUGIN_USB_CONNECTED:
580 if(*c->dirfilter > NUM_FILTER_MODES)
581 /* leave sub-browsers after usb, doing
582 otherwise might be confusing to the user */
583 exit_func = true;
584 else
585 reload_dir = true;
586 break;
588 case PLUGIN_ERROR:
589 case PLUGIN_OK:
591 default:
592 break;
594 break;
596 case FILE_ATTR_CUE:
597 display_cuesheet_content(buf);
598 break;
600 default:
602 const char* plugin;
604 if (global_settings.party_mode && audio_status()) {
605 splash(HZ, ID2P(LANG_PARTY_MODE));
606 break;
609 plugin = filetype_get_plugin(file);
610 if (plugin)
612 switch (plugin_load(plugin,buf))
614 case PLUGIN_USB_CONNECTED:
615 reload_dir = true;
616 break;
617 case PLUGIN_GOTO_WPS:
618 play = true;
619 break;
621 case PLUGIN_OK:
622 case PLUGIN_ERROR:
624 default:
625 break;
628 break;
632 send_event(GUI_EVENT_REFRESH, filetree_drawlists);
634 if ( play ) {
635 /* the resume_index must always be the index in the
636 shuffled list in case shuffle is enabled */
637 global_status.resume_index = start_index;
638 global_status.resume_offset = 0;
639 status_save();
641 start_wps = true;
643 else {
644 if (*c->dirfilter > NUM_FILTER_MODES &&
645 *c->dirfilter != SHOW_CFG &&
646 *c->dirfilter != SHOW_FONT &&
647 *c->dirfilter != SHOW_PLUGINS)
649 exit_func = true;
654 if (reload_dir)
655 rc = 1;
656 if (start_wps)
657 rc = 2;
658 if (exit_func)
659 rc = 3;
661 return rc;
664 int ft_exit(struct tree_context* c)
666 extern char lastfile[]; /* from tree.c */
667 char buf[MAX_PATH];
668 int rc = 0;
669 bool exit_func = false;
671 int i = strlen(c->currdir);
672 if (i>1) {
673 while (c->currdir[i-1]!='/')
674 i--;
675 strcpy(buf,&c->currdir[i]);
676 if (i==1)
677 c->currdir[i]=0;
678 else
679 c->currdir[i-1]=0;
681 if (*c->dirfilter > NUM_FILTER_MODES && c->dirlevel < 1)
682 exit_func = true;
684 c->dirlevel--;
685 if ( c->dirlevel < MAX_DIR_LEVELS )
686 c->selected_item=c->selected_item_history[c->dirlevel];
687 else
688 c->selected_item=0;
690 /* if undefined position */
691 if (c->selected_item == -1)
692 strcpy(lastfile, buf);
694 else
696 if (*c->dirfilter > NUM_FILTER_MODES && c->dirlevel < 1)
697 exit_func = true;
700 if (exit_func)
701 rc = 3;
703 return rc;