3 #include <pbd/compose.h>
5 #include <pbd/pathscanner.h>
7 #include <ardour/control_protocol.h>
8 #include <ardour/control_protocol_manager.h>
10 using namespace ARDOUR;
16 ControlProtocolManager* ControlProtocolManager::_instance = 0;
18 ControlProtocolManager::ControlProtocolManager ()
25 ControlProtocolManager::~ControlProtocolManager()
27 LockMonitor lm (protocols_lock, __LINE__, __FILE__);
29 for (list<ControlProtocol*>::iterator i = control_protocols.begin(); i != control_protocols.end(); ++i) {
33 control_protocols.clear ();
38 ControlProtocolManager::startup (Session& s)
40 list<ControlProtocolInfo *>::iterator i;
42 for (i = control_protocol_info.begin(); i != control_protocol_info.end(); ++i) {
44 ControlProtocolInfo* cpi = (*i);
46 if (cpi->name == "Tranzport") {
48 cpi->descriptor = get_descriptor ((*i)->path);
50 if (cpi->descriptor == 0) {
51 error << string_compose (_("control protocol name \"%1\" has no descriptor"), cpi->name) << endmsg;
55 if ((cpi->protocol = cpi->descriptor->initialize (cpi->descriptor, &s)) == 0) {
56 error << string_compose (_("control protocol name \"%1\" could not be initialized"), cpi->name) << endmsg;
61 LockMonitor lm (protocols_lock, __LINE__, __FILE__);
62 control_protocols.push_back (cpi->protocol);
65 cpi->protocol->init ();
66 cpi->protocol->set_active (true);
72 ControlProtocolManager::instantiate (Session& session, string name)
74 list<ControlProtocolInfo *>::iterator i;
76 for (i = control_protocol_info.begin(); i != control_protocol_info.end(); ++i) {
77 if ((*i)->name == name) {
82 if (i == control_protocol_info.end()) {
83 error << string_compose (_("control protocol name \"%1\" is unknown"), name) << endmsg;
87 ControlProtocolInfo* cpi = (*i);
89 cpi->descriptor = get_descriptor ((*i)->path);
91 if (cpi->descriptor == 0) {
92 error << string_compose (_("control protocol name \"%1\" has no descriptor"), name) << endmsg;
96 if ((cpi->protocol = cpi->descriptor->initialize (cpi->descriptor, &session)) == 0) {
97 error << string_compose (_("control protocol name \"%1\" could not be initialized"), name) << endmsg;
101 LockMonitor lm (protocols_lock, __LINE__, __FILE__);
102 control_protocols.push_back (cpi->protocol);
103 return cpi->protocol;
107 ControlProtocolManager::teardown (string name)
109 for (list<ControlProtocolInfo*>::iterator i = control_protocol_info.begin(); i != control_protocol_info.end(); ++i) {
110 ControlProtocolInfo* cpi = *i;
112 if (cpi->name == name && cpi->descriptor && cpi->protocol) {
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);
131 static bool protocol_filter (const string& str, void *arg)
133 /* Not a dotfile, has a prefix before a period, suffix is "so" */
135 return str[0] != '.' && (str.length() > 3 && str.find (".so") == (str.length() - 3));
139 ControlProtocolManager::discover_control_protocols (string path)
141 vector<string *> *found;
144 cerr << "CP Manager looking for surfaces\n";
146 found = scanner (path, protocol_filter, 0, false, true);
148 for (vector<string*>::iterator i = found->begin(); i != found->end(); ++i) {
149 cerr << "CP Manager looking at " << **i << endl;
150 control_protocol_discover (**i);
158 ControlProtocolManager::control_protocol_discover (string path)
160 ControlProtocolDescriptor* descriptor;
162 if ((descriptor = get_descriptor (path)) != 0) {
164 ControlProtocolInfo* info = new ControlProtocolInfo ();
166 info->descriptor = descriptor;
167 info->name = descriptor->name;
170 control_protocol_info.push_back (info);
172 cerr << "Found \"" << info->name << "\"\n";
174 dlclose (descriptor->module);
177 cerr << "no descriptor\n";
183 ControlProtocolDescriptor*
184 ControlProtocolManager::get_descriptor (string path)
187 ControlProtocolDescriptor *descriptor = 0;
188 ControlProtocolDescriptor* (*dfunc)(void);
191 if ((module = dlopen (path.c_str(), RTLD_NOW)) == 0) {
192 error << string_compose(_("ControlProtocolManager: cannot load module \"%1\" (%2)"), path, dlerror()) << endmsg;
197 dfunc = (ControlProtocolDescriptor* (*)(void)) dlsym (module, "protocol_descriptor");
199 if ((errstr = dlerror()) != 0) {
200 error << string_compose(_("ControlProtocolManager: module \"%1\" has no descriptor function."), path) << endmsg;
201 error << errstr << endmsg;
206 descriptor = dfunc();
208 descriptor->module = module;