summaryrefslogtreecommitdiff
path: root/libs/ardour/control_protocol_manager.cc
blob: c79aa74698fc9c56383053106270a607303aed38 (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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
/*
    Copyright (C) 2000-2007 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 <dlfcn.h>

#include <pbd/compose.h>
#include <pbd/error.h>
#include <pbd/pathscanner.h>

#include <control_protocol/control_protocol.h>

#include <ardour/session.h>
#include <ardour/control_protocol_manager.h>

using namespace ARDOUR;
using namespace std;
using namespace PBD;

#include "i18n.h"

ControlProtocolManager* ControlProtocolManager::_instance = 0;
const string ControlProtocolManager::state_node_name = X_("ControlProtocols");

ControlProtocolManager::ControlProtocolManager ()
{
	if (_instance == 0) {
		_instance = this;
	}

	_session = 0;
}

ControlProtocolManager::~ControlProtocolManager()
{
	Glib::Mutex::Lock lm (protocols_lock);

	for (list<ControlProtocol*>::iterator i = control_protocols.begin(); i != control_protocols.end(); ++i) {
		delete (*i);
	}

	control_protocols.clear ();

	
	for (list<ControlProtocolInfo*>::iterator p = control_protocol_info.begin(); p != control_protocol_info.end(); ++p) {
		delete (*p);
	}

	control_protocol_info.clear();
}

void
ControlProtocolManager::set_session (Session& s)
{
	_session = &s;
	_session->GoingAway.connect (mem_fun (*this, &ControlProtocolManager::drop_session));

	for (list<ControlProtocolInfo*>::iterator i = control_protocol_info.begin(); i != control_protocol_info.end(); ++i) {
		if ((*i)->requested || (*i)->mandatory) {
			instantiate (**i);
			(*i)->requested = false;

			if ((*i)->protocol && (*i)->state) {
				(*i)->protocol->set_state (*(*i)->state);
			}
		}
	}
}

void
ControlProtocolManager::drop_session ()
{
	_session = 0;

	{
		Glib::Mutex::Lock lm (protocols_lock);
		for (list<ControlProtocol*>::iterator p = control_protocols.begin(); p != control_protocols.end(); ++p) {
			delete *p;
		}
		control_protocols.clear ();
		
		for (list<ControlProtocolInfo*>::iterator p = control_protocol_info.begin(); p != control_protocol_info.end(); ++p) {
			// otherwise the ControlProtocol instances are not recreated in set_session
			(*p)->requested = true;
			(*p)->protocol = 0;
		}
	}
}

ControlProtocol*
ControlProtocolManager::instantiate (ControlProtocolInfo& cpi)
{
	if (_session == 0) {
		return 0;
	}

	cpi.descriptor = get_descriptor (cpi.path);

	if (cpi.descriptor == 0) {
		error << string_compose (_("control protocol name \"%1\" has no descriptor"), cpi.name) << endmsg;
		return 0;
	}

	if ((cpi.protocol = cpi.descriptor->initialize (cpi.descriptor, _session)) == 0) {
		error << string_compose (_("control protocol name \"%1\" could not be initialized"), cpi.name) << endmsg;
		return 0;
	}

	Glib::Mutex::Lock lm (protocols_lock);
	control_protocols.push_back (cpi.protocol);

	return cpi.protocol;
}

int
ControlProtocolManager::teardown (ControlProtocolInfo& cpi)
{
	if (!cpi.protocol) {
		return 0;
	}

	if (!cpi.descriptor) {
		return 0;
	}

	if (cpi.mandatory) {
		return 0;
	}

	cpi.descriptor->destroy (cpi.descriptor, cpi.protocol);
	
	{
		Glib::Mutex::Lock lm (protocols_lock);
		list<ControlProtocol*>::iterator p = find (control_protocols.begin(), control_protocols.end(), cpi.protocol);
		if (p != control_protocols.end()) {
			control_protocols.erase (p);
		} else {
			cerr << "Programming error: ControlProtocolManager::teardown() called for " << cpi.name << ", but it was not found in control_protocols" << endl;
		}

		list<ControlProtocolInfo*>::iterator p2 = find (control_protocol_info.begin(), control_protocol_info.end(), &cpi);
		if (p2 != control_protocol_info.end()) {
			control_protocol_info.erase (p2);
		} else {
			cerr << "Programming error: ControlProtocolManager::teardown() called for " << cpi.name << ", but it was not found in control_protocol_info" << endl;
		}
	}
	
	cpi.protocol = 0;
	dlclose (cpi.descriptor->module);
	return 0;
}

static bool protocol_filter (const string& str, void *arg)
{
	/* Not a dotfile, has a prefix before a period, suffix is "so", or "dylib" */
	
	return str[0] != '.' 
	  && ((str.length() > 3 && str.find (".so") == (str.length() - 3))
	      || (str.length() > 6 && str.find (".dylib") == (str.length() - 6)));
}

void
ControlProtocolManager::load_mandatory_protocols ()
{
	if (_session == 0) {
		return;
	}

	for (list<ControlProtocolInfo*>::iterator i = control_protocol_info.begin(); i != control_protocol_info.end(); ++i) {
		if ((*i)->mandatory && ((*i)->protocol == 0)) {
			info << string_compose (_("Instantiating mandatory control protocol %1"), (*i)->name) << endmsg;
			instantiate (**i);
		}
	}
}

