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 ((existing = ports_by_device.find (req.devname)) !=
70 ports_by_device.end()) {
72 port = (*existing).second;
73 if (port->mode() == req.mode) {
75 /* Same mode - reuse the port, and just
76 create a new tag entry.
79 newpair.first = req.tagname;
80 newpair.second = port;
82 ports_by_tag.insert (newpair);
86 /* If the existing is duplex, and this request
87 is not, then fail, because most drivers won't
88 allow opening twice with duplex and non-duplex
92 if ((req.mode == O_RDWR && port->mode() != O_RDWR) ||
93 (req.mode != O_RDWR && port->mode() == O_RDWR)) {
94 error << "MIDIManager: port tagged \""
96 << "\" cannot be opened duplex and non-duplex"
101 /* modes must be different or complementary */
104 port = factory.create_port (req, api_data);
115 newpair.first = port->name();
116 newpair.second = port;
117 ports_by_tag.insert (newpair);
119 newpair.first = port->device();
120 newpair.second = port;
121 ports_by_device.insert (newpair);
127 Manager::remove_port (string name)
129 PortMap::iterator res;
131 if ((res = ports_by_device.find (name)) == ports_by_device.end()) {
135 ports_by_device.erase (res);
136 ports_by_device.erase ((*res).second->name());
138 delete (*res).second;
144 Manager::port (string name)
146 PortMap::iterator res;
148 for (res = ports_by_tag.begin(); res != ports_by_tag.end(); res++) {
149 if (name == (*res).first) {
150 return (*res).second;
158 Manager::port (size_t portnum)
161 PortMap::iterator res;
163 for (res = ports_by_tag.begin(); res != ports_by_tag.end(); res++) {
164 if ((*res).second->number() == portnum) {
165 return (*res).second;
173 Manager::foreach_port (int (*func)(const Port &, size_t, void *),
177 PortMap::const_iterator i;
181 for (n = 0, i = ports_by_device.begin();
182 i != ports_by_device.end(); i++, n++) {
184 if ((retval = func (*((*i).second), n, arg)) != 0) {
193 Manager::parse_port_request (string str, Port::Type type)
196 string::size_type colon;
199 if (str.length() == 0) {
200 error << "MIDI: missing port specification" << endmsg;
204 /* Port specifications look like:
208 devicename:tagname:mode
212 "devicename" is the full path to the requested file
214 "tagname" (optional) is the name used to refer to the
215 port. If not given, g_path_get_basename (devicename)
218 "mode" (optional) is either "r" or "w" or something else.
219 if it is "r", the port will be opened
220 read-only, if "w", the port will be opened
221 write-only. Any other value, or no mode
222 specification at all, will cause the port to
223 be opened for reading and writing.
226 req = new PortRequest;
227 colon = str.find_first_of (':');
229 if (colon != string::npos) {
230 req->devname = strdup (str.substr (0, colon).c_str());
232 req->devname = strdup (str.c_str());
235 if (colon < str.length()) {
237 tag = str.substr (colon+1);
239 /* see if there is a mode specification in the tag part */
241 colon = tag.find_first_of (':');
243 if (colon != string::npos) {
246 req->tagname = strdup (tag.substr (0, colon).c_str());
248 modestr = tag.substr (colon+1);
249 if (modestr == "r") {
250 req->mode = O_RDONLY;
251 } else if (modestr == "w") {
252 req->mode = O_WRONLY;
258 req->tagname = strdup (tag.c_str());
263 req->tagname = g_path_get_basename (req->devname);
269 if (MIDI::Manager::instance()->add_port (*req) == 0) {
277 Manager::cycle_start(nframes_t nframes)
279 for (PortMap::iterator i = ports_by_device.begin();
280 i != ports_by_device.end(); i++)
281 (*i).second->cycle_start(nframes);
287 for (PortMap::iterator i = ports_by_device.begin();
288 i != ports_by_device.end(); i++)
289 (*i).second->cycle_end();