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