Prevent deadlock when removing routes w/o engine
[ardour.git] / libs / ardour / control_protocol_manager.cc
1 /*
2     Copyright (C) 2000-2007 Paul Davis
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include <glibmm/module.h>
21
22 #include <glibmm/fileutils.h>
23
24 #include "pbd/compose.h"
25 #include "pbd/event_loop.h"
26 #include "pbd/file_utils.h"
27 #include "pbd/error.h"
28
29 #include "control_protocol/control_protocol.h"
30
31 #include "ardour/debug.h"
32 #include "ardour/control_protocol_manager.h"
33
34 #include "ardour/search_paths.h"
35 #include "ardour/selection.h"
36 #include "ardour/session.h"
37
38 using namespace ARDOUR;
39 using namespace std;
40 using namespace PBD;
41
42 #include "pbd/i18n.h"
43
44 ControlProtocolManager* ControlProtocolManager::_instance = 0;
45 const string ControlProtocolManager::state_node_name = X_("ControlProtocols");
46 PBD::Signal1<void,StripableNotificationListPtr> ControlProtocolManager::StripableSelectionChanged;
47
48 ControlProtocolInfo::~ControlProtocolInfo ()
49 {
50         if (protocol && descriptor) {
51                 descriptor->destroy (descriptor, protocol);
52                 protocol = 0;
53         }
54
55         delete state; state = 0;
56
57         if (descriptor) {
58                 delete (Glib::Module*) descriptor->module;
59                 descriptor = 0;
60         }
61 }
62
63 ControlProtocolManager::ControlProtocolManager ()
64 {
65 }
66
67 ControlProtocolManager::~ControlProtocolManager()
68 {
69         Glib::Threads::RWLock::WriterLock lm (protocols_lock);
70
71         for (list<ControlProtocol*>::iterator i = control_protocols.begin(); i != control_protocols.end(); ++i) {
72                 delete (*i);
73         }
74
75         control_protocols.clear ();
76
77
78         for (list<ControlProtocolInfo*>::iterator p = control_protocol_info.begin(); p != control_protocol_info.end(); ++p) {
79                 (*p)->protocol = 0; // protocol was already destroyed above.
80                 delete (*p);
81         }
82
83         control_protocol_info.clear();
84 }
85
86 void
87 ControlProtocolManager::set_session (Session* s)
88 {
89         SessionHandlePtr::set_session (s);
90
91         if (!_session) {
92                 return;
93         }
94
95         {
96                 Glib::Threads::RWLock::ReaderLock lm (protocols_lock);
97
98                 for (list<ControlProtocolInfo*>::iterator i = control_protocol_info.begin(); i != control_protocol_info.end(); ++i) {
99                         if ((*i)->requested || (*i)->mandatory) {
100                                 (void) activate (**i);
101                         }
102                 }
103         }
104
105         CoreSelection::StripableAutomationControls sac;
106         _session->selection().get_stripables (sac);
107
108         if (!sac.empty()) {
109                 StripableNotificationListPtr v (new StripableNotificationList);
110                 for (CoreSelection::StripableAutomationControls::iterator i = sac.begin(); i != sac.end(); ++i) {
111                         if ((*i).stripable) {
112                                 v->push_back (boost::weak_ptr<Stripable> ((*i).stripable));
113                         }
114                 }
115                 if (!v->empty()) {
116                         StripableSelectionChanged (v); /* EMIT SIGNAL */
117                 }
118         }
119 }
120
121 int
122 ControlProtocolManager::activate (ControlProtocolInfo& cpi)
123 {
124         ControlProtocol* cp;
125
126         cpi.requested = true;
127
128         if ((cp = instantiate (cpi)) == 0) {
129                 return -1;
130         }
131
132         /* we split the set_state() and set_active() operations so that
133            protocols that need state to configure themselves (e.g. "What device
134            is connected, or supposed to be connected?") can get it before
135            actually starting any interaction.
136         */
137
138         if (cpi.state) {
139                 /* force this by tweaking the internals of the state
140                  * XMLNode. Ugh.
141                  */
142                 cp->set_state (*cpi.state, Stateful::loading_state_version);
143         } else {
144                 /* guarantee a call to
145                    set_state() whether we have
146                    existing state or not
147                 */
148                 cp->set_state (XMLNode(""), Stateful::loading_state_version);
149         }
150
151         if (cp->set_active (true)) {
152                 error << string_compose (_("Control protocol support for %1 failed to activate"), cpi.name) << endmsg;
153                 teardown (cpi, false);
154         }
155
156         return 0;
157 }
158
159 int
160 ControlProtocolManager::deactivate (ControlProtocolInfo& cpi)
161 {
162         cpi.requested = false;
163         return teardown (cpi, true);
164 }
165
166 void
167 ControlProtocolManager::session_going_away()
168 {
169         SessionHandlePtr::session_going_away ();
170         /* Session::destroy() will explicitly call drop_protocols() so we don't
171          * have to worry about that here.
172          */
173 }
174
175 void
176 ControlProtocolManager::drop_protocols ()
177 {
178         /* called explicitly by Session::destroy() so that we can clean up
179          * before the process cycle stops and ports vanish.
180          */
181
182         Glib::Threads::RWLock::WriterLock lm (protocols_lock);
183
184         for (list<ControlProtocol*>::iterator p = control_protocols.begin(); p != control_protocols.end(); ++p) {
185                 delete *p;
186         }
187
188         control_protocols.clear ();
189
190         for (list<ControlProtocolInfo*>::iterator p = control_protocol_info.begin(); p != control_protocol_info.end(); ++p) {
191                 // mark existing protocols as requested
192                 // otherwise the ControlProtocol instances are not recreated in set_session
193                 if ((*p)->protocol) {
194                         (*p)->requested = true;
195                         (*p)->protocol = 0;
196                         ProtocolStatusChange (*p); /* EMIT SIGNAL */
197                 }
198         }
199 }
200
201 ControlProtocol*
202 ControlProtocolManager::instantiate (ControlProtocolInfo& cpi)
203 {
204         /* CALLER MUST HOLD LOCK */
205
206         if (_session == 0) {
207                 return 0;
208         }
209
210         cpi.descriptor = get_descriptor (cpi.path);
211
212         DEBUG_TRACE (DEBUG::ControlProtocols, string_compose ("instantiating %1\n", cpi.name));
213
214         if (cpi.descriptor == 0) {
215                 error << string_compose (_("control protocol name \"%1\" has no descriptor"), cpi.name) << endmsg;
216                 return 0;
217         }
218
219         DEBUG_TRACE (DEBUG::ControlProtocols, string_compose ("initializing %1\n", cpi.name));
220
221         if ((cpi.protocol = cpi.descriptor->initialize (cpi.descriptor, _session)) == 0) {
222                 error << string_compose (_("control protocol name \"%1\" could not be initialized"), cpi.name) << endmsg;
223                 return 0;
224         }
225
226         control_protocols.push_back (cpi.protocol);
227
228         ProtocolStatusChange (&cpi);
229
230         return cpi.protocol;
231 }
232
233 int
234 ControlProtocolManager::teardown (ControlProtocolInfo& cpi, bool lock_required)
235 {
236         if (!cpi.protocol) {
237
238                 /* we could still have a descriptor even if the protocol was
239                    never instantiated. Close the associated module (shared
240                    object/DLL) and make sure we forget about it.
241                 */
242
243                 if (cpi.descriptor) {
244                         cerr << "Closing descriptor for CPI anyway\n";
245                         delete (Glib::Module*) cpi.descriptor->module;
246                         cpi.descriptor = 0;
247                 }
248
249                 return 0;
250         }
251
252         if (!cpi.descriptor) {
253                 return 0;
254         }
255
256         if (cpi.mandatory) {
257                 return 0;
258         }
259
260         /* save current state */
261
262         delete cpi.state;
263         cpi.state = new XMLNode (cpi.protocol->get_state());
264         cpi.state->set_property (X_("active"), false);
265
266         cpi.descriptor->destroy (cpi.descriptor, cpi.protocol);
267
268         if (lock_required) {
269                 /* the lock is required when the protocol is torn down by a user from the GUI. */
270                 Glib::Threads::RWLock::WriterLock lm (protocols_lock);
271                 list<ControlProtocol*>::iterator p = find (control_protocols.begin(), control_protocols.end(), cpi.protocol);
272                 if (p != control_protocols.end()) {
273                         control_protocols.erase (p);
274                 } else {
275                         cerr << "Programming error: ControlProtocolManager::teardown() called for " << cpi.name << ", but it was not found in control_protocols" << endl;
276                 }
277         } else {
278                 list<ControlProtocol*>::iterator p = find (control_protocols.begin(), control_protocols.end(), cpi.protocol);
279                 if (p != control_protocols.end()) {
280                         control_protocols.erase (p);
281                 } else {
282                         cerr << "Programming error: ControlProtocolManager::teardown() called for " << cpi.name << ", but it was not found in control_protocols" << endl;
283                 }
284         }
285
286         cpi.protocol = 0;
287
288         delete (Glib::Module*) cpi.descriptor->module;
289         /* cpi->descriptor is now inaccessible since dlclose() or equivalent
290          * has been performed, and the descriptor is (or could be) a static
291          * object made accessible by dlopen().
292          */
293         cpi.descriptor = 0;
294
295         ProtocolStatusChange (&cpi);
296
297         return 0;
298 }
299
300 void
301 ControlProtocolManager::load_mandatory_protocols ()
302 {
303         if (_session == 0) {
304                 return;
305         }
306
307         Glib::Threads::RWLock::ReaderLock lm (protocols_lock);
308
309         for (list<ControlProtocolInfo*>::iterator i = control_protocol_info.begin(); i != control_protocol_info.end(); ++i) {
310                 if ((*i)->mandatory && ((*i)->protocol == 0)) {
311                         DEBUG_TRACE (DEBUG::ControlProtocols,
312                                      string_compose (_("Instantiating mandatory control protocol %1"), (*i)->name));
313                         instantiate (**i);
314                 }
315         }
316 }
317
318 void
319 ControlProtocolManager::discover_control_protocols ()
320 {
321         vector<std::string> cp_modules;
322
323 #ifdef COMPILER_MSVC
324    /**
325     * Different build targets (Debug / Release etc) use different versions
326     * of the 'C' runtime (which can't be 'mixed & matched'). Therefore, in
327     * case the supplied search path contains multiple version(s) of a given
328     * module, only select the one(s) which match the current build target
329     */
330         #if defined (_DEBUG)
331                 Glib::PatternSpec dll_extension_pattern("*D.dll");
332         #elif defined (RDC_BUILD)
333                 Glib::PatternSpec dll_extension_pattern("*RDC.dll");
334         #elif defined (_WIN64)
335                 Glib::PatternSpec dll_extension_pattern("*64.dll");
336         #else
337                 Glib::PatternSpec dll_extension_pattern("*32.dll");
338         #endif
339 #else
340         Glib::PatternSpec dll_extension_pattern("*.dll");
341 #endif
342
343         Glib::PatternSpec so_extension_pattern("*.so");
344         Glib::PatternSpec dylib_extension_pattern("*.dylib");
345
346         find_files_matching_pattern (cp_modules, control_protocol_search_path (),
347                                      dll_extension_pattern);
348
349         find_files_matching_pattern (cp_modules, control_protocol_search_path (),
350                                      so_extension_pattern);
351
352         find_files_matching_pattern (cp_modules, control_protocol_search_path (),
353                                      dylib_extension_pattern);
354
355         DEBUG_TRACE (DEBUG::ControlProtocols,
356                      string_compose (_("looking for control protocols in %1\n"), control_protocol_search_path().to_string()));
357
358         for (vector<std::string>::iterator i = cp_modules.begin(); i != cp_modules.end(); ++i) {
359                 control_protocol_discover (*i);
360         }
361 }
362
363 int
364 ControlProtocolManager::control_protocol_discover (string path)
365 {
366         ControlProtocolDescriptor* descriptor;
367
368 #ifdef __APPLE__
369         /* don't load OS X shared objects that are just symlinks to the real thing.
370          */
371
372         if (path.find (".dylib") && Glib::file_test (path, Glib::FILE_TEST_IS_SYMLINK)) {
373                 return 0;
374         }
375 #endif
376
377         if ((descriptor = get_descriptor (path)) != 0) {
378
379                 if (!descriptor->probe (descriptor)) {
380                         warning << string_compose (_("Control protocol %1 not usable"), descriptor->name) << endmsg;
381                 } else {
382
383                         ControlProtocolInfo* cpi = new ControlProtocolInfo ();
384
385                         cpi->descriptor = descriptor;
386                         cpi->name = descriptor->name;
387                         cpi->path = path;
388                         cpi->protocol = 0;
389                         cpi->requested = false;
390                         cpi->mandatory = descriptor->mandatory;
391                         cpi->supports_feedback = descriptor->supports_feedback;
392                         cpi->state = 0;
393
394                         control_protocol_info.push_back (cpi);
395
396                         DEBUG_TRACE (DEBUG::ControlProtocols,
397                                      string_compose(_("Control surface protocol discovered: \"%1\"\n"), cpi->name));
398                 }
399         }
400
401         return 0;
402 }
403
404 ControlProtocolDescriptor*
405 ControlProtocolManager::get_descriptor (string path)
406 {
407         Glib::Module* module = new Glib::Module(path);
408         ControlProtocolDescriptor *descriptor = 0;
409         ControlProtocolDescriptor* (*dfunc)(void);
410         void* func = 0;
411
412         if (!(*module)) {
413                 error << string_compose(_("ControlProtocolManager: cannot load module \"%1\" (%2)"), path, Glib::Module::get_last_error()) << endmsg;
414                 delete module;
415                 return 0;
416         }
417
418         if (!module->get_symbol("protocol_descriptor", func)) {
419                 error << string_compose(_("ControlProtocolManager: module \"%1\" has no descriptor function."), path) << endmsg;
420                 error << Glib::Module::get_last_error() << endmsg;
421                 delete module;
422                 return 0;
423         }
424
425         dfunc = (ControlProtocolDescriptor* (*)(void))func;
426         descriptor = dfunc();
427
428         if (descriptor) {
429                 descriptor->module = (void*)module;
430         }
431
432         return descriptor;
433 }
434
435 void
436 ControlProtocolManager::foreach_known_protocol (boost::function<void(const ControlProtocolInfo*)> method)
437 {
438         for (list<ControlProtocolInfo*>::iterator i = control_protocol_info.begin(); i != control_protocol_info.end(); ++i) {
439                 method (*i);
440         }
441 }
442
443 ControlProtocolInfo*
444 ControlProtocolManager::cpi_by_name (string name)
445 {
446         for (list<ControlProtocolInfo*>::iterator i = control_protocol_info.begin(); i != control_protocol_info.end(); ++i) {
447                 if (name == (*i)->name) {
448                         return *i;
449                 }
450         }
451         return 0;
452 }
453
454 int
455 ControlProtocolManager::set_state (const XMLNode& node, int session_specific_state /* here: not version */)
456 {
457         XMLNodeList clist;
458         XMLNodeConstIterator citer;
459
460         Glib::Threads::RWLock::WriterLock lm (protocols_lock);
461
462         clist = node.children();
463
464         for (citer = clist.begin(); citer != clist.end(); ++citer) {
465                 XMLNode const * child = *citer;
466
467                 if (child->name() == X_("Protocol")) {
468
469                         bool active;
470                         std::string name;
471                         if (!child->get_property (X_("active"), active) ||
472                             !child->get_property (X_("name"), name)) {
473                                 continue;
474                         }
475
476                         ControlProtocolInfo* cpi = cpi_by_name (name);
477
478                         if (cpi) {
479 #ifndef NDEBUG
480                                 std::cerr << "protocol " << name << " active ? " << active << std::endl;
481 #endif
482
483                                 if (active) {
484                                         delete cpi->state;
485                                         cpi->state = new XMLNode (**citer);
486                                         cpi->state->set_property (X_("session-state"), session_specific_state ? true : false);
487                                         if (_session) {
488                                                 instantiate (*cpi);
489                                         } else {
490                                                 cpi->requested = true;
491                                         }
492                                 } else {
493                                         if (!cpi->state) {
494                                                 cpi->state = new XMLNode (**citer);
495                                                 cpi->state->set_property (X_("active"), false);
496                                                 cpi->state->set_property (X_("session-state"), session_specific_state ? true : false);
497                                         }
498                                         cpi->requested = false;
499                                         if (_session) {
500                                                 teardown (*cpi, false);
501                                         }
502                                 }
503                         } else {
504                                 std::cerr << "protocol " << name << " not found\n";
505                         }
506                 }
507         }
508
509         return 0;
510 }
511
512 XMLNode&
513 ControlProtocolManager::get_state ()
514 {
515         XMLNode* root = new XMLNode (state_node_name);
516         Glib::Threads::RWLock::ReaderLock lm (protocols_lock);
517
518         for (list<ControlProtocolInfo*>::iterator i = control_protocol_info.begin(); i != control_protocol_info.end(); ++i) {
519
520                 if ((*i)->protocol) {
521                         XMLNode& child_state ((*i)->protocol->get_state());
522                         child_state.set_property (X_("active"), true);
523                         delete ((*i)->state);
524                         (*i)->state = new XMLNode (child_state);
525                         root->add_child_nocopy (child_state);
526                 } else if ((*i)->state) {
527                         XMLNode* child_state = new XMLNode (*(*i)->state);
528                         child_state->set_property (X_("active"), false);
529                         root->add_child_nocopy (*child_state);
530                 } else {
531                         XMLNode* child_state = new XMLNode (X_("Protocol"));
532                         child_state->set_property (X_("name"), (*i)->name);
533                         child_state->set_property (X_("active"), false);
534                         root->add_child_nocopy (*child_state);
535                 }
536
537         }
538
539         return *root;
540 }
541
542
543 ControlProtocolManager&
544 ControlProtocolManager::instance ()
545 {
546         if (_instance == 0) {
547                 _instance = new ControlProtocolManager ();
548         }
549
550         return *_instance;
551 }
552
553 void
554 ControlProtocolManager::midi_connectivity_established ()
555 {
556         Glib::Threads::RWLock::ReaderLock lm (protocols_lock);
557
558         for (list<ControlProtocol*>::iterator p = control_protocols.begin(); p != control_protocols.end(); ++p) {
559                 (*p)->midi_connectivity_established ();
560         }
561 }
562
563 void
564 ControlProtocolManager::register_request_buffer_factories ()
565 {
566         Glib::Threads::RWLock::ReaderLock lm (protocols_lock);
567
568         for (list<ControlProtocolInfo*>::iterator i = control_protocol_info.begin(); i != control_protocol_info.end(); ++i) {
569
570                 if ((*i)->descriptor == 0) {
571                         warning << string_compose (_("Control protocol \"%1\" has no descriptor"), (*i)->name) << endmsg;
572                         continue;
573                 }
574
575                 if ((*i)->descriptor->request_buffer_factory) {
576                         EventLoop::register_request_buffer_factory ((*i)->descriptor->name, (*i)->descriptor->request_buffer_factory);
577                 }
578         }
579 }
580
581 void
582 ControlProtocolManager::stripable_selection_changed (StripableNotificationListPtr sp)
583 {
584         /* this sets up the (static) data structures owned by ControlProtocol
585            that are "shared" across all control protocols.
586         */
587
588         DEBUG_TRACE (DEBUG::Selection, string_compose ("Surface manager: selection changed, now %1 stripables\n", sp ? sp->size() : -1));
589         StripableSelectionChanged (sp); /* EMIT SIGNAL */
590
591         /* now give each protocol the chance to respond to the selection change
592          */
593
594         {
595                 Glib::Threads::RWLock::ReaderLock lm (protocols_lock);
596
597                 for (list<ControlProtocol*>::iterator p = control_protocols.begin(); p != control_protocols.end(); ++p) {
598                         DEBUG_TRACE (DEBUG::Selection, string_compose ("selection change notification for surface \"%1\"\n", (*p)->name()));
599                         (*p)->stripable_selection_changed ();
600                 }
601         }
602 }