Call PBD::EnumWriter::destroy in ARDOUR::cleanup (from
[ardour.git] / libs / surfaces / mackie / mackie_control_protocol.cc
index 85b92cbeabe72a8f6d676e8bbf59ea57f39e757e..b13314fdc4472bd96c273deb9c2527f0cf99d7c9 100644 (file)
@@ -107,13 +107,14 @@ MackieControlProtocol::MackieControlProtocol (Session& session)
        , _modifier_state (0)
        , _ipmidi_base (MIDI::IPMIDIPort::lowest_ipmidi_port_default)
        , needs_ipmidi_restart (false)
+       , _metering_active (true)
 {
        DEBUG_TRACE (DEBUG::MackieControl, "MackieControlProtocol::MackieControlProtocol\n");
 
        DeviceInfo::reload_device_info ();
        DeviceProfile::reload_device_profiles ();
 
-       TrackSelectionChanged.connect (gui_connections, MISSING_INVALIDATOR, boost::bind (&MackieControlProtocol::gui_track_selection_changed, this, _1), this);
+       TrackSelectionChanged.connect (gui_connections, MISSING_INVALIDATOR, boost::bind (&MackieControlProtocol::gui_track_selection_changed, this, _1, true), this);
 
        _instance = this;
 
@@ -123,6 +124,9 @@ MackieControlProtocol::MackieControlProtocol (Session& session)
 MackieControlProtocol::~MackieControlProtocol()
 {
        DEBUG_TRACE (DEBUG::MackieControl, "MackieControlProtocol::~MackieControlProtocol\n");
+       
+       drop_connections ();
+       tear_down_gui ();
 
        _active = false;
 
@@ -357,7 +361,7 @@ MackieControlProtocol::switch_banks (uint32_t initial, bool force)
 
        /* make sure selection is correct */
        
-       // gui_track_selection_changed (_last_selected_routes);
+       _gui_track_selection_changed (&_last_selected_routes, false);
        
        /* current bank has not been saved */
        session->set_dirty();
@@ -469,7 +473,7 @@ MackieControlProtocol::update_global_led (int id, LedState ls)
 {
        boost::shared_ptr<Surface> surface = surfaces.front();
 
-       if (!surface->type() == mcu) {
+       if (surface->type() != mcu) {
                return;
        }
 
@@ -710,40 +714,29 @@ MackieControlProtocol::set_state (const XMLNode & node, int /*version*/)
        return retval;
 }
 
-
-/////////////////////////////////////////////////
-// handlers for Route signals
-// TODO should these be part of RouteSignal?
-// They started off as signal/slot handlers for signals
-// from Route, but they're also used in polling for automation
-/////////////////////////////////////////////////
-
-// TODO handle plugin automation polling
 string 
 MackieControlProtocol::format_bbt_timecode (framepos_t now_frame)
 {
        Timecode::BBT_Time bbt_time;
+
        session->bbt_time (now_frame, bbt_time);
 
-       // According to the Logic docs
-       // digits: 888/88/88/888
-       // BBT mode: Bars/Beats/Subdivisions/Ticks
+       // The Mackie protocol spec is built around a BBT time display of
+       //
+       // digits:     888/88/88/888
+       // semantics:  BBB/bb/ss/ttt
+       //
+       // The third field is "subdivisions" which is a concept found in Logic
+       // but not present in Ardour. Instead Ardour displays a 4 digit tick
+       // count, which we need to spread across the 5 digits of ss/ttt.
+
        ostringstream os;
+
        os << setw(3) << setfill('0') << bbt_time.bars;
        os << setw(2) << setfill('0') << bbt_time.beats;
-
-       // figure out subdivisions per beat
-       const ARDOUR::Meter & meter = session->tempo_map().meter_at (now_frame);
-       int subdiv = 2;
-       if (meter.note_divisor() == 8 && (meter.divisions_per_bar() == 12.0 || meter.divisions_per_bar() == 9.0 || meter.divisions_per_bar() == 6.0)) {
-               subdiv = 3;
-       }
-
-       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;
+       os << ' ';
+       os << setw(1) << setfill('0') << bbt_time.ticks / 1000;
+       os << setw(3) << setfill('0') << bbt_time.ticks % 1000;
 
        return os.str();
 }
@@ -758,10 +751,12 @@ MackieControlProtocol::format_timecode_timecode (framepos_t now_frame)
        // digits: 888/88/88/888
        // Timecode mode: Hours/Minutes/Seconds/Frames
        ostringstream os;
-       os << setw(3) << setfill('0') << timecode.hours;
+       os << ' ';
+       os << setw(2) << setfill('0') << timecode.hours;
        os << setw(2) << setfill('0') << timecode.minutes;
        os << setw(2) << setfill('0') << timecode.seconds;
-       os << setw(3) << setfill('0') << timecode.frames;
+       os << ' ';
+       os << setw(2) << setfill('0') << timecode.frames;
 
        return os.str();
 }
@@ -775,7 +770,7 @@ MackieControlProtocol::update_timecode_display()
 
        boost::shared_ptr<Surface> surface = surfaces.front();
 
-       if (surface->type() != mcu || !_device_info.has_timecode_display()) {
+       if (surface->type() != mcu || !_device_info.has_timecode_display() || !surface->active ()) {
                return;
        }
 
@@ -890,9 +885,19 @@ MackieControlProtocol::notify_transport_state_changed()
        update_global_button (Button::Rewind, session->transport_speed() < 0.0);
        update_global_button (Button::Ffwd, session->transport_speed() > 1.0);
 
+       notify_metering_state_changed ();
+       
        _transport_previously_rolling = session->transport_rolling();
 }
 
+void 
+MackieControlProtocol::notify_metering_state_changed()
+{
+       for (Surfaces::iterator s = surfaces.begin(); s != surfaces.end(); ++s) {
+               (*s)->notify_metering_state_changed ();
+       }       
+}
+
 void
 MackieControlProtocol::notify_record_state_changed ()
 {
@@ -1130,6 +1135,18 @@ MackieControlProtocol::midi_input_handler (IOCondition ioc, MIDI::Port* port)
 
        if (ioc & IO_IN) {
 
+               /* Devices using regular JACK MIDI ports will need to have
+                  the x-thread FIFO drained to avoid burning endless CPU.
+
+                  Devices using ipMIDI have port->selectable() as the same
+                  file descriptor that data arrives on, so doing this
+                  for them will simply throw all incoming data away.
+               */
+
+               if (!_device_info.uses_ipmidi()) {
+                       CrossThreadChannel::drain (port->selectable());
+               }
+
                DEBUG_TRACE (DEBUG::MackieControl, string_compose ("data available on %1\n", port->name()));
                framepos_t now = session->engine().frame_time();
                port->parse (now);
@@ -1204,13 +1221,40 @@ MackieControlProtocol::force_special_route_to_strip (boost::shared_ptr<Route> r,
 }
 
 void
-MackieControlProtocol::gui_track_selection_changed (ARDOUR::RouteNotificationListPtr rl)
+MackieControlProtocol::gui_track_selection_changed (ARDOUR::RouteNotificationListPtr rl, bool save_list)
+{
+       _gui_track_selection_changed (rl.get(), save_list);
+}
+
+void
+MackieControlProtocol::_gui_track_selection_changed (ARDOUR::RouteNotificationList* rl, bool save_list)
 {
+
+       /* We need to keep a list of the most recently selected routes around,
+          but we are not allowed to keep shared_ptr<Route> unless we want to
+          handle the complexities of route deletion. So instead, the GUI sends
+          us a notification using weak_ptr<Route>, which we keep a copy
+          of. For efficiency's sake, however, we convert the weak_ptr's into
+          shared_ptr<Route> before passing them to however many surfaces (and
+          thus strips) that we have.
+       */
+
+       StrongRouteNotificationList srl;
+
+       for (ARDOUR::RouteNotificationList::const_iterator i = rl->begin(); i != rl->end(); ++i) {
+               boost::shared_ptr<ARDOUR::Route> r = (*i).lock();
+               if (r) {
+                       srl.push_back (r);
+               }
+       }
+
        for (Surfaces::iterator s = surfaces.begin(); s != surfaces.end(); ++s) {
-               (*s)->gui_selection_changed (rl);
+               (*s)->gui_selection_changed (srl);
        }
        
-       // _last_selected_routes = *rl;
+       if (save_list) {
+               _last_selected_routes = *rl;
+       }
 }
 
 framepos_t