allow ControlProtocols to call for undo/redo in GUI; make MCP use this
[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/error.h"
22
23 #include "ardour/session.h"
24 #include "ardour/route.h"
25 #include "ardour/audio_track.h"
26 #include "ardour/meter.h"
27 #include "ardour/amp.h"
28 #include "control_protocol/control_protocol.h"
29
30 using namespace ARDOUR;
31 using namespace std;
32 using namespace PBD;
33
34 Signal0<void>       ControlProtocol::ZoomToSession;
35 Signal0<void>       ControlProtocol::ZoomOut;
36 Signal0<void>       ControlProtocol::ZoomIn;
37 Signal0<void>       ControlProtocol::Enter;
38 Signal0<void>       ControlProtocol::Undo;
39 Signal0<void>       ControlProtocol::Redo;
40 Signal1<void,float> ControlProtocol::ScrollTimeline;
41 Signal1<void,uint32_t> ControlProtocol::SelectByRID;
42
43 ControlProtocol::ControlProtocol (Session& s, string str, EventLoop* evloop)
44         : BasicUI (s),
45           _name (str)
46 {
47         if (evloop) {
48                 _own_event_loop = false;
49                 _event_loop = evloop;
50         } else {
51                 _own_event_loop = true;
52                 fatal << "programming error: cannot create control protocols without an existing event loop (yet)" << endmsg;
53                 /*NOTREACHED*/
54         }
55
56         _active = false;
57         
58         session->RouteAdded.connect (*this, MISSING_INVALIDATOR, boost::protect (boost::bind (&ControlProtocol::add_strip, this, _1)), _event_loop);
59 }
60
61 ControlProtocol::~ControlProtocol ()
62 {
63 }
64
65 void
66 ControlProtocol::add_strip (ARDOUR::RouteList&)
67 {
68         route_list_changed();
69 }
70         
71 void
72 ControlProtocol::next_track (uint32_t initial_id)
73 {
74         uint32_t limit = session->nroutes();
75         boost::shared_ptr<Route> cr = route_table[0];
76         uint32_t id;
77
78         if (cr) {
79                 id = cr->remote_control_id ();
80         } else {
81                 id = 0;
82         }
83
84         if (id == limit) {
85                 id = 0;
86         } else {
87                 id++;
88         }
89
90         while (id <= limit) {
91                 if ((cr = session->route_by_remote_id (id)) != 0) {
92                         break;
93                 }
94                 id++;
95         }
96
97         if (id >= limit) {
98                 id = 0;
99                 while (id != initial_id) {
100                         if ((cr = session->route_by_remote_id (id)) != 0) {
101                                 break;
102                         }
103                         id++;
104                 }
105         }
106
107         route_table[0] = cr;
108 }
109
110 void
111 ControlProtocol::prev_track (uint32_t initial_id)
112 {
113         uint32_t limit = session->nroutes();
114         boost::shared_ptr<Route> cr = route_table[0];
115         int32_t id;
116
117         if (cr) {
118                 id = cr->remote_control_id ();
119         } else {
120                 id = 0;
121         }
122
123         if (id == 0) {
124                 id = limit;
125         } else {
126                 id--;
127         }
128
129         while (id >= 0) {
130                 if ((cr = session->route_by_remote_id (id)) != 0) {
131                         break;
132                 }
133                 id--;
134         }
135
136         if (id < 0) {
137                 uint32_t i = limit;
138                 while (i > initial_id) {
139                         if ((cr = session->route_by_remote_id (i)) != 0) {
140                                 break;
141                         }
142                         i--;
143                 }
144         }
145
146         route_table[0] = cr;
147 }
148
149
150 void
151 ControlProtocol::set_route_table_size (uint32_t size)
152 {
153         while (route_table.size() < size) {
154                 route_table.push_back (boost::shared_ptr<Route> ((Route*) 0));
155         }
156 }
157
158 void
159 ControlProtocol::set_route_table (uint32_t table_index, boost::shared_ptr<ARDOUR::Route> r)
160 {
161         if (table_index >= route_table.size()) {
162                 return;
163         }
164         
165         route_table[table_index] = r;
166
167         // XXX SHAREDPTR need to handle r->GoingAway
168 }
169
170 bool
171 ControlProtocol::set_route_table (uint32_t table_index, uint32_t remote_control_id)
172 {
173         boost::shared_ptr<Route> r = session->route_by_remote_id (remote_control_id);
174
175         if (!r) {
176                 return false;
177         }
178
179         set_route_table (table_index, r);
180
181         return true;
182 }
183
184 void
185 ControlProtocol::route_set_rec_enable (uint32_t table_index, bool yn)
186 {
187         if (table_index > route_table.size()) {
188                 return;
189         }
190
191         boost::shared_ptr<Route> r = route_table[table_index];
192
193         boost::shared_ptr<AudioTrack> at = boost::dynamic_pointer_cast<AudioTrack>(r);
194
195         if (at) {
196                 at->set_record_enabled (yn, this);
197         }
198 }
199
200 bool
201 ControlProtocol::route_get_rec_enable (uint32_t table_index)
202 {
203         if (table_index > route_table.size()) {
204                 return false;
205         }
206
207         boost::shared_ptr<Route> r = route_table[table_index];
208
209         boost::shared_ptr<AudioTrack> at = boost::dynamic_pointer_cast<AudioTrack>(r);
210
211         if (at) {
212                 return at->record_enabled ();
213         }
214
215         return false;
216 }
217
218
219 float
220 ControlProtocol::route_get_gain (uint32_t table_index)
221 {
222         if (table_index > route_table.size()) {
223                 return 0.0f;
224         }
225
226         boost::shared_ptr<Route> r = route_table[table_index];
227
228         if (r == 0) {
229                 return 0.0f;
230         }
231
232         return r->amp()->gain ();
233 }
234
235 void
236 ControlProtocol::route_set_gain (uint32_t table_index, float gain)
237 {
238         if (table_index > route_table.size()) {
239                 return;
240         }
241
242         boost::shared_ptr<Route> r = route_table[table_index];
243         
244         if (r != 0) {
245                 r->set_gain (gain, this);
246         }
247 }
248
249 float
250 ControlProtocol::route_get_effective_gain (uint32_t table_index)
251 {
252         if (table_index > route_table.size()) {
253                 return 0.0f;
254         }
255
256         boost::shared_ptr<Route> r = route_table[table_index];
257
258         if (r == 0) {
259                 return 0.0f;
260         }
261
262         return r->amp()->gain_control()->get_value();
263 }
264
265
266 float
267 ControlProtocol::route_get_peak_input_power (uint32_t table_index, uint32_t which_input)
268 {
269         if (table_index > route_table.size()) {
270                 return 0.0f;
271         }
272
273         boost::shared_ptr<Route> r = route_table[table_index];
274
275         if (r == 0) {
276                 return 0.0f;
277         }
278
279         return r->peak_meter().peak_power (which_input);
280 }
281
282
283 bool
284 ControlProtocol::route_get_muted (uint32_t table_index)
285 {
286         if (table_index > route_table.size()) {
287                 return false;
288         }
289
290         boost::shared_ptr<Route> r = route_table[table_index];
291
292         if (r == 0) {
293                 return false;
294         }
295
296         return r->muted ();
297 }
298
299 void
300 ControlProtocol::route_set_muted (uint32_t table_index, bool yn)
301 {
302         if (table_index > route_table.size()) {
303                 return;
304         }
305
306         boost::shared_ptr<Route> r = route_table[table_index];
307
308         if (r != 0) {
309                 r->set_mute (yn, this);
310         }
311 }
312
313
314 bool
315 ControlProtocol::route_get_soloed (uint32_t table_index)
316 {
317         if (table_index > route_table.size()) {
318                 return false;
319         }
320
321         boost::shared_ptr<Route> r = route_table[table_index];
322
323         if (r == 0) {
324                 return false;
325         }
326
327         return r->soloed ();
328 }
329
330 void
331 ControlProtocol::route_set_soloed (uint32_t table_index, bool yn)
332 {
333         if (table_index > route_table.size()) {
334                 return;
335         }
336
337         boost::shared_ptr<Route> r = route_table[table_index];
338
339         if (r != 0) {
340                 r->set_solo (yn, this);
341         }
342 }
343
344 string
345 ControlProtocol:: route_get_name (uint32_t table_index)
346 {
347         if (table_index > route_table.size()) {
348                 return "";
349         }
350
351         boost::shared_ptr<Route> r = route_table[table_index];
352
353         if (r == 0) {
354                 return "";
355         }
356
357         return r->name();
358 }
359
360 list<boost::shared_ptr<Bundle> >
361 ControlProtocol::bundles ()
362 {
363        return list<boost::shared_ptr<Bundle> > ();
364 }