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
52 #if (LCD_DEPTH > 1) || (defined(HAVE_LCD_REMOTE) && (LCD_REMOTE_DEPTH > 1))
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 DEBUGF("Adding %s\n", dircache
[i
].name
);
68 if (playlist_add(dircache
[i
].name
) < 0)
73 /* Adjust the start index when se skip non-MP3 entries */
82 /* Start playback of a playlist, checking for bookmark autoload, modified
83 * playlists, etc., as required. Returns false if playback wasn't started,
84 * or started via bookmark autoload, true otherwise.
86 * Pointers to both the full pathname and the separated parts needed to
87 * avoid allocating yet another path buffer on the stack (and save some
88 * code; the caller typically needs to create the full pathname anyway)...
90 bool ft_play_playlist(char* pathname
, char* dirname
, char* filename
)
92 if (global_settings
.party_mode
)
94 gui_syncsplash(HZ
, ID2P(LANG_PARTY_MODE
));
98 if (bookmark_autoload(pathname
))
103 gui_syncsplash(0, ID2P(LANG_WAIT
));
105 /* about to create a new current playlist...
106 allow user to cancel the operation */
107 if (global_settings
.warnon_erase_dynplaylist
&&
108 playlist_modified(NULL
))
110 char *lines
[] = {ID2P(LANG_WARN_ERASEDYNPLAYLIST_PROMPT
)};
111 struct text_message message
= {lines
, 1};
113 if (gui_syncyesno_run(&message
, NULL
, NULL
) != YESNO_YES
)
117 if (playlist_create(dirname
, filename
) != -1)
119 if (global_settings
.playlist_shuffle
)
121 playlist_shuffle(current_tick
, -1);
124 playlist_start(0, 0);
131 /* walk a directory and check all dircache entries if a .talk file exists */
132 static void check_file_thumbnails(struct tree_context
* c
)
135 struct dirent
*entry
;
136 struct entry
* dircache
= c
->dircache
;
139 dir
= opendir(c
->currdir
);
142 /* mark all files as non talking, except the .talk ones */
143 for (i
=0; i
< c
->filesindir
; i
++)
145 if (dircache
[i
].attr
& ATTR_DIRECTORY
)
146 continue; /* we're not touching directories */
148 if (strcasecmp(file_thumbnail_ext
,
149 &dircache
[i
].name
[strlen(dircache
[i
].name
)
150 - strlen(file_thumbnail_ext
)]))
151 { /* no .talk file */
152 dircache
[i
].attr
&= ~FILE_ATTR_THUMBNAIL
; /* clear */
155 { /* .talk file, we later let them speak themselves */
156 dircache
[i
].attr
|= FILE_ATTR_THUMBNAIL
; /* set */
160 while((entry
= readdir(dir
)) != 0) /* walk directory */
164 ext_pos
= strlen((char *)entry
->d_name
) - strlen(file_thumbnail_ext
);
165 if (ext_pos
<= 0 /* too short to carry ".talk" */
166 || (entry
->attribute
& ATTR_DIRECTORY
) /* no file */
167 || strcasecmp((char *)&entry
->d_name
[ext_pos
], file_thumbnail_ext
))
168 { /* or doesn't end with ".talk", no candidate */
172 /* terminate the (disposable) name in dir buffer,
173 this truncates off the ".talk" without needing an extra buffer */
174 entry
->d_name
[ext_pos
] = '\0';
176 /* search corresponding file in dir cache */
177 for (i
=0; i
< c
->filesindir
; i
++)
179 if (!strcasecmp(dircache
[i
].name
, (char *)entry
->d_name
))
181 dircache
[i
].attr
|= FILE_ATTR_THUMBNAIL
; /* set the flag */
182 break; /* exit search loop, because we found it */
189 /* support function for qsort() */
190 static int compare(const void* p1
, const void* p2
)
192 struct entry
* e1
= (struct entry
*)p1
;
193 struct entry
* e2
= (struct entry
*)p2
;
196 if (e1
->attr
& ATTR_DIRECTORY
&& e2
->attr
& ATTR_DIRECTORY
)
197 { /* two directories */
198 criteria
= global_settings
.sort_dir
;
200 #ifdef HAVE_MULTIVOLUME
201 if (e1
->attr
& ATTR_VOLUME
|| e2
->attr
& ATTR_VOLUME
)
202 { /* a volume identifier is involved */
203 if (e1
->attr
& ATTR_VOLUME
&& e2
->attr
& ATTR_VOLUME
)
204 criteria
= 0; /* two volumes: sort alphabetically */
205 else /* only one is a volume: volume first */
206 return (e2
->attr
& ATTR_VOLUME
) - (e1
->attr
& ATTR_VOLUME
);
211 else if (!(e1
->attr
& ATTR_DIRECTORY
) && !(e2
->attr
& ATTR_DIRECTORY
))
213 criteria
= global_settings
.sort_file
;
215 else /* dir and file, dir goes first */
216 return ( e2
->attr
& ATTR_DIRECTORY
) - ( e1
->attr
& ATTR_DIRECTORY
);
220 case 3: /* sort type */
222 int t1
= e1
->attr
& FILE_ATTR_MASK
;
223 int t2
= e2
->attr
& FILE_ATTR_MASK
;
225 if (!t1
) /* unknown type */
226 t1
= INT_MAX
; /* gets a high number, to sort after known */
227 if (!t2
) /* unknown type */
228 t2
= INT_MAX
; /* gets a high number, to sort after known */
230 if (t1
- t2
) /* if different */
232 /* else fall through to alphabetical sorting */
234 case 0: /* sort alphabetically asc */
235 if (global_settings
.sort_case
)
236 return strncmp(e1
->name
, e2
->name
, MAX_PATH
);
238 return strncasecmp(e1
->name
, e2
->name
, MAX_PATH
);
240 case 4: /* sort alphabetically desc */
241 if (global_settings
.sort_case
)
242 return strncmp(e2
->name
, e1
->name
, MAX_PATH
);
244 return strncasecmp(e2
->name
, e1
->name
, MAX_PATH
);
246 case 1: /* sort date */
247 return e1
->time_write
- e2
->time_write
;
249 case 2: /* sort date, newest first */
250 return e2
->time_write
- e1
->time_write
;
252 return 0; /* never reached */
255 /* load and sort directory into dircache. returns NULL on failure. */
256 int ft_load(struct tree_context
* c
, const char* tempdir
)
259 int name_buffer_used
= 0;
263 dir
= opendir(tempdir
);
265 dir
= opendir(c
->currdir
);
267 return -1; /* not a directory */
272 for ( i
=0; i
< global_settings
.max_files_in_dir
; i
++ ) {
274 struct dirent
*entry
= readdir(dir
);
276 (struct entry
*)(c
->dircache
+ i
* sizeof(struct entry
));
280 len
= strlen((char *)entry
->d_name
);
282 /* skip directories . and .. */
283 if ((entry
->attribute
& ATTR_DIRECTORY
) &&
284 (((len
== 1) && (!strncmp((char *)entry
->d_name
, ".", 1))) ||
285 ((len
== 2) && (!strncmp((char *)entry
->d_name
, "..", 2))))) {
290 /* Skip FAT volume ID */
291 if (entry
->attribute
& ATTR_VOLUME_ID
) {
296 /* filter out dotfiles and hidden files */
297 if (*c
->dirfilter
!= SHOW_ALL
&&
298 ((entry
->d_name
[0]=='.') ||
299 (entry
->attribute
& ATTR_HIDDEN
))) {
304 dptr
->attr
= entry
->attribute
;
306 /* check for known file types */
307 if ( !(dptr
->attr
& ATTR_DIRECTORY
) )
308 dptr
->attr
|= filetype_get_attr((char *)entry
->d_name
);
310 /* filter out non-visible files */
311 if ((!(dptr
->attr
& ATTR_DIRECTORY
) && (
312 (*c
->dirfilter
== SHOW_PLAYLIST
&&
313 (dptr
->attr
& FILE_ATTR_MASK
) != FILE_ATTR_M3U
) ||
314 ((*c
->dirfilter
== SHOW_MUSIC
&&
315 (dptr
->attr
& FILE_ATTR_MASK
) != FILE_ATTR_AUDIO
) &&
316 (dptr
->attr
& FILE_ATTR_MASK
) != FILE_ATTR_M3U
) ||
317 (*c
->dirfilter
== SHOW_SUPPORTED
&& !filetype_supported(dptr
->attr
)))) ||
318 (*c
->dirfilter
== SHOW_WPS
&& (dptr
->attr
& FILE_ATTR_MASK
) != FILE_ATTR_WPS
) ||
319 #ifdef HAVE_REMOTE_LCD
320 (*c
->dirfilter
== SHOW_RWPS
&& (dptr
->attr
& FILE_ATTR_MASK
) != FILE_ATTR_RWPS
) ||
323 (*c
->dirfilter
== SHOW_FMR
&& (dptr
->attr
& FILE_ATTR_MASK
) != FILE_ATTR_FMR
) ||
325 (*c
->dirfilter
== SHOW_CFG
&& (dptr
->attr
& FILE_ATTR_MASK
) != FILE_ATTR_CFG
) ||
326 (*c
->dirfilter
== SHOW_LNG
&& (dptr
->attr
& FILE_ATTR_MASK
) != FILE_ATTR_LNG
) ||
327 (*c
->dirfilter
== SHOW_MOD
&& (dptr
->attr
& FILE_ATTR_MASK
) != FILE_ATTR_MOD
) ||
328 (*c
->dirfilter
== SHOW_FONT
&& (dptr
->attr
& FILE_ATTR_MASK
) != FILE_ATTR_FONT
) ||
329 (*c
->dirfilter
== SHOW_PLUGINS
&& (dptr
->attr
& FILE_ATTR_MASK
) != FILE_ATTR_ROCK
))
335 if (len
> c
->name_buffer_size
- name_buffer_used
- 1) {
336 /* Tell the world that we ran out of buffer space */
340 dptr
->name
= &c
->name_buffer
[name_buffer_used
];
342 (long)entry
->wrtdate
<<16 |
343 (long)entry
->wrttime
; /* in one # */
344 strcpy(dptr
->name
, (char *)entry
->d_name
);
345 name_buffer_used
+= len
+ 1;
347 if (dptr
->attr
& ATTR_DIRECTORY
) /* count the remaining dirs */
354 qsort(c
->dircache
,i
,sizeof(struct entry
),compare
);
356 /* If thumbnail talking is enabled, make an extra run to mark files with
357 associated thumbnails, so we don't do unsuccessful spinups later. */
358 if (global_settings
.talk_file_clip
)
359 check_file_thumbnails(c
); /* map .talk to ours */
364 int ft_enter(struct tree_context
* c
)
368 struct entry
*dircache
= c
->dircache
;
369 struct entry
* file
= &dircache
[c
->selected_item
];
370 bool reload_dir
= false;
371 bool start_wps
= false;
372 bool exit_func
= false;
375 snprintf(buf
,sizeof(buf
),"%s/%s",c
->currdir
, file
->name
);
377 snprintf(buf
,sizeof(buf
),"/%s",file
->name
);
379 if (file
->attr
& ATTR_DIRECTORY
) {
380 memcpy(c
->currdir
, buf
, sizeof(c
->currdir
));
381 if ( c
->dirlevel
< MAX_DIR_LEVELS
)
382 c
->selected_item_history
[c
->dirlevel
] = c
->selected_item
;
387 int seed
= current_tick
;
391 switch ( file
->attr
& FILE_ATTR_MASK
) {
393 play
= ft_play_playlist(buf
, c
->currdir
, file
->name
);
402 case FILE_ATTR_AUDIO
:
403 if (bookmark_autoload(c
->currdir
))
406 gui_syncsplash(0, ID2P(LANG_WAIT
));
408 /* about to create a new current playlist...
409 allow user to cancel the operation */
410 if (global_settings
.warnon_erase_dynplaylist
&&
411 !global_settings
.party_mode
&&
412 playlist_modified(NULL
))
414 char *lines
[]={ID2P(LANG_WARN_ERASEDYNPLAYLIST_PROMPT
)};
415 struct text_message message
={lines
, 1};
417 if(gui_syncyesno_run(&message
, NULL
, NULL
) != YESNO_YES
)
421 if (global_settings
.party_mode
)
423 playlist_insert_track(NULL
, buf
,
424 PLAYLIST_INSERT_LAST
, true, true);
425 gui_syncsplash(HZ
, ID2P(LANG_QUEUE_LAST
));
427 else if (playlist_create(c
->currdir
, NULL
) != -1)
429 start_index
= ft_build_playlist(c
, c
->selected_item
);
430 if (global_settings
.playlist_shuffle
)
432 start_index
= playlist_shuffle(seed
, start_index
);
434 /* when shuffling dir.: play all files
435 even if the file selected by user is
437 if (!global_settings
.play_selected
)
441 playlist_start(start_index
, 0);
447 /* fmr preset file */
450 gui_syncsplash(0, ID2P(LANG_WAIT
));
452 /* Preset inside the default folder. */
453 if(!strncasecmp(FMPRESET_PATH
, buf
, strlen(FMPRESET_PATH
)))
455 set_file(buf
, global_settings
.fmr_file
, MAX_FILENAME
);
456 radio_load_presets(global_settings
.fmr_file
);
457 if(!in_radio_screen())
461 * Preset outside default folder, we can choose such only
462 * if we are out of the radio screen, so the check for the
463 * radio status isn't neccessary
467 radio_load_presets(buf
);
475 /* wps config file */
477 gui_syncsplash(0, ID2P(LANG_WAIT
));
479 unload_wps_backdrop();
481 wps_data_load(gui_wps
[0].data
, buf
, true);
482 set_file(buf
, (char *)global_settings
.wps_file
,
486 #if defined(HAVE_REMOTE_LCD) && (NB_SCREENS > 1)
487 /* remote-wps config file */
489 gui_syncsplash(0, ID2P(LANG_WAIT
));
490 #if defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1
491 unload_remote_wps_backdrop();
493 wps_data_load(gui_wps
[1].data
, buf
, true);
494 set_file(buf
, (char *)global_settings
.rwps_file
,
500 gui_syncsplash(0, ID2P(LANG_WAIT
));
501 if (!settings_load_config(buf
,true))
503 gui_syncsplash(HZ
, ID2P(LANG_SETTINGS_LOADED
));
506 case FILE_ATTR_BMARK
:
507 gui_syncsplash(0, ID2P(LANG_WAIT
));
508 bookmark_load(buf
, false);
513 gui_syncsplash(0, ID2P(LANG_WAIT
));
514 if(!lang_load(buf
)) {
515 set_file(buf
, (char *)global_settings
.lang_file
,
517 talk_init(); /* use voice of same language */
518 gui_syncsplash(HZ
, ID2P(LANG_LANGUAGE_LOADED
));
522 #ifdef HAVE_LCD_BITMAP
524 gui_syncsplash(0, ID2P(LANG_WAIT
));
526 set_file(buf
, (char *)global_settings
.font_file
, MAX_FILENAME
);
530 gui_syncsplash(0, ID2P(LANG_WAIT
));
532 gui_syncsplash(HZ
, ID2P(LANG_KEYBOARD_LOADED
));
533 set_file(buf
, (char *)global_settings
.kbd_file
, MAX_FILENAME
);
540 gui_syncsplash(0, ID2P(LANG_WAIT
));
547 if (global_settings
.party_mode
) {
548 gui_syncsplash(HZ
, ID2P(LANG_PARTY_MODE
));
552 gui_syncsplash(0, ID2P(LANG_WAIT
));
554 if (plugin_load(buf
,NULL
) == PLUGIN_USB_CONNECTED
)
556 if(*c
->dirfilter
> NUM_FILTER_MODES
)
557 /* leave sub-browsers after usb, doing
558 otherwise might be confusing to the user */
566 display_cuesheet_content(buf
);
573 if (global_settings
.party_mode
) {
574 gui_syncsplash(HZ
, ID2P(LANG_PARTY_MODE
));
578 plugin
= filetype_get_plugin(file
);
581 if (plugin_load(plugin
,buf
) == PLUGIN_USB_CONNECTED
)
589 /* the resume_index must always be the index in the
590 shuffled list in case shuffle is enabled */
591 global_status
.resume_index
= start_index
;
592 global_status
.resume_offset
= 0;
598 if (*c
->dirfilter
> NUM_FILTER_MODES
&&
599 *c
->dirfilter
!= SHOW_FONT
&&
600 *c
->dirfilter
!= SHOW_PLUGINS
)
617 int ft_exit(struct tree_context
* c
)
619 extern char lastfile
[]; /* from tree.c */
622 bool exit_func
= false;
624 int i
= strlen(c
->currdir
);
626 while (c
->currdir
[i
-1]!='/')
628 strcpy(buf
,&c
->currdir
[i
]);
634 if (*c
->dirfilter
> NUM_FILTER_MODES
&& c
->dirlevel
< 1)
638 if ( c
->dirlevel
< MAX_DIR_LEVELS
)
639 c
->selected_item
=c
->selected_item_history
[c
->dirlevel
];
643 /* if undefined position */
644 if (c
->selected_item
== -1)
645 strcpy(lastfile
, buf
);
649 if (*c
->dirfilter
> NUM_FILTER_MODES
&& c
->dirlevel
< 1)