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 <glibmm/fileutils.h>
24 #include "pbd/compose.h"
25 #include "pbd/file_utils.h"
26 #include "pbd/error.h"
28 #include "control_protocol/control_protocol.h"
30 #include "ardour/debug.h"
31 #include "ardour/session.h"
32 #include "ardour/control_protocol_manager.h"
33 #include "ardour/control_protocol_search_path.h"
35 using namespace ARDOUR;
41 ControlProtocolManager* ControlProtocolManager::_instance = 0;
42 const string ControlProtocolManager::state_node_name = X_("ControlProtocols");
44 ControlProtocolManager::ControlProtocolManager ()
48 ControlProtocolManager::~ControlProtocolManager()
50 Glib::Mutex::Lock lm (protocols_lock);
52 for (list<ControlProtocol*>::iterator i = control_protocols.begin(); i != control_protocols.end(); ++i) {
56 control_protocols.clear ();
59 for (list<ControlProtocolInfo*>::iterator p = control_protocol_info.begin(); p != control_protocol_info.end(); ++p) {
63 control_protocol_info.clear();
67 ControlProtocolManager::set_session (Session* s)
69 SessionHandlePtr::set_session (s);
72 Glib::Mutex::Lock lm (protocols_lock);
74 for (list<ControlProtocolInfo*>::iterator i = control_protocol_info.begin(); i != control_protocol_info.end(); ++i) {
75 if ((*i)->requested || (*i)->mandatory) {
77 (*i)->requested = false;
81 (*i)->protocol->set_state (*(*i)->state, Stateful::loading_state_version);
83 /* guarantee a call to
84 set_state() whether we have
87 (*i)->protocol->set_state (XMLNode(""), Stateful::loading_state_version);
96 ControlProtocolManager::session_going_away()
98 SessionHandlePtr::session_going_away ();
101 Glib::Mutex::Lock lm (protocols_lock);
103 for (list<ControlProtocol*>::iterator p = control_protocols.begin(); p != control_protocols.end(); ++p) {
107 control_protocols.clear ();
109 for (list<ControlProtocolInfo*>::iterator p = control_protocol_info.begin(); p != control_protocol_info.end(); ++p) {
110 // mark existing protocols as requested
111 // otherwise the ControlProtocol instances are not recreated in set_session
112 if ((*p)->protocol) {
113 (*p)->requested = true;
121 ControlProtocolManager::instantiate (ControlProtocolInfo& cpi)
123 /* CALLER MUST HOLD LOCK */
129 cpi.descriptor = get_descriptor (cpi.path);
131 if (cpi.descriptor == 0) {
132 error << string_compose (_("control protocol name \"%1\" has no descriptor"), cpi.name) << endmsg;
136 if ((cpi.protocol = cpi.descriptor->initialize (cpi.descriptor, _session)) == 0) {
137 error << string_compose (_("control protocol name \"%1\" could not be initialized"), cpi.name) << endmsg;
141 control_protocols.push_back (cpi.protocol);
147 ControlProtocolManager::teardown (ControlProtocolInfo& cpi)
153 if (!cpi.descriptor) {
161 cpi.descriptor->destroy (cpi.descriptor, cpi.protocol);
164 Glib::Mutex::Lock lm (protocols_lock);
165 list<ControlProtocol*>::iterator p = find (control_protocols.begin(), control_protocols.end(), cpi.protocol);
166 if (p != control_protocols.end()) {
167 control_protocols.erase (p);
169 cerr << "Programming error: ControlProtocolManager::teardown() called for " << cpi.name << ", but it was not found in control_protocols" << endl;
176 dlclose (cpi.descriptor->module);
181 ControlProtocolManager::load_mandatory_protocols ()
187 Glib::Mutex::Lock lm (protocols_lock);
189 for (list<ControlProtocolInfo*>::iterator i = control_protocol_info.begin(); i != control_protocol_info.end(); ++i) {
190 if ((*i)->mandatory && ((*i)->protocol == 0)) {
191 DEBUG_TRACE (DEBUG::ControlProtocols,
192 string_compose (_("Instantiating mandatory control protocol %1"), (*i)->name));
199 ControlProtocolManager::discover_control_protocols ()
201 vector<sys::path> cp_modules;
203 Glib::PatternSpec so_extension_pattern("*.so");
204 Glib::PatternSpec dylib_extension_pattern("*.dylib");
206 find_matching_files_in_search_path (control_protocol_search_path (),
207 so_extension_pattern, cp_modules);
209 find_matching_files_in_search_path (control_protocol_search_path (),
210 dylib_extension_pattern, cp_modules);
212 DEBUG_TRACE (DEBUG::ControlProtocols,
213 string_compose (_("looking for control protocols in %1\n"), control_protocol_search_path().to_string()));
215 for (vector<sys::path>::iterator i = cp_modules.begin(); i != cp_modules.end(); ++i) {
216 control_protocol_discover ((*i).to_string());
221 ControlProtocolManager::control_protocol_discover (string path)
223 ControlProtocolDescriptor* descriptor;
226 /* don't load OS X shared objects that are just symlinks to the real thing.
229 if (path.find (".dylib") && Glib::file_test (path, Glib::FILE_TEST_IS_SYMLINK)) {
234 if ((descriptor = get_descriptor (path)) != 0) {
236 if (!descriptor->probe (descriptor)) {
237 DEBUG_TRACE (DEBUG::ControlProtocols,
238 string_compose (_("Control protocol %1 not usable"), descriptor->name));
241 ControlProtocolInfo* cpi = new ControlProtocolInfo ();
243 cpi->descriptor = descriptor;
244 cpi->name = descriptor->name;
247 cpi->requested = false;
248 cpi->mandatory = descriptor->mandatory;
249 cpi->supports_feedback = descriptor->supports_feedback;
252 control_protocol_info.push_back (cpi);
254 DEBUG_TRACE (DEBUG::ControlProtocols,
255 string_compose(_("Control surface protocol discovered: \"%1\""), cpi->name));
258 dlclose (descriptor->module);
264 ControlProtocolDescriptor*
265 ControlProtocolManager::get_descriptor (string path)
268 ControlProtocolDescriptor *descriptor = 0;
269 ControlProtocolDescriptor* (*dfunc)(void);
272 if ((module = dlopen (path.c_str(), RTLD_NOW)) == 0) {
273 error << string_compose(_("ControlProtocolManager: cannot load module \"%1\" (%2)"), path, dlerror()) << endmsg;
278 dfunc = (ControlProtocolDescriptor* (*)(void)) dlsym (module, "protocol_descriptor");
280 if ((errstr = dlerror()) != 0) {
281 error << string_compose(_("ControlProtocolManager: module \"%1\" has no descriptor function."), path) << endmsg;
282 error << errstr << endmsg;
287 descriptor = dfunc();
289 descriptor->module = module;
298 ControlProtocolManager::foreach_known_protocol (boost::function<void(const ControlProtocolInfo*)> method)
300 for (list<ControlProtocolInfo*>::iterator i = control_protocol_info.begin(); i != control_protocol_info.end(); ++i) {
306 ControlProtocolManager::cpi_by_name (string name)
308 for (list<ControlProtocolInfo*>::iterator i = control_protocol_info.begin(); i != control_protocol_info.end(); ++i) {
309 if (name == (*i)->name) {
317 ControlProtocolManager::set_state (const XMLNode& node, int /*version*/)
320 XMLNodeConstIterator citer;
323 Glib::Mutex::Lock lm (protocols_lock);
325 clist = node.children();
327 for (citer = clist.begin(); citer != clist.end(); ++citer) {
328 if ((*citer)->name() == X_("Protocol")) {
330 prop = (*citer)->property (X_("active"));
332 if (prop && string_is_affirmative (prop->value())) {
333 if ((prop = (*citer)->property (X_("name"))) != 0) {
334 ControlProtocolInfo* cpi = cpi_by_name (prop->value());
342 cpi->state = new XMLNode (**citer);
347 cpi->requested = true;
358 ControlProtocolManager::get_state ()
360 XMLNode* root = new XMLNode (state_node_name);
361 Glib::Mutex::Lock lm (protocols_lock);
363 for (list<ControlProtocolInfo*>::iterator i = control_protocol_info.begin(); i != control_protocol_info.end(); ++i) {
367 if ((*i)->protocol) {
368 child = &((*i)->protocol->get_state());
369 child->add_property (X_("active"), "yes");
370 // should we update (*i)->state here? probably.
371 root->add_child_nocopy (*child);
372 } else if ((*i)->state) {
373 // keep ownership clear
374 root->add_child_copy (*(*i)->state);
376 child = new XMLNode (X_("Protocol"));
377 child->add_property (X_("name"), (*i)->name);
378 child->add_property (X_("active"), "no");
379 root->add_child_nocopy (*child);
387 ControlProtocolManager::set_protocol_states (const XMLNode& node)
390 XMLNodeConstIterator niter;
393 nlist = node.children();
395 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
397 XMLNode* child = (*niter);
399 if ((prop = child->property ("name")) == 0) {
400 error << _("control protocol XML node has no name property. Ignored.") << endmsg;
404 ControlProtocolInfo* cpi = cpi_by_name (prop->value());
407 warning << string_compose (_("control protocol \"%1\" is not known. Ignored"), prop->value()) << endmsg;
411 /* copy the node so that ownership is clear */
413 cpi->state = new XMLNode (*child);
417 ControlProtocolManager&
418 ControlProtocolManager::instance ()
420 if (_instance == 0) {
421 _instance = new ControlProtocolManager ();
428 ControlProtocolManager::midi_connectivity_established ()
430 Glib::Mutex::Lock lm (protocols_lock);
432 for (list<ControlProtocol*>::iterator p = control_protocols.begin(); p != control_protocols.end(); ++p) {
433 (*p)->midi_connectivity_established ();