3 #include <pbd/compose.h>
5 #include <pbd/pathscanner.h>
7 #include <ardour/session.h>
8 #include <ardour/control_protocol.h>
9 #include <ardour/control_protocol_manager.h>
11 using namespace ARDOUR;
17 ControlProtocolManager* ControlProtocolManager::_instance = 0;
18 const string ControlProtocolManager::state_node_name = X_("ControlProtocols");
20 ControlProtocolManager::ControlProtocolManager ()
29 ControlProtocolManager::~ControlProtocolManager()
31 LockMonitor lm (protocols_lock, __LINE__, __FILE__);
33 for (list<ControlProtocol*>::iterator i = control_protocols.begin(); i != control_protocols.end(); ++i) {
37 control_protocols.clear ();
42 ControlProtocolManager::set_session (Session& s)
45 _session->going_away.connect (mem_fun (*this, &ControlProtocolManager::drop_session));
47 for (list<ControlProtocolInfo*>::iterator i = control_protocol_info.begin(); i != control_protocol_info.end(); ++i) {
48 if ((*i)->requested) {
50 (*i)->requested = false;
56 ControlProtocolManager::drop_session ()
61 LockMonitor lm (protocols_lock, __LINE__, __FILE__);
62 for (list<ControlProtocol*>::iterator p = control_protocols.begin(); p != control_protocols.end(); ++p) {
65 control_protocols.clear ();
70 ControlProtocolManager::instantiate (ControlProtocolInfo& cpi)
76 cpi.descriptor = get_descriptor (cpi.path);
78 if (cpi.descriptor == 0) {
79 error << string_compose (_("control protocol name \"%1\" has no descriptor"), cpi.name) << endmsg;
83 if ((cpi.protocol = cpi.descriptor->initialize (cpi.descriptor, _session)) == 0) {
84 error << string_compose (_("control protocol name \"%1\" could not be initialized"), cpi.name) << endmsg;
88 LockMonitor lm (protocols_lock, __LINE__, __FILE__);
89 control_protocols.push_back (cpi.protocol);
95 ControlProtocolManager::teardown (ControlProtocolInfo& cpi)
101 if (!cpi.descriptor) {
105 cpi.descriptor->destroy (cpi.descriptor, cpi.protocol);
108 LockMonitor lm (protocols_lock, __LINE__, __FILE__);
109 list<ControlProtocol*>::iterator p = find (control_protocols.begin(), control_protocols.end(), cpi.protocol);
110 if (p != control_protocols.end()) {
111 control_protocols.erase (p);
116 dlclose (cpi.descriptor->module);
120 static bool protocol_filter (const string& str, void *arg)
122 /* Not a dotfile, has a prefix before a period, suffix is "so" */
124 return str[0] != '.' && (str.length() > 3 && str.find (".so") == (str.length() - 3));
128 ControlProtocolManager::discover_control_protocols (string path)
130 vector<string *> *found;
133 cerr << "looking for control protocols in " << path << endl;
135 found = scanner (path, protocol_filter, 0, false, true);
137 for (vector<string*>::iterator i = found->begin(); i != found->end(); ++i) {
138 control_protocol_discover (**i);
146 ControlProtocolManager::control_protocol_discover (string path)
148 ControlProtocolDescriptor* descriptor;
150 if ((descriptor = get_descriptor (path)) != 0) {
152 ControlProtocolInfo* info = new ControlProtocolInfo ();
154 info->descriptor = descriptor;
155 info->name = descriptor->name;
158 info->requested = false;
160 control_protocol_info.push_back (info);
162 cerr << "discovered control surface protocol \"" << info->name << '"' << endl;
164 dlclose (descriptor->module);
171 ControlProtocolDescriptor*
172 ControlProtocolManager::get_descriptor (string path)
175 ControlProtocolDescriptor *descriptor = 0;
176 ControlProtocolDescriptor* (*dfunc)(void);
179 if ((module = dlopen (path.c_str(), RTLD_NOW)) == 0) {
180 error << string_compose(_("ControlProtocolManager: cannot load module \"%1\" (%2)"), path, dlerror()) << endmsg;
185 dfunc = (ControlProtocolDescriptor* (*)(void)) dlsym (module, "protocol_descriptor");
187 if ((errstr = dlerror()) != 0) {
188 error << string_compose(_("ControlProtocolManager: module \"%1\" has no descriptor function."), path) << endmsg;
189 error << errstr << endmsg;
194 descriptor = dfunc();
196 descriptor->module = module;
205 ControlProtocolManager::foreach_known_protocol (sigc::slot<void,const ControlProtocolInfo*> method)
207 for (list<ControlProtocolInfo*>::iterator i = control_protocol_info.begin(); i != control_protocol_info.end(); ++i) {
213 ControlProtocolManager::cpi_by_name (string name)
215 for (list<ControlProtocolInfo*>::iterator i = control_protocol_info.begin(); i != control_protocol_info.end(); ++i) {
216 if (name == (*i)->name) {
224 ControlProtocolManager::set_state (const XMLNode& node)
227 XMLNodeConstIterator citer;
230 clist = node.children();
232 for (citer = clist.begin(); citer != clist.end(); ++citer) {
233 if ((*citer)->name() == X_("Protocol")) {
234 if ((prop = (*citer)->property (X_("active"))) != 0) {
235 if (prop->value() == X_("yes")) {
236 if ((prop = (*citer)->property (X_("name"))) != 0) {
237 ControlProtocolInfo* cpi = cpi_by_name (prop->value());
242 cpi->requested = true;
253 ControlProtocolManager::get_state (void)
255 XMLNode* root = new XMLNode (state_node_name);
256 LockMonitor lm (protocols_lock, __LINE__, __FILE__);
258 for (list<ControlProtocolInfo*>::iterator i = control_protocol_info.begin(); i != control_protocol_info.end(); ++i) {
259 XMLNode* child = new XMLNode (X_("Protocol"));
260 child->add_property (X_("name"), (*i)->name);
261 child->add_property (X_("active"), (*i)->protocol ? "yes" : "no");
262 root->add_child_nocopy (*child);