2024-10-09 02:01:23 +02:00
|
|
|
#pragma once
|
|
|
|
|
#include <libremidi/backends/winmidi/config.hpp>
|
|
|
|
|
#include <libremidi/backends/winmidi/helpers.hpp>
|
|
|
|
|
#include <libremidi/detail/observer.hpp>
|
|
|
|
|
;
|
|
|
|
|
namespace libremidi::winmidi
|
|
|
|
|
{
|
|
|
|
|
struct port_info
|
|
|
|
|
{
|
|
|
|
|
hstring id;
|
|
|
|
|
hstring name;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class observer_impl final : public observer_api
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
struct
|
|
|
|
|
: libremidi::observer_configuration
|
|
|
|
|
, winmidi::observer_configuration
|
|
|
|
|
{
|
|
|
|
|
} configuration;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
explicit observer_impl(
|
|
|
|
|
libremidi::observer_configuration&& conf, winmidi::observer_configuration&& apiconf)
|
|
|
|
|
: configuration{std::move(conf), std::move(apiconf)}
|
2024-10-28 19:24:18 +01:00
|
|
|
, session{MidiSession::CreateSession(L"libremidi session")}
|
2024-10-09 02:01:23 +02:00
|
|
|
{
|
|
|
|
|
if (!configuration.has_callbacks())
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (configuration.notify_in_constructor)
|
|
|
|
|
{
|
|
|
|
|
if (configuration.input_added)
|
|
|
|
|
for (const auto& p : get_input_ports())
|
|
|
|
|
configuration.input_added(p);
|
2024-10-28 19:24:18 +01:00
|
|
|
|
2024-10-09 02:01:23 +02:00
|
|
|
if (configuration.output_added)
|
|
|
|
|
for (const auto& p : get_output_ports())
|
|
|
|
|
configuration.output_added(p);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
evTokenOnInputAdded_
|
|
|
|
|
= internalInPortObserver_.PortAdded([this](const port_info& p) { on_input_added(p); });
|
|
|
|
|
evTokenOnInputRemoved_
|
|
|
|
|
= internalInPortObserver_.PortRemoved([this](const port_info& p) { on_input_removed(p); });
|
|
|
|
|
evTokenOnOutputAdded_
|
|
|
|
|
= internalOutPortObserver_.PortAdded([this](const port_info& p) { on_output_added(p); });
|
|
|
|
|
evTokenOnOutputRemoved_ = internalOutPortObserver_.PortRemoved(
|
|
|
|
|
[this](const port_info& p) { on_output_removed(p); });
|
|
|
|
|
*/
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
~observer_impl()
|
|
|
|
|
{
|
|
|
|
|
if (!configuration.has_callbacks())
|
|
|
|
|
return;
|
|
|
|
|
// internalInPortObserver_.PortAdded(evTokenOnInputAdded_);
|
|
|
|
|
// internalInPortObserver_.PortRemoved(evTokenOnInputRemoved_);
|
|
|
|
|
// internalOutPortObserver_.PortAdded(evTokenOnOutputAdded_);
|
|
|
|
|
// internalOutPortObserver_.PortRemoved(evTokenOnOutputRemoved_);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
libremidi::API get_current_api() const noexcept override
|
|
|
|
|
{
|
|
|
|
|
return libremidi::API::WINDOWS_MIDI_SERVICES;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <bool Input>
|
|
|
|
|
auto to_port_info(const DeviceInformation& p) const noexcept
|
|
|
|
|
-> std::conditional_t<Input, input_port, output_port>
|
|
|
|
|
{
|
|
|
|
|
return {
|
|
|
|
|
{.client = 0,
|
|
|
|
|
.port = 0,
|
|
|
|
|
.manufacturer = "",
|
|
|
|
|
.device_name = "",
|
|
|
|
|
.port_name = to_string(p.Id()),
|
|
|
|
|
.display_name = to_string(p.Name())}};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::vector<libremidi::input_port> get_input_ports() const noexcept override
|
|
|
|
|
{
|
|
|
|
|
std::vector<libremidi::input_port> ret;
|
|
|
|
|
|
|
|
|
|
auto deviceSelector = MidiEndpointConnection::GetDeviceSelector();
|
|
|
|
|
auto endpointDevices = DeviceInformation::FindAllAsync(deviceSelector).get();
|
2024-10-28 19:24:18 +01:00
|
|
|
for (const auto& ep : endpointDevices)
|
2024-10-09 02:01:23 +02:00
|
|
|
{
|
2024-10-28 19:24:18 +01:00
|
|
|
if(ep.Name().starts_with(L"Diagnostics")) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2024-10-09 02:01:23 +02:00
|
|
|
// FIXME if(has input...)
|
2024-10-28 19:24:18 +01:00
|
|
|
|
|
|
|
|
ret.emplace_back(to_port_info<true>(ep));
|
2024-10-09 02:01:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::vector<libremidi::output_port> get_output_ports() const noexcept override
|
|
|
|
|
{
|
|
|
|
|
std::vector<libremidi::output_port> ret;
|
|
|
|
|
|
|
|
|
|
auto deviceSelector = MidiEndpointConnection::GetDeviceSelector();
|
|
|
|
|
auto endpointDevices = DeviceInformation::FindAllAsync(deviceSelector).get();
|
2024-10-28 19:24:18 +01:00
|
|
|
for (const auto& ep : endpointDevices)
|
2024-10-09 02:01:23 +02:00
|
|
|
{
|
2024-10-28 19:24:18 +01:00
|
|
|
if(ep.Name().starts_with(L"Diagnostics")) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2024-10-09 02:01:23 +02:00
|
|
|
// FIXME if(has output...)
|
2024-10-28 19:24:18 +01:00
|
|
|
ret.emplace_back(to_port_info<false>(ep));
|
2024-10-09 02:01:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void on_input_added(const DeviceInformation& name)
|
|
|
|
|
{
|
|
|
|
|
if (configuration.input_added)
|
2024-10-28 19:24:18 +01:00
|
|
|
configuration.input_added(to_port_info<true>(name));
|
2024-10-09 02:01:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void on_input_removed(const DeviceInformation& name)
|
|
|
|
|
{
|
|
|
|
|
if (configuration.input_removed)
|
2024-10-28 19:24:18 +01:00
|
|
|
configuration.input_removed(to_port_info<true>(name));
|
2024-10-09 02:01:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void on_output_added(const DeviceInformation& name)
|
|
|
|
|
{
|
|
|
|
|
if (configuration.output_added)
|
2024-10-28 19:24:18 +01:00
|
|
|
configuration.output_added(to_port_info<false>(name));
|
2024-10-09 02:01:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void on_output_removed(const DeviceInformation& name)
|
|
|
|
|
{
|
|
|
|
|
if (configuration.output_removed)
|
2024-10-28 19:24:18 +01:00
|
|
|
configuration.output_removed(to_port_info<false>(name));
|
2024-10-09 02:01:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
2024-10-28 19:24:18 +01:00
|
|
|
MidiSession session;
|
2024-10-09 02:01:23 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}
|