3 #include <pbd/compose.h>
5 #include <pbd/pathscanner.h>
7 #include <control_protocol/control_protocol.h>
9 #include <ardour/session.h>
10 #include <ardour/control_protocol_manager.h>
12 using namespace ARDOUR;
18 ControlProtocolManager* ControlProtocolManager::_instance = 0;
19 const string ControlProtocolManager::state_node_name = X_("ControlProtocols");
21 ControlProtocolManager::ControlProtocolManager ()
30 ControlProtocolManager::~ControlProtocolManager()
32 Glib::Mutex::Lock lm (protocols_lock);
34 for (list<ControlProtocol*>::iterator i = control_protocols.begin(); i != control_protocols.end(); ++i) {
38 control_protocols.clear ();
41 for (list<ControlProtocolInfo*>::iterator p = control_protocol_info.begin(); p != control_protocol_info.end(); ++p) {
45 control_protocol_info.clear();
49 ControlProtocolManager::set_session (Session& s)
52 _session->GoingAway.connect (mem_fun (*this, &ControlProtocolManager::drop_session));
54 for (list<ControlProtocolInfo*>::iterator i = control_protocol_info.begin(); i != control_protocol_info.end(); ++i) {
55 if ((*i)->requested || (*i)->mandatory) {
57 (*i)->requested = false;
59 if ((*i)->protocol && (*i)->state) {
60 (*i)->protocol->set_state (*(*i)->state);
67 ControlProtocolManager::drop_session ()
72 Glib::Mutex::Lock lm (protocols_lock);
73 for (list<ControlProtocol*>::iterator p = control_protocols.begin(); p != control_protocols.end(); ++p) {
76 control_protocols.clear ();
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;
86 ControlProtocolManager::instantiate (ControlProtocolInfo& cpi)
92 cpi.descriptor = get_descriptor (cpi.path);
94 if (cpi.descriptor == 0) {
95 error << string_compose (_("control protocol name \"%1\" has no descriptor"), cpi.name) << endmsg;
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;
104 Glib::Mutex::Lock lm (protocols_lock);
105 control_protocols.push_back (cpi.protocol);
111 ControlProtocolManager::teardown (ControlProtocolInfo& cpi)
117 if (!cpi.descriptor) {
125 cpi.descriptor->destroy (cpi.descriptor, cpi.protocol);
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);
133 cerr << "Programming error: ControlProtocolManager::teardown() called for " << cpi.name << ", but it was not found in control_protocols" << endl;
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);
140 cerr << "Programming error: ControlProtocolManager::teardown() called for " << cpi.name << ", but it was not found in control_protocol_info" << endl;
145 dlclose (cpi.descriptor->module);
149 static bool protocol_filter (const string& str, void *arg)
151 /* Not a dotfile, has a prefix before a period, suffix is "so", or "dylib" */
154 && ((str.length() > 3 && str.find (".so") == (str.length() - 3))
155 || (str.length() > 6 && str.find (".dylib") == (str.length() - 6)));
159 ControlProtocolManager::load_mandatory_protocols ()
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;
174 ControlProtocolManager::discover_control_protocols (string path)
176 vector<string *> *found;
179 info << string_compose (_("looking for control protocols in %1"), path) << endmsg;
181 found = scanner (path, protocol_filter, 0, false, true);
183 for (vector<string*>::iterator i = found->begin(); i != found->end(); ++i) {
184 control_protocol_discover (**i);
192 ControlProtocolManager::control_protocol_discover (string path)
194 ControlProtocolDescriptor* descriptor;
196 if ((descriptor = get_descriptor (path)) != 0) {
198 ControlProtocolInfo* cpi = new ControlProtocolInfo ();
200 if (!descriptor->probe (descriptor)) {
201 info << string_compose (_("Control protocol %1 not usable"), descriptor->name) << endmsg;
204 cpi->descriptor = descriptor;
205 cpi->name = descriptor->name;
208 cpi->requested = false;
209 cpi->mandatory = descriptor->mandatory;
210 cpi->supports_feedback = descriptor->supports_feedback;
213 control_protocol_info.push_back (cpi);
215 info << string_compose(_("Control surface protocol discovered: \"%1\""), cpi->name) << endmsg;
218 dlclose (descriptor->module);
224 ControlProtocolDescriptor*
225 ControlProtocolManager::get_descriptor (string path)
228 ControlProtocolDescriptor *descriptor = 0;
229 ControlProtocolDescriptor* (*dfunc)(void);
232 if ((module = dlopen (path.c_str(), RTLD_NOW)) == 0) {
233 error << string_compose(_("ControlProtocolManager: cannot load module \"%1\" (%2)"), path, dlerror()) << endmsg;
238 dfunc = (ControlProtocolDescriptor* (*)(void)) dlsym (module, "protocol_descriptor");
240 if ((errstr = dlerror()) != 0) {
241 error << string_compose(_("ControlProtocolManager: module \"%1\" has no descriptor function."), path) << endmsg;
242 error << errstr << endmsg;
247 descriptor = dfunc();
249 descriptor->module = module;
258 ControlProtocolManager::foreach_known_protocol (sigc::slot<void,const ControlProtocolInfo*> method)
260 for (list<ControlProtocolInfo*>::iterator i = control_protocol_info.begin(); i != control_protocol_info.end(); ++i) {
266 ControlProtocolManager::cpi_by_name (string name)
268 for (list<ControlProtocolInfo*>::iterator i = control_protocol_info.begin(); i != control_protocol_info.end(); ++i) {
269 if (name == (*i)->name) {
277 ControlProtocolManager::set_state (const XMLNode& node)
280 XMLNodeConstIterator citer;
283 clist = node.children();
285 for (citer = clist.begin(); citer != clist.end(); ++citer) {
286 if ((*citer)->name() == X_("Protocol")) {
288 prop = (*citer)->property (X_("active"));
290 if (prop && prop->value() == X_("yes")) {
291 if ((prop = (*citer)->property (X_("name"))) != 0) {
292 ControlProtocolInfo* cpi = cpi_by_name (prop->value());
294 if (!(*citer)->children().empty()) {
295 cpi->state = (*citer)->children().front ();
303 cpi->requested = true;
314 ControlProtocolManager::get_state (void)
316 XMLNode* root = new XMLNode (state_node_name);
317 Glib::Mutex::Lock lm (protocols_lock);
319 for (list<ControlProtocolInfo*>::iterator i = control_protocol_info.begin(); i != control_protocol_info.end(); ++i) {
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);
329 else if ((*i)->state) {
330 // keep ownership clear
331 root->add_child_copy (*(*i)->state);
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);
345 ControlProtocolManager::set_protocol_states (const XMLNode& node)
348 XMLNodeConstIterator niter;
351 nlist = node.children();
353 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
355 XMLNode* child = (*niter);
357 if ((prop = child->property ("name")) == 0) {
358 error << _("control protocol XML node has no name property. Ignored.") << endmsg;
362 ControlProtocolInfo* cpi = cpi_by_name (prop->value());
365 warning << string_compose (_("control protocol \"%1\" is not known. Ignored"), prop->value()) << endmsg;
369 /* copy the node so that ownership is clear */
371 cpi->state = new XMLNode (*child);