summaryrefslogtreecommitdiff
path: root/libs/midi++2/port.cc
blob: c4ae0abdbf03750108a281dfbc1cc774fab45160 (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
/*
 * Copyright (C) 1998-2015 Paul Davis <paul@linuxaudiosystems.com>
 * Copyright (C) 2009-2011 Carl Hetherington <carl@carlh.net>
 * Copyright (C) 2015 Robin Gareus <robin@gareus.org>
 *
 * 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.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */
#include <iostream>
#include <cstdio>
#include <fcntl.h>
#include <errno.h>

#include "pbd/xml++.h"
#include "pbd/error.h"
#include "pbd/failed_constructor.h"
#include "pbd/convert.h"
#include "pbd/strsplit.h"
#include "pbd/stacktrace.h"

#include "midi++/types.h"
#include "midi++/port.h"
#include "midi++/channel.h"

using namespace MIDI;
using namespace std;
using namespace PBD;

string Port::state_node_name = "MIDI-port";

Port::Port (string const & name, Flags flags)
	: _flags (flags)
	, _centrally_parsed (true)
{
	init (name, flags);
}

Port::Port (const XMLNode& node)
	: _centrally_parsed (true)
{
	Descriptor desc (node);

	init (desc.tag, desc.flags);

	/* derived class must call ::set_state() */
}

void
Port::init (string const & name, Flags flags)
{
	_ok = false;  /* derived class must set to true if constructor
			 succeeds.
		      */

	_parser = 0;

	_tagname = name;
	_flags = flags;

	_parser = new Parser ();

	for (int i = 0; i < 16; i++) {
		_channel[i] = new Channel (i, *this);
		_channel[i]->connect_signals ();
	}
}

Port::~Port ()
{
	for (int i = 0; i < 16; i++) {
		delete _channel[i];
	}

	delete _parser;
}

/** Send a clock tick message.
 * \return true on success.
 */
bool
Port::clock (timestamp_t timestamp)
{
	static byte clockmsg = 0xf8;

	if (sends_output()) {
		return midimsg (&clockmsg, 1, timestamp);
	}

	return false;
}

std::ostream & MIDI::operator << ( std::ostream & os, const MIDI::Port & port )
{
	using namespace std;
	os << "MIDI::Port { ";
	os << "name: " << port.name();
	os << "; ";
	os << "ok: " << port.ok();
	os << "; ";
	os << " }";
	return os;
}

Port::Descriptor::Descriptor (const XMLNode& node)
{
	const XMLProperty *prop;
	bool have_tag = false;
	bool have_mode = false;

	if ((prop = node.property ("tag")) != 0) {
		tag = prop->value();
		have_tag = true;
	}

	if ((prop = node.property ("mode")) != 0) {

		if (strings_equal_ignore_case (prop->value(), "output") || strings_equal_ignore_case (prop->value(), "out")) {
			flags = IsOutput;
		} else if (strings_equal_ignore_case (prop->value(), "input") || strings_equal_ignore_case (prop->value(), "in")) {
			flags = IsInput;
		}

		have_mode = true;
	}

	if (!have_tag || !have_mode) {
		throw failed_constructor();
	}
}

XMLNode&
Port::get_state () const
{
	XMLNode* root = new XMLNode (state_node_name);
	root->set_property ("tag", _tagname);

	if (_flags == IsInput) {
		root->set_property ("mode", "input");
	} else {
		root->set_property ("mode", "output");
	}

#if 0
	byte device_inquiry[6];

	device_inquiry[0] = 0xf0;
	device_inquiry[0] = 0x7e;
	device_inquiry[0] = 0x7f;
	device_inquiry[0] = 0x06;
	device_inquiry[0] = 0x02;
	device_inquiry[0] = 0xf7;

	write (device_inquiry, sizeof (device_inquiry), 0);
#endif

	return *root;
}

void
Port::set_state (const XMLNode& node)
{
	const XMLProperty* prop;

	if ((prop = node.property ("tag")) == 0 || prop->value() != _tagname) {
		return;
	}
}

bool
Port::centrally_parsed() const
{
	return _centrally_parsed;
}