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;
32 Connection::Connection (const XMLNode& node)
34 if (set_state (node)) {
35 throw failed_constructor();
39 InputConnection::InputConnection (const XMLNode& node)
44 OutputConnection::OutputConnection (const XMLNode& node)
50 Connection::set_name (string name, void *src)
57 Connection::add_connection (int port, string portname)
60 Glib::Mutex::Lock lm (port_lock);
61 _ports[port].push_back (portname);
63 ConnectionsChanged (port); /* EMIT SIGNAL */
67 Connection::remove_connection (int port, string portname)
72 Glib::Mutex::Lock lm (port_lock);
73 PortList& pl = _ports[port];
74 PortList::iterator i = find (pl.begin(), pl.end(), portname);
83 ConnectionsChanged (port); /* EMIT SIGNAL */
87 const Connection::PortList&
88 Connection::port_connections (int port) const
90 Glib::Mutex::Lock lm (port_lock);
95 Connection::operator== (const Connection& other) const
97 return other._ports == _ports;
101 Connection::add_port ()
104 Glib::Mutex::Lock lm (port_lock);
105 _ports.push_back (PortList());
107 ConfigurationChanged(); /* EMIT SIGNAL */
111 Connection::remove_port (int which_port)
113 bool changed = false;
116 Glib::Mutex::Lock lm (port_lock);
117 vector<PortList>::iterator i;
120 for (n = 0, i = _ports.begin(); i != _ports.end() && n < which_port; ++i, ++n);
122 if (i != _ports.end()) {
129 ConfigurationChanged(); /* EMIT SIGNAL */
137 Glib::Mutex::Lock lm (port_lock);
141 ConfigurationChanged(); /* EMIT SIGNAL */
145 Connection::get_state ()
150 if (dynamic_cast<InputConnection *> (this)) {
151 node = new XMLNode ("InputConnection");
153 node = new XMLNode ("OutputConnection");
156 node->add_property ("name", _name);
158 for (vector<PortList>::iterator i = _ports.begin(); i != _ports.end(); ++i) {
162 for (vector<string>::iterator ii = (*i).begin(); ii != (*i).end(); ++ii) {
163 if (ii != (*i).begin()) {
171 node->add_property ("connections", str);
177 Connection::set_state (const XMLNode& node)
179 const XMLProperty *prop;
181 if ((prop = node.property ("name")) == 0) {
182 error << _("Node for Connection has no \"name\" property") << endmsg;
186 _name = prop->value();
189 if ((prop = node.property ("connections")) == 0) {
190 error << _("Node for Connection has no \"connections\" property") << endmsg;
194 set_connections (prop->value());
200 Connection::set_connections (const string& str)
202 vector<string> ports;
207 if ((nports = count (str.begin(), str.end(), '{')) == 0) {
211 for (n = 0; n < nports; ++n) {
215 string::size_type start, end, ostart;
222 while ((start = str.find_first_of ('{', ostart)) != string::npos) {
225 if ((end = str.find_first_of ('}', start)) == string::npos) {
226 error << string_compose(_("IO: badly formed string in XML node for inputs \"%1\""), str) << endmsg;
230 if ((n = parse_io_string (str.substr (start, end - start), ports)) < 0) {
231 error << string_compose(_("bad input string in XML node \"%1\""), str) << endmsg;
237 for (int x = 0; x < n; ++x) {
238 add_connection (i, ports[x]);
250 Connection::parse_io_string (const string& str, vector<string>& ports)
252 string::size_type pos, opos;
254 if (str.length() == 0) {
263 while ((pos = str.find_first_of (',', opos)) != string::npos) {
264 ports.push_back (str.substr (opos, pos - opos));
268 if (opos < str.length()) {
269 ports.push_back (str.substr(opos));