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;
194 control_protocol_info.push_back (cpi);
196 info << string_compose(_("Control surface protocol discovered: \"%1\""), cpi->name) << endmsg;
199 dlclose (descriptor->module);
205 ControlProtocolDescriptor*
206 ControlProtocolManager::get_descriptor (string path)
209 ControlProtocolDescriptor *descriptor = 0;
210 ControlProtocolDescriptor* (*dfunc)(void);
213 if ((module = dlopen (path.c_str(), RTLD_NOW)) == 0) {
214 error << string_compose(_("ControlProtocolManager: cannot load module \"%1\" (%2)"), path, dlerror()) << endmsg;
219 dfunc = (ControlProtocolDescriptor* (*)(void)) dlsym (module, "protocol_descriptor");
221 if ((errstr = dlerror()) != 0) {
222 error << string_compose(_("ControlProtocolManager: module \"%1\" has no descriptor function."), path) << endmsg;
223 error << errstr << endmsg;
228 descriptor = dfunc();
230 descriptor->module = module;
239 ControlProtocolManager::foreach_known_protocol (sigc::slot<void,const ControlProtocolInfo*> method)
241 for (list<ControlProtocolInfo*>::iterator i = control_protocol_info.begin(); i != control_protocol_info.end(); ++i) {
247 ControlProtocolManager::cpi_by_name (string name)
249 for (list<ControlProtocolInfo*>::iterator i = control_protocol_info.begin(); i != control_protocol_info.end(); ++i) {
250 if (name == (*i)->name) {
258 ControlProtocolManager::set_state (const XMLNode& node)
261 XMLNodeConstIterator citer;
264 clist = node.children();
266 for (citer = clist.begin(); citer != clist.end(); ++citer) {
267 if ((*citer)->name() == X_("Protocol")) {
269 prop = (*citer)->property (X_("active"));
271 if (prop && prop->value() == X_("yes")) {
272 if ((prop = (*citer)->property (X_("name"))) != 0) {
273 ControlProtocolInfo* cpi = cpi_by_name (prop->value());
276 if (!(*citer)->children().empty()) {
277 cpi->state = (*citer)->children().front ();
285 cpi->requested = true;
296 ControlProtocolManager::get_state (void)
298 XMLNode* root = new XMLNode (state_node_name);
299 Glib::Mutex::Lock lm (protocols_lock);
301 for (list<ControlProtocolInfo*>::iterator i = control_protocol_info.begin(); i != control_protocol_info.end(); ++i) {
302 XMLNode* child = new XMLNode (X_("Protocol"));
303 child->add_property (X_("name"), (*i)->name);
304 child->add_property (X_("active"), (*i)->protocol ? "yes" : "no");
305 root->add_child_nocopy (*child);
312 ControlProtocolManager::set_protocol_states (const XMLNode& node)
315 XMLNodeConstIterator niter;
318 nlist = node.children();
320 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
322 XMLNode* child = (*niter);
324 if ((prop = child->property ("name")) == 0) {
325 error << _("control protocol XML node has no name property. Ignored.") << endmsg;
329 ControlProtocolInfo* cpi = cpi_by_name (prop->value());
332 warning << string_compose (_("control protocol \"%1\" is not known. Ignored"), prop->value()) << endmsg;
336 /* copy the node so that ownership is clear */
338 cpi->state = new XMLNode (*child);