2 Copyright (C) 2002 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 <pbd/failed_constructor.h>
24 #include <ardour/ardour.h>
25 #include <ardour/connection.h>
26 #include <pbd/xml++.h>
30 using namespace ARDOUR;
33 Connection::Connection (const XMLNode& node)
35 if (set_state (node)) {
36 throw failed_constructor();
40 InputConnection::InputConnection (const XMLNode& node)
45 OutputConnection::OutputConnection (const XMLNode& node)
51 Connection::set_name (string name, void *src)
58 Connection::add_connection (int port, string portname)
61 Glib::Mutex::Lock lm (port_lock);
62 _ports[port].push_back (portname);
64 ConnectionsChanged (port); /* EMIT SIGNAL */
68 Connection::remove_connection (int port, string portname)
73 Glib::Mutex::Lock lm (port_lock);
74 PortList& pl = _ports[port];
75 PortList::iterator i = find (pl.begin(), pl.end(), portname);
84 ConnectionsChanged (port); /* EMIT SIGNAL */
88 const Connection::PortList&
89 Connection::port_connections (int port) const
91 Glib::Mutex::Lock lm (port_lock);
96 Connection::operator== (const Connection& other) const
98 return other._ports == _ports;
102 Connection::add_port ()
105 Glib::Mutex::Lock lm (port_lock);
106 _ports.push_back (PortList());
108 ConfigurationChanged(); /* EMIT SIGNAL */
112 Connection::remove_port (int which_port)
114 bool changed = false;
117 Glib::Mutex::Lock lm (port_lock);
118 vector<PortList>::iterator i;
121 for (n = 0, i = _ports.begin(); i != _ports.end() && n < which_port; ++i, ++n);
123 if (i != _ports.end()) {
130 ConfigurationChanged(); /* EMIT SIGNAL */
138 Glib::Mutex::Lock lm (port_lock);
142 ConfigurationChanged(); /* EMIT SIGNAL */
146 Connection::get_state ()
151 if (dynamic_cast<InputConnection *> (this)) {
152 node = new XMLNode ("InputConnection");
154 node = new XMLNode ("OutputConnection");
157 node->add_property ("name", _name);
159 for (vector<PortList>::iterator i = _ports.begin(); i != _ports.end(); ++i) {
163 for (vector<string>::iterator ii = (*i).begin(); ii != (*i).end(); ++ii) {
164 if (ii != (*i).begin()) {
172 node->add_property ("connections", str);
178 Connection::set_state (const XMLNode& node)
180 const XMLProperty *prop;
182 if ((prop = node.property ("name")) == 0) {
183 error << _("Node for Connection has no \"name\" property") << endmsg;
187 _name = prop->value();
190 if ((prop = node.property ("connections")) == 0) {
191 error << _("Node for Connection has no \"connections\" property") << endmsg;
195 set_connections (prop->value());
201 Connection::set_connections (const string& str)
203 vector<string> ports;
208 if ((nports = count (str.begin(), str.end(), '{')) == 0) {
212 for (n = 0; n < nports; ++n) {
216 string::size_type start, end, ostart;
223 while ((start = str.find_first_of ('{', ostart)) != string::npos) {
226 if ((end = str.find_first_of ('}', start)) == string::npos) {
227 error << string_compose(_("IO: badly formed string in XML node for inputs \"%1\""), str) << endmsg;
231 if ((n = parse_io_string (str.substr (start, end - start), ports)) < 0) {
232 error << string_compose(_("bad input string in XML node \"%1\""), str) << endmsg;
238 for (int x = 0; x < n; ++x) {
239 add_connection (i, ports[x]);
251 Connection::parse_io_string (const string& str, vector<string>& ports)
253 string::size_type pos, opos;
255 if (str.length() == 0) {
264 while ((pos = str.find_first_of (',', opos)) != string::npos) {
265 ports.push_back (str.substr (opos, pos - opos));
269 if (opos < str.length()) {
270 ports.push_back (str.substr(opos));