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"
45 #ifdef HAVE_LCD_BITMAP
53 #if (LCD_DEPTH > 1) || (defined(HAVE_LCD_REMOTE) && (LCD_REMOTE_DEPTH > 1))
57 int ft_build_playlist(struct tree_context
* c
, int start_index
)
60 int start
=start_index
;
62 struct entry
*dircache
= c
->dircache
;
64 for(i
= 0;i
< c
->filesindir
;i
++)
66 if((dircache
[i
].attr
& FILE_ATTR_MASK
) == FILE_ATTR_AUDIO
)
68 DEBUGF("Adding %s\n", dircache
[i
].name
);
69 if (playlist_add(dircache
[i
].name
) < 0)
74 /* Adjust the start index when se skip non-MP3 entries */
83 /* walk a directory and check all dircache entries if a .talk file exists */
84 static void check_file_thumbnails(struct tree_context
* c
)
87 struct dircache_entry
*entry
;
88 struct entry
* dircache
= c
->dircache
;
91 dir
= opendir_cached(c
->currdir
);
94 /* mark all files as non talking, except the .talk ones */
95 for (i
=0; i
< c
->filesindir
; i
++)
97 if (dircache
[i
].attr
& ATTR_DIRECTORY
)
98 continue; /* we're not touching directories */
100 if (strcasecmp(file_thumbnail_ext
,
101 &dircache
[i
].name
[strlen(dircache
[i
].name
)
102 - strlen(file_thumbnail_ext
)]))
103 { /* no .talk file */
104 dircache
[i
].attr
&= ~FILE_ATTR_THUMBNAIL
; /* clear */
107 { /* .talk file, we later let them speak themselves */
108 dircache
[i
].attr
|= FILE_ATTR_THUMBNAIL
; /* set */
112 while((entry
= readdir_cached(dir
)) != 0) /* walk directory */
116 ext_pos
= strlen((char *)entry
->d_name
) - strlen(file_thumbnail_ext
);
117 if (ext_pos
<= 0 /* too short to carry ".talk" */
118 || (entry
->attribute
& ATTR_DIRECTORY
) /* no file */
119 || strcasecmp((char *)&entry
->d_name
[ext_pos
], file_thumbnail_ext
))
120 { /* or doesn't end with ".talk", no candidate */
124 /* terminate the (disposable) name in dir buffer,
125 this truncates off the ".talk" without needing an extra buffer */
126 entry
->d_name
[ext_pos
] = '\0';
128 /* search corresponding file in dir cache */
129 for (i
=0; i
< c
->filesindir
; i
++)
131 if (!strcasecmp(dircache
[i
].name
, (char *)entry
->d_name
))
133 dircache
[i
].attr
|= FILE_ATTR_THUMBNAIL
; /* set the flag */
134 break; /* exit search loop, because we found it */
138 closedir_cached(dir
);
141 /* support function for qsort() */
142 static int compare(const void* p1
, const void* p2
)
144 struct entry
* e1
= (struct entry
*)p1
;
145 struct entry
* e2
= (struct entry
*)p2
;
148 if (e1
->attr
& ATTR_DIRECTORY
&& e2
->attr
& ATTR_DIRECTORY
)
149 { /* two directories */
150 criteria
= global_settings
.sort_dir
;
152 #ifdef HAVE_MULTIVOLUME
153 if (e1
->attr
& ATTR_VOLUME
|| e2
->attr
& ATTR_VOLUME
)
154 { /* a volume identifier is involved */
155 if (e1
->attr
& ATTR_VOLUME
&& e2
->attr
& ATTR_VOLUME
)
156 criteria
= 0; /* two volumes: sort alphabetically */
157 else /* only one is a volume: volume first */
158 return (e2
->attr
& ATTR_VOLUME
) - (e1
->attr
& ATTR_VOLUME
);
163 else if (!(e1
->attr
& ATTR_DIRECTORY
) && !(e2
->attr
& ATTR_DIRECTORY
))
165 criteria
= global_settings
.sort_file
;
167 else /* dir and file, dir goes first */
168 return ( e2
->attr
& ATTR_DIRECTORY
) - ( e1
->attr
& ATTR_DIRECTORY
);
172 case 3: /* sort type */
174 int t1
= e1
->attr
& FILE_ATTR_MASK
;
175 int t2
= e2
->attr
& FILE_ATTR_MASK
;
177 if (!t1
) /* unknown type */
178 t1
= INT_MAX
; /* gets a high number, to sort after known */
179 if (!t2
) /* unknown type */
180 t2
= INT_MAX
; /* gets a high number, to sort after known */
182 if (t1
- t2
) /* if different */
184 /* else fall through to alphabetical sorting */
186 case 0: /* sort alphabetically asc */
187 if (global_settings
.sort_case
)
188 return strncmp(e1
->name
, e2
->name
, MAX_PATH
);
190 return strncasecmp(e1
->name
, e2
->name
, MAX_PATH
);
192 case 4: /* sort alphabetically desc */
193 if (global_settings
.sort_case
)
194 return strncmp(e2
->name
, e1
->name
, MAX_PATH
);
196 return strncasecmp(e2
->name
, e1
->name
, MAX_PATH
);
198 case 1: /* sort date */
199 return e1
->time_write
- e2
->time_write
;
201 case 2: /* sort date, newest first */
202 return e2
->time_write
- e1
->time_write
;
204 return 0; /* never reached */
207 /* load and sort directory into dircache. returns NULL on failure. */
208 int ft_load(struct tree_context
* c
, const char* tempdir
)
211 int name_buffer_used
= 0;
215 dir
= opendir_cached(tempdir
);
217 dir
= opendir_cached(c
->currdir
);
219 return -1; /* not a directory */
224 for ( i
=0; i
< global_settings
.max_files_in_dir
; i
++ ) {
226 struct dircache_entry
*entry
= readdir_cached(dir
);
228 (struct entry
*)(c
->dircache
+ i
* sizeof(struct entry
));
232 len
= strlen((char *)entry
->d_name
);
234 /* skip directories . and .. */
235 if ((entry
->attribute
& ATTR_DIRECTORY
) &&
236 (((len
== 1) && (!strncmp((char *)entry
->d_name
, ".", 1))) ||
237 ((len
== 2) && (!strncmp((char *)entry
->d_name
, "..", 2))))) {
242 /* Skip FAT volume ID */
243 if (entry
->attribute
& ATTR_VOLUME_ID
) {
248 /* filter out dotfiles and hidden files */
249 if (*c
->dirfilter
!= SHOW_ALL
&&
250 ((entry
->d_name
[0]=='.') ||
251 (entry
->attribute
& ATTR_HIDDEN
))) {
256 dptr
->attr
= entry
->attribute
;
258 /* check for known file types */
259 if ( !(dptr
->attr
& ATTR_DIRECTORY
) )
260 dptr
->attr
|= filetype_get_attr((char *)entry
->d_name
);
262 /* filter out non-visible files */
263 if ((!(dptr
->attr
& ATTR_DIRECTORY
) && (
264 (*c
->dirfilter
== SHOW_PLAYLIST
&&
265 (dptr
->attr
& FILE_ATTR_MASK
) != FILE_ATTR_M3U
) ||
266 ((*c
->dirfilter
== SHOW_MUSIC
&&
267 (dptr
->attr
& FILE_ATTR_MASK
) != FILE_ATTR_AUDIO
) &&
268 (dptr
->attr
& FILE_ATTR_MASK
) != FILE_ATTR_M3U
) ||
269 (*c
->dirfilter
== SHOW_SUPPORTED
&& !filetype_supported(dptr
->attr
)))) ||
270 (*c
->dirfilter
== SHOW_WPS
&& (dptr
->attr
& FILE_ATTR_MASK
) != FILE_ATTR_WPS
) ||
271 #ifdef HAVE_REMOTE_LCD
272 (*c
->dirfilter
== SHOW_RWPS
&& (dptr
->attr
& FILE_ATTR_MASK
) != FILE_ATTR_RWPS
) ||
275 (*c
->dirfilter
== SHOW_FMR
&& (dptr
->attr
& FILE_ATTR_MASK
) != FILE_ATTR_FMR
) ||
277 (*c
->dirfilter
== SHOW_CFG
&& (dptr
->attr
& FILE_ATTR_MASK
) != FILE_ATTR_CFG
) ||
278 (*c
->dirfilter
== SHOW_LNG
&& (dptr
->attr
& FILE_ATTR_MASK
) != FILE_ATTR_LNG
) ||
279 (*c
->dirfilter
== SHOW_MOD
&& (dptr
->attr
& FILE_ATTR_MASK
) != FILE_ATTR_MOD
) ||
280 (*c
->dirfilter
== SHOW_FONT
&& (dptr
->attr
& FILE_ATTR_MASK
) != FILE_ATTR_FONT
) ||
281 (*c
->dirfilter
== SHOW_PLUGINS
&& (dptr
->attr
& FILE_ATTR_MASK
) != FILE_ATTR_ROCK
))
287 if (len
> c
->name_buffer_size
- name_buffer_used
- 1) {
288 /* Tell the world that we ran out of buffer space */
292 dptr
->name
= &c
->name_buffer
[name_buffer_used
];
294 (long)entry
->wrtdate
<<16 |
295 (long)entry
->wrttime
; /* in one # */
296 strcpy(dptr
->name
, (char *)entry
->d_name
);
297 name_buffer_used
+= len
+ 1;
299 if (dptr
->attr
& ATTR_DIRECTORY
) /* count the remaining dirs */
304 closedir_cached(dir
);
306 qsort(c
->dircache
,i
,sizeof(struct entry
),compare
);
308 /* If thumbnail talking is enabled, make an extra run to mark files with
309 associated thumbnails, so we don't do unsuccessful spinups later. */
310 if (global_settings
.talk_file_clip
)
311 check_file_thumbnails(c
); /* map .talk to ours */
316 int ft_enter(struct tree_context
* c
)
320 struct entry
*dircache
= c
->dircache
;
321 struct entry
* file
= &dircache
[c
->selected_item
];
322 bool reload_dir
= false;
323 bool start_wps
= false;
324 bool exit_func
= false;
327 snprintf(buf
,sizeof(buf
),"%s/%s",c
->currdir
, file
->name
);
329 snprintf(buf
,sizeof(buf
),"/%s",file
->name
);
331 if (file
->attr
& ATTR_DIRECTORY
) {
332 memcpy(c
->currdir
, buf
, sizeof(c
->currdir
));
333 if ( c
->dirlevel
< MAX_DIR_LEVELS
)
334 c
->selected_item_history
[c
->dirlevel
] = c
->selected_item
;
339 int seed
= current_tick
;
343 switch ( file
->attr
& FILE_ATTR_MASK
) {
345 if (global_settings
.party_mode
) {
346 gui_syncsplash(HZ
, str(LANG_PARTY_MODE
));
350 if (bookmark_autoload(buf
))
353 gui_syncsplash(0, str(LANG_WAIT
));
355 /* about to create a new current playlist...
356 allow user to cancel the operation */
357 if (global_settings
.warnon_erase_dynplaylist
&&
358 playlist_modified(NULL
))
360 char *lines
[]={str(LANG_WARN_ERASEDYNPLAYLIST_PROMPT
)};
361 struct text_message message
={lines
, 1};
363 if(gui_syncyesno_run(&message
, NULL
, NULL
) != YESNO_YES
)
367 if (playlist_create(c
->currdir
, file
->name
) != -1)
369 if (global_settings
.playlist_shuffle
)
370 playlist_shuffle(seed
, -1);
372 playlist_start(start_index
,0);
377 case FILE_ATTR_AUDIO
:
378 if (bookmark_autoload(c
->currdir
))
381 gui_syncsplash(0, str(LANG_WAIT
));
383 /* about to create a new current playlist...
384 allow user to cancel the operation */
385 if (global_settings
.warnon_erase_dynplaylist
&&
386 !global_settings
.party_mode
&&
387 playlist_modified(NULL
))
389 char *lines
[]={str(LANG_WARN_ERASEDYNPLAYLIST_PROMPT
)};
390 struct text_message message
={lines
, 1};
392 if(gui_syncyesno_run(&message
, NULL
, NULL
) != YESNO_YES
)
396 if (global_settings
.party_mode
)
398 playlist_insert_track(NULL
, buf
,
399 PLAYLIST_INSERT_LAST
, true, true);
400 gui_syncsplash(HZ
, str(LANG_QUEUE_LAST
));
402 else if (playlist_create(c
->currdir
, NULL
) != -1)
404 start_index
= ft_build_playlist(c
, c
->selected_item
);
405 if (global_settings
.playlist_shuffle
)
407 start_index
= playlist_shuffle(seed
, start_index
);
409 /* when shuffling dir.: play all files
410 even if the file selected by user is
412 if (!global_settings
.play_selected
)
416 playlist_start(start_index
, 0);
422 /* fmr preset file */
425 gui_syncsplash(0, str(LANG_WAIT
));
427 /* Preset inside the default folder. */
428 if(!strncasecmp(FMPRESET_PATH
, buf
, strlen(FMPRESET_PATH
)))
430 set_file(buf
, global_settings
.fmr_file
, MAX_FILENAME
);
431 radio_load_presets(global_settings
.fmr_file
);
432 if(!in_radio_screen())
436 * Preset outside default folder, we can choose such only
437 * if we are out of the radio screen, so the check for the
438 * radio status isn't neccessary
442 radio_load_presets(buf
);
450 /* wps config file */
452 gui_syncsplash(0, str(LANG_WAIT
));
454 unload_wps_backdrop();
456 wps_data_load(gui_wps
[0].data
, buf
, true);
457 set_file(buf
, (char *)global_settings
.wps_file
,
461 #if defined(HAVE_REMOTE_LCD) && (NB_SCREENS > 1)
462 /* remote-wps config file */
464 gui_syncsplash(0, str(LANG_WAIT
));
465 #if defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1
466 unload_remote_wps_backdrop();
468 wps_data_load(gui_wps
[1].data
, buf
, true);
469 set_file(buf
, (char *)global_settings
.rwps_file
,
475 gui_syncsplash(0, str(LANG_WAIT
));
476 if (!settings_load_config(buf
,true))
478 gui_syncsplash(HZ
, str(LANG_SETTINGS_LOADED
));
481 case FILE_ATTR_BMARK
:
482 gui_syncsplash(0, str(LANG_WAIT
));
483 bookmark_load(buf
, false);
488 gui_syncsplash(0, str(LANG_WAIT
));
489 if(!lang_load(buf
)) {
490 set_file(buf
, (char *)global_settings
.lang_file
,
492 talk_init(); /* use voice of same language */
493 gui_syncsplash(HZ
, str(LANG_LANGUAGE_LOADED
));
497 #ifdef HAVE_LCD_BITMAP
499 gui_syncsplash(0, str(LANG_WAIT
));
501 set_file(buf
, (char *)global_settings
.font_file
, MAX_FILENAME
);
505 gui_syncsplash(0, str(LANG_WAIT
));
507 gui_syncsplash(HZ
, str(LANG_KEYBOARD_LOADED
));
508 set_file(buf
, (char *)global_settings
.kbd_file
, MAX_FILENAME
);
515 gui_syncsplash(0, str(LANG_WAIT
));
522 if (global_settings
.party_mode
) {
523 gui_syncsplash(HZ
, str(LANG_PARTY_MODE
));
527 gui_syncsplash(0, str(LANG_WAIT
));
529 if (plugin_load(buf
,NULL
) == PLUGIN_USB_CONNECTED
)
531 if(*c
->dirfilter
> NUM_FILTER_MODES
)
532 /* leave sub-browsers after usb, doing
533 otherwise might be confusing to the user */
541 display_cuesheet_content(buf
);
548 if (global_settings
.party_mode
) {
549 gui_syncsplash(HZ
, str(LANG_PARTY_MODE
));
553 plugin
= filetype_get_plugin(file
);
556 if (plugin_load(plugin
,buf
) == PLUGIN_USB_CONNECTED
)
564 /* the resume_index must always be the index in the
565 shuffled list in case shuffle is enabled */
566 global_status
.resume_index
= start_index
;
567 global_status
.resume_offset
= 0;
573 if (*c
->dirfilter
> NUM_FILTER_MODES
&&
574 *c
->dirfilter
!= SHOW_FONT
&&
575 *c
->dirfilter
!= SHOW_PLUGINS
)
592 int ft_exit(struct tree_context
* c
)
594 extern char lastfile
[]; /* from tree.c */
597 bool exit_func
= false;
599 int i
= strlen(c
->currdir
);
601 while (c
->currdir
[i
-1]!='/')
603 strcpy(buf
,&c
->currdir
[i
]);
609 if (*c
->dirfilter
> NUM_FILTER_MODES
&& c
->dirlevel
< 1)
613 if ( c
->dirlevel
< MAX_DIR_LEVELS
)
614 c
->selected_item
=c
->selected_item_history
[c
->dirlevel
];
618 /* if undefined position */
619 if (c
->selected_item
== -1)
620 strcpy(lastfile
, buf
);
624 if (*c
->dirfilter
> NUM_FILTER_MODES
&& c
->dirlevel
< 1)