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 (!PortFactory::ignore_duplicate_devices (req.type)) {
77 if ((existing = ports_by_device.find (req.devname)) != ports_by_device.end()) {
79 port = (*existing).second;
81 if (port->mode() == req.mode) {
83 /* Same mode - reuse the port, and just
84 create a new tag entry.
87 newpair.first = req.tagname;
88 newpair.second = port;
90 ports_by_tag.insert (newpair);
94 /* If the existing is duplex, and this request
95 is not, then fail, because most drivers won't
96 allow opening twice with duplex and non-duplex
100 if ((req.mode == O_RDWR && port->mode() != O_RDWR) ||
101 (req.mode != O_RDWR && port->mode() == O_RDWR)) {
102 error << "MIDIManager: port tagged \""
104 << "\" cannot be opened duplex and non-duplex"
109 /* modes must be different or complementary */
113 port = factory.create_port (req);
124 newpair.first = port->name();
125 newpair.second = port;
126 ports_by_tag.insert (newpair);
128 newpair.first = port->device();
129 newpair.second = port;
130 ports_by_device.insert (newpair);
132 /* first port added becomes the default input
136 if (inputPort == 0) {
140 if (outputPort == 0) {
148 Manager::remove_port (string name)
150 PortMap::iterator res;
152 if ((res = ports_by_device.find (name)) == ports_by_device.end()) {
156 ports_by_device.erase (res);
157 ports_by_device.erase ((*res).second->name());
159 delete (*res).second;
165 Manager::set_input_port (string tag)
167 PortMap::iterator res;
170 for (res = ports_by_tag.begin(); res != ports_by_tag.end(); res++) {
171 if (tag == (*res).first) {
181 inputPort = (*res).second;
187 Manager::set_input_port (size_t portnum)
190 PortMap::iterator res;
192 for (res = ports_by_tag.begin(); res != ports_by_tag.end(); res++) {
193 if ((*res).second->number() == portnum) {
194 inputPort = (*res).second;
203 Manager::set_output_port (string tag)
206 PortMap::iterator res;
209 for (res = ports_by_tag.begin(); res != ports_by_tag.end(); res++) {
210 if (tag == (*res).first) {
220 // XXX send a signal to say we're about to change output ports
223 for (channel_t chan = 0; chan < 16; chan++) {
224 outputPort->channel (chan)->all_notes_off ();
227 outputPort = (*res).second;
229 // XXX send a signal to say we've changed output ports
235 Manager::set_output_port (size_t portnum)
238 PortMap::iterator res;
240 for (res = ports_by_tag.begin(); res != ports_by_tag.end(); res++) {
241 if ((*res).second->number() == portnum) {
242 outputPort = (*res).second;
251 Manager::port (string name)
253 PortMap::iterator res;
255 for (res = ports_by_tag.begin(); res != ports_by_tag.end(); res++) {
256 if (name == (*res).first) {
257 return (*res).second;
265 Manager::port (size_t portnum)
268 PortMap::iterator res;
270 for (res = ports_by_tag.begin(); res != ports_by_tag.end(); res++) {
271 if ((*res).second->number() == portnum) {
272 return (*res).second;
280 Manager::foreach_port (int (*func)(const Port &, size_t, void *),
284 PortMap::const_iterator i;
288 for (n = 0, i = ports_by_device.begin();
289 i != ports_by_device.end(); i++, n++) {
291 if ((retval = func (*((*i).second), n, arg)) != 0) {
300 Manager::parse_port_request (string str, Port::Type type)
303 string::size_type colon;
306 if (str.length() == 0) {
307 error << "MIDI: missing port specification" << endmsg;
311 /* Port specifications look like:
315 devicename:tagname:mode
319 "devicename" is the full path to the requested file
321 "tagname" (optional) is the name used to refer to the
322 port. If not given, g_path_get_basename (devicename)
325 "mode" (optional) is either "r" or "w" or something else.
326 if it is "r", the port will be opened
327 read-only, if "w", the port will be opened
328 write-only. Any other value, or no mode
329 specification at all, will cause the port to
330 be opened for reading and writing.
333 req = new PortRequest;
334 colon = str.find_first_of (':');
336 if (colon != string::npos) {
337 req->devname = strdup (str.substr (0, colon).c_str());
339 req->devname = strdup (str.c_str());
342 if (colon < str.length()) {
344 tag = str.substr (colon+1);
346 /* see if there is a mode specification in the tag part */
348 colon = tag.find_first_of (':');
350 if (colon != string::npos) {
353 req->tagname = strdup (tag.substr (0, colon).c_str());
355 modestr = tag.substr (colon+1);
356 if (modestr == "r") {
357 req->mode = O_RDONLY;
358 } else if (modestr == "w") {
359 req->mode = O_WRONLY;
365 req->tagname = strdup (tag.c_str());
370 // check when tagname is freed
371 req->tagname = g_path_get_basename (req->devname);
377 if (MIDI::Manager::instance()->add_port (*req) == 0) {
385 Manager::get_known_ports (vector<PortSet>& ports)
387 return PortFactory::get_known_ports (ports);