2 Copyright (C) 1998-99 Paul Barton-Davis
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; either version 2 of the License, or
6 (at your option) any later version.
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
13 You should have received a copy of the GNU General Public License
14 along with this program; if not, write to the Free Software
15 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include "pbd/error.h"
26 #include "midi++/types.h"
27 #include "midi++/manager.h"
28 #include "midi++/factory.h"
29 #include "midi++/channel.h"
35 /* XXX check for strdup leaks */
37 Manager *Manager::theManager = 0;
43 inputChannelNumber = 0;
44 outputChannelNumber = 0;
52 for (i = ports_by_device.begin(); i != ports_by_device.end(); i++) {
56 ports_by_device.erase (ports_by_device.begin(), ports_by_device.end());
57 ports_by_tag.erase (ports_by_tag.begin(), ports_by_tag.end());
59 if (theManager == this) {
65 Manager::add_port (const XMLNode& node)
67 Port::Descriptor desc (node);
70 PortMap::iterator existing;
71 pair<string, Port *> newpair;
73 if ((existing = ports_by_tag.find (desc.tag)) != ports_by_tag.end()) {
75 port = (*existing).second;
77 if (port->mode() == desc.mode) {
79 /* Same mode - reuse the port, and just
80 create a new tag entry.
83 newpair.first = desc.tag;
84 newpair.second = port;
86 ports_by_tag.insert (newpair);
90 /* If the existing is duplex, and this request
91 is not, then fail, because most drivers won't
92 allow opening twice with duplex and non-duplex
96 if ((desc.mode == O_RDWR && port->mode() != O_RDWR) ||
97 (desc.mode != O_RDWR && port->mode() == O_RDWR)) {
98 error << "MIDIManager: port tagged \""
100 << "\" cannot be opened duplex and non-duplex"
105 /* modes must be different or complementary */
108 if (!PortFactory::ignore_duplicate_devices (desc.type)) {
110 if ((existing = ports_by_device.find (desc.device)) != ports_by_device.end()) {
112 port = (*existing).second;
114 if (port->mode() == desc.mode) {
116 /* Same mode - reuse the port, and just
117 create a new tag entry.
120 newpair.first = desc.tag;
121 newpair.second = port;
123 ports_by_tag.insert (newpair);
127 /* If the existing is duplex, and this request
128 is not, then fail, because most drivers won't
129 allow opening twice with duplex and non-duplex
133 if ((desc.mode == O_RDWR && port->mode() != O_RDWR) ||
134 (desc.mode != O_RDWR && port->mode() == O_RDWR)) {
135 error << "MIDIManager: port tagged \""
137 << "\" cannot be opened duplex and non-duplex"
142 /* modes must be different or complementary */
146 port = factory.create_port (node, api_data);
158 newpair.first = port->name();
159 newpair.second = port;
160 ports_by_tag.insert (newpair);
162 newpair.first = port->device();
163 newpair.second = port;
164 ports_by_device.insert (newpair);
166 /* first port added becomes the default input
170 if (inputPort == 0) {
174 if (outputPort == 0) {
182 Manager::remove_port (Port* port)
184 PortMap::iterator res;
186 for (res = ports_by_device.begin(); res != ports_by_device.end(); ) {
187 PortMap::iterator tmp;
190 if (res->second == port) {
191 ports_by_device.erase (res);
197 for (res = ports_by_tag.begin(); res != ports_by_tag.end(); ) {
198 PortMap::iterator tmp;
201 if (res->second == port) {
202 ports_by_tag.erase (res);
213 Manager::set_input_port (string tag)
215 PortMap::iterator res;
218 for (res = ports_by_tag.begin(); res != ports_by_tag.end(); res++) {
219 if (tag == (*res).first) {
229 inputPort = (*res).second;
235 Manager::set_output_port (string tag)
238 PortMap::iterator res;
241 for (res = ports_by_tag.begin(); res != ports_by_tag.end(); res++) {
242 if (tag == (*res).first) {
252 // XXX send a signal to say we're about to change output ports
255 for (channel_t chan = 0; chan < 16; chan++) {
256 outputPort->channel (chan)->all_notes_off (0);
259 outputPort = (*res).second;
261 // XXX send a signal to say we've changed output ports
267 Manager::port (string name)
269 PortMap::iterator res;
271 for (res = ports_by_tag.begin(); res != ports_by_tag.end(); res++) {
272 if (name == (*res).first) {
273 return (*res).second;
281 Manager::foreach_port (int (*func)(const Port &, size_t, void *),
284 PortMap::const_iterator i;
288 for (n = 0, i = ports_by_device.begin();
289 i != ports_by_device.end(); i++, n++) {
291 if ((retval = func (*((*i).second), n, arg)) != 0) {
300 Manager::cycle_start(nframes_t nframes)
302 for (PortMap::iterator i = ports_by_device.begin(); i != ports_by_device.end(); i++) {
303 (*i).second->cycle_start (nframes);
310 for (PortMap::iterator i = ports_by_device.begin(); i != ports_by_device.end(); i++) {
311 (*i).second->cycle_end ();
317 Manager::get_known_ports (vector<PortSet>& ports)
319 return PortFactory::get_known_ports (ports);