PlayerCommands.cxx 10.6 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2014 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 * 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.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include "config.h"
#include "PlayerCommands.hxx"
22
#include "CommandError.hxx"
23
#include "queue/Playlist.hxx"
24
#include "PlaylistPrint.hxx"
25
#include "client/Client.hxx"
Max Kellermann's avatar
Max Kellermann committed
26
#include "mixer/Volume.hxx"
27
#include "Partition.hxx"
28
#include "Instance.hxx"
Max Kellermann's avatar
Max Kellermann committed
29 30
#include "protocol/Result.hxx"
#include "protocol/ArgParser.hxx"
31
#include "AudioFormat.hxx"
32
#include "ReplayGainConfig.hxx"
33

34 35 36 37
#ifdef ENABLE_DATABASE
#include "db/update/Service.hxx"
#endif

38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
#define COMMAND_STATUS_STATE            "state"
#define COMMAND_STATUS_REPEAT           "repeat"
#define COMMAND_STATUS_SINGLE           "single"
#define COMMAND_STATUS_CONSUME          "consume"
#define COMMAND_STATUS_RANDOM           "random"
#define COMMAND_STATUS_PLAYLIST         "playlist"
#define COMMAND_STATUS_PLAYLIST_LENGTH  "playlistlength"
#define COMMAND_STATUS_SONG             "song"
#define COMMAND_STATUS_SONGID           "songid"
#define COMMAND_STATUS_NEXTSONG         "nextsong"
#define COMMAND_STATUS_NEXTSONGID       "nextsongid"
#define COMMAND_STATUS_TIME             "time"
#define COMMAND_STATUS_BITRATE          "bitrate"
#define COMMAND_STATUS_ERROR            "error"
#define COMMAND_STATUS_CROSSFADE	"xfade"
#define COMMAND_STATUS_MIXRAMPDB	"mixrampdb"
#define COMMAND_STATUS_MIXRAMPDELAY	"mixrampdelay"
#define COMMAND_STATUS_AUDIO		"audio"
#define COMMAND_STATUS_UPDATING_DB	"updating_db"

58
CommandResult
59
handle_play(Client &client, unsigned argc, char *argv[])
60 61 62 63
{
	int song = -1;

	if (argc == 2 && !check_int(client, &song, argv[1]))
64
		return CommandResult::ERROR;
65
	PlaylistResult result = client.partition.PlayPosition(song);
66 67 68
	return print_playlist_result(client, result);
}

69
CommandResult
70
handle_playid(Client &client, unsigned argc, char *argv[])
71 72 73 74
{
	int id = -1;

	if (argc == 2 && !check_int(client, &id, argv[1]))
75
		return CommandResult::ERROR;
76

77
	PlaylistResult result = client.partition.PlayId(id);
78 79 80
	return print_playlist_result(client, result);
}

81
CommandResult
82
handle_stop(Client &client,
83
	    gcc_unused unsigned argc, gcc_unused char *argv[])
84
{
85
	client.partition.Stop();
86
	return CommandResult::OK;
87 88
}

89
CommandResult
90
handle_currentsong(Client &client,
91
		   gcc_unused unsigned argc, gcc_unused char *argv[])
92
{
93
	playlist_print_current(client, client.playlist);
94
	return CommandResult::OK;
95 96
}

97
CommandResult
98
handle_pause(Client &client,
99
	     unsigned argc, char *argv[])
100 101 102 103
{
	if (argc == 2) {
		bool pause_flag;
		if (!check_bool(client, &pause_flag, argv[1]))
104
			return CommandResult::ERROR;
105

106
		client.player_control.SetPause(pause_flag);
107
	} else
108
		client.player_control.Pause();
109

110
	return CommandResult::OK;
111 112
}

113
CommandResult
114
handle_status(Client &client,
115
	      gcc_unused unsigned argc, gcc_unused char *argv[])
