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
53 static int boot_size
= 0;
54 static int boot_cluster
;
56 extern bool boot_changed
;
58 int ft_build_playlist(struct tree_context
* c
, int start_index
)
61 int start
=start_index
;
63 struct entry
*dircache
= c
->dircache
;
65 for(i
= 0;i
< c
->filesindir
;i
++)
67 if((dircache
[i
].attr
& TREE_ATTR_MASK
) == TREE_ATTR_MPA
)
69 DEBUGF("Adding %s\n", dircache
[i
].name
);
70 if (playlist_add(dircache
[i
].name
) < 0)
75 /* Adjust the start index when se skip non-MP3 entries */
84 /* walk a directory and check all dircache entries if a .talk file exists */
85 static void check_file_thumbnails(struct tree_context
* c
)
88 struct dircache_entry
*entry
;
89 struct entry
* dircache
= c
->dircache
;
92 dir
= opendir_cached(c
->currdir
);
95 /* mark all files as non talking, except the .talk ones */
96 for (i
=0; i
< c
->filesindir
; i
++)
98 if (dircache
[i
].attr
& ATTR_DIRECTORY
)
99 continue; /* we're not touching directories */
101 if (strcasecmp(file_thumbnail_ext
,
102 &dircache
[i
].name
[strlen(dircache
[i
].name
)
103 - strlen(file_thumbnail_ext
)]))
104 { /* no .talk file */
105 dircache
[i
].attr
&= ~TREE_ATTR_THUMBNAIL
; /* clear */
108 { /* .talk file, we later let them speak themselves */
109 dircache
[i
].attr
|= TREE_ATTR_THUMBNAIL
; /* set */
113 while((entry
= readdir_cached(dir
)) != 0) /* walk directory */
117 ext_pos
= strlen((char *)entry
->d_name
) - strlen(file_thumbnail_ext
);
118 if (ext_pos
<= 0 /* too short to carry ".talk" */
119 || (entry
->attribute
& ATTR_DIRECTORY
) /* no file */
120 || strcasecmp((char *)&entry
->d_name
[ext_pos
], file_thumbnail_ext
))
121 { /* or doesn't end with ".talk", no candidate */
125 /* terminate the (disposable) name in dir buffer,
126 this truncates off the ".talk" without needing an extra buffer */
127 entry
->d_name
[ext_pos
] = '\0';
129 /* search corresponding file in dir cache */
130 for (i
=0; i
< c
->filesindir
; i
++)
132 if (!strcasecmp(dircache
[i
].name
, (char *)entry
->d_name
))
134 dircache
[i
].attr
|= TREE_ATTR_THUMBNAIL
; /* set the flag */
135 break; /* exit search loop, because we found it */
139 closedir_cached(dir
);
142 /* support function for qsort() */
143 static int compare(const void* p1
, const void* p2
)
145 struct entry
* e1
= (struct entry
*)p1
;
146 struct entry
* e2
= (struct entry
*)p2
;
149 if (e1
->attr
& ATTR_DIRECTORY
&& e2
->attr
& ATTR_DIRECTORY
)
150 { /* two directories */
151 criteria
= global_settings
.sort_dir
;
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
);
161 else if (!(e1
->attr
& ATTR_DIRECTORY
) && !(e2
->attr
& ATTR_DIRECTORY
))
163 criteria
= global_settings
.sort_file
;
165 else /* dir and file, dir goes first */
166 return ( e2
->attr
& ATTR_DIRECTORY
) - ( e1
->attr
& ATTR_DIRECTORY
);
170 case 3: /* sort type */
172 int t1
= e1
->attr
& TREE_ATTR_MASK
;
173 int t2
= e2
->attr
& TREE_ATTR_MASK
;
175 if (!t1
) /* unknown type */
176 t1
= INT_MAX
; /* gets a high number, to sort after known */
177 if (!t2
) /* unknown type */
178 t2
= INT_MAX
; /* gets a high number, to sort after known */
180 if (t1
- t2
) /* if different */
182 /* else fall through to alphabetical sorting */
184 case 0: /* sort alphabetically asc */
185 if (global_settings
.sort_case
)
186 return strncmp(e1
->name
, e2
->name
, MAX_PATH
);
188 return strncasecmp(e1
->name
, e2
->name
, MAX_PATH
);
190 case 4: /* sort alphabetically desc */
191 if (global_settings
.sort_case
)
192 return strncmp(e2
->name
, e1
->name
, MAX_PATH
);
194 return strncasecmp(e2
->name
, e1
->name
, MAX_PATH
);
196 case 1: /* sort date */
197 return e1
->time_write
- e2
->time_write
;
199 case 2: /* sort date, newest first */
200 return e2
->time_write
- e1
->time_write
;
202 return 0; /* never reached */
205 /* load and sort directory into dircache. returns NULL on failure. */
206 int ft_load(struct tree_context
* c
, const char* tempdir
)
209 int name_buffer_used
= 0;
213 dir
= opendir_cached(tempdir
);
215 dir
= opendir_cached(c
->currdir
);
217 return -1; /* not a directory */
222 for ( i
=0; i
< global_settings
.max_files_in_dir
; i
++ ) {
224 struct dircache_entry
*entry
= readdir_cached(dir
);
226 (struct entry
*)(c
->dircache
+ i
* sizeof(struct entry
));
230 len
= strlen((char *)entry
->d_name
);
232 /* skip directories . and .. */
233 if ((entry
->attribute
& ATTR_DIRECTORY
) &&
234 (((len
== 1) && (!strncmp((char *)entry
->d_name
, ".", 1))) ||
235 ((len
== 2) && (!strncmp((char *)entry
->d_name
, "..", 2))))) {
240 /* Skip FAT volume ID */
241 if (entry
->attribute
& ATTR_VOLUME_ID
) {
246 /* filter out dotfiles and hidden files */
247 if (*c
->dirfilter
!= SHOW_ALL
&&
248 ((entry
->d_name
[0]=='.') ||
249 (entry
->attribute
& ATTR_HIDDEN
))) {
254 dptr
->attr
= entry
->attribute
;
256 /* check for known file types */
257 if ( !(dptr
->attr
& ATTR_DIRECTORY
) )
258 dptr
->attr
|= filetype_get_attr((char *)entry
->d_name
);
261 /* memorize/compare details about the boot file */
262 if ((c
->currdir
[1] == 0) && !strcasecmp(entry
->d_name
, BOOTFILE
)) {
264 if ((entry
->size
!= boot_size
) ||
265 (entry
->startcluster
!= boot_cluster
))
268 boot_size
= entry
->size
;
269 boot_cluster
= entry
->startcluster
;
273 /* filter out non-visible files */
274 if ((!(dptr
->attr
& ATTR_DIRECTORY
) && (
275 (*c
->dirfilter
== SHOW_PLAYLIST
&&
276 (dptr
->attr
& TREE_ATTR_MASK
) != TREE_ATTR_M3U
) ||
277 ((*c
->dirfilter
== SHOW_MUSIC
&&
278 (dptr
->attr
& TREE_ATTR_MASK
) != TREE_ATTR_MPA
) &&
279 (dptr
->attr
& TREE_ATTR_MASK
) != TREE_ATTR_M3U
) ||
280 (*c
->dirfilter
== SHOW_SUPPORTED
&& !filetype_supported(dptr
->attr
)))) ||
281 (*c
->dirfilter
== SHOW_WPS
&& (dptr
->attr
& TREE_ATTR_MASK
) != TREE_ATTR_WPS
) ||
282 #ifdef HAVE_REMOTE_LCD
283 (*c
->dirfilter
== SHOW_RWPS
&& (dptr
->attr
& TREE_ATTR_MASK
) != TREE_ATTR_RWPS
) ||
286 (*c
->dirfilter
== SHOW_FMR
&& (dptr
->attr
& TREE_ATTR_MASK
) != TREE_ATTR_FMR
) ||
288 (*c
->dirfilter
== SHOW_CFG
&& (dptr
->attr
& TREE_ATTR_MASK
) != TREE_ATTR_CFG
) ||
289 (*c
->dirfilter
== SHOW_LNG
&& (dptr
->attr
& TREE_ATTR_MASK
) != TREE_ATTR_LNG
) ||
290 (*c
->dirfilter
== SHOW_MOD
&& (dptr
->attr
& TREE_ATTR_MASK
) != TREE_ATTR_MOD
) ||
291 (*c
->dirfilter
== SHOW_FONT
&& (dptr
->attr
& TREE_ATTR_MASK
) != TREE_ATTR_FONT
) ||
292 (*c
->dirfilter
== SHOW_PLUGINS
&& (dptr
->attr
& TREE_ATTR_MASK
) != TREE_ATTR_ROCK
))
298 if (len
> c
->name_buffer_size
- name_buffer_used
- 1) {
299 /* Tell the world that we ran out of buffer space */
303 dptr
->name
= &c
->name_buffer
[name_buffer_used
];
305 (long)entry
->wrtdate
<<16 |
306 (long)entry
->wrttime
; /* in one # */
307 strcpy(dptr
->name
, (char *)entry
->d_name
);
308 name_buffer_used
+= len
+ 1;
310 if (dptr
->attr
& ATTR_DIRECTORY
) /* count the remaining dirs */
315 closedir_cached(dir
);
317 qsort(c
->dircache
,i
,sizeof(struct entry
),compare
);
319 /* If thumbnail talking is enabled, make an extra run to mark files with
320 associated thumbnails, so we don't do unsuccessful spinups later. */
321 if (global_settings
.talk_file
== 3)
322 check_file_thumbnails(c
); /* map .talk to ours */
327 int ft_enter(struct tree_context
* c
)
331 struct entry
*dircache
= c
->dircache
;
332 struct entry
* file
= &dircache
[c
->selected_item
];
333 bool reload_dir
= false;
334 bool start_wps
= false;
335 bool exit_func
= false;
338 snprintf(buf
,sizeof(buf
),"%s/%s",c
->currdir
, file
->name
);
340 snprintf(buf
,sizeof(buf
),"/%s",file
->name
);
342 if (file
->attr
& ATTR_DIRECTORY
) {
343 memcpy(c
->currdir
, buf
, sizeof(c
->currdir
));
344 if ( c
->dirlevel
< MAX_DIR_LEVELS
)
345 c
->selected_item_history
[c
->dirlevel
] = c
->selected_item
;
350 int seed
= current_tick
;
354 switch ( file
->attr
& TREE_ATTR_MASK
) {
356 if (global_settings
.party_mode
) {
357 gui_syncsplash(HZ
, true, str(LANG_PARTY_MODE
));
361 if (bookmark_autoload(buf
))
364 gui_syncsplash(0, true, str(LANG_WAIT
));
366 /* about to create a new current playlist...
367 allow user to cancel the operation */
368 if (global_settings
.warnon_erase_dynplaylist
&&
369 playlist_modified(NULL
))
371 char *lines
[]={str(LANG_WARN_ERASEDYNPLAYLIST_PROMPT
)};
372 struct text_message message
={lines
, 1};
374 if(gui_syncyesno_run(&message
, NULL
, NULL
) != YESNO_YES
)
378 if (playlist_create(c
->currdir
, file
->name
) != -1)
380 if (global_settings
.playlist_shuffle
)
381 playlist_shuffle(seed
, -1);
383 playlist_start(start_index
,0);
389 if (bookmark_autoload(c
->currdir
))
392 gui_syncsplash(0, true, str(LANG_WAIT
));
394 /* about to create a new current playlist...
395 allow user to cancel the operation */
396 if (global_settings
.warnon_erase_dynplaylist
&&
397 !global_settings
.party_mode
&&
398 playlist_modified(NULL
))
400 char *lines
[]={str(LANG_WARN_ERASEDYNPLAYLIST_PROMPT
)};
401 struct text_message message
={lines
, 1};
403 if(gui_syncyesno_run(&message
, NULL
, NULL
) != YESNO_YES
)
407 if (global_settings
.party_mode
) {
408 playlist_insert_track(NULL
, buf
,
409 PLAYLIST_INSERT_LAST
, true);
410 gui_syncsplash(HZ
, true, str(LANG_QUEUE_LAST
));
412 else if (playlist_create(c
->currdir
, NULL
) != -1)
414 start_index
= ft_build_playlist(c
, c
->selected_item
);
415 if (global_settings
.playlist_shuffle
)
417 start_index
= playlist_shuffle(seed
, start_index
);
419 /* when shuffling dir.: play all files
420 even if the file selected by user is
422 if (!global_settings
.play_selected
)
426 playlist_start(start_index
, 0);
432 /* fmr preset file */
435 gui_syncsplash(0, true, str(LANG_WAIT
));
437 /* Preset inside the default folder. */
438 if(!strncasecmp(FMPRESET_PATH
, buf
, strlen(FMPRESET_PATH
)))
440 set_file(buf
, global_settings
.fmr_file
, MAX_FILENAME
);
441 radio_load_presets(global_settings
.fmr_file
);
442 if(get_radio_status() != FMRADIO_PLAYING
&&
443 get_radio_status() != FMRADIO_PAUSED
)
447 * Preset outside default folder, we can choose such only
448 * if we are out of the radio screen, so the check for the
449 * radio status isn't neccessary
453 radio_load_presets(buf
);
461 /* wps config file */
463 gui_syncsplash(0, true, str(LANG_WAIT
));
464 wps_data_load(gui_wps
[0].data
, buf
, true);
465 set_file(buf
, (char *)global_settings
.wps_file
,
469 #if defined(HAVE_REMOTE_LCD) && (NB_SCREENS > 1)
470 /* remote-wps config file */
472 gui_syncsplash(0, true, str(LANG_WAIT
));
473 wps_data_load(gui_wps
[1].data
, buf
, true);
474 set_file(buf
, (char *)global_settings
.rwps_file
,
480 gui_syncsplash(0, true, str(LANG_WAIT
));
481 if (!settings_load_config(buf
))
483 gui_syncsplash(HZ
, true, str(LANG_SETTINGS_LOADED
));
486 case TREE_ATTR_BMARK
:
487 gui_syncsplash(0, true, str(LANG_WAIT
));
488 bookmark_load(buf
, false);
493 gui_syncsplash(0, true, str(LANG_WAIT
));
494 if(!lang_load(buf
)) {
495 set_file(buf
, (char *)global_settings
.lang_file
,
497 talk_init(); /* use voice of same language */
498 gui_syncsplash(HZ
, true, str(LANG_LANGUAGE_LOADED
));
502 #ifdef HAVE_LCD_BITMAP
504 gui_syncsplash(0, true, str(LANG_WAIT
));
506 set_file(buf
, (char *)global_settings
.font_file
, MAX_FILENAME
);
510 gui_syncsplash(0, true, str(LANG_WAIT
));
512 gui_syncsplash(HZ
, true, str(LANG_KEYBOARD_LOADED
));
513 set_file(buf
, (char *)global_settings
.kbd_file
, MAX_FILENAME
);
520 gui_syncsplash(0, true, str(LANG_WAIT
));
527 if (global_settings
.party_mode
) {
528 gui_syncsplash(HZ
, true, str(LANG_PARTY_MODE
));
532 gui_syncsplash(0, true, str(LANG_WAIT
));
534 if (plugin_load(buf
,NULL
) == PLUGIN_USB_CONNECTED
)
536 if(*c
->dirfilter
> NUM_FILTER_MODES
)
537 /* leave sub-browsers after usb, doing
538 otherwise might be confusing to the user */
549 if (global_settings
.party_mode
) {
550 gui_syncsplash(HZ
, true, str(LANG_PARTY_MODE
));
554 plugin
= filetype_get_plugin(file
);
557 if (plugin_load(plugin
,buf
) == PLUGIN_USB_CONNECTED
)
565 /* the resume_index must always be the index in the
566 shuffled list in case shuffle is enabled */
567 global_settings
.resume_index
= start_index
;
568 global_settings
.resume_offset
= 0;
574 if (*c
->dirfilter
> NUM_FILTER_MODES
&&
575 *c
->dirfilter
!= SHOW_FONT
&&
576 *c
->dirfilter
!= SHOW_PLUGINS
)
593 int ft_exit(struct tree_context
* c
)
595 extern char lastfile
[]; /* from tree.c */
598 bool exit_func
= false;
600 int i
= strlen(c
->currdir
);
602 while (c
->currdir
[i
-1]!='/')
604 strcpy(buf
,&c
->currdir
[i
]);
610 if (*c
->dirfilter
> NUM_FILTER_MODES
&& c
->dirlevel
< 1)
614 if ( c
->dirlevel
< MAX_DIR_LEVELS
)
615 c
->selected_item
=c
->selected_item_history
[c
->dirlevel
];
619 /* if undefined position */
620 if (c
->selected_item
== -1)
621 strcpy(lastfile
, buf
);
625 if (*c
->dirfilter
> NUM_FILTER_MODES
&& c
->dirlevel
< 1)