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 DEBUGF("Adding %s\n", dircache
[i
].name
);
66 if (playlist_add(dircache
[i
].name
) < 0)
71 /* Adjust the start index when se skip non-MP3 entries */
80 /* Start playback of a playlist, checking for bookmark autoload, modified
81 * playlists, etc., as required. Returns false if playback wasn't started,
82 * or started via bookmark autoload, true otherwise.
84 * Pointers to both the full pathname and the separated parts needed to
85 * avoid allocating yet another path buffer on the stack (and save some
86 * code; the caller typically needs to create the full pathname anyway)...
88 bool ft_play_playlist(char* pathname
, char* dirname
, char* filename
)
90 if (global_settings
.party_mode
)
92 gui_syncsplash(HZ
, ID2P(LANG_PARTY_MODE
));
96 if (bookmark_autoload(pathname
))
101 gui_syncsplash(0, ID2P(LANG_WAIT
));
103 /* about to create a new current playlist...
104 allow user to cancel the operation */
105 if (global_settings
.warnon_erase_dynplaylist
&&
106 playlist_modified(NULL
))
108 char *lines
[] = {ID2P(LANG_WARN_ERASEDYNPLAYLIST_PROMPT
)};
109 struct text_message message
= {lines
, 1};
111 if (gui_syncyesno_run(&message
, NULL
, NULL
) != YESNO_YES
)
115 if (playlist_create(dirname
, filename
) != -1)
117 if (global_settings
.playlist_shuffle
)
119 playlist_shuffle(current_tick
, -1);
122 playlist_start(0, 0);
129 /* walk a directory and check all dircache entries if a .talk file exists */
130 static void check_file_thumbnails(struct tree_context
* c
)
133 struct dirent
*entry
;
134 struct entry
* dircache
= c
->dircache
;
137 dir
= opendir(c
->currdir
);
140 /* mark all files as non talking, except the .talk ones */
141 for (i
=0; i
< c
->filesindir
; i
++)
143 if (dircache
[i
].attr
& ATTR_DIRECTORY
)
144 continue; /* we're not touching directories */
146 if (strcasecmp(file_thumbnail_ext
,
147 &dircache
[i
].name
[strlen(dircache
[i
].name
)
148 - strlen(file_thumbnail_ext
)]))
149 { /* no .talk file */
150 dircache
[i
].attr
&= ~FILE_ATTR_THUMBNAIL
; /* clear */
153 { /* .talk file, we later let them speak themselves */
154 dircache
[i
].attr
|= FILE_ATTR_THUMBNAIL
; /* set */
158 while((entry
= readdir(dir
)) != 0) /* walk directory */
162 ext_pos
= strlen((char *)entry
->d_name
) - strlen(file_thumbnail_ext
);
163 if (ext_pos
<= 0 /* too short to carry ".talk" */
164 || (entry
->attribute
& ATTR_DIRECTORY
) /* no file */
165 || strcasecmp((char *)&entry
->d_name
[ext_pos
], file_thumbnail_ext
))
166 { /* or doesn't end with ".talk", no candidate */
170 /* terminate the (disposable) name in dir buffer,
171 this truncates off the ".talk" without needing an extra buffer */
172 entry
->d_name
[ext_pos
] = '\0';
174 /* search corresponding file in dir cache */
175 for (i
=0; i
< c
->filesindir
; i
++)
177 if (!strcasecmp(dircache
[i
].name
, (char *)entry
->d_name
))
179 dircache
[i
].attr
|= FILE_ATTR_THUMBNAIL
; /* set the flag */
180 break; /* exit search loop, because we found it */
187 /* support function for qsort() */
188 static int compare(const void* p1
, const void* p2
)
190 struct entry
* e1
= (struct entry
*)p1
;
191 struct entry
* e2
= (struct entry
*)p2
;
194 if (e1
->attr
& ATTR_DIRECTORY
&& e2
->attr
& ATTR_DIRECTORY
)
195 { /* two directories */
196 criteria
= global_settings
.sort_dir
;
198 #ifdef HAVE_MULTIVOLUME
199 if (e1
->attr
& ATTR_VOLUME
|| e2
->attr
& ATTR_VOLUME
)
200 { /* a volume identifier is involved */
201 if (e1
->attr
& ATTR_VOLUME
&& e2
->attr
& ATTR_VOLUME
)
202 criteria
= 0; /* two volumes: sort alphabetically */
203 else /* only one is a volume: volume first */
204 return (e2
->attr
& ATTR_VOLUME
) - (e1
->attr
& ATTR_VOLUME
);
209 else if (!(e1
->attr
& ATTR_DIRECTORY
) && !(e2
->attr
& ATTR_DIRECTORY
))
211 criteria
= global_settings
.sort_file
;
213 else /* dir and file, dir goes first */
214 return ( e2
->attr
& ATTR_DIRECTORY
) - ( e1
->attr
& ATTR_DIRECTORY
);
218 case 3: /* sort type */
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 */
230 /* else fall through to alphabetical sorting */
232 case 0: /* sort alphabetically asc */
233 if (global_settings
.sort_case
)
234 return strncmp(e1
->name
, e2
->name
, MAX_PATH
);
236 return strncasecmp(e1
->name
, e2
->name
, MAX_PATH
);
238 case 4: /* sort alphabetically desc */
239 if (global_settings
.sort_case
)
240 return strncmp(e2
->name
, e1
->name
, MAX_PATH
);
242 return strncasecmp(e2
->name
, e1
->name
, MAX_PATH
);
244 case 1: /* sort date */
245 return e1
->time_write
- e2
->time_write
;
247 case 2: /* sort date, newest first */
248 return e2
->time_write
- e1
->time_write
;
250 return 0; /* never reached */
253 /* load and sort directory into dircache. returns NULL on failure. */
254 int ft_load(struct tree_context
* c
, const char* tempdir
)
257 int name_buffer_used
= 0;
261 dir
= opendir(tempdir
);
263 dir
= opendir(c
->currdir
);
265 return -1; /* not a directory */
270 for ( i
=0; i
< global_settings
.max_files_in_dir
; i
++ ) {
272 struct dirent
*entry
= readdir(dir
);
274 (struct entry
*)(c
->dircache
+ i
* sizeof(struct entry
));
278 len
= strlen((char *)entry
->d_name
);
280 /* skip directories . and .. */
281 if ((entry
->attribute
& ATTR_DIRECTORY
) &&
282 (((len
== 1) && (!strncmp((char *)entry
->d_name
, ".", 1))) ||
283 ((len
== 2) && (!strncmp((char *)entry
->d_name
, "..", 2))))) {
288 /* Skip FAT volume ID */
289 if (entry
->attribute
& ATTR_VOLUME_ID
) {
294 /* filter out dotfiles and hidden files */
295 if (*c
->dirfilter
!= SHOW_ALL
&&
296 ((entry
->d_name
[0]=='.') ||
297 (entry
->attribute
& ATTR_HIDDEN
))) {
302 dptr
->attr
= entry
->attribute
;
304 /* check for known file types */
305 if ( !(dptr
->attr
& ATTR_DIRECTORY
) )
306 dptr
->attr
|= filetype_get_attr((char *)entry
->d_name
);
308 /* filter out non-visible files */
309 if ((!(dptr
->attr
& ATTR_DIRECTORY
) && (
310 (*c
->dirfilter
== SHOW_PLAYLIST
&&
311 (dptr
->attr
& FILE_ATTR_MASK
) != FILE_ATTR_M3U
) ||
312 ((*c
->dirfilter
== SHOW_MUSIC
&&
313 (dptr
->attr
& FILE_ATTR_MASK
) != FILE_ATTR_AUDIO
) &&
314 (dptr
->attr
& FILE_ATTR_MASK
) != FILE_ATTR_M3U
) ||
315 (*c
->dirfilter
== SHOW_SUPPORTED
&& !filetype_supported(dptr
->attr
)))) ||
316 (*c
->dirfilter
== SHOW_WPS
&& (dptr
->attr
& FILE_ATTR_MASK
) != FILE_ATTR_WPS
) ||
317 #ifdef HAVE_REMOTE_LCD
318 (*c
->dirfilter
== SHOW_RWPS
&& (dptr
->attr
& FILE_ATTR_MASK
) != FILE_ATTR_RWPS
) ||
321 (*c
->dirfilter
== SHOW_FMR
&& (dptr
->attr
& FILE_ATTR_MASK
) != FILE_ATTR_FMR
) ||
323 (*c
->dirfilter
== SHOW_CFG
&& (dptr
->attr
& FILE_ATTR_MASK
) != FILE_ATTR_CFG
) ||
324 (*c
->dirfilter
== SHOW_LNG
&& (dptr
->attr
& FILE_ATTR_MASK
) != FILE_ATTR_LNG
) ||
325 (*c
->dirfilter
== SHOW_MOD
&& (dptr
->attr
& FILE_ATTR_MASK
) != FILE_ATTR_MOD
) ||
326 (*c
->dirfilter
== SHOW_FONT
&& (dptr
->attr
& FILE_ATTR_MASK
) != FILE_ATTR_FONT
) ||
327 (*c
->dirfilter
== SHOW_PLUGINS
&& (dptr
->attr
& FILE_ATTR_MASK
) != FILE_ATTR_ROCK
))
333 if (len
> c
->name_buffer_size
- name_buffer_used
- 1) {
334 /* Tell the world that we ran out of buffer space */
338 dptr
->name
= &c
->name_buffer
[name_buffer_used
];
340 (long)entry
->wrtdate
<<16 |
341 (long)entry
->wrttime
; /* in one # */
342 strcpy(dptr
->name
, (char *)entry
->d_name
);
343 name_buffer_used
+= len
+ 1;
345 if (dptr
->attr
& ATTR_DIRECTORY
) /* count the remaining dirs */
352 qsort(c
->dircache
,i
,sizeof(struct entry
),compare
);
354 /* If thumbnail talking is enabled, make an extra run to mark files with
355 associated thumbnails, so we don't do unsuccessful spinups later. */
356 if (global_settings
.talk_file_clip
)
357 check_file_thumbnails(c
); /* map .talk to ours */
362 int ft_enter(struct tree_context
* c
)
366 struct entry
*dircache
= c
->dircache
;
367 struct entry
* file
= &dircache
[c
->selected_item
];
368 bool reload_dir
= false;
369 bool start_wps
= false;
370 bool exit_func
= false;
373 snprintf(buf
,sizeof(buf
),"%s/%s",c
->currdir
, file
->name
);
375 snprintf(buf
,sizeof(buf
),"/%s",file
->name
);
377 if (file
->attr
& ATTR_DIRECTORY
) {
378 memcpy(c
->currdir
, buf
, sizeof(c
->currdir
));
379 if ( c
->dirlevel
< MAX_DIR_LEVELS
)
380 c
->selected_item_history
[c
->dirlevel
] = c
->selected_item
;
385 int seed
= current_tick
;
389 switch ( file
->attr
& FILE_ATTR_MASK
) {
391 play
= ft_play_playlist(buf
, c
->currdir
, file
->name
);
400 case FILE_ATTR_AUDIO
:
401 if (bookmark_autoload(c
->currdir
))
404 gui_syncsplash(0, ID2P(LANG_WAIT
));
406 /* about to create a new current playlist...
407 allow user to cancel the operation */
408 if (global_settings
.warnon_erase_dynplaylist
&&
409 !global_settings
.party_mode
&&
410 playlist_modified(NULL
))
412 char *lines
[]={ID2P(LANG_WARN_ERASEDYNPLAYLIST_PROMPT
)};
413 struct text_message message
={lines
, 1};
415 if(gui_syncyesno_run(&message
, NULL
, NULL
) != YESNO_YES
)
419 if (global_settings
.party_mode
)
421 playlist_insert_track(NULL
, buf
,
422 PLAYLIST_INSERT_LAST
, true, true);
423 gui_syncsplash(HZ
, ID2P(LANG_QUEUE_LAST
));
425 else if (playlist_create(c
->currdir
, NULL
) != -1)
427 start_index
= ft_build_playlist(c
, c
->selected_item
);
428 if (global_settings
.playlist_shuffle
)
430 start_index
= playlist_shuffle(seed
, start_index
);
432 /* when shuffling dir.: play all files
433 even if the file selected by user is
435 if (!global_settings
.play_selected
)
439 playlist_start(start_index
, 0);
445 /* fmr preset file */
448 gui_syncsplash(0, ID2P(LANG_WAIT
));
450 /* Preset inside the default folder. */
451 if(!strncasecmp(FMPRESET_PATH
, buf
, strlen(FMPRESET_PATH
)))
453 set_file(buf
, global_settings
.fmr_file
, MAX_FILENAME
);
454 radio_load_presets(global_settings
.fmr_file
);
455 if(!in_radio_screen())
459 * Preset outside default folder, we can choose such only
460 * if we are out of the radio screen, so the check for the
461 * radio status isn't neccessary
465 radio_load_presets(buf
);
473 /* wps config file */
475 gui_syncsplash(0, ID2P(LANG_WAIT
));
477 unload_wps_backdrop();
479 wps_data_load(gui_wps
[0].data
, &screens
[0], buf
, true);
480 set_file(buf
, (char *)global_settings
.wps_file
,
484 #if defined(HAVE_REMOTE_LCD) && (NB_SCREENS > 1)
485 /* remote-wps config file */
487 gui_syncsplash(0, ID2P(LANG_WAIT
));
488 #if defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1
489 unload_remote_wps_backdrop();
491 wps_data_load(gui_wps
[1].data
, &screens
[1], buf
, true);
492 set_file(buf
, (char *)global_settings
.rwps_file
,
498 gui_syncsplash(0, ID2P(LANG_WAIT
));
499 if (!settings_load_config(buf
,true))
501 gui_syncsplash(HZ
, ID2P(LANG_SETTINGS_LOADED
));
504 case FILE_ATTR_BMARK
:
505 gui_syncsplash(0, ID2P(LANG_WAIT
));
506 bookmark_load(buf
, false);
511 gui_syncsplash(0, ID2P(LANG_WAIT
));
512 if(!lang_load(buf
)) {
513 set_file(buf
, (char *)global_settings
.lang_file
,
515 talk_init(); /* use voice of same language */
516 gui_syncsplash(HZ
, ID2P(LANG_LANGUAGE_LOADED
));
520 #ifdef HAVE_LCD_BITMAP
522 gui_syncsplash(0, ID2P(LANG_WAIT
));
524 set_file(buf
, (char *)global_settings
.font_file
, MAX_FILENAME
);
528 gui_syncsplash(0, ID2P(LANG_WAIT
));
530 gui_syncsplash(HZ
, ID2P(LANG_KEYBOARD_LOADED
));
531 set_file(buf
, (char *)global_settings
.kbd_file
, MAX_FILENAME
);
538 gui_syncsplash(0, ID2P(LANG_WAIT
));
545 if (global_settings
.party_mode
) {
546 gui_syncsplash(HZ
, ID2P(LANG_PARTY_MODE
));
550 gui_syncsplash(0, ID2P(LANG_WAIT
));
552 if (plugin_load(buf
,NULL
) == PLUGIN_USB_CONNECTED
)
554 if(*c
->dirfilter
> NUM_FILTER_MODES
)
555 /* leave sub-browsers after usb, doing
556 otherwise might be confusing to the user */
564 display_cuesheet_content(buf
);
571 if (global_settings
.party_mode
) {
572 gui_syncsplash(HZ
, ID2P(LANG_PARTY_MODE
));
576 plugin
= filetype_get_plugin(file
);
579 if (plugin_load(plugin
,buf
) == PLUGIN_USB_CONNECTED
)
587 /* the resume_index must always be the index in the
588 shuffled list in case shuffle is enabled */
589 global_status
.resume_index
= start_index
;
590 global_status
.resume_offset
= 0;
596 if (*c
->dirfilter
> NUM_FILTER_MODES
&&
597 *c
->dirfilter
!= SHOW_FONT
&&
598 *c
->dirfilter
!= SHOW_PLUGINS
)
615 int ft_exit(struct tree_context
* c
)
617 extern char lastfile
[]; /* from tree.c */
620 bool exit_func
= false;
622 int i
= strlen(c
->currdir
);
624 while (c
->currdir
[i
-1]!='/')
626 strcpy(buf
,&c
->currdir
[i
]);
632 if (*c
->dirfilter
> NUM_FILTER_MODES
&& c
->dirlevel
< 1)
636 if ( c
->dirlevel
< MAX_DIR_LEVELS
)
637 c
->selected_item
=c
->selected_item_history
[c
->dirlevel
];
641 /* if undefined position */
642 if (c
->selected_item
== -1)
643 strcpy(lastfile
, buf
);
647 if (*c
->dirfilter
> NUM_FILTER_MODES
&& c
->dirlevel
< 1)