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