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