2 * Copyright (C) 2013-2019 Paul Davis <paul@linuxaudiosystems.com>
3 * Copyright (C) 2015-2019 Robin Gareus <robin@gareus.org>
4 * Copyright (C) 2017-2018 Ben Loftis <ben@harrisonconsoles.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 #include <io.h> // Microsoft's nearest equivalent to <unistd.h>
25 #include <ardourext/misc.h>
30 #include <glibmm/fileutils.h>
31 #include <glibmm/miscutils.h>
33 #include "pbd/error.h"
34 #include "pbd/strsplit.h"
36 #include "ardour/async_midi_port.h"
37 #include "ardour/audio_backend.h"
38 #include "ardour/audio_port.h"
39 #include "ardour/debug.h"
40 #include "ardour/filesystem_paths.h"
41 #include "ardour/midi_port.h"
42 #include "ardour/midiport_manager.h"
43 #include "ardour/port_manager.h"
44 #include "ardour/profile.h"
45 #include "ardour/rt_tasklist.h"
46 #include "ardour/session.h"
47 #include "ardour/types_convert.h"
51 using namespace ARDOUR;
56 PortManager::PortManager ()
58 , _port_remove_in_progress (false)
59 , _port_deletions_pending (8192) /* ick, arbitrary sizing */
60 , midi_info_dirty (true)
62 load_midi_port_info ();
66 PortManager::clear_pending_port_deletions ()
70 DEBUG_TRACE (DEBUG::Ports, string_compose ("pending port deletions: %1\n", _port_deletions_pending.read_space()));
72 while (_port_deletions_pending.read (&p, 1) == 1) {
78 PortManager::remove_all_ports ()
80 /* make sure that JACK callbacks that will be invoked as we cleanup
81 * ports know that they have nothing to do.
84 _port_remove_in_progress = true;
86 /* process lock MUST be held by caller
90 RCUWriter<Ports> writer (ports);
91 boost::shared_ptr<Ports> ps = writer.get_copy ();
95 /* clear dead wood list in RCU */
99 /* clear out pending port deletion list. we know this is safe because
100 * the auto connect thread in Session is already dead when this is
101 * done. It doesn't use shared_ptr<Port> anyway.
104 _port_deletions_pending.reset ();
106 _port_remove_in_progress = false;
111 PortManager::make_port_name_relative (const string& portname) const
117 string::size_type colon = portname.find (':');
119 if (colon == string::npos) {
123 if (portname.substr (0, colon) == _backend->my_name()) {
124 return portname.substr (colon+1);
131 PortManager::make_port_name_non_relative (const string& portname) const
135 if (portname.find_first_of (':') != string::npos) {
139 str = _backend->my_name();
147 PortManager::get_pretty_name_by_name(const std::string& portname) const
149 PortEngine::PortHandle ph = _backend->get_port_by_name (portname);
154 if (0 == _backend->get_port_property (ph, "http://jackaudio.org/metadata/pretty-name", value, type)) {
163 PortManager::port_is_mine (const string& portname) const
169 string self = _backend->my_name();
171 if (portname.find_first_of (':') != string::npos) {
172 if (portname.substr (0, self.length ()) != self) {
181 PortManager::port_is_physical (const std::string& portname) const
187 PortEngine::PortHandle ph = _backend->get_port_by_name (portname);
192 return _backend->port_is_physical (ph);
196 PortManager::filter_midi_ports (vector<string>& ports, MidiPortFlags include, MidiPortFlags exclude)
199 if (!include && !exclude) {
204 Glib::Threads::Mutex::Lock lm (midi_port_info_mutex);
206 fill_midi_port_info_locked ();
208 for (vector<string>::iterator si = ports.begin(); si != ports.end(); ) {
210 MidiPortInfo::iterator x = midi_port_info.find (*si);
212 if (x == midi_port_info.end()) {
217 MidiPortInformation& mpi (x->second);
219 if (mpi.pretty_name.empty()) {
220 /* no information !!! */
226 if ((mpi.properties & include) != include) {
227 /* properties do not include requested ones */
228 si = ports.erase (si);
234 if ((mpi.properties & exclude)) {
235 /* properties include ones to avoid */
236 si = ports.erase (si);
247 PortManager::get_physical_outputs (DataType type, std::vector<std::string>& s, MidiPortFlags include, MidiPortFlags exclude)
253 _backend->get_physical_outputs (type, s);
254 filter_midi_ports (s, include, exclude);
258 PortManager::get_physical_inputs (DataType type, std::vector<std::string>& s, MidiPortFlags include, MidiPortFlags exclude)
265 _backend->get_physical_inputs (type, s);
266 filter_midi_ports (s, include, exclude);
270 PortManager::n_physical_outputs () const
273 return ChanCount::ZERO;
276 return _backend->n_physical_outputs ();
280 PortManager::n_physical_inputs () const
283 return ChanCount::ZERO;
285 return _backend->n_physical_inputs ();
288 /** @param name Full or short name of port
289 * @return Corresponding Port or 0.
291 boost::shared_ptr<Port>
292 PortManager::get_port_by_name (const string& portname)
295 return boost::shared_ptr<Port>();
298 if (!port_is_mine (portname)) {
299 /* not an ardour port */
300 return boost::shared_ptr<Port> ();
303 boost::shared_ptr<Ports> pr = ports.reader();
304 std::string rel = make_port_name_relative (portname);
305 Ports::iterator x = pr->find (rel);
307 if (x != pr->end()) {
308 /* its possible that the port was renamed by some 3rd party and
309 * we don't know about it. check for this (the check is quick
310 * and cheap), and if so, rename the port (which will alter
311 * the port map as a side effect).
313 const std::string check = make_port_name_relative (_backend->get_port_name (x->second->port_handle()));
315 x->second->set_name (check);
320 return boost::shared_ptr<Port> ();
324 PortManager::port_renamed (const std::string& old_relative_name, const std::string& new_relative_name)
326 RCUWriter<Ports> writer (ports);
327 boost::shared_ptr<Ports> p = writer.get_copy();
328 Ports::iterator x = p->find (old_relative_name);
331 boost::shared_ptr<Port> port = x->second;
333 p->insert (make_pair (new_relative_name, port));
338 PortManager::get_ports (DataType type, PortList& pl)
340 boost::shared_ptr<Ports> plist = ports.reader();
341 for (Ports::iterator p = plist->begin(); p != plist->end(); ++p) {
342 if (p->second->type() == type) {
343 pl.push_back (p->second);
350 PortManager::get_ports (const string& port_name_pattern, DataType type, PortFlags flags, vector<string>& s)
358 return _backend->get_ports (port_name_pattern, type, flags, s);
362 PortManager::port_registration_failure (const std::string& portname)
368 string full_portname = _backend->my_name();
369 full_portname += ':';
370 full_portname += portname;
373 PortEngine::PortHandle p = _backend->get_port_by_name (full_portname);
377 reason = string_compose (_("a port with the name \"%1\" already exists: check for duplicated track/bus names"), portname);
379 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);
382 throw PortRegistrationFailure (string_compose (_("AudioEngine: cannot register port \"%1\": %2"), portname, reason).c_str());
387 void operator() (Port* p) {
388 AudioEngine::instance()->add_pending_port_deletion (p);
392 boost::shared_ptr<Port>
393 PortManager::register_port (DataType dtype, const string& portname, bool input, bool async, PortFlags flags)
395 boost::shared_ptr<Port> newport;
397 /* limit the possible flags that can be set */
399 flags = PortFlags (flags & (Hidden|Shadow|IsTerminal|TransportMasterPort));
402 if (dtype == DataType::AUDIO) {
403 DEBUG_TRACE (DEBUG::Ports, string_compose ("registering AUDIO port %1, input %2\n",
405 newport.reset (new AudioPort (portname, PortFlags ((input ? IsInput : IsOutput) | flags)),
407 } else if (dtype == DataType::MIDI) {
409 DEBUG_TRACE (DEBUG::Ports, string_compose ("registering ASYNC MIDI port %1, input %2\n",
411 newport.reset (new AsyncMIDIPort (portname, PortFlags ((input ? IsInput : IsOutput) | flags)),
414 DEBUG_TRACE (DEBUG::Ports, string_compose ("registering MIDI port %1, input %2\n",
416 newport.reset (new MidiPort (portname, PortFlags ((input ? IsInput : IsOutput) | flags)),
420 throw PortRegistrationFailure (string_compose ("unable to create port '%1': %2", portname, _("(unknown type)")));
423 newport->set_buffer_size (AudioEngine::instance()->samples_per_cycle());
425 RCUWriter<Ports> writer (ports);
426 boost::shared_ptr<Ports> ps = writer.get_copy ();
427 ps->insert (make_pair (make_port_name_relative (portname), newport));
429 /* writer goes out of scope, forces update */
432 catch (PortRegistrationFailure& err) {
434 } catch (std::exception& e) {
435 throw PortRegistrationFailure (string_compose ("unable to create port '%1': %2", portname, e.what()).c_str());
437 throw PortRegistrationFailure (string_compose ("unable to create port '%1': %2", portname, _("(unknown error)")));
440 DEBUG_TRACE (DEBUG::Ports, string_compose ("\t%2 port registration success, ports now = %1\n", ports.reader()->size(), this));
444 boost::shared_ptr<Port>
445 PortManager::register_input_port (DataType type, const string& portname, bool async, PortFlags extra_flags)
447 return register_port (type, portname, true, async, extra_flags);
450 boost::shared_ptr<Port>
451 PortManager::register_output_port (DataType type, const string& portname, bool async, PortFlags extra_flags)
453 return register_port (type, portname, false, async, extra_flags);
457 PortManager::unregister_port (boost::shared_ptr<Port> port)
459 /* This is a little subtle. We do not call the backend's port
460 * unregistration code from here. That is left for the Port
461 * destructor. We are trying to drop references to the Port object
462 * here, so that its destructor will run and it will unregister itself.
465 /* caller must hold process lock */
468 RCUWriter<Ports> writer (ports);
469 boost::shared_ptr<Ports> ps = writer.get_copy ();
470 Ports::iterator x = ps->find (make_port_name_relative (port->name()));
472 if (x != ps->end()) {
473 DEBUG_TRACE (DEBUG::Ports, string_compose ("removing %1 from port map (uc=%2)\n", port->name(), port.use_count()));
477 /* writer goes out of scope, forces update */
486 PortManager::connected (const string& port_name)
492 PortEngine::PortHandle handle = _backend->get_port_by_name (port_name);
498 return _backend->connected (handle);
502 PortManager::physically_connected (const string& port_name)
508 PortEngine::PortHandle handle = _backend->get_port_by_name (port_name);
514 return _backend->physically_connected (handle);
518 PortManager::get_connections (const string& port_name, std::vector<std::string>& s)
525 PortEngine::PortHandle handle = _backend->get_port_by_name (port_name);
532 return _backend->get_connections (handle, s);
536 PortManager::connect (const string& source, const string& destination)
540 string s = make_port_name_non_relative (source);
541 string d = make_port_name_non_relative (destination);
543 boost::shared_ptr<Port> src = get_port_by_name (s);
544 boost::shared_ptr<Port> dst = get_port_by_name (d);
547 ret = src->connect (d);
549 ret = dst->connect (s);
551 /* neither port is known to us ...hand-off to the PortEngine
554 ret = _backend->connect (s, d);
561 /* already exists - no error, no warning */
562 } else if (ret < 0) {
563 error << string_compose(_("AudioEngine: cannot connect %1 (%2) to %3 (%4)"),
564 source, s, destination, d)
572 PortManager::disconnect (const string& source, const string& destination)
576 string s = make_port_name_non_relative (source);
577 string d = make_port_name_non_relative (destination);
579 boost::shared_ptr<Port> src = get_port_by_name (s);
580 boost::shared_ptr<Port> dst = get_port_by_name (d);
583 ret = src->disconnect (d);
585 ret = dst->disconnect (s);
587 /* neither port is known to us ...hand-off to the PortEngine
590 ret = _backend->disconnect (s, d);
599 PortManager::disconnect (boost::shared_ptr<Port> port)
601 return port->disconnect_all ();
605 PortManager::disconnect (std::string const & name)
607 PortEngine::PortHandle ph = _backend->get_port_by_name (name);
609 return _backend->disconnect_all (ph);
615 PortManager::reestablish_ports ()
619 boost::shared_ptr<Ports> p = ports.reader ();
621 DEBUG_TRACE (DEBUG::Ports, string_compose ("reestablish %1 ports\n", p->size()));
623 for (i = p->begin(); i != p->end(); ++i) {
624 if (i->second->reestablish ()) {
625 error << string_compose (_("Re-establising port %1 failed"), i->second->name()) << endmsg;
626 std::cerr << string_compose (_("Re-establising port %1 failed"), i->second->name()) << std::endl;
641 PortManager::reconnect_ports ()
643 boost::shared_ptr<Ports> p = ports.reader ();
645 /* re-establish connections */
647 DEBUG_TRACE (DEBUG::Ports, string_compose ("reconnect %1 ports\n", p->size()));
649 for (Ports::iterator i = p->begin(); i != p->end(); ++i) {
650 i->second->reconnect ();
657 PortManager::connect_callback (const string& a, const string& b, bool conn)
659 DEBUG_TRACE (DEBUG::BackendCallbacks, string_compose (X_("connect callback %1 + %2 connected ? %3\n"), a, b, conn));
661 boost::shared_ptr<Port> port_a;
662 boost::shared_ptr<Port> port_b;
664 boost::shared_ptr<Ports> pr = ports.reader ();
666 x = pr->find (make_port_name_relative (a));
667 if (x != pr->end()) {
671 x = pr->find (make_port_name_relative (b));
672 if (x != pr->end()) {
677 if (port_a && !port_b) {
678 port_a->increment_external_connections ();
679 } else if (port_b && !port_a) {
680 port_b->increment_external_connections ();
683 if (port_a && !port_b) {
684 port_a->decrement_external_connections ();
685 } else if (port_b && !port_a) {
686 port_b->decrement_external_connections ();
690 PortConnectedOrDisconnected (
698 PortManager::registration_callback ()
700 DEBUG_TRACE (DEBUG::BackendCallbacks, "port registration callback\n");
702 if (!_port_remove_in_progress) {
705 Glib::Threads::Mutex::Lock lm (midi_port_info_mutex);
706 midi_info_dirty = true;
709 PortRegisteredOrUnregistered (); /* EMIT SIGNAL */
714 PortManager::can_request_input_monitoring () const
720 return _backend->can_monitor_input ();
724 PortManager::request_input_monitoring (const string& name, bool yn) const
730 PortEngine::PortHandle ph = _backend->get_port_by_name (name);
733 _backend->request_input_monitoring (ph, yn);
738 PortManager::ensure_input_monitoring (const string& name, bool yn) const
744 PortEngine::PortHandle ph = _backend->get_port_by_name (name);
747 _backend->ensure_input_monitoring (ph, yn);
752 PortManager::port_name_size() const
758 return _backend->port_name_size ();
762 PortManager::my_name() const
768 return _backend->my_name();
772 PortManager::graph_order_callback ()
774 DEBUG_TRACE (DEBUG::BackendCallbacks, "graph order callback\n");
776 if (!_port_remove_in_progress) {
777 GraphReordered(); /* EMIT SIGNAL */
784 PortManager::cycle_start (pframes_t nframes, Session* s)
786 Port::set_global_port_buffer_offset (0);
787 Port::set_cycle_samplecnt (nframes);
789 _cycle_ports = ports.reader ();
792 * - when speed == 1.0, the resampler copies data without processing
793 * it may (or may not) be more efficient to just run all in sequence.
795 * - single sequential task for 'lightweight' tasks would make sense
796 * (run it in parallel with 'heavy' resampling.
797 * * output ports (sends_output()) only set a flag
798 * * midi-ports only scale event timestamps
800 * - a threshold parallel vs searial processing may be appropriate.
801 * amount of work (how many connected ports are there, how
802 * many resamplers need to run) vs. available CPU cores and semaphore
803 * synchronization overhead.
805 * - input ports: it would make sense to resample each input only once
806 * (rather than resample into each ardour-owned input port).
807 * A single external source-port may be connected to many ardour
808 * input-ports. Currently re-sampling is per input.
810 if (s && s->rt_tasklist () && fabs (Port::speed_ratio ()) != 1.0) {
811 RTTaskList::TaskList tl;
812 for (Ports::iterator p = _cycle_ports->begin(); p != _cycle_ports->end(); ++p) {
813 if (!(p->second->flags() & TransportMasterPort)) {
814 tl.push_back (boost::bind (&Port::cycle_start, p->second, nframes));
817 s->rt_tasklist()->process (tl);
819 for (Ports::iterator p = _cycle_ports->begin(); p != _cycle_ports->end(); ++p) {
820 if (!(p->second->flags() & TransportMasterPort)) {
821 p->second->cycle_start (nframes);
828 PortManager::cycle_end (pframes_t nframes, Session* s)
830 // see optimzation note in ::cycle_start()
831 if (0 && s && s->rt_tasklist () && fabs (Port::speed_ratio ()) != 1.0) {
832 RTTaskList::TaskList tl;
833 for (Ports::iterator p = _cycle_ports->begin(); p != _cycle_ports->end(); ++p) {
834 if (!(p->second->flags() & TransportMasterPort)) {
835 tl.push_back (boost::bind (&Port::cycle_end, p->second, nframes));
838 s->rt_tasklist()->process (tl);
840 for (Ports::iterator p = _cycle_ports->begin(); p != _cycle_ports->end(); ++p) {
841 if (!(p->second->flags() & TransportMasterPort)) {
842 p->second->cycle_end (nframes);
847 for (Ports::iterator p = _cycle_ports->begin(); p != _cycle_ports->end(); ++p) {
848 /* AudioEngine::split_cycle flushes buffers until Port::port_offset.
849 * Now only flush remaining events (after Port::port_offset) */
850 p->second->flush_buffers (nframes - Port::port_offset ());
853 _cycle_ports.reset ();
859 PortManager::silence (pframes_t nframes, Session *s)
861 for (Ports::iterator i = _cycle_ports->begin(); i != _cycle_ports->end(); ++i) {
862 if (s && i->second == s->mtc_output_port ()) {
865 if (s && i->second == s->midi_clock_output_port ()) {
868 if (s && i->second == s->ltc_output_port ()) {
871 if (boost::dynamic_pointer_cast<AsyncMIDIPort>(i->second)) {
874 if (i->second->sends_output()) {
875 i->second->get_buffer(nframes).silence(nframes);
881 PortManager::silence_outputs (pframes_t nframes)
883 std::vector<std::string> port_names;
884 if (get_ports("", DataType::AUDIO, IsOutput, port_names)) {
885 for (std::vector<std::string>::iterator p = port_names.begin(); p != port_names.end(); ++p) {
886 if (!port_is_mine(*p)) {
889 PortEngine::PortHandle ph = _backend->get_port_by_name (*p);
893 void *buf = _backend->get_buffer(ph, nframes);
897 memset (buf, 0, sizeof(float) * nframes);
901 if (get_ports("", DataType::MIDI, IsOutput, port_names)) {
902 for (std::vector<std::string>::iterator p = port_names.begin(); p != port_names.end(); ++p) {
903 if (!port_is_mine(*p)) {
906 PortEngine::PortHandle ph = _backend->get_port_by_name (*p);
910 void *buf = _backend->get_buffer(ph, nframes);
914 _backend->midi_clear (buf);
920 PortManager::check_monitoring ()
922 for (Ports::iterator i = _cycle_ports->begin(); i != _cycle_ports->end(); ++i) {
926 if (i->second->last_monitor() != (x = i->second->monitoring_input ())) {
927 i->second->set_last_monitor (x);
928 /* XXX I think this is dangerous, due to
929 a likely mutex in the signal handlers ...
931 i->second->MonitorInputChanged (x); /* EMIT SIGNAL */
937 PortManager::cycle_end_fade_out (gain_t base_gain, gain_t gain_step, pframes_t nframes, Session* s)
939 // see optimzation note in ::cycle_start()
940 if (0 && s && s->rt_tasklist () && fabs (Port::speed_ratio ()) != 1.0) {
941 RTTaskList::TaskList tl;
942 for (Ports::iterator p = _cycle_ports->begin(); p != _cycle_ports->end(); ++p) {
943 if (!(p->second->flags() & TransportMasterPort)) {
944 tl.push_back (boost::bind (&Port::cycle_end, p->second, nframes));
947 s->rt_tasklist()->process (tl);
949 for (Ports::iterator p = _cycle_ports->begin(); p != _cycle_ports->end(); ++p) {
950 if (!(p->second->flags() & TransportMasterPort)) {
951 p->second->cycle_end (nframes);
956 for (Ports::iterator p = _cycle_ports->begin(); p != _cycle_ports->end(); ++p) {
957 p->second->flush_buffers (nframes);
959 if (p->second->sends_output()) {
961 boost::shared_ptr<AudioPort> ap = boost::dynamic_pointer_cast<AudioPort> (p->second);
963 Sample* s = ap->engine_get_whole_audio_buffer ();
964 gain_t g = base_gain;
966 for (pframes_t n = 0; n < nframes; ++n) {
973 _cycle_ports.reset ();
978 PortManager::port_engine()
985 PortManager::port_is_control_only (std::string const& name)
987 static regex_t compiled_pattern;
988 static string pattern;
990 if (pattern.empty()) {
992 /* This is a list of regular expressions that match ports
993 * related to physical MIDI devices that we do not want to
994 * expose as normal physical ports.
997 const char * const control_only_ports[] = {
998 X_(".*Ableton Push.*"),
999 X_(".*FaderPort .*"),
1000 X_(".*FaderPort8 .*"),
1001 X_(".*FaderPort16 .*"),
1002 X_(".*FaderPort2 .*"),
1008 for (size_t n = 0; n < sizeof (control_only_ports)/sizeof (control_only_ports[0]); ++n) {
1012 pattern += control_only_ports[n];
1016 regcomp (&compiled_pattern, pattern.c_str(), REG_EXTENDED|REG_NOSUB);
1019 return regexec (&compiled_pattern, name.c_str(), 0, 0, 0) == 0;
1022 PortManager::MidiPortInformation
1023 PortManager::midi_port_information (std::string const & name)
1025 Glib::Threads::Mutex::Lock lm (midi_port_info_mutex);
1027 fill_midi_port_info_locked ();
1029 MidiPortInfo::iterator x = midi_port_info.find (name);
1031 if (x != midi_port_info.end()) {
1035 return MidiPortInformation ();
1039 PortManager::get_known_midi_ports (vector<string>& copy)
1041 Glib::Threads::Mutex::Lock lm (midi_port_info_mutex);
1043 fill_midi_port_info_locked ();
1045 for (MidiPortInfo::const_iterator x = midi_port_info.begin(); x != midi_port_info.end(); ++x) {
1046 copy.push_back (x->first);
1051 PortManager::get_midi_selection_ports (vector<string>& copy)
1053 Glib::Threads::Mutex::Lock lm (midi_port_info_mutex);
1055 fill_midi_port_info_locked ();
1057 for (MidiPortInfo::const_iterator x = midi_port_info.begin(); x != midi_port_info.end(); ++x) {
1058 if (x->second.properties & MidiPortSelection) {
1059 copy.push_back (x->first);
1065 PortManager::set_port_pretty_name (string const & port, string const & pretty)
1068 Glib::Threads::Mutex::Lock lm (midi_port_info_mutex);
1070 fill_midi_port_info_locked ();
1072 MidiPortInfo::iterator x = midi_port_info.find (port);
1073 if (x == midi_port_info.end()) {
1076 x->second.pretty_name = pretty;
1079 /* push into back end */
1081 PortEngine::PortHandle ph = _backend->get_port_by_name (port);
1084 _backend->set_port_property (ph, "http://jackaudio.org/metadata/pretty-name", pretty, string());
1087 save_midi_port_info ();
1088 MidiPortInfoChanged (); /* EMIT SIGNAL*/
1092 PortManager::add_midi_port_flags (string const & port, MidiPortFlags flags)
1097 Glib::Threads::Mutex::Lock lm (midi_port_info_mutex);
1099 fill_midi_port_info_locked ();
1101 MidiPortInfo::iterator x = midi_port_info.find (port);
1103 if (x != midi_port_info.end()) {
1104 if ((x->second.properties & flags) != flags) { // at least one missing
1105 x->second.properties = MidiPortFlags (x->second.properties | flags);
1112 if (flags & MidiPortSelection) {
1113 MidiSelectionPortsChanged (); /* EMIT SIGNAL */
1116 if (flags != MidiPortSelection) {
1117 MidiPortInfoChanged (); /* EMIT SIGNAL */
1120 save_midi_port_info ();
1125 PortManager::remove_midi_port_flags (string const & port, MidiPortFlags flags)
1130 Glib::Threads::Mutex::Lock lm (midi_port_info_mutex);
1132 fill_midi_port_info_locked ();
1134 MidiPortInfo::iterator x = midi_port_info.find (port);
1136 if (x != midi_port_info.end()) {
1137 if (x->second.properties & flags) { // at least one is set
1138 x->second.properties = MidiPortFlags (x->second.properties & ~flags);
1145 if (flags & MidiPortSelection) {
1146 MidiSelectionPortsChanged (); /* EMIT SIGNAL */
1149 if (flags != MidiPortSelection) {
1150 MidiPortInfoChanged (); /* EMIT SIGNAL */
1153 save_midi_port_info ();
1158 PortManager::midi_port_info_file ()
1160 return Glib::build_filename (user_config_directory(), X_("midi_port_info"));
1164 PortManager::save_midi_port_info ()
1166 string path = midi_port_info_file ();
1168 XMLNode* root = new XMLNode (X_("MidiPortInfo"));
1171 Glib::Threads::Mutex::Lock lm (midi_port_info_mutex);
1173 if (midi_port_info.empty()) {
1178 for (MidiPortInfo::iterator i = midi_port_info.begin(); i != midi_port_info.end(); ++i) {
1179 XMLNode* node = new XMLNode (X_("port"));
1180 node->set_property (X_("name"), i->first);
1181 node->set_property (X_("backend"), i->second.backend);
1182 node->set_property (X_("pretty-name"), i->second.pretty_name);
1183 node->set_property (X_("input"), i->second.input);
1184 node->set_property (X_("properties"), i->second.properties);
1185 root->add_child_nocopy (*node);
1191 tree.set_root (root);
1193 if (!tree.write (path)) {
1194 error << string_compose (_("Could not save MIDI port info to %1"), path) << endmsg;
1199 PortManager::load_midi_port_info ()
1201 string path = midi_port_info_file ();
1204 if (!Glib::file_test (path, Glib::FILE_TEST_EXISTS)) {
1208 if (!tree.read (path)) {
1209 error << string_compose (_("Cannot load MIDI port info from %1"), path) << endmsg;
1213 midi_port_info.clear ();
1215 for (XMLNodeConstIterator i = tree.root()->children().begin(); i != tree.root()->children().end(); ++i) {
1220 MidiPortFlags properties;
1223 if (!(*i)->get_property (X_("name"), name) ||
1224 !(*i)->get_property (X_("backend"), backend) ||
1225 !(*i)->get_property (X_("pretty-name"), pretty) ||
1226 !(*i)->get_property (X_("input"), input) ||
1227 !(*i)->get_property (X_("properties"), properties)) {
1228 /* should only affect version changes */
1229 error << string_compose (_("MIDI port info file %1 contains invalid information - please remove it."), path) << endmsg;
1233 MidiPortInformation mpi (backend, pretty, input, properties, false);
1235 midi_port_info.insert (make_pair (name, mpi));
1240 PortManager::fill_midi_port_info ()
1243 Glib::Threads::Mutex::Lock lm (midi_port_info_mutex);
1244 fill_midi_port_info_locked ();
1249 PortManager::short_port_name_from_port_name (std::string const & full_name) const
1251 string::size_type colon = full_name.find_first_of (':');
1252 if (colon == string::npos || colon == full_name.length()) {
1255 return full_name.substr (colon+1);
1259 PortManager::fill_midi_port_info_locked ()
1261 /* MIDI info mutex MUST be held */
1263 if (!midi_info_dirty || !_backend) {
1267 std::vector<string> ports;
1269 AudioEngine::instance()->get_ports (string(), DataType::MIDI, IsOutput, ports);
1271 for (vector<string>::iterator p = ports.begin(); p != ports.end(); ++p) {
1273 /* ugly hack, ideally we'd use a port-flag, or at vkbd_output_port()->name() */
1274 if (port_is_mine (*p) && *p != _backend->my_name() + ":" + _("Virtual Keyboard")) {
1278 if (midi_port_info.find (*p) == midi_port_info.end()) {
1280 MidiPortFlags flags (MidiPortFlags (0));
1282 if (port_is_control_only (*p)) {
1283 flags = MidiPortControl;
1284 } else if (*p == _backend->my_name() + ":" + _("Virtual Keyboard")) {
1285 flags = MidiPortFlags(MidiPortSelection | MidiPortMusic);
1288 MidiPortInformation mpi (_backend->name(), *p, true, flags, true);
1291 if ((*p.find (X_("Midi Through")) != string::npos || (*p).find (X_("Midi-Through")) != string::npos)) {
1292 mpi.properties = MidiPortFlags (mpi.properties | MidiPortVirtual);
1295 midi_port_info.insert (make_pair (*p, mpi));
1299 AudioEngine::instance()->get_ports (string(), DataType::MIDI, IsInput, ports);
1301 for (vector<string>::iterator p = ports.begin(); p != ports.end(); ++p) {
1303 if (port_is_mine (*p)) {
1307 if (midi_port_info.find (*p) == midi_port_info.end()) {
1309 MidiPortFlags flags (MidiPortFlags (0));
1311 if (port_is_control_only (*p)) {
1312 flags = MidiPortControl;
1315 MidiPortInformation mpi (_backend->name(), *p, false, flags, true);
1318 if ((*p.find (X_("Midi Through")) != string::npos || (*p).find (X_("Midi-Through")) != string::npos)) {
1319 mpi.properties = MidiPortFlags (mpi.properties | MidiPortVirtual);
1322 midi_port_info.insert (make_pair (*p, mpi));
1326 /* now check with backend about which ports are present and pull
1327 * pretty-name if it exists
1330 for (MidiPortInfo::iterator x = midi_port_info.begin(); x != midi_port_info.end(); ++x) {
1332 if (x->second.backend != _backend->name()) {
1333 /* this port (info) comes from a different
1334 * backend. While there's a reasonable chance that it
1335 * refers to the same physical (or virtual) endpoint, we
1336 * don't allow its use with this backend.
1338 x->second.exists = false;
1342 PortEngine::PortHandle ph = _backend->get_port_by_name (x->first);
1345 /* port info saved from some condition where this port
1346 * existed, but no longer does (i.e. device unplugged
1347 * at present). We don't remove it from midi_port_info.
1349 x->second.exists = false;
1352 x->second.exists = true;
1354 /* check with backend for pre-existing pretty name */
1356 string value = AudioEngine::instance()->get_pretty_name_by_name (x->first);
1358 if (!value.empty()) {
1359 x->second.pretty_name = value;
1364 midi_info_dirty = false;
1368 PortManager::set_port_buffer_sizes (pframes_t n)
1371 boost::shared_ptr<Ports> all = ports.reader();
1373 for (Ports::iterator p = all->begin(); p != all->end(); ++p) {
1374 p->second->set_buffer_size (n);