Push2: implement MIDI port probing, auto-enable

This commit is contained in:
Robin Gareus 2023-05-03 15:46:10 +02:00
parent da9ed129e3
commit 336f51e1f0
Signed by: rgareus
GPG Key ID: A090BCE02CF57F04
3 changed files with 49 additions and 2 deletions

View File

@ -60,13 +60,21 @@ delete_push2 (ControlProtocol* cp)
}
}
static bool
probe_push2_midi_protocol ()
{
std::string i, o;
return Push2::probe (i, o);
}
static ControlProtocolDescriptor push2_descriptor = {
/* name */ "Ableton Push 2",
/* id */ "uri://ardour.org/surfaces/push2:0",
/* module */ 0,
/* available */ Push2::available,
/* probe_port */ 0,
/* match usb */ 0,
/* probe_port */ probe_push2_midi_protocol,
/* match usb */ 0, // Push2::match_usb,
/* initialize */ new_push2,
/* destroy */ delete_push2,
};

View File

@ -106,6 +106,37 @@ Push2::available ()
return rv;
}
bool
Push2::match_usb (uint16_t vendor, uint16_t device)
{
return vendor == ABLETON && device == PUSH2;
}
bool
Push2::probe (std::string& i, std::string& o)
{
vector<string> midi_inputs;
vector<string> midi_outputs;
AudioEngine::instance()->get_ports ("", DataType::MIDI, PortFlags (IsOutput|IsTerminal), midi_inputs);
AudioEngine::instance()->get_ports ("", DataType::MIDI, PortFlags (IsInput|IsTerminal), midi_outputs);
auto has_fp8 = [](string const& s) {
std::string pn = AudioEngine::instance()->get_hardware_port_name_by_name (s);
return pn.find ("Ableton Push 2 MIDI 1") != string::npos;
};
auto pi = std::find_if (midi_inputs.begin (), midi_inputs.end (), has_fp8);
auto po = std::find_if (midi_outputs.begin (), midi_outputs.end (), has_fp8);
if (pi == midi_inputs.end () || po == midi_outputs.end ()) {
return false;
}
i = *pi;
o = *po;
return true;
}
Push2::Push2 (ARDOUR::Session& s)
: MIDISurface (s, X_("Ableton Push 2"), X_("Push 2"), true)
, _handle (0)
@ -149,6 +180,12 @@ Push2::Push2 (ARDOUR::Session& s)
run_event_loop ();
port_setup ();
std::string pn_in, pn_out;
if (probe (pn_in, pn_out)) {
_async_in->connect (pn_in);
_async_out->connect (pn_out);
}
}
Push2::~Push2 ()

View File

@ -297,6 +297,8 @@ class Push2 : public MIDISurface
~Push2 ();
static bool available ();
static bool match_usb (uint16_t, uint16_t);
static bool probe (std::string&, std::string&);
std::string input_port_name () const;
std::string output_port_name () const;