Merge branch 'master' into audioengine
[ardour.git] / libs / surfaces / mackie / surface_port.cc
1 /*
2         Copyright (C) 2006,2007 John Anderson
3
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.
8
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.
13
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.
17 */
18
19 #include <sstream>
20 #include <cstring>
21 #include <cerrno>
22
23 #include <sigc++/sigc++.h>
24 #include <boost/shared_array.hpp>
25
26 #include "midi++/types.h"
27 #include "midi++/ipmidi_port.h"
28
29 #include "ardour/async_midi_port.h"
30 #include "ardour/debug.h"
31 #include "ardour/rc_configuration.h"
32 #include "ardour/session.h"
33 #include "ardour/audioengine.h"
34 #include "ardour/async_midi_port.h"
35 #include "ardour/midiport_manager.h"
36
37 #include "controls.h"
38 #include "mackie_control_protocol.h"
39 #include "surface.h"
40 #include "surface_port.h"
41
42 #include "i18n.h"
43
44 using namespace std;
45 using namespace Mackie;
46 using namespace PBD;
47 using namespace ARDOUR;
48
49 /** @param input_port Input MIDI::Port; this object takes responsibility for
50  *  adding & removing it from the MIDI::Manager and destroying it.  @param
51  *  output_port Output MIDI::Port; responsibility similarly taken.
52  */
53 SurfacePort::SurfacePort (Surface& s)
54         : _surface (&s)
55 {
56         if (_surface->mcp().device_info().uses_ipmidi()) {
57                 _input_port = new MIDI::IPMIDIPort (_surface->mcp().ipmidi_base() +_surface->number());
58                 _output_port = _input_port;
59         } else {
60                 _async_in  = AudioEngine::instance()->register_input_port (DataType::MIDI, string_compose (_("%1 in"),  _surface->name()), true);
61                 _async_out = AudioEngine::instance()->register_output_port (DataType::MIDI, string_compose (_("%1 out"),  _surface->name()), true);
62
63                 _input_port = boost::dynamic_pointer_cast<AsyncMIDIPort>(_async_in).get();
64                 _output_port = boost::dynamic_pointer_cast<AsyncMIDIPort>(_async_out).get();
65         }
66 }
67
68 SurfacePort::~SurfacePort()
69 {
70         if (_surface->mcp().device_info().uses_ipmidi()) {
71                 delete _input_port;
72         } else {
73
74                 if (_async_in) {
75                         AudioEngine::instance()->unregister_port (_async_in);
76                         _async_in.reset ();
77                 }
78                 
79                 if (_async_out) {
80                         _output_port->drain (10000);
81                         AudioEngine::instance()->unregister_port (_async_out);
82                         _async_out.reset ();
83                 }
84         }
85 }
86
87 // wrapper for one day when strerror_r is working properly
88 string fetch_errmsg (int error_number)
89 {
90         char * msg = strerror (error_number);
91         return msg;
92 }
93         
94 int 
95 SurfacePort::write (const MidiByteArray & mba)
96 {
97         if (mba.empty()) {
98                 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("port %1 asked to write an empty MBA\n", output_port().name()));
99                 return 0;
100         }
101
102         DEBUG_TRACE (DEBUG::MackieControl, string_compose ("port %1 write %2\n", output_port().name(), mba));
103
104         if (mba[0] != 0xf0 && mba.size() > 3) {
105                 std::cerr << "TOO LONG WRITE: " << mba << std::endl;
106         }
107                 
108         /* this call relies on std::vector<T> using contiguous storage. not
109          * actually guaranteed by the standard, but way, way beyond likely.
110          */
111
112         int count = output_port().write (&mba[0], mba.size(), 0);
113
114         if  (count != (int) mba.size()) {
115
116                 if (errno == 0) {
117
118                         cout << "port overflow on " << output_port().name() << ". Did not write all of " << mba << endl;
119
120                 } else if  (errno != EAGAIN) {
121                         ostringstream os;
122                         os << "Surface: couldn't write to port " << output_port().name();
123                         os << ", error: " << fetch_errmsg (errno) << "(" << errno << ")";
124                         cout << os.str() << endl;
125                 }
126
127                 return -1;
128         }
129
130         return 0;
131 }
132
133 ostream & 
134 Mackie::operator <<  (ostream & os, const SurfacePort & port)
135 {
136         os << "{ ";
137         os << "name: " << port.input_port().name() << " " << port.output_port().name();
138         os << "; ";
139         os << " }";
140         return os;
141 }
142