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/debug.h"
29 #include "ardour/session.h"
30 #include "ardour/control_protocol_manager.h"
31 #include "ardour/control_protocol_search_path.h"
33 using namespace ARDOUR;
39 ControlProtocolManager* ControlProtocolManager::_instance = 0;
40 const string ControlProtocolManager::state_node_name = X_("ControlProtocols");
42 ControlProtocolManager::ControlProtocolManager ()
46 ControlProtocolManager::~ControlProtocolManager()
48 Glib::Mutex::Lock lm (protocols_lock);
50 for (list<ControlProtocol*>::iterator i = control_protocols.begin(); i != control_protocols.end(); ++i) {
54 control_protocols.clear ();
57 for (list<ControlProtocolInfo*>::iterator p = control_protocol_info.begin(); p != control_protocol_info.end(); ++p) {
61 control_protocol_info.clear();
65 ControlProtocolManager::set_session (Session* s)
67 SessionHandlePtr::set_session (s);
70 Glib::Mutex::Lock lm (protocols_lock);
72 for (list<ControlProtocolInfo*>::iterator i = control_protocol_info.begin(); i != control_protocol_info.end(); ++i) {
73 if ((*i)->requested || (*i)->mandatory) {
75 (*i)->requested = false;
79 (*i)->protocol->set_state (*(*i)->state, Stateful::loading_state_version);
81 /* guarantee a call to
82 set_state() whether we have
85 (*i)->protocol->set_state (XMLNode(""), Stateful::loading_state_version);
94 ControlProtocolManager::session_going_away()
96 SessionHandlePtr::session_going_away ();
99 Glib::Mutex::Lock lm (protocols_lock);
101 for (list<ControlProtocol*>::iterator p = control_protocols.begin(); p != control_protocols.end(); ++p) {
105 control_protocols.clear ();
107 for (list<ControlProtocolInfo*>::iterator p = control_protocol_info.begin(); p != control_protocol_info.end(); ++p) {
108 // mark existing protocols as requested
109 // otherwise the ControlProtocol instances are not recreated in set_session
110 if ((*p)->protocol) {
111 (*p)->requested = true;
119 ControlProtocolManager::instantiate (ControlProtocolInfo& cpi)
121 /* CALLER MUST HOLD LOCK */
127 cpi.descriptor = get_descriptor (cpi.path);
129 if (cpi.descriptor == 0) {
130 error << string_compose (_("control protocol name \"%1\" has no descriptor"), cpi.name) << endmsg;
134 if ((cpi.protocol = cpi.descriptor->initialize (cpi.descriptor, _session)) == 0) {
135 error << string_compose (_("control protocol name \"%1\" could not be initialized"), cpi.name) << endmsg;
139 control_protocols.push_back (cpi.protocol);
145 ControlProtocolManager::teardown (ControlProtocolInfo& cpi)
151 if (!cpi.descriptor) {
159 cpi.descriptor->destroy (cpi.descriptor, cpi.protocol);
162 Glib::Mutex::Lock lm (protocols_lock);
163 list<ControlProtocol*>::iterator p = find (control_protocols.begin(), control_protocols.end(), cpi.protocol);
164 if (p != control_protocols.end()) {
165 control_protocols.erase (p);
167 cerr << "Programming error: ControlProtocolManager::teardown() called for " << cpi.name << ", but it was not found in control_protocols" << endl;
172 dlclose (cpi.descriptor->module);
177 ControlProtocolManager::load_mandatory_protocols ()
183 Glib::Mutex::Lock lm (protocols_lock);
185 for (list<ControlProtocolInfo*>::iterator i = control_protocol_info.begin(); i != control_protocol_info.end(); ++i) {
186 if ((*i)->mandatory && ((*i)->protocol == 0)) {
187 DEBUG_TRACE (DEBUG::ControlProtocols,
188 string_compose (_("Instantiating mandatory control protocol %1"), (*i)->name));
195 ControlProtocolManager::discover_control_protocols ()
197 vector<sys::path> cp_modules;
199 Glib::PatternSpec so_extension_pattern("*.so");
200 Glib::PatternSpec dylib_extension_pattern("*.dylib");
202 find_matching_files_in_search_path (control_protocol_search_path (),
203 so_extension_pattern, cp_modules);
205 find_matching_files_in_search_path (control_protocol_search_path (),
206 dylib_extension_pattern, cp_modules);
208 DEBUG_TRACE (DEBUG::ControlProtocols,
209 string_compose (_("looking for control protocols in %1"), control_protocol_search_path().to_string()));
211 for (vector<sys::path>::iterator i = cp_modules.begin(); i != cp_modules.end(); ++i) {
212 control_protocol_discover ((*i).to_string());
217 ControlProtocolManager::control_protocol_discover (string path)
219 ControlProtocolDescriptor* descriptor;
221 if ((descriptor = get_descriptor (path)) != 0) {
223 if (!descriptor->probe (descriptor)) {
224 DEBUG_TRACE (DEBUG::ControlProtocols,
225 string_compose (_("Control protocol %1 not usable"), descriptor->name));
228 ControlProtocolInfo* cpi = new ControlProtocolInfo ();
230 cpi->descriptor = descriptor;
231 cpi->name = descriptor->name;
234 cpi->requested = false;
235 cpi->mandatory = descriptor->mandatory;
236 cpi->supports_feedback = descriptor->supports_feedback;
239 control_protocol_info.push_back (cpi);
241 DEBUG_TRACE (DEBUG::ControlProtocols,
242 string_compose(_("Control surface protocol discovered: \"%1\""), cpi->name));
245 dlclose (descriptor->module);
251 ControlProtocolDescriptor*
252 ControlProtocolManager::get_descriptor (string path)
255 ControlProtocolDescriptor *descriptor = 0;
256 ControlProtocolDescriptor* (*dfunc)(void);
259 if ((module = dlopen (path.c_str(), RTLD_NOW)) == 0) {
260 error << string_compose(_("ControlProtocolManager: cannot load module \"%1\" (%2)"), path, dlerror()) << endmsg;
265 dfunc = (ControlProtocolDescriptor* (*)(void)) dlsym (module, "protocol_descriptor");
267 if ((errstr = dlerror()) != 0) {
268 error << string_compose(_("ControlProtocolManager: module \"%1\" has no descriptor function."), path) << endmsg;
269 error << errstr << endmsg;
274 descriptor = dfunc();
276 descriptor->module = module;
285 ControlProtocolManager::foreach_known_protocol (boost::function<void(const ControlProtocolInfo*)> method)
287 for (list<ControlProtocolInfo*>::iterator i = control_protocol_info.begin(); i != control_protocol_info.end(); ++i) {
293 ControlProtocolManager::cpi_by_name (string name)
295 for (list<ControlProtocolInfo*>::iterator i = control_protocol_info.begin(); i != control_protocol_info.end(); ++i) {
296 if (name == (*i)->name) {
304 ControlProtocolManager::set_state (const XMLNode& node, int /*version*/)
307 XMLNodeConstIterator citer;
310 Glib::Mutex::Lock lm (protocols_lock);
312 clist = node.children();
314 for (citer = clist.begin(); citer != clist.end(); ++citer) {
315 if ((*citer)->name() == X_("Protocol")) {
317 prop = (*citer)->property (X_("active"));
319 if (prop && string_is_affirmative (prop->value())) {
320 if ((prop = (*citer)->property (X_("name"))) != 0) {
321 ControlProtocolInfo* cpi = cpi_by_name (prop->value());
329 cpi->state = new XMLNode (**citer);
334 cpi->requested = true;
345 ControlProtocolManager::get_state ()
347 XMLNode* root = new XMLNode (state_node_name);
348 Glib::Mutex::Lock lm (protocols_lock);
350 for (list<ControlProtocolInfo*>::iterator i = control_protocol_info.begin(); i != control_protocol_info.end(); ++i) {
354 if ((*i)->protocol) {
355 child = &((*i)->protocol->get_state());
356 child->add_property (X_("active"), "yes");
357 // should we update (*i)->state here? probably.
358 root->add_child_nocopy (*child);
359 } else if ((*i)->state) {
360 // keep ownership clear
361 root->add_child_copy (*(*i)->state);
363 child = new XMLNode (X_("Protocol"));
364 child->add_property (X_("name"), (*i)->name);
365 child->add_property (X_("active"), "no");
366 root->add_child_nocopy (*child);
374 ControlProtocolManager::set_protocol_states (const XMLNode& node)
377 XMLNodeConstIterator niter;
380 nlist = node.children();
382 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
384 XMLNode* child = (*niter);
386 if ((prop = child->property ("name")) == 0) {
387 error << _("control protocol XML node has no name property. Ignored.") << endmsg;
391 ControlProtocolInfo* cpi = cpi_by_name (prop->value());
394 warning << string_compose (_("control protocol \"%1\" is not known. Ignored"), prop->value()) << endmsg;
398 /* copy the node so that ownership is clear */
400 cpi->state = new XMLNode (*child);
404 ControlProtocolManager&
405 ControlProtocolManager::instance ()
407 if (_instance == 0) {
408 _instance = new ControlProtocolManager ();
415 ControlProtocolManager::midi_connectivity_established ()
417 Glib::Mutex::Lock lm (protocols_lock);
419 for (list<ControlProtocol*>::iterator p = control_protocols.begin(); p != control_protocols.end(); ++p) {
420 (*p)->midi_connectivity_established ();