116
{
117
	const char *state = nullptr;
118 119
	int song;

120
	const auto player_status = client.player_control.GetStatus();
121 122

	switch (player_status.state) {
123
	case PlayerState::STOP:
124 125
		state = "stop";
		break;
126
	case PlayerState::PAUSE:
127 128
		state = "pause";
		break;
129
	case PlayerState::PLAY:
130 131 132 133
		state = "play";
		break;
	}

134
	const playlist &playlist = client.playlist;
135 136 137 138 139 140 141 142 143 144
	client_printf(client,
		      "volume: %i\n"
		      COMMAND_STATUS_REPEAT ": %i\n"
		      COMMAND_STATUS_RANDOM ": %i\n"
		      COMMAND_STATUS_SINGLE ": %i\n"
		      COMMAND_STATUS_CONSUME ": %i\n"
		      COMMAND_STATUS_PLAYLIST ": %li\n"
		      COMMAND_STATUS_PLAYLIST_LENGTH ": %i\n"
		      COMMAND_STATUS_MIXRAMPDB ": %f\n"
		      COMMAND_STATUS_STATE ": %s\n",
145
		      volume_level_get(client.partition.outputs),
146 147 148 149 150 151
		      playlist.GetRepeat(),
		      playlist.GetRandom(),
		      playlist.GetSingle(),
		      playlist.GetConsume(),
		      (unsigned long)playlist.GetVersion(),
		      playlist.GetLength(),
152
		      client.player_control.GetMixRampDb(),
153 154
		      state);

155 156 157 158 159 160 161 162 163 164
	if (client.player_control.GetCrossFade() > 0)
		client_printf(client,
			      COMMAND_STATUS_CROSSFADE ": %i\n",
			      int(client.player_control.GetCrossFade() + 0.5));

	if (client.player_control.GetMixRampDelay() > 0)
		client_printf(client,
			      COMMAND_STATUS_MIXRAMPDELAY ": %f\n",
			      client.player_control.GetMixRampDelay());

165
	song = playlist.GetCurrentPosition();
166 167 168 169
	if (song >= 0) {
		client_printf(client,
			      COMMAND_STATUS_SONG ": %i\n"
			      COMMAND_STATUS_SONGID ": %u\n",
170
			      song, playlist.PositionToId(song));
171 172
	}

173
	if (player_status.state != PlayerState::STOP) {
174 175 176
		client_printf(client,
			      COMMAND_STATUS_TIME ": %i:%i\n"
			      "elapsed: %1.3f\n"
177
			      COMMAND_STATUS_BITRATE ": %u\n",
178 179 180
			      (int)(player_status.elapsed_time + 0.5),
			      (int)(player_status.total_time + 0.5),
			      player_status.elapsed_time,
181 182
			      player_status.bit_rate);

183
		if (player_status.audio_format.IsDefined()) {
184 185 186 187
			struct audio_format_string af_string;

			client_printf(client,
				      COMMAND_STATUS_AUDIO ": %s\n",
188
				      audio_format_to_string(player_status.audio_format,
189 190
							     &af_string));
		}
191 192
	}

193
#ifdef ENABLE_DATABASE
194 195 196 197 198
	const UpdateService *update_service = client.partition.instance.update;
	unsigned updateJobId = update_service != nullptr
		? update_service->GetId()
		: 0;
	if (updateJobId != 0) {
199 200 201 202
		client_printf(client,
			      COMMAND_STATUS_UPDATING_DB ": %i\n",
			      updateJobId);
	}
203
#endif
204

205
	Error error = client.player_control.LockGetError();
206
	if (error.IsDefined())
207 208
		client_printf(client,
			      COMMAND_STATUS_ERROR ": %s\n",
209
			      error.GetMessage());
210

211
	song = playlist.GetNextPosition();
212 213 214 215
	if (song >= 0) {
		client_printf(client,
			      COMMAND_STATUS_NEXTSONG ": %i\n"
			      COMMAND_STATUS_NEXTSONGID ": %u\n",
216
			      song, playlist.PositionToId(song));
217 218
	}

219
	return CommandResult::OK;
220 221
}

222
CommandResult
223
handle_next(Client &client,
224
	    gcc_unused unsigned argc, gcc_unused char *argv[])
225
{
226
	playlist &playlist = client.playlist;
227

228 229
	/* single mode is not considered when this is user who
	 * wants to change song. */
230 231
	const bool single = playlist.queue.single;
	playlist.queue.single = false;
232

233
	client.partition.PlayNext();
234

235
	playlist.queue.single = single;
236
	return CommandResult::OK;
237 238
}

239
CommandResult
240
handle_previous(Client &client,
241
		gcc_unused unsigned argc, gcc_unused char *argv[])
242
{
243
	client.partition.PlayPrevious();
244
	return CommandResult::OK;
245 246
}

247
CommandResult
248
handle_repeat(Client &client, gcc_unused unsigned argc, char *argv[])
249 250 251
{
	bool status;
	if (!check_bool(client, &status, argv[1]))
252
		return CommandResult::ERROR;
253

254
	client.partition.SetRepeat(status);
255
	return CommandResult::OK;
256 257
}

258
CommandResult
259
handle_single(Client &client, gcc_unused unsigned argc, char *argv[])
260 261 262
{
	bool status;
	if (!check_bool(client, &status, argv[1]))
263
		return CommandResult::ERROR;
264

265
	client.partition.SetSingle(status);
266
	return CommandResult::OK;
267 268
}

