3 #include <pbd/compose.h>
5 #include <pbd/pathscanner.h>
7 #include "control_protocol.h"
9 #include <ardour/session.h>
10 #include <ardour/control_protocol_manager.h>
15 using namespace ARDOUR;
21 ControlProtocolManager* ControlProtocolManager::_instance = 0;
22 const string ControlProtocolManager::state_node_name = X_("ControlProtocols");
24 ControlProtocolManager::ControlProtocolManager ()
33 ControlProtocolManager::~ControlProtocolManager()
35 LockMonitor lm (protocols_lock, __LINE__, __FILE__);
37 for (list<ControlProtocol*>::iterator i = control_protocols.begin(); i != control_protocols.end(); ++i) {
41 control_protocols.clear ();
46 ControlProtocolManager::set_session (Session& s)
49 _session->going_away.connect (mem_fun (*this, &ControlProtocolManager::drop_session));
51 for (list<ControlProtocolInfo*>::iterator i = control_protocol_info.begin(); i != control_protocol_info.end(); ++i) {
52 if ((*i)->requested || (*i)->mandatory) {
54 (*i)->requested = false;
60 ControlProtocolManager::drop_session ()
65 LockMonitor lm (protocols_lock, __LINE__, __FILE__);
66 for (list<ControlProtocol*>::iterator p = control_protocols.begin(); p != control_protocols.end(); ++p) {
69 control_protocols.clear ();
74 ControlProtocolManager::instantiate (ControlProtocolInfo& cpi)
80 cpi.descriptor = get_descriptor (cpi.path);
82 if (cpi.descriptor == 0) {
83 error << string_compose (_("control protocol name \"%1\" has no descriptor"), cpi.name) << endmsg;
87 if ((cpi.protocol = cpi.descriptor->initialize (cpi.descriptor, _session)) == 0) {
88 error << string_compose (_("control protocol name \"%1\" could not be initialized"), cpi.name) << endmsg;
92 LockMonitor lm (protocols_lock, __LINE__, __FILE__);
93 control_protocols.push_back (cpi.protocol);
99 ControlProtocolManager::teardown (ControlProtocolInfo& cpi)
105 if (!cpi.descriptor) {
113 cpi.descriptor->destroy (cpi.descriptor, cpi.protocol);
116 LockMonitor lm (protocols_lock, __LINE__, __FILE__);
117 list<ControlProtocol*>::iterator p = find (control_protocols.begin(), control_protocols.end(), cpi.protocol);
118 if (p != control_protocols.end()) {
119 control_protocols.erase (p);
124 dlclose (cpi.descriptor->module);
128 static bool protocol_filter (const string& str, void *arg)
130 /* Not a dotfile, has a prefix before a period, suffix is "so" */
132 return str[0] != '.' && (str.length() > 3 && str.find (".so") == (str.length() - 3));
136 ControlProtocolManager::load_mandatory_protocols ()
142 for (list<ControlProtocolInfo*>::iterator i = control_protocol_info.begin(); i != control_protocol_info.end(); ++i) {
143 if ((*i)->mandatory && ((*i)->protocol == 0)) {
144 info << string_compose (_("Instantiating mandatory control protocol %1"), (*i)->name) << endmsg;
151 ControlProtocolManager::discover_control_protocols (string path)
153 vector<string *> *found;
156 cerr << "looking for control protocols in " << path << endl;
158 found = scanner (path, protocol_filter, 0, false, true);
160 for (vector<string*>::iterator i = found->begin(); i != found->end(); ++i) {
161 control_protocol_discover (**i);
169 ControlProtocolManager::control_protocol_discover (string path)
171 ControlProtocolDescriptor* descriptor;
173 if ((descriptor = get_descriptor (path)) != 0) {
175 ControlProtocolInfo* cpi = new ControlProtocolInfo ();
177 if (!descriptor->probe (descriptor)) {
178 info << string_compose (_("Control protocol %1 not usable"), descriptor->name) << endmsg;
181 cpi->descriptor = descriptor;
182 cpi->name = descriptor->name;
185 cpi->requested = false;
186 cpi->mandatory = descriptor->mandatory;
188 control_protocol_info.push_back (cpi);
190 info << string_compose(_("Control surface protocol discovered: \"%1\""), cpi->name) << endmsg;
193 dlclose (descriptor->module);
199 ControlProtocolDescriptor*
200 ControlProtocolManager::get_descriptor (string path)
203 ControlProtocolDescriptor *descriptor = 0;
204 ControlProtocolDescriptor* (*dfunc)(void);
207 if ((module = dlopen (path.c_str(), RTLD_NOW)) == 0) {
208 error << string_compose(_("ControlProtocolManager: cannot load module \"%1\" (%2)"), path, dlerror()) << endmsg;
213 dfunc = (ControlProtocolDescriptor* (*)(void)) dlsym (module, "protocol_descriptor");
215 if ((errstr = dlerror()) != 0) {
216 error << string_compose(_("ControlProtocolManager: module \"%1\" has no descriptor function."), path) << endmsg;
217 error << errstr << endmsg;
222 descriptor = dfunc();
224 descriptor->module = module;
233 ControlProtocolManager::foreach_known_protocol (sigc::slot<void,const ControlProtocolInfo*> method)
235 for (list<ControlProtocolInfo*>::iterator i = control_protocol_info.begin(); i != control_protocol_info.end(); ++i) {
241 ControlProtocolManager::cpi_by_name (string name)
243 for (list<ControlProtocolInfo*>::iterator i = control_protocol_info.begin(); i != control_protocol_info.end(); ++i) {
244 if (name == (*i)->name) {
252 ControlProtocolManager::set_state (const XMLNode& node)
255 XMLNodeConstIterator citer;
258 clist = node.children();
260 for (citer = clist.begin(); citer != clist.end(); ++citer) {
261 if ((*citer)->name() == X_("Protocol")) {
262 prop = (*citer)->property (X_("active"));
263 if (prop && prop->value() == X_("yes")) {
264 if ((prop = (*citer)->property (X_("name"))) != 0) {
265 ControlProtocolInfo* cpi = cpi_by_name (prop->value());
270 cpi->requested = true;
281 ControlProtocolManager::get_state (void)
283 XMLNode* root = new XMLNode (state_node_name);
284 LockMonitor lm (protocols_lock, __LINE__, __FILE__);
286 for (list<ControlProtocolInfo*>::iterator i = control_protocol_info.begin(); i != control_protocol_info.end(); ++i) {
287 XMLNode* child = new XMLNode (X_("Protocol"));
288 child->add_property (X_("name"), (*i)->name);
289 child->add_property (X_("active"), (*i)->protocol ? "yes" : "no");
290 root->add_child_nocopy (*child);