summaryrefslogtreecommitdiff
path: root/libs/ardour/control_protocol_manager.cc
blob: b89b83a29ffe51492de740d912b308d5e4cd0e0e (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
#include <dlfcn.h>

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

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

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

#include "i18n.h"

ControlProtocolManager* ControlProtocolManager::_instance = 0;

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

ControlProtocolManager::~ControlProtocolManager()
{
	LockMonitor lm (protocols_lock, __LINE__, __FILE__);

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

	control_protocols.clear ();
		
}

void
ControlProtocolManager::startup (Session& s)
{
	list<ControlProtocolInfo *>::iterator i;
	
	for (i = control_protocol_info.begin(); i != control_protocol_info.end(); ++i) {

		ControlProtocolInfo* cpi = (*i);

		if (cpi->name == "Tranzport") {
		
			cpi->descriptor = get_descriptor ((*i)->path);
			
			if (cpi->descriptor == 0) {
				error << string_compose (_("control protocol name \"%1\" has no descriptor"), cpi->name) << endmsg;
				continue;
			}
			
			if ((cpi->protocol = cpi->descriptor->initialize (cpi->descriptor, &s)) == 0) {
				error << string_compose (_("control protocol name \"%1\" could not be initialized"), cpi->name) << endmsg;
				continue;
			}
			
			{
				LockMonitor lm (protocols_lock, __LINE__, __FILE__);
				control_protocols.push_back (cpi->protocol);
			}
			
			cpi->protocol->init ();
			cpi->protocol->set_active (true);
		}
	}
}

ControlProtocol*
ControlProtocolManager::instantiate (Session& session, string name)
{
	list<ControlProtocolInfo *>::iterator i;

	for (i = control_protocol_info.begin(); i != control_protocol_info.end(); ++i) {
		if ((*i)->name == name) {
			break;
		}
	}

	if (i == control_protocol_info.end()) {
		error << string_compose (_("control protocol name \"%1\" is unknown"), name) << endmsg;
		return 0;
	}

	ControlProtocolInfo* cpi = (*i);

	cpi->descriptor = get_descriptor ((*i)->path);

	if (cpi->descriptor == 0) {
		error << string_compose (_("control protocol name \"%1\" has no descriptor"), 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"), name) << endmsg;
		return 0;
	}

	LockMonitor lm (protocols_lock, __LINE__, __FILE__);
	control_protocols.push_back (cpi->protocol);
	return cpi->protocol;
}

int
ControlProtocolManager::teardown (string name)
{
	for (list<ControlProtocolInfo*>::iterator i = control_protocol_info.begin(); i != control_protocol_info.end(); ++i) {
		ControlProtocolInfo* cpi = *i;

		if (cpi->name == name && cpi->descriptor && cpi->protocol) {
			cpi->descriptor->destroy (cpi->descriptor, cpi->protocol);
			
			{
				LockMonitor lm (protocols_lock, __LINE__, __FILE__);
				list<ControlProtocol*>::iterator p = find (control_protocols.begin(), control_protocols.end(), cpi->protocol);
				if (p != control_protocols.end()) {
					control_protocols.erase (p);
				}
			}

			cpi->protocol = 0;
			return 0;
		}
	}

	return -1;
}

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

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

	cerr << "CP Manager looking for surfaces\n";

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

	for (vector<string*>::iterator i = found->begin(); i != found->end(); ++i) {
		cerr << "CP Manager looking at " << **i << endl;
		control_protocol_discover (**i);
		delete *i;
	}

	delete found;
}

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

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

		ControlProtocolInfo* info = new ControlProtocolInfo ();

		info->descriptor = descriptor;
		info->name = descriptor->name;
		info->path = path;
		
		control_protocol_info.push_back (info);

		cerr << "Found \"" << info->name << "\"\n";

		dlclose (descriptor->module);

	} else {
		cerr << "no descriptor\n";
	}

	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;
}