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