rollback to 3428, before the mysterious removal of libs/* at 3431/3432
[ardour.git] / libs / ardour / bundle.cc
1 /*
2     Copyright (C) 2002 Paul Davis 
3
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.
8
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.
13
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.
17
18 */
19
20 #include <algorithm>
21
22 #include <pbd/failed_constructor.h>
23 #include <ardour/ardour.h>
24 #include <ardour/bundle.h>
25 #include <pbd/xml++.h>
26
27 #include "i18n.h"
28
29 using namespace ARDOUR;
30 using namespace PBD;
31
32 /** Construct a Bundle from an XML node.
33  * @param node XML node.
34  */
35 Bundle::Bundle (const XMLNode& node)
36 {
37         if (set_state (node)) {
38                 throw failed_constructor();
39         }
40 }
41
42 /** Construct an InputBundle from an XML node.
43  * @param node XML node.
44  */
45 InputBundle::InputBundle (const XMLNode& node)
46         : Bundle (node)
47 {
48   
49 }
50
51 /** Construct an OutputBundle from an XML node.
52  * @param node XML node.
53  */
54 OutputBundle::OutputBundle (const XMLNode& node)
55         : Bundle (node)
56 {
57   
58 }
59
60 /** Set the name.
61  * @param name New name.
62  */
63 void
64 Bundle::set_name (string name, void *src)
65 {
66         _name = name;
67         NameChanged (src);
68 }
69
70 /** Add an association between one of our channels and a JACK port.
71  * @param ch Channel index.
72  * @param portname JACK port name to associate with.
73  */
74 void
75 Bundle::add_port_to_channel (int ch, string portname)
76 {
77         {
78                 Glib::Mutex::Lock lm (channels_lock);
79                 _channels[ch].push_back (portname);
80         }
81         
82         PortsChanged (ch); /* EMIT SIGNAL */
83 }
84
85 /** Disassociate a JACK port from one of our channels.
86  * @param ch Channel index.
87  * @param portname JACK port name to disassociate from.
88  */
89
90 void
91 Bundle::remove_port_from_channel (int ch, string portname)
92 {
93         bool changed = false;
94
95         {
96                 Glib::Mutex::Lock lm (channels_lock);
97                 PortList& pl = _channels[ch];
98                 PortList::iterator i = find (pl.begin(), pl.end(), portname);
99                 
100                 if (i != pl.end()) {
101                         pl.erase (i);
102                         changed = true;
103                 }
104         }
105
106         if (changed) {
107                  PortsChanged (ch); /* EMIT SIGNAL */
108         }
109 }
110
111 /**
112  * @param ch Channel index.
113  * @return List of JACK ports that this channel is connected to.
114  */
115 const Bundle::PortList&
116 Bundle::channel_ports (int ch) const
117 {
118         Glib::Mutex::Lock lm (channels_lock);
119         return _channels[ch];
120 }
121
122 /** operator== for Bundles; they are equal if their channels are the same.
123  * @param other Bundle to compare with this one.
124  */
125 bool
126 Bundle::operator== (const Bundle& other) const
127 {
128         return other._channels == _channels;
129 }
130
131
132 /** Set the number of channels.
133  * @param n New number of channels.
134  */
135
136 void
137 Bundle::set_nchannels (int n)
138 {
139         {
140                 Glib::Mutex::Lock lm (channels_lock);
141                 _channels.clear ();
142                 for (int i = 0; i < n; ++i) {
143                         _channels.push_back (PortList());
144                 }
145         }
146
147         ConfigurationChanged (); /* EMIT SIGNAL */
148 }
149
150 XMLNode&
151 Bundle::get_state ()
152 {
153         XMLNode *node;
154         string str;
155
156         if (dynamic_cast<InputBundle *> (this)) {
157                 node = new XMLNode ("InputConnection");
158         } else {
159                 node = new XMLNode ("OutputConnection");
160         }
161
162         node->add_property ("name", _name);
163
164         for (vector<PortList>::iterator i = _channels.begin(); i != _channels.end(); ++i) {
165
166                 str += '{';
167
168                 for (vector<string>::iterator ii = (*i).begin(); ii != (*i).end(); ++ii) {
169                         if (ii != (*i).begin()) {
170                                 str += ',';
171                         }
172                         str += *ii;
173                 }
174                 str += '}';
175         }
176
177         node->add_property ("connections", str);
178
179         return *node;
180 }
181
182 int
183 Bundle::set_state (const XMLNode& node)
184 {
185         const XMLProperty *prop;
186
187         if ((prop = node.property ("name")) == 0) {
188                 error << _("Node for Connection has no \"name\" property") << endmsg;
189                 return -1;
190         }
191
192         _name = prop->value();
193         _dynamic = false;
194         
195         if ((prop = node.property ("connections")) == 0) {
196                 error << _("Node for Connection has no \"connections\" property") << endmsg;
197                 return -1;
198         }
199         
200         set_channels (prop->value());
201
202         return 0;
203 }
204
205 /** Set up channels from an XML property string.
206  * @param str String.
207  * @return 0 on success, -1 on error.
208  */
209 int
210 Bundle::set_channels (const string& str)
211 {
212         vector<string> ports;
213         int i;
214         int n;
215         int nchannels;
216         
217         if ((nchannels = count (str.begin(), str.end(), '{')) == 0) {
218                 return 0;
219         }
220
221         set_nchannels (nchannels);
222
223         string::size_type start, end, ostart;
224
225         ostart = 0;
226         start = 0;
227         end = 0;
228         i = 0;
229
230         while ((start = str.find_first_of ('{', ostart)) != string::npos) {
231                 start += 1;
232
233                 if ((end = str.find_first_of ('}', start)) == string::npos) {
234                         error << string_compose(_("IO: badly formed string in XML node for inputs \"%1\""), str) << endmsg;
235                         return -1;
236                 }
237
238                 if ((n = parse_io_string (str.substr (start, end - start), ports)) < 0) {
239                         error << string_compose(_("bad input string in XML node \"%1\""), str) << endmsg;
240
241                         return -1;
242                         
243                 } else if (n > 0) {
244
245                         for (int x = 0; x < n; ++x) {
246                                 add_port_to_channel (i, ports[x]);
247                         }
248                 }
249
250                 ostart = end+1;
251                 i++;
252         }
253
254         return 0;
255 }
256
257 int
258 Bundle::parse_io_string (const string& str, vector<string>& ports)
259 {
260         string::size_type pos, opos;
261
262         if (str.length() == 0) {
263                 return 0;
264         }
265
266         pos = 0;
267         opos = 0;
268
269         ports.clear ();
270
271         while ((pos = str.find_first_of (',', opos)) != string::npos) {
272                 ports.push_back (str.substr (opos, pos - opos));
273                 opos = pos + 1;
274         }
275         
276         if (opos < str.length()) {
277                 ports.push_back (str.substr(opos));
278         }
279
280         return ports.size();
281 }
282