Fix red and yellow. Move resume_index from mp3entry to playlist_info struct. Bump...
[kugel-rb.git] / apps / playlist.c
blob6c1d97a6ef9e9b461d115dff1d5147926e3a6089
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
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
43 occurred.
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
61 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.
70 #include <stdio.h>
71 #include <stdlib.h>
72 #include <ctype.h>
73 #include "string-extra.h"
74 #include "playlist.h"
75 #include "ata_idle_notify.h"
76 #include "file.h"
77 #include "action.h"
78 #include "dir.h"
79 #include "debug.h"
80 #include "audio.h"
81 #include "lcd.h"
82 #include "kernel.h"
83 #include "settings.h"
84 #include "status.h"
85 #include "applimits.h"
86 #include "screens.h"
87 #include "buffer.h"
88 #include "misc.h"
89 #include "filefuncs.h"
90 #include "button.h"
91 #include "filetree.h"
92 #include "abrepeat.h"
93 #include "thread.h"
94 #include "usb.h"
95 #include "filetypes.h"
96 #ifdef HAVE_LCD_BITMAP
97 #include "icons.h"
98 #endif
99 #include "system.h"
101 #include "lang.h"
102 #include "talk.h"
103 #include "splash.h"
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
112 the index.
114 NOTE: This limits the playlist file size to a max of 256M.
116 Bits 31-30:
117 00 = Playlist track
118 01 = Track was prepended into playlist
119 10 = Track was inserted into playlist
120 11 = Track was appended into playlist
121 Bit 29:
122 0 = Added track
123 1 = Queued track
124 Bit 28:
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;
141 int position;
142 bool queue;
143 int count;
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,
150 const char *file);
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,
166 bool write);
167 static int sort_playlist(struct playlist_info* playlist, bool start_current,
168 bool write);
169 static int get_next_index(const struct playlist_info* playlist, int steps,
170 int repeat_mode);
171 static void find_and_set_playlist_index(struct playlist_info* playlist,
172 unsigned int seek);
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,
181 const char *dir);
182 static void display_playlist_count(int count, const unsigned char *fmt,
183 bool final);
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);
192 #ifdef HAVE_DIRCACHE
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";
198 #endif
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
216 * playlist.
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)
225 int i = 0;
226 char* dest;
228 /* Locate EOL. */
229 while ((buf[i] != '\n') && (buf[i] != '\r') && (i < buf_len))
231 i++;
234 /* Work back killing white space. */
235 while ((i > 0) && isspace(buf[i - 1]))
237 i--;
240 buf_len = i;
241 dest = temp;
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
245 * each input char.
247 for (i = 0; i < buf_len && dest < (temp + buf_max - 4); i++)
249 dest = iso_decode(&buf[i], dest, -1, 1);
252 *dest = 0;
253 strcpy(buf, temp);
254 return dest - temp;
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. */
267 close(playlist->fd);
268 playlist->fd = -1;
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;
282 playlist->index = 0;
283 playlist->first_index = 0;
284 playlist->amount = 0;
285 playlist->last_insert_pos = -1;
286 playlist->seed = 0;
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
298 playlist */
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,
308 const char *file)
310 const char *fileused = file;
311 const char *dirused = dir;
312 empty_playlist(playlist, false);
314 if (!fileused)
316 fileused = "";
318 if (dirused && playlist->current) /* !current cannot be in_ram */
319 playlist->in_ram = true;
320 else
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;
352 else
354 playlist->control_created = true;
359 * validate the control file. This may include creating/initializing it if
360 * necessary;
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)
384 return -1;
386 return 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];
395 int temp_fd = -1;
396 int i;
397 int result = 0;
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)
411 return -1;
413 temp_fd = open(temp_file, O_RDONLY);
414 if (temp_fd < 0)
415 return -1;
417 playlist->control_fd = open(playlist->control_filename,
418 O_CREAT|O_RDWR|O_TRUNC, 0666);
419 if (playlist->control_fd < 0)
420 return -1;
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;
430 if (result < 0)
432 close(temp_fd);
433 return result;
437 playlist->seed = 0;
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,
450 SEEK_SET);
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);
455 if (result > 0)
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",
461 inserted_file);
463 playlist->indices[i] =
464 (playlist->indices[i] & ~PLAYLIST_SEEK_MASK) | seek_pos;
467 if (result < 0)
468 break;
470 playlist->num_inserted_tracks++;
474 close(temp_fd);
475 remove(temp_file);
476 fsync(playlist->control_fd);
478 if (result < 0)
479 return result;
481 return 0;
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)
490 char *sep="";
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])
499 sep="/";
500 dirlen++;
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)
515 unsigned int nread;
516 unsigned int i = 0;
517 unsigned int count = 0;
518 bool store_index;
519 unsigned char *p;
520 int result = 0;
522 if(-1 == playlist->fd)
523 playlist->fd = open_utf8(playlist->filename, O_RDONLY);
524 if(playlist->fd < 0)
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));
531 if (!buffer)
533 /* use mp3 buffer for maximum load speed */
534 audio_stop();
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;
539 #else
540 buffer = (char *)audio_get_buffer(false, &buflen);
541 #endif
544 store_index = true;
546 while(1)
548 nread = read(playlist->fd, buffer, buflen);
549 /* Terminate on EOF */
550 if(nread <= 0)
551 break;
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'))
560 store_index = true;
562 else if(store_index)
564 store_index = false;
566 if(*p != '#')
568 if ( playlist->amount >= playlist->max_playlist_size ) {
569 display_buffer_full();
570 result = -1;
571 goto exit;
574 /* Store a new entry */
575 playlist->indices[ playlist->amount ] = i+count;
576 #ifdef HAVE_DIRCACHE
577 if (playlist->filenames)
578 playlist->filenames[ playlist->amount ] = NULL;
579 #endif
580 playlist->amount++;
585 i+= count;
588 exit:
589 #ifdef HAVE_DIRCACHE
590 queue_post(&playlist_queue, PLAYLIST_LOAD_POINTERS, 0);
591 #endif
593 return result;
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];
605 int res;
606 int index = -1;
608 if(direction > 0)
609 res = get_next_directory(dir);
610 else
611 res = get_previous_directory(dir);
613 if (!res)
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;
624 else
625 index = 0;
627 #if (CONFIG_CODEC != SWCODEC)
628 playlist_start(index, 0);
629 #endif
632 /* we've overwritten the dircache when getting the next/previous dir,
633 so the tree browser context will need to be reloaded */
634 reload_directory();
637 return index;
641 * Removes all tracks, from the playlist, leaving the presently playing
642 * track queued.
644 int playlist_remove_all_tracks(struct playlist_info *playlist)
646 int result;
648 if (playlist == NULL)
649 playlist = &current_playlist;
651 while (playlist->index > 0)
652 if ((result = remove_track_from_playlist(playlist, 0, true)) < 0)
653 return result;
655 while (playlist->amount > 1)
656 if ((result = remove_track_from_playlist(playlist, 1, true)) < 0)
657 return result;
659 if (playlist->amount == 1) {
660 playlist->indices[0] |= PLAYLIST_QUEUED;
663 return 0;
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
680 * the playlist.
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;
690 int i;
692 insert_position = orig_position = position;
694 if (playlist->amount >= playlist->max_playlist_size)
696 display_buffer_full();
697 return -1;
700 switch (position)
702 case PLAYLIST_PREPEND:
703 position = insert_position = playlist->first_index;
704 break;
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;
715 else
716 position = insert_position = 0;
718 playlist->last_insert_pos = position;
719 break;
720 case PLAYLIST_INSERT_FIRST:
721 if (playlist->amount > 0)
722 position = insert_position = playlist->index + 1;
723 else
724 position = insert_position = 0;
726 playlist->last_insert_pos = position;
727 break;
728 case PLAYLIST_INSERT_LAST:
729 if (playlist->first_index > 0)
730 position = insert_position = playlist->first_index;
731 else
732 position = insert_position = playlist->amount;
734 playlist->last_insert_pos = position;
735 break;
736 case PLAYLIST_INSERT_SHUFFLED:
738 if (playlist->started)
740 int offset;
741 int n = playlist->amount -
742 rotate_index(playlist, playlist->index);
744 if (n > 0)
745 offset = rand() % n;
746 else
747 offset = 0;
749 position = playlist->index + offset + 1;
750 if (position >= playlist->amount)
751 position -= playlist->amount;
753 insert_position = position;
755 else
756 position = insert_position = (rand() % (playlist->amount+1));
757 break;
759 case PLAYLIST_INSERT_LAST_SHUFFLED:
761 position = insert_position = playlist->last_shuffled_start +
762 rand() % (playlist->amount - playlist->last_shuffled_start + 1);
763 break;
765 case PLAYLIST_REPLACE:
766 if (playlist_remove_all_tracks(playlist) < 0)
767 return -1;
769 playlist->last_insert_pos = position = insert_position = playlist->index + 1;
770 break;
773 if (queue)
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];
780 #ifdef HAVE_DIRCACHE
781 if (playlist->filenames)
782 playlist->filenames[i] = playlist->filenames[i-1];
783 #endif
786 /* update stored indices if needed */
788 if (orig_position < 0)
790 if (playlist->amount > 0 && insert_position <= playlist->index &&
791 playlist->started)
792 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);
809 if (result < 0)
810 return result;
813 playlist->indices[insert_position] = flags | seek_pos;
815 #ifdef HAVE_DIRCACHE
816 if (playlist->filenames)
817 playlist->filenames[insert_position] = NULL;
818 #endif
820 playlist->amount++;
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
831 * playlist.
833 static int directory_search_callback(char* filename, void* context)
835 struct directory_search_context* c =
836 (struct directory_search_context*) context;
837 int insert_pos;
839 insert_pos = add_track_to_playlist(c->playlist, filename, c->position,
840 c->queue, -1);
842 if (insert_pos < 0)
843 return -1;
845 (c->count)++;
847 /* Make sure tracks are inserted in correct order if user requests
848 INSERT_FIRST */
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;
856 if (c->queue)
857 count_str = ID2P(LANG_PLAYLIST_QUEUE_COUNT);
858 else
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();
869 return 0;
873 * remove track at specified position
875 static int remove_track_from_playlist(struct playlist_info* playlist,
876 int position, bool write)
878 int i;
879 bool inserted;
881 if (playlist->amount <= 0)
882 return -1;
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];
890 #ifdef HAVE_DIRCACHE
891 if (playlist->filenames)
892 playlist->filenames[i] = playlist->filenames[i+1];
893 #endif
896 playlist->amount--;
898 if (inserted)
899 playlist->num_inserted_tracks--;
900 else
901 playlist->deleted = true;
903 /* update stored indices if needed */
904 if (position < playlist->index)
905 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);
920 if (result < 0)
921 return result;
923 sync_control(playlist, false);
926 /* Update index for resume. */
927 playlist_update_resume_index();
929 return 0;
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,
938 bool write)
940 int count;
941 int candidate;
942 long store;
943 unsigned int current = playlist->indices[playlist->index];
945 /* seed 0 is used to identify sorted playlist for resume purposes */
946 if (seed == 0)
947 seed = 1;
949 /* seed with the given seed */
950 srand(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;
962 #ifdef HAVE_DIRCACHE
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;
969 #endif
972 if (start_current)
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;
982 if (write)
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();
991 return 0;
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,
1000 bool write)
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);
1014 #endif
1016 if (start_current)
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();
1034 return 0;
1037 /* Calculate how many steps we have to really step when skipping entries
1038 * marked as bad.
1040 static int calculate_step_count(const struct playlist_info *playlist, int steps)
1042 int i, count, direction;
1043 int index;
1044 int stepped_count = 0;
1046 if (steps < 0)
1048 direction = -1;
1049 count = -steps;
1051 else
1053 direction = 1;
1054 count = steps;
1057 index = playlist->index;
1058 i = 0;
1059 do {
1060 /* Boundary check */
1061 if (index < 0)
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)
1069 steps += direction;
1070 /* Are all entries bad? */
1071 if (stepped_count++ > playlist->amount)
1072 break ;
1074 else
1075 i++;
1077 index += direction;
1078 } while (i <= count);
1080 return steps;
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)
1088 int index;
1090 if (playlist == NULL)
1091 playlist = &current_playlist;
1093 /* need to account for already skipped tracks */
1094 steps = calculate_step_count(playlist, steps);
1096 index = playlist->index + steps;
1097 if (index < 0)
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
1107 * track.
1109 static int get_next_index(const struct playlist_info* playlist, int steps,
1110 int repeat_mode)
1112 int current_index = playlist->index;
1113 int next_index = -1;
1115 if (playlist->amount <= 0)
1116 return -1;
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() */
1130 case REPEAT_OFF:
1132 current_index = rotate_index(playlist, current_index);
1133 next_index = current_index+steps;
1134 if ((next_index < 0) || (next_index >= playlist->amount))
1135 next_index = -1;
1136 else
1137 next_index = (next_index+playlist->first_index) %
1138 playlist->amount;
1140 break;
1143 case REPEAT_ONE:
1144 #ifdef AB_REPEAT_ENABLE
1145 case REPEAT_AB:
1146 #endif
1147 next_index = current_index;
1148 break;
1150 case REPEAT_ALL:
1151 default:
1153 next_index = (current_index+steps) % playlist->amount;
1154 while (next_index < 0)
1155 next_index += playlist->amount;
1157 if (steps >= playlist->amount)
1159 int i, index;
1161 index = next_index;
1162 next_index = -1;
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;
1169 else
1171 next_index = index;
1172 break;
1176 break;
1180 /* No luck if the whole playlist was bad. */
1181 if (playlist->indices[next_index] & PLAYLIST_SKIPPED)
1182 return -1;
1184 return next_index;
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,
1192 unsigned int seek)
1194 int i;
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;
1203 break;
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)
1228 return -1;
1229 else if (flags1 == PLAYLIST_INSERT_TYPE_APPEND ||
1230 flags2 == PLAYLIST_INSERT_TYPE_PREPEND)
1231 return 1;
1232 else if (flags1 && flags2)
1233 return (*e1 & PLAYLIST_SEEK_MASK) - (*e2 & PLAYLIST_SEEK_MASK);
1234 else
1235 return *e1 - *e2;
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)
1246 (void)param;
1247 struct playlist_info *playlist;
1248 playlist = &current_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;
1268 int index;
1269 int seek;
1270 bool control_file;
1272 int sleep_time = 5;
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;
1278 #endif
1280 while (1)
1282 queue_wait_w_tmo(&playlist_queue, &ev, HZ*sleep_time);
1284 switch (ev.id)
1286 case PLAYLIST_LOAD_POINTERS:
1287 dirty_pointers = true;
1288 break ;
1290 /* Start the background scanning after either the disk spindown
1291 timeout or 5s, whichever is less */
1292 case SYS_TIMEOUT:
1293 playlist = &current_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)
1301 break ;
1303 if (!dircache_is_enabled() || !playlist->filenames
1304 || playlist->amount <= 0)
1305 break ;
1307 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
1308 cpu_boost(true);
1309 #endif
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])
1315 continue ;
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,
1322 sizeof(tmp)) < 0)
1323 break ;
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. */
1329 yield();
1332 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
1333 cpu_boost(false);
1334 #endif
1335 dirty_pointers = false;
1336 break ;
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);
1342 break ;
1343 #endif
1347 #endif
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)
1355 int fd;
1356 int max = -1;
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);
1373 #else
1374 (void)index;
1375 #endif
1377 if (playlist->in_ram && !control_file && max < 0)
1379 max = strlcpy(tmp_buf, &playlist->buffer[seek], sizeof(tmp_buf));
1381 else if (max < 0)
1383 mutex_lock(playlist->control_mutex);
1385 if (control_file)
1387 fd = playlist->control_fd;
1388 utf8 = true;
1390 else
1392 if(-1 == playlist->fd)
1393 playlist->fd = open(playlist->filename, O_RDONLY);
1395 fd = playlist->fd;
1398 if(-1 != fd)
1401 if (lseek(fd, seek, SEEK_SET) != seek)
1402 max = -1;
1403 else
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);
1419 if (max < 0)
1421 if (control_file)
1422 splash(HZ*2, ID2P(LANG_PLAYLIST_CONTROL_ACCESS_ERROR));
1423 else
1424 splash(HZ*2, ID2P(LANG_PLAYLIST_ACCESS_ERROR));
1426 return max;
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 = &current_playlist;
1450 int result = -1;
1451 char *start_dir = NULL;
1452 bool exit = false;
1453 int i;
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);
1461 if (fd >= 0)
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));
1469 if (!folder_count)
1470 exit = true;
1471 while (!exit)
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)
1477 exit = true;
1479 if (folder_count)
1480 strcpy(dir,buffer);
1481 close(fd);
1482 *(tc->dirfilter) = saved_dirfilter;
1483 tc->sort_dir = global_settings.sort_dir;
1484 reload_directory();
1485 return 0;
1489 /* not random folder advance (or random folder advance unavailable) */
1490 if (recursion)
1492 /* start with root */
1493 dir[0] = '\0';
1495 else
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;
1506 if (!is_forward)
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];
1522 while (!exit)
1524 struct entry *files;
1525 int num_files = 0;
1526 int i;
1528 if (ft_load(tc, (dir[0]=='\0')?"/":dir) < 0)
1530 exit = true;
1531 result = -1;
1532 break;
1535 files = (struct entry*) tc->dircache;
1536 num_files = tc->filesindir;
1538 for (i=0; i<num_files; i++)
1540 /* user abort */
1541 if (action_userabort(TIMEOUT_NOBLOCK))
1543 result = -1;
1544 exit = true;
1545 break;
1548 if (files[i].attr & ATTR_DIRECTORY)
1550 if (!start_dir)
1552 result = check_subdir_for_music(dir, files[i].name, true);
1553 if (result != -1)
1555 exit = true;
1556 break;
1559 else if (!strcmp(start_dir, files[i].name))
1560 start_dir = NULL;
1564 if (!exit)
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, '/');
1569 if (start_dir)
1571 *start_dir = '\0';
1572 start_dir++;
1574 else
1575 break;
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);
1588 return result;
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)
1597 int result = -1;
1598 int dirlen = strlen(dir);
1599 int num_files = 0;
1600 int i;
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)
1610 return -2;
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)
1619 has_subdir = true;
1620 else if ((files[i].attr & FILE_ATTR_MASK) == FILE_ATTR_AUDIO)
1622 has_music = true;
1623 break;
1627 if (has_music)
1628 return 0;
1630 if (has_subdir && recurse)
1632 for (i=0; i<num_files; i++)
1634 if (action_userabort(TIMEOUT_NOBLOCK))
1636 result = -2;
1637 break;
1640 if (files[i].attr & ATTR_DIRECTORY)
1642 result = check_subdir_for_music(dir, files[i].name, true);
1643 if (!result)
1644 break;
1649 if (result < 0)
1651 if (dirlen)
1653 dir[dirlen] = '\0';
1655 else
1657 strcpy(dir, "/");
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));
1664 return result;
1668 * Returns absolute path of track
1670 static int format_track_path(char *dest, char *src, int buf_length, int max,
1671 const char *dir)
1673 int i = 0;
1674 int j;
1675 char *temp_ptr;
1677 /* Look for the end of the string */
1678 while((i < max) &&
1679 (src[i] != '\n') &&
1680 (src[i] != '\r') &&
1681 (src[i] != '\0'))
1682 i++;
1684 /* Now work back killing white space */
1685 while((i > 0) &&
1686 ((src[i-1] == ' ') ||
1687 (src[i-1] == '\t')))
1688 i--;
1690 /* Zero-terminate the file name */
1691 src[i]=0;
1693 /* replace backslashes with forward slashes */
1694 for ( j=0; j<i; j++ )
1695 if ( src[j] == '\\' )
1696 src[j] = '/';
1698 if('/' == src[0])
1700 strlcpy(dest, src, buf_length);
1702 else
1704 /* handle dos style drive letter */
1705 if (':' == src[1])
1706 strlcpy(dest, &src[2], buf_length);
1707 else if (!strncmp(src, "../", 3))
1709 /* handle relative paths */
1710 i=3;
1711 while(!strncmp(&src[i], "../", 3))
1712 i += 3;
1713 for (j=0; j<i/3; j++) {
1714 temp_ptr = strrchr(dir, '/');
1715 if (temp_ptr)
1716 *temp_ptr = '\0';
1717 else
1718 break;
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]);
1725 else {
1726 snprintf(dest, buf_length, "%s/%s", dir, src);
1730 return 0;
1734 * Display splash message showing progress of playlist/directory insertion or
1735 * save.
1737 static void display_playlist_count(int count, const unsigned char *fmt,
1738 bool final)
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);
1749 talk_id(id, true);
1752 fmt = P2STR(fmt);
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)
1771 int result = 0;
1772 int i;
1774 if (!playlist->num_cached)
1775 return 0;
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);
1789 break;
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);
1795 if (result > 0)
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",
1801 cache->s1);
1803 break;
1804 case PLAYLIST_COMMAND_DELETE:
1805 result = fdprintf(playlist->control_fd, "D:%d\n", cache->i1);
1806 break;
1807 case PLAYLIST_COMMAND_SHUFFLE:
1808 result = fdprintf(playlist->control_fd, "S:%d:%d\n",
1809 cache->i1, cache->i2);
1810 break;
1811 case PLAYLIST_COMMAND_UNSHUFFLE:
1812 result = fdprintf(playlist->control_fd, "U:%d\n", cache->i1);
1813 break;
1814 case PLAYLIST_COMMAND_RESET:
1815 result = fdprintf(playlist->control_fd, "R\n");
1816 break;
1817 default:
1818 break;
1821 if (result <= 0)
1822 break;
1825 if (result > 0)
1827 playlist->num_cached = 0;
1828 playlist->pending_control_sync = true;
1830 result = 0;
1832 else
1834 result = -1;
1835 splash(HZ*2, ID2P(LANG_PLAYLIST_CONTROL_UPDATE_ERROR));
1838 return result;
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)
1849 int result = 0;
1850 struct playlist_control_cache* cache;
1851 bool flush = false;
1853 mutex_lock(playlist->control_mutex);
1855 cache = &(playlist->control_cache[playlist->num_cached++]);
1857 cache->command = command;
1858 cache->i1 = i1;
1859 cache->i2 = i2;
1860 cache->s1 = s1;
1861 cache->s2 = s2;
1862 cache->data = data;
1864 switch (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:
1872 #endif
1873 flush = true;
1874 break;
1875 case PLAYLIST_COMMAND_SHUFFLE:
1876 case PLAYLIST_COMMAND_UNSHUFFLE:
1877 default:
1878 /* only flush when needed */
1879 break;
1882 if (flush || playlist->num_cached == PLAYLIST_MAX_CACHE)
1883 result = flush_cached_control(playlist);
1885 mutex_unlock(playlist->control_mutex);
1887 return result;
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)
1897 #else
1898 (void) force;
1900 if (playlist->started)
1901 #endif
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;
1919 if (index < 0)
1920 index += playlist->amount;
1922 return index;
1926 * Initialize playlist entries at startup
1928 void playlist_init(void)
1930 struct playlist_info* playlist = &current_playlist;
1932 mutex_init(&current_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));
1938 playlist->fd = -1;
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 = &current_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)
1957 IF_COP(, CPU));
1958 queue_init(&playlist_queue, true);
1959 #endif
1963 * Clean playlist at shutdown
1965 void playlist_shutdown(void)
1967 struct playlist_info* playlist = &current_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 = &current_playlist;
1989 new_playlist(playlist, dir, file);
1991 if (file)
1992 /* load the playlist file */
1993 add_indices_to_playlist(playlist, NULL, 0);
1995 return 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 = &current_playlist;
2007 char *buffer;
2008 size_t buflen;
2009 int nread;
2010 int total_read = 0;
2011 int control_file_size = 0;
2012 bool first = true;
2013 bool sorted = true;
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;
2020 #else
2021 buffer = (char *)audio_get_buffer(false, &buflen);
2022 #endif
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));
2031 return -1;
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));
2039 return -1;
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);
2045 if(nread <= 0)
2047 splash(HZ*2, ID2P(LANG_PLAYLIST_CONTROL_ACCESS_ERROR));
2048 return -1;
2051 playlist->started = true;
2053 while (1)
2055 int result = 0;
2056 int count;
2057 enum playlist_command current_command = PLAYLIST_COMMAND_COMMENT;
2058 int last_newline = 0;
2059 int str_count = -1;
2060 bool newline = true;
2061 bool exit_loop = false;
2062 char *p = buffer;
2063 char *str1 = NULL;
2064 char *str2 = NULL;
2065 char *str3 = NULL;
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))
2079 useraborted = true;
2080 break;
2082 last_tick = current_tick;
2085 /* Are we on a new line? */
2086 if((*p == '\n') || (*p == '\r'))
2088 *p = '\0';
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 */
2098 int version;
2100 if (!str1)
2102 result = -1;
2103 exit_loop = true;
2104 break;
2107 if (!str2)
2108 str2 = "";
2110 if (!str3)
2111 str3 = "";
2113 version = atoi(str1);
2115 if (version != PLAYLIST_CONTROL_FILE_VERSION)
2116 return -1;
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
2124 data */
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 */
2134 first = false;
2135 exit_loop = true;
2137 break;
2139 case PLAYLIST_COMMAND_ADD:
2140 case PLAYLIST_COMMAND_QUEUE:
2142 /* str1=position str2=last_position str3=file */
2143 int position, last_position;
2144 bool queue;
2146 if (!str1 || !str2 || !str3)
2148 result = -1;
2149 exit_loop = true;
2150 break;
2153 position = atoi(str1);
2154 last_position = atoi(str2);
2156 queue = (current_command == PLAYLIST_COMMAND_ADD)?
2157 false:true;
2159 /* seek position is based on str3's position in
2160 buffer */
2161 if (add_track_to_playlist(playlist, str3, position,
2162 queue, total_read+(str3-buffer)) < 0)
2163 return -1;
2165 playlist->last_insert_pos = last_position;
2167 break;
2169 case PLAYLIST_COMMAND_DELETE:
2171 /* str1=position */
2172 int position;
2174 if (!str1)
2176 result = -1;
2177 exit_loop = true;
2178 break;
2181 position = atoi(str1);
2183 if (remove_track_from_playlist(playlist, position,
2184 false) < 0)
2185 return -1;
2187 break;
2189 case PLAYLIST_COMMAND_SHUFFLE:
2191 /* str1=seed str2=first_index */
2192 int seed;
2194 if (!str1 || !str2)
2196 result = -1;
2197 exit_loop = true;
2198 break;
2201 if (!sorted)
2203 /* Always sort list before shuffling */
2204 sort_playlist(playlist, false, false);
2207 seed = atoi(str1);
2208 playlist->first_index = atoi(str2);
2210 if (randomise_playlist(playlist, seed, false,
2211 false) < 0)
2212 return -1;
2213 sorted = false;
2214 break;
2216 case PLAYLIST_COMMAND_UNSHUFFLE:
2218 /* str1=first_index */
2219 if (!str1)
2221 result = -1;
2222 exit_loop = true;
2223 break;
2226 playlist->first_index = atoi(str1);
2228 if (sort_playlist(playlist, false, false) < 0)
2229 return -1;
2231 sorted = true;
2232 break;
2234 case PLAYLIST_COMMAND_RESET:
2236 playlist->last_insert_pos = -1;
2237 break;
2239 case PLAYLIST_COMMAND_COMMENT:
2240 default:
2241 break;
2244 newline = true;
2246 /* to ignore any extra newlines */
2247 current_command = PLAYLIST_COMMAND_COMMENT;
2249 else if(newline)
2251 newline = false;
2253 /* first non-comment line must always specify playlist */
2254 if (first && *p != 'P' && *p != '#')
2256 result = -1;
2257 exit_loop = true;
2258 break;
2261 switch (*p)
2263 case 'P':
2264 /* playlist can only be specified once */
2265 if (!first)
2267 result = -1;
2268 exit_loop = true;
2269 break;
2272 current_command = PLAYLIST_COMMAND_PLAYLIST;
2273 break;
2274 case 'A':
2275 current_command = PLAYLIST_COMMAND_ADD;
2276 break;
2277 case 'Q':
2278 current_command = PLAYLIST_COMMAND_QUEUE;
2279 break;
2280 case 'D':
2281 current_command = PLAYLIST_COMMAND_DELETE;
2282 break;
2283 case 'S':
2284 current_command = PLAYLIST_COMMAND_SHUFFLE;
2285 break;
2286 case 'U':
2287 current_command = PLAYLIST_COMMAND_UNSHUFFLE;
2288 break;
2289 case 'R':
2290 current_command = PLAYLIST_COMMAND_RESET;
2291 break;
2292 case '#':
2293 current_command = PLAYLIST_COMMAND_COMMENT;
2294 break;
2295 default:
2296 result = -1;
2297 exit_loop = true;
2298 break;
2301 str_count = -1;
2302 str1 = NULL;
2303 str2 = NULL;
2304 str3 = NULL;
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 */
2311 if (*p == ':')
2313 *p = '\0';
2314 str_count++;
2316 if ((count+1) < nread)
2318 switch (str_count)
2320 case 0:
2321 str1 = p+1;
2322 break;
2323 case 1:
2324 str2 = p+1;
2325 break;
2326 case 2:
2327 str3 = p+1;
2328 break;
2329 default:
2330 /* allow last string to contain colons */
2331 *p = ':';
2332 break;
2339 if (result < 0)
2341 splash(HZ*2, ID2P(LANG_PLAYLIST_CONTROL_INVALID));
2342 return result;
2345 if (useraborted)
2347 splash(HZ*2, ID2P(LANG_CANCEL));
2348 return -1;
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));
2356 return -1;
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;
2367 if (first)
2368 /* still looking for header */
2369 nread = read(playlist->control_fd, buffer,
2370 PLAYLIST_COMMAND_SIZE<buflen?PLAYLIST_COMMAND_SIZE:buflen);
2371 else
2372 nread = read(playlist->control_fd, buffer, buflen);
2374 /* Terminate on EOF */
2375 if(nread <= 0)
2377 break;
2381 #ifdef HAVE_DIRCACHE
2382 queue_post(&playlist_queue, PLAYLIST_LOAD_POINTERS, 0);
2383 #endif
2385 return 0;
2389 * Add track to in_ram playlist. Used when playing directories.
2391 int playlist_add(const char *filename)
2393 struct playlist_info* playlist = &current_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();
2400 return -1;
2403 playlist->indices[playlist->amount] = playlist->buffer_end_pos;
2404 #ifdef HAVE_DIRCACHE
2405 playlist->filenames[playlist->amount] = NULL;
2406 #endif
2407 playlist->amount++;
2409 strcpy(&playlist->buffer[playlist->buffer_end_pos], filename);
2410 playlist->buffer_end_pos += len;
2411 playlist->buffer[playlist->buffer_end_pos++] = '\0';
2413 return 0;
2416 /* shuffle newly created playlist using random seed. */
2417 int playlist_shuffle(int random_seed, int start_index)
2419 struct playlist_info* playlist = &current_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 = &current_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 */
2449 #endif
2451 playlist->started = true;
2452 sync_control(playlist, false);
2453 audio_play(offset);
2456 /* Returns false if 'steps' is out of bounds, else true */
2457 bool playlist_check(int steps)
2459 struct playlist_info* playlist = &current_playlist;
2461 /* always allow folder navigation */
2462 if (global_settings.next_folder && playlist->in_ram)
2463 return true;
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 = &current_playlist;
2478 int seek;
2479 char *temp_ptr;
2480 int index;
2481 bool control_file;
2483 index = get_next_index(playlist, steps, -1);
2484 if (index < 0)
2485 return NULL;
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,
2491 buf_size) < 0)
2492 return NULL;
2494 temp_ptr = buf;
2496 if (!playlist->in_ram || control_file)
2498 /* remove bogus dirs from beginning of path
2499 (workaround for buggy playlist creation tools) */
2500 while (temp_ptr)
2502 if (file_exists(temp_ptr))
2503 break;
2505 temp_ptr = strchr(temp_ptr+1, '/');
2508 if (!temp_ptr)
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
2512 of playlist */
2513 return buf;
2517 return temp_ptr;
2521 * Update indices as track has changed
2523 int playlist_next(int steps)
2525 struct playlist_info* playlist = &current_playlist;
2526 int index;
2528 if ( (steps > 0)
2529 #ifdef AB_REPEAT_ENABLE
2530 && (global_settings.repeat_mode != REPEAT_AB)
2531 #endif
2532 && (global_settings.repeat_mode != REPEAT_ONE) )
2534 int i, j;
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);
2551 if (index < 0)
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);
2563 #endif
2564 playlist->index = 0;
2565 index = 0;
2567 else if (playlist->in_ram && global_settings.next_folder)
2569 index = create_and_play_dir(steps, true);
2571 if (index >= 0)
2573 playlist->index = index;
2577 return 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);
2588 if (cur > last_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);
2598 if (result < 0)
2599 return result;
2601 sync_control(playlist, false);
2606 return index;
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)
2614 return false;
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 = &current_playlist;
2625 *resume_index = playlist->index;
2627 return 0;
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 = &current_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 = &current_playlist;
2648 if (id3)
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;
2655 status_save();
2658 else
2660 global_status.resume_index = -1;
2661 global_status.resume_offset = -1;
2662 status_save();
2665 return 0;
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 = &current_playlist;
2675 /* first_index should always be index 0 for display purposes */
2676 int index = rotate_index(playlist, playlist->index);
2678 return (index+1);
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 = &current_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)
2704 if (!playlist)
2705 playlist = &current_playlist;
2706 else
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);
2715 playlist->fd = -1;
2716 playlist->control_fd = -1;
2718 if (index_buffer)
2720 int num_indices = index_buffer_size / sizeof(int);
2722 #ifdef HAVE_DIRCACHE
2723 num_indices /= 2;
2724 #endif
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];
2733 #endif
2735 else
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;
2741 #endif
2744 playlist->buffer_size = 0;
2745 playlist->buffer = NULL;
2746 playlist->control_mutex = &created_playlist_mutex;
2749 new_playlist(playlist, dir, file);
2751 if (file)
2752 /* load the playlist file */
2753 add_indices_to_playlist(playlist, temp_buffer, temp_buffer_size);
2755 return 0;
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))
2767 return -1;
2769 empty_playlist(&current_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)
2782 return -1;
2783 current_playlist.control_fd = open(current_playlist.control_filename,
2784 O_RDWR);
2785 if (current_playlist.control_fd < 0)
2786 return -1;
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));
2798 #endif
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;
2814 return 0;
2816 struct playlist_info *playlist_get_current(void)
2818 return &current_playlist;
2821 * Close files and delete control file for non-current playlist.
2823 void playlist_close(struct playlist_info* playlist)
2825 if (!playlist)
2826 return;
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)
2840 if (!playlist)
2841 playlist = &current_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);
2849 #endif
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)
2859 int result;
2861 if (!playlist)
2862 playlist = &current_playlist;
2864 if (check_control(playlist) < 0)
2866 splash(HZ*2, ID2P(LANG_PLAYLIST_CONTROL_ACCESS_ERROR));
2867 return -1;
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);
2878 return result;
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,
2886 bool recurse)
2888 int result;
2889 unsigned char *count_str;
2890 struct directory_search_context context;
2892 if (!playlist)
2893 playlist = &current_playlist;
2895 if (check_control(playlist) < 0)
2897 splash(HZ*2, ID2P(LANG_PLAYLIST_CONTROL_ACCESS_ERROR));
2898 return -1;
2901 if (position == PLAYLIST_REPLACE)
2903 if (playlist_remove_all_tracks(playlist) == 0)
2904 position = PLAYLIST_INSERT_LAST;
2905 else
2906 return -1;
2909 if (queue)
2910 count_str = ID2P(LANG_PLAYLIST_QUEUE_COUNT);
2911 else
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;
2919 context.count = 0;
2921 cpu_boost(true);
2923 result = playlist_directory_tracksearch(dirname, recurse,
2924 directory_search_callback, &context);
2926 sync_control(playlist, false);
2928 cpu_boost(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);
2937 #endif
2939 return result;
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)
2948 int fd;
2949 int max;
2950 char *temp_ptr;
2951 const char *dir;
2952 unsigned char *count_str;
2953 char temp_buf[MAX_PATH+1];
2954 char trackname[MAX_PATH+1];
2955 int count = 0;
2956 int result = 0;
2957 bool utf8 = is_m3u8(filename);
2959 if (!playlist)
2960 playlist = &current_playlist;
2962 if (check_control(playlist) < 0)
2964 splash(HZ*2, ID2P(LANG_PLAYLIST_CONTROL_ACCESS_ERROR));
2965 return -1;
2968 fd = open_utf8(filename, O_RDONLY);
2969 if (fd < 0)
2971 splash(HZ*2, ID2P(LANG_PLAYLIST_ACCESS_ERROR));
2972 return -1;
2975 /* we need the directory name for formatting purposes */
2976 dir = filename;
2978 temp_ptr = strrchr(filename+1,'/');
2979 if (temp_ptr)
2980 *temp_ptr = 0;
2981 else
2982 dir = "/";
2984 if (queue)
2985 count_str = ID2P(LANG_PLAYLIST_QUEUE_COUNT);
2986 else
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;
2995 else return -1;
2998 cpu_boost(true);
3000 while ((max = read_line(fd, temp_buf, sizeof(temp_buf))) > 0)
3002 /* user abort */
3003 if (action_userabort(TIMEOUT_NOBLOCK))
3004 break;
3006 if (temp_buf[0] != '#' && temp_buf[0] != '\0')
3008 int insert_pos;
3010 if (!utf8)
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
3019 handled */
3020 if (format_track_path(trackname, temp_buf, sizeof(trackname), max,
3021 dir) < 0)
3023 result = -1;
3024 break;
3027 insert_pos = add_track_to_playlist(playlist, trackname, position,
3028 queue, -1);
3030 if (insert_pos < 0)
3032 result = -1;
3033 break;
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;
3041 count++;
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) &&
3049 playlist->started)
3050 audio_flush_and_reload_tracks();
3054 /* let the other threads work */
3055 yield();
3058 close(fd);
3060 if (temp_ptr)
3061 *temp_ptr = '/';
3063 sync_control(playlist, false);
3065 cpu_boost(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);
3074 #endif
3076 return result;
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)
3085 int result = 0;
3087 if (!playlist)
3088 playlist = &current_playlist;
3090 if (check_control(playlist) < 0)
3092 splash(HZ*2, ID2P(LANG_PLAYLIST_CONTROL_ACCESS_ERROR));
3093 return -1;
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) &&
3102 playlist->started)
3103 audio_flush_and_reload_tracks();
3105 return result;
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)
3114 int result;
3115 int seek;
3116 bool control_file;
3117 bool queue;
3118 bool current = false;
3119 int r;
3120 char filename[MAX_PATH];
3122 if (!playlist)
3123 playlist = &current_playlist;
3125 if (check_control(playlist) < 0)
3127 splash(HZ*2, ID2P(LANG_PLAYLIST_CONTROL_ACCESS_ERROR));
3128 return -1;
3131 if (index == new_index)
3132 return -1;
3134 if (index == playlist->index)
3135 /* Moving the current track */
3136 current = true;
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)
3144 return -1;
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);
3156 if (result != -1)
3158 if (r == 0)
3159 /* First index */
3160 new_index = PLAYLIST_PREPEND;
3161 else if (r == playlist->amount)
3162 /* Append */
3163 new_index = PLAYLIST_INSERT_LAST;
3164 else
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,
3169 -1);
3171 if (result != -1)
3173 if (current)
3175 /* Moved the current track */
3176 switch (new_index)
3178 case PLAYLIST_PREPEND:
3179 playlist->index = playlist->first_index;
3180 break;
3181 case PLAYLIST_INSERT_LAST:
3182 playlist->index = playlist->first_index - 1;
3183 if (playlist->index < 0)
3184 playlist->index += playlist->amount;
3185 break;
3186 default:
3187 playlist->index = new_index;
3188 break;
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);
3199 #endif
3201 /* Update index for resume. */
3202 playlist_update_resume_index();
3204 return result;
3207 /* shuffle currently playing playlist */
3208 int playlist_randomise(struct playlist_info* playlist, unsigned int seed,
3209 bool start_current)
3211 int result;
3213 if (!playlist)
3214 playlist = &current_playlist;
3216 check_control(playlist);
3218 result = randomise_playlist(playlist, seed, start_current, true);
3220 if (result != -1 && (audio_status() & AUDIO_STATUS_PLAY) &&
3221 playlist->started)
3222 audio_flush_and_reload_tracks();
3224 return result;
3227 /* sort currently playing playlist */
3228 int playlist_sort(struct playlist_info* playlist, bool start_current)
3230 int result;
3232 if (!playlist)
3233 playlist = &current_playlist;
3235 check_control(playlist);
3237 result = sort_playlist(playlist, start_current, true);
3239 if (result != -1 && (audio_status() & AUDIO_STATUS_PLAY) &&
3240 playlist->started)
3241 audio_flush_and_reload_tracks();
3243 return result;
3246 /* returns true if playlist has been modified */
3247 bool playlist_modified(const struct playlist_info* playlist)
3249 if (!playlist)
3250 playlist = &current_playlist;
3252 if (playlist->shuffle_modified ||
3253 playlist->deleted ||
3254 playlist->num_inserted_tracks > 0)
3255 return true;
3257 return false;
3260 /* returns index of first track in playlist */
3261 int playlist_get_first_index(const struct playlist_info* playlist)
3263 if (!playlist)
3264 playlist = &current_playlist;
3266 return playlist->first_index;
3269 /* returns shuffle seed of playlist */
3270 int playlist_get_seed(const struct playlist_info* playlist)
3272 if (!playlist)
3273 playlist = &current_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)
3281 if (!playlist)
3282 playlist = &current_playlist;
3284 return playlist->amount;
3287 /* returns full path of playlist (minus extension) */
3288 char *playlist_name(const struct playlist_info* playlist, char *buf,
3289 int buf_size)
3291 char *sep;
3293 if (!playlist)
3294 playlist = &current_playlist;
3296 strlcpy(buf, playlist->filename+playlist->dirlen, buf_size);
3298 if (!buf[0])
3299 return NULL;
3301 /* Remove extension */
3302 sep = strrchr(buf, '.');
3303 if (sep)
3304 *sep = 0;
3306 return buf;
3309 /* returns the playlist filename */
3310 char *playlist_get_name(const struct playlist_info* playlist, char *buf,
3311 int buf_size)
3313 if (!playlist)
3314 playlist = &current_playlist;
3316 strlcpy(buf, playlist->filename, buf_size);
3318 if (!buf[0])
3319 return NULL;
3321 return buf;
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)
3329 int seek;
3330 bool control_file;
3332 if (!playlist)
3333 playlist = &current_playlist;
3335 if (index < 0 || index >= playlist->amount)
3336 return -1;
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)
3343 return -1;
3345 info->attr = 0;
3347 if (control_file)
3349 if (playlist->indices[index] & PLAYLIST_QUEUE_MASK)
3350 info->attr |= PLAYLIST_ATTR_QUEUED;
3351 else
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;
3362 return 0;
3365 /* save the current dynamic playlist to specified file */
3366 int playlist_save(struct playlist_info* playlist, char *filename)
3368 int fd;
3369 int i, index;
3370 int count = 0;
3371 char path[MAX_PATH+1];
3372 char tmp_buf[MAX_PATH+1];
3373 int result = 0;
3374 bool overwrite_current = false;
3375 int* index_buf = NULL;
3377 if (!playlist)
3378 playlist = &current_playlist;
3380 if (playlist->amount <= 0)
3381 return -1;
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)
3386 return -1;
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));
3396 return -1;
3399 /* in_ram buffer is unused for m3u files so we'll use for storing
3400 updated indices */
3401 index_buf = (int*)playlist->buffer;
3403 /* use temporary pathname */
3404 snprintf(path, sizeof(path), "%s_temp", playlist->filename);
3405 overwrite_current = true;
3408 if (is_m3u8(path))
3410 fd = open_utf8(path, O_CREAT|O_WRONLY|O_TRUNC);
3412 else
3414 /* some applications require a BOM to read the file properly */
3415 fd = open(path, O_CREAT|O_WRONLY|O_TRUNC, 0666);
3417 if (fd < 0)
3419 splash(HZ*2, ID2P(LANG_PLAYLIST_ACCESS_ERROR));
3420 return -1;
3423 display_playlist_count(count, ID2P(LANG_PLAYLIST_SAVE_COUNT), false);
3425 cpu_boost(true);
3427 index = playlist->first_index;
3428 for (i=0; i<playlist->amount; i++)
3430 bool control_file;
3431 bool queue;
3432 int seek;
3434 /* user abort */
3435 if (action_userabort(TIMEOUT_NOBLOCK))
3437 result = -1;
3438 break;
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 */
3446 if (!queue)
3448 if (get_filename(playlist, index, seek, control_file, tmp_buf,
3449 MAX_PATH+1) < 0)
3451 result = -1;
3452 break;
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));
3461 result = -1;
3462 break;
3465 count++;
3467 if ((count % PLAYLIST_DISPLAY_COUNT) == 0)
3468 display_playlist_count(count, ID2P(LANG_PLAYLIST_SAVE_COUNT),
3469 false);
3471 yield();
3474 index = (index+1)%playlist->amount;
3477 display_playlist_count(count, ID2P(LANG_PLAYLIST_SAVE_COUNT), true);
3479 close(fd);
3481 if (overwrite_current && result >= 0)
3483 result = -1;
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];
3502 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
3509 invalidated */
3510 result = recreate_control(playlist);
3515 mutex_unlock(playlist->control_mutex);
3519 cpu_boost(false);
3521 return result;
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*),
3530 void* context)
3532 char buf[MAX_PATH+1];
3533 int result = 0;
3534 int num_files = 0;
3535 int i;
3536 struct entry *files;
3537 struct tree_context* tc = tree_get_context();
3538 int old_dirfilter = *(tc->dirfilter);
3540 if (!callback)
3541 return -1;
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;
3550 return -1;
3553 files = (struct entry*) tc->dircache;
3554 num_files = tc->filesindir;
3556 /* we've overwritten the dircache so tree browser will need to be
3557 reloaded */
3558 reload_directory();
3560 for (i=0; i<num_files; i++)
3562 /* user abort */
3563 if (action_userabort(TIMEOUT_NOBLOCK))
3565 result = -1;
3566 break;
3569 if (files[i].attr & ATTR_DIRECTORY)
3571 if (recurse)
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,
3577 callback, context);
3578 if (result < 0)
3579 break;
3581 /* we now need to reload our current directory */
3582 if(ft_load(tc, dirname) < 0)
3584 result = -1;
3585 break;
3588 files = (struct entry*) tc->dircache;
3589 num_files = tc->filesindir;
3590 if (!num_files)
3592 result = -1;
3593 break;
3596 else
3597 continue;
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)
3606 result = -1;
3607 break;
3610 /* let the other threads work */
3611 yield();
3615 /* restore dirfilter */
3616 *(tc->dirfilter) = old_dirfilter;
3618 return result;