MCP: stop using signals to handle parsed control events; add debugging
[ardour.git] / libs / surfaces / mackie / mackie_control_protocol.cc
index 6fda7d74049878a8d234282d16107e704e2b4dfa..8ad9db142e3e22358b2785edcb26414d5d717745 100644 (file)
@@ -72,6 +72,8 @@ using namespace PBD;
 
 #include "pbd/abstract_ui.cc" // instantiate template
 
+#define NUCLEUS_DEBUG 1
+
 MackieMidiBuilder builder;
 
 #define midi_ui_context() MidiControlUI::instance() /* a UICallback-derived object that specifies the event loop for signal handling */
@@ -183,14 +185,18 @@ MackiePort&
 MackieControlProtocol::port_for_id (uint32_t index)
 {
        uint32_t current_max = 0;
+
        for (MackiePorts::iterator it = _ports.begin(); it != _ports.end(); ++it) {
                current_max += (*it)->strips();
-               if (index < current_max) return **it;
+               if (index < current_max) { 
+                       return **it;
+               }
        }
 
        // oops - no matching port
        ostringstream os;
        os << "No port for index " << index;
+       cerr << "No port for index " << index << endl;
        throw MackieControlException (os.str());
 }
 
@@ -279,10 +285,8 @@ MackieControlProtocol::switch_banks (int initial)
 
                DEBUG_TRACE (DEBUG::MackieControl, string_compose ("switch to %1, %2\n", _current_initial_bank, end_pos));
 
-               // clear out routes from our table in case any have been deleted
-               for (vector<boost::shared_ptr<Route> >::iterator i = route_table.begin(); i != route_table.end(); ++i) {
-                       i->reset ();
-               }
+               route_table.clear ();
+               set_route_table_size (surface().strips.size());
 
                // link routes to strips
                uint32_t i = 0;
@@ -294,7 +298,7 @@ MackieControlProtocol::switch_banks (int initial)
 
                        DEBUG_TRACE (DEBUG::MackieControl, string_compose ("remote id %1 connecting %2 to %3 with port %4\n", 
                                                                           route->remote_control_id(), route->name(), strip.name(), port_for_id(i)));
-                       route_table[i] = route;
+                       set_route_table (1, route);
                        RouteSignal * rs = new RouteSignal (route, *this, strip, port_for_id(i));
                        route_signals.push_back (rs);
                        // update strip from route
@@ -556,11 +560,11 @@ MackieControlProtocol::connect_session_signals()
 }
 
 void 
-MackieControlProtocol::add_port (MIDI::Port & midi_input_port, MIDI::Port & midi_output_port, int number)
+MackieControlProtocol::add_port (MIDI::Port & midi_input_port, MIDI::Port & midi_output_port, int number, MackiePort::port_type_t port_type)
 {
        DEBUG_TRACE (DEBUG::MackieControl, string_compose ("add port %1 %2\n", midi_input_port.name(), midi_output_port.name()));
 
-       MackiePort * sport = new MackiePort (*this, midi_input_port, midi_output_port, number);
+       MackiePort * sport = new MackiePort (*this, midi_input_port, midi_output_port, number, port_type);
        _ports.push_back (sport);
        
        sport->init_event.connect_same_thread (port_connections, boost::bind (&MackieControlProtocol::handle_port_init, this, sport));
@@ -600,7 +604,7 @@ MackieControlProtocol::create_ports()
                throw MackieControlException (os.str());
        }
 
-       add_port (*midi_input_port, *midi_output_port, 0);
+       add_port (*midi_input_port, *midi_output_port, 0, MackiePort::mcu);
 
        /* Create extender ports */
 
@@ -612,7 +616,7 @@ MackieControlProtocol::create_ports()
                        new MIDI::Port (string_compose (_("mcu_xt_%1 out"), index), MIDI::Port::IsOutput, session->engine().jack())
                        );
                if (midi_input_port->ok() && midi_output_port->ok()) {
-                       add_port (*midi_input_port, *midi_output_port, index);
+                       add_port (*midi_input_port, *midi_output_port, index, MackiePort::ext);
                }
        }
 }
@@ -653,12 +657,6 @@ MackieControlProtocol::initialize_surface()
        }
 
        _surface->init();
-
-       // Connect events. Must be after route table otherwise there will be trouble
-
-       for (MackiePorts::iterator it = _ports.begin(); it != _ports.end(); ++it) {
-               (*it)->control_event.connect_same_thread (port_connections, boost::bind (&MackieControlProtocol::handle_control_event, this, _1, _2, _3));
-       }
 }
 
 void 
