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