Merge branch 'master' into saveas
[ardour.git] / libs / midi++2 / mmc.cc
index 8adfcafb85cc7b234ff994a29f062effcc5b43ef..d722a816eef104932a99669b875aed6bfd09de0b 100644 (file)
     $Id$
 */
 
+#include <fcntl.h>
 #include <map>
 
-#include "control_protocol/timecode.h"
+#include "timecode/time.h"
+#include "timecode/bbt_time.h"
+
 #include "pbd/error.h"
+
 #include "midi++/mmc.h"
 #include "midi++/port.h"
 #include "midi++/parser.h"
 
+#ifndef __INT_MAX__   // 'ssize_t' won't be defined yet
+typedef long ssize_t;
+#endif
+
 using namespace std;
 using namespace MIDI;
 using namespace PBD;
 
-pthread_t MachineControl::_sending_thread;
+/**
+ * As libtimecode is linked statically to libmidi++ this
+ * is necessary to pull in all the symbols from libtimecode
+ * so they are exported for other users of libtimecode.
+ */
+double tmp = Timecode::BBT_Time::ticks_per_beat;
 
 static std::map<int,string> mmc_cmd_map;
 static void build_mmc_cmd_map ()
@@ -194,45 +207,42 @@ static void build_mmc_cmd_map ()
        mmc_cmd_map.insert (newpair);
 }
 
-
 MachineControl::MachineControl ()
-       : _port (0)
-       , _pending (16)
 {
        build_mmc_cmd_map ();
 
-       _receive_device_id = 0;
+       _receive_device_id = 0x7f;
        _send_device_id = 0x7f;
 }
 
 void
-MachineControl::set_port (Port* p)
+MachineControl::set_ports (MIDI::Port* ip, MIDI::Port* op)
 {
-       _port = p;
+       port_connections.drop_connections ();
 
-       mmc_connection.disconnect ();
+       _input_port = ip;
+       _output_port = op;
 
-       if (_port->input()) {
-               _port->input()->mmc.connect_same_thread (mmc_connection, boost::bind (&MachineControl::process_mmc_message, this, _1, _2, _3));
-       } else {
-               warning << "MMC connected to a non-input port: useless!" << endmsg;
-       }
+       _input_port->parser()->mmc.connect_same_thread (port_connections, boost::bind (&MachineControl::process_mmc_message, this, _1, _2, _3));
+       _input_port->parser()->start.connect_same_thread (port_connections, boost::bind (&MachineControl::spp_start, this));
+       _input_port->parser()->contineu.connect_same_thread (port_connections, boost::bind (&MachineControl::spp_continue, this));
+       _input_port->parser()->stop.connect_same_thread (port_connections, boost::bind (&MachineControl::spp_stop, this));
 }
 
 void
-MachineControl::set_receive_device_id (byte id)
+MachineControl::set_receive_device_id (MIDI::byte id)
 {
        _receive_device_id = id & 0x7f;
 }
 
 void
-MachineControl::set_send_device_id (byte id)
+MachineControl::set_send_device_id (MIDI::byte id)
 {
        _send_device_id = id & 0x7f;
 }
 
 bool
