Merge branch 'refactor-orbit-ui' into orbit-ui
This commit is contained in:
@@ -3,15 +3,17 @@
|
||||
|
||||
#include <string>
|
||||
|
||||
#if !defined(LIBREMIDI_MODULE_BUILD) || !defined(_WIN32)
|
||||
namespace winrt::Microsoft::Windows::Devices::Midi2
|
||||
{
|
||||
class MidiSession;
|
||||
class MidiEndpointDeviceInformation;
|
||||
class MidiEndpointConnection;
|
||||
struct MidiSession;
|
||||
struct MidiEndpointDeviceInformation;
|
||||
struct MidiEndpointConnection;
|
||||
}
|
||||
#endif
|
||||
|
||||
// TODO allow to share midi session and endpoints
|
||||
namespace libremidi::winmidi
|
||||
NAMESPACE_LIBREMIDI::winmidi
|
||||
{
|
||||
|
||||
struct input_configuration
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
#include <cctype>
|
||||
#include <string>
|
||||
#include <guiddef.h>
|
||||
#include <unknwn.h>
|
||||
|
||||
#include <winrt/Windows.Foundation.h>
|
||||
#include <winrt/Windows.Foundation.Collections.h>
|
||||
@@ -15,18 +16,20 @@
|
||||
#include <winrt/Microsoft.Windows.Devices.Midi2.h>
|
||||
#include <libremidi/cmidi2.hpp>
|
||||
|
||||
// MinGW support
|
||||
#if !defined(_MSC_VER)
|
||||
namespace Microsoft::Windows::Devices::Midi2::Initialization
|
||||
{
|
||||
struct IMidiClientInitializer;
|
||||
struct MidiClientInitializerUuid;
|
||||
}
|
||||
#if __has_include(<WindowsMidiServicesAppSdkComExtensions.h>)
|
||||
#include <WindowsMidiServicesAppSdkComExtensions.h>
|
||||
#define LIBREMIDI_WINMIDI_HAS_COM_EXTENSIONS 1
|
||||
|
||||
__CRT_UUID_DECL(Microsoft::Windows::Devices::Midi2::Initialization::IMidiClientInitializer,
|
||||
0x8087b303, 0xd551, 0xbce2, 0x1e, 0xad, 0xa2, 0x50, 0x0d, 0x50, 0xc5, 0x80);
|
||||
__CRT_UUID_DECL(Microsoft::Windows::Devices::Midi2::Initialization::MidiClientInitializerUuid,
|
||||
0xc3263827, 0xc3b0, 0xbdbd, 0x25, 0x00, 0xce, 0x63, 0xa3, 0xf3, 0xf2, 0xc3);
|
||||
#define LIBREMIDI_DEFINE_GUID_CONSTEXPR(type,name,l,w1,w2,b1,b2,b3,b4,b5,b6,b7,b8) \
|
||||
LIBREMIDI_STATIC constexpr const type name = {l,w1,w2,{b1,b2,b3,b4,b5,b6,b7,b8}}
|
||||
|
||||
NAMESPACE_LIBREMIDI {
|
||||
LIBREMIDI_DEFINE_GUID_CONSTEXPR(IID, IID_IMidiEndpointConnectionMessagesReceivedCallback, 0x8087b303, 0x0519, 0x31d1, 0x31, 0xd1, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10);
|
||||
LIBREMIDI_DEFINE_GUID_CONSTEXPR(IID, IID_IMidiEndpointConnectionRaw, 0x8087b303, 0x0519, 0x31d1, 0x31, 0xd1, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20);
|
||||
LIBREMIDI_DEFINE_GUID_CONSTEXPR(IID, IID_IMidiClientInitializer, 0x8087b303, 0xd551, 0xbce2, 0x1e, 0xad, 0xa2, 0x50, 0x0d, 0x50, 0xc5, 0x80);
|
||||
LIBREMIDI_DEFINE_GUID_CONSTEXPR(IID, IID_MidiClientInitializerUuid, 0xc3263827, 0xc3b0, 0xbdbd, 0x25, 0x00, 0xce, 0x63, 0xa3, 0xf3, 0xf2, 0xc3);
|
||||
LIBREMIDI_DEFINE_GUID_CONSTEXPR(IID, IID_MidiSrvTransportUuid, 0x2ba15e4e, 0x5417, 0x4a66, 0x85, 0xb8, 0x2b, 0x22, 0x60, 0xef, 0xbc, 0x84);
|
||||
}
|
||||
#endif
|
||||
|
||||
#include <init/Microsoft.Windows.Devices.Midi2.Initialization.hpp>
|
||||
@@ -35,20 +38,18 @@ __CRT_UUID_DECL(Microsoft::Windows::Devices::Midi2::Initialization::MidiClientIn
|
||||
namespace midi2 = winrt::Microsoft::Windows::Devices::Midi2;
|
||||
namespace foundation = winrt::Windows::Foundation;
|
||||
|
||||
namespace libremidi::winmidi
|
||||
NAMESPACE_LIBREMIDI::winmidi
|
||||
{
|
||||
using namespace winrt;
|
||||
using namespace winrt::Windows::Foundation;
|
||||
using namespace winrt::Windows::Devices::Enumeration;
|
||||
using namespace winrt::Windows::Storage::Streams;
|
||||
using namespace winrt::Microsoft::Windows::Devices::Midi2;
|
||||
using namespace Windows::Devices::Enumeration;
|
||||
using namespace ::Microsoft::Windows::Devices::Midi2::Initialization;
|
||||
|
||||
inline bool ichar_equals(char a, char b)
|
||||
{
|
||||
return std::tolower(static_cast<unsigned char>(a)) ==
|
||||
std::tolower(static_cast<unsigned char>(b));
|
||||
return std::tolower(static_cast<unsigned char>(a))
|
||||
== std::tolower(static_cast<unsigned char>(b));
|
||||
}
|
||||
|
||||
inline bool iequals(std::string_view lhs, std::string_view rhs)
|
||||
@@ -81,16 +82,138 @@ get_port(const std::string& device_name, int group_terminal_block)
|
||||
}
|
||||
return {nullptr, nullptr};
|
||||
}
|
||||
|
||||
// From Microsoft.Windows.Devices.Midi2.Initialization.hpp
|
||||
typedef enum
|
||||
{
|
||||
Platform_x64 = 1,
|
||||
// Platform_Arm64 = 2,
|
||||
// Platform_Arm64EC = 3,
|
||||
Platform_Arm64X = 4,
|
||||
} MidiAppSDKPlatform;
|
||||
|
||||
struct IMidiClientInitializer : ::IUnknown
|
||||
{
|
||||
// returns the SDK version info. Supply nullptr for arguments you don't care about
|
||||
STDMETHOD(GetInstalledWindowsMidiServicesSdkVersion)(
|
||||
MidiAppSDKPlatform* buildPlatform,
|
||||
USHORT* versionMajor,
|
||||
USHORT* versionMinor,
|
||||
USHORT* versionPatch,
|
||||
|
||||
LPWSTR* buildSource,
|
||||
LPWSTR* versionName,
|
||||
LPWSTR* versionFullString
|
||||
) = 0;
|
||||
|
||||
// demand-starts the service if present
|
||||
STDMETHOD(EnsureServiceAvailable)() = 0;
|
||||
};
|
||||
struct winmidi_shared_data_instance
|
||||
{
|
||||
IMidiClientInitializer* initializer{nullptr};
|
||||
bool ready{false};
|
||||
winmidi_shared_data_instance()
|
||||
{
|
||||
// 1. Check if MIDI Services are available
|
||||
{
|
||||
::IUnknown* servicePointer{ nullptr };
|
||||
auto hr = CoCreateInstance(
|
||||
libremidi::IID_MidiSrvTransportUuid,
|
||||
NULL,
|
||||
CLSCTX_INPROC_SERVER,
|
||||
IID_PPV_ARGS(&servicePointer)
|
||||
);
|
||||
|
||||
if (SUCCEEDED(hr))
|
||||
{
|
||||
if (servicePointer == nullptr)
|
||||
{
|
||||
ready = false;
|
||||
return;
|
||||
}
|
||||
|
||||
// Here is the good case:
|
||||
servicePointer->Release();
|
||||
servicePointer = nullptr;
|
||||
ready = true;
|
||||
}
|
||||
else if (hr == REGDB_E_CLASSNOTREG)
|
||||
{
|
||||
servicePointer = nullptr;
|
||||
ready = false;
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
servicePointer = nullptr;
|
||||
ready = false;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// 2. Check if MIDI services can be created
|
||||
{
|
||||
if (SUCCEEDED(CoCreateInstance(
|
||||
libremidi::IID_MidiClientInitializerUuid,
|
||||
NULL,
|
||||
CLSCTX::CLSCTX_INPROC_SERVER | CLSCTX::CLSCTX_FROM_DEFAULT_CONTEXT,
|
||||
libremidi::IID_IMidiClientInitializer,
|
||||
reinterpret_cast<void**>(&initializer)
|
||||
)))
|
||||
{
|
||||
if (initializer != nullptr)
|
||||
{
|
||||
ready = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
ready = false;
|
||||
return;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
ready = false;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// 3. Check if MIDI services can be used
|
||||
if (SUCCEEDED(initializer->EnsureServiceAvailable()))
|
||||
{
|
||||
ready = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
ready = false;
|
||||
initializer->Release();
|
||||
initializer = nullptr;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
~winmidi_shared_data_instance()
|
||||
{
|
||||
if (initializer != nullptr)
|
||||
{
|
||||
initializer->Release();
|
||||
initializer = nullptr;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
struct winmidi_shared_data
|
||||
{
|
||||
std::shared_ptr<MidiDesktopAppSdkInitializer> initializer;
|
||||
bool m_ready{false};
|
||||
std::shared_ptr<winmidi_shared_data_instance> self = libremidi::instance<winmidi_shared_data_instance>();
|
||||
|
||||
winmidi_shared_data()
|
||||
: initializer{libremidi::instance<MidiDesktopAppSdkInitializer>()}
|
||||
, m_ready{
|
||||
initializer && initializer->InitializeSdkRuntime()
|
||||
&& initializer->EnsureServiceAvailable()}
|
||||
{
|
||||
}
|
||||
|
||||
~winmidi_shared_data()
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
#include <libremidi/detail/midi_in.hpp>
|
||||
#include <libremidi/detail/midi_stream_decoder.hpp>
|
||||
|
||||
namespace libremidi::winmidi
|
||||
NAMESPACE_LIBREMIDI::winmidi
|
||||
{
|
||||
class midi_in_impl final
|
||||
: public midi2::in_api
|
||||
@@ -19,6 +19,56 @@ public:
|
||||
{
|
||||
} configuration;
|
||||
|
||||
#if LIBREMIDI_WINMIDI_HAS_COM_EXTENSIONS
|
||||
struct raw_callback_type final : IMidiEndpointConnectionMessagesReceivedCallback
|
||||
{
|
||||
explicit raw_callback_type(midi_in_impl& self)
|
||||
: self{self}
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
midi_in_impl& self;
|
||||
HRESULT STDMETHODCALLTYPE QueryInterface(REFIID riid, void** ppvObject) override
|
||||
{
|
||||
if (!ppvObject)
|
||||
return E_POINTER;
|
||||
|
||||
if (riid == __uuidof(IUnknown) ||
|
||||
riid == libremidi::IID_IMidiEndpointConnectionMessagesReceivedCallback)
|
||||
{
|
||||
*ppvObject = static_cast<IMidiEndpointConnectionMessagesReceivedCallback*>(this);
|
||||
AddRef();
|
||||
return S_OK;
|
||||
}
|
||||
|
||||
*ppvObject = nullptr;
|
||||
return E_NOINTERFACE;
|
||||
}
|
||||
|
||||
ULONG STDMETHODCALLTYPE AddRef() override
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
ULONG STDMETHODCALLTYPE Release() override
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
HRESULT STDMETHODCALLTYPE MessagesReceived(
|
||||
GUID sessionId,
|
||||
GUID connectionId,
|
||||
UINT64 timestamp,
|
||||
UINT32 wordCount,
|
||||
UINT32 *messages) override {
|
||||
HRESULT res{};
|
||||
self.process_message(sessionId, connectionId, timestamp, wordCount, messages);
|
||||
return res;
|
||||
}
|
||||
} raw_callback{*this};
|
||||
#endif
|
||||
|
||||
explicit midi_in_impl(
|
||||
libremidi::ump_input_configuration&& conf, winmidi::input_configuration&& apiconf)
|
||||
: configuration{std::move(conf), std::move(apiconf)}
|
||||
@@ -42,7 +92,7 @@ public:
|
||||
|
||||
stdx::error open_port(const input_port& port, std::string_view) override
|
||||
{
|
||||
auto device_id = std::get_if<std::string>(&port.device);
|
||||
auto device_id = get_if<std::string>(&port.device);
|
||||
if (!device_id)
|
||||
return std::errc::invalid_argument;
|
||||
|
||||
@@ -55,19 +105,28 @@ public:
|
||||
// TODO use a MidiGroupEndpointListener for the filtering
|
||||
m_endpoint = m_session.CreateEndpointConnection(ep.EndpointDeviceId());
|
||||
|
||||
#if !LIBREMIDI_WINMIDI_HAS_COM_EXTENSIONS
|
||||
m_revoke_token = m_endpoint.MessageReceived(
|
||||
[this](
|
||||
const winrt::Microsoft::Windows::Devices::Midi2::IMidiMessageReceivedEventSource&,
|
||||
const winrt::Microsoft::Windows::Devices::Midi2::MidiMessageReceivedEventArgs& args) {
|
||||
process_message(args);
|
||||
});
|
||||
#else
|
||||
m_endpoint.as(libremidi::IID_IMidiEndpointConnectionRaw, m_raw_endpoint.put_void());
|
||||
|
||||
m_raw_endpoint->SetMessagesReceivedCallback(
|
||||
&raw_callback
|
||||
);
|
||||
#endif
|
||||
|
||||
m_endpoint.Open();
|
||||
|
||||
return stdx::error{};
|
||||
}
|
||||
|
||||
void process_message(const winrt::Microsoft::Windows::Devices::Midi2::MidiMessageReceivedEventArgs& msg)
|
||||
void process_message(
|
||||
const winrt::Microsoft::Windows::Devices::Midi2::MidiMessageReceivedEventArgs& msg)
|
||||
{
|
||||
static constexpr timestamp_backend_info timestamp_info{
|
||||
.has_absolute_timestamps = true,
|
||||
@@ -95,9 +154,49 @@ public:
|
||||
{ump_space, ump_space + b.Size()}, m_processing.timestamp<timestamp_info>(to_ns, 0));
|
||||
}
|
||||
|
||||
#if LIBREMIDI_WINMIDI_HAS_COM_EXTENSIONS
|
||||
void process_message(
|
||||
const GUID& /* sessionId */,
|
||||
const GUID& /* connectionId */,
|
||||
UINT64 timestamp,
|
||||
UINT32 wordCount,
|
||||
UINT32 *ump)
|
||||
{
|
||||
static constexpr timestamp_backend_info timestamp_info{
|
||||
.has_absolute_timestamps = true,
|
||||
.absolute_is_monotonic = false,
|
||||
.has_samples = false,
|
||||
};
|
||||
|
||||
if(wordCount == 0)
|
||||
return;
|
||||
|
||||
if (m_group_filter >= 0)
|
||||
{
|
||||
int group = cmidi2_ump_get_group(ump);
|
||||
if (group != m_group_filter)
|
||||
return;
|
||||
}
|
||||
|
||||
auto to_ns = [t = timestamp] { return t; };
|
||||
m_processing.on_bytes(
|
||||
{ump, ump + wordCount}, m_processing.timestamp<timestamp_info>(to_ns, 0));
|
||||
}
|
||||
#endif
|
||||
|
||||
stdx::error close_port() override
|
||||
{
|
||||
if(!m_endpoint)
|
||||
return std::errc::not_connected;
|
||||
|
||||
#if !LIBREMIDI_WINMIDI_HAS_COM_EXTENSIONS
|
||||
m_endpoint.MessageReceived(m_revoke_token);
|
||||
#else
|
||||
if(m_raw_endpoint) {
|
||||
m_raw_endpoint->RemoveMessagesReceivedCallback();
|
||||
m_raw_endpoint = nullptr;
|
||||
}
|
||||
#endif
|
||||
m_session.DisconnectEndpointConnection(m_endpoint.ConnectionId());
|
||||
return stdx::error{};
|
||||
}
|
||||
@@ -108,6 +207,9 @@ private:
|
||||
MidiSession m_session;
|
||||
winrt::event_token m_revoke_token{};
|
||||
winrt::Microsoft::Windows::Devices::Midi2::MidiEndpointConnection m_endpoint{nullptr};
|
||||
#if LIBREMIDI_WINMIDI_HAS_COM_EXTENSIONS
|
||||
winrt::impl::com_ref<IMidiEndpointConnectionRaw> m_raw_endpoint{};
|
||||
#endif
|
||||
midi2::input_state_machine m_processing{this->configuration};
|
||||
int m_group_filter = -1;
|
||||
};
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
#pragma once
|
||||
#include <libremidi/detail/ump_stream.hpp>
|
||||
#include <libremidi/backends/winmidi/config.hpp>
|
||||
#include <libremidi/backends/winmidi/helpers.hpp>
|
||||
#include <libremidi/backends/winmidi/observer.hpp>
|
||||
#include <libremidi/detail/midi_out.hpp>
|
||||
#include <libremidi/detail/ump_stream.hpp>
|
||||
|
||||
namespace libremidi::winmidi
|
||||
NAMESPACE_LIBREMIDI::winmidi
|
||||
{
|
||||
|
||||
class midi_out_impl final
|
||||
@@ -38,7 +38,7 @@ public:
|
||||
|
||||
stdx::error open_port(const output_port& port, std::string_view) override
|
||||
{
|
||||
auto device_id = std::get_if<std::string>(&port.device);
|
||||
auto device_id = get_if<std::string>(&port.device);
|
||||
if (!device_id)
|
||||
return std::errc::invalid_argument;
|
||||
|
||||
@@ -47,6 +47,9 @@ public:
|
||||
return std::errc::address_not_available;
|
||||
|
||||
m_endpoint = m_session.CreateEndpointConnection(ep.EndpointDeviceId());
|
||||
#if LIBREMIDI_WINMIDI_HAS_COM_EXTENSIONS
|
||||
m_endpoint.as(libremidi::IID_IMidiEndpointConnectionRaw, m_raw_endpoint.put_void());
|
||||
#endif
|
||||
m_endpoint.Open();
|
||||
|
||||
return stdx::error{};
|
||||
@@ -54,6 +57,9 @@ public:
|
||||
|
||||
stdx::error close_port() override
|
||||
{
|
||||
if(!m_endpoint)
|
||||
return std::errc::not_connected;
|
||||
|
||||
m_session.DisconnectEndpointConnection(m_endpoint.ConnectionId());
|
||||
return stdx::error{};
|
||||
}
|
||||
@@ -61,8 +67,10 @@ public:
|
||||
stdx::error send_ump(const uint32_t* message, size_t size) override
|
||||
{
|
||||
auto write_func = [this](const uint32_t* ump, int64_t bytes) -> std::errc {
|
||||
|
||||
#if !LIBREMIDI_WINMIDI_HAS_COM_EXTENSIONS
|
||||
MidiSendMessageResults ret{};
|
||||
switch(bytes / 4)
|
||||
switch (bytes / 4)
|
||||
{
|
||||
case 1:
|
||||
ret = m_endpoint.SendSingleMessagePacket(MidiMessage32(0, ump[0]));
|
||||
@@ -74,23 +82,52 @@ public:
|
||||
ret = m_endpoint.SendSingleMessagePacket(MidiMessage96(0, ump[0], ump[1], ump[2]));
|
||||
break;
|
||||
case 4:
|
||||
ret = m_endpoint.SendSingleMessagePacket(MidiMessage128(0, ump[0], ump[1], ump[2], ump[3]));
|
||||
ret = m_endpoint.SendSingleMessagePacket(
|
||||
MidiMessage128(0, ump[0], ump[1], ump[2], ump[3]));
|
||||
break;
|
||||
default:
|
||||
return std::errc::bad_message;
|
||||
}
|
||||
|
||||
if(ret != MidiSendMessageResults::Succeeded)
|
||||
if (ret != MidiSendMessageResults::Succeeded)
|
||||
return std::errc::bad_message;
|
||||
#else
|
||||
HRESULT ret{};
|
||||
switch (bytes / 4)
|
||||
{
|
||||
case 1:
|
||||
assert( m_raw_endpoint->ValidateBufferHasOnlyCompleteUmps(1, (UINT32*)ump));
|
||||
ret = m_raw_endpoint->SendMidiMessagesRaw(0, 1, (UINT32*)ump);
|
||||
break;
|
||||
case 2:
|
||||
assert( m_raw_endpoint->ValidateBufferHasOnlyCompleteUmps(2, (UINT32*)ump));
|
||||
ret = m_raw_endpoint->SendMidiMessagesRaw(0, 2, (UINT32*)ump);
|
||||
break;
|
||||
case 3:
|
||||
assert( m_raw_endpoint->ValidateBufferHasOnlyCompleteUmps(3, (UINT32*)ump));
|
||||
ret = m_raw_endpoint->SendMidiMessagesRaw(0, 3, (UINT32*)ump);
|
||||
break;
|
||||
case 4:
|
||||
assert( m_raw_endpoint->ValidateBufferHasOnlyCompleteUmps(4, (UINT32*)ump));
|
||||
ret = m_raw_endpoint->SendMidiMessagesRaw(0, 4, (UINT32*)ump);
|
||||
break;
|
||||
default:
|
||||
return std::errc::bad_message;
|
||||
}
|
||||
#endif
|
||||
if(ret < 0)
|
||||
return std::errc::io_error;
|
||||
return std::errc{0};
|
||||
};
|
||||
|
||||
return segment_ump_stream(message, size, write_func, []() {});
|
||||
return segment_ump_stream(message, size, write_func, []() { });
|
||||
}
|
||||
|
||||
private:
|
||||
MidiSession m_session;
|
||||
winrt::Microsoft::Windows::Devices::Midi2::MidiEndpointConnection m_endpoint{nullptr};
|
||||
#if LIBREMIDI_WINMIDI_HAS_COM_EXTENSIONS
|
||||
winrt::impl::com_ref<IMidiEndpointConnectionRaw> m_raw_endpoint{};
|
||||
#endif
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
@@ -3,12 +3,12 @@
|
||||
#include <libremidi/backends/winmidi/helpers.hpp>
|
||||
#include <libremidi/detail/observer.hpp>
|
||||
|
||||
namespace libremidi::winmidi
|
||||
NAMESPACE_LIBREMIDI::winmidi
|
||||
{
|
||||
struct port_info
|
||||
{
|
||||
hstring id;
|
||||
hstring name;
|
||||
winrt::hstring id;
|
||||
winrt::hstring name;
|
||||
};
|
||||
|
||||
class observer_impl final
|
||||
@@ -26,28 +26,22 @@ public:
|
||||
MidiEndpointDeviceWatcher::Added_revoker m_addHandler;
|
||||
MidiEndpointDeviceWatcher::Updated_revoker m_updHandler;
|
||||
MidiEndpointDeviceWatcher::Removed_revoker m_delHandler;
|
||||
std::map<hstring, std::vector<input_port>> m_known_input_devices;
|
||||
std::map<hstring, std::vector<output_port>> m_known_output_devices;
|
||||
std::map<winrt::hstring, std::vector<input_port>> m_known_input_devices;
|
||||
std::map<winrt::hstring, std::vector<output_port>> m_known_output_devices;
|
||||
std::mutex m_devices_mtx;
|
||||
std::atomic_bool m_in_constructor{true};
|
||||
|
||||
explicit observer_impl(
|
||||
libremidi::observer_configuration&& conf, winmidi::observer_configuration&& apiconf)
|
||||
: configuration{std::move(conf), std::move(apiconf)}
|
||||
{
|
||||
struct on_delete { observer_impl& self; ~on_delete() { self.m_in_constructor = false; } } on_delete{*this};
|
||||
|
||||
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);
|
||||
|
||||
if (configuration.output_added)
|
||||
for (const auto& p : get_output_ports())
|
||||
configuration.output_added(p);
|
||||
}
|
||||
|
||||
// Note: winmidi also notifies for existing devices, so the "notify in constructor"
|
||||
// is handled in the callbacks
|
||||
if ((watcher = MidiEndpointDeviceWatcher::Create()))
|
||||
{
|
||||
namespace enumeration = winrt::Windows::Devices::Enumeration;
|
||||
@@ -84,18 +78,18 @@ public:
|
||||
return libremidi::API::WINDOWS_MIDI_SERVICES;
|
||||
}
|
||||
|
||||
static port_information::port_type code_to_type(std::string_view str) noexcept
|
||||
static transport_type code_to_type(std::string_view str) noexcept
|
||||
{
|
||||
using enum port_information::port_type;
|
||||
using enum transport_type;
|
||||
|
||||
if (str.starts_with("KS"))
|
||||
return hardware;
|
||||
if (str == "BLE")
|
||||
return port_information::port_type(hardware | bluetooth);
|
||||
return transport_type(hardware | bluetooth);
|
||||
if (str == "VPB" || str == "APP")
|
||||
return software;
|
||||
if (str == "LOOP")
|
||||
return port_information::port_type(software | loopback);
|
||||
return transport_type(software | loopback);
|
||||
if (str.starts_with("NET"))
|
||||
return network;
|
||||
return unknown;
|
||||
@@ -108,11 +102,14 @@ public:
|
||||
const auto& tinfo = p.GetTransportSuppliedInfo();
|
||||
|
||||
return {
|
||||
{.client = 0,
|
||||
{.api = libremidi::API::WINDOWS_MIDI_SERVICES,
|
||||
.client = 0,
|
||||
.container = std::bit_cast<libremidi::uuid>(p.ContainerId()),
|
||||
.device = to_string(p.EndpointDeviceId()),
|
||||
.port = gp.Number(),
|
||||
.manufacturer = to_string(tinfo.ManufacturerName),
|
||||
.product = to_string(tinfo.Name),
|
||||
.serial = to_string(tinfo.SerialNumber),
|
||||
.device_name = to_string(p.Name()),
|
||||
.port_name = to_string(gp.Name()),
|
||||
.display_name = to_string(gp.Name()) + " " + std::to_string(gp.Number()),
|
||||
@@ -125,7 +122,8 @@ public:
|
||||
|
||||
for (const auto& ep : MidiEndpointDeviceInformation::FindAll())
|
||||
{
|
||||
if(ep.Name().starts_with(L"Diagnostics")) {
|
||||
if (ep.Name().starts_with(L"Diagnostics"))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -145,7 +143,8 @@ public:
|
||||
|
||||
for (const auto& ep : MidiEndpointDeviceInformation::FindAll())
|
||||
{
|
||||
if(ep.Name().starts_with(L"Diagnostics")) {
|
||||
if (ep.Name().starts_with(L"Diagnostics"))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -167,7 +166,7 @@ public:
|
||||
}
|
||||
|
||||
void on_device_updated(
|
||||
const MidiEndpointDeviceWatcher& e,
|
||||
const MidiEndpointDeviceWatcher&,
|
||||
const MidiEndpointDeviceInformationUpdatedEventArgs& result)
|
||||
{
|
||||
// OPTIMIZEME
|
||||
@@ -199,7 +198,9 @@ public:
|
||||
std::lock_guard _{m_devices_mtx};
|
||||
m_known_input_devices[ep.EndpointDeviceId()].push_back(ip);
|
||||
}
|
||||
configuration.input_added(std::move(ip));
|
||||
|
||||
if(!m_in_constructor || configuration.notify_in_constructor)
|
||||
configuration.input_added(std::move(ip));
|
||||
}
|
||||
if (configuration.output_added)
|
||||
{
|
||||
@@ -208,7 +209,9 @@ public:
|
||||
std::lock_guard _{m_devices_mtx};
|
||||
m_known_output_devices[ep.EndpointDeviceId()].push_back(op);
|
||||
}
|
||||
configuration.output_added(std::move(op));
|
||||
|
||||
if(!m_in_constructor || configuration.notify_in_constructor)
|
||||
configuration.output_added(std::move(op));
|
||||
}
|
||||
break;
|
||||
}
|
||||
@@ -220,7 +223,9 @@ public:
|
||||
std::lock_guard _{m_devices_mtx};
|
||||
m_known_input_devices[ep.EndpointDeviceId()].push_back(ip);
|
||||
}
|
||||
configuration.input_added(std::move(ip));
|
||||
|
||||
if(!m_in_constructor || configuration.notify_in_constructor)
|
||||
configuration.input_added(std::move(ip));
|
||||
}
|
||||
break;
|
||||
case MidiGroupTerminalBlockDirection::BlockOutput:
|
||||
@@ -231,13 +236,16 @@ public:
|
||||
std::lock_guard _{m_devices_mtx};
|
||||
m_known_output_devices[ep.EndpointDeviceId()].push_back(op);
|
||||
}
|
||||
configuration.output_added(std::move(op));
|
||||
|
||||
if(!m_in_constructor || configuration.notify_in_constructor)
|
||||
configuration.output_added(std::move(op));
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
void remove_device(hstring eid)
|
||||
|
||||
void remove_device(winrt::hstring eid)
|
||||
{
|
||||
std::vector<input_port> to_remove_in;
|
||||
std::vector<output_port> to_remove_out;
|
||||
|
||||
Reference in New Issue
Block a user