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 ()
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;
78 if ((*i)->protocol && (*i)->state) {
79 (*i)->protocol->set_state (*(*i)->state, Stateful::loading_state_version);
87 ControlProtocolManager::session_going_away()
89 SessionHandlePtr::session_going_away ();
92 Glib::Mutex::Lock lm (protocols_lock);
94 for (list<ControlProtocol*>::iterator p = control_protocols.begin(); p != control_protocols.end(); ++p) {
98 control_protocols.clear ();
100 for (list<ControlProtocolInfo*>::iterator p = control_protocol_info.begin(); p != control_protocol_info.end(); ++p) {
101 // mark existing protocols as requested
102 // otherwise the ControlProtocol instances are not recreated in set_session
103 if ((*p)->protocol) {
104 (*p)->requested = true;
112 ControlProtocolManager::instantiate (ControlProtocolInfo& cpi)
114 /* CALLER MUST HOLD LOCK */
120 cpi.descriptor = get_descriptor (cpi.path);
122 if (cpi.descriptor == 0) {
123 error << string_compose (_("control protocol name \"%1\" has no descriptor"), cpi.name) << endmsg;
127 if ((cpi.protocol = cpi.descriptor->initialize (cpi.descriptor, _session)) == 0) {
128 error << string_compose (_("control protocol name \"%1\" could not be initialized"), cpi.name) << endmsg;
132 control_protocols.push_back (cpi.protocol);
138 ControlProtocolManager::teardown (ControlProtocolInfo& cpi)
144 if (!cpi.descriptor) {
152 cpi.descriptor->destroy (cpi.descriptor, cpi.protocol);
155 Glib::Mutex::Lock lm (protocols_lock);
156 list<ControlProtocol*>::iterator p = find (control_protocols.begin(), control_protocols.end(), cpi.protocol);
157 if (p != control_protocols.end()) {
158 control_protocols.erase (p);
160 cerr << "Programming error: ControlProtocolManager::teardown() called for " << cpi.name << ", but it was not found in control_protocols" << endl;
165 dlclose (cpi.descriptor->module);
170 ControlProtocolManager::load_mandatory_protocols ()
176 Glib::Mutex::Lock lm (protocols_lock);
178 for (list<ControlProtocolInfo*>::iterator i = control_protocol_info.begin(); i != control_protocol_info.end(); ++i) {
179 if ((*i)->mandatory && ((*i)->protocol == 0)) {
180 DEBUG_TRACE (DEBUG::ControlProtocols,
181 string_compose (_("Instantiating mandatory control protocol %1"), (*i)->name));
188 ControlProtocolManager::discover_control_protocols ()
190 vector<sys::path> cp_modules;
192 Glib::PatternSpec so_extension_pattern("*.so");
193 Glib::PatternSpec dylib_extension_pattern("*.dylib");
195 find_matching_files_in_search_path (control_protocol_search_path (),
196 so_extension_pattern, cp_modules);
198 find_matching_files_in_search_path (control_protocol_search_path (),
199 dylib_extension_pattern, cp_modules);
201 DEBUG_TRACE (DEBUG::ControlProtocols,
202 string_compose (_("looking for control protocols in %1"), control_protocol_search_path().to_string()));
204 for (vector<sys::path>::iterator i = cp_modules.begin(); i != cp_modules.end(); ++i) {
205 control_protocol_discover ((*i).to_string());
210 ControlProtocolManager::control_protocol_discover (string path)
212 ControlProtocolDescriptor* descriptor;
214 if ((descriptor = get_descriptor (path)) != 0) {
216 if (!descriptor->probe (descriptor)) {
217 DEBUG_TRACE (DEBUG::ControlProtocols,
218 string_compose (_("Control protocol %1 not usable"), descriptor->name));
221 ControlProtocolInfo* cpi = new ControlProtocolInfo ();
223 cpi->descriptor = descriptor;
224 cpi->name = descriptor->name;
227 cpi->requested = false;
228 cpi->mandatory = descriptor->mandatory;
229 cpi->supports_feedback = descriptor->supports_feedback;
232 control_protocol_info.push_back (cpi);
234 DEBUG_TRACE (DEBUG::ControlProtocols,
235 string_compose(_("Control surface protocol discovered: \"%1\""), cpi->name));
238 dlclose (descriptor->module);
244 ControlProtocolDescriptor*
245 ControlProtocolManager::get_descriptor (string path)
248 ControlProtocolDescriptor *descriptor = 0;
249 ControlProtocolDescriptor* (*dfunc)(void);
252 if ((module = dlopen (path.c_str(), RTLD_NOW)) == 0) {
253 error << string_compose(_("ControlProtocolManager: cannot load module \"%1\" (%2)"), path, dlerror()) << endmsg;
258 dfunc = (ControlProtocolDescriptor* (*)(void)) dlsym (module, "protocol_descriptor");
260 if ((errstr = dlerror()) != 0) {
261 error << string_compose(_("ControlProtocolManager: module \"%1\" has no descriptor function."), path) << endmsg;
262 error << errstr << endmsg;
267 descriptor = dfunc();
269 descriptor->module = module;
278 ControlProtocolManager::foreach_known_protocol (boost::function<void(const ControlProtocolInfo*)> method)
280 for (list<ControlProtocolInfo*>::iterator i = control_protocol_info.begin(); i != control_protocol_info.end(); ++i) {
286 ControlProtocolManager::cpi_by_name (string name)
288 for (list<ControlProtocolInfo*>::iterator i = control_protocol_info.begin(); i != control_protocol_info.end(); ++i) {
289 if (name == (*i)->name) {
297 ControlProtocolManager::set_state (const XMLNode& node, int /*version*/)
300 XMLNodeConstIterator citer;
303 Glib::Mutex::Lock lm (protocols_lock);
305 clist = node.children();
307 for (citer = clist.begin(); citer != clist.end(); ++citer) {
308 if ((*citer)->name() == X_("Protocol")) {
310 prop = (*citer)->property (X_("active"));
312 if (prop && string_is_affirmative (prop->value())) {
313 if ((prop = (*citer)->property (X_("name"))) != 0) {
314 ControlProtocolInfo* cpi = cpi_by_name (prop->value());
316 if (!(*citer)->children().empty()) {
317 cpi->state = (*citer)->children().front ();
325 cpi->requested = true;
336 ControlProtocolManager::get_state ()
338 XMLNode* root = new XMLNode (state_node_name);
339 Glib::Mutex::Lock lm (protocols_lock);
341 for (list<ControlProtocolInfo*>::iterator i = control_protocol_info.begin(); i != control_protocol_info.end(); ++i) {
345 if ((*i)->protocol) {
346 child = &((*i)->protocol->get_state());
347 child->add_property (X_("active"), "yes");
348 // should we update (*i)->state here? probably.
349 root->add_child_nocopy (*child);
350 } else if ((*i)->state) {
351 // keep ownership clear
352 root->add_child_copy (*(*i)->state);
354 child = new XMLNode (X_("Protocol"));
355 child->add_property (X_("name"), (*i)->name);
356 child->add_property (X_("active"), "no");
357 root->add_child_nocopy (*child);
365 ControlProtocolManager::set_protocol_states (const XMLNode& node)
368 XMLNodeConstIterator niter;
371 nlist = node.children();
373 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
375 XMLNode* child = (*niter);
377 if ((prop = child->property ("name")) == 0) {
378 error << _("control protocol XML node has no name property. Ignored.") << endmsg;
382 ControlProtocolInfo* cpi = cpi_by_name (prop->value());
385 warning << string_compose (_("control protocol \"%1\" is not known. Ignored"), prop->value()) << endmsg;
389 /* copy the node so that ownership is clear */
391 cpi->state = new XMLNode (*child);
395 ControlProtocolManager&
396 ControlProtocolManager::instance ()
398 if (_instance == 0) {
399 _instance = new ControlProtocolManager ();