PartitionCommands.cxx 3.05 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 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 "PartitionCommands.hxx"
#include "Request.hxx"
22
#include "Instance.hxx"
23
#include "Partition.hxx"
24
#include "IdleFlags.hxx"
25
#include "client/Client.hxx"
26
#include "client/Response.hxx"
27
#include "util/CharUtil.hxx"
28

29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
CommandResult
handle_partition(Client &client, Request request, Response &response)
{
	const char *name = request.front();
	auto &instance = client.GetInstance();
	auto *partition = instance.FindPartition(name);
	if (partition == nullptr) {
		response.Error(ACK_ERROR_NO_EXIST, "partition does not exist");
		return CommandResult::ERROR;
	}

	client.SetPartition(*partition);
	return CommandResult::OK;
}

44
CommandResult
45
handle_listpartitions(Client &client, Request, Response &r)
46
{
47
	for (const auto &partition : client.GetInstance().partitions) {
48 49 50
		r.Format("partition: %s\n", partition.name.c_str());
	}

51 52
	return CommandResult::OK;
}
53 54 55 56 57 58 59 60 61

static constexpr bool
IsValidPartitionChar(char ch)
{
	return IsAlphaNumericASCII(ch) || ch == '-' || ch == '_';
}

gcc_pure
static bool
62
IsValidPartitionName(const char *name) noexcept
63 64 65 66 67 68 69 70 71 72 73
{
	do {
		if (!IsValidPartitionChar(*name))
			return false;
	} while (*++name != 0);

	return true;
}

gcc_pure
static bool
74
HasPartitionNamed(Instance &instance, const char *name) noexcept
75
{
76
	return instance.FindPartition(name) != nullptr;
77 78 79 80 81 82 83 84 85 86 87
}

CommandResult
handle_newpartition(Client &client, Request request, Response &response)
{
	const char *name = request.front();
	if (!IsValidPartitionName(name)) {
		response.Error(ACK_ERROR_ARG, "bad name");
		return CommandResult::ERROR;
	}

88
	auto &instance = client.GetInstance();
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
	if (HasPartitionNamed(instance, name)) {
		response.Error(ACK_ERROR_EXIST, "name already exists");
		return CommandResult::ERROR;
	}

	if (instance.partitions.size() >= 16) {
		/* arbitrary limit for now */
		response.Error(ACK_ERROR_UNKNOWN, "too many partitions");
		return CommandResult::ERROR;
	}

	instance.partitions.emplace_back(instance, name,
					 // TODO: use real configuration
					 16384,
					 1024,
					 AudioFormat::Undefined(),
					 ReplayGainConfig());
	auto &partition = instance.partitions.back();
	partition.outputs.AddNullOutput(instance.io_thread.GetEventLoop(),
					ReplayGainConfig(),
					partition.pc);
	partition.UpdateEffectiveReplayGainMode();

112 113
	instance.EmitIdle(IDLE_PARTITION);

114 115
	return CommandResult::OK;
}