Merged with trunk R1393.
[ardour.git] / libs / ardour / control_protocol_manager.cc
1 #include <dlfcn.h>
2
3 #include <pbd/compose.h>
4 #include <pbd/error.h>
5 #include <pbd/pathscanner.h>
6
7 #include <control_protocol/control_protocol.h>
8
9 #include <ardour/session.h>
10 #include <ardour/control_protocol_manager.h>
11
12 using namespace ARDOUR;
13 using namespace std;
14 using namespace PBD;
15
16 #include "i18n.h"
17
18 ControlProtocolManager* ControlProtocolManager::_instance = 0;
19 const string ControlProtocolManager::state_node_name = X_("ControlProtocols");
20
21 ControlProtocolManager::ControlProtocolManager ()
22 {
23         if (_instance == 0) {
24                 _instance = this;
25         }
26
27         _session = 0;
28 }
29
30 ControlProtocolManager::~ControlProtocolManager()
31 {
32         Glib::Mutex::Lock lm (protocols_lock);
33
34         for (list<ControlProtocol*>::iterator i = control_protocols.begin(); i != control_protocols.end(); ++i) {
35                 delete (*i);
36         }
37
38         control_protocols.clear ();
39
40         
41         for (list<ControlProtocolInfo*>::iterator p = control_protocol_info.begin(); p != control_protocol_info.end(); ++p) {
42                 delete (*p);
43         }
44
45         control_protocol_info.clear();
46                 
47 }
48
49 void
50 ControlProtocolManager::set_session (Session& s)
51 {
52         _session = &s;
53         _session->GoingAway.connect (mem_fun (*this, &ControlProtocolManager::drop_session));
54
55         for (list<ControlProtocolInfo*>::iterator i = control_protocol_info.begin(); i != control_protocol_info.end(); ++i) {
56                 if ((*i)->requested || (*i)->mandatory) {
57                         instantiate (**i);
58                         (*i)->requested = false;
59
60                         if ((*i)->protocol && (*i)->state) {
61                                 (*i)->protocol->set_state (*(*i)->state);
62                         }
63                 }
64         }
65 }
66
67 void
68 ControlProtocolManager::drop_session ()
69 {
70         _session = 0;
71
72         {
73                 Glib::Mutex::Lock lm (protocols_lock);
74                 for (list<ControlProtocol*>::iterator p = control_protocols.begin(); p != control_protocols.end(); ++p) {
75                         delete *p;
76                 }
77                 control_protocols.clear ();
78
79                 for (list<ControlProtocolInfo*>::iterator p = control_protocol_info.begin(); p != control_protocol_info.end(); ++p) {
80                         delete *p;
81                 }
82
83                 control_protocol_info.clear();
84         }
85 }
86
87 ControlProtocol*
88 ControlProtocolManager::instantiate (ControlProtocolInfo& cpi)
89 {
90         if (_session == 0) {
91                 return 0;
92         }
93
94         cpi.descriptor = get_descriptor (cpi.path);
95
96         if (cpi.descriptor == 0) {
97                 error << string_compose (_("control protocol name \"%1\" has no descriptor"), cpi.name) << endmsg;
98                 return 0;
99         }
100
101         if ((cpi.protocol = cpi.descriptor->initialize (cpi.descriptor, _session)) == 0) {
102                 error << string_compose (_("control protocol name \"%1\" could not be initialized"), cpi.name) << endmsg;
103                 return 0;
104         }
105
106         Glib::Mutex::Lock lm (protocols_lock);
107         control_protocols.push_back (cpi.protocol);
108
109         if (cpi.state) {
110                 cpi.protocol->set_state (*cpi.state);
111         }
112
113         return cpi.protocol;
114 }
115
116 int
117 ControlProtocolManager::teardown (ControlProtocolInfo& cpi)
118 {
119         if (!cpi.protocol) {
120                 return 0;
121         }
122
123         if (!cpi.descriptor) {
124                 return 0;
125         }
126
127         if (cpi.mandatory) {
128                 return 0;
129         }
130
131         cpi.descriptor->destroy (cpi.descriptor, cpi.protocol);
132         
133         {
134                 Glib::Mutex::Lock lm (protocols_lock);
135                 list<ControlProtocol*>::iterator p = find (control_protocols.begin(), control_protocols.end(), cpi.protocol);
136                 if (p != control_protocols.end()) {
137                         control_protocols.erase (p);
138                 } else {
139                         cerr << "Programming error: ControlProtocolManager::teardown() called for " << cpi.name << ", but it was not found in control_protocols" << endl;
140                 }
141
142                 list<ControlProtocolInfo*>::iterator p2 = find (control_protocol_info.begin(), control_protocol_info.end(), &cpi);
143                 if (p2 != control_protocol_info.end()) {
144                         control_protocol_info.erase (p2);
145                 } else {
146                         cerr << "Programming error: ControlProtocolManager::teardown() called for " << cpi.name << ", but it was not found in control_protocol_info" << endl;
147                 }
148         }
149         
150         cpi.protocol = 0;
151         dlclose (cpi.descriptor->module);
152         return 0;
153 }
154
155 static bool protocol_filter (const string& str, void *arg)
156 {
157         /* Not a dotfile, has a prefix before a period, suffix is "so", or "dylib" */
158         
159         return str[0] != '.' 
160           && ((str.length() > 3 && str.find (".so") == (str.length() - 3))
161               || (str.length() > 6 && str.find (".dylib") == (str.length() - 6)));
162 }
163
164 void
165 ControlProtocolManager::load_mandatory_protocols ()
166 {
167         if (_session == 0) {
168                 return;
169         }
170
171         for (list<ControlProtocolInfo*>::iterator i = control_protocol_info.begin(); i != control_protocol_info.end(); ++i) {
172                 if ((*i)->mandatory && ((*i)->protocol == 0)) {
173                         info << string_compose (_("Instantiating mandatory control protocol %1"), (*i)->name) << endmsg;
174                         instantiate (**i);
175                 }
176         }
177 }
178
179 void
180 ControlProtocolManager::discover_control_protocols (string path)
181 {
182         vector<string *> *found;
183         PathScanner scanner;
184
185         info << string_compose (_("looking for control protocols in %1"), path) << endmsg;
186
187         found = scanner (path, protocol_filter, 0, false, true);
188
189         for (vector<string*>::iterator i = found->begin(); i != found->end(); ++i) {
190                 control_protocol_discover (**i);
191                 delete *i;
192         }
193
194         delete found;
195 }
196
197 int
198 ControlProtocolManager::control_protocol_discover (string path)
199 {
200         ControlProtocolDescriptor* descriptor;
201
202         if ((descriptor = get_descriptor (path)) != 0) {
203
204                 ControlProtocolInfo* cpi = new ControlProtocolInfo ();
205
206                 if (!descriptor->probe (descriptor)) {
207                         info << string_compose (_("Control protocol %1 not usable"), descriptor->name) << endmsg;
208                 } else {
209
210                         cpi->descriptor = descriptor;
211                         cpi->name = descriptor->name;
212                         cpi->path = path;
213                         cpi->protocol = 0;
214                         cpi->requested = false;
215                         cpi->mandatory = descriptor->mandatory;
216                         cpi->supports_feedback = descriptor->supports_feedback;
217                         cpi->state = 0;
218                         
219                         control_protocol_info.push_back (cpi);
220                         
221                         info << string_compose(_("Control surface protocol discovered: \"%1\""), cpi->name) << endmsg;
222                 }
223
224                 dlclose (descriptor->module);
225         }
226
227         return 0;
228 }
229
230 ControlProtocolDescriptor*
231 ControlProtocolManager::get_descriptor (string path)
232 {
233         void *module;
234         ControlProtocolDescriptor *descriptor = 0;
235         ControlProtocolDescriptor* (*dfunc)(void);
236         const char *errstr;
237
238         if ((module = dlopen (path.c_str(), RTLD_NOW)) == 0) {
239                 error << string_compose(_("ControlProtocolManager: cannot load module \"%1\" (%2)"), path, dlerror()) << endmsg;
240                 return 0;
241         }
242
243
244         dfunc = (ControlProtocolDescriptor* (*)(void)) dlsym (module, "protocol_descriptor");
245
246         if ((errstr = dlerror()) != 0) {
247                 error << string_compose(_("ControlProtocolManager: module \"%1\" has no descriptor function."), path) << endmsg;
248                 error << errstr << endmsg;
249                 dlclose (module);
250                 return 0;
251         }
252
253         descriptor = dfunc();
254         if (descriptor) {
255                 descriptor->module = module;
256         } else {
257                 dlclose (module);
258         }
259
260         return descriptor;
261 }
262
263 void
264 ControlProtocolManager::foreach_known_protocol (sigc::slot<void,const ControlProtocolInfo*> method)
265 {
266         for (list<ControlProtocolInfo*>::iterator i = control_protocol_info.begin(); i != control_protocol_info.end(); ++i) {
267                 method (*i);
268         }
269 }
270
271 ControlProtocolInfo*
272 ControlProtocolManager::cpi_by_name (string name)
273 {
274         for (list<ControlProtocolInfo*>::iterator i = control_protocol_info.begin(); i != control_protocol_info.end(); ++i) {
275                 if (name == (*i)->name) {
276                         return *i;
277                 }
278         }
279         return 0;
280 }
281
282 int
283 ControlProtocolManager::set_state (const XMLNode& node)
284 {
285         XMLNodeList clist;
286         XMLNodeConstIterator citer;
287         XMLProperty* prop;
288
289         clist = node.children();
290
291         for (citer = clist.begin(); citer != clist.end(); ++citer) {
292                 if ((*citer)->name() == X_("Protocol")) {
293
294                         prop = (*citer)->property (X_("active"));
295
296                         if (prop && prop->value() == X_("yes")) {
297                                 if ((prop = (*citer)->property (X_("name"))) != 0) {
298                                         ControlProtocolInfo* cpi = cpi_by_name (prop->value());
299                                         if (cpi) {
300
301                                                 if (!(*citer)->children().empty()) {
302                                                         cpi->state = (*citer)->children().front ();
303                                                 } else {
304                                                         cpi->state = 0;
305                                                 }
306                                                 
307                                                 if (_session) {
308                                                         instantiate (*cpi);
309                                                 } else {
310                                                         cpi->requested = true;
311                                                 }
312                                         }
313                                 }
314                         }
315                 }    
316         }
317         return 0;
318 }
319
320 XMLNode&
321 ControlProtocolManager::get_state (void)
322 {
323         XMLNode* root = new XMLNode (state_node_name);
324         Glib::Mutex::Lock lm (protocols_lock);
325
326         for (list<ControlProtocolInfo*>::iterator i = control_protocol_info.begin(); i != control_protocol_info.end(); ++i) {
327
328                 XMLNode * child;
329
330                 if ((*i)->protocol) {
331                         child = &((*i)->protocol->get_state());
332                         child->add_property (X_("active"), "yes");
333                         // should we update (*i)->state here?  probably.
334                         root->add_child_nocopy (*child);
335                 }
336                 else if ((*i)->state) {
337                         // keep ownership clear
338                         root->add_child_copy (*(*i)->state);
339                 }
340                 else {
341                         child = new XMLNode (X_("Protocol"));
342                         child->add_property (X_("name"), (*i)->name);
343                         child->add_property (X_("active"), "no");
344                         root->add_child_nocopy (*child);
345                 }
346         }
347
348         return *root;
349 }
350
351 void
352 ControlProtocolManager::set_protocol_states (const XMLNode& node)
353 {
354         XMLNodeList nlist;
355         XMLNodeConstIterator niter;
356         XMLProperty* prop;
357
358         nlist = node.children();
359
360         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
361
362                 XMLNode* child = (*niter);
363
364                 if ((prop = child->property ("name")) == 0) {
365                         error << _("control protocol XML node has no name property. Ignored.") << endmsg;
366                         continue;
367                 }
368
369                 ControlProtocolInfo* cpi = cpi_by_name (prop->value());
370
371                 if (!cpi) {
372                         warning << string_compose (_("control protocol \"%1\" is not known. Ignored"), prop->value()) << endmsg;
373                         continue;
374                 }
375
376                 /* copy the node so that ownership is clear */
377
378                 cpi->state = new XMLNode (*child);
379         }
380 }