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/control_protocol_manager.h"
32 #include "ardour/control_protocol_search_path.h"
34 using namespace ARDOUR;
40 ControlProtocolManager* ControlProtocolManager::_instance = 0;
41 const string ControlProtocolManager::state_node_name = X_("ControlProtocols");
43 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)
68 SessionHandlePtr::set_session (s);
71 Glib::Mutex::Lock lm (protocols_lock);
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;
80 (*i)->protocol->set_state (*(*i)->state, Stateful::loading_state_version);
82 /* guarantee a call to
83 set_state() whether we have
86 (*i)->protocol->set_state (XMLNode(""), Stateful::loading_state_version);
95 ControlProtocolManager::session_going_away()
97 SessionHandlePtr::session_going_away ();
100 Glib::Mutex::Lock lm (protocols_lock);
102 for (list<ControlProtocol*>::iterator p = control_protocols.begin(); p != control_protocols.end(); ++p) {
106 control_protocols.clear ();
108 for (list<ControlProtocolInfo*>::iterator p = control_protocol_info.begin(); p != control_protocol_info.end(); ++p) {
109 // mark existing protocols as requested
110 // otherwise the ControlProtocol instances are not recreated in set_session
111 if ((*p)->protocol) {
112 (*p)->requested = true;
120 ControlProtocolManager::instantiate (ControlProtocolInfo& cpi)
122 /* CALLER MUST HOLD LOCK */
128 cpi.descriptor = get_descriptor (cpi.path);
130 if (cpi.descriptor == 0) {
131 error << string_compose (_("control protocol name \"%1\" has no descriptor"), cpi.name) << endmsg;
135 if ((cpi.protocol = cpi.descriptor->initialize (cpi.descriptor, _session)) == 0) {
136 error << string_compose (_("control protocol name \"%1\" could not be initialized"), cpi.name) << endmsg;
140 control_protocols.push_back (cpi.protocol);
146 ControlProtocolManager::teardown (ControlProtocolInfo& cpi)
152 if (!cpi.descriptor) {
160 cpi.descriptor->destroy (cpi.descriptor, cpi.protocol);
163 Glib::Mutex::Lock lm (protocols_lock);
164 list<ControlProtocol*>::iterator p = find (control_protocols.begin(), control_protocols.end(), cpi.protocol);
165 if (p != control_protocols.end()) {
166 control_protocols.erase (p);
168 cerr << "Programming error: ControlProtocolManager::teardown() called for " << cpi.name << ", but it was not found in control_protocols" << endl;
175 dlclose (cpi.descriptor->module);
180 ControlProtocolManager::load_mandatory_protocols ()
186 Glib::Mutex::Lock lm (protocols_lock);
188 for (list<ControlProtocolInfo*>::iterator i = control_protocol_info.begin(); i != control_protocol_info.end(); ++i) {
189 if ((*i)->mandatory && ((*i)->protocol == 0)) {
190 DEBUG_TRACE (DEBUG::ControlProtocols,
191 string_compose (_("Instantiating mandatory control protocol %1"), (*i)->name));
198 ControlProtocolManager::discover_control_protocols ()
200 vector<std::string> cp_modules;
202 Glib::PatternSpec so_extension_pattern("*.so");
203 Glib::PatternSpec dylib_extension_pattern("*.dylib");
205 find_matching_files_in_search_path (control_protocol_search_path (),
206 so_extension_pattern, cp_modules);
208 find_matching_files_in_search_path (control_protocol_search_path (),
209 dylib_extension_pattern, cp_modules);
211 DEBUG_TRACE (DEBUG::ControlProtocols,
212 string_compose (_("looking for control protocols in %1\n"), control_protocol_search_path().to_string()));
214 for (vector<std::string>::iterator i = cp_modules.begin(); i != cp_modules.end(); ++i) {
215 control_protocol_discover (*i);
220 ControlProtocolManager::control_protocol_discover (string path)
222 ControlProtocolDescriptor* descriptor;
225 /* don't load OS X shared objects that are just symlinks to the real thing.
228 if (path.find (".dylib") && Glib::file_test (path, Glib::FILE_TEST_IS_SYMLINK)) {
233 if ((descriptor = get_descriptor (path)) != 0) {
235 if (!descriptor->probe (descriptor)) {
236 DEBUG_TRACE (DEBUG::ControlProtocols,
237 string_compose (_("Control protocol %1 not usable"), descriptor->name));
240 ControlProtocolInfo* cpi = new ControlProtocolInfo ();
242 cpi->descriptor = descriptor;
243 cpi->name = descriptor->name;
246 cpi->requested = false;
247 cpi->mandatory = descriptor->mandatory;
248 cpi->supports_feedback = descriptor->supports_feedback;
251 control_protocol_info.push_back (cpi);
253 DEBUG_TRACE (DEBUG::ControlProtocols,
254 string_compose(_("Control surface protocol discovered: \"%1\""), cpi->name));
257 dlclose (descriptor->module);
263 ControlProtocolDescriptor*
264 ControlProtocolManager::get_descriptor (string path)
267 ControlProtocolDescriptor *descriptor = 0;
268 ControlProtocolDescriptor* (*dfunc)(void);
271 if ((module = dlopen (path.c_str(), RTLD_NOW)) == 0) {
272 error << string_compose(_("ControlProtocolManager: cannot load module \"%1\" (%2)"), path, dlerror()) << endmsg;
277 dfunc = (ControlProtocolDescriptor* (*)(void)) dlsym (module, "protocol_descriptor");
279 if ((errstr = dlerror()) != 0) {
280 error << string_compose(_("ControlProtocolManager: module \"%1\" has no descriptor function."), path) << endmsg;
281 error << errstr << endmsg;
286 descriptor = dfunc();
288 descriptor->module = module;
297 ControlProtocolManager::foreach_known_protocol (boost::function<void(const ControlProtocolInfo*)> method)
299 for (list<ControlProtocolInfo*>::iterator i = control_protocol_info.begin(); i != control_protocol_info.end(); ++i) {
305 ControlProtocolManager::cpi_by_name (string name)
307 for (list<ControlProtocolInfo*>::iterator i = control_protocol_info.begin(); i != control_protocol_info.end(); ++i) {
308 if (name == (*i)->name) {
316 ControlProtocolManager::set_state (const XMLNode& node, int /*version*/)
319 XMLNodeConstIterator citer;
322 Glib::Mutex::Lock lm (protocols_lock);
324 clist = node.children();
326 for (citer = clist.begin(); citer != clist.end(); ++citer) {
327 if ((*citer)->name() == X_("Protocol")) {
329 prop = (*citer)->property (X_("active"));
331 if (prop && string_is_affirmative (prop->value())) {
332 if ((prop = (*citer)->property (X_("name"))) != 0) {
333 ControlProtocolInfo* cpi = cpi_by_name (prop->value());
341 cpi->state = new XMLNode (**citer);
346 cpi->requested = true;
357 ControlProtocolManager::get_state ()
359 XMLNode* root = new XMLNode (state_node_name);
360 Glib::Mutex::Lock lm (protocols_lock);
362 for (list<ControlProtocolInfo*>::iterator i = control_protocol_info.begin(); i != control_protocol_info.end(); ++i) {
366 if ((*i)->protocol) {
367 child = &((*i)->protocol->get_state());
368 child->add_property (X_("active"), "yes");
369 // should we update (*i)->state here? probably.
370 root->add_child_nocopy (*child);
371 } else if ((*i)->state) {
372 // keep ownership clear
373 root->add_child_copy (*(*i)->state);
375 child = new XMLNode (X_("Protocol"));
376 child->add_property (X_("name"), (*i)->name);
377 child->add_property (X_("active"), "no");
378 root->add_child_nocopy (*child);
386 ControlProtocolManager::set_protocol_states (const XMLNode& node)
389 XMLNodeConstIterator niter;
392 nlist = node.children();
394 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
396 XMLNode* child = (*niter);
398 if ((prop = child->property ("name")) == 0) {
399 error << _("control protocol XML node has no name property. Ignored.") << endmsg;
403 ControlProtocolInfo* cpi = cpi_by_name (prop->value());
406 warning << string_compose (_("control protocol \"%1\" is not known. Ignored"), prop->value()) << endmsg;
410 /* copy the node so that ownership is clear */
412 cpi->state = new XMLNode (*child);
416 ControlProtocolManager&
417 ControlProtocolManager::instance ()
419 if (_instance == 0) {
420 _instance = new ControlProtocolManager ();
427 ControlProtocolManager::midi_connectivity_established ()
429 Glib::Mutex::Lock lm (protocols_lock);
431 for (list<ControlProtocol*>::iterator p = control_protocols.begin(); p != control_protocols.end(); ++p) {
432 (*p)->midi_connectivity_established ();