@@ -763,15 +761,26 @@ MackieControlProtocol::handle_control_event (SurfacePort & port, Control & contr
 {
        // find the route for the control, if there is one
        boost::shared_ptr<Route> route;
+
        if (control.group().is_strip()) {
                if (control.group().is_master()) {
+                       DEBUG_TRACE (DEBUG::MackieControl, "master strip control event\n");
                        route = master_route();
                } else {
                        uint32_t index = control.ordinal() - 1 + (port.number() * port.strips());
-                       if (index < route_table.size())
+                       DEBUG_TRACE (DEBUG::MackieControl, string_compose ("strip control event, index = %1, rt size = %2\n",
+                                                                          index, route_table.size()));
+                       if (index < route_table.size()) {
                                route = route_table[index];
-                       else
+                               if (route) {
+                                       DEBUG_TRACE (DEBUG::MackieControl, string_compose ("modifying %1\n", route->name()));
+                               } else {
+                                       DEBUG_TRACE (DEBUG::MackieControl, "no route found!\n");
+                               }
+                       } else {
                                cerr << "Warning: index is " << index << " which is not in the route table, size: " << route_table.size() << endl;
+                               DEBUG_TRACE (DEBUG::MackieControl, "illegal route index found!\n");
+                       }
                }
        }
 
@@ -785,6 +794,8 @@ MackieControlProtocol::handle_control_event (SurfacePort & port, Control & contr
                        // at which point the fader should just reset itself
                        if (route != 0)
                        {
+                               DEBUG_TRACE (DEBUG::MackieControl, string_compose ("fader to %1\n", state.pos));
+
                                route->gain_control()->set_value (slider_position_to_gain (state.pos));
 
                                if (ARDOUR::Config->get_mackie_emulation() == "bcf") {
@@ -802,6 +813,7 @@ MackieControlProtocol::handle_control_event (SurfacePort & port, Control & contr
                case Control::type_button:
                        if (control.group().is_strip()) {
                                // strips
+                               DEBUG_TRACE (DEBUG::MackieControl, string_compose ("strip button %1\n", control.id()));
                                if (route != 0) {
                                        handle_strip_button (port, control, state.button_state, route);
                                } else {
@@ -811,11 +823,13 @@ MackieControlProtocol::handle_control_event (SurfacePort & port, Control & contr
                                }
                        } else if (control.group().is_master()) {
                                // master fader touch
+                               DEBUG_TRACE (DEBUG::MackieControl, string_compose ("master strip button %1\n", control.id()));
                                if (route != 0) {
                                        handle_strip_button (port, control, state.button_state, route);
                                }
                        } else {
                                // handle all non-strip buttons
+                               DEBUG_TRACE (DEBUG::MackieControl, string_compose ("global button %1\n", control.id()));
                                surface().handle_button (*this, state.button_state, dynamic_cast<Button&> (control));
                        }
                        break;
@@ -823,6 +837,7 @@ MackieControlProtocol::handle_control_event (SurfacePort & port, Control & contr
                // pot (jog wheel, external control)
                case Control::type_pot:
                        if (control.group().is_strip()) {
+                               DEBUG_TRACE (DEBUG::MackieControl, string_compose ("strip pot %1\n", control.id()));
                                if (route) {
                                         boost::shared_ptr<Panner> panner = route->panner_shell()->panner();
                                        // pan for mono input routes, or stereo linked panners
@@ -839,12 +854,12 @@ MackieControlProtocol::handle_control_event (SurfacePort & port, Control & contr
                                        // it's a pot for an umnapped route, so turn all the lights off
                                        port.write (builder.build_led_ring (dynamic_cast<Pot &> (control), off));
                                }
-                       }
-                       else
-                       {
+                       } else {
                                if (control.is_jog()) {
+                                       DEBUG_TRACE (DEBUG::MackieControl, string_compose ("Jog wheel moved %1\n", state.ticks));
                                        _jog_wheel.jog_event (port, control, state);
                                } else {
+                                       DEBUG_TRACE (DEBUG::MackieControl, string_compose ("External controller moved %1\n", state.ticks));
                                        cout << "external controller" << state.ticks * state.sign << endl;
                                }
                        }
@@ -938,8 +953,7 @@ MackieControlProtocol::notify_property_changed (const PropertyChange& what_chang
        try {
                Strip & strip = route_signal->strip();
                
-               /* XXX: not sure about this check to only display stuff for strips of index < 8 */
-               if (!strip.is_master() && strip.index() < 8) {
+               if (!strip.is_master()) {
                        string line1;
                        string fullname = route_signal->route()->name();
 
@@ -949,6 +963,10 @@ MackieControlProtocol::notify_property_changed (const PropertyChange& what_chang
                                line1 = PBD::short_version (fullname, 6);
                        }
 
+#ifdef NUCLEUS_DEBUG
+                       cerr << "show strip name from " << fullname << " as " << line1 << endl;
+#endif
+
                        SurfacePort & port = route_signal->port();
                        port.write (builder.strip_display (port, strip, 0, line1));
                        port.write (builder.strip_display_blank (port, strip, 1));
@@ -1025,8 +1043,8 @@ MackieControlProtocol::format_bbt_timecode (framepos_t now_frame)
                subdiv = 3;
        }
 
-       uint32_t subdivisions = bbt_time.ticks / uint32_t (Timecode::BBT_Time::ticks_per_bar_division / subdiv);
-       uint32_t ticks = bbt_time.ticks % uint32_t (Timecode::BBT_Time::ticks_per_bar_division / subdiv);
+       uint32_t subdivisions = bbt_time.ticks / uint32_t (Timecode::BBT_Time::ticks_per_beat / subdiv);
+       uint32_t ticks = bbt_time.ticks % uint32_t (Timecode::BBT_Time::ticks_per_beat / subdiv);
 
        os << setw(2) << setfill('0') << subdivisions + 1;
        os << setw(3) << setfill('0') << ticks;