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