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::Threads::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::Threads::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::Threads::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);
142 ProtocolStatusChange (&cpi);
148 ControlProtocolManager::teardown (ControlProtocolInfo& cpi)
154 if (!cpi.descriptor) {
162 cpi.descriptor->destroy (cpi.descriptor, cpi.protocol);
165 Glib::Threads::Mutex::Lock lm (protocols_lock);
166 list<ControlProtocol*>::iterator p = find (control_protocols.begin(), control_protocols.end(), cpi.protocol);
167 if (p != control_protocols.end()) {
168 control_protocols.erase (p);
170 cerr << "Programming error: ControlProtocolManager::teardown() called for " << cpi.name << ", but it was not found in control_protocols" << endl;
177 dlclose (cpi.descriptor->module);
179 ProtocolStatusChange (&cpi);
185 ControlProtocolManager::load_mandatory_protocols ()
191 Glib::Threads::Mutex::Lock lm (protocols_lock);
193 for (list<ControlProtocolInfo*>::iterator i = control_protocol_info.begin(); i != control_protocol_info.end(); ++i) {
194 if ((*i)->mandatory && ((*i)->protocol == 0)) {
195 DEBUG_TRACE (DEBUG::ControlProtocols,
196 string_compose (_("Instantiating mandatory control protocol %1"), (*i)->name));
203 ControlProtocolManager::discover_control_protocols ()
205 vector<std::string> cp_modules;
207 Glib::PatternSpec so_extension_pattern("*.so");
208 Glib::PatternSpec dylib_extension_pattern("*.dylib");
210 find_matching_files_in_search_path (control_protocol_search_path (),
211 so_extension_pattern, cp_modules);
213 find_matching_files_in_search_path (control_protocol_search_path (),
214 dylib_extension_pattern, cp_modules);
216 DEBUG_TRACE (DEBUG::ControlProtocols,
217 string_compose (_("looking for control protocols in %1\n"), control_protocol_search_path().to_string()));
219 for (vector<std::string>::iterator i = cp_modules.begin(); i != cp_modules.end(); ++i) {
220 control_protocol_discover (*i);
225 ControlProtocolManager::control_protocol_discover (string path)
227 ControlProtocolDescriptor* descriptor;
230 /* don't load OS X shared objects that are just symlinks to the real thing.
233 if (path.find (".dylib") && Glib::file_test (path, Glib::FILE_TEST_IS_SYMLINK)) {
238 if ((descriptor = get_descriptor (path)) != 0) {
240 if (!descriptor->probe (descriptor)) {
241 DEBUG_TRACE (DEBUG::ControlProtocols,
242 string_compose (_("Control protocol %1 not usable"), descriptor->name));
245 ControlProtocolInfo* cpi = new ControlProtocolInfo ();
247 cpi->descriptor = descriptor;
248 cpi->name = descriptor->name;
251 cpi->requested = false;
252 cpi->mandatory = descriptor->mandatory;
253 cpi->supports_feedback = descriptor->supports_feedback;
256 control_protocol_info.push_back (cpi);
258 DEBUG_TRACE (DEBUG::ControlProtocols,
259 string_compose(_("Control surface protocol discovered: \"%1\""), cpi->name));
262 dlclose (descriptor->module);
268 ControlProtocolDescriptor*
269 ControlProtocolManager::get_descriptor (string path)
272 ControlProtocolDescriptor *descriptor = 0;
273 ControlProtocolDescriptor* (*dfunc)(void);
276 if ((module = dlopen (path.c_str(), RTLD_NOW)) == 0) {
277 error << string_compose(_("ControlProtocolManager: cannot load module \"%1\" (%2)"), path, dlerror()) << endmsg;
282 dfunc = (ControlProtocolDescriptor* (*)(void)) dlsym (module, "protocol_descriptor");
284 if ((errstr = dlerror()) != 0) {
285 error << string_compose(_("ControlProtocolManager: module \"%1\" has no descriptor function."), path) << endmsg;
286 error << errstr << endmsg;
291 descriptor = dfunc();
293 descriptor->module = module;
302 ControlProtocolManager::foreach_known_protocol (boost::function<void(const ControlProtocolInfo*)> method)
304 for (list<ControlProtocolInfo*>::iterator i = control_protocol_info.begin(); i != control_protocol_info.end(); ++i) {
310 ControlProtocolManager::cpi_by_name (string name)
312 for (list<ControlProtocolInfo*>::iterator i = control_protocol_info.begin(); i != control_protocol_info.end(); ++i) {
313 if (name == (*i)->name) {
321 ControlProtocolManager::set_state (const XMLNode& node, int /*version*/)
324 XMLNodeConstIterator citer;
327 Glib::Threads::Mutex::Lock lm (protocols_lock);
329 clist = node.children();
331 for (citer = clist.begin(); citer != clist.end(); ++citer) {
332 if ((*citer)->name() == X_("Protocol")) {
334 if ((prop = (*citer)->property (X_("active"))) == 0) {
338 bool active = string_is_affirmative (prop->value());
340 if ((prop = (*citer)->property (X_("name"))) == 0) {
344 ControlProtocolInfo* cpi = cpi_by_name (prop->value());
348 if (!(*citer)->children().empty()) {
349 cpi->state = new XMLNode (*((*citer)->children().front ()));
358 cpi->requested = true;
364 cpi->requested = false;
375 ControlProtocolManager::get_state ()
377 XMLNode* root = new XMLNode (state_node_name);
378 Glib::Threads::Mutex::Lock lm (protocols_lock);
380 for (list<ControlProtocolInfo*>::iterator i = control_protocol_info.begin(); i != control_protocol_info.end(); ++i) {
384 if ((*i)->protocol) {
385 child = &((*i)->protocol->get_state());
386 child->add_property (X_("active"), "yes");
387 // should we update (*i)->state here? probably.
388 root->add_child_nocopy (*child);
389 } else if ((*i)->state) {
390 // keep ownership clear
391 root->add_child_copy (*(*i)->state);
393 child = new XMLNode (X_("Protocol"));
394 child->add_property (X_("name"), (*i)->name);
395 child->add_property (X_("active"), "no");
396 root->add_child_nocopy (*child);
404 ControlProtocolManager&
405 ControlProtocolManager::instance ()
407 if (_instance == 0) {
408 _instance = new ControlProtocolManager ();
415 ControlProtocolManager::midi_connectivity_established ()
417 Glib::Threads::Mutex::Lock lm (protocols_lock);
419 for (list<ControlProtocol*>::iterator p = control_protocols.begin(); p != control_protocols.end(); ++p) {
420 (*p)->midi_connectivity_established ();