2023-03-06 14:42:04 +01:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
// Copyright The Music Player Daemon Project
|
2017-02-17 23:07:31 +01:00
|
|
|
|
|
|
|
#include "PartitionCommands.hxx"
|
|
|
|
#include "Request.hxx"
|
2017-02-17 23:21:20 +01:00
|
|
|
#include "Instance.hxx"
|
2017-02-17 23:18:51 +01:00
|
|
|
#include "Partition.hxx"
|
2017-02-17 23:59:06 +01:00
|
|
|
#include "IdleFlags.hxx"
|
2019-09-26 13:02:34 +02:00
|
|
|
#include "output/Filtered.hxx"
|
2017-02-17 23:18:51 +01:00
|
|
|
#include "client/Client.hxx"
|
2017-02-17 23:07:31 +01:00
|
|
|
#include "client/Response.hxx"
|
2017-02-17 23:38:30 +01:00
|
|
|
#include "util/CharUtil.hxx"
|
2017-02-17 23:07:31 +01:00
|
|
|
|
2021-05-21 20:35:29 +02:00
|
|
|
#include <fmt/format.h>
|
|
|
|
|
2017-02-20 12:37:14 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-02-17 23:07:31 +01:00
|
|
|
CommandResult
|
2017-02-17 23:18:51 +01:00
|
|
|
handle_listpartitions(Client &client, Request, Response &r)
|
2017-02-17 23:07:31 +01:00
|
|
|
{
|
2017-02-25 10:00:05 +01:00
|
|
|
for (const auto &partition : client.GetInstance().partitions) {
|
2021-05-21 20:35:29 +02:00
|
|
|
r.Fmt(FMT_STRING("partition: {}\n"), partition.name);
|
2017-02-17 23:21:20 +01:00
|
|
|
}
|
|
|
|
|
2017-02-17 23:07:31 +01:00
|
|
|
return CommandResult::OK;
|
|
|
|
}
|
2017-02-17 23:38:30 +01:00
|
|
|
|
|
|
|
static constexpr bool
|
|
|
|
IsValidPartitionChar(char ch)
|
|
|
|
{
|
|
|
|
return IsAlphaNumericASCII(ch) || ch == '-' || ch == '_';
|
|
|
|
}
|
|
|
|
|
2023-03-06 15:57:36 +01:00
|
|
|
[[gnu::pure]]
|
2017-02-17 23:38:30 +01:00
|
|
|
static bool
|
2017-06-04 13:09:11 +02:00
|
|
|
IsValidPartitionName(const char *name) noexcept
|
2017-02-17 23:38:30 +01:00
|
|
|
{
|
|
|
|
do {
|
|
|
|
if (!IsValidPartitionChar(*name))
|
|
|
|
return false;
|
|
|
|
} while (*++name != 0);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-03-06 15:57:36 +01:00
|
|
|
[[gnu::pure]]
|
2017-02-17 23:38:30 +01:00
|
|
|
static bool
|
2017-06-04 13:09:11 +02:00
|
|
|
HasPartitionNamed(Instance &instance, const char *name) noexcept
|
2017-02-17 23:38:30 +01:00
|
|
|
{
|
2017-02-20 12:38:27 +01:00
|
|
|
return instance.FindPartition(name) != nullptr;
|
2017-02-17 23:38:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-02-25 10:00:05 +01:00
|
|
|
auto &instance = client.GetInstance();
|
2017-02-17 23:38:30 +01:00
|
|
|
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,
|
2021-12-03 20:22:52 +01:00
|
|
|
client.GetPartition().config);
|
2017-02-17 23:38:30 +01:00
|
|
|
auto &partition = instance.partitions.back();
|
|
|
|
partition.UpdateEffectiveReplayGainMode();
|
|
|
|
|
2017-02-17 23:59:06 +01:00
|
|
|
instance.EmitIdle(IDLE_PARTITION);
|
|
|
|
|
2017-02-17 23:38:30 +01:00
|
|
|
return CommandResult::OK;
|
|
|
|
}
|
2019-09-26 13:02:34 +02:00
|
|
|
|
2020-01-20 09:10:02 +01:00
|
|
|
CommandResult
|
|
|
|
handle_delpartition(Client &client, Request request, Response &response)
|
|
|
|
{
|
|
|
|
const char *name = request.front();
|
|
|
|
if (!IsValidPartitionName(name)) {
|
|
|
|
response.Error(ACK_ERROR_ARG, "bad name");
|
|
|
|
return CommandResult::ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto &instance = client.GetInstance();
|
|
|
|
auto *partition = instance.FindPartition(name);
|
|
|
|
if (partition == nullptr) {
|
|
|
|
response.Error(ACK_ERROR_NO_EXIST, "no such partition");
|
|
|
|
return CommandResult::ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (partition == &instance.partitions.front()) {
|
|
|
|
response.Error(ACK_ERROR_UNKNOWN,
|
|
|
|
"cannot delete the default partition");
|
|
|
|
return CommandResult::ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!partition->clients.empty()) {
|
|
|
|
response.Error(ACK_ERROR_UNKNOWN,
|
|
|
|
"partition still has clients");
|
|
|
|
return CommandResult::ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!partition->outputs.IsDummy()) {
|
|
|
|
response.Error(ACK_ERROR_UNKNOWN,
|
|
|
|
"partition still has outputs");
|
|
|
|
return CommandResult::ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
partition->BeginShutdown();
|
|
|
|
instance.DeletePartition(*partition);
|
|
|
|
|
|
|
|
instance.EmitIdle(IDLE_PARTITION);
|
|
|
|
|
|
|
|
return CommandResult::OK;
|
|
|
|
}
|
|
|
|
|
2019-09-26 13:02:34 +02:00
|
|
|
CommandResult
|
|
|
|
handle_moveoutput(Client &client, Request request, Response &response)
|
|
|
|
{
|
2022-07-13 11:32:29 +02:00
|
|
|
const std::string_view output_name = request[0];
|
2019-09-26 13:02:34 +02:00
|
|
|
|
|
|
|
auto &dest_partition = client.GetPartition();
|
|
|
|
auto *existing_output = dest_partition.outputs.FindByName(output_name);
|
|
|
|
if (existing_output != nullptr && !existing_output->IsDummy())
|
|
|
|
/* this output is already in the specified partition,
|
|
|
|
so nothing needs to be done */
|
|
|
|
return CommandResult::OK;
|
|
|
|
|
|
|
|
/* find the partition which owns this output currently */
|
|
|
|
auto &instance = client.GetInstance();
|
|
|
|
for (auto &partition : instance.partitions) {
|
|
|
|
if (&partition == &dest_partition)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
auto *output = partition.outputs.FindByName(output_name);
|
|
|
|
if (output == nullptr || output->IsDummy())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
const bool was_enabled = output->IsEnabled();
|
|
|
|
|
|
|
|
if (existing_output != nullptr)
|
|
|
|
/* move the output back where it once was */
|
|
|
|
existing_output->ReplaceDummy(output->Steal(),
|
|
|
|
was_enabled);
|
|
|
|
else
|
2020-11-13 17:35:36 +01:00
|
|
|
/* copy the AudioOutputControl and add it to the output list */
|
2021-10-22 20:10:42 +02:00
|
|
|
dest_partition.outputs.AddMoveFrom(std::move(*output),
|
|
|
|
was_enabled);
|
2019-09-26 13:02:34 +02:00
|
|
|
|
|
|
|
instance.EmitIdle(IDLE_OUTPUT);
|
|
|
|
return CommandResult::OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
response.Error(ACK_ERROR_NO_EXIST, "No such output");
|
|
|
|
return CommandResult::ERROR;
|
|
|
|
}
|