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.
75 #include "ata_idle_notify.h"
86 #include "applimits.h"
95 #include "filetypes.h"
96 #ifdef HAVE_LCD_BITMAP
103 #include "rbunicode.h"
104 #include "root_menu.h"
106 #define PLAYLIST_CONTROL_FILE ROCKBOX_DIR "/.playlist_control"
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 #define PLAYLIST_DISPLAY_COUNT 10
141 struct directory_search_context
{
142 struct playlist_info
* playlist
;
148 static struct playlist_info current_playlist
;
149 static char now_playing
[MAX_PATH
+1];
151 static void empty_playlist(struct playlist_info
* playlist
, bool resume
);
152 static void new_playlist(struct playlist_info
* playlist
, const char *dir
,
154 static void create_control(struct playlist_info
* playlist
);
155 static int check_control(struct playlist_info
* playlist
);
156 static int recreate_control(struct playlist_info
* playlist
);
157 static void update_playlist_filename(struct playlist_info
* playlist
,
158 const char *dir
, const char *file
);
159 static int add_indices_to_playlist(struct playlist_info
* playlist
,
160 char* buffer
, size_t buflen
);
161 static int add_track_to_playlist(struct playlist_info
* playlist
,
162 const char *filename
, int position
,
163 bool queue
, int seek_pos
);
164 static int directory_search_callback(char* filename
, void* context
);
165 static int remove_track_from_playlist(struct playlist_info
* playlist
,
166 int position
, bool write
);
167 static int randomise_playlist(struct playlist_info
* playlist
,
168 unsigned int seed
, bool start_current
,
170 static int sort_playlist(struct playlist_info
* playlist
, bool start_current
,
172 static int get_next_index(const struct playlist_info
* playlist
, int steps
,
174 static void find_and_set_playlist_index(struct playlist_info
* playlist
,
176 static int compare(const void* p1
, const void* p2
);
177 static int get_filename(struct playlist_info
* playlist
, int index
, int seek
,
178 bool control_file
, char *buf
, int buf_length
);
179 static int get_next_directory(char *dir
);
180 static int get_next_dir(char *dir
, bool is_forward
, bool recursion
);
181 static int get_previous_directory(char *dir
);
182 static int check_subdir_for_music(char *dir
, char *subdir
);
183 static int format_track_path(char *dest
, char *src
, int buf_length
, int max
,
185 static void display_playlist_count(int count
, const unsigned char *fmt
,
187 static void display_buffer_full(void);
188 static int flush_cached_control(struct playlist_info
* playlist
);
189 static int update_control(struct playlist_info
* playlist
,
190 enum playlist_command command
, int i1
, int i2
,
191 const char* s1
, const char* s2
, void* data
);
192 static void sync_control(struct playlist_info
* playlist
, bool force
);
193 static int rotate_index(const struct playlist_info
* playlist
, int index
);
196 #define PLAYLIST_LOAD_POINTERS 1
198 static struct event_queue playlist_queue
;
199 static long playlist_stack
[(DEFAULT_STACK_SIZE
+ 0x800)/sizeof(long)];
200 static const char playlist_thread_name
[] = "playlist cachectrl";
203 #define BOM "\xef\xbb\xbf"
206 /* Check if the filename suggests M3U or M3U8 format. */
207 static bool is_m3u8(const char* filename
)
209 int len
= strlen(filename
);
211 /* Default to M3U8 unless explicitly told otherwise. */
212 return !(len
> 4 && strcasecmp(&filename
[len
- 4], ".m3u") == 0);
215 /* Check if a strings starts with an UTF-8 byte-order mark. */
216 static bool is_utf8_bom(const char* str
, int len
)
218 return len
>= BOM_SIZE
&& memcmp(str
, BOM
, BOM_SIZE
) == 0;
221 /* Convert a filename in an M3U playlist to UTF-8.
223 * buf - the filename to convert; can contain more than one line from the
225 * buf_len - amount of buf that is used.
226 * buf_max - total size of buf.
227 * temp - temporary conversion buffer, at least buf_max bytes.
229 * Returns the length of the converted filename.
231 static int convert_m3u(char* buf
, int buf_len
, int buf_max
, char* temp
)
237 while ((buf
[i
] != '\n') && (buf
[i
] != '\r') && (i
< buf_len
))
242 /* Work back killing white space. */
243 while ((i
> 0) && isspace(buf
[i
- 1]))
251 /* Convert char by char, so as to not overflow temp (iso_decode should
252 * preferably handle this). No more than 4 bytes should be generated for
255 for (i
= 0; i
< buf_len
&& dest
< (temp
+ buf_max
- 4); i
++)
257 dest
= iso_decode(&buf
[i
], dest
, -1, 1);
266 * remove any files and indices associated with the playlist
268 static void empty_playlist(struct playlist_info
* playlist
, bool resume
)
270 playlist
->filename
[0] = '\0';
271 playlist
->utf8
= true;
273 if(playlist
->fd
>= 0)
274 /* If there is an already open playlist, close it. */
278 if(playlist
->control_fd
>= 0)
279 close(playlist
->control_fd
);
280 playlist
->control_fd
= -1;
281 playlist
->control_created
= false;
283 playlist
->in_ram
= false;
285 if (playlist
->buffer
)
286 playlist
->buffer
[0] = 0;
288 playlist
->buffer_end_pos
= 0;
291 playlist
->first_index
= 0;
292 playlist
->amount
= 0;
293 playlist
->last_insert_pos
= -1;
295 playlist
->shuffle_modified
= false;
296 playlist
->deleted
= false;
297 playlist
->num_inserted_tracks
= 0;
298 playlist
->started
= false;
300 playlist
->num_cached
= 0;
301 playlist
->pending_control_sync
= false;
303 if (!resume
&& playlist
->current
)
305 /* start with fresh playlist control file when starting new
307 create_control(playlist
);
309 /* Reset resume settings */
310 global_status
.resume_first_index
= 0;
311 global_status
.resume_seed
= -1;
316 * Initialize a new playlist for viewing/editing/playing. dir is the
317 * directory where the playlist is located and file is the filename.
319 static void new_playlist(struct playlist_info
* playlist
, const char *dir
,
322 const char *fileused
= file
;
323 const char *dirused
= dir
;
324 empty_playlist(playlist
, false);
330 if (dirused
&& playlist
->current
) /* !current cannot be in_ram */
331 playlist
->in_ram
= true;
333 dirused
= ""; /* empty playlist */
336 update_playlist_filename(playlist
, dirused
, fileused
);
338 if (playlist
->control_fd
>= 0)
340 update_control(playlist
, PLAYLIST_COMMAND_PLAYLIST
,
341 PLAYLIST_CONTROL_FILE_VERSION
, -1, dirused
, fileused
, NULL
);
342 sync_control(playlist
, false);
347 * create control file for playlist
349 static void create_control(struct playlist_info
* playlist
)
351 playlist
->control_fd
= open(playlist
->control_filename
,
352 O_CREAT
|O_RDWR
|O_TRUNC
);
353 if (playlist
->control_fd
< 0)
355 if (check_rockboxdir())
357 cond_talk_ids_fq(LANG_PLAYLIST_CONTROL_ACCESS_ERROR
);
358 gui_syncsplash(HZ
*2, (unsigned char *)"%s (%d)",
359 str(LANG_PLAYLIST_CONTROL_ACCESS_ERROR
),
360 playlist
->control_fd
);
362 playlist
->control_created
= false;
366 playlist
->control_created
= true;
371 * validate the control file. This may include creating/initializing it if
374 static int check_control(struct playlist_info
* playlist
)
376 if (!playlist
->control_created
)
378 create_control(playlist
);
380 if (playlist
->control_fd
>= 0)
382 char* dir
= playlist
->filename
;
383 char* file
= playlist
->filename
+playlist
->dirlen
;
384 char c
= playlist
->filename
[playlist
->dirlen
-1];
386 playlist
->filename
[playlist
->dirlen
-1] = '\0';
388 update_control(playlist
, PLAYLIST_COMMAND_PLAYLIST
,
389 PLAYLIST_CONTROL_FILE_VERSION
, -1, dir
, file
, NULL
);
390 sync_control(playlist
, false);
391 playlist
->filename
[playlist
->dirlen
-1] = c
;
395 if (playlist
->control_fd
< 0)
402 * recreate the control file based on current playlist entries
404 static int recreate_control(struct playlist_info
* playlist
)
406 char temp_file
[MAX_PATH
+1];
411 if(playlist
->control_fd
>= 0)
413 char* dir
= playlist
->filename
;
414 char* file
= playlist
->filename
+playlist
->dirlen
;
415 char c
= playlist
->filename
[playlist
->dirlen
-1];
417 close(playlist
->control_fd
);
419 snprintf(temp_file
, sizeof(temp_file
), "%s_temp",
420 playlist
->control_filename
);
422 if (rename(playlist
->control_filename
, temp_file
) < 0)
425 temp_fd
= open(temp_file
, O_RDONLY
);
429 playlist
->control_fd
= open(playlist
->control_filename
,
430 O_CREAT
|O_RDWR
|O_TRUNC
);
431 if (playlist
->control_fd
< 0)
434 playlist
->filename
[playlist
->dirlen
-1] = '\0';
436 /* cannot call update_control() because of mutex */
437 result
= fdprintf(playlist
->control_fd
, "P:%d:%s:%s\n",
438 PLAYLIST_CONTROL_FILE_VERSION
, dir
, file
);
440 playlist
->filename
[playlist
->dirlen
-1] = c
;
450 playlist
->shuffle_modified
= false;
451 playlist
->deleted
= false;
452 playlist
->num_inserted_tracks
= 0;
454 if (playlist
->current
)
456 global_status
.resume_seed
= -1;
460 for (i
=0; i
<playlist
->amount
; i
++)
462 if (playlist
->indices
[i
] & PLAYLIST_INSERT_TYPE_MASK
)
464 bool queue
= playlist
->indices
[i
] & PLAYLIST_QUEUE_MASK
;
465 char inserted_file
[MAX_PATH
+1];
467 lseek(temp_fd
, playlist
->indices
[i
] & PLAYLIST_SEEK_MASK
,
469 read_line(temp_fd
, inserted_file
, sizeof(inserted_file
));
471 result
= fdprintf(playlist
->control_fd
, "%c:%d:%d:",
472 queue
?'Q':'A', i
, playlist
->last_insert_pos
);
475 /* save the position in file where name is written */
476 int seek_pos
= lseek(playlist
->control_fd
, 0, SEEK_CUR
);
478 result
= fdprintf(playlist
->control_fd
, "%s\n",
481 playlist
->indices
[i
] =
482 (playlist
->indices
[i
] & ~PLAYLIST_SEEK_MASK
) | seek_pos
;
488 playlist
->num_inserted_tracks
++;
494 fsync(playlist
->control_fd
);
503 * store directory and name of playlist file
505 static void update_playlist_filename(struct playlist_info
* playlist
,
506 const char *dir
, const char *file
)
509 int dirlen
= strlen(dir
);
511 playlist
->utf8
= is_m3u8(file
);
513 /* If the dir does not end in trailing slash, we use a separator.
514 Otherwise we don't. */
515 if('/' != dir
[dirlen
-1])
521 playlist
->dirlen
= dirlen
;
523 snprintf(playlist
->filename
, sizeof(playlist
->filename
),
524 "%s%s%s", dir
, sep
, file
);
528 * calculate track offsets within a playlist file
530 static int add_indices_to_playlist(struct playlist_info
* playlist
,
531 char* buffer
, size_t buflen
)
535 unsigned int count
= 0;
540 if(-1 == playlist
->fd
)
541 playlist
->fd
= open(playlist
->filename
, O_RDONLY
);
543 return -1; /* failure */
545 gui_syncsplash(0, ID2P(LANG_WAIT
));
549 /* use mp3 buffer for maximum load speed */
551 #if CONFIG_CODEC != SWCODEC
552 talk_buffer_steal(); /* we use the mp3 buffer, need to tell */
553 buflen
= (audiobufend
- audiobuf
);
554 buffer
= (char *)audiobuf
;
556 buffer
= (char *)audio_get_buffer(false, &buflen
);
564 nread
= read(playlist
->fd
, buffer
, buflen
);
565 /* Terminate on EOF */
569 p
= (unsigned char *)buffer
;
571 /* utf8 BOM at beginning of file? */
572 if(i
== 0 && is_utf8_bom(p
, nread
)) {
576 playlist
->utf8
= true; /* Override any earlier indication. */
579 for(count
=0; count
< nread
; count
++,p
++) {
581 /* Are we on a new line? */
582 if((*p
== '\n') || (*p
== '\r'))
592 if ( playlist
->amount
>= playlist
->max_playlist_size
) {
593 display_buffer_full();
598 /* Store a new entry */
599 playlist
->indices
[ playlist
->amount
] = i
+count
;
601 if (playlist
->filenames
)
602 playlist
->filenames
[ playlist
->amount
] = NULL
;
614 queue_post(&playlist_queue
, PLAYLIST_LOAD_POINTERS
, 0);
621 * Utility function to create a new playlist, fill it with the next or
622 * previous directory, shuffle it if needed, and start playback.
623 * If play_last is true and direction zero or negative, start playing
624 * the last file in the directory, otherwise start playing the first.
626 static int create_and_play_dir(int direction
, bool play_last
)
628 char dir
[MAX_PATH
+ 1];
633 res
= get_next_directory(dir
);
635 res
= get_previous_directory(dir
);
639 if (playlist_create(dir
, NULL
) != -1)
641 ft_build_playlist(tree_get_context(), 0);
643 if (global_settings
.playlist_shuffle
)
644 playlist_shuffle(current_tick
, -1);
646 if (play_last
&& direction
<= 0)
647 index
= current_playlist
.amount
- 1;
651 #if (CONFIG_CODEC != SWCODEC)
652 playlist_start(index
, 0);
656 /* we've overwritten the dircache when getting the next/previous dir,
657 so the tree browser context will need to be reloaded */
665 * Removes all tracks, from the playlist, leaving the presently playing
668 int playlist_remove_all_tracks(struct playlist_info
*playlist
)
672 if (playlist
== NULL
)
673 playlist
= ¤t_playlist
;
675 while (playlist
->index
> 0)
676 if ((result
= remove_track_from_playlist(playlist
, 0, true)) < 0)
679 while (playlist
->amount
> 1)
680 if ((result
= remove_track_from_playlist(playlist
, 1, true)) < 0)
683 if (playlist
->amount
== 1) {
684 playlist
->indices
[0] |= PLAYLIST_QUEUED
;
692 * Add track to playlist at specified position. There are five special
693 * positions that can be specified:
694 * PLAYLIST_PREPEND - Add track at beginning of playlist
695 * PLAYLIST_INSERT - Add track after current song. NOTE: If
696 * there are already inserted tracks then track
697 * is added to the end of the insertion list
698 * PLAYLIST_INSERT_FIRST - Add track immediately after current song, no
699 * matter what other tracks have been inserted
700 * PLAYLIST_INSERT_LAST - Add track to end of playlist
701 * PLAYLIST_INSERT_SHUFFLED - Add track at some random point between the
702 * current playing track and end of playlist
703 * PLAYLIST_REPLACE - Erase current playlist, Cue the current track
704 * and inster this track at the end.
706 static int add_track_to_playlist(struct playlist_info
* playlist
,
707 const char *filename
, int position
,
708 bool queue
, int seek_pos
)
710 int insert_position
, orig_position
;
711 unsigned long flags
= PLAYLIST_INSERT_TYPE_INSERT
;
714 insert_position
= orig_position
= position
;
716 if (playlist
->amount
>= playlist
->max_playlist_size
)
718 display_buffer_full();
724 case PLAYLIST_PREPEND
:
725 position
= insert_position
= playlist
->first_index
;
727 case PLAYLIST_INSERT
:
728 /* if there are already inserted tracks then add track to end of
729 insertion list else add after current playing track */
730 if (playlist
->last_insert_pos
>= 0 &&
731 playlist
->last_insert_pos
< playlist
->amount
&&
732 (playlist
->indices
[playlist
->last_insert_pos
]&
733 PLAYLIST_INSERT_TYPE_MASK
) == PLAYLIST_INSERT_TYPE_INSERT
)
734 position
= insert_position
= playlist
->last_insert_pos
+1;
735 else if (playlist
->amount
> 0)
736 position
= insert_position
= playlist
->index
+ 1;
738 position
= insert_position
= 0;
740 if (playlist
->started
)
741 playlist
->last_insert_pos
= position
;
743 case PLAYLIST_INSERT_FIRST
:
744 if (playlist
->amount
> 0)
745 position
= insert_position
= playlist
->index
+ 1;
747 position
= insert_position
= 0;
749 if (playlist
->last_insert_pos
< 0 && playlist
->started
)
750 playlist
->last_insert_pos
= position
;
752 case PLAYLIST_INSERT_LAST
:
753 if (playlist
->first_index
> 0)
754 position
= insert_position
= playlist
->first_index
;
756 position
= insert_position
= playlist
->amount
;
758 case PLAYLIST_INSERT_SHUFFLED
:
760 if (playlist
->started
)
763 int n
= playlist
->amount
-
764 rotate_index(playlist
, playlist
->index
);
771 position
= playlist
->index
+ offset
+ 1;
772 if (position
>= playlist
->amount
)
773 position
-= playlist
->amount
;
775 insert_position
= position
;
778 position
= insert_position
= (rand() % (playlist
->amount
+1));
781 case PLAYLIST_REPLACE
:
782 if (playlist_remove_all_tracks(playlist
) < 0)
785 position
= insert_position
= playlist
->index
+ 1;
790 flags
|= PLAYLIST_QUEUED
;
792 /* shift indices so that track can be added */
793 for (i
=playlist
->amount
; i
>insert_position
; i
--)
795 playlist
->indices
[i
] = playlist
->indices
[i
-1];
797 if (playlist
->filenames
)
798 playlist
->filenames
[i
] = playlist
->filenames
[i
-1];
802 /* update stored indices if needed */
803 if (playlist
->amount
> 0 && insert_position
<= playlist
->index
&&
807 if (playlist
->amount
> 0 && insert_position
<= playlist
->first_index
&&
808 orig_position
!= PLAYLIST_PREPEND
&& playlist
->started
)
810 playlist
->first_index
++;
812 if (seek_pos
< 0 && playlist
->current
)
814 global_status
.resume_first_index
= playlist
->first_index
;
819 if (insert_position
< playlist
->last_insert_pos
||
820 (insert_position
== playlist
->last_insert_pos
&& position
< 0))
821 playlist
->last_insert_pos
++;
823 if (seek_pos
< 0 && playlist
->control_fd
>= 0)
825 int result
= update_control(playlist
,
826 (queue
?PLAYLIST_COMMAND_QUEUE
:PLAYLIST_COMMAND_ADD
), position
,
827 playlist
->last_insert_pos
, filename
, NULL
, &seek_pos
);
833 playlist
->indices
[insert_position
] = flags
| seek_pos
;
836 if (playlist
->filenames
)
837 playlist
->filenames
[insert_position
] = NULL
;
841 playlist
->num_inserted_tracks
++;
843 return insert_position
;
847 * Callback for playlist_directory_tracksearch to insert track into
850 static int directory_search_callback(char* filename
, void* context
)
852 struct directory_search_context
* c
=
853 (struct directory_search_context
*) context
;
856 insert_pos
= add_track_to_playlist(c
->playlist
, filename
, c
->position
,
864 /* Make sure tracks are inserted in correct order if user requests
866 if (c
->position
== PLAYLIST_INSERT_FIRST
|| c
->position
>= 0)
867 c
->position
= insert_pos
+ 1;
869 if (((c
->count
)%PLAYLIST_DISPLAY_COUNT
) == 0)
871 unsigned char* count_str
;
874 count_str
= ID2P(LANG_PLAYLIST_QUEUE_COUNT
);
876 count_str
= ID2P(LANG_PLAYLIST_INSERT_COUNT
);
878 display_playlist_count(c
->count
, count_str
, false);
880 if ((c
->count
) == PLAYLIST_DISPLAY_COUNT
&&
881 (audio_status() & AUDIO_STATUS_PLAY
) &&
882 c
->playlist
->started
)
883 audio_flush_and_reload_tracks();
890 * remove track at specified position
892 static int remove_track_from_playlist(struct playlist_info
* playlist
,
893 int position
, bool write
)
898 if (playlist
->amount
<= 0)
901 inserted
= playlist
->indices
[position
] & PLAYLIST_INSERT_TYPE_MASK
;
903 /* shift indices now that track has been removed */
904 for (i
=position
; i
<playlist
->amount
; i
++)
906 playlist
->indices
[i
] = playlist
->indices
[i
+1];
908 if (playlist
->filenames
)
909 playlist
->filenames
[i
] = playlist
->filenames
[i
+1];
916 playlist
->num_inserted_tracks
--;
918 playlist
->deleted
= true;
920 /* update stored indices if needed */
921 if (position
< playlist
->index
)
924 if (position
< playlist
->first_index
)
926 playlist
->first_index
--;
930 global_status
.resume_first_index
= playlist
->first_index
;
935 if (position
<= playlist
->last_insert_pos
)
936 playlist
->last_insert_pos
--;
938 if (write
&& playlist
->control_fd
>= 0)
940 int result
= update_control(playlist
, PLAYLIST_COMMAND_DELETE
,
941 position
, -1, NULL
, NULL
, NULL
);
946 sync_control(playlist
, false);
953 * randomly rearrange the array of indices for the playlist. If start_current
954 * is true then update the index to the new index of the current playing track
956 static int randomise_playlist(struct playlist_info
* playlist
,
957 unsigned int seed
, bool start_current
,
963 unsigned int current
= playlist
->indices
[playlist
->index
];
965 /* seed 0 is used to identify sorted playlist for resume purposes */
969 /* seed with the given seed */
972 /* randomise entire indices list */
973 for(count
= playlist
->amount
- 1; count
>= 0; count
--)
975 /* the rand is from 0 to RAND_MAX, so adjust to our value range */
976 candidate
= rand() % (count
+ 1);
978 /* now swap the values at the 'count' and 'candidate' positions */
979 store
= playlist
->indices
[candidate
];
980 playlist
->indices
[candidate
] = playlist
->indices
[count
];
981 playlist
->indices
[count
] = store
;
983 if (playlist
->filenames
)
985 store
= (long)playlist
->filenames
[candidate
];
986 playlist
->filenames
[candidate
] = playlist
->filenames
[count
];
987 playlist
->filenames
[count
] = (struct dircache_entry
*)store
;
993 find_and_set_playlist_index(playlist
, current
);
995 /* indices have been moved so last insert position is no longer valid */
996 playlist
->last_insert_pos
= -1;
998 playlist
->seed
= seed
;
999 if (playlist
->num_inserted_tracks
> 0 || playlist
->deleted
)
1000 playlist
->shuffle_modified
= true;
1004 update_control(playlist
, PLAYLIST_COMMAND_SHUFFLE
, seed
,
1005 playlist
->first_index
, NULL
, NULL
, NULL
);
1006 global_status
.resume_seed
= seed
;
1014 * Sort the array of indices for the playlist. If start_current is true then
1015 * set the index to the new index of the current song.
1017 static int sort_playlist(struct playlist_info
* playlist
, bool start_current
,
1020 unsigned int current
= playlist
->indices
[playlist
->index
];
1022 if (playlist
->amount
> 0)
1023 qsort(playlist
->indices
, playlist
->amount
,
1024 sizeof(playlist
->indices
[0]), compare
);
1026 #ifdef HAVE_DIRCACHE
1027 /** We need to re-check the song names from disk because qsort can't
1028 * sort two arrays at once :/
1029 * FIXME: Please implement a better way to do this. */
1030 memset(playlist
->filenames
, 0, playlist
->max_playlist_size
* sizeof(int));
1031 queue_post(&playlist_queue
, PLAYLIST_LOAD_POINTERS
, 0);
1035 find_and_set_playlist_index(playlist
, current
);
1037 /* indices have been moved so last insert position is no longer valid */
1038 playlist
->last_insert_pos
= -1;
1040 if (!playlist
->num_inserted_tracks
&& !playlist
->deleted
)
1041 playlist
->shuffle_modified
= false;
1042 if (write
&& playlist
->control_fd
>= 0)
1044 update_control(playlist
, PLAYLIST_COMMAND_UNSHUFFLE
,
1045 playlist
->first_index
, -1, NULL
, NULL
, NULL
);
1046 global_status
.resume_seed
= 0;
1053 /* Calculate how many steps we have to really step when skipping entries
1056 static int calculate_step_count(const struct playlist_info
*playlist
, int steps
)
1058 int i
, count
, direction
;
1060 int stepped_count
= 0;
1073 index
= playlist
->index
;
1076 /* Boundary check */
1078 index
+= playlist
->amount
;
1079 if (index
>= playlist
->amount
)
1080 index
-= playlist
->amount
;
1082 /* Check if we found a bad entry. */
1083 if (playlist
->indices
[index
] & PLAYLIST_SKIPPED
)
1086 /* Are all entries bad? */
1087 if (stepped_count
++ > playlist
->amount
)
1094 } while (i
<= count
);
1099 /* Marks the index of the track to be skipped that is "steps" away from
1100 * current playing track.
1102 void playlist_skip_entry(struct playlist_info
*playlist
, int steps
)
1106 if (playlist
== NULL
)
1107 playlist
= ¤t_playlist
;
1109 /* need to account for already skipped tracks */
1110 steps
= calculate_step_count(playlist
, steps
);
1112 index
= playlist
->index
+ steps
;
1114 index
+= playlist
->amount
;
1115 else if (index
>= playlist
->amount
)
1116 index
-= playlist
->amount
;
1118 playlist
->indices
[index
] |= PLAYLIST_SKIPPED
;
1122 * returns the index of the track that is "steps" away from current playing
1125 static int get_next_index(const struct playlist_info
* playlist
, int steps
,
1128 int current_index
= playlist
->index
;
1129 int next_index
= -1;
1131 if (playlist
->amount
<= 0)
1134 if (repeat_mode
== -1)
1135 repeat_mode
= global_settings
.repeat_mode
;
1137 if (repeat_mode
== REPEAT_SHUFFLE
&& playlist
->amount
<= 1)
1138 repeat_mode
= REPEAT_ALL
;
1140 steps
= calculate_step_count(playlist
, steps
);
1141 switch (repeat_mode
)
1143 case REPEAT_SHUFFLE
:
1144 /* Treat repeat shuffle just like repeat off. At end of playlist,
1145 play will be resumed in playlist_next() */
1148 current_index
= rotate_index(playlist
, current_index
);
1149 next_index
= current_index
+steps
;
1150 if ((next_index
< 0) || (next_index
>= playlist
->amount
))
1153 next_index
= (next_index
+playlist
->first_index
) %
1160 #ifdef AB_REPEAT_ENABLE
1163 next_index
= current_index
;
1169 next_index
= (current_index
+steps
) % playlist
->amount
;
1170 while (next_index
< 0)
1171 next_index
+= playlist
->amount
;
1173 if (steps
>= playlist
->amount
)
1180 /* second time around so skip the queued files */
1181 for (i
=0; i
<playlist
->amount
; i
++)
1183 if (playlist
->indices
[index
] & PLAYLIST_QUEUE_MASK
)
1184 index
= (index
+1) % playlist
->amount
;
1196 /* No luck if the whole playlist was bad. */
1197 if (playlist
->indices
[next_index
] & PLAYLIST_SKIPPED
)
1204 * Search for the seek track and set appropriate indices. Used after shuffle
1205 * to make sure the current index is still pointing to correct track.
1207 static void find_and_set_playlist_index(struct playlist_info
* playlist
,
1212 /* Set the index to the current song */
1213 for (i
=0; i
<playlist
->amount
; i
++)
1215 if (playlist
->indices
[i
] == seek
)
1217 playlist
->index
= playlist
->first_index
= i
;
1219 if (playlist
->current
)
1221 global_status
.resume_first_index
= i
;
1231 * used to sort track indices. Sort order is as follows:
1232 * 1. Prepended tracks (in prepend order)
1233 * 2. Playlist/directory tracks (in playlist order)
1234 * 3. Inserted/Appended tracks (in insert order)
1236 static int compare(const void* p1
, const void* p2
)
1238 unsigned long* e1
= (unsigned long*) p1
;
1239 unsigned long* e2
= (unsigned long*) p2
;
1240 unsigned long flags1
= *e1
& PLAYLIST_INSERT_TYPE_MASK
;
1241 unsigned long flags2
= *e2
& PLAYLIST_INSERT_TYPE_MASK
;
1243 if (flags1
== flags2
)
1244 return (*e1
& PLAYLIST_SEEK_MASK
) - (*e2
& PLAYLIST_SEEK_MASK
);
1245 else if (flags1
== PLAYLIST_INSERT_TYPE_PREPEND
||
1246 flags2
== PLAYLIST_INSERT_TYPE_APPEND
)
1248 else if (flags1
== PLAYLIST_INSERT_TYPE_APPEND
||
1249 flags2
== PLAYLIST_INSERT_TYPE_PREPEND
)
1251 else if (flags1
&& flags2
)
1252 return (*e1
& PLAYLIST_SEEK_MASK
) - (*e2
& PLAYLIST_SEEK_MASK
);
1257 #ifdef HAVE_DIRCACHE
1259 * Thread to update filename pointers to dircache on background
1260 * without affecting playlist load up performance. This thread also flushes
1261 * any pending control commands when the disk spins up.
1263 static bool playlist_flush_callback(void)
1265 struct playlist_info
*playlist
;
1266 playlist
= ¤t_playlist
;
1267 if (playlist
->control_fd
>= 0)
1269 if (playlist
->num_cached
> 0)
1271 mutex_lock(&playlist
->control_mutex
);
1272 flush_cached_control(playlist
);
1273 mutex_unlock(&playlist
->control_mutex
);
1275 sync_control(playlist
, true);
1280 static void playlist_thread(void)
1282 struct queue_event ev
;
1283 bool dirty_pointers
= false;
1284 static char tmp
[MAX_PATH
+1];
1286 struct playlist_info
*playlist
;
1293 #ifndef HAVE_FLASH_STORAGE
1294 if (global_settings
.disk_spindown
> 1 &&
1295 global_settings
.disk_spindown
<= 5)
1296 sleep_time
= global_settings
.disk_spindown
- 1;
1301 queue_wait_w_tmo(&playlist_queue
, &ev
, HZ
*sleep_time
);
1305 case PLAYLIST_LOAD_POINTERS
:
1306 dirty_pointers
= true;
1309 /* Start the background scanning after either the disk spindown
1310 timeout or 5s, whichever is less */
1312 playlist
= ¤t_playlist
;
1313 if (playlist
->control_fd
>= 0)
1315 if (playlist
->num_cached
> 0)
1316 register_ata_idle_func(playlist_flush_callback
);
1319 if (!dirty_pointers
)
1322 if (!dircache_is_enabled() || !playlist
->filenames
1323 || playlist
->amount
<= 0)
1326 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
1329 for (index
= 0; index
< playlist
->amount
1330 && queue_empty(&playlist_queue
); index
++)
1332 /* Process only pointers that are not already loaded. */
1333 if (playlist
->filenames
[index
])
1336 control_file
= playlist
->indices
[index
] & PLAYLIST_INSERT_TYPE_MASK
;
1337 seek
= playlist
->indices
[index
] & PLAYLIST_SEEK_MASK
;
1339 /* Load the filename from playlist file. */
1340 if (get_filename(playlist
, index
, seek
, control_file
, tmp
,
1344 /* Set the dircache entry pointer. */
1345 playlist
->filenames
[index
] = dircache_get_entry_ptr(tmp
);
1347 /* And be on background so user doesn't notice any delays. */
1351 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
1354 dirty_pointers
= false;
1358 case SYS_USB_CONNECTED
:
1359 usb_acknowledge(SYS_USB_CONNECTED_ACK
);
1360 usb_wait_for_disconnect(&playlist_queue
);
1369 * gets pathname for track at seek index
1371 static int get_filename(struct playlist_info
* playlist
, int index
, int seek
,
1372 bool control_file
, char *buf
, int buf_length
)
1376 char tmp_buf
[MAX_PATH
+1];
1377 char dir_buf
[MAX_PATH
+1];
1378 bool utf8
= playlist
->utf8
;
1380 if (buf_length
> MAX_PATH
+1)
1381 buf_length
= MAX_PATH
+1;
1383 #ifdef HAVE_DIRCACHE
1384 if (dircache_is_enabled() && playlist
->filenames
)
1386 if (playlist
->filenames
[index
] != NULL
)
1388 dircache_copy_path(playlist
->filenames
[index
], tmp_buf
, sizeof(tmp_buf
)-1);
1389 max
= strlen(tmp_buf
) + 1;
1396 if (playlist
->in_ram
&& !control_file
&& max
< 0)
1398 strncpy(tmp_buf
, &playlist
->buffer
[seek
], sizeof(tmp_buf
));
1399 tmp_buf
[MAX_PATH
] = '\0';
1400 max
= strlen(tmp_buf
) + 1;
1404 mutex_lock(&playlist
->control_mutex
);
1408 fd
= playlist
->control_fd
;
1413 if(-1 == playlist
->fd
)
1414 playlist
->fd
= open(playlist
->filename
, O_RDONLY
);
1422 if (lseek(fd
, seek
, SEEK_SET
) != seek
)
1426 max
= read(fd
, tmp_buf
, MIN((size_t) buf_length
, sizeof(tmp_buf
)));
1428 if ((max
> 0) && !utf8
)
1430 /* Use dir_buf as a temporary buffer. Note that dir_buf must
1431 * be as large as tmp_buf.
1433 max
= convert_m3u(tmp_buf
, max
, sizeof(tmp_buf
), dir_buf
);
1438 mutex_unlock(&playlist
->control_mutex
);
1443 gui_syncsplash(HZ
*2, ID2P(LANG_PLAYLIST_CONTROL_ACCESS_ERROR
));
1445 gui_syncsplash(HZ
*2, ID2P(LANG_PLAYLIST_ACCESS_ERROR
));
1451 strncpy(dir_buf
, playlist
->filename
, playlist
->dirlen
-1);
1452 dir_buf
[playlist
->dirlen
-1] = 0;
1454 return (format_track_path(buf
, tmp_buf
, buf_length
, max
, dir_buf
));
1457 static int get_next_directory(char *dir
){
1458 return get_next_dir(dir
,true,false);
1461 static int get_previous_directory(char *dir
){
1462 return get_next_dir(dir
,false,false);
1466 * search through all the directories (starting with the current) to find
1467 * one that has tracks to play
1469 static int get_next_dir(char *dir
, bool is_forward
, bool recursion
)
1471 struct playlist_info
* playlist
= ¤t_playlist
;
1473 int sort_dir
= global_settings
.sort_dir
;
1474 char *start_dir
= NULL
;
1476 struct tree_context
* tc
= tree_get_context();
1477 int dirfilter
= *(tc
->dirfilter
);
1479 if (global_settings
.next_folder
== FOLDER_ADVANCE_RANDOM
)
1481 int fd
= open(ROCKBOX_DIR
"/folder_advance_list.dat",O_RDONLY
);
1482 char buffer
[MAX_PATH
];
1483 int folder_count
= 0,i
;
1484 srand(current_tick
);
1485 *(tc
->dirfilter
) = SHOW_MUSIC
;
1488 read(fd
,&folder_count
,sizeof(int));
1491 i
= rand()%folder_count
;
1492 lseek(fd
,sizeof(int) + (MAX_PATH
*i
),SEEK_SET
);
1493 read(fd
,buffer
,MAX_PATH
);
1494 if (check_subdir_for_music(buffer
,"") ==0)
1499 *(tc
->dirfilter
) = dirfilter
;
1504 /* not random folder advance */
1506 /* start with root */
1510 /* start with current directory */
1511 strncpy(dir
, playlist
->filename
, playlist
->dirlen
-1);
1512 dir
[playlist
->dirlen
-1] = '\0';
1515 /* use the tree browser dircache to load files */
1516 *(tc
->dirfilter
) = SHOW_ALL
;
1518 /* sort in another direction if previous dir is requested */
1520 if ((global_settings
.sort_dir
== 0) || (global_settings
.sort_dir
== 3))
1521 global_settings
.sort_dir
= 4;
1522 else if (global_settings
.sort_dir
== 1)
1523 global_settings
.sort_dir
= 2;
1524 else if (global_settings
.sort_dir
== 2)
1525 global_settings
.sort_dir
= 1;
1526 else if (global_settings
.sort_dir
== 4)
1527 global_settings
.sort_dir
= 0;
1532 struct entry
*files
;
1536 if (ft_load(tc
, (dir
[0]=='\0')?"/":dir
) < 0)
1538 gui_syncsplash(HZ
*2, ID2P(LANG_PLAYLIST_DIRECTORY_ACCESS_ERROR
));
1544 files
= (struct entry
*) tc
->dircache
;
1545 num_files
= tc
->filesindir
;
1547 for (i
=0; i
<num_files
; i
++)
1550 if (action_userabort(TIMEOUT_NOBLOCK
))
1557 if (files
[i
].attr
& ATTR_DIRECTORY
)
1561 result
= check_subdir_for_music(dir
, files
[i
].name
);
1568 else if (!strcmp(start_dir
, files
[i
].name
))
1575 /* move down to parent directory. current directory name is
1576 stored as the starting point for the search in parent */
1577 start_dir
= strrchr(dir
, '/');
1588 /* restore dirfilter & sort_dir */
1589 *(tc
->dirfilter
) = dirfilter
;
1590 global_settings
.sort_dir
= sort_dir
;
1592 /* special case if nothing found: try start searching again from root */
1593 if (result
== -1 && !recursion
){
1594 result
= get_next_dir(dir
,is_forward
, true);
1601 * Checks if there are any music files in the dir or any of its
1602 * subdirectories. May be called recursively.
1604 static int check_subdir_for_music(char *dir
, char *subdir
)
1607 int dirlen
= strlen(dir
);
1610 struct entry
*files
;
1611 bool has_music
= false;
1612 bool has_subdir
= false;
1613 struct tree_context
* tc
= tree_get_context();
1615 snprintf(dir
+dirlen
, MAX_PATH
-dirlen
, "/%s", subdir
);
1617 if (ft_load(tc
, dir
) < 0)
1619 gui_syncsplash(HZ
*2, ID2P(LANG_PLAYLIST_DIRECTORY_ACCESS_ERROR
));
1623 files
= (struct entry
*) tc
->dircache
;
1624 num_files
= tc
->filesindir
;
1626 for (i
=0; i
<num_files
; i
++)
1628 if (files
[i
].attr
& ATTR_DIRECTORY
)
1630 else if ((files
[i
].attr
& FILE_ATTR_MASK
) == FILE_ATTR_AUDIO
)
1642 for (i
=0; i
<num_files
; i
++)
1644 if (action_userabort(TIMEOUT_NOBLOCK
))
1650 if (files
[i
].attr
& ATTR_DIRECTORY
)
1652 result
= check_subdir_for_music(dir
, files
[i
].name
);
1670 /* we now need to reload our current directory */
1671 if(ft_load(tc
, dir
) < 0)
1672 gui_syncsplash(HZ
*2,
1673 ID2P(LANG_PLAYLIST_DIRECTORY_ACCESS_ERROR
));
1680 * Returns absolute path of track
1682 static int format_track_path(char *dest
, char *src
, int buf_length
, int max
,
1689 /* Zero-terminate the file name */
1690 while((src
[i
] != '\n') &&
1695 /* Now work back killing white space */
1696 while((src
[i
-1] == ' ') ||
1702 /* replace backslashes with forward slashes */
1703 for ( j
=0; j
<i
; j
++ )
1704 if ( src
[j
] == '\\' )
1709 strncpy(dest
, src
, buf_length
);
1713 /* handle dos style drive letter */
1715 strncpy(dest
, &src
[2], buf_length
);
1716 else if (!strncmp(src
, "../", 3))
1718 /* handle relative paths */
1720 while(!strncmp(&src
[i
], "../", 3))
1722 for (j
=0; j
<i
/3; j
++) {
1723 temp_ptr
= strrchr(dir
, '/');
1729 snprintf(dest
, buf_length
, "%s/%s", dir
, &src
[i
]);
1731 else if ( '.' == src
[0] && '/' == src
[1] ) {
1732 snprintf(dest
, buf_length
, "%s/%s", dir
, &src
[2]);
1735 snprintf(dest
, buf_length
, "%s/%s", dir
, src
);
1743 * Display splash message showing progress of playlist/directory insertion or
1746 static void display_playlist_count(int count
, const unsigned char *fmt
,
1749 static long talked_tick
= 0;
1750 long id
= P2ID(fmt
);
1751 if(global_settings
.talk_menu
&& id
>=0)
1753 if(final
|| (count
&& (talked_tick
== 0
1754 || TIME_AFTER(current_tick
, talked_tick
+5*HZ
))))
1756 talked_tick
= current_tick
;
1757 talk_number(count
, false);
1763 gui_syncsplash(0, fmt
, count
, str(LANG_OFF_ABORT
));
1767 * Display buffer full message
1769 static void display_buffer_full(void)
1771 gui_syncsplash(HZ
*2, ID2P(LANG_PLAYLIST_BUFFER_FULL
));
1775 * Flush any cached control commands to disk. Called when playlist is being
1776 * modified. Returns 0 on success and -1 on failure.
1778 static int flush_cached_control(struct playlist_info
* playlist
)
1783 if (!playlist
->num_cached
)
1786 lseek(playlist
->control_fd
, 0, SEEK_END
);
1788 for (i
=0; i
<playlist
->num_cached
; i
++)
1790 struct playlist_control_cache
* cache
=
1791 &(playlist
->control_cache
[i
]);
1793 switch (cache
->command
)
1795 case PLAYLIST_COMMAND_PLAYLIST
:
1796 result
= fdprintf(playlist
->control_fd
, "P:%d:%s:%s\n",
1797 cache
->i1
, cache
->s1
, cache
->s2
);
1799 case PLAYLIST_COMMAND_ADD
:
1800 case PLAYLIST_COMMAND_QUEUE
:
1801 result
= fdprintf(playlist
->control_fd
, "%c:%d:%d:",
1802 (cache
->command
== PLAYLIST_COMMAND_ADD
)?'A':'Q',
1803 cache
->i1
, cache
->i2
);
1806 /* save the position in file where name is written */
1807 int* seek_pos
= (int *)cache
->data
;
1808 *seek_pos
= lseek(playlist
->control_fd
, 0, SEEK_CUR
);
1809 result
= fdprintf(playlist
->control_fd
, "%s\n",
1813 case PLAYLIST_COMMAND_DELETE
:
1814 result
= fdprintf(playlist
->control_fd
, "D:%d\n", cache
->i1
);
1816 case PLAYLIST_COMMAND_SHUFFLE
:
1817 result
= fdprintf(playlist
->control_fd
, "S:%d:%d\n",
1818 cache
->i1
, cache
->i2
);
1820 case PLAYLIST_COMMAND_UNSHUFFLE
:
1821 result
= fdprintf(playlist
->control_fd
, "U:%d\n", cache
->i1
);
1823 case PLAYLIST_COMMAND_RESET
:
1824 result
= fdprintf(playlist
->control_fd
, "R\n");
1836 if (global_status
.resume_seed
>= 0)
1838 global_status
.resume_seed
= -1;
1842 playlist
->num_cached
= 0;
1843 playlist
->pending_control_sync
= true;
1850 gui_syncsplash(HZ
*2, ID2P(LANG_PLAYLIST_CONTROL_UPDATE_ERROR
));
1857 * Update control data with new command. Depending on the command, it may be
1858 * cached or flushed to disk.
1860 static int update_control(struct playlist_info
* playlist
,
1861 enum playlist_command command
, int i1
, int i2
,
1862 const char* s1
, const char* s2
, void* data
)
1865 struct playlist_control_cache
* cache
;
1868 mutex_lock(&playlist
->control_mutex
);
1870 cache
= &(playlist
->control_cache
[playlist
->num_cached
++]);
1872 cache
->command
= command
;
1881 case PLAYLIST_COMMAND_PLAYLIST
:
1882 case PLAYLIST_COMMAND_ADD
:
1883 case PLAYLIST_COMMAND_QUEUE
:
1884 #ifndef HAVE_DIRCACHE
1885 case PLAYLIST_COMMAND_DELETE
:
1886 case PLAYLIST_COMMAND_RESET
:
1890 case PLAYLIST_COMMAND_SHUFFLE
:
1891 case PLAYLIST_COMMAND_UNSHUFFLE
:
1893 /* only flush when needed */
1897 if (flush
|| playlist
->num_cached
== PLAYLIST_MAX_CACHE
)
1898 result
= flush_cached_control(playlist
);
1900 mutex_unlock(&playlist
->control_mutex
);
1906 * sync control file to disk
1908 static void sync_control(struct playlist_info
* playlist
, bool force
)
1910 #ifdef HAVE_DIRCACHE
1911 if (playlist
->started
&& force
)
1915 if (playlist
->started
)
1918 if (playlist
->pending_control_sync
)
1920 mutex_lock(&playlist
->control_mutex
);
1921 fsync(playlist
->control_fd
);
1922 playlist
->pending_control_sync
= false;
1923 mutex_unlock(&playlist
->control_mutex
);
1929 * Rotate indices such that first_index is index 0
1931 static int rotate_index(const struct playlist_info
* playlist
, int index
)
1933 index
-= playlist
->first_index
;
1935 index
+= playlist
->amount
;
1941 * Initialize playlist entries at startup
1943 void playlist_init(void)
1945 struct playlist_info
* playlist
= ¤t_playlist
;
1947 playlist
->current
= true;
1948 snprintf(playlist
->control_filename
, sizeof(playlist
->control_filename
),
1949 "%s", PLAYLIST_CONTROL_FILE
);
1951 playlist
->control_fd
= -1;
1952 playlist
->max_playlist_size
= global_settings
.max_files_in_playlist
;
1953 playlist
->indices
= buffer_alloc(
1954 playlist
->max_playlist_size
* sizeof(int));
1955 playlist
->buffer_size
=
1956 AVERAGE_FILENAME_LENGTH
* global_settings
.max_files_in_dir
;
1957 playlist
->buffer
= buffer_alloc(playlist
->buffer_size
);
1958 mutex_init(&playlist
->control_mutex
);
1959 empty_playlist(playlist
, true);
1961 #ifdef HAVE_DIRCACHE
1962 playlist
->filenames
= buffer_alloc(
1963 playlist
->max_playlist_size
* sizeof(int));
1964 memset(playlist
->filenames
, 0,
1965 playlist
->max_playlist_size
* sizeof(int));
1966 create_thread(playlist_thread
, playlist_stack
, sizeof(playlist_stack
),
1967 0, playlist_thread_name
IF_PRIO(, PRIORITY_BACKGROUND
)
1969 queue_init(&playlist_queue
, true);
1974 * Clean playlist at shutdown
1976 void playlist_shutdown(void)
1978 struct playlist_info
* playlist
= ¤t_playlist
;
1980 if (playlist
->control_fd
>= 0)
1982 mutex_lock(&playlist
->control_mutex
);
1984 if (playlist
->num_cached
> 0)
1985 flush_cached_control(playlist
);
1987 close(playlist
->control_fd
);
1989 mutex_unlock(&playlist
->control_mutex
);
1994 * Create new playlist
1996 int playlist_create(const char *dir
, const char *file
)
1998 struct playlist_info
* playlist
= ¤t_playlist
;
2000 new_playlist(playlist
, dir
, file
);
2003 /* load the playlist file */
2004 add_indices_to_playlist(playlist
, NULL
, 0);
2009 #define PLAYLIST_COMMAND_SIZE (MAX_PATH+12)
2012 * Restore the playlist state based on control file commands. Called to
2013 * resume playback after shutdown.
2015 int playlist_resume(void)
2017 struct playlist_info
* playlist
= ¤t_playlist
;
2022 int control_file_size
= 0;
2026 /* use mp3 buffer for maximum load speed */
2027 #if CONFIG_CODEC != SWCODEC
2028 talk_buffer_steal(); /* we use the mp3 buffer, need to tell */
2029 buflen
= (audiobufend
- audiobuf
);
2030 buffer
= (char *)audiobuf
;
2032 buffer
= (char *)audio_get_buffer(false, &buflen
);
2035 empty_playlist(playlist
, true);
2037 gui_syncsplash(0, ID2P(LANG_WAIT
));
2038 playlist
->control_fd
= open(playlist
->control_filename
, O_RDWR
);
2039 if (playlist
->control_fd
< 0)
2041 gui_syncsplash(HZ
*2, ID2P(LANG_PLAYLIST_CONTROL_ACCESS_ERROR
));
2044 playlist
->control_created
= true;
2046 control_file_size
= filesize(playlist
->control_fd
);
2047 if (control_file_size
<= 0)
2049 gui_syncsplash(HZ
*2, ID2P(LANG_PLAYLIST_CONTROL_ACCESS_ERROR
));
2053 /* read a small amount first to get the header */
2054 nread
= read(playlist
->control_fd
, buffer
,
2055 PLAYLIST_COMMAND_SIZE
<buflen
?PLAYLIST_COMMAND_SIZE
:buflen
);
2058 gui_syncsplash(HZ
*2, ID2P(LANG_PLAYLIST_CONTROL_ACCESS_ERROR
));
2062 playlist
->started
= true;
2068 enum playlist_command current_command
= PLAYLIST_COMMAND_COMMENT
;
2069 int last_newline
= 0;
2071 bool newline
= true;
2072 bool exit_loop
= false;
2077 unsigned long last_tick
= current_tick
;
2079 for(count
=0; count
<nread
&& !exit_loop
; count
++,p
++)
2081 /* So a splash while we are loading. */
2082 if (current_tick
- last_tick
> HZ
/4)
2084 gui_syncsplash(0, str(LANG_LOADING_PERCENT
),
2085 (total_read
+count
)*100/control_file_size
,
2086 str(LANG_OFF_ABORT
));
2087 if (action_userabort(TIMEOUT_NOBLOCK
))
2090 * Not sure how to implement this, somebody more familiar
2091 * with the code, please fix this. */
2093 last_tick
= current_tick
;
2096 /* Are we on a new line? */
2097 if((*p
== '\n') || (*p
== '\r'))
2101 /* save last_newline in case we need to load more data */
2102 last_newline
= count
;
2104 switch (current_command
)
2106 case PLAYLIST_COMMAND_PLAYLIST
:
2108 /* str1=version str2=dir str3=file */
2124 version
= atoi(str1
);
2126 if (version
!= PLAYLIST_CONTROL_FILE_VERSION
)
2129 update_playlist_filename(playlist
, str2
, str3
);
2131 if (str3
[0] != '\0')
2133 /* NOTE: add_indices_to_playlist() overwrites the
2134 audiobuf so we need to reload control file
2136 add_indices_to_playlist(playlist
, NULL
, 0);
2138 else if (str2
[0] != '\0')
2140 playlist
->in_ram
= true;
2141 resume_directory(str2
);
2144 /* load the rest of the data */
2150 case PLAYLIST_COMMAND_ADD
:
2151 case PLAYLIST_COMMAND_QUEUE
:
2153 /* str1=position str2=last_position str3=file */
2154 int position
, last_position
;
2157 if (!str1
|| !str2
|| !str3
)
2164 position
= atoi(str1
);
2165 last_position
= atoi(str2
);
2167 queue
= (current_command
== PLAYLIST_COMMAND_ADD
)?
2170 /* seek position is based on str3's position in
2172 if (add_track_to_playlist(playlist
, str3
, position
,
2173 queue
, total_read
+(str3
-buffer
)) < 0)
2176 playlist
->last_insert_pos
= last_position
;
2180 case PLAYLIST_COMMAND_DELETE
:
2192 position
= atoi(str1
);
2194 if (remove_track_from_playlist(playlist
, position
,
2200 case PLAYLIST_COMMAND_SHUFFLE
:
2202 /* str1=seed str2=first_index */
2214 /* Always sort list before shuffling */
2215 sort_playlist(playlist
, false, false);
2219 playlist
->first_index
= atoi(str2
);
2221 if (randomise_playlist(playlist
, seed
, false,
2228 case PLAYLIST_COMMAND_UNSHUFFLE
:
2230 /* str1=first_index */
2238 playlist
->first_index
= atoi(str1
);
2240 if (sort_playlist(playlist
, false, false) < 0)
2246 case PLAYLIST_COMMAND_RESET
:
2248 playlist
->last_insert_pos
= -1;
2251 case PLAYLIST_COMMAND_COMMENT
:
2258 /* to ignore any extra newlines */
2259 current_command
= PLAYLIST_COMMAND_COMMENT
;
2265 /* first non-comment line must always specify playlist */
2266 if (first
&& *p
!= 'P' && *p
!= '#')
2276 /* playlist can only be specified once */
2284 current_command
= PLAYLIST_COMMAND_PLAYLIST
;
2287 current_command
= PLAYLIST_COMMAND_ADD
;
2290 current_command
= PLAYLIST_COMMAND_QUEUE
;
2293 current_command
= PLAYLIST_COMMAND_DELETE
;
2296 current_command
= PLAYLIST_COMMAND_SHUFFLE
;
2299 current_command
= PLAYLIST_COMMAND_UNSHUFFLE
;
2302 current_command
= PLAYLIST_COMMAND_RESET
;
2305 current_command
= PLAYLIST_COMMAND_COMMENT
;
2318 else if(current_command
!= PLAYLIST_COMMAND_COMMENT
)
2320 /* all control file strings are separated with a colon.
2321 Replace the colon with 0 to get proper strings that can be
2322 used by commands above */
2328 if ((count
+1) < nread
)
2342 /* allow last string to contain colons */
2353 gui_syncsplash(HZ
*2, ID2P(LANG_PLAYLIST_CONTROL_INVALID
));
2357 if (!newline
|| (exit_loop
&& count
<nread
))
2359 if ((total_read
+ count
) >= control_file_size
)
2361 /* no newline at end of control file */
2362 gui_syncsplash(HZ
*2, ID2P(LANG_PLAYLIST_CONTROL_INVALID
));
2366 /* We didn't end on a newline or we exited loop prematurely.
2367 Either way, re-read the remainder. */
2368 count
= last_newline
;
2369 lseek(playlist
->control_fd
, total_read
+count
, SEEK_SET
);
2372 total_read
+= count
;
2375 /* still looking for header */
2376 nread
= read(playlist
->control_fd
, buffer
,
2377 PLAYLIST_COMMAND_SIZE
<buflen
?PLAYLIST_COMMAND_SIZE
:buflen
);
2379 nread
= read(playlist
->control_fd
, buffer
, buflen
);
2381 /* Terminate on EOF */
2384 if (global_status
.resume_seed
>= 0)
2386 /* Apply shuffle command saved in settings */
2387 if (global_status
.resume_seed
== 0)
2388 sort_playlist(playlist
, false, true);
2392 sort_playlist(playlist
, false, false);
2394 randomise_playlist(playlist
, global_status
.resume_seed
,
2399 playlist
->first_index
= global_status
.resume_first_index
;
2404 #ifdef HAVE_DIRCACHE
2405 queue_post(&playlist_queue
, PLAYLIST_LOAD_POINTERS
, 0);
2412 * Add track to in_ram playlist. Used when playing directories.
2414 int playlist_add(const char *filename
)
2416 struct playlist_info
* playlist
= ¤t_playlist
;
2417 int len
= strlen(filename
);
2419 if((len
+1 > playlist
->buffer_size
- playlist
->buffer_end_pos
) ||
2420 (playlist
->amount
>= playlist
->max_playlist_size
))
2422 display_buffer_full();
2426 playlist
->indices
[playlist
->amount
] = playlist
->buffer_end_pos
;
2427 #ifdef HAVE_DIRCACHE
2428 playlist
->filenames
[playlist
->amount
] = NULL
;
2432 strcpy(&playlist
->buffer
[playlist
->buffer_end_pos
], filename
);
2433 playlist
->buffer_end_pos
+= len
;
2434 playlist
->buffer
[playlist
->buffer_end_pos
++] = '\0';
2439 /* shuffle newly created playlist using random seed. */
2440 int playlist_shuffle(int random_seed
, int start_index
)
2442 struct playlist_info
* playlist
= ¤t_playlist
;
2444 unsigned int seek_pos
= 0;
2445 bool start_current
= false;
2447 if (start_index
>= 0 && global_settings
.play_selected
)
2449 /* store the seek position before the shuffle */
2450 seek_pos
= playlist
->indices
[start_index
];
2451 playlist
->index
= global_status
.resume_first_index
=
2452 playlist
->first_index
= start_index
;
2453 start_current
= true;
2456 randomise_playlist(playlist
, random_seed
, start_current
, true);
2458 return playlist
->index
;
2461 /* start playing current playlist at specified index/offset */
2462 int playlist_start(int start_index
, int offset
)
2464 struct playlist_info
* playlist
= ¤t_playlist
;
2466 /* Cancel FM radio selection as previous music. For cases where we start
2467 playback without going to the WPS, such as playlist insert.. or
2468 playlist catalog. */
2469 previous_music_is_wps();
2471 playlist
->index
= start_index
;
2473 #if CONFIG_CODEC != SWCODEC
2474 talk_buffer_steal(); /* will use the mp3 buffer */
2477 playlist
->started
= true;
2478 sync_control(playlist
, false);
2484 /* Returns false if 'steps' is out of bounds, else true */
2485 bool playlist_check(int steps
)
2487 struct playlist_info
* playlist
= ¤t_playlist
;
2489 /* always allow folder navigation */
2490 if (global_settings
.next_folder
&& playlist
->in_ram
)
2493 int index
= get_next_index(playlist
, steps
, -1);
2495 if (index
< 0 && steps
>= 0 && global_settings
.repeat_mode
== REPEAT_SHUFFLE
)
2496 index
= get_next_index(playlist
, steps
, REPEAT_ALL
);
2498 return (index
>= 0);
2501 /* get trackname of track that is "steps" away from current playing track.
2502 NULL is used to identify end of playlist */
2503 char* playlist_peek(int steps
)
2505 struct playlist_info
* playlist
= ¤t_playlist
;
2511 index
= get_next_index(playlist
, steps
, -1);
2515 control_file
= playlist
->indices
[index
] & PLAYLIST_INSERT_TYPE_MASK
;
2516 seek
= playlist
->indices
[index
] & PLAYLIST_SEEK_MASK
;
2518 if (get_filename(playlist
, index
, seek
, control_file
, now_playing
,
2522 temp_ptr
= now_playing
;
2524 if (!playlist
->in_ram
|| control_file
)
2526 /* remove bogus dirs from beginning of path
2527 (workaround for buggy playlist creation tools) */
2530 if (file_exists(temp_ptr
))
2533 temp_ptr
= strchr(temp_ptr
+1, '/');
2538 /* Even though this is an invalid file, we still need to pass a
2539 file name to the caller because NULL is used to indicate end
2549 * Update indices as track has changed
2551 int playlist_next(int steps
)
2553 struct playlist_info
* playlist
= ¤t_playlist
;
2557 #ifdef AB_REPEAT_ENABLE
2558 && (global_settings
.repeat_mode
!= REPEAT_AB
)
2560 && (global_settings
.repeat_mode
!= REPEAT_ONE
) )
2564 /* We need to delete all the queued songs */
2565 for (i
=0, j
=steps
; i
<j
; i
++)
2567 index
= get_next_index(playlist
, i
, -1);
2569 if (playlist
->indices
[index
] & PLAYLIST_QUEUE_MASK
)
2571 remove_track_from_playlist(playlist
, index
, true);
2572 steps
--; /* one less track */
2577 index
= get_next_index(playlist
, steps
, -1);
2581 /* end of playlist... or is it */
2582 if (global_settings
.repeat_mode
== REPEAT_SHUFFLE
&&
2583 playlist
->amount
> 1)
2585 /* Repeat shuffle mode. Re-shuffle playlist and resume play */
2586 playlist
->first_index
= global_status
.resume_first_index
= 0;
2587 sort_playlist(playlist
, false, false);
2588 randomise_playlist(playlist
, current_tick
, false, true);
2589 #if CONFIG_CODEC != SWCODEC
2590 playlist_start(0, 0);
2592 playlist
->index
= 0;
2595 else if (playlist
->in_ram
&& global_settings
.next_folder
)
2597 index
= create_and_play_dir(steps
, true);
2601 playlist
->index
= index
;
2608 playlist
->index
= index
;
2610 if (playlist
->last_insert_pos
>= 0 && steps
> 0)
2612 /* check to see if we've gone beyond the last inserted track */
2613 int cur
= rotate_index(playlist
, index
);
2614 int last_pos
= rotate_index(playlist
, playlist
->last_insert_pos
);
2618 /* reset last inserted track */
2619 playlist
->last_insert_pos
= -1;
2621 if (playlist
->control_fd
>= 0)
2623 int result
= update_control(playlist
, PLAYLIST_COMMAND_RESET
,
2624 -1, -1, NULL
, NULL
, NULL
);
2629 sync_control(playlist
, false);
2637 /* try playing next or previous folder */
2638 bool playlist_next_dir(int direction
)
2640 /* not to mess up real playlists */
2641 if(!current_playlist
.in_ram
)
2644 return create_and_play_dir(direction
, false) >= 0;
2647 /* Get resume info for current playing song. If return value is -1 then
2648 settings shouldn't be saved. */
2649 int playlist_get_resume_info(int *resume_index
)
2651 struct playlist_info
* playlist
= ¤t_playlist
;
2653 *resume_index
= playlist
->index
;
2658 /* Update resume info for current playing song. Returns -1 on error. */
2659 int playlist_update_resume_info(const struct mp3entry
* id3
)
2661 struct playlist_info
* playlist
= ¤t_playlist
;
2665 if (global_status
.resume_index
!= playlist
->index
||
2666 global_status
.resume_offset
!= id3
->offset
)
2668 global_status
.resume_index
= playlist
->index
;
2669 global_status
.resume_offset
= id3
->offset
;
2675 global_status
.resume_index
= -1;
2676 global_status
.resume_offset
= -1;
2683 /* Returns index of current playing track for display purposes. This value
2684 should not be used for resume purposes as it doesn't represent the actual
2685 index into the playlist */
2686 int playlist_get_display_index(void)
2688 struct playlist_info
* playlist
= ¤t_playlist
;
2690 /* first_index should always be index 0 for display purposes */
2691 int index
= rotate_index(playlist
, playlist
->index
);
2696 /* returns number of tracks in current playlist */
2697 int playlist_amount(void)
2699 return playlist_amount_ex(NULL
);
2703 * Create a new playlist If playlist is not NULL then we're loading a
2704 * playlist off disk for viewing/editing. The index_buffer is used to store
2705 * playlist indices (required for and only used if !current playlist). The
2706 * temp_buffer (if not NULL) is used as a scratchpad when loading indices.
2708 int playlist_create_ex(struct playlist_info
* playlist
,
2709 const char* dir
, const char* file
,
2710 void* index_buffer
, int index_buffer_size
,
2711 void* temp_buffer
, int temp_buffer_size
)
2714 playlist
= ¤t_playlist
;
2717 /* Initialize playlist structure */
2718 int r
= rand() % 10;
2719 playlist
->current
= false;
2721 /* Use random name for control file */
2722 snprintf(playlist
->control_filename
, sizeof(playlist
->control_filename
),
2723 "%s.%d", PLAYLIST_CONTROL_FILE
, r
);
2725 playlist
->control_fd
= -1;
2729 int num_indices
= index_buffer_size
/ sizeof(int);
2731 #ifdef HAVE_DIRCACHE
2734 if (num_indices
> global_settings
.max_files_in_playlist
)
2735 num_indices
= global_settings
.max_files_in_playlist
;
2737 playlist
->max_playlist_size
= num_indices
;
2738 playlist
->indices
= index_buffer
;
2739 #ifdef HAVE_DIRCACHE
2740 playlist
->filenames
= (const struct dircache_entry
**)
2741 &playlist
->indices
[num_indices
];
2746 playlist
->max_playlist_size
= current_playlist
.max_playlist_size
;
2747 playlist
->indices
= current_playlist
.indices
;
2748 #ifdef HAVE_DIRCACHE
2749 playlist
->filenames
= current_playlist
.filenames
;
2753 playlist
->buffer_size
= 0;
2754 playlist
->buffer
= NULL
;
2755 mutex_init(&playlist
->control_mutex
);
2758 new_playlist(playlist
, dir
, file
);
2761 /* load the playlist file */
2762 add_indices_to_playlist(playlist
, temp_buffer
, temp_buffer_size
);
2768 * Set the specified playlist as the current.
2769 * NOTE: You will get undefined behaviour if something is already playing so
2770 * remember to stop before calling this. Also, this call will
2771 * effectively close your playlist, making it unusable.
2773 int playlist_set_current(struct playlist_info
* playlist
)
2775 if (!playlist
|| (check_control(playlist
) < 0))
2778 empty_playlist(¤t_playlist
, false);
2780 strncpy(current_playlist
.filename
, playlist
->filename
,
2781 sizeof(current_playlist
.filename
));
2783 current_playlist
.utf8
= playlist
->utf8
;
2784 current_playlist
.fd
= playlist
->fd
;
2786 close(playlist
->control_fd
);
2787 close(current_playlist
.control_fd
);
2788 remove(current_playlist
.control_filename
);
2789 if (rename(playlist
->control_filename
,
2790 current_playlist
.control_filename
) < 0)
2792 current_playlist
.control_fd
= open(current_playlist
.control_filename
,
2794 if (current_playlist
.control_fd
< 0)
2796 current_playlist
.control_created
= true;
2798 current_playlist
.dirlen
= playlist
->dirlen
;
2800 if (playlist
->indices
&& playlist
->indices
!= current_playlist
.indices
)
2802 memcpy(current_playlist
.indices
, playlist
->indices
,
2803 playlist
->max_playlist_size
*sizeof(int));
2804 #ifdef HAVE_DIRCACHE
2805 memcpy(current_playlist
.filenames
, playlist
->filenames
,
2806 playlist
->max_playlist_size
*sizeof(int));
2810 current_playlist
.first_index
= playlist
->first_index
;
2811 current_playlist
.amount
= playlist
->amount
;
2812 current_playlist
.last_insert_pos
= playlist
->last_insert_pos
;
2813 current_playlist
.seed
= playlist
->seed
;
2814 current_playlist
.shuffle_modified
= playlist
->shuffle_modified
;
2815 current_playlist
.deleted
= playlist
->deleted
;
2816 current_playlist
.num_inserted_tracks
= playlist
->num_inserted_tracks
;
2818 memcpy(current_playlist
.control_cache
, playlist
->control_cache
,
2819 sizeof(current_playlist
.control_cache
));
2820 current_playlist
.num_cached
= playlist
->num_cached
;
2821 current_playlist
.pending_control_sync
= playlist
->pending_control_sync
;
2827 * Close files and delete control file for non-current playlist.
2829 void playlist_close(struct playlist_info
* playlist
)
2834 if (playlist
->fd
>= 0)
2835 close(playlist
->fd
);
2837 if (playlist
->control_fd
>= 0)
2838 close(playlist
->control_fd
);
2840 if (playlist
->control_created
)
2841 remove(playlist
->control_filename
);
2844 void playlist_sync(struct playlist_info
* playlist
)
2847 playlist
= ¤t_playlist
;
2849 sync_control(playlist
, false);
2850 if ((audio_status() & AUDIO_STATUS_PLAY
) && playlist
->started
)
2851 audio_flush_and_reload_tracks();
2853 #ifdef HAVE_DIRCACHE
2854 queue_post(&playlist_queue
, PLAYLIST_LOAD_POINTERS
, 0);
2859 * Insert track into playlist at specified position (or one of the special
2860 * positions). Returns position where track was inserted or -1 if error.
2862 int playlist_insert_track(struct playlist_info
* playlist
, const char *filename
,
2863 int position
, bool queue
, bool sync
)
2868 playlist
= ¤t_playlist
;
2870 if (check_control(playlist
) < 0)
2872 gui_syncsplash(HZ
*2, ID2P(LANG_PLAYLIST_CONTROL_ACCESS_ERROR
));
2876 result
= add_track_to_playlist(playlist
, filename
, position
, queue
, -1);
2878 /* Check if we want manually sync later. For example when adding
2879 * bunch of files from tagcache, syncing after every file wouldn't be
2880 * a good thing to do. */
2881 if (sync
&& result
>= 0)
2882 playlist_sync(playlist
);
2888 * Insert all tracks from specified directory into playlist.
2890 int playlist_insert_directory(struct playlist_info
* playlist
,
2891 const char *dirname
, int position
, bool queue
,
2895 unsigned char *count_str
;
2896 struct directory_search_context context
;
2899 playlist
= ¤t_playlist
;
2901 if (check_control(playlist
) < 0)
2903 gui_syncsplash(HZ
*2, ID2P(LANG_PLAYLIST_CONTROL_ACCESS_ERROR
));
2907 if (position
== PLAYLIST_REPLACE
)
2909 if (playlist_remove_all_tracks(playlist
) == 0)
2910 position
= PLAYLIST_INSERT_LAST
;
2916 count_str
= ID2P(LANG_PLAYLIST_QUEUE_COUNT
);
2918 count_str
= ID2P(LANG_PLAYLIST_INSERT_COUNT
);
2920 display_playlist_count(0, count_str
, false);
2922 context
.playlist
= playlist
;
2923 context
.position
= position
;
2924 context
.queue
= queue
;
2929 result
= playlist_directory_tracksearch(dirname
, recurse
,
2930 directory_search_callback
, &context
);
2932 sync_control(playlist
, false);
2936 display_playlist_count(context
.count
, count_str
, true);
2938 if ((audio_status() & AUDIO_STATUS_PLAY
) && playlist
->started
)
2939 audio_flush_and_reload_tracks();
2941 #ifdef HAVE_DIRCACHE
2942 queue_post(&playlist_queue
, PLAYLIST_LOAD_POINTERS
, 0);
2949 * Insert all tracks from specified playlist into dynamic playlist.
2951 int playlist_insert_playlist(struct playlist_info
* playlist
, const char *filename
,
2952 int position
, bool queue
)
2958 unsigned char *count_str
;
2959 char temp_buf
[MAX_PATH
+1];
2960 char trackname
[MAX_PATH
+1];
2963 bool utf8
= is_m3u8(filename
);
2966 playlist
= ¤t_playlist
;
2968 if (check_control(playlist
) < 0)
2970 gui_syncsplash(HZ
*2, ID2P(LANG_PLAYLIST_CONTROL_ACCESS_ERROR
));
2974 fd
= open(filename
, O_RDONLY
);
2977 gui_syncsplash(HZ
*2, ID2P(LANG_PLAYLIST_ACCESS_ERROR
));
2981 /* we need the directory name for formatting purposes */
2984 temp_ptr
= strrchr(filename
+1,'/');
2991 count_str
= ID2P(LANG_PLAYLIST_QUEUE_COUNT
);
2993 count_str
= ID2P(LANG_PLAYLIST_INSERT_COUNT
);
2995 display_playlist_count(count
, count_str
, false);
2997 if (position
== PLAYLIST_REPLACE
)
2999 if (playlist_remove_all_tracks(playlist
) == 0)
3000 position
= PLAYLIST_INSERT_LAST
;
3006 while ((max
= read_line(fd
, temp_buf
, sizeof(temp_buf
))) > 0)
3009 if (action_userabort(TIMEOUT_NOBLOCK
))
3012 if (count
== 0 && is_utf8_bom(temp_buf
, max
))
3015 memmove(temp_buf
, temp_buf
+ BOM_SIZE
, max
);
3018 if (temp_buf
[0] != '#' && temp_buf
[0] != '\0')
3024 /* Use trackname as a temporay buffer. Note that trackname must
3025 * be as large as temp_buf.
3027 max
= convert_m3u(temp_buf
, max
, sizeof(temp_buf
), trackname
);
3030 /* we need to format so that relative paths are correctly
3032 if (format_track_path(trackname
, temp_buf
, sizeof(trackname
), max
,
3039 insert_pos
= add_track_to_playlist(playlist
, trackname
, position
,
3048 /* Make sure tracks are inserted in correct order if user
3049 requests INSERT_FIRST */
3050 if (position
== PLAYLIST_INSERT_FIRST
|| position
>= 0)
3051 position
= insert_pos
+ 1;
3055 if ((count
%PLAYLIST_DISPLAY_COUNT
) == 0)
3057 display_playlist_count(count
, count_str
, false);
3059 if (count
== PLAYLIST_DISPLAY_COUNT
&&
3060 (audio_status() & AUDIO_STATUS_PLAY
) &&
3062 audio_flush_and_reload_tracks();
3066 /* let the other threads work */
3075 sync_control(playlist
, false);
3079 display_playlist_count(count
, count_str
, true);
3081 if ((audio_status() & AUDIO_STATUS_PLAY
) && playlist
->started
)
3082 audio_flush_and_reload_tracks();
3084 #ifdef HAVE_DIRCACHE
3085 queue_post(&playlist_queue
, PLAYLIST_LOAD_POINTERS
, 0);
3092 * Delete track at specified index. If index is PLAYLIST_DELETE_CURRENT then
3093 * we want to delete the current playing track.
3095 int playlist_delete(struct playlist_info
* playlist
, int index
)
3100 playlist
= ¤t_playlist
;
3102 if (check_control(playlist
) < 0)
3104 gui_syncsplash(HZ
*2, ID2P(LANG_PLAYLIST_CONTROL_ACCESS_ERROR
));
3108 if (index
== PLAYLIST_DELETE_CURRENT
)
3109 index
= playlist
->index
;
3111 result
= remove_track_from_playlist(playlist
, index
, true);
3113 if (result
!= -1 && (audio_status() & AUDIO_STATUS_PLAY
) &&
3115 audio_flush_and_reload_tracks();
3121 * Move track at index to new_index. Tracks between the two are shifted
3122 * appropriately. Returns 0 on success and -1 on failure.
3124 int playlist_move(struct playlist_info
* playlist
, int index
, int new_index
)
3130 bool current
= false;
3132 char filename
[MAX_PATH
];
3135 playlist
= ¤t_playlist
;
3137 if (check_control(playlist
) < 0)
3139 gui_syncsplash(HZ
*2, ID2P(LANG_PLAYLIST_CONTROL_ACCESS_ERROR
));
3143 if (index
== new_index
)
3146 if (index
== playlist
->index
)
3147 /* Moving the current track */
3150 control_file
= playlist
->indices
[index
] & PLAYLIST_INSERT_TYPE_MASK
;
3151 queue
= playlist
->indices
[index
] & PLAYLIST_QUEUE_MASK
;
3152 seek
= playlist
->indices
[index
] & PLAYLIST_SEEK_MASK
;
3154 if (get_filename(playlist
, index
, seek
, control_file
, filename
,
3155 sizeof(filename
)) < 0)
3158 /* Delete track from original position */
3159 result
= remove_track_from_playlist(playlist
, index
, true);
3163 /* We want to insert the track at the position that was specified by
3164 new_index. This may be different then new_index because of the
3165 shifting that occurred after the delete */
3166 r
= rotate_index(playlist
, new_index
);
3170 new_index
= PLAYLIST_PREPEND
;
3171 else if (r
== playlist
->amount
)
3173 new_index
= PLAYLIST_INSERT_LAST
;
3175 /* Calculate index of desired position */
3176 new_index
= (r
+playlist
->first_index
)%playlist
->amount
;
3178 result
= add_track_to_playlist(playlist
, filename
, new_index
, queue
,
3185 /* Moved the current track */
3188 case PLAYLIST_PREPEND
:
3189 playlist
->index
= playlist
->first_index
;
3191 case PLAYLIST_INSERT_LAST
:
3192 playlist
->index
= playlist
->first_index
- 1;
3193 if (playlist
->index
< 0)
3194 playlist
->index
+= playlist
->amount
;
3197 playlist
->index
= new_index
;
3202 if ((audio_status() & AUDIO_STATUS_PLAY
) && playlist
->started
)
3203 audio_flush_and_reload_tracks();
3207 #ifdef HAVE_DIRCACHE
3208 queue_post(&playlist_queue
, PLAYLIST_LOAD_POINTERS
, 0);
3214 /* shuffle currently playing playlist */
3215 int playlist_randomise(struct playlist_info
* playlist
, unsigned int seed
,
3221 playlist
= ¤t_playlist
;
3223 check_control(playlist
);
3225 result
= randomise_playlist(playlist
, seed
, start_current
, true);
3227 if (result
!= -1 && (audio_status() & AUDIO_STATUS_PLAY
) &&
3229 audio_flush_and_reload_tracks();
3234 /* sort currently playing playlist */
3235 int playlist_sort(struct playlist_info
* playlist
, bool start_current
)
3240 playlist
= ¤t_playlist
;
3242 check_control(playlist
);
3244 result
= sort_playlist(playlist
, start_current
, true);
3246 if (result
!= -1 && (audio_status() & AUDIO_STATUS_PLAY
) &&
3248 audio_flush_and_reload_tracks();
3253 /* returns true if playlist has been modified */
3254 bool playlist_modified(const struct playlist_info
* playlist
)
3257 playlist
= ¤t_playlist
;
3259 if (playlist
->shuffle_modified
||
3260 playlist
->deleted
||
3261 playlist
->num_inserted_tracks
> 0)
3267 /* returns index of first track in playlist */
3268 int playlist_get_first_index(const struct playlist_info
* playlist
)
3271 playlist
= ¤t_playlist
;
3273 return playlist
->first_index
;
3276 /* returns shuffle seed of playlist */
3277 int playlist_get_seed(const struct playlist_info
* playlist
)
3280 playlist
= ¤t_playlist
;
3282 return playlist
->seed
;
3285 /* returns number of tracks in playlist (includes queued/inserted tracks) */
3286 int playlist_amount_ex(const struct playlist_info
* playlist
)
3289 playlist
= ¤t_playlist
;
3291 return playlist
->amount
;
3294 /* returns full path of playlist (minus extension) */
3295 char *playlist_name(const struct playlist_info
* playlist
, char *buf
,
3301 playlist
= ¤t_playlist
;
3303 snprintf(buf
, buf_size
, "%s", playlist
->filename
+playlist
->dirlen
);
3308 /* Remove extension */
3309 sep
= strrchr(buf
, '.');
3316 /* returns the playlist filename */
3317 char *playlist_get_name(const struct playlist_info
* playlist
, char *buf
,
3321 playlist
= ¤t_playlist
;
3323 snprintf(buf
, buf_size
, "%s", playlist
->filename
);
3331 /* Fills info structure with information about track at specified index.
3332 Returns 0 on success and -1 on failure */
3333 int playlist_get_track_info(struct playlist_info
* playlist
, int index
,
3334 struct playlist_track_info
* info
)
3340 playlist
= ¤t_playlist
;
3342 if (index
< 0 || index
>= playlist
->amount
)
3345 control_file
= playlist
->indices
[index
] & PLAYLIST_INSERT_TYPE_MASK
;
3346 seek
= playlist
->indices
[index
] & PLAYLIST_SEEK_MASK
;
3348 if (get_filename(playlist
, index
, seek
, control_file
, info
->filename
,
3349 sizeof(info
->filename
)) < 0)
3356 if (playlist
->indices
[index
] & PLAYLIST_QUEUE_MASK
)
3357 info
->attr
|= PLAYLIST_ATTR_QUEUED
;
3359 info
->attr
|= PLAYLIST_ATTR_INSERTED
;
3363 if (playlist
->indices
[index
] & PLAYLIST_SKIPPED
)
3364 info
->attr
|= PLAYLIST_ATTR_SKIPPED
;
3366 info
->index
= index
;
3367 info
->display_index
= rotate_index(playlist
, index
) + 1;
3372 /* save the current dynamic playlist to specified file */
3373 int playlist_save(struct playlist_info
* playlist
, char *filename
)
3378 char path
[MAX_PATH
+1];
3379 char tmp_buf
[MAX_PATH
+1];
3381 bool overwrite_current
= false;
3382 int* index_buf
= NULL
;
3385 playlist
= ¤t_playlist
;
3387 if (playlist
->amount
<= 0)
3390 /* use current working directory as base for pathname */
3391 if (format_track_path(path
, filename
, sizeof(tmp_buf
),
3392 strlen(filename
)+1, getcwd(NULL
, -1)) < 0)
3395 if (!strncmp(playlist
->filename
, path
, strlen(path
)))
3397 /* Attempting to overwrite current playlist file.*/
3399 if (playlist
->buffer_size
< (int)(playlist
->amount
* sizeof(int)))
3401 /* not enough buffer space to store updated indices */
3402 gui_syncsplash(HZ
*2, ID2P(LANG_PLAYLIST_ACCESS_ERROR
));
3406 /* in_ram buffer is unused for m3u files so we'll use for storing
3408 index_buf
= (int*)playlist
->buffer
;
3410 /* use temporary pathname */
3411 snprintf(path
, sizeof(path
), "%s_temp", playlist
->filename
);
3412 overwrite_current
= true;
3415 fd
= open(path
, O_CREAT
|O_WRONLY
|O_TRUNC
);
3418 gui_syncsplash(HZ
*2, ID2P(LANG_PLAYLIST_ACCESS_ERROR
));
3422 display_playlist_count(count
, ID2P(LANG_PLAYLIST_SAVE_COUNT
), false);
3428 /* some applications require a BOM to read the file properly */
3429 write(fd
, BOM
, BOM_SIZE
);
3432 index
= playlist
->first_index
;
3433 for (i
=0; i
<playlist
->amount
; i
++)
3440 if (action_userabort(TIMEOUT_NOBLOCK
))
3446 control_file
= playlist
->indices
[index
] & PLAYLIST_INSERT_TYPE_MASK
;
3447 queue
= playlist
->indices
[index
] & PLAYLIST_QUEUE_MASK
;
3448 seek
= playlist
->indices
[index
] & PLAYLIST_SEEK_MASK
;
3450 /* Don't save queued files */
3453 if (get_filename(playlist
, index
, seek
, control_file
, tmp_buf
,
3460 if (overwrite_current
)
3461 index_buf
[count
] = lseek(fd
, 0, SEEK_CUR
);
3463 if (fdprintf(fd
, "%s\n", tmp_buf
) < 0)
3465 gui_syncsplash(HZ
*2, ID2P(LANG_PLAYLIST_ACCESS_ERROR
));
3472 if ((count
% PLAYLIST_DISPLAY_COUNT
) == 0)
3473 display_playlist_count(count
, ID2P(LANG_PLAYLIST_SAVE_COUNT
),
3479 index
= (index
+1)%playlist
->amount
;
3482 display_playlist_count(count
, ID2P(LANG_PLAYLIST_SAVE_COUNT
), true);
3486 if (overwrite_current
&& result
>= 0)
3490 mutex_lock(&playlist
->control_mutex
);
3492 /* Replace the current playlist with the new one and update indices */
3493 close(playlist
->fd
);
3494 if (remove(playlist
->filename
) >= 0)
3496 if (rename(path
, playlist
->filename
) >= 0)
3498 playlist
->fd
= open(playlist
->filename
, O_RDONLY
);
3499 if (playlist
->fd
>= 0)
3501 index
= playlist
->first_index
;
3502 for (i
=0, count
=0; i
<playlist
->amount
; i
++)
3504 if (!(playlist
->indices
[index
] & PLAYLIST_QUEUE_MASK
))
3506 playlist
->indices
[index
] = index_buf
[count
];
3509 index
= (index
+1)%playlist
->amount
;
3512 /* we need to recreate control because inserted tracks are
3513 now part of the playlist and shuffle has been
3515 result
= recreate_control(playlist
);
3520 mutex_unlock(&playlist
->control_mutex
);
3530 * Search specified directory for tracks and notify via callback. May be
3531 * called recursively.
3533 int playlist_directory_tracksearch(const char* dirname
, bool recurse
,
3534 int (*callback
)(char*, void*),
3537 char buf
[MAX_PATH
+1];
3541 struct entry
*files
;
3542 struct tree_context
* tc
= tree_get_context();
3543 int old_dirfilter
= *(tc
->dirfilter
);
3548 /* use the tree browser dircache to load files */
3549 *(tc
->dirfilter
) = SHOW_ALL
;
3551 if (ft_load(tc
, dirname
) < 0)
3553 gui_syncsplash(HZ
*2, ID2P(LANG_PLAYLIST_DIRECTORY_ACCESS_ERROR
));
3554 *(tc
->dirfilter
) = old_dirfilter
;
3558 files
= (struct entry
*) tc
->dircache
;
3559 num_files
= tc
->filesindir
;
3561 /* we've overwritten the dircache so tree browser will need to be
3565 for (i
=0; i
<num_files
; i
++)
3568 if (action_userabort(TIMEOUT_NOBLOCK
))
3574 if (files
[i
].attr
& ATTR_DIRECTORY
)
3578 /* recursively add directories */
3579 snprintf(buf
, sizeof(buf
), "%s/%s", dirname
, files
[i
].name
);
3580 result
= playlist_directory_tracksearch(buf
, recurse
,
3585 /* we now need to reload our current directory */
3586 if(ft_load(tc
, dirname
) < 0)
3592 files
= (struct entry
*) tc
->dircache
;
3593 num_files
= tc
->filesindir
;
3603 else if ((files
[i
].attr
& FILE_ATTR_MASK
) == FILE_ATTR_AUDIO
)
3605 snprintf(buf
, sizeof(buf
), "%s/%s", dirname
, files
[i
].name
);
3607 if (callback(buf
, context
) != 0)
3613 /* let the other threads work */
3618 /* restore dirfilter */
3619 *(tc
->dirfilter
) = old_dirfilter
;