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 ****************************************************************************/
32 #include "core_alloc.h"
34 #include "filetypes.h"
46 #include "strnatcmp.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
;
64 struct entry
*entries
= tree_get_entries(c
);
66 for(i
= 0;i
< c
->filesindir
;i
++)
68 if((entries
[i
].attr
& FILE_ATTR_MASK
) == FILE_ATTR_AUDIO
)
70 if (playlist_add(entries
[i
].name
) < 0)
75 /* Adjust the start index when se skip non-MP3 entries */
86 /* Start playback of a playlist, checking for bookmark autoload, modified
87 * playlists, etc., as required. Returns false if playback wasn't started,
88 * or started via bookmark autoload, true otherwise.
90 * Pointers to both the full pathname and the separated parts needed to
91 * avoid allocating yet another path buffer on the stack (and save some
92 * code; the caller typically needs to create the full pathname anyway)...
94 bool ft_play_playlist(char* pathname
, char* dirname
, char* filename
)
96 if (global_settings
.party_mode
&& audio_status())
98 splash(HZ
, ID2P(LANG_PARTY_MODE
));
102 if (bookmark_autoload(pathname
))
107 splash(0, ID2P(LANG_WAIT
));
109 /* about to create a new current playlist...
110 allow user to cancel the operation */
111 if (!warn_on_pl_erase())
114 if (playlist_create(dirname
, filename
) != -1)
116 if (global_settings
.playlist_shuffle
)
118 playlist_shuffle(current_tick
, -1);
121 playlist_start(0, 0);
128 /* walk a directory and check all entries if a .talk file exists */
129 static void check_file_thumbnails(struct tree_context
* c
)
132 struct dirent
*entry
;
133 struct entry
* entries
;
136 dir
= opendir(c
->currdir
);
139 /* mark all files as non talking, except the .talk ones */
140 entries
= tree_get_entries(c
);
142 for (i
=0; i
< c
->filesindir
; i
++)
144 if (entries
[i
].attr
& ATTR_DIRECTORY
)
145 continue; /* we're not touching directories */
147 if (strcasecmp(file_thumbnail_ext
,
148 &entries
[i
].name
[strlen(entries
[i
].name
)
149 - strlen(file_thumbnail_ext
)]))
150 { /* no .talk file */
151 entries
[i
].attr
&= ~FILE_ATTR_THUMBNAIL
; /* clear */
154 { /* .talk file, we later let them speak themselves */
155 entries
[i
].attr
|= FILE_ATTR_THUMBNAIL
; /* set */
159 while((entry
= readdir(dir
)) != 0) /* walk directory */
162 struct dirinfo info
= dir_get_info(dir
, entry
);
163 ext_pos
= strlen((char *)entry
->d_name
) - strlen(file_thumbnail_ext
);
164 if (ext_pos
<= 0 /* too short to carry ".talk" */
165 || (info
.attribute
& ATTR_DIRECTORY
) /* no file */
166 || strcasecmp((char *)&entry
->d_name
[ext_pos
], file_thumbnail_ext
))
167 { /* or doesn't end with ".talk", no candidate */
171 /* terminate the (disposable) name in dir buffer,
172 this truncates off the ".talk" without needing an extra buffer */
173 entry
->d_name
[ext_pos
] = '\0';
175 /* search corresponding file in dir cache */
176 for (i
=0; i
< c
->filesindir
; i
++)
178 if (!strcasecmp(entries
[i
].name
, (char *)entry
->d_name
))
180 entries
[i
].attr
|= FILE_ATTR_THUMBNAIL
; /* set the flag */
181 break; /* exit search loop, because we found it */
185 tree_unlock_cache(c
);
189 /* support function for qsort() */
190 static int compare(const void* p1
, const void* p2
)
192 struct entry
* e1
= (struct entry
*)p1
;
193 struct entry
* e2
= (struct entry
*)p2
;
196 if (e1
->attr
& ATTR_DIRECTORY
&& e2
->attr
& ATTR_DIRECTORY
)
197 { /* two directories */
198 criteria
= compare_sort_dir
;
200 #ifdef HAVE_MULTIVOLUME
201 if (e1
->attr
& ATTR_VOLUME
|| e2
->attr
& ATTR_VOLUME
)
202 { /* a volume identifier is involved */
203 if (e1
->attr
& ATTR_VOLUME
&& e2
->attr
& ATTR_VOLUME
)
204 criteria
= SORT_ALPHA
; /* two volumes: sort alphabetically */
205 else /* only one is a volume: volume first */
206 return (e2
->attr
& ATTR_VOLUME
) - (e1
->attr
& ATTR_VOLUME
);
211 else if (!(e1
->attr
& ATTR_DIRECTORY
) && !(e2
->attr
& ATTR_DIRECTORY
))
213 criteria
= global_settings
.sort_file
;
215 else /* dir and file, dir goes first */
216 return (e2
->attr
& ATTR_DIRECTORY
) - (e1
->attr
& ATTR_DIRECTORY
);
221 case SORT_TYPE_REVERSED
:
223 int t1
= e1
->attr
& FILE_ATTR_MASK
;
224 int t2
= e2
->attr
& FILE_ATTR_MASK
;
226 if (!t1
) /* unknown type */
227 t1
= INT_MAX
; /* gets a high number, to sort after known */
228 if (!t2
) /* unknown type */
229 t2
= INT_MAX
; /* gets a high number, to sort after known */
231 if (t1
!= t2
) /* if different */
232 return (t1
- t2
) * (criteria
== SORT_TYPE_REVERSED
? -1 : 1);
233 /* else fall through to alphabetical sorting */
237 case SORT_DATE_REVERSED
:
238 /* Ignore SORT_TYPE */
239 if (criteria
== SORT_DATE
|| criteria
== SORT_DATE_REVERSED
)
241 if (e1
->time_write
!= e2
->time_write
)
242 return (e1
->time_write
- e2
->time_write
)
243 * (criteria
== SORT_DATE_REVERSED
? -1 : 1);
244 /* else fall through to alphabetical sorting */
248 case SORT_ALPHA_REVERSED
:
250 if (global_settings
.sort_case
)
252 if (global_settings
.interpret_numbers
== SORT_INTERPRET_AS_NUMBER
)
253 return strnatcmp(e1
->name
, e2
->name
)
254 * (criteria
== SORT_ALPHA_REVERSED
? -1 : 1);
256 return strncmp(e1
->name
, e2
->name
, MAX_PATH
)
257 * (criteria
== SORT_ALPHA_REVERSED
? -1 : 1);
261 if (global_settings
.interpret_numbers
== SORT_INTERPRET_AS_NUMBER
)
262 return strnatcasecmp(e1
->name
, e2
->name
)
263 * (criteria
== SORT_ALPHA_REVERSED
? -1 : 1);
265 return strncasecmp(e1
->name
, e2
->name
, MAX_PATH
)
266 * (criteria
== SORT_ALPHA_REVERSED
? -1 : 1);
271 return 0; /* never reached */
274 /* load and sort directory into the tree's cache. returns NULL on failure. */
275 int ft_load(struct tree_context
* c
, const char* tempdir
)
277 int files_in_dir
= 0;
278 int name_buffer_used
= 0;
279 struct dirent
*entry
;
280 bool (*callback_show_item
)(char *, int, struct tree_context
*) = NULL
;
284 dir
= opendir(tempdir
);
287 dir
= opendir(c
->currdir
);
288 callback_show_item
= c
->browse
? c
->browse
->callback_show_item
: NULL
;
291 return -1; /* not a directory */
297 while ((entry
= readdir(dir
))) {
300 struct entry
* dptr
= tree_get_entry_at(c
, files_in_dir
);
304 info
= dir_get_info(dir
, entry
);
305 len
= strlen((char *)entry
->d_name
);
307 /* skip directories . and .. */
308 if ((info
.attribute
& ATTR_DIRECTORY
) &&
309 (((len
== 1) && (!strncmp((char *)entry
->d_name
, ".", 1))) ||
310 ((len
== 2) && (!strncmp((char *)entry
->d_name
, "..", 2))))) {
314 /* Skip FAT volume ID */
315 if (info
.attribute
& ATTR_VOLUME_ID
) {
319 /* filter out dotfiles and hidden files */
320 if (*c
->dirfilter
!= SHOW_ALL
&&
321 ((entry
->d_name
[0]=='.') ||
322 (info
.attribute
& ATTR_HIDDEN
))) {
326 dptr
->attr
= info
.attribute
;
328 /* check for known file types */
329 if ( !(dptr
->attr
& ATTR_DIRECTORY
) )
330 dptr
->attr
|= filetype_get_attr((char *)entry
->d_name
);
332 /* filter out non-visible files */
333 if ((!(dptr
->attr
& ATTR_DIRECTORY
) && (
334 (*c
->dirfilter
== SHOW_PLAYLIST
&&
335 (dptr
->attr
& FILE_ATTR_MASK
) != FILE_ATTR_M3U
) ||
336 ((*c
->dirfilter
== SHOW_MUSIC
&&
337 (dptr
->attr
& FILE_ATTR_MASK
) != FILE_ATTR_AUDIO
) &&
338 (dptr
->attr
& FILE_ATTR_MASK
) != FILE_ATTR_M3U
) ||
339 (*c
->dirfilter
== SHOW_SUPPORTED
&& !filetype_supported(dptr
->attr
)))) ||
340 (*c
->dirfilter
== SHOW_WPS
&& (dptr
->attr
& FILE_ATTR_MASK
) != FILE_ATTR_WPS
) ||
341 #ifdef HAVE_LCD_BITMAP
342 (*c
->dirfilter
== SHOW_FONT
&& (dptr
->attr
& FILE_ATTR_MASK
) != FILE_ATTR_FONT
) ||
343 (*c
->dirfilter
== SHOW_SBS
&& (dptr
->attr
& FILE_ATTR_MASK
) != FILE_ATTR_SBS
) ||
345 (*c
->dirfilter
== SHOW_FMS
&& (dptr
->attr
& FILE_ATTR_MASK
) != FILE_ATTR_FMS
) ||
348 #ifdef HAVE_REMOTE_LCD
349 (*c
->dirfilter
== SHOW_RWPS
&& (dptr
->attr
& FILE_ATTR_MASK
) != FILE_ATTR_RWPS
) ||
350 (*c
->dirfilter
== SHOW_RSBS
&& (dptr
->attr
& FILE_ATTR_MASK
) != FILE_ATTR_RSBS
) ||
352 (*c
->dirfilter
== SHOW_RFMS
&& (dptr
->attr
& FILE_ATTR_MASK
) != FILE_ATTR_RFMS
) ||
356 (*c
->dirfilter
== SHOW_FMR
&& (dptr
->attr
& FILE_ATTR_MASK
) != FILE_ATTR_FMR
) ||
358 (*c
->dirfilter
== SHOW_M3U
&& (dptr
->attr
& FILE_ATTR_MASK
) != FILE_ATTR_M3U
) ||
359 (*c
->dirfilter
== SHOW_CFG
&& (dptr
->attr
& FILE_ATTR_MASK
) != FILE_ATTR_CFG
) ||
360 (*c
->dirfilter
== SHOW_LNG
&& (dptr
->attr
& FILE_ATTR_MASK
) != FILE_ATTR_LNG
) ||
361 (*c
->dirfilter
== SHOW_MOD
&& (dptr
->attr
& FILE_ATTR_MASK
) != FILE_ATTR_MOD
) ||
362 (*c
->dirfilter
== SHOW_PLUGINS
&& (dptr
->attr
& FILE_ATTR_MASK
) != FILE_ATTR_ROCK
&&
363 (dptr
->attr
& FILE_ATTR_MASK
) != FILE_ATTR_LUA
) ||
364 (callback_show_item
&& !callback_show_item(entry
->d_name
, dptr
->attr
, c
)))
369 if ((len
> c
->cache
.name_buffer_size
- name_buffer_used
- 1) ||
370 (files_in_dir
>= c
->cache
.max_entries
)) {
371 /* Tell the world that we ran out of buffer space */
378 dptr
->name
= core_get_data(c
->cache
.name_buffer_handle
)+name_buffer_used
;
380 (long)info
.wrtdate
<<16 |
381 (long)info
.wrttime
; /* in one # */
382 strcpy(dptr
->name
, (char *)entry
->d_name
);
383 name_buffer_used
+= len
+ 1;
385 if (dptr
->attr
& ATTR_DIRECTORY
) /* count the remaining dirs */
388 c
->filesindir
= files_in_dir
;
389 c
->dirlength
= files_in_dir
;
392 compare_sort_dir
= c
->sort_dir
;
393 qsort(tree_get_entries(c
), files_in_dir
, sizeof(struct entry
), compare
);
395 /* If thumbnail talking is enabled, make an extra run to mark files with
396 associated thumbnails, so we don't do unsuccessful spinups later. */
397 if (global_settings
.talk_file_clip
)
398 check_file_thumbnails(c
); /* map .talk to ours */
400 tree_unlock_cache(c
);
403 #ifdef HAVE_LCD_BITMAP
404 static void ft_load_font(char *file
)
407 enum screen_type screen
= SCREEN_MAIN
;
409 MENUITEM_STRINGLIST(menu
, ID2P(LANG_CUSTOM_FONT
), NULL
,
410 ID2P(LANG_MAIN_SCREEN
), ID2P(LANG_REMOTE_SCREEN
))
411 switch (do_menu(&menu
, NULL
, NULL
, false))
413 case 0: /* main lcd */
414 screen
= SCREEN_MAIN
;
415 set_file(file
, (char *)global_settings
.font_file
, MAX_FILENAME
);
418 screen
= SCREEN_REMOTE
;
419 set_file(file
, (char *)global_settings
.remote_font_file
, MAX_FILENAME
);
423 set_file(file
, (char *)global_settings
.font_file
, MAX_FILENAME
);
425 splash(0, ID2P(LANG_WAIT
));
426 current_font_id
= screens
[screen
].getuifont();
427 if (current_font_id
>= 0)
428 font_unload(current_font_id
);
429 screens
[screen
].setuifont(
430 font_load_ex(file
,0,global_settings
.glyphs_to_cache
));
431 viewportmanager_theme_changed(THEME_UI_VIEWPORT
);
435 int ft_enter(struct tree_context
* c
)
437 int rc
= GO_TO_PREVIOUS
;
439 struct entry
* file
= tree_get_entry_at(c
, c
->selected_item
);
440 int file_attr
= file
->attr
;
443 snprintf(buf
,sizeof(buf
),"%s/%s",c
->currdir
, file
->name
);
445 snprintf(buf
,sizeof(buf
),"/%s",file
->name
);
447 if (file_attr
& ATTR_DIRECTORY
) {
448 memcpy(c
->currdir
, buf
, sizeof(c
->currdir
));
449 if ( c
->dirlevel
< MAX_DIR_LEVELS
)
450 c
->selected_item_history
[c
->dirlevel
] = c
->selected_item
;
455 int seed
= current_tick
;
459 switch ( file_attr
& FILE_ATTR_MASK
) {
461 play
= ft_play_playlist(buf
, c
->currdir
, file
->name
);
470 case FILE_ATTR_AUDIO
:
471 if (bookmark_autoload(c
->currdir
))
474 splash(0, ID2P(LANG_WAIT
));
476 /* about to create a new current playlist...
477 allow user to cancel the operation */
478 if (!warn_on_pl_erase())
481 if (global_settings
.party_mode
&& audio_status())
483 playlist_insert_track(NULL
, buf
,
484 PLAYLIST_INSERT_LAST
, true, true);
485 splash(HZ
, ID2P(LANG_QUEUE_LAST
));
487 else if (playlist_create(c
->currdir
, NULL
) != -1)
489 start_index
= ft_build_playlist(c
, c
->selected_item
);
490 if (global_settings
.playlist_shuffle
)
492 start_index
= playlist_shuffle(seed
, start_index
);
494 /* when shuffling dir.: play all files
495 even if the file selected by user is
497 if (!global_settings
.play_selected
)
501 playlist_start(start_index
, 0);
507 /* fmr preset file */
509 splash(0, ID2P(LANG_WAIT
));
511 /* Preset inside the default folder. */
512 if(!strncasecmp(FMPRESET_PATH
, buf
, strlen(FMPRESET_PATH
)))
514 set_file(buf
, global_settings
.fmr_file
, MAX_FILENAME
);
515 radio_load_presets(global_settings
.fmr_file
);
518 * Preset outside default folder, we can choose such only
519 * if we are out of the radio screen, so the check for the
520 * radio status isn't neccessary
524 radio_load_presets(buf
);
530 splash(0, ID2P(LANG_WAIT
));
531 set_file(buf
, (char *)global_settings
.fms_file
, MAX_FILENAME
);
532 settings_apply_skins();
534 #ifdef HAVE_REMOTE_LCD
536 splash(0, ID2P(LANG_WAIT
));
537 set_file(buf
, (char *)global_settings
.rfms_file
, MAX_FILENAME
);
538 settings_apply_skins();
543 #ifdef HAVE_LCD_BITMAP
545 splash(0, ID2P(LANG_WAIT
));
546 set_file(buf
, (char *)global_settings
.sbs_file
, MAX_FILENAME
);
547 settings_apply_skins();
550 #ifdef HAVE_REMOTE_LCD
552 splash(0, ID2P(LANG_WAIT
));
553 set_file(buf
, (char *)global_settings
.rsbs_file
, MAX_FILENAME
);
554 settings_apply_skins();
557 /* wps config file */
559 splash(0, ID2P(LANG_WAIT
));
560 set_file(buf
, (char *)global_settings
.wps_file
,
562 settings_apply_skins();
565 #if defined(HAVE_REMOTE_LCD) && (NB_SCREENS > 1)
566 /* remote-wps config file */
568 splash(0, ID2P(LANG_WAIT
));
569 set_file(buf
, (char *)global_settings
.rwps_file
,
571 settings_apply_skins();
576 splash(0, ID2P(LANG_WAIT
));
577 if (!settings_load_config(buf
,true))
579 splash(HZ
, ID2P(LANG_SETTINGS_LOADED
));
582 case FILE_ATTR_BMARK
:
583 splash(0, ID2P(LANG_WAIT
));
584 bookmark_load(buf
, false);
585 rc
= GO_TO_FILEBROWSER
;
589 splash(0, ID2P(LANG_WAIT
));
590 if (lang_core_load(buf
))
592 splash(HZ
, ID2P(LANG_FAILED
));
595 set_file(buf
, (char *)global_settings
.lang_file
,
597 talk_init(); /* use voice of same language */
598 viewportmanager_theme_changed(THEME_LANGUAGE
);
599 settings_apply_skins();
600 splash(HZ
, ID2P(LANG_LANGUAGE_LOADED
));
603 #ifdef HAVE_LCD_BITMAP
609 splash(0, ID2P(LANG_WAIT
));
611 splash(HZ
, ID2P(LANG_KEYBOARD_LOADED
));
612 set_file(buf
, (char *)global_settings
.kbd_file
, MAX_FILENAME
);
616 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
619 splash(0, ID2P(LANG_WAIT
));
629 char *plugin
= buf
, *argument
= NULL
, lua_path
[MAX_PATH
];
632 if ((file_attr
& FILE_ATTR_MASK
) == FILE_ATTR_LUA
) {
633 snprintf(lua_path
, sizeof(lua_path
)-1, "%s/lua.rock", VIEWERS_DIR
); /* Use a #define here ? */
638 if (global_settings
.party_mode
&& audio_status()) {
639 splash(HZ
, ID2P(LANG_PARTY_MODE
));
642 ret
= plugin_load(plugin
, argument
);
645 case PLUGIN_GOTO_WPS
:
648 case PLUGIN_USB_CONNECTED
:
649 if(*c
->dirfilter
> NUM_FILTER_MODES
)
650 /* leave sub-browsers after usb, doing
651 otherwise might be confusing to the user */
654 rc
= GO_TO_FILEBROWSER
;
666 display_cuesheet_content(buf
);
673 if (global_settings
.party_mode
&& audio_status()) {
674 splash(HZ
, ID2P(LANG_PARTY_MODE
));
678 struct entry
* file
= tree_get_entry_at(c
, c
->selected_item
);
679 plugin
= filetype_get_plugin(file
);
682 switch (plugin_load(plugin
,buf
))
684 case PLUGIN_USB_CONNECTED
:
685 rc
= GO_TO_FILEBROWSER
;
687 case PLUGIN_GOTO_WPS
:
703 /* the resume_index must always be the index in the
704 shuffled list in case shuffle is enabled */
705 global_status
.resume_index
= start_index
;
706 global_status
.resume_offset
= 0;
711 if (*c
->dirfilter
> NUM_FILTER_MODES
&&
712 *c
->dirfilter
!= SHOW_CFG
&&
713 *c
->dirfilter
!= SHOW_FONT
&&
714 *c
->dirfilter
!= SHOW_PLUGINS
)
723 int ft_exit(struct tree_context
* c
)
725 extern char lastfile
[]; /* from tree.c */
728 bool exit_func
= false;
730 int i
= strlen(c
->currdir
);
732 while (c
->currdir
[i
-1]!='/')
734 strcpy(buf
,&c
->currdir
[i
]);
740 if (*c
->dirfilter
> NUM_FILTER_MODES
&& c
->dirlevel
< 1)
744 if ( c
->dirlevel
< MAX_DIR_LEVELS
)
745 c
->selected_item
=c
->selected_item_history
[c
->dirlevel
];
749 /* if undefined position */
750 if (c
->selected_item
== -1)
751 strcpy(lastfile
, buf
);
755 if (*c
->dirfilter
> NUM_FILTER_MODES
&& c
->dirlevel
< 1)