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>
30 #include <midi++/port_request.h>
36 /* XXX check for strdup leaks */
38 Manager *Manager::theManager = 0;
45 inputChannelNumber = 0;
46 outputChannelNumber = 0;
54 for (i = ports_by_device.begin(); i != ports_by_device.end(); i++) {
58 ports_by_device.erase (ports_by_device.begin(), ports_by_device.end());
59 ports_by_tag.erase (ports_by_tag.begin(), ports_by_tag.end());
61 if (theManager == this) {
67 Manager::add_port (PortRequest &req)
72 PortMap::iterator existing;
73 pair<string, Port *> newpair;
75 if ((existing = ports_by_device.find (req.devname)) !=
76 ports_by_device.end()) {
78 port = (*existing).second;
79 if (port->mode() == req.mode) {
81 /* Same mode - reuse the port, and just
82 create a new tag entry.
85 newpair.first = req.tagname;
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 ((req.mode == O_RDWR && port->mode() != O_RDWR) ||
99 (req.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 (req);
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 (string name)
148 PortMap::iterator res;
150 if ((res = ports_by_device.find (name)) == ports_by_device.end()) {
154 ports_by_device.erase (res);
155 ports_by_device.erase ((*res).second->name());
157 delete (*res).second;
163 Manager::set_input_port (string tag)
165 PortMap::iterator res;
168 for (res = ports_by_tag.begin(); res != ports_by_tag.end(); res++) {
169 if (tag == (*res).first) {
179 inputPort = (*res).second;
185 Manager::set_input_port (size_t portnum)
188 PortMap::iterator res;
190 for (res = ports_by_tag.begin(); res != ports_by_tag.end(); res++) {
191 if ((*res).second->number() == portnum) {
192 inputPort = (*res).second;
201 Manager::set_output_port (string tag)
204 PortMap::iterator res;
207 for (res = ports_by_tag.begin(); res != ports_by_tag.end(); res++) {
208 if (tag == (*res).first) {
218 // XXX send a signal to say we're about to change output ports
221 for (channel_t chan = 0; chan < 16; chan++) {
222 outputPort->channel (chan)->all_notes_off ();
225 outputPort = (*res).second;
227 // XXX send a signal to say we've changed output ports
233 Manager::set_output_port (size_t portnum)
236 PortMap::iterator res;
238 for (res = ports_by_tag.begin(); res != ports_by_tag.end(); res++) {
239 if ((*res).second->number() == portnum) {
240 outputPort = (*res).second;
249 Manager::port (string name)
251 PortMap::iterator res;
253 for (res = ports_by_tag.begin(); res != ports_by_tag.end(); res++) {
254 if (name == (*res).first) {
255 return (*res).second;
263 Manager::port (size_t portnum)
266 PortMap::iterator res;
268 for (res = ports_by_tag.begin(); res != ports_by_tag.end(); res++) {
269 if ((*res).second->number() == portnum) {
270 return (*res).second;
278 Manager::foreach_port (int (*func)(const Port &, size_t, void *),
282 PortMap::const_iterator i;
286 for (n = 0, i = ports_by_device.begin();
287 i != ports_by_device.end(); i++, n++) {
289 if ((retval = func (*((*i).second), n, arg)) != 0) {
298 Manager::parse_port_request (string str, Port::Type type)
301 string::size_type colon;
304 if (str.length() == 0) {
305 error << "MIDI: missing port specification" << endmsg;
309 /* Port specifications look like:
313 devicename:tagname:mode
317 "devicename" is the full path to the requested file
319 "tagname" (optional) is the name used to refer to the
320 port. If not given, g_path_get_basename (devicename)
323 "mode" (optional) is either "r" or "w" or something else.
324 if it is "r", the port will be opened
325 read-only, if "w", the port will be opened
326 write-only. Any other value, or no mode
327 specification at all, will cause the port to
328 be opened for reading and writing.
331 req = new PortRequest;
332 colon = str.find_first_of (':');
334 if (colon != string::npos) {
335 req->devname = strdup (str.substr (0, colon).c_str());
337 req->devname = strdup (str.c_str());
340 if (colon < str.length()) {
342 tag = str.substr (colon+1);
344 /* see if there is a mode specification in the tag part */
346 colon = tag.find_first_of (':');
348 if (colon != string::npos) {
351 req->tagname = strdup (tag.substr (0, colon).c_str());
353 modestr = tag.substr (colon+1);
354 if (modestr == "r") {
355 req->mode = O_RDONLY;
356 } else if (modestr == "w") {
357 req->mode = O_WRONLY;
363 req->tagname = strdup (tag.c_str());
368 // check when tagname is freed
369 req->tagname = g_path_get_basename (req->devname);
375 if (MIDI::Manager::instance()->add_port (*req) == 0) {