PlaylistState.cxx 7.55 KB
Newer Older
1
/*
2
 * Copyright (C) 2003-2013 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13
 * 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.
14 15 16 17
 *
 * 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.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 19 20 21 22 23 24
 */

/*
 * Saving and loading the playlist to/from the state file.
 *
 */

25
#include "config.h"
26
#include "PlaylistState.hxx"
27
#include "PlaylistError.hxx"
28
#include "Playlist.hxx"
29
#include "QueueSave.hxx"
30
#include "TextFile.hxx"
31
#include "PlayerControl.hxx"
32 33
#include "ConfigGlobal.hxx"
#include "ConfigOption.hxx"
34
#include "fs/Limits.hxx"
35
#include "util/CharUtil.hxx"
36
#include "Log.hxx"
37

38 39
#include <glib.h>

40 41 42 43 44 45
#include <string.h>
#include <stdlib.h>

#define PLAYLIST_STATE_FILE_STATE		"state: "
#define PLAYLIST_STATE_FILE_RANDOM		"random: "
#define PLAYLIST_STATE_FILE_REPEAT		"repeat: "
46
#define PLAYLIST_STATE_FILE_SINGLE		"single: "
47
#define PLAYLIST_STATE_FILE_CONSUME		"consume: "
48 49 50
#define PLAYLIST_STATE_FILE_CURRENT		"current: "
#define PLAYLIST_STATE_FILE_TIME		"time: "
#define PLAYLIST_STATE_FILE_CROSSFADE		"crossfade: "
51 52
#define PLAYLIST_STATE_FILE_MIXRAMPDB		"mixrampdb: "
#define PLAYLIST_STATE_FILE_MIXRAMPDELAY	"mixrampdelay: "
53 54 55 56 57 58 59 60 61 62
#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*MPD_PATH_MAX

void
63
playlist_state_save(FILE *fp, const struct playlist &playlist,
64
		    PlayerControl &pc)
65
{
66
	const auto player_status = pc.GetStatus();
67

68
	fputs(PLAYLIST_STATE_FILE_STATE, fp);
69

70
	if (playlist.playing) {
71
		switch (player_status.state) {
72
		case PlayerState::PAUSE:
73
			fputs(PLAYLIST_STATE_FILE_STATE_PAUSE "\n", fp);
74 75
			break;
		default:
76
			fputs(PLAYLIST_STATE_FILE_STATE_PLAY "\n", fp);
77
		}
78
		fprintf(fp, PLAYLIST_STATE_FILE_CURRENT "%i\n",
79
			playlist.queue.OrderToPosition(playlist.current));
80
		fprintf(fp, PLAYLIST_STATE_FILE_TIME "%i\n",
81
			(int)player_status.elapsed_time);
82
	} else {
83
		fputs(PLAYLIST_STATE_FILE_STATE_STOP "\n", fp);
84

85
		if (playlist.current >= 0)
86
			fprintf(fp, PLAYLIST_STATE_FILE_CURRENT "%i\n",
87
				playlist.queue.OrderToPosition(playlist.current));
88
	}
89

90 91 92
	fprintf(fp, PLAYLIST_STATE_FILE_RANDOM "%i\n", playlist.queue.random);
	fprintf(fp, PLAYLIST_STATE_FILE_REPEAT "%i\n", playlist.queue.repeat);
	fprintf(fp, PLAYLIST_STATE_FILE_SINGLE "%i\n", playlist.queue.single);
93
	fprintf(fp, PLAYLIST_STATE_FILE_CONSUME "%i\n",
94
		playlist.queue.consume);
95
	fprintf(fp, PLAYLIST_STATE_FILE_CROSSFADE "%i\n",
96
		(int)pc.GetCrossFade());
97
	fprintf(fp, PLAYLIST_STATE_FILE_MIXRAMPDB "%f\n",
98
		pc.GetMixRampDb());
99
	fprintf(fp, PLAYLIST_STATE_FILE_MIXRAMPDELAY "%f\n",
100
		pc.GetMixRampDelay());
101
	fputs(PLAYLIST_STATE_FILE_PLAYLIST_BEGIN "\n", fp);
102
	queue_save(fp, playlist.queue);
103
	fputs(PLAYLIST_STATE_FILE_PLAYLIST_END "\n", fp);
104 105 106
}

static void
107
playlist_state_load(TextFile &file, struct playlist &playlist)
108
{
109
	const char *line = file.ReadLine();
110
	if (line == nullptr) {
111
		LogWarning(playlist_domain, "No playlist in state file");
112 113 114
		return;
	}

115
	while (!g_str_has_prefix(line, PLAYLIST_STATE_FILE_PLAYLIST_END)) {
116
		queue_load_song(file, line, playlist.queue);
117

118
		line = file.ReadLine();
119
		if (line == nullptr) {
120 121 122
			LogWarning(playlist_domain,
				   "'" PLAYLIST_STATE_FILE_PLAYLIST_END
				   "' not found in state file");
123 124 125
			break;
		}
	}
126

127
	playlist.queue.IncrementVersion();
128 129
}

130
bool
131
playlist_state_restore(const char *line, TextFile &file,
132
		       struct playlist &playlist, PlayerControl &pc)
