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.
21 #include <pbd/error.h>
22 #include <pbd/basename.h>
24 #include <midi++/types.h>
25 #include <midi++/manager.h>
26 #include <midi++/factory.h>
27 #include <midi++/channel.h>
28 #include <midi++/port_request.h>
33 Manager *Manager::theManager = 0;
40 inputChannelNumber = 0;
41 outputChannelNumber = 0;
49 for (i = ports_by_device.begin(); i != ports_by_device.end(); i++) {
53 ports_by_device.erase (ports_by_device.begin(), ports_by_device.end());
54 ports_by_tag.erase (ports_by_tag.begin(), ports_by_tag.end());
56 if (theManager == this) {
62 Manager::add_port (PortRequest &req)
67 PortMap::iterator existing;
68 pair<string, Port *> newpair;
70 if ((existing = ports_by_device.find (req.devname)) !=
71 ports_by_device.end()) {
73 port = (*existing).second;
74 if (port->mode() == req.mode) {
76 /* Same mode - reuse the port, and just
77 create a new tag entry.
80 newpair.first = req.tagname;
81 newpair.second = port;
83 ports_by_tag.insert (newpair);
87 /* If the existing is duplex, and this request
88 is not, then fail, because most drivers won't
89 allow opening twice with duplex and non-duplex
93 if ((req.mode == O_RDWR && port->mode() != O_RDWR) ||
94 (req.mode != O_RDWR && port->mode() == O_RDWR)) {
95 error << "MIDIManager: port tagged \""
97 << "\" cannot be opened duplex and non-duplex"
102 /* modes must be different or complementary */
106 port = factory.create_port (req);
117 newpair.first = port->name();
118 newpair.second = port;
119 ports_by_tag.insert (newpair);
121 newpair.first = port->device();
122 newpair.second = port;
123 ports_by_device.insert (newpair);
125 /* first port added becomes the default input
129 if (inputPort == 0) {
133 if (outputPort == 0) {
141 Manager::remove_port (string name)
143 PortMap::iterator res;
145 if ((res = ports_by_device.find (name)) == ports_by_device.end()) {
149 ports_by_device.erase (res);
150 ports_by_device.erase ((*res).second->name());
152 delete (*res).second;
158 Manager::set_input_port (string tag)
160 PortMap::iterator res;
163 for (res = ports_by_tag.begin(); res != ports_by_tag.end(); res++) {
164 if (tag == (*res).first) {
174 inputPort = (*res).second;
180 Manager::set_input_port (size_t portnum)
183 PortMap::iterator res;
185 for (res = ports_by_tag.begin(); res != ports_by_tag.end(); res++) {
186 if ((*res).second->number() == portnum) {
187 inputPort = (*res).second;
196 Manager::set_output_port (string tag)
199 PortMap::iterator res;
202 for (res = ports_by_tag.begin(); res != ports_by_tag.end(); res++) {
203 if (tag == (*res).first) {
213 // XXX send a signal to say we're about to change output ports
216 for (channel_t chan = 0; chan < 16; chan++) {
217 outputPort->channel (chan)->all_notes_off ();
220 outputPort = (*res).second;
222 // XXX send a signal to say we've changed output ports
228 Manager::set_output_port (size_t portnum)
231 PortMap::iterator res;
233 for (res = ports_by_tag.begin(); res != ports_by_tag.end(); res++) {
234 if ((*res).second->number() == portnum) {
235 outputPort = (*res).second;
244 Manager::port (string name)
246 PortMap::iterator res;
248 for (res = ports_by_tag.begin(); res != ports_by_tag.end(); res++) {
249 if (name == (*res).first) {
250 return (*res).second;
258 Manager::port (size_t portnum)
261 PortMap::iterator res;
263 for (res = ports_by_tag.begin(); res != ports_by_tag.end(); res++) {
264 if ((*res).second->number() == portnum) {
265 return (*res).second;
273 Manager::foreach_port (int (*func)(const Port &, size_t, void *),
277 PortMap::const_iterator i;
281 for (n = 0, i = ports_by_device.begin();
282 i != ports_by_device.end(); i++, n++) {
284 if ((retval = func (*((*i).second), n, arg)) != 0) {
293 Manager::parse_port_request (string str, Port::Type type)
296 string::size_type colon;
299 if (str.length() == 0) {
300 error << "MIDI: missing port specification" << endmsg;
304 /* Port specifications look like:
308 devicename:tagname:mode
312 "devicename" is the full path to the requested file
314 "tagname" (optional) is the name used to refer to the
315 port. If not given, PBD::basename (devicename)
318 "mode" (optional) is either "r" or "w" or something else.
319 if it is "r", the port will be opened
320 read-only, if "w", the port will be opened
321 write-only. Any other value, or no mode
322 specification at all, will cause the port to
323 be opened for reading and writing.
326 req = new PortRequest;
327 colon = str.find_first_of (':');
329 if (colon != string::npos) {
330 req->devname = strdup (str.substr (0, colon).c_str());
332 req->devname = strdup (str.c_str());
335 if (colon < str.length()) {
337 tag = str.substr (colon+1);
339 /* see if there is a mode specification in the tag part */
341 colon = tag.find_first_of (':');
343 if (colon != string::npos) {
346 req->tagname = strdup (tag.substr (0, colon).c_str());
348 modestr = tag.substr (colon+1);
349 if (modestr == "r") {
350 req->mode = O_RDONLY;
351 } else if (modestr == "w") {
352 req->mode = O_WRONLY;
358 req->tagname = strdup (tag.c_str());
363 req->tagname = strdup (PBD::basename (req->devname));
369 if (MIDI::Manager::instance()->add_port (*req) == 0) {