r269@gandalf: fugalh | 2006-08-03 20:18:05 -0600
[ardour.git] / libs / ardour / control_protocol_manager.cc
1 #include <dlfcn.h>
2
3 #include <pbd/compose.h>
4 #include <pbd/error.h>
5 #include <pbd/pathscanner.h>
6
7 #include <control_protocol/control_protocol.h>
8
9 #include <ardour/session.h>
10 #include <ardour/control_protocol_manager.h>
11
12 using namespace ARDOUR;
13 using namespace std;
14 using namespace PBD;
15
16 #include "i18n.h"
17
18 ControlProtocolManager* ControlProtocolManager::_instance = 0;
19 const string ControlProtocolManager::state_node_name = X_("ControlProtocols");
20
21 ControlProtocolManager::ControlProtocolManager ()
22 {
23         if (_instance == 0) {
24                 _instance = this;
25         }
26
27         _session = 0;
28 }
29
30 ControlProtocolManager::~ControlProtocolManager()
31 {
32         Glib::Mutex::Lock lm (protocols_lock);
33
34         for (list<ControlProtocol*>::iterator i = control_protocols.begin(); i != control_protocols.end(); ++i) {
35                 delete (*i);
36         }
37
38         control_protocols.clear ();
39                 
40 }
41
42 void
43 ControlProtocolManager::set_session (Session& s)
44 {
45         _session = &s;
46         _session->going_away.connect (mem_fun (*this, &ControlProtocolManager::drop_session));
47
48         for (list<ControlProtocolInfo*>::iterator i = control_protocol_info.begin(); i != control_protocol_info.end(); ++i) {
49                 if ((*i)->requested || (*i)->mandatory) {
50                         instantiate (**i);
51                         (*i)->requested = false;
52
53                         if ((*i)->state) {
54                                 (*i)->protocol->set_state (*(*i)->state);
55                         }
56                 }
57         }
58 }
59
60 void
61 ControlProtocolManager::drop_session ()
62 {
63         _session = 0;
64
65         {
66                 Glib::Mutex::Lock lm (protocols_lock);
67                 for (list<ControlProtocol*>::iterator p = control_protocols.begin(); p != control_protocols.end(); ++p) {
68                         delete *p;
69                 }
70                 control_protocols.clear ();
71         }
72 }
73
74 ControlProtocol*
75 ControlProtocolManager::instantiate (ControlProtocolInfo& cpi)
76 {
77         if (_session == 0) {
78                 return 0;
79         }
80
81         cpi.descriptor = get_descriptor (cpi.path);
82
83         if (cpi.descriptor == 0) {
84                 error << string_compose (_("control protocol name \"%1\" has no descriptor"), cpi.name) << endmsg;
85                 return 0;
86         }
87
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;
90                 return 0;
91         }
92
93         Glib::Mutex::Lock lm (protocols_lock);
94         control_protocols.push_back (cpi.protocol);
95
96         return cpi.protocol;
97 }
98
99 int
100 ControlProtocolManager::teardown (ControlProtocolInfo& cpi)
101 {
102         if (!cpi.protocol) {
103                 return 0;
104         }
105
106         if (!cpi.descriptor) {
107                 return 0;
108         }
109
110         if (cpi.mandatory) {
111                 return 0;
112         }
113
114         cpi.descriptor->destroy (cpi.descriptor, cpi.protocol);
115         
116         {
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);
121                 }
122         }
123         
124         cpi.protocol = 0;
125         dlclose (cpi.descriptor->module);
126         return 0;
127 }
128
129 static bool protocol_filter (const string& str, void *arg)
130 {
131         /* Not a dotfile, has a prefix before a period, suffix is "so" */
132         
133         return str[0] != '.' && (str.length() > 3 && str.find (".so") == (str.length() - 3));
134 }
135
136 void
137 ControlProtocolManager::load_mandatory_protocols ()
138 {
139         if (_session == 0) {
140                 return;
141         }
142
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;
146                         instantiate (**i);
147                 }
148         }
149 }
150
151 void
152 ControlProtocolManager::discover_control_protocols (string path)
153 {
154         vector<string *> *found;
155         PathScanner scanner;
156
157         cerr << "looking for control protocols in " << path << endl;
158
159         found = scanner (path, protocol_filter, 0, false, true);
160
161         for (vector<string*>::iterator i = found->begin(); i != found->end(); ++i) {
162                 control_protocol_discover (**i);
163                 delete *i;
164         }
165
166         delete found;
167 }
168
169 int
170 ControlProtocolManager::control_protocol_discover (string path)
171 {
172         ControlProtocolDescriptor* descriptor;
173
174         if ((descriptor = get_descriptor (path)) != 0) {
175
176                 ControlProtocolInfo* cpi = new ControlProtocolInfo ();
177
178                 if (!descriptor->probe (descriptor)) {
179                         info << string_compose (_("Control protocol %1 not usable"), descriptor->name) << endmsg;
180                 } else {
181
182                         cpi->descriptor = descriptor;
183                         cpi->name = descriptor->name;
184                         cpi->path = path;
185                         cpi->protocol = 0;
186                         cpi->requested = false;
187                         cpi->mandatory = descriptor->mandatory;
188                         cpi->state = 0;
189                         
190                         control_protocol_info.push_back (cpi);
191                         
192                         info << string_compose(_("Control surface protocol discovered: \"%1\""), cpi->name) << endmsg;
193                 }
194
195                 dlclose (descriptor->module);
196         }
197
198         return 0;
199 }
200
201 ControlProtocolDescriptor*
202 ControlProtocolManager::get_descriptor (string path)
203 {
204         void *module;
205         ControlProtocolDescriptor *descriptor = 0;
206         ControlProtocolDescriptor* (*dfunc)(void);
207         const char *errstr;
208
209         if ((module = dlopen (path.c_str(), RTLD_NOW)) == 0) {
210                 error << string_compose(_("ControlProtocolManager: cannot load module \"%1\" (%2)"), path, dlerror()) << endmsg;
211                 return 0;
212         }
213
214
215         dfunc = (ControlProtocolDescriptor* (*)(void)) dlsym (module, "protocol_descriptor");
216
217         if ((errstr = dlerror()) != 0) {
218                 error << string_compose(_("ControlProtocolManager: module \"%1\" has no descriptor function."), path) << endmsg;
219                 error << errstr << endmsg;
220                 dlclose (module);
221                 return 0;
222         }
223
224         descriptor = dfunc();
225         if (descriptor) {
226                 descriptor->module = module;
227         } else {
228                 dlclose (module);
229         }
230
231         return descriptor;
232 }
233
234 void
235 ControlProtocolManager::foreach_known_protocol (sigc::slot<void,const ControlProtocolInfo*> method)
236 {
237         for (list<ControlProtocolInfo*>::iterator i = control_protocol_info.begin(); i != control_protocol_info.end(); ++i) {
238                 method (*i);
239         }
240 }
241
242 ControlProtocolInfo*
243 ControlProtocolManager::cpi_by_name (string name)
244 {
245         for (list<ControlProtocolInfo*>::iterator i = control_protocol_info.begin(); i != control_protocol_info.end(); ++i) {
246                 if (name == (*i)->name) {
247                         return *i;
248                 }
249         }
250         return 0;
251 }
252
253 int
254 ControlProtocolManager::set_state (const XMLNode& node)
255 {
256         XMLNodeList clist;
257         XMLNodeConstIterator citer;
258         XMLProperty* prop;
259
260         clist = node.children();
261
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());
268                                         if (cpi) {
269                                                 if (_session) {
270                                                         instantiate (*cpi);
271                                                 } else {
272                                                         cpi->requested = true;
273                                                 }
274                                         }
275                                 }
276                         }
277                 }    
278         }
279         return 0;
280 }
281
282 XMLNode&
283 ControlProtocolManager::get_state (void)
284 {
285         XMLNode* root = new XMLNode (state_node_name);
286         Glib::Mutex::Lock lm (protocols_lock);
287
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);
293         }
294
295         return *root;
296 }