1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
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 ****************************************************************************/
33 #include "filetypes.h"
47 #include "strnatcmp.h"
48 #ifdef HAVE_LCD_BITMAP
58 static int compare_sort_dir
; /* qsort key for sorting directories */
60 int ft_build_playlist(struct tree_context
* c
, int start_index
)
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)
76 /* Adjust the start index when se skip non-MP3 entries */
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
));
101 if (bookmark_autoload(pathname
))
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())
113 if (playlist_create(dirname
, filename
) != -1)
115 if (global_settings
.playlist_shuffle
)
117 playlist_shuffle(current_tick
, -1);
120 playlist_start(0, 0);
127 /* walk a directory and check all dircache entries if a .talk file exists */
128 static void check_file_thumbnails(struct tree_context
* c
)
131 struct dirent
*entry
;
132 struct entry
* dircache
= c
->dircache
;
135 dir
= opendir(c
->currdir
);
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 */
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 */
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 */
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
))
177 dircache
[i
].attr
|= FILE_ATTR_THUMBNAIL
; /* set the flag */
178 break; /* exit search loop, because we found it */
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
;
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
);
207 else if (!(e1
->attr
& ATTR_DIRECTORY
) && !(e2
->attr
& ATTR_DIRECTORY
))
209 criteria
= global_settings
.sort_file
;
211 else /* dir and file, dir goes first */
212 return (e2
->attr
& ATTR_DIRECTORY
) - (e1
->attr
& ATTR_DIRECTORY
);
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 */
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 */
244 case SORT_ALPHA_REVERSED
:
246 if (global_settings
.sort_case
)
248 if (global_settings
.interpret_numbers
== SORT_INTERPRET_AS_NUMBER
)
249 return strnatcmp(e1
->name
, e2
->name
)
250 * (criteria
== SORT_ALPHA_REVERSED
? -1 : 1);
252 return strncmp(e1
->name
, e2
->name
, MAX_PATH
)
253 * (criteria
== SORT_ALPHA_REVERSED
? -1 : 1);
257 if (global_settings
.interpret_numbers
== SORT_INTERPRET_AS_NUMBER
)
258 return strnatcasecmp(e1
->name
, e2
->name
)
259 * (criteria
== SORT_ALPHA_REVERSED
? -1 : 1);
261 return strncasecmp(e1
->name
, e2
->name
, MAX_PATH
)
262 * (criteria
== SORT_ALPHA_REVERSED
? -1 : 1);
267 return 0; /* never reached */
270 /* load and sort directory into dircache. returns NULL on failure. */
271 int ft_load(struct tree_context
* c
, const char* tempdir
)
274 int name_buffer_used
= 0;
278 dir
= opendir(tempdir
);
280 dir
= opendir(c
->currdir
);
282 return -1; /* not a directory */
287 for ( i
=0; i
< global_settings
.max_files_in_dir
; i
++ ) {
289 struct dirent
*entry
= readdir(dir
);
291 (struct entry
*)(c
->dircache
+ i
* sizeof(struct entry
));
295 len
= strlen((char *)entry
->d_name
);
297 /* skip directories . and .. */
298 if ((entry
->attribute
& ATTR_DIRECTORY
) &&
299 (((len
== 1) && (!strncmp((char *)entry
->d_name
, ".", 1))) ||
300 ((len
== 2) && (!strncmp((char *)entry
->d_name
, "..", 2))))) {
305 /* Skip FAT volume ID */
306 if (entry
->attribute
& ATTR_VOLUME_ID
) {
311 /* filter out dotfiles and hidden files */
312 if (*c
->dirfilter
!= SHOW_ALL
&&
313 ((entry
->d_name
[0]=='.') ||
314 (entry
->attribute
& ATTR_HIDDEN
))) {
319 dptr
->attr
= entry
->attribute
;
321 /* check for known file types */
322 if ( !(dptr
->attr
& ATTR_DIRECTORY
) )
323 dptr
->attr
|= filetype_get_attr((char *)entry
->d_name
);
325 /* filter out non-visible files */
326 if ((!(dptr
->attr
& ATTR_DIRECTORY
) && (
327 (*c
->dirfilter
== SHOW_PLAYLIST
&&
328 (dptr
->attr
& FILE_ATTR_MASK
) != FILE_ATTR_M3U
) ||
329 ((*c
->dirfilter
== SHOW_MUSIC
&&
330 (dptr
->attr
& FILE_ATTR_MASK
) != FILE_ATTR_AUDIO
) &&
331 (dptr
->attr
& FILE_ATTR_MASK
) != FILE_ATTR_M3U
) ||
332 (*c
->dirfilter
== SHOW_SUPPORTED
&& !filetype_supported(dptr
->attr
)))) ||
333 (*c
->dirfilter
== SHOW_WPS
&& (dptr
->attr
& FILE_ATTR_MASK
) != FILE_ATTR_WPS
) ||
334 #ifdef HAVE_REMOTE_LCD
335 (*c
->dirfilter
== SHOW_RWPS
&& (dptr
->attr
& FILE_ATTR_MASK
) != FILE_ATTR_RWPS
) ||
338 (*c
->dirfilter
== SHOW_FMR
&& (dptr
->attr
& FILE_ATTR_MASK
) != FILE_ATTR_FMR
) ||
340 (*c
->dirfilter
== SHOW_CFG
&& (dptr
->attr
& FILE_ATTR_MASK
) != FILE_ATTR_CFG
) ||
341 (*c
->dirfilter
== SHOW_LNG
&& (dptr
->attr
& FILE_ATTR_MASK
) != FILE_ATTR_LNG
) ||
342 (*c
->dirfilter
== SHOW_MOD
&& (dptr
->attr
& FILE_ATTR_MASK
) != FILE_ATTR_MOD
) ||
343 (*c
->dirfilter
== SHOW_FONT
&& (dptr
->attr
& FILE_ATTR_MASK
) != FILE_ATTR_FONT
) ||
344 (*c
->dirfilter
== SHOW_PLUGINS
&& (dptr
->attr
& FILE_ATTR_MASK
) != FILE_ATTR_ROCK
))
350 if (len
> c
->name_buffer_size
- name_buffer_used
- 1) {
351 /* Tell the world that we ran out of buffer space */
355 dptr
->name
= &c
->name_buffer
[name_buffer_used
];
357 (long)entry
->wrtdate
<<16 |
358 (long)entry
->wrttime
; /* in one # */
359 strcpy(dptr
->name
, (char *)entry
->d_name
);
360 name_buffer_used
+= len
+ 1;
362 if (dptr
->attr
& ATTR_DIRECTORY
) /* count the remaining dirs */
369 compare_sort_dir
= c
->sort_dir
;
370 qsort(c
->dircache
,i
,sizeof(struct entry
),compare
);
372 /* If thumbnail talking is enabled, make an extra run to mark files with
373 associated thumbnails, so we don't do unsuccessful spinups later. */
374 if (global_settings
.talk_file_clip
)
375 check_file_thumbnails(c
); /* map .talk to ours */
380 int ft_enter(struct tree_context
* c
)
384 struct entry
*dircache
= c
->dircache
;
385 struct entry
* file
= &dircache
[c
->selected_item
];
386 bool reload_dir
= false;
387 bool start_wps
= false;
388 bool exit_func
= false;
391 snprintf(buf
,sizeof(buf
),"%s/%s",c
->currdir
, file
->name
);
393 snprintf(buf
,sizeof(buf
),"/%s",file
->name
);
395 if (file
->attr
& ATTR_DIRECTORY
) {
396 memcpy(c
->currdir
, buf
, sizeof(c
->currdir
));
397 if ( c
->dirlevel
< MAX_DIR_LEVELS
)
398 c
->selected_item_history
[c
->dirlevel
] = c
->selected_item
;
403 int seed
= current_tick
;
407 switch ( file
->attr
& FILE_ATTR_MASK
) {
409 play
= ft_play_playlist(buf
, c
->currdir
, file
->name
);
418 case FILE_ATTR_AUDIO
:
419 if (bookmark_autoload(c
->currdir
))
422 splash(0, ID2P(LANG_WAIT
));
424 /* about to create a new current playlist...
425 allow user to cancel the operation */
426 if (!warn_on_pl_erase())
429 if (global_settings
.party_mode
&& audio_status())
431 playlist_insert_track(NULL
, buf
,
432 PLAYLIST_INSERT_LAST
, true, true);
433 splash(HZ
, ID2P(LANG_QUEUE_LAST
));
435 else if (playlist_create(c
->currdir
, NULL
) != -1)
437 start_index
= ft_build_playlist(c
, c
->selected_item
);
438 if (global_settings
.playlist_shuffle
)
440 start_index
= playlist_shuffle(seed
, start_index
);
442 /* when shuffling dir.: play all files
443 even if the file selected by user is
445 if (!global_settings
.play_selected
)
449 playlist_start(start_index
, 0);
455 /* fmr preset file */
457 splash(0, ID2P(LANG_WAIT
));
459 /* Preset inside the default folder. */
460 if(!strncasecmp(FMPRESET_PATH
, buf
, strlen(FMPRESET_PATH
)))
462 set_file(buf
, global_settings
.fmr_file
, MAX_FILENAME
);
463 radio_load_presets(global_settings
.fmr_file
);
464 if(!in_radio_screen())
468 * Preset outside default folder, we can choose such only
469 * if we are out of the radio screen, so the check for the
470 * radio status isn't neccessary
474 radio_load_presets(buf
);
482 /* wps config file */
484 splash(0, ID2P(LANG_WAIT
));
486 unload_wps_backdrop();
488 wps_data_load(gui_wps
[0].data
, &screens
[0], buf
, true);
489 set_file(buf
, (char *)global_settings
.wps_file
,
493 #if defined(HAVE_REMOTE_LCD) && (NB_SCREENS > 1)
494 /* remote-wps config file */
496 splash(0, ID2P(LANG_WAIT
));
497 #if defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1
498 unload_remote_wps_backdrop();
500 wps_data_load(gui_wps
[1].data
, &screens
[1], buf
, true);
501 set_file(buf
, (char *)global_settings
.rwps_file
,
507 splash(0, ID2P(LANG_WAIT
));
508 if (!settings_load_config(buf
,true))
510 gui_synclist_draw(&tree_lists
);
511 splash(HZ
, ID2P(LANG_SETTINGS_LOADED
));
514 case FILE_ATTR_BMARK
:
515 splash(0, ID2P(LANG_WAIT
));
516 bookmark_load(buf
, false);
521 splash(0, ID2P(LANG_WAIT
));
522 if(!lang_load(buf
)) {
523 set_file(buf
, (char *)global_settings
.lang_file
,
525 talk_init(); /* use voice of same language */
526 splash(HZ
, ID2P(LANG_LANGUAGE_LOADED
));
530 #ifdef HAVE_LCD_BITMAP
532 splash(0, ID2P(LANG_WAIT
));
534 set_file(buf
, (char *)global_settings
.font_file
, MAX_FILENAME
);
538 splash(0, ID2P(LANG_WAIT
));
540 splash(HZ
, ID2P(LANG_KEYBOARD_LOADED
));
541 set_file(buf
, (char *)global_settings
.kbd_file
, MAX_FILENAME
);
548 splash(0, ID2P(LANG_WAIT
));
557 if (global_settings
.party_mode
&& audio_status()) {
558 splash(HZ
, ID2P(LANG_PARTY_MODE
));
561 ret
= plugin_load(buf
,NULL
);
564 case PLUGIN_GOTO_WPS
:
567 case PLUGIN_USB_CONNECTED
:
568 if(*c
->dirfilter
> NUM_FILTER_MODES
)
569 /* leave sub-browsers after usb, doing
570 otherwise might be confusing to the user */
585 display_cuesheet_content(buf
);
592 if (global_settings
.party_mode
&& audio_status()) {
593 splash(HZ
, ID2P(LANG_PARTY_MODE
));
597 plugin
= filetype_get_plugin(file
);
600 switch (plugin_load(plugin
,buf
))
602 case PLUGIN_USB_CONNECTED
:
605 case PLUGIN_GOTO_WPS
:
621 /* the resume_index must always be the index in the
622 shuffled list in case shuffle is enabled */
623 global_status
.resume_index
= start_index
;
624 global_status
.resume_offset
= 0;
630 if (*c
->dirfilter
> NUM_FILTER_MODES
&&
631 *c
->dirfilter
!= SHOW_CFG
&&
632 *c
->dirfilter
!= SHOW_FONT
&&
633 *c
->dirfilter
!= SHOW_PLUGINS
)
650 int ft_exit(struct tree_context
* c
)
652 extern char lastfile
[]; /* from tree.c */
655 bool exit_func
= false;
657 int i
= strlen(c
->currdir
);
659 while (c
->currdir
[i
-1]!='/')
661 strcpy(buf
,&c
->currdir
[i
]);
667 if (*c
->dirfilter
> NUM_FILTER_MODES
&& c
->dirlevel
< 1)
671 if ( c
->dirlevel
< MAX_DIR_LEVELS
)
672 c
->selected_item
=c
->selected_item_history
[c
->dirlevel
];
676 /* if undefined position */
677 if (c
->selected_item
== -1)
678 strcpy(lastfile
, buf
);
682 if (*c
->dirfilter
> NUM_FILTER_MODES
&& c
->dirlevel
< 1)