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 ();
43 ControlProtocolManager::set_session (Session& s)
46 _session->GoingAway.connect (mem_fun (*this, &ControlProtocolManager::drop_session));
48 for (list<ControlProtocolInfo*>::iterator i = control_protocol_info.begin(); i != control_protocol_info.end(); ++i) {
49 if ((*i)->requested || (*i)->mandatory) {
51 (*i)->requested = false;
53 if ((*i)->protocol && (*i)->state) {
54 (*i)->protocol->set_state (*(*i)->state);
61 ControlProtocolManager::drop_session ()
66 Glib::Mutex::Lock lm (protocols_lock);
67 for (list<ControlProtocol*>::iterator p = control_protocols.begin(); p != control_protocols.end(); ++p) {
70 control_protocols.clear ();
75 ControlProtocolManager::instantiate (ControlProtocolInfo& cpi)
81 cpi.descriptor = get_descriptor (cpi.path);
83 if (cpi.descriptor == 0) {
84 error << string_compose (_("control protocol name \"%1\" has no descriptor"), cpi.name) << endmsg;
88 if ((cpi.protocol = cpi.descriptor->initialize (cpi.descriptor, _session)) == 0) {
89 error << string_compose (_("control protocol name \"%1\" could not be initialized"), cpi.name) << endmsg;
93 Glib::Mutex::Lock lm (protocols_lock);
94 control_protocols.push_back (cpi.protocol);
97 cpi.protocol->set_state (*cpi.state);
104 ControlProtocolManager::teardown (ControlProtocolInfo& cpi)
110 if (!cpi.descriptor) {
118 cpi.descriptor->destroy (cpi.descriptor, cpi.protocol);
121 Glib::Mutex::Lock lm (protocols_lock);
122 list<ControlProtocol*>::iterator p = find (control_protocols.begin(), control_protocols.end(), cpi.protocol);
123 if (p != control_protocols.end()) {
124 control_protocols.erase (p);
129 dlclose (cpi.descriptor->module);
133 static bool protocol_filter (const string& str, void *arg)
135 /* Not a dotfile, has a prefix before a period, suffix is "so" */
137 return str[0] != '.' && (str.length() > 3 && str.find (".so") == (str.length() - 3));
141 ControlProtocolManager::load_mandatory_protocols ()
147 for (list<ControlProtocolInfo*>::iterator i = control_protocol_info.begin(); i != control_protocol_info.end(); ++i) {
148 if ((*i)->mandatory && ((*i)->protocol == 0)) {
149 info << string_compose (_("Instantiating mandatory control protocol %1"), (*i)->name) << endmsg;
156 ControlProtocolManager::discover_control_protocols (string path)
158 vector<string *> *found;
161 info << string_compose (_("looking for control protocols in %1"), path) << endmsg;
163 found = scanner (path, protocol_filter, 0, false, true);
165 for (vector<string*>::iterator i = found->begin(); i != found->end(); ++i) {
166 control_protocol_discover (**i);
174 ControlProtocolManager::control_protocol_discover (string path)
176 ControlProtocolDescriptor* descriptor;
178 if ((descriptor = get_descriptor (path)) != 0) {
180 ControlProtocolInfo* cpi = new ControlProtocolInfo ();
182 if (!descriptor->probe (descriptor)) {
183 info << string_compose (_("Control protocol %1 not usable"), descriptor->name) << endmsg;
186 cpi->descriptor = descriptor;
187 cpi->name = descriptor->name;
190 cpi->requested = false;
191 cpi->mandatory = descriptor->mandatory;
192 cpi->supports_feedback = descriptor->supports_feedback;
195 control_protocol_info.push_back (cpi);
197 info << string_compose(_("Control surface protocol discovered: \"%1\""), cpi->name) << endmsg;
200 dlclose (descriptor->module);
206 ControlProtocolDescriptor*
207 ControlProtocolManager::get_descriptor (string path)
210 ControlProtocolDescriptor *descriptor = 0;
211 ControlProtocolDescriptor* (*dfunc)(void);
214 if ((module = dlopen (path.c_str(), RTLD_NOW)) == 0) {
215 error << string_compose(_("ControlProtocolManager: cannot load module \"%1\" (%2)"), path, dlerror()) << endmsg;
220 dfunc = (ControlProtocolDescriptor* (*)(void)) dlsym (module, "protocol_descriptor");
222 if ((errstr = dlerror()) != 0) {
223 error << string_compose(_("ControlProtocolManager: module \"%1\" has no descriptor function."), path) << endmsg;
224 error << errstr << endmsg;
229 descriptor = dfunc();
231 descriptor->module = module;
240 ControlProtocolManager::foreach_known_protocol (sigc::slot<void,const ControlProtocolInfo*> method)
242 for (list<ControlProtocolInfo*>::iterator i = control_protocol_info.begin(); i != control_protocol_info.end(); ++i) {
248 ControlProtocolManager::cpi_by_name (string name)
250 for (list<ControlProtocolInfo*>::iterator i = control_protocol_info.begin(); i != control_protocol_info.end(); ++i) {
251 if (name == (*i)->name) {
259 ControlProtocolManager::set_state (const XMLNode& node)
262 XMLNodeConstIterator citer;
265 clist = node.children();
267 for (citer = clist.begin(); citer != clist.end(); ++citer) {
268 if ((*citer)->name() == X_("Protocol")) {
270 prop = (*citer)->property (X_("active"));
272 if (prop && prop->value() == X_("yes")) {
273 if ((prop = (*citer)->property (X_("name"))) != 0) {
274 ControlProtocolInfo* cpi = cpi_by_name (prop->value());
277 if (!(*citer)->children().empty()) {
278 cpi->state = (*citer)->children().front ();
286 cpi->requested = true;
297 ControlProtocolManager::get_state (void)
299 XMLNode* root = new XMLNode (state_node_name);
300 Glib::Mutex::Lock lm (protocols_lock);
302 for (list<ControlProtocolInfo*>::iterator i = control_protocol_info.begin(); i != control_protocol_info.end(); ++i) {
303 XMLNode* child = new XMLNode (X_("Protocol"));
304 child->add_property (X_("name"), (*i)->name);
305 child->add_property (X_("active"), (*i)->protocol ? "yes" : "no");
306 root->add_child_nocopy (*child);
313 ControlProtocolManager::set_protocol_states (const XMLNode& node)
316 XMLNodeConstIterator niter;
319 nlist = node.children();
321 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
323 XMLNode* child = (*niter);
325 if ((prop = child->property ("name")) == 0) {
326 error << _("control protocol XML node has no name property. Ignored.") << endmsg;
330 ControlProtocolInfo* cpi = cpi_by_name (prop->value());
333 warning << string_compose (_("control protocol \"%1\" is not known. Ignored"), prop->value()) << endmsg;
337 /* copy the node so that ownership is clear */
339 cpi->state = new XMLNode (*child);