2024-10-09 02:01:23 +02:00
|
|
|
#pragma once
|
|
|
|
|
// clang-format off
|
|
|
|
|
#define NOMINMAX 1
|
|
|
|
|
#define WIN32_LEAN_AND_MEAN 1
|
|
|
|
|
#include <libremidi/detail/midi_api.hpp>
|
2025-06-13 19:16:35 +02:00
|
|
|
#include <libremidi/detail/memory.hpp>
|
2024-10-09 02:01:23 +02:00
|
|
|
|
2024-10-28 19:24:18 +01:00
|
|
|
#include <cctype>
|
2024-10-09 02:01:23 +02:00
|
|
|
#include <string>
|
|
|
|
|
#include <guiddef.h>
|
2024-10-28 19:24:18 +01:00
|
|
|
|
2024-10-09 02:01:23 +02:00
|
|
|
#include <winrt/Windows.Foundation.h>
|
|
|
|
|
#include <winrt/Windows.Foundation.Collections.h>
|
|
|
|
|
#include <winrt/Windows.Devices.Enumeration.h>
|
2025-06-13 19:16:35 +02:00
|
|
|
#include <winrt/Microsoft.Windows.Devices.Midi2.h>
|
|
|
|
|
#include <libremidi/cmidi2.hpp>
|
2024-10-28 19:24:18 +01:00
|
|
|
|
2025-06-13 19:16:35 +02:00
|
|
|
// MinGW support
|
|
|
|
|
#if !defined(_MSC_VER)
|
|
|
|
|
namespace Microsoft::Windows::Devices::Midi2::Initialization
|
|
|
|
|
{
|
|
|
|
|
struct IMidiClientInitializer;
|
|
|
|
|
struct MidiClientInitializerUuid;
|
|
|
|
|
}
|
2024-10-28 19:24:18 +01:00
|
|
|
|
2025-06-13 19:16:35 +02:00
|
|
|
__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);
|
|
|
|
|
#endif
|
2024-10-09 02:01:23 +02:00
|
|
|
|
2025-06-13 19:16:35 +02:00
|
|
|
#include <init/Microsoft.Windows.Devices.Midi2.Initialization.hpp>
|
2024-10-09 02:01:23 +02:00
|
|
|
// clang-format on
|
|
|
|
|
|
2025-06-13 19:16:35 +02:00
|
|
|
namespace midi2 = winrt::Microsoft::Windows::Devices::Midi2;
|
|
|
|
|
namespace foundation = winrt::Windows::Foundation;
|
|
|
|
|
|
2024-10-09 02:01:23 +02:00
|
|
|
namespace libremidi::winmidi
|
|
|
|
|
{
|
|
|
|
|
using namespace winrt;
|
|
|
|
|
using namespace winrt::Windows::Foundation;
|
|
|
|
|
using namespace winrt::Windows::Devices::Enumeration;
|
|
|
|
|
using namespace winrt::Windows::Storage::Streams;
|
2025-06-13 19:16:35 +02:00
|
|
|
using namespace winrt::Microsoft::Windows::Devices::Midi2;
|
2024-10-09 02:01:23 +02:00
|
|
|
using namespace Windows::Devices::Enumeration;
|
2025-06-13 19:16:35 +02:00
|
|
|
using namespace ::Microsoft::Windows::Devices::Midi2::Initialization;
|
2024-10-28 19:24:18 +01:00
|
|
|
|
|
|
|
|
inline bool ichar_equals(char a, char b)
|
|
|
|
|
{
|
|
|
|
|
return std::tolower(static_cast<unsigned char>(a)) ==
|
|
|
|
|
std::tolower(static_cast<unsigned char>(b));
|
|
|
|
|
}
|
2025-06-13 19:16:35 +02:00
|
|
|
|
2024-10-28 19:24:18 +01:00
|
|
|
inline bool iequals(std::string_view lhs, std::string_view rhs)
|
|
|
|
|
{
|
|
|
|
|
return std::ranges::equal(lhs, rhs, ichar_equals);
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-13 19:16:35 +02:00
|
|
|
inline std::pair<
|
|
|
|
|
winrt::Microsoft::Windows::Devices::Midi2::MidiEndpointDeviceInformation,
|
|
|
|
|
winrt::Microsoft::Windows::Devices::Midi2::MidiGroupTerminalBlock>
|
|
|
|
|
get_port(const std::string& device_name, int group_terminal_block)
|
|
|
|
|
{
|
2024-10-28 19:24:18 +01:00
|
|
|
auto eps = MidiEndpointDeviceInformation::FindAll();
|
|
|
|
|
for (const auto& ep : eps)
|
|
|
|
|
{
|
2025-06-13 19:16:35 +02:00
|
|
|
auto str = to_string(ep.EndpointDeviceId());
|
2024-10-28 19:24:18 +01:00
|
|
|
if (str.empty())
|
|
|
|
|
continue;
|
|
|
|
|
|
2025-06-13 19:16:35 +02:00
|
|
|
if (iequals(str, device_name))
|
|
|
|
|
{
|
|
|
|
|
for (const auto& gp : ep.GetGroupTerminalBlocks())
|
|
|
|
|
{
|
|
|
|
|
if (gp.Number() == group_terminal_block)
|
|
|
|
|
{
|
|
|
|
|
return std::make_pair(ep, gp);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-10-28 19:24:18 +01:00
|
|
|
}
|
2025-06-13 19:16:35 +02:00
|
|
|
return {nullptr, nullptr};
|
2024-10-28 19:24:18 +01:00
|
|
|
}
|
2025-06-13 19:16:35 +02:00
|
|
|
struct winmidi_shared_data
|
|
|
|
|
{
|
|
|
|
|
std::shared_ptr<MidiDesktopAppSdkInitializer> initializer;
|
|
|
|
|
bool m_ready{false};
|
|
|
|
|
winmidi_shared_data()
|
|
|
|
|
: initializer{libremidi::instance<MidiDesktopAppSdkInitializer>()}
|
|
|
|
|
, m_ready{
|
|
|
|
|
initializer && initializer->InitializeSdkRuntime()
|
|
|
|
|
&& initializer->EnsureServiceAvailable()}
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
};
|
2024-10-09 02:01:23 +02:00
|
|
|
}
|