1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2005 by Björn Stenberg
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
33 #include "filetypes.h"
45 #include "strnatcmp.h"
46 #include "playlist_viewer.h"
47 #ifdef HAVE_LCD_BITMAP
56 static int compare_sort_dir
; /* qsort key for sorting directories */
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
& FILE_ATTR_MASK
) == FILE_ATTR_AUDIO
)
69 if (playlist_add(dircache
[i
].name
) < 0)
74 /* Adjust the start index when se skip non-MP3 entries */
83 /* Start playback of a playlist, checking for bookmark autoload, modified
84 * playlists, etc., as required. Returns false if playback wasn't started,
85 * or started via bookmark autoload, true otherwise.
87 * Pointers to both the full pathname and the separated parts needed to
88 * avoid allocating yet another path buffer on the stack (and save some
89 * code; the caller typically needs to create the full pathname anyway)...
91 bool ft_play_playlist(char* pathname
, char* dirname
, char* filename
)
93 if (global_settings
.party_mode
&& audio_status())
95 splash(HZ
, ID2P(LANG_PARTY_MODE
));
99 if (bookmark_autoload(pathname
))
104 splash(0, ID2P(LANG_WAIT
));
106 /* about to create a new current playlist...
107 allow user to cancel the operation */
108 if (!warn_on_pl_erase())
111 if (playlist_create(dirname
, filename
) != -1)
113 if (global_settings
.playlist_shuffle
)
115 playlist_shuffle(current_tick
, -1);
118 playlist_start(0, 0);
125 /* walk a directory and check all dircache entries if a .talk file exists */
126 static void check_file_thumbnails(struct tree_context
* c
)
129 struct dirent
*entry
;
130 struct entry
* dircache
= c
->dircache
;
133 dir
= opendir(c
->currdir
);
136 /* mark all files as non talking, except the .talk ones */
137 for (i
=0; i
< c
->filesindir
; i
++)
139 if (dircache
[i
].attr
& ATTR_DIRECTORY
)
140 continue; /* we're not touching directories */
142 if (strcasecmp(file_thumbnail_ext
,
143 &dircache
[i
].name
[strlen(dircache
[i
].name
)
144 - strlen(file_thumbnail_ext
)]))
145 { /* no .talk file */
146 dircache
[i
].attr
&= ~FILE_ATTR_THUMBNAIL
; /* clear */
149 { /* .talk file, we later let them speak themselves */
150 dircache
[i
].attr
|= FILE_ATTR_THUMBNAIL
; /* set */
154 while((entry
= readdir(dir
)) != 0) /* walk directory */
157 struct dirinfo info
= dir_get_info(dir
, entry
);
158 ext_pos
= strlen((char *)entry
->d_name
) - strlen(file_thumbnail_ext
);
159 if (ext_pos
<= 0 /* too short to carry ".talk" */
160 || (info
.attribute
& ATTR_DIRECTORY
) /* no file */
161 || strcasecmp((char *)&entry
->d_name
[ext_pos
], file_thumbnail_ext
))
162 { /* or doesn't end with ".talk", no candidate */
166 /* terminate the (disposable) name in dir buffer,
167 this truncates off the ".talk" without needing an extra buffer */
168 entry
->d_name
[ext_pos
] = '\0';
170 /* search corresponding file in dir cache */
171 for (i
=0; i
< c
->filesindir
; i
++)
173 if (!strcasecmp(dircache
[i
].name
, (char *)entry
->d_name
))
175 dircache
[i
].attr
|= FILE_ATTR_THUMBNAIL
; /* set the flag */
176 break; /* exit search loop, because we found it */
183 /* support function for qsort() */
184 static int compare(const void* p1
, const void* p2
)
186 struct entry
* e1
= (struct entry
*)p1
;
187 struct entry
* e2
= (struct entry
*)p2
;
190 if (e1
->attr
& ATTR_DIRECTORY
&& e2
->attr
& ATTR_DIRECTORY
)
191 { /* two directories */
192 criteria
= compare_sort_dir
;
194 #ifdef HAVE_MULTIVOLUME
195 if (e1
->attr
& ATTR_VOLUME
|| e2
->attr
& ATTR_VOLUME
)
196 { /* a volume identifier is involved */
197 if (e1
->attr
& ATTR_VOLUME
&& e2
->attr
& ATTR_VOLUME
)
198 criteria
= SORT_ALPHA
; /* two volumes: sort alphabetically */
199 else /* only one is a volume: volume first */
200 return (e2
->attr
& ATTR_VOLUME
) - (e1
->attr
& ATTR_VOLUME
);
205 else if (!(e1
->attr
& ATTR_DIRECTORY
) && !(e2
->attr
& ATTR_DIRECTORY
))
207 criteria
= global_settings
.sort_file
;
209 else /* dir and file, dir goes first */
210 return (e2
->attr
& ATTR_DIRECTORY
) - (e1
->attr
& ATTR_DIRECTORY
);
215 case SORT_TYPE_REVERSED
:
217 int t1
= e1
->attr
& FILE_ATTR_MASK
;
218 int t2
= e2
->attr
& FILE_ATTR_MASK
;
220 if (!t1
) /* unknown type */
221 t1
= INT_MAX
; /* gets a high number, to sort after known */
222 if (!t2
) /* unknown type */
223 t2
= INT_MAX
; /* gets a high number, to sort after known */
225 if (t1
!= t2
) /* if different */
226 return (t1
- t2
) * (criteria
== SORT_TYPE_REVERSED
? -1 : 1);
227 /* else fall through to alphabetical sorting */
231 case SORT_DATE_REVERSED
:
232 /* Ignore SORT_TYPE */
233 if (criteria
== SORT_DATE
|| criteria
== SORT_DATE_REVERSED
)
235 if (e1
->time_write
!= e2
->time_write
)
236 return (e1
->time_write
- e2
->time_write
)
237 * (criteria
== SORT_DATE_REVERSED
? -1 : 1);
238 /* else fall through to alphabetical sorting */
242 case SORT_ALPHA_REVERSED
:
244 if (global_settings
.sort_case
)
246 if (global_settings
.interpret_numbers
== SORT_INTERPRET_AS_NUMBER
)
247 return strnatcmp(e1
->name
, e2
->name
)
248 * (criteria
== SORT_ALPHA_REVERSED
? -1 : 1);
250 return strncmp(e1
->name
, e2
->name
, MAX_PATH
)
251 * (criteria
== SORT_ALPHA_REVERSED
? -1 : 1);
255 if (global_settings
.interpret_numbers
== SORT_INTERPRET_AS_NUMBER
)
256 return strnatcasecmp(e1
->name
, e2
->name
)
257 * (criteria
== SORT_ALPHA_REVERSED
? -1 : 1);
259 return strncasecmp(e1
->name
, e2
->name
, MAX_PATH
)
260 * (criteria
== SORT_ALPHA_REVERSED
? -1 : 1);
265 return 0; /* never reached */
268 /* load and sort directory into dircache. returns NULL on failure. */
269 int ft_load(struct tree_context
* c
, const char* tempdir
)
271 int files_in_dir
= 0;
272 int name_buffer_used
= 0;
273 struct dirent
*entry
;
274 bool (*callback_show_item
)(char *, int, struct tree_context
*) = NULL
;
278 dir
= opendir(tempdir
);
281 dir
= opendir(c
->currdir
);
282 callback_show_item
= c
->browse
? c
->browse
->callback_show_item
: NULL
;
285 return -1; /* not a directory */
290 while ((entry
= readdir(dir
))) {
294 (struct entry
*)(c
->dircache
+ files_in_dir
* sizeof(struct entry
));
298 info
= dir_get_info(dir
, entry
);
299 len
= strlen((char *)entry
->d_name
);
301 /* skip directories . and .. */
302 if ((info
.attribute
& ATTR_DIRECTORY
) &&
303 (((len
== 1) && (!strncmp((char *)entry
->d_name
, ".", 1))) ||
304 ((len
== 2) && (!strncmp((char *)entry
->d_name
, "..", 2))))) {
308 /* Skip FAT volume ID */
309 if (info
.attribute
& ATTR_VOLUME_ID
) {
313 /* filter out dotfiles and hidden files */
314 if (*c
->dirfilter
!= SHOW_ALL
&&
315 ((entry
->d_name
[0]=='.') ||
316 (info
.attribute
& ATTR_HIDDEN
))) {
320 dptr
->attr
= info
.attribute
;
322 /* check for known file types */
323 if ( !(dptr
->attr
& ATTR_DIRECTORY
) )
324 dptr
->attr
|= filetype_get_attr((char *)entry
->d_name
);
326 /* filter out non-visible files */
327 if ((!(dptr
->attr
& ATTR_DIRECTORY
) && (
328 (*c
->dirfilter
== SHOW_PLAYLIST
&&
329 (dptr
->attr
& FILE_ATTR_MASK
) != FILE_ATTR_M3U
) ||
330 ((*c
->dirfilter
== SHOW_MUSIC
&&
331 (dptr
->attr
& FILE_ATTR_MASK
) != FILE_ATTR_AUDIO
) &&
332 (dptr
->attr
& FILE_ATTR_MASK
) != FILE_ATTR_M3U
) ||
333 (*c
->dirfilter
== SHOW_SUPPORTED
&& !filetype_supported(dptr
->attr
)))) ||
334 (*c
->dirfilter
== SHOW_WPS
&& (dptr
->attr
& FILE_ATTR_MASK
) != FILE_ATTR_WPS
) ||
335 #ifdef HAVE_LCD_BITMAP
336 (*c
->dirfilter
== SHOW_FONT
&& (dptr
->attr
& FILE_ATTR_MASK
) != FILE_ATTR_FONT
) ||
337 (*c
->dirfilter
== SHOW_SBS
&& (dptr
->attr
& FILE_ATTR_MASK
) != FILE_ATTR_SBS
) ||
339 (*c
->dirfilter
== SHOW_FMS
&& (dptr
->attr
& FILE_ATTR_MASK
) != FILE_ATTR_FMS
) ||
342 #ifdef HAVE_REMOTE_LCD
343 (*c
->dirfilter
== SHOW_RWPS
&& (dptr
->attr
& FILE_ATTR_MASK
) != FILE_ATTR_RWPS
) ||
344 (*c
->dirfilter
== SHOW_RSBS
&& (dptr
->attr
& FILE_ATTR_MASK
) != FILE_ATTR_RSBS
) ||
346 (*c
->dirfilter
== SHOW_RFMS
&& (dptr
->attr
& FILE_ATTR_MASK
) != FILE_ATTR_RFMS
) ||
350 (*c
->dirfilter
== SHOW_FMR
&& (dptr
->attr
& FILE_ATTR_MASK
) != FILE_ATTR_FMR
) ||
352 (*c
->dirfilter
== SHOW_M3U
&& (dptr
->attr
& FILE_ATTR_MASK
) != FILE_ATTR_M3U
) ||
353 (*c
->dirfilter
== SHOW_CFG
&& (dptr
->attr
& FILE_ATTR_MASK
) != FILE_ATTR_CFG
) ||
354 (*c
->dirfilter
== SHOW_LNG
&& (dptr
->attr
& FILE_ATTR_MASK
) != FILE_ATTR_LNG
) ||
355 (*c
->dirfilter
== SHOW_MOD
&& (dptr
->attr
& FILE_ATTR_MASK
) != FILE_ATTR_MOD
) ||
356 (*c
->dirfilter
== SHOW_PLUGINS
&& (dptr
->attr
& FILE_ATTR_MASK
) != FILE_ATTR_ROCK
&&
357 (dptr
->attr
& FILE_ATTR_MASK
) != FILE_ATTR_LUA
) ||
358 (callback_show_item
&& !callback_show_item(entry
->d_name
, dptr
->attr
, c
)))
363 if ((len
> c
->name_buffer_size
- name_buffer_used
- 1) ||
364 (files_in_dir
>= c
->dircache_count
)) {
365 /* Tell the world that we ran out of buffer space */
372 dptr
->name
= &c
->name_buffer
[name_buffer_used
];
374 (long)info
.wrtdate
<<16 |
375 (long)info
.wrttime
; /* in one # */
376 strcpy(dptr
->name
, (char *)entry
->d_name
);
377 name_buffer_used
+= len
+ 1;
379 if (dptr
->attr
& ATTR_DIRECTORY
) /* count the remaining dirs */
382 c
->filesindir
= files_in_dir
;
383 c
->dirlength
= files_in_dir
;
386 compare_sort_dir
= c
->sort_dir
;
387 qsort(c
->dircache
, files_in_dir
, sizeof(struct entry
), compare
);
389 /* If thumbnail talking is enabled, make an extra run to mark files with
390 associated thumbnails, so we don't do unsuccessful spinups later. */
391 if (global_settings
.talk_file_clip
)
392 check_file_thumbnails(c
); /* map .talk to ours */
396 #ifdef HAVE_LCD_BITMAP
397 static void ft_load_font(char *file
)
400 MENUITEM_STRINGLIST(menu
, ID2P(LANG_CUSTOM_FONT
), NULL
,
401 ID2P(LANG_MAIN_SCREEN
), ID2P(LANG_REMOTE_SCREEN
))
402 switch (do_menu(&menu
, NULL
, NULL
, false))
404 case 0: /* main lcd */
405 splash(0, ID2P(LANG_WAIT
));
406 font_load(NULL
, file
);
407 set_file(file
, (char *)global_settings
.font_file
, MAX_FILENAME
);
410 splash(0, ID2P(LANG_WAIT
));
411 font_load_remoteui(file
);
412 set_file(file
, (char *)global_settings
.remote_font_file
, MAX_FILENAME
);
416 splash(0, ID2P(LANG_WAIT
));
417 font_load(NULL
, file
);
418 set_file(file
, (char *)global_settings
.font_file
, MAX_FILENAME
);
423 int ft_enter(struct tree_context
* c
)
425 int rc
= GO_TO_PREVIOUS
;
427 struct entry
*dircache
= c
->dircache
;
428 struct entry
* file
= &dircache
[c
->selected_item
];
431 snprintf(buf
,sizeof(buf
),"%s/%s",c
->currdir
, file
->name
);
433 snprintf(buf
,sizeof(buf
),"/%s",file
->name
);
435 if (file
->attr
& ATTR_DIRECTORY
) {
436 memcpy(c
->currdir
, buf
, sizeof(c
->currdir
));
437 if ( c
->dirlevel
< MAX_DIR_LEVELS
)
438 c
->selected_item_history
[c
->dirlevel
] = c
->selected_item
;
443 int seed
= current_tick
;
447 switch ( file
->attr
& FILE_ATTR_MASK
) {
449 if (!bookmark_autoload(buf
))
450 playlist_viewer_ex(buf
);
453 case FILE_ATTR_AUDIO
:
454 if (bookmark_autoload(c
->currdir
))
457 splash(0, ID2P(LANG_WAIT
));
459 /* about to create a new current playlist...
460 allow user to cancel the operation */
461 if (!warn_on_pl_erase())
464 if (global_settings
.party_mode
&& audio_status())
466 playlist_insert_track(NULL
, buf
,
467 PLAYLIST_INSERT_LAST
, true, true);
468 splash(HZ
, ID2P(LANG_QUEUE_LAST
));
470 else if (playlist_create(c
->currdir
, NULL
) != -1)
472 start_index
= ft_build_playlist(c
, c
->selected_item
);
473 if (global_settings
.playlist_shuffle
)
475 start_index
= playlist_shuffle(seed
, start_index
);
477 /* when shuffling dir.: play all files
478 even if the file selected by user is
480 if (!global_settings
.play_selected
)
484 playlist_start(start_index
, 0);
490 /* fmr preset file */
492 splash(0, ID2P(LANG_WAIT
));
494 /* Preset inside the default folder. */
495 if(!strncasecmp(FMPRESET_PATH
, buf
, strlen(FMPRESET_PATH
)))
497 set_file(buf
, global_settings
.fmr_file
, MAX_FILENAME
);
498 radio_load_presets(global_settings
.fmr_file
);
501 * Preset outside default folder, we can choose such only
502 * if we are out of the radio screen, so the check for the
503 * radio status isn't neccessary
507 radio_load_presets(buf
);
513 splash(0, ID2P(LANG_WAIT
));
514 set_file(buf
, (char *)global_settings
.fms_file
, MAX_FILENAME
);
515 settings_apply_skins();
517 #ifdef HAVE_REMOTE_LCD
519 splash(0, ID2P(LANG_WAIT
));
520 set_file(buf
, (char *)global_settings
.rfms_file
, MAX_FILENAME
);
521 settings_apply_skins();
526 #ifdef HAVE_LCD_BITMAP
528 splash(0, ID2P(LANG_WAIT
));
529 set_file(buf
, (char *)global_settings
.sbs_file
, MAX_FILENAME
);
530 settings_apply_skins();
533 #ifdef HAVE_REMOTE_LCD
535 splash(0, ID2P(LANG_WAIT
));
536 set_file(buf
, (char *)global_settings
.rsbs_file
, MAX_FILENAME
);
537 settings_apply_skins();
540 /* wps config file */
542 splash(0, ID2P(LANG_WAIT
));
543 set_file(buf
, (char *)global_settings
.wps_file
,
545 settings_apply_skins();
548 #if defined(HAVE_REMOTE_LCD) && (NB_SCREENS > 1)
549 /* remote-wps config file */
551 splash(0, ID2P(LANG_WAIT
));
552 set_file(buf
, (char *)global_settings
.rwps_file
,
554 settings_apply_skins();
559 splash(0, ID2P(LANG_WAIT
));
560 if (!settings_load_config(buf
,true))
562 splash(HZ
, ID2P(LANG_SETTINGS_LOADED
));
565 case FILE_ATTR_BMARK
:
566 splash(0, ID2P(LANG_WAIT
));
567 bookmark_load(buf
, false);
568 rc
= GO_TO_FILEBROWSER
;
572 splash(0, ID2P(LANG_WAIT
));
573 if (lang_core_load(buf
))
575 splash(HZ
, ID2P(LANG_FAILED
));
578 set_file(buf
, (char *)global_settings
.lang_file
,
580 talk_init(); /* use voice of same language */
581 viewportmanager_theme_changed(THEME_LANGUAGE
);
582 settings_apply_skins();
583 splash(HZ
, ID2P(LANG_LANGUAGE_LOADED
));
586 #ifdef HAVE_LCD_BITMAP
592 splash(0, ID2P(LANG_WAIT
));
594 splash(HZ
, ID2P(LANG_KEYBOARD_LOADED
));
595 set_file(buf
, (char *)global_settings
.kbd_file
, MAX_FILENAME
);
599 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
602 splash(0, ID2P(LANG_WAIT
));
611 char *plugin
= buf
, *argument
= NULL
, lua_path
[MAX_PATH
];
614 if ((file
->attr
& FILE_ATTR_MASK
) == FILE_ATTR_LUA
) {
615 snprintf(lua_path
, sizeof(lua_path
)-1, "%s/lua.rock", VIEWERS_DIR
); /* Use a #define here ? */
620 if (global_settings
.party_mode
&& audio_status()) {
621 splash(HZ
, ID2P(LANG_PARTY_MODE
));
624 ret
= plugin_load(plugin
, argument
);
627 case PLUGIN_GOTO_WPS
:
630 case PLUGIN_USB_CONNECTED
:
631 if(*c
->dirfilter
> NUM_FILTER_MODES
)
632 /* leave sub-browsers after usb, doing
633 otherwise might be confusing to the user */
636 rc
= GO_TO_FILEBROWSER
;
648 display_cuesheet_content(buf
);
655 if (global_settings
.party_mode
&& audio_status()) {
656 splash(HZ
, ID2P(LANG_PARTY_MODE
));
660 plugin
= filetype_get_plugin(file
);
663 switch (plugin_load(plugin
,buf
))
665 case PLUGIN_USB_CONNECTED
:
666 rc
= GO_TO_FILEBROWSER
;
668 case PLUGIN_GOTO_WPS
:
684 /* the resume_index must always be the index in the
685 shuffled list in case shuffle is enabled */
686 global_status
.resume_index
= start_index
;
687 global_status
.resume_offset
= 0;
692 if (*c
->dirfilter
> NUM_FILTER_MODES
&&
693 *c
->dirfilter
!= SHOW_CFG
&&
694 *c
->dirfilter
!= SHOW_FONT
&&
695 *c
->dirfilter
!= SHOW_PLUGINS
)
704 int ft_exit(struct tree_context
* c
)
706 extern char lastfile
[]; /* from tree.c */
709 bool exit_func
= false;
711 int i
= strlen(c
->currdir
);
713 while (c
->currdir
[i
-1]!='/')
715 strcpy(buf
,&c
->currdir
[i
]);
721 if (*c
->dirfilter
> NUM_FILTER_MODES
&& c
->dirlevel
< 1)
725 if ( c
->dirlevel
< MAX_DIR_LEVELS
)
726 c
->selected_item
=c
->selected_item_history
[c
->dirlevel
];
730 /* if undefined position */
731 if (c
->selected_item
== -1)
732 strcpy(lastfile
, buf
);
736 if (*c
->dirfilter
> NUM_FILTER_MODES
&& c
->dirlevel
< 1)