2 Copyright (C) 2013 Paul Davis
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #include <io.h> // Microsoft's nearest equivalent to <unistd.h>
24 #include <ardourext/misc.h>
29 #include <glibmm/fileutils.h>
30 #include <glibmm/miscutils.h>
32 #include "pbd/error.h"
33 #include "pbd/strsplit.h"
35 #include "ardour/async_midi_port.h"
36 #include "ardour/audio_backend.h"
37 #include "ardour/audio_port.h"
38 #include "ardour/debug.h"
39 #include "ardour/filesystem_paths.h"
40 #include "ardour/midi_port.h"
41 #include "ardour/midiport_manager.h"
42 #include "ardour/port_manager.h"
43 #include "ardour/profile.h"
44 #include "ardour/rt_tasklist.h"
45 #include "ardour/session.h"
46 #include "ardour/types_convert.h"
50 using namespace ARDOUR;
55 PortManager::PortManager ()
57 , _port_remove_in_progress (false)
58 , _port_deletions_pending (8192) /* ick, arbitrary sizing */
59 , midi_info_dirty (true)
61 load_midi_port_info ();
65 PortManager::clear_pending_port_deletions ()
69 DEBUG_TRACE (DEBUG::Ports, string_compose ("pending port deletions: %1\n", _port_deletions_pending.read_space()));
71 while (_port_deletions_pending.read (&p, 1) == 1) {
77 PortManager::remove_all_ports ()
79 /* make sure that JACK callbacks that will be invoked as we cleanup
80 * ports know that they have nothing to do.
83 _port_remove_in_progress = true;
85 /* process lock MUST be held by caller
89 RCUWriter<Ports> writer (ports);
90 boost::shared_ptr<Ports> ps = writer.get_copy ();
94 /* clear dead wood list in RCU */
98 /* clear out pending port deletion list. we know this is safe because
99 * the auto connect thread in Session is already dead when this is
100 * done. It doesn't use shared_ptr<Port> anyway.
103 _port_deletions_pending.reset ();
105 _port_remove_in_progress = false;
110 PortManager::make_port_name_relative (const string& portname) const
116 string::size_type colon = portname.find (':');
118 if (colon == string::npos) {
122 if (portname.substr (0, colon) == _backend->my_name()) {
123 return portname.substr (colon+1);
130 PortManager::make_port_name_non_relative (const string& portname) const
134 if (portname.find_first_of (':') != string::npos) {
138 str = _backend->my_name();
146 PortManager::get_pretty_name_by_name(const std::string& portname) const
148 PortEngine::PortHandle ph = _backend->get_port_by_name (portname);
153 if (0 == _backend->get_port_property (ph, "http://jackaudio.org/metadata/pretty-name", value, type)) {
162 PortManager::port_is_mine (const string& portname) const
168 string self = _backend->my_name();
170 if (portname.find_first_of (':') != string::npos) {
171 if (portname.substr (0, self.length ()) != self) {
180 PortManager::port_is_physical (const std::string& portname) const
186 PortEngine::PortHandle ph = _backend->get_port_by_name (portname);
191 return _backend->port_is_physical (ph);
195 PortManager::filter_midi_ports (vector<string>& ports, MidiPortFlags include, MidiPortFlags exclude)
198 if (!include && !exclude) {
203 Glib::Threads::Mutex::Lock lm (midi_port_info_mutex);
205 fill_midi_port_info_locked ();
207 for (vector<string>::iterator si = ports.begin(); si != ports.end(); ) {
209 MidiPortInfo::iterator x = midi_port_info.find (*si);
211 if (x == midi_port_info.end()) {
216 MidiPortInformation& mpi (x->second);
218 if (mpi.pretty_name.empty()) {
219 /* no information !!! */
225 if ((mpi.properties & include) != include) {
226 /* properties do not include requested ones */
227 si = ports.erase (si);
233 if ((mpi.properties & exclude)) {
234 /* properties include ones to avoid */
235 si = ports.erase (si);
246 PortManager::get_physical_outputs (DataType type, std::vector<std::string>& s, MidiPortFlags include, MidiPortFlags exclude)
252 _backend->get_physical_outputs (type, s);
253 filter_midi_ports (s, include, exclude);
257 PortManager::get_physical_inputs (DataType type, std::vector<std::string>& s, MidiPortFlags include, MidiPortFlags exclude)
264 _backend->get_physical_inputs (type, s);
265 filter_midi_ports (s, include, exclude);
269 PortManager::n_physical_outputs () const
272 return ChanCount::ZERO;
275 return _backend->n_physical_outputs ();
279 PortManager::n_physical_inputs () const
282 return ChanCount::ZERO;
284 return _backend->n_physical_inputs ();
287 /** @param name Full or short name of port
288 * @return Corresponding Port or 0.
290 boost::shared_ptr<Port>
291 PortManager::get_port_by_name (const string& portname)
294 return boost::shared_ptr<Port>();
297 if (!port_is_mine (portname)) {
298 /* not an ardour port */
299 return boost::shared_ptr<Port> ();
302 boost::shared_ptr<Ports> pr = ports.reader();
303 std::string rel = make_port_name_relative (portname);
304 Ports::iterator x = pr->find (rel);
306 if (x != pr->end()) {
307 /* its possible that the port was renamed by some 3rd party and
308 * we don't know about it. check for this (the check is quick
309 * and cheap), and if so, rename the port (which will alter
310 * the port map as a side effect).
312 const std::string check = make_port_name_relative (_backend->get_port_name (x->second->port_handle()));
314 x->second->set_name (check);
319 return boost::shared_ptr<Port> ();
323 PortManager::port_renamed (const std::string& old_relative_name, const std::string& new_relative_name)
325 RCUWriter<Ports> writer (ports);
326 boost::shared_ptr<Ports> p = writer.get_copy();
327 Ports::iterator x = p->find (old_relative_name);
330 boost::shared_ptr<Port> port = x->second;
332 p->insert (make_pair (new_relative_name, port));
337 PortManager::get_ports (DataType type, PortList& pl)
339 boost::shared_ptr<Ports> plist = ports.reader();
340 for (Ports::iterator p = plist->begin(); p != plist->end(); ++p) {
341 if (p->second->type() == type) {
342 pl.push_back (p->second);
349 PortManager::get_ports (const string& port_name_pattern, DataType type, PortFlags flags, vector<string>& s)
357 return _backend->get_ports (port_name_pattern, type, flags, s);
361 PortManager::port_registration_failure (const std::string& portname)
367 string full_portname = _backend->my_name();
368 full_portname += ':';
369 full_portname += portname;
372 PortEngine::PortHandle p = _backend->get_port_by_name (full_portname);
376 reason = string_compose (_("a port with the name \"%1\" already exists: check for duplicated track/bus names"), portname);
378 reason = string_compose (_("No more ports are available. You will need to stop %1 and restart with more ports if you need this many tracks."), PROGRAM_NAME);
381 throw PortRegistrationFailure (string_compose (_("AudioEngine: cannot register port \"%1\": %2"), portname, reason).c_str());
386 void operator() (Port* p) {
387 AudioEngine::instance()->add_pending_port_deletion (p);
391 boost::shared_ptr<Port>
392 PortManager::register_port (DataType dtype, const string& portname, bool input, bool async, PortFlags flags)
394 boost::shared_ptr<Port> newport;
396 /* limit the possible flags that can be set */
398 flags = PortFlags (flags & (Hidden|Shadow|IsTerminal));
401 if (dtype == DataType::AUDIO) {
402 DEBUG_TRACE (DEBUG::Ports, string_compose ("registering AUDIO port %1, input %2\n",
404 newport.reset (new AudioPort (portname, PortFlags ((input ? IsInput : IsOutput) | flags)),
406 } else if (dtype == DataType::MIDI) {
408 DEBUG_TRACE (DEBUG::Ports, string_compose ("registering ASYNC MIDI port %1, input %2\n",
410 newport.reset (new AsyncMIDIPort (portname, PortFlags ((input ? IsInput : IsOutput) | flags)),
413 DEBUG_TRACE (DEBUG::Ports, string_compose ("registering MIDI port %1, input %2\n",
415 newport.reset (new MidiPort (portname, PortFlags ((input ? IsInput : IsOutput) | flags)),
419 throw PortRegistrationFailure (string_compose ("unable to create port '%1': %2", portname, _("(unknown type)")));
422 RCUWriter<Ports> writer (ports);
423 boost::shared_ptr<Ports> ps = writer.get_copy ();
424 ps->insert (make_pair (make_port_name_relative (portname), newport));
426 /* writer goes out of scope, forces update */
430 catch (PortRegistrationFailure& err) {
432 } catch (std::exception& e) {
433 throw PortRegistrationFailure (string_compose ("unable to create port '%1': %2", portname, e.what()).c_str());
435 throw PortRegistrationFailure (string_compose ("unable to create port '%1': %2", portname, _("(unknown error)")));
438 DEBUG_TRACE (DEBUG::Ports, string_compose ("\t%2 port registration success, ports now = %1\n", ports.reader()->size(), this));
442 boost::shared_ptr<Port>
443 PortManager::register_input_port (DataType type, const string& portname, bool async, PortFlags extra_flags)
445 return register_port (type, portname, true, async, extra_flags);
448 boost::shared_ptr<Port>
449 PortManager::register_output_port (DataType type, const string& portname, bool async, PortFlags extra_flags)
451 return register_port (type, portname, false, async, extra_flags);
455 PortManager::unregister_port (boost::shared_ptr<Port> port)
457 /* This is a little subtle. We do not call the backend's port
458 * unregistration code from here. That is left for the Port
459 * destructor. We are trying to drop references to the Port object
460 * here, so that its destructor will run and it will unregister itself.
463 /* caller must hold process lock */
466 RCUWriter<Ports> writer (ports);
467 boost::shared_ptr<Ports> ps = writer.get_copy ();
468 Ports::iterator x = ps->find (make_port_name_relative (port->name()));
470 if (x != ps->end()) {
471 DEBUG_TRACE (DEBUG::Ports, string_compose ("removing %1 from port map (uc=%2)\n", port->name(), port.use_count()));
475 /* writer goes out of scope, forces update */
484 PortManager::connected (const string& port_name)
490 PortEngine::PortHandle handle = _backend->get_port_by_name (port_name);
496 return _backend->connected (handle);
500 PortManager::physically_connected (const string& port_name)
506 PortEngine::PortHandle handle = _backend->get_port_by_name (port_name);
512 return _backend->physically_connected (handle);
516 PortManager::get_connections (const string& port_name, std::vector<std::string>& s)
523 PortEngine::PortHandle handle = _backend->get_port_by_name (port_name);
530 return _backend->get_connections (handle, s);
534 PortManager::connect (const string& source, const string& destination)
538 string s = make_port_name_non_relative (source);
539 string d = make_port_name_non_relative (destination);
541 boost::shared_ptr<Port> src = get_port_by_name (s);
542 boost::shared_ptr<Port> dst = get_port_by_name (d);
545 ret = src->connect (d);
547 ret = dst->connect (s);
549 /* neither port is known to us ...hand-off to the PortEngine
552 ret = _backend->connect (s, d);
559 /* already exists - no error, no warning */
560 } else if (ret < 0) {
561 error << string_compose(_("AudioEngine: cannot connect %1 (%2) to %3 (%4)"),
562 source, s, destination, d)
570 PortManager::disconnect (const string& source, const string& destination)
574 string s = make_port_name_non_relative (source);
575 string d = make_port_name_non_relative (destination);
577 boost::shared_ptr<Port> src = get_port_by_name (s);
578 boost::shared_ptr<Port> dst = get_port_by_name (d);
581 ret = src->disconnect (d);
583 ret = dst->disconnect (s);
585 /* neither port is known to us ...hand-off to the PortEngine
588 ret = _backend->disconnect (s, d);
597 PortManager::disconnect (boost::shared_ptr<Port> port)
599 return port->disconnect_all ();
603 PortManager::disconnect (std::string const & name)
605 PortEngine::PortHandle ph = _backend->get_port_by_name (name);
607 return _backend->disconnect_all (ph);
613 PortManager::reestablish_ports ()
617 boost::shared_ptr<Ports> p = ports.reader ();
619 DEBUG_TRACE (DEBUG::Ports, string_compose ("reestablish %1 ports\n", p->size()));
621 for (i = p->begin(); i != p->end(); ++i) {
622 if (i->second->reestablish ()) {
623 error << string_compose (_("Re-establising port %1 failed"), i->second->name()) << endmsg;
624 std::cerr << string_compose (_("Re-establising port %1 failed"), i->second->name()) << std::endl;
639 PortManager::reconnect_ports ()
641 boost::shared_ptr<Ports> p = ports.reader ();
643 if (!Profile->get_trx()) {
644 /* re-establish connections */
646 DEBUG_TRACE (DEBUG::Ports, string_compose ("reconnect %1 ports\n", p->size()));
648 for (Ports::iterator i = p->begin(); i != p->end(); ++i) {
649 i->second->reconnect ();
657 PortManager::connect_callback (const string& a, const string& b, bool conn)
659 boost::shared_ptr<Port> port_a;
660 boost::shared_ptr<Port> port_b;
662 boost::shared_ptr<Ports> pr = ports.reader ();
664 x = pr->find (make_port_name_relative (a));
665 if (x != pr->end()) {
669 x = pr->find (make_port_name_relative (b));
670 if (x != pr->end()) {
675 if (port_a && !port_b) {
676 port_a->increment_external_connections ();
677 } else if (port_b && !port_a) {
678 port_b->increment_external_connections ();
681 if (port_a && !port_b) {
682 port_a->decrement_external_connections ();
683 } else if (port_b && !port_a) {
684 port_b->decrement_external_connections ();
688 PortConnectedOrDisconnected (
696 PortManager::registration_callback ()
698 if (!_port_remove_in_progress) {
701 Glib::Threads::Mutex::Lock lm (midi_port_info_mutex);
702 midi_info_dirty = true;
705 PortRegisteredOrUnregistered (); /* EMIT SIGNAL */
710 PortManager::can_request_input_monitoring () const
716 return _backend->can_monitor_input ();
720 PortManager::request_input_monitoring (const string& name, bool yn) const
726 PortEngine::PortHandle ph = _backend->get_port_by_name (name);
729 _backend->request_input_monitoring (ph, yn);
734 PortManager::ensure_input_monitoring (const string& name, bool yn) const
740 PortEngine::PortHandle ph = _backend->get_port_by_name (name);
743 _backend->ensure_input_monitoring (ph, yn);
748 PortManager::port_name_size() const
754 return _backend->port_name_size ();
758 PortManager::my_name() const
764 return _backend->my_name();
768 PortManager::graph_order_callback ()
770 if (!_port_remove_in_progress) {
771 GraphReordered(); /* EMIT SIGNAL */
778 PortManager::cycle_start (pframes_t nframes, Session* s)
780 Port::set_global_port_buffer_offset (0);
781 Port::set_cycle_samplecnt (nframes);
783 _cycle_ports = ports.reader ();
786 * - when speed == 1.0, the resampler copies data without processing
787 * it may (or may not) be more efficient to just run all in sequence.
789 * - single sequential task for 'lightweight' tasks would make sense
790 * (run it in parallel with 'heavy' resampling.
791 * * output ports (sends_output()) only set a flag
792 * * midi-ports only scale event timestamps
794 * - a threshold parallel vs searial processing may be appropriate.
795 * amount of work (how many connected ports are there, how
796 * many resamplers need to run) vs. available CPU cores and semaphore
797 * synchronization overhead.
799 * - input ports: it would make sense to resample each input only once
800 * (rather than resample into each ardour-owned input port).
801 * A single external source-port may be connected to many ardour
802 * input-ports. Currently re-sampling is per input.
804 if (s && s->rt_tasklist () && fabs (Port::speed_ratio ()) != 1.0) {
805 RTTaskList::TaskList tl;
806 for (Ports::iterator p = _cycle_ports->begin(); p != _cycle_ports->end(); ++p) {
807 tl.push_back (boost::bind (&Port::cycle_start, p->second, nframes));
809 s->rt_tasklist()->process (tl);
811 for (Ports::iterator p = _cycle_ports->begin(); p != _cycle_ports->end(); ++p) {
812 p->second->cycle_start (nframes);
818 PortManager::cycle_end (pframes_t nframes, Session* s)
820 // see optimzation note in ::cycle_start()
821 if (s && s->rt_tasklist () && fabs (Port::speed_ratio ()) != 1.0) {
822 RTTaskList::TaskList tl;
823 for (Ports::iterator p = _cycle_ports->begin(); p != _cycle_ports->end(); ++p) {
824 tl.push_back (boost::bind (&Port::cycle_end, p->second, nframes));
826 s->rt_tasklist()->process (tl);
828 for (Ports::iterator p = _cycle_ports->begin(); p != _cycle_ports->end(); ++p) {
829 p->second->cycle_end (nframes);
833 for (Ports::iterator p = _cycle_ports->begin(); p != _cycle_ports->end(); ++p) {
834 p->second->flush_buffers (nframes);
837 _cycle_ports.reset ();
843 PortManager::silence (pframes_t nframes, Session *s)
845 for (Ports::iterator i = _cycle_ports->begin(); i != _cycle_ports->end(); ++i) {
846 if (s && i->second == s->mtc_output_port ()) {
849 if (s && i->second == s->midi_clock_output_port ()) {
852 if (s && i->second == s->ltc_output_port ()) {
855 if (boost::dynamic_pointer_cast<AsyncMIDIPort>(i->second)) {
858 if (i->second->sends_output()) {
859 i->second->get_buffer(nframes).silence(nframes);
865 PortManager::silence_outputs (pframes_t nframes)
867 std::vector<std::string> port_names;
868 if (get_ports("", DataType::AUDIO, IsOutput, port_names)) {
869 for (std::vector<std::string>::iterator p = port_names.begin(); p != port_names.end(); ++p) {
870 if (!port_is_mine(*p)) {
873 PortEngine::PortHandle ph = _backend->get_port_by_name (*p);
877 void *buf = _backend->get_buffer(ph, nframes);
881 memset (buf, 0, sizeof(float) * nframes);
885 if (get_ports("", DataType::MIDI, IsOutput, port_names)) {
886 for (std::vector<std::string>::iterator p = port_names.begin(); p != port_names.end(); ++p) {
887 if (!port_is_mine(*p)) {
890 PortEngine::PortHandle ph = _backend->get_port_by_name (*p);
894 void *buf = _backend->get_buffer(ph, nframes);
898 _backend->midi_clear (buf);
904 PortManager::check_monitoring ()
906 for (Ports::iterator i = _cycle_ports->begin(); i != _cycle_ports->end(); ++i) {
910 if (i->second->last_monitor() != (x = i->second->monitoring_input ())) {
911 i->second->set_last_monitor (x);
912 /* XXX I think this is dangerous, due to
913 a likely mutex in the signal handlers ...
915 i->second->MonitorInputChanged (x); /* EMIT SIGNAL */
921 PortManager::cycle_end_fade_out (gain_t base_gain, gain_t gain_step, pframes_t nframes, Session* s)
923 // see optimzation note in ::cycle_start()
924 if (s && s->rt_tasklist () && fabs (Port::speed_ratio ()) != 1.0) {
925 RTTaskList::TaskList tl;
926 for (Ports::iterator p = _cycle_ports->begin(); p != _cycle_ports->end(); ++p) {
927 tl.push_back (boost::bind (&Port::cycle_end, p->second, nframes));
929 s->rt_tasklist()->process (tl);
931 for (Ports::iterator p = _cycle_ports->begin(); p != _cycle_ports->end(); ++p) {
932 p->second->cycle_end (nframes);
936 for (Ports::iterator p = _cycle_ports->begin(); p != _cycle_ports->end(); ++p) {
937 p->second->flush_buffers (nframes);
939 if (p->second->sends_output()) {
941 boost::shared_ptr<AudioPort> ap = boost::dynamic_pointer_cast<AudioPort> (p->second);
943 Sample* s = ap->engine_get_whole_audio_buffer ();
944 gain_t g = base_gain;
946 for (pframes_t n = 0; n < nframes; ++n) {
953 _cycle_ports.reset ();
958 PortManager::port_engine()
965 PortManager::port_is_control_only (std::string const& name)
967 static regex_t compiled_pattern;
968 static string pattern;
970 if (pattern.empty()) {
972 /* This is a list of regular expressions that match ports
973 * related to physical MIDI devices that we do not want to
974 * expose as normal physical ports.
977 const char * const control_only_ports[] = {
978 X_(".*Ableton Push.*"),
979 X_(".*FaderPort .*"),
980 X_(".*FaderPort8 .*"),
981 X_(".*FaderPort16 .*"),
982 X_(".*FaderPort2 .*"),
988 for (size_t n = 0; n < sizeof (control_only_ports)/sizeof (control_only_ports[0]); ++n) {
992 pattern += control_only_ports[n];
996 regcomp (&compiled_pattern, pattern.c_str(), REG_EXTENDED|REG_NOSUB);
999 return regexec (&compiled_pattern, name.c_str(), 0, 0, 0) == 0;
1002 PortManager::MidiPortInformation
1003 PortManager::midi_port_information (std::string const & name)
1005 Glib::Threads::Mutex::Lock lm (midi_port_info_mutex);
1007 fill_midi_port_info_locked ();
1009 MidiPortInfo::iterator x = midi_port_info.find (name);
1011 if (x != midi_port_info.end()) {
1015 return MidiPortInformation ();
1019 PortManager::get_known_midi_ports (vector<string>& copy)
1021 Glib::Threads::Mutex::Lock lm (midi_port_info_mutex);
1023 fill_midi_port_info_locked ();
1025 for (MidiPortInfo::const_iterator x = midi_port_info.begin(); x != midi_port_info.end(); ++x) {
1026 copy.push_back (x->first);
1031 PortManager::get_midi_selection_ports (vector<string>& copy)
1033 Glib::Threads::Mutex::Lock lm (midi_port_info_mutex);
1035 fill_midi_port_info_locked ();
1037 for (MidiPortInfo::const_iterator x = midi_port_info.begin(); x != midi_port_info.end(); ++x) {
1038 if (x->second.properties & MidiPortSelection) {
1039 copy.push_back (x->first);
1045 PortManager::set_port_pretty_name (string const & port, string const & pretty)
1048 Glib::Threads::Mutex::Lock lm (midi_port_info_mutex);
1050 fill_midi_port_info_locked ();
1052 MidiPortInfo::iterator x = midi_port_info.find (port);
1053 if (x == midi_port_info.end()) {
1056 x->second.pretty_name = pretty;
1059 /* push into back end */
1061 PortEngine::PortHandle ph = _backend->get_port_by_name (port);
1064 _backend->set_port_property (ph, "http://jackaudio.org/metadata/pretty-name", pretty, string());
1067 save_midi_port_info ();
1068 MidiPortInfoChanged (); /* EMIT SIGNAL*/
1072 PortManager::add_midi_port_flags (string const & port, MidiPortFlags flags)
1077 Glib::Threads::Mutex::Lock lm (midi_port_info_mutex);
1079 fill_midi_port_info_locked ();
1081 MidiPortInfo::iterator x = midi_port_info.find (port);
1083 if (x != midi_port_info.end()) {
1084 if ((x->second.properties & flags) != flags) { // at least one missing
1085 x->second.properties = MidiPortFlags (x->second.properties | flags);
1092 if (flags & MidiPortSelection) {
1093 MidiSelectionPortsChanged (); /* EMIT SIGNAL */
1096 if (flags != MidiPortSelection) {
1097 MidiPortInfoChanged (); /* EMIT SIGNAL */
1100 save_midi_port_info ();
1105 PortManager::remove_midi_port_flags (string const & port, MidiPortFlags flags)
1110 Glib::Threads::Mutex::Lock lm (midi_port_info_mutex);
1112 fill_midi_port_info_locked ();
1114 MidiPortInfo::iterator x = midi_port_info.find (port);
1116 if (x != midi_port_info.end()) {
1117 if (x->second.properties & flags) { // at least one is set
1118 x->second.properties = MidiPortFlags (x->second.properties & ~flags);
1125 if (flags & MidiPortSelection) {
1126 MidiSelectionPortsChanged (); /* EMIT SIGNAL */
1129 if (flags != MidiPortSelection) {
1130 MidiPortInfoChanged (); /* EMIT SIGNAL */
1133 save_midi_port_info ();
1138 PortManager::midi_port_info_file ()
1140 return Glib::build_filename (user_config_directory(), X_("midi_port_info"));
1144 PortManager::save_midi_port_info ()
1146 string path = midi_port_info_file ();
1148 XMLNode* root = new XMLNode (X_("MidiPortInfo"));
1151 Glib::Threads::Mutex::Lock lm (midi_port_info_mutex);
1153 if (midi_port_info.empty()) {
1158 for (MidiPortInfo::iterator i = midi_port_info.begin(); i != midi_port_info.end(); ++i) {
1159 XMLNode* node = new XMLNode (X_("port"));
1160 node->set_property (X_("name"), i->first);
1161 node->set_property (X_("backend"), i->second.backend);
1162 node->set_property (X_("pretty-name"), i->second.pretty_name);
1163 node->set_property (X_("input"), i->second.input);
1164 node->set_property (X_("properties"), i->second.properties);
1165 root->add_child_nocopy (*node);
1171 tree.set_root (root);
1173 if (!tree.write (path)) {
1174 error << string_compose (_("Could not save MIDI port info to %1"), path) << endmsg;
1179 PortManager::load_midi_port_info ()
1181 string path = midi_port_info_file ();
1184 if (!Glib::file_test (path, Glib::FILE_TEST_EXISTS)) {
1188 if (!tree.read (path)) {
1189 error << string_compose (_("Cannot load MIDI port info from %1"), path) << endmsg;
1193 midi_port_info.clear ();
1195 for (XMLNodeConstIterator i = tree.root()->children().begin(); i != tree.root()->children().end(); ++i) {
1200 MidiPortFlags properties;
1203 if (!(*i)->get_property (X_("name"), name) ||
1204 !(*i)->get_property (X_("backend"), backend) ||
1205 !(*i)->get_property (X_("pretty-name"), pretty) ||
1206 !(*i)->get_property (X_("input"), input) ||
1207 !(*i)->get_property (X_("properties"), properties)) {
1208 /* should only affect version changes */
1209 error << string_compose (_("MIDI port info file %1 contains invalid information - please remove it."), path) << endmsg;
1213 MidiPortInformation mpi (backend, pretty, input, properties, false);
1215 midi_port_info.insert (make_pair (name, mpi));
1220 PortManager::fill_midi_port_info ()
1223 Glib::Threads::Mutex::Lock lm (midi_port_info_mutex);
1224 fill_midi_port_info_locked ();
1229 PortManager::short_port_name_from_port_name (std::string const & full_name) const
1231 string::size_type colon = full_name.find_first_of (':');
1232 if (colon == string::npos || colon == full_name.length()) {
1235 return full_name.substr (colon+1);
1239 PortManager::fill_midi_port_info_locked ()
1241 /* MIDI info mutex MUST be held */
1243 if (!midi_info_dirty || !_backend) {
1247 std::vector<string> ports;
1249 AudioEngine::instance()->get_ports (string(), DataType::MIDI, IsOutput, ports);
1251 for (vector<string>::iterator p = ports.begin(); p != ports.end(); ++p) {
1253 if (port_is_mine (*p)) {
1257 if (midi_port_info.find (*p) == midi_port_info.end()) {
1259 MidiPortFlags flags (MidiPortFlags (0));
1261 if (port_is_control_only (*p)) {
1262 flags = MidiPortControl;
1265 MidiPortInformation mpi (_backend->name(), *p, true, flags, true);
1268 if ((*p.find (X_("Midi Through")) != string::npos || (*p).find (X_("Midi-Through")) != string::npos)) {
1269 mpi.properties = MidiPortFlags (mpi.properties | MidiPortVirtual);
1272 midi_port_info.insert (make_pair (*p, mpi));
1276 AudioEngine::instance()->get_ports (string(), DataType::MIDI, IsInput, ports);
1278 for (vector<string>::iterator p = ports.begin(); p != ports.end(); ++p) {
1280 if (port_is_mine (*p)) {
1284 if (midi_port_info.find (*p) == midi_port_info.end()) {
1286 MidiPortFlags flags (MidiPortFlags (0));
1288 if (port_is_control_only (*p)) {
1289 flags = MidiPortControl;
1292 MidiPortInformation mpi (_backend->name(), *p, false, flags, true);
1295 if ((*p.find (X_("Midi Through")) != string::npos || (*p).find (X_("Midi-Through")) != string::npos)) {
1296 mpi.properties = MidiPortFlags (mpi.properties | MidiPortVirtual);
1299 midi_port_info.insert (make_pair (*p, mpi));
1303 /* now check with backend about which ports are present and pull
1304 * pretty-name if it exists
1307 for (MidiPortInfo::iterator x = midi_port_info.begin(); x != midi_port_info.end(); ++x) {
1309 if (x->second.backend != _backend->name()) {
1310 /* this port (info) comes from a different
1311 * backend. While there's a reasonable chance that it
1312 * refers to the same physical (or virtual) endpoint, we
1313 * don't allow its use with this backend.
1315 x->second.exists = false;
1319 PortEngine::PortHandle ph = _backend->get_port_by_name (x->first);
1322 /* port info saved from some condition where this port
1323 * existed, but no longer does (i.e. device unplugged
1324 * at present). We don't remove it from midi_port_info.
1326 x->second.exists = false;
1329 x->second.exists = true;
1331 /* check with backend for pre-existing pretty name */
1333 string value = AudioEngine::instance()->get_pretty_name_by_name (x->first);
1335 if (!value.empty()) {
1336 x->second.pretty_name = value;
1341 midi_info_dirty = false;