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/file_utils.h"
24 #include "pbd/error.h"
26 #include "control_protocol/control_protocol.h"
28 #include "ardour/session.h"
29 #include "ardour/control_protocol_manager.h"
30 #include "ardour/control_protocol_search_path.h"
32 using namespace ARDOUR;
38 ControlProtocolManager* ControlProtocolManager::_instance = 0;
39 const string ControlProtocolManager::state_node_name = X_("ControlProtocols");
41 ControlProtocolManager::ControlProtocolManager ()
47 ControlProtocolManager::~ControlProtocolManager()
49 Glib::Mutex::Lock lm (protocols_lock);
51 for (list<ControlProtocol*>::iterator i = control_protocols.begin(); i != control_protocols.end(); ++i) {
55 control_protocols.clear ();
58 for (list<ControlProtocolInfo*>::iterator p = control_protocol_info.begin(); p != control_protocol_info.end(); ++p) {
62 control_protocol_info.clear();
66 ControlProtocolManager::set_session (Session& s)
69 _session->GoingAway.connect (mem_fun (*this, &ControlProtocolManager::drop_session));
71 for (list<ControlProtocolInfo*>::iterator i = control_protocol_info.begin(); i != control_protocol_info.end(); ++i) {
72 if ((*i)->requested || (*i)->mandatory) {
74 (*i)->requested = false;
76 if ((*i)->protocol && (*i)->state) {
77 (*i)->protocol->set_state (*(*i)->state, Stateful::loading_state_version);
84 ControlProtocolManager::drop_session ()
89 Glib::Mutex::Lock lm (protocols_lock);
90 for (list<ControlProtocol*>::iterator p = control_protocols.begin(); p != control_protocols.end(); ++p) {
93 control_protocols.clear ();
95 for (list<ControlProtocolInfo*>::iterator p = control_protocol_info.begin(); p != control_protocol_info.end(); ++p) {
96 // otherwise the ControlProtocol instances are not recreated in set_session
98 (*p)->requested = true;
106 ControlProtocolManager::instantiate (ControlProtocolInfo& cpi)
112 cpi.descriptor = get_descriptor (cpi.path);
114 if (cpi.descriptor == 0) {
115 error << string_compose (_("control protocol name \"%1\" has no descriptor"), cpi.name) << endmsg;
119 if ((cpi.protocol = cpi.descriptor->initialize (cpi.descriptor, _session)) == 0) {
120 error << string_compose (_("control protocol name \"%1\" could not be initialized"), cpi.name) << endmsg;
125 Glib::Mutex::Lock lm (protocols_lock);
126 control_protocols.push_back (cpi.protocol);
132 ControlProtocolManager::teardown (ControlProtocolInfo& cpi)
138 if (!cpi.descriptor) {
146 cpi.descriptor->destroy (cpi.descriptor, cpi.protocol);
149 Glib::Mutex::Lock lm (protocols_lock);
150 list<ControlProtocol*>::iterator p = find (control_protocols.begin(), control_protocols.end(), cpi.protocol);
151 if (p != control_protocols.end()) {
152 control_protocols.erase (p);
154 cerr << "Programming error: ControlProtocolManager::teardown() called for " << cpi.name << ", but it was not found in control_protocols" << endl;
159 dlclose (cpi.descriptor->module);
164 ControlProtocolManager::load_mandatory_protocols ()
170 for (list<ControlProtocolInfo*>::iterator i = control_protocol_info.begin(); i != control_protocol_info.end(); ++i) {
171 if ((*i)->mandatory && ((*i)->protocol == 0)) {
172 info << string_compose (_("Instantiating mandatory control protocol %1"), (*i)->name) << endmsg;
179 ControlProtocolManager::discover_control_protocols ()
181 vector<sys::path> cp_modules;
183 Glib::PatternSpec so_extension_pattern("*.so");
184 Glib::PatternSpec dylib_extension_pattern("*.dylib");
186 find_matching_files_in_search_path (control_protocol_search_path (),
187 so_extension_pattern, cp_modules);
189 find_matching_files_in_search_path (control_protocol_search_path (),
190 dylib_extension_pattern, cp_modules);
192 info << string_compose (_("looking for control protocols in %1"), control_protocol_search_path().to_string()) << endmsg;
194 for (vector<sys::path>::iterator i = cp_modules.begin(); i != cp_modules.end(); ++i) {
195 control_protocol_discover ((*i).to_string());
200 ControlProtocolManager::control_protocol_discover (string path)
202 ControlProtocolDescriptor* descriptor;
204 if ((descriptor = get_descriptor (path)) != 0) {
206 ControlProtocolInfo* cpi = new ControlProtocolInfo ();
208 if (!descriptor->probe (descriptor)) {
209 info << string_compose (_("Control protocol %1 not usable"), descriptor->name) << endmsg;
212 cpi->descriptor = descriptor;
213 cpi->name = descriptor->name;
216 cpi->requested = false;
217 cpi->mandatory = descriptor->mandatory;
218 cpi->supports_feedback = descriptor->supports_feedback;
221 control_protocol_info.push_back (cpi);
223 info << string_compose(_("Control surface protocol discovered: \"%1\""), cpi->name) << endmsg;
226 dlclose (descriptor->module);
232 ControlProtocolDescriptor*
233 ControlProtocolManager::get_descriptor (string path)
236 ControlProtocolDescriptor *descriptor = 0;
237 ControlProtocolDescriptor* (*dfunc)(void);
240 if ((module = dlopen (path.c_str(), RTLD_NOW)) == 0) {
241 error << string_compose(_("ControlProtocolManager: cannot load module \"%1\" (%2)"), path, dlerror()) << endmsg;
246 dfunc = (ControlProtocolDescriptor* (*)(void)) dlsym (module, "protocol_descriptor");
248 if ((errstr = dlerror()) != 0) {
249 error << string_compose(_("ControlProtocolManager: module \"%1\" has no descriptor function."), path) << endmsg;
250 error << errstr << endmsg;
255 descriptor = dfunc();
257 descriptor->module = module;
266 ControlProtocolManager::foreach_known_protocol (sigc::slot<void,const ControlProtocolInfo*> method)
268 for (list<ControlProtocolInfo*>::iterator i = control_protocol_info.begin(); i != control_protocol_info.end(); ++i) {
274 ControlProtocolManager::cpi_by_name (string name)
276 for (list<ControlProtocolInfo*>::iterator i = control_protocol_info.begin(); i != control_protocol_info.end(); ++i) {
277 if (name == (*i)->name) {
285 ControlProtocolManager::set_state (const XMLNode& node, int /*version*/)
288 XMLNodeConstIterator citer;
291 clist = node.children();
293 for (citer = clist.begin(); citer != clist.end(); ++citer) {
294 if ((*citer)->name() == X_("Protocol")) {
296 prop = (*citer)->property (X_("active"));
298 if (prop && string_is_affirmative (prop->value())) {
299 if ((prop = (*citer)->property (X_("name"))) != 0) {
300 ControlProtocolInfo* cpi = cpi_by_name (prop->value());
302 if (!(*citer)->children().empty()) {
303 cpi->state = (*citer)->children().front ();
311 cpi->requested = true;
322 ControlProtocolManager::get_state (void)
324 XMLNode* root = new XMLNode (state_node_name);
325 Glib::Mutex::Lock lm (protocols_lock);
327 for (list<ControlProtocolInfo*>::iterator i = control_protocol_info.begin(); i != control_protocol_info.end(); ++i) {
331 if ((*i)->protocol) {
332 child = &((*i)->protocol->get_state());
333 child->add_property (X_("active"), "yes");
334 // should we update (*i)->state here? probably.
335 root->add_child_nocopy (*child);
337 else if ((*i)->state) {
338 // keep ownership clear
339 root->add_child_copy (*(*i)->state);
342 child = new XMLNode (X_("Protocol"));
343 child->add_property (X_("name"), (*i)->name);
344 child->add_property (X_("active"), "no");
345 root->add_child_nocopy (*child);
353 ControlProtocolManager::set_protocol_states (const XMLNode& node)
356 XMLNodeConstIterator niter;
359 nlist = node.children();
361 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
363 XMLNode* child = (*niter);
365 if ((prop = child->property ("name")) == 0) {
366 error << _("control protocol XML node has no name property. Ignored.") << endmsg;
370 ControlProtocolInfo* cpi = cpi_by_name (prop->value());
373 warning << string_compose (_("control protocol \"%1\" is not known. Ignored"), prop->value()) << endmsg;
377 /* copy the node so that ownership is clear */
379 cpi->state = new XMLNode (*child);
383 ControlProtocolManager&
384 ControlProtocolManager::instance ()
386 if (_instance == 0) {
387 _instance = new ControlProtocolManager ();