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>
12 using namespace ARDOUR;
18 ControlProtocolManager* ControlProtocolManager::_instance = 0;
19 const string ControlProtocolManager::state_node_name = X_("ControlProtocols");
21 ControlProtocolManager::ControlProtocolManager ()
30 ControlProtocolManager::~ControlProtocolManager()
32 Glib::Mutex::Lock lm (protocols_lock);
34 for (list<ControlProtocol*>::iterator i = control_protocols.begin(); i != control_protocols.end(); ++i) {
38 control_protocols.clear ();
43 ControlProtocolManager::set_session (Session& s)
46 _session->going_away.connect (mem_fun (*this, &ControlProtocolManager::drop_session));
48 for (list<ControlProtocolInfo*>::iterator i = control_protocol_info.begin(); i != control_protocol_info.end(); ++i) {
49 if ((*i)->requested || (*i)->mandatory) {
51 (*i)->requested = false;
54 (*i)->protocol->set_state (*(*i)->state);
61 ControlProtocolManager::drop_session ()
66 Glib::Mutex::Lock lm (protocols_lock);
67 for (list<ControlProtocol*>::iterator p = control_protocols.begin(); p != control_protocols.end(); ++p) {
70 control_protocols.clear ();
75 ControlProtocolManager::instantiate (ControlProtocolInfo& cpi)
81 cpi.descriptor = get_descriptor (cpi.path);
83 if (cpi.descriptor == 0) {
84 error << string_compose (_("control protocol name \"%1\" has no descriptor"), cpi.name) << endmsg;
88 if ((cpi.protocol = cpi.descriptor->initialize (cpi.descriptor, _session)) == 0) {
89 error << string_compose (_("control protocol name \"%1\" could not be initialized"), cpi.name) << endmsg;
93 Glib::Mutex::Lock lm (protocols_lock);
94 control_protocols.push_back (cpi.protocol);
100 ControlProtocolManager::teardown (ControlProtocolInfo& cpi)
106 if (!cpi.descriptor) {
114 cpi.descriptor->destroy (cpi.descriptor, cpi.protocol);
117 Glib::Mutex::Lock lm (protocols_lock);
118 list<ControlProtocol*>::iterator p = find (control_protocols.begin(), control_protocols.end(), cpi.protocol);
119 if (p != control_protocols.end()) {
120 control_protocols.erase (p);
125 dlclose (cpi.descriptor->module);
129 static bool protocol_filter (const string& str, void *arg)
131 /* Not a dotfile, has a prefix before a period, suffix is "so" */
133 return str[0] != '.' && (str.length() > 3 && str.find (".so") == (str.length() - 3));
137 ControlProtocolManager::load_mandatory_protocols ()
143 for (list<ControlProtocolInfo*>::iterator i = control_protocol_info.begin(); i != control_protocol_info.end(); ++i) {
144 if ((*i)->mandatory && ((*i)->protocol == 0)) {
145 info << string_compose (_("Instantiating mandatory control protocol %1"), (*i)->name) << endmsg;
152 ControlProtocolManager::discover_control_protocols (string path)
154 vector<string *> *found;
157 cerr << "looking for control protocols in " << path << endl;
159 found = scanner (path, protocol_filter, 0, false, true);
161 for (vector<string*>::iterator i = found->begin(); i != found->end(); ++i) {
162 control_protocol_discover (**i);
170 ControlProtocolManager::control_protocol_discover (string path)
172 ControlProtocolDescriptor* descriptor;
174 if ((descriptor = get_descriptor (path)) != 0) {
176 ControlProtocolInfo* cpi = new ControlProtocolInfo ();
178 if (!descriptor->probe (descriptor)) {
179 info << string_compose (_("Control protocol %1 not usable"), descriptor->name) << endmsg;
182 cpi->descriptor = descriptor;
183 cpi->name = descriptor->name;
186 cpi->requested = false;
187 cpi->mandatory = descriptor->mandatory;
190 control_protocol_info.push_back (cpi);
192 info << string_compose(_("Control surface protocol discovered: \"%1\""), cpi->name) << endmsg;
195 dlclose (descriptor->module);
201 ControlProtocolDescriptor*
202 ControlProtocolManager::get_descriptor (string path)
205 ControlProtocolDescriptor *descriptor = 0;
206 ControlProtocolDescriptor* (*dfunc)(void);
209 if ((module = dlopen (path.c_str(), RTLD_NOW)) == 0) {
210 error << string_compose(_("ControlProtocolManager: cannot load module \"%1\" (%2)"), path, dlerror()) << endmsg;
215 dfunc = (ControlProtocolDescriptor* (*)(void)) dlsym (module, "protocol_descriptor");
217 if ((errstr = dlerror()) != 0) {
218 error << string_compose(_("ControlProtocolManager: module \"%1\" has no descriptor function."), path) << endmsg;
219 error << errstr << endmsg;
224 descriptor = dfunc();
226 descriptor->module = module;
235 ControlProtocolManager::foreach_known_protocol (sigc::slot<void,const ControlProtocolInfo*> method)
237 for (list<ControlProtocolInfo*>::iterator i = control_protocol_info.begin(); i != control_protocol_info.end(); ++i) {
243 ControlProtocolManager::cpi_by_name (string name)
245 for (list<ControlProtocolInfo*>::iterator i = control_protocol_info.begin(); i != control_protocol_info.end(); ++i) {
246 if (name == (*i)->name) {
254 ControlProtocolManager::set_state (const XMLNode& node)
257 XMLNodeConstIterator citer;
260 clist = node.children();
262 for (citer = clist.begin(); citer != clist.end(); ++citer) {
263 if ((*citer)->name() == X_("Protocol")) {
264 prop = (*citer)->property (X_("active"));
265 if (prop && prop->value() == X_("yes")) {
266 if ((prop = (*citer)->property (X_("name"))) != 0) {
267 ControlProtocolInfo* cpi = cpi_by_name (prop->value());
272 cpi->requested = true;
283 ControlProtocolManager::get_state (void)
285 XMLNode* root = new XMLNode (state_node_name);
286 Glib::Mutex::Lock lm (protocols_lock);
288 for (list<ControlProtocolInfo*>::iterator i = control_protocol_info.begin(); i != control_protocol_info.end(); ++i) {
289 XMLNode* child = new XMLNode (X_("Protocol"));
290 child->add_property (X_("name"), (*i)->name);
291 child->add_property (X_("active"), (*i)->protocol ? "yes" : "no");
292 root->add_child_nocopy (*child);