Call PBD::EnumWriter::destroy in ARDOUR::cleanup (from
[ardour.git] / libs / surfaces / mackie / surface.cc
1 #include <sstream>
2 #include <iomanip>
3 #include <iostream>
4 #include <cstdio>
5 #include <cmath>
6
7 #include "midi++/port.h"
8 #include "midi++/manager.h"
9
10 #include "ardour/automation_control.h"
11 #include "ardour/debug.h"
12 #include "ardour/route.h"
13 #include "ardour/panner.h"
14 #include "ardour/panner_shell.h"
15 #include "ardour/rc_configuration.h"
16 #include "ardour/session.h"
17 #include "ardour/utils.h"
18
19 #include "control_group.h"
20 #include "surface_port.h"
21 #include "surface.h"
22 #include "strip.h"
23 #include "mackie_control_protocol.h"
24 #include "jog_wheel.h"
25
26 #include "strip.h"
27 #include "button.h"
28 #include "led.h"
29 #include "pot.h"
30 #include "fader.h"
31 #include "jog.h"
32 #include "meter.h"
33
34 #include "i18n.h"
35
36 using namespace std;
37 using namespace PBD;
38 using namespace Mackie;
39 using ARDOUR::Route;
40 using ARDOUR::Panner;
41 using ARDOUR::Pannable;
42 using ARDOUR::AutomationControl;
43
44 #define ui_context() MackieControlProtocol::instance() /* a UICallback-derived object that specifies the event loop for signal handling */
45
46 // The MCU sysex header.4th byte Will be overwritten
47 // when we get an incoming sysex that identifies
48 // the device type
49 static MidiByteArray mackie_sysex_hdr  (5, MIDI::sysex, 0x0, 0x0, 0x66, 0x14);
50
51 // The MCU extender sysex header.4th byte Will be overwritten
52 // when we get an incoming sysex that identifies
53 // the device type
54 static MidiByteArray mackie_sysex_hdr_xt  (5, MIDI::sysex, 0x0, 0x0, 0x66, 0x15);
55
56 static MidiByteArray empty_midi_byte_array;
57
58 Surface::Surface (MackieControlProtocol& mcp, const std::string& device_name, uint32_t number, surface_type_t stype)
59         : _mcp (mcp)
60         , _stype (stype)
61         , _number (number)
62         , _name (device_name)
63         , _active (false)
64         , _connected (false)
65         , _jog_wheel (0)
66 {
67         DEBUG_TRACE (DEBUG::MackieControl, "Surface::init\n");
68         
69         _port = new SurfacePort (*this);
70
71         /* only the first Surface object has global controls */
72
73         if (_number == 0) {
74                 if (_mcp.device_info().has_global_controls()) {
75                         init_controls ();
76                 }
77
78                 if (_mcp.device_info().has_master_fader()) {
79                         setup_master ();
80                 }
81         }
82
83         uint32_t n = _mcp.device_info().strip_cnt();
84         
85         if (n) {
86                 init_strips (n);
87         }
88         
89         connect_to_signals ();
90
91         DEBUG_TRACE (DEBUG::MackieControl, "Surface::init finish\n");
92 }
93
94 Surface::~Surface ()
95 {
96         DEBUG_TRACE (DEBUG::MackieControl, "Surface: destructor\n");
97
98         zero_all ();
99
100         // delete groups
101         for (Groups::iterator it = groups.begin(); it != groups.end(); ++it) {
102                 delete it->second;
103         }
104         
105         // delete controls
106         for (Controls::iterator it = controls.begin(); it != controls.end(); ++it) {
107                 delete *it;
108         }
109         
110         delete _jog_wheel;
111         delete _port;
112 }
113
114 const MidiByteArray& 
115 Surface::sysex_hdr() const
116 {
117         switch  (_stype) {
118         case mcu: return mackie_sysex_hdr;
119         case ext: return mackie_sysex_hdr_xt;
120         }
121         cout << "SurfacePort::sysex_hdr _port_type not known" << endl;
122         return mackie_sysex_hdr;
123 }
124
125 static GlobalControlDefinition mackie_global_controls[] = {
126         { "external", Pot::External, Pot::factory, "none" },
127         { "fader_touch", Led::FaderTouch, Led::factory, "master" },
128         { "timecode", Led::Timecode, Led::factory, "none" },
129         { "beats", Led::Beats, Led::factory, "none" },
130         { "solo", Led::RudeSolo, Led::factory, "none" },
131         { "relay_click", Led::RelayClick, Led::factory, "none" },
132         { "", 0, Led::factory, "" }
133 };
134
135 void 
136 Surface::init_controls()
137 {
138         Group* group;
139
140         groups["assignment"] = new Group  ("assignment");
141         groups["automation"] = new Group  ("automation");
142         groups["bank"] = new Group  ("bank");
143         groups["cursor"] = new Group  ("cursor");
144         groups["display"] = new Group  ("display");
145         groups["functions"] = new Group  ("functions");
146         groups["modifiers"] = new Group  ("modifiers");
147         groups["none"] = new Group  ("none");
148         groups["transport"] = new Group  ("transport");
149         groups["user"] = new Group  ("user");
150         groups["master"] = new Group ("master");
151         groups["view"] = new Group ("view");
152                 
153         if (_mcp.device_info().has_jog_wheel()) {
154                 _jog_wheel = new Mackie::JogWheel (_mcp);
155         }
156
157         for (uint32_t n = 0; mackie_global_controls[n].name[0]; ++n) {
158                 group = groups[mackie_global_controls[n].group_name];
159                 Control* control = mackie_global_controls[n].factory (*this, mackie_global_controls[n].id, mackie_global_controls[n].name, *group);
160                 controls_by_device_independent_id[mackie_global_controls[n].id] = control;
161         }
162
163         /* add global buttons */
164
165         const map<Button::ID,GlobalButtonInfo>& global_buttons (_mcp.device_info().global_buttons());
166
167         for (map<Button::ID,GlobalButtonInfo>::const_iterator b = global_buttons.begin(); b != global_buttons.end(); ++b){
168                 group = groups[b->second.group];
169                 controls_by_device_independent_id[b->first] = Button::factory (*this, b->first, b->second.id, b->second.label, *group);
170         }
171 }
172
173 void 
174 Surface::init_strips (uint32_t n)
175 {
176         const map<Button::ID,StripButtonInfo>& strip_buttons (_mcp.device_info().strip_buttons());
177
178         for (uint32_t i = 0; i < n; ++i) {
179
180                 char name[32];
181                 
182                 snprintf (name, sizeof (name), "strip_%d", (8* _number) + i);
183
184                 Strip* strip = new Strip (*this, name, i, strip_buttons);
185                 
186                 groups[name] = strip;
187                 strips.push_back (strip);
188         }
189 }
190
191 void
192 Surface::setup_master ()
193 {
194         _master_fader = dynamic_cast<Fader*> (Fader::factory (*this, 8, "master", *groups["master"]));
195         
196         boost::shared_ptr<Route> m;
197         
198         if ((m = _mcp.get_session().monitor_out()) == 0) {
199                 m = _mcp.get_session().master_out();
200         } 
201         
202         if (!m) {
203                 return;
204         }
205         
206         _master_fader->set_control (m->gain_control());
207         m->gain_control()->Changed.connect (*this, MISSING_INVALIDATOR, boost::bind (&Surface::master_gain_changed, this), ui_context());
208 }
209
210 void
211 Surface::master_gain_changed ()
212 {
213         boost::shared_ptr<AutomationControl> ac = _master_fader->control();
214         float pos = ac->internal_to_interface (ac->get_value());
215         _port->write (_master_fader->set_position (pos));
216 }
217
218 float 
219 Surface::scaled_delta (float delta, float current_speed)
220 {
221         /* XXX needs work before use */
222         const float sign = delta < 0.0 ? -1.0 : 1.0;
223
224         return ((sign * std::pow (delta + 1.0, 2.0)) + current_speed) / 100.0;
225 }
226
227 void 
228 Surface::display_bank_start (uint32_t current_bank)
229 {
230         if  (current_bank == 0) {
231                 // send Ar. to 2-char display on the master
232                 show_two_char_display ("Ar", "..");
233         } else {
234                 // write the current first remote_id to the 2-char display
235                 show_two_char_display (current_bank);
236         }
237 }
238
239 void 
240 Surface::blank_jog_ring ()
241 {
242         Control* control = controls_by_device_independent_id[Jog::ID];
243
244         if (control) {
245                 Pot* pot = dynamic_cast<Pot*> (control);
246                 if (pot) {
247                         _port->write (pot->set (0.0, false, Pot::spread));
248                 }
249         }
250 }
251
252 float
253 Surface::scrub_scaling_factor () const
254 {
255         return 100.0;
256 }
257
258 void 
259 Surface::connect_to_signals ()
260 {
261         if (!_connected) {
262
263
264                 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("Surface %1 connecting to signals on port %2\n", 
265                                                                    number(), _port->input_port().name()));
266
267                 MIDI::Parser* p = _port->input_port().parser();
268
269                 /* Incoming sysex */
270                 p->sysex.connect_same_thread (*this, boost::bind (&Surface::handle_midi_sysex, this, _1, _2, _3));
271                 /* V-Pot messages are Controller */
272                 p->controller.connect_same_thread (*this, boost::bind (&Surface::handle_midi_controller_message, this, _1, _2));
273                 /* Button messages are NoteOn */
274                 p->note_on.connect_same_thread (*this, boost::bind (&Surface::handle_midi_note_on_message, this, _1, _2));
275                 /* Button messages are NoteOn. libmidi++ sends note-on w/velocity = 0 as note-off so catch them too */
276                 p->note_off.connect_same_thread (*this, boost::bind (&Surface::handle_midi_note_on_message, this, _1, _2));
277                 /* Fader messages are Pitchbend */
278                 p->channel_pitchbend[0].connect_same_thread (*this, boost::bind (&Surface::handle_midi_pitchbend_message, this, _1, _2, 0U));
279                 p->channel_pitchbend[1].connect_same_thread (*this, boost::bind (&Surface::handle_midi_pitchbend_message, this, _1, _2, 1U));
280                 p->channel_pitchbend[2].connect_same_thread (*this, boost::bind (&Surface::handle_midi_pitchbend_message, this, _1, _2, 2U));
281                 p->channel_pitchbend[3].connect_same_thread (*this, boost::bind (&Surface::handle_midi_pitchbend_message, this, _1, _2, 3U));
282                 p->channel_pitchbend[4].connect_same_thread (*this, boost::bind (&Surface::handle_midi_pitchbend_message, this, _1, _2, 4U));
283                 p->channel_pitchbend[5].connect_same_thread (*this, boost::bind (&Surface::handle_midi_pitchbend_message, this, _1, _2, 5U));
284                 p->channel_pitchbend[6].connect_same_thread (*this, boost::bind (&Surface::handle_midi_pitchbend_message, this, _1, _2, 6U));
285                 p->channel_pitchbend[7].connect_same_thread (*this, boost::bind (&Surface::handle_midi_pitchbend_message, this, _1, _2, 7U));
286                 
287                 _connected = true;
288         }
289 }
290
291 void
292 Surface::handle_midi_pitchbend_message (MIDI::Parser&, MIDI::pitchbend_t pb, uint32_t fader_id)
293 {
294         /* Pitchbend messages are fader messages. Nothing in the data we get
295          * from the MIDI::Parser conveys the fader ID, which was given by the
296          * channel ID in the status byte.
297          *
298          * Instead, we have used bind() to supply the fader-within-strip ID 
299          * when we connected to the per-channel pitchbend events.
300          */
301
302
303         DEBUG_TRACE (DEBUG::MackieControl, string_compose ("handle_midi pitchbend on port %3, fader = %1 value = %2\n", 
304                                                            fader_id, pb, _number));
305         
306         if (_mcp.device_info().no_handshake()) {
307                 turn_it_on ();
308         }
309
310         Fader* fader = faders[fader_id];
311
312         if (fader) {
313                 Strip* strip = dynamic_cast<Strip*> (&fader->group());
314                 float pos = (pb >> 4)/1023.0; // only the top 10 bytes are used
315                 if (strip) {
316                         strip->handle_fader (*fader, pos);
317                 } else {
318                         /* master fader */
319                         fader->set_value (pos); // alter master gain
320                         _port->write (fader->set_position (pos)); // write back value (required for servo)
321                 }
322         } else {
323                 DEBUG_TRACE (DEBUG::MackieControl, "fader not found\n");
324         }
325 }
326
327 void 
328 Surface::handle_midi_note_on_message (MIDI::Parser &, MIDI::EventTwoBytes* ev)
329 {
330         DEBUG_TRACE (DEBUG::MackieControl, string_compose ("SurfacePort::handle_note_on %1 = %2\n", (int) ev->note_number, (int) ev->velocity));
331         
332         if (_mcp.device_info().no_handshake()) {
333                 turn_it_on ();
334         }
335
336         Button* button = buttons[ev->note_number];
337
338         if (button) {
339                 Strip* strip = dynamic_cast<Strip*> (&button->group());
340
341                 if (strip) {
342                         DEBUG_TRACE (DEBUG::MackieControl, string_compose ("strip %1 button %2 pressed ? %3\n",
343                                                                            strip->index(), button->name(), (ev->velocity > 64)));
344                         strip->handle_button (*button, ev->velocity > 64 ? press : release);
345                 } else {
346                         /* global button */
347                         DEBUG_TRACE (DEBUG::MackieControl, string_compose ("global button %1\n", button->id()));
348                         _mcp.handle_button_event (*this, *button, ev->velocity > 64 ? press : release);
349                 }
350         } else {
351                 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("no button found for %1\n", ev->note_number));
352         }
353 }
354
355 void 
356 Surface::handle_midi_controller_message (MIDI::Parser &, MIDI::EventTwoBytes* ev)
357 {
358         DEBUG_TRACE (DEBUG::MackieControl, string_compose ("SurfacePort::handle_midi_controller %1 = %2\n", (int) ev->controller_number, (int) ev->value));
359
360         if (_mcp.device_info().no_handshake()) {
361                 turn_it_on ();
362         }
363
364         Pot* pot = pots[ev->controller_number];
365
366         // bit 6 gives the sign
367         float sign = (ev->value & 0x40) == 0 ? 1.0 : -1.0; 
368         // bits 0..5 give the velocity. we interpret this as "ticks
369         // moved before this message was sent"
370         float ticks = (ev->value & 0x3f);
371         if (ticks == 0) {
372                 /* euphonix and perhaps other devices send zero
373                    when they mean 1, we think.
374                 */
375                 ticks = 1;
376         }
377         float delta = sign * (ticks / (float) 0x3f);
378         
379         if (!pot) {
380                 if (ev->controller_number == Jog::ID && _jog_wheel) {
381
382                         DEBUG_TRACE (DEBUG::MackieControl, string_compose ("Jog wheel moved %1\n", ticks));
383                         _jog_wheel->jog_event (delta);
384                         return;
385                 }
386
387                 return;
388         }
389
390         Strip* strip = dynamic_cast<Strip*> (&pot->group());
391         if (strip) {
392                 strip->handle_pot (*pot, delta);
393         } 
394 }
395
396 void 
397 Surface::handle_midi_sysex (MIDI::Parser &, MIDI::byte * raw_bytes, size_t count)
398 {
399         MidiByteArray bytes (count, raw_bytes);
400
401         DEBUG_TRACE (DEBUG::MackieControl, string_compose ("handle_midi_sysex: %1\n", bytes));
402
403         if (_mcp.device_info().no_handshake()) {
404                 turn_it_on ();
405         }
406
407         /* always save the device type ID so that our outgoing sysex messages
408          * are correct 
409          */
410
411         if (_stype == mcu) {
412                 mackie_sysex_hdr[4] = bytes[4];
413         } else {
414                 mackie_sysex_hdr_xt[4] = bytes[4];
415         }
416
417         switch (bytes[5]) {
418         case 0x01:
419                 /* MCP: Device Ready 
420                    LCP: Connection Challenge 
421                 */
422                 if (bytes[4] == 0x10 || bytes[4] == 0x11) {
423                         write_sysex (host_connection_query (bytes));
424                 } else {
425                         if (!_active) {
426                                 turn_it_on ();
427                         }
428                 }
429                 break;
430
431         case 0x03: /* LCP Connection Confirmation */
432                 if (bytes[4] == 0x10 || bytes[4] == 0x11) {
433                         write_sysex (host_connection_confirmation (bytes));
434                         _active = true;
435                 }
436                 break;
437
438         case 0x04: /* LCP: Confirmation Denied */
439                 _active = false;
440                 break;
441         default:
442                 error << "MCP: unknown sysex: " << bytes << endmsg;
443         }
444 }
445
446 static MidiByteArray 
447 calculate_challenge_response (MidiByteArray::iterator begin, MidiByteArray::iterator end)
448 {
449         MidiByteArray l;
450         back_insert_iterator<MidiByteArray> back  (l);
451         copy (begin, end, back);
452         
453         MidiByteArray retval;
454         
455         // this is how to calculate the response to the challenge.
456         // from the Logic docs.
457         retval <<  (0x7f &  (l[0] +  (l[1] ^ 0xa) - l[3]));
458         retval <<  (0x7f &  ( (l[2] >> l[3]) ^  (l[0] + l[3])));
459         retval <<  (0x7f &  ((l[3] -  (l[2] << 2)) ^  (l[0] | l[1])));
460         retval <<  (0x7f &  (l[1] - l[2] +  (0xf0 ^  (l[3] << 4))));
461         
462         return retval;
463 }
464
465 // not used right now
466 MidiByteArray 
467 Surface::host_connection_query (MidiByteArray & bytes)
468 {
469         MidiByteArray response;
470         
471         if (bytes[4] != 0x10 && bytes[4] != 0x11) {
472                 /* not a Logic Control device - no response required */
473                 return response;
474         }
475
476         // handle host connection query
477         DEBUG_TRACE (DEBUG::MackieControl, string_compose ("host connection query: %1\n", bytes));
478         
479         if  (bytes.size() != 18) {
480                 cerr << "expecting 18 bytes, read " << bytes << " from " << _port->input_port().name() << endl;
481                 return response;
482         }
483
484         // build and send host connection reply
485         response << 0x02;
486         copy (bytes.begin() + 6, bytes.begin() + 6 + 7, back_inserter (response));
487         response << calculate_challenge_response (bytes.begin() + 6 + 7, bytes.begin() + 6 + 7 + 4);
488         return response;
489 }
490
491 // not used right now
492 MidiByteArray 
493 Surface::host_connection_confirmation (const MidiByteArray & bytes)
494 {
495         DEBUG_TRACE (DEBUG::MackieControl, string_compose ("host_connection_confirmation: %1\n", bytes));
496         
497         // decode host connection confirmation
498         if  (bytes.size() != 14) {
499                 ostringstream os;
500                 os << "expecting 14 bytes, read " << bytes << " from " << _port->input_port().name();
501                 throw MackieControlException (os.str());
502         }
503         
504         // send version request
505         return MidiByteArray (2, 0x13, 0x00);
506 }
507
508 void
509 Surface::turn_it_on ()
510 {
511         if (!_active) {
512                 _active = true;
513                 zero_controls ();
514                 for (Strips::iterator s = strips.begin(); s != strips.end(); ++s) {
515                         (*s)->notify_all ();
516                 }
517                 update_view_mode_display ();
518                 if (_mcp.device_info ().has_master_fader ()) {
519                         master_gain_changed ();
520                 }
521                 
522                 if (_mcp.device_info ().has_global_controls ()) {
523                         _mcp.update_global_button (Button::Read, _mcp.metering_active ());
524                         _mcp.update_global_button (Button::Stop, _mcp.get_session ().transport_stopped ());
525                         _mcp.update_global_button (Button::Play, (_mcp.get_session ().transport_speed () == 1.0f));
526                 }
527         }
528 }
529
530 void 
531 Surface::handle_port_inactive (SurfacePort*)
532 {
533         _active = false;
534 }
535
536 void 
537 Surface::write_sysex (const MidiByteArray & mba)
538 {
539         if (mba.empty()) {
540                 return;
541         }
542
543         MidiByteArray buf;
544         buf << sysex_hdr() << mba << MIDI::eox;
545         _port->write (buf);
546 }
547
548 void 
549 Surface::write_sysex (MIDI::byte msg)
550 {
551         MidiByteArray buf;
552         buf << sysex_hdr() << msg << MIDI::eox;
553         _port->write (buf);
554 }
555
556 uint32_t
557 Surface::n_strips (bool with_locked_strips) const
558 {
559         if (with_locked_strips) {
560                 return strips.size();
561         } 
562
563         uint32_t n = 0;
564
565         for (Strips::const_iterator it = strips.begin(); it != strips.end(); ++it) {
566                 if (!(*it)->locked()) {
567                         ++n;
568                 }
569         }
570         return n;
571 }
572
573 Strip*
574 Surface::nth_strip (uint32_t n) const
575 {
576         if (n > n_strips()) {
577                 return 0;
578         }
579         return strips[n];
580 }
581
582 void
583 Surface::zero_all ()
584 {
585         if (_mcp.device_info().has_timecode_display ()) {
586                 display_timecode (string (10, '0'), string (10, ' '));
587         }
588         
589         if (_mcp.device_info().has_two_character_display()) {
590                 show_two_char_display (string (2, '0'), string (2, ' '));
591         }
592
593         if (_mcp.device_info().has_master_fader ()) {
594                 _port->write (_master_fader->zero ());
595         }
596         
597         // zero all strips
598         for (Strips::iterator it = strips.begin(); it != strips.end(); ++it) {
599                 (*it)->zero();
600         }
601
602         zero_controls ();
603 }
604
605 void
606 Surface::zero_controls ()
607 {
608         if (_stype != mcu || !_mcp.device_info().has_global_controls()) {
609                 return;
610         }
611
612         // turn off global buttons and leds
613         // global buttons are only ever on mcu_port, so we don't have
614         // to figure out which port.
615
616         for (Controls::iterator it = controls.begin(); it != controls.end(); ++it) {
617                 Control & control = **it;
618                 if (!control.group().is_strip()) {
619                         _port->write (control.zero());
620                 }
621         }
622
623         // and the led ring for the master strip
624         blank_jog_ring ();
625 }
626
627 void
628 Surface::periodic (uint64_t now_usecs)
629 {
630         for (Strips::iterator s = strips.begin(); s != strips.end(); ++s) {
631                 (*s)->periodic (now_usecs);
632         }
633 }
634
635 void
636 Surface::write (const MidiByteArray& data) 
637 {
638         if (_active) {
639                 _port->write (data);
640         }
641 }
642
643 void
644 Surface::map_routes (const vector<boost::shared_ptr<Route> >& routes)
645 {
646         vector<boost::shared_ptr<Route> >::const_iterator r;
647         Strips::iterator s = strips.begin();
648
649         for (r = routes.begin(); r != routes.end() && s != strips.end(); ++s) {
650
651                 /* don't try to assign routes to a locked strip. it won't
652                    use it anyway, but if we do, then we get out of sync
653                    with the proposed mapping.
654                 */
655
656                 if (!(*s)->locked()) {
657                         (*s)->set_route (*r);
658                         ++r;
659                 }
660         }
661
662         for (; s != strips.end(); ++s) {
663                 (*s)->set_route (boost::shared_ptr<Route>());
664         }
665
666
667 }
668
669 static char 
670 translate_seven_segment (char achar)
671 {
672         achar = toupper (achar);
673
674         if  (achar >= 0x40 && achar <= 0x60) {
675                 return achar - 0x40;
676         } else if  (achar >= 0x21 && achar <= 0x3f) {
677                 return achar;
678         } else {
679                 return 0x00;
680         }
681 }
682
683 void
684 Surface::show_two_char_display (const std::string & msg, const std::string & dots)
685 {
686         if (_stype != mcu || !_mcp.device_info().has_two_character_display() || msg.length() != 2 || dots.length() != 2) {
687                 return;
688         }
689         
690         MidiByteArray right (3, 0xb0, 0x4b, 0x00);
691         MidiByteArray left (3, 0xb0, 0x4a, 0x00);
692         
693         right[2] = translate_seven_segment (msg[0]) +  (dots[0] == '.' ? 0x40 : 0x00);
694         left[2] = translate_seven_segment (msg[1]) +  (dots[1] == '.' ? 0x40 : 0x00);
695         
696         _port->write (right);
697         _port->write (left);
698 }
699
700 void
701 Surface::show_two_char_display (unsigned int value, const std::string & /*dots*/)
702 {
703         ostringstream os;
704         os << setfill('0') << setw(2) << value % 100;
705         show_two_char_display (os.str());
706 }
707
708 void
709 Surface::display_timecode (const std::string & timecode, const std::string & last_timecode)
710 {
711         if (!_active || !_mcp.device_info().has_timecode_display()) {
712                 return;
713         }
714         // if there's no change, send nothing, not even sysex header
715         if  (timecode == last_timecode) return;
716         
717         // length sanity checking
718         string local_timecode = timecode;
719
720         // truncate to 10 characters
721         if  (local_timecode.length() > 10) {
722                 local_timecode = local_timecode.substr (0, 10);
723         }
724
725         // pad to 10 characters
726         while  (local_timecode.length() < 10) { 
727                 local_timecode += " ";
728         }
729         
730         // translate characters.
731         // Only the characters that actually changed are sent.
732         int position = 0x3f;
733         int i;
734         for (i = local_timecode.length () - 1; i >= 0; i--) {
735                 position++;
736                 if (local_timecode[i] == last_timecode[i]) {
737                         continue;
738                 }
739                 MidiByteArray retval (2, 0xb0, position);
740                 retval << translate_seven_segment (local_timecode[i]);
741                 _port->write (retval);
742         }
743 }
744
745 void
746 Surface::update_flip_mode_display ()
747 {
748         for (Strips::iterator s = strips.begin(); s != strips.end(); ++s) {
749                 (*s)->flip_mode_changed (true);
750         }
751 }
752
753 void
754 Surface::update_view_mode_display ()
755 {
756         string text;
757         int id = -1;
758
759         if (!_active) {
760                 return;
761         }
762
763         switch (_mcp.view_mode()) {
764         case MackieControlProtocol::Mixer:
765                 show_two_char_display ("Mx");
766                 id = Button::Pan;
767                 break;
768         case MackieControlProtocol::Dynamics:
769                 show_two_char_display ("Dy");
770                 id = Button::Dyn;
771                 break;
772         case MackieControlProtocol::EQ:
773                 show_two_char_display ("EQ");
774                 id = Button::Eq;
775                 break;
776         case MackieControlProtocol::Loop:
777                 show_two_char_display ("LP");
778                 id = Button::Loop;
779                 break;
780         case MackieControlProtocol::AudioTracks:
781                 show_two_char_display ("AT");
782                 break;
783         case MackieControlProtocol::MidiTracks:
784                 show_two_char_display ("MT");
785                 break;
786         case MackieControlProtocol::Sends:
787                 show_two_char_display ("Sn");
788                 id = Button::Sends;
789                 break;
790         case MackieControlProtocol::Plugins:
791                 show_two_char_display ("Pl");
792                 id = Button::Plugin;
793                 break;
794         default:
795                 break;
796         }
797
798         if (id >= 0) {
799                 
800                 /* we are attempting to turn a global button/LED on */
801
802                 map<int,Control*>::iterator x = controls_by_device_independent_id.find (id);
803
804                 if (x != controls_by_device_independent_id.end()) {
805                         Button* button = dynamic_cast<Button*> (x->second);
806                         if (button) {
807                                 _port->write (button->set_state (on));
808                         }
809                 }
810         }
811
812         if (!text.empty()) {
813                 for (Strips::iterator s = strips.begin(); s != strips.end(); ++s) {
814                         _port->write ((*s)->display (1, text));
815                 }
816         }
817 }
818
819 void
820 Surface::gui_selection_changed (const ARDOUR::StrongRouteNotificationList& routes)
821 {
822         for (Strips::iterator s = strips.begin(); s != strips.end(); ++s) {
823                 (*s)->gui_selection_changed (routes);
824         }
825 }
826
827 void
828 Surface::say_hello ()
829 {
830         /* wakeup for Mackie Control */
831         MidiByteArray wakeup (7, MIDI::sysex, 0x00, 0x00, 0x66, 0x14, 0x00, MIDI::eox);
832         _port->write (wakeup);
833         wakeup[4] = 0x15; /* wakup Mackie XT */
834         _port->write (wakeup);
835         wakeup[4] = 0x10; /* wakupe Logic Control */
836         _port->write (wakeup);
837         wakeup[4] = 0x11; /* wakeup Logic Control XT */
838         _port->write (wakeup);
839 }
840
841 void
842 Surface::next_jog_mode ()
843 {
844 }
845
846 void
847 Surface::set_jog_mode (JogWheel::Mode)
848 {
849 }       
850
851 bool
852 Surface::route_is_locked_to_strip (boost::shared_ptr<Route> r) const
853 {
854         for (Strips::const_iterator s = strips.begin(); s != strips.end(); ++s) {
855                 if ((*s)->route() == r && (*s)->locked()) {
856                         return true;
857                 }
858         }
859         return false;
860 }
861
862 void 
863 Surface::notify_metering_state_changed()
864 {
865         for (Strips::const_iterator s = strips.begin(); s != strips.end(); ++s) {
866                 (*s)->notify_metering_state_changed ();
867         }
868 }