handle new installations (or upgrades) differently from invalid configurations
[kugel-rb.git] / apps / playlist.c
blob53becb9fc1563d01a17a808d2ae6b112ab3fec2f
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 <string.h>
73 #include <ctype.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 "sprintf.h"
80 #include "debug.h"
81 #include "audio.h"
82 #include "lcd.h"
83 #include "kernel.h"
84 #include "settings.h"
85 #include "status.h"
86 #include "applimits.h"
87 #include "screens.h"
88 #include "buffer.h"
89 #include "misc.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
100 #include "lang.h"
101 #include "talk.h"
102 #include "splash.h"
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
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 #define PLAYLIST_DISPLAY_COUNT 10
141 struct directory_search_context {
142 struct playlist_info* playlist;
143 int position;
144 bool queue;
145 int count;
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,
153 const char *file);
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,
169 bool write);
170 static int sort_playlist(struct playlist_info* playlist, bool start_current,
171 bool write);
172 static int get_next_index(const struct playlist_info* playlist, int steps,
173 int repeat_mode);
174 static void find_and_set_playlist_index(struct playlist_info* playlist,
175 unsigned int seek);
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, bool recurse);
183 static int format_track_path(char *dest, char *src, int buf_length, int max,
184 const char *dir);
185 static void display_playlist_count(int count, const unsigned char *fmt,
186 bool final);
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);
195 #ifdef HAVE_DIRCACHE
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";
201 #endif
203 #define BOM "\xef\xbb\xbf"
204 #define BOM_SIZE 3
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
224 * playlist.
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)
233 int i = 0;
234 char* dest;
236 /* Locate EOL. */
237 while ((buf[i] != '\n') && (buf[i] != '\r') && (i < buf_len))
239 i++;
242 /* Work back killing white space. */
243 while ((i > 0) && isspace(buf[i - 1]))
245 i--;
248 buf_len = i;
249 dest = temp;
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
253 * each input char.
255 for (i = 0; i < buf_len && dest < (temp + buf_max - 4); i++)
257 dest = iso_decode(&buf[i], dest, -1, 1);
260 *dest = 0;
261 strcpy(buf, temp);
262 return dest - temp;
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. */
275 close(playlist->fd);
276 playlist->fd = -1;
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;
290 playlist->index = 0;
291 playlist->first_index = 0;
292 playlist->amount = 0;
293 playlist->last_insert_pos = -1;
294 playlist->seed = 0;
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
306 playlist */
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,
320 const char *file)
322 const char *fileused = file;
323 const char *dirused = dir;
324 empty_playlist(playlist, false);
326 if (!fileused)
328 fileused = "";
330 if (dirused && playlist->current) /* !current cannot be in_ram */
331 playlist->in_ram = true;
332 else
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;
364 else
366 playlist->control_created = true;
371 * validate the control file. This may include creating/initializing it if
372 * necessary;
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)
396 return -1;
398 return 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];
407 int temp_fd = -1;
408 int i;
409 int result = 0;
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)
423 return -1;
425 temp_fd = open(temp_file, O_RDONLY);
426 if (temp_fd < 0)
427 return -1;
429 playlist->control_fd = open(playlist->control_filename,
430 O_CREAT|O_RDWR|O_TRUNC);
431 if (playlist->control_fd < 0)
432 return -1;
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;
442 if (result < 0)
444 close(temp_fd);
445 return result;
449 playlist->seed = 0;
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;
457 status_save();
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,
468 SEEK_SET);
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);
473 if (result > 0)
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",
479 inserted_file);
481 playlist->indices[i] =
482 (playlist->indices[i] & ~PLAYLIST_SEEK_MASK) | seek_pos;
485 if (result < 0)
486 break;
488 playlist->num_inserted_tracks++;
492 close(temp_fd);
493 remove(temp_file);
494 fsync(playlist->control_fd);
496 if (result < 0)
497 return result;
499 return 0;
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)
508 char *sep="";
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])
517 sep="/";
518 dirlen++;
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)
533 unsigned int nread;
534 unsigned int i = 0;
535 unsigned int count = 0;
536 bool store_index;
537 unsigned char *p;
538 int result = 0;
540 if(-1 == playlist->fd)
541 playlist->fd = open(playlist->filename, O_RDONLY);
542 if(playlist->fd < 0)
543 return -1; /* failure */
545 gui_syncsplash(0, ID2P(LANG_WAIT));
547 if (!buffer)
549 /* use mp3 buffer for maximum load speed */
550 audio_stop();
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;
555 #else
556 buffer = (char *)audio_get_buffer(false, &buflen);
557 #endif
560 store_index = true;
562 while(1)
564 nread = read(playlist->fd, buffer, buflen);
565 /* Terminate on EOF */
566 if(nread <= 0)
567 break;
569 p = (unsigned char *)buffer;
571 /* utf8 BOM at beginning of file? */
572 if(i == 0 && is_utf8_bom(p, nread)) {
573 nread -= BOM_SIZE;
574 p += BOM_SIZE;
575 i += BOM_SIZE;
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'))
584 store_index = true;
586 else if(store_index)
588 store_index = false;
590 if(*p != '#')
592 if ( playlist->amount >= playlist->max_playlist_size ) {
593 display_buffer_full();
594 result = -1;
595 goto exit;
598 /* Store a new entry */
599 playlist->indices[ playlist->amount ] = i+count;
600 #ifdef HAVE_DIRCACHE
601 if (playlist->filenames)
602 playlist->filenames[ playlist->amount ] = NULL;
603 #endif
604 playlist->amount++;
609 i+= count;
612 exit:
613 #ifdef HAVE_DIRCACHE
614 queue_post(&playlist_queue, PLAYLIST_LOAD_POINTERS, 0);
615 #endif
617 return result;
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];
629 int res;
630 int index = -1;
632 if(direction > 0)
633 res = get_next_directory(dir);
634 else
635 res = get_previous_directory(dir);
637 if (!res)
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;
648 else
649 index = 0;
651 #if (CONFIG_CODEC != SWCODEC)
652 playlist_start(index, 0);
653 #endif
656 /* we've overwritten the dircache when getting the next/previous dir,
657 so the tree browser context will need to be reloaded */
658 reload_directory();
661 return index;
665 * Removes all tracks, from the playlist, leaving the presently playing
666 * track queued.
668 int playlist_remove_all_tracks(struct playlist_info *playlist)
670 int result;
672 if (playlist == NULL)
673 playlist = &current_playlist;
675 while (playlist->index > 0)
676 if ((result = remove_track_from_playlist(playlist, 0, true)) < 0)
677 return result;
679 while (playlist->amount > 1)
680 if ((result = remove_track_from_playlist(playlist, 1, true)) < 0)
681 return result;
683 if (playlist->amount == 1) {
684 playlist->indices[0] |= PLAYLIST_QUEUED;
687 return 0;
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;
712 int i;
714 insert_position = orig_position = position;
716 if (playlist->amount >= playlist->max_playlist_size)
718 display_buffer_full();
719 return -1;
722 switch (position)
724 case PLAYLIST_PREPEND:
725 position = insert_position = playlist->first_index;
726 break;
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;
737 else
738 position = insert_position = 0;
740 if (playlist->started)
741 playlist->last_insert_pos = position;
742 break;
743 case PLAYLIST_INSERT_FIRST:
744 if (playlist->amount > 0)
745 position = insert_position = playlist->index + 1;
746 else
747 position = insert_position = 0;
749 if (playlist->last_insert_pos < 0 && playlist->started)
750 playlist->last_insert_pos = position;
751 break;
752 case PLAYLIST_INSERT_LAST:
753 if (playlist->first_index > 0)
754 position = insert_position = playlist->first_index;
755 else
756 position = insert_position = playlist->amount;
757 break;
758 case PLAYLIST_INSERT_SHUFFLED:
760 if (playlist->started)
762 int offset;
763 int n = playlist->amount -
764 rotate_index(playlist, playlist->index);
766 if (n > 0)
767 offset = rand() % n;
768 else
769 offset = 0;
771 position = playlist->index + offset + 1;
772 if (position >= playlist->amount)
773 position -= playlist->amount;
775 insert_position = position;
777 else
778 position = insert_position = (rand() % (playlist->amount+1));
779 break;
781 case PLAYLIST_REPLACE:
782 if (playlist_remove_all_tracks(playlist) < 0)
783 return -1;
785 position = insert_position = playlist->index + 1;
786 break;
789 if (queue)
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];
796 #ifdef HAVE_DIRCACHE
797 if (playlist->filenames)
798 playlist->filenames[i] = playlist->filenames[i-1];
799 #endif
802 /* update stored indices if needed */
803 if (playlist->amount > 0 && insert_position <= playlist->index &&
804 playlist->started)
805 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;
815 status_save();
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);
829 if (result < 0)
830 return result;
833 playlist->indices[insert_position] = flags | seek_pos;
835 #ifdef HAVE_DIRCACHE
836 if (playlist->filenames)
837 playlist->filenames[insert_position] = NULL;
838 #endif
840 playlist->amount++;
841 playlist->num_inserted_tracks++;
843 return insert_position;
847 * Callback for playlist_directory_tracksearch to insert track into
848 * playlist.
850 static int directory_search_callback(char* filename, void* context)
852 struct directory_search_context* c =
853 (struct directory_search_context*) context;
854 int insert_pos;
856 insert_pos = add_track_to_playlist(c->playlist, filename, c->position,
857 c->queue, -1);
859 if (insert_pos < 0)
860 return -1;
862 (c->count)++;
864 /* Make sure tracks are inserted in correct order if user requests
865 INSERT_FIRST */
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;
873 if (c->queue)
874 count_str = ID2P(LANG_PLAYLIST_QUEUE_COUNT);
875 else
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();
886 return 0;
890 * remove track at specified position
892 static int remove_track_from_playlist(struct playlist_info* playlist,
893 int position, bool write)
895 int i;
896 bool inserted;
898 if (playlist->amount <= 0)
899 return -1;
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];
907 #ifdef HAVE_DIRCACHE
908 if (playlist->filenames)
909 playlist->filenames[i] = playlist->filenames[i+1];
910 #endif
913 playlist->amount--;
915 if (inserted)
916 playlist->num_inserted_tracks--;
917 else
918 playlist->deleted = true;
920 /* update stored indices if needed */
921 if (position < playlist->index)
922 playlist->index--;
924 if (position < playlist->first_index)
926 playlist->first_index--;
928 if (write)
930 global_status.resume_first_index = playlist->first_index;
931 status_save();
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);
943 if (result < 0)
944 return result;
946 sync_control(playlist, false);
949 return 0;
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,
958 bool write)
960 int count;
961 int candidate;
962 long store;
963 unsigned int current = playlist->indices[playlist->index];
965 /* seed 0 is used to identify sorted playlist for resume purposes */
966 if (seed == 0)
967 seed = 1;
969 /* seed with the given seed */
970 srand(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;
982 #ifdef HAVE_DIRCACHE
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;
989 #endif
992 if (start_current)
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;
1002 if (write)
1004 update_control(playlist, PLAYLIST_COMMAND_SHUFFLE, seed,
1005 playlist->first_index, NULL, NULL, NULL);
1006 global_status.resume_seed = seed;
1007 status_save();
1010 return 0;
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,
1018 bool write)
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);
1032 #endif
1034 if (start_current)
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;
1047 status_save();
1050 return 0;
1053 /* Calculate how many steps we have to really step when skipping entries
1054 * marked as bad.
1056 static int calculate_step_count(const struct playlist_info *playlist, int steps)
1058 int i, count, direction;
1059 int index;
1060 int stepped_count = 0;
1062 if (steps < 0)
1064 direction = -1;
1065 count = -steps;
1067 else
1069 direction = 1;
1070 count = steps;
1073 index = playlist->index;
1074 i = 0;
1075 do {
1076 /* Boundary check */
1077 if (index < 0)
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)
1085 steps += direction;
1086 /* Are all entries bad? */
1087 if (stepped_count++ > playlist->amount)
1088 break ;
1090 else
1091 i++;
1093 index += direction;
1094 } while (i <= count);
1096 return steps;
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)
1104 int index;
1106 if (playlist == NULL)
1107 playlist = &current_playlist;
1109 /* need to account for already skipped tracks */
1110 steps = calculate_step_count(playlist, steps);
1112 index = playlist->index + steps;
1113 if (index < 0)
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
1123 * track.
1125 static int get_next_index(const struct playlist_info* playlist, int steps,
1126 int repeat_mode)
1128 int current_index = playlist->index;
1129 int next_index = -1;
1131 if (playlist->amount <= 0)
1132 return -1;
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() */
1146 case REPEAT_OFF:
1148 current_index = rotate_index(playlist, current_index);
1149 next_index = current_index+steps;
1150 if ((next_index < 0) || (next_index >= playlist->amount))
1151 next_index = -1;
1152 else
1153 next_index = (next_index+playlist->first_index) %
1154 playlist->amount;
1156 break;
1159 case REPEAT_ONE:
1160 #ifdef AB_REPEAT_ENABLE
1161 case REPEAT_AB:
1162 #endif
1163 next_index = current_index;
1164 break;
1166 case REPEAT_ALL:
1167 default:
1169 next_index = (current_index+steps) % playlist->amount;
1170 while (next_index < 0)
1171 next_index += playlist->amount;
1173 if (steps >= playlist->amount)
1175 int i, index;
1177 index = next_index;
1178 next_index = -1;
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;
1185 else
1187 next_index = index;
1188 break;
1192 break;
1196 /* No luck if the whole playlist was bad. */
1197 if (playlist->indices[next_index] & PLAYLIST_SKIPPED)
1198 return -1;
1200 return next_index;
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,
1208 unsigned int seek)
1210 int i;
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;
1222 status_save();
1225 break;
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)
1247 return -1;
1248 else if (flags1 == PLAYLIST_INSERT_TYPE_APPEND ||
1249 flags2 == PLAYLIST_INSERT_TYPE_PREPEND)
1250 return 1;
1251 else if (flags1 && flags2)
1252 return (*e1 & PLAYLIST_SEEK_MASK) - (*e2 & PLAYLIST_SEEK_MASK);
1253 else
1254 return *e1 - *e2;
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 = &current_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);
1277 return 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;
1287 int index;
1288 int seek;
1289 bool control_file;
1291 int sleep_time = 5;
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;
1297 #endif
1299 while (1)
1301 queue_wait_w_tmo(&playlist_queue, &ev, HZ*sleep_time);
1303 switch (ev.id)
1305 case PLAYLIST_LOAD_POINTERS:
1306 dirty_pointers = true;
1307 break ;
1309 /* Start the background scanning after either the disk spindown
1310 timeout or 5s, whichever is less */
1311 case SYS_TIMEOUT:
1312 playlist = &current_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)
1320 break ;
1322 if (!dircache_is_enabled() || !playlist->filenames
1323 || playlist->amount <= 0)
1324 break ;
1326 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
1327 cpu_boost(true);
1328 #endif
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])
1334 continue ;
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,
1341 sizeof(tmp)) < 0)
1342 break ;
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. */
1348 yield();
1351 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
1352 cpu_boost(false);
1353 #endif
1354 dirty_pointers = false;
1355 break ;
1357 #ifndef SIMULATOR
1358 case SYS_USB_CONNECTED:
1359 usb_acknowledge(SYS_USB_CONNECTED_ACK);
1360 usb_wait_for_disconnect(&playlist_queue);
1361 break ;
1362 #endif
1366 #endif
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)
1374 int fd;
1375 int max = -1;
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;
1392 #else
1393 (void)index;
1394 #endif
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;
1402 else if (max < 0)
1404 mutex_lock(&playlist->control_mutex);
1406 if (control_file)
1408 fd = playlist->control_fd;
1409 utf8 = true;
1411 else
1413 if(-1 == playlist->fd)
1414 playlist->fd = open(playlist->filename, O_RDONLY);
1416 fd = playlist->fd;
1419 if(-1 != fd)
1422 if (lseek(fd, seek, SEEK_SET) != seek)
1423 max = -1;
1424 else
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);
1440 if (max < 0)
1442 if (control_file)
1443 gui_syncsplash(HZ*2, ID2P(LANG_PLAYLIST_CONTROL_ACCESS_ERROR));
1444 else
1445 gui_syncsplash(HZ*2, ID2P(LANG_PLAYLIST_ACCESS_ERROR));
1447 return max;
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 = &current_playlist;
1472 int result = -1;
1473 int sort_dir = global_settings.sort_dir;
1474 char *start_dir = NULL;
1475 bool exit = false;
1476 struct tree_context* tc = tree_get_context();
1477 int dirfilter = *(tc->dirfilter);
1478 if (global_settings.next_folder == FOLDER_ADVANCE_RANDOM)
1480 int fd = open(ROCKBOX_DIR "/folder_advance_list.dat",O_RDONLY);
1481 char buffer[MAX_PATH];
1482 int folder_count = 0,i;
1483 srand(current_tick);
1484 *(tc->dirfilter) = SHOW_MUSIC;
1485 if (fd >= 0)
1487 read(fd,&folder_count,sizeof(int));
1488 if (!folder_count)
1489 exit = true;
1490 while (!exit)
1492 i = rand()%folder_count;
1493 lseek(fd,sizeof(int) + (MAX_PATH*i),SEEK_SET);
1494 read(fd,buffer,MAX_PATH);
1495 if (check_subdir_for_music(buffer, "", false) ==0)
1496 exit = true;
1498 if (folder_count)
1499 strcpy(dir,buffer);
1500 close(fd);
1501 *(tc->dirfilter) = dirfilter;
1502 reload_directory();
1503 return 0;
1506 /* not random folder advance */
1507 if (recursion){
1508 /* start with root */
1509 dir[0] = '\0';
1511 else{
1512 /* start with current directory */
1513 strncpy(dir, playlist->filename, playlist->dirlen-1);
1514 dir[playlist->dirlen-1] = '\0';
1517 /* use the tree browser dircache to load files */
1518 *(tc->dirfilter) = SHOW_ALL;
1520 /* sort in another direction if previous dir is requested */
1521 if(!is_forward){
1522 if ((global_settings.sort_dir == 0) || (global_settings.sort_dir == 3))
1523 global_settings.sort_dir = 4;
1524 else if (global_settings.sort_dir == 1)
1525 global_settings.sort_dir = 2;
1526 else if (global_settings.sort_dir == 2)
1527 global_settings.sort_dir = 1;
1528 else if (global_settings.sort_dir == 4)
1529 global_settings.sort_dir = 0;
1532 while (!exit)
1534 struct entry *files;
1535 int num_files = 0;
1536 int i;
1538 if (ft_load(tc, (dir[0]=='\0')?"/":dir) < 0)
1540 gui_syncsplash(HZ*2, ID2P(LANG_PLAYLIST_DIRECTORY_ACCESS_ERROR));
1541 exit = true;
1542 result = -1;
1543 break;
1546 files = (struct entry*) tc->dircache;
1547 num_files = tc->filesindir;
1549 for (i=0; i<num_files; i++)
1551 /* user abort */
1552 if (action_userabort(TIMEOUT_NOBLOCK))
1554 result = -1;
1555 exit = true;
1556 break;
1559 if (files[i].attr & ATTR_DIRECTORY)
1561 if (!start_dir)
1563 result = check_subdir_for_music(dir, files[i].name, true);
1564 if (result != -1)
1566 exit = true;
1567 break;
1570 else if (!strcmp(start_dir, files[i].name))
1571 start_dir = NULL;
1575 if (!exit)
1577 /* move down to parent directory. current directory name is
1578 stored as the starting point for the search in parent */
1579 start_dir = strrchr(dir, '/');
1580 if (start_dir)
1582 *start_dir = '\0';
1583 start_dir++;
1585 else
1586 break;
1590 /* restore dirfilter & sort_dir */
1591 *(tc->dirfilter) = dirfilter;
1592 global_settings.sort_dir = sort_dir;
1594 /* special case if nothing found: try start searching again from root */
1595 if (result == -1 && !recursion){
1596 result = get_next_dir(dir,is_forward, true);
1599 return result;
1603 * Checks if there are any music files in the dir or any of its
1604 * subdirectories. May be called recursively.
1606 static int check_subdir_for_music(char *dir, char *subdir, bool recurse)
1608 int result = -1;
1609 int dirlen = strlen(dir);
1610 int num_files = 0;
1611 int i;
1612 struct entry *files;
1613 bool has_music = false;
1614 bool has_subdir = false;
1615 struct tree_context* tc = tree_get_context();
1617 snprintf(dir+dirlen, MAX_PATH-dirlen, "/%s", subdir);
1619 if (ft_load(tc, dir) < 0)
1621 gui_syncsplash(HZ*2, ID2P(LANG_PLAYLIST_DIRECTORY_ACCESS_ERROR));
1622 return -2;
1625 files = (struct entry*) tc->dircache;
1626 num_files = tc->filesindir;
1628 for (i=0; i<num_files; i++)
1630 if (files[i].attr & ATTR_DIRECTORY)
1631 has_subdir = true;
1632 else if ((files[i].attr & FILE_ATTR_MASK) == FILE_ATTR_AUDIO)
1634 has_music = true;
1635 break;
1639 if (has_music)
1640 return 0;
1642 if (has_subdir && recurse)
1644 for (i=0; i<num_files; i++)
1646 if (action_userabort(TIMEOUT_NOBLOCK))
1648 result = -2;
1649 break;
1652 if (files[i].attr & ATTR_DIRECTORY)
1654 result = check_subdir_for_music(dir, files[i].name, true);
1655 if (!result)
1656 break;
1661 if (result < 0)
1663 if (dirlen)
1665 dir[dirlen] = '\0';
1667 else
1669 strcpy(dir, "/");
1672 /* we now need to reload our current directory */
1673 if(ft_load(tc, dir) < 0)
1674 gui_syncsplash(HZ*2,
1675 ID2P(LANG_PLAYLIST_DIRECTORY_ACCESS_ERROR));
1677 return result;
1681 * Returns absolute path of track
1683 static int format_track_path(char *dest, char *src, int buf_length, int max,
1684 const char *dir)
1686 int i = 0;
1687 int j;
1688 char *temp_ptr;
1690 /* Zero-terminate the file name */
1691 while((src[i] != '\n') &&
1692 (src[i] != '\r') &&
1693 (i < max))
1694 i++;
1696 /* Now work back killing white space */
1697 while((src[i-1] == ' ') ||
1698 (src[i-1] == '\t'))
1699 i--;
1701 src[i]=0;
1703 /* replace backslashes with forward slashes */
1704 for ( j=0; j<i; j++ )
1705 if ( src[j] == '\\' )
1706 src[j] = '/';
1708 if('/' == src[0])
1710 strncpy(dest, src, buf_length);
1712 else
1714 /* handle dos style drive letter */
1715 if (':' == src[1])
1716 strncpy(dest, &src[2], buf_length);
1717 else if (!strncmp(src, "../", 3))
1719 /* handle relative paths */
1720 i=3;
1721 while(!strncmp(&src[i], "../", 3))
1722 i += 3;
1723 for (j=0; j<i/3; j++) {
1724 temp_ptr = strrchr(dir, '/');
1725 if (temp_ptr)
1726 *temp_ptr = '\0';
1727 else
1728 break;
1730 snprintf(dest, buf_length, "%s/%s", dir, &src[i]);
1732 else if ( '.' == src[0] && '/' == src[1] ) {
1733 snprintf(dest, buf_length, "%s/%s", dir, &src[2]);
1735 else {
1736 snprintf(dest, buf_length, "%s/%s", dir, src);
1740 return 0;
1744 * Display splash message showing progress of playlist/directory insertion or
1745 * save.
1747 static void display_playlist_count(int count, const unsigned char *fmt,
1748 bool final)
1750 static long talked_tick = 0;
1751 long id = P2ID(fmt);
1752 if(global_settings.talk_menu && id>=0)
1754 if(final || (count && (talked_tick == 0
1755 || TIME_AFTER(current_tick, talked_tick+5*HZ))))
1757 talked_tick = current_tick;
1758 talk_number(count, false);
1759 talk_id(id, true);
1762 fmt = P2STR(fmt);
1764 gui_syncsplash(0, fmt, count, str(LANG_OFF_ABORT));
1768 * Display buffer full message
1770 static void display_buffer_full(void)
1772 gui_syncsplash(HZ*2, ID2P(LANG_PLAYLIST_BUFFER_FULL));
1776 * Flush any cached control commands to disk. Called when playlist is being
1777 * modified. Returns 0 on success and -1 on failure.
1779 static int flush_cached_control(struct playlist_info* playlist)
1781 int result = 0;
1782 int i;
1784 if (!playlist->num_cached)
1785 return 0;
1787 lseek(playlist->control_fd, 0, SEEK_END);
1789 for (i=0; i<playlist->num_cached; i++)
1791 struct playlist_control_cache* cache =
1792 &(playlist->control_cache[i]);
1794 switch (cache->command)
1796 case PLAYLIST_COMMAND_PLAYLIST:
1797 result = fdprintf(playlist->control_fd, "P:%d:%s:%s\n",
1798 cache->i1, cache->s1, cache->s2);
1799 break;
1800 case PLAYLIST_COMMAND_ADD:
1801 case PLAYLIST_COMMAND_QUEUE:
1802 result = fdprintf(playlist->control_fd, "%c:%d:%d:",
1803 (cache->command == PLAYLIST_COMMAND_ADD)?'A':'Q',
1804 cache->i1, cache->i2);
1805 if (result > 0)
1807 /* save the position in file where name is written */
1808 int* seek_pos = (int *)cache->data;
1809 *seek_pos = lseek(playlist->control_fd, 0, SEEK_CUR);
1810 result = fdprintf(playlist->control_fd, "%s\n",
1811 cache->s1);
1813 break;
1814 case PLAYLIST_COMMAND_DELETE:
1815 result = fdprintf(playlist->control_fd, "D:%d\n", cache->i1);
1816 break;
1817 case PLAYLIST_COMMAND_SHUFFLE:
1818 result = fdprintf(playlist->control_fd, "S:%d:%d\n",
1819 cache->i1, cache->i2);
1820 break;
1821 case PLAYLIST_COMMAND_UNSHUFFLE:
1822 result = fdprintf(playlist->control_fd, "U:%d\n", cache->i1);
1823 break;
1824 case PLAYLIST_COMMAND_RESET:
1825 result = fdprintf(playlist->control_fd, "R\n");
1826 break;
1827 default:
1828 break;
1831 if (result <= 0)
1832 break;
1835 if (result > 0)
1837 if (global_status.resume_seed >= 0)
1839 global_status.resume_seed = -1;
1840 status_save();
1843 playlist->num_cached = 0;
1844 playlist->pending_control_sync = true;
1846 result = 0;
1848 else
1850 result = -1;
1851 gui_syncsplash(HZ*2, ID2P(LANG_PLAYLIST_CONTROL_UPDATE_ERROR));
1854 return result;
1858 * Update control data with new command. Depending on the command, it may be
1859 * cached or flushed to disk.
1861 static int update_control(struct playlist_info* playlist,
1862 enum playlist_command command, int i1, int i2,
1863 const char* s1, const char* s2, void* data)
1865 int result = 0;
1866 struct playlist_control_cache* cache;
1867 bool flush = false;
1869 mutex_lock(&playlist->control_mutex);
1871 cache = &(playlist->control_cache[playlist->num_cached++]);
1873 cache->command = command;
1874 cache->i1 = i1;
1875 cache->i2 = i2;
1876 cache->s1 = s1;
1877 cache->s2 = s2;
1878 cache->data = data;
1880 switch (command)
1882 case PLAYLIST_COMMAND_PLAYLIST:
1883 case PLAYLIST_COMMAND_ADD:
1884 case PLAYLIST_COMMAND_QUEUE:
1885 #ifndef HAVE_DIRCACHE
1886 case PLAYLIST_COMMAND_DELETE:
1887 case PLAYLIST_COMMAND_RESET:
1888 #endif
1889 flush = true;
1890 break;
1891 case PLAYLIST_COMMAND_SHUFFLE:
1892 case PLAYLIST_COMMAND_UNSHUFFLE:
1893 default:
1894 /* only flush when needed */
1895 break;
1898 if (flush || playlist->num_cached == PLAYLIST_MAX_CACHE)
1899 result = flush_cached_control(playlist);
1901 mutex_unlock(&playlist->control_mutex);
1903 return result;
1907 * sync control file to disk
1909 static void sync_control(struct playlist_info* playlist, bool force)
1911 #ifdef HAVE_DIRCACHE
1912 if (playlist->started && force)
1913 #else
1914 (void) force;
1916 if (playlist->started)
1917 #endif
1919 if (playlist->pending_control_sync)
1921 mutex_lock(&playlist->control_mutex);
1922 fsync(playlist->control_fd);
1923 playlist->pending_control_sync = false;
1924 mutex_unlock(&playlist->control_mutex);
1930 * Rotate indices such that first_index is index 0
1932 static int rotate_index(const struct playlist_info* playlist, int index)
1934 index -= playlist->first_index;
1935 if (index < 0)
1936 index += playlist->amount;
1938 return index;
1942 * Initialize playlist entries at startup
1944 void playlist_init(void)
1946 struct playlist_info* playlist = &current_playlist;
1948 playlist->current = true;
1949 snprintf(playlist->control_filename, sizeof(playlist->control_filename),
1950 "%s", PLAYLIST_CONTROL_FILE);
1951 playlist->fd = -1;
1952 playlist->control_fd = -1;
1953 playlist->max_playlist_size = global_settings.max_files_in_playlist;
1954 playlist->indices = buffer_alloc(
1955 playlist->max_playlist_size * sizeof(int));
1956 playlist->buffer_size =
1957 AVERAGE_FILENAME_LENGTH * global_settings.max_files_in_dir;
1958 playlist->buffer = buffer_alloc(playlist->buffer_size);
1959 mutex_init(&playlist->control_mutex);
1960 empty_playlist(playlist, true);
1962 #ifdef HAVE_DIRCACHE
1963 playlist->filenames = buffer_alloc(
1964 playlist->max_playlist_size * sizeof(int));
1965 memset(playlist->filenames, 0,
1966 playlist->max_playlist_size * sizeof(int));
1967 create_thread(playlist_thread, playlist_stack, sizeof(playlist_stack),
1968 0, playlist_thread_name IF_PRIO(, PRIORITY_BACKGROUND)
1969 IF_COP(, CPU));
1970 queue_init(&playlist_queue, true);
1971 #endif
1975 * Clean playlist at shutdown
1977 void playlist_shutdown(void)
1979 struct playlist_info* playlist = &current_playlist;
1981 if (playlist->control_fd >= 0)
1983 mutex_lock(&playlist->control_mutex);
1985 if (playlist->num_cached > 0)
1986 flush_cached_control(playlist);
1988 close(playlist->control_fd);
1990 mutex_unlock(&playlist->control_mutex);
1995 * Create new playlist
1997 int playlist_create(const char *dir, const char *file)
1999 struct playlist_info* playlist = &current_playlist;
2001 new_playlist(playlist, dir, file);
2003 if (file)
2004 /* load the playlist file */
2005 add_indices_to_playlist(playlist, NULL, 0);
2007 return 0;
2010 #define PLAYLIST_COMMAND_SIZE (MAX_PATH+12)
2013 * Restore the playlist state based on control file commands. Called to
2014 * resume playback after shutdown.
2016 int playlist_resume(void)
2018 struct playlist_info* playlist = &current_playlist;
2019 char *buffer;
2020 size_t buflen;
2021 int nread;
2022 int total_read = 0;
2023 int control_file_size = 0;
2024 bool first = true;
2025 bool sorted = true;
2027 /* use mp3 buffer for maximum load speed */
2028 #if CONFIG_CODEC != SWCODEC
2029 talk_buffer_steal(); /* we use the mp3 buffer, need to tell */
2030 buflen = (audiobufend - audiobuf);
2031 buffer = (char *)audiobuf;
2032 #else
2033 buffer = (char *)audio_get_buffer(false, &buflen);
2034 #endif
2036 empty_playlist(playlist, true);
2038 gui_syncsplash(0, ID2P(LANG_WAIT));
2039 playlist->control_fd = open(playlist->control_filename, O_RDWR);
2040 if (playlist->control_fd < 0)
2042 gui_syncsplash(HZ*2, ID2P(LANG_PLAYLIST_CONTROL_ACCESS_ERROR));
2043 return -1;
2045 playlist->control_created = true;
2047 control_file_size = filesize(playlist->control_fd);
2048 if (control_file_size <= 0)
2050 gui_syncsplash(HZ*2, ID2P(LANG_PLAYLIST_CONTROL_ACCESS_ERROR));
2051 return -1;
2054 /* read a small amount first to get the header */
2055 nread = read(playlist->control_fd, buffer,
2056 PLAYLIST_COMMAND_SIZE<buflen?PLAYLIST_COMMAND_SIZE:buflen);
2057 if(nread <= 0)
2059 gui_syncsplash(HZ*2, ID2P(LANG_PLAYLIST_CONTROL_ACCESS_ERROR));
2060 return -1;
2063 playlist->started = true;
2065 while (1)
2067 int result = 0;
2068 int count;
2069 enum playlist_command current_command = PLAYLIST_COMMAND_COMMENT;
2070 int last_newline = 0;
2071 int str_count = -1;
2072 bool newline = true;
2073 bool exit_loop = false;
2074 char *p = buffer;
2075 char *str1 = NULL;
2076 char *str2 = NULL;
2077 char *str3 = NULL;
2078 unsigned long last_tick = current_tick;
2080 for(count=0; count<nread && !exit_loop; count++,p++)
2082 /* So a splash while we are loading. */
2083 if (current_tick - last_tick > HZ/4)
2085 gui_syncsplash(0, str(LANG_LOADING_PERCENT),
2086 (total_read+count)*100/control_file_size,
2087 str(LANG_OFF_ABORT));
2088 if (action_userabort(TIMEOUT_NOBLOCK))
2090 /* FIXME:
2091 * Not sure how to implement this, somebody more familiar
2092 * with the code, please fix this. */
2094 last_tick = current_tick;
2097 /* Are we on a new line? */
2098 if((*p == '\n') || (*p == '\r'))
2100 *p = '\0';
2102 /* save last_newline in case we need to load more data */
2103 last_newline = count;
2105 switch (current_command)
2107 case PLAYLIST_COMMAND_PLAYLIST:
2109 /* str1=version str2=dir str3=file */
2110 int version;
2112 if (!str1)
2114 result = -1;
2115 exit_loop = true;
2116 break;
2119 if (!str2)
2120 str2 = "";
2122 if (!str3)
2123 str3 = "";
2125 version = atoi(str1);
2127 if (version != PLAYLIST_CONTROL_FILE_VERSION)
2128 return -1;
2130 update_playlist_filename(playlist, str2, str3);
2132 if (str3[0] != '\0')
2134 /* NOTE: add_indices_to_playlist() overwrites the
2135 audiobuf so we need to reload control file
2136 data */
2137 add_indices_to_playlist(playlist, NULL, 0);
2139 else if (str2[0] != '\0')
2141 playlist->in_ram = true;
2142 resume_directory(str2);
2145 /* load the rest of the data */
2146 first = false;
2147 exit_loop = true;
2149 break;
2151 case PLAYLIST_COMMAND_ADD:
2152 case PLAYLIST_COMMAND_QUEUE:
2154 /* str1=position str2=last_position str3=file */
2155 int position, last_position;
2156 bool queue;
2158 if (!str1 || !str2 || !str3)
2160 result = -1;
2161 exit_loop = true;
2162 break;
2165 position = atoi(str1);
2166 last_position = atoi(str2);
2168 queue = (current_command == PLAYLIST_COMMAND_ADD)?
2169 false:true;
2171 /* seek position is based on str3's position in
2172 buffer */
2173 if (add_track_to_playlist(playlist, str3, position,
2174 queue, total_read+(str3-buffer)) < 0)
2175 return -1;
2177 playlist->last_insert_pos = last_position;
2179 break;
2181 case PLAYLIST_COMMAND_DELETE:
2183 /* str1=position */
2184 int position;
2186 if (!str1)
2188 result = -1;
2189 exit_loop = true;
2190 break;
2193 position = atoi(str1);
2195 if (remove_track_from_playlist(playlist, position,
2196 false) < 0)
2197 return -1;
2199 break;
2201 case PLAYLIST_COMMAND_SHUFFLE:
2203 /* str1=seed str2=first_index */
2204 int seed;
2206 if (!str1 || !str2)
2208 result = -1;
2209 exit_loop = true;
2210 break;
2213 if (!sorted)
2215 /* Always sort list before shuffling */
2216 sort_playlist(playlist, false, false);
2219 seed = atoi(str1);
2220 playlist->first_index = atoi(str2);
2222 if (randomise_playlist(playlist, seed, false,
2223 false) < 0)
2224 return -1;
2226 sorted = false;
2227 break;
2229 case PLAYLIST_COMMAND_UNSHUFFLE:
2231 /* str1=first_index */
2232 if (!str1)
2234 result = -1;
2235 exit_loop = true;
2236 break;
2239 playlist->first_index = atoi(str1);
2241 if (sort_playlist(playlist, false, false) < 0)
2242 return -1;
2244 sorted = true;
2245 break;
2247 case PLAYLIST_COMMAND_RESET:
2249 playlist->last_insert_pos = -1;
2250 break;
2252 case PLAYLIST_COMMAND_COMMENT:
2253 default:
2254 break;
2257 newline = true;
2259 /* to ignore any extra newlines */
2260 current_command = PLAYLIST_COMMAND_COMMENT;
2262 else if(newline)
2264 newline = false;
2266 /* first non-comment line must always specify playlist */
2267 if (first && *p != 'P' && *p != '#')
2269 result = -1;
2270 exit_loop = true;
2271 break;
2274 switch (*p)
2276 case 'P':
2277 /* playlist can only be specified once */
2278 if (!first)
2280 result = -1;
2281 exit_loop = true;
2282 break;
2285 current_command = PLAYLIST_COMMAND_PLAYLIST;
2286 break;
2287 case 'A':
2288 current_command = PLAYLIST_COMMAND_ADD;
2289 break;
2290 case 'Q':
2291 current_command = PLAYLIST_COMMAND_QUEUE;
2292 break;
2293 case 'D':
2294 current_command = PLAYLIST_COMMAND_DELETE;
2295 break;
2296 case 'S':
2297 current_command = PLAYLIST_COMMAND_SHUFFLE;
2298 break;
2299 case 'U':
2300 current_command = PLAYLIST_COMMAND_UNSHUFFLE;
2301 break;
2302 case 'R':
2303 current_command = PLAYLIST_COMMAND_RESET;
2304 break;
2305 case '#':
2306 current_command = PLAYLIST_COMMAND_COMMENT;
2307 break;
2308 default:
2309 result = -1;
2310 exit_loop = true;
2311 break;
2314 str_count = -1;
2315 str1 = NULL;
2316 str2 = NULL;
2317 str3 = NULL;
2319 else if(current_command != PLAYLIST_COMMAND_COMMENT)
2321 /* all control file strings are separated with a colon.
2322 Replace the colon with 0 to get proper strings that can be
2323 used by commands above */
2324 if (*p == ':')
2326 *p = '\0';
2327 str_count++;
2329 if ((count+1) < nread)
2331 switch (str_count)
2333 case 0:
2334 str1 = p+1;
2335 break;
2336 case 1:
2337 str2 = p+1;
2338 break;
2339 case 2:
2340 str3 = p+1;
2341 break;
2342 default:
2343 /* allow last string to contain colons */
2344 *p = ':';
2345 break;
2352 if (result < 0)
2354 gui_syncsplash(HZ*2, ID2P(LANG_PLAYLIST_CONTROL_INVALID));
2355 return result;
2358 if (!newline || (exit_loop && count<nread))
2360 if ((total_read + count) >= control_file_size)
2362 /* no newline at end of control file */
2363 gui_syncsplash(HZ*2, ID2P(LANG_PLAYLIST_CONTROL_INVALID));
2364 return -1;
2367 /* We didn't end on a newline or we exited loop prematurely.
2368 Either way, re-read the remainder. */
2369 count = last_newline;
2370 lseek(playlist->control_fd, total_read+count, SEEK_SET);
2373 total_read += count;
2375 if (first)
2376 /* still looking for header */
2377 nread = read(playlist->control_fd, buffer,
2378 PLAYLIST_COMMAND_SIZE<buflen?PLAYLIST_COMMAND_SIZE:buflen);
2379 else
2380 nread = read(playlist->control_fd, buffer, buflen);
2382 /* Terminate on EOF */
2383 if(nread <= 0)
2385 if (global_status.resume_seed >= 0)
2387 /* Apply shuffle command saved in settings */
2388 if (global_status.resume_seed == 0)
2389 sort_playlist(playlist, false, true);
2390 else
2392 if (!sorted)
2393 sort_playlist(playlist, false, false);
2395 randomise_playlist(playlist, global_status.resume_seed,
2396 false, true);
2400 playlist->first_index = global_status.resume_first_index;
2401 break;
2405 #ifdef HAVE_DIRCACHE
2406 queue_post(&playlist_queue, PLAYLIST_LOAD_POINTERS, 0);
2407 #endif
2409 return 0;
2413 * Add track to in_ram playlist. Used when playing directories.
2415 int playlist_add(const char *filename)
2417 struct playlist_info* playlist = &current_playlist;
2418 int len = strlen(filename);
2420 if((len+1 > playlist->buffer_size - playlist->buffer_end_pos) ||
2421 (playlist->amount >= playlist->max_playlist_size))
2423 display_buffer_full();
2424 return -1;
2427 playlist->indices[playlist->amount] = playlist->buffer_end_pos;
2428 #ifdef HAVE_DIRCACHE
2429 playlist->filenames[playlist->amount] = NULL;
2430 #endif
2431 playlist->amount++;
2433 strcpy(&playlist->buffer[playlist->buffer_end_pos], filename);
2434 playlist->buffer_end_pos += len;
2435 playlist->buffer[playlist->buffer_end_pos++] = '\0';
2437 return 0;
2440 /* shuffle newly created playlist using random seed. */
2441 int playlist_shuffle(int random_seed, int start_index)
2443 struct playlist_info* playlist = &current_playlist;
2445 unsigned int seek_pos = 0;
2446 bool start_current = false;
2448 if (start_index >= 0 && global_settings.play_selected)
2450 /* store the seek position before the shuffle */
2451 seek_pos = playlist->indices[start_index];
2452 playlist->index = global_status.resume_first_index =
2453 playlist->first_index = start_index;
2454 start_current = true;
2457 randomise_playlist(playlist, random_seed, start_current, true);
2459 return playlist->index;
2462 /* start playing current playlist at specified index/offset */
2463 int playlist_start(int start_index, int offset)
2465 struct playlist_info* playlist = &current_playlist;
2467 /* Cancel FM radio selection as previous music. For cases where we start
2468 playback without going to the WPS, such as playlist insert.. or
2469 playlist catalog. */
2470 previous_music_is_wps();
2472 playlist->index = start_index;
2474 #if CONFIG_CODEC != SWCODEC
2475 talk_buffer_steal(); /* will use the mp3 buffer */
2476 #endif
2478 playlist->started = true;
2479 sync_control(playlist, false);
2480 audio_play(offset);
2482 return 0;
2485 /* Returns false if 'steps' is out of bounds, else true */
2486 bool playlist_check(int steps)
2488 struct playlist_info* playlist = &current_playlist;
2490 /* always allow folder navigation */
2491 if (global_settings.next_folder && playlist->in_ram)
2492 return true;
2494 int index = get_next_index(playlist, steps, -1);
2496 if (index < 0 && steps >= 0 && global_settings.repeat_mode == REPEAT_SHUFFLE)
2497 index = get_next_index(playlist, steps, REPEAT_ALL);
2499 return (index >= 0);
2502 /* get trackname of track that is "steps" away from current playing track.
2503 NULL is used to identify end of playlist */
2504 char* playlist_peek(int steps)
2506 struct playlist_info* playlist = &current_playlist;
2507 int seek;
2508 char *temp_ptr;
2509 int index;
2510 bool control_file;
2512 index = get_next_index(playlist, steps, -1);
2513 if (index < 0)
2514 return NULL;
2516 control_file = playlist->indices[index] & PLAYLIST_INSERT_TYPE_MASK;
2517 seek = playlist->indices[index] & PLAYLIST_SEEK_MASK;
2519 if (get_filename(playlist, index, seek, control_file, now_playing,
2520 MAX_PATH+1) < 0)
2521 return NULL;
2523 temp_ptr = now_playing;
2525 if (!playlist->in_ram || control_file)
2527 /* remove bogus dirs from beginning of path
2528 (workaround for buggy playlist creation tools) */
2529 while (temp_ptr)
2531 if (file_exists(temp_ptr))
2532 break;
2534 temp_ptr = strchr(temp_ptr+1, '/');
2537 if (!temp_ptr)
2539 /* Even though this is an invalid file, we still need to pass a
2540 file name to the caller because NULL is used to indicate end
2541 of playlist */
2542 return now_playing;
2546 return temp_ptr;
2550 * Update indices as track has changed
2552 int playlist_next(int steps)
2554 struct playlist_info* playlist = &current_playlist;
2555 int index;
2557 if ( (steps > 0)
2558 #ifdef AB_REPEAT_ENABLE
2559 && (global_settings.repeat_mode != REPEAT_AB)
2560 #endif
2561 && (global_settings.repeat_mode != REPEAT_ONE) )
2563 int i, j;
2565 /* We need to delete all the queued songs */
2566 for (i=0, j=steps; i<j; i++)
2568 index = get_next_index(playlist, i, -1);
2570 if (playlist->indices[index] & PLAYLIST_QUEUE_MASK)
2572 remove_track_from_playlist(playlist, index, true);
2573 steps--; /* one less track */
2578 index = get_next_index(playlist, steps, -1);
2580 if (index < 0)
2582 /* end of playlist... or is it */
2583 if (global_settings.repeat_mode == REPEAT_SHUFFLE &&
2584 playlist->amount > 1)
2586 /* Repeat shuffle mode. Re-shuffle playlist and resume play */
2587 playlist->first_index = global_status.resume_first_index = 0;
2588 sort_playlist(playlist, false, false);
2589 randomise_playlist(playlist, current_tick, false, true);
2590 #if CONFIG_CODEC != SWCODEC
2591 playlist_start(0, 0);
2592 #endif
2593 playlist->index = 0;
2594 index = 0;
2596 else if (playlist->in_ram && global_settings.next_folder)
2598 index = create_and_play_dir(steps, true);
2600 if (index >= 0)
2602 playlist->index = index;
2606 return index;
2609 playlist->index = index;
2611 if (playlist->last_insert_pos >= 0 && steps > 0)
2613 /* check to see if we've gone beyond the last inserted track */
2614 int cur = rotate_index(playlist, index);
2615 int last_pos = rotate_index(playlist, playlist->last_insert_pos);
2617 if (cur > last_pos)
2619 /* reset last inserted track */
2620 playlist->last_insert_pos = -1;
2622 if (playlist->control_fd >= 0)
2624 int result = update_control(playlist, PLAYLIST_COMMAND_RESET,
2625 -1, -1, NULL, NULL, NULL);
2627 if (result < 0)
2628 return result;
2630 sync_control(playlist, false);
2635 return index;
2638 /* try playing next or previous folder */
2639 bool playlist_next_dir(int direction)
2641 /* not to mess up real playlists */
2642 if(!current_playlist.in_ram)
2643 return false;
2645 return create_and_play_dir(direction, false) >= 0;
2648 /* Get resume info for current playing song. If return value is -1 then
2649 settings shouldn't be saved. */
2650 int playlist_get_resume_info(int *resume_index)
2652 struct playlist_info* playlist = &current_playlist;
2654 *resume_index = playlist->index;
2656 return 0;
2659 /* Update resume info for current playing song. Returns -1 on error. */
2660 int playlist_update_resume_info(const struct mp3entry* id3)
2662 struct playlist_info* playlist = &current_playlist;
2664 if (id3)
2666 if (global_status.resume_index != playlist->index ||
2667 global_status.resume_offset != id3->offset)
2669 global_status.resume_index = playlist->index;
2670 global_status.resume_offset = id3->offset;
2671 status_save();
2674 else
2676 global_status.resume_index = -1;
2677 global_status.resume_offset = -1;
2678 status_save();
2681 return 0;
2684 /* Returns index of current playing track for display purposes. This value
2685 should not be used for resume purposes as it doesn't represent the actual
2686 index into the playlist */
2687 int playlist_get_display_index(void)
2689 struct playlist_info* playlist = &current_playlist;
2691 /* first_index should always be index 0 for display purposes */
2692 int index = rotate_index(playlist, playlist->index);
2694 return (index+1);
2697 /* returns number of tracks in current playlist */
2698 int playlist_amount(void)
2700 return playlist_amount_ex(NULL);
2704 * Create a new playlist If playlist is not NULL then we're loading a
2705 * playlist off disk for viewing/editing. The index_buffer is used to store
2706 * playlist indices (required for and only used if !current playlist). The
2707 * temp_buffer (if not NULL) is used as a scratchpad when loading indices.
2709 int playlist_create_ex(struct playlist_info* playlist,
2710 const char* dir, const char* file,
2711 void* index_buffer, int index_buffer_size,
2712 void* temp_buffer, int temp_buffer_size)
2714 if (!playlist)
2715 playlist = &current_playlist;
2716 else
2718 /* Initialize playlist structure */
2719 int r = rand() % 10;
2720 playlist->current = false;
2722 /* Use random name for control file */
2723 snprintf(playlist->control_filename, sizeof(playlist->control_filename),
2724 "%s.%d", PLAYLIST_CONTROL_FILE, r);
2725 playlist->fd = -1;
2726 playlist->control_fd = -1;
2728 if (index_buffer)
2730 int num_indices = index_buffer_size / sizeof(int);
2732 #ifdef HAVE_DIRCACHE
2733 num_indices /= 2;
2734 #endif
2735 if (num_indices > global_settings.max_files_in_playlist)
2736 num_indices = global_settings.max_files_in_playlist;
2738 playlist->max_playlist_size = num_indices;
2739 playlist->indices = index_buffer;
2740 #ifdef HAVE_DIRCACHE
2741 playlist->filenames = (const struct dircache_entry **)
2742 &playlist->indices[num_indices];
2743 #endif
2745 else
2747 playlist->max_playlist_size = current_playlist.max_playlist_size;
2748 playlist->indices = current_playlist.indices;
2749 #ifdef HAVE_DIRCACHE
2750 playlist->filenames = current_playlist.filenames;
2751 #endif
2754 playlist->buffer_size = 0;
2755 playlist->buffer = NULL;
2756 mutex_init(&playlist->control_mutex);
2759 new_playlist(playlist, dir, file);
2761 if (file)
2762 /* load the playlist file */
2763 add_indices_to_playlist(playlist, temp_buffer, temp_buffer_size);
2765 return 0;
2769 * Set the specified playlist as the current.
2770 * NOTE: You will get undefined behaviour if something is already playing so
2771 * remember to stop before calling this. Also, this call will
2772 * effectively close your playlist, making it unusable.
2774 int playlist_set_current(struct playlist_info* playlist)
2776 if (!playlist || (check_control(playlist) < 0))
2777 return -1;
2779 empty_playlist(&current_playlist, false);
2781 strncpy(current_playlist.filename, playlist->filename,
2782 sizeof(current_playlist.filename));
2784 current_playlist.utf8 = playlist->utf8;
2785 current_playlist.fd = playlist->fd;
2787 close(playlist->control_fd);
2788 close(current_playlist.control_fd);
2789 remove(current_playlist.control_filename);
2790 if (rename(playlist->control_filename,
2791 current_playlist.control_filename) < 0)
2792 return -1;
2793 current_playlist.control_fd = open(current_playlist.control_filename,
2794 O_RDWR);
2795 if (current_playlist.control_fd < 0)
2796 return -1;
2797 current_playlist.control_created = true;
2799 current_playlist.dirlen = playlist->dirlen;
2801 if (playlist->indices && playlist->indices != current_playlist.indices)
2803 memcpy(current_playlist.indices, playlist->indices,
2804 playlist->max_playlist_size*sizeof(int));
2805 #ifdef HAVE_DIRCACHE
2806 memcpy(current_playlist.filenames, playlist->filenames,
2807 playlist->max_playlist_size*sizeof(int));
2808 #endif
2811 current_playlist.first_index = playlist->first_index;
2812 current_playlist.amount = playlist->amount;
2813 current_playlist.last_insert_pos = playlist->last_insert_pos;
2814 current_playlist.seed = playlist->seed;
2815 current_playlist.shuffle_modified = playlist->shuffle_modified;
2816 current_playlist.deleted = playlist->deleted;
2817 current_playlist.num_inserted_tracks = playlist->num_inserted_tracks;
2819 memcpy(current_playlist.control_cache, playlist->control_cache,
2820 sizeof(current_playlist.control_cache));
2821 current_playlist.num_cached = playlist->num_cached;
2822 current_playlist.pending_control_sync = playlist->pending_control_sync;
2824 return 0;
2828 * Close files and delete control file for non-current playlist.
2830 void playlist_close(struct playlist_info* playlist)
2832 if (!playlist)
2833 return;
2835 if (playlist->fd >= 0)
2836 close(playlist->fd);
2838 if (playlist->control_fd >= 0)
2839 close(playlist->control_fd);
2841 if (playlist->control_created)
2842 remove(playlist->control_filename);
2845 void playlist_sync(struct playlist_info* playlist)
2847 if (!playlist)
2848 playlist = &current_playlist;
2850 sync_control(playlist, false);
2851 if ((audio_status() & AUDIO_STATUS_PLAY) && playlist->started)
2852 audio_flush_and_reload_tracks();
2854 #ifdef HAVE_DIRCACHE
2855 queue_post(&playlist_queue, PLAYLIST_LOAD_POINTERS, 0);
2856 #endif
2860 * Insert track into playlist at specified position (or one of the special
2861 * positions). Returns position where track was inserted or -1 if error.
2863 int playlist_insert_track(struct playlist_info* playlist, const char *filename,
2864 int position, bool queue, bool sync)
2866 int result;
2868 if (!playlist)
2869 playlist = &current_playlist;
2871 if (check_control(playlist) < 0)
2873 gui_syncsplash(HZ*2, ID2P(LANG_PLAYLIST_CONTROL_ACCESS_ERROR));
2874 return -1;
2877 result = add_track_to_playlist(playlist, filename, position, queue, -1);
2879 /* Check if we want manually sync later. For example when adding
2880 * bunch of files from tagcache, syncing after every file wouldn't be
2881 * a good thing to do. */
2882 if (sync && result >= 0)
2883 playlist_sync(playlist);
2885 return result;
2889 * Insert all tracks from specified directory into playlist.
2891 int playlist_insert_directory(struct playlist_info* playlist,
2892 const char *dirname, int position, bool queue,
2893 bool recurse)
2895 int result;
2896 unsigned char *count_str;
2897 struct directory_search_context context;
2899 if (!playlist)
2900 playlist = &current_playlist;
2902 if (check_control(playlist) < 0)
2904 gui_syncsplash(HZ*2, ID2P(LANG_PLAYLIST_CONTROL_ACCESS_ERROR));
2905 return -1;
2908 if (position == PLAYLIST_REPLACE)
2910 if (playlist_remove_all_tracks(playlist) == 0)
2911 position = PLAYLIST_INSERT_LAST;
2912 else
2913 return -1;
2916 if (queue)
2917 count_str = ID2P(LANG_PLAYLIST_QUEUE_COUNT);
2918 else
2919 count_str = ID2P(LANG_PLAYLIST_INSERT_COUNT);
2921 display_playlist_count(0, count_str, false);
2923 context.playlist = playlist;
2924 context.position = position;
2925 context.queue = queue;
2926 context.count = 0;
2928 cpu_boost(true);
2930 result = playlist_directory_tracksearch(dirname, recurse,
2931 directory_search_callback, &context);
2933 sync_control(playlist, false);
2935 cpu_boost(false);
2937 display_playlist_count(context.count, count_str, true);
2939 if ((audio_status() & AUDIO_STATUS_PLAY) && playlist->started)
2940 audio_flush_and_reload_tracks();
2942 #ifdef HAVE_DIRCACHE
2943 queue_post(&playlist_queue, PLAYLIST_LOAD_POINTERS, 0);
2944 #endif
2946 return result;
2950 * Insert all tracks from specified playlist into dynamic playlist.
2952 int playlist_insert_playlist(struct playlist_info* playlist, const char *filename,
2953 int position, bool queue)
2955 int fd;
2956 int max;
2957 char *temp_ptr;
2958 const char *dir;
2959 unsigned char *count_str;
2960 char temp_buf[MAX_PATH+1];
2961 char trackname[MAX_PATH+1];
2962 int count = 0;
2963 int result = 0;
2964 bool utf8 = is_m3u8(filename);
2966 if (!playlist)
2967 playlist = &current_playlist;
2969 if (check_control(playlist) < 0)
2971 gui_syncsplash(HZ*2, ID2P(LANG_PLAYLIST_CONTROL_ACCESS_ERROR));
2972 return -1;
2975 fd = open(filename, O_RDONLY);
2976 if (fd < 0)
2978 gui_syncsplash(HZ*2, ID2P(LANG_PLAYLIST_ACCESS_ERROR));
2979 return -1;
2982 /* we need the directory name for formatting purposes */
2983 dir = filename;
2985 temp_ptr = strrchr(filename+1,'/');
2986 if (temp_ptr)
2987 *temp_ptr = 0;
2988 else
2989 dir = "/";
2991 if (queue)
2992 count_str = ID2P(LANG_PLAYLIST_QUEUE_COUNT);
2993 else
2994 count_str = ID2P(LANG_PLAYLIST_INSERT_COUNT);
2996 display_playlist_count(count, count_str, false);
2998 if (position == PLAYLIST_REPLACE)
3000 if (playlist_remove_all_tracks(playlist) == 0)
3001 position = PLAYLIST_INSERT_LAST;
3002 else return -1;
3005 cpu_boost(true);
3007 while ((max = read_line(fd, temp_buf, sizeof(temp_buf))) > 0)
3009 /* user abort */
3010 if (action_userabort(TIMEOUT_NOBLOCK))
3011 break;
3013 if (count == 0 && is_utf8_bom(temp_buf, max))
3015 max -= BOM_SIZE;
3016 memmove(temp_buf, temp_buf + BOM_SIZE, max);
3019 if (temp_buf[0] != '#' && temp_buf[0] != '\0')
3021 int insert_pos;
3023 if (!utf8)
3025 /* Use trackname as a temporay buffer. Note that trackname must
3026 * be as large as temp_buf.
3028 max = convert_m3u(temp_buf, max, sizeof(temp_buf), trackname);
3031 /* we need to format so that relative paths are correctly
3032 handled */
3033 if (format_track_path(trackname, temp_buf, sizeof(trackname), max,
3034 dir) < 0)
3036 result = -1;
3037 break;
3040 insert_pos = add_track_to_playlist(playlist, trackname, position,
3041 queue, -1);
3043 if (insert_pos < 0)
3045 result = -1;
3046 break;
3049 /* Make sure tracks are inserted in correct order if user
3050 requests INSERT_FIRST */
3051 if (position == PLAYLIST_INSERT_FIRST || position >= 0)
3052 position = insert_pos + 1;
3054 count++;
3056 if ((count%PLAYLIST_DISPLAY_COUNT) == 0)
3058 display_playlist_count(count, count_str, false);
3060 if (count == PLAYLIST_DISPLAY_COUNT &&
3061 (audio_status() & AUDIO_STATUS_PLAY) &&
3062 playlist->started)
3063 audio_flush_and_reload_tracks();
3067 /* let the other threads work */
3068 yield();
3071 close(fd);
3073 if (temp_ptr)
3074 *temp_ptr = '/';
3076 sync_control(playlist, false);
3078 cpu_boost(false);
3080 display_playlist_count(count, count_str, true);
3082 if ((audio_status() & AUDIO_STATUS_PLAY) && playlist->started)
3083 audio_flush_and_reload_tracks();
3085 #ifdef HAVE_DIRCACHE
3086 queue_post(&playlist_queue, PLAYLIST_LOAD_POINTERS, 0);
3087 #endif
3089 return result;
3093 * Delete track at specified index. If index is PLAYLIST_DELETE_CURRENT then
3094 * we want to delete the current playing track.
3096 int playlist_delete(struct playlist_info* playlist, int index)
3098 int result = 0;
3100 if (!playlist)
3101 playlist = &current_playlist;
3103 if (check_control(playlist) < 0)
3105 gui_syncsplash(HZ*2, ID2P(LANG_PLAYLIST_CONTROL_ACCESS_ERROR));
3106 return -1;
3109 if (index == PLAYLIST_DELETE_CURRENT)
3110 index = playlist->index;
3112 result = remove_track_from_playlist(playlist, index, true);
3114 if (result != -1 && (audio_status() & AUDIO_STATUS_PLAY) &&
3115 playlist->started)
3116 audio_flush_and_reload_tracks();
3118 return result;
3122 * Move track at index to new_index. Tracks between the two are shifted
3123 * appropriately. Returns 0 on success and -1 on failure.
3125 int playlist_move(struct playlist_info* playlist, int index, int new_index)
3127 int result;
3128 int seek;
3129 bool control_file;
3130 bool queue;
3131 bool current = false;
3132 int r;
3133 char filename[MAX_PATH];
3135 if (!playlist)
3136 playlist = &current_playlist;
3138 if (check_control(playlist) < 0)
3140 gui_syncsplash(HZ*2, ID2P(LANG_PLAYLIST_CONTROL_ACCESS_ERROR));
3141 return -1;
3144 if (index == new_index)
3145 return -1;
3147 if (index == playlist->index)
3148 /* Moving the current track */
3149 current = true;
3151 control_file = playlist->indices[index] & PLAYLIST_INSERT_TYPE_MASK;
3152 queue = playlist->indices[index] & PLAYLIST_QUEUE_MASK;
3153 seek = playlist->indices[index] & PLAYLIST_SEEK_MASK;
3155 if (get_filename(playlist, index, seek, control_file, filename,
3156 sizeof(filename)) < 0)
3157 return -1;
3159 /* Delete track from original position */
3160 result = remove_track_from_playlist(playlist, index, true);
3162 if (result != -1)
3164 /* We want to insert the track at the position that was specified by
3165 new_index. This may be different then new_index because of the
3166 shifting that occurred after the delete */
3167 r = rotate_index(playlist, new_index);
3169 if (r == 0)
3170 /* First index */
3171 new_index = PLAYLIST_PREPEND;
3172 else if (r == playlist->amount)
3173 /* Append */
3174 new_index = PLAYLIST_INSERT_LAST;
3175 else
3176 /* Calculate index of desired position */
3177 new_index = (r+playlist->first_index)%playlist->amount;
3179 result = add_track_to_playlist(playlist, filename, new_index, queue,
3180 -1);
3182 if (result != -1)
3184 if (current)
3186 /* Moved the current track */
3187 switch (new_index)
3189 case PLAYLIST_PREPEND:
3190 playlist->index = playlist->first_index;
3191 break;
3192 case PLAYLIST_INSERT_LAST:
3193 playlist->index = playlist->first_index - 1;
3194 if (playlist->index < 0)
3195 playlist->index += playlist->amount;
3196 break;
3197 default:
3198 playlist->index = new_index;
3199 break;
3203 if ((audio_status() & AUDIO_STATUS_PLAY) && playlist->started)
3204 audio_flush_and_reload_tracks();
3208 #ifdef HAVE_DIRCACHE
3209 queue_post(&playlist_queue, PLAYLIST_LOAD_POINTERS, 0);
3210 #endif
3212 return result;
3215 /* shuffle currently playing playlist */
3216 int playlist_randomise(struct playlist_info* playlist, unsigned int seed,
3217 bool start_current)
3219 int result;
3221 if (!playlist)
3222 playlist = &current_playlist;
3224 check_control(playlist);
3226 result = randomise_playlist(playlist, seed, start_current, true);
3228 if (result != -1 && (audio_status() & AUDIO_STATUS_PLAY) &&
3229 playlist->started)
3230 audio_flush_and_reload_tracks();
3232 return result;
3235 /* sort currently playing playlist */
3236 int playlist_sort(struct playlist_info* playlist, bool start_current)
3238 int result;
3240 if (!playlist)
3241 playlist = &current_playlist;
3243 check_control(playlist);
3245 result = sort_playlist(playlist, start_current, true);
3247 if (result != -1 && (audio_status() & AUDIO_STATUS_PLAY) &&
3248 playlist->started)
3249 audio_flush_and_reload_tracks();
3251 return result;
3254 /* returns true if playlist has been modified */
3255 bool playlist_modified(const struct playlist_info* playlist)
3257 if (!playlist)
3258 playlist = &current_playlist;
3260 if (playlist->shuffle_modified ||
3261 playlist->deleted ||
3262 playlist->num_inserted_tracks > 0)
3263 return true;
3265 return false;
3268 /* returns index of first track in playlist */
3269 int playlist_get_first_index(const struct playlist_info* playlist)
3271 if (!playlist)
3272 playlist = &current_playlist;
3274 return playlist->first_index;
3277 /* returns shuffle seed of playlist */
3278 int playlist_get_seed(const struct playlist_info* playlist)
3280 if (!playlist)
3281 playlist = &current_playlist;
3283 return playlist->seed;
3286 /* returns number of tracks in playlist (includes queued/inserted tracks) */
3287 int playlist_amount_ex(const struct playlist_info* playlist)
3289 if (!playlist)
3290 playlist = &current_playlist;
3292 return playlist->amount;
3295 /* returns full path of playlist (minus extension) */
3296 char *playlist_name(const struct playlist_info* playlist, char *buf,
3297 int buf_size)
3299 char *sep;
3301 if (!playlist)
3302 playlist = &current_playlist;
3304 snprintf(buf, buf_size, "%s", playlist->filename+playlist->dirlen);
3306 if (!buf[0])
3307 return NULL;
3309 /* Remove extension */
3310 sep = strrchr(buf, '.');
3311 if (sep)
3312 *sep = 0;
3314 return buf;
3317 /* returns the playlist filename */
3318 char *playlist_get_name(const struct playlist_info* playlist, char *buf,
3319 int buf_size)
3321 if (!playlist)
3322 playlist = &current_playlist;
3324 snprintf(buf, buf_size, "%s", playlist->filename);
3326 if (!buf[0])
3327 return NULL;
3329 return buf;
3332 /* Fills info structure with information about track at specified index.
3333 Returns 0 on success and -1 on failure */
3334 int playlist_get_track_info(struct playlist_info* playlist, int index,
3335 struct playlist_track_info* info)
3337 int seek;
3338 bool control_file;
3340 if (!playlist)
3341 playlist = &current_playlist;
3343 if (index < 0 || index >= playlist->amount)
3344 return -1;
3346 control_file = playlist->indices[index] & PLAYLIST_INSERT_TYPE_MASK;
3347 seek = playlist->indices[index] & PLAYLIST_SEEK_MASK;
3349 if (get_filename(playlist, index, seek, control_file, info->filename,
3350 sizeof(info->filename)) < 0)
3351 return -1;
3353 info->attr = 0;
3355 if (control_file)
3357 if (playlist->indices[index] & PLAYLIST_QUEUE_MASK)
3358 info->attr |= PLAYLIST_ATTR_QUEUED;
3359 else
3360 info->attr |= PLAYLIST_ATTR_INSERTED;
3364 if (playlist->indices[index] & PLAYLIST_SKIPPED)
3365 info->attr |= PLAYLIST_ATTR_SKIPPED;
3367 info->index = index;
3368 info->display_index = rotate_index(playlist, index) + 1;
3370 return 0;
3373 /* save the current dynamic playlist to specified file */
3374 int playlist_save(struct playlist_info* playlist, char *filename)
3376 int fd;
3377 int i, index;
3378 int count = 0;
3379 char path[MAX_PATH+1];
3380 char tmp_buf[MAX_PATH+1];
3381 int result = 0;
3382 bool overwrite_current = false;
3383 int* index_buf = NULL;
3385 if (!playlist)
3386 playlist = &current_playlist;
3388 if (playlist->amount <= 0)
3389 return -1;
3391 /* use current working directory as base for pathname */
3392 if (format_track_path(path, filename, sizeof(tmp_buf),
3393 strlen(filename)+1, getcwd(NULL, -1)) < 0)
3394 return -1;
3396 if (!strncmp(playlist->filename, path, strlen(path)))
3398 /* Attempting to overwrite current playlist file.*/
3400 if (playlist->buffer_size < (int)(playlist->amount * sizeof(int)))
3402 /* not enough buffer space to store updated indices */
3403 gui_syncsplash(HZ*2, ID2P(LANG_PLAYLIST_ACCESS_ERROR));
3404 return -1;
3407 /* in_ram buffer is unused for m3u files so we'll use for storing
3408 updated indices */
3409 index_buf = (int*)playlist->buffer;
3411 /* use temporary pathname */
3412 snprintf(path, sizeof(path), "%s_temp", playlist->filename);
3413 overwrite_current = true;
3416 fd = open(path, O_CREAT|O_WRONLY|O_TRUNC);
3417 if (fd < 0)
3419 gui_syncsplash(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 if (is_m3u8(path))
3429 /* some applications require a BOM to read the file properly */
3430 write(fd, BOM, BOM_SIZE);
3433 index = playlist->first_index;
3434 for (i=0; i<playlist->amount; i++)
3436 bool control_file;
3437 bool queue;
3438 int seek;
3440 /* user abort */
3441 if (action_userabort(TIMEOUT_NOBLOCK))
3443 result = -1;
3444 break;
3447 control_file = playlist->indices[index] & PLAYLIST_INSERT_TYPE_MASK;
3448 queue = playlist->indices[index] & PLAYLIST_QUEUE_MASK;
3449 seek = playlist->indices[index] & PLAYLIST_SEEK_MASK;
3451 /* Don't save queued files */
3452 if (!queue)
3454 if (get_filename(playlist, index, seek, control_file, tmp_buf,
3455 MAX_PATH+1) < 0)
3457 result = -1;
3458 break;
3461 if (overwrite_current)
3462 index_buf[count] = lseek(fd, 0, SEEK_CUR);
3464 if (fdprintf(fd, "%s\n", tmp_buf) < 0)
3466 gui_syncsplash(HZ*2, ID2P(LANG_PLAYLIST_ACCESS_ERROR));
3467 result = -1;
3468 break;
3471 count++;
3473 if ((count % PLAYLIST_DISPLAY_COUNT) == 0)
3474 display_playlist_count(count, ID2P(LANG_PLAYLIST_SAVE_COUNT),
3475 false);
3477 yield();
3480 index = (index+1)%playlist->amount;
3483 display_playlist_count(count, ID2P(LANG_PLAYLIST_SAVE_COUNT), true);
3485 close(fd);
3487 if (overwrite_current && result >= 0)
3489 result = -1;
3491 mutex_lock(&playlist->control_mutex);
3493 /* Replace the current playlist with the new one and update indices */
3494 close(playlist->fd);
3495 if (remove(playlist->filename) >= 0)
3497 if (rename(path, playlist->filename) >= 0)
3499 playlist->fd = open(playlist->filename, O_RDONLY);
3500 if (playlist->fd >= 0)
3502 index = playlist->first_index;
3503 for (i=0, count=0; i<playlist->amount; i++)
3505 if (!(playlist->indices[index] & PLAYLIST_QUEUE_MASK))
3507 playlist->indices[index] = index_buf[count];
3508 count++;
3510 index = (index+1)%playlist->amount;
3513 /* we need to recreate control because inserted tracks are
3514 now part of the playlist and shuffle has been
3515 invalidated */
3516 result = recreate_control(playlist);
3521 mutex_unlock(&playlist->control_mutex);
3525 cpu_boost(false);
3527 return result;
3531 * Search specified directory for tracks and notify via callback. May be
3532 * called recursively.
3534 int playlist_directory_tracksearch(const char* dirname, bool recurse,
3535 int (*callback)(char*, void*),
3536 void* context)
3538 char buf[MAX_PATH+1];
3539 int result = 0;
3540 int num_files = 0;
3541 int i;
3542 struct entry *files;
3543 struct tree_context* tc = tree_get_context();
3544 int old_dirfilter = *(tc->dirfilter);
3546 if (!callback)
3547 return -1;
3549 /* use the tree browser dircache to load files */
3550 *(tc->dirfilter) = SHOW_ALL;
3552 if (ft_load(tc, dirname) < 0)
3554 gui_syncsplash(HZ*2, ID2P(LANG_PLAYLIST_DIRECTORY_ACCESS_ERROR));
3555 *(tc->dirfilter) = old_dirfilter;
3556 return -1;
3559 files = (struct entry*) tc->dircache;
3560 num_files = tc->filesindir;
3562 /* we've overwritten the dircache so tree browser will need to be
3563 reloaded */
3564 reload_directory();
3566 for (i=0; i<num_files; i++)
3568 /* user abort */
3569 if (action_userabort(TIMEOUT_NOBLOCK))
3571 result = -1;
3572 break;
3575 if (files[i].attr & ATTR_DIRECTORY)
3577 if (recurse)
3579 /* recursively add directories */
3580 snprintf(buf, sizeof(buf), "%s/%s", dirname, files[i].name);
3581 result = playlist_directory_tracksearch(buf, recurse,
3582 callback, context);
3583 if (result < 0)
3584 break;
3586 /* we now need to reload our current directory */
3587 if(ft_load(tc, dirname) < 0)
3589 result = -1;
3590 break;
3593 files = (struct entry*) tc->dircache;
3594 num_files = tc->filesindir;
3595 if (!num_files)
3597 result = -1;
3598 break;
3601 else
3602 continue;
3604 else if ((files[i].attr & FILE_ATTR_MASK) == FILE_ATTR_AUDIO)
3606 snprintf(buf, sizeof(buf), "%s/%s", dirname, files[i].name);
3608 if (callback(buf, context) != 0)
3610 result = -1;
3611 break;
3614 /* let the other threads work */
3615 yield();
3619 /* restore dirfilter */
3620 *(tc->dirfilter) = old_dirfilter;
3622 return result;