Mackie Control, Fix sends after monitor not showing.
[ardour.git] / libs / surfaces / faderport / faderport.cc
index 97155480e7b56e79086e6a0b1a9cd8f2154b0af7..cb231633d27b35d5f1bbb0d107fce373eb7abf24 100644 (file)
@@ -66,7 +66,7 @@ using namespace std;
 
 FaderPort::FaderPort (Session& s)
        : ControlProtocol (s, _("Faderport"))
-       , AbstractUI<FaderPortRequest> ("faderport")
+       , AbstractUI<FaderPortRequest> (name())
        , gui (0)
        , connection_state (ConnectionState (0))
        , _device_active (false)
@@ -152,8 +152,11 @@ FaderPort::FaderPort (Session& s)
        get_button (Undo).set_flash (true);
 
        get_button (FP_Read).set_action (boost::bind (&FaderPort::read, this), true);
+       get_button (FP_Read).set_action (boost::bind (&FaderPort::off, this), false, LongPress);
        get_button (FP_Write).set_action (boost::bind (&FaderPort::write, this), true);
+       get_button (FP_Write).set_action (boost::bind (&FaderPort::off, this), false, LongPress);
        get_button (FP_Touch).set_action (boost::bind (&FaderPort::touch, this), true);
+       get_button (FP_Touch).set_action (boost::bind (&FaderPort::off, this), false, LongPress);
        get_button (FP_Off).set_action (boost::bind (&FaderPort::off, this), true);
 
        get_button (Play).set_action (boost::bind (&BasicUI::transport_play, this, true), true);
@@ -179,7 +182,7 @@ FaderPort::FaderPort (Session& s)
        get_button (Loop).set_action (boost::bind (&BasicUI::add_marker, this, string()), true, ShiftDown);
 
        get_button (Punch).set_action (boost::bind (&BasicUI::prev_marker, this), true, ShiftDown);
-       get_button (User).set_action (boost::bind (&BasicUI::next_marker, this), true, ShiftDown);
+       get_button (User).set_action (boost::bind (&BasicUI::next_marker, this), true, ButtonState(ShiftDown|UserDown));
 
        get_button (Mute).set_action (boost::bind (&FaderPort::mute, this), true);
        get_button (Solo).set_action (boost::bind (&FaderPort::solo, this), true);
@@ -207,6 +210,17 @@ FaderPort::~FaderPort ()
        tear_down_gui ();
 }
 
