Skip to content

Commit

Permalink
[core] Small bugfixes, improve object creation logic
Browse files Browse the repository at this point in the history
  • Loading branch information
jcelerier committed Sep 11, 2024
1 parent 09fc6a2 commit 93204d5
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 85 deletions.
2 changes: 1 addition & 1 deletion include/libremidi/backends/alsa_raw_ump/midi_out.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class midi_out_impl final
client_open_ = std::errc::not_connected;
}

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

stdx::error connect_port(const char* portname)
{
Expand Down
8 changes: 7 additions & 1 deletion include/libremidi/backends/alsa_seq/midi_in.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,13 @@ class midi_in_impl
snd.seq.close(this->seq);
}

libremidi::API get_current_api() const noexcept override { return libremidi::API::ALSA_SEQ; }
libremidi::API get_current_api() const noexcept override
{
if constexpr (ConfigurationImpl::midi_version == 1)
return libremidi::API::ALSA_SEQ;
else
return libremidi::API::ALSA_SEQ_UMP;
}

[[nodiscard]] int create_port(std::string_view portName)
{
Expand Down
2 changes: 1 addition & 1 deletion include/libremidi/backends/alsa_seq_ump/midi_out.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class midi_out_impl final
client_open_ = std::errc::not_connected;
}

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

[[nodiscard]] int create_port(std::string_view portName)
{
Expand Down
3 changes: 2 additions & 1 deletion include/libremidi/backends/keyboard/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ struct kbd_input_configuration
{
using scancode_callback = std::function<void(int)>;
// First argument is on key press, second on key release
std::function<void(scancode_callback, scancode_callback)> set_input_scancode_callbacks;
std::function<void(scancode_callback, scancode_callback)> set_input_scancode_callbacks
= [](scancode_callback, scancode_callback) {};

enum kbd_event
{
Expand Down
107 changes: 46 additions & 61 deletions include/libremidi/midi_in.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,21 @@ make_midi1_in(const input_configuration& base_conf)
return std::make_unique<midi_in_dummy>(input_configuration{}, dummy_configuration{});
}

static LIBREMIDI_INLINE std::unique_ptr<midi_in_api>
make_midi1_in(const input_configuration& base_conf, const std::any& api_conf, libremidi::API api)
{
if (libremidi::is_midi1(api))
{
return make_midi_in(base_conf, api_conf, midi1::available_backends);
}
else if (libremidi::is_midi2(api))
{
auto c2 = convert_midi1_to_midi2_input_configuration(base_conf);
return make_midi_in(c2, api_conf, midi2::available_backends);
}
return {};
}

static LIBREMIDI_INLINE std::unique_ptr<midi_in_api>
make_midi1_in(const input_configuration& base_conf, const std::any& api_conf)
{
Expand All @@ -128,43 +143,20 @@ make_midi1_in(const input_configuration& base_conf, const std::any& api_conf)
{
if (*api_p == libremidi::API::UNSPECIFIED)
{
if (auto backend = make_midi_in(
base_conf, midi_in_configuration_for(libremidi::midi1::default_api()),
midi1::available_backends))
return backend;

auto c2 = convert_midi1_to_midi2_input_configuration(base_conf);
if (auto backend = make_midi_in(
c2, midi_in_configuration_for(libremidi::midi2::default_api()),
midi2::available_backends))
return backend;
return make_midi1_in(base_conf);
}
else if (is_midi1(*api_p))
else
{
// all good
return make_midi_in(base_conf, midi_in_configuration_for(*api_p), midi1::available_backends);
}
else if (is_midi2(*api_p))
{
auto c2 = convert_midi1_to_midi2_input_configuration(base_conf);
return make_midi_in(c2, midi_in_configuration_for(*api_p), midi2::available_backends);
return make_midi1_in(base_conf, midi_in_configuration_for(*api_p), *api_p);
}
}
else
{
const auto api = libremidi::midi_api(api_conf);
if (libremidi::is_midi1(api))
{
return make_midi_in(base_conf, api_conf, midi1::available_backends);
}
else if (libremidi::is_midi2(api))
{
auto c2 = convert_midi1_to_midi2_input_configuration(base_conf);
return make_midi_in(c2, api_conf, midi2::available_backends);
}
if (auto api = libremidi::midi_api(api_conf); api == libremidi::API::UNSPECIFIED)
return {};
else
return make_midi1_in(base_conf, api_conf, libremidi::midi_api(api_conf));
}

return std::make_unique<midi_in_dummy>(input_configuration{}, dummy_configuration{});
}

/// MIDI 1 constructors
Expand Down Expand Up @@ -218,7 +210,23 @@ make_midi2_in(const ump_input_configuration& base_conf)
}
}

