2004-02-24 00:41:20 +01:00
|
|
|
/* the Music Player Daemon (MPD)
|
2007-04-05 05:22:33 +02:00
|
|
|
* Copyright (C) 2003-2007 by Warren Dukes (warren.dukes@gmail.com)
|
2004-02-24 00:41:20 +01:00
|
|
|
* 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.h"
|
|
|
|
#include "command.h"
|
|
|
|
#include "ls.h"
|
|
|
|
#include "tag.h"
|
|
|
|
#include "conf.h"
|
|
|
|
#include "directory.h"
|
|
|
|
#include "log.h"
|
|
|
|
#include "path.h"
|
|
|
|
#include "utils.h"
|
|
|
|
#include "sig_handlers.h"
|
2006-07-31 01:32:47 +02:00
|
|
|
#include "state_file.h"
|
2007-05-16 14:02:10 +02:00
|
|
|
#include "storedPlaylist.h"
|
2004-02-24 00:41:20 +01:00
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <time.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"
|
|
|
|
|
|
|
|
#define PLAYLIST_BUFFER_SIZE 2*MAXPATHLEN
|
|
|
|
|
2004-06-09 02:18:31 +02:00
|
|
|
#define PLAYLIST_HASH_MULT 4
|
|
|
|
|
2004-10-28 07:14:55 +02:00
|
|
|
#define DEFAULT_PLAYLIST_MAX_LENGTH (1024*16)
|
|
|
|
#define DEFAULT_PLAYLIST_SAVE_ABSOLUTE_PATHS 0
|
|
|
|
|
2004-06-02 22:40:30 +02:00
|
|
|
static Playlist playlist;
|
|
|
|
static int playlist_state = PLAYLIST_STATE_STOP;
|
2004-10-28 07:14:55 +02:00
|
|
|
static int playlist_max_length = DEFAULT_PLAYLIST_MAX_LENGTH;
|
2004-06-02 22:40:30 +02:00
|
|
|
static int playlist_stopOnError;
|
2007-01-14 04:07:53 +01:00
|
|
|
static int playlist_errorCount;
|
2004-06-02 22:40:30 +02:00
|
|
|
static int playlist_queueError;
|
2007-01-14 04:07:53 +01:00
|
|
|
static int playlist_noGoToNext;
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2007-05-16 14:02:10 +02:00
|
|
|
int playlist_saveAbsolutePaths = DEFAULT_PLAYLIST_SAVE_ABSOLUTE_PATHS;
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2004-06-02 22:40:30 +02:00
|
|
|
static void swapOrder(int a, int b);
|
2006-07-30 05:43:38 +02:00
|
|
|
static int playPlaylistOrderNumber(int fd, int orderNum);
|
2004-06-02 22:40:30 +02:00
|
|
|
static void randomizeOrder(int start, int end);
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
static void incrPlaylistVersion(void)
|
|
|
|
{
|
|
|
|
static unsigned long max = ((mpd_uint32) 1 << 31) - 1;
|
2004-02-24 00:41:20 +01:00
|
|
|
playlist.version++;
|
2006-07-20 18:02:40 +02:00
|
|
|
if (playlist.version >= max) {
|
2004-06-05 03:14:37 +02:00
|
|
|
int i;
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
for (i = 0; i < playlist.length; i++) {
|
2004-06-05 03:14:37 +02:00
|
|
|
playlist.songMod[i] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
playlist.version = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
void playlistVersionChange(void)
|
|
|
|
{
|
2004-06-05 03:14:37 +02:00
|
|
|
int i = 0;
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
for (i = 0; i < playlist.length; i++) {
|
2004-06-05 03:14:37 +02:00
|
|
|
playlist.songMod[i] = playlist.version;
|
|
|
|
}
|
|
|
|
|
|
|
|
incrPlaylistVersion();
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
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++;
|
2004-06-02 22:40:30 +02:00
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
void initPlaylist(void)
|
|
|
|
{
|
|
|
|
char *test;
|
2004-06-09 02:18:31 +02:00
|
|
|
int i;
|
2006-07-20 18:02:40 +02:00
|
|
|
ConfigParam *param;
|
2004-02-24 00:41:20 +01:00
|
|
|
|
|
|
|
playlist.length = 0;
|
|
|
|
playlist.repeat = 0;
|
2004-06-05 03:14:37 +02:00
|
|
|
playlist.version = 1;
|
2004-02-24 00:41:20 +01:00
|
|
|
playlist.random = 0;
|
|
|
|
playlist.queued = -1;
|
2006-07-20 18:02:40 +02:00
|
|
|
playlist.current = -1;
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2004-10-28 07:14:55 +02:00
|
|
|
param = getConfigParam(CONF_MAX_PLAYLIST_LENGTH);
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (param) {
|
2004-10-28 07:14:55 +02:00
|
|
|
playlist_max_length = strtol(param->value, &test, 10);
|
2006-07-20 18:02:40 +02:00
|
|
|
if (*test != '\0') {
|
2004-10-28 07:14:55 +02:00
|
|
|
ERROR("max playlist length \"%s\" is not an integer, "
|
2006-07-20 18:02:40 +02:00
|
|
|
"line %i\n", param->value, param->line);
|
2004-10-28 07:14:55 +02:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
2004-10-28 07:14:55 +02:00
|
|
|
|
2006-07-21 19:11:45 +02:00
|
|
|
playlist_saveAbsolutePaths = getBoolConfigParam(CONF_SAVE_ABSOLUTE_PATHS);
|
2006-12-23 17:37:36 +01:00
|
|
|
if (playlist_saveAbsolutePaths == -1) playlist_saveAbsolutePaths = DEFAULT_PLAYLIST_SAVE_ABSOLUTE_PATHS;
|
2006-07-27 02:50:59 +02:00
|
|
|
else if (playlist_saveAbsolutePaths < 0) exit(EXIT_FAILURE);
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2006-08-26 08:25:57 +02:00
|
|
|
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 *
|
2006-07-20 18:02:40 +02:00
|
|
|
PLAYLIST_HASH_MULT);
|
2006-08-26 08:25:57 +02:00
|
|
|
playlist.positionToId = xmalloc(sizeof(int) * playlist_max_length);
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
memset(playlist.songs, 0, sizeof(char *) * playlist_max_length);
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2004-10-30 16:33:19 +02:00
|
|
|
srandom(time(NULL));
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
for (i = 0; i < playlist_max_length * PLAYLIST_HASH_MULT; i++) {
|
2004-06-30 04:12:49 +02:00
|
|
|
playlist.idToPosition[i] = -1;
|
2004-06-09 02:18:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
static int getNextId(void)
|
|
|
|
{
|
2006-05-08 10:15:24 +02:00
|
|
|
static int cur = -1;
|
2004-06-09 02:18:31 +02:00
|
|
|
|
2006-05-08 10:15:24 +02:00
|
|
|
do {
|
2004-06-09 02:18:31 +02:00
|
|
|
cur++;
|
2006-07-20 18:02:40 +02:00
|
|
|
if (cur >= playlist_max_length * PLAYLIST_HASH_MULT) {
|
2004-06-09 02:18:31 +02:00
|
|
|
cur = 0;
|
|
|
|
}
|
2006-07-20 18:02:40 +02:00
|
|
|
} while (playlist.idToPosition[cur] != -1);
|
2004-06-09 02:18:31 +02:00
|
|
|
|
|
|
|
return cur;
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
void finishPlaylist(void)
|
|
|
|
{
|
2006-07-14 23:01:19 +02:00
|
|
|
int i;
|
2006-07-20 18:02:40 +02:00
|
|
|
for (i = 0; i < playlist.length; i++) {
|
|
|
|
if (playlist.songs[i]->type == SONG_TYPE_URL) {
|
2004-05-25 21:01:58 +02:00
|
|
|
freeJustSong(playlist.songs[i]);
|
2004-05-19 07:07:31 +02:00
|
|
|
}
|
2006-07-14 23:01:19 +02:00
|
|
|
}
|
2004-06-05 03:14:37 +02:00
|
|
|
|
|
|
|
playlist.length = 0;
|
|
|
|
|
2004-02-24 00:41:20 +01:00
|
|
|
free(playlist.songs);
|
2004-04-11 03:53:25 +02:00
|
|
|
playlist.songs = NULL;
|
2004-06-05 03:14:37 +02:00
|
|
|
free(playlist.songMod);
|
|
|
|
playlist.songMod = NULL;
|
2004-02-24 00:41:20 +01:00
|
|
|
free(playlist.order);
|
2004-04-11 03:53:25 +02:00
|
|
|
playlist.order = NULL;
|
2004-06-30 04:12:49 +02:00
|
|
|
free(playlist.idToPosition);
|
|
|
|
playlist.idToPosition = NULL;
|
|
|
|
free(playlist.positionToId);
|
|
|
|
playlist.positionToId = NULL;
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
2006-07-30 05:43:38 +02:00
|
|
|
int clearPlaylist(int fd)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2004-02-24 00:41:20 +01:00
|
|
|
int i;
|
|
|
|
|
2006-07-30 05:43:38 +02:00
|
|
|
if (stopPlaylist(fd) < 0)
|
2006-07-20 18:02:40 +02:00
|
|
|
return -1;
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
for (i = 0; i < playlist.length; i++) {
|
|
|
|
if (playlist.songs[i]->type == SONG_TYPE_URL) {
|
2004-05-25 21:01:58 +02:00
|
|
|
freeJustSong(playlist.songs[i]);
|
2004-05-13 20:46:38 +02:00
|
|
|
}
|
2004-06-30 04:12:49 +02:00
|
|
|
playlist.idToPosition[playlist.positionToId[i]] = -1;
|
2004-05-13 20:46:38 +02:00
|
|
|
playlist.songs[i] = NULL;
|
|
|
|
}
|
2004-02-24 00:41:20 +01:00
|
|
|
playlist.length = 0;
|
2006-07-14 23:01:19 +02:00
|
|
|
playlist.current = -1;
|
2004-02-24 00:41:20 +01:00
|
|
|
|
|
|
|
incrPlaylistVersion();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-11-20 16:37:58 +01:00
|
|
|
int clearStoredPlaylist(int fd, char *utf8file)
|
|
|
|
{
|
2007-05-16 14:02:10 +02:00
|
|
|
removeAllFromStoredPlaylistByPath(fd, utf8file);
|
2006-11-20 16:37:58 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-07-30 05:43:38 +02:00
|
|
|
int showPlaylist(int fd)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2004-02-24 00:41:20 +01:00
|
|
|
int i;
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
for (i = 0; i < playlist.length; i++) {
|
2006-07-30 05:43:38 +02:00
|
|
|
fdprintf(fd, "%i:%s\n", i, getSongUrl(playlist.songs[i]));
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-07-31 01:32:47 +02:00
|
|
|
void savePlaylistState(FILE *fp)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2006-07-31 01:32:47 +02:00
|
|
|
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);
|
2004-02-24 00:41:20 +01:00
|
|
|
break;
|
|
|
|
default:
|
2006-07-31 01:32:47 +02:00
|
|
|
fprintf(fp, "%s\n", PLAYLIST_STATE_FILE_STATE_PLAY);
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
2006-07-31 01:32:47 +02:00
|
|
|
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;
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
2006-07-31 01:32:47 +02:00
|
|
|
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);
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
2006-07-31 01:32:47 +02:00
|
|
|
static void loadPlaylistFromStateFile(FILE *fp, char *buffer,
|
|
|
|
int state, int current, int time)
|
2004-02-24 00:41:20 +01:00
|
|
|
{
|
2006-07-20 18:02:40 +02:00
|
|
|
char *temp;
|
2004-02-24 00:41:20 +01:00
|
|
|
int song;
|
|
|
|
|
2006-07-31 01:32:47 +02:00
|
|
|
if (!myFgets(buffer, PLAYLIST_BUFFER_SIZE, fp))
|
|
|
|
state_file_fatal();
|
2006-07-20 18:02:40 +02:00
|
|
|
while (strcmp(buffer, PLAYLIST_STATE_FILE_PLAYLIST_END)) {
|
|
|
|
song = atoi(strtok(buffer, ":"));
|
2006-07-31 01:32:47 +02:00
|
|
|
if (!(temp = strtok(NULL, "")))
|
|
|
|
state_file_fatal();
|
2006-07-30 05:43:38 +02:00
|
|
|
if (!addToPlaylist(STDERR_FILENO, temp, 0) && current == song) {
|
2006-07-20 18:02:40 +02:00
|
|
|
if (state != PLAYER_STATE_STOP) {
|
2006-07-30 05:43:38 +02:00
|
|
|
playPlaylist(STDERR_FILENO,
|
2006-07-31 01:32:47 +02:00
|
|
|
playlist.length - 1, 0);
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
2006-07-20 18:02:40 +02:00
|
|
|
if (state == PLAYER_STATE_PAUSE) {
|
2006-07-30 05:43:38 +02:00
|
|
|
playerPause(STDERR_FILENO);
|
2004-03-17 21:58:09 +01:00
|
|
|
}
|
2006-07-20 18:02:40 +02:00
|
|
|
if (state != PLAYER_STATE_STOP) {
|
2006-07-30 05:43:38 +02:00
|
|
|
seekSongInPlaylist(STDERR_FILENO,
|
2006-07-31 01:32:47 +02:00
|
|
|
playlist.length - 1, time);
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
}
|
2006-07-31 01:32:47 +02:00
|
|
|
if (!myFgets(buffer, PLAYLIST_BUFFER_SIZE, fp))
|
|
|
|
state_file_fatal();
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-07-31 01:32:47 +02:00
|
|
|
void readPlaylistState(FILE *fp)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2006-07-31 01:32:47 +02:00
|
|
|
int current = -1;
|
|
|
|
int 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;
|
2006-07-20 18:02:40 +02:00
|
|
|
} else
|
2006-07-31 01:32:47 +02:00
|
|
|
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) {
|
|
|
|
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(STDERR_FILENO, 1);
|
2006-07-20 18:02:40 +02:00
|
|
|
} else
|
2006-07-31 01:32:47 +02:00
|
|
|
setPlaylistRepeatStatus(STDERR_FILENO, 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(STDERR_FILENO, 1);
|
2006-07-20 18:02:40 +02:00
|
|
|
} else
|
2006-07-31 01:32:47 +02:00
|
|
|
setPlaylistRandomStatus(STDERR_FILENO, 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, time);
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-08-20 02:50:44 +02:00
|
|
|
static void printPlaylistSongInfo(int fd, int song)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2006-07-30 05:43:38 +02:00
|
|
|
printSongInfo(fd, playlist.songs[song]);
|
|
|
|
fdprintf(fd, "Pos: %i\nId: %i\n", song, playlist.positionToId[song]);
|
2004-06-05 03:14:37 +02:00
|
|
|
}
|
|
|
|
|
2006-07-30 05:43:38 +02:00
|
|
|
int playlistChanges(int fd, mpd_uint32 version)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2004-06-05 03:14:37 +02:00
|
|
|
int i;
|
2006-07-14 23:01:19 +02:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
for (i = 0; i < playlist.length; i++) {
|
|
|
|
if (version > playlist.version ||
|
|
|
|
playlist.songMod[i] >= version ||
|
|
|
|
playlist.songMod[i] == 0) {
|
2006-07-30 05:43:38 +02:00
|
|
|
printPlaylistSongInfo(fd, i);
|
2004-06-05 03:14:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-07-30 05:43:38 +02:00
|
|
|
int playlistChangesPosId(int fd, mpd_uint32 version)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2006-04-23 13:10:41 +02:00
|
|
|
int i;
|
2006-07-14 23:01:19 +02:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
for (i = 0; i < playlist.length; i++) {
|
|
|
|
if (version > playlist.version ||
|
|
|
|
playlist.songMod[i] >= version ||
|
|
|
|
playlist.songMod[i] == 0) {
|
2006-07-30 05:43:38 +02:00
|
|
|
fdprintf(fd, "cpos: %i\nId: %i\n",
|
|
|
|
i, playlist.positionToId[i]);
|
2006-04-23 13:10:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-07-30 05:43:38 +02:00
|
|
|
int playlistInfo(int fd, int song)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2004-02-24 00:41:20 +01:00
|
|
|
int i;
|
|
|
|
int begin = 0;
|
|
|
|
int end = playlist.length;
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (song >= 0) {
|
2004-02-24 00:41:20 +01:00
|
|
|
begin = song;
|
2006-07-20 18:02:40 +02:00
|
|
|
end = song + 1;
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
2006-07-20 18:02:40 +02:00
|
|
|
if (song >= playlist.length) {
|
2006-07-30 05:43:38 +02:00
|
|
|
commandError(fd, ACK_ERROR_NO_EXIST,
|
2006-07-20 18:02:40 +02:00
|
|
|
"song doesn't exist: \"%i\"", song);
|
2004-02-24 00:41:20 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
for (i = begin; i < end; i++)
|
2006-07-30 05:43:38 +02:00
|
|
|
printPlaylistSongInfo(fd, i);
|
2004-02-24 00:41:20 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-06-09 03:30:51 +02:00
|
|
|
# define checkSongId(id) { \
|
|
|
|
if(id < 0 || id >= PLAYLIST_HASH_MULT*playlist_max_length || \
|
2004-06-30 04:12:49 +02:00
|
|
|
playlist.idToPosition[id] == -1 ) \
|
2004-06-09 03:30:51 +02:00
|
|
|
{ \
|
2006-07-30 05:43:38 +02:00
|
|
|
commandError(fd, ACK_ERROR_NO_EXIST, \
|
2006-07-14 23:01:19 +02:00
|
|
|
"song id doesn't exist: \"%i\"", id); \
|
2004-06-09 03:30:51 +02:00
|
|
|
return -1; \
|
|
|
|
} \
|
|
|
|
}
|
|
|
|
|
2006-07-30 05:43:38 +02:00
|
|
|
int playlistId(int fd, int id)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2004-06-09 03:30:51 +02:00
|
|
|
int i;
|
|
|
|
int begin = 0;
|
|
|
|
int end = playlist.length;
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (id >= 0) {
|
2004-06-09 03:30:51 +02:00
|
|
|
checkSongId(id);
|
2004-06-30 04:12:49 +02:00
|
|
|
begin = playlist.idToPosition[id];
|
2006-07-20 18:02:40 +02:00
|
|
|
end = begin + 1;
|
2004-06-09 03:30:51 +02:00
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
for (i = begin; i < end; i++)
|
2006-07-30 05:43:38 +02:00
|
|
|
printPlaylistSongInfo(fd, i);
|
2004-06-09 03:30:51 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-08-20 02:50:44 +02:00
|
|
|
static void swapSongs(int song1, int song2)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
|
|
|
Song *sTemp;
|
2004-06-09 02:18:31 +02:00
|
|
|
int iTemp;
|
2006-07-20 18:02:40 +02:00
|
|
|
|
2004-06-09 02:18:31 +02:00
|
|
|
sTemp = playlist.songs[song1];
|
2004-02-24 00:41:20 +01:00
|
|
|
playlist.songs[song1] = playlist.songs[song2];
|
2004-06-09 02:18:31 +02:00
|
|
|
playlist.songs[song2] = sTemp;
|
|
|
|
|
2004-06-05 03:14:37 +02:00
|
|
|
playlist.songMod[song1] = playlist.version;
|
|
|
|
playlist.songMod[song2] = playlist.version;
|
2004-06-09 02:18:31 +02:00
|
|
|
|
2004-06-30 04:12:49 +02:00
|
|
|
playlist.idToPosition[playlist.positionToId[song1]] = song2;
|
|
|
|
playlist.idToPosition[playlist.positionToId[song2]] = song1;
|
2004-06-09 02:18:31 +02:00
|
|
|
|
2004-06-30 04:12:49 +02:00
|
|
|
iTemp = playlist.positionToId[song1];
|
|
|
|
playlist.positionToId[song1] = playlist.positionToId[song2];
|
|
|
|
playlist.positionToId[song2] = iTemp;
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
2006-08-20 02:50:44 +02:00
|
|
|
static void queueNextSongInPlaylist(void)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
|
|
|
if (playlist.current < playlist.length - 1) {
|
|
|
|
playlist.queued = playlist.current + 1;
|
2004-02-24 00:41:20 +01:00
|
|
|
DEBUG("playlist: queue song %i:\"%s\"\n",
|
2006-07-20 18:02:40 +02:00
|
|
|
playlist.queued,
|
|
|
|
getSongUrl(playlist.
|
|
|
|
songs[playlist.order[playlist.queued]]));
|
|
|
|
if (queueSong(playlist.songs[playlist.order[playlist.queued]]) <
|
|
|
|
0) {
|
2004-02-24 00:41:20 +01:00
|
|
|
playlist.queued = -1;
|
|
|
|
playlist_queueError = 1;
|
|
|
|
}
|
2006-07-20 18:02:40 +02:00
|
|
|
} else if (playlist.length && playlist.repeat) {
|
|
|
|
if (playlist.length > 1 && playlist.random) {
|
|
|
|
randomizeOrder(0, playlist.length - 1);
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
playlist.queued = 0;
|
|
|
|
DEBUG("playlist: queue song %i:\"%s\"\n",
|
2006-07-20 18:02:40 +02:00
|
|
|
playlist.queued,
|
|
|
|
getSongUrl(playlist.
|
|
|
|
songs[playlist.order[playlist.queued]]));
|
|
|
|
if (queueSong(playlist.songs[playlist.order[playlist.queued]]) <
|
|
|
|
0) {
|
2004-02-24 00:41:20 +01:00
|
|
|
playlist.queued = -1;
|
|
|
|
playlist_queueError = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-08-20 02:50:44 +02:00
|
|
|
static void syncPlaylistWithQueue(int queue)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
|
|
|
if (queue && getPlayerQueueState() == PLAYER_QUEUE_BLANK) {
|
2004-02-24 00:41:20 +01:00
|
|
|
queueNextSongInPlaylist();
|
2006-07-20 18:02:40 +02:00
|
|
|
} else if (getPlayerQueueState() == PLAYER_QUEUE_DECODE) {
|
|
|
|
if (playlist.queued != -1)
|
|
|
|
setQueueState(PLAYER_QUEUE_PLAY);
|
|
|
|
else
|
|
|
|
setQueueState(PLAYER_QUEUE_STOP);
|
|
|
|
} else if (getPlayerQueueState() == PLAYER_QUEUE_EMPTY) {
|
2004-02-24 00:41:20 +01:00
|
|
|
setQueueState(PLAYER_QUEUE_BLANK);
|
2006-07-20 18:02:40 +02:00
|
|
|
if (playlist.queued >= 0) {
|
2004-02-24 00:41:20 +01:00
|
|
|
DEBUG("playlist: now playing queued song\n");
|
|
|
|
playlist.current = playlist.queued;
|
|
|
|
}
|
|
|
|
playlist.queued = -1;
|
2006-07-20 18:02:40 +02:00
|
|
|
if (queue)
|
|
|
|
queueNextSongInPlaylist();
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-08-20 02:50:44 +02:00
|
|
|
static void lockPlaylistInteraction(void)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
|
|
|
if (getPlayerQueueState() == PLAYER_QUEUE_PLAY ||
|
|
|
|
getPlayerQueueState() == PLAYER_QUEUE_FULL) {
|
2004-02-24 00:41:20 +01:00
|
|
|
playerQueueLock();
|
|
|
|
syncPlaylistWithQueue(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
static void unlockPlaylistInteraction(void)
|
|
|
|
{
|
2004-02-24 00:41:20 +01:00
|
|
|
playerQueueUnlock();
|
|
|
|
}
|
|
|
|
|
2006-08-20 02:50:44 +02:00
|
|
|
static void clearPlayerQueue(void)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2004-02-24 00:41:20 +01:00
|
|
|
playlist.queued = -1;
|
2006-07-20 18:02:40 +02:00
|
|
|
switch (getPlayerQueueState()) {
|
2004-02-24 00:41:20 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-07-30 05:43:38 +02:00
|
|
|
int addToPlaylist(int fd, char *url, int printId)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
|
|
|
Song *song;
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
DEBUG("add to playlist: %s\n", url);
|
|
|
|
|
|
|
|
if ((song = getSongFromDB(url))) {
|
|
|
|
} else if (!(isValidRemoteUtf8Url(url) &&
|
|
|
|
(song = newSong(url, SONG_TYPE_URL, NULL)))) {
|
2006-07-30 05:43:38 +02:00
|
|
|
commandError(fd, ACK_ERROR_NO_EXIST,
|
2006-07-20 18:02:40 +02:00
|
|
|
"\"%s\" is not in the music db or is "
|
|
|
|
"not a valid url", url);
|
2004-02-24 00:41:20 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2006-07-30 05:43:38 +02:00
|
|
|
return addSongToPlaylist(fd, song, printId);
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
2006-11-20 16:37:58 +01:00
|
|
|
int addToStoredPlaylist(int fd, char *url, char *utf8file)
|
|
|
|
{
|
|
|
|
Song *song;
|
|
|
|
|
|
|
|
DEBUG("add to stored playlist: %s\n", url);
|
|
|
|
|
2007-05-26 17:00:38 +02:00
|
|
|
song = getSongFromDB(url);
|
|
|
|
if (song) {
|
|
|
|
appendSongToStoredPlaylistByPath(fd, utf8file, song);
|
|
|
|
return 0;
|
2006-11-20 16:37:58 +01:00
|
|
|
}
|
|
|
|
|
2007-05-26 17:00:38 +02:00
|
|
|
if (!isValidRemoteUtf8Url(url))
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
song = newSong(url, SONG_TYPE_URL, NULL);
|
|
|
|
if (song) {
|
|
|
|
appendSongToStoredPlaylistByPath(fd, utf8file, song);
|
|
|
|
freeJustSong(song);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
fail:
|
|
|
|
commandError(fd, ACK_ERROR_NO_EXIST, "\"%s\" is not in the music db"
|
|
|
|
"or is not a valid url", url);
|
|
|
|
return -1;
|
2006-11-20 16:37:58 +01:00
|
|
|
}
|
|
|
|
|
2006-07-30 05:43:38 +02:00
|
|
|
int addSongToPlaylist(int fd, Song * song, int printId)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2004-11-03 00:44:33 +01:00
|
|
|
int id;
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (playlist.length == playlist_max_length) {
|
2006-07-30 05:43:38 +02:00
|
|
|
commandError(fd, ACK_ERROR_PLAYLIST_MAX,
|
2006-07-30 10:47:50 +02:00
|
|
|
"playlist is at the max size");
|
2004-02-24 00:41:20 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (playlist_state == PLAYLIST_STATE_PLAY) {
|
|
|
|
if (playlist.queued >= 0
|
|
|
|
&& playlist.current == playlist.length - 1) {
|
2004-02-24 00:41:20 +01:00
|
|
|
lockPlaylistInteraction();
|
|
|
|
clearPlayerQueue();
|
|
|
|
unlockPlaylistInteraction();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-11-03 00:44:33 +01:00
|
|
|
id = getNextId();
|
|
|
|
|
2004-02-24 00:41:20 +01:00
|
|
|
playlist.songs[playlist.length] = song;
|
2004-06-05 03:14:37 +02:00
|
|
|
playlist.songMod[playlist.length] = playlist.version;
|
2004-02-24 00:41:20 +01:00
|
|
|
playlist.order[playlist.length] = playlist.length;
|
2004-11-03 00:44:33 +01:00
|
|
|
playlist.positionToId[playlist.length] = id;
|
2006-07-20 18:02:40 +02:00
|
|
|
playlist.idToPosition[playlist.positionToId[playlist.length]] =
|
|
|
|
playlist.length;
|
2004-02-24 00:41:20 +01:00
|
|
|
playlist.length++;
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (playlist.random) {
|
2004-02-24 00:41:20 +01:00
|
|
|
int swap;
|
|
|
|
int start;
|
2004-05-23 16:12:05 +02:00
|
|
|
/*if(playlist_state==PLAYLIST_STATE_STOP) start = 0;
|
2006-07-20 18:02:40 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-02-24 00:41:20 +01:00
|
|
|
incrPlaylistVersion();
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (printId)
|
2006-07-30 05:43:38 +02:00
|
|
|
fdprintf(fd, "Id: %i\n", id);
|
2004-11-03 00:44:33 +01:00
|
|
|
|
2004-02-24 00:41:20 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-07-30 05:43:38 +02:00
|
|
|
int swapSongsInPlaylist(int fd, int song1, int song2)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2004-02-24 00:41:20 +01:00
|
|
|
int queuedSong = -1;
|
|
|
|
int currentSong = -1;
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (song1 < 0 || song1 >= playlist.length) {
|
2006-07-30 05:43:38 +02:00
|
|
|
commandError(fd, ACK_ERROR_NO_EXIST,
|
2006-07-20 18:02:40 +02:00
|
|
|
"song doesn't exist: \"%i\"", song1);
|
2004-02-24 00:41:20 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2006-07-20 18:02:40 +02:00
|
|
|
if (song2 < 0 || song2 >= playlist.length) {
|
2006-07-30 05:43:38 +02:00
|
|
|
commandError(fd, ACK_ERROR_NO_EXIST,
|
2006-07-20 18:02:40 +02:00
|
|
|
"song doesn't exist: \"%i\"", song2);
|
2004-02-24 00:41:20 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2006-07-20 18:02:40 +02:00
|
|
|
|
|
|
|
if (playlist_state == PLAYLIST_STATE_PLAY) {
|
|
|
|
if (playlist.queued >= 0) {
|
2004-02-24 00:41:20 +01:00
|
|
|
queuedSong = playlist.order[playlist.queued];
|
|
|
|
}
|
|
|
|
currentSong = playlist.order[playlist.current];
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (queuedSong == song1 || queuedSong == song2
|
|
|
|
|| currentSong == song1 || currentSong == song2) {
|
2004-02-24 00:41:20 +01:00
|
|
|
lockPlaylistInteraction();
|
|
|
|
clearPlayerQueue();
|
|
|
|
unlockPlaylistInteraction();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
swapSongs(song1, song2);
|
|
|
|
if (playlist.random) {
|
2004-02-24 00:41:20 +01:00
|
|
|
int i;
|
|
|
|
int k;
|
|
|
|
int j = -1;
|
2006-07-20 18:02:40 +02:00
|
|
|
for (i = 0; playlist.order[i] != song1; i++) {
|
|
|
|
if (playlist.order[i] == song2)
|
|
|
|
j = i;
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
k = i;
|
2006-07-20 18:02:40 +02:00
|
|
|
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;
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
incrPlaylistVersion();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-07-30 05:43:38 +02:00
|
|
|
int swapSongsInPlaylistById(int fd, int id1, int id2)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2004-06-09 03:30:51 +02:00
|
|
|
checkSongId(id1);
|
|
|
|
checkSongId(id2);
|
|
|
|
|
2006-07-30 05:43:38 +02:00
|
|
|
return swapSongsInPlaylist(fd, playlist.idToPosition[id1],
|
2006-07-20 18:02:40 +02:00
|
|
|
playlist.idToPosition[id2]);
|
2004-06-09 03:30:51 +02:00
|
|
|
}
|
|
|
|
|
2004-06-09 02:18:31 +02:00
|
|
|
#define moveSongFromTo(from, to) { \
|
2004-06-30 04:12:49 +02:00
|
|
|
playlist.idToPosition[playlist.positionToId[from]] = to; \
|
|
|
|
playlist.positionToId[to] = playlist.positionToId[from]; \
|
2004-06-09 02:18:31 +02:00
|
|
|
playlist.songs[to] = playlist.songs[from]; \
|
|
|
|
playlist.songMod[to] = playlist.version; \
|
|
|
|
}
|
|
|
|
|
2006-07-30 05:43:38 +02:00
|
|
|
int deleteFromPlaylist(int fd, int song)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2004-02-24 00:41:20 +01:00
|
|
|
int i;
|
|
|
|
int songOrder;
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (song < 0 || song >= playlist.length) {
|
2006-07-30 05:43:38 +02:00
|
|
|
commandError(fd, ACK_ERROR_NO_EXIST,
|
2006-07-20 18:02:40 +02:00
|
|
|
"song doesn't exist: \"%i\"", song);
|
2004-02-24 00:41:20 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (playlist_state == PLAYLIST_STATE_PLAY) {
|
|
|
|
if (playlist.queued >= 0
|
|
|
|
&& (playlist.order[playlist.queued] == song
|
|
|
|
|| playlist.order[playlist.current] == song)) {
|
2004-02-24 00:41:20 +01:00
|
|
|
lockPlaylistInteraction();
|
|
|
|
clearPlayerQueue();
|
2006-07-20 18:02:40 +02:00
|
|
|
unlockPlaylistInteraction();
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (playlist.songs[song]->type == SONG_TYPE_URL) {
|
2004-05-19 07:07:31 +02:00
|
|
|
freeJustSong(playlist.songs[song]);
|
|
|
|
}
|
|
|
|
|
2004-06-30 04:12:49 +02:00
|
|
|
playlist.idToPosition[playlist.positionToId[song]] = -1;
|
2004-06-09 02:18:31 +02:00
|
|
|
|
2004-02-24 00:41:20 +01:00
|
|
|
/* delete song from songs array */
|
2006-07-20 18:02:40 +02:00
|
|
|
for (i = song; i < playlist.length - 1; i++) {
|
|
|
|
moveSongFromTo(i + 1, i);
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
/* now find it in the order array */
|
2006-07-20 18:02:40 +02:00
|
|
|
for (i = 0; i < playlist.length - 1; i++) {
|
|
|
|
if (playlist.order[i] == song)
|
|
|
|
break;
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
songOrder = i;
|
|
|
|
/* delete the entry from the order array */
|
2006-07-20 18:02:40 +02:00
|
|
|
for (; i < playlist.length - 1; i++)
|
|
|
|
playlist.order[i] = playlist.order[i + 1];
|
2004-02-24 00:41:20 +01:00
|
|
|
/* readjust values in the order array */
|
2006-07-20 18:02:40 +02:00
|
|
|
for (i = 0; i < playlist.length - 1; i++) {
|
|
|
|
if (playlist.order[i] > song)
|
|
|
|
playlist.order[i]--;
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
/* now take care of other misc stuff */
|
2006-07-20 18:02:40 +02:00
|
|
|
playlist.songs[playlist.length - 1] = NULL;
|
2004-02-24 00:41:20 +01:00
|
|
|
playlist.length--;
|
|
|
|
|
|
|
|
incrPlaylistVersion();
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (playlist_state != PLAYLIST_STATE_STOP
|
|
|
|
&& playlist.current == songOrder) {
|
2006-07-30 05:43:38 +02:00
|
|
|
/*if(playlist.current>=playlist.length) return playerStop(fd);
|
|
|
|
else return playPlaylistOrderNumber(fd,playlist.current); */
|
|
|
|
playerStop(STDERR_FILENO);
|
2004-02-24 00:41:20 +01:00
|
|
|
playlist_noGoToNext = 1;
|
|
|
|
}
|
2004-06-02 22:40:30 +02:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (playlist.current > songOrder) {
|
2004-02-24 00:41:20 +01:00
|
|
|
playlist.current--;
|
2006-07-20 18:02:40 +02:00
|
|
|
} else if (playlist.current >= playlist.length) {
|
2004-06-14 17:29:55 +02:00
|
|
|
incrPlaylistCurrent();
|
|
|
|
}
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (playlist.queued > songOrder) {
|
2004-02-24 00:41:20 +01:00
|
|
|
playlist.queued--;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-07-30 05:43:38 +02:00
|
|
|
int deleteFromPlaylistById(int fd, int id)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2004-06-09 03:30:51 +02:00
|
|
|
checkSongId(id);
|
|
|
|
|
2006-07-30 05:43:38 +02:00
|
|
|
return deleteFromPlaylist(fd, playlist.idToPosition[id]);
|
2004-06-09 03:30:51 +02:00
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
void deleteASongFromPlaylist(Song * song)
|
|
|
|
{
|
2004-02-24 00:41:20 +01:00
|
|
|
int i;
|
2004-04-11 03:53:25 +02:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (NULL == playlist.songs)
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (i = 0; i < playlist.length; i++) {
|
|
|
|
if (song == playlist.songs[i]) {
|
2006-07-30 05:43:38 +02:00
|
|
|
deleteFromPlaylist(STDERR_FILENO, i);
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-07-30 05:43:38 +02:00
|
|
|
int stopPlaylist(int fd)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2004-02-24 00:41:20 +01:00
|
|
|
DEBUG("playlist: stop\n");
|
2006-07-30 05:43:38 +02:00
|
|
|
if (playerStop(fd) < 0)
|
2006-07-20 18:02:40 +02:00
|
|
|
return -1;
|
2004-02-24 00:41:20 +01:00
|
|
|
playerCloseAudio();
|
|
|
|
playlist.queued = -1;
|
|
|
|
playlist_state = PLAYLIST_STATE_STOP;
|
|
|
|
playlist_noGoToNext = 0;
|
2006-07-20 18:02:40 +02:00
|
|
|
if (playlist.random)
|
|
|
|
randomizeOrder(0, playlist.length - 1);
|
2004-02-24 00:41:20 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-01-14 05:33:11 +01:00
|
|
|
static int playPlaylistOrderNumber(int fd, int orderNum)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2006-07-30 05:43:38 +02:00
|
|
|
if (playerStop(fd) < 0)
|
2006-07-20 18:02:40 +02:00
|
|
|
return -1;
|
2004-02-24 00:41:20 +01:00
|
|
|
|
|
|
|
playlist_state = PLAYLIST_STATE_PLAY;
|
|
|
|
playlist_noGoToNext = 0;
|
|
|
|
playlist.queued = -1;
|
|
|
|
playlist_queueError = 0;
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
DEBUG("playlist: play %i:\"%s\"\n", orderNum,
|
|
|
|
getSongUrl(playlist.songs[playlist.order[orderNum]]));
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2006-07-30 05:43:38 +02:00
|
|
|
if (playerPlay(fd, (playlist.songs[playlist.order[orderNum]])) < 0) {
|
|
|
|
stopPlaylist(fd);
|
2004-02-24 00:41:20 +01:00
|
|
|
return -1;
|
2007-01-11 15:47:12 +01:00
|
|
|
}
|
2004-06-01 16:16:17 +02:00
|
|
|
|
|
|
|
playlist.current = orderNum;
|
2004-02-24 00:41:20 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-07-30 05:43:38 +02:00
|
|
|
int playPlaylist(int fd, int song, int stopOnError)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2004-02-24 00:41:20 +01:00
|
|
|
int i = song;
|
|
|
|
|
|
|
|
clearPlayerError();
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (song == -1) {
|
|
|
|
if (playlist.length == 0)
|
|
|
|
return 0;
|
2004-06-04 05:10:54 +02:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (playlist_state == PLAYLIST_STATE_PLAY) {
|
2006-07-30 05:43:38 +02:00
|
|
|
return playerSetPause(fd, 0);
|
2006-07-14 23:01:19 +02:00
|
|
|
}
|
2006-07-20 18:02:40 +02:00
|
|
|
if (playlist.current >= 0 && playlist.current < playlist.length) {
|
2006-07-14 23:01:19 +02:00
|
|
|
i = playlist.current;
|
2006-07-20 18:02:40 +02:00
|
|
|
} else {
|
2006-07-14 23:01:19 +02:00
|
|
|
i = 0;
|
|
|
|
}
|
2006-07-20 18:02:40 +02:00
|
|
|
} else if (song < 0 || song >= playlist.length) {
|
2006-07-30 05:43:38 +02:00
|
|
|
commandError(fd, ACK_ERROR_NO_EXIST,
|
2006-07-20 18:02:40 +02:00
|
|
|
"song doesn't exist: \"%i\"", song);
|
2004-02-24 00:41:20 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
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) {
|
2004-02-24 00:41:20 +01:00
|
|
|
playlist.current = 0;
|
|
|
|
}
|
2006-07-20 18:02:40 +02:00
|
|
|
swapOrder(i, playlist.current);
|
2004-02-24 00:41:20 +01:00
|
|
|
i = playlist.current;
|
2004-05-23 16:12:05 +02:00
|
|
|
}
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
playlist_stopOnError = stopOnError;
|
|
|
|
playlist_errorCount = 0;
|
|
|
|
|
2006-07-30 05:43:38 +02:00
|
|
|
return playPlaylistOrderNumber(fd, i);
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
2006-07-30 05:43:38 +02:00
|
|
|
int playPlaylistById(int fd, int id, int stopOnError)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
|
|
|
if (id == -1) {
|
2006-07-30 05:43:38 +02:00
|
|
|
return playPlaylist(fd, id, stopOnError);
|
2004-06-09 03:30:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
checkSongId(id);
|
|
|
|
|
2006-07-30 05:43:38 +02:00
|
|
|
return playPlaylist(fd, playlist.idToPosition[id], stopOnError);
|
2004-06-09 03:30:51 +02:00
|
|
|
}
|
|
|
|
|
2006-08-20 02:50:44 +02:00
|
|
|
static void syncCurrentPlayerDecodeMetadata(void)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
|
|
|
Song *songPlayer = playerCurrentDecodeSong();
|
|
|
|
Song *song;
|
2004-06-05 03:14:37 +02:00
|
|
|
int songNum;
|
2004-05-31 13:42:46 +02:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (!songPlayer)
|
|
|
|
return;
|
2004-05-31 13:42:46 +02:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (playlist_state != PLAYLIST_STATE_PLAY)
|
|
|
|
return;
|
2004-05-31 13:42:46 +02:00
|
|
|
|
2004-06-05 03:14:37 +02:00
|
|
|
songNum = playlist.order[playlist.current];
|
2006-07-14 23:01:19 +02:00
|
|
|
song = playlist.songs[songNum];
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (song->type == SONG_TYPE_URL &&
|
|
|
|
0 == strcmp(getSongUrl(song), songPlayer->url) &&
|
|
|
|
!mpdTagsAreEqual(song->tag, songPlayer->tag)) {
|
|
|
|
if (song->tag)
|
|
|
|
freeMpdTag(song->tag);
|
2006-07-14 23:01:19 +02:00
|
|
|
song->tag = mpdTagDup(songPlayer->tag);
|
2004-06-05 03:14:37 +02:00
|
|
|
playlist.songMod[songNum] = playlist.version;
|
2006-07-14 23:01:19 +02:00
|
|
|
incrPlaylistVersion();
|
|
|
|
}
|
2004-05-31 13:42:46 +02:00
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
void syncPlayerAndPlaylist(void)
|
|
|
|
{
|
|
|
|
if (playlist_state != PLAYLIST_STATE_PLAY)
|
|
|
|
return;
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (getPlayerState() == PLAYER_STATE_STOP)
|
|
|
|
playPlaylistIfPlayerStopped();
|
|
|
|
else
|
|
|
|
syncPlaylistWithQueue(!playlist_queueError);
|
2004-05-31 13:42:46 +02:00
|
|
|
|
2006-07-14 23:01:19 +02:00
|
|
|
syncCurrentPlayerDecodeMetadata();
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
2006-08-20 02:50:44 +02:00
|
|
|
static int currentSongInPlaylist(int fd)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
|
|
|
if (playlist_state != PLAYLIST_STATE_PLAY)
|
|
|
|
return 0;
|
2004-02-24 00:41:20 +01:00
|
|
|
|
|
|
|
playlist_stopOnError = 0;
|
|
|
|
|
|
|
|
syncPlaylistWithQueue(0);
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (playlist.current >= 0 && playlist.current < playlist.length) {
|
2006-07-30 05:43:38 +02:00
|
|
|
return playPlaylistOrderNumber(fd, playlist.current);
|
2006-07-20 18:02:40 +02:00
|
|
|
} else
|
2006-07-30 05:43:38 +02:00
|
|
|
return stopPlaylist(fd);
|
2004-02-24 00:41:20 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-07-30 05:43:38 +02:00
|
|
|
int nextSongInPlaylist(int fd)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
|
|
|
if (playlist_state != PLAYLIST_STATE_PLAY)
|
|
|
|
return 0;
|
2004-02-24 00:41:20 +01:00
|
|
|
|
|
|
|
syncPlaylistWithQueue(0);
|
2006-07-20 18:02:40 +02:00
|
|
|
|
2004-02-24 00:41:20 +01:00
|
|
|
playlist_stopOnError = 0;
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (playlist.current < playlist.length - 1) {
|
2006-07-30 05:43:38 +02:00
|
|
|
return playPlaylistOrderNumber(fd, playlist.current + 1);
|
2006-07-20 18:02:40 +02:00
|
|
|
} else if (playlist.length && playlist.repeat) {
|
|
|
|
if (playlist.random)
|
|
|
|
randomizeOrder(0, playlist.length - 1);
|
2006-07-30 05:43:38 +02:00
|
|
|
return playPlaylistOrderNumber(fd, 0);
|
2006-07-20 18:02:40 +02:00
|
|
|
} else {
|
2006-07-14 23:01:19 +02:00
|
|
|
incrPlaylistCurrent();
|
2006-07-30 05:43:38 +02:00
|
|
|
return stopPlaylist(fd);
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
void playPlaylistIfPlayerStopped(void)
|
|
|
|
{
|
|
|
|
if (getPlayerState() == PLAYER_STATE_STOP) {
|
2004-02-24 00:41:20 +01:00
|
|
|
int error = getPlayerError();
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (error == PLAYER_ERROR_NOERROR)
|
|
|
|
playlist_errorCount = 0;
|
|
|
|
else
|
|
|
|
playlist_errorCount++;
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2006-07-20 20:53:56 +02:00
|
|
|
if (playlist_state == PLAYLIST_STATE_PLAY
|
|
|
|
&& ((playlist_stopOnError && error != PLAYER_ERROR_NOERROR)
|
|
|
|
|| error == PLAYER_ERROR_AUDIO
|
|
|
|
|| error == PLAYER_ERROR_SYSTEM
|
|
|
|
|| playlist_errorCount >= playlist.length)) {
|
2006-07-30 05:43:38 +02:00
|
|
|
stopPlaylist(STDERR_FILENO);
|
2006-07-20 18:02:40 +02:00
|
|
|
} else if (playlist_noGoToNext)
|
2006-07-30 05:43:38 +02:00
|
|
|
currentSongInPlaylist(STDERR_FILENO);
|
2006-07-20 18:02:40 +02:00
|
|
|
else
|
2006-07-30 05:43:38 +02:00
|
|
|
nextSongInPlaylist(STDERR_FILENO);
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
int getPlaylistRepeatStatus(void)
|
|
|
|
{
|
2004-02-24 00:41:20 +01:00
|
|
|
return playlist.repeat;
|
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
int getPlaylistRandomStatus(void)
|
|
|
|
{
|
2004-02-24 00:41:20 +01:00
|
|
|
return playlist.random;
|
|
|
|
}
|
|
|
|
|
2006-07-30 05:43:38 +02:00
|
|
|
int setPlaylistRepeatStatus(int fd, int status)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
|
|
|
if (status != 0 && status != 1) {
|
2006-07-30 05:43:38 +02:00
|
|
|
commandError(fd, ACK_ERROR_ARG, "\"%i\" is not 0 or 1", status);
|
2004-02-24 00:41:20 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (playlist_state == PLAYLIST_STATE_PLAY) {
|
|
|
|
if (playlist.repeat && !status && playlist.queued == 0) {
|
2004-02-24 00:41:20 +01:00
|
|
|
lockPlaylistInteraction();
|
|
|
|
clearPlayerQueue();
|
|
|
|
unlockPlaylistInteraction();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
playlist.repeat = status;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-07-30 05:43:38 +02:00
|
|
|
int moveSongInPlaylist(int fd, int from, int to)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2004-02-24 00:41:20 +01:00
|
|
|
int i;
|
2006-07-20 18:02:40 +02:00
|
|
|
Song *tmpSong;
|
2004-06-09 02:18:31 +02:00
|
|
|
int tmpId;
|
2004-02-24 00:41:20 +01:00
|
|
|
int queuedSong = -1;
|
|
|
|
int currentSong = -1;
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (from < 0 || from >= playlist.length) {
|
2006-07-30 05:43:38 +02:00
|
|
|
commandError(fd, ACK_ERROR_NO_EXIST,
|
2006-07-20 18:02:40 +02:00
|
|
|
"song doesn't exist: \"%i\"", from);
|
2004-02-24 00:41:20 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (to < 0 || to >= playlist.length) {
|
2006-07-30 05:43:38 +02:00
|
|
|
commandError(fd, ACK_ERROR_NO_EXIST,
|
2006-07-20 18:02:40 +02:00
|
|
|
"song doesn't exist: \"%i\"", to);
|
2004-02-24 00:41:20 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (playlist_state == PLAYLIST_STATE_PLAY) {
|
|
|
|
if (playlist.queued >= 0) {
|
2004-02-24 00:41:20 +01:00
|
|
|
queuedSong = playlist.order[playlist.queued];
|
|
|
|
}
|
|
|
|
currentSong = playlist.order[playlist.current];
|
2006-07-20 18:02:40 +02:00
|
|
|
if (queuedSong == from || queuedSong == to
|
|
|
|
|| currentSong == from || currentSong == to) {
|
2004-02-24 00:41:20 +01:00
|
|
|
lockPlaylistInteraction();
|
|
|
|
clearPlayerQueue();
|
|
|
|
unlockPlaylistInteraction();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
tmpSong = playlist.songs[from];
|
2004-06-30 04:12:49 +02:00
|
|
|
tmpId = playlist.positionToId[from];
|
2004-02-24 00:41:20 +01:00
|
|
|
/* move songs to one less in from->to */
|
2006-07-20 18:02:40 +02:00
|
|
|
for (i = from; i < to; i++) {
|
|
|
|
moveSongFromTo(i + 1, i);
|
2004-06-05 03:14:37 +02:00
|
|
|
}
|
2004-02-24 00:41:20 +01:00
|
|
|
/* move songs to one more in to->from */
|
2006-07-20 18:02:40 +02:00
|
|
|
for (i = from; i > to; i--) {
|
|
|
|
moveSongFromTo(i - 1, i);
|
2004-06-05 03:14:37 +02:00
|
|
|
}
|
2004-02-24 00:41:20 +01:00
|
|
|
/* put song at _to_ */
|
2004-06-30 04:12:49 +02:00
|
|
|
playlist.idToPosition[tmpId] = to;
|
|
|
|
playlist.positionToId[to] = tmpId;
|
2004-02-24 00:41:20 +01:00
|
|
|
playlist.songs[to] = tmpSong;
|
2004-06-05 03:14:37 +02:00
|
|
|
playlist.songMod[to] = playlist.version;
|
2004-02-24 00:41:20 +01:00
|
|
|
/* now deal with order */
|
2006-07-20 18:02:40 +02:00
|
|
|
if (playlist.random) {
|
|
|
|
for (i = 0; i < playlist.length; i++) {
|
|
|
|
if (playlist.order[i] > from && playlist.order[i] <= to) {
|
2004-02-24 00:41:20 +01:00
|
|
|
playlist.order[i]--;
|
2006-07-20 18:02:40 +02:00
|
|
|
} else if (playlist.order[i] < from &&
|
|
|
|
playlist.order[i] >= to) {
|
2004-02-24 00:41:20 +01:00
|
|
|
playlist.order[i]++;
|
2006-07-20 18:02:40 +02:00
|
|
|
} else if (from == playlist.order[i]) {
|
2004-02-24 00:41:20 +01:00
|
|
|
playlist.order[i] = to;
|
|
|
|
}
|
|
|
|
}
|
2007-02-18 01:06:20 +01:00
|
|
|
}
|
|
|
|
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++;
|
|
|
|
}
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
incrPlaylistVersion();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-07-30 05:43:38 +02:00
|
|
|
int moveSongInPlaylistById(int fd, int id1, int to)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2004-06-09 03:30:51 +02:00
|
|
|
checkSongId(id1);
|
|
|
|
|
2006-07-30 05:43:38 +02:00
|
|
|
return moveSongInPlaylist(fd, playlist.idToPosition[id1], to);
|
2004-06-09 03:30:51 +02:00
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
static void orderPlaylist(void)
|
|
|
|
{
|
2004-02-24 00:41:20 +01:00
|
|
|
int i;
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (playlist.current >= 0 && playlist.current < playlist.length) {
|
2004-06-26 06:34:28 +02:00
|
|
|
playlist.current = playlist.order[playlist.current];
|
|
|
|
}
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (playlist_state == PLAYLIST_STATE_PLAY) {
|
|
|
|
if (playlist.queued >= 0) {
|
2004-02-24 00:41:20 +01:00
|
|
|
lockPlaylistInteraction();
|
|
|
|
clearPlayerQueue();
|
|
|
|
unlockPlaylistInteraction();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
for (i = 0; i < playlist.length; i++) {
|
2004-02-24 00:41:20 +01:00
|
|
|
playlist.order[i] = i;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
static void swapOrder(int a, int b)
|
|
|
|
{
|
2004-02-24 00:41:20 +01:00
|
|
|
int bak = playlist.order[a];
|
|
|
|
playlist.order[a] = playlist.order[b];
|
|
|
|
playlist.order[b] = bak;
|
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
static void randomizeOrder(int start, int end)
|
|
|
|
{
|
2004-02-24 00:41:20 +01:00
|
|
|
int i;
|
|
|
|
int ri;
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
DEBUG("playlist: randomize from %i to %i\n", start, end);
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (playlist_state == PLAYLIST_STATE_PLAY) {
|
|
|
|
if (playlist.queued >= start && playlist.queued <= end) {
|
2004-02-24 00:41:20 +01:00
|
|
|
lockPlaylistInteraction();
|
|
|
|
clearPlayerQueue();
|
|
|
|
unlockPlaylistInteraction();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
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);
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2006-07-30 05:43:38 +02:00
|
|
|
int setPlaylistRandomStatus(int fd, int status)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2004-02-24 00:41:20 +01:00
|
|
|
int statusWas = playlist.random;
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (status != 0 && status != 1) {
|
2006-07-30 05:43:38 +02:00
|
|
|
commandError(fd, ACK_ERROR_ARG, "\"%i\" is not 0 or 1", status);
|
2004-02-24 00:41:20 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
playlist.random = status;
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (status != statusWas) {
|
|
|
|
if (playlist.random) {
|
2004-06-08 22:12:10 +02:00
|
|
|
/*if(playlist_state==PLAYLIST_STATE_PLAY) {
|
2006-07-20 18:02:40 +02:00
|
|
|
randomizeOrder(playlist.current+1,
|
|
|
|
playlist.length-1);
|
|
|
|
}
|
|
|
|
else */ randomizeOrder(0, playlist.length - 1);
|
|
|
|
if (playlist.current >= 0 &&
|
|
|
|
playlist.current < playlist.length) {
|
2004-06-08 22:16:28 +02:00
|
|
|
swapOrder(playlist.current, 0);
|
2004-06-14 19:01:40 +02:00
|
|
|
playlist.current = 0;
|
2004-06-08 22:16:28 +02:00
|
|
|
}
|
2006-07-20 18:02:40 +02:00
|
|
|
} else
|
|
|
|
orderPlaylist();
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-07-30 05:43:38 +02:00
|
|
|
int previousSongInPlaylist(int fd)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2007-01-14 04:07:53 +01:00
|
|
|
static time_t lastTime;
|
2004-06-10 23:42:20 +02:00
|
|
|
time_t diff = time(NULL) - lastTime;
|
|
|
|
|
|
|
|
lastTime += diff;
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (playlist_state != PLAYLIST_STATE_PLAY)
|
|
|
|
return 0;
|
2004-02-24 00:41:20 +01:00
|
|
|
|
|
|
|
syncPlaylistWithQueue(0);
|
|
|
|
|
2006-07-14 23:01:19 +02:00
|
|
|
if (diff && getPlayerElapsedTime() > PLAYLIST_PREV_UNLESS_ELAPSED) {
|
2006-07-30 05:43:38 +02:00
|
|
|
return playPlaylistOrderNumber(fd, playlist.current);
|
2006-07-20 18:02:40 +02:00
|
|
|
} else {
|
|
|
|
if (playlist.current > 0) {
|
2006-07-30 05:43:38 +02:00
|
|
|
return playPlaylistOrderNumber(fd,
|
2006-07-20 18:02:40 +02:00
|
|
|
playlist.current - 1);
|
|
|
|
} else if (playlist.repeat) {
|
2006-07-30 05:43:38 +02:00
|
|
|
return playPlaylistOrderNumber(fd, playlist.length - 1);
|
2006-07-20 18:02:40 +02:00
|
|
|
} else {
|
2006-07-30 05:43:38 +02:00
|
|
|
return playPlaylistOrderNumber(fd, playlist.current);
|
2006-07-14 23:01:19 +02:00
|
|
|
}
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-07-30 05:43:38 +02:00
|
|
|
int shufflePlaylist(int fd)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2004-02-24 00:41:20 +01:00
|
|
|
int i;
|
|
|
|
int ri;
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (playlist.length > 1) {
|
|
|
|
if (playlist_state == PLAYLIST_STATE_PLAY) {
|
2004-02-24 00:41:20 +01:00
|
|
|
lockPlaylistInteraction();
|
|
|
|
clearPlayerQueue();
|
|
|
|
unlockPlaylistInteraction();
|
|
|
|
/* put current playing song first */
|
2006-07-20 18:02:40 +02:00
|
|
|
swapSongs(0, playlist.order[playlist.current]);
|
|
|
|
if (playlist.random) {
|
2004-02-24 00:41:20 +01:00
|
|
|
int j;
|
2006-07-20 18:02:40 +02:00
|
|
|
for (j = 0; 0 != playlist.order[j]; j++) ;
|
2004-02-24 00:41:20 +01:00
|
|
|
playlist.current = j;
|
2006-07-20 18:02:40 +02:00
|
|
|
} else
|
|
|
|
playlist.current = 0;
|
2004-02-24 00:41:20 +01:00
|
|
|
i = 1;
|
2006-07-20 18:02:40 +02:00
|
|
|
} else {
|
|
|
|
i = 0;
|
|
|
|
playlist.current = -1;
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
/* shuffle the rest of the list */
|
2006-07-20 18:02:40 +02:00
|
|
|
for (; i < playlist.length; i++) {
|
|
|
|
ri = random() % (playlist.length - 1) + 1;
|
|
|
|
swapSongs(i, ri);
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
incrPlaylistVersion();
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-07-30 05:43:38 +02:00
|
|
|
int deletePlaylist(int fd, char *utf8file)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
|
|
|
char *file = utf8ToFsCharset(utf8file);
|
2006-08-26 08:25:57 +02:00
|
|
|
char *rfile = xmalloc(strlen(file) + strlen(".") +
|
2006-07-20 18:02:40 +02:00
|
|
|
strlen(PLAYLIST_FILE_SUFFIX) + 1);
|
|
|
|
char *actualFile;
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
strcpy(rfile, file);
|
|
|
|
strcat(rfile, ".");
|
|
|
|
strcat(rfile, PLAYLIST_FILE_SUFFIX);
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if ((actualFile = rpp2app(rfile)) && isPlaylist(actualFile))
|
|
|
|
free(rfile);
|
2004-02-24 00:41:20 +01:00
|
|
|
else {
|
|
|
|
free(rfile);
|
2006-07-30 05:43:38 +02:00
|
|
|
commandError(fd, ACK_ERROR_NO_EXIST,
|
2006-07-20 18:02:40 +02:00
|
|
|
"playlist \"%s\" not found", utf8file);
|
2004-02-24 00:41:20 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (unlink(actualFile) < 0) {
|
2006-07-30 05:43:38 +02:00
|
|
|
commandError(fd, ACK_ERROR_SYSTEM,
|
2006-07-30 10:47:50 +02:00
|
|
|
"problems deleting file");
|
2004-02-24 00:41:20 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-07-30 05:43:38 +02:00
|
|
|
int savePlaylist(int fd, char *utf8file)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2007-05-16 14:02:10 +02:00
|
|
|
StoredPlaylist *sp = newStoredPlaylist(utf8file, fd, 0);
|
|
|
|
if (!sp)
|
2004-02-24 00:41:20 +01:00
|
|
|
return -1;
|
|
|
|
|
2007-05-16 14:02:10 +02:00
|
|
|
appendPlaylistToStoredPlaylist(sp, &playlist);
|
|
|
|
if (writeStoredPlaylist(sp) != 0) {
|
|
|
|
freeStoredPlaylist(sp);
|
2004-02-24 00:41:20 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2007-05-16 14:02:10 +02:00
|
|
|
freeStoredPlaylist(sp);
|
2004-02-24 00:41:20 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
int getPlaylistCurrentSong(void)
|
|
|
|
{
|
|
|
|
if (playlist.current >= 0 && playlist.current < playlist.length) {
|
|
|
|
return playlist.order[playlist.current];
|
|
|
|
}
|
2006-07-14 23:01:19 +02:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
return -1;
|
2006-03-26 15:46:05 +02:00
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
unsigned long getPlaylistVersion(void)
|
|
|
|
{
|
2006-03-26 15:46:05 +02:00
|
|
|
return playlist.version;
|
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
int getPlaylistLength(void)
|
|
|
|
{
|
2006-03-26 15:46:05 +02:00
|
|
|
return playlist.length;
|
|
|
|
}
|
|
|
|
|
2006-07-30 05:43:38 +02:00
|
|
|
int seekSongInPlaylist(int fd, int song, float time)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2006-03-26 15:46:05 +02:00
|
|
|
int i = song;
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (song < 0 || song >= playlist.length) {
|
2006-07-30 05:43:38 +02:00
|
|
|
commandError(fd, ACK_ERROR_NO_EXIST,
|
2006-07-20 18:02:40 +02:00
|
|
|
"song doesn't exist: \"%i\"", song);
|
2006-03-26 15:46:05 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (playlist.random)
|
|
|
|
for (i = 0; song != playlist.order[i]; i++) ;
|
2006-03-26 15:46:05 +02:00
|
|
|
|
|
|
|
clearPlayerError();
|
|
|
|
playlist_stopOnError = 1;
|
|
|
|
playlist_errorCount = 0;
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (playlist_state == PLAYLIST_STATE_PLAY) {
|
|
|
|
if (playlist.queued >= 0) {
|
2006-03-26 15:46:05 +02:00
|
|
|
lockPlaylistInteraction();
|
|
|
|
clearPlayerQueue();
|
|
|
|
unlockPlaylistInteraction();
|
|
|
|
}
|
2006-07-30 05:43:38 +02:00
|
|
|
} else if (playPlaylistOrderNumber(fd, i) < 0)
|
2006-07-20 18:02:40 +02:00
|
|
|
return -1;
|
2006-03-26 15:46:05 +02:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (playlist.current != i) {
|
2006-07-30 05:43:38 +02:00
|
|
|
if (playPlaylistOrderNumber(fd, i) < 0)
|
2006-07-20 18:02:40 +02:00
|
|
|
return -1;
|
2006-03-26 15:46:05 +02:00
|
|
|
}
|
|
|
|
|
2006-07-30 05:43:38 +02:00
|
|
|
return playerSeek(fd, playlist.songs[playlist.order[i]], time);
|
2006-03-26 15:46:05 +02:00
|
|
|
}
|
|
|
|
|
2006-07-30 05:43:38 +02:00
|
|
|
int seekSongInPlaylistById(int fd, int id, float time)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2006-03-26 15:46:05 +02:00
|
|
|
checkSongId(id);
|
|
|
|
|
2006-07-30 05:43:38 +02:00
|
|
|
return seekSongInPlaylist(fd, playlist.idToPosition[id], time);
|
2006-03-26 15:46:05 +02:00
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
int getPlaylistSongId(int song)
|
|
|
|
{
|
2006-03-26 15:46:05 +02:00
|
|
|
return playlist.positionToId[song];
|
|
|
|
}
|
|
|
|
|
2007-05-16 14:02:10 +02:00
|
|
|
int PlaylistInfo(int fd, char *utf8file, int detail)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2007-05-16 14:02:10 +02:00
|
|
|
ListNode *node;
|
|
|
|
StoredPlaylist *sp = loadStoredPlaylist(utf8file, fd);
|
|
|
|
if (sp == NULL)
|
2004-02-24 00:41:20 +01:00
|
|
|
return -1;
|
|
|
|
|
2007-05-16 14:02:10 +02:00
|
|
|
node = sp->list->firstNode;
|
|
|
|
while (node != NULL) {
|
|
|
|
char *temp = node->data;
|
|
|
|
int wrote = 0;
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2007-05-16 14:02:10 +02:00
|
|
|
if (detail) {
|
|
|
|
Song *song = getSongFromDB(temp);
|
|
|
|
if (song) {
|
|
|
|
printSongInfo(fd, song);
|
|
|
|
wrote = 1;
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
2007-05-16 14:02:10 +02:00
|
|
|
}
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2007-05-16 14:02:10 +02:00
|
|
|
if (!wrote) {
|
|
|
|
fdprintf(fd, SONG_FILE "%s\n", temp);
|
|
|
|
}
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2007-05-16 14:02:10 +02:00
|
|
|
node = node->nextNode;
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
2007-05-16 14:02:10 +02:00
|
|
|
freeStoredPlaylist(sp);
|
2004-02-24 00:41:20 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-05-16 14:02:10 +02:00
|
|
|
int loadPlaylist(int fd, char *utf8file)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2007-05-16 14:02:10 +02:00
|
|
|
ListNode *node;
|
|
|
|
StoredPlaylist *sp = loadStoredPlaylist(utf8file, fd);
|
|
|
|
if (sp == NULL)
|
|
|
|
return -1;
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2007-05-16 14:02:10 +02:00
|
|
|
node = sp->list->firstNode;
|
|
|
|
while (node != NULL) {
|
|
|
|
char *temp = node->data;
|
2007-05-26 16:09:09 +02:00
|
|
|
if ((addToPlaylist(STDERR_FILENO, temp, 0)) < 0) {
|
2007-05-16 14:02:10 +02:00
|
|
|
/* for windows compatibility, convert slashes */
|
|
|
|
char *temp2 = xstrdup(temp);
|
|
|
|
char *p = temp2;
|
|
|
|
while (*p) {
|
|
|
|
if (*p == '\\')
|
|
|
|
*p = '/';
|
|
|
|
p++;
|
2006-03-26 15:46:05 +02:00
|
|
|
}
|
2007-05-16 14:02:10 +02:00
|
|
|
if ((addToPlaylist(STDERR_FILENO, temp2, 0)) < 0) {
|
|
|
|
commandError(fd, ACK_ERROR_PLAYLIST_LOAD,
|
|
|
|
"can't add file \"%s\"", temp2);
|
|
|
|
}
|
|
|
|
free(temp2);
|
2006-03-26 15:46:05 +02:00
|
|
|
}
|
2004-06-09 03:30:51 +02:00
|
|
|
|
2007-05-16 14:02:10 +02:00
|
|
|
node = node->nextNode;
|
2006-03-26 15:46:05 +02:00
|
|
|
}
|
2004-06-09 04:50:44 +02:00
|
|
|
|
2007-05-16 14:02:10 +02:00
|
|
|
freeStoredPlaylist(sp);
|
|
|
|
return 0;
|
2004-06-09 04:50:44 +02:00
|
|
|
}
|
2007-02-24 03:00:03 +01:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|