+void*
+FaderPort::request_factory (uint32_t num_requests)
+{
+       /* AbstractUI<T>::request_buffer_factory() is a template method only
+          instantiated in this source module. To provide something visible for
+          use in the interface/descriptor, we have this static method that is
+          template-free.
+       */
+       return request_buffer_factory (num_requests);
+}
+
 void
 FaderPort::start_midi_handling ()
 {
@@ -264,10 +278,10 @@ FaderPort::thread_init ()
 {
        struct sched_param rtparam;
 
-       pthread_set_name (X_("FaderPort"));
+       pthread_set_name (event_loop_name().c_str());
 
-       PBD::notify_gui_about_thread_creation (X_("gui"), pthread_self(), X_("FaderPort"), 2048);
-       ARDOUR::SessionEvent::create_per_thread_pool (X_("FaderPort"), 128);
+       PBD::notify_event_loops_about_thread_creation (pthread_self(), event_loop_name(), 2048);
+       ARDOUR::SessionEvent::create_per_thread_pool (event_loop_name(), 128);
 
        memset (&rtparam, 0, sizeof (rtparam));
        rtparam.sched_priority = 9; /* XXX should be relative to audio (JACK) thread */
@@ -281,7 +295,7 @@ void
 FaderPort::all_lights_out ()
 {
        for (ButtonMap::iterator b = buttons.begin(); b != buttons.end(); ++b) {
-               b->second.set_led_state (_output_port, false, true);
+               b->second.set_led_state (_output_port, false);
        }
 }
 
@@ -324,6 +338,8 @@ FaderPort::button_handler (MIDI::Parser &, MIDI::EventTwoBytes* tb)
        ButtonID id (ButtonID (tb->controller_number));
        Button& button (get_button (id));
 
+       DEBUG_TRACE (DEBUG::FaderPort, string_compose ("button event for ID %1 press ? %2\n", (int) tb->controller_number, (tb->value ? "yes" : "no")));
+
        if (tb->value) {
                buttons_down.insert (id);
        } else {
@@ -335,10 +351,7 @@ FaderPort::button_handler (MIDI::Parser &, MIDI::EventTwoBytes* tb)
 
        switch (id) {
        case Shift:
-               /* set this bit on press, do NOT clear it on release */
-               if (tb->value) {
-                       bs = ShiftDown;
-               }
+               bs = ShiftDown;
                break;
        case Stop:
                bs = StopDown;
@@ -372,7 +385,7 @@ FaderPort::button_handler (MIDI::Parser &, MIDI::EventTwoBytes* tb)
 
        if (bs) {
                button_state = (tb->value ? ButtonState (button_state|bs) : ButtonState (button_state&~bs));
-               DEBUG_TRACE (DEBUG::FaderPort, string_compose ("reset button state to %1%2 using %3%4\n", hex, button_state, bs, dec));
+               DEBUG_TRACE (DEBUG::FaderPort, string_compose ("reset button state to %1 using %2\n", button_state, (int) bs));
        }
 
        if (button.uses_flash()) {
@@ -384,19 +397,9 @@ FaderPort::button_handler (MIDI::Parser &, MIDI::EventTwoBytes* tb)
        if (c == consumed.end()) {
                button.invoke (button_state, tb->value ? true : false);
        } else {
+               DEBUG_TRACE (DEBUG::FaderPort, "button was consumed, ignored\n");
                consumed.erase (c);
        }
-
-       if (!tb->value && (id != Shift)) {
-               /* non-shift key was released, clear shift modifier */
-               button_state = ButtonState (button_state&~ShiftDown);
-               DEBUG_TRACE (DEBUG::FaderPort, "clear shift modifier\n");
-       }
-
-       if (!tb->value && (id != User)) {
-               consumed.insert (User);
-               DEBUG_TRACE (DEBUG::FaderPort, "clear user modifier\n");
-       }
 }
 
 void
@@ -501,6 +504,8 @@ FaderPort::fader_handler (MIDI::Parser &, MIDI::EventTwoBytes* tb)
 void
 FaderPort::sysex_handler (MIDI::Parser &p, MIDI::byte *buf, size_t sz)
 {
+        DEBUG_TRACE (DEBUG::FaderPort, string_compose ("sysex message received, size = %1\n", sz));
+
        if (sz < 17) {
                return;
        }
@@ -535,14 +540,14 @@ FaderPort::sysex_handler (MIDI::Parser &p, MIDI::byte *buf, size_t sz)
 
        /* catch up on state */
 
-       notify_transport_state_changed ();
-       notify_record_state_changed ();
+       map_transport_state ();
+       map_recenable_state ();
 }
 
 int
 FaderPort::set_active (bool yn)
 {
-       DEBUG_TRACE (DEBUG::FaderPort, string_compose("MackieControlProtocol::set_active init with yn: '%1'\n", yn));
+       DEBUG_TRACE (DEBUG::FaderPort, string_compose("Faderport::set_active init with yn: '%1'\n", yn));
 
        if (yn == active()) {
                return 0;
@@ -560,6 +565,10 @@ FaderPort::set_active (bool yn)
                blink_connection = blink_timeout->connect (sigc::mem_fun (*this, &FaderPort::blink));
                blink_timeout->attach (main_loop()->get_context());
 
+               Glib::RefPtr<Glib::TimeoutSource> periodic_timeout = Glib::TimeoutSource::create (100); // milliseconds
+               periodic_connection = periodic_timeout->connect (sigc::mem_fun (*this, &FaderPort::periodic));
+               periodic_timeout->attach (main_loop()->get_context());
+
        } else {
 
                BaseUI::quit ();
@@ -569,11 +578,27 @@ FaderPort::set_active (bool yn)
 
        ControlProtocol::set_active (yn);
 
-       DEBUG_TRACE (DEBUG::FaderPort, string_compose("MackieControlProtocol::set_active done with yn: '%1'\n", yn));
+       DEBUG_TRACE (DEBUG::FaderPort, string_compose("Faderport::set_active done with yn: '%1'\n", yn));
 
        return 0;
 }
 
+bool
+FaderPort::periodic ()
+{
+       if (!_current_route) {
+               return true;
+       }
+
+       ARDOUR::AutoState gain_state = _current_route->gain_control()->automation_state();
+
+       if (gain_state == ARDOUR::Touch || gain_state == ARDOUR::Play) {
+               map_gain ();
+       }
+
+       return true;
+}
+
 void
 FaderPort::stop_blinking (ButtonID id)
 {
@@ -618,7 +643,7 @@ FaderPort::close ()
 }
 
 void
-FaderPort::notify_record_state_changed ()
+FaderPort::map_recenable_state ()
 {
        switch (session->record_status()) {
        case Session::Disabled:
@@ -634,10 +659,21 @@ FaderPort::notify_record_state_changed ()
 }
 
 void
-FaderPort::notify_transport_state_changed ()
+FaderPort::map_transport_state ()
 {
        get_button (Loop).set_led_state (_output_port, session->get_play_loop());
-       get_button (Play).set_led_state (_output_port, session->transport_speed() == 1.0);
+
+       float ts = session->transport_speed();
+
+       if (ts == 0) {
+               stop_blinking (Play);
+       } else if (fabs (ts) == 1.0) {
+               stop_blinking (Play);
+               get_button (Play).set_led_state (_output_port, true);
+       } else {
+               start_blinking (Play);
+       }
+
        get_button (Stop).set_led_state (_output_port, session->transport_stopped ());
        get_button (Rewind).set_led_state (_output_port, session->transport_speed() < 0.0);
        get_button (Ffwd).set_led_state (_output_port, session->transport_speed() > 1.0);
@@ -663,8 +699,8 @@ FaderPort::parameter_changed (string what)
 void
 FaderPort::connect_session_signals()
 {
-       session->RecordStateChanged.connect(session_connections, MISSING_INVALIDATOR, boost::bind (&FaderPort::notify_record_state_changed, this), this);
-       session->TransportStateChange.connect(session_connections, MISSING_INVALIDATOR, boost::bind (&FaderPort::notify_transport_state_changed, this), this);
+       session->RecordStateChanged.connect(session_connections, MISSING_INVALIDATOR, boost::bind (&FaderPort::map_recenable_state, this), this);
+       session->TransportStateChange.connect(session_connections, MISSING_INVALIDATOR, boost::bind (&FaderPort::map_transport_state, this), this);
        /* not session, but treat it similarly */
        session->config.ParameterChanged.connect (session_connections, MISSING_INVALIDATOR, boost::bind (&FaderPort::parameter_changed, this, _1), this);
 }
@@ -800,6 +836,7 @@ FaderPort::connection_handler (boost::weak_ptr<ARDOUR::Port>, std::string name1,
                */
 
                g_usleep (100000);
+                DEBUG_TRACE (DEBUG::FaderPort, "device now connected for both input and output\n");
                connected ();
 
        } else {
@@ -817,7 +854,7 @@ FaderPort::connection_handler (boost::weak_ptr<ARDOUR::Port>, std::string name1,
 void
 FaderPort::connected ()
 {
-       DEBUG_TRACE (DEBUG::FaderPort, "connection status changed\n");
+       DEBUG_TRACE (DEBUG::FaderPort, "sending device inquiry message...\n");
 
        start_midi_handling ();
 
@@ -844,12 +881,12 @@ FaderPort::Button::invoke (FaderPort::ButtonState bs, bool press)
 
        if (press) {
                if ((x = on_press.find (bs)) == on_press.end()) {
-                       DEBUG_TRACE (DEBUG::FaderPort, string_compose ("no press action for button %1 state %2%3\%4\n", id, hex, bs, dec));
+                       DEBUG_TRACE (DEBUG::FaderPort, string_compose ("no press action for button %1 state %2 @ %3 in %4\n", id, bs, this, &on_press));
                        return;
                }
        } else {
                if ((x = on_release.find (bs)) == on_release.end()) {
-                       DEBUG_TRACE (DEBUG::FaderPort, string_compose ("no release action for button %1 state %2%3\%4\n", id, hex, bs, dec));
+                       DEBUG_TRACE (DEBUG::FaderPort, string_compose ("no release action for button %1 state %2 @%3 in %4\n", id, bs, this, &on_release));
                        return;
                }
        }
@@ -924,22 +961,19 @@ FaderPort::Button::set_action (boost::function<void()> f, bool when_pressed, Fad
        todo.type = InternalFunction;
 
        if (when_pressed) {
+               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));
                todo.function = f;
                on_press[bs] = todo;
        } else {
+               DEBUG_TRACE (DEBUG::FaderPort, string_compose ("set button %1 (%2) @ %5 to some functor on release + %3\n", id, name, bs, this));
                todo.function = f;
                on_release[bs] = todo;
        }
 }
 
 void
-FaderPort::Button::set_led_state (boost::shared_ptr<MIDI::Port> port, int onoff, bool force)
+FaderPort::Button::set_led_state (boost::shared_ptr<MIDI::Port> port, bool onoff)
 {
-       if (!force && (led_on == (bool) onoff)) {
-               /* nothing to do */
-               return;
-       }
-
        if (out < 0) {
                /* fader button ID - no LED */
                return;
@@ -950,7 +984,6 @@ FaderPort::Button::set_led_state (boost::shared_ptr<MIDI::Port> port, int onoff,
        buf[1] = out;
        buf[2] = onoff ? 1 : 0;
        port->write (buf, 3, 0);
-       led_on = (onoff ? true : false);
 }
 
 int
@@ -973,9 +1006,6 @@ FaderPort::Button::set_state (XMLNode const& node)
        state_pairs.push_back (make_pair (string ("shift"), ShiftDown));
        state_pairs.push_back (make_pair (string ("long"), LongPress));
 
-       on_press.clear ();
-       on_release.clear ();
-
        for (vector<state_pair_t>::const_iterator sp = state_pairs.begin(); sp != state_pairs.end(); ++sp) {
                string propname;
 
@@ -1098,6 +1128,10 @@ FaderPort::set_current_route (boost::shared_ptr<Route> r)
 void
 FaderPort::map_auto ()
 {
+       /* Under no circumstances send a message to "enable" the LED state of
+        * the Off button, because this will disable the fader.
+        */
+
        boost::shared_ptr<AutomationControl> control = _current_route->gain_control ();
        const AutoState as = control->automation_state ();
 
@@ -1106,25 +1140,21 @@ FaderPort::map_auto ()
                        get_button (FP_Read).set_led_state (_output_port, true);
                        get_button (FP_Write).set_led_state (_output_port, false);
                        get_button (FP_Touch).set_led_state (_output_port, false);
-                       get_button (FP_Off).set_led_state (_output_port, false);
                break;
                case ARDOUR::Write:
                        get_button (FP_Read).set_led_state (_output_port, false);
                        get_button (FP_Write).set_led_state (_output_port, true);
                        get_button (FP_Touch).set_led_state (_output_port, false);
-                       get_button (FP_Off).set_led_state (_output_port, false);
                break;
                case ARDOUR::Touch:
                        get_button (FP_Read).set_led_state (_output_port, false);
                        get_button (FP_Write).set_led_state (_output_port, false);
                        get_button (FP_Touch).set_led_state (_output_port, true);
-                       get_button (FP_Off).set_led_state (_output_port, false);
                break;
                case ARDOUR::Off:
                        get_button (FP_Read).set_led_state (_output_port, false);
                        get_button (FP_Write).set_led_state (_output_port, false);
                        get_button (FP_Touch).set_led_state (_output_port, false);
-                       get_button (FP_Off).set_led_state (_output_port, true);
                break;
        }
 
@@ -1151,19 +1181,38 @@ FaderPort::map_cut ()
 void
 FaderPort::map_mute (void*)
 {
-       get_button (Mute).set_led_state (_output_port, _current_route->muted());
+       if (_current_route) {
+               if (_current_route->muted()) {
+                       stop_blinking (Mute);
+                       get_button (Mute).set_led_state (_output_port, true);
+               } else if (_current_route->muted_by_others()) {
+                       start_blinking (Mute);
+               } else {
+                       stop_blinking (Mute);
+               }
+       } else {
+               stop_blinking (Mute);
+       }
 }
 
 void
 FaderPort::map_solo (bool, void*, bool)
 {
-       get_button (Solo).set_led_state (_output_port, _current_route->soloed() || _current_route->listening_via_monitor());
+       if (_current_route) {
+               get_button (Solo).set_led_state (_output_port, _current_route->soloed() || _current_route->listening_via_monitor());
+       } else {
+               get_button (Solo).set_led_state (_output_port, false);
+       }
 }
 
 void
 FaderPort::map_listen (void*, bool)
 {
-       get_button (Solo).set_led_state (_output_port, _current_route->listening_via_monitor());
+       if (_current_route) {
+               get_button (Solo).set_led_state (_output_port, _current_route->listening_via_monitor());
+       } else {
+               get_button (Solo).set_led_state (_output_port, false);
+       }
 }
 
 void
@@ -1236,12 +1285,16 @@ FaderPort::map_route_state ()
                get_button (Rec).set_led_state (_output_port, false);
        } else {
                /* arguments to these map_*() methods are all ignored */
-               map_mute (0);
                map_solo (false, 0, false);
                map_recenable ();
                map_gain ();
-               map_cut ();
                map_auto ();
+
+               if (_current_route == session->monitor_out()) {
+                       map_cut ();
+               } else {
+                       map_mute (0);
+               }
        }
 }