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()
30 ControlProtocolManager::startup (Session& s)
32 list<ControlProtocolInfo *>::iterator i;
34 for (i = control_protocol_info.begin(); i != control_protocol_info.end(); ++i) {
36 ControlProtocolInfo* cpi = (*i);
38 if (cpi->name == "Tranzport") {
40 cpi->descriptor = get_descriptor ((*i)->path);
42 if (cpi->descriptor == 0) {
43 error << string_compose (_("control protocol name \"%1\" has no descriptor"), cpi->name) << endmsg;
47 if ((cpi->protocol = cpi->descriptor->initialize (cpi->descriptor, &s)) == 0) {
48 error << string_compose (_("control protocol name \"%1\" could not be initialized"), cpi->name) << endmsg;
53 LockMonitor lm (protocols_lock, __LINE__, __FILE__);
54 control_protocols.push_back (cpi->protocol);
57 cerr << "start " << cpi->name << endl;
58 cpi->protocol->init ();
60 cerr << "activate " << cpi->name << endl;
61 cpi->protocol->set_active (true);
63 cerr << cpi->name << " now running\n";
69 ControlProtocolManager::instantiate (Session& session, string name)
71 list<ControlProtocolInfo *>::iterator i;
73 for (i = control_protocol_info.begin(); i != control_protocol_info.end(); ++i) {
74 if ((*i)->name == name) {
79 if (i == control_protocol_info.end()) {
80 error << string_compose (_("control protocol name \"%1\" is unknown"), name) << endmsg;
84 ControlProtocolInfo* cpi = (*i);
86 cpi->descriptor = get_descriptor ((*i)->path);
88 if (cpi->descriptor == 0) {
89 error << string_compose (_("control protocol name \"%1\" has no descriptor"), name) << endmsg;
93 if ((cpi->protocol = cpi->descriptor->initialize (cpi->descriptor, &session)) == 0) {
94 error << string_compose (_("control protocol name \"%1\" could not be initialized"), name) << endmsg;
98 LockMonitor lm (protocols_lock, __LINE__, __FILE__);
99 control_protocols.push_back (cpi->protocol);
100 return cpi->protocol;
104 ControlProtocolManager::teardown (string name)
106 for (list<ControlProtocolInfo*>::iterator i = control_protocol_info.begin(); i != control_protocol_info.end(); ++i) {
107 ControlProtocolInfo* cpi = *i;
109 if (cpi->name == name && cpi->descriptor && cpi->protocol) {
110 cpi->descriptor->destroy (cpi->descriptor, cpi->protocol);
113 LockMonitor lm (protocols_lock, __LINE__, __FILE__);
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);
128 static bool protocol_filter (const string& str, void *arg)
130 /* Not a dotfile, has a prefix before a period, suffix is "so" */
132 return str[0] != '.' && (str.length() > 3 && str.find (".so") == (str.length() - 3));
136 ControlProtocolManager::discover_control_protocols (string path)
138 vector<string *> *found;
141 cerr << "CP Manager looking for surfaces\n";
143 found = scanner (path, protocol_filter, 0, false, true);
145 for (vector<string*>::iterator i = found->begin(); i != found->end(); ++i) {
146 cerr << "CP Manager looking at " << **i << endl;
147 control_protocol_discover (**i);
155 ControlProtocolManager::control_protocol_discover (string path)
157 ControlProtocolDescriptor* descriptor;
159 if ((descriptor = get_descriptor (path)) != 0) {
161 ControlProtocolInfo* info = new ControlProtocolInfo ();
163 info->descriptor = descriptor;
164 info->name = descriptor->name;
167 control_protocol_info.push_back (info);
169 cerr << "Found \"" << info->name << "\"\n";
171 dlclose (descriptor->module);
174 cerr << "no descriptor\n";
180 ControlProtocolDescriptor*
181 ControlProtocolManager::get_descriptor (string path)
184 ControlProtocolDescriptor *descriptor = 0;
185 ControlProtocolDescriptor* (*dfunc)(void);
188 if ((module = dlopen (path.c_str(), RTLD_NOW)) == 0) {
189 error << string_compose(_("ControlProtocolManager: cannot load module \"%1\" (%2)"), path, dlerror()) << endmsg;
194 dfunc = (ControlProtocolDescriptor* (*)(void)) dlsym (module, "protocol_descriptor");
196 if ((errstr = dlerror()) != 0) {
197 error << string_compose(_("ControlProtocolManager: module \"%1\" has no descriptor function."), path) << endmsg;
198 error << errstr << endmsg;
203 descriptor = dfunc();
205 descriptor->module = module;