3 #include <pbd/compose.h>
5 #include <pbd/pathscanner.h>
7 #include <control_protocol/control_protocol.h>
9 #include <ardour/session.h>
10 #include <ardour/control_protocol_manager.h>
15 using namespace ARDOUR;
20 ControlProtocolManager* ControlProtocolManager::_instance = 0;
21 const string ControlProtocolManager::state_node_name = X_("ControlProtocols");
23 ControlProtocolManager::ControlProtocolManager ()
32 ControlProtocolManager::~ControlProtocolManager()
34 Glib::Mutex::Lock lm (protocols_lock);
36 for (list<ControlProtocol*>::iterator i = control_protocols.begin(); i != control_protocols.end(); ++i) {
40 control_protocols.clear ();
45 ControlProtocolManager::set_session (Session& s)
48 _session->going_away.connect (mem_fun (*this, &ControlProtocolManager::drop_session));
50 for (list<ControlProtocolInfo*>::iterator i = control_protocol_info.begin(); i != control_protocol_info.end(); ++i) {
51 if ((*i)->requested || (*i)->mandatory) {
53 (*i)->requested = false;
59 ControlProtocolManager::drop_session ()
64 Glib::Mutex::Lock lm (protocols_lock);
65 for (list<ControlProtocol*>::iterator p = control_protocols.begin(); p != control_protocols.end(); ++p) {
68 control_protocols.clear ();
73 ControlProtocolManager::instantiate (ControlProtocolInfo& cpi)
79 cpi.descriptor = get_descriptor (cpi.path);
81 if (cpi.descriptor == 0) {
82 error << string_compose (_("control protocol name \"%1\" has no descriptor"), cpi.name) << endmsg;
86 if ((cpi.protocol = cpi.descriptor->initialize (cpi.descriptor, _session)) == 0) {
87 error << string_compose (_("control protocol name \"%1\" could not be initialized"), cpi.name) << endmsg;
91 Glib::Mutex::Lock lm (protocols_lock);
92 control_protocols.push_back (cpi.protocol);
98 ControlProtocolManager::teardown (ControlProtocolInfo& cpi)
104 if (!cpi.descriptor) {
112 cpi.descriptor->destroy (cpi.descriptor, cpi.protocol);
115 Glib::Mutex::Lock lm (protocols_lock);
116 list<ControlProtocol*>::iterator p = find (control_protocols.begin(), control_protocols.end(), cpi.protocol);
117 if (p != control_protocols.end()) {
118 control_protocols.erase (p);
123 dlclose (cpi.descriptor->module);
127 static bool protocol_filter (const string& str, void *arg)
129 /* Not a dotfile, has a prefix before a period, suffix is "so" */
131 return str[0] != '.' && (str.length() > 3 && str.find (".so") == (str.length() - 3));
135 ControlProtocolManager::load_mandatory_protocols ()
141 for (list<ControlProtocolInfo*>::iterator i = control_protocol_info.begin(); i != control_protocol_info.end(); ++i) {
142 if ((*i)->mandatory && ((*i)->protocol == 0)) {
143 info << string_compose (_("Instantiating mandatory control protocol %1"), (*i)->name) << endmsg;
150 ControlProtocolManager::discover_control_protocols (string path)
152 vector<string *> *found;
155 cerr << "looking for control protocols in " << path << endl;
157 found = scanner (path, protocol_filter, 0, false, true);
159 for (vector<string*>::iterator i = found->begin(); i != found->end(); ++i) {
160 control_protocol_discover (**i);
168 ControlProtocolManager::control_protocol_discover (string path)
170 ControlProtocolDescriptor* descriptor;
172 if ((descriptor = get_descriptor (path)) != 0) {
174 ControlProtocolInfo* cpi = new ControlProtocolInfo ();
176 if (!descriptor->probe (descriptor)) {
177 info << string_compose (_("Control protocol %1 not usable"), descriptor->name) << endmsg;
180 cpi->descriptor = descriptor;
181 cpi->name = descriptor->name;
184 cpi->requested = false;
185 cpi->mandatory = descriptor->mandatory;
187 control_protocol_info.push_back (cpi);
189 info << string_compose(_("Control surface protocol discovered: \"%1\""), cpi->name) << endmsg;
192 dlclose (descriptor->module);
198 ControlProtocolDescriptor*
199 ControlProtocolManager::get_descriptor (string path)
202 ControlProtocolDescriptor *descriptor = 0;
203 ControlProtocolDescriptor* (*dfunc)(void);
206 if ((module = dlopen (path.c_str(), RTLD_NOW)) == 0) {
207 error << string_compose(_("ControlProtocolManager: cannot load module \"%1\" (%2)"), path, dlerror()) << endmsg;
212 dfunc = (ControlProtocolDescriptor* (*)(void)) dlsym (module, "protocol_descriptor");
214 if ((errstr = dlerror()) != 0) {
215 error << string_compose(_("ControlProtocolManager: module \"%1\" has no descriptor function."), path) << endmsg;
216 error << errstr << endmsg;
221 descriptor = dfunc();
223 descriptor->module = module;
232 ControlProtocolManager::foreach_known_protocol (sigc::slot<void,const ControlProtocolInfo*> method)
234 for (list<ControlProtocolInfo*>::iterator i = control_protocol_info.begin(); i != control_protocol_info.end(); ++i) {
240 ControlProtocolManager::cpi_by_name (string name)
242 for (list<ControlProtocolInfo*>::iterator i = control_protocol_info.begin(); i != control_protocol_info.end(); ++i) {
243 if (name == (*i)->name) {
251 ControlProtocolManager::set_state (const XMLNode& node)
254 XMLNodeConstIterator citer;
257 clist = node.children();
259 for (citer = clist.begin(); citer != clist.end(); ++citer) {
260 if ((*citer)->name() == X_("Protocol")) {
261 prop = (*citer)->property (X_("active"));
262 if (prop && prop->value() == X_("yes")) {
263 if ((prop = (*citer)->property (X_("name"))) != 0) {
264 ControlProtocolInfo* cpi = cpi_by_name (prop->value());
269 cpi->requested = true;
280 ControlProtocolManager::get_state (void)
282 XMLNode* root = new XMLNode (state_node_name);
283 Glib::Mutex::Lock lm (protocols_lock);
285 for (list<ControlProtocolInfo*>::iterator i = control_protocol_info.begin(); i != control_protocol_info.end(); ++i) {
286 XMLNode* child = new XMLNode (X_("Protocol"));
287 child->add_property (X_("name"), (*i)->name);
288 child->add_property (X_("active"), (*i)->protocol ? "yes" : "no");
289 root->add_child_nocopy (*child);