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"
44 #ifdef HAVE_LCD_BITMAP
54 int ft_build_playlist(struct tree_context
* c
, int start_index
)
57 int start
=start_index
;
59 struct entry
*dircache
= c
->dircache
;
61 for(i
= 0;i
< c
->filesindir
;i
++)
63 if((dircache
[i
].attr
& FILE_ATTR_MASK
) == FILE_ATTR_AUDIO
)
65 if (playlist_add(dircache
[i
].name
) < 0)
70 /* Adjust the start index when se skip non-MP3 entries */
79 /* Start playback of a playlist, checking for bookmark autoload, modified
80 * playlists, etc., as required. Returns false if playback wasn't started,
81 * or started via bookmark autoload, true otherwise.
83 * Pointers to both the full pathname and the separated parts needed to
84 * avoid allocating yet another path buffer on the stack (and save some
85 * code; the caller typically needs to create the full pathname anyway)...
87 bool ft_play_playlist(char* pathname
, char* dirname
, char* filename
)
89 if (global_settings
.party_mode
)
91 gui_syncsplash(HZ
, ID2P(LANG_PARTY_MODE
));
95 if (bookmark_autoload(pathname
))
100 gui_syncsplash(0, ID2P(LANG_WAIT
));
102 /* about to create a new current playlist...
103 allow user to cancel the operation */
104 if (global_settings
.warnon_erase_dynplaylist
&&
105 playlist_modified(NULL
))
107 char *lines
[] = {ID2P(LANG_WARN_ERASEDYNPLAYLIST_PROMPT
)};
108 struct text_message message
= {lines
, 1};
110 if (gui_syncyesno_run(&message
, NULL
, NULL
) != YESNO_YES
)
114 if (playlist_create(dirname
, filename
) != -1)
116 if (global_settings
.playlist_shuffle
)
118 playlist_shuffle(current_tick
, -1);
121 playlist_start(0, 0);
128 /* walk a directory and check all dircache entries if a .talk file exists */
129 static void check_file_thumbnails(struct tree_context
* c
)
132 struct dirent
*entry
;
133 struct entry
* dircache
= c
->dircache
;
136 dir
= opendir(c
->currdir
);
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 */
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 */
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 */
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
))
178 dircache
[i
].attr
|= FILE_ATTR_THUMBNAIL
; /* set the flag */
179 break; /* exit search loop, because we found it */
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
;
193 if (e1
->attr
& ATTR_DIRECTORY
&& e2
->attr
& ATTR_DIRECTORY
)
194 { /* two directories */
195 criteria
= global_settings
.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
= 0; /* two volumes: sort alphabetically */
202 else /* only one is a volume: volume first */
203 return (e2
->attr
& ATTR_VOLUME
) - (e1
->attr
& ATTR_VOLUME
);
208 else if (!(e1
->attr
& ATTR_DIRECTORY
) && !(e2
->attr
& ATTR_DIRECTORY
))
210 criteria
= global_settings
.sort_file
;
212 else /* dir and file, dir goes first */
213 return ( e2
->attr
& ATTR_DIRECTORY
) - ( e1
->attr
& ATTR_DIRECTORY
);
217 case 3: /* sort type */
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 */
229 /* else fall through to alphabetical sorting */
231 case 0: /* sort alphabetically asc */
232 if (global_settings
.sort_case
)
233 return strncmp(e1
->name
, e2
->name
, MAX_PATH
);
235 return strncasecmp(e1
->name
, e2
->name
, MAX_PATH
);
237 case 4: /* sort alphabetically desc */
238 if (global_settings
.sort_case
)
239 return strncmp(e2
->name
, e1
->name
, MAX_PATH
);
241 return strncasecmp(e2
->name
, e1
->name
, MAX_PATH
);
243 case 1: /* sort date */
244 return e1
->time_write
- e2
->time_write
;
246 case 2: /* sort date, newest first */
247 return e2
->time_write
- e1
->time_write
;
249 return 0; /* never reached */
252 /* load and sort directory into dircache. returns NULL on failure. */
253 int ft_load(struct tree_context
* c
, const char* tempdir
)
256 int name_buffer_used
= 0;
260 dir
= opendir(tempdir
);
262 dir
= opendir(c
->currdir
);
264 return -1; /* not a directory */
269 for ( i
=0; i
< global_settings
.max_files_in_dir
; i
++ ) {
271 struct dirent
*entry
= readdir(dir
);
273 (struct entry
*)(c
->dircache
+ i
* sizeof(struct entry
));
277 len
= strlen((char *)entry
->d_name
);
279 /* skip directories . and .. */
280 if ((entry
->attribute
& ATTR_DIRECTORY
) &&
281 (((len
== 1) && (!strncmp((char *)entry
->d_name
, ".", 1))) ||
282 ((len
== 2) && (!strncmp((char *)entry
->d_name
, "..", 2))))) {
287 /* Skip FAT volume ID */
288 if (entry
->attribute
& ATTR_VOLUME_ID
) {
293 /* filter out dotfiles and hidden files */
294 if (*c
->dirfilter
!= SHOW_ALL
&&
295 ((entry
->d_name
[0]=='.') ||
296 (entry
->attribute
& ATTR_HIDDEN
))) {
301 dptr
->attr
= entry
->attribute
;
303 /* check for known file types */
304 if ( !(dptr
->attr
& ATTR_DIRECTORY
) )
305 dptr
->attr
|= filetype_get_attr((char *)entry
->d_name
);
307 /* filter out non-visible files */
308 if ((!(dptr
->attr
& ATTR_DIRECTORY
) && (
309 (*c
->dirfilter
== SHOW_PLAYLIST
&&
310 (dptr
->attr
& FILE_ATTR_MASK
) != FILE_ATTR_M3U
) ||
311 ((*c
->dirfilter
== SHOW_MUSIC
&&
312 (dptr
->attr
& FILE_ATTR_MASK
) != FILE_ATTR_AUDIO
) &&
313 (dptr
->attr
& FILE_ATTR_MASK
) != FILE_ATTR_M3U
) ||
314 (*c
->dirfilter
== SHOW_SUPPORTED
&& !filetype_supported(dptr
->attr
)))) ||
315 (*c
->dirfilter
== SHOW_WPS
&& (dptr
->attr
& FILE_ATTR_MASK
) != FILE_ATTR_WPS
) ||
316 #ifdef HAVE_REMOTE_LCD
317 (*c
->dirfilter
== SHOW_RWPS
&& (dptr
->attr
& FILE_ATTR_MASK
) != FILE_ATTR_RWPS
) ||
320 (*c
->dirfilter
== SHOW_FMR
&& (dptr
->attr
& FILE_ATTR_MASK
) != FILE_ATTR_FMR
) ||
322 (*c
->dirfilter
== SHOW_CFG
&& (dptr
->attr
& FILE_ATTR_MASK
) != FILE_ATTR_CFG
) ||
323 (*c
->dirfilter
== SHOW_LNG
&& (dptr
->attr
& FILE_ATTR_MASK
) != FILE_ATTR_LNG
) ||
324 (*c
->dirfilter
== SHOW_MOD
&& (dptr
->attr
& FILE_ATTR_MASK
) != FILE_ATTR_MOD
) ||
325 (*c
->dirfilter
== SHOW_FONT
&& (dptr
->attr
& FILE_ATTR_MASK
) != FILE_ATTR_FONT
) ||
326 (*c
->dirfilter
== SHOW_PLUGINS
&& (dptr
->attr
& FILE_ATTR_MASK
) != FILE_ATTR_ROCK
))
332 if (len
> c
->name_buffer_size
- name_buffer_used
- 1) {
333 /* Tell the world that we ran out of buffer space */
337 dptr
->name
= &c
->name_buffer
[name_buffer_used
];
339 (long)entry
->wrtdate
<<16 |
340 (long)entry
->wrttime
; /* in one # */
341 strcpy(dptr
->name
, (char *)entry
->d_name
);
342 name_buffer_used
+= len
+ 1;
344 if (dptr
->attr
& ATTR_DIRECTORY
) /* count the remaining dirs */
351 qsort(c
->dircache
,i
,sizeof(struct entry
),compare
);
353 /* If thumbnail talking is enabled, make an extra run to mark files with
354 associated thumbnails, so we don't do unsuccessful spinups later. */
355 if (global_settings
.talk_file_clip
)
356 check_file_thumbnails(c
); /* map .talk to ours */
361 int ft_enter(struct tree_context
* c
)
365 struct entry
*dircache
= c
->dircache
;
366 struct entry
* file
= &dircache
[c
->selected_item
];
367 bool reload_dir
= false;
368 bool start_wps
= false;
369 bool exit_func
= false;
372 snprintf(buf
,sizeof(buf
),"%s/%s",c
->currdir
, file
->name
);
374 snprintf(buf
,sizeof(buf
),"/%s",file
->name
);
376 if (file
->attr
& ATTR_DIRECTORY
) {
377 memcpy(c
->currdir
, buf
, sizeof(c
->currdir
));
378 if ( c
->dirlevel
< MAX_DIR_LEVELS
)
379 c
->selected_item_history
[c
->dirlevel
] = c
->selected_item
;
384 int seed
= current_tick
;
388 switch ( file
->attr
& FILE_ATTR_MASK
) {
390 play
= ft_play_playlist(buf
, c
->currdir
, file
->name
);
399 case FILE_ATTR_AUDIO
:
400 if (bookmark_autoload(c
->currdir
))
403 gui_syncsplash(0, ID2P(LANG_WAIT
));
405 /* about to create a new current playlist...
406 allow user to cancel the operation */
407 if (global_settings
.warnon_erase_dynplaylist
&&
408 !global_settings
.party_mode
&&
409 playlist_modified(NULL
))
411 char *lines
[]={ID2P(LANG_WARN_ERASEDYNPLAYLIST_PROMPT
)};
412 struct text_message message
={lines
, 1};
414 if(gui_syncyesno_run(&message
, NULL
, NULL
) != YESNO_YES
)
418 if (global_settings
.party_mode
)
420 playlist_insert_track(NULL
, buf
,
421 PLAYLIST_INSERT_LAST
, true, true);
422 gui_syncsplash(HZ
, ID2P(LANG_QUEUE_LAST
));
424 else if (playlist_create(c
->currdir
, NULL
) != -1)
426 start_index
= ft_build_playlist(c
, c
->selected_item
);
427 if (global_settings
.playlist_shuffle
)
429 start_index
= playlist_shuffle(seed
, start_index
);
431 /* when shuffling dir.: play all files
432 even if the file selected by user is
434 if (!global_settings
.play_selected
)
438 playlist_start(start_index
, 0);
444 /* fmr preset file */
447 gui_syncsplash(0, ID2P(LANG_WAIT
));
449 /* Preset inside the default folder. */
450 if(!strncasecmp(FMPRESET_PATH
, buf
, strlen(FMPRESET_PATH
)))
452 set_file(buf
, global_settings
.fmr_file
, MAX_FILENAME
);
453 radio_load_presets(global_settings
.fmr_file
);
454 if(!in_radio_screen())
458 * Preset outside default folder, we can choose such only
459 * if we are out of the radio screen, so the check for the
460 * radio status isn't neccessary
464 radio_load_presets(buf
);
472 /* wps config file */
474 gui_syncsplash(0, ID2P(LANG_WAIT
));
476 unload_wps_backdrop();
478 wps_data_load(gui_wps
[0].data
, &screens
[0], buf
, true);
479 set_file(buf
, (char *)global_settings
.wps_file
,
483 #if defined(HAVE_REMOTE_LCD) && (NB_SCREENS > 1)
484 /* remote-wps config file */
486 gui_syncsplash(0, ID2P(LANG_WAIT
));
487 #if defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1
488 unload_remote_wps_backdrop();
490 wps_data_load(gui_wps
[1].data
, &screens
[1], buf
, true);
491 set_file(buf
, (char *)global_settings
.rwps_file
,
497 gui_syncsplash(0, ID2P(LANG_WAIT
));
498 if (!settings_load_config(buf
,true))
500 gui_syncsplash(HZ
, ID2P(LANG_SETTINGS_LOADED
));
503 case FILE_ATTR_BMARK
:
504 gui_syncsplash(0, ID2P(LANG_WAIT
));
505 bookmark_load(buf
, false);
510 gui_syncsplash(0, ID2P(LANG_WAIT
));
511 if(!lang_load(buf
)) {
512 set_file(buf
, (char *)global_settings
.lang_file
,
514 talk_init(); /* use voice of same language */
515 gui_syncsplash(HZ
, ID2P(LANG_LANGUAGE_LOADED
));
519 #ifdef HAVE_LCD_BITMAP
521 gui_syncsplash(0, ID2P(LANG_WAIT
));
523 set_file(buf
, (char *)global_settings
.font_file
, MAX_FILENAME
);
527 gui_syncsplash(0, ID2P(LANG_WAIT
));
529 gui_syncsplash(HZ
, ID2P(LANG_KEYBOARD_LOADED
));
530 set_file(buf
, (char *)global_settings
.kbd_file
, MAX_FILENAME
);
537 gui_syncsplash(0, ID2P(LANG_WAIT
));
544 if (global_settings
.party_mode
) {
545 gui_syncsplash(HZ
, ID2P(LANG_PARTY_MODE
));
549 gui_syncsplash(0, ID2P(LANG_WAIT
));
551 if (plugin_load(buf
,NULL
) == PLUGIN_USB_CONNECTED
)
553 if(*c
->dirfilter
> NUM_FILTER_MODES
)
554 /* leave sub-browsers after usb, doing
555 otherwise might be confusing to the user */
563 display_cuesheet_content(buf
);
570 if (global_settings
.party_mode
) {
571 gui_syncsplash(HZ
, ID2P(LANG_PARTY_MODE
));
575 plugin
= filetype_get_plugin(file
);
578 if (plugin_load(plugin
,buf
) == PLUGIN_USB_CONNECTED
)
586 /* the resume_index must always be the index in the
587 shuffled list in case shuffle is enabled */
588 global_status
.resume_index
= start_index
;
589 global_status
.resume_offset
= 0;
595 if (*c
->dirfilter
> NUM_FILTER_MODES
&&
596 *c
->dirfilter
!= SHOW_FONT
&&
597 *c
->dirfilter
!= SHOW_PLUGINS
)
614 int ft_exit(struct tree_context
* c
)
616 extern char lastfile
[]; /* from tree.c */
619 bool exit_func
= false;
621 int i
= strlen(c
->currdir
);
623 while (c
->currdir
[i
-1]!='/')
625 strcpy(buf
,&c
->currdir
[i
]);
631 if (*c
->dirfilter
> NUM_FILTER_MODES
&& c
->dirlevel
< 1)
635 if ( c
->dirlevel
< MAX_DIR_LEVELS
)
636 c
->selected_item
=c
->selected_item_history
[c
->dirlevel
];
640 /* if undefined position */
641 if (c
->selected_item
== -1)
642 strcpy(lastfile
, buf
);
646 if (*c
->dirfilter
> NUM_FILTER_MODES
&& c
->dirlevel
< 1)