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