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 (!PortFactory::ignore_duplicate_devices (desc.type)) {
75 if ((existing = ports_by_device.find (desc.device)) != ports_by_device.end()) {
77 port = (*existing).second;
79 if (port->mode() == desc.mode) {
81 /* Same mode - reuse the port, and just
82 create a new tag entry.
85 newpair.first = desc.tag;
86 newpair.second = port;
88 ports_by_tag.insert (newpair);
92 /* If the existing is duplex, and this request
93 is not, then fail, because most drivers won't
94 allow opening twice with duplex and non-duplex
98 if ((desc.mode == O_RDWR && port->mode() != O_RDWR) ||
99 (desc.mode != O_RDWR && port->mode() == O_RDWR)) {
100 error << "MIDIManager: port tagged \""
102 << "\" cannot be opened duplex and non-duplex"
107 /* modes must be different or complementary */
111 port = factory.create_port (node, api_data);
122 newpair.first = port->name();
123 newpair.second = port;
124 ports_by_tag.insert (newpair);
126 newpair.first = port->device();
127 newpair.second = port;
128 ports_by_device.insert (newpair);
130 /* first port added becomes the default input
134 if (inputPort == 0) {
138 if (outputPort == 0) {
146 Manager::remove_port (Port* port)
148 PortMap::iterator res;
150 for (res = ports_by_device.begin(); res != ports_by_device.end(); ) {
151 PortMap::iterator tmp;
154 if (res->second == port) {
155 ports_by_device.erase (res);
161 for (res = ports_by_tag.begin(); res != ports_by_tag.end(); ) {
162 PortMap::iterator tmp;
165 if (res->second == port) {
166 ports_by_tag.erase (res);
177 Manager::set_input_port (string tag)
179 PortMap::iterator res;
182 for (res = ports_by_tag.begin(); res != ports_by_tag.end(); res++) {
183 if (tag == (*res).first) {
193 inputPort = (*res).second;
199 Manager::set_output_port (string tag)
202 PortMap::iterator res;
205 for (res = ports_by_tag.begin(); res != ports_by_tag.end(); res++) {
206 if (tag == (*res).first) {
216 // XXX send a signal to say we're about to change output ports
219 for (channel_t chan = 0; chan < 16; chan++) {
220 outputPort->channel (chan)->all_notes_off (0);
223 outputPort = (*res).second;
225 // XXX send a signal to say we've changed output ports
231 Manager::port (string name)
233 PortMap::iterator res;
235 for (res = ports_by_tag.begin(); res != ports_by_tag.end(); res++) {
236 if (name == (*res).first) {
237 return (*res).second;
245 Manager::foreach_port (int (*func)(const Port &, size_t, void *),
248 PortMap::const_iterator i;
252 for (n = 0, i = ports_by_device.begin();
253 i != ports_by_device.end(); i++, n++) {
255 if ((retval = func (*((*i).second), n, arg)) != 0) {
264 Manager::cycle_start(nframes_t nframes)
266 for (PortMap::iterator i = ports_by_device.begin(); i != ports_by_device.end(); i++) {
267 (*i).second->cycle_start (nframes);
274 for (PortMap::iterator i = ports_by_device.begin(); i != ports_by_device.end(); i++) {
275 (*i).second->cycle_end ();
281 Manager::get_known_ports (vector<PortSet>& ports)
283 return PortFactory::get_known_ports (ports);