put all of the Mackie control surface code into the ArdourSurface namespace
[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 "pbd/failed_constructor.h"
27
28 #include "midi++/types.h"
29 #include "midi++/ipmidi_port.h"
30
31 #include "ardour/async_midi_port.h"
32 #include "ardour/debug.h"
33 #include "ardour/rc_configuration.h"
34 #include "ardour/session.h"
35 #include "ardour/audioengine.h"
36 #include "ardour/async_midi_port.h"
37 #include "ardour/midiport_manager.h"
38
39 #include "controls.h"
40 #include "mackie_control_protocol.h"
41 #include "surface.h"
42 #include "surface_port.h"
43
44 #include "i18n.h"
45
46 using namespace std;
47 using namespace PBD;
48 using namespace ARDOUR;
49 using namespace ArdourSurface;
50 using namespace Mackie;
51
52 SurfacePort::SurfacePort (Surface& s)
53         : _surface (&s)
54 {
55         if (_surface->mcp().device_info().uses_ipmidi()) {
56                 _input_port = new MIDI::IPMIDIPort (_surface->mcp().ipmidi_base() +_surface->number());
57                 _output_port = _input_port;
58
59         } else {
60                 
61                 _async_in  = AudioEngine::instance()->register_input_port (DataType::MIDI, string_compose (_("%1 in"),  _surface->name()), true);
62                 _async_out = AudioEngine::instance()->register_output_port (DataType::MIDI, string_compose (_("%1 out"),  _surface->name()), true);
63
64                 if (_async_in == 0 || _async_out == 0) {
65                         throw failed_constructor();
66                 }
67
68                 _input_port = boost::dynamic_pointer_cast<AsyncMIDIPort>(_async_in).get();
69                 _output_port = boost::dynamic_pointer_cast<AsyncMIDIPort>(_async_out).get();
70         }
71 }
72
73 SurfacePort::~SurfacePort()
74 {
75         if (dynamic_cast<MIDI::IPMIDIPort*>(_input_port)) {
76                 delete _input_port;
77         } else {
78
79                 if (_async_in) {
80                         AudioEngine::instance()->unregister_port (_async_in);
81                         _async_in.reset ((ARDOUR::Port*) 0);
82                 }
83                 
84                 if (_async_out) {
85                         _output_port->drain (10000);
86                         AudioEngine::instance()->unregister_port (_async_out);
87                         _async_out.reset ((ARDOUR::Port*) 0);
88                 }
89         }
90 }
91
92 XMLNode&
93 SurfacePort::get_state ()
94 {
95         XMLNode* node = new XMLNode (X_("Port"));
96
97         if (dynamic_cast<MIDI::IPMIDIPort*>(_input_port)) {
98                 /* no state required for IPMidi ports */
99                 return *node;
100         }
101
102         XMLNode* child;
103
104         child = new XMLNode (X_("Input"));
105         child->add_child_nocopy (_async_in->get_state());
106         node->add_child_nocopy (*child);
107         
108
109         child = new XMLNode (X_("Output"));
110         child->add_child_nocopy (_async_out->get_state());
111         node->add_child_nocopy (*child);
112
113         return *node;
114 }
115
116 int
117 SurfacePort::set_state (const XMLNode& node, int version)
118 {
119         if (dynamic_cast<MIDI::IPMIDIPort*>(_input_port)) {
120                 return 0;
121         }
122
123         XMLNode* child;
124
125         if ((child = node.child (X_("Input"))) != 0) {
126                 XMLNode* portnode = child->child (Port::state_node_name.c_str());
127                 if (portnode) {
128                         _async_in->set_state (*portnode, version);
129                 }
130         }
131
132         if ((child = node.child (X_("Output"))) != 0) {
133                 XMLNode* portnode = child->child (Port::state_node_name.c_str());
134                 if (portnode) {
135                         _async_out->set_state (*portnode, version);
136                 }
137         }
138
139         return 0;
140 }
141
142 // wrapper for one day when strerror_r is working properly
143 string fetch_errmsg (int error_number)
144 {
145         char * msg = strerror (error_number);
146         return msg;
147 }
148         
149 int 
150 SurfacePort::write (const MidiByteArray & mba)
151 {
152         if (mba.empty()) {
153                 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("port %1 asked to write an empty MBA\n", output_port().name()));
154                 return 0;
155         }
156
157         DEBUG_TRACE (DEBUG::MackieControl, string_compose ("port %1 write %2\n", output_port().name(), mba));
158
159         if (mba[0] != 0xf0 && mba.size() > 3) {
160                 std::cerr << "TOO LONG WRITE: " << mba << std::endl;
161         }
162                 
163         /* this call relies on std::vector<T> using contiguous storage. not
164          * actually guaranteed by the standard, but way, way beyond likely.
165          */
166
167         int count = output_port().write (&mba[0], mba.size(), 0);
168
169         if  (count != (int) mba.size()) {
170
171                 if (errno == 0) {
172
173                         cout << "port overflow on " << output_port().name() << ". Did not write all of " << mba << endl;
174
175                 } else if  (errno != EAGAIN) {
176                         ostringstream os;
177                         os << "Surface: couldn't write to port " << output_port().name();
178                         os << ", error: " << fetch_errmsg (errno) << "(" << errno << ")";
179                         cout << os.str() << endl;
180                 }
181
182                 return -1;
183         }
184
185         return 0;
186 }
187
188 ostream & 
189 Mackie::operator <<  (ostream & os, const SurfacePort & port)
190 {
191         os << "{ ";
192         os << "name: " << port.input_port().name() << " " << port.output_port().name();
193         os << "; ";
194         os << " }";
195         return os;
196 }
197