Merged with trunk R1141
[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" */
158         
159         return str[0] != '.' && (str.length() > 3 && str.find (".so") == (str.length() - 3));
160 }
161
162 void
163 ControlProtocolManager::load_mandatory_protocols ()
164 {
165         if (_session == 0) {
166                 return;
167         }
168
169         for (list<ControlProtocolInfo*>::iterator i = control_protocol_info.begin(); i != control_protocol_info.end(); ++i) {
170                 if ((*i)->mandatory && ((*i)->protocol == 0)) {
171                         info << string_compose (_("Instantiating mandatory control protocol %1"), (*i)->name) << endmsg;
172                         instantiate (**i);
173                 }
174         }
175 }
176
177 void
178 ControlProtocolManager::discover_control_protocols (string path)
179 {
180         vector<string *> *found;
181         PathScanner scanner;
182
183         info << string_compose (_("looking for control protocols in %1"), path) << endmsg;
184
185         found = scanner (path, protocol_filter, 0, false, true);
186
187         for (vector<string*>::iterator i = found->begin(); i != found->end(); ++i) {
188                 control_protocol_discover (**i);
189                 delete *i;
190         }
191
192         delete found;
193 }
194
195 int
196 ControlProtocolManager::control_protocol_discover (string path)
197 {
198         ControlProtocolDescriptor* descriptor;
199
200         if ((descriptor = get_descriptor (path)) != 0) {
201
202                 ControlProtocolInfo* cpi = new ControlProtocolInfo ();
203
204                 if (!descriptor->probe (descriptor)) {
205                         info << string_compose (_("Control protocol %1 not usable"), descriptor->name) << endmsg;
206                 } else {
207
208                         cpi->descriptor = descriptor;
209                         cpi->name = descriptor->name;
210                         cpi->path = path;
211                         cpi->protocol = 0;
212                         cpi->requested = false;
213                         cpi->mandatory = descriptor->mandatory;
214                         cpi->supports_feedback = descriptor->supports_feedback;
215                         cpi->state = 0;
216                         
217                         control_protocol_info.push_back (cpi);
218                         
219                         info << string_compose(_("Control surface protocol discovered: \"%1\""), cpi->name) << endmsg;
220                 }
221
222                 dlclose (descriptor->module);
223         }
224
225         return 0;
226 }
227
228 ControlProtocolDescriptor*
229 ControlProtocolManager::get_descriptor (string path)
230 {
231         void *module;
232         ControlProtocolDescriptor *descriptor = 0;
233         ControlProtocolDescriptor* (*dfunc)(void);
234         const char *errstr;
235
236         if ((module = dlopen (path.c_str(), RTLD_NOW)) == 0) {
237                 error << string_compose(_("ControlProtocolManager: cannot load module \"%1\" (%2)"), path, dlerror()) << endmsg;
238                 return 0;
239         }
240
241
242         dfunc = (ControlProtocolDescriptor* (*)(void)) dlsym (module, "protocol_descriptor");
243
244         if ((errstr = dlerror()) != 0) {
245                 error << string_compose(_("ControlProtocolManager: module \"%1\" has no descriptor function."), path) << endmsg;
246                 error << errstr << endmsg;
247                 dlclose (module);
248                 return 0;
249         }
250
251         descriptor = dfunc();
252         if (descriptor) {
253                 descriptor->module = module;
254         } else {
255                 dlclose (module);
256         }
257
258         return descriptor;
259 }
260
261 void
262 ControlProtocolManager::foreach_known_protocol (sigc::slot<void,const ControlProtocolInfo*> method)
263 {
264         for (list<ControlProtocolInfo*>::iterator i = control_protocol_info.begin(); i != control_protocol_info.end(); ++i) {
265                 method (*i);
266         }
267 }
268
269 ControlProtocolInfo*
270 ControlProtocolManager::cpi_by_name (string name)
271 {
272         for (list<ControlProtocolInfo*>::iterator i = control_protocol_info.begin(); i != control_protocol_info.end(); ++i) {
273                 if (name == (*i)->name) {
274                         return *i;
275                 }
276         }
277         return 0;
278 }
279
280 int
281 ControlProtocolManager::set_state (const XMLNode& node)
282 {
283         XMLNodeList clist;
284         XMLNodeConstIterator citer;
285         XMLProperty* prop;
286
287         clist = node.children();
288
289         for (citer = clist.begin(); citer != clist.end(); ++citer) {
290                 if ((*citer)->name() == X_("Protocol")) {
291
292                         prop = (*citer)->property (X_("active"));
293
294                         if (prop && prop->value() == X_("yes")) {
295                                 if ((prop = (*citer)->property (X_("name"))) != 0) {
296                                         ControlProtocolInfo* cpi = cpi_by_name (prop->value());
297                                         if (cpi) {
298
299                                                 if (!(*citer)->children().empty()) {
300                                                         cpi->state = (*citer)->children().front ();
301                                                 } else {
302                                                         cpi->state = 0;
303                                                 }
304                                                 
305                                                 if (_session) {
306                                                         instantiate (*cpi);
307                                                 } else {
308                                                         cpi->requested = true;
309                                                 }
310                                         }
311                                 }
312                         }
313                 }    
314         }
315         return 0;
316 }
317
318 XMLNode&
319 ControlProtocolManager::get_state (void)
320 {
321         XMLNode* root = new XMLNode (state_node_name);
322         Glib::Mutex::Lock lm (protocols_lock);
323
324         for (list<ControlProtocolInfo*>::iterator i = control_protocol_info.begin(); i != control_protocol_info.end(); ++i) {
325                 XMLNode* child = new XMLNode (X_("Protocol"));
326                 child->add_property (X_("name"), (*i)->name);
327                 child->add_property (X_("active"), (*i)->protocol ? "yes" : "no");
328                 root->add_child_nocopy (*child);
329         }
330
331         return *root;
332 }
333
334 void
335 ControlProtocolManager::set_protocol_states (const XMLNode& node)
336 {
337         XMLNodeList nlist;
338         XMLNodeConstIterator niter;
339         XMLProperty* prop;
340
341         nlist = node.children();
342
343         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
344
345                 XMLNode* child = (*niter);
346
347                 if ((prop = child->property ("name")) == 0) {
348                         error << _("control protocol XML node has no name property. Ignored.") << endmsg;
349                         continue;
350                 }
351
352                 ControlProtocolInfo* cpi = cpi_by_name (prop->value());
353
354                 if (!cpi) {
355                         warning << string_compose (_("control protocol \"%1\" is not known. Ignored"), prop->value()) << endmsg;
356                         continue;
357                 }
358
359                 /* copy the node so that ownership is clear */
360
361                 cpi->state = new XMLNode (*child);
362         }
363 }