summaryrefslogtreecommitdiff
path: root/libs/ardour/bundle.cc
blob: 0d8c36a84f4fffec9b921e3e5861358c6f9fc98e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/*
    Copyright (C) 2002 Paul Davis 

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

*/

#include <algorithm>

#include <pbd/failed_constructor.h>
#include <ardour/ardour.h>
#include <ardour/bundle.h>
#include <pbd/xml++.h>

#include "i18n.h"

using namespace ARDOUR;
using namespace PBD;

/** Construct a Bundle from an XML node.
 * @param node XML node.
 */
Bundle::Bundle (const XMLNode& node)
{
	if (set_state (node)) {
		throw failed_constructor();
	}
}

/** Construct an InputBundle from an XML node.
 * @param node XML node.
 */
InputBundle::InputBundle (const XMLNode& node)
	: Bundle (node)
{
  
}

/** Construct an OutputBundle from an XML node.
 * @param node XML node.
 */
OutputBundle::OutputBundle (const XMLNode& node)
	: Bundle (node)
{
  
}

/** Set the name.
 * @param name New name.
 */
void
Bundle::set_name (string name, void *src)
{
	_name = name;
	NameChanged (src);
}

/** Add an association between one of our channels and a JACK port.
 * @param ch Channel index.
 * @param portname JACK port name to associate with.
 */
void
Bundle::add_port_to_channel (int ch, string portname)
{
	{
		Glib::Mutex::Lock lm (channels_lock);
		_channels[ch].push_back (portname);
	}
	
	PortsChanged (ch); /* EMIT SIGNAL */
}

/** Disassociate a JACK port from one of our channels.
 * @param ch Channel index.
 * @param portname JACK port name to disassociate from.
 */

void
Bundle::remove_port_from_channel (int ch, string portname)
{
	bool changed = false;

	{
		Glib::Mutex::Lock lm (channels_lock);
		PortList& pl = _channels[ch];
		PortList::iterator i = find (pl.begin(), pl.end(), portname);
		
		if (i != pl.end()) {
			pl.erase (i);
			changed = true;
		}
	}

	if (changed) {
		 PortsChanged (ch); /* EMIT SIGNAL */
	}
}

/**
 * @param ch Channel index.
 * @return List of JACK ports that this channel is connected to.
 */
const Bundle::PortList&
Bundle::channel_ports (int ch) const
{
	Glib::Mutex::Lock lm (channels_lock);
	return _channels[ch];
}

/** operator== for Bundles; they are equal if their channels are the same.
 * @param other Bundle to compare with this one.
 */
bool
Bundle::operator== (const Bundle& other) const
{
	return other._channels == _channels;
}


/** Set the number of channels.
 * @param n New number of channels.
 */

void
Bundle::set_nchannels (int n)
{
	{
		Glib::Mutex::Lock lm (channels_lock);
		_channels.clear ();
		for (int i = 0; i < n; ++i) {
			_channels.push_back (PortList());
		}
	}

	ConfigurationChanged (); /* EMIT SIGNAL */
}

XMLNode&
Bundle::get_state ()
{
	XMLNode *node;
	string str;

	if (dynamic_cast<InputBundle *> (this)) {
		node = new XMLNode ("InputConnection");
	} else {
		node = new XMLNode ("OutputConnection");
	}

	node->add_property ("name", _name);

	for (vector<PortList>::iterator i = _channels.begin(); i != _channels.end(); ++i) {

		str += '{';

		for (vector<string>::iterator ii = (*i).begin(); ii != (*i).end(); ++ii) {
			if (ii != (*i).begin()) {
				str += ',';
			}
			str += *ii;
		}
		str += '}';
	}

	node->add_property ("connections", str);

	return *node;
}

int
Bundle::set_state (const XMLNode& node)
{
	const XMLProperty *prop;

	if ((prop = node.property ("name")) == 0) {
		error << _("Node for Connection has no \"name\" property") << endmsg;
		return -1;
	}

	_name = prop->value();
	_dynamic = false;
	
	if ((prop = node.property ("connections")) == 0) {
		error << _("Node for Connection has no \"connections\" property") << endmsg;
		return -1;
	}
	
	set_channels (prop->value());

	return 0;
}

/** Set up channels from an XML property string.
 * @param str String.
 * @return 0 on success, -1 on error.
 */
int
Bundle::set_channels (const string& str)
{
	vector<string> ports;
	int i;
	int n;
	int nchannels;
	
	if ((nchannels = count (str.begin(), str.end(), '{')) == 0) {
		return 0;
	}

	set_nchannels (nchannels);

	string::size_type start, end, ostart;

	ostart = 0;
	start = 0;
	end = 0;
	i = 0;

	while ((start = str.find_first_of ('{', ostart)) != string::npos) {
		start += 1;

		if ((end = str.find_first_of ('}', start)) == string::npos) {
			error << string_compose(_("IO: badly formed string in XML node for inputs \"%1\""), str) << endmsg;
			return -1;
		}

		if ((n = parse_io_string (str.substr (start, end - start), ports)) < 0) {
			error << string_compose(_("bad input string in XML node \"%1\""), str) << endmsg;

			return -1;
			
		} else if (n > 0) {

			for (int x = 0; x < n; ++x) {
				add_port_to_channel (i, ports[x]);
			}
		}

		ostart = end+1;
		i++;
	}

	return 0;
}

int
Bundle::parse_io_string (const string& str, vector<string>& ports)
{
	string::size_type pos, opos;

	if (str.length() == 0) {
		return 0;
	}

	pos = 0;
	opos = 0;

	ports.clear ();

	while ((pos = str.find_first_of (',', opos)) != string::npos) {
		ports.push_back (str.substr (opos, pos - opos));
		opos = pos + 1;
	}
	
	if (opos < str.length()) {
		ports.push_back (str.substr(opos));
	}

	return ports.size();
}