globally remove all trailing whitespace from ardour code base.
[ardour.git] / libs / surfaces / mackie / strip.cc
index 84c32138a34ff1930a06942a3468eb5626f134ca..ca5c79e9e012ce1757124324a51b309f2e482edb 100644 (file)
@@ -23,6 +23,8 @@
 
 #include <sys/time.h>
 
+#include <glibmm/convert.h>
+
 #include "midi++/port.h"
 
 #include "pbd/compose.h"
 #include "jog.h"
 #include "meter.h"
 
-using namespace Mackie;
 using namespace std;
 using namespace ARDOUR;
 using namespace PBD;
+using namespace ArdourSurface;
+using namespace Mackie;
+
+#ifndef timeradd /// only avail with __USE_BSD
+#define timeradd(a,b,result)                         \
+  do {                                               \
+    (result)->tv_sec = (a)->tv_sec + (b)->tv_sec;    \
+    (result)->tv_usec = (a)->tv_usec + (b)->tv_usec; \
+    if ((result)->tv_usec >= 1000000)                \
+    {                                                \
+      ++(result)->tv_sec;                            \
+      (result)->tv_usec -= 1000000;                  \
+    }                                                \
+  } while (0)
+#endif
 
 #define ui_context() MackieControlProtocol::instance() /* a UICallback-derived object that specifies the event loop for signal handling */
 
