Skip to content

Commit

Permalink
[pipewire] Implement notify-on-constructor and better port type tracking
Browse files Browse the repository at this point in the history
  • Loading branch information
jcelerier committed Aug 26, 2024
1 parent b381443 commit 71d4ed1
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 16 deletions.
32 changes: 18 additions & 14 deletions include/libremidi/backends/pipewire/helpers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -437,30 +437,34 @@ struct pipewire_helpers
// Note: keep in mind that an "input" port for us (e.g. a keyboard that goes to the computer)
// is an "output" port from the point of view of pipewire as data will come out of it
template <spa_direction Direction>
static auto get_ports(const pipewire_context& ctx) noexcept -> std::vector<
std::conditional_t<Direction == SPA_DIRECTION_OUTPUT, input_port, output_port>>
static auto get_ports(const observer_configuration& conf, const pipewire_context& ctx) noexcept
-> std::vector<
std::conditional_t<Direction == SPA_DIRECTION_OUTPUT, input_port, output_port>>
{
std::vector<std::conditional_t<Direction == SPA_DIRECTION_OUTPUT, input_port, output_port>>
ret;

{
std::lock_guard _{ctx.current_graph.mtx};
for (auto& node : ctx.current_graph.physical_midi)
{
for (auto& p :
(Direction == SPA_DIRECTION_INPUT ? node.second.inputs : node.second.outputs))
if (conf.track_any || conf.track_hardware)
for (auto& node : ctx.current_graph.physical_midi)
{
ret.push_back(to_port_info<Direction>(p));
for (auto& port :
(Direction == SPA_DIRECTION_INPUT ? node.second.inputs : node.second.outputs))
{
ret.push_back(to_port_info<Direction>(port));
}
}
}
for (auto& node : ctx.current_graph.software_midi)
{
for (auto& p :
(Direction == SPA_DIRECTION_INPUT ? node.second.inputs : node.second.outputs))

if (conf.track_any || conf.track_virtual)
for (auto& node : ctx.current_graph.software_midi)
{
ret.push_back(to_port_info<Direction>(p));
for (auto& port :
(Direction == SPA_DIRECTION_INPUT ? node.second.inputs : node.second.outputs))
{
ret.push_back(to_port_info<Direction>(port));
}
}
}
}

return ret;
Expand Down
15 changes: 13 additions & 2 deletions include/libremidi/backends/pipewire/observer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,29 @@ class observer_pipewire final
this->add_callbacks(configuration);
this->start_thread();
}

if (configuration.notify_in_constructor)
{
if (configuration.input_added)
for (const auto& p : get_input_ports())
configuration.input_added(p);

if (configuration.output_added)
for (const auto& p : get_output_ports())
configuration.output_added(p);
}
}

libremidi::API get_current_api() const noexcept override { return libremidi::API::PIPEWIRE; }

std::vector<libremidi::input_port> get_input_ports() const noexcept override
{
return get_ports<SPA_DIRECTION_OUTPUT>(*this->global_context);
return get_ports<SPA_DIRECTION_OUTPUT>(this->configuration, *this->global_context);
}

std::vector<libremidi::output_port> get_output_ports() const noexcept override
{
return get_ports<SPA_DIRECTION_INPUT>(*this->global_context);
return get_ports<SPA_DIRECTION_INPUT>(this->configuration, *this->global_context);
}

~observer_pipewire()
Expand Down

0 comments on commit 71d4ed1

Please sign in to comment.