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 (string name)
131 PortMap::iterator res;
133 if ((res = ports_by_device.find (name)) == ports_by_device.end()) {
137 ports_by_device.erase (res);
138 ports_by_device.erase ((*res).second->name());
140 delete (*res).second;
146 Manager::port (string name)
148 PortMap::iterator res;
150 for (res = ports_by_tag.begin(); res != ports_by_tag.end(); res++) {
151 if (name == (*res).first) {
152 return (*res).second;
160 Manager::port (size_t portnum)
163 PortMap::iterator res;
165 for (res = ports_by_tag.begin(); res != ports_by_tag.end(); res++) {
166 if ((*res).second->number() == portnum) {
167 return (*res).second;
175 Manager::foreach_port (int (*func)(const Port &, size_t, void *),
179 PortMap::const_iterator i;
183 for (n = 0, i = ports_by_device.begin();
184 i != ports_by_device.end(); i++, n++) {
186 if ((retval = func (*((*i).second), n, arg)) != 0) {
195 Manager::parse_port_request (string str, Port::Type type)
198 string::size_type colon;
201 if (str.length() == 0) {
202 error << "MIDI: missing port specification" << endmsg;
206 /* Port specifications look like:
210 devicename:tagname:mode
214 "devicename" is the full path to the requested file
216 "tagname" (optional) is the name used to refer to the
217 port. If not given, g_path_get_basename (devicename)
220 "mode" (optional) is either "r" or "w" or something else.
221 if it is "r", the port will be opened
222 read-only, if "w", the port will be opened
223 write-only. Any other value, or no mode
224 specification at all, will cause the port to
225 be opened for reading and writing.
228 req = new PortRequest;
229 colon = str.find_first_of (':');
231 if (colon != string::npos) {
232 req->devname = strdup (str.substr (0, colon).c_str());
234 req->devname = strdup (str.c_str());
237 if (colon < str.length()) {
239 tag = str.substr (colon+1);
241 /* see if there is a mode specification in the tag part */
243 colon = tag.find_first_of (':');
245 if (colon != string::npos) {
248 req->tagname = strdup (tag.substr (0, colon).c_str());
250 modestr = tag.substr (colon+1);
251 if (modestr == "r") {
252 req->mode = O_RDONLY;
253 } else if (modestr == "w") {
254 req->mode = O_WRONLY;
260 req->tagname = strdup (tag.c_str());
265 req->tagname = g_path_get_basename (req->devname);
271 if (MIDI::Manager::instance()->add_port (*req) == 0) {
279 Manager::cycle_start(nframes_t nframes)
281 for (PortMap::iterator i = ports_by_device.begin();
282 i != ports_by_device.end(); i++)
283 (*i).second->cycle_start(nframes);
289 for (PortMap::iterator i = ports_by_device.begin();
290 i != ports_by_device.end(); i++)
291 (*i).second->cycle_end();