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 #include "playlist_viewer.h"
48 #ifdef HAVE_LCD_BITMAP
57 static int compare_sort_dir
; /* qsort key for sorting directories */
59 int ft_build_playlist(struct tree_context
* c
, int start_index
)
62 int start
=start_index
;
65 struct entry
*entries
= tree_get_entries(c
);
67 for(i
= 0;i
< c
->filesindir
;i
++)
69 if((entries
[i
].attr
& FILE_ATTR_MASK
) == FILE_ATTR_AUDIO
)
71 if (playlist_add(entries
[i
].name
) < 0)
76 /* Adjust the start index when se skip non-MP3 entries */
87 /* Start playback of a playlist, checking for bookmark autoload, modified
88 * playlists, etc., as required. Returns false if playback wasn't started,
89 * or started via bookmark autoload, true otherwise.
91 * Pointers to both the full pathname and the separated parts needed to
92 * avoid allocating yet another path buffer on the stack (and save some
93 * code; the caller typically needs to create the full pathname anyway)...
95 bool ft_play_playlist(char* pathname
, char* dirname
, char* filename
)
97 if (global_settings
.party_mode
&& audio_status())
99 splash(HZ
, ID2P(LANG_PARTY_MODE
));
103 if (bookmark_autoload(pathname
))
108 splash(0, ID2P(LANG_WAIT
));
110 /* about to create a new current playlist...
111 allow user to cancel the operation */
112 if (!warn_on_pl_erase())
115 if (playlist_create(dirname
, filename
) != -1)
117 if (global_settings
.playlist_shuffle
)
119 playlist_shuffle(current_tick
, -1);
122 playlist_start(0, 0);
129 /* walk a directory and check all entries if a .talk file exists */
130 static void check_file_thumbnails(struct tree_context
* c
)
133 struct dirent
*entry
;
134 struct entry
* entries
;
137 dir
= opendir(c
->currdir
);
140 /* mark all files as non talking, except the .talk ones */
141 entries
= tree_get_entries(c
);
143 for (i
=0; i
< c
->filesindir
; i
++)
145 if (entries
[i
].attr
& ATTR_DIRECTORY
)
146 continue; /* we're not touching directories */
148 if (strcasecmp(file_thumbnail_ext
,
149 &entries
[i
].name
[strlen(entries
[i
].name
)
150 - strlen(file_thumbnail_ext
)]))
151 { /* no .talk file */
152 entries
[i
].attr
&= ~FILE_ATTR_THUMBNAIL
; /* clear */
155 { /* .talk file, we later let them speak themselves */
156 entries
[i
].attr
|= FILE_ATTR_THUMBNAIL
; /* set */
160 while((entry
= readdir(dir
)) != 0) /* walk directory */
163 struct dirinfo info
= dir_get_info(dir
, entry
);
164 ext_pos
= strlen((char *)entry
->d_name
) - strlen(file_thumbnail_ext
);
165 if (ext_pos
<= 0 /* too short to carry ".talk" */
166 || (info
.attribute
& ATTR_DIRECTORY
) /* no file */
167 || strcasecmp((char *)&entry
->d_name
[ext_pos
], file_thumbnail_ext
))
168 { /* or doesn't end with ".talk", no candidate */
172 /* terminate the (disposable) name in dir buffer,
173 this truncates off the ".talk" without needing an extra buffer */
174 entry
->d_name
[ext_pos
] = '\0';
176 /* search corresponding file in dir cache */
177 for (i
=0; i
< c
->filesindir
; i
++)
179 if (!strcasecmp(entries
[i
].name
, (char *)entry
->d_name
))
181 entries
[i
].attr
|= FILE_ATTR_THUMBNAIL
; /* set the flag */
182 break; /* exit search loop, because we found it */
186 tree_unlock_cache(c
);
190 /* support function for qsort() */
191 static int compare(const void* p1
, const void* p2
)
193 struct entry
* e1
= (struct entry
*)p1
;
194 struct entry
* e2
= (struct entry
*)p2
;
197 if (e1
->attr
& ATTR_DIRECTORY
&& e2
->attr
& ATTR_DIRECTORY
)
198 { /* two directories */
199 criteria
= compare_sort_dir
;
201 #ifdef HAVE_MULTIVOLUME
202 if (e1
->attr
& ATTR_VOLUME
|| e2
->attr
& ATTR_VOLUME
)
203 { /* a volume identifier is involved */
204 if (e1
->attr
& ATTR_VOLUME
&& e2
->attr
& ATTR_VOLUME
)
205 criteria
= SORT_ALPHA
; /* two volumes: sort alphabetically */
206 else /* only one is a volume: volume first */
207 return (e2
->attr
& ATTR_VOLUME
) - (e1
->attr
& ATTR_VOLUME
);
212 else if (!(e1
->attr
& ATTR_DIRECTORY
) && !(e2
->attr
& ATTR_DIRECTORY
))
214 criteria
= global_settings
.sort_file
;
216 else /* dir and file, dir goes first */
217 return (e2
->attr
& ATTR_DIRECTORY
) - (e1
->attr
& ATTR_DIRECTORY
);
222 case SORT_TYPE_REVERSED
:
224 int t1
= e1
->attr
& FILE_ATTR_MASK
;
225 int t2
= e2
->attr
& FILE_ATTR_MASK
;
227 if (!t1
) /* unknown type */
228 t1
= INT_MAX
; /* gets a high number, to sort after known */
229 if (!t2
) /* unknown type */
230 t2
= INT_MAX
; /* gets a high number, to sort after known */
232 if (t1
!= t2
) /* if different */
233 return (t1
- t2
) * (criteria
== SORT_TYPE_REVERSED
? -1 : 1);
234 /* else fall through to alphabetical sorting */
238 case SORT_DATE_REVERSED
:
239 /* Ignore SORT_TYPE */
240 if (criteria
== SORT_DATE
|| criteria
== SORT_DATE_REVERSED
)
242 if (e1
->time_write
!= e2
->time_write
)
243 return (e1
->time_write
- e2
->time_write
)
244 * (criteria
== SORT_DATE_REVERSED
? -1 : 1);
245 /* else fall through to alphabetical sorting */
249 case SORT_ALPHA_REVERSED
:
251 if (global_settings
.sort_case
)
253 if (global_settings
.interpret_numbers
== SORT_INTERPRET_AS_NUMBER
)
254 return strnatcmp(e1
->name
, e2
->name
)
255 * (criteria
== SORT_ALPHA_REVERSED
? -1 : 1);
257 return strncmp(e1
->name
, e2
->name
, MAX_PATH
)
258 * (criteria
== SORT_ALPHA_REVERSED
? -1 : 1);
262 if (global_settings
.interpret_numbers
== SORT_INTERPRET_AS_NUMBER
)
263 return strnatcasecmp(e1
->name
, e2
->name
)
264 * (criteria
== SORT_ALPHA_REVERSED
? -1 : 1);
266 return strncasecmp(e1
->name
, e2
->name
, MAX_PATH
)
267 * (criteria
== SORT_ALPHA_REVERSED
? -1 : 1);
272 return 0; /* never reached */
275 /* load and sort directory into the tree's cache. returns NULL on failure. */
276 int ft_load(struct tree_context
* c
, const char* tempdir
)
278 int files_in_dir
= 0;
279 int name_buffer_used
= 0;
280 struct dirent
*entry
;
281 bool (*callback_show_item
)(char *, int, struct tree_context
*) = NULL
;
285 dir
= opendir(tempdir
);
288 dir
= opendir(c
->currdir
);
289 callback_show_item
= c
->browse
? c
->browse
->callback_show_item
: NULL
;
292 return -1; /* not a directory */
298 while ((entry
= readdir(dir
))) {
301 struct entry
* dptr
= tree_get_entry_at(c
, files_in_dir
);
305 info
= dir_get_info(dir
, entry
);
306 len
= strlen((char *)entry
->d_name
);
308 /* skip directories . and .. */
309 if ((info
.attribute
& ATTR_DIRECTORY
) &&
310 (((len
== 1) && (!strncmp((char *)entry
->d_name
, ".", 1))) ||
311 ((len
== 2) && (!strncmp((char *)entry
->d_name
, "..", 2))))) {
315 /* Skip FAT volume ID */
316 if (info
.attribute
& ATTR_VOLUME_ID
) {
320 /* filter out dotfiles and hidden files */
321 if (*c
->dirfilter
!= SHOW_ALL
&&
322 ((entry
->d_name
[0]=='.') ||
323 (info
.attribute
& ATTR_HIDDEN
))) {
327 dptr
->attr
= info
.attribute
;
329 /* check for known file types */
330 if ( !(dptr
->attr
& ATTR_DIRECTORY
) )
331 dptr
->attr
|= filetype_get_attr((char *)entry
->d_name
);
333 /* filter out non-visible files */
334 if ((!(dptr
->attr
& ATTR_DIRECTORY
) && (
335 (*c
->dirfilter
== SHOW_PLAYLIST
&&
336 (dptr
->attr
& FILE_ATTR_MASK
) != FILE_ATTR_M3U
) ||
337 ((*c
->dirfilter
== SHOW_MUSIC
&&
338 (dptr
->attr
& FILE_ATTR_MASK
) != FILE_ATTR_AUDIO
) &&
339 (dptr
->attr
& FILE_ATTR_MASK
) != FILE_ATTR_M3U
) ||
340 (*c
->dirfilter
== SHOW_SUPPORTED
&& !filetype_supported(dptr
->attr
)))) ||
341 (*c
->dirfilter
== SHOW_WPS
&& (dptr
->attr
& FILE_ATTR_MASK
) != FILE_ATTR_WPS
) ||
342 #ifdef HAVE_LCD_BITMAP
343 (*c
->dirfilter
== SHOW_FONT
&& (dptr
->attr
& FILE_ATTR_MASK
) != FILE_ATTR_FONT
) ||
344 (*c
->dirfilter
== SHOW_SBS
&& (dptr
->attr
& FILE_ATTR_MASK
) != FILE_ATTR_SBS
) ||
346 (*c
->dirfilter
== SHOW_FMS
&& (dptr
->attr
& FILE_ATTR_MASK
) != FILE_ATTR_FMS
) ||
349 #ifdef HAVE_REMOTE_LCD
350 (*c
->dirfilter
== SHOW_RWPS
&& (dptr
->attr
& FILE_ATTR_MASK
) != FILE_ATTR_RWPS
) ||
351 (*c
->dirfilter
== SHOW_RSBS
&& (dptr
->attr
& FILE_ATTR_MASK
) != FILE_ATTR_RSBS
) ||
353 (*c
->dirfilter
== SHOW_RFMS
&& (dptr
->attr
& FILE_ATTR_MASK
) != FILE_ATTR_RFMS
) ||
357 (*c
->dirfilter
== SHOW_FMR
&& (dptr
->attr
& FILE_ATTR_MASK
) != FILE_ATTR_FMR
) ||
359 (*c
->dirfilter
== SHOW_M3U
&& (dptr
->attr
& FILE_ATTR_MASK
) != FILE_ATTR_M3U
) ||
360 (*c
->dirfilter
== SHOW_CFG
&& (dptr
->attr
& FILE_ATTR_MASK
) != FILE_ATTR_CFG
) ||
361 (*c
->dirfilter
== SHOW_LNG
&& (dptr
->attr
& FILE_ATTR_MASK
) != FILE_ATTR_LNG
) ||
362 (*c
->dirfilter
== SHOW_MOD
&& (dptr
->attr
& FILE_ATTR_MASK
) != FILE_ATTR_MOD
) ||
363 (*c
->dirfilter
== SHOW_PLUGINS
&& (dptr
->attr
& FILE_ATTR_MASK
) != FILE_ATTR_ROCK
&&
364 (dptr
->attr
& FILE_ATTR_MASK
) != FILE_ATTR_LUA
) ||
365 (callback_show_item
&& !callback_show_item(entry
->d_name
, dptr
->attr
, c
)))
370 if ((len
> c
->cache
.name_buffer_size
- name_buffer_used
- 1) ||
371 (files_in_dir
>= c
->cache
.max_entries
)) {
372 /* Tell the world that we ran out of buffer space */
379 dptr
->name
= core_get_data(c
->cache
.name_buffer_handle
)+name_buffer_used
;
381 (long)info
.wrtdate
<<16 |
382 (long)info
.wrttime
; /* in one # */
383 strcpy(dptr
->name
, (char *)entry
->d_name
);
384 name_buffer_used
+= len
+ 1;
386 if (dptr
->attr
& ATTR_DIRECTORY
) /* count the remaining dirs */
389 c
->filesindir
= files_in_dir
;
390 c
->dirlength
= files_in_dir
;
393 compare_sort_dir
= c
->sort_dir
;
394 qsort(tree_get_entries(c
), files_in_dir
, sizeof(struct entry
), compare
);
396 /* If thumbnail talking is enabled, make an extra run to mark files with
397 associated thumbnails, so we don't do unsuccessful spinups later. */
398 if (global_settings
.talk_file_clip
)
399 check_file_thumbnails(c
); /* map .talk to ours */
401 tree_unlock_cache(c
);
404 #ifdef HAVE_LCD_BITMAP
405 static void ft_load_font(char *file
)
408 MENUITEM_STRINGLIST(menu
, ID2P(LANG_CUSTOM_FONT
), NULL
,
409 ID2P(LANG_MAIN_SCREEN
), ID2P(LANG_REMOTE_SCREEN
))
410 switch (do_menu(&menu
, NULL
, NULL
, false))
412 case 0: /* main lcd */
413 splash(0, ID2P(LANG_WAIT
));
414 font_load(NULL
, file
);
415 set_file(file
, (char *)global_settings
.font_file
, MAX_FILENAME
);
418 splash(0, ID2P(LANG_WAIT
));
419 font_load_remoteui(file
);
420 set_file(file
, (char *)global_settings
.remote_font_file
, MAX_FILENAME
);
424 splash(0, ID2P(LANG_WAIT
));
425 font_load(NULL
, file
);
426 set_file(file
, (char *)global_settings
.font_file
, MAX_FILENAME
);
431 int ft_enter(struct tree_context
* c
)
433 int rc
= GO_TO_PREVIOUS
;
435 struct entry
* file
= tree_get_entry_at(c
, c
->selected_item
);
436 int file_attr
= file
->attr
;
439 snprintf(buf
,sizeof(buf
),"%s/%s",c
->currdir
, file
->name
);
441 snprintf(buf
,sizeof(buf
),"/%s",file
->name
);
443 if (file_attr
& ATTR_DIRECTORY
) {
444 memcpy(c
->currdir
, buf
, sizeof(c
->currdir
));
445 if ( c
->dirlevel
< MAX_DIR_LEVELS
)
446 c
->selected_item_history
[c
->dirlevel
] = c
->selected_item
;
451 int seed
= current_tick
;
455 switch ( file_attr
& FILE_ATTR_MASK
) {
457 if (!bookmark_autoload(buf
))
458 playlist_viewer_ex(buf
);
461 case FILE_ATTR_AUDIO
:
462 if (bookmark_autoload(c
->currdir
))
465 splash(0, ID2P(LANG_WAIT
));
467 /* about to create a new current playlist...
468 allow user to cancel the operation */
469 if (!warn_on_pl_erase())
472 if (global_settings
.party_mode
&& audio_status())
474 playlist_insert_track(NULL
, buf
,
475 PLAYLIST_INSERT_LAST
, true, true);
476 splash(HZ
, ID2P(LANG_QUEUE_LAST
));
478 else if (playlist_create(c
->currdir
, NULL
) != -1)
480 start_index
= ft_build_playlist(c
, c
->selected_item
);
481 if (global_settings
.playlist_shuffle
)
483 start_index
= playlist_shuffle(seed
, start_index
);
485 /* when shuffling dir.: play all files
486 even if the file selected by user is
488 if (!global_settings
.play_selected
)
492 playlist_start(start_index
, 0);
498 /* fmr preset file */
500 splash(0, ID2P(LANG_WAIT
));
502 /* Preset inside the default folder. */
503 if(!strncasecmp(FMPRESET_PATH
, buf
, strlen(FMPRESET_PATH
)))
505 set_file(buf
, global_settings
.fmr_file
, MAX_FILENAME
);
506 radio_load_presets(global_settings
.fmr_file
);
509 * Preset outside default folder, we can choose such only
510 * if we are out of the radio screen, so the check for the
511 * radio status isn't neccessary
515 radio_load_presets(buf
);
521 splash(0, ID2P(LANG_WAIT
));
522 set_file(buf
, (char *)global_settings
.fms_file
, MAX_FILENAME
);
523 settings_apply_skins();
525 #ifdef HAVE_REMOTE_LCD
527 splash(0, ID2P(LANG_WAIT
));
528 set_file(buf
, (char *)global_settings
.rfms_file
, MAX_FILENAME
);
529 settings_apply_skins();
534 #ifdef HAVE_LCD_BITMAP
536 splash(0, ID2P(LANG_WAIT
));
537 set_file(buf
, (char *)global_settings
.sbs_file
, MAX_FILENAME
);
538 settings_apply_skins();
541 #ifdef HAVE_REMOTE_LCD
543 splash(0, ID2P(LANG_WAIT
));
544 set_file(buf
, (char *)global_settings
.rsbs_file
, MAX_FILENAME
);
545 settings_apply_skins();
548 /* wps config file */
550 splash(0, ID2P(LANG_WAIT
));
551 set_file(buf
, (char *)global_settings
.wps_file
,
553 settings_apply_skins();
556 #if defined(HAVE_REMOTE_LCD) && (NB_SCREENS > 1)
557 /* remote-wps config file */
559 splash(0, ID2P(LANG_WAIT
));
560 set_file(buf
, (char *)global_settings
.rwps_file
,
562 settings_apply_skins();
567 splash(0, ID2P(LANG_WAIT
));
568 if (!settings_load_config(buf
,true))
570 splash(HZ
, ID2P(LANG_SETTINGS_LOADED
));
573 case FILE_ATTR_BMARK
:
574 splash(0, ID2P(LANG_WAIT
));
575 bookmark_load(buf
, false);
576 rc
= GO_TO_FILEBROWSER
;
580 splash(0, ID2P(LANG_WAIT
));
581 if (lang_core_load(buf
))
583 splash(HZ
, ID2P(LANG_FAILED
));
586 set_file(buf
, (char *)global_settings
.lang_file
,
588 talk_init(); /* use voice of same language */
589 viewportmanager_theme_changed(THEME_LANGUAGE
);
590 settings_apply_skins();
591 splash(HZ
, ID2P(LANG_LANGUAGE_LOADED
));
594 #ifdef HAVE_LCD_BITMAP
600 splash(0, ID2P(LANG_WAIT
));
602 splash(HZ
, ID2P(LANG_KEYBOARD_LOADED
));
603 set_file(buf
, (char *)global_settings
.kbd_file
, MAX_FILENAME
);
607 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
610 splash(0, ID2P(LANG_WAIT
));
620 char *plugin
= buf
, *argument
= NULL
, lua_path
[MAX_PATH
];
623 if ((file_attr
& FILE_ATTR_MASK
) == FILE_ATTR_LUA
) {
624 snprintf(lua_path
, sizeof(lua_path
)-1, "%s/lua.rock", VIEWERS_DIR
); /* Use a #define here ? */
629 if (global_settings
.party_mode
&& audio_status()) {
630 splash(HZ
, ID2P(LANG_PARTY_MODE
));
633 ret
= plugin_load(plugin
, argument
);
636 case PLUGIN_GOTO_WPS
:
639 case PLUGIN_USB_CONNECTED
:
640 if(*c
->dirfilter
> NUM_FILTER_MODES
)
641 /* leave sub-browsers after usb, doing
642 otherwise might be confusing to the user */
645 rc
= GO_TO_FILEBROWSER
;
657 display_cuesheet_content(buf
);
664 if (global_settings
.party_mode
&& audio_status()) {
665 splash(HZ
, ID2P(LANG_PARTY_MODE
));
669 struct entry
* file
= tree_get_entry_at(c
, c
->selected_item
);
670 plugin
= filetype_get_plugin(file
);
673 switch (plugin_load(plugin
,buf
))
675 case PLUGIN_USB_CONNECTED
:
676 rc
= GO_TO_FILEBROWSER
;
678 case PLUGIN_GOTO_WPS
:
694 /* the resume_index must always be the index in the
695 shuffled list in case shuffle is enabled */
696 global_status
.resume_index
= start_index
;
697 global_status
.resume_offset
= 0;
702 if (*c
->dirfilter
> NUM_FILTER_MODES
&&
703 *c
->dirfilter
!= SHOW_CFG
&&
704 *c
->dirfilter
!= SHOW_FONT
&&
705 *c
->dirfilter
!= SHOW_PLUGINS
)
714 int ft_exit(struct tree_context
* c
)
716 extern char lastfile
[]; /* from tree.c */
719 bool exit_func
= false;
721 int i
= strlen(c
->currdir
);
723 while (c
->currdir
[i
-1]!='/')
725 strcpy(buf
,&c
->currdir
[i
]);
731 if (*c
->dirfilter
> NUM_FILTER_MODES
&& c
->dirlevel
< 1)
735 if ( c
->dirlevel
< MAX_DIR_LEVELS
)
736 c
->selected_item
=c
->selected_item_history
[c
->dirlevel
];
740 /* if undefined position */
741 if (c
->selected_item
== -1)
742 strcpy(lastfile
, buf
);
746 if (*c
->dirfilter
> NUM_FILTER_MODES
&& c
->dirlevel
< 1)