Make start_touch() truly idempotent
[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                 delete (*p);
80         }
81
82         control_protocol_info.clear();
83 }
84
85 void
86 ControlProtocolManager::set_session (Session* s)
87 {
88         SessionHandlePtr::set_session (s);
89
90         if (!_session) {
91                 return;
92         }
93
94         {
95                 Glib::Threads::RWLock::ReaderLock lm (protocols_lock);
96
97                 for (list<ControlProtocolInfo*>::iterator i = control_protocol_info.begin(); i != control_protocol_info.end(); ++i) {
98                         if ((*i)->requested || (*i)->mandatory) {
99                                 (void) activate (**i);
100                         }
101                 }
102         }
103
104         CoreSelection::StripableAutomationControls sac;
105         _session->selection().get_stripables (sac);
106
107         if (!sac.empty()) {
108                 StripableNotificationListPtr v (new StripableNotificationList);
109                 for (CoreSelection::StripableAutomationControls::iterator i = sac.begin(); i != sac.end(); ++i) {
110                         if ((*i).stripable) {
111                                 v->push_back (boost::weak_ptr<Stripable> ((*i).stripable));
112                         }
113                 }
114                 if (!v->empty()) {
115                         StripableSelectionChanged (v); /* EMIT SIGNAL */
116                 }
117         }
118 }
119
120 int
121 ControlProtocolManager::activate (ControlProtocolInfo& cpi)
122 {
123         ControlProtocol* cp;
124
125         cpi.requested = true;
126
127         if ((cp = instantiate (cpi)) == 0) {
128                 return -1;
129         }
130
131         /* we split the set_state() and set_active() operations so that
132            protocols that need state to configure themselves (e.g. "What device
133            is connected, or supposed to be connected?") can get it before
134            actually starting any interaction.
135         */
136
137         if (cpi.state) {
138                 /* force this by tweaking the internals of the state
139                  * XMLNode. Ugh.
140                  */
141                 cp->set_state (*cpi.state, Stateful::loading_state_version);
142         } else {
143                 /* guarantee a call to
144                    set_state() whether we have
145                    existing state or not
146                 */
147                 cp->set_state (XMLNode(""), Stateful::loading_state_version);
148         }
149
150         if (cp->set_active (true)) {
151                 error << string_compose (_("Control protocol support for %1 failed to activate"), cpi.name) << endmsg;
152                 teardown (cpi, false);
153         }
154
155         return 0;
156 }
157
158 int
159 ControlProtocolManager::deactivate (ControlProtocolInfo& cpi)
160 {
161         cpi.requested = false;
162         return teardown (cpi, true);
163 }
164
165 void
166 ControlProtocolManager::session_going_away()
167 {
168         SessionHandlePtr::session_going_away ();
169         /* Session::destroy() will explicitly call drop_protocols() so we don't
170          * have to worry about that here.
171          */
172 }
173
174 void
175 ControlProtocolManager::drop_protocols ()
176 {
177         /* called explicitly by Session::destroy() so that we can clean up
178          * before the process cycle stops and ports vanish.
179          */
180
181         Glib::Threads::RWLock::WriterLock lm (protocols_lock);
182
183         for (list<ControlProtocol*>::iterator p = control_protocols.begin(); p != control_protocols.end(); ++p) {
184                 delete *p;
185         }
186
187         control_protocols.clear ();
188
189         for (list<ControlProtocolInfo*>::iterator p = control_protocol_info.begin(); p != control_protocol_info.end(); ++p) {
190                 // mark existing protocols as requested
191                 // otherwise the ControlProtocol instances are not recreated in set_session
192                 if ((*p)->protocol) {
193                         (*p)->requested = true;
194                         (*p)->protocol = 0;
195                         ProtocolStatusChange (*p); /* EMIT SIGNAL */
196                 }
197         }
198 }
199
200 ControlProtocol*
201 ControlProtocolManager::instantiate (ControlProtocolInfo& cpi)
202 {
203         /* CALLER MUST HOLD LOCK */
204
205         if (_session == 0) {
206                 return 0;
207         }
208
209         cpi.descriptor = get_descriptor (cpi.path);
210
211         DEBUG_TRACE (DEBUG::ControlProtocols, string_compose ("instantiating %1\n", cpi.name));
212
213         if (cpi.descriptor == 0) {
214                 error << string_compose (_("control protocol name \"%1\" has no descriptor"), cpi.name) << endmsg;
215                 return 0;
216         }
217
218         DEBUG_TRACE (DEBUG::ControlProtocols, string_compose ("initializing %1\n", cpi.name));
219
220         if ((cpi.protocol = cpi.descriptor->initialize (cpi.descriptor, _session)) == 0) {
221                 error << string_compose (_("control protocol name \"%1\" could not be initialized"), cpi.name) << endmsg;
222                 return 0;
223         }
224
225         control_protocols.push_back (cpi.protocol);
226
227         ProtocolStatusChange (&cpi);
228
229         return cpi.protocol;
230 }
231
232 int
233 ControlProtocolManager::teardown (ControlProtocolInfo& cpi, bool lock_required)
234 {
235         if (!cpi.protocol) {
236
237                 /* we could still have a descriptor even if the protocol was
238                    never instantiated. Close the associated module (shared
239                    object/DLL) and make sure we forget about it.
240                 */
241
242                 if (cpi.descriptor) {
243                         cerr << "Closing descriptor for CPI anyway\n";
244                         delete (Glib::Module*) cpi.descriptor->module;
245                         cpi.descriptor = 0;
246                 }
247
248                 return 0;
249         }
250
251         if (!cpi.descriptor) {
252                 return 0;
253         }
254
255         if (cpi.mandatory) {
256                 return 0;
257         }
258
259         /* save current state */
260
261         delete cpi.state;
262         cpi.state = new XMLNode (cpi.protocol->get_state());
263         cpi.state->set_property (X_("active"), false);
264
265         cpi.descriptor->destroy (cpi.descriptor, cpi.protocol);
266
267         if (lock_required) {
268                 Glib::Threads::RWLock::WriterLock lm (protocols_lock);
269                 list<ControlProtocol*>::iterator p = find (control_protocols.begin(), control_protocols.end(), cpi.protocol);
270                 if (p != control_protocols.end()) {
271                         control_protocols.erase (p);
272                 } else {
273                         cerr << "Programming error: ControlProtocolManager::teardown() called for " << cpi.name << ", but it was not found in control_protocols" << endl;
274                 }
275         } else {
276                 list<ControlProtocol*>::iterator p = find (control_protocols.begin(), control_protocols.end(), cpi.protocol);
277                 if (p != control_protocols.end()) {
278                         control_protocols.erase (p);
279                 } else {
280                         cerr << "Programming error: ControlProtocolManager::teardown() called for " << cpi.name << ", but it was not found in control_protocols" << endl;
281                 }
282         }
283
284         cpi.protocol = 0;
285
286         if (lock_required) {
287                 /* the lock is only required when the protocol is torn down from the GUI.
288                  * If a user disables a protocol, we take this as indicator to forget the
289                  * state.
290                  */
291                 delete cpi.state;
292                 cpi.state = 0;
293         }
294         delete (Glib::Module*) cpi.descriptor->module;
295         /* cpi->descriptor is now inaccessible since dlclose() or equivalent
296          * has been performed, and the descriptor is (or could be) a static
297          * object made accessible by dlopen().
298          */
299         cpi.descriptor = 0;
300
301         ProtocolStatusChange (&cpi);
302
303         return 0;
304 }
305
306 void
307 ControlProtocolManager::load_mandatory_protocols ()
308 {
309         if (_session == 0) {
310                 return;
311         }
312
313         Glib::Threads::RWLock::ReaderLock lm (protocols_lock);
314
315         for (list<ControlProtocolInfo*>::iterator i = control_protocol_info.begin(); i != control_protocol_info.end(); ++i) {
316                 if ((*i)->mandatory && ((*i)->protocol == 0)) {
317                         DEBUG_TRACE (DEBUG::ControlProtocols,
318                                      string_compose (_("Instantiating mandatory control protocol %1"), (*i)->name));
319                         instantiate (**i);
320                 }
321         }
322 }
323
324 void
325 ControlProtocolManager::discover_control_protocols ()
326 {
327         vector<std::string> cp_modules;
328
329 #ifdef COMPILER_MSVC
330    /**
331     * Different build targets (Debug / Release etc) use different versions
332     * of the 'C' runtime (which can't be 'mixed & matched'). Therefore, in
333     * case the supplied search path contains multiple version(s) of a given
334     * module, only select the one(s) which match the current build target
335     */
336         #if defined (_DEBUG)
337                 Glib::PatternSpec dll_extension_pattern("*D.dll");
338         #elif defined (RDC_BUILD)
339                 Glib::PatternSpec dll_extension_pattern("*RDC.dll");
340         #elif defined (_WIN64)
341                 Glib::PatternSpec dll_extension_pattern("*64.dll");
342         #else
343                 Glib::PatternSpec dll_extension_pattern("*32.dll");
344         #endif
345 #else
346         Glib::PatternSpec dll_extension_pattern("*.dll");
347 #endif
348
349         Glib::PatternSpec so_extension_pattern("*.so");
350         Glib::PatternSpec dylib_extension_pattern("*.dylib");
351
352         find_files_matching_pattern (cp_modules, control_protocol_search_path (),
353                                      dll_extension_pattern);
354
355         find_files_matching_pattern (cp_modules, control_protocol_search_path (),
356                                      so_extension_pattern);
357
358         find_files_matching_pattern (cp_modules, control_protocol_search_path (),
359                                      dylib_extension_pattern);
360
361         DEBUG_TRACE (DEBUG::ControlProtocols,
362                      string_compose (_("looking for control protocols in %1\n"), control_protocol_search_path().to_string()));
363
364         for (vector<std::string>::iterator i = cp_modules.begin(); i != cp_modules.end(); ++i) {
365                 control_protocol_discover (*i);
366         }
367 }
368
369 int
370 ControlProtocolManager::control_protocol_discover (string path)
371 {
372         ControlProtocolDescriptor* descriptor;
373
374 #ifdef __APPLE__
375         /* don't load OS X shared objects that are just symlinks to the real thing.
376          */
377
378         if (path.find (".dylib") && Glib::file_test (path, Glib::FILE_TEST_IS_SYMLINK)) {
379                 return 0;
380         }
381 #endif
382
383         if ((descriptor = get_descriptor (path)) != 0) {
384
385                 if (!descriptor->probe (descriptor)) {
386                         warning << string_compose (_("Control protocol %1 not usable"), descriptor->name) << endmsg;
387                 } else {
388
389                         ControlProtocolInfo* cpi = new ControlProtocolInfo ();
390
391                         cpi->descriptor = descriptor;
392                         cpi->name = descriptor->name;
393                         cpi->path = path;
394                         cpi->protocol = 0;
395                         cpi->requested = false;
396                         cpi->mandatory = descriptor->mandatory;
397                         cpi->supports_feedback = descriptor->supports_feedback;
398                         cpi->state = 0;
399
400                         control_protocol_info.push_back (cpi);
401
402                         DEBUG_TRACE (DEBUG::ControlProtocols,
403                                      string_compose(_("Control surface protocol discovered: \"%1\"\n"), cpi->name));
404                 }
405         }
406
407         return 0;
408 }
409
410 ControlProtocolDescriptor*
411 ControlProtocolManager::get_descriptor (string path)
412 {
413         Glib::Module* module = new Glib::Module(path);
414         ControlProtocolDescriptor *descriptor = 0;
415         ControlProtocolDescriptor* (*dfunc)(void);
416         void* func = 0;
417
418         if (!(*module)) {
419                 error << string_compose(_("ControlProtocolManager: cannot load module \"%1\" (%2)"), path, Glib::Module::get_last_error()) << endmsg;
420                 delete module;
421                 return 0;
422         }
423
424         if (!module->get_symbol("protocol_descriptor", func)) {
425                 error << string_compose(_("ControlProtocolManager: module \"%1\" has no descriptor function."), path) << endmsg;
426                 error << Glib::Module::get_last_error() << endmsg;
427                 delete module;
428                 return 0;
429         }
430
431         dfunc = (ControlProtocolDescriptor* (*)(void))func;
432         descriptor = dfunc();
433
434         if (descriptor) {
435                 descriptor->module = (void*)module;
436         }
437
438         return descriptor;
439 }
440
441 void
442 ControlProtocolManager::foreach_known_protocol (boost::function<void(const ControlProtocolInfo*)> method)
443 {
444         for (list<ControlProtocolInfo*>::iterator i = control_protocol_info.begin(); i != control_protocol_info.end(); ++i) {
445                 method (*i);
446         }
447 }
448
449 ControlProtocolInfo*
450 ControlProtocolManager::cpi_by_name (string name)
451 {
452         for (list<ControlProtocolInfo*>::iterator i = control_protocol_info.begin(); i != control_protocol_info.end(); ++i) {
453                 if (name == (*i)->name) {
454                         return *i;
455                 }
456         }
457         return 0;
458 }
459
460 int
461 ControlProtocolManager::set_state (const XMLNode& node, int /*version*/)
462 {
463         XMLNodeList clist;
464         XMLNodeConstIterator citer;
465
466         Glib::Threads::RWLock::WriterLock lm (protocols_lock);
467
468         clist = node.children();
469
470         for (citer = clist.begin(); citer != clist.end(); ++citer) {
471                 XMLNode const * child = *citer;
472
473                 if (child->name() == X_("Protocol")) {
474
475                         bool active;
476                         std::string name;
477                         if (!child->get_property (X_("active"), active) ||
478                             !child->get_property (X_("name"), name)) {
479                                 continue;
480                         }
481
482                         ControlProtocolInfo* cpi = cpi_by_name (name);
483
484                         if (cpi) {
485                                 std::cerr << "protocol " << name << " active ? " << active << std::endl;
486
487                                 if (active) {
488                                         delete cpi->state;
489                                         cpi->state = new XMLNode (**citer);
490                                         if (_session) {
491                                                 instantiate (*cpi);
492                                         } else {
493                                                 cpi->requested = true;
494                                         }
495                                 } else {
496                                         if (!cpi->state) {
497                                                 cpi->state = new XMLNode (**citer);
498                                                 cpi->state->set_property (X_("active"), false);
499                                         }
500                                         cpi->requested = false;
501                                         if (_session) {
502                                                 teardown (*cpi, false);
503                                         }
504                                 }
505                         } else {
506                                 std::cerr << "protocol " << name << " not found\n";
507                         }
508                 }
509         }
510
511         return 0;
512 }
513
514 XMLNode&
515 ControlProtocolManager::get_state ()
516 {
517         XMLNode* root = new XMLNode (state_node_name);
518         Glib::Threads::RWLock::ReaderLock lm (protocols_lock);
519
520         for (list<ControlProtocolInfo*>::iterator i = control_protocol_info.begin(); i != control_protocol_info.end(); ++i) {
521
522                 if ((*i)->protocol) {
523                         XMLNode& child_state ((*i)->protocol->get_state());
524                         child_state.set_property (X_("active"), true);
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 }