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.
20 #include <glibmm/module.h>
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"
33 #include "ardour/control_protocol_search_path.h"
36 using namespace ARDOUR;
42 ControlProtocolManager* ControlProtocolManager::_instance = 0;
43 const string ControlProtocolManager::state_node_name = X_("ControlProtocols");
45 ControlProtocolManager::ControlProtocolManager ()
49 ControlProtocolManager::~ControlProtocolManager()
51 Glib::Threads::Mutex::Lock lm (protocols_lock);
53 for (list<ControlProtocol*>::iterator i = control_protocols.begin(); i != control_protocols.end(); ++i) {
57 control_protocols.clear ();
60 for (list<ControlProtocolInfo*>::iterator p = control_protocol_info.begin(); p != control_protocol_info.end(); ++p) {
64 control_protocol_info.clear();
68 ControlProtocolManager::set_session (Session* s)
70 SessionHandlePtr::set_session (s);
73 Glib::Threads::Mutex::Lock lm (protocols_lock);
75 for (list<ControlProtocolInfo*>::iterator i = control_protocol_info.begin(); i != control_protocol_info.end(); ++i) {
76 if ((*i)->requested || (*i)->mandatory) {
79 (*i)->requested = false;
83 (*i)->protocol->set_state (*(*i)->state, Stateful::loading_state_version);
85 /* guarantee a call to
86 set_state() whether we have
89 (*i)->protocol->set_state (XMLNode(""), Stateful::loading_state_version);
98 ControlProtocolManager::session_going_away()
100 SessionHandlePtr::session_going_away ();
103 Glib::Threads::Mutex::Lock lm (protocols_lock);
105 for (list<ControlProtocol*>::iterator p = control_protocols.begin(); p != control_protocols.end(); ++p) {
109 control_protocols.clear ();
111 for (list<ControlProtocolInfo*>::iterator p = control_protocol_info.begin(); p != control_protocol_info.end(); ++p) {
112 // mark existing protocols as requested
113 // otherwise the ControlProtocol instances are not recreated in set_session
114 if ((*p)->protocol) {
115 (*p)->requested = true;
123 ControlProtocolManager::instantiate (ControlProtocolInfo& cpi)
125 /* CALLER MUST HOLD LOCK */
131 cpi.descriptor = get_descriptor (cpi.path);
133 DEBUG_TRACE (DEBUG::ControlProtocols, string_compose ("instantiating %1\n", cpi.name));
135 if (cpi.descriptor == 0) {
136 error << string_compose (_("control protocol name \"%1\" has no descriptor"), cpi.name) << endmsg;
140 DEBUG_TRACE (DEBUG::ControlProtocols, string_compose ("initializing %1\n", cpi.name));
142 if ((cpi.protocol = cpi.descriptor->initialize (cpi.descriptor, _session)) == 0) {
143 error << string_compose (_("control protocol name \"%1\" could not be initialized"), cpi.name) << endmsg;
147 control_protocols.push_back (cpi.protocol);
149 ProtocolStatusChange (&cpi);
155 ControlProtocolManager::teardown (ControlProtocolInfo& cpi)
161 if (!cpi.descriptor) {
169 cpi.descriptor->destroy (cpi.descriptor, cpi.protocol);
172 Glib::Threads::Mutex::Lock lm (protocols_lock);
173 list<ControlProtocol*>::iterator p = find (control_protocols.begin(), control_protocols.end(), cpi.protocol);
174 if (p != control_protocols.end()) {
175 control_protocols.erase (p);
177 cerr << "Programming error: ControlProtocolManager::teardown() called for " << cpi.name << ", but it was not found in control_protocols" << endl;
184 delete (Glib::Module*)cpi.descriptor->module;
186 ProtocolStatusChange (&cpi);
192 ControlProtocolManager::load_mandatory_protocols ()
198 Glib::Threads::Mutex::Lock lm (protocols_lock);
200 for (list<ControlProtocolInfo*>::iterator i = control_protocol_info.begin(); i != control_protocol_info.end(); ++i) {
201 if ((*i)->mandatory && ((*i)->protocol == 0)) {
202 DEBUG_TRACE (DEBUG::ControlProtocols,
203 string_compose (_("Instantiating mandatory control protocol %1"), (*i)->name));
210 ControlProtocolManager::discover_control_protocols ()
212 vector<std::string> cp_modules;
214 Glib::PatternSpec so_extension_pattern("*.so");
215 Glib::PatternSpec dylib_extension_pattern("*.dylib");
217 find_matching_files_in_search_path (control_protocol_search_path (),
218 so_extension_pattern, cp_modules);
220 find_matching_files_in_search_path (control_protocol_search_path (),
221 dylib_extension_pattern, cp_modules);
223 DEBUG_TRACE (DEBUG::ControlProtocols,
224 string_compose (_("looking for control protocols in %1\n"), control_protocol_search_path().to_string()));
226 for (vector<std::string>::iterator i = cp_modules.begin(); i != cp_modules.end(); ++i) {
227 control_protocol_discover (*i);
232 ControlProtocolManager::control_protocol_discover (string path)
234 ControlProtocolDescriptor* descriptor;
237 /* don't load OS X shared objects that are just symlinks to the real thing.
240 if (path.find (".dylib") && Glib::file_test (path, Glib::FILE_TEST_IS_SYMLINK)) {
245 if ((descriptor = get_descriptor (path)) != 0) {
247 if (!descriptor->probe (descriptor)) {
248 DEBUG_TRACE (DEBUG::ControlProtocols,
249 string_compose (_("Control protocol %1 not usable"), descriptor->name));
252 ControlProtocolInfo* cpi = new ControlProtocolInfo ();
254 cpi->descriptor = descriptor;
255 cpi->name = descriptor->name;
258 cpi->requested = false;
259 cpi->mandatory = descriptor->mandatory;
260 cpi->supports_feedback = descriptor->supports_feedback;
263 control_protocol_info.push_back (cpi);
265 DEBUG_TRACE (DEBUG::ControlProtocols,
266 string_compose(_("Control surface protocol discovered: \"%1\"\n"), cpi->name));
269 delete (Glib::Module*)descriptor->module;
275 ControlProtocolDescriptor*
276 ControlProtocolManager::get_descriptor (string path)
278 Glib::Module* module = new Glib::Module(path);
279 ControlProtocolDescriptor *descriptor = 0;
280 ControlProtocolDescriptor* (*dfunc)(void);
284 error << string_compose(_("ControlProtocolManager: cannot load module \"%1\" (%2)"), path, Glib::Module::get_last_error()) << endmsg;
289 if (!module->get_symbol("protocol_descriptor", func)) {
290 error << string_compose(_("ControlProtocolManager: module \"%1\" has no descriptor function."), path) << endmsg;
291 error << Glib::Module::get_last_error() << endmsg;
296 dfunc = (ControlProtocolDescriptor* (*)(void))func;
297 descriptor = dfunc();
300 descriptor->module = (void*)module;
309 ControlProtocolManager::foreach_known_protocol (boost::function<void(const ControlProtocolInfo*)> method)
311 for (list<ControlProtocolInfo*>::iterator i = control_protocol_info.begin(); i != control_protocol_info.end(); ++i) {
317 ControlProtocolManager::cpi_by_name (string name)
319 for (list<ControlProtocolInfo*>::iterator i = control_protocol_info.begin(); i != control_protocol_info.end(); ++i) {
320 if (name == (*i)->name) {
328 ControlProtocolManager::set_state (const XMLNode& node, int /*version*/)
331 XMLNodeConstIterator citer;
334 Glib::Threads::Mutex::Lock lm (protocols_lock);
336 clist = node.children();
338 for (citer = clist.begin(); citer != clist.end(); ++citer) {
339 if ((*citer)->name() == X_("Protocol")) {
341 if ((prop = (*citer)->property (X_("active"))) == 0) {
345 bool active = string_is_affirmative (prop->value());
347 if ((prop = (*citer)->property (X_("name"))) == 0) {
351 ControlProtocolInfo* cpi = cpi_by_name (prop->value());
354 cpi->state = new XMLNode (**citer);
360 cpi->requested = true;
366 cpi->requested = false;
377 ControlProtocolManager::get_state ()
379 XMLNode* root = new XMLNode (state_node_name);
380 Glib::Threads::Mutex::Lock lm (protocols_lock);
382 for (list<ControlProtocolInfo*>::iterator i = control_protocol_info.begin(); i != control_protocol_info.end(); ++i) {
386 if ((*i)->protocol) {
387 child = &((*i)->protocol->get_state());
388 child->add_property (X_("active"), "yes");
389 // should we update (*i)->state here? probably.
390 root->add_child_nocopy (*child);
391 } else if ((*i)->state) {
392 // keep ownership clear
393 root->add_child_copy (*(*i)->state);
395 child = new XMLNode (X_("Protocol"));
396 child->add_property (X_("name"), (*i)->name);
397 child->add_property (X_("active"), "no");
398 root->add_child_nocopy (*child);
406 ControlProtocolManager&
407 ControlProtocolManager::instance ()
409 if (_instance == 0) {
410 _instance = new ControlProtocolManager ();
417 ControlProtocolManager::midi_connectivity_established ()
419 Glib::Threads::Mutex::Lock lm (protocols_lock);
421 for (list<ControlProtocol*>::iterator p = control_protocols.begin(); p != control_protocols.end(); ++p) {
422 (*p)->midi_connectivity_established ();