playlist_state.c 7.48 KB
Newer Older
1
/*
2
 * Copyright (C) 2003-2010 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 27 28 29 30 31 32 33 34 35 36 37
#include "playlist_state.h"
#include "playlist.h"
#include "player_control.h"
#include "queue_save.h"
#include "path.h"

#include <string.h>
#include <stdlib.h>

#define PLAYLIST_STATE_FILE_STATE		"state: "
#define PLAYLIST_STATE_FILE_RANDOM		"random: "
#define PLAYLIST_STATE_FILE_REPEAT		"repeat: "
38
#define PLAYLIST_STATE_FILE_SINGLE		"single: "
39
#define PLAYLIST_STATE_FILE_CONSUME		"consume: "
40 41 42
#define PLAYLIST_STATE_FILE_CURRENT		"current: "
#define PLAYLIST_STATE_FILE_TIME		"time: "
#define PLAYLIST_STATE_FILE_CROSSFADE		"crossfade: "
43 44
#define PLAYLIST_STATE_FILE_MIXRAMPDB		"mixrampdb: "
#define PLAYLIST_STATE_FILE_MIXRAMPDELAY	"mixrampdelay: "
45 46 47 48 49 50 51 52 53 54 55 56
#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
playlist_state_save(FILE *fp, const struct playlist *playlist)
{
57 58 59 60
	struct player_status player_status;

	pc_get_status(&player_status);

61 62 63
	fprintf(fp, "%s", PLAYLIST_STATE_FILE_STATE);

	if (playlist->playing) {
64
		switch (player_status.state) {
65 66 67 68 69 70 71 72 73 74
		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,
			queue_order_to_position(&playlist->queue,
						playlist->current));
		fprintf(fp, "%s%i\n", PLAYLIST_STATE_FILE_TIME,
75
			(int)player_status.elapsed_time);
76
	} else {
77
		fprintf(fp, "%s\n", PLAYLIST_STATE_FILE_STATE_STOP);
78 79 80

		if (playlist->current >= 0)
			fprintf(fp, "%s%i\n", PLAYLIST_STATE_FILE_CURRENT,
81
				queue_order_to_position(&playlist->queue,
82
							playlist->current));
83
	}
84 85 86 87 88

	fprintf(fp, "%s%i\n", PLAYLIST_STATE_FILE_RANDOM,
		playlist->queue.random);
	fprintf(fp, "%s%i\n", PLAYLIST_STATE_FILE_REPEAT,
		playlist->queue.repeat);
89 90
	fprintf(fp, "%s%i\n", PLAYLIST_STATE_FILE_SINGLE,
		playlist->queue.single);
91 92
	fprintf(fp, "%s%i\n", PLAYLIST_STATE_FILE_CONSUME,
		playlist->queue.consume);
93
	fprintf(fp, "%s%i\n", PLAYLIST_STATE_FILE_CROSSFADE,
94
		(int)(pc_get_cross_fade()));
95 96 97 98
	fprintf(fp, "%s%f\n", PLAYLIST_STATE_FILE_MIXRAMPDB,
		pc_get_mixramp_db());
	fprintf(fp, "%s%f\n", PLAYLIST_STATE_FILE_MIXRAMPDELAY,
		pc_get_mixramp_delay());
99 100 101 102 103 104
	fprintf(fp, "%s\n", PLAYLIST_STATE_FILE_PLAYLIST_BEGIN);
	queue_save(fp, &playlist->queue);
	fprintf(fp, "%s\n", PLAYLIST_STATE_FILE_PLAYLIST_END);
}

static void
105
playlist_state_load(FILE *fp, struct playlist *playlist, char *buffer)
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
{
	int song;

	if (!fgets(buffer, PLAYLIST_BUFFER_SIZE, fp)) {
		g_warning("No playlist in state file");
		return;
	}

	while (!g_str_has_prefix(buffer, PLAYLIST_STATE_FILE_PLAYLIST_END)) {
		g_strchomp(buffer);

		song = queue_load_song(&playlist->queue, buffer);

		if (!fgets(buffer, PLAYLIST_BUFFER_SIZE, fp)) {
			g_warning("'%s' not found in state file",
				  PLAYLIST_STATE_FILE_PLAYLIST_END);
			break;
		}
	}
125 126

	queue_increment_version(&playlist->queue);
127 128
}

129 130
bool
playlist_state_restore(const char *line, FILE *fp, struct playlist *playlist)
131 132 133 134 135 136 137
{
	int current = -1;
	int seek_time = 0;
	int state = PLAYER_STATE_STOP;
	char buffer[PLAYLIST_BUFFER_SIZE];
	bool random_mode = false;

138 139 140 141 142 143 144 145 146 147
	if (!g_str_has_prefix(line, PLAYLIST_STATE_FILE_STATE))
		return false;

	line += sizeof(PLAYLIST_STATE_FILE_STATE) - 1;

	if (strcmp(line, PLAYLIST_STATE_FILE_STATE_PLAY) == 0)
		state = PLAYER_STATE_PLAY;
	else if (strcmp(line, PLAYLIST_STATE_FILE_STATE_PAUSE) == 0)
		state = PLAYER_STATE_PAUSE;

148 149 150
	while (fgets(buffer, sizeof(buffer), fp)) {
		g_strchomp(buffer);

151
		if (g_str_has_prefix(buffer, PLAYLIST_STATE_FILE_TIME)) {
152 153
			seek_time =
			    atoi(&(buffer[strlen(PLAYLIST_STATE_FILE_TIME)]));
154
		} else if (g_str_has_prefix(buffer, PLAYLIST_STATE_FILE_REPEAT)) {
155 156 157
			if (strcmp
			    (&(buffer[strlen(PLAYLIST_STATE_FILE_REPEAT)]),
			     "1") == 0) {
158
				playlist_set_repeat(playlist, true);
159
			} else
160
				playlist_set_repeat(playlist, false);
161
		} else if (g_str_has_prefix(buffer, PLAYLIST_STATE_FILE_SINGLE)) {
162
			if (strcmp
163
			    (&(buffer[strlen(PLAYLIST_STATE_FILE_SINGLE)]),
164
			     "1") == 0) {
165
				playlist_set_single(playlist, true);
166
			} else
167
				playlist_set_single(playlist, false);
168 169 170 171
		} else if (g_str_has_prefix(buffer, PLAYLIST_STATE_FILE_CONSUME)) {
			if (strcmp
			    (&(buffer[strlen(PLAYLIST_STATE_FILE_CONSUME)]),
			     "1") == 0) {
172
				playlist_set_consume(playlist, true);
173
			} else
174
				playlist_set_consume(playlist, false);
175
		} else if (g_str_has_prefix(buffer, PLAYLIST_STATE_FILE_CROSSFADE)) {
176
			pc_set_cross_fade(atoi(buffer + strlen(PLAYLIST_STATE_FILE_CROSSFADE)));
177 178 179 180
		} else if (g_str_has_prefix(buffer, PLAYLIST_STATE_FILE_MIXRAMPDB)) {
			pc_set_mixramp_db(atof(buffer + strlen(PLAYLIST_STATE_FILE_MIXRAMPDB)));
		} else if (g_str_has_prefix(buffer, PLAYLIST_STATE_FILE_MIXRAMPDELAY)) {
			pc_set_mixramp_delay(atof(buffer + strlen(PLAYLIST_STATE_FILE_MIXRAMPDELAY)));
181 182 183 184 185 186 187 188 189 190
		} else if (g_str_has_prefix(buffer, PLAYLIST_STATE_FILE_RANDOM)) {
			random_mode =
				strcmp(buffer + strlen(PLAYLIST_STATE_FILE_RANDOM),
				       "1") == 0;
		} else if (g_str_has_prefix(buffer, PLAYLIST_STATE_FILE_CURRENT)) {
			current = atoi(&(buffer
					 [strlen
					  (PLAYLIST_STATE_FILE_CURRENT)]));
		} else if (g_str_has_prefix(buffer,
					    PLAYLIST_STATE_FILE_PLAYLIST_BEGIN)) {
191
			playlist_state_load(fp, playlist, buffer);
192 193 194
		}
	}

195
	playlist_set_random(playlist, random_mode);
196

197
	if (!queue_is_empty(&playlist->queue)) {
198 199 200
		if (!queue_valid_position(&playlist->queue, current))
			current = 0;

201 202 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 */
		if (state != PLAYER_STATE_STOP)
			pc_update_audio();

207 208 209
		if (state == PLAYER_STATE_STOP /* && config_option */)
			playlist->current = current;
		else if (seek_time == 0)
210
			playlist_play(playlist, current);
211
		else
212
			playlist_seek_song(playlist, current, seek_time);
213 214

		if (state == PLAYER_STATE_PAUSE)
215
			pc_pause();
216
	}
217 218

	return true;
219
}
220 221 222 223

unsigned
playlist_state_get_hash(const struct playlist *playlist)
{
224 225 226 227
	struct player_status player_status;

	pc_get_status(&player_status);

228
	return playlist->queue.version ^
229 230 231
		(player_status.state != PLAYER_STATE_STOP
		 ? ((int)player_status.elapsed_time << 8)
		 : 0) ^
232 233 234 235
		(playlist->current >= 0
		 ? (queue_order_to_position(&playlist->queue,
					    playlist->current) << 16)
		 : 0) ^
236
		((int)pc_get_cross_fade() << 20) ^
237
		(player_status.state << 24) ^
238 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);
}