diff --git a/libs/surfaces/push2/interface.cc b/libs/surfaces/push2/interface.cc index eb69807edf..abe7c7f9e5 100644 --- a/libs/surfaces/push2/interface.cc +++ b/libs/surfaces/push2/interface.cc @@ -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, }; diff --git a/libs/surfaces/push2/push2.cc b/libs/surfaces/push2/push2.cc index 2a7fc98abd..1ea1fe33b8 100644 --- a/libs/surfaces/push2/push2.cc +++ b/libs/surfaces/push2/push2.cc @@ -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 midi_inputs; + vector 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 () diff --git a/libs/surfaces/push2/push2.h b/libs/surfaces/push2/push2.h index 864753f18b..110b6370d4 100644 --- a/libs/surfaces/push2/push2.h +++ b/libs/surfaces/push2/push2.h @@ -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;