2 Copyright (C) 2006,2007 John Anderson
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 #include "surface_port.h"
20 #include "mackie_control_exception.h"
23 #include "midi++/types.h"
24 #include "midi++/port.h"
25 #include "midi++/manager.h"
26 #include <sigc++/sigc++.h>
27 #include <boost/shared_array.hpp>
37 using namespace Mackie;
39 SurfacePort::SurfacePort()
40 : _input_port (0), _output_port (0), _number (0), _active (false)
44 /** @param input_port Input MIDI::Port; this object takes responsibility for removing it from
45 * the MIDI::Manager and destroying it.
46 * @param output_port Output MIDI::Port; responsibility similarly taken.
48 SurfacePort::SurfacePort (MIDI::Port & input_port, MIDI::Port & output_port, int number)
49 : _input_port (&input_port), _output_port (&output_port), _number (number), _active (false)
53 SurfacePort::~SurfacePort()
56 cout << "~SurfacePort::SurfacePort()" << endl;
58 // make sure another thread isn't reading or writing as we close the port
59 Glib::RecMutex::Lock lock (_rwlock);
62 MIDI::Manager* mm = MIDI::Manager::instance ();
65 mm->remove_port (_input_port);
70 mm->remove_port (_output_port);
75 cout << "~SurfacePort::SurfacePort() finished" << endl;
79 // wrapper for one day when strerror_r is working properly
80 string fetch_errmsg (int error_number)
82 char * msg = strerror (error_number);
86 MidiByteArray SurfacePort::read()
88 const int max_buf_size = 512;
89 MIDI::byte buf[max_buf_size];
92 // check active. Mainly so that the destructor
93 // doesn't destroy the mutex while it's still locked
98 // return nothing read if the lock isn't acquired
100 Glib::RecMutex::Lock lock (_rwlock, Glib::TRY_LOCK);
102 if (!lock.locked()) {
103 cout << "SurfacePort::read not locked" << endl;
107 // check active again - destructor sequence
108 if (!active()) return retval;
111 // read port and copy to return value
112 int nread = input_port().read (buf, sizeof (buf));
115 retval.copy (nread, buf);
116 if ((size_t) nread == sizeof (buf)) {
118 cout << "SurfacePort::read recursive" << endl;
123 if (errno != EAGAIN) {
125 os << "Surface: error reading from port: " << input_port().name();
126 os << ": " << errno << fetch_errmsg (errno);
128 cout << os.str() << endl;
130 throw MackieControlException (os.str());
134 cout << "SurfacePort::read: " << retval << endl;
139 void SurfacePort::write (const MidiByteArray & mba)
146 cout << "SurfacePort::write: " << mba << " to " << output_port().name() << endl;
149 // check active before and after lock - to make sure
150 // that the destructor doesn't destroy the mutex while
152 if (!active()) return;
153 Glib::RecMutex::Lock lock (_rwlock);
154 if (!active()) return;
156 int count = output_port().write (mba.bytes().get(), mba.size(), 0);
157 if (count != (int)mba.size()) {
159 cout << "port overflow on " << output_port().name() << ". Did not write all of " << mba << endl;
160 } else if (errno != EAGAIN) {
162 os << "Surface: couldn't write to port " << output_port().name();
163 os << ", error: " << fetch_errmsg (errno) << "(" << errno << ")";
165 cout << os.str() << endl;
170 cout << "SurfacePort::wrote " << count << endl;
174 void SurfacePort::write_sysex (const MidiByteArray & mba)
181 buf << sysex_hdr() << mba << MIDI::eox;
185 void SurfacePort::write_sysex (MIDI::byte msg)
188 buf << sysex_hdr() << msg << MIDI::eox;
192 ostream & Mackie::operator << (ostream & os, const SurfacePort & port)
195 os << "name: " << port.input_port().name() << " " << port.output_port().name();