playlist_state.c 7.37 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
#include "playlist_state.h"
#include "playlist.h"
#include "player_control.h"
#include "queue_save.h"
#include "path.h"
31
#include "text_file.h"
32 33 34 35 36 37 38

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

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

	pc_get_status(&player_status);

62
	fputs(PLAYLIST_STATE_FILE_STATE, fp);
63 64

	if (playlist->playing) {
65
		switch (player_status.state) {
66
		case PLAYER_STATE_PAUSE:
67
			fputs(PLAYLIST_STATE_FILE_STATE_PAUSE "\n", fp);
68 69
			break;
		default:
70
			fputs(PLAYLIST_STATE_FILE_STATE_PLAY "\n", fp);
71
		}
72
		fprintf(fp, PLAYLIST_STATE_FILE_CURRENT "%i\n",
73 74
			queue_order_to_position(&playlist->queue,
						playlist->current));
75
		fprintf(fp, PLAYLIST_STATE_FILE_TIME "%i\n",
76
			(int)player_status.elapsed_time);
77
	} else {
78
		fputs(PLAYLIST_STATE_FILE_STATE_STOP "\n", fp);
79 80

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

86 87 88 89
	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);
	fprintf(fp, PLAYLIST_STATE_FILE_CONSUME "%i\n",
90
		playlist->queue.consume);
91
	fprintf(fp, PLAYLIST_STATE_FILE_CROSSFADE "%i\n",
92
		(int)(pc_get_cross_fade()));
93 94
	fprintf(fp, PLAYLIST_STATE_FILE_MIXRAMPDB "%f\n", pc_get_mixramp_db());
	fprintf(fp, PLAYLIST_STATE_FILE_MIXRAMPDELAY "%f\n",
95
		pc_get_mixramp_delay());
96
	fputs(PLAYLIST_STATE_FILE_PLAYLIST_BEGIN "\n", fp);
97
	queue_save(fp, &playlist->queue);
98
	fputs(PLAYLIST_STATE_FILE_PLAYLIST_END "\n", fp);
99 100 101
}

static void
102
playlist_state_load(FILE *fp, GString *buffer, struct playlist *playlist)
103
{
104 105
	const char *line = read_text_line(fp, buffer);
	if (line == NULL) {
106 107 108 109
		g_warning("No playlist in state file");
		return;
	}

110
	while (!g_str_has_prefix(line, PLAYLIST_STATE_FILE_PLAYLIST_END)) {
111
		queue_load_song(fp, buffer, line, &playlist->queue);
112

113 114
		line = read_text_line(fp, buffer);
		if (line == NULL) {
115 116
			g_warning("'" PLAYLIST_STATE_FILE_PLAYLIST_END
				  "' not found in state file");
117 118 119
			break;
		}
	}
120 121

	queue_increment_version(&playlist->queue);
122 123
}

124
bool
125 126
playlist_state_restore(const char *line, FILE *fp, GString *buffer,
		       struct playlist *playlist)
127 128 129 130 131 132
{
	int current = -1;
	int seek_time = 0;
	int state = PLAYER_STATE_STOP;
	bool random_mode = false;

133 134 135 136 137 138 139 140 141 142
	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;

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

188
	playlist_set_random(playlist, random_mode);
189

190
	if (!queue_is_empty(&playlist->queue)) {
191 192 193
		if (!queue_valid_position(&playlist->queue, current))
			current = 0;

194 195 196 197 198 199
		/* 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();

200 201 202
		if (state == PLAYER_STATE_STOP /* && config_option */)
			playlist->current = current;
		else if (seek_time == 0)
203
			playlist_play(playlist, current);
204
		else
205
			playlist_seek_song(playlist, current, seek_time);
206 207

		if (state == PLAYER_STATE_PAUSE)
208
			pc_pause();
209
	}
210 211

	return true;
212
}
213 214 215 216

unsigned
playlist_state_get_hash(const struct playlist *playlist)
{
217 218 219 220
	struct player_status player_status;

	pc_get_status(&player_status);

221
	return playlist->queue.version ^
222 223 224
		(player_status.state != PLAYER_STATE_STOP
		 ? ((int)player_status.elapsed_time << 8)
		 : 0) ^
225 226 227 228
		(playlist->current >= 0
		 ? (queue_order_to_position(&playlist->queue,
					    playlist->current) << 16)
		 : 0) ^
229
		((int)pc_get_cross_fade() << 20) ^
230
		(player_status.state << 24) ^
231 232 233 234 235 236
		(playlist->queue.random << 27) ^
		(playlist->queue.repeat << 28) ^
		(playlist->queue.single << 29) ^
		(playlist->queue.consume << 30) ^
		(playlist->queue.random << 31);
}