MCP: more button tracing, more config stuff
[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 <sys/time.h>
25
26 #include "midi++/port.h"
27
28 #include "pbd/compose.h"
29 #include "pbd/convert.h"
30
31 #include "ardour/debug.h"
32 #include "ardour/midi_ui.h"
33 #include "ardour/route.h"
34 #include "ardour/track.h"
35 #include "ardour/pannable.h"
36 #include "ardour/panner.h"
37 #include "ardour/panner_shell.h"
38 #include "ardour/rc_configuration.h"
39 #include "ardour/meter.h"
40
41 #include "mackie_control_protocol.h"
42 #include "surface_port.h"
43 #include "surface.h"
44 #include "button.h"
45 #include "led.h"
46 #include "pot.h"
47 #include "fader.h"
48 #include "jog.h"
49 #include "meter.h"
50
51 using namespace Mackie;
52 using namespace std;
53 using namespace ARDOUR;
54 using namespace PBD;
55
56 #define ui_context() MackieControlProtocol::instance() /* a UICallback-derived object that specifies the event loop for signal handling */
57 #define ui_bind(f, ...) boost::protect (boost::bind (f, __VA_ARGS__))
58
59 extern PBD::EventLoop::InvalidationRecord* __invalidator (sigc::trackable& trackable, const char*, int);
60 #define invalidator() __invalidator (*(MackieControlProtocol::instance()), __FILE__, __LINE__)
61
62 Strip::Strip (Surface& s, const std::string& name, int index, StripControlDefinition* ctls)
63         : Group (name)
64         , _solo (0)
65         , _recenable (0)
66         , _mute (0)
67         , _select (0)
68         , _vselect (0)
69         , _fader_touch (0)
70         , _vpot (0)
71         , _fader (0)
72         , _index (index)
73         , _surface (&s)
74         , _controls_locked (false)
75         , _reset_display_at (0)
76         , _last_gain_position_written (-1.0)
77         , _last_pan_position_written (-1.0)
78 {
79         /* build the controls for this track, which will automatically add them
80            to the Group 
81         */
82
83         for (uint32_t i = 0; ctls[i].name[0]; ++i) {
84                 ctls[i].factory (*_surface, ctls[i].base_id + index, ctls[i].name, *this);
85         }
86 }       
87
88 Strip::~Strip ()
89 {
90 }
91
92 /**
93         TODO could optimise this to use enum, but it's only
94         called during the protocol class instantiation.
95 */
96 void Strip::add (Control & control)
97 {
98         Group::add (control);
99
100         Fader* fader;
101         Pot* pot;
102         Button* button;
103         Meter* meter;
104
105         if ((fader = dynamic_cast<Fader*>(&control)) != 0) {
106
107                 _fader = fader;
108
109         } else if ((pot = dynamic_cast<Pot*>(&control)) != 0) {
110
111                 _vpot = pot;
112
113         } else if ((button = dynamic_cast<Button*>(&control)) != 0) {
114
115                 if (control.id() >= Button::recenable_base_id &&
116                     control.id() < Button::recenable_base_id + 8) {
117                         
118                         _recenable = button;
119
120                 } else if (control.id() >= Button::mute_base_id &&
121                            control.id() < Button::mute_base_id + 8) {
122
123                         _mute = button;
124
125                 } else if (control.id() >= Button::solo_base_id &&
126                            control.id() < Button::solo_base_id + 8) {
127
128                         _solo = button;
129
130                 } else if (control.id() >= Button::select_base_id &&
131                            control.id() < Button::select_base_id + 8) {
132
133                         _select = button;
134
135                 } else if (control.id() >= Button::vselect_base_id &&
136                            control.id() < Button::vselect_base_id + 8) {
137
138                         _vselect = button;
139
140                 } else if (control.id() >= Button::fader_touch_base_id &&
141                            control.id() < Button::fader_touch_base_id + 8) {
142
143                         _fader_touch = button;
144                 }
145
146         } else if ((meter = dynamic_cast<Meter*>(&control)) != 0) {
147                 _meter = meter;
148         }
149 }
150
151 void
152 Strip::set_route (boost::shared_ptr<Route> r)
153 {
154         if (_controls_locked) {
155                 return;
156         }
157
158         route_connections.drop_connections ();
159
160         _route = r;
161
162         if (r) {
163
164                 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("Surface %1 strip %2 now mapping route %3\n",
165                                                                    _surface->number(), _index, _route->name()));
166                 
167
168                 if (_solo) {
169                         _solo->set_normal_control (_route->solo_control());
170                         _solo->set_modified_control (boost::shared_ptr<AutomationControl>());
171                         _route->solo_control()->Changed.connect(route_connections, invalidator(), ui_bind (&Strip::notify_solo_changed, this), ui_context());
172                 }
173
174                 if (_mute) {
175                         _mute->set_normal_control (_route->mute_control());
176                         _mute->set_modified_control (boost::shared_ptr<AutomationControl>());
177                         _route->mute_control()->Changed.connect(route_connections, invalidator(), ui_bind (&Strip::notify_mute_changed, this), ui_context());
178                 }
179                 
180                 _route->gain_control()->Changed.connect(route_connections, invalidator(), ui_bind (&Strip::notify_gain_changed, this, false), ui_context());
181                 
182                 _route->PropertyChanged.connect (route_connections, invalidator(), ui_bind (&Strip::notify_property_changed, this, _1), ui_context());
183                 
184                 if (_route->pannable()) {
185                         _route->pannable()->pan_azimuth_control->Changed.connect(route_connections, invalidator(), ui_bind (&Strip::notify_panner_changed, this, false), ui_context());
186                         _route->pannable()->pan_width_control->Changed.connect(route_connections, invalidator(), ui_bind (&Strip::notify_panner_changed, this, false), ui_context());
187                 }
188
189                 /* bind fader & pan pot, as appropriate for current flip mode */
190
191                 flip_mode_changed (false);
192                 
193                 boost::shared_ptr<Track> trk = boost::dynamic_pointer_cast<ARDOUR::Track>(_route);
194         
195                 if (trk) {
196                         _recenable->set_normal_control (trk->rec_enable_control());
197                         _recenable->set_modified_control (boost::shared_ptr<AutomationControl>());
198                         trk->rec_enable_control()->Changed .connect(route_connections, invalidator(), ui_bind (&Strip::notify_record_enable_changed, this), ui_context());
199                 }
200                 
201                 // TODO this works when a currently-banked route is made inactive, but not
202                 // when a route is activated which should be currently banked.
203                 
204                 _route->active_changed.connect (route_connections, invalidator(), ui_bind (&Strip::notify_active_changed, this), ui_context());
205                 _route->DropReferences.connect (route_connections, invalidator(), ui_bind (&Strip::notify_route_deleted, this), ui_context());
206         
207                 // TODO
208                 // SelectedChanged
209                 // RemoteControlIDChanged. Better handled at Session level.
210
211                 /* Update */
212
213                 notify_all ();
214         } else {
215                 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("Surface %1 strip %2 now unmapped\n",
216                                                                    _surface->number(), _index));
217         }
218 }
219
220 void 
221 Strip::notify_all()
222 {
223         notify_solo_changed ();
224         notify_mute_changed ();
225         notify_gain_changed ();
226         notify_property_changed (PBD::PropertyChange (ARDOUR::Properties::name));
227         notify_panner_changed ();
228         notify_record_enable_changed ();
229 }
230
231 void 
232 Strip::notify_solo_changed ()
233 {
234         if (_route && _solo) {
235                 _surface->write (_solo->set_state (_route->soloed() ? on : off));
236         }
237 }
238
239 void 
240 Strip::notify_mute_changed ()
241 {
242         DEBUG_TRACE (DEBUG::MackieControl, string_compose ("Strip %1 mute changed\n", _index));
243         if (_route && _mute) {
244                 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("\troute muted ? %1\n", _route->muted()));
245                 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("mute message: %1\n", _mute->set_state (_route->muted() ? on : off)));
246
247                 _surface->write (_mute->set_state (_route->muted() ? on : off));
248         }
249 }
250
251 void 
252 Strip::notify_record_enable_changed ()
253 {
254         if (_route && _recenable)  {
255                 _surface->write (_recenable->set_state (_route->record_enabled() ? on : off));
256         }
257 }
258
259 void 
260 Strip::notify_active_changed ()
261 {
262         _surface->mcp().refresh_current_bank();
263 }
264
265 void 
266 Strip::notify_route_deleted ()
267 {
268         _surface->mcp().refresh_current_bank();
269 }
270
271 void 
272 Strip::notify_gain_changed (bool force_update)
273 {
274         DEBUG_TRACE (DEBUG::MackieControl, string_compose ("gain changed for strip %1, flip mode %2\n", _index, _surface->mcp().flip_mode()));
275
276         if (_route) {
277                 
278                 Control* control;
279
280                 if (_surface->mcp().flip_mode()) {
281                         control = _vpot;
282                 } else {
283                         control = _fader;
284                 }
285
286                 if (!control->in_use()) {
287
288                         float pos = _route->gain_control()->internal_to_interface (_route->gain_control()->get_value());
289
290                         if (force_update || pos != _last_gain_position_written) {
291
292                                 if (_surface->mcp().flip_mode()) {
293                                         _surface->write (_vpot->set_all (pos, true, Pot::wrap));
294                                 } else {
295                                         _surface->write (_fader->set_position (pos));
296                                 }
297
298                                 float dB = fast_coefficient_to_dB (pos);
299                                 if (pos == 0.0) {
300                                         _surface->write (display (1, "   0.0"));
301                                 } else {
302                                         char buf[16];
303                                         
304                                         snprintf (buf, sizeof (buf), "%6.1f", dB);
305                                         _surface->write (display (1, buf));
306                                 }
307
308                                 queue_display_reset (500);
309                                 _last_gain_position_written = pos;
310                                 
311                         } else {
312                                 DEBUG_TRACE (DEBUG::MackieControl, "value is stale, no message sent\n");
313                         }
314                 } else {
315                         DEBUG_TRACE (DEBUG::MackieControl, "fader in use, no message sent\n");
316                 }
317         } else {
318                 DEBUG_TRACE (DEBUG::MackieControl, "no route or no fader\n");
319         }
320 }
321
322 void 
323 Strip::notify_property_changed (const PropertyChange& what_changed)
324 {
325         if (!what_changed.contains (ARDOUR::Properties::name)) {
326                 return;
327         }
328
329         if (_route) {
330                 string line1;
331                 string fullname = _route->name();
332                 
333                 if (fullname.length() <= 6) {
334                         line1 = fullname;
335                 } else {
336                         line1 = PBD::short_version (fullname, 6);
337                 }
338                 
339                 _surface->write (display (0, line1));
340         }
341 }
342
343 void 
344 Strip::notify_panner_changed (bool force_update)
345 {
346         if (_route) {
347
348                 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("pan change for strip %1\n", _index));
349
350                 boost::shared_ptr<Pannable> pannable = _route->pannable();
351
352                 if (!pannable) {
353                         _surface->write (_vpot->zero());
354                         return;
355                 }
356
357                 Control* control;
358
359                 if (_surface->mcp().flip_mode()) {
360                         control = _fader;
361                 } else {
362                         control = _vpot;
363                 }
364
365                 if (!control->in_use()) {
366                         
367                         double pos = pannable->pan_azimuth_control->internal_to_interface (pannable->pan_azimuth_control->get_value());
368                         
369                         if (force_update || pos != _last_pan_position_written) {
370                                 
371                                 if (_surface->mcp().flip_mode()) {
372                                         
373                                         _surface->write (_fader->set_position (pos));
374                                         
375                                 } else {
376                                         _surface->write (_vpot->set_all (pos, true, Pot::dot));
377                                 }
378                                 
379                                 if (pannable->panner()) {
380                                         string str = pannable->panner()->value_as_string (pannable->pan_azimuth_control);
381                                         _surface->write (display (1, str));
382                                         queue_display_reset (500);
383                                 }
384                                 
385                                 _last_pan_position_written = pos;
386                         }
387                 }
388         }
389 }
390
391 void
392 Strip::handle_button (Button& button, ButtonState bs)
393 {
394         if (bs == press) {
395                 button.set_in_use (true);
396         } else {
397                 button.set_in_use (false);
398         }
399
400         DEBUG_TRACE (DEBUG::MackieControl, string_compose ("strip %1 handling button %2\n", _index, button.id()));
401
402         int lock_mod = (MackieControlProtocol::MODIFIER_CONTROL|MackieControlProtocol::MODIFIER_SHIFT);
403         int ms = _surface->mcp().modifier_state();
404         bool modified = (ms & MackieControlProtocol::MODIFIER_CONTROL);
405
406         if (button.id() >= Button::select_base_id &&
407             button.id() < Button::select_base_id + 8) {
408
409                 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("select touch, lock ? %1\n", ((ms & lock_mod) == lock_mod) ? 1 : 0));
410
411                 if (bs == press) {
412
413                         if ((ms & lock_mod) == lock_mod) {
414                                 _controls_locked = !_controls_locked;
415                                 return;
416                         }
417                         
418                         DEBUG_TRACE (DEBUG::MackieControl, "add select button on press\n");
419                         _surface->mcp().add_down_select_button (_surface->number(), _index);                    
420
421                         _surface->mcp().select_range ();
422
423                 } else {
424                         DEBUG_TRACE (DEBUG::MackieControl, "remove select button on release\n");
425                         _surface->mcp().remove_down_select_button (_surface->number(), _index);                 
426                 }
427
428                 return;
429         }
430
431         if (button.id() >= Button::fader_touch_base_id &&
432             button.id() < Button::fader_touch_base_id + 8) {
433
434                 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("fader touch, press ? %1\n", (bs == press)));
435
436                 bool state = (bs == press);
437                 
438                 _fader->set_in_use (state);
439                 _fader->start_touch (_surface->mcp().transport_frame(), modified);
440
441                 if (bs != press) {
442                         /* fader touch ended, revert back to label display for fader */
443                         _surface->write (display (1, static_display_string()));
444                 }
445                 
446                 return;
447         }
448
449         boost::shared_ptr<AutomationControl> control = button.control (modified);
450                 
451         if (control) {
452
453                 if (bs == press) {
454                         DEBUG_TRACE (DEBUG::MackieControl, "add button on release\n");
455                         _surface->mcp().add_down_button ((AutomationType) control->parameter().type(), _surface->number(), _index);
456                         
457                         float new_value;
458
459                         if (ms & MackieControlProtocol::MODIFIER_OPTION) {
460                                 /* reset to default/normal value */
461                                 new_value = control->normal();
462                         } else {
463                                 new_value = control->get_value() ? 0.0 : 1.0;
464                         }
465
466                         /* get all controls that either have their
467                          * button down or are within a range of
468                          * several down buttons
469                          */
470                         
471                         MackieControlProtocol::ControlList controls = _surface->mcp().down_controls ((AutomationType) control->parameter().type());
472                         
473                         
474                         DEBUG_TRACE (DEBUG::MackieControl, string_compose ("there are %1 buttons down for control type %2, new value = %3\n",
475                                                                             controls.size(), control->parameter().type(), new_value));
476
477                         /* apply change */
478
479                         for (MackieControlProtocol::ControlList::iterator c = controls.begin(); c != controls.end(); ++c) {
480                                 (*c)->set_value (new_value);
481                         }
482
483                 } else {
484                         DEBUG_TRACE (DEBUG::MackieControl, "remove button on release\n");
485                         _surface->mcp().remove_down_button ((AutomationType) control->parameter().type(), _surface->number(), _index);
486                 }
487                         
488         } else {
489                 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("button has no control at present (modified ? %1)\n", modified));
490         }
491 }
492
493 void
494 Strip::handle_fader (Fader& fader, float position)
495 {
496         DEBUG_TRACE (DEBUG::MackieControl, string_compose ("fader to %1\n", position));
497
498         bool modified = (_surface->mcp().modifier_state() & MackieControlProtocol::MODIFIER_CONTROL);
499
500         fader.set_value (position, modified);
501         fader.start_touch (_surface->mcp().transport_frame(), modified);
502
503         if (!_surface->mcp().device_info().has_touch_sense_faders()) {
504                 _surface->mcp().add_in_use_timeout (*_surface, fader, fader.control (modified));
505         }
506         
507         // must echo bytes back to slider now, because
508         // the notifier only works if the fader is not being
509         // touched. Which it is if we're getting input.
510
511         _surface->write (fader.set_position (position));
512 }
513
514 void
515 Strip::handle_pot (Pot& pot, float delta)
516 {
517         /* Pots only emit events when they move, not when they
518            stop moving. So to get a stop event, we need to use a timeout.
519         */
520         
521         bool modified = (_surface->mcp().modifier_state() & MackieControlProtocol::MODIFIER_CONTROL);
522         pot.start_touch (_surface->mcp().transport_frame(), modified);
523         _surface->mcp().add_in_use_timeout (*_surface, pot, pot.control (modified));
524
525         double p = pot.get_value (modified);
526         p += delta;
527         p = min (1.0, p);
528         p = max (0.0, p);
529         pot.set_value (p, modified);
530 }
531
532 void
533 Strip::periodic (uint64_t usecs)
534 {
535         if (!_route) {
536                 return;
537         }
538
539         update_automation ();
540         update_meter ();
541
542         if (_reset_display_at && _reset_display_at < usecs) {
543                 reset_display ();
544         }
545 }
546
547 void 
548 Strip::update_automation ()
549 {
550         ARDOUR::AutoState gain_state = _route->gain_control()->automation_state();
551
552         if (gain_state == Touch || gain_state == Play) {
553                 notify_gain_changed (false);
554         }
555
556         if (_route->panner()) {
557                 ARDOUR::AutoState panner_state = _route->panner()->automation_state();
558                 if (panner_state == Touch || panner_state == Play) {
559                         notify_panner_changed (false);
560                 }
561         }
562 }
563
564 void
565 Strip::update_meter ()
566 {
567         if (_meter) {
568                 float dB = const_cast<PeakMeter&> (_route->peak_meter()).peak_power (0);
569                 _surface->write (_meter->update_message (dB));
570         }
571 }
572
573 MidiByteArray
574 Strip::zero ()
575 {
576         MidiByteArray retval;
577
578         for (Group::Controls::const_iterator it = _controls.begin(); it != _controls.end(); ++it) {
579                 retval << (*it)->zero ();
580         }
581
582         retval << blank_display (0);
583         retval << blank_display (1);
584         
585         return retval;
586 }
587
588 MidiByteArray
589 Strip::blank_display (uint32_t line_number)
590 {
591         return display (line_number, string());
592 }
593
594 MidiByteArray
595 Strip::display (uint32_t line_number, const std::string& line)
596 {
597         assert (line_number <= 1);
598
599         MidiByteArray retval;
600
601         DEBUG_TRACE (DEBUG::MackieControl, string_compose ("strip_display index: %1, line %2 = %3\n", _index, line_number, line));
602
603         // sysex header
604         retval << _surface->sysex_hdr();
605         
606         // code for display
607         retval << 0x12;
608         // offset (0 to 0x37 first line, 0x38 to 0x6f for second line)
609         retval << (_index * 7 + (line_number * 0x38));
610         
611         // ascii data to display
612         retval << line;
613         // pad with " " out to 6 chars
614         for (int i = line.length(); i < 6; ++i) {
615                 retval << ' ';
616         }
617         
618         // column spacer, unless it's the right-hand column
619         if (_index < 7) {
620                 retval << ' ';
621         }
622
623         // sysex trailer
624         retval << MIDI::eox;
625         
626         DEBUG_TRACE (DEBUG::MackieControl, string_compose ("MackieMidiBuilder::strip_display midi: %1\n", retval));
627
628         return retval;
629 }
630
631 void
632 Strip::lock_controls ()
633 {
634         _controls_locked = true;
635 }
636
637 void
638 Strip::unlock_controls ()
639 {
640         _controls_locked = false;
641 }
642
643 MidiByteArray
644 Strip::gui_selection_changed (ARDOUR::RouteNotificationListPtr rl)
645 {
646         for (ARDOUR::RouteNotificationList::iterator i = rl->begin(); i != rl->end(); ++i) {
647                 if ((*i) == _route) {
648                         return _select->set_state (on);
649                 }
650         }
651
652         return _select->set_state (off);
653 }
654
655 string
656 Strip::static_display_string () const
657 {
658         if (_surface->mcp().flip_mode()) {
659                 return "Fader";
660         } else {
661                 return "Pan";
662         }
663 }
664
665 void
666 Strip::flip_mode_changed (bool notify)
667 {
668         if (!_route) {
669                 return;
670         }
671
672         boost::shared_ptr<Pannable> pannable = _route->pannable();
673
674         if (_surface->mcp().flip_mode()) {
675
676                 if (pannable) {
677                         _fader->set_normal_control (pannable->pan_azimuth_control);
678                         _fader->set_modified_control (pannable->pan_width_control);
679                 }
680                 _vpot->set_normal_control (_route->gain_control());
681                 _vpot->set_modified_control (boost::shared_ptr<AutomationControl>());
682
683                 _surface->write (display (1, static_display_string ()));
684
685         } else {
686
687                 if (pannable) {
688                         _vpot->set_normal_control (pannable->pan_azimuth_control);
689                         _vpot->set_modified_control (pannable->pan_width_control);
690                 }
691                 _fader->set_normal_control (_route->gain_control());
692                 _fader->set_modified_control (boost::shared_ptr<AutomationControl>());
693
694                 _surface->write (display (1, static_display_string()));
695         }
696
697         if (notify) {
698                 notify_all ();
699         }
700 }
701
702 void
703 Strip::queue_display_reset (uint32_t msecs)
704 {
705         struct timeval now;
706         struct timeval delta;
707         struct timeval when;
708         gettimeofday (&now, 0);
709         
710         delta.tv_sec = msecs/1000;
711         delta.tv_usec = (msecs - ((msecs/1000) * 1000)) * 1000;
712         
713         timeradd (&now, &delta, &when);
714
715         _reset_display_at = (when.tv_sec * 1000000) + when.tv_usec;
716 }
717
718 void
719 Strip::clear_display_reset ()
720 {
721         _reset_display_at = 0;
722 }
723
724 void
725 Strip::reset_display ()
726 {
727         _surface->write (display (1, static_display_string()));
728         clear_display_reset ();
729 }
730