OutputCommands.cxx 3.17 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2019 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
 * 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 "OutputCommands.hxx"
21
#include "Request.hxx"
22
#include "output/Print.hxx"
23
#include "output/OutputCommand.hxx"
24
#include "client/Client.hxx"
25
#include "client/Response.hxx"
26
#include "Partition.hxx"
27 28
#include "IdleFlags.hxx"
#include "util/CharUtil.hxx"
29

30
CommandResult
31
handle_enableoutput(Client &client, Request args, Response &r)
32
{
33
	assert(args.size == 1);
34
	unsigned device = args.ParseUnsigned(0);
35

36
	if (!audio_output_enable_index(client.GetPartition().outputs, device)) {
37
		r.Error(ACK_ERROR_NO_EXIST, "No such audio output");
38
		return CommandResult::ERROR;
39 40
	}

41
	return CommandResult::OK;
42 43
}

44
CommandResult
45
handle_disableoutput(Client &client, Request args, Response &r)
46
{
47
	assert(args.size == 1);
48
	unsigned device = args.ParseUnsigned(0);
49

50
	if (!audio_output_disable_index(client.GetPartition().outputs, device)) {
51
		r.Error(ACK_ERROR_NO_EXIST, "No such audio output");
52
		return CommandResult::ERROR;
53 54
	}

55
	return CommandResult::OK;
56 57
}

58
CommandResult
59
handle_toggleoutput(Client &client, Request args, Response &r)
60
{
61
	assert(args.size == 1);
62
	unsigned device = args.ParseUnsigned(0);
63

64
	if (!audio_output_toggle_index(client.GetPartition().outputs, device)) {
65
		r.Error(ACK_ERROR_NO_EXIST, "No such audio output");
66
		return CommandResult::ERROR;
67 68
	}

69
	return CommandResult::OK;
70 71
}

72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
static bool
IsValidAttributeNameChar(char ch) noexcept
{
	return IsAlphaNumericASCII(ch) || ch == '_';
}

gcc_pure
static bool
IsValidAttributeName(const char *s) noexcept
{
	do {
		if (!IsValidAttributeNameChar(*s))
			return false;
	} while (*++s);

	return true;
}

CommandResult
handle_outputset(Client &client, Request request, Response &response)
{
	assert(request.size == 3);
	const unsigned i = request.ParseUnsigned(0);

	auto &partition = client.GetPartition();
	auto &outputs = partition.outputs;
	if (i >= outputs.Size()) {
		response.Error(ACK_ERROR_NO_EXIST, "No such audio output");
		return CommandResult::ERROR;
	}

	auto &ao = outputs.Get(i);

	const char *const name = request[1];
	if (!IsValidAttributeName(name)) {
		response.Error(ACK_ERROR_ARG, "Illegal attribute name");
		return CommandResult::ERROR;
	}

	const char *const value = request[2];

	ao.SetAttribute(name, value);

	partition.EmitIdle(IDLE_OUTPUT);

	return CommandResult::OK;
}

120
CommandResult
121
handle_devices(Client &client, gcc_unused Request args, Response &r)
122
{
123
	assert(args.empty());
124

125
	printAudioDevices(r, client.GetPartition().outputs);
126
	return CommandResult::OK;
127
}