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"
37 #include "ardour/control_protocol_search_path.h"
40 using namespace ARDOUR;
46 ControlProtocolManager* ControlProtocolManager::_instance = 0;
47 const string ControlProtocolManager::state_node_name = X_("ControlProtocols");
49 ControlProtocolManager::ControlProtocolManager ()
53 ControlProtocolManager::~ControlProtocolManager()
55 Glib::Threads::Mutex::Lock lm (protocols_lock);
57 for (list<ControlProtocol*>::iterator i = control_protocols.begin(); i != control_protocols.end(); ++i) {
61 control_protocols.clear ();
64 for (list<ControlProtocolInfo*>::iterator p = control_protocol_info.begin(); p != control_protocol_info.end(); ++p) {
68 control_protocol_info.clear();
72 ControlProtocolManager::set_session (Session* s)
74 SessionHandlePtr::set_session (s);
77 Glib::Threads::Mutex::Lock lm (protocols_lock);
79 for (list<ControlProtocolInfo*>::iterator i = control_protocol_info.begin(); i != control_protocol_info.end(); ++i) {
80 if ((*i)->requested || (*i)->mandatory) {
83 (*i)->requested = false;
87 (*i)->protocol->set_state (*(*i)->state, Stateful::loading_state_version);
89 /* guarantee a call to
90 set_state() whether we have
93 (*i)->protocol->set_state (XMLNode(""), Stateful::loading_state_version);
102 ControlProtocolManager::session_going_away()
104 SessionHandlePtr::session_going_away ();
107 Glib::Threads::Mutex::Lock lm (protocols_lock);
109 for (list<ControlProtocol*>::iterator p = control_protocols.begin(); p != control_protocols.end(); ++p) {
113 control_protocols.clear ();
115 for (list<ControlProtocolInfo*>::iterator p = control_protocol_info.begin(); p != control_protocol_info.end(); ++p) {
116 // mark existing protocols as requested
117 // otherwise the ControlProtocol instances are not recreated in set_session
118 if ((*p)->protocol) {
119 (*p)->requested = true;
127 ControlProtocolManager::instantiate (ControlProtocolInfo& cpi)
129 /* CALLER MUST HOLD LOCK */
135 cpi.descriptor = get_descriptor (cpi.path);
137 DEBUG_TRACE (DEBUG::ControlProtocols, string_compose ("instantiating %1\n", cpi.name));
139 if (cpi.descriptor == 0) {
140 error << string_compose (_("control protocol name \"%1\" has no descriptor"), cpi.name) << endmsg;
144 DEBUG_TRACE (DEBUG::ControlProtocols, string_compose ("initializing %1\n", cpi.name));
146 if ((cpi.protocol = cpi.descriptor->initialize (cpi.descriptor, _session)) == 0) {
147 error << string_compose (_("control protocol name \"%1\" could not be initialized"), cpi.name) << endmsg;
151 control_protocols.push_back (cpi.protocol);
153 ProtocolStatusChange (&cpi);
159 ControlProtocolManager::teardown (ControlProtocolInfo& cpi)
165 if (!cpi.descriptor) {
173 cpi.descriptor->destroy (cpi.descriptor, cpi.protocol);
176 Glib::Threads::Mutex::Lock lm (protocols_lock);
177 list<ControlProtocol*>::iterator p = find (control_protocols.begin(), control_protocols.end(), cpi.protocol);
178 if (p != control_protocols.end()) {
179 control_protocols.erase (p);
181 cerr << "Programming error: ControlProtocolManager::teardown() called for " << cpi.name << ", but it was not found in control_protocols" << endl;
188 delete (Glib::Module*)cpi.descriptor->module;
190 ProtocolStatusChange (&cpi);
196 ControlProtocolManager::load_mandatory_protocols ()
202 Glib::Threads::Mutex::Lock lm (protocols_lock);
204 for (list<ControlProtocolInfo*>::iterator i = control_protocol_info.begin(); i != control_protocol_info.end(); ++i) {
205 if ((*i)->mandatory && ((*i)->protocol == 0)) {
206 DEBUG_TRACE (DEBUG::ControlProtocols,
207 string_compose (_("Instantiating mandatory control protocol %1"), (*i)->name));
214 ControlProtocolManager::discover_control_protocols ()
216 vector<std::string> cp_modules;
218 Glib::PatternSpec so_extension_pattern("*.so");
219 Glib::PatternSpec dylib_extension_pattern("*.dylib");
221 find_matching_files_in_search_path (control_protocol_search_path (),
222 so_extension_pattern, cp_modules);
224 find_matching_files_in_search_path (control_protocol_search_path (),
225 dylib_extension_pattern, cp_modules);
227 DEBUG_TRACE (DEBUG::ControlProtocols,
228 string_compose (_("looking for control protocols in %1\n"), control_protocol_search_path().to_string()));
230 for (vector<std::string>::iterator i = cp_modules.begin(); i != cp_modules.end(); ++i) {
231 control_protocol_discover (*i);
236 ControlProtocolManager::control_protocol_discover (string path)
238 ControlProtocolDescriptor* descriptor;
241 /* don't load OS X shared objects that are just symlinks to the real thing.
244 if (path.find (".dylib") && Glib::file_test (path, Glib::FILE_TEST_IS_SYMLINK)) {
249 if ((descriptor = get_descriptor (path)) != 0) {
251 if (!descriptor->probe (descriptor)) {
252 DEBUG_TRACE (DEBUG::ControlProtocols,
253 string_compose (_("Control protocol %1 not usable"), descriptor->name));
256 ControlProtocolInfo* cpi = new ControlProtocolInfo ();
258 cpi->descriptor = descriptor;
259 cpi->name = descriptor->name;
262 cpi->requested = false;
263 cpi->mandatory = descriptor->mandatory;
264 cpi->supports_feedback = descriptor->supports_feedback;
267 control_protocol_info.push_back (cpi);
269 DEBUG_TRACE (DEBUG::ControlProtocols,
270 string_compose(_("Control surface protocol discovered: \"%1\"\n"), cpi->name));
273 delete (Glib::Module*)descriptor->module;
279 ControlProtocolDescriptor*
280 ControlProtocolManager::get_descriptor (string path)
282 Glib::Module* module = new Glib::Module(path);
283 ControlProtocolDescriptor *descriptor = 0;
284 ControlProtocolDescriptor* (*dfunc)(void);
288 error << string_compose(_("ControlProtocolManager: cannot load module \"%1\" (%2)"), path, Glib::Module::get_last_error()) << endmsg;
293 if (!module->get_symbol("protocol_descriptor", func)) {
294 error << string_compose(_("ControlProtocolManager: module \"%1\" has no descriptor function."), path) << endmsg;
295 error << Glib::Module::get_last_error() << endmsg;
300 dfunc = (ControlProtocolDescriptor* (*)(void))func;
301 descriptor = dfunc();
304 descriptor->module = (void*)module;
313 ControlProtocolManager::foreach_known_protocol (boost::function<void(const ControlProtocolInfo*)> method)
315 for (list<ControlProtocolInfo*>::iterator i = control_protocol_info.begin(); i != control_protocol_info.end(); ++i) {
321 ControlProtocolManager::cpi_by_name (string name)
323 for (list<ControlProtocolInfo*>::iterator i = control_protocol_info.begin(); i != control_protocol_info.end(); ++i) {
324 if (name == (*i)->name) {
332 ControlProtocolManager::set_state (const XMLNode& node, int /*version*/)
335 XMLNodeConstIterator citer;
338 Glib::Threads::Mutex::Lock lm (protocols_lock);
340 clist = node.children();
342 for (citer = clist.begin(); citer != clist.end(); ++citer) {
343 if ((*citer)->name() == X_("Protocol")) {
345 if ((prop = (*citer)->property (X_("active"))) == 0) {
349 bool active = string_is_affirmative (prop->value());
351 if ((prop = (*citer)->property (X_("name"))) == 0) {
355 ControlProtocolInfo* cpi = cpi_by_name (prop->value());
358 cpi->state = new XMLNode (**citer);
364 cpi->requested = true;
370 cpi->requested = false;
381 ControlProtocolManager::get_state ()
383 XMLNode* root = new XMLNode (state_node_name);
384 Glib::Threads::Mutex::Lock lm (protocols_lock);
386 for (list<ControlProtocolInfo*>::iterator i = control_protocol_info.begin(); i != control_protocol_info.end(); ++i) {
390 if ((*i)->protocol) {
391 child = &((*i)->protocol->get_state());
392 child->add_property (X_("active"), "yes");
393 // should we update (*i)->state here? probably.
394 root->add_child_nocopy (*child);
395 } else if ((*i)->state) {
396 // keep ownership clear
397 root->add_child_copy (*(*i)->state);
399 child = new XMLNode (X_("Protocol"));
400 child->add_property (X_("name"), (*i)->name);
401 child->add_property (X_("active"), "no");
402 root->add_child_nocopy (*child);
410 ControlProtocolManager&
411 ControlProtocolManager::instance ()
413 if (_instance == 0) {
414 _instance = new ControlProtocolManager ();
421 ControlProtocolManager::midi_connectivity_established ()
423 Glib::Threads::Mutex::Lock lm (protocols_lock);
425 for (list<ControlProtocol*>::iterator p = control_protocols.begin(); p != control_protocols.end(); ++p) {
426 (*p)->midi_connectivity_established ();