void
ControlProtocolManager::discover_control_protocols (string path)
{
	vector<string *> *found;
	PathScanner scanner;

	info << string_compose (_("looking for control protocols in %1"), path) << endmsg;

	found = scanner (path, protocol_filter, 0, false, true);

	for (vector<string*>::iterator i = found->begin(); i != found->end(); ++i) {
		control_protocol_discover (**i);
		delete *i;
	}

	delete found;
}

int
ControlProtocolManager::control_protocol_discover (string path)
{
	ControlProtocolDescriptor* descriptor;

	if ((descriptor = get_descriptor (path)) != 0) {

		ControlProtocolInfo* cpi = new ControlProtocolInfo ();

		if (!descriptor->probe (descriptor)) {
			info << string_compose (_("Control protocol %1 not usable"), descriptor->name) << endmsg;
		} else {

			cpi->descriptor = descriptor;
			cpi->name = descriptor->name;
			cpi->path = path;
			cpi->protocol = 0;
			cpi->requested = false;
			cpi->mandatory = descriptor->mandatory;
			cpi->supports_feedback = descriptor->supports_feedback;
			cpi->state = 0;
			
			control_protocol_info.push_back (cpi);
			
			info << string_compose(_("Control surface protocol discovered: \"%1\""), cpi->name) << endmsg;
		}

		dlclose (descriptor->module);
	}

	return 0;
}

ControlProtocolDescriptor*
ControlProtocolManager::get_descriptor (string path)
{
	void *module;
	ControlProtocolDescriptor *descriptor = 0;
	ControlProtocolDescriptor* (*dfunc)(void);
	const char *errstr;

	if ((module = dlopen (path.c_str(), RTLD_NOW)) == 0) {
		error << string_compose(_("ControlProtocolManager: cannot load module \"%1\" (%2)"), path, dlerror()) << endmsg;
		return 0;
	}


	dfunc = (ControlProtocolDescriptor* (*)(void)) dlsym (module, "protocol_descriptor");

	if ((errstr = dlerror()) != 0) {
		error << string_compose(_("ControlProtocolManager: module \"%1\" has no descriptor function."), path) << endmsg;
		error << errstr << endmsg;
		dlclose (module);
		return 0;
	}

	descriptor = dfunc();
	if (descriptor) {
		descriptor->module = module;
	} else {
		dlclose (module);
	}

	return descriptor;
}

void
ControlProtocolManager::foreach_known_protocol (sigc::slot<void,const ControlProtocolInfo*> method)
{
	for (list<ControlProtocolInfo*>::iterator i = control_protocol_info.begin(); i != control_protocol_info.end(); ++i) {
		method (*i);
	}
}

ControlProtocolInfo*
ControlProtocolManager::cpi_by_name (string name)
{
	for (list<ControlProtocolInfo*>::iterator i = control_protocol_info.begin(); i != control_protocol_info.end(); ++i) {
		if (name == (*i)->name) {
			return *i;
		}
	}
	return 0;
}

int
ControlProtocolManager::set_state (const XMLNode& node)
{
	XMLNodeList clist;
	XMLNodeConstIterator citer;
	XMLProperty* prop;

	clist = node.children();

	for (citer = clist.begin(); citer != clist.end(); ++citer) {
		if ((*citer)->name() == X_("Protocol")) {

			prop = (*citer)->property (X_("active"));

			if (prop && prop->value() == X_("yes")) {
				if ((prop = (*citer)->property (X_("name"))) != 0) {
					ControlProtocolInfo* cpi = cpi_by_name (prop->value());
					if (cpi) {
						if (!(*citer)->children().empty()) {
							cpi->state = (*citer)->children().front ();
						} else {
							cpi->state = 0;
						}
						
						if (_session) {
							instantiate (*cpi);
						} else {
							cpi->requested = true;
						}
					}
				}
			}
		}    
	}
	return 0;
}

XMLNode&
ControlProtocolManager::get_state (void)
{
	XMLNode* root = new XMLNode (state_node_name);
	Glib::Mutex::Lock lm (protocols_lock);

	for (list<ControlProtocolInfo*>::iterator i = control_protocol_info.begin(); i != control_protocol_info.end(); ++i) {

		XMLNode * child;

		if ((*i)->protocol) {
			child = &((*i)->protocol->get_state());
			child->add_property (X_("active"), "yes");
			// should we update (*i)->state here?  probably.
			root->add_child_nocopy (*child);
		}
		else if ((*i)->state) {
			// keep ownership clear
			root->add_child_copy (*(*i)->state);
		}
		else {
			child = new XMLNode (X_("Protocol"));
			child->add_property (X_("name"), (*i)->name);
			child->add_property (X_("active"), "no");
			root->add_child_nocopy (*child);
		}
	}

	return *root;
}

void
ControlProtocolManager::set_protocol_states (const XMLNode& node)
{
	XMLNodeList nlist;
	XMLNodeConstIterator niter;
	XMLProperty* prop;

	nlist = node.children();

	for (niter = nlist.begin(); niter != nlist.end(); ++niter) {

		XMLNode* child = (*niter);

		if ((prop = child->property ("name")) == 0) {
			error << _("control protocol XML node has no name property. Ignored.") << endmsg;
			continue;
		}

		ControlProtocolInfo* cpi = cpi_by_name (prop->value());

		if (!cpi) {
			warning << string_compose (_("control protocol \"%1\" is not known. Ignored"), prop->value()) << endmsg;
			continue;
		}

		/* copy the node so that ownership is clear */

		cpi->state = new XMLNode (*child);
	}
}