return std::make_unique<midi_in_dummy>(ump_input_configuration{}, dummy_configuration{});
return {};
}

static LIBREMIDI_INLINE std::unique_ptr<midi_in_api> make_midi2_in(
const ump_input_configuration& base_conf, const std::any& api_conf, libremidi::API api)
{
if (is_midi2(api))
{
return make_midi_in(base_conf, api_conf, midi2::available_backends);
}
else if (is_midi1(api))
{
auto c2 = convert_midi2_to_midi1_input_configuration(base_conf);
return make_midi_in(c2, api_conf, midi1::available_backends);
}

return {};
}

static LIBREMIDI_INLINE std::unique_ptr<midi_in_api>
Expand All @@ -232,43 +240,20 @@ make_midi2_in(const ump_input_configuration& base_conf, const std::any& api_conf
{
if (*api_p == libremidi::API::UNSPECIFIED)
{
if (auto backend = make_midi_in(
base_conf, midi_in_configuration_for(libremidi::midi2::default_api()),
midi2::available_backends))
return backend;

auto c2 = convert_midi2_to_midi1_input_configuration(base_conf);
if (auto backend = make_midi_in(
c2, midi_in_configuration_for(libremidi::midi1::default_api()),
midi1::available_backends))
return backend;
return make_midi2_in(base_conf);
}
else if (is_midi2(*api_p))
else
{
// all good
return make_midi_in(base_conf, midi_in_configuration_for(*api_p), midi2::available_backends);
}
else if (is_midi1(*api_p))
{
auto c2 = convert_midi2_to_midi1_input_configuration(base_conf);
return make_midi_in(c2, midi_in_configuration_for(*api_p), midi1::available_backends);
return make_midi2_in(base_conf, midi_in_configuration_for(*api_p), *api_p);
}
}
else
{
const auto api = libremidi::midi_api(api_conf);
if (libremidi::is_midi2(api))
{
return make_midi_in(base_conf, api_conf, midi2::available_backends);
}
else if (libremidi::is_midi1(api))
{
auto c2 = convert_midi2_to_midi1_input_configuration(base_conf);
return make_midi_in(c2, api_conf, midi1::available_backends);
}
if (auto api = libremidi::midi_api(api_conf); api == libremidi::API::UNSPECIFIED)
return {};
else
return make_midi2_in(base_conf, api_conf, libremidi::midi_api(api_conf));
}

return std::make_unique<midi_in_dummy>(input_configuration{}, dummy_configuration{});
}

/// MIDI 2 constructors
Expand Down
15 changes: 5 additions & 10 deletions include/libremidi/midi_out.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,7 @@ make_midi_out(const output_configuration& base_conf, const std::any& api_conf)
{
if (*api_p == libremidi::API::UNSPECIFIED)
{
if (auto backend = make_midi_out_impl(
base_conf, midi_out_configuration_for(libremidi::midi1::default_api())))
return backend;

if (auto backend = make_midi_out_impl(
base_conf, midi_out_configuration_for(libremidi::midi2::default_api())))
return backend;

return std::make_unique<midi_out_dummy>(output_configuration{}, dummy_configuration{});
return make_midi_out(base_conf);
}
else
{
Expand All @@ -85,7 +77,10 @@ make_midi_out(const output_configuration& base_conf, const std::any& api_conf)
}
else
{
return make_midi_out_impl(base_conf, api_conf);
if (auto api = libremidi::midi_api(api_conf); api == libremidi::API::UNSPECIFIED)
return make_midi_out(base_conf);
else
return make_midi_out_impl(base_conf, api_conf);
}
}

Expand Down
15 changes: 5 additions & 10 deletions include/libremidi/observer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,7 @@ LIBREMIDI_INLINE std::unique_ptr<observer_api> make_observer(auto base_conf, std
{
if (*api_p == libremidi::API::UNSPECIFIED)
{
if (auto backend = make_observer_impl(
base_conf, observer_configuration_for(libremidi::midi1::default_api())))
return backend;

if (auto backend = make_observer_impl(
base_conf, observer_configuration_for(libremidi::midi2::default_api())))
return backend;

return std::make_unique<observer_dummy>(observer_configuration{}, dummy_configuration{});
return make_observer(base_conf);
}
else
{
Expand All @@ -80,7 +72,10 @@ LIBREMIDI_INLINE std::unique_ptr<observer_api> make_observer(auto base_conf, std
}
else
{
return make_observer_impl(base_conf, api_conf);
if (auto api = libremidi::midi_api(api_conf); api == libremidi::API::UNSPECIFIED)
return {};
else
return make_observer_impl(base_conf, api_conf);
}
}

Expand Down

0 comments on commit 93204d5

Please sign in to comment.