27ee8f9406617fe3e147a6c9f44f9a973f068fbf
[ardour.git] / libs / surfaces / faderport / faderport.cc
1 /*
2     Copyright (C) 2015 Paul Davis
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include <cstdlib>
21 #include <sstream>
22 #include <algorithm>
23
24 #include <stdint.h>
25
26 #include <glibmm/fileutils.h>
27 #include <glibmm/miscutils.h>
28
29 #include "pbd/controllable_descriptor.h"
30 #include "pbd/error.h"
31 #include "pbd/failed_constructor.h"
32 #include "pbd/file_utils.h"
33 #include "pbd/pthread_utils.h"
34 #include "pbd/compose.h"
35 #include "pbd/xml++.h"
36
37 #include "midi++/port.h"
38
39 #include "ardour/async_midi_port.h"
40 #include "ardour/audioengine.h"
41 #include "ardour/amp.h"
42 #include "ardour/bundle.h"
43 #include "ardour/debug.h"
44 #include "ardour/filesystem_paths.h"
45 #include "ardour/midi_port.h"
46 #include "ardour/midiport_manager.h"
47 #include "ardour/monitor_processor.h"
48 #include "ardour/profile.h"
49 #include "ardour/rc_configuration.h"
50 #include "ardour/route.h"
51 #include "ardour/session.h"
52 #include "ardour/session_configuration.h"
53 #include "ardour/track.h"
54
55 #include "faderport.h"
56
57 using namespace ARDOUR;
58 using namespace ArdourSurface;
59 using namespace PBD;
60 using namespace Glib;
61 using namespace std;
62
63 #include "i18n.h"
64
65 #include "pbd/abstract_ui.cc" // instantiate template
66
67 FaderPort::FaderPort (Session& s)
68         : ControlProtocol (s, _("Faderport"))
69         , AbstractUI<FaderPortRequest> (name())
70         , gui (0)
71         , connection_state (ConnectionState (0))
72         , _device_active (false)
73         , fader_msb (0)
74         , fader_lsb (0)
75         , fader_is_touched (false)
76         , button_state (ButtonState (0))
77         , blink_state (false)
78         , rec_enable_state (false)
79 {
80         last_encoder_time = 0;
81
82         boost::shared_ptr<ARDOUR::Port> inp;
83         boost::shared_ptr<ARDOUR::Port> outp;
84
85         inp  = AudioEngine::instance()->register_input_port (DataType::MIDI, "Faderport Recv", true);
86         outp = AudioEngine::instance()->register_output_port (DataType::MIDI, "Faderport Send", true);
87
88         _input_port = boost::dynamic_pointer_cast<AsyncMIDIPort>(inp);
89         _output_port = boost::dynamic_pointer_cast<AsyncMIDIPort>(outp);
90
91         if (_input_port == 0 || _output_port == 0) {
92                 throw failed_constructor();
93         }
94
95         _input_bundle.reset (new ARDOUR::Bundle (_("Faderport Support (Receive)"), true));
96         _output_bundle.reset (new ARDOUR::Bundle (_("Faderport Support (Send) "), false));
97
98         _input_bundle->add_channel (
99                 inp->name(),
100                 ARDOUR::DataType::MIDI,
101                 session->engine().make_port_name_non_relative (inp->name())
102                 );
103
104         _output_bundle->add_channel (
105                 outp->name(),
106                 ARDOUR::DataType::MIDI,
107                 session->engine().make_port_name_non_relative (outp->name())
108                 );
109
110
111         TrackSelectionChanged.connect (selection_connection, MISSING_INVALIDATOR, boost::bind (&FaderPort::gui_track_selection_changed, this, _1), this);
112
113         /* Catch port connections and disconnections */
114         ARDOUR::AudioEngine::instance()->PortConnectedOrDisconnected.connect (port_connection, MISSING_INVALIDATOR, boost::bind (&FaderPort::connection_handler, this, _1, _2, _3, _4, _5), this);
115
116         buttons.insert (std::make_pair (Mute, Button (*this, _("Mute"), Mute, 21)));
117         buttons.insert (std::make_pair (Solo, Button (*this, _("Solo"), Solo, 22)));
118         buttons.insert (std::make_pair (Rec, Button (*this, _("Rec"), Rec, 23)));
119         buttons.insert (std::make_pair (Left, Button (*this, _("Left"), Left, 20)));
120         buttons.insert (std::make_pair (Bank, Button (*this, _("Bank"), Bank, 19)));
121         buttons.insert (std::make_pair (Right, Button (*this, _("Right"), Right, 18)));
122         buttons.insert (std::make_pair (Output, Button (*this, _("Output"), Output, 17)));
123         buttons.insert (std::make_pair (FP_Read, Button (*this, _("Read"), FP_Read, 13)));
124         buttons.insert (std::make_pair (FP_Write, Button (*this, _("Write"), FP_Write, 14)));
125         buttons.insert (std::make_pair (FP_Touch, Button (*this, _("Touch"), FP_Touch, 15)));
126         buttons.insert (std::make_pair (FP_Off, Button (*this, _("Off"), FP_Off, 16)));
127         buttons.insert (std::make_pair (Mix, Button (*this, _("Mix"), Mix, 12)));
128         buttons.insert (std::make_pair (Proj, Button (*this, _("Proj"), Proj, 11)));
129         buttons.insert (std::make_pair (Trns, Button (*this, _("Trns"), Trns, 10)));
130         buttons.insert (std::make_pair (Undo, Button (*this, _("Undo"), Undo, 9)));
131         buttons.insert (std::make_pair (Shift, Button (*this, _("Shift"), Shift, 5)));
132         buttons.insert (std::make_pair (Punch, Button (*this, _("Punch"), Punch, 6)));
133         buttons.insert (std::make_pair (User, Button (*this, _("User"), User, 7)));
134         buttons.insert (std::make_pair (Loop, Button (*this, _("Loop"), Loop, 8)));
135         buttons.insert (std::make_pair (Rewind, Button (*this, _("Rewind"), Rewind, 4)));
136         buttons.insert (std::make_pair (Ffwd, Button (*this, _("Ffwd"), Ffwd, 3)));
137         buttons.insert (std::make_pair (Stop, Button (*this, _("Stop"), Stop, 2)));
138         buttons.insert (std::make_pair (Play, Button (*this, _("Play"), Play, 1)));
139         buttons.insert (std::make_pair (RecEnable, Button (*this, _("RecEnable"), RecEnable, 0)));
140         buttons.insert (std::make_pair (Footswitch, Button (*this, _("Footswitch"), Footswitch, -1)));
141         buttons.insert (std::make_pair (FaderTouch, Button (*this, _("Fader (touch)"), FaderTouch, -1)));
142
143         get_button (Shift).set_flash (true);
144         get_button (Mix).set_flash (true);
145         get_button (Proj).set_flash (true);
146         get_button (Trns).set_flash (true);
147         get_button (User).set_flash (true);
148
149         get_button (Left).set_action ( boost::bind (&FaderPort::left, this), true);
150         get_button (Right).set_action ( boost::bind (&FaderPort::right, this), true);
151
152         get_button (Undo).set_action (boost::bind (&FaderPort::undo, this), true);
153         get_button (Undo).set_action (boost::bind (&FaderPort::redo, this), true, ShiftDown);
154         get_button (Undo).set_flash (true);
155
156         get_button (FP_Read).set_action (boost::bind (&FaderPort::read, this), true);
157         get_button (FP_Read).set_action (boost::bind (&FaderPort::off, this), false, LongPress);
158         get_button (FP_Write).set_action (boost::bind (&FaderPort::write, this), true);
159         get_button (FP_Write).set_action (boost::bind (&FaderPort::off, this), false, LongPress);
160         get_button (FP_Touch).set_action (boost::bind (&FaderPort::touch, this), true);
161         get_button (FP_Touch).set_action (boost::bind (&FaderPort::off, this), false, LongPress);
162         get_button (FP_Off).set_action (boost::bind (&FaderPort::off, this), true);
163
164         get_button (Play).set_action (boost::bind (&BasicUI::transport_play, this, true), true);
165         get_button (RecEnable).set_action (boost::bind (&BasicUI::rec_enable_toggle, this), true);
166         /* Stop is a modifier, so we have to use its own button state to get
167            the default action (since StopDown will be set when looking for the
168            action to invoke.
169         */
170         get_button (Stop).set_action (boost::bind (&BasicUI::transport_stop, this), true, StopDown);
171         get_button (Ffwd).set_action (boost::bind (&BasicUI::ffwd, this), true);
172
173         /* See comments about Stop above .. */
174         get_button (Rewind).set_action (boost::bind (&BasicUI::rewind, this), true, RewindDown);
175         get_button (Rewind).set_action (boost::bind (&BasicUI::goto_zero, this), true, ButtonState (RewindDown|StopDown));
176         get_button (Rewind).set_action (boost::bind (&BasicUI::goto_start, this), true, ButtonState (RewindDown|ShiftDown));
177
178         get_button (Ffwd).set_action (boost::bind (&BasicUI::ffwd, this), true);
179         get_button (Ffwd).set_action (boost::bind (&BasicUI::goto_end, this), true, ShiftDown);
180
181         get_button (Punch).set_action (boost::bind (&FaderPort::punch, this), true);
182
183         get_button (Loop).set_action (boost::bind (&BasicUI::loop_toggle, this), true);
184         get_button (Loop).set_action (boost::bind (&BasicUI::add_marker, this, string()), true, ShiftDown);
185
186         get_button (Punch).set_action (boost::bind (&BasicUI::prev_marker, this), true, ShiftDown);
187         get_button (User).set_action (boost::bind (&BasicUI::next_marker, this), true, ButtonState(ShiftDown|UserDown));
188
189         get_button (Mute).set_action (boost::bind (&FaderPort::mute, this), true);
190         get_button (Solo).set_action (boost::bind (&FaderPort::solo, this), true);
191         get_button (Rec).set_action (boost::bind (&FaderPort::rec_enable, this), true);
192
193         get_button (Output).set_action (boost::bind (&FaderPort::use_master, this), true);
194         get_button (Output).set_action (boost::bind (&FaderPort::use_monitor, this), true, ShiftDown);
195 }
196
197 FaderPort::~FaderPort ()
198 {
199         all_lights_out ();
200
201         if (_input_port) {
202                 DEBUG_TRACE (DEBUG::FaderPort, string_compose ("unregistering input port %1\n", boost::shared_ptr<ARDOUR::Port>(_input_port)->name()));
203                 AudioEngine::instance()->unregister_port (_input_port);
204                 _input_port.reset ();
205         }
206
207         if (_output_port) {
208                 _output_port->drain (10000,  250000); /* check every 10 msecs, wait up to 1/4 second for the port to drain */
209                 DEBUG_TRACE (DEBUG::FaderPort, string_compose ("unregistering output port %1\n", boost::shared_ptr<ARDOUR::Port>(_output_port)->name()));
210                 AudioEngine::instance()->unregister_port (_output_port);
211                 _output_port.reset ();
212         }
213
214         tear_down_gui ();
215
216         /* stop event loop */
217         DEBUG_TRACE (DEBUG::FaderPort, "BaseUI::quit ()\n");
218         BaseUI::quit ();
219 }
220
221 void*
222 FaderPort::request_factory (uint32_t num_requests)
223 {
224         /* AbstractUI<T>::request_buffer_factory() is a template method only
225            instantiated in this source module. To provide something visible for
226            use in the interface/descriptor, we have this static method that is
227            template-free.
228         */
229         return request_buffer_factory (num_requests);
230 }
231
232 void
233 FaderPort::start_midi_handling ()
234 {
235         /* handle device inquiry response */
236         _input_port->parser()->sysex.connect_same_thread (midi_connections, boost::bind (&FaderPort::sysex_handler, this, _1, _2, _3));
237         /* handle buttons */
238         _input_port->parser()->poly_pressure.connect_same_thread (midi_connections, boost::bind (&FaderPort::button_handler, this, _1, _2));
239         /* handle encoder */
240         _input_port->parser()->pitchbend.connect_same_thread (midi_connections, boost::bind (&FaderPort::encoder_handler, this, _1, _2));
241         /* handle fader */
242         _input_port->parser()->controller.connect_same_thread (midi_connections, boost::bind (&FaderPort::fader_handler, this, _1, _2));
243
244         /* This connection means that whenever data is ready from the input
245          * port, the relevant thread will invoke our ::midi_input_handler()
246          * method, which will read the data, and invoke the parser.
247          */
248
249         _input_port->xthread().set_receive_handler (sigc::bind (sigc::mem_fun (this, &FaderPort::midi_input_handler), _input_port));
250         _input_port->xthread().attach (main_loop()->get_context());
251 }
252
253 void
254 FaderPort::stop_midi_handling ()
255 {
256         midi_connections.drop_connections ();
257
258         /* Note: the input handler is still active at this point, but we're no
259          * longer connected to any of the parser signals
260          */
261 }
262
263 void
264 FaderPort::do_request (FaderPortRequest* req)
265 {
266         if (req->type == CallSlot) {
267
268                 call_slot (MISSING_INVALIDATOR, req->the_slot);
269
270         } else if (req->type == Quit) {
271
272                 stop ();
273         }
274 }
275
276 int
277 FaderPort::stop ()
278 {
279         BaseUI::quit ();
280
281         return 0;
282 }
283
284 void
285 FaderPort::thread_init ()
286 {
287         struct sched_param rtparam;
288
289         pthread_set_name (event_loop_name().c_str());
290
291         PBD::notify_event_loops_about_thread_creation (pthread_self(), event_loop_name(), 2048);
292         ARDOUR::SessionEvent::create_per_thread_pool (event_loop_name(), 128);
293
294         memset (&rtparam, 0, sizeof (rtparam));
295         rtparam.sched_priority = 9; /* XXX should be relative to audio (JACK) thread */
296
297         if (pthread_setschedparam (pthread_self(), SCHED_FIFO, &rtparam) != 0) {
298                 // do we care? not particularly.
299         }
300 }
301
302 void
303 FaderPort::all_lights_out ()
304 {
305         for (ButtonMap::iterator b = buttons.begin(); b != buttons.end(); ++b) {
306                 b->second.set_led_state (_output_port, false);
307         }
308 }
309
310 FaderPort::Button&
311 FaderPort::get_button (ButtonID id) const
312 {
313         ButtonMap::const_iterator b = buttons.find (id);
314         assert (b != buttons.end());
315         return const_cast<Button&>(b->second);
316 }
317
318 bool
319 FaderPort::button_long_press_timeout (ButtonID id)
320 {
321         if (buttons_down.find (id) != buttons_down.end()) {
322                 get_button (id).invoke (ButtonState (LongPress|button_state), false);
323         } else {
324                 /* release happened and somehow we were not cancelled */
325         }
326
327         /* whichever button this was, we've used it ... don't invoke the
328            release action.
329         */
330         consumed.insert (id);
331
332         return false; /* don't get called again */
333 }
334
335 void
336 FaderPort::start_press_timeout (Button& button, ButtonID id)
337 {
338         Glib::RefPtr<Glib::TimeoutSource> timeout = Glib::TimeoutSource::create (500); // milliseconds
339         button.timeout_connection = timeout->connect (sigc::bind (sigc::mem_fun (*this, &FaderPort::button_long_press_timeout), id));
340         timeout->attach (main_loop()->get_context());
341 }
342
343 void
344 FaderPort::button_handler (MIDI::Parser &, MIDI::EventTwoBytes* tb)
345 {
346         ButtonID id (ButtonID (tb->controller_number));
347         Button& button (get_button (id));
348
349         DEBUG_TRACE (DEBUG::FaderPort, string_compose ("button event for ID %1 press ? %2\n", (int) tb->controller_number, (tb->value ? "yes" : "no")));
350
351         if (tb->value) {
352                 buttons_down.insert (id);
353         } else {
354                 buttons_down.erase (id);
355                 button.timeout_connection.disconnect ();
356         }
357
358         ButtonState bs (ButtonState (0));
359
360         switch (id) {
361         case Shift:
362                 bs = ShiftDown;
363                 break;
364         case Stop:
365                 bs = StopDown;
366                 break;
367         case Rewind:
368                 bs = RewindDown;
369                 break;
370         case User:
371                 bs = UserDown;
372                 break;
373         case FaderTouch:
374                 fader_is_touched = tb->value;
375                 if (_current_route) {
376                         boost::shared_ptr<AutomationControl> gain = _current_route->gain_control ();
377                         if (gain) {
378                                 framepos_t now = session->engine().sample_time();
379                                 if (tb->value) {
380                                         gain->start_touch (now);
381                                 } else {
382                                         gain->stop_touch (true, now);
383                                 }
384                         }
385                 }
386                 break;
387         default:
388                 if (tb->value) {
389                         start_press_timeout (button, id);
390                 }
391                 break;
392         }
393
394         if (bs) {
395                 button_state = (tb->value ? ButtonState (button_state|bs) : ButtonState (button_state&~bs));
396                 DEBUG_TRACE (DEBUG::FaderPort, string_compose ("reset button state to %1 using %2\n", button_state, (int) bs));
397         }
398
399         if (button.uses_flash()) {
400                 button.set_led_state (_output_port, (int)tb->value);
401         }
402
403         set<ButtonID>::iterator c = consumed.find (id);
404
405         if (c == consumed.end()) {
406                 button.invoke (button_state, tb->value ? true : false);
407         } else {
408                 DEBUG_TRACE (DEBUG::FaderPort, "button was consumed, ignored\n");
409                 consumed.erase (c);
410         }
411 }
412
413 void
414 FaderPort::encoder_handler (MIDI::Parser &, MIDI::pitchbend_t pb)
415 {
416         int delta = 1;
417
418         if (pb >= 8192) {
419                 delta = -1;
420         }
421
422         //knob debouncing and hysteresis.  The presonus encoder often sends bursts of events, or goes the wrong direction
423         {
424                 last_last_encoder_delta = last_encoder_delta;
425                 last_encoder_delta = delta;
426                 microseconds_t now = get_microseconds ();
427                 if ((now - last_encoder_time) < 10*1000) { //require at least 10ms interval between changes, because the device sometimes sends multiple deltas
428                         return;
429                 }
430                 if ((now - last_encoder_time) < 100*1000) { //avoid directional changes while "spinning", 100ms window
431                         if ( (delta == last_encoder_delta) && (delta == last_last_encoder_delta) ) {
432                                 last_good_encoder_delta = delta;  //3 in a row, grudgingly accept this as the new direction
433                         }
434                         if (delta != last_good_encoder_delta) {  //otherwise ensure we keep going the same way
435                                 delta = last_good_encoder_delta;
436                         }
437                 } else {  //we aren't yet in a spin window, just assume this move is really what we want
438                         //NOTE:  if you are worried about where these get initialized, here it is.
439                         last_last_encoder_delta = delta;
440                         last_encoder_delta = delta;
441                 }
442                 last_encoder_time = now;
443                 last_good_encoder_delta = delta;
444         }
445
446         if (_current_route) {
447
448                 ButtonState trim_modifier;
449                 ButtonState width_modifier;
450
451                 if (Profile->get_mixbus()) {
452                         trim_modifier = ShiftDown;
453                         width_modifier = ButtonState (0);
454                 } else {
455                         trim_modifier = UserDown;
456                         width_modifier = ShiftDown;
457                 }
458
459                 if ((button_state & trim_modifier) == trim_modifier ) {    // mod+encoder = input trim
460                         boost::shared_ptr<AutomationControl> trim = _current_route->trim()->gain_control ();
461                         if (trim) {
462                                 float val = trim->get_user();  //for gain elements, the "user" value is in dB
463                                 val += delta;
464                                 trim->set_user(val);
465                         }
466                 } else if (width_modifier && ((button_state & width_modifier) == width_modifier)) {
467                         ardour_pan_width (delta);
468
469                 } else {  // pan/balance
470                         if (!Profile->get_mixbus()) {
471                                 ardour_pan_azimuth (delta);
472                         } else {
473                                 mixbus_pan (delta);
474                         }
475                 }
476         }
477
478         /* if the user button was pressed, mark it as consumed so that its
479          * release action has no effect.
480          */
481
482         if (!Profile->get_mixbus() && (button_state & UserDown)) {
483                 consumed.insert (User);
484         }
485 }
486
487 void
488 FaderPort::fader_handler (MIDI::Parser &, MIDI::EventTwoBytes* tb)
489 {
490         bool was_fader = false;
491
492         if (tb->controller_number == 0x0) {
493                 fader_msb = tb->value;
494                 was_fader = true;
495         } else if (tb->controller_number == 0x20) {
496                 fader_lsb = tb->value;
497                 was_fader = true;
498         }
499
500         if (was_fader) {
501                 if (_current_route) {
502                         boost::shared_ptr<AutomationControl> gain = _current_route->gain_control ();
503                         if (gain) {
504                                 int ival = (fader_msb << 7) | fader_lsb;
505                                 float val = gain->interface_to_internal (ival/16384.0);
506                                 /* even though the faderport only controls a
507                                    single route at a time, allow the fader to
508                                    modify the group, if appropriate.
509                                 */
510                                 _current_route->set_gain (val, Controllable::UseGroup);
511                         }
512                 }
513         }
514 }
515
516 void
517 FaderPort::sysex_handler (MIDI::Parser &p, MIDI::byte *buf, size_t sz)
518 {
519         DEBUG_TRACE (DEBUG::FaderPort, string_compose ("sysex message received, size = %1\n", sz));
520
521         if (sz < 17) {
522                 return;
523         }
524
525         if (buf[2] != 0x7f ||
526             buf[3] != 0x06 ||
527             buf[4] != 0x02 ||
528             buf[5] != 0x0 ||
529             buf[6] != 0x1 ||
530             buf[7] != 0x06 ||
531             buf[8] != 0x02 ||
532             buf[9] != 0x0 ||
533             buf[10] != 0x01 ||
534             buf[11] != 0x0) {
535                 return;
536         }
537
538         _device_active = true;
539
540         DEBUG_TRACE (DEBUG::FaderPort, "FaderPort identified via MIDI Device Inquiry response\n");
541
542         /* put it into native mode */
543
544         MIDI::byte native[3];
545         native[0] = 0x91;
546         native[1] = 0x00;
547         native[2] = 0x64;
548
549         _output_port->write (native, 3, 0);
550
551         all_lights_out ();
552
553         /* catch up on state */
554
555         /* make sure that rec_enable_state is consistent with current device state */
556         get_button (RecEnable).set_led_state (_output_port, rec_enable_state);
557
558         map_transport_state ();
559         map_recenable_state ();
560 }
561
562 int
563 FaderPort::set_active (bool yn)
564 {
565         DEBUG_TRACE (DEBUG::FaderPort, string_compose("Faderport::set_active init with yn: '%1'\n", yn));
566
567         if (yn == active()) {
568                 return 0;
569         }
570
571         if (yn) {
572
573                 /* start event loop */
574
575                 BaseUI::run ();
576
577                 connect_session_signals ();
578
579                 Glib::RefPtr<Glib::TimeoutSource> blink_timeout = Glib::TimeoutSource::create (200); // milliseconds
580                 blink_connection = blink_timeout->connect (sigc::mem_fun (*this, &FaderPort::blink));
581                 blink_timeout->attach (main_loop()->get_context());
582
583                 Glib::RefPtr<Glib::TimeoutSource> periodic_timeout = Glib::TimeoutSource::create (100); // milliseconds
584                 periodic_connection = periodic_timeout->connect (sigc::mem_fun (*this, &FaderPort::periodic));
585                 periodic_timeout->attach (main_loop()->get_context());
586
587         } else {
588
589                 BaseUI::quit ();
590                 close ();
591
592         }
593
594         ControlProtocol::set_active (yn);
595
596         DEBUG_TRACE (DEBUG::FaderPort, string_compose("Faderport::set_active done with yn: '%1'\n", yn));
597
598         return 0;
599 }
600
601 bool
602 FaderPort::periodic ()
603 {
604         if (!_current_route) {
605                 return true;
606         }
607
608         ARDOUR::AutoState gain_state = _current_route->gain_control()->automation_state();
609
610         if (gain_state == ARDOUR::Touch || gain_state == ARDOUR::Play) {
611                 map_gain ();
612         }
613
614         return true;
615 }
616
617 void
618 FaderPort::stop_blinking (ButtonID id)
619 {
620         blinkers.remove (id);
621         get_button (id).set_led_state (_output_port, false);
622 }
623
624 void
625 FaderPort::start_blinking (ButtonID id)
626 {
627         blinkers.push_back (id);
628         get_button (id).set_led_state (_output_port, true);
629 }
630
631 bool
632 FaderPort::blink ()
633 {
634         blink_state = !blink_state;
635
636         for (Blinkers::iterator b = blinkers.begin(); b != blinkers.end(); b++) {
637                 get_button(*b).set_led_state (_output_port, blink_state);
638         }
639
640         map_recenable_state ();
641
642         return true;
643 }
644
645 void
646 FaderPort::close ()
647 {
648         all_lights_out ();
649
650         stop_midi_handling ();
651         session_connections.drop_connections ();
652         port_connection.disconnect ();
653         blink_connection.disconnect ();
654         selection_connection.disconnect ();
655         route_connections.drop_connections ();
656
657 #if 0
658         route_connections.drop_connections ();
659 #endif
660 }
661
662 void
663 FaderPort::map_recenable_state ()
664 {
665         /* special case for RecEnable because its status can change as a
666          * confluence of unrelated parameters: (a) session rec-enable state (b)
667          * rec-enabled tracks. So we don't add the button to the blinkers list,
668          * we just call this:
669          *
670          *  * from the blink callback
671          *  * when the session tells us about a status change
672          *
673          * We do the last one so that the button changes state promptly rather
674          * than waiting for the next blink callback. The change in "blinking"
675          * based on having record-enabled tracks isn't urgent, and that happens
676          * during the blink callback.
677          */
678
679         bool onoff;
680
681         switch (session->record_status()) {
682         case Session::Disabled:
683                 onoff = false;
684                 break;
685         case Session::Enabled:
686                 onoff = blink_state;
687                 break;
688         case Session::Recording:
689                 if (session->have_rec_enabled_track ()) {
690                         onoff = true;
691                 } else {
692                         onoff = blink_state;
693                 }
694                 break;
695         }
696
697         if (onoff != rec_enable_state) {
698                 get_button(RecEnable).set_led_state (_output_port, onoff);
699                 rec_enable_state = onoff;
700         }
701 }
702
703 void
704 FaderPort::map_transport_state ()
705 {
706         get_button (Loop).set_led_state (_output_port, session->get_play_loop());
707
708         float ts = session->transport_speed();
709
710         if (ts == 0) {
711                 stop_blinking (Play);
712         } else if (fabs (ts) == 1.0) {
713                 stop_blinking (Play);
714                 get_button (Play).set_led_state (_output_port, true);
715         } else {
716                 start_blinking (Play);
717         }
718
719         get_button (Stop).set_led_state (_output_port, session->transport_stopped ());
720         get_button (Rewind).set_led_state (_output_port, session->transport_speed() < 0.0);
721         get_button (Ffwd).set_led_state (_output_port, session->transport_speed() > 1.0);
722 }
723
724 void
725 FaderPort::parameter_changed (string what)
726 {
727         if (what == "punch-in" || what == "punch-out") {
728                 bool in = session->config.get_punch_in ();
729                 bool out = session->config.get_punch_out ();
730                 if (in && out) {
731                         get_button (Punch).set_led_state (_output_port, true);
732                         blinkers.remove (Punch);
733                 } else if (in || out) {
734                         start_blinking (Punch);
735                 } else {
736                         stop_blinking (Punch);
737                 }
738         }
739 }
740
741 void
742 FaderPort::connect_session_signals()
743 {
744         session->RecordStateChanged.connect(session_connections, MISSING_INVALIDATOR, boost::bind (&FaderPort::map_recenable_state, this), this);
745         session->TransportStateChange.connect(session_connections, MISSING_INVALIDATOR, boost::bind (&FaderPort::map_transport_state, this), this);
746         /* not session, but treat it similarly */
747         session->config.ParameterChanged.connect (session_connections, MISSING_INVALIDATOR, boost::bind (&FaderPort::parameter_changed, this, _1), this);
748 }
749
750 bool
751 FaderPort::midi_input_handler (Glib::IOCondition ioc, boost::shared_ptr<ARDOUR::AsyncMIDIPort> port)
752 {
753         DEBUG_TRACE (DEBUG::FaderPort, string_compose ("something happend on  %1\n", boost::shared_ptr<MIDI::Port>(port)->name()));
754
755         if (ioc & ~IO_IN) {
756                 return false;
757         }
758
759         if (ioc & IO_IN) {
760
761                 port->clear ();
762                 DEBUG_TRACE (DEBUG::FaderPort, string_compose ("data available on %1\n", boost::shared_ptr<MIDI::Port>(port)->name()));
763                 framepos_t now = session->engine().sample_time();
764                 port->parse (now);
765         }
766
767         return true;
768 }
769
770
771 XMLNode&
772 FaderPort::get_state ()
773 {
774         XMLNode& node (ControlProtocol::get_state());
775
776         XMLNode* child;
777
778         child = new XMLNode (X_("Input"));
779         child->add_child_nocopy (boost::shared_ptr<ARDOUR::Port>(_input_port)->get_state());
780         node.add_child_nocopy (*child);
781
782
783         child = new XMLNode (X_("Output"));
784         child->add_child_nocopy (boost::shared_ptr<ARDOUR::Port>(_output_port)->get_state());
785         node.add_child_nocopy (*child);
786
787         /* Save action state for Mix, Proj, Trns and User buttons, since these
788          * are user controlled. We can only save named-action operations, since
789          * internal functions are just pointers to functions and hard to
790          * serialize without enumerating them all somewhere.
791          */
792
793         node.add_child_nocopy (get_button (Mix).get_state());
794         node.add_child_nocopy (get_button (Proj).get_state());
795         node.add_child_nocopy (get_button (Trns).get_state());
796         node.add_child_nocopy (get_button (User).get_state());
797
798         return node;
799 }
800
801 int
802 FaderPort::set_state (const XMLNode& node, int version)
803 {
804         XMLNodeList nlist;
805         XMLNodeConstIterator niter;
806         XMLNode const* child;
807
808         if (ControlProtocol::set_state (node, version)) {
809                 return -1;
810         }
811
812         if ((child = node.child (X_("Input"))) != 0) {
813                 XMLNode* portnode = child->child (Port::state_node_name.c_str());
814                 if (portnode) {
815                         boost::shared_ptr<ARDOUR::Port>(_input_port)->set_state (*portnode, version);
816                 }
817         }
818
819         if ((child = node.child (X_("Output"))) != 0) {
820                 XMLNode* portnode = child->child (Port::state_node_name.c_str());
821                 if (portnode) {
822                         boost::shared_ptr<ARDOUR::Port>(_output_port)->set_state (*portnode, version);
823                 }
824         }
825
826         for (XMLNodeList::const_iterator n = node.children().begin(); n != node.children().end(); ++n) {
827                 if ((*n)->name() == X_("Button")) {
828                         XMLProperty const * prop = (*n)->property (X_("id"));
829                         if (!prop) {
830                                 continue;
831                         }
832                         int xid = atoi (prop->value());
833                         ButtonMap::iterator b = buttons.find (ButtonID (xid));
834                         if (b == buttons.end()) {
835                                 continue;
836                         }
837                         b->second.set_state (**n);
838                 }
839         }
840
841         return 0;
842 }
843
844 bool
845 FaderPort::connection_handler (boost::weak_ptr<ARDOUR::Port>, std::string name1, boost::weak_ptr<ARDOUR::Port>, std::string name2, bool yn)
846 {
847         DEBUG_TRACE (DEBUG::FaderPort, "FaderPort::connection_handler  start\n");
848         if (!_input_port || !_output_port) {
849                 return false;
850         }
851
852         string ni = ARDOUR::AudioEngine::instance()->make_port_name_non_relative (boost::shared_ptr<ARDOUR::Port>(_input_port)->name());
853         string no = ARDOUR::AudioEngine::instance()->make_port_name_non_relative (boost::shared_ptr<ARDOUR::Port>(_output_port)->name());
854
855         if (ni == name1 || ni == name2) {
856                 if (yn) {
857                         connection_state |= InputConnected;
858                 } else {
859                         connection_state &= ~InputConnected;
860                 }
861         } else if (no == name1 || no == name2) {
862                 if (yn) {
863                         connection_state |= OutputConnected;
864                 } else {
865                         connection_state &= ~OutputConnected;
866                 }
867         } else {
868                 DEBUG_TRACE (DEBUG::FaderPort, string_compose ("Connections between %1 and %2 changed, but I ignored it\n", name1, name2));
869                 /* not our ports */
870                 return false;
871         }
872
873         if ((connection_state & (InputConnected|OutputConnected)) == (InputConnected|OutputConnected)) {
874
875                 /* XXX this is a horrible hack. Without a short sleep here,
876                    something prevents the device wakeup messages from being
877                    sent and/or the responses from being received.
878                 */
879
880                 g_usleep (100000);
881                 DEBUG_TRACE (DEBUG::FaderPort, "device now connected for both input and output\n");
882                 connected ();
883
884         } else {
885                 DEBUG_TRACE (DEBUG::FaderPort, "Device disconnected (input or output or both) or not yet fully connected\n");
886                 _device_active = false;
887         }
888
889         ConnectionChange (); /* emit signal for our GUI */
890
891         DEBUG_TRACE (DEBUG::FaderPort, "FaderPort::connection_handler  end\n");
892
893         return true; /* connection status changed */
894 }
895
896 void
897 FaderPort::connected ()
898 {
899         DEBUG_TRACE (DEBUG::FaderPort, "sending device inquiry message...\n");
900
901         start_midi_handling ();
902
903         /* send device inquiry */
904
905         MIDI::byte buf[6];
906
907         buf[0] = 0xf0;
908         buf[1] = 0x7e;
909         buf[2] = 0x7f;
910         buf[3] = 0x06;
911         buf[4] = 0x01;
912         buf[5] = 0xf7;
913
914         _output_port->write (buf, 6, 0);
915 }
916
917 void
918 FaderPort::Button::invoke (FaderPort::ButtonState bs, bool press)
919 {
920         DEBUG_TRACE (DEBUG::FaderPort, string_compose ("invoke button %1 for %2 state %3%4%5\n", id, (press ? "press":"release"), hex, bs, dec));
921
922         ToDoMap::iterator x;
923
924         if (press) {
925                 if ((x = on_press.find (bs)) == on_press.end()) {
926                         DEBUG_TRACE (DEBUG::FaderPort, string_compose ("no press action for button %1 state %2 @ %3 in %4\n", id, bs, this, &on_press));
927                         return;
928                 }
929         } else {
930                 if ((x = on_release.find (bs)) == on_release.end()) {
931                         DEBUG_TRACE (DEBUG::FaderPort, string_compose ("no release action for button %1 state %2 @%3 in %4\n", id, bs, this, &on_release));
932                         return;
933                 }
934         }
935
936         switch (x->second.type) {
937         case NamedAction:
938                 if (!x->second.action_name.empty()) {
939                         fp.access_action (x->second.action_name);
940                 }
941                 break;
942         case InternalFunction:
943                 if (x->second.function) {
944                         x->second.function ();
945                 }
946         }
947 }
948
949 void
950 FaderPort::Button::set_action (string const& name, bool when_pressed, FaderPort::ButtonState bs)
951 {
952         ToDo todo;
953
954         todo.type = NamedAction;
955
956         if (when_pressed) {
957                 if (name.empty()) {
958                         on_press.erase (bs);
959                 } else {
960                         DEBUG_TRACE (DEBUG::FaderPort, string_compose ("set button %1 to action %2 on press + %3%4%5\n", id, name, bs));
961                         todo.action_name = name;
962                         on_press[bs] = todo;
963                 }
964         } else {
965                 if (name.empty()) {
966                         on_release.erase (bs);
967                 } else {
968                         DEBUG_TRACE (DEBUG::FaderPort, string_compose ("set button %1 to action %2 on release + %3%4%5\n", id, name, bs));
969                         todo.action_name = name;
970                         on_release[bs] = todo;
971                 }
972         }
973 }
974
975 string
976 FaderPort::Button::get_action (bool press, FaderPort::ButtonState bs)
977 {
978         ToDoMap::iterator x;
979
980         if (press) {
981                 if ((x = on_press.find (bs)) == on_press.end()) {
982                         return string();
983                 }
984                 if (x->second.type != NamedAction) {
985                         return string ();
986                 }
987                 return x->second.action_name;
988         } else {
989                 if ((x = on_release.find (bs)) == on_release.end()) {
990                         return string();
991                 }
992                 if (x->second.type != NamedAction) {
993                         return string ();
994                 }
995                 return x->second.action_name;
996         }
997 }
998
999 void
1000 FaderPort::Button::set_action (boost::function<void()> f, bool when_pressed, FaderPort::ButtonState bs)
1001 {
1002         ToDo todo;
1003         todo.type = InternalFunction;
1004
1005         if (when_pressed) {
1006                 DEBUG_TRACE (DEBUG::FaderPort, string_compose ("set button %1 (%2) @ %5 to some functor on press + %3 in %4\n", id, name, bs, &on_press, this));
1007                 todo.function = f;
1008                 on_press[bs] = todo;
1009         } else {
1010                 DEBUG_TRACE (DEBUG::FaderPort, string_compose ("set button %1 (%2) @ %5 to some functor on release + %3\n", id, name, bs, this));
1011                 todo.function = f;
1012                 on_release[bs] = todo;
1013         }
1014 }
1015
1016 void
1017 FaderPort::Button::set_led_state (boost::shared_ptr<MIDI::Port> port, bool onoff)
1018 {
1019         if (out < 0) {
1020                 /* fader button ID - no LED */
1021                 return;
1022         }
1023
1024         MIDI::byte buf[3];
1025         buf[0] = 0xa0;
1026         buf[1] = out;
1027         buf[2] = onoff ? 1 : 0;
1028         port->write (buf, 3, 0);
1029 }
1030
1031 int
1032 FaderPort::Button::set_state (XMLNode const& node)
1033 {
1034         const XMLProperty* prop = node.property ("id");
1035         if (!prop) {
1036                 return -1;
1037         }
1038
1039         int xid = atoi (prop->value());
1040         if (xid != id) {
1041                 return -1;
1042         }
1043
1044         typedef pair<string,FaderPort::ButtonState> state_pair_t;
1045         vector<state_pair_t> state_pairs;
1046
1047         state_pairs.push_back (make_pair (string ("plain"), ButtonState (0)));
1048         state_pairs.push_back (make_pair (string ("shift"), ShiftDown));
1049         state_pairs.push_back (make_pair (string ("long"), LongPress));
1050
1051         for (vector<state_pair_t>::const_iterator sp = state_pairs.begin(); sp != state_pairs.end(); ++sp) {
1052                 string propname;
1053
1054                 propname = sp->first + X_("-press");
1055                 if ((prop = node.property (propname)) != 0) {
1056                         set_action (prop->value(), true, sp->second);
1057                 }
1058
1059                 propname = sp->first + X_("-release");
1060                 if ((prop = node.property (propname)) != 0) {
1061                         set_action (prop->value(), false, sp->second);
1062                 }
1063         }
1064
1065         return 0;
1066 }
1067
1068 XMLNode&
1069 FaderPort::Button::get_state () const
1070 {
1071         XMLNode* node = new XMLNode (X_("Button"));
1072         char buf[16];
1073         snprintf (buf, sizeof (buf), "%d", id);
1074
1075         node->add_property (X_("id"), buf);
1076
1077         ToDoMap::const_iterator x;
1078         ToDo null;
1079         null.type = NamedAction;
1080
1081         typedef pair<string,FaderPort::ButtonState> state_pair_t;
1082         vector<state_pair_t> state_pairs;
1083
1084         state_pairs.push_back (make_pair (string ("plain"), ButtonState (0)));
1085         state_pairs.push_back (make_pair (string ("shift"), ShiftDown));
1086         state_pairs.push_back (make_pair (string ("long"), LongPress));
1087
1088         for (vector<state_pair_t>::const_iterator sp = state_pairs.begin(); sp != state_pairs.end(); ++sp) {
1089                 if ((x = on_press.find (sp->second)) != on_press.end()) {
1090                         if (x->second.type == NamedAction) {
1091                                 node->add_property (string (sp->first + X_("-press")).c_str(), x->second.action_name);
1092                         }
1093                 }
1094
1095                 if ((x = on_release.find (sp->second)) != on_release.end()) {
1096                         if (x->second.type == NamedAction) {
1097                                 node->add_property (string (sp->first + X_("-release")).c_str(), x->second.action_name);
1098                         }
1099                 }
1100         }
1101
1102         return *node;
1103 }
1104
1105 void
1106 FaderPort::gui_track_selection_changed (RouteNotificationListPtr routes)
1107 {
1108         boost::shared_ptr<Route> r;
1109
1110         if (!routes->empty()) {
1111                 r = routes->front().lock();
1112         }
1113
1114         set_current_route (r);
1115 }
1116
1117 void
1118 FaderPort::drop_current_route ()
1119 {
1120         if (_current_route) {
1121                 if (_current_route == session->monitor_out()) {
1122                         set_current_route (session->master_out());
1123                 } else {
1124                         set_current_route (boost::shared_ptr<Route>());
1125                 }
1126         }
1127 }
1128
1129 void
1130 FaderPort::set_current_route (boost::shared_ptr<Route> r)
1131 {
1132         route_connections.drop_connections ();
1133
1134         _current_route = r;
1135
1136         /* turn this off. It will be turned on back on in use_master() or
1137            use_monitor() as appropriate.
1138         */
1139         get_button(Output).set_led_state (_output_port, false);
1140
1141         if (_current_route) {
1142                 _current_route->DropReferences.connect (route_connections, MISSING_INVALIDATOR, boost::bind (&FaderPort::drop_current_route, this), this);
1143
1144                 _current_route->mute_changed.connect (route_connections, MISSING_INVALIDATOR, boost::bind (&FaderPort::map_mute, this), this);
1145                 _current_route->solo_changed.connect (route_connections, MISSING_INVALIDATOR, boost::bind (&FaderPort::map_solo, this), this);
1146                 _current_route->listen_changed.connect (route_connections, MISSING_INVALIDATOR, boost::bind (&FaderPort::map_listen, this), this);
1147
1148                 boost::shared_ptr<Track> t = boost::dynamic_pointer_cast<Track> (_current_route);
1149                 if (t) {
1150                         t->RecordEnableChanged.connect (route_connections, MISSING_INVALIDATOR, boost::bind (&FaderPort::map_recenable, this), this);
1151                 }
1152
1153                 boost::shared_ptr<AutomationControl> control = _current_route->gain_control ();
1154                 if (control) {
1155                         control->Changed.connect (route_connections, MISSING_INVALIDATOR, boost::bind (&FaderPort::map_gain, this), this);
1156                         control->alist()->automation_state_changed.connect (route_connections, MISSING_INVALIDATOR, boost::bind (&FaderPort::map_auto, this), this);
1157                 }
1158
1159                 boost::shared_ptr<MonitorProcessor> mp = _current_route->monitor_control();
1160                 if (mp) {
1161                         mp->cut_control()->Changed.connect (route_connections, MISSING_INVALIDATOR, boost::bind (&FaderPort::map_cut, this), this);
1162                 }
1163         }
1164
1165         //ToDo: subscribe to the fader automation modes so we can light the LEDs
1166
1167         map_route_state ();
1168 }
1169
1170 void
1171 FaderPort::map_auto ()
1172 {
1173         /* Under no circumstances send a message to "enable" the LED state of
1174          * the Off button, because this will disable the fader.
1175          */
1176
1177         boost::shared_ptr<AutomationControl> control = _current_route->gain_control ();
1178         const AutoState as = control->automation_state ();
1179
1180         switch (as) {
1181                 case ARDOUR::Play:
1182                         get_button (FP_Read).set_led_state (_output_port, true);
1183                         get_button (FP_Write).set_led_state (_output_port, false);
1184                         get_button (FP_Touch).set_led_state (_output_port, false);
1185                 break;
1186                 case ARDOUR::Write:
1187                         get_button (FP_Read).set_led_state (_output_port, false);
1188                         get_button (FP_Write).set_led_state (_output_port, true);
1189                         get_button (FP_Touch).set_led_state (_output_port, false);
1190                 break;
1191                 case ARDOUR::Touch:
1192                         get_button (FP_Read).set_led_state (_output_port, false);
1193                         get_button (FP_Write).set_led_state (_output_port, false);
1194                         get_button (FP_Touch).set_led_state (_output_port, true);
1195                 break;
1196                 case ARDOUR::Off:
1197                         get_button (FP_Read).set_led_state (_output_port, false);
1198                         get_button (FP_Write).set_led_state (_output_port, false);
1199                         get_button (FP_Touch).set_led_state (_output_port, false);
1200                 break;
1201         }
1202
1203 }
1204
1205
1206 void
1207 FaderPort::map_cut ()
1208 {
1209         boost::shared_ptr<MonitorProcessor> mp = _current_route->monitor_control();
1210
1211         if (mp) {
1212                 bool yn = mp->cut_all ();
1213                 if (yn) {
1214                         start_blinking (Mute);
1215                 } else {
1216                         stop_blinking (Mute);
1217                 }
1218         } else {
1219                 stop_blinking (Mute);
1220         }
1221 }
1222
1223 void
1224 FaderPort::map_mute ()
1225 {
1226         if (_current_route) {
1227                 if (_current_route->muted()) {
1228                         stop_blinking (Mute);
1229                         get_button (Mute).set_led_state (_output_port, true);
1230                 } else if (_current_route->muted_by_others()) {
1231                         start_blinking (Mute);
1232                 } else {
1233                         stop_blinking (Mute);
1234                 }
1235         } else {
1236                 stop_blinking (Mute);
1237         }
1238 }
1239
1240 void
1241 FaderPort::map_solo ()
1242 {
1243         if (_current_route) {
1244                 get_button (Solo).set_led_state (_output_port, _current_route->soloed() || _current_route->listening_via_monitor());
1245         } else {
1246                 get_button (Solo).set_led_state (_output_port, false);
1247         }
1248 }
1249
1250 void
1251 FaderPort::map_listen ()
1252 {
1253         if (_current_route) {
1254                 get_button (Solo).set_led_state (_output_port, _current_route->listening_via_monitor());
1255         } else {
1256                 get_button (Solo).set_led_state (_output_port, false);
1257         }
1258 }
1259
1260 void
1261 FaderPort::map_recenable ()
1262 {
1263         boost::shared_ptr<Track> t = boost::dynamic_pointer_cast<Track> (_current_route);
1264         if (t) {
1265                 get_button (Rec).set_led_state (_output_port, t->record_enabled());
1266         } else {
1267                 get_button (Rec).set_led_state (_output_port, false);
1268         }
1269 }
1270
1271 void
1272 FaderPort::map_gain ()
1273 {
1274         if (fader_is_touched) {
1275                 /* Do not send fader moves while the user is touching the fader */
1276                 return;
1277         }
1278
1279         if (!_current_route) {
1280                 return;
1281         }
1282
1283         boost::shared_ptr<AutomationControl> control = _current_route->gain_control ();
1284         double val;
1285
1286         if (!control) {
1287                 val = 0.0;
1288         } else {
1289                 val = control->internal_to_interface (control->get_value ());
1290         }
1291
1292         /* Faderport sends fader position with range 0..16384 (though some of
1293          * the least-significant bits at the top end are missing - it may only
1294          * get to 1636X or so).
1295          *
1296          * But ... position must be sent in the range 0..1023.
1297          *
1298          * Thanks, Obama.
1299          */
1300
1301         int ival = (int) lrintf (val * 1023.0);
1302
1303         /* MIDI normalization requires that we send two separate messages here,
1304          * not one single 6 byte one.
1305          */
1306
1307         MIDI::byte buf[3];
1308
1309         buf[0] = 0xb0;
1310         buf[1] = 0x0;
1311         buf[2] = ival >> 7;
1312
1313         _output_port->write (buf, 3, 0);
1314
1315         buf[1] = 0x20;
1316         buf[2] = ival & 0x7f;
1317
1318         _output_port->write (buf, 3, 0);
1319 }
1320
1321 void
1322 FaderPort::map_route_state ()
1323 {
1324         if (!_current_route) {
1325                 stop_blinking (Mute);
1326                 stop_blinking (Solo);
1327                 get_button (Rec).set_led_state (_output_port, false);
1328         } else {
1329                 map_solo ();
1330                 map_recenable ();
1331                 map_gain ();
1332                 map_auto ();
1333
1334                 if (_current_route == session->monitor_out()) {
1335                         map_cut ();
1336                 } else {
1337                         map_mute ();
1338                 }
1339         }
1340 }
1341
1342 list<boost::shared_ptr<ARDOUR::Bundle> >
1343 FaderPort::bundles ()
1344 {
1345         list<boost::shared_ptr<ARDOUR::Bundle> > b;
1346
1347         if (_input_bundle) {
1348                 b.push_back (_input_bundle);
1349                 b.push_back (_output_bundle);
1350         }
1351
1352         return b;
1353 }
1354
1355 boost::shared_ptr<Port>
1356 FaderPort::output_port()
1357 {
1358         return _output_port;
1359 }
1360
1361 boost::shared_ptr<Port>
1362 FaderPort::input_port()
1363 {
1364         return _input_port;
1365 }
1366
1367 void
1368 FaderPort::set_action (ButtonID id, std::string const& action_name, bool on_press, ButtonState bs)
1369 {
1370         get_button(id).set_action (action_name, on_press, bs);
1371 }
1372
1373 string
1374 FaderPort::get_action (ButtonID id, bool press, ButtonState bs)
1375 {
1376         return get_button(id).get_action (press, bs);
1377 }