c2589d24e3b3166834d0ebefea5995ad60231e16
[ardour.git] / libs / surfaces / control_protocol / control_protocol.cc
1 /*
2     Copyright (C) 2006 Paul Davis
3
4     This program is free software; you can redistribute it
5     and/or modify it under the terms of the GNU Lesser
6     General Public License as published by the Free Software
7     Foundation; either version 2 of the License, or (at your
8     option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License
16     along with this program; if not, write to the Free Software
17     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18
19 */
20
21 #include "pbd/convert.h"
22 #include "pbd/error.h"
23
24 #include "ardour/gain_control.h"
25 #include "ardour/session.h"
26 #include "ardour/record_enable_control.h"
27 #include "ardour/route.h"
28 #include "ardour/audio_track.h"
29 #include "ardour/meter.h"
30 #include "ardour/amp.h"
31 #include "control_protocol/control_protocol.h"
32
33 using namespace ARDOUR;
34 using namespace std;
35 using namespace PBD;
36
37 Signal0<void>       ControlProtocol::ZoomToSession;
38 Signal0<void>       ControlProtocol::ZoomOut;
39 Signal0<void>       ControlProtocol::ZoomIn;
40 Signal0<void>       ControlProtocol::Enter;
41 Signal0<void>       ControlProtocol::Undo;
42 Signal0<void>       ControlProtocol::Redo;
43 Signal1<void,float> ControlProtocol::ScrollTimeline;
44 Signal1<void,uint32_t> ControlProtocol::GotoView;
45 Signal0<void> ControlProtocol::CloseDialog;
46 PBD::Signal0<void> ControlProtocol::VerticalZoomInAll;
47 PBD::Signal0<void> ControlProtocol::VerticalZoomOutAll;
48 PBD::Signal0<void> ControlProtocol::VerticalZoomInSelected;
49 PBD::Signal0<void> ControlProtocol::VerticalZoomOutSelected;
50 PBD::Signal0<void>          ControlProtocol::StepTracksDown;
51 PBD::Signal0<void>          ControlProtocol::StepTracksUp;
52
53 PBD::Signal1<void,boost::shared_ptr<ARDOUR::Stripable> > ControlProtocol::AddStripableToSelection;
54 PBD::Signal1<void,boost::shared_ptr<ARDOUR::Stripable> > ControlProtocol::SetStripableSelection;
55 PBD::Signal1<void,boost::shared_ptr<ARDOUR::Stripable> > ControlProtocol::ToggleStripableSelection;
56 PBD::Signal1<void,boost::shared_ptr<ARDOUR::Stripable> > ControlProtocol::RemoveStripableFromSelection;
57 PBD::Signal0<void>          ControlProtocol::ClearStripableSelection;
58
59 Glib::Threads::Mutex ControlProtocol::special_stripable_mutex;
60 boost::weak_ptr<Stripable> ControlProtocol::_first_selected_stripable;
61 boost::weak_ptr<Stripable> ControlProtocol::_leftmost_mixer_stripable;
62 StripableNotificationList ControlProtocol::_last_selected;
63
64 const std::string ControlProtocol::state_node_name ("Protocol");
65
66 ControlProtocol::ControlProtocol (Session& s, string str)
67         : BasicUI (s)
68         , _name (str)
69         , _active (false)
70 {
71 }
72
73 ControlProtocol::~ControlProtocol ()
74 {
75 }
76
77 int
78 ControlProtocol::set_active (bool yn)
79 {
80         _active = yn;
81         return 0;
82 }
83
84 void
85 ControlProtocol::next_track (uint32_t initial_id)
86 {
87         // STRIPABLE route_table[0] = _session->get_nth_stripable (++initial_id, RemoteControlID::Route);
88 }
89
90 void
91 ControlProtocol::prev_track (uint32_t initial_id)
92 {
93         if (!initial_id) {
94                 return;
95         }
96         // STRIPABLE route_table[0] = _session->get_nth_stripable (--initial_id, RemoteControlID::Route);
97 }
98
99 void
100 ControlProtocol::set_route_table_size (uint32_t size)
101 {
102         while (route_table.size() < size) {
103                 route_table.push_back (boost::shared_ptr<Route> ((Route*) 0));
104         }
105 }
106
107 void
108 ControlProtocol::set_route_table (uint32_t table_index, boost::shared_ptr<ARDOUR::Route> r)
109 {
110         if (table_index >= route_table.size()) {
111                 return;
112         }
113
114         route_table[table_index] = r;
115
116         // XXX SHAREDPTR need to handle r->GoingAway
117 }
118
119 bool
120 ControlProtocol::set_route_table (uint32_t table_index, uint32_t remote_control_id)
121 {
122 #if 0 // STRIPABLE
123         boost::shared_ptr<Route> r = session->route_by_remote_id (remote_control_id);
124
125         if (!r) {
126                 return false;
127         }
128
129         set_route_table (table_index, r);
130 #endif
131         return true;
132 }
133
134 void
135 ControlProtocol::route_set_rec_enable (uint32_t table_index, bool yn)
136 {
137         if (table_index > route_table.size()) {
138                 return;
139         }
140
141         boost::shared_ptr<Route> r = route_table[table_index];
142
143         boost::shared_ptr<AudioTrack> at = boost::dynamic_pointer_cast<AudioTrack>(r);
144
145         if (at) {
146                 at->rec_enable_control()->set_value (1.0, Controllable::UseGroup);
147         }
148 }
149
150 bool
151 ControlProtocol::route_get_rec_enable (uint32_t table_index)
152 {
153         if (table_index > route_table.size()) {
154                 return false;
155         }
156
157         boost::shared_ptr<Route> r = route_table[table_index];
158
159         boost::shared_ptr<AudioTrack> at = boost::dynamic_pointer_cast<AudioTrack>(r);
160
161         if (at) {
162                 return at->rec_enable_control()->get_value();
163         }
164
165         return false;
166 }
167
168
169 float
170 ControlProtocol::route_get_gain (uint32_t table_index)
171 {
172         if (table_index > route_table.size()) {
173                 return 0.0f;
174         }
175
176         boost::shared_ptr<Route> r = route_table[table_index];
177
178         if (r == 0) {
179                 return 0.0f;
180         }
181
182         return r->gain_control()->get_value();
183 }
184
185 void
186 ControlProtocol::route_set_gain (uint32_t table_index, float gain)
187 {
188         if (table_index > route_table.size()) {
189                 return;
190         }
191
192         boost::shared_ptr<Route> r = route_table[table_index];
193
194         if (r != 0) {
195                 r->gain_control()->set_value (gain, Controllable::UseGroup);
196         }
197 }
198
199 float
200 ControlProtocol::route_get_effective_gain (uint32_t table_index)
201 {
202         if (table_index > route_table.size()) {
203                 return 0.0f;
204         }
205
206         boost::shared_ptr<Route> r = route_table[table_index];
207
208         if (r == 0) {
209                 return 0.0f;
210         }
211
212         return r->amp()->gain_control()->get_value();
213 }
214
215
216 float
217 ControlProtocol::route_get_peak_input_power (uint32_t table_index, uint32_t which_input)
218 {
219         if (table_index > route_table.size()) {
220                 return 0.0f;
221         }
222
223         boost::shared_ptr<Route> r = route_table[table_index];
224
225         if (r == 0) {
226                 return 0.0f;
227         }
228
229         return r->peak_meter()->meter_level (which_input, MeterPeak);
230 }
231
232 bool
233 ControlProtocol::route_get_muted (uint32_t table_index)
234 {
235         if (table_index > route_table.size()) {
236                 return false;
237         }
238
239         boost::shared_ptr<Route> r = route_table[table_index];
240
241         if (r == 0) {
242                 return false;
243         }
244
245         return r->mute_control()->muted ();
246 }
247
248 void
249 ControlProtocol::route_set_muted (uint32_t table_index, bool yn)
250 {
251         if (table_index > route_table.size()) {
252                 return;
253         }
254
255         boost::shared_ptr<Route> r = route_table[table_index];
256
257         if (r != 0) {
258                 r->mute_control()->set_value (yn ? 1.0 : 0.0, Controllable::UseGroup);
259         }
260 }
261
262
263 bool
264 ControlProtocol::route_get_soloed (uint32_t table_index)
265 {
266         if (table_index > route_table.size()) {
267                 return false;
268         }
269
270         boost::shared_ptr<Route> r = route_table[table_index];
271
272         if (r == 0) {
273                 return false;
274         }
275
276         return r->soloed ();
277 }
278
279 void
280 ControlProtocol::route_set_soloed (uint32_t table_index, bool yn)
281 {
282         if (table_index > route_table.size()) {
283                 return;
284         }
285
286         boost::shared_ptr<Route> r = route_table[table_index];
287
288         if (r != 0) {
289                 r->solo_control()->set_value (yn ? 1.0 : 0.0, Controllable::UseGroup); // XXX does not propagate
290                 //_session->set_control (r->solo_control(), yn ? 1.0 : 0.0, Controllable::UseGroup); // << correct way, needs a session ptr
291         }
292 }
293
294 string
295 ControlProtocol:: route_get_name (uint32_t table_index)
296 {
297         if (table_index > route_table.size()) {
298                 return "";
299         }
300
301         boost::shared_ptr<Route> r = route_table[table_index];
302
303         if (r == 0) {
304                 return "";
305         }
306
307         return r->name();
308 }
309
310 list<boost::shared_ptr<Bundle> >
311 ControlProtocol::bundles ()
312 {
313         return list<boost::shared_ptr<Bundle> > ();
314 }
315
316 XMLNode&
317 ControlProtocol::get_state ()
318 {
319         XMLNode* node = new XMLNode (state_node_name);
320
321         node->set_property ("name", _name);
322         node->set_property ("feedback", get_feedback());
323
324         return *node;
325 }
326
327 int
328 ControlProtocol::set_state (XMLNode const & node, int /* version */)
329 {
330         bool feedback;
331         if (node.get_property ("feedback", feedback)) {
332                 set_feedback (feedback);
333         }
334
335         return 0;
336 }
337
338 boost::shared_ptr<Stripable>
339 ControlProtocol::first_selected_stripable ()
340 {
341         Glib::Threads::Mutex::Lock lm (special_stripable_mutex);
342         return _first_selected_stripable.lock();
343 }
344
345 boost::shared_ptr<Stripable>
346 ControlProtocol::leftmost_mixer_stripable ()
347 {
348         Glib::Threads::Mutex::Lock lm (special_stripable_mutex);
349         return _leftmost_mixer_stripable.lock();
350 }
351
352 void
353 ControlProtocol::set_leftmost_mixer_stripable (boost::shared_ptr<Stripable> s)
354 {
355         Glib::Threads::Mutex::Lock lm (special_stripable_mutex);
356         _leftmost_mixer_stripable = s;
357 }
358
359 void
360 ControlProtocol::set_first_selected_stripable (boost::shared_ptr<Stripable> s)
361 {
362         Glib::Threads::Mutex::Lock lm (special_stripable_mutex);
363         _first_selected_stripable = s;
364 }
365
366 void
367 ControlProtocol::notify_stripable_selection_changed (StripableNotificationListPtr sp)
368 {
369         bool had_selection = !_last_selected.empty();
370
371         _last_selected = *sp;
372
373         {
374                 Glib::Threads::Mutex::Lock lm (special_stripable_mutex);
375
376                 if (!_last_selected.empty()) {
377                         if (!had_selection) {
378                                 _first_selected_stripable = _last_selected.front().lock();
379                         }
380                 } else {
381                         _first_selected_stripable = boost::weak_ptr<Stripable>();
382                 }
383         }
384 }