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;
44 inputChannelNumber = 0;
45 outputChannelNumber = 0;
53 for (i = ports_by_device.begin(); i != ports_by_device.end(); i++) {
57 ports_by_device.erase (ports_by_device.begin(), ports_by_device.end());
58 ports_by_tag.erase (ports_by_tag.begin(), ports_by_tag.end());
60 if (theManager == this) {
66 Manager::add_port (const XMLNode& node)
68 Port::Descriptor desc (node);
71 PortMap::iterator existing;
72 pair<string, Port *> newpair;
74 if (!PortFactory::ignore_duplicate_devices (desc.type)) {
76 if ((existing = ports_by_device.find (desc.device)) != ports_by_device.end()) {
78 port = (*existing).second;
80 if (port->mode() == desc.mode) {
82 /* Same mode - reuse the port, and just
83 create a new tag entry.
86 newpair.first = desc.tag;
87 newpair.second = port;
89 ports_by_tag.insert (newpair);
93 /* If the existing is duplex, and this request
94 is not, then fail, because most drivers won't
95 allow opening twice with duplex and non-duplex
99 if ((desc.mode == O_RDWR && port->mode() != O_RDWR) ||
100 (desc.mode != O_RDWR && port->mode() == O_RDWR)) {
101 error << "MIDIManager: port tagged \""
103 << "\" cannot be opened duplex and non-duplex"
108 /* modes must be different or complementary */
112 port = factory.create_port (node);
123 newpair.first = port->name();
124 newpair.second = port;
125 ports_by_tag.insert (newpair);
127 newpair.first = port->device();
128 newpair.second = port;
129 ports_by_device.insert (newpair);
131 /* first port added becomes the default input
135 if (inputPort == 0) {
139 if (outputPort == 0) {
147 Manager::remove_port (Port* port)
149 PortMap::iterator res;
151 for (res = ports_by_device.begin(); res != ports_by_device.end(); ) {
152 PortMap::iterator tmp;
155 if (res->second == port) {
156 ports_by_device.erase (res);
162 for (res = ports_by_tag.begin(); res != ports_by_tag.end(); ) {
163 PortMap::iterator tmp;
166 if (res->second == port) {
167 ports_by_tag.erase (res);
178 Manager::set_input_port (string tag)
180 PortMap::iterator res;
183 for (res = ports_by_tag.begin(); res != ports_by_tag.end(); res++) {
184 if (tag == (*res).first) {
194 inputPort = (*res).second;
200 Manager::set_output_port (string tag)
203 PortMap::iterator res;
206 for (res = ports_by_tag.begin(); res != ports_by_tag.end(); res++) {
207 if (tag == (*res).first) {
217 // XXX send a signal to say we're about to change output ports
220 for (channel_t chan = 0; chan < 16; chan++) {
221 outputPort->channel (chan)->all_notes_off ();
224 outputPort = (*res).second;
226 // XXX send a signal to say we've changed output ports
232 Manager::port (string name)
234 PortMap::iterator res;
236 for (res = ports_by_tag.begin(); res != ports_by_tag.end(); res++) {
237 if (name == (*res).first) {
238 return (*res).second;
246 Manager::foreach_port (int (*func)(const Port &, size_t, void *),
249 PortMap::const_iterator i;
253 for (n = 0, i = ports_by_device.begin();
254 i != ports_by_device.end(); i++, n++) {
256 if ((retval = func (*((*i).second), n, arg)) != 0) {
266 Manager::get_known_ports (vector<PortSet>& ports)
268 return PortFactory::get_known_ports (ports);