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