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 Manager *Manager::theManager = 0;
48 for (i = ports_by_device.begin(); i != ports_by_device.end(); i++) {
52 ports_by_device.erase (ports_by_device.begin(), ports_by_device.end());
53 ports_by_tag.erase (ports_by_tag.begin(), ports_by_tag.end());
55 if (theManager == this) {
61 Manager::add_port (PortRequest &req)
66 PortMap::iterator existing;
67 pair<string, Port *> newpair;
69 if (!PortFactory::ignore_duplicate_devices (req.type)) {
71 if ((existing = ports_by_device.find (req.devname)) != ports_by_device.end()) {
73 port = (*existing).second;
75 if (port->mode() == req.mode) {
77 /* Same mode - reuse the port, and just
78 create a new tag entry.
81 newpair.first = req.tagname;
82 newpair.second = port;
84 ports_by_tag.insert (newpair);
88 /* If the existing is duplex, and this request
89 is not, then fail, because most drivers won't
90 allow opening twice with duplex and non-duplex
94 if ((req.mode == O_RDWR && port->mode() != O_RDWR) ||
95 (req.mode != O_RDWR && port->mode() == O_RDWR)) {
96 error << "MIDIManager: port tagged \""
98 << "\" cannot be opened duplex and non-duplex"
103 /* modes must be different or complementary */
106 port = factory.create_port (req, api_data);
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);
129 Manager::remove_port (Port* port)
131 PortMap::iterator res;
133 for (res = ports_by_device.begin(); res != ports_by_device.end(); ) {
134 PortMap::iterator tmp;
137 if (res->second == port) {
138 ports_by_device.erase (res);
144 for (res = ports_by_tag.begin(); res != ports_by_tag.end(); ) {
145 PortMap::iterator tmp;
148 if (res->second == port) {
149 ports_by_tag.erase (res);
160 Manager::port (string name)
162 PortMap::iterator res;
164 for (res = ports_by_tag.begin(); res != ports_by_tag.end(); res++) {
165 if (name == (*res).first) {
166 return (*res).second;
174 Manager::foreach_port (int (*func)(const Port &, size_t, void *),
178 PortMap::const_iterator i;
182 for (n = 0, i = ports_by_device.begin();
183 i != ports_by_device.end(); i++, n++) {
185 if ((retval = func (*((*i).second), n, arg)) != 0) {
194 Manager::parse_port_request (string str, Port::Type type)
197 string::size_type colon;
200 if (str.length() == 0) {
201 error << "MIDI: missing port specification" << endmsg;
205 /* Port specifications look like:
209 devicename:tagname:mode
213 "devicename" is the full path to the requested file
215 "tagname" (optional) is the name used to refer to the
216 port. If not given, g_path_get_basename (devicename)
219 "mode" (optional) is either "r" or "w" or something else.
220 if it is "r", the port will be opened
221 read-only, if "w", the port will be opened
222 write-only. Any other value, or no mode
223 specification at all, will cause the port to
224 be opened for reading and writing.
227 req = new PortRequest;
228 colon = str.find_first_of (':');
230 if (colon != string::npos) {
231 req->devname = strdup (str.substr (0, colon).c_str());
233 req->devname = strdup (str.c_str());
236 if (colon < str.length()) {
238 tag = str.substr (colon+1);
240 /* see if there is a mode specification in the tag part */
242 colon = tag.find_first_of (':');
244 if (colon != string::npos) {
247 req->tagname = strdup (tag.substr (0, colon).c_str());
249 modestr = tag.substr (colon+1);
250 if (modestr == "r") {
251 req->mode = O_RDONLY;
252 } else if (modestr == "w") {
253 req->mode = O_WRONLY;
259 req->tagname = strdup (tag.c_str());
264 req->tagname = g_path_get_basename (req->devname);
270 if (MIDI::Manager::instance()->add_port (*req) == 0) {
278 Manager::cycle_start(nframes_t nframes)
280 for (PortMap::iterator i = ports_by_device.begin();
281 i != ports_by_device.end(); i++)
282 (*i).second->cycle_start(nframes);
288 for (PortMap::iterator i = ports_by_device.begin();
289 i != ports_by_device.end(); i++)
290 (*i).second->cycle_end();