1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2005 by Björn Stenberg
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
31 #include "filetypes.h"
46 #ifdef HAVE_LCD_BITMAP
56 int ft_build_playlist(struct tree_context
* c
, int start_index
)
59 int start
=start_index
;
61 struct entry
*dircache
= c
->dircache
;
63 for(i
= 0;i
< c
->filesindir
;i
++)
65 if((dircache
[i
].attr
& FILE_ATTR_MASK
) == FILE_ATTR_AUDIO
)
67 if (playlist_add(dircache
[i
].name
) < 0)
72 /* Adjust the start index when se skip non-MP3 entries */
81 /* Start playback of a playlist, checking for bookmark autoload, modified
82 * playlists, etc., as required. Returns false if playback wasn't started,
83 * or started via bookmark autoload, true otherwise.
85 * Pointers to both the full pathname and the separated parts needed to
86 * avoid allocating yet another path buffer on the stack (and save some
87 * code; the caller typically needs to create the full pathname anyway)...
89 bool ft_play_playlist(char* pathname
, char* dirname
, char* filename
)
91 if (global_settings
.party_mode
)
93 gui_syncsplash(HZ
, ID2P(LANG_PARTY_MODE
));
97 if (bookmark_autoload(pathname
))
102 gui_syncsplash(0, ID2P(LANG_WAIT
));
104 /* about to create a new current playlist...
105 allow user to cancel the operation */
106 if (!warn_on_pl_erase())
109 if (playlist_create(dirname
, filename
) != -1)
111 if (global_settings
.playlist_shuffle
)
113 playlist_shuffle(current_tick
, -1);
116 playlist_start(0, 0);
123 /* walk a directory and check all dircache entries if a .talk file exists */
124 static void check_file_thumbnails(struct tree_context
* c
)
127 struct dirent
*entry
;
128 struct entry
* dircache
= c
->dircache
;
131 dir
= opendir(c
->currdir
);
134 /* mark all files as non talking, except the .talk ones */
135 for (i
=0; i
< c
->filesindir
; i
++)
137 if (dircache
[i
].attr
& ATTR_DIRECTORY
)
138 continue; /* we're not touching directories */
140 if (strcasecmp(file_thumbnail_ext
,
141 &dircache
[i
].name
[strlen(dircache
[i
].name
)
142 - strlen(file_thumbnail_ext
)]))
143 { /* no .talk file */
144 dircache
[i
].attr
&= ~FILE_ATTR_THUMBNAIL
; /* clear */
147 { /* .talk file, we later let them speak themselves */
148 dircache
[i
].attr
|= FILE_ATTR_THUMBNAIL
; /* set */
152 while((entry
= readdir(dir
)) != 0) /* walk directory */
156 ext_pos
= strlen((char *)entry
->d_name
) - strlen(file_thumbnail_ext
);
157 if (ext_pos
<= 0 /* too short to carry ".talk" */
158 || (entry
->attribute
& ATTR_DIRECTORY
) /* no file */
159 || strcasecmp((char *)&entry
->d_name
[ext_pos
], file_thumbnail_ext
))
160 { /* or doesn't end with ".talk", no candidate */
164 /* terminate the (disposable) name in dir buffer,
165 this truncates off the ".talk" without needing an extra buffer */
166 entry
->d_name
[ext_pos
] = '\0';
168 /* search corresponding file in dir cache */
169 for (i
=0; i
< c
->filesindir
; i
++)
171 if (!strcasecmp(dircache
[i
].name
, (char *)entry
->d_name
))
173 dircache
[i
].attr
|= FILE_ATTR_THUMBNAIL
; /* set the flag */
174 break; /* exit search loop, because we found it */
181 /* support function for qsort() */
182 static int compare(const void* p1
, const void* p2
)
184 struct entry
* e1
= (struct entry
*)p1
;
185 struct entry
* e2
= (struct entry
*)p2
;
188 if (e1
->attr
& ATTR_DIRECTORY
&& e2
->attr
& ATTR_DIRECTORY
)
189 { /* two directories */
190 criteria
= global_settings
.sort_dir
;
192 #ifdef HAVE_MULTIVOLUME
193 if (e1
->attr
& ATTR_VOLUME
|| e2
->attr
& ATTR_VOLUME
)
194 { /* a volume identifier is involved */
195 if (e1
->attr
& ATTR_VOLUME
&& e2
->attr
& ATTR_VOLUME
)
196 criteria
= 0; /* two volumes: sort alphabetically */
197 else /* only one is a volume: volume first */
198 return (e2
->attr
& ATTR_VOLUME
) - (e1
->attr
& ATTR_VOLUME
);
203 else if (!(e1
->attr
& ATTR_DIRECTORY
) && !(e2
->attr
& ATTR_DIRECTORY
))
205 criteria
= global_settings
.sort_file
;
207 else /* dir and file, dir goes first */
208 return ( e2
->attr
& ATTR_DIRECTORY
) - ( e1
->attr
& ATTR_DIRECTORY
);
212 case 3: /* sort type */
214 int t1
= e1
->attr
& FILE_ATTR_MASK
;
215 int t2
= e2
->attr
& FILE_ATTR_MASK
;
217 if (!t1
) /* unknown type */
218 t1
= INT_MAX
; /* gets a high number, to sort after known */
219 if (!t2
) /* unknown type */
220 t2
= INT_MAX
; /* gets a high number, to sort after known */
222 if (t1
- t2
) /* if different */
224 /* else fall through to alphabetical sorting */
226 case 0: /* sort alphabetically asc */
227 if (global_settings
.sort_case
)
228 return strncmp(e1
->name
, e2
->name
, MAX_PATH
);
230 return strncasecmp(e1
->name
, e2
->name
, MAX_PATH
);
232 case 4: /* sort alphabetically desc */
233 if (global_settings
.sort_case
)
234 return strncmp(e2
->name
, e1
->name
, MAX_PATH
);
236 return strncasecmp(e2
->name
, e1
->name
, MAX_PATH
);
238 case 1: /* sort date */
239 return e1
->time_write
- e2
->time_write
;
241 case 2: /* sort date, newest first */
242 return e2
->time_write
- e1
->time_write
;
244 return 0; /* never reached */
247 /* load and sort directory into dircache. returns NULL on failure. */
248 int ft_load(struct tree_context
* c
, const char* tempdir
)
251 int name_buffer_used
= 0;
255 dir
= opendir(tempdir
);
257 dir
= opendir(c
->currdir
);
259 return -1; /* not a directory */
264 for ( i
=0; i
< global_settings
.max_files_in_dir
; i
++ ) {
266 struct dirent
*entry
= readdir(dir
);
268 (struct entry
*)(c
->dircache
+ i
* sizeof(struct entry
));
272 len
= strlen((char *)entry
->d_name
);
274 /* skip directories . and .. */
275 if ((entry
->attribute
& ATTR_DIRECTORY
) &&
276 (((len
== 1) && (!strncmp((char *)entry
->d_name
, ".", 1))) ||
277 ((len
== 2) && (!strncmp((char *)entry
->d_name
, "..", 2))))) {
282 /* Skip FAT volume ID */
283 if (entry
->attribute
& ATTR_VOLUME_ID
) {
288 /* filter out dotfiles and hidden files */
289 if (*c
->dirfilter
!= SHOW_ALL
&&
290 ((entry
->d_name
[0]=='.') ||
291 (entry
->attribute
& ATTR_HIDDEN
))) {
296 dptr
->attr
= entry
->attribute
;
298 /* check for known file types */
299 if ( !(dptr
->attr
& ATTR_DIRECTORY
) )
300 dptr
->attr
|= filetype_get_attr((char *)entry
->d_name
);
302 /* filter out non-visible files */
303 if ((!(dptr
->attr
& ATTR_DIRECTORY
) && (
304 (*c
->dirfilter
== SHOW_PLAYLIST
&&
305 (dptr
->attr
& FILE_ATTR_MASK
) != FILE_ATTR_M3U
) ||
306 ((*c
->dirfilter
== SHOW_MUSIC
&&
307 (dptr
->attr
& FILE_ATTR_MASK
) != FILE_ATTR_AUDIO
) &&
308 (dptr
->attr
& FILE_ATTR_MASK
) != FILE_ATTR_M3U
) ||
309 (*c
->dirfilter
== SHOW_SUPPORTED
&& !filetype_supported(dptr
->attr
)))) ||
310 (*c
->dirfilter
== SHOW_WPS
&& (dptr
->attr
& FILE_ATTR_MASK
) != FILE_ATTR_WPS
) ||
311 #ifdef HAVE_REMOTE_LCD
312 (*c
->dirfilter
== SHOW_RWPS
&& (dptr
->attr
& FILE_ATTR_MASK
) != FILE_ATTR_RWPS
) ||
315 (*c
->dirfilter
== SHOW_FMR
&& (dptr
->attr
& FILE_ATTR_MASK
) != FILE_ATTR_FMR
) ||
317 (*c
->dirfilter
== SHOW_CFG
&& (dptr
->attr
& FILE_ATTR_MASK
) != FILE_ATTR_CFG
) ||
318 (*c
->dirfilter
== SHOW_LNG
&& (dptr
->attr
& FILE_ATTR_MASK
) != FILE_ATTR_LNG
) ||
319 (*c
->dirfilter
== SHOW_MOD
&& (dptr
->attr
& FILE_ATTR_MASK
) != FILE_ATTR_MOD
) ||
320 (*c
->dirfilter
== SHOW_FONT
&& (dptr
->attr
& FILE_ATTR_MASK
) != FILE_ATTR_FONT
) ||
321 (*c
->dirfilter
== SHOW_PLUGINS
&& (dptr
->attr
& FILE_ATTR_MASK
) != FILE_ATTR_ROCK
))
327 if (len
> c
->name_buffer_size
- name_buffer_used
- 1) {
328 /* Tell the world that we ran out of buffer space */
332 dptr
->name
= &c
->name_buffer
[name_buffer_used
];
334 (long)entry
->wrtdate
<<16 |
335 (long)entry
->wrttime
; /* in one # */
336 strcpy(dptr
->name
, (char *)entry
->d_name
);
337 name_buffer_used
+= len
+ 1;
339 if (dptr
->attr
& ATTR_DIRECTORY
) /* count the remaining dirs */
346 qsort(c
->dircache
,i
,sizeof(struct entry
),compare
);
348 /* If thumbnail talking is enabled, make an extra run to mark files with
349 associated thumbnails, so we don't do unsuccessful spinups later. */
350 if (global_settings
.talk_file_clip
)
351 check_file_thumbnails(c
); /* map .talk to ours */
356 int ft_enter(struct tree_context
* c
)
360 struct entry
*dircache
= c
->dircache
;
361 struct entry
* file
= &dircache
[c
->selected_item
];
362 bool reload_dir
= false;
363 bool start_wps
= false;
364 bool exit_func
= false;
367 snprintf(buf
,sizeof(buf
),"%s/%s",c
->currdir
, file
->name
);
369 snprintf(buf
,sizeof(buf
),"/%s",file
->name
);
371 if (file
->attr
& ATTR_DIRECTORY
) {
372 memcpy(c
->currdir
, buf
, sizeof(c
->currdir
));
373 if ( c
->dirlevel
< MAX_DIR_LEVELS
)
374 c
->selected_item_history
[c
->dirlevel
] = c
->selected_item
;
379 int seed
= current_tick
;
383 switch ( file
->attr
& FILE_ATTR_MASK
) {
385 play
= ft_play_playlist(buf
, c
->currdir
, file
->name
);
394 case FILE_ATTR_AUDIO
:
395 if (bookmark_autoload(c
->currdir
))
398 gui_syncsplash(0, ID2P(LANG_WAIT
));
400 /* about to create a new current playlist...
401 allow user to cancel the operation */
402 if (!warn_on_pl_erase())
405 if (global_settings
.party_mode
)
407 playlist_insert_track(NULL
, buf
,
408 PLAYLIST_INSERT_LAST
, true, true);
409 gui_syncsplash(HZ
, ID2P(LANG_QUEUE_LAST
));
411 else if (playlist_create(c
->currdir
, NULL
) != -1)
413 start_index
= ft_build_playlist(c
, c
->selected_item
);
414 if (global_settings
.playlist_shuffle
)
416 start_index
= playlist_shuffle(seed
, start_index
);
418 /* when shuffling dir.: play all files
419 even if the file selected by user is
421 if (!global_settings
.play_selected
)
425 playlist_start(start_index
, 0);
431 /* fmr preset file */
434 gui_syncsplash(0, ID2P(LANG_WAIT
));
436 /* Preset inside the default folder. */
437 if(!strncasecmp(FMPRESET_PATH
, buf
, strlen(FMPRESET_PATH
)))
439 set_file(buf
, global_settings
.fmr_file
, MAX_FILENAME
);
440 radio_load_presets(global_settings
.fmr_file
);
441 if(!in_radio_screen())
445 * Preset outside default folder, we can choose such only
446 * if we are out of the radio screen, so the check for the
447 * radio status isn't neccessary
451 radio_load_presets(buf
);
459 /* wps config file */
461 gui_syncsplash(0, ID2P(LANG_WAIT
));
463 unload_wps_backdrop();
465 wps_data_load(gui_wps
[0].data
, &screens
[0], buf
, true);
466 set_file(buf
, (char *)global_settings
.wps_file
,
470 #if defined(HAVE_REMOTE_LCD) && (NB_SCREENS > 1)
471 /* remote-wps config file */
473 gui_syncsplash(0, ID2P(LANG_WAIT
));
474 #if defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1
475 unload_remote_wps_backdrop();
477 wps_data_load(gui_wps
[1].data
, &screens
[1], buf
, true);
478 set_file(buf
, (char *)global_settings
.rwps_file
,
484 gui_syncsplash(0, ID2P(LANG_WAIT
));
485 if (!settings_load_config(buf
,true))
487 gui_syncsplash(HZ
, ID2P(LANG_SETTINGS_LOADED
));
490 case FILE_ATTR_BMARK
:
491 gui_syncsplash(0, ID2P(LANG_WAIT
));
492 bookmark_load(buf
, false);
497 gui_syncsplash(0, ID2P(LANG_WAIT
));
498 if(!lang_load(buf
)) {
499 set_file(buf
, (char *)global_settings
.lang_file
,
501 talk_init(); /* use voice of same language */
502 gui_syncsplash(HZ
, ID2P(LANG_LANGUAGE_LOADED
));
506 #ifdef HAVE_LCD_BITMAP
508 gui_syncsplash(0, ID2P(LANG_WAIT
));
510 set_file(buf
, (char *)global_settings
.font_file
, MAX_FILENAME
);
514 gui_syncsplash(0, ID2P(LANG_WAIT
));
516 gui_syncsplash(HZ
, ID2P(LANG_KEYBOARD_LOADED
));
517 set_file(buf
, (char *)global_settings
.kbd_file
, MAX_FILENAME
);
524 gui_syncsplash(0, ID2P(LANG_WAIT
));
531 if (global_settings
.party_mode
) {
532 gui_syncsplash(HZ
, ID2P(LANG_PARTY_MODE
));
536 gui_syncsplash(0, ID2P(LANG_WAIT
));
538 if (plugin_load(buf
,NULL
) == PLUGIN_USB_CONNECTED
)
540 if(*c
->dirfilter
> NUM_FILTER_MODES
)
541 /* leave sub-browsers after usb, doing
542 otherwise might be confusing to the user */
550 display_cuesheet_content(buf
);
557 if (global_settings
.party_mode
) {
558 gui_syncsplash(HZ
, ID2P(LANG_PARTY_MODE
));
562 plugin
= filetype_get_plugin(file
);
565 if (plugin_load(plugin
,buf
) == PLUGIN_USB_CONNECTED
)
573 /* the resume_index must always be the index in the
574 shuffled list in case shuffle is enabled */
575 global_status
.resume_index
= start_index
;
576 global_status
.resume_offset
= 0;
582 if (*c
->dirfilter
> NUM_FILTER_MODES
&&
583 *c
->dirfilter
!= SHOW_FONT
&&
584 *c
->dirfilter
!= SHOW_PLUGINS
)
601 int ft_exit(struct tree_context
* c
)
603 extern char lastfile
[]; /* from tree.c */
606 bool exit_func
= false;
608 int i
= strlen(c
->currdir
);
610 while (c
->currdir
[i
-1]!='/')
612 strcpy(buf
,&c
->currdir
[i
]);
618 if (*c
->dirfilter
> NUM_FILTER_MODES
&& c
->dirlevel
< 1)
622 if ( c
->dirlevel
< MAX_DIR_LEVELS
)
623 c
->selected_item
=c
->selected_item_history
[c
->dirlevel
];
627 /* if undefined position */
628 if (c
->selected_item
== -1)
629 strcpy(lastfile
, buf
);
633 if (*c
->dirfilter
> NUM_FILTER_MODES
&& c
->dirlevel
< 1)