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();
50 ControlProtocolManager::set_session (Session& s)
53 _session->GoingAway.connect (mem_fun (*this, &ControlProtocolManager::drop_session));
55 for (list<ControlProtocolInfo*>::iterator i = control_protocol_info.begin(); i != control_protocol_info.end(); ++i) {
56 if ((*i)->requested || (*i)->mandatory) {
58 (*i)->requested = false;
60 if ((*i)->protocol && (*i)->state) {
61 (*i)->protocol->set_state (*(*i)->state);
68 ControlProtocolManager::drop_session ()
73 Glib::Mutex::Lock lm (protocols_lock);
74 for (list<ControlProtocol*>::iterator p = control_protocols.begin(); p != control_protocols.end(); ++p) {
77 control_protocols.clear ();
79 for (list<ControlProtocolInfo*>::iterator p = control_protocol_info.begin(); p != control_protocol_info.end(); ++p) {
83 control_protocol_info.clear();
88 ControlProtocolManager::instantiate (ControlProtocolInfo& cpi)
94 cpi.descriptor = get_descriptor (cpi.path);
96 if (cpi.descriptor == 0) {
97 error << string_compose (_("control protocol name \"%1\" has no descriptor"), cpi.name) << endmsg;
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;
106 Glib::Mutex::Lock lm (protocols_lock);
107 control_protocols.push_back (cpi.protocol);
110 cpi.protocol->set_state (*cpi.state);
117 ControlProtocolManager::teardown (ControlProtocolInfo& cpi)
123 if (!cpi.descriptor) {
131 cpi.descriptor->destroy (cpi.descriptor, cpi.protocol);
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);
139 cerr << "Programming error: ControlProtocolManager::teardown() called for " << cpi.name << ", but it was not found in control_protocols" << endl;
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);
146 cerr << "Programming error: ControlProtocolManager::teardown() called for " << cpi.name << ", but it was not found in control_protocol_info" << endl;
151 dlclose (cpi.descriptor->module);
155 static bool protocol_filter (const string& str, void *arg)
157 /* Not a dotfile, has a prefix before a period, suffix is "so", or "dylib" */
160 && ((str.length() > 3 && str.find (".so") == (str.length() - 3))
161 || (str.length() > 6 && str.find (".dylib") == (str.length() - 6)));
165 ControlProtocolManager::load_mandatory_protocols ()
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;
180 ControlProtocolManager::discover_control_protocols (string path)
182 vector<string *> *found;
185 info << string_compose (_("looking for control protocols in %1"), path) << endmsg;
187 found = scanner (path, protocol_filter, 0, false, true);
189 for (vector<string*>::iterator i = found->begin(); i != found->end(); ++i) {
190 control_protocol_discover (**i);
198 ControlProtocolManager::control_protocol_discover (string path)
200 ControlProtocolDescriptor* descriptor;
202 if ((descriptor = get_descriptor (path)) != 0) {
204 ControlProtocolInfo* cpi = new ControlProtocolInfo ();
206 if (!descriptor->probe (descriptor)) {
207 info << string_compose (_("Control protocol %1 not usable"), descriptor->name) << endmsg;
210 cpi->descriptor = descriptor;
211 cpi->name = descriptor->name;
214 cpi->requested = false;
215 cpi->mandatory = descriptor->mandatory;
216 cpi->supports_feedback = descriptor->supports_feedback;
219 control_protocol_info.push_back (cpi);
221 info << string_compose(_("Control surface protocol discovered: \"%1\""), cpi->name) << endmsg;
224 dlclose (descriptor->module);
230 ControlProtocolDescriptor*
231 ControlProtocolManager::get_descriptor (string path)
234 ControlProtocolDescriptor *descriptor = 0;
235 ControlProtocolDescriptor* (*dfunc)(void);
238 if ((module = dlopen (path.c_str(), RTLD_NOW)) == 0) {
239 error << string_compose(_("ControlProtocolManager: cannot load module \"%1\" (%2)"), path, dlerror()) << endmsg;
244 dfunc = (ControlProtocolDescriptor* (*)(void)) dlsym (module, "protocol_descriptor");
246 if ((errstr = dlerror()) != 0) {
247 error << string_compose(_("ControlProtocolManager: module \"%1\" has no descriptor function."), path) << endmsg;
248 error << errstr << endmsg;
253 descriptor = dfunc();
255 descriptor->module = module;
264 ControlProtocolManager::foreach_known_protocol (sigc::slot<void,const ControlProtocolInfo*> method)
266 for (list<ControlProtocolInfo*>::iterator i = control_protocol_info.begin(); i != control_protocol_info.end(); ++i) {
272 ControlProtocolManager::cpi_by_name (string name)
274 for (list<ControlProtocolInfo*>::iterator i = control_protocol_info.begin(); i != control_protocol_info.end(); ++i) {
275 if (name == (*i)->name) {
283 ControlProtocolManager::set_state (const XMLNode& node)
286 XMLNodeConstIterator citer;
289 clist = node.children();
291 for (citer = clist.begin(); citer != clist.end(); ++citer) {
292 if ((*citer)->name() == X_("Protocol")) {
294 prop = (*citer)->property (X_("active"));
296 if (prop && prop->value() == X_("yes")) {
297 if ((prop = (*citer)->property (X_("name"))) != 0) {
298 ControlProtocolInfo* cpi = cpi_by_name (prop->value());
301 if (!(*citer)->children().empty()) {
302 cpi->state = (*citer)->children().front ();
310 cpi->requested = true;
321 ControlProtocolManager::get_state (void)
323 XMLNode* root = new XMLNode (state_node_name);
324 Glib::Mutex::Lock lm (protocols_lock);
326 for (list<ControlProtocolInfo*>::iterator i = control_protocol_info.begin(); i != control_protocol_info.end(); ++i) {
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);
336 else if ((*i)->state) {
337 // keep ownership clear
338 root->add_child_copy (*(*i)->state);
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);
352 ControlProtocolManager::set_protocol_states (const XMLNode& node)
355 XMLNodeConstIterator niter;
358 nlist = node.children();
360 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
362 XMLNode* child = (*niter);
364 if ((prop = child->property ("name")) == 0) {
365 error << _("control protocol XML node has no name property. Ignored.") << endmsg;
369 ControlProtocolInfo* cpi = cpi_by_name (prop->value());
372 warning << string_compose (_("control protocol \"%1\" is not known. Ignored"), prop->value()) << endmsg;
376 /* copy the node so that ownership is clear */
378 cpi->state = new XMLNode (*child);