1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2002 by wavey@wavey.org
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 ****************************************************************************/
23 Dynamic playlist design (based on design originally proposed by ricII)
25 There are two files associated with a dynamic playlist:
26 1. Playlist file : This file contains the initial songs in the playlist.
27 The file is created by the user and stored on the hard
28 drive. NOTE: If we are playing the contents of a
29 directory, there will be no playlist file.
30 2. Control file : This file is automatically created when a playlist is
31 started and contains all the commands done to it.
33 The first non-comment line in a control file must begin with
34 "P:VERSION:DIR:FILE" where VERSION is the playlist control file version,
35 DIR is the directory where the playlist is located and FILE is the
36 playlist filename. For dirplay, FILE will be empty. An empty playlist
37 will have both entries as null.
39 Control file commands:
40 a. Add track (A:<position>:<last position>:<path to track>)
41 - Insert a track at the specified position in the current
42 playlist. Last position is used to specify where last insertion
44 b. Queue track (Q:<position>:<last position>:<path to track>)
45 - Queue a track at the specified position in the current
46 playlist. Queued tracks differ from added tracks in that they
47 are deleted from the playlist as soon as they are played and
48 they are not saved to disk as part of the playlist.
49 c. Delete track (D:<position>)
50 - Delete track from specified position in the current playlist.
51 d. Shuffle playlist (S:<seed>:<index>)
52 - Shuffle entire playlist with specified seed. The index
53 identifies the first index in the newly shuffled playlist
54 (needed for repeat mode).
55 e. Unshuffle playlist (U:<index>)
56 - Unshuffle entire playlist. The index identifies the first index
57 in the newly unshuffled playlist.
58 f. Reset last insert position (R)
59 - Needed so that insertions work properly after resume
62 The only resume info that needs to be saved is the current index in the
63 playlist and the position in the track. When resuming, all the commands
64 in the control file will be reapplied so that the playlist indices are
65 exactly the same as before shutdown. To avoid unnecessary disk
66 accesses, the shuffle mode settings are also saved in settings and only
67 flushed to disk when required.
73 #include "string-extra.h"
75 #include "ata_idle_notify.h"
85 #include "applimits.h"
89 #include "filefuncs.h"
95 #include "filetypes.h"
96 #ifdef HAVE_LCD_BITMAP
104 #include "rbunicode.h"
105 #include "root_menu.h"
107 #define PLAYLIST_CONTROL_FILE_VERSION 2
110 Each playlist index has a flag associated with it which identifies what
111 type of track it is. These flags are stored in the 4 high order bits of
114 NOTE: This limits the playlist file size to a max of 256M.
118 01 = Track was prepended into playlist
119 10 = Track was inserted into playlist
120 11 = Track was appended into playlist
125 0 = Track entry is valid
126 1 = Track does not exist on disk and should be skipped
128 #define PLAYLIST_SEEK_MASK 0x0FFFFFFF
129 #define PLAYLIST_INSERT_TYPE_MASK 0xC0000000
130 #define PLAYLIST_QUEUE_MASK 0x20000000
132 #define PLAYLIST_INSERT_TYPE_PREPEND 0x40000000
133 #define PLAYLIST_INSERT_TYPE_INSERT 0x80000000
134 #define PLAYLIST_INSERT_TYPE_APPEND 0xC0000000
136 #define PLAYLIST_QUEUED 0x20000000
137 #define PLAYLIST_SKIPPED 0x10000000
139 struct directory_search_context
{
140 struct playlist_info
* playlist
;
146 static struct playlist_info current_playlist
;
148 static void empty_playlist(struct playlist_info
* playlist
, bool resume
);
149 static void new_playlist(struct playlist_info
* playlist
, const char *dir
,
151 static void create_control(struct playlist_info
* playlist
);
152 static int check_control(struct playlist_info
* playlist
);
153 static int recreate_control(struct playlist_info
* playlist
);
154 static void update_playlist_filename(struct playlist_info
* playlist
,
155 const char *dir
, const char *file
);
156 static int add_indices_to_playlist(struct playlist_info
* playlist
,
157 char* buffer
, size_t buflen
);
158 static int add_track_to_playlist(struct playlist_info
* playlist
,
159 const char *filename
, int position
,
160 bool queue
, int seek_pos
);
161 static int directory_search_callback(char* filename
, void* context
);
162 static int remove_track_from_playlist(struct playlist_info
* playlist
,
163 int position
, bool write
);
164 static int randomise_playlist(struct playlist_info
* playlist
,
165 unsigned int seed
, bool start_current
,
167 static int sort_playlist(struct playlist_info
* playlist
, bool start_current
,
169 static int get_next_index(const struct playlist_info
* playlist
, int steps
,
171 static void find_and_set_playlist_index(struct playlist_info
* playlist
,
173 static int compare(const void* p1
, const void* p2
);
174 static int get_filename(struct playlist_info
* playlist
, int index
, int seek
,
175 bool control_file
, char *buf
, int buf_length
);
176 static int get_next_directory(char *dir
);
177 static int get_next_dir(char *dir
, bool is_forward
, bool recursion
);
178 static int get_previous_directory(char *dir
);
179 static int check_subdir_for_music(char *dir
, const char *subdir
, bool recurse
);
180 static int format_track_path(char *dest
, char *src
, int buf_length
, int max
,
182 static void display_playlist_count(int count
, const unsigned char *fmt
,
184 static void display_buffer_full(void);
185 static int flush_cached_control(struct playlist_info
* playlist
);
186 static int update_control(struct playlist_info
* playlist
,
187 enum playlist_command command
, int i1
, int i2
,
188 const char* s1
, const char* s2
, void* data
);
189 static void sync_control(struct playlist_info
* playlist
, bool force
);
190 static int rotate_index(const struct playlist_info
* playlist
, int index
);
193 #define PLAYLIST_LOAD_POINTERS 1
195 static struct event_queue playlist_queue SHAREDBSS_ATTR
;
196 static long playlist_stack
[(DEFAULT_STACK_SIZE
+ 0x800)/sizeof(long)];
197 static const char playlist_thread_name
[] = "playlist cachectrl";
200 static struct mutex current_playlist_mutex SHAREDBSS_ATTR
;
201 static struct mutex created_playlist_mutex SHAREDBSS_ATTR
;
203 /* Check if the filename suggests M3U or M3U8 format. */
204 static bool is_m3u8(const char* filename
)
206 int len
= strlen(filename
);
208 /* Default to M3U8 unless explicitly told otherwise. */
209 return !(len
> 4 && strcasecmp(&filename
[len
- 4], ".m3u") == 0);
213 /* Convert a filename in an M3U playlist to UTF-8.
215 * buf - the filename to convert; can contain more than one line from the
217 * buf_len - amount of buf that is used.
218 * buf_max - total size of buf.
219 * temp - temporary conversion buffer, at least buf_max bytes.
221 * Returns the length of the converted filename.
223 static int convert_m3u(char* buf
, int buf_len
, int buf_max
, char* temp
)
229 while ((buf
[i
] != '\n') && (buf
[i
] != '\r') && (i
< buf_len
))
234 /* Work back killing white space. */
235 while ((i
> 0) && isspace(buf
[i
- 1]))
243 /* Convert char by char, so as to not overflow temp (iso_decode should
244 * preferably handle this). No more than 4 bytes should be generated for
247 for (i
= 0; i
< buf_len
&& dest
< (temp
+ buf_max
- 4); i
++)
249 dest
= iso_decode(&buf
[i
], dest
, -1, 1);
258 * remove any files and indices associated with the playlist
260 static void empty_playlist(struct playlist_info
* playlist
, bool resume
)
262 playlist
->filename
[0] = '\0';
263 playlist
->utf8
= true;
265 if(playlist
->fd
>= 0)
266 /* If there is an already open playlist, close it. */
270 if(playlist
->control_fd
>= 0)
271 close(playlist
->control_fd
);
272 playlist
->control_fd
= -1;
273 playlist
->control_created
= false;
275 playlist
->in_ram
= false;
277 if (playlist
->buffer
)
278 playlist
->buffer
[0] = 0;
280 playlist
->buffer_end_pos
= 0;
283 playlist
->first_index
= 0;
284 playlist
->amount
= 0;
285 playlist
->last_insert_pos
= -1;
287 playlist
->shuffle_modified
= false;
288 playlist
->deleted
= false;
289 playlist
->num_inserted_tracks
= 0;
290 playlist
->started
= false;
292 playlist
->num_cached
= 0;
293 playlist
->pending_control_sync
= false;
295 if (!resume
&& playlist
->current
)
297 /* start with fresh playlist control file when starting new
299 create_control(playlist
);
304 * Initialize a new playlist for viewing/editing/playing. dir is the
305 * directory where the playlist is located and file is the filename.
307 static void new_playlist(struct playlist_info
* playlist
, const char *dir
,
310 const char *fileused
= file
;
311 const char *dirused
= dir
;
312 empty_playlist(playlist
, false);
318 if (dirused
&& playlist
->current
) /* !current cannot be in_ram */
319 playlist
->in_ram
= true;
321 dirused
= ""; /* empty playlist */
324 update_playlist_filename(playlist
, dirused
, fileused
);
326 if (playlist
->control_fd
>= 0)
328 update_control(playlist
, PLAYLIST_COMMAND_PLAYLIST
,
329 PLAYLIST_CONTROL_FILE_VERSION
, -1, dirused
, fileused
, NULL
);
330 sync_control(playlist
, false);
335 * create control file for playlist
337 static void create_control(struct playlist_info
* playlist
)
339 playlist
->control_fd
= open(playlist
->control_filename
,
340 O_CREAT
|O_RDWR
|O_TRUNC
, 0666);
341 if (playlist
->control_fd
< 0)
343 if (check_rockboxdir())
345 cond_talk_ids_fq(LANG_PLAYLIST_CONTROL_ACCESS_ERROR
);
346 splashf(HZ
*2, (unsigned char *)"%s (%d)",
347 str(LANG_PLAYLIST_CONTROL_ACCESS_ERROR
),
348 playlist
->control_fd
);
350 playlist
->control_created
= false;
354 playlist
->control_created
= true;
359 * validate the control file. This may include creating/initializing it if
362 static int check_control(struct playlist_info
* playlist
)
364 if (!playlist
->control_created
)
366 create_control(playlist
);
368 if (playlist
->control_fd
>= 0)
370 char* dir
= playlist
->filename
;
371 char* file
= playlist
->filename
+playlist
->dirlen
;
372 char c
= playlist
->filename
[playlist
->dirlen
-1];
374 playlist
->filename
[playlist
->dirlen
-1] = '\0';
376 update_control(playlist
, PLAYLIST_COMMAND_PLAYLIST
,
377 PLAYLIST_CONTROL_FILE_VERSION
, -1, dir
, file
, NULL
);
378 sync_control(playlist
, false);
379 playlist
->filename
[playlist
->dirlen
-1] = c
;
383 if (playlist
->control_fd
< 0)
390 * recreate the control file based on current playlist entries
392 static int recreate_control(struct playlist_info
* playlist
)
394 char temp_file
[MAX_PATH
+1];
399 if(playlist
->control_fd
>= 0)
401 char* dir
= playlist
->filename
;
402 char* file
= playlist
->filename
+playlist
->dirlen
;
403 char c
= playlist
->filename
[playlist
->dirlen
-1];
405 close(playlist
->control_fd
);
407 snprintf(temp_file
, sizeof(temp_file
), "%s_temp",
408 playlist
->control_filename
);
410 if (rename(playlist
->control_filename
, temp_file
) < 0)
413 temp_fd
= open(temp_file
, O_RDONLY
);
417 playlist
->control_fd
= open(playlist
->control_filename
,
418 O_CREAT
|O_RDWR
|O_TRUNC
, 0666);
419 if (playlist
->control_fd
< 0)
422 playlist
->filename
[playlist
->dirlen
-1] = '\0';
424 /* cannot call update_control() because of mutex */
425 result
= fdprintf(playlist
->control_fd
, "P:%d:%s:%s\n",
426 PLAYLIST_CONTROL_FILE_VERSION
, dir
, file
);
428 playlist
->filename
[playlist
->dirlen
-1] = c
;
438 playlist
->shuffle_modified
= false;
439 playlist
->deleted
= false;
440 playlist
->num_inserted_tracks
= 0;
442 for (i
=0; i
<playlist
->amount
; i
++)
444 if (playlist
->indices
[i
] & PLAYLIST_INSERT_TYPE_MASK
)
446 bool queue
= playlist
->indices
[i
] & PLAYLIST_QUEUE_MASK
;
447 char inserted_file
[MAX_PATH
+1];
449 lseek(temp_fd
, playlist
->indices
[i
] & PLAYLIST_SEEK_MASK
,
451 read_line(temp_fd
, inserted_file
, sizeof(inserted_file
));
453 result
= fdprintf(playlist
->control_fd
, "%c:%d:%d:",
454 queue
?'Q':'A', i
, playlist
->last_insert_pos
);
457 /* save the position in file where name is written */
458 int seek_pos
= lseek(playlist
->control_fd
, 0, SEEK_CUR
);
460 result
= fdprintf(playlist
->control_fd
, "%s\n",
463 playlist
->indices
[i
] =
464 (playlist
->indices
[i
] & ~PLAYLIST_SEEK_MASK
) | seek_pos
;
470 playlist
->num_inserted_tracks
++;
476 fsync(playlist
->control_fd
);
485 * store directory and name of playlist file
487 static void update_playlist_filename(struct playlist_info
* playlist
,
488 const char *dir
, const char *file
)
491 int dirlen
= strlen(dir
);
493 playlist
->utf8
= is_m3u8(file
);
495 /* If the dir does not end in trailing slash, we use a separator.
496 Otherwise we don't. */
497 if('/' != dir
[dirlen
-1])
503 playlist
->dirlen
= dirlen
;
505 snprintf(playlist
->filename
, sizeof(playlist
->filename
),
506 "%s%s%s", dir
, sep
, file
);
510 * calculate track offsets within a playlist file
512 static int add_indices_to_playlist(struct playlist_info
* playlist
,
513 char* buffer
, size_t buflen
)
517 unsigned int count
= 0;
522 if(-1 == playlist
->fd
)
523 playlist
->fd
= open_utf8(playlist
->filename
, O_RDONLY
);
525 return -1; /* failure */
526 if((i
= lseek(playlist
->fd
, 0, SEEK_CUR
)) > 0)
527 playlist
->utf8
= true; /* Override any earlier indication. */
529 splash(0, ID2P(LANG_WAIT
));
533 /* use mp3 buffer for maximum load speed */
535 #if CONFIG_CODEC != SWCODEC
536 talk_buffer_steal(); /* we use the mp3 buffer, need to tell */
537 buflen
= (audiobufend
- audiobuf
);
538 buffer
= (char *)audiobuf
;
540 buffer
= (char *)audio_get_buffer(false, &buflen
);
548 nread
= read(playlist
->fd
, buffer
, buflen
);
549 /* Terminate on EOF */
553 p
= (unsigned char *)buffer
;
555 for(count
=0; count
< nread
; count
++,p
++) {
557 /* Are we on a new line? */
558 if((*p
== '\n') || (*p
== '\r'))
568 if ( playlist
->amount
>= playlist
->max_playlist_size
) {
569 display_buffer_full();
574 /* Store a new entry */
575 playlist
->indices
[ playlist
->amount
] = i
+count
;
577 if (playlist
->filenames
)
578 playlist
->filenames
[ playlist
->amount
] = NULL
;
590 queue_post(&playlist_queue
, PLAYLIST_LOAD_POINTERS
, 0);
597 * Utility function to create a new playlist, fill it with the next or
598 * previous directory, shuffle it if needed, and start playback.
599 * If play_last is true and direction zero or negative, start playing
600 * the last file in the directory, otherwise start playing the first.
602 static int create_and_play_dir(int direction
, bool play_last
)
604 char dir
[MAX_PATH
+ 1];
609 res
= get_next_directory(dir
);
611 res
= get_previous_directory(dir
);
615 if (playlist_create(dir
, NULL
) != -1)
617 ft_build_playlist(tree_get_context(), 0);
619 if (global_settings
.playlist_shuffle
)
620 playlist_shuffle(current_tick
, -1);
622 if (play_last
&& direction
<= 0)
623 index
= current_playlist
.amount
- 1;
627 #if (CONFIG_CODEC != SWCODEC)
628 playlist_start(index
, 0);
632 /* we've overwritten the dircache when getting the next/previous dir,
633 so the tree browser context will need to be reloaded */
641 * Removes all tracks, from the playlist, leaving the presently playing
644 int playlist_remove_all_tracks(struct playlist_info
*playlist
)
648 if (playlist
== NULL
)
649 playlist
= ¤t_playlist
;
651 while (playlist
->index
> 0)
652 if ((result
= remove_track_from_playlist(playlist
, 0, true)) < 0)
655 while (playlist
->amount
> 1)
656 if ((result
= remove_track_from_playlist(playlist
, 1, true)) < 0)
659 if (playlist
->amount
== 1) {
660 playlist
->indices
[0] |= PLAYLIST_QUEUED
;
668 * Add track to playlist at specified position. There are seven special
669 * positions that can be specified:
670 * PLAYLIST_PREPEND - Add track at beginning of playlist
671 * PLAYLIST_INSERT - Add track after current song. NOTE: If
672 * there are already inserted tracks then track
673 * is added to the end of the insertion list
674 * PLAYLIST_INSERT_FIRST - Add track immediately after current song, no
675 * matter what other tracks have been inserted
676 * PLAYLIST_INSERT_LAST - Add track to end of playlist
677 * PLAYLIST_INSERT_SHUFFLED - Add track at some random point between the
678 * current playing track and end of playlist
679 * PLAYLIST_INSERT_LAST_SHUFFLED - Add tracks in random order to the end of
681 * PLAYLIST_REPLACE - Erase current playlist, Cue the current track
682 * and inster this track at the end.
684 static int add_track_to_playlist(struct playlist_info
* playlist
,
685 const char *filename
, int position
,
686 bool queue
, int seek_pos
)
688 int insert_position
, orig_position
;
689 unsigned long flags
= PLAYLIST_INSERT_TYPE_INSERT
;
692 insert_position
= orig_position
= position
;
694 if (playlist
->amount
>= playlist
->max_playlist_size
)
696 display_buffer_full();
702 case PLAYLIST_PREPEND
:
703 position
= insert_position
= playlist
->first_index
;
705 case PLAYLIST_INSERT
:
706 /* if there are already inserted tracks then add track to end of
707 insertion list else add after current playing track */
708 if (playlist
->last_insert_pos
>= 0 &&
709 playlist
->last_insert_pos
< playlist
->amount
&&
710 (playlist
->indices
[playlist
->last_insert_pos
]&
711 PLAYLIST_INSERT_TYPE_MASK
) == PLAYLIST_INSERT_TYPE_INSERT
)
712 position
= insert_position
= playlist
->last_insert_pos
+1;
713 else if (playlist
->amount
> 0)
714 position
= insert_position
= playlist
->index
+ 1;
716 position
= insert_position
= 0;
718 playlist
->last_insert_pos
= position
;
720 case PLAYLIST_INSERT_FIRST
:
721 if (playlist
->amount
> 0)
722 position
= insert_position
= playlist
->index
+ 1;
724 position
= insert_position
= 0;
726 playlist
->last_insert_pos
= position
;
728 case PLAYLIST_INSERT_LAST
:
729 if (playlist
->first_index
> 0)
730 position
= insert_position
= playlist
->first_index
;
732 position
= insert_position
= playlist
->amount
;
734 playlist
->last_insert_pos
= position
;
736 case PLAYLIST_INSERT_SHUFFLED
:
738 if (playlist
->started
)
741 int n
= playlist
->amount
-
742 rotate_index(playlist
, playlist
->index
);
749 position
= playlist
->index
+ offset
+ 1;
750 if (position
>= playlist
->amount
)
751 position
-= playlist
->amount
;
753 insert_position
= position
;
756 position
= insert_position
= (rand() % (playlist
->amount
+1));
759 case PLAYLIST_INSERT_LAST_SHUFFLED
:
761 position
= insert_position
= playlist
->last_shuffled_start
+
762 rand() % (playlist
->amount
- playlist
->last_shuffled_start
+ 1);
765 case PLAYLIST_REPLACE
:
766 if (playlist_remove_all_tracks(playlist
) < 0)
769 playlist
->last_insert_pos
= position
= insert_position
= playlist
->index
+ 1;
774 flags
|= PLAYLIST_QUEUED
;
776 /* shift indices so that track can be added */
777 for (i
=playlist
->amount
; i
>insert_position
; i
--)
779 playlist
->indices
[i
] = playlist
->indices
[i
-1];
781 if (playlist
->filenames
)
782 playlist
->filenames
[i
] = playlist
->filenames
[i
-1];
786 /* update stored indices if needed */
788 if (orig_position
< 0)
790 if (playlist
->amount
> 0 && insert_position
<= playlist
->index
&&
794 if (playlist
->amount
> 0 && insert_position
<= playlist
->first_index
&&
795 orig_position
!= PLAYLIST_PREPEND
&& playlist
->started
)
796 playlist
->first_index
++;
799 if (insert_position
< playlist
->last_insert_pos
||
800 (insert_position
== playlist
->last_insert_pos
&& position
< 0))
801 playlist
->last_insert_pos
++;
803 if (seek_pos
< 0 && playlist
->control_fd
>= 0)
805 int result
= update_control(playlist
,
806 (queue
?PLAYLIST_COMMAND_QUEUE
:PLAYLIST_COMMAND_ADD
), position
,
807 playlist
->last_insert_pos
, filename
, NULL
, &seek_pos
);
813 playlist
->indices
[insert_position
] = flags
| seek_pos
;
816 if (playlist
->filenames
)
817 playlist
->filenames
[insert_position
] = NULL
;
821 playlist
->num_inserted_tracks
++;
823 /* Update index for resume. */
824 playlist_update_resume_index();
826 return insert_position
;
830 * Callback for playlist_directory_tracksearch to insert track into
833 static int directory_search_callback(char* filename
, void* context
)
835 struct directory_search_context
* c
=
836 (struct directory_search_context
*) context
;
839 insert_pos
= add_track_to_playlist(c
->playlist
, filename
, c
->position
,
847 /* Make sure tracks are inserted in correct order if user requests
849 if (c
->position
== PLAYLIST_INSERT_FIRST
|| c
->position
>= 0)
850 c
->position
= insert_pos
+ 1;
852 if (((c
->count
)%PLAYLIST_DISPLAY_COUNT
) == 0)
854 unsigned char* count_str
;
857 count_str
= ID2P(LANG_PLAYLIST_QUEUE_COUNT
);
859 count_str
= ID2P(LANG_PLAYLIST_INSERT_COUNT
);
861 display_playlist_count(c
->count
, count_str
, false);
863 if ((c
->count
) == PLAYLIST_DISPLAY_COUNT
&&
864 (audio_status() & AUDIO_STATUS_PLAY
) &&
865 c
->playlist
->started
)
866 audio_flush_and_reload_tracks();
873 * remove track at specified position
875 static int remove_track_from_playlist(struct playlist_info
* playlist
,
876 int position
, bool write
)
881 if (playlist
->amount
<= 0)
884 inserted
= playlist
->indices
[position
] & PLAYLIST_INSERT_TYPE_MASK
;
886 /* shift indices now that track has been removed */
887 for (i
=position
; i
<playlist
->amount
; i
++)
889 playlist
->indices
[i
] = playlist
->indices
[i
+1];
891 if (playlist
->filenames
)
892 playlist
->filenames
[i
] = playlist
->filenames
[i
+1];
899 playlist
->num_inserted_tracks
--;
901 playlist
->deleted
= true;
903 /* update stored indices if needed */
904 if (position
< playlist
->index
)
907 if (position
< playlist
->first_index
)
909 playlist
->first_index
--;
912 if (position
<= playlist
->last_insert_pos
)
913 playlist
->last_insert_pos
--;
915 if (write
&& playlist
->control_fd
>= 0)
917 int result
= update_control(playlist
, PLAYLIST_COMMAND_DELETE
,
918 position
, -1, NULL
, NULL
, NULL
);
923 sync_control(playlist
, false);
926 /* Update index for resume. */
927 playlist_update_resume_index();
933 * randomly rearrange the array of indices for the playlist. If start_current
934 * is true then update the index to the new index of the current playing track
936 static int randomise_playlist(struct playlist_info
* playlist
,
937 unsigned int seed
, bool start_current
,
943 unsigned int current
= playlist
->indices
[playlist
->index
];
945 /* seed 0 is used to identify sorted playlist for resume purposes */
949 /* seed with the given seed */
952 /* randomise entire indices list */
953 for(count
= playlist
->amount
- 1; count
>= 0; count
--)
955 /* the rand is from 0 to RAND_MAX, so adjust to our value range */
956 candidate
= rand() % (count
+ 1);
958 /* now swap the values at the 'count' and 'candidate' positions */
959 store
= playlist
->indices
[candidate
];
960 playlist
->indices
[candidate
] = playlist
->indices
[count
];
961 playlist
->indices
[count
] = store
;
963 if (playlist
->filenames
)
965 store
= (long)playlist
->filenames
[candidate
];
966 playlist
->filenames
[candidate
] = playlist
->filenames
[count
];
967 playlist
->filenames
[count
] = (struct dircache_entry
*)store
;
973 find_and_set_playlist_index(playlist
, current
);
975 /* indices have been moved so last insert position is no longer valid */
976 playlist
->last_insert_pos
= -1;
978 playlist
->seed
= seed
;
979 if (playlist
->num_inserted_tracks
> 0 || playlist
->deleted
)
980 playlist
->shuffle_modified
= true;
984 update_control(playlist
, PLAYLIST_COMMAND_SHUFFLE
, seed
,
985 playlist
->first_index
, NULL
, NULL
, NULL
);
988 /* Update index for resume. */
989 playlist_update_resume_index();
995 * Sort the array of indices for the playlist. If start_current is true then
996 * set the index to the new index of the current song.
997 * Also while going to unshuffled mode set the first_index to 0.
999 static int sort_playlist(struct playlist_info
* playlist
, bool start_current
,
1002 unsigned int current
= playlist
->indices
[playlist
->index
];
1004 if (playlist
->amount
> 0)
1005 qsort(playlist
->indices
, playlist
->amount
,
1006 sizeof(playlist
->indices
[0]), compare
);
1008 #ifdef HAVE_DIRCACHE
1009 /** We need to re-check the song names from disk because qsort can't
1010 * sort two arrays at once :/
1011 * FIXME: Please implement a better way to do this. */
1012 memset(playlist
->filenames
, 0, playlist
->max_playlist_size
* sizeof(int));
1013 queue_post(&playlist_queue
, PLAYLIST_LOAD_POINTERS
, 0);
1017 find_and_set_playlist_index(playlist
, current
);
1019 /* indices have been moved so last insert position is no longer valid */
1020 playlist
->last_insert_pos
= -1;
1022 if (!playlist
->num_inserted_tracks
&& !playlist
->deleted
)
1023 playlist
->shuffle_modified
= false;
1024 if (write
&& playlist
->control_fd
>= 0)
1026 playlist
->first_index
= 0;
1027 update_control(playlist
, PLAYLIST_COMMAND_UNSHUFFLE
,
1028 playlist
->first_index
, -1, NULL
, NULL
, NULL
);
1031 /* Update index for resume. */
1032 playlist_update_resume_index();
1037 /* Calculate how many steps we have to really step when skipping entries
1040 static int calculate_step_count(const struct playlist_info
*playlist
, int steps
)
1042 int i
, count
, direction
;
1044 int stepped_count
= 0;
1057 index
= playlist
->index
;
1060 /* Boundary check */
1062 index
+= playlist
->amount
;
1063 if (index
>= playlist
->amount
)
1064 index
-= playlist
->amount
;
1066 /* Check if we found a bad entry. */
1067 if (playlist
->indices
[index
] & PLAYLIST_SKIPPED
)
1070 /* Are all entries bad? */
1071 if (stepped_count
++ > playlist
->amount
)
1078 } while (i
<= count
);
1083 /* Marks the index of the track to be skipped that is "steps" away from
1084 * current playing track.
1086 void playlist_skip_entry(struct playlist_info
*playlist
, int steps
)
1090 if (playlist
== NULL
)
1091 playlist
= ¤t_playlist
;
1093 /* need to account for already skipped tracks */
1094 steps
= calculate_step_count(playlist
, steps
);
1096 index
= playlist
->index
+ steps
;
1098 index
+= playlist
->amount
;
1099 else if (index
>= playlist
->amount
)
1100 index
-= playlist
->amount
;
1102 playlist
->indices
[index
] |= PLAYLIST_SKIPPED
;
1106 * returns the index of the track that is "steps" away from current playing
1109 static int get_next_index(const struct playlist_info
* playlist
, int steps
,
1112 int current_index
= playlist
->index
;
1113 int next_index
= -1;
1115 if (playlist
->amount
<= 0)
1118 if (repeat_mode
== -1)
1119 repeat_mode
= global_settings
.repeat_mode
;
1121 if (repeat_mode
== REPEAT_SHUFFLE
&& playlist
->amount
<= 1)
1122 repeat_mode
= REPEAT_ALL
;
1124 steps
= calculate_step_count(playlist
, steps
);
1125 switch (repeat_mode
)
1127 case REPEAT_SHUFFLE
:
1128 /* Treat repeat shuffle just like repeat off. At end of playlist,
1129 play will be resumed in playlist_next() */
1132 current_index
= rotate_index(playlist
, current_index
);
1133 next_index
= current_index
+steps
;
1134 if ((next_index
< 0) || (next_index
>= playlist
->amount
))
1137 next_index
= (next_index
+playlist
->first_index
) %
1144 #ifdef AB_REPEAT_ENABLE
1147 next_index
= current_index
;
1153 next_index
= (current_index
+steps
) % playlist
->amount
;
1154 while (next_index
< 0)
1155 next_index
+= playlist
->amount
;
1157 if (steps
>= playlist
->amount
)
1164 /* second time around so skip the queued files */
1165 for (i
=0; i
<playlist
->amount
; i
++)
1167 if (playlist
->indices
[index
] & PLAYLIST_QUEUE_MASK
)
1168 index
= (index
+1) % playlist
->amount
;
1180 /* No luck if the whole playlist was bad. */
1181 if (playlist
->indices
[next_index
] & PLAYLIST_SKIPPED
)
1188 * Search for the seek track and set appropriate indices. Used after shuffle
1189 * to make sure the current index is still pointing to correct track.
1191 static void find_and_set_playlist_index(struct playlist_info
* playlist
,
1196 /* Set the index to the current song */
1197 for (i
=0; i
<playlist
->amount
; i
++)
1199 if (playlist
->indices
[i
] == seek
)
1201 playlist
->index
= playlist
->first_index
= i
;
1207 /* Update index for resume. */
1208 playlist_update_resume_index();
1212 * used to sort track indices. Sort order is as follows:
1213 * 1. Prepended tracks (in prepend order)
1214 * 2. Playlist/directory tracks (in playlist order)
1215 * 3. Inserted/Appended tracks (in insert order)
1217 static int compare(const void* p1
, const void* p2
)
1219 unsigned long* e1
= (unsigned long*) p1
;
1220 unsigned long* e2
= (unsigned long*) p2
;
1221 unsigned long flags1
= *e1
& PLAYLIST_INSERT_TYPE_MASK
;
1222 unsigned long flags2
= *e2
& PLAYLIST_INSERT_TYPE_MASK
;
1224 if (flags1
== flags2
)
1225 return (*e1
& PLAYLIST_SEEK_MASK
) - (*e2
& PLAYLIST_SEEK_MASK
);
1226 else if (flags1
== PLAYLIST_INSERT_TYPE_PREPEND
||
1227 flags2
== PLAYLIST_INSERT_TYPE_APPEND
)
1229 else if (flags1
== PLAYLIST_INSERT_TYPE_APPEND
||
1230 flags2
== PLAYLIST_INSERT_TYPE_PREPEND
)
1232 else if (flags1
&& flags2
)
1233 return (*e1
& PLAYLIST_SEEK_MASK
) - (*e2
& PLAYLIST_SEEK_MASK
);
1238 #ifdef HAVE_DIRCACHE
1240 * Thread to update filename pointers to dircache on background
1241 * without affecting playlist load up performance. This thread also flushes
1242 * any pending control commands when the disk spins up.
1244 static void playlist_flush_callback(void *param
)
1247 struct playlist_info
*playlist
;
1248 playlist
= ¤t_playlist
;
1249 if (playlist
->control_fd
>= 0)
1251 if (playlist
->num_cached
> 0)
1253 mutex_lock(playlist
->control_mutex
);
1254 flush_cached_control(playlist
);
1255 mutex_unlock(playlist
->control_mutex
);
1257 sync_control(playlist
, true);
1261 static void playlist_thread(void)
1263 struct queue_event ev
;
1264 bool dirty_pointers
= false;
1265 static char tmp
[MAX_PATH
+1];
1267 struct playlist_info
*playlist
;
1274 #ifdef HAVE_DISK_STORAGE
1275 if (global_settings
.disk_spindown
> 1 &&
1276 global_settings
.disk_spindown
<= 5)
1277 sleep_time
= global_settings
.disk_spindown
- 1;
1282 queue_wait_w_tmo(&playlist_queue
, &ev
, HZ
*sleep_time
);
1286 case PLAYLIST_LOAD_POINTERS
:
1287 dirty_pointers
= true;
1290 /* Start the background scanning after either the disk spindown
1291 timeout or 5s, whichever is less */
1293 playlist
= ¤t_playlist
;
1294 if (playlist
->control_fd
>= 0)
1296 if (playlist
->num_cached
> 0)
1297 register_storage_idle_func(playlist_flush_callback
);
1300 if (!dirty_pointers
)
1303 if (!dircache_is_enabled() || !playlist
->filenames
1304 || playlist
->amount
<= 0)
1307 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
1310 for (index
= 0; index
< playlist
->amount
1311 && queue_empty(&playlist_queue
); index
++)
1313 /* Process only pointers that are not already loaded. */
1314 if (playlist
->filenames
[index
])
1317 control_file
= playlist
->indices
[index
] & PLAYLIST_INSERT_TYPE_MASK
;
1318 seek
= playlist
->indices
[index
] & PLAYLIST_SEEK_MASK
;
1320 /* Load the filename from playlist file. */
1321 if (get_filename(playlist
, index
, seek
, control_file
, tmp
,
1325 /* Set the dircache entry pointer. */
1326 playlist
->filenames
[index
] = dircache_get_entry_ptr(tmp
);
1328 /* And be on background so user doesn't notice any delays. */
1332 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
1335 dirty_pointers
= false;
1338 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
1339 case SYS_USB_CONNECTED
:
1340 usb_acknowledge(SYS_USB_CONNECTED_ACK
);
1341 usb_wait_for_disconnect(&playlist_queue
);
1350 * gets pathname for track at seek index
1352 static int get_filename(struct playlist_info
* playlist
, int index
, int seek
,
1353 bool control_file
, char *buf
, int buf_length
)
1357 char tmp_buf
[MAX_PATH
+1];
1358 char dir_buf
[MAX_PATH
+1];
1359 bool utf8
= playlist
->utf8
;
1361 if (buf_length
> MAX_PATH
+1)
1362 buf_length
= MAX_PATH
+1;
1364 #ifdef HAVE_DIRCACHE
1365 if (dircache_is_enabled() && playlist
->filenames
)
1367 if (playlist
->filenames
[index
] != NULL
)
1369 dircache_copy_path(playlist
->filenames
[index
], tmp_buf
, sizeof(tmp_buf
)-1);
1370 max
= strlen(tmp_buf
);
1377 if (playlist
->in_ram
&& !control_file
&& max
< 0)
1379 max
= strlcpy(tmp_buf
, &playlist
->buffer
[seek
], sizeof(tmp_buf
));
1383 mutex_lock(playlist
->control_mutex
);
1387 fd
= playlist
->control_fd
;
1392 if(-1 == playlist
->fd
)
1393 playlist
->fd
= open(playlist
->filename
, O_RDONLY
);
1401 if (lseek(fd
, seek
, SEEK_SET
) != seek
)
1405 max
= read(fd
, tmp_buf
, MIN((size_t) buf_length
, sizeof(tmp_buf
)));
1407 if ((max
> 0) && !utf8
)
1409 /* Use dir_buf as a temporary buffer. Note that dir_buf must
1410 * be as large as tmp_buf.
1412 max
= convert_m3u(tmp_buf
, max
, sizeof(tmp_buf
), dir_buf
);
1417 mutex_unlock(playlist
->control_mutex
);
1422 splash(HZ
*2, ID2P(LANG_PLAYLIST_CONTROL_ACCESS_ERROR
));
1424 splash(HZ
*2, ID2P(LANG_PLAYLIST_ACCESS_ERROR
));
1430 strlcpy(dir_buf
, playlist
->filename
, playlist
->dirlen
);
1432 return (format_track_path(buf
, tmp_buf
, buf_length
, max
, dir_buf
));
1435 static int get_next_directory(char *dir
){
1436 return get_next_dir(dir
,true,false);
1439 static int get_previous_directory(char *dir
){
1440 return get_next_dir(dir
,false,false);
1444 * search through all the directories (starting with the current) to find
1445 * one that has tracks to play
1447 static int get_next_dir(char *dir
, bool is_forward
, bool recursion
)
1449 struct playlist_info
* playlist
= ¤t_playlist
;
1451 char *start_dir
= NULL
;
1454 struct tree_context
* tc
= tree_get_context();
1455 int saved_dirfilter
= *(tc
->dirfilter
);
1457 /* process random folder advance */
1458 if (global_settings
.next_folder
== FOLDER_ADVANCE_RANDOM
)
1460 int fd
= open(ROCKBOX_DIR
"/folder_advance_list.dat", O_RDONLY
);
1463 char buffer
[MAX_PATH
];
1464 int folder_count
= 0;
1465 srand(current_tick
);
1466 *(tc
->dirfilter
) = SHOW_MUSIC
;
1467 tc
->sort_dir
= global_settings
.sort_dir
;
1468 read(fd
,&folder_count
,sizeof(int));
1473 i
= rand()%folder_count
;
1474 lseek(fd
,sizeof(int) + (MAX_PATH
*i
),SEEK_SET
);
1475 read(fd
,buffer
,MAX_PATH
);
1476 if (check_subdir_for_music(buffer
, "", false) ==0)
1482 *(tc
->dirfilter
) = saved_dirfilter
;
1483 tc
->sort_dir
= global_settings
.sort_dir
;
1489 /* not random folder advance (or random folder advance unavailable) */
1492 /* start with root */
1497 /* start with current directory */
1498 strlcpy(dir
, playlist
->filename
, playlist
->dirlen
);
1501 /* use the tree browser dircache to load files */
1502 *(tc
->dirfilter
) = SHOW_ALL
;
1504 /* set up sorting/direction */
1505 tc
->sort_dir
= global_settings
.sort_dir
;
1508 static const char sortpairs
[] =
1510 [SORT_ALPHA
] = SORT_ALPHA_REVERSED
,
1511 [SORT_DATE
] = SORT_DATE_REVERSED
,
1512 [SORT_TYPE
] = SORT_TYPE_REVERSED
,
1513 [SORT_ALPHA_REVERSED
] = SORT_ALPHA
,
1514 [SORT_DATE_REVERSED
] = SORT_DATE
,
1515 [SORT_TYPE_REVERSED
] = SORT_TYPE
,
1518 if ((unsigned)tc
->sort_dir
< sizeof(sortpairs
))
1519 tc
->sort_dir
= sortpairs
[tc
->sort_dir
];
1524 struct entry
*files
;
1528 if (ft_load(tc
, (dir
[0]=='\0')?"/":dir
) < 0)
1535 files
= (struct entry
*) tc
->dircache
;
1536 num_files
= tc
->filesindir
;
1538 for (i
=0; i
<num_files
; i
++)
1541 if (action_userabort(TIMEOUT_NOBLOCK
))
1548 if (files
[i
].attr
& ATTR_DIRECTORY
)
1552 result
= check_subdir_for_music(dir
, files
[i
].name
, true);
1559 else if (!strcmp(start_dir
, files
[i
].name
))
1566 /* move down to parent directory. current directory name is
1567 stored as the starting point for the search in parent */
1568 start_dir
= strrchr(dir
, '/');
1579 /* restore dirfilter */
1580 *(tc
->dirfilter
) = saved_dirfilter
;
1581 tc
->sort_dir
= global_settings
.sort_dir
;
1583 /* special case if nothing found: try start searching again from root */
1584 if (result
== -1 && !recursion
){
1585 result
= get_next_dir(dir
, is_forward
, true);
1592 * Checks if there are any music files in the dir or any of its
1593 * subdirectories. May be called recursively.
1595 static int check_subdir_for_music(char *dir
, const char *subdir
, bool recurse
)
1598 int dirlen
= strlen(dir
);
1601 struct entry
*files
;
1602 bool has_music
= false;
1603 bool has_subdir
= false;
1604 struct tree_context
* tc
= tree_get_context();
1606 snprintf(dir
+dirlen
, MAX_PATH
-dirlen
, "/%s", subdir
);
1608 if (ft_load(tc
, dir
) < 0)
1613 files
= (struct entry
*) tc
->dircache
;
1614 num_files
= tc
->filesindir
;
1616 for (i
=0; i
<num_files
; i
++)
1618 if (files
[i
].attr
& ATTR_DIRECTORY
)
1620 else if ((files
[i
].attr
& FILE_ATTR_MASK
) == FILE_ATTR_AUDIO
)
1630 if (has_subdir
&& recurse
)
1632 for (i
=0; i
<num_files
; i
++)
1634 if (action_userabort(TIMEOUT_NOBLOCK
))
1640 if (files
[i
].attr
& ATTR_DIRECTORY
)
1642 result
= check_subdir_for_music(dir
, files
[i
].name
, true);
1660 /* we now need to reload our current directory */
1661 if(ft_load(tc
, dir
) < 0)
1662 splash(HZ
*2, ID2P(LANG_PLAYLIST_DIRECTORY_ACCESS_ERROR
));
1668 * Returns absolute path of track
1670 static int format_track_path(char *dest
, char *src
, int buf_length
, int max
,
1677 /* Look for the end of the string */
1684 /* Now work back killing white space */
1686 ((src
[i
-1] == ' ') ||
1687 (src
[i
-1] == '\t')))
1690 /* Zero-terminate the file name */
1693 /* replace backslashes with forward slashes */
1694 for ( j
=0; j
<i
; j
++ )
1695 if ( src
[j
] == '\\' )
1700 strlcpy(dest
, src
, buf_length
);
1704 /* handle dos style drive letter */
1706 strlcpy(dest
, &src
[2], buf_length
);
1707 else if (!strncmp(src
, "../", 3))
1709 /* handle relative paths */
1711 while(!strncmp(&src
[i
], "../", 3))
1713 for (j
=0; j
<i
/3; j
++) {
1714 temp_ptr
= strrchr(dir
, '/');
1720 snprintf(dest
, buf_length
, "%s/%s", dir
, &src
[i
]);
1722 else if ( '.' == src
[0] && '/' == src
[1] ) {
1723 snprintf(dest
, buf_length
, "%s/%s", dir
, &src
[2]);
1726 snprintf(dest
, buf_length
, "%s/%s", dir
, src
);
1734 * Display splash message showing progress of playlist/directory insertion or
1737 static void display_playlist_count(int count
, const unsigned char *fmt
,
1740 static long talked_tick
= 0;
1741 long id
= P2ID(fmt
);
1742 if(global_settings
.talk_menu
&& id
>=0)
1744 if(final
|| (count
&& (talked_tick
== 0
1745 || TIME_AFTER(current_tick
, talked_tick
+5*HZ
))))
1747 talked_tick
= current_tick
;
1748 talk_number(count
, false);
1754 splashf(0, fmt
, count
, str(LANG_OFF_ABORT
));
1758 * Display buffer full message
1760 static void display_buffer_full(void)
1762 splash(HZ
*2, ID2P(LANG_PLAYLIST_BUFFER_FULL
));
1766 * Flush any cached control commands to disk. Called when playlist is being
1767 * modified. Returns 0 on success and -1 on failure.
1769 static int flush_cached_control(struct playlist_info
* playlist
)
1774 if (!playlist
->num_cached
)
1777 lseek(playlist
->control_fd
, 0, SEEK_END
);
1779 for (i
=0; i
<playlist
->num_cached
; i
++)
1781 struct playlist_control_cache
* cache
=
1782 &(playlist
->control_cache
[i
]);
1784 switch (cache
->command
)
1786 case PLAYLIST_COMMAND_PLAYLIST
:
1787 result
= fdprintf(playlist
->control_fd
, "P:%d:%s:%s\n",
1788 cache
->i1
, cache
->s1
, cache
->s2
);
1790 case PLAYLIST_COMMAND_ADD
:
1791 case PLAYLIST_COMMAND_QUEUE
:
1792 result
= fdprintf(playlist
->control_fd
, "%c:%d:%d:",
1793 (cache
->command
== PLAYLIST_COMMAND_ADD
)?'A':'Q',
1794 cache
->i1
, cache
->i2
);
1797 /* save the position in file where name is written */
1798 int* seek_pos
= (int *)cache
->data
;
1799 *seek_pos
= lseek(playlist
->control_fd
, 0, SEEK_CUR
);
1800 result
= fdprintf(playlist
->control_fd
, "%s\n",
1804 case PLAYLIST_COMMAND_DELETE
:
1805 result
= fdprintf(playlist
->control_fd
, "D:%d\n", cache
->i1
);
1807 case PLAYLIST_COMMAND_SHUFFLE
:
1808 result
= fdprintf(playlist
->control_fd
, "S:%d:%d\n",
1809 cache
->i1
, cache
->i2
);
1811 case PLAYLIST_COMMAND_UNSHUFFLE
:
1812 result
= fdprintf(playlist
->control_fd
, "U:%d\n", cache
->i1
);
1814 case PLAYLIST_COMMAND_RESET
:
1815 result
= fdprintf(playlist
->control_fd
, "R\n");
1827 playlist
->num_cached
= 0;
1828 playlist
->pending_control_sync
= true;
1835 splash(HZ
*2, ID2P(LANG_PLAYLIST_CONTROL_UPDATE_ERROR
));
1842 * Update control data with new command. Depending on the command, it may be
1843 * cached or flushed to disk.
1845 static int update_control(struct playlist_info
* playlist
,
1846 enum playlist_command command
, int i1
, int i2
,
1847 const char* s1
, const char* s2
, void* data
)
1850 struct playlist_control_cache
* cache
;
1853 mutex_lock(playlist
->control_mutex
);
1855 cache
= &(playlist
->control_cache
[playlist
->num_cached
++]);
1857 cache
->command
= command
;
1866 case PLAYLIST_COMMAND_PLAYLIST
:
1867 case PLAYLIST_COMMAND_ADD
:
1868 case PLAYLIST_COMMAND_QUEUE
:
1869 #ifndef HAVE_DIRCACHE
1870 case PLAYLIST_COMMAND_DELETE
:
1871 case PLAYLIST_COMMAND_RESET
:
1875 case PLAYLIST_COMMAND_SHUFFLE
:
1876 case PLAYLIST_COMMAND_UNSHUFFLE
:
1878 /* only flush when needed */
1882 if (flush
|| playlist
->num_cached
== PLAYLIST_MAX_CACHE
)
1883 result
= flush_cached_control(playlist
);
1885 mutex_unlock(playlist
->control_mutex
);
1891 * sync control file to disk
1893 static void sync_control(struct playlist_info
* playlist
, bool force
)
1895 #ifdef HAVE_DIRCACHE
1896 if (playlist
->started
&& force
)
1900 if (playlist
->started
)
1903 if (playlist
->pending_control_sync
)
1905 mutex_lock(playlist
->control_mutex
);
1906 fsync(playlist
->control_fd
);
1907 playlist
->pending_control_sync
= false;
1908 mutex_unlock(playlist
->control_mutex
);
1914 * Rotate indices such that first_index is index 0
1916 static int rotate_index(const struct playlist_info
* playlist
, int index
)
1918 index
-= playlist
->first_index
;
1920 index
+= playlist
->amount
;
1926 * Initialize playlist entries at startup
1928 void playlist_init(void)
1930 struct playlist_info
* playlist
= ¤t_playlist
;
1932 mutex_init(¤t_playlist_mutex
);
1933 mutex_init(&created_playlist_mutex
);
1935 playlist
->current
= true;
1936 strlcpy(playlist
->control_filename
, PLAYLIST_CONTROL_FILE
,
1937 sizeof(playlist
->control_filename
));
1939 playlist
->control_fd
= -1;
1940 playlist
->max_playlist_size
= global_settings
.max_files_in_playlist
;
1941 playlist
->indices
= buffer_alloc(
1942 playlist
->max_playlist_size
* sizeof(int));
1943 playlist
->buffer_size
=
1944 AVERAGE_FILENAME_LENGTH
* global_settings
.max_files_in_dir
;
1945 playlist
->buffer
= buffer_alloc(playlist
->buffer_size
);
1946 playlist
->control_mutex
= ¤t_playlist_mutex
;
1948 empty_playlist(playlist
, true);
1950 #ifdef HAVE_DIRCACHE
1951 playlist
->filenames
= buffer_alloc(
1952 playlist
->max_playlist_size
* sizeof(int));
1953 memset(playlist
->filenames
, 0,
1954 playlist
->max_playlist_size
* sizeof(int));
1955 create_thread(playlist_thread
, playlist_stack
, sizeof(playlist_stack
),
1956 0, playlist_thread_name
IF_PRIO(, PRIORITY_BACKGROUND
)
1958 queue_init(&playlist_queue
, true);
1963 * Clean playlist at shutdown
1965 void playlist_shutdown(void)
1967 struct playlist_info
* playlist
= ¤t_playlist
;
1969 if (playlist
->control_fd
>= 0)
1971 mutex_lock(playlist
->control_mutex
);
1973 if (playlist
->num_cached
> 0)
1974 flush_cached_control(playlist
);
1976 close(playlist
->control_fd
);
1978 mutex_unlock(playlist
->control_mutex
);
1983 * Create new playlist
1985 int playlist_create(const char *dir
, const char *file
)
1987 struct playlist_info
* playlist
= ¤t_playlist
;
1989 new_playlist(playlist
, dir
, file
);
1992 /* load the playlist file */
1993 add_indices_to_playlist(playlist
, NULL
, 0);
1998 #define PLAYLIST_COMMAND_SIZE (MAX_PATH+12)
2001 * Restore the playlist state based on control file commands. Called to
2002 * resume playback after shutdown.
2004 int playlist_resume(void)
2006 struct playlist_info
* playlist
= ¤t_playlist
;
2011 int control_file_size
= 0;
2015 /* use mp3 buffer for maximum load speed */
2016 #if CONFIG_CODEC != SWCODEC
2017 talk_buffer_steal(); /* we use the mp3 buffer, need to tell */
2018 buflen
= (audiobufend
- audiobuf
);
2019 buffer
= (char *)audiobuf
;
2021 buffer
= (char *)audio_get_buffer(false, &buflen
);
2024 empty_playlist(playlist
, true);
2026 splash(0, ID2P(LANG_WAIT
));
2027 playlist
->control_fd
= open(playlist
->control_filename
, O_RDWR
);
2028 if (playlist
->control_fd
< 0)
2030 splash(HZ
*2, ID2P(LANG_PLAYLIST_CONTROL_ACCESS_ERROR
));
2033 playlist
->control_created
= true;
2035 control_file_size
= filesize(playlist
->control_fd
);
2036 if (control_file_size
<= 0)
2038 splash(HZ
*2, ID2P(LANG_PLAYLIST_CONTROL_ACCESS_ERROR
));
2042 /* read a small amount first to get the header */
2043 nread
= read(playlist
->control_fd
, buffer
,
2044 PLAYLIST_COMMAND_SIZE
<buflen
?PLAYLIST_COMMAND_SIZE
:buflen
);
2047 splash(HZ
*2, ID2P(LANG_PLAYLIST_CONTROL_ACCESS_ERROR
));
2051 playlist
->started
= true;
2057 enum playlist_command current_command
= PLAYLIST_COMMAND_COMMENT
;
2058 int last_newline
= 0;
2060 bool newline
= true;
2061 bool exit_loop
= false;
2066 unsigned long last_tick
= current_tick
;
2067 bool useraborted
= false;
2069 for(count
=0; count
<nread
&& !exit_loop
&& !useraborted
; count
++,p
++)
2071 /* So a splash while we are loading. */
2072 if (TIME_AFTER(current_tick
, last_tick
+ HZ
/4))
2074 splashf(0, str(LANG_LOADING_PERCENT
),
2075 (total_read
+count
)*100/control_file_size
,
2076 str(LANG_OFF_ABORT
));
2077 if (action_userabort(TIMEOUT_NOBLOCK
))
2082 last_tick
= current_tick
;
2085 /* Are we on a new line? */
2086 if((*p
== '\n') || (*p
== '\r'))
2090 /* save last_newline in case we need to load more data */
2091 last_newline
= count
;
2093 switch (current_command
)
2095 case PLAYLIST_COMMAND_PLAYLIST
:
2097 /* str1=version str2=dir str3=file */
2113 version
= atoi(str1
);
2115 if (version
!= PLAYLIST_CONTROL_FILE_VERSION
)
2118 update_playlist_filename(playlist
, str2
, str3
);
2120 if (str3
[0] != '\0')
2122 /* NOTE: add_indices_to_playlist() overwrites the
2123 audiobuf so we need to reload control file
2125 add_indices_to_playlist(playlist
, NULL
, 0);
2127 else if (str2
[0] != '\0')
2129 playlist
->in_ram
= true;
2130 resume_directory(str2
);
2133 /* load the rest of the data */
2139 case PLAYLIST_COMMAND_ADD
:
2140 case PLAYLIST_COMMAND_QUEUE
:
2142 /* str1=position str2=last_position str3=file */
2143 int position
, last_position
;
2146 if (!str1
|| !str2
|| !str3
)
2153 position
= atoi(str1
);
2154 last_position
= atoi(str2
);
2156 queue
= (current_command
== PLAYLIST_COMMAND_ADD
)?
2159 /* seek position is based on str3's position in
2161 if (add_track_to_playlist(playlist
, str3
, position
,
2162 queue
, total_read
+(str3
-buffer
)) < 0)
2165 playlist
->last_insert_pos
= last_position
;
2169 case PLAYLIST_COMMAND_DELETE
:
2181 position
= atoi(str1
);
2183 if (remove_track_from_playlist(playlist
, position
,
2189 case PLAYLIST_COMMAND_SHUFFLE
:
2191 /* str1=seed str2=first_index */
2203 /* Always sort list before shuffling */
2204 sort_playlist(playlist
, false, false);
2208 playlist
->first_index
= atoi(str2
);
2210 if (randomise_playlist(playlist
, seed
, false,
2216 case PLAYLIST_COMMAND_UNSHUFFLE
:
2218 /* str1=first_index */
2226 playlist
->first_index
= atoi(str1
);
2228 if (sort_playlist(playlist
, false, false) < 0)
2234 case PLAYLIST_COMMAND_RESET
:
2236 playlist
->last_insert_pos
= -1;
2239 case PLAYLIST_COMMAND_COMMENT
:
2246 /* to ignore any extra newlines */
2247 current_command
= PLAYLIST_COMMAND_COMMENT
;
2253 /* first non-comment line must always specify playlist */
2254 if (first
&& *p
!= 'P' && *p
!= '#')
2264 /* playlist can only be specified once */
2272 current_command
= PLAYLIST_COMMAND_PLAYLIST
;
2275 current_command
= PLAYLIST_COMMAND_ADD
;
2278 current_command
= PLAYLIST_COMMAND_QUEUE
;
2281 current_command
= PLAYLIST_COMMAND_DELETE
;
2284 current_command
= PLAYLIST_COMMAND_SHUFFLE
;
2287 current_command
= PLAYLIST_COMMAND_UNSHUFFLE
;
2290 current_command
= PLAYLIST_COMMAND_RESET
;
2293 current_command
= PLAYLIST_COMMAND_COMMENT
;
2306 else if(current_command
!= PLAYLIST_COMMAND_COMMENT
)
2308 /* all control file strings are separated with a colon.
2309 Replace the colon with 0 to get proper strings that can be
2310 used by commands above */
2316 if ((count
+1) < nread
)
2330 /* allow last string to contain colons */
2341 splash(HZ
*2, ID2P(LANG_PLAYLIST_CONTROL_INVALID
));
2347 splash(HZ
*2, ID2P(LANG_CANCEL
));
2350 if (!newline
|| (exit_loop
&& count
<nread
))
2352 if ((total_read
+ count
) >= control_file_size
)
2354 /* no newline at end of control file */
2355 splash(HZ
*2, ID2P(LANG_PLAYLIST_CONTROL_INVALID
));
2359 /* We didn't end on a newline or we exited loop prematurely.
2360 Either way, re-read the remainder. */
2361 count
= last_newline
;
2362 lseek(playlist
->control_fd
, total_read
+count
, SEEK_SET
);
2365 total_read
+= count
;
2368 /* still looking for header */
2369 nread
= read(playlist
->control_fd
, buffer
,
2370 PLAYLIST_COMMAND_SIZE
<buflen
?PLAYLIST_COMMAND_SIZE
:buflen
);
2372 nread
= read(playlist
->control_fd
, buffer
, buflen
);
2374 /* Terminate on EOF */
2381 #ifdef HAVE_DIRCACHE
2382 queue_post(&playlist_queue
, PLAYLIST_LOAD_POINTERS
, 0);
2389 * Add track to in_ram playlist. Used when playing directories.
2391 int playlist_add(const char *filename
)
2393 struct playlist_info
* playlist
= ¤t_playlist
;
2394 int len
= strlen(filename
);
2396 if((len
+1 > playlist
->buffer_size
- playlist
->buffer_end_pos
) ||
2397 (playlist
->amount
>= playlist
->max_playlist_size
))
2399 display_buffer_full();
2403 playlist
->indices
[playlist
->amount
] = playlist
->buffer_end_pos
;
2404 #ifdef HAVE_DIRCACHE
2405 playlist
->filenames
[playlist
->amount
] = NULL
;
2409 strcpy(&playlist
->buffer
[playlist
->buffer_end_pos
], filename
);
2410 playlist
->buffer_end_pos
+= len
;
2411 playlist
->buffer
[playlist
->buffer_end_pos
++] = '\0';
2416 /* shuffle newly created playlist using random seed. */
2417 int playlist_shuffle(int random_seed
, int start_index
)
2419 struct playlist_info
* playlist
= ¤t_playlist
;
2421 bool start_current
= false;
2423 if (start_index
>= 0 && global_settings
.play_selected
)
2425 /* store the seek position before the shuffle */
2426 playlist
->index
= playlist
->first_index
= start_index
;
2427 start_current
= true;
2430 randomise_playlist(playlist
, random_seed
, start_current
, true);
2432 return playlist
->index
;
2435 /* start playing current playlist at specified index/offset */
2436 void playlist_start(int start_index
, int offset
)
2438 struct playlist_info
* playlist
= ¤t_playlist
;
2440 /* Cancel FM radio selection as previous music. For cases where we start
2441 playback without going to the WPS, such as playlist insert.. or
2442 playlist catalog. */
2443 previous_music_is_wps();
2445 playlist
->index
= start_index
;
2447 #if CONFIG_CODEC != SWCODEC
2448 talk_buffer_steal(); /* will use the mp3 buffer */
2451 playlist
->started
= true;
2452 sync_control(playlist
, false);
2456 /* Returns false if 'steps' is out of bounds, else true */
2457 bool playlist_check(int steps
)
2459 struct playlist_info
* playlist
= ¤t_playlist
;
2461 /* always allow folder navigation */
2462 if (global_settings
.next_folder
&& playlist
->in_ram
)
2465 int index
= get_next_index(playlist
, steps
, -1);
2467 if (index
< 0 && steps
>= 0 && global_settings
.repeat_mode
== REPEAT_SHUFFLE
)
2468 index
= get_next_index(playlist
, steps
, REPEAT_ALL
);
2470 return (index
>= 0);
2473 /* get trackname of track that is "steps" away from current playing track.
2474 NULL is used to identify end of playlist */
2475 const char* playlist_peek(int steps
, char* buf
, size_t buf_size
)
2477 struct playlist_info
* playlist
= ¤t_playlist
;
2483 index
= get_next_index(playlist
, steps
, -1);
2487 control_file
= playlist
->indices
[index
] & PLAYLIST_INSERT_TYPE_MASK
;
2488 seek
= playlist
->indices
[index
] & PLAYLIST_SEEK_MASK
;
2490 if (get_filename(playlist
, index
, seek
, control_file
, buf
,
2496 if (!playlist
->in_ram
|| control_file
)
2498 /* remove bogus dirs from beginning of path
2499 (workaround for buggy playlist creation tools) */
2502 if (file_exists(temp_ptr
))
2505 temp_ptr
= strchr(temp_ptr
+1, '/');
2510 /* Even though this is an invalid file, we still need to pass a
2511 file name to the caller because NULL is used to indicate end
2521 * Update indices as track has changed
2523 int playlist_next(int steps
)
2525 struct playlist_info
* playlist
= ¤t_playlist
;
2529 #ifdef AB_REPEAT_ENABLE
2530 && (global_settings
.repeat_mode
!= REPEAT_AB
)
2532 && (global_settings
.repeat_mode
!= REPEAT_ONE
) )
2536 /* We need to delete all the queued songs */
2537 for (i
=0, j
=steps
; i
<j
; i
++)
2539 index
= get_next_index(playlist
, i
, -1);
2541 if (playlist
->indices
[index
] & PLAYLIST_QUEUE_MASK
)
2543 remove_track_from_playlist(playlist
, index
, true);
2544 steps
--; /* one less track */
2549 index
= get_next_index(playlist
, steps
, -1);
2553 /* end of playlist... or is it */
2554 if (global_settings
.repeat_mode
== REPEAT_SHUFFLE
&&
2555 playlist
->amount
> 1)
2557 /* Repeat shuffle mode. Re-shuffle playlist and resume play */
2558 playlist
->first_index
= 0;
2559 sort_playlist(playlist
, false, false);
2560 randomise_playlist(playlist
, current_tick
, false, true);
2561 #if CONFIG_CODEC != SWCODEC
2562 playlist_start(0, 0);
2564 playlist
->index
= 0;
2567 else if (playlist
->in_ram
&& global_settings
.next_folder
)
2569 index
= create_and_play_dir(steps
, true);
2573 playlist
->index
= index
;
2580 playlist
->index
= index
;
2582 if (playlist
->last_insert_pos
>= 0 && steps
> 0)
2584 /* check to see if we've gone beyond the last inserted track */
2585 int cur
= rotate_index(playlist
, index
);
2586 int last_pos
= rotate_index(playlist
, playlist
->last_insert_pos
);
2590 /* reset last inserted track */
2591 playlist
->last_insert_pos
= -1;
2593 if (playlist
->control_fd
>= 0)
2595 int result
= update_control(playlist
, PLAYLIST_COMMAND_RESET
,
2596 -1, -1, NULL
, NULL
, NULL
);
2601 sync_control(playlist
, false);
2609 /* try playing next or previous folder */
2610 bool playlist_next_dir(int direction
)
2612 /* not to mess up real playlists */
2613 if(!current_playlist
.in_ram
)
2616 return create_and_play_dir(direction
, false) >= 0;
2619 /* Get resume info for current playing song. If return value is -1 then
2620 settings shouldn't be saved. */
2621 int playlist_get_resume_info(int *resume_index
)
2623 struct playlist_info
* playlist
= ¤t_playlist
;
2625 *resume_index
= playlist
->index
;
2630 /* Get current playlist index. */
2631 int playlist_get_index(void)
2633 return current_playlist
.index
;
2636 /* Update resume index within playlist_info structure. */
2637 void playlist_update_resume_index(void)
2639 struct playlist_info
* playlist
= ¤t_playlist
;
2640 playlist
->resume_index
= playlist
->index
;
2643 /* Update resume info for current playing song. Returns -1 on error. */
2644 int playlist_update_resume_info(const struct mp3entry
* id3
)
2646 struct playlist_info
* playlist
= ¤t_playlist
;
2650 if (global_status
.resume_index
!= playlist
->resume_index
||
2651 global_status
.resume_offset
!= id3
->offset
)
2653 global_status
.resume_index
= playlist
->resume_index
;
2654 global_status
.resume_offset
= id3
->offset
;
2660 global_status
.resume_index
= -1;
2661 global_status
.resume_offset
= -1;
2668 /* Returns index of current playing track for display purposes. This value
2669 should not be used for resume purposes as it doesn't represent the actual
2670 index into the playlist */
2671 int playlist_get_display_index(void)
2673 struct playlist_info
* playlist
= ¤t_playlist
;
2675 /* first_index should always be index 0 for display purposes */
2676 int index
= rotate_index(playlist
, playlist
->index
);
2681 /* returns number of tracks in current playlist */
2682 int playlist_amount(void)
2684 return playlist_amount_ex(NULL
);
2686 /* set playlist->last_shuffle_start to playlist->amount for
2687 PLAYLIST_INSERT_LAST_SHUFFLED command purposes*/
2688 void playlist_set_last_shuffled_start(void)
2690 struct playlist_info
* playlist
= ¤t_playlist
;
2691 playlist
->last_shuffled_start
= playlist
->amount
;
2694 * Create a new playlist If playlist is not NULL then we're loading a
2695 * playlist off disk for viewing/editing. The index_buffer is used to store
2696 * playlist indices (required for and only used if !current playlist). The
2697 * temp_buffer (if not NULL) is used as a scratchpad when loading indices.
2699 int playlist_create_ex(struct playlist_info
* playlist
,
2700 const char* dir
, const char* file
,
2701 void* index_buffer
, int index_buffer_size
,
2702 void* temp_buffer
, int temp_buffer_size
)
2705 playlist
= ¤t_playlist
;
2708 /* Initialize playlist structure */
2709 int r
= rand() % 10;
2710 playlist
->current
= false;
2712 /* Use random name for control file */
2713 snprintf(playlist
->control_filename
, sizeof(playlist
->control_filename
),
2714 "%s.%d", PLAYLIST_CONTROL_FILE
, r
);
2716 playlist
->control_fd
= -1;
2720 int num_indices
= index_buffer_size
/ sizeof(int);
2722 #ifdef HAVE_DIRCACHE
2725 if (num_indices
> global_settings
.max_files_in_playlist
)
2726 num_indices
= global_settings
.max_files_in_playlist
;
2728 playlist
->max_playlist_size
= num_indices
;
2729 playlist
->indices
= index_buffer
;
2730 #ifdef HAVE_DIRCACHE
2731 playlist
->filenames
= (const struct dircache_entry
**)
2732 &playlist
->indices
[num_indices
];
2737 playlist
->max_playlist_size
= current_playlist
.max_playlist_size
;
2738 playlist
->indices
= current_playlist
.indices
;
2739 #ifdef HAVE_DIRCACHE
2740 playlist
->filenames
= current_playlist
.filenames
;
2744 playlist
->buffer_size
= 0;
2745 playlist
->buffer
= NULL
;
2746 playlist
->control_mutex
= &created_playlist_mutex
;
2749 new_playlist(playlist
, dir
, file
);
2752 /* load the playlist file */
2753 add_indices_to_playlist(playlist
, temp_buffer
, temp_buffer_size
);
2759 * Set the specified playlist as the current.
2760 * NOTE: You will get undefined behaviour if something is already playing so
2761 * remember to stop before calling this. Also, this call will
2762 * effectively close your playlist, making it unusable.
2764 int playlist_set_current(struct playlist_info
* playlist
)
2766 if (!playlist
|| (check_control(playlist
) < 0))
2769 empty_playlist(¤t_playlist
, false);
2771 strlcpy(current_playlist
.filename
, playlist
->filename
,
2772 sizeof(current_playlist
.filename
));
2774 current_playlist
.utf8
= playlist
->utf8
;
2775 current_playlist
.fd
= playlist
->fd
;
2777 close(playlist
->control_fd
);
2778 close(current_playlist
.control_fd
);
2779 remove(current_playlist
.control_filename
);
2780 if (rename(playlist
->control_filename
,
2781 current_playlist
.control_filename
) < 0)
2783 current_playlist
.control_fd
= open(current_playlist
.control_filename
,
2785 if (current_playlist
.control_fd
< 0)
2787 current_playlist
.control_created
= true;
2789 current_playlist
.dirlen
= playlist
->dirlen
;
2791 if (playlist
->indices
&& playlist
->indices
!= current_playlist
.indices
)
2793 memcpy(current_playlist
.indices
, playlist
->indices
,
2794 playlist
->max_playlist_size
*sizeof(int));
2795 #ifdef HAVE_DIRCACHE
2796 memcpy(current_playlist
.filenames
, playlist
->filenames
,
2797 playlist
->max_playlist_size
*sizeof(int));
2801 current_playlist
.first_index
= playlist
->first_index
;
2802 current_playlist
.amount
= playlist
->amount
;
2803 current_playlist
.last_insert_pos
= playlist
->last_insert_pos
;
2804 current_playlist
.seed
= playlist
->seed
;
2805 current_playlist
.shuffle_modified
= playlist
->shuffle_modified
;
2806 current_playlist
.deleted
= playlist
->deleted
;
2807 current_playlist
.num_inserted_tracks
= playlist
->num_inserted_tracks
;
2809 memcpy(current_playlist
.control_cache
, playlist
->control_cache
,
2810 sizeof(current_playlist
.control_cache
));
2811 current_playlist
.num_cached
= playlist
->num_cached
;
2812 current_playlist
.pending_control_sync
= playlist
->pending_control_sync
;
2816 struct playlist_info
*playlist_get_current(void)
2818 return ¤t_playlist
;
2821 * Close files and delete control file for non-current playlist.
2823 void playlist_close(struct playlist_info
* playlist
)
2828 if (playlist
->fd
>= 0)
2829 close(playlist
->fd
);
2831 if (playlist
->control_fd
>= 0)
2832 close(playlist
->control_fd
);
2834 if (playlist
->control_created
)
2835 remove(playlist
->control_filename
);
2838 void playlist_sync(struct playlist_info
* playlist
)
2841 playlist
= ¤t_playlist
;
2843 sync_control(playlist
, false);
2844 if ((audio_status() & AUDIO_STATUS_PLAY
) && playlist
->started
)
2845 audio_flush_and_reload_tracks();
2847 #ifdef HAVE_DIRCACHE
2848 queue_post(&playlist_queue
, PLAYLIST_LOAD_POINTERS
, 0);
2853 * Insert track into playlist at specified position (or one of the special
2854 * positions). Returns position where track was inserted or -1 if error.
2856 int playlist_insert_track(struct playlist_info
* playlist
, const char *filename
,
2857 int position
, bool queue
, bool sync
)
2862 playlist
= ¤t_playlist
;
2864 if (check_control(playlist
) < 0)
2866 splash(HZ
*2, ID2P(LANG_PLAYLIST_CONTROL_ACCESS_ERROR
));
2870 result
= add_track_to_playlist(playlist
, filename
, position
, queue
, -1);
2872 /* Check if we want manually sync later. For example when adding
2873 * bunch of files from tagcache, syncing after every file wouldn't be
2874 * a good thing to do. */
2875 if (sync
&& result
>= 0)
2876 playlist_sync(playlist
);
2882 * Insert all tracks from specified directory into playlist.
2884 int playlist_insert_directory(struct playlist_info
* playlist
,
2885 const char *dirname
, int position
, bool queue
,
2889 unsigned char *count_str
;
2890 struct directory_search_context context
;
2893 playlist
= ¤t_playlist
;
2895 if (check_control(playlist
) < 0)
2897 splash(HZ
*2, ID2P(LANG_PLAYLIST_CONTROL_ACCESS_ERROR
));
2901 if (position
== PLAYLIST_REPLACE
)
2903 if (playlist_remove_all_tracks(playlist
) == 0)
2904 position
= PLAYLIST_INSERT_LAST
;
2910 count_str
= ID2P(LANG_PLAYLIST_QUEUE_COUNT
);
2912 count_str
= ID2P(LANG_PLAYLIST_INSERT_COUNT
);
2914 display_playlist_count(0, count_str
, false);
2916 context
.playlist
= playlist
;
2917 context
.position
= position
;
2918 context
.queue
= queue
;
2923 result
= playlist_directory_tracksearch(dirname
, recurse
,
2924 directory_search_callback
, &context
);
2926 sync_control(playlist
, false);
2930 display_playlist_count(context
.count
, count_str
, true);
2932 if ((audio_status() & AUDIO_STATUS_PLAY
) && playlist
->started
)
2933 audio_flush_and_reload_tracks();
2935 #ifdef HAVE_DIRCACHE
2936 queue_post(&playlist_queue
, PLAYLIST_LOAD_POINTERS
, 0);
2943 * Insert all tracks from specified playlist into dynamic playlist.
2945 int playlist_insert_playlist(struct playlist_info
* playlist
, const char *filename
,
2946 int position
, bool queue
)
2952 unsigned char *count_str
;
2953 char temp_buf
[MAX_PATH
+1];
2954 char trackname
[MAX_PATH
+1];
2957 bool utf8
= is_m3u8(filename
);
2960 playlist
= ¤t_playlist
;
2962 if (check_control(playlist
) < 0)
2964 splash(HZ
*2, ID2P(LANG_PLAYLIST_CONTROL_ACCESS_ERROR
));
2968 fd
= open_utf8(filename
, O_RDONLY
);
2971 splash(HZ
*2, ID2P(LANG_PLAYLIST_ACCESS_ERROR
));
2975 /* we need the directory name for formatting purposes */
2978 temp_ptr
= strrchr(filename
+1,'/');
2985 count_str
= ID2P(LANG_PLAYLIST_QUEUE_COUNT
);
2987 count_str
= ID2P(LANG_PLAYLIST_INSERT_COUNT
);
2989 display_playlist_count(count
, count_str
, false);
2991 if (position
== PLAYLIST_REPLACE
)
2993 if (playlist_remove_all_tracks(playlist
) == 0)
2994 position
= PLAYLIST_INSERT_LAST
;
3000 while ((max
= read_line(fd
, temp_buf
, sizeof(temp_buf
))) > 0)
3003 if (action_userabort(TIMEOUT_NOBLOCK
))
3006 if (temp_buf
[0] != '#' && temp_buf
[0] != '\0')
3012 /* Use trackname as a temporay buffer. Note that trackname must
3013 * be as large as temp_buf.
3015 max
= convert_m3u(temp_buf
, max
, sizeof(temp_buf
), trackname
);
3018 /* we need to format so that relative paths are correctly
3020 if (format_track_path(trackname
, temp_buf
, sizeof(trackname
), max
,
3027 insert_pos
= add_track_to_playlist(playlist
, trackname
, position
,
3036 /* Make sure tracks are inserted in correct order if user
3037 requests INSERT_FIRST */
3038 if (position
== PLAYLIST_INSERT_FIRST
|| position
>= 0)
3039 position
= insert_pos
+ 1;
3043 if ((count
%PLAYLIST_DISPLAY_COUNT
) == 0)
3045 display_playlist_count(count
, count_str
, false);
3047 if (count
== PLAYLIST_DISPLAY_COUNT
&&
3048 (audio_status() & AUDIO_STATUS_PLAY
) &&
3050 audio_flush_and_reload_tracks();
3054 /* let the other threads work */
3063 sync_control(playlist
, false);
3067 display_playlist_count(count
, count_str
, true);
3069 if ((audio_status() & AUDIO_STATUS_PLAY
) && playlist
->started
)
3070 audio_flush_and_reload_tracks();
3072 #ifdef HAVE_DIRCACHE
3073 queue_post(&playlist_queue
, PLAYLIST_LOAD_POINTERS
, 0);
3080 * Delete track at specified index. If index is PLAYLIST_DELETE_CURRENT then
3081 * we want to delete the current playing track.
3083 int playlist_delete(struct playlist_info
* playlist
, int index
)
3088 playlist
= ¤t_playlist
;
3090 if (check_control(playlist
) < 0)
3092 splash(HZ
*2, ID2P(LANG_PLAYLIST_CONTROL_ACCESS_ERROR
));
3096 if (index
== PLAYLIST_DELETE_CURRENT
)
3097 index
= playlist
->index
;
3099 result
= remove_track_from_playlist(playlist
, index
, true);
3101 if (result
!= -1 && (audio_status() & AUDIO_STATUS_PLAY
) &&
3103 audio_flush_and_reload_tracks();
3109 * Move track at index to new_index. Tracks between the two are shifted
3110 * appropriately. Returns 0 on success and -1 on failure.
3112 int playlist_move(struct playlist_info
* playlist
, int index
, int new_index
)
3118 bool current
= false;
3120 char filename
[MAX_PATH
];
3123 playlist
= ¤t_playlist
;
3125 if (check_control(playlist
) < 0)
3127 splash(HZ
*2, ID2P(LANG_PLAYLIST_CONTROL_ACCESS_ERROR
));
3131 if (index
== new_index
)
3134 if (index
== playlist
->index
)
3135 /* Moving the current track */
3138 control_file
= playlist
->indices
[index
] & PLAYLIST_INSERT_TYPE_MASK
;
3139 queue
= playlist
->indices
[index
] & PLAYLIST_QUEUE_MASK
;
3140 seek
= playlist
->indices
[index
] & PLAYLIST_SEEK_MASK
;
3142 if (get_filename(playlist
, index
, seek
, control_file
, filename
,
3143 sizeof(filename
)) < 0)
3146 /* We want to insert the track at the position that was specified by
3147 new_index. This may be different then new_index because of the
3148 shifting that will occur after the delete.
3149 We calculate this before we do the remove as it depends on the
3150 size of the playlist before the track removal */
3151 r
= rotate_index(playlist
, new_index
);
3153 /* Delete track from original position */
3154 result
= remove_track_from_playlist(playlist
, index
, true);
3160 new_index
= PLAYLIST_PREPEND
;
3161 else if (r
== playlist
->amount
)
3163 new_index
= PLAYLIST_INSERT_LAST
;
3165 /* Calculate index of desired position */
3166 new_index
= (r
+playlist
->first_index
)%playlist
->amount
;
3168 result
= add_track_to_playlist(playlist
, filename
, new_index
, queue
,
3175 /* Moved the current track */
3178 case PLAYLIST_PREPEND
:
3179 playlist
->index
= playlist
->first_index
;
3181 case PLAYLIST_INSERT_LAST
:
3182 playlist
->index
= playlist
->first_index
- 1;
3183 if (playlist
->index
< 0)
3184 playlist
->index
+= playlist
->amount
;
3187 playlist
->index
= new_index
;
3192 if ((audio_status() & AUDIO_STATUS_PLAY
) && playlist
->started
)
3193 audio_flush_and_reload_tracks();
3197 #ifdef HAVE_DIRCACHE
3198 queue_post(&playlist_queue
, PLAYLIST_LOAD_POINTERS
, 0);
3201 /* Update index for resume. */
3202 playlist_update_resume_index();
3207 /* shuffle currently playing playlist */
3208 int playlist_randomise(struct playlist_info
* playlist
, unsigned int seed
,
3214 playlist
= ¤t_playlist
;
3216 check_control(playlist
);
3218 result
= randomise_playlist(playlist
, seed
, start_current
, true);
3220 if (result
!= -1 && (audio_status() & AUDIO_STATUS_PLAY
) &&
3222 audio_flush_and_reload_tracks();
3227 /* sort currently playing playlist */
3228 int playlist_sort(struct playlist_info
* playlist
, bool start_current
)
3233 playlist
= ¤t_playlist
;
3235 check_control(playlist
);
3237 result
= sort_playlist(playlist
, start_current
, true);
3239 if (result
!= -1 && (audio_status() & AUDIO_STATUS_PLAY
) &&
3241 audio_flush_and_reload_tracks();
3246 /* returns true if playlist has been modified */
3247 bool playlist_modified(const struct playlist_info
* playlist
)
3250 playlist
= ¤t_playlist
;
3252 if (playlist
->shuffle_modified
||
3253 playlist
->deleted
||
3254 playlist
->num_inserted_tracks
> 0)
3260 /* returns index of first track in playlist */
3261 int playlist_get_first_index(const struct playlist_info
* playlist
)
3264 playlist
= ¤t_playlist
;
3266 return playlist
->first_index
;
3269 /* returns shuffle seed of playlist */
3270 int playlist_get_seed(const struct playlist_info
* playlist
)
3273 playlist
= ¤t_playlist
;
3275 return playlist
->seed
;
3278 /* returns number of tracks in playlist (includes queued/inserted tracks) */
3279 int playlist_amount_ex(const struct playlist_info
* playlist
)
3282 playlist
= ¤t_playlist
;
3284 return playlist
->amount
;
3287 /* returns full path of playlist (minus extension) */
3288 char *playlist_name(const struct playlist_info
* playlist
, char *buf
,
3294 playlist
= ¤t_playlist
;
3296 strlcpy(buf
, playlist
->filename
+playlist
->dirlen
, buf_size
);
3301 /* Remove extension */
3302 sep
= strrchr(buf
, '.');
3309 /* returns the playlist filename */
3310 char *playlist_get_name(const struct playlist_info
* playlist
, char *buf
,
3314 playlist
= ¤t_playlist
;
3316 strlcpy(buf
, playlist
->filename
, buf_size
);
3324 /* Fills info structure with information about track at specified index.
3325 Returns 0 on success and -1 on failure */
3326 int playlist_get_track_info(struct playlist_info
* playlist
, int index
,
3327 struct playlist_track_info
* info
)
3333 playlist
= ¤t_playlist
;
3335 if (index
< 0 || index
>= playlist
->amount
)
3338 control_file
= playlist
->indices
[index
] & PLAYLIST_INSERT_TYPE_MASK
;
3339 seek
= playlist
->indices
[index
] & PLAYLIST_SEEK_MASK
;
3341 if (get_filename(playlist
, index
, seek
, control_file
, info
->filename
,
3342 sizeof(info
->filename
)) < 0)
3349 if (playlist
->indices
[index
] & PLAYLIST_QUEUE_MASK
)
3350 info
->attr
|= PLAYLIST_ATTR_QUEUED
;
3352 info
->attr
|= PLAYLIST_ATTR_INSERTED
;
3356 if (playlist
->indices
[index
] & PLAYLIST_SKIPPED
)
3357 info
->attr
|= PLAYLIST_ATTR_SKIPPED
;
3359 info
->index
= index
;
3360 info
->display_index
= rotate_index(playlist
, index
) + 1;
3365 /* save the current dynamic playlist to specified file */
3366 int playlist_save(struct playlist_info
* playlist
, char *filename
)
3371 char path
[MAX_PATH
+1];
3372 char tmp_buf
[MAX_PATH
+1];
3374 bool overwrite_current
= false;
3375 int* index_buf
= NULL
;
3378 playlist
= ¤t_playlist
;
3380 if (playlist
->amount
<= 0)
3383 /* use current working directory as base for pathname */
3384 if (format_track_path(path
, filename
, sizeof(tmp_buf
),
3385 strlen(filename
)+1, getcwd(NULL
, -1)) < 0)
3388 if (!strncmp(playlist
->filename
, path
, strlen(path
)))
3390 /* Attempting to overwrite current playlist file.*/
3392 if (playlist
->buffer_size
< (int)(playlist
->amount
* sizeof(int)))
3394 /* not enough buffer space to store updated indices */
3395 splash(HZ
*2, ID2P(LANG_PLAYLIST_ACCESS_ERROR
));
3399 /* in_ram buffer is unused for m3u files so we'll use for storing
3401 index_buf
= (int*)playlist
->buffer
;
3403 /* use temporary pathname */
3404 snprintf(path
, sizeof(path
), "%s_temp", playlist
->filename
);
3405 overwrite_current
= true;
3410 fd
= open_utf8(path
, O_CREAT
|O_WRONLY
|O_TRUNC
);
3414 /* some applications require a BOM to read the file properly */
3415 fd
= open(path
, O_CREAT
|O_WRONLY
|O_TRUNC
, 0666);
3419 splash(HZ
*2, ID2P(LANG_PLAYLIST_ACCESS_ERROR
));
3423 display_playlist_count(count
, ID2P(LANG_PLAYLIST_SAVE_COUNT
), false);
3427 index
= playlist
->first_index
;
3428 for (i
=0; i
<playlist
->amount
; i
++)
3435 if (action_userabort(TIMEOUT_NOBLOCK
))
3441 control_file
= playlist
->indices
[index
] & PLAYLIST_INSERT_TYPE_MASK
;
3442 queue
= playlist
->indices
[index
] & PLAYLIST_QUEUE_MASK
;
3443 seek
= playlist
->indices
[index
] & PLAYLIST_SEEK_MASK
;
3445 /* Don't save queued files */
3448 if (get_filename(playlist
, index
, seek
, control_file
, tmp_buf
,
3455 if (overwrite_current
)
3456 index_buf
[count
] = lseek(fd
, 0, SEEK_CUR
);
3458 if (fdprintf(fd
, "%s\n", tmp_buf
) < 0)
3460 splash(HZ
*2, ID2P(LANG_PLAYLIST_ACCESS_ERROR
));
3467 if ((count
% PLAYLIST_DISPLAY_COUNT
) == 0)
3468 display_playlist_count(count
, ID2P(LANG_PLAYLIST_SAVE_COUNT
),
3474 index
= (index
+1)%playlist
->amount
;
3477 display_playlist_count(count
, ID2P(LANG_PLAYLIST_SAVE_COUNT
), true);
3481 if (overwrite_current
&& result
>= 0)
3485 mutex_lock(playlist
->control_mutex
);
3487 /* Replace the current playlist with the new one and update indices */
3488 close(playlist
->fd
);
3489 if (remove(playlist
->filename
) >= 0)
3491 if (rename(path
, playlist
->filename
) >= 0)
3493 playlist
->fd
= open_utf8(playlist
->filename
, O_RDONLY
);
3494 if (playlist
->fd
>= 0)
3496 index
= playlist
->first_index
;
3497 for (i
=0, count
=0; i
<playlist
->amount
; i
++)
3499 if (!(playlist
->indices
[index
] & PLAYLIST_QUEUE_MASK
))
3501 playlist
->indices
[index
] = index_buf
[count
];
3504 index
= (index
+1)%playlist
->amount
;
3507 /* we need to recreate control because inserted tracks are
3508 now part of the playlist and shuffle has been
3510 result
= recreate_control(playlist
);
3515 mutex_unlock(playlist
->control_mutex
);
3525 * Search specified directory for tracks and notify via callback. May be
3526 * called recursively.
3528 int playlist_directory_tracksearch(const char* dirname
, bool recurse
,
3529 int (*callback
)(char*, void*),
3532 char buf
[MAX_PATH
+1];
3536 struct entry
*files
;
3537 struct tree_context
* tc
= tree_get_context();
3538 int old_dirfilter
= *(tc
->dirfilter
);
3543 /* use the tree browser dircache to load files */
3544 *(tc
->dirfilter
) = SHOW_ALL
;
3546 if (ft_load(tc
, dirname
) < 0)
3548 splash(HZ
*2, ID2P(LANG_PLAYLIST_DIRECTORY_ACCESS_ERROR
));
3549 *(tc
->dirfilter
) = old_dirfilter
;
3553 files
= (struct entry
*) tc
->dircache
;
3554 num_files
= tc
->filesindir
;
3556 /* we've overwritten the dircache so tree browser will need to be
3560 for (i
=0; i
<num_files
; i
++)
3563 if (action_userabort(TIMEOUT_NOBLOCK
))
3569 if (files
[i
].attr
& ATTR_DIRECTORY
)
3573 /* recursively add directories */
3574 snprintf(buf
, sizeof(buf
), "%s/%s",
3575 dirname
[1]? dirname
: "", files
[i
].name
);
3576 result
= playlist_directory_tracksearch(buf
, recurse
,
3581 /* we now need to reload our current directory */
3582 if(ft_load(tc
, dirname
) < 0)
3588 files
= (struct entry
*) tc
->dircache
;
3589 num_files
= tc
->filesindir
;
3599 else if ((files
[i
].attr
& FILE_ATTR_MASK
) == FILE_ATTR_AUDIO
)
3601 snprintf(buf
, sizeof(buf
), "%s/%s",
3602 dirname
[1]? dirname
: "", files
[i
].name
);
3604 if (callback(buf
, context
) != 0)
3610 /* let the other threads work */
3615 /* restore dirfilter */
3616 *(tc
->dirfilter
) = old_dirfilter
;