269
CommandResult
270
handle_consume(Client &client, gcc_unused unsigned argc, char *argv[])
271 272 273
{
	bool status;
	if (!check_bool(client, &status, argv[1]))
274
		return CommandResult::ERROR;
275

276
	client.partition.SetConsume(status);
277
	return CommandResult::OK;
278 279
}

280
CommandResult
281
handle_random(Client &client, gcc_unused unsigned argc, char *argv[])
282 283 284
{
	bool status;
	if (!check_bool(client, &status, argv[1]))
285
		return CommandResult::ERROR;
286

287
	client.partition.SetRandom(status);
288
	client.partition.outputs.SetReplayGainMode(replay_gain_get_real_mode(client.partition.GetRandom()));
289
	return CommandResult::OK;
290 291
}

292
CommandResult
293
handle_clearerror(gcc_unused Client &client,
294
		  gcc_unused unsigned argc, gcc_unused char *argv[])
295
{
296
	client.player_control.ClearError();
297
	return CommandResult::OK;
298 299
}

300
CommandResult
301
handle_seek(Client &client, gcc_unused unsigned argc, char *argv[])
302 303 304 305
{
	unsigned song, seek_time;

	if (!check_unsigned(client, &song, argv[1]))
306
		return CommandResult::ERROR;
307
	if (!check_unsigned(client, &seek_time, argv[2]))
308
		return CommandResult::ERROR;
309

310
	PlaylistResult result =
311
		client.partition.SeekSongPosition(song, seek_time);
312 313 314
	return print_playlist_result(client, result);
}

315
CommandResult
316
handle_seekid(Client &client, gcc_unused unsigned argc, char *argv[])
317 318 319 320
{
	unsigned id, seek_time;

	if (!check_unsigned(client, &id, argv[1]))
321
		return CommandResult::ERROR;
322
	if (!check_unsigned(client, &seek_time, argv[2]))
323
		return CommandResult::ERROR;
324

325
	PlaylistResult result =
326
		client.partition.SeekSongId(id, seek_time);
327 328 329
	return print_playlist_result(client, result);
}

330
CommandResult
331
handle_seekcur(Client &client, gcc_unused unsigned argc, char *argv[])
332 333 334 335 336
{
	const char *p = argv[1];
	bool relative = *p == '+' || *p == '-';
	int seek_time;
	if (!check_int(client, &seek_time, p))
337
		return CommandResult::ERROR;
338

339
	PlaylistResult result =
340
		client.partition.SeekCurrent(seek_time, relative);
341 342 343
	return print_playlist_result(client, result);
}

344
CommandResult
345
handle_crossfade(Client &client, gcc_unused unsigned argc, char *argv[])
346 347 348 349
{
	unsigned xfade_time;

	if (!check_unsigned(client, &xfade_time, argv[1]))
350
		return CommandResult::ERROR;
351
	client.player_control.SetCrossFade(xfade_time);
352

353
	return CommandResult::OK;
354 355
}

356
CommandResult
357
handle_mixrampdb(Client &client, gcc_unused unsigned argc, char *argv[])
358 359 360 361
{
	float db;

	if (!check_float(client, &db, argv[1]))
362
		return CommandResult::ERROR;
363
	client.player_control.SetMixRampDb(db);
364

365
	return CommandResult::OK;
366 367
}

368
CommandResult
369
handle_mixrampdelay(Client &client, gcc_unused unsigned argc, char *argv[])
370 371 372 373
{
	float delay_secs;

	if (!check_float(client, &delay_secs, argv[1]))
374
		return CommandResult::ERROR;
375
	client.player_control.SetMixRampDelay(delay_secs);
376

377
	return CommandResult::OK;
378 379
}

380
CommandResult
381
handle_replay_gain_mode(Client &client,
382
			gcc_unused unsigned argc, char *argv[])
383 384 385 386
{
	if (!replay_gain_set_mode_string(argv[1])) {
		command_error(client, ACK_ERROR_ARG,
			      "Unrecognized replay gain mode");
387
		return CommandResult::ERROR;
388 389
	}

390
	client.partition.outputs.SetReplayGainMode(replay_gain_get_real_mode(client.playlist.queue.random));
391
	return CommandResult::OK;
392 393
}

394
CommandResult
395
handle_replay_gain_status(Client &client,
396
			  gcc_unused unsigned argc, gcc_unused char *argv[])
397 398 399
{
	client_printf(client, "replay_gain_mode: %s\n",
		      replay_gain_get_mode_string());
400
	return CommandResult::OK;
401
}