-MachineControl::is_mmc (byte *sysex_buf, size_t len)
+MachineControl::is_mmc (MIDI::byte *sysex_buf, size_t len)
 {
        if (len < 4 || len > 48) {
                return false;
@@ -251,7 +261,7 @@ MachineControl::is_mmc (byte *sysex_buf, size_t len)
 }
 
 void
-MachineControl::process_mmc_message (Parser &, byte *msg, size_t len)
+MachineControl::process_mmc_message (Parser &, MIDI::byte *msg, size_t len)
 {
        size_t skiplen;
        byte *mmc_msg;
@@ -274,7 +284,7 @@ MachineControl::process_mmc_message (Parser &, byte *msg, size_t len)
        cerr << endl;
 #endif
 
-       if (msg[1] != 0x7f && msg[1] != _receive_device_id) {
+       if (_receive_device_id != 0x7f && msg[1] != 0x7f && msg[1] != _receive_device_id) {
                return;
        }
 
@@ -458,7 +468,7 @@ MachineControl::process_mmc_message (Parser &, byte *msg, size_t len)
 }              
 
 int
-MachineControl::do_masked_write (byte *msg, size_t len)
+MachineControl::do_masked_write (MIDI::byte *msg, size_t len)
 {
        /* return the number of bytes "consumed" */
 
@@ -484,7 +494,7 @@ MachineControl::do_masked_write (byte *msg, size_t len)
 }
 
 void
-MachineControl::write_track_status (byte *msg, size_t /*len*/, byte reg)
+MachineControl::write_track_status (MIDI::byte *msg, size_t /*len*/, MIDI::byte reg)
 {
        size_t n;
        ssize_t base_track;
@@ -573,7 +583,7 @@ MachineControl::write_track_status (byte *msg, size_t /*len*/, byte reg)
 }
 
 int
-MachineControl::do_locate (byte *msg, size_t /*msglen*/)
+MachineControl::do_locate (MIDI::byte *msg, size_t /*msglen*/)
 {
        if (msg[2] == 0) {
                warning << "MIDI::MMC: locate [I/F] command not supported"
@@ -588,7 +598,7 @@ MachineControl::do_locate (byte *msg, size_t /*msglen*/)
 }
 
 int
-MachineControl::do_step (byte *msg, size_t /*msglen*/)
+MachineControl::do_step (MIDI::byte *msg, size_t /*msglen*/)
 {
        int steps = msg[2] & 0x3f;
 
@@ -601,7 +611,7 @@ MachineControl::do_step (byte *msg, size_t /*msglen*/)
 }
 
 int
-MachineControl::do_shuttle (byte *msg, size_t /*msglen*/)
+MachineControl::do_shuttle (MIDI::byte *msg, size_t /*msglen*/)
 {
        size_t forward;
        byte sh = msg[2];
@@ -637,38 +647,13 @@ MachineControl::enable_send (bool yn)
        _enable_send = yn;
 }
 
-/** Send a MMC command.  It will be sent immediately if the call is made in _sending_thread,
- *  otherwise it will be queued and sent next time flush_pending()
- *  is called.
- *  @param c command, which this method takes ownership of.
+/** Send a MMC command to a the MMC port.
+ *  @param c command.
  */
 void
-MachineControl::send (MachineControlCommand const & c)
+MachineControl::send (MachineControlCommand const & c, timestamp_t when)
 {
-       if (pthread_self() == _sending_thread) {
-               send_immediately (c);
-       } else {
-               _pending.write (&c, 1);
-       }
-}
-
-/** Send any pending MMC commands immediately.  Must be called from _sending_thread */
-void
-MachineControl::flush_pending ()
-{
-       MachineControlCommand c;
-       while (_pending.read (&c, 1) == 1) {
-               send_immediately (c);
-       }
-}
-
-/** Send a MMC immediately.  Must be called from _sending_thread.
- *  @param c command, which this method takes ownership of.
- */
-void
-MachineControl::send_immediately (MachineControlCommand const & c)
-{
-       if (_port == 0 || !_enable_send) {
+       if (_output_port == 0 || !_enable_send) {
                // cerr << "Not delivering MMC " << _mmc->port() << " - " << session_send_mmc << endl;
                return;
        }
@@ -676,16 +661,27 @@ MachineControl::send_immediately (MachineControlCommand const & c)
        MIDI::byte buffer[32];
        MIDI::byte* b = c.fill_buffer (this, buffer);
 
-       if (_port->midimsg (buffer, b - buffer, 0)) {
+       if (_output_port->midimsg (buffer, b - buffer, when)) {
                error << "MMC: cannot send command" << endmsg;
        }
 }
 
-/** Set the thread that we should send MMC in */
 void
-MachineControl::set_sending_thread (pthread_t t)
+MachineControl::spp_start ()
+{
+       SPPStart (); /* EMIT SIGNAL */
+}
+
+void
+MachineControl::spp_continue ()
 {
-       _sending_thread = t;
+       SPPContinue (); /* EMIT SIGNAL */
+}
+
+void
+MachineControl::spp_stop ()
+{
+       SPPStop (); /* EMIT SIGNAL */
 }
 
 MachineControlCommand::MachineControlCommand (MachineControl::Command c)
@@ -714,7 +710,7 @@ MachineControlCommand::fill_buffer (MachineControl* mmc, MIDI::byte* b) const
        if (_command == MachineControl::cmdLocate) {
                *b++ = 0x6; // byte count
                *b++ = 0x1; // "TARGET" subcommand
-               *b++ = _time.hours;
+               *b++ = _time.hours % 24;
                *b++ = _time.minutes;
                *b++ = _time.seconds;
                *b++ = _time.frames;
@@ -725,3 +721,4 @@ MachineControlCommand::fill_buffer (MachineControl* mmc, MIDI::byte* b) const
 
        return b;
 }
+