Skip to content

Commit

Permalink
Instance: add method FindOutput()
Browse files Browse the repository at this point in the history
Move code from handle_moveoutput().
  • Loading branch information
MaxKellermann committed Nov 11, 2024
1 parent 370df37 commit 8db14c9
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 23 deletions.
16 changes: 16 additions & 0 deletions src/Instance.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,22 @@ Instance::DeletePartition(Partition &partition) noexcept
}
}

AudioOutputControl *
Instance::FindOutput(std::string_view name,
Partition &excluding_partition) noexcept
{
for (auto &partition : partitions) {
if (&partition == &excluding_partition)
continue;

auto *output = partition.outputs.FindByName(name);
if (output != nullptr && !output->IsDummy())
return output;
}

return nullptr;
}

#ifdef ENABLE_DATABASE

const Database &
Expand Down
12 changes: 12 additions & 0 deletions src/Instance.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class InotifyUpdate;

class ClientList;
struct Partition;
class AudioOutputControl;
class StateFile;
class RemoteTagCache;
class StickerDatabase;
Expand Down Expand Up @@ -171,6 +172,17 @@ struct Instance final

void BeginShutdownPartitions() noexcept;

/**
* Returns the (non-dummy) audio output device with the
* specified name. Returns nullptr if the name does not
* exist.
*
* @param excluding_partition ignore this partition
*/
[[gnu::pure]]
AudioOutputControl *FindOutput(std::string_view name,
Partition &excluding_partition) noexcept;

#ifdef ENABLE_DATABASE
/**
* Returns the global #Database instance. May return nullptr
Expand Down
41 changes: 18 additions & 23 deletions src/command/PartitionCommands.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -145,29 +145,24 @@ handle_moveoutput(Client &client, Request request, Response &response)

/* 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
/* copy the AudioOutputControl and add it to the output list */
dest_partition.outputs.AddMoveFrom(std::move(*output),
was_enabled);

instance.EmitIdle(IDLE_OUTPUT);
return CommandResult::OK;

auto *output = instance.FindOutput(output_name, dest_partition);
if (output == nullptr) {
response.Error(ACK_ERROR_NO_EXIST, "No such output");
return CommandResult::ERROR;
}

response.Error(ACK_ERROR_NO_EXIST, "No such output");
return CommandResult::ERROR;
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
/* copy the AudioOutputControl and add it to the output list */
dest_partition.outputs.AddMoveFrom(std::move(*output),
was_enabled);

instance.EmitIdle(IDLE_OUTPUT);
return CommandResult::OK;
}

0 comments on commit 8db14c9

Please sign in to comment.