MCP: more mute button debug tracing; fix action for fn keys
[ardour.git] / libs / surfaces / mackie / strip.cc
1 /*
2         Copyright (C) 2006,2007 John Anderson
3         Copyright (C) 2012 Paul Davis
4
5         This program is free software; you can redistribute it and/or modify
6         it under the terms of the GNU General Public License as published by
7         the Free Software Foundation; either version 2 of the License, or
8         (at your 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 #include <sstream>
21 #include <stdint.h>
22 #include "strip.h"
23
24 #include "midi++/port.h"
25
26 #include "pbd/compose.h"
27 #include "pbd/convert.h"
28
29 #include "ardour/debug.h"
30 #include "ardour/midi_ui.h"
31 #include "ardour/route.h"
32 #include "ardour/track.h"
33 #include "ardour/pannable.h"
34 #include "ardour/panner.h"
35 #include "ardour/panner_shell.h"
36 #include "ardour/rc_configuration.h"
37 #include "ardour/meter.h"
38
39 #include "mackie_control_protocol.h"
40 #include "surface_port.h"
41 #include "surface.h"
42 #include "button.h"
43 #include "led.h"
44 #include "pot.h"
45 #include "fader.h"
46 #include "jog.h"
47 #include "meter.h"
48
49 using namespace Mackie;
50 using namespace std;
51 using namespace ARDOUR;
52 using namespace PBD;
53
54 #define ui_context() MackieControlProtocol::instance() /* a UICallback-derived object that specifies the event loop for signal handling */
55 #define ui_bind(f, ...) boost::protect (boost::bind (f, __VA_ARGS__))
56
57 extern PBD::EventLoop::InvalidationRecord* __invalidator (sigc::trackable& trackable, const char*, int);
58 #define invalidator() __invalidator (*(MackieControlProtocol::instance()), __FILE__, __LINE__)
59
60 Strip::Strip (Surface& s, const std::string& name, int index, StripControlDefinition* ctls)
61         : Group (name)
62         , _solo (0)
63         , _recenable (0)
64         , _mute (0)
65         , _select (0)
66         , _vselect (0)
67         , _fader_touch (0)
68         , _vpot (0)
69         , _gain (0)
70         , _index (index)
71         , _surface (&s)
72 {
73         /* build the controls for this track, which will automatically add them
74            to the Group 
75         */
76
77         for (uint32_t i = 0; ctls[i].name[0]; ++i) {
78                 ctls[i].factory (*_surface, ctls[i].base_id + index, ctls[i].name, *this);
79         }
80 }       
81
82 Strip::~Strip ()
83 {
84         
85 }
86
87 /**
88         TODO could optimise this to use enum, but it's only
89         called during the protocol class instantiation.
90 */
91 void Strip::add (Control & control)
92 {
93         Group::add (control);
94
95         Fader* fader;
96         Pot* pot;
97         Button* button;
98         Meter* meter;
99
100         if ((fader = dynamic_cast<Fader*>(&control)) != 0) {
101
102                 _gain = fader;
103
104         } else if ((pot = dynamic_cast<Pot*>(&control)) != 0) {
105
106                 _vpot = pot;
107
108         } else if ((button = dynamic_cast<Button*>(&control)) != 0) {
109
110                 if (control.id() >= Button::recenable_base_id &&
111                     control.id() < Button::recenable_base_id + 8) {
112                         
113                         _recenable = button;
114
115                 } else if (control.id() >= Button::mute_base_id &&
116                            control.id() < Button::mute_base_id + 8) {
117
118                         _mute = button;
119
120                 } else if (control.id() >= Button::solo_base_id &&
121                            control.id() < Button::solo_base_id + 8) {
122
123                         _solo = button;
124
125                 } else if (control.id() >= Button::select_base_id &&
126                            control.id() < Button::select_base_id + 8) {
127
128                         _select = button;
129
130                 } else if (control.id() >= Button::vselect_base_id &&
131                            control.id() < Button::vselect_base_id + 8) {
132
133                         _vselect = button;
134
135                 } else if (control.id() >= Button::fader_touch_base_id &&
136                            control.id() < Button::fader_touch_base_id + 8) {
137
138                         _fader_touch = button;
139                 }
140
141         } else if ((meter = dynamic_cast<Meter*>(&control)) != 0) {
142                 _meter = meter;
143         }
144 }
145
146 Fader& 
147 Strip::gain()
148 {
149         if  (_gain == 0) {
150                 throw MackieControlException ("gain is null");
151         }
152         return *_gain;
153 }
154
155 Pot& 
156 Strip::vpot()
157 {
158         if  (_vpot == 0) {
159                 throw MackieControlException ("vpot is null");
160         }
161         return *_vpot;
162 }
163
164 Button& 
165 Strip::recenable()
166 {
167         if  (_recenable == 0) {
168                 throw MackieControlException ("recenable is null");
169         }
170         return *_recenable;
171 }
172
173 Button& 
174 Strip::solo()
175 {
176         if  (_solo == 0) {
177                 throw MackieControlException ("solo is null");
178         }
179         return *_solo;
180 }
181 Button& 
182 Strip::mute()
183 {
184         if  (_mute == 0) {
185                 throw MackieControlException ("mute is null");
186         }
187         return *_mute;
188 }
189
190 Button& 
191 Strip::select()
192 {
193         if  (_select == 0) {
194                 throw MackieControlException ("select is null");
195         }
196         return *_select;
197 }
198
199 Button& 
200 Strip::vselect()
201 {
202         if  (_vselect == 0) {
203                 throw MackieControlException ("vselect is null");
204         }
205         return *_vselect;
206 }
207
208 Button& 
209 Strip::fader_touch()
210 {
211         if  (_fader_touch == 0) {
212                 throw MackieControlException ("fader_touch is null");
213         }
214         return *_fader_touch;
215 }
216
217 Meter& 
218 Strip::meter()
219 {
220         if  (_meter == 0) {
221                 throw MackieControlException ("meter is null");
222         }
223         return *_meter;
224 }
225
226 std::ostream & Mackie::operator <<  (std::ostream & os, const Strip & strip)
227 {
228         os << typeid (strip).name();
229         os << " { ";
230         os << "has_solo: " << boolalpha << strip.has_solo();
231         os << ", ";
232         os << "has_recenable: " << boolalpha << strip.has_recenable();
233         os << ", ";
234         os << "has_mute: " << boolalpha << strip.has_mute();
235         os << ", ";
236         os << "has_select: " << boolalpha << strip.has_select();
237         os << ", ";
238         os << "has_vselect: " << boolalpha << strip.has_vselect();
239         os << ", ";
240         os << "has_fader_touch: " << boolalpha << strip.has_fader_touch();
241         os << ", ";
242         os << "has_vpot: " << boolalpha << strip.has_vpot();
243         os << ", ";
244         os << "has_gain: " << boolalpha << strip.has_gain();
245         os << " }";
246         
247         return os;
248 }
249
250 void
251 Strip::set_route (boost::shared_ptr<Route> r)
252 {
253         route_connections.drop_connections ();
254
255         _route = r;
256
257         if (r) {
258                 
259                 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("Surface %1 strip %2 now mapping route %3\n",
260                                                                    _surface->number(), _index, _route->name()));
261                 
262
263                 if (has_solo()) {
264                         _route->solo_control()->Changed.connect(route_connections, invalidator(), ui_bind (&Strip::notify_solo_changed, this), ui_context());
265                 }
266                 if (has_mute()) {
267                         _route->mute_control()->Changed.connect(route_connections, invalidator(), ui_bind (&Strip::notify_mute_changed, this), ui_context());
268                 }
269                 
270                 if (has_gain()) {
271                         _route->gain_control()->Changed.connect(route_connections, invalidator(), ui_bind (&Strip::notify_gain_changed, this, false), ui_context());
272                 }
273                 
274                 _route->PropertyChanged.connect (route_connections, invalidator(), ui_bind (&Strip::notify_property_changed, this, _1), ui_context());
275                 
276                 if (_route->pannable()) {
277                         _route->pannable()->pan_azimuth_control->Changed.connect(route_connections, invalidator(), ui_bind (&Strip::notify_panner_changed, this, false), ui_context());
278                         _route->pannable()->pan_width_control->Changed.connect(route_connections, invalidator(), ui_bind (&Strip::notify_panner_changed, this, false), ui_context());
279                 }
280                 
281                 boost::shared_ptr<Track> trk = boost::dynamic_pointer_cast<ARDOUR::Track>(_route);
282         
283                 if (trk) {
284                         trk->rec_enable_control()->Changed .connect(route_connections, invalidator(), ui_bind (&Strip::notify_record_enable_changed, this), ui_context());
285                 }
286                 
287                 // TODO this works when a currently-banked route is made inactive, but not
288                 // when a route is activated which should be currently banked.
289                 
290                 _route->active_changed.connect (route_connections, invalidator(), ui_bind (&Strip::notify_active_changed, this), ui_context());
291                 _route->DropReferences.connect (route_connections, invalidator(), ui_bind (&Strip::notify_route_deleted, this), ui_context());
292         
293                 // TODO
294                 // SelectedChanged
295                 // RemoteControlIDChanged. Better handled at Session level.
296
297                 /* Update */
298
299                 notify_all ();
300         } else {
301                 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("Surface %1 strip %2 now unmapped\n",
302                                                                    _surface->number(), _index));
303         }
304 }
305
306 void 
307 Strip::notify_all()
308 {
309         if  (has_solo()) {
310                 notify_solo_changed ();
311         }
312         
313         if  (has_mute()) {
314                 notify_mute_changed ();
315         }
316         
317         if  (has_gain()) {
318                 notify_gain_changed ();
319         }
320         
321         notify_property_changed (PBD::PropertyChange (ARDOUR::Properties::name));
322         
323         if  (has_vpot()) {
324                 notify_panner_changed ();
325         }
326         
327         if  (has_recenable()) {
328                 notify_record_enable_changed ();
329         }
330 }
331
332 void 
333 Strip::notify_solo_changed ()
334 {
335         if (_route && _solo) {
336                 _surface->write (_solo->led().set_state (_route->soloed() ? on : off));
337         }
338 }
339
340 void 
341 Strip::notify_mute_changed ()
342 {
343         DEBUG_TRACE (DEBUG::MackieControl, string_compose ("Strip %1 mute changed\n", _index));
344         if (_route && _mute) {
345                 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("\troute muted ? %1\n", _route->muted()));
346                 _surface->write (_mute->led().set_state (_route->muted() ? on : off));
347         }
348 }
349
350 void 
351 Strip::notify_record_enable_changed ()
352 {
353         if (_route && _recenable)  {
354                 _surface->write (_recenable->led().set_state (_route->record_enabled() ? on : off));
355         }
356 }
357
358 void 
359 Strip::notify_active_changed ()
360 {
361         _surface->mcp().refresh_current_bank();
362 }
363
364 void 
365 Strip::notify_route_deleted ()
366 {
367         _surface->mcp().refresh_current_bank();
368 }
369
370 void 
371 Strip::notify_gain_changed (bool force_update)
372 {
373         if (_route) {
374                 Fader & fader = gain();
375
376                 if (!fader.in_use()) {
377                         float position = gain_to_slider_position (_route->gain_control()->get_value());
378                         // check that something has actually changed
379                         if (force_update || position != _last_gain_written) {
380                                 _surface->write (fader.set_position (position));
381                                 _last_gain_written = position;
382                         }
383                 }
384         }
385 }
386
387 void 
388 Strip::notify_property_changed (const PropertyChange& what_changed)
389 {
390         if (!what_changed.contains (ARDOUR::Properties::name)) {
391                 return;
392         }
393
394         if (_route) {
395                 string line1;
396                 string fullname = _route->name();
397                 
398                 if (fullname.length() <= 6) {
399                         line1 = fullname;
400                 } else {
401                         line1 = PBD::short_version (fullname, 6);
402                 }
403                 
404                 _surface->write (display (0, line1));
405                 _surface->write (blank_display (1));
406         }
407 }
408
409 void 
410 Strip::notify_panner_changed (bool force_update)
411 {
412         if (_route) {
413                 Pot & pot = vpot();
414                 boost::shared_ptr<Panner> panner = _route->panner();
415
416                 if (panner) {
417                         double pos = panner->position ();
418
419                         // cache the MidiByteArray here, because the mackie led control is much lower
420                         // resolution than the panner control. So we save lots of byte
421                         // sends in spite of more work on the comparison
422
423                         MidiByteArray bytes = pot.set_all (pos, true, Pot::dot);
424
425                         // check that something has actually changed
426                         if (force_update || bytes != _last_pan_written)
427                         {
428                                 _surface->write (bytes);
429                                 _last_pan_written = bytes;
430                         }
431                 } else {
432                         _surface->write (pot.zero());
433                 }
434         }
435 }
436
437 void
438 Strip::handle_button (Button& button, ButtonState bs)
439 {
440         button.set_in_use (bs == press);
441
442         if (!_route) {
443                 // no route so always switch the light off
444                 // because no signals will be emitted by a non-route
445                 _surface->write (button.led().set_state  (off));
446                 return;
447         }
448
449         if (bs == press) {
450                 if (button.id() >= Button::recenable_base_id &&
451                     button.id() < Button::recenable_base_id + 8) {
452
453                         _route->set_record_enabled (!_route->record_enabled(), this);
454
455                 } else if (button.id() >= Button::mute_base_id &&
456                            button.id() < Button::mute_base_id + 8) {
457
458                         _route->set_mute (!_route->muted(), this);
459
460                 } else if (button.id() >= Button::solo_base_id &&
461                            button.id() < Button::solo_base_id + 8) {
462
463                         _route->set_solo (!_route->soloed(), this);
464
465                 } else if (button.id() >= Button::select_base_id &&
466                            button.id() < Button::select_base_id + 8) {
467
468                         _surface->mcp().select_track (_route);
469
470                 } else if (button.id() >= Button::vselect_base_id &&
471                            button.id() < Button::vselect_base_id + 8) {
472
473                 }
474         }
475
476         if (button.id() >= Button::fader_touch_base_id &&
477             button.id() < Button::fader_touch_base_id + 8) {
478
479                 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("fader touch, press ? %1\n", (bs == press)));
480
481                 bool state = (bs == press);
482
483                 _gain->set_in_use (state);
484                 
485                 if (ARDOUR::Config->get_mackie_emulation() == "bcf" && state) {
486
487                         /* BCF faders don't support touch, so add a timeout to reset
488                            their `in_use' state.
489                         */
490
491                         _surface->mcp().add_in_use_timeout (*_surface, gain(), &fader_touch());
492                 }
493         }
494 }
495
496 void
497 Strip::handle_fader (Fader& fader, float position)
498 {
499         DEBUG_TRACE (DEBUG::MackieControl, string_compose ("fader to %1\n", position));
500
501         if (_route) {
502                 _route->gain_control()->set_value (slider_position_to_gain (position));
503         }
504         
505         if (ARDOUR::Config->get_mackie_emulation() == "bcf") {
506                 /* reset the timeout while we're still moving the fader */
507                 _surface->mcp().add_in_use_timeout (*_surface, fader, fader.in_use_touch_control);
508         }
509         
510         // must echo bytes back to slider now, because
511         // the notifier only works if the fader is not being
512         // touched. Which it is if we're getting input.
513
514         _surface->write (fader.set_position (position));
515 }
516
517 void
518 Strip::handle_pot (Pot& pot, ControlState& state)
519 {
520         if (!_route) {
521                 _surface->write (pot.set_onoff (false));
522                 return;
523         }
524
525         boost::shared_ptr<Pannable> pannable = _route->pannable();
526
527         if (pannable) {
528                 boost::shared_ptr<AutomationControl> ac;
529                 
530                 if (_surface->mcp().modifier_state() & MackieControlProtocol::MODIFIER_CONTROL) {
531                         ac = pannable->pan_width_control;
532                 } else {
533                         ac = pannable->pan_azimuth_control;
534                 }
535                 
536                 double p = ac->get_value();
537                 
538                 // calculate new value, and adjust
539                 p += state.delta * state.sign;
540                 p = min (1.0, p);
541                 p = max (0.0, p);
542
543                 ac->set_value (p);
544         }
545 }
546
547 void
548 Strip::periodic ()
549 {
550         if (!_route) {
551                 return;
552         }
553
554         update_automation ();
555         update_meter ();
556 }
557
558 void 
559 Strip::update_automation ()
560 {
561         ARDOUR::AutoState gain_state = _route->gain_control()->automation_state();
562
563         if (gain_state == Touch || gain_state == Play) {
564                 notify_gain_changed (false);
565         }
566
567         if (_route->panner()) {
568                 ARDOUR::AutoState panner_state = _route->panner()->automation_state();
569                 if (panner_state == Touch || panner_state == Play) {
570                         notify_panner_changed (false);
571                 }
572         }
573 }
574
575 void
576 Strip::update_meter ()
577 {
578         float dB = const_cast<PeakMeter&> (_route->peak_meter()).peak_power (0);
579         _surface->write (meter().update_message (dB));
580 }
581
582 MidiByteArray
583 Strip::zero ()
584 {
585         MidiByteArray retval;
586
587         for (Group::Controls::const_iterator it = _controls.begin(); it != _controls.end(); ++it) {
588                 retval << (*it)->zero ();
589         }
590
591         retval << blank_display (0);
592         retval << blank_display (1);
593         
594         return retval;
595 }
596
597 MidiByteArray
598 Strip::blank_display (uint32_t line_number)
599 {
600         return display (line_number, string());
601 }
602
603 MidiByteArray
604 Strip::display (uint32_t line_number, const std::string& line)
605 {
606         assert (line_number <= 1);
607
608         MidiByteArray retval;
609
610         DEBUG_TRACE (DEBUG::MackieControl, string_compose ("strip_display index: %1, line %2 = %3\n", _index, line_number, line));
611
612         // sysex header
613         retval << _surface->sysex_hdr();
614         
615         // code for display
616         retval << 0x12;
617         // offset (0 to 0x37 first line, 0x38 to 0x6f for second line)
618         retval << (_index * 7 + (line_number * 0x38));
619         
620         // ascii data to display
621         retval << line;
622         // pad with " " out to 6 chars
623         for (int i = line.length(); i < 6; ++i) {
624                 retval << ' ';
625         }
626         
627         // column spacer, unless it's the right-hand column
628         if (_index < 7) {
629                 retval << ' ';
630         }
631
632         // sysex trailer
633         retval << MIDI::eox;
634         
635         DEBUG_TRACE (DEBUG::MackieControl, string_compose ("MackieMidiBuilder::strip_display midi: %1\n", retval));
636
637         return retval;
638 }