enough with umpteen "i18n.h" files. Consolidate on pbd/i18n.h
[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 "pbd/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                 string in_name;
62                 string out_name;
63
64                 if (_surface->mcp().device_info().extenders() > 0) {
65                         if (_surface->number() == _surface->mcp().device_info().master_position()) {
66                                 in_name = X_("mackie control in");
67                                 out_name = X_("mackie control out");
68                         } else {
69                                 in_name = string_compose (X_("mackie control in ext %1"), (_surface->number() + 1));
70                                 out_name = string_compose (X_("mackie control out ext %1"), _surface->number() + 1);
71                         }
72                 } else {
73                         in_name = X_("mackie control in");
74                         out_name = X_("mackie control out");
75                 }
76
77                 _async_in  = AudioEngine::instance()->register_input_port (DataType::MIDI, in_name, true);
78                 _async_out = AudioEngine::instance()->register_output_port (DataType::MIDI, out_name, true);
79
80                 if (_async_in == 0 || _async_out == 0) {
81                         throw failed_constructor();
82                 }
83
84                 _input_port = boost::dynamic_pointer_cast<AsyncMIDIPort>(_async_in).get();
85                 _output_port = boost::dynamic_pointer_cast<AsyncMIDIPort>(_async_out).get();
86         }
87 }
88
89 SurfacePort::~SurfacePort()
90 {
91         if (dynamic_cast<MIDI::IPMIDIPort*>(_input_port)) {
92                 delete _input_port;
93                 _input_port = 0;
94         } else {
95                 if (_async_in) {
96                         DEBUG_TRACE (DEBUG::MackieControl, string_compose ("unregistering input port %1\n", _async_in->name()));
97                         AudioEngine::instance()->unregister_port (_async_in);
98                         _async_in.reset ((ARDOUR::Port*) 0);
99                 }
100
101                 if (_async_out) {
102                         _output_port->drain (10000, 250000);
103                         DEBUG_TRACE (DEBUG::MackieControl, string_compose ("unregistering output port %1\n", _async_out->name()));
104                         AudioEngine::instance()->unregister_port (_async_out);
105                         _async_out.reset ((ARDOUR::Port*) 0);
106                 }
107         }
108 }
109
110 XMLNode&
111 SurfacePort::get_state ()
112 {
113         XMLNode* node = new XMLNode (X_("Port"));
114
115         if (dynamic_cast<MIDI::IPMIDIPort*>(_input_port)) {
116                 /* no state required for IPMidi ports */
117                 return *node;
118         }
119
120         XMLNode* child;
121
122         child = new XMLNode (X_("Input"));
123         child->add_child_nocopy (_async_in->get_state());
124         node->add_child_nocopy (*child);
125
126
127         child = new XMLNode (X_("Output"));
128         child->add_child_nocopy (_async_out->get_state());
129         node->add_child_nocopy (*child);
130
131         return *node;
132 }
133
134 int
135 SurfacePort::set_state (const XMLNode& node, int version)
136 {
137         if (dynamic_cast<MIDI::IPMIDIPort*>(_input_port)) {
138                 return 0;
139         }
140
141         XMLNode* child;
142
143         if ((child = node.child (X_("Input"))) != 0) {
144                 XMLNode* portnode = child->child (Port::state_node_name.c_str());
145                 if (portnode) {
146                         _async_in->set_state (*portnode, version);
147                 }
148         }
149
150         if ((child = node.child (X_("Output"))) != 0) {
151                 XMLNode* portnode = child->child (Port::state_node_name.c_str());
152                 if (portnode) {
153                         _async_out->set_state (*portnode, version);
154                 }
155         }
156
157         return 0;
158 }
159
160 void
161 SurfacePort::reconnect ()
162 {
163         _async_out->reconnect ();
164         _async_in->reconnect ();
165 }
166
167 std::string
168 SurfacePort::input_name () const
169 {
170         return _async_in->name();
171 }
172
173 std::string
174 SurfacePort::output_name () const
175 {
176         return _async_out->name();
177 }
178
179 // wrapper for one day when strerror_r is working properly
180 string fetch_errmsg (int error_number)
181 {
182         char * msg = strerror (error_number);
183         return msg;
184 }
185
186 int
187 SurfacePort::write (const MidiByteArray & mba)
188 {
189         if (mba.empty()) {
190                 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("port %1 asked to write an empty MBA\n", output_port().name()));
191                 return 0;
192         }
193
194         DEBUG_TRACE (DEBUG::MackieControl, string_compose ("port %1 write %2\n", output_port().name(), mba));
195
196         if (mba[0] != 0xf0 && mba.size() > 3) {
197                 std::cerr << "TOO LONG WRITE: " << mba << std::endl;
198         }
199
200         /* this call relies on std::vector<T> using contiguous storage. not
201          * actually guaranteed by the standard, but way, way beyond likely.
202          */
203
204         int count = output_port().write (&mba[0], mba.size(), 0);
205
206         if  (count != (int) mba.size()) {
207
208                 if (errno == 0) {
209
210                         cout << "port overflow on " << output_port().name() << ". Did not write all of " << mba << endl;
211
212                 } else if  (errno != EAGAIN) {
213                         ostringstream os;
214                         os << "Surface: couldn't write to port " << output_port().name();
215                         os << ", error: " << fetch_errmsg (errno) << "(" << errno << ")";
216                         cout << os.str() << endl;
217                 }
218
219                 return -1;
220         }
221
222         return 0;
223 }
224
225 ostream &
226 Mackie::operator <<  (ostream & os, const SurfacePort & port)
227 {
228         os << "{ ";
229         os << "name: " << port.input_port().name() << " " << port.output_port().name();
230         os << "; ";
231         os << " }";
232         return os;
233 }