mpd/src/playlist.c

1457 lines
34 KiB
C
Raw Normal View History

/* the Music Player Daemon (MPD)
* Copyright (C) 2003-2007 by Warren Dukes (warren.dukes@gmail.com)
* This project's homepage is: http://www.musicpd.org
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "playlist.h"
#include "player_control.h"
#include "command.h"
#include "ls.h"
#include "tag.h"
#include "song_print.h"
#include "conf.h"
#include "directory.h"
#include "log.h"
#include "path.h"
#include "utils.h"
#include "state_file.h"
#include "storedPlaylist.h"
#include "ack.h"
#include "myfprintf.h"
#include "os_compat.h"
#define PLAYLIST_STATE_STOP 0
#define PLAYLIST_STATE_PLAY 1
#define PLAYLIST_PREV_UNLESS_ELAPSED 10
#define PLAYLIST_STATE_FILE_STATE "state: "
#define PLAYLIST_STATE_FILE_RANDOM "random: "
#define PLAYLIST_STATE_FILE_REPEAT "repeat: "
#define PLAYLIST_STATE_FILE_CURRENT "current: "
#define PLAYLIST_STATE_FILE_TIME "time: "
#define PLAYLIST_STATE_FILE_CROSSFADE "crossfade: "
#define PLAYLIST_STATE_FILE_PLAYLIST_BEGIN "playlist_begin"
#define PLAYLIST_STATE_FILE_PLAYLIST_END "playlist_end"
#define PLAYLIST_STATE_FILE_STATE_PLAY "play"
#define PLAYLIST_STATE_FILE_STATE_PAUSE "pause"
#define PLAYLIST_STATE_FILE_STATE_STOP "stop"
Merge branches/ew r7104 thread-safety work in preparation for rewrite to use pthreads Expect no regressions against trunk (r7078), possibly minor performance improvements in update (due to fewer heap allocations), but increased stack usage. Applied the following patches: * maxpath_str for reentrancy (temporary fix, reverted) * path: start working on thread-safe variants of these methods * Re-entrancy work on path/character-set conversions * directory.c: exploreDirectory() use reentrant functions here * directory/update: more use of reentrant functions + cleanups * string_toupper: a strdup-less version of strDupToUpper * get_song_url: a static-variable-free version of getSongUrl() * Use reentrant/thread-safe get_song_url everywhere * replace rmp2amp with the reentrant version, rmp2amp_r * Get rid of the non-reentrant/non-thread-safe rpp2app, too. * buffer2array: assert strdup() returns a usable value in unit tests * replace utf8ToFsCharset and fsCharsetToUtf8 with thread-safe variants * fix storing playlists w/o absolute paths * parent_path(), a reentrant version of parentPath() * parentPath => parent_path for reentrancy and thread-safety * allow "make test" to automatically run embedded unit tests * remove convStrDup() and maxpath_str() * use MPD_PATH_MAX everywhere instead of MAXPATHLEN * path: get rid of appendSlash, pfx_path and just use pfx_dir * get_song_url: fix the ability to play songs in the top-level music_directory git-svn-id: https://svn.musicpd.org/mpd/trunk@7106 09075e82-0dd4-0310-85a5-a0d7c8717e4f
2007-12-28 03:56:25 +01:00
#define PLAYLIST_BUFFER_SIZE 2*MPD_PATH_MAX
#define PLAYLIST_HASH_MULT 4
#define DEFAULT_PLAYLIST_MAX_LENGTH (1024*16)
#define DEFAULT_PLAYLIST_SAVE_ABSOLUTE_PATHS 0
static Playlist playlist;
static int playlist_state = PLAYLIST_STATE_STOP;
int playlist_max_length = DEFAULT_PLAYLIST_MAX_LENGTH;
static int playlist_stopOnError;
static int playlist_errorCount;
static int playlist_noGoToNext;
int playlist_saveAbsolutePaths = DEFAULT_PLAYLIST_SAVE_ABSOLUTE_PATHS;
static void swapOrder(int a, int b);
static void playPlaylistOrderNumber(int orderNum);
static void randomizeOrder(int start, int end);
static void incrPlaylistVersion(void)
{
static unsigned long max = ((mpd_uint32) 1 << 31) - 1;
playlist.version++;
if (playlist.version >= max) {
int i;
for (i = 0; i < playlist.length; i++) {
playlist.songMod[i] = 0;
}
playlist.version = 1;
}
}
void playlistVersionChange(void)
{
int i;
for (i = 0; i < playlist.length; i++) {
playlist.songMod[i] = playlist.version;
}
incrPlaylistVersion();
}
static void incrPlaylistCurrent(void)
{
if (playlist.current < 0)
return;
if (playlist.current >= playlist.length - 1) {
if (playlist.repeat)
playlist.current = 0;
else
playlist.current = -1;
} else
playlist.current++;
}
void initPlaylist(void)
{
char *test;
int i;
ConfigParam *param;
playlist.length = 0;
playlist.repeat = 0;
playlist.version = 1;
playlist.random = 0;
playlist.queued = -1;
playlist.current = -1;
param = getConfigParam(CONF_MAX_PLAYLIST_LENGTH);
if (param) {
playlist_max_length = strtol(param->value, &test, 10);
if (*test != '\0') {
FATAL("max playlist length \"%s\" is not an integer, "
"line %i\n", param->value, param->line);
}
}
playlist_saveAbsolutePaths = getBoolConfigParam(
CONF_SAVE_ABSOLUTE_PATHS, 1);
if (playlist_saveAbsolutePaths == CONF_BOOL_UNSET)
playlist_saveAbsolutePaths =
DEFAULT_PLAYLIST_SAVE_ABSOLUTE_PATHS;
playlist.songs = xmalloc(sizeof(Song *) * playlist_max_length);
playlist.songMod = xmalloc(sizeof(mpd_uint32) * playlist_max_length);
playlist.order = xmalloc(sizeof(int) * playlist_max_length);
playlist.idToPosition = xmalloc(sizeof(int) * playlist_max_length *
PLAYLIST_HASH_MULT);
playlist.positionToId = xmalloc(sizeof(int) * playlist_max_length);
memset(playlist.songs, 0, sizeof(char *) * playlist_max_length);
srandom(time(NULL));
for (i = 0; i < playlist_max_length * PLAYLIST_HASH_MULT; i++) {
playlist.idToPosition[i] = -1;
}
}
static int getNextId(void)
{
static int cur = -1;
do {
cur++;
if (cur >= playlist_max_length * PLAYLIST_HASH_MULT) {
cur = 0;
}
} while (playlist.idToPosition[cur] != -1);
return cur;
}
void finishPlaylist(void)
{
int i;
for (i = 0; i < playlist.length; i++) {
if (playlist.songs[i]->type == SONG_TYPE_URL) {
freeJustSong(playlist.songs[i]);
}
}
playlist.length = 0;
free(playlist.songs);
playlist.songs = NULL;
free(playlist.songMod);
playlist.songMod = NULL;
free(playlist.order);
playlist.order = NULL;
free(playlist.idToPosition);
playlist.idToPosition = NULL;
free(playlist.positionToId);
playlist.positionToId = NULL;
}
void clearPlaylist(void)
{
int i;
stopPlaylist();
for (i = 0; i < playlist.length; i++) {
if (playlist.songs[i]->type == SONG_TYPE_URL) {
freeJustSong(playlist.songs[i]);
}
playlist.idToPosition[playlist.positionToId[i]] = -1;
playlist.songs[i] = NULL;
}
playlist.length = 0;
playlist.current = -1;
incrPlaylistVersion();
}
int clearStoredPlaylist(const char *utf8file)
{
return removeAllFromStoredPlaylistByPath(utf8file);
}
void showPlaylist(int fd)
{
int i;
Merge branches/ew r7104 thread-safety work in preparation for rewrite to use pthreads Expect no regressions against trunk (r7078), possibly minor performance improvements in update (due to fewer heap allocations), but increased stack usage. Applied the following patches: * maxpath_str for reentrancy (temporary fix, reverted) * path: start working on thread-safe variants of these methods * Re-entrancy work on path/character-set conversions * directory.c: exploreDirectory() use reentrant functions here * directory/update: more use of reentrant functions + cleanups * string_toupper: a strdup-less version of strDupToUpper * get_song_url: a static-variable-free version of getSongUrl() * Use reentrant/thread-safe get_song_url everywhere * replace rmp2amp with the reentrant version, rmp2amp_r * Get rid of the non-reentrant/non-thread-safe rpp2app, too. * buffer2array: assert strdup() returns a usable value in unit tests * replace utf8ToFsCharset and fsCharsetToUtf8 with thread-safe variants * fix storing playlists w/o absolute paths * parent_path(), a reentrant version of parentPath() * parentPath => parent_path for reentrancy and thread-safety * allow "make test" to automatically run embedded unit tests * remove convStrDup() and maxpath_str() * use MPD_PATH_MAX everywhere instead of MAXPATHLEN * path: get rid of appendSlash, pfx_path and just use pfx_dir * get_song_url: fix the ability to play songs in the top-level music_directory git-svn-id: https://svn.musicpd.org/mpd/trunk@7106 09075e82-0dd4-0310-85a5-a0d7c8717e4f
2007-12-28 03:56:25 +01:00
char path_max_tmp[MPD_PATH_MAX];
for (i = 0; i < playlist.length; i++) {
Merge branches/ew r7104 thread-safety work in preparation for rewrite to use pthreads Expect no regressions against trunk (r7078), possibly minor performance improvements in update (due to fewer heap allocations), but increased stack usage. Applied the following patches: * maxpath_str for reentrancy (temporary fix, reverted) * path: start working on thread-safe variants of these methods * Re-entrancy work on path/character-set conversions * directory.c: exploreDirectory() use reentrant functions here * directory/update: more use of reentrant functions + cleanups * string_toupper: a strdup-less version of strDupToUpper * get_song_url: a static-variable-free version of getSongUrl() * Use reentrant/thread-safe get_song_url everywhere * replace rmp2amp with the reentrant version, rmp2amp_r * Get rid of the non-reentrant/non-thread-safe rpp2app, too. * buffer2array: assert strdup() returns a usable value in unit tests * replace utf8ToFsCharset and fsCharsetToUtf8 with thread-safe variants * fix storing playlists w/o absolute paths * parent_path(), a reentrant version of parentPath() * parentPath => parent_path for reentrancy and thread-safety * allow "make test" to automatically run embedded unit tests * remove convStrDup() and maxpath_str() * use MPD_PATH_MAX everywhere instead of MAXPATHLEN * path: get rid of appendSlash, pfx_path and just use pfx_dir * get_song_url: fix the ability to play songs in the top-level music_directory git-svn-id: https://svn.musicpd.org/mpd/trunk@7106 09075e82-0dd4-0310-85a5-a0d7c8717e4f
2007-12-28 03:56:25 +01:00
fdprintf(fd, "%i:%s\n", i,
get_song_url(path_max_tmp, playlist.songs[i]));
}
}
void savePlaylistState(FILE *fp)
{
fprintf(fp, "%s", PLAYLIST_STATE_FILE_STATE);
switch (playlist_state) {
case PLAYLIST_STATE_PLAY:
switch (getPlayerState()) {
case PLAYER_STATE_PAUSE:
fprintf(fp, "%s\n", PLAYLIST_STATE_FILE_STATE_PAUSE);
break;
default:
fprintf(fp, "%s\n", PLAYLIST_STATE_FILE_STATE_PLAY);
}
fprintf(fp, "%s%i\n", PLAYLIST_STATE_FILE_CURRENT,
playlist.order[playlist.current]);
fprintf(fp, "%s%i\n", PLAYLIST_STATE_FILE_TIME,
getPlayerElapsedTime());
break;
default:
fprintf(fp, "%s\n", PLAYLIST_STATE_FILE_STATE_STOP);
break;
}
fprintf(fp, "%s%i\n", PLAYLIST_STATE_FILE_RANDOM, playlist.random);
fprintf(fp, "%s%i\n", PLAYLIST_STATE_FILE_REPEAT, playlist.repeat);
fprintf(fp, "%s%i\n", PLAYLIST_STATE_FILE_CROSSFADE,
(int)(getPlayerCrossFade()));
fprintf(fp, "%s\n", PLAYLIST_STATE_FILE_PLAYLIST_BEGIN);
fflush(fp);
showPlaylist(fileno(fp));
fprintf(fp, "%s\n", PLAYLIST_STATE_FILE_PLAYLIST_END);
}
static void loadPlaylistFromStateFile(FILE *fp, char *buffer,
int state, int current, int seek_time)
{
char *temp;
int song;
if (!myFgets(buffer, PLAYLIST_BUFFER_SIZE, fp))
state_file_fatal();
while (strcmp(buffer, PLAYLIST_STATE_FILE_PLAYLIST_END)) {
temp = strtok(buffer, ":");
if (temp == NULL)
state_file_fatal();
song = atoi(temp);
if (!(temp = strtok(NULL, "")))
state_file_fatal();
if (addToPlaylist(temp, NULL) == PLAYLIST_RESULT_SUCCESS
&& current == song) {
if (state != PLAYER_STATE_STOP) {
playPlaylist(playlist.length - 1, 0);
}
if (state == PLAYER_STATE_PAUSE) {
playerPause();
}
if (state != PLAYER_STATE_STOP) {
seekSongInPlaylist(playlist.length - 1,
seek_time);
}
}
if (!myFgets(buffer, PLAYLIST_BUFFER_SIZE, fp))
state_file_fatal();
}
}
void readPlaylistState(FILE *fp)
{
int current = -1;
int seek_time = 0;
int state = PLAYER_STATE_STOP;
char buffer[PLAYLIST_BUFFER_SIZE];
while (myFgets(buffer, PLAYLIST_BUFFER_SIZE, fp)) {
if (strncmp(buffer, PLAYLIST_STATE_FILE_STATE,
strlen(PLAYLIST_STATE_FILE_STATE)) == 0) {
if (strcmp(&(buffer[strlen(PLAYLIST_STATE_FILE_STATE)]),
PLAYLIST_STATE_FILE_STATE_PLAY) == 0) {
state = PLAYER_STATE_PLAY;
} else
if (strcmp
(&(buffer[strlen(PLAYLIST_STATE_FILE_STATE)]),
PLAYLIST_STATE_FILE_STATE_PAUSE)
== 0) {
state = PLAYER_STATE_PAUSE;
}
} else if (strncmp(buffer, PLAYLIST_STATE_FILE_TIME,
strlen(PLAYLIST_STATE_FILE_TIME)) == 0) {
seek_time =
atoi(&(buffer[strlen(PLAYLIST_STATE_FILE_TIME)]));
} else
if (strncmp
(buffer, PLAYLIST_STATE_FILE_REPEAT,
strlen(PLAYLIST_STATE_FILE_REPEAT)) == 0) {
if (strcmp
(&(buffer[strlen(PLAYLIST_STATE_FILE_REPEAT)]),
"1") == 0) {
setPlaylistRepeatStatus(1);
} else
setPlaylistRepeatStatus(0);
} else
if (strncmp
(buffer, PLAYLIST_STATE_FILE_CROSSFADE,
strlen(PLAYLIST_STATE_FILE_CROSSFADE)) == 0) {
setPlayerCrossFade(atoi
(&
(buffer
[strlen
(PLAYLIST_STATE_FILE_CROSSFADE)])));
} else
if (strncmp
(buffer, PLAYLIST_STATE_FILE_RANDOM,
strlen(PLAYLIST_STATE_FILE_RANDOM)) == 0) {
if (strcmp
(&
(buffer
[strlen(PLAYLIST_STATE_FILE_RANDOM)]),
"1") == 0) {
setPlaylistRandomStatus(1);
} else
setPlaylistRandomStatus(0);
} else if (strncmp(buffer, PLAYLIST_STATE_FILE_CURRENT,
strlen(PLAYLIST_STATE_FILE_CURRENT))
== 0) {
if (strlen(buffer) ==
strlen(PLAYLIST_STATE_FILE_CURRENT))
state_file_fatal();
current = atoi(&(buffer
[strlen
(PLAYLIST_STATE_FILE_CURRENT)]));
} else
if (strncmp
(buffer, PLAYLIST_STATE_FILE_PLAYLIST_BEGIN,
strlen(PLAYLIST_STATE_FILE_PLAYLIST_BEGIN)
) == 0) {
if (state == PLAYER_STATE_STOP)
current = -1;
loadPlaylistFromStateFile(fp, buffer, state,
current, seek_time);
}
}
}
static void printPlaylistSongInfo(int fd, int song)
{
printSongInfo(fd, playlist.songs[song]);
fdprintf(fd, "Pos: %i\nId: %i\n", song, playlist.positionToId[song]);
}
int playlistChanges(int fd, mpd_uint32 version)
{
int i;
for (i = 0; i < playlist.length; i++) {
if (version > playlist.version ||
playlist.songMod[i] >= version ||
playlist.songMod[i] == 0) {
printPlaylistSongInfo(fd, i);
}
}
return 0;
}
int playlistChangesPosId(int fd, mpd_uint32 version)
{
int i;
for (i = 0; i < playlist.length; i++) {
if (version > playlist.version ||
playlist.songMod[i] >= version ||
playlist.songMod[i] == 0) {
fdprintf(fd, "cpos: %i\nId: %i\n",
i, playlist.positionToId[i]);
}
}
return 0;
}
enum playlist_result playlistInfo(int fd, int song)
{
int i;
int begin = 0;
int end = playlist.length;
if (song >= 0) {
begin = song;
end = song + 1;
}
if (song >= playlist.length)
return PLAYLIST_RESULT_BAD_RANGE;
for (i = begin; i < end; i++)
printPlaylistSongInfo(fd, i);
return PLAYLIST_RESULT_SUCCESS;
}
static int song_id_exists(int id)
{
return id >= 0 && id < PLAYLIST_HASH_MULT*playlist_max_length &&
playlist.idToPosition[id] != -1;
}
enum playlist_result playlistId(int fd, int id)
{
int i;
int begin = 0;
int end = playlist.length;
if (id >= 0) {
if (!song_id_exists(id))
return PLAYLIST_RESULT_NO_SUCH_SONG;
begin = playlist.idToPosition[id];
end = begin + 1;
}
for (i = begin; i < end; i++)
printPlaylistSongInfo(fd, i);
return PLAYLIST_RESULT_SUCCESS;
}
static void swapSongs(int song1, int song2)
{
Song *sTemp;
int iTemp;
sTemp = playlist.songs[song1];
playlist.songs[song1] = playlist.songs[song2];
playlist.songs[song2] = sTemp;
playlist.songMod[song1] = playlist.version;
playlist.songMod[song2] = playlist.version;
playlist.idToPosition[playlist.positionToId[song1]] = song2;
playlist.idToPosition[playlist.positionToId[song2]] = song1;
iTemp = playlist.positionToId[song1];
playlist.positionToId[song1] = playlist.positionToId[song2];
playlist.positionToId[song2] = iTemp;
}
static void queueNextSongInPlaylist(void)
{
Merge branches/ew r7104 thread-safety work in preparation for rewrite to use pthreads Expect no regressions against trunk (r7078), possibly minor performance improvements in update (due to fewer heap allocations), but increased stack usage. Applied the following patches: * maxpath_str for reentrancy (temporary fix, reverted) * path: start working on thread-safe variants of these methods * Re-entrancy work on path/character-set conversions * directory.c: exploreDirectory() use reentrant functions here * directory/update: more use of reentrant functions + cleanups * string_toupper: a strdup-less version of strDupToUpper * get_song_url: a static-variable-free version of getSongUrl() * Use reentrant/thread-safe get_song_url everywhere * replace rmp2amp with the reentrant version, rmp2amp_r * Get rid of the non-reentrant/non-thread-safe rpp2app, too. * buffer2array: assert strdup() returns a usable value in unit tests * replace utf8ToFsCharset and fsCharsetToUtf8 with thread-safe variants * fix storing playlists w/o absolute paths * parent_path(), a reentrant version of parentPath() * parentPath => parent_path for reentrancy and thread-safety * allow "make test" to automatically run embedded unit tests * remove convStrDup() and maxpath_str() * use MPD_PATH_MAX everywhere instead of MAXPATHLEN * path: get rid of appendSlash, pfx_path and just use pfx_dir * get_song_url: fix the ability to play songs in the top-level music_directory git-svn-id: https://svn.musicpd.org/mpd/trunk@7106 09075e82-0dd4-0310-85a5-a0d7c8717e4f
2007-12-28 03:56:25 +01:00
char path_max_tmp[MPD_PATH_MAX];
Revert the queue implementation and commands It's too ugly and broken (both technically and usability-wise) to be worth supporting in any stable release. In one sentence: The queue is a very crippled version of the playlist that takes precedence over the normal playlist. How is it crippled? * The "queueid" command only allows the queuing of songs ALREADY IN THE PLAYLIST! This promotes having the entire mpd database of songs in the playlist, which is a stupid practice to begin with. * It doesn't allow for meaningful rearranging and movement of songs within the queue. To move a song, you'd need to dequeue and requeue it (and other songs on the list). Why? The playlist already allows _all_ these features and shows everything a client needs to know about the ordering of songs in a _single_ command! * Random was a stupid idea to begin with and unfortunately we're stuck supporting it since we've always had it. Users should learn to use "shuffle" instead and not look at their playlists. Implementing queue because we have the problem of random is just a bandage fix and digging ourselves a new hole. This protocol addition was never in a stable release of mpd, so reverting it will only break things for people following trunk; which I'm not too worried about. I am however worried about long-term support of this misfeature, so I'm removing it. Additionally, there are other points: * It's trivially DoS-able: (while true; do echo queueid $song_id; done) | nc $MPD_HOST $MPD_PORT The above commands would cause the queue to become infinitely expanding, taking up all available memory in the system. The mpd playlist was implemented as an array with a fixed (but configurable) size limit for this reason. * It's not backwards-compatible. All clients would require upgrades (and additional complexity) to even know what the next song in the playlist is. mpd is a shared architecture, and we should not violate the principle of least astonishment here. This removes the following commands: queueid, dequeue, queueinfo Additionally, the status field of "playlistqueue: " is removed from the status command. While this DoS is trivial to fix, the design is simply too broken to ever support in a real release. The overloading of the "addid" command and the allowing of negative numbers to be used as offsets is far more flexible. This improved "addid" is completely backwards-compatible with all clients, and does not require clients to have UI changes or run additional commands to display the queue. git-svn-id: https://svn.musicpd.org/mpd/trunk@7155 09075e82-0dd4-0310-85a5-a0d7c8717e4f
2008-01-26 21:21:07 +01:00
if (playlist.current < playlist.length - 1) {
playlist.queued = playlist.current + 1;
DEBUG("playlist: queue song %i:\"%s\"\n",
playlist.queued,
Merge branches/ew r7104 thread-safety work in preparation for rewrite to use pthreads Expect no regressions against trunk (r7078), possibly minor performance improvements in update (due to fewer heap allocations), but increased stack usage. Applied the following patches: * maxpath_str for reentrancy (temporary fix, reverted) * path: start working on thread-safe variants of these methods * Re-entrancy work on path/character-set conversions * directory.c: exploreDirectory() use reentrant functions here * directory/update: more use of reentrant functions + cleanups * string_toupper: a strdup-less version of strDupToUpper * get_song_url: a static-variable-free version of getSongUrl() * Use reentrant/thread-safe get_song_url everywhere * replace rmp2amp with the reentrant version, rmp2amp_r * Get rid of the non-reentrant/non-thread-safe rpp2app, too. * buffer2array: assert strdup() returns a usable value in unit tests * replace utf8ToFsCharset and fsCharsetToUtf8 with thread-safe variants * fix storing playlists w/o absolute paths * parent_path(), a reentrant version of parentPath() * parentPath => parent_path for reentrancy and thread-safety * allow "make test" to automatically run embedded unit tests * remove convStrDup() and maxpath_str() * use MPD_PATH_MAX everywhere instead of MAXPATHLEN * path: get rid of appendSlash, pfx_path and just use pfx_dir * get_song_url: fix the ability to play songs in the top-level music_directory git-svn-id: https://svn.musicpd.org/mpd/trunk@7106 09075e82-0dd4-0310-85a5-a0d7c8717e4f
2007-12-28 03:56:25 +01:00
get_song_url(path_max_tmp,
playlist.
songs[playlist.order[playlist.queued]]));
queueSong(playlist.songs[playlist.order[playlist.queued]]);
} else if (playlist.length && playlist.repeat) {
if (playlist.length > 1 && playlist.random) {
randomizeOrder(0, playlist.length - 1);
}
playlist.queued = 0;
DEBUG("playlist: queue song %i:\"%s\"\n",
playlist.queued,
Merge branches/ew r7104 thread-safety work in preparation for rewrite to use pthreads Expect no regressions against trunk (r7078), possibly minor performance improvements in update (due to fewer heap allocations), but increased stack usage. Applied the following patches: * maxpath_str for reentrancy (temporary fix, reverted) * path: start working on thread-safe variants of these methods * Re-entrancy work on path/character-set conversions * directory.c: exploreDirectory() use reentrant functions here * directory/update: more use of reentrant functions + cleanups * string_toupper: a strdup-less version of strDupToUpper * get_song_url: a static-variable-free version of getSongUrl() * Use reentrant/thread-safe get_song_url everywhere * replace rmp2amp with the reentrant version, rmp2amp_r * Get rid of the non-reentrant/non-thread-safe rpp2app, too. * buffer2array: assert strdup() returns a usable value in unit tests * replace utf8ToFsCharset and fsCharsetToUtf8 with thread-safe variants * fix storing playlists w/o absolute paths * parent_path(), a reentrant version of parentPath() * parentPath => parent_path for reentrancy and thread-safety * allow "make test" to automatically run embedded unit tests * remove convStrDup() and maxpath_str() * use MPD_PATH_MAX everywhere instead of MAXPATHLEN * path: get rid of appendSlash, pfx_path and just use pfx_dir * get_song_url: fix the ability to play songs in the top-level music_directory git-svn-id: https://svn.musicpd.org/mpd/trunk@7106 09075e82-0dd4-0310-85a5-a0d7c8717e4f
2007-12-28 03:56:25 +01:00
get_song_url(path_max_tmp,
playlist.
songs[playlist.order[playlist.queued]]));
queueSong(playlist.songs[playlist.order[playlist.queued]]);
}
}
static void syncPlaylistWithQueue(void)
{
switch (getPlayerQueueState()) {
case PLAYER_QUEUE_EMPTY:
setQueueState(PLAYER_QUEUE_BLANK);
if (playlist.queued >= 0) {
DEBUG("playlist: now playing queued song\n");
playlist.current = playlist.queued;
}
playlist.queued = -1;
break;
case PLAYER_QUEUE_DECODE:
if (playlist.queued != -1)
setQueueState(PLAYER_QUEUE_PLAY);
else
setQueueState(PLAYER_QUEUE_STOP);
break;
case PLAYER_QUEUE_FULL:
case PLAYER_QUEUE_PLAY:
case PLAYER_QUEUE_STOP:
case PLAYER_QUEUE_BLANK:
break;
}
}
static void clearPlayerQueue(void)
{
if (getPlayerQueueState() == PLAYER_QUEUE_PLAY ||
getPlayerQueueState() == PLAYER_QUEUE_FULL) {
playerQueueLock();
syncPlaylistWithQueue();
}
playlist.queued = -1;
switch (getPlayerQueueState()) {
2008-08-26 08:27:16 +02:00
case PLAYER_QUEUE_BLANK:
case PLAYER_QUEUE_DECODE:
case PLAYER_QUEUE_STOP:
case PLAYER_QUEUE_EMPTY:
break;
case PLAYER_QUEUE_FULL:
DEBUG("playlist: dequeue song\n");
setQueueState(PLAYER_QUEUE_BLANK);
break;
case PLAYER_QUEUE_PLAY:
DEBUG("playlist: stop decoding queued song\n");
setQueueState(PLAYER_QUEUE_STOP);
break;
}
playerQueueUnlock();
}
enum playlist_result addToPlaylist(const char *url, int *added_id)
{
Song *song;
DEBUG("add to playlist: %s\n", url);
if ((song = getSongFromDB(url))) {
} else if (!(isValidRemoteUtf8Url(url) &&
(song = newSong(url, SONG_TYPE_URL, NULL)))) {
return PLAYLIST_RESULT_NO_SUCH_SONG;
}
return addSongToPlaylist(song, added_id);
}
int addToStoredPlaylist(const char *url, const char *utf8file)
{
Song *song;
DEBUG("add to stored playlist: %s\n", url);
song = getSongFromDB(url);
if (song)
return appendSongToStoredPlaylistByPath(utf8file, song);
if (!isValidRemoteUtf8Url(url))
return ACK_ERROR_NO_EXIST;
song = newSong(url, SONG_TYPE_URL, NULL);
if (song) {
int ret = appendSongToStoredPlaylistByPath(utf8file, song);
freeJustSong(song);
return ret;
}
return ACK_ERROR_NO_EXIST;
}
enum playlist_result addSongToPlaylist(Song * song, int *added_id)
{
int id;
if (playlist.length == playlist_max_length)
return PLAYLIST_RESULT_TOO_LARGE;
if (playlist_state == PLAYLIST_STATE_PLAY) {
if (playlist.queued >= 0
&& playlist.current == playlist.length - 1)
clearPlayerQueue();
}
id = getNextId();
playlist.songs[playlist.length] = song;
playlist.songMod[playlist.length] = playlist.version;
playlist.order[playlist.length] = playlist.length;
playlist.positionToId[playlist.length] = id;
playlist.idToPosition[playlist.positionToId[playlist.length]] =
playlist.length;
playlist.length++;
if (playlist.random) {
int swap;
int start;
/*if(playlist_state==PLAYLIST_STATE_STOP) start = 0;
else */ if (playlist.queued >= 0)
start = playlist.queued + 1;
else
start = playlist.current + 1;
if (start < playlist.length) {
swap = random() % (playlist.length - start);
swap += start;
swapOrder(playlist.length - 1, swap);
}
}
incrPlaylistVersion();
if (added_id)
*added_id = id;
return PLAYLIST_RESULT_SUCCESS;
}
enum playlist_result swapSongsInPlaylist(int song1, int song2)
{
int queuedSong = -1;
int currentSong;
if (song1 < 0 || song1 >= playlist.length ||
song2 < 0 || song2 >= playlist.length)
return PLAYLIST_RESULT_BAD_RANGE;
if (playlist_state == PLAYLIST_STATE_PLAY) {
if (playlist.queued >= 0) {
queuedSong = playlist.order[playlist.queued];
}
currentSong = playlist.order[playlist.current];
if (queuedSong == song1 || queuedSong == song2
|| currentSong == song1 || currentSong == song2)
clearPlayerQueue();
}
swapSongs(song1, song2);
if (playlist.random) {
int i;
int k;
int j = -1;
for (i = 0; playlist.order[i] != song1; i++) {
if (playlist.order[i] == song2)
j = i;
}
k = i;
for (; j == -1; i++)
if (playlist.order[i] == song2)
j = i;
swapOrder(k, j);
} else {
if (playlist.current == song1)
playlist.current = song2;
else if (playlist.current == song2)
playlist.current = song1;
}
incrPlaylistVersion();
return PLAYLIST_RESULT_SUCCESS;
}
enum playlist_result swapSongsInPlaylistById(int id1, int id2)
{
if (!song_id_exists(id1) || !song_id_exists(id2))
return PLAYLIST_RESULT_NO_SUCH_SONG;
return swapSongsInPlaylist(playlist.idToPosition[id1],
playlist.idToPosition[id2]);
}
#define moveSongFromTo(from, to) { \
playlist.idToPosition[playlist.positionToId[from]] = to; \
playlist.positionToId[to] = playlist.positionToId[from]; \
playlist.songs[to] = playlist.songs[from]; \
playlist.songMod[to] = playlist.version; \
}
enum playlist_result deleteFromPlaylist(int song)
{
int i;
int songOrder;
if (song < 0 || song >= playlist.length)
return PLAYLIST_RESULT_BAD_RANGE;
if (playlist_state == PLAYLIST_STATE_PLAY) {
if (playlist.queued >= 0
&& (playlist.order[playlist.queued] == song
|| playlist.order[playlist.current] == song))
clearPlayerQueue();
}
if (playlist.songs[song]->type == SONG_TYPE_URL) {
freeJustSong(playlist.songs[song]);
}
playlist.idToPosition[playlist.positionToId[song]] = -1;
/* delete song from songs array */
for (i = song; i < playlist.length - 1; i++) {
moveSongFromTo(i + 1, i);
}
/* now find it in the order array */
for (i = 0; i < playlist.length - 1; i++) {
if (playlist.order[i] == song)
break;
}
songOrder = i;
/* delete the entry from the order array */
for (; i < playlist.length - 1; i++)
playlist.order[i] = playlist.order[i + 1];
/* readjust values in the order array */
for (i = 0; i < playlist.length - 1; i++) {
if (playlist.order[i] > song)
playlist.order[i]--;
}
/* now take care of other misc stuff */
playlist.songs[playlist.length - 1] = NULL;
playlist.length--;
incrPlaylistVersion();
if (playlist_state != PLAYLIST_STATE_STOP
&& playlist.current == songOrder) {
/*if(playlist.current>=playlist.length) return playerStop(fd);
else return playPlaylistOrderNumber(fd,playlist.current); */
playerWait();
playlist_noGoToNext = 1;
}
if (playlist.current > songOrder) {
playlist.current--;
} else if (playlist.current >= playlist.length) {
incrPlaylistCurrent();
}
if (playlist.queued > songOrder) {
playlist.queued--;
}
return PLAYLIST_RESULT_SUCCESS;
}
enum playlist_result deleteFromPlaylistById(int id)
{
if (!song_id_exists(id))
return PLAYLIST_RESULT_NO_SUCH_SONG;
return deleteFromPlaylist(playlist.idToPosition[id]);
}
void deleteASongFromPlaylist(Song * song)
{
int i;
if (NULL == playlist.songs)
return;
for (i = 0; i < playlist.length; i++) {
if (song == playlist.songs[i]) {
deleteFromPlaylist(i);
}
}
}
void stopPlaylist(void)
{
DEBUG("playlist: stop\n");
playerWait();
playlist.queued = -1;
playlist_state = PLAYLIST_STATE_STOP;
playlist_noGoToNext = 0;
if (playlist.random)
randomizeOrder(0, playlist.length - 1);
}
static void playPlaylistOrderNumber(int orderNum)
{
Merge branches/ew r7104 thread-safety work in preparation for rewrite to use pthreads Expect no regressions against trunk (r7078), possibly minor performance improvements in update (due to fewer heap allocations), but increased stack usage. Applied the following patches: * maxpath_str for reentrancy (temporary fix, reverted) * path: start working on thread-safe variants of these methods * Re-entrancy work on path/character-set conversions * directory.c: exploreDirectory() use reentrant functions here * directory/update: more use of reentrant functions + cleanups * string_toupper: a strdup-less version of strDupToUpper * get_song_url: a static-variable-free version of getSongUrl() * Use reentrant/thread-safe get_song_url everywhere * replace rmp2amp with the reentrant version, rmp2amp_r * Get rid of the non-reentrant/non-thread-safe rpp2app, too. * buffer2array: assert strdup() returns a usable value in unit tests * replace utf8ToFsCharset and fsCharsetToUtf8 with thread-safe variants * fix storing playlists w/o absolute paths * parent_path(), a reentrant version of parentPath() * parentPath => parent_path for reentrancy and thread-safety * allow "make test" to automatically run embedded unit tests * remove convStrDup() and maxpath_str() * use MPD_PATH_MAX everywhere instead of MAXPATHLEN * path: get rid of appendSlash, pfx_path and just use pfx_dir * get_song_url: fix the ability to play songs in the top-level music_directory git-svn-id: https://svn.musicpd.org/mpd/trunk@7106 09075e82-0dd4-0310-85a5-a0d7c8717e4f
2007-12-28 03:56:25 +01:00
char path_max_tmp[MPD_PATH_MAX];
playlist_state = PLAYLIST_STATE_PLAY;
playlist_noGoToNext = 0;
playlist.queued = -1;
DEBUG("playlist: play %i:\"%s\"\n", orderNum,
Merge branches/ew r7104 thread-safety work in preparation for rewrite to use pthreads Expect no regressions against trunk (r7078), possibly minor performance improvements in update (due to fewer heap allocations), but increased stack usage. Applied the following patches: * maxpath_str for reentrancy (temporary fix, reverted) * path: start working on thread-safe variants of these methods * Re-entrancy work on path/character-set conversions * directory.c: exploreDirectory() use reentrant functions here * directory/update: more use of reentrant functions + cleanups * string_toupper: a strdup-less version of strDupToUpper * get_song_url: a static-variable-free version of getSongUrl() * Use reentrant/thread-safe get_song_url everywhere * replace rmp2amp with the reentrant version, rmp2amp_r * Get rid of the non-reentrant/non-thread-safe rpp2app, too. * buffer2array: assert strdup() returns a usable value in unit tests * replace utf8ToFsCharset and fsCharsetToUtf8 with thread-safe variants * fix storing playlists w/o absolute paths * parent_path(), a reentrant version of parentPath() * parentPath => parent_path for reentrancy and thread-safety * allow "make test" to automatically run embedded unit tests * remove convStrDup() and maxpath_str() * use MPD_PATH_MAX everywhere instead of MAXPATHLEN * path: get rid of appendSlash, pfx_path and just use pfx_dir * get_song_url: fix the ability to play songs in the top-level music_directory git-svn-id: https://svn.musicpd.org/mpd/trunk@7106 09075e82-0dd4-0310-85a5-a0d7c8717e4f
2007-12-28 03:56:25 +01:00
get_song_url(path_max_tmp,
playlist.songs[playlist.order[orderNum]]));
playerPlay(playlist.songs[playlist.order[orderNum]]);
playlist.current = orderNum;
}
enum playlist_result playPlaylist(int song, int stopOnError)
{
int i = song;
clearPlayerError();
if (song == -1) {
if (playlist.length == 0)
return PLAYLIST_RESULT_SUCCESS;
if (playlist_state == PLAYLIST_STATE_PLAY) {
playerSetPause(0);
return PLAYLIST_RESULT_SUCCESS;
}
if (playlist.current >= 0 && playlist.current < playlist.length) {
i = playlist.current;
} else {
i = 0;
}
} else if (song < 0 || song >= playlist.length) {
return PLAYLIST_RESULT_BAD_RANGE;
}
if (playlist.random) {
if (song == -1 && playlist_state == PLAYLIST_STATE_PLAY) {
randomizeOrder(0, playlist.length - 1);
} else {
if (song >= 0)
for (i = 0; song != playlist.order[i]; i++) ;
if (playlist_state == PLAYLIST_STATE_STOP) {
playlist.current = 0;
}
swapOrder(i, playlist.current);
i = playlist.current;
}
}
playlist_stopOnError = stopOnError;
playlist_errorCount = 0;
playPlaylistOrderNumber(i);
return PLAYLIST_RESULT_SUCCESS;
}
enum playlist_result playPlaylistById(int id, int stopOnError)
{
if (id == -1) {
return playPlaylist(id, stopOnError);
}
if (!song_id_exists(id))
return PLAYLIST_RESULT_NO_SUCH_SONG;
return playPlaylist(playlist.idToPosition[id], stopOnError);
}
static void syncCurrentPlayerDecodeMetadata(void)
{
Song *songPlayer = playerCurrentDecodeSong();
Song *song;
int songNum;
Merge branches/ew r7104 thread-safety work in preparation for rewrite to use pthreads Expect no regressions against trunk (r7078), possibly minor performance improvements in update (due to fewer heap allocations), but increased stack usage. Applied the following patches: * maxpath_str for reentrancy (temporary fix, reverted) * path: start working on thread-safe variants of these methods * Re-entrancy work on path/character-set conversions * directory.c: exploreDirectory() use reentrant functions here * directory/update: more use of reentrant functions + cleanups * string_toupper: a strdup-less version of strDupToUpper * get_song_url: a static-variable-free version of getSongUrl() * Use reentrant/thread-safe get_song_url everywhere * replace rmp2amp with the reentrant version, rmp2amp_r * Get rid of the non-reentrant/non-thread-safe rpp2app, too. * buffer2array: assert strdup() returns a usable value in unit tests * replace utf8ToFsCharset and fsCharsetToUtf8 with thread-safe variants * fix storing playlists w/o absolute paths * parent_path(), a reentrant version of parentPath() * parentPath => parent_path for reentrancy and thread-safety * allow "make test" to automatically run embedded unit tests * remove convStrDup() and maxpath_str() * use MPD_PATH_MAX everywhere instead of MAXPATHLEN * path: get rid of appendSlash, pfx_path and just use pfx_dir * get_song_url: fix the ability to play songs in the top-level music_directory git-svn-id: https://svn.musicpd.org/mpd/trunk@7106 09075e82-0dd4-0310-85a5-a0d7c8717e4f
2007-12-28 03:56:25 +01:00
char path_max_tmp[MPD_PATH_MAX];
if (!songPlayer)
return;
if (playlist_state != PLAYLIST_STATE_PLAY)
return;
songNum = playlist.order[playlist.current];
song = playlist.songs[songNum];
if (song->type == SONG_TYPE_URL &&
Merge branches/ew r7104 thread-safety work in preparation for rewrite to use pthreads Expect no regressions against trunk (r7078), possibly minor performance improvements in update (due to fewer heap allocations), but increased stack usage. Applied the following patches: * maxpath_str for reentrancy (temporary fix, reverted) * path: start working on thread-safe variants of these methods * Re-entrancy work on path/character-set conversions * directory.c: exploreDirectory() use reentrant functions here * directory/update: more use of reentrant functions + cleanups * string_toupper: a strdup-less version of strDupToUpper * get_song_url: a static-variable-free version of getSongUrl() * Use reentrant/thread-safe get_song_url everywhere * replace rmp2amp with the reentrant version, rmp2amp_r * Get rid of the non-reentrant/non-thread-safe rpp2app, too. * buffer2array: assert strdup() returns a usable value in unit tests * replace utf8ToFsCharset and fsCharsetToUtf8 with thread-safe variants * fix storing playlists w/o absolute paths * parent_path(), a reentrant version of parentPath() * parentPath => parent_path for reentrancy and thread-safety * allow "make test" to automatically run embedded unit tests * remove convStrDup() and maxpath_str() * use MPD_PATH_MAX everywhere instead of MAXPATHLEN * path: get rid of appendSlash, pfx_path and just use pfx_dir * get_song_url: fix the ability to play songs in the top-level music_directory git-svn-id: https://svn.musicpd.org/mpd/trunk@7106 09075e82-0dd4-0310-85a5-a0d7c8717e4f
2007-12-28 03:56:25 +01:00
0 == strcmp(get_song_url(path_max_tmp, song), songPlayer->url) &&
2008-08-29 09:38:21 +02:00
!tag_equal(song->tag, songPlayer->tag)) {
if (song->tag)
2008-08-29 09:38:21 +02:00
tag_free(song->tag);
song->tag = tag_dup(songPlayer->tag);
playlist.songMod[songNum] = playlist.version;
incrPlaylistVersion();
}
}
void syncPlayerAndPlaylist(void)
{
if (playlist_state != PLAYLIST_STATE_PLAY)
return;
if (getPlayerState() == PLAYER_STATE_STOP)
playPlaylistIfPlayerStopped();
else {
syncPlaylistWithQueue();
if (getPlayerQueueState() == PLAYER_QUEUE_BLANK)
queueNextSongInPlaylist();
}
syncCurrentPlayerDecodeMetadata();
}
static void currentSongInPlaylist(void)
{
if (playlist_state != PLAYLIST_STATE_PLAY)
return;
playlist_stopOnError = 0;
syncPlaylistWithQueue();
if (playlist.current >= 0 && playlist.current < playlist.length)
playPlaylistOrderNumber(playlist.current);
else
stopPlaylist();
}
void nextSongInPlaylist(void)
{
if (playlist_state != PLAYLIST_STATE_PLAY)
return;
syncPlaylistWithQueue();
playlist_stopOnError = 0;
if (playlist.current < playlist.length - 1) {
playPlaylistOrderNumber(playlist.current + 1);
} else if (playlist.length && playlist.repeat) {
if (playlist.random)
randomizeOrder(0, playlist.length - 1);
playPlaylistOrderNumber(0);
} else {
incrPlaylistCurrent();
stopPlaylist();
}
}
void playPlaylistIfPlayerStopped(void)
{
if (getPlayerState() == PLAYER_STATE_STOP) {
int error = getPlayerError();
if (error == PLAYER_ERROR_NOERROR)
playlist_errorCount = 0;
else
playlist_errorCount++;
if (playlist_state == PLAYLIST_STATE_PLAY
&& ((playlist_stopOnError && error != PLAYER_ERROR_NOERROR)
|| error == PLAYER_ERROR_AUDIO
|| error == PLAYER_ERROR_SYSTEM
|| playlist_errorCount >= playlist.length)) {
stopPlaylist();
} else if (playlist_noGoToNext)
currentSongInPlaylist();
else
nextSongInPlaylist();
}
}
int getPlaylistRepeatStatus(void)
{
return playlist.repeat;
}
int getPlaylistRandomStatus(void)
{
return playlist.random;
}
void setPlaylistRepeatStatus(int status)
{
if (playlist_state == PLAYLIST_STATE_PLAY) {
if (playlist.repeat && !status && playlist.queued == 0)
clearPlayerQueue();
}
playlist.repeat = status;
}
enum playlist_result moveSongInPlaylist(int from, int to)
{
int i;
Song *tmpSong;
int tmpId;
int currentSong;
if (from < 0 || from >= playlist.length)
return PLAYLIST_RESULT_BAD_RANGE;
if ((to >= 0 && to >= playlist.length) ||
(to < 0 && abs(to) > playlist.length))
return PLAYLIST_RESULT_BAD_RANGE;
if (from == to) /* no-op */
return PLAYLIST_RESULT_SUCCESS;
/*
* (to < 0) => move to offset from current song
* (-playlist.length == to) => move to position BEFORE current song
*/
currentSong = playlist.order[playlist.current];
if (to < 0 && playlist.current >= 0) {
if (currentSong == from)
/* no-op, can't be moved to offset of itself */
return PLAYLIST_RESULT_SUCCESS;
to = (currentSong + abs(to)) % playlist.length;
}
if (playlist_state == PLAYLIST_STATE_PLAY) {
int queuedSong = -1;
if (playlist.queued >= 0)
queuedSong = playlist.order[playlist.queued];
if (queuedSong == from || queuedSong == to
|| currentSong == from || currentSong == to)
clearPlayerQueue();
}
tmpSong = playlist.songs[from];
tmpId = playlist.positionToId[from];
/* move songs to one less in from->to */
for (i = from; i < to; i++) {
moveSongFromTo(i + 1, i);
}
/* move songs to one more in to->from */
for (i = from; i > to; i--) {
moveSongFromTo(i - 1, i);
}
/* put song at _to_ */
playlist.idToPosition[tmpId] = to;
playlist.positionToId[to] = tmpId;
playlist.songs[to] = tmpSong;
playlist.songMod[to] = playlist.version;
/* now deal with order */
if (playlist.random) {
for (i = 0; i < playlist.length; i++) {
if (playlist.order[i] > from && playlist.order[i] <= to) {
playlist.order[i]--;
} else if (playlist.order[i] < from &&
playlist.order[i] >= to) {
playlist.order[i]++;
} else if (from == playlist.order[i]) {
playlist.order[i] = to;
}
}
}
else
{
if (playlist.current == from)
playlist.current = to;
else if (playlist.current > from && playlist.current <= to) {
playlist.current--;
} else if (playlist.current >= to && playlist.current < from) {
playlist.current++;
}
/* this first if statement isn't necessary since the queue
* would have been cleared out if queued == from */
if (playlist.queued == from)
playlist.queued = to;
else if (playlist.queued > from && playlist.queued <= to) {
playlist.queued--;
} else if (playlist.queued>= to && playlist.queued < from) {
playlist.queued++;
}
}
incrPlaylistVersion();
return PLAYLIST_RESULT_SUCCESS;
}
enum playlist_result moveSongInPlaylistById(int id1, int to)
{
if (!song_id_exists(id1))
return PLAYLIST_RESULT_NO_SUCH_SONG;
return moveSongInPlaylist(playlist.idToPosition[id1], to);
}
static void orderPlaylist(void)
{
int i;
if (playlist.current >= 0 && playlist.current < playlist.length) {
playlist.current = playlist.order[playlist.current];
}
if (playlist_state == PLAYLIST_STATE_PLAY) {
if (playlist.queued >= 0)
clearPlayerQueue();
}
for (i = 0; i < playlist.length; i++) {
playlist.order[i] = i;
}
}
static void swapOrder(int a, int b)
{
int bak = playlist.order[a];
playlist.order[a] = playlist.order[b];
playlist.order[b] = bak;
}
static void randomizeOrder(int start, int end)
{
int i;
int ri;
DEBUG("playlist: randomize from %i to %i\n", start, end);
if (playlist_state == PLAYLIST_STATE_PLAY) {
if (playlist.queued >= start && playlist.queued <= end)
clearPlayerQueue();
}
for (i = start; i <= end; i++) {
ri = random() % (end - start + 1) + start;
if (ri == playlist.current)
playlist.current = i;
else if (i == playlist.current)
playlist.current = ri;
swapOrder(i, ri);
}
}
void setPlaylistRandomStatus(int status)
{
int statusWas = playlist.random;
playlist.random = status;
if (status != statusWas) {
if (playlist.random) {
/*if(playlist_state==PLAYLIST_STATE_PLAY) {
randomizeOrder(playlist.current+1,
playlist.length-1);
}
else */ randomizeOrder(0, playlist.length - 1);
if (playlist.current >= 0 &&
playlist.current < playlist.length) {
swapOrder(playlist.current, 0);
playlist.current = 0;
}
} else
orderPlaylist();
}
}
void previousSongInPlaylist(void)
{
static time_t lastTime;
time_t diff = time(NULL) - lastTime;
lastTime += diff;
if (playlist_state != PLAYLIST_STATE_PLAY)
return;
syncPlaylistWithQueue();
if (diff && getPlayerElapsedTime() > PLAYLIST_PREV_UNLESS_ELAPSED) {
playPlaylistOrderNumber(playlist.current);
} else {
if (playlist.current > 0) {
playPlaylistOrderNumber(playlist.current - 1);
} else if (playlist.repeat) {
playPlaylistOrderNumber(playlist.length - 1);
} else {
playPlaylistOrderNumber(playlist.current);
}
}
}
void shufflePlaylist(void)
{
int i;
int ri;
if (playlist.length > 1) {
if (playlist_state == PLAYLIST_STATE_PLAY) {
clearPlayerQueue();
/* put current playing song first */
swapSongs(0, playlist.order[playlist.current]);
if (playlist.random) {
int j;
for (j = 0; 0 != playlist.order[j]; j++) ;
playlist.current = j;
} else
playlist.current = 0;
i = 1;
} else {
i = 0;
playlist.current = -1;
}
/* shuffle the rest of the list */
for (; i < playlist.length; i++) {
ri = random() % (playlist.length - 1) + 1;
swapSongs(i, ri);
}
incrPlaylistVersion();
}
}
enum playlist_result deletePlaylist(const char *utf8file)
{
Merge branches/ew r7104 thread-safety work in preparation for rewrite to use pthreads Expect no regressions against trunk (r7078), possibly minor performance improvements in update (due to fewer heap allocations), but increased stack usage. Applied the following patches: * maxpath_str for reentrancy (temporary fix, reverted) * path: start working on thread-safe variants of these methods * Re-entrancy work on path/character-set conversions * directory.c: exploreDirectory() use reentrant functions here * directory/update: more use of reentrant functions + cleanups * string_toupper: a strdup-less version of strDupToUpper * get_song_url: a static-variable-free version of getSongUrl() * Use reentrant/thread-safe get_song_url everywhere * replace rmp2amp with the reentrant version, rmp2amp_r * Get rid of the non-reentrant/non-thread-safe rpp2app, too. * buffer2array: assert strdup() returns a usable value in unit tests * replace utf8ToFsCharset and fsCharsetToUtf8 with thread-safe variants * fix storing playlists w/o absolute paths * parent_path(), a reentrant version of parentPath() * parentPath => parent_path for reentrancy and thread-safety * allow "make test" to automatically run embedded unit tests * remove convStrDup() and maxpath_str() * use MPD_PATH_MAX everywhere instead of MAXPATHLEN * path: get rid of appendSlash, pfx_path and just use pfx_dir * get_song_url: fix the ability to play songs in the top-level music_directory git-svn-id: https://svn.musicpd.org/mpd/trunk@7106 09075e82-0dd4-0310-85a5-a0d7c8717e4f
2007-12-28 03:56:25 +01:00
char path_max_tmp[MPD_PATH_MAX];
utf8_to_fs_playlist_path(path_max_tmp, utf8file);
if (!isPlaylist(path_max_tmp))
return PLAYLIST_RESULT_NO_SUCH_LIST;
if (unlink(path_max_tmp) < 0)
return PLAYLIST_RESULT_ERRNO;
return PLAYLIST_RESULT_SUCCESS;
}
enum playlist_result savePlaylist(const char *utf8file)
{
FILE *fp;
int i;
struct stat sb;
char path_max_tmp[MPD_PATH_MAX];
if (!is_valid_playlist_name(utf8file))
return PLAYLIST_RESULT_BAD_NAME;
utf8_to_fs_playlist_path(path_max_tmp, utf8file);
if (!stat(path_max_tmp, &sb))
return PLAYLIST_RESULT_LIST_EXISTS;
while (!(fp = fopen(path_max_tmp, "w")) && errno == EINTR);
if (fp == NULL)
return PLAYLIST_RESULT_ERRNO;
for (i = 0; i < playlist.length; i++) {
char tmp[MPD_PATH_MAX];
get_song_url(path_max_tmp, playlist.songs[i]);
utf8_to_fs_charset(tmp, path_max_tmp);
if (playlist_saveAbsolutePaths &&
playlist.songs[i]->type == SONG_TYPE_FILE)
fprintf(fp, "%s\n", rmp2amp_r(tmp, tmp));
else
fprintf(fp, "%s\n", tmp);
}
while (fclose(fp) && errno == EINTR) ;
return PLAYLIST_RESULT_SUCCESS;
}
int getPlaylistCurrentSong(void)
{
if (playlist.current >= 0 && playlist.current < playlist.length) {
return playlist.order[playlist.current];
}
return -1;
}
unsigned long getPlaylistVersion(void)
{
return playlist.version;
}
int getPlaylistLength(void)
{
return playlist.length;
}
enum playlist_result seekSongInPlaylist(int song, float seek_time)
{
int i, ret;
if (song < 0 || song >= playlist.length)
return PLAYLIST_RESULT_BAD_RANGE;
if (playlist.random)
for (i = 0; song != playlist.order[i]; i++) ;
else
i = song;
clearPlayerError();
playlist_stopOnError = 1;
playlist_errorCount = 0;
if (playlist_state == PLAYLIST_STATE_PLAY) {
if (playlist.queued >= 0)
clearPlayerQueue();
} else
playPlaylistOrderNumber(i);
if (playlist.current != i) {
playPlaylistOrderNumber(i);
}
ret = playerSeek(playlist.songs[playlist.order[i]], seek_time);
if (ret < 0)
return PLAYLIST_RESULT_NOT_PLAYING;
return PLAYLIST_RESULT_SUCCESS;
}
enum playlist_result seekSongInPlaylistById(int id, float seek_time)
{
if (!song_id_exists(id))
return PLAYLIST_RESULT_NO_SUCH_SONG;
return seekSongInPlaylist(playlist.idToPosition[id], seek_time);
}
int getPlaylistSongId(int song)
{
return playlist.positionToId[song];
}
int PlaylistInfo(int fd, const char *utf8file, int detail)
{
ListNode *node;
List *list;
if (!(list = loadStoredPlaylist(utf8file))) {
commandError(fd, ACK_ERROR_NO_EXIST, "could not open playlist "
"\"%s\": %s", utf8file, strerror(errno));
return -1;
}
node = list->firstNode;
while (node != NULL) {
char *temp = node->data;
int wrote = 0;
if (detail) {
Song *song = getSongFromDB(temp);
if (song) {
printSongInfo(fd, song);
wrote = 1;
}
}
if (!wrote) {
fdprintf(fd, SONG_FILE "%s\n", temp);
}
node = node->nextNode;
}
freeList(list);
return 0;
}
enum playlist_result loadPlaylist(int fd, const char *utf8file)
{
ListNode *node;
List *list;
if (!(list = loadStoredPlaylist(utf8file)))
return PLAYLIST_RESULT_NO_SUCH_LIST;
node = list->firstNode;
while (node != NULL) {
char *temp = node->data;
if ((addToPlaylist(temp, NULL)) != PLAYLIST_RESULT_SUCCESS) {
/* for windows compatibility, convert slashes */
char *temp2 = xstrdup(temp);
char *p = temp2;
while (*p) {
if (*p == '\\')
*p = '/';
p++;
}
if ((addToPlaylist(temp, NULL)) != PLAYLIST_RESULT_SUCCESS) {
commandError(fd, ACK_ERROR_PLAYLIST_LOAD,
"can't add file \"%s\"", temp2);
}
free(temp2);
}
node = node->nextNode;
}
freeList(list);
return PLAYLIST_RESULT_SUCCESS;
}
void searchForSongsInPlaylist(int fd, int numItems, LocateTagItem * items)
{
int i;
char **originalNeedles = xmalloc(numItems * sizeof(char *));
for (i = 0; i < numItems; i++) {
originalNeedles[i] = items[i].needle;
items[i].needle = strDupToUpper(originalNeedles[i]);
}
for (i = 0; i < playlist.length; i++) {
if (strstrSearchTags(playlist.songs[i], numItems, items))
printPlaylistSongInfo(fd, i);
}
for (i = 0; i < numItems; i++) {
free(items[i].needle);
items[i].needle = originalNeedles[i];
}
free(originalNeedles);
}
void findSongsInPlaylist(int fd, int numItems, LocateTagItem * items)
{
int i;
for (i = 0; i < playlist.length; i++) {
if (tagItemsFoundAndMatches(playlist.songs[i], numItems, items))
printPlaylistSongInfo(fd, i);
}
}
/*
* Not supporting '/' was done out of laziness, and we should really
* strive to support it in the future.
*
* Not supporting '\r' and '\n' is done out of protocol limitations (and
* arguably laziness), but bending over head over heels to modify the
* protocol (and compatibility with all clients) to support idiots who
* put '\r' and '\n' in filenames isn't going to happen, either.
*/
int is_valid_playlist_name(const char *utf8path)
{
return strchr(utf8path, '/') == NULL &&
strchr(utf8path, '\n') == NULL &&
strchr(utf8path, '\r') == NULL;
}