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