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
54 static int boot_size
= 0;
55 static int boot_cluster
;
62 extern bool boot_changed
;
64 int ft_build_playlist(struct tree_context
* c
, int start_index
)
67 int start
=start_index
;
69 struct entry
*dircache
= c
->dircache
;
71 for(i
= 0;i
< c
->filesindir
;i
++)
73 if((dircache
[i
].attr
& TREE_ATTR_MASK
) == TREE_ATTR_MPA
)
75 DEBUGF("Adding %s\n", dircache
[i
].name
);
76 if (playlist_add(dircache
[i
].name
) < 0)
81 /* Adjust the start index when se skip non-MP3 entries */
90 /* walk a directory and check all dircache entries if a .talk file exists */
91 static void check_file_thumbnails(struct tree_context
* c
)
94 struct dircache_entry
*entry
;
95 struct entry
* dircache
= c
->dircache
;
98 dir
= opendir_cached(c
->currdir
);
101 /* mark all files as non talking, except the .talk ones */
102 for (i
=0; i
< c
->filesindir
; i
++)
104 if (dircache
[i
].attr
& ATTR_DIRECTORY
)
105 continue; /* we're not touching directories */
107 if (strcasecmp(file_thumbnail_ext
,
108 &dircache
[i
].name
[strlen(dircache
[i
].name
)
109 - strlen(file_thumbnail_ext
)]))
110 { /* no .talk file */
111 dircache
[i
].attr
&= ~TREE_ATTR_THUMBNAIL
; /* clear */
114 { /* .talk file, we later let them speak themselves */
115 dircache
[i
].attr
|= TREE_ATTR_THUMBNAIL
; /* set */
119 while((entry
= readdir_cached(dir
)) != 0) /* walk directory */
123 ext_pos
= strlen((char *)entry
->d_name
) - strlen(file_thumbnail_ext
);
124 if (ext_pos
<= 0 /* too short to carry ".talk" */
125 || (entry
->attribute
& ATTR_DIRECTORY
) /* no file */
126 || strcasecmp((char *)&entry
->d_name
[ext_pos
], file_thumbnail_ext
))
127 { /* or doesn't end with ".talk", no candidate */
131 /* terminate the (disposable) name in dir buffer,
132 this truncates off the ".talk" without needing an extra buffer */
133 entry
->d_name
[ext_pos
] = '\0';
135 /* search corresponding file in dir cache */
136 for (i
=0; i
< c
->filesindir
; i
++)
138 if (!strcasecmp(dircache
[i
].name
, (char *)entry
->d_name
))
140 dircache
[i
].attr
|= TREE_ATTR_THUMBNAIL
; /* set the flag */
141 break; /* exit search loop, because we found it */
145 closedir_cached(dir
);
148 /* support function for qsort() */
149 static int compare(const void* p1
, const void* p2
)
151 struct entry
* e1
= (struct entry
*)p1
;
152 struct entry
* e2
= (struct entry
*)p2
;
155 if (e1
->attr
& ATTR_DIRECTORY
&& e2
->attr
& ATTR_DIRECTORY
)
156 { /* two directories */
157 criteria
= global_settings
.sort_dir
;
159 #ifdef HAVE_MULTIVOLUME
160 if (e1
->attr
& ATTR_VOLUME
|| e2
->attr
& ATTR_VOLUME
)
161 { /* a volume identifier is involved */
162 if (e1
->attr
& ATTR_VOLUME
&& e2
->attr
& ATTR_VOLUME
)
163 criteria
= 0; /* two volumes: sort alphabetically */
164 else /* only one is a volume: volume first */
165 return (e2
->attr
& ATTR_VOLUME
) - (e1
->attr
& ATTR_VOLUME
);
170 else if (!(e1
->attr
& ATTR_DIRECTORY
) && !(e2
->attr
& ATTR_DIRECTORY
))
172 criteria
= global_settings
.sort_file
;
174 else /* dir and file, dir goes first */
175 return ( e2
->attr
& ATTR_DIRECTORY
) - ( e1
->attr
& ATTR_DIRECTORY
);
179 case 3: /* sort type */
181 int t1
= e1
->attr
& TREE_ATTR_MASK
;
182 int t2
= e2
->attr
& TREE_ATTR_MASK
;
184 if (!t1
) /* unknown type */
185 t1
= INT_MAX
; /* gets a high number, to sort after known */
186 if (!t2
) /* unknown type */
187 t2
= INT_MAX
; /* gets a high number, to sort after known */
189 if (t1
- t2
) /* if different */
191 /* else fall through to alphabetical sorting */
193 case 0: /* sort alphabetically asc */
194 if (global_settings
.sort_case
)
195 return strncmp(e1
->name
, e2
->name
, MAX_PATH
);
197 return strncasecmp(e1
->name
, e2
->name
, MAX_PATH
);
199 case 4: /* sort alphabetically desc */
200 if (global_settings
.sort_case
)
201 return strncmp(e2
->name
, e1
->name
, MAX_PATH
);
203 return strncasecmp(e2
->name
, e1
->name
, MAX_PATH
);
205 case 1: /* sort date */
206 return e1
->time_write
- e2
->time_write
;
208 case 2: /* sort date, newest first */
209 return e2
->time_write
- e1
->time_write
;
211 return 0; /* never reached */
214 /* load and sort directory into dircache. returns NULL on failure. */
215 int ft_load(struct tree_context
* c
, const char* tempdir
)
218 int name_buffer_used
= 0;
222 dir
= opendir_cached(tempdir
);
224 dir
= opendir_cached(c
->currdir
);
226 return -1; /* not a directory */
231 for ( i
=0; i
< global_settings
.max_files_in_dir
; i
++ ) {
233 struct dircache_entry
*entry
= readdir_cached(dir
);
235 (struct entry
*)(c
->dircache
+ i
* sizeof(struct entry
));
239 len
= strlen((char *)entry
->d_name
);
241 /* skip directories . and .. */
242 if ((entry
->attribute
& ATTR_DIRECTORY
) &&
243 (((len
== 1) && (!strncmp((char *)entry
->d_name
, ".", 1))) ||
244 ((len
== 2) && (!strncmp((char *)entry
->d_name
, "..", 2))))) {
249 /* Skip FAT volume ID */
250 if (entry
->attribute
& ATTR_VOLUME_ID
) {
255 /* filter out dotfiles and hidden files */
256 if (*c
->dirfilter
!= SHOW_ALL
&&
257 ((entry
->d_name
[0]=='.') ||
258 (entry
->attribute
& ATTR_HIDDEN
))) {
263 dptr
->attr
= entry
->attribute
;
265 /* check for known file types */
266 if ( !(dptr
->attr
& ATTR_DIRECTORY
) )
267 dptr
->attr
|= filetype_get_attr((char *)entry
->d_name
);
270 /* memorize/compare details about the boot file */
271 if ((c
->currdir
[1] == 0) && !strcasecmp(entry
->d_name
, BOOTFILE
)) {
273 if ((entry
->size
!= boot_size
) ||
274 (entry
->startcluster
!= boot_cluster
))
277 boot_size
= entry
->size
;
278 boot_cluster
= entry
->startcluster
;
282 /* filter out non-visible files */
283 if ((!(dptr
->attr
& ATTR_DIRECTORY
) && (
284 (*c
->dirfilter
== SHOW_PLAYLIST
&&
285 (dptr
->attr
& TREE_ATTR_MASK
) != TREE_ATTR_M3U
) ||
286 ((*c
->dirfilter
== SHOW_MUSIC
&&
287 (dptr
->attr
& TREE_ATTR_MASK
) != TREE_ATTR_MPA
) &&
288 (dptr
->attr
& TREE_ATTR_MASK
) != TREE_ATTR_M3U
) ||
289 (*c
->dirfilter
== SHOW_SUPPORTED
&& !filetype_supported(dptr
->attr
)))) ||
290 (*c
->dirfilter
== SHOW_WPS
&& (dptr
->attr
& TREE_ATTR_MASK
) != TREE_ATTR_WPS
) ||
291 #ifdef HAVE_REMOTE_LCD
292 (*c
->dirfilter
== SHOW_RWPS
&& (dptr
->attr
& TREE_ATTR_MASK
) != TREE_ATTR_RWPS
) ||
295 (*c
->dirfilter
== SHOW_FMR
&& (dptr
->attr
& TREE_ATTR_MASK
) != TREE_ATTR_FMR
) ||
297 (*c
->dirfilter
== SHOW_CFG
&& (dptr
->attr
& TREE_ATTR_MASK
) != TREE_ATTR_CFG
) ||
298 (*c
->dirfilter
== SHOW_LNG
&& (dptr
->attr
& TREE_ATTR_MASK
) != TREE_ATTR_LNG
) ||
299 (*c
->dirfilter
== SHOW_MOD
&& (dptr
->attr
& TREE_ATTR_MASK
) != TREE_ATTR_MOD
) ||
300 (*c
->dirfilter
== SHOW_FONT
&& (dptr
->attr
& TREE_ATTR_MASK
) != TREE_ATTR_FONT
) ||
301 (*c
->dirfilter
== SHOW_PLUGINS
&& (dptr
->attr
& TREE_ATTR_MASK
) != TREE_ATTR_ROCK
))
307 if (len
> c
->name_buffer_size
- name_buffer_used
- 1) {
308 /* Tell the world that we ran out of buffer space */
312 dptr
->name
= &c
->name_buffer
[name_buffer_used
];
314 (long)entry
->wrtdate
<<16 |
315 (long)entry
->wrttime
; /* in one # */
316 strcpy(dptr
->name
, (char *)entry
->d_name
);
317 name_buffer_used
+= len
+ 1;
319 if (dptr
->attr
& ATTR_DIRECTORY
) /* count the remaining dirs */
324 closedir_cached(dir
);
326 qsort(c
->dircache
,i
,sizeof(struct entry
),compare
);
328 /* If thumbnail talking is enabled, make an extra run to mark files with
329 associated thumbnails, so we don't do unsuccessful spinups later. */
330 if (global_settings
.talk_file
== 3)
331 check_file_thumbnails(c
); /* map .talk to ours */
336 int ft_enter(struct tree_context
* c
)
340 struct entry
*dircache
= c
->dircache
;
341 struct entry
* file
= &dircache
[c
->selected_item
];
342 bool reload_dir
= false;
343 bool start_wps
= false;
344 bool exit_func
= false;
347 snprintf(buf
,sizeof(buf
),"%s/%s",c
->currdir
, file
->name
);
349 snprintf(buf
,sizeof(buf
),"/%s",file
->name
);
351 if (file
->attr
& ATTR_DIRECTORY
) {
352 memcpy(c
->currdir
, buf
, sizeof(c
->currdir
));
353 if ( c
->dirlevel
< MAX_DIR_LEVELS
)
354 c
->selected_item_history
[c
->dirlevel
] = c
->selected_item
;
359 int seed
= current_tick
;
363 switch ( file
->attr
& TREE_ATTR_MASK
) {
365 if (global_settings
.party_mode
) {
366 gui_syncsplash(HZ
, str(LANG_PARTY_MODE
));
370 if (bookmark_autoload(buf
))
373 gui_syncsplash(0, str(LANG_WAIT
));
375 /* about to create a new current playlist...
376 allow user to cancel the operation */
377 if (global_settings
.warnon_erase_dynplaylist
&&
378 playlist_modified(NULL
))
380 char *lines
[]={str(LANG_WARN_ERASEDYNPLAYLIST_PROMPT
)};
381 struct text_message message
={lines
, 1};
383 if(gui_syncyesno_run(&message
, NULL
, NULL
) != YESNO_YES
)
387 if (playlist_create(c
->currdir
, file
->name
) != -1)
389 if (global_settings
.playlist_shuffle
)
390 playlist_shuffle(seed
, -1);
392 playlist_start(start_index
,0);
398 if (bookmark_autoload(c
->currdir
))
401 gui_syncsplash(0, str(LANG_WAIT
));
403 /* about to create a new current playlist...
404 allow user to cancel the operation */
405 if (global_settings
.warnon_erase_dynplaylist
&&
406 !global_settings
.party_mode
&&
407 playlist_modified(NULL
))
409 char *lines
[]={str(LANG_WARN_ERASEDYNPLAYLIST_PROMPT
)};
410 struct text_message message
={lines
, 1};
412 if(gui_syncyesno_run(&message
, NULL
, NULL
) != YESNO_YES
)
416 if (global_settings
.party_mode
)
418 playlist_insert_track(NULL
, buf
,
419 PLAYLIST_INSERT_LAST
, true, true);
420 gui_syncsplash(HZ
, str(LANG_QUEUE_LAST
));
422 else if (playlist_create(c
->currdir
, NULL
) != -1)
424 start_index
= ft_build_playlist(c
, c
->selected_item
);
425 if (global_settings
.playlist_shuffle
)
427 start_index
= playlist_shuffle(seed
, start_index
);
429 /* when shuffling dir.: play all files
430 even if the file selected by user is
432 if (!global_settings
.play_selected
)
436 playlist_start(start_index
, 0);
442 /* fmr preset file */
445 gui_syncsplash(0, str(LANG_WAIT
));
447 /* Preset inside the default folder. */
448 if(!strncasecmp(FMPRESET_PATH
, buf
, strlen(FMPRESET_PATH
)))
450 set_file(buf
, global_settings
.fmr_file
, MAX_FILENAME
);
451 radio_load_presets(global_settings
.fmr_file
);
452 if(!in_radio_screen())
456 * Preset outside default folder, we can choose such only
457 * if we are out of the radio screen, so the check for the
458 * radio status isn't neccessary
462 radio_load_presets(buf
);
470 /* wps config file */
472 gui_syncsplash(0, str(LANG_WAIT
));
474 unload_wps_backdrop();
476 wps_data_load(gui_wps
[0].data
, buf
, true);
477 set_file(buf
, (char *)global_settings
.wps_file
,
481 #if defined(HAVE_REMOTE_LCD) && (NB_SCREENS > 1)
482 /* remote-wps config file */
484 gui_syncsplash(0, str(LANG_WAIT
));
485 wps_data_load(gui_wps
[1].data
, buf
, true);
486 set_file(buf
, (char *)global_settings
.rwps_file
,
492 gui_syncsplash(0, str(LANG_WAIT
));
493 if (!settings_load_config(buf
,true))
495 gui_syncsplash(HZ
, str(LANG_SETTINGS_LOADED
));
498 case TREE_ATTR_BMARK
:
499 gui_syncsplash(0, str(LANG_WAIT
));
500 bookmark_load(buf
, false);
505 gui_syncsplash(0, str(LANG_WAIT
));
506 if(!lang_load(buf
)) {
507 set_file(buf
, (char *)global_settings
.lang_file
,
509 talk_init(); /* use voice of same language */
510 gui_syncsplash(HZ
, str(LANG_LANGUAGE_LOADED
));
514 #ifdef HAVE_LCD_BITMAP
516 gui_syncsplash(0, str(LANG_WAIT
));
518 set_file(buf
, (char *)global_settings
.font_file
, MAX_FILENAME
);
522 gui_syncsplash(0, str(LANG_WAIT
));
524 gui_syncsplash(HZ
, str(LANG_KEYBOARD_LOADED
));
525 set_file(buf
, (char *)global_settings
.kbd_file
, MAX_FILENAME
);
532 gui_syncsplash(0, str(LANG_WAIT
));
539 if (global_settings
.party_mode
) {
540 gui_syncsplash(HZ
, str(LANG_PARTY_MODE
));
544 gui_syncsplash(0, str(LANG_WAIT
));
546 if (plugin_load(buf
,NULL
) == PLUGIN_USB_CONNECTED
)
548 if(*c
->dirfilter
> NUM_FILTER_MODES
)
549 /* leave sub-browsers after usb, doing
550 otherwise might be confusing to the user */
558 display_cuesheet_content(buf
);
565 if (global_settings
.party_mode
) {
566 gui_syncsplash(HZ
, str(LANG_PARTY_MODE
));
570 plugin
= filetype_get_plugin(file
);
573 if (plugin_load(plugin
,buf
) == PLUGIN_USB_CONNECTED
)
581 /* the resume_index must always be the index in the
582 shuffled list in case shuffle is enabled */
583 global_status
.resume_index
= start_index
;
584 global_status
.resume_offset
= 0;
590 if (*c
->dirfilter
> NUM_FILTER_MODES
&&
591 *c
->dirfilter
!= SHOW_FONT
&&
592 *c
->dirfilter
!= SHOW_PLUGINS
)
609 int ft_exit(struct tree_context
* c
)
611 extern char lastfile
[]; /* from tree.c */
614 bool exit_func
= false;
616 int i
= strlen(c
->currdir
);
618 while (c
->currdir
[i
-1]!='/')
620 strcpy(buf
,&c
->currdir
[i
]);
626 if (*c
->dirfilter
> NUM_FILTER_MODES
&& c
->dirlevel
< 1)
630 if ( c
->dirlevel
< MAX_DIR_LEVELS
)
631 c
->selected_item
=c
->selected_item_history
[c
->dirlevel
];
635 /* if undefined position */
636 if (c
->selected_item
== -1)
637 strcpy(lastfile
, buf
);
641 if (*c
->dirfilter
> NUM_FILTER_MODES
&& c
->dirlevel
< 1)