breakout control protocol code into LGPL library; fix panner buttons even more than...
[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     $Id$
20 */
21
22 #include <ardour/session.h>
23 #include <ardour/route.h>
24 #include <ardour/audio_track.h>
25
26 #include "control_protocol.h"
27
28 using namespace ARDOUR;
29 using namespace std;
30
31 sigc::signal<void> ControlProtocol::ZoomToSession;
32 sigc::signal<void> ControlProtocol::ZoomOut;
33 sigc::signal<void> ControlProtocol::ZoomIn;
34 sigc::signal<void> ControlProtocol::Enter;
35 sigc::signal<void,float> ControlProtocol::ScrollTimeline;
36
37 ControlProtocol::ControlProtocol (Session& s, string str)
38         : BasicUI (s),
39           _name (str)
40 {
41         _active = false;
42 }
43
44 ControlProtocol::~ControlProtocol ()
45 {
46 }
47
48 void
49 ControlProtocol::next_track (uint32_t initial_id)
50 {
51         uint32_t limit = session->nroutes();
52         Route* cr = route_table[0];
53         uint32_t id;
54
55         if (cr) {
56                 id = cr->remote_control_id ();
57         } else {
58                 id = 0;
59         }
60
61         if (id == limit) {
62                 id = 0;
63         } else {
64                 id++;
65         }
66
67         while (id < limit) {
68                 if ((cr = session->route_by_remote_id (id)) != 0) {
69                         break;
70                 }
71                 id++;
72         }
73
74         if (id == limit) {
75                 id = 0;
76                 while (id != initial_id) {
77                         if ((cr = session->route_by_remote_id (id)) != 0) {
78                                 break;
79                         }
80                         id++;
81                 }
82         }
83
84         route_table[0] = cr;
85 }
86
87 void
88 ControlProtocol::prev_track (uint32_t initial_id)
89 {
90         uint32_t limit = session->nroutes() - 1;
91         Route* cr = route_table[0];
92         uint32_t id;
93
94         if (cr) {
95                 id = cr->remote_control_id ();
96         } else {
97                 id = 0;
98         }
99
100         if (id == 0) {
101                 id = session->nroutes() - 1;
102         } else {
103                 id--;
104         }
105
106         while (id >= 0) {
107                 if ((cr = session->route_by_remote_id (id)) != 0) {
108                         break;
109                 }
110                 id--;
111         }
112
113         if (id < 0) {
114                 id = limit;
115                 while (id > initial_id) {
116                         if ((cr = session->route_by_remote_id (id)) != 0) {
117                                 break;
118                         }
119                         id--;
120                 }
121         }
122
123         route_table[0] = cr;
124 }
125
126
127 void
128 ControlProtocol::set_route_table_size (uint32_t size)
129 {
130         while (route_table.size() < size) {
131                 route_table.push_back (0);
132         }
133 }
134
135 void
136 ControlProtocol::set_route_table (uint32_t table_index, ARDOUR::Route*)
137 {
138 }
139
140 bool
141 ControlProtocol::set_route_table (uint32_t table_index, uint32_t remote_control_id)
142 {
143         if (table_index >= route_table.size()) {
144                 return false;
145         }
146                 
147         Route* r = session->route_by_remote_id (remote_control_id);
148
149         if (!r) {
150                 return false;
151         }
152         
153         route_table[table_index] = r;
154
155         return true;
156 }
157
158 void
159 ControlProtocol::route_set_rec_enable (uint32_t table_index, bool yn)
160 {
161         if (table_index > route_table.size()) {
162                 return;
163         }
164
165         Route* r = route_table[table_index];
166
167         AudioTrack* at = dynamic_cast<AudioTrack*>(r);
168
169         if (at) {
170                 at->set_record_enable (yn, this);
171         }
172 }
173
174 bool
175 ControlProtocol::route_get_rec_enable (uint32_t table_index)
176 {
177         if (table_index > route_table.size()) {
178                 return false;
179         }
180
181         Route* r = route_table[table_index];
182
183         AudioTrack* at = dynamic_cast<AudioTrack*>(r);
184
185         if (at) {
186                 return at->record_enabled ();
187         }
188
189         return false;
190 }
191
192
193 float
194 ControlProtocol::route_get_gain (uint32_t table_index)
195 {
196         if (table_index > route_table.size()) {
197                 return 0.0f;
198         }
199
200         Route* r = route_table[table_index];
201
202         if (r == 0) {
203                 return 0.0f;
204         }
205
206         return r->gain ();
207 }
208
209 void
210 ControlProtocol::route_set_gain (uint32_t table_index, float gain)
211 {
212         if (table_index > route_table.size()) {
213                 return;
214         }
215
216         Route* r = route_table[table_index];
217         
218         if (r != 0) {
219                 r->set_gain (gain, this);
220         }
221 }
222
223 float
224 ControlProtocol::route_get_effective_gain (uint32_t table_index)
225 {
226         if (table_index > route_table.size()) {
227                 return 0.0f;
228         }
229
230         Route* r = route_table[table_index];
231
232         if (r == 0) {
233                 return 0.0f;
234         }
235
236         return r->effective_gain ();
237 }
238
239
240 float
241 ControlProtocol::route_get_peak_input_power (uint32_t table_index, uint32_t which_input)
242 {
243         if (table_index > route_table.size()) {
244                 return 0.0f;
245         }
246
247         Route* r = route_table[table_index];
248
249         if (r == 0) {
250                 return 0.0f;
251         }
252
253         return r->peak_input_power (which_input);
254 }
255
256
257 bool
258 ControlProtocol::route_get_muted (uint32_t table_index)
259 {
260         if (table_index > route_table.size()) {
261                 return false;
262         }
263
264         Route* r = route_table[table_index];
265
266         if (r == 0) {
267                 return false;
268         }
269
270         return r->muted ();
271 }
272
273 void
274 ControlProtocol::route_set_muted (uint32_t table_index, bool yn)
275 {
276         if (table_index > route_table.size()) {
277                 return;
278         }
279
280         Route* r = route_table[table_index];
281
282         if (r != 0) {
283                 r->set_mute (yn, this);
284         }
285 }
286
287
288 bool
289 ControlProtocol::route_get_soloed (uint32_t table_index)
290 {
291         if (table_index > route_table.size()) {
292                 return false;
293         }
294
295         Route* r = route_table[table_index];
296
297         if (r == 0) {
298                 return false;
299         }
300
301         return r->soloed ();
302 }
303
304 void
305 ControlProtocol::route_set_soloed (uint32_t table_index, bool yn)
306 {
307         if (table_index > route_table.size()) {
308                 return;
309         }
310
311         Route* r = route_table[table_index];
312
313         if (r != 0) {
314                 r->set_solo (yn, this);
315         }
316 }
317
318 string
319 ControlProtocol:: route_get_name (uint32_t table_index)
320 {
321         if (table_index > route_table.size()) {
322                 return "";
323         }
324
325         Route* r = route_table[table_index];
326
327         if (r == 0) {
328                 return "";
329         }
330
331         return r->name();
332 }
333