Merge branch 'master' into cairocanvas
[ardour.git] / libs / midi++2 / parser.cc
index 2efa77ae0b21b3fb70254135c286e196e4e73cde..7d31c9978166064e983fcc6b8f07e9971ec8b360 100644 (file)
@@ -104,9 +104,7 @@ Parser::midi_event_type_name (eventType t)
        }
 }
 
-Parser::Parser (Port &p) 
-       : _port (p)
-
+Parser::Parser () 
 {
        trace_stream = 0;
        trace_prefix = "";
@@ -138,7 +136,7 @@ Parser::~Parser ()
 }
 
 void
-Parser::trace_event (Parser &, byte *msg, size_t len)
+Parser::trace_event (Parser &, MIDI::byte *msg, size_t len)
 {
        eventType type;
        ostream *o;
@@ -313,10 +311,9 @@ Parser::trace (bool onoff, ostream *o, const string &prefix)
        trace_connection.disconnect ();
 
        if (onoff) {
-               cerr << "enabling tracing for port " << _port.name() << endl;
                trace_stream = o;
                trace_prefix = prefix;
-               any.connect (trace_connection, boost::bind (&Parser::trace_event, this, _1, _2, _3));
+               any.connect_same_thread (trace_connection, boost::bind (&Parser::trace_event, this, _1, _2, _3));
        } else {
                trace_prefix = "";
                trace_stream = 0;
@@ -327,9 +324,10 @@ void
 Parser::scanner (unsigned char inbyte)
 {
        bool statusbit;
+        boost::optional<int> edit_result;
 
        // cerr << "parse: " << hex << (int) inbyte << dec << " state = " << state << " msgindex = " << msgindex << " runnable = " << runnable << endl;
-
+       
        /* Check active sensing early, so it doesn't interrupt sysex. 
           
           NOTE: active sense messages are not considered to fit under
@@ -387,9 +385,11 @@ Parser::scanner (unsigned char inbyte)
        }
 
        if (rtmsg) {
-               if (edit (&inbyte, 1) >= 0 && !_offline) {
+               boost::optional<int> res = edit (&inbyte, 1);
+               
+               if (res.get_value_or (1) >= 0 && !_offline) {
                        realtime_msg (inbyte);
-               }
+               } 
                
                return;
        } 
@@ -402,8 +402,9 @@ Parser::scanner (unsigned char inbyte)
         * an EOX.  Actually, since EOX is a status byte, this
         * code ALWAYS handles the end of a VARIABLELENGTH message.
         */
-       
+
        if (state == VARIABLELENGTH && statusbit)  {
+
                /* The message has ended, so process it */
 
                /* add EOX to any sysex message */
@@ -419,16 +420,21 @@ Parser::scanner (unsigned char inbyte)
                }
                cerr << dec << endl;
 #endif
-               if (msgindex > 0 && edit (msgbuf, msgindex) >= 0) {
-                       if (!possible_mmc (msgbuf, msgindex) || _mmc_forward) {
-                               if (!possible_mtc (msgbuf, msgindex) || _mtc_forward) {
-                                       if (!_offline) {
-                                               sysex (*this, msgbuf, msgindex);
+               if (msgindex > 0) {
+
+                       boost::optional<int> res = edit (msgbuf, msgindex);
+
+                       if (res.get_value_or (1) >= 0) {
+                               if (!possible_mmc (msgbuf, msgindex) || _mmc_forward) {
+                                       if (!possible_mtc (msgbuf, msgindex) || _mtc_forward) {
+                                               if (!_offline) {
+                                                       sysex (*this, msgbuf, msgindex);
+                                               }
                                        }
                                }
-                       }
-                       if (!_offline) {
-                               any (*this, msgbuf, msgindex);
+                               if (!_offline) {
+                                       any (*this, msgbuf, msgindex);
+                               } 
                        }
                }
        }
@@ -490,8 +496,10 @@ Parser::scanner (unsigned char inbyte)
                
        case NEEDONEBYTE:
                /* We've completed a 1 or 2 byte message. */
-               
-               if (edit (msgbuf, msgindex) == 0) {
+
+                edit_result = edit (msgbuf, msgindex);
+                
+               if (edit_result.get_value_or (1)) {
                        
                        /* message not cancelled by an editor */
                        
@@ -650,7 +658,7 @@ Parser::system_msg (unsigned char inbyte)
 }
 
 void 
-Parser::signal (byte *msg, size_t len)
+Parser::signal (MIDI::byte *msg, size_t len)
 {
        channel_t chan = msg[0]&0xF;
        int chan_i = chan;
@@ -753,7 +761,7 @@ Parser::signal (byte *msg, size_t len)
 }
 
 bool
-Parser::possible_mmc (byte *msg, size_t msglen)
+Parser::possible_mmc (MIDI::byte *msg, size_t msglen)
 {
        if (!MachineControl::is_mmc (msg, msglen)) {
                return false;