7a7b8e291947bc4c85d66b4293357414426f5de5
[ardour.git] / libs / ardour / slavable.cc
1 /*
2     Copyright (C) 2016 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 <vector>
21
22 #include <glibmm/threads.h>
23
24 #include "pbd/convert.h"
25 #include "pbd/error.h"
26 #include "pbd/xml++.h"
27
28 #include "ardour/slavable.h"
29 #include "ardour/vca.h"
30 #include "ardour/vca_manager.h"
31
32 #include "i18n.h"
33
34 using namespace PBD;
35 using namespace ARDOUR;
36
37 std::string Slavable::xml_node_name = X_("Slavable");
38 PBD::Signal1<void,VCAManager*> Slavable::Assign; /* signal sent once
39                                                   * assignment is possible */
40
41 Slavable::Slavable ()
42 {
43         Assign.connect_same_thread (assign_connection, boost::bind (&Slavable::do_assign, this, _1));
44 }
45
46 XMLNode&
47 Slavable::get_state () const
48 {
49         XMLNode* node = new XMLNode (xml_node_name);
50         XMLNode* child;
51
52         Glib::Threads::RWLock::ReaderLock lm (master_lock);
53         for (std::set<uint32_t>::const_iterator i = _masters.begin(); i != _masters.end(); ++i) {
54                 child = new XMLNode (X_("Master"));
55                 child->add_property (X_("number"), to_string (*i, std::dec));
56                 node->add_child_nocopy (*child);
57         }
58
59         return *node;
60 }
61
62 int
63 Slavable::set_state (XMLNode const& node, int version)
64 {
65         if (node.name() != xml_node_name) {
66                 return -1;
67         }
68
69         XMLNodeList const& children (node.children());
70         Glib::Threads::RWLock::WriterLock lm (master_lock);
71
72         for (XMLNodeList::const_iterator i = children.begin(); i != children.end(); ++i) {
73                 if ((*i)->name() == X_("Master")) {
74                         XMLProperty const* prop = (*i)->property (X_("number"));
75                         if (prop) {
76                                 uint32_t n = atoi (prop->value());
77                                 _masters.insert (n);
78                         }
79                 }
80         }
81
82         return 0;
83 }
84
85 int
86 Slavable::do_assign (VCAManager* manager)
87 {
88         std::vector<boost::shared_ptr<VCA> > vcas;
89
90         {
91                 Glib::Threads::RWLock::ReaderLock lm (master_lock);
92
93                 for (std::set<uint32_t>::const_iterator i = _masters.begin(); i != _masters.end(); ++i) {
94                         boost::shared_ptr<VCA> v = manager->vca_by_number (*i);
95                         if (v) {
96                                 vcas.push_back (v);
97                         } else {
98                                 warning << string_compose (_("Master #%1 not found, assignment lost"), *i) << endmsg;
99                         }
100                 }
101         }
102
103         /* now that we've released the lock, we can do the assignments */
104
105         for (std::vector<boost::shared_ptr<VCA> >::iterator v = vcas.begin(); v != vcas.end(); ++v) {
106                 assign (*v);
107         }
108
109         assign_connection.disconnect ();
110
111         return 0;
112 }
113
114 void
115 Slavable::assign (boost::shared_ptr<VCA> v)
116 {
117         Glib::Threads::RWLock::WriterLock lm (master_lock);
118         if (assign_controls (v) == 0) {
119                 _masters.insert (v->number());
120         }
121 }
122
123 void
124 Slavable::unassign (boost::shared_ptr<VCA> v)
125 {
126         Glib::Threads::RWLock::WriterLock lm (master_lock);
127         (void) unassign_controls (v);
128         _masters.erase (v->number());
129 }