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 #ifdef HAVE_LCD_BITMAP
55 static int compare_sort_dir
; /* qsort key for sorting directories */
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 if (playlist_add(dircache
[i
].name
) < 0)
73 /* Adjust the start index when se skip non-MP3 entries */
82 /* Start playback of a playlist, checking for bookmark autoload, modified
83 * playlists, etc., as required. Returns false if playback wasn't started,
84 * or started via bookmark autoload, true otherwise.
86 * Pointers to both the full pathname and the separated parts needed to
87 * avoid allocating yet another path buffer on the stack (and save some
88 * code; the caller typically needs to create the full pathname anyway)...
90 bool ft_play_playlist(char* pathname
, char* dirname
, char* filename
)
92 if (global_settings
.party_mode
&& audio_status())
94 splash(HZ
, ID2P(LANG_PARTY_MODE
));
98 if (bookmark_autoload(pathname
))
103 splash(0, ID2P(LANG_WAIT
));
105 /* about to create a new current playlist...
106 allow user to cancel the operation */
107 if (!warn_on_pl_erase())
110 if (playlist_create(dirname
, filename
) != -1)
112 if (global_settings
.playlist_shuffle
)
114 playlist_shuffle(current_tick
, -1);
117 playlist_start(0, 0);
124 /* walk a directory and check all dircache entries if a .talk file exists */
125 static void check_file_thumbnails(struct tree_context
* c
)
128 struct dirent
*entry
;
129 struct entry
* dircache
= c
->dircache
;
132 dir
= opendir(c
->currdir
);
135 /* mark all files as non talking, except the .talk ones */
136 for (i
=0; i
< c
->filesindir
; i
++)
138 if (dircache
[i
].attr
& ATTR_DIRECTORY
)
139 continue; /* we're not touching directories */
141 if (strcasecmp(file_thumbnail_ext
,
142 &dircache
[i
].name
[strlen(dircache
[i
].name
)
143 - strlen(file_thumbnail_ext
)]))
144 { /* no .talk file */
145 dircache
[i
].attr
&= ~FILE_ATTR_THUMBNAIL
; /* clear */
148 { /* .talk file, we later let them speak themselves */
149 dircache
[i
].attr
|= FILE_ATTR_THUMBNAIL
; /* set */
153 while((entry
= readdir(dir
)) != 0) /* walk directory */
156 struct dirinfo info
= dir_get_info(dir
, entry
);
157 ext_pos
= strlen((char *)entry
->d_name
) - strlen(file_thumbnail_ext
);
158 if (ext_pos
<= 0 /* too short to carry ".talk" */
159 || (info
.attribute
& ATTR_DIRECTORY
) /* no file */
160 || strcasecmp((char *)&entry
->d_name
[ext_pos
], file_thumbnail_ext
))
161 { /* or doesn't end with ".talk", no candidate */
165 /* terminate the (disposable) name in dir buffer,
166 this truncates off the ".talk" without needing an extra buffer */
167 entry
->d_name
[ext_pos
] = '\0';
169 /* search corresponding file in dir cache */
170 for (i
=0; i
< c
->filesindir
; i
++)
172 if (!strcasecmp(dircache
[i
].name
, (char *)entry
->d_name
))
174 dircache
[i
].attr
|= FILE_ATTR_THUMBNAIL
; /* set the flag */
175 break; /* exit search loop, because we found it */
182 /* support function for qsort() */
183 static int compare(const void* p1
, const void* p2
)
185 struct entry
* e1
= (struct entry
*)p1
;
186 struct entry
* e2
= (struct entry
*)p2
;
189 if (e1
->attr
& ATTR_DIRECTORY
&& e2
->attr
& ATTR_DIRECTORY
)
190 { /* two directories */
191 criteria
= compare_sort_dir
;
193 #ifdef HAVE_MULTIVOLUME
194 if (e1
->attr
& ATTR_VOLUME
|| e2
->attr
& ATTR_VOLUME
)
195 { /* a volume identifier is involved */
196 if (e1
->attr
& ATTR_VOLUME
&& e2
->attr
& ATTR_VOLUME
)
197 criteria
= SORT_ALPHA
; /* two volumes: sort alphabetically */
198 else /* only one is a volume: volume first */
199 return (e2
->attr
& ATTR_VOLUME
) - (e1
->attr
& ATTR_VOLUME
);
204 else if (!(e1
->attr
& ATTR_DIRECTORY
) && !(e2
->attr
& ATTR_DIRECTORY
))
206 criteria
= global_settings
.sort_file
;
208 else /* dir and file, dir goes first */
209 return (e2
->attr
& ATTR_DIRECTORY
) - (e1
->attr
& ATTR_DIRECTORY
);
214 case SORT_TYPE_REVERSED
:
216 int t1
= e1
->attr
& FILE_ATTR_MASK
;
217 int t2
= e2
->attr
& FILE_ATTR_MASK
;
219 if (!t1
) /* unknown type */
220 t1
= INT_MAX
; /* gets a high number, to sort after known */
221 if (!t2
) /* unknown type */
222 t2
= INT_MAX
; /* gets a high number, to sort after known */
224 if (t1
!= t2
) /* if different */
225 return (t1
- t2
) * (criteria
== SORT_TYPE_REVERSED
? -1 : 1);
226 /* else fall through to alphabetical sorting */
230 case SORT_DATE_REVERSED
:
231 /* Ignore SORT_TYPE */
232 if (criteria
== SORT_DATE
|| criteria
== SORT_DATE_REVERSED
)
234 if (e1
->time_write
!= e2
->time_write
)
235 return (e1
->time_write
- e2
->time_write
)
236 * (criteria
== SORT_DATE_REVERSED
? -1 : 1);
237 /* else fall through to alphabetical sorting */
241 case SORT_ALPHA_REVERSED
:
243 if (global_settings
.sort_case
)
245 if (global_settings
.interpret_numbers
== SORT_INTERPRET_AS_NUMBER
)
246 return strnatcmp(e1
->name
, e2
->name
)
247 * (criteria
== SORT_ALPHA_REVERSED
? -1 : 1);
249 return strncmp(e1
->name
, e2
->name
, MAX_PATH
)
250 * (criteria
== SORT_ALPHA_REVERSED
? -1 : 1);
254 if (global_settings
.interpret_numbers
== SORT_INTERPRET_AS_NUMBER
)
255 return strnatcasecmp(e1
->name
, e2
->name
)
256 * (criteria
== SORT_ALPHA_REVERSED
? -1 : 1);
258 return strncasecmp(e1
->name
, e2
->name
, MAX_PATH
)
259 * (criteria
== SORT_ALPHA_REVERSED
? -1 : 1);
264 return 0; /* never reached */
267 /* load and sort directory into dircache. returns NULL on failure. */
268 int ft_load(struct tree_context
* c
, const char* tempdir
)
271 int name_buffer_used
= 0;
275 dir
= opendir(tempdir
);
277 dir
= opendir(c
->currdir
);
279 return -1; /* not a directory */
284 for ( i
=0; i
< global_settings
.max_files_in_dir
; i
++ ) {
286 struct dirent
*entry
= readdir(dir
);
289 (struct entry
*)(c
->dircache
+ i
* sizeof(struct entry
));
293 info
= dir_get_info(dir
, entry
);
294 len
= strlen((char *)entry
->d_name
);
296 /* skip directories . and .. */
297 if ((info
.attribute
& ATTR_DIRECTORY
) &&
298 (((len
== 1) && (!strncmp((char *)entry
->d_name
, ".", 1))) ||
299 ((len
== 2) && (!strncmp((char *)entry
->d_name
, "..", 2))))) {
304 /* Skip FAT volume ID */
305 if (info
.attribute
& ATTR_VOLUME_ID
) {
310 /* filter out dotfiles and hidden files */
311 if (*c
->dirfilter
!= SHOW_ALL
&&
312 ((entry
->d_name
[0]=='.') ||
313 (info
.attribute
& ATTR_HIDDEN
))) {
318 dptr
->attr
= info
.attribute
;
320 /* check for known file types */
321 if ( !(dptr
->attr
& ATTR_DIRECTORY
) )
322 dptr
->attr
|= filetype_get_attr((char *)entry
->d_name
);
324 /* filter out non-visible files */
325 if ((!(dptr
->attr
& ATTR_DIRECTORY
) && (
326 (*c
->dirfilter
== SHOW_PLAYLIST
&&
327 (dptr
->attr
& FILE_ATTR_MASK
) != FILE_ATTR_M3U
) ||
328 ((*c
->dirfilter
== SHOW_MUSIC
&&
329 (dptr
->attr
& FILE_ATTR_MASK
) != FILE_ATTR_AUDIO
) &&
330 (dptr
->attr
& FILE_ATTR_MASK
) != FILE_ATTR_M3U
) ||
331 (*c
->dirfilter
== SHOW_SUPPORTED
&& !filetype_supported(dptr
->attr
)))) ||
332 (*c
->dirfilter
== SHOW_WPS
&& (dptr
->attr
& FILE_ATTR_MASK
) != FILE_ATTR_WPS
) ||
333 #ifdef HAVE_LCD_BITMAP
334 (*c
->dirfilter
== SHOW_FONT
&& (dptr
->attr
& FILE_ATTR_MASK
) != FILE_ATTR_FONT
) ||
335 (*c
->dirfilter
== SHOW_SBS
&& (dptr
->attr
& FILE_ATTR_MASK
) != FILE_ATTR_SBS
) ||
337 (*c
->dirfilter
== SHOW_FMS
&& (dptr
->attr
& FILE_ATTR_MASK
) != FILE_ATTR_FMS
) ||
340 #ifdef HAVE_REMOTE_LCD
341 (*c
->dirfilter
== SHOW_RWPS
&& (dptr
->attr
& FILE_ATTR_MASK
) != FILE_ATTR_RWPS
) ||
342 (*c
->dirfilter
== SHOW_RSBS
&& (dptr
->attr
& FILE_ATTR_MASK
) != FILE_ATTR_RSBS
) ||
344 (*c
->dirfilter
== SHOW_RFMS
&& (dptr
->attr
& FILE_ATTR_MASK
) != FILE_ATTR_RFMS
) ||
348 (*c
->dirfilter
== SHOW_FMR
&& (dptr
->attr
& FILE_ATTR_MASK
) != FILE_ATTR_FMR
) ||
350 (*c
->dirfilter
== SHOW_CFG
&& (dptr
->attr
& FILE_ATTR_MASK
) != FILE_ATTR_CFG
) ||
351 (*c
->dirfilter
== SHOW_LNG
&& (dptr
->attr
& FILE_ATTR_MASK
) != FILE_ATTR_LNG
) ||
352 (*c
->dirfilter
== SHOW_MOD
&& (dptr
->attr
& FILE_ATTR_MASK
) != FILE_ATTR_MOD
) ||
353 (*c
->dirfilter
== SHOW_PLUGINS
&& (dptr
->attr
& FILE_ATTR_MASK
) != FILE_ATTR_ROCK
&&
354 (dptr
->attr
& FILE_ATTR_MASK
) != FILE_ATTR_LUA
))
360 if (len
> c
->name_buffer_size
- name_buffer_used
- 1) {
361 /* Tell the world that we ran out of buffer space */
365 dptr
->name
= &c
->name_buffer
[name_buffer_used
];
367 (long)info
.wrtdate
<<16 |
368 (long)info
.wrttime
; /* in one # */
369 strcpy(dptr
->name
, (char *)entry
->d_name
);
370 name_buffer_used
+= len
+ 1;
372 if (dptr
->attr
& ATTR_DIRECTORY
) /* count the remaining dirs */
379 compare_sort_dir
= c
->sort_dir
;
380 qsort(c
->dircache
,i
,sizeof(struct entry
),compare
);
382 /* If thumbnail talking is enabled, make an extra run to mark files with
383 associated thumbnails, so we don't do unsuccessful spinups later. */
384 if (global_settings
.talk_file_clip
)
385 check_file_thumbnails(c
); /* map .talk to ours */
389 #ifdef HAVE_LCD_BITMAP
390 static void ft_load_font(char *file
)
393 MENUITEM_STRINGLIST(menu
, ID2P(LANG_CUSTOM_FONT
), NULL
,
394 ID2P(LANG_MAIN_SCREEN
), ID2P(LANG_REMOTE_SCREEN
))
395 switch (do_menu(&menu
, NULL
, NULL
, false))
397 case 0: /* main lcd */
398 splash(0, ID2P(LANG_WAIT
));
399 font_load(NULL
, file
);
400 set_file(file
, (char *)global_settings
.font_file
, MAX_FILENAME
);
403 splash(0, ID2P(LANG_WAIT
));
404 font_load_remoteui(file
);
405 set_file(file
, (char *)global_settings
.remote_font_file
, MAX_FILENAME
);
409 splash(0, ID2P(LANG_WAIT
));
410 font_load(NULL
, file
);
411 set_file(file
, (char *)global_settings
.font_file
, MAX_FILENAME
);
416 int ft_enter(struct tree_context
* c
)
418 int rc
= GO_TO_PREVIOUS
;
420 struct entry
*dircache
= c
->dircache
;
421 struct entry
* file
= &dircache
[c
->selected_item
];
424 snprintf(buf
,sizeof(buf
),"%s/%s",c
->currdir
, file
->name
);
426 snprintf(buf
,sizeof(buf
),"/%s",file
->name
);
428 if (file
->attr
& ATTR_DIRECTORY
) {
429 memcpy(c
->currdir
, buf
, sizeof(c
->currdir
));
430 if ( c
->dirlevel
< MAX_DIR_LEVELS
)
431 c
->selected_item_history
[c
->dirlevel
] = c
->selected_item
;
436 int seed
= current_tick
;
440 switch ( file
->attr
& FILE_ATTR_MASK
) {
442 play
= ft_play_playlist(buf
, c
->currdir
, file
->name
);
451 case FILE_ATTR_AUDIO
:
452 if (bookmark_autoload(c
->currdir
))
455 splash(0, ID2P(LANG_WAIT
));
457 /* about to create a new current playlist...
458 allow user to cancel the operation */
459 if (!warn_on_pl_erase())
462 if (global_settings
.party_mode
&& audio_status())
464 playlist_insert_track(NULL
, buf
,
465 PLAYLIST_INSERT_LAST
, true, true);
466 splash(HZ
, ID2P(LANG_QUEUE_LAST
));
468 else if (playlist_create(c
->currdir
, NULL
) != -1)
470 start_index
= ft_build_playlist(c
, c
->selected_item
);
471 if (global_settings
.playlist_shuffle
)
473 start_index
= playlist_shuffle(seed
, start_index
);
475 /* when shuffling dir.: play all files
476 even if the file selected by user is
478 if (!global_settings
.play_selected
)
482 playlist_start(start_index
, 0);
488 /* fmr preset file */
490 splash(0, ID2P(LANG_WAIT
));
492 /* Preset inside the default folder. */
493 if(!strncasecmp(FMPRESET_PATH
, buf
, strlen(FMPRESET_PATH
)))
495 set_file(buf
, global_settings
.fmr_file
, MAX_FILENAME
);
496 radio_load_presets(global_settings
.fmr_file
);
499 * Preset outside default folder, we can choose such only
500 * if we are out of the radio screen, so the check for the
501 * radio status isn't neccessary
505 radio_load_presets(buf
);
511 splash(0, ID2P(LANG_WAIT
));
512 set_file(buf
, (char *)global_settings
.fms_file
, MAX_FILENAME
);
513 settings_apply_skins();
515 #ifdef HAVE_REMOTE_LCD
517 splash(0, ID2P(LANG_WAIT
));
518 set_file(buf
, (char *)global_settings
.rfms_file
, MAX_FILENAME
);
519 settings_apply_skins();
524 #ifdef HAVE_LCD_BITMAP
526 splash(0, ID2P(LANG_WAIT
));
527 set_file(buf
, (char *)global_settings
.sbs_file
, MAX_FILENAME
);
528 settings_apply_skins();
531 #ifdef HAVE_REMOTE_LCD
533 splash(0, ID2P(LANG_WAIT
));
534 set_file(buf
, (char *)global_settings
.rsbs_file
, MAX_FILENAME
);
535 settings_apply_skins();
538 /* wps config file */
540 splash(0, ID2P(LANG_WAIT
));
541 set_file(buf
, (char *)global_settings
.wps_file
,
543 settings_apply_skins();
546 #if defined(HAVE_REMOTE_LCD) && (NB_SCREENS > 1)
547 /* remote-wps config file */
549 splash(0, ID2P(LANG_WAIT
));
550 set_file(buf
, (char *)global_settings
.rwps_file
,
552 settings_apply_skins();
557 splash(0, ID2P(LANG_WAIT
));
558 if (!settings_load_config(buf
,true))
560 splash(HZ
, ID2P(LANG_SETTINGS_LOADED
));
563 case FILE_ATTR_BMARK
:
564 splash(0, ID2P(LANG_WAIT
));
565 bookmark_load(buf
, false);
566 rc
= GO_TO_FILEBROWSER
;
570 splash(0, ID2P(LANG_WAIT
));
571 if (lang_core_load(buf
))
573 splash(HZ
, ID2P(LANG_FAILED
));
576 set_file(buf
, (char *)global_settings
.lang_file
,
578 talk_init(); /* use voice of same language */
579 viewportmanager_theme_changed(THEME_LANGUAGE
);
580 settings_apply_skins();
581 splash(HZ
, ID2P(LANG_LANGUAGE_LOADED
));
584 #ifdef HAVE_LCD_BITMAP
590 splash(0, ID2P(LANG_WAIT
));
592 splash(HZ
, ID2P(LANG_KEYBOARD_LOADED
));
593 set_file(buf
, (char *)global_settings
.kbd_file
, MAX_FILENAME
);
597 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
600 splash(0, ID2P(LANG_WAIT
));
609 char *plugin
= buf
, *argument
= NULL
, lua_path
[MAX_PATH
];
612 if ((file
->attr
& FILE_ATTR_MASK
) == FILE_ATTR_LUA
) {
613 snprintf(lua_path
, sizeof(lua_path
)-1, "%s/lua.rock", VIEWERS_DIR
); /* Use a #define here ? */
618 if (global_settings
.party_mode
&& audio_status()) {
619 splash(HZ
, ID2P(LANG_PARTY_MODE
));
622 ret
= plugin_load(plugin
, argument
);
625 case PLUGIN_GOTO_WPS
:
628 case PLUGIN_USB_CONNECTED
:
629 if(*c
->dirfilter
> NUM_FILTER_MODES
)
630 /* leave sub-browsers after usb, doing
631 otherwise might be confusing to the user */
634 rc
= GO_TO_FILEBROWSER
;
646 display_cuesheet_content(buf
);
653 if (global_settings
.party_mode
&& audio_status()) {
654 splash(HZ
, ID2P(LANG_PARTY_MODE
));
658 plugin
= filetype_get_plugin(file
);
661 switch (plugin_load(plugin
,buf
))
663 case PLUGIN_USB_CONNECTED
:
664 rc
= GO_TO_FILEBROWSER
;
666 case PLUGIN_GOTO_WPS
:
682 /* the resume_index must always be the index in the
683 shuffled list in case shuffle is enabled */
684 global_status
.resume_index
= start_index
;
685 global_status
.resume_offset
= 0;
690 if (*c
->dirfilter
> NUM_FILTER_MODES
&&
691 *c
->dirfilter
!= SHOW_CFG
&&
692 *c
->dirfilter
!= SHOW_FONT
&&
693 *c
->dirfilter
!= SHOW_PLUGINS
)
702 int ft_exit(struct tree_context
* c
)
704 extern char lastfile
[]; /* from tree.c */
707 bool exit_func
= false;
709 int i
= strlen(c
->currdir
);
711 while (c
->currdir
[i
-1]!='/')
713 strcpy(buf
,&c
->currdir
[i
]);
719 if (*c
->dirfilter
> NUM_FILTER_MODES
&& c
->dirlevel
< 1)
723 if ( c
->dirlevel
< MAX_DIR_LEVELS
)
724 c
->selected_item
=c
->selected_item_history
[c
->dirlevel
];
728 /* if undefined position */
729 if (c
->selected_item
== -1)
730 strcpy(lastfile
, buf
);
734 if (*c
->dirfilter
> NUM_FILTER_MODES
&& c
->dirlevel
< 1)