part-way through getting the audioengine changes to compile
[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++/port.h"
28 #include "midi++/jack_midi_port.h"
29 #include "midi++/ipmidi_port.h"
30 #include "midi++/manager.h"
31
32 #include "ardour/debug.h"
33 #include "ardour/rc_configuration.h"
34 #include "ardour/session.h"
35 #include "ardour/audioengine.h"
36
37 #include "controls.h"
38 #include "mackie_control_protocol.h"
39 #include "surface.h"
40 #include "surface_port.h"
41
42
43 #include "i18n.h"
44
45 using namespace std;
46 using namespace Mackie;
47 using namespace PBD;
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         , _input_port (0)
56         , _output_port (0)
57 {
58         if (_surface->mcp().device_info().uses_ipmidi()) {
59                 _input_port = new MIDI::IPMIDIPort (_surface->mcp().ipmidi_base() +_surface->number());
60                 _output_port = _input_port;
61         } else {
62                 ARDOUR::PortEngine& port_engine (ARDOUR::AudioEngine::instance()->port_engine());
63                 
64                 _input_port = new MIDI::JackMIDIPort (string_compose (_("%1 in"),  _surface->name()), MIDI::Port::IsInput, port_engine);
65                 _output_port =new MIDI::JackMIDIPort (string_compose (_("%1 out"), _surface->name()), MIDI::Port::IsOutput, port_engine);
66                 
67                 /* MackieControl has its own thread for handling input from the input
68                  * port, and we don't want anything handling output from the output
69                  * port. This stops the Generic MIDI UI event loop in ardour from
70                  * attempting to handle these ports.
71                  */
72                 
73                 _input_port->set_centrally_parsed (false);
74                 _output_port->set_centrally_parsed (false);
75                 
76                 MIDI::Manager * mm = MIDI::Manager::instance();
77                 
78                 mm->add_port (_input_port);
79                 mm->add_port (_output_port);
80         }
81 }
82
83 SurfacePort::~SurfacePort()
84 {
85         if (_surface->mcp().device_info().uses_ipmidi()) {
86                 delete _input_port;
87         } else {
88
89                 MIDI::Manager* mm = MIDI::Manager::instance ();
90                 
91                 if (_input_port) {
92                         mm->remove_port (_input_port);
93                         delete _input_port;
94                 }
95                 
96                 if (_output_port) {
97                         _output_port->drain (10000);
98                         mm->remove_port (_output_port);
99                         delete _output_port;
100                 }
101         }
102 }
103
104 // wrapper for one day when strerror_r is working properly
105 string fetch_errmsg (int error_number)
106 {
107         char * msg = strerror (error_number);
108         return msg;
109 }
110         
111 int 
112 SurfacePort::write (const MidiByteArray & mba)
113 {
114         if (mba.empty()) {
115                 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("port %1 asked to write an empty MBA\n", output_port().name()));
116                 return 0;
117         }
118
119         DEBUG_TRACE (DEBUG::MackieControl, string_compose ("port %1 write %2\n", output_port().name(), mba));
120
121         if (mba[0] != 0xf0 && mba.size() > 3) {
122                 std::cerr << "TOO LONG WRITE: " << mba << std::endl;
123         }
124                 
125         /* this call relies on std::vector<T> using contiguous storage. not
126          * actually guaranteed by the standard, but way, way beyond likely.
127          */
128
129         int count = output_port().write (&mba[0], mba.size(), 0);
130
131         if  (count != (int) mba.size()) {
132
133                 if (errno == 0) {
134
135                         cout << "port overflow on " << output_port().name() << ". Did not write all of " << mba << endl;
136
137                 } else if  (errno != EAGAIN) {
138                         ostringstream os;
139                         os << "Surface: couldn't write to port " << output_port().name();
140                         os << ", error: " << fetch_errmsg (errno) << "(" << errno << ")";
141                         cout << os.str() << endl;
142                 }
143
144                 return -1;
145         }
146
147         return 0;
148 }
149
150 ostream & 
151 Mackie::operator <<  (ostream & os, const SurfacePort & port)
152 {
153         os << "{ ";
154         os << "name: " << port.input_port().name() << " " << port.output_port().name();
155         os << "; ";
156         os << " }";
157         return os;
158 }
159