2 Copyright (C) 2000-2007 Paul Davis
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include <pbd/compose.h>
23 #include <pbd/error.h>
24 #include <pbd/pathscanner.h>
26 #include <control_protocol/control_protocol.h>
28 #include <ardour/session.h>
29 #include <ardour/control_protocol_manager.h>
31 using namespace ARDOUR;
37 ControlProtocolManager* ControlProtocolManager::_instance = 0;
38 const string ControlProtocolManager::state_node_name = X_("ControlProtocols");
40 ControlProtocolManager::ControlProtocolManager ()
49 ControlProtocolManager::~ControlProtocolManager()
51 Glib::Mutex::Lock lm (protocols_lock);
53 for (list<ControlProtocol*>::iterator i = control_protocols.begin(); i != control_protocols.end(); ++i) {
57 control_protocols.clear ();
60 for (list<ControlProtocolInfo*>::iterator p = control_protocol_info.begin(); p != control_protocol_info.end(); ++p) {
64 control_protocol_info.clear();
68 ControlProtocolManager::set_session (Session& s)
71 _session->GoingAway.connect (mem_fun (*this, &ControlProtocolManager::drop_session));
73 for (list<ControlProtocolInfo*>::iterator i = control_protocol_info.begin(); i != control_protocol_info.end(); ++i) {
74 if ((*i)->requested || (*i)->mandatory) {
76 (*i)->requested = false;
78 if ((*i)->protocol && (*i)->state) {
79 (*i)->protocol->set_state (*(*i)->state);
86 ControlProtocolManager::drop_session ()
91 Glib::Mutex::Lock lm (protocols_lock);
92 for (list<ControlProtocol*>::iterator p = control_protocols.begin(); p != control_protocols.end(); ++p) {
95 control_protocols.clear ();
97 for (list<ControlProtocolInfo*>::iterator p = control_protocol_info.begin(); p != control_protocol_info.end(); ++p) {
98 // otherwise the ControlProtocol instances are not recreated in set_session
100 (*p)->requested = true;
108 ControlProtocolManager::instantiate (ControlProtocolInfo& cpi)
114 cpi.descriptor = get_descriptor (cpi.path);
116 if (cpi.descriptor == 0) {
117 error << string_compose (_("control protocol name \"%1\" has no descriptor"), cpi.name) << endmsg;
121 if ((cpi.protocol = cpi.descriptor->initialize (cpi.descriptor, _session)) == 0) {
122 error << string_compose (_("control protocol name \"%1\" could not be initialized"), cpi.name) << endmsg;
126 Glib::Mutex::Lock lm (protocols_lock);
127 control_protocols.push_back (cpi.protocol);
133 ControlProtocolManager::teardown (ControlProtocolInfo& cpi)
139 if (!cpi.descriptor) {
147 cpi.descriptor->destroy (cpi.descriptor, cpi.protocol);
150 Glib::Mutex::Lock lm (protocols_lock);
151 list<ControlProtocol*>::iterator p = find (control_protocols.begin(), control_protocols.end(), cpi.protocol);
152 if (p != control_protocols.end()) {
153 control_protocols.erase (p);
155 cerr << "Programming error: ControlProtocolManager::teardown() called for " << cpi.name << ", but it was not found in control_protocols" << endl;
158 list<ControlProtocolInfo*>::iterator p2 = find (control_protocol_info.begin(), control_protocol_info.end(), &cpi);
159 if (p2 != control_protocol_info.end()) {
160 control_protocol_info.erase (p2);
162 cerr << "Programming error: ControlProtocolManager::teardown() called for " << cpi.name << ", but it was not found in control_protocol_info" << endl;
167 dlclose (cpi.descriptor->module);
171 static bool protocol_filter (const string& str, void *arg)
173 /* Not a dotfile, has a prefix before a period, suffix is "so", or "dylib" */
176 && ((str.length() > 3 && str.find (".so") == (str.length() - 3))
177 || (str.length() > 6 && str.find (".dylib") == (str.length() - 6)));
181 ControlProtocolManager::load_mandatory_protocols ()
187 for (list<ControlProtocolInfo*>::iterator i = control_protocol_info.begin(); i != control_protocol_info.end(); ++i) {
188 if ((*i)->mandatory && ((*i)->protocol == 0)) {
189 info << string_compose (_("Instantiating mandatory control protocol %1"), (*i)->name) << endmsg;
196 ControlProtocolManager::discover_control_protocols (string path)
198 vector<string *> *found;
201 info << string_compose (_("looking for control protocols in %1"), path) << endmsg;
203 found = scanner (path, protocol_filter, 0, false, true);
205 for (vector<string*>::iterator i = found->begin(); i != found->end(); ++i) {
206 control_protocol_discover (**i);
214 ControlProtocolManager::control_protocol_discover (string path)
216 ControlProtocolDescriptor* descriptor;
218 if ((descriptor = get_descriptor (path)) != 0) {
220 ControlProtocolInfo* cpi = new ControlProtocolInfo ();
222 if (!descriptor->probe (descriptor)) {
223 info << string_compose (_("Control protocol %1 not usable"), descriptor->name) << endmsg;
226 cpi->descriptor = descriptor;
227 cpi->name = descriptor->name;
230 cpi->requested = false;
231 cpi->mandatory = descriptor->mandatory;
232 cpi->supports_feedback = descriptor->supports_feedback;
235 control_protocol_info.push_back (cpi);
237 info << string_compose(_("Control surface protocol discovered: \"%1\""), cpi->name) << endmsg;
240 dlclose (descriptor->module);
246 ControlProtocolDescriptor*
247 ControlProtocolManager::get_descriptor (string path)
250 ControlProtocolDescriptor *descriptor = 0;
251 ControlProtocolDescriptor* (*dfunc)(void);
254 if ((module = dlopen (path.c_str(), RTLD_NOW)) == 0) {
255 error << string_compose(_("ControlProtocolManager: cannot load module \"%1\" (%2)"), path, dlerror()) << endmsg;
260 dfunc = (ControlProtocolDescriptor* (*)(void)) dlsym (module, "protocol_descriptor");
262 if ((errstr = dlerror()) != 0) {
263 error << string_compose(_("ControlProtocolManager: module \"%1\" has no descriptor function."), path) << endmsg;
264 error << errstr << endmsg;
269 descriptor = dfunc();
271 descriptor->module = module;
280 ControlProtocolManager::foreach_known_protocol (sigc::slot<void,const ControlProtocolInfo*> method)
282 for (list<ControlProtocolInfo*>::iterator i = control_protocol_info.begin(); i != control_protocol_info.end(); ++i) {
288 ControlProtocolManager::cpi_by_name (string name)
290 for (list<ControlProtocolInfo*>::iterator i = control_protocol_info.begin(); i != control_protocol_info.end(); ++i) {
291 if (name == (*i)->name) {
299 ControlProtocolManager::set_state (const XMLNode& node)
302 XMLNodeConstIterator citer;
305 clist = node.children();
307 for (citer = clist.begin(); citer != clist.end(); ++citer) {
308 if ((*citer)->name() == X_("Protocol")) {
310 prop = (*citer)->property (X_("active"));
312 if (prop && prop->value() == X_("yes")) {
313 if ((prop = (*citer)->property (X_("name"))) != 0) {
314 ControlProtocolInfo* cpi = cpi_by_name (prop->value());
316 if (!(*citer)->children().empty()) {
317 cpi->state = (*citer)->children().front ();
325 cpi->requested = true;
336 ControlProtocolManager::get_state (void)
338 XMLNode* root = new XMLNode (state_node_name);
339 Glib::Mutex::Lock lm (protocols_lock);
341 for (list<ControlProtocolInfo*>::iterator i = control_protocol_info.begin(); i != control_protocol_info.end(); ++i) {
345 if ((*i)->protocol) {
346 child = &((*i)->protocol->get_state());
347 child->add_property (X_("active"), "yes");
348 // should we update (*i)->state here? probably.
349 root->add_child_nocopy (*child);
351 else if ((*i)->state) {
352 // keep ownership clear
353 root->add_child_copy (*(*i)->state);
356 child = new XMLNode (X_("Protocol"));
357 child->add_property (X_("name"), (*i)->name);
358 child->add_property (X_("active"), "no");
359 root->add_child_nocopy (*child);
367 ControlProtocolManager::set_protocol_states (const XMLNode& node)
370 XMLNodeConstIterator niter;
373 nlist = node.children();
375 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
377 XMLNode* child = (*niter);
379 if ((prop = child->property ("name")) == 0) {
380 error << _("control protocol XML node has no name property. Ignored.") << endmsg;
384 ControlProtocolInfo* cpi = cpi_by_name (prop->value());
387 warning << string_compose (_("control protocol \"%1\" is not known. Ignored"), prop->value()) << endmsg;
391 /* copy the node so that ownership is clear */
393 cpi->state = new XMLNode (*child);