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;
57 ControlProtocolManager::drop_session ()
62 Glib::Mutex::Lock lm (protocols_lock);
63 for (list<ControlProtocol*>::iterator p = control_protocols.begin(); p != control_protocols.end(); ++p) {
66 control_protocols.clear ();
71 ControlProtocolManager::instantiate (ControlProtocolInfo& cpi)
77 cpi.descriptor = get_descriptor (cpi.path);
79 if (cpi.descriptor == 0) {
80 error << string_compose (_("control protocol name \"%1\" has no descriptor"), cpi.name) << endmsg;
84 if ((cpi.protocol = cpi.descriptor->initialize (cpi.descriptor, _session)) == 0) {
85 error << string_compose (_("control protocol name \"%1\" could not be initialized"), cpi.name) << endmsg;
89 Glib::Mutex::Lock lm (protocols_lock);
90 control_protocols.push_back (cpi.protocol);
96 ControlProtocolManager::teardown (ControlProtocolInfo& cpi)
102 if (!cpi.descriptor) {
110 cpi.descriptor->destroy (cpi.descriptor, cpi.protocol);
113 Glib::Mutex::Lock lm (protocols_lock);
114 list<ControlProtocol*>::iterator p = find (control_protocols.begin(), control_protocols.end(), cpi.protocol);
115 if (p != control_protocols.end()) {
116 control_protocols.erase (p);
121 dlclose (cpi.descriptor->module);
125 static bool protocol_filter (const string& str, void *arg)
127 /* Not a dotfile, has a prefix before a period, suffix is "so" */
129 return str[0] != '.' && (str.length() > 3 && str.find (".so") == (str.length() - 3));
133 ControlProtocolManager::load_mandatory_protocols ()
139 for (list<ControlProtocolInfo*>::iterator i = control_protocol_info.begin(); i != control_protocol_info.end(); ++i) {
140 if ((*i)->mandatory && ((*i)->protocol == 0)) {
141 info << string_compose (_("Instantiating mandatory control protocol %1"), (*i)->name) << endmsg;
148 ControlProtocolManager::discover_control_protocols (string path)
150 vector<string *> *found;
153 cerr << "looking for control protocols in " << path << endl;
155 found = scanner (path, protocol_filter, 0, false, true);
157 for (vector<string*>::iterator i = found->begin(); i != found->end(); ++i) {
158 control_protocol_discover (**i);
166 ControlProtocolManager::control_protocol_discover (string path)
168 ControlProtocolDescriptor* descriptor;
170 if ((descriptor = get_descriptor (path)) != 0) {
172 ControlProtocolInfo* cpi = new ControlProtocolInfo ();
174 if (!descriptor->probe (descriptor)) {
175 info << string_compose (_("Control protocol %1 not usable"), descriptor->name) << endmsg;
178 cpi->descriptor = descriptor;
179 cpi->name = descriptor->name;
182 cpi->requested = false;
183 cpi->mandatory = descriptor->mandatory;
185 control_protocol_info.push_back (cpi);
187 info << string_compose(_("Control surface protocol discovered: \"%1\""), cpi->name) << endmsg;
190 dlclose (descriptor->module);
196 ControlProtocolDescriptor*
197 ControlProtocolManager::get_descriptor (string path)
200 ControlProtocolDescriptor *descriptor = 0;
201 ControlProtocolDescriptor* (*dfunc)(void);
204 if ((module = dlopen (path.c_str(), RTLD_NOW)) == 0) {
205 error << string_compose(_("ControlProtocolManager: cannot load module \"%1\" (%2)"), path, dlerror()) << endmsg;
210 dfunc = (ControlProtocolDescriptor* (*)(void)) dlsym (module, "protocol_descriptor");
212 if ((errstr = dlerror()) != 0) {
213 error << string_compose(_("ControlProtocolManager: module \"%1\" has no descriptor function."), path) << endmsg;
214 error << errstr << endmsg;
219 descriptor = dfunc();
221 descriptor->module = module;
230 ControlProtocolManager::foreach_known_protocol (sigc::slot<void,const ControlProtocolInfo*> method)
232 for (list<ControlProtocolInfo*>::iterator i = control_protocol_info.begin(); i != control_protocol_info.end(); ++i) {
238 ControlProtocolManager::cpi_by_name (string name)
240 for (list<ControlProtocolInfo*>::iterator i = control_protocol_info.begin(); i != control_protocol_info.end(); ++i) {
241 if (name == (*i)->name) {
249 ControlProtocolManager::set_state (const XMLNode& node)
252 XMLNodeConstIterator citer;
255 clist = node.children();
257 for (citer = clist.begin(); citer != clist.end(); ++citer) {
258 if ((*citer)->name() == X_("Protocol")) {
259 prop = (*citer)->property (X_("active"));
260 if (prop && prop->value() == X_("yes")) {
261 if ((prop = (*citer)->property (X_("name"))) != 0) {
262 ControlProtocolInfo* cpi = cpi_by_name (prop->value());
267 cpi->requested = true;
278 ControlProtocolManager::get_state (void)
280 XMLNode* root = new XMLNode (state_node_name);
281 Glib::Mutex::Lock lm (protocols_lock);
283 for (list<ControlProtocolInfo*>::iterator i = control_protocol_info.begin(); i != control_protocol_info.end(); ++i) {
284 XMLNode* child = new XMLNode (X_("Protocol"));
285 child->add_property (X_("name"), (*i)->name);
286 child->add_property (X_("active"), (*i)->protocol ? "yes" : "no");
287 root->add_child_nocopy (*child);