@@ -70,6 +86,7 @@ Strip::Strip (Surface& s, const std::string& name, int index, const map<Button::
        , _fader_touch (0)
        , _vpot (0)
        , _fader (0)
+       , _meter (0)
        , _index (index)
        , _surface (&s)
        , _controls_locked (false)
@@ -79,6 +96,7 @@ Strip::Strip (Surface& s, const std::string& name, int index, const map<Button::
        , _last_gain_position_written (-1.0)
        , _last_pan_azi_position_written (-1.0)
        , _last_pan_width_position_written (-1.0)
+       , redisplay_requests (256)
 {
        _fader = dynamic_cast<Fader*> (Fader::factory (*_surface, index, "fader", *this));
        _vpot = dynamic_cast<Pot*> (Pot::factory (*_surface, Pot::ID + index, "vpot", *this));
@@ -90,7 +108,7 @@ Strip::Strip (Surface& s, const std::string& name, int index, const map<Button::
        for (map<Button::ID,StripButtonInfo>::const_iterator b = strip_buttons.begin(); b != strip_buttons.end(); ++b) {
                Button* bb = dynamic_cast<Button*> (Button::factory (*_surface, b->first, b->second.base_id + index, b->second.name, *this));
                DEBUG_TRACE (DEBUG::MackieControl, string_compose ("surface %1 strip %2 new button BID %3 id %4 from base %5\n",
-                                                                  _surface->number(), index, Button::id_to_name (bb->bid()), 
+                                                                  _surface->number(), index, Button::id_to_name (bb->bid()),
                                                                   bb->id(), b->second.base_id));
        }
 }      
@@ -100,7 +118,7 @@ Strip::~Strip ()
        /* surface is responsible for deleting all controls */
 }
 
-void 
+void
 Strip::add (Control & control)
 {
        Button* button;
@@ -128,6 +146,7 @@ Strip::add (Control & control)
                        break;
                case Button::FaderTouch:
                        _fader_touch = button;
+                       break;
                default:
                        break;
                }
@@ -175,7 +194,7 @@ Strip::set_route (boost::shared_ptr<Route> r, bool /*with_messages*/)
 
        boost::shared_ptr<Pannable> pannable = _route->pannable();
 
-       if (pannable && pannable->panner()) {
+       if (pannable && _route->panner()) {
                pannable->pan_azimuth_control->Changed.connect(route_connections, MISSING_INVALIDATOR, boost::bind (&Strip::notify_panner_azi_changed, this, false), ui_context());
                pannable->pan_width_control->Changed.connect(route_connections, MISSING_INVALIDATOR, boost::bind (&Strip::notify_panner_width_changed, this, false), ui_context());
        }
@@ -209,7 +228,7 @@ Strip::set_route (boost::shared_ptr<Route> r, bool /*with_messages*/)
        possible_pot_parameters.clear();
 
        if (pannable) {
-               boost::shared_ptr<Panner> panner = pannable->panner();
+               boost::shared_ptr<Panner> panner = _route->panner();
                if (panner) {
                        set<Evoral::Parameter> automatable = panner->what_can_be_automated ();
                        set<Evoral::Parameter>::iterator a;
@@ -225,7 +244,7 @@ Strip::set_route (boost::shared_ptr<Route> r, bool /*with_messages*/)
        }
 }
 
-void 
+void
 Strip::notify_all()
 {
        if (!_route) {
@@ -242,7 +261,7 @@ Strip::notify_all()
        notify_record_enable_changed ();
 }
 
-void 
+void
 Strip::notify_solo_changed ()
 {
        if (_route && _solo) {
@@ -250,7 +269,7 @@ Strip::notify_solo_changed ()
        }
 }
 
-void 
+void
 Strip::notify_mute_changed ()
 {
        DEBUG_TRACE (DEBUG::MackieControl, string_compose ("Strip %1 mute changed\n", _index));
@@ -262,7 +281,7 @@ Strip::notify_mute_changed ()
        }
 }
 
-void 
+void
 Strip::notify_record_enable_changed ()
 {
        if (_route && _recenable)  {
@@ -270,49 +289,49 @@ Strip::notify_record_enable_changed ()
        }
 }
 
-void 
+void
 Strip::notify_active_changed ()
 {
        _surface->mcp().refresh_current_bank();
 }
 
-void 
+void
 Strip::notify_route_deleted ()
 {
        _surface->mcp().refresh_current_bank();
 }
 
-void 
+void
 Strip::notify_gain_changed (bool force_update)
 {
        if (_route) {
                
                Control* control;
 
-               if (_surface->mcp().flip_mode()) {
+               if (_surface->mcp().flip_mode() != MackieControlProtocol::Normal) {
                        control = _vpot;
                } else {
                        control = _fader;
                }
 
-
                boost::shared_ptr<AutomationControl> ac = _route->gain_control();
                
                float gain_coefficient = ac->get_value();
                float normalized_position = ac->internal_to_interface (gain_coefficient);
-               
+
+
                if (force_update || normalized_position != _last_gain_position_written) {
                        
-                       if (_surface->mcp().flip_mode()) {
+                       if (_surface->mcp().flip_mode() != MackieControlProtocol::Normal) {
                                if (!control->in_use()) {
                                        _surface->write (_vpot->set (normalized_position, true, Pot::wrap));
                                }
-                               do_parameter_display (GainAutomation, gain_coefficient);
+                               queue_parameter_display (GainAutomation, gain_coefficient);
                        } else {
                                if (!control->in_use()) {
                                        _surface->write (_fader->set_position (normalized_position));
                                }
-                               do_parameter_display (GainAutomation, gain_coefficient);
+                               queue_parameter_display (GainAutomation, gain_coefficient);
                        }
 
                        queue_display_reset (2000);
@@ -321,7 +340,7 @@ Strip::notify_gain_changed (bool force_update)
        }
 }
 
-void 
+void
 Strip::notify_property_changed (const PropertyChange& what_changed)
 {
        if (!what_changed.contains (ARDOUR::Properties::name)) {
@@ -337,12 +356,12 @@ Strip::notify_property_changed (const PropertyChange& what_changed)
                } else {
                        line1 = PBD::short_version (fullname, 6);
                }
-
+               
                _surface->write (display (0, line1));
        }
 }
 
-void 
+void
 Strip::notify_panner_azi_changed (bool force_update)
 {
        if (_route) {
@@ -351,7 +370,7 @@ Strip::notify_panner_azi_changed (bool force_update)
 
                boost::shared_ptr<Pannable> pannable = _route->pannable();
 
-               if (!pannable || !pannable->panner()) {
+               if (!pannable || !_route->panner()) {
                        _surface->write (_vpot->zero());
                        return;
                }
@@ -374,14 +393,14 @@ Strip::notify_panner_azi_changed (bool force_update)
                                _surface->write (_vpot->set (pos, true, Pot::dot));
                        }
                        
-                       do_parameter_display (PanAzimuthAutomation, pos);
+                       queue_parameter_display (PanAzimuthAutomation, pos);
                        queue_display_reset (2000);
                        _last_pan_azi_position_written = pos;
                }
        }
 }
 
-void 
+void
 Strip::notify_panner_width_changed (bool force_update)
 {
        if (_route) {
@@ -390,7 +409,7 @@ Strip::notify_panner_width_changed (bool force_update)
 
                boost::shared_ptr<Pannable> pannable = _route->pannable();
 
-               if (!pannable || !pannable->panner()) {
+               if (!pannable || !_route->panner()) {
                        _surface->write (_vpot->zero());
                        return;
                }
@@ -406,7 +425,7 @@ Strip::notify_panner_width_changed (bool force_update)
                
                if (force_update || pos != _last_pan_azi_position_written) {
                        
-                       if (_surface->mcp().flip_mode()) {
+                       if (_surface->mcp().flip_mode() != MackieControlProtocol::Normal) {
 
                                if (control == _fader) {
                                        if (!control->in_use()) {
@@ -418,7 +437,7 @@ Strip::notify_panner_width_changed (bool force_update)
                                _surface->write (_vpot->set (pos, true, Pot::spread));
                        }
                        
-                       do_parameter_display (PanWidthAutomation, pos);
+                       queue_parameter_display (PanWidthAutomation, pos);
                        queue_display_reset (2000);
                        _last_pan_azi_position_written = pos;
                }
@@ -432,7 +451,7 @@ Strip::select_event (Button&, ButtonState bs)
        
        if (bs == press) {
                
-               int ms = _surface->mcp().modifier_state();
+               int ms = _surface->mcp().main_modifier_state();
 
                if (ms & MackieControlProtocol::MODIFIER_CMDALT) {
                        _controls_locked = !_controls_locked;
@@ -465,7 +484,7 @@ Strip::vselect_event (Button&, ButtonState bs)
 {
        if (bs == press) {
 
-               int ms = _surface->mcp().modifier_state();
+               int ms = _surface->mcp().main_modifier_state();
                                
                if (ms & MackieControlProtocol::MODIFIER_SHIFT) {
 
@@ -495,17 +514,17 @@ Strip::fader_touch_event (Button&, ButtonState bs)
 
                boost::shared_ptr<AutomationControl> ac = _fader->control ();
 
-               if (_surface->mcp().modifier_state() == MackieControlProtocol::MODIFIER_SHIFT) {
+               if (_surface->mcp().main_modifier_state() & MackieControlProtocol::MODIFIER_SHIFT) {
                        if (ac) {
                                ac->set_value (ac->normal());
                        }
                } else {
-               
+
                        _fader->set_in_use (true);
                        _fader->start_touch (_surface->mcp().transport_frame());
                        
                        if (ac) {
-                               do_parameter_display ((AutomationType) ac->parameter().type(), ac->internal_to_interface (ac->get_value()));
+                               queue_parameter_display ((AutomationType) ac->parameter().type(), ac->internal_to_interface (ac->get_value()));
                                queue_display_reset (2000);
                        }
                }
@@ -552,7 +571,7 @@ Strip::handle_button (Button& button, ButtonState bs)
                                _surface->mcp().add_down_button ((AutomationType) control->parameter().type(), _surface->number(), _index);
                                
                                float new_value;
-                               int ms = _surface->mcp().modifier_state();
+                               int ms = _surface->mcp().main_modifier_state();
                                
                                if (ms & MackieControlProtocol::MODIFIER_SHIFT) {
                                        /* reset to default/normal value */
@@ -587,6 +606,17 @@ Strip::handle_button (Button& button, ButtonState bs)
        }
 }
 
+void
+Strip::queue_parameter_display (AutomationType type, float val)
+{
+       RedisplayRequest req;
+
+       req.type = type;
+       req.val = val;
+
+       redisplay_requests.write (&req, 1);
+}
+
 void
 Strip::do_parameter_display (AutomationType type, float val)
 {
@@ -605,8 +635,8 @@ Strip::do_parameter_display (AutomationType type, float val)
        case PanAzimuthAutomation:
                if (_route) {
                        boost::shared_ptr<Pannable> p = _route->pannable();
-                       if (p && p->panner()) {
-                               string str = p->panner()->value_as_string (p->pan_azimuth_control);
+                       if (p && _route->panner()) {
+                               string str =_route->panner()->value_as_string (p->pan_azimuth_control);
                                _surface->write (display (1, str));
                        }
                }
@@ -615,7 +645,7 @@ Strip::do_parameter_display (AutomationType type, float val)
        case PanWidthAutomation:
                if (_route) {
                        char buf[16];
-                       snprintf (buf, sizeof (buf), "%5ld%%", lrintf (val * 100.0));
+                       snprintf (buf, sizeof (buf), "%5ld%%", lrintf ((val * 200.0)-100));
                        _surface->write (display (1, buf));
                }
                break;
@@ -625,18 +655,32 @@ Strip::do_parameter_display (AutomationType type, float val)
        }
 }
 
+void
+Strip::handle_fader_touch (Fader& fader, bool touch_on)
+{
+       if (touch_on) {
+               fader.start_touch (_surface->mcp().transport_frame());
+       } else {
+               fader.stop_touch (_surface->mcp().transport_frame(), false);
+       }
+}
+
 void
 Strip::handle_fader (Fader& fader, float position)
 {
        DEBUG_TRACE (DEBUG::MackieControl, string_compose ("fader to %1\n", position));
 
        fader.set_value (position);
-       fader.start_touch (_surface->mcp().transport_frame());
-       queue_display_reset (2000);
 
-       // must echo bytes back to slider now, because
-       // the notifier only works if the fader is not being
-       // touched. Which it is if we're getting input.
+       /* From the Mackie Control MIDI implementation docs:
+
+          In order to ensure absolute synchronization with the host software,
+          Mackie Control uses a closed-loop servo system for the faders,
+          meaning the faders will always move to their last received position.
+          When a host receives a Fader Position Message, it must then
+          re-transmit that message to the Mackie Control or else the faders
+          will return to their last position.
+       */
 
        _surface->write (fader.set_position (position));
 }
@@ -671,7 +715,23 @@ Strip::periodic (uint64_t usecs)
        }
 }
 
-void 
+void
+Strip::redisplay ()
+{
+       RedisplayRequest req;
+       bool have_request = false;
+
+       while (redisplay_requests.read (&req, 1) == 1) {
+               /* read them all */
+               have_request = true;
+       }
+
+       if (have_request) {
+               do_parameter_display (req.type, req.val);
+       }
+}
+
+void
 Strip::update_automation ()
 {
        ARDOUR::AutoState gain_state = _route->gain_control()->automation_state();
@@ -693,7 +753,7 @@ void
 Strip::update_meter ()
 {
        if (_meter && _transport_is_rolling && _metering_active) {
-               float dB = const_cast<PeakMeter&> (_route->peak_meter()).peak_power (0);
+               float dB = const_cast<PeakMeter&> (_route->peak_meter()).meter_level (0, MeterMCP);
                _meter->send_update (*_surface, dB);
        }
 }
@@ -732,10 +792,16 @@ Strip::display (uint32_t line_number, const std::string& line)
        // offset (0 to 0x37 first line, 0x38 to 0x6f for second line)
        retval << (_index * 7 + (line_number * 0x38));
        
-       // ascii data to display
-       retval << line;
+       // ascii data to display. @param line is UTF-8
+       string ascii = Glib::convert_with_fallback (line, "UTF-8", "ISO-8859-1", "_");
+       string::size_type len = ascii.length();
+       if (len > 6) {
+               ascii = ascii.substr (0, 6);
+               len = 6;
+       }
+       retval << ascii;
        // pad with " " out to 6 chars
-       for (int i = line.length(); i < 6; ++i) {
+       for (int i = len; i < 6; ++i) {
                retval << ' ';
        }
        
@@ -860,7 +926,7 @@ Strip::reset_display ()
                
        clear_display_reset ();
 }
-                        
+                       
 struct RouteCompareByName {
        bool operator() (boost::shared_ptr<Route> a, boost::shared_ptr<Route> b) {
                return a->name().compare (b->name()) < 0;
@@ -943,7 +1009,7 @@ Strip::next_pot_mode ()
 {
        vector<Evoral::Parameter>::iterator i;
 
-       if (_surface->mcp().flip_mode()) {
+       if (_surface->mcp().flip_mode() != MackieControlProtocol::Normal) {
                /* do not change vpot mode while in flipped mode */
                DEBUG_TRACE (DEBUG::MackieControl, "not stepping pot mode - in flip mode\n");
                _surface->write (display (1, "Flip"));
@@ -996,7 +1062,7 @@ Strip::set_vpot_parameter (Evoral::Parameter p)
        case PanAzimuthAutomation:
                pannable = _route->pannable ();
                if (pannable) {
-                       if (_surface->mcp().flip_mode()) {
+                       if (_surface->mcp().flip_mode() != MackieControlProtocol::Normal) {
                                /* gain to vpot, pan azi to fader */
                                _vpot->set_control (_route->gain_control());
                                control_by_parameter[GainAutomation] = _vpot;
@@ -1024,7 +1090,7 @@ Strip::set_vpot_parameter (Evoral::Parameter p)
        case PanWidthAutomation:
                pannable = _route->pannable ();
                if (pannable) {
-                       if (_surface->mcp().flip_mode()) {
+                       if (_surface->mcp().flip_mode() != MackieControlProtocol::Normal) {
                                /* gain to vpot, pan width to fader */
                                _vpot->set_control (_route->gain_control());
                                control_by_parameter[GainAutomation] = _vpot;
@@ -1069,7 +1135,7 @@ Strip::reset_saved_values ()
 
 }
 
-void 
+void
 Strip::notify_metering_state_changed()
 {
        if (!_route || !_meter) {