summaryrefslogtreecommitdiff
path: root/libs/surfaces/mackie/surface_port.cc
blob: 3129b5a1fd4303d1de4280d7ceda78d639fe7057 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*
	Copyright (C) 2006,2007 John Anderson

	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 2 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program; if not, write to the Free Software
	Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include <sstream>
#include <cstring>
#include <cerrno>

#include <sigc++/sigc++.h>
#include <boost/shared_array.hpp>

#include "pbd/failed_constructor.h"

#include "midi++/types.h"
#include "midi++/ipmidi_port.h"

#include "ardour/async_midi_port.h"
#include "ardour/debug.h"
#include "ardour/rc_configuration.h"
#include "ardour/session.h"
#include "ardour/audioengine.h"
#include "ardour/async_midi_port.h"
#include "ardour/midiport_manager.h"

#include "controls.h"
#include "mackie_control_protocol.h"
#include "surface.h"
#include "surface_port.h"

#include "i18n.h"

using namespace std;
using namespace PBD;
using namespace ARDOUR;
using namespace ArdourSurface;
using namespace Mackie;

SurfacePort::SurfacePort (Surface& s)
	: _surface (&s)
{
	if (_surface->mcp().device_info().uses_ipmidi()) {
		_input_port = new MIDI::IPMIDIPort (_surface->mcp().ipmidi_base() +_surface->number());
		_output_port = _input_port;

	} else {

		_async_in  = AudioEngine::instance()->register_input_port (DataType::MIDI, string_compose (_("%1 in"),  _surface->name()), true);
		_async_out = AudioEngine::instance()->register_output_port (DataType::MIDI, string_compose (_("%1 out"),  _surface->name()), true);

		if (_async_in == 0 || _async_out == 0) {
			throw failed_constructor();
		}

		_input_port = boost::dynamic_pointer_cast<AsyncMIDIPort>(_async_in).get();
		_output_port = boost::dynamic_pointer_cast<AsyncMIDIPort>(_async_out).get();
	}
}

SurfacePort::~SurfacePort()
{
	if (dynamic_cast<MIDI::IPMIDIPort*>(_input_port)) {
		delete _input_port;
		_input_port = 0;
	} else {
		if (_async_in) {
			DEBUG_TRACE (DEBUG::MackieControl, string_compose ("unregistering input port %1\n", _async_in->name()));
			AudioEngine::instance()->unregister_port (_async_in);
			_async_in.reset ((ARDOUR::Port*) 0);
		}

		if (_async_out) {
			_output_port->drain (10000);
			DEBUG_TRACE (DEBUG::MackieControl, string_compose ("unregistering output port %1\n", _async_out->name()));
			AudioEngine::instance()->unregister_port (_async_out);
			_async_out.reset ((ARDOUR::Port*) 0);
		}
	}
}

XMLNode&
SurfacePort::get_state ()
{
	XMLNode* node = new XMLNode (X_("Port"));

	if (dynamic_cast<MIDI::IPMIDIPort*>(_input_port)) {
		/* no state required for IPMidi ports */
		return *node;
	}

	XMLNode* child;

	child = new XMLNode (X_("Input"));
	child->add_child_nocopy (_async_in->get_state());
	node->add_child_nocopy (*child);


	child = new XMLNode (X_("Output"));
	child->add_child_nocopy (_async_out->get_state());
	node->add_child_nocopy (*child);

	return *node;
}

int
SurfacePort::set_state (const XMLNode& node, int version)
{
	if (dynamic_cast<MIDI::IPMIDIPort*>(_input_port)) {
		return 0;
	}
	// the rest should not be run if the device-name changes outside of a session load.
	if ( _surface->mcp().session_load()) {

		XMLNode* child;

		if ((child = node.child (X_("Input"))) != 0) {
			XMLNode* portnode = child->child (Port::state_node_name.c_str());
			if (portnode) {
				_async_in->set_state (*portnode, version);
			}
		}

		if ((child = node.child (X_("Output"))) != 0) {
			XMLNode* portnode = child->child (Port::state_node_name.c_str());
			if (portnode) {
				_async_out->set_state (*portnode, version);
			}
		}
	}

	return 0;
}

// wrapper for one day when strerror_r is working properly
string fetch_errmsg (int error_number)
{
	char * msg = strerror (error_number);
	return msg;
}

int
SurfacePort::write (const MidiByteArray & mba)
{
	if (mba.empty()) {
		DEBUG_TRACE (DEBUG::MackieControl, string_compose ("port %1 asked to write an empty MBA\n", output_port().name()));
		return 0;
	}

	DEBUG_TRACE (DEBUG::MackieControl, string_compose ("port %1 write %2\n", output_port().name(), mba));

	if (mba[0] != 0xf0 && mba.size() > 3) {
		std::cerr << "TOO LONG WRITE: " << mba << std::endl;
	}

	/* this call relies on std::vector<T> using contiguous storage. not
	 * actually guaranteed by the standard, but way, way beyond likely.
	 */

	int count = output_port().write (&mba[0], mba.size(), 0);

	if  (count != (int) mba.size()) {

		if (errno == 0) {

			cout << "port overflow on " << output_port().name() << ". Did not write all of " << mba << endl;

		} else if  (errno != EAGAIN) {
			ostringstream os;
			os << "Surface: couldn't write to port " << output_port().name();
			os << ", error: " << fetch_errmsg (errno) << "(" << errno << ")";
			cout << os.str() << endl;
		}

		return -1;
	}

	return 0;
}

ostream &
Mackie::operator <<  (ostream & os, const SurfacePort & port)
{
	os << "{ ";
	os << "name: " << port.input_port().name() << " " << port.output_port().name();
	os << "; ";
	os << " }";
	return os;
}