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 /* walk a directory and check all dircache entries if a .talk file exists */
83 static void check_file_thumbnails(struct tree_context
* c
)
87 struct entry
* dircache
= c
->dircache
;
90 dir
= opendir(c
->currdir
);
93 /* mark all files as non talking, except the .talk ones */
94 for (i
=0; i
< c
->filesindir
; i
++)
96 if (dircache
[i
].attr
& ATTR_DIRECTORY
)
97 continue; /* we're not touching directories */
99 if (strcasecmp(file_thumbnail_ext
,
100 &dircache
[i
].name
[strlen(dircache
[i
].name
)
101 - strlen(file_thumbnail_ext
)]))
102 { /* no .talk file */
103 dircache
[i
].attr
&= ~FILE_ATTR_THUMBNAIL
; /* clear */
106 { /* .talk file, we later let them speak themselves */
107 dircache
[i
].attr
|= FILE_ATTR_THUMBNAIL
; /* set */
111 while((entry
= readdir(dir
)) != 0) /* walk directory */
115 ext_pos
= strlen((char *)entry
->d_name
) - strlen(file_thumbnail_ext
);
116 if (ext_pos
<= 0 /* too short to carry ".talk" */
117 || (entry
->attribute
& ATTR_DIRECTORY
) /* no file */
118 || strcasecmp((char *)&entry
->d_name
[ext_pos
], file_thumbnail_ext
))
119 { /* or doesn't end with ".talk", no candidate */
123 /* terminate the (disposable) name in dir buffer,
124 this truncates off the ".talk" without needing an extra buffer */
125 entry
->d_name
[ext_pos
] = '\0';
127 /* search corresponding file in dir cache */
128 for (i
=0; i
< c
->filesindir
; i
++)
130 if (!strcasecmp(dircache
[i
].name
, (char *)entry
->d_name
))
132 dircache
[i
].attr
|= FILE_ATTR_THUMBNAIL
; /* set the flag */
133 break; /* exit search loop, because we found it */
140 /* support function for qsort() */
141 static int compare(const void* p1
, const void* p2
)
143 struct entry
* e1
= (struct entry
*)p1
;
144 struct entry
* e2
= (struct entry
*)p2
;
147 if (e1
->attr
& ATTR_DIRECTORY
&& e2
->attr
& ATTR_DIRECTORY
)
148 { /* two directories */
149 criteria
= global_settings
.sort_dir
;
151 #ifdef HAVE_MULTIVOLUME
152 if (e1
->attr
& ATTR_VOLUME
|| e2
->attr
& ATTR_VOLUME
)
153 { /* a volume identifier is involved */
154 if (e1
->attr
& ATTR_VOLUME
&& e2
->attr
& ATTR_VOLUME
)
155 criteria
= 0; /* two volumes: sort alphabetically */
156 else /* only one is a volume: volume first */
157 return (e2
->attr
& ATTR_VOLUME
) - (e1
->attr
& ATTR_VOLUME
);
162 else if (!(e1
->attr
& ATTR_DIRECTORY
) && !(e2
->attr
& ATTR_DIRECTORY
))
164 criteria
= global_settings
.sort_file
;
166 else /* dir and file, dir goes first */
167 return ( e2
->attr
& ATTR_DIRECTORY
) - ( e1
->attr
& ATTR_DIRECTORY
);
171 case 3: /* sort type */
173 int t1
= e1
->attr
& FILE_ATTR_MASK
;
174 int t2
= e2
->attr
& FILE_ATTR_MASK
;
176 if (!t1
) /* unknown type */
177 t1
= INT_MAX
; /* gets a high number, to sort after known */
178 if (!t2
) /* unknown type */
179 t2
= INT_MAX
; /* gets a high number, to sort after known */
181 if (t1
- t2
) /* if different */
183 /* else fall through to alphabetical sorting */
185 case 0: /* sort alphabetically asc */
186 if (global_settings
.sort_case
)
187 return strncmp(e1
->name
, e2
->name
, MAX_PATH
);
189 return strncasecmp(e1
->name
, e2
->name
, MAX_PATH
);
191 case 4: /* sort alphabetically desc */
192 if (global_settings
.sort_case
)
193 return strncmp(e2
->name
, e1
->name
, MAX_PATH
);
195 return strncasecmp(e2
->name
, e1
->name
, MAX_PATH
);
197 case 1: /* sort date */
198 return e1
->time_write
- e2
->time_write
;
200 case 2: /* sort date, newest first */
201 return e2
->time_write
- e1
->time_write
;
203 return 0; /* never reached */
206 /* load and sort directory into dircache. returns NULL on failure. */
207 int ft_load(struct tree_context
* c
, const char* tempdir
)
210 int name_buffer_used
= 0;
214 dir
= opendir(tempdir
);
216 dir
= opendir(c
->currdir
);
218 return -1; /* not a directory */
223 for ( i
=0; i
< global_settings
.max_files_in_dir
; i
++ ) {
225 struct dirent
*entry
= readdir(dir
);
227 (struct entry
*)(c
->dircache
+ i
* sizeof(struct entry
));
231 len
= strlen((char *)entry
->d_name
);
233 /* skip directories . and .. */
234 if ((entry
->attribute
& ATTR_DIRECTORY
) &&
235 (((len
== 1) && (!strncmp((char *)entry
->d_name
, ".", 1))) ||
236 ((len
== 2) && (!strncmp((char *)entry
->d_name
, "..", 2))))) {
241 /* Skip FAT volume ID */
242 if (entry
->attribute
& ATTR_VOLUME_ID
) {
247 /* filter out dotfiles and hidden files */
248 if (*c
->dirfilter
!= SHOW_ALL
&&
249 ((entry
->d_name
[0]=='.') ||
250 (entry
->attribute
& ATTR_HIDDEN
))) {
255 dptr
->attr
= entry
->attribute
;
257 /* check for known file types */
258 if ( !(dptr
->attr
& ATTR_DIRECTORY
) )
259 dptr
->attr
|= filetype_get_attr((char *)entry
->d_name
);
261 /* filter out non-visible files */
262 if ((!(dptr
->attr
& ATTR_DIRECTORY
) && (
263 (*c
->dirfilter
== SHOW_PLAYLIST
&&
264 (dptr
->attr
& FILE_ATTR_MASK
) != FILE_ATTR_M3U
) ||
265 ((*c
->dirfilter
== SHOW_MUSIC
&&
266 (dptr
->attr
& FILE_ATTR_MASK
) != FILE_ATTR_AUDIO
) &&
267 (dptr
->attr
& FILE_ATTR_MASK
) != FILE_ATTR_M3U
) ||
268 (*c
->dirfilter
== SHOW_SUPPORTED
&& !filetype_supported(dptr
->attr
)))) ||
269 (*c
->dirfilter
== SHOW_WPS
&& (dptr
->attr
& FILE_ATTR_MASK
) != FILE_ATTR_WPS
) ||
270 #ifdef HAVE_REMOTE_LCD
271 (*c
->dirfilter
== SHOW_RWPS
&& (dptr
->attr
& FILE_ATTR_MASK
) != FILE_ATTR_RWPS
) ||
274 (*c
->dirfilter
== SHOW_FMR
&& (dptr
->attr
& FILE_ATTR_MASK
) != FILE_ATTR_FMR
) ||
276 (*c
->dirfilter
== SHOW_CFG
&& (dptr
->attr
& FILE_ATTR_MASK
) != FILE_ATTR_CFG
) ||
277 (*c
->dirfilter
== SHOW_LNG
&& (dptr
->attr
& FILE_ATTR_MASK
) != FILE_ATTR_LNG
) ||
278 (*c
->dirfilter
== SHOW_MOD
&& (dptr
->attr
& FILE_ATTR_MASK
) != FILE_ATTR_MOD
) ||
279 (*c
->dirfilter
== SHOW_FONT
&& (dptr
->attr
& FILE_ATTR_MASK
) != FILE_ATTR_FONT
) ||
280 (*c
->dirfilter
== SHOW_PLUGINS
&& (dptr
->attr
& FILE_ATTR_MASK
) != FILE_ATTR_ROCK
))
286 if (len
> c
->name_buffer_size
- name_buffer_used
- 1) {
287 /* Tell the world that we ran out of buffer space */
291 dptr
->name
= &c
->name_buffer
[name_buffer_used
];
293 (long)entry
->wrtdate
<<16 |
294 (long)entry
->wrttime
; /* in one # */
295 strcpy(dptr
->name
, (char *)entry
->d_name
);
296 name_buffer_used
+= len
+ 1;
298 if (dptr
->attr
& ATTR_DIRECTORY
) /* count the remaining dirs */
305 qsort(c
->dircache
,i
,sizeof(struct entry
),compare
);
307 /* If thumbnail talking is enabled, make an extra run to mark files with
308 associated thumbnails, so we don't do unsuccessful spinups later. */
309 if (global_settings
.talk_file_clip
)
310 check_file_thumbnails(c
); /* map .talk to ours */
315 int ft_enter(struct tree_context
* c
)
319 struct entry
*dircache
= c
->dircache
;
320 struct entry
* file
= &dircache
[c
->selected_item
];
321 bool reload_dir
= false;
322 bool start_wps
= false;
323 bool exit_func
= false;
326 snprintf(buf
,sizeof(buf
),"%s/%s",c
->currdir
, file
->name
);
328 snprintf(buf
,sizeof(buf
),"/%s",file
->name
);
330 if (file
->attr
& ATTR_DIRECTORY
) {
331 memcpy(c
->currdir
, buf
, sizeof(c
->currdir
));
332 if ( c
->dirlevel
< MAX_DIR_LEVELS
)
333 c
->selected_item_history
[c
->dirlevel
] = c
->selected_item
;
338 int seed
= current_tick
;
342 switch ( file
->attr
& FILE_ATTR_MASK
) {
344 if (global_settings
.party_mode
) {
345 gui_syncsplash(HZ
, ID2P(LANG_PARTY_MODE
));
349 if (bookmark_autoload(buf
))
352 gui_syncsplash(0, ID2P(LANG_WAIT
));
354 /* about to create a new current playlist...
355 allow user to cancel the operation */
356 if (global_settings
.warnon_erase_dynplaylist
&&
357 playlist_modified(NULL
))
359 char *lines
[]={ID2P(LANG_WARN_ERASEDYNPLAYLIST_PROMPT
)};
360 struct text_message message
={lines
, 1};
362 if(gui_syncyesno_run(&message
, NULL
, NULL
) != YESNO_YES
)
366 if (playlist_create(c
->currdir
, file
->name
) != -1)
368 if (global_settings
.playlist_shuffle
)
369 playlist_shuffle(seed
, -1);
371 playlist_start(start_index
,0);
376 case FILE_ATTR_AUDIO
:
377 if (bookmark_autoload(c
->currdir
))
380 gui_syncsplash(0, ID2P(LANG_WAIT
));
382 /* about to create a new current playlist...
383 allow user to cancel the operation */
384 if (global_settings
.warnon_erase_dynplaylist
&&
385 !global_settings
.party_mode
&&
386 playlist_modified(NULL
))
388 char *lines
[]={ID2P(LANG_WARN_ERASEDYNPLAYLIST_PROMPT
)};
389 struct text_message message
={lines
, 1};
391 if(gui_syncyesno_run(&message
, NULL
, NULL
) != YESNO_YES
)
395 if (global_settings
.party_mode
)
397 playlist_insert_track(NULL
, buf
,
398 PLAYLIST_INSERT_LAST
, true, true);
399 gui_syncsplash(HZ
, ID2P(LANG_QUEUE_LAST
));
401 else if (playlist_create(c
->currdir
, NULL
) != -1)
403 start_index
= ft_build_playlist(c
, c
->selected_item
);
404 if (global_settings
.playlist_shuffle
)
406 start_index
= playlist_shuffle(seed
, start_index
);
408 /* when shuffling dir.: play all files
409 even if the file selected by user is
411 if (!global_settings
.play_selected
)
415 playlist_start(start_index
, 0);
421 /* fmr preset file */
424 gui_syncsplash(0, ID2P(LANG_WAIT
));
426 /* Preset inside the default folder. */
427 if(!strncasecmp(FMPRESET_PATH
, buf
, strlen(FMPRESET_PATH
)))
429 set_file(buf
, global_settings
.fmr_file
, MAX_FILENAME
);
430 radio_load_presets(global_settings
.fmr_file
);
431 if(!in_radio_screen())
435 * Preset outside default folder, we can choose such only
436 * if we are out of the radio screen, so the check for the
437 * radio status isn't neccessary
441 radio_load_presets(buf
);
449 /* wps config file */
451 gui_syncsplash(0, ID2P(LANG_WAIT
));
453 unload_wps_backdrop();
455 wps_data_load(gui_wps
[0].data
, buf
, true);
456 set_file(buf
, (char *)global_settings
.wps_file
,
460 #if defined(HAVE_REMOTE_LCD) && (NB_SCREENS > 1)
461 /* remote-wps config file */
463 gui_syncsplash(0, ID2P(LANG_WAIT
));
464 #if defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1
465 unload_remote_wps_backdrop();
467 wps_data_load(gui_wps
[1].data
, buf
, true);
468 set_file(buf
, (char *)global_settings
.rwps_file
,
474 gui_syncsplash(0, ID2P(LANG_WAIT
));
475 if (!settings_load_config(buf
,true))
477 gui_syncsplash(HZ
, ID2P(LANG_SETTINGS_LOADED
));
480 case FILE_ATTR_BMARK
:
481 gui_syncsplash(0, ID2P(LANG_WAIT
));
482 bookmark_load(buf
, false);
487 gui_syncsplash(0, ID2P(LANG_WAIT
));
488 if(!lang_load(buf
)) {
489 set_file(buf
, (char *)global_settings
.lang_file
,
491 talk_init(); /* use voice of same language */
492 gui_syncsplash(HZ
, ID2P(LANG_LANGUAGE_LOADED
));
496 #ifdef HAVE_LCD_BITMAP
498 gui_syncsplash(0, ID2P(LANG_WAIT
));
500 set_file(buf
, (char *)global_settings
.font_file
, MAX_FILENAME
);
504 gui_syncsplash(0, ID2P(LANG_WAIT
));
506 gui_syncsplash(HZ
, ID2P(LANG_KEYBOARD_LOADED
));
507 set_file(buf
, (char *)global_settings
.kbd_file
, MAX_FILENAME
);
514 gui_syncsplash(0, ID2P(LANG_WAIT
));
521 if (global_settings
.party_mode
) {
522 gui_syncsplash(HZ
, ID2P(LANG_PARTY_MODE
));
526 gui_syncsplash(0, ID2P(LANG_WAIT
));
528 if (plugin_load(buf
,NULL
) == PLUGIN_USB_CONNECTED
)
530 if(*c
->dirfilter
> NUM_FILTER_MODES
)
531 /* leave sub-browsers after usb, doing
532 otherwise might be confusing to the user */
540 display_cuesheet_content(buf
);
547 if (global_settings
.party_mode
) {
548 gui_syncsplash(HZ
, ID2P(LANG_PARTY_MODE
));
552 plugin
= filetype_get_plugin(file
);
555 if (plugin_load(plugin
,buf
) == PLUGIN_USB_CONNECTED
)
563 /* the resume_index must always be the index in the
564 shuffled list in case shuffle is enabled */
565 global_status
.resume_index
= start_index
;
566 global_status
.resume_offset
= 0;
572 if (*c
->dirfilter
> NUM_FILTER_MODES
&&
573 *c
->dirfilter
!= SHOW_FONT
&&
574 *c
->dirfilter
!= SHOW_PLUGINS
)
591 int ft_exit(struct tree_context
* c
)
593 extern char lastfile
[]; /* from tree.c */
596 bool exit_func
= false;
598 int i
= strlen(c
->currdir
);
600 while (c
->currdir
[i
-1]!='/')
602 strcpy(buf
,&c
->currdir
[i
]);
608 if (*c
->dirfilter
> NUM_FILTER_MODES
&& c
->dirlevel
< 1)
612 if ( c
->dirlevel
< MAX_DIR_LEVELS
)
613 c
->selected_item
=c
->selected_item_history
[c
->dirlevel
];
617 /* if undefined position */
618 if (c
->selected_item
== -1)
619 strcpy(lastfile
, buf
);
623 if (*c
->dirfilter
> NUM_FILTER_MODES
&& c
->dirlevel
< 1)