OutputCommands.cxx 3.19 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2017 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 "OutputCommands.hxx"
22
#include "Request.hxx"
23
#include "output/Print.hxx"
24
#include "output/OutputCommand.hxx"
25
#include "client/Client.hxx"
26
#include "client/Response.hxx"
27
#include "Partition.hxx"
28 29
#include "IdleFlags.hxx"
#include "util/CharUtil.hxx"
30

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

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

42
	return CommandResult::OK;
43 44
}

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

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

56
	return CommandResult::OK;
57 58
}

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

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

70
	return CommandResult::OK;
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 120
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;
}

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

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