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