libremidi update to v5.4.3 and added virtual midi out as default
This commit is contained in:
@@ -6,9 +6,10 @@
|
||||
#if LIBREMIDI_HAS_UDEV
|
||||
#include <libremidi/backends/linux/helpers.hpp>
|
||||
#include <libremidi/backends/linux/udev.hpp>
|
||||
#include <libremidi/detail/observer.hpp>
|
||||
#endif
|
||||
#include <libremidi/detail/observer.hpp>
|
||||
|
||||
namespace libremidi::alsa_raw
|
||||
NAMESPACE_LIBREMIDI::alsa_raw
|
||||
{
|
||||
template <typename Enumerator>
|
||||
class observer_impl_base
|
||||
@@ -27,28 +28,39 @@ public:
|
||||
explicit observer_impl_base(
|
||||
observer_configuration&& conf, alsa_raw_observer_configuration&& apiconf)
|
||||
: configuration{std::move(conf), std::move(apiconf)}
|
||||
{
|
||||
}
|
||||
|
||||
~observer_impl_base()
|
||||
{
|
||||
#if LIBREMIDI_HAS_UDEV
|
||||
m_termination_event.notify();
|
||||
|
||||
if (m_thread.joinable())
|
||||
m_thread.join();
|
||||
#endif
|
||||
}
|
||||
|
||||
// Must be called for child classes due to virtual function calls
|
||||
void finish_init()
|
||||
{
|
||||
if (!configuration.has_callbacks())
|
||||
return;
|
||||
|
||||
fds[0] = this->udev;
|
||||
fds[1] = termination_event;
|
||||
fds[2] = timer_fd;
|
||||
#if LIBREMIDI_HAS_UDEV
|
||||
m_fds[0] = this->m_udev;
|
||||
m_fds[1] = m_termination_event;
|
||||
m_fds[2] = m_timer_fd;
|
||||
#endif
|
||||
|
||||
// Set-up initial state
|
||||
if (configuration.notify_in_constructor)
|
||||
this->check_devices();
|
||||
|
||||
#if LIBREMIDI_HAS_UDEV
|
||||
// Start thread
|
||||
thread = std::thread{[this] { this->run(); }};
|
||||
}
|
||||
|
||||
~observer_impl_base()
|
||||
{
|
||||
termination_event.notify();
|
||||
|
||||
if (thread.joinable())
|
||||
thread.join();
|
||||
m_thread = std::thread{[this] { this->run(); }};
|
||||
#endif
|
||||
}
|
||||
|
||||
std::vector<libremidi::input_port> get_input_ports() const noexcept override
|
||||
@@ -77,12 +89,12 @@ public:
|
||||
return ret;
|
||||
}
|
||||
|
||||
private:
|
||||
#if LIBREMIDI_HAS_UDEV
|
||||
void run()
|
||||
{
|
||||
for (;;)
|
||||
{
|
||||
if (int err = poll(fds, 3, -1); err < 0)
|
||||
if (int err = poll(m_fds, 3, -1); err < 0)
|
||||
{
|
||||
if (err == -EAGAIN)
|
||||
continue;
|
||||
@@ -90,59 +102,87 @@ private:
|
||||
return;
|
||||
}
|
||||
|
||||
// Check udev
|
||||
if (fds[0].revents & POLLIN)
|
||||
// Check eventfd to see if we need to exit
|
||||
if (m_fds[1].revents & POLLIN)
|
||||
{
|
||||
udev_device* dev = udev.udev.monitor_receive_device(udev.monitor);
|
||||
break;
|
||||
}
|
||||
|
||||
// Check udev
|
||||
if (m_fds[0].revents & POLLIN)
|
||||
{
|
||||
udev_device* dev = m_udev.udev.monitor_receive_device(m_udev.monitor);
|
||||
if (!dev)
|
||||
continue;
|
||||
|
||||
std::string_view act = udev.udev.device_get_action(dev);
|
||||
std::string_view ss = udev.udev.device_get_subsystem(dev);
|
||||
std::string_view act = m_udev.udev.device_get_action(dev);
|
||||
std::string_view ss = m_udev.udev.device_get_subsystem(dev);
|
||||
if (!act.empty() && ss == "snd_seq")
|
||||
{
|
||||
if (act == "add" || act == "remove")
|
||||
{
|
||||
// Check every 100 milliseconds for ten seconds
|
||||
this->timer_fd.restart(configuration.poll_period.count());
|
||||
timer_check_counts = 100;
|
||||
this->m_timer_fd.restart(configuration.poll_period.count());
|
||||
m_timer_check_counts = 100;
|
||||
}
|
||||
}
|
||||
|
||||
udev.udev.device_unref(dev);
|
||||
m_udev.udev.device_unref(dev);
|
||||
|
||||
fds[0].revents = 0;
|
||||
}
|
||||
|
||||
// Check eventfd
|
||||
if (fds[1].revents & POLLIN)
|
||||
{
|
||||
break;
|
||||
m_fds[0].revents = 0;
|
||||
}
|
||||
|
||||
// Check timer
|
||||
if (fds[2].revents & POLLIN)
|
||||
if (m_fds[2].revents & POLLIN)
|
||||
{
|
||||
if (this->timer_check_counts-- <= 0)
|
||||
this->timer_fd.cancel();
|
||||
fds[2].revents = 0;
|
||||
if (this->m_timer_check_counts-- <= 0)
|
||||
this->m_timer_fd.cancel();
|
||||
m_fds[2].revents = 0;
|
||||
|
||||
check_devices();
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
template <bool Input>
|
||||
auto to_port_info(alsa_raw::alsa_raw_port_info p) const noexcept
|
||||
auto to_port_info(const alsa_raw::alsa_raw_port_info& p) const noexcept
|
||||
-> std::conditional_t<Input, input_port, output_port>
|
||||
{
|
||||
#if LIBREMIDI_HAS_UDEV
|
||||
auto [container, device, type, manufacturer, product, serial]
|
||||
= get_udev_soundcard_info(m_udev, p.card);
|
||||
#else
|
||||
container_identifier container{};
|
||||
device_identifier device{};
|
||||
libremidi::transport_type type{};
|
||||
std::string manufacturer;
|
||||
std::string product;
|
||||
std::string serial;
|
||||
#endif
|
||||
std::string display_name = p.subdevice_name;
|
||||
if (display_name.empty())
|
||||
display_name = p.device_name;
|
||||
if (display_name.empty())
|
||||
display_name = p.card_name;
|
||||
if (display_name.empty())
|
||||
display_name = product;
|
||||
if (display_name.empty())
|
||||
display_name = "unknown";
|
||||
|
||||
return {
|
||||
{.client = 0,
|
||||
{.api = get_current_api(),
|
||||
.client = 0,
|
||||
.container = container,
|
||||
.device = device,
|
||||
.port = raw_to_port_handle({p.card, p.dev, p.sub}),
|
||||
.manufacturer = p.card_name,
|
||||
.manufacturer = manufacturer,
|
||||
.product = product,
|
||||
.serial = serial,
|
||||
.device_name = p.device_name,
|
||||
.port_name = p.subdevice_name,
|
||||
.display_name = p.subdevice_name}};
|
||||
.display_name = std::move(display_name),
|
||||
.type = type}};
|
||||
}
|
||||
|
||||
void check_devices()
|
||||
@@ -151,7 +191,7 @@ private:
|
||||
|
||||
new_devs.enumerate_cards();
|
||||
|
||||
for (auto& in_prev : current_inputs)
|
||||
for (auto& in_prev : m_current_inputs)
|
||||
{
|
||||
if (auto it = std::find(new_devs.inputs.begin(), new_devs.inputs.end(), in_prev);
|
||||
it == new_devs.inputs.end())
|
||||
@@ -165,8 +205,8 @@ private:
|
||||
|
||||
for (auto& in_next : new_devs.inputs)
|
||||
{
|
||||
if (auto it = std::find(current_inputs.begin(), current_inputs.end(), in_next);
|
||||
it == current_inputs.end())
|
||||
if (auto it = std::find(m_current_inputs.begin(), m_current_inputs.end(), in_next);
|
||||
it == m_current_inputs.end())
|
||||
{
|
||||
if (auto& cb = this->configuration.input_added)
|
||||
{
|
||||
@@ -175,7 +215,7 @@ private:
|
||||
}
|
||||
}
|
||||
|
||||
for (auto& out_prev : current_outputs)
|
||||
for (auto& out_prev : m_current_outputs)
|
||||
{
|
||||
if (auto it = std::find(new_devs.outputs.begin(), new_devs.outputs.end(), out_prev);
|
||||
it == new_devs.outputs.end())
|
||||
@@ -189,8 +229,8 @@ private:
|
||||
|
||||
for (auto& out_next : new_devs.outputs)
|
||||
{
|
||||
if (auto it = std::find(current_outputs.begin(), current_outputs.end(), out_next);
|
||||
it == current_outputs.end())
|
||||
if (auto it = std::find(m_current_outputs.begin(), m_current_outputs.end(), out_next);
|
||||
it == m_current_outputs.end())
|
||||
{
|
||||
if (auto& cb = this->configuration.output_added)
|
||||
{
|
||||
@@ -198,43 +238,34 @@ private:
|
||||
}
|
||||
}
|
||||
}
|
||||
current_inputs = std::move(new_devs.inputs);
|
||||
current_outputs = std::move(new_devs.outputs);
|
||||
m_current_inputs = std::move(new_devs.inputs);
|
||||
m_current_outputs = std::move(new_devs.outputs);
|
||||
}
|
||||
|
||||
udev_helper udev{};
|
||||
eventfd_notifier termination_event{};
|
||||
timerfd_timer timer_fd{};
|
||||
int timer_check_counts = 0;
|
||||
std::thread thread;
|
||||
std::vector<alsa_raw_port_info> current_inputs;
|
||||
std::vector<alsa_raw_port_info> current_outputs;
|
||||
|
||||
pollfd fds[3]{};
|
||||
};
|
||||
}
|
||||
#else
|
||||
#include <libremidi/backends/dummy.hpp>
|
||||
namespace libremidi::alsa_raw
|
||||
{
|
||||
template <typename Enumerator>
|
||||
struct observer_impl_base : observer_dummy
|
||||
{
|
||||
explicit observer_impl_base(
|
||||
[[maybe_unused]] observer_configuration&& conf,
|
||||
[[maybe_unused]] alsa_raw_observer_configuration&& apiconf)
|
||||
: observer_dummy{dummy_configuration{}, dummy_configuration{}}
|
||||
{
|
||||
}
|
||||
};
|
||||
}
|
||||
#if LIBREMIDI_HAS_UDEV
|
||||
udev_helper m_udev{};
|
||||
eventfd_notifier m_termination_event{};
|
||||
timerfd_timer m_timer_fd{};
|
||||
int m_timer_check_counts = 0;
|
||||
std::thread m_thread;
|
||||
pollfd m_fds[3]{};
|
||||
#endif
|
||||
|
||||
namespace libremidi::alsa_raw
|
||||
std::vector<alsa_raw_port_info> m_current_inputs;
|
||||
std::vector<alsa_raw_port_info> m_current_outputs;
|
||||
};
|
||||
}
|
||||
|
||||
NAMESPACE_LIBREMIDI::alsa_raw
|
||||
{
|
||||
struct observer_impl : observer_impl_base<alsa_raw::midi1_enumerator>
|
||||
{
|
||||
using alsa_raw::observer_impl_base<midi1_enumerator>::observer_impl_base;
|
||||
observer_impl(observer_configuration&& conf, alsa_raw_observer_configuration&& apiconf)
|
||||
: observer_impl_base<alsa_raw::midi1_enumerator>{std::move(conf), std::move(apiconf)}
|
||||
{
|
||||
finish_init();
|
||||
}
|
||||
|
||||
libremidi::API get_current_api() const noexcept override { return libremidi::API::ALSA_RAW; }
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user