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