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;
45 for (i = ports_by_device.begin(); i != ports_by_device.end(); i++) {
49 ports_by_device.erase (ports_by_device.begin(), ports_by_device.end());
50 ports_by_tag.erase (ports_by_tag.begin(), ports_by_tag.end());
52 if (theManager == this) {
58 Manager::add_port (PortRequest &req)
63 PortMap::iterator existing;
64 pair<string, Port *> newpair;
66 if ((existing = ports_by_device.find (req.devname)) !=
67 ports_by_device.end()) {
69 port = (*existing).second;
70 if (port->mode() == req.mode) {
72 /* Same mode - reuse the port, and just
73 create a new tag entry.
76 newpair.first = req.tagname;
77 newpair.second = port;
79 ports_by_tag.insert (newpair);
83 /* If the existing is duplex, and this request
84 is not, then fail, because most drivers won't
85 allow opening twice with duplex and non-duplex
89 if ((req.mode == O_RDWR && port->mode() != O_RDWR) ||
90 (req.mode != O_RDWR && port->mode() == O_RDWR)) {
91 error << "MIDIManager: port tagged \""
93 << "\" cannot be opened duplex and non-duplex"
98 /* modes must be different or complementary */
101 port = factory.create_port (req, api_data);
112 newpair.first = port->name();
113 newpair.second = port;
114 ports_by_tag.insert (newpair);
116 newpair.first = port->device();
117 newpair.second = port;
118 ports_by_device.insert (newpair);
124 Manager::remove_port (string name)
126 PortMap::iterator res;
128 if ((res = ports_by_device.find (name)) == ports_by_device.end()) {
132 ports_by_device.erase (res);
133 ports_by_device.erase ((*res).second->name());
135 delete (*res).second;
141 Manager::port (string name)
143 PortMap::iterator res;
145 for (res = ports_by_tag.begin(); res != ports_by_tag.end(); res++) {
146 if (name == (*res).first) {
147 return (*res).second;
155 Manager::port (size_t portnum)
158 PortMap::iterator res;
160 for (res = ports_by_tag.begin(); res != ports_by_tag.end(); res++) {
161 if ((*res).second->number() == portnum) {
162 return (*res).second;
170 Manager::foreach_port (int (*func)(const Port &, size_t, void *),
174 PortMap::const_iterator i;
178 for (n = 0, i = ports_by_device.begin();
179 i != ports_by_device.end(); i++, n++) {
181 if ((retval = func (*((*i).second), n, arg)) != 0) {
190 Manager::parse_port_request (string str, Port::Type type)
193 string::size_type colon;
196 if (str.length() == 0) {
197 error << "MIDI: missing port specification" << endmsg;
201 /* Port specifications look like:
205 devicename:tagname:mode
209 "devicename" is the full path to the requested file
211 "tagname" (optional) is the name used to refer to the
212 port. If not given, PBD::basename (devicename)
215 "mode" (optional) is either "r" or "w" or something else.
216 if it is "r", the port will be opened
217 read-only, if "w", the port will be opened
218 write-only. Any other value, or no mode
219 specification at all, will cause the port to
220 be opened for reading and writing.
223 req = new PortRequest;
224 colon = str.find_first_of (':');
226 if (colon != string::npos) {
227 req->devname = strdup (str.substr (0, colon).c_str());
229 req->devname = strdup (str.c_str());
232 if (colon < str.length()) {
234 tag = str.substr (colon+1);
236 /* see if there is a mode specification in the tag part */
238 colon = tag.find_first_of (':');
240 if (colon != string::npos) {
243 req->tagname = strdup (tag.substr (0, colon).c_str());
245 modestr = tag.substr (colon+1);
246 if (modestr == "r") {
247 req->mode = O_RDONLY;
248 } else if (modestr == "w") {
249 req->mode = O_WRONLY;
255 req->tagname = strdup (tag.c_str());
260 req->tagname = strdup (PBD::basename (req->devname));
266 if (MIDI::Manager::instance()->add_port (*req) == 0) {
274 Manager::cycle_start(nframes_t nframes)
276 for (PortMap::iterator i = ports_by_device.begin();
277 i != ports_by_device.end(); i++)
278 (*i).second->cycle_start(nframes);
284 for (PortMap::iterator i = ports_by_device.begin();
285 i != ports_by_device.end(); i++)
286 (*i).second->cycle_end();