133 134 135 136 137
{
	int current = -1;
	int seek_time = 0;
	bool random_mode = false;

138 139 140 141 142
	if (!g_str_has_prefix(line, PLAYLIST_STATE_FILE_STATE))
		return false;

	line += sizeof(PLAYLIST_STATE_FILE_STATE) - 1;

143
	PlayerState state;
144
	if (strcmp(line, PLAYLIST_STATE_FILE_STATE_PLAY) == 0)
145
		state = PlayerState::PLAY;
146
	else if (strcmp(line, PLAYLIST_STATE_FILE_STATE_PAUSE) == 0)
147 148 149
		state = PlayerState::PAUSE;
	else
		state = PlayerState::STOP;
150

151
	while ((line = file.ReadLine()) != nullptr) {
152
		if (g_str_has_prefix(line, PLAYLIST_STATE_FILE_TIME)) {
153
			seek_time =
154
				atoi(&(line[strlen(PLAYLIST_STATE_FILE_TIME)]));
155
		} else if (g_str_has_prefix(line, PLAYLIST_STATE_FILE_REPEAT)) {
156 157 158
			playlist.SetRepeat(pc,
					   strcmp(&(line[strlen(PLAYLIST_STATE_FILE_REPEAT)]),
						  "1") == 0);
159
		} else if (g_str_has_prefix(line, PLAYLIST_STATE_FILE_SINGLE)) {
160 161 162
			playlist.SetSingle(pc,
					   strcmp(&(line[strlen(PLAYLIST_STATE_FILE_SINGLE)]),
						  "1") == 0);
163
		} else if (g_str_has_prefix(line, PLAYLIST_STATE_FILE_CONSUME)) {
164 165
			playlist.SetConsume(strcmp(&(line[strlen(PLAYLIST_STATE_FILE_CONSUME)]),
						   "1") == 0);
166
		} else if (g_str_has_prefix(line, PLAYLIST_STATE_FILE_CROSSFADE)) {
167
			pc.SetCrossFade(atoi(line + strlen(PLAYLIST_STATE_FILE_CROSSFADE)));
168
		} else if (g_str_has_prefix(line, PLAYLIST_STATE_FILE_MIXRAMPDB)) {
169
			pc.SetMixRampDb(atof(line + strlen(PLAYLIST_STATE_FILE_MIXRAMPDB)));
170
		} else if (g_str_has_prefix(line, PLAYLIST_STATE_FILE_MIXRAMPDELAY)) {
171 172 173 174 175 176
			const char *p = line + strlen(PLAYLIST_STATE_FILE_MIXRAMPDELAY);

			/* this check discards "nan" which was used
			   prior to MPD 0.18 */
			if (IsDigitASCII(*p))
				pc.SetMixRampDelay(atof(p));
177
		} else if (g_str_has_prefix(line, PLAYLIST_STATE_FILE_RANDOM)) {
178
			random_mode =
179
				strcmp(line + strlen(PLAYLIST_STATE_FILE_RANDOM),
180
				       "1") == 0;
181 182
		} else if (g_str_has_prefix(line, PLAYLIST_STATE_FILE_CURRENT)) {
			current = atoi(&(line
183 184
					 [strlen
					  (PLAYLIST_STATE_FILE_CURRENT)]));
185
		} else if (g_str_has_prefix(line,
186
					    PLAYLIST_STATE_FILE_PLAYLIST_BEGIN)) {
187
			playlist_state_load(file, playlist);
188 189 190
		}
	}

191
	playlist.SetRandom(pc, random_mode);
192

193 194
	if (!playlist.queue.IsEmpty()) {
		if (!playlist.queue.IsValidPosition(current))
195 196
			current = 0;

197
		if (state == PlayerState::PLAY &&
198
		    config_get_bool(CONF_RESTORE_PAUSED, false))
199 200 201
			/* the user doesn't want MPD to auto-start
			   playback after startup; fall back to
			   "pause" */
202
			state = PlayerState::PAUSE;
203

204 205 206
		/* enable all devices for the first time; this must be
		   called here, after the audio output states were
		   restored, before playback begins */
207
		if (state != PlayerState::STOP)
208
			pc.UpdateAudio();
209

210
		if (state == PlayerState::STOP /* && config_option */)
211
			playlist.current = current;
212
		else if (seek_time == 0)
213
			playlist.PlayPosition(pc, current);
214
		else
215
			playlist.SeekSongPosition(pc, current, seek_time);
216

217
		if (state == PlayerState::PAUSE)
218
			pc.Pause();
219
	}
220 221

	return true;
222
}
223 224

unsigned
225
playlist_state_get_hash(const playlist &playlist,
226
			PlayerControl &pc)
227
{
228
	const auto player_status = pc.GetStatus();
229

230
	return playlist.queue.version ^
231
		(player_status.state != PlayerState::STOP
232 233
		 ? ((int)player_status.elapsed_time << 8)
		 : 0) ^
234 235
		(playlist.current >= 0
		 ? (playlist.queue.OrderToPosition(playlist.current) << 16)
236
		 : 0) ^
237
		((int)pc.GetCrossFade() << 20) ^
238
		(unsigned(player_status.state) << 24) ^
239 240 241 242 243
		(playlist.queue.random << 27) ^
		(playlist.queue.repeat << 28) ^
		(playlist.queue.single << 29) ^
		(playlist.queue.consume << 30) ^
		(playlist.queue.random << 31);
244
}