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