Set x bit aka fix red...
[kugel-rb.git] / apps / filetree.c
blob0ceb5c094168df8f2598e3ff03984d4e2e74629a
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 #include "strnatcmp.h"
49 #ifdef HAVE_LCD_BITMAP
50 #include "keyboard.h"
51 #endif
53 #if CONFIG_TUNER
54 #include "radio.h"
55 #endif
57 #include "backdrop.h"
59 static int compare_sort_dir; /* qsort key for sorting directories */
61 int ft_build_playlist(struct tree_context* c, int start_index)
63 int i;
64 int start=start_index;
66 struct entry *dircache = c->dircache;
68 for(i = 0;i < c->filesindir;i++)
70 if((dircache[i].attr & FILE_ATTR_MASK) == FILE_ATTR_AUDIO)
72 if (playlist_add(dircache[i].name) < 0)
73 break;
75 else
77 /* Adjust the start index when se skip non-MP3 entries */
78 if(i < start)
79 start_index--;
83 return start_index;
86 /* Start playback of a playlist, checking for bookmark autoload, modified
87 * playlists, etc., as required. Returns false if playback wasn't started,
88 * or started via bookmark autoload, true otherwise.
90 * Pointers to both the full pathname and the separated parts needed to
91 * avoid allocating yet another path buffer on the stack (and save some
92 * code; the caller typically needs to create the full pathname anyway)...
94 bool ft_play_playlist(char* pathname, char* dirname, char* filename)
96 if (global_settings.party_mode && audio_status())
98 splash(HZ, ID2P(LANG_PARTY_MODE));
99 return false;
102 if (bookmark_autoload(pathname))
104 return false;
107 splash(0, ID2P(LANG_WAIT));
109 /* about to create a new current playlist...
110 allow user to cancel the operation */
111 if (!warn_on_pl_erase())
112 return false;
114 if (playlist_create(dirname, filename) != -1)
116 if (global_settings.playlist_shuffle)
118 playlist_shuffle(current_tick, -1);
121 playlist_start(0, 0);
122 return true;
125 return false;
128 /* walk a directory and check all dircache entries if a .talk file exists */
129 static void check_file_thumbnails(struct tree_context* c)
131 int i;
132 struct dirent *entry;
133 struct entry* dircache = c->dircache;
134 DIR *dir;
136 dir = opendir(c->currdir);
137 if(!dir)
138 return;
139 /* mark all files as non talking, except the .talk ones */
140 for (i=0; i < c->filesindir; i++)
142 if (dircache[i].attr & ATTR_DIRECTORY)
143 continue; /* we're not touching directories */
145 if (strcasecmp(file_thumbnail_ext,
146 &dircache[i].name[strlen(dircache[i].name)
147 - strlen(file_thumbnail_ext)]))
148 { /* no .talk file */
149 dircache[i].attr &= ~FILE_ATTR_THUMBNAIL; /* clear */
151 else
152 { /* .talk file, we later let them speak themselves */
153 dircache[i].attr |= FILE_ATTR_THUMBNAIL; /* set */
157 while((entry = readdir(dir)) != 0) /* walk directory */
159 int ext_pos;
161 ext_pos = strlen((char *)entry->d_name) - strlen(file_thumbnail_ext);
162 if (ext_pos <= 0 /* too short to carry ".talk" */
163 || (entry->attribute & ATTR_DIRECTORY) /* no file */
164 || strcasecmp((char *)&entry->d_name[ext_pos], file_thumbnail_ext))
165 { /* or doesn't end with ".talk", no candidate */
166 continue;
169 /* terminate the (disposable) name in dir buffer,
170 this truncates off the ".talk" without needing an extra buffer */
171 entry->d_name[ext_pos] = '\0';
173 /* search corresponding file in dir cache */
174 for (i=0; i < c->filesindir; i++)
176 if (!strcasecmp(dircache[i].name, (char *)entry->d_name))
177 { /* match */
178 dircache[i].attr |= FILE_ATTR_THUMBNAIL; /* set the flag */
179 break; /* exit search loop, because we found it */
183 closedir(dir);
186 /* support function for qsort() */
187 static int compare(const void* p1, const void* p2)
189 struct entry* e1 = (struct entry*)p1;
190 struct entry* e2 = (struct entry*)p2;
191 int criteria;
193 if (e1->attr & ATTR_DIRECTORY && e2->attr & ATTR_DIRECTORY)
194 { /* two directories */
195 criteria = compare_sort_dir;
197 #ifdef HAVE_MULTIVOLUME
198 if (e1->attr & ATTR_VOLUME || e2->attr & ATTR_VOLUME)
199 { /* a volume identifier is involved */
200 if (e1->attr & ATTR_VOLUME && e2->attr & ATTR_VOLUME)
201 criteria = SORT_ALPHA; /* two volumes: sort alphabetically */
202 else /* only one is a volume: volume first */
203 return (e2->attr & ATTR_VOLUME) - (e1->attr & ATTR_VOLUME);
205 #endif
208 else if (!(e1->attr & ATTR_DIRECTORY) && !(e2->attr & ATTR_DIRECTORY))
209 { /* two files */
210 criteria = global_settings.sort_file;
212 else /* dir and file, dir goes first */
213 return (e2->attr & ATTR_DIRECTORY) - (e1->attr & ATTR_DIRECTORY);
215 switch(criteria)
217 case SORT_TYPE:
218 case SORT_TYPE_REVERSED:
220 int t1 = e1->attr & FILE_ATTR_MASK;
221 int t2 = e2->attr & FILE_ATTR_MASK;
223 if (!t1) /* unknown type */
224 t1 = INT_MAX; /* gets a high number, to sort after known */
225 if (!t2) /* unknown type */
226 t2 = INT_MAX; /* gets a high number, to sort after known */
228 if (t1 != t2) /* if different */
229 return (t1 - t2) * (criteria == SORT_TYPE_REVERSED ? -1 : 1);
230 /* else fall through to alphabetical sorting */
233 case SORT_DATE:
234 case SORT_DATE_REVERSED:
235 /* Ignore SORT_TYPE */
236 if (criteria == SORT_DATE || criteria == SORT_DATE_REVERSED)
238 if (e1->time_write != e2->time_write)
239 return (e1->time_write - e2->time_write)
240 * (criteria == SORT_DATE_REVERSED ? -1 : 1);
241 /* else fall through to alphabetical sorting */
244 case SORT_ALPHA:
245 case SORT_ALPHA_REVERSED:
247 if (global_settings.sort_case)
249 if (global_settings.interpret_numbers == SORT_INTERPRET_AS_NUMBER)
250 return strnatcmp(e1->name, e2->name)
251 * (criteria == SORT_ALPHA_REVERSED ? -1 : 1);
252 else
253 return strncmp(e1->name, e2->name, MAX_PATH)
254 * (criteria == SORT_ALPHA_REVERSED ? -1 : 1);
256 else
258 if (global_settings.interpret_numbers == SORT_INTERPRET_AS_NUMBER)
259 return strnatcasecmp(e1->name, e2->name)
260 * (criteria == SORT_ALPHA_REVERSED ? -1 : 1);
261 else
262 return strncasecmp(e1->name, e2->name, MAX_PATH)
263 * (criteria == SORT_ALPHA_REVERSED ? -1 : 1);
268 return 0; /* never reached */
271 /* load and sort directory into dircache. returns NULL on failure. */
272 int ft_load(struct tree_context* c, const char* tempdir)
274 int i;
275 int name_buffer_used = 0;
276 DIR *dir;
278 if (tempdir)
279 dir = opendir(tempdir);
280 else
281 dir = opendir(c->currdir);
282 if(!dir)
283 return -1; /* not a directory */
285 c->dirsindir = 0;
286 c->dirfull = false;
288 for ( i=0; i < global_settings.max_files_in_dir; i++ ) {
289 int len;
290 struct dirent *entry = readdir(dir);
291 struct entry* dptr =
292 (struct entry*)(c->dircache + i * sizeof(struct entry));
293 if (!entry)
294 break;
296 len = strlen((char *)entry->d_name);
298 /* skip directories . and .. */
299 if ((entry->attribute & ATTR_DIRECTORY) &&
300 (((len == 1) && (!strncmp((char *)entry->d_name, ".", 1))) ||
301 ((len == 2) && (!strncmp((char *)entry->d_name, "..", 2))))) {
302 i--;
303 continue;
306 /* Skip FAT volume ID */
307 if (entry->attribute & ATTR_VOLUME_ID) {
308 i--;
309 continue;
312 /* filter out dotfiles and hidden files */
313 if (*c->dirfilter != SHOW_ALL &&
314 ((entry->d_name[0]=='.') ||
315 (entry->attribute & ATTR_HIDDEN))) {
316 i--;
317 continue;
320 dptr->attr = entry->attribute;
322 /* check for known file types */
323 if ( !(dptr->attr & ATTR_DIRECTORY) )
324 dptr->attr |= filetype_get_attr((char *)entry->d_name);
326 /* filter out non-visible files */
327 if ((!(dptr->attr & ATTR_DIRECTORY) && (
328 (*c->dirfilter == SHOW_PLAYLIST &&
329 (dptr->attr & FILE_ATTR_MASK) != FILE_ATTR_M3U) ||
330 ((*c->dirfilter == SHOW_MUSIC &&
331 (dptr->attr & FILE_ATTR_MASK) != FILE_ATTR_AUDIO) &&
332 (dptr->attr & FILE_ATTR_MASK) != FILE_ATTR_M3U) ||
333 (*c->dirfilter == SHOW_SUPPORTED && !filetype_supported(dptr->attr)))) ||
334 (*c->dirfilter == SHOW_WPS && (dptr->attr & FILE_ATTR_MASK) != FILE_ATTR_WPS) ||
335 #ifdef HAVE_REMOTE_LCD
336 (*c->dirfilter == SHOW_RWPS && (dptr->attr & FILE_ATTR_MASK) != FILE_ATTR_RWPS) ||
337 #endif
338 #if CONFIG_TUNER
339 (*c->dirfilter == SHOW_FMR && (dptr->attr & FILE_ATTR_MASK) != FILE_ATTR_FMR) ||
340 #endif
341 (*c->dirfilter == SHOW_CFG && (dptr->attr & FILE_ATTR_MASK) != FILE_ATTR_CFG) ||
342 (*c->dirfilter == SHOW_LNG && (dptr->attr & FILE_ATTR_MASK) != FILE_ATTR_LNG) ||
343 (*c->dirfilter == SHOW_MOD && (dptr->attr & FILE_ATTR_MASK) != FILE_ATTR_MOD) ||
344 (*c->dirfilter == SHOW_FONT && (dptr->attr & FILE_ATTR_MASK) != FILE_ATTR_FONT) ||
345 (*c->dirfilter == SHOW_PLUGINS && (dptr->attr & FILE_ATTR_MASK) != FILE_ATTR_ROCK))
347 i--;
348 continue;
351 if (len > c->name_buffer_size - name_buffer_used - 1) {
352 /* Tell the world that we ran out of buffer space */
353 c->dirfull = true;
354 break;
356 dptr->name = &c->name_buffer[name_buffer_used];
357 dptr->time_write =
358 (long)entry->wrtdate<<16 |
359 (long)entry->wrttime; /* in one # */
360 strcpy(dptr->name, (char *)entry->d_name);
361 name_buffer_used += len + 1;
363 if (dptr->attr & ATTR_DIRECTORY) /* count the remaining dirs */
364 c->dirsindir++;
366 c->filesindir = i;
367 c->dirlength = i;
368 closedir(dir);
370 compare_sort_dir = c->sort_dir;
371 qsort(c->dircache,i,sizeof(struct entry),compare);
373 /* If thumbnail talking is enabled, make an extra run to mark files with
374 associated thumbnails, so we don't do unsuccessful spinups later. */
375 if (global_settings.talk_file_clip)
376 check_file_thumbnails(c); /* map .talk to ours */
378 return 0;
381 int ft_enter(struct tree_context* c)
383 int rc = 0;
384 char buf[MAX_PATH];
385 struct entry *dircache = c->dircache;
386 struct entry* file = &dircache[c->selected_item];
387 bool reload_dir = false;
388 bool start_wps = false;
389 bool exit_func = false;
391 if (c->currdir[1])
392 snprintf(buf,sizeof(buf),"%s/%s",c->currdir, file->name);
393 else
394 snprintf(buf,sizeof(buf),"/%s",file->name);
396 if (file->attr & ATTR_DIRECTORY) {
397 memcpy(c->currdir, buf, sizeof(c->currdir));
398 if ( c->dirlevel < MAX_DIR_LEVELS )
399 c->selected_item_history[c->dirlevel] = c->selected_item;
400 c->dirlevel++;
401 c->selected_item=0;
403 else {
404 int seed = current_tick;
405 bool play = false;
406 int start_index=0;
408 switch ( file->attr & FILE_ATTR_MASK ) {
409 case FILE_ATTR_M3U:
410 play = ft_play_playlist(buf, c->currdir, file->name);
412 if (play)
414 start_index = 0;
417 break;
419 case FILE_ATTR_AUDIO:
420 if (bookmark_autoload(c->currdir))
421 break;
423 splash(0, ID2P(LANG_WAIT));
425 /* about to create a new current playlist...
426 allow user to cancel the operation */
427 if (!warn_on_pl_erase())
428 break;
430 if (global_settings.party_mode && audio_status())
432 playlist_insert_track(NULL, buf,
433 PLAYLIST_INSERT_LAST, true, true);
434 splash(HZ, ID2P(LANG_QUEUE_LAST));
436 else if (playlist_create(c->currdir, NULL) != -1)
438 start_index = ft_build_playlist(c, c->selected_item);
439 if (global_settings.playlist_shuffle)
441 start_index = playlist_shuffle(seed, start_index);
443 /* when shuffling dir.: play all files
444 even if the file selected by user is
445 not the first one */
446 if (!global_settings.play_selected)
447 start_index = 0;
450 playlist_start(start_index, 0);
451 play = true;
453 break;
455 #if CONFIG_TUNER
456 /* fmr preset file */
457 case FILE_ATTR_FMR:
458 splash(0, ID2P(LANG_WAIT));
460 /* Preset inside the default folder. */
461 if(!strncasecmp(FMPRESET_PATH, buf, strlen(FMPRESET_PATH)))
463 set_file(buf, global_settings.fmr_file, MAX_FILENAME);
464 radio_load_presets(global_settings.fmr_file);
465 if(!in_radio_screen())
466 radio_screen();
469 * Preset outside default folder, we can choose such only
470 * if we are out of the radio screen, so the check for the
471 * radio status isn't neccessary
473 else
475 radio_load_presets(buf);
476 radio_screen();
479 break;
480 #endif
483 /* wps config file */
484 case FILE_ATTR_WPS:
485 splash(0, ID2P(LANG_WAIT));
486 #if LCD_DEPTH > 1
487 unload_wps_backdrop();
488 #endif
489 wps_data_load(gui_wps[0].data, &screens[0], buf, true);
490 set_file(buf, (char *)global_settings.wps_file,
491 MAX_FILENAME);
492 break;
494 #if defined(HAVE_REMOTE_LCD) && (NB_SCREENS > 1)
495 /* remote-wps config file */
496 case FILE_ATTR_RWPS:
497 splash(0, ID2P(LANG_WAIT));
498 #if defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1
499 unload_remote_wps_backdrop();
500 #endif
501 wps_data_load(gui_wps[1].data, &screens[1], buf, true);
502 set_file(buf, (char *)global_settings.rwps_file,
503 MAX_FILENAME);
504 break;
505 #endif
507 case FILE_ATTR_CFG:
508 splash(0, ID2P(LANG_WAIT));
509 if (!settings_load_config(buf,true))
510 break;
511 splash(HZ, ID2P(LANG_SETTINGS_LOADED));
512 break;
514 case FILE_ATTR_BMARK:
515 splash(0, ID2P(LANG_WAIT));
516 bookmark_load(buf, false);
517 reload_dir = true;
518 break;
520 case FILE_ATTR_LNG:
521 splash(0, ID2P(LANG_WAIT));
522 if(!lang_load(buf)) {
523 set_file(buf, (char *)global_settings.lang_file,
524 MAX_FILENAME);
525 talk_init(); /* use voice of same language */
526 splash(HZ, ID2P(LANG_LANGUAGE_LOADED));
528 break;
530 #ifdef HAVE_LCD_BITMAP
531 case FILE_ATTR_FONT:
532 splash(0, ID2P(LANG_WAIT));
533 font_load(buf);
534 set_file(buf, (char *)global_settings.font_file, MAX_FILENAME);
535 break;
537 case FILE_ATTR_KBD:
538 splash(0, ID2P(LANG_WAIT));
539 if (!load_kbd(buf))
540 splash(HZ, ID2P(LANG_KEYBOARD_LOADED));
541 set_file(buf, (char *)global_settings.kbd_file, MAX_FILENAME);
542 break;
543 #endif
545 #ifndef SIMULATOR
546 /* firmware file */
547 case FILE_ATTR_MOD:
548 splash(0, ID2P(LANG_WAIT));
549 rolo_load(buf);
550 break;
551 #endif
553 /* plugin file */
554 case FILE_ATTR_ROCK:
555 if (global_settings.party_mode && audio_status()) {
556 splash(HZ, ID2P(LANG_PARTY_MODE));
557 break;
560 if (plugin_load(buf,NULL) == PLUGIN_USB_CONNECTED)
562 if(*c->dirfilter > NUM_FILTER_MODES)
563 /* leave sub-browsers after usb, doing
564 otherwise might be confusing to the user */
565 exit_func = true;
566 else
567 reload_dir = true;
569 break;
571 case FILE_ATTR_CUE:
572 display_cuesheet_content(buf);
573 break;
575 default:
577 const char* plugin;
579 if (global_settings.party_mode && audio_status()) {
580 splash(HZ, ID2P(LANG_PARTY_MODE));
581 break;
584 plugin = filetype_get_plugin(file);
585 if (plugin)
587 if (plugin_load(plugin,buf) == PLUGIN_USB_CONNECTED)
588 reload_dir = true;
590 break;
594 if ( play ) {
595 /* the resume_index must always be the index in the
596 shuffled list in case shuffle is enabled */
597 global_status.resume_index = start_index;
598 global_status.resume_offset = 0;
599 status_save();
601 start_wps = true;
603 else {
604 if (*c->dirfilter > NUM_FILTER_MODES &&
605 *c->dirfilter != SHOW_FONT &&
606 *c->dirfilter != SHOW_PLUGINS)
608 exit_func = true;
613 if (reload_dir)
614 rc = 1;
615 if (start_wps)
616 rc = 2;
617 if (exit_func)
618 rc = 3;
620 return rc;
623 int ft_exit(struct tree_context* c)
625 extern char lastfile[]; /* from tree.c */
626 char buf[MAX_PATH];
627 int rc = 0;
628 bool exit_func = false;
630 int i = strlen(c->currdir);
631 if (i>1) {
632 while (c->currdir[i-1]!='/')
633 i--;
634 strcpy(buf,&c->currdir[i]);
635 if (i==1)
636 c->currdir[i]=0;
637 else
638 c->currdir[i-1]=0;
640 if (*c->dirfilter > NUM_FILTER_MODES && c->dirlevel < 1)
641 exit_func = true;
643 c->dirlevel--;
644 if ( c->dirlevel < MAX_DIR_LEVELS )
645 c->selected_item=c->selected_item_history[c->dirlevel];
646 else
647 c->selected_item=0;
649 /* if undefined position */
650 if (c->selected_item == -1)
651 strcpy(lastfile, buf);
653 else
655 if (*c->dirfilter > NUM_FILTER_MODES && c->dirlevel < 1)
656 exit_func = true;
659 if (exit_func)
660 rc = 